From 2b5267c1dd761f39dfbaca33fed667707fea9467 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 11 Apr 2018 23:09:09 +0100 Subject: C++17 Update TransactionScope interface to be more compliant with C++CG --- libdbpp/connection.cpp | 10 ++++------ libdbpp/connection.h | 4 ++-- libdbpp/tablepatch.cpp | 2 +- libdbpp/unittests/pq | 2 +- libdbpp/unittests/testConnection.cpp | 4 ++-- 5 files changed, 10 insertions(+), 12 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 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); + TransactionScope(Connection &); ~TransactionScope(); private: TransactionScope(const TransactionScope &) = delete; void operator=(const TransactionScope &) = delete; - std::weak_ptr conn; + Connection & conn; }; typedef AdHoc::Factory 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 index 40beff4..f86b8d2 160000 --- a/libdbpp/unittests/pq +++ b/libdbpp/unittests/pq @@ -1 +1 @@ -Subproject commit 40beff4b1f58aa05fdffd2b7e7a24bbcb721cf07 +Subproject commit f86b8d2e23339e27d416145ad7b660d66267e151 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(); } -- cgit v1.2.3