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);
}
});
}
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);
});
}
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;
}
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;
}
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;
}
Aggregations