|
6 | 6 | #include <Windows.h> // NOLINT(llvm-include-order) — must precede sqlext.h on Windows. |
7 | 7 | #endif |
8 | 8 |
|
9 | | -#include <sql.h> |
10 | | -#include <sqlext.h> |
11 | | - |
12 | 9 | #include <array> |
13 | 10 | #include <cstdlib> |
14 | 11 | #include <cstring> |
15 | 12 | #include <span> |
16 | 13 | #include <string_view> |
17 | 14 |
|
| 15 | +#include <sql.h> |
| 16 | +#include <sqlext.h> |
| 17 | + |
18 | 18 | namespace Lightweight::Odbc |
19 | 19 | { |
20 | 20 |
|
21 | 21 | namespace |
22 | 22 | { |
23 | 23 |
|
24 | | -/// RAII wrapper around a scratch `SQL_HANDLE_ENV` used only for enumeration. |
25 | | -/// |
26 | | -/// A fresh env handle per call keeps us decoupled from `SqlConnection`'s |
27 | | -/// per-connection env — this matters for the migrations GUI, which may want |
28 | | -/// to refresh the DSN list while a connection is open against a different |
29 | | -/// driver manager configuration. |
30 | | -class ScratchEnv |
31 | | -{ |
32 | | - public: |
33 | | - ScratchEnv() noexcept |
| 24 | + /// RAII wrapper around a scratch `SQL_HANDLE_ENV` used only for enumeration. |
| 25 | + /// |
| 26 | + /// A fresh env handle per call keeps us decoupled from `SqlConnection`'s |
| 27 | + /// per-connection env — this matters for the migrations GUI, which may want |
| 28 | + /// to refresh the DSN list while a connection is open against a different |
| 29 | + /// driver manager configuration. |
| 30 | + class ScratchEnv |
34 | 31 | { |
35 | | - if (SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &_hEnv) != SQL_SUCCESS) |
| 32 | + public: |
| 33 | + ScratchEnv() noexcept |
36 | 34 | { |
37 | | - _hEnv = SQL_NULL_HANDLE; |
38 | | - return; |
| 35 | + if (SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &_hEnv) != SQL_SUCCESS) |
| 36 | + { |
| 37 | + _hEnv = SQL_NULL_HANDLE; |
| 38 | + return; |
| 39 | + } |
| 40 | + // ODBC 3.x is a prerequisite for the driver manager to honour the |
| 41 | + // `SQL_FETCH_*` modes used below. Cast matches Microsoft's header |
| 42 | + // expectations (integer-as-pointer for environment attributes). |
| 43 | + SQLSetEnvAttr(_hEnv, |
| 44 | + SQL_ATTR_ODBC_VERSION, |
| 45 | + reinterpret_cast<SQLPOINTER>(SQL_OV_ODBC3), |
| 46 | + 0); // NOLINT(performance-no-int-to-ptr) |
39 | 47 | } |
40 | | - // ODBC 3.x is a prerequisite for the driver manager to honour the |
41 | | - // `SQL_FETCH_*` modes used below. Cast matches Microsoft's header |
42 | | - // expectations (integer-as-pointer for environment attributes). |
43 | | - SQLSetEnvAttr( |
44 | | - _hEnv, SQL_ATTR_ODBC_VERSION, reinterpret_cast<SQLPOINTER>(SQL_OV_ODBC3), 0); // NOLINT(performance-no-int-to-ptr) |
45 | | - } |
46 | | - |
47 | | - ~ScratchEnv() |
48 | | - { |
49 | | - if (_hEnv != SQL_NULL_HANDLE) |
50 | | - SQLFreeHandle(SQL_HANDLE_ENV, _hEnv); |
51 | | - } |
52 | 48 |
|
53 | | - ScratchEnv(ScratchEnv const&) = delete; |
54 | | - ScratchEnv(ScratchEnv&&) = delete; |
55 | | - ScratchEnv& operator=(ScratchEnv const&) = delete; |
56 | | - ScratchEnv& operator=(ScratchEnv&&) = delete; |
57 | | - |
58 | | - [[nodiscard]] SQLHENV Handle() const noexcept |
59 | | - { |
60 | | - return _hEnv; |
61 | | - } |
62 | | - [[nodiscard]] bool Ok() const noexcept |
63 | | - { |
64 | | - return _hEnv != SQL_NULL_HANDLE; |
65 | | - } |
66 | | - |
67 | | - private: |
68 | | - SQLHENV _hEnv = SQL_NULL_HANDLE; |
69 | | -}; |
| 49 | + ~ScratchEnv() |
| 50 | + { |
| 51 | + if (_hEnv != SQL_NULL_HANDLE) |
| 52 | + SQLFreeHandle(SQL_HANDLE_ENV, _hEnv); |
| 53 | + } |
70 | 54 |
|
71 | | -/// Trims trailing NULs / whitespace that some driver managers include when |
72 | | -/// reporting fixed-width DSN fields. |
73 | | -[[nodiscard]] std::string NormalizeBuffer(std::span<SQLCHAR const> buffer) noexcept |
74 | | -{ |
75 | | - std::string_view const view { reinterpret_cast<char const*>(buffer.data()), buffer.size() }; |
76 | | - auto const firstNul = view.find('\0'); |
77 | | - auto const bounded = firstNul == std::string_view::npos ? view : view.substr(0, firstNul); |
| 55 | + ScratchEnv(ScratchEnv const&) = delete; |
| 56 | + ScratchEnv(ScratchEnv&&) = delete; |
| 57 | + ScratchEnv& operator=(ScratchEnv const&) = delete; |
| 58 | + ScratchEnv& operator=(ScratchEnv&&) = delete; |
78 | 59 |
|
79 | | - auto const last = bounded.find_last_not_of(" \t\r\n"); |
80 | | - if (last == std::string_view::npos) |
81 | | - return {}; |
82 | | - return std::string { bounded.substr(0, last + 1) }; |
83 | | -} |
84 | | - |
85 | | -/// Parses a driver attributes buffer — a double-NUL-terminated sequence of |
86 | | -/// `KEY=VALUE\0KEY=VALUE\0\0` entries — into a vector of key/value pairs. |
87 | | -[[nodiscard]] std::vector<std::pair<std::string, std::string>> ParseDriverAttributes( |
88 | | - std::span<SQLCHAR const> buffer) noexcept |
89 | | -{ |
90 | | - std::vector<std::pair<std::string, std::string>> attributes; |
| 60 | + [[nodiscard]] SQLHENV Handle() const noexcept |
| 61 | + { |
| 62 | + return _hEnv; |
| 63 | + } |
| 64 | + [[nodiscard]] bool Ok() const noexcept |
| 65 | + { |
| 66 | + return _hEnv != SQL_NULL_HANDLE; |
| 67 | + } |
91 | 68 |
|
92 | | - char const* cursor = reinterpret_cast<char const*>(buffer.data()); |
93 | | - char const* const end = cursor + buffer.size(); |
| 69 | + private: |
| 70 | + SQLHENV _hEnv = SQL_NULL_HANDLE; |
| 71 | + }; |
94 | 72 |
|
95 | | - while (cursor < end && *cursor != '\0') |
| 73 | + /// Trims trailing NULs / whitespace that some driver managers include when |
| 74 | + /// reporting fixed-width DSN fields. |
| 75 | + [[nodiscard]] std::string NormalizeBuffer(std::span<SQLCHAR const> buffer) noexcept |
96 | 76 | { |
97 | | - std::string_view const entry { cursor, std::strlen(cursor) }; |
98 | | - if (auto const eq = entry.find('='); eq != std::string_view::npos) |
99 | | - attributes.emplace_back(std::string { entry.substr(0, eq) }, std::string { entry.substr(eq + 1) }); |
100 | | - else |
101 | | - attributes.emplace_back(std::string { entry }, std::string {}); |
102 | | - cursor += entry.size() + 1; |
| 77 | + std::string_view const view { reinterpret_cast<char const*>(buffer.data()), buffer.size() }; |
| 78 | + auto const firstNul = view.find('\0'); |
| 79 | + auto const bounded = firstNul == std::string_view::npos ? view : view.substr(0, firstNul); |
| 80 | + |
| 81 | + auto const last = bounded.find_last_not_of(" \t\r\n"); |
| 82 | + if (last == std::string_view::npos) |
| 83 | + return {}; |
| 84 | + return std::string { bounded.substr(0, last + 1) }; |
103 | 85 | } |
104 | 86 |
|
105 | | - return attributes; |
106 | | -} |
| 87 | + /// Parses a driver attributes buffer — a double-NUL-terminated sequence of |
| 88 | + /// `KEY=VALUE\0KEY=VALUE\0\0` entries — into a vector of key/value pairs. |
| 89 | + [[nodiscard]] std::vector<std::pair<std::string, std::string>> ParseDriverAttributes( |
| 90 | + std::span<SQLCHAR const> buffer) noexcept |
| 91 | + { |
| 92 | + std::vector<std::pair<std::string, std::string>> attributes; |
107 | 93 |
|
108 | | -/// Runs a single `SQLDataSources` scan pass (user or system) and appends its |
109 | | -/// results to `out`. Stops on the first non-success / non-info return code, |
110 | | -/// which matches the driver manager's end-of-iteration contract. |
111 | | -void EnumerateDataSourcesPass(SQLHENV hEnv, SQLUSMALLINT fetchType, DataSourceInfo::Scope scope, |
112 | | - std::vector<DataSourceInfo>& out) |
113 | | -{ |
114 | | - constexpr SQLSMALLINT NameBufferSize = SQL_MAX_DSN_LENGTH + 1; |
115 | | - constexpr SQLSMALLINT DescriptionBufferSize = 256; |
| 94 | + char const* cursor = reinterpret_cast<char const*>(buffer.data()); |
| 95 | + char const* const end = cursor + buffer.size(); |
116 | 96 |
|
117 | | - std::array<SQLCHAR, NameBufferSize> nameBuf {}; |
118 | | - std::array<SQLCHAR, DescriptionBufferSize> descBuf {}; |
119 | | - SQLSMALLINT nameLen = 0; |
120 | | - SQLSMALLINT descLen = 0; |
| 97 | + while (cursor < end && *cursor != '\0') |
| 98 | + { |
| 99 | + std::string_view const entry { cursor, std::strlen(cursor) }; |
| 100 | + if (auto const eq = entry.find('='); eq != std::string_view::npos) |
| 101 | + attributes.emplace_back(std::string { entry.substr(0, eq) }, std::string { entry.substr(eq + 1) }); |
| 102 | + else |
| 103 | + attributes.emplace_back(std::string { entry }, std::string {}); |
| 104 | + cursor += entry.size() + 1; |
| 105 | + } |
121 | 106 |
|
122 | | - SQLRETURN rc = SQLDataSources(hEnv, |
123 | | - fetchType, |
124 | | - nameBuf.data(), |
125 | | - static_cast<SQLSMALLINT>(nameBuf.size()), |
126 | | - &nameLen, |
127 | | - descBuf.data(), |
128 | | - static_cast<SQLSMALLINT>(descBuf.size()), |
129 | | - &descLen); |
130 | | - while (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) |
131 | | - { |
132 | | - out.push_back(DataSourceInfo { |
133 | | - .name = NormalizeBuffer(std::span<SQLCHAR const> { nameBuf.data(), static_cast<size_t>(nameLen) }), |
134 | | - .description = NormalizeBuffer(std::span<SQLCHAR const> { descBuf.data(), static_cast<size_t>(descLen) }), |
135 | | - .scope = scope, |
136 | | - }); |
| 107 | + return attributes; |
| 108 | + } |
137 | 109 |
|
138 | | - // Every pass after the first on the same fetchType wants FETCH_NEXT. |
139 | | - rc = SQLDataSources(hEnv, |
140 | | - SQL_FETCH_NEXT, |
141 | | - nameBuf.data(), |
142 | | - static_cast<SQLSMALLINT>(nameBuf.size()), |
143 | | - &nameLen, |
144 | | - descBuf.data(), |
145 | | - static_cast<SQLSMALLINT>(descBuf.size()), |
146 | | - &descLen); |
| 110 | + /// Runs a single `SQLDataSources` scan pass (user or system) and appends its |
| 111 | + /// results to `out`. Stops on the first non-success / non-info return code, |
| 112 | + /// which matches the driver manager's end-of-iteration contract. |
| 113 | + void EnumerateDataSourcesPass(SQLHENV hEnv, |
| 114 | + SQLUSMALLINT fetchType, |
| 115 | + DataSourceInfo::Scope scope, |
| 116 | + std::vector<DataSourceInfo>& out) |
| 117 | + { |
| 118 | + constexpr SQLSMALLINT NameBufferSize = SQL_MAX_DSN_LENGTH + 1; |
| 119 | + constexpr SQLSMALLINT DescriptionBufferSize = 256; |
| 120 | + |
| 121 | + std::array<SQLCHAR, NameBufferSize> nameBuf {}; |
| 122 | + std::array<SQLCHAR, DescriptionBufferSize> descBuf {}; |
| 123 | + SQLSMALLINT nameLen = 0; |
| 124 | + SQLSMALLINT descLen = 0; |
| 125 | + |
| 126 | + SQLRETURN rc = SQLDataSources(hEnv, |
| 127 | + fetchType, |
| 128 | + nameBuf.data(), |
| 129 | + static_cast<SQLSMALLINT>(nameBuf.size()), |
| 130 | + &nameLen, |
| 131 | + descBuf.data(), |
| 132 | + static_cast<SQLSMALLINT>(descBuf.size()), |
| 133 | + &descLen); |
| 134 | + while (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) |
| 135 | + { |
| 136 | + out.push_back(DataSourceInfo { |
| 137 | + .name = NormalizeBuffer(std::span<SQLCHAR const> { nameBuf.data(), static_cast<size_t>(nameLen) }), |
| 138 | + .description = NormalizeBuffer(std::span<SQLCHAR const> { descBuf.data(), static_cast<size_t>(descLen) }), |
| 139 | + .scope = scope, |
| 140 | + }); |
| 141 | + |
| 142 | + // Every pass after the first on the same fetchType wants FETCH_NEXT. |
| 143 | + rc = SQLDataSources(hEnv, |
| 144 | + SQL_FETCH_NEXT, |
| 145 | + nameBuf.data(), |
| 146 | + static_cast<SQLSMALLINT>(nameBuf.size()), |
| 147 | + &nameLen, |
| 148 | + descBuf.data(), |
| 149 | + static_cast<SQLSMALLINT>(descBuf.size()), |
| 150 | + &descLen); |
| 151 | + } |
147 | 152 | } |
148 | | -} |
149 | 153 |
|
150 | 154 | } // namespace |
151 | 155 |
|
|
0 commit comments