Split TableReference class into interface and NamedTableReference implementation to allow QueryPartTableReference and ValuesTableReference to fit into the picture

This commit is contained in:
Christian Beikov 2021-12-14 16:20:20 +01:00 committed by Steve Ebersole
parent 70114d30ec
commit b75277b421
66 changed files with 789 additions and 560 deletions

View File

@ -18,8 +18,8 @@ import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.tree.Statement; import org.hibernate.sql.ast.tree.Statement;
import org.hibernate.sql.ast.tree.expression.Expression; import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.Literal; import org.hibernate.sql.ast.tree.expression.Literal;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.StandardTableGroup; import org.hibernate.sql.ast.tree.from.StandardTableGroup;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.select.QuerySpec; import org.hibernate.sql.ast.tree.select.QuerySpec;
/** /**
@ -54,7 +54,7 @@ public class InformixSqmToSqlAstConverter<T extends Statement> extends BaseSqmTo
null, null,
null, null,
null, null,
new TableReference( new NamedTableReference(
"(select 1)", "(select 1)",
"dummy_(x)", "dummy_(x)",
false, false,

View File

@ -18,8 +18,8 @@ import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.tree.Statement; import org.hibernate.sql.ast.tree.Statement;
import org.hibernate.sql.ast.tree.expression.Expression; import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.Literal; import org.hibernate.sql.ast.tree.expression.Literal;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.StandardTableGroup; import org.hibernate.sql.ast.tree.from.StandardTableGroup;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.select.QuerySpec; import org.hibernate.sql.ast.tree.select.QuerySpec;
/** /**
@ -54,7 +54,7 @@ public class IngresSqmToSqlAstConverter<T extends Statement> extends BaseSqmToSq
null, null,
null, null,
null, null,
new TableReference( new NamedTableReference(
"(select 1)", "(select 1)",
"dummy_(x)", "dummy_(x)",
false, false,

View File

@ -23,7 +23,7 @@ import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.Literal; import org.hibernate.sql.ast.tree.expression.Literal;
import org.hibernate.sql.ast.tree.expression.SqlTuple; import org.hibernate.sql.ast.tree.expression.SqlTuple;
import org.hibernate.sql.ast.tree.expression.Summarization; import org.hibernate.sql.ast.tree.expression.Summarization;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.select.QueryPart; import org.hibernate.sql.ast.tree.select.QueryPart;
import org.hibernate.sql.ast.tree.select.QuerySpec; import org.hibernate.sql.ast.tree.select.QuerySpec;
import org.hibernate.sql.ast.tree.select.SelectClause; import org.hibernate.sql.ast.tree.select.SelectClause;
@ -93,8 +93,8 @@ public class SybaseAnywhereSqlAstTranslator<T extends JdbcOperation> extends Abs
} }
@Override @Override
protected boolean renderTableReference(TableReference tableReference, LockMode lockMode) { protected boolean renderNamedTableReference(NamedTableReference tableReference, LockMode lockMode) {
super.renderTableReference( tableReference, lockMode ); super.renderNamedTableReference( tableReference, lockMode );
if ( getDialect().getVersion().isBefore( 10 ) ) { if ( getDialect().getVersion().isBefore( 10 ) ) {
if ( LockMode.READ.lessThan( lockMode ) ) { if ( LockMode.READ.lessThan( lockMode ) ) {
appendSql( " holdlock" ); appendSql( " holdlock" );

View File

@ -247,10 +247,10 @@ public class OracleSqlAstTranslator<T extends JdbcOperation> extends AbstractSql
} }
@Override @Override
protected void renderValuesTableReference(ValuesTableReference tableReference) { public void visitValuesTableReference(ValuesTableReference tableReference) {
final List<Values> valuesList = tableReference.getValuesList(); final List<Values> valuesList = tableReference.getValuesList();
if ( valuesList.size() < 2 ) { if ( valuesList.size() < 2 ) {
super.renderValuesTableReference( tableReference ); super.visitValuesTableReference( tableReference );
} }
else { else {
append( '(' ); append( '(' );

View File

@ -22,7 +22,7 @@ import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.Literal; import org.hibernate.sql.ast.tree.expression.Literal;
import org.hibernate.sql.ast.tree.expression.SqlTuple; import org.hibernate.sql.ast.tree.expression.SqlTuple;
import org.hibernate.sql.ast.tree.expression.Summarization; import org.hibernate.sql.ast.tree.expression.Summarization;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.UnionTableReference; import org.hibernate.sql.ast.tree.from.UnionTableReference;
import org.hibernate.sql.ast.tree.select.QueryGroup; import org.hibernate.sql.ast.tree.select.QueryGroup;
import org.hibernate.sql.ast.tree.select.QueryPart; import org.hibernate.sql.ast.tree.select.QueryPart;
@ -45,7 +45,7 @@ public class SQLServerSqlAstTranslator<T extends JdbcOperation> extends Abstract
} }
@Override @Override
protected boolean renderTableReference(TableReference tableReference, LockMode lockMode) { protected boolean renderNamedTableReference(NamedTableReference tableReference, LockMode lockMode) {
final String tableExpression = tableReference.getTableExpression(); final String tableExpression = tableReference.getTableExpression();
if ( tableReference instanceof UnionTableReference && lockMode != LockMode.NONE && tableExpression.charAt( 0 ) == '(' ) { if ( tableReference instanceof UnionTableReference && lockMode != LockMode.NONE && tableExpression.charAt( 0 ) == '(' ) {
// SQL Server requires to push down the lock hint to the actual table names // SQL Server requires to push down the lock hint to the actual table names
@ -72,7 +72,7 @@ public class SQLServerSqlAstTranslator<T extends JdbcOperation> extends Abstract
} }
} }
else { else {
super.renderTableReference( tableReference, lockMode ); super.renderNamedTableReference( tableReference, lockMode );
renderLockHint( lockMode ); renderLockHint( lockMode );
} }
// Just always return true because SQL Server doesn't support the FOR UPDATE clause // Just always return true because SQL Server doesn't support the FOR UPDATE clause

View File

@ -25,8 +25,8 @@ import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.Literal; import org.hibernate.sql.ast.tree.expression.Literal;
import org.hibernate.sql.ast.tree.expression.SqlTuple; import org.hibernate.sql.ast.tree.expression.SqlTuple;
import org.hibernate.sql.ast.tree.expression.Summarization; import org.hibernate.sql.ast.tree.expression.Summarization;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.UnionTableReference; import org.hibernate.sql.ast.tree.from.UnionTableReference;
import org.hibernate.sql.ast.tree.select.QueryGroup; import org.hibernate.sql.ast.tree.select.QueryGroup;
import org.hibernate.sql.ast.tree.select.QueryPart; import org.hibernate.sql.ast.tree.select.QueryPart;
@ -98,8 +98,8 @@ public class SybaseASESqlAstTranslator<T extends JdbcOperation> extends Abstract
} }
@Override @Override
protected boolean renderTableReference(TableReference tableReference, LockMode lockMode) { protected boolean renderNamedTableReference(NamedTableReference tableReference, LockMode lockMode) {
super.renderTableReference( tableReference, lockMode ); super.renderNamedTableReference( tableReference, lockMode );
if ( getDialect().getVersion().isBefore( 15, 7 ) ) { if ( getDialect().getVersion().isBefore( 15, 7 ) ) {
if ( LockMode.READ.lessThan( lockMode ) ) { if ( LockMode.READ.lessThan( lockMode ) ) {
appendSql( " holdlock" ); appendSql( " holdlock" );

View File

@ -23,7 +23,7 @@ import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.Literal; import org.hibernate.sql.ast.tree.expression.Literal;
import org.hibernate.sql.ast.tree.expression.SqlTuple; import org.hibernate.sql.ast.tree.expression.SqlTuple;
import org.hibernate.sql.ast.tree.expression.Summarization; import org.hibernate.sql.ast.tree.expression.Summarization;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.select.QueryPart; import org.hibernate.sql.ast.tree.select.QueryPart;
import org.hibernate.sql.ast.tree.select.QuerySpec; import org.hibernate.sql.ast.tree.select.QuerySpec;
import org.hibernate.sql.exec.spi.JdbcOperation; import org.hibernate.sql.exec.spi.JdbcOperation;
@ -92,8 +92,8 @@ public class SybaseSqlAstTranslator<T extends JdbcOperation> extends AbstractSql
} }
@Override @Override
protected boolean renderTableReference(TableReference tableReference, LockMode lockMode) { protected boolean renderNamedTableReference(NamedTableReference tableReference, LockMode lockMode) {
super.renderTableReference( tableReference, lockMode ); super.renderNamedTableReference( tableReference, lockMode );
if ( LockMode.READ.lessThan( lockMode ) ) { if ( LockMode.READ.lessThan( lockMode ) ) {
appendSql( " holdlock" ); appendSql( " holdlock" );
} }

View File

@ -18,8 +18,8 @@ import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.tree.Statement; import org.hibernate.sql.ast.tree.Statement;
import org.hibernate.sql.ast.tree.expression.Expression; import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.Literal; import org.hibernate.sql.ast.tree.expression.Literal;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.StandardTableGroup; import org.hibernate.sql.ast.tree.from.StandardTableGroup;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.select.QuerySpec; import org.hibernate.sql.ast.tree.select.QuerySpec;
/** /**
@ -54,7 +54,7 @@ public class SybaseSqmToSqlAstConverter<T extends Statement> extends BaseSqmToSq
null, null,
null, null,
null, null,
new TableReference( new NamedTableReference(
"(select 1)", "(select 1)",
"dummy_(x)", "dummy_(x)",
false, false,

View File

@ -226,7 +226,7 @@ public abstract class AbstractCompositeIdentifierMapping
final TableReference defaultTableReference = tableGroup.resolveTableReference( navigablePath, getContainingTableExpression() ); final TableReference defaultTableReference = tableGroup.resolveTableReference( navigablePath, getContainingTableExpression() );
getEmbeddableTypeDescriptor().forEachSelectable( getEmbeddableTypeDescriptor().forEachSelectable(
(columnIndex, selection) -> { (columnIndex, selection) -> {
final TableReference tableReference = selection.getContainingTableExpression().equals( defaultTableReference.getTableExpression() ) final TableReference tableReference = defaultTableReference.resolveTableReference( selection.getContainingTableExpression() ) != null
? defaultTableReference ? defaultTableReference
: tableGroup.resolveTableReference( navigablePath, selection.getContainingTableExpression() ); : tableGroup.resolveTableReference( navigablePath, selection.getContainingTableExpression() );
final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver() final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver()

View File

@ -279,7 +279,7 @@ public class EmbeddedAttributeMapping
final TableReference defaultTableReference = tableGroup.resolveTableReference( navigablePath, getContainingTableExpression() ); final TableReference defaultTableReference = tableGroup.resolveTableReference( navigablePath, getContainingTableExpression() );
getEmbeddableTypeDescriptor().forEachSelectable( getEmbeddableTypeDescriptor().forEachSelectable(
(columnIndex, selection) -> { (columnIndex, selection) -> {
final TableReference tableReference = selection.getContainingTableExpression().equals( defaultTableReference.getTableExpression() ) final TableReference tableReference = defaultTableReference.resolveTableReference( selection.getContainingTableExpression() ) != null
? defaultTableReference ? defaultTableReference
: tableGroup.resolveTableReference( navigablePath, selection.getContainingTableExpression() ); : tableGroup.resolveTableReference( navigablePath, selection.getContainingTableExpression() );
final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression( final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(

View File

@ -406,14 +406,16 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor {
} }
protected TableReference getTableReference(TableGroup lhs, TableGroup tableGroup, String table) { protected TableReference getTableReference(TableGroup lhs, TableGroup tableGroup, String table) {
if ( lhs.getPrimaryTableReference().getTableExpression().equals( table ) ) { TableReference tableReference = lhs.getPrimaryTableReference().resolveTableReference( table );
return lhs.getPrimaryTableReference(); if ( tableReference != null ) {
return tableReference;
} }
else if ( tableGroup.getPrimaryTableReference().getTableExpression().equals( table ) ) { tableReference = tableGroup.getPrimaryTableReference().resolveTableReference( table );
return tableGroup.getPrimaryTableReference(); if ( tableReference != null ) {
return tableReference;
} }
final TableReference tableReference = lhs.resolveTableReference( tableReference = lhs.resolveTableReference(
lhs.getNavigablePath().append( getNavigableRole().getNavigableName() ), lhs.getNavigablePath().append( getNavigableRole().getNavigableName() ),
table table
); );

View File

@ -20,7 +20,6 @@ import org.hibernate.metamodel.internal.AbstractCompositeIdentifierMapping;
import org.hibernate.metamodel.mapping.AttributeMapping; import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.EmbeddableMappingType; import org.hibernate.metamodel.mapping.EmbeddableMappingType;
import org.hibernate.metamodel.mapping.EntityIdentifierMapping; import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.ModelPart; import org.hibernate.metamodel.mapping.ModelPart;
import org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping; import org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping;
@ -175,13 +174,9 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif
offset += attributeMapping.forEachSelectable( offset += attributeMapping.forEachSelectable(
offset, offset,
(columnIndex, selection) -> { (columnIndex, selection) -> {
final TableReference tableReference = selection.getContainingTableExpression().equals( final TableReference tableReference = defaultTableReference.resolveTableReference( selection.getContainingTableExpression() ) != null
defaultTableReference.getTableExpression() )
? defaultTableReference ? defaultTableReference
: tableGroup.resolveTableReference( : tableGroup.resolveTableReference( navigablePath, selection.getContainingTableExpression() );
navigablePath,
selection.getContainingTableExpression()
);
final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver() final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver()
.resolveSqlExpression( .resolveSqlExpression(
SqlExpressionResolver.createColumnReferenceKey( SqlExpressionResolver.createColumnReferenceKey(

View File

@ -51,6 +51,7 @@ import org.hibernate.sql.ast.spi.SqlAstCreationState;
import org.hibernate.sql.ast.spi.SqlExpressionResolver; import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.spi.SqlSelection; import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.from.CollectionTableGroup; import org.hibernate.sql.ast.tree.from.CollectionTableGroup;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin; import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.from.TableGroupJoinProducer; import org.hibernate.sql.ast.tree.from.TableGroupJoinProducer;
@ -663,7 +664,7 @@ public class PluralAttributeMappingImpl
assert !getCollectionDescriptor().isOneToMany(); assert !getCollectionDescriptor().isOneToMany();
final String collectionTableName = ( (Joinable) collectionDescriptor ).getTableName(); final String collectionTableName = ( (Joinable) collectionDescriptor ).getTableName();
final TableReference collectionTableReference = new TableReference( final TableReference collectionTableReference = new NamedTableReference(
collectionTableName, collectionTableName,
sqlAliasBase.generateNewAlias(), sqlAliasBase.generateNewAlias(),
true, true,

View File

@ -1870,7 +1870,7 @@ public abstract class AbstractCollectionPersister
alias = tableReference.getIdentificationVariable(); alias = tableReference.getIdentificationVariable();
} }
else { else {
alias = tableReference.getTableExpression(); alias = tableReference.getTableId();
} }
applyWhereFragments( predicateConsumer, alias, tableGroup, creationState ); applyWhereFragments( predicateConsumer, alias, tableGroup, creationState );
@ -1937,7 +1937,7 @@ public abstract class AbstractCollectionPersister
alias = tableReference.getIdentificationVariable(); alias = tableReference.getIdentificationVariable();
} }
else { else {
alias = tableReference.getTableExpression(); alias = tableReference.getTableId();
} }
applyWhereFragments( predicateConsumer, alias, manyToManyWhereTemplate ); applyWhereFragments( predicateConsumer, alias, manyToManyWhereTemplate );
@ -1984,11 +1984,6 @@ public abstract class AbstractCollectionPersister
return elementPropertyMapping.toColumns( propertyName ); return elementPropertyMapping.toColumns( propertyName );
} }
// @Override
// public Type getType() {
// return elementPropertyMapping.getType(); // ==elementType ??
// }
@Override @Override
public String getName() { public String getName() {
return getRole(); return getRole();
@ -2079,7 +2074,6 @@ public abstract class AbstractCollectionPersister
return factory; return factory;
} }
protected boolean isInsertCallable() { protected boolean isInsertCallable() {
return insertCallable; return insertCallable;
} }

View File

@ -230,6 +230,7 @@ import org.hibernate.sql.ast.tree.expression.AliasedExpression;
import org.hibernate.sql.ast.tree.expression.ColumnReference; import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.Expression; import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.JdbcParameter; import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.StandardTableGroup; import org.hibernate.sql.ast.tree.from.StandardTableGroup;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
@ -1347,7 +1348,7 @@ public abstract class AbstractEntityPersister
for ( int i = 0; i < subclassTableNames.length; i++ ) { for ( int i = 0; i < subclassTableNames.length; i++ ) {
if ( tableExpression.equals( subclassTableNames[ i ] ) ) { if ( tableExpression.equals( subclassTableNames[ i ] ) ) {
final boolean isNullableTable = isNullableSubclassTable( i ); final boolean isNullableTable = isNullableSubclassTable( i );
final TableReference joinedTableReference = new TableReference( final NamedTableReference joinedTableReference = new NamedTableReference(
tableExpression, tableExpression,
sqlAliasBase.generateNewAlias(), sqlAliasBase.generateNewAlias(),
isNullableTable, isNullableTable,
@ -1420,7 +1421,7 @@ public abstract class AbstractEntityPersister
SqlAstJoinType joinType, SqlAstJoinType joinType,
String[] targetColumns, String[] targetColumns,
SqlExpressionResolver sqlExpressionResolver) { SqlExpressionResolver sqlExpressionResolver) {
final TableReference joinedTableReference = new TableReference( final NamedTableReference joinedTableReference = new NamedTableReference(
joinTableExpression, joinTableExpression,
sqlAliasBase.generateNewAlias(), sqlAliasBase.generateNewAlias(),
joinType != SqlAstJoinType.INNER, joinType != SqlAstJoinType.INNER,
@ -1440,7 +1441,7 @@ public abstract class AbstractEntityPersister
} }
protected TableReference resolvePrimaryTableReference(SqlAliasBase sqlAliasBase) { protected TableReference resolvePrimaryTableReference(SqlAliasBase sqlAliasBase) {
return new TableReference( return new NamedTableReference(
getTableName(), getTableName(),
sqlAliasBase.generateNewAlias(), sqlAliasBase.generateNewAlias(),
false, false,
@ -4027,11 +4028,18 @@ public abstract class AbstractEntityPersister
return; return;
} }
final FilterAliasGenerator aliasGenerator = useQualifier && tableGroup != null final String alias;
? getFilterAliasGenerator( tableGroup ) if ( tableGroup == null ) {
: null; alias = null;
}
else if ( useQualifier && tableGroup.getPrimaryTableReference().getIdentificationVariable() != null ) {
alias = tableGroup.getPrimaryTableReference().getIdentificationVariable();
}
else {
alias = tableGroup.getPrimaryTableReference().getTableId();
}
final String fragment = StringHelper.replace( sqlWhereStringTemplate, Template.TEMPLATE, aliasGenerator.getAlias( getTableName() ) ); final String fragment = StringHelper.replace( sqlWhereStringTemplate, Template.TEMPLATE, alias );
predicateConsumer.accept( new SqlFragmentPredicate( fragment ) ); predicateConsumer.accept( new SqlFragmentPredicate( fragment ) );
} }

View File

@ -45,7 +45,6 @@ import org.hibernate.metamodel.mapping.EntityDiscriminatorMapping;
import org.hibernate.metamodel.mapping.EntityIdentifierMapping; import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
import org.hibernate.metamodel.mapping.EntityMappingType; import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.mapping.EntityVersionMapping; import org.hibernate.metamodel.mapping.EntityVersionMapping;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.internal.BasicEntityIdentifierMappingImpl; import org.hibernate.metamodel.mapping.internal.BasicEntityIdentifierMappingImpl;
import org.hibernate.metamodel.mapping.internal.CaseStatementDiscriminatorMappingImpl; import org.hibernate.metamodel.mapping.internal.CaseStatementDiscriminatorMappingImpl;
import org.hibernate.metamodel.mapping.internal.MappingModelCreationHelper; import org.hibernate.metamodel.mapping.internal.MappingModelCreationHelper;
@ -55,15 +54,10 @@ import org.hibernate.query.NavigablePath;
import org.hibernate.sql.InFragment; import org.hibernate.sql.InFragment;
import org.hibernate.sql.Insert; import org.hibernate.sql.Insert;
import org.hibernate.sql.ast.SqlAstJoinType; import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.tree.expression.CaseSearchedExpression; import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.QueryLiteral;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin; import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
import org.hibernate.sql.ast.tree.predicate.NullnessPredicate;
import org.hibernate.sql.ast.tree.predicate.Predicate;
import org.hibernate.sql.results.graph.DomainResult; import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState; import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.entity.internal.EntityResultJoinedSubclassImpl; import org.hibernate.sql.results.graph.entity.internal.EntityResultJoinedSubclassImpl;
@ -1346,7 +1340,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
final TableReferenceJoin[] oldJoins = tableReferenceJoins.toArray( new TableReferenceJoin[0] ); final TableReferenceJoin[] oldJoins = tableReferenceJoins.toArray( new TableReferenceJoin[0] );
tableReferenceJoins.clear(); tableReferenceJoins.clear();
for ( TableReferenceJoin oldJoin : oldJoins ) { for ( TableReferenceJoin oldJoin : oldJoins ) {
final TableReference joinedTableReference = oldJoin.getJoinedTableReference(); final NamedTableReference joinedTableReference = oldJoin.getJoinedTableReference();
if ( retainedTableReferences.contains( joinedTableReference ) ) { if ( retainedTableReferences.contains( joinedTableReference ) ) {
if ( oldJoin.getJoinType() != SqlAstJoinType.INNER if ( oldJoin.getJoinType() != SqlAstJoinType.INNER
&& sharedSuperclassTables.contains( joinedTableReference.getTableExpression() ) ) { && sharedSuperclassTables.contains( joinedTableReference.getTableExpression() ) ) {
@ -1383,80 +1377,4 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
} }
} }
private static class CaseSearchedExpressionInfo{
CaseSearchedExpression caseSearchedExpression;
List<ColumnReference> columnReferences = new ArrayList<>( );
}
private CaseSearchedExpressionInfo getCaseSearchedExpression(TableGroup entityTableGroup) {
CaseSearchedExpressionInfo info = new CaseSearchedExpressionInfo();
final TableReference primaryTableReference = entityTableGroup.getPrimaryTableReference();
final BasicType<?> discriminatorType = (BasicType<?>) getDiscriminatorType();
final CaseSearchedExpression caseSearchedExpression = new CaseSearchedExpression( discriminatorType );
boolean addPrimaryTableCaseAsLastCaseExpression = false;
for ( String tableName : discriminatorValuesByTableName.keySet() ) {
if ( !primaryTableReference.getTableExpression().equals( tableName ) ) {
TableReference tableReference = entityTableGroup.getTableReference( entityTableGroup.getNavigablePath(), tableName );
if ( tableReference == null ) {
// we have not yet created a TableReference for this sub-class table, but we need to because
// it has a discriminator value associated with it
tableReference = entityTableGroup.resolveTableReference( entityTableGroup.getNavigablePath(), tableName );
}
final ColumnReference identifierColumnReference = getIdentifierColumnReference( tableReference );
info.columnReferences.add( identifierColumnReference );
addWhen(
caseSearchedExpression,
tableReference,
identifierColumnReference,
discriminatorType
);
}
else {
addPrimaryTableCaseAsLastCaseExpression = true;
}
}
if ( addPrimaryTableCaseAsLastCaseExpression ) {
addWhen(
caseSearchedExpression,
primaryTableReference,
getIdentifierColumnReference( primaryTableReference ),
discriminatorType
);
}
info.caseSearchedExpression = caseSearchedExpression;
return info;
}
private void addWhen(
CaseSearchedExpression caseSearchedExpression,
TableReference table,
ColumnReference identifierColumnReference,
BasicType<?> resultType) {
final Predicate predicate = new NullnessPredicate( identifierColumnReference, true );
final Expression expression = new QueryLiteral<>(
discriminatorValuesByTableName.get( table.getTableExpression() ),
resultType
);
caseSearchedExpression.when( predicate, expression );
}
private ColumnReference getIdentifierColumnReference(TableReference tableReference) {
final List<JdbcMapping> jdbcMappings = getIdentifierMapping().getJdbcMappings();
return new ColumnReference(
tableReference.getIdentificationVariable(),
discriminatorColumnNameByTableName.get( tableReference.getTableExpression() ),
false,
null,
null,
jdbcMappings.get( 0 ),
getFactory()
);
}
} }

View File

@ -52,6 +52,7 @@ import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.tree.expression.ColumnReference; import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.Expression; import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.QueryLiteral; import org.hibernate.sql.ast.tree.expression.QueryLiteral;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.predicate.ComparisonPredicate; import org.hibernate.sql.ast.tree.predicate.ComparisonPredicate;
@ -846,6 +847,7 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
if ( additionalPredicateCollectorAccess != null && needsDiscriminator() ) { if ( additionalPredicateCollectorAccess != null && needsDiscriminator() ) {
final Predicate discriminatorPredicate = createDiscriminatorPredicate( final Predicate discriminatorPredicate = createDiscriminatorPredicate(
tableGroup.getPrimaryTableReference().getIdentificationVariable(),
tableGroup, tableGroup,
expressionResolver, expressionResolver,
creationContext creationContext
@ -865,6 +867,7 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
if ( needsDiscriminator() ) { if ( needsDiscriminator() ) {
predicateConsumer.accept( predicateConsumer.accept(
createDiscriminatorPredicate( createDiscriminatorPredicate(
alias,
tableGroup, tableGroup,
creationState.getSqlExpressionResolver(), creationState.getSqlExpressionResolver(),
creationState.getCreationContext() creationState.getCreationContext()
@ -875,6 +878,7 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
} }
private Predicate createDiscriminatorPredicate( private Predicate createDiscriminatorPredicate(
String alias,
TableGroup tableGroup, TableGroup tableGroup,
SqlExpressionResolver sqlExpressionResolver, SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) { SqlAstCreationContext creationContext) {
@ -899,15 +903,16 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
final Expression sqlExpression = sqlExpressionResolver.resolveSqlExpression( final Expression sqlExpression = sqlExpressionResolver.resolveSqlExpression(
columnReferenceKey, columnReferenceKey,
sqlAstProcessingState -> new ColumnReference( sqlAstProcessingState -> new ColumnReference(
tableGroup.getPrimaryTableReference().getIdentificationVariable(), alias,
discriminatorExpression, discriminatorExpression,
isDiscriminatorFormula(), isDiscriminatorFormula(),
null, null,
null, null,
(discriminatorType).getJdbcMapping(), discriminatorType.getJdbcMapping(),
getFactory() getFactory()
) )
); );
if ( hasSubclasses() ) { if ( hasSubclasses() ) {
final Object[] discriminatorValues = fullDiscriminatorValues(); final Object[] discriminatorValues = fullDiscriminatorValues();
final List<Expression> values = new ArrayList<>( discriminatorValues.length ); final List<Expression> values = new ArrayList<>( discriminatorValues.length );
@ -941,6 +946,7 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
} }
return p; return p;
} }
final Object value = getDiscriminatorValue(); final Object value = getDiscriminatorValue();
final boolean hasNotNullDiscriminator = value == NOT_NULL_DISCRIMINATOR; final boolean hasNotNullDiscriminator = value == NOT_NULL_DISCRIMINATOR;
final boolean hasNullDiscriminator = value == NULL_DISCRIMINATOR; final boolean hasNullDiscriminator = value == NULL_DISCRIMINATOR;
@ -967,7 +973,7 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
return; return;
} }
// The optimization is to simply add the discriminator filter fragment for all treated entity names // The optimization is to simply add the discriminator filter fragment for all treated entity names
final TableReference tableReference = tableGroup.getPrimaryTableReference(); final NamedTableReference tableReference = (NamedTableReference) tableGroup.getPrimaryTableReference();
tableReference.setPrunedTableExpression( tableReference.setPrunedTableExpression(
"(select * from " + getTableName() + " t where " + discriminatorFilterFragment( "t", treatedEntityNames ) + ")" "(select * from " + getTableName() + " t where " + discriminatorFilterFragment( "t", treatedEntityNames ) + ")"
); );

View File

@ -52,6 +52,7 @@ import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAstCreationContext; import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlExpressionResolver; import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.tree.expression.ColumnReference; import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.UnionTableGroup; import org.hibernate.sql.ast.tree.from.UnionTableGroup;
@ -257,13 +258,13 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
SqlExpressionResolver expressionResolver, SqlExpressionResolver expressionResolver,
FromClauseAccess fromClauseAccess, FromClauseAccess fromClauseAccess,
SqlAstCreationContext creationContext) { SqlAstCreationContext creationContext) {
final TableReference tableReference = resolvePrimaryTableReference( sqlAliasBase ); final UnionTableReference tableReference = resolvePrimaryTableReference( sqlAliasBase );
return new UnionTableGroup( canUseInnerJoins, navigablePath, tableReference, this, explicitSourceAlias ); return new UnionTableGroup( canUseInnerJoins, navigablePath, tableReference, this, explicitSourceAlias );
} }
@Override @Override
protected TableReference resolvePrimaryTableReference(SqlAliasBase sqlAliasBase) { protected UnionTableReference resolvePrimaryTableReference(SqlAliasBase sqlAliasBase) {
return new UnionTableReference( return new UnionTableReference(
getTableName(), getTableName(),
subclassTableExpressions, subclassTableExpressions,
@ -401,7 +402,7 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
if ( treatedEntityNames.contains( getEntityName() ) ) { if ( treatedEntityNames.contains( getEntityName() ) ) {
return; return;
} }
final TableReference tableReference = tableGroup.resolveTableReference( getRootTableName() ); final NamedTableReference tableReference = (NamedTableReference) tableGroup.resolveTableReference( getRootTableName() );
// Replace the default union sub-query with a specially created one that only selects the tables for the treated entity names // Replace the default union sub-query with a specially created one that only selects the tables for the treated entity names
tableReference.setPrunedTableExpression( generateSubquery( treatedEntityNames ) ); tableReference.setPrunedTableExpression( generateSubquery( treatedEntityNames ) );
} }

View File

@ -292,7 +292,7 @@ public class DomainResultCreationStateImpl
@Override @Override
public SqlSelection resolveSqlSelection( public SqlSelection resolveSqlSelection(
Expression expression, Expression expression,
JavaType javaTypeDescriptor, JavaType<?> javaTypeDescriptor,
TypeConfiguration typeConfiguration) { TypeConfiguration typeConfiguration) {
if ( expression == null ) { if ( expression == null ) {
throw new IllegalArgumentException( "Expression cannot be null" ); throw new IllegalArgumentException( "Expression cannot be null" );

View File

@ -16,10 +16,10 @@ import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.mapping.PluralAttributeMapping; import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess; import org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess;
import org.hibernate.metamodel.spi.RuntimeModelCreationContext; import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
import org.hibernate.query.spi.SqlOmittingQueryOptions;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy; import org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy; import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.sql.ast.tree.delete.DeleteStatement; import org.hibernate.sql.ast.tree.delete.DeleteStatement;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.predicate.Predicate; import org.hibernate.sql.ast.tree.predicate.Predicate;
import org.hibernate.sql.exec.spi.ExecutionContext; import org.hibernate.sql.exec.spi.ExecutionContext;
@ -130,7 +130,12 @@ public class SqmMutationStrategyHelper {
else { else {
// element-collection or many-to-many - delete the collection-table row // element-collection or many-to-many - delete the collection-table row
final TableReference tableReference = new TableReference( separateCollectionTable, DeleteStatement.DEFAULT_ALIAS, true, sessionFactory ); final NamedTableReference tableReference = new NamedTableReference(
separateCollectionTable,
DeleteStatement.DEFAULT_ALIAS,
true,
sessionFactory
);
final DeleteStatement sqlAstDelete = new DeleteStatement( final DeleteStatement sqlAstDelete = new DeleteStatement(
tableReference, tableReference,

View File

@ -45,6 +45,7 @@ import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.Expression; import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.JdbcParameter; import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.expression.SqlTuple; import org.hibernate.sql.ast.tree.expression.SqlTuple;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.UnionTableReference; import org.hibernate.sql.ast.tree.from.UnionTableReference;
import org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate; import org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate;
@ -177,7 +178,7 @@ public abstract class AbstractCteMutationHandler extends AbstractMutationHandler
querySpec.getSelectClause().addSqlSelection( new SqlSelectionImpl( 1, 0, count ) ); querySpec.getSelectClause().addSqlSelection( new SqlSelectionImpl( 1, 0, count ) );
querySpec.getFromClause().addRoot( querySpec.getFromClause().addRoot(
new CteTableGroup( new CteTableGroup(
new TableReference( new NamedTableReference(
idSelectCte.getCteTable().getTableExpression(), idSelectCte.getCteTable().getTableExpression(),
CTE_TABLE_IDENTIFIER, CTE_TABLE_IDENTIFIER,
false, false,
@ -241,7 +242,7 @@ public abstract class AbstractCteMutationHandler extends AbstractMutationHandler
CteStatement idSelectCte, CteStatement idSelectCte,
ModelPart fkModelPart, ModelPart fkModelPart,
SessionFactoryImplementor factory) { SessionFactoryImplementor factory) {
final TableReference idSelectTableReference = new TableReference( final NamedTableReference idSelectTableReference = new NamedTableReference(
idSelectCte.getCteTable().getTableExpression(), idSelectCte.getCteTable().getTableExpression(),
CTE_TABLE_IDENTIFIER, CTE_TABLE_IDENTIFIER,
false, false,
@ -313,11 +314,11 @@ public abstract class AbstractCteMutationHandler extends AbstractMutationHandler
SessionFactoryImplementor factory); SessionFactoryImplementor factory);
protected TableReference resolveUnionTableReference( protected NamedTableReference resolveUnionTableReference(
TableReference tableReference, TableReference tableReference,
String tableExpression) { String tableExpression) {
if ( tableReference instanceof UnionTableReference ) { if ( tableReference instanceof UnionTableReference ) {
return new TableReference( return new NamedTableReference(
tableExpression, tableExpression,
tableReference.getIdentificationVariable(), tableReference.getIdentificationVariable(),
tableReference.isOptional(), tableReference.isOptional(),
@ -325,7 +326,7 @@ public abstract class AbstractCteMutationHandler extends AbstractMutationHandler
); );
} }
else { else {
return tableReference; return (NamedTableReference) tableReference;
} }
} }

View File

@ -27,6 +27,7 @@ import org.hibernate.sql.ast.tree.cte.CteTable;
import org.hibernate.sql.ast.tree.delete.DeleteStatement; import org.hibernate.sql.ast.tree.delete.DeleteStatement;
import org.hibernate.sql.ast.tree.expression.ColumnReference; import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.JdbcParameter; import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.select.SelectStatement; import org.hibernate.sql.ast.tree.select.SelectStatement;
@ -106,7 +107,12 @@ public class CteDeleteHandler extends AbstractCteMutationHandler implements Dele
idSelectCte.getCteTable().getCteColumns(), idSelectCte.getCteTable().getCteColumns(),
factory factory
); );
final TableReference dmlTableReference = new TableReference( tableExpression, null, true, factory ); final NamedTableReference dmlTableReference = new NamedTableReference(
tableExpression,
DeleteStatement.DEFAULT_ALIAS,
true,
factory
);
final List<ColumnReference> columnReferences = new ArrayList<>( idSelectCte.getCteTable().getCteColumns().size() ); final List<ColumnReference> columnReferences = new ArrayList<>( idSelectCte.getCteTable().getCteColumns().size() );
pluralAttribute.getKeyDescriptor().visitKeySelectables( pluralAttribute.getKeyDescriptor().visitKeySelectables(
(index, selectable) -> columnReferences.add( (index, selectable) -> columnReferences.add(
@ -147,7 +153,7 @@ public class CteDeleteHandler extends AbstractCteMutationHandler implements Dele
true, true,
true true
); );
final TableReference dmlTableReference = resolveUnionTableReference( final NamedTableReference dmlTableReference = resolveUnionTableReference(
updatingTableReference, updatingTableReference,
tableExpression tableExpression
); );

View File

@ -76,7 +76,7 @@ import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.expression.Over; import org.hibernate.sql.ast.tree.expression.Over;
import org.hibernate.sql.ast.tree.expression.QueryLiteral; import org.hibernate.sql.ast.tree.expression.QueryLiteral;
import org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression; import org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression;
import org.hibernate.sql.ast.tree.from.StandardTableGroup; import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin; import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
@ -189,7 +189,7 @@ public class CteInsertHandler implements InsertHandler {
final List<Map.Entry<SqmCteTableColumn, Assignment>> targetPathColumns = new ArrayList<>( size ); final List<Map.Entry<SqmCteTableColumn, Assignment>> targetPathColumns = new ArrayList<>( size );
final List<SqmCteTableColumn> targetPathSqmCteColumns = new ArrayList<>( size ); final List<SqmCteTableColumn> targetPathSqmCteColumns = new ArrayList<>( size );
final Map<SqmParameter, MappingModelExpressable> paramTypeResolutions = new LinkedHashMap<>(); final Map<SqmParameter, MappingModelExpressable> paramTypeResolutions = new LinkedHashMap<>();
final TableReference entityTableReference = new TableReference( final NamedTableReference entityTableReference = new NamedTableReference(
cteTable.getCteName(), cteTable.getCteName(),
TemporaryTable.DEFAULT_ALIAS, TemporaryTable.DEFAULT_ALIAS,
true, true,
@ -240,7 +240,7 @@ public class CteInsertHandler implements InsertHandler {
final Statement queryStatement; final Statement queryStatement;
if ( sqmInsertStatement instanceof SqmInsertSelectStatement ) { if ( sqmInsertStatement instanceof SqmInsertSelectStatement ) {
final QueryPart queryPart = sqmConverter.visitQueryPart( ( (SqmInsertSelectStatement<?>) sqmInsertStatement ).getSelectQueryPart() ); final QueryPart queryPart = sqmConverter.visitQueryPart( ( (SqmInsertSelectStatement<?>) sqmInsertStatement ).getSelectQueryPart() );
queryPart.forEachQuerySpec( queryPart.visitQuerySpecs(
querySpec -> { querySpec -> {
// This returns true if the insertion target uses a sequence with an optimizer // This returns true if the insertion target uses a sequence with an optimizer
// in which case we will fill the row_number column instead of the id column // in which case we will fill the row_number column instead of the id column
@ -407,7 +407,7 @@ public class CteInsertHandler implements InsertHandler {
final QuerySpec rowsWithSequenceQuery = new QuerySpec( true ); final QuerySpec rowsWithSequenceQuery = new QuerySpec( true );
rowsWithSequenceQuery.getFromClause().addRoot( rowsWithSequenceQuery.getFromClause().addRoot(
new CteTableGroup( new CteTableGroup(
new TableReference( new NamedTableReference(
baseTableName, baseTableName,
"e", "e",
false, false,
@ -465,7 +465,7 @@ public class CteInsertHandler implements InsertHandler {
final TableGroup baseTableGroup = new TableGroupImpl( final TableGroup baseTableGroup = new TableGroupImpl(
navigablePath, navigablePath,
null, null,
new TableReference( new NamedTableReference(
baseTableName, baseTableName,
"e", "e",
false, false,
@ -475,7 +475,7 @@ public class CteInsertHandler implements InsertHandler {
null null
); );
final TableGroup rowsWithSequenceTableGroup = new CteTableGroup( final TableGroup rowsWithSequenceTableGroup = new CteTableGroup(
new TableReference( new NamedTableReference(
ROW_NUMBERS_WITH_SEQUENCE_VALUE, ROW_NUMBERS_WITH_SEQUENCE_VALUE,
"t", "t",
false, false,
@ -639,7 +639,7 @@ public class CteInsertHandler implements InsertHandler {
querySpec.getSelectClause().addSqlSelection( new SqlSelectionImpl( 1, 0, count ) ); querySpec.getSelectClause().addSqlSelection( new SqlSelectionImpl( 1, 0, count ) );
querySpec.getFromClause().addRoot( querySpec.getFromClause().addRoot(
new CteTableGroup( new CteTableGroup(
new TableReference( new NamedTableReference(
// We want to return the insertion count of the base table // We want to return the insertion count of the base table
baseInsertCte, baseInsertCte,
CTE_TABLE_IDENTIFIER, CTE_TABLE_IDENTIFIER,
@ -789,7 +789,7 @@ public class CteInsertHandler implements InsertHandler {
true true
); );
final List<Map.Entry<SqmCteTableColumn, Assignment>> assignmentList = assignmentsByTable.get( updatingTableReference ); final List<Map.Entry<SqmCteTableColumn, Assignment>> assignmentList = assignmentsByTable.get( updatingTableReference );
final TableReference dmlTableReference = resolveUnionTableReference( final NamedTableReference dmlTableReference = resolveUnionTableReference(
updatingTableReference, updatingTableReference,
tableExpression tableExpression
); );
@ -806,7 +806,7 @@ public class CteInsertHandler implements InsertHandler {
final String baseTableName = "base_" + queryCte.getCteTable().getTableExpression(); final String baseTableName = "base_" + queryCte.getCteTable().getTableExpression();
insertSelectSpec.getFromClause().addRoot( insertSelectSpec.getFromClause().addRoot(
new CteTableGroup( new CteTableGroup(
new TableReference( new NamedTableReference(
baseTableName, baseTableName,
"e", "e",
false, false,
@ -862,7 +862,7 @@ public class CteInsertHandler implements InsertHandler {
final TableGroup baseTableGroup = new TableGroupImpl( final TableGroup baseTableGroup = new TableGroupImpl(
navigablePath, navigablePath,
null, null,
new TableReference( new NamedTableReference(
baseTableName, baseTableName,
"e", "e",
false, false,
@ -872,7 +872,7 @@ public class CteInsertHandler implements InsertHandler {
null null
); );
final TableGroup rootInsertCteTableGroup = new CteTableGroup( final TableGroup rootInsertCteTableGroup = new CteTableGroup(
new TableReference( new NamedTableReference(
getCteTableName( tableExpression ), getCteTableName( tableExpression ),
"t", "t",
false, false,
@ -950,7 +950,7 @@ public class CteInsertHandler implements InsertHandler {
final QuerySpec finalResultQuery = new QuerySpec( true ); final QuerySpec finalResultQuery = new QuerySpec( true );
finalResultQuery.getFromClause().addRoot( finalResultQuery.getFromClause().addRoot(
new CteTableGroup( new CteTableGroup(
new TableReference( new NamedTableReference(
dmlResultCte.getTableExpression(), dmlResultCte.getTableExpression(),
"e", "e",
false, false,
@ -1008,7 +1008,7 @@ public class CteInsertHandler implements InsertHandler {
else { else {
insertSelectSpec.getFromClause().addRoot( insertSelectSpec.getFromClause().addRoot(
new CteTableGroup( new CteTableGroup(
new TableReference( new NamedTableReference(
queryCte.getCteTable().getTableExpression(), queryCte.getCteTable().getTableExpression(),
"e", "e",
false, false,
@ -1103,11 +1103,11 @@ public class CteInsertHandler implements InsertHandler {
return getCteTableName( rootTableName ); return getCteTableName( rootTableName );
} }
protected TableReference resolveUnionTableReference( protected NamedTableReference resolveUnionTableReference(
TableReference tableReference, TableReference tableReference,
String tableExpression) { String tableExpression) {
if ( tableReference instanceof UnionTableReference ) { if ( tableReference instanceof UnionTableReference ) {
return new TableReference( return new NamedTableReference(
tableExpression, tableExpression,
tableReference.getIdentificationVariable(), tableReference.getIdentificationVariable(),
tableReference.isOptional(), tableReference.isOptional(),
@ -1115,7 +1115,7 @@ public class CteInsertHandler implements InsertHandler {
); );
} }
else { else {
return tableReference; return (NamedTableReference) tableReference;
} }
} }

View File

@ -32,6 +32,7 @@ import org.hibernate.sql.ast.tree.cte.CteStatement;
import org.hibernate.sql.ast.tree.cte.CteTable; import org.hibernate.sql.ast.tree.cte.CteTable;
import org.hibernate.sql.ast.tree.expression.ColumnReference; import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.JdbcParameter; import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin; import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
@ -154,7 +155,7 @@ public class CteUpdateHandler extends AbstractCteMutationHandler implements Upda
if ( assignmentList == null ) { if ( assignmentList == null ) {
return; return;
} }
final TableReference dmlTableReference = resolveUnionTableReference( final NamedTableReference dmlTableReference = resolveUnionTableReference(
updatingTableReference, updatingTableReference,
tableExpression tableExpression
); );

View File

@ -18,7 +18,6 @@ import org.hibernate.metamodel.mapping.ModelPart;
import org.hibernate.metamodel.mapping.SelectableConsumer; import org.hibernate.metamodel.mapping.SelectableConsumer;
import org.hibernate.metamodel.mapping.EntityMappingType; import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.mapping.PluralAttributeMapping; import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.metamodel.mapping.SelectableConsumer;
import org.hibernate.query.spi.DomainQueryExecutionContext; import org.hibernate.query.spi.DomainQueryExecutionContext;
import org.hibernate.query.sqm.internal.DomainParameterXref; import org.hibernate.query.sqm.internal.DomainParameterXref;
import org.hibernate.query.sqm.internal.SqmJdbcExecutionContextAdapter; import org.hibernate.query.sqm.internal.SqmJdbcExecutionContextAdapter;
@ -27,7 +26,7 @@ import org.hibernate.query.sqm.mutation.internal.MatchingIdSelectionHelper;
import org.hibernate.query.sqm.tree.delete.SqmDeleteStatement; import org.hibernate.query.sqm.tree.delete.SqmDeleteStatement;
import org.hibernate.sql.ast.SqlAstTranslatorFactory; import org.hibernate.sql.ast.SqlAstTranslatorFactory;
import org.hibernate.sql.ast.tree.delete.DeleteStatement; import org.hibernate.sql.ast.tree.delete.DeleteStatement;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.predicate.Predicate; import org.hibernate.sql.ast.tree.predicate.Predicate;
import org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl; import org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl;
import org.hibernate.sql.exec.spi.JdbcDelete; import org.hibernate.sql.exec.spi.JdbcDelete;
@ -160,7 +159,7 @@ public class InlineDeleteHandler implements DeleteHandler {
ModelPart valueModelPart, ModelPart valueModelPart,
JdbcParameterBindings jdbcParameterBindings, JdbcParameterBindings jdbcParameterBindings,
DomainQueryExecutionContext executionContext) { DomainQueryExecutionContext executionContext) {
final TableReference targetTableReference = new TableReference( final NamedTableReference targetTableReference = new NamedTableReference(
targetTableExpression, targetTableExpression,
DeleteStatement.DEFAULT_ALIAS, DeleteStatement.DEFAULT_ALIAS,
false, false,

View File

@ -31,6 +31,7 @@ import org.hibernate.sql.ast.SqlAstTranslatorFactory;
import org.hibernate.sql.ast.spi.SqlExpressionResolver; import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.tree.expression.ColumnReference; import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.QueryLiteral; import org.hibernate.sql.ast.tree.expression.QueryLiteral;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.StandardTableGroup; import org.hibernate.sql.ast.tree.from.StandardTableGroup;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
@ -65,9 +66,9 @@ public final class ExecuteWithTemporaryTableHelper {
assert mutatingTableGroup.getModelPart() instanceof EntityMappingType; assert mutatingTableGroup.getModelPart() instanceof EntityMappingType;
final EntityMappingType mutatingEntityDescriptor = (EntityMappingType) mutatingTableGroup.getModelPart(); final EntityMappingType mutatingEntityDescriptor = (EntityMappingType) mutatingTableGroup.getModelPart();
final TableReference idTableReference = new TableReference( final NamedTableReference idTableReference = new NamedTableReference(
idTable.getTableExpression(), idTable.getTableExpression(),
null, InsertStatement.DEFAULT_ALIAS,
false, false,
factory factory
); );
@ -153,7 +154,7 @@ public final class ExecuteWithTemporaryTableHelper {
// Visit the table joins and reset the lock mode if we encounter OUTER joins that are not supported // Visit the table joins and reset the lock mode if we encounter OUTER joins that are not supported
if ( temporaryTableInsert.getSourceSelectStatement() != null if ( temporaryTableInsert.getSourceSelectStatement() != null
&& !jdbcEnvironment.getDialect().supportsOuterJoinForUpdate() ) { && !jdbcEnvironment.getDialect().supportsOuterJoinForUpdate() ) {
temporaryTableInsert.getSourceSelectStatement().forEachQuerySpec( temporaryTableInsert.getSourceSelectStatement().visitQuerySpecs(
querySpec -> { querySpec -> {
querySpec.getFromClause().visitTableJoins( querySpec.getFromClause().visitTableJoins(
tableJoin -> { tableJoin -> {
@ -197,7 +198,7 @@ public final class ExecuteWithTemporaryTableHelper {
ExecutionContext executionContext) { ExecutionContext executionContext) {
final QuerySpec querySpec = new QuerySpec( false ); final QuerySpec querySpec = new QuerySpec( false );
final TableReference idTableReference = new TableReference( final NamedTableReference idTableReference = new NamedTableReference(
idTable.getTableExpression(), idTable.getTableExpression(),
TemporaryTable.DEFAULT_ALIAS, TemporaryTable.DEFAULT_ALIAS,
true, true,

View File

@ -15,7 +15,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import org.hibernate.dialect.identity.IdentityColumnSupport;
import org.hibernate.dialect.temptable.TemporaryTable; import org.hibernate.dialect.temptable.TemporaryTable;
import org.hibernate.dialect.temptable.TemporaryTableColumn; import org.hibernate.dialect.temptable.TemporaryTableColumn;
import org.hibernate.engine.FetchTiming; import org.hibernate.engine.FetchTiming;
@ -23,7 +22,6 @@ import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.id.OptimizableGenerator; import org.hibernate.id.OptimizableGenerator;
import org.hibernate.id.PostInsertIdentifierGenerator; import org.hibernate.id.PostInsertIdentifierGenerator;
import org.hibernate.id.PostInsertIdentityPersister; import org.hibernate.id.PostInsertIdentityPersister;
@ -48,6 +46,7 @@ import org.hibernate.query.sqm.tree.expression.SqmParameter;
import org.hibernate.query.sqm.tree.insert.SqmInsertStatement; import org.hibernate.query.sqm.tree.insert.SqmInsertStatement;
import org.hibernate.sql.ast.tree.expression.ColumnReference; import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.JdbcParameter; import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.UnionTableReference; import org.hibernate.sql.ast.tree.from.UnionTableReference;
@ -227,9 +226,9 @@ public class InsertExecutionDelegate implements TableBasedInsertHandler.Executio
throw new SemanticException( "Assignment referred to column of a joined association: " + columnReference ); throw new SemanticException( "Assignment referred to column of a joined association: " + columnReference );
} }
private TableReference resolveUnionTableReference(TableReference tableReference, String tableExpression) { private NamedTableReference resolveUnionTableReference(TableReference tableReference, String tableExpression) {
if ( tableReference instanceof UnionTableReference ) { if ( tableReference instanceof UnionTableReference ) {
return new TableReference( return new NamedTableReference(
tableExpression, tableExpression,
tableReference.getIdentificationVariable(), tableReference.getIdentificationVariable(),
tableReference.isOptional(), tableReference.isOptional(),
@ -237,7 +236,7 @@ public class InsertExecutionDelegate implements TableBasedInsertHandler.Executio
); );
} }
else { else {
return tableReference; return (NamedTableReference) tableReference;
} }
} }
@ -259,20 +258,21 @@ public class InsertExecutionDelegate implements TableBasedInsertHandler.Executio
throw new IllegalStateException( "There must be at least a single root table assignment" ); throw new IllegalStateException( "There must be at least a single root table assignment" );
} }
final TableReference dmlTableReference = resolveUnionTableReference( updatingTableReference, tableExpression ); final NamedTableReference dmlTableReference = resolveUnionTableReference( updatingTableReference, tableExpression );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Create the SQL AST and convert it into a JdbcOperation // Create the SQL AST and convert it into a JdbcOperation
final QuerySpec querySpec = new QuerySpec( true ); final QuerySpec querySpec = new QuerySpec( true );
final NamedTableReference temporaryTableReference = new NamedTableReference(
insertStatement.getTargetTable().getTableExpression(),
updatingTableReference.getIdentificationVariable(),
false,
sessionFactory
);
final TableGroupImpl temporaryTableGroup = new TableGroupImpl( final TableGroupImpl temporaryTableGroup = new TableGroupImpl(
updatingTableGroup.getNavigablePath(), updatingTableGroup.getNavigablePath(),
null, null,
new TableReference( temporaryTableReference,
insertStatement.getTargetTable().getTableExpression(),
updatingTableReference.getIdentificationVariable(),
false,
sessionFactory
),
entityDescriptor, entityDescriptor,
null null
); );
@ -386,7 +386,7 @@ public class InsertExecutionDelegate implements TableBasedInsertHandler.Executio
entityTable.getColumns().size() - 1 entityTable.getColumns().size() - 1
); );
final UpdateStatement updateStatement = new UpdateStatement( final UpdateStatement updateStatement = new UpdateStatement(
temporaryTableGroup.getPrimaryTableReference(), temporaryTableReference,
temporaryTableAssignments, temporaryTableAssignments,
new ComparisonPredicate( new ComparisonPredicate(
new ColumnReference( new ColumnReference(
@ -524,7 +524,7 @@ public class InsertExecutionDelegate implements TableBasedInsertHandler.Executio
) )
); );
final UpdateStatement updateStatement = new UpdateStatement( final UpdateStatement updateStatement = new UpdateStatement(
temporaryTableGroup.getPrimaryTableReference(), temporaryTableReference,
temporaryTableAssignments, temporaryTableAssignments,
new ComparisonPredicate( new ComparisonPredicate(
new ColumnReference( new ColumnReference(
@ -595,13 +595,13 @@ public class InsertExecutionDelegate implements TableBasedInsertHandler.Executio
// no assignments for this table - skip it // no assignments for this table - skip it
return; return;
} }
final TableReference dmlTargetTableReference = resolveUnionTableReference( updatingTableReference, tableExpression ); final NamedTableReference dmlTargetTableReference = resolveUnionTableReference( updatingTableReference, tableExpression );
final QuerySpec querySpec = new QuerySpec( true ); final QuerySpec querySpec = new QuerySpec( true );
final TableGroupImpl temporaryTableGroup = new TableGroupImpl( final TableGroupImpl temporaryTableGroup = new TableGroupImpl(
updatingTableGroup.getNavigablePath(), updatingTableGroup.getNavigablePath(),
null, null,
new TableReference( new NamedTableReference(
insertStatement.getTargetTable().getTableExpression(), insertStatement.getTargetTable().getTableExpression(),
updatingTableReference.getIdentificationVariable(), updatingTableReference.getIdentificationVariable(),
false, false,

View File

@ -47,6 +47,7 @@ import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.Expression; import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.JdbcParameter; import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.expression.SqlTuple; import org.hibernate.sql.ast.tree.expression.SqlTuple;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.UnionTableReference; import org.hibernate.sql.ast.tree.from.UnionTableReference;
@ -210,7 +211,7 @@ public class RestrictedDeleteExecutionDelegate implements TableBasedDeleteHandle
final EntityPersister rootEntityPersister = entityDescriptor.getEntityPersister(); final EntityPersister rootEntityPersister = entityDescriptor.getEntityPersister();
final String rootTableName = ( (Joinable) rootEntityPersister ).getTableName(); final String rootTableName = ( (Joinable) rootEntityPersister ).getTableName();
final TableReference rootTableReference = tableGroup.resolveTableReference( final NamedTableReference rootTableReference = (NamedTableReference) tableGroup.resolveTableReference(
tableGroup.getNavigablePath(), tableGroup.getNavigablePath(),
rootTableName rootTableName
); );
@ -279,7 +280,7 @@ public class RestrictedDeleteExecutionDelegate implements TableBasedDeleteHandle
final MutableInteger rows = new MutableInteger(); final MutableInteger rows = new MutableInteger();
entityDescriptor.visitConstraintOrderedTables( entityDescriptor.visitConstraintOrderedTables(
(tableExpression, tableKeyColumnVisitationSupplier) -> { (tableExpression, tableKeyColumnVisitationSupplier) -> {
final TableReference tableReference = new TableReference( final NamedTableReference tableReference = new NamedTableReference(
tableExpression, tableExpression,
tableGroup.getPrimaryTableReference().getIdentificationVariable(), tableGroup.getPrimaryTableReference().getIdentificationVariable(),
false, false,
@ -312,7 +313,7 @@ public class RestrictedDeleteExecutionDelegate implements TableBasedDeleteHandle
entityDescriptor.visitConstraintOrderedTables( entityDescriptor.visitConstraintOrderedTables(
(tableExpression, tableKeyColumnVisitationSupplier) -> { (tableExpression, tableKeyColumnVisitationSupplier) -> {
if ( !tableExpression.equals( rootTableName ) ) { if ( !tableExpression.equals( rootTableName ) ) {
final TableReference tableReference = tableGroup.getTableReference( final NamedTableReference tableReference = (NamedTableReference) tableGroup.getTableReference(
tableGroup.getNavigablePath(), tableGroup.getNavigablePath(),
tableExpression, tableExpression,
true, true,
@ -349,7 +350,7 @@ public class RestrictedDeleteExecutionDelegate implements TableBasedDeleteHandle
} }
private int deleteFromRootTableWithoutIdTable( private int deleteFromRootTableWithoutIdTable(
TableReference rootTableReference, NamedTableReference rootTableReference,
Predicate predicate, Predicate predicate,
JdbcParameterBindings jdbcParameterBindings, JdbcParameterBindings jdbcParameterBindings,
ExecutionContext executionContext) { ExecutionContext executionContext) {
@ -361,7 +362,7 @@ public class RestrictedDeleteExecutionDelegate implements TableBasedDeleteHandle
} }
private int deleteFromNonRootTableWithoutIdTable( private int deleteFromNonRootTableWithoutIdTable(
TableReference targetTableReference, NamedTableReference targetTableReference,
Supplier<Consumer<SelectableConsumer>> tableKeyColumnVisitationSupplier, Supplier<Consumer<SelectableConsumer>> tableKeyColumnVisitationSupplier,
SqlExpressionResolver sqlExpressionResolver, SqlExpressionResolver sqlExpressionResolver,
TableGroup rootTableGroup, TableGroup rootTableGroup,
@ -371,7 +372,7 @@ public class RestrictedDeleteExecutionDelegate implements TableBasedDeleteHandle
assert targetTableReference != null; assert targetTableReference != null;
log.tracef( "deleteFromNonRootTable - %s", targetTableReference.getTableExpression() ); log.tracef( "deleteFromNonRootTable - %s", targetTableReference.getTableExpression() );
final TableReference deleteTableReference = new TableReference( final NamedTableReference deleteTableReference = new NamedTableReference(
targetTableReference.getTableExpression(), targetTableReference.getTableExpression(),
DeleteStatement.DEFAULT_ALIAS, DeleteStatement.DEFAULT_ALIAS,
true, true,
@ -573,7 +574,7 @@ public class RestrictedDeleteExecutionDelegate implements TableBasedDeleteHandle
final SessionFactoryImplementor factory = executionContext.getSession().getFactory(); final SessionFactoryImplementor factory = executionContext.getSession().getFactory();
final TableKeyExpressionCollector keyColumnCollector = new TableKeyExpressionCollector( entityDescriptor ); final TableKeyExpressionCollector keyColumnCollector = new TableKeyExpressionCollector( entityDescriptor );
final TableReference targetTable = new TableReference( final NamedTableReference targetTable = new NamedTableReference(
tableExpression, tableExpression,
DeleteStatement.DEFAULT_ALIAS, DeleteStatement.DEFAULT_ALIAS,
true, true,

View File

@ -41,6 +41,7 @@ import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.Expression; import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.JdbcParameter; import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.expression.Over; import org.hibernate.sql.ast.tree.expression.Over;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin; import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
@ -140,7 +141,7 @@ public class TableBasedInsertHandler implements InsertHandler {
final List<Assignment> targetPathColumns = new ArrayList<>(); final List<Assignment> targetPathColumns = new ArrayList<>();
final Map<SqmParameter, MappingModelExpressable> paramTypeResolutions = new LinkedHashMap<>(); final Map<SqmParameter, MappingModelExpressable> paramTypeResolutions = new LinkedHashMap<>();
final TableReference entityTableReference = new TableReference( final NamedTableReference entityTableReference = new NamedTableReference(
entityTable.getTableExpression(), entityTable.getTableExpression(),
TemporaryTable.DEFAULT_ALIAS, TemporaryTable.DEFAULT_ALIAS,
true, true,
@ -171,7 +172,7 @@ public class TableBasedInsertHandler implements InsertHandler {
if ( sqmInsertStatement instanceof SqmInsertSelectStatement ) { if ( sqmInsertStatement instanceof SqmInsertSelectStatement ) {
final QueryPart queryPart = converterDelegate.visitQueryPart( ( (SqmInsertSelectStatement<?>) sqmInsertStatement ).getSelectQueryPart() ); final QueryPart queryPart = converterDelegate.visitQueryPart( ( (SqmInsertSelectStatement<?>) sqmInsertStatement ).getSelectQueryPart() );
queryPart.forEachQuerySpec( queryPart.visitQuerySpecs(
querySpec -> { querySpec -> {
if ( additionalInsertValues.applySelections( querySpec, sessionFactory ) ) { if ( additionalInsertValues.applySelections( querySpec, sessionFactory ) ) {
final TemporaryTableColumn rowNumberColumn = entityTable.getColumns() final TemporaryTableColumn rowNumberColumn = entityTable.getColumns()

View File

@ -31,6 +31,7 @@ import org.hibernate.query.sqm.tree.expression.SqmParameter;
import org.hibernate.query.sqm.tree.update.SqmUpdateStatement; import org.hibernate.query.sqm.tree.update.SqmUpdateStatement;
import org.hibernate.sql.ast.tree.expression.ColumnReference; import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.JdbcParameter; import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.UnionTableReference; import org.hibernate.sql.ast.tree.from.UnionTableReference;
@ -200,18 +201,18 @@ public class UpdateExecutionDelegate implements TableBasedUpdateHandler.Executio
throw new SemanticException( "Assignment referred to column of a joined association: " + columnReference ); throw new SemanticException( "Assignment referred to column of a joined association: " + columnReference );
} }
private TableReference resolveUnionTableReference( private NamedTableReference resolveUnionTableReference(
TableReference tableReference, TableReference tableReference,
String tableExpression) { String tableExpression) {
if ( tableReference instanceof UnionTableReference ) { if ( tableReference instanceof UnionTableReference ) {
return new TableReference( return new NamedTableReference(
tableExpression, tableExpression,
tableReference.getIdentificationVariable(), tableReference.getIdentificationVariable(),
tableReference.isOptional(), tableReference.isOptional(),
sessionFactory sessionFactory
); );
} }
return tableReference; return (NamedTableReference) tableReference;
} }
private void updateTable( private void updateTable(

View File

@ -106,7 +106,9 @@ import org.hibernate.query.sqm.tree.domain.SqmTreatedRoot;
import org.hibernate.sql.ast.tree.expression.Over; import org.hibernate.sql.ast.tree.expression.Over;
import org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression; import org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression;
import org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup; import org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.PluralTableGroup; import org.hibernate.sql.ast.tree.from.PluralTableGroup;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.exec.internal.VersionTypeSeedParameterSpecification; import org.hibernate.sql.exec.internal.VersionTypeSeedParameterSpecification;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
@ -316,7 +318,6 @@ import org.hibernate.sql.ast.tree.from.PluralTableGroup;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin; import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.from.TableGroupJoinProducer; import org.hibernate.sql.ast.tree.from.TableGroupJoinProducer;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.VirtualTableGroup; import org.hibernate.sql.ast.tree.from.VirtualTableGroup;
import org.hibernate.sql.ast.tree.insert.InsertStatement; import org.hibernate.sql.ast.tree.insert.InsertStatement;
import org.hibernate.sql.ast.tree.insert.Values; import org.hibernate.sql.ast.tree.insert.Values;
@ -711,7 +712,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
return new UpdateStatement( return new UpdateStatement(
sqmStatement.isWithRecursive(), cteStatements, sqmStatement.isWithRecursive(), cteStatements,
rootTableGroup.getPrimaryTableReference(), (NamedTableReference) rootTableGroup.getPrimaryTableReference(),
assignments, assignments,
SqlAstTreeHelper.combinePredicates( suppliedPredicate, additionalRestrictions ), SqlAstTreeHelper.combinePredicates( suppliedPredicate, additionalRestrictions ),
Collections.emptyList() Collections.emptyList()
@ -940,7 +941,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
return new DeleteStatement( return new DeleteStatement(
statement.isWithRecursive(), statement.isWithRecursive(),
cteStatements, cteStatements,
rootTableGroup.getPrimaryTableReference(), (NamedTableReference) rootTableGroup.getPrimaryTableReference(),
SqlAstTreeHelper.combinePredicates( suppliedPredicate, additionalRestrictions ), SqlAstTreeHelper.combinePredicates( suppliedPredicate, additionalRestrictions ),
Collections.emptyList() Collections.emptyList()
); );
@ -995,7 +996,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
insertStatement = new InsertStatement( insertStatement = new InsertStatement(
sqmStatement.isWithRecursive(), sqmStatement.isWithRecursive(),
cteStatements, cteStatements,
rootTableGroup.getPrimaryTableReference(), (NamedTableReference) rootTableGroup.getPrimaryTableReference(),
Collections.emptyList() Collections.emptyList()
); );
additionalInsertValues = visitInsertionTargetPaths( additionalInsertValues = visitInsertionTargetPaths(
@ -1019,7 +1020,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
visitQueryPart( selectQueryPart ) visitQueryPart( selectQueryPart )
); );
insertStatement.getSourceSelectStatement().forEachQuerySpec( insertStatement.getSourceSelectStatement().visitQuerySpecs(
querySpec -> { querySpec -> {
final boolean appliedRowNumber = additionalInsertValues.applySelections( final boolean appliedRowNumber = additionalInsertValues.applySelections(
querySpec, querySpec,
@ -1074,7 +1075,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
final InsertStatement insertStatement = new InsertStatement( final InsertStatement insertStatement = new InsertStatement(
sqmStatement.isWithRecursive(), sqmStatement.isWithRecursive(),
cteStatements, cteStatements,
rootTableGroup.getPrimaryTableReference(), (NamedTableReference) rootTableGroup.getPrimaryTableReference(),
Collections.emptyList() Collections.emptyList()
); );
@ -5833,7 +5834,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
@Override @Override
public SqlSelection resolveSqlSelection( public SqlSelection resolveSqlSelection(
Expression expression, Expression expression,
JavaType javaTypeDescriptor, JavaType<?> javaTypeDescriptor,
TypeConfiguration typeConfiguration) { TypeConfiguration typeConfiguration) {
return delegate.resolveSqlSelection( expression, javaTypeDescriptor, typeConfiguration ); return delegate.resolveSqlSelection( expression, javaTypeDescriptor, typeConfiguration );
} }
@ -5889,7 +5890,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
@Override @Override
public SqlSelection resolveSqlSelection( public SqlSelection resolveSqlSelection(
Expression expression, Expression expression,
JavaType javaTypeDescriptor, JavaType<?> javaTypeDescriptor,
TypeConfiguration typeConfiguration) { TypeConfiguration typeConfiguration) {
SqlSelection selection = delegate.resolveSqlSelection( expression, javaTypeDescriptor, typeConfiguration ); SqlSelection selection = delegate.resolveSqlSelection( expression, javaTypeDescriptor, typeConfiguration );
List<SqlSelection> sqlSelectionList = sqlSelectionsForSqmSelection[index]; List<SqlSelection> sqlSelectionList = sqlSelectionsForSqmSelection[index];

View File

@ -140,7 +140,7 @@ public class SqlAstProcessingStateImpl
@Override @Override
public SqlSelection resolveSqlSelection( public SqlSelection resolveSqlSelection(
Expression expression, Expression expression,
JavaType javaTypeDescriptor, JavaType<?> javaTypeDescriptor,
TypeConfiguration typeConfiguration) { TypeConfiguration typeConfiguration) {
throw new ConversionException( "Unexpected call to resolve SqlSelection outside of QuerySpec processing" ); throw new ConversionException( "Unexpected call to resolve SqlSelection outside of QuerySpec processing" );
} }

View File

@ -68,7 +68,7 @@ public class SqlAstQueryPartProcessingStateImpl
@Override @Override
public SqlSelection resolveSqlSelection( public SqlSelection resolveSqlSelection(
Expression expression, Expression expression,
JavaType javaTypeDescriptor, JavaType<?> javaTypeDescriptor,
TypeConfiguration typeConfiguration) { TypeConfiguration typeConfiguration) {
final SqlSelection existing; final SqlSelection existing;
if ( sqlSelectionMap == null ) { if ( sqlSelectionMap == null ) {

View File

@ -38,10 +38,12 @@ import org.hibernate.sql.ast.tree.expression.Summarization;
import org.hibernate.sql.ast.tree.expression.TrimSpecification; import org.hibernate.sql.ast.tree.expression.TrimSpecification;
import org.hibernate.sql.ast.tree.expression.UnaryOperation; import org.hibernate.sql.ast.tree.expression.UnaryOperation;
import org.hibernate.sql.ast.tree.from.FromClause; import org.hibernate.sql.ast.tree.from.FromClause;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.QueryPartTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin; import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin; import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
import org.hibernate.sql.ast.tree.from.ValuesTableReference;
import org.hibernate.sql.ast.tree.insert.InsertStatement; import org.hibernate.sql.ast.tree.insert.InsertStatement;
import org.hibernate.sql.ast.tree.predicate.BetweenPredicate; import org.hibernate.sql.ast.tree.predicate.BetweenPredicate;
import org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate; import org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate;
@ -100,7 +102,11 @@ public interface SqlAstWalker {
void visitTableGroupJoin(TableGroupJoin tableGroupJoin); void visitTableGroupJoin(TableGroupJoin tableGroupJoin);
void visitTableReference(TableReference tableReference); void visitNamedTableReference(NamedTableReference tableReference);
void visitValuesTableReference(ValuesTableReference tableReference);
void visitQueryPartTableReference(QueryPartTableReference tableReference);
void visitTableReferenceJoin(TableReferenceJoin tableReferenceJoin); void visitTableReferenceJoin(TableReferenceJoin tableReferenceJoin);

View File

@ -13,10 +13,17 @@ import org.hibernate.sql.ast.tree.Statement;
import org.hibernate.sql.ast.tree.delete.DeleteStatement; import org.hibernate.sql.ast.tree.delete.DeleteStatement;
import org.hibernate.sql.ast.tree.from.FromClause; import org.hibernate.sql.ast.tree.from.FromClause;
import org.hibernate.sql.ast.tree.from.LazyTableGroup; import org.hibernate.sql.ast.tree.from.LazyTableGroup;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.QueryPartTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin; import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin; import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
import org.hibernate.sql.ast.tree.from.ValuesTableReference;
import org.hibernate.sql.ast.tree.insert.InsertStatement; import org.hibernate.sql.ast.tree.insert.InsertStatement;
import org.hibernate.sql.ast.tree.select.QueryGroup;
import org.hibernate.sql.ast.tree.select.QueryPart;
import org.hibernate.sql.ast.tree.select.QuerySpec;
import org.hibernate.sql.ast.tree.select.SelectStatement; import org.hibernate.sql.ast.tree.select.SelectStatement;
import org.hibernate.sql.ast.tree.update.UpdateStatement; import org.hibernate.sql.ast.tree.update.UpdateStatement;
@ -50,7 +57,7 @@ public class SqlTreePrinter {
final SelectStatement selectStatement = (SelectStatement) sqlAstStatement; final SelectStatement selectStatement = (SelectStatement) sqlAstStatement;
logNode( logNode(
"SelectStatement", "SelectStatement",
() -> visitFromClause( selectStatement.getQuerySpec().getFromClause() ) () -> visitQueryPart( selectStatement.getQueryPart() )
); );
} }
else if ( sqlAstStatement instanceof DeleteStatement ) { else if ( sqlAstStatement instanceof DeleteStatement ) {
@ -85,6 +92,30 @@ public class SqlTreePrinter {
} }
} }
private void visitQueryPart(QueryPart queryPart) {
if ( queryPart instanceof QueryGroup ) {
visitQueryGroup( (QueryGroup) queryPart );
}
else {
visitQuerySpec( (QuerySpec) queryPart );
}
}
private void visitQueryGroup(QueryGroup queryGroup) {
logNode(
"QueryGroup: " + queryGroup.getSetOperator(),
() -> {
for ( QueryPart queryPart : queryGroup.getQueryParts() ) {
visitQueryPart( queryPart );
}
}
);
}
private void visitQuerySpec(QuerySpec querySpec) {
visitFromClause( querySpec.getFromClause() );
}
private void visitFromClause(FromClause fromClause) { private void visitFromClause(FromClause fromClause) {
logNode( logNode(
"FromClause", "FromClause",
@ -114,11 +145,30 @@ public class SqlTreePrinter {
} }
return; return;
} }
logWithIndentation( if ( tableGroup.getPrimaryTableReference() instanceof NamedTableReference ) {
"primaryTableReference : %s as %s", logWithIndentation(
tableGroup.getPrimaryTableReference().getTableExpression(), "primaryTableReference : %s as %s",
tableGroup.getPrimaryTableReference().getIdentificationVariable() tableGroup.getPrimaryTableReference().getTableId(),
); tableGroup.getPrimaryTableReference().getIdentificationVariable()
);
}
else {
if ( tableGroup.getPrimaryTableReference() instanceof ValuesTableReference ) {
logWithIndentation(
"primaryTableReference : values (..) as %s",
tableGroup.getPrimaryTableReference().getIdentificationVariable()
);
}
else {
logNode(
"PrimaryTableReference as " + tableGroup.getPrimaryTableReference().getIdentificationVariable(),
() -> {
QueryPart queryPart = ( (QueryPartTableReference) tableGroup.getPrimaryTableReference() ).getQueryPart();
visitQueryPart( queryPart );
}
);
}
}
final List<TableReferenceJoin> tableReferenceJoins = tableGroup.getTableReferenceJoins(); final List<TableReferenceJoin> tableReferenceJoins = tableGroup.getTableReferenceJoins();
if ( ! tableReferenceJoins.isEmpty() ) { if ( ! tableReferenceJoins.isEmpty() ) {

View File

@ -111,6 +111,8 @@ import org.hibernate.sql.ast.tree.expression.TrimSpecification;
import org.hibernate.sql.ast.tree.expression.UnaryOperation; import org.hibernate.sql.ast.tree.expression.UnaryOperation;
import org.hibernate.sql.ast.tree.from.FromClause; import org.hibernate.sql.ast.tree.from.FromClause;
import org.hibernate.sql.ast.tree.from.LazyTableGroup; import org.hibernate.sql.ast.tree.from.LazyTableGroup;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.QueryPartTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin; import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
@ -848,7 +850,7 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
final Stack<Clause> clauseStack = getClauseStack(); final Stack<Clause> clauseStack = getClauseStack();
try { try {
clauseStack.push( Clause.DELETE ); clauseStack.push( Clause.DELETE );
renderTableReference( statement.getTargetTable(), LockMode.NONE ); renderNamedTableReference( statement.getTargetTable(), LockMode.NONE );
} }
finally { finally {
clauseStack.pop(); clauseStack.pop();
@ -873,7 +875,7 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
final Stack<Clause> clauseStack = getClauseStack(); final Stack<Clause> clauseStack = getClauseStack();
try { try {
clauseStack.push( Clause.UPDATE ); clauseStack.push( Clause.UPDATE );
renderTableReference( statement.getTargetTable(), LockMode.NONE ); renderNamedTableReference( statement.getTargetTable(), LockMode.NONE );
} }
finally { finally {
clauseStack.pop(); clauseStack.pop();
@ -3615,8 +3617,31 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
return false; return false;
} }
protected boolean renderTableReference(TableReference tableReference, LockMode lockMode) {
if ( tableReference instanceof NamedTableReference ) {
return renderNamedTableReference( (NamedTableReference) tableReference, lockMode );
}
tableReference.accept( this );
return false;
}
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
protected void renderValuesTableReference(ValuesTableReference tableReference) { protected boolean renderNamedTableReference(NamedTableReference tableReference, LockMode lockMode) {
appendSql( tableReference.getTableExpression() );
registerAffectedTable( tableReference );
final Clause currentClause = clauseStack.getCurrent();
if ( rendersTableReferenceAlias( currentClause ) ) {
final String identificationVariable = tableReference.getIdentificationVariable();
if ( identificationVariable != null ) {
appendSql( WHITESPACE );
appendSql( identificationVariable );
}
}
return false;
}
@Override
public void visitValuesTableReference(ValuesTableReference tableReference) {
append( '(' ); append( '(' );
visitValuesList( tableReference.getValuesList() ); visitValuesList( tableReference.getValuesList() );
append( ')' ); append( ')' );
@ -3635,24 +3660,22 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
} }
} }
@SuppressWarnings("WeakerAccess") @Override
protected boolean renderTableReference(TableReference tableReference, LockMode lockMode) { public void visitQueryPartTableReference(QueryPartTableReference tableReference) {
if ( tableReference instanceof ValuesTableReference ) { tableReference.getQueryPart().accept( this );
renderValuesTableReference( (ValuesTableReference) tableReference ); final String identificationVariable = tableReference.getIdentificationVariable();
} if ( identificationVariable != null ) {
else { append( WHITESPACE );
appendSql( tableReference.getTableExpression() ); append( tableReference.getIdentificationVariable() );
registerAffectedTable( tableReference ); final List<String> columnNames = tableReference.getColumnNames();
final Clause currentClause = clauseStack.getCurrent(); append( '(' );
if ( rendersTableReferenceAlias( currentClause ) ) { append( columnNames.get( 0 ) );
final String identificationVariable = tableReference.getIdentificationVariable(); for ( int i = 1; i < columnNames.size(); i++ ) {
if ( identificationVariable != null ) { append( ',' );
appendSql( WHITESPACE ); append( columnNames.get( i ) );
appendSql( identificationVariable );
}
} }
append( ')' );
} }
return false;
} }
public static boolean rendersTableReferenceAlias(Clause clause) { public static boolean rendersTableReferenceAlias(Clause clause) {
@ -3666,7 +3689,7 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
return true; return true;
} }
protected void registerAffectedTable(TableReference tableReference) { protected void registerAffectedTable(NamedTableReference tableReference) {
registerAffectedTable( tableReference.getTableExpression() ); registerAffectedTable( tableReference.getTableExpression() );
} }
@ -3755,7 +3778,7 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
} }
@Override @Override
public void visitTableReference(TableReference tableReference) { public void visitNamedTableReference(NamedTableReference tableReference) {
// nothing to do... handled via TableGroup#render // nothing to do... handled via TableGroup#render
} }

View File

@ -43,10 +43,12 @@ import org.hibernate.sql.ast.tree.expression.Summarization;
import org.hibernate.sql.ast.tree.expression.TrimSpecification; import org.hibernate.sql.ast.tree.expression.TrimSpecification;
import org.hibernate.sql.ast.tree.expression.UnaryOperation; import org.hibernate.sql.ast.tree.expression.UnaryOperation;
import org.hibernate.sql.ast.tree.from.FromClause; import org.hibernate.sql.ast.tree.from.FromClause;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.QueryPartTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin; import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin; import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
import org.hibernate.sql.ast.tree.from.ValuesTableReference;
import org.hibernate.sql.ast.tree.insert.InsertStatement; import org.hibernate.sql.ast.tree.insert.InsertStatement;
import org.hibernate.sql.ast.tree.insert.Values; import org.hibernate.sql.ast.tree.insert.Values;
import org.hibernate.sql.ast.tree.predicate.BetweenPredicate; import org.hibernate.sql.ast.tree.predicate.BetweenPredicate;
@ -472,6 +474,20 @@ public class AbstractSqlAstWalker implements SqlAstWalker {
} }
@Override @Override
public void visitTableReference(TableReference tableReference) { public void visitNamedTableReference(NamedTableReference tableReference) {
}
@Override
public void visitValuesTableReference(ValuesTableReference tableReference) {
for ( Values values : tableReference.getValuesList() ) {
for ( Expression expression : values.getExpressions() ) {
expression.accept( this );
}
}
}
@Override
public void visitQueryPartTableReference(QueryPartTableReference tableReference) {
tableReference.getQueryPart().accept( this );
} }
} }

View File

@ -19,6 +19,7 @@ import org.hibernate.sql.ast.tree.expression.Duration;
import org.hibernate.sql.ast.tree.expression.DurationUnit; import org.hibernate.sql.ast.tree.expression.DurationUnit;
import org.hibernate.sql.ast.tree.expression.EntityTypeLiteral; import org.hibernate.sql.ast.tree.expression.EntityTypeLiteral;
import org.hibernate.sql.ast.tree.expression.Every; import org.hibernate.sql.ast.tree.expression.Every;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.ExtractUnit; import org.hibernate.sql.ast.tree.expression.ExtractUnit;
import org.hibernate.sql.ast.tree.expression.Format; import org.hibernate.sql.ast.tree.expression.Format;
import org.hibernate.sql.ast.tree.expression.FunctionExpression; import org.hibernate.sql.ast.tree.expression.FunctionExpression;
@ -33,11 +34,14 @@ import org.hibernate.sql.ast.tree.expression.Star;
import org.hibernate.sql.ast.tree.expression.Summarization; import org.hibernate.sql.ast.tree.expression.Summarization;
import org.hibernate.sql.ast.tree.expression.TrimSpecification; import org.hibernate.sql.ast.tree.expression.TrimSpecification;
import org.hibernate.sql.ast.tree.from.FromClause; import org.hibernate.sql.ast.tree.from.FromClause;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.QueryPartTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin; import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin; import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
import org.hibernate.sql.ast.tree.from.ValuesTableReference;
import org.hibernate.sql.ast.tree.insert.InsertStatement; import org.hibernate.sql.ast.tree.insert.InsertStatement;
import org.hibernate.sql.ast.tree.insert.Values;
import org.hibernate.sql.ast.tree.predicate.ExistsPredicate; import org.hibernate.sql.ast.tree.predicate.ExistsPredicate;
import org.hibernate.sql.ast.tree.predicate.FilterPredicate; import org.hibernate.sql.ast.tree.predicate.FilterPredicate;
import org.hibernate.sql.ast.tree.predicate.InListPredicate; import org.hibernate.sql.ast.tree.predicate.InListPredicate;
@ -230,7 +234,15 @@ public class AggregateFunctionChecker extends AbstractSqlAstWalker {
} }
@Override @Override
public void visitTableReference(TableReference tableReference) { public void visitNamedTableReference(NamedTableReference tableReference) {
}
@Override
public void visitValuesTableReference(ValuesTableReference tableReference) {
}
@Override
public void visitQueryPartTableReference(QueryPartTableReference tableReference) {
} }
@Override @Override

View File

@ -11,7 +11,12 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.hibernate.sql.ast.tree.SqlAstNode; import org.hibernate.sql.ast.tree.SqlAstNode;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.QueryPartTableReference;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.ValuesTableReference;
import org.hibernate.sql.ast.tree.insert.Values;
/** /**
* A simple walker that checks for aggregate functions. * A simple walker that checks for aggregate functions.
@ -29,7 +34,17 @@ public class AliasCollector extends AbstractSqlAstWalker {
} }
@Override @Override
public void visitTableReference(TableReference tableReference) { public void visitNamedTableReference(NamedTableReference tableReference) {
tableReferenceMap.put( tableReference.getIdentificationVariable(), tableReference );
}
@Override
public void visitValuesTableReference(ValuesTableReference tableReference) {
tableReferenceMap.put( tableReference.getIdentificationVariable(), tableReference );
}
@Override
public void visitQueryPartTableReference(QueryPartTableReference tableReference) {
tableReferenceMap.put( tableReference.getIdentificationVariable(), tableReference ); tableReferenceMap.put( tableReference.getIdentificationVariable(), tableReference );
} }
} }

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.sql.ast.spi; package org.hibernate.sql.ast.spi;
import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
import org.hibernate.metamodel.mapping.SelectableMapping; import org.hibernate.metamodel.mapping.SelectableMapping;
@ -50,10 +51,8 @@ public interface SqlExpressionResolver {
static String createColumnReferenceKey(TableReference tableReference, String columnExpression) { static String createColumnReferenceKey(TableReference tableReference, String columnExpression) {
assert tableReference != null : "tableReference expected to be non-null"; assert tableReference != null : "tableReference expected to be non-null";
assert columnExpression != null : "columnExpression expected to be non-null"; assert columnExpression != null : "columnExpression expected to be non-null";
assert tableReference.getIdentificationVariable() != null : "tableReference#identificationVariable expected to be non-null";
final String qualifier = tableReference.getIdentificationVariable() == null final String qualifier = tableReference.getIdentificationVariable();
? tableReference.getTableExpression()
: tableReference.getIdentificationVariable();
return qualifier + columnExpression; return qualifier + columnExpression;
} }
@ -61,8 +60,8 @@ public interface SqlExpressionResolver {
* Convenience form for creating a key from TableReference and SelectableMapping * Convenience form for creating a key from TableReference and SelectableMapping
*/ */
static String createColumnReferenceKey(TableReference tableReference, SelectableMapping selectable) { static String createColumnReferenceKey(TableReference tableReference, SelectableMapping selectable) {
assert selectable.getContainingTableExpression().equals( tableReference.getTableExpression() ) assert Objects.equals( selectable.getContainingTableExpression(), tableReference.getTableId() )
: String.format( ROOT, "Expecting tables to match between TableReference (%s) and SelectableMapping (%s)", tableReference.getTableExpression(), selectable.getContainingTableExpression() ); : String.format( ROOT, "Expecting tables to match between TableReference (%s) and SelectableMapping (%s)", tableReference.getTableId(), selectable.getContainingTableExpression() );
return createColumnReferenceKey( tableReference, selectable.getSelectionExpression() ); return createColumnReferenceKey( tableReference, selectable.getSelectionExpression() );
} }
@ -77,6 +76,6 @@ public interface SqlExpressionResolver {
*/ */
SqlSelection resolveSqlSelection( SqlSelection resolveSqlSelection(
Expression expression, Expression expression,
JavaType javaTypeDescriptor, JavaType<?> javaTypeDescriptor,
TypeConfiguration typeConfiguration); TypeConfiguration typeConfiguration);
} }

View File

@ -13,30 +13,30 @@ import java.util.Map;
import org.hibernate.sql.ast.tree.cte.CteStatement; import org.hibernate.sql.ast.tree.cte.CteStatement;
import org.hibernate.sql.ast.tree.expression.ColumnReference; import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.NamedTableReference;
/** /**
* @author Christian Beikov * @author Christian Beikov
*/ */
public abstract class AbstractMutationStatement extends AbstractStatement implements MutationStatement { public abstract class AbstractMutationStatement extends AbstractStatement implements MutationStatement {
private final TableReference targetTable; private final NamedTableReference targetTable;
private final List<ColumnReference> returningColumns; private final List<ColumnReference> returningColumns;
public AbstractMutationStatement(TableReference targetTable) { public AbstractMutationStatement(NamedTableReference targetTable) {
super( new LinkedHashMap<>() ); super( new LinkedHashMap<>() );
this.targetTable = targetTable; this.targetTable = targetTable;
this.returningColumns = Collections.emptyList(); this.returningColumns = Collections.emptyList();
} }
public AbstractMutationStatement(Map<String, CteStatement> cteStatements, TableReference targetTable, List<ColumnReference> returningColumns) { public AbstractMutationStatement(Map<String, CteStatement> cteStatements, NamedTableReference targetTable, List<ColumnReference> returningColumns) {
super( cteStatements ); super( cteStatements );
this.targetTable = targetTable; this.targetTable = targetTable;
this.returningColumns = returningColumns; this.returningColumns = returningColumns;
} }
@Override @Override
public TableReference getTargetTable() { public NamedTableReference getTargetTable() {
return targetTable; return targetTable;
} }

View File

@ -9,7 +9,7 @@ package org.hibernate.sql.ast.tree;
import java.util.List; import java.util.List;
import org.hibernate.sql.ast.tree.expression.ColumnReference; import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.NamedTableReference;
/** /**
* Specialization of Statement for mutation (DML) statements * Specialization of Statement for mutation (DML) statements
@ -17,6 +17,6 @@ import org.hibernate.sql.ast.tree.from.TableReference;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public interface MutationStatement extends Statement { public interface MutationStatement extends Statement {
TableReference getTargetTable(); NamedTableReference getTargetTable();
List<ColumnReference> getReturningColumns(); List<ColumnReference> getReturningColumns();
} }

View File

@ -7,24 +7,10 @@
package org.hibernate.sql.ast.tree.cte; package org.hibernate.sql.ast.tree.cte;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.mapping.Bindable;
import org.hibernate.metamodel.mapping.EntityMappingType; import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.Clause;
import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.from.StandardTableGroup;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.select.QuerySpec;
import org.hibernate.sql.exec.internal.JdbcParameterImpl;
import org.hibernate.sql.exec.spi.ExecutionContext;
import org.hibernate.sql.exec.spi.JdbcParameterBindings;
import org.hibernate.sql.results.internal.SqlSelectionImpl;
/** /**
* Describes the table definition for the CTE - its name amd its columns * Describes the table definition for the CTE - its name amd its columns
@ -67,120 +53,4 @@ public class CteTable {
return new CteTable( name, cteColumns, sessionFactory ); return new CteTable( name, cteColumns, sessionFactory );
} }
public QuerySpec createCteDefinition(
List<?> matchingIds,
Bindable bindable,
JdbcParameterBindings jdbcParameterBindings,
ExecutionContext executionContext) {
final QuerySpec querySpec = new QuerySpec( false );
final TableReference tableValueConstructorReference = createCteDefinitionTableValueCtor(
matchingIds,
bindable,
jdbcParameterBindings,
executionContext
);
final StandardTableGroup tableValueCtorGroup = new StandardTableGroup(
true,
new NavigablePath( "cte" ),
null,
null,
tableValueConstructorReference,
null,
sessionFactory
);
querySpec.getFromClause().addRoot( tableValueCtorGroup );
applySelections( querySpec, tableValueConstructorReference );
return querySpec;
}
private TableReference createCteDefinitionTableValueCtor(
List<?> matchingValues,
Bindable bindable,
JdbcParameterBindings jdbcParameterBindings,
ExecutionContext executionContext) {
// use `DerivedTable` as the TableValueConstructor
// so its `#expression` would be something like `values ( (a1, b1), (a2, b2), ... )`
final int numberOfColumns = getCteColumns().size();
final StringBuilder tableValueCtorExpressionBuffer = new StringBuilder( "values(" );
final List<JdbcParameter> jdbcParameters = Arrays.asList( new JdbcParameterImpl[numberOfColumns] );
String rowSeparator = "";
for ( Object matchingId : matchingValues ) {
tableValueCtorExpressionBuffer.append( rowSeparator );
char separator = '(';
for ( int i = 0; i < numberOfColumns; i++ ) {
tableValueCtorExpressionBuffer.append( separator );
tableValueCtorExpressionBuffer.append( '?' );
separator = ',';
jdbcParameters.set( i, new JdbcParameterImpl( cteColumns.get( i ).getJdbcMapping() ) );
}
tableValueCtorExpressionBuffer.append( ')' );
jdbcParameterBindings.registerParametersForEachJdbcValue(
matchingId,
Clause.IRRELEVANT,
bindable,
jdbcParameters,
executionContext.getSession()
);
rowSeparator = ", ";
}
tableValueCtorExpressionBuffer.append( ')' );
return new TableReference(
tableValueCtorExpressionBuffer.toString(),
cteName,
false,
sessionFactory
);
}
public QuerySpec createCteSubQuery(@SuppressWarnings("unused") ExecutionContext executionContext) {
final QuerySpec querySpec = new QuerySpec( false );
final TableReference cteTableReference = new TableReference(
getTableExpression(),
null,
false,
sessionFactory
);
final CteTableGroup cteTableGroup = new CteTableGroup( cteTableReference );
querySpec.getFromClause().addRoot( cteTableGroup );
applySelections( querySpec, cteTableReference );
return querySpec;
}
private void applySelections(QuerySpec querySpec, TableReference tableReference) {
for ( int i = 0; i < cteColumns.size(); i++ ) {
final CteColumn cteColumn = cteColumns.get( i );
querySpec.getSelectClause().addSqlSelection(
new SqlSelectionImpl(
i + 1,
i,
// todo (6.0) : handle read/write transformers for the CTE columns
new ColumnReference(
tableReference,
cteColumn.getColumnExpression(),
false,
null,
null,
cteColumn.getJdbcMapping(),
sessionFactory
)
)
);
}
}
} }

View File

@ -13,23 +13,24 @@ import java.util.function.Consumer;
import org.hibernate.metamodel.mapping.ModelPart; import org.hibernate.metamodel.mapping.ModelPart;
import org.hibernate.metamodel.mapping.ModelPartContainer; import org.hibernate.metamodel.mapping.ModelPartContainer;
import org.hibernate.query.NavigablePath; import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin; import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin; import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
/** /**
* Wraps a {@link TableReference} representing the CTE and adapts it to * Wraps a {@link NamedTableReference} representing the CTE and adapts it to
* {@link TableGroup} for use in SQL AST * {@link TableGroup} for use in SQL AST
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class CteTableGroup implements TableGroup { public class CteTableGroup implements TableGroup {
private final NavigablePath navigablePath; private final NavigablePath navigablePath;
private final TableReference cteTableReference; private final NamedTableReference cteTableReference;
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
public CteTableGroup(TableReference cteTableReference) { public CteTableGroup(NamedTableReference cteTableReference) {
this.navigablePath = new NavigablePath( cteTableReference.getTableExpression() ); this.navigablePath = new NavigablePath( cteTableReference.getTableExpression() );
this.cteTableReference = cteTableReference; this.cteTableReference = cteTableReference;
} }

View File

@ -15,7 +15,7 @@ import org.hibernate.sql.ast.spi.SqlAstHelper;
import org.hibernate.sql.ast.tree.AbstractMutationStatement; import org.hibernate.sql.ast.tree.AbstractMutationStatement;
import org.hibernate.sql.ast.tree.cte.CteStatement; import org.hibernate.sql.ast.tree.cte.CteStatement;
import org.hibernate.sql.ast.tree.expression.ColumnReference; import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.predicate.Junction; import org.hibernate.sql.ast.tree.predicate.Junction;
import org.hibernate.sql.ast.tree.predicate.Predicate; import org.hibernate.sql.ast.tree.predicate.Predicate;
@ -27,13 +27,13 @@ public class DeleteStatement extends AbstractMutationStatement {
public static final String DEFAULT_ALIAS = "to_delete_"; public static final String DEFAULT_ALIAS = "to_delete_";
private final Predicate restriction; private final Predicate restriction;
public DeleteStatement(TableReference targetTable, Predicate restriction) { public DeleteStatement(NamedTableReference targetTable, Predicate restriction) {
super( targetTable ); super( targetTable );
this.restriction = restriction; this.restriction = restriction;
} }
public DeleteStatement( public DeleteStatement(
TableReference targetTable, NamedTableReference targetTable,
Predicate restriction, Predicate restriction,
List<ColumnReference> returningColumns) { List<ColumnReference> returningColumns) {
super( new LinkedHashMap<>(), targetTable, returningColumns ); super( new LinkedHashMap<>(), targetTable, returningColumns );
@ -43,7 +43,7 @@ public class DeleteStatement extends AbstractMutationStatement {
public DeleteStatement( public DeleteStatement(
boolean withRecursive, boolean withRecursive,
Map<String, CteStatement> cteStatements, Map<String, CteStatement> cteStatements,
TableReference targetTable, NamedTableReference targetTable,
Predicate restriction, Predicate restriction,
List<ColumnReference> returningColumns) { List<ColumnReference> returningColumns) {
super( cteStatements, targetTable, returningColumns ); super( cteStatements, targetTable, returningColumns );
@ -56,10 +56,10 @@ public class DeleteStatement extends AbstractMutationStatement {
} }
public static class DeleteStatementBuilder { public static class DeleteStatementBuilder {
private final TableReference targetTable; private final NamedTableReference targetTable;
private Predicate restriction; private Predicate restriction;
public DeleteStatementBuilder(TableReference targetTable) { public DeleteStatementBuilder(NamedTableReference targetTable) {
this.targetTable = targetTable; this.targetTable = targetTable;
} }

View File

@ -0,0 +1,48 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.sql.ast.tree.from;
import java.util.Objects;
public abstract class AbstractTableReference implements TableReference {
protected final String identificationVariable;
protected final boolean isOptional;
public AbstractTableReference(String identificationVariable, boolean isOptional) {
assert identificationVariable != null;
this.identificationVariable = identificationVariable;
this.isOptional = isOptional;
}
@Override
public String getIdentificationVariable() {
return identificationVariable;
}
@Override
public boolean isOptional() {
return isOptional;
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
TableReference that = (TableReference) o;
return Objects.equals( identificationVariable, that.getIdentificationVariable() );
}
@Override
public int hashCode() {
return Objects.hash( identificationVariable );
}
}

View File

@ -112,7 +112,7 @@ public class CorrelatedTableGroup extends AbstractTableGroup {
@Override @Override
public void applyAffectedTableNames(Consumer<String> nameCollector) { public void applyAffectedTableNames(Consumer<String> nameCollector) {
nameCollector.accept( getPrimaryTableReference().getTableExpression() ); getPrimaryTableReference().applyAffectedTableNames( nameCollector );
} }
@Override @Override

View File

@ -0,0 +1,55 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.sql.ast.tree.from;
import java.util.List;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.NavigablePath;
/**
* @author Christian Beikov
*/
public abstract class DerivedTableReference extends AbstractTableReference {
private final List<String> columnNames;
public DerivedTableReference(
String identificationVariable,
List<String> columnNames,
SessionFactoryImplementor sessionFactory) {
super( identificationVariable, false );
this.columnNames = columnNames;
}
@Override
public String getTableId() {
return null;
}
public List<String> getColumnNames() {
return columnNames;
}
@Override
public TableReference resolveTableReference(
NavigablePath navigablePath,
String tableExpression,
boolean allowFkOptimization) {
throw new IllegalStateException( "Could not resolve binding for table `" + tableExpression + "`" );
}
@Override
public TableReference getTableReference(
NavigablePath navigablePath,
String tableExpression,
boolean allowFkOptimization,
boolean resolve) {
return null;
}
}

View File

@ -44,17 +44,12 @@ public class FromClause implements SqlAstNode {
} }
public void visitTableGroups(Consumer<TableGroup> action) { public void visitTableGroups(Consumer<TableGroup> action) {
for ( int i = 0; i < roots.size(); i++ ) { queryTableGroups(
visitTableGroups( roots.get( i ), action ); tableGroup -> {
} action.accept( tableGroup );
} return null;
}
private void visitTableGroups(TableGroup tableGroup, Consumer<TableGroup> action) { );
action.accept( tableGroup );
final List<TableGroupJoin> tableGroupJoins = tableGroup.getTableGroupJoins();
for ( int i = 0; i < tableGroupJoins.size(); i++ ) {
visitTableGroups( tableGroupJoins.get( i ).getJoinedGroup(), action );
}
} }
public <T> T queryTableGroups(Function<TableGroup, T> action) { public <T> T queryTableGroups(Function<TableGroup, T> action) {
@ -79,28 +74,28 @@ public class FromClause implements SqlAstNode {
return nestedResult; return nestedResult;
} }
} }
final List<TableGroupJoin> nestedTableGroupJoins = tableGroup.getNestedTableGroupJoins();
for ( int i = 0; i < nestedTableGroupJoins.size(); i++ ) {
final T nestedResult = queryTableGroups( nestedTableGroupJoins.get( i ).getJoinedGroup(), action );
if ( nestedResult != null ) {
return nestedResult;
}
}
return null; return null;
} }
public void visitTableJoins(Consumer<TableJoin> action) { public void visitTableJoins(Consumer<TableJoin> action) {
for ( int i = 0; i < roots.size(); i++ ) { queryTableJoins(
visitTableJoins( roots.get( i ), action ); tableGroupJoin -> {
} action.accept( tableGroupJoin );
} return null;
}
private void visitTableJoins(TableGroup tableGroup, Consumer<TableJoin> action) { );
tableGroup.getTableReferenceJoins().forEach( action );
final List<TableGroupJoin> tableGroupJoins = tableGroup.getTableGroupJoins();
for ( int i = 0; i < tableGroupJoins.size(); i++ ) {
final TableGroupJoin tableGroupJoin = tableGroupJoins.get( i );
action.accept( tableGroupJoin );
visitTableJoins( tableGroupJoin.getJoinedGroup(), action );
}
} }
public <T> T queryTableJoins(Function<TableJoin, T> action) { public <T> T queryTableJoins(Function<TableJoin, T> action) {
for ( int i = 0; i < roots.size(); i++ ) { for ( int i = 0; i < roots.size(); i++ ) {
final T result = queryTableJoins( roots.get( i ), action ); T result = queryTableJoins( roots.get( i ), action );
if ( result != null ) { if ( result != null ) {
return result; return result;
} }
@ -116,7 +111,14 @@ public class FromClause implements SqlAstNode {
} }
} }
final List<TableGroupJoin> tableGroupJoins = tableGroup.getTableGroupJoins(); final T result = queryTableJoins( tableGroup.getTableGroupJoins(), action );
if ( result != null ) {
return result;
}
return queryTableJoins( tableGroup.getNestedTableGroupJoins(), action );
}
private <T> T queryTableJoins(List<TableGroupJoin> tableGroupJoins, Function<TableJoin, T> action) {
for ( int i = 0; i < tableGroupJoins.size(); i++ ) { for ( int i = 0; i < tableGroupJoins.size(); i++ ) {
final TableGroupJoin tableGroupJoin = tableGroupJoins.get( i ); final TableGroupJoin tableGroupJoin = tableGroupJoins.get( i );
T result = action.apply( tableGroupJoin ); T result = action.apply( tableGroupJoin );
@ -132,18 +134,12 @@ public class FromClause implements SqlAstNode {
} }
public void visitTableGroupJoins(Consumer<TableGroupJoin> action) { public void visitTableGroupJoins(Consumer<TableGroupJoin> action) {
for ( int i = 0; i < roots.size(); i++ ) { queryTableGroupJoins(
visitTableGroupJoins( roots.get( i ), action ); tableGroupJoin -> {
} action.accept( tableGroupJoin );
} return null;
}
private void visitTableGroupJoins(TableGroup tableGroup, Consumer<TableGroupJoin> action) { );
final List<TableGroupJoin> tableGroupJoins = tableGroup.getTableGroupJoins();
for ( int i = 0; i < tableGroupJoins.size(); i++ ) {
final TableGroupJoin tableGroupJoin = tableGroupJoins.get( i );
action.accept( tableGroupJoin );
visitTableGroupJoins( tableGroupJoin.getJoinedGroup(), action );
}
} }
public <T> T queryTableGroupJoins(Function<TableGroupJoin, T> action) { public <T> T queryTableGroupJoins(Function<TableGroupJoin, T> action) {
@ -157,7 +153,14 @@ public class FromClause implements SqlAstNode {
} }
private <T> T queryTableGroupJoins(TableGroup tableGroup, Function<TableGroupJoin, T> action) { private <T> T queryTableGroupJoins(TableGroup tableGroup, Function<TableGroupJoin, T> action) {
final List<TableGroupJoin> tableGroupJoins = tableGroup.getTableGroupJoins(); final T result = queryTableGroupJoins( tableGroup.getTableGroupJoins(), action );
if ( result != null ) {
return result;
}
return queryTableGroupJoins( tableGroup.getNestedTableGroupJoins(), action );
}
private <T> T queryTableGroupJoins(List<TableGroupJoin> tableGroupJoins, Function<TableGroupJoin, T> action) {
for ( int i = 0; i < tableGroupJoins.size(); i++ ) { for ( int i = 0; i < tableGroupJoins.size(); i++ ) {
final TableGroupJoin tableGroupJoin = tableGroupJoins.get( i ); final TableGroupJoin tableGroupJoin = tableGroupJoins.get( i );
T result = action.apply( tableGroupJoin ); T result = action.apply( tableGroupJoin );
@ -172,6 +175,55 @@ public class FromClause implements SqlAstNode {
return null; return null;
} }
public void visitTableReferences(Consumer<TableReference> action) {
queryTableReferences(
tableGroupJoin -> {
action.accept( tableGroupJoin );
return null;
}
);
}
public <T> T queryTableReferences(Function<TableReference, T> action) {
for ( int i = 0; i < roots.size(); i++ ) {
T result = queryTableReferences( roots.get( i ), action );
if ( result != null ) {
return result;
}
}
return null;
}
private <T> T queryTableReferences(TableGroup tableGroup, Function<TableReference, T> action) {
final T result = action.apply( tableGroup.getPrimaryTableReference() );
if ( result != null ) {
return result;
}
for ( TableReferenceJoin tableReferenceJoin : tableGroup.getTableReferenceJoins() ) {
final T nestedResult = action.apply( tableReferenceJoin.getJoinedTableReference() );
if ( nestedResult != null ) {
return nestedResult;
}
}
final T nestedResult = queryTableReferences( tableGroup.getTableGroupJoins(), action );
if ( nestedResult != null ) {
return nestedResult;
}
return queryTableReferences( tableGroup.getNestedTableGroupJoins(), action );
}
private <T> T queryTableReferences(List<TableGroupJoin> tableGroupJoins, Function<TableReference, T> action) {
for ( int i = 0; i < tableGroupJoins.size(); i++ ) {
final TableGroupJoin tableGroupJoin = tableGroupJoins.get( i );
T result = queryTableReferences( tableGroupJoin.getJoinedGroup(), action );
if ( result != null ) {
return result;
}
}
return null;
}
@Override @Override
public void accept(SqlAstWalker sqlTreeWalker) { public void accept(SqlAstWalker sqlTreeWalker) {
sqlTreeWalker.visitFromClause( this ); sqlTreeWalker.visitFromClause( this );

View File

@ -23,12 +23,12 @@ import org.hibernate.query.NavigablePath;
public class MutatingTableReferenceGroupWrapper implements VirtualTableGroup { public class MutatingTableReferenceGroupWrapper implements VirtualTableGroup {
private final NavigablePath navigablePath; private final NavigablePath navigablePath;
private final ModelPartContainer modelPart; private final ModelPartContainer modelPart;
private final TableReference mutatingTableReference; private final NamedTableReference mutatingTableReference;
public MutatingTableReferenceGroupWrapper( public MutatingTableReferenceGroupWrapper(
NavigablePath navigablePath, NavigablePath navigablePath,
ModelPartContainer modelPart, ModelPartContainer modelPart,
TableReference mutatingTableReference) { NamedTableReference mutatingTableReference) {
this.navigablePath = navigablePath; this.navigablePath = navigablePath;
this.modelPart = modelPart; this.modelPart = modelPart;
this.mutatingTableReference = mutatingTableReference; this.mutatingTableReference = mutatingTableReference;

View File

@ -0,0 +1,104 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.sql.ast.tree.from;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.SqlAstWalker;
/**
* Represents a reference to a "named" table in a query's from clause.
*
* @author Steve Ebersole
*/
public class NamedTableReference extends AbstractTableReference {
private final String tableExpression;
private String prunedTableExpression;
public NamedTableReference(
String tableExpression,
String identificationVariable,
boolean isOptional,
SessionFactoryImplementor sessionFactory) {
super( identificationVariable, isOptional );
assert tableExpression != null;
this.tableExpression = tableExpression;
}
public String getTableExpression() {
return prunedTableExpression == null ? tableExpression : prunedTableExpression;
}
@Override
public String getTableId() {
return getTableExpression();
}
public void setPrunedTableExpression(String prunedTableExpression) {
this.prunedTableExpression = prunedTableExpression;
}
@Override
public void accept(SqlAstWalker sqlTreeWalker) {
sqlTreeWalker.visitNamedTableReference( this );
}
@Override
public void applyAffectedTableNames(Consumer<String> nameCollector) {
nameCollector.accept( getTableExpression() );
}
@Override
public List<String> getAffectedTableNames() {
return Collections.singletonList( getTableExpression() );
}
@Override
public boolean containsAffectedTableName(String requestedName) {
return getTableExpression().equals( requestedName );
}
@Override
public Boolean visitAffectedTableNames(Function<String, Boolean> nameCollector) {
return nameCollector.apply( getTableExpression() );
}
@Override
public TableReference resolveTableReference(
NavigablePath navigablePath,
String tableExpression,
boolean allowFkOptimization) {
if ( tableExpression.equals( getTableExpression() ) ) {
return this;
}
throw new IllegalStateException( "Could not resolve binding for table `" + tableExpression + "`" );
}
@Override
public TableReference getTableReference(
NavigablePath navigablePath,
String tableExpression,
boolean allowFkOptimization,
boolean resolve) {
if ( this.tableExpression.equals( tableExpression ) ) {
return this;
}
return null;
}
@Override
public String toString() {
return getTableExpression() + "(" + getIdentificationVariable() + ')';
}
}

View File

@ -0,0 +1,49 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.sql.ast.tree.from;
import java.util.List;
import java.util.function.Function;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.sql.ast.SqlAstWalker;
import org.hibernate.sql.ast.tree.select.QueryPart;
/**
* @author Christian Beikov
*/
public class QueryPartTableReference extends DerivedTableReference {
private final QueryPart queryPart;
public QueryPartTableReference(
QueryPart queryPart,
String identificationVariable,
List<String> columnNames,
SessionFactoryImplementor sessionFactory) {
super( identificationVariable, columnNames, sessionFactory );
this.queryPart = queryPart;
}
public QueryPart getQueryPart() {
return queryPart;
}
@Override
public void accept(SqlAstWalker sqlTreeWalker) {
sqlTreeWalker.visitQueryPartTableReference( this );
}
@Override
public Boolean visitAffectedTableNames(Function<String, Boolean> nameCollector) {
final Function<TableReference, Boolean> tableReferenceBooleanFunction =
tableReference -> tableReference.visitAffectedTableNames( nameCollector );
return queryPart.queryQuerySpecs(
querySpec -> querySpec.getFromClause().queryTableReferences( tableReferenceBooleanFunction )
);
}
}

View File

@ -45,7 +45,7 @@ public class StandardTableGroup extends AbstractTableGroup {
this.tableReferenceJoinCreator = null; this.tableReferenceJoinCreator = null;
this.tableReferenceJoinNameChecker = s -> { this.tableReferenceJoinNameChecker = s -> {
for ( int i = 0; i < tableJoins.size(); i++ ) { for ( int i = 0; i < tableJoins.size(); i++ ) {
if ( tableJoins.get( i ).getJoinedTableReference().getTableExpression().equals( s ) ) { if ( tableJoins.get( i ).getJoinedTableReference().containsAffectedTableName( s ) ) {
return true; return true;
} }
} }
@ -97,9 +97,9 @@ public class StandardTableGroup extends AbstractTableGroup {
@Override @Override
public void applyAffectedTableNames(Consumer<String> nameCollector) { public void applyAffectedTableNames(Consumer<String> nameCollector) {
// todo (6.0) : if we implement dynamic TableReference creation, this still needs to return the expressions for all mapped tables not just the ones with a TableReference at this time // todo (6.0) : if we implement dynamic TableReference creation, this still needs to return the expressions for all mapped tables not just the ones with a TableReference at this time
nameCollector.accept( getPrimaryTableReference().getTableExpression() ); getPrimaryTableReference().applyAffectedTableNames( nameCollector );
for ( TableReferenceJoin tableReferenceJoin : tableJoins ) { for ( TableReferenceJoin tableReferenceJoin : tableJoins ) {
nameCollector.accept( tableReferenceJoin.getJoinedTableReference().getTableExpression() ); tableReferenceJoin.getJoinedTableReference().applyAffectedTableNames( nameCollector );
} }
} }

View File

@ -6,9 +6,11 @@
*/ */
package org.hibernate.sql.ast.tree.from; package org.hibernate.sql.ast.tree.from;
import java.util.Objects; import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.NavigablePath; import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.SqlAstWalker; import org.hibernate.sql.ast.SqlAstWalker;
import org.hibernate.sql.ast.tree.SqlAstNode; import org.hibernate.sql.ast.tree.SqlAstNode;
@ -18,87 +20,56 @@ import org.hibernate.sql.ast.tree.SqlAstNode;
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class TableReference implements SqlAstNode, ColumnReferenceQualifier { public interface TableReference extends SqlAstNode, ColumnReferenceQualifier {
private final String tableExpression;
private final String identificationVariable;
private final boolean isOptional; String getIdentificationVariable();
private String prunedTableExpression;
public TableReference( /**
String tableExpression, * An identifier for the table reference. May be null if this is not a named table reference.
String identificationVariable, */
boolean isOptional, String getTableId();
SessionFactoryImplementor sessionFactory) {
this.tableExpression = tableExpression;
this.identificationVariable = identificationVariable;
this.isOptional = isOptional;
}
public String getTableExpression() { boolean isOptional();
return prunedTableExpression == null ? tableExpression : prunedTableExpression;
}
public String getIdentificationVariable() {
return identificationVariable;
}
public boolean isOptional() {
return isOptional;
}
public void setPrunedTableExpression(String prunedTableExpression) {
this.prunedTableExpression = prunedTableExpression;
}
@Override @Override
public void accept(SqlAstWalker sqlTreeWalker) { void accept(SqlAstWalker sqlTreeWalker);
sqlTreeWalker.visitTableReference( this );
default void applyAffectedTableNames(Consumer<String> nameCollector) {
visitAffectedTableNames(
name -> {
nameCollector.accept( name );
return null;
}
);
} }
default List<String> getAffectedTableNames() {
final List<String> affectedTableNames = new ArrayList<>();
visitAffectedTableNames(
name -> {
affectedTableNames.add( name );
return null;
}
);
return affectedTableNames;
}
default boolean containsAffectedTableName(String requestedName) {
return visitAffectedTableNames( requestedName::equals );
}
Boolean visitAffectedTableNames(Function<String, Boolean> nameCollector);
@Override @Override
public TableReference resolveTableReference( TableReference resolveTableReference(
NavigablePath navigablePath, NavigablePath navigablePath,
String tableExpression, String tableExpression,
boolean allowFkOptimization) { boolean allowFkOptimization);
if ( tableExpression.equals( getTableExpression() ) ) {
return this;
}
throw new IllegalStateException( "Could not resolve binding for table `" + tableExpression + "`" );
}
@Override @Override
public TableReference getTableReference( TableReference getTableReference(
NavigablePath navigablePath, NavigablePath navigablePath,
String tableExpression, String tableExpression,
boolean allowFkOptimization, boolean allowFkOptimization,
boolean resolve) { boolean resolve);
if ( this.tableExpression.equals( tableExpression ) ) {
return this;
}
return null;
}
@Override
public String toString() {
return getTableExpression() + "(" + getIdentificationVariable() + ')';
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
TableReference that = (TableReference) o;
return Objects.equals( identificationVariable, that.identificationVariable );
}
@Override
public int hashCode() {
return Objects.hash( identificationVariable );
}
} }

View File

@ -20,10 +20,10 @@ import org.hibernate.sql.ast.tree.predicate.PredicateContainer;
*/ */
public class TableReferenceJoin implements TableJoin, PredicateContainer { public class TableReferenceJoin implements TableJoin, PredicateContainer {
private final SqlAstJoinType sqlAstJoinType; private final SqlAstJoinType sqlAstJoinType;
private final TableReference joinedTableBinding; private final NamedTableReference joinedTableBinding;
private Predicate predicate; private Predicate predicate;
public TableReferenceJoin(SqlAstJoinType sqlAstJoinType, TableReference joinedTableBinding, Predicate predicate) { public TableReferenceJoin(SqlAstJoinType sqlAstJoinType, NamedTableReference joinedTableBinding, Predicate predicate) {
this.sqlAstJoinType = sqlAstJoinType == null ? SqlAstJoinType.LEFT : sqlAstJoinType; this.sqlAstJoinType = sqlAstJoinType == null ? SqlAstJoinType.LEFT : sqlAstJoinType;
this.joinedTableBinding = joinedTableBinding; this.joinedTableBinding = joinedTableBinding;
this.predicate = predicate; this.predicate = predicate;
@ -40,7 +40,7 @@ public class TableReferenceJoin implements TableJoin, PredicateContainer {
return sqlAstJoinType; return sqlAstJoinType;
} }
public TableReference getJoinedTableReference() { public NamedTableReference getJoinedTableReference() {
return joinedTableBinding; return joinedTableBinding;
} }

View File

@ -27,12 +27,12 @@ public class UnionTableGroup implements VirtualTableGroup {
private final UnionSubclassEntityPersister modelPart; private final UnionSubclassEntityPersister modelPart;
private final String sourceAlias; private final String sourceAlias;
private final TableReference tableReference; private final UnionTableReference tableReference;
public UnionTableGroup( public UnionTableGroup(
boolean canUseInnerJoins, boolean canUseInnerJoins,
NavigablePath navigablePath, NavigablePath navigablePath,
TableReference tableReference, UnionTableReference tableReference,
UnionSubclassEntityPersister modelPart, UnionSubclassEntityPersister modelPart,
String sourceAlias) { String sourceAlias) {
this.canUseInnerJoins = canUseInnerJoins; this.canUseInnerJoins = canUseInnerJoins;
@ -124,7 +124,7 @@ public class UnionTableGroup implements VirtualTableGroup {
} }
@Override @Override
public TableReference getPrimaryTableReference() { public UnionTableReference getPrimaryTableReference() {
return tableReference; return tableReference;
} }

View File

@ -12,7 +12,7 @@ import org.hibernate.query.NavigablePath;
/** /**
* @author Andrea Boriero * @author Andrea Boriero
*/ */
public class UnionTableReference extends TableReference { public class UnionTableReference extends NamedTableReference {
private final String[] subclassTableSpaceExpressions; private final String[] subclassTableSpaceExpressions;
public UnionTableReference( public UnionTableReference(

View File

@ -7,51 +7,45 @@
package org.hibernate.sql.ast.tree.from; package org.hibernate.sql.ast.tree.from;
import java.util.List; import java.util.List;
import java.util.function.Function;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.NavigablePath; import org.hibernate.sql.ast.SqlAstWalker;
import org.hibernate.sql.ast.tree.insert.Values; import org.hibernate.sql.ast.tree.insert.Values;
/** /**
* @author Christian Beikov * @author Christian Beikov
*/ */
public class ValuesTableReference extends TableReference { public class ValuesTableReference extends DerivedTableReference {
private final List<Values> valuesList; private final List<Values> valuesList;
private final List<String> columnNames;
public ValuesTableReference( public ValuesTableReference(
List<Values> valuesList, List<Values> valuesList,
String identificationVariable, String identificationVariable,
List<String> columnNames, List<String> columnNames,
SessionFactoryImplementor sessionFactory) { SessionFactoryImplementor sessionFactory) {
super( null, identificationVariable, false, sessionFactory ); super( identificationVariable, columnNames, sessionFactory );
this.valuesList = valuesList; this.valuesList = valuesList;
this.columnNames = columnNames; }
@Override
public String getTableId() {
return null;
} }
public List<Values> getValuesList() { public List<Values> getValuesList() {
return valuesList; return valuesList;
} }
public List<String> getColumnNames() { @Override
return columnNames; public void accept(SqlAstWalker sqlTreeWalker) {
sqlTreeWalker.visitValuesTableReference( this );
} }
@Override @Override
public TableReference resolveTableReference( public Boolean visitAffectedTableNames(Function<String, Boolean> nameCollector) {
NavigablePath navigablePath,
String tableExpression,
boolean allowFkOptimization) {
throw new IllegalStateException( "Could not resolve binding for table `" + tableExpression + "`" );
}
@Override
public TableReference getTableReference(
NavigablePath navigablePath,
String tableExpression,
boolean allowFkOptimization,
boolean resolve) {
return null; return null;
} }
} }

View File

@ -14,10 +14,9 @@ import java.util.Map;
import org.hibernate.sql.ast.SqlAstWalker; import org.hibernate.sql.ast.SqlAstWalker;
import org.hibernate.sql.ast.tree.AbstractMutationStatement; import org.hibernate.sql.ast.tree.AbstractMutationStatement;
import org.hibernate.sql.ast.tree.MutationStatement;
import org.hibernate.sql.ast.tree.cte.CteStatement; import org.hibernate.sql.ast.tree.cte.CteStatement;
import org.hibernate.sql.ast.tree.expression.ColumnReference; import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.select.QueryPart; import org.hibernate.sql.ast.tree.select.QueryPart;
/** /**
@ -25,19 +24,20 @@ import org.hibernate.sql.ast.tree.select.QueryPart;
*/ */
public class InsertStatement extends AbstractMutationStatement { public class InsertStatement extends AbstractMutationStatement {
public static final String DEFAULT_ALIAS = "to_insert_";
private List<ColumnReference> targetColumnReferences; private List<ColumnReference> targetColumnReferences;
private QueryPart sourceSelectStatement; private QueryPart sourceSelectStatement;
private List<Values> valuesList = new ArrayList<>(); private List<Values> valuesList = new ArrayList<>();
public InsertStatement(TableReference targetTable) { public InsertStatement(NamedTableReference targetTable) {
super( targetTable ); super( targetTable );
} }
public InsertStatement(TableReference targetTable, List<ColumnReference> returningColumns) { public InsertStatement(NamedTableReference targetTable, List<ColumnReference> returningColumns) {
super( new LinkedHashMap<>(), targetTable, returningColumns ); super( new LinkedHashMap<>(), targetTable, returningColumns );
} }
public InsertStatement(boolean withRecursive, Map<String, CteStatement> cteStatements, TableReference targetTable, List<ColumnReference> returningColumns) { public InsertStatement(boolean withRecursive, Map<String, CteStatement> cteStatements, NamedTableReference targetTable, List<ColumnReference> returningColumns) {
super( cteStatements, targetTable, returningColumns ); super( cteStatements, targetTable, returningColumns );
setWithRecursive( withRecursive ); setWithRecursive( withRecursive );
} }

View File

@ -8,6 +8,7 @@ package org.hibernate.sql.ast.tree.select;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function;
import org.hibernate.metamodel.mapping.JdbcMappingContainer; import org.hibernate.metamodel.mapping.JdbcMappingContainer;
import org.hibernate.query.SetOperator; import org.hibernate.query.SetOperator;
@ -39,12 +40,23 @@ public class QueryGroup extends QueryPart {
} }
@Override @Override
public void forEachQuerySpec(Consumer<QuerySpec> querySpecConsumer) { public void visitQuerySpecs(Consumer<QuerySpec> querySpecConsumer) {
for ( int i = 0; i < queryParts.size(); i++ ) { for ( int i = 0; i < queryParts.size(); i++ ) {
queryParts.get( i ).forEachQuerySpec( querySpecConsumer ); queryParts.get( i ).visitQuerySpecs( querySpecConsumer );
} }
} }
@Override
public <T> T queryQuerySpecs(Function<QuerySpec, T> querySpecConsumer) {
for ( int i = 0; i < queryParts.size(); i++ ) {
T result = queryParts.get( i ).queryQuerySpecs( querySpecConsumer );
if ( result != null ) {
return result;
}
}
return null;
}
public SetOperator getSetOperator() { public SetOperator getSetOperator() {
return setOperator; return setOperator;
} }

View File

@ -9,6 +9,7 @@ package org.hibernate.sql.ast.tree.select;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function;
import org.hibernate.query.FetchClauseType; import org.hibernate.query.FetchClauseType;
import org.hibernate.query.sqm.sql.internal.DomainResultProducer; import org.hibernate.query.sqm.sql.internal.DomainResultProducer;
@ -37,7 +38,9 @@ public abstract class QueryPart implements SqlAstNode, Expression, DomainResultP
public abstract QuerySpec getLastQuerySpec(); public abstract QuerySpec getLastQuerySpec();
public abstract void forEachQuerySpec(Consumer<QuerySpec> querySpecConsumer); public abstract void visitQuerySpecs(Consumer<QuerySpec> querySpecConsumer);
public abstract <T> T queryQuerySpecs(Function<QuerySpec, T> querySpecConsumer);
/** /**
* Does this QueryPart map to the statement's root query (as * Does this QueryPart map to the statement's root query (as

View File

@ -9,6 +9,7 @@ package org.hibernate.sql.ast.tree.select;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function;
import org.hibernate.metamodel.mapping.JdbcMappingContainer; import org.hibernate.metamodel.mapping.JdbcMappingContainer;
import org.hibernate.query.sqm.sql.internal.DomainResultProducer; import org.hibernate.query.sqm.sql.internal.DomainResultProducer;
@ -63,10 +64,15 @@ public class QuerySpec extends QueryPart implements SqlAstNode, PredicateContain
} }
@Override @Override
public void forEachQuerySpec(Consumer<QuerySpec> querySpecConsumer) { public void visitQuerySpecs(Consumer<QuerySpec> querySpecConsumer) {
querySpecConsumer.accept( this ); querySpecConsumer.accept( this );
} }
@Override
public <T> T queryQuerySpecs(Function<QuerySpec, T> querySpecConsumer) {
return querySpecConsumer.apply( this );
}
public FromClause getFromClause() { public FromClause getFromClause() {
return fromClause; return fromClause;
} }

View File

@ -16,7 +16,7 @@ import org.hibernate.sql.ast.spi.SqlAstTreeHelper;
import org.hibernate.sql.ast.tree.AbstractMutationStatement; import org.hibernate.sql.ast.tree.AbstractMutationStatement;
import org.hibernate.sql.ast.tree.cte.CteStatement; import org.hibernate.sql.ast.tree.cte.CteStatement;
import org.hibernate.sql.ast.tree.expression.ColumnReference; import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.predicate.Predicate; import org.hibernate.sql.ast.tree.predicate.Predicate;
/** /**
@ -27,7 +27,7 @@ public class UpdateStatement extends AbstractMutationStatement {
private final Predicate restriction; private final Predicate restriction;
public UpdateStatement( public UpdateStatement(
TableReference targetTable, NamedTableReference targetTable,
List<Assignment> assignments, List<Assignment> assignments,
Predicate restriction) { Predicate restriction) {
super( targetTable ); super( targetTable );
@ -36,7 +36,7 @@ public class UpdateStatement extends AbstractMutationStatement {
} }
public UpdateStatement( public UpdateStatement(
TableReference targetTable, NamedTableReference targetTable,
List<Assignment> assignments, List<Assignment> assignments,
Predicate restriction, Predicate restriction,
List<ColumnReference> returningColumns) { List<ColumnReference> returningColumns) {
@ -48,7 +48,7 @@ public class UpdateStatement extends AbstractMutationStatement {
public UpdateStatement( public UpdateStatement(
boolean withRecursive, boolean withRecursive,
Map<String, CteStatement> cteStatements, Map<String, CteStatement> cteStatements,
TableReference targetTable, NamedTableReference targetTable,
List<Assignment> assignments, List<Assignment> assignments,
Predicate restriction, Predicate restriction,
List<ColumnReference> returningColumns) { List<ColumnReference> returningColumns) {
@ -67,11 +67,11 @@ public class UpdateStatement extends AbstractMutationStatement {
} }
public static class UpdateStatementBuilder { public static class UpdateStatementBuilder {
private final TableReference targetTableRef; private final NamedTableReference targetTableRef;
private List<Assignment> assignments; private List<Assignment> assignments;
private Predicate restriction; private Predicate restriction;
public UpdateStatementBuilder(TableReference targetTableRef) { public UpdateStatementBuilder(NamedTableReference targetTableRef) {
this.targetTableRef = targetTableRef; this.targetTableRef = targetTableRef;
} }

View File

@ -96,7 +96,7 @@ public class SmokeTests {
final TableGroup rootTableGroup = fromClause.getRoots().get( 0 ); final TableGroup rootTableGroup = fromClause.getRoots().get( 0 );
assertThat( rootTableGroup.getPrimaryTableReference(), notNullValue() ); assertThat( rootTableGroup.getPrimaryTableReference(), notNullValue() );
assertThat( rootTableGroup.getPrimaryTableReference().getTableExpression(), is( "mapping_simple_entity" ) ); assertThat( rootTableGroup.getPrimaryTableReference().getTableId(), is( "mapping_simple_entity" ) );
assertThat( rootTableGroup.getTableReferenceJoins().size(), is( 0 ) ); assertThat( rootTableGroup.getTableReferenceJoins().size(), is( 0 ) );
@ -155,7 +155,7 @@ public class SmokeTests {
final TableGroup rootTableGroup = fromClause.getRoots().get( 0 ); final TableGroup rootTableGroup = fromClause.getRoots().get( 0 );
assertThat( rootTableGroup.getPrimaryTableReference(), notNullValue() ); assertThat( rootTableGroup.getPrimaryTableReference(), notNullValue() );
assertThat( rootTableGroup.getPrimaryTableReference().getTableExpression(), is( "mapping_simple_entity" ) ); assertThat( rootTableGroup.getPrimaryTableReference().getTableId(), is( "mapping_simple_entity" ) );
assertThat( rootTableGroup.getTableReferenceJoins().size(), is( 0 ) ); assertThat( rootTableGroup.getTableReferenceJoins().size(), is( 0 ) );

View File

@ -179,7 +179,7 @@ public class CollectionMapWithComponentValueTest extends BaseCoreFunctionalTestC
@TestForIssue(jiraKey = "HHH-10577") @TestForIssue(jiraKey = "HHH-10577")
public void testMapKeyExpressionDereferenceInSelect() { public void testMapKeyExpressionDereferenceInSelect() {
doInHibernate( this::sessionFactory, s -> { doInHibernate( this::sessionFactory, s -> {
List<String> keyValueNames = s.createQuery( "select key(v).name as name from TestEntity te join te.values v order by name", String.class ).list(); List<String> keyValueNames = s.createQuery( "select key(v).name as name from TestEntity te join te.values v order by name", String.class ).getResultList();
assertEquals( 2, keyValueNames.size() ); assertEquals( 2, keyValueNames.size() );
assertEquals( "key1", keyValueNames.get( 0 ) ); assertEquals( "key1", keyValueNames.get( 0 ) );
assertEquals( "key2", keyValueNames.get( 1 ) ); assertEquals( "key2", keyValueNames.get( 1 ) );

View File

@ -29,6 +29,7 @@ import org.hibernate.query.sqm.sql.SqmToSqlAstConverter;
import org.hibernate.query.sqm.tree.SqmTypedNode; import org.hibernate.query.sqm.tree.SqmTypedNode;
import org.hibernate.query.sqm.tree.expression.SqmLiteral; import org.hibernate.query.sqm.tree.expression.SqmLiteral;
import org.hibernate.sql.ast.spi.SqlAstQueryPartProcessingState; import org.hibernate.sql.ast.spi.SqlAstQueryPartProcessingState;
import org.hibernate.sql.ast.tree.from.NamedTableReference;
import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin; import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.from.TableReference; import org.hibernate.sql.ast.tree.from.TableReference;
@ -117,7 +118,7 @@ public class OrderByFragmentFunction extends AbstractSqmFunctionDescriptor {
public AuditingTableGroup(TableGroup delegate, String normalTableExpression) { public AuditingTableGroup(TableGroup delegate, String normalTableExpression) {
this.delegate = delegate; this.delegate = delegate;
this.auditTableExpression = delegate.getPrimaryTableReference().getTableExpression(); this.auditTableExpression = ( (NamedTableReference) delegate.getPrimaryTableReference() ).getTableExpression();
this.normalTableExpression = normalTableExpression; this.normalTableExpression = normalTableExpression;
} }