Search in sources :

Example 6 with JDOQLSingleStringParser

use of org.datanucleus.query.JDOQLSingleStringParser 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)

Example 7 with JDOQLSingleStringParser

use of org.datanucleus.query.JDOQLSingleStringParser in project tests by datanucleus.

the class JDOQLSingleStringParserTest method testSubselectWithMissingCloseParen.

/**
 * Test that we give an appropriate exception when parsing a subselect with a missing close paren.
 */
public void testSubselectWithMissingCloseParen() {
    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();
        // Should preserve spaces in the WHERE
        fail("Expected NucleusException");
    } catch (NucleusUserException e) {
    // Expected since we're missing a close parent
    }
}
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)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)7 Query (javax.jdo.Query)7 JDOQuery (org.datanucleus.api.jdo.JDOQuery)7 JDOQLSingleStringParser (org.datanucleus.query.JDOQLSingleStringParser)7 AbstractJDOQLQuery (org.datanucleus.store.query.AbstractJDOQLQuery)7 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)6 SubqueryDefinition (org.datanucleus.store.query.Query.SubqueryDefinition)2