Search in sources :

Example 11 with ExpressionEvaluator

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

the class SqlParserTest method testFor_index.

@Test
public void testFor_index() {
    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_index */ = /*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 name0 = ? or name1 = ? or name2 = ?", sql.getRawSql());
    assertEquals("select * from aaa where name0 = 'aaa' or name1 = 'bbb' or name2 = '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 12 with ExpressionEvaluator

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

the class SqlParserTest method testIf_nestContinuously.

@Test
public void testIf_nestContinuously() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, "hoge"));
    evaluator.add("name2", new Value(String.class, null));
    String testSql = "select * from aaa where /*%if name != null*//*%if name2 == \"hoge\"*/ ddd = eee/*%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) 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 13 with ExpressionEvaluator

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

the class SqlParserTest method testLiteralVariable.

@Test
public void testLiteralVariable() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, "hoge"));
    evaluator.add("salary", new Value(BigDecimal.class, new BigDecimal(10000)));
    String testSql = "select * from aaa where ename = /*^name*/'aaa' and sal = /*^salary*/-2000";
    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 ename = 'hoge' and sal = 10000", sql.getRawSql());
    assertEquals("select * from aaa where ename = 'hoge' and sal = 10000", sql.getFormattedSql());
    assertEquals(0, sql.getParameters().size());
}
Also used : PreparedSql(org.seasar.doma.jdbc.PreparedSql) Value(org.seasar.doma.internal.expr.Value) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) BigDecimal(java.math.BigDecimal) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 14 with ExpressionEvaluator

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

the class SqlParserTest method testSelect.

@Test
public void testSelect() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, "hoge"));
    evaluator.add("count", new Value(Integer.class, 5));
    String testSql = "select aaa.deptname, count(*) from aaa join bbb on aaa.id = bbb.id where aaa.name = /*name*/'ccc' group by aaa.deptname having count(*) > /*count*/10 order by aaa.name for update bbb";
    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 aaa.deptname, count(*) from aaa join bbb on aaa.id = bbb.id where aaa.name = ? group by aaa.deptname having count(*) > ? order by aaa.name for update bbb", sql.getRawSql());
    assertEquals("select aaa.deptname, count(*) from aaa join bbb on aaa.id = bbb.id where aaa.name = 'hoge' group by aaa.deptname having count(*) > 5 order by aaa.name for update bbb", sql.getFormattedSql());
    assertEquals(2, sql.getParameters().size());
    assertEquals("hoge", sql.getParameters().get(0).getWrapper().get());
    assertEquals(5, sql.getParameters().get(1).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 15 with ExpressionEvaluator

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

the class SqlParserTest method testIf_removeAnd.

@Test
public void testIf_removeAnd() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, null));
    String testSql = "select * from aaa where \n/*%if name != null*/bbb = /*name*/'ccc' \n/*%else*/\n --comment\nand ddd is null\n /*%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 \n\n --comment\n ddd is null", sql.getRawSql());
    assertEquals("select * from aaa where \n\n --comment\n ddd is null", sql.getFormattedSql());
    assertEquals(0, sql.getParameters().size());
}
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)

Aggregations

ExpressionEvaluator (org.seasar.doma.internal.expr.ExpressionEvaluator)83 Test (org.junit.jupiter.api.Test)77 Value (org.seasar.doma.internal.expr.Value)52 PreparedSql (org.seasar.doma.jdbc.PreparedSql)51 SqlNode (org.seasar.doma.jdbc.SqlNode)48 EvaluationResult (org.seasar.doma.internal.expr.EvaluationResult)18 BigDecimal (java.math.BigDecimal)14 List (java.util.List)14 ArrayList (java.util.ArrayList)12 JdbcException (org.seasar.doma.jdbc.JdbcException)10 NodePreparedSqlBuilder (org.seasar.doma.internal.jdbc.sql.NodePreparedSqlBuilder)8 AnonymousNode (org.seasar.doma.internal.jdbc.sql.node.AnonymousNode)6 FromClauseNode (org.seasar.doma.internal.jdbc.sql.node.FromClauseNode)6 SelectClauseNode (org.seasar.doma.internal.jdbc.sql.node.SelectClauseNode)6 SelectStatementNode (org.seasar.doma.internal.jdbc.sql.node.SelectStatementNode)6 WhereClauseNode (org.seasar.doma.internal.jdbc.sql.node.WhereClauseNode)6 WordNode (org.seasar.doma.internal.jdbc.sql.node.WordNode)6 EndNode (org.seasar.doma.internal.jdbc.sql.node.EndNode)5 IfBlockNode (org.seasar.doma.internal.jdbc.sql.node.IfBlockNode)5 IfNode (org.seasar.doma.internal.jdbc.sql.node.IfNode)5