From fd9ad6023012494356829e9b7642ef4ec07f30a3 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 --- libpqpp/pq-command.cpp | 2 +- libpqpp/pq-command.h | 4 ++-- libpqpp/pq-connection.cpp | 51 ++++++++------------------------------------ libpqpp/pq-connection.h | 10 +++------ libpqpp/pq-modifycommand.cpp | 2 +- libpqpp/pq-modifycommand.h | 2 +- libpqpp/pq-selectcommand.cpp | 2 +- libpqpp/pq-selectcommand.h | 2 +- libpqpp/unittests/testpq.cpp | 1 - 9 files changed, 19 insertions(+), 57 deletions(-) diff --git a/libpqpp/pq-command.cpp b/libpqpp/pq-command.cpp index 9d6b7c2..9b336bd 100644 --- a/libpqpp/pq-command.cpp +++ b/libpqpp/pq-command.cpp @@ -5,7 +5,7 @@ #include #include -PQ::Command::Command(const Connection * conn, const std::string & sql, unsigned int no) : +PQ::Command::Command(Connection * conn, const std::string & sql, unsigned int no) : DB::Command(sql), stmntName(stringbf("pStatement_%u_%p", no, this)), c(conn) diff --git a/libpqpp/pq-command.h b/libpqpp/pq-command.h index e0f1e61..ea32f61 100644 --- a/libpqpp/pq-command.h +++ b/libpqpp/pq-command.h @@ -9,7 +9,7 @@ namespace PQ { class Connection; class Command : public virtual DB::Command { public: - Command(const Connection *, const std::string & sql, unsigned int no); + Command(Connection *, const std::string & sql, unsigned int no); virtual ~Command() = 0; void bindParamI(unsigned int, int) override; @@ -33,7 +33,7 @@ namespace PQ { protected: static void prepareSql(std::string & psql, const std::string & sql); const std::string stmntName; - const Connection * c; + Connection * const c; void paramsAtLeast(unsigned int); std::vector values; diff --git a/libpqpp/pq-connection.cpp b/libpqpp/pq-connection.cpp index 42fc1aa..e6340d5 100644 --- a/libpqpp/pq-connection.cpp +++ b/libpqpp/pq-connection.cpp @@ -27,9 +27,7 @@ PQ::ConnectionError::ConnectionError(const PGconn * conn) : PQ::Connection::Connection(const std::string & info) : conn(PQconnectdb(info.c_str())), - txDepth(0), - pstmntNo(0), - rolledback(false) + pstmntNo(0) { if (PQstatus(conn) != CONNECTION_OK) { ConnectionError ce(conn); @@ -45,52 +43,21 @@ PQ::Connection::~Connection() } void -PQ::Connection::finish() const +PQ::Connection::beginTxInt() { - if (txDepth != 0) { - rollbackTx(); - throw DB::TransactionStillOpen(); - } -} - -int -PQ::Connection::beginTx() const -{ - if (txDepth == 0) { - checkResultFree(PQexec(conn, "BEGIN"), PGRES_COMMAND_OK); - rolledback = false; - } - return ++txDepth; + checkResultFree(PQexec(conn, "BEGIN"), PGRES_COMMAND_OK); } -int -PQ::Connection::commitTx() const -{ - if (rolledback) { - return rollbackTx(); - } - if (--txDepth == 0) { - checkResultFree(PQexec(conn, "COMMIT"), PGRES_COMMAND_OK); - } - return txDepth; -} - -int -PQ::Connection::rollbackTx() const +void +PQ::Connection::commitTxInt() { - if (--txDepth == 0) { - checkResultFree(PQexec(conn, "ROLLBACK"), PGRES_COMMAND_OK); - } - else { - rolledback = true; - } - return txDepth; + checkResultFree(PQexec(conn, "COMMIT"), PGRES_COMMAND_OK); } -bool -PQ::Connection::inTx() const +void +PQ::Connection::rollbackTxInt() { - return txDepth; + checkResultFree(PQexec(conn, "ROLLBACK"), PGRES_COMMAND_OK); } void diff --git a/libpqpp/pq-connection.h b/libpqpp/pq-connection.h index 65fd7b0..59b200c 100644 --- a/libpqpp/pq-connection.h +++ b/libpqpp/pq-connection.h @@ -17,11 +17,9 @@ namespace PQ { 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; void execute(const std::string & sql) override; DB::BulkDeleteStyle bulkDeleteStyle() const override; @@ -44,9 +42,7 @@ namespace PQ { private: static bool checkResultInt(PGresult * res, int expected, int alternative); - mutable unsigned int txDepth; mutable unsigned int pstmntNo; - mutable bool rolledback; }; } diff --git a/libpqpp/pq-modifycommand.cpp b/libpqpp/pq-modifycommand.cpp index 1e9f434..e08057a 100644 --- a/libpqpp/pq-modifycommand.cpp +++ b/libpqpp/pq-modifycommand.cpp @@ -3,7 +3,7 @@ #include #include "pq-connection.h" -PQ::ModifyCommand::ModifyCommand(const Connection * conn, const std::string & sql, unsigned int no) : +PQ::ModifyCommand::ModifyCommand(Connection * conn, const std::string & sql, unsigned int no) : DB::Command(sql), DB::ModifyCommand(sql), PQ::Command(conn, sql, no), diff --git a/libpqpp/pq-modifycommand.h b/libpqpp/pq-modifycommand.h index 20e5035..5f48074 100644 --- a/libpqpp/pq-modifycommand.h +++ b/libpqpp/pq-modifycommand.h @@ -8,7 +8,7 @@ namespace PQ { class Connection; class ModifyCommand : public DB::ModifyCommand, public Command { public: - ModifyCommand(const Connection *, const std::string & sql, unsigned int no); + ModifyCommand(Connection *, const std::string & sql, unsigned int no); virtual ~ModifyCommand(); unsigned int execute(bool) override; diff --git a/libpqpp/pq-selectcommand.cpp b/libpqpp/pq-selectcommand.cpp index 26967c5..e5a6cf2 100644 --- a/libpqpp/pq-selectcommand.cpp +++ b/libpqpp/pq-selectcommand.cpp @@ -3,7 +3,7 @@ #include "pq-column.h" #include "pq-error.h" -PQ::SelectCommand::SelectCommand(const Connection * conn, const std::string & sql, unsigned int no) : +PQ::SelectCommand::SelectCommand(Connection * conn, const std::string & sql, unsigned int no) : DB::Command(sql), DB::SelectCommand(sql), PQ::Command(conn, sql, no), diff --git a/libpqpp/pq-selectcommand.h b/libpqpp/pq-selectcommand.h index cc35957..f2e0c74 100644 --- a/libpqpp/pq-selectcommand.h +++ b/libpqpp/pq-selectcommand.h @@ -11,7 +11,7 @@ namespace PQ { class Column; class SelectCommand : public DB::SelectCommand, public Command { public: - SelectCommand(const Connection *, const std::string & sql, unsigned int no); + SelectCommand(Connection *, const std::string & sql, unsigned int no); virtual ~SelectCommand(); bool fetch() override; diff --git a/libpqpp/unittests/testpq.cpp b/libpqpp/unittests/testpq.cpp index e6721ec..ed6c4bc 100644 --- a/libpqpp/unittests/testpq.cpp +++ b/libpqpp/unittests/testpq.cpp @@ -55,7 +55,6 @@ BOOST_AUTO_TEST_CASE( bindAndSend ) mod->bindParamT(5, testInterval); mod->execute(); delete mod; - rw->commitTx(); delete rw; } -- cgit v1.2.3