KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > commons > dsi > dml > WhereCondition


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.commons.dsi.dml;
20
21
22 import java.util.*;
23
24 import org.openharmonise.commons.dsi.*;
25
26 /**
27  * This class represents a where condition which can be included in a DML statement.
28  *
29  * Note: the default operator is the '=' operator.
30  *
31  * @author Michael Bell
32  * @version $Revision: 1.1 $
33  *
34  */

35 public class WhereCondition extends Object JavaDoc {
36     
37     /**
38      * An array of valid operators
39      */

40     static private final String JavaDoc[] saValidOperators = {
41         "<>", ">", "<", "=", ">=", "<=", "!=", "IN", "CONTAINS", "BETWEEN",
42         "LIKE", "STARTS_WITH", "NOT IN", "NULL", "is", "is not"
43     }; //TODO this list should be data store dependant...
44

45     /**
46      * The column reference.
47      */

48     private ColumnRef m_colref = null;
49     
50     /**
51      * The comparison operator.
52      */

53     private String JavaDoc m_sOp = null;
54     
55     /**
56      * The list of values to be compared.
57      */

58     private List m_value = null;
59    
60     /**
61      * The collection of join conditions to be associaged with this where condition.
62      */

63     private JoinConditions m_associated_joins = null;
64
65     /**.
66      * Constructs a where condition from the given parameters
67      *
68      * @param colref the condition column reference
69      * @param val the condition value
70      * @throws DataStoreException if the data for the condition is somehow invalid
71      */

72     public WhereCondition(ColumnRef colref, int val) throws DataStoreException {
73         Vector vec = new Vector();
74         vec.add(new Integer JavaDoc(val));
75         addColumnRef(colref);
76         addOperator("=");
77         addValue(vec);
78     }
79
80     /**
81      * Constructs a where condition from the given parameters.
82      *
83      * @param colref the condition column reference
84      * @param sOp the comparison operator
85      * @param val the condition value
86      * @throws DataStoreException if the data for the condition is somehow invalid
87      */

88     public WhereCondition(ColumnRef colref, String JavaDoc sOp, int val)
89                    throws DataStoreException {
90         Vector vec = new Vector();
91         vec.add(new Integer JavaDoc(val));
92         addColumnRef(colref);
93         addOperator(sOp);
94         addValue(vec);
95     }
96
97     /**
98      * Constructs a where condition from the given parameters.
99      *
100      * @param colref the condition column reference
101      * @param val the condition value
102      * @throws DataStoreException if the data for the condition is somehow invalid
103      */

104     public WhereCondition(ColumnRef colref, Object JavaDoc val)
105                    throws DataStoreException {
106
107         Vector vec = new Vector();
108         vec.add(val);
109         addColumnRef(colref);
110         addOperator("=");
111         addValue(vec);
112     }
113
114     /**
115      * Constructs a where condition from the given parameters.
116      *
117      * @param colref the condition column reference
118      * @param sOp the comparison operator
119      * @param val the condition value
120      * @throws DataStoreException if the data for the condition is somehow invalid
121      */

122     public WhereCondition(ColumnRef colref, String JavaDoc sOp, Object JavaDoc val)
123                    throws DataStoreException {
124
125         Vector vec = new Vector();
126         vec.add(val);
127         addColumnRef(colref);
128         addOperator(sOp);
129         addValue(vec);
130     }
131
132     /**
133      * Constructs a where condition from the given parameters.
134      *
135      * @param colref the condition column reference
136      * @param val the condition value
137      * @throws DataStoreException if the data for the condition is somehow invalid
138      */

139     public WhereCondition(ColumnRef colref, Vector val)
140                    throws DataStoreException {
141         addColumnRef(colref);
142         addOperator("=");
143         addValue(val);
144     }
145
146     /**
147      * Constructs a where condition from the given parameters.
148      *
149      * @param colref the condition column reference
150      * @param sOp the comparison operator
151      * @param val the list of condition values
152      * @throws DataStoreException
153      */

154     public WhereCondition(ColumnRef colref, String JavaDoc sOp, List val)
155                    throws DataStoreException {
156         boolean bfound = false;
157
158         //check for valid operator
159
int i = 0;
160
161         while ((i < saValidOperators.length) && !bfound) {
162             if (sOp.equalsIgnoreCase(saValidOperators[i])) {
163                 bfound = true;
164             }
165
166             i++;
167         }
168
169         if (!bfound) {
170             throw new DataStoreException("Invalid Search Operator=" + i +
171                                          sOp + ":");
172         }
173
174         m_colref = colref;
175         m_sOp = sOp;
176         m_value = val;
177     }
178
179     /**
180      * Adds the column reference for this condition.
181      *
182      * @param colref the column reference
183      */

184     private void addColumnRef(ColumnRef colref) {
185         m_colref = colref;
186     }
187
188     /**
189      * Adds the comparison operator for this condition.
190      *
191      * @param sOp the comparison operator
192      *
193      * @throws DataStoreException if the operator is invalid
194      */

195     private void addOperator(String JavaDoc sOp) throws DataStoreException {
196         boolean bfound = false;
197
198         //check for valid operator
199
int i = 0;
200
201         while ((i < saValidOperators.length) && !bfound) {
202             if (sOp.equalsIgnoreCase(saValidOperators[i])) {
203                 bfound = true;
204             }
205
206             i++;
207         }
208
209         if (!bfound) {
210             throw new DataStoreException("Invalid Search Operator=" + i + " - " + sOp);
211         }
212
213         m_sOp = sOp;
214     }
215     
216     /**
217      * Adds the conditional value to this condition.
218      *
219      * @param val the conditional value
220      */

221     private void addValue(List val) {
222         if(val == null) {
223             m_value = new Vector();
224             m_value.add(null);
225         } else {
226             m_value = val;
227         }
228     }
229
230     /**
231      * Returns the column reference for this condition.
232      *
233      * @return the condition column reference
234      */

235     public ColumnRef getColumnRef() {
236         return m_colref;
237     }
238
239     /**
240      * Returns the comparison operator for this condition.
241      *
242      * @return the comparison operator
243      */

244     public String JavaDoc getOperator() {
245         return m_sOp;
246     }
247     
248     /**
249      * Returns the list of conditional values for this condition.
250      *
251      * @return the list of conditional values
252      */

253     public List getValues() {
254         return m_value;
255     }
256
257     /**
258      * Returns the column name for this condition.
259      *
260      * @return the column name
261      */

262     public String JavaDoc getColumnName() {
263         return (m_colref.getColumn());
264     }
265
266     /**
267      * Returns the table name for this condition.
268      *
269      * @return the table name
270      */

271     public String JavaDoc getTableName() {
272         String JavaDoc sTable = m_colref.getTable();
273         
274         if(m_colref.hasTableAlias() == true) {
275             sTable = m_colref.getTableAlias();
276         }
277         
278         return sTable;
279     }
280
281     /**
282      * Returns the full string representation for the column reference for condition.
283      *
284      * @return the full string representation for the column reference
285      */

286     public String JavaDoc getFullColumnRef() {
287         return (m_colref.getFullRef());
288     }
289     
290     /**
291      * Adds join conditions which are associated with this condition.
292      *
293      * @param joins the join conditions
294      */

295     public void addAssociatedJoinConditions(JoinConditions joins) {
296         m_associated_joins = joins;
297     }
298     
299     /**
300      * Returns the join conditions that are associated with this condition.
301      *
302      * @return the join conditions
303      */

304     public JoinConditions getAssociatedJoinConditions() {
305         return m_associated_joins;
306     }
307 }
Popular Tags