use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataNodeQueriesIT method testRunMultiLineSQLTemplateUNIX.
@Test
public void testRunMultiLineSQLTemplateUNIX() throws Exception {
String templateString = "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME)" + "\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 DataContextExtrasIT method testSelectException.
/**
* Testing behavior of Cayenne when a database exception is thrown in SELECT query.
*/
@Test
public void testSelectException() {
SQLTemplate q = new SQLTemplate(Artist.class, "SELECT * FROM NON_EXISTENT_TABLE");
try {
context.performGenericQuery(q);
fail("Query was invalid and was supposed to fail.");
} catch (RuntimeException ex) {
// exception expected
}
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataContextIT method testPerformNonSelectingQueryCounts2.
@Test
public void testPerformNonSelectingQueryCounts2() throws Exception {
createArtistsDataSet();
SQLTemplate query = new SQLTemplate(Painting.class, "INSERT INTO PAINTING (PAINTING_ID, PAINTING_TITLE, ARTIST_ID, ESTIMATED_PRICE) " + "VALUES ($pid, '$pt', $aid, #bind($price 'DECIMAL' 2))");
Map<String, Object>[] maps = new Map[3];
for (int i = 0; i < maps.length; i++) {
maps[i] = new HashMap<>();
maps[i].put("pid", new Integer(1 + i));
maps[i].put("pt", "P-" + i);
maps[i].put("aid", new Integer(33002));
maps[i].put("price", new BigDecimal("1." + i));
}
// single batch of parameters
query.setParameters(maps);
int[] counts = context.performNonSelectingQuery(query);
assertNotNull(counts);
assertEquals(maps.length, counts.length);
for (int i = 0; i < maps.length; i++) {
assertEquals(1, counts[i]);
}
SQLTemplate delete = new SQLTemplate(Painting.class, "delete from PAINTING");
counts = context.performNonSelectingQuery(delete);
assertNotNull(counts);
assertEquals(1, counts.length);
assertEquals(3, counts[0]);
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataContextObjectIdQueryIT method testRefreshNullifiedValuesNew.
@Test
public void testRefreshNullifiedValuesNew() {
Artist a = context.newObject(Artist.class);
a.setArtistName("X");
a.setDateOfBirth(new Date());
context.commitChanges();
context.performGenericQuery(new SQLTemplate(Artist.class, "UPDATE ARTIST SET DATE_OF_BIRTH = NULL"));
long id = Cayenne.longPKForObject(a);
ObjectIdQuery query = new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, id), false, ObjectIdQuery.CACHE_REFRESH);
Artist a1 = (Artist) Cayenne.objectForQuery(context, query);
assertNull(a1.getDateOfBirth());
assertEquals("X", a1.getArtistName());
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class DataContextObjectIdQueryIT method testRefreshNullifiedValuesExisting.
@Test
public void testRefreshNullifiedValuesExisting() {
SQLTemplate insert = new SQLTemplate(Artist.class, "INSERT INTO ARTIST (ARTIST_ID, ARTIST_NAME, DATE_OF_BIRTH) VALUES (44, 'X', #bind($date 'DATE'))");
insert.setParameters(Collections.singletonMap("date", new Date()));
context.performGenericQuery(insert);
Artist a = Cayenne.objectForPK(context, Artist.class, 44l);
assertNotNull(a.getDateOfBirth());
assertEquals("X", a.getArtistName());
context.performGenericQuery(new SQLTemplate(Artist.class, "UPDATE ARTIST SET DATE_OF_BIRTH = NULL"));
ObjectIdQuery query = new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, 44l), false, ObjectIdQuery.CACHE_REFRESH);
Artist a1 = (Artist) Cayenne.objectForQuery(context, query);
assertNull(a1.getDateOfBirth());
assertEquals("X", a1.getArtistName());
}
Aggregations