Search in sources :

Example 1 with Value

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

the class NodePreparedSqlBuilderTest method testBindVariableNode.

@Test
public void testBindVariableNode() {
    SelectClauseNode select = new SelectClauseNode("select");
    select.appendNode(OtherNode.of(" * "));
    FromClauseNode from = new FromClauseNode("from");
    from.appendNode(WhitespaceNode.of(" "));
    from.appendNode(new WordNode("aaa"));
    from.appendNode(WhitespaceNode.of(" "));
    WhereClauseNode where = new WhereClauseNode("where");
    where.appendNode(WhitespaceNode.of(" "));
    where.appendNode(new WordNode("bbb"));
    where.appendNode(OtherNode.of(" = "));
    BindVariableNode variable1 = new BindVariableNode(location, "name", "/*#name*/");
    variable1.setWordNode(new WordNode("'hoge'"));
    where.appendNode(variable1);
    where.appendNode(WhitespaceNode.of(" "));
    where.appendNode(new LogicalOperatorNode("and"));
    where.appendNode(WhitespaceNode.of(" "));
    where.appendNode(new WordNode("ccc"));
    where.appendNode(OtherNode.of(" = "));
    BindVariableNode variable2 = new BindVariableNode(location, "salary", "/*#salary*/");
    variable2.setWordNode(new WordNode("100"));
    where.appendNode(variable2);
    SelectStatementNode statement = new SelectStatementNode();
    statement.setSelectClauseNode(select);
    statement.setFromClauseNode(from);
    statement.setWhereClauseNode(where);
    AnonymousNode root = new AnonymousNode();
    root.appendNode(statement);
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, "hoge"));
    evaluator.add("salary", new Value(BigDecimal.class, new BigDecimal(100)));
    NodePreparedSqlBuilder builder = new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED);
    PreparedSql sql = builder.build(root, Function.identity());
    assertEquals("select * from aaa where bbb = ? and ccc = ?", sql.getRawSql());
    assertEquals(2, sql.getParameters().size());
    assertEquals("hoge", sql.getParameters().get(0).getWrapper().get());
    assertEquals(new BigDecimal(100), sql.getParameters().get(1).getWrapper().get());
}
Also used : FromClauseNode(org.seasar.doma.internal.jdbc.sql.node.FromClauseNode) SelectStatementNode(org.seasar.doma.internal.jdbc.sql.node.SelectStatementNode) PreparedSql(org.seasar.doma.jdbc.PreparedSql) WordNode(org.seasar.doma.internal.jdbc.sql.node.WordNode) SelectClauseNode(org.seasar.doma.internal.jdbc.sql.node.SelectClauseNode) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) BigDecimal(java.math.BigDecimal) AnonymousNode(org.seasar.doma.internal.jdbc.sql.node.AnonymousNode) LogicalOperatorNode(org.seasar.doma.internal.jdbc.sql.node.LogicalOperatorNode) Value(org.seasar.doma.internal.expr.Value) WhereClauseNode(org.seasar.doma.internal.jdbc.sql.node.WhereClauseNode) BindVariableNode(org.seasar.doma.internal.jdbc.sql.node.BindVariableNode) Test(org.junit.jupiter.api.Test)

Example 2 with Value

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

the class SqlParserTest method testParens_removeAnd.

@Test
public void testParens_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*/\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 ddd is null\n )", sql.getRawSql());
    assertEquals("select * from aaa where (\n\n ddd is null\n )", 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 3 with Value

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

the class SqlParserTest method testIf_removeWhere.

@Test
public void testIf_removeWhere() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, null));
    String testSql = "select * from aaa where /*%if name != null*/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", sql.getRawSql());
    assertEquals("select * from aaa", 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 4 with Value

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

the class SqlParserTest method testBindVariable_in_empty_iterable.

@Test
public void testBindVariable_in_empty_iterable() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(List.class, Collections.emptyList()));
    String testSql = "select * from aaa where ename in /*name*/('aaa', '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 * from aaa where ename in (null)", sql.getRawSql());
    assertEquals("select * from aaa where ename in (null)", sql.getFormattedSql());
    assertEquals(0, sql.getParameters().size());
}
Also used : PreparedSql(org.seasar.doma.jdbc.PreparedSql) 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 5 with Value

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

the class SqlParserTest method testIf_removeOrderBy.

@Test
public void testIf_removeOrderBy() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, null));
    String testSql = "select * from aaa order by /*%if name != null*/bbb/*%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) 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)

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