Search in sources :

Example 6 with SqmComparisonPredicate

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());
}
Also used : ComparisonOperator(org.hibernate.query.sqm.ComparisonOperator) SqmNullnessPredicate(org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) SqmLiteralNull(org.hibernate.query.sqm.tree.expression.SqmLiteralNull) HqlParser(org.hibernate.grammars.hql.HqlParser) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) Map(java.util.Map)

Example 7 with SqmComparisonPredicate

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());
}
Also used : ComparisonOperator(org.hibernate.query.sqm.ComparisonOperator) 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) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)

Example 8 with SqmComparisonPredicate

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();
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) BigInteger(java.math.BigInteger) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel) Test(org.junit.jupiter.api.Test)

Example 9 with SqmComparisonPredicate

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");
}
Also used : SqmSelection(org.hibernate.query.sqm.tree.select.SqmSelection) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) SqmEntityValuedSimplePath(org.hibernate.query.sqm.tree.domain.SqmEntityValuedSimplePath) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType) BaseSqmUnitTest(org.hibernate.orm.test.query.sqm.BaseSqmUnitTest) Test(org.junit.jupiter.api.Test)

Example 10 with SqmComparisonPredicate

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

SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)12 Test (org.junit.jupiter.api.Test)8 BaseSqmUnitTest (org.hibernate.orm.test.query.sqm.BaseSqmUnitTest)7 SqmLiteral (org.hibernate.query.sqm.tree.expression.SqmLiteral)5 SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)3 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)3 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)2 ComparisonOperator (org.hibernate.query.sqm.ComparisonOperator)2 SqmPredicate (org.hibernate.query.sqm.tree.predicate.SqmPredicate)2 SqmSelection (org.hibernate.query.sqm.tree.select.SqmSelection)2 EntityManager (jakarta.persistence.EntityManager)1 Expression (jakarta.persistence.criteria.Expression)1 Metamodel (jakarta.persistence.metamodel.Metamodel)1 BigInteger (java.math.BigInteger)1 Map (java.util.Map)1 HqlParser (org.hibernate.grammars.hql.HqlParser)1 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)1 JpaMetamodel (org.hibernate.metamodel.model.domain.JpaMetamodel)1 Product (org.hibernate.orm.test.jpa.metamodel.Product)1 AbstractSqmFunctionDescriptor (org.hibernate.query.sqm.function.AbstractSqmFunctionDescriptor)1