Skip to content

Commit 521006f

Browse files
authored
Add QueryCriteria arrayContaining. (#1110)
Add QueryCriteria arrayContaining which maps to n1ql array_containing. Closes #1073. Original pull request #1109. Co-authored-by: mikereiche <[email protected]>
1 parent 214b250 commit 521006f

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/main/java/org/springframework/data/couchbase/core/query/QueryCriteria.java

+7
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ public QueryCriteria containing(@Nullable Object o) {
192192
return this;
193193
}
194194

195+
public QueryCriteria arrayContaining(@Nullable Object o) {
196+
operator = "ARRAY_CONTAINING";
197+
value = new Object[] { o };
198+
format = "array_containing(%1$s, %3$s)";
199+
return this;
200+
}
201+
195202
public QueryCriteria notContaining(@Nullable Object o) {
196203
value = new QueryCriteria[] { wrap(containing(o)) };
197204
operator = "NOT";

src/test/java/org/springframework/data/couchbase/core/query/QueryCriteriaTests.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,11 @@ void testStartingWith() {
123123
assertEquals("`name` like (\"Cou\"||\"%\")", c.export());
124124
}
125125

126-
/* cannot do this properly yet because in arg to when() in
127-
* startingWith() cannot be a QueryCriteria
128126
@Test
129127
void testStartingWithExpr() {
130128
QueryCriteria c = where(i("name")).startingWith(where(i("name")).plus("xxx"));
131-
assertEquals("`name` like (((`name` || "xxx") || ""%""))", c.export());
129+
assertEquals("`name` like (((`name` || \"xxx\"))||\"%\")", c.export());
132130
}
133-
*/
134131

135132
@Test
136133
void testEndingWith() {
@@ -162,6 +159,12 @@ void testNotContaining() {
162159
assertEquals("not( (contains(`name`, \"Elvis\")) )", c.export());
163160
}
164161

162+
@Test
163+
void testArrayContaining() {
164+
QueryCriteria c = where(i("name")).arrayContaining("Elvis");
165+
assertEquals("array_containing(`name`, \"Elvis\")", c.export());
166+
}
167+
165168
@Test
166169
void testLike() {
167170
QueryCriteria c = where(i("name")).like("%ouch%");

0 commit comments

Comments
 (0)