Search in sources :

Example 16 with ExpressionEvaluator

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

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

the class SqlParserTest method testBindVariable_endsWithBindVariableComment.

@Test
public void testBindVariable_endsWithBindVariableComment() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, "hoge"));
    String testSql = "select * from aaa where ename = /*name*/";
    SqlParser parser = new SqlParser(testSql);
    try {
        parser.parse();
        fail();
    } catch (JdbcException expected) {
        System.out.println(expected.getMessage());
        assertEquals(Message.DOMA2110, expected.getMessageResource());
    }
}
Also used : Value(org.seasar.doma.internal.expr.Value) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) JdbcException(org.seasar.doma.jdbc.JdbcException) Test(org.junit.jupiter.api.Test)

Example 18 with ExpressionEvaluator

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

the class SqlParserTest method testFor_removeOrderBy.

@Test
public void testFor_removeOrderBy() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    ArrayList<String> list = new ArrayList<>();
    evaluator.add("names", new Value(List.class, list));
    String testSql = "select * from aaa order by /*%for n : names*/name = /*n*/'a' /*%if n_has_next */, /*%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)

Example 19 with ExpressionEvaluator

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

the class NodePreparedSqlBuilderTest method testWhere_embeddedVariable_orderBy_followedByForUpdate.

@Test
public void testWhere_embeddedVariable_orderBy_followedByForUpdate() {
    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*/ for update";
    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 for update", 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 20 with ExpressionEvaluator

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

the class NodePreparedSqlBuilderTest method testEmbeddedVariable_lineComment.

@Test
public void testEmbeddedVariable_lineComment() {
    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.DOMA2122, 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)

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