Search in sources :

Example 11 with ArrayExpression

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

the class JoinManager method addRoot.

String addRoot(String correlationPath, Expression expr, String rootAlias, boolean lateral, boolean implicitCorrelation) {
    PathExpression pathExpression;
    String treatEntityType = null;
    // First we extract the path expression and some parameters from surrounding expressions
    if (expr instanceof PathExpression) {
        pathExpression = (PathExpression) expr;
    } else if (expr instanceof TreatExpression) {
        TreatExpression treatExpression = (TreatExpression) expr;
        Expression expression = treatExpression.getExpression();
        if (expression instanceof PathExpression) {
            pathExpression = (PathExpression) expression;
            treatEntityType = treatExpression.getType();
        } else {
            throw new IllegalArgumentException("Unexpected expression type[" + expression.getClass().getSimpleName() + "] in treat expression: " + treatExpression);
        }
    } else if (expr instanceof FunctionExpression && ExpressionUtils.isOuterFunction((FunctionExpression) expr)) {
        FunctionExpression outerFunctionExpr = (FunctionExpression) expr;
        pathExpression = (PathExpression) outerFunctionExpr.getExpressions().get(0);
    } else {
        throw new IllegalArgumentException("Correlation join path [" + correlationPath + "] is not a valid join path");
    }
    if (isJoinableSelectAlias(pathExpression, false, false)) {
        throw new IllegalArgumentException("No select alias allowed in join path");
    }
    // Correlation is split into 3 phases
    // Phase 1 is determining the correlation basis which must be an alias
    // Phase 2 is determining the correlated attribute which we use in the root node of the subquery
    // Phase 3 is joining the rest of the path and assigning the last join node the given alias
    List<JoinNode> treatedCorrelationNodes = new ArrayList<>();
    List<PathExpression> pathExpressionStack = new ArrayList<>();
    pathExpressionStack.add(pathExpression);
    // Phase 1
    JoinNode correlationParent = null;
    boolean needsCorrelationAttribute = true;
    int start = 0;
    for (int i = 0; i < pathExpressionStack.size(); i++) {
        PathExpression currentPathExpression = pathExpressionStack.get(i);
        List<PathElementExpression> pathElements = currentPathExpression.getExpressions();
        AliasInfo aliasInfo;
        if (pathElements.get(0) instanceof PropertyExpression) {
            if ((aliasInfo = aliasManager.getAliasInfo(pathElements.get(0).toString())) != null) {
                if (aliasInfo instanceof SelectInfo) {
                    if (pathElements.size() != 1) {
                        // We actually allow usage of select aliases in expressions, but JPA doesn't, so we have to resolve them here
                        Expression selectExpr = ((SelectInfo) aliasInfo).getExpression();
                        if (!(selectExpr instanceof PathExpression)) {
                            throw new RuntimeException("The select expression '" + selectExpr.toString() + "' is not a simple path expression! No idea how to implicit join that.");
                        }
                        // join the expression behind a select alias once when it is encountered the first time
                        if (((PathExpression) selectExpr).getBaseNode() == null) {
                            implicitJoin(selectExpr, true, true, true, null, ClauseType.JOIN, null, false, true, true, false);
                        }
                        PathExpression selectPathExpr = (PathExpression) selectExpr;
                        correlationParent = (JoinNode) selectPathExpr.getBaseNode();
                        if (selectPathExpr.getField() != null) {
                            rootAlias += "." + selectPathExpr.getField();
                        }
                        start = 1;
                    } else {
                        // We can't correlate a single select expression
                        return null;
                    }
                } else {
                    correlationParent = ((JoinAliasInfo) aliasInfo).getJoinNode();
                    start = 1;
                }
            } else {
                correlationParent = parent.getRootNodeOrFail("Could not join correlation path [", correlationPath, "] because it did not use an absolute path but multiple root nodes are available!");
            }
        } else if (pathElements.get(0) instanceof TreatExpression) {
            TreatExpression treatExpression = (TreatExpression) pathElements.get(0);
            PathExpression treatExpressionPathExpression = (PathExpression) treatExpression.getExpression();
            if (treatExpressionPathExpression.getExpressions().size() == 1) {
                if ((aliasInfo = aliasManager.getAliasInfo(treatExpressionPathExpression.getExpressions().get(0).toString())) != null) {
                    // Root treat
                    correlationParent = ((JoinAliasInfo) aliasInfo).getJoinNode().getTreatedJoinNode(metamodel.entity(treatExpression.getType()));
                    treatedCorrelationNodes.add(correlationParent);
                    // Use the treated root node as correlation parent
                    start = 1;
                } else {
                    // Treat of an association on a query root
                    correlationParent = parent.getRootNodeOrFail("Could not join correlation path [", correlationPath, "] because it did not use an absolute path but multiple root nodes are available!");
                    pathExpressionStack.add(treatExpressionPathExpression);
                    break;
                }
            } else {
                pathExpressionStack.add(treatExpressionPathExpression);
            }
        } else if (pathElements.get(0) instanceof ArrayExpression && ((ArrayExpression) pathElements.get(0)).getBase() instanceof EntityLiteral) {
            ArrayExpression arrayExpression = (ArrayExpression) pathElements.get(0);
            JoinNode matchingNode = implicitCorrelation ? findNode(null, null, arrayExpression) : null;
            if (matchingNode == null) {
                rootAlias = addRoot(metamodel.entity(((EntityLiteral) arrayExpression.getBase()).getValue()), rootAlias, lateral && pathElements.size() == 1);
                correlationParent = ((JoinAliasInfo) aliasManager.getAliasInfo(rootAlias)).getJoinNode();
                // This is only necessary in the CTE query where the lateral flag is set to false
                if (!lateral) {
                    implicitJoinIndex(arrayExpression);
                    generateAndApplyOnPredicate(correlationParent, arrayExpression);
                }
            } else {
                rootAlias = matchingNode.getAliasExpression();
                correlationParent = matchingNode;
            }
            start = 1;
            needsCorrelationAttribute = false;
        } else if (pathElements.get(0) instanceof ArrayExpression) {
            correlationParent = parent.getRootNodeOrFail("Could not join correlation path [", correlationPath, "] because it did not use an absolute path but multiple root nodes are available!");
        } else {
            throw new IllegalArgumentException("The correlation path '" + correlationPath + "' couldn't be properly analyzed because of an unsupported expression structure!");
        }
    }
    // Phase 2
    PathExpression currentPathExpression = pathExpressionStack.remove(pathExpressionStack.size() - 1);
    List<PathElementExpression> pathElements = currentPathExpression.getExpressions();
    int correlatedAttributeIndex = findCorrelatedAttributeIndex(correlationParent, pathElements, start, pathElements.size());
    if (correlatedAttributeIndex == -1) {
        if (needsCorrelationAttribute) {
            if (!implicitCorrelation) {
                throw new IllegalArgumentException("The correlation path '" + correlationPath + "' does not contain an attribute that can be correlated!");
            } else {
                return null;
            }
        }
        return rootAlias;
    }
    Expression correlatedAttributeExpr;
    String correlatedAttribute;
    if (start == correlatedAttributeIndex) {
        correlatedAttributeExpr = pathElements.get(start);
        correlatedAttribute = getCorrelatedAttribute(new PathExpression(pathElements.subList(start, correlatedAttributeIndex + 1)));
    } else {
        correlatedAttributeExpr = new PathExpression(pathElements.subList(start, correlatedAttributeIndex + 1));
        correlatedAttribute = getCorrelatedAttribute((PathExpression) correlatedAttributeExpr);
        start += correlatedAttributeIndex - start;
    }
    // Phase 3
    final JoinNode rootNode;
    if (pathExpressionStack.isEmpty() && start + 1 == pathElements.size()) {
        // This is a simple path to an association, no deep expression that requires implicit joining
        JoinNode matchingNode = null;
        if (implicitCorrelation) {
            JoinTreeNode existingNode = correlationParent.getNodes().get(correlatedAttribute);
            if (existingNode != null && existingNode.getDefaultNode() != null) {
                matchingNode = existingNode.getDefaultNode();
            }
        }
        if (matchingNode == null) {
            rootNode = correlate(new JoinResult(correlationParent), rootAlias, correlatedAttributeExpr, metamodel.getEntity(treatEntityType), true, lateral, implicitCorrelation).baseNode;
            rootAlias = rootNode.getAliasExpression();
        } else {
            // Try reusing an existing join
            rootNode = matchingNode;
            rootAlias = matchingNode.getAliasExpression();
        }
    } else {
        JoinNode matchingNode = null;
        if (implicitCorrelation) {
            String path;
            if (correlationParent.getAliasInfo() instanceof TreatedJoinAliasInfo) {
                path = ((TreatedJoinAliasInfo) correlationParent.getAliasInfo()).getTreatedJoinNode().getAliasInfo().getAbsolutePath();
            } else {
                path = correlationParent.getAliasInfo().getAbsolutePath();
            }
            String correlationBaseAlias = (path + "_" + correlatedAttribute).replace('.', '_') + "_base";
            for (int i = 0; i < rootNodes.size(); i++) {
                JoinNode node = rootNodes.get(i);
                if (node.getAliasInfo().isImplicit()) {
                    if (node.getCorrelationParent() == correlationParent && correlatedAttribute.equals(node.getCorrelationPath())) {
                        matchingNode = node;
                        break;
                    } else if (node.getOnPredicate() != null && correlationBaseAlias.equals(node.getAlias())) {
                        matchingNode = node;
                        start--;
                        break;
                    }
                }
            }
        }
        if (matchingNode == null) {
            // This is a simple path to an association, no deep expression that requires implicit joining
            JoinTreeNode existingNode = correlationParent.getNodes().get(correlatedAttribute);
            if (!implicitCorrelation || existingNode == null || existingNode.getDefaultNode() == null) {
                if (isSingleValuedAssociationId(correlationParent, currentPathExpression, start)) {
                    return correlationParent.getAliasExpression() + "." + new PathExpression(pathElements.subList(start, pathElements.size()));
                }
                rootNode = correlate(new JoinResult(correlationParent), rootAlias, correlatedAttributeExpr, null, false, lateral, implicitCorrelation).baseNode;
            } else {
                // Try reusing an existing join
                rootNode = existingNode.getDefaultNode();
            }
        } else {
            rootNode = matchingNode;
        }
        // We correlate or reuse a join for the current position, so increment
        start++;
        JoinResult result = new JoinResult(rootNode);
        if (pathExpressionStack.size() > 0) {
            // Implicit join the rest of the current level
            pathExpressionStack.add(currentPathExpression);
            while (pathExpressionStack.size() > 1) {
                currentPathExpression = pathExpressionStack.remove(pathExpressionStack.size() - 1);
                pathElements = currentPathExpression.getExpressions();
                for (; start < pathElements.size(); start++) {
                    PathElementExpression pathElementExpression = pathElements.get(start);
                    if (pathElementExpression instanceof TreatExpression) {
                        TreatExpression treatExpression = (TreatExpression) pathElementExpression;
                        EntityType<?> treatType = metamodel.entity(treatExpression.getType());
                        JoinNode treatedNode = result.baseNode.getTreatedJoinNode(treatType);
                        treatedCorrelationNodes.add(treatedNode);
                        // We just implicit join the rest of the expression
                        result = implicitJoin(treatedNode, currentPathExpression, null, implicitCorrelation ? JoinType.LEFT : JoinType.INNER, null, new HashSet<String>(), start + 1, pathElements.size(), true, true, true, false);
                        start = pathElements.size();
                    } else {
                        JoinTreeNode existingNode = correlationParent.getNodes().get(((PropertyExpression) pathElementExpression).getProperty());
                        if (existingNode == null || existingNode.getDefaultNode() == null) {
                            break;
                        }
                        result = new JoinResult(existingNode.getDefaultNode());
                    }
                }
                if (result.baseNode.getAliasInfo().getAliasOwner() != aliasManager && start + 1 < pathElements.size() - 1) {
                    result = correlate(result, rootAlias, pathElements.get(start), null, false, lateral, implicitCorrelation);
                    start++;
                }
                result = implicitJoin(result.baseNode, currentPathExpression, null, implicitCorrelation ? JoinType.LEFT : JoinType.INNER, null, new HashSet<String>(), start, pathElements.size(), true, true, true, false);
                // Reset start
                start = 0;
            }
            // At the end of treat processing, we are at index 1
            start = 1;
        }
        if (pathExpressionStack.size() > 0) {
            currentPathExpression = pathExpressionStack.remove(0);
            pathElements = currentPathExpression.getExpressions();
            // This can only be a treat expression
            TreatExpression treatExpression = (TreatExpression) pathElements.get(0);
            EntityType<?> treatType = metamodel.entity(treatExpression.getType());
            JoinNode treatedNode = result.baseNode.getTreatedJoinNode(treatType);
            treatedCorrelationNodes.add(treatedNode);
            result = new JoinResult(treatedNode, null, treatType, -1, -1);
            // Reset start
            start = 1;
        }
        pathElements = currentPathExpression.getExpressions();
        Expression elementExpr = pathElements.get(pathElements.size() - 1);
        while (result.baseNode.getAliasInfo().getAliasOwner() != aliasManager && start < pathElements.size() - 1) {
            if (isSingleValuedAssociationId(result.baseNode, currentPathExpression, start)) {
                return result.baseNode.getAliasExpression() + "." + new PathExpression(pathElements.subList(start, pathElements.size()));
            }
            JoinNode defaultJoin = result.baseNode.getDefaultJoin(pathElements, start - result.fieldCount(), start);
            if (defaultJoin == null) {
                result = correlate(result, rootAlias, pathElements.get(start), null, false, lateral, implicitCorrelation);
            } else {
                result = new JoinResult(defaultJoin);
            }
            start++;
        }
        result = implicitJoin(result.baseNode, currentPathExpression, null, implicitCorrelation ? JoinType.LEFT : JoinType.INNER, null, new HashSet<String>(), start, pathElements.size() - 1, true, true, true, false);
        JoinResult finalNode;
        if (pathExpression.isUsedInCollectionFunction()) {
            JoinNode current = result.baseNode;
            List<String> resultFields = result.fields;
            if (result.hasField()) {
                resultFields.add(elementExpr.toString());
                String attributeName = StringUtils.join(".", resultFields);
                finalNode = new JoinResult(current, resultFields, getPathType(current.getNodeType(), attributeName, pathExpression), -1, -1);
            } else {
                String attributeName = elementExpr.toString();
                finalNode = new JoinResult(current, Arrays.asList(attributeName), getPathType(current.getNodeType(), attributeName, pathExpression), -1, -1);
            }
        } else {
            if (result.hasField()) {
                start = pathElements.size() - 1 - result.fields.size();
            } else {
                start = pathElements.size() - 1;
            }
            if (result.baseNode.getAliasInfo().getAliasOwner() != aliasManager) {
                Expression finalExpression;
                if (result.hasField()) {
                    finalExpression = new PathExpression(pathElements.subList(start, pathElements.size()));
                } else {
                    finalExpression = pathElements.get(start);
                }
                if (isJoinable(result, finalExpression)) {
                    finalNode = correlate(result, rootAlias, finalExpression, null, true, lateral, implicitCorrelation);
                } else {
                    finalNode = result.withField(((PropertyExpression) finalExpression).getProperty());
                }
            } else {
                finalNode = implicitJoin(result.baseNode, pathExpression, null, implicitCorrelation ? JoinType.LEFT : JoinType.INNER, null, new HashSet<String>(), start, pathElements.size(), true, true, true, false);
            }
            if (implicitCorrelation) {
                rootAlias = finalNode.baseNode.getAliasExpression();
            } else {
                aliasManager.unregisterAliasInfoForBottomLevel(finalNode.baseNode.getAliasInfo());
                finalNode.baseNode.getAliasInfo().setAlias(rootAlias);
                aliasManager.registerAliasInfo(finalNode.baseNode.getAliasInfo());
            }
        }
        if (treatEntityType != null) {
            treatedCorrelationNodes.add(finalNode.baseNode);
        }
        if (implicitCorrelation) {
            if (finalNode.hasField()) {
                rootAlias = finalNode.baseNode.getAliasExpression() + "." + finalNode.joinFields();
            }
        } else {
            finalNode.baseNode.getAliasInfo().setImplicit(false);
            explicitJoinNodes.add(finalNode.baseNode);
        }
    }
    if (!treatedCorrelationNodes.isEmpty()) {
        rootNode.setJoinNodesNeedingTreatConjunct(treatedCorrelationNodes);
    }
    return rootAlias;
}
Also used : TreatExpression(com.blazebit.persistence.parser.expression.TreatExpression) EntityLiteral(com.blazebit.persistence.parser.expression.EntityLiteral) ArrayList(java.util.ArrayList) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) ListIndexExpression(com.blazebit.persistence.parser.expression.ListIndexExpression) Expression(com.blazebit.persistence.parser.expression.Expression) QualifiedExpression(com.blazebit.persistence.parser.expression.QualifiedExpression) TreatExpression(com.blazebit.persistence.parser.expression.TreatExpression) ParameterExpression(com.blazebit.persistence.parser.expression.ParameterExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) SubqueryExpression(com.blazebit.persistence.parser.expression.SubqueryExpression) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) GeneralCaseExpression(com.blazebit.persistence.parser.expression.GeneralCaseExpression) MapEntryExpression(com.blazebit.persistence.parser.expression.MapEntryExpression) MapValueExpression(com.blazebit.persistence.parser.expression.MapValueExpression) MapKeyExpression(com.blazebit.persistence.parser.expression.MapKeyExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression)

Example 12 with ArrayExpression

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

the class EqualityCheckingVisitor method visit.

@Override
public Boolean visit(ArrayExpression expression) {
    if (referenceExpression.getClass() != expression.getClass()) {
        return Boolean.TRUE;
    }
    ArrayExpression reference = (ArrayExpression) referenceExpression;
    referenceExpression = reference.getBase();
    if (expression.getBase().accept(this)) {
        return Boolean.TRUE;
    }
    referenceExpression = reference.getIndex();
    return expression.getIndex().accept(this);
}
Also used : ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression)

Example 13 with ArrayExpression

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

the class AbstractCommonQueryBuilder method joinOnForLateralEmulation.

private JoinOnBuilder<BuilderType> joinOnForLateralEmulation(String correlationPath, String alias, JoinType type) {
    Expression expr = expressionFactory.createJoinPathExpression(correlationPath);
    JoinNode baseNode = null;
    String baseNodeAlias = null;
    if (expr instanceof PathExpression) {
        PathElementExpression firstElem = ExpressionUtils.getLeftMostPathExpression((PathExpression) expr).getExpressions().get(0);
        AliasInfo startAlias;
        if (firstElem instanceof ArrayExpression) {
            startAlias = null;
        } else if (firstElem instanceof PropertyExpression) {
            startAlias = aliasManager.getAliasInfo(((PropertyExpression) firstElem).getProperty());
        } else {
            throw new IllegalArgumentException("Unexpected expression type[" + firstElem.getClass().getSimpleName() + "] in expression: " + correlationPath);
        }
        if (startAlias instanceof JoinAliasInfo) {
            baseNodeAlias = startAlias.getAlias();
            baseNode = ((JoinAliasInfo) startAlias).getJoinNode();
        }
    } else {
        throw new IllegalArgumentException("Unexpected expression type[" + expr.getClass().getSimpleName() + "] in expression: " + correlationPath);
    }
    if (baseNode == null) {
        baseNode = getRoot();
    }
    PathTargetResolvingExpressionVisitor visitor = new PathTargetResolvingExpressionVisitor(getMetamodel(), baseNode.getType(), baseNodeAlias);
    expr.accept(visitor);
    Map<Attribute<?, ?>, Type<?>> possibleTargets = visitor.getPossibleTargets();
    Type<?> t;
    if (possibleTargets.size() == 1 && (t = possibleTargets.values().iterator().next()) instanceof EntityType<?>) {
        return joinOn((EntityType<?>) t, alias, type);
    }
    return joinOn(expr, alias, type);
}
Also used : PathTargetResolvingExpressionVisitor(com.blazebit.persistence.parser.PathTargetResolvingExpressionVisitor) PluralAttribute(javax.persistence.metamodel.PluralAttribute) MapAttribute(javax.persistence.metamodel.MapAttribute) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ExtendedAttribute(com.blazebit.persistence.spi.ExtendedAttribute) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) NumericType(com.blazebit.persistence.parser.expression.NumericType) SetOperationType(com.blazebit.persistence.spi.SetOperationType) DbmsStatementType(com.blazebit.persistence.spi.DbmsStatementType) ManagedType(javax.persistence.metamodel.ManagedType) Type(javax.persistence.metamodel.Type) ExtendedManagedType(com.blazebit.persistence.spi.ExtendedManagedType) IdentifiableType(javax.persistence.metamodel.IdentifiableType) JoinType(com.blazebit.persistence.JoinType) EntityType(javax.persistence.metamodel.EntityType) TemporalType(javax.persistence.TemporalType) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) ParameterExpression(javax.persistence.criteria.ParameterExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) Expression(com.blazebit.persistence.parser.expression.Expression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) SubqueryExpression(com.blazebit.persistence.parser.expression.SubqueryExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression)

Example 14 with ArrayExpression

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

the class PrefixingQueryGenerator method visit.

@Override
public void visit(PathExpression expression) {
    final List<PathElementExpression> expressions = expression.getExpressions();
    if (expressions.isEmpty()) {
        sb.append(prefix);
    } else {
        final PathElementExpression firstElement = expressions.get(0);
        final String firstProperty = firstElement instanceof PropertyExpression ? ((PropertyExpression) firstElement).getProperty() : null;
        final int size = expressions.size();
        // If we encounter a query alias, we don't prefix
        if (!doPrefix || queryAliases.contains(firstProperty)) {
            super.visit(expression);
            return;
        }
        // Since the correlated alias will not be a prefix of the subview mapping prefix, we have to replace that properly
        if (aliasToReplace != null && aliasToReplace.equals(firstProperty)) {
            sb.append(substitute);
            for (int i = 1; i < size; i++) {
                sb.append(".");
                expressions.get(i).accept(this);
            }
            return;
        }
        if (firstElement instanceof ArrayExpression && ((ArrayExpression) firstElement).getBase() instanceof EntityLiteral) {
            super.visit(expression);
            return;
        }
        // Prefixing will only be done for the inner most path, but not the treat expression
        if (!(firstElement instanceof TreatExpression)) {
            String expressionString = expression.toString();
            // Find out the common prefix
            int dotIndex = -1;
            int length = Math.min(prefix.length(), expressionString.length());
            for (int i = 0; i < length; i++) {
                if (prefix.charAt(i) != expressionString.charAt(i)) {
                    for (; i > 0; i--) {
                        if (prefix.charAt(i) == '.' && expressionString.charAt(i) == '.') {
                            dotIndex = i;
                            break;
                        }
                    }
                    break;
                }
            }
            if (dotIndex != -1 || prefix.isEmpty() || expressionString.length() < prefix.length() && prefix.charAt(expressionString.length()) == '.' && prefix.startsWith(expressionString) || expressionString.length() > prefix.length() && expressionString.charAt(prefix.length()) == '.' && expressionString.startsWith(prefix) || expressionString.equals(prefix)) {
            // We only do prefixing if the expressions have a non-common base to avoid prefixing already prefixed expressions
            // If both point to an alias, dotIndex will be -1 but then the expressions would have the same length
            // If we have a dotIndex, we know there is a common base, so just skip prefixing
            } else {
                sb.append(prefix);
                sb.append('.');
            }
        }
        // Remove the this property
        if ("this".equalsIgnoreCase(firstProperty)) {
            if (size > 1) {
                for (int i = 1; i < size; i++) {
                    expressions.get(i).accept(this);
                }
            } else {
                sb.setLength(sb.length() - 1);
            }
        } else {
            super.visit(expression);
        }
    }
}
Also used : TreatExpression(com.blazebit.persistence.parser.expression.TreatExpression) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) EntityLiteral(com.blazebit.persistence.parser.expression.EntityLiteral) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression)

Example 15 with ArrayExpression

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

ArrayExpression (com.blazebit.persistence.parser.expression.ArrayExpression)18 PropertyExpression (com.blazebit.persistence.parser.expression.PropertyExpression)16 PathElementExpression (com.blazebit.persistence.parser.expression.PathElementExpression)15 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)14 Expression (com.blazebit.persistence.parser.expression.Expression)13 FunctionExpression (com.blazebit.persistence.parser.expression.FunctionExpression)11 TreatExpression (com.blazebit.persistence.parser.expression.TreatExpression)10 ParameterExpression (com.blazebit.persistence.parser.expression.ParameterExpression)9 SubqueryExpression (com.blazebit.persistence.parser.expression.SubqueryExpression)9 MapKeyExpression (com.blazebit.persistence.parser.expression.MapKeyExpression)8 ListIndexExpression (com.blazebit.persistence.parser.expression.ListIndexExpression)7 MapEntryExpression (com.blazebit.persistence.parser.expression.MapEntryExpression)7 MapValueExpression (com.blazebit.persistence.parser.expression.MapValueExpression)7 QualifiedExpression (com.blazebit.persistence.parser.expression.QualifiedExpression)7 GeneralCaseExpression (com.blazebit.persistence.parser.expression.GeneralCaseExpression)6 ArrayList (java.util.ArrayList)4 EntityLiteral (com.blazebit.persistence.parser.expression.EntityLiteral)3 ExtendedManagedType (com.blazebit.persistence.spi.ExtendedManagedType)3 EntityType (javax.persistence.metamodel.EntityType)3 ManagedType (javax.persistence.metamodel.ManagedType)3