forked from SqliteModernCpp/sqlite_modern_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrycatchblocks.cc
More file actions
80 lines (67 loc) · 2.26 KB
/
trycatchblocks.cc
File metadata and controls
80 lines (67 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sqlite_modern_cpp.h>
#include <catch2/catch.hpp>
using namespace sqlite;
using std::string;
struct TmpFile {
string fname;
TmpFile(): fname("./trycatchblocks.db") {}
~TmpFile() { remove(fname.c_str()); }
};
class DBInterface {
database db;
public:
DBInterface( const string& fileName ) : db( fileName ) { }
void LogRequest( const string& username, const string& ip, const string& request )
{
try {
auto timestamp = std::to_string( time( nullptr ) );
db <<
"create table if not exists log_request ("
" _id integer primary key autoincrement not null,"
" username text,"
" timestamp text,"
" ip text,"
" request text"
");";
db << "INSERT INTO log_request (username, timestamp, ip, request) VALUES (?,?,?,?);"
<< username
<< timestamp
<< ip
<< request;
} catch ( const std::exception& e ) {
std::cout << e.what() << std::endl;
}
}
bool TestData( void ) {
try {
string username, timestamp, ip, request;
db << "select username, timestamp, ip, request from log_request where username = ?"
<< "test"
>> std::tie(username, timestamp, ip, request);
if ( username == "test" && ip == "127.0.0.1" && request == "hello world" ) {
return true;
}
} catch ( const std::exception& e ) {
std::cout << e.what() << std::endl;
}
return false;
}
};
TEST_CASE("try catch blocks", "[trycatchblocks]") {
// --------------------------------------------------------------------------
// -- Test if writing to disk works properly from within a catch block.
// --------------------------------------------------------------------------
try {
throw "hello";
}
catch ( ... ) {
TmpFile tmpF;
DBInterface interf(tmpF.fname);
interf.LogRequest( "test", "127.0.0.1", "hello world" );
REQUIRE(interf.TestData() == true);
}
}