Search in sources :

Example 56 with UnifiedSchema

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

the class DfSynonymExtractorOracle method extractSynonymMap.

// ===================================================================================
// Extract
// =======
public Map<String, DfSynonymMeta> extractSynonymMap() {
    final Map<String, DfSynonymMeta> synonymMap = StringKeyMap.createAsFlexibleOrdered();
    Map<String, Map<String, SynonymNativeInfo>> dbLinkSynonymNativeMap = null;
    final String sql = buildSynonymSelect();
    Connection conn = null;
    Statement statement = null;
    ResultSet rs = null;
    try {
        conn = _dataSource.getConnection();
        statement = conn.createStatement();
        _log.info(sql);
        rs = statement.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");
            if (_tableExtractor.isTableExcept(synonymOwner, synonymName)) {
                // because it is not necessary to handle excepted tables
                continue;
            }
            final DfSynonymMeta info = new DfSynonymMeta();
            // Basic
            info.setSynonymOwner(synonymOwner);
            info.setSynonymName(synonymName);
            info.setTableOwner(tableOwner);
            info.setTableName(tableName);
            info.setDBLinkName(dbLinkName);
            // Select-able?
            judgeSynonymSelectable(info);
            if (info.isSelectable()) {
                // e.g. procedure synonym
                // set up column definition for supplement info
                final List<DfColumnMeta> columnMetaInfoList = getSynonymColumns(conn, synonymOwner, synonymName);
                info.setColumnMetaInfoList(columnMetaInfoList);
            }
            if (dbLinkName != null && dbLinkName.trim().length() > 0) {
                if (dbLinkSynonymNativeMap == null) {
                    // lazy load
                    dbLinkSynonymNativeMap = extractDBLinkSynonymNativeMap();
                }
                // = = = = = = = = = = = =
                try {
                    final String synonymKey = buildSynonymMapKey(synonymOwner, synonymName);
                    synonymMap.put(synonymKey, setupDBLinkSynonym(conn, info, dbLinkSynonymNativeMap));
                } catch (Exception continued) {
                    _log.info("Failed to get meta data of " + synonymName + ": " + continued.getMessage());
                }
                continue;
            }
            if (!tableOwner.hasSchema()) {
                // basically no way because it may be for DB Link Synonym
                continue;
            }
            // PK, ID, UQ, FK, Index
            try {
                setupBasicConstraintInfo(info, tableOwner, tableName, conn);
            } catch (Exception continued) {
                _log.info("Failed to get meta data of " + synonymName + ": " + continued.getMessage());
                continue;
            }
            final String synonymKey = buildSynonymMapKey(synonymOwner, synonymName);
            synonymMap.put(synonymKey, info);
        }
    } catch (SQLException e) {
        throw new IllegalStateException(e);
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException ignored) {
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ignored) {
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ignored) {
            }
        }
    }
    // It translates foreign key meta informations.
    translateFKTable(synonymMap);
    setupTableColumnComment(synonymMap);
    return synonymMap;
}
Also used : DfColumnMeta(org.dbflute.logic.jdbc.metadata.info.DfColumnMeta) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) SQLException(java.sql.SQLException) DfSynonymMeta(org.dbflute.logic.jdbc.metadata.info.DfSynonymMeta) UnifiedSchema(org.apache.torque.engine.database.model.UnifiedSchema) ResultSet(java.sql.ResultSet) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) StringKeyMap(org.dbflute.helper.StringKeyMap)

Example 57 with UnifiedSchema

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

the class DfSynonymExtractorOracle method createOwnerTableSetMap.

protected Map<UnifiedSchema, Set<String>> createOwnerTableSetMap(Map<String, DfSynonymMeta> synonymMap) {
    final Map<UnifiedSchema, Set<String>> ownerTabSetMap = newLinkedHashMap();
    for (DfSynonymMeta synonym : synonymMap.values()) {
        final UnifiedSchema owner = synonym.getTableOwner();
        if (synonym.isDBLink()) {
            // Synonym of DB Link is out of target!
            continue;
        }
        Set<String> tableSet = ownerTabSetMap.get(owner);
        if (tableSet == null) {
            tableSet = new LinkedHashSet<String>();
            ownerTabSetMap.put(owner, tableSet);
        }
        tableSet.add(synonym.getTableName());
    }
    return ownerTabSetMap;
}
Also used : ResultSet(java.sql.ResultSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) DfSynonymMeta(org.dbflute.logic.jdbc.metadata.info.DfSynonymMeta) UnifiedSchema(org.apache.torque.engine.database.model.UnifiedSchema)

Example 58 with UnifiedSchema

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

the class DfArrayExtractorOracle method setupFlatArrayInfo.

protected void setupFlatArrayInfo(StringKeyMap<DfTypeArrayInfo> flatArrayInfoMap, List<DfProcedureArgumentInfo> argInfoList, DfProcedureArgumentInfo argInfo, int index) {
    final UnifiedSchema owner = UnifiedSchema.createAsDynamicSchema(null, argInfo.getTypeOwner());
    final String realTypeName = buildArrayTypeName(argInfo);
    final DfTypeArrayInfo arrayInfo = new DfTypeArrayInfo(owner, realTypeName);
    final boolean nestedArray = reflectArrayElementType(argInfoList, index, arrayInfo);
    flatArrayInfoMap.put(realTypeName, arrayInfo);
    if (nestedArray) {
        final int nextIndex = (index + 1);
        final DfProcedureArgumentInfo nextArgInfo = argInfoList.get(nextIndex);
        // recursive call
        setupFlatArrayInfo(flatArrayInfoMap, argInfoList, nextArgInfo, nextIndex);
    }
}
Also used : UnifiedSchema(org.apache.torque.engine.database.model.UnifiedSchema) DfTypeArrayInfo(org.dbflute.logic.jdbc.metadata.info.DfTypeArrayInfo) DfProcedureArgumentInfo(org.dbflute.logic.jdbc.metadata.info.DfProcedureArgumentInfo)

Example 59 with UnifiedSchema

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

the class DfSchemaXmlSerializer method doHelpTableComment.

protected void doHelpTableComment(List<DfTableMeta> tableList, UnifiedSchema unifiedSchema) {
    final DfDbCommentExtractor dbCommentExtractor = createDbCommentExtractor(unifiedSchema);
    if (dbCommentExtractor != null) {
        final Set<String> tableSet = new HashSet<String>();
        for (DfTableMeta table : tableList) {
            tableSet.add(table.getTableName());
        }
        try {
            final Map<String, UserTabComments> tableCommentMap = dbCommentExtractor.extractTableComment(tableSet);
            for (DfTableMeta table : tableList) {
                table.acceptTableComment(tableCommentMap);
            // *Synonym Processing is after loading synonyms.
            }
        } catch (RuntimeException ignored) {
            _log.info("Failed to extract table comments: extractor=" + dbCommentExtractor, ignored);
        }
        try {
            if (_columnCommentAllMap == null) {
                _columnCommentAllMap = new LinkedHashMap<UnifiedSchema, Map<String, Map<String, UserColComments>>>();
            }
            final Map<String, Map<String, UserColComments>> columnCommentMap = _columnCommentAllMap.get(unifiedSchema);
            final Map<String, Map<String, UserColComments>> extractedMap = dbCommentExtractor.extractColumnComment(tableSet);
            if (columnCommentMap == null) {
                _columnCommentAllMap.put(unifiedSchema, extractedMap);
            } else {
                // basically no way, schema is unique but just in case
                // merge
                columnCommentMap.putAll(extractedMap);
            }
        } catch (RuntimeException continued) {
            _log.info("Failed to extract column comments: extractor=" + dbCommentExtractor, continued);
        }
    }
}
Also used : UserColComments(org.dbflute.logic.jdbc.metadata.comment.DfDbCommentExtractor.UserColComments) UserTabComments(org.dbflute.logic.jdbc.metadata.comment.DfDbCommentExtractor.UserTabComments) DfDbCommentExtractor(org.dbflute.logic.jdbc.metadata.comment.DfDbCommentExtractor) UnifiedSchema(org.apache.torque.engine.database.model.UnifiedSchema) DfTableMeta(org.dbflute.logic.jdbc.metadata.info.DfTableMeta) 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) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 60 with UnifiedSchema

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

the class DfSchemaXmlSerializer method doProcessTable.

protected boolean doProcessTable(Connection conn, DatabaseMetaData metaData, DfTableMeta tableMeta) throws SQLException {
    final String tableFullQualifiedName = tableMeta.getTableFullQualifiedName();
    if (tableMeta.isOutOfGenerateTarget()) {
        // for example, sequence synonym and so on...
        _log.info("$ " + tableFullQualifiedName + " is out of generation target!");
        return false;
    }
    _log.info("$ " + tableMeta.toString());
    final Element tableElement = _doc.createElement("table");
    tableElement.setAttribute("name", tableMeta.getTableName());
    tableElement.setAttribute("type", tableMeta.getTableType());
    final UnifiedSchema unifiedSchema = tableMeta.getUnifiedSchema();
    if (unifiedSchema.hasSchema()) {
        tableElement.setAttribute("schema", unifiedSchema.getIdentifiedSchema());
    }
    final String tableComment = tableMeta.getTableComment();
    if (Srl.is_NotNull_and_NotTrimmedEmpty(tableComment)) {
        tableElement.setAttribute("comment", tableComment);
    }
    final DfPrimaryKeyMeta pkInfo = getPrimaryColumnMetaInfo(metaData, tableMeta);
    final List<DfColumnMeta> columns = getColumns(metaData, tableMeta);
    for (int j = 0; j < columns.size(); j++) {
        final DfColumnMeta columnMeta = columns.get(j);
        final Element columnElement = _doc.createElement("column");
        processColumnName(columnMeta, columnElement);
        processColumnType(columnMeta, columnElement);
        processColumnDbType(columnMeta, columnElement);
        processColumnJavaType(columnMeta, columnElement);
        processColumnSize(columnMeta, columnElement);
        processDatetimePrecision(columnMeta, columnElement);
        processRequired(columnMeta, columnElement);
        processPrimaryKey(columnMeta, pkInfo, columnElement);
        processColumnComment(columnMeta, columnElement);
        processDefaultValue(columnMeta, columnElement);
        processAutoIncrement(tableMeta, columnMeta, pkInfo, conn, columnElement);
        tableElement.appendChild(columnElement);
    }
    processForeignKey(conn, metaData, tableMeta, tableElement);
    final Map<String, Map<Integer, String>> uniqueKeyMap = processUniqueKey(metaData, tableMeta, pkInfo, tableElement);
    processIndex(metaData, tableMeta, tableElement, uniqueKeyMap);
    _tableElementStagingMap.put(tableFullQualifiedName, tableElement);
    return true;
}
Also used : DfPrimaryKeyMeta(org.dbflute.logic.jdbc.metadata.info.DfPrimaryKeyMeta) DfColumnMeta(org.dbflute.logic.jdbc.metadata.info.DfColumnMeta) Element(org.w3c.dom.Element) UnifiedSchema(org.apache.torque.engine.database.model.UnifiedSchema) 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) Constraint(org.apache.torque.engine.database.model.Constraint)

Aggregations

UnifiedSchema (org.apache.torque.engine.database.model.UnifiedSchema)63 Map (java.util.Map)9 StringKeyMap (org.dbflute.helper.StringKeyMap)9 DfProcedureMeta (org.dbflute.logic.jdbc.metadata.info.DfProcedureMeta)8 LinkedHashMap (java.util.LinkedHashMap)7 ResultSet (java.sql.ResultSet)6 DfSynonymMeta (org.dbflute.logic.jdbc.metadata.info.DfSynonymMeta)6 DfTableMeta (org.dbflute.logic.jdbc.metadata.info.DfTableMeta)6 ArrayList (java.util.ArrayList)5 LinkedHashSet (java.util.LinkedHashSet)4 DfProcedureSupplementExtractor (org.dbflute.logic.jdbc.metadata.procedure.DfProcedureSupplementExtractor)4 List (java.util.List)3 TreeMap (java.util.TreeMap)3 TypeMap (org.apache.torque.engine.database.model.TypeMap)3 DfColumnMeta (org.dbflute.logic.jdbc.metadata.info.DfColumnMeta)3 DfAdditionalSchemaInfo (org.dbflute.properties.assistant.database.DfAdditionalSchemaInfo)3 Connection (java.sql.Connection)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2