Search in sources :

Example 66 with EJBQLQuery

use of org.apache.cayenne.query.EJBQLQuery in project cayenne by apache.

the class DataContextEJBQLGroupByHavingIT method testGroupByHavingOnColumn.

@Test
public void testGroupByHavingOnColumn() throws Exception {
    createFivePaintings();
    String ejbql = "SELECT p.estimatedPrice, count(p) FROM Painting p" + " GROUP BY p.estimatedPrice" + " HAVING p.estimatedPrice > 1";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    List<?> data = context.performQuery(query);
    assertEquals(1, data.size());
    assertTrue(data.get(0) instanceof Object[]);
    Object[] row0 = (Object[]) data.get(0);
    assertEquals(2d, ((BigDecimal) row0[0]).doubleValue(), 0.00001d);
    assertEquals(new Long(2), row0[1]);
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Test(org.junit.Test)

Example 67 with EJBQLQuery

use of org.apache.cayenne.query.EJBQLQuery in project cayenne by apache.

the class DataContextEJBQLGroupByHavingIT method testGroupByHavingOnAggregateMultipleConditions.

@Test
public void testGroupByHavingOnAggregateMultipleConditions() throws Exception {
    createFivePaintings();
    String ejbql = "SELECT p.estimatedPrice, count(p) FROM Painting p" + " GROUP BY p.estimatedPrice" + " HAVING count(p) > 2 AND p.estimatedPrice < 10";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    List<?> data = context.performQuery(query);
    assertEquals(1, data.size());
    assertTrue(data.get(0) instanceof Object[]);
    Object[] row0 = (Object[]) data.get(0);
    assertEquals(1d, ((BigDecimal) row0[0]).doubleValue(), 0.00001d);
    assertEquals(new Long(3l), row0[1]);
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Test(org.junit.Test)

Example 68 with EJBQLQuery

use of org.apache.cayenne.query.EJBQLQuery in project cayenne by apache.

the class DataContextEJBQLGroupByHavingIT method testGroupByJoinedEntities.

@Test
public void testGroupByJoinedEntities() throws Exception {
    createArtistsPaintingGalleries();
    EJBQLQuery query = new EJBQLQuery("SELECT COUNT(p), p.toArtist, p.toGallery FROM Painting p " + "GROUP BY p.toGallery, p.toArtist ");
    List<Object[]> data = context.performQuery(query);
    assertNotNull(data);
    assertEquals(2, data.size());
    HashSet<List<?>> expectedResults = new HashSet<List<?>>();
    expectedResults.add(Arrays.asList(1L, "AA2", "gallery1"));
    expectedResults.add(Arrays.asList(1L, "AA1", "gallery2"));
    for (Object[] row : data) {
        assertFalse(expectedResults.add(Arrays.asList(row[0], row[1] == null ? null : ((Artist) row[1]).getArtistName(), row[2] == null ? null : ((Gallery) row[2]).getGalleryName())));
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Gallery(org.apache.cayenne.testdo.testmap.Gallery) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 69 with EJBQLQuery

use of org.apache.cayenne.query.EJBQLQuery in project cayenne by apache.

the class DataContextEJBQLGroupByHavingIT method testGroupByJoinedEntityInCount.

@Test
public void testGroupByJoinedEntityInCount() throws Exception {
    createArtistsPaintingGalleries();
    EJBQLQuery query = new EJBQLQuery("SELECT COUNT(p.toArtist), p.paintingTitle FROM Painting p " + "GROUP BY p.paintingTitle " + "HAVING p.paintingTitle LIKE 'P1%'");
    List<Object[]> data = context.performQuery(query);
    assertNotNull(data);
    assertEquals(3, data.size());
    HashSet<List<?>> expectedResults = new HashSet<List<?>>();
    expectedResults.add(Arrays.asList(1L, "P1"));
    expectedResults.add(Arrays.asList(1L, "P111"));
    expectedResults.add(Arrays.asList(1L, "P112"));
    for (Object[] row : data) {
        assertFalse(expectedResults.add(Arrays.asList(row[0], row[1])));
    }
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 70 with EJBQLQuery

use of org.apache.cayenne.query.EJBQLQuery in project cayenne by apache.

the class DataContextEJBQLInheritanceIT method testSelect.

@Test
public void testSelect() throws Exception {
    EJBQLQuery superclass = new EJBQLQuery("select p from AbstractPerson p ORDER BY p.name");
    List<?> superclassResult = context.performQuery(superclass);
    assertEquals(5, superclassResult.size());
    assertEquals(Employee.class.getName(), superclassResult.get(0).getClass().getName());
    assertEquals(Employee.class.getName(), superclassResult.get(1).getClass().getName());
    assertEquals(Manager.class.getName(), superclassResult.get(2).getClass().getName());
    assertEquals(Manager.class.getName(), superclassResult.get(3).getClass().getName());
    assertEquals(CustomerRepresentative.class.getName(), superclassResult.get(4).getClass().getName());
    EJBQLQuery subclass = new EJBQLQuery("select e from Employee e ORDER BY e.name");
    List<?> subclassResult = context.performQuery(subclass);
    assertEquals(4, subclassResult.size());
    assertEquals(Employee.class.getName(), subclassResult.get(0).getClass().getName());
    assertEquals(Employee.class.getName(), subclassResult.get(1).getClass().getName());
    assertEquals(Manager.class.getName(), subclassResult.get(2).getClass().getName());
    assertEquals(Manager.class.getName(), subclassResult.get(3).getClass().getName());
}
Also used : Employee(org.apache.cayenne.testdo.inheritance_people.Employee) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) CustomerRepresentative(org.apache.cayenne.testdo.inheritance_people.CustomerRepresentative) Manager(org.apache.cayenne.testdo.inheritance_people.Manager) 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