/** * Walk the supplied constraint to extract a list of the constraints that can be AND-ed together. For example, given the * constraint tree ((C1 AND C2) AND (C3 OR C4)), this method would result in a list of three separate criteria: [C1,C2,(C3 OR * C4)]. The resulting <code>andConstraints</code> list will contain Constraint objects that all must be true. * * @param constraint the input constraint * @param andableConstraints the collection into which all non-{@link And AND} constraints should be placed */ protected void separateAndConstraints( Constraint constraint, List<Constraint> andableConstraints ) { if (constraint == null) return; assert andableConstraints != null; if (constraint instanceof And) { And and = (And)constraint; separateAndConstraints(and.left(), andableConstraints); separateAndConstraints(and.right(), andableConstraints); } else { andableConstraints.add(constraint); } }
/** * Walk the supplied constraint to extract a list of the constraints that can be AND-ed together. For example, given the * constraint tree ((C1 AND C2) AND (C3 OR C4)), this method would result in a list of three separate criteria: [C1,C2,(C3 OR * C4)]. The resulting <code>andConstraints</code> list will contain Constraint objects that all must be true. * * @param constraint the input constraint * @param andableConstraints the collection into which all non-{@link And AND} constraints should be placed */ protected void separateAndConstraints( Constraint constraint, List<Constraint> andableConstraints ) { if (constraint == null) return; assert andableConstraints != null; if (constraint instanceof And) { And and = (And)constraint; separateAndConstraints(and.left(), andableConstraints); separateAndConstraints(and.right(), andableConstraints); } else { andableConstraints.add(constraint); } }
@Override public void visit( And and ) { strategy.visit(and); enqueue(and.left()); enqueue(and.right()); visitNext(); }
@Override public void visit( And and ) { strategy.visit(and); enqueue(and.left()); enqueue(and.right()); visitNext(); }
@Override public void visit( And and ) { append('('); and.left().accept(this); append(" AND "); and.right().accept(this); append(')'); }
@Override public void visit( And and ) { append('('); and.left().accept(this); append(" AND "); and.right().accept(this); append(')'); }
Constraint left = and.left(); Constraint right = and.right(); Constraint newLeft = rewrite(context, left);
@Test public void shouldParseConstraintFromStringWithAndExpressionWithNoParentheses() { NamedSelector selector = new NamedSelector(selectorName("tableA")); Constraint constraint = parser.parseConstraint(tokens("ISSAMENODE('/a/b/c') AND CONTAINS(p1,term1)"), typeSystem, selector); assertThat(constraint, is(instanceOf(And.class))); And and = (And)constraint; assertThat(and.left(), is(instanceOf(SameNode.class))); SameNode same = (SameNode)and.left(); assertThat(same.selectorName(), is(selectorName("tableA"))); assertThat(same.getPath(), is("/a/b/c")); assertThat(and.right(), is(instanceOf(FullTextSearch.class))); FullTextSearch search = (FullTextSearch)and.right(); assertThat(search.selectorName(), is(selectorName("tableA"))); assertThat(search.getPropertyName(), is("p1")); assertThat(search.fullTextSearchExpression(), is("term1")); }
Constraint left = and.left(); Constraint right = and.right(); Constraint newLeft = rewrite(context, left);
@Test public void shouldParseConstraintFromStringWithMultipleAndExpressions() { NamedSelector selector = new NamedSelector(selectorName("tableA")); Constraint constraint = parser.parseConstraint(tokens("ISSAMENODE('/a/b/c') AND CONTAINS(p1,term1) AND CONTAINS(p2,term2)"), typeSystem, selector); assertThat(constraint, is(instanceOf(And.class))); And and = (And)constraint; assertThat(and.left(), is(instanceOf(SameNode.class))); SameNode same = (SameNode)and.left(); assertThat(same.selectorName(), is(selectorName("tableA"))); assertThat(same.getPath(), is("/a/b/c")); assertThat(and.right(), is(instanceOf(And.class))); And secondAnd = (And)and.right(); assertThat(secondAnd.left(), is(instanceOf(FullTextSearch.class))); FullTextSearch search1 = (FullTextSearch)secondAnd.left(); assertThat(search1.selectorName(), is(selectorName("tableA"))); assertThat(search1.getPropertyName(), is("p1")); assertThat(search1.fullTextSearchExpression(), is("term1")); assertThat(secondAnd.right(), is(instanceOf(FullTextSearch.class))); FullTextSearch search2 = (FullTextSearch)secondAnd.right(); assertThat(search2.selectorName(), is(selectorName("tableA"))); assertThat(search2.getPropertyName(), is("p2")); assertThat(search2.fullTextSearchExpression(), is("term2")); }
@Test public void shouldParseConstraintFromStringWithParenthesesAndConjunctionAndDisjunctions() { NamedSelector selector = new NamedSelector(selectorName("tableA")); Constraint constraint = parser.parseConstraint(tokens("ISSAMENODE('/a/b') OR (ISSAMENODE('/c/d') AND ISSAMENODE('/e/f'))"), typeSystem, selector); assertThat(constraint, is(instanceOf(Or.class))); Or or = (Or)constraint; assertThat(or.left(), is(instanceOf(SameNode.class))); SameNode first = (SameNode)or.left(); assertThat(first.selectorName(), is(selectorName("tableA"))); assertThat(first.getPath(), is("/a/b")); assertThat(or.right(), is(instanceOf(And.class))); And and = (And)or.right(); assertThat(and.left(), is(instanceOf(SameNode.class))); SameNode second = (SameNode)and.left(); assertThat(second.selectorName(), is(selectorName("tableA"))); assertThat(second.getPath(), is("/c/d")); assertThat(and.right(), is(instanceOf(SameNode.class))); SameNode third = (SameNode)and.right(); assertThat(third.selectorName(), is(selectorName("tableA"))); assertThat(third.getPath(), is("/e/f")); }
constraint = new And(rewriteConstraint(and.left()), rewriteConstraint(and.right())); } else if (constraint instanceof Or) { Or or = (Or)constraint;
constraint = new And(rewriteConstraint(and.left()), rewriteConstraint(and.right())); } else if (constraint instanceof Or) { Or or = (Or)constraint;
/** * Tests that the child nodes (but no grandchild nodes) are returned. */ @Test public void shouldParseSelectWithChildAxisCriteria() { query = parse("SELECT * FROM nt:base WHERE jcr:path LIKE '/a/b/%' AND NOT jcr:path LIKE '/a/b/%/%'"); // SELECT * ... assertThat(query.columns().isEmpty(), is(true)); // FROM ... NamedSelector selector = (NamedSelector)query.source(); assertThat(selector.name(), is(selectorName("nt:base"))); assertThat(selector.aliasOrName(), is(selectorName("nt:base"))); assertThat(selector.alias(), is(nullValue())); // WHERE ... And and = isAnd(query.constraint()); Comparison comparison1 = isComparison(and.left()); assertThat(comparison1.getOperand1(), is((DynamicOperand)nodePath(selectorName("nt:base")))); assertThat(comparison1.getOperand2(), is((StaticOperand)literal("/a/b/%"))); Not not = isNot(and.right()); Comparison comparison2a = isComparison(not.getConstraint()); assertThat(comparison2a.getOperand1(), is((DynamicOperand)nodePath(selectorName("nt:base")))); assertThat(comparison2a.getOperand2(), is((StaticOperand)literal("/a/b/%/%"))); }
/** * Tests that the child nodes (but no grandchild nodes) are returned. */ @Test public void shouldParseSelectWithChildAxisCriteria() { query = parse("SELECT * FROM [nt:base] WHERE PATH() LIKE '/a/b/%' AND NOT PATH() LIKE '/a/b/%/%'"); // SELECT * ... assertThat(query.columns().isEmpty(), is(true)); // FROM ... NamedSelector selector = (NamedSelector)query.source(); assertThat(selector.name(), is(selectorName("nt:base"))); assertThat(selector.aliasOrName(), is(selectorName("nt:base"))); assertThat(selector.alias(), is(nullValue())); // WHERE ... And and = isAnd(query.constraint()); Comparison comparison1 = isComparison(and.left()); assertThat(comparison1.getOperand1(), is((DynamicOperand)nodePath(selectorName("nt:base")))); assertThat(comparison1.getOperand2(), is((StaticOperand)literal("/a/b/%"))); Not not = isNot(and.right()); Comparison comparison2a = isComparison(not.getConstraint()); assertThat(comparison2a.getOperand1(), is((DynamicOperand)nodePath(selectorName("nt:base")))); assertThat(comparison2a.getOperand2(), is((StaticOperand)literal("/a/b/%/%"))); }
if (constraint instanceof And) { And and = (And)constraint; Constraint left = replaceReferencesToRemovedSource(context, and.left(), rewrittenSelectors); Constraint right = replaceReferencesToRemovedSource(context, and.right(), rewrittenSelectors); if (left == and.left() && right == and.right()) return and; return new And(left, right);
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 And) { And and = (And)constraint; Constraint left = replaceAliasesWithProperties(context, and.left(), propertyByAlias); Constraint right = replaceAliasesWithProperties(context, and.right(), propertyByAlias); if (left == and.left() && right == and.right()) return and; return new And(left, right);
if (constraint instanceof And) { And and = (And)constraint; Constraint left = replaceAliasesWithProperties(context, and.left(), propertyByAlias); Constraint right = replaceAliasesWithProperties(context, and.right(), propertyByAlias); if (left == and.left() && right == and.right()) return and; return new And(left, right);