Search in sources :

Example 1 with TreatExpression

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

the class JpaUtils method getAttributeForJoining.

public static AttributeHolder getAttributeForJoining(EntityMetamodel metamodel, PathExpression expression) {
    JoinNode expressionBaseNode = ((JoinNode) expression.getPathReference().getBaseNode());
    Expression p = expression.getExpressions().get(0);
    while (!(p instanceof PropertyExpression)) {
        if (p instanceof PathExpression) {
            p = ((PathExpression) p).getExpressions().get(0);
        } else if (p instanceof QualifiedExpression) {
            p = ((QualifiedExpression) p).getPath().getExpressions().get(0);
        } else if (p instanceof ArrayExpression) {
            p = ((ArrayExpression) p).getBase();
        } else {
            p = ((TreatExpression) p).getExpression();
        }
    }
    String firstElementString = p.toString();
    String baseNodeAlias;
    JoinNode baseNode = expressionBaseNode;
    do {
        baseNodeAlias = baseNode.getAlias();
    } while (!firstElementString.equals(baseNodeAlias) && (baseNode = baseNode.getParent()) != null);
    if (baseNode == null) {
        baseNodeAlias = null;
        if (expressionBaseNode.getParent() == null) {
            baseNode = expressionBaseNode;
        } else {
            baseNode = expressionBaseNode.getParent();
        }
    }
    return getAttributeForJoining(metamodel, baseNode.getNodeType(), expression, baseNodeAlias);
}
Also used : PathExpression(com.blazebit.persistence.parser.expression.PathExpression) QualifiedExpression(com.blazebit.persistence.parser.expression.QualifiedExpression) 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) NullExpression(com.blazebit.persistence.parser.expression.NullExpression) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) SubqueryExpression(com.blazebit.persistence.parser.expression.SubqueryExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression)

Example 2 with TreatExpression

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

the class JoinManager method implicitJoin.

private JoinResult implicitJoin(JoinNode current, List<String> resultFields, PathExpression pathExpression, ClauseType fromClause, JoinType joinType, JoinNode currentJoinNode, Set<String> currentlyResolvingAliases, int start, int end, boolean allowParentAliases, boolean joinAllowed, boolean singularJoinAllowed, boolean idRemovable) {
    List<PathElementExpression> pathElements = pathExpression.getExpressions();
    PathElementExpression elementExpr;
    int singleValuedAssociationNameStartIndex = -1;
    int singleValuedAssociationNameEndIndex = -1;
    for (int i = start; i < end; i++) {
        AliasInfo aliasInfo;
        elementExpr = pathElements.get(i);
        if (elementExpr instanceof ArrayExpression) {
            ArrayExpression arrayExpr = (ArrayExpression) elementExpr;
            String joinRelationName;
            List<String> joinRelationAttributes;
            if (!resultFields.isEmpty()) {
                resultFields.add(arrayExpr.getBase().toString());
                joinRelationAttributes = resultFields;
                resultFields = new ArrayList<>();
                joinRelationName = StringUtils.join(".", joinRelationAttributes);
            } else {
                joinRelationName = arrayExpr.getBase().toString();
                joinRelationAttributes = Arrays.asList(joinRelationName);
            }
            current = current == null ? getRootNodeOrFail("Ambiguous join path [", joinRelationName, "] because of multiple root nodes!") : current;
            implicitJoinIndex(arrayExpr);
            // Find a node by a predicate match
            JoinNode matchingNode = findNode(current, joinRelationName, arrayExpr);
            if (matchingNode != null) {
                current = matchingNode;
            } else if (i == 0 && (aliasInfo = aliasManager.getAliasInfoForBottomLevel(joinRelationName)) != null) {
                // The first node is allowed to be a join alias
                if (aliasInfo instanceof SelectInfo) {
                    throw new IllegalArgumentException("Illegal reference to the select alias '" + joinRelationName + "'");
                }
                current = ((JoinAliasInfo) aliasInfo).getJoinNode();
                generateAndApplyOnPredicate(current, arrayExpr);
            } else {
                String joinAlias = getJoinAlias(arrayExpr);
                if (arrayExpr.getBase() instanceof PropertyExpression) {
                    final JoinResult result = createOrUpdateNode(current, joinRelationAttributes, null, joinAlias, joinType, currentJoinNode, true, false, joinAllowed, singularJoinAllowed);
                    current = result.baseNode;
                    resultFields = result.addToList(resultFields);
                } else {
                    joinAlias = aliasManager.generateJoinAlias(joinAlias);
                    Class<?> entityClass = ((EntityLiteral) arrayExpr.getBase()).getValue();
                    joinOn(null, current.getAlias(), entityClass, joinAlias, JoinType.LEFT, false).end();
                    current = ((JoinAliasInfo) aliasManager.getAliasInfo(joinAlias)).getJoinNode();
                }
                generateAndApplyOnPredicate(current, arrayExpr);
            }
        } else if (elementExpr instanceof TreatExpression) {
            if (i != 0 || current != null) {
                throw new IllegalArgumentException("A treat expression should be the first element in a path!");
            }
            TreatExpression treatExpression = (TreatExpression) elementExpr;
            boolean fromSubquery = false;
            boolean fromSelectAlias = false;
            boolean joinRequired = false;
            boolean fetch = false;
            current = implicitJoinTreatExpression((TreatExpression) elementExpr, joinAllowed, singularJoinAllowed, fromClause, joinType, currentJoinNode, currentlyResolvingAliases, fromSubquery, fromSelectAlias, true, false, fetch, false);
        } else if (elementExpr instanceof MapKeyExpression) {
            MapKeyExpression mapKeyExpression = (MapKeyExpression) elementExpr;
            boolean fromSubquery = false;
            boolean fromSelectAlias = false;
            boolean joinRequired = true;
            boolean fetch = false;
            current = joinMapKey(mapKeyExpression, null, fromClause, currentlyResolvingAliases, fromSubquery, fromSelectAlias, joinRequired, fetch, true, true);
        } else if (elementExpr instanceof MapValueExpression) {
            MapValueExpression mapValueExpression = (MapValueExpression) elementExpr;
            boolean fromSubquery = false;
            boolean fromSelectAlias = false;
            boolean joinRequired = true;
            boolean fetch = false;
            implicitJoin(mapValueExpression.getPath(), joinAllowed, singularJoinAllowed, true, null, fromClause, currentlyResolvingAliases, fromSubquery, fromSelectAlias, joinRequired, fetch);
            current = (JoinNode) mapValueExpression.getPath().getBaseNode();
        } else if (pathElements.size() == 1 && (aliasInfo = aliasManager.getAliasInfoForBottomLevel(elementExpr.toString())) != null) {
            if (aliasInfo instanceof SelectInfo) {
                throw new IllegalArgumentException("Can't dereference a select alias");
            } else {
                // Join alias usage like in "joinAlias.relationName"
                current = ((JoinAliasInfo) aliasInfo).getJoinNode();
            }
        } else {
            String elementExpressionString = elementExpr.toString();
            if (current == null) {
                // When no base is given, check if the attribute name is an alias
                aliasInfo = allowParentAliases ? aliasManager.getAliasInfo(elementExpressionString) : aliasManager.getAliasInfoForBottomLevel(elementExpressionString);
                if (aliasInfo instanceof JoinAliasInfo) {
                    current = ((JoinAliasInfo) aliasInfo).getJoinNode();
                    // We can only "consider" this path a single valued association id when we are about to "remove" the id part
                    if (idRemovable && current.getNodeType() instanceof ManagedType<?> && pathElements.size() == i + 2) {
                        ExtendedManagedType<?> managedType = metamodel.getManagedType(ExtendedManagedType.class, current.getManagedType());
                        if (contains(managedType.getIdAttributes(), pathElements.get(i + 1))) {
                            singleValuedAssociationNameStartIndex = 0;
                            singleValuedAssociationNameEndIndex = 0;
                            break;
                        }
                    }
                    continue;
                } else {
                    current = getRootNodeOrFail("Ambiguous join path [", elementExpressionString, "] because of multiple root nodes!");
                }
            }
            int pathElementsSize = pathElements.size();
            if (joinType != JoinType.INNER && (idRemovable || mainQuery.jpaProvider.supportsSingleValuedAssociationIdExpressions()) && (current.getManagedType().getPersistenceType() != Type.PersistenceType.EMBEDDABLE || current.getValuesLikeAttribute() != null) && i + 1 < pathElementsSize) {
                // If the current type is not an embeddable, we check if the path elements access a singular association id
                List<PathElementExpression> pathElementExpressions = new ArrayList<>(pathElementsSize - i);
                for (int j = i; j < pathElementsSize; j++) {
                    PathElementExpression pathElementExpression = pathElements.get(j);
                    if (!(pathElementExpression instanceof PropertyExpression)) {
                        break;
                    }
                    pathElementExpressions.add(pathElementExpression);
                }
                ExtendedManagedType<?> extendedManagedType = metamodel.getManagedType(ExtendedManagedType.class, current.getManagedType());
                // We collect and check if we have only property expressions
                if (pathElementExpressions.size() == pathElementsSize - i) {
                    // Only if all path elements are property expressions, we check if this is single valued association id
                    PathExpression pathRestExpression = new PathExpression(pathElementExpressions);
                    String pathRestString = pathRestExpression.toString();
                    int idx = 0;
                    ExtendedAttribute<?, ?> extendedAttribute;
                    if (current.getValuesLikeAttribute() == null) {
                        extendedAttribute = extendedManagedType.getOwnedSingularAttributes().get(pathRestString);
                    } else {
                        extendedAttribute = extendedManagedType.getAttributes().get(pathRestString);
                    }
                    if (extendedAttribute != null && !JpaMetamodelUtils.isAssociation(extendedAttribute.getAttribute())) {
                        ExtendedAttribute<?, ?> associationAttribute = null;
                        ExtendedAttribute<?, ?> attr;
                        singleValuedAssociationNameStartIndex = i;
                        List<String> newResultFields = new ArrayList<>();
                        for (int j = i; j < end; j++) {
                            idx = pathRestString.indexOf('.', idx + 1);
                            if (idx != -1 && JpaMetamodelUtils.isAssociation((attr = extendedManagedType.getAttribute(pathRestString.substring(0, idx))).getAttribute())) {
                                associationAttribute = attr;
                                singleValuedAssociationNameEndIndex = j;
                            }
                            newResultFields.add(pathElements.get(j).toString());
                        }
                        if (singleValuedAssociationNameEndIndex == -1) {
                            // The expression ends at an association, so this can't be a single valued association id expression
                            singleValuedAssociationNameStartIndex = -1;
                        } else if (current.getValueType() == null && mainQuery.jpaProvider.isForeignJoinColumn((EntityType<?>) current.getManagedType(), new PathExpression(pathElements.subList(singleValuedAssociationNameStartIndex, singleValuedAssociationNameEndIndex + 1)).toString()) || current.getValueType() != null && mainQuery.jpaProvider.isForeignJoinColumn(current.getValueType(), current.getValuesLikeAttribute() + "." + new PathExpression(pathElements.subList(singleValuedAssociationNameStartIndex, singleValuedAssociationNameEndIndex + 1)).toString())) {
                            // If the column is "foreign", we can't do any optimizations
                            singleValuedAssociationNameStartIndex = -1;
                        } else if (!mainQuery.jpaProvider.supportsSingleValuedAssociationNaturalIdExpressions() && !contains(metamodel.getManagedType(ExtendedManagedType.class, associationAttribute.getElementClass()), new PathExpression(pathElements.subList(singleValuedAssociationNameEndIndex + 1, pathElementsSize)))) {
                            // If the jpa provider doesn't support any optimizations, we are done
                            singleValuedAssociationNameStartIndex = -1;
                        } else {
                            resultFields.addAll(newResultFields);
                            break;
                        }
                    }
                }
            }
            if (resultFields.isEmpty()) {
                final JoinResult result = implicitJoinSingle(current, elementExpressionString, null, joinType, currentJoinNode, allowParentAliases, joinAllowed, singularJoinAllowed);
                if (current != result.baseNode) {
                    current = result.baseNode;
                }
                resultFields = result.addToList(resultFields);
            } else {
                resultFields.add(elementExpressionString);
                JoinResult currentResult = createOrUpdateNode(current, resultFields, null, null, joinType, currentJoinNode, true, true, joinAllowed, singularJoinAllowed);
                current = currentResult.baseNode;
                if (!currentResult.hasField()) {
                    resultFields.clear();
                }
            }
        }
    }
    if (resultFields.isEmpty()) {
        return new JoinResult(current, null, current == null ? null : current.getNodeType(), singleValuedAssociationNameStartIndex, singleValuedAssociationNameEndIndex);
    } else {
        List<PathElementExpression> pathElementExpressions = new ArrayList<>(resultFields.size());
        for (int i = 0; i < resultFields.size(); i++) {
            pathElementExpressions.add(new PropertyExpression(resultFields.get(i)));
        }
        Expression expression = new PathExpression(pathElementExpressions);
        Type<?> type = JpaUtils.getAttributeForJoining(metamodel, current.getNodeType(), expression, null).getAttributeType();
        return new JoinResult(current, resultFields, type, singleValuedAssociationNameStartIndex, singleValuedAssociationNameEndIndex);
    }
}
Also used : TreatExpression(com.blazebit.persistence.parser.expression.TreatExpression) MapValueExpression(com.blazebit.persistence.parser.expression.MapValueExpression) ArrayList(java.util.ArrayList) MapKeyExpression(com.blazebit.persistence.parser.expression.MapKeyExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) ManagedType(javax.persistence.metamodel.ManagedType) ExtendedManagedType(com.blazebit.persistence.spi.ExtendedManagedType) ExtendedManagedType(com.blazebit.persistence.spi.ExtendedManagedType) EntityType(javax.persistence.metamodel.EntityType) 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 3 with TreatExpression

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

the class JoinManager method join.

JoinNode join(Expression expr, String alias, JoinType type, boolean fetch, boolean defaultJoin, String deReferenceFunction) {
    PathElementExpression elementExpr;
    String treatType = null;
    JoinResult result;
    JoinNode current;
    if (type == JoinType.FULL) {
        hasFullJoin = true;
    }
    if (expr instanceof PathExpression) {
        PathExpression pathExpression = (PathExpression) expr;
        if (isExternal(pathExpression) || isJoinableSelectAlias(pathExpression, false, false)) {
            throw new IllegalArgumentException("No external path or select alias allowed in join path");
        }
        List<PathElementExpression> pathElements = pathExpression.getExpressions();
        elementExpr = pathElements.get(pathElements.size() - 1);
        result = implicitJoin(null, pathExpression, null, null, null, new HashSet<String>(), 0, pathElements.size() - 1, false, true, true, false);
        current = result.baseNode;
    } else if (expr instanceof QualifiedExpression) {
        elementExpr = (PathElementExpression) expr;
        result = null;
        current = null;
    } else if (expr instanceof TreatExpression) {
        TreatExpression treatExpression = (TreatExpression) expr;
        if (isExternal(treatExpression)) {
            throw new IllegalArgumentException("No external path or select alias allowed in join path");
        }
        Expression expression = treatExpression.getExpression();
        if (expression instanceof PathExpression) {
            PathExpression pathExpression = (PathExpression) expression;
            List<PathElementExpression> pathElements = pathExpression.getExpressions();
            elementExpr = pathElements.get(pathElements.size() - 1);
            result = implicitJoin(null, pathExpression, null, null, null, new HashSet<String>(), 0, pathElements.size() - 1, false, true, true, false);
            current = result.baseNode;
            treatType = treatExpression.getType();
        } else {
            throw new IllegalArgumentException("Unexpected expression type[" + expression.getClass().getSimpleName() + "] in treat expression: " + treatExpression);
        }
    } else {
        throw new IllegalArgumentException("Join path [" + expr + "] is not a path");
    }
    if (elementExpr instanceof ArrayExpression) {
        ArrayExpression arrayExpr = (ArrayExpression) elementExpr;
        implicitJoinIndex(arrayExpr);
        if (arrayExpr.getBase() instanceof PropertyExpression) {
            List<String> resultFields = result.addToList(new ArrayList<String>());
            current = current == null ? getRootNodeOrFail("Could not join path [", expr, "] because it did not use an absolute path but multiple root nodes are available!") : current;
            resultFields.add(arrayExpr.getBase().toString());
            result = createOrUpdateNode(current, resultFields, treatType, alias, type, null, false, defaultJoin, true, true);
        } else {
            Class<?> entityClass = ((EntityLiteral) arrayExpr.getBase()).getValue();
            joinOn(null, rootNodes.get(0).getAlias(), entityClass, alias, JoinType.LEFT, false).end();
            result = new JoinResult(((JoinAliasInfo) aliasManager.getAliasInfo(alias)).getJoinNode());
        }
        generateAndApplyOnPredicate(result.baseNode, arrayExpr);
    } else if (elementExpr instanceof MapKeyExpression) {
        MapKeyExpression mapKeyExpression = (MapKeyExpression) elementExpr;
        boolean fromSubquery = false;
        boolean fromSelectAlias = false;
        boolean joinRequired = true;
        current = joinMapKey(mapKeyExpression, alias, null, new HashSet<String>(), fromSubquery, fromSelectAlias, joinRequired, fetch, false, defaultJoin);
        result = new JoinResult(current);
    } else {
        List<String> joinRelationAttributes = result.addToList(new ArrayList<String>());
        joinRelationAttributes.add(elementExpr.toString());
        current = current == null ? getRootNodeOrFail("Could not join path [", expr, "] because it did not use an absolute path but multiple root nodes are available!") : current;
        result = createOrUpdateNode(current, joinRelationAttributes, treatType, alias, type, null, false, defaultJoin, true, true);
    }
    result.baseNode.setDeReferenceFunction(deReferenceFunction);
    if (fetch) {
        fetchPath(result.baseNode);
    }
    return result.baseNode;
}
Also used : TreatExpression(com.blazebit.persistence.parser.expression.TreatExpression) EntityLiteral(com.blazebit.persistence.parser.expression.EntityLiteral) QualifiedExpression(com.blazebit.persistence.parser.expression.QualifiedExpression) ArrayList(java.util.ArrayList) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) MapKeyExpression(com.blazebit.persistence.parser.expression.MapKeyExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) 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) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with TreatExpression

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

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

the class EqualityCheckingVisitor method visit.

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

Aggregations

TreatExpression (com.blazebit.persistence.parser.expression.TreatExpression)11 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)9 PropertyExpression (com.blazebit.persistence.parser.expression.PropertyExpression)9 PathElementExpression (com.blazebit.persistence.parser.expression.PathElementExpression)7 SubqueryExpression (com.blazebit.persistence.parser.expression.SubqueryExpression)7 ArrayExpression (com.blazebit.persistence.parser.expression.ArrayExpression)6 Expression (com.blazebit.persistence.parser.expression.Expression)6 FunctionExpression (com.blazebit.persistence.parser.expression.FunctionExpression)6 ParameterExpression (com.blazebit.persistence.parser.expression.ParameterExpression)6 ListIndexExpression (com.blazebit.persistence.parser.expression.ListIndexExpression)5 MapEntryExpression (com.blazebit.persistence.parser.expression.MapEntryExpression)5 MapKeyExpression (com.blazebit.persistence.parser.expression.MapKeyExpression)5 MapValueExpression (com.blazebit.persistence.parser.expression.MapValueExpression)5 QualifiedExpression (com.blazebit.persistence.parser.expression.QualifiedExpression)5 ArrayList (java.util.ArrayList)5 GeneralCaseExpression (com.blazebit.persistence.parser.expression.GeneralCaseExpression)4 EntityLiteral (com.blazebit.persistence.parser.expression.EntityLiteral)3 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 Predicate (com.blazebit.persistence.parser.predicate.Predicate)2