Google Cloud Spanner C++ Client
A C++ Client Library for Google Cloud Spanner
client.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_CLIENT_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_CLIENT_H
17 
37 #include "google/cloud/optional.h"
38 #include "google/cloud/status.h"
39 #include "google/cloud/status_or.h"
40 #include <google/spanner/v1/spanner.pb.h>
41 #include <grpcpp/grpcpp.h>
42 #include <functional>
43 #include <memory>
44 #include <string>
45 #include <vector>
46 
47 namespace google {
48 namespace cloud {
49 namespace spanner {
50 inline namespace SPANNER_CLIENT_NS {
51 
113 class Client {
114  public:
122  explicit Client(std::shared_ptr<Connection> conn, ClientOptions opts = {})
123  : conn_(std::move(conn)), opts_(std::move(opts)) {}
124 
126  Client() = delete;
127 
129  // @name Copy and move support
130  Client(Client const&) = default;
131  Client& operator=(Client const&) = default;
132  Client(Client&&) = default;
133  Client& operator=(Client&&) = default;
135 
137  // @name Equality
138  friend bool operator==(Client const& a, Client const& b) {
139  return a.conn_ == b.conn_;
140  }
141  friend bool operator!=(Client const& a, Client const& b) { return !(a == b); }
143 
145 
169  RowStream Read(std::string table, KeySet keys,
170  std::vector<std::string> columns,
171  ReadOptions read_options = {});
172 
179  RowStream Read(Transaction::SingleUseOptions transaction_options,
180  std::string table, KeySet keys,
181  std::vector<std::string> columns,
182  ReadOptions read_options = {});
183 
189  RowStream Read(Transaction transaction, std::string table, KeySet keys,
190  std::vector<std::string> columns,
191  ReadOptions read_options = {});
193 
207  RowStream Read(ReadPartition const& partition);
208 
241  StatusOr<std::vector<ReadPartition>> PartitionRead(
242  Transaction transaction, std::string table, KeySet keys,
243  std::vector<std::string> columns, ReadOptions read_options = {},
244  PartitionOptions const& partition_options = PartitionOptions{});
245 
247 
280  RowStream ExecuteQuery(SqlStatement statement, QueryOptions const& opts = {});
281 
288  RowStream ExecuteQuery(Transaction::SingleUseOptions transaction_options,
289  SqlStatement statement, QueryOptions const& opts = {});
290 
296  RowStream ExecuteQuery(Transaction transaction, SqlStatement statement,
297  QueryOptions const& opts = {});
314  RowStream ExecuteQuery(QueryPartition const& partition,
315  QueryOptions const& opts = {});
317 
319 
347  ProfileQueryResult ProfileQuery(SqlStatement statement,
348  QueryOptions const& opts = {});
349 
356  ProfileQueryResult ProfileQuery(
357  Transaction::SingleUseOptions transaction_options, SqlStatement statement,
358  QueryOptions const& opts = {});
359 
365  ProfileQueryResult ProfileQuery(Transaction transaction,
366  SqlStatement statement,
367  QueryOptions const& opts = {});
369 
391  StatusOr<std::vector<QueryPartition>> PartitionQuery(
392  Transaction transaction, SqlStatement statement,
393  PartitionOptions const& partition_options = PartitionOptions{});
394 
412  StatusOr<DmlResult> ExecuteDml(Transaction transaction,
413  SqlStatement statement,
414  QueryOptions const& opts = {});
415 
436  StatusOr<ProfileDmlResult> ProfileDml(Transaction transaction,
437  SqlStatement statement,
438  QueryOptions const& opts = {});
439 
460  StatusOr<ExecutionPlan> AnalyzeSql(Transaction transaction,
461  SqlStatement statement,
462  QueryOptions const& opts = {});
463 
490  StatusOr<BatchDmlResult> ExecuteBatchDml(
491  Transaction transaction, std::vector<SqlStatement> statements);
492 
531  StatusOr<CommitResult> Commit(
532  std::function<StatusOr<Mutations>(Transaction)> const& mutator,
533  std::unique_ptr<TransactionRerunPolicy> rerun_policy,
534  std::unique_ptr<BackoffPolicy> backoff_policy);
535 
546  StatusOr<CommitResult> Commit(
547  std::function<StatusOr<Mutations>(Transaction)> const& mutator);
548 
558  StatusOr<CommitResult> Commit(Mutations mutations);
559 
584  StatusOr<CommitResult> Commit(Transaction transaction, Mutations mutations);
585 
600  Status Rollback(Transaction transaction);
601 
620  StatusOr<PartitionedDmlResult> ExecutePartitionedDml(SqlStatement statement);
621 
622  private:
623  QueryOptions OverlayQueryOptions(QueryOptions const&);
624 
625  std::shared_ptr<Connection> conn_;
626  ClientOptions opts_;
627 };
628 
643 std::shared_ptr<Connection> MakeConnection(
644  Database const& db,
645  ConnectionOptions const& connection_options = ConnectionOptions(),
646  SessionPoolOptions session_pool_options = SessionPoolOptions());
647 
657 std::shared_ptr<Connection> MakeConnection(
658  Database const& db, ConnectionOptions const& connection_options,
659  SessionPoolOptions session_pool_options,
660  std::unique_ptr<RetryPolicy> retry_policy,
661  std::unique_ptr<BackoffPolicy> backoff_policy);
662 
663 } // namespace SPANNER_CLIENT_NS
664 } // namespace spanner
665 } // namespace cloud
666 } // namespace google
667 
668 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_CLIENT_H
std::shared_ptr< Connection > MakeConnection(Database const &db, ConnectionOptions const &connection_options, SessionPoolOptions session_pool_options)
Returns a Connection object that can be used for interacting with Spanner.
Definition: client.cc:320
Performs database client operations on Spanner.
Definition: client.h:113
ClientOptions allows the caller to set a variety of options when constructing a Client instance.
std::vector< Mutation > Mutations
An ordered sequence of mutations to pass to Client::Commit() or return from the Client::Commit() muta...
Definition: mutations.h:95
friend bool operator==(Client const &a, Client const &b)
Definition: client.h:138
The KeySet class is a regular type that represents a collection of Keys.
Definition: keys.h:158
Contains all the Cloud Spanner C++ client types and functions.
google::cloud::ConnectionOptions< ConnectionOptionsTraits > ConnectionOptions
The options for Cloud Spanner connections.
#define SPANNER_CLIENT_NS
Definition: version.h:22
friend bool operator!=(Client const &a, Client const &b)
Definition: client.h:141
Represents the stream of Rows returned from spanner::Client::Read() or spanner::Client::ExecuteQuery(...
Definition: results.h:68
Client(std::shared_ptr< Connection > conn, ClientOptions opts={})
Constructs a Client object using the specified conn and opts.
Definition: client.h:122
Options passed to Client::Read or Client::PartitionRead.
Definition: read_options.h:28