001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.commons.dsi.dml;
020:
021: import java.util.*;
022:
023: import org.openharmonise.commons.dsi.*;
024:
025: /**
026: * This class represents a where condition which can be included in a DML statement.
027: *
028: * Note: the default operator is the '=' operator.
029: *
030: * @author Michael Bell
031: * @version $Revision: 1.1 $
032: *
033: */
034: public class WhereCondition extends Object {
035:
036: /**
037: * An array of valid operators
038: */
039: static private final String[] saValidOperators = { "<>", ">", "<",
040: "=", ">=", "<=", "!=", "IN", "CONTAINS", "BETWEEN", "LIKE",
041: "STARTS_WITH", "NOT IN", "NULL", "is", "is not" }; //TODO this list should be data store dependant...
042:
043: /**
044: * The column reference.
045: */
046: private ColumnRef m_colref = null;
047:
048: /**
049: * The comparison operator.
050: */
051: private String m_sOp = null;
052:
053: /**
054: * The list of values to be compared.
055: */
056: private List m_value = null;
057:
058: /**
059: * The collection of join conditions to be associaged with this where condition.
060: */
061: private JoinConditions m_associated_joins = null;
062:
063: /**.
064: * Constructs a where condition from the given parameters
065: *
066: * @param colref the condition column reference
067: * @param val the condition value
068: * @throws DataStoreException if the data for the condition is somehow invalid
069: */
070: public WhereCondition(ColumnRef colref, int val)
071: throws DataStoreException {
072: Vector vec = new Vector();
073: vec.add(new Integer(val));
074: addColumnRef(colref);
075: addOperator("=");
076: addValue(vec);
077: }
078:
079: /**
080: * Constructs a where condition from the given parameters.
081: *
082: * @param colref the condition column reference
083: * @param sOp the comparison operator
084: * @param val the condition value
085: * @throws DataStoreException if the data for the condition is somehow invalid
086: */
087: public WhereCondition(ColumnRef colref, String sOp, int val)
088: throws DataStoreException {
089: Vector vec = new Vector();
090: vec.add(new Integer(val));
091: addColumnRef(colref);
092: addOperator(sOp);
093: addValue(vec);
094: }
095:
096: /**
097: * Constructs a where condition from the given parameters.
098: *
099: * @param colref the condition column reference
100: * @param val the condition value
101: * @throws DataStoreException if the data for the condition is somehow invalid
102: */
103: public WhereCondition(ColumnRef colref, Object val)
104: throws DataStoreException {
105:
106: Vector vec = new Vector();
107: vec.add(val);
108: addColumnRef(colref);
109: addOperator("=");
110: addValue(vec);
111: }
112:
113: /**
114: * Constructs a where condition from the given parameters.
115: *
116: * @param colref the condition column reference
117: * @param sOp the comparison operator
118: * @param val the condition value
119: * @throws DataStoreException if the data for the condition is somehow invalid
120: */
121: public WhereCondition(ColumnRef colref, String sOp, Object val)
122: throws DataStoreException {
123:
124: Vector vec = new Vector();
125: vec.add(val);
126: addColumnRef(colref);
127: addOperator(sOp);
128: addValue(vec);
129: }
130:
131: /**
132: * Constructs a where condition from the given parameters.
133: *
134: * @param colref the condition column reference
135: * @param val the condition value
136: * @throws DataStoreException if the data for the condition is somehow invalid
137: */
138: public WhereCondition(ColumnRef colref, Vector val)
139: throws DataStoreException {
140: addColumnRef(colref);
141: addOperator("=");
142: addValue(val);
143: }
144:
145: /**
146: * Constructs a where condition from the given parameters.
147: *
148: * @param colref the condition column reference
149: * @param sOp the comparison operator
150: * @param val the list of condition values
151: * @throws DataStoreException
152: */
153: public WhereCondition(ColumnRef colref, String sOp, List val)
154: throws DataStoreException {
155: boolean bfound = false;
156:
157: //check for valid operator
158: int i = 0;
159:
160: while ((i < saValidOperators.length) && !bfound) {
161: if (sOp.equalsIgnoreCase(saValidOperators[i])) {
162: bfound = true;
163: }
164:
165: i++;
166: }
167:
168: if (!bfound) {
169: throw new DataStoreException("Invalid Search Operator=" + i
170: + sOp + ":");
171: }
172:
173: m_colref = colref;
174: m_sOp = sOp;
175: m_value = val;
176: }
177:
178: /**
179: * Adds the column reference for this condition.
180: *
181: * @param colref the column reference
182: */
183: private void addColumnRef(ColumnRef colref) {
184: m_colref = colref;
185: }
186:
187: /**
188: * Adds the comparison operator for this condition.
189: *
190: * @param sOp the comparison operator
191: *
192: * @throws DataStoreException if the operator is invalid
193: */
194: private void addOperator(String sOp) throws DataStoreException {
195: boolean bfound = false;
196:
197: //check for valid operator
198: int i = 0;
199:
200: while ((i < saValidOperators.length) && !bfound) {
201: if (sOp.equalsIgnoreCase(saValidOperators[i])) {
202: bfound = true;
203: }
204:
205: i++;
206: }
207:
208: if (!bfound) {
209: throw new DataStoreException("Invalid Search Operator=" + i
210: + " - " + 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 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 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 getTableName() {
272: String 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 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: }
|