Search in sources :

Example 76 with SQLTemplate

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

the class DataContextSQLTemplateIT method testFetchOffsetFetchLimit.

@Test
public void testFetchOffsetFetchLimit() throws Exception {
    createFourArtists();
    String template = "SELECT * FROM ARTIST ORDER BY ARTIST_ID";
    SQLTemplate query = sqlTemplateCustomizer.createSQLTemplate(Artist.class, template);
    query.setFetchOffset(1);
    query.setFetchLimit(2);
    List<?> objects = context.performQuery(query);
    assertEquals(2, objects.size());
    assertTrue(objects.get(0) instanceof Artist);
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Artist(org.apache.cayenne.testdo.testmap.Artist) Test(org.junit.Test)

Example 77 with SQLTemplate

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

the class DataContextSQLTemplateIT method testRootless_DefaultDataNode.

@Test
public void testRootless_DefaultDataNode() throws Exception {
    createFourArtists();
    SQLTemplate query = new SQLTemplate("SELECT * FROM ARTIST", true);
    assertEquals(4, context.performQuery(query).size());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Test(org.junit.Test)

Example 78 with SQLTemplate

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

the class DataContextSQLTemplateIT method testBindObjectNotEqualShort.

@Test
public void testBindObjectNotEqualShort() throws Exception {
    createFourArtistsAndThreePaintingsDataSet();
    Artist a = Cayenne.objectForPK(context, Artist.class, 101);
    String template = "SELECT * FROM PAINTING " + "WHERE #bindObjectNotEqual($a) 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 79 with SQLTemplate

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

the class DataContextSQLTemplateIT method testBindObjectEqualFull.

@Test
public void testBindObjectEqualFull() throws Exception {
    createFourArtistsAndThreePaintingsDataSet();
    Artist a = Cayenne.objectForPK(context, Artist.class, 101);
    String template = "SELECT * FROM PAINTING t0" + " WHERE #bindObjectEqual($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);
    assertEquals(1, objects.size());
    Painting p = (Painting) objects.get(0);
    assertEquals(7, 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 80 with SQLTemplate

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

the class DataContextSharedCacheIT method testSnapshotEvictedAndChangedForModified.

@Test
public void testSnapshotEvictedAndChangedForModified() throws Exception {
    String originalName = artist.getArtistName();
    String newName = "version2";
    String backendName = "version3";
    assertEquals(PersistenceState.COMMITTED, artist.getPersistenceState());
    // modify object PRIOR to killing the snapshot
    artist.setArtistName(newName);
    context.getObjectStore().getDataRowCache().forgetSnapshot(artist.getObjectId());
    assertNull(context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId()));
    // now replace the row in the database
    String template = "UPDATE ARTIST SET ARTIST_NAME = #bind($newName) WHERE ARTIST_NAME = #bind($oldName)";
    SQLTemplate update = new SQLTemplate(Artist.class, template);
    Map<String, Object> map = new HashMap<>(3);
    map.put("newName", backendName);
    map.put("oldName", originalName);
    update.setParams(map);
    context.performNonSelectingQuery(update);
    context.commitChanges();
    assertEquals(newName, artist.getArtistName());
    DataRow freshSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(artist.getObjectId());
    assertNotNull(freshSnapshot);
    assertEquals(newName, freshSnapshot.get("ARTIST_NAME"));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) HashMap(java.util.HashMap) DataRow(org.apache.cayenne.DataRow) 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