Google Cloud Spanner C++ Client
A C++ Client Library for Google Cloud Spanner
results.cc
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 
16 #include "google/cloud/optional.h"
17 #include <google/spanner/v1/result_set.pb.h>
18 #include <memory>
19 #include <string>
20 #include <unordered_map>
21 
22 namespace google {
23 namespace cloud {
24 namespace spanner {
25 inline namespace SPANNER_CLIENT_NS {
26 namespace {
27 
28 optional<Timestamp> GetReadTimestamp(
29  std::unique_ptr<internal::ResultSourceInterface> const& source) {
30  auto metadata = source->Metadata();
31  if (metadata.has_value() && metadata->has_transaction() &&
32  metadata->transaction().has_read_timestamp()) {
33  return internal::TimestampFromProto(
34  metadata->transaction().read_timestamp());
35  }
36  return optional<Timestamp>();
37 }
38 
39 std::int64_t GetRowsModified(
40  std::unique_ptr<internal::ResultSourceInterface> const& source) {
41  return source->Stats()->row_count_exact();
42 }
43 
44 optional<std::unordered_map<std::string, std::string>> GetExecutionStats(
45  std::unique_ptr<internal::ResultSourceInterface> const& source) {
46  auto stats = source->Stats();
47  if (stats && stats->has_query_stats()) {
48  std::unordered_map<std::string, std::string> execution_stats;
49  for (auto const& entry : stats->query_stats().fields()) {
50  execution_stats.insert(
51  std::make_pair(entry.first, entry.second.string_value()));
52  }
53  return execution_stats;
54  }
55  return {};
56 }
57 
58 optional<spanner::ExecutionPlan> GetExecutionPlan(
59  std::unique_ptr<internal::ResultSourceInterface> const& source) {
60  auto stats = source->Stats();
61  if (stats && stats->has_query_plan()) {
62  return source->Stats()->query_plan();
63  }
64  return {};
65 }
66 } // namespace
67 
68 optional<Timestamp> RowStream::ReadTimestamp() const {
69  return GetReadTimestamp(source_);
70 }
71 
72 optional<Timestamp> ProfileQueryResult::ReadTimestamp() const {
73  return GetReadTimestamp(source_);
74 }
75 
76 std::int64_t DmlResult::RowsModified() const {
77  return GetRowsModified(source_);
78 }
79 
80 std::int64_t ProfileDmlResult::RowsModified() const {
81  return GetRowsModified(source_);
82 }
83 
84 optional<std::unordered_map<std::string, std::string>>
85 ProfileQueryResult::ExecutionStats() const {
86  return GetExecutionStats(source_);
87 }
88 
89 optional<spanner::ExecutionPlan> ProfileQueryResult::ExecutionPlan() const {
90  return GetExecutionPlan(source_);
91 }
92 
93 optional<std::unordered_map<std::string, std::string>>
94 ProfileDmlResult::ExecutionStats() const {
95  return GetExecutionStats(source_);
96 }
97 
98 optional<spanner::ExecutionPlan> ProfileDmlResult::ExecutionPlan() const {
99  return GetExecutionPlan(source_);
100 }
101 
102 } // namespace SPANNER_CLIENT_NS
103 } // namespace spanner
104 } // namespace cloud
105 } // namespace google
::google::spanner::v1::QueryPlan ExecutionPlan
Contains a hierarchical representation of the operations the database server performs in order to exe...
Definition: results.h:40
Contains all the Cloud Spanner C++ client types and functions.
#define SPANNER_CLIENT_NS
Definition: version.h:22