Search in sources :

Example 36 with Table

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

the class DfClsDeploymentInitializer method initializeClassificationDeployment.

// ===================================================================================
// Initialize
// ==========
public Map<String, Map<String, String>> initializeClassificationDeployment(Database database) {
    // this should be called immediately after creating schema data
    // it should be called after additional-foreign-key initialization
    // so no modification for now
    _log.info("...Initializing ClassificationDeployment: project={}", database.getProjectName());
    // only overridden if called with same database
    if (_allColumnClassificationMap != null) {
        final List<Table> tableList = database.getTableList();
        for (Table table : tableList) {
            final Map<String, String> columnClsMap = getColumnClsMap(_existingDeploymentMap, table.getTableDbName());
            for (Entry<String, String> entry : _allColumnClassificationMap.entrySet()) {
                columnClsMap.put(entry.getKey(), entry.getValue());
            }
        }
    }
    _classificationDefinitionInitializer.process();
    for (Entry<String, DfClassificationElement> entry : _tableClassificationMap.entrySet()) {
        final DfClassificationElement element = entry.getValue();
        if (element.getClassificationTop().isSuppressAutoDeploy()) {
            continue;
        }
        final Map<String, String> columnClsMap = getColumnClsMap(_existingDeploymentMap, element.getTable());
        final String classificationName = element.getClassificationName();
        registerColumnClsIfNeeds(columnClsMap, element.getCode(), classificationName);
        final Table table = database.getTable(element.getTable());
        if (table == null || table.hasCompoundPrimaryKey()) {
            continue;
        }
        final Column column = table.getColumn(element.getCode());
        if (column == null || !column.isPrimaryKey()) {
            continue;
        }
        final List<ForeignKey> referrers = column.getReferrers();
        for (ForeignKey referrer : referrers) {
            if (!referrer.isSimpleKeyFK()) {
                continue;
            }
            final Table referrerTable = referrer.getTable();
            final String referrerTableDbName = referrerTable.getTableDbName();
            final Map<String, String> referrerClsMap = getColumnClsMap(_existingDeploymentMap, referrerTableDbName);
            final Column localColumnAsOne = referrer.getLocalColumnAsOne();
            registerColumnClsIfNeeds(referrerClsMap, localColumnAsOne.getName(), classificationName);
        }
    }
    return _existingDeploymentMap;
}
Also used : DfClassificationElement(org.dbflute.properties.assistant.classification.DfClassificationElement) Table(org.apache.torque.engine.database.model.Table) Column(org.apache.torque.engine.database.model.Column) ForeignKey(org.apache.torque.engine.database.model.ForeignKey)

Example 37 with Table

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

the class DfTableNameProp method outputTableNameMap.

public void outputTableNameMap(String baseDir, Map<String, Table> tableNameMap) {
    final Map<String, String> map = new LinkedHashMap<String, String>();
    for (Entry<String, Table> entry : tableNameMap.entrySet()) {
        final String sheetName = entry.getKey();
        final Table table = entry.getValue();
        map.put(sheetName, table.getTableSqlName());
    }
    final String mapString = new MapListString().buildMapString(map);
    final File dataPropFile = new File(baseDir + "/tableNameMap.dataprop");
    BufferedWriter bw = null;
    try {
        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dataPropFile), "UTF-8"));
        bw.write(mapString);
        bw.flush();
    } catch (IOException e) {
        String msg = "Failed to write tableNameMap.dataprop: " + dataPropFile;
        throw new IllegalStateException(msg, e);
    } finally {
        if (bw != null) {
            try {
                bw.close();
            } catch (IOException ignored) {
            }
        }
    }
}
Also used : MapListString(org.dbflute.helper.mapstring.MapListString) Table(org.apache.torque.engine.database.model.Table) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) MapListString(org.dbflute.helper.mapstring.MapListString) IOException(java.io.IOException) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) BufferedWriter(java.io.BufferedWriter)

Example 38 with Table

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

the class DfLReverseOutputHandler method outputDelimiterData.

// ===================================================================================
// Delimiter Data
// ==============
protected void outputDelimiterData(final Table table, DfLReverseDataResult templateDataResult, final int limit, DfLReverseOutputResource resource, int sheetNumber, List<String> sectionInfoList) {
    if (_delimiterDataDir == null) {
        return;
    }
    final File delimiterDir = new File(_delimiterDataDir);
    // fixed
    final String ext = "tsv";
    final String outputInfo = "...Outputting the over-xls-limit table: " + table.getTableDispName();
    _log.info(outputInfo);
    sectionInfoList.add(outputInfo);
    if (!delimiterDir.exists()) {
        delimiterDir.mkdirs();
    }
    final FileToken fileToken = new FileToken();
    // file name uses DB name (no display name) just in case
    final String delimiterFilePath = buildDelimiterFilePath(table, resource, sheetNumber, delimiterDir, ext);
    final List<String> columnNameList = new ArrayList<String>();
    for (Column column : table.getColumnList()) {
        if (!_containsCommonColumn && column.isCommonColumn()) {
            continue;
        }
        columnNameList.add(column.getName());
    }
    final DfJFadCursorCallback cursorCallback = templateDataResult.getCursorCallback();
    cursorCallback.select(new DfJFadCursorHandler() {

        int count = 0;

        public void handle(final DfJFadResultSetWrapper wrapper) {
            try {
                fileToken.make(delimiterFilePath, new FileMakingCallback() {

                    public void write(FileMakingRowWriter writer) throws IOException, SQLException {
                        while (wrapper.next()) {
                            if (limit >= 0 && limit < count) {
                                break;
                            }
                            final List<String> valueList = new ArrayList<String>();
                            for (String columnName : columnNameList) {
                                valueList.add(wrapper.getString(columnName));
                            }
                            writer.writeRow(valueList);
                            ++count;
                        }
                    }
                }, op -> op.encodeAsUTF8().separateByLf().delimitateByTab().headerInfo(columnNameList));
            } catch (IOException e) {
                handleDelimiterDataFailureException(table, delimiterFilePath, e);
            }
            final String delimiterInfo = " -> " + delimiterFilePath + " (" + count + ")";
            _log.info(delimiterInfo);
            sectionInfoList.add(delimiterInfo);
        }
    });
}
Also used : DfJFadCursorHandler(org.dbflute.helper.jdbc.facade.DfJFadCursorHandler) Column(org.apache.torque.engine.database.model.Column) DfTableXlsWriter(org.dbflute.helper.io.xls.DfTableXlsWriter) LoggerFactory(org.slf4j.LoggerFactory) DfJFadResultSetWrapper(org.dbflute.helper.jdbc.facade.DfJFadResultSetWrapper) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) Table(org.apache.torque.engine.database.model.Table) Map(java.util.Map) DfLittleAdjustmentProperties(org.dbflute.properties.DfLittleAdjustmentProperties) DataSource(javax.sql.DataSource) DfDataTable(org.dbflute.helper.dataset.DfDataTable) DfDataSet(org.dbflute.helper.dataset.DfDataSet) FileMakingRowWriter(org.dbflute.helper.token.file.FileMakingRowWriter) Logger(org.slf4j.Logger) DfJFadCursorCallback(org.dbflute.helper.jdbc.facade.DfJFadCursorCallback) FileToken(org.dbflute.helper.token.file.FileToken) FileMakingCallback(org.dbflute.helper.token.file.FileMakingCallback) Set(java.util.Set) IOException(java.io.IOException) DfBuildProperties(org.dbflute.DfBuildProperties) File(java.io.File) DfDtsColumnTypes(org.dbflute.helper.dataset.types.DfDtsColumnTypes) List(java.util.List) Entry(java.util.Map.Entry) DfDataRow(org.dbflute.helper.dataset.DfDataRow) DfAdditionalTableProperties(org.dbflute.properties.DfAdditionalTableProperties) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileMakingCallback(org.dbflute.helper.token.file.FileMakingCallback) DfJFadResultSetWrapper(org.dbflute.helper.jdbc.facade.DfJFadResultSetWrapper) Column(org.apache.torque.engine.database.model.Column) DfJFadCursorHandler(org.dbflute.helper.jdbc.facade.DfJFadCursorHandler) FileMakingRowWriter(org.dbflute.helper.token.file.FileMakingRowWriter) DfJFadCursorCallback(org.dbflute.helper.jdbc.facade.DfJFadCursorCallback) File(java.io.File) FileToken(org.dbflute.helper.token.file.FileToken)

Example 39 with Table

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

Example 40 with Table

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

the class DfTableOrderAnalyzer 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)

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