Search in sources :

Example 36 with JavaQueryCompiler

use of org.datanucleus.query.compiler.JavaQueryCompiler 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 37 with JavaQueryCompiler

use of org.datanucleus.query.compiler.JavaQueryCompiler in project tests by datanucleus.

the class JDOQLCompilerTest method testResultGroupingForMethods.

/**
 * Test use of result clauses, and presence in grouping.
 */
public void testResultGroupingForMethods() {
    JavaQueryCompiler compiler = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Person.class, null, null, null, null, "birthDate.getYear() AS YEAR", "birthDate.getYear()", null, null, null, null);
        compiler.compile(null, null);
    } catch (NucleusUserException ne) {
        NucleusLogger.GENERAL.error(">> Exception in compile()", ne);
        fail("Compile of query with grouping and result clause using method and alias failed : " + ne.getMessage());
    }
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Person.class, null, null, null, null, "birthDate.getYear()/10", "birthDate.getYear()/10", null, null, null, null);
        compiler.compile(null, null);
    } catch (NucleusUserException ne) {
        NucleusLogger.GENERAL.error(">> Exception in compile()", ne);
        fail("Compile of query with grouping and result clause using method and alias failed : " + ne.getMessage());
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Person(org.jpox.samples.models.company.Person)

Example 38 with JavaQueryCompiler

use of org.datanucleus.query.compiler.JavaQueryCompiler 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)

Example 39 with JavaQueryCompiler

use of org.datanucleus.query.compiler.JavaQueryCompiler in project tests by datanucleus.

the class JDOQLCompilerTest method testQueryCompilationSerializable.

/**
 * Test for serialisability of QueryCompilation.
 */
public void testQueryCompilationSerializable() {
    // 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());
    }
    try {
        try {
            // Serialise the Expression
            FileOutputStream fileStream = new FileOutputStream("compilation.ser");
            ObjectOutputStream os = new ObjectOutputStream(fileStream);
            os.writeObject(compilation);
            os.close();
        } catch (Exception e) {
            NucleusLogger.GENERAL.error(">> Exception in serialise", e);
            fail("Failed to serialise " + StringUtils.toJVMIDString(compilation));
        }
        try {
            // Deserialise the Expression
            FileInputStream fileInputStream = new FileInputStream("compilation.ser");
            ObjectInputStream oInputStream = new ObjectInputStream(fileInputStream);
            Object obj = oInputStream.readObject();
            if (obj instanceof QueryCompilation) {
                QueryCompilation compilation1 = (QueryCompilation) obj;
                Expression expr1 = compilation1.getExprFilter();
                assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr1 instanceof DyadicExpression);
                DyadicExpression dyExpr1 = (DyadicExpression) expr1;
                assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr1.getLeft() instanceof PrimaryExpression);
                assertTrue("Compiled right expression should be ParameterExpression but isnt", dyExpr1.getRight() instanceof ParameterExpression);
                PrimaryExpression left1 = (PrimaryExpression) dyExpr1.getLeft();
                assertEquals("Primary expression name is wrong", left1.getId(), "name");
                ParameterExpression right1 = (ParameterExpression) dyExpr1.getRight();
                assertEquals("ParameterExpression has wrong value", "param1", right1.getId());
            } else {
                fail("Deserialised object is " + obj.getClass().getName() + " not QueryCompilation");
            }
            oInputStream.close();
        } catch (Exception e) {
            NucleusLogger.GENERAL.error(">> Exception in deserialise", e);
            fail("Failed to deserialise " + StringUtils.toJVMIDString(compilation));
        }
    } finally {
        // Delete the file
        File file = new File("compilation.ser");
        if (file.exists()) {
            file.delete();
        }
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Product(org.datanucleus.samples.store.Product) ObjectOutputStream(java.io.ObjectOutputStream) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NucleusException(org.datanucleus.exceptions.NucleusException) FileInputStream(java.io.FileInputStream) 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) FileOutputStream(java.io.FileOutputStream) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) File(java.io.File) ObjectInputStream(java.io.ObjectInputStream)

Example 40 with JavaQueryCompiler

use of org.datanucleus.query.compiler.JavaQueryCompiler in project tests by datanucleus.

the class JDOQLCompilerTest method testFilterImplicitParameter.

/**
 * Test for use of an implicit parameter in the filter.
 */
public void testFilterImplicitParameter() {
    // 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, null, 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)

Aggregations

JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)47 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)44 HashMap (java.util.HashMap)35 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)33 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)25 Expression (org.datanucleus.query.expression.Expression)25 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)25 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)25 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)25 VariableExpression (org.datanucleus.query.expression.VariableExpression)25 NucleusException (org.datanucleus.exceptions.NucleusException)23 Literal (org.datanucleus.query.expression.Literal)17 Product (org.datanucleus.samples.store.Product)17 List (java.util.List)16 OrderExpression (org.datanucleus.query.expression.OrderExpression)16 ArrayList (java.util.ArrayList)14 PersistenceManager (javax.jdo.PersistenceManager)14 Transaction (javax.jdo.Transaction)14 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)14 JDOQuery (org.datanucleus.api.jdo.JDOQuery)14