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