KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > ql > EjbSelectQuery


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.ql;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.util.L10N;
33 import com.caucho.util.Log;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 /**
39  * Parsed expression for EJB-QL.
40  */

41 public class EjbSelectQuery extends EjbQuery {
42   private static final Logger JavaDoc log = Log.open(EjbSelectQuery.class);
43   private static final L10N L = new L10N(EjbSelectQuery.class);
44
45   private ArrayList JavaDoc<PathExpr> _fromList;
46   protected Expr _selectExpr;
47   protected Expr _whereExpr;
48   protected IdExpr _thisExpr;
49
50   private ArrayList JavaDoc<Expr> _orderByExpr;
51   private ArrayList JavaDoc<Boolean JavaDoc> _orderByAscending;
52
53   private Expr _offsetExpr;
54   private Expr _limitExpr;
55
56   private boolean _queryLoadsBean = true;
57   private boolean _isDistinct = false;
58
59   EjbSelectQuery(String JavaDoc ejbql)
60   {
61     super(ejbql);
62   }
63
64   /**
65    * Sets the from list.
66    */

67   public void setFromList(ArrayList JavaDoc<PathExpr> fromList)
68   {
69     _fromList = fromList;
70   }
71
72   /**
73    * Sets the select expr.
74    */

75   public void setSelectExpr(Expr selectExpr)
76   {
77     _selectExpr = selectExpr;
78   }
79
80   /**
81    * Sets the where expr
82    */

83   public void setWhereExpr(Expr whereExpr)
84   {
85     _whereExpr = whereExpr;
86   }
87
88   /**
89    * Set true if there's a "this" expression
90    */

91   public void setThisExpr(IdExpr thisExpr)
92   {
93     _thisExpr = thisExpr;
94   }
95
96   /**
97    * Return true if there's a "this" expression
98    */

99   public IdExpr getThisExpr()
100   {
101     return _thisExpr;
102   }
103
104   /**
105    * Sets the order by
106    */

107   public void setOrderBy(ArrayList JavaDoc<Expr> exprList,
108              ArrayList JavaDoc<Boolean JavaDoc> ascendingList)
109   {
110     _orderByExpr = exprList;
111     _orderByAscending = ascendingList;
112   }
113
114   /**
115    * Sets the limit offset.
116    */

117   public void setOffset(Expr offset)
118   {
119     _offsetExpr = offset;
120   }
121
122   /**
123    * Gets the offset as an argument.
124    */

125   public int getOffsetValue()
126   {
127     if (_offsetExpr instanceof LiteralExpr)
128       return Integer.parseInt(((LiteralExpr) _offsetExpr).getValue());
129     else
130       return -1;
131   }
132
133   /**
134    * Gets the offset as an argument.
135    */

136   public int getOffsetArg()
137   {
138     if (_offsetExpr instanceof ArgExpr)
139       return ((ArgExpr) _offsetExpr).getIndex();
140     else
141       return -1;
142   }
143
144   /**
145    * Sets the limit max.
146    */

147   public void setLimit(Expr limit)
148   {
149     _limitExpr = limit;
150   }
151
152   /**
153    * Gets the offset as an argument.
154    */

155   public int getLimitValue()
156   {
157     if (_limitExpr instanceof LiteralExpr)
158       return Integer.parseInt(((LiteralExpr) _limitExpr).getValue());
159     else
160       return -1;
161   }
162
163   /**
164    * Gets the limit as an argument.
165    */

166   public int getLimitArg()
167   {
168     if (_limitExpr instanceof ArgExpr)
169       return ((ArgExpr) _limitExpr).getIndex();
170     else
171       return -1;
172   }
173
174   /**
175    * Sets true for the distinct.
176    */

177   public void setDistinct(boolean isDistinct)
178   {
179     _isDistinct = isDistinct;
180   }
181
182   /**
183    * Sets true if the query should load the bean.
184    */

185   public void setQueryLoadsBean(boolean queryLoadsBean)
186   {
187     _queryLoadsBean = queryLoadsBean;
188   }
189
190   /**
191    * Returns true if the query should load the bean.
192    */

193   public boolean getQueryLoadsBean()
194   {
195     return _queryLoadsBean;
196   }
197
198   /**
199    * Convert to an amber query.
200    */

201   public String JavaDoc toAmberQuery(String JavaDoc []args)
202   {
203     CharBuffer cb = new CharBuffer();
204
205     cb.append("SELECT ");
206
207     if (_isDistinct)
208       cb.append("DISTINCT ");
209
210     _selectExpr.generateSelect(cb);
211
212     cb.append(" FROM ");
213
214     for (int i = 0; i < _fromList.size(); i++) {
215       PathExpr item = _fromList.get(i);
216
217       if (i != 0)
218     cb.append(", ");
219
220       item.generateAmber(cb);
221     }
222
223     if (_whereExpr != null) {
224       cb.append(" WHERE ");
225       _whereExpr.generateWhere(cb);
226     }
227
228     if (_thisExpr != null) {
229       if (_whereExpr == null)
230     cb.append(" WHERE ");
231       else
232     cb.append(" AND ");
233
234       for (int i = 0; i < _thisExpr.getComponentCount(); i++) {
235     if (i != 0)
236       cb.append(" AND ");
237     
238     _thisExpr.generateComponent(cb, i);
239     cb.append("=?" + (getMaxArg() + i + 1));
240       }
241     }
242
243     if (_orderByExpr != null && _orderByExpr.size() > 0) {
244       cb.append(" ORDER BY ");
245
246       for (int i = 0; i < _orderByExpr.size(); i++) {
247     if (i != 0)
248       cb.append(", ");
249
250     Expr orderBy = _orderByExpr.get(i);;
251
252     if (orderBy instanceof ArgExpr)
253       cb.append("\" + " + args[((ArgExpr) orderBy).getIndex() - 1] + " + \"");
254     else
255       orderBy.generateSelect(cb);
256
257     if (Boolean.FALSE.equals(_orderByAscending.get(i)))
258       cb.append(" DESC");
259       }
260     }
261     
262     return cb.toString();
263   }
264 }
265
Popular Tags