summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-12-29 05:16:51 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2015-12-29 06:00:08 +0000
commit2a8c566bee4f12f48a441fd37f64102328eac6d0 (patch)
tree68d232be8d4a10877ac56268a2e309f39fe4deef
parentTidy up (diff)
downloadlibdbpp-postgresql-2a8c566bee4f12f48a441fd37f64102328eac6d0.tar.bz2
libdbpp-postgresql-2a8c566bee4f12f48a441fd37f64102328eac6d0.tar.xz
libdbpp-postgresql-2a8c566bee4f12f48a441fd37f64102328eac6d0.zip
Improve and centralise transaction handling logiclibdbpp-postgresql-1.0.0
-rw-r--r--libpqpp/pq-command.cpp2
-rw-r--r--libpqpp/pq-command.h4
-rw-r--r--libpqpp/pq-connection.cpp51
-rw-r--r--libpqpp/pq-connection.h10
-rw-r--r--libpqpp/pq-modifycommand.cpp2
-rw-r--r--libpqpp/pq-modifycommand.h2
-rw-r--r--libpqpp/pq-selectcommand.cpp2
-rw-r--r--libpqpp/pq-selectcommand.h2
-rw-r--r--libpqpp/unittests/testpq.cpp1
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 <buffer.h>
#include <boost/date_time/posix_time/posix_time.hpp>
-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<char *> 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 <stdlib.h>
#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;
}