Search in sources :

Example 21 with EJBQLQuery

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]);
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Test(org.junit.Test)

Example 22 with EJBQLQuery

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);
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 23 with EJBQLQuery

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));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 24 with EJBQLQuery

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);
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Test(org.junit.Test)

Example 25 with EJBQLQuery

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);
    }
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Aggregations

EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)160 Test (org.junit.Test)158 Artist (org.apache.cayenne.testdo.testmap.Artist)39 HashSet (java.util.HashSet)35 Painting (org.apache.cayenne.testdo.testmap.Painting)15 QueryResponse (org.apache.cayenne.QueryResponse)12 BigDecimal (java.math.BigDecimal)10 Persistent (org.apache.cayenne.Persistent)10 List (java.util.List)9 ArrayList (java.util.ArrayList)7 FlattenedTest1 (org.apache.cayenne.testdo.relationships_flattened.FlattenedTest1)6 CompoundPainting (org.apache.cayenne.testdo.testmap.CompoundPainting)5 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)5 HashMap (java.util.HashMap)4 Iterator (java.util.Iterator)4 ValueHolder (org.apache.cayenne.ValueHolder)4 CompoundPkTestEntity (org.apache.cayenne.testdo.compound.CompoundPkTestEntity)4 Gallery (org.apache.cayenne.testdo.testmap.Gallery)4 Calendar (java.util.Calendar)3 EJBQLCompiledExpression (org.apache.cayenne.ejbql.EJBQLCompiledExpression)3