Google Cloud Spanner C++ Client 2.13.0
A C++ Client Library for Google Cloud Spanner
Loading...
Searching...
No Matches
results.h
1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_RESULTS_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_RESULTS_H
17
18#include "google/cloud/spanner/row.h"
19#include "google/cloud/spanner/timestamp.h"
20#include "google/cloud/spanner/version.h"
21#include "google/cloud/optional.h"
22#include "absl/types/optional.h"
23#include <google/spanner/v1/spanner.pb.h>
24#include <memory>
25#include <string>
26#include <unordered_map>
27
28namespace google {
29namespace cloud {
30namespace spanner {
31GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
32
33/**
34 * Defines the interface for `RowStream` implementations.
35 *
36 * The `RowStream` class represents a stream of `Rows` returned from
37 * `spanner::Client::Read()` or `spanner::Client::ExecuteQuery()`. There are
38 * different implementations depending the the RPC. Applications can also
39 * mock this class when testing their code and mocking the `spanner::Client`
40 * behavior.
41 */
43 public:
44 virtual ~ResultSourceInterface() = default;
45
46 /**
47 * Returns the next row in the stream.
48 *
49 * @return if the stream is interrupted due to a failure the
50 * `StatusOr<spanner::Row>` contains the error. The function returns a
51 * successful `StatusOr<>` with an empty `spanner::Row` to indicate
52 * end-of-stream.
53 */
54 virtual StatusOr<spanner::Row> NextRow() = 0;
55
56 /**
57 * Returns metadata about the result set, such as the field types and the
58 * transaction id created by the request.
59 *
60 * @see https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#resultsetmetadata
61 * for more information.
62 */
63 virtual absl::optional<google::spanner::v1::ResultSetMetadata> Metadata() = 0;
64
65 /**
66 * Returns statistics about the result set, such as the number of rows, and
67 * the query plan used to compute the results.
68 *
69 * @see https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#resultsetstats
70 * for more information.
71 */
72 virtual absl::optional<google::spanner::v1::ResultSetStats> Stats() const = 0;
73};
74
75/**
76 * Contains a hierarchical representation of the operations the database server
77 * performs in order to execute a particular SQL statement.
78 * [Query Plan
79 * proto](https://github.com/googleapis/googleapis/blob/master/google/spanner/v1/query_plan.proto)
80 *
81 * @par Example:
82 * @snippet samples.cc analyze-query
83 */
84using ExecutionPlan = ::google::spanner::v1::QueryPlan;
85
86/**
87 * Represents the stream of `Rows` returned from `spanner::Client::Read()` or
88 * `spanner::Client::ExecuteQuery()`.
89 *
90 * This is a range defined by the [Input Iterators][input-iterator] returned
91 * from its `begin()` and `end()` members. Callers may directly iterate the
92 * `RowStream` instance, which will return a sequence of `StatusOr<Row>`
93 * objects.
94 *
95 * For convenience, callers may wrap instances in a `StreamOf<std::tuple<...>>`
96 * object, which will automatically parse each `Row` into a `std::tuple` with
97 * the specified types.
98 *
99 * [input-iterator]: https://en.cppreference.com/w/cpp/named_req/InputIterator
100 */
101class RowStream {
102 public:
103 RowStream() = default;
104 explicit RowStream(std::unique_ptr<ResultSourceInterface> source)
105 : source_(std::move(source)) {}
106
107 // This class is movable but not copyable.
108 RowStream(RowStream&&) = default;
109 RowStream& operator=(RowStream&&) = default;
110
111 /// Returns a `RowStreamIterator` defining the beginning of this range.
113 return RowStreamIterator([this]() mutable { return source_->NextRow(); });
114 }
115
116 /// Returns a `RowStreamIterator` defining the end of this range.
117 // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
118 RowStreamIterator end() { return {}; }
119
120 /// Returns the number of rows modified by a DML statement.
121 std::int64_t RowsModified() const;
122
123 /**
124 * Retrieves the timestamp at which the read occurred.
125 *
126 * @note Only available if a read-only transaction was used.
127 */
128 absl::optional<Timestamp> ReadTimestamp() const;
129
130 private:
131 std::unique_ptr<ResultSourceInterface> source_;
132};
133
134/**
135 * Represents the result of a data modifying operation using
136 * `spanner::Client::ExecuteDml()`.
137 *
138 * This class encapsulates the result of a Cloud Spanner DML operation, i.e.,
139 * `INSERT`, `UPDATE`, or `DELETE`.
140 *
141 * @note `ExecuteDmlResult` returns the number of rows modified.
142 *
143 * @par Example:
144 * @snippet samples.cc execute-dml
145 */
146class DmlResult {
147 public:
148 DmlResult() = default;
149 explicit DmlResult(std::unique_ptr<ResultSourceInterface> source)
150 : source_(std::move(source)) {}
151
152 // This class is movable but not copyable.
153 DmlResult(DmlResult&&) = default;
154 DmlResult& operator=(DmlResult&&) = default;
155
156 /**
157 * Returns the number of rows modified by the DML statement.
158 *
159 * @note Partitioned DML only provides a lower bound of the rows modified, all
160 * other DML statements provide an exact count.
161 */
162 std::int64_t RowsModified() const;
163
164 private:
165 std::unique_ptr<ResultSourceInterface> source_;
166};
167
168/**
169 * Represents the stream of `Rows` and profile stats returned from
170 * `spanner::Client::ProfileQuery()`.
171 *
172 * This is a range defined by the [Input Iterators][input-iterator] returned
173 * from its `begin()` and `end()` members. Callers may directly iterate the
174 * `ProfileQueryResult` instance, which will return a sequence of
175 * `StatusOr<Row>` objects.
176 *
177 * For convenience, callers may wrap instances in a `StreamOf<std::tuple<...>>`
178 * object, which will automatically parse each `Row` into a `std::tuple` with
179 * the specified types.
180 *
181 * [input-iterator]: https://en.cppreference.com/w/cpp/named_req/InputIterator
182 *
183 * @par Example:
184 * @snippet samples.cc profile-query
185 */
186class ProfileQueryResult {
187 public:
188 ProfileQueryResult() = default;
189 explicit ProfileQueryResult(std::unique_ptr<ResultSourceInterface> source)
190 : source_(std::move(source)) {}
191
192 // This class is movable but not copyable.
195
196 /// Returns a `RowStreamIterator` defining the beginning of this result set.
198 return RowStreamIterator([this]() mutable { return source_->NextRow(); });
199 }
200
201 /// Returns a `RowStreamIterator` defining the end of this result set.
202 // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
203 RowStreamIterator end() { return {}; }
204
205 /**
206 * Retrieves the timestamp at which the read occurred.
207 *
208 * @note Only available if a read-only transaction was used.
209 */
210 absl::optional<Timestamp> ReadTimestamp() const;
211
212 /**
213 * Returns a collection of key value pair statistics for the SQL statement
214 * execution.
215 *
216 * @note Only available when the statement is executed and all results have
217 * been read.
218 */
219 absl::optional<std::unordered_map<std::string, std::string>> ExecutionStats()
220 const;
221
222 /**
223 * Returns the plan of execution for the SQL statement.
224 */
225 absl::optional<spanner::ExecutionPlan> ExecutionPlan() const;
226
227 private:
228 std::unique_ptr<ResultSourceInterface> source_;
229};
230
231/**
232 * Represents the result and profile stats of a data modifying operation using
233 * `spanner::Client::ProfileDml()`.
234 *
235 * This class encapsulates the result of a Cloud Spanner DML operation, i.e.,
236 * `INSERT`, `UPDATE`, or `DELETE`.
237 *
238 * @note `ProfileDmlResult` returns the number of rows modified, execution
239 * statistics, and query plan.
240 *
241 * @par Example:
242 * @snippet samples.cc profile-dml
243 */
244class ProfileDmlResult {
245 public:
246 ProfileDmlResult() = default;
247 explicit ProfileDmlResult(std::unique_ptr<ResultSourceInterface> source)
248 : source_(std::move(source)) {}
249
250 // This class is movable but not copyable.
253
254 /**
255 * Returns the number of rows modified by the DML statement.
256 *
257 * @note Partitioned DML only provides a lower bound of the rows modified, all
258 * other DML statements provide an exact count.
259 */
260 std::int64_t RowsModified() const;
261
262 /**
263 * Returns a collection of key value pair statistics for the SQL statement
264 * execution.
265 *
266 * @note Only available when the SQL statement is executed.
267 */
268 absl::optional<std::unordered_map<std::string, std::string>> ExecutionStats()
269 const;
270
271 /**
272 * Returns the plan of execution for the SQL statement.
273 */
274 absl::optional<spanner::ExecutionPlan> ExecutionPlan() const;
275
276 private:
277 std::unique_ptr<ResultSourceInterface> source_;
278};
279
280GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
281} // namespace spanner
282
283namespace spanner_internal {
284GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
285
286/// @deprecated Prefer using `spanner::ResultSourceInterface` directly.
287using ResultSourceInterface = spanner::ResultSourceInterface;
288
289GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
290} // namespace spanner_internal
291} // namespace cloud
292} // namespace google
293
294#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_RESULTS_H
Represents the result of a data modifying operation using spanner::Client::ExecuteDml().
Definition: results.h:146
DmlResult(DmlResult &&)=default
DmlResult & operator=(DmlResult &&)=default
std::int64_t RowsModified() const
Returns the number of rows modified by the DML statement.
DmlResult(std::unique_ptr< ResultSourceInterface > source)
Definition: results.h:149
Represents the result and profile stats of a data modifying operation using spanner::Client::ProfileD...
Definition: results.h:244
absl::optional< std::unordered_map< std::string, std::string > > ExecutionStats() const
Returns a collection of key value pair statistics for the SQL statement execution.
std::int64_t RowsModified() const
Returns the number of rows modified by the DML statement.
ProfileDmlResult & operator=(ProfileDmlResult &&)=default
ProfileDmlResult(std::unique_ptr< ResultSourceInterface > source)
Definition: results.h:247
ProfileDmlResult(ProfileDmlResult &&)=default
Represents the stream of Rows and profile stats returned from spanner::Client::ProfileQuery().
Definition: results.h:186
ProfileQueryResult(std::unique_ptr< ResultSourceInterface > source)
Definition: results.h:189
RowStreamIterator end()
Returns a RowStreamIterator defining the end of this result set.
Definition: results.h:203
ProfileQueryResult(ProfileQueryResult &&)=default
absl::optional< Timestamp > ReadTimestamp() const
Retrieves the timestamp at which the read occurred.
RowStreamIterator begin()
Returns a RowStreamIterator defining the beginning of this result set.
Definition: results.h:197
ProfileQueryResult & operator=(ProfileQueryResult &&)=default
absl::optional< std::unordered_map< std::string, std::string > > ExecutionStats() const
Returns a collection of key value pair statistics for the SQL statement execution.
Defines the interface for RowStream implementations.
Definition: results.h:42
virtual absl::optional< google::spanner::v1::ResultSetStats > Stats() const =0
Returns statistics about the result set, such as the number of rows, and the query plan used to compu...
virtual StatusOr< spanner::Row > NextRow()=0
Returns the next row in the stream.
virtual absl::optional< google::spanner::v1::ResultSetMetadata > Metadata()=0
Returns metadata about the result set, such as the field types and the transaction id created by the ...
A RowStreamIterator is an Input Iterator (see below) that returns a sequence of StatusOr<Row> objects...
Definition: row.h:251
RowStreamIterator()
Default constructs an "end" iterator.
RowStreamIterator(Source source)
Constructs a RowStreamIterator that will consume rows from the given source, which must not be nullpt...
Represents the stream of Rows returned from spanner::Client::Read() or spanner::Client::ExecuteQuery(...
Definition: results.h:101
RowStream & operator=(RowStream &&)=default
RowStreamIterator begin()
Returns a RowStreamIterator defining the beginning of this range.
Definition: results.h:112
absl::optional< Timestamp > ReadTimestamp() const
Retrieves the timestamp at which the read occurred.
RowStream(std::unique_ptr< ResultSourceInterface > source)
Definition: results.h:104
std::int64_t RowsModified() const
Returns the number of rows modified by a DML statement.
RowStream(RowStream &&)=default
RowStreamIterator end()
Returns a RowStreamIterator defining the end of this range.
Definition: results.h:118
A Row is a sequence of columns each with a name and an associated Value.
Definition: row.h:84
A representation of the Spanner TIMESTAMP type: An instant in time.
Definition: timestamp.h:54
Contains all the Cloud Spanner C++ client types and functions.
Definition: backoff_policy.h:23