diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-04-11 23:09:09 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-04-12 18:27:12 +0100 |
commit | 2b5267c1dd761f39dfbaca33fed667707fea9467 (patch) | |
tree | f6e0d9e7307ab080dc3d1b1e7cabcad768f40a05 | |
parent | Split ConnectionPool into two (diff) | |
download | libdbpp-2b5267c1dd761f39dfbaca33fed667707fea9467.tar.bz2 libdbpp-2b5267c1dd761f39dfbaca33fed667707fea9467.tar.xz libdbpp-2b5267c1dd761f39dfbaca33fed667707fea9467.zip |
C++17
Update TransactionScope interface to be more compliant with C++CG
-rw-r--r-- | libdbpp/connection.cpp | 10 | ||||
-rw-r--r-- | libdbpp/connection.h | 4 | ||||
-rw-r--r-- | libdbpp/tablepatch.cpp | 2 | ||||
m--------- | libdbpp/unittests/pq | 0 | ||||
-rw-r--r-- | libdbpp/unittests/testConnection.cpp | 4 |
5 files changed, 9 insertions, 11 deletions
diff --git a/libdbpp/connection.cpp b/libdbpp/connection.cpp index 276c4ed..5c63d0a 100644 --- a/libdbpp/connection.cpp +++ b/libdbpp/connection.cpp @@ -185,21 +185,19 @@ DB::TransactionRequired::message() const throw() return "A transaction must be opened before performing this operation"; } -DB::TransactionScope::TransactionScope(std::weak_ptr<DB::Connection> c) : +DB::TransactionScope::TransactionScope(DB::Connection & c) : conn(c) { - conn.lock()->beginTx(); + conn.beginTx(); } DB::TransactionScope::~TransactionScope() { - if (conn.expired()) return; - if (std::uncaught_exception()) { - conn.lock()->rollbackTx(); + conn.rollbackTx(); } else { - conn.lock()->commitTx(); + conn.commitTx(); } } diff --git a/libdbpp/connection.h b/libdbpp/connection.h index 4a424a7..e1fb2d2 100644 --- a/libdbpp/connection.h +++ b/libdbpp/connection.h @@ -175,14 +175,14 @@ namespace DB { class DLL_PUBLIC TransactionScope { public: /// Create a new helper and associated transaction on the given connection. - TransactionScope(std::weak_ptr<DB::Connection>); + TransactionScope(Connection &); ~TransactionScope(); private: TransactionScope(const TransactionScope &) = delete; void operator=(const TransactionScope &) = delete; - std::weak_ptr<Connection> conn; + Connection & conn; }; typedef AdHoc::Factory<Connection, std::string> ConnectionFactory; diff --git a/libdbpp/tablepatch.cpp b/libdbpp/tablepatch.cpp index c7b5597..3c3796d 100644 --- a/libdbpp/tablepatch.cpp +++ b/libdbpp/tablepatch.cpp @@ -27,7 +27,7 @@ DB::Connection::patchTable(TablePatch * tp) if (!inTx()) { throw TransactionRequired(); } - TransactionScope tx(shared_from_this()); + TransactionScope tx(*this); bool ownedExpr = false; if (!tp->srcExpr && !tp->src.empty()) { tp->srcExpr = new DB::StaticSqlWriter(tp->src); diff --git a/libdbpp/unittests/pq b/libdbpp/unittests/pq -Subproject 40beff4b1f58aa05fdffd2b7e7a24bbcb721cf0 +Subproject f86b8d2e23339e27d416145ad7b660d66267e15 diff --git a/libdbpp/unittests/testConnection.cpp b/libdbpp/unittests/testConnection.cpp index 7229f6e..b874a99 100644 --- a/libdbpp/unittests/testConnection.cpp +++ b/libdbpp/unittests/testConnection.cpp @@ -68,12 +68,12 @@ BOOST_AUTO_TEST_CASE( txscope ) BOOST_REQUIRE(mock); BOOST_REQUIRE_EQUAL(false, mock->inTx()); { - DB::TransactionScope tx(mock); + DB::TransactionScope tx(*mock); BOOST_REQUIRE_EQUAL(true, mock->inTx()); } BOOST_REQUIRE_EQUAL(false, mock->inTx()); try { - DB::TransactionScope tx(mock); + DB::TransactionScope tx(*mock); BOOST_REQUIRE_EQUAL(true, mock->inTx()); throw std::exception(); } |