Skip to content

Commit 767499e

Browse files
authored
Merge pull request #124 from aminroosta/tmpnam
remove references to "tmpnam"
2 parents 82e3c2a + c498cb8 commit 767499e

File tree

6 files changed

+91
-141
lines changed

6 files changed

+91
-141
lines changed

tests/boost_optional.cc

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,10 @@ void select(database& db, bool should_be_null) {
2626
};
2727
}
2828

29-
struct TmpFile {
30-
string fname;
31-
32-
TmpFile(): fname(tmpnam(nullptr)) {}
33-
34-
~TmpFile() {
35-
remove(fname.c_str());
36-
}
37-
};
38-
3929
int main() {
4030
try {
4131
// creates a database file 'dbfile.db' if it does not exists.
42-
TmpFile file;
43-
database db(file.fname);
32+
database db(":memory:");
4433

4534
db << "drop table if exists test";
4635
db <<

tests/flags.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ struct TmpFile
1010
{
1111
string fname;
1212

13-
TmpFile(): fname(tmpnam(nullptr)) {}
14-
15-
~TmpFile()
16-
{
17-
remove(fname.c_str());
18-
}
13+
TmpFile(): fname("./flags.db") { }
14+
~TmpFile() { remove(fname.c_str()); }
1915
};
2016

2117
#if __BYTE_ORDER == __BIG_ENDIAN

tests/shared_connection.cc

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,10 @@
66
using namespace sqlite;
77
using namespace std;
88

9-
struct TmpFile {
10-
string fname;
11-
12-
TmpFile(): fname(tmpnam(nullptr)) {}
13-
14-
~TmpFile() {
15-
remove(fname.c_str());
16-
}
17-
};
18-
199
int main() {
2010
try {
2111

22-
TmpFile file;
23-
database db(file.fname);
12+
database db(":memory:");
2413

2514
{
2615

tests/simple_examples.cc

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,11 @@
55
using namespace sqlite;
66
using namespace std;
77

8-
struct TmpFile
9-
{
10-
string fname;
11-
12-
TmpFile(): fname(tmpnam(nullptr)) {}
13-
14-
~TmpFile()
15-
{
16-
remove(fname.c_str());
17-
}
18-
};
19-
208
int main()
219
{
2210
try
2311
{
24-
TmpFile file;
25-
database db(file.fname);
12+
database db(":memory:");
2613

2714
db << "CREATE TABLE foo (a integer, b string);";
2815
db << "INSERT INTO foo VALUES (?, ?)" << 1 << "hello";

tests/sqlcipher.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ struct TmpFile
1010
{
1111
string fname;
1212

13-
TmpFile(): fname(tmpnam(nullptr)) {}
14-
15-
~TmpFile()
16-
{
17-
remove(fname.c_str());
18-
}
13+
TmpFile(): fname("./sqlcipher.db") { }
14+
~TmpFile() { remove(fname.c_str()); }
1915
};
2016

2117
int main()

tests/trycatchblocks.cc

Lines changed: 84 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,84 @@
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

Comments
 (0)