Skip to content

Commit 4375928

Browse files
authored
Merge pull request #105 from zauguin/integer
Support all integer types
2 parents 1c92215 + e317be6 commit 4375928

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

hdr/sqlite_modern_cpp.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,6 @@ namespace sqlite {
285285
#endif
286286

287287

288-
template<typename T> friend database_binder& operator <<(database_binder& db, const T& val);
289-
template<typename T> friend void get_col_from_db(database_binder& db, int inx, T& val);
290288
/* for vector<T, A> support */
291289
template<typename T, typename A> friend database_binder& operator <<(database_binder& db, const std::vector<T, A>& val);
292290
template<typename T, typename A> friend void get_col_from_db(database_binder& db, int inx, std::vector<T, A>& val);
@@ -848,6 +846,29 @@ namespace sqlite {
848846
inline void store_result_in_db(sqlite3_context* db, const std::u16string& val) {
849847
sqlite3_result_text16(db, val.data(), -1, SQLITE_TRANSIENT);
850848
}
849+
850+
// Other integer types
851+
template<class Integral, class = typename std::enable_if<std::is_integral<Integral>::value>::type>
852+
inline database_binder& operator <<(database_binder& db, const Integral& val) {
853+
return db << static_cast<sqlite3_int64>(val);
854+
}
855+
template<class Integral, class = std::enable_if<std::is_integral<Integral>::type>>
856+
inline void store_result_in_db(sqlite3_context* db, const Integral& val) {
857+
store_result_in_db(db, static_cast<sqlite3_int64>(val));
858+
}
859+
template<class Integral, class = typename std::enable_if<std::is_integral<Integral>::value>::type>
860+
inline void get_col_from_db(database_binder& db, int inx, Integral& val) {
861+
sqlite3_int64 i;
862+
get_col_from_db(db, inx, i);
863+
val = i;
864+
}
865+
template<class Integral, class = typename std::enable_if<std::is_integral<Integral>::value>::type>
866+
inline void get_val_from_db(sqlite3_value *value, Integral& val) {
867+
sqlite3_int64 i;
868+
get_val_from_db(value, i);
869+
val = i;
870+
}
871+
851872
// std::optional support for NULL values
852873
#ifdef MODERN_SQLITE_STD_OPTIONAL_SUPPORT
853874
template <typename OptionalT> inline database_binder& operator <<(database_binder& db, const std::optional<OptionalT>& val) {

tests/simple_examples.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int main()
2929
db << "INSERT INTO foo VALUES (?, ?)" << 2 << "world";
3030

3131
string str;
32-
db << "SELECT b from FOO where a=?;" << 2 >> str;
32+
db << "SELECT b from FOO where a=?;" << 2L >> str;
3333

3434
if(str != "world")
3535
{
@@ -38,7 +38,7 @@ int main()
3838
}
3939

4040
std::string sql("select 1+1");
41-
int test = 0;
41+
long test = 0;
4242
db << sql >> test;
4343

4444
if(test != 2) exit(EXIT_FAILURE);

0 commit comments

Comments
 (0)