Search in sources :

Example 1 with CompoundPredicate

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

the class JoinNode method getTreatedJoinNode.

public JoinNode getTreatedJoinNode(EntityType<?> type) {
    // Return this when the treat is an upcast
    if (type.getJavaType().isAssignableFrom(getJavaType())) {
        return this;
    }
    String typeName = type.getJavaType().getName();
    JoinNode treatedNode = treatedJoinNodes.get(typeName);
    if (treatedNode != null) {
        return treatedNode;
    }
    String alias = aliasInfo.getAliasOwner().generateRootAlias(aliasInfo.getAlias());
    TreatedJoinAliasInfo treatedJoinAliasInfo = new TreatedJoinAliasInfo(this, type, alias);
    aliasInfo.getAliasOwner().registerAliasInfo(treatedJoinAliasInfo);
    treatedNode = new JoinNode(treatedJoinAliasInfo);
    List<Predicate> predicates = new ArrayList<>(1);
    predicates.add(new EqPredicate(createExpression(null), treatedNode.createExpression(null)));
    treatedNode.onPredicate = new CompoundPredicate(CompoundPredicate.BooleanOperator.AND, predicates);
    treatedJoinAliasInfo.setJoinNode(treatedNode);
    treatedJoinNodes.put(typeName, treatedNode);
    return treatedNode;
}
Also used : ArrayList(java.util.ArrayList) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate) Predicate(com.blazebit.persistence.parser.predicate.Predicate) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate)

Example 2 with CompoundPredicate

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

the class JoinVisitor method visit.

@Override
public void visit(JoinNode node) {
    if (node.getOnPredicate() != null) {
        currentJoinNode = node;
        try {
            node.getOnPredicate().accept(this);
            if (!correlationPathReplacementVisitor.getPathsToCorrelate().isEmpty()) {
                // We rewrite the predicate to use correlated subqueries for implicit joins
                SubqueryInitiatorImpl<Object> subqueryInitiator = (SubqueryInitiatorImpl<Object>) joinManager.getSubqueryInitFactory().createSubqueryInitiator(null, this, true, fromClause);
                SubqueryBuilderImpl<?> subqueryBuilder = null;
                List<Predicate> additionalPredicates = new ArrayList<>(correlationPathReplacementVisitor.getRootsToCorrelate().size());
                for (ImplicitJoinCorrelationPathReplacementVisitor.RootCorrelationEntry entry : correlationPathReplacementVisitor.getRootsToCorrelate()) {
                    if (subqueryBuilder == null) {
                        subqueryBuilder = (SubqueryBuilderImpl<?>) subqueryInitiator.from(entry.getEntityClass(), entry.getAlias());
                    } else {
                        subqueryBuilder.from(entry.getEntityClass(), entry.getAlias());
                    }
                    additionalPredicates.add(entry.getAdditionalPredicate());
                }
                for (ImplicitJoinCorrelationPathReplacementVisitor.CorrelationTransformEntry correlationTransformEntry : correlationPathReplacementVisitor.getPathsToCorrelate()) {
                    String alias = correlationTransformEntry.getAlias();
                    String correlationExpression = correlationTransformEntry.getCorrelationExpression();
                    if (correlationTransformEntry.isInConjunction()) {
                        if (subqueryBuilder == null) {
                            subqueryBuilder = (SubqueryBuilderImpl<?>) subqueryInitiator.from(correlationExpression, alias);
                        } else {
                            subqueryBuilder.from(correlationExpression, alias);
                        }
                    } else {
                        subqueryBuilder.leftJoinDefault(correlationExpression, alias);
                    }
                }
                subqueryBuilder.whereManager.restrictSetExpression(correlationPathReplacementVisitor.rewritePredicate(node.getOnPredicate()));
                for (Predicate additionalPredicate : additionalPredicates) {
                    subqueryBuilder.whereManager.restrictExpression(additionalPredicate);
                }
                node.setOnPredicate(new CompoundPredicate(CompoundPredicate.BooleanOperator.AND, new ExistsPredicate(new SubqueryExpression(subqueryBuilder), false)));
                node.getOnPredicate().accept(this);
            }
        } finally {
            currentJoinNode = null;
        }
    }
    node.registerDependencies();
    if (node.isFetch()) {
        fetchableNodes.add(node);
    }
}
Also used : ExistsPredicate(com.blazebit.persistence.parser.predicate.ExistsPredicate) ArrayList(java.util.ArrayList) Predicate(com.blazebit.persistence.parser.predicate.Predicate) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) IsEmptyPredicate(com.blazebit.persistence.parser.predicate.IsEmptyPredicate) EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate) 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) SubqueryExpression(com.blazebit.persistence.parser.expression.SubqueryExpression) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate)

Example 3 with CompoundPredicate

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

the class JoinManager method findNode.

private JoinNode findNode(JoinNode baseNode, String joinRelationName, final ArrayExpression arrayExpression) {
    if (arrayExpression.getBase() instanceof PropertyExpression) {
        if (baseNode == null) {
            for (JoinNode node : rootNodes) {
                Predicate pred = getArrayExpressionPredicate(node, arrayExpression);
                CompoundPredicate compoundPredicate = node.getOnPredicate();
                if (findPredicate(compoundPredicate, pred, node.getAlias())) {
                    return node;
                }
            }
        } else {
            JoinTreeNode treeNode = baseNode.getNodes().get(joinRelationName);
            if (treeNode == null) {
                return null;
            }
            for (JoinNode node : treeNode.getJoinNodes().values()) {
                Predicate pred = getArrayExpressionPredicate(node, arrayExpression);
                CompoundPredicate compoundPredicate = node.getOnPredicate();
                if (findPredicate(compoundPredicate, pred, node.getAlias())) {
                    return node;
                }
            }
        }
    } else {
        final Class<?> entityType = ((EntityLiteral) arrayExpression.getBase()).getValue();
        JoinNode node = null;
        AbortableResultJoinNodeVisitor<Object> visitor = new AbortableResultJoinNodeVisitor<Object>() {

            @Override
            public Object getStopValue() {
                return this;
            }

            @Override
            public Object visit(JoinNode node) {
                if (node.isEntityJoinNode() && entityType == node.getNodeType().getJavaType()) {
                    Predicate pred = getArrayExpressionPredicate(node, arrayExpression);
                    CompoundPredicate compoundPredicate = node.getOnPredicate();
                    if (findPredicate(compoundPredicate, pred, node.getAlias())) {
                        return node;
                    }
                }
                return null;
            }

            @Override
            public int hashCode() {
                return 0;
            }

            // This is a fake implementation for the stopValue
            @Override
            public boolean equals(Object obj) {
                return obj != null;
            }
        };
        for (int i = 0; i < rootNodes.size(); i++) {
            node = (JoinNode) rootNodes.get(i).accept(visitor);
            if (node != null) {
                break;
            }
        }
        return node;
    }
    return null;
}
Also used : EntityLiteral(com.blazebit.persistence.parser.expression.EntityLiteral) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate) Predicate(com.blazebit.persistence.parser.predicate.Predicate) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate)

Example 4 with CompoundPredicate

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

the class JoinManager method generateAndApplyOnPredicate.

private void generateAndApplyOnPredicate(JoinNode joinNode, ArrayExpression arrayExpr) {
    PathExpression joinAliasPathExpression = new PathExpression(new PropertyExpression(joinNode.getAlias()));
    arrayExpr.getIndex().accept(new AliasReplacementVisitor(joinAliasPathExpression, ArrayExpression.ELEMENT_NAME));
    Predicate filterPredicate = getArrayExpressionPredicate(joinNode, arrayExpr);
    // Array expression predicates get a different root, normal expressions not
    ClauseType fromClause = joinVisitor.getFromClause();
    JoinNode currentJoinNode = joinVisitor.getCurrentJoinNode();
    CompoundPredicate currentPred = joinNode.getOnPredicate();
    boolean newPredicate = false;
    if (currentPred != null) {
        // Only add the predicate if it isn't contained yet
        if (!findPredicate(currentPred, filterPredicate, joinNode.getAlias())) {
            currentPred.getChildren().add(filterPredicate);
            newPredicate = true;
        }
    } else {
        CompoundPredicate onAndPredicate = new CompoundPredicate(CompoundPredicate.BooleanOperator.AND);
        onAndPredicate.getChildren().add(filterPredicate);
        joinNode.setOnPredicate(onAndPredicate);
        newPredicate = true;
    }
    try {
        joinVisitor.setFromClause(ClauseType.JOIN);
        joinVisitor.setCurrentJoinNode(joinNode);
        if (arrayExpr.getIndex() instanceof Predicate) {
            JoinNode oldRootNode = rootNode;
            try {
                rootNode = joinNode;
                joinNode.accept(joinVisitor);
            } finally {
                rootNode = oldRootNode;
            }
        } else {
            arrayExpr.getIndex().accept(joinVisitor);
        }
    } finally {
        joinVisitor.setFromClause(fromClause);
        joinVisitor.setCurrentJoinNode(currentJoinNode);
    }
    if (newPredicate) {
        joinNode.registerDependencies();
        joinNode.updateClauseDependencies(ClauseType.JOIN, new LinkedHashSet<JoinNode>());
    }
}
Also used : AliasReplacementVisitor(com.blazebit.persistence.parser.AliasReplacementVisitor) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate) Predicate(com.blazebit.persistence.parser.predicate.Predicate) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate)

Example 5 with CompoundPredicate

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

the class ResolvingQueryGenerator method visit.

@Override
public void visit(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() + " ";
    final 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) 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) Predicate(com.blazebit.persistence.parser.predicate.Predicate) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate) LikePredicate(com.blazebit.persistence.parser.predicate.LikePredicate) LtPredicate(com.blazebit.persistence.parser.predicate.LtPredicate) InPredicate(com.blazebit.persistence.parser.predicate.InPredicate) ExistsPredicate(com.blazebit.persistence.parser.predicate.ExistsPredicate)

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