Skip to content

Commit

Permalink
Fix NEGATE of more than one expression and refine serialization of qu…
Browse files Browse the repository at this point in the history
…eries

Signed-off-by: Jorge Ruesga <[email protected]>
  • Loading branch information
jruesga committed Oct 20, 2016
1 parent 3dfae49 commit 6767c9d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6767c9d

Please sign in to comment.