-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from zauguin/std_optional
std::optional support
- Loading branch information
Showing
2 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <unistd.h> | ||
#include <iostream> | ||
|
||
#include <sqlite_modern_cpp.h> | ||
|
||
using namespace sqlite; | ||
using namespace std; | ||
|
||
#if __has_include(<optional>) | ||
void insert(database& db, bool is_null) { | ||
int id = 1; | ||
std::optional<int> val; | ||
if(!is_null) val = 5; | ||
|
||
db << "delete from test where id = 1"; | ||
db << "insert into test(id,val) values(?,?)" << id << val; | ||
} | ||
|
||
void select(database& db, bool should_be_null) { | ||
db << "select id,val from test" >> [&](long long, std::optional<int> val) { | ||
if(should_be_null) { | ||
if(val) exit(EXIT_FAILURE); | ||
} else { | ||
if(!val) exit(EXIT_FAILURE); | ||
} | ||
}; | ||
} | ||
|
||
struct TmpFile { | ||
string fname; | ||
|
||
TmpFile() { | ||
char f[] = "/tmp/sqlite_modern_cpp_test_XXXXXX"; | ||
int fid = mkstemp(f); | ||
close(fid); | ||
|
||
fname = f; | ||
} | ||
|
||
~TmpFile() { | ||
unlink(fname.c_str()); | ||
} | ||
}; | ||
|
||
int main() { | ||
try { | ||
// creates a database file 'dbfile.db' if it does not exists. | ||
TmpFile file; | ||
database db(file.fname); | ||
|
||
db << "drop table if exists test"; | ||
db << | ||
"create table if not exists test (" | ||
" id integer primary key," | ||
" val int" | ||
");"; | ||
|
||
insert(db, true); | ||
select(db, true); | ||
|
||
insert(db, false); | ||
select(db, false); | ||
|
||
} catch(exception& e) { | ||
cout << e.what() << endl; | ||
exit(EXIT_FAILURE); | ||
} | ||
exit(EXIT_SUCCESS); | ||
} | ||
#else | ||
#pragma message "<optional> not found, test disabled." | ||
int main() { | ||
exit(EXIT_SUCCESS); | ||
} | ||
#endif |