Search in sources :

Example 6 with JDOQuery

use of org.datanucleus.api.jdo.JDOQuery 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)

Example 7 with JDOQuery

use of org.datanucleus.api.jdo.JDOQuery in project tests by datanucleus.

the class JDOQLEvaluatorTest method testFilterStringEndsWith.

/**
 * Test of filter with String.endsWith().
 */
public void testFilterStringEndsWith() {
    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('d')");
        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());
        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 JDOQuery

use of org.datanucleus.api.jdo.JDOQuery in project tests by datanucleus.

the class JDOQLSingleStringParserTest method testImport.

/**
 * Test for the detection of "import" blocks.
 */
public void testImport() {
    // Test of the basic import parsing
    PersistenceManager pm = pmf.getPersistenceManager();
    Query q = pm.newQuery("JDOQL", null);
    AbstractJDOQLQuery query = (AbstractJDOQLQuery) ((JDOQuery) q).getInternalQuery();
    String str = "SELECT FROM org.jpox.samples.MyClass import java.util.*; import java.net.*";
    JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
    parser.parse();
    String queryString = query.toString();
    // Parser will strip out whitespace from the imports
    String expectationString = "SELECT FROM org.jpox.samples.MyClass import java.util.*;import java.net.*";
    assertEquals("Parse of single-string basic import failed", expectationString, queryString);
}
Also used : AbstractJDOQLQuery(org.datanucleus.store.query.AbstractJDOQLQuery) AbstractJDOQLQuery(org.datanucleus.store.query.AbstractJDOQLQuery) JDOQuery(org.datanucleus.api.jdo.JDOQuery) Query(javax.jdo.Query) JDOQLSingleStringParser(org.datanucleus.query.JDOQLSingleStringParser) PersistenceManager(javax.jdo.PersistenceManager)

Example 9 with JDOQuery

use of org.datanucleus.api.jdo.JDOQuery in project tests by datanucleus.

the class JDOQLSingleStringParserTest method testLiteralWithKeyword.

/**
 * Test for the parse of a literal which includes a keyword.
 */
public void testLiteralWithKeyword() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Query q = pm.newQuery("JDOQL", null);
    AbstractJDOQLQuery query = (AbstractJDOQLQuery) ((JDOQuery) q).getInternalQuery();
    String str = "SELECT FROM org.jpox.samples.MyClass WHERE field1 == 'The book from which I took inspiration'";
    JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
    try {
        parser.parse();
    } catch (NucleusUserException e) {
        LOG.error("Exception in test", e);
        fail("Exception in parse : " + e.getMessage());
    }
}
Also used : AbstractJDOQLQuery(org.datanucleus.store.query.AbstractJDOQLQuery) AbstractJDOQLQuery(org.datanucleus.store.query.AbstractJDOQLQuery) JDOQuery(org.datanucleus.api.jdo.JDOQuery) Query(javax.jdo.Query) JDOQLSingleStringParser(org.datanucleus.query.JDOQLSingleStringParser) PersistenceManager(javax.jdo.PersistenceManager) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Example 10 with JDOQuery

use of org.datanucleus.api.jdo.JDOQuery 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)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)21 JDOQuery (org.datanucleus.api.jdo.JDOQuery)21 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)14 List (java.util.List)14 Transaction (javax.jdo.Transaction)14 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)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 Query (javax.jdo.Query)7 JDOQLSingleStringParser (org.datanucleus.query.JDOQLSingleStringParser)7 AbstractJDOQLQuery (org.datanucleus.store.query.AbstractJDOQLQuery)7 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)6 Iterator (java.util.Iterator)2 SubqueryDefinition (org.datanucleus.store.query.Query.SubqueryDefinition)2