Search in sources :

Example 1 with ExpressionEvaluator

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

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

the class NodePreparedSqlBuilderTest method testAndNode.

@Test
public void testAndNode() {
    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");
    IfNode ifNode1 = new IfNode(location, "true", "/*if true*/");
    ifNode1.appendNode(WhitespaceNode.of(" "));
    ifNode1.appendNode(new WordNode("bbb"));
    ifNode1.appendNode(OtherNode.of(" = "));
    ifNode1.appendNode(new WordNode("ccc"));
    EndNode endNode1 = new EndNode("/*end*/");
    IfBlockNode ifBlock1 = new IfBlockNode();
    ifBlock1.setIfNode(ifNode1);
    ifBlock1.setEndNode(endNode1);
    where.appendNode(ifBlock1);
    IfNode ifNode2 = new IfNode(location, "true", "/*if true*/");
    ifNode2.appendNode(WhitespaceNode.of(" "));
    LogicalOperatorNode and = new LogicalOperatorNode("and");
    ifNode2.appendNode(and);
    and.appendNode(WhitespaceNode.of(" "));
    and.appendNode(new WordNode("ddd"));
    and.appendNode(OtherNode.of(" = "));
    and.appendNode(new WordNode("eee"));
    EndNode endNode2 = new EndNode("/*end*/");
    IfBlockNode ifBlock2 = new IfBlockNode();
    ifBlock2.setIfNode(ifNode2);
    ifBlock2.setEndNode(endNode2);
    where.appendNode(ifBlock2);
    SelectStatementNode statement = new SelectStatementNode();
    statement.setSelectClauseNode(select);
    statement.setFromClauseNode(from);
    statement.setWhereClauseNode(where);
    AnonymousNode root = new AnonymousNode();
    root.appendNode(statement);
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    NodePreparedSqlBuilder builder = new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED);
    PreparedSql sql = builder.build(statement, Function.identity());
    assertEquals("select * from aaa where bbb = ccc and ddd = eee", sql.getRawSql());
}
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) IfNode(org.seasar.doma.internal.jdbc.sql.node.IfNode) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) AnonymousNode(org.seasar.doma.internal.jdbc.sql.node.AnonymousNode) EndNode(org.seasar.doma.internal.jdbc.sql.node.EndNode) LogicalOperatorNode(org.seasar.doma.internal.jdbc.sql.node.LogicalOperatorNode) IfBlockNode(org.seasar.doma.internal.jdbc.sql.node.IfBlockNode) WhereClauseNode(org.seasar.doma.internal.jdbc.sql.node.WhereClauseNode) Test(org.junit.jupiter.api.Test)

Example 3 with ExpressionEvaluator

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

the class NodePreparedSqlBuilderTest method testIfNode_false.

@Test
public void testIfNode_false() {
    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(" "));
    IfNode ifNode = new IfNode(location, "false", "/*if false*/");
    ifNode.appendNode(new WordNode("bbb"));
    ifNode.appendNode(OtherNode.of(" = "));
    ifNode.appendNode(new WordNode("ccc"));
    IfBlockNode ifBlockNode = new IfBlockNode();
    ifBlockNode.setIfNode(ifNode);
    ifBlockNode.setEndNode(new EndNode("/*end*/"));
    where.appendNode(ifBlockNode);
    SelectStatementNode statement = new SelectStatementNode();
    statement.setSelectClauseNode(select);
    statement.setFromClauseNode(from);
    statement.setWhereClauseNode(where);
    AnonymousNode root = new AnonymousNode();
    root.appendNode(statement);
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    NodePreparedSqlBuilder builder = new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED);
    PreparedSql sql = builder.build(statement, Function.identity());
    assertEquals("select * from aaa", sql.getRawSql());
}
Also used : AnonymousNode(org.seasar.doma.internal.jdbc.sql.node.AnonymousNode) 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) EndNode(org.seasar.doma.internal.jdbc.sql.node.EndNode) IfBlockNode(org.seasar.doma.internal.jdbc.sql.node.IfBlockNode) SelectClauseNode(org.seasar.doma.internal.jdbc.sql.node.SelectClauseNode) IfNode(org.seasar.doma.internal.jdbc.sql.node.IfNode) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) WhereClauseNode(org.seasar.doma.internal.jdbc.sql.node.WhereClauseNode) Test(org.junit.jupiter.api.Test)

Example 4 with ExpressionEvaluator

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

the class NodePreparedSqlBuilderTest method testElseNode.

@Test
public void testElseNode() {
    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(" "));
    IfNode ifNode = new IfNode(location, "false", "/*if false*/");
    ifNode.appendNode(new WordNode("bbb"));
    ifNode.appendNode(OtherNode.of(" = "));
    ifNode.appendNode(new WordNode("ccc"));
    ElseNode elseNode = new ElseNode("/*else*/");
    elseNode.appendNode(new WordNode("ddd"));
    elseNode.appendNode(OtherNode.of(" = "));
    elseNode.appendNode(new WordNode("eee"));
    EndNode endNode = new EndNode("/*end*/");
    IfBlockNode ifBlock = new IfBlockNode();
    ifBlock.setIfNode(ifNode);
    ifBlock.setElseNode(elseNode);
    ifBlock.setEndNode(endNode);
    where.appendNode(ifBlock);
    SelectStatementNode statement = new SelectStatementNode();
    statement.setSelectClauseNode(select);
    statement.setFromClauseNode(from);
    statement.setWhereClauseNode(where);
    AnonymousNode root = new AnonymousNode();
    root.appendNode(statement);
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    NodePreparedSqlBuilder builder = new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED);
    PreparedSql sql = builder.build(statement, Function.identity());
    assertEquals("select * from aaa where ddd = eee", sql.getRawSql());
}
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) IfNode(org.seasar.doma.internal.jdbc.sql.node.IfNode) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) AnonymousNode(org.seasar.doma.internal.jdbc.sql.node.AnonymousNode) EndNode(org.seasar.doma.internal.jdbc.sql.node.EndNode) ElseNode(org.seasar.doma.internal.jdbc.sql.node.ElseNode) IfBlockNode(org.seasar.doma.internal.jdbc.sql.node.IfBlockNode) WhereClauseNode(org.seasar.doma.internal.jdbc.sql.node.WhereClauseNode) Test(org.junit.jupiter.api.Test)

Example 5 with ExpressionEvaluator

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

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