Search in sources :

Example 86 with Expression

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

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

the class JDOQLCompilerTest method testFilterExplicitParameter.

/**
 * Test for use of an explicit parameter in the filter.
 */
public void testFilterExplicitParameter() {
    // Test use of implicit variable in filter
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "name == param1", null, null, null, null, null, "java.lang.String param1", null, null);
        compilation = compiler.compile(null, null);
    } catch (NucleusUserException ne) {
        // TODO Debatable if this should throw a JDOUserException since the "notaField" is not bound, nor typed
        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;
    assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr.getLeft() instanceof PrimaryExpression);
    assertTrue("Compiled right expression should be ParameterExpression but isnt", dyExpr.getRight() instanceof ParameterExpression);
    PrimaryExpression left = (PrimaryExpression) dyExpr.getLeft();
    assertEquals("Primary expression name is wrong", left.getId(), "name");
    ParameterExpression right = (ParameterExpression) dyExpr.getRight();
    assertEquals("ParameterExpression has wrong value", "param1", right.getId());
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) 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) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Product(org.datanucleus.samples.store.Product) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 88 with Expression

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

the class JDOQLCompilerTest method testFilterComparisonWithAndOr.

/**
 * Tests for filter with field-literal comparison AND another comparison, and ORed with
 * another set of expressions.
 */
public void testFilterComparisonWithAndOr() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "(statusId == 2 && 100.0 > price) || (price >= 50 && price <= 95)", null, null, null, null, null, null, null, null);
        compilation = compiler.compile(null, 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;
    assertTrue("Compiled left expression should be DyadicExpression but isnt", dyExpr.getLeft() instanceof DyadicExpression);
    assertTrue("Compiled right expression should be DyadicExpression but isnt", dyExpr.getRight() instanceof DyadicExpression);
    DyadicExpression dyExpr1 = (DyadicExpression) dyExpr.getLeft();
    DyadicExpression dyExpr2 = (DyadicExpression) dyExpr.getRight();
    assertEquals("Operator between left and right is incorrect", Expression.OP_OR, dyExpr.getOperator());
    assertTrue("Compiled left(left) expression should be DyadicExpression but isnt", dyExpr1.getLeft() instanceof DyadicExpression);
    assertTrue("Compiled left(right) expression should be DyadicExpression but isnt", dyExpr1.getRight() instanceof DyadicExpression);
    DyadicExpression dyExpr1a = (DyadicExpression) dyExpr1.getLeft();
    DyadicExpression dyExpr1b = (DyadicExpression) dyExpr1.getRight();
    // 1a : statusId == 2
    assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr1a.getLeft() instanceof PrimaryExpression);
    assertTrue("Compiled right expression should be Literal but isnt", dyExpr1a.getRight() instanceof Literal);
    assertEquals("Operator between left (left and right) is incorrect", Expression.OP_EQ, dyExpr1a.getOperator());
    PrimaryExpression leftExpr1a = (PrimaryExpression) dyExpr1a.getLeft();
    assertEquals("Compiled left expression has incorrect number of tuples", 1, leftExpr1a.getTuples().size());
    assertEquals("Compiled left expression 'id' is incorrect", "statusId", leftExpr1a.getId());
    Literal rightExpr1a = (Literal) dyExpr1a.getRight();
    assertTrue("Compiled right expression literal is of incorrect type", rightExpr1a.getLiteral() instanceof Long);
    assertEquals("Compiled right expression literal has incorrect value", 2, ((Long) rightExpr1a.getLiteral()).longValue());
    // 1b : 100.0 > price
    assertTrue("Compiled right expression should be PrimaryExpression but isnt", dyExpr1b.getRight() instanceof PrimaryExpression);
    assertTrue("Compiled left expression should be Literal but isnt", dyExpr1b.getLeft() instanceof Literal);
    assertEquals("Operator between right (left and right) is incorrect", Expression.OP_GT, dyExpr1b.getOperator());
    PrimaryExpression rightExpr1b = (PrimaryExpression) dyExpr1b.getRight();
    assertEquals("Compiled left expression has incorrect number of tuples", 1, rightExpr1b.getTuples().size());
    assertEquals("Compiled left expression 'id' is incorrect", "price", rightExpr1b.getId());
    Literal leftExpr1b = (Literal) dyExpr1b.getLeft();
    // TODO Why BigDecimal and not Double??
    assertTrue("Compiled right expression literal is of incorrect type", leftExpr1b.getLiteral() instanceof BigDecimal);
    assertEquals("Compiled right expression literal has incorrect value", 100.0, ((BigDecimal) leftExpr1b.getLiteral()).longValue(), 0.1);
    assertTrue("Compiled right(left) expression should be DyadicExpression but isnt", dyExpr2.getLeft() instanceof DyadicExpression);
    assertTrue("Compiled right(right) expression should be DyadicExpression but isnt", dyExpr2.getRight() instanceof DyadicExpression);
    DyadicExpression dyExpr2a = (DyadicExpression) dyExpr2.getLeft();
    DyadicExpression dyExpr2b = (DyadicExpression) dyExpr2.getRight();
    // 2a : price >= 50
    assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr2a.getLeft() instanceof PrimaryExpression);
    assertTrue("Compiled right expression should be Literal but isnt", dyExpr2a.getRight() instanceof Literal);
    assertEquals("Operator between right (left and right) is incorrect", Expression.OP_GTEQ, dyExpr2a.getOperator());
    PrimaryExpression leftExpr2a = (PrimaryExpression) dyExpr2a.getLeft();
    assertEquals("Compiled left expression has incorrect number of tuples", 1, leftExpr2a.getTuples().size());
    assertEquals("Compiled left expression 'id' is incorrect", "price", leftExpr2a.getId());
    Literal rightExpr2a = (Literal) dyExpr2a.getRight();
    assertTrue("Compiled right expression literal is of incorrect type " + rightExpr2a.getLiteral().getClass().getName(), rightExpr2a.getLiteral() instanceof Long);
    assertEquals("Compiled right expression literal has incorrect value", 50.0, ((Long) rightExpr2a.getLiteral()).longValue(), 0.1);
    // 2b : price >= 50
    assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr2b.getLeft() instanceof PrimaryExpression);
    assertTrue("Compiled right expression should be Literal but isnt", dyExpr2b.getRight() instanceof Literal);
    assertEquals("Operator between right (left and right) is incorrect", Expression.OP_LTEQ, dyExpr2b.getOperator());
    PrimaryExpression leftExpr2b = (PrimaryExpression) dyExpr2b.getLeft();
    assertEquals("Compiled left expression has incorrect number of tuples", 1, leftExpr2b.getTuples().size());
    assertEquals("Compiled left expression 'id' is incorrect", "price", leftExpr2b.getId());
    Literal rightExpr2b = (Literal) dyExpr2b.getRight();
    assertTrue("Compiled right expression literal is of incorrect type", rightExpr2b.getLiteral() instanceof Long);
    assertEquals("Compiled right expression literal has incorrect value", 95.0, ((Long) rightExpr2b.getLiteral()).longValue(), 0.1);
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) 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) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) BigDecimal(java.math.BigDecimal)

Example 89 with Expression

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

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

the class JDOQLCompilerTest method testFilterWithCast.

/**
 * Tests for "(cast)expr" in filter.
 */
public void testFilterWithCast() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "((Book)this).author == 'Tolkien'", 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 DyadicExpression but is " + expr, expr instanceof DyadicExpression);
    DyadicExpression dyExpr = (DyadicExpression) expr;
    Expression leftExpr = dyExpr.getLeft();
    assertTrue("Left side should be PrimaryExpression but is " + leftExpr, leftExpr instanceof PrimaryExpression);
    PrimaryExpression primExpr = (PrimaryExpression) leftExpr;
    assertTrue("PrimaryExpression should have left of CastExpression but is " + primExpr.getLeft(), primExpr.getLeft() instanceof DyadicExpression);
    DyadicExpression castDyExpr = (DyadicExpression) primExpr.getLeft();
    assertTrue("Cast DyadicExpression left should be PrimaryExpression", castDyExpr.getLeft() instanceof PrimaryExpression);
    assertTrue("Cast DyadicExpression right should be PrimaryExpression", castDyExpr.getRight() instanceof Literal);
    assertEquals("Cast class is incorrect", "Book", ((Literal) castDyExpr.getRight()).getLiteral());
    PrimaryExpression castPrimExpr = (PrimaryExpression) castDyExpr.getLeft();
    assertEquals("Expression being cast is incorrect", "this", castPrimExpr.getId());
    assertEquals("PrimaryExpression off cast is incorrect", "author", primExpr.getId());
    Expression rightExpr = dyExpr.getRight();
    assertTrue("Right side should be Literal but is " + rightExpr, rightExpr instanceof Literal);
    assertEquals("Right side literal value is incorrect", "Tolkien", ((Literal) rightExpr).getLiteral());
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) 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) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Aggregations

Expression (org.datanucleus.query.expression.Expression)93 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)81 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)66 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)65 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)65 VariableExpression (org.datanucleus.query.expression.VariableExpression)57 NucleusException (org.datanucleus.exceptions.NucleusException)41 Literal (org.datanucleus.query.expression.Literal)37 OrderExpression (org.datanucleus.query.expression.OrderExpression)31 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)28 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)25 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)24 CreatorExpression (org.datanucleus.query.expression.CreatorExpression)23 ClassExpression (org.datanucleus.query.expression.ClassExpression)22 JoinExpression (org.datanucleus.query.expression.JoinExpression)22 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)22 HashMap (java.util.HashMap)17 ArrayExpression (org.datanucleus.query.expression.ArrayExpression)17 CaseExpression (org.datanucleus.query.expression.CaseExpression)17 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)16