Search in sources :

Example 36 with SQLStatement

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

the class VelocitySQLTemplateProcessorTest method testProcessTemplateBindGuessInteger.

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

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

the class VelocitySQLTemplateProcessorTest 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.<String, Object>singletonMap("a", dataObject);
    SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
    assertEquals("SELECT * FROM ME WHERE COLUMN1 = ?", compiled.getSql());
    assertEquals(1, compiled.getBindings().length);
    assertBindingValue(new Integer(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 38 with SQLStatement

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

the class VelocitySQLTemplateProcessor_ChainTest method testProcessTemplateNoChunks.

@Test
public void testProcessTemplateNoChunks() throws Exception {
    // whatever is inside the chain, it should render as empty if there
    // is no chunks...
    SQLStatement compiled = processor.processTemplate("#chain(' AND ') #end", Collections.<String, Object>emptyMap());
    assertEquals("", compiled.getSql());
    compiled = processor.processTemplate("#chain(' AND ') garbage #end", Collections.<String, Object>emptyMap());
    assertEquals("", compiled.getSql());
    compiled = processor.processTemplate("#chain(' AND ' 'PREFIX') #end", Collections.<String, Object>emptyMap());
    assertEquals("", compiled.getSql());
    compiled = processor.processTemplate("#chain(' AND ' 'PREFIX') garbage #end", Collections.<String, Object>emptyMap());
    assertEquals("", compiled.getSql());
}
Also used : SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 39 with SQLStatement

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

the class VelocitySQLTemplateProcessor_ChainTest method testProcessTemplateChainWithGarbage.

@Test
public void testProcessTemplateChainWithGarbage() throws Exception {
    String template = "#chain(' OR ' 'WHERE ')" + "#chunk($a)$a#end" + " some other stuff" + "#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] some other stuff OR [C]", compiled.getSql());
}
Also used : HashMap(java.util.HashMap) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 40 with SQLStatement

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

the class VelocitySQLTemplateProcessor_ChainTest method testProcessTemplateChainUnconditionalChunks.

@Test
public void testProcessTemplateChainUnconditionalChunks() throws Exception {
    String template = "#chain(' OR ' 'WHERE ')" + "#chunk()C1#end" + "#chunk()C2#end" + "#chunk()C3#end" + "#end";
    SQLStatement compiled = processor.processTemplate(template, Collections.<String, Object>emptyMap());
    assertEquals("WHERE C1 OR C2 OR C3", 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