Skip to content

Commit b091c72

Browse files
committed
Added blob_t load/store
1 parent b6a1321 commit b091c72

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

hdr/sqlite_modern_cpp/type_wrapper.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "errors.h"
3737

3838
namespace sqlite {
39+
using blob_t = std::pair<void const*, int>;
40+
3941
template<class T, int Type, class = void>
4042
struct has_sqlite_type : std::false_type {};
4143

@@ -249,6 +251,31 @@ namespace sqlite {
249251
return std::vector<T, A>(buf, buf + bytes/sizeof(T));
250252
}
251253

254+
// blob_t
255+
template<>
256+
struct has_sqlite_type<blob_t, SQLITE_BLOB, void> : std::true_type {};
257+
258+
inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const blob_t& blob) {
259+
return sqlite3_bind_blob(stmt, inx, blob.first, blob.second, SQLITE_TRANSIENT);
260+
}
261+
inline void store_result_in_db(sqlite3_context* db, const blob_t& blob) {
262+
sqlite3_result_blob(db, blob.first, blob.second, SQLITE_TRANSIENT);
263+
}
264+
inline blob_t get_col_from_db(sqlite3_stmt* stmt, int inx, result_type<blob_t>) {
265+
if(sqlite3_column_type(stmt, inx) == SQLITE_NULL) {
266+
return blob_t(nullptr, 0);
267+
}
268+
int bytes = sqlite3_column_bytes(stmt, inx);
269+
return blob_t(sqlite3_column_blob(stmt, inx), bytes);
270+
}
271+
inline blob_t get_val_from_db(sqlite3_value *value, result_type<blob_t>) {
272+
if(sqlite3_value_type(value) == SQLITE_NULL) {
273+
return blob_t(nullptr, 0);
274+
}
275+
int bytes = sqlite3_value_bytes(value);
276+
return blob_t(sqlite3_value_blob(value), bytes);
277+
}
278+
252279
/* for unique_ptr<T> support */
253280
template<typename T, int Type>
254281
struct has_sqlite_type<std::unique_ptr<T>, Type, void> : has_sqlite_type<T, Type> {};

0 commit comments

Comments
 (0)