8#if defined(LIGHTWEIGHT_DRIVER_ODBC)
10 #include "Odbc/Handles.hpp"
14#if defined(LIGHTWEIGHT_DRIVER_SQLITE3)
16 #include "Sqlite3/Handles.hpp"
28namespace Lightweight::Driver
34 template <SqlDriver... Drivers>
40 template <
typename... Lists>
41 struct ConcatDriverLists;
44 struct ConcatDriverLists<>
46 using Type = DriverList<>;
49 template <SqlDriver... Drivers>
50 struct ConcatDriverLists<DriverList<Drivers...>>
52 using Type = DriverList<Drivers...>;
55 template <SqlDriver... First, SqlDriver... Second,
typename... Rest>
56 struct ConcatDriverLists<DriverList<First...>, DriverList<Second...>, Rest...>
58 using Type = ConcatDriverLists<DriverList<First..., Second...>, Rest...>::Type;
61#if defined(LIGHTWEIGHT_DRIVER_ODBC)
63 using OdbcDrivers = DriverList<Odbc::Driver>;
65 using OdbcDrivers = DriverList<>;
68#if defined(LIGHTWEIGHT_DRIVER_SQLITE3)
70 using Sqlite3Drivers = DriverList<Sqlite3::Driver>;
72 using Sqlite3Drivers = DriverList<>;
76 using EnabledDrivers = ConcatDriverLists<OdbcDrivers, Sqlite3Drivers>::Type;
88 template <
template <
typename>
typename Trait,
typename... Drivers>
89 constexpr bool AnyStatementOf(DriverList<Drivers...> )
noexcept
91 return (Trait<typename Drivers::Statement>::value || ...);
98 template <SqlDriver D>
99 using Type = D::Statement;
103 struct ConnectionRole
106 template <SqlDriver D>
107 using Type = D::Connection;
112 template <
typename Role,
typename Struct>
113 constexpr auto FindDriverOf(DriverList<> )
noexcept -> std::type_identity<void>
120 template <
typename Role,
typename Struct,
typename D,
typename... Rest>
121 constexpr auto FindDriverOf(DriverList<D, Rest...> )
noexcept
123 if constexpr (std::same_as<Struct, typename Role::template Type<D>>)
124 return std::type_identity<D> {};
126 return FindDriverOf<Role, Struct>(DriverList<Rest...> {});
130 template <
typename StatementStruct,
typename List>
131 using DriverOfStatement =
decltype(FindDriverOf<StatementRole, StatementStruct>(List {}))::type;
134 template <
typename ConnectionStruct,
typename List>
135 using DriverOfConnection =
decltype(FindDriverOf<ConnectionRole, ConnectionStruct>(List {}))::type;
138 template <
typename StatementStruct>
139 concept EnabledStatementStruct =
140 !std::is_void_v<DriverOfStatement<std::remove_const_t<StatementStruct>, EnabledDrivers>>;
143 template <EnabledStatementStruct StatementStruct>
144 using DriverOfStatementT = DriverOfStatement<std::remove_const_t<StatementStruct>, EnabledDrivers>;
151 template <
typename Role, SqlDriver... Drivers>
157 template <
typename Role, SqlDriver D>
158 class BasicHandle<Role, D>
162 using Value = Role::template Type<D>;
165 BasicHandle() noexcept = default;
169 explicit BasicHandle(Value value) noexcept:
174 BasicHandle(BasicHandle
const&) =
delete;
175 BasicHandle& operator=(BasicHandle
const&) =
delete;
178 BasicHandle(BasicHandle&& other)
noexcept:
179 m_value { std::exchange(other.m_value, Value {}) }
184 BasicHandle& operator=(BasicHandle&& other)
noexcept
186 m_value = std::exchange(other.m_value, Value {});
190 ~BasicHandle() noexcept = default;
193 [[nodiscard]]
bool IsEmpty() const noexcept
195 return m_value == Value {};
199 void Reset() noexcept
208 [[nodiscard]]
constexpr SqlDriverId DriverId() const noexcept
216 template <
typename F>
217 LIGHTWEIGHT_FORCE_INLINE
decltype(
auto) Visit(F&& f)
219 return std::invoke(std::forward<F>(f), m_value);
223 template <
typename F>
224 LIGHTWEIGHT_FORCE_INLINE
decltype(
auto) Visit(F&& f)
const
226 return std::invoke(std::forward<F>(f), m_value);
237 template <
typename Role, SqlDriver... Drivers>
240 static_assert(
sizeof...(Drivers) > 1,
"The single-driver handle is the partial specialization above.");
242 using Storage = std::variant<typename Role::template Type<Drivers>...>;
246 BasicHandle() noexcept = default;
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 }
258 BasicHandle(BasicHandle
const&) =
delete;
259 BasicHandle& operator=(BasicHandle
const&) =
delete;
262 BasicHandle(BasicHandle&& other)
noexcept:
263 m_storage { other.m_storage }
270 BasicHandle& operator=(BasicHandle&& other)
noexcept
274 m_storage = other.m_storage;
279 ~BasicHandle() noexcept = default;
282 [[nodiscard]]
bool IsEmpty() const noexcept
284 return Visit([](
auto const& value) {
return value == std::remove_cvref_t<
decltype(value)> {}; });
288 void Reset() noexcept
290 Visit([](
auto& value) { value = std::remove_cvref_t<
decltype(value)> {}; });
294 [[nodiscard]] SqlDriverId DriverId() const noexcept
296 static constexpr auto ids = std::array<SqlDriverId,
sizeof...(Drivers)> { Drivers::Id... };
297 return ids[m_storage.index()];
303 template <
typename F>
304 [[nodiscard]]
decltype(
auto) Visit(F&& f)
306 return Dispatch<0>(m_storage, std::forward<F>(f));
310 template <
typename F>
311 [[nodiscard]]
decltype(
auto) Visit(F&& f)
const
313 return Dispatch<0>(m_storage, std::forward<F>(f));
319 template <std::
size_t I,
typename S,
typename F>
320 static decltype(
auto) Dispatch(S& storage, F&& f)
322 if constexpr (I + 1 ==
sizeof...(Drivers))
323 return std::invoke(std::forward<F>(f), *std::get_if<I>(&storage));
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));
332 Storage m_storage {};
336 template <
typename Role,
typename List>
339 template <
typename Role, SqlDriver... Drivers>
340 struct HandleFor<Role, DriverList<Drivers...>>
342 static_assert(
sizeof...(Drivers) > 0,
"The library must be built with at least one driver.");
343 using Type = BasicHandle<Role, Drivers...>;
353template <
template <
typename>
typename Trait>
354inline constexpr bool AnyEnabledStatement = detail::AnyStatementOf<Trait>(detail::EnabledDrivers {});
359using StatementHandle = detail::HandleFor<detail::StatementRole, detail::EnabledDrivers>::Type;
364using ConnectionHandle = detail::HandleFor<detail::ConnectionRole, detail::EnabledDrivers>::Type;
371inline LIGHTWEIGHT_FORCE_INLINE
decltype(
auto) Visit(StatementHandle& handle, F&& f)
373 return handle.Visit(std::forward<F>(f));
378inline LIGHTWEIGHT_FORCE_INLINE
decltype(
auto) Visit(StatementHandle
const& handle, F&& f)
380 return handle.Visit(std::forward<F>(f));
385inline LIGHTWEIGHT_FORCE_INLINE
decltype(
auto) Visit(ConnectionHandle& handle, F&& f)
387 return handle.Visit(std::forward<F>(f));
392inline LIGHTWEIGHT_FORCE_INLINE
decltype(
auto) Visit(ConnectionHandle
const& handle, F&& f)
394 return handle.Visit(std::forward<F>(f));