Search in sources :

Example 11 with QueryResponse

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

the class DataContextEJBQLDeleteIT method testDeleteNoIdVar.

@Test
public void testDeleteNoIdVar() throws Exception {
    createPaintingsDataSet();
    String ejbql = "delete from Painting";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    QueryResponse result = context.performGenericQuery(query);
    int[] count = result.firstUpdateCount();
    assertNotNull(count);
    assertEquals(1, count.length);
    assertEquals(2, count[0]);
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) QueryResponse(org.apache.cayenne.QueryResponse) Test(org.junit.Test)

Example 12 with QueryResponse

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

the class DataContextEJBQLDeleteIT method testDeleteSameEntityQualifier.

@Test
public void testDeleteSameEntityQualifier() throws Exception {
    createPaintingsDataSet();
    String ejbql = "delete from Painting AS p WHERE p.paintingTitle = 'P2'";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    QueryResponse result = context.performGenericQuery(query);
    int[] count = result.firstUpdateCount();
    assertNotNull(count);
    assertEquals(1, count.length);
    assertEquals(1, count[0]);
    ObjectContext freshContext = runtime.newContext();
    assertNotNull(Cayenne.objectForPK(freshContext, Painting.class, 33001));
    assertNull(Cayenne.objectForPK(freshContext, Painting.class, 33002));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) QueryResponse(org.apache.cayenne.QueryResponse) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 13 with QueryResponse

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

the class DataContextEJBQLUpdateCompoundIT method testUpdateNoQualifierToOneCompoundPK.

@Test
public void testUpdateNoQualifierToOneCompoundPK() throws Exception {
    createTwoCompoundPKTwoFK();
    Map<String, String> key1 = new HashMap<>();
    key1.put(CompoundPkTestEntity.KEY1_PK_COLUMN, "b1");
    key1.put(CompoundPkTestEntity.KEY2_PK_COLUMN, "b2");
    CompoundPkTestEntity object = Cayenne.objectForPK(context, CompoundPkTestEntity.class, key1);
    EJBQLQuery check = new EJBQLQuery("select count(e) from CompoundFkTestEntity e WHERE e.toCompoundPk <> :param");
    check.setParameter("param", object);
    Object notUpdated = Cayenne.objectForQuery(context, check);
    assertEquals(new Long(1l), notUpdated);
    String ejbql = "UPDATE CompoundFkTestEntity e SET e.toCompoundPk = :param";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    query.setParameter("param", 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 : CompoundPkTestEntity(org.apache.cayenne.testdo.compound.CompoundPkTestEntity) HashMap(java.util.HashMap) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) QueryResponse(org.apache.cayenne.QueryResponse) Test(org.junit.Test)

Example 14 with QueryResponse

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

the class DataContextEJBQLUpdateIT method testUpdateNoQualifierString.

@Test
public void testUpdateNoQualifierString() throws Exception {
    createThreeArtistsTwoPaintings();
    EJBQLQuery check = new EJBQLQuery("select count(p) from Painting p " + "WHERE p.paintingTitle is NULL or p.paintingTitle <> 'XX'");
    Object notUpdated = Cayenne.objectForQuery(context, check);
    assertEquals(new Long(2l), notUpdated);
    String ejbql = "UPDATE Painting AS p SET p.paintingTitle = 'XX'";
    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 15 with QueryResponse

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

the class DataContextEJBQLUpdateIT method testUpdateNoQualifierMultipleItems.

// This fails until we implement arithmetic exps
// public void testUpdateNoQualifierArithmeticExpression() throws Exception {
// createThreeArtistsTwoPaintings();
// 
// 
// EJBQLQuery check = new EJBQLQuery("select count(p) from Painting p "
// + "WHERE p.paintingTitle is NULL or p.estimatedPrice <= 5000");
// 
// Object notUpdated = Cayenne.objectForQuery(context, check);
// assertEquals(new Long(2l), notUpdated);
// 
// String ejbql = "UPDATE Painting AS p SET p.estimatedPrice = p.estimatedPrice * 2";
// 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);
// }
@Test
public void testUpdateNoQualifierMultipleItems() throws Exception {
    createThreeArtistsTwoPaintings();
    EJBQLQuery check = new EJBQLQuery("select count(p) from Painting p " + "WHERE p.estimatedPrice is NULL or p.estimatedPrice <> 1");
    Object notUpdated = Cayenne.objectForQuery(context, check);
    assertEquals(new Long(2l), notUpdated);
    String ejbql = "UPDATE Painting AS p SET p.paintingTitle = 'XX', p.estimatedPrice = 1";
    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)

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