public static JoinCondition replaceViewReferences( QueryContext context,
JoinCondition joinCondition,
ColumnMapping mapping,
PlanNode node ) {
if (joinCondition instanceof EquiJoinCondition) {
EquiJoinCondition condition = (EquiJoinCondition)joinCondition;
SelectorName replacement1 = condition.getSelector1Name();
SelectorName replacement2 = condition.getSelector2Name();
String property1 = condition.getProperty1Name();
String property2 = condition.getProperty2Name();
if (replacement1.equals(mapping.getOriginalName())) {
Column sourceColumn = mapping.getMappedColumn(property1);
if (sourceColumn != null) {
replacement1 = sourceColumn.getSelectorName();
property1 = sourceColumn.getPropertyName();
}
}
if (replacement2.equals(mapping.getOriginalName())) {
Column sourceColumn = mapping.getMappedColumn(property2);
if (sourceColumn != null) {
replacement2 = sourceColumn.getSelectorName();
property2 = sourceColumn.getPropertyName();
}
}
if (replacement1 == condition.getSelector1Name() && replacement2 == condition.getSelector2Name()) return condition;
node.addSelector(replacement1, replacement2);
return new EquiJoinCondition(replacement1, property1, replacement2, property2);
}
// All the remaining conditions can only be rewritten if there is a single source ...
if (!mapping.isMappedToSingleSelector()) return joinCondition;
// There is only a single source ...
SelectorName viewName = mapping.getOriginalName();
SelectorName sourceName = mapping.getSingleMappedSelectorName();
if (joinCondition instanceof SameNodeJoinCondition) {
SameNodeJoinCondition condition = (SameNodeJoinCondition)joinCondition;
SelectorName replacement1 = condition.getSelector1Name();
SelectorName replacement2 = condition.getSelector2Name();
if (replacement1.equals(viewName)) replacement1 = sourceName;
if (replacement2.equals(viewName)) replacement2 = sourceName;
if (replacement1 == condition.getSelector1Name() && replacement2 == condition.getSelector2Name()) return condition;
node.addSelector(replacement1, replacement2);
if (condition.getSelector2Path() == null) return new SameNodeJoinCondition(replacement1, replacement2);
return new SameNodeJoinCondition(replacement1, replacement2, condition.getSelector2Path());
}
if (joinCondition instanceof ChildNodeJoinCondition) {
ChildNodeJoinCondition condition = (ChildNodeJoinCondition)joinCondition;
SelectorName childSelector = condition.getChildSelectorName();
SelectorName parentSelector = condition.getParentSelectorName();
if (childSelector.equals(viewName)) childSelector = sourceName;
if (parentSelector.equals(viewName)) parentSelector = sourceName;
if (childSelector == condition.getChildSelectorName() && parentSelector == condition.getParentSelectorName()) return condition;
node.addSelector(childSelector, parentSelector);
return new ChildNodeJoinCondition(parentSelector, childSelector);
}
if (joinCondition instanceof DescendantNodeJoinCondition) {
DescendantNodeJoinCondition condition = (DescendantNodeJoinCondition)joinCondition;
SelectorName ancestor = condition.getAncestorSelectorName();
SelectorName descendant = condition.getDescendantSelectorName();
if (ancestor.equals(viewName)) ancestor = sourceName;
if (descendant.equals(viewName)) descendant = sourceName;
if (ancestor == condition.getAncestorSelectorName() && descendant == condition.getDescendantSelectorName()) return condition;
node.addSelector(ancestor, descendant);
return new ChildNodeJoinCondition(ancestor, descendant);
}
return joinCondition;
}