1+ #include < iostream>
2+ #include < string>
3+ #include < cstdlib>
4+ #include < unistd.h>
5+ #include < sqlite_modern_cpp.h>
6+
7+ using namespace sqlite ;
8+ using std::string;
9+
10+ struct TmpFile
11+ {
12+ string fname;
13+
14+ TmpFile ()
15+ {
16+ char f[]=" /tmp/sqlite_modern_cpp_test_XXXXXX" ;
17+ int fid = mkstemp (f);
18+ close (fid);
19+
20+ fname = f;
21+ }
22+
23+ ~TmpFile ()
24+ {
25+ unlink (fname.c_str ());
26+ }
27+ };
28+
29+
30+ class DBInterface {
31+ database db;
32+
33+ public:
34+ DBInterface ( const string& fileName ) : db( fileName )
35+ {
36+ }
37+
38+ void LogRequest ( const string& username, const string& ip, const string& request )
39+ {
40+ try {
41+ auto timestamp = std::to_string ( time ( nullptr ) );
42+
43+ db <<
44+ " create table if not exists log_request ("
45+ " _id integer primary key autoincrement not null,"
46+ " username text,"
47+ " timestamp text,"
48+ " ip text,"
49+ " request text"
50+ " );" ;
51+ db << " INSERT INTO log_request (username, timestamp, ip, request) VALUES (?,?,?,?);"
52+ << username
53+ << timestamp
54+ << ip
55+ << request;
56+ } catch ( const std::exception& e ) {
57+ std::cout << e.what () << std::endl;
58+ }
59+ }
60+
61+ bool TestData ( void ) {
62+ try {
63+ string username, timestamp, ip, request;
64+
65+ db << " select username, timestamp, ip, request from log_request where username = ?"
66+ << " test"
67+ >> std::tie (username, timestamp, ip, request);
68+
69+ if ( username == " test" && ip == " 127.0.0.1" && request == " hello world" ) {
70+ return true ;
71+ }
72+ } catch ( const std::exception& e ) {
73+ std::cout << e.what () << std::endl;
74+ }
75+
76+ return false ;
77+ }
78+ };
79+
80+ int main ( void )
81+ {
82+ // --------------------------------------------------------------------------
83+ // -- Test if writing to disk works properly from within a catch block.
84+ // --------------------------------------------------------------------------
85+ try {
86+ throw " hello" ;
87+ }
88+ catch ( ... ) {
89+ TmpFile tmpF;
90+ DBInterface interf ( tmpF.fname );
91+ interf.LogRequest ( " test" , " 127.0.0.1" , " hello world" );
92+ if ( !interf.TestData () ) {
93+ exit ( EXIT_FAILURE );
94+ }
95+ }
96+
97+ exit ( EXIT_SUCCESS );
98+ }
0 commit comments