Google Cloud Spanner C++ Client 2.13.0
A C++ Client Library for Google Cloud Spanner
Loading...
Searching...
No Matches
database.h
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// https://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
24namespace google {
25namespace cloud {
26namespace spanner {
27GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
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 */
43class 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 */
99StatusOr<Database> MakeDatabase(std::string const& full_name);
100
101GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
102} // namespace spanner
103} // namespace cloud
104} // namespace google
105
106#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_DATABASE_H
This class identifies a Cloud Spanner Database.
Definition: database.h:43
std::string const & database_id() const
Returns the Database ID.
Definition: database.h:73
Database(std::string project_id, std::string instance_id, std::string database_id)
Constructs a Database object identified by the given IDs.
Database(Database const &)=default
Database & operator=(Database const &)=default
Instance const & instance() const
Returns the Instance containing this database.
Definition: database.h:70
Database(Instance instance, std::string database_id)
Constructs a Database object identified by the given instance and database_id.
friend bool operator!=(Database const &a, Database const &b)
std::string FullName() const
Returns the fully qualified database name as a string of the form: "projects/<project-id>/instances/<...
Database(Database &&)=default
friend bool operator==(Database const &a, Database const &b)
Database & operator=(Database &&)=default
This class identifies a Cloud Spanner Instance.
Definition: instance.h:42
Contains all the Cloud Spanner C++ client types and functions.
Definition: backoff_policy.h:23
StatusOr< Database > MakeDatabase(std::string const &full_name)
Constructs a Database from the given full_name.