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