use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class CayenneContextCayenneIT method testObjectForPK.
@Test
public void testObjectForPK() throws Exception {
context.performGenericQuery(new SQLTemplate(ClientMtTable1.class, "insert into MT_TABLE1 " + "(TABLE1_ID, GLOBAL_ATTRIBUTE1, SERVER_ATTRIBUTE1) " + "values (1, 'g1', 's1')"));
ClientMtTable1 o = Cayenne.objectForPK(context, ClientMtTable1.class, 1);
assertNotNull(o);
assertEquals("g1", o.getGlobalAttribute1());
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class SQLTemplateCustomizer method createSQLTemplate.
public SQLTemplate createSQLTemplate(Class<?> root, String defaultTemplate) {
SQLTemplate template = new SQLTemplate(root, defaultTemplate);
updateSQLTemplate(template);
return template;
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class BindDirectiveIT method performInsertForParameters.
/**
* Inserts row for given parameters
*
* @return inserted row
*/
private Map<String, ?> performInsertForParameters(Map<String, Object> parameters, String templateString) throws Exception {
// TODO: do we really care if an inserting SQLTemplate is executed via
// ObjectContext?
SQLTemplate template = new SQLTemplate(Object.class, templateString);
template.setParams(parameters);
MockOperationObserver observer = new MockOperationObserver();
node.performQueries(Collections.singletonList(template), observer);
return ObjectSelect.dataRowQuery(Artist.class).selectOne(context);
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class BindDirectiveIT method testBind_Collection.
@Test
public void testBind_Collection() throws Exception {
TableHelper tArtist = new TableHelper(dbHelper, "ARTIST").setColumns("ARTIST_ID", "ARTIST_NAME");
// insert 3 artists
for (int i = 1; i < 4; i++) {
tArtist.insert(new Long(i), "Artist" + i);
}
// now select only with names: Artist1 and Artist3
Set<String> artistNames = new HashSet<String>();
artistNames.add("Artist1");
artistNames.add("Artist3");
String sql = "SELECT * FROM ARTIST WHERE ARTIST_NAME in (#bind($ARTISTNAMES))";
SQLTemplate query = new SQLTemplate(Artist.class, sql);
// customize for DB's that require trimming CHAR spaces
query.setTemplate(OracleAdapter.class.getName(), "SELECT * FROM ARTIST WHERE RTRIM(ARTIST_NAME) in (#bind($ARTISTNAMES))");
query.setColumnNamesCapitalization(CapsStrategy.UPPER);
query.setParams(Collections.singletonMap("ARTISTNAMES", artistNames));
List<?> result = context.performQuery(query);
assertEquals(2, result.size());
}
use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.
the class ResultDirectiveIT method selectForQuery.
private Map<String, Object> selectForQuery(String sql) {
SQLTemplate template = new SQLTemplate(Artist.class, sql);
template.setColumnNamesCapitalization(CapsStrategy.UPPER);
MockOperationObserver observer = new MockOperationObserver();
runtime.getDataDomain().performQueries(Collections.singletonList(template), observer);
@SuppressWarnings("unchecked") List<Map<String, Object>> data = observer.rowsForQuery(template);
assertEquals(1, data.size());
Map<String, Object> row = data.get(0);
return row;
}
Aggregations