Skip to content

Commit 7840c71

Browse files
committed
Added a test. It seems to work
1 parent c14735f commit 7840c71

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

hdr/sqlite_modern_cpp/type_wrapper.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,21 @@ namespace sqlite {
167167
sqlite3_result_null(db);
168168
}
169169
#ifdef MODERN_SQLITE_STD_VARIANT_SUPPORT
170+
template<>
171+
struct has_sqlite_type<std::monostate, SQLITE_NULL, void> : std::true_type {};
172+
173+
inline std::monostate get_col_from_db(sqlite3_stmt* stmt, int inx, result_type<std::monostate>) {
174+
if(sqlite3_column_type(stmt,inx) != SQLITE_NULL) throw 1;
175+
return std::monostate();
176+
}
177+
inline std::monostate get_val_from_db(sqlite3_value *value, result_type<std::monostate>) {
178+
return std::monostate();
179+
}
180+
170181
inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, std::monostate) {
171182
return sqlite3_bind_null(stmt, inx);
172183
}
184+
173185
inline void store_result_in_db(sqlite3_context* db, std::monostate) {
174186
sqlite3_result_null(db);
175187
}

tests/variant.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,25 @@ TEST_CASE("std::variant works", "[variant]") {
2929
db << "SELECT 0.0" >> v;
3030
REQUIRE(std::get<2>(v));
3131
}
32+
TEST_CASE("std::monostate is a nullptr substitute", "[variant]"){
33+
database db(":memory:");
34+
db << "CREATE TABLE foo (a);";
35+
36+
std::variant<std::monostate,std::string> v;
37+
v=std::monostate();
38+
db << "INSERT INTO foo values(?)" << v;
39+
db << "INSERT INTO foo values(?)" << "This isn't a monostate!";
40+
bool found_null=false,
41+
found_string=false;
42+
db << "select * from foo" >> [&found_null,&found_string](std::variant<std::monostate,std::string> z){
43+
if(z.index()==0){
44+
found_null=true;
45+
}else{
46+
found_string=true;
47+
};
48+
};
49+
REQUIRE((found_null&&found_string));
50+
db << "SELECT NULL" >> v;
51+
REQUIRE(v.index()==0);
52+
}
3253
#endif

0 commit comments

Comments
 (0)