From 6767c9debcce97bb25e9058306493d37998bb2de Mon Sep 17 00:00:00 2001 From: Jorge Ruesga Date: Fri, 21 Oct 2016 00:29:12 +0200 Subject: [PATCH] Fix NEGATE of more than one expression and refine serialization of queries Signed-off-by: Jorge Ruesga --- .../ruesga/rview/gerrit/filter/ChangeQuery.java | 9 +++++++-- .../ruesga/rview/gerrit/filter/ComplexQuery.java | 14 ++++++++++++-- .../rview/gerrit/filter/ChangeQueryTest.java | 10 ++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/gerrit/src/main/java/com/ruesga/rview/gerrit/filter/ChangeQuery.java b/gerrit/src/main/java/com/ruesga/rview/gerrit/filter/ChangeQuery.java index 992b7264..0ff955f7 100644 --- a/gerrit/src/main/java/com/ruesga/rview/gerrit/filter/ChangeQuery.java +++ b/gerrit/src/main/java/com/ruesga/rview/gerrit/filter/ChangeQuery.java @@ -333,9 +333,14 @@ private ChangeQuery toChangeQuery(Tree tree) throws QueryParseException { case QueryLexer.AND: case QueryLexer.OR: - addField(tree.getChild(0), query); + int childType = tree.getChild(0).getType(); + if (childType == QueryLexer.NOT) { + query.negate(toChangeQuery(tree.getChild(0).getChild(0))); + } else { + addField(tree.getChild(0), query); + } for (int i = 1; i < tree.getChildCount(); i++) { - int childType = tree.getChild(i).getType(); + childType = tree.getChild(i).getType(); if (childType == QueryLexer.FIELD_NAME) { if (tree.getType() == QueryLexer.AND) { query.and(toChangeQuery(tree.getChild(i))); diff --git a/gerrit/src/main/java/com/ruesga/rview/gerrit/filter/ComplexQuery.java b/gerrit/src/main/java/com/ruesga/rview/gerrit/filter/ComplexQuery.java index a56a0d9f..02efd924 100644 --- a/gerrit/src/main/java/com/ruesga/rview/gerrit/filter/ComplexQuery.java +++ b/gerrit/src/main/java/com/ruesga/rview/gerrit/filter/ComplexQuery.java @@ -24,7 +24,12 @@ public T and(T query) { if (query.queries().size() == 0) { throw new IllegalArgumentException("Empty query"); } - add("AND (" + query + ")"); + int size = query.queries().size(); + if (size > 1) { + add("AND (" + query + ")"); + } else { + add("AND " + query); + } //noinspection unchecked return (T)this; } @@ -36,7 +41,12 @@ public T or(T query) { if (query.queries().size() == 0) { throw new IllegalArgumentException("Empty query"); } - add("OR (" + query + ")"); + int size = query.queries().size(); + if (size > 1) { + add("OR (" + query + ")"); + } else { + add("OR " + query); + } //noinspection unchecked return (T)this; } diff --git a/gerrit/src/test/java/com/ruesga/rview/gerrit/filter/ChangeQueryTest.java b/gerrit/src/test/java/com/ruesga/rview/gerrit/filter/ChangeQueryTest.java index 7a35c7af..cfd05fc6 100644 --- a/gerrit/src/test/java/com/ruesga/rview/gerrit/filter/ChangeQueryTest.java +++ b/gerrit/src/test/java/com/ruesga/rview/gerrit/filter/ChangeQueryTest.java @@ -72,6 +72,16 @@ public void testParseQuery() { .and(new ChangeQuery().ownerSelf() .or(new ChangeQuery().reviewerSelf())) .and(new ChangeQuery().negate(new ChangeQuery().age(TimeUnit.WEEKS, 4)))); + + testParseQuery("status:open AND -(topic:\"translations\")", + new ChangeQuery().status(StatusType.OPEN).and( + new ChangeQuery().negate( + new ChangeQuery().topic("translations")))); + testParseQuery("status:open AND -(topic:\"translations\" AND owner:\"aa\")", + new ChangeQuery().status(StatusType.OPEN).and( + new ChangeQuery().negate( + new ChangeQuery().topic("translations").and( + new ChangeQuery().owner("aa"))))); } private void testParseQuery(String expression, ChangeQuery expectedResult) {