HHH-15761 Extract creation of execution context in ConcreteSqmSelectQueryPlan

This commit is contained in:
Davide D'Alto 2022-11-25 14:58:00 +00:00 committed by Davide D'Alto
parent 5bdd79baf1
commit c9df6af30a
2 changed files with 62 additions and 50 deletions

View File

@ -113,25 +113,7 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
return session.getFactory().getJdbcServices().getJdbcSelectExecutor().list( return session.getFactory().getJdbcServices().getJdbcSelectExecutor().list(
jdbcSelect, jdbcSelect,
jdbcParameterBindings, jdbcParameterBindings,
new SqmJdbcExecutionContextAdapter( executionContext, jdbcSelect ) { listInterpreterExecutionContext( hql, executionContext, jdbcSelect, subSelectFetchKeyHandler ),
@Override
public void registerLoadingEntityEntry(EntityKey entityKey, LoadingEntityEntry entry) {
subSelectFetchKeyHandler.addKey( entityKey, entry );
}
@Override
public String getQueryIdentifier(String sql) {
if ( CRITERIA_HQL_STRING.equals( hql ) ) {
return "[CRITERIA] " + sql;
}
return hql;
}
@Override
public boolean hasQueryExecutionToBeAddedToStatistics() {
return true;
}
},
rowTransformer, rowTransformer,
uniqueSemantic uniqueSemantic
); );
@ -179,6 +161,32 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
// `#performList` and `#performScroll`. // `#performList` and `#performScroll`.
} }
protected static SqmJdbcExecutionContextAdapter listInterpreterExecutionContext(
String hql,
DomainQueryExecutionContext executionContext,
JdbcOperationQuerySelect jdbcSelect,
SubselectFetch.RegistrationHandler subSelectFetchKeyHandler) {
return new SqmJdbcExecutionContextAdapter( executionContext, jdbcSelect ) {
@Override
public void registerLoadingEntityEntry(EntityKey entityKey, LoadingEntityEntry entry) {
subSelectFetchKeyHandler.addKey( entityKey, entry );
}
@Override
public String getQueryIdentifier(String sql) {
if ( CRITERIA_HQL_STRING.equals( hql ) ) {
return "[CRITERIA] " + sql;
}
return hql;
}
@Override
public boolean hasQueryExecutionToBeAddedToStatistics() {
return true;
}
};
}
private static boolean containsCollectionFetches(QueryOptions queryOptions) { private static boolean containsCollectionFetches(QueryOptions queryOptions) {
final AppliedGraph appliedGraph = queryOptions.getAppliedGraph(); final AppliedGraph appliedGraph = queryOptions.getAppliedGraph();
return appliedGraph != null && appliedGraph.getGraph() != null && containsCollectionFetches( appliedGraph.getGraph() ); return appliedGraph != null && appliedGraph.getGraph() != null && containsCollectionFetches( appliedGraph.getGraph() );
@ -199,9 +207,9 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private RowTransformer<R> determineRowTransformer( protected static <T> RowTransformer<T> determineRowTransformer(
SqmSelectStatement<?> sqm, SqmSelectStatement<?> sqm,
Class<R> resultType, Class<T> resultType,
TupleMetadata tupleMetadata, TupleMetadata tupleMetadata,
QueryOptions queryOptions) { QueryOptions queryOptions) {
if ( queryOptions.getTupleTransformer() != null ) { if ( queryOptions.getTupleTransformer() != null ) {
@ -213,7 +221,7 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
} }
if ( resultType.isArray() ) { if ( resultType.isArray() ) {
return (RowTransformer<R>) RowTransformerArrayImpl.instance(); return (RowTransformer<T>) RowTransformerArrayImpl.instance();
} }
// NOTE : if we get here : // NOTE : if we get here :
@ -224,7 +232,7 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
if ( tupleMetadata != null ) { if ( tupleMetadata != null ) {
// resultType is Tuple.. // resultType is Tuple..
if ( queryOptions.getTupleTransformer() == null ) { if ( queryOptions.getTupleTransformer() == null ) {
return (RowTransformer<R>) new RowTransformerJpaTupleImpl( tupleMetadata ); return (RowTransformer<T>) new RowTransformerJpaTupleImpl( tupleMetadata );
} }
throw new IllegalArgumentException( throw new IllegalArgumentException(
@ -243,7 +251,7 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
} }
} }
private RowTransformer<R> makeRowTransformerTupleTransformerAdapter( private static <T> RowTransformer<T> makeRowTransformerTupleTransformerAdapter(
SqmSelectStatement<?> sqm, SqmSelectStatement<?> sqm,
QueryOptions queryOptions) { QueryOptions queryOptions) {
final List<String> aliases = new ArrayList<>(); final List<String> aliases = new ArrayList<>();
@ -261,8 +269,8 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
TupleTransformer<R> tupleTransformer = (TupleTransformer<R>) queryOptions.getTupleTransformer(); TupleTransformer<T> tupleTransformer = (TupleTransformer<T>) queryOptions.getTupleTransformer();
return new RowTransformerTupleTransformerAdapter<R>( return new RowTransformerTupleTransformerAdapter<T>(
ArrayHelper.toStringArray( aliases ), ArrayHelper.toStringArray( aliases ),
tupleTransformer tupleTransformer
); );

View File

@ -516,6 +516,33 @@ public class QuerySqmImpl<R>
final boolean needsDistinct = containsCollectionFetches final boolean needsDistinct = containsCollectionFetches
&& ( sqmStatement.usesDistinct() || hasAppliedGraph( getQueryOptions() ) || hasLimit ); && ( sqmStatement.usesDistinct() || hasAppliedGraph( getQueryOptions() ) || hasLimit );
final List<R> list = resolveSelectQueryPlan()
.performList( executionContextFordoList( containsCollectionFetches, hasLimit, needsDistinct ) );
if ( needsDistinct ) {
final int first = !hasLimit || getQueryOptions().getLimit().getFirstRow() == null
? getIntegerLiteral( sqmStatement.getOffset(), 0 )
: getQueryOptions().getLimit().getFirstRow();
final int max = !hasLimit || getQueryOptions().getLimit().getMaxRows() == null
? getMaxRows( sqmStatement, list.size() )
: getQueryOptions().getLimit().getMaxRows();
if ( first > 0 || max != -1 ) {
final int toIndex;
final int resultSize = list.size();
if ( max != -1 ) {
toIndex = first + max;
}
else {
toIndex = resultSize;
}
return list.subList( first, toIndex > resultSize ? resultSize : toIndex );
}
}
return list;
}
protected DomainQueryExecutionContext executionContextFordoList(boolean containsCollectionFetches, boolean hasLimit, boolean needsDistinct) {
final DomainQueryExecutionContext executionContextToUse; final DomainQueryExecutionContext executionContextToUse;
if ( hasLimit && containsCollectionFetches ) { if ( hasLimit && containsCollectionFetches ) {
boolean fail = getSessionFactory().getSessionFactoryOptions().isFailOnPaginationOverCollectionFetchEnabled(); boolean fail = getSessionFactory().getSessionFactoryOptions().isFailOnPaginationOverCollectionFetchEnabled();
@ -570,30 +597,7 @@ public class QuerySqmImpl<R>
executionContextToUse = this; executionContextToUse = this;
} }
} }
return executionContextToUse;
final List<R> list = resolveSelectQueryPlan().performList( executionContextToUse );
if ( needsDistinct ) {
final int first = !hasLimit || getQueryOptions().getLimit().getFirstRow() == null
? getIntegerLiteral( sqmStatement.getOffset(), 0 )
: getQueryOptions().getLimit().getFirstRow();
final int max = !hasLimit || getQueryOptions().getLimit().getMaxRows() == null
? getMaxRows( sqmStatement, list.size() )
: getQueryOptions().getLimit().getMaxRows();
if ( first > 0 || max != -1 ) {
final int toIndex;
final int resultSize = list.size();
if ( max != -1 ) {
toIndex = first + max;
}
else {
toIndex = resultSize;
}
return list.subList( first, toIndex > resultSize ? resultSize : toIndex );
}
}
return list;
} }
public static QueryOptions uniqueSemanticQueryOptions(QueryOptions originalOptions) { public static QueryOptions uniqueSemanticQueryOptions(QueryOptions originalOptions) {