Google Cloud Spanner C++ Client  1.32.0
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 
15 #include "google/cloud/spanner/results.h"
16 #include "absl/types/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 absl::optional<Timestamp> GetReadTimestamp(
29  std::unique_ptr<spanner_internal::ResultSourceInterface> const& source) {
30  auto metadata = source->Metadata();
31  absl::optional<Timestamp> timestamp;
32  if (metadata.has_value() && metadata->has_transaction() &&
33  metadata->transaction().has_read_timestamp()) {
34  auto ts = MakeTimestamp(metadata->transaction().read_timestamp());
35  if (ts) timestamp = *std::move(ts);
36  }
37  return timestamp;
38 }
39 
40 std::int64_t GetRowsModified(
41  std::unique_ptr<spanner_internal::ResultSourceInterface> const& source) {
42  return source->Stats()->row_count_exact();
43 }
44 
45 absl::optional<std::unordered_map<std::string, std::string>> GetExecutionStats(
46  std::unique_ptr<spanner_internal::ResultSourceInterface> const& source) {
47  auto stats = source->Stats();
48  if (stats && stats->has_query_stats()) {
49  std::unordered_map<std::string, std::string> execution_stats;
50  for (auto const& entry : stats->query_stats().fields()) {
51  execution_stats.insert(
52  std::make_pair(entry.first, entry.second.string_value()));
53  }
54  return execution_stats;
55  }
56  return {};
57 }
58 
59 absl::optional<spanner::ExecutionPlan> GetExecutionPlan(
60  std::unique_ptr<spanner_internal::ResultSourceInterface> const& source) {
61  auto stats = source->Stats();
62  if (stats && stats->has_query_plan()) {
63  return source->Stats()->query_plan();
64  }
65  return {};
66 }
67 } // namespace
68 
69 absl::optional<Timestamp> RowStream::ReadTimestamp() const {
70  return GetReadTimestamp(source_);
71 }
72 
73 absl::optional<Timestamp> ProfileQueryResult::ReadTimestamp() const {
74  return GetReadTimestamp(source_);
75 }
76 
77 std::int64_t DmlResult::RowsModified() const {
78  return GetRowsModified(source_);
79 }
80 
81 std::int64_t ProfileDmlResult::RowsModified() const {
82  return GetRowsModified(source_);
83 }
84 
85 absl::optional<std::unordered_map<std::string, std::string>>
87  return GetExecutionStats(source_);
88 }
89 
90 absl::optional<spanner::ExecutionPlan> ProfileQueryResult::ExecutionPlan()
91  const {
92  return GetExecutionPlan(source_);
93 }
94 
95 absl::optional<std::unordered_map<std::string, std::string>>
97  return GetExecutionStats(source_);
98 }
99 
100 absl::optional<spanner::ExecutionPlan> ProfileDmlResult::ExecutionPlan() const {
101  return GetExecutionPlan(source_);
102 }
103 
104 } // namespace SPANNER_CLIENT_NS
105 } // namespace spanner
106 } // namespace cloud
107 } // namespace google