use of org.hibernate.sql.ast.tree.from.UnionTableReference in project hibernate-orm by hibernate.
the class RestrictedDeleteExecutionDelegate method executeWithoutIdTable.
private int executeWithoutIdTable(Predicate suppliedPredicate, TableGroup tableGroup, Map<SqmParameter<?>, List<List<JdbcParameter>>> restrictionSqmParameterResolutions, Map<SqmParameter<?>, MappingModelExpressible<?>> paramTypeResolutions, SqlExpressionResolver sqlExpressionResolver, ExecutionContext executionContext) {
assert entityDescriptor == entityDescriptor.getRootEntityDescriptor();
final EntityPersister rootEntityPersister = entityDescriptor.getEntityPersister();
final String rootTableName = ((Joinable) rootEntityPersister).getTableName();
final NamedTableReference rootTableReference = (NamedTableReference) tableGroup.resolveTableReference(tableGroup.getNavigablePath(), rootTableName);
final QuerySpec matchingIdSubQuerySpec = ExecuteWithoutIdTableHelper.createIdMatchingSubQuerySpec(tableGroup.getNavigablePath(), rootTableReference, suppliedPredicate, rootEntityPersister, sqlExpressionResolver, sessionFactory);
final JdbcParameterBindings jdbcParameterBindings = SqmUtil.createJdbcParameterBindings(executionContext.getQueryParameterBindings(), domainParameterXref, SqmUtil.generateJdbcParamsXref(domainParameterXref, () -> restrictionSqmParameterResolutions), sessionFactory.getRuntimeMetamodels().getMappingMetamodel(), navigablePath -> tableGroup, new SqmParameterMappingModelResolutionAccess() {
@Override
@SuppressWarnings("unchecked")
public <T> MappingModelExpressible<T> getResolvedMappingModelType(SqmParameter<T> parameter) {
return (MappingModelExpressible<T>) paramTypeResolutions.get(parameter);
}
}, executionContext.getSession());
SqmMutationStrategyHelper.cleanUpCollectionTables(entityDescriptor, (tableReference, attributeMapping) -> {
// No need for a predicate if there is no supplied predicate i.e. this is a full cleanup
if (suppliedPredicate == null) {
return null;
}
final ForeignKeyDescriptor fkDescriptor = attributeMapping.getKeyDescriptor();
final QuerySpec idSelectFkSubQuery;
// todo (6.0): based on the location of the attribute mapping, we could prune the table group of the subquery
if (fkDescriptor.getTargetPart() instanceof EntityIdentifierMapping) {
idSelectFkSubQuery = matchingIdSubQuerySpec;
} else {
idSelectFkSubQuery = ExecuteWithoutIdTableHelper.createIdMatchingSubQuerySpec(tableGroup.getNavigablePath(), rootTableReference, suppliedPredicate, rootEntityPersister, sqlExpressionResolver, sessionFactory);
}
return new InSubQueryPredicate(MappingModelHelper.buildColumnReferenceExpression(fkDescriptor, null, sessionFactory), idSelectFkSubQuery, false);
}, jdbcParameterBindings, executionContext);
if (rootTableReference instanceof UnionTableReference) {
final MutableInteger rows = new MutableInteger();
entityDescriptor.visitConstraintOrderedTables((tableExpression, tableKeyColumnVisitationSupplier) -> {
final NamedTableReference tableReference = new NamedTableReference(tableExpression, tableGroup.getPrimaryTableReference().getIdentificationVariable(), false, sessionFactory);
final QuerySpec idMatchingSubQuerySpec;
// No need for a predicate if there is no supplied predicate i.e. this is a full cleanup
if (suppliedPredicate == null) {
idMatchingSubQuerySpec = null;
} else {
idMatchingSubQuerySpec = matchingIdSubQuerySpec;
}
rows.plus(deleteFromNonRootTableWithoutIdTable(tableReference, tableKeyColumnVisitationSupplier, sqlExpressionResolver, tableGroup, idMatchingSubQuerySpec, jdbcParameterBindings, executionContext));
});
return rows.get();
} else {
entityDescriptor.visitConstraintOrderedTables((tableExpression, tableKeyColumnVisitationSupplier) -> {
if (!tableExpression.equals(rootTableName)) {
final NamedTableReference tableReference = (NamedTableReference) tableGroup.getTableReference(tableGroup.getNavigablePath(), tableExpression, true, true);
final QuerySpec idMatchingSubQuerySpec;
// No need for a predicate if there is no supplied predicate i.e. this is a full cleanup
if (suppliedPredicate == null) {
idMatchingSubQuerySpec = null;
} else {
idMatchingSubQuerySpec = matchingIdSubQuerySpec;
}
deleteFromNonRootTableWithoutIdTable(tableReference, tableKeyColumnVisitationSupplier, sqlExpressionResolver, tableGroup, idMatchingSubQuerySpec, jdbcParameterBindings, executionContext);
}
});
return deleteFromRootTableWithoutIdTable(rootTableReference, suppliedPredicate, jdbcParameterBindings, executionContext);
}
}
use of org.hibernate.sql.ast.tree.from.UnionTableReference in project hibernate-orm by hibernate.
the class SQLServerSqlAstTranslator method renderNamedTableReference.
@Override
protected boolean renderNamedTableReference(NamedTableReference tableReference, LockMode lockMode) {
final String tableExpression = tableReference.getTableExpression();
if (tableReference instanceof UnionTableReference && lockMode != LockMode.NONE && tableExpression.charAt(0) == '(') {
// SQL Server requires to push down the lock hint to the actual table names
int searchIndex = 0;
int unionIndex;
while ((unionIndex = tableExpression.indexOf(UNION_ALL, searchIndex)) != -1) {
append(tableExpression, searchIndex, unionIndex);
renderLockHint(lockMode);
appendSql(UNION_ALL);
searchIndex = unionIndex + UNION_ALL.length();
}
append(tableExpression, searchIndex, tableExpression.length() - 2);
renderLockHint(lockMode);
appendSql(" )");
registerAffectedTable(tableReference);
final Clause currentClause = getClauseStack().getCurrent();
if (rendersTableReferenceAlias(currentClause)) {
final String identificationVariable = tableReference.getIdentificationVariable();
if (identificationVariable != null) {
appendSql(' ');
appendSql(identificationVariable);
}
}
} else {
super.renderNamedTableReference(tableReference, lockMode);
renderLockHint(lockMode);
}
// Just always return true because SQL Server doesn't support the FOR UPDATE clause
return true;
}
use of org.hibernate.sql.ast.tree.from.UnionTableReference in project hibernate-orm by hibernate.
the class SybaseASESqlAstTranslator method visitColumnReference.
@Override
public void visitColumnReference(ColumnReference columnReference) {
final String dmlTargetTableAlias = getDmlTargetTableAlias();
if (dmlTargetTableAlias != null && dmlTargetTableAlias.equals(columnReference.getQualifier())) {
// Sybase needs a table name prefix
// but not if this is a restricted union table reference subquery
final QuerySpec currentQuerySpec = (QuerySpec) getQueryPartStack().getCurrent();
final List<TableGroup> roots;
if (currentQuerySpec != null && !currentQuerySpec.isRoot() && (roots = currentQuerySpec.getFromClause().getRoots()).size() == 1 && roots.get(0).getPrimaryTableReference() instanceof UnionTableReference) {
appendSql(columnReference.getExpressionText());
} else // for now, use the unqualified form
if (columnReference.isColumnExpressionFormula()) {
// For formulas, we have to replace the qualifier as the alias was already rendered into the formula
// This is fine for now as this is only temporary anyway until we render aliases for table references
appendSql(columnReference.getColumnExpression().replaceAll("(\\b)(" + dmlTargetTableAlias + "\\.)(\\b)", "$1$3"));
} else {
appendSql(getCurrentDmlStatement().getTargetTable().getTableExpression());
appendSql('.');
appendSql(columnReference.getColumnExpression());
}
} else {
appendSql(columnReference.getExpressionText());
}
}
Aggregations