Google Cloud Spanner C++ Client  1.33.0
A C++ Client Library for Google Cloud Spanner
database.cc
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 #include "google/cloud/spanner/database.h"
16 #include <ostream>
17 #include <regex>
18 
19 namespace google {
20 namespace cloud {
21 namespace spanner {
23 
24 Database::Database(Instance instance, std::string database_id)
25  : instance_(std::move(instance)), database_id_(std::move(database_id)) {}
26 
27 Database::Database(std::string project_id, std::string instance_id,
28  std::string database_id)
29  : Database(Instance(std::move(project_id), std::move(instance_id)),
30  std::move(database_id)) {}
31 
32 std::string Database::FullName() const {
33  return instance_.FullName() + "/databases/" + database_id_;
34 }
35 
36 bool operator==(Database const& a, Database const& b) {
37  return a.instance_ == b.instance_ && a.database_id_ == b.database_id_;
38 }
39 
40 bool operator!=(Database const& a, Database const& b) { return !(a == b); }
41 
42 std::ostream& operator<<(std::ostream& os, Database const& db) {
43  return os << db.FullName();
44 }
45 
46 StatusOr<Database> MakeDatabase(std::string const& full_name) {
47  std::regex re("projects/([^/]+)/instances/([^/]+)/databases/([^/]+)");
48  std::smatch matches;
49  if (!std::regex_match(full_name, matches, re)) {
51  "Improperly formatted Database: " + full_name);
52  }
53  return Database(Instance(std::move(matches[1]), std::move(matches[2])),
54  std::move(matches[3]));
55 }
56 
58 } // namespace spanner
59 } // namespace cloud
60 } // namespace google