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());
}
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));
}
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));
}
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));
}
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);
}
Aggregations