use of org.dbflute.twowaysql.SqlAnalyzer in project dbflute-core by dbflute.
the class ForNodeTest method test_accept_nullElement_in_IF.
public void test_accept_nullElement_in_IF() throws Exception {
// ## Arrange ##
MockPmb pmb = new MockPmb();
pmb.setMemberNameList(DfCollectionUtil.newArrayList("foo", null, "baz"));
pmb.setMemberNameListInternalLikeSearchOption(new LikeSearchOption().likePrefix());
StringBuilder sb = new StringBuilder();
sb.append("select * from MEMBER");
sb.append(" /*BEGIN*/where");
sb.append(" /*IF pmb.memberId != null*/");
sb.append(" MEMBER_ID = /*pmb.memberId*/");
sb.append(" /*END*/");
sb.append(" /*FOR pmb.memberNameList*//*FIRST*/and (/*END*/");
sb.append(" /*IF #current != null*/");
sb.append(" /*NEXT 'or '*/MEMBER_NAME like /*#current*/'foo%'");
sb.append(" /*END*/");
sb.append(" /*LAST*/)/*END*//*END*/");
sb.append(" /*END*/");
SqlAnalyzer analyzer = new SqlAnalyzer(sb.toString(), true);
Node rootNode = analyzer.analyze();
CommandContext ctx = createCtx(pmb);
// ## Act ##
rootNode.accept(ctx);
// ## Assert ##
String actual = ctx.getSql();
log(ln() + actual);
assertTrue(actual.contains("select * from MEMBER "));
assertTrue(actual.contains("where"));
assertTrue(actual.contains(" ("));
assertTrue(actual.contains(" MEMBER_NAME like ?"));
assertTrue(actual.contains(" or MEMBER_NAME like ?"));
assertTrue(Srl.count(actual, "MEMBER_NAME") == 2);
assertTrue(actual.contains(" )"));
assertEquals(2, ctx.getBindVariables().length);
assertEquals("foo%", ctx.getBindVariables()[0]);
assertEquals("baz%", ctx.getBindVariables()[1]);
}
use of org.dbflute.twowaysql.SqlAnalyzer in project dbflute-core by dbflute.
the class ForNodeTest method test_accept_BEGIN_connectorAdjustment_go.
public void test_accept_BEGIN_connectorAdjustment_go() 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*/").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 ##
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.SqlAnalyzer in project dbflute-core by dbflute.
the class ForNodeTest method test_accept_nested_bind_overrideOption_in_FOR.
public void test_accept_nested_bind_overrideOption_in_FOR() throws Exception {
// ## Arrange ##
MockPmb pmb = new MockPmb();
// unused
pmb.setMemberName("parent");
List<MockPmb> nestPmbList = DfCollectionUtil.newArrayList();
{
MockPmb element = new MockPmb();
element.setMemberId(3);
element.setMemberName("fo%o");
element.setMemberStatusCode("FML");
List<MockPmb> nestNestPmbList = DfCollectionUtil.newArrayList();
{
MockPmb nestElement = new MockPmb();
nestElement.setMemberName("baz");
nestElement.setParentPmb(element);
nestNestPmbList.add(nestElement);
}
{
MockPmb nestElement = new MockPmb();
nestElement.setMemberName("baz");
nestElement.setParentPmb(element);
nestNestPmbList.add(nestElement);
}
element.setNestPmbList(nestNestPmbList);
nestPmbList.add(element);
}
{
MockPmb element = new MockPmb();
element.setMemberId(4);
element.setMemberName("b|ar");
// override
element.setMemberNameInternalLikeSearchOption(new LikeSearchOption().likeContain());
element.setMemberStatusCode("WDL");
List<MockPmb> nestNestPmbList = DfCollectionUtil.newArrayList();
{
MockPmb nestElement = new MockPmb();
nestElement.setMemberName("qux");
nestElement.setParentPmb(element);
nestNestPmbList.add(nestElement);
}
{
MockPmb nestElement = new MockPmb();
nestElement.setMemberName("qux");
nestElement.setParentPmb(element);
nestNestPmbList.add(nestElement);
}
element.setNestPmbList(nestNestPmbList);
nestPmbList.add(element);
}
pmb.setNestPmbList(nestPmbList);
pmb.setNestPmbListInternalLikeSearchOption(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.nestPmbList*//*FIRST*/and (/*END*/").append(ln());
sb.append(" /*NEXT 'or '*/MEMBER_STATUS_CODE = /*#current.memberStatusCode:notLike*/'test'").append(ln());
sb.append(" /*FOR #current.nestPmbList*//*FIRST*/and (/*END*/").append(ln());
sb.append(" /*NEXT 'or '*/MEMBER_NAME like /*#current.parentPmb.memberName*/'foo%'").append(ln());
sb.append(" /*LAST*/)/*END*//*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(" ("));
assertTrue(actual.contains(" MEMBER_STATUS_CODE = ?\n"));
assertTrue(actual.contains(" MEMBER_NAME like ? escape '|'"));
assertTrue(actual.contains(" or MEMBER_NAME like ? escape '|'"));
assertTrue(Srl.count(actual, "MEMBER_NAME") == 4);
assertTrue(actual.contains(" and ("));
assertTrue(Srl.count(actual, " and (") == 2);
assertTrue(actual.contains(" )"));
assertTrue(Srl.count(actual, " )") == 3);
assertEquals("FML", ctx.getBindVariables()[0]);
assertEquals("fo|%o%", ctx.getBindVariables()[1]);
assertEquals("fo|%o%", ctx.getBindVariables()[2]);
assertEquals("WDL", ctx.getBindVariables()[3]);
// override
assertEquals("%b||ar%", ctx.getBindVariables()[4]);
// override
assertEquals("%b||ar%", ctx.getBindVariables()[5]);
}
use of org.dbflute.twowaysql.SqlAnalyzer in project dbflute-core by dbflute.
the class ForNodeTest method test_accept_FIRST_endNotFound.
public void test_accept_FIRST_endNotFound() throws Exception {
// ## Arrange ##
MockPmb pmb = new MockPmb();
pmb.setMemberNameList(DfCollectionUtil.newArrayList("foo", "bar", "baz"));
pmb.setMemberNameListInternalLikeSearchOption(new LikeSearchOption().likeContain());
pmb.setMemberAccountList(DfCollectionUtil.newArrayList("ab%c", "%def"));
StringBuilder sb = new StringBuilder();
sb.append("select * from MEMBER").append(ln());
sb.append(" /*FOR pmb.memberNameList*//*FIRST*/and (").append(ln());
sb.append(" /*NEXT 'or '*/MEMBER_NAME like /*#current*/'foo%'").append(ln());
sb.append(" /*LAST*/)/*END*//*END*/").append(ln());
SqlAnalyzer analyzer = new SqlAnalyzer(sb.toString(), false);
// ## Act ##
try {
analyzer.analyze();
// ## Assert ##
fail();
} catch (EndCommentNotFoundException e) {
// OK
log(e.getMessage());
}
}
use of org.dbflute.twowaysql.SqlAnalyzer in project dbflute-core by dbflute.
the class ForNodeTest method test_accept_nested_current_in_FOR.
public void test_accept_nested_current_in_FOR() throws Exception {
// ## Arrange ##
MockPmb pmb = new MockPmb();
List<MockPmb> nestPmbList = DfCollectionUtil.newArrayList();
{
MockPmb element = new MockPmb();
element.setMemberId(3);
element.setMemberNameList(DfCollectionUtil.newArrayList("fo%o", "b|ar"));
element.setMemberNameListInternalLikeSearchOption(new LikeSearchOption().likePrefix());
nestPmbList.add(element);
}
{
MockPmb element = new MockPmb();
element.setMemberId(4);
element.setMemberNameList(DfCollectionUtil.newArrayList("ba%z", "qu_x"));
// element.setMemberNameListInternalLikeSearchOption(new LikeSearchOption().likePrefix());
nestPmbList.add(element);
}
pmb.setNestPmbList(nestPmbList);
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.nestPmbList*//*FIRST*/and (/*END*/").append(ln());
sb.append(" /*NEXT 'or '*/MEMBER_ID = /*#current.memberId*/99").append(ln());
sb.append(" /*FOR #current.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(" /*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(" ("));
assertTrue(actual.contains(" MEMBER_ID = ?"));
assertTrue(actual.contains(" MEMBER_ID = ?"));
assertTrue(actual.contains(" MEMBER_NAME like ?"));
assertTrue(actual.contains(" or MEMBER_NAME like ?"));
assertTrue(Srl.count(actual, "MEMBER_NAME") == 4);
assertTrue(actual.contains(" and ("));
assertTrue(Srl.count(actual, " and (") == 2);
assertTrue(actual.contains(" )"));
assertTrue(Srl.count(actual, " )") == 3);
assertEquals(3, ctx.getBindVariables()[0]);
assertEquals("fo|%o%", ctx.getBindVariables()[1]);
assertEquals("b||ar%", ctx.getBindVariables()[2]);
assertEquals(4, ctx.getBindVariables()[3]);
assertEquals("ba%z", ctx.getBindVariables()[4]);
assertEquals("qu_x", ctx.getBindVariables()[5]);
}
Aggregations