Lightweight 0.20260617.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 <cstddef>
9#include <format>
10#include <map>
11#include <string>
12#include <variant>
13
14namespace Lightweight
15{
16
17/// @brief Default block-prefetch depth for new connections: the number of rows a classic per-row
18/// fetch loop requests per @c SQLFetchScroll round-trip on the transparent prefetch path.
19///
20/// Suffixed (not @c DefaultPrefetchDepth) so it does not collide with the
21/// @c SqlConnection::DefaultPrefetchDepth() accessor. A connection's depth can be overridden via
22/// @c SqlConnection::SetDefaultPrefetchDepth or @ref SqlConnectionDataSource::defaultPrefetchDepth;
23/// a value <= 1 disables prefetch.
24constexpr std::size_t PrefetchDepthDefault = 1000;
25
26/// Represents an ODBC connection string.
28{
29 /// The raw ODBC connection string value.
30 std::string value;
31
32 /// Three-way comparison operator.
33 auto operator<=>(SqlConnectionString const&) const noexcept = default;
34
35 /// Returns a sanitized copy of the connection string with the password masked.
36 [[nodiscard]] LIGHTWEIGHT_API std::string Sanitized() const;
37
38 /// Sanitizes the password in the given connection string input.
39 [[nodiscard]] LIGHTWEIGHT_API static std::string SanitizePwd(std::string_view input);
40};
41
42using SqlConnectionStringMap = std::map<std::string, std::string>;
43
44/// Parses an ODBC connection string into a map.
45LIGHTWEIGHT_API SqlConnectionStringMap ParseConnectionString(SqlConnectionString const& connectionString);
46
47/// Builds an ODBC connection string from a map.
48LIGHTWEIGHT_API SqlConnectionString BuildConnectionString(SqlConnectionStringMap const& map);
49
50/// If `connectionString` targets a file-based SQLite database, ensures the
51/// parent directory exists and touches an empty file when missing.
52///
53/// An empty file is a valid zero-table SQLite database, so this lets callers
54/// bootstrap a fresh SQLite deployment from scratch without requiring the
55/// user to pre-create the file. In-memory databases (`:memory:`,
56/// `file::memory:`, URIs with `mode=memory`) and non-SQLite drivers are
57/// left untouched.
58///
59/// Returns true on success or when no action was needed. Returns false only
60/// when the parent directory could not be created or the file could not be
61/// opened for writing.
62[[nodiscard]] LIGHTWEIGHT_API bool EnsureSqliteDatabaseFileExists(SqlConnectionString const& connectionString);
63
64/// Represents a connection data source as a DSN, username, password, and timeout.
65struct [[nodiscard]] SqlConnectionDataSource
66{
67 /// The ODBC data source name (DSN).
68 std::string datasource;
69 /// The username for authentication.
70 std::string username;
71 /// The password for authentication.
72 std::string password;
73 /// The connection timeout duration.
74 std::chrono::seconds timeout { 5 };
75 /// @brief Default block-prefetch depth applied to statements created on the resulting connection
76 /// (rows requested per @c SQLFetchScroll round-trip on the transparent per-row fetch path).
77 ///
78 /// A value <= 1 disables prefetch (every classic loop keeps issuing one @c SQLFetch per row).
79 /// Defaults to @c PrefetchDepthDefault. Has effect only on backends whose driver supports
80 /// native row-array fetching (see @c SqlConnection::SupportsNativeRowArrayFetch).
81 std::size_t defaultPrefetchDepth = PrefetchDepthDefault;
82
83 /// Constructs a SqlConnectionDataSource from the given connection string.
85
86 /// Converts this data source to an ODBC connection string.
87 [[nodiscard]] LIGHTWEIGHT_API SqlConnectionString ToConnectionString() const
88 {
89 return SqlConnectionString {
90 .value = std::format("DSN={};UID={};PWD={};TIMEOUT={}", datasource, username, password, timeout.count())
91 };
92 }
93
94 /// Three-way comparison operator.
95 auto operator<=>(SqlConnectionDataSource const&) const noexcept = default;
96};
97
98using SqlConnectInfo = std::variant<SqlConnectionDataSource, SqlConnectionString>;
99
100} // namespace Lightweight
101
102template <>
103struct std::formatter<Lightweight::SqlConnectInfo>: std::formatter<std::string>
104{
105 auto format(Lightweight::SqlConnectInfo const& info, format_context& ctx) const -> format_context::iterator
106 {
107 if (auto const* dsn = std::get_if<Lightweight::SqlConnectionDataSource>(&info))
108 {
109 return formatter<string>::format(
110 std::format(
111 "DSN={};UID={};PWD={};TIMEOUT={}", dsn->datasource, dsn->username, dsn->password, dsn->timeout.count()),
112 ctx);
113 }
114 else if (auto const* connectionString = std::get_if<Lightweight::SqlConnectionString>(&info))
115 {
116 return formatter<string>::format(connectionString->value, ctx);
117 }
118 else
119 {
120 return formatter<string>::format("Invalid connection info", ctx);
121 }
122 }
123};
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.