Search in sources :

Example 1 with SqmDeleteStatement

use of org.hibernate.query.sqm.tree.delete.SqmDeleteStatement in project hibernate-orm by hibernate.

the class IdSelectionTests method testJoinedSubclassRestrictedOnPrimaryNonRootTable.

@Test
public void testJoinedSubclassRestrictedOnPrimaryNonRootTable(SessionFactoryScope scope) {
    final SqmDeleteStatement<?> sqm = (SqmDeleteStatement<?>) scope.getSessionFactory().getQueryEngine().getHqlTranslator().translate("delete ForeignCustomer where vat = :v");
    final DomainParameterXref domainParameterXref = DomainParameterXref.from(sqm);
    final ParameterMetadataImpl parameterMetadata = new ParameterMetadataImpl(domainParameterXref.getQueryParameters());
    final QueryParameterBindingsImpl domainParamBindings = QueryParameterBindingsImpl.from(parameterMetadata, scope.getSessionFactory());
    domainParamBindings.getBinding("v").setBindValue("123");
    scope.inTransaction(session -> {
        final DomainQueryExecutionContext executionContext = new TestExecutionContext(session, domainParamBindings);
        MatchingIdSelectionHelper.selectMatchingIds(sqm, domainParameterXref, executionContext);
    });
}
Also used : SqmDeleteStatement(org.hibernate.query.sqm.tree.delete.SqmDeleteStatement) DomainParameterXref(org.hibernate.query.sqm.internal.DomainParameterXref) QueryParameterBindingsImpl(org.hibernate.query.internal.QueryParameterBindingsImpl) ParameterMetadataImpl(org.hibernate.query.internal.ParameterMetadataImpl) DomainQueryExecutionContext(org.hibernate.query.spi.DomainQueryExecutionContext) JoinedInheritanceTest(org.hibernate.orm.test.mapping.inheritance.joined.JoinedInheritanceTest) Test(org.junit.jupiter.api.Test)

Example 2 with SqmDeleteStatement

use of org.hibernate.query.sqm.tree.delete.SqmDeleteStatement in project hibernate-orm by hibernate.

the class IdSelectionTests method testJoinedSubclassRestrictedOnRootTable.

@Test
public void testJoinedSubclassRestrictedOnRootTable(SessionFactoryScope scope) {
    final SqmDeleteStatement<?> sqm = (SqmDeleteStatement<?>) scope.getSessionFactory().getQueryEngine().getHqlTranslator().translate("delete Customer where name = :n");
    final DomainParameterXref domainParameterXref = DomainParameterXref.from(sqm);
    final ParameterMetadataImpl parameterMetadata = new ParameterMetadataImpl(domainParameterXref.getQueryParameters());
    final QueryParameterBindingsImpl domainParamBindings = QueryParameterBindingsImpl.from(parameterMetadata, scope.getSessionFactory());
    domainParamBindings.getBinding("n").setBindValue("Acme");
    scope.inTransaction(session -> {
        final DomainQueryExecutionContext executionContext = new TestExecutionContext(session, domainParamBindings);
        MatchingIdSelectionHelper.selectMatchingIds(sqm, domainParameterXref, executionContext);
    });
}
Also used : SqmDeleteStatement(org.hibernate.query.sqm.tree.delete.SqmDeleteStatement) DomainParameterXref(org.hibernate.query.sqm.internal.DomainParameterXref) QueryParameterBindingsImpl(org.hibernate.query.internal.QueryParameterBindingsImpl) ParameterMetadataImpl(org.hibernate.query.internal.ParameterMetadataImpl) DomainQueryExecutionContext(org.hibernate.query.spi.DomainQueryExecutionContext) JoinedInheritanceTest(org.hibernate.orm.test.mapping.inheritance.joined.JoinedInheritanceTest) Test(org.junit.jupiter.api.Test)

Example 3 with SqmDeleteStatement

use of org.hibernate.query.sqm.tree.delete.SqmDeleteStatement in project hibernate-orm by hibernate.

the class IdSelectionTests method testJoinedSubclassRestrictedOnNonPrimaryRootTable.

@Test
public void testJoinedSubclassRestrictedOnNonPrimaryRootTable(SessionFactoryScope scope) {
    final SqmDeleteStatement<?> sqm = (SqmDeleteStatement<?>) scope.getSessionFactory().getQueryEngine().getHqlTranslator().translate("delete ForeignCustomer where name = :n");
    final DomainParameterXref domainParameterXref = DomainParameterXref.from(sqm);
    final ParameterMetadataImpl parameterMetadata = new ParameterMetadataImpl(domainParameterXref.getQueryParameters());
    final QueryParameterBindingsImpl domainParamBindings = QueryParameterBindingsImpl.from(parameterMetadata, scope.getSessionFactory());
    domainParamBindings.getBinding("n").setBindValue("Acme");
    scope.inTransaction(session -> {
        final DomainQueryExecutionContext executionContext = new TestExecutionContext(session, domainParamBindings);
        MatchingIdSelectionHelper.selectMatchingIds(sqm, domainParameterXref, executionContext);
    });
}
Also used : SqmDeleteStatement(org.hibernate.query.sqm.tree.delete.SqmDeleteStatement) DomainParameterXref(org.hibernate.query.sqm.internal.DomainParameterXref) QueryParameterBindingsImpl(org.hibernate.query.internal.QueryParameterBindingsImpl) ParameterMetadataImpl(org.hibernate.query.internal.ParameterMetadataImpl) DomainQueryExecutionContext(org.hibernate.query.spi.DomainQueryExecutionContext) JoinedInheritanceTest(org.hibernate.orm.test.mapping.inheritance.joined.JoinedInheritanceTest) Test(org.junit.jupiter.api.Test)

Example 4 with SqmDeleteStatement

use of org.hibernate.query.sqm.tree.delete.SqmDeleteStatement in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitDeleteStatement.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Delete statement
@Override
public DeleteStatement visitDeleteStatement(SqmDeleteStatement<?> statement) {
    final CteContainer cteContainer = this.visitCteContainer(statement);
    final String entityName = statement.getTarget().getEntityName();
    final EntityPersister entityDescriptor = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(entityName);
    assert entityDescriptor != null;
    pushProcessingState(new SqlAstProcessingStateImpl(getCurrentProcessingState(), this, getCurrentClauseStack()::getCurrent));
    try {
        final NavigablePath rootPath = statement.getTarget().getNavigablePath();
        final TableGroup rootTableGroup = entityDescriptor.createRootTableGroup(true, rootPath, statement.getRoot().getAlias(), () -> predicate -> additionalRestrictions = SqlAstTreeHelper.combinePredicates(additionalRestrictions, predicate), this, getCreationContext());
        getFromClauseAccess().registerTableGroup(rootPath, rootTableGroup);
        if (!rootTableGroup.getTableReferenceJoins().isEmpty()) {
            throw new HibernateException("Not expecting multiple table references for an SQM DELETE");
        }
        FilterHelper.applyBaseRestrictions((filterPredicate) -> additionalRestrictions = filterPredicate, entityDescriptor, rootTableGroup, AbstractSqlAstTranslator.rendersTableReferenceAlias(Clause.DELETE), getLoadQueryInfluencers(), this);
        Predicate suppliedPredicate = null;
        final SqmWhereClause whereClause = statement.getWhereClause();
        if (whereClause != null) {
            suppliedPredicate = visitWhereClause(whereClause.getPredicate());
        }
        return new DeleteStatement(cteContainer, (NamedTableReference) rootTableGroup.getPrimaryTableReference(), SqlAstTreeHelper.combinePredicates(suppliedPredicate, additionalRestrictions), Collections.emptyList());
    } finally {
        popProcessingStateStack();
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SingleTableEntityPersister(org.hibernate.persister.entity.SingleTableEntityPersister) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) SqmCteContainer(org.hibernate.query.sqm.tree.cte.SqmCteContainer) CteContainer(org.hibernate.sql.ast.tree.cte.CteContainer) NavigablePath(org.hibernate.query.spi.NavigablePath) VirtualTableGroup(org.hibernate.sql.ast.tree.from.VirtualTableGroup) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) CorrelatedTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedTableGroup) CorrelatedPluralTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) QueryPartTableGroup(org.hibernate.sql.ast.tree.from.QueryPartTableGroup) HibernateException(org.hibernate.HibernateException) SqmWhereClause(org.hibernate.query.sqm.tree.predicate.SqmWhereClause) SqlAstProcessingStateImpl(org.hibernate.query.sqm.sql.internal.SqlAstProcessingStateImpl) DeleteStatement(org.hibernate.sql.ast.tree.delete.DeleteStatement) SqmDeleteStatement(org.hibernate.query.sqm.tree.delete.SqmDeleteStatement) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) SqmBooleanExpressionPredicate(org.hibernate.query.sqm.tree.predicate.SqmBooleanExpressionPredicate) SelfRenderingPredicate(org.hibernate.sql.ast.tree.predicate.SelfRenderingPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) LikePredicate(org.hibernate.sql.ast.tree.predicate.LikePredicate) BetweenPredicate(org.hibernate.sql.ast.tree.predicate.BetweenPredicate) SqmPredicate(org.hibernate.query.sqm.tree.predicate.SqmPredicate) SqmNegatedPredicate(org.hibernate.query.sqm.tree.predicate.SqmNegatedPredicate) ExistsPredicate(org.hibernate.sql.ast.tree.predicate.ExistsPredicate) SqmAndPredicate(org.hibernate.query.sqm.tree.predicate.SqmAndPredicate) SqmMemberOfPredicate(org.hibernate.query.sqm.tree.predicate.SqmMemberOfPredicate) SqmLikePredicate(org.hibernate.query.sqm.tree.predicate.SqmLikePredicate) SqmExistsPredicate(org.hibernate.query.sqm.tree.predicate.SqmExistsPredicate) SqmOrPredicate(org.hibernate.query.sqm.tree.predicate.SqmOrPredicate) SqmNullnessPredicate(org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) SqmGroupedPredicate(org.hibernate.query.sqm.tree.predicate.SqmGroupedPredicate) SqmInListPredicate(org.hibernate.query.sqm.tree.predicate.SqmInListPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) SqmBetweenPredicate(org.hibernate.query.sqm.tree.predicate.SqmBetweenPredicate) GroupedPredicate(org.hibernate.sql.ast.tree.predicate.GroupedPredicate) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) SqmInSubQueryPredicate(org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate) SqmEmptinessPredicate(org.hibernate.query.sqm.tree.predicate.SqmEmptinessPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate)

Example 5 with SqmDeleteStatement

use of org.hibernate.query.sqm.tree.delete.SqmDeleteStatement in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitDeleteStatement.

@Override
public SqmDeleteStatement<R> visitDeleteStatement(HqlParser.DeleteStatementContext ctx) {
    final int dmlTargetIndex;
    if (ctx.getChild(1) instanceof HqlParser.TargetEntityContext) {
        dmlTargetIndex = 1;
    } else {
        dmlTargetIndex = 2;
    }
    final HqlParser.TargetEntityContext dmlTargetContext = (HqlParser.TargetEntityContext) ctx.getChild(dmlTargetIndex);
    final SqmRoot<R> root = visitTargetEntity(dmlTargetContext);
    final SqmDeleteStatement<R> deleteStatement = new SqmDeleteStatement<>(root, SqmQuerySource.HQL, creationContext.getNodeBuilder());
    parameterCollector = deleteStatement;
    final SqmDmlCreationProcessingState sqmDeleteCreationState = new SqmDmlCreationProcessingState(deleteStatement, this);
    sqmDeleteCreationState.getPathRegistry().register(root);
    processingStateStack.push(sqmDeleteCreationState);
    try {
        if (dmlTargetIndex + 1 <= ctx.getChildCount()) {
            deleteStatement.applyPredicate(visitWhereClause((HqlParser.WhereClauseContext) ctx.getChild(dmlTargetIndex + 1)));
        }
        return deleteStatement;
    } finally {
        processingStateStack.pop();
    }
}
Also used : TIMEZONE_HOUR(org.hibernate.query.sqm.TemporalUnit.TIMEZONE_HOUR) WEEK_OF_YEAR(org.hibernate.query.sqm.TemporalUnit.WEEK_OF_YEAR) DAY_OF_YEAR(org.hibernate.query.sqm.TemporalUnit.DAY_OF_YEAR) IDENTIFIER(org.hibernate.grammars.hql.HqlParser.IDENTIFIER) HqlParser(org.hibernate.grammars.hql.HqlParser) SqmDeleteStatement(org.hibernate.query.sqm.tree.delete.SqmDeleteStatement) SqmDmlCreationProcessingState(org.hibernate.query.sqm.internal.SqmDmlCreationProcessingState)

Aggregations

SqmDeleteStatement (org.hibernate.query.sqm.tree.delete.SqmDeleteStatement)7 JoinedInheritanceTest (org.hibernate.orm.test.mapping.inheritance.joined.JoinedInheritanceTest)5 ParameterMetadataImpl (org.hibernate.query.internal.ParameterMetadataImpl)5 QueryParameterBindingsImpl (org.hibernate.query.internal.QueryParameterBindingsImpl)5 DomainQueryExecutionContext (org.hibernate.query.spi.DomainQueryExecutionContext)5 DomainParameterXref (org.hibernate.query.sqm.internal.DomainParameterXref)5 Test (org.junit.jupiter.api.Test)5 HibernateException (org.hibernate.HibernateException)1 HqlParser (org.hibernate.grammars.hql.HqlParser)1 IDENTIFIER (org.hibernate.grammars.hql.HqlParser.IDENTIFIER)1 AbstractEntityPersister (org.hibernate.persister.entity.AbstractEntityPersister)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 SingleTableEntityPersister (org.hibernate.persister.entity.SingleTableEntityPersister)1 NavigablePath (org.hibernate.query.spi.NavigablePath)1 DAY_OF_YEAR (org.hibernate.query.sqm.TemporalUnit.DAY_OF_YEAR)1 TIMEZONE_HOUR (org.hibernate.query.sqm.TemporalUnit.TIMEZONE_HOUR)1 WEEK_OF_YEAR (org.hibernate.query.sqm.TemporalUnit.WEEK_OF_YEAR)1 SqmDmlCreationProcessingState (org.hibernate.query.sqm.internal.SqmDmlCreationProcessingState)1 SqlAstProcessingStateImpl (org.hibernate.query.sqm.sql.internal.SqlAstProcessingStateImpl)1 SqmCteContainer (org.hibernate.query.sqm.tree.cte.SqmCteContainer)1