Lightweight 0.20261105.1
Loading...
Searching...
No Matches
DriverSet.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "../Api.hpp"
6#include "Concept.hpp"
7
8#if defined(LIGHTWEIGHT_DRIVER_ODBC)
9 #include "Odbc/Connection.hpp"
10 #include "Odbc/Handles.hpp"
11 #include "Odbc/Statement.hpp"
12#endif
13
14#if defined(LIGHTWEIGHT_DRIVER_SQLITE3)
15 #include "Sqlite3/Connection.hpp"
16 #include "Sqlite3/Handles.hpp"
17 #include "Sqlite3/Statement.hpp"
18#endif
19
20#include <array>
21#include <concepts>
22#include <cstddef>
23#include <functional>
24#include <type_traits>
25#include <utility>
26#include <variant>
27
28namespace Lightweight::Driver
29{
30
31namespace detail
32{
33 /// @brief A compile-time list of drivers.
34 template <SqlDriver... Drivers>
35 struct DriverList
36 {
37 };
38
39 /// @brief Concatenates driver lists.
40 template <typename... Lists>
41 struct ConcatDriverLists;
42
43 template <>
44 struct ConcatDriverLists<>
45 {
46 using Type = DriverList<>;
47 };
48
49 template <SqlDriver... Drivers>
50 struct ConcatDriverLists<DriverList<Drivers...>>
51 {
52 using Type = DriverList<Drivers...>;
53 };
54
55 template <SqlDriver... First, SqlDriver... Second, typename... Rest>
56 struct ConcatDriverLists<DriverList<First...>, DriverList<Second...>, Rest...>
57 {
58 using Type = ConcatDriverLists<DriverList<First..., Second...>, Rest...>::Type;
59 };
60
61#if defined(LIGHTWEIGHT_DRIVER_ODBC)
62 /// The ODBC driver, if enabled.
63 using OdbcDrivers = DriverList<Odbc::Driver>;
64#else
65 using OdbcDrivers = DriverList<>;
66#endif
67
68#if defined(LIGHTWEIGHT_DRIVER_SQLITE3)
69 /// The native SQLite3 driver, if enabled.
70 using Sqlite3Drivers = DriverList<Sqlite3::Driver>;
71#else
72 using Sqlite3Drivers = DriverList<>;
73#endif
74
75 /// The drivers this library was built with, in @ref SqlDriverId order.
76 using EnabledDrivers = ConcatDriverLists<OdbcDrivers, Sqlite3Drivers>::Type;
77
78 // The traits over a driver list that the binder concepts and templates instantiate are function templates deduced
79 // from the list, not class templates with partial specializations: an application importing the Lightweight module
80 // instantiates them for its own types, and GCC 14 does not find a partial specialization from the module's global
81 // module fragment there (it reported the primary template as incomplete). The lists the library itself names
82 // (ConcatDriverLists, HandleFor, and BasicHandle<Role, D> for a single driver) are instantiated inside the module,
83 // through the non-dependent StatementHandle and ConnectionHandle, and keep their partial specializations; the
84 // importer only instantiates member templates (Visit) of those already instantiated classes.
85
86 /// @brief Whether @p Trait holds for the statement handle struct of at least one driver of @p drivers.
87 /// @return The disjunction over the drivers; @c false for an empty list.
88 template <template <typename> typename Trait, typename... Drivers>
89 constexpr bool AnyStatementOf(DriverList<Drivers...> /*drivers*/) noexcept
90 {
91 return (Trait<typename Drivers::Statement>::value || ...);
92 }
93
94 /// @brief Selects a driver's statement handle struct.
95 struct StatementRole
96 {
97 /// The handle struct of driver @p D.
98 template <SqlDriver D>
99 using Type = D::Statement;
100 };
101
102 /// @brief Selects a driver's connection handle struct.
103 struct ConnectionRole
104 {
105 /// The handle struct of driver @p D.
106 template <SqlDriver D>
107 using Type = D::Connection;
108 };
109
110 /// @brief The driver among an empty list whose @p Role handle struct is @p Struct: none.
111 /// @return @c std::type_identity<void>.
112 template <typename Role, typename Struct>
113 constexpr auto FindDriverOf(DriverList<> /*drivers*/) noexcept -> std::type_identity<void>
114 {
115 return {};
116 }
117
118 /// @brief The driver among @p D and @p Rest whose @p Role handle struct is @p Struct.
119 /// @return @c std::type_identity of that driver, or of @c void if there is none.
120 template <typename Role, typename Struct, typename D, typename... Rest>
121 constexpr auto FindDriverOf(DriverList<D, Rest...> /*drivers*/) noexcept
122 {
123 if constexpr (std::same_as<Struct, typename Role::template Type<D>>)
124 return std::type_identity<D> {};
125 else
126 return FindDriverOf<Role, Struct>(DriverList<Rest...> {});
127 }
128
129 /// @brief The driver among @p List whose statement handle struct is @p StatementStruct; @c void if none.
130 template <typename StatementStruct, typename List>
131 using DriverOfStatement = decltype(FindDriverOf<StatementRole, StatementStruct>(List {}))::type;
132
133 /// @brief The driver among @p List whose connection handle struct is @p ConnectionStruct; @c void if none.
134 template <typename ConnectionStruct, typename List>
135 using DriverOfConnection = decltype(FindDriverOf<ConnectionRole, ConnectionStruct>(List {}))::type;
136
137 /// @brief Whether @p StatementStruct is the statement handle struct of an enabled driver.
138 template <typename StatementStruct>
139 concept EnabledStatementStruct =
140 !std::is_void_v<DriverOfStatement<std::remove_const_t<StatementStruct>, EnabledDrivers>>;
141
142 /// @brief The enabled driver whose statement handle struct is @p StatementStruct (possibly const).
143 template <EnabledStatementStruct StatementStruct>
144 using DriverOfStatementT = DriverOfStatement<std::remove_const_t<StatementStruct>, EnabledDrivers>;
145
146 /// @brief A tagged handle: always one of @p Drivers' handle structs for @p Role.
147 ///
148 /// The handle is bound to exactly one driver at all times; "empty" means its struct holds no native
149 /// resource (it equals the value-initialized struct), not that it holds no driver. The handle never
150 /// owns or frees what it holds; its owner does. Moving leaves the source empty, bound to the same driver.
151 template <typename Role, SqlDriver... Drivers>
152 class BasicHandle;
153
154 /// @brief The single-driver handle: just the driver's struct, with no tag.
155 ///
156 /// @ref Visit is a direct call and the handle is exactly the size of the struct.
157 template <typename Role, SqlDriver D>
158 class BasicHandle<Role, D>
159 {
160 public:
161 /// The handle struct held.
162 using Value = Role::template Type<D>;
163
164 /// Constructs an empty handle.
165 BasicHandle() noexcept = default;
166
167 /// @brief Constructs a handle holding @p value.
168 /// @param value The driver's handle struct; a value-initialized one makes an empty handle.
169 explicit BasicHandle(Value value) noexcept:
170 m_value { value }
171 {
172 }
173
174 BasicHandle(BasicHandle const&) = delete;
175 BasicHandle& operator=(BasicHandle const&) = delete;
176
177 /// Takes over what @p other holds, leaving @p other empty.
178 BasicHandle(BasicHandle&& other) noexcept:
179 m_value { std::exchange(other.m_value, Value {}) }
180 {
181 }
182
183 /// Takes over what @p other holds, leaving @p other empty. What this handle held is forgotten.
184 BasicHandle& operator=(BasicHandle&& other) noexcept
185 {
186 m_value = std::exchange(other.m_value, Value {});
187 return *this;
188 }
189
190 ~BasicHandle() noexcept = default;
191
192 /// @return Whether the held struct holds no native resource, i.e. equals a value-initialized one.
193 [[nodiscard]] bool IsEmpty() const noexcept
194 {
195 return m_value == Value {};
196 }
197
198 /// Forgets the native resource the handle holds, without freeing it. The driver is kept.
199 void Reset() noexcept
200 {
201 m_value = Value {};
202 }
203
204 /// @brief The id of the driver the handle is bound to.
205 ///
206 /// Non-static like the several-drivers handle's, so callers spell it the same for either handle.
207 /// @return The driver's id; valid on an empty handle too.
208 [[nodiscard]] constexpr SqlDriverId DriverId() const noexcept
209 {
210 return D::Id;
211 }
212
213 /// @brief Calls @p f with the held driver struct; valid on an empty handle too.
214 /// @param f The callable; it is invoked with a reference to the struct.
215 /// @return What @p f returns.
216 template <typename F>
217 LIGHTWEIGHT_FORCE_INLINE decltype(auto) Visit(F&& f)
218 {
219 return std::invoke(std::forward<F>(f), m_value);
220 }
221
222 /// @copydoc Visit(F&&)
223 template <typename F>
224 LIGHTWEIGHT_FORCE_INLINE decltype(auto) Visit(F&& f) const
225 {
226 return std::invoke(std::forward<F>(f), m_value);
227 }
228
229 private:
230 Value m_value {};
231 };
232
233 /// @brief The several-drivers handle: a tag and the struct of the driver it names.
234 ///
235 /// @ref Visit branches on the tag; there is no function-pointer table. A default-constructed handle is
236 /// bound to the first driver.
237 template <typename Role, SqlDriver... Drivers>
238 class BasicHandle
239 {
240 static_assert(sizeof...(Drivers) > 1, "The single-driver handle is the partial specialization above.");
241
242 using Storage = std::variant<typename Role::template Type<Drivers>...>;
243
244 public:
245 /// Constructs an empty handle bound to the first driver.
246 BasicHandle() noexcept = default;
247
248 /// @brief Constructs a handle bound to @p value's driver, holding @p value.
249 /// @param value One driver's handle struct; a value-initialized one makes an empty handle for
250 /// that driver.
251 template <typename Value>
252 requires((std::same_as<Value, typename Role::template Type<Drivers>> || ...))
253 explicit BasicHandle(Value value) noexcept:
254 m_storage { std::in_place_type<Value>, value }
255 {
256 }
257
258 BasicHandle(BasicHandle const&) = delete;
259 BasicHandle& operator=(BasicHandle const&) = delete;
260
261 /// Takes over what @p other holds, leaving @p other empty and bound to the same driver.
262 BasicHandle(BasicHandle&& other) noexcept:
263 m_storage { other.m_storage }
264 {
265 other.Reset();
266 }
267
268 /// Takes over what @p other holds, leaving @p other empty and bound to the same driver. What this
269 /// handle held is forgotten.
270 BasicHandle& operator=(BasicHandle&& other) noexcept
271 {
272 if (this == &other)
273 return *this;
274 m_storage = other.m_storage;
275 other.Reset();
276 return *this;
277 }
278
279 ~BasicHandle() noexcept = default;
280
281 /// @return Whether the held struct holds no native resource, i.e. equals a value-initialized one.
282 [[nodiscard]] bool IsEmpty() const noexcept
283 {
284 return Visit([](auto const& value) { return value == std::remove_cvref_t<decltype(value)> {}; });
285 }
286
287 /// Forgets the native resource the handle holds, without freeing it. The driver is kept.
288 void Reset() noexcept
289 {
290 Visit([](auto& value) { value = std::remove_cvref_t<decltype(value)> {}; });
291 }
292
293 /// @return The id of the driver the handle is bound to; valid on an empty handle too.
294 [[nodiscard]] SqlDriverId DriverId() const noexcept
295 {
296 static constexpr auto ids = std::array<SqlDriverId, sizeof...(Drivers)> { Drivers::Id... };
297 return ids[m_storage.index()];
298 }
299
300 /// @brief Calls @p f with the held driver struct; valid on an empty handle too.
301 /// @param f The callable; it is invoked with a reference to the struct, whichever driver's it is.
302 /// @return What @p f returns; it must return the same type for every driver.
303 template <typename F>
304 [[nodiscard]] decltype(auto) Visit(F&& f)
305 {
306 return Dispatch<0>(m_storage, std::forward<F>(f));
307 }
308
309 /// @copydoc Visit(F&&)
310 template <typename F>
311 [[nodiscard]] decltype(auto) Visit(F&& f) const
312 {
313 return Dispatch<0>(m_storage, std::forward<F>(f));
314 }
315
316 private:
317 /// Compares the tag with alternative @p I, and either calls @p f with it or tries the next one.
318 /// The last alternative needs no comparison: the storage always holds one of them.
319 template <std::size_t I, typename S, typename F>
320 static decltype(auto) Dispatch(S& storage, F&& f)
321 {
322 if constexpr (I + 1 == sizeof...(Drivers))
323 return std::invoke(std::forward<F>(f), *std::get_if<I>(&storage));
324 else
325 {
326 if (storage.index() == I)
327 return std::invoke(std::forward<F>(f), *std::get_if<I>(&storage));
328 return Dispatch<I + 1>(storage, std::forward<F>(f));
329 }
330 }
331
332 Storage m_storage {};
333 };
334
335 /// @brief The tagged handle type for @p Role over the drivers in @p List.
336 template <typename Role, typename List>
337 struct HandleFor;
338
339 template <typename Role, SqlDriver... Drivers>
340 struct HandleFor<Role, DriverList<Drivers...>>
341 {
342 static_assert(sizeof...(Drivers) > 0, "The library must be built with at least one driver.");
343 using Type = BasicHandle<Role, Drivers...>;
344 };
345} // namespace detail
346
347/// @brief Whether @p Trait holds for the statement handle struct of at least one enabled driver.
348///
349/// @p Trait is a class template over a statement handle struct with a @c bool member @c value, e.g. "the
350/// binder of some type provides an input parameter function for this statement struct". A binder concept
351/// asks for at least one driver rather than all of them, so that a type only one driver can bind (such as
352/// an ODBC-only raw column) is still a binder there.
353template <template <typename> typename Trait>
354inline constexpr bool AnyEnabledStatement = detail::AnyStatementOf<Trait>(detail::EnabledDrivers {});
355
356/// @brief A statement's tagged driver handle: always one enabled driver's @c Statement struct, possibly empty.
357///
358/// With one enabled driver it is exactly that driver's struct; it never owns or frees the handle.
359using StatementHandle = detail::HandleFor<detail::StatementRole, detail::EnabledDrivers>::Type;
360
361/// @brief A connection's tagged driver handle: always one enabled driver's @c Connection struct, possibly empty.
362///
363/// With one enabled driver it is exactly that driver's struct; it never owns or frees the handle.
364using ConnectionHandle = detail::HandleFor<detail::ConnectionRole, detail::EnabledDrivers>::Type;
365
366/// @brief Calls @p f with the driver struct @p handle holds, by reference.
367/// @param handle The handle; it may be empty, @p f then sees the value-initialized struct of its driver.
368/// @param f The callable.
369/// @return What @p f returns.
370template <typename F>
371inline LIGHTWEIGHT_FORCE_INLINE decltype(auto) Visit(StatementHandle& handle, F&& f)
372{
373 return handle.Visit(std::forward<F>(f));
374}
375
376/// @copydoc Visit(StatementHandle&, F&&)
377template <typename F>
378inline LIGHTWEIGHT_FORCE_INLINE decltype(auto) Visit(StatementHandle const& handle, F&& f)
379{
380 return handle.Visit(std::forward<F>(f));
381}
382
383/// @copydoc Visit(StatementHandle&, F&&)
384template <typename F>
385inline LIGHTWEIGHT_FORCE_INLINE decltype(auto) Visit(ConnectionHandle& handle, F&& f)
386{
387 return handle.Visit(std::forward<F>(f));
388}
389
390/// @copydoc Visit(StatementHandle&, F&&)
391template <typename F>
392inline LIGHTWEIGHT_FORCE_INLINE decltype(auto) Visit(ConnectionHandle const& handle, F&& f)
393{
394 return handle.Visit(std::forward<F>(f));
395}
396
397} // namespace Lightweight::Driver