Search in sources :

Example 1 with JoinType

use of org.datanucleus.query.expression.JoinExpression.JoinType in project datanucleus-core by datanucleus.

the class ExpressionCompiler method compileFromExpression.

/**
 * Primary entry point for compiling a node for the from clause.
 * @param node The node
 * @param classIsExpression whether the class of the from node is an expression relating to the outer query
 * @return Its compiled expression
 */
public Expression compileFromExpression(Node node, boolean classIsExpression) {
    if (node.getNodeType() == NodeType.CLASS) {
        Node aliasNode = node.getFirstChild();
        ClassExpression clsExpr = new ClassExpression((String) aliasNode.getNodeValue());
        if (classIsExpression) {
            clsExpr.setCandidateExpression((String) node.getNodeValue());
        }
        // Process any joins, chained down off the ClassExpression
        // So you can do clsExpr.getRight() to get the JoinExpression
        // then joinExpr.getRight() to get the next JoinExpression (if any)
        JoinExpression currentJoinExpr = null;
        Iterator childIter = node.getChildNodes().iterator();
        while (childIter.hasNext()) {
            Node childNode = (Node) childIter.next();
            if (childNode.getNodeType() == NodeType.OPERATOR) {
                String joinType = (String) childNode.getNodeValue();
                JoinType joinTypeId = JoinType.JOIN_INNER;
                if (joinType.equals(JavaQueryCompiler.JOIN_INNER_FETCH)) {
                    joinTypeId = JoinType.JOIN_INNER_FETCH;
                } else if (joinType.equals(JavaQueryCompiler.JOIN_OUTER_FETCH)) {
                    joinTypeId = JoinType.JOIN_LEFT_OUTER_FETCH;
                } else if (joinType.equals(JavaQueryCompiler.JOIN_OUTER_FETCH_RIGHT)) {
                    joinTypeId = JoinType.JOIN_RIGHT_OUTER_FETCH;
                } else if (joinType.equals(JavaQueryCompiler.JOIN_OUTER)) {
                    joinTypeId = JoinType.JOIN_LEFT_OUTER;
                } else if (joinType.equals(JavaQueryCompiler.JOIN_OUTER_RIGHT)) {
                    joinTypeId = JoinType.JOIN_RIGHT_OUTER;
                }
                Node joinedNode = childNode.getFirstChild();
                Expression joinedExpr = compilePrimaryExpression(joinedNode);
                Node joinedAliasNode = childNode.getNextChild();
                JoinExpression joinExpr = new JoinExpression(joinedExpr, (String) joinedAliasNode.getNodeValue(), joinTypeId);
                if (currentJoinExpr != null) {
                    currentJoinExpr.setJoinExpression(joinExpr);
                } else {
                    clsExpr.setJoinExpression(joinExpr);
                }
                if (childNode.hasNextChild()) {
                    // JOIN "ON" expression
                    Node nextNode = childNode.getNextChild();
                    Expression onExpr = compileExpression(nextNode);
                    if (onExpr != null) {
                        joinExpr.setOnExpression(onExpr);
                    }
                }
                currentJoinExpr = joinExpr;
            }
        }
        return clsExpr;
    }
    return null;
}
Also used : Node(org.datanucleus.query.compiler.Node) ParameterNode(org.datanucleus.query.compiler.ParameterNode) Iterator(java.util.Iterator) JoinType(org.datanucleus.query.expression.JoinExpression.JoinType)

Aggregations

Iterator (java.util.Iterator)1 Node (org.datanucleus.query.compiler.Node)1 ParameterNode (org.datanucleus.query.compiler.ParameterNode)1 JoinType (org.datanucleus.query.expression.JoinExpression.JoinType)1