Search in sources :

Example 1 with DfForeignKeyExtractor

use of org.dbflute.logic.jdbc.metadata.basic.DfForeignKeyExtractor 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 2 with DfForeignKeyExtractor

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

Aggregations

DfForeignKeyExtractor (org.dbflute.logic.jdbc.metadata.basic.DfForeignKeyExtractor)2 DfForeignKeyMeta (org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta)2 DatabaseMetaData (java.sql.DatabaseMetaData)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Entry (java.util.Map.Entry)1 SQLFailureException (org.dbflute.exception.SQLFailureException)1 DfTableMeta (org.dbflute.logic.jdbc.metadata.info.DfTableMeta)1