Skip to content

Commit

Permalink
Added blob_t load/store
Browse files Browse the repository at this point in the history
  • Loading branch information
roussosalex committed Mar 21, 2018
1 parent b6a1321 commit 5922145
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions hdr/sqlite_modern_cpp/type_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "errors.h"

namespace sqlite {
using blob_t = std::pair<void const*, int>;

template<class T, int Type, class = void>
struct has_sqlite_type : std::false_type {};

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

// blob_t
template<>
struct has_sqlite_type<blob_t, SQLITE_BLOB, void> : std::true_type {};

inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const blob_t& blob) {
return sqlite3_bind_blob(stmt, inx, blob.first, blob.second, SQLITE_TRANSIENT);
}
inline void store_result_in_db(sqlite3_context* db, const blob_t& blob) {
sqlite3_result_blob(db, blob.first, blob.second, SQLITE_TRANSIENT);
}
inline blob_t get_col_from_db(sqlite3_stmt* stmt, int inx, result_type<blob_t>) {
if(sqlite3_column_type(stmt, inx) == SQLITE_NULL) {
return blob_t(nullptr, 0);
}
int bytes = sqlite3_column_bytes(stmt, inx);
return blob_t(sqlite3_column_blob(stmt, inx), bytes);
}
inline blob_t get_val_from_db(sqlite3_value *value, result_type<blob_t>) {
if(sqlite3_value_type(value) == SQLITE_NULL) {
return blob_t(nullptr, 0);
}
int bytes = sqlite3_value_bytes(value);
return blob_t(sqlite3_value_blob(value), bytes);
}

/* for unique_ptr<T> support */
template<typename T, int Type>
struct has_sqlite_type<std::unique_ptr<T>, Type, void> : has_sqlite_type<T, Type> {};
Expand Down

0 comments on commit 5922145

Please sign in to comment.