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 ++++++++---------------------------- libsqlitepp/sqlite-connection.h | 12 ++------ libsqlitepp/unittests/testsqlite.cpp | 1 - 3 files changed, 15 insertions(+), 55 deletions(-) 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 diff --git a/libsqlitepp/sqlite-connection.h b/libsqlitepp/sqlite-connection.h index 4f705a3..f3f354d 100644 --- a/libsqlitepp/sqlite-connection.h +++ b/libsqlitepp/sqlite-connection.h @@ -16,11 +16,9 @@ namespace SQLite { Connection(const std::string & info); ~Connection(); - void finish() const override; - int beginTx() const override; - int commitTx() const override; - int rollbackTx() const override; - bool inTx() const override; + void beginTxInt() override; + void commitTxInt() override; + void rollbackTxInt() override; void ping() const override; DB::BulkDeleteStyle bulkDeleteStyle() const override; DB::BulkUpdateStyle bulkUpdateStyle() const override; @@ -31,10 +29,6 @@ namespace SQLite { int64_t insertId() override; sqlite3 * db; - - private: - mutable unsigned int txDepth; - mutable bool rolledback; }; } diff --git a/libsqlitepp/unittests/testsqlite.cpp b/libsqlitepp/unittests/testsqlite.cpp index 1e4aa90..18707ef 100644 --- a/libsqlitepp/unittests/testsqlite.cpp +++ b/libsqlitepp/unittests/testsqlite.cpp @@ -50,7 +50,6 @@ BOOST_AUTO_TEST_CASE( bindAndSend ) mod->execute(); BOOST_REQUIRE_EQUAL(2, rw->insertId()); delete mod; - rw->commitTx(); delete rw; } -- cgit v1.2.3