Search in sources :

Example 46 with Table

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;
}
Also used : Column(org.apache.torque.engine.database.model.Column) DfCollectionUtil(org.dbflute.util.DfCollectionUtil) Predicate(java.util.function.Predicate) DfBuildProperties(org.dbflute.DfBuildProperties) DfBasicProperties(org.dbflute.properties.DfBasicProperties) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Database(org.apache.torque.engine.database.model.Database) List(java.util.List) Table(org.apache.torque.engine.database.model.Table) Map(java.util.Map) StringKeyMap(org.dbflute.helper.StringKeyMap) Table(org.apache.torque.engine.database.model.Table) Column(org.apache.torque.engine.database.model.Column) ArrayList(java.util.ArrayList) List(java.util.List)

Example 47 with Table

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;
}
Also used : Entry(java.util.Map.Entry) Table(org.apache.torque.engine.database.model.Table) HashMap(java.util.HashMap)

Example 48 with Table

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));
}
Also used : Table(org.apache.torque.engine.database.model.Table) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Comparator(java.util.Comparator)

Example 49 with Table

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;
}
Also used : Table(org.apache.torque.engine.database.model.Table) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 50 with Table

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;
}
Also used : Table(org.apache.torque.engine.database.model.Table) ArrayList(java.util.ArrayList) ForeignKey(org.apache.torque.engine.database.model.ForeignKey)

Aggregations

Table (org.apache.torque.engine.database.model.Table)58 Column (org.apache.torque.engine.database.model.Column)16 ArrayList (java.util.ArrayList)15 List (java.util.List)14 File (java.io.File)11 LinkedHashMap (java.util.LinkedHashMap)10 Map (java.util.Map)10 ForeignKey (org.apache.torque.engine.database.model.ForeignKey)9 HashSet (java.util.HashSet)8 DfDataTable (org.dbflute.helper.dataset.DfDataTable)8 Database (org.apache.torque.engine.database.model.Database)6 IOException (java.io.IOException)4 Entry (java.util.Map.Entry)4 DfBuildProperties (org.dbflute.DfBuildProperties)4 DfDataSet (org.dbflute.helper.dataset.DfDataSet)4 HashMap (java.util.HashMap)3 Function (java.util.function.Function)3 DfLReverseOutputResource (org.dbflute.logic.doc.lreverse.DfLReverseOutputResource)3 BufferedWriter (java.io.BufferedWriter)2 FileOutputStream (java.io.FileOutputStream)2