Search in sources :

Example 1 with SubqueryDefinition

use of org.datanucleus.store.query.Query.SubqueryDefinition in project tests by datanucleus.

the class JPQLSingleStringParserTest method testSubqueryAnd.

/**
 * Test for the parse of a subquery and conversion into the correct components.
 */
public void testSubqueryAnd() {
    EntityManager em = emf.createEntityManager();
    String str = "SELECT T FROM org.jpox.samples.MyClass T WHERE " + "T.field1 < (SELECT AVG(S.price) FROM org.jpox.samples.MyClass S)" + " AND T.field2 == 'Some String'";
    Query q = em.createQuery(str);
    AbstractJPQLQuery query = (AbstractJPQLQuery) ((JPAQuery) q).getInternalQuery();
    JPQLSingleStringParser parser = new JPQLSingleStringParser(query, str);
    try {
        parser.parse();
        assertEquals("Filter is incorrect", "T.field1 < DN_SUBQUERY_1 AND T.field2 == 'Some String'", query.getFilter());
        SubqueryDefinition subqDef = query.getSubqueryForVariable("DN_SUBQUERY_1");
        assertNotNull("No subquery defined", subqDef);
        org.datanucleus.store.query.Query subq = subqDef.getQuery();
        assertEquals("Subquery result is incorrect", "AVG(S.price)", subq.getResult());
        assertNull("Subquery filter is not null", subq.getFilter());
    } catch (NucleusUserException e) {
        fail("Exception thrown in parse " + e.getMessage());
    }
}
Also used : AbstractJPQLQuery(org.datanucleus.store.query.AbstractJPQLQuery) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) AbstractJPQLQuery(org.datanucleus.store.query.AbstractJPQLQuery) JPAQuery(org.datanucleus.api.jpa.JPAQuery) SubqueryDefinition(org.datanucleus.store.query.Query.SubqueryDefinition) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) JPQLSingleStringParser(org.datanucleus.query.JPQLSingleStringParser)

Example 2 with SubqueryDefinition

use of org.datanucleus.store.query.Query.SubqueryDefinition in project tests by datanucleus.

the class JPQLSingleStringParserTest method testSubqueryIn.

/**
 * Test for the parse of a subquery and conversion into the correct components.
 */
public void testSubqueryIn() {
    EntityManager em = emf.createEntityManager();
    String str = "SELECT T FROM org.jpox.samples.MyClass T WHERE T.field1 IN (SELECT S FROM org.jpox.samples.OtherClass S)";
    Query q = em.createQuery(str);
    AbstractJPQLQuery query = (AbstractJPQLQuery) ((JPAQuery) q).getInternalQuery();
    JPQLSingleStringParser parser = new JPQLSingleStringParser(query, str);
    try {
        parser.parse();
        assertEquals("Filter is incorrect", "T.field1 IN DN_SUBQUERY_1", query.getFilter());
        SubqueryDefinition subqDef = query.getSubqueryForVariable("DN_SUBQUERY_1");
        assertNotNull("No subquery defined", subqDef);
        org.datanucleus.store.query.Query subq = subqDef.getQuery();
        assertNull("Subquery filter is not null", subq.getFilter());
    } catch (NucleusUserException e) {
        fail("Exception thrown in parse " + e.getMessage());
    }
}
Also used : AbstractJPQLQuery(org.datanucleus.store.query.AbstractJPQLQuery) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) AbstractJPQLQuery(org.datanucleus.store.query.AbstractJPQLQuery) JPAQuery(org.datanucleus.api.jpa.JPAQuery) SubqueryDefinition(org.datanucleus.store.query.Query.SubqueryDefinition) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) JPQLSingleStringParser(org.datanucleus.query.JPQLSingleStringParser)

Example 3 with SubqueryDefinition

use of org.datanucleus.store.query.Query.SubqueryDefinition in project tests by datanucleus.

the class JPQLSingleStringParserTest method testSubqueryIn2.

/**
 * Test for the parse of a subquery and conversion into the correct components.
 */
public void testSubqueryIn2() {
    EntityManager em = emf.createEntityManager();
    String str = "SELECT T FROM org.jpox.samples.MyClass T WHERE " + "T.field1 IN (SELECT S FROM org.jpox.samples.OtherClass S) AND " + "T.field2 IN (SELECT R FROM org.jpox.samples.ExtraClass R)";
    Query q = em.createQuery(str);
    AbstractJPQLQuery query = (AbstractJPQLQuery) ((JPAQuery) q).getInternalQuery();
    JPQLSingleStringParser parser = new JPQLSingleStringParser(query, str);
    try {
        parser.parse();
        assertEquals("Filter is incorrect", "T.field1 IN DN_SUBQUERY_1 AND T.field2 IN DN_SUBQUERY_2", query.getFilter());
        SubqueryDefinition subqDef = query.getSubqueryForVariable("DN_SUBQUERY_1");
        assertNotNull("No subquery defined", subqDef);
        org.datanucleus.store.query.Query subq = subqDef.getQuery();
        assertNull("Subquery1 filter is not null", subq.getFilter());
        SubqueryDefinition subqDef2 = query.getSubqueryForVariable("DN_SUBQUERY_2");
        assertNotNull("No subquery defined", subqDef2);
        org.datanucleus.store.query.Query subq2 = subqDef2.getQuery();
        assertNull("Subquery2 filter is not null", subq2.getFilter());
    } catch (NucleusUserException e) {
        fail("Exception thrown in parse " + e.getMessage());
    }
}
Also used : AbstractJPQLQuery(org.datanucleus.store.query.AbstractJPQLQuery) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) AbstractJPQLQuery(org.datanucleus.store.query.AbstractJPQLQuery) JPAQuery(org.datanucleus.api.jpa.JPAQuery) SubqueryDefinition(org.datanucleus.store.query.Query.SubqueryDefinition) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) JPQLSingleStringParser(org.datanucleus.query.JPQLSingleStringParser)

Example 4 with SubqueryDefinition

use of org.datanucleus.store.query.Query.SubqueryDefinition in project tests by datanucleus.

the class JPQLSingleStringParserTest method testSubquery.

/**
 * Test for the parse of a subquery and conversion into the correct components.
 */
public void testSubquery() {
    EntityManager em = emf.createEntityManager();
    String str = "SELECT T FROM org.jpox.samples.MyClass T WHERE T.field1 < (SELECT AVG(S.price) FROM org.jpox.samples.MyClass S)";
    Query q = em.createQuery(str);
    AbstractJPQLQuery query = (AbstractJPQLQuery) ((JPAQuery) q).getInternalQuery();
    JPQLSingleStringParser parser = new JPQLSingleStringParser(query, str);
    try {
        parser.parse();
        assertEquals("Filter is incorrect", "T.field1 < DN_SUBQUERY_1", query.getFilter());
        SubqueryDefinition subqDef = query.getSubqueryForVariable("DN_SUBQUERY_1");
        assertNotNull("No subquery defined", subqDef);
        org.datanucleus.store.query.Query subq = subqDef.getQuery();
        assertEquals("Subquery result is incorrect", "AVG(S.price)", subq.getResult());
        assertNull("Subquery filter is not null", subq.getFilter());
    } catch (NucleusUserException e) {
        fail("Exception thrown in parse " + e.getMessage());
    }
}
Also used : AbstractJPQLQuery(org.datanucleus.store.query.AbstractJPQLQuery) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) AbstractJPQLQuery(org.datanucleus.store.query.AbstractJPQLQuery) JPAQuery(org.datanucleus.api.jpa.JPAQuery) SubqueryDefinition(org.datanucleus.store.query.Query.SubqueryDefinition) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) JPQLSingleStringParser(org.datanucleus.query.JPQLSingleStringParser)

Example 5 with SubqueryDefinition

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

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