View Javadoc

1   package org.apache.torque.util;
2   
3   /*
4    * Copyright 2001-2004 The Apache Software Foundation.
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.commons.lang.StringUtils;
20  
21  /***
22   * Used to assemble an SQL SELECT query.  Attributes exist for the
23   * sections of a SELECT: modifiers, columns, from clause, where
24   * clause, and order by clause.  The various parts of the query are
25   * appended to buffers which only accept unique entries.  This class
26   * is used primarily by BasePeer.
27   *
28   * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
29   * @author <a href="mailto:sam@neurogrid.com">Sam Joseph</a>
30   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
31   * @version $Id: Query.java,v 1.13 2004/02/22 06:16:35 jmcnally Exp $
32   */
33  public class Query
34  {
35      private static final String SELECT = "SELECT ";
36      private static final String FROM = " FROM ";
37      private static final String WHERE = " WHERE ";
38      private static final String AND = " AND ";
39      private static final String ORDER_BY = " ORDER BY ";
40      private static final String GROUP_BY = " GROUP BY ";
41      private static final String HAVING = " HAVING ";
42      private static final String LIMIT = " LIMIT ";
43      private static final String ROWCOUNT = " SET ROWCOUNT ";
44  
45      private UniqueList selectModifiers = new UniqueList();
46      private UniqueList selectColumns = new UniqueList();
47      private UniqueList fromTables = new UniqueList();
48      private UniqueList whereCriteria = new UniqueList();
49      private UniqueList orderByColumns = new UniqueList();
50      private UniqueList groupByColumns = new UniqueList();
51      private String having;
52      private String limit;
53      private String rowcount;
54  
55      /***
56       * Retrieve the modifier buffer in order to add modifiers to this
57       * query.  E.g. DISTINCT and ALL.
58       *
59       * @return An UniqueList used to add modifiers.
60       */
61      public UniqueList getSelectModifiers()
62      {
63          return selectModifiers;
64      }
65  
66      /***
67       * Set the modifiers. E.g. DISTINCT and ALL.
68       *
69       * @param modifiers the modifiers
70       */
71      public void setSelectModifiers(UniqueList modifiers)
72      {
73          selectModifiers = modifiers;
74      }
75  
76      /***
77       * Retrieve the columns buffer in order to specify which columns
78       * are returned in this query.
79       *
80       *
81       * @return An UniqueList used to add columns to be selected.
82       */
83      public UniqueList getSelectClause()
84      {
85          return selectColumns;
86      }
87  
88      /***
89       * Set the columns.
90       *
91       * @param columns columns list
92       */
93      public void setSelectClause(UniqueList columns)
94      {
95          selectColumns = columns;
96      }
97  
98      /***
99       * Retrieve the from buffer in order to specify which tables are
100      * involved in this query.
101      *
102      * @return An UniqueList used to add tables involved in the query.
103      */
104     public UniqueList getFromClause()
105     {
106         return fromTables;
107     }
108 
109     /***
110      * Set the from clause.
111      *
112      * @param tables the tables
113      */
114     public void setFromClause(UniqueList tables)
115     {
116         fromTables = tables;
117     }
118 
119     /***
120      * Retrieve the where buffer in order to specify the selection
121      * criteria E.g. column_a=3.  Expressions added to the buffer will
122      * be separated using AND.
123      *
124      * @return An UniqueList used to add selection criteria.
125      */
126     public UniqueList getWhereClause()
127     {
128         return whereCriteria;
129     }
130 
131     /***
132      * Set the where clause.
133      *
134      * @param where where clause
135      */
136     public void setWhereClause(UniqueList where)
137     {
138         whereCriteria = where;
139     }
140 
141     /***
142      * Retrieve the order by columns buffer in order to specify which
143      * columns are used to sort the results of the query.
144      *
145      * @return An UniqueList used to add columns to sort on.
146      */
147     public UniqueList getOrderByClause()
148     {
149         return orderByColumns;
150     }
151 
152     /***
153      * Retrieve the group by columns buffer in order to specify which
154      * columns are used to group the results of the query.
155      *
156      * @return An UniqueList used to add columns to group on.
157      */
158     public UniqueList getGroupByClause()
159     {
160         return groupByColumns;
161     }
162 
163     /***
164      * Set the having clause.  This is used to restrict which rows
165      * are returned.
166      *
167      * @param having A String.
168      */
169     public void setHaving(String having)
170     {
171         this.having = having;
172     }
173 
174     /***
175      * Set the limit number.  This is used to limit the number of rows
176      * returned by a query, and the row where the resultset starts.
177      *
178      * @param limit A String.
179      */
180     public void setLimit(String limit)
181     {
182         this.limit = limit;
183     }
184 
185     /***
186      * Set the rowcount number.  This is used to limit the number of
187      * rows returned by Sybase and MS SQL/Server.
188      *
189      * @param rowcount A String.
190      */
191     public void setRowcount(String rowcount)
192     {
193         this.rowcount = rowcount;
194     }
195 
196     /***
197      * Get the having clause.  This is used to restrict which
198      * rows are returned based on some condition.
199      *
200      * @return A String that is the having clause.
201      */
202     public String getHaving()
203     {
204         return having;
205     }
206 
207     /***
208      * Get the limit number.  This is used to limit the number of
209      * returned by a query in Postgres.
210      *
211      * @return A String with the limit.
212      */
213     public String getLimit()
214     {
215         return limit;
216     }
217 
218     /***
219      * Get the rowcount number.  This is used to limit the number of
220      * returned by a query in Sybase and MS SQL/Server.
221      *
222      * @return A String with the row count.
223      */
224     public String getRowcount()
225     {
226         return rowcount;
227     }
228 
229     /***
230      * Outputs the query statement.
231      *
232      * @return A String with the query statement.
233      */
234     public String toString()
235     {
236         StringBuffer stmt = new StringBuffer();
237         if (rowcount != null)
238         {
239             stmt.append(ROWCOUNT)
240                 .append(rowcount)
241                 .append(" ");
242         }
243         stmt.append(SELECT)
244             .append(StringUtils.join(selectModifiers.iterator(), " "))
245             .append(StringUtils.join(selectColumns.iterator(), ", "))
246             .append(FROM)
247             .append(StringUtils.join(fromTables.iterator(), ", "));
248         if (!whereCriteria.isEmpty())
249         {
250             stmt.append(WHERE)
251                 .append(StringUtils.join(whereCriteria.iterator(), AND));
252         }
253         if (!groupByColumns.isEmpty())
254         {
255             stmt.append(GROUP_BY)
256                 .append(StringUtils.join(groupByColumns.iterator(), ", "));
257         }
258         if (having != null)
259         {
260             stmt.append(HAVING)
261                 .append(having);
262         }
263         if (!orderByColumns.isEmpty())
264         {
265             stmt.append(ORDER_BY)
266                 .append(StringUtils.join(orderByColumns.iterator(), ", "));
267         }
268         if (limit != null)
269         {
270             stmt.append(LIMIT)
271                 .append(limit);
272         }
273         if (rowcount != null)
274         {
275             stmt.append(ROWCOUNT)
276                 .append("0");
277         }
278         return stmt.toString();
279     }
280 }