Search in sources :

Example 31 with Column

use of org.apache.torque.engine.database.model.Column 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 32 with Column

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

the class DfSPolicyTableThemeChecker method prepareTableTheme.

// ===================================================================================
// Theme Logic
// ===========
protected void prepareTableTheme(Map<String, BiConsumer<Table, DfSPolicyResult>> themeMap) {
    // e.g.
    // ; tableMap = map:{
    // ; themeList = list:{ hasPK ; upperCaseBasis ; identityIfPureIDPK }
    // }
    define(themeMap, "hasPK", table -> !table.hasPrimaryKey(), table -> {
        return "The table should have primary key: " + toTableDisp(table);
    });
    define(themeMap, "upperCaseBasis", table -> Srl.isLowerCaseAny(toComparingTableName(table)), table -> {
        return "The table name should be on upper case basis: " + toTableDisp(table);
    });
    define(themeMap, "lowerCaseBasis", table -> Srl.isUpperCaseAny(toComparingTableName(table)), table -> {
        return "The table name should be on lower case basis: " + toTableDisp(table);
    });
    define(themeMap, "identityIfPureIDPK", table -> {
        if (table.hasPrimaryKey() && table.hasSinglePrimaryKey()) {
            final Column pk = table.getPrimaryKeyAsOne();
            return !pk.isForeignKey() && Srl.endsWith(pk.getName(), "ID") && !pk.isIdentity();
        } else {
            return false;
        }
    }, table -> {
        return "The primary key should be identity: " + toTableDisp(table) + "." + table.getPrimaryKeyAsOne().getName();
    });
    define(themeMap, "hasCommonColumn", table -> !table.hasAllCommonColumn(), table -> {
        return "The table should have common columns: " + toTableDisp(table);
    });
    define(themeMap, "hasAlias", table -> !table.hasAlias(), table -> {
        return "The table should have table alias: " + toTableDisp(table);
    });
    define(themeMap, "hasComment", table -> !table.hasComment(), table -> {
        return "The table should have table comment: " + toTableDisp(table);
    });
}
Also used : Column(org.apache.torque.engine.database.model.Column)

Example 33 with Column

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

the class DfSPolicyTableStatementChecker method convertToConstraintNameComparingValue.

protected String convertToConstraintNameComparingValue(Table table, String thenValue, List<Column> columnList) {
    final String tableName = toComparingTableName(table);
    String comparingValue = thenValue;
    // @since 1.1.9
    comparingValue = replaceComparingValue(comparingValue, "tableName", tableName, /*suppressUpper*/
    true);
    // facade style, @since first
    comparingValue = replaceComparingValue(comparingValue, "table", tableName);
    if (!columnList.isEmpty()) {
        // no way, just in case
        // for MySQL only-one column unique constraint @since 1.2.3
        // e.g. then uniqueName is UQ_$$tableName$$ or $$first_columnName$$
        final Column firstColumn = columnList.get(0);
        comparingValue = replaceComparingValue(comparingValue, "first_columnName", firstColumn.getName(), /*suppressUpper*/
        true);
    }
    return comparingValue;
}
Also used : Column(org.apache.torque.engine.database.model.Column)

Example 34 with Column

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

the class DfSPolicyCrossSecretary method determineSameWhatIfSameColumnAlias.

protected String determineSameWhatIfSameColumnAlias(Column myColumn, Predicate<Column> yourTargeting, Function<Column, Object> valueProvider, boolean ignoreEmpty) {
    if (!myColumn.hasAlias()) {
        // cannot determine (possible when from statement)
        return null;
    }
    final Map<String, List<Column>> columnListMap = getSameAliasColumnListMap(myColumn.getTable().getDatabase());
    final List<Column> columnList = columnListMap.getOrDefault(myColumn.getName(), DfCollectionUtil.emptyList());
    for (Column yourColumn : columnList) {
        // alias always exists here
        if (myColumn.equals(yourColumn)) {
            // myself
            continue;
        }
        if (!yourTargeting.test(yourColumn)) {
            // non-target (e.g. by statement)
            continue;
        }
        final Object myValue = valueProvider.apply(myColumn);
        final Object yourValue = valueProvider.apply(yourColumn);
        if (ignoreEmpty && eitherEmpty(myValue, yourValue)) {
            continue;
        }
        if (!isEqual(myValue, yourValue)) {
            // different in spite of same column alias
            return toColumnExp(myColumn) + "=" + myValue + ", " + toColumnExp(yourColumn) + "=" + yourValue;
        }
    }
    return null;
// memorable code before performance tuning
// final String myColumnAlias = myColumn.getAlias();
// final Table myTable = myColumn.getTable();
// for (Table yourTable : myTable.getDatabase().getTableList()) {
// if (!isTargetTable(yourTable)) { // non-target
// continue;
// }
// if (myTable.equals(yourTable)) { // myself
// continue;
// }
// final List<Column> yourColumnList = yourTable.getColumnList();
// for (Column yourColumn : yourColumnList) {
// if (!isTargetColumn(yourColumn)) { // non-target
// continue;
// }
// if (!yourTargeting.test(yourColumn)) { // non-target (e.g. by statement)
// continue;
// }
// if (!yourColumn.hasAlias()) { // cannot determine
// continue;
// }
// if (myColumnAlias.equals(yourColumn.getAlias())) { // same column alias
// final Object myValue = valueProvider.apply(myColumn);
// final Object yourValue = valueProvider.apply(yourColumn);
// if (ignoreEmpty && eitherEmpty(myValue, yourValue)) {
// continue;
// }
// if (!isEqual(myValue, yourValue)) { // different in spite of same column alias
// return toColumnExp(myColumn) + "=" + myValue + ", " + toColumnExp(yourColumn) + "=" + yourValue;
// }
// }
// }
// }
// return null;
}
Also used : Column(org.apache.torque.engine.database.model.Column) ArrayList(java.util.ArrayList) List(java.util.List)

Example 35 with Column

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

the class DfSPolicyCrossSecretary method getSameAliasColumnListMap.

protected Map<String, List<Column>> getSameAliasColumnListMap(Database database) {
    if (_sameAliasColumnListMap != null) {
        return _sameAliasColumnListMap;
    }
    // case insensitive (not flexible), alias handling rule does not exist in DBFlute
    // but flexible is hard to implement and small merit
    final Map<String, List<Column>> sameAliasColumnListMap = StringKeyMap.createAsCaseInsensitive();
    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;
            }
            if (!myColumn.hasAlias()) {
                // cannot determine
                continue;
            }
            final String myAlias = myColumn.getAlias();
            if (sameAliasColumnListMap.containsKey(myAlias)) {
                // registered by the other same-name column
                continue;
            }
            for (Table yourTable : targetTableList) {
                List<Column> yourColumnList = yourTable.getColumnList();
                Column yourColumn = null;
                for (Column currentColumn : yourColumnList) {
                    if (currentColumn.hasAlias() && myAlias.equalsIgnoreCase(currentColumn.getAlias())) {
                        yourColumn = currentColumn;
                        break;
                    }
                }
                if (yourColumn != null) {
                    if (!isTargetColumn(yourColumn)) {
                        // non-target
                        continue;
                    }
                    if (!yourColumn.hasAlias()) {
                        // cannot determine
                        continue;
                    }
                    List<Column> sameColumnList = sameAliasColumnListMap.get(myAlias);
                    if (sameColumnList == null) {
                        sameColumnList = new ArrayList<Column>();
                        sameColumnList.add(myColumn);
                        sameAliasColumnListMap.put(myAlias, sameColumnList);
                    }
                    sameColumnList.add(yourColumn);
                }
            }
        }
    }
    _sameAliasColumnListMap = sameAliasColumnListMap;
    return _sameAliasColumnListMap;
}
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)

Aggregations

Column (org.apache.torque.engine.database.model.Column)50 Table (org.apache.torque.engine.database.model.Table)16 List (java.util.List)10 Map (java.util.Map)8 ForeignKey (org.apache.torque.engine.database.model.ForeignKey)8 ArrayList (java.util.ArrayList)6 LinkedHashMap (java.util.LinkedHashMap)6 Database (org.apache.torque.engine.database.model.Database)5 DfBuildProperties (org.dbflute.DfBuildProperties)4 DfDataRow (org.dbflute.helper.dataset.DfDataRow)4 DfDataTable (org.dbflute.helper.dataset.DfDataTable)4 ExceptionMessageBuilder (org.dbflute.helper.message.ExceptionMessageBuilder)4 DfLanguageDependency (org.dbflute.logic.generate.language.DfLanguageDependency)4 Function (java.util.function.Function)3 File (java.io.File)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 Entry (java.util.Map.Entry)2 Set (java.util.Set)2 Predicate (java.util.function.Predicate)2