Search in sources :

Example 26 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project spring-data-couchbase by spring-projects.

the class QueryCriteria method addAsCollection.

private void addAsCollection(JsonArray posValues, Collection collection, CouchbaseConverter converter) {
    JsonArray ja = JsonValue.ja();
    for (Object e : collection) {
        ja.add(String.valueOf(convert(converter, e)));
    }
    posValues.add(ja);
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 27 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project spring-data-couchbase by spring-projects.

the class QueryCriteria method in.

public QueryCriteria in(@Nullable Object... o) {
    operator = "IN";
    format = "%1$s in %3$s";
    value = new Object[1];
    if (o.length > 0) {
        if (o[0] instanceof JsonArray || o[0] instanceof List || o[0] instanceof Object[]) {
            if (o.length != 1) {
                throw new RuntimeException("IN cannot take multiple lists");
            }
            if (o[0] instanceof Object[]) {
                value[0] = o[0];
            } else if (o[0] instanceof JsonArray) {
                JsonArray ja = ((JsonArray) o[0]);
                value[0] = ja.toList().toArray();
            } else if (o[0] instanceof List) {
                List l = ((List) o[0]);
                value[0] = l.toArray();
            }
        } else {
            // Query expected = (new Query()).addCriteria(where(i("firstname")).in("Oliver", "Charles"));
            if (o instanceof Object[]) {
                value[0] = o;
            } else {
                // notIn((Object) new String[] { "Alabama", "Florida" }));
                throw new CouchbaseException("unhandled parameters " + o);
            }
        }
    }
    return this;
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 28 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project spring-data-couchbase by spring-projects.

the class OptionsBuilder method buildQueryOptions.

static QueryOptions buildQueryOptions(Query query, QueryOptions options, QueryScanConsistency scanConsistency) {
    options = options != null ? options : QueryOptions.queryOptions();
    if (query.getParameters() != null) {
        if (query.getParameters() instanceof JsonArray) {
            options.parameters((JsonArray) query.getParameters());
        } else {
            options.parameters((JsonObject) query.getParameters());
        }
    }
    Meta meta = query.getMeta() != null ? query.getMeta() : new Meta();
    QueryOptions.Built optsBuilt = options.build();
    JsonObject optsJson = getQueryOpts(optsBuilt);
    QueryScanConsistency metaQueryScanConsistency = meta.get(SCAN_CONSISTENCY) != null ? ((ScanConsistency) meta.get(SCAN_CONSISTENCY)).query() : null;
    QueryScanConsistency qsc = fromFirst(QueryScanConsistency.NOT_BOUNDED, getScanConsistency(optsJson), scanConsistency, metaQueryScanConsistency);
    Duration timeout = fromFirst(Duration.ofSeconds(0), getTimeout(optsBuilt), meta.get(TIMEOUT));
    RetryStrategy retryStrategy = fromFirst(null, getRetryStrategy(optsBuilt), meta.get(RETRY_STRATEGY));
    if (qsc != null) {
        options.scanConsistency(qsc);
    }
    if (timeout != null) {
        options.timeout(timeout);
    }
    if (retryStrategy != null) {
        options.retryStrategy(retryStrategy);
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("query options: {}", getQueryOpts(options.build()));
    }
    return options;
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) JsonObject(com.couchbase.client.java.json.JsonObject) QueryScanConsistency(com.couchbase.client.java.query.QueryScanConsistency) Duration(java.time.Duration) QueryOptions(com.couchbase.client.java.query.QueryOptions) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy)

Example 29 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project spring-data-couchbase by spring-projects.

the class StringBasedN1qlQueryParser method addAsArray.

private void addAsArray(JsonArray posValues, Object o) {
    Object[] array = (Object[]) o;
    JsonArray ja = JsonValue.ja();
    for (Object e : array) {
        ja.add(String.valueOf(couchbaseConverter.convertForWriteIfNeeded(e)));
    }
    posValues.add(ja);
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 30 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project spring-data-couchbase by spring-projects.

the class StringBasedN1qlQueryParser method addAsArray.

private void addAsArray(JsonObject namedValues, String placeholder, Object o) {
    Object[] array = (Object[]) o;
    JsonArray ja = JsonValue.ja();
    for (Object e : array) {
        ja.add(String.valueOf(couchbaseConverter.convertForWriteIfNeeded(e)));
    }
    namedValues.put(placeholder, ja);
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) JsonObject(com.couchbase.client.java.json.JsonObject)

Aggregations

JsonArray (com.couchbase.client.java.json.JsonArray)48 Test (org.junit.jupiter.api.Test)26 JsonObject (com.couchbase.client.java.json.JsonObject)24 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)8 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)3 IndexFailureException (com.couchbase.client.core.error.IndexFailureException)3 List (java.util.List)3 Airport (org.springframework.data.couchbase.domain.Airport)3 DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)2 InvalidArgumentException (com.couchbase.client.core.error.InvalidArgumentException)2 RetryExhaustedException (com.couchbase.client.core.retry.reactor.RetryExhaustedException)2 JsonValue (com.couchbase.client.java.json.JsonValue)2 GetResult (com.couchbase.client.java.kv.GetResult)2 QueryOptions (com.couchbase.client.java.query.QueryOptions)2 SearchQuery (com.couchbase.client.java.search.SearchQuery)2 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Query (org.springframework.data.couchbase.core.query.Query)2 AirportRepositoryScanConsistencyTest (org.springframework.data.couchbase.domain.AirportRepositoryScanConsistencyTest)2