Search in sources :

Example 1 with DfProcedureSynonymMeta

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

the class DfProcedureExtractor method setupProcedureSynonym.

// -----------------------------------------------------
// Procedure Synonym
// -----------------
protected void setupProcedureSynonym(List<DfProcedureMeta> procedureList) {
    if (_procedureSynonymDataSource == null) {
        return;
    }
    final DfOutsideSqlProperties prop = getOutsideSqlProperties();
    final ProcedureSynonymHandlingType handlingType = prop.getProcedureSynonymHandlingType();
    if (handlingType.equals(ProcedureSynonymHandlingType.NONE)) {
        return;
    }
    final DfProcedureSynonymExtractor extractor = createProcedureSynonymExtractor();
    if (extractor == null) {
        // unsupported at the database
        return;
    }
    final Map<String, DfProcedureSynonymMeta> procedureSynonymMap = extractor.extractProcedureSynonymMap();
    if (handlingType.equals(ProcedureSynonymHandlingType.INCLUDE)) {
    // only add procedure synonyms to the procedure list
    } else if (handlingType.equals(ProcedureSynonymHandlingType.SWITCH)) {
        log("...Clearing normal procedures: count=" + procedureList.size());
        // because of switch
        procedureList.clear();
    } else {
        String msg = "Unexpected handling type of procedure sysnonym: " + handlingType;
        throw new IllegalStateException(msg);
    }
    log("...Adding procedure synonyms as procedure: count=" + procedureSynonymMap.size());
    final List<DfProcedureMeta> procedureSynonymList = new ArrayList<DfProcedureMeta>();
    for (Entry<String, DfProcedureSynonymMeta> entry : procedureSynonymMap.entrySet()) {
        final DfProcedureSynonymMeta metaInfo = entry.getValue();
        if (!isSynonymAllowedSchema(metaInfo)) {
            continue;
        }
        // merge synonym to procedure (create copied instance)
        final String beforeName = metaInfo.getProcedureMetaInfo().buildProcedureLoggingName();
        final DfProcedureMeta mergedProcedure = metaInfo.createMergedProcedure();
        final String afterName = mergedProcedure.buildProcedureLoggingName();
        log("  " + beforeName + " to " + afterName);
        procedureSynonymList.add(mergedProcedure);
    }
    procedureList.addAll(procedureSynonymList);
}
Also used : DfProcedureSynonymExtractor(org.dbflute.logic.jdbc.metadata.synonym.DfProcedureSynonymExtractor) DfProcedureSynonymMeta(org.dbflute.logic.jdbc.metadata.info.DfProcedureSynonymMeta) DfOutsideSqlProperties(org.dbflute.properties.DfOutsideSqlProperties) ArrayList(java.util.ArrayList) DfProcedureMeta(org.dbflute.logic.jdbc.metadata.info.DfProcedureMeta) ProcedureSynonymHandlingType(org.dbflute.properties.DfOutsideSqlProperties.ProcedureSynonymHandlingType)

Example 2 with DfProcedureSynonymMeta

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

Aggregations

ArrayList (java.util.ArrayList)2 DfProcedureMeta (org.dbflute.logic.jdbc.metadata.info.DfProcedureMeta)2 DfProcedureSynonymMeta (org.dbflute.logic.jdbc.metadata.info.DfProcedureSynonymMeta)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 LinkedHashMap (java.util.LinkedHashMap)1 UnifiedSchema (org.apache.torque.engine.database.model.UnifiedSchema)1 SQLFailureException (org.dbflute.exception.SQLFailureException)1 DfProcedureExtractor (org.dbflute.logic.jdbc.metadata.basic.DfProcedureExtractor)1 DfSynonymMeta (org.dbflute.logic.jdbc.metadata.info.DfSynonymMeta)1 DfProcedureNativeTranslatorOracle (org.dbflute.logic.jdbc.metadata.procedure.DfProcedureNativeTranslatorOracle)1 DfProcedureSynonymExtractor (org.dbflute.logic.jdbc.metadata.synonym.DfProcedureSynonymExtractor)1 DfOutsideSqlProperties (org.dbflute.properties.DfOutsideSqlProperties)1 ProcedureSynonymHandlingType (org.dbflute.properties.DfOutsideSqlProperties.ProcedureSynonymHandlingType)1