Search in sources :

Example 1 with SqmDmlCreationProcessingState

use of org.hibernate.query.sqm.internal.SqmDmlCreationProcessingState in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitUpdateStatement.

@Override
public SqmUpdateStatement<R> visitUpdateStatement(HqlParser.UpdateStatementContext ctx) {
    final boolean versioned = !(ctx.getChild(1) instanceof HqlParser.TargetEntityContext);
    final int dmlTargetIndex = versioned ? 2 : 1;
    final HqlParser.TargetEntityContext dmlTargetContext = (HqlParser.TargetEntityContext) ctx.getChild(dmlTargetIndex);
    final SqmRoot<R> root = visitTargetEntity(dmlTargetContext);
    if (root.getReferencedPathSource() instanceof SqmPolymorphicRootDescriptor<?>) {
        throw new SemanticException(String.format("Target type '%s' in update statement is not an entity", root.getReferencedPathSource().getHibernateEntityName()));
    }
    final SqmUpdateStatement<R> updateStatement = new SqmUpdateStatement<>(root, creationContext.getNodeBuilder());
    parameterCollector = updateStatement;
    final SqmDmlCreationProcessingState processingState = new SqmDmlCreationProcessingState(updateStatement, this);
    processingStateStack.push(processingState);
    processingState.getPathRegistry().register(root);
    try {
        updateStatement.versioned(versioned);
        final HqlParser.SetClauseContext setClauseCtx = (HqlParser.SetClauseContext) ctx.getChild(dmlTargetIndex + 1);
        for (ParseTree subCtx : setClauseCtx.children) {
            if (subCtx instanceof HqlParser.AssignmentContext) {
                final HqlParser.AssignmentContext assignmentContext = (HqlParser.AssignmentContext) subCtx;
                updateStatement.applyAssignment(consumeDomainPath((HqlParser.SimplePathContext) assignmentContext.getChild(0)), (SqmExpression<?>) assignmentContext.getChild(2).accept(this));
            }
        }
        if (dmlTargetIndex + 2 <= ctx.getChildCount()) {
            updateStatement.applyPredicate(visitWhereClause((HqlParser.WhereClauseContext) ctx.getChild(dmlTargetIndex + 2)));
        }
        return updateStatement;
    } finally {
        processingStateStack.pop();
    }
}
Also used : SqmUpdateStatement(org.hibernate.query.sqm.tree.update.SqmUpdateStatement) 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) SqmPolymorphicRootDescriptor(org.hibernate.query.sqm.tree.domain.SqmPolymorphicRootDescriptor) SqmDmlCreationProcessingState(org.hibernate.query.sqm.internal.SqmDmlCreationProcessingState) ParseTree(org.antlr.v4.runtime.tree.ParseTree) SemanticException(org.hibernate.query.SemanticException)

Example 2 with SqmDmlCreationProcessingState

use of org.hibernate.query.sqm.internal.SqmDmlCreationProcessingState in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitInsertStatement.

@Override
public SqmInsertStatement<R> visitInsertStatement(HqlParser.InsertStatementContext 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 HqlParser.TargetFieldsContext targetFieldsSpecContext = (HqlParser.TargetFieldsContext) ctx.getChild(dmlTargetIndex + 1);
    final SqmRoot<R> root = visitTargetEntity(dmlTargetContext);
    if (root.getReferencedPathSource() instanceof SqmPolymorphicRootDescriptor<?>) {
        throw new SemanticException(String.format("Target type '%s' in insert statement is not an entity", root.getReferencedPathSource().getHibernateEntityName()));
    }
    final HqlParser.QueryExpressionContext queryExpressionContext = ctx.queryExpression();
    if (queryExpressionContext != null) {
        final SqmInsertSelectStatement<R> insertStatement = new SqmInsertSelectStatement<>(root, creationContext.getNodeBuilder());
        parameterCollector = insertStatement;
        final SqmDmlCreationProcessingState processingState = new SqmDmlCreationProcessingState(insertStatement, this);
        processingStateStack.push(processingState);
        try {
            queryExpressionContext.accept(this);
            final SqmCreationProcessingState stateFieldsProcessingState = new SqmCreationProcessingStateImpl(insertStatement, this);
            stateFieldsProcessingState.getPathRegistry().register(root);
            processingStateStack.push(stateFieldsProcessingState);
            try {
                for (HqlParser.SimplePathContext stateFieldCtx : targetFieldsSpecContext.simplePath()) {
                    final SqmPath<?> stateField = (SqmPath<?>) visitSimplePath(stateFieldCtx);
                    // todo : validate each resolved stateField...
                    insertStatement.addInsertTargetStateField(stateField);
                }
            } finally {
                processingStateStack.pop();
            }
            return insertStatement;
        } finally {
            processingStateStack.pop();
        }
    } else {
        final SqmInsertValuesStatement<R> insertStatement = new SqmInsertValuesStatement<>(root, creationContext.getNodeBuilder());
        parameterCollector = insertStatement;
        final SqmDmlCreationProcessingState processingState = new SqmDmlCreationProcessingState(insertStatement, this);
        processingStateStack.push(processingState);
        processingState.getPathRegistry().register(root);
        try {
            final HqlParser.ValuesListContext valuesListContext = ctx.valuesList();
            for (int i = 1; i < valuesListContext.getChildCount(); i += 2) {
                final ParseTree values = valuesListContext.getChild(i);
                final SqmValues sqmValues = new SqmValues();
                for (int j = 1; j < values.getChildCount(); j += 2) {
                    sqmValues.getExpressions().add((SqmExpression<?>) values.getChild(j).accept(this));
                }
                insertStatement.getValuesList().add(sqmValues);
            }
            for (HqlParser.SimplePathContext stateFieldCtx : targetFieldsSpecContext.simplePath()) {
                final SqmPath<?> stateField = (SqmPath<?>) visitSimplePath(stateFieldCtx);
                // todo : validate each resolved stateField...
                insertStatement.addInsertTargetStateField(stateField);
            }
            return insertStatement;
        } finally {
            processingStateStack.pop();
        }
    }
}
Also used : SqmInsertValuesStatement(org.hibernate.query.sqm.tree.insert.SqmInsertValuesStatement) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) 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) SqmCreationProcessingState(org.hibernate.query.hql.spi.SqmCreationProcessingState) HqlParser(org.hibernate.grammars.hql.HqlParser) SqmValues(org.hibernate.query.sqm.tree.insert.SqmValues) SqmPolymorphicRootDescriptor(org.hibernate.query.sqm.tree.domain.SqmPolymorphicRootDescriptor) SqmInsertSelectStatement(org.hibernate.query.sqm.tree.insert.SqmInsertSelectStatement) SqmDmlCreationProcessingState(org.hibernate.query.sqm.internal.SqmDmlCreationProcessingState) SqmCreationProcessingStateImpl(org.hibernate.query.sqm.internal.SqmCreationProcessingStateImpl) ParseTree(org.antlr.v4.runtime.tree.ParseTree) SemanticException(org.hibernate.query.SemanticException)

Example 3 with SqmDmlCreationProcessingState

use of org.hibernate.query.sqm.internal.SqmDmlCreationProcessingState 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

HqlParser (org.hibernate.grammars.hql.HqlParser)3 IDENTIFIER (org.hibernate.grammars.hql.HqlParser.IDENTIFIER)3 DAY_OF_YEAR (org.hibernate.query.sqm.TemporalUnit.DAY_OF_YEAR)3 TIMEZONE_HOUR (org.hibernate.query.sqm.TemporalUnit.TIMEZONE_HOUR)3 WEEK_OF_YEAR (org.hibernate.query.sqm.TemporalUnit.WEEK_OF_YEAR)3 SqmDmlCreationProcessingState (org.hibernate.query.sqm.internal.SqmDmlCreationProcessingState)3 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 SemanticException (org.hibernate.query.SemanticException)2 SqmPolymorphicRootDescriptor (org.hibernate.query.sqm.tree.domain.SqmPolymorphicRootDescriptor)2 SqmCreationProcessingState (org.hibernate.query.hql.spi.SqmCreationProcessingState)1 SqmCreationProcessingStateImpl (org.hibernate.query.sqm.internal.SqmCreationProcessingStateImpl)1 SqmDeleteStatement (org.hibernate.query.sqm.tree.delete.SqmDeleteStatement)1 SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)1 SqmInsertSelectStatement (org.hibernate.query.sqm.tree.insert.SqmInsertSelectStatement)1 SqmInsertValuesStatement (org.hibernate.query.sqm.tree.insert.SqmInsertValuesStatement)1 SqmValues (org.hibernate.query.sqm.tree.insert.SqmValues)1 SqmUpdateStatement (org.hibernate.query.sqm.tree.update.SqmUpdateStatement)1