Search in sources :

Example 61 with PrimaryExpression

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

the class JavaQueryCompiler method compileResult.

public Expression[] compileResult() {
    if (result == null) {
        return null;
    }
    Node[] node = parser.parseResult(result);
    Expression[] expr = new Expression[node.length];
    for (int i = 0; i < node.length; i++) {
        ExpressionCompiler comp = new ExpressionCompiler();
        comp.setSymbolTable(symtbl);
        comp.setMethodAliases(queryMgr.getQueryMethodAliasesByPrefix());
        // Find the last child of this node, and check for an alias (type = NAME)
        String alias = null;
        Node aliasNode = null;
        List<Node> childNodes = node[i].getChildNodes();
        if (childNodes != null && childNodes.size() > 0) {
            Node lastChild = childNodes.get(childNodes.size() - 1);
            if (lastChild.getNodeType() == NodeType.NAME) {
                // Alias node
                aliasNode = lastChild;
            }
        }
        if (aliasNode != null) {
            alias = (String) aliasNode.getNodeValue();
            node[i].removeChildNode(aliasNode);
        }
        if (candidateAliasOrig != null) {
            swapCandidateAliasNodeName(node[i]);
        }
        if (parameterSubtitutionMap != null) {
            node[i] = swapSubqueryParameters(node[i]);
        }
        expr[i] = comp.compileExpression(node[i]);
        if (alias != null) {
            expr[i].setAlias(alias);
        }
        try {
            expr[i].bind(symtbl);
        } catch (PrimaryExpressionIsClassLiteralException peil) {
            // PrimaryExpression should be swapped for a class Literal
            expr[i] = peil.getLiteral();
            expr[i].bind(symtbl);
        } catch (PrimaryExpressionIsClassStaticFieldException peil) {
            // PrimaryExpression should be swapped for a static field Literal
            Field fld = peil.getLiteralField();
            try {
                // Get the value of the static field
                Object value = fld.get(null);
                expr[i] = new Literal(value);
                expr[i].bind(symtbl);
            } catch (Exception e) {
                throw new NucleusUserException("Error processing static field " + fld.getName(), e);
            }
        } catch (PrimaryExpressionIsVariableException pive) {
            // PrimaryExpression should be swapped for an implicit variable
            expr[i] = pive.getVariableExpression();
            expr[i].bind(symtbl);
        }
        if (expr[i] instanceof PrimaryExpression) {
            String id = ((PrimaryExpression) expr[i]).getId();
            if (isKeyword(id)) {
                throw new NucleusUserException(Localiser.msg("021052", getLanguage(), id));
            }
        } else if (expr[i] instanceof ParameterExpression) {
            String id = ((ParameterExpression) expr[i]).getId();
            if (isKeyword(id)) {
                throw new NucleusUserException(Localiser.msg("021052", getLanguage(), id));
            }
        } else if (expr[i] instanceof VariableExpression) {
            String id = ((VariableExpression) expr[i]).getId();
            if (isKeyword(id)) {
                throw new NucleusUserException(Localiser.msg("021052", getLanguage(), id));
            }
        }
    }
    return expr;
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) PrimaryExpressionIsClassLiteralException(org.datanucleus.query.expression.PrimaryExpressionIsClassLiteralException) VariableExpression(org.datanucleus.query.expression.VariableExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) PrimaryExpressionIsClassStaticFieldException(org.datanucleus.query.expression.PrimaryExpressionIsClassStaticFieldException) QueryCompilerSyntaxException(org.datanucleus.store.query.QueryCompilerSyntaxException) PrimaryExpressionIsVariableException(org.datanucleus.query.expression.PrimaryExpressionIsVariableException) NucleusException(org.datanucleus.exceptions.NucleusException) PrimaryExpressionIsClassLiteralException(org.datanucleus.query.expression.PrimaryExpressionIsClassLiteralException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) PrimaryExpressionIsClassStaticFieldException(org.datanucleus.query.expression.PrimaryExpressionIsClassStaticFieldException) Field(java.lang.reflect.Field) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) PrimaryExpressionIsVariableException(org.datanucleus.query.expression.PrimaryExpressionIsVariableException) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) ExpressionCompiler(org.datanucleus.query.expression.ExpressionCompiler)

Example 62 with PrimaryExpression

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

the class VarThisCompilationOptimiser method findRedundantFilterVariables.

/**
 * Method to process the provided filter expression and find any variables that are to all intents
 * and purposes redundant. Checks for "var == this". In this case we can just replace the variable
 * occurrences with "this".
 * @param filterExpr The filter
 * @param varNames The variable names that are redundant (updated by this method)
 */
private void findRedundantFilterVariables(Expression filterExpr, Set<String> varNames) {
    if (filterExpr instanceof DyadicExpression) {
        DyadicExpression dyExpr = (DyadicExpression) filterExpr;
        if (dyExpr.getOperator() == Expression.OP_EQ) {
            if (dyExpr.getLeft() instanceof VariableExpression) {
                if (dyExpr.getRight() instanceof PrimaryExpression) {
                    PrimaryExpression rightExpr = (PrimaryExpression) dyExpr.getRight();
                    if (rightExpr.getId().equals(compilation.getCandidateAlias())) {
                        varNames.add(((VariableExpression) dyExpr.getLeft()).getId());
                    }
                }
            } else if (dyExpr.getRight() instanceof VariableExpression) {
                if (dyExpr.getLeft() instanceof PrimaryExpression) {
                    PrimaryExpression leftExpr = (PrimaryExpression) dyExpr.getLeft();
                    if (leftExpr.getId().equals(compilation.getCandidateAlias())) {
                        varNames.add(((VariableExpression) dyExpr.getRight()).getId());
                    }
                }
            }
        } else if (dyExpr.getOperator() == Expression.OP_AND) {
            findRedundantFilterVariables(dyExpr.getLeft(), varNames);
            findRedundantFilterVariables(dyExpr.getRight(), varNames);
        } else if (dyExpr.getOperator() == Expression.OP_OR) {
            findRedundantFilterVariables(dyExpr.getLeft(), varNames);
            findRedundantFilterVariables(dyExpr.getRight(), varNames);
        }
    }
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 63 with PrimaryExpression

use of org.datanucleus.query.expression.PrimaryExpression in project tests by datanucleus.

the class JPQLCompilerTest method testFilterWithNegateExpression.

/**
 * Tests for "!(expression)".
 */
public void testFilterWithNegateExpression() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "NOT (budget > 32)", null, null, null, null, null, null, null);
        compilation = compiler.compile(new HashMap(), null);
    } catch (NucleusException ne) {
        NucleusLogger.QUERY.error("Exception during compile", ne);
        fail("compilation of filter with valid field threw exception : " + ne.getMessage());
    }
    Expression expr = compilation.getExprFilter();
    assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr instanceof DyadicExpression);
    DyadicExpression dyExpr = (DyadicExpression) expr;
    assertTrue("Compiled left expression should have been DyadicExpression but wasnt", dyExpr.getLeft() instanceof DyadicExpression);
    assertNull("Compiled right expression should have been null but wasnt", dyExpr.getRight());
    assertEquals("Expression operator is wrong", Expression.OP_NOT, dyExpr.getOperator());
    DyadicExpression leftExpr = (DyadicExpression) dyExpr.getLeft();
    assertTrue("Left (left) should be PrimaryExpression but isnt", leftExpr.getLeft() instanceof PrimaryExpression);
    assertTrue("Left (right) should be Literal but isnt", leftExpr.getRight() instanceof Literal);
    assertEquals("Left expression operator is wrong", Expression.OP_GT, leftExpr.getOperator());
    PrimaryExpression primExpr = (PrimaryExpression) leftExpr.getLeft();
    assertEquals("Left (left) expression has incorrect number of tuples", 1, primExpr.getTuples().size());
    assertEquals("Left (left) expression 'id' is incorrect", "budget", primExpr.getId());
    Literal lit = (Literal) leftExpr.getRight();
    assertTrue("Left (right) expression literal is of incorrect type", lit.getLiteral() instanceof Long);
    assertEquals("Left (right) expression literal has incorrect value", 32, ((Long) lit.getLiteral()).longValue());
}
Also used : Project(org.datanucleus.samples.annotations.models.company.Project) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) JoinExpression(org.datanucleus.query.expression.JoinExpression) ClassExpression(org.datanucleus.query.expression.ClassExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 64 with PrimaryExpression

use of org.datanucleus.query.expression.PrimaryExpression in project tests by datanucleus.

the class JPQLCompilerTest method testFromMemberOfExpression.

/**
 * Tests for from clause with a "MEMBER OF {primary}" expression.
 */
public void testFromMemberOfExpression() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Department.class, null, ":param MEMBER OF projects", null, null, null, null, null, null, null);
        compilation = compiler.compile(new HashMap(), null);
    } catch (NucleusException ne) {
        NucleusLogger.QUERY.error("Exception during compile", ne);
        fail("compilation of filter with valid field threw exception : " + ne.getMessage());
    }
    // InvokeExpression{[PrimaryExpression{elements}].contains(ParameterExpression{param})}
    Expression expr = compilation.getExprFilter();
    assertTrue("Invalid type of filter expression", expr instanceof InvokeExpression);
    InvokeExpression invokeExpr = (InvokeExpression) expr;
    assertEquals("Invoke method is incorrect", "contains", invokeExpr.getOperation());
    assertTrue("Invoke left expression is of incorrect type", invokeExpr.getLeft() instanceof PrimaryExpression);
    PrimaryExpression leftExpr = (PrimaryExpression) invokeExpr.getLeft();
    assertEquals("Invoke left expression id is wrong", "projects", leftExpr.getId());
    List args = invokeExpr.getArguments();
    assertNotNull("Number of args is null!", args);
    assertEquals("Incorrect number of args to invoke", 1, args.size());
    assertTrue("Argument is of incorrect type", args.get(0) instanceof ParameterExpression);
    ParameterExpression argExpr = (ParameterExpression) args.get(0);
    assertEquals("Argument param name is incorrect", "param", argExpr.getId());
}
Also used : Department(org.datanucleus.samples.annotations.models.company.Department) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) JoinExpression(org.datanucleus.query.expression.JoinExpression) ClassExpression(org.datanucleus.query.expression.ClassExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler)

Example 65 with PrimaryExpression

use of org.datanucleus.query.expression.PrimaryExpression in project tests by datanucleus.

the class JPQLCompilerTest method testUpdateSimple.

/**
 * Test for use of update clause.
 */
public void testUpdateSimple() {
    // Test use of UPDATE clause
    try {
        JPQLCompiler compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, null, null, null, null, null, null, null, "name = \"Sample Name\"");
        QueryCompilation compilation = compiler.compile(null, null);
        Expression[] updateExprs = compilation.getExprUpdate();
        assertNotNull("Update clause is null but shouldnt be", updateExprs);
        assertEquals("Number of update expressions is incorrect", 1, updateExprs.length);
        assertTrue("Update expression is of incorrect type " + updateExprs[0].getClass().getName(), updateExprs[0] instanceof DyadicExpression);
        DyadicExpression updateExpr = (DyadicExpression) updateExprs[0];
        Expression left = updateExpr.getLeft();
        Expression right = updateExpr.getRight();
        Operator op = updateExpr.getOperator();
        assertEquals("Operator in update expression is wrong", op, Expression.OP_EQ);
        assertTrue("Left hand side in update is wrong", left instanceof PrimaryExpression);
        assertTrue("Right hand side in update is wrong", right instanceof Literal);
        PrimaryExpression primExpr = (PrimaryExpression) left;
        assertEquals("Left hand side primary is wrong", "name", primExpr.getId());
        Literal lit = (Literal) right;
        assertEquals("Right hand side literal is wrong", "Sample Name", lit.getLiteral());
    } catch (NucleusUserException ne) {
        fail("Exception thrown in compile of update clause : " + ne.getMessage());
    }
}
Also used : Operator(org.datanucleus.query.expression.Expression.Operator) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) JoinExpression(org.datanucleus.query.expression.JoinExpression) ClassExpression(org.datanucleus.query.expression.ClassExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Literal(org.datanucleus.query.expression.Literal) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Aggregations

PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)77 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)73 Literal (org.datanucleus.query.expression.Literal)60 NucleusException (org.datanucleus.exceptions.NucleusException)59 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)54 VariableExpression (org.datanucleus.query.expression.VariableExpression)48 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)46 Expression (org.datanucleus.query.expression.Expression)46 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)25 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)23 OrderExpression (org.datanucleus.query.expression.OrderExpression)22 HashMap (java.util.HashMap)16 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)15 ClassExpression (org.datanucleus.query.expression.ClassExpression)15 JoinExpression (org.datanucleus.query.expression.JoinExpression)15 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)15 Product (org.datanucleus.samples.store.Product)15 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)12 JPQLCompiler (org.datanucleus.query.compiler.JPQLCompiler)9 List (java.util.List)8