Search in sources :

Example 31 with SqlAnalyzer

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

the class ForNodeTest method test_accept_nested_bind_notInheritOption_in_FOR.

public void test_accept_nested_bind_notInheritOption_in_FOR() throws Exception {
    // ## Arrange ##
    MockPmb pmb = new MockPmb();
    pmb.setMemberId(3);
    pmb.setMemberName("qux");
    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(" /*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("     or MEMBER_ACCOUNT like /*pmb.memberName*/'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(" 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(" or MEMBER_ACCOUNT like ?"));
    assertTrue(Srl.count(actual, "MEMBER_ACCOUNT") == 3);
    assertTrue(actual.contains(" )"));
    assertEquals(3, ctx.getBindVariables()[0]);
    assertEquals("%foo%", ctx.getBindVariables()[1]);
    assertEquals("qux", ctx.getBindVariables()[2]);
    assertEquals("%bar%", ctx.getBindVariables()[3]);
    assertEquals("qux", ctx.getBindVariables()[4]);
    assertEquals("%baz%", ctx.getBindVariables()[5]);
    assertEquals("qux", ctx.getBindVariables()[6]);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 32 with SqlAnalyzer

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

the class ForNodeTest method test_accept_endNotFound.

// ===================================================================================
// Exception
// =========
public void test_accept_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 (/*END*/").append(ln());
    sb.append("     /*NEXT 'or '*/MEMBER_NAME like /*#current*/'foo%'").append(ln());
    sb.append("   /*LAST*/)/*END*/").append(ln());
    SqlAnalyzer analyzer = new SqlAnalyzer(sb.toString(), false);
    // ## Act ##
    try {
        analyzer.analyze();
        // ## Assert ##
        fail();
    } catch (EndCommentNotFoundException e) {
        // OK
        log(e.getMessage());
    }
}
Also used : LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) EndCommentNotFoundException(org.dbflute.twowaysql.exception.EndCommentNotFoundException) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 33 with SqlAnalyzer

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

the class ForNodeTest method test_accept_basic.

// ===================================================================================
// Basic
// =====
public void test_accept_basic() 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("   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("select * from MEMBER"));
    assertTrue(actual.contains(" where"));
    assertFalse(actual.contains("/*FOR "));
    assertFalse(actual.contains("/*END*/"));
    assertFalse(actual.contains("FIRST"));
    assertFalse(actual.contains("NEXT"));
    assertFalse(actual.contains("LAST"));
    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 34 with SqlAnalyzer

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

the class BeginNodeTest method test_parse_BEGIN_that_has_nested_IFIF_all_false.

public void test_parse_BEGIN_that_has_nested_IFIF_all_false() {
    // ## Arrange ##
    String sql = "/*BEGIN*/where";
    sql = sql + " ";
    sql = sql + "/*IF pmb.memberId != null*/";
    sql = sql + "AAA /*IF false*/and BBB /*IF false*/and CCC/*END*//*END*/ /*IF false*/and DDD/*END*/";
    sql = sql + "/*END*/";
    sql = sql + "/*END*/";
    SqlAnalyzer analyzer = new SqlAnalyzer(sql, false);
    // ## Act ##
    Node rootNode = analyzer.analyze();
    // ## Assert ##
    MockMemberPmb pmb = new MockMemberPmb();
    CommandContext ctx = createCtx(pmb);
    rootNode.accept(ctx);
    log("ctx:" + ctx);
    assertEquals("", ctx.getSql());
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 35 with SqlAnalyzer

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

the class BeginNodeTest method test_parse_BEGIN_that_has_nested_BEGIN_false.

public void test_parse_BEGIN_that_has_nested_BEGIN_false() {
    // ## Arrange ##
    String sql = "/*BEGIN*/where";
    sql = sql + " ";
    sql = sql + "/*IF pmb.memberName != null*/";
    sql = sql + "FIXED";
    sql = sql + "/*END*/";
    sql = sql + " ";
    sql = sql + "/*BEGIN*/";
    sql = sql + "FIXED2 /*IF false*/and BBB/*END*/ /*IF false*/and CCC/*END*/";
    sql = sql + "/*END*/";
    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);
    // Basically Unsupported!
    assertEquals("where FIXED ", ctx.getSql());
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) 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