Search in sources :

Example 6 with JDOQLCompiler

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

the class JDOQLEvaluatorTest method testFilterGreaterThanLessThan.

/**
 * Test of filter with ">", "<" and "+".
 */
public void testFilterGreaterThanLessThan() {
    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);
        Person p2 = new Person(102, "Donald", "Duck", "donald.duck@warnerbros.com");
        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, "age > 34");
        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());
        // Compile the query
        q = (JDOQuery) pm.newQuery(Person.class, "age+2 < 35");
        query = q.getInternalQuery();
        clr = query.getExecutionContext().getClassLoaderResolver();
        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);
        compilation = compiler.compile(new HashMap(), null);
        // Execute the query
        eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
        results = (List) eval.execute(true, true, true, true, true);
        assertEquals("Number of result instances was wrong", 1, results.size());
        p = (Person) results.get(0);
        assertEquals("Result instance has wrong first name", "Minnie", p.getFirstName());
        assertEquals("Result instance has wrong last name", "Mouse", p.getLastName());
        assertEquals("Person number of result instance is wrong", 103, p.getPersonNum());
        assertEquals("Age of result instance is wrong", 31, 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) 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)

Example 7 with JDOQLCompiler

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

the class JDOQLEvaluatorTest method testImplicitParametersPositionalRepeated.

/**
 * Test use of input params, positional, with one repeated before the other appears
 */
public void testImplicitParametersPositionalRepeated() {
    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");
        Person p2 = new Person(102, "Donald", "Duck", "donald.duck@warnerbros.com");
        Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
        instances.add(p1);
        instances.add(p2);
        instances.add(p3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "firstName.endsWith(:param1) && !lastName.endsWith(:param1)" + " && lastName.length() == :param2");
        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(null, null);
        // Execute the query
        Map<Integer, Object> paramValues = new HashMap<>();
        // Simulate what org.datanucleus.store.query.Query does
        paramValues.put(new Integer(0), "ald");
        paramValues.put(new Integer(1), 4);
        JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, paramValues, 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());
        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) 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)

Example 8 with JDOQLCompiler

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

the class JDOQLEvaluatorTest method testFilterOrWithEqualityOperator.

/**
 * Test of filter with OR and equality operator.
 */
public void testFilterOrWithEqualityOperator() {
    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");
        Person p2 = new Person(102, "Donald", "Duck", "donald.duck@warnerbros.com");
        Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
        instances.add(p1);
        instances.add(p2);
        instances.add(p3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "firstName == 'Mickey' || lastName == 'Mouse'");
        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", 2, results.size());
        Iterator iter = results.iterator();
        while (iter.hasNext()) {
            Person p = (Person) iter.next();
            if (p.getFirstName().equals("Mickey")) {
                assertEquals("Last name of result instance is wrong", "Mouse", p.getLastName());
                assertEquals("Person number of result instance is wrong", 101, p.getPersonNum());
            } else if (p.getFirstName().equals("Minnie")) {
                assertEquals("Last name of result instance is wrong", "Mouse", p.getLastName());
                assertEquals("Person number of result instance is wrong", 103, p.getPersonNum());
            } else {
                fail("Incorrect Person found by query " + p.asString());
            }
        }
        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) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) Person(org.jpox.samples.models.company.Person)

Example 9 with JDOQLCompiler

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

the class JDOQLEvaluatorTest method testFilterCollectionContainsNonPC.

/**
 * Test of filter with collectionField.contains(nonPC).
 */
public void testFilterCollectionContainsNonPC() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // Create some instances to query over
        List<SetHolder> instances = new ArrayList<>();
        SetHolder holder1 = new SetHolder("First");
        SetHolder holder2 = new SetHolder("Second");
        SetHolder holder3 = new SetHolder("Third");
        holder1.getJoinSetNonPC1().add("First Element");
        holder2.getJoinSetNonPC1().add("First Element");
        holder2.getJoinSetNonPC1().add("Second Element");
        holder3.getJoinSetNonPC1().add("Second Element");
        holder3.getJoinSetNonPC1().add("Third Element");
        instances.add(holder1);
        instances.add(holder2);
        instances.add(holder3);
        // Compile the query
        JDOQuery q = (JDOQuery) pm.newQuery(SetHolder.class, "joinSetNonPC1.contains('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());
        SetHolder holder = (SetHolder) 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) 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) SetHolder(org.jpox.samples.one_many.collection.SetHolder) 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 10 with JDOQLCompiler

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

the class JDOQLEvaluatorTest method testFilterNegate.

/**
 * Test of filter with !(condition).
 */
public void testFilterNegate() {
    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, "!(age > 32)");
        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", "Minnie", p.getFirstName());
        assertEquals("Result instance has wrong last name", "Mouse", p.getLastName());
        assertEquals("Person number of result instance is wrong", 103, p.getPersonNum());
        assertEquals("Age of result instance is wrong", 31, 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

JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)36 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)33 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)33 HashMap (java.util.HashMap)24 Product (org.datanucleus.samples.store.Product)17 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)16 Expression (org.datanucleus.query.expression.Expression)16 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)16 OrderExpression (org.datanucleus.query.expression.OrderExpression)16 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)16 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)16 VariableExpression (org.datanucleus.query.expression.VariableExpression)16 ArrayList (java.util.ArrayList)14 List (java.util.List)14 PersistenceManager (javax.jdo.PersistenceManager)14 Transaction (javax.jdo.Transaction)14 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)14 JDOQuery (org.datanucleus.api.jdo.JDOQuery)14 NucleusException (org.datanucleus.exceptions.NucleusException)14 JDOQLInMemoryEvaluator (org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator)14