Search in sources :

Example 1 with SqmCaseSimple

use of org.hibernate.query.sqm.tree.expression.SqmCaseSimple in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitSimpleCaseExpression.

@Override
public CaseSimpleExpression visitSimpleCaseExpression(SqmCaseSimple<?, ?> expression) {
    final List<CaseSimpleExpression.WhenFragment> whenFragments = new ArrayList<>(expression.getWhenFragments().size());
    final Supplier<MappingModelExpressible<?>> inferenceSupplier = inferrableTypeAccessStack.getCurrent();
    inferrableTypeAccessStack.push(() -> {
        for (SqmCaseSimple.WhenFragment<?, ?> whenFragment : expression.getWhenFragments()) {
            final MappingModelExpressible<?> resolved = determineCurrentExpressible(whenFragment.getCheckValue());
            if (resolved != null) {
                return resolved;
            }
        }
        return null;
    });
    final Expression fixture = (Expression) expression.getFixture().accept(this);
    final MappingModelExpressible<?> fixtureType = (MappingModelExpressible<?>) fixture.getExpressionType();
    inferrableTypeAccessStack.pop();
    MappingModelExpressible<?> resolved = determineCurrentExpressible(expression);
    Expression otherwise = null;
    for (SqmCaseSimple.WhenFragment<?, ?> whenFragment : expression.getWhenFragments()) {
        inferrableTypeAccessStack.push(() -> fixtureType);
        final Expression checkValue = (Expression) whenFragment.getCheckValue().accept(this);
        inferrableTypeAccessStack.pop();
        final MappingModelExpressible<?> alreadyKnown = resolved;
        inferrableTypeAccessStack.push(() -> alreadyKnown == null && inferenceSupplier != null ? inferenceSupplier.get() : alreadyKnown);
        final Expression resultExpression = (Expression) whenFragment.getResult().accept(this);
        inferrableTypeAccessStack.pop();
        resolved = (MappingModelExpressible<?>) highestPrecedence(resolved, resultExpression.getExpressionType());
        whenFragments.add(new CaseSimpleExpression.WhenFragment(checkValue, resultExpression));
    }
    if (expression.getOtherwise() != null) {
        final MappingModelExpressible<?> alreadyKnown = resolved;
        inferrableTypeAccessStack.push(() -> alreadyKnown == null && inferenceSupplier != null ? inferenceSupplier.get() : alreadyKnown);
        otherwise = (Expression) expression.getOtherwise().accept(this);
        inferrableTypeAccessStack.pop();
        resolved = (MappingModelExpressible<?>) highestPrecedence(resolved, otherwise.getExpressionType());
    }
    return new CaseSimpleExpression(resolved, fixture, whenFragments, otherwise);
}
Also used : SqmCaseSimple(org.hibernate.query.sqm.tree.expression.SqmCaseSimple) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) 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) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) ArrayList(java.util.ArrayList)

Example 2 with SqmCaseSimple

use of org.hibernate.query.sqm.tree.expression.SqmCaseSimple in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitSimpleCaseList.

@Override
public SqmCaseSimple<?, ?> visitSimpleCaseList(HqlParser.SimpleCaseListContext ctx) {
    final int size = ctx.getChildCount();
    // noinspection unchecked
    final SqmCaseSimple<Object, Object> caseExpression = new SqmCaseSimple<>((SqmExpression<Object>) ctx.getChild(1).accept(this), null, size - 3, creationContext.getNodeBuilder());
    for (int i = 2; i < size; i++) {
        final ParseTree parseTree = ctx.getChild(i);
        if (parseTree instanceof HqlParser.SimpleCaseWhenContext) {
            // noinspection unchecked
            caseExpression.when((SqmExpression<Object>) parseTree.getChild(1).accept(this), (SqmExpression<Object>) parseTree.getChild(3).accept(this));
        }
    }
    final ParseTree lastChild = ctx.getChild(ctx.getChildCount() - 2);
    if (lastChild instanceof HqlParser.CaseOtherwiseContext) {
        // noinspection unchecked
        caseExpression.otherwise((SqmExpression<Object>) lastChild.getChild(1).accept(this));
    }
    return caseExpression;
}
Also used : SqmCaseSimple(org.hibernate.query.sqm.tree.expression.SqmCaseSimple) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 3 with SqmCaseSimple

use of org.hibernate.query.sqm.tree.expression.SqmCaseSimple in project hibernate-orm by hibernate.

the class CaseExpressionsTest method testBasicSimpleCaseExpression.

@Test
public void testBasicSimpleCaseExpression() {
    SqmSelectStatement<?> select = interpretSelect("select p from Person p where p.numberOfToes = case p.dob when ?1 then 6 else 8 end");
    final SqmComparisonPredicate predicate = TestingUtil.cast(select.getQuerySpec().getWhereClause().getPredicate(), SqmComparisonPredicate.class);
    final SqmCaseSimple caseStatement = TestingUtil.cast(predicate.getRightHandExpression(), SqmCaseSimple.class);
    assertThat(caseStatement.getFixture(), notNullValue());
    assertThat(caseStatement.getFixture(), instanceOf(SqmPath.class));
    assertThat(caseStatement.getOtherwise(), notNullValue());
    assertThat(caseStatement.getOtherwise(), instanceOf(SqmLiteral.class));
    assertThat(caseStatement.getWhenFragments().size(), is(1));
}
Also used : SqmCaseSimple(org.hibernate.query.sqm.tree.expression.SqmCaseSimple) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral) BaseSqmUnitTest(org.hibernate.orm.test.query.sqm.BaseSqmUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

SqmCaseSimple (org.hibernate.query.sqm.tree.expression.SqmCaseSimple)3 ArrayList (java.util.ArrayList)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 MappingModelExpressible (org.hibernate.metamodel.mapping.MappingModelExpressible)1 BaseSqmUnitTest (org.hibernate.orm.test.query.sqm.BaseSqmUnitTest)1 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)1 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)1 SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)1 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)1 SqmLiteral (org.hibernate.query.sqm.tree.expression.SqmLiteral)1 SqmModifiedSubQueryExpression (org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression)1 SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)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 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