Search in sources :

Example 31 with SQLStatement

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

the class VelocitySQLTemplateProcessorTest method testProcessTemplateUnchanged2.

@Test
public void testProcessTemplateUnchanged2() throws Exception {
    String sqlTemplate = "SELECT a.b as XYZ FROM $SYSTEM_TABLE";
    SQLStatement compiled = processor.processTemplate(sqlTemplate, Collections.<String, Object>emptyMap());
    assertEquals(sqlTemplate, compiled.getSql());
    assertEquals(0, compiled.getBindings().length);
}
Also used : SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 32 with SQLStatement

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

the class VelocitySQLTemplateProcessorTest method testProcessTemplateBindCollection.

@Test
public void testProcessTemplateBindCollection() throws Exception {
    String sqlTemplate = "SELECT * FROM ME WHERE COLUMN IN (#bind($list 'VARCHAR'))";
    Map<String, Object> map = Collections.<String, Object>singletonMap("list", Arrays.asList("a", "b", "c"));
    SQLStatement compiled = new VelocitySQLTemplateProcessor().processTemplate(sqlTemplate, map);
    assertEquals("SELECT * FROM ME WHERE COLUMN IN (?,?,?)", compiled.getSql());
    assertEquals(3, compiled.getBindings().length);
    compiled = processor.processTemplate(sqlTemplate, map);
    assertBindingValue("a", compiled.getBindings()[0]);
    assertBindingValue("b", compiled.getBindings()[1]);
    assertBindingValue("c", compiled.getBindings()[2]);
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 33 with SQLStatement

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

the class VelocitySQLTemplateProcessorTest method testProcessTemplateUnchanged1.

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

Example 34 with SQLStatement

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

the class VelocitySQLTemplateProcessorTest method testProcessTemplateNotEqualID.

@Test
public void testProcessTemplateNotEqualID() throws Exception {
    String sqlTemplate = "SELECT * FROM ME WHERE " + "COLUMN1 #bindNotEqual($helper.cayenneExp($a, 'db:ID_COLUMN1')) " + "AND COLUMN2 #bindNotEqual($helper.cayenneExp($a, 'db:ID_COLUMN2'))";
    Map<String, Object> idMap = new HashMap<>();
    idMap.put("ID_COLUMN1", new Integer(3));
    idMap.put("ID_COLUMN2", "aaa");
    ObjectId id = new ObjectId("T", idMap);
    DataObject dataObject = new CayenneDataObject();
    dataObject.setObjectId(id);
    Map<String, Object> map = Collections.<String, Object>singletonMap("a", dataObject);
    SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
    assertEquals("SELECT * FROM ME WHERE COLUMN1 <> ? AND COLUMN2 <> ?", compiled.getSql());
    assertEquals(2, compiled.getBindings().length);
    assertBindingValue(new Integer(3), compiled.getBindings()[0]);
    assertBindingValue("aaa", compiled.getBindings()[1]);
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) HashMap(java.util.HashMap) ObjectId(org.apache.cayenne.ObjectId) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 35 with SQLStatement

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

the class VelocitySQLTemplateProcessorTest method testProcessTemplateConditions.

@Test
public void testProcessTemplateConditions() throws Exception {
    String sqlTemplate = "SELECT * FROM ME #if($a) WHERE COLUMN1 > #bind($a)#end";
    Map<String, Object> map = Collections.<String, Object>singletonMap("a", "VALUE_OF_A");
    SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
    assertEquals("SELECT * FROM ME  WHERE COLUMN1 > ?", compiled.getSql());
    assertEquals(1, compiled.getBindings().length);
    assertBindingValue("VALUE_OF_A", compiled.getBindings()[0]);
    compiled = processor.processTemplate(sqlTemplate, Collections.<String, Object>emptyMap());
    assertEquals("SELECT * FROM ME ", compiled.getSql());
    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