35
35
import org .hibernate .query .sqm .tree .select .SqmSelectStatement ;
36
36
import org .hibernate .query .sqm .tree .select .SqmSelection ;
37
37
import org .hibernate .sql .ast .SqlAstTranslator ;
38
- import org .hibernate .sql .ast .spi .FromClauseAccess ;
39
38
import org .hibernate .sql .ast .tree .expression .Expression ;
40
39
import org .hibernate .sql .ast .tree .expression .JdbcParameter ;
41
40
import org .hibernate .sql .ast .tree .expression .Literal ;
61
60
import static java .util .Collections .emptyList ;
62
61
import static org .hibernate .internal .util .ReflectHelper .isClass ;
63
62
import static org .hibernate .internal .util .collections .ArrayHelper .toStringArray ;
63
+ import static org .hibernate .query .sqm .internal .AppliedGraphs .containsCollectionFetches ;
64
64
import static org .hibernate .query .sqm .internal .QuerySqmImpl .CRITERIA_HQL_STRING ;
65
+ import static org .hibernate .query .sqm .internal .SqmUtil .generateJdbcParamsXref ;
65
66
import static org .hibernate .query .sqm .internal .SqmUtil .isSelectionAssignableToResultType ;
66
67
67
68
/**
@@ -93,13 +94,10 @@ public ConcreteSqmSelectQueryPlan(
93
94
94
95
this .rowTransformer = determineRowTransformer ( sqm , resultType , tupleMetadata , queryOptions );
95
96
96
- final ListResultsConsumer .UniqueSemantic uniqueSemantic ;
97
- if ( sqm .producesUniqueResults () && !AppliedGraphs .containsCollectionFetches ( queryOptions ) ) {
98
- uniqueSemantic = ListResultsConsumer .UniqueSemantic .NONE ;
99
- }
100
- else {
101
- uniqueSemantic = ListResultsConsumer .UniqueSemantic .ALLOW ;
102
- }
97
+ final ListResultsConsumer .UniqueSemantic uniqueSemantic =
98
+ sqm .producesUniqueResults () && !containsCollectionFetches ( queryOptions )
99
+ ? ListResultsConsumer .UniqueSemantic .NONE
100
+ : ListResultsConsumer .UniqueSemantic .ALLOW ;
103
101
this .executeQueryInterpreter = (resultsConsumer , executionContext , sqmInterpretation , jdbcParameterBindings ) -> {
104
102
final SharedSessionContractImplementor session = executionContext .getSession ();
105
103
final JdbcOperationQuerySelect jdbcSelect = sqmInterpretation .getJdbcSelect ();
@@ -141,8 +139,9 @@ public ConcreteSqmSelectQueryPlan(
141
139
jdbcParameterBindings
142
140
);
143
141
session .autoFlushIfRequired ( jdbcSelect .getAffectedTableNames (), true );
144
- final Expression fetchExpression = sqmInterpretation .selectStatement .getQueryPart ()
145
- .getFetchClauseExpression ();
142
+ final Expression fetchExpression =
143
+ sqmInterpretation .selectStatement .getQueryPart ()
144
+ .getFetchClauseExpression ();
146
145
final int resultCountEstimate = fetchExpression != null
147
146
? interpretIntExpression ( fetchExpression , jdbcParameterBindings )
148
147
: -1 ;
@@ -173,12 +172,12 @@ public ConcreteSqmSelectQueryPlan(
173
172
// jdbcParameterBindings
174
173
// );
175
174
176
- final JdbcSelectExecutor jdbcSelectExecutor = session .getFactory ()
177
- .getJdbcServices ()
178
- .getJdbcSelectExecutor ();
175
+ final JdbcSelectExecutor jdbcSelectExecutor =
176
+ session .getFactory ().getJdbcServices ().getJdbcSelectExecutor ();
179
177
session .autoFlushIfRequired ( jdbcSelect .getAffectedTableNames (), true );
180
- final Expression fetchExpression = sqmInterpretation .selectStatement .getQueryPart ()
181
- .getFetchClauseExpression ();
178
+ final Expression fetchExpression =
179
+ sqmInterpretation .selectStatement .getQueryPart ()
180
+ .getFetchClauseExpression ();
182
181
final int resultCountEstimate = fetchExpression != null
183
182
? interpretIntExpression ( fetchExpression , jdbcParameterBindings )
184
183
: -1 ;
@@ -217,17 +216,19 @@ protected static SqmJdbcExecutionContextAdapter listInterpreterExecutionContext(
217
216
}
218
217
219
218
protected static int interpretIntExpression (Expression expression , JdbcParameterBindings jdbcParameterBindings ) {
220
- if ( expression instanceof Literal ) {
221
- return ( (Number ) ( ( Literal ) expression ) .getLiteralValue () ).intValue ();
219
+ if ( expression instanceof Literal literal ) {
220
+ return ( (Number ) literal .getLiteralValue () ).intValue ();
222
221
}
223
- else if ( expression instanceof JdbcParameter ) {
224
- return (int ) jdbcParameterBindings .getBinding ( ( JdbcParameter ) expression ).getBindValue ();
222
+ else if ( expression instanceof JdbcParameter jdbcParameter ) {
223
+ return (int ) jdbcParameterBindings .getBinding ( jdbcParameter ).getBindValue ();
225
224
}
226
- else if ( expression instanceof SqmParameterInterpretation ) {
227
- return (int ) jdbcParameterBindings .getBinding ( (JdbcParameter ) ( (SqmParameterInterpretation ) expression ).getResolvedExpression () )
228
- .getBindValue ();
225
+ else if ( expression instanceof SqmParameterInterpretation parameterInterpretation ) {
226
+ final JdbcParameter jdbcParameter = (JdbcParameter ) parameterInterpretation .getResolvedExpression ();
227
+ return (int ) jdbcParameterBindings .getBinding ( jdbcParameter ).getBindValue ();
228
+ }
229
+ else {
230
+ return -1 ;
229
231
}
230
- return -1 ;
231
232
}
232
233
233
234
private static List <SqmSelection <?>> selections (SqmSelectStatement <?> sqm ) {
@@ -389,11 +390,7 @@ private <T, X> T withCacheableSqmInterpretation(DomainQueryExecutionContext exec
389
390
synchronized ( this ) {
390
391
localCopy = cacheableSqmInterpretation ;
391
392
if ( localCopy == null ) {
392
- localCopy = buildCacheableSqmInterpretation (
393
- sqm ,
394
- domainParameterXref ,
395
- executionContext
396
- );
393
+ localCopy = buildCacheableSqmInterpretation ( sqm , domainParameterXref , executionContext );
397
394
jdbcParameterBindings = localCopy .firstParameterBindings ;
398
395
localCopy .firstParameterBindings = null ;
399
396
cacheableSqmInterpretation = localCopy ;
@@ -407,11 +404,7 @@ private <T, X> T withCacheableSqmInterpretation(DomainQueryExecutionContext exec
407
404
// If the translation depends on the limit or lock options, we have to rebuild the JdbcSelect
408
405
// We could avoid this by putting the lock options into the cache key
409
406
if ( !localCopy .jdbcSelect .isCompatibleWith ( jdbcParameterBindings , executionContext .getQueryOptions () ) ) {
410
- localCopy = buildCacheableSqmInterpretation (
411
- sqm ,
412
- domainParameterXref ,
413
- executionContext
414
- );
407
+ localCopy = buildCacheableSqmInterpretation ( sqm , domainParameterXref , executionContext );
415
408
jdbcParameterBindings = localCopy .firstParameterBindings ;
416
409
localCopy .firstParameterBindings = null ;
417
410
cacheableSqmInterpretation = localCopy ;
@@ -428,11 +421,7 @@ private <T, X> T withCacheableSqmInterpretation(DomainQueryExecutionContext exec
428
421
// If the translation depends on the limit or lock options, we have to rebuild the JdbcSelect
429
422
// We could avoid this by putting the lock options into the cache key
430
423
if ( !localCopy .jdbcSelect .isCompatibleWith ( jdbcParameterBindings , executionContext .getQueryOptions () ) ) {
431
- localCopy = buildCacheableSqmInterpretation (
432
- sqm ,
433
- domainParameterXref ,
434
- executionContext
435
- );
424
+ localCopy = buildCacheableSqmInterpretation ( sqm , domainParameterXref , executionContext );
436
425
jdbcParameterBindings = localCopy .firstParameterBindings ;
437
426
localCopy .firstParameterBindings = null ;
438
427
cacheableSqmInterpretation = localCopy ;
@@ -482,14 +471,12 @@ private static CacheableSqmInterpretation buildCacheableSqmInterpretation(
482
471
)
483
472
.translate ();
484
473
485
- final FromClauseAccess tableGroupAccess = sqmInterpretation .getFromClauseAccess ();
486
-
487
474
final SqlAstTranslator <JdbcOperationQuerySelect > selectTranslator =
488
475
sessionFactory .getJdbcServices ().getJdbcEnvironment ().getSqlAstTranslatorFactory ()
489
476
.buildSelectTranslator ( sessionFactory , sqmInterpretation .getSqlAst () );
490
477
491
- final Map < QueryParameterImplementor <?>, Map < SqmParameter <?>, List < JdbcParametersList >>> jdbcParamsXref
492
- = SqmUtil . generateJdbcParamsXref ( domainParameterXref , sqmInterpretation ::getJdbcParamsBySqmParam );
478
+ final var jdbcParamsXref =
479
+ generateJdbcParamsXref ( domainParameterXref , sqmInterpretation ::getJdbcParamsBySqmParam );
493
480
494
481
final JdbcParameterBindings jdbcParameterBindings = SqmUtil .createJdbcParameterBindings (
495
482
executionContext .getQueryParameterBindings (),
@@ -576,10 +563,7 @@ public void registerLoadingEntityHolder(EntityHolder holder) {
576
563
577
564
@ Override
578
565
public String getQueryIdentifier (String sql ) {
579
- if ( CRITERIA_HQL_STRING .equals ( hql ) ) {
580
- return "[CRITERIA] " + sql ;
581
- }
582
- return hql ;
566
+ return CRITERIA_HQL_STRING .equals ( hql ) ? "[CRITERIA] " + sql : hql ;
583
567
}
584
568
}
585
569
}
0 commit comments