1 package org.hibernate.sql; 3 4 import java.util.HashSet ; 5 import java.util.Iterator ; 6 7 import org.hibernate.dialect.Dialect; 8 9 13 public class QuerySelect { 14 private Dialect dialect; 15 private JoinFragment joins; 16 private StringBuffer select = new StringBuffer (); 17 private StringBuffer where = new StringBuffer (); 18 private StringBuffer groupBy = new StringBuffer (); 19 private StringBuffer orderBy = new StringBuffer (); 20 private StringBuffer having = new StringBuffer (); 21 private String comment; 22 private boolean distinct=false; 23 24 private static final HashSet DONT_SPACE_TOKENS = new HashSet (); 25 static { 26 DONT_SPACE_TOKENS.add("."); 28 DONT_SPACE_TOKENS.add("+"); 29 DONT_SPACE_TOKENS.add("-"); 30 DONT_SPACE_TOKENS.add("/"); 31 DONT_SPACE_TOKENS.add("*"); 32 DONT_SPACE_TOKENS.add("<"); 33 DONT_SPACE_TOKENS.add(">"); 34 DONT_SPACE_TOKENS.add("="); 35 DONT_SPACE_TOKENS.add("#"); 36 DONT_SPACE_TOKENS.add("~"); 37 DONT_SPACE_TOKENS.add("|"); 38 DONT_SPACE_TOKENS.add("&"); 39 DONT_SPACE_TOKENS.add("<="); 40 DONT_SPACE_TOKENS.add(">="); 41 DONT_SPACE_TOKENS.add("=>"); 42 DONT_SPACE_TOKENS.add("=<"); 43 DONT_SPACE_TOKENS.add("!="); 44 DONT_SPACE_TOKENS.add("<>"); 45 DONT_SPACE_TOKENS.add("!#"); 46 DONT_SPACE_TOKENS.add("!~"); 47 DONT_SPACE_TOKENS.add("!<"); 48 DONT_SPACE_TOKENS.add("!>"); 49 DONT_SPACE_TOKENS.add("("); DONT_SPACE_TOKENS.add(")"); 51 } 52 53 public QuerySelect(Dialect dialect) { 54 this.dialect = dialect; 55 joins = new QueryJoinFragment(dialect, false); 56 } 57 58 public JoinFragment getJoinFragment() { 59 return joins; 60 } 61 62 public void addSelectFragmentString(String fragment) { 63 if ( fragment.length()>0 && fragment.charAt(0)==',' ) fragment = fragment.substring(1); 64 fragment = fragment.trim(); 65 if ( fragment.length()>0 ) { 66 if ( select.length()>0 ) select.append(", "); 67 select.append(fragment); 68 } 69 } 70 71 public void addSelectColumn(String columnName, String alias) { 72 addSelectFragmentString(columnName + ' ' + alias); 73 } 74 75 public void setDistinct(boolean distinct) { 76 this.distinct = distinct; 77 } 78 79 public void setWhereTokens(Iterator tokens) { 80 appendTokens(where, tokens); 82 } 83 84 public void prependWhereConditions(String conditions) { 85 if (where.length() > 0) { 86 where.insert(0, conditions + " and "); 87 } 88 else { 89 where.append(conditions); 90 } 91 } 92 93 public void setGroupByTokens(Iterator tokens) { 94 appendTokens(groupBy, tokens); 96 } 97 98 public void setOrderByTokens(Iterator tokens) { 99 appendTokens(orderBy, tokens); 101 } 102 103 public void setHavingTokens(Iterator tokens) { 104 appendTokens(having, tokens); 106 } 107 108 public void addOrderBy(String orderByString) { 109 if ( orderBy.length() > 0 ) orderBy.append(", "); 110 orderBy.append(orderByString); 111 } 112 113 public String toQueryString() { 114 StringBuffer buf = new StringBuffer (50); 115 if (comment!=null) buf.append("/* ").append(comment).append(" */ "); 116 buf.append("select "); 117 if (distinct) buf.append("distinct "); 118 String from = joins.toFromFragmentString(); 119 if ( from.startsWith(",") ) { 120 from = from.substring(1); 121 } 122 else if ( from.startsWith(" inner join") ){ 123 from = from.substring(11); 124 } 125 126 buf.append( select.toString() ) 127 .append(" from") 128 .append(from); 129 130 String outerJoinsAfterWhere = joins.toWhereFragmentString().trim(); 131 String whereConditions = where.toString().trim(); 132 boolean hasOuterJoinsAfterWhere = outerJoinsAfterWhere.length() > 0; 133 boolean hasWhereConditions = whereConditions.length() > 0; 134 if (hasOuterJoinsAfterWhere || hasWhereConditions) { 135 buf.append(" where "); 136 if (hasOuterJoinsAfterWhere) { 137 buf.append( outerJoinsAfterWhere.substring(4) ); 138 } 139 if (hasWhereConditions) { 140 if (hasOuterJoinsAfterWhere) { 141 buf.append(" and ("); 142 } 143 buf.append(whereConditions); 144 if (hasOuterJoinsAfterWhere) { 145 buf.append(")"); 146 } 147 } 148 } 149 150 if ( groupBy.length() > 0 ) buf.append(" group by ").append( groupBy.toString() ); 151 if ( having.length() > 0 ) buf.append(" having ").append( having.toString() ); 152 if ( orderBy.length() > 0 ) buf.append(" order by ").append( orderBy.toString() ); 153 154 return dialect.transformSelectString( buf.toString() ); 155 } 156 157 private static void appendTokens(StringBuffer buf, Iterator iter) { 158 boolean lastSpaceable=true; 159 boolean lastQuoted=false; 160 while ( iter.hasNext() ) { 161 String token = (String ) iter.next(); 162 boolean spaceable = !DONT_SPACE_TOKENS.contains(token); 163 boolean quoted = token.startsWith("'"); 164 if (spaceable && lastSpaceable) { 165 if ( !quoted || !lastQuoted ) buf.append(' '); 166 } 167 lastSpaceable = spaceable; 168 buf.append(token); 169 lastQuoted = token.endsWith("'"); 170 } 171 } 172 173 public void setComment(String comment) { 174 this.comment = comment; 175 } 176 177 public QuerySelect copy() { 178 QuerySelect copy = new QuerySelect(dialect); 179 copy.joins = this.joins.copy(); 180 copy.select.append( this.select.toString() ); 181 copy.where.append( this.where.toString() ); 182 copy.groupBy.append( this.groupBy.toString() ); 183 copy.orderBy.append( this.orderBy.toString() ); 184 copy.having.append( this.having.toString() ); 185 copy.comment = this.comment; 186 copy.distinct = this.distinct; 187 return copy; 188 } 189 190 } 191 | Popular Tags |