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: * Abstract class providing base to implementations of data manipulation language
027: * statement implementations.
028: *
029: * @author Michael Bell
030: * @version $Revision: 1.1 $
031: *
032: */
033: public abstract class AbstractDMLStatement {
034:
035: /**
036: * 'where' clause conditions attached to this statement.
037: */
038: protected WhereConditionGroup m_where = null;
039:
040: /**
041: * List of tables which have alias names, indexes match with <code>m_TableAliases</code>.
042: */
043: protected Vector m_Tables = null;
044:
045: /**
046: * List of table aliases, indexes match with <code>m_Tables</code>.
047: */
048: protected Vector m_TableAliases = null;
049:
050: /**
051: * Flag which indicates whether all column refs have checked for aliases.
052: */
053: protected boolean m_bIsAliasListComplete = false;
054:
055: /**
056: * Basic constructor.
057: *
058: */
059: protected AbstractDMLStatement() {
060: }
061:
062: /**
063: * Add a table alias to this statment.
064: *
065: * @param sTable the table name
066: * @param sAlias the alias
067: * @throws DataStoreException if the alias already exists
068: */
069: public void addTableAlias(String sTable, String sAlias)
070: throws DataStoreException {
071: if (m_Tables == null) {
072: m_Tables = new Vector(16);
073: m_TableAliases = new Vector(16);
074: }
075:
076: if (m_TableAliases.contains(sAlias)) {
077: throw new DataStoreException("Alias already exists");
078: }
079:
080: m_Tables.add(sTable);
081: m_TableAliases.add(sAlias);
082: }
083:
084: /**
085: * Get table alias.
086: *
087: * @param sTable the table name
088: * @return the alias for the given table name
089: */
090: public String getTableAlias(String sTable) {
091: String sReturn = null;
092:
093: int index = m_Tables.indexOf(sTable);
094:
095: if (index >= 0) {
096: sReturn = (String) m_TableAliases.elementAt(index);
097: }
098:
099: return sReturn;
100: }
101:
102: /**
103: * Returns <code>true</code> if the given <code>String</code> is an alias
104: * for this statement.
105: *
106: * @param sAlias the alias
107: * @return <code>true</code> if the given alias is an alias in this statement
108: * @throws DataStoreException if an error occurs while processing column refs for aliases
109: */
110: public boolean isAlias(String sAlias) throws DataStoreException {
111: boolean bIsAlias = false;
112:
113: if (m_TableAliases != null) {
114: bIsAlias = m_TableAliases.contains(sAlias);
115: }
116:
117: if (bIsAlias == false && m_bIsAliasListComplete == false
118: && m_where != null) {
119: List cols = m_where.getColumnRefs();
120:
121: Iterator iter = cols.iterator();
122:
123: while (iter.hasNext()) {
124: ColumnRef col = (ColumnRef) iter.next();
125:
126: if (col.hasTableAlias() == true) {
127: addTableAlias(col.getTable(), col.getTableAlias());
128: }
129: }
130:
131: m_bIsAliasListComplete = true;
132: if (m_TableAliases != null) {
133: bIsAlias = m_TableAliases.contains(sAlias);
134: }
135: }
136:
137: return bIsAlias;
138: }
139:
140: /**
141: * Get table name from alias.
142: *
143: * @param sAlias the alias
144: * @return the table name
145: */
146: public String getTableName(String sAlias) {
147: String sReturn = null;
148:
149: int index = m_TableAliases.indexOf(sAlias);
150:
151: if (index >= 0) {
152: sReturn = (String) m_Tables.elementAt(index);
153: }
154:
155: return sReturn;
156: }
157:
158: /**
159: * Add a where condition to this statement.
160: *
161: * @param colref the column ref the condition applies to
162: * @param sOperator the comparison operator to be used in the condition
163: * @param val the value to be used in the comparison of the condition
164: * @throws DataStoreException if the condition can not be added
165: */
166: public void addWhereCondition(ColumnRef colref, String sOperator,
167: int val) throws DataStoreException {
168: Vector vec = new Vector(1);
169: vec.add(new Integer(val));
170:
171: addWhereCondition(colref, sOperator, vec);
172: }
173:
174: /**
175: * Add a where condition to this statement.
176: *
177: * @param colref the column ref the condition applies to
178: * @param sOperator the comparison operator to be used in the condition
179: * @param val the value to be used in the comparison of the condition
180: *
181: * @throws DataStoreException if the condition can not be added
182: */
183: public void addWhereCondition(ColumnRef colref, String sOperator,
184: Object val) throws DataStoreException {
185: Vector vec = new Vector(1);
186: vec.add(val);
187:
188: addWhereCondition(colref, sOperator, vec);
189: }
190:
191: /**
192: * Add a where condition to this statement.
193: *
194: * @param colref the column ref the condition applies to
195: * @param sOperator the comparison operator to be used in the condition
196: * @param vals the list of values to be used in the comparison
197: * @throws DataStoreException if the condition can not be added
198: */
199: public void addWhereCondition(ColumnRef colref, String sOperator,
200: List vals) throws DataStoreException {
201: if (m_where == null) {
202: m_where = new WhereConditionGroup();
203: }
204:
205: if (vals == null) {
206: vals = new Vector();
207: vals.add(null);
208: }
209:
210: m_where.addCondition(colref, sOperator, vals);
211: }
212:
213: /**
214: * Adds a collection of where conditions as a where condition to this statement.
215: *
216: * @param where a collection of where conditions
217: * @throws DataStoreException if the condition cannot be added
218: */
219: public void addWhereCondition(WhereConditionGroup where)
220: throws DataStoreException {
221: if (m_where == null) {
222: m_where = new WhereConditionGroup();
223: }
224:
225: m_where.addCondition(where);
226: }
227:
228: /**
229: * Adds a where condition to this statement.
230: *
231: * @param where the where condition to add
232: * @throws DataStoreException if the condition cannot be added
233: */
234: public void addWhereCondition(WhereCondition where)
235: throws DataStoreException {
236: if (m_where == null) {
237: m_where = new WhereConditionGroup();
238: }
239:
240: m_where.addCondition(where);
241: }
242:
243: /**
244: * Sets the where condition stringing operator (AND/OR).
245: *
246: * @param sOp the stringing operator
247: * @throws DataStoreException if the stringing operator is invalid
248: */
249: public void setWhereConditionStringingOperator(String sOp)
250: throws DataStoreException {
251: m_where.setStringingOperator(sOp);
252: }
253:
254: /**
255: * Returns <code>true</code> if a where clause has been defined
256: *
257: * @return <code>true</code> if a where clause has been defined
258: */
259: public boolean hasWhereClause() {
260: return (m_where != null);
261: }
262:
263: /**
264: * Returns <code>true</code> if this statement has where conditions
265: *
266: * @return <code>true</code> if this statement has where conditions
267: */
268: public boolean hasWhereConditions() {
269: if ((m_where == null) || (m_where.size() == 0)) {
270: return false;
271: } else {
272: return true;
273: }
274: }
275:
276: /**
277: * Returns the where conditions for this statement
278: * @return the where conditions for this statement
279: */
280: public WhereConditionGroup getWhereConditions() {
281: return m_where;
282: }
283:
284: /**
285: * Clears all data in this statement
286: *
287: */
288: public void clear() {
289: m_where = null;
290:
291: if (m_Tables != null) {
292: m_Tables.removeAllElements();
293: m_TableAliases.removeAllElements();
294: }
295: }
296: }
|