Search in sources :

Example 1 with WhenClauseExpression

use of com.blazebit.persistence.parser.expression.WhenClauseExpression 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 WhenClauseExpression

use of com.blazebit.persistence.parser.expression.WhenClauseExpression in project blaze-persistence by Blazebit.

the class TypeEqTest method testTypeEqPath2.

@Test
public void testTypeEqPath2() {
    entityTypes.put(Entity.class.getName(), Entity.class);
    entityTypes.put(Entity.class.getSimpleName(), Entity.class);
    GeneralCaseExpression actual = (GeneralCaseExpression) parse("CASE WHEN TYPE(a.b.c) = " + Entity.class.getName() + " THEN 0 ELSE 1 END");
    GeneralCaseExpression expected = new GeneralCaseExpression(Arrays.asList(new WhenClauseExpression(new EqPredicate(typeFunction(path("a", "b", "c")), _entity(Entity.class)), _int("0"))), _int("1"));
    assertEquals(expected, actual);
}
Also used : WhenClauseExpression(com.blazebit.persistence.parser.expression.WhenClauseExpression) GeneralCaseExpression(com.blazebit.persistence.parser.expression.GeneralCaseExpression) EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate) Test(org.junit.Test)

Example 3 with WhenClauseExpression

use of com.blazebit.persistence.parser.expression.WhenClauseExpression in project blaze-persistence by Blazebit.

the class TypeEqTest method testPathEqType1.

@Test
public void testPathEqType1() {
    entityTypes.put(Entity.class.getName(), Entity.class);
    entityTypes.put(Entity.class.getSimpleName(), Entity.class);
    GeneralCaseExpression actual = (GeneralCaseExpression) parse("CASE WHEN Entity = TYPE(d.e.f) THEN 0 ELSE 1 END");
    GeneralCaseExpression expected = new GeneralCaseExpression(Arrays.asList(new WhenClauseExpression(new EqPredicate(_entity(Entity.class), typeFunction(path("d", "e", "f"))), _int("0"))), _int("1"));
    assertEquals(expected, actual);
}
Also used : WhenClauseExpression(com.blazebit.persistence.parser.expression.WhenClauseExpression) GeneralCaseExpression(com.blazebit.persistence.parser.expression.GeneralCaseExpression) EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate) Test(org.junit.Test)

Example 4 with WhenClauseExpression

use of com.blazebit.persistence.parser.expression.WhenClauseExpression in project blaze-persistence by Blazebit.

the class TypeEqTest method testTypeEqPath1.

@Test
public void testTypeEqPath1() {
    entityTypes.put(Entity.class.getName(), Entity.class);
    entityTypes.put(Entity.class.getSimpleName(), Entity.class);
    GeneralCaseExpression actual = (GeneralCaseExpression) parse("CASE WHEN TYPE(a.b.c) = Entity THEN 0 ELSE 1 END");
    GeneralCaseExpression expected = new GeneralCaseExpression(Arrays.asList(new WhenClauseExpression(new EqPredicate(typeFunction(path("a", "b", "c")), _entity(Entity.class)), _int("0"))), _int("1"));
    assertEquals(expected, actual);
}
Also used : WhenClauseExpression(com.blazebit.persistence.parser.expression.WhenClauseExpression) GeneralCaseExpression(com.blazebit.persistence.parser.expression.GeneralCaseExpression) EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate) Test(org.junit.Test)

Example 5 with WhenClauseExpression

use of com.blazebit.persistence.parser.expression.WhenClauseExpression in project blaze-persistence by Blazebit.

the class CaseWhenAndThenBuilderImpl method thenExpression.

@Override
public T thenExpression(String expression) {
    verifyBuilderEnded();
    if (predicate.getChildren().isEmpty()) {
        throw new IllegalStateException("No and clauses specified!");
    }
    if (whenClause != null) {
        throw new IllegalStateException("Method then/thenExpression called multiple times");
    }
    whenClause = new WhenClauseExpression(predicate, expressionFactory.createSimpleExpression(expression, false));
    listener.onBuilderEnded(this);
    return result;
}
Also used : WhenClauseExpression(com.blazebit.persistence.parser.expression.WhenClauseExpression)

Aggregations

WhenClauseExpression (com.blazebit.persistence.parser.expression.WhenClauseExpression)18 GeneralCaseExpression (com.blazebit.persistence.parser.expression.GeneralCaseExpression)8 Test (org.junit.Test)6 EqPredicate (com.blazebit.persistence.parser.predicate.EqPredicate)5 Expression (com.blazebit.persistence.parser.expression.Expression)2 SimpleQueryGenerator (com.blazebit.persistence.parser.SimpleQueryGenerator)1 FunctionExpression (com.blazebit.persistence.parser.expression.FunctionExpression)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 GtPredicate (com.blazebit.persistence.parser.predicate.GtPredicate)1 IsNullPredicate (com.blazebit.persistence.parser.predicate.IsNullPredicate)1 ArrayList (java.util.ArrayList)1