Lightweight 0.20260303.0
Loading...
Searching...
No Matches
SqlConnectInfo.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "Api.hpp"
6
7#include <chrono>
8#include <format>
9#include <map>
10#include <string>
11#include <variant>
12
13namespace Lightweight
14{
15
16/// Represents an ODBC connection string.
18{
19 /// The raw ODBC connection string value.
20 std::string value;
21
22 /// Three-way comparison operator.
23 auto operator<=>(SqlConnectionString const&) const noexcept = default;
24
25 /// Returns a sanitized copy of the connection string with the password masked.
26 [[nodiscard]] LIGHTWEIGHT_API std::string Sanitized() const;
27
28 /// Sanitizes the password in the given connection string input.
29 [[nodiscard]] LIGHTWEIGHT_API static std::string SanitizePwd(std::string_view input);
30};
31
32using SqlConnectionStringMap = std::map<std::string, std::string>;
33
34/// Parses an ODBC connection string into a map.
35LIGHTWEIGHT_API SqlConnectionStringMap ParseConnectionString(SqlConnectionString const& connectionString);
36
37/// Builds an ODBC connection string from a map.
38LIGHTWEIGHT_API SqlConnectionString BuildConnectionString(SqlConnectionStringMap const& map);
39
40/// If `connectionString` targets a file-based SQLite database, ensures the
41/// parent directory exists and touches an empty file when missing.
42///
43/// An empty file is a valid zero-table SQLite database, so this lets callers
44/// bootstrap a fresh SQLite deployment from scratch without requiring the
45/// user to pre-create the file. In-memory databases (`:memory:`,
46/// `file::memory:`, URIs with `mode=memory`) and non-SQLite drivers are
47/// left untouched.
48///
49/// Returns true on success or when no action was needed. Returns false only
50/// when the parent directory could not be created or the file could not be
51/// opened for writing.
52[[nodiscard]] LIGHTWEIGHT_API bool EnsureSqliteDatabaseFileExists(SqlConnectionString const& connectionString);
53
54/// Represents a connection data source as a DSN, username, password, and timeout.
55struct [[nodiscard]] SqlConnectionDataSource
56{
57 /// The ODBC data source name (DSN).
58 std::string datasource;
59 /// The username for authentication.
60 std::string username;
61 /// The password for authentication.
62 std::string password;
63 /// The connection timeout duration.
64 std::chrono::seconds timeout { 5 };
65
66 /// Constructs a SqlConnectionDataSource from the given connection string.
68
69 /// Converts this data source to an ODBC connection string.
70 [[nodiscard]] LIGHTWEIGHT_API SqlConnectionString ToConnectionString() const
71 {
72 return SqlConnectionString {
73 .value = std::format("DSN={};UID={};PWD={};TIMEOUT={}", datasource, username, password, timeout.count())
74 };
75 }
76
77 /// Three-way comparison operator.
78 auto operator<=>(SqlConnectionDataSource const&) const noexcept = default;
79};
80
81using SqlConnectInfo = std::variant<SqlConnectionDataSource, SqlConnectionString>;
82
83} // namespace Lightweight
84
85template <>
86struct std::formatter<Lightweight::SqlConnectInfo>: std::formatter<std::string>
87{
88 auto format(Lightweight::SqlConnectInfo const& info, format_context& ctx) const -> format_context::iterator
89 {
90 if (auto const* dsn = std::get_if<Lightweight::SqlConnectionDataSource>(&info))
91 {
92 return formatter<string>::format(
93 std::format(
94 "DSN={};UID={};PWD={};TIMEOUT={}", dsn->datasource, dsn->username, dsn->password, dsn->timeout.count()),
95 ctx);
96 }
97 else if (auto const* connectionString = std::get_if<Lightweight::SqlConnectionString>(&info))
98 {
99 return formatter<string>::format(connectionString->value, ctx);
100 }
101 else
102 {
103 return formatter<string>::format("Invalid connection info", ctx);
104 }
105 }
106};
Represents a connection data source as a DSN, username, password, and timeout.
auto operator<=>(SqlConnectionDataSource const &) const noexcept=default
Three-way comparison operator.
static LIGHTWEIGHT_API SqlConnectionDataSource FromConnectionString(SqlConnectionString const &value)
Constructs a SqlConnectionDataSource from the given connection string.
LIGHTWEIGHT_API SqlConnectionString ToConnectionString() const
Converts this data source to an ODBC connection string.
std::string datasource
The ODBC data source name (DSN).
std::string password
The password for authentication.
std::string username
The username for authentication.
Represents an ODBC connection string.
auto operator<=>(SqlConnectionString const &) const noexcept=default
Three-way comparison operator.
static LIGHTWEIGHT_API std::string SanitizePwd(std::string_view input)
Sanitizes the password in the given connection string input.
std::string value
The raw ODBC connection string value.
LIGHTWEIGHT_API std::string Sanitized() const
Returns a sanitized copy of the connection string with the password masked.