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/// Represents a connection data source as a DSN, username, password, and timeout.
41struct [[nodiscard]] SqlConnectionDataSource
42{
43 /// The ODBC data source name (DSN).
44 std::string datasource;
45 /// The username for authentication.
46 std::string username;
47 /// The password for authentication.
48 std::string password;
49 /// The connection timeout duration.
50 std::chrono::seconds timeout { 5 };
51
52 /// Constructs a SqlConnectionDataSource from the given connection string.
54
55 /// Converts this data source to an ODBC connection string.
56 [[nodiscard]] LIGHTWEIGHT_API SqlConnectionString ToConnectionString() const
57 {
58 return SqlConnectionString {
59 .value = std::format("DSN={};UID={};PWD={};TIMEOUT={}", datasource, username, password, timeout.count())
60 };
61 }
62
63 /// Three-way comparison operator.
64 auto operator<=>(SqlConnectionDataSource const&) const noexcept = default;
65};
66
67using SqlConnectInfo = std::variant<SqlConnectionDataSource, SqlConnectionString>;
68
69} // namespace Lightweight
70
71template <>
72struct std::formatter<Lightweight::SqlConnectInfo>: std::formatter<std::string>
73{
74 auto format(Lightweight::SqlConnectInfo const& info, format_context& ctx) const -> format_context::iterator
75 {
76 if (auto const* dsn = std::get_if<Lightweight::SqlConnectionDataSource>(&info))
77 {
78 return formatter<string>::format(
79 std::format(
80 "DSN={};UID={};PWD={};TIMEOUT={}", dsn->datasource, dsn->username, dsn->password, dsn->timeout.count()),
81 ctx);
82 }
83 else if (auto const* connectionString = std::get_if<Lightweight::SqlConnectionString>(&info))
84 {
85 return formatter<string>::format(connectionString->value, ctx);
86 }
87 else
88 {
89 return formatter<string>::format("Invalid connection info", ctx);
90 }
91 }
92};
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.