// add tables from where element set
if (whereElemNames != null) {
for (QueryElement qe : whereElemNames) {
MappingField fld = mapping.getFieldByLocalName(qe.getValue());
if (fld != null) {
DatabaseTable t = mapping.getTableByName(fld.getTableName());
if (t != null && !tables.contains(t) && !t.getName().equals(mapping.getDefaultTable())) {
tables.add(t);
}
}
}
}
// add tables from select element set
if (selectElemNames != null) {
for (QueryElement qe : selectElemNames) {
MappingField fld = mapping.getFieldByLocalName(qe.getValue());
if (fld != null) {
DatabaseTable t = mapping.getTableByName(fld.getTableName());
if (t != null && !tables.contains(t) && !t.getName().equals(mapping.getDefaultTable())) {
tables.add(t);
}
}
}
}
// the tables found may be joined on columns from tables we haven't found
// yet
// add additional required join tables
Set<DatabaseTable> moreTables = new HashSet<DatabaseTable>(tables);
for (DatabaseTable t : tables) {
DatabaseTable join = mapping.getTableByName(t.getDefaultTableJoin());
// recursively add all join tables until we get to either
// (a) the mapping default table (join == null)
// (b) or a table already found (moreTables.contains(join))
while (join != null && !moreTables.contains(join) && !join.getName().equals(mapping.getDefaultTable())) {
moreTables.add(join);
join = mapping.getTableByName(join.getDefaultTableJoin());
}
}
return moreTables;
}