Search in sources :

Example 96 with SqlNode

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

the class SqlParserTest method testManyEol.

@Test
public void testManyEol() throws WrapException {
    String path = "META-INF/" + getClass().getName().replace('.', '/') + "/manyEol.sql";
    String sql = ResourceUtil.getResourceAsString(path);
    SqlParser parser = new SqlParser(sql);
    SqlNode sqlNode = parser.parse();
    assertNotNull(sqlNode);
}
Also used : SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 97 with SqlNode

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

the class NodePreparedSqlBuilderTest method testWhere_embeddedVariable_orderBy.

@Test
public void testWhere_embeddedVariable_orderBy() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("embedded", new Value(String.class, "order by bbb"));
    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   order by bbb", 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 98 with SqlNode

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

the class NodePreparedSqlBuilderTest method testEmbeddedVariable_containsSemicolon.

@Test
public void testEmbeddedVariable_containsSemicolon() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, "hoge"));
    evaluator.add("salary", new Value(BigDecimal.class, new BigDecimal(10000)));
    evaluator.add("orderBy", new Value(String.class, "aaa;bbb"));
    String testSql = "select * from aaa where ename = /*name*/'aaa' and sal = /*salary*/-2000 /*#orderBy*/";
    SqlParser parser = new SqlParser(testSql);
    SqlNode sqlNode = parser.parse();
    try {
        new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED).build(sqlNode, Function.identity());
        fail();
    } catch (JdbcException expected) {
        System.out.println(expected.getMessage());
        assertEquals(Message.DOMA2117, expected.getMessageResource());
    }
}
Also used : Value(org.seasar.doma.internal.expr.Value) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) JdbcException(org.seasar.doma.jdbc.JdbcException) BigDecimal(java.math.BigDecimal) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 99 with SqlNode

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

the class SqlParserTest method testExpand_alias.

@Test
public void testExpand_alias() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    String testSql = "select /*%expand \"a\"*/* from aaa a";
    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 a.bbb, a.ccc from aaa a", sql.getRawSql());
    assertEquals("select a.bbb, a.ccc from aaa a", 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 100 with SqlNode

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

the class SqlParserTest method testBindVariable_enum.

@Test
public void testBindVariable_enum() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(MyEnum.class, MyEnum.BBB));
    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 = ? and sal = ?", sql.getRawSql());
    assertEquals("select * from aaa where ename = 'BBB' and sal = 10000", sql.getFormattedSql());
    assertEquals(2, sql.getParameters().size());
    assertEquals(MyEnum.BBB, sql.getParameters().get(0).getWrapper().get());
    assertEquals(new BigDecimal(10000), 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) BigDecimal(java.math.BigDecimal) 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