summaryrefslogtreecommitdiff
path: root/libpqpp/connection.cpp
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2011-02-17 20:42:53 +0000
committerrandomdan <randomdan@localhost>2011-02-17 20:42:53 +0000
commitc5f116af8c044883edf44311173a0c6c017b7d59 (patch)
tree1ec836764f704d6b39ad52bdddda7fcd35d642f5 /libpqpp/connection.cpp
parentFixes to compile with all gcc warnings as errors (diff)
downloadlibdbpp-postgresql-c5f116af8c044883edf44311173a0c6c017b7d59.tar.bz2
libdbpp-postgresql-c5f116af8c044883edf44311173a0c6c017b7d59.tar.xz
libdbpp-postgresql-c5f116af8c044883edf44311173a0c6c017b7d59.zip
Add check function for when a connection is finished with, but you don't want to close it
Use prepared statements only for modifications, use fetch instead for selects (doesn't load an entire record set) Support varchar oid
Diffstat (limited to 'libpqpp/connection.cpp')
-rw-r--r--libpqpp/connection.cpp22
1 files changed, 20 insertions, 2 deletions
diff --git a/libpqpp/connection.cpp b/libpqpp/connection.cpp
index ef8e72d..11c043e 100644
--- a/libpqpp/connection.cpp
+++ b/libpqpp/connection.cpp
@@ -11,7 +11,8 @@ noNoticeProcessor(void *, const char *)
PQ::Connection::Connection(const std::string & info) :
conn(PQconnectdb(info.c_str())),
txDepth(0),
- pstmntNo(0)
+ pstmntNo(0),
+ rolledback(false)
{
if (PQstatus(conn) != CONNECTION_OK) {
throw ConnectionError();
@@ -24,11 +25,21 @@ PQ::Connection::~Connection()
PQfinish(conn);
}
+void
+PQ::Connection::finish() const
+{
+ if (txDepth != 0) {
+ rollbackTx();
+ throw Error("Transaction still open");
+ }
+}
+
int
PQ::Connection::beginTx() const
{
if (txDepth == 0) {
checkResultFree(PQexec(conn, "BEGIN"), PGRES_COMMAND_OK);
+ rolledback = false;
}
return ++txDepth;
}
@@ -36,6 +47,9 @@ PQ::Connection::beginTx() const
int
PQ::Connection::commitTx() const
{
+ if (rolledback) {
+ return rollbackTx();
+ }
if (--txDepth == 0) {
checkResultFree(PQexec(conn, "COMMIT"), PGRES_COMMAND_OK);
}
@@ -48,6 +62,9 @@ PQ::Connection::rollbackTx() const
if (--txDepth == 0) {
checkResultFree(PQexec(conn, "ROLLBACK"), PGRES_COMMAND_OK);
}
+ else {
+ rolledback = true;
+ }
return txDepth;
}
@@ -93,13 +110,14 @@ PQ::Connection::checkResultInt(PGresult * res, int expected)
return (PQresultStatus(res) == expected);
}
-void
+PGresult *
PQ::Connection::checkResult(PGresult * res, int expected) const
{
if (!checkResultInt(res, expected)) {
PQclear(res);
throw Error(PQerrorMessage(conn));
}
+ return res;
}
void