Lightweight 0.1.0
Loading...
Searching...
No Matches
StdStringView.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "Core.hpp"
6#include "UnicodeConverter.hpp"
7
8#include <concepts>
9#include <memory>
10#include <string_view>
11
12template <>
13struct SqlDataBinder<std::basic_string_view<char>>
14{
15 static constexpr SQLSMALLINT CType = SQL_C_CHAR;
16 static constexpr SQLSMALLINT SqlType = SQL_VARCHAR;
17
18 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
19 SQLUSMALLINT column,
20 std::basic_string_view<char> value,
21 SqlDataBinderCallback& /*cb*/) noexcept
22 {
23 return SQLBindParameter(
24 stmt, column, SQL_PARAM_INPUT, CType, SqlType, value.size(), 0, (SQLPOINTER) value.data(), 0, nullptr);
25 }
26
27 static LIGHTWEIGHT_FORCE_INLINE std::string_view Inspect(std::basic_string_view<char> value) noexcept
28 {
29 return { value.data(), value.size() };
30 }
31};
32
33template <typename Char16Type>
34 requires std::same_as<Char16Type, char16_t> || (std::same_as<Char16Type, wchar_t> && sizeof(wchar_t) == 2)
35struct SqlDataBinder<std::basic_string_view<Char16Type>>
36{
37 static constexpr SQLSMALLINT CType = SQL_C_WCHAR;
38 static constexpr SQLSMALLINT SqlType = SQL_WVARCHAR;
39
40 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
41 SQLUSMALLINT column,
42 std::basic_string_view<Char16Type> value,
43 SqlDataBinderCallback& /*cb*/) noexcept
44 {
45 return SQLBindParameter(
46 stmt, column, SQL_PARAM_INPUT, CType, SqlType, value.size(), 0, (SQLPOINTER) value.data(), 0, nullptr);
47 }
48
49 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(std::basic_string_view<Char16Type> value) noexcept
50 {
51 auto u8String = ToUtf8(value);
52 return std::string((char const*) u8String.data(), u8String.size());
53 }
54};
55
56template <typename Char32Type>
57 requires std::same_as<Char32Type, char32_t> || (std::same_as<Char32Type, wchar_t> && sizeof(wchar_t) == 4)
58struct SqlDataBinder<std::basic_string_view<Char32Type>>
59{
60 static constexpr SQLSMALLINT CType = SQL_C_WCHAR;
61 static constexpr SQLSMALLINT SqlType = SQL_WVARCHAR;
62
63 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
64 SQLUSMALLINT column,
65 std::basic_string_view<Char32Type> value,
66 SqlDataBinderCallback& cb) noexcept
67 {
68 auto u16String = std::make_shared<std::u16string>(ToUtf16(value));
69 cb.PlanPostExecuteCallback([u16String = u16String]() {}); // Keep the string alive
70 auto const* data = u16String->data();
71 auto const sizeInBytes = u16String->size() * sizeof(Char32Type);
72 return SQLBindParameter(
73 stmt, column, SQL_PARAM_INPUT, CType, SqlType, sizeInBytes, 0, (SQLPOINTER) data, 0, nullptr);
74 }
75
76 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(std::basic_string_view<Char32Type> value) noexcept
77 {
78 auto u8String = ToUtf8(value);
79 return std::string((char const*) u8String.data(), u8String.size());
80 }
81};
82
83template <>
84struct SqlDataBinder<std::u8string_view>
85{
86 static constexpr SQLSMALLINT CType = SQL_C_WCHAR;
87 static constexpr SQLSMALLINT SqlType = SQL_WVARCHAR;
88
89 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
90 SQLUSMALLINT column,
91 std::u8string_view value,
92 SqlDataBinderCallback& cb) noexcept
93 {
94 auto u16String = std::make_shared<std::u16string>(ToUtf16(value));
95 cb.PlanPostExecuteCallback([u16String = u16String]() {}); // Keep the string alive
96 auto const* data = u16String->data();
97 auto const sizeInBytes = u16String->size() * sizeof(char8_t);
98 return SQLBindParameter(
99 stmt, column, SQL_PARAM_INPUT, CType, SqlType, sizeInBytes, 0, (SQLPOINTER) data, 0, nullptr);
100 }
101
102 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(std::basic_string_view<char8_t> value) noexcept
103 {
104 return std::string { (char const*) value.data(), value.size() };
105 }
106};
107
std::u16string ToUtf16(const std::basic_string_view< T > u32InputString)
LIGHTWEIGHT_API std::u8string ToUtf8(std::u32string_view u32InputString)