Data_deduplication_service
Service that will use hashes to deduplicate files
myConnString.h
Go to the documentation of this file.
1 #ifndef DATA_DEDUPLICATION_SERVICE_MYCONNSTRING_H
2 #define DATA_DEDUPLICATION_SERVICE_MYCONNSTRING_H
3 
4 #include <string_view>
5 #include <string>
6 #include <memory>
7 
8 #include "myConcepts.h"
9 
10 namespace db_services {
11 
15  struct myConnString {
16  myConnString() : port(5432) {}
17 
18  myConnString(std::string_view user,
19  std::string_view password,
20  std::string_view host,
21  std::string_view dbname, unsigned port)
22  : user(user), password(password),
23  host(host),
24  dbname(dbname),
25  port(port) {
26  updateFormat();
27  }
28 
29  explicit operator std::string() {
30  return formattedString;
31  }
32 
33  operator std::string_view() {
34  return formattedString;
35  }
36 
37  [[nodiscard]] const char *c_str() const {
38  return formattedString.c_str();
39  }
40 
41  void setUser(std::string_view newUser) {
42  user = std::forward<std::string_view>(newUser);
43  updateFormat();
44  }
45 
46  void setPassword(std::string_view newPassword) {
47  password = newPassword;
48  updateFormat();
49  }
50 
51  void setHost(std::string_view newHost) {
52  host = newHost;
53  updateFormat();
54  }
55 
56  void setPort(unsigned newPort) {
57  port = newPort;
58  updateFormat();
59  }
60 
61  void setDbname(std::string_view newDbname) {
62  dbname = newDbname;
63  updateFormat();
64  }
65 
66  [[nodiscard]] const std::string &getUser() const {
67  return user;
68  }
69 
70  [[nodiscard]] const std::string &getPassword() const {
71  return password;
72  }
73 
74  [[nodiscard]] const std::string &getHost() const {
75  return host;
76  }
77 
78  [[nodiscard]] const std::string &getDbname() const {
79  return dbname;
80  }
81 
82  [[nodiscard]] unsigned int getPort() const {
83  return port;
84  }
85 
86  private:
87  void updateFormat() {
88  formattedString = myConcepts::vformat("postgresql://%s:%s@%s:%d/%s",
89  user.c_str(), password.c_str(), host.c_str(), port, dbname.c_str());
90  }
91 
92  std::string user, password, host, dbname;
93  unsigned port;
94  std::string formattedString;
95  };
96 }
97 
98 #endif //DATA_DEDUPLICATION_SERVICE_MYCONNSTRING_H
db_services namespace
Definition: dbCommon.h:17
std::string vformat(const char *zcFormat,...)
Definition: myConcepts.cpp:11
Structure to store and format connection string.
Definition: myConnString.h:15
myConnString(std::string_view user, std::string_view password, std::string_view host, std::string_view dbname, unsigned port)
Definition: myConnString.h:18
const std::string & getPassword() const
Definition: myConnString.h:70
void setUser(std::string_view newUser)
Definition: myConnString.h:41
void setDbname(std::string_view newDbname)
Definition: myConnString.h:61
const std::string & getUser() const
Definition: myConnString.h:66
const char * c_str() const
Definition: myConnString.h:37
const std::string & getDbname() const
Definition: myConnString.h:78
const std::string & getHost() const
Definition: myConnString.h:74
myConnString()
Definition: myConnString.h:16
void setPort(unsigned newPort)
Definition: myConnString.h:56
void setHost(std::string_view newHost)
Definition: myConnString.h:51
unsigned int getPort() const
Definition: myConnString.h:82
void setPassword(std::string_view newPassword)
Definition: myConnString.h:46