Package org.datanucleus.query.node

Examples of org.datanucleus.query.node.Node


        processAndExpression();

        while (p.parseStringIgnoreCase("OR "))
        {
            processAndExpression();
            Node expr = new Node(NodeType.OPERATOR, "||");
            expr.insertChildNode(stack.pop());
            expr.insertChildNode(stack.pop());
            stack.push(expr);
        }
    }
View Full Code Here


        processRelationalExpression();

        while (p.parseStringIgnoreCase("AND "))
        {
            processRelationalExpression();
            Node expr = new Node(NodeType.OPERATOR, "&&");
            expr.insertChildNode(stack.pop());
            expr.insertChildNode(stack.pop());
            stack.push(expr);
        }
    }
View Full Code Here

        for (;;)
        {
            if (p.parseString("="))
            {
                processAdditiveExpression();
                Node right = stack.pop();
                Node left = stack.pop();
                if (right.getNodeType() == NodeType.TYPE)
                {
                    // Convert TYPE into "instanceof"
                    Node primNode = right.getFirstChild();
                    Node expr = new Node(NodeType.OPERATOR, "instanceof");
                    expr.appendChildNode(primNode);
                    expr.appendChildNode(left);
                    stack.push(expr);
                }
                else if (left.getNodeType() == NodeType.TYPE)
                {
                    // Convert TYPE into "instanceof"
                    Node primNode = left.getFirstChild();
                    Node expr = new Node(NodeType.OPERATOR, "instanceof");
                    expr.appendChildNode(primNode);
                    expr.appendChildNode(right);
                    stack.push(expr);
                }
                else
                {
                    Node expr = new Node(NodeType.OPERATOR, "==");
                    expr.insertChildNode(right);
                    expr.insertChildNode(left);
                    stack.push(expr);
                }
            }
            else if (p.parseString("<>"))
            {
                processAdditiveExpression();
                Node right = stack.pop();
                Node left = stack.pop();
                if (right.getNodeType() == NodeType.TYPE)
                {
                    // Convert TYPE into "instanceof"
                    Node primNode = right.getFirstChild();
                    Node expr = new Node(NodeType.OPERATOR, "instanceof");
                    expr.appendChildNode(primNode);
                    expr.appendChildNode(left);
                    Node notNode = new Node(NodeType.OPERATOR, "!");
                    notNode.appendChildNode(expr);
                    stack.push(notNode);
                }
                else if (left.getNodeType() == NodeType.TYPE)
                {
                    // Convert TYPE into "instanceof"
                    Node primNode = left.getFirstChild();
                    Node expr = new Node(NodeType.OPERATOR, "instanceof");
                    expr.appendChildNode(primNode);
                    expr.appendChildNode(right);
                    Node notNode = new Node(NodeType.OPERATOR, "!");
                    notNode.appendChildNode(expr);
                    stack.push(notNode);
                }
                else
                {
                    Node expr = new Node(NodeType.OPERATOR, "!=");
                    expr.insertChildNode(right);
                    expr.insertChildNode(left);
                    stack.push(expr);
                }
            }
            else if (p.parseStringIgnoreCase("NOT "))
            {
                if (p.parseStringIgnoreCase("BETWEEN "))
                {
                    // {expression} NOT BETWEEN {lower} AND {upper}
                    Node inputNode = stack.pop();
                    processAdditiveExpression();
                    Node lowerNode = stack.pop();
                    if (p.parseStringIgnoreCase("AND "))
                    {
                        processAdditiveExpression();
                        Node upperNode = stack.pop();

                        Node leftNode = new Node(NodeType.OPERATOR, "<");
                        leftNode.appendChildNode(inputNode);
                        leftNode.appendChildNode(lowerNode);

                        Node rightNode = new Node(NodeType.OPERATOR, ">");
                        rightNode.appendChildNode(inputNode);
                        rightNode.appendChildNode(upperNode);

                        Node betweenNode = new Node(NodeType.OPERATOR, "||");
                        betweenNode.appendChildNode(leftNode);
                        betweenNode.appendChildNode(rightNode);
                        stack.push(betweenNode);
                    }
                    else
                    {
                        throw new NucleusUserException("Query has BETWEEN keyword with no AND clause");
                    }
                }
                else if (p.parseStringIgnoreCase("LIKE "))
                {
                    processLikeExpression();
                    Node notNode = new Node(NodeType.OPERATOR, "!");
                    notNode.insertChildNode(stack.pop());
                    stack.push(notNode);
                }
                else if (p.parseStringIgnoreCase("IN"))
                {
                    // {expression} NOT IN (expr1 [,expr2[,expr3]])
                    processInExpression(true);
                }
                else if (p.parseStringIgnoreCase("MEMBER "))
                {
                    processMemberExpression(true);
                }
                else
                {
                    throw new NucleusException("Unsupported query syntax NOT followed by unsupported keyword");
                }
            }
            else if (p.parseStringIgnoreCase("BETWEEN "))
            {
                // {expression} BETWEEN {lower} AND {upper}
                Node inputNode = stack.pop();
                processAdditiveExpression();
                Node lowerNode = stack.pop();
                if (p.parseStringIgnoreCase("AND "))
                {
                    processAdditiveExpression();
                    Node upperNode = stack.pop();
                    Node leftNode = new Node(NodeType.OPERATOR, ">=");
                    leftNode.appendChildNode(inputNode);
                    leftNode.appendChildNode(lowerNode);

                    Node rightNode = new Node(NodeType.OPERATOR, "<=");
                    rightNode.appendChildNode(inputNode);
                    rightNode.appendChildNode(upperNode);

                    Node betweenNode = new Node(NodeType.OPERATOR, "&&");
                    betweenNode.appendChildNode(rightNode);
                    betweenNode.appendChildNode(leftNode);
                    stack.push(betweenNode);
                }
                else
                {
                    throw new NucleusUserException("Query has BETWEEN keyword with no AND clause");
                }
            }
            else if (p.parseStringIgnoreCase("LIKE "))
            {
                // {expression} LIKE {pattern_value} [ESCAPE {escape_char}]
                processLikeExpression();
            }
            else if (p.parseStringIgnoreCase("IN"))
            {
                // {expression} IN (expr1 [,expr2[,expr3]])
                processInExpression(false);
            }
            else if (p.parseStringIgnoreCase("MEMBER "))
            {
                processMemberExpression(false);
            }
            else if (p.parseStringIgnoreCase("IS "))
            {
                // {expression} IS [NOT] [NULL | EMPTY]
                Node inputNode = stack.pop();
                Node inputRootNode = inputNode;
                if (inputNode.getNodeType() == NodeType.IDENTIFIER)
                {
                    // Find the end of the identifier chain
                    while (inputNode.getFirstChild() != null)
                    {
                        inputNode = inputNode.getFirstChild();
                    }
                }

                boolean not = false;
                if (p.parseStringIgnoreCase("NOT "))
                {
                    not = true;
                }

                if (p.parseStringIgnoreCase("NULL"))
                {
                    Node isNode = new Node(NodeType.OPERATOR, (not ? "!=" : "=="));
                    Node compareNode = new Node(NodeType.LITERAL, null);
                    isNode.insertChildNode(compareNode);
                    isNode.insertChildNode(inputRootNode);
                    stack.push(isNode);
                }
                else if (p.parseStringIgnoreCase("EMPTY"))
                {
                    // Convert IS EMPTY to a method call of "size()==0" on collection/map
                    Node sizeNode = new Node(NodeType.INVOKE, "size");
                    inputNode.insertChildNode(sizeNode);
                    Node isEmptyNode = new Node(NodeType.OPERATOR, not ? "!=" : "==");
                    isEmptyNode.appendChildNode(inputNode);
                    Node zeroNode = new Node(NodeType.LITERAL, 0);
                    isEmptyNode.appendChildNode(zeroNode);
                    stack.push(isEmptyNode);
                }
                else
                {
                    throw new NucleusException("Encountered IS " + (not ? "NOT " : " ") +
                        " that should be followed by NULL | EMPTY but isnt");
                }
            }
            else if (p.parseString("<="))
            {
                processAdditiveExpression();
                Node expr = new Node(NodeType.OPERATOR, "<=");
                expr.insertChildNode(stack.pop());
                expr.insertChildNode(stack.pop());
                stack.push(expr);
            }
            else if (p.parseString(">="))
            {
                processAdditiveExpression();
                Node expr = new Node(NodeType.OPERATOR, ">=");
                expr.insertChildNode(stack.pop());
                expr.insertChildNode(stack.pop());
                stack.push(expr);
            }
            else if (p.parseChar('<'))
            {
                processAdditiveExpression();
                Node expr = new Node(NodeType.OPERATOR, "<");
                expr.insertChildNode(stack.pop());
                expr.insertChildNode(stack.pop());
                stack.push(expr);
            }
            else if (p.parseChar('>'))
            {
                processAdditiveExpression();
                Node expr = new Node(NodeType.OPERATOR, ">");
                expr.insertChildNode(stack.pop());
                expr.insertChildNode(stack.pop());
                stack.push(expr);
            }
            else
            {
                break;
View Full Code Here

     * Requires that the node at the top of the stack is the field expression node.
     * Returns "{primaryNode}.INVOKE("matches", (likeExprNode))"
     */
    private void processLikeExpression()
    {
        Node primaryNode = stack.pop();
        Node primaryRootNode = primaryNode;
        if (primaryNode.getNodeType() == NodeType.IDENTIFIER)
        {
            // Make sure we put the INVOKE at the end of the identifier chain
            while (primaryNode.getFirstChild() != null)
            {
                primaryNode = primaryNode.getFirstChild();
            }
        }

        processAdditiveExpression();
        Node likeExprNode = stack.pop();

        if (p.parseStringIgnoreCase("ESCAPE"))
        {
            // Return matchesNode with 2 property nodes - the pattern expression, and the escape char
            processAdditiveExpression();
            Node escapeNode = stack.pop();
            Node matchesNode = new Node(NodeType.INVOKE, "matches");
            matchesNode.addProperty(likeExprNode);
            matchesNode.addProperty(escapeNode);

            primaryNode.appendChildNode(matchesNode);
            stack.push(primaryRootNode);
        }
        else
        {
            // Return matchesNode with 1 property node - the pattern expression
            Node matchesNode = new Node(NodeType.INVOKE, "matches");
            matchesNode.addProperty(likeExprNode);

            primaryNode.appendChildNode(matchesNode);
            stack.push(primaryRootNode);
        }
    }
View Full Code Here

     * @param not Whether this is an expression "NOT IN"
     */
    private void processInExpression(boolean not)
    {
        // TODO Cater for TYPE as the inputNode
        Node inputNode = stack.pop(); // The left hand side expression

        if (!p.parseChar('('))
        {
            // Subquery
            Node inNode = new Node(NodeType.OPERATOR, (not ? "NOT IN" : "IN"));
            inNode.appendChildNode(inputNode);

            processExpression(); // subquery variable
            Node subqueryNode = stack.pop();
            inNode.appendChildNode(subqueryNode);
            stack.push(inNode);
            return;
        }

        Node inNode = null;
        int numArgs = 0;
        do
        {
            // IN ((literal|parameter) [, (literal|parameter)])
            processPrimary();
            if (stack.peek() == null)
            {
                throw new QueryCompilerSyntaxException("Expected literal|parameter but got " +
                    p.remaining(), p.getIndex(), p.getInput());
            }

            // Generate node for comparison with this value
            numArgs++;
            Node valueNode = stack.pop();

            p.skipWS();
            if (numArgs == 1 && !p.peekStringIgnoreCase(",") && valueNode.getNodeType() == NodeType.PARAMETER &&
                parameterValues != null && parameterValues.containsKey(valueNode.getNodeValue()))
            {
                // Special case of "xxx IN :param" where param is multiple-valued
                Object paramValue = parameterValues.get(valueNode.getNodeValue());
                if (paramValue instanceof Collection)
                {
                    // Node (PARAMETER, param)
                    // ---> Node (INVOKE, "contains")
                    //      ---> Node(IDENTIFIER, inputNode)
                    Node containsNode = new Node(NodeType.INVOKE, "contains");
                    containsNode.addProperty(inputNode);
                    valueNode.appendChildNode(containsNode);
                    inNode = valueNode;
                    break;
                }
            }

            Node compareNode = new Node(NodeType.OPERATOR, (not ? "!=" : "=="));
            compareNode.appendChildNode(inputNode);
            compareNode.appendChildNode(valueNode);

            if (inNode == null)
            {
                inNode = compareNode;
            }
            else
            {
                Node newInNode = new Node(NodeType.OPERATOR, (not ? "&&" : "||"));
                newInNode.appendChildNode(inNode);
                newInNode.appendChildNode(compareNode);
                inNode = newInNode;
            }
        } while (p.parseChar(','));

        if (!p.parseChar(')'))
View Full Code Here

     * Expression is of the form "{expr1} MEMBER [OF] expr2".
     * @param not Whether this is an expression "NOT MEMBER"
     */
    private void processMemberExpression(boolean not)
    {
        Node inputNode = stack.pop(); // The left hand side expression
        p.parseStringIgnoreCase("OF"); // Ignore any "OF" keyword here (optional)
        processPrimary(); // Container node at top of stack
        Node containerNode = stack.peek();

        // Make sure we put the INVOKE at the end of the IDENTIFIER chain
        while (containerNode.getFirstChild() != null)
        {
            containerNode = containerNode.getFirstChild();
        }

        if (not)
        {
            Node notNode = new Node(NodeType.OPERATOR, "!");
            stack.pop();
            notNode.insertChildNode(containerNode);
            stack.push(notNode);
        }

        // Node (IDENTIFIER, container)
        // ---> Node (INVOKE, "contains")
        //      ---> Node(IDENTIFIER, containsNode)
        Node containsNode = new Node(NodeType.INVOKE, "contains");
        containsNode.addProperty(inputNode);
        containerNode.appendChildNode(containsNode);
    }
View Full Code Here

     * Creates a Node of type "CASE" with children in the order
     * <pre>whenNode, actionNode [, whenNode, actionNode]*, elseNode</pre>
     */
    private void processCaseExpression()
    {
        Node caseNode = new Node(NodeType.CASE);
        while (p.parseStringIgnoreCase("WHEN "))
        {
            processExpression();
            Node whenNode = stack.pop();
            caseNode.appendChildNode(whenNode);

            boolean hasThen = p.parseStringIgnoreCase("THEN ");
            if (!hasThen)
            {
                throw new QueryCompilerSyntaxException("expected 'THEN' as part of CASE", p.getIndex(), p.getInput());
            }
            processExpression();
            Node actionNode = stack.pop();
            caseNode.appendChildNode(actionNode);
        }
        if (p.parseStringIgnoreCase("ELSE "))
        {
            processExpression();
            Node elseNode = stack.pop();
            caseNode.appendChildNode(elseNode);
        }
        stack.push(caseNode);
    }
View Full Code Here

        for (;;)
        {
            if (p.parseChar('+'))
            {
                processMultiplicativeExpression();
                Node expr = new Node(NodeType.OPERATOR, "+");
                expr.insertChildNode(stack.pop());
                expr.insertChildNode(stack.pop());
                stack.push(expr);
            }
            else if (p.parseChar('-'))
            {
                processMultiplicativeExpression();
                Node expr = new Node(NodeType.OPERATOR, "-");
                expr.insertChildNode(stack.pop());
                expr.insertChildNode(stack.pop());
                stack.push(expr);
            }
            else
            {
                break;
View Full Code Here

        for (;;)
        {
            if (p.parseChar('*'))
            {
                processUnaryExpression();
                Node expr = new Node(NodeType.OPERATOR, "*");
                expr.insertChildNode(stack.pop());
                expr.insertChildNode(stack.pop());
                stack.push(expr);
            }
            else if (p.parseChar('/'))
            {
                processUnaryExpression();
                Node expr = new Node(NodeType.OPERATOR, "/");
                expr.insertChildNode(stack.pop());
                expr.insertChildNode(stack.pop());
                stack.push(expr);
            }
            else if (p.parseChar('%'))
            {
                processUnaryExpression();
                Node expr = new Node(NodeType.OPERATOR, "%");
                expr.insertChildNode(stack.pop());
                expr.insertChildNode(stack.pop());
                stack.push(expr);
            }
            else
            {
                break;
View Full Code Here

            processUnaryExpression();
        }
        else if (p.parseChar('-'))
        {
            processUnaryExpression();
            Node expr = new Node(NodeType.OPERATOR, "-");
            expr.insertChildNode(stack.pop());
            stack.push(expr);
        }
        else if (p.parseStringIgnoreCase("NOT "))
        {
            processRelationalExpression();
            Node expr = new Node(NodeType.OPERATOR, "!");
            expr.insertChildNode(stack.pop());
            stack.push(expr);
        }
        else
        {
            processPrimary();
View Full Code Here

TOP

Related Classes of org.datanucleus.query.node.Node

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.