diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-12-29 05:16:51 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-12-29 06:00:08 +0000 |
commit | fd28011455bd9552150031e9f4a69f5b185b4ae2 (patch) | |
tree | f1bfa778f310c387fe2645527c6065436b27e903 | |
parent | Add missing override attribute (diff) | |
download | libdbpp-sqlite-1.0.0.tar.bz2 libdbpp-sqlite-1.0.0.tar.xz libdbpp-sqlite-1.0.0.zip |
Improve and centralise transaction handling logiclibdbpp-sqlite-1.0.0
-rw-r--r-- | libsqlitepp/sqlite-connection.cpp | 57 | ||||
-rw-r--r-- | libsqlitepp/sqlite-connection.h | 12 | ||||
-rw-r--r-- | 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; } |