From fd28011455bd9552150031e9f4a69f5b185b4ae2 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 29 Dec 2015 05:16:51 +0000 Subject: Improve and centralise transaction handling logic --- libsqlitepp/sqlite-connection.cpp | 57 +++++++++------------------------------ 1 file changed, 12 insertions(+), 45 deletions(-) (limited to 'libsqlitepp/sqlite-connection.cpp') diff --git a/libsqlitepp/sqlite-connection.cpp b/libsqlitepp/sqlite-connection.cpp index a284a2e..6545918 100644 --- a/libsqlitepp/sqlite-connection.cpp +++ b/libsqlitepp/sqlite-connection.cpp @@ -8,9 +8,7 @@ SQLite::ConnectionError::ConnectionError(sqlite3 * db) : { } -SQLite::Connection::Connection(const std::string & str) : - txDepth(0), - rolledback(false) +SQLite::Connection::Connection(const std::string & str) { if (sqlite3_open(str.c_str(), &db) != SQLITE_OK) { ConnectionError err(db); @@ -25,58 +23,27 @@ SQLite::Connection::~Connection() } void -SQLite::Connection::finish() const +SQLite::Connection::beginTxInt() { - if (txDepth != 0) { - rollbackTx(); - throw DB::TransactionStillOpen(); + if (sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { + throw Error(db); } } -int -SQLite::Connection::beginTx() const -{ - if (txDepth == 0) { - if (sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { - throw Error(db); - } - rolledback = false; - } - return ++txDepth; -} - -int -SQLite::Connection::commitTx() const +void +SQLite::Connection::commitTxInt() { - if (rolledback) { - return rollbackTx(); - } - if (--txDepth == 0) { - if (sqlite3_exec(db, "COMMIT TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { - throw Error(db); - } + if (sqlite3_exec(db, "COMMIT TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { + throw Error(db); } - return txDepth; } -int -SQLite::Connection::rollbackTx() const +void +SQLite::Connection::rollbackTxInt() { - if (--txDepth == 0) { - if (sqlite3_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { - throw Error(db); - } - } - else { - rolledback = true; + if (sqlite3_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { + throw Error(db); } - return txDepth; -} - -bool -SQLite::Connection::inTx() const -{ - return txDepth; } DB::BulkDeleteStyle -- cgit v1.2.3