use of org.datanucleus.api.jpa.JPAQuery 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
}
}
use of org.datanucleus.api.jpa.JPAQuery 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());
}
}
use of org.datanucleus.api.jpa.JPAQuery 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());
}
}
use of org.datanucleus.api.jpa.JPAQuery 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());
}
}
use of org.datanucleus.api.jpa.JPAQuery 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());
}
}
Aggregations