Search in sources :

Example 1 with SQLStatement

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);
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) ArrayList(java.util.ArrayList) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) ParameterBinding(org.apache.cayenne.access.translator.ParameterBinding) ExpressionException(org.apache.cayenne.exp.ExpressionException) ParseException(org.apache.velocity.runtime.parser.ParseException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 2 with SQLStatement

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]);
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 3 with SQLStatement

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());
}
Also used : SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 4 with SQLStatement

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);
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 5 with SQLStatement

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]);
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Aggregations

SQLStatement (org.apache.cayenne.access.jdbc.SQLStatement)44 Test (org.junit.Test)42 CayenneDataObject (org.apache.cayenne.CayenneDataObject)20 DataObject (org.apache.cayenne.DataObject)20 HashMap (java.util.HashMap)10 ObjectId (org.apache.cayenne.ObjectId)4 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 ColumnDescriptor (org.apache.cayenne.access.jdbc.ColumnDescriptor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1 ParameterBinding (org.apache.cayenne.access.translator.ParameterBinding)1 ExpressionException (org.apache.cayenne.exp.ExpressionException)1 Node (org.apache.cayenne.template.parser.Node)1 ParseException (org.apache.cayenne.template.parser.ParseException)1 SQLTemplateParser (org.apache.cayenne.template.parser.SQLTemplateParser)1 TokenMgrError (org.apache.cayenne.template.parser.TokenMgrError)1 VelocityContext (org.apache.velocity.VelocityContext)1 ParseException (org.apache.velocity.runtime.parser.ParseException)1