Google Cloud Spanner C++ Client 2.8.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_internal {
31GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
32
33class ResultSourceInterface {
34 public:
35 virtual ~ResultSourceInterface() = default;
36 // Returns OK Status with an empty Row to indicate end-of-stream.
37 virtual StatusOr<spanner::Row> NextRow() = 0;
38 virtual absl::optional<google::spanner::v1::ResultSetMetadata> Metadata() = 0;
39 virtual absl::optional<google::spanner::v1::ResultSetStats> Stats() const = 0;
40};
41
42GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
43} // namespace spanner_internal
44
45namespace spanner {
46GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
47
48/**
49 * Contains a hierarchical representation of the operations the database server
50 * performs in order to execute a particular SQL statement.
51 * [Query Plan
52 * proto](https://github.com/googleapis/googleapis/blob/master/google/spanner/v1/query_plan.proto)
53 *
54 * @par Example:
55 * @snippet samples.cc analyze-query
56 */
57using ExecutionPlan = ::google::spanner::v1::QueryPlan;
58
59/**
60 * Represents the stream of `Rows` returned from `spanner::Client::Read()` or
61 * `spanner::Client::ExecuteQuery()`.
62 *
63 * This is a range defined by the [Input Iterators][input-iterator] returned
64 * from its `begin()` and `end()` members. Callers may directly iterate the
65 * `RowStream` instance, which will return a sequence of `StatusOr<Row>`
66 * objects.
67 *
68 * For convenience, callers may wrap instances in a `StreamOf<std::tuple<...>>`
69 * object, which will automatically parse each `Row` into a `std::tuple` with
70 * the specified types.
71 *
72 * [input-iterator]: https://en.cppreference.com/w/cpp/named_req/InputIterator
73 */
74class RowStream {
75 public:
76 RowStream() = default;
77 explicit RowStream(
78 std::unique_ptr<spanner_internal::ResultSourceInterface> source)
79 : source_(std::move(source)) {}
80
81 // This class is movable but not copyable.
82 RowStream(RowStream&&) = default;
83 RowStream& operator=(RowStream&&) = default;
84
85 /// Returns a `RowStreamIterator` defining the beginning of this range.
87 return RowStreamIterator([this]() mutable { return source_->NextRow(); });
88 }
89
90 /// Returns a `RowStreamIterator` defining the end of this range.
91 // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
92 RowStreamIterator end() { return {}; }
93
94 /// Returns the number of rows modified by a DML statement.
95 std::int64_t RowsModified() const;
96
97 /**
98 * Retrieves the timestamp at which the read occurred.
99 *
100 * @note Only available if a read-only transaction was used.
101 */
102 absl::optional<Timestamp> ReadTimestamp() const;
103
104 private:
105 std::unique_ptr<spanner_internal::ResultSourceInterface> source_;
106};
107
108/**
109 * Represents the result of a data modifying operation using
110 * `spanner::Client::ExecuteDml()`.
111 *
112 * This class encapsulates the result of a Cloud Spanner DML operation, i.e.,
113 * `INSERT`, `UPDATE`, or `DELETE`.
114 *
115 * @note `ExecuteDmlResult` returns the number of rows modified.
116 *
117 * @par Example:
118 * @snippet samples.cc execute-dml
119 */
120class DmlResult {
121 public:
122 DmlResult() = default;
123 explicit DmlResult(
124 std::unique_ptr<spanner_internal::ResultSourceInterface> source)
125 : source_(std::move(source)) {}
126
127 // This class is movable but not copyable.
128 DmlResult(DmlResult&&) = default;
129 DmlResult& operator=(DmlResult&&) = default;
130
131 /**
132 * Returns the number of rows modified by the DML statement.
133 *
134 * @note Partitioned DML only provides a lower bound of the rows modified, all
135 * other DML statements provide an exact count.
136 */
137 std::int64_t RowsModified() const;
138
139 private:
140 std::unique_ptr<spanner_internal::ResultSourceInterface> source_;
141};
142
143/**
144 * Represents the stream of `Rows` and profile stats returned from
145 * `spanner::Client::ProfileQuery()`.
146 *
147 * This is a range defined by the [Input Iterators][input-iterator] returned
148 * from its `begin()` and `end()` members. Callers may directly iterate the
149 * `ProfileQueryResult` instance, which will return a sequence of
150 * `StatusOr<Row>` objects.
151 *
152 * For convenience, callers may wrap instances in a `StreamOf<std::tuple<...>>`
153 * object, which will automatically parse each `Row` into a `std::tuple` with
154 * the specified types.
155 *
156 * [input-iterator]: https://en.cppreference.com/w/cpp/named_req/InputIterator
157 *
158 * @par Example:
159 * @snippet samples.cc profile-query
160 */
161class ProfileQueryResult {
162 public:
163 ProfileQueryResult() = default;
164 explicit ProfileQueryResult(
165 std::unique_ptr<spanner_internal::ResultSourceInterface> source)
166 : source_(std::move(source)) {}
167
168 // This class is movable but not copyable.
171
172 /// Returns a `RowStreamIterator` defining the beginning of this result set.
174 return RowStreamIterator([this]() mutable { return source_->NextRow(); });
175 }
176
177 /// Returns a `RowStreamIterator` defining the end of this result set.
178 // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
179 RowStreamIterator end() { return {}; }
180
181 /**
182 * Retrieves the timestamp at which the read occurred.
183 *
184 * @note Only available if a read-only transaction was used.
185 */
186 absl::optional<Timestamp> ReadTimestamp() const;
187
188 /**
189 * Returns a collection of key value pair statistics for the SQL statement
190 * execution.
191 *
192 * @note Only available when the statement is executed and all results have
193 * been read.
194 */
195 absl::optional<std::unordered_map<std::string, std::string>> ExecutionStats()
196 const;
197
198 /**
199 * Returns the plan of execution for the SQL statement.
200 */
201 absl::optional<spanner::ExecutionPlan> ExecutionPlan() const;
202
203 private:
204 std::unique_ptr<spanner_internal::ResultSourceInterface> source_;
205};
206
207/**
208 * Represents the result and profile stats of a data modifying operation using
209 * `spanner::Client::ProfileDml()`.
210 *
211 * This class encapsulates the result of a Cloud Spanner DML operation, i.e.,
212 * `INSERT`, `UPDATE`, or `DELETE`.
213 *
214 * @note `ProfileDmlResult` returns the number of rows modified, execution
215 * statistics, and query plan.
216 *
217 * @par Example:
218 * @snippet samples.cc profile-dml
219 */
220class ProfileDmlResult {
221 public:
222 ProfileDmlResult() = default;
223 explicit ProfileDmlResult(
224 std::unique_ptr<spanner_internal::ResultSourceInterface> source)
225 : source_(std::move(source)) {}
226
227 // This class is movable but not copyable.
230
231 /**
232 * Returns the number of rows modified by the DML statement.
233 *
234 * @note Partitioned DML only provides a lower bound of the rows modified, all
235 * other DML statements provide an exact count.
236 */
237 std::int64_t RowsModified() const;
238
239 /**
240 * Returns a collection of key value pair statistics for the SQL statement
241 * execution.
242 *
243 * @note Only available when the SQL statement is executed.
244 */
245 absl::optional<std::unordered_map<std::string, std::string>> ExecutionStats()
246 const;
247
248 /**
249 * Returns the plan of execution for the SQL statement.
250 */
251 absl::optional<spanner::ExecutionPlan> ExecutionPlan() const;
252
253 private:
254 std::unique_ptr<spanner_internal::ResultSourceInterface> source_;
255};
256
257GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
258} // namespace spanner
259} // namespace cloud
260} // namespace google
261
262#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_RESULTS_H
Represents the result of a data modifying operation using spanner::Client::ExecuteDml().
Definition: results.h:120
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< spanner_internal::ResultSourceInterface > source)
Definition: results.h:123
Represents the result and profile stats of a data modifying operation using spanner::Client::ProfileD...
Definition: results.h:220
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< spanner_internal::ResultSourceInterface > source)
Definition: results.h:223
ProfileDmlResult(ProfileDmlResult &&)=default
Represents the stream of Rows and profile stats returned from spanner::Client::ProfileQuery().
Definition: results.h:161
RowStreamIterator end()
Returns a RowStreamIterator defining the end of this result set.
Definition: results.h:179
ProfileQueryResult(ProfileQueryResult &&)=default
absl::optional< Timestamp > ReadTimestamp() const
Retrieves the timestamp at which the read occurred.
ProfileQueryResult(std::unique_ptr< spanner_internal::ResultSourceInterface > source)
Definition: results.h:164
RowStreamIterator begin()
Returns a RowStreamIterator defining the beginning of this result set.
Definition: results.h:173
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.
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:74
RowStream & operator=(RowStream &&)=default
RowStreamIterator begin()
Returns a RowStreamIterator defining the beginning of this range.
Definition: results.h:86
RowStream(std::unique_ptr< spanner_internal::ResultSourceInterface > source)
Definition: results.h:77
absl::optional< Timestamp > ReadTimestamp() const
Retrieves the timestamp at which the read occurred.
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:92
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