Search in sources :

Example 1 with AbstractJPQLQuery

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

use of org.datanucleus.store.query.AbstractJPQLQuery in project tests by datanucleus.

the class JPQLSingleStringParserTest method testInsert.

/**
 * Test for the parse of an INSERT query and conversion into the correct components.
 */
public void testInsert() {
    EntityManager em = emf.createEntityManager();
    String str = "INSERT INTO org.jpox.samples.MyClass (id, name, otherField) SELECT p.id, p.name, p.description FROM org.jpox.samples.Person p WHERE p.id > 3";
    Query q = em.createQuery(str);
    AbstractJPQLQuery query = (AbstractJPQLQuery) ((JPAQuery) q).getInternalQuery();
    JPQLSingleStringParser parser = new JPQLSingleStringParser(query, str);
    try {
        parser.parse();
        assertEquals("Type is wrong", QueryType.BULK_INSERT, query.getType());
        assertEquals("Candidate is wrong", "org.jpox.samples.MyClass", query.getFrom());
        assertEquals("INSERT fields are wrong", "id, name, otherField", query.getInsertFields());
        assertEquals("INSERT select query is wrong", "SELECT p.id, p.name, p.description FROM org.jpox.samples.Person p WHERE p.id > 3", query.getInsertSelectQuery());
    } catch (NucleusUserException nue) {
        fail("Exception thrown in parse " + nue.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) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) JPQLSingleStringParser(org.datanucleus.query.JPQLSingleStringParser)

Example 3 with AbstractJPQLQuery

use of org.datanucleus.store.query.AbstractJPQLQuery in project tests by datanucleus.

the class JPQLSingleStringParserTest method testWhereWithMultipleSpaces.

/**
 * Test for the parse of a WHERE clause which has multiple spaces present.
 */
public void testWhereWithMultipleSpaces() {
    EntityManager em = emf.createEntityManager();
    String str = "SELECT T FROM org.jpox.samples.MyClass T WHERE T.field1 = 'The     value to compare against'";
    Query q = em.createQuery(str);
    AbstractJPQLQuery query = (AbstractJPQLQuery) ((JPAQuery) q).getInternalQuery();
    JPQLSingleStringParser parser = new JPQLSingleStringParser(query, str);
    try {
        parser.parse();
        // Should preserve spaces in the WHERE
        assertEquals("The WHERE clause was not correctly parsed", "T.field1 = 'The     value to compare against'", query.getFilter());
    } catch (NucleusUserException e) {
    // Expected since VARIABLES should be before PARAMETERS
    }
}
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) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) JPQLSingleStringParser(org.datanucleus.query.JPQLSingleStringParser)

Example 4 with AbstractJPQLQuery

use of org.datanucleus.store.query.AbstractJPQLQuery 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 5 with AbstractJPQLQuery

use of org.datanucleus.store.query.AbstractJPQLQuery in project tests by datanucleus.

the class JPQLSingleStringParserTest method testLiteralWithKeyword.

/**
 * Test for the parse of a literal which includes a keyword.
 */
public void testLiteralWithKeyword() {
    EntityManager em = emf.createEntityManager();
    String str = "SELECT T FROM org.jpox.samples.MyClass T WHERE T.field1 = 'The book from which I took inspiration'";
    Query q = em.createQuery(str);
    AbstractJPQLQuery query = (AbstractJPQLQuery) ((JPAQuery) q).getInternalQuery();
    JPQLSingleStringParser parser = new JPQLSingleStringParser(query, str);
    try {
        parser.parse();
    } catch (NucleusUserException e) {
        LOG.error("Exception in test", e);
        fail("Exception 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) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) JPQLSingleStringParser(org.datanucleus.query.JPQLSingleStringParser)

Aggregations

EntityManager (javax.persistence.EntityManager)7 Query (javax.persistence.Query)7 JPAQuery (org.datanucleus.api.jpa.JPAQuery)7 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)7 JPQLSingleStringParser (org.datanucleus.query.JPQLSingleStringParser)7 AbstractJPQLQuery (org.datanucleus.store.query.AbstractJPQLQuery)7 SubqueryDefinition (org.datanucleus.store.query.Query.SubqueryDefinition)4