KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > engine > QueryParameters


1 //$Id: QueryParameters.java,v 1.17 2005/05/25 01:22:05 oneovthafew Exp $
2
package org.hibernate.engine;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Arrays JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.StringTokenizer JavaDoc;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import org.hibernate.HibernateException;
17 import org.hibernate.QueryException;
18 import org.hibernate.ScrollMode;
19 import org.hibernate.dialect.Dialect;
20 import org.hibernate.hql.classic.ParserHelper;
21 import org.hibernate.pretty.Printer;
22 import org.hibernate.type.Type;
23 import org.hibernate.util.ArrayHelper;
24
25 /**
26  * @author Gavin King
27  */

28 public final class QueryParameters {
29     private static final Log log = LogFactory.getLog(QueryParameters.class);
30
31     private Type[] positionalParameterTypes;
32     private Object JavaDoc[] positionalParameterValues;
33     private Map JavaDoc namedParameters;
34     private Map JavaDoc lockModes;
35     private RowSelection rowSelection;
36     private boolean cacheable;
37     private String JavaDoc cacheRegion;
38     private String JavaDoc comment;
39     private ScrollMode scrollMode;
40     private Serializable JavaDoc[] collectionKeys;
41     private Object JavaDoc optionalObject;
42     private String JavaDoc optionalEntityName;
43     private Serializable JavaDoc optionalId;
44     private boolean readOnly;
45     private boolean callable = false;
46     private boolean isNaturalKeyLookup;
47     
48     private String JavaDoc processedSQL;
49     private Type[] processedPositionalParameterTypes;
50     private Object JavaDoc[] processedPositionalParameterValues;
51     
52     public QueryParameters() {
53         this( ArrayHelper.EMPTY_TYPE_ARRAY, ArrayHelper.EMPTY_OBJECT_ARRAY );
54     }
55
56     public QueryParameters(Type type, Object JavaDoc value) {
57         this( new Type[] {type}, new Object JavaDoc[] {value} );
58     }
59
60     public QueryParameters(
61         final Type[] positionalParameterTypes,
62         final Object JavaDoc[] postionalParameterValues,
63         final Object JavaDoc optionalObject,
64         final String JavaDoc optionalEntityName,
65         final Serializable JavaDoc optionalObjectId
66     ) {
67         this(positionalParameterTypes, postionalParameterValues);
68         this.optionalObject = optionalObject;
69         this.optionalId = optionalObjectId;
70         this.optionalEntityName = optionalEntityName;
71
72     }
73
74     public QueryParameters(
75         final Type[] positionalParameterTypes,
76         final Object JavaDoc[] postionalParameterValues
77     ) {
78         this(
79             positionalParameterTypes,
80             postionalParameterValues,
81             null,
82             null,
83             false,
84             null,
85             null,
86             false
87         );
88     }
89
90     public QueryParameters(
91         final Type[] positionalParameterTypes,
92         final Object JavaDoc[] postionalParameterValues,
93         final Serializable JavaDoc[] collectionKeys
94     ) {
95         this(
96             positionalParameterTypes,
97             postionalParameterValues,
98             null,
99             collectionKeys
100         );
101     }
102
103     public QueryParameters(
104             final Type[] positionalParameterTypes,
105             final Object JavaDoc[] postionalParameterValues,
106             final Map JavaDoc namedParameters,
107             final Serializable JavaDoc[] collectionKeys
108         ) {
109             this(
110                 positionalParameterTypes,
111                 postionalParameterValues,
112                 namedParameters,
113                 null,
114                 null,
115                 false,
116                 false,
117                 null,
118                 null,
119                 collectionKeys
120             );
121         }
122
123     public QueryParameters(
124         final Type[] positionalParameterTypes,
125         final Object JavaDoc[] positionalParameterValues,
126         final Map JavaDoc lockModes,
127         final RowSelection rowSelection,
128         final boolean cacheable,
129         final String JavaDoc cacheRegion,
130         //final boolean forceCacheRefresh,
131
final String JavaDoc comment,
132         final boolean isLookupByNaturalKey
133     ) {
134         this(
135             positionalParameterTypes,
136             positionalParameterValues,
137             null,
138             lockModes,
139             rowSelection,
140             false,
141             cacheable,
142             cacheRegion,
143             comment,
144             null
145         );
146         isNaturalKeyLookup = isLookupByNaturalKey;
147     }
148
149     public QueryParameters(
150         final Type[] positionalParameterTypes,
151         final Object JavaDoc[] positionalParameterValues,
152         final Map JavaDoc namedParameters,
153         final Map JavaDoc lockModes,
154         final RowSelection rowSelection,
155         final boolean readOnly,
156         final boolean cacheable,
157         final String JavaDoc cacheRegion,
158         //final boolean forceCacheRefresh,
159
final String JavaDoc comment,
160         final Serializable JavaDoc[] collectionKeys
161     ) {
162         this.positionalParameterTypes = positionalParameterTypes;
163         this.positionalParameterValues = positionalParameterValues;
164         this.namedParameters = namedParameters;
165         this.lockModes = lockModes;
166         this.rowSelection = rowSelection;
167         this.cacheable = cacheable;
168         this.cacheRegion = cacheRegion;
169         //this.forceCacheRefresh = forceCacheRefresh;
170
this.comment = comment;
171         this.collectionKeys = collectionKeys;
172         this.readOnly = readOnly;
173     }
174
175     public boolean hasRowSelection() {
176         return rowSelection!=null;
177     }
178
179     public Map JavaDoc getNamedParameters() {
180         return namedParameters;
181     }
182
183     public Type[] getPositionalParameterTypes() {
184         return positionalParameterTypes;
185     }
186
187     public Object JavaDoc[] getPositionalParameterValues() {
188         return positionalParameterValues;
189     }
190
191     public RowSelection getRowSelection() {
192         return rowSelection;
193     }
194
195     public void setNamedParameters(Map JavaDoc map) {
196         namedParameters = map;
197     }
198
199     public void setPositionalParameterTypes(Type[] types) {
200         positionalParameterTypes = types;
201     }
202
203     public void setPositionalParameterValues(Object JavaDoc[] objects) {
204         positionalParameterValues = objects;
205     }
206
207     public void setRowSelection(RowSelection selection) {
208         rowSelection = selection;
209     }
210
211     public Map JavaDoc getLockModes() {
212         return lockModes;
213     }
214
215     public void setLockModes(Map JavaDoc map) {
216         lockModes = map;
217     }
218
219     public void traceParameters(SessionFactoryImplementor factory) throws HibernateException {
220         Printer print = new Printer(factory);
221         if (positionalParameterValues.length!=0) log.trace(
222             "parameters: " + print.toString(positionalParameterTypes, positionalParameterValues)
223         );
224         if (namedParameters!=null) log.trace(
225             "named parameters: " + print.toString(namedParameters)
226         );
227     }
228
229     public boolean isCacheable() {
230         return cacheable;
231     }
232
233     public void setCacheable(boolean b) {
234         cacheable = b;
235     }
236
237     public String JavaDoc getCacheRegion() {
238         return cacheRegion;
239     }
240
241     public void setCacheRegion(String JavaDoc cacheRegion) {
242         this.cacheRegion = cacheRegion;
243     }
244
245     public void validateParameters() throws QueryException {
246         int types = positionalParameterTypes==null ? 0 : positionalParameterTypes.length;
247         int values = positionalParameterValues==null ? 0 : positionalParameterValues.length;
248         if (types!=values) {
249             throw new QueryException("Number of positional parameter types (" + types + " does not match number of positional parameters (" + values + ")");
250         }
251     }
252
253     public String JavaDoc getComment() {
254         return comment;
255     }
256
257     public void setComment(String JavaDoc comment) {
258         this.comment = comment;
259     }
260
261     public ScrollMode getScrollMode() {
262         return scrollMode;
263     }
264
265     public void setScrollMode(ScrollMode scrollMode) {
266         this.scrollMode = scrollMode;
267     }
268
269     public Serializable JavaDoc[] getCollectionKeys() {
270         return collectionKeys;
271     }
272
273     public void setCollectionKeys(Serializable JavaDoc[] collectionKeys) {
274         this.collectionKeys = collectionKeys;
275     }
276
277     public String JavaDoc getOptionalEntityName() {
278         return optionalEntityName;
279     }
280
281     public void setOptionalEntityName(String JavaDoc optionalEntityName) {
282         this.optionalEntityName = optionalEntityName;
283     }
284
285     public Serializable JavaDoc getOptionalId() {
286         return optionalId;
287     }
288
289     public void setOptionalId(Serializable JavaDoc optionalId) {
290         this.optionalId = optionalId;
291     }
292
293     public Object JavaDoc getOptionalObject() {
294         return optionalObject;
295     }
296
297     public void setOptionalObject(Object JavaDoc optionalObject) {
298         this.optionalObject = optionalObject;
299     }
300
301     public boolean isReadOnly() {
302         return readOnly;
303     }
304
305     public void setReadOnly(boolean readOnly) {
306         this.readOnly = readOnly;
307     }
308
309     public void setCallable(boolean callable) {
310         this.callable = callable;
311     }
312
313     public boolean isCallable() {
314         return callable;
315     }
316
317     public void processFilters(String JavaDoc sql, SessionImplementor session) {
318         
319         if ( session.getEnabledFilters().size()==0 || sql.indexOf(ParserHelper.HQL_VARIABLE_PREFIX)<0 ) {
320             // HELLA IMPORTANT OPTIMIZATION!!!
321
processedPositionalParameterValues = getPositionalParameterValues();
322             processedPositionalParameterTypes = getPositionalParameterTypes();
323             processedSQL = sql;
324         }
325         else {
326             
327             Dialect dialect = session.getFactory().getDialect();
328             String JavaDoc symbols = new StringBuffer JavaDoc().append( ParserHelper.HQL_SEPARATORS )
329                     .append( dialect.openQuote() )
330                     .append( dialect.closeQuote() )
331                     .toString();
332             StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc( sql, symbols, true );
333             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
334         
335             List JavaDoc parameters = new ArrayList JavaDoc();
336             List JavaDoc parameterTypes = new ArrayList JavaDoc();
337         
338             while ( tokens.hasMoreTokens() ) {
339                 final String JavaDoc token = tokens.nextToken();
340                 if ( token.startsWith( ParserHelper.HQL_VARIABLE_PREFIX ) ) {
341                     String JavaDoc filterParameterName = token.substring( 1 );
342                     Object JavaDoc value = session.getFilterParameterValue( filterParameterName );
343                     Type type = session.getFilterParameterType( filterParameterName );
344                     if ( value != null && Collection JavaDoc.class.isAssignableFrom( value.getClass() ) ) {
345                         Iterator JavaDoc itr = ( ( Collection JavaDoc ) value ).iterator();
346                         while ( itr.hasNext() ) {
347                             Object JavaDoc elementValue = itr.next();
348                             result.append( '?' );
349                             parameters.add( elementValue );
350                             parameterTypes.add( type );
351                             if ( itr.hasNext() ) {
352                                 result.append( ", " );
353                             }
354                         }
355                     }
356                     else {
357                         result.append( '?' );
358                         parameters.add( value );
359                         parameterTypes.add( type );
360                     }
361                 }
362                 else {
363                     result.append( token );
364                 }
365             }
366             parameters.addAll( Arrays.asList( getPositionalParameterValues() ) );
367             parameterTypes.addAll( Arrays.asList( getPositionalParameterTypes() ) );
368             processedPositionalParameterValues = parameters.toArray();
369             processedPositionalParameterTypes = ( Type[] ) parameterTypes.toArray( new Type[0] );
370             processedSQL = result.toString();
371             
372         }
373     }
374
375     public String JavaDoc getFilteredSQL() {
376         return processedSQL;
377     }
378
379     public Object JavaDoc[] getFilteredPositionalParameterValues() {
380         return processedPositionalParameterValues;
381     }
382
383     public Type[] getFilteredPositionalParameterTypes() {
384         return processedPositionalParameterTypes;
385     }
386
387     public boolean isNaturalKeyLookup() {
388         return isNaturalKeyLookup;
389     }
390
391     public void setNaturalKeyLookup(boolean isNaturalKeyLookup) {
392         this.isNaturalKeyLookup = isNaturalKeyLookup;
393     }
394         
395 }
396
Popular Tags