Search in sources :

Example 21 with LikeSearchOption

use of org.dbflute.cbean.coption.LikeSearchOption in project dbflute-core by dbflute.

the class ForNodeTest method test_accept_BEGIN_connectorAdjustment_not.

public void test_accept_BEGIN_connectorAdjustment_not() 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("   /*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_ID = ?"));
    assertTrue(actual.contains(" and MEMBER_NAME like ? escape '|'"));
    assertTrue(Srl.count(actual, "MEMBER_NAME") == 3);
    assertEquals(3, ctx.getBindVariables()[0]);
    assertEquals("foo%", ctx.getBindVariables()[1]);
    assertEquals("bar%", ctx.getBindVariables()[2]);
    assertEquals("baz%", ctx.getBindVariables()[3]);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 22 with LikeSearchOption

use of org.dbflute.cbean.coption.LikeSearchOption in project dbflute-core by dbflute.

the class ForNodeTest method test_accept_currentParameter_in_Bind_null_allowed.

public void test_accept_currentParameter_in_Bind_null_allowed() throws Exception {
    // ## Arrange ##
    MockPmb pmb = new MockPmb();
    pmb.setMemberId(3);
    pmb.setMemberNameList(DfCollectionUtil.newArrayList("foo", null, "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 ##
    // expect no exception
    rootNode.accept(ctx);
    // ## Assert ##
    String actual = ctx.getSql();
    log(ln() + actual);
    assertTrue(Srl.count(actual, "and MEMBER_NAME like ? escape '|' ") == 2);
    assertTrue(Srl.count(actual, "and MEMBER_NAME like ?\n") == 1);
    assertEquals(4, ctx.getBindVariables().length);
    assertEquals(3, ctx.getBindVariables()[0]);
    assertEquals("foo%", ctx.getBindVariables()[1]);
    assertEquals(null, ctx.getBindVariables()[2]);
    assertEquals("baz%", ctx.getBindVariables()[3]);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 23 with LikeSearchOption

use of org.dbflute.cbean.coption.LikeSearchOption in project dbflute-core by dbflute.

the class ForNodeTest method test_accept_NextAnd.

public void test_accept_NextAnd() throws Exception {
    // ## Arrange ##
    MockPmb pmb = new MockPmb();
    pmb.setMemberNameList(DfCollectionUtil.newArrayList("foo", "bar", "baz"));
    pmb.setMemberNameListInternalLikeSearchOption(new LikeSearchOption().likeSuffix());
    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("   /*NEXT 'and '*/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]);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 24 with LikeSearchOption

use of org.dbflute.cbean.coption.LikeSearchOption in project dbflute-core by dbflute.

the class ForNodeTest method test_accept_nested_current_in_FOR_nullElement.

public void test_accept_nested_current_in_FOR_nullElement() 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);
    }
    nestPmbList.add(null);
    {
        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("     /*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(), true);
    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_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("fo|%o%", ctx.getBindVariables()[0]);
    assertEquals("b||ar%", ctx.getBindVariables()[1]);
    assertEquals("ba%z", ctx.getBindVariables()[2]);
    assertEquals("qu_x", ctx.getBindVariables()[3]);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 25 with LikeSearchOption

use of org.dbflute.cbean.coption.LikeSearchOption in project dbflute-core by dbflute.

the class ForNodeTest method test_accept_currentParameter_outOfForComment.

public void test_accept_currentParameter_outOfForComment() 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(" /*#current*/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 ##
    try {
        rootNode.accept(ctx);
        // ## Assert ##
        fail();
    } catch (LoopCurrentVariableOutOfForCommentException e) {
        // OK
        log(e.getMessage());
    }
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) LoopCurrentVariableOutOfForCommentException(org.dbflute.twowaysql.exception.LoopCurrentVariableOutOfForCommentException) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Aggregations

LikeSearchOption (org.dbflute.cbean.coption.LikeSearchOption)37 SqlAnalyzer (org.dbflute.twowaysql.SqlAnalyzer)33 CommandContext (org.dbflute.twowaysql.context.CommandContext)31 List (java.util.List)5 EndCommentNotFoundException (org.dbflute.twowaysql.exception.EndCommentNotFoundException)2 MelodicNodeAdviceFactory (org.dbflute.bhv.core.melodicsql.MelodicNodeAdviceFactory)1 SpecifiedColumn (org.dbflute.cbean.dream.SpecifiedColumn)1 DBWay (org.dbflute.dbway.DBWay)1 BindVariableCommentIllegalParameterBeanSpecificationException (org.dbflute.twowaysql.exception.BindVariableCommentIllegalParameterBeanSpecificationException)1 ForCommentParameterNullElementException (org.dbflute.twowaysql.exception.ForCommentParameterNullElementException)1 LoopCurrentVariableOutOfForCommentException (org.dbflute.twowaysql.exception.LoopCurrentVariableOutOfForCommentException)1