Search in sources :

Example 66 with SQLTemplate

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

the class DataContextObjectIdQueryIT method testNoRefreshValuesNew.

@Test
public void testNoRefreshValuesNew() {
    Artist a = context.newObject(Artist.class);
    a.setArtistName("X");
    context.commitChanges();
    context.performGenericQuery(new SQLTemplate(Artist.class, "UPDATE ARTIST SET ARTIST_NAME = 'Y'"));
    long id = Cayenne.longPKForObject(a);
    ObjectIdQuery query = new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, id), false, ObjectIdQuery.CACHE);
    Artist a1 = (Artist) Cayenne.objectForQuery(context, query);
    assertEquals("X", a1.getArtistName());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SQLTemplate(org.apache.cayenne.query.SQLTemplate) ObjectId(org.apache.cayenne.ObjectId) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 67 with SQLTemplate

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

the class DataContextPerformQueryAPIIT method testPerfomQueryNonSelecting.

@Test
public void testPerfomQueryNonSelecting() throws Exception {
    Artist a = context.newObject(Artist.class);
    a.setArtistName("aa");
    context.commitChanges();
    SQLTemplate q = new SQLTemplate(Artist.class, "DELETE FROM ARTIST");
    // this way of executing a query makes no sense, but it shouldn't blow
    // either...
    List<?> result = context.performQuery(q);
    assertNotNull(result);
    assertEquals(0, result.size());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SQLTemplate(org.apache.cayenne.query.SQLTemplate) Test(org.junit.Test)

Example 68 with SQLTemplate

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

the class DataContextSQLTemplateCompoundIT method testBindObjectNotEqualCompound.

@Test
public void testBindObjectNotEqualCompound() throws Exception {
    createTwoCompoundPKsAndCompoundFKsDataSet();
    Map<String, String> pk = new HashMap<>();
    pk.put(CompoundPkTestEntity.KEY1_PK_COLUMN, "a1");
    pk.put(CompoundPkTestEntity.KEY2_PK_COLUMN, "a2");
    CompoundPkTestEntity a = Cayenne.objectForPK(context, CompoundPkTestEntity.class, pk);
    String template = "SELECT * FROM COMPOUND_FK_TEST t0" + " WHERE #bindObjectNotEqual($a [ 't0.F_KEY1', 't0.F_KEY2' ] [ 'KEY1', 'KEY2' ] ) ORDER BY PKEY";
    SQLTemplate query = new SQLTemplate(CompoundFkTestEntity.class, template);
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    query.setParams(Collections.singletonMap("a", a));
    List<CompoundFkTestEntity> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    CompoundFkTestEntity p = objects.get(0);
    assertEquals(7, Cayenne.intPKForObject(p));
}
Also used : CompoundPkTestEntity(org.apache.cayenne.testdo.compound.CompoundPkTestEntity) SQLTemplate(org.apache.cayenne.query.SQLTemplate) HashMap(java.util.HashMap) CompoundFkTestEntity(org.apache.cayenne.testdo.compound.CompoundFkTestEntity) Test(org.junit.Test)

Example 69 with SQLTemplate

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

the class DataContextSQLTemplateIT method testBindObjectNotEqualFull.

@Test
public void testBindObjectNotEqualFull() throws Exception {
    createFourArtistsAndThreePaintingsDataSet();
    Artist a = Cayenne.objectForPK(context, Artist.class, 101);
    String template = "SELECT * FROM PAINTING t0" + " WHERE #bindObjectNotEqual($a [ 't0.ARTIST_ID' ] [ 'ARTIST_ID' ] ) ORDER BY PAINTING_ID";
    SQLTemplate query = new SQLTemplate(Painting.class, template);
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    query.setParams(Collections.singletonMap("a", a));
    List<?> objects = context.performQuery(query);
    // null comparison is unpredictable across DB's ... some would return
    // true on null
    // <> value, some - false
    assertTrue(objects.size() == 1 || objects.size() == 2);
    Painting p = (Painting) objects.get(0);
    assertEquals(6, Cayenne.intPKForObject(p));
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SQLTemplate(org.apache.cayenne.query.SQLTemplate) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 70 with SQLTemplate

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

the class DataContextSQLTemplateIT method testBindEqualNull.

@Test
public void testBindEqualNull() throws Exception {
    createFourArtistsAndThreePaintingsDataSet();
    String template = "SELECT * FROM PAINTING t0" + " WHERE t0.ARTIST_ID #bindEqual($id) ORDER BY PAINTING_ID";
    SQLTemplate query = new SQLTemplate(Painting.class, template);
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    query.setParams(Collections.singletonMap("id", null));
    List<Painting> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    Painting p = objects.get(0);
    assertEquals(8, Cayenne.intPKForObject(p));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

SQLTemplate (org.apache.cayenne.query.SQLTemplate)118 Test (org.junit.Test)108 Artist (org.apache.cayenne.testdo.testmap.Artist)27 DataRow (org.apache.cayenne.DataRow)18 HashMap (java.util.HashMap)16 MockOperationObserver (org.apache.cayenne.access.MockOperationObserver)14 Query (org.apache.cayenne.query.Query)14 SelectQuery (org.apache.cayenne.query.SelectQuery)11 Painting (org.apache.cayenne.testdo.testmap.Painting)11 List (java.util.List)8 Connection (java.sql.Connection)7 SQLAction (org.apache.cayenne.query.SQLAction)7 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)6 Map (java.util.Map)5 OperationObserver (org.apache.cayenne.access.OperationObserver)5 DataMap (org.apache.cayenne.map.DataMap)5 AbstractPerson (org.apache.cayenne.testdo.inheritance_people.AbstractPerson)5 PersonNotes (org.apache.cayenne.testdo.inheritance_people.PersonNotes)5 Date (java.sql.Date)4 FrontBaseAdapter (org.apache.cayenne.dba.frontbase.FrontBaseAdapter)4