Search in sources :

Example 16 with CompoundPredicate

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

the class ExpressionOptimizer method visit.

@Override
public Expression visit(CompoundPredicate predicate) {
    if (predicate.getChildren().size() == 1) {
        Predicate subPredicate = predicate.getChildren().get(0);
        if (predicate.isNegated()) {
            subPredicate.negate();
        }
        return subPredicate.accept(this);
    } else {
        boolean negate = predicate.isNegated();
        if (negate) {
            predicate = new CompoundPredicate(predicate.getOperator().invert(), predicate.getChildren());
        }
        for (int i = 0; i < predicate.getChildren().size(); i++) {
            Predicate child = predicate.getChildren().get(i);
            if (negate) {
                child.negate();
            }
            predicate.getChildren().set(i, (Predicate) child.accept(this));
        }
        return predicate;
    }
}
Also used : CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) BetweenPredicate(com.blazebit.persistence.parser.predicate.BetweenPredicate) EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate) GtPredicate(com.blazebit.persistence.parser.predicate.GtPredicate) UnaryExpressionPredicate(com.blazebit.persistence.parser.predicate.UnaryExpressionPredicate) LePredicate(com.blazebit.persistence.parser.predicate.LePredicate) 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) GePredicate(com.blazebit.persistence.parser.predicate.GePredicate) BinaryExpressionPredicate(com.blazebit.persistence.parser.predicate.BinaryExpressionPredicate) IsEmptyPredicate(com.blazebit.persistence.parser.predicate.IsEmptyPredicate) ExistsPredicate(com.blazebit.persistence.parser.predicate.ExistsPredicate) LikePredicate(com.blazebit.persistence.parser.predicate.LikePredicate) MemberOfPredicate(com.blazebit.persistence.parser.predicate.MemberOfPredicate)

Example 17 with CompoundPredicate

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

the class JPQLNextExpressionVisitorImpl method visitAndPredicate.

@Override
public Expression visitAndPredicate(JPQLNextParser.AndPredicateContext ctx) {
    List<JPQLNextParser.PredicateContext> predicate = ctx.predicate();
    Predicate left = (Predicate) predicate.get(0).accept(this);
    if (left instanceof CompoundPredicate && ((CompoundPredicate) left).getOperator() == CompoundPredicate.BooleanOperator.AND) {
        ((CompoundPredicate) left).getChildren().add((Predicate) predicate.get(1).accept(this));
        return left;
    } else {
        List<Predicate> predicates = new ArrayList<>(2);
        predicates.add(left);
        predicates.add((Predicate) predicate.get(1).accept(this));
        return new CompoundPredicate(CompoundPredicate.BooleanOperator.AND, predicates);
    }
}
Also used : ArrayList(java.util.ArrayList) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) BetweenPredicate(com.blazebit.persistence.parser.predicate.BetweenPredicate) Predicate(com.blazebit.persistence.parser.predicate.Predicate) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) GePredicate(com.blazebit.persistence.parser.predicate.GePredicate) IsEmptyPredicate(com.blazebit.persistence.parser.predicate.IsEmptyPredicate) LikePredicate(com.blazebit.persistence.parser.predicate.LikePredicate) EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate) GtPredicate(com.blazebit.persistence.parser.predicate.GtPredicate) LePredicate(com.blazebit.persistence.parser.predicate.LePredicate) LtPredicate(com.blazebit.persistence.parser.predicate.LtPredicate) IsNullPredicate(com.blazebit.persistence.parser.predicate.IsNullPredicate) InPredicate(com.blazebit.persistence.parser.predicate.InPredicate) ExistsPredicate(com.blazebit.persistence.parser.predicate.ExistsPredicate) MemberOfPredicate(com.blazebit.persistence.parser.predicate.MemberOfPredicate)

Example 18 with CompoundPredicate

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

the class SimpleQueryGenerator method visit.

@Override
public void visit(final CompoundPredicate predicate) {
    BooleanLiteralRenderingContext oldConditionalContext = setBooleanLiteralRenderingContext(BooleanLiteralRenderingContext.PREDICATE);
    ParameterRenderingMode oldParameterRenderingMode = setParameterRenderingMode(ParameterRenderingMode.PLACEHOLDER);
    boolean parenthesisRequired = predicate.getChildren().size() > 1;
    if (predicate.isNegated()) {
        sb.append("NOT ");
        if (parenthesisRequired) {
            sb.append('(');
        }
    }
    if (predicate.getChildren().size() == 1) {
        predicate.getChildren().get(0).accept(this);
        return;
    }
    final int startLen = sb.length();
    final String operator = " " + predicate.getOperator().toString() + " ";
    List<Predicate> children = predicate.getChildren();
    int size = children.size();
    for (int i = 0; i < size; i++) {
        Predicate child = children.get(i);
        if (child instanceof CompoundPredicate && ((CompoundPredicate) child).getOperator() != predicate.getOperator() && !child.isNegated()) {
            sb.append("(");
            int len = sb.length();
            child.accept(this);
            // If the child was empty, we remove the opening parenthesis again
            if (len == sb.length()) {
                sb.deleteCharAt(len - 1);
            } else {
                sb.append(")");
                sb.append(operator);
            }
        } else {
            child.accept(this);
            sb.append(operator);
        }
    }
    // Delete the last operator only if the children actually generated something
    if (startLen < sb.length()) {
        sb.delete(sb.length() - operator.length(), sb.length());
    }
    if (predicate.isNegated() && parenthesisRequired) {
        sb.append(')');
    }
    setBooleanLiteralRenderingContext(oldConditionalContext);
    setParameterRenderingMode(oldParameterRenderingMode);
}
Also used : CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) GePredicate(com.blazebit.persistence.parser.predicate.GePredicate) IsEmptyPredicate(com.blazebit.persistence.parser.predicate.IsEmptyPredicate) EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate) GtPredicate(com.blazebit.persistence.parser.predicate.GtPredicate) LePredicate(com.blazebit.persistence.parser.predicate.LePredicate) IsNullPredicate(com.blazebit.persistence.parser.predicate.IsNullPredicate) BetweenPredicate(com.blazebit.persistence.parser.predicate.BetweenPredicate) Predicate(com.blazebit.persistence.parser.predicate.Predicate) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) LikePredicate(com.blazebit.persistence.parser.predicate.LikePredicate) QuantifiableBinaryExpressionPredicate(com.blazebit.persistence.parser.predicate.QuantifiableBinaryExpressionPredicate) LtPredicate(com.blazebit.persistence.parser.predicate.LtPredicate) InPredicate(com.blazebit.persistence.parser.predicate.InPredicate) ExistsPredicate(com.blazebit.persistence.parser.predicate.ExistsPredicate) MemberOfPredicate(com.blazebit.persistence.parser.predicate.MemberOfPredicate)

Example 19 with CompoundPredicate

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

the class JoinManager method findPredicate.

private boolean findPredicate(CompoundPredicate compoundPredicate, Predicate pred, String elementAlias) {
    if (compoundPredicate != null) {
        EqualityCheckingVisitor equalityCheckingVisitor = new EqualityCheckingVisitor();
        List<Predicate> children = compoundPredicate.getChildren();
        int size = children.size();
        for (int i = 0; i < size; i++) {
            if (equalityCheckingVisitor.isEqual(children.get(i), pred, elementAlias)) {
                return true;
            }
        }
    }
    return false;
}
Also used : EqualityCheckingVisitor(com.blazebit.persistence.parser.EqualityCheckingVisitor) EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate) Predicate(com.blazebit.persistence.parser.predicate.Predicate) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate)

Example 20 with CompoundPredicate

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

the class JoinManager method correlate.

private JoinResult correlate(JoinResult result, String rootAlias, Expression correlatedAttributeExpr, EntityType<?> treatType, boolean finalOperation, boolean lateral, boolean implicitCorrelation) {
    AttributeHolder joinResult = JpaUtils.getAttributeForJoining(metamodel, result.baseNode.getNodeType(), correlatedAttributeExpr, null);
    Type<?> type = joinResult.getAttributeType();
    String correlatedAttribute;
    if (correlatedAttributeExpr instanceof ArrayExpression) {
        correlatedAttribute = ((ArrayExpression) correlatedAttributeExpr).getBase().toString();
    } else {
        correlatedAttribute = correlatedAttributeExpr.toString();
    }
    boolean implicit = implicitCorrelation || !finalOperation;
    JoinNode correlationRootNode;
    // Lateral roots/joins are special i.e. we push the array index expression into the subquery/cte, so we don't have to handle this here
    if ((!(correlatedAttributeExpr instanceof ArrayExpression) || lateral) && (rootNodes.isEmpty() || finalOperation)) {
        if (rootAlias == null) {
            StringBuilder sb = new StringBuilder(correlatedAttribute);
            sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
            for (int i = 0; i < sb.length(); i++) {
                if ('.' == sb.charAt(i)) {
                    sb.setCharAt(i, '_');
                }
            }
            String alias = sb.toString();
            if (metamodel.getEntity(alias) == null && aliasManager.isAliasAvailable(alias)) {
                rootAlias = alias;
            } else {
                rootAlias = aliasManager.generateRootAlias(alias);
            }
        } else if (!finalOperation) {
            rootAlias = rootAlias + "_base";
        }
        if (metamodel.getEntity(rootAlias) != null || !aliasManager.isAliasAvailable(rootAlias)) {
            rootAlias = aliasManager.generateRootAlias(rootAlias);
        }
        JoinAliasInfo rootAliasInfo = new JoinAliasInfo(rootAlias, rootAlias, implicit, true, aliasManager);
        correlationRootNode = JoinNode.createCorrelationRootNode(result.baseNode, correlatedAttribute, joinResult.getAttribute(), type, treatType, rootAliasInfo, lateral);
        rootAliasInfo.setJoinNode(correlationRootNode);
        rootNodes.add(correlationRootNode);
        explicitJoinNodes.add(correlationRootNode);
        // register root alias in aliasManager
        aliasManager.registerAliasInfo(rootAliasInfo);
    } else {
        // If there is a root node already, we have to correlate the base entity and join instead
        String path;
        if (result.baseNode.getAliasInfo() instanceof TreatedJoinAliasInfo) {
            path = ((TreatedJoinAliasInfo) result.baseNode.getAliasInfo()).getTreatedJoinNode().getAliasInfo().getAbsolutePath();
        } else {
            path = result.baseNode.getAliasInfo().getAbsolutePath();
        }
        String alias = (path + "_" + correlatedAttribute).replace('.', '_') + "_base";
        if (!aliasManager.isAliasAvailable(alias)) {
            alias = aliasManager.generateRootAlias(alias);
        }
        String baseAlias = addRoot(result.baseNode.getEntityType(), alias, false);
        JoinNode joinNode = ((JoinAliasInfo) aliasManager.getAliasInfo(baseAlias)).getJoinNode();
        joinNode.getAliasInfo().setImplicit(true);
        Predicate correlationPredicate = expressionFactory.createBooleanExpression(createCorrelationPredicate(result.baseNode.getEntityType(), result.baseNode.getAliasExpression(), baseAlias), false);
        correlationPredicate.accept(joinVisitor);
        joinNode.setOnPredicate(new CompoundPredicate(CompoundPredicate.BooleanOperator.AND, correlationPredicate));
        if (implicit || !(correlatedAttributeExpr instanceof ArrayExpression)) {
            PathExpression pathExpression = new PathExpression();
            pathExpression.getExpressions().add(new PropertyExpression(baseAlias));
            if (correlatedAttributeExpr instanceof PathExpression) {
                pathExpression.getExpressions().addAll(((PathExpression) correlatedAttributeExpr).getExpressions());
            } else {
                pathExpression.getExpressions().add((PathElementExpression) correlatedAttributeExpr);
            }
            implicitJoin(pathExpression, true, true, true, treatType == null ? null : treatType.getName(), ClauseType.JOIN, null, false, false, true, false);
            correlationRootNode = (JoinNode) pathExpression.getBaseNode();
        } else {
            JoinResult node = createOrUpdateNode(joinNode, Collections.singletonList(correlatedAttribute), treatType == null ? null : treatType.getName(), rootAlias, JoinType.INNER, null, false, false, true, true);
            correlationRootNode = node.baseNode;
            ArrayExpression arrayExpression = (ArrayExpression) correlatedAttributeExpr;
            implicitJoinIndex(arrayExpression);
            generateAndApplyOnPredicate(correlationRootNode, arrayExpression);
            // In case we introduce a synthetic subquery for the ON clause predicate, we need to replace uses of the outer query with a subquery alias
            correlationRootNode.getOnPredicate().accept(new CorrelationReplacementVisitor(result.baseNode, joinNode));
        }
    }
    return new JoinResult(correlationRootNode);
}
Also used : EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate) Predicate(com.blazebit.persistence.parser.predicate.Predicate) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression)

Aggregations

CompoundPredicate (com.blazebit.persistence.parser.predicate.CompoundPredicate)23 Predicate (com.blazebit.persistence.parser.predicate.Predicate)18 EqPredicate (com.blazebit.persistence.parser.predicate.EqPredicate)17 LtPredicate (com.blazebit.persistence.parser.predicate.LtPredicate)12 ExistsPredicate (com.blazebit.persistence.parser.predicate.ExistsPredicate)10 GtPredicate (com.blazebit.persistence.parser.predicate.GtPredicate)10 InPredicate (com.blazebit.persistence.parser.predicate.InPredicate)9 IsNullPredicate (com.blazebit.persistence.parser.predicate.IsNullPredicate)8 GePredicate (com.blazebit.persistence.parser.predicate.GePredicate)7 IsEmptyPredicate (com.blazebit.persistence.parser.predicate.IsEmptyPredicate)7 LePredicate (com.blazebit.persistence.parser.predicate.LePredicate)7 LikePredicate (com.blazebit.persistence.parser.predicate.LikePredicate)7 MemberOfPredicate (com.blazebit.persistence.parser.predicate.MemberOfPredicate)7 BetweenPredicate (com.blazebit.persistence.parser.predicate.BetweenPredicate)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)3 PropertyExpression (com.blazebit.persistence.parser.expression.PropertyExpression)3 BinaryExpressionPredicate (com.blazebit.persistence.parser.predicate.BinaryExpressionPredicate)3 RootPredicate (com.blazebit.persistence.impl.builder.predicate.RootPredicate)2