Search in sources :

Example 1 with QueryChain

use of org.apache.cayenne.query.QueryChain 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 2 with QueryChain

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

the class DataContextQueryChainIT method testSelectQuery.

@Test
public void testSelectQuery() {
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("X");
    context.commitChanges();
    QueryChain chain = new QueryChain();
    chain.addQuery(new SelectQuery(Artist.class));
    chain.addQuery(new SelectQuery(Artist.class));
    QueryResponse r = context.performGenericQuery(chain);
    // data comes back as datarows
    assertEquals(2, r.size());
    r.reset();
    r.next();
    List<?> l1 = r.currentList();
    r.next();
    List<?> l2 = r.currentList();
    assertTrue(l1.get(0) instanceof DataRow);
    assertTrue(l2.get(0) instanceof DataRow);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) QueryChain(org.apache.cayenne.query.QueryChain) QueryResponse(org.apache.cayenne.QueryResponse) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Aggregations

QueryChain (org.apache.cayenne.query.QueryChain)2 Artist (org.apache.cayenne.testdo.testmap.Artist)2 Test (org.junit.Test)2 DataRow (org.apache.cayenne.DataRow)1 QueryResponse (org.apache.cayenne.QueryResponse)1 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)1 SQLTemplate (org.apache.cayenne.query.SQLTemplate)1 SelectQuery (org.apache.cayenne.query.SelectQuery)1