001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb.ql;
030:
031: import com.caucho.util.CharBuffer;
032: import com.caucho.util.L10N;
033: import com.caucho.util.Log;
034:
035: import java.util.ArrayList;
036: import java.util.logging.Logger;
037:
038: /**
039: * Parsed expression for EJB-QL.
040: */
041: public class EjbSelectQuery extends EjbQuery {
042: private static final Logger log = Log.open(EjbSelectQuery.class);
043: private static final L10N L = new L10N(EjbSelectQuery.class);
044:
045: private ArrayList<PathExpr> _fromList;
046: protected Expr _selectExpr;
047: protected Expr _whereExpr;
048: protected IdExpr _this Expr;
049:
050: private ArrayList<Expr> _orderByExpr;
051: private ArrayList<Boolean> _orderByAscending;
052:
053: private Expr _offsetExpr;
054: private Expr _limitExpr;
055:
056: private boolean _queryLoadsBean = true;
057: private boolean _isDistinct = false;
058:
059: EjbSelectQuery(String ejbql) {
060: super (ejbql);
061: }
062:
063: /**
064: * Sets the from list.
065: */
066: public void setFromList(ArrayList<PathExpr> fromList) {
067: _fromList = fromList;
068: }
069:
070: /**
071: * Sets the select expr.
072: */
073: public void setSelectExpr(Expr selectExpr) {
074: _selectExpr = selectExpr;
075: }
076:
077: /**
078: * Sets the where expr
079: */
080: public void setWhereExpr(Expr whereExpr) {
081: _whereExpr = whereExpr;
082: }
083:
084: /**
085: * Set true if there's a "this" expression
086: */
087: public void setThisExpr(IdExpr this Expr) {
088: _this Expr = this Expr;
089: }
090:
091: /**
092: * Return true if there's a "this" expression
093: */
094: public IdExpr getThisExpr() {
095: return _this Expr;
096: }
097:
098: /**
099: * Sets the order by
100: */
101: public void setOrderBy(ArrayList<Expr> exprList,
102: ArrayList<Boolean> ascendingList) {
103: _orderByExpr = exprList;
104: _orderByAscending = ascendingList;
105: }
106:
107: /**
108: * Sets the limit offset.
109: */
110: public void setOffset(Expr offset) {
111: _offsetExpr = offset;
112: }
113:
114: /**
115: * Gets the offset as an argument.
116: */
117: public int getOffsetValue() {
118: if (_offsetExpr instanceof LiteralExpr)
119: return Integer.parseInt(((LiteralExpr) _offsetExpr)
120: .getValue());
121: else
122: return -1;
123: }
124:
125: /**
126: * Gets the offset as an argument.
127: */
128: public int getOffsetArg() {
129: if (_offsetExpr instanceof ArgExpr)
130: return ((ArgExpr) _offsetExpr).getIndex();
131: else
132: return -1;
133: }
134:
135: /**
136: * Sets the limit max.
137: */
138: public void setLimit(Expr limit) {
139: _limitExpr = limit;
140: }
141:
142: /**
143: * Gets the offset as an argument.
144: */
145: public int getLimitValue() {
146: if (_limitExpr instanceof LiteralExpr)
147: return Integer.parseInt(((LiteralExpr) _limitExpr)
148: .getValue());
149: else
150: return -1;
151: }
152:
153: /**
154: * Gets the limit as an argument.
155: */
156: public int getLimitArg() {
157: if (_limitExpr instanceof ArgExpr)
158: return ((ArgExpr) _limitExpr).getIndex();
159: else
160: return -1;
161: }
162:
163: /**
164: * Sets true for the distinct.
165: */
166: public void setDistinct(boolean isDistinct) {
167: _isDistinct = isDistinct;
168: }
169:
170: /**
171: * Sets true if the query should load the bean.
172: */
173: public void setQueryLoadsBean(boolean queryLoadsBean) {
174: _queryLoadsBean = queryLoadsBean;
175: }
176:
177: /**
178: * Returns true if the query should load the bean.
179: */
180: public boolean getQueryLoadsBean() {
181: return _queryLoadsBean;
182: }
183:
184: /**
185: * Convert to an amber query.
186: */
187: public String toAmberQuery(String[] args) {
188: CharBuffer cb = new CharBuffer();
189:
190: cb.append("SELECT ");
191:
192: if (_isDistinct)
193: cb.append("DISTINCT ");
194:
195: _selectExpr.generateSelect(cb);
196:
197: cb.append(" FROM ");
198:
199: for (int i = 0; i < _fromList.size(); i++) {
200: PathExpr item = _fromList.get(i);
201:
202: if (i != 0)
203: cb.append(", ");
204:
205: item.generateAmber(cb);
206: }
207:
208: if (_whereExpr != null) {
209: cb.append(" WHERE ");
210: _whereExpr.generateWhere(cb);
211: }
212:
213: if (_this Expr != null) {
214: if (_whereExpr == null)
215: cb.append(" WHERE ");
216: else
217: cb.append(" AND ");
218:
219: for (int i = 0; i < _this Expr.getComponentCount(); i++) {
220: if (i != 0)
221: cb.append(" AND ");
222:
223: _this Expr.generateComponent(cb, i);
224: cb.append("=?" + (getMaxArg() + i + 1));
225: }
226: }
227:
228: if (_orderByExpr != null && _orderByExpr.size() > 0) {
229: cb.append(" ORDER BY ");
230:
231: for (int i = 0; i < _orderByExpr.size(); i++) {
232: if (i != 0)
233: cb.append(", ");
234:
235: Expr orderBy = _orderByExpr.get(i);
236: ;
237:
238: if (orderBy instanceof ArgExpr)
239: cb.append("\" + "
240: + args[((ArgExpr) orderBy).getIndex() - 1]
241: + " + \"");
242: else
243: orderBy.generateSelect(cb);
244:
245: if (Boolean.FALSE.equals(_orderByAscending.get(i)))
246: cb.append(" DESC");
247: }
248: }
249:
250: return cb.toString();
251: }
252: }
|