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

Commit b970b22

Browse files
authored
Allow query operators to be used with ONE query expression element (#516)
Fixes #515
1 parent 4bf641e commit b970b22

4 files changed

Lines changed: 25 additions & 4 deletions

File tree

CHANGES.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Unreleased
2+
- [FIXED] Allow `$and`, `$or`, and `$nor` operator selectors
3+
to be used with only one expression selector.
4+
15
# 2.19.1 (2020-07-03)
26
- [FIXED] Connection leak regression introduced in 2.18.0 caused by not closing streams from
37
successful session response bodies.
@@ -296,7 +300,7 @@
296300
- [FIXED] `NullPointerException` when parsing `{doc: null}` JSON in search or view results.
297301
- [FIXED] Fixed issue with pagination numbering when using `queryPage` with
298302
a clustered DB.
299-
- [FIXED] Fixed issue where `queryPage` could not handle JSON values emitted from views.
303+
- [FIXED] Fixed issue where `queryPage` could not handle JSON values emitted from views.
300304
- [IMPROVED] Various documentation updates.
301305
- [DEPRECATED] `com.cloudant.client.api.model.Page` setter methods.
302306

cloudant-client/src/main/java/com/cloudant/client/api/query/Operation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static Operation nor(Selector... rhs) {
8080
@Override
8181
public String toString() {
8282
// op rhs format ($and etc)
83-
return String.format("\"%s\": %s", this.op, quoteCurly(this.rhs));
83+
return String.format("\"%s\": %s", this.op, quoteCurly(this.rhs, this.op));
8484
}
8585

8686
}

cloudant-client/src/main/java/com/cloudant/client/internal/query/Helpers.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2017, 2018 IBM Corp. All rights reserved.
2+
* Copyright © 2017, 2020 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -59,6 +59,14 @@ public static String quoteNoSquare(Object[] os) {
5959
return quoteInternal(os, ", ", "", "", "", "");
6060
}
6161

62+
public static String quoteCurly(Object[] os, String op) {
63+
if (op.equals("$not")) {
64+
// the operation "not" only takes one argument, so we don't need to make an array
65+
return String.format("%s%s%s", "{", quote(os[0]), "}");
66+
}
67+
return quoteInternal(os, ", ", "{", "}", "[", "]");
68+
}
69+
6270
public static String quoteCurly(Object[] os) {
6371
if (os.length == 1) {
6472
// the operation "not" only takes one argument, so we don't need to make an array

cloudant-client/src/test/java/com/cloudant/tests/QueryTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2017, 2018 IBM Corp. All rights reserved.
2+
* Copyright © 2017, 2020 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -57,6 +57,15 @@ public void basicSelector2() {
5757
"{\"location\": {\"$eq\": \"Boston\"}}]}}", qb.build());
5858
}
5959

60+
// "And selector with only one field"
61+
@Test
62+
public void basicSelector2WithOneField() {
63+
QueryBuilder qb = new QueryBuilder(and(
64+
eq("name", "Paul")));
65+
Assertions.assertEquals("{\"selector\": " +
66+
"{\"$and\": [{\"name\": {\"$eq\": \"Paul\"}}]}}", qb.build());
67+
}
68+
6069
// "SUBFIELDS"
6170
@Test
6271
public void basicSelector3() {

0 commit comments

Comments
 (0)