Google Cloud Spanner C++ Client
A C++ Client Library for Google Cloud Spanner
results.h
Go to the documentation of this file.
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 // http://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 
20 #include "google/cloud/optional.h"
21 #include <google/spanner/v1/spanner.pb.h>
22 #include <memory>
23 #include <string>
24 #include <unordered_map>
25 
26 namespace google {
27 namespace cloud {
28 namespace spanner {
29 inline namespace SPANNER_CLIENT_NS {
30 
40 using ExecutionPlan = ::google::spanner::v1::QueryPlan;
41 
42 namespace internal {
43 class ResultSourceInterface {
44  public:
45  virtual ~ResultSourceInterface() = default;
46  // Returns OK Status with an empty Row to indicate end-of-stream.
47  virtual StatusOr<Row> NextRow() = 0;
48  virtual optional<google::spanner::v1::ResultSetMetadata> Metadata() = 0;
49  virtual optional<google::spanner::v1::ResultSetStats> Stats() const = 0;
50 };
51 } // namespace internal
52 
68 class RowStream {
69  public:
70  RowStream() = default;
71  explicit RowStream(std::unique_ptr<internal::ResultSourceInterface> source)
72  : source_(std::move(source)) {}
73 
74  // This class is movable but not copyable.
75  RowStream(RowStream&&) = default;
76  RowStream& operator=(RowStream&&) = default;
77 
80  return RowStreamIterator([this]() mutable { return source_->NextRow(); });
81  }
82 
84  // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
85  RowStreamIterator end() { return {}; }
86 
92  optional<Timestamp> ReadTimestamp() const;
93 
94  private:
95  std::unique_ptr<internal::ResultSourceInterface> source_;
96 };
97 
110 class DmlResult {
111  public:
112  DmlResult() = default;
113  explicit DmlResult(std::unique_ptr<internal::ResultSourceInterface> source)
114  : source_(std::move(source)) {}
115 
116  // This class is movable but not copyable.
117  DmlResult(DmlResult&&) = default;
118  DmlResult& operator=(DmlResult&&) = default;
119 
126  std::int64_t RowsModified() const;
127 
128  private:
129  std::unique_ptr<internal::ResultSourceInterface> source_;
130 };
131 
151  public:
152  ProfileQueryResult() = default;
154  std::unique_ptr<internal::ResultSourceInterface> source)
155  : source_(std::move(source)) {}
156 
157  // This class is movable but not copyable.
159  ProfileQueryResult& operator=(ProfileQueryResult&&) = default;
160 
163  return RowStreamIterator([this]() mutable { return source_->NextRow(); });
164  }
165 
167  // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
168  RowStreamIterator end() { return {}; }
169 
175  optional<Timestamp> ReadTimestamp() const;
176 
184  optional<std::unordered_map<std::string, std::string>> ExecutionStats() const;
185 
189  optional<spanner::ExecutionPlan> ExecutionPlan() const;
190 
191  private:
192  std::unique_ptr<internal::ResultSourceInterface> source_;
193 };
194 
209  public:
210  ProfileDmlResult() = default;
212  std::unique_ptr<internal::ResultSourceInterface> source)
213  : source_(std::move(source)) {}
214 
215  // This class is movable but not copyable.
216  ProfileDmlResult(ProfileDmlResult&&) = default;
217  ProfileDmlResult& operator=(ProfileDmlResult&&) = default;
218 
225  std::int64_t RowsModified() const;
226 
233  optional<std::unordered_map<std::string, std::string>> ExecutionStats() const;
234 
238  optional<spanner::ExecutionPlan> ExecutionPlan() const;
239 
240  private:
241  std::unique_ptr<internal::ResultSourceInterface> source_;
242 };
243 } // namespace SPANNER_CLIENT_NS
244 } // namespace spanner
245 } // namespace cloud
246 } // namespace google
247 
248 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_RESULTS_H
Represents the result and profile stats of a data modifying operation using spanner::Client::ProfileD...
Definition: results.h:208
::google::spanner::v1::QueryPlan ExecutionPlan
Contains a hierarchical representation of the operations the database server performs in order to exe...
Definition: results.h:40
RowStreamIterator end()
Returns a RowStreamIterator defining the end of this range.
Definition: results.h:85
Represents the stream of Rows and profile stats returned from spanner::Client::ProfileQuery().
Definition: results.h:150
ProfileDmlResult(std::unique_ptr< internal::ResultSourceInterface > source)
Definition: results.h:211
RowStreamIterator begin()
Returns a RowStreamIterator defining the beginning of this result set.
Definition: results.h:162
RowStream(std::unique_ptr< internal::ResultSourceInterface > source)
Definition: results.h:71
A RowStreamIterator is an Input Iterator (see below) that returns a sequence of StatusOr<Row> objects...
Definition: row.h:244
RowStreamIterator end()
Returns a RowStreamIterator defining the end of this result set.
Definition: results.h:168
Contains all the Cloud Spanner C++ client types and functions.
RowStreamIterator begin()
Returns a RowStreamIterator defining the beginning of this range.
Definition: results.h:79
ProfileQueryResult(std::unique_ptr< internal::ResultSourceInterface > source)
Definition: results.h:153
#define SPANNER_CLIENT_NS
Definition: version.h:22
Represents the result of a data modifying operation using spanner::Client::ExecuteDml().
Definition: results.h:110
DmlResult(std::unique_ptr< internal::ResultSourceInterface > source)
Definition: results.h:113
Represents the stream of Rows returned from spanner::Client::Read() or spanner::Client::ExecuteQuery(...
Definition: results.h:68