Search in sources :

Example 11 with ForeignKey

use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.

the class DfTableOrderAnalyzer method doAnalyzeOrder.

/**
 * @param tableList The list of table, which may be registered. (NotNull)
 * @param alreadyRegisteredSet The (pure) name set of already registered table. (NotNull)
 * @param outputOrderedList The ordered list of table for output. (NotNull)
 * @return The list of unregistered table. (NotNull)
 */
protected List<Table> doAnalyzeOrder(List<Table> tableList, Set<String> alreadyRegisteredSet, List<List<Table>> outputOrderedList, final int level) {
    final List<Table> unregisteredTableList = new ArrayList<Table>();
    final List<Table> elementList = new ArrayList<Table>();
    for (Table table : tableList) {
        final List<ForeignKey> foreignKeyList = table.getForeignKeyList();
        boolean dependsOnAny = false;
        for (ForeignKey fk : foreignKeyList) {
            final String foreignTablePureName = fk.getForeignTablePureName();
            if (level >= 1 && fk.hasFixedCondition()) {
                // from first level, ignore fixed condition
                continue;
            }
            if (level >= 2 && fk.isAdditionalForeignKey()) {
                // from second level, ignore additional FK
                continue;
            }
            if (!fk.isSelfReference() && !alreadyRegisteredSet.contains(foreignTablePureName)) {
                // found non-registered parent table so it still depends on any
                dependsOnAny = true;
                break;
            }
        }
        if (dependsOnAny) {
            unregisteredTableList.add(table);
        } else {
            elementList.add(table);
            // pure name here
            alreadyRegisteredSet.add(table.getName());
        }
    }
    if (!elementList.isEmpty()) {
        outputOrderedList.add(elementList);
    }
    return unregisteredTableList;
}
Also used : Table(org.apache.torque.engine.database.model.Table) ArrayList(java.util.ArrayList) ForeignKey(org.apache.torque.engine.database.model.ForeignKey)

Example 12 with ForeignKey

use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.

the class DfLReverseTableOrder method doAnalyzeOrder.

/**
 * @param tableList The list of table, which may be registered. (NotNull)
 * @param alreadyRegisteredSet The (pure) name set of already registered table. (NotNull)
 * @param outputOrderedList The ordered list of table for output. (NotNull)
 * @return The list of unregistered table. (NotNull)
 */
protected List<Table> doAnalyzeOrder(List<Table> tableList, Set<String> alreadyRegisteredSet, List<List<Table>> outputOrderedList, final int level) {
    final List<Table> unregisteredTableList = new ArrayList<Table>();
    final List<Table> elementList = new ArrayList<Table>();
    for (Table table : tableList) {
        final List<ForeignKey> foreignKeyList = table.getForeignKeyList();
        boolean dependsOnAny = false;
        for (ForeignKey fk : foreignKeyList) {
            final String foreignTablePureName = fk.getForeignTablePureName();
            if (level >= 1 && fk.hasFixedCondition()) {
                // from first level, ignore fixed condition
                continue;
            }
            if (level >= 2 && fk.isAdditionalForeignKey()) {
                // from second level, ignore additional FK
                continue;
            }
            if (!fk.isSelfReference() && !alreadyRegisteredSet.contains(foreignTablePureName)) {
                // found non-registered parent table so it still depends on any
                dependsOnAny = true;
                break;
            }
        }
        if (dependsOnAny) {
            unregisteredTableList.add(table);
        } else {
            elementList.add(table);
            // pure name here
            alreadyRegisteredSet.add(table.getName());
        }
    }
    if (!elementList.isEmpty()) {
        outputOrderedList.add(elementList);
    }
    return unregisteredTableList;
}
Also used : Table(org.apache.torque.engine.database.model.Table) ArrayList(java.util.ArrayList) ForeignKey(org.apache.torque.engine.database.model.ForeignKey)

Example 13 with ForeignKey

use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.

the class DfAdditionalForeignKeyInitializer method processAllTableFK.

protected void processAllTableFK(String foreignKeyName, String foreignTableName, List<String> foreignColumnNameList, DfAdditionalForeignKeyOption option) {
    if (option.isFixedOnlyJoin()) {
        String msg = "Cannot use fixedOnlyJoin when all-table FK: " + foreignKeyName;
        throw new DfIllegalPropertySettingException(msg);
    }
    // for check about same-column self reference
    final Table foreignTable = getTable(foreignTableName);
    final StringSet foreignColumnSet = StringSet.createAsFlexible();
    foreignColumnSet.addAll(foreignColumnNameList);
    for (Table table : getTableList()) {
        final String localTableName = table.getTableDbName();
        final List<String> localColumnNameList = getLocalColumnNameList(table, foreignKeyName, foreignTableName, foreignColumnNameList, localTableName, option, true, false);
        if (!table.containsColumn(localColumnNameList)) {
            continue;
        }
        // check same-column self reference
        final StringSet localColumnSet = StringSet.createAsFlexible();
        localColumnSet.addAll(localColumnNameList);
        final boolean selfReference = table.getTableDbName().equals(foreignTable.getTableDbName());
        if (selfReference && localColumnSet.equalsUnderCharOption(foreignColumnSet)) {
            continue;
        }
        // check same foreign key existence
        final String fixedSuffix = option.getFixedSuffix();
        final ForeignKey existingFK = table.findExistingForeignKey(foreignTableName, localColumnNameList, foreignColumnNameList, fixedSuffix);
        if (existingFK != null) {
            _log.info("The foreign key has already set up: " + foreignKeyName + "(" + fixedSuffix + ")");
            reflectOptionToExistingFKIfNeeds(foreignKeyName, option, existingFK);
            continue;
        }
        final String currentForeignKeyName = foreignKeyName + "_" + toConstraintPart(localTableName);
        setupForeignKeyToTable(currentForeignKeyName, foreignTableName, foreignColumnNameList, table, localColumnNameList, option);
        showResult(foreignTableName, foreignColumnNameList, table, localColumnNameList, option);
    }
}
Also used : Table(org.apache.torque.engine.database.model.Table) StringSet(org.dbflute.helper.StringSet) ForeignKey(org.apache.torque.engine.database.model.ForeignKey) DfIllegalPropertySettingException(org.dbflute.exception.DfIllegalPropertySettingException)

Example 14 with ForeignKey

use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.

the class DfAdditionalForeignKeyInitializer method getLocalColumnNameList.

protected List<String> getLocalColumnNameList(Table table, String foreignKeyName, String foreignTableName, List<String> foreignColumnNameList, String localTableName, DfAdditionalForeignKeyOption option, boolean searchFromExistingFK, boolean errorIfNotFound) {
    List<String> localColumnNameList = getLocalColumnNameList(foreignKeyName);
    if (localColumnNameList != null && !localColumnNameList.isEmpty()) {
        return localColumnNameList;
    }
    // searching from existing foreign key
    if (searchFromExistingFK) {
        final ForeignKey existingFK = table.findExistingForeignKey(foreignTableName, foreignColumnNameList, option.getFixedSuffix());
        if (existingFK != null) {
            return existingFK.getLocalColumnNameList();
        }
    }
    // searching local columns by foreign columns (PK or UQ: PK when omitted)
    localColumnNameList = DfCollectionUtil.newArrayList();
    if (option.isFixedOnlyJoin()) {
        // no need to search
        return localColumnNameList;
    }
    for (String foreignColumnName : foreignColumnNameList) {
        final Column column = table.getColumn(foreignColumnName);
        if (column != null) {
            // no check about same-column self reference here
            localColumnNameList.add(column.getName());
            continue;
        }
        if (errorIfNotFound) {
            final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
            br.addNotice("Not found the local column by the foreign column of additionalForeignKey.");
            br.addItem("Advice");
            br.addElement("When localColumnName is omitted, the local table should have");
            br.addElement("the columns that are same as primary keys of foreign table.");
            br.addItem("Additional FK");
            br.addElement(foreignKeyName);
            br.addElement(option);
            br.addItem("Local Table");
            br.addElement(localTableName);
            br.addItem("Foreign Table");
            br.addElement(foreignTableName);
            br.addItem("Foreign Column");
            br.addElement(foreignColumnNameList);
            final String msg = br.buildExceptionMessage();
            throw new DfPropertySettingColumnNotFoundException(msg);
        } else {
            // means not found
            return DfCollectionUtil.newArrayList();
        }
    }
    return localColumnNameList;
}
Also used : DfPropertySettingColumnNotFoundException(org.dbflute.exception.DfPropertySettingColumnNotFoundException) Column(org.apache.torque.engine.database.model.Column) ExceptionMessageBuilder(org.dbflute.helper.message.ExceptionMessageBuilder) ForeignKey(org.apache.torque.engine.database.model.ForeignKey)

Example 15 with ForeignKey

use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.

the class DfAdditionalForeignKeyInitializer method createAdditionalForeignKey.

protected ForeignKey createAdditionalForeignKey(String foreignKeyName, String foreignTableName, List<String> localColumnNameList, List<String> foreignColumnNameList, DfAdditionalForeignKeyOption option) {
    final ForeignKey fk = new ForeignKey();
    fk.setName(foreignKeyName);
    if (foreignTableName.contains(".")) {
        final String foreignSchema = Srl.substringLastFront(foreignTableName, ".");
        final String foreignTablePureName = Srl.substringLastRear(foreignTableName, ".");
        fk.setForeignSchema(UnifiedSchema.createAsDynamicSchema(foreignSchema));
        fk.setForeignTablePureName(foreignTablePureName);
    } else {
        // main schema
        fk.setForeignSchema(getDatabase().getDatabaseSchema());
        fk.setForeignTablePureName(foreignTableName);
    }
    fk.addReference(localColumnNameList, foreignColumnNameList);
    fk.setAdditionalForeignKey(true);
    final String fixedCondition = option.getFixedCondition();
    if (Srl.is_NotNull_and_NotTrimmedEmpty(fixedCondition)) {
        fk.setFixedCondition(fixedCondition);
    }
    final String fixedSuffix = option.getFixedSuffix();
    if (Srl.is_NotNull_and_NotTrimmedEmpty(fixedSuffix)) {
        fk.setFixedSuffix(fixedSuffix);
    }
    fk.setFixedInline(option.isFixedInline());
    fk.setFixedReferrer(option.isFixedReferrer());
    fk.setFixedOnlyJoin(option.isFixedOnlyJoin());
    final String comment = option.getComment();
    if (Srl.is_NotNull_and_NotTrimmedEmpty(comment)) {
        fk.setComment(comment);
    }
    return fk;
}
Also used : ForeignKey(org.apache.torque.engine.database.model.ForeignKey)

Aggregations

ForeignKey (org.apache.torque.engine.database.model.ForeignKey)15 Table (org.apache.torque.engine.database.model.Table)9 Column (org.apache.torque.engine.database.model.Column)8 ArrayList (java.util.ArrayList)4 List (java.util.List)3 HashSet (java.util.HashSet)2 Function (java.util.function.Function)1 Index (org.apache.torque.engine.database.model.Index)1 Unique (org.apache.torque.engine.database.model.Unique)1 DfIllegalPropertySettingException (org.dbflute.exception.DfIllegalPropertySettingException)1 DfPropertySettingColumnNotFoundException (org.dbflute.exception.DfPropertySettingColumnNotFoundException)1 StringSet (org.dbflute.helper.StringSet)1 ExceptionMessageBuilder (org.dbflute.helper.message.ExceptionMessageBuilder)1 DfSPolicyStatement (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement)1 DfSPolicyIfPart (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyIfPart)1 DfSPolicyThenClause (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenClause)1 DfSPolicyThenPart (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenPart)1 DfSPolicyResult (org.dbflute.logic.doc.spolicy.result.DfSPolicyResult)1 DfSPolicyFirstDateSecretary (org.dbflute.logic.doc.spolicy.secretary.DfSPolicyFirstDateSecretary)1 DfSPolicyLogicalSecretary (org.dbflute.logic.doc.spolicy.secretary.DfSPolicyLogicalSecretary)1