use of org.apache.torque.engine.database.model.Table in project dbflute-core by dbflute.
the class DfSPolicyCrossSecretary method getSameNameColumnListMap.
protected Map<String, List<Column>> getSameNameColumnListMap(Database database) {
if (_sameNameColumnListMap != null) {
return _sameNameColumnListMap;
}
// flexible map because column name is treated as flexible in DBFlute (so MEMBER_NAME and MemberName are same-name)
final Map<String, List<Column>> sameNameColumnListMap = StringKeyMap.createAsFlexible();
final List<Table> targetTableList = database.getTableList().stream().filter(tbl -> isTargetTable(tbl)).collect(Collectors.toList());
for (Table myTable : targetTableList) {
final List<Column> columnList = myTable.getColumnList();
for (Column myColumn : columnList) {
if (!isTargetColumn(myColumn)) {
// non-target
continue;
}
final String myName = myColumn.getName();
if (sameNameColumnListMap.containsKey(myName)) {
// registered by the other same-name column
continue;
}
for (Table yourTable : targetTableList) {
final Column yourColumn = yourTable.getColumn(myName);
if (yourColumn != null) {
if (!isTargetColumn(yourColumn)) {
// non-target
continue;
}
List<Column> sameColumnList = sameNameColumnListMap.get(myName);
if (sameColumnList == null) {
sameColumnList = new ArrayList<Column>();
sameColumnList.add(myColumn);
sameNameColumnListMap.put(myName, sameColumnList);
}
sameColumnList.add(yourColumn);
}
}
}
}
_sameNameColumnListMap = sameNameColumnListMap;
return _sameNameColumnListMap;
}
use of org.apache.torque.engine.database.model.Table in project dbflute-core by dbflute.
the class DfLReverseTableOrder method deriveMostName.
protected String deriveMostName(List<Table> tableList, boolean secondLevel) {
final String plusSuffix = "-plus";
final Map<String, Integer> prefixMap = new HashMap<String, Integer>();
for (Table table : tableList) {
final String tableName = table.getName();
final String prefix;
if (secondLevel) {
final String secondLevelPrefix = extractSecondLevelPrefix(tableName);
if (secondLevelPrefix != null) {
prefix = secondLevelPrefix;
} else {
prefix = extractFirstLevelPrefix(tableName);
}
} else {
prefix = extractFirstLevelPrefix(tableName);
}
final Integer size = prefixMap.get(prefix);
if (size != null) {
prefixMap.put(prefix, size + 1);
} else {
prefixMap.put(prefix, 1);
}
}
if (prefixMap.size() == 1) {
// no way because of process before
return prefixMap.keySet().iterator().next().toUpperCase() + plusSuffix;
} else if (prefixMap.size() >= 2) {
String mostPrefix = null;
Integer mostSize = 0;
for (Entry<String, Integer> entry : prefixMap.entrySet()) {
final Integer count = entry.getValue();
if (mostSize < count) {
mostPrefix = entry.getKey();
mostSize = count;
}
}
if (secondLevel && mostPrefix.contains("_")) {
final String firstLevelPrefix = extractFirstLevelPrefix(mostPrefix);
final Integer firstLevelSize = prefixMap.get(firstLevelPrefix);
if (firstLevelSize != null && firstLevelSize > 0) {
return firstLevelPrefix.toUpperCase() + (isFirstLevelGroup(tableList) ? "" : plusSuffix);
}
}
if (mostSize > 1 && mostSize > (tableList.size() / 2)) {
return mostPrefix.toUpperCase() + plusSuffix;
}
}
return null;
}
use of org.apache.torque.engine.database.model.Table in project dbflute-core by dbflute.
the class DfLReverseTableOrder method analyzeOrder.
// ===================================================================================
// Analyzer Order
// ==============
public List<List<Table>> analyzeOrder(List<Table> tableList, List<Table> skippedTableList) {
final Set<String> alreadyRegisteredSet = new HashSet<String>();
for (Table skippedTable : skippedTableList) {
// pure name here
alreadyRegisteredSet.add(skippedTable.getName());
}
final List<List<Table>> orderedList = new ArrayList<List<Table>>();
List<Table> unregisteredTableList;
{
final TreeSet<Table> allTableSet = new TreeSet<Table>(new Comparator<Table>() {
public int compare(Table o1, Table o2) {
// e.g. order, order_detail, order_detail_more, ...
return o1.getTableDbName().compareTo(o2.getTableDbName());
}
});
allTableSet.addAll(tableList);
unregisteredTableList = new ArrayList<Table>(allTableSet);
}
int level = 1;
while (true) {
final int beforeSize = unregisteredTableList.size();
unregisteredTableList = doAnalyzeOrder(unregisteredTableList, alreadyRegisteredSet, orderedList, level);
if (unregisteredTableList.isEmpty()) {
// happy end
break;
}
final int afterSize = unregisteredTableList.size();
if (beforeSize == afterSize) {
// means it cannot analyze more
if (level == 1) {
// level finished: next challenge, ignores additional foreign key
++level;
} else {
// level 2 finished: however unregistered tables exist
orderedList.add(unregisteredTableList);
// sadly end
break;
}
}
}
return groupingSize(groupingCategory(orderedList));
}
use of org.apache.torque.engine.database.model.Table in project dbflute-core by dbflute.
the class DfLReverseTableOrder method doGroupingCategory.
protected List<List<Table>> doGroupingCategory(List<List<Table>> outputOrderedList, boolean secondLevel) {
final int standardSize = STANDARD_SIZE;
final List<List<Table>> groupedList = new ArrayList<List<Table>>();
for (List<Table> tableList : outputOrderedList) {
if (secondLevel && (!isFirstLevelGroup(tableList) || tableList.size() <= standardSize)) {
groupedList.add(new ArrayList<Table>(tableList));
continue;
}
// as initial instance
List<Table> workTableList = new ArrayList<Table>();
String currentPrefix = null;
boolean inGroup = false;
for (Table table : tableList) {
final String tableName = table.getName();
if (currentPrefix != null) {
if (tableName.startsWith(currentPrefix)) {
// grouped
inGroup = true;
final int workSize = workTableList.size();
if (workSize >= 2) {
final Table requiredSizeBefore = workTableList.get(workSize - 2);
if (!requiredSizeBefore.getName().startsWith(currentPrefix)) {
// the work list has non-group elements at the front so split them
final Table groupBase = workTableList.remove(workSize - 1);
groupedList.add(workTableList);
workTableList = new ArrayList<Table>();
workTableList.add(groupBase);
}
}
workTableList.add(table);
} else {
if (inGroup) {
// switched
groupedList.add(workTableList);
workTableList = new ArrayList<Table>();
inGroup = false;
}
currentPrefix = null;
}
}
if (currentPrefix == null) {
if (secondLevel) {
currentPrefix = extractSecondLevelPrefix(tableName);
} else {
currentPrefix = extractFirstLevelPrefix(tableName);
}
workTableList.add(table);
}
}
if (!workTableList.isEmpty()) {
groupedList.add(workTableList);
}
}
assertAdjustmentBeforeAfter(outputOrderedList, groupedList);
return groupedList;
}
use of org.apache.torque.engine.database.model.Table 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;
}
Aggregations