Google Cloud Spanner C++ Client  1.32.1
A C++ Client Library for Google Cloud Spanner
database.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_DATABASE_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_DATABASE_H
17 
18 #include "google/cloud/spanner/instance.h"
19 #include "google/cloud/spanner/version.h"
20 #include "google/cloud/status_or.h"
21 #include <iosfwd>
22 #include <string>
23 
24 namespace google {
25 namespace cloud {
26 namespace spanner {
27 inline namespace SPANNER_CLIENT_NS {
28 
29 /**
30  * This class identifies a Cloud Spanner Database.
31  *
32  * A Cloud Spanner database is identified by its `project_id`, `instance_id`,
33  * and `database_id`.
34  *
35  * @note This class makes no effort to validate the components of the
36  * database name. It is the application's responsibility to provide valid
37  * project, instance, and database ids. Passing invalid values will not be
38  * checked until the database name is used in a RPC to spanner.
39  *
40  * For more info about the `database_id` format, see
41  * https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.CreateDatabaseRequest
42  */
43 class Database {
44  public:
45  /**
46  * Constructs a Database object identified by the given @p instance and
47  * @p database_id.
48  */
49  Database(Instance instance, std::string database_id);
50 
51  /**
52  * Constructs a Database object identified by the given IDs.
53  *
54  * This is equivalent to first constructing an `Instance` from the given
55  * @p project_id and @p instance_id arguments and then calling the
56  * `Database(Instance, std::string)` constructor.
57  */
58  Database(std::string project_id, std::string instance_id,
59  std::string database_id);
60 
61  /// @name Copy and move
62  //@{
63  Database(Database const&) = default;
64  Database& operator=(Database const&) = default;
65  Database(Database&&) = default;
66  Database& operator=(Database&&) = default;
67  //@}
68 
69  /// Returns the `Instance` containing this database.
70  Instance const& instance() const { return instance_; }
71 
72  /// Returns the Database ID.
73  std::string const& database_id() const { return database_id_; }
74 
75  /**
76  * Returns the fully qualified database name as a string of the form:
77  * "projects/<project-id>/instances/<instance-id>/databases/<database-id>"
78  */
79  std::string FullName() const;
80 
81  /// @name Equality operators
82  //@{
83  friend bool operator==(Database const& a, Database const& b);
84  friend bool operator!=(Database const& a, Database const& b);
85  //@}
86 
87  /// Output the `FullName()` format.
88  friend std::ostream& operator<<(std::ostream&, Database const&);
89 
90  private:
91  Instance instance_;
92  std::string database_id_;
93 };
94 
95 /**
96  * Constructs a `Database` from the given @p full_name.
97  * Returns a non-OK Status if `full_name` is improperly formed.
98  */
99 StatusOr<Database> MakeDatabase(std::string const& full_name);
100 
101 } // namespace SPANNER_CLIENT_NS
102 } // namespace spanner
103 } // namespace cloud
104 } // namespace google
105 
106 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_DATABASE_H