Search in sources :

Example 26 with SQLStatement

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

the class CayenneSQLTemplateProcessorTest method testProcessTemplateBindNotEqual.

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

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

the class CayenneSQLTemplateProcessorTest method testProcessTemplateBindGuessVarchar.

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

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

the class CayenneSQLTemplateProcessorTest method testProcessTemplateBindGuessInteger.

@Test
public void testProcessTemplateBindGuessInteger() throws Exception {
    String sqlTemplate = "SELECT * FROM ME WHERE COLUMN1 = #bind($a)";
    Map<String, Object> map = Collections.singletonMap("a", 4);
    SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
    assertEquals(1, compiled.getBindings().length);
    assertBindingType(Types.INTEGER, 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 29 with SQLStatement

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

the class CayenneSQLTemplateProcessorTest method testProcessTemplateID.

@Test
public void testProcessTemplateID() throws Exception {
    String sqlTemplate = "SELECT * FROM ME WHERE COLUMN1 = #bind($helper.cayenneExp($a, 'db:ID_COLUMN'))";
    DataObject dataObject = new CayenneDataObject();
    dataObject.setObjectId(new ObjectId("T", "ID_COLUMN", 5));
    Map<String, Object> map = Collections.singletonMap("a", dataObject);
    SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
    assertEquals("SELECT * FROM ME WHERE COLUMN1 = ?", compiled.getSql());
    assertEquals(1, compiled.getBindings().length);
    assertBindingValue(5, compiled.getBindings()[0]);
}
Also used : CayenneDataObject(org.apache.cayenne.CayenneDataObject) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DataObject(org.apache.cayenne.DataObject) 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 30 with SQLStatement

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

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