Search in sources :

Example 21 with SqlNode

use of org.seasar.doma.jdbc.SqlNode in project doma by domaframework.

the class NodePreparedSqlBuilderTest method testWhere.

@Test
public void testWhere() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    String testSql = "select * from aaa where /*%if false*/ename = 'aaa'/*%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());
}
Also used : PreparedSql(org.seasar.doma.jdbc.PreparedSql) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 22 with SqlNode

use of org.seasar.doma.jdbc.SqlNode 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 23 with SqlNode

use of org.seasar.doma.jdbc.SqlNode 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 24 with SqlNode

use of org.seasar.doma.jdbc.SqlNode in project doma by domaframework.

the class SqlParserTest method testBindVariable_floatLiteral.

@Test
public void testBindVariable_floatLiteral() {
    String testSql = "select * from aaa where ename = /*name*/.0";
    SqlParser parser = new SqlParser(testSql);
    SqlNode node = parser.parse();
    assertNotNull(node);
}
Also used : SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 25 with SqlNode

use of org.seasar.doma.jdbc.SqlNode 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

SqlNode (org.seasar.doma.jdbc.SqlNode)176 Test (org.junit.jupiter.api.Test)145 PreparedSql (org.seasar.doma.jdbc.PreparedSql)102 SqlParser (org.seasar.doma.internal.jdbc.sql.SqlParser)90 MockConfig (org.seasar.doma.internal.jdbc.mock.MockConfig)62 NodePreparedSqlBuilder (org.seasar.doma.internal.jdbc.sql.NodePreparedSqlBuilder)62 ExpressionEvaluator (org.seasar.doma.internal.expr.ExpressionEvaluator)48 Value (org.seasar.doma.internal.expr.Value)44 LinkedHashMap (java.util.LinkedHashMap)25 ExecutableElement (javax.lang.model.element.ExecutableElement)25 TestProcessor (org.seasar.doma.internal.apt.TestProcessor)25 JdbcException (org.seasar.doma.jdbc.JdbcException)20 List (java.util.List)17 BigDecimal (java.math.BigDecimal)13 ArrayList (java.util.ArrayList)12 FragmentNode (org.seasar.doma.internal.jdbc.sql.node.FragmentNode)11 SelectStatementNode (org.seasar.doma.internal.jdbc.sql.node.SelectStatementNode)11 AptException (org.seasar.doma.internal.apt.AptException)8 OrderByClauseNode (org.seasar.doma.internal.jdbc.sql.node.OrderByClauseNode)8 WordNode (org.seasar.doma.internal.jdbc.sql.node.WordNode)6