15#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_MUTATIONS_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_MUTATIONS_H
18#include "google/cloud/spanner/keys.h"
19#include "google/cloud/spanner/value.h"
20#include "google/cloud/spanner/version.h"
21#include <google/spanner/v1/mutation.pb.h>
27namespace spanner_internal {
28GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
30class WriteMutationBuilder;
31class DeleteMutationBuilder;
32GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
36GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
39
40
41
42
43
44
45
46
47
48
49
50
51
55
56
57
58
72 google::spanner::v1::Mutation
as_proto() && {
return std::move(m_); }
76
77
78
79
80
81
85 google::spanner::v1::Mutation& proto() & {
return m_; }
87 template <
typename Op>
88 friend class spanner_internal::WriteMutationBuilder;
89 friend class spanner_internal::DeleteMutationBuilder;
90 explicit Mutation(
google::spanner::v1::Mutation m) : m_(std::move(m)) {}
92 google::spanner::v1::Mutation m_;
96
97
98
99using Mutations = std::vector<
Mutation>;
101GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
106namespace spanner_internal {
107GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
109template <
typename Op>
110class WriteMutationBuilder {
112 WriteMutationBuilder(std::string table_name,
113 std::vector<std::string> column_names) {
114 auto& field = Op::mutable_field(m_.proto());
115 field.set_table(std::move(table_name));
116 field.mutable_columns()->Reserve(
static_cast<
int>(column_names.size()));
117 for (
auto& name : column_names) {
118 field.add_columns(std::move(name));
125 WriteMutationBuilder& AddRow(std::vector<
spanner::
Value> values) & {
126 auto& lv = *Op::mutable_field(m_.proto()).add_values();
127 for (
auto& v : values) {
128 std::tie(std::ignore, *lv.add_values()) =
129 spanner_internal::ToProto(std::move(v));
134 WriteMutationBuilder&& AddRow(std::vector<
spanner::
Value> values) && {
135 return std::move(AddRow(std::move(values)));
138 template <
typename... Ts>
139 WriteMutationBuilder& EmplaceRow(Ts&&... values) & {
140 return AddRow({
spanner::
Value(std::forward<Ts>(values))...});
143 template <
typename... Ts>
144 WriteMutationBuilder&& EmplaceRow(Ts&&... values) && {
145 return std::move(EmplaceRow(std::forward<Ts>(values)...));
153 static google::spanner::v1::Mutation::Write& mutable_field(
154 google::spanner::v1::Mutation& m) {
155 return *m.mutable_insert();
160 static google::spanner::v1::Mutation::Write& mutable_field(
161 google::spanner::v1::Mutation& m) {
162 return *m.mutable_update();
166struct InsertOrUpdateOp {
167 static google::spanner::v1::Mutation::Write& mutable_field(
168 google::spanner::v1::Mutation& m) {
169 return *m.mutable_insert_or_update();
174 static google::spanner::v1::Mutation::Write& mutable_field(
175 google::spanner::v1::Mutation& m) {
176 return *m.mutable_replace();
180class DeleteMutationBuilder {
182 DeleteMutationBuilder(std::string table_name,
spanner::
KeySet keys) {
183 auto& field = *m_.proto().mutable_delete_();
184 field.set_table(std::move(table_name));
185 *field.mutable_key_set() = spanner_internal::ToProto(std::move(keys));
195GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
199GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
202
203
204
205
206
207
208
209
210
211
212
213using InsertMutationBuilder =
214 spanner_internal::WriteMutationBuilder<spanner_internal::InsertOp>;
217
218
219
220
221
222
223
224
225
226
227
228template <
typename... Ts>
230 std::vector<std::string> columns, Ts&&... values) {
231 return InsertMutationBuilder(std::move(table_name), std::move(columns))
232 .EmplaceRow(std::forward<Ts>(values)...)
237
238
239
240
241
242
243
244
245
246
247
248using UpdateMutationBuilder =
249 spanner_internal::WriteMutationBuilder<spanner_internal::UpdateOp>;
252
253
254
255
256
257
258
259
260
261
262
263template <
typename... Ts>
265 std::vector<std::string> columns, Ts&&... values) {
266 return UpdateMutationBuilder(std::move(table_name), std::move(columns))
267 .EmplaceRow(std::forward<Ts>(values)...)
272
273
274
275
276
277
278
279
280
281
282
283using InsertOrUpdateMutationBuilder =
284 spanner_internal::WriteMutationBuilder<spanner_internal::InsertOrUpdateOp>;
287
288
289
290
291
292
293
294
295
296
297
298template <
typename... Ts>
300 std::vector<std::string> columns,
302 return InsertOrUpdateMutationBuilder(std::move(table_name),
304 .EmplaceRow(std::forward<Ts>(values)...)
309
310
311
312
313
314
315
316
317
318
319
320using ReplaceMutationBuilder =
321 spanner_internal::WriteMutationBuilder<spanner_internal::ReplaceOp>;
324
325
326
327
328
329
330
331
332
333
334
335template <
typename... Ts>
337 std::vector<std::string> columns, Ts&&... values) {
338 return ReplaceMutationBuilder(std::move(table_name), std::move(columns))
339 .EmplaceRow(std::forward<Ts>(values)...)
344
345
346
347
348
349
350
351
352
353
354
355using DeleteMutationBuilder = spanner_internal::DeleteMutationBuilder;
358
359
360
361
362
363
364
365
366
367
368
370 return DeleteMutationBuilder(std::move(table_name), std::move(keys)).Build();
373GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
The KeySet class is a regular type that represents a collection of Keys.
Definition: keys.h:157
A wrapper for Cloud Spanner mutations.
Definition: mutations.h:52
Mutation(Mutation const &)=default
google::spanner::v1::Mutation as_proto() const &
Definition: mutations.h:73
Mutation(Mutation &&)=default
Mutation()=default
Creates an empty mutation.
friend bool operator!=(Mutation const &lhs, Mutation const &rhs)
Definition: mutations.h:67
friend void PrintTo(Mutation const &m, std::ostream *os)
Allows Google Test to print internal debugging information when test assertions fail.
Mutation & operator=(Mutation const &)=default
google::spanner::v1::Mutation as_proto() &&
Convert the mutation to the underlying proto.
Definition: mutations.h:72
friend bool operator==(Mutation const &lhs, Mutation const &rhs)
Mutation & operator=(Mutation &&)=default
The Value class represents a type-safe, nullable Spanner value.
Definition: value.h:170
Contains all the Cloud Spanner C++ client types and functions.
Definition: backoff_policy.h:23
Mutation MakeInsertMutation(std::string table_name, std::vector< std::string > columns, Ts &&... values)
Creates a simple insert mutation for the values in values.
Definition: mutations.h:229
Mutation MakeUpdateMutation(std::string table_name, std::vector< std::string > columns, Ts &&... values)
Creates a simple update mutation for the values in values.
Definition: mutations.h:264
Mutation MakeInsertOrUpdateMutation(std::string table_name, std::vector< std::string > columns, Ts &&... values)
Creates a simple "insert or update" mutation for the values in values.
Definition: mutations.h:299
Mutation MakeDeleteMutation(std::string table_name, KeySet keys)
Creates a simple "delete" mutation for the values in keys.
Definition: mutations.h:369
Mutation MakeReplaceMutation(std::string table_name, std::vector< std::string > columns, Ts &&... values)
Creates a simple "replace" mutation for the values in values.
Definition: mutations.h:336