1 #ifndef ZSERIO_SQLITE_UTIL_H_INC
2 #define ZSERIO_SQLITE_UTIL_H_INC
24 template <
typename T,
typename =
void>
28 struct 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;
35 struct 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;
42 struct 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;
49 struct ColumnTraits<T,
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;
56 template <
typename ALLOC>
59 static constexpr std::string_view TYPE_NAME =
"TEXT";
60 static constexpr
int TYPE = SQLITE_TEXT;
63 template <
typename ALLOC,
typename T,
typename... ARGS>
64 std::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));
75 template <
typename ALLOC,
typename T>
76 std::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));
82 template <
typename ALLOC,
typename T>
83 std::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));
89 template <
typename ALLOC>
90 void readColumn(BasicOptional<ALLOC, Bool>& column, sqlite3_stmt& stmt,
int index)
92 const int64_t intValue = sqlite3_column_int64(&stmt, index);
93 column = intValue != 0;
96 template <
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);
104 template <
typename ALLOC,
typename T>
105 void 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);
111 template <
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);
119 template <
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);
127 template <
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));
135 template <
typename T>
136 BitSize prepareColumn(
const View<T>& view)
139 return initializeOffsets(view, 0);
142 template <
typename T>
143 BitSize prepareColumn(
const T&)
148 template <
typename ALLOC,
typename T>
149 int 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);
158 template <
typename ALLOC,
typename T>
159 std::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));
165 template <
typename ALLOC,
typename T>
166 std::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()));
171 template <
typename ALLOC>
172 int bindColumn(sqlite3_stmt& stmt,
int index,
Bool value,
BitSize,
const ALLOC&)
174 return sqlite3_bind_int64(&stmt, index,
static_cast<int64_t
>(value));
177 template <
typename ALLOC, BitSize BIT_SIZE,
bool IS_SIGNED>
178 int 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));
183 template <
typename ALLOC,
typename T>
184 int bindColumn(sqlite3_stmt& stmt, DynIntWrapper<T> value,
BitSize,
int index,
const ALLOC&)
186 return sqlite3_bind_int64(&stmt, index,
static_cast<int64_t
>(value));
189 template <
typename ALLOC,
typename VALUE_TYPE, VarIntType VAR_TYPE>
190 int 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));
195 template <
typename ALLOC,
typename VALUE_TYPE, FloatType FLOAT_TYPE>
196 int 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));
201 template <
typename ALLOC>
202 int 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);
View< T > deserializeFromBytes(Span< const uint8_t > buffer, T &data, ARGS &&... arguments)
std::basic_string< char, std::char_traits< char >, ALLOC > BasicString