KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > sql > QuerySelect


1 //$Id: QuerySelect.java,v 1.6 2005/06/03 02:04:13 oneovthafew Exp $
2
package org.hibernate.sql;
3
4 import java.util.HashSet JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import org.hibernate.dialect.Dialect;
8
9 /**
10  * A translated HQL query
11  * @author Gavin King
12  */

13 public class QuerySelect {
14     private Dialect dialect;
15     private JoinFragment joins;
16     private StringBuffer JavaDoc select = new StringBuffer JavaDoc();
17     private StringBuffer JavaDoc where = new StringBuffer JavaDoc();
18     private StringBuffer JavaDoc groupBy = new StringBuffer JavaDoc();
19     private StringBuffer JavaDoc orderBy = new StringBuffer JavaDoc();
20     private StringBuffer JavaDoc having = new StringBuffer JavaDoc();
21     private String JavaDoc comment;
22     private boolean distinct=false;
23
24     private static final HashSet JavaDoc DONT_SPACE_TOKENS = new HashSet JavaDoc();
25     static {
26         //dontSpace.add("'");
27
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("("); //for MySQL
50
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 JavaDoc 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 JavaDoc columnName, String JavaDoc alias) {
72         addSelectFragmentString(columnName + ' ' + alias);
73     }
74
75     public void setDistinct(boolean distinct) {
76         this.distinct = distinct;
77     }
78
79     public void setWhereTokens(Iterator JavaDoc tokens) {
80         //if ( conjunctiveWhere.length()>0 ) conjunctiveWhere.append(" and ");
81
appendTokens(where, tokens);
82     }
83
84     public void prependWhereConditions(String JavaDoc 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 JavaDoc tokens) {
94         //if ( groupBy.length()>0 ) groupBy.append(" and ");
95
appendTokens(groupBy, tokens);
96     }
97
98     public void setOrderByTokens(Iterator JavaDoc tokens) {
99         //if ( orderBy.length()>0 ) orderBy.append(" and ");
100
appendTokens(orderBy, tokens);
101     }
102
103     public void setHavingTokens(Iterator JavaDoc tokens) {
104         //if ( having.length()>0 ) having.append(" and ");
105
appendTokens(having, tokens);
106     }
107
108     public void addOrderBy(String JavaDoc orderByString) {
109         if ( orderBy.length() > 0 ) orderBy.append(", ");
110         orderBy.append(orderByString);
111     }
112
113     public String JavaDoc toQueryString() {
114         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(50);
115         if (comment!=null) buf.append("/* ").append(comment).append(" */ ");
116         buf.append("select ");
117         if (distinct) buf.append("distinct ");
118         String JavaDoc 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 JavaDoc outerJoinsAfterWhere = joins.toWhereFragmentString().trim();
131         String JavaDoc 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 JavaDoc buf, Iterator JavaDoc iter) {
158         boolean lastSpaceable=true;
159         boolean lastQuoted=false;
160         while ( iter.hasNext() ) {
161             String JavaDoc token = (String JavaDoc) 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 JavaDoc 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