1 package org.hibernate.sql; 3 4 import org.hibernate.LockMode; 5 import org.hibernate.dialect.Dialect; 6 import org.hibernate.util.StringHelper; 7 8 9 13 public class Select { 14 15 private String selectClause; 16 private String fromClause; 17 private String outerJoinsAfterFrom; 18 private String whereClause; 19 private String outerJoinsAfterWhere; 20 private String orderByClause; 21 private String groupByClause; 22 private String comment; 23 private LockMode lockMode; 24 public final Dialect dialect; 25 26 private int guesstimatedBufferSize = 20; 27 28 public Select(Dialect dialect) { 29 this.dialect = dialect; 30 } 31 32 35 public String toStatementString() { 36 StringBuffer buf = new StringBuffer (guesstimatedBufferSize); 37 if ( StringHelper.isNotEmpty(comment) ) { 38 buf.append("/* ").append(comment).append(" */ "); 39 } 40 41 buf.append("select ").append(selectClause) 42 .append(" from ").append(fromClause); 43 44 if ( StringHelper.isNotEmpty(outerJoinsAfterFrom) ) { 45 buf.append(outerJoinsAfterFrom); 46 } 47 48 if ( StringHelper.isNotEmpty(whereClause) || StringHelper.isNotEmpty(outerJoinsAfterWhere) ) { 49 buf.append(" where " ); 50 if ( StringHelper.isNotEmpty(outerJoinsAfterWhere) ) { 53 buf.append(outerJoinsAfterWhere); 54 if ( StringHelper.isNotEmpty(whereClause) ) { 55 buf.append( " and " ); 56 } 57 } 58 if ( StringHelper.isNotEmpty(whereClause) ) { 59 buf.append(whereClause); 60 } 61 } 62 63 if ( StringHelper.isNotEmpty(groupByClause) ) { 64 buf.append(" group by ").append(groupByClause); 65 } 66 67 if ( StringHelper.isNotEmpty(orderByClause) ) { 68 buf.append(" order by ").append(orderByClause); 69 } 70 71 if (lockMode!=null) { 72 buf.append( dialect.getForUpdateString(lockMode) ); 73 } 74 75 return dialect.transformSelectString( buf.toString() ); 76 } 77 78 82 public Select setFromClause(String fromClause) { 83 this.fromClause = fromClause; 84 this.guesstimatedBufferSize += fromClause.length(); 85 return this; 86 } 87 88 public Select setFromClause(String tableName, String alias) { 89 this.fromClause = tableName + ' ' + alias; 90 this.guesstimatedBufferSize += fromClause.length(); 91 return this; 92 } 93 94 public Select setOrderByClause(String orderByClause) { 95 this.orderByClause = orderByClause; 96 this.guesstimatedBufferSize += orderByClause.length(); 97 return this; 98 } 99 100 public Select setGroupByClause(String groupByClause) { 101 this.groupByClause = groupByClause; 102 this.guesstimatedBufferSize += groupByClause.length(); 103 return this; 104 } 105 106 public Select setOuterJoins(String outerJoinsAfterFrom, String outerJoinsAfterWhere) { 107 this.outerJoinsAfterFrom = outerJoinsAfterFrom; 108 109 String tmpOuterJoinsAfterWhere = outerJoinsAfterWhere.trim(); 111 if ( tmpOuterJoinsAfterWhere.startsWith("and") ) { 112 tmpOuterJoinsAfterWhere = tmpOuterJoinsAfterWhere.substring(4); 113 } 114 this.outerJoinsAfterWhere = tmpOuterJoinsAfterWhere; 115 116 this.guesstimatedBufferSize += outerJoinsAfterFrom.length() + outerJoinsAfterWhere.length(); 117 return this; 118 } 119 120 121 125 public Select setSelectClause(String selectClause) { 126 this.selectClause = selectClause; 127 this.guesstimatedBufferSize += selectClause.length(); 128 return this; 129 } 130 131 135 public Select setWhereClause(String whereClause) { 136 this.whereClause = whereClause; 137 this.guesstimatedBufferSize += whereClause.length(); 138 return this; 139 } 140 141 public Select setComment(String comment) { 142 this.comment = comment; 143 this.guesstimatedBufferSize += comment.length(); 144 return this; 145 } 146 147 public LockMode getLockMode() { 148 return lockMode; 149 } 150 151 public Select setLockMode(LockMode lockMode) { 152 this.lockMode = lockMode; 153 return this; 154 } 155 } 156 | Popular Tags |