Search in sources :

Example 21 with SQLStatement

use of org.apache.cayenne.access.jdbc.SQLStatement in project cayenne by apache.

the class CayenneSQLTemplateProcessorTest 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.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)

Example 22 with SQLStatement

use of org.apache.cayenne.access.jdbc.SQLStatement in project cayenne by apache.

the class CayenneSQLTemplateProcessorTest method testProcessTemplateBindEqual.

@Test
public void testProcessTemplateBindEqual() throws Exception {
    String sqlTemplate = "SELECT * FROM ME WHERE COLUMN #bindEqual($a 'VARCHAR')";
    SQLStatement compiled = processor.processTemplate(sqlTemplate, Collections.emptyMap());
    assertEquals("SELECT * FROM ME WHERE COLUMN IS NULL", compiled.getSql());
    assertEquals(0, compiled.getBindings().length);
    Map<String, Object> map = Collections.singletonMap("a", "VALUE_OF_A");
    compiled = processor.processTemplate(sqlTemplate, map);
    assertEquals("SELECT * FROM ME WHERE COLUMN = ?", compiled.getSql());
    assertEquals(1, compiled.getBindings().length);
    assertBindingValue("VALUE_OF_A", 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 23 with SQLStatement

use of org.apache.cayenne.access.jdbc.SQLStatement in project cayenne by apache.

the class CayenneSQLTemplateProcessor method process.

protected SQLStatement process(String template, Context context) {
    Node node = templateCache.get(template);
    if (node == null) {
        SQLTemplateParser parser = parserPool.get();
        try {
            parser.ReInit(new ByteArrayInputStream(template.getBytes()));
            node = parser.template();
        } catch (ParseException | TokenMgrError ex) {
            throw new CayenneRuntimeException("Error parsing template '%s' : %s", template, ex.getMessage());
        } finally {
            parserPool.put(parser);
        }
        // can ignore case when someone resolved this template concurrently, it has no side effects
        templateCache.put(template, node);
    }
    node.evaluate(context);
    return new SQLStatement(context.buildTemplate(), context.getColumnDescriptors(), context.getParameterBindings());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Node(org.apache.cayenne.template.parser.Node) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) SQLTemplateParser(org.apache.cayenne.template.parser.SQLTemplateParser) TokenMgrError(org.apache.cayenne.template.parser.TokenMgrError) ParseException(org.apache.cayenne.template.parser.ParseException) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement)

Example 24 with SQLStatement

use of org.apache.cayenne.access.jdbc.SQLStatement in project cayenne by apache.

the class CayenneSQLTemplateProcessorTest method testProcessTemplateUnchanged1.

@Test
public void testProcessTemplateUnchanged1() throws Exception {
    String sqlTemplate = "SELECT * FROM ME";
    SQLStatement compiled = processor.processTemplate(sqlTemplate, Collections.emptyMap());
    assertEquals(sqlTemplate, compiled.getSql());
    assertEquals(0, compiled.getBindings().length);
}
Also used : SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 25 with SQLStatement

use of org.apache.cayenne.access.jdbc.SQLStatement in project cayenne by apache.

the class CayenneSQLTemplateProcessorTest method testProcessTemplateSimpleDynamicContent.

@Test
public void testProcessTemplateSimpleDynamicContent() throws Exception {
    String sqlTemplate = "SELECT * FROM ME WHERE $a";
    Map<String, Object> map = Collections.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)

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