Search in sources :

Example 86 with SqlAnalyzer

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

the class EmbeddedVariableNodeTest method test_analyze_dynamicBinding.

// ===================================================================================
// Dynamic Binding
// ===============
public void test_analyze_dynamicBinding() {
    // ## Arrange ##
    String sql = "/*BEGIN*/where";
    sql = sql + " /*IF pmb.memberId != null*/member.MEMBER_ID /*$pmb.memberName*//*END*/";
    sql = sql + "/*END*/";
    SqlAnalyzer analyzer = new SqlAnalyzer(sql, false);
    // ## Act ##
    Node rootNode = analyzer.analyze();
    // ## Assert ##
    MockMemberPmb pmb = new MockMemberPmb();
    pmb.setMemberId(12);
    pmb.setMemberName("= /*pmb.memberId*/99");
    CommandContext ctx = createCtx(pmb);
    rootNode.accept(ctx);
    log("ctx:" + ctx);
    String expected = "where member.MEMBER_ID = ?";
    assertEquals(expected, ctx.getSql());
    assertEquals(1, ctx.getBindVariables().length);
    assertEquals(12, ctx.getBindVariables()[0]);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 87 with SqlAnalyzer

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

the class EmbeddedVariableNodeTest method test_accept_number_quoted.

public void test_accept_number_quoted() {
    // ## Arrange ##
    String sql = "= /*$pmb.memberId*/'8'";
    SqlAnalyzer analyzer = new SqlAnalyzer(sql, false);
    Node rootNode = analyzer.analyze();
    MockMemberPmb pmb = new MockMemberPmb();
    pmb.setMemberId(3);
    CommandContext ctx = createCtx(pmb);
    // ## Act ##
    rootNode.accept(ctx);
    // ## Assert ##
    log("ctx:" + ctx);
    assertEquals("= '3'", ctx.getSql());
    assertEquals(0, ctx.getBindVariables().length);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 88 with SqlAnalyzer

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

the class EmbeddedVariableNodeTest method test_accept_inScope_list_integer_basic.

public void test_accept_inScope_list_integer_basic() {
    // ## Arrange ##
    String sql = "in /*$pmb.memberIdList*/(56, 88)";
    SqlAnalyzer analyzer = new SqlAnalyzer(sql, false);
    Node rootNode = analyzer.analyze();
    MockMemberPmb pmb = new MockMemberPmb();
    pmb.setMemberIdList(DfCollectionUtil.newArrayList(123, 456));
    CommandContext ctx = createCtx(pmb);
    // ## Act ##
    rootNode.accept(ctx);
    // ## Assert ##
    log("ctx:" + ctx);
    assertEquals("in (123, 456)", ctx.getSql());
    assertEquals(0, ctx.getBindVariables().length);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 89 with SqlAnalyzer

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

the class ForNodeTest method test_accept_loopVariable_in_IF.

public void test_accept_loopVariable_in_IF() throws Exception {
    // ## Arrange ##
    MockPmb pmb = new MockPmb();
    pmb.setMemberNameList(DfCollectionUtil.newArrayList("foo", "bar", "baz"));
    pmb.setMemberNameListInternalLikeSearchOption(new LikeSearchOption().likeContain());
    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*//*IF true*//*FIRST*/and (/*END*//*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("  ("));
    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 90 with SqlAnalyzer

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

the class ForNodeTest method test_accept_nullList.

public void test_accept_nullList() throws Exception {
    // ## Arrange ##
    MockPmb pmb = new MockPmb();
    pmb.setMemberNameList(null);
    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("select * from MEMBER "));
    assertFalse(actual.contains("where"));
    assertFalse(actual.contains("  ("));
    assertFalse(actual.contains("  MEMBER_NAME like ?"));
    assertFalse(actual.contains(" or MEMBER_NAME like ?"));
    assertTrue(Srl.count(actual, "MEMBER_NAME") == 0);
    assertFalse(actual.contains(" )"));
    assertEquals(0, ctx.getBindVariables().length);
}
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