Skip to content
This repository has been archived by the owner on Mar 11, 2022. It is now read-only.

Commit

Permalink
Allow query operators to be used with ONE query expression element (#516
Browse files Browse the repository at this point in the history
)

Fixes #515
  • Loading branch information
mojito317 authored Sep 15, 2020
1 parent 4bf641e commit b970b22
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased
- [FIXED] Allow `$and`, `$or`, and `$nor` operator selectors
to be used with only one expression selector.

# 2.19.1 (2020-07-03)
- [FIXED] Connection leak regression introduced in 2.18.0 caused by not closing streams from
successful session response bodies.
Expand Down Expand Up @@ -296,7 +300,7 @@
- [FIXED] `NullPointerException` when parsing `{doc: null}` JSON in search or view results.
- [FIXED] Fixed issue with pagination numbering when using `queryPage` with
a clustered DB.
- [FIXED] Fixed issue where `queryPage` could not handle JSON values emitted from views.
- [FIXED] Fixed issue where `queryPage` could not handle JSON values emitted from views.
- [IMPROVED] Various documentation updates.
- [DEPRECATED] `com.cloudant.client.api.model.Page` setter methods.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static Operation nor(Selector... rhs) {
@Override
public String toString() {
// op rhs format ($and etc)
return String.format("\"%s\": %s", this.op, quoteCurly(this.rhs));
return String.format("\"%s\": %s", this.op, quoteCurly(this.rhs, this.op));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2017, 2018 IBM Corp. All rights reserved.
* Copyright © 2017, 2020 IBM Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -59,6 +59,14 @@ public static String quoteNoSquare(Object[] os) {
return quoteInternal(os, ", ", "", "", "", "");
}

public static String quoteCurly(Object[] os, String op) {
if (op.equals("$not")) {
// the operation "not" only takes one argument, so we don't need to make an array
return String.format("%s%s%s", "{", quote(os[0]), "}");
}
return quoteInternal(os, ", ", "{", "}", "[", "]");
}

public static String quoteCurly(Object[] os) {
if (os.length == 1) {
// the operation "not" only takes one argument, so we don't need to make an array
Expand Down
11 changes: 10 additions & 1 deletion cloudant-client/src/test/java/com/cloudant/tests/QueryTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2017, 2018 IBM Corp. All rights reserved.
* Copyright © 2017, 2020 IBM Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -57,6 +57,15 @@ public void basicSelector2() {
"{\"location\": {\"$eq\": \"Boston\"}}]}}", qb.build());
}

// "And selector with only one field"
@Test
public void basicSelector2WithOneField() {
QueryBuilder qb = new QueryBuilder(and(
eq("name", "Paul")));
Assertions.assertEquals("{\"selector\": " +
"{\"$and\": [{\"name\": {\"$eq\": \"Paul\"}}]}}", qb.build());
}

// "SUBFIELDS"
@Test
public void basicSelector3() {
Expand Down

0 comments on commit b970b22

Please sign in to comment.