Search in sources :

Example 16 with Value

use of org.seasar.doma.internal.expr.Value in project doma by domaframework.

the class NodePreparedSqlBuilderTest method testWhere_embeddedVariable.

@Test
public void testWhere_embeddedVariable() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("embedded", new Value(String.class, "bbb = ccc"));
    String testSql = "select * from aaa where /*%if false*/ename = 'aaa'/*%end */ /*#embedded*/";
    SqlParser parser = new SqlParser(testSql);
    SqlNode sqlNode = parser.parse();
    PreparedSql sql = new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED).build(sqlNode, Function.identity());
    assertEquals("select * from aaa where  bbb = ccc", sql.getRawSql());
}
Also used : PreparedSql(org.seasar.doma.jdbc.PreparedSql) Value(org.seasar.doma.internal.expr.Value) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 17 with Value

use of org.seasar.doma.internal.expr.Value in project doma by domaframework.

the class NodePreparedSqlBuilderTest method testEmbeddedVariable_issue_95.

@Test
public void testEmbeddedVariable_issue_95() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("envPrefix", new Value(String.class, "prefix_"));
    String testSql = "select * from /*#envPrefix*/SCHEMA.TABLE";
    SqlParser parser = new SqlParser(testSql);
    SqlNode sqlNode = parser.parse();
    PreparedSql sql = new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED).build(sqlNode, Function.identity());
    assertEquals("select * from prefix_SCHEMA.TABLE", sql.getRawSql());
}
Also used : PreparedSql(org.seasar.doma.jdbc.PreparedSql) Value(org.seasar.doma.internal.expr.Value) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 18 with Value

use of org.seasar.doma.internal.expr.Value in project doma by domaframework.

the class SqlParserTest method testIf.

@Test
public void testIf() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, "hoge"));
    String testSql = "select * from aaa where /*%if name != null*/bbb = /*name*/'ccc' /*%end*/";
    SqlParser parser = new SqlParser(testSql);
    SqlNode sqlNode = parser.parse();
    PreparedSql sql = new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED).build(sqlNode, Function.identity());
    assertEquals("select * from aaa where bbb = ?", sql.getRawSql());
    assertEquals("select * from aaa where bbb = 'hoge'", sql.getFormattedSql());
    assertEquals(1, sql.getParameters().size());
    assertEquals("hoge", sql.getParameters().get(0).getWrapper().get());
}
Also used : PreparedSql(org.seasar.doma.jdbc.PreparedSql) Value(org.seasar.doma.internal.expr.Value) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 19 with Value

use of org.seasar.doma.internal.expr.Value in project doma by domaframework.

the class SqlParserTest method testFor.

@Test
public void testFor() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    ArrayList<String> list = new ArrayList<>();
    list.add("aaa");
    list.add("bbb");
    list.add("ccc");
    evaluator.add("names", new Value(List.class, list));
    String testSql = "select * from aaa where /*%for n : names*/name = /*n*/'a' /*%if n_has_next */or /*%end*//*%end*/";
    SqlParser parser = new SqlParser(testSql);
    SqlNode sqlNode = parser.parse();
    PreparedSql sql = new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED).build(sqlNode, Function.identity());
    assertEquals("select * from aaa where name = ? or name = ? or name = ?", sql.getRawSql());
    assertEquals("select * from aaa where name = 'aaa' or name = 'bbb' or name = 'ccc'", sql.getFormattedSql());
    assertEquals(3, sql.getParameters().size());
    assertEquals("aaa", sql.getParameters().get(0).getWrapper().get());
    assertEquals("bbb", sql.getParameters().get(1).getWrapper().get());
    assertEquals("ccc", sql.getParameters().get(2).getWrapper().get());
}
Also used : PreparedSql(org.seasar.doma.jdbc.PreparedSql) ArrayList(java.util.ArrayList) Value(org.seasar.doma.internal.expr.Value) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 20 with Value

use of org.seasar.doma.internal.expr.Value in project doma by domaframework.

the class SqlParserTest method testFor_removeWhere.

@Test
public void testFor_removeWhere() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    ArrayList<String> list = new ArrayList<>();
    evaluator.add("names", new Value(List.class, list));
    String testSql = "select * from aaa where /*%for n : names*/name = /*n*/'a' /*%if n_has_next */or /*%end*//*%end*/";
    SqlParser parser = new SqlParser(testSql);
    SqlNode sqlNode = parser.parse();
    PreparedSql sql = new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED).build(sqlNode, Function.identity());
    assertEquals("select * from aaa", sql.getRawSql());
    assertEquals("select * from aaa", sql.getFormattedSql());
    assertEquals(0, sql.getParameters().size());
}
Also used : PreparedSql(org.seasar.doma.jdbc.PreparedSql) ArrayList(java.util.ArrayList) Value(org.seasar.doma.internal.expr.Value) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Aggregations

Value (org.seasar.doma.internal.expr.Value)54 ExpressionEvaluator (org.seasar.doma.internal.expr.ExpressionEvaluator)49 Test (org.junit.jupiter.api.Test)47 SqlNode (org.seasar.doma.jdbc.SqlNode)41 PreparedSql (org.seasar.doma.jdbc.PreparedSql)38 BigDecimal (java.math.BigDecimal)11 List (java.util.List)11 ArrayList (java.util.ArrayList)10 JdbcException (org.seasar.doma.jdbc.JdbcException)8 NodePreparedSqlBuilder (org.seasar.doma.internal.jdbc.sql.NodePreparedSqlBuilder)5 SqlParser (org.seasar.doma.internal.jdbc.sql.SqlParser)3 EvaluationResult (org.seasar.doma.internal.expr.EvaluationResult)2 PhoneNumber (example.domain.PhoneNumber)1 LinkedHashMap (java.util.LinkedHashMap)1 MockConfig (org.seasar.doma.internal.jdbc.mock.MockConfig)1 AnonymousNode (org.seasar.doma.internal.jdbc.sql.node.AnonymousNode)1 BindVariableNode (org.seasar.doma.internal.jdbc.sql.node.BindVariableNode)1 EndNode (org.seasar.doma.internal.jdbc.sql.node.EndNode)1 ForNode (org.seasar.doma.internal.jdbc.sql.node.ForNode)1 FromClauseNode (org.seasar.doma.internal.jdbc.sql.node.FromClauseNode)1