use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class ThrowOnPartialSchemaStrategyIT method testThrowOnPartialStrategyWithOneTable.
@Test
public void testThrowOnPartialStrategyWithOneTable() throws Exception {
createOneTable("SUS1");
setStrategy(ThrowOnPartialSchemaStrategy.class);
String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
SQLTemplate query = new SQLTemplate(Object.class, template);
MockOperationObserver observer = new MockOperationObserver();
try {
node.performQueries(Collections.singletonList(query), observer);
assertEquals(1, existingTables().size());
fail("Must have thrown on partial schema");
} catch (CayenneRuntimeException e) {
// expected
}
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class ThrowOnPartialSchemaStrategyIT method testThrowOnPartialStrategyTableExist.
@Test
public void testThrowOnPartialStrategyTableExist() throws Exception {
String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
SQLTemplate query = new SQLTemplate(Object.class, template);
MockOperationObserver observer = new MockOperationObserver();
createOneTable("SUS1");
createOneTable("SUS2");
setStrategy(ThrowOnPartialSchemaStrategy.class);
node.performQueries(Collections.singletonList(query), observer);
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class ThrowOnPartialSchemaStrategyIT method testThrowOnPartialStrategyTableNoExist.
@Test
public void testThrowOnPartialStrategyTableNoExist() throws Exception {
String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
SQLTemplate query = new SQLTemplate(Object.class, template);
MockOperationObserver observer = new MockOperationObserver();
setStrategy(ThrowOnPartialSchemaStrategy.class);
try {
node.performQueries(Collections.singletonList((Query) query), observer);
} catch (CayenneRuntimeException e) {
assertNotNull(e);
}
try {
node.performQueries(Collections.singletonList((Query) query), observer);
} catch (CayenneRuntimeException e) {
assertNotNull(e);
}
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class SQLTemplateActionIT method testExtractTemplateString.
@Test
public void testExtractTemplateString() throws Exception {
SQLTemplate template = new SQLTemplate(Artist.class, "A\nBC");
SQLTemplateAction action = new SQLTemplateAction(template, node);
assertEquals("A BC", action.extractTemplateString());
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class SQLTemplateActionIT method testExecuteSelect.
@Test
public void testExecuteSelect() throws Exception {
createFourArtists();
String templateString = "SELECT * FROM ARTIST WHERE ARTIST_ID = #bind($id)";
SQLTemplate template = new SQLTemplate(Object.class, templateString);
sqlTemplateCustomizer.updateSQLTemplate(template);
Map<String, Object> bindings = new HashMap<>();
bindings.put("id", 201l);
template.setParameters(bindings);
// must ensure the right SQLTemplateAction is created
SQLAction plan = adapter.getAction(template, node);
assertTrue(plan instanceof SQLTemplateAction);
MockOperationObserver observer = new MockOperationObserver();
try (Connection c = dataSourceFactory.getSharedDataSource().getConnection()) {
plan.performAction(c, observer);
}
List<DataRow> rows = observer.rowsForQuery(template);
assertNotNull(rows);
assertEquals(1, rows.size());
DataRow row = rows.get(0);
// In the absence of ObjEntity most DB's return a Long here, except for
// Oracle
// that has no BIGINT type and
// returns BigDecimal, so do a Number comparison
Number id = (Number) row.get("ARTIST_ID");
assertNotNull(id);
assertEquals(((Number) bindings.get("id")).longValue(), id.longValue());
assertEquals("artist4", row.get("ARTIST_NAME"));
assertTrue(row.containsKey("DATE_OF_BIRTH"));
}
Aggregations