From cfa36aa5466d4c342c7de0f6cea7b8386984fd36 Mon Sep 17 00:00:00 2001 From: randomdan Date: Wed, 9 Feb 2011 01:33:33 +0000 Subject: Fix the build system to do dependencies properly Break down libodbcpp into a set of base classes; libdbpp Add a native PostgreSQL implementation of libdbpp; libpqpp Extend project2 rdbms stuff to work with generic connectors Update datasources to specify connector type Build libmisc as .so --- libpqpp/connection.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 libpqpp/connection.cpp (limited to 'libpqpp/connection.cpp') diff --git a/libpqpp/connection.cpp b/libpqpp/connection.cpp new file mode 100644 index 0000000..4330b3f --- /dev/null +++ b/libpqpp/connection.cpp @@ -0,0 +1,96 @@ +#include "connection.h" +#include "error.h" +#include "selectcommand.h" +#include "modifycommand.h" + +PQ::Connection::Connection(const std::string & info) : + conn(PQconnectdb(info.c_str())), + txDepth(0), + pstmntNo(0) +{ + if (PQstatus(conn) != CONNECTION_OK) { + throw ConnectionError(); + } +} + +PQ::Connection::~Connection() +{ + PQfinish(conn); +} + +int +PQ::Connection::beginTx() const +{ + if (txDepth == 0) { + checkResultFree(PQexec(conn, "BEGIN"), PGRES_COMMAND_OK, __PRETTY_FUNCTION__); + } + return ++txDepth; +} + +int +PQ::Connection::commitTx() const +{ + if (--txDepth == 0) { + checkResultFree(PQexec(conn, "COMMIT"), PGRES_COMMAND_OK, __PRETTY_FUNCTION__); + } + return txDepth; +} + +int +PQ::Connection::rollbackTx() const +{ + if (--txDepth == 0) { + checkResultFree(PQexec(conn, "ROLLBACK"), PGRES_COMMAND_OK, __PRETTY_FUNCTION__); + } + return txDepth; +} + +bool +PQ::Connection::inTx() const +{ + return txDepth; +} + +void +PQ::Connection::ping() const +{ +} + + +DB::SelectCommand * +PQ::Connection::newSelectCommand(const std::string & sql) const +{ + return new SelectCommand(this, sql, pstmntNo++); +} + +DB::ModifyCommand * +PQ::Connection::newModifyCommand(const std::string & sql) const +{ + return new ModifyCommand(this, sql, pstmntNo++); +} + +bool +PQ::Connection::checkResultInt(PGresult * res, int expected) +{ + return (PQresultStatus(res) == expected); +} + +void +PQ::Connection::checkResult(PGresult * res, int expected, const char * doing) const +{ + if (!checkResultInt(res, expected)) { + PQclear(res); + throw Error(PQerrorMessage(conn)); + } +} + +void +PQ::Connection::checkResultFree(PGresult * res, int expected, const char * doing) const +{ + if (!checkResultInt(res, expected)) { + PQclear(res); + throw Error(PQerrorMessage(conn)); + } + PQclear(res); +} + -- cgit v1.2.3