Search in sources :

Example 1 with JdbcDelete

use of org.hibernate.sql.exec.spi.JdbcDelete in project hibernate-orm by hibernate.

the class InlineDeleteHandler method executeDelete.

private void executeDelete(String targetTableExpression, EntityMappingType entityDescriptor, Supplier<Consumer<SelectableConsumer>> tableKeyColumnsVisitationSupplier, List<Object> ids, int valueIndex, ModelPart valueModelPart, JdbcParameterBindings jdbcParameterBindings, DomainQueryExecutionContext executionContext) {
    final NamedTableReference targetTableReference = new NamedTableReference(targetTableExpression, DeleteStatement.DEFAULT_ALIAS, false, sessionFactory);
    final SqmJdbcExecutionContextAdapter executionContextAdapter = SqmJdbcExecutionContextAdapter.omittingLockingAndPaging(executionContext);
    final Predicate matchingIdsPredicate = matchingIdsPredicateProducer.produceRestriction(ids, entityDescriptor, valueIndex, valueModelPart, targetTableReference, tableKeyColumnsVisitationSupplier, executionContextAdapter);
    final DeleteStatement deleteStatement = new DeleteStatement(targetTableReference, matchingIdsPredicate);
    final JdbcDelete jdbcOperation = sqlAstTranslatorFactory.buildDeleteTranslator(sessionFactory, deleteStatement).translate(jdbcParameterBindings, executionContext.getQueryOptions());
    jdbcMutationExecutor.execute(jdbcOperation, jdbcParameterBindings, this::prepareQueryStatement, (integer, preparedStatement) -> {
    }, executionContextAdapter);
}
Also used : NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) JdbcDelete(org.hibernate.sql.exec.spi.JdbcDelete) SqmDeleteStatement(org.hibernate.query.sqm.tree.delete.SqmDeleteStatement) DeleteStatement(org.hibernate.sql.ast.tree.delete.DeleteStatement) SqmJdbcExecutionContextAdapter(org.hibernate.query.sqm.internal.SqmJdbcExecutionContextAdapter) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate)

Example 2 with JdbcDelete

use of org.hibernate.sql.exec.spi.JdbcDelete in project hibernate-orm by hibernate.

the class SimpleDeleteQueryPlan method executeUpdate.

@Override
public int executeUpdate(DomainQueryExecutionContext executionContext) {
    BulkOperationCleanupAction.schedule(executionContext.getSession(), sqmDelete);
    final SharedSessionContractImplementor session = executionContext.getSession();
    final SessionFactoryImplementor factory = session.getFactory();
    final JdbcServices jdbcServices = factory.getJdbcServices();
    SqlAstTranslator<JdbcDelete> deleteTranslator = null;
    if (jdbcDelete == null) {
        deleteTranslator = createDeleteTranslator(executionContext);
    }
    final JdbcParameterBindings jdbcParameterBindings = SqmUtil.createJdbcParameterBindings(executionContext.getQueryParameterBindings(), domainParameterXref, jdbcParamsXref, factory.getRuntimeMetamodels().getMappingMetamodel(), sqmInterpretation.getFromClauseAccess()::findTableGroup, new SqmParameterMappingModelResolutionAccess() {

        @Override
        @SuppressWarnings("unchecked")
        public <T> MappingModelExpressible<T> getResolvedMappingModelType(SqmParameter<T> parameter) {
            return (MappingModelExpressible<T>) sqmInterpretation.getSqmParameterMappingModelTypeResolutions().get(parameter);
        }
    }, session);
    if (jdbcDelete != null && !jdbcDelete.isCompatibleWith(jdbcParameterBindings, executionContext.getQueryOptions())) {
        deleteTranslator = createDeleteTranslator(executionContext);
    }
    if (deleteTranslator != null) {
        jdbcDelete = deleteTranslator.translate(jdbcParameterBindings, executionContext.getQueryOptions());
    } else {
        jdbcDelete.bindFilterJdbcParameters(jdbcParameterBindings);
    }
    final boolean missingRestriction = sqmDelete.getWhereClause() == null || sqmDelete.getWhereClause().getPredicate() == null;
    if (missingRestriction) {
        assert domainParameterXref.getSqmParameterCount() == 0;
        assert jdbcParamsXref.isEmpty();
    }
    final SqmJdbcExecutionContextAdapter executionContextAdapter = SqmJdbcExecutionContextAdapter.usingLockingAndPaging(executionContext);
    SqmMutationStrategyHelper.cleanUpCollectionTables(entityDescriptor, (tableReference, attributeMapping) -> {
        if (missingRestriction) {
            return null;
        }
        final ForeignKeyDescriptor fkDescriptor = attributeMapping.getKeyDescriptor();
        final Expression fkColumnExpression = MappingModelHelper.buildColumnReferenceExpression(fkDescriptor.getKeyPart(), null, factory);
        final QuerySpec matchingIdSubQuery = new QuerySpec(false);
        final MutatingTableReferenceGroupWrapper tableGroup = new MutatingTableReferenceGroupWrapper(new NavigablePath(attributeMapping.getRootPathName()), attributeMapping, sqmInterpretation.getSqlAst().getTargetTable());
        final Expression fkTargetColumnExpression = MappingModelHelper.buildColumnReferenceExpression(tableGroup, fkDescriptor.getTargetPart(), sqmInterpretation.getSqlExpressionResolver(), factory);
        matchingIdSubQuery.getSelectClause().addSqlSelection(new SqlSelectionImpl(1, 0, fkTargetColumnExpression));
        matchingIdSubQuery.getFromClause().addRoot(tableGroup);
        matchingIdSubQuery.applyPredicate(sqmInterpretation.getSqlAst().getRestriction());
        return new InSubQueryPredicate(fkColumnExpression, matchingIdSubQuery, false);
    }, (missingRestriction ? JdbcParameterBindings.NO_BINDINGS : jdbcParameterBindings), executionContextAdapter);
    return jdbcServices.getJdbcMutationExecutor().execute(jdbcDelete, jdbcParameterBindings, sql -> session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql), (integer, preparedStatement) -> {
    }, executionContextAdapter);
}
Also used : MutatingTableReferenceGroupWrapper(org.hibernate.sql.ast.tree.from.MutatingTableReferenceGroupWrapper) NavigablePath(org.hibernate.query.spi.NavigablePath) SqmParameterMappingModelResolutionAccess(org.hibernate.query.sqm.spi.SqmParameterMappingModelResolutionAccess) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) Expression(org.hibernate.sql.ast.tree.expression.Expression) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) SqlSelectionImpl(org.hibernate.sql.results.internal.SqlSelectionImpl) JdbcDelete(org.hibernate.sql.exec.spi.JdbcDelete) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Example 3 with JdbcDelete

use of org.hibernate.sql.exec.spi.JdbcDelete in project hibernate-orm by hibernate.

the class RestrictedDeleteExecutionDelegate method executeSqlDelete.

private static int executeSqlDelete(DeleteStatement sqlAst, JdbcParameterBindings jdbcParameterBindings, ExecutionContext executionContext) {
    final SessionFactoryImplementor factory = executionContext.getSession().getFactory();
    final JdbcServices jdbcServices = factory.getJdbcServices();
    final JdbcDelete jdbcDelete = jdbcServices.getJdbcEnvironment().getSqlAstTranslatorFactory().buildDeleteTranslator(factory, sqlAst).translate(jdbcParameterBindings, executionContext.getQueryOptions());
    return jdbcServices.getJdbcMutationExecutor().execute(jdbcDelete, jdbcParameterBindings, sql -> executionContext.getSession().getJdbcCoordinator().getStatementPreparer().prepareStatement(sql), (integer, preparedStatement) -> {
    }, executionContext);
}
Also used : SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) JdbcDelete(org.hibernate.sql.exec.spi.JdbcDelete)

Aggregations

JdbcDelete (org.hibernate.sql.exec.spi.JdbcDelete)3 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)1 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)1 MappingModelExpressible (org.hibernate.metamodel.mapping.MappingModelExpressible)1 NavigablePath (org.hibernate.query.spi.NavigablePath)1 SqmJdbcExecutionContextAdapter (org.hibernate.query.sqm.internal.SqmJdbcExecutionContextAdapter)1 SqmParameterMappingModelResolutionAccess (org.hibernate.query.sqm.spi.SqmParameterMappingModelResolutionAccess)1 SqmDeleteStatement (org.hibernate.query.sqm.tree.delete.SqmDeleteStatement)1 DeleteStatement (org.hibernate.sql.ast.tree.delete.DeleteStatement)1 Expression (org.hibernate.sql.ast.tree.expression.Expression)1 MutatingTableReferenceGroupWrapper (org.hibernate.sql.ast.tree.from.MutatingTableReferenceGroupWrapper)1 NamedTableReference (org.hibernate.sql.ast.tree.from.NamedTableReference)1 InSubQueryPredicate (org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate)1 Predicate (org.hibernate.sql.ast.tree.predicate.Predicate)1 QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)1 JdbcParameterBindings (org.hibernate.sql.exec.spi.JdbcParameterBindings)1 SqlSelectionImpl (org.hibernate.sql.results.internal.SqlSelectionImpl)1