Couchbase C++ SDK 1.4.0 (rev. 59aa900)
Loading...
Searching...
No Matches
query_stream_result.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright 2026. Couchbase, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
21#include <couchbase/error.hxx>
24
25#include <functional>
26#include <future>
27#include <memory>
28#include <optional>
29#include <utility>
30
31namespace couchbase
32{
33#ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
34class internal_query_stream_result;
35#endif
36
43using query_row_handler = std::function<void(error, std::optional<query_row>)>;
44
68{
69public:
77
86 explicit query_stream_result(std::shared_ptr<internal_query_stream_result> internal);
87
106 void next(query_row_handler&& handler) const;
107
125 [[nodiscard]] auto next() const -> std::future<std::pair<error, std::optional<query_row>>>;
126
140 [[nodiscard]] auto signature() const -> std::optional<codec::binary>;
141
161 [[nodiscard]] auto meta_data() const -> std::future<std::pair<error, query_meta_data>>;
162
185 void cancel() const;
186
196 };
197
224 {
225 public:
226 auto operator*() const -> std::pair<error, query_row>;
227 auto operator++() -> iterator&;
228
235 explicit iterator(std::shared_ptr<internal_query_stream_result> internal);
236
244 friend auto operator==(const iterator& it, end_sentinel /* end */) -> bool
245 {
246 return it.terminal_;
247 }
248 friend auto operator==(end_sentinel end, const iterator& it) -> bool
249 {
250 return it == end;
251 }
252 friend auto operator!=(const iterator& it, end_sentinel end) -> bool
253 {
254 return !(it == end);
255 }
256 friend auto operator!=(end_sentinel end, const iterator& it) -> bool
257 {
258 return !(it == end);
259 }
260
261 // Intentionally no std::iterator_traits typedefs (notably no iterator_category). This is a
262 // deliberately minimal single-pass iterator for range-based for and `while (it !=
263 // result.end())` loops only (see the class doc); it omits post-increment and operator->.
264 // Declaring an input_iterator_tag while omitting those would falsely advertise
265 // LegacyInputIterator / std::input_iterator conformance and could break generic code that
266 // trusts iterator_traits. Range-based for needs none of these typedefs.
267
268 private:
269 void fetch_item();
270
271 std::shared_ptr<internal_query_stream_result> internal_{};
272 std::pair<error, query_row> item_{};
273 bool terminal_{ false };
274 // Set when fetch_item() lands on a terminal error: the error element is delivered once (the
275 // iterator is not yet equal to end()), and the next increment advances to true end.
276 bool error_pending_{ false };
277 };
278
287 [[nodiscard]] auto begin() const -> iterator;
288
297 [[nodiscard]] auto end() const -> end_sentinel;
298
299private:
300 std::shared_ptr<internal_query_stream_result> internal_{};
301};
302} // namespace couchbase
Definition error.hxx:31
Stores any non-rows results related to the execution of a particular N1QL query.
Definition query_meta_data.hxx:39
Represents a single row of a streaming query result.
Definition query_row.hxx:41
A single-pass input iterator that synchronously fetches rows one at a time.
Definition query_stream_result.hxx:224
friend auto operator==(end_sentinel end, const iterator &it) -> bool
Definition query_stream_result.hxx:248
friend auto operator!=(const iterator &it, end_sentinel end) -> bool
Definition query_stream_result.hxx:252
friend auto operator!=(end_sentinel end, const iterator &it) -> bool
Definition query_stream_result.hxx:256
auto operator*() const -> std::pair< error, query_row >
iterator(std::shared_ptr< internal_query_stream_result > internal)
Constructs an iterator over an internal result; prefer begin().
auto meta_data() const -> std::future< std::pair< error, query_meta_data > >
Returns the query metadata.
auto signature() const -> std::optional< codec::binary >
Returns the query signature captured from the response metadata, if present.
auto next() const -> std::future< std::pair< error, std::optional< query_row > > >
Fetches the next row, returning a future.
void next(query_row_handler &&handler) const
Fetches the next row asynchronously, invoking the handler when ready.
query_stream_result(std::shared_ptr< internal_query_stream_result > internal)
Constructs a query stream result from an internal result.
void cancel() const
Cancels the stream and closes the underlying HTTP connection.
auto begin() const -> iterator
Returns an iterator to the beginning.
query_stream_result()=default
Constructs an empty (no-op) result handle.
auto end() const -> end_sentinel
Returns the end sentinel.
Definition analytics_options.hxx:36
Represents a single item from the result of scan().
Definition allow_querying_search_index_options.hxx:28
std::function< void(error, std::optional< query_row >)> query_row_handler
The signature for the handler of the next() operation.
Definition query_stream_result.hxx:43
Sentinel returned by end().
Definition query_stream_result.hxx:195