Search in sources :

Example 1 with DfSynonymMeta

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

the class DfSchemaXmlSerializer method getUniqueKeyMap.

// -----------------------------------------------------
// Unique Key
// ----------
/**
 * Get unique column name list.
 * @param metaData The meta data of a database. (NotNull)
 * @param tableMeta The meta information of table. (NotNull)
 * @param pkInfo The meta information of primary key. (NotNull)
 * @return The list of unique columns. (NotNull)
 * @throws SQLException When it fails to handle the SQL.
 */
protected Map<String, Map<Integer, String>> getUniqueKeyMap(DatabaseMetaData metaData, DfTableMeta tableMeta, DfPrimaryKeyMeta pkInfo) throws SQLException {
    final Map<String, Map<Integer, String>> uniqueKeyMap = _uniqueKeyExtractor.getUniqueKeyMap(metaData, tableMeta, pkInfo);
    if (!canHandleSynonym(tableMeta) || !uniqueKeyMap.isEmpty()) {
        return uniqueKeyMap;
    }
    final DfSynonymMeta synonym = getSynonymMetaInfo(tableMeta);
    return synonym != null ? synonym.getUniqueKeyMap() : uniqueKeyMap;
}
Also used : DfSynonymMeta(org.dbflute.logic.jdbc.metadata.info.DfSynonymMeta) Map(java.util.Map) TypeMap(org.apache.torque.engine.database.model.TypeMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) StringKeyMap(org.dbflute.helper.StringKeyMap)

Example 2 with DfSynonymMeta

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

the class DfSynonymExtractorOracle method setupTableColumnComment.

/**
 * Set up table and column comment. <br>
 * This does not support DB link synonym.
 * @param synonymMap The map of synonym. (NotNull)
 */
protected void setupTableColumnComment(Map<String, DfSynonymMeta> synonymMap) {
    final Map<UnifiedSchema, Set<String>> ownerTabSetMap = createOwnerTableSetMap(synonymMap);
    final Map<UnifiedSchema, Map<String, UserTabComments>> ownerTabCommentMap = newLinkedHashMap();
    final Map<UnifiedSchema, Map<String, Map<String, UserColComments>>> ownerTabColCommentMap = newLinkedHashMap();
    for (UnifiedSchema owner : ownerTabSetMap.keySet()) {
        final Set<String> tableSet = ownerTabSetMap.get(owner);
        final DfDbCommentExtractorOracle extractor = createDbCommentExtractor(owner);
        final Map<String, UserTabComments> tabCommentMap = extractor.extractTableComment(tableSet);
        final Map<String, Map<String, UserColComments>> tabColCommentMap = extractor.extractColumnComment(tableSet);
        ownerTabCommentMap.put(owner, tabCommentMap);
        ownerTabColCommentMap.put(owner, tabColCommentMap);
    }
    for (DfSynonymMeta synonym : synonymMap.values()) {
        final UnifiedSchema owner = synonym.getTableOwner();
        final String tableName = synonym.getTableName();
        final Map<String, UserTabComments> tableCommentMap = ownerTabCommentMap.get(owner);
        if (tableCommentMap != null) {
            final UserTabComments userTabComments = tableCommentMap.get(tableName);
            if (userTabComments != null && userTabComments.hasComments()) {
                synonym.setTableComment(userTabComments.getComments());
            }
        }
        final Map<String, Map<String, UserColComments>> tabColCommentMap = ownerTabColCommentMap.get(owner);
        if (tabColCommentMap != null) {
            final Map<String, UserColComments> colCommentMap = tabColCommentMap.get(tableName);
            if (colCommentMap != null && !colCommentMap.isEmpty()) {
                synonym.setColumnCommentMap(colCommentMap);
            }
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) UserColComments(org.dbflute.logic.jdbc.metadata.comment.DfDbCommentExtractor.UserColComments) UserTabComments(org.dbflute.logic.jdbc.metadata.comment.DfDbCommentExtractor.UserTabComments) DfSynonymMeta(org.dbflute.logic.jdbc.metadata.info.DfSynonymMeta) UnifiedSchema(org.apache.torque.engine.database.model.UnifiedSchema) DfDbCommentExtractorOracle(org.dbflute.logic.jdbc.metadata.comment.DfDbCommentExtractorOracle) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) StringKeyMap(org.dbflute.helper.StringKeyMap)

Example 3 with DfSynonymMeta

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

the class DfSynonymExtractorOracle method translateFKTable.

protected void translateFKTable(Map<String, DfSynonymMeta> synonymMap) {
    final Collection<DfSynonymMeta> synonymList = synonymMap.values();
    final Map<String, List<DfSynonymMeta>> referredTableSynonymListMap = DfCollectionUtil.newLinkedHashMap();
    for (DfSynonymMeta synonym : synonymList) {
        final String tableName = synonym.getTableName();
        final List<DfSynonymMeta> foreignSynonymList = referredTableSynonymListMap.get(tableName);
        if (foreignSynonymList != null) {
            foreignSynonymList.add(synonym);
        } else {
            final List<DfSynonymMeta> foreignNewSynonymList = DfCollectionUtil.newArrayList();
            foreignNewSynonymList.add(synonym);
            referredTableSynonymListMap.put(tableName, foreignNewSynonymList);
        }
    }
    for (DfSynonymMeta synonym : synonymList) {
        final Map<String, DfForeignKeyMeta> fkMap = synonym.getForeignKeyMap();
        if (fkMap == null || fkMap.isEmpty()) {
            continue;
        }
        final Map<String, DfForeignKeyMeta> additionalFKMap = DfCollectionUtil.newLinkedHashMap();
        final Map<String, String> removedFKMap = DfCollectionUtil.newLinkedHashMap();
        for (Entry<String, DfForeignKeyMeta> entry : fkMap.entrySet()) {
            final String fkName = entry.getKey();
            final DfForeignKeyMeta fk = entry.getValue();
            // at first translate a local table name
            fk.setLocalSchema(synonym.getSynonymOwner());
            fk.setLocalTablePureName(synonym.getSynonymName());
            final String orignalForeignTableName = fk.getForeignTablePureName();
            final List<DfSynonymMeta> foreignSynonymList = referredTableSynonymListMap.get(orignalForeignTableName);
            if (foreignSynonymList == null || foreignSynonymList.isEmpty()) {
                final UnifiedSchema tableOwner = synonym.getTableOwner();
                if (_tableExtractor.isTableExcept(tableOwner, orignalForeignTableName)) {
                    removedFKMap.put(fkName, orignalForeignTableName);
                } else if (!isForeignTableGenerated(tableOwner, orignalForeignTableName)) {
                    removedFKMap.put(fkName, orignalForeignTableName);
                }
                continue;
            }
            final String originalForeignKeyName = fk.getForeignKeyName();
            boolean firstDone = false;
            for (int i = 0; i < foreignSynonymList.size(); i++) {
                final String newForeignKeyName = originalForeignKeyName + "_SYNONYM" + (i + 1);
                final DfSynonymMeta newForeignSynonym = foreignSynonymList.get(i);
                final String newForeignTableName = newForeignSynonym.getSynonymName();
                final UnifiedSchema newForeignSchema = newForeignSynonym.getSynonymOwner();
                if (!firstDone) {
                    // first (switching FK informations)
                    fk.setForeignKeyName(newForeignKeyName);
                    fk.setForeignTablePureName(newForeignTableName);
                    fk.setForeignSchema(newForeignSchema);
                    firstDone = true;
                    continue;
                }
                // second or more (creating new FK instance)
                final DfForeignKeyMeta additionalFK = new DfForeignKeyMeta();
                additionalFK.setForeignKeyName(newForeignKeyName);
                additionalFK.setLocalSchema(fk.getLocalSchema());
                additionalFK.setLocalTablePureName(fk.getLocalTablePureName());
                additionalFK.setForeignSchema(newForeignSchema);
                additionalFK.setForeignTablePureName(newForeignTableName);
                additionalFK.putColumnNameAll(fk.getColumnNameMap());
                additionalFKMap.put(additionalFK.getForeignKeyName(), additionalFK);
            }
        }
        fkMap.putAll(additionalFKMap);
        if (!removedFKMap.isEmpty()) {
            final StringBuilder sb = new StringBuilder();
            sb.append("...Excepting foreign keys from the synonym:").append(ln()).append("[Excepted Foreign Key]");
            final Set<String> removedFKKeySet = removedFKMap.keySet();
            for (String removedKey : removedFKKeySet) {
                sb.append(ln()).append(" ").append(removedKey);
                sb.append(" (").append(synonym.getSynonymName()).append(" to ");
                sb.append(removedFKMap.get(removedKey)).append(")");
                fkMap.remove(removedKey);
            }
            _log.info(sb.toString());
        }
    }
}
Also used : DfForeignKeyMeta(org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta) DfSynonymMeta(org.dbflute.logic.jdbc.metadata.info.DfSynonymMeta) UnifiedSchema(org.apache.torque.engine.database.model.UnifiedSchema) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with DfSynonymMeta

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

the class DfProcedureSynonymExtractorOracle method extractProcedureSynonymMap.

// ===================================================================================
// Extract
// =======
public Map<String, DfProcedureSynonymMeta> extractProcedureSynonymMap() {
    _log.info("...Extracting procedure synonym");
    final Map<String, DfProcedureSynonymMeta> procedureSynonymMap = StringKeyMap.createAsFlexibleOrdered();
    final String sql = buildSynonymSelect();
    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    try {
        conn = _dataSource.getConnection();
        final Map<String, DfProcedureMeta> procedureMap = new LinkedHashMap<String, DfProcedureMeta>();
        final List<DfProcedureMeta> procedureList = new ArrayList<DfProcedureMeta>();
        final DfProcedureExtractor extractor = new DfProcedureExtractor();
        extractor.suppressLogging();
        for (UnifiedSchema unifiedSchema : _targetSchemaList) {
            // get new procedure list because different instances is needed at this process
            procedureList.addAll(extractor.getPlainProcedureList(_dataSource, unifiedSchema));
        }
        for (DfProcedureMeta metaInfo : procedureList) {
            final String procedureKeyName = metaInfo.getProcedureFullQualifiedName();
            procedureMap.put(procedureKeyName, metaInfo);
        }
        DfProcedureNativeTranslatorOracle translator = null;
        st = conn.createStatement();
        _log.info(sql);
        rs = st.executeQuery(sql);
        while (rs.next()) {
            final UnifiedSchema synonymOwner = createAsDynamicSchema(null, rs.getString("OWNER"));
            final String synonymName = rs.getString("SYNONYM_NAME");
            final UnifiedSchema tableOwner = createAsDynamicSchema(null, rs.getString("TABLE_OWNER"));
            final String tableName = rs.getString("TABLE_NAME");
            final String dbLinkName = rs.getString("DB_LINK");
            final DfSynonymMeta synonymMetaInfo = new DfSynonymMeta();
            // Basic
            synonymMetaInfo.setSynonymOwner(synonymOwner);
            synonymMetaInfo.setSynonymName(synonymName);
            synonymMetaInfo.setTableOwner(tableOwner);
            synonymMetaInfo.setTableName(tableName);
            synonymMetaInfo.setDBLinkName(dbLinkName);
            // Select-able?
            judgeSynonymSelectable(synonymMetaInfo);
            if (synonymMetaInfo.isSelectable()) {
                // select-able synonyms are out of target
                continue;
            }
            DfProcedureMeta procedureMeta = null;
            if (dbLinkName != null && dbLinkName.trim().length() > 0) {
                // synonym for DB link
                if (translator == null) {
                    translator = new DfProcedureNativeTranslatorOracle(_dataSource);
                }
                procedureMeta = prepareProcedureToDBLink(tableOwner, tableName, dbLinkName, extractor, translator);
            } else {
                procedureMeta = findProcedureMeta(tableOwner, tableName, procedureMap);
            }
            if (procedureMeta == null) {
                // may be package procedure or other schema's one
                continue;
            }
            procedureMeta.setProcedureSynonym(true);
            final DfProcedureSynonymMeta procedureSynonymMetaInfo = new DfProcedureSynonymMeta();
            procedureSynonymMetaInfo.setProcedureMetaInfo(procedureMeta);
            procedureSynonymMetaInfo.setSynonymMetaInfo(synonymMetaInfo);
            final String synonymKey = buildSynonymMapKey(synonymOwner, synonymName);
            procedureSynonymMap.put(synonymKey, procedureSynonymMetaInfo);
        }
    } catch (SQLException e) {
        String msg = "Failed to get procedure synonyms: sql=" + sql;
        throw new SQLFailureException(msg, e);
    } finally {
        if (st != null) {
            try {
                st.close();
            } catch (SQLException ignored) {
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ignored) {
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ignored) {
            }
        }
    }
    return procedureSynonymMap;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SQLFailureException(org.dbflute.exception.SQLFailureException) DfProcedureSynonymMeta(org.dbflute.logic.jdbc.metadata.info.DfProcedureSynonymMeta) DfProcedureExtractor(org.dbflute.logic.jdbc.metadata.basic.DfProcedureExtractor) DfSynonymMeta(org.dbflute.logic.jdbc.metadata.info.DfSynonymMeta) UnifiedSchema(org.apache.torque.engine.database.model.UnifiedSchema) ResultSet(java.sql.ResultSet) DfProcedureMeta(org.dbflute.logic.jdbc.metadata.info.DfProcedureMeta) DfProcedureNativeTranslatorOracle(org.dbflute.logic.jdbc.metadata.procedure.DfProcedureNativeTranslatorOracle)

Example 5 with DfSynonymMeta

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

the class DfSchemaXmlSerializer method getSynonymMetaInfo.

protected DfSynonymMeta getSynonymMetaInfo(DfTableMeta tableInfo) {
    if (!canHandleSynonym(tableInfo)) {
        String msg = "The table meta information should be for synonym: " + tableInfo;
        throw new IllegalStateException(msg);
    }
    String key = tableInfo.getTableFullQualifiedName();
    DfSynonymMeta info = _supplementarySynonymInfoMap.get(key);
    if (info != null) {
        return info;
    }
    key = tableInfo.getSchemaQualifiedName();
    info = _supplementarySynonymInfoMap.get(key);
    if (info != null) {
        return info;
    }
    return null;
}
Also used : DfSynonymMeta(org.dbflute.logic.jdbc.metadata.info.DfSynonymMeta)

Aggregations

DfSynonymMeta (org.dbflute.logic.jdbc.metadata.info.DfSynonymMeta)14 UnifiedSchema (org.apache.torque.engine.database.model.UnifiedSchema)6 LinkedHashMap (java.util.LinkedHashMap)5 ResultSet (java.sql.ResultSet)4 Map (java.util.Map)4 StringKeyMap (org.dbflute.helper.StringKeyMap)4 ArrayList (java.util.ArrayList)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 Set (java.util.Set)2 TreeMap (java.util.TreeMap)2 TypeMap (org.apache.torque.engine.database.model.TypeMap)2 DfColumnMeta (org.dbflute.logic.jdbc.metadata.info.DfColumnMeta)2 DfForeignKeyMeta (org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta)2 Entry (java.util.Map.Entry)1 SQLFailureException (org.dbflute.exception.SQLFailureException)1 StringSet (org.dbflute.helper.StringSet)1