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;
}
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);
}
}
}
}
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());
}
}
}
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;
}
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;
}
Aggregations