Search in sources :

Example 11 with Value

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

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

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

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

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

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