-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
QueryParameters.java
597 lines (526 loc) · 17.3 KB
/
QueryParameters.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.engine.spi;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.LockOptions;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.QueryException;
import org.hibernate.ScrollMode;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.util.EntityPrinter;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.query.spi.QueryOptions;
import org.hibernate.query.spi.QueryParameterBindings;
import org.hibernate.transform.ResultTransformer;
import org.hibernate.type.ComponentType;
import org.hibernate.type.Type;
import org.jboss.logging.Logger;
/**
* @author Gavin King
*/
public final class QueryParameters {
private static final Logger LOG = CoreLogging.logger( QueryParameters.class );
public static final String HQL_VARIABLE_PREFIX = ":";
private Type[] positionalParameterTypes;
private Object[] positionalParameterValues;
private Map<String, TypedValue> namedParameters;
private LockOptions lockOptions;
private RowSelection rowSelection;
private boolean cacheable;
private String cacheRegion;
private String comment;
private List<String> queryHints;
private ScrollMode scrollMode;
private Serializable[] collectionKeys;
private Object optionalObject;
private String optionalEntityName;
private Serializable optionalId;
private boolean isReadOnlyInitialized;
private boolean readOnly;
private boolean callable;
private boolean autodiscovertypes;
private boolean isNaturalKeyLookup;
private boolean passDistinctThrough = true;
private final ResultTransformer resultTransformer; // why is all others non final ?
private String processedSQL;
private Type[] processedPositionalParameterTypes;
private Object[] processedPositionalParameterValues;
public QueryParameters() {
this( ArrayHelper.EMPTY_TYPE_ARRAY, ArrayHelper.EMPTY_OBJECT_ARRAY );
}
public QueryParameters(Type type, Object value) {
this( new Type[] { type }, new Object[] { value } );
}
public QueryParameters(
final Type[] positionalParameterTypes,
final Object[] positionalParameterValues,
final Object optionalObject,
final String optionalEntityName,
final Serializable optionalObjectId) {
this( positionalParameterTypes, positionalParameterValues );
this.optionalObject = optionalObject;
this.optionalId = optionalObjectId;
this.optionalEntityName = optionalEntityName;
}
public QueryParameters(
final Type[] positionalParameterTypes,
final Object[] positionalParameterValues) {
this( positionalParameterTypes, positionalParameterValues, null, null, false, false, false, null, null, null, false, null );
}
public QueryParameters(
final Type[] positionalParameterTypes,
final Object[] positionalParameterValues,
final Object[] collectionKeys) {
this( positionalParameterTypes, positionalParameterValues, null, (Serializable[]) collectionKeys );
}
public QueryParameters(
final Type[] positionalParameterTypes,
final Object[] positionalParameterValues,
final Serializable[] collectionKeys) {
this( positionalParameterTypes, positionalParameterValues, null, collectionKeys );
}
public QueryParameters(
final Type[] positionalParameterTypes,
final Object[] positionalParameterValues,
final Map<String, TypedValue> namedParameters,
final Serializable[] collectionKeys) {
this(
positionalParameterTypes,
positionalParameterValues,
namedParameters,
null,
null,
false,
false,
false,
null,
null,
null,
collectionKeys,
null
);
}
public QueryParameters(
final Type[] positionalParameterTypes,
final Object[] positionalParameterValues,
final LockOptions lockOptions,
final RowSelection rowSelection,
final boolean isReadOnlyInitialized,
final boolean readOnly,
final boolean cacheable,
final String cacheRegion,
//final boolean forceCacheRefresh,
final String comment,
final List<String> queryHints,
final boolean isLookupByNaturalKey,
final ResultTransformer transformer) {
this(
positionalParameterTypes,
positionalParameterValues,
null,
lockOptions,
rowSelection,
isReadOnlyInitialized,
readOnly,
cacheable,
cacheRegion,
comment,
queryHints,
null,
transformer
);
isNaturalKeyLookup = isLookupByNaturalKey;
}
public QueryParameters(
final Type[] positionalParameterTypes,
final Object[] positionalParameterValues,
final Map<String, TypedValue> namedParameters,
final LockOptions lockOptions,
final RowSelection rowSelection,
final boolean isReadOnlyInitialized,
final boolean readOnly,
final boolean cacheable,
final String cacheRegion,
//final boolean forceCacheRefresh,
final String comment,
final List<String> queryHints,
final Serializable[] collectionKeys,
ResultTransformer transformer) {
this.positionalParameterTypes = positionalParameterTypes;
this.positionalParameterValues = positionalParameterValues;
this.namedParameters = namedParameters;
this.lockOptions = lockOptions;
this.rowSelection = rowSelection;
this.cacheable = cacheable;
this.cacheRegion = cacheRegion;
//this.forceCacheRefresh = forceCacheRefresh;
this.comment = comment;
this.queryHints = queryHints;
this.collectionKeys = collectionKeys;
this.isReadOnlyInitialized = isReadOnlyInitialized;
this.readOnly = readOnly;
this.resultTransformer = transformer;
}
public QueryParameters(
final Type[] positionalParameterTypes,
final Object[] positionalParameterValues,
final Map<String, TypedValue> namedParameters,
final LockOptions lockOptions,
final RowSelection rowSelection,
final boolean isReadOnlyInitialized,
final boolean readOnly,
final boolean cacheable,
final String cacheRegion,
//final boolean forceCacheRefresh,
final String comment,
final List<String> queryHints,
final Serializable[] collectionKeys,
final Object optionalObject,
final String optionalEntityName,
final Serializable optionalId,
final ResultTransformer transformer) {
this(
positionalParameterTypes,
positionalParameterValues,
namedParameters,
lockOptions,
rowSelection,
isReadOnlyInitialized,
readOnly,
cacheable,
cacheRegion,
comment,
queryHints,
collectionKeys,
transformer
);
this.optionalEntityName = optionalEntityName;
this.optionalId = optionalId;
this.optionalObject = optionalObject;
}
public QueryParameters(
QueryParameterBindings queryParameterBindings,
LockOptions lockOptions,
QueryOptions queryOptions,
final Serializable[] collectionKeys,
final Object optionalObject,
final String optionalEntityName,
final Serializable optionalId,
ResultTransformer resultTransformer) {
this(
null,
null,
null,
lockOptions,
new RowSelection(
queryOptions.getFirstRow(),
queryOptions.getMaxRows(),
queryOptions.getTimeout(),
queryOptions.getFetchSize()
),
queryOptions.isReadOnly() != null,
queryOptions.isReadOnly(),
queryOptions.isResultCachingEnabled(),
queryOptions.getResultCacheRegionName(),
queryOptions.getComment(),
queryOptions.getDatabaseHints(),
collectionKeys,
optionalObject,
optionalEntityName,
optionalId,
resultTransformer
);
}
@SuppressWarnings( {"UnusedDeclaration"})
public boolean hasRowSelection() {
return rowSelection != null;
}
public Map<String, TypedValue> getNamedParameters() {
return namedParameters;
}
public Type[] getPositionalParameterTypes() {
return positionalParameterTypes;
}
public Object[] getPositionalParameterValues() {
return positionalParameterValues;
}
public RowSelection getRowSelection() {
return rowSelection;
}
public ResultTransformer getResultTransformer() {
return resultTransformer;
}
@SuppressWarnings( {"UnusedDeclaration"})
public void setNamedParameters(Map<String, TypedValue> map) {
namedParameters = map;
}
public void setPositionalParameterTypes(Type[] types) {
positionalParameterTypes = types;
}
public void setPositionalParameterValues(Object[] objects) {
positionalParameterValues = objects;
}
@SuppressWarnings( {"UnusedDeclaration"})
public void setRowSelection(RowSelection selection) {
rowSelection = selection;
}
public LockOptions getLockOptions() {
return lockOptions;
}
public void setLockOptions(LockOptions lockOptions) {
this.lockOptions = lockOptions;
}
public void traceParameters(SessionFactoryImplementor factory) throws HibernateException {
EntityPrinter print = new EntityPrinter( factory );
if ( positionalParameterValues.length != 0 ) {
LOG.tracev( "Parameters: {0}", print.toString( positionalParameterTypes, positionalParameterValues ) );
}
if ( namedParameters != null ) {
LOG.tracev( "Named parameters: {0}", print.toString( namedParameters ) );
}
}
public boolean isCacheable() {
return cacheable;
}
public void setCacheable(boolean b) {
cacheable = b;
}
public String getCacheRegion() {
return cacheRegion;
}
public void setCacheRegion(String cacheRegion) {
this.cacheRegion = cacheRegion;
}
public void validateParameters() throws QueryException {
final int types = positionalParameterTypes == null ? 0 : positionalParameterTypes.length;
final int values = positionalParameterValues == null ? 0 : positionalParameterValues.length;
if ( types != values ) {
throw new QueryException(
"Number of positional parameter types [" + types +
"] does not match number of positional parameters [" + values + "]"
);
}
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public List<String> getQueryHints() {
return queryHints;
}
public void setQueryHints(List<String> queryHints) {
this.queryHints = queryHints;
}
public ScrollMode getScrollMode() {
return scrollMode;
}
public void setScrollMode(ScrollMode scrollMode) {
this.scrollMode = scrollMode;
}
public Serializable[] getCollectionKeys() {
return collectionKeys;
}
@SuppressWarnings( {"UnusedDeclaration"})
public void setCollectionKeys(Serializable[] collectionKeys) {
this.collectionKeys = collectionKeys;
}
public String getOptionalEntityName() {
return optionalEntityName;
}
public void setOptionalEntityName(String optionalEntityName) {
this.optionalEntityName = optionalEntityName;
}
public Serializable getOptionalId() {
return optionalId;
}
public void setOptionalId(Serializable optionalId) {
this.optionalId = optionalId;
}
public Object getOptionalObject() {
return optionalObject;
}
public void setOptionalObject(Object optionalObject) {
this.optionalObject = optionalObject;
}
/**
* Has the read-only/modifiable mode been explicitly set?
* @see QueryParameters#setReadOnly(boolean)
* @see QueryParameters#isReadOnly(SharedSessionContractImplementor)
*
* @return true, the read-only/modifiable mode was explicitly set
* false, the read-only/modifiable mode was not explicitly set
*/
public boolean isReadOnlyInitialized() {
return isReadOnlyInitialized;
}
/**
* Should entities and proxies loaded by the Query be put in read-only mode? The
* read-only/modifiable setting must be initialized via QueryParameters#setReadOnly(boolean)
* before calling this method.
*
* @see QueryParameters#isReadOnlyInitialized()
* @see QueryParameters#isReadOnly(SharedSessionContractImplementor)
* @see QueryParameters#setReadOnly(boolean)
*
* The read-only/modifiable setting has no impact on entities/proxies returned by the
* query that existed in the session before the query was executed.
*
* @return true, entities and proxies loaded by the Query will be put in read-only mode
* false, entities and proxies loaded by the Query will be put in modifiable mode
* @throws IllegalStateException if the read-only/modifiable setting has not been
* initialized (i.e., isReadOnlyInitialized() == false).
*/
public boolean isReadOnly() {
if ( !isReadOnlyInitialized() ) {
throw new IllegalStateException( "cannot call isReadOnly() when isReadOnlyInitialized() returns false" );
}
return readOnly;
}
/**
* Should entities and proxies loaded by the Query be put in read-only mode? If the
* read-only/modifiable setting was not initialized (i.e., QueryParameters#isReadOnlyInitialized() == false),
* then the default read-only/modifiable setting for the persistence context is returned instead.
* <p/>
* The read-only/modifiable setting has no impact on entities/proxies returned by the
* query that existed in the session before the query was executed.
*
* @param session The originating session
*
* @return {@code true} indicates that entities and proxies loaded by the query will be put in read-only mode;
* {@code false} indicates that entities and proxies loaded by the query will be put in modifiable mode
*
* @see QueryParameters#isReadOnlyInitialized()
* @see QueryParameters#setReadOnly(boolean)
* @see org.hibernate.engine.spi.PersistenceContext#isDefaultReadOnly()
*
* The read-only/modifiable setting has no impact on entities/proxies returned by the
* query that existed in the session before the query was executed.
*
*/
public boolean isReadOnly(SharedSessionContractImplementor session) {
return isReadOnlyInitialized
? isReadOnly()
: session.getPersistenceContextInternal().isDefaultReadOnly();
}
/**
* Set the read-only/modifiable mode for entities and proxies loaded by the query.
* <p/>
* The read-only/modifiable setting has no impact on entities/proxies returned by the
* query that existed in the session before the query was executed.
*
* @param readOnly if {@code true}, entities and proxies loaded by the query will be put in read-only mode; if
* {@code false}, entities and proxies loaded by the query will be put in modifiable mode
*
* @see QueryParameters#isReadOnlyInitialized()
* @see QueryParameters#isReadOnly(SharedSessionContractImplementor)
* @see QueryParameters#setReadOnly(boolean)
* @see org.hibernate.engine.spi.PersistenceContext#isDefaultReadOnly()
*/
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
this.isReadOnlyInitialized = true;
}
public void setCallable(boolean callable) {
this.callable = callable;
}
public boolean isCallable() {
return callable;
}
public boolean hasAutoDiscoverScalarTypes() {
return autodiscovertypes;
}
/**
* Check if this query should pass the {@code distinct} to the database.
* @return the query passes {@code distinct} to the database
*/
public boolean isPassDistinctThrough() {
return passDistinctThrough;
}
/**
* Set if this query should pass the {@code distinct} to the database.
* @param passDistinctThrough the query passes {@code distinct} to the database
*/
public void setPassDistinctThrough(boolean passDistinctThrough) {
this.passDistinctThrough = passDistinctThrough;
}
private int getNumberOfParametersCoveredBy(Type[] subtypes) {
int numberOfParameters = 0;
for ( Type type : subtypes ) {
if ( type.isComponentType() ) {
numberOfParameters = numberOfParameters + getNumberOfParametersCoveredBy( ((ComponentType) type).getSubtypes() );
}
else {
numberOfParameters++;
}
}
return numberOfParameters;
}
public String getFilteredSQL() {
return processedSQL;
}
public Object[] getFilteredPositionalParameterValues() {
return processedPositionalParameterValues;
}
public Type[] getFilteredPositionalParameterTypes() {
return processedPositionalParameterTypes;
}
public boolean isNaturalKeyLookup() {
return isNaturalKeyLookup;
}
@SuppressWarnings( {"UnusedDeclaration"})
public void setNaturalKeyLookup(boolean isNaturalKeyLookup) {
this.isNaturalKeyLookup = isNaturalKeyLookup;
}
public void setAutoDiscoverScalarTypes(boolean autodiscovertypes) {
this.autodiscovertypes = autodiscovertypes;
}
public QueryParameters createCopyUsing(RowSelection selection) {
QueryParameters copy = new QueryParameters(
this.positionalParameterTypes,
this.positionalParameterValues,
this.namedParameters,
this.lockOptions,
selection,
this.isReadOnlyInitialized,
this.readOnly,
this.cacheable,
this.cacheRegion,
this.comment,
this.queryHints,
this.collectionKeys,
this.optionalObject,
this.optionalEntityName,
this.optionalId,
this.resultTransformer
);
copy.processedSQL = this.processedSQL;
copy.processedPositionalParameterTypes = this.processedPositionalParameterTypes;
copy.processedPositionalParameterValues = this.processedPositionalParameterValues;
copy.passDistinctThrough = this.passDistinctThrough;
return copy;
}
public void bindDynamicParameter(Type paramType, Object paramValue) {
if(processedPositionalParameterTypes != null) {
int length = processedPositionalParameterTypes.length;
Type[] types = new Type[length + 1];
Object[] values = new Object[length + 1];
for ( int i = 0; i < length; i++ ) {
types[i] = processedPositionalParameterTypes[i];
values[i] = processedPositionalParameterValues[i];
}
types[length] = paramType;
values[length] = paramValue;
processedPositionalParameterTypes = types;
processedPositionalParameterValues = values;
}
}
}