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