Search in sources :

Example 11 with CommandContext

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());
    }
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) BindVariableCommentIllegalParameterBeanSpecificationException(org.dbflute.twowaysql.exception.BindVariableCommentIllegalParameterBeanSpecificationException) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 12 with CommandContext

use of org.dbflute.twowaysql.context.CommandContext in project dbflute-core by dbflute.

the class ForNodeTest method test_accept_nested_inLoopOption_likeContain_bind_in_FOR.

public void test_accept_nested_inLoopOption_likeContain_bind_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");
        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);
    // overridden
    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:likeSuffix*/'test'").append(ln());
    sb.append("     /*FOR #current.nestPmbList*//*FIRST*/and (/*END*/").append(ln());
    sb.append("       /*NEXT 'or '*/MEMBER_NAME like /*#current.parentPmb.memberName:likeContain*/'foo%'").append(ln());
    sb.append("     /*LAST*/)/*END*//*END*/").append(ln());
    sb.append("   /*LAST*/)/*END*//*END*/").append(ln());
    sb.append(" /*END*/");
    String sql = sb.toString();
    SqlAnalyzer analyzer = new SqlAnalyzer(sql, false) {

        @Override
        protected NodeAdviceFactory getNodeAdviceFactory() {
            return new MelodicNodeAdviceFactory();
        }
    };
    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 = ? escape '|'"));
    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]);
    assertEquals("%b||ar%", ctx.getBindVariables()[4]);
    assertEquals("%b||ar%", ctx.getBindVariables()[5]);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) List(java.util.List) MelodicNodeAdviceFactory(org.dbflute.bhv.core.melodicsql.MelodicNodeAdviceFactory) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 13 with CommandContext

use of org.dbflute.twowaysql.context.CommandContext 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 14 with CommandContext

use of org.dbflute.twowaysql.context.CommandContext 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 15 with CommandContext

use of org.dbflute.twowaysql.context.CommandContext in project dbflute-core by dbflute.

the class IfNodeTest method test_parse_IF_Else_elseValid.

// ===================================================================================
// Else
// ====
public void test_parse_IF_Else_elseValid() {
    // ## Arrange ##
    String sql = "where";
    sql = sql + " /*IF pmb.memberId != null*/";
    sql = sql + " select foo";
    sql = sql + "  -- ELSE select count(*)";
    sql = sql + " /*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 select count(*) ";
    assertEquals(expected, ctx.getSql());
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Aggregations

CommandContext (org.dbflute.twowaysql.context.CommandContext)116 SqlAnalyzer (org.dbflute.twowaysql.SqlAnalyzer)99 LikeSearchOption (org.dbflute.cbean.coption.LikeSearchOption)31 Node (org.dbflute.twowaysql.node.Node)11 SqlPartsNode (org.dbflute.twowaysql.node.SqlPartsNode)9 List (java.util.List)5 ConditionBean (org.dbflute.cbean.ConditionBean)3 TnCommandContextHandler (org.dbflute.s2dao.sqlhandler.TnCommandContextHandler)3 CommandContextCreator (org.dbflute.twowaysql.context.CommandContextCreator)3 IfCommentNotFoundPropertyException (org.dbflute.twowaysql.exception.IfCommentNotFoundPropertyException)3 ArrayList (java.util.ArrayList)2 Entity (org.dbflute.Entity)2 MelodicNodeAdviceFactory (org.dbflute.bhv.core.melodicsql.MelodicNodeAdviceFactory)2 TnPropertyType (org.dbflute.s2dao.metadata.TnPropertyType)2 EmbeddedVariableCommentContainsBindSymbolException (org.dbflute.twowaysql.exception.EmbeddedVariableCommentContainsBindSymbolException)2 EmbeddedVariableNode (org.dbflute.twowaysql.node.EmbeddedVariableNode)2 TnBasicParameterHandler (org.dbflute.s2dao.sqlhandler.TnBasicParameterHandler)1 BindVariableCommentIllegalParameterBeanSpecificationException (org.dbflute.twowaysql.exception.BindVariableCommentIllegalParameterBeanSpecificationException)1 BindVariableCommentInScopeNotListException (org.dbflute.twowaysql.exception.BindVariableCommentInScopeNotListException)1 BindVariableCommentParameterNullValueException (org.dbflute.twowaysql.exception.BindVariableCommentParameterNullValueException)1