Search in sources :

Example 1 with DfUniqueKeyFkExtractor

use of org.dbflute.logic.jdbc.metadata.supplement.DfUniqueKeyFkExtractor in project dbflute-core by dbflute.

the class DfForeignKeyExtractor method prepareUniqueKeyFkCache.

protected void prepareUniqueKeyFkCache(Connection conn, UnifiedSchema unifiedSchema) {
    // preparing unique-key FK info of the schema
    final Map<String, List<DfForeignKeyMeta>> tableMap = StringKeyMap.createAsFlexibleConcurrent();
    final DfUniqueKeyFkExtractor extractor = createUniqueKeyFkExtractor(conn, unifiedSchema);
    if (extractor == null) {
        // no need to extract in this DBMS
        _uniqueKeyFkMap.put(unifiedSchema, new ConcurrentHashMap<String, List<DfForeignKeyMeta>>());
        return;
    }
    _log.info("...Extracting unique-key FK: " + unifiedSchema);
    final Map<String, Map<String, List<UserUniqueFkColumn>>> uniqueKeyFkMap = extractor.extractUniqueKeyFkMap();
    for (Entry<String, Map<String, List<UserUniqueFkColumn>>> tableEntry : uniqueKeyFkMap.entrySet()) {
        final String tableKey = tableEntry.getKey();
        final List<DfForeignKeyMeta> metaList = new ArrayList<DfForeignKeyMeta>();
        final Map<String, List<UserUniqueFkColumn>> fkColumnListMap = tableEntry.getValue();
        for (Entry<String, List<UserUniqueFkColumn>> fkEntry : fkColumnListMap.entrySet()) {
            final List<UserUniqueFkColumn> columnList = fkEntry.getValue();
            DfForeignKeyMeta meta = null;
            for (UserUniqueFkColumn uniqueFkColumn : columnList) {
                if (meta == null) {
                    meta = new DfForeignKeyMeta();
                    meta.setForeignKeyName(uniqueFkColumn.getForeignKeyName());
                    meta.setLocalSchema(unifiedSchema);
                    meta.setLocalTablePureName(uniqueFkColumn.getLocalTableName());
                    // same schema only supported
                    meta.setForeignSchema(unifiedSchema);
                    final String foreignTableName = uniqueFkColumn.getForeignTableName();
                    meta.setForeignTablePureName(foreignTableName);
                    if (!isForeignTableGenerated(foreignTableName)) {
                        break;
                    }
                }
                meta.putColumnName(uniqueFkColumn.getLocalColumnName(), uniqueFkColumn.getForeignColumnName());
            }
            if (meta == null) {
                // basically no way
                throw new IllegalStateException("The key should have any elements: " + tableKey);
            }
            metaList.add(meta);
        }
        tableMap.put(tableKey, metaList);
    }
    _uniqueKeyFkMap.put(unifiedSchema, tableMap);
    if (!tableMap.isEmpty()) {
        _log.info(" -> Unique-key FK: " + tableMap.keySet());
    } else {
        _log.info(" -> Not found unique-key FK");
    }
}
Also used : DfUniqueKeyFkExtractor(org.dbflute.logic.jdbc.metadata.supplement.DfUniqueKeyFkExtractor) DfForeignKeyMeta(org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta) ArrayList(java.util.ArrayList) UserUniqueFkColumn(org.dbflute.logic.jdbc.metadata.supplement.DfUniqueKeyFkExtractor.UserUniqueFkColumn) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap) StringKeyMap(org.dbflute.helper.StringKeyMap)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 StringKeyMap (org.dbflute.helper.StringKeyMap)1 DfForeignKeyMeta (org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta)1 DfUniqueKeyFkExtractor (org.dbflute.logic.jdbc.metadata.supplement.DfUniqueKeyFkExtractor)1 UserUniqueFkColumn (org.dbflute.logic.jdbc.metadata.supplement.DfUniqueKeyFkExtractor.UserUniqueFkColumn)1