Google Cloud Spanner C++ Client  1.32.0
A C++ Client Library for Google Cloud Spanner
sql_statement.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/sql_statement.h"
16 #include <algorithm>
17 
18 namespace google {
19 namespace cloud {
20 namespace spanner_internal {
21 inline namespace SPANNER_CLIENT_NS {
22 SqlStatementProto SqlStatementInternals::ToProto(spanner::SqlStatement s) {
23  ::google::spanner::v1::ExecuteBatchDmlRequest::Statement statement_proto;
24  statement_proto.set_sql(std::move(s.statement_));
25  if (!s.params_.empty()) {
26  auto& values = *statement_proto.mutable_params()->mutable_fields();
27  auto& types = *statement_proto.mutable_param_types();
28  for (auto& param : s.params_) {
29  auto type_and_value = spanner_internal::ToProto(std::move(param.second));
30  values[param.first] = std::move(type_and_value.second);
31  types[param.first] = std::move(type_and_value.first);
32  }
33  }
34  return statement_proto;
35 }
36 } // namespace SPANNER_CLIENT_NS
37 } // namespace spanner_internal
38 
39 namespace spanner {
40 inline namespace SPANNER_CLIENT_NS {
41 
42 std::vector<std::string> SqlStatement::ParameterNames() const {
43  std::vector<std::string> keys;
44  keys.reserve(params_.size());
45  for (auto const& p : params_) {
46  keys.push_back(p.first);
47  }
48  return keys;
49 }
50 
52  std::string const& parameter_name) const {
53  auto iter = params_.find(parameter_name);
54  if (iter != params_.end()) {
55  return iter->second;
56  }
57  return Status(StatusCode::kNotFound, "No such parameter: " + parameter_name);
58 }
59 
60 std::ostream& operator<<(std::ostream& os, SqlStatement const& stmt) {
61  os << stmt.statement_;
62  for (auto const& param : stmt.params_) {
63  os << "\n[param]: {" << param.first << "=" << param.second << "}";
64  }
65  return os;
66 }
67 
68 } // namespace SPANNER_CLIENT_NS
69 } // namespace spanner
70 } // namespace cloud
71 } // namespace google