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-connection.cpp | 51 +++++++++-------------------------------------- 1 file changed, 9 insertions(+), 42 deletions(-) (limited to 'libpqpp/pq-connection.cpp') 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 -- cgit v1.2.3