KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: Select.java,v 1.14 2005/06/15 02:57:03 oneovthafew Exp $
2
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 /**
10  * A simple SQL <tt>SELECT</tt> statement
11  * @author Gavin King
12  */

13 public class Select {
14
15     private String JavaDoc selectClause;
16     private String JavaDoc fromClause;
17     private String JavaDoc outerJoinsAfterFrom;
18     private String JavaDoc whereClause;
19     private String JavaDoc outerJoinsAfterWhere;
20     private String JavaDoc orderByClause;
21     private String JavaDoc groupByClause;
22     private String JavaDoc 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     /**
33      * Construct an SQL <tt>SELECT</tt> statement from the given clauses
34      */

35     public String JavaDoc toStatementString() {
36         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(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             // the outerJoinsAfterWhere needs to come before where clause to properly
51
// handle dynamic filters
52
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     /**
79      * Sets the fromClause.
80      * @param fromClause The fromClause to set
81      */

82     public Select setFromClause(String JavaDoc fromClause) {
83         this.fromClause = fromClause;
84         this.guesstimatedBufferSize += fromClause.length();
85         return this;
86     }
87
88     public Select setFromClause(String JavaDoc tableName, String JavaDoc alias) {
89         this.fromClause = tableName + ' ' + alias;
90         this.guesstimatedBufferSize += fromClause.length();
91         return this;
92     }
93
94     public Select setOrderByClause(String JavaDoc orderByClause) {
95         this.orderByClause = orderByClause;
96         this.guesstimatedBufferSize += orderByClause.length();
97         return this;
98     }
99
100     public Select setGroupByClause(String JavaDoc groupByClause) {
101         this.groupByClause = groupByClause;
102         this.guesstimatedBufferSize += groupByClause.length();
103         return this;
104     }
105
106     public Select setOuterJoins(String JavaDoc outerJoinsAfterFrom, String JavaDoc outerJoinsAfterWhere) {
107         this.outerJoinsAfterFrom = outerJoinsAfterFrom;
108
109         // strip off any leading 'and' token
110
String JavaDoc 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     /**
122      * Sets the selectClause.
123      * @param selectClause The selectClause to set
124      */

125     public Select setSelectClause(String JavaDoc selectClause) {
126         this.selectClause = selectClause;
127         this.guesstimatedBufferSize += selectClause.length();
128         return this;
129     }
130
131     /**
132      * Sets the whereClause.
133      * @param whereClause The whereClause to set
134      */

135     public Select setWhereClause(String JavaDoc whereClause) {
136         this.whereClause = whereClause;
137         this.guesstimatedBufferSize += whereClause.length();
138         return this;
139     }
140
141     public Select setComment(String JavaDoc 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