Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
database::integrated::connection_string_builder Class Reference

Fluent builder for constructing database connection strings. More...

#include <connection_string_builder.h>

Collaboration diagram for database::integrated::connection_string_builder:
Collaboration graph

Public Member Functions

 connection_string_builder ()=default
 
 ~connection_string_builder ()=default
 
 connection_string_builder (const connection_string_builder &)=default
 
connection_string_builderoperator= (const connection_string_builder &)=default
 
 connection_string_builder (connection_string_builder &&) noexcept=default
 
connection_string_builderoperator= (connection_string_builder &&) noexcept=default
 
connection_string_builderhost (std::string_view h)
 Set the database host.
 
connection_string_builderport (uint16_t p)
 Set the database port.
 
connection_string_builderdatabase (std::string_view db)
 Set the database name.
 
connection_string_builderuser (std::string_view u)
 Set the username for authentication.
 
connection_string_builderpassword (std::string_view p)
 Set the password for authentication.
 
connection_string_builderssl_mode (enum ssl_mode mode)
 Set the SSL connection mode.
 
connection_string_builderconnect_timeout (uint32_t seconds)
 Set the connection timeout.
 
connection_string_builderapplication_name (std::string_view name)
 Set the application name (for PostgreSQL)
 
connection_string_builderin_memory ()
 Configure SQLite to use in-memory database.
 
connection_string_builderoption (std::string_view key, std::string_view value)
 Add a custom option.
 
kcenon::common::Result< std::string > build (backend_type type) const
 Build the connection string for the specified backend.
 
connection_string_builderreset ()
 Reset the builder to initial state.
 

Private Member Functions

kcenon::common::Result< std::string > build_postgres () const
 
kcenon::common::Result< std::string > build_sqlite () const
 
kcenon::common::Result< std::string > build_mongodb () const
 
kcenon::common::Result< std::string > build_redis () const
 

Static Private Member Functions

static std::string ssl_mode_to_postgres_string (enum ssl_mode mode)
 

Private Attributes

std::optional< std::string > host_
 
std::optional< uint16_t > port_
 
std::optional< std::string > database_
 
std::optional< std::string > user_
 
std::optional< std::string > password_
 
std::optional< enum ssl_modessl_mode_
 
std::optional< uint32_t > connect_timeout_
 
std::optional< std::string > application_name_
 
bool in_memory_ = false
 
std::vector< std::pair< std::string, std::string > > custom_options_
 

Detailed Description

Fluent builder for constructing database connection strings.

This class provides a type-safe way to build connection strings for different database backends. It validates required fields per backend and generates properly formatted connection strings.

Thread Safety: This class is not thread-safe. Each thread should use its own builder instance.

Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 76 of file connection_string_builder.h.

Constructor & Destructor Documentation

◆ connection_string_builder() [1/3]

database::integrated::connection_string_builder::connection_string_builder ( )
default

◆ ~connection_string_builder()

database::integrated::connection_string_builder::~connection_string_builder ( )
default

◆ connection_string_builder() [2/3]

database::integrated::connection_string_builder::connection_string_builder ( const connection_string_builder & )
default

◆ connection_string_builder() [3/3]

database::integrated::connection_string_builder::connection_string_builder ( connection_string_builder && )
defaultnoexcept

Member Function Documentation

◆ application_name()

connection_string_builder & database::integrated::connection_string_builder::application_name ( std::string_view name)

Set the application name (for PostgreSQL)

Parameters
nameApplication name
Returns
Reference to this builder for chaining
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 46 of file connection_string_builder.cpp.

46 {
47 application_name_ = std::string(name);
48 return *this;
49}

References application_name_.

Referenced by test_builder_chaining(), and test_postgres_app_name().

Here is the caller graph for this function:

◆ build()

kcenon::common::Result< std::string > database::integrated::connection_string_builder::build ( backend_type type) const
nodiscard

Build the connection string for the specified backend.

Parameters
typeDatabase backend type
Returns
Result containing the connection string or validation error

Validates that required fields are set for the backend:

  • PostgreSQL: host is recommended (defaults to localhost)
  • SQLite: database path is required (or in_memory must be set)
  • MongoDB: host is required
  • Redis: host is required
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 61 of file connection_string_builder.cpp.

61 {
62 switch (type) {
64 return build_postgres();
66 return build_sqlite();
68 return build_mongodb();
70 return build_redis();
71 default:
72 return kcenon::common::Result<std::string>::err(
73 -1, "Unknown backend type", "connection_string_builder");
74 }
75}
kcenon::common::Result< std::string > build_sqlite() const
kcenon::common::Result< std::string > build_postgres() const
kcenon::common::Result< std::string > build_redis() const
kcenon::common::Result< std::string > build_mongodb() const
@ mongodb
MongoDB NoSQL database.
@ sqlite
SQLite embedded database.
@ redis
Redis key-value store.
@ postgres
PostgreSQL database.

References build_mongodb(), build_postgres(), build_redis(), build_sqlite(), database::integrated::mongodb, database::integrated::postgres, database::integrated::redis, and database::integrated::sqlite.

Referenced by test_builder_chaining(), test_builder_copy(), test_builder_reset(), test_builder_reuse(), test_mongodb_basic(), test_mongodb_default_host(), test_mongodb_ssl(), test_mongodb_with_auth(), test_postgres_app_name(), test_postgres_basic(), test_postgres_custom_option(), test_postgres_empty(), test_postgres_ssl(), test_postgres_timeout(), test_postgres_verify_ca(), test_redis_basic(), test_redis_with_database(), test_redis_with_password(), test_sqlite_file(), test_sqlite_memory(), test_sqlite_memory_overrides_file(), and test_sqlite_no_database().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ build_mongodb()

kcenon::common::Result< std::string > database::integrated::connection_string_builder::build_mongodb ( ) const
nodiscardprivate
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 158 of file connection_string_builder.cpp.

158 {
159 // MongoDB uses URI format: mongodb://[user:password@]host[:port]/[database]
160 std::ostringstream oss;
161 oss << "mongodb://";
162
163 if (user_.has_value() && password_.has_value()) {
164 oss << *user_ << ':' << *password_ << '@';
165 }
166
167 if (host_.has_value()) {
168 oss << *host_;
169 } else {
170 oss << "localhost";
171 }
172
173 if (port_.has_value()) {
174 oss << ':' << *port_;
175 }
176
177 if (database_.has_value()) {
178 oss << '/' << *database_;
179 }
180
181 // Add options as query parameters
182 if (!custom_options_.empty() || ssl_mode_.has_value() || connect_timeout_.has_value()) {
183 oss << '?';
184 bool first = true;
185
186 auto append_param = [&oss, &first](const std::string& key, const std::string& value) {
187 if (!first) {
188 oss << '&';
189 }
190 oss << key << '=' << value;
191 first = false;
192 };
193
194 if (ssl_mode_.has_value()) {
195 bool use_ssl = *ssl_mode_ != ssl_mode::disable;
196 append_param("ssl", use_ssl ? "true" : "false");
197 }
198
199 if (connect_timeout_.has_value()) {
200 append_param("connectTimeoutMS", std::to_string(*connect_timeout_ * 1000));
201 }
202
203 for (const auto& [key, value] : custom_options_) {
204 append_param(key, value);
205 }
206 }
207
208 return kcenon::common::Result<std::string>::ok(oss.str());
209}
std::vector< std::pair< std::string, std::string > > custom_options_

References connect_timeout_, custom_options_, database_, database::integrated::disable, host_, password_, port_, ssl_mode_, and user_.

Referenced by build().

Here is the caller graph for this function:

◆ build_postgres()

kcenon::common::Result< std::string > database::integrated::connection_string_builder::build_postgres ( ) const
nodiscardprivate
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 91 of file connection_string_builder.cpp.

91 {
92 std::ostringstream oss;
93 bool first = true;
94
95 auto append = [&oss, &first](const std::string& key, const std::string& value) {
96 if (!first) {
97 oss << ' ';
98 }
99 oss << key << '=' << value;
100 first = false;
101 };
102
103 // PostgreSQL uses space-separated key=value pairs
104 if (host_.has_value()) {
105 append("host", *host_);
106 }
107
108 if (port_.has_value()) {
109 append("port", std::to_string(*port_));
110 }
111
112 if (database_.has_value()) {
113 append("dbname", *database_);
114 }
115
116 if (user_.has_value()) {
117 append("user", *user_);
118 }
119
120 if (password_.has_value()) {
121 append("password", *password_);
122 }
123
124 if (ssl_mode_.has_value()) {
125 append("sslmode", ssl_mode_to_postgres_string(*ssl_mode_));
126 }
127
128 if (connect_timeout_.has_value()) {
129 append("connect_timeout", std::to_string(*connect_timeout_));
130 }
131
132 if (application_name_.has_value()) {
133 append("application_name", *application_name_);
134 }
135
136 for (const auto& [key, value] : custom_options_) {
137 append(key, value);
138 }
139
140 return kcenon::common::Result<std::string>::ok(oss.str());
141}
static std::string ssl_mode_to_postgres_string(enum ssl_mode mode)

References application_name_, connect_timeout_, custom_options_, database_, host_, password_, port_, ssl_mode_, ssl_mode_to_postgres_string(), and user_.

Referenced by build().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ build_redis()

kcenon::common::Result< std::string > database::integrated::connection_string_builder::build_redis ( ) const
nodiscardprivate
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 211 of file connection_string_builder.cpp.

211 {
212 // Redis uses URI format: redis://[user:password@]host[:port][/database]
213 std::ostringstream oss;
214 oss << "redis://";
215
216 if (user_.has_value() && password_.has_value()) {
217 oss << *user_ << ':' << *password_ << '@';
218 } else if (password_.has_value()) {
219 // Redis often uses just password without username
220 oss << ':' << *password_ << '@';
221 }
222
223 if (host_.has_value()) {
224 oss << *host_;
225 } else {
226 oss << "localhost";
227 }
228
229 if (port_.has_value()) {
230 oss << ':' << *port_;
231 }
232
233 if (database_.has_value()) {
234 // Redis database is a number (0-15 typically)
235 oss << '/' << *database_;
236 }
237
238 return kcenon::common::Result<std::string>::ok(oss.str());
239}

References database_, host_, password_, port_, and user_.

Referenced by build().

Here is the caller graph for this function:

◆ build_sqlite()

kcenon::common::Result< std::string > database::integrated::connection_string_builder::build_sqlite ( ) const
nodiscardprivate
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 143 of file connection_string_builder.cpp.

143 {
144 // SQLite uses file path or :memory: for in-memory database
145 if (in_memory_) {
146 return kcenon::common::Result<std::string>::ok(":memory:");
147 }
148
149 if (!database_.has_value() || database_->empty()) {
150 return kcenon::common::Result<std::string>::err(
151 -1, "SQLite requires a database file path or in_memory() to be set",
152 "connection_string_builder");
153 }
154
155 return kcenon::common::Result<std::string>::ok(*database_);
156}

References database_, and in_memory_.

Referenced by build().

Here is the caller graph for this function:

◆ connect_timeout()

connection_string_builder & database::integrated::connection_string_builder::connect_timeout ( uint32_t seconds)

Set the connection timeout.

Parameters
secondsTimeout in seconds
Returns
Reference to this builder for chaining
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 41 of file connection_string_builder.cpp.

41 {
42 connect_timeout_ = seconds;
43 return *this;
44}

References connect_timeout_.

Referenced by test_builder_chaining(), and test_postgres_timeout().

Here is the caller graph for this function:

◆ database()

connection_string_builder & database::integrated::connection_string_builder::database ( std::string_view db)

Set the database name.

Parameters
dbDatabase name or file path (for SQLite)
Returns
Reference to this builder for chaining
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 21 of file connection_string_builder.cpp.

21 {
22 database_ = std::string(db);
23 return *this;
24}

References database_.

Referenced by test_builder_chaining(), test_builder_copy(), test_builder_reset(), test_builder_reuse(), test_mongodb_basic(), test_mongodb_default_host(), test_mongodb_with_auth(), test_postgres_basic(), test_postgres_ssl(), test_redis_with_database(), test_sqlite_file(), and test_sqlite_memory_overrides_file().

Here is the caller graph for this function:

◆ host()

connection_string_builder & database::integrated::connection_string_builder::host ( std::string_view h)

◆ in_memory()

connection_string_builder & database::integrated::connection_string_builder::in_memory ( )

Configure SQLite to use in-memory database.

Returns
Reference to this builder for chaining
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 51 of file connection_string_builder.cpp.

51 {
52 in_memory_ = true;
53 return *this;
54}

References in_memory_.

Referenced by test_sqlite_memory(), and test_sqlite_memory_overrides_file().

Here is the caller graph for this function:

◆ operator=() [1/2]

connection_string_builder & database::integrated::connection_string_builder::operator= ( connection_string_builder && )
defaultnoexcept

◆ operator=() [2/2]

◆ option()

connection_string_builder & database::integrated::connection_string_builder::option ( std::string_view key,
std::string_view value )

Add a custom option.

Parameters
keyOption key
valueOption value
Returns
Reference to this builder for chaining
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 56 of file connection_string_builder.cpp.

56 {
57 custom_options_.emplace_back(std::string(key), std::string(value));
58 return *this;
59}

References custom_options_.

Referenced by test_builder_chaining(), and test_postgres_custom_option().

Here is the caller graph for this function:

◆ password()

connection_string_builder & database::integrated::connection_string_builder::password ( std::string_view p)

Set the password for authentication.

Parameters
pPassword
Returns
Reference to this builder for chaining
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 31 of file connection_string_builder.cpp.

31 {
32 password_ = std::string(p);
33 return *this;
34}

References password_.

Referenced by test_builder_chaining(), test_builder_reuse(), test_mongodb_with_auth(), test_postgres_basic(), and test_redis_with_password().

Here is the caller graph for this function:

◆ port()

connection_string_builder & database::integrated::connection_string_builder::port ( uint16_t p)

Set the database port.

Parameters
pPort number (1-65535)
Returns
Reference to this builder for chaining
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 16 of file connection_string_builder.cpp.

16 {
17 port_ = p;
18 return *this;
19}

References port_.

Referenced by test_builder_chaining(), test_builder_copy(), test_builder_reset(), test_builder_reuse(), test_mongodb_basic(), test_postgres_basic(), and test_redis_basic().

Here is the caller graph for this function:

◆ reset()

connection_string_builder & database::integrated::connection_string_builder::reset ( )

Reset the builder to initial state.

Returns
Reference to this builder for chaining
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 77 of file connection_string_builder.cpp.

77 {
78 host_.reset();
79 port_.reset();
80 database_.reset();
81 user_.reset();
82 password_.reset();
83 ssl_mode_.reset();
84 connect_timeout_.reset();
85 application_name_.reset();
86 in_memory_ = false;
87 custom_options_.clear();
88 return *this;
89}

References application_name_, connect_timeout_, custom_options_, database_, host_, in_memory_, password_, port_, reset(), ssl_mode_, and user_.

Referenced by reset(), test_builder_reset(), and test_builder_reuse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ssl_mode()

connection_string_builder & database::integrated::connection_string_builder::ssl_mode ( enum ssl_mode mode)

Set the SSL connection mode.

Parameters
modeSSL mode
Returns
Reference to this builder for chaining
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 36 of file connection_string_builder.cpp.

36 {
37 ssl_mode_ = mode;
38 return *this;
39}

References ssl_mode_.

Referenced by test_builder_chaining(), test_mongodb_ssl(), test_postgres_ssl(), and test_postgres_verify_ca().

Here is the caller graph for this function:

◆ ssl_mode_to_postgres_string()

std::string database::integrated::connection_string_builder::ssl_mode_to_postgres_string ( enum ssl_mode mode)
staticnodiscardprivate
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 241 of file connection_string_builder.cpp.

241 {
242 switch (mode) {
244 return "disable";
245 case ssl_mode::allow:
246 return "allow";
247 case ssl_mode::prefer:
248 return "prefer";
250 return "require";
252 return "verify-ca";
254 return "verify-full";
255 default:
256 return "prefer";
257 }
258}
@ prefer
Try SSL first, fall back to non-SSL.
@ verify_ca
Require SSL with CA verification.
@ allow
Try SSL, fall back to non-SSL.
@ verify_full
Require SSL with full verification.
@ require
Require SSL, no verification.

References database::integrated::allow, database::integrated::disable, database::integrated::prefer, database::integrated::require, database::integrated::verify_ca, and database::integrated::verify_full.

Referenced by build_postgres().

Here is the caller graph for this function:

◆ user()

connection_string_builder & database::integrated::connection_string_builder::user ( std::string_view u)

Set the username for authentication.

Parameters
uUsername
Returns
Reference to this builder for chaining
Examples
/home/runner/work/database_system/database_system/database/integrated/connection_string_builder.h.

Definition at line 26 of file connection_string_builder.cpp.

26 {
27 user_ = std::string(u);
28 return *this;
29}

References user_.

Referenced by test_builder_chaining(), test_builder_reuse(), test_mongodb_with_auth(), and test_postgres_basic().

Here is the caller graph for this function:

Member Data Documentation

◆ application_name_

std::optional<std::string> database::integrated::connection_string_builder::application_name_
private

◆ connect_timeout_

std::optional<uint32_t> database::integrated::connection_string_builder::connect_timeout_
private

◆ custom_options_

std::vector<std::pair<std::string, std::string> > database::integrated::connection_string_builder::custom_options_
private

◆ database_

std::optional<std::string> database::integrated::connection_string_builder::database_
private

◆ host_

std::optional<std::string> database::integrated::connection_string_builder::host_
private

◆ in_memory_

bool database::integrated::connection_string_builder::in_memory_ = false
private

◆ password_

std::optional<std::string> database::integrated::connection_string_builder::password_
private

◆ port_

std::optional<uint16_t> database::integrated::connection_string_builder::port_
private

◆ ssl_mode_

std::optional<enum ssl_mode> database::integrated::connection_string_builder::ssl_mode_
private

◆ user_

std::optional<std::string> database::integrated::connection_string_builder::user_
private

The documentation for this class was generated from the following files: