Skip to content

Added blob_t alternative #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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