Search in sources :

Example 16 with QueryResponse

use of org.apache.cayenne.QueryResponse in project cayenne by apache.

the class DataContextEJBQLUpdateIT method testUpdateNoQualifierNull.

@Test
public void testUpdateNoQualifierNull() throws Exception {
    createThreeArtistsTwoPaintings();
    EJBQLQuery check = new EJBQLQuery("select count(p) from Painting p " + "WHERE p.estimatedPrice is not null");
    Object notUpdated = Cayenne.objectForQuery(context, check);
    assertEquals(new Long(2l), notUpdated);
    String ejbql = "UPDATE Painting AS p SET p.estimatedPrice = NULL";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    QueryResponse result = context.performGenericQuery(query);
    int[] count = result.firstUpdateCount();
    assertNotNull(count);
    assertEquals(1, count.length);
    assertEquals(2, count[0]);
    notUpdated = Cayenne.objectForQuery(context, check);
    assertEquals(new Long(0l), notUpdated);
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) QueryResponse(org.apache.cayenne.QueryResponse) Test(org.junit.Test)

Example 17 with QueryResponse

use of org.apache.cayenne.QueryResponse in project cayenne by apache.

the class DataContextEJBQLUpdateIT method testUpdateNoQualifierToOne.

@Test
public void testUpdateNoQualifierToOne() throws Exception {
    createThreeArtistsTwoPaintings();
    Artist object = Cayenne.objectForPK(context, Artist.class, 33003);
    EJBQLQuery check = new EJBQLQuery("select count(p) from Painting p " + "WHERE p.toArtist <> :artist");
    check.setParameter("artist", object);
    Object notUpdated = Cayenne.objectForQuery(context, check);
    assertEquals(new Long(2l), notUpdated);
    String ejbql = "UPDATE Painting AS p SET p.toArtist = :artist";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    query.setParameter("artist", object);
    QueryResponse result = context.performGenericQuery(query);
    int[] count = result.firstUpdateCount();
    assertNotNull(count);
    assertEquals(1, count.length);
    assertEquals(2, count[0]);
    notUpdated = Cayenne.objectForQuery(context, check);
    assertEquals(new Long(0l), notUpdated);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) QueryResponse(org.apache.cayenne.QueryResponse) Test(org.junit.Test)

Example 18 with QueryResponse

use of org.apache.cayenne.QueryResponse 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)

Example 19 with QueryResponse

use of org.apache.cayenne.QueryResponse in project cayenne by apache.

the class DataDomainFiltersIT method testOnQuery_FilterOrdering.

@Test
public void testOnQuery_FilterOrdering() {
    DataDomain domain = runtime.getDataDomain();
    final List<String> results = new ArrayList<String>();
    DataChannelFilter f1 = new MockDataChannelFilter() {

        @Override
        public QueryResponse onQuery(ObjectContext originatingContext, Query query, DataChannelFilterChain filterChain) {
            results.add("f1start");
            QueryResponse response = filterChain.onQuery(originatingContext, query);
            results.add("f1end");
            return response;
        }
    };
    DataChannelFilter f2 = new MockDataChannelFilter() {

        @Override
        public QueryResponse onQuery(ObjectContext originatingContext, Query query, DataChannelFilterChain filterChain) {
            results.add("f2start");
            QueryResponse response = filterChain.onQuery(originatingContext, query);
            results.add("f2end");
            return response;
        }
    };
    domain.filters.add(f1);
    domain.filters.add(f2);
    SelectQuery query = new SelectQuery(Artist.class);
    QueryResponse response = domain.onQuery(context, query);
    assertNotNull(response);
    assertEquals(4, results.size());
    assertEquals("f2start", results.get(0));
    assertEquals("f1start", results.get(1));
    assertEquals("f1end", results.get(2));
    assertEquals("f2end", results.get(3));
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) MockDataChannelFilter(org.apache.cayenne.MockDataChannelFilter) DataChannelFilter(org.apache.cayenne.DataChannelFilter) SelectQuery(org.apache.cayenne.query.SelectQuery) Query(org.apache.cayenne.query.Query) QueryResponse(org.apache.cayenne.QueryResponse) ArrayList(java.util.ArrayList) MockDataChannelFilter(org.apache.cayenne.MockDataChannelFilter) DataChannelFilterChain(org.apache.cayenne.DataChannelFilterChain) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 20 with QueryResponse

use of org.apache.cayenne.QueryResponse in project cayenne by apache.

the class MappedExec method execute.

public QueryResult execute(ObjectContext context) {
    // TODO: switch ObjectContext to QueryResult instead of QueryResponse
    // and create its own 'exec' method
    QueryResponse response = context.performGenericQuery(this);
    QueryResultBuilder builder = QueryResultBuilder.builder(response.size());
    for (response.reset(); response.next(); ) {
        if (response.isList()) {
            builder.addSelectResult(response.currentList());
        } else {
            builder.addBatchUpdateResult(response.currentUpdateCount());
        }
    }
    return builder.build();
}
Also used : QueryResultBuilder(org.apache.cayenne.util.QueryResultBuilder) QueryResponse(org.apache.cayenne.QueryResponse)

Aggregations

QueryResponse (org.apache.cayenne.QueryResponse)31 Test (org.junit.Test)24 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)12 SelectQuery (org.apache.cayenne.query.SelectQuery)9 ObjectId (org.apache.cayenne.ObjectId)7 Query (org.apache.cayenne.query.Query)6 DataRow (org.apache.cayenne.DataRow)5 ObjectContext (org.apache.cayenne.ObjectContext)5 EntityResolver (org.apache.cayenne.map.EntityResolver)5 List (java.util.List)3 CayenneContext (org.apache.cayenne.CayenneContext)3 MockPersistentObject (org.apache.cayenne.MockPersistentObject)3 MockEventManager (org.apache.cayenne.event.MockEventManager)3 DataMap (org.apache.cayenne.map.DataMap)3 ObjEntity (org.apache.cayenne.map.ObjEntity)3 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)3 HashSet (java.util.HashSet)2 DataChannelFilter (org.apache.cayenne.DataChannelFilter)2 DataChannelFilterChain (org.apache.cayenne.DataChannelFilterChain)2 MockDataChannelFilter (org.apache.cayenne.MockDataChannelFilter)2