use of org.seasar.doma.internal.jdbc.sql.node.IfBlockNode 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());
}
use of org.seasar.doma.internal.jdbc.sql.node.IfBlockNode 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());
}
use of org.seasar.doma.internal.jdbc.sql.node.IfBlockNode 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());
}
use of org.seasar.doma.internal.jdbc.sql.node.IfBlockNode in project doma by domaframework.
the class SqlParser method parseIfBlockComment.
protected void parseIfBlockComment() {
IfBlockNode ifBlockNode = new IfBlockNode();
appendNode(ifBlockNode);
push(ifBlockNode);
String expression = tokenType.extract(token);
IfNode ifNode = new IfNode(getLocation(), expression, token);
ifBlockNode.setIfNode(ifNode);
push(ifNode);
}
use of org.seasar.doma.internal.jdbc.sql.node.IfBlockNode in project doma by domaframework.
the class SqlParser method parseElseifBlockComment.
protected void parseElseifBlockComment() {
if (!isInIfBlockNode()) {
throw new JdbcException(Message.DOMA2138, sql, tokenizer.getLineNumber(), tokenizer.getPosition());
}
removeNodesTo(IfBlockNode.class);
IfBlockNode ifBlockNode = peek();
if (ifBlockNode.isElseNodeExistent()) {
throw new JdbcException(Message.DOMA2139, sql, tokenizer.getLineNumber(), tokenizer.getPosition());
}
String expression = tokenType.extract(token);
ElseifNode node = new ElseifNode(getLocation(), expression, token);
ifBlockNode.addElseifNode(node);
push(node);
}
Aggregations