Search in sources :

Example 1 with BindVariableNode

use of org.seasar.doma.internal.jdbc.sql.node.BindVariableNode 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 BindVariableNode

use of org.seasar.doma.internal.jdbc.sql.node.BindVariableNode in project doma by domaframework.

the class SqlParser method parseBindVariableBlockComment.

protected void parseBindVariableBlockComment() {
    String variableName = tokenType.extract(token);
    if (variableName.isEmpty()) {
        throw new JdbcException(Message.DOMA2120, sql, tokenizer.getLineNumber(), tokenizer.getPosition(), token);
    }
    BindVariableNode node = new BindVariableNode(getLocation(), variableName, token);
    appendNode(node);
    push(node);
}
Also used : JdbcException(org.seasar.doma.jdbc.JdbcException) BindVariableNode(org.seasar.doma.internal.jdbc.sql.node.BindVariableNode)

Aggregations

BindVariableNode (org.seasar.doma.internal.jdbc.sql.node.BindVariableNode)2 BigDecimal (java.math.BigDecimal)1 Test (org.junit.jupiter.api.Test)1 ExpressionEvaluator (org.seasar.doma.internal.expr.ExpressionEvaluator)1 Value (org.seasar.doma.internal.expr.Value)1 AnonymousNode (org.seasar.doma.internal.jdbc.sql.node.AnonymousNode)1 FromClauseNode (org.seasar.doma.internal.jdbc.sql.node.FromClauseNode)1 LogicalOperatorNode (org.seasar.doma.internal.jdbc.sql.node.LogicalOperatorNode)1 SelectClauseNode (org.seasar.doma.internal.jdbc.sql.node.SelectClauseNode)1 SelectStatementNode (org.seasar.doma.internal.jdbc.sql.node.SelectStatementNode)1 WhereClauseNode (org.seasar.doma.internal.jdbc.sql.node.WhereClauseNode)1 WordNode (org.seasar.doma.internal.jdbc.sql.node.WordNode)1 JdbcException (org.seasar.doma.jdbc.JdbcException)1 PreparedSql (org.seasar.doma.jdbc.PreparedSql)1