Search in sources :

Example 81 with Literal

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

use of org.datanucleus.query.expression.Literal 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)

Example 83 with Literal

use of org.datanucleus.query.expression.Literal 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

Literal (org.datanucleus.query.expression.Literal)83 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)62 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)61 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)60 NucleusException (org.datanucleus.exceptions.NucleusException)54 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)52 VariableExpression (org.datanucleus.query.expression.VariableExpression)38 Expression (org.datanucleus.query.expression.Expression)37 ArrayList (java.util.ArrayList)18 BooleanExpression (javax.jdo.query.BooleanExpression)18 NumericExpression (javax.jdo.query.NumericExpression)18 PersistableExpression (javax.jdo.query.PersistableExpression)18 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)18 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)17 OrderExpression (org.datanucleus.query.expression.OrderExpression)17 HashMap (java.util.HashMap)14 Expression (javax.jdo.query.Expression)14 ClassExpression (org.datanucleus.query.expression.ClassExpression)12 JoinExpression (org.datanucleus.query.expression.JoinExpression)12 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)12