Skip to content

Commit 92168f9

Browse files
committed
minor refactorings to AbstractSqlAstTranslator and SelectStatement
1 parent f0792cc commit 92168f9

File tree

2 files changed

+68
-54
lines changed

2 files changed

+68
-54
lines changed

hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3657,31 +3657,12 @@ public void visitQuerySpec(QuerySpec querySpec) {
36573657
this.queryPartForRowNumbering = null;
36583658
this.queryPartForRowNumberingClauseDepth = -1;
36593659
}
3660-
String queryGroupAlias = null;
3661-
if ( currentQueryPart instanceof QueryGroup ) {
3662-
// We always need query wrapping if we are in a query group and this query spec has a fetch or order by
3663-
// clause, because of order by precedence in SQL
3664-
if ( querySpec.hasOffsetOrFetchClause() || querySpec.hasSortSpecifications() ) {
3665-
queryGroupAlias = "";
3666-
// If the parent is a query group with a fetch clause we must use a select wrapper,
3667-
// or if the database does not support simple query grouping, we must use a select wrapper
3668-
if ( ( !dialect.supportsSimpleQueryGrouping() || currentQueryPart.hasOffsetOrFetchClause() )
3669-
// We can skip it though if this query spec is being row numbered,
3670-
// because then we already have a wrapper
3671-
&& queryPartForRowNumbering != querySpec ) {
3672-
queryGroupAlias = " grp_" + queryGroupAliasCounter + '_';
3673-
queryGroupAliasCounter++;
3674-
appendSql( "select" );
3675-
appendSql( queryGroupAlias );
3676-
appendSql( ".* from " );
3677-
// We need to assign aliases when we render a query spec as subquery to avoid clashing aliases
3678-
this.needsSelectAliases = this.needsSelectAliases || hasDuplicateSelectItems( querySpec );
3679-
}
3680-
else if ( !dialect.supportsDuplicateSelectItemsInQueryGroup() ) {
3681-
this.needsSelectAliases = this.needsSelectAliases || hasDuplicateSelectItems( querySpec );
3682-
}
3683-
}
3684-
}
3660+
final String queryGroupAlias =
3661+
wrapQueryPartsIfNecessary(
3662+
querySpec,
3663+
currentQueryPart,
3664+
queryPartForRowNumbering
3665+
);
36853666
queryPartStack.push( querySpec );
36863667
if ( queryGroupAlias != null ) {
36873668
appendSql( OPEN_PARENTHESIS );
@@ -3710,6 +3691,40 @@ else if ( !dialect.supportsDuplicateSelectItemsInQueryGroup() ) {
37103691
}
37113692
}
37123693

3694+
private String wrapQueryPartsIfNecessary(
3695+
QuerySpec querySpec, QueryPart currentQueryPart, QueryPart queryPartForRowNumbering) {
3696+
// We always need query wrapping if we are in a query group and if this query
3697+
// spec has a fetch or order by clause, because of order by precedence in SQL
3698+
if ( currentQueryPart instanceof QueryGroup
3699+
&& ( querySpec.hasOffsetOrFetchClause() || querySpec.hasSortSpecifications() ) ) {
3700+
// If the parent is a query group with a fetch clause, we must use a select wrapper
3701+
// Or, if the database does not support simple query grouping, we must use a select wrapper
3702+
if ( ( !dialect.supportsSimpleQueryGrouping() || currentQueryPart.hasOffsetOrFetchClause() )
3703+
// We can skip it though if this query spec is being row numbered,
3704+
// because then we already have a wrapper
3705+
&& queryPartForRowNumbering != querySpec ) {
3706+
final String queryGroupAlias = " grp_" + queryGroupAliasCounter + '_';
3707+
queryGroupAliasCounter++;
3708+
appendSql( "select" );
3709+
appendSql( queryGroupAlias );
3710+
appendSql( ".* from " );
3711+
// We need to assign aliases when we render a query spec as subquery to avoid clashing aliases
3712+
this.needsSelectAliases = this.needsSelectAliases || hasDuplicateSelectItems( querySpec );
3713+
return queryGroupAlias;
3714+
}
3715+
else if ( !dialect.supportsDuplicateSelectItemsInQueryGroup() ) {
3716+
this.needsSelectAliases = this.needsSelectAliases || hasDuplicateSelectItems( querySpec );
3717+
return "";
3718+
}
3719+
else {
3720+
return "";
3721+
}
3722+
}
3723+
else {
3724+
return null;
3725+
}
3726+
}
3727+
37133728
protected void visitQueryClauses(QuerySpec querySpec) {
37143729
visitSelectClause( querySpec.getSelectClause() );
37153730
visitFromClause( querySpec.getFromClause() );

hibernate-core/src/main/java/org/hibernate/sql/ast/tree/select/SelectStatement.java

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,21 @@ public void accept(SqlAstWalker walker) {
6464
}
6565

6666
@Override
67-
public DomainResult createDomainResult(String resultVariable, DomainResultCreationState creationState) {
68-
final SelectClause selectClause = queryPart.getFirstQuerySpec().getSelectClause();
69-
final TypeConfiguration typeConfiguration = creationState.getSqlAstCreationState()
70-
.getCreationContext()
71-
.getMappingMetamodel()
72-
.getTypeConfiguration();
73-
final SqlExpressionResolver sqlExpressionResolver = creationState.getSqlAstCreationState().getSqlExpressionResolver();
74-
if ( selectClause.getSqlSelections().size() == 1 ) {
75-
final SqlSelection first = selectClause.getSqlSelections().get( 0 );
67+
public DomainResult<?> createDomainResult(String resultVariable, DomainResultCreationState creationState) {
68+
final List<SqlSelection> sqlSelections =
69+
queryPart.getFirstQuerySpec().getSelectClause().getSqlSelections();
70+
if ( sqlSelections.size() == 1 ) {
71+
final SqlSelection first = sqlSelections.get( 0 );
7672
final JdbcMapping jdbcMapping = first.getExpressionType().getSingleJdbcMapping();
77-
78-
final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(
79-
this,
80-
jdbcMapping.getJdbcJavaType(),
81-
null,
82-
typeConfiguration
83-
);
84-
73+
final SqlSelection sqlSelection =
74+
creationState.getSqlAstCreationState().getSqlExpressionResolver()
75+
.resolveSqlSelection(
76+
this,
77+
jdbcMapping.getJdbcJavaType(),
78+
null,
79+
creationState.getSqlAstCreationState().getCreationContext()
80+
.getTypeConfiguration()
81+
);
8582
return new BasicResult<>(
8683
sqlSelection.getValuesArrayPosition(),
8784
resultVariable,
@@ -95,20 +92,22 @@ public DomainResult createDomainResult(String resultVariable, DomainResultCreati
9592

9693
@Override
9794
public void applySqlSelections(DomainResultCreationState creationState) {
98-
final SelectClause selectClause = queryPart.getFirstQuerySpec().getSelectClause();
99-
final TypeConfiguration typeConfiguration = creationState.getSqlAstCreationState()
100-
.getCreationContext()
101-
.getMappingMetamodel()
102-
.getTypeConfiguration();
103-
for ( SqlSelection sqlSelection : selectClause.getSqlSelections() ) {
95+
final TypeConfiguration typeConfiguration =
96+
creationState.getSqlAstCreationState().getCreationContext()
97+
.getTypeConfiguration();
98+
final SqlExpressionResolver expressionResolver =
99+
creationState.getSqlAstCreationState().getSqlExpressionResolver();
100+
for ( SqlSelection sqlSelection :
101+
queryPart.getFirstQuerySpec().getSelectClause().getSqlSelections() ) {
104102
sqlSelection.getExpressionType().forEachJdbcType(
105103
(index, jdbcMapping) -> {
106-
creationState.getSqlAstCreationState().getSqlExpressionResolver().resolveSqlSelection(
107-
this,
108-
jdbcMapping.getJdbcJavaType(),
109-
null,
110-
typeConfiguration
111-
);
104+
expressionResolver
105+
.resolveSqlSelection(
106+
this,
107+
jdbcMapping.getJdbcJavaType(),
108+
null,
109+
typeConfiguration
110+
);
112111
}
113112
);
114113
}

0 commit comments

Comments
 (0)