Google Cloud Spanner C++ Client
A C++ Client Library for Google Cloud Spanner
sql_statement.h
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 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_SQL_STATEMENT_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_SQL_STATEMENT_H
17 
19 #include "google/cloud/status_or.h"
20 #include <google/spanner/v1/spanner.pb.h>
21 #include <string>
22 #include <unordered_map>
23 
24 namespace google {
25 namespace cloud {
26 namespace spanner {
27 inline namespace SPANNER_CLIENT_NS {
28 
29 class SqlStatement; // Defined later in this file.
30 
31 // Internal implementation details that callers should not use.
32 namespace internal {
33 // Use this proto type because it conveniently wraps all three attributes
34 // required to represent a SQL statement.
35 using SqlStatementProto =
36  google::spanner::v1::ExecuteBatchDmlRequest::Statement;
37 SqlStatementProto ToProto(SqlStatement s);
38 } // namespace internal
39 
55 class SqlStatement {
56  public:
58  using ParamType = std::unordered_map<std::string, Value>;
59 
60  SqlStatement() = default;
62  explicit SqlStatement(std::string statement)
63  : statement_(std::move(statement)) {}
65  SqlStatement(std::string statement, ParamType params)
66  : statement_(std::move(statement)), params_(std::move(params)) {}
67 
69  SqlStatement(SqlStatement const&) = default;
70  SqlStatement(SqlStatement&&) = default;
71  SqlStatement& operator=(SqlStatement const&) = default;
72  SqlStatement& operator=(SqlStatement&&) = default;
73 
78  std::string const& sql() const { return statement_; }
79 
84  ParamType const& params() const { return params_; }
85 
89  std::vector<std::string> ParameterNames() const;
90 
96  google::cloud::StatusOr<Value> GetParameter(
97  std::string const& parameter_name) const;
98 
99  friend bool operator==(SqlStatement const& a, SqlStatement const& b) {
100  return a.statement_ == b.statement_ && a.params_ == b.params_;
101  }
102  friend bool operator!=(SqlStatement const& a, SqlStatement const& b) {
103  return !(a == b);
104  }
105 
112  friend std::ostream& operator<<(std::ostream& os, SqlStatement const& stmt);
113 
114  private:
115  friend internal::SqlStatementProto internal::ToProto(SqlStatement s);
116 
117  std::string statement_;
118  ParamType params_;
119 };
120 
121 } // namespace SPANNER_CLIENT_NS
122 } // namespace spanner
123 } // namespace cloud
124 } // namespace google
125 
126 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_SQL_STATEMENT_H
friend bool operator==(SqlStatement const &a, SqlStatement const &b)
Definition: sql_statement.h:99
std::string const & sql() const
Returns the SQL statement.
Definition: sql_statement.h:78
SqlStatement(std::string statement, ParamType params)
Constructs a SqlStatement with specified parameters.
Definition: sql_statement.h:65
friend bool operator!=(SqlStatement const &a, SqlStatement const &b)
std::unordered_map< std::string, Value > ParamType
Type alias for parameter collection.
Definition: sql_statement.h:58
Contains all the Cloud Spanner C++ client types and functions.
SqlStatement(std::string statement)
Constructs a SqlStatement without parameters.
Definition: sql_statement.h:62
#define SPANNER_CLIENT_NS
Definition: version.h:22
ParamType const & params() const
Returns the collection of parameters.
Definition: sql_statement.h:84
std::ostream & operator<<(std::ostream &os, Backup const &bn)
Definition: backup.cc:35
Represents a potentially parameterized SQL statement.
Definition: sql_statement.h:55