Search in sources :

Example 1 with UnionTableReference

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);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) SqmParameterMappingModelResolutionAccess(org.hibernate.query.sqm.spi.SqmParameterMappingModelResolutionAccess) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) MutableInteger(org.hibernate.internal.util.MutableInteger) UnionTableReference(org.hibernate.sql.ast.tree.from.UnionTableReference) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) Joinable(org.hibernate.persister.entity.Joinable) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Example 2 with UnionTableReference

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;
}
Also used : Clause(org.hibernate.sql.ast.Clause) SelectClause(org.hibernate.sql.ast.tree.select.SelectClause) UnionTableReference(org.hibernate.sql.ast.tree.from.UnionTableReference)

Example 3 with UnionTableReference

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());
    }
}
Also used : TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) UnionTableReference(org.hibernate.sql.ast.tree.from.UnionTableReference)

Aggregations

UnionTableReference (org.hibernate.sql.ast.tree.from.UnionTableReference)3 QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)2 MutableInteger (org.hibernate.internal.util.MutableInteger)1 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)1 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)1 MappingModelExpressible (org.hibernate.metamodel.mapping.MappingModelExpressible)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 Joinable (org.hibernate.persister.entity.Joinable)1 SqmParameterMappingModelResolutionAccess (org.hibernate.query.sqm.spi.SqmParameterMappingModelResolutionAccess)1 Clause (org.hibernate.sql.ast.Clause)1 NamedTableReference (org.hibernate.sql.ast.tree.from.NamedTableReference)1 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)1 InSubQueryPredicate (org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate)1 SelectClause (org.hibernate.sql.ast.tree.select.SelectClause)1 JdbcParameterBindings (org.hibernate.sql.exec.spi.JdbcParameterBindings)1