public static JoinCondition replaceViewReferences( QueryContext context,
JoinCondition joinCondition,
ColumnMapping mapping,
PlanNode node ) {
if (joinCondition instanceof EquiJoinCondition) {
EquiJoinCondition condition = (EquiJoinCondition)joinCondition;
SelectorName replacement1 = condition.selector1Name();
SelectorName replacement2 = condition.selector2Name();
String property1 = condition.getProperty1Name();
String property2 = condition.getProperty2Name();
if (replacement1.equals(mapping.getOriginalName())) {
Column sourceColumn = mapping.getMappedColumn(property1);
if (sourceColumn != null) {
replacement1 = sourceColumn.selectorName();
property1 = sourceColumn.getPropertyName();
}
}
if (replacement2.equals(mapping.getOriginalName())) {
Column sourceColumn = mapping.getMappedColumn(property2);
if (sourceColumn != null) {
replacement2 = sourceColumn.selectorName();
property2 = sourceColumn.getPropertyName();
}
}
if (replacement1 == condition.selector1Name() && replacement2 == condition.selector2Name()) 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.selector1Name();
SelectorName replacement2 = condition.selector2Name();
if (replacement1.equals(viewName)) replacement1 = sourceName;
if (replacement2.equals(viewName)) replacement2 = sourceName;
if (replacement1 == condition.selector1Name() && replacement2 == condition.selector2Name()) 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.childSelectorName();
SelectorName parentSelector = condition.parentSelectorName();
if (childSelector.equals(viewName)) childSelector = sourceName;
if (parentSelector.equals(viewName)) parentSelector = sourceName;
if (childSelector == condition.childSelectorName() && parentSelector == condition.parentSelectorName()) return condition;
node.addSelector(childSelector, parentSelector);
return new ChildNodeJoinCondition(parentSelector, childSelector);
}
if (joinCondition instanceof DescendantNodeJoinCondition) {
DescendantNodeJoinCondition condition = (DescendantNodeJoinCondition)joinCondition;
SelectorName ancestor = condition.ancestorSelectorName();
SelectorName descendant = condition.descendantSelectorName();
if (ancestor.equals(viewName)) ancestor = sourceName;
if (descendant.equals(viewName)) descendant = sourceName;
if (ancestor == condition.ancestorSelectorName() && descendant == condition.descendantSelectorName()) return condition;
node.addSelector(ancestor, descendant);
return new DescendantNodeJoinCondition(ancestor, descendant);
}
return joinCondition;
}