Search in sources :

Example 6 with UpdateStatement

use of org.hibernate.sql.ast.tree.update.UpdateStatement in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitUpdateStatement.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Update statement
@Override
public UpdateStatement visitUpdateStatement(SqmUpdateStatement<?> sqmStatement) {
    final CteContainer cteContainer = this.visitCteContainer(sqmStatement);
    final SqmRoot<?> sqmTarget = sqmStatement.getTarget();
    final String entityName = sqmTarget.getEntityName();
    final EntityPersister entityDescriptor = getCreationContext().getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(entityName);
    assert entityDescriptor != null;
    pushProcessingState(new SqlAstProcessingStateImpl(getCurrentProcessingState(), this, getCurrentClauseStack()::getCurrent));
    try {
        final NavigablePath rootPath = sqmTarget.getNavigablePath();
        final TableGroup rootTableGroup = entityDescriptor.createRootTableGroup(true, rootPath, sqmStatement.getRoot().getAlias(), () -> predicate -> additionalRestrictions = SqlAstTreeHelper.combinePredicates(additionalRestrictions, predicate), this, getCreationContext());
        if (!rootTableGroup.getTableReferenceJoins().isEmpty()) {
            throw new HibernateException("Not expecting multiple table references for an SQM UPDATE");
        }
        if (sqmTarget.hasJoins()) {
            throw new HibernateException("SQM UPDATE does not support explicit joins");
        }
        getFromClauseAccess().registerTableGroup(rootPath, rootTableGroup);
        final List<Assignment> assignments = visitSetClause(sqmStatement.getSetClause());
        addVersionedAssignment(assignments::add, sqmStatement);
        FilterHelper.applyBaseRestrictions((filterPredicate) -> additionalRestrictions = filterPredicate, entityDescriptor, rootTableGroup, AbstractSqlAstTranslator.rendersTableReferenceAlias(Clause.UPDATE), getLoadQueryInfluencers(), this);
        Predicate suppliedPredicate = null;
        final SqmWhereClause whereClause = sqmStatement.getWhereClause();
        if (whereClause != null) {
            suppliedPredicate = visitWhereClause(whereClause.getPredicate());
        }
        return new UpdateStatement(cteContainer, (NamedTableReference) rootTableGroup.getPrimaryTableReference(), assignments, 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) UpdateStatement(org.hibernate.sql.ast.tree.update.UpdateStatement) SqmUpdateStatement(org.hibernate.query.sqm.tree.update.SqmUpdateStatement) 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) SqlAstProcessingStateImpl(org.hibernate.query.sqm.sql.internal.SqlAstProcessingStateImpl) 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) Assignment(org.hibernate.sql.ast.tree.update.Assignment) SqmAssignment(org.hibernate.query.sqm.tree.update.SqmAssignment) SqmWhereClause(org.hibernate.query.sqm.tree.predicate.SqmWhereClause)

Example 7 with UpdateStatement

use of org.hibernate.sql.ast.tree.update.UpdateStatement in project hibernate-orm by hibernate.

the class SqlTreePrinter method visitStatement.

private void visitStatement(Statement sqlAstStatement) {
    if (sqlAstStatement instanceof SelectStatement) {
        final SelectStatement selectStatement = (SelectStatement) sqlAstStatement;
        logNode("SelectStatement", () -> visitQueryPart(selectStatement.getQueryPart()));
    } else if (sqlAstStatement instanceof DeleteStatement) {
        final DeleteStatement deleteStatement = (DeleteStatement) sqlAstStatement;
        logNode("DeleteStatement", () -> logWithIndentation("target : " + deleteStatement.getTargetTable().getTableExpression()));
    } else if (sqlAstStatement instanceof UpdateStatement) {
        final UpdateStatement updateStatement = (UpdateStatement) sqlAstStatement;
        logNode("UpdateStatement", () -> logWithIndentation("target : " + updateStatement.getTargetTable().getTableExpression()));
    } else if (sqlAstStatement instanceof InsertStatement) {
        final InsertStatement insertStatement = (InsertStatement) sqlAstStatement;
        logNode("InsertStatement", () -> logWithIndentation("target : " + insertStatement.getTargetTable().getTableExpression()));
    } else {
        throw new UnsupportedOperationException("Printing for this type of SQL AST not supported : " + sqlAstStatement);
    }
}
Also used : SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) UpdateStatement(org.hibernate.sql.ast.tree.update.UpdateStatement) DeleteStatement(org.hibernate.sql.ast.tree.delete.DeleteStatement) InsertStatement(org.hibernate.sql.ast.tree.insert.InsertStatement)

Aggregations

UpdateStatement (org.hibernate.sql.ast.tree.update.UpdateStatement)7 SqmUpdateStatement (org.hibernate.query.sqm.tree.update.SqmUpdateStatement)3 Assignment (org.hibernate.sql.ast.tree.update.Assignment)3 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)2 MappingModelExpressible (org.hibernate.metamodel.mapping.MappingModelExpressible)2 AbstractEntityPersister (org.hibernate.persister.entity.AbstractEntityPersister)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)2 NamedTableReference (org.hibernate.sql.ast.tree.from.NamedTableReference)2 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)2 TableReference (org.hibernate.sql.ast.tree.from.TableReference)2 InsertStatement (org.hibernate.sql.ast.tree.insert.InsertStatement)2 InSubQueryPredicate (org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate)2 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)2 PreparedStatement (java.sql.PreparedStatement)1