Search in sources :

Example 1 with IsNullPredicate

use of com.blazebit.persistence.parser.predicate.IsNullPredicate in project blaze-persistence by Blazebit.

the class OrderByManager method buildImplicitGroupByClauses.

/**
 * Builds the clauses needed for the group by clause for a query that uses aggregate functions to work.
 *
 * @return
 */
void buildImplicitGroupByClauses(GroupByManager groupByManager, boolean hasGroupBy, JoinVisitor joinVisitor) {
    if (orderByInfos.isEmpty()) {
        return;
    }
    SimpleQueryGenerator.BooleanLiteralRenderingContext oldBooleanLiteralRenderingContext = queryGenerator.setBooleanLiteralRenderingContext(SimpleQueryGenerator.BooleanLiteralRenderingContext.CASE_WHEN);
    StringBuilder sb = new StringBuilder();
    List<OrderByInfo> infos = orderByInfos;
    boolean hasFullJoin = !jpaProvider.supportsNullPrecedenceExpression() && joinManager.hasFullJoin();
    int size = infos.size();
    for (int i = 0; i < size; i++) {
        final OrderByInfo orderByInfo = infos.get(i);
        String potentialSelectAlias = orderByInfo.getExpressionString();
        AliasInfo aliasInfo = aliasManager.getAliasInfo(potentialSelectAlias);
        Expression expr;
        if (aliasInfo instanceof SelectInfo) {
            SelectInfo selectInfo = (SelectInfo) aliasInfo;
            expr = selectInfo.getExpression();
        } else {
            expr = orderByInfo.getExpression();
        }
        Set<Expression> extractedGroupByExpressions = groupByExpressionGatheringVisitor.extractGroupByExpressions(expr, getClauseType());
        if (!extractedGroupByExpressions.isEmpty()) {
            queryGenerator.setClauseType(ClauseType.GROUP_BY);
            queryGenerator.setQueryBuffer(sb);
            for (Expression extractedExpression : extractedGroupByExpressions) {
                sb.setLength(0);
                queryGenerator.generate(extractedExpression);
                String expressionString = sb.toString();
                if (!jpaProvider.supportsNullPrecedenceExpression()) {
                    boolean nullable = hasFullJoin || ExpressionUtils.isNullable(metamodel, functionalDependencyAnalyzerVisitor.getConstantifiedJoinNodeAttributeCollector(), extractedExpression);
                    Expression resultExpression;
                    Expression defaultExpression;
                    if (nullable) {
                        sb.insert(0, "CASE WHEN ");
                        sb.append(" IS NULL THEN ");
                        if (orderByInfo.nullFirst) {
                            resultExpression = new NumericLiteral("0", NumericType.INTEGER);
                            defaultExpression = new NumericLiteral("1", NumericType.INTEGER);
                            sb.append("0 ELSE 1 END");
                        } else {
                            resultExpression = new NumericLiteral("1", NumericType.INTEGER);
                            defaultExpression = new NumericLiteral("0", NumericType.INTEGER);
                            sb.append("1 ELSE 0 END");
                        }
                        List<WhenClauseExpression> whenClauses = new ArrayList<>(1);
                        whenClauses.add(new WhenClauseExpression(new IsNullPredicate(extractedExpression.copy(ExpressionCopyContext.CLONE)), resultExpression));
                        Expression nullEmulationExpression = new GeneralCaseExpression(whenClauses, defaultExpression);
                        String nullPrecedenceEmulationExpression = sb.toString();
                        groupByManager.collect(new ResolvedExpression(nullPrecedenceEmulationExpression, nullEmulationExpression), ClauseType.ORDER_BY, hasGroupBy, joinVisitor);
                    }
                }
                groupByManager.collect(new ResolvedExpression(expressionString, extractedExpression), ClauseType.ORDER_BY, hasGroupBy, joinVisitor);
            }
            queryGenerator.setClauseType(null);
        }
    }
    queryGenerator.setBooleanLiteralRenderingContext(oldBooleanLiteralRenderingContext);
    groupByExpressionGatheringVisitor.clear();
}
Also used : WhenClauseExpression(com.blazebit.persistence.parser.expression.WhenClauseExpression) NumericLiteral(com.blazebit.persistence.parser.expression.NumericLiteral) ArrayList(java.util.ArrayList) WhenClauseExpression(com.blazebit.persistence.parser.expression.WhenClauseExpression) Expression(com.blazebit.persistence.parser.expression.Expression) GeneralCaseExpression(com.blazebit.persistence.parser.expression.GeneralCaseExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) IsNullPredicate(com.blazebit.persistence.parser.predicate.IsNullPredicate) GeneralCaseExpression(com.blazebit.persistence.parser.expression.GeneralCaseExpression) SimpleQueryGenerator(com.blazebit.persistence.parser.SimpleQueryGenerator)

Example 2 with IsNullPredicate

use of com.blazebit.persistence.parser.predicate.IsNullPredicate in project blaze-persistence by Blazebit.

the class GeneralParserTest method testIsNull.

@Test
public void testIsNull() {
    GeneralCaseExpression result = (GeneralCaseExpression) parse("CASE WHEN a.x IS NULL THEN 0 ELSE 1 END");
    GeneralCaseExpression expected = new GeneralCaseExpression(Arrays.asList(new WhenClauseExpression(new IsNullPredicate(path("a", "x")), _int("0"))), _int("1"));
    assertEquals(expected, result);
}
Also used : IsNullPredicate(com.blazebit.persistence.parser.predicate.IsNullPredicate) Test(org.junit.Test)

Example 3 with IsNullPredicate

use of com.blazebit.persistence.parser.predicate.IsNullPredicate in project blaze-persistence by Blazebit.

the class GeneralParserTest method testIsNotNull.

@Test
public void testIsNotNull() {
    GeneralCaseExpression result = (GeneralCaseExpression) parse("CASE WHEN a.x IS NOT NULL THEN 0 ELSE 1 END");
    GeneralCaseExpression expected = new GeneralCaseExpression(Arrays.asList(new WhenClauseExpression(new IsNullPredicate(path("a", "x"), true), _int("0"))), _int("1"));
    assertEquals(expected, result);
}
Also used : IsNullPredicate(com.blazebit.persistence.parser.predicate.IsNullPredicate) Test(org.junit.Test)

Example 4 with IsNullPredicate

use of com.blazebit.persistence.parser.predicate.IsNullPredicate in project blaze-persistence by Blazebit.

the class EqualityCheckingVisitor method visit.

@Override
public Boolean visit(IsNullPredicate predicate) {
    if (referenceExpression.getClass() != predicate.getClass()) {
        return Boolean.TRUE;
    }
    IsNullPredicate referencePredicate = (IsNullPredicate) referenceExpression;
    if (referencePredicate.isNegated() != predicate.isNegated()) {
        return Boolean.TRUE;
    }
    referenceExpression = referencePredicate.getExpression();
    return predicate.getExpression().accept(this);
}
Also used : IsNullPredicate(com.blazebit.persistence.parser.predicate.IsNullPredicate)

Example 5 with IsNullPredicate

use of com.blazebit.persistence.parser.predicate.IsNullPredicate in project blaze-persistence by Blazebit.

the class GeneralParserTest method testTreatPredicate1.

@Test
public void testTreatPredicate1() {
    Predicate result = parsePredicate("TREAT(d AS GoodDocument).name IS NOT NULL", false);
    Predicate expected = new IsNullPredicate(path(treat(path("d"), "GoodDocument"), "name"), true);
    assertEquals(expected, result);
}
Also used : IsNullPredicate(com.blazebit.persistence.parser.predicate.IsNullPredicate) BetweenPredicate(com.blazebit.persistence.parser.predicate.BetweenPredicate) EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate) GtPredicate(com.blazebit.persistence.parser.predicate.GtPredicate) LtPredicate(com.blazebit.persistence.parser.predicate.LtPredicate) IsNullPredicate(com.blazebit.persistence.parser.predicate.IsNullPredicate) InPredicate(com.blazebit.persistence.parser.predicate.InPredicate) Predicate(com.blazebit.persistence.parser.predicate.Predicate) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) LikePredicate(com.blazebit.persistence.parser.predicate.LikePredicate) MemberOfPredicate(com.blazebit.persistence.parser.predicate.MemberOfPredicate) Test(org.junit.Test)

Aggregations

IsNullPredicate (com.blazebit.persistence.parser.predicate.IsNullPredicate)5 Test (org.junit.Test)3 SimpleQueryGenerator (com.blazebit.persistence.parser.SimpleQueryGenerator)1 Expression (com.blazebit.persistence.parser.expression.Expression)1 FunctionExpression (com.blazebit.persistence.parser.expression.FunctionExpression)1 GeneralCaseExpression (com.blazebit.persistence.parser.expression.GeneralCaseExpression)1 NumericLiteral (com.blazebit.persistence.parser.expression.NumericLiteral)1 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)1 PropertyExpression (com.blazebit.persistence.parser.expression.PropertyExpression)1 WhenClauseExpression (com.blazebit.persistence.parser.expression.WhenClauseExpression)1 BetweenPredicate (com.blazebit.persistence.parser.predicate.BetweenPredicate)1 CompoundPredicate (com.blazebit.persistence.parser.predicate.CompoundPredicate)1 EqPredicate (com.blazebit.persistence.parser.predicate.EqPredicate)1 GtPredicate (com.blazebit.persistence.parser.predicate.GtPredicate)1 InPredicate (com.blazebit.persistence.parser.predicate.InPredicate)1 LikePredicate (com.blazebit.persistence.parser.predicate.LikePredicate)1 LtPredicate (com.blazebit.persistence.parser.predicate.LtPredicate)1 MemberOfPredicate (com.blazebit.persistence.parser.predicate.MemberOfPredicate)1 Predicate (com.blazebit.persistence.parser.predicate.Predicate)1 ArrayList (java.util.ArrayList)1