Skip to content

Commit 28fc4b9

Browse files
authored
doc(spanner): update sample implementation to match description (#15871)
1 parent 49e2ddd commit 28fc4b9

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

google/cloud/spanner/samples/samples.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3426,6 +3426,8 @@ void ReadDataWithStoringIndex(google::cloud::spanner::Client client) {
34263426
//! [START spanner_read_write_transaction]
34273427
void ReadWriteTransaction(google::cloud::spanner::Client client) {
34283428
namespace spanner = ::google::cloud::spanner;
3429+
using ::google::cloud::Status;
3430+
using ::google::cloud::StatusCode;
34293431
using ::google::cloud::StatusOr;
34303432

34313433
// A helper to read a single album MarketingBudget.
@@ -3442,15 +3444,21 @@ void ReadWriteTransaction(google::cloud::spanner::Client client) {
34423444
return std::get<0>(*std::move(row));
34433445
};
34443446

3447+
auto constexpr kInsufficientFundsMessage =
3448+
"The second album does not have enough funds to transfer";
34453449
auto commit = client.Commit(
3446-
[&client, &get_current_budget](
3447-
spanner::Transaction const& txn) -> StatusOr<spanner::Mutations> {
3450+
[&](spanner::Transaction const& txn) -> StatusOr<spanner::Mutations> {
34483451
auto b1 = get_current_budget(client, txn, 1, 1);
34493452
if (!b1) return std::move(b1).status();
34503453
auto b2 = get_current_budget(client, txn, 2, 2);
34513454
if (!b2) return std::move(b2).status();
34523455
std::int64_t transfer_amount = 200000;
34533456

3457+
if (*b2 < transfer_amount) {
3458+
return Status(StatusCode::kFailedPrecondition,
3459+
kInsufficientFundsMessage);
3460+
}
3461+
34543462
return spanner::Mutations{
34553463
spanner::UpdateMutationBuilder(
34563464
"Albums", {"SingerId", "AlbumId", "MarketingBudget"})

0 commit comments

Comments
 (0)