View Javadoc

1   /*
2    *******************************************************************************
3    * Copyright (c) 2005 Chris Rose and AIMedia
4    * All rights reserved. SQLSelectableStatement and the accompanying materials
5    * are made available under the terms of the Common Public License v1.0
6    * which accompanies this distribution, and is available at
7    * http://www.eclipse.org/legal/cpl-v10.html
8    * 
9    * Contributors:
10   *     Chris Rose
11   *******************************************************************************/
12  package ca.spaz.sql;
13  
14  import java.util.ArrayList;
15  
16  /***
17   * An abstract class encapsulating capabilities that are consistent across all selecting SQL
18   * statements.
19   *  
20   * @author Chris Rose
21   */
22  public abstract class SQLSelectableStatement extends SQLStatement {
23      
24      private boolean and;
25  
26     protected SQLSelectableStatement(String table, boolean and, boolean querySupport, boolean executeSupport) {
27          super(table, querySupport, executeSupport);
28          this.and = and;
29      }
30  
31      private final ArrayList where = new ArrayList();
32      public static final String EQ = "=";
33      public static final String GT = ">";
34      public static final String LT = "<";
35  
36      /***
37       * Added a WHERE constraint to the SELECT command.
38       * 
39       * @param name
40       *            the field to constrain
41       * @param val
42       *            the value this field must equal as a constraint
43       */
44      public void addWhere(String name, Object val) {
45          addWhere(name, EQ, val);
46      }
47  
48      public void addWhere(String name, String operator, Object val) {
49          where.add("upper(" + name + ") " + operator + " '"
50                  + escape(fixClass(val).toString()).toUpperCase() + "' ");
51      }
52      
53      public void addWhere(String name, int val) {
54          addWhere(name, EQ, val);
55      }
56      
57      public void addWhere(String name, String op, int val) {
58          addWhere(name, op, new Integer(val));
59      }
60  
61      public void addWhere(String name, double val) {
62          addWhere(name, EQ, val);
63      }
64      
65      public void addWhere(String name, String op, double val) {
66          addWhere(name, op, new Double(val));
67      }
68  
69      public void addWhere(String name, char val) {
70          addWhere(name, EQ, val);
71      }
72      
73      public void addWhere(String name, String op, char val) {
74          addWhere(name, op, new Character(val));
75      }
76  
77      /***
78       * Added a WHERE constraint to the SELECT command.
79       * 
80       * @param name
81       *            the field to constrain
82       * @param val
83       *            the value this field must be like
84       */
85      public void addWhereLike(String name, String val) {
86          where.add("upper(" + name + ") like '"
87                  + escape(val.toString()).toUpperCase() + "' ");
88      }
89  
90      /***
91       */
92      protected String getWhere() {
93          StringBuffer sb = new StringBuffer();
94          if (where.size() > 0) {
95              sb.append(" WHERE ");
96              for (int i = 0; i < where.size(); i++) {
97                  Object w = where.get(i);
98                  sb.append(w.toString());
99                  if (i < where.size() - 1) {
100                     sb.append(and ? " AND " : " OR ");
101                 }
102             }
103         }
104         return sb.toString();
105     }
106 
107 }