Search in sources :

Example 1 with CaseSearchedExpression

use of org.hibernate.sql.ast.tree.expression.CaseSearchedExpression in project hibernate-orm by hibernate.

the class DB2SqlAstTranslator method visitAnsiCaseSearchedExpression.

// DB2 does not allow CASE expressions where all result arms contain plain parameters.
// At least one result arm must provide some type context for inference,
// so we cast the first result arm if we encounter this condition
@Override
protected void visitAnsiCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression, Consumer<Expression> resultRenderer) {
    if (getParameterRenderingMode() == SqlAstNodeRenderingMode.DEFAULT && areAllResultsParameters(caseSearchedExpression)) {
        final List<CaseSearchedExpression.WhenFragment> whenFragments = caseSearchedExpression.getWhenFragments();
        final Expression firstResult = whenFragments.get(0).getResult();
        super.visitAnsiCaseSearchedExpression(caseSearchedExpression, e -> {
            if (e == firstResult) {
                renderCasted(e);
            } else {
                resultRenderer.accept(e);
            }
        });
    } else {
        super.visitAnsiCaseSearchedExpression(caseSearchedExpression, resultRenderer);
    }
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)

Example 2 with CaseSearchedExpression

use of org.hibernate.sql.ast.tree.expression.CaseSearchedExpression in project hibernate-orm by hibernate.

the class DerbySqlAstTranslator method visitAnsiCaseSearchedExpression.

// Derby does not allow CASE expressions where all result arms contain plain parameters.
// At least one result arm must provide some type context for inference,
// so we cast the first result arm if we encounter this condition
@Override
protected void visitAnsiCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression, Consumer<Expression> resultRenderer) {
    if (getParameterRenderingMode() == SqlAstNodeRenderingMode.DEFAULT && areAllResultsParameters(caseSearchedExpression)) {
        final List<CaseSearchedExpression.WhenFragment> whenFragments = caseSearchedExpression.getWhenFragments();
        final Expression firstResult = whenFragments.get(0).getResult();
        super.visitAnsiCaseSearchedExpression(caseSearchedExpression, e -> {
            if (e == firstResult) {
                renderCasted(e);
            } else {
                resultRenderer.accept(e);
            }
        });
    } else {
        super.visitAnsiCaseSearchedExpression(caseSearchedExpression, resultRenderer);
    }
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)

Example 3 with CaseSearchedExpression

use of org.hibernate.sql.ast.tree.expression.CaseSearchedExpression in project hibernate-orm by hibernate.

the class AbstractSqlAstTranslator method visitAnsiCaseSearchedExpression.

protected void visitAnsiCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression, Consumer<Expression> resultRenderer) {
    appendSql("case");
    final SqlAstNodeRenderingMode original = this.parameterRenderingMode;
    for (CaseSearchedExpression.WhenFragment whenFragment : caseSearchedExpression.getWhenFragments()) {
        if (original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS) {
            this.parameterRenderingMode = SqlAstNodeRenderingMode.DEFAULT;
        }
        appendSql(" when ");
        whenFragment.getPredicate().accept(this);
        this.parameterRenderingMode = original;
        appendSql(" then ");
        resultRenderer.accept(whenFragment.getResult());
    }
    final Expression otherwise = caseSearchedExpression.getOtherwise();
    if (otherwise != null) {
        appendSql(" else ");
        resultRenderer.accept(otherwise);
    }
    appendSql(" end");
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) OrderedSetAggregateFunctionExpression(org.hibernate.sql.ast.tree.expression.OrderedSetAggregateFunctionExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SqlAstNodeRenderingMode(org.hibernate.sql.ast.SqlAstNodeRenderingMode)

Example 4 with CaseSearchedExpression

use of org.hibernate.sql.ast.tree.expression.CaseSearchedExpression in project hibernate-orm by hibernate.

the class AbstractSqlAstTranslator method visitDecodeCaseSearchedExpression.

protected void visitDecodeCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression) {
    appendSql("decode( ");
    final SqlAstNodeRenderingMode original = this.parameterRenderingMode;
    final List<CaseSearchedExpression.WhenFragment> whenFragments = caseSearchedExpression.getWhenFragments();
    final int caseNumber = whenFragments.size();
    CaseSearchedExpression.WhenFragment firstWhenFragment = null;
    for (int i = 0; i < caseNumber; i++) {
        final CaseSearchedExpression.WhenFragment whenFragment = whenFragments.get(i);
        Predicate predicate = whenFragment.getPredicate();
        if (original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS) {
            this.parameterRenderingMode = SqlAstNodeRenderingMode.DEFAULT;
        }
        if (i != 0) {
            appendSql(',');
            getLeftHandExpression(predicate).accept(this);
            this.parameterRenderingMode = original;
            appendSql(',');
            whenFragment.getResult().accept(this);
        } else {
            getLeftHandExpression(predicate).accept(this);
            firstWhenFragment = whenFragment;
        }
    }
    this.parameterRenderingMode = original;
    appendSql(',');
    firstWhenFragment.getResult().accept(this);
    final Expression otherwise = caseSearchedExpression.getOtherwise();
    if (otherwise != null) {
        appendSql(',');
        otherwise.accept(this);
    }
    appendSql(CLOSE_PARENTHESIS);
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) OrderedSetAggregateFunctionExpression(org.hibernate.sql.ast.tree.expression.OrderedSetAggregateFunctionExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SqlAstNodeRenderingMode(org.hibernate.sql.ast.SqlAstNodeRenderingMode) SqlFragmentPredicate(org.hibernate.persister.internal.SqlFragmentPredicate) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) SelfRenderingPredicate(org.hibernate.sql.ast.tree.predicate.SelfRenderingPredicate) FilterPredicate(org.hibernate.sql.ast.tree.predicate.FilterPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) LikePredicate(org.hibernate.sql.ast.tree.predicate.LikePredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) BetweenPredicate(org.hibernate.sql.ast.tree.predicate.BetweenPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) GroupedPredicate(org.hibernate.sql.ast.tree.predicate.GroupedPredicate) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) ExistsPredicate(org.hibernate.sql.ast.tree.predicate.ExistsPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate)

Example 5 with CaseSearchedExpression

use of org.hibernate.sql.ast.tree.expression.CaseSearchedExpression in project hibernate-orm by hibernate.

the class SybaseASESqlAstTranslator method visitAnsiCaseSearchedExpression.

// Sybase ASE does not allow CASE expressions where all result arms contain plain parameters.
// At least one result arm must provide some type context for inference,
// so we cast the first result arm if we encounter this condition
@Override
protected void visitAnsiCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression, Consumer<Expression> resultRenderer) {
    if (getParameterRenderingMode() == SqlAstNodeRenderingMode.DEFAULT && areAllResultsParameters(caseSearchedExpression)) {
        final List<CaseSearchedExpression.WhenFragment> whenFragments = caseSearchedExpression.getWhenFragments();
        final Expression firstResult = whenFragments.get(0).getResult();
        super.visitAnsiCaseSearchedExpression(caseSearchedExpression, e -> {
            if (e == firstResult) {
                renderCasted(e);
            } else {
                resultRenderer.accept(e);
            }
        });
    } else {
        super.visitAnsiCaseSearchedExpression(caseSearchedExpression, resultRenderer);
    }
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)

Aggregations

CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)12 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)10 Expression (org.hibernate.sql.ast.tree.expression.Expression)10 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)6 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)5 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)4 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)4 OrderedSetAggregateFunctionExpression (org.hibernate.sql.ast.tree.expression.OrderedSetAggregateFunctionExpression)3 NullnessPredicate (org.hibernate.sql.ast.tree.predicate.NullnessPredicate)3 Predicate (org.hibernate.sql.ast.tree.predicate.Predicate)3 ArrayList (java.util.ArrayList)2 SqlAstNodeRenderingMode (org.hibernate.sql.ast.SqlAstNodeRenderingMode)2 BetweenPredicate (org.hibernate.sql.ast.tree.predicate.BetweenPredicate)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 BasicValuedMapping (org.hibernate.metamodel.mapping.BasicValuedMapping)1 MappingModelExpressible (org.hibernate.metamodel.mapping.MappingModelExpressible)1 SqlFragmentPredicate (org.hibernate.persister.internal.SqlFragmentPredicate)1 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)1 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)1 SqmCaseSearched (org.hibernate.query.sqm.tree.expression.SqmCaseSearched)1