Search in sources :

Example 1 with SqlAnalyzer

use of org.dbflute.twowaysql.SqlAnalyzer in project dbflute-core by dbflute.

the class DfParameterAutoDetectProcess method processAutoDetect.

// ===================================================================================
// Auto Detect
// ===========
public void processAutoDetect(String sql, Map<String, String> propertyNameTypeMap, Map<String, String> propertyNameOptionMap, Set<String> autoDetectedPropertyNameSet) {
    final SqlAnalyzer analyzer = new SqlAnalyzer(sql, false);
    final Node rootNode = analyzer.analyze();
    doProcessAutoDetect(sql, propertyNameTypeMap, propertyNameOptionMap, autoDetectedPropertyNameSet, rootNode);
}
Also used : IfNode(org.dbflute.twowaysql.node.IfNode) BindVariableNode(org.dbflute.twowaysql.node.BindVariableNode) Node(org.dbflute.twowaysql.node.Node) ForNode(org.dbflute.twowaysql.node.ForNode) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 2 with SqlAnalyzer

use of org.dbflute.twowaysql.SqlAnalyzer in project dbflute-core by dbflute.

the class ForNodeTest method test_accept_nested_notLike_embedded_in_FOR.

public void test_accept_nested_notLike_embedded_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);
    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 = 'FML'\n"));
    assertTrue(actual.contains("  MEMBER_NAME like 'fo|%o%' escape '|'"));
    assertTrue(actual.contains(" or MEMBER_NAME like 'b||ar%' 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(0, ctx.getBindVariables().length);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) List(java.util.List) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 3 with SqlAnalyzer

use of org.dbflute.twowaysql.SqlAnalyzer in project dbflute-core by dbflute.

the class ForNodeTest method test_accept_allStars_connectorAdjustment_noLn.

public void test_accept_allStars_connectorAdjustment_noLn() 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");
    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("     /*NEXT 'or '*/MEMBER_NAME like /*#current*/'foo%'");
    sb.append("   /*LAST*/)/*END*//*END*/");
    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_NAME like ? escape '|'"));
    assertTrue(actual.contains(" or MEMBER_NAME like ? escape '|'"));
    assertTrue(Srl.count(actual, "MEMBER_NAME") == 3);
    assertTrue(actual.contains(" )"));
    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 4 with SqlAnalyzer

use of org.dbflute.twowaysql.SqlAnalyzer in project dbflute-core by dbflute.

the class ForNodeTest method test_accept_nested_inLoopOption_notLike_bind_in_FOR.

public void test_accept_nested_inLoopOption_notLike_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);
    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]);
    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) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 5 with SqlAnalyzer

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

Aggregations

SqlAnalyzer (org.dbflute.twowaysql.SqlAnalyzer)107 CommandContext (org.dbflute.twowaysql.context.CommandContext)99 LikeSearchOption (org.dbflute.cbean.coption.LikeSearchOption)33 List (java.util.List)5 EndCommentNotFoundException (org.dbflute.twowaysql.exception.EndCommentNotFoundException)3 IfCommentNotFoundPropertyException (org.dbflute.twowaysql.exception.IfCommentNotFoundPropertyException)3 MelodicNodeAdviceFactory (org.dbflute.bhv.core.melodicsql.MelodicNodeAdviceFactory)2 EmbeddedVariableCommentContainsBindSymbolException (org.dbflute.twowaysql.exception.EmbeddedVariableCommentContainsBindSymbolException)2 CommentTerminatorNotFoundException (org.dbflute.exception.CommentTerminatorNotFoundException)1 CommandContextCreator (org.dbflute.twowaysql.context.CommandContextCreator)1 BindVariableCommentIllegalParameterBeanSpecificationException (org.dbflute.twowaysql.exception.BindVariableCommentIllegalParameterBeanSpecificationException)1 BindVariableCommentInScopeNotListException (org.dbflute.twowaysql.exception.BindVariableCommentInScopeNotListException)1 BindVariableCommentParameterNullValueException (org.dbflute.twowaysql.exception.BindVariableCommentParameterNullValueException)1 EmbeddedVariableCommentInScopeNotListException (org.dbflute.twowaysql.exception.EmbeddedVariableCommentInScopeNotListException)1 EmbeddedVariableCommentParameterNullValueException (org.dbflute.twowaysql.exception.EmbeddedVariableCommentParameterNullValueException)1 ForCommentParameterNullElementException (org.dbflute.twowaysql.exception.ForCommentParameterNullElementException)1 IfCommentConditionEmptyException (org.dbflute.twowaysql.exception.IfCommentConditionEmptyException)1 InLoopOptionOutOfLoopException (org.dbflute.twowaysql.exception.InLoopOptionOutOfLoopException)1 LoopCurrentVariableOutOfForCommentException (org.dbflute.twowaysql.exception.LoopCurrentVariableOutOfForCommentException)1 SqlAnalyzerFactory (org.dbflute.twowaysql.factory.SqlAnalyzerFactory)1