{
CROM rootTable=root.getChild(0);
if(rootTable==null){
String err="No root table in the source.";
if(debug_log!=null)writeLog(err);
throw new ExternalDatabaseException(err);
}
populateChildTablesMap(rootTable);
if(StringUtils.isNotEmpty(adHocFilter)){
modifyRootTableFilter(rootTable, adHocFilter);
}
try {
boolean isFlat=root.isFlat();
for(CROM table: root.getChildren()){
if(table.isElementNode()){
List<CROM> childTables = childTablesMap.get(table);
sql = buildSqlForTable(table);
int nField0=nField;
if(debug_log!=null)writeLog("SQL for table \"" + table.getName() + "\": " + sql);
PreparedStatement stmt = null;
boolean statementMustBeClosedByMe = true;
ResultSet rs = null;
try {
PreparedStatementCache statementCache = getStatementCacheForTable(table);
stmt = statementCache.getPreparedStatement(sql);
statementMustBeClosedByMe = statementCache.mustLastReturnedStatementBeClosedByCaller();
if(debug_log!=null)writeLog("About to execute query.");
rs = stmt.executeQuery();
if(debug_log!=null)writeLog("Done executing query.");
int numRecordOfParent=0;
while(rs.next()){
Record record = createRowRecord(table, nField0, rs);
if(debug_log!=null)writeLog("ADD - "+record.toString());
numRecordOfParent++;
if(debug_log!=null)writeLog(String.format("%8d rows of table %s added to the parent table %s\n", numRecordOfParent, table.getName(), root.getName()));
if (!childTables.isEmpty()) {
// Note that elements may be removed from the records list by
// createSourceTreeForTable
List<Record>records = Lists.newArrayList(record);
createSourceTreeForTable(table, childTables, records);
checkInnerJoin(records);
if(records.size()==0)continue;
}
// this records contains one or zero record
record.writeToText(writer, isFlat);
}
}
finally {
KongaDbUtils.close(rs);
if (statementMustBeClosedByMe) {
KongaDbUtils.close(stmt);
}
}
}
}
}
catch (SQLException e) {
String err="SQL:"+sql+"\n"+e.getMessage();
writeLog(err);
throw new ExternalDatabaseException(err);
}
}