Skip to content

Commit e537195

Browse files
committed
Fix #47 setBusyTimeout in constructor
- add corresponding Unit Test
1 parent 3c39f1f commit e537195

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

include/SQLiteCpp/Database.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ class Database
5555
*
5656
* @param[in] apFilename UTF-8 path/uri to the database file ("filename" sqlite3 parameter)
5757
* @param[in] aFlags SQLITE_OPEN_READONLY/SQLITE_OPEN_READWRITE/SQLITE_OPEN_CREATE...
58+
* @param[in] aTimeoutMs Amount of milliseconds to wait before returning SQLITE_BUSY (see setBusyTimeout())
5859
* @param[in] apVfs UTF-8 name of custom VFS to use, or nullptr for sqlite3 default
5960
*
6061
* @throw SQLite::Exception in case of error
6162
*/
62-
Database(const char* apFilename, const int aFlags = SQLITE_OPEN_READONLY, const char* apVfs = NULL);
63+
Database(const char* apFilename,
64+
const int aFlags = SQLITE_OPEN_READONLY,
65+
const int aTimeoutMs = 0,
66+
const char* apVfs = NULL);
6367

6468
/**
6569
* @brief Open the provided database UTF-8 filename.
@@ -73,11 +77,15 @@ class Database
7377
*
7478
* @param[in] aFilename UTF-8 path/uri to the database file ("filename" sqlite3 parameter)
7579
* @param[in] aFlags SQLITE_OPEN_READONLY/SQLITE_OPEN_READWRITE/SQLITE_OPEN_CREATE...
80+
* @param[in] aTimeoutMs Amount of milliseconds to wait before returning SQLITE_BUSY (see setBusyTimeout())
7681
* @param[in] aVfs UTF-8 name of custom VFS to use, or empty string for sqlite3 default
7782
*
7883
* @throw SQLite::Exception in case of error
7984
*/
80-
Database(const std::string& aFilename, const int aFlags = SQLITE_OPEN_READONLY, const std::string& aVfs = "");
85+
Database(const std::string& aFilename,
86+
const int aFlags = SQLITE_OPEN_READONLY,
87+
const int aTimeoutMs = 0,
88+
const std::string& aVfs = "");
8189

8290
/**
8391
* @brief Close the SQLite database connection.

src/Database.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ namespace SQLite
2626

2727

2828
// Open the provided database UTF-8 filename with SQLITE_OPEN_xxx provided flags.
29-
Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/, const char* apVfs /*= NULL*/) :
29+
Database::Database(const char* apFilename,
30+
const int aFlags /* = SQLITE_OPEN_READONLY*/,
31+
const int aTimeoutMs /* = 0 */,
32+
const char* apVfs /* = NULL*/) :
3033
mpSQLite(NULL),
3134
mFilename(apFilename)
3235
{
@@ -37,10 +40,18 @@ Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READ
3740
sqlite3_close(mpSQLite); // close is required even in case of error on opening
3841
throw SQLite::Exception(strerr);
3942
}
43+
44+
if (aTimeoutMs > 0)
45+
{
46+
setBusyTimeout(aTimeoutMs);
47+
}
4048
}
4149

4250
// Open the provided database UTF-8 filename with SQLITE_OPEN_xxx provided flags.
43-
Database::Database(const std::string& aFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/, const std::string& aVfs) :
51+
Database::Database(const std::string& aFilename,
52+
const int aFlags /* = SQLITE_OPEN_READONLY*/,
53+
const int aTimeoutMs /* = 0 */,
54+
const std::string& aVfs /* = "" */) :
4455
mpSQLite(NULL),
4556
mFilename(aFilename)
4657
{
@@ -51,6 +62,11 @@ Database::Database(const std::string& aFilename, const int aFlags /*= SQLITE_OPE
5162
sqlite3_close(mpSQLite); // close is required even in case of error on opening
5263
throw SQLite::Exception(strerr);
5364
}
65+
66+
if (aTimeoutMs > 0)
67+
{
68+
setBusyTimeout(aTimeoutMs);
69+
}
5470
}
5571

5672
// Close the SQLite database connection.

tests/Database_test.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ TEST(Database, ctorExecCreateDropExist) {
3232
remove("test.db3");
3333
{
3434
// Try to open an unexisting database
35-
EXPECT_THROW(SQLite::Database not_found("test.db3"), SQLite::Exception);
35+
std::string filename = "test.db3";
36+
EXPECT_THROW(SQLite::Database not_found(filename), SQLite::Exception);
3637

3738
// Create a new database
3839
SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
@@ -92,20 +93,31 @@ TEST(Database, inMemory) {
9293
} // Close an destroy DB
9394
}
9495

95-
#if SQLITE_VERSION_NUMBER >= 3007015 // first version with PRAGMA busy_timeout
96+
#if SQLITE_VERSION_NUMBER >= 3007015 // SQLite v3.7.15 is first version with PRAGMA busy_timeout
9697
TEST(Database, busyTimeout) {
97-
// Create a new database
98-
SQLite::Database db(":memory:");
99-
// Busy timeout default to 0ms: any contention between threads or process leads to SQLITE_BUSY error
100-
EXPECT_EQ(0, db.execAndGet("PRAGMA busy_timeout").getInt());
98+
{
99+
// Create a new database with default timeout of 0ms
100+
SQLite::Database db(":memory:");
101+
// Busy timeout default to 0ms: any contention between threads or process leads to SQLITE_BUSY error
102+
EXPECT_EQ(0, db.execAndGet("PRAGMA busy_timeout").getInt());
101103

102-
// Set a non null busy timeout: any contention between threads will leads to as much retry as possible during the time
103-
db.setBusyTimeout(5000);
104-
EXPECT_EQ(5000, db.execAndGet("PRAGMA busy_timeout").getInt());
104+
// Set a non null busy timeout: any contention between threads will leads to as much retry as possible during the time
105+
db.setBusyTimeout(5000);
106+
EXPECT_EQ(5000, db.execAndGet("PRAGMA busy_timeout").getInt());
105107

106-
// Reset timeout to null
107-
db.setBusyTimeout(0);
108-
EXPECT_EQ(0, db.execAndGet("PRAGMA busy_timeout").getInt());
108+
// Reset timeout to 0
109+
db.setBusyTimeout(0);
110+
EXPECT_EQ(0, db.execAndGet("PRAGMA busy_timeout").getInt());
111+
}
112+
{
113+
// Create a new database with a non null busy timeout
114+
SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE, 5000);
115+
EXPECT_EQ(5000, db.execAndGet("PRAGMA busy_timeout").getInt());
116+
117+
// Reset timeout to null
118+
db.setBusyTimeout(0);
119+
EXPECT_EQ(0, db.execAndGet("PRAGMA busy_timeout").getInt());
120+
}
109121
}
110122
#endif // SQLITE_VERSION_NUMBER >= 3007015
111123

0 commit comments

Comments
 (0)