KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Abstract class providing base to implementations of data manipulation language
28  * statement implementations.
29  *
30  * @author Michael Bell
31  * @version $Revision: 1.1 $
32  *
33  */

34 public abstract class AbstractDMLStatement {
35     
36     /**
37      * 'where' clause conditions attached to this statement.
38      */

39     protected WhereConditionGroup m_where = null;
40     
41     /**
42      * List of tables which have alias names, indexes match with <code>m_TableAliases</code>.
43      */

44     protected Vector m_Tables = null;
45     
46     /**
47      * List of table aliases, indexes match with <code>m_Tables</code>.
48      */

49     protected Vector m_TableAliases = null;
50     
51     /**
52      * Flag which indicates whether all column refs have checked for aliases.
53      */

54     protected boolean m_bIsAliasListComplete = false;
55
56     /**
57      * Basic constructor.
58      *
59      */

60     protected AbstractDMLStatement() {
61     }
62
63     /**
64      * Add a table alias to this statment.
65      *
66      * @param sTable the table name
67      * @param sAlias the alias
68      * @throws DataStoreException if the alias already exists
69      */

70     public void addTableAlias(String JavaDoc sTable, String JavaDoc sAlias)
71         throws DataStoreException {
72         if (m_Tables == null) {
73             m_Tables = new Vector(16);
74             m_TableAliases = new Vector(16);
75         }
76
77         if (m_TableAliases.contains(sAlias)) {
78             throw new DataStoreException("Alias already exists");
79         }
80
81         m_Tables.add(sTable);
82         m_TableAliases.add(sAlias);
83     }
84
85     /**
86      * Get table alias.
87      *
88      * @param sTable the table name
89      * @return the alias for the given table name
90      */

91     public String JavaDoc getTableAlias(String JavaDoc sTable) {
92         String JavaDoc sReturn = null;
93
94         int index = m_Tables.indexOf(sTable);
95
96         if (index >= 0) {
97             sReturn = (String JavaDoc) m_TableAliases.elementAt(index);
98         }
99
100         return sReturn;
101     }
102
103     /**
104      * Returns <code>true</code> if the given <code>String</code> is an alias
105      * for this statement.
106      *
107      * @param sAlias the alias
108      * @return <code>true</code> if the given alias is an alias in this statement
109      * @throws DataStoreException if an error occurs while processing column refs for aliases
110      */

111     public boolean isAlias(String JavaDoc sAlias) throws DataStoreException {
112         boolean bIsAlias = false;
113
114         if (m_TableAliases != null) {
115             bIsAlias = m_TableAliases.contains(sAlias);
116         }
117
118         if (bIsAlias == false && m_bIsAliasListComplete == false && 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 JavaDoc getTableName(String JavaDoc sAlias) {
147         String JavaDoc sReturn = null;
148
149         int index = m_TableAliases.indexOf(sAlias);
150
151         if (index >= 0) {
152             sReturn = (String JavaDoc) 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 JavaDoc sOperator, int val)
167         throws DataStoreException {
168         Vector vec = new Vector(1);
169         vec.add(new Integer JavaDoc(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(
184         ColumnRef colref,
185         String JavaDoc sOperator,
186         Object JavaDoc val)
187         throws DataStoreException {
188         Vector vec = new Vector(1);
189         vec.add(val);
190
191         addWhereCondition(colref, sOperator, vec);
192     }
193
194     /**
195      * Add a where condition to this statement.
196      *
197      * @param colref the column ref the condition applies to
198      * @param sOperator the comparison operator to be used in the condition
199      * @param vals the list of values to be used in the comparison
200      * @throws DataStoreException if the condition can not be added
201      */

202     public void addWhereCondition(
203         ColumnRef colref,
204         String JavaDoc sOperator,
205         List vals)
206         throws DataStoreException {
207         if (m_where == null) {
208             m_where = new WhereConditionGroup();
209         }
210         
211         if(vals == null) {
212             vals = new Vector();
213             vals.add(null);
214         }
215
216         m_where.addCondition(colref, sOperator, vals);
217     }
218
219     /**
220      * Adds a collection of where conditions as a where condition to this statement.
221      *
222      * @param where a collection of where conditions
223      * @throws DataStoreException if the condition cannot be added
224      */

225     public void addWhereCondition(WhereConditionGroup where)
226         throws DataStoreException {
227         if (m_where == null) {
228             m_where = new WhereConditionGroup();
229         }
230
231         m_where.addCondition(where);
232     }
233
234     /**
235      * Adds a where condition to this statement.
236      *
237      * @param where the where condition to add
238      * @throws DataStoreException if the condition cannot be added
239      */

240     public void addWhereCondition(WhereCondition where)
241         throws DataStoreException {
242         if (m_where == null) {
243             m_where = new WhereConditionGroup();
244         }
245
246         m_where.addCondition(where);
247     }
248
249     /**
250      * Sets the where condition stringing operator (AND/OR).
251      *
252      * @param sOp the stringing operator
253      * @throws DataStoreException if the stringing operator is invalid
254      */

255     public void setWhereConditionStringingOperator(String JavaDoc sOp)
256         throws DataStoreException {
257         m_where.setStringingOperator(sOp);
258     }
259
260     /**
261      * Returns <code>true</code> if a where clause has been defined
262      *
263      * @return <code>true</code> if a where clause has been defined
264      */

265     public boolean hasWhereClause() {
266         return (m_where != null);
267     }
268     
269     /**
270      * Returns <code>true</code> if this statement has where conditions
271      *
272      * @return <code>true</code> if this statement has where conditions
273      */

274     public boolean hasWhereConditions() {
275         if ((m_where == null) || (m_where.size() == 0)) {
276             return false;
277         } else {
278             return true;
279         }
280     }
281     
282     /**
283      * Returns the where conditions for this statement
284      * @return the where conditions for this statement
285      */

286     public WhereConditionGroup getWhereConditions() {
287         return m_where;
288     }
289
290     /**
291      * Clears all data in this statement
292      *
293      */

294     public void clear() {
295         m_where = null;
296
297         if (m_Tables != null) {
298             m_Tables.removeAllElements();
299             m_TableAliases.removeAllElements();
300         }
301     }
302 }
Popular Tags