KickJava   Java API By Example, From Geeks To Geeks.

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


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  * A DML select statement query.
28  *
29  * @author Michael Bell
30  * @version $Revision: 1.2 $
31  *
32  */

33 public class SelectStatement extends AbstractDMLStatement {
34     
35     /**
36      * Constant to indicate results should be ordered in ascending order.
37      */

38     static public String JavaDoc ORDER_ASCENDING = "ASC";
39     
40     /**
41      * Constant to indicate results should be ordered in descending order.
42      */

43     static public String JavaDoc ORDER_DESCENDING = "DESC";
44     
45     /**
46      * Join conditions for this select statement.
47      */

48     private JoinConditions m_join = null;
49     
50     /**
51      * List of column references to be returned by query.
52      */

53     private List m_SelectCols = null;
54     
55     /**
56      * Mapping of column refs to order by with the order direction to be
57      * ordered by.
58      */

59     private Map m_orderColMap = null;
60     
61     /**
62      * The maximum number of rows to be returned.
63      */

64     private int m_nLimit = -1;
65     
66     /**
67      * Flag to indicate whether this select statement is to return distict
68      * result rows.
69      */

70     private boolean m_distinct = false;
71     
72     /**
73      * List of select column references to have the 'max' function applied to.
74      */

75     private List m_maxcols = null;
76
77     /**
78      * Constructs a select statement.
79      *
80      */

81     public SelectStatement() {
82         m_SelectCols = new Vector();
83     }
84
85     /**
86      * Adds a constant to be returned in select statement result.
87      *
88      * @param i <code>int</code> constant to be returned by query
89      */

90     public void addSelectColumn(int i) {
91         if (m_SelectCols == null) {
92             m_SelectCols = new Vector(16);
93         }
94
95         m_SelectCols.add(new Integer JavaDoc(i));
96     }
97
98     /**
99      * Adds list of column references to be added to list of columns
100      * to be returned by this query.
101      *
102      * @param colrefs list of <code>ColumnRef</code>s to be returned
103      */

104     public void addSelectColumns(List colrefs) {
105         if (m_SelectCols == null) {
106             m_SelectCols = new Vector(16);
107         }
108
109         m_SelectCols.addAll(colrefs);
110     }
111
112     /**
113      * Adds a column reference to be added to list of columns
114      * to be returned by this query.
115      *
116      * @param colref
117      */

118     public void addSelectColumn(ColumnRef colref) {
119         if (m_SelectCols == null) {
120             m_SelectCols = new Vector(16);
121         }
122
123         m_SelectCols.add(colref);
124     }
125
126     /**
127      * Returns the list of column references to be returned by this query.
128      *
129      * @return the list of column references to be returned by this query
130      */

131     public List getSelectColumns() {
132         return m_SelectCols;
133     }
134     
135     /**
136      * Returns the index of the <code>ColumnRef</code> object in result set
137      * of this query.
138      *
139      * @param colRef the column reference
140      *
141      * @return the index of the column in the result set
142      */

143     public int getResultSetIndex(ColumnRef colRef) {
144       return m_SelectCols.indexOf(colRef) + 1;
145     }
146     
147     /**
148      * Returns <code>true</code> if colRef is a select column.
149      *
150      * @param colRef the column reference
151      *
152      * @return <code>true</code> if colRef is a select column.
153      */

154     public boolean containsSelectColumn(ColumnRef colRef) {
155       return m_SelectCols.contains(colRef);
156     }
157
158     /**
159      * Adds a column to be returned in select column with the 'max()' function
160      * applied.
161      *
162      * @param colref the column reference
163      */

164     public void addSelectMaxColumn(ColumnRef colref) {
165         if (m_maxcols == null) {
166             m_maxcols = new Vector(16);
167         }
168
169         m_maxcols.add(new Integer JavaDoc(m_SelectCols.size()));
170
171         m_SelectCols.add(colref);
172     }
173
174     /**
175      * Returns the list of column references which have to have the 'max()'
176      * function applied to by the query.
177      *
178      * @return the list of column references
179      */

180     public List getSelectMaxColumns() {
181         return m_maxcols;
182     }
183
184     /**
185      * Set the column reference which is to be used to order the result of this
186      * query.
187      *
188      * @param colref the column reference
189      * @throws DataStoreException
190      */

191     public void setOrderBy(ColumnRef colref) {
192         addOrderBy(colref);
193     }
194
195     /**
196      * Sets the maximum number of rows that can be returned by this query.
197      *
198      * @param num maximum number of rows to be returned
199      */

200     public void setLimit(int num) {
201         m_nLimit = num;
202     }
203
204     /**
205      * Returns <code>true</code> if a maximum number of rows that can be
206      * returned by this query has been set.
207      *
208      * @return <code>true</code> if a limit has been set
209      */

210     public boolean isLimit() {
211         return (m_nLimit > 0);
212     }
213
214     /**
215      * Returns the maximum number of rows that can be returned by this query.
216      *
217      * @return the maximum number of rows that can be returned
218      */

219     public int getLimit() {
220         return m_nLimit;
221     }
222
223     /**
224      * Adds a collection of join conditions to this query.
225      *
226      * @param join the collection of join conditions
227      * @throws DataStoreException if an error occurs merging the join conditions
228      */

229     public void addJoinConditions(JoinConditions join)
230                            throws DataStoreException {
231         if(join != null) {
232             if (m_join == null) {
233                 m_join = new JoinConditions();
234             }
235     
236             m_join.merge(join);
237         }
238     }
239
240     /**
241      * Adds an inner join condition to this query.
242      *
243      * @param colref1 the left column reference for this join
244      * @param colref2 the right column reference for this join
245      * @throws DataStoreException if the join is invalid
246      */

247     public void addJoinCondition(ColumnRef colref1, ColumnRef colref2)
248                           throws DataStoreException {
249         if (m_join == null) {
250             m_join = new JoinConditions();
251         }
252
253         m_join.addCondition(colref1, colref2);
254     }
255
256     /**
257      * Adds an outer join condition to this query.
258      *
259      * @param innerMember the inner memeber column reference of this join
260      * @param outerMember the outer memeber column reference of this join
261      * @throws DataStoreException if the join is invalid
262      */

263     public void addOuterJoinCondition(ColumnRef innerMember, ColumnRef outerMember)
264                                throws DataStoreException {
265         if (m_join == null) {
266             m_join = new JoinConditions();
267         }
268
269         m_join.addOuterJoin(innerMember, outerMember);
270     }
271
272     /**
273      * Returns the collection of join conditions applicable to this query.
274      *
275      * @return the collection of join conditions
276      */

277     public JoinConditions getJoinConditions() {
278         return m_join;
279     }
280
281     /**
282      * Returns <code>true</code> if this query has join conditions.
283      *
284      * @return <code>true</code> if this query has join conditions.
285      */

286     public boolean hasJoinConditions() {
287         if ((m_join == null) || (m_join.size() == 0)) {
288             return false;
289         } else {
290             return true;
291         }
292     }
293     
294
295     /* (non-Javadoc)
296      * @see org.openharmonise.commons.dsi.dml.AbstractDMLStatement#addWhereCondition(org.openharmonise.commons.dsi.dml.WhereCondition)
297      */

298     public void addWhereCondition(WhereCondition where)
299                            throws DataStoreException {
300         super.addWhereCondition(where);
301         
302         //get associated joins
303
JoinConditions joins = where.getAssociatedJoinConditions();
304         
305         addJoinConditions(joins);
306     }
307     
308
309     /* (non-Javadoc)
310      * @see org.openharmonise.commons.dsi.dml.AbstractDMLStatement#addWhereCondition(org.openharmonise.commons.dsi.dml.WhereConditionGroup)
311      */

312     public void addWhereCondition(WhereConditionGroup where)
313                            throws DataStoreException {
314         super.addWhereCondition(where);
315         
316         //get associated joins
317
for(int i=0;i<where.size();i++) {
318             Object JavaDoc cond = where.getCondition(i);
319             
320             if(cond instanceof WhereCondition) {
321                 JoinConditions joins = ((WhereCondition)cond).getAssociatedJoinConditions();
322                 
323                 addJoinConditions(joins);
324             }
325         }
326     }
327     
328     /**
329      * Sets whether this query will return distinct results.
330      *
331      * @param bIsDistinct <code>true</code> if the results should be distinct
332      */

333     public void setDistinct(boolean bIsDistinct) {
334         m_distinct = bIsDistinct;
335     }
336
337     /**
338      * Returns <code>true</code> if the results of this query should be
339      * distinct.
340      *
341      * @return<code>true</code> if the results should be distinct
342      */

343     public boolean isDistinct() {
344         return m_distinct;
345     }
346
347     /* (non-Javadoc)
348      * @see org.openharmonise.commons.dsi.dml.AbstractDMLStatement#clear()
349      */

350     public void clear() {
351         super.clear();
352
353         if (m_join != null) {
354             m_join.empty();
355         }
356
357         if (m_SelectCols != null) {
358             m_SelectCols.clear();
359         }
360
361         m_orderColMap.clear();
362         m_nLimit = -1;
363     }
364     
365     /**
366      * Add column reference to order the results of this select statement by.
367      *
368      * @param colref the column reference
369      */

370     public void addOrderBy(ColumnRef colref) {
371         addOrderBy(colref,ORDER_ASCENDING);
372     }
373     
374     /**
375      * Add column reference to order the results of this select statement by.
376      *
377      * @param colref the column reference
378      * @param orderDir the direction of ordering
379      */

380     public void addOrderBy(ColumnRef colref,String JavaDoc orderDir) {
381         initialiseOrderByMap();
382         
383         m_orderColMap.put(colref, orderDir);
384     }
385     
386     /**
387      * Returns the <code>Set<code> of column references to order
388      * the results of this select statment by.
389      *
390      * @return the <code>Set<code> of column references
391      */

392     public Set getOrderByColumns() {
393         initialiseOrderByMap();
394         
395         return m_orderColMap.keySet();
396     }
397     
398     /**
399      * Returns the ordering direction for the specified 'order by' column.
400      *
401      * @param colref the column reference
402      * @return the ordering direction
403      */

404     public String JavaDoc getOrderByDirection(ColumnRef colref) {
405         initialiseOrderByMap();
406         
407         return (String JavaDoc) m_orderColMap.get(colref);
408     }
409     
410     /**
411      * Initialise the mapping between 'order by' column references
412      * and the associated ordering direction.
413      *
414      */

415     private void initialiseOrderByMap() {
416         if(m_orderColMap == null) {
417             //use LinkedHashMap to ensure ordering is maintained
418
m_orderColMap = new LinkedHashMap();
419         }
420     }
421
422     /**
423      * Add order by column reference to ordering direction mappings
424      * to this select statement.
425      *
426      * @param byColMap Map of column reference ordering direction pairs
427      */

428     public void addOrderBy(Map byColMap) {
429         if(m_orderColMap == null) {
430             m_orderColMap = new LinkedHashMap(byColMap);
431         } else {
432             Iterator iter = byColMap.keySet().iterator();
433             
434             while (iter.hasNext()) {
435                 ColumnRef colref = (ColumnRef) iter.next();
436                 m_orderColMap.put(colref, byColMap.get(colref));
437             }
438         }
439         
440     }
441 }
Popular Tags