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);
});
}
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);
});
}
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);
});
}
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();
}
}
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();
}
}
Aggregations