1#ifndef ZSERIO_SQLITE_UTIL_H_INC
2#define ZSERIO_SQLITE_UTIL_H_INC
24template <
typename T,
typename =
void>
28struct ColumnTraits<T,
std::enable_if_t<is_complete_v<View<T>>>>
30 static constexpr std::string_view TYPE_NAME =
"BLOB";
31 static constexpr int TYPE = SQLITE_BLOB;
35struct ColumnTraits<T,
std::enable_if_t<std::is_enum_v<T> || is_bitmask_v<T>>>
37 static constexpr std::string_view TYPE_NAME =
"INTEGER";
38 static constexpr int TYPE = SQLITE_INTEGER;
42struct ColumnTraits<T,
std::enable_if_t<is_numeric_wrapper_v<T> && std::is_integral_v<typename T::ValueType>>>
44 static constexpr std::string_view TYPE_NAME =
"INTEGER";
45 static constexpr int TYPE = SQLITE_INTEGER;
50 std::enable_if_t<is_numeric_wrapper_v<T> && std::is_floating_point_v<typename T::ValueType>>>
52 static constexpr std::string_view TYPE_NAME =
"REAL";
53 static constexpr int TYPE = SQLITE_FLOAT;
56template <
typename ALLOC>
59 static constexpr std::string_view TYPE_NAME =
"TEXT";
60 static constexpr int TYPE = SQLITE_TEXT;
63template <
typename ALLOC,
typename T,
typename... ARGS>
64std::enable_if_t<is_complete_v<View<T>>> readColumn(
65 BasicOptional<ALLOC, T>& column, sqlite3_stmt& stmt,
int index,
const ARGS&... args)
68 const void* blobDataPtr = sqlite3_column_blob(&stmt, index);
69 const int blobDataLength = sqlite3_column_bytes(&stmt, index);
70 Span<const uint8_t> blobData(
static_cast<const uint8_t*
>(blobDataPtr),
static_cast<size_t>(blobDataLength));
75template <
typename ALLOC,
typename T>
76std::enable_if_t<std::is_enum_v<T>> readColumn(BasicOptional<ALLOC, T>& column, sqlite3_stmt& stmt,
int index)
78 const int64_t intValue = sqlite3_column_int64(&stmt, index);
79 column = valueToEnum<T>(
static_cast<std::underlying_type_t<T>
>(intValue));
82template <
typename ALLOC,
typename T>
83std::enable_if_t<is_bitmask_v<T>> readColumn(BasicOptional<ALLOC, T>& column, sqlite3_stmt& stmt,
int index)
85 const int64_t intValue = sqlite3_column_int64(&stmt, index);
86 column = T(
static_cast<typename T::ZserioType::ValueType
>(intValue));
89template <
typename ALLOC>
90void readColumn(BasicOptional<ALLOC, Bool>& column, sqlite3_stmt& stmt,
int index)
92 const int64_t intValue = sqlite3_column_int64(&stmt, index);
93 column = intValue != 0;
96template <
typename ALLOC, BitSize BIT_SIZE,
bool IS_SIGNED>
98 BasicOptional<ALLOC, FixedIntWrapper<BIT_SIZE, IS_SIGNED>>& column, sqlite3_stmt& stmt,
int index)
100 const int64_t intValue = sqlite3_column_int64(&stmt, index);
101 column =
static_cast<typename FixedIntWrapper<BIT_SIZE, IS_SIGNED>::ValueType
>(intValue);
104template <
typename ALLOC,
typename T>
105void readColumn(BasicOptional<ALLOC, DynIntWrapper<T>>& column, sqlite3_stmt& stmt,
int index)
107 const int64_t intValue = sqlite3_column_int64(&stmt, index);
108 column =
static_cast<T
>(intValue);
111template <
typename ALLOC,
typename VALUE_TYPE, VarIntType VAR_TYPE>
113 BasicOptional<ALLOC, VarIntWrapper<VALUE_TYPE, VAR_TYPE>>& column, sqlite3_stmt& stmt,
int index)
115 const int64_t intValue = sqlite3_column_int64(&stmt, index);
116 column =
static_cast<VALUE_TYPE
>(intValue);
119template <
typename ALLOC,
typename VALUE_TYPE, FloatType FLOAT_TYPE>
121 BasicOptional<ALLOC, FloatWrapper<VALUE_TYPE, FLOAT_TYPE>>& column, sqlite3_stmt& stmt,
int index)
123 const double doubleValue = sqlite3_column_double(&stmt, index);
124 column =
static_cast<VALUE_TYPE
>(doubleValue);
127template <
typename ALLOC>
129 BasicOptional<ALLOC,
BasicString<RebindAlloc<ALLOC, char>>>& column, sqlite3_stmt& stmt,
int index)
131 const unsigned char* textValue = sqlite3_column_text(&stmt, index);
132 column.emplace(
reinterpret_cast<const char*
>(textValue));
136BitSize prepareColumn(
const View<T>& view)
139 return initializeOffsets(view, 0);
148template <
typename ALLOC,
typename T>
149int bindColumn(sqlite3_stmt& stmt,
int index,
const View<T>& view,
BitSize bitSize,
const ALLOC& allocator)
151 BasicBitBuffer<ALLOC> bitBuffer(bitSize, allocator);
152 BitStreamWriter writer(bitBuffer);
154 return sqlite3_bind_blob(
155 &stmt, index, bitBuffer.getBuffer(),
static_cast<int>(bitBuffer.getByteSize()), SQLITE_TRANSIENT);
158template <
typename ALLOC,
typename T>
159std::enable_if_t<std::is_enum_v<T>,
int> bindColumn(
160 sqlite3_stmt& stmt,
int index, T value,
BitSize,
const ALLOC&)
162 return sqlite3_bind_int64(&stmt, index,
static_cast<int64_t
>(value));
165template <
typename ALLOC,
typename T>
166std::enable_if_t<is_bitmask_v<T>,
int> bindColumn(sqlite3_stmt& stmt,
int index, T value,
BitSize,
const ALLOC&)
168 return sqlite3_bind_int64(&stmt, index,
static_cast<int64_t
>(value.getValue()));
171template <
typename ALLOC>
172int bindColumn(sqlite3_stmt& stmt,
int index,
Bool value,
BitSize,
const ALLOC&)
174 return sqlite3_bind_int64(&stmt, index,
static_cast<int64_t
>(value));
177template <
typename ALLOC, BitSize BIT_SIZE,
bool IS_SIGNED>
178int bindColumn(sqlite3_stmt& stmt,
int index, FixedIntWrapper<BIT_SIZE, IS_SIGNED> value,
BitSize,
const ALLOC&)
180 return sqlite3_bind_int64(&stmt, index,
static_cast<int64_t
>(value));
183template <
typename ALLOC,
typename T>
184int bindColumn(sqlite3_stmt& stmt, DynIntWrapper<T> value,
BitSize,
int index,
const ALLOC&)
186 return sqlite3_bind_int64(&stmt, index,
static_cast<int64_t
>(value));
189template <
typename ALLOC,
typename VALUE_TYPE, VarIntType VAR_TYPE>
190int bindColumn(sqlite3_stmt& stmt,
int index, VarIntWrapper<VALUE_TYPE, VAR_TYPE> value,
BitSize,
const ALLOC&)
192 return sqlite3_bind_int64(&stmt, index,
static_cast<int64_t
>(value));
195template <
typename ALLOC,
typename VALUE_TYPE, FloatType FLOAT_TYPE>
196int bindColumn(sqlite3_stmt& stmt,
int index, FloatWrapper<VALUE_TYPE, FLOAT_TYPE> value,
BitSize,
const ALLOC&)
198 return sqlite3_bind_double(&stmt, index,
static_cast<double>(value));
201template <
typename ALLOC>
202int bindColumn(sqlite3_stmt& stmt,
int index, std::string_view value,
BitSize,
const ALLOC&)
204 return sqlite3_bind_text(&stmt, index, value.data(),
static_cast<int>(value.size()), SQLITE_TRANSIENT);
std::basic_string< char, std::char_traits< char >, ALLOC > BasicString
View< T > deserializeFromBytes(Span< const uint8_t > buffer, ArrayPreallocation maxArrayPrealloc, T &data, ARGS &&... arguments)