Search in sources :

Example 1 with SqmAssignment

use of org.hibernate.query.sqm.tree.update.SqmAssignment in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitSetClause.

@Override
public List<Assignment> visitSetClause(SqmSetClause setClause) {
    final List<Assignment> assignments = new ArrayList<>(setClause.getAssignments().size());
    for (SqmAssignment sqmAssignment : setClause.getAssignments()) {
        final List<ColumnReference> targetColumnReferences = new ArrayList<>();
        pushProcessingState(new SqlAstProcessingStateImpl(getCurrentProcessingState(), this, getCurrentClauseStack()::getCurrent) {

            @Override
            public Expression resolveSqlExpression(String key, Function<SqlAstProcessingState, Expression> creator) {
                final Expression expression = getParentState().getSqlExpressionResolver().resolveSqlExpression(key, creator);
                assert expression instanceof ColumnReference;
                targetColumnReferences.add((ColumnReference) expression);
                return expression;
            }
        }, getFromClauseIndex());
        final SqmPathInterpretation<?> assignedPathInterpretation;
        try {
            assignedPathInterpretation = (SqmPathInterpretation<?>) sqmAssignment.getTargetPath().accept(this);
        } finally {
            popProcessingStateStack();
        }
        inferrableTypeAccessStack.push(assignedPathInterpretation::getExpressionType);
        // final List<ColumnReference> valueColumnReferences = new ArrayList<>();
        pushProcessingState(new SqlAstProcessingStateImpl(getCurrentProcessingState(), this, getCurrentClauseStack()::getCurrent) {

            @Override
            public Expression resolveSqlExpression(String key, Function<SqlAstProcessingState, Expression> creator) {
                final Expression expression = getParentState().getSqlExpressionResolver().resolveSqlExpression(key, creator);
                assert expression instanceof ColumnReference;
                // valueColumnReferences.add( (ColumnReference) expression );
                return expression;
            }
        }, getFromClauseIndex());
        try {
            final SqmExpression<?> assignmentValue = sqmAssignment.getValue();
            final SqmParameter<?> assignmentValueParameter = getSqmParameter(assignmentValue);
            if (assignmentValueParameter != null) {
                consumeSqmParameter(assignmentValueParameter, assignedPathInterpretation.getExpressionType(), (index, jdbcParameter) -> assignments.add(new Assignment(targetColumnReferences.get(index), jdbcParameter)));
            } else {
                final Expression valueExpression = (Expression) assignmentValue.accept(this);
                final int valueExprJdbcCount = getKeyExpressible(valueExpression.getExpressionType()).getJdbcTypeCount();
                final int assignedPathJdbcCount = getKeyExpressible(assignedPathInterpretation.getExpressionType()).getJdbcTypeCount();
                if (valueExprJdbcCount != assignedPathJdbcCount) {
                    SqlTreeCreationLogger.LOGGER.debugf("JDBC type count does not match in UPDATE assignment between the assigned-path and the assigned-value; " + "this will likely lead to problems executing the query");
                }
                assert assignedPathJdbcCount == valueExprJdbcCount;
                for (ColumnReference columnReference : targetColumnReferences) {
                    assignments.add(new Assignment(columnReference, valueExpression));
                }
            }
        } finally {
            popProcessingStateStack();
            inferrableTypeAccessStack.pop();
        }
    }
    return assignments;
}
Also used : ArrayList(java.util.ArrayList) SqlAstProcessingStateImpl(org.hibernate.query.sqm.sql.internal.SqlAstProcessingStateImpl) Assignment(org.hibernate.sql.ast.tree.update.Assignment) SqmAssignment(org.hibernate.query.sqm.tree.update.SqmAssignment) SqlAstProcessingState(org.hibernate.sql.ast.spi.SqlAstProcessingState) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SqmModifiedSubQueryExpression(org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) SelfRenderingAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SelfRenderingSqlFragmentExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) SqmAssignment(org.hibernate.query.sqm.tree.update.SqmAssignment) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Aggregations

ArrayList (java.util.ArrayList)1 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)1 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)1 SqlAstProcessingStateImpl (org.hibernate.query.sqm.sql.internal.SqlAstProcessingStateImpl)1 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)1 SqmModifiedSubQueryExpression (org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression)1 SqmAssignment (org.hibernate.query.sqm.tree.update.SqmAssignment)1 SqlAstProcessingState (org.hibernate.sql.ast.spi.SqlAstProcessingState)1 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)1 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)1 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)1 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)1 Expression (org.hibernate.sql.ast.tree.expression.Expression)1 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)1 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)1 SelfRenderingSqlFragmentExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression)1 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)1 Assignment (org.hibernate.sql.ast.tree.update.Assignment)1