Search in sources :

Example 11 with SqlNode

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

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

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

Example 14 with SqlNode

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

the class SqlParserTest method testExpand.

@Test
public void testExpand() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    String testSql = "select /*%expand*/* from aaa";
    SqlParser parser = new SqlParser(testSql);
    SqlNode sqlNode = parser.parse();
    PreparedSql sql = new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED, node -> Arrays.asList("bbb", "ccc")).build(sqlNode, Function.identity());
    assertEquals("select bbb, ccc from aaa", sql.getRawSql());
    assertEquals("select bbb, ccc from aaa", sql.getFormattedSql());
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) PreparedSql(org.seasar.doma.jdbc.PreparedSql) Value(org.seasar.doma.internal.expr.Value) PhoneNumber(example.domain.PhoneNumber) ResourceUtil(org.seasar.doma.internal.util.ResourceUtil) Function(java.util.function.Function) ArrayList(java.util.ArrayList) SqlKind(org.seasar.doma.jdbc.SqlKind) Test(org.junit.jupiter.api.Test) Message(org.seasar.doma.message.Message) BigDecimal(java.math.BigDecimal) List(java.util.List) WrapException(org.seasar.doma.internal.WrapException) SqlLogType(org.seasar.doma.jdbc.SqlLogType) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MockConfig(org.seasar.doma.internal.jdbc.mock.MockConfig) Collections(java.util.Collections) JdbcException(org.seasar.doma.jdbc.JdbcException) SqlNode(org.seasar.doma.jdbc.SqlNode) 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 15 with SqlNode

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

the class SqlParserTest method testBindVariable_nullLiteral.

@Test
public void testBindVariable_nullLiteral() {
    String testSql = "select * from aaa where ename = /*name*/null";
    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)

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