Search in sources :

Example 1 with LikeSearchOption

use of org.dbflute.cbean.coption.LikeSearchOption in project dbflute-core by dbflute.

the class AbstractConditionQuery method doRegisterLikeSearchQueryCompoundOptimized.

protected void doRegisterLikeSearchQueryCompoundOptimized(String value, ConditionValue cvalue, String columnDbName, LikeSearchOption option) {
    if (!option.isLikePrefix()) {
        String msg = "This optimization is only for LikePrefix: " + option;
        throw new IllegalStateException(msg);
    }
    // *char type only but no checked (cannot check)
    final List<SpecifiedColumn> compoundColumnList = option.getCompoundColumnList();
    final List<Integer> sizeList = option.getCompoundColumnSizeList();
    String currentValue = value;
    int currentLength = value.length();
    String currentColumn = columnDbName;
    final boolean needsAndPart = isOrScopeQueryDirectlyUnder();
    if (needsAndPart) {
        xgetSqlClause().beginOrScopeQueryAndPart();
    }
    try {
        boolean shortLengthBreak = false;
        final Iterator<SpecifiedColumn> compoundColumnIterator = compoundColumnList.iterator();
        for (Integer columnSize : sizeList) {
            // should be less or equal column count (checked in option)
            if (currentLength >= columnSize) {
                // can treat current condition as equal
                final String equalValue = currentValue.substring(0, columnSize);
                invokeQueryEqual(currentColumn, equalValue);
                currentValue = currentValue.substring(columnSize);
                currentLength = currentValue.length();
                final SpecifiedColumn specifiedColumn;
                if (compoundColumnIterator.hasNext()) {
                    specifiedColumn = compoundColumnIterator.next();
                    currentColumn = specifiedColumn.getColumnDbName();
                } else {
                    // means just size
                    // means end
                    currentColumn = null;
                    // though basically no need to break because of size loop end
                    break;
                }
            } else {
                // short length condition value
                shortLengthBreak = true;
                break;
            }
        }
        if (currentValue.length() > 0 && currentColumn != null) {
            // double check
            final LikeSearchOption copyOption = option.createDeepCopy();
            // also fixed sizes cleared
            copyOption.clearCompoundColumn();
            if (!shortLengthBreak) {
                while (compoundColumnIterator.hasNext()) {
                    copyOption.addCompoundColumn(compoundColumnIterator.next());
                }
            }
            invokeQueryLikeSearch(currentColumn, currentValue, copyOption);
        }
    } finally {
        if (needsAndPart) {
            xgetSqlClause().endOrScopeQueryAndPart();
        }
    }
}
Also used : LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) SpecifiedColumn(org.dbflute.cbean.dream.SpecifiedColumn)

Example 2 with LikeSearchOption

use of org.dbflute.cbean.coption.LikeSearchOption 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)

Example 3 with LikeSearchOption

use of org.dbflute.cbean.coption.LikeSearchOption in project dbflute-core by dbflute.

the class ForNodeTest method test_accept_nested_BEGIN_in_loop.

public void test_accept_nested_BEGIN_in_loop() 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(" /*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("     /*BEGIN*/where").append(ln());
    sb.append("     /*IF false*/").append(ln());
    sb.append("     MEMBER_NAME like /*#current*/'foo%'").append(ln());
    sb.append("     /*END*/").append(ln());
    sb.append("     /*IF true*/").append(ln());
    sb.append("     or MEMBER_ACCOUNT like /*#current*/'foo%'").append(ln());
    sb.append("     /*END*/").append(ln());
    sb.append("     /*IF true*/").append(ln());
    sb.append("     or MEMBER_ACCOUNT like /*#current*/'foo%'").append(ln());
    sb.append("     /*END*/").append(ln());
    sb.append("     /*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(" MEMBER_ACCOUNT like ? escape '|'"));
    assertTrue(Srl.count(actual, "MEMBER_NAME") == 0);
    assertTrue(Srl.count(actual, "MEMBER_ACCOUNT") == 6);
    assertEquals(6, ctx.getBindVariables().length);
    assertEquals("foo%", ctx.getBindVariables()[0]);
    assertEquals("foo%", ctx.getBindVariables()[1]);
    assertEquals("bar%", ctx.getBindVariables()[2]);
    assertEquals("bar%", ctx.getBindVariables()[3]);
    assertEquals("baz%", ctx.getBindVariables()[4]);
    assertEquals("baz%", ctx.getBindVariables()[5]);
}
Also used : CommandContext(org.dbflute.twowaysql.context.CommandContext) LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 4 with LikeSearchOption

use of org.dbflute.cbean.coption.LikeSearchOption in project dbflute-core by dbflute.

the class ForNodeTest method test_accept_currentParameter_in_Bind_null_notAllowed.

public void test_accept_currentParameter_in_Bind_null_notAllowed() 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(), true);
    Node rootNode = analyzer.analyze();
    CommandContext ctx = createCtx(pmb);
    // ## Act ##
    try {
        rootNode.accept(ctx);
        // ## Assert ##
        fail();
    } catch (ForCommentParameterNullElementException e) {
        // OK
        log(e.getMessage());
    }
}
Also used : ForCommentParameterNullElementException(org.dbflute.twowaysql.exception.ForCommentParameterNullElementException) CommandContext(org.dbflute.twowaysql.context.CommandContext) LikeSearchOption(org.dbflute.cbean.coption.LikeSearchOption) SqlAnalyzer(org.dbflute.twowaysql.SqlAnalyzer)

Example 5 with LikeSearchOption

use of org.dbflute.cbean.coption.LikeSearchOption in project dbflute-core by dbflute.

the class ForNodeTest method test_accept_emptyList.

public void test_accept_emptyList() throws Exception {
    // ## Arrange ##
    MockPmb pmb = new MockPmb();
    List<String> emptyList = DfCollectionUtil.emptyList();
    pmb.setMemberNameList(emptyList);
    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

LikeSearchOption (org.dbflute.cbean.coption.LikeSearchOption)37 SqlAnalyzer (org.dbflute.twowaysql.SqlAnalyzer)33 CommandContext (org.dbflute.twowaysql.context.CommandContext)31 List (java.util.List)5 EndCommentNotFoundException (org.dbflute.twowaysql.exception.EndCommentNotFoundException)2 MelodicNodeAdviceFactory (org.dbflute.bhv.core.melodicsql.MelodicNodeAdviceFactory)1 SpecifiedColumn (org.dbflute.cbean.dream.SpecifiedColumn)1 DBWay (org.dbflute.dbway.DBWay)1 BindVariableCommentIllegalParameterBeanSpecificationException (org.dbflute.twowaysql.exception.BindVariableCommentIllegalParameterBeanSpecificationException)1 ForCommentParameterNullElementException (org.dbflute.twowaysql.exception.ForCommentParameterNullElementException)1 LoopCurrentVariableOutOfForCommentException (org.dbflute.twowaysql.exception.LoopCurrentVariableOutOfForCommentException)1