Search in sources :

Example 1 with ForeignKey

use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.

the class DfLReverseDataExtractor method buildOrderByClause.

protected String buildOrderByClause(Table table) {
    final ForeignKey selfReferenceFK = table.getSelfReferenceForeignKey();
    final String orderBy;
    if (selfReferenceFK != null && selfReferenceFK.isSimpleKeyFK()) {
        final Column firstColumn = table.getColumn(selfReferenceFK.getFirstLocalColumnName());
        final String firstName = firstColumn.getColumnSqlNameDirectUse();
        orderBy = buildOrderByNullsFirst(firstName);
    } else {
        orderBy = "";
    }
    return orderBy;
}
Also used : Column(org.apache.torque.engine.database.model.Column) ForeignKey(org.apache.torque.engine.database.model.ForeignKey)

Example 2 with ForeignKey

use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.

the class DfTableOrderAnalyzer method groupingSize.

protected List<List<Table>> groupingSize(List<List<Table>> outputOrderedList) {
    final int standardSize = STANDARD_SIZE;
    final List<List<Table>> groupedList = new ArrayList<List<Table>>();
    for (List<Table> tableList : outputOrderedList) {
        final int tableSize = tableList.size();
        if (!groupedList.isEmpty() && tableSize < standardSize) {
            // handle only-one table
            if (tableSize == 1) {
                final Table onlyOneTable = tableList.get(0);
                final List<ForeignKey> foreignKeyList = onlyOneTable.getForeignKeyList();
                final Set<String> foreignTableSet = new HashSet<String>();
                for (ForeignKey fk : foreignKeyList) {
                    if (!fk.hasFixedCondition()) {
                        // ignore fixed condition
                        foreignTableSet.add(fk.getForeignTablePureName());
                    }
                }
                List<Table> candidatePreviousList = null;
                for (int i = groupedList.size() - 1; i >= 0; --i) {
                    // reverse loop
                    final List<Table> previousList = groupedList.get(i);
                    boolean existsFK = false;
                    for (Table previousTable : previousList) {
                        if (foreignTableSet.contains(previousTable.getName())) {
                            existsFK = true;
                        }
                    }
                    if (!isFirstLevelGroup(previousList) && previousList.size() < standardSize) {
                        // not group and small
                        candidatePreviousList = previousList;
                    }
                    if (existsFK) {
                        break;
                    }
                }
                if (candidatePreviousList != null) {
                    candidatePreviousList.add(onlyOneTable);
                    continue;
                }
            }
            // join small sections
            final List<Table> lastList = groupedList.get(groupedList.size() - 1);
            if (isSecondLevelGroup(lastList) && isSecondLevelGroup(tableList)) {
            }
            if (isJoinSmallSection(lastList, tableList)) {
                lastList.addAll(tableList);
                continue;
            }
        }
        // needs new list to manipulate
        groupedList.add(new ArrayList<Table>(tableList));
    }
    assertAdjustmentBeforeAfter(outputOrderedList, groupedList);
    return groupedList;
}
Also used : Table(org.apache.torque.engine.database.model.Table) ArrayList(java.util.ArrayList) ForeignKey(org.apache.torque.engine.database.model.ForeignKey) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 3 with ForeignKey

use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.

the class DfLReverseTableOrder method groupingSize.

protected List<List<Table>> groupingSize(List<List<Table>> outputOrderedList) {
    final int standardSize = STANDARD_SIZE;
    final List<List<Table>> groupedList = new ArrayList<List<Table>>();
    for (List<Table> tableList : outputOrderedList) {
        final int tableSize = tableList.size();
        if (!groupedList.isEmpty() && tableSize < standardSize) {
            // handle only-one table
            if (tableSize == 1) {
                final Table onlyOneTable = tableList.get(0);
                final List<ForeignKey> foreignKeyList = onlyOneTable.getForeignKeyList();
                final Set<String> foreignTableSet = new HashSet<String>();
                for (ForeignKey fk : foreignKeyList) {
                    if (!fk.hasFixedCondition()) {
                        // ignore fixed condition
                        foreignTableSet.add(fk.getForeignTablePureName());
                    }
                }
                List<Table> candidatePreviousList = null;
                for (int i = groupedList.size() - 1; i >= 0; --i) {
                    // reverse loop
                    final List<Table> previousList = groupedList.get(i);
                    boolean existsFK = false;
                    for (Table previousTable : previousList) {
                        if (foreignTableSet.contains(previousTable.getName())) {
                            existsFK = true;
                        }
                    }
                    if (!isFirstLevelGroup(previousList) && previousList.size() < standardSize) {
                        // not group and small
                        candidatePreviousList = previousList;
                    }
                    if (existsFK) {
                        break;
                    }
                }
                if (candidatePreviousList != null) {
                    candidatePreviousList.add(onlyOneTable);
                    continue;
                }
            }
            // join small sections
            final List<Table> lastList = groupedList.get(groupedList.size() - 1);
            if (isSecondLevelGroup(lastList) && isSecondLevelGroup(tableList)) {
            }
            if (isJoinSmallSection(lastList, tableList)) {
                lastList.addAll(tableList);
                continue;
            }
        }
        // needs new list to manipulate
        groupedList.add(new ArrayList<Table>(tableList));
    }
    assertAdjustmentBeforeAfter(outputOrderedList, groupedList);
    return groupedList;
}
Also used : Table(org.apache.torque.engine.database.model.Table) ArrayList(java.util.ArrayList) ForeignKey(org.apache.torque.engine.database.model.ForeignKey) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 4 with ForeignKey

use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.

the class DfLReverseDataExtractor method buildSelfReferenceExtractingTrySqlList.

// ===================================================================================
// Extraction SQL
// ==============
// -----------------------------------------------------
// Self Reference
// --------------
protected OptionalThing<List<String>> buildSelfReferenceExtractingTrySqlList(Table table) {
    final ForeignKey selfReferenceFK = table.getSelfReferenceForeignKey();
    if (selfReferenceFK == null || !selfReferenceFK.isSimpleKeyFK()) {
        return OptionalThing.empty();
    }
    final String tableSqlName = table.getTableSqlNameDirectUse();
    final Column firstLocalColumn = table.getColumn(selfReferenceFK.getFirstLocalColumnName());
    final String firstLocalName = firstLocalColumn.getColumnSqlNameDirectUse();
    final Column firstForeignColumn = table.getColumn(selfReferenceFK.getFirstForeignColumnName());
    final String firstForeignName = firstForeignColumn.getColumnSqlNameDirectUse();
    final List<String> sqlList = DfCollectionUtil.newArrayList();
    sqlList.add(doBuildSelfReferenceRecursiveSql(table, tableSqlName, firstLocalName, firstForeignName));
    sqlList.add(doBuildSelfReferenceJoinJoinSql(table, tableSqlName, firstLocalName, firstForeignName));
    return OptionalThing.of(sqlList);
}
Also used : Column(org.apache.torque.engine.database.model.Column) ForeignKey(org.apache.torque.engine.database.model.ForeignKey)

Example 5 with ForeignKey

use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.

the class DfLReverseDataExtractor method buildOrderByClause.

protected String buildOrderByClause(Table table) {
    final ForeignKey selfReferenceFK = table.getSelfReferenceForeignKey();
    final String orderBy;
    if (selfReferenceFK != null && selfReferenceFK.isSimpleKeyFK()) {
        final Column firstColumn = table.getColumn(selfReferenceFK.getFirstLocalColumnName());
        final String firstName = firstColumn.getColumnSqlNameDirectUse();
        orderBy = buildOrderByNullsFirst(firstName);
    } else {
        orderBy = "";
    }
    return orderBy;
}
Also used : Column(org.apache.torque.engine.database.model.Column) ForeignKey(org.apache.torque.engine.database.model.ForeignKey)

Aggregations

ForeignKey (org.apache.torque.engine.database.model.ForeignKey)15 Table (org.apache.torque.engine.database.model.Table)9 Column (org.apache.torque.engine.database.model.Column)8 ArrayList (java.util.ArrayList)4 List (java.util.List)3 HashSet (java.util.HashSet)2 Function (java.util.function.Function)1 Index (org.apache.torque.engine.database.model.Index)1 Unique (org.apache.torque.engine.database.model.Unique)1 DfIllegalPropertySettingException (org.dbflute.exception.DfIllegalPropertySettingException)1 DfPropertySettingColumnNotFoundException (org.dbflute.exception.DfPropertySettingColumnNotFoundException)1 StringSet (org.dbflute.helper.StringSet)1 ExceptionMessageBuilder (org.dbflute.helper.message.ExceptionMessageBuilder)1 DfSPolicyStatement (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement)1 DfSPolicyIfPart (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyIfPart)1 DfSPolicyThenClause (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenClause)1 DfSPolicyThenPart (org.dbflute.logic.doc.spolicy.parsed.DfSPolicyStatement.DfSPolicyThenPart)1 DfSPolicyResult (org.dbflute.logic.doc.spolicy.result.DfSPolicyResult)1 DfSPolicyFirstDateSecretary (org.dbflute.logic.doc.spolicy.secretary.DfSPolicyFirstDateSecretary)1 DfSPolicyLogicalSecretary (org.dbflute.logic.doc.spolicy.secretary.DfSPolicyLogicalSecretary)1