1 package org.hibernate.engine; 3 4 import java.io.Serializable ; 5 import java.util.ArrayList ; 6 import java.util.Arrays ; 7 import java.util.Collection ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Map ; 11 import java.util.StringTokenizer ; 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 28 public final class QueryParameters { 29 private static final Log log = LogFactory.getLog(QueryParameters.class); 30 31 private Type[] positionalParameterTypes; 32 private Object [] positionalParameterValues; 33 private Map namedParameters; 34 private Map lockModes; 35 private RowSelection rowSelection; 36 private boolean cacheable; 37 private String cacheRegion; 38 private String comment; 39 private ScrollMode scrollMode; 40 private Serializable [] collectionKeys; 41 private Object optionalObject; 42 private String optionalEntityName; 43 private Serializable optionalId; 44 private boolean readOnly; 45 private boolean callable = false; 46 private boolean isNaturalKeyLookup; 47 48 private String processedSQL; 49 private Type[] processedPositionalParameterTypes; 50 private Object [] processedPositionalParameterValues; 51 52 public QueryParameters() { 53 this( ArrayHelper.EMPTY_TYPE_ARRAY, ArrayHelper.EMPTY_OBJECT_ARRAY ); 54 } 55 56 public QueryParameters(Type type, Object value) { 57 this( new Type[] {type}, new Object [] {value} ); 58 } 59 60 public QueryParameters( 61 final Type[] positionalParameterTypes, 62 final Object [] postionalParameterValues, 63 final Object optionalObject, 64 final String optionalEntityName, 65 final Serializable 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 [] 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 [] postionalParameterValues, 93 final Serializable [] 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 [] postionalParameterValues, 106 final Map namedParameters, 107 final Serializable [] 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 [] positionalParameterValues, 126 final Map lockModes, 127 final RowSelection rowSelection, 128 final boolean cacheable, 129 final String cacheRegion, 130 final String 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 [] positionalParameterValues, 152 final Map namedParameters, 153 final Map lockModes, 154 final RowSelection rowSelection, 155 final boolean readOnly, 156 final boolean cacheable, 157 final String cacheRegion, 158 final String comment, 160 final Serializable [] 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.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 getNamedParameters() { 180 return namedParameters; 181 } 182 183 public Type[] getPositionalParameterTypes() { 184 return positionalParameterTypes; 185 } 186 187 public Object [] getPositionalParameterValues() { 188 return positionalParameterValues; 189 } 190 191 public RowSelection getRowSelection() { 192 return rowSelection; 193 } 194 195 public void setNamedParameters(Map map) { 196 namedParameters = map; 197 } 198 199 public void setPositionalParameterTypes(Type[] types) { 200 positionalParameterTypes = types; 201 } 202 203 public void setPositionalParameterValues(Object [] objects) { 204 positionalParameterValues = objects; 205 } 206 207 public void setRowSelection(RowSelection selection) { 208 rowSelection = selection; 209 } 210 211 public Map getLockModes() { 212 return lockModes; 213 } 214 215 public void setLockModes(Map 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 getCacheRegion() { 238 return cacheRegion; 239 } 240 241 public void setCacheRegion(String 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 getComment() { 254 return comment; 255 } 256 257 public void setComment(String 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 [] getCollectionKeys() { 270 return collectionKeys; 271 } 272 273 public void setCollectionKeys(Serializable [] collectionKeys) { 274 this.collectionKeys = collectionKeys; 275 } 276 277 public String getOptionalEntityName() { 278 return optionalEntityName; 279 } 280 281 public void setOptionalEntityName(String optionalEntityName) { 282 this.optionalEntityName = optionalEntityName; 283 } 284 285 public Serializable getOptionalId() { 286 return optionalId; 287 } 288 289 public void setOptionalId(Serializable optionalId) { 290 this.optionalId = optionalId; 291 } 292 293 public Object getOptionalObject() { 294 return optionalObject; 295 } 296 297 public void setOptionalObject(Object 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 sql, SessionImplementor session) { 318 319 if ( session.getEnabledFilters().size()==0 || sql.indexOf(ParserHelper.HQL_VARIABLE_PREFIX)<0 ) { 320 processedPositionalParameterValues = getPositionalParameterValues(); 322 processedPositionalParameterTypes = getPositionalParameterTypes(); 323 processedSQL = sql; 324 } 325 else { 326 327 Dialect dialect = session.getFactory().getDialect(); 328 String symbols = new StringBuffer ().append( ParserHelper.HQL_SEPARATORS ) 329 .append( dialect.openQuote() ) 330 .append( dialect.closeQuote() ) 331 .toString(); 332 StringTokenizer tokens = new StringTokenizer ( sql, symbols, true ); 333 StringBuffer result = new StringBuffer (); 334 335 List parameters = new ArrayList (); 336 List parameterTypes = new ArrayList (); 337 338 while ( tokens.hasMoreTokens() ) { 339 final String token = tokens.nextToken(); 340 if ( token.startsWith( ParserHelper.HQL_VARIABLE_PREFIX ) ) { 341 String filterParameterName = token.substring( 1 ); 342 Object value = session.getFilterParameterValue( filterParameterName ); 343 Type type = session.getFilterParameterType( filterParameterName ); 344 if ( value != null && Collection .class.isAssignableFrom( value.getClass() ) ) { 345 Iterator itr = ( ( Collection ) value ).iterator(); 346 while ( itr.hasNext() ) { 347 Object 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 getFilteredSQL() { 376 return processedSQL; 377 } 378 379 public Object [] 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 |