}
ExpressionBuilder builder = new ExpressionBuilder();
Expression joinExpression = getQueryManager().getMultipleTableJoinExpression();
for (int index = getTables().size() - additionalTablesSize; index < getTables().size();
index++) {
DatabaseTable table = (DatabaseTable)getTables().elementAt(index);
Map oldKeyMapping = (Map)getAdditionalTablePrimaryKeyFields().get(table);
if (oldKeyMapping != null) {
if (!getQueryManager().hasCustomMultipleTableJoinExpression()) {
// Build the multiple table join expression resulting from the fk relationships.
for (Iterator enumtr = oldKeyMapping.keySet().iterator(); enumtr.hasNext();) {
DatabaseField sourceTableField = (DatabaseField)enumtr.next();
DatabaseField targetTableField = (DatabaseField)oldKeyMapping.get(sourceTableField);
DatabaseTable sourceTable = sourceTableField.getTable();
DatabaseTable targetTable = targetTableField.getTable();
// Must add this field to read, so translations work on database row, this could be either.
if (!getFields().contains(sourceTableField)) {
getFields().addElement(sourceTableField);
}
if (!getFields().contains(targetTableField)) {
getFields().addElement(targetTableField);
}
Expression keyJoinExpression = builder.getField(targetTableField).equal(builder.getField(sourceTableField));
joinExpression = keyJoinExpression.and(joinExpression);
getQueryManager().getTablesJoinExpressions().put(targetTable, keyJoinExpression);
if(isChild) {
getInheritancePolicy().addChildTableJoinExpressionToAllParents(targetTable, keyJoinExpression);
}
}
}
} else {
// If the user has specified a custom multiple table join then we do not assume that the secondary tables have identically named pk as the primary table.
// No additional fk info was specified so assume the pk field(s) are the named the same in the additional table.
Map newKeyMapping = new HashMap(getPrimaryKeyFields().size() + 1);
getAdditionalTablePrimaryKeyFields().put(table, newKeyMapping);
// For each primary key field in the primary table, add a pk relationship from the primary table's pk field to the assumed identically named secondary pk field.
List primaryKeyFields = getPrimaryKeyFields();
for (int pkIndex = 0; pkIndex < primaryKeyFields.size(); pkIndex++) {
DatabaseField primaryKeyField = (DatabaseField)primaryKeyFields.get(pkIndex);
DatabaseField secondaryKeyField = (DatabaseField)primaryKeyField.clone();
secondaryKeyField.setTable(table);
newKeyMapping.put(primaryKeyField, secondaryKeyField);
// Must add this field to read, so translations work on database row.
getFields().addElement(secondaryKeyField);
if (!getQueryManager().hasCustomMultipleTableJoinExpression()) {
Expression keyJoinExpression = builder.getField(secondaryKeyField).equal(builder.getField(primaryKeyField));
joinExpression = keyJoinExpression.and(joinExpression);
getQueryManager().getTablesJoinExpressions().put(table, keyJoinExpression);
if(isChild) {
getInheritancePolicy().addChildTableJoinExpressionToAllParents(table, keyJoinExpression);
}
}
}
}
}
if (joinExpression != null) {
getQueryManager().setInternalMultipleTableJoinExpression(joinExpression);
}
if (getQueryManager().hasCustomMultipleTableJoinExpression()) {
Map tablesJoinExpressions = SQLSelectStatement.mapTableToExpression(joinExpression, getTables());
getQueryManager().getTablesJoinExpressions().putAll(tablesJoinExpressions);
if(isChild) {
for (int index = getTables().size() - additionalTablesSize; index < getTables().size();
index++) {
DatabaseTable table = (DatabaseTable)getTables().elementAt(index);
getInheritancePolicy().addChildTableJoinExpressionToAllParents(table, (Expression)tablesJoinExpressions.get(table));
}
}
}
}