1 package org.apache.torque.util; 2 3 21 22 import java.util.Iterator ; 23 24 import org.apache.commons.lang.StringUtils; 25 26 39 public class Query 40 { 41 private static final String SELECT = "SELECT "; 42 private static final String FROM = " FROM "; 43 private static final String WHERE = " WHERE "; 44 private static final String AND = " AND "; 45 private static final String ORDER_BY = " ORDER BY "; 46 private static final String GROUP_BY = " GROUP BY "; 47 private static final String HAVING = " HAVING "; 48 private static final String LIMIT = " LIMIT "; 49 private static final String ROWCOUNT = " SET ROWCOUNT "; 50 51 private UniqueList selectModifiers = new UniqueList(); 52 private UniqueList selectColumns = new UniqueList(); 53 private UniqueList fromTables = new UniqueList(); 54 private UniqueList whereCriteria = new UniqueList(); 55 private UniqueList orderByColumns = new UniqueList(); 56 private UniqueList groupByColumns = new UniqueList(); 57 private String having; 58 private String limit; 59 private String preLimit; 60 private String postLimit; 61 private String rowcount; 62 63 69 public UniqueList getSelectModifiers() 70 { 71 return selectModifiers; 72 } 73 74 79 public void setSelectModifiers(UniqueList modifiers) 80 { 81 selectModifiers = modifiers; 82 } 83 84 91 public UniqueList getSelectClause() 92 { 93 return selectColumns; 94 } 95 96 101 public void setSelectClause(UniqueList columns) 102 { 103 selectColumns = columns; 104 } 105 106 112 public UniqueList getFromClause() 113 { 114 return fromTables; 115 } 116 117 122 public void setFromClause(UniqueList tables) 123 { 124 fromTables = tables; 125 } 126 127 134 public UniqueList getWhereClause() 135 { 136 return whereCriteria; 137 } 138 139 144 public void setWhereClause(UniqueList where) 145 { 146 whereCriteria = where; 147 } 148 149 155 public UniqueList getOrderByClause() 156 { 157 return orderByColumns; 158 } 159 160 166 public UniqueList getGroupByClause() 167 { 168 return groupByColumns; 169 } 170 171 177 public void setHaving(String having) 178 { 179 this.having = having; 180 } 181 182 188 public void setLimit(String limit) 189 { 190 this.limit = limit; 191 } 192 193 199 public void setPreLimit(String preLimit) 200 { 201 this.preLimit = preLimit; 202 } 203 204 210 public void setPostLimit(String postLimit) 211 { 212 this.postLimit = postLimit; 213 } 214 215 221 public void setRowcount(String rowcount) 222 { 223 this.rowcount = rowcount; 224 } 225 226 232 public String getHaving() 233 { 234 return having; 235 } 236 237 243 public String getLimit() 244 { 245 return limit; 246 } 247 248 254 public String getPostLimit() 255 { 256 return postLimit; 257 } 258 259 265 public String getPreLimit() 266 { 267 return preLimit; 268 } 269 270 275 public boolean hasLimit() 276 { 277 return ((preLimit != null) 278 || (postLimit != null) 279 || (limit != null)); 280 } 281 282 288 public String getRowcount() 289 { 290 return rowcount; 291 } 292 293 298 public String toString() 299 { 300 return toStringBuffer(new StringBuffer ()).toString(); 301 } 302 303 public StringBuffer toStringBuffer(StringBuffer stmt) 304 { 305 if (preLimit != null) 306 { 307 stmt.append(preLimit); 308 } 309 310 if (rowcount != null) 311 { 312 stmt.append(ROWCOUNT) 313 .append(rowcount) 314 .append(" "); 315 } 316 stmt.append(SELECT) 317 .append(StringUtils.join(selectModifiers.iterator(), " ")) 318 .append(StringUtils.join(selectColumns.iterator(), ", ")) 319 .append(FROM); 320 321 boolean first = true; 322 for (Iterator it = fromTables.iterator(); it.hasNext();) 323 { 324 FromElement fromElement = (FromElement) it.next(); 325 326 if (!first && fromElement.getJoinCondition() == null) 327 { 328 stmt.append(", "); 329 } 330 first = false; 331 stmt.append(fromElement.toString()); 332 } 333 334 if (!whereCriteria.isEmpty()) 335 { 336 stmt.append(WHERE) 337 .append(StringUtils.join(whereCriteria.iterator(), AND)); 338 } 339 if (!groupByColumns.isEmpty()) 340 { 341 stmt.append(GROUP_BY) 342 .append(StringUtils.join(groupByColumns.iterator(), ", ")); 343 } 344 if (having != null) 345 { 346 stmt.append(HAVING) 347 .append(having); 348 } 349 if (!orderByColumns.isEmpty()) 350 { 351 stmt.append(ORDER_BY) 352 .append(StringUtils.join(orderByColumns.iterator(), ", ")); 353 } 354 if (limit != null) 355 { 356 stmt.append(LIMIT) 357 .append(limit); 358 } 359 if (rowcount != null) 360 { 361 stmt.append(ROWCOUNT) 362 .append("0"); 363 } 364 if (postLimit != null) 365 { 366 stmt.append(postLimit); 367 } 368 369 return stmt; 370 } 371 372 380 public static class FromElement 381 { 382 383 384 private String tableName = null; 385 386 387 private SqlEnum joinType = null; 388 389 390 private String joinCondition = null; 391 392 406 public FromElement(String tableName, 407 SqlEnum joinType, 408 String joinCondition) 409 { 410 this.tableName = tableName; 411 this.joinType = joinType; 412 this.joinCondition = joinCondition; 413 } 414 415 416 420 public String getJoinCondition() 421 { 422 return joinCondition; 423 } 424 425 429 public SqlEnum getJoinType() 430 { 431 return joinType; 432 } 433 434 441 public String getTableName() 442 { 443 return tableName; 444 } 445 446 450 public String toString() 451 { 452 StringBuffer result = new StringBuffer (); 453 if (joinType != null) 454 { 455 result.append(joinType); 456 } 457 result.append(tableName); 458 if (joinCondition != null) 459 { 460 result.append(SqlEnum.ON); 461 result.append(joinCondition); 462 } 463 return result.toString(); 464 } 465 } } 467
| Popular Tags
|