Google Cloud Spanner C++ Client  1.33.0
A C++ Client Library for Google Cloud Spanner
query_partition.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/query_partition.h"
16 #include <google/spanner/v1/spanner.pb.h>
17 
18 namespace google {
19 namespace cloud {
20 namespace spanner {
22 
23 QueryPartition::QueryPartition(std::string transaction_id,
24  std::string transaction_tag,
25  std::string session_id,
26  std::string partition_token,
27  SqlStatement sql_statement)
28  : transaction_id_(std::move(transaction_id)),
29  transaction_tag_(std::move(transaction_tag)),
30  session_id_(std::move(session_id)),
31  partition_token_(std::move(partition_token)),
32  sql_statement_(std::move(sql_statement)) {}
33 
34 bool operator==(QueryPartition const& a, QueryPartition const& b) {
35  return a.transaction_id_ == b.transaction_id_ &&
36  a.transaction_tag_ == b.transaction_tag_ &&
37  a.session_id_ == b.session_id_ &&
38  a.partition_token_ == b.partition_token_ &&
39  a.sql_statement_ == b.sql_statement_;
40 }
41 
42 StatusOr<std::string> SerializeQueryPartition(
43  QueryPartition const& query_partition) {
44  google::spanner::v1::ExecuteSqlRequest proto;
45 
46  proto.set_partition_token(query_partition.partition_token());
47  proto.set_session(query_partition.session_id());
48  proto.mutable_transaction()->set_id(query_partition.transaction_id());
49  proto.set_sql(query_partition.sql_statement_.sql());
50  for (auto const& param : query_partition.sql_statement_.params()) {
51  auto const& param_name = param.first;
52  auto const& type_value = spanner_internal::ToProto(param.second);
53  (*proto.mutable_params()->mutable_fields())[param_name] = type_value.second;
54  (*proto.mutable_param_types())[param_name] = type_value.first;
55  }
56 
57  // QueryOptions are not serialized, but are instead applied on the remote
58  // side during the Client::ExecuteQuery(QueryPartition, QueryOptions) call.
59  // However, we do encode any transaction tag in proto.request_options.
60  proto.mutable_request_options()->set_transaction_tag(
61  query_partition.transaction_tag());
62 
63  std::string serialized_proto;
64  if (proto.SerializeToString(&serialized_proto)) {
65  return serialized_proto;
66  }
68  "Failed to serialize QueryPartition");
69 }
70 
72  std::string const& serialized_query_partition) {
73  google::spanner::v1::ExecuteSqlRequest proto;
74  if (!proto.ParseFromString(serialized_query_partition)) {
76  "Failed to deserialize into QueryPartition");
77  }
78 
79  SqlStatement::ParamType sql_parameters;
80  if (proto.has_params()) {
81  auto const& param_types = proto.param_types();
82  for (auto& param : *(proto.mutable_params()->mutable_fields())) {
83  auto const& param_name = param.first;
84  auto iter = param_types.find(param_name);
85  if (iter != param_types.end()) {
86  auto const& param_type = iter->second;
87  sql_parameters.insert(std::make_pair(
88  param_name,
89  spanner_internal::FromProto(param_type, std::move(param.second))));
90  }
91  }
92  }
93 
94  QueryPartition query_partition(proto.transaction().id(),
95  proto.request_options().transaction_tag(),
96  proto.session(), proto.partition_token(),
97  SqlStatement(proto.sql(), sql_parameters));
98  return query_partition;
99 }
100 
102 } // namespace spanner
103 } // namespace cloud
104 } // namespace google