Search in sources :

Example 11 with JDOQLInMemoryEvaluator

use of org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator in project tests by datanucleus.

the class JDOQLEvaluatorTest method testImplicitParametersPositional.

/**
 * Test use of input params, positional.
 */
public void testImplicitParametersPositional() {
    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(:param)");
        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");
        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 12 with JDOQLInMemoryEvaluator

use of org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator in project tests by datanucleus.

the class JDOQLEvaluatorTest method testFilterStringStartsWith.

/**
 * Test of filter with String.startsWith().
 */
public void testFilterStringStartsWith() {
    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.startsWith('Mi')");
        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 13 with JDOQLInMemoryEvaluator

use of org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator in project tests by datanucleus.

the class JDOQLEvaluatorTest method testFilterAndWithEqualityOperator.

/**
 * Test of filter with AND and equality operator.
 */
public void testFilterAndWithEqualityOperator() {
    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", 1, results.size());
        Person p = (Person) results.get(0);
        assertEquals("Result instance has wrong first name", "Mickey", p.getFirstName());
        assertEquals("Result instance has wrong last name", "Mouse", p.getLastName());
        assertEquals("Person number of result instance is wrong", 101, 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 14 with JDOQLInMemoryEvaluator

use of org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator in project tests by datanucleus.

the class JDOQLEvaluatorTest method testFilterEqualityOperator.

/**
 * Test of filter with == and != operators.
 */
public void testFilterEqualityOperator() {
    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'");
        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", "Mickey", p.getFirstName());
        assertEquals("Result instance has wrong last name", "Mouse", p.getLastName());
        assertEquals("Person number of result instance is wrong", 101, p.getPersonNum());
        // Compile the query
        q = (JDOQuery) pm.newQuery(Person.class, "lastName != 'Mouse'");
        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", "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)

Aggregations

ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)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 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)14 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)14 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)14 JDOQLInMemoryEvaluator (org.datanucleus.query.inmemory.JDOQLInMemoryEvaluator)14 JavaQueryInMemoryEvaluator (org.datanucleus.query.inmemory.JavaQueryInMemoryEvaluator)14 Query (org.datanucleus.store.query.Query)14 Person (org.jpox.samples.models.company.Person)10 Iterator (java.util.Iterator)2 Employee (org.jpox.samples.models.company.Employee)2 MapHolder (org.jpox.samples.one_many.map.MapHolder)2 SetHolder (org.jpox.samples.one_many.collection.SetHolder)1 GroupMember (org.jpox.samples.one_many.unidir_2.GroupMember)1 UserGroup (org.jpox.samples.one_many.unidir_2.UserGroup)1