1 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 ; 36 import java.util.logging.Logger ; 37 38 41 public class EjbSelectQuery extends EjbQuery { 42 private static final Logger log = Log.open(EjbSelectQuery.class); 43 private static final L10N L = new L10N(EjbSelectQuery.class); 44 45 private ArrayList <PathExpr> _fromList; 46 protected Expr _selectExpr; 47 protected Expr _whereExpr; 48 protected IdExpr _thisExpr; 49 50 private ArrayList <Expr> _orderByExpr; 51 private ArrayList <Boolean > _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 ejbql) 60 { 61 super(ejbql); 62 } 63 64 67 public void setFromList(ArrayList <PathExpr> fromList) 68 { 69 _fromList = fromList; 70 } 71 72 75 public void setSelectExpr(Expr selectExpr) 76 { 77 _selectExpr = selectExpr; 78 } 79 80 83 public void setWhereExpr(Expr whereExpr) 84 { 85 _whereExpr = whereExpr; 86 } 87 88 91 public void setThisExpr(IdExpr thisExpr) 92 { 93 _thisExpr = thisExpr; 94 } 95 96 99 public IdExpr getThisExpr() 100 { 101 return _thisExpr; 102 } 103 104 107 public void setOrderBy(ArrayList <Expr> exprList, 108 ArrayList <Boolean > ascendingList) 109 { 110 _orderByExpr = exprList; 111 _orderByAscending = ascendingList; 112 } 113 114 117 public void setOffset(Expr offset) 118 { 119 _offsetExpr = offset; 120 } 121 122 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 136 public int getOffsetArg() 137 { 138 if (_offsetExpr instanceof ArgExpr) 139 return ((ArgExpr) _offsetExpr).getIndex(); 140 else 141 return -1; 142 } 143 144 147 public void setLimit(Expr limit) 148 { 149 _limitExpr = limit; 150 } 151 152 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 166 public int getLimitArg() 167 { 168 if (_limitExpr instanceof ArgExpr) 169 return ((ArgExpr) _limitExpr).getIndex(); 170 else 171 return -1; 172 } 173 174 177 public void setDistinct(boolean isDistinct) 178 { 179 _isDistinct = isDistinct; 180 } 181 182 185 public void setQueryLoadsBean(boolean queryLoadsBean) 186 { 187 _queryLoadsBean = queryLoadsBean; 188 } 189 190 193 public boolean getQueryLoadsBean() 194 { 195 return _queryLoadsBean; 196 } 197 198 201 public String toAmberQuery(String []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 |