Search in sources :

Example 16 with JDOQuery

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

Example 17 with JDOQuery

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

the class JDOQLSingleStringParserTest method testWhereWithMultipleSpaces.

/**
 * Test for the parse of a WHERE clause which has multiple spaces present.
 */
public void testWhereWithMultipleSpaces() {
    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     value to compare against'";
    JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
    try {
        parser.parse();
        // Should preserve spaces in the WHERE
        assertEquals("The WHERE clause was not correctly parsed", "field1 == 'The     value to compare against'", query.getFilter());
    } catch (NucleusUserException 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 18 with JDOQuery

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

the class JDOQLSingleStringParserTest method testSubquery.

/**
 * Test for parse and compile of single-string JDOQL subquery case.
 */
public void testSubquery() {
    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 > " + "(SELECT avg(f.field1) FROM org.jpox.samples.MyClass f)";
    JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
    try {
        parser.parse();
        SubqueryDefinition subq1 = query.getSubqueryForVariable("DN_SUBQUERY_1");
        assertNotNull("Subquery #1 should be generated by single-string parser but isnt", subq1);
        assertEquals(subq1.getQuery().toString(), "SELECT avg(f.field1) FROM org.jpox.samples.MyClass f");
        SubqueryDefinition subq2 = query.getSubqueryForVariable("DN_SUBQUERY_2");
        assertNull("Subquery #2 shouldnt be generated by single-string parser but is", subq2);
    } catch (NucleusUserException e) {
        fail("Exception in parser : " + 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) SubqueryDefinition(org.datanucleus.store.query.Query.SubqueryDefinition) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Example 19 with JDOQuery

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

the class JDOQLSingleStringParserTest method testVariableBeforeParameter.

/**
 * Test for the order of variable and parameter definitions.
 */
public void testVariableBeforeParameter() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Query q = pm.newQuery("JDOQL", null);
    AbstractJDOQLQuery query = (AbstractJDOQLQuery) ((JDOQuery) q).getInternalQuery();
    String str = "SELECT FROM org.jpox.samples.MyClass PARAMETERS String p1 VARIABLES int v1";
    JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
    try {
        parser.parse();
        fail("Query using VARIABLES after PARAMETERS should have thrown an exception but passed");
    } catch (NucleusUserException e) {
    // Expected since VARIABLES should be before PARAMETERS
    }
}
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 20 with JDOQuery

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

the class JDOQLSingleStringParserTest method testSubquery2.

/**
 * Test for parse and compile of single-string JDOQL subquery case.
 */
public void testSubquery2() {
    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 > (SELECT avg(f.field1) FROM org.jpox.samples.MyClass f) AND " + "field2 > (SELECT min(s.field1) FROM org.jpox.samples.OtherClass s)";
    JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
    try {
        parser.parse();
        SubqueryDefinition subq1 = query.getSubqueryForVariable("DN_SUBQUERY_1");
        assertNotNull("Subquery #1 should be generated by single-string parser but isnt", subq1);
        assertEquals("SELECT avg(f.field1) FROM org.jpox.samples.MyClass f", subq1.getQuery().toString());
        SubqueryDefinition subq2 = query.getSubqueryForVariable("DN_SUBQUERY_2");
        assertNotNull("Subquery #2 should be generated by single-string parser but isnt", subq2);
        assertEquals("SELECT min(s.field1) FROM org.jpox.samples.OtherClass s", subq2.getQuery().toString());
    } catch (NucleusUserException e) {
        fail("Exception in parser : " + 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) SubqueryDefinition(org.datanucleus.store.query.Query.SubqueryDefinition) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

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