use of org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitComparisonPredicate.
@Override
public SqmPredicate visitComparisonPredicate(HqlParser.ComparisonPredicateContext ctx) {
final ComparisonOperator comparisonOperator = (ComparisonOperator) ctx.getChild(1).accept(this);
final SqmExpression<?> left;
final SqmExpression<?> right;
final HqlParser.ExpressionContext leftExpressionContext = (HqlParser.ExpressionContext) ctx.getChild(0);
final HqlParser.ExpressionContext rightExpressionContext = (HqlParser.ExpressionContext) ctx.getChild(2);
switch(comparisonOperator) {
case EQUAL:
case NOT_EQUAL:
case DISTINCT_FROM:
case NOT_DISTINCT_FROM:
{
Map<Class<?>, Enum<?>> possibleEnumValues;
if ((possibleEnumValues = getPossibleEnumValues(leftExpressionContext)) != null) {
right = (SqmExpression<?>) rightExpressionContext.accept(this);
left = resolveEnumShorthandLiteral(leftExpressionContext, possibleEnumValues, right.getJavaType());
break;
} else if ((possibleEnumValues = getPossibleEnumValues(rightExpressionContext)) != null) {
left = (SqmExpression<?>) leftExpressionContext.accept(this);
right = resolveEnumShorthandLiteral(rightExpressionContext, possibleEnumValues, left.getJavaType());
break;
}
left = (SqmExpression<?>) leftExpressionContext.accept(this);
right = (SqmExpression<?>) rightExpressionContext.accept(this);
// This is something that we used to support before 6 which is also used in our testsuite
if (left instanceof SqmLiteralNull<?>) {
return new SqmNullnessPredicate(right, comparisonOperator == ComparisonOperator.NOT_EQUAL || comparisonOperator == ComparisonOperator.DISTINCT_FROM, creationContext.getNodeBuilder());
} else if (right instanceof SqmLiteralNull<?>) {
return new SqmNullnessPredicate(left, comparisonOperator == ComparisonOperator.NOT_EQUAL || comparisonOperator == ComparisonOperator.DISTINCT_FROM, creationContext.getNodeBuilder());
}
break;
}
default:
{
left = (SqmExpression<?>) leftExpressionContext.accept(this);
right = (SqmExpression<?>) rightExpressionContext.accept(this);
break;
}
}
return new SqmComparisonPredicate(left, comparisonOperator, right, creationContext.getNodeBuilder());
}
use of org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitComparisonPredicate.
@Override
public ComparisonPredicate visitComparisonPredicate(SqmComparisonPredicate predicate) {
final FromClauseIndex fromClauseIndex = fromClauseIndexStack.getCurrent();
inferrableTypeAccessStack.push(() -> determineValueMapping(predicate.getRightHandExpression(), fromClauseIndex));
final Expression lhs;
try {
lhs = (Expression) predicate.getLeftHandExpression().accept(this);
} finally {
inferrableTypeAccessStack.pop();
}
inferrableTypeAccessStack.push(() -> determineValueMapping(predicate.getLeftHandExpression(), fromClauseIndex));
final Expression rhs;
try {
rhs = (Expression) predicate.getRightHandExpression().accept(this);
} finally {
inferrableTypeAccessStack.pop();
}
ComparisonOperator sqmOperator = predicate.getSqmOperator();
if (predicate.isNegated()) {
sqmOperator = sqmOperator.negated();
}
return new ComparisonPredicate(lhs, sqmOperator, rhs, getBooleanType());
}
use of org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate in project hibernate-orm by hibernate.
the class EqualityComparisonTest method testEqualityComparisonLiteralConversion.
@Test
public void testEqualityComparisonLiteralConversion(EntityManagerFactoryScope scope) {
scope.inTransaction(entityManager -> {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
JpaMetamodel mm = (JpaMetamodel) entityManager.getMetamodel();
CriteriaQuery<Integer> cquery = cb.createQuery(Integer.class);
Root<Product> product = cquery.from(Product.class);
EntityType<Product> Product_ = mm.entity(Product.class);
cquery.select(cb.toInteger(product.get(Product_.getSingularAttribute("quantity", Integer.class))));
SqmComparisonPredicate predicate = (SqmComparisonPredicate) cb.equal(product.get(Product_.getSingularAttribute("partNumber", Long.class)), 373767373);
Assert.assertEquals(Long.class, predicate.getLeftHandExpression().getJavaType());
cquery.where(predicate);
entityManager.createQuery(cquery).getResultList();
predicate = (SqmComparisonPredicate) cb.ge(cb.length(product.get(Product_.getSingularAttribute("name", String.class))), 4L);
Assert.assertEquals(Integer.class, predicate.getLeftHandExpression().getJavaType());
cquery.where(predicate);
entityManager.createQuery(cquery).getResultList();
});
}
use of org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate in project hibernate-orm by hibernate.
the class AttributePathTests method testImplicitJoinReuse2.
@Test
public void testImplicitJoinReuse2() {
final SqmSelectStatement<?> statement = interpretSelect("select s.mate from Person s where s.mate.dob = ?1");
assertThat(statement.getQuerySpec().getFromClause().getRoots().size(), is(1));
final SqmRoot<?> sqmRoot = statement.getQuerySpec().getFromClause().getRoots().get(0);
assertThat(sqmRoot.getJoins().size(), is(0));
assertThat(sqmRoot.getReusablePaths().size(), is(1));
final SqmSelection selection = statement.getQuerySpec().getSelectClause().getSelections().get(0);
assertThat(selection.getSelectableNode(), instanceOf(SqmEntityValuedSimplePath.class));
final SqmPath selectExpression = (SqmPath) selection.getSelectableNode();
assertThat(selectExpression.getReferencedPathSource().getSqmPathType(), instanceOf(EntityDomainType.class));
final SqmComparisonPredicate predicate = (SqmComparisonPredicate) statement.getQuerySpec().getWhereClause().getPredicate();
final SqmPath predicateLhs = (SqmPath) predicate.getLeftHandExpression();
assertThat(predicateLhs.getLhs(), notNullValue());
// from-clause paths
// assertPropertyPath( space.getRoot(), "com.acme.Something(s)" );
// assertPropertyPath( space.getJoins().get( 0 ), "com.acme.Something(s).entity" );
// expression paths
assertPropertyPath((SqmExpression) selection.getSelectableNode(), Person.class.getName() + "(s).mate");
assertPropertyPath(predicateLhs, Person.class.getName() + "(s).mate.dob");
}
use of org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate 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