Skip to content

Commit 69388a0

Browse files
committed
support nullptr and uniqueptr
1 parent 67d00e8 commit 69388a0

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

hdr/sqlite_modern_cpp.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,12 @@ namespace sqlite {
210210

211211
template<typename T> friend database_binder::chain_type& operator <<(database_binder::chain_type& db, const T& val);
212212
template<typename T> friend void get_col_from_db(database_binder& db, int inx, T& val);
213+
/* for vector<T> support */
213214
template<typename T> friend database_binder::chain_type& operator <<(database_binder::chain_type& db, const std::vector<T>& val);
214215
template<typename T> friend void get_col_from_db(database_binder& db, int inx, std::vector<T>& val);
216+
/* for nullptr & unique_ptr support */
217+
friend database_binder::chain_type& operator <<(database_binder::chain_type& db, std::nullptr_t);
218+
template<typename T> friend void get_col_from_db(database_binder& db, int inx, std::unique_ptr<T>& val);
215219
template<typename T> friend T operator++(database_binder& db, int);
216220

217221

@@ -458,6 +462,27 @@ namespace sqlite {
458462
}
459463
}
460464

465+
/* for nullptr support */
466+
inline database_binder::chain_type& operator <<(database_binder::chain_type& db, std::nullptr_t) {
467+
int hresult;
468+
if((hresult = sqlite3_bind_null(db->_stmt.get(), db->_inx)) != SQLITE_OK) {
469+
exceptions::throw_sqlite_error(hresult);
470+
}
471+
++db->_inx;
472+
return db;
473+
}
474+
475+
/* for unique_ptr<T> support */
476+
template<typename T> inline void get_col_from_db(database_binder& db, int inx, std::unique_ptr<T>& _ptr_) {
477+
if(sqlite3_column_type(db._stmt.get(), inx) == SQLITE_NULL) {
478+
_ptr_ = nullptr;
479+
} else {
480+
auto underling_ptr = new T();
481+
get_col_from_db(db, inx, *underling_ptr);
482+
_ptr_.reset(underling_ptr);
483+
}
484+
}
485+
461486
// std::string
462487
template<> inline void get_col_from_db(database_binder& db, int inx, std::string & s) {
463488
if(sqlite3_column_type(db._stmt.get(), inx) == SQLITE_NULL) {

tests/nullptr_uniqueptr.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
#include <sqlite_modern_cpp.h>
5+
using namespace std;
6+
using namespace sqlite;
7+
8+
int main() {
9+
10+
try {
11+
database db(":memory:");
12+
db << "CREATE TABLE tbl (id integer,age integer, name string, img blob);";
13+
db << "INSERT INTO tbl VALUES (?, ?, ?, ?);" << 1 << 24 << "bob" << vector<int> { 1, 2 , 3};
14+
db << "INSERT INTO tbl VALUES (?, ?, ?, ?);" << 2 << nullptr << nullptr << nullptr;
15+
16+
db << "select age,name,img from tbl where id = 1" >> [](unique_ptr<int> age_p, unique_ptr<string> name_p, unique_ptr<vector<int>> img_p) {
17+
if(age_p == nullptr || name_p == nullptr || img_p == nullptr) {
18+
cerr << "ERROR: values should not be null" << std::endl;
19+
exit(EXIT_FAILURE);
20+
}
21+
22+
cout << "age:" << *age_p << " name:" << *name_p << " img:";
23+
for(auto i : *img_p) cout << i << ","; cout << endl;
24+
};
25+
26+
db << "select age,name,img from tbl where id = 2" >> [](unique_ptr<int> age_p, unique_ptr<string> name_p, unique_ptr<vector<int>> img_p) {
27+
if(age_p != nullptr || name_p != nullptr || img_p != nullptr) {
28+
cerr << "ERROR: values should be nullptr" << std::endl;
29+
exit(EXIT_FAILURE);
30+
}
31+
32+
cout << "OK all three values are nullptr" << endl;
33+
};
34+
35+
} catch(sqlite_exception e) {
36+
cout << "Sqlite error " << e.what() << endl;
37+
exit(EXIT_FAILURE);
38+
} catch(...) {
39+
cout << "Unknown error\n";
40+
exit(EXIT_FAILURE);
41+
}
42+
43+
cout << "OK\n";
44+
exit(EXIT_SUCCESS);
45+
return 0;
46+
}

0 commit comments

Comments
 (0)