/**
* INTERNAL:
* Build the selection criteria to join the source, relation, and target tables.
*/
public Expression buildSelectionCriteriaAndAddFieldsToQueryInternal(ForeignReferenceMapping mapping, Expression criteria, boolean shouldAddTargetFields, boolean shouldAddFieldsToQuery) {
Expression builder = new ExpressionBuilder();
Expression linkTable = builder.getTable(this.relationTable);
if (shouldAddTargetFields) {
Iterator<DatabaseField> targetKeyIterator = getTargetKeyFields().iterator();
Iterator<DatabaseField> relationKeyIterator = getTargetRelationKeyFields().iterator();
while (targetKeyIterator.hasNext()) {
DatabaseField relationKey = relationKeyIterator.next();
DatabaseField targetKey = targetKeyIterator.next();
Expression expression = builder.getField(targetKey).equal(linkTable.getField(relationKey));
if (criteria == null) {
criteria = expression;
} else {
criteria = expression.and(criteria);
}
}
}
Iterator<DatabaseField> relationKeyIterator = getSourceRelationKeyFields().iterator();
Iterator<DatabaseField> sourceKeyIterator = getSourceKeyFields().iterator();
while (relationKeyIterator.hasNext()) {
DatabaseField relationKey = relationKeyIterator.next();
DatabaseField sourceKey = sourceKeyIterator.next();
Expression expression = linkTable.getField(relationKey).equal(builder.getParameter(sourceKey));
if (criteria == null) {
criteria = expression;
} else {
criteria = expression.and(criteria);
}