Search in sources :

Example 1 with SqmNullnessPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate 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 2 with SqmNullnessPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate in project hibernate-orm by hibernate.

the class WhereClauseTests method testIsNotNullPredicate.

@Test
public void testIsNotNullPredicate() {
    SqmSelectStatement<?> statement = interpretSelect("select l from Person l where l.nickName is not null");
    assertThat(statement.getQuerySpec().getWhereClause().getPredicate(), instanceOf(SqmNullnessPredicate.class));
    SqmNullnessPredicate predicate = (SqmNullnessPredicate) statement.getQuerySpec().getWhereClause().getPredicate();
    assertThat(predicate.isNegated(), is(true));
}
Also used : SqmNullnessPredicate(org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate) BaseSqmUnitTest(org.hibernate.orm.test.query.sqm.BaseSqmUnitTest) Test(org.junit.jupiter.api.Test)

Example 3 with SqmNullnessPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate in project hibernate-orm by hibernate.

the class WhereClauseTests method testNotIsNullPredicate.

@Test
public void testNotIsNullPredicate() {
    SqmSelectStatement<?> statement = interpretSelect("select l from Person l where not l.nickName is null");
    assertThat(statement.getQuerySpec().getWhereClause().getPredicate(), instanceOf(SqmNullnessPredicate.class));
    SqmNullnessPredicate predicate = (SqmNullnessPredicate) statement.getQuerySpec().getWhereClause().getPredicate();
    assertThat(predicate.isNegated(), is(true));
}
Also used : SqmNullnessPredicate(org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate) BaseSqmUnitTest(org.hibernate.orm.test.query.sqm.BaseSqmUnitTest) Test(org.junit.jupiter.api.Test)

Example 4 with SqmNullnessPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate in project hibernate-orm by hibernate.

the class WhereClauseTests method testNotIsNotNullPredicate.

@Test
public void testNotIsNotNullPredicate() {
    SqmSelectStatement<?> statement = interpretSelect("select l from Person l where not l.nickName is not null");
    assertThat(statement.getQuerySpec().getWhereClause().getPredicate(), instanceOf(SqmNullnessPredicate.class));
    SqmNullnessPredicate predicate = (SqmNullnessPredicate) statement.getQuerySpec().getWhereClause().getPredicate();
    assertThat(predicate.isNegated(), is(false));
}
Also used : SqmNullnessPredicate(org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate) BaseSqmUnitTest(org.hibernate.orm.test.query.sqm.BaseSqmUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

SqmNullnessPredicate (org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate)4 BaseSqmUnitTest (org.hibernate.orm.test.query.sqm.BaseSqmUnitTest)3 Test (org.junit.jupiter.api.Test)3 Map (java.util.Map)1 HqlParser (org.hibernate.grammars.hql.HqlParser)1 ComparisonOperator (org.hibernate.query.sqm.ComparisonOperator)1 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)1 SqmLiteralNull (org.hibernate.query.sqm.tree.expression.SqmLiteralNull)1 SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)1