Google Cloud Spanner C++ Client
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 
16 #include <google/spanner/v1/spanner.pb.h>
17 
18 namespace google {
19 namespace cloud {
20 namespace spanner {
21 inline namespace SPANNER_CLIENT_NS {
22 
23 QueryPartition::QueryPartition(std::string transaction_id,
24  std::string session_id,
25  std::string partition_token,
26  SqlStatement sql_statement)
27  : transaction_id_(std::move(transaction_id)),
28  session_id_(std::move(session_id)),
29  partition_token_(std::move(partition_token)),
30  sql_statement_(std::move(sql_statement)) {}
31 
32 bool operator==(QueryPartition const& a, QueryPartition const& b) {
33  return a.transaction_id_ == b.transaction_id_ &&
34  a.session_id_ == b.session_id_ &&
35  a.partition_token_ == b.partition_token_ &&
36  a.sql_statement_ == b.sql_statement_;
37 }
38 
39 StatusOr<std::string> SerializeQueryPartition(
40  QueryPartition const& query_partition) {
41  google::spanner::v1::ExecuteSqlRequest proto;
42  proto.set_partition_token(query_partition.partition_token());
43  proto.set_session(query_partition.session_id());
44  proto.mutable_transaction()->set_id(query_partition.transaction_id());
45  proto.set_sql(query_partition.sql_statement_.sql());
46 
47  for (auto const& param : query_partition.sql_statement_.params()) {
48  auto const& param_name = param.first;
49  auto const& type_value = internal::ToProto(param.second);
50  (*proto.mutable_params()->mutable_fields())[param_name] = type_value.second;
51  (*proto.mutable_param_types())[param_name] = type_value.first;
52  }
53  std::string serialized_proto;
54  if (proto.SerializeToString(&serialized_proto)) {
55  return serialized_proto;
56  }
57  return Status(StatusCode::kInvalidArgument,
58  "Failed to serialize QueryPartition");
59 }
60 
61 StatusOr<QueryPartition> DeserializeQueryPartition(
62  std::string const& serialized_query_partition) {
63  google::spanner::v1::ExecuteSqlRequest proto;
64  if (!proto.ParseFromString(serialized_query_partition)) {
65  return Status(StatusCode::kInvalidArgument,
66  "Failed to deserialize into QueryPartition");
67  }
68 
69  SqlStatement::ParamType sql_parameters;
70  if (proto.has_params()) {
71  auto const& param_types = proto.param_types();
72  for (auto& param : *(proto.mutable_params()->mutable_fields())) {
73  auto const& param_name = param.first;
74  auto iter = param_types.find(param_name);
75  if (iter != param_types.end()) {
76  auto const& param_type = iter->second;
77  sql_parameters.insert(std::make_pair(
78  param_name,
79  internal::FromProto(param_type, std::move(param.second))));
80  }
81  }
82  }
83 
84  QueryPartition query_partition(proto.transaction().id(), proto.session(),
85  proto.partition_token(),
86  SqlStatement(proto.sql(), sql_parameters));
87  return query_partition;
88 }
89 
90 namespace internal {
91 QueryPartition MakeQueryPartition(std::string const& transaction_id,
92  std::string const& session_id,
93  std::string const& partition_token,
94  SqlStatement const& sql_statement) {
95  return QueryPartition(transaction_id, session_id, partition_token,
96  sql_statement);
97 }
98 
99 Connection::SqlParams MakeSqlParams(QueryPartition const& query_partition) {
100  return {internal::MakeTransactionFromIds(query_partition.session_id(),
101  query_partition.transaction_id()),
102  query_partition.sql_statement(), QueryOptions{},
103  query_partition.partition_token()};
104 }
105 
106 } // namespace internal
107 } // namespace SPANNER_CLIENT_NS
108 } // namespace spanner
109 } // namespace cloud
110 } // namespace google
StatusOr< std::string > SerializeQueryPartition(QueryPartition const &query_partition)
Serializes an instance of QueryPartition to a string of bytes.
std::string const & sql() const
Returns the SQL statement.
Definition: sql_statement.h:78
bool operator==(Backup const &a, Backup const &b)
Definition: backup.cc:29
The QueryPartition class is a regular type that represents a single slice of a parallel SQL read.
std::unordered_map< std::string, Value > ParamType
Type alias for parameter collection.
Definition: sql_statement.h:58
google::cloud::optional< std::string > partition_token
Definition: connection.h:93
Contains all the Cloud Spanner C++ client types and functions.
#define SPANNER_CLIENT_NS
Definition: version.h:22
StatusOr< QueryPartition > DeserializeQueryPartition(std::string const &serialized_query_partition)
Deserializes the provided string into a QueryPartition.
ParamType const & params() const
Returns the collection of parameters.
Definition: sql_statement.h:84
Represents a potentially parameterized SQL statement.
Definition: sql_statement.h:55