Search in sources :

Example 6 with Value

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

the class SqlParserTest method testIf_removeGroupBy.

@Test
public void testIf_removeGroupBy() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, null));
    String testSql = "select * from aaa group by /*%if name != null*/bbb/*%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 7 with Value

use of org.seasar.doma.internal.expr.Value 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 8 with Value

use of org.seasar.doma.internal.expr.Value 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 9 with Value

use of org.seasar.doma.internal.expr.Value 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 10 with Value

use of org.seasar.doma.internal.expr.Value 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)

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