|
18 | 18 | import static org.springframework.data.couchbase.core.query.N1QLExpression.x; |
19 | 19 |
|
20 | 20 | import java.util.ArrayList; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.Collections; |
21 | 23 | import java.util.Formatter; |
22 | 24 | import java.util.LinkedList; |
23 | 25 | import java.util.List; |
24 | 26 |
|
25 | 27 | import org.springframework.data.couchbase.core.convert.CouchbaseConverter; |
| 28 | +import org.springframework.data.couchbase.core.mapping.CouchbaseDocument; |
| 29 | +import org.springframework.data.couchbase.core.mapping.CouchbaseList; |
26 | 30 | import org.springframework.lang.Nullable; |
27 | 31 |
|
28 | 32 | import com.couchbase.client.core.error.InvalidArgumentException; |
29 | 33 | import com.couchbase.client.java.json.JsonArray; |
30 | 34 | import com.couchbase.client.java.json.JsonObject; |
31 | 35 | import com.couchbase.client.java.json.JsonValue; |
| 36 | +import org.springframework.util.CollectionUtils; |
32 | 37 |
|
33 | 38 | /** |
34 | 39 | * @author Michael Nitschinger |
@@ -412,8 +417,8 @@ private String maybeWrapValue(N1QLExpression key, Object value, int[] paramIndex |
412 | 417 | try { |
413 | 418 | params.add(convert(converter, value)); |
414 | 419 | } catch (InvalidArgumentException iae) { |
415 | | - if (value instanceof Object[]) { |
416 | | - addAsArray(params, value, converter); |
| 420 | + if (value instanceof Object[] || value instanceof Collection) { |
| 421 | + addAsCollection(params, asCollection(value), converter); |
417 | 422 | } else { |
418 | 423 | throw iae; |
419 | 424 | } |
@@ -462,15 +467,28 @@ private static Object convert(CouchbaseConverter converter, Object value) { |
462 | 467 | return converter != null ? converter.convertForWriteIfNeeded(value) : value; |
463 | 468 | } |
464 | 469 |
|
465 | | - private void addAsArray(JsonArray posValues, Object o, CouchbaseConverter converter) { |
466 | | - Object[] array = (Object[]) o; |
| 470 | + private void addAsCollection(JsonArray posValues, Collection collection, CouchbaseConverter converter) { |
467 | 471 | JsonArray ja = JsonValue.ja(); |
468 | | - for (Object e : array) { |
| 472 | + for (Object e : collection) { |
469 | 473 | ja.add(String.valueOf(convert(converter, e))); |
470 | 474 | } |
471 | 475 | posValues.add(ja); |
472 | 476 | } |
473 | 477 |
|
| 478 | + /** |
| 479 | + * Returns a collection from the given source object. From MappingCouchbaseConverter. |
| 480 | + * |
| 481 | + * @param source the source object. |
| 482 | + * @return the target collection. |
| 483 | + */ |
| 484 | + private static Collection<?> asCollection(final Object source) { |
| 485 | + if (source instanceof Collection) { |
| 486 | + return (Collection<?>) source; |
| 487 | + } |
| 488 | + return source.getClass().isArray() ? CollectionUtils.arrayToList(source) : Collections.singleton(source); |
| 489 | + } |
| 490 | + |
| 491 | + |
474 | 492 | private String maybeBackTic(String value) { |
475 | 493 | if (value == null || (value.startsWith("`") && value.endsWith("`"))) { |
476 | 494 | return value; |
|
0 commit comments