Search in sources :

Example 6 with SubqueryDefinition

use of org.datanucleus.store.query.Query.SubqueryDefinition 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

NucleusUserException (org.datanucleus.exceptions.NucleusUserException)6 SubqueryDefinition (org.datanucleus.store.query.Query.SubqueryDefinition)6 EntityManager (javax.persistence.EntityManager)4 Query (javax.persistence.Query)4 JPAQuery (org.datanucleus.api.jpa.JPAQuery)4 JPQLSingleStringParser (org.datanucleus.query.JPQLSingleStringParser)4 AbstractJPQLQuery (org.datanucleus.store.query.AbstractJPQLQuery)4 PersistenceManager (javax.jdo.PersistenceManager)2 Query (javax.jdo.Query)2 JDOQuery (org.datanucleus.api.jdo.JDOQuery)2 JDOQLSingleStringParser (org.datanucleus.query.JDOQLSingleStringParser)2 AbstractJDOQLQuery (org.datanucleus.store.query.AbstractJDOQLQuery)2