public static Constraint replaceSubqueriesWithBindVariables( QueryContext context,
Constraint constraint,
Map<String, Subquery> subqueriesByVariableName ) {
if (constraint instanceof And) {
And and = (And)constraint;
Constraint left = replaceSubqueriesWithBindVariables(context, and.left(), subqueriesByVariableName);
Constraint right = replaceSubqueriesWithBindVariables(context, and.right(), subqueriesByVariableName);
if (left == and.left() && right == and.right()) return and;
return new And(left, right);
}
if (constraint instanceof Or) {
Or or = (Or)constraint;
Constraint left = replaceSubqueriesWithBindVariables(context, or.left(), subqueriesByVariableName);
Constraint right = replaceSubqueriesWithBindVariables(context, or.right(), subqueriesByVariableName);
if (left == or.left() && right == or.right()) return or;
return new Or(left, right);
}
if (constraint instanceof Not) {
Not not = (Not)constraint;
Constraint wrapped = replaceSubqueriesWithBindVariables(context, not.getConstraint(), subqueriesByVariableName);
if (wrapped == not.getConstraint()) return not;
return new Not(wrapped);
}
if (constraint instanceof SameNode) {
return constraint;