Search in sources :

Example 36 with QueryOptions

use of com.couchbase.client.java.query.QueryOptions in project couchbase-jvm-clients by couchbase.

the class AsyncCluster method query.

/**
 * Performs a N1QL query with custom {@link QueryOptions}.
 *
 * @param statement the N1QL query statement as a raw string.
 * @param options the custom options for this query.
 * @return the {@link QueryResult} once the response arrives successfully.
 */
public CompletableFuture<QueryResult> query(final String statement, final QueryOptions options) {
    notNull(options, "QueryOptions", () -> new ReducedQueryErrorContext(statement));
    final QueryOptions.Built opts = options.build();
    JsonSerializer serializer = opts.serializer() == null ? environment.get().jsonSerializer() : opts.serializer();
    return queryAccessor.queryAsync(queryRequest(statement, opts), opts, serializer);
}
Also used : JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) ReducedQueryErrorContext(com.couchbase.client.core.error.context.ReducedQueryErrorContext) QueryOptions(com.couchbase.client.java.query.QueryOptions)

Example 37 with QueryOptions

use of com.couchbase.client.java.query.QueryOptions in project couchbase-jvm-clients by couchbase.

the class AsyncQueryIndexManager method exec.

private CompletableFuture<QueryResult> exec(QueryType queryType, CharSequence statement, CommonOptions<?>.BuiltCommonOptions options, String spanName, String bucketName, JsonArray parameters) {
    QueryOptions queryOpts = toQueryOptions(options).readonly(requireNonNull(queryType) == READ_ONLY);
    if (parameters != null && !parameters.isEmpty()) {
        queryOpts.parameters(parameters);
    }
    RequestSpan parent = cluster.environment().requestTracer().requestSpan(spanName, options.parentSpan().orElse(null));
    parent.attribute(TracingIdentifiers.ATTR_SYSTEM, TracingIdentifiers.ATTR_SYSTEM_COUCHBASE);
    if (bucketName != null) {
        parent.attribute(TracingIdentifiers.ATTR_NAME, bucketName);
    }
    queryOpts.parentSpan(parent);
    return cluster.query(statement.toString(), queryOpts).exceptionally(t -> {
        throw translateException(t);
    }).whenComplete((r, t) -> parent.end());
}
Also used : Arrays(java.util.Arrays) Validators.notNull(com.couchbase.client.core.util.Validators.notNull) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) CbThrowables.findCause(com.couchbase.client.core.util.CbThrowables.findCause) TimeoutException(java.util.concurrent.TimeoutException) QueryResult(com.couchbase.client.java.query.QueryResult) Collectors.toMap(java.util.stream.Collectors.toMap) Duration(java.time.Duration) Map(java.util.Map) CbThrowables.throwIfUnchecked(com.couchbase.client.core.util.CbThrowables.throwIfUnchecked) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) READ_ONLY(com.couchbase.client.java.manager.query.AsyncQueryIndexManager.QueryType.READ_ONLY) IndexExistsException(com.couchbase.client.core.error.IndexExistsException) Collectors.toSet(java.util.stream.Collectors.toSet) DropPrimaryQueryIndexOptions.dropPrimaryQueryIndexOptions(com.couchbase.client.java.manager.query.DropPrimaryQueryIndexOptions.dropPrimaryQueryIndexOptions) CbThrowables.hasCause(com.couchbase.client.core.util.CbThrowables.hasCause) Predicate(java.util.function.Predicate) Mapper(com.couchbase.client.core.json.Mapper) Collection(java.util.Collection) Set(java.util.Set) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Collectors(java.util.stream.Collectors) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) QueryOptions(com.couchbase.client.java.query.QueryOptions) List(java.util.List) GetAllQueryIndexesOptions.getAllQueryIndexesOptions(com.couchbase.client.java.manager.query.GetAllQueryIndexesOptions.getAllQueryIndexesOptions) RedactableArgument.redactMeta(com.couchbase.client.core.logging.RedactableArgument.redactMeta) Optional(java.util.Optional) WRITE(com.couchbase.client.java.manager.query.AsyncQueryIndexManager.QueryType.WRITE) CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions(com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions) CreateQueryIndexOptions.createQueryIndexOptions(com.couchbase.client.java.manager.query.CreateQueryIndexOptions.createQueryIndexOptions) Retry(com.couchbase.client.core.retry.reactor.Retry) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) CompletableFuture(java.util.concurrent.CompletableFuture) WatchQueryIndexesOptions.watchQueryIndexesOptions(com.couchbase.client.java.manager.query.WatchQueryIndexesOptions.watchQueryIndexesOptions) QueryException(com.couchbase.client.core.error.QueryException) Function(java.util.function.Function) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) CommonOptions(com.couchbase.client.java.CommonOptions) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) Objects.requireNonNull(java.util.Objects.requireNonNull) Stability(com.couchbase.client.core.annotation.Stability) IndexFailureException(com.couchbase.client.core.error.IndexFailureException) IndexesNotReadyException(com.couchbase.client.core.error.IndexesNotReadyException) DropQueryIndexOptions.dropQueryIndexOptions(com.couchbase.client.java.manager.query.DropQueryIndexOptions.dropQueryIndexOptions) Reactor(com.couchbase.client.core.Reactor) IndexNotFoundException(com.couchbase.client.core.error.IndexNotFoundException) Mono(reactor.core.publisher.Mono) JsonArray(com.couchbase.client.java.json.JsonArray) Collectors.toList(java.util.stream.Collectors.toList) BuildQueryIndexOptions.buildDeferredQueryIndexesOptions(com.couchbase.client.java.manager.query.BuildQueryIndexOptions.buildDeferredQueryIndexesOptions) AsyncCluster(com.couchbase.client.java.AsyncCluster) QueryOptions(com.couchbase.client.java.query.QueryOptions) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 38 with QueryOptions

use of com.couchbase.client.java.query.QueryOptions in project couchbase-jvm-clients by couchbase.

the class QueryCollectionIntegrationTest method performsAdhocQuery.

@Test
void performsAdhocQuery() {
    Scope scope = cluster.bucket(config().bucketname()).scope(SCOPE_NAME);
    Collection collection = scope.collection(COLLECTION_NAME);
    String id = insertDoc(collection);
    QueryOptions options = queryOptions().scanConsistency(QueryScanConsistency.REQUEST_PLUS);
    QueryResult result = scope.query("select * from `" + COLLECTION_NAME + "` where meta().id=\"" + id + "\"", options);
    assertEquals(QueryStatus.SUCCESS, result.metaData().status());
    assertEquals(1, result.rowsAsObject().size());
    ReactiveScope reactiveScope = cluster.bucket(config().bucketname()).reactive().scope(SCOPE_NAME);
    ReactiveQueryResult reactiveResult = reactiveScope.query("select * from `" + COLLECTION_NAME + "` where meta().id=\"" + id + "\"", options).block();
    assertEquals(QueryStatus.SUCCESS, reactiveResult.metaData().block().status());
    assertEquals(1, reactiveResult.rowsAsObject().blockLast().size());
}
Also used : QueryResult(com.couchbase.client.java.query.QueryResult) ReactiveQueryResult(com.couchbase.client.java.query.ReactiveQueryResult) ReactiveQueryResult(com.couchbase.client.java.query.ReactiveQueryResult) QueryOptions(com.couchbase.client.java.query.QueryOptions) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 39 with QueryOptions

use of com.couchbase.client.java.query.QueryOptions in project couchbase-jvm-clients by couchbase.

the class QueryIntegrationTest method blockingPositionalParameterizedSelectQuery.

@Test
void blockingPositionalParameterizedSelectQuery() {
    String id = insertDoc();
    QueryOptions options = queryOptions().scanConsistency(QueryScanConsistency.REQUEST_PLUS).parameters(JsonArray.from(id));
    QueryResult result = cluster.query("select  " + bucketName + ".* from " + bucketName + " where meta().id=$1", options);
    List<JsonObject> rows = result.rowsAs(JsonObject.class);
    assertEquals(1, rows.size());
    assertEquals(FOO_CONTENT, rows.get(0));
}
Also used : QueryResult(com.couchbase.client.java.query.QueryResult) ReactiveQueryResult(com.couchbase.client.java.query.ReactiveQueryResult) JsonObject(com.couchbase.client.java.json.JsonObject) QueryOptions(com.couchbase.client.java.query.QueryOptions) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 40 with QueryOptions

use of com.couchbase.client.java.query.QueryOptions in project couchbase-jvm-clients by couchbase.

the class QueryIntegrationTest method reactiveNamedParameterizedSelectQuery.

@Test
void reactiveNamedParameterizedSelectQuery() {
    String id = insertDoc();
    QueryOptions options = queryOptions().scanConsistency(QueryScanConsistency.REQUEST_PLUS).parameters(JsonObject.create().put("id", id));
    Mono<ReactiveQueryResult> result = cluster.reactive().query("select * from " + bucketName + " where meta().id=$id", options);
    List<JsonObject> rows = result.flux().flatMap(ReactiveQueryResult::rowsAsObject).collectList().block();
    assertNotNull(rows);
    assertEquals(1, rows.size());
}
Also used : ReactiveQueryResult(com.couchbase.client.java.query.ReactiveQueryResult) JsonObject(com.couchbase.client.java.json.JsonObject) QueryOptions(com.couchbase.client.java.query.QueryOptions) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

QueryOptions (com.couchbase.client.java.query.QueryOptions)40 Test (org.junit.jupiter.api.Test)31 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)19 ReactiveQueryResult (com.couchbase.client.java.query.ReactiveQueryResult)18 QueryResult (com.couchbase.client.java.query.QueryResult)16 JsonObject (com.couchbase.client.java.json.JsonObject)15 Airport (org.springframework.data.couchbase.domain.Airport)10 ReducedQueryErrorContext (com.couchbase.client.core.error.context.ReducedQueryErrorContext)4 JsonSerializer (com.couchbase.client.java.codec.JsonSerializer)4 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)2 JsonArray (com.couchbase.client.java.json.JsonArray)2 MutationResult (com.couchbase.client.java.kv.MutationResult)2 Duration (java.time.Duration)2 RemoveResult (org.springframework.data.couchbase.core.RemoveResult)2 Reactor (com.couchbase.client.core.Reactor)1 Stability (com.couchbase.client.core.annotation.Stability)1 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)1 TracingIdentifiers (com.couchbase.client.core.cnc.TracingIdentifiers)1 IndexExistsException (com.couchbase.client.core.error.IndexExistsException)1 IndexFailureException (com.couchbase.client.core.error.IndexFailureException)1