forked from hyderabad-care-center/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.h
742 lines (702 loc) · 29.2 KB
/
client.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_CLIENT_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_CLIENT_H
#include "google/cloud/spanner/batch_dml_result.h"
#include "google/cloud/spanner/client_options.h"
#include "google/cloud/spanner/commit_options.h"
#include "google/cloud/spanner/commit_result.h"
#include "google/cloud/spanner/connection.h"
#include "google/cloud/spanner/connection_options.h"
#include "google/cloud/spanner/database.h"
#include "google/cloud/spanner/keys.h"
#include "google/cloud/spanner/mutations.h"
#include "google/cloud/spanner/partition_options.h"
#include "google/cloud/spanner/query_options.h"
#include "google/cloud/spanner/query_partition.h"
#include "google/cloud/spanner/read_options.h"
#include "google/cloud/spanner/read_partition.h"
#include "google/cloud/spanner/results.h"
#include "google/cloud/spanner/retry_policy.h"
#include "google/cloud/spanner/session_pool_options.h"
#include "google/cloud/spanner/sql_statement.h"
#include "google/cloud/spanner/transaction.h"
#include "google/cloud/spanner/version.h"
#include "google/cloud/backoff_policy.h"
#include "google/cloud/optional.h"
#include "google/cloud/options.h"
#include "google/cloud/status.h"
#include "google/cloud/status_or.h"
#include <google/spanner/v1/spanner.pb.h>
#include <grpcpp/grpcpp.h>
#include <functional>
#include <memory>
#include <string>
#include <vector>
namespace google {
namespace cloud {
namespace spanner {
inline namespace SPANNER_CLIENT_NS {
// clang-format off
/**
* Performs database client operations on Spanner.
*
* Applications use this class to perform operations on
* [Spanner Databases][spanner-doc-link].
*
* @par Performance
*
* `Client` objects are relatively cheap to create, copy, and move. However,
* each `Client` object must be created with a `std::shared_ptr<Connection>`,
* which itself is relatively expensive to create. Therefore, connection
* instances should be shared when possible. See the `MakeConnection()` method
* and the `Connection` interface for more details.
*
* @par Thread Safety
*
* Instances of this class created via copy-construction or copy-assignment
* share the underlying pool of connections. Access to these copies via multiple
* threads is guaranteed to work. Two threads operating on the same instance of
* this class is not guaranteed to work.
*
* @par Error Handling
*
* This class uses `StatusOr<T>` to report errors. When an operation fails to
* perform its work the returned `StatusOr<T>` contains the error details. If
* the `ok()` member function in the `StatusOr<T>` returns `true` then it
* contains the expected result. Please consult the
* [`StatusOr<T>` documentation](#google::cloud::v0::StatusOr) for more details.
*
* @code
* namespace spanner = ::google::cloud::spanner;
*
* auto db = spanner::Database("my_project", "my_instance", "my_db_id"));
* auto conn = spanner::MakeConnection(db);
* auto client = spanner::Client(conn);
*
* auto rows = client.Read(...);
* using RowType = std::tuple<std::int64_t, std::string>;
* for (auto const& row : spanner::StreamOf<RowType>(rows)) {
* // ...
* }
* @endcode
*
* @par Query Options
*
* Most operations that take an `SqlStatement` may also be modified with
* `QueryOptions`. These options can be set at various levels, with more
* specific levels taking precedence over broader ones. For example,
* `QueryOptions` that are passed directly to `Client::ExecuteQuery()` will
* take precedence over the `Client`-level defaults (if any), which will
* themselves take precedence over any environment variables. The following
* table shows the environment variables that may optionally be set and the
* `QueryOptions` setting that they affect.
*
* Environment Variable | QueryOptions setting
* -------------------------------------- | --------------------
* `SPANNER_OPTIMIZER_VERSION` | `QueryOptions::optimizer_version()`
* `SPANNER_OPTIMIZER_STATISTICS_PACKAGE` | `QueryOptions::optimizer_statistics_package()`
*
* @see https://cloud.google.com/spanner/docs/reference/rest/v1/QueryOptions
* @see http://cloud/spanner/docs/query-optimizer/manage-query-optimizer
*
* [spanner-doc-link]:
* https://cloud.google.com/spanner/docs/api-libraries-overview
*/
// clang-format on
class Client {
public:
/**
* Constructs a `Client` object using the specified @p conn and @p opts.
*
* See `MakeConnection()` for how to create a connection to Spanner. To help
* with unit testing, callers may create fake/mock `Connection` objects that
* are injected into the `Client`.
*/
explicit Client(std::shared_ptr<Connection> conn, ClientOptions opts = {})
: conn_(std::move(conn)), opts_(std::move(opts)) {}
/// No default construction. Use `Client(std::shared_ptr<Connection>)`
Client() = delete;
//@{
// @name Copy and move support
Client(Client const&) = default;
Client& operator=(Client const&) = default;
Client(Client&&) = default;
Client& operator=(Client&&) = default;
//@}
//@{
// @name Equality
friend bool operator==(Client const& a, Client const& b) {
return a.conn_ == b.conn_;
}
friend bool operator!=(Client const& a, Client const& b) { return !(a == b); }
//@}
//@{
/**
* Reads rows from the database using key lookups and scans, as a simple
* key/value style alternative to `ExecuteQuery()`.
*
* Callers can optionally supply a `Transaction` or
* `Transaction::SingleUseOptions` used to create a single-use transaction -
* or neither, in which case a single-use transaction with default options
* is used.
*
* @param table The name of the table in the database to be read.
* @param keys Identifies the rows to be yielded. If `read_options.index_name`
* is set, names keys in that index; otherwise names keys in the primary
* index of `table`. It is not an error for `keys` to name rows that do
* not exist in the database; `Read` yields nothing for nonexistent rows.
* @param columns The columns of `table` to be returned for each row matching
* this request.
* @param read_options `ReadOptions` used for this request.
*
* @par Example
* @snippet samples.cc read-data
*
* @note No individual row in the `ReadResult` can exceed 100 MiB, and no
* column value can exceed 10 MiB.
*/
RowStream Read(std::string table, KeySet keys,
std::vector<std::string> columns,
ReadOptions read_options = {});
/**
* @copydoc Read
*
* @param transaction_options Execute this read in a single-use transaction
* with these options.
*/
RowStream Read(Transaction::SingleUseOptions transaction_options,
std::string table, KeySet keys,
std::vector<std::string> columns,
ReadOptions read_options = {});
/**
* @copydoc Read
*
* @param transaction Execute this read as part of an existing transaction.
*/
RowStream Read(Transaction transaction, std::string table, KeySet keys,
std::vector<std::string> columns,
ReadOptions read_options = {});
//@}
/**
* Reads rows from a subset of rows in a database. Requires a prior call
* to `PartitionRead` to obtain the partition information; see the
* documentation of that method for full details.
*
* @param partition A `ReadPartition`, obtained by calling `PartitionRead`.
*
* @note No individual row in the `ReadResult` can exceed 100 MiB, and no
* column value can exceed 10 MiB.
*
* @par Example
* @snippet samples.cc read-read-partition
*/
RowStream Read(ReadPartition const& partition);
/**
* Creates a set of partitions that can be used to execute a read
* operation in parallel. Each of the returned partitions can be passed
* to `Read` to specify a subset of the read result to read.
*
* There are no ordering guarantees on rows returned among the returned
* partition, or even within each individual `Read` call issued with a given
* partition.
*
* Partitions become invalid when the session used to create them is deleted,
* is idle for too long, begins a new transaction, or becomes too old. When
* any of these happen, it is not possible to resume the read, and the whole
* operation must be restarted from the beginning.
*
* @param transaction The transaction to execute the operation in.
* **Must** be a read-only snapshot transaction.
* @param table The name of the table in the database to be read.
* @param keys Identifies the rows to be yielded. If `read_options.index_name`
* is set, names keys in that index; otherwise names keys in the primary
* index of `table`. It is not an error for `keys` to name rows that do
* not exist in the database; `Read` yields nothing for nonexistent rows.
* @param columns The columns of `table` to be returned for each row matching
* this request.
* @param read_options `ReadOptions` used for this request.
* @param partition_options `PartitionOptions` used for this request.
*
* @return A `StatusOr` containing a vector of `ReadPartition` or error
* status on failure.
*
* @par Example
* @snippet samples.cc partition-read
*/
StatusOr<std::vector<ReadPartition>> PartitionRead(
Transaction transaction, std::string table, KeySet keys,
std::vector<std::string> columns, ReadOptions read_options = {},
PartitionOptions const& partition_options = PartitionOptions{});
//@{
/**
* Executes a SQL query.
*
* Operations inside read-write transactions might return `ABORTED`. If this
* occurs, the application should restart the transaction from the beginning.
*
* Callers can optionally supply a `Transaction` or
* `Transaction::SingleUseOptions` used to create a single-use transaction -
* or neither, in which case a single-use transaction with default options
* is used.
*
* `SELECT * ...` queries are supported, but there's no guarantee about the
* order, nor number, of returned columns. Therefore, the caller must look up
* the wanted values in each row by column name. When the desired column
* names are known in advance, it is better to list them explicitly in the
* query's SELECT statement, so that unnecessary values are not
* returned/ignored, and the column order is known. This enables more
* efficient and simpler code.
*
* @par Example with explicitly selected columns.
* @snippet samples.cc spanner-query-data
*
* @par Example using SELECT *
* @snippet samples.cc spanner-query-data-select-star
*
* @param statement The SQL statement to execute.
* @param opts The `QueryOptions` to use for this call. If given, these will
* take precedence over the options set at the client and environment
* levels.
*
* @note No individual row in the `RowStream` can exceed 100 MiB, and no
* column value can exceed 10 MiB.
*/
RowStream ExecuteQuery(SqlStatement statement, QueryOptions const& opts = {});
/**
* @copydoc ExecuteQuery(SqlStatement, QueryOptions const&)
*
* @param transaction_options Execute this query in a single-use transaction
* with these options.
*/
RowStream ExecuteQuery(Transaction::SingleUseOptions transaction_options,
SqlStatement statement, QueryOptions const& opts = {});
/**
* @copydoc ExecuteQuery(SqlStatement, QueryOptions const&)
*
* @param transaction Execute this query as part of an existing transaction.
*/
RowStream ExecuteQuery(Transaction transaction, SqlStatement statement,
QueryOptions const& opts = {});
/**
* Executes a SQL query on a subset of rows in a database. Requires a prior
* call to `PartitionQuery` to obtain the partition information; see the
* documentation of that method for full details.
*
* @param partition A `QueryPartition`, obtained by calling `PartitionQuery`.
* @param opts The `QueryOptions` to use for this call. If given, these will
* take precedence over the options set at the client and environment
* levels.
*
* @note No individual row in the `RowStream` can exceed 100 MiB, and no
* column value can exceed 10 MiB.
*
* @par Example
* @snippet samples.cc execute-sql-query-partition
*/
RowStream ExecuteQuery(QueryPartition const& partition,
QueryOptions const& opts = {});
//@}
//@{
/**
* Profiles a SQL query.
*
* Profiling executes the query, provides the resulting rows, `ExecutionPlan`,
* and execution statistics.
*
* Operations inside read-write transactions might return `kAborted`. If this
* occurs, the application should restart the transaction from the beginning.
*
* Callers can optionally supply a `Transaction` or
* `Transaction::SingleUseOptions` used to create a single-use transaction -
* or neither, in which case a single-use transaction with default options
* is used.
*
* @note Callers must consume all rows from the result before execution
* statistics and `ExecutionPlan` are available.
*
* @param statement The SQL statement to execute.
* @param opts The `QueryOptions` to use for this call. If given, these will
* take precedence over the options set at the client and environment
* levels.
*
* @note No individual row in the `ProfileQueryResult` can exceed 100 MiB, and
* no column value can exceed 10 MiB.
*
* @par Example
* @snippet samples.cc profile-query
*/
ProfileQueryResult ProfileQuery(SqlStatement statement,
QueryOptions const& opts = {});
/**
* @copydoc ProfileQuery(SqlStatement, QueryOptions const&)
*
* @param transaction_options Execute this query in a single-use transaction
* with these options.
*/
ProfileQueryResult ProfileQuery(
Transaction::SingleUseOptions transaction_options, SqlStatement statement,
QueryOptions const& opts = {});
/**
* @copydoc ProfileQuery(SqlStatement, QueryOptions const&)
*
* @param transaction Execute this query as part of an existing transaction.
*/
ProfileQueryResult ProfileQuery(Transaction transaction,
SqlStatement statement,
QueryOptions const& opts = {});
//@}
/**
* Creates a set of partitions that can be used to execute a query
* operation in parallel. Each of the returned partitions can be passed
* to `ExecuteQuery` to specify a subset of the query result to read.
*
* Partitions become invalid when the session used to create them is deleted,
* is idle for too long, begins a new transaction, or becomes too old. When
* any of these happen, it is not possible to resume the query, and the whole
* operation must be restarted from the beginning.
*
* @param transaction The transaction to execute the operation in.
* **Must** be a read-only snapshot transaction.
* @param statement The SQL statement to execute.
* @param partition_options `PartitionOptions` used for this request.
*
* @return A `StatusOr` containing a vector of `QueryPartition`s or error
* status on failure.
*
* @par Example
* @snippet samples.cc partition-query
*/
StatusOr<std::vector<QueryPartition>> PartitionQuery(
Transaction transaction, SqlStatement statement,
PartitionOptions const& partition_options = PartitionOptions{});
/**
* Executes a SQL DML statement.
*
* Operations inside read-write transactions might return `ABORTED`. If this
* occurs, the application should restart the transaction from the beginning.
*
* @note Single-use transactions are not supported with DML statements.
*
* @param transaction Execute this query as part of an existing transaction.
* @param statement The SQL statement to execute.
* @param opts The `QueryOptions` to use for this call. If given, these will
* take precedence over the options set at the client and environment
* levels.
*
* @par Example
* @snippet samples.cc execute-dml
*/
StatusOr<DmlResult> ExecuteDml(Transaction transaction,
SqlStatement statement,
QueryOptions const& opts = {});
/**
* Profiles a SQL DML statement.
*
* Profiling executes the DML statement, provides the modified row count,
* `ExecutionPlan`, and execution statistics.
*
* Operations inside read-write transactions might return `ABORTED`. If this
* occurs, the application should restart the transaction from the beginning.
*
* @note Single-use transactions are not supported with DML statements.
*
* @param transaction Execute this query as part of an existing transaction.
* @param statement The SQL statement to execute.
* @param opts The `QueryOptions` to use for this call. If given, these will
* take precedence over the options set at the client and environment
* levels.
*
* @par Example:
* @snippet samples.cc profile-dml
*/
StatusOr<ProfileDmlResult> ProfileDml(Transaction transaction,
SqlStatement statement,
QueryOptions const& opts = {});
/**
* Analyzes the execution plan of a SQL statement.
*
* Analyzing provides the `ExecutionPlan`, but does not execute the SQL
* statement.
*
* Operations inside read-write transactions might return `ABORTED`. If this
* occurs, the application should restart the transaction from the beginning.
*
* @note Single-use transactions are not supported with DML statements.
*
* @param transaction Execute this query as part of an existing transaction.
* @param statement The SQL statement to execute.
* @param opts The `QueryOptions` to use for this call. If given, these will
* take precedence over the options set at the client and environment
* levels.
*
* @par Example:
* @snippet samples.cc analyze-query
*/
StatusOr<ExecutionPlan> AnalyzeSql(Transaction transaction,
SqlStatement statement,
QueryOptions const& opts = {});
/**
* Executes a batch of SQL DML statements. This method allows many statements
* to be run with lower latency than submitting them sequentially with
* `ExecuteDml`.
*
* Statements are executed in order, sequentially. Execution will stop at the
* first failed statement; the remaining statements will not run.
*
* As with all read-write transactions, the results will not be visible
* outside of the transaction until it is committed. For that reason, it is
* advisable to run this method from a `Commit` mutator.
*
* @warning A returned status of OK from this function does not imply that
* all the statements were executed successfully. For that, you need to
* inspect the `BatchDmlResult::status` field.
*
* @param transaction The read-write transaction to execute the operation in.
* @param statements The list of statements to execute in this batch.
* Statements are executed serially, such that the effects of statement i
* are visible to statement i+1. Each statement must be a DML statement.
* Execution will stop at the first failed statement; the remaining
* statements will not run. Must not be empty.
* @param opts The options to use for this call. Expected options are any
* of the types in the following option lists.
* - `google::cloud::RequestOptionList`
*
* @par Example
* @snippet samples.cc execute-batch-dml
*/
StatusOr<BatchDmlResult> ExecuteBatchDml(Transaction transaction,
std::vector<SqlStatement> statements,
Options opts = {});
/**
* Commits a read-write transaction.
*
* Calls the @p mutator in the context of a new read-write transaction.
* The @p mutator can execute read/write operations using the transaction,
* and returns any additional `Mutations` to commit.
*
* If the @p mutator succeeds and the transaction commits, then `Commit()`
* returns the `CommitResult`.
*
* If the @p mutator returns a non-rerunnable status (according to the
* @p rerun_policy), the transaction is rolled back and that status is
* returned. Similarly, if the transaction fails to commit with a non-
* rerunnable status, that status is returned.
*
* Otherwise the whole process repeats (subject to @p rerun_policy and
* @p backoff_policy), by building a new transaction and re-running the
* @p mutator. The lock priority of the operation increases after each
* rerun, meaning that the next attempt has a slightly better chance of
* success.
*
* Note that the @p mutator should only return a rerunnable status when
* the transaction is no longer usable (e.g., it was aborted). Otherwise
* the transaction will be leaked.
*
* @param mutator the function called to create mutations
* @param rerun_policy controls for how long (or how many times) the mutator
* will be rerun after the transaction aborts.
* @param backoff_policy controls how long `Commit` waits between reruns.
* @param options to apply to the commit.
*
* @throw Rethrows any exception thrown by @p `mutator` (after rolling back
* the transaction). However, a `RuntimeStatusError` exception is
* instead consumed and converted into a `mutator` return value of the
* enclosed `Status`.
*
* @par Example
* @snippet samples.cc commit-with-policies
*/
StatusOr<CommitResult> Commit(
std::function<StatusOr<Mutations>(Transaction)> const& mutator,
std::unique_ptr<TransactionRerunPolicy> rerun_policy,
std::unique_ptr<BackoffPolicy> backoff_policy,
CommitOptions const& options = {});
/**
* Commits a read-write transaction.
*
* Same as above, but uses the default rerun and backoff policies.
*
* @param mutator the function called to create mutations
* @param options to apply to the commit.
*
* @par Example
* @snippet samples.cc commit-with-mutator
*/
StatusOr<CommitResult> Commit(
std::function<StatusOr<Mutations>(Transaction)> const& mutator,
CommitOptions const& options = {});
/**
* Commits the @p mutations, using the @p options, atomically in order.
*
* This function uses the re-run loop described above with the default
* policies.
*
* @par Example
* @snippet samples.cc commit-with-mutations
*/
StatusOr<CommitResult> Commit(Mutations mutations,
CommitOptions const& options = {});
/**
* Commits a read-write transaction.
*
* The commit might return a `kAborted` error. This can occur at any time.
* Commonly the cause is conflicts with concurrent transactions, however
* it can also happen for a variety of other reasons. If `Commit` returns
* `kAborted`, the caller may try to reapply the mutations within a new
* read-write transaction (which should share lock priority with the aborted
* transaction so that the new attempt has a slightly better chance of
* success).
*
* @note Prefer the previous `Commit` overloads if you want to simply reapply
* mutations after a `kAborted` error.
*
* @warning It is an error to call `Commit` with a read-only transaction.
*
* @param transaction The transaction to commit.
* @param mutations The mutations to be executed when this transaction
* commits. All mutations are applied atomically, in the order they appear
* in this list.
* @param options to apply to the commit.
*
* @return A `StatusOr` containing the result of the commit or error status
* on failure.
*/
StatusOr<CommitResult> Commit(Transaction transaction, Mutations mutations,
CommitOptions const& options = {});
/**
* Rolls back a read-write transaction, releasing any locks it holds.
*
* At any time before `Commit`, the client can call `Rollback` to abort the
* transaction. It is a good idea to call this for any read-write transaction
* that includes one or more `Read`, `ExecuteQuery`, or `ExecuteDml` requests
* and ultimately decides not to commit.
*
* @warning It is an error to call `Rollback` with a read-only transaction.
*
* @param transaction The transaction to roll back.
*
* @return The error status of the rollback.
*/
Status Rollback(Transaction transaction);
/**
* Executes a Partitioned DML SQL query.
*
* @param statement the SQL statement to execute. Please see the
* [spanner documentation][dml-partitioned] for the restrictions on the
* SQL statements supported by this function.
* @param opts The `QueryOptions` to use for this call. If given, these will
* take precedence over the options set at the client and environment
* levels.
*
* @par Example
* @snippet samples.cc execute-sql-partitioned
*
* @see [Partitioned DML Transactions][txn-partitioned] for an overview of
* Partitioned DML transactions.
* @see [Partitioned DML][dml-partitioned] for a description of which SQL
* statements are supported in Partitioned DML transactions.
* [txn-partitioned]:
* https://cloud.google.com/spanner/docs/transactions#partitioned_dml_transactions
* [dml-partitioned]: https://cloud.google.com/spanner/docs/dml-partitioned
*/
StatusOr<PartitionedDmlResult> ExecutePartitionedDml(
SqlStatement statement, QueryOptions const& opts = {});
private:
QueryOptions OverlayQueryOptions(QueryOptions const&);
std::shared_ptr<Connection> conn_;
ClientOptions opts_;
};
/**
* Returns a Connection object that can be used for interacting with Spanner.
*
* The returned connection object should not be used directly; instead it
* should be given to a `Client` instance, and methods should be invoked on
* `Client`.
*
* The optional @p opts argument may be used to configure aspects of the
* returned `Connection`. Expected options are any of the types in the
* following option lists.
*
* - `google::cloud::CommonOptionList`
* - `google::cloud::GrpcOptionList`
* - `google::cloud::spanner::SpannerPolicyOptionList`
* - `google::cloud::spanner::SessionPoolOptionList`
*
* @note Unrecognized options will be ignored. To debug issues with options set
* `GOOGLE_CLOUD_CPP_ENABLE_CLOG=yes` in the environment and unexpected
* options will be logged.
*
* @see `Connection`
*
* @param db See `Database`.
* @param opts (optional) configure the `Connection` created by
* this function.
*/
std::shared_ptr<spanner::Connection> MakeConnection(spanner::Database const& db,
Options opts = {});
/**
* Returns a Connection object that can be used for interacting with Spanner.
*
* The returned connection object should not be used directly, rather it should
* be given to a `Client` instance, and methods should be invoked on `Client`.
*
* @note Prefer using the `MakeConnection()` overload that accepts
* `google::cloud::Options`.
*
* @see `Connection`
*
* @param db See `Database`.
* @param connection_options configure the `Connection` created by this
* function.
* @param session_pool_options (optional) configure the `SessionPool` created
* by the `Connection`.
*/
std::shared_ptr<Connection> MakeConnection(
Database const& db, ConnectionOptions const& connection_options,
SessionPoolOptions session_pool_options = SessionPoolOptions());
/**
* @copydoc MakeConnection(Database const&, ConnectionOptions const&, SessionPoolOptions)
*
* @note Prefer using the `MakeConnection()` overload that accepts
* `google::cloud::Options`.
*
* @param retry_policy override the default `RetryPolicy`, controls how long
* the returned `Connection` object retries requests on transient
* failures.
* @param backoff_policy override the default `BackoffPolicy`, controls how
* long the `Connection` object waits before retrying a failed request.
*
* @par Example
* @snippet samples.cc custom-retry-policy
*/
std::shared_ptr<Connection> MakeConnection(
Database const& db, ConnectionOptions const& connection_options,
SessionPoolOptions session_pool_options,
std::unique_ptr<RetryPolicy> retry_policy,
std::unique_ptr<BackoffPolicy> backoff_policy);
} // namespace SPANNER_CLIENT_NS
} // namespace spanner
namespace spanner_internal {
inline namespace SPANNER_CLIENT_NS {
spanner::QueryOptions OverlayQueryOptions(
spanner::QueryOptions const& preferred,
spanner::QueryOptions const& fallback,
absl::optional<std::string> const& optimizer_version_env,
absl::optional<std::string> const& optimizer_statistics_package_env);
} // namespace SPANNER_CLIENT_NS
} // namespace spanner_internal
} // namespace cloud
} // namespace google
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_CLIENT_H