use of org.apache.cayenne.query.EJBQLQuery in project cayenne by apache.
the class DataContextEJBQLQueryIT method testSelectAggregateNull.
@Test
public void testSelectAggregateNull() throws Exception {
if (!accessStackAdapter.supportNullRowForAggregateFunctions()) {
return;
}
String ejbql = "select count(p), max(p.estimatedPrice), sum(p.estimatedPrice) " + "from Painting p WHERE p.paintingTitle = 'X'";
EJBQLQuery query = new EJBQLQuery(ejbql);
List<?> data = context.performQuery(query);
assertEquals(1, data.size());
assertTrue(data.get(0) instanceof Object[]);
Object[] aggregates = (Object[]) data.get(0);
assertEquals(new Long(0), aggregates[0]);
assertEquals(null, aggregates[1]);
assertEquals(null, aggregates[2]);
}
use of org.apache.cayenne.query.EJBQLQuery in project cayenne by apache.
the class DataContextEJBQLQueryIT method testSelectFromWhereLess.
@Test
public void testSelectFromWhereLess() throws Exception {
createFourArtistsTwoPaintings();
String ejbql = "select P from Painting P WHERE p.estimatedPrice < 5000";
EJBQLQuery query = new EJBQLQuery(ejbql);
List<?> ps = context.performQuery(query);
assertEquals(1, ps.size());
Painting p = (Painting) ps.get(0);
assertEquals("P1", p.getPaintingTitle());
assertEquals(3000d, p.getEstimatedPrice().doubleValue(), 0.00001);
}
use of org.apache.cayenne.query.EJBQLQuery in project cayenne by apache.
the class DataContextEJBQLQueryIT method testSelectFromWhereMatchRelationshipAndScalar.
@Test
public void testSelectFromWhereMatchRelationshipAndScalar() throws Exception {
createFourArtistsTwoPaintings();
String ejbql = "select P from Painting P WHERE p.toArtist = 33002";
EJBQLQuery query = new EJBQLQuery(ejbql);
List<?> ps = context.performQuery(query);
assertEquals(1, ps.size());
Painting p = (Painting) ps.get(0);
assertEquals(33002, Cayenne.intPKForObject(p));
}
use of org.apache.cayenne.query.EJBQLQuery in project cayenne by apache.
the class DataContextEJBQLQueryIT method testSelectAggregate.
@Test
public void testSelectAggregate() throws Exception {
createFourArtistsTwoPaintings();
String ejbql = "select count(p), count(distinct p.estimatedPrice), max(p.estimatedPrice), sum(p.estimatedPrice) from Painting p";
EJBQLQuery query = new EJBQLQuery(ejbql);
List<?> data = context.performQuery(query);
assertEquals(1, data.size());
assertTrue(data.get(0) instanceof Object[]);
Object[] aggregates = (Object[]) data.get(0);
assertEquals(new Long(2), aggregates[0]);
assertEquals(new Long(2), aggregates[1]);
assertEquals(5000d, ((BigDecimal) aggregates[2]).doubleValue(), 0.00001);
assertEquals(8000d, ((BigDecimal) aggregates[3]).doubleValue(), 0.00001);
}
use of org.apache.cayenne.query.EJBQLQuery in project cayenne by apache.
the class DataContextEJBQLQueryIT method testSelectAggregatePostLoadCallback.
/**
* CAY-899: Checks that aggregate results do not cause callbacks execution.
*/
@Test
public void testSelectAggregatePostLoadCallback() throws Exception {
createFourArtistsTwoPaintings();
LifecycleCallbackRegistry existingCallbacks = context.getEntityResolver().getCallbackRegistry();
LifecycleCallbackRegistry testCallbacks = new LifecycleCallbackRegistry(context.getEntityResolver());
DataContextEJBQLQueryCallback listener = new DataContextEJBQLQueryCallback();
testCallbacks.addDefaultListener(LifecycleEvent.POST_LOAD, listener, "postLoad");
context.getEntityResolver().setCallbackRegistry(testCallbacks);
try {
String ejbql = "select count(p), count(distinct p.estimatedPrice), max(p.estimatedPrice), sum(p.estimatedPrice) from Painting p";
EJBQLQuery query = new EJBQLQuery(ejbql);
List<?> data = context.performQuery(query);
assertFalse(listener.postLoad);
assertEquals(1, data.size());
assertTrue(data.get(0) instanceof Object[]);
} finally {
context.getEntityResolver().setCallbackRegistry(existingCallbacks);
}
}
Aggregations