KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > sql > SqlSelect


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.extractor.sql;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.xquark.extractor.algebra.Expression;
30
31 public class SqlSelect extends SqlExpression implements Executable
32 {
33
34     private static final String JavaDoc RCSRevision = "$Revision: 1.6 $";
35     private static final String JavaDoc RCSName = "$Name: $";
36
37     private boolean _selectDistinct;
38     private SqlExpression _whereClause;
39     private List JavaDoc _selectList;
40     private List JavaDoc _orderbyClause;
41     private List JavaDoc _groupbyClause;
42     private SqlExpression _havingClause;
43     private List JavaDoc _fromClause;
44     private Expression _originalExpression;
45     /**
46     @roseuid 3BD177FC0041
47      */

48     public SqlSelect(Expression originalExpression) {
49         _originalExpression = originalExpression;
50     }
51
52     /**
53     Access method for the _whereClause property.
54
55     @return the current value of the _whereClause property
56      */

57     public SqlExpression getWhereClause()
58     {
59         return _whereClause;
60     }
61
62     public void setWhereClause(SqlExpression aWhereClause)
63     {
64         //_whereClause = predicate ;
65
_whereClause = aWhereClause;
66     }
67
68     /**
69     Access method for the _selectList property.
70
71     @return the current value of the _selectList property
72      */

73     public List JavaDoc getSelectList()
74     {
75         return _selectList;
76     }
77
78     /**
79     Sets the value of the _selectList property.
80
81     @param aSelectList the new value of the _selectList property
82      */

83     public void setSelectList(List JavaDoc aSelectList)
84     {
85         _selectList = aSelectList;
86     }
87
88     /**
89     Access method for the _orderbyClause property.
90
91     @return the current value of the _orderbyClause property
92      */

93     public List JavaDoc getOrderbyClause()
94     {
95         return _orderbyClause;
96     }
97
98     /**
99     Sets the value of the _orderbyClause property.
100
101     @param aOrderbyClause the new value of the _orderbyClause property
102      */

103     public void setOrderbyClause(List JavaDoc aOrderbyClause)
104     {
105         //_orderbyClause = sortSpecList ;
106
_orderbyClause = aOrderbyClause;
107     }
108
109     /**
110     Access method for the _groupbyClause property.
111
112     @return the current value of the _groupbyClause property
113      */

114     public List JavaDoc getGroupbyClause()
115     {
116         return _groupbyClause ;
117     }
118
119     /**
120     Sets the value of the _groupbyClause property.
121
122     @param aGroupbyClause the new value of the _groupbyClause property
123      */

124     public void setGroupbyClause(List JavaDoc aGroupbyClause)
125     {
126         //_groupbyClause = groupbyClause ;
127
_groupbyClause = aGroupbyClause;
128     }
129
130     /**
131     Access method for the _havingClause property.
132
133     @return the current value of the _havingClause property
134      */

135     public SqlExpression getHavingClause()
136     {
137         return _havingClause;
138     }
139
140     /**
141     Sets the value of the _havingClause property.
142
143     @param aHavingClause the new value of the _havingClause property
144      */

145     public void setHavingClause(SqlExpression aHavingClause)
146     {
147         _havingClause = aHavingClause;
148     }
149
150     /**
151     Access method for the _fromClause property.
152
153     @return the current value of the _fromClause property
154      */

155     public List JavaDoc getFromClause()
156     {
157      return _fromClause;
158     }
159
160     /**
161     Sets the value of the _fromClause property.
162
163     @param aFromClause the new value of the _fromClause property
164      */

165     public void setFromClause(List JavaDoc aFromClause)
166     {
167         _fromClause = aFromClause;
168     }
169
170     /**
171     @return boolean
172     @roseuid 3BD177FC00FF
173      */

174     public boolean getSelectDistinct()
175     {
176         return _selectDistinct;
177     }
178
179     public void setSelectDistinct(boolean selectDistinct )
180     {
181         _selectDistinct = selectDistinct ;
182     }
183
184     /**
185     Sets the value of the _selectClause property.
186     @param aSelectClause the new value of the _selectClause property
187     @param selectDistinct
188     @param selectList
189     @roseuid 3BD177FC013C
190      */

191     public void setSelectClause(boolean selectDistinct, List JavaDoc selectList)
192     {
193         _selectDistinct = selectDistinct ;
194         _selectList = selectList ;
195     }
196
197
198 // /**
199
// @param predicate
200
// @roseuid 3BD177FC0358
201
// */
202
// public void addWhereClause(SqlExpression predicate)
203
// {
204
// if ( null == predicate ) {
205
// return ;
206
// }
207
//
208
// if (null == _whereClause) {
209
// _whereClause = new ArrayList();
210
// }
211
// _whereClause.add(predicate);
212
// }
213

214
215
216     /**
217     @param sortSpecList
218     @return java.util.List
219     @roseuid 3BD177FE00D0
220      */

221     public List JavaDoc getOrderbyClause(List JavaDoc sortSpecList)
222     {
223         return _orderbyClause ;
224     }
225
226
227     public void addFromClause(SqlExpression fromItem)
228     {
229         if ( null == _fromClause )
230             _fromClause = new ArrayList JavaDoc () ;
231
232         _fromClause.add(fromItem);
233     }
234
235     public String JavaDoc toSql(Context context)
236     {
237         Context newContext = new Context(context);
238
239         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc() ;
240         selectToSql(retVal, newContext);
241         fromToSql(retVal, newContext);
242         whereToSql(retVal, newContext);
243         groupbyToSql(retVal, newContext);
244         havingToSql(retVal, newContext);
245         orderbyToSql(retVal, newContext);
246
247         return retVal.toString() ;
248     }
249
250     protected StringBuffer JavaDoc selectToSql(StringBuffer JavaDoc statementBuffer, Context context) {
251         statementBuffer.append("SELECT") ;
252         if ( _selectDistinct )
253             statementBuffer.append(" DISTINCT ");
254         else
255             statementBuffer.append(" ALL ");
256
257         String JavaDoc tmpStr = "";
258         Iterator JavaDoc tmpItr ;
259         context.selectList = true;
260         if ( null != _selectList)
261         {
262             tmpItr = _selectList.iterator();
263             while (tmpItr.hasNext()) {
264                 tmpStr += ((SqlExpression)tmpItr.next()).toSql(context) + " , ";
265             }
266             tmpStr = tmpStr.substring(0 ,tmpStr.length()-3 );
267         }
268         else
269             tmpStr += " * " ;
270         context.selectList = false;
271         statementBuffer.append(tmpStr);
272
273         return statementBuffer;
274     }
275
276     protected StringBuffer JavaDoc fromToSql(StringBuffer JavaDoc statementBuffer, Context context) {
277         Iterator JavaDoc tmpItr = null;
278
279         if ( null != _fromClause )
280         {
281             statementBuffer.append("\nFROM ");
282
283             tmpItr = _fromClause.iterator();
284             while (tmpItr.hasNext()) {
285                 SqlExpression relation = (SqlExpression)tmpItr.next();
286
287                 if (relation instanceof SqlSelect) {
288                     statementBuffer.append('(');
289                     statementBuffer.append(relation.toSql(context));
290                     statementBuffer.append(')');
291                 }
292                 else
293                     statementBuffer.append(relation.toSql(context));
294
295                 statementBuffer.append(' '); // NOTE: not needed anymore since a SqlJoin node exists
296
}
297         }
298         return statementBuffer;
299     }
300
301     protected StringBuffer JavaDoc whereToSql(StringBuffer JavaDoc statementBuffer, Context context) {
302         if ( null != _whereClause )
303         {
304             statementBuffer.append("\nWHERE ");
305             statementBuffer.append(_whereClause.toSql(context));
306         }
307         return statementBuffer;
308     }
309
310     protected StringBuffer JavaDoc groupbyToSql(StringBuffer JavaDoc statementBuffer, Context context) {
311         // groupby clause
312
String JavaDoc tmpStr = "" ;
313         Iterator JavaDoc tmpItr = null;
314         if ( null != _groupbyClause )
315         {
316             tmpItr = _groupbyClause.iterator();
317             while (tmpItr.hasNext()) {
318                 tmpStr += ((SqlExpression)tmpItr.next()).toSql(context) + " , ";
319             }
320             tmpStr = tmpStr.substring(0 ,tmpStr.length()-3 );
321
322             statementBuffer.append("\nGROUP BY ");
323             statementBuffer.append(tmpStr);
324         }
325         return statementBuffer;
326     }
327
328     protected StringBuffer JavaDoc havingToSql(StringBuffer JavaDoc statementBuffer, Context context) {
329         // having clause
330
if ( null != _havingClause )
331         {
332             statementBuffer.append("\nHAVING ");
333             statementBuffer.append(_havingClause.toSql(context));
334         }
335         return statementBuffer;
336     }
337
338     protected StringBuffer JavaDoc orderbyToSql(StringBuffer JavaDoc statementBuffer, Context context) {
339         String JavaDoc tmpStr = "" ;
340         Iterator JavaDoc tmpItr = null;
341         if ( null != _orderbyClause && !_orderbyClause.isEmpty())
342         {
343             tmpItr = _orderbyClause.iterator();
344             while (tmpItr.hasNext()) {
345                 tmpStr += ((SqlExpression)tmpItr.next()).toSql(context) + " , ";
346             }
347             tmpStr = tmpStr.substring(0 ,tmpStr.length()-3 );
348
349             statementBuffer.append("\nORDER BY ");
350             statementBuffer.append(tmpStr);
351         }
352         return statementBuffer;
353     }
354
355     public Expression getOriginalExpression() {
356         return _originalExpression;
357     }
358
359 }
360
Popular Tags