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);
}
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;
}
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));
}
Aggregations