6#include "DataBinder/UnicodeConverter.hpp"
7#include "SqlOdbcPrelude.hpp"
19namespace Lightweight::detail
25static_assert(
sizeof(SQLWCHAR) ==
sizeof(
char16_t),
"ODBC W variants require a 16-bit code unit; SQLWCHAR shape mismatch");
31inline std::u16string OdbcUtf8ToUtf16(std::string_view utf8)
33 return ToUtf16(std::u8string_view {
reinterpret_cast<char8_t const*
>(utf8.data()), utf8.size() });
39inline SQLWCHAR* AsSqlWChar(
char16_t* p)
noexcept
41 return reinterpret_cast<SQLWCHAR*
>(p);
51 std::u16string buffer;
53 explicit OdbcWideArg(std::string_view utf8):
54 buffer(utf8.empty() ? std::u16string {} : OdbcUtf8ToUtf16(utf8))
58 explicit OdbcWideArg(std::u16string utf16)
noexcept:
59 buffer(std::move(utf16))
63 [[nodiscard]] SQLWCHAR* data() noexcept
65 return buffer.empty() ? nullptr : AsSqlWChar(buffer.data());
68 [[nodiscard]] SQLSMALLINT length() const noexcept
70 return static_cast<SQLSMALLINT
>(buffer.size());
81 explicit OdbcNarrowArg(std::string_view utf8):
86 [[nodiscard]] SQLCHAR* data() noexcept
88 return buffer.empty() ? nullptr :
reinterpret_cast<SQLCHAR*
>(buffer.data());
91 [[nodiscard]] SQLSMALLINT length() const noexcept
93 return static_cast<SQLSMALLINT
>(buffer.size());
105template <
typename Cursor>
106std::optional<std::string> GetNullableTextColumn(Cursor
const& cursor, SQLUSMALLINT column)
108 auto const text = cursor.template GetNullableColumn<std::u16string>(column);
111 auto const utf8 =
ToUtf8(std::u16string_view { *text });
112 return std::string {
reinterpret_cast<char const*
>(utf8.data()), utf8.size() };
116template <
typename Cursor>
117std::string GetTextColumn(Cursor
const& cursor, SQLUSMALLINT column)
119 return GetNullableTextColumn(cursor, column).value_or(std::string {});
127void RegisterAnsiOnlyHandle(SQLHANDLE handle, SQLHDBC connection)
noexcept;
131void UnregisterAnsiOnlyHandle(SQLHANDLE handle)
noexcept;
134[[nodiscard]]
bool IsAnsiOnlyHandle(SQLHANDLE handle)
noexcept;
139template <
typename Call,
typename... Texts>
140decltype(
auto) CallWithOdbcText(
bool wide, Call
const& call, Texts
const&... texts)
144 auto args = std::tuple { OdbcWideArg { std::string_view { texts } }... };
145 return std::apply(call, args);
147 auto args = std::tuple { OdbcNarrowArg { std::string_view { texts } }... };
148 return std::apply(call, args);
153template <
typename Arg,
typename W
ideFunction,
typename AnsiFunction>
154[[nodiscard]]
constexpr auto OdbcEntryPoint(WideFunction* wideFunction, AnsiFunction* ansiFunction)
noexcept
156 if constexpr (std::same_as<std::remove_cvref_t<Arg>, OdbcWideArg>)
LIGHTWEIGHT_API std::u8string ToUtf8(std::u32string_view u32InputString)
std::u16string ToUtf16(std::basic_string_view< T > const u32InputString)