Search in sources :

Example 1 with IndexOfInfo

use of org.dbflute.util.Srl.IndexOfInfo in project dbflute-core by dbflute.

the class DfStringUtilTest method test_indexOfFirst_basic.

// ===================================================================================
// IndexOf
// =======
public void test_indexOfFirst_basic() {
    assertEquals(3, indexOfFirst("foo.bar/baz.qux", ".", "/").getIndex());
    assertEquals(3, indexOfFirst("foo/bar.baz/qux", ".", "/").getIndex());
    IndexOfInfo info = indexOfFirst("foo.bar/baz.qux", ".", "/");
    assertEquals(4, info.getRearIndex());
    assertEquals("foo", info.substringFront());
    assertEquals("bar/baz.qux", info.substringRear());
    assertNull(indexOfFirst("foo.bar/baz.qux", "O", "A"));
}
Also used : IndexOfInfo(org.dbflute.util.Srl.IndexOfInfo)

Example 2 with IndexOfInfo

use of org.dbflute.util.Srl.IndexOfInfo in project dbflute-core by dbflute.

the class DfSql2EntityTask method getPrimaryKeyMap.

protected StringKeyMap<String> getPrimaryKeyMap(DfCustomizeEntityInfo entityInfo) {
    final StringKeyMap<String> pkMap = StringKeyMap.createAsFlexibleOrdered();
    final List<String> pkList = entityInfo.getPrimaryKeyList();
    if (pkList == null || pkList.isEmpty()) {
        return pkMap;
    }
    for (String pk : pkList) {
        if (Srl.contains(pk, ".")) {
            final IndexOfInfo info = Srl.indexOfFirst(pk, ".");
            String tableName = info.substringFrontTrimmed();
            String pkName = info.substringRearTrimmed();
            pkMap.put(pkName, tableName);
        } else {
            // no specified related table
            pkMap.put(pk, null);
        }
    }
    return pkMap;
}
Also used : IndexOfInfo(org.dbflute.util.Srl.IndexOfInfo)

Example 3 with IndexOfInfo

use of org.dbflute.util.Srl.IndexOfInfo in project dbflute-core by dbflute.

the class HpFixedConditionQueryResolver method resolveOverRelation.

// ===================================================================================
// Resolve Over Relation
// =====================
protected String resolveOverRelation(String fixedCondition, boolean fixedInline) {
    // analyze:
    // - "$$over($localTable.memberSecurity)$$.REMINDER_QUESTION"
    // - "$$over($foreignTable.memberStatus, DISPLAY_ORDER)$$.ORDER_NO"
    // - "$$over(PURCHASE.product.productStatus)$$.PRODUCT_STATUS_NAME"
    final String relationBeginMark = getRelationBeginMark();
    final String relationEndMark = getRelationEndMark();
    String resolvedClause = fixedCondition;
    String remainder = resolvedClause;
    while (true) {
        // "|$$over(|$localTable.memberSecurity)$$.REMINDER_QUESTION"
        final IndexOfInfo relationBeginIndex = Srl.indexOfFirst(remainder, relationBeginMark);
        if (relationBeginIndex == null) {
            break;
        }
        remainder = relationBeginIndex.substringRear();
        // "$localTable.memberSecurity|)$$|.REMINDER_QUESTION"
        final IndexOfInfo relationEndIndex = Srl.indexOfFirst(remainder, relationEndMark);
        if (relationEndIndex == null) {
            break;
        }
        // remainder is e.g. "$localTable.memberSecurity)$$" now
        // e.g. "$localTable.memberSecurity" or "$foreignTable.memberStatus, DISPLAY_ORDER"
        final String relationExp = relationEndIndex.substringFront();
        // e.g. "$$over($localTable.memberSecurity)$$" or "$$over($foreignTable.memberStatus, DISPLAY_ORDER)$$"
        final String relationVariable = relationBeginMark + relationExp + relationEndMark;
        // e.g. "$localTable" or "$foreignTable" or "PURCHASE"
        final String pointTable;
        // e.g. "memberSecurity" or "product.productStatus" or null (means base only)
        final String targetRelation;
        // e.g. DISPLAY_ORDER or null (means no argument)
        final String secondArg;
        {
            final IndexOfInfo separatorIndex = Srl.indexOfFirst(relationExp, ".");
            if (separatorIndex != null) {
                // normally here
                // e.g. $localTable
                pointTable = separatorIndex.substringFrontTrimmed();
                final String separatorRear = separatorIndex.substringRearTrimmed();
                final IndexOfInfo argIndex = Srl.indexOfFirst(separatorRear, ",");
                targetRelation = argIndex != null ? argIndex.substringFrontTrimmed() : separatorRear;
                secondArg = argIndex != null ? argIndex.substringRearTrimmed() : null;
            } else {
                // e.g. "$$over(PURCHASE)$$"
                final IndexOfInfo argIndex = Srl.indexOfFirst(relationExp, ",");
                pointTable = argIndex != null ? argIndex.substringFrontTrimmed() : Srl.trim(relationExp);
                targetRelation = null;
                secondArg = argIndex != null ? argIndex.substringRearTrimmed() : null;
            }
        }
        final ConditionQuery relationPointCQ;
        final ConditionQuery columnTargetCQ;
        if (Srl.equalsPlain(pointTable, getLocalTableMark())) {
            // local table
            relationPointCQ = _localCQ;
            if (targetRelation != null) {
                columnTargetCQ = invokeColumnTargetCQ(relationPointCQ, targetRelation);
            } else {
                String notice = "The relation on fixed condition is required if the table is not referrer.";
                throwIllegalFixedConditionOverRelationException(notice, pointTable, null, fixedCondition);
                // unreachable
                return null;
            }
        } else if (Srl.equalsPlain(pointTable, getForeignTableMark())) {
            // foreign table
            relationPointCQ = _foreignCQ;
            columnTargetCQ = relationPointCQ;
            if (targetRelation == null) {
                String notice = "The relation on fixed condition is required if the table is not referrer.";
                throwIllegalFixedConditionOverRelationException(notice, pointTable, null, fixedCondition);
                // unreachable
                return null;
            }
            // prepare fixed InlineView
            if (_inlineViewResourceMap == null) {
                _inlineViewResourceMap = new LinkedHashMap<String, InlineViewResource>();
            }
            final InlineViewResource resource;
            if (_inlineViewResourceMap.containsKey(targetRelation)) {
                resource = _inlineViewResourceMap.get(targetRelation);
            } else {
                resource = new InlineViewResource();
                _inlineViewResourceMap.put(targetRelation, resource);
            }
            final String columnName;
            {
                // e.g. "$$over($localTable.memberSecurity)$$|.|REMINDER_QUESTION = ..."
                final IndexOfInfo rearIndex = Srl.indexOfFirst(relationEndIndex.substringRearTrimmed(), ".");
                if (rearIndex == null || rearIndex.getIndex() > 0) {
                    String notice = "The OverRelation variable should continue to column after the variable.";
                    throwIllegalFixedConditionOverRelationException(notice, pointTable, targetRelation, fixedCondition);
                    // unreachable
                    return null;
                }
                // e.g. REMINDER_QUESTION = ...
                final String columnStart = rearIndex.substringRear();
                final IndexOfInfo indexInfo = Srl.indexOfFirst(columnStart, " ", ",", ")", "\n", "\t");
                // REMINDER_QUESTION
                columnName = indexInfo != null ? indexInfo.substringFront() : columnStart;
            }
            // the secondArg should be a column DB name, and then rear column is alias name
            final String resolvedColumn = secondArg != null ? secondArg + " as " + columnName : columnName;
            // selected in in-line view
            resource.addAdditionalColumn(resolvedColumn);
            if (!resource.hasJoinInfo()) {
                // first analyze
                final List<String> traceList = Srl.splitList(targetRelation, ".");
                DBMeta currentDBMeta = _dbmetaProvider.provideDBMeta(_foreignCQ.asTableDbName());
                for (String trace : traceList) {
                    final ForeignInfo foreignInfo = currentDBMeta.findForeignInfo(trace);
                    resource.addJoinInfo(foreignInfo);
                    currentDBMeta = foreignInfo.getForeignDBMeta();
                }
            }
            final List<ForeignInfo> joinInfoList = resource.getJoinInfoList();
            if (!joinInfoList.isEmpty()) {
                // basically true (but just in case)
                final ForeignInfo latestForeignInfo = joinInfoList.get(joinInfoList.size() - 1);
                resource.addOptimizedVariable(relationVariable, latestForeignInfo);
            }
        } else {
            // referrer table
            final DBMeta pointDBMeta;
            try {
                pointDBMeta = _dbmetaProvider.provideDBMeta(pointTable);
            } catch (DBMetaNotFoundException e) {
                String notice = "The table for relation on fixed condition does not exist.";
                throwIllegalFixedConditionOverRelationException(notice, pointTable, targetRelation, fixedCondition, e);
                // unreachable
                return null;
            }
            ConditionQuery referrerQuery = _localCQ.xgetReferrerQuery();
            while (true) {
                if (referrerQuery == null) {
                    // means not found
                    break;
                }
                if (Srl.equalsPlain(pointDBMeta.getTableDbName(), referrerQuery.asTableDbName())) {
                    break;
                }
                referrerQuery = referrerQuery.xgetReferrerQuery();
            }
            relationPointCQ = referrerQuery;
            if (relationPointCQ == null) {
                String notice = "The table for relation on fixed condition was not found in the scope.";
                throwIllegalFixedConditionOverRelationException(notice, pointTable, targetRelation, fixedCondition);
                // unreachable
                return null;
            }
            if (targetRelation != null) {
                columnTargetCQ = invokeColumnTargetCQ(relationPointCQ, targetRelation);
            } else {
                columnTargetCQ = relationPointCQ;
            }
        }
        // resolve over-relation variables in clause
        // e.g. "dfrel_4"
        final String relationAlias = columnTargetCQ.xgetAliasName();
        resolvedClause = replaceString(resolvedClause, relationVariable, relationAlias);
        // after case for loop
        remainder = relationEndIndex.substringRear();
    // no replace even if same relation because of additional column
    // // to prevent from processing same one
    // remainder = replaceString(remainder, relationVariable, relationAlias);
    }
    resolvedClause = adjustOptimizedLine(resolvedClause);
    return resolvedClause;
}
Also used : ForeignInfo(org.dbflute.dbmeta.info.ForeignInfo) DBMeta(org.dbflute.dbmeta.DBMeta) IndexOfInfo(org.dbflute.util.Srl.IndexOfInfo) ConditionQuery(org.dbflute.cbean.ConditionQuery) ArrayList(java.util.ArrayList) List(java.util.List) DBMetaNotFoundException(org.dbflute.exception.DBMetaNotFoundException) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

IndexOfInfo (org.dbflute.util.Srl.IndexOfInfo)3 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 ConditionQuery (org.dbflute.cbean.ConditionQuery)1 DBMeta (org.dbflute.dbmeta.DBMeta)1 ForeignInfo (org.dbflute.dbmeta.info.ForeignInfo)1 DBMetaNotFoundException (org.dbflute.exception.DBMetaNotFoundException)1