use of org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta 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"));
}
}
use of org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta 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);
}
}
use of org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta in project dbflute-core by dbflute.
the class DfForeignKeyExtractor method reflectUniqueKeyFk.
// ===================================================================================
// UniqueKey FK
// ============
protected void reflectUniqueKeyFk(Connection conn, UnifiedSchema unifiedSchema, String tableName, Map<String, DfForeignKeyMeta> fkMap) {
final List<DfForeignKeyMeta> uniqueKeyFkMetaList = findUniqueKeyFkMetaList(conn, unifiedSchema, tableName);
if (uniqueKeyFkMetaList == null) {
return;
}
for (DfForeignKeyMeta uniqueKeyFkMeta : uniqueKeyFkMetaList) {
final String foreignKeyName = uniqueKeyFkMeta.getForeignKeyName();
if (fkMap.containsKey(foreignKeyName)) {
_log.info("*The foreign key already exists: table=" + tableName + ", fk=" + foreignKeyName);
continue;
}
fkMap.put(foreignKeyName, uniqueKeyFkMeta);
}
}
use of org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta in project dbflute-core by dbflute.
the class DfForeignKeyExtractor method prepareUniqueKeyFkCache.
protected void prepareUniqueKeyFkCache(Connection conn, UnifiedSchema unifiedSchema) {
// preparing unique-key FK info of the schema
final Map<String, List<DfForeignKeyMeta>> tableMap = StringKeyMap.createAsFlexibleConcurrent();
final DfUniqueKeyFkExtractor extractor = createUniqueKeyFkExtractor(conn, unifiedSchema);
if (extractor == null) {
// no need to extract in this DBMS
_uniqueKeyFkMap.put(unifiedSchema, new ConcurrentHashMap<String, List<DfForeignKeyMeta>>());
return;
}
_log.info("...Extracting unique-key FK: " + unifiedSchema);
final Map<String, Map<String, List<UserUniqueFkColumn>>> uniqueKeyFkMap = extractor.extractUniqueKeyFkMap();
for (Entry<String, Map<String, List<UserUniqueFkColumn>>> tableEntry : uniqueKeyFkMap.entrySet()) {
final String tableKey = tableEntry.getKey();
final List<DfForeignKeyMeta> metaList = new ArrayList<DfForeignKeyMeta>();
final Map<String, List<UserUniqueFkColumn>> fkColumnListMap = tableEntry.getValue();
for (Entry<String, List<UserUniqueFkColumn>> fkEntry : fkColumnListMap.entrySet()) {
final List<UserUniqueFkColumn> columnList = fkEntry.getValue();
DfForeignKeyMeta meta = null;
for (UserUniqueFkColumn uniqueFkColumn : columnList) {
if (meta == null) {
meta = new DfForeignKeyMeta();
meta.setForeignKeyName(uniqueFkColumn.getForeignKeyName());
meta.setLocalSchema(unifiedSchema);
meta.setLocalTablePureName(uniqueFkColumn.getLocalTableName());
// same schema only supported
meta.setForeignSchema(unifiedSchema);
final String foreignTableName = uniqueFkColumn.getForeignTableName();
meta.setForeignTablePureName(foreignTableName);
if (!isForeignTableGenerated(foreignTableName)) {
break;
}
}
meta.putColumnName(uniqueFkColumn.getLocalColumnName(), uniqueFkColumn.getForeignColumnName());
}
if (meta == null) {
// basically no way
throw new IllegalStateException("The key should have any elements: " + tableKey);
}
metaList.add(meta);
}
tableMap.put(tableKey, metaList);
}
_uniqueKeyFkMap.put(unifiedSchema, tableMap);
if (!tableMap.isEmpty()) {
_log.info(" -> Unique-key FK: " + tableMap.keySet());
} else {
_log.info(" -> Not found unique-key FK");
}
}
use of org.dbflute.logic.jdbc.metadata.info.DfForeignKeyMeta in project dbflute-core by dbflute.
the class DfForeignKeyExtractor method createImmobilizedComparator.
protected Comparator<String> createImmobilizedComparator(final Map<String, DfForeignKeyMeta> fkMap) {
return new Comparator<String>() {
public int compare(String o1, String o2) {
// sorted by "column names + FK name" (overridden default sort is by FK name)
// because FK name might be auto-generated name by DBMS,
// no change FK but generated classes might be changed after ReplaceSchema
// (basically FK name should be named fixedly...)
// so uses local column names as first key here
final DfForeignKeyMeta meta1 = fkMap.get(o1);
final DfForeignKeyMeta meta2 = fkMap.get(o2);
// the map is sorted
final Map<String, String> columnNameMap1 = meta1.getColumnNameMap();
final Map<String, String> columnNameMap2 = meta2.getColumnNameMap();
final String exp1 = Srl.connectByDelimiter(columnNameMap1.keySet(), "/") + "," + o1;
final String exp2 = Srl.connectByDelimiter(columnNameMap2.keySet(), "/") + "," + o2;
return exp1.compareTo(exp2);
}
};
}
Aggregations