Search in sources :

Example 1 with SqmCaseSearched

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

the class CaseExpressionsTest method testBasicSearchedCaseExpression.

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

Example 2 with SqmCaseSearched

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

the class SemanticQueryBuilder method visitSearchedCaseList.

@Override
public SqmCaseSearched<?> visitSearchedCaseList(HqlParser.SearchedCaseListContext ctx) {
    final int size = ctx.getChildCount();
    final SqmCaseSearched<Object> caseExpression = new SqmCaseSearched<>(null, size - 2, creationContext.getNodeBuilder());
    for (int i = 1; i < size; i++) {
        final ParseTree parseTree = ctx.getChild(i);
        if (parseTree instanceof HqlParser.SearchedCaseWhenContext) {
            // noinspection unchecked
            caseExpression.when((SqmPredicate) 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 : SqmCaseSearched(org.hibernate.query.sqm.tree.expression.SqmCaseSearched) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 3 with SqmCaseSearched

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

the class BaseSqmToSqlAstConverter method visitSearchedCaseExpression.

@Override
public CaseSearchedExpression visitSearchedCaseExpression(SqmCaseSearched<?> expression) {
    final List<CaseSearchedExpression.WhenFragment> whenFragments = new ArrayList<>(expression.getWhenFragments().size());
    final Supplier<MappingModelExpressible<?>> inferenceSupplier = inferrableTypeAccessStack.getCurrent();
    MappingModelExpressible<?> resolved = determineCurrentExpressible(expression);
    Expression otherwise = null;
    for (SqmCaseSearched.WhenFragment<?> whenFragment : expression.getWhenFragments()) {
        inferrableTypeAccessStack.push(() -> null);
        final Predicate whenPredicate = visitNestedTopLevelPredicate(whenFragment.getPredicate());
        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 CaseSearchedExpression.WhenFragment(whenPredicate, 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 CaseSearchedExpression(resolved, whenFragments, otherwise);
}
Also used : 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) ArrayList(java.util.ArrayList) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SqmCaseSearched(org.hibernate.query.sqm.tree.expression.SqmCaseSearched) 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)

Aggregations

SqmCaseSearched (org.hibernate.query.sqm.tree.expression.SqmCaseSearched)3 SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)2 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 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 SqmAndPredicate (org.hibernate.query.sqm.tree.predicate.SqmAndPredicate)1 SqmBetweenPredicate (org.hibernate.query.sqm.tree.predicate.SqmBetweenPredicate)1 SqmBooleanExpressionPredicate (org.hibernate.query.sqm.tree.predicate.SqmBooleanExpressionPredicate)1 SqmEmptinessPredicate (org.hibernate.query.sqm.tree.predicate.SqmEmptinessPredicate)1 SqmExistsPredicate (org.hibernate.query.sqm.tree.predicate.SqmExistsPredicate)1 SqmGroupedPredicate (org.hibernate.query.sqm.tree.predicate.SqmGroupedPredicate)1 SqmInListPredicate (org.hibernate.query.sqm.tree.predicate.SqmInListPredicate)1 SqmInSubQueryPredicate (org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate)1 SqmLikePredicate (org.hibernate.query.sqm.tree.predicate.SqmLikePredicate)1