Search in sources :

Example 61 with EJBQLQuery

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

the class DataContextEJBQLFlattenedRelationshipsIT method testGroupByFlattenedRelationship.

@Test
public void testGroupByFlattenedRelationship() throws Exception {
    createFt123();
    String ejbql = "SELECT COUNT(ft3), ft3.toFT1 FROM FlattenedTest3 ft3  GROUP BY ft3.toFT1 ";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    List<?> objects = context.performQuery(query);
    assertEquals(2, objects.size());
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Test(org.junit.Test)

Example 62 with EJBQLQuery

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

the class DataContextEJBQLFunctionalExpressionsIT method testLOCATE.

@Test
public void testLOCATE() {
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("___A___");
    Artist a2 = context.newObject(Artist.class);
    a2.setArtistName("_A_____");
    context.commitChanges();
    EJBQLQuery query = new EJBQLQuery("SELECT a FROM Artist a WHERE LOCATE('A', a.artistName) = 2");
    List<?> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    assertTrue(objects.contains(a2));
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Test(org.junit.Test)

Example 63 with EJBQLQuery

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

the class DataContextEJBQLFunctionalExpressionsIT method testSUBSTRING.

@Test
public void testSUBSTRING() {
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("12345678");
    Artist a2 = context.newObject(Artist.class);
    a2.setArtistName("abcdefg");
    context.commitChanges();
    EJBQLQuery query = new EJBQLQuery("SELECT a FROM Artist a WHERE SUBSTRING(a.artistName, 2, 3) = 'bcd'");
    List<?> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    assertTrue(objects.contains(a2));
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Test(org.junit.Test)

Example 64 with EJBQLQuery

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

the class DataContextEJBQLFunctionalExpressionsIT method testTRIM.

@Test
public void testTRIM() {
    // insert via a SQL template to prevent adapter trimming and such...
    QueryChain inserts = new QueryChain();
    inserts.addQuery(new SQLTemplate(Artist.class, "INSERT INTO ARTIST (ARTIST_ID,ARTIST_NAME) VALUES(1, '  A')"));
    inserts.addQuery(new SQLTemplate(Artist.class, "INSERT INTO ARTIST (ARTIST_ID,ARTIST_NAME) VALUES(2, 'A  ')"));
    context.performGenericQuery(inserts);
    Artist a1 = Cayenne.objectForPK(context, Artist.class, 1);
    Artist a2 = Cayenne.objectForPK(context, Artist.class, 2);
    EJBQLQuery query = new EJBQLQuery("SELECT a FROM Artist a WHERE TRIM(a.artistName) = 'A'");
    List<?> objects = context.performQuery(query);
    assertEquals(2, objects.size());
    assertTrue(objects.contains(a1));
    assertTrue(objects.contains(a2));
    query = new EJBQLQuery("SELECT a FROM Artist a WHERE TRIM(LEADING FROM a.artistName) = 'A'");
    objects = context.performQuery(query);
    // this is fuzzy cause some DB trim trailing data by default
    assertTrue(objects.size() == 1 || objects.size() == 2);
    assertTrue(objects.contains(a1));
    query = new EJBQLQuery("SELECT a FROM Artist a WHERE TRIM(TRAILING FROM a.artistName) = 'A'");
    objects = context.performQuery(query);
    assertEquals(1, objects.size());
    assertTrue(objects.contains(a2));
    query = new EJBQLQuery("SELECT a FROM Artist a WHERE TRIM(BOTH FROM a.artistName) = 'A'");
    objects = context.performQuery(query);
    assertEquals(2, objects.size());
    assertTrue(objects.contains(a1));
    assertTrue(objects.contains(a2));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Artist(org.apache.cayenne.testdo.testmap.Artist) QueryChain(org.apache.cayenne.query.QueryChain) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Test(org.junit.Test)

Example 65 with EJBQLQuery

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

the class DataContextEJBQLGroupByHavingIT method testGroupByRelatedEntity.

@Test
public void testGroupByRelatedEntity() throws Exception {
    createFourArtistsAndTwoPaintings();
    String ejbql = "SELECT COUNT(p), a, a.artistName " + "FROM Painting p INNER JOIN p.toArtist a GROUP BY a, a.artistName " + "ORDER BY a.artistName";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    List<?> data = context.performQuery(query);
    assertEquals(2, data.size());
    assertTrue(data.get(0) instanceof Object[]);
    Object[] row0 = (Object[]) data.get(0);
    assertEquals(3, row0.length);
    assertEquals(new Long(1), row0[0]);
    assertEquals("AA1", row0[2]);
    assertTrue(row0[1] instanceof Artist);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) 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