Search in sources :

Example 1 with DfForeignKeyMeta

use of org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta in project dbflute-core by dbflute.

the class DfForeignKeyExtractorTest method test_immobilizeOrder_basic.

public void test_immobilizeOrder_basic() throws Exception {
    // ## Arrange ##
    DfForeignKeyExtractor extractor = new DfForeignKeyExtractor();
    Map<String, DfForeignKeyMeta> fkMap = newLinkedHashMap();
    {
        DfForeignKeyMeta meta = new DfForeignKeyMeta();
        meta.putColumnNameAll(newLinkedHashMap("b", "1", "c", "2"));
        fkMap.put("foo", meta);
    }
    {
        DfForeignKeyMeta meta = new DfForeignKeyMeta();
        meta.putColumnNameAll(newLinkedHashMap("a", "1", "b", "2"));
        fkMap.put("bar", meta);
    }
    {
        DfForeignKeyMeta meta = new DfForeignKeyMeta();
        meta.putColumnNameAll(newLinkedHashMap("c", "1", "b", "2"));
        fkMap.put("qux", meta);
    }
    {
        DfForeignKeyMeta meta = new DfForeignKeyMeta();
        meta.putColumnNameAll(newLinkedHashMap("c", "7", "a", "2"));
        fkMap.put("corge", meta);
    }
    {
        DfForeignKeyMeta meta = new DfForeignKeyMeta();
        meta.putColumnNameAll(newLinkedHashMap("c", "8"));
        fkMap.put("grault", meta);
    }
    // ## Act ##
    Map<String, DfForeignKeyMeta> sortedMap = extractor.immobilizeOrder(fkMap);
    // ## Assert ##
    assertEquals(5, sortedMap.size());
    Set<Entry<String, DfForeignKeyMeta>> entrySet = sortedMap.entrySet();
    Iterator<Entry<String, DfForeignKeyMeta>> iterator = entrySet.iterator();
    {
        Entry<String, DfForeignKeyMeta> entry = iterator.next();
        assertEquals("bar", entry.getKey());
        assertEquals("1", entry.getValue().getColumnNameMap().get("a"));
        assertEquals("2", entry.getValue().getColumnNameMap().get("b"));
    }
    {
        Entry<String, DfForeignKeyMeta> entry = iterator.next();
        assertEquals("foo", entry.getKey());
        assertEquals("1", entry.getValue().getColumnNameMap().get("b"));
        assertEquals("2", entry.getValue().getColumnNameMap().get("c"));
    }
    {
        Entry<String, DfForeignKeyMeta> entry = iterator.next();
        assertEquals("grault", entry.getKey());
        assertEquals("8", entry.getValue().getColumnNameMap().get("c"));
    }
    {
        Entry<String, DfForeignKeyMeta> entry = iterator.next();
        assertEquals("corge", entry.getKey());
        assertEquals("7", entry.getValue().getColumnNameMap().get("c"));
        assertEquals("2", entry.getValue().getColumnNameMap().get("a"));
    }
    {
        Entry<String, DfForeignKeyMeta> entry = iterator.next();
        assertEquals("qux", entry.getKey());
        assertEquals("1", entry.getValue().getColumnNameMap().get("c"));
        assertEquals("2", entry.getValue().getColumnNameMap().get("b"));
    }
}
Also used : Entry(java.util.Map.Entry) DfForeignKeyMeta(org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta) DfForeignKeyExtractor(org.dbflute.logic.jdbc.metadata.basic.DfForeignKeyExtractor)

Example 2 with DfForeignKeyMeta

use of org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta in project dbflute-core by dbflute.

the class DfSchemaInitializerJdbc method callbackDropForeignKeyByJdbc.

protected void callbackDropForeignKeyByJdbc(Connection conn, List<DfTableMeta> tableMetaList, DfDropForeignKeyByJdbcCallback callback) {
    Statement st = null;
    try {
        st = conn.createStatement();
        for (DfTableMeta tableMeta : tableMetaList) {
            if (isSkipDropForeignKey(tableMeta)) {
                continue;
            }
            final DfForeignKeyExtractor extractor = new DfForeignKeyExtractor();
            extractor.suppressExceptTarget();
            final DatabaseMetaData dbMetaData = conn.getMetaData();
            final Map<String, DfForeignKeyMeta> foreignKeyMetaInfoMap = extractor.getForeignKeyMap(conn, dbMetaData, tableMeta);
            final Set<String> keySet = foreignKeyMetaInfoMap.keySet();
            for (String foreignKeyName : keySet) {
                final DfForeignKeyMeta foreignKeyMetaInfo = foreignKeyMetaInfoMap.get(foreignKeyName);
                final String dropForeignKeySql = callback.buildDropForeignKeySql(foreignKeyMetaInfo);
                logReplaceSql(dropForeignKeySql);
                st.execute(dropForeignKeySql);
            }
        }
    } catch (SQLException e) {
        String msg = "Failed to drop foreign keys!";
        throw new SQLFailureException(msg, e);
    } finally {
        closeStatement(st);
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) DfForeignKeyMeta(org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta) DfTableMeta(org.dbflute.logic.jdbc.metadata.info.DfTableMeta) DatabaseMetaData(java.sql.DatabaseMetaData) DfForeignKeyExtractor(org.dbflute.logic.jdbc.metadata.basic.DfForeignKeyExtractor) SQLFailureException(org.dbflute.exception.SQLFailureException)

Example 3 with DfForeignKeyMeta

use of org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta in project dbflute-core by dbflute.

the class DfForeignKeyExtractor method reflectUniqueKeyFk.

// ===================================================================================
// UniqueKey FK
// ============
protected void reflectUniqueKeyFk(Connection conn, UnifiedSchema unifiedSchema, String tableName, Map<String, DfForeignKeyMeta> fkMap) {
    final List<DfForeignKeyMeta> uniqueKeyFkMetaList = findUniqueKeyFkMetaList(conn, unifiedSchema, tableName);
    if (uniqueKeyFkMetaList == null) {
        return;
    }
    for (DfForeignKeyMeta uniqueKeyFkMeta : uniqueKeyFkMetaList) {
        final String foreignKeyName = uniqueKeyFkMeta.getForeignKeyName();
        if (fkMap.containsKey(foreignKeyName)) {
            _log.info("*The foreign key already exists: table=" + tableName + ", fk=" + foreignKeyName);
            continue;
        }
        fkMap.put(foreignKeyName, uniqueKeyFkMeta);
    }
}
Also used : DfForeignKeyMeta(org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta)

Example 4 with DfForeignKeyMeta

use of org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta 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)

Example 5 with DfForeignKeyMeta

use of org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta in project dbflute-core by dbflute.

the class DfForeignKeyExtractor method createImmobilizedComparator.

protected Comparator<String> createImmobilizedComparator(final Map<String, DfForeignKeyMeta> fkMap) {
    return new Comparator<String>() {

        public int compare(String o1, String o2) {
            // sorted by "column names + FK name" (overridden default sort is by FK name)
            // because FK name might be auto-generated name by DBMS,
            // no change FK but generated classes might be changed after ReplaceSchema
            // (basically FK name should be named fixedly...)
            // so uses local column names as first key here
            final DfForeignKeyMeta meta1 = fkMap.get(o1);
            final DfForeignKeyMeta meta2 = fkMap.get(o2);
            // the map is sorted
            final Map<String, String> columnNameMap1 = meta1.getColumnNameMap();
            final Map<String, String> columnNameMap2 = meta2.getColumnNameMap();
            final String exp1 = Srl.connectByDelimiter(columnNameMap1.keySet(), "/") + "," + o1;
            final String exp2 = Srl.connectByDelimiter(columnNameMap2.keySet(), "/") + "," + o2;
            return exp1.compareTo(exp2);
        }
    };
}
Also used : DfForeignKeyMeta(org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta) Comparator(java.util.Comparator)

Aggregations

DfForeignKeyMeta (org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta)11 Map (java.util.Map)3 StringKeyMap (org.dbflute.helper.StringKeyMap)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 UnifiedSchema (org.apache.torque.engine.database.model.UnifiedSchema)2 DfForeignKeyExtractor (org.dbflute.logic.jdbc.metadata.basic.DfForeignKeyExtractor)2 DfSynonymMeta (org.dbflute.logic.jdbc.metadata.info.DfSynonymMeta)2 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Comparator (java.util.Comparator)1 LinkedHashMap (java.util.LinkedHashMap)1 Entry (java.util.Map.Entry)1 SQLFailureException (org.dbflute.exception.SQLFailureException)1 DfPrimaryKeyMeta (org.dbflute.logic.jdbc.metadata.info.DfPrimaryKeyMeta)1 DfTableMeta (org.dbflute.logic.jdbc.metadata.info.DfTableMeta)1