View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   package com.rc.celeritas.query;
6   
7   import java.util.ArrayList;
8   import java.util.HashMap;
9   import java.util.Hashtable;
10  import org.apache.commons.lang.StringUtils;
11  import org.apache.log4j.Logger;
12  
13  /**
14   *
15   * @author rchoudhary
16   */
17  public class QueryHelper {
18  
19      private static Logger log = Logger.getLogger(QueryHelper.class);
20  
21      /**
22       *
23       * @param order_by
24       * @return
25       */
26      public static String buildOrderBy(String order_by) {
27          if (order_by != null && !order_by.trim().equals("")) {
28              return QueryConstants.ORDER_BY + order_by;
29          } else {
30              return QueryConstants.BLANK;
31          }
32      }
33  
34      /**
35       *
36       * @param table
37       * @return
38       */
39      public static String buildQuerySQL(String table) {
40          return QueryConstants.QUERY_PREFIX + table;
41      }
42  
43      /**
44       *
45       * @param table
46       * @param pk
47       * @param pVal
48       * @return
49       */
50      public static String createDeleteSQL(String table, String pk, String pVal) {
51          StringBuffer deleteSQL = new StringBuffer(QueryConstants.DELETE_FROM);
52          deleteSQL.append(table);
53          deleteSQL.append(QueryConstants.WHERE);
54          deleteSQL.append(pk);
55          deleteSQL.append(QueryConstants.EQUALS);
56          deleteSQL.append(QueryConstants.QUOTE);
57          deleteSQL.append(pVal);
58          deleteSQL.append(QueryConstants.QUOTE);
59          return deleteSQL.toString();
60      }
61  
62      /**
63       *
64       * @param table
65       * @param columns
66       * @param rMap
67       * @param pk
68       * @param pVal
69       * @return
70       */
71      public static synchronized String createUpdateSQL(String table, ArrayList<Column> columns, HashMap rMap, String pk, String pVal) {
72          StringBuffer updateDSQL = new StringBuffer(QueryConstants.UPDATE);
73          updateDSQL.append(table);
74          updateDSQL.append(QueryConstants.SET);
75          for (int i = 0; i < columns.size(); i++) {
76              if (i > 0) {
77                  updateDSQL.append(QueryConstants.COMA);
78              }
79              String column = columns.get(i).toString();
80              updateDSQL.append(column);
81              updateDSQL.append(" = '");
82              updateDSQL.append(rMap.get(column));
83              updateDSQL.append(QueryConstants.QUOTE);
84          }
85          updateDSQL.append(QueryConstants.WHERE);
86          updateDSQL.append(pk);
87          updateDSQL.append(" = '");
88          updateDSQL.append(pVal);
89          updateDSQL.append(QueryConstants.QUOTE);
90          return updateDSQL.toString();
91      }
92  
93      /**
94       * 
95       * @param table
96       * @param columns
97       * @param rMap
98       * @return
99       */
100     public static synchronized String createInsertSQL(String table, ArrayList<Column> columns, Hashtable rMap) {
101         StringBuffer insertDSQL = new StringBuffer(QueryConstants.INSERT_INTO);
102         StringBuffer values = new StringBuffer(QueryConstants.VALUES);
103         insertDSQL.append(table);
104         insertDSQL.append(QueryConstants.O_BRACE);
105         for (int i = 0; i < columns.size(); i++) {
106             if (i > 0) {
107                 insertDSQL.append(QueryConstants.COMA);
108                 values.append(QueryConstants.COMA);
109             }
110             Column column = columns.get(i);
111             insertDSQL.append(column.getColumnName());
112             if(StringUtils.isEmpty((String)rMap.get(column.getColumnName()))){
113                 values.append(QueryConstants.NULL);
114             }else{
115                 values.append(QueryConstants.QUOTE);
116                 if(StringUtils.isNotEmpty(column.getColumnType()) && column.getColumnType().equals("DATE")){
117                     values.append(rMap.get(column.getColumnName()));
118                 }else{
119                     values.append(rMap.get(column.getColumnName()));
120                 }
121                 values.append(QueryConstants.QUOTE);
122             }
123         }
124         insertDSQL.append(QueryConstants.C_BRACE);
125         insertDSQL.append(values);
126         insertDSQL.append(QueryConstants.C_BRACE);
127         return insertDSQL.toString();
128     }
129 
130     /**
131      * 
132      * @param table
133      * @param pk
134      * @param pVal
135      * @return
136      */
137     public static String buildQuerySQLWithPKey(String table, String pk, String pVal) {
138         StringBuffer sb = new StringBuffer(QueryConstants.QUERY_PREFIX);
139         sb.append(table);
140         sb.append(QueryConstants.WHERE);
141         sb.append(pk);
142         sb.append(QueryConstants.EQUALS);
143         sb.append(QueryConstants.QUOTE);
144         sb.append(pVal);
145         sb.append(QueryConstants.QUOTE);
146         return sb.toString();
147     }
148 }