Skip to content

Commit

Permalink
Support all integer types
Browse files Browse the repository at this point in the history
  • Loading branch information
zauguin committed Apr 5, 2017
1 parent 1c92215 commit 20c98a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
25 changes: 23 additions & 2 deletions hdr/sqlite_modern_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,6 @@ namespace sqlite {
#endif


template<typename T> friend database_binder& operator <<(database_binder& db, const T& val);
template<typename T> friend void get_col_from_db(database_binder& db, int inx, T& val);
/* for vector<T, A> support */
template<typename T, typename A> friend database_binder& operator <<(database_binder& db, const std::vector<T, A>& val);
template<typename T, typename A> friend void get_col_from_db(database_binder& db, int inx, std::vector<T, A>& val);
Expand Down Expand Up @@ -848,6 +846,29 @@ namespace sqlite {
inline void store_result_in_db(sqlite3_context* db, const std::u16string& val) {
sqlite3_result_text16(db, val.data(), -1, SQLITE_TRANSIENT);
}

// Other integer types
template<class Integral, class = typename std::enable_if<std::is_integral<Integral>::value>::type>
inline database_binder& operator <<(database_binder& db, const Integral& val) {
return db << static_cast<sqlite3_int64>(val);
}
template<class Integral, class = std::enable_if<std::is_integral<Integral>::type>>
inline void store_result_in_db(sqlite3_context* db, const Integral& val) {
store_result_in_db(db, val);
}
template<class Integral, class = typename std::enable_if<std::is_integral<Integral>::value>::type>
inline void get_col_from_db(database_binder& db, int inx, Integral& val) {
sqlite3_int64 i;
get_col_from_db(db, inx, i);
val = i;
}
template<class Integral, class = typename std::enable_if<std::is_integral<Integral>::value>::type>
inline void get_val_from_db(sqlite3_value *value, Integral& val) {
sqlite3_int64 i;
get_val_from_db(value, i);
val = i;
}

// std::optional support for NULL values
#ifdef MODERN_SQLITE_STD_OPTIONAL_SUPPORT
template <typename OptionalT> inline database_binder& operator <<(database_binder& db, const std::optional<OptionalT>& val) {
Expand Down
4 changes: 2 additions & 2 deletions tests/simple_examples.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int main()
db << "INSERT INTO foo VALUES (?, ?)" << 2 << "world";

string str;
db << "SELECT b from FOO where a=?;" << 2 >> str;
db << "SELECT b from FOO where a=?;" << 2L >> str;

if(str != "world")
{
Expand All @@ -38,7 +38,7 @@ int main()
}

std::string sql("select 1+1");
int test = 0;
long test = 0;
db << sql >> test;

if(test != 2) exit(EXIT_FAILURE);
Expand Down

0 comments on commit 20c98a2

Please sign in to comment.