use of org.dbflute.twowaysql.context.CommandContext in project dbflute-core by dbflute.
the class ForNodeTest method test_accept_FOR_spellMiss.
public void test_accept_FOR_spellMiss() throws Exception {
// ## Arrange ##
MockPmb pmb = new MockPmb();
pmb.setMemberId(3);
pmb.setMemberNameList(DfCollectionUtil.newArrayList("foo", "bar", "baz"));
pmb.setMemberNameListInternalLikeSearchOption(new LikeSearchOption().likePrefix());
StringBuilder sb = new StringBuilder();
sb.append("select * from MEMBER").append(ln());
sb.append(" /*BEGIN*/where").append(ln());
sb.append(" /*IF pmb.memberId != null*/").append(ln());
sb.append(" MEMBER_ID = /*pmb.memberId*/").append(ln());
sb.append(" /*END*/").append(ln());
sb.append(" /*FAR pmb.memberNameList*/").append(ln());
sb.append(" and MEMBER_NAME like /*#current*/'foo%'").append(ln());
sb.append(" /*END*/").append(ln());
sb.append(" /*END*/");
SqlAnalyzer analyzer = new SqlAnalyzer(sb.toString(), false);
Node rootNode = analyzer.analyze();
CommandContext ctx = createCtx(pmb);
// ## Act ##
try {
rootNode.accept(ctx);
// ## Assert ##
fail();
} catch (BindVariableCommentIllegalParameterBeanSpecificationException e) {
// OK
log(e.getMessage());
}
}
use of org.dbflute.twowaysql.context.CommandContext in project dbflute-core by dbflute.
the class ForNodeTest method test_accept_LastAnd.
public void test_accept_LastAnd() throws Exception {
// ## Arrange ##
MockPmb pmb = new MockPmb();
pmb.setMemberNameList(DfCollectionUtil.newArrayList("foo", "bar", "baz"));
pmb.setMemberNameListInternalLikeSearchOption(new LikeSearchOption().likePrefix());
StringBuilder sb = new StringBuilder();
sb.append("select * from MEMBER").append(ln());
sb.append(" where").append(ln());
sb.append(" /*FOR pmb.memberNameList*/").append(ln());
sb.append(" /*LAST*/and /*END*/MEMBER_NAME like /*#current*/'foo%'").append(ln());
sb.append(" /*END*/");
SqlAnalyzer analyzer = new SqlAnalyzer(sb.toString(), false);
Node rootNode = analyzer.analyze();
CommandContext ctx = createCtx(pmb);
// ## Act ##
rootNode.accept(ctx);
// ## Assert ##
String actual = ctx.getSql();
log(ln() + actual);
assertTrue(actual.contains(" MEMBER_NAME like ? escape '|'"));
assertTrue(actual.contains(" and MEMBER_NAME like ? escape '|'"));
assertTrue(Srl.count(actual, "MEMBER_NAME") == 3);
assertEquals("foo%", ctx.getBindVariables()[0]);
assertEquals("bar%", ctx.getBindVariables()[1]);
assertEquals("baz%", ctx.getBindVariables()[2]);
}
use of org.dbflute.twowaysql.context.CommandContext in project dbflute-core by dbflute.
the class ForNodeTest method test_accept_allStars_all_true.
public void test_accept_allStars_all_true() throws Exception {
// ## Arrange ##
MockPmb pmb = new MockPmb();
pmb.setMemberId(4);
pmb.setMemberNameList(DfCollectionUtil.newArrayList("fo|o", "ba%r", "b_a|z"));
pmb.setMemberNameListInternalLikeSearchOption(new LikeSearchOption().likePrefix());
StringBuilder sb = new StringBuilder();
sb.append("select * from MEMBER").append(ln());
sb.append(" /*BEGIN*/where").append(ln());
sb.append(" /*IF pmb.memberId != null*/").append(ln());
sb.append(" MEMBER_ID = /*pmb.memberId*/").append(ln());
sb.append(" /*END*/").append(ln());
sb.append(" /*FOR pmb.memberNameList*//*FIRST*/and (/*END*/").append(ln());
sb.append(" /*NEXT 'or '*/MEMBER_NAME like /*#current*/'foo%'").append(ln());
sb.append(" /*LAST*/)/*END*//*END*/").append(ln());
sb.append(" /*END*/");
SqlAnalyzer analyzer = new SqlAnalyzer(sb.toString(), false);
Node rootNode = analyzer.analyze();
CommandContext ctx = createCtx(pmb);
// ## Act ##
rootNode.accept(ctx);
// ## Assert ##
String actual = ctx.getSql();
log(ln() + actual);
assertTrue(actual.contains(" MEMBER_ID = ?"));
assertTrue(actual.contains(" and ("));
assertTrue(actual.contains(" MEMBER_NAME like ? escape '|'"));
assertTrue(actual.contains(" or MEMBER_NAME like ? escape '|'"));
assertTrue(Srl.count(actual, "MEMBER_NAME") == 3);
assertTrue(actual.contains(" )"));
assertEquals(4, ctx.getBindVariables()[0]);
assertEquals("fo||o%", ctx.getBindVariables()[1]);
assertEquals("ba|%r%", ctx.getBindVariables()[2]);
assertEquals("b|_a||z%", ctx.getBindVariables()[3]);
}
use of org.dbflute.twowaysql.context.CommandContext in project dbflute-core by dbflute.
the class ForNodeTest method test_accept_nested_BEGIN_in_loop.
public void test_accept_nested_BEGIN_in_loop() throws Exception {
// ## Arrange ##
MockPmb pmb = new MockPmb();
pmb.setMemberNameList(DfCollectionUtil.newArrayList("foo", "bar", "baz"));
pmb.setMemberNameListInternalLikeSearchOption(new LikeSearchOption().likePrefix());
StringBuilder sb = new StringBuilder();
sb.append("select * from MEMBER").append(ln());
sb.append(" /*BEGIN*/where").append(ln());
sb.append(" /*IF pmb.memberId != null*/").append(ln());
sb.append(" MEMBER_ID = /*pmb.memberId*/").append(ln());
sb.append(" /*END*/").append(ln());
sb.append(" /*FOR pmb.memberNameList*//*FIRST*/and (/*END*/").append(ln());
sb.append(" /*BEGIN*/where").append(ln());
sb.append(" /*IF false*/").append(ln());
sb.append(" MEMBER_NAME like /*#current*/'foo%'").append(ln());
sb.append(" /*END*/").append(ln());
sb.append(" /*IF true*/").append(ln());
sb.append(" or MEMBER_ACCOUNT like /*#current*/'foo%'").append(ln());
sb.append(" /*END*/").append(ln());
sb.append(" /*IF true*/").append(ln());
sb.append(" or MEMBER_ACCOUNT like /*#current*/'foo%'").append(ln());
sb.append(" /*END*/").append(ln());
sb.append(" /*END*/").append(ln());
sb.append(" /*LAST*/)/*END*//*END*/").append(ln());
sb.append(" /*END*/");
SqlAnalyzer analyzer = new SqlAnalyzer(sb.toString(), false);
Node rootNode = analyzer.analyze();
CommandContext ctx = createCtx(pmb);
// ## Act ##
rootNode.accept(ctx);
// ## Assert ##
String actual = ctx.getSql();
log(ln() + actual);
assertTrue(actual.contains(" MEMBER_ACCOUNT like ? escape '|'"));
assertTrue(Srl.count(actual, "MEMBER_NAME") == 0);
assertTrue(Srl.count(actual, "MEMBER_ACCOUNT") == 6);
assertEquals(6, ctx.getBindVariables().length);
assertEquals("foo%", ctx.getBindVariables()[0]);
assertEquals("foo%", ctx.getBindVariables()[1]);
assertEquals("bar%", ctx.getBindVariables()[2]);
assertEquals("bar%", ctx.getBindVariables()[3]);
assertEquals("baz%", ctx.getBindVariables()[4]);
assertEquals("baz%", ctx.getBindVariables()[5]);
}
use of org.dbflute.twowaysql.context.CommandContext in project dbflute-core by dbflute.
the class IfNodeTest method test_parse_IF_for_where_one_true.
public void test_parse_IF_for_where_one_true() {
// ## Arrange ##
String sql = "where";
sql = sql + " /*IF pmb.memberId != null*/member.MEMBER_ID = 3/*END*/";
sql = sql + " /*IF pmb.memberName != null*/and member.MEMBER_NAME = 'TEST'/*END*/";
SqlAnalyzer analyzer = new SqlAnalyzer(sql, false);
// ## Act ##
Node rootNode = analyzer.analyze();
// ## Assert ##
MockMemberPmb pmb = new MockMemberPmb();
pmb.setMemberName("foo");
CommandContext ctx = createCtx(pmb);
rootNode.accept(ctx);
log("ctx:" + ctx);
String expected = "where and member.MEMBER_NAME = 'TEST'";
assertEquals(expected, ctx.getSql());
}
Aggregations