Skip to content

Commit 2679046

Browse files
committed
reduce copy/paste in some code in org.hibernate.query.sqm.sql.internal
and use safe casts
1 parent d318379 commit 2679046

File tree

9 files changed

+130
-148
lines changed

9 files changed

+130
-148
lines changed

hibernate-core/src/main/java/org/hibernate/dialect/OracleSqlAstTranslator.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.ArrayList;
88
import java.util.List;
99

10+
import org.hibernate.AssertionFailure;
1011
import org.hibernate.engine.spi.SessionFactoryImplementor;
1112
import org.hibernate.metamodel.mapping.CollectionPart;
1213
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
@@ -56,7 +57,6 @@
5657
import org.hibernate.sql.ast.tree.select.SelectClause;
5758
import org.hibernate.sql.ast.tree.select.SelectStatement;
5859
import org.hibernate.sql.ast.tree.select.SortSpecification;
59-
import org.hibernate.sql.ast.tree.update.Assignable;
6060
import org.hibernate.sql.ast.tree.update.Assignment;
6161
import org.hibernate.sql.ast.tree.update.UpdateStatement;
6262
import org.hibernate.sql.exec.spi.JdbcOperation;
@@ -467,7 +467,8 @@ protected void renderRowNumber(SelectClause selectClause, QueryPart queryPart) {
467467
@Override
468468
public void visitOver(Over<?> over) {
469469
final Expression expression = over.getExpression();
470-
if ( expression instanceof FunctionExpression && "row_number".equals( ( (FunctionExpression) expression ).getFunctionName() ) ) {
470+
if ( expression instanceof FunctionExpression functionExpression
471+
&& "row_number".equals( functionExpression.getFunctionName() ) ) {
471472
if ( over.getPartitions().isEmpty() && over.getOrderList().isEmpty()
472473
&& over.getStartKind() == FrameKind.UNBOUNDED_PRECEDING
473474
&& over.getEndKind() == FrameKind.CURRENT_ROW
@@ -653,9 +654,8 @@ protected void renderNull(Literal literal) {
653654

654655
@Override
655656
protected void visitSetAssignment(Assignment assignment) {
656-
final Assignable assignable = assignment.getAssignable();
657-
if ( assignable instanceof SqmPathInterpretation<?> ) {
658-
final String affectedTableName = ( (SqmPathInterpretation<?>) assignable ).getAffectedTableName();
657+
if ( assignment.getAssignable() instanceof SqmPathInterpretation<?> sqmPathInterpretation ) {
658+
final String affectedTableName = sqmPathInterpretation.getAffectedTableName();
659659
if ( affectedTableName != null ) {
660660
addAffectedTableName( affectedTableName );
661661
}
@@ -684,9 +684,9 @@ else if ( assignedValue instanceof SelectStatement ) {
684684
appendSql( ")=" );
685685
assignedValue.accept( this );
686686
}
687-
else {
688-
assert assignedValue instanceof SqlTupleContainer;
689-
final List<? extends Expression> expressions = ( (SqlTupleContainer) assignedValue ).getSqlTuple().getExpressions();
687+
else if ( assignedValue instanceof SqlTupleContainer sqlTupleContainer ) {
688+
final List<? extends Expression> expressions =
689+
sqlTupleContainer.getSqlTuple().getExpressions();
690690
columnReferences.get( 0 ).appendColumnForWrite( this, null );
691691
appendSql( '=' );
692692
expressions.get( 0 ).accept( this );
@@ -697,6 +697,9 @@ else if ( assignedValue instanceof SelectStatement ) {
697697
expressions.get( i ).accept( this );
698698
}
699699
}
700+
else {
701+
throw new AssertionFailure( "Unexpected assigned value" );
702+
}
700703
}
701704

702705
@Override

hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmUtil.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.hibernate.metamodel.mapping.ModelPart;
3535
import org.hibernate.metamodel.mapping.ModelPartContainer;
3636
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
37+
import org.hibernate.metamodel.mapping.ValuedModelPart;
3738
import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping;
3839
import org.hibernate.metamodel.model.domain.BasicDomainType;
3940
import org.hibernate.metamodel.model.domain.EntityDomainType;
@@ -86,6 +87,7 @@
8687
import org.hibernate.sql.ast.Clause;
8788
import org.hibernate.sql.ast.SqlTreeCreationException;
8889
import org.hibernate.sql.ast.tree.expression.JdbcParameter;
90+
import org.hibernate.sql.ast.tree.from.TableGroup;
8991
import org.hibernate.sql.exec.internal.JdbcParameterBindingImpl;
9092
import org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl;
9193
import org.hibernate.sql.exec.spi.JdbcParameterBindings;
@@ -161,6 +163,13 @@ public static IllegalQueryOperationException expectingNonSelect(SqmStatement<?>
161163
);
162164
}
163165

166+
public static @Nullable String determineAffectedTableName(TableGroup tableGroup, ValuedModelPart mapping) {
167+
return tableGroup.getModelPart() instanceof EntityAssociationMapping associationMapping
168+
&& !associationMapping.containsTableReference( mapping.getContainingTableExpression() )
169+
? associationMapping.getAssociatedEntityMappingType().getMappedTableDetails().getTableName()
170+
: null;
171+
}
172+
164173
/**
165174
* Utility that returns the entity association target's mapping type if the specified {@code sqmPath} should
166175
* be dereferenced using the target table, i.e. when the path's lhs is an explicit join that is used in the
@@ -178,10 +187,9 @@ public static ModelPartContainer getTargetMappingIfNeeded(
178187
modelPartContainer instanceof PluralAttributeMapping plural
179188
? getCollectionPart( plural, castNonNull( sqmPath.getNavigablePath().getParent() ) )
180189
: modelPartContainer;
181-
if ( modelPart instanceof EntityAssociationMapping association ) {
182-
if ( shouldRenderTargetSide( sqmPath, association, sqlAstCreationState ) ) {
183-
return association.getAssociatedEntityMappingType();
184-
}
190+
if ( modelPart instanceof EntityAssociationMapping association
191+
&& shouldRenderTargetSide( sqmPath, association, sqlAstCreationState ) ) {
192+
return association.getAssociatedEntityMappingType();
185193
}
186194
}
187195
return modelPartContainer;

hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -857,10 +857,9 @@ public List<Assignment> visitSetClause(SqmSetClause setClause) {
857857
final SqmExpression<?> assignmentValue = sqmAssignment.getValue();
858858
final SqmParameter<?> assignmentValueParameter = getSqmParameter( assignmentValue );
859859
final Expression pathSqlExpression = assignedPathInterpretation.getSqlExpression();
860-
//noinspection unchecked
861860
final List<ColumnReference> targetColumnReferences =
862-
pathSqlExpression instanceof SqlTupleContainer sqlTuple
863-
? (List<ColumnReference>) sqlTuple.getSqlTuple().getExpressions()
861+
pathSqlExpression instanceof SqlTupleContainer sqlTupleContainer
862+
? sqlTupleContainer.getSqlTuple().getColumnReferences()
864863
: pathSqlExpression.getColumnReference().getColumnReferences();
865864
if ( assignmentValueParameter != null ) {
866865
final ArrayList<Expression> expressions = new ArrayList<>( targetColumnReferences.size() );
@@ -7739,7 +7738,8 @@ private void handleTypeComparison(
77397738
DiscriminatorPathInterpretation<?> typeExpression,
77407739
List<EntityTypeLiteral> literalExpressions,
77417740
boolean inclusive) {
7742-
final TableGroup tableGroup = getFromClauseIndex().getTableGroup( typeExpression.getNavigablePath().getParent() );
7741+
final TableGroup tableGroup =
7742+
getFromClauseIndex().getTableGroup( typeExpression.getNavigablePath().getParent() );
77437743
final MappingType partMappingType = tableGroup.getModelPart().getPartMappingType();
77447744
if ( !(partMappingType instanceof EntityMappingType entityMappingType) ) {
77457745
return;

hibernate-core/src/main/java/org/hibernate/query/sqm/sql/internal/BasicValuedPathInterpretation.java

Lines changed: 63 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
import org.hibernate.metamodel.MappingMetamodel;
1212
import org.hibernate.metamodel.mapping.BasicValuedModelPart;
13-
import org.hibernate.metamodel.mapping.EntityAssociationMapping;
1413
import org.hibernate.metamodel.mapping.EntityMappingType;
15-
import org.hibernate.metamodel.mapping.MappingType;
1614
import org.hibernate.metamodel.mapping.ModelPart;
1715
import org.hibernate.metamodel.mapping.ModelPartContainer;
1816
import org.hibernate.metamodel.model.domain.EntityDomainType;
@@ -25,6 +23,7 @@
2523
import org.hibernate.query.sqm.tree.domain.SqmTreatedPath;
2624
import org.hibernate.spi.NavigablePath;
2725
import org.hibernate.sql.ast.SqlAstWalker;
26+
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
2827
import org.hibernate.sql.ast.tree.expression.ColumnReference;
2928
import org.hibernate.sql.ast.tree.expression.Expression;
3029
import org.hibernate.sql.ast.tree.expression.SqlSelectionExpression;
@@ -36,6 +35,7 @@
3635

3736
import static jakarta.persistence.metamodel.Type.PersistenceType.ENTITY;
3837
import static org.hibernate.internal.util.NullnessUtil.castNonNull;
38+
import static org.hibernate.query.sqm.internal.SqmUtil.determineAffectedTableName;
3939
import static org.hibernate.query.sqm.internal.SqmUtil.getTargetMappingIfNeeded;
4040

4141
/**
@@ -49,81 +49,87 @@ public static <T> BasicValuedPathInterpretation<T> from(
4949
SqmBasicValuedSimplePath<T> sqmPath,
5050
SqmToSqlAstConverter sqlAstCreationState,
5151
boolean jpaQueryComplianceEnabled) {
52+
final SqlAstCreationContext creationContext = sqlAstCreationState.getCreationContext();
53+
5254
final SqmPath<?> lhs = sqmPath.getLhs();
53-
final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().getTableGroup( lhs.getNavigablePath() );
54-
EntityMappingType treatTarget = null;
55+
final TableGroup tableGroup =
56+
sqlAstCreationState.getFromClauseAccess()
57+
.getTableGroup( lhs.getNavigablePath() );
58+
final ModelPartContainer tableGroupModelPart = tableGroup.getModelPart();
59+
60+
final MappingMetamodel mappingMetamodel = creationContext.getMappingMetamodel();
61+
final EntityMappingType treatTarget;
5562
final ModelPartContainer modelPartContainer;
56-
if ( lhs instanceof SqmTreatedPath<?, ?> && ( (SqmTreatedPath<?, ?>) lhs ).getTreatTarget().getPersistenceType() == ENTITY ) {
57-
final EntityDomainType<?> treatTargetDomainType = (EntityDomainType<?>) ( (SqmTreatedPath<?, ?>) lhs ).getTreatTarget();
58-
59-
final MappingMetamodel mappingMetamodel = sqlAstCreationState.getCreationContext().getMappingMetamodel();
60-
final EntityPersister treatEntityDescriptor = mappingMetamodel.findEntityDescriptor( treatTargetDomainType.getHibernateEntityName() );
61-
final MappingType tableGroupMappingType = tableGroup.getModelPart().getPartMappingType();
62-
if ( tableGroupMappingType instanceof EntityMappingType
63-
&& treatEntityDescriptor.isTypeOrSuperType( (EntityMappingType) tableGroupMappingType ) ) {
64-
modelPartContainer = tableGroup.getModelPart();
63+
if ( lhs instanceof SqmTreatedPath<?, ?> treatedPath
64+
&& treatedPath.getTreatTarget().getPersistenceType() == ENTITY ) {
65+
final EntityDomainType<?> treatTargetDomainType = (EntityDomainType<?>) treatedPath.getTreatTarget();
66+
final EntityPersister treatEntityDescriptor =
67+
mappingMetamodel.findEntityDescriptor( treatTargetDomainType.getHibernateEntityName() );
68+
if ( tableGroupModelPart.getPartMappingType() instanceof EntityMappingType entityMappingType
69+
&& treatEntityDescriptor.isTypeOrSuperType( entityMappingType ) ) {
70+
modelPartContainer = tableGroupModelPart;
6571
treatTarget = treatEntityDescriptor;
6672
}
6773
else {
6874
modelPartContainer = treatEntityDescriptor;
75+
treatTarget = null;
6976
}
7077
}
7178
else {
72-
modelPartContainer = tableGroup.getModelPart();
73-
if ( jpaQueryComplianceEnabled && lhs.getNodeType() instanceof EntityDomainType<?> entityDomainType ) {
74-
treatTarget = sqlAstCreationState.getCreationContext().getMappingMetamodel()
75-
.findEntityDescriptor( entityDomainType.getHibernateEntityName() );
76-
}
79+
modelPartContainer = tableGroupModelPart;
80+
treatTarget =
81+
jpaQueryComplianceEnabled && lhs.getNodeType() instanceof EntityDomainType<?> entityDomainType
82+
? mappingMetamodel.findEntityDescriptor( entityDomainType.getHibernateEntityName() )
83+
: null;
7784
}
7885

7986
// Use the target type to find the sub part if needed, otherwise just use the container
80-
final ModelPart modelPart = getTargetMappingIfNeeded(
81-
sqmPath,
82-
modelPartContainer,
83-
sqlAstCreationState
84-
).findSubPart( sqmPath.getReferencedPathSource().getPathName(), treatTarget );
85-
87+
final ModelPart modelPart =
88+
getTargetMappingIfNeeded( sqmPath, modelPartContainer, sqlAstCreationState )
89+
.findSubPart( sqmPath.getReferencedPathSource().getPathName(), treatTarget );
8690
if ( modelPart == null ) {
87-
if ( jpaQueryComplianceEnabled ) {
88-
// to get the better error, see if we got nothing because of treat handling
89-
final ModelPart subPart = tableGroup.getModelPart().findSubPart(
90-
sqmPath.getReferencedPathSource().getPathName(),
91-
null
92-
);
93-
if ( subPart != null ) {
94-
throw new StrictJpaComplianceViolation( StrictJpaComplianceViolation.Type.IMPLICIT_TREAT );
95-
}
96-
}
97-
98-
throw new UnknownPathException( "Path '" + sqmPath.getNavigablePath() + "' did not reference a known model part" );
91+
modelPartError( sqmPath, jpaQueryComplianceEnabled, tableGroup );
9992
}
10093

94+
final NavigablePath navigablePath = sqmPath.getNavigablePath();
10195
final BasicValuedModelPart mapping = castNonNull( modelPart.asBasicValuedModelPart() );
102-
final TableReference tableReference = tableGroup.resolveTableReference(
103-
sqmPath.getNavigablePath(),
104-
mapping,
105-
mapping.getContainingTableExpression()
106-
);
107-
108-
final Expression expression = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(
109-
tableReference,
110-
mapping
111-
);
112-
113-
final ColumnReference columnReference;
114-
if ( expression instanceof ColumnReference ) {
115-
columnReference = ( (ColumnReference) expression );
96+
final TableReference tableReference =
97+
tableGroup.resolveTableReference( navigablePath, mapping, mapping.getContainingTableExpression() );
98+
final Expression expression =
99+
sqlAstCreationState.getSqlExpressionResolver()
100+
.resolveSqlExpression( tableReference, mapping );
101+
return new BasicValuedPathInterpretation<>( columnReference( expression ), navigablePath, mapping, tableGroup );
102+
}
103+
104+
private static <T> void modelPartError(
105+
SqmBasicValuedSimplePath<T> sqmPath,
106+
boolean jpaQueryComplianceEnabled,
107+
TableGroup tableGroup) {
108+
if ( jpaQueryComplianceEnabled ) {
109+
// to get the better error, see if we got nothing because of treat handling
110+
final ModelPart subPart =
111+
tableGroup.getModelPart()
112+
.findSubPart( sqmPath.getReferencedPathSource().getPathName(), null );
113+
if ( subPart != null ) {
114+
throw new StrictJpaComplianceViolation( StrictJpaComplianceViolation.Type.IMPLICIT_TREAT );
115+
}
116+
}
117+
118+
throw new UnknownPathException( "Path '" + sqmPath.getNavigablePath() + "' did not reference a known model part" );
119+
}
120+
121+
private static ColumnReference columnReference(Expression expression) {
122+
if ( expression instanceof ColumnReference reference ) {
123+
return reference;
116124
}
117-
else if ( expression instanceof SqlSelectionExpression ) {
118-
final Expression selectedExpression = ( (SqlSelectionExpression) expression ).getSelection().getExpression();
125+
else if ( expression instanceof SqlSelectionExpression selection ) {
126+
final Expression selectedExpression = selection.getSelection().getExpression();
119127
assert selectedExpression instanceof ColumnReference;
120-
columnReference = (ColumnReference) selectedExpression;
128+
return (ColumnReference) selectedExpression;
121129
}
122130
else {
123131
throw new UnsupportedOperationException( "Unsupported basic-valued path expression : " + expression );
124132
}
125-
126-
return new BasicValuedPathInterpretation<>( columnReference, sqmPath.getNavigablePath(), mapping, tableGroup );
127133
}
128134

129135
private final ColumnReference columnReference;
@@ -134,18 +140,8 @@ public BasicValuedPathInterpretation(
134140
NavigablePath navigablePath,
135141
BasicValuedModelPart mapping,
136142
TableGroup tableGroup) {
137-
this( columnReference, navigablePath, mapping, tableGroup, determineAffectedTableName( tableGroup, mapping ) );
138-
}
139-
140-
private static @Nullable String determineAffectedTableName(TableGroup tableGroup, BasicValuedModelPart mapping) {
141-
final ModelPartContainer modelPart = tableGroup.getModelPart();
142-
if ( modelPart instanceof EntityAssociationMapping ) {
143-
final EntityAssociationMapping associationMapping = (EntityAssociationMapping) modelPart;
144-
if ( !associationMapping.containsTableReference( mapping.getContainingTableExpression() ) ) {
145-
return associationMapping.getAssociatedEntityMappingType().getMappedTableDetails().getTableName();
146-
}
147-
}
148-
return null;
143+
this( columnReference, navigablePath, mapping, tableGroup,
144+
determineAffectedTableName( tableGroup, mapping ) );
149145
}
150146

151147
public BasicValuedPathInterpretation(

0 commit comments

Comments
 (0)