Search in sources :

Example 46 with QueryCompilation

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

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

Example 48 with QueryCompilation

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

the class JDOQLEvaluatorTest method testFilterMapContainsKeyNonPC.

/**
 * Test of filter with mapField.containsKey(nonPC).
 */
public void testFilterMapContainsKeyNonPC() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create some instances to query over
        List<MapHolder> instances = new ArrayList<>();
        MapHolder holder1 = new MapHolder("First");
        MapHolder holder2 = new MapHolder("Second");
        MapHolder holder3 = new MapHolder("Third");
        holder1.getJoinMapNonNon().put("First", "First Element");
        holder2.getJoinMapNonNon().put("First", "First Element");
        holder2.getJoinMapNonNon().put("Second", "Second Element");
        holder3.getJoinMapNonNon().put("Second", "Second Element");
        holder3.getJoinMapNonNon().put("Third", "Third Element");
        instances.add(holder1);
        instances.add(holder2);
        instances.add(holder3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(MapHolder.class, "joinMapNonNon.containsKey('Third')");
        Query query = q.getInternalQuery();
        ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
        JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
        QueryCompilation compilation = compiler.compile(new HashMap(), null);
        // Execute the query
        JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
        List results = (List) eval.execute(true, true, true, true, true);
        assertEquals("Number of result instances was wrong", 1, results.size());
        MapHolder holder = (MapHolder) results.get(0);
        assertEquals("Result instance has wrong name", "Third", holder.getName());
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown during query execution " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) Query(org.datanucleus.store.query.Query) JDOQuery(org.datanucleus.api.jdo.JDOQuery) PersistenceManager(javax.jdo.PersistenceManager) MapHolder(org.jpox.samples.one_many.map.MapHolder) HashMap(java.util.HashMap) JavaQueryInMemoryEvaluator(org.datanucleus.query.inmemory.JavaQueryInMemoryEvaluator) ArrayList(java.util.ArrayList) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JDOQLInMemoryEvaluator(org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator) JDOQuery(org.datanucleus.api.jdo.JDOQuery) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation)

Example 49 with QueryCompilation

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

the class JDOQLEvaluatorTest method testFilterMapContainsValueNonPC.

/**
 * Test of filter with mapField.containsValue(nonPC).
 */
public void testFilterMapContainsValueNonPC() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create some instances to query over
        List<MapHolder> instances = new ArrayList<>();
        MapHolder holder1 = new MapHolder("First");
        MapHolder holder2 = new MapHolder("Second");
        MapHolder holder3 = new MapHolder("Third");
        holder1.getJoinMapNonNon().put("First", "First Element");
        holder2.getJoinMapNonNon().put("First", "First Element");
        holder2.getJoinMapNonNon().put("Second", "Second Element");
        holder3.getJoinMapNonNon().put("Second", "Second Element");
        holder3.getJoinMapNonNon().put("Third", "Third Element");
        instances.add(holder1);
        instances.add(holder2);
        instances.add(holder3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(MapHolder.class, "joinMapNonNon.containsValue('Third Element')");
        Query query = q.getInternalQuery();
        ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
        JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
        QueryCompilation compilation = compiler.compile(new HashMap(), null);
        // Execute the query
        JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
        List results = (List) eval.execute(true, true, true, true, true);
        assertEquals("Number of result instances was wrong", 1, results.size());
        MapHolder holder = (MapHolder) results.get(0);
        assertEquals("Result instance has wrong name", "Third", holder.getName());
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown during query execution " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) Query(org.datanucleus.store.query.Query) JDOQuery(org.datanucleus.api.jdo.JDOQuery) PersistenceManager(javax.jdo.PersistenceManager) MapHolder(org.jpox.samples.one_many.map.MapHolder) HashMap(java.util.HashMap) JavaQueryInMemoryEvaluator(org.datanucleus.query.inmemory.JavaQueryInMemoryEvaluator) ArrayList(java.util.ArrayList) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JDOQLInMemoryEvaluator(org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator) JDOQuery(org.datanucleus.api.jdo.JDOQuery) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation)

Example 50 with QueryCompilation

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

the class JDOQLEvaluatorTest method testFilterInstanceOf.

/**
 * Test of filter with "instanceof".
 */
public void testFilterInstanceOf() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create some instances to query over
        List<Person> instances = new ArrayList<>();
        Person p1 = new Person(101, "Mickey", "Mouse", "mickey.mouse@warnerbros.com");
        p1.setAge(34);
        Employee p2 = new Employee(102, "Donald", "Duck", "donald.duck@warnerbros.com", 13400.0f, "12345");
        p2.setAge(38);
        Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
        p3.setAge(31);
        instances.add(p1);
        instances.add(p2);
        instances.add(p3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "this instanceof " + Employee.class.getName());
        Query query = q.getInternalQuery();
        ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
        JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
        QueryCompilation compilation = compiler.compile(new HashMap(), null);
        // Execute the query
        JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
        List results = (List) eval.execute(true, true, true, true, true);
        assertEquals("Number of result instances was wrong", 1, results.size());
        Person p = (Person) results.get(0);
        assertEquals("Result instance has wrong first name", "Donald", p.getFirstName());
        assertEquals("Result instance has wrong last name", "Duck", p.getLastName());
        assertEquals("Person number of result instance is wrong", 102, p.getPersonNum());
        assertEquals("Age of result instance is wrong", 38, p.getAge());
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown during query execution " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) Query(org.datanucleus.store.query.Query) JDOQuery(org.datanucleus.api.jdo.JDOQuery) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) JavaQueryInMemoryEvaluator(org.datanucleus.query.inmemory.JavaQueryInMemoryEvaluator) ArrayList(java.util.ArrayList) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JDOQLInMemoryEvaluator(org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator) JDOQuery(org.datanucleus.api.jdo.JDOQuery) Employee(org.jpox.samples.models.company.Employee) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) Person(org.jpox.samples.models.company.Person)

Aggregations

QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)54 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)44 HashMap (java.util.HashMap)34 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)33 Expression (org.datanucleus.query.expression.Expression)28 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)28 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)27 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)27 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)27 VariableExpression (org.datanucleus.query.expression.VariableExpression)27 NucleusException (org.datanucleus.exceptions.NucleusException)23 Literal (org.datanucleus.query.expression.Literal)18 List (java.util.List)17 OrderExpression (org.datanucleus.query.expression.OrderExpression)16 Product (org.datanucleus.samples.store.Product)16 Query (org.datanucleus.store.query.Query)16 ArrayList (java.util.ArrayList)15 JDOQuery (org.datanucleus.api.jdo.JDOQuery)15 PersistenceManager (javax.jdo.PersistenceManager)14 Transaction (javax.jdo.Transaction)14