use of org.apache.cayenne.access.jdbc.SQLStatement in project cayenne by apache.
the class VelocitySQLTemplateProcessor method processTemplate.
SQLStatement processTemplate(String template, SimpleNode parsedTemplate, Map<String, Object> parameters) {
List<ParameterBinding> bindings = new ArrayList<>();
List<ColumnDescriptor> results = new ArrayList<>();
parameters.put(BINDINGS_LIST_KEY, bindings);
parameters.put(RESULT_COLUMNS_LIST_KEY, results);
parameters.put(HELPER_KEY, renderingUtils);
String sql;
try {
sql = buildStatement(new VelocityContext(parameters), template, parsedTemplate);
} catch (Exception e) {
throw new CayenneRuntimeException("Error processing Velocity template", e);
}
ParameterBinding[] bindingsArray = new ParameterBinding[bindings.size()];
bindings.toArray(bindingsArray);
ColumnDescriptor[] resultsArray = new ColumnDescriptor[results.size()];
results.toArray(resultsArray);
return new SQLStatement(sql, resultsArray, bindingsArray);
}
use of org.apache.cayenne.access.jdbc.SQLStatement in project cayenne by apache.
the class VelocitySQLTemplateProcessorTest method testProcessTemplateBindGuessVarchar.
@Test
public void testProcessTemplateBindGuessVarchar() throws Exception {
String sqlTemplate = "SELECT * FROM ME WHERE COLUMN1 = #bind($a)";
Map<String, Object> map = Collections.<String, Object>singletonMap("a", "VALUE_OF_A");
SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
assertEquals(1, compiled.getBindings().length);
assertBindingType(Types.VARCHAR, compiled.getBindings()[0]);
}
use of org.apache.cayenne.access.jdbc.SQLStatement in project cayenne by apache.
the class VelocitySQLTemplateProcessorTest method testUnknownDirective.
@Test
public void testUnknownDirective() throws Exception {
String sqlTemplate = "SELECT #from(1) FROM a";
SQLStatement compiled = processor.processTemplate(sqlTemplate, Collections.emptyMap());
assertEquals("SELECT #from(1) FROM a", compiled.getSql());
}
use of org.apache.cayenne.access.jdbc.SQLStatement in project cayenne by apache.
the class VelocitySQLTemplateProcessorTest method testProcessTemplateSimpleDynamicContent.
@Test
public void testProcessTemplateSimpleDynamicContent() throws Exception {
String sqlTemplate = "SELECT * FROM ME WHERE $a";
Map<String, Object> map = Collections.<String, Object>singletonMap("a", "VALUE_OF_A");
SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
assertEquals("SELECT * FROM ME WHERE VALUE_OF_A", compiled.getSql());
// bindings are not populated, since no "bind" macro is used.
assertEquals(0, compiled.getBindings().length);
}
use of org.apache.cayenne.access.jdbc.SQLStatement in project cayenne by apache.
the class VelocitySQLTemplateProcessorTest method testProcessTemplateBind.
@Test
public void testProcessTemplateBind() throws Exception {
String sqlTemplate = "SELECT * FROM ME WHERE " + "COLUMN1 = #bind($a 'VARCHAR') AND COLUMN2 = #bind($b 'INTEGER')";
Map<String, Object> map = Collections.<String, Object>singletonMap("a", "VALUE_OF_A");
SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
assertEquals("SELECT * FROM ME WHERE COLUMN1 = ? AND COLUMN2 = ?", compiled.getSql());
assertEquals(2, compiled.getBindings().length);
assertBindingValue("VALUE_OF_A", compiled.getBindings()[0]);
assertBindingValue(null, compiled.getBindings()[1]);
}
Aggregations