use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.
the class DfTableOrderAnalyzer method doAnalyzeOrder.
/**
* @param tableList The list of table, which may be registered. (NotNull)
* @param alreadyRegisteredSet The (pure) name set of already registered table. (NotNull)
* @param outputOrderedList The ordered list of table for output. (NotNull)
* @return The list of unregistered table. (NotNull)
*/
protected List<Table> doAnalyzeOrder(List<Table> tableList, Set<String> alreadyRegisteredSet, List<List<Table>> outputOrderedList, final int level) {
final List<Table> unregisteredTableList = new ArrayList<Table>();
final List<Table> elementList = new ArrayList<Table>();
for (Table table : tableList) {
final List<ForeignKey> foreignKeyList = table.getForeignKeyList();
boolean dependsOnAny = false;
for (ForeignKey fk : foreignKeyList) {
final String foreignTablePureName = fk.getForeignTablePureName();
if (level >= 1 && fk.hasFixedCondition()) {
// from first level, ignore fixed condition
continue;
}
if (level >= 2 && fk.isAdditionalForeignKey()) {
// from second level, ignore additional FK
continue;
}
if (!fk.isSelfReference() && !alreadyRegisteredSet.contains(foreignTablePureName)) {
// found non-registered parent table so it still depends on any
dependsOnAny = true;
break;
}
}
if (dependsOnAny) {
unregisteredTableList.add(table);
} else {
elementList.add(table);
// pure name here
alreadyRegisteredSet.add(table.getName());
}
}
if (!elementList.isEmpty()) {
outputOrderedList.add(elementList);
}
return unregisteredTableList;
}
use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.
the class DfLReverseTableOrder method doAnalyzeOrder.
/**
* @param tableList The list of table, which may be registered. (NotNull)
* @param alreadyRegisteredSet The (pure) name set of already registered table. (NotNull)
* @param outputOrderedList The ordered list of table for output. (NotNull)
* @return The list of unregistered table. (NotNull)
*/
protected List<Table> doAnalyzeOrder(List<Table> tableList, Set<String> alreadyRegisteredSet, List<List<Table>> outputOrderedList, final int level) {
final List<Table> unregisteredTableList = new ArrayList<Table>();
final List<Table> elementList = new ArrayList<Table>();
for (Table table : tableList) {
final List<ForeignKey> foreignKeyList = table.getForeignKeyList();
boolean dependsOnAny = false;
for (ForeignKey fk : foreignKeyList) {
final String foreignTablePureName = fk.getForeignTablePureName();
if (level >= 1 && fk.hasFixedCondition()) {
// from first level, ignore fixed condition
continue;
}
if (level >= 2 && fk.isAdditionalForeignKey()) {
// from second level, ignore additional FK
continue;
}
if (!fk.isSelfReference() && !alreadyRegisteredSet.contains(foreignTablePureName)) {
// found non-registered parent table so it still depends on any
dependsOnAny = true;
break;
}
}
if (dependsOnAny) {
unregisteredTableList.add(table);
} else {
elementList.add(table);
// pure name here
alreadyRegisteredSet.add(table.getName());
}
}
if (!elementList.isEmpty()) {
outputOrderedList.add(elementList);
}
return unregisteredTableList;
}
use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.
the class DfAdditionalForeignKeyInitializer method processAllTableFK.
protected void processAllTableFK(String foreignKeyName, String foreignTableName, List<String> foreignColumnNameList, DfAdditionalForeignKeyOption option) {
if (option.isFixedOnlyJoin()) {
String msg = "Cannot use fixedOnlyJoin when all-table FK: " + foreignKeyName;
throw new DfIllegalPropertySettingException(msg);
}
// for check about same-column self reference
final Table foreignTable = getTable(foreignTableName);
final StringSet foreignColumnSet = StringSet.createAsFlexible();
foreignColumnSet.addAll(foreignColumnNameList);
for (Table table : getTableList()) {
final String localTableName = table.getTableDbName();
final List<String> localColumnNameList = getLocalColumnNameList(table, foreignKeyName, foreignTableName, foreignColumnNameList, localTableName, option, true, false);
if (!table.containsColumn(localColumnNameList)) {
continue;
}
// check same-column self reference
final StringSet localColumnSet = StringSet.createAsFlexible();
localColumnSet.addAll(localColumnNameList);
final boolean selfReference = table.getTableDbName().equals(foreignTable.getTableDbName());
if (selfReference && localColumnSet.equalsUnderCharOption(foreignColumnSet)) {
continue;
}
// check same foreign key existence
final String fixedSuffix = option.getFixedSuffix();
final ForeignKey existingFK = table.findExistingForeignKey(foreignTableName, localColumnNameList, foreignColumnNameList, fixedSuffix);
if (existingFK != null) {
_log.info("The foreign key has already set up: " + foreignKeyName + "(" + fixedSuffix + ")");
reflectOptionToExistingFKIfNeeds(foreignKeyName, option, existingFK);
continue;
}
final String currentForeignKeyName = foreignKeyName + "_" + toConstraintPart(localTableName);
setupForeignKeyToTable(currentForeignKeyName, foreignTableName, foreignColumnNameList, table, localColumnNameList, option);
showResult(foreignTableName, foreignColumnNameList, table, localColumnNameList, option);
}
}
use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.
the class DfAdditionalForeignKeyInitializer method getLocalColumnNameList.
protected List<String> getLocalColumnNameList(Table table, String foreignKeyName, String foreignTableName, List<String> foreignColumnNameList, String localTableName, DfAdditionalForeignKeyOption option, boolean searchFromExistingFK, boolean errorIfNotFound) {
List<String> localColumnNameList = getLocalColumnNameList(foreignKeyName);
if (localColumnNameList != null && !localColumnNameList.isEmpty()) {
return localColumnNameList;
}
// searching from existing foreign key
if (searchFromExistingFK) {
final ForeignKey existingFK = table.findExistingForeignKey(foreignTableName, foreignColumnNameList, option.getFixedSuffix());
if (existingFK != null) {
return existingFK.getLocalColumnNameList();
}
}
// searching local columns by foreign columns (PK or UQ: PK when omitted)
localColumnNameList = DfCollectionUtil.newArrayList();
if (option.isFixedOnlyJoin()) {
// no need to search
return localColumnNameList;
}
for (String foreignColumnName : foreignColumnNameList) {
final Column column = table.getColumn(foreignColumnName);
if (column != null) {
// no check about same-column self reference here
localColumnNameList.add(column.getName());
continue;
}
if (errorIfNotFound) {
final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
br.addNotice("Not found the local column by the foreign column of additionalForeignKey.");
br.addItem("Advice");
br.addElement("When localColumnName is omitted, the local table should have");
br.addElement("the columns that are same as primary keys of foreign table.");
br.addItem("Additional FK");
br.addElement(foreignKeyName);
br.addElement(option);
br.addItem("Local Table");
br.addElement(localTableName);
br.addItem("Foreign Table");
br.addElement(foreignTableName);
br.addItem("Foreign Column");
br.addElement(foreignColumnNameList);
final String msg = br.buildExceptionMessage();
throw new DfPropertySettingColumnNotFoundException(msg);
} else {
// means not found
return DfCollectionUtil.newArrayList();
}
}
return localColumnNameList;
}
use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.
the class DfAdditionalForeignKeyInitializer method createAdditionalForeignKey.
protected ForeignKey createAdditionalForeignKey(String foreignKeyName, String foreignTableName, List<String> localColumnNameList, List<String> foreignColumnNameList, DfAdditionalForeignKeyOption option) {
final ForeignKey fk = new ForeignKey();
fk.setName(foreignKeyName);
if (foreignTableName.contains(".")) {
final String foreignSchema = Srl.substringLastFront(foreignTableName, ".");
final String foreignTablePureName = Srl.substringLastRear(foreignTableName, ".");
fk.setForeignSchema(UnifiedSchema.createAsDynamicSchema(foreignSchema));
fk.setForeignTablePureName(foreignTablePureName);
} else {
// main schema
fk.setForeignSchema(getDatabase().getDatabaseSchema());
fk.setForeignTablePureName(foreignTableName);
}
fk.addReference(localColumnNameList, foreignColumnNameList);
fk.setAdditionalForeignKey(true);
final String fixedCondition = option.getFixedCondition();
if (Srl.is_NotNull_and_NotTrimmedEmpty(fixedCondition)) {
fk.setFixedCondition(fixedCondition);
}
final String fixedSuffix = option.getFixedSuffix();
if (Srl.is_NotNull_and_NotTrimmedEmpty(fixedSuffix)) {
fk.setFixedSuffix(fixedSuffix);
}
fk.setFixedInline(option.isFixedInline());
fk.setFixedReferrer(option.isFixedReferrer());
fk.setFixedOnlyJoin(option.isFixedOnlyJoin());
final String comment = option.getComment();
if (Srl.is_NotNull_and_NotTrimmedEmpty(comment)) {
fk.setComment(comment);
}
return fk;
}
Aggregations