12 host_ = std::string(h);
27 user_ = std::string(u);
72 return kcenon::common::Result<std::string>::err(
73 -1,
"Unknown backend type",
"connection_string_builder");
92 std::ostringstream oss;
95 auto append = [&oss, &first](
const std::string& key,
const std::string& value) {
99 oss << key <<
'=' << value;
104 if (
host_.has_value()) {
105 append(
"host", *
host_);
108 if (
port_.has_value()) {
109 append(
"port", std::to_string(*
port_));
116 if (
user_.has_value()) {
117 append(
"user", *
user_);
140 return kcenon::common::Result<std::string>::ok(oss.str());
146 return kcenon::common::Result<std::string>::ok(
":memory:");
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");
155 return kcenon::common::Result<std::string>::ok(*
database_);
160 std::ostringstream oss;
167 if (
host_.has_value()) {
173 if (
port_.has_value()) {
174 oss <<
':' << *
port_;
186 auto append_param = [&oss, &first](
const std::string& key,
const std::string& value) {
190 oss << key <<
'=' << value;
196 append_param(
"ssl", use_ssl ?
"true" :
"false");
204 append_param(key, value);
208 return kcenon::common::Result<std::string>::ok(oss.str());
213 std::ostringstream oss;
223 if (
host_.has_value()) {
229 if (
port_.has_value()) {
230 oss <<
':' << *
port_;
238 return kcenon::common::Result<std::string>::ok(oss.str());
254 return "verify-full";
Fluent builder for constructing database connection strings.
std::optional< uint32_t > connect_timeout_
kcenon::common::Result< std::string > build_sqlite() const
connection_string_builder & database(std::string_view db)
Set the database name.
connection_string_builder & user(std::string_view u)
Set the username for authentication.
connection_string_builder & port(uint16_t p)
Set the database port.
connection_string_builder & host(std::string_view h)
Set the database host.
connection_string_builder & ssl_mode(enum ssl_mode mode)
Set the SSL connection mode.
std::optional< std::string > application_name_
connection_string_builder & application_name(std::string_view name)
Set the application name (for PostgreSQL)
kcenon::common::Result< std::string > build_postgres() const
connection_string_builder & reset()
Reset the builder to initial state.
std::vector< std::pair< std::string, std::string > > custom_options_
static std::string ssl_mode_to_postgres_string(enum ssl_mode mode)
kcenon::common::Result< std::string > build_redis() const
connection_string_builder & option(std::string_view key, std::string_view value)
Add a custom option.
std::optional< std::string > database_
std::optional< std::string > password_
connection_string_builder & in_memory()
Configure SQLite to use in-memory database.
connection_string_builder & password(std::string_view p)
Set the password for authentication.
std::optional< std::string > host_
kcenon::common::Result< std::string > build_mongodb() const
std::optional< std::string > user_
std::optional< enum ssl_mode > ssl_mode_
connection_string_builder & connect_timeout(uint32_t seconds)
Set the connection timeout.
std::optional< uint16_t > port_
kcenon::common::Result< std::string > build(backend_type type) const
Build the connection string for the specified backend.
Fluent builder for constructing type-safe database connection strings.
ssl_mode
SSL connection mode for database connections.
@ 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.
backend_type
Database backend type enumeration.
@ mongodb
MongoDB NoSQL database.
@ sqlite
SQLite embedded database.
@ redis
Redis key-value store.
@ postgres
PostgreSQL database.