Search in sources :

Example 61 with InvokeExpression

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

the class QueryUtils method getParameterExpressionForPosition.

/**
 * Convenience method to return the ParameterExpression for the specified position
 * if found in the expression tree starting at <pre>rootExpr</pre>
 * @param rootExpr The expression
 * @param pos The position
 * @return The ParameterExpression (if found)
 */
public static ParameterExpression getParameterExpressionForPosition(Expression rootExpr, int pos) {
    if (rootExpr instanceof ParameterExpression && ((ParameterExpression) rootExpr).getPosition() == pos) {
        return (ParameterExpression) rootExpr;
    }
    if (rootExpr.getLeft() != null) {
        ParameterExpression paramExpr = getParameterExpressionForPosition(rootExpr.getLeft(), pos);
        if (paramExpr != null) {
            return paramExpr;
        }
    }
    if (rootExpr.getRight() != null) {
        ParameterExpression paramExpr = getParameterExpressionForPosition(rootExpr.getRight(), pos);
        if (paramExpr != null) {
            return paramExpr;
        }
    }
    if (rootExpr instanceof InvokeExpression) {
        InvokeExpression invokeExpr = (InvokeExpression) rootExpr;
        List<Expression> args = invokeExpr.getArguments();
        if (args != null) {
            Iterator<Expression> argIter = args.iterator();
            while (argIter.hasNext()) {
                ParameterExpression paramExpr = getParameterExpressionForPosition(argIter.next(), pos);
                if (paramExpr != null) {
                    return paramExpr;
                }
            }
        }
    }
    return null;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression)

Example 62 with InvokeExpression

use of org.datanucleus.query.expression.InvokeExpression 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 63 with InvokeExpression

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

the class JDOQLCompilerTest method testFilterCollectionContainsVariablePlusExtraJoin.

/**
 * Tests for collection.contains(element) && elem.other.field == val.
 */
public void testFilterCollectionContainsVariablePlusExtraJoin() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Inventory.class, null, "products.contains(element) && element.guarantee.numberOfYears < 3", null, null, null, null, null, null, Product.class.getName() + " element", null);
        compilation = compiler.compile(new HashMap(), null);
    } catch (NucleusException ne) {
        NucleusLogger.QUERY.error("Exception thrown during compilation", 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;
    LOG.info(">> expr=" + dyExpr);
    // product.contains(element)
    assertTrue("Left expression should have been InvokeExpression but wasnt", dyExpr.getLeft() instanceof InvokeExpression);
    InvokeExpression leftExpr = (InvokeExpression) dyExpr.getLeft();
    assertTrue("InvokeExpression should have been invoked on PrimaryExpression but wasnt", leftExpr.getLeft() instanceof PrimaryExpression);
    assertEquals("Left expression : Name of field upon which we invoke the method was wrong", "products", ((PrimaryExpression) leftExpr.getLeft()).getId());
    assertEquals("Left expression : Name of invoked method was wrong", "contains", leftExpr.getOperation());
    assertEquals("Left expression : Number of parameters to contains() is wrong", 1, leftExpr.getArguments().size());
    Object param1 = leftExpr.getArguments().get(0);
    assertTrue("Left expression : Parameter1 to contains() is of wrong type", param1 instanceof VariableExpression);
    VariableExpression vrExpr = (VariableExpression) param1;
    assertEquals("Left expression : Name of variable to contains() is incorrect", "element", vrExpr.getId());
    // element.guarantee.numberOfYears < 3
    assertTrue("Right expression should have been DyadicExpression but wasnt", dyExpr.getRight() instanceof DyadicExpression);
    DyadicExpression rightExpr = (DyadicExpression) dyExpr.getRight();
    assertTrue("Right expression (left) should have been PrimaryExpression but wasnt", rightExpr.getLeft() instanceof PrimaryExpression);
    PrimaryExpression rightExprLeft = (PrimaryExpression) rightExpr.getLeft();
    assertTrue("Right expression (left).left is of incorrect type", rightExprLeft.getLeft() instanceof VariableExpression);
    VariableExpression rightExprLeftLeft = (VariableExpression) rightExprLeft.getLeft();
    assertTrue("Right expression (left).left is of incorrect type", rightExprLeft.getLeft() instanceof VariableExpression);
    assertEquals("Right expression (left) part1 is incorrect", "element", rightExprLeftLeft.getId());
    assertEquals("Right expression (left) has incorrect number of tuples", 2, rightExprLeft.getTuples().size());
    assertEquals("Right expression (left) part2 is incorrect", "guarantee", rightExprLeft.getTuples().get(0));
    assertEquals("Right expression (left) part2 is incorrect", "numberOfYears", rightExprLeft.getTuples().get(1));
    assertEquals("Right expression : Operator between left and right is incorrect", Expression.OP_LT, rightExpr.getOperator());
    assertTrue("Right expression (right) should have been Literal but wasnt", rightExpr.getRight() instanceof Literal);
    Literal rightExprRight = (Literal) rightExpr.getRight();
    assertEquals("Right expression (right) literal has incorrect value", 3, ((Long) rightExprRight.getLiteral()).longValue());
    // Check symbols
    SymbolTable symbols = compilation.getSymbolTable();
    assertTrue("Symbol table doesnt have entry for 'element'", symbols.hasSymbol("element"));
    assertTrue("Symbol table doesnt have entry for 'this'", symbols.hasSymbol("this"));
    Symbol sy1 = symbols.getSymbol("element");
    assertEquals("Type of symbol for 'element' is wrong", Product.class, sy1.getValueType());
    Symbol sy2 = symbols.getSymbol("this");
    assertEquals("Type of symbol for 'this' is wrong", Inventory.class, sy2.getValueType());
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) Symbol(org.datanucleus.query.compiler.Symbol) Product(org.datanucleus.samples.store.Product) SymbolTable(org.datanucleus.query.compiler.SymbolTable) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) Inventory(org.datanucleus.samples.store.Inventory)

Example 64 with InvokeExpression

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

the class JDOQLCompilerTest method testFilterWithStringIndexOfLiteral.

/**
 * Tests for "String.indexOf(Literal, int)" in filter.
 */
public void testFilterWithStringIndexOfLiteral() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "name.indexOf(\"nd\", 3)", null, null, null, null, null, null, null, null);
        compilation = compiler.compile(new HashMap(), null);
    } catch (NucleusException ne) {
        NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
        fail("compilation of filter with valid field threw exception : " + ne.getMessage());
    }
    Expression expr = compilation.getExprFilter();
    assertTrue("Compiled expression should have been InvokeExpression but wasnt", expr instanceof InvokeExpression);
    InvokeExpression invExpr = (InvokeExpression) expr;
    assertTrue("InvokeExpression should have been invoked on PrimaryExpression but wasnt", invExpr.getLeft() instanceof PrimaryExpression);
    assertEquals("Name of field upon which we invoke the method was wrong", "name", ((PrimaryExpression) invExpr.getLeft()).getId());
    assertEquals("Name of invoked method was wrong", "indexOf", invExpr.getOperation());
    assertEquals("Number of parameters is wrong", 2, invExpr.getArguments().size());
    Object param1 = invExpr.getArguments().get(0);
    assertTrue("Parameter1 to indexOf() is of wrong type", param1 instanceof Literal);
    Literal param1Lit = (Literal) param1;
    assertEquals("Parameter1 to indexOf() has wrong value", "nd", param1Lit.getLiteral());
    Object param2 = invExpr.getArguments().get(1);
    assertTrue("Parameter2 to indexOf() is of wrong type", param2 instanceof Literal);
    Literal param2Lit = (Literal) param2;
    assertEquals("Parameter2 to indexOf() has wrong value", new Long(3), param2Lit.getLiteral());
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) Product(org.datanucleus.samples.store.Product) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 65 with InvokeExpression

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

the class JDOQLCompilerTest method testFilterWithStringLiteralStartsWith.

/**
 * Tests for "StringLiteral.startsWith(var)" in filter.
 */
public void testFilterWithStringLiteralStartsWith() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "\"SomeString\".startsWith(name)", null, null, null, null, null, null, null, null);
        compilation = compiler.compile(new HashMap(), null);
    } catch (NucleusException ne) {
        NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
        fail("compilation of filter with valid field threw exception : " + ne.getMessage());
    }
    Expression expr = compilation.getExprFilter();
    assertTrue("Filter should be InvokeExpression but is " + expr, expr instanceof InvokeExpression);
    InvokeExpression invExpr = (InvokeExpression) expr;
    assertTrue("InvokeExpression should have been invoked on Literal but wasnt", invExpr.getLeft() instanceof Literal);
    assertEquals("Value of literal is wrong", "SomeString", ((Literal) invExpr.getLeft()).getLiteral());
    assertEquals("Name of invoked method was wrong", "startsWith", invExpr.getOperation());
    assertEquals("Number of parameters is wrong", 1, invExpr.getArguments().size());
    Object param1 = invExpr.getArguments().get(0);
    assertTrue("Parameter1 to startsWith() is of wrong type : " + param1, param1 instanceof PrimaryExpression);
    PrimaryExpression param1Expr = (PrimaryExpression) param1;
    assertEquals("Parameter1 expression has incorrect number of tuples", 1, param1Expr.getTuples().size());
    assertEquals("Parameter1 expression 'id' is incorrect", "name", param1Expr.getId());
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) Product(org.datanucleus.samples.store.Product) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException)

Aggregations

InvokeExpression (org.datanucleus.query.expression.InvokeExpression)65 Literal (org.datanucleus.query.expression.Literal)42 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)40 Expression (org.datanucleus.query.expression.Expression)39 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)38 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)37 NucleusException (org.datanucleus.exceptions.NucleusException)32 ArrayList (java.util.ArrayList)25 VariableExpression (org.datanucleus.query.expression.VariableExpression)21 BooleanExpression (javax.jdo.query.BooleanExpression)17 NumericExpression (javax.jdo.query.NumericExpression)17 PersistableExpression (javax.jdo.query.PersistableExpression)17 Expression (javax.jdo.query.Expression)15 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)13 OrderExpression (org.datanucleus.query.expression.OrderExpression)12 List (java.util.List)10 CharacterExpression (javax.jdo.query.CharacterExpression)10 StringExpression (javax.jdo.query.StringExpression)10 LocalDateTime (java.time.LocalDateTime)7 Calendar (java.util.Calendar)7