Skip to content

Commit 010e365

Browse files
committed
Implement SQLiteDatabase::TxnBegin, TxnCommit, and TxnAbort
1 parent ac5c161 commit 010e365

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/wallet/sqlite.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,17 +431,32 @@ void SQLiteBatch::CloseCursor()
431431

432432
bool SQLiteBatch::TxnBegin()
433433
{
434-
return false;
434+
if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) == 0) return false;
435+
int res = sqlite3_exec(m_database.m_db, "BEGIN TRANSACTION", nullptr, nullptr, nullptr);
436+
if (res != SQLITE_OK) {
437+
LogPrintf("SQLiteBatch: Failed to begin the transaction\n");
438+
}
439+
return res == SQLITE_OK;
435440
}
436441

437442
bool SQLiteBatch::TxnCommit()
438443
{
439-
return false;
444+
if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) != 0) return false;
445+
int res = sqlite3_exec(m_database.m_db, "COMMIT TRANSACTION", nullptr, nullptr, nullptr);
446+
if (res != SQLITE_OK) {
447+
LogPrintf("SQLiteBatch: Failed to commit the transaction\n");
448+
}
449+
return res == SQLITE_OK;
440450
}
441451

442452
bool SQLiteBatch::TxnAbort()
443453
{
444-
return false;
454+
if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) != 0) return false;
455+
int res = sqlite3_exec(m_database.m_db, "ROLLBACK TRANSACTION", nullptr, nullptr, nullptr);
456+
if (res != SQLITE_OK) {
457+
LogPrintf("SQLiteBatch: Failed to abort the transaction\n");
458+
}
459+
return res == SQLITE_OK;
445460
}
446461

447462
bool ExistsSQLiteDatabase(const fs::path& path)

0 commit comments

Comments
 (0)