Search in sources :

Example 1 with Predicate

use of com.blazebit.persistence.parser.predicate.Predicate 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 Predicate

use of com.blazebit.persistence.parser.predicate.Predicate 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 Predicate

use of com.blazebit.persistence.parser.predicate.Predicate 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 Predicate

use of com.blazebit.persistence.parser.predicate.Predicate 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 Predicate

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

the class JoinManager method implicitJoinIndex.

private void implicitJoinIndex(ArrayExpression arrayExpr) {
    // Array expression predicates get a different root, normal expressions not
    if (arrayExpr.getIndex() instanceof Predicate) {
        // So we have to prefix relative expressions with ArrayExpression.ELEMENT_NAME
        PathElementExpression old = joinVisitor.getRelativeExpressionPrefix();
        try {
            joinVisitor.setRelativeExpressionPrefix(new PropertyExpression(ArrayExpression.ELEMENT_NAME));
            arrayExpr.getIndex().accept(joinVisitor);
        } finally {
            joinVisitor.setRelativeExpressionPrefix(old);
        }
    } else {
        arrayExpr.getIndex().accept(joinVisitor);
    }
}
Also used : PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) EqPredicate(com.blazebit.persistence.parser.predicate.EqPredicate) Predicate(com.blazebit.persistence.parser.predicate.Predicate) CompoundPredicate(com.blazebit.persistence.parser.predicate.CompoundPredicate)

Aggregations

Predicate (com.blazebit.persistence.parser.predicate.Predicate)77 CompoundPredicate (com.blazebit.persistence.parser.predicate.CompoundPredicate)58 ExistsPredicate (com.blazebit.persistence.parser.predicate.ExistsPredicate)39 EqPredicate (com.blazebit.persistence.parser.predicate.EqPredicate)34 GtPredicate (com.blazebit.persistence.parser.predicate.GtPredicate)29 LtPredicate (com.blazebit.persistence.parser.predicate.LtPredicate)26 InPredicate (com.blazebit.persistence.parser.predicate.InPredicate)25 IsNullPredicate (com.blazebit.persistence.parser.predicate.IsNullPredicate)24 LikePredicate (com.blazebit.persistence.parser.predicate.LikePredicate)22 BetweenPredicate (com.blazebit.persistence.parser.predicate.BetweenPredicate)21 MemberOfPredicate (com.blazebit.persistence.parser.predicate.MemberOfPredicate)21 GePredicate (com.blazebit.persistence.parser.predicate.GePredicate)18 IsEmptyPredicate (com.blazebit.persistence.parser.predicate.IsEmptyPredicate)18 LePredicate (com.blazebit.persistence.parser.predicate.LePredicate)18 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)11 PropertyExpression (com.blazebit.persistence.parser.expression.PropertyExpression)11 Test (org.junit.Test)11 ExpressionBuilder (com.blazebit.persistence.impl.builder.expression.ExpressionBuilder)10 ExpressionBuilderEndedListener (com.blazebit.persistence.impl.builder.expression.ExpressionBuilderEndedListener)10 Expression (com.blazebit.persistence.parser.expression.Expression)9