3535import org .hibernate .query .sqm .tree .select .SqmSelectStatement ;
3636import org .hibernate .query .sqm .tree .select .SqmSelection ;
3737import org .hibernate .sql .ast .SqlAstTranslator ;
38- import org .hibernate .sql .ast .spi .FromClauseAccess ;
3938import org .hibernate .sql .ast .tree .expression .Expression ;
4039import org .hibernate .sql .ast .tree .expression .JdbcParameter ;
4140import org .hibernate .sql .ast .tree .expression .Literal ;
6160import static java .util .Collections .emptyList ;
6261import static org .hibernate .internal .util .ReflectHelper .isClass ;
6362import static org .hibernate .internal .util .collections .ArrayHelper .toStringArray ;
63+ import static org .hibernate .query .sqm .internal .AppliedGraphs .containsCollectionFetches ;
6464import static org .hibernate .query .sqm .internal .QuerySqmImpl .CRITERIA_HQL_STRING ;
65+ import static org .hibernate .query .sqm .internal .SqmUtil .generateJdbcParamsXref ;
6566import static org .hibernate .query .sqm .internal .SqmUtil .isSelectionAssignableToResultType ;
6667
6768/**
@@ -93,13 +94,10 @@ public ConcreteSqmSelectQueryPlan(
9394
9495 this .rowTransformer = determineRowTransformer ( sqm , resultType , tupleMetadata , queryOptions );
9596
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 ;
103101 this .executeQueryInterpreter = (resultsConsumer , executionContext , sqmInterpretation , jdbcParameterBindings ) -> {
104102 final SharedSessionContractImplementor session = executionContext .getSession ();
105103 final JdbcOperationQuerySelect jdbcSelect = sqmInterpretation .getJdbcSelect ();
@@ -141,8 +139,9 @@ public ConcreteSqmSelectQueryPlan(
141139 jdbcParameterBindings
142140 );
143141 session .autoFlushIfRequired ( jdbcSelect .getAffectedTableNames (), true );
144- final Expression fetchExpression = sqmInterpretation .selectStatement .getQueryPart ()
145- .getFetchClauseExpression ();
142+ final Expression fetchExpression =
143+ sqmInterpretation .selectStatement .getQueryPart ()
144+ .getFetchClauseExpression ();
146145 final int resultCountEstimate = fetchExpression != null
147146 ? interpretIntExpression ( fetchExpression , jdbcParameterBindings )
148147 : -1 ;
@@ -173,12 +172,12 @@ public ConcreteSqmSelectQueryPlan(
173172// jdbcParameterBindings
174173// );
175174
176- final JdbcSelectExecutor jdbcSelectExecutor = session .getFactory ()
177- .getJdbcServices ()
178- .getJdbcSelectExecutor ();
175+ final JdbcSelectExecutor jdbcSelectExecutor =
176+ session .getFactory ().getJdbcServices ().getJdbcSelectExecutor ();
179177 session .autoFlushIfRequired ( jdbcSelect .getAffectedTableNames (), true );
180- final Expression fetchExpression = sqmInterpretation .selectStatement .getQueryPart ()
181- .getFetchClauseExpression ();
178+ final Expression fetchExpression =
179+ sqmInterpretation .selectStatement .getQueryPart ()
180+ .getFetchClauseExpression ();
182181 final int resultCountEstimate = fetchExpression != null
183182 ? interpretIntExpression ( fetchExpression , jdbcParameterBindings )
184183 : -1 ;
@@ -217,17 +216,19 @@ protected static SqmJdbcExecutionContextAdapter listInterpreterExecutionContext(
217216 }
218217
219218 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 ();
222221 }
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 ();
225224 }
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 ;
229231 }
230- return -1 ;
231232 }
232233
233234 private static List <SqmSelection <?>> selections (SqmSelectStatement <?> sqm ) {
@@ -389,11 +390,7 @@ private <T, X> T withCacheableSqmInterpretation(DomainQueryExecutionContext exec
389390 synchronized ( this ) {
390391 localCopy = cacheableSqmInterpretation ;
391392 if ( localCopy == null ) {
392- localCopy = buildCacheableSqmInterpretation (
393- sqm ,
394- domainParameterXref ,
395- executionContext
396- );
393+ localCopy = buildCacheableSqmInterpretation ( sqm , domainParameterXref , executionContext );
397394 jdbcParameterBindings = localCopy .firstParameterBindings ;
398395 localCopy .firstParameterBindings = null ;
399396 cacheableSqmInterpretation = localCopy ;
@@ -407,11 +404,7 @@ private <T, X> T withCacheableSqmInterpretation(DomainQueryExecutionContext exec
407404 // If the translation depends on the limit or lock options, we have to rebuild the JdbcSelect
408405 // We could avoid this by putting the lock options into the cache key
409406 if ( !localCopy .jdbcSelect .isCompatibleWith ( jdbcParameterBindings , executionContext .getQueryOptions () ) ) {
410- localCopy = buildCacheableSqmInterpretation (
411- sqm ,
412- domainParameterXref ,
413- executionContext
414- );
407+ localCopy = buildCacheableSqmInterpretation ( sqm , domainParameterXref , executionContext );
415408 jdbcParameterBindings = localCopy .firstParameterBindings ;
416409 localCopy .firstParameterBindings = null ;
417410 cacheableSqmInterpretation = localCopy ;
@@ -428,11 +421,7 @@ private <T, X> T withCacheableSqmInterpretation(DomainQueryExecutionContext exec
428421 // If the translation depends on the limit or lock options, we have to rebuild the JdbcSelect
429422 // We could avoid this by putting the lock options into the cache key
430423 if ( !localCopy .jdbcSelect .isCompatibleWith ( jdbcParameterBindings , executionContext .getQueryOptions () ) ) {
431- localCopy = buildCacheableSqmInterpretation (
432- sqm ,
433- domainParameterXref ,
434- executionContext
435- );
424+ localCopy = buildCacheableSqmInterpretation ( sqm , domainParameterXref , executionContext );
436425 jdbcParameterBindings = localCopy .firstParameterBindings ;
437426 localCopy .firstParameterBindings = null ;
438427 cacheableSqmInterpretation = localCopy ;
@@ -482,14 +471,12 @@ private static CacheableSqmInterpretation buildCacheableSqmInterpretation(
482471 )
483472 .translate ();
484473
485- final FromClauseAccess tableGroupAccess = sqmInterpretation .getFromClauseAccess ();
486-
487474 final SqlAstTranslator <JdbcOperationQuerySelect > selectTranslator =
488475 sessionFactory .getJdbcServices ().getJdbcEnvironment ().getSqlAstTranslatorFactory ()
489476 .buildSelectTranslator ( sessionFactory , sqmInterpretation .getSqlAst () );
490477
491- final Map < QueryParameterImplementor <?>, Map < SqmParameter <?>, List < JdbcParametersList >>> jdbcParamsXref
492- = SqmUtil . generateJdbcParamsXref ( domainParameterXref , sqmInterpretation ::getJdbcParamsBySqmParam );
478+ final var jdbcParamsXref =
479+ generateJdbcParamsXref ( domainParameterXref , sqmInterpretation ::getJdbcParamsBySqmParam );
493480
494481 final JdbcParameterBindings jdbcParameterBindings = SqmUtil .createJdbcParameterBindings (
495482 executionContext .getQueryParameterBindings (),
@@ -576,10 +563,7 @@ public void registerLoadingEntityHolder(EntityHolder holder) {
576563
577564 @ Override
578565 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 ;
583567 }
584568 }
585569}
0 commit comments