Search in sources :

Example 1 with CommandContext

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

the class EmbeddedVariableNodeTest method test_accept_inScope_array_string_basic.

public void test_accept_inScope_array_string_basic() {
    // ## Arrange ##
    String sql = "in /*$pmb.memberNames*/('foo', 'bar')";
    SqlAnalyzer analyzer = new SqlAnalyzer(sql, false);
    Node rootNode = analyzer.analyze();
    MockMemberPmb pmb = new MockMemberPmb();
    pmb.setMemberNames(new String[] { "baz", "qux" });
    CommandContext ctx = createCtx(pmb);
    // ## Act ##
    rootNode.accept(ctx);
    // ## Assert ##
    log("ctx:" + ctx);
    assertEquals("in ('baz', 'qux')", ctx.getSql());
    assertEquals(0, ctx.getBindVariables().length);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 2 with CommandContext

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

the class EmbeddedVariableNodeTest method test_accept_inScope_bindSymbol.

public void test_accept_inScope_bindSymbol() {
    // ## Arrange ##
    String sql = "in /*$pmb.memberNameList*/('foo', 'bar')";
    SqlAnalyzer analyzer = new SqlAnalyzer(sql, false);
    Node rootNode = analyzer.analyze();
    MockMemberPmb pmb = new MockMemberPmb();
    pmb.setMemberNameList(DfCollectionUtil.newArrayList("baz", "q?ux"));
    CommandContext ctx = createCtx(pmb);
    // ## Act ##
    try {
        rootNode.accept(ctx);
        // ## Assert ##
        fail();
    } catch (EmbeddedVariableCommentContainsBindSymbolException e) {
        // OK
        log(e.getMessage());
    }
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) EmbeddedVariableCommentContainsBindSymbolException(org.dbflute.twowaysql.exception.EmbeddedVariableCommentContainsBindSymbolException) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 3 with CommandContext

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

the class EmbeddedVariableNodeTest method test_accept_inScope_list_string_basic.

// ===================================================================================
// InScope
// =======
public void test_accept_inScope_list_string_basic() {
    // ## Arrange ##
    String sql = "in /*$pmb.memberNameList*/('foo', 'bar')";
    SqlAnalyzer analyzer = new SqlAnalyzer(sql, false);
    Node rootNode = analyzer.analyze();
    MockMemberPmb pmb = new MockMemberPmb();
    pmb.setMemberNameList(DfCollectionUtil.newArrayList("baz", "qux"));
    CommandContext ctx = createCtx(pmb);
    // ## Act ##
    rootNode.accept(ctx);
    // ## Assert ##
    log("ctx:" + ctx);
    assertEquals("in ('baz', 'qux')", ctx.getSql());
    assertEquals(0, ctx.getBindVariables().length);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 4 with CommandContext

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

the class EmbeddedVariableNodeTest method test_accept_inScope_list_string_notQuoted.

public void test_accept_inScope_list_string_notQuoted() {
    // ## Arrange ##
    String sql = "in /*$pmb.memberNameList*/(foo, bar)";
    SqlAnalyzer analyzer = new SqlAnalyzer(sql, false);
    Node rootNode = analyzer.analyze();
    MockMemberPmb pmb = new MockMemberPmb();
    pmb.setMemberNameList(DfCollectionUtil.newArrayList("baz", "qux"));
    CommandContext ctx = createCtx(pmb);
    // ## Act ##
    rootNode.accept(ctx);
    // ## Assert ##
    log("ctx:" + ctx);
    assertEquals("in (baz, qux)", ctx.getSql());
    assertEquals(0, ctx.getBindVariables().length);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 5 with CommandContext

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

the class ForNodeTest method test_accept_nested_basic.

// ===================================================================================
// Nested
// ======
public void test_accept_nested_basic() 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(" /*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("     /*FOR pmb.memberAccountList*//*FIRST*/and (/*END*/").append(ln());
    sb.append("       /*NEXT 'or '*/MEMBER_ACCOUNT 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(), 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(" and ("));
    assertTrue(actual.contains("  MEMBER_ACCOUNT like ?"));
    assertTrue(actual.contains(" or MEMBER_ACCOUNT like ?"));
    assertTrue(Srl.count(actual, "MEMBER_ACCOUNT") == 6);
    assertTrue(actual.contains(" )"));
    assertTrue(Srl.count(actual, " )") == 4);
    assertEquals("%foo%", ctx.getBindVariables()[0]);
    assertEquals("ab%c", ctx.getBindVariables()[1]);
    assertEquals("%def", ctx.getBindVariables()[2]);
    assertEquals("%bar%", ctx.getBindVariables()[3]);
    assertEquals("ab%c", ctx.getBindVariables()[4]);
    assertEquals("%def", ctx.getBindVariables()[5]);
    assertEquals("%baz%", ctx.getBindVariables()[6]);
    assertEquals("ab%c", ctx.getBindVariables()[7]);
    assertEquals("%def", ctx.getBindVariables()[8]);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) 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