Search in sources :

Example 21 with Value

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

the class SqlParserTest method testFor_array.

@Test
public void testFor_array() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    String[] array = new String[] { "aaa", "bbb", "ccc" };
    evaluator.add("names", new Value(String[].class, array));
    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) 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 22 with Value

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

the class SqlParserTest method testElseBlock.

@Test
public void testElseBlock() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, "hoge"));
    String testSql = "select * from aaa where /*%if name == null*/bbb is null\n/*%elseif name == \"\"*/\n/*%else*/ 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 Value

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

the class JdbcExceptionTest method testCollectionOfBindValueContainsNull.

@Test
public void testCollectionOfBindValueContainsNull() {
    SqlParser parser = new SqlParser("select * from aaa where bbb in /*bbb*/(1,2,3)");
    SqlNode sqlNode = parser.parse();
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("bbb", new Value(List.class, Arrays.asList(1, null)));
    NodePreparedSqlBuilder builder = new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED);
    try {
        builder.build(sqlNode, Function.identity());
        fail();
    } catch (JdbcException e) {
        System.out.println(e.getMessage());
        assertEquals(Message.DOMA2115, e.getMessageResource());
    }
}
Also used : SqlParser(org.seasar.doma.internal.jdbc.sql.SqlParser) Value(org.seasar.doma.internal.expr.Value) List(java.util.List) NodePreparedSqlBuilder(org.seasar.doma.internal.jdbc.sql.NodePreparedSqlBuilder) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) Test(org.junit.jupiter.api.Test)

Example 24 with Value

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

the class StandardPagingTransformerTest method testOffsetLimit_forNode.

@Test
public void testOffsetLimit_forNode() {
    String expected = "select * from ( select temp_.*, row_number() over( order by temp_.name1, temp_.name2, temp_.id ) as doma_rownumber_ from ( select emp.id from emp ) as temp_ ) as temp2_ where doma_rownumber_ > 5 and doma_rownumber_ <= 15";
    StandardPagingTransformer transformer = new StandardPagingTransformer(5, 10);
    SqlParser parser = new SqlParser("select emp.id from emp order by /*%for e: values*/emp.name/*#e*/, /*%end*/emp.id");
    SqlNode sqlNode = transformer.transform(parser.parse());
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("values", new Value(List.class, Arrays.asList(1, 2)));
    NodePreparedSqlBuilder sqlBuilder = new NodePreparedSqlBuilder(new MockConfig(), SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED);
    PreparedSql sql = sqlBuilder.build(sqlNode, Function.identity());
    assertEquals(expected, sql.getRawSql());
}
Also used : SqlParser(org.seasar.doma.internal.jdbc.sql.SqlParser) Value(org.seasar.doma.internal.expr.Value) List(java.util.List) NodePreparedSqlBuilder(org.seasar.doma.internal.jdbc.sql.NodePreparedSqlBuilder) MockConfig(org.seasar.doma.internal.jdbc.mock.MockConfig) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) Test(org.junit.jupiter.api.Test)

Example 25 with Value

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

the class SqlModifyQuery method addParameter.

public void addParameter(String name, Class<?> type, Object value) {
    assertNotNull(name, type);
    parameters.put(name, new Value(type, value));
}
Also used : Value(org.seasar.doma.internal.expr.Value)

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