use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataContextSharedCacheIT method testSnapshotChangePropagationOnSelect.
/**
* Test case to prove that refreshing snapshots as a result of the database fetch will
* be propagated across DataContexts.
*/
@Test
public void testSnapshotChangePropagationOnSelect() throws Exception {
String originalName = artist.getArtistName();
final String newName = "version2";
// update artist using raw SQL
SQLTemplate query = sqlTemplateCustomizer.createSQLTemplate(Artist.class, "UPDATE ARTIST SET ARTIST_NAME = #bind($newName) " + "WHERE ARTIST_NAME = #bind($oldName)");
Map<String, Object> map = new HashMap<>(3);
map.put("newName", newName);
map.put("oldName", originalName);
query.setParams(map);
context.performNonSelectingQuery(query);
// fetch updated artist into the new context, and see if the original
// one gets updated
Expression qual = ExpressionFactory.matchExp("artistName", newName);
List artists = context1.performQuery(new SelectQuery<>(Artist.class, qual));
assertEquals(1, artists.size());
Artist altArtist = (Artist) artists.get(0);
// check underlying cache
DataRow freshSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(altArtist.getObjectId());
assertNotNull(freshSnapshot);
assertEquals(newName, freshSnapshot.get("ARTIST_NAME"));
// check both artists
assertEquals(newName, altArtist.getArtistName());
ParallelTestContainer helper = new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertEquals("Peer object state wasn't refreshed on fetch", newName, artist.getArtistName());
}
};
helper.runTest(3000);
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataNodeQueriesIT method testRunMultiLineSQLTemplateMac.
@Test
public void testRunMultiLineSQLTemplateMac() throws Exception {
String templateString = "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME)" + "\r" + "VALUES (1, 'A')";
Query template = new SQLTemplate(Object.class, templateString);
node.performQueries(Collections.singletonList(template), new MockOperationObserver());
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataNodeQueriesIT method testPerfomQueriesSelectingSQLTemplate1.
@Test
public void testPerfomQueriesSelectingSQLTemplate1() throws Exception {
createFourArtists();
String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
SQLTemplate query = new SQLTemplate(Object.class, template);
MockOperationObserver observer = new MockOperationObserver();
node.performQueries(Collections.singletonList((Query) query), observer);
List<DataRow> data = observer.rowsForQuery(query);
assertEquals(4, data.size());
DataRow row = data.get(2);
assertEquals(1, row.size());
assertEquals(201, row.get("ARTIST_ID"));
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataNodeQueriesIT method testRunMultiLineSQLTemplateWindows.
@Test
public void testRunMultiLineSQLTemplateWindows() throws Exception {
String templateString = "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME)" + "\r\n" + "VALUES (1, 'A')";
Query template = new SQLTemplate(Object.class, templateString);
node.performQueries(Collections.singletonList(template), new MockOperationObserver());
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class SQLTemplateDescriptor method buildQuery.
@Override
public SQLTemplate buildQuery() {
SQLTemplate template = new SQLTemplate();
if (root != null) {
template.setRoot(root);
}
if (prefetchesMap != null) {
for (Map.Entry<String, Integer> entry : prefetchesMap.entrySet()) {
template.addPrefetch(PrefetchTreeNode.withPath(entry.getKey(), entry.getValue()));
}
}
template.initWithProperties(this.getProperties());
// init SQL
template.setDefaultTemplate(this.getSql());
Map<String, String> adapterSql = this.getAdapterSql();
if (adapterSql != null) {
for (Map.Entry<String, String> entry : adapterSql.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key != null && value != null) {
template.setTemplate(key, value);
}
}
}
return template;
}
Aggregations