Merge pull request #390 fix incorrect work of savepoint from spoyler/save_point

Fix #381
This commit is contained in:
Sébastien Rombauts 2022-12-15 13:12:10 +01:00 committed by GitHub
commit 343299a31d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 5 deletions

View File

@ -93,7 +93,7 @@ void Database::Deleter::operator()(sqlite3* apSQLite)
SQLITECPP_ASSERT(SQLITE_OK == ret, "database is locked"); // See SQLITECPP_ENABLE_ASSERT_HANDLER SQLITECPP_ASSERT(SQLITE_OK == ret, "database is locked"); // See SQLITECPP_ENABLE_ASSERT_HANDLER
} }
//Set a busy handler that sleeps for a specified amount of time when a table is locked. // Set a busy handler that sleeps for a specified amount of time when a table is locked.
void Database::setBusyTimeout(const int aBusyTimeoutMs) void Database::setBusyTimeout(const int aBusyTimeoutMs)
{ {
const int ret = sqlite3_busy_timeout(getHandle(), aBusyTimeoutMs); const int ret = sqlite3_busy_timeout(getHandle(), aBusyTimeoutMs);

View File

@ -36,6 +36,7 @@ Savepoint::~Savepoint() {
if (!mbReleased) { if (!mbReleased) {
try { try {
rollback(); rollback();
release();
} catch (SQLite::Exception&) { } catch (SQLite::Exception&) {
// Never throw an exception in a destructor: error if already rolled // Never throw an exception in a destructor: error if already rolled
// back or released, but no harm is caused by this. // back or released, but no harm is caused by this.
@ -49,7 +50,7 @@ void Savepoint::release() {
mDatabase.exec(std::string("RELEASE SAVEPOINT ") + msName); mDatabase.exec(std::string("RELEASE SAVEPOINT ") + msName);
mbReleased = true; mbReleased = true;
} else { } else {
throw SQLite::Exception("Savepoint already released or rolled back."); throw SQLite::Exception("Savepoint already released.");
} }
} }
@ -57,9 +58,8 @@ void Savepoint::release() {
void Savepoint::rollback() { void Savepoint::rollback() {
if (!mbReleased) { if (!mbReleased) {
mDatabase.exec(std::string("ROLLBACK TO SAVEPOINT ") + msName); mDatabase.exec(std::string("ROLLBACK TO SAVEPOINT ") + msName);
mbReleased = true;
} else { } else {
throw SQLite::Exception("Savepoint already released or rolled back."); throw SQLite::Exception("Savepoint already released.");
} }
} }

View File

@ -33,7 +33,7 @@ TEST(Savepoint, commitRollback) {
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)")); db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"));
EXPECT_EQ(SQLite::OK, db.getErrorCode()); EXPECT_EQ(SQLite::OK, db.getErrorCode());
// Insert a first valu // Insert a first value
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'first')")); EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'first')"));
EXPECT_EQ(1, db.getLastInsertRowid()); EXPECT_EQ(1, db.getLastInsertRowid());