Search in sources :

Example 6 with SQLStatement

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

the class VelocitySQLTemplateProcessorTest method testProcessTemplateBindNotEqual.

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

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

the class VelocitySQLTemplateProcessorTest method testProcessTemplateBindEqual.

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

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

the class VelocitySQLTemplateProcessor_ChainTest method testProcessTemplatePartialChainStart.

@Test
public void testProcessTemplatePartialChainStart() throws Exception {
    String template = "#chain(' OR ' 'WHERE ')" + "#chunk($a)$a#end" + "#chunk($b)$b#end" + "#chunk($c)$c#end" + "#end";
    Map<String, Object> map = new HashMap<>();
    map.put("b", "[B]");
    map.put("c", "[C]");
    SQLStatement compiled = processor.processTemplate(template, map);
    assertEquals("WHERE [B] OR [C]", compiled.getSql());
}
Also used : HashMap(java.util.HashMap) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 9 with SQLStatement

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

the class VelocitySQLTemplateProcessor_ChainTest method testProcessTemplatePartialChainMiddle.

@Test
public void testProcessTemplatePartialChainMiddle() throws Exception {
    String template = "#chain(' OR ' 'WHERE ')" + "#chunk($a)$a#end" + "#chunk($b)$b#end" + "#chunk($c)$c#end" + "#end";
    Map<String, Object> map = new HashMap<>();
    map.put("a", "[A]");
    map.put("c", "[C]");
    SQLStatement compiled = processor.processTemplate(template, map);
    assertEquals("WHERE [A] OR [C]", compiled.getSql());
}
Also used : HashMap(java.util.HashMap) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 10 with SQLStatement

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

the class VelocitySQLTemplateProcessor_ChainTest method testProcessTemplateEmptyChain.

@Test
public void testProcessTemplateEmptyChain() throws Exception {
    String template = "#chain(' OR ' 'WHERE ')" + "#chunk($a)$a#end" + "#chunk($b)$b#end" + "#chunk($c)$c#end" + "#end";
    SQLStatement compiled = processor.processTemplate(template, Collections.<String, Object>emptyMap());
    assertEquals("", compiled.getSql());
}
Also used : 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