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);
}
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());
}
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));
}
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));
}
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"));
}
Aggregations