summaryrefslogtreecommitdiff
path: root/libpqpp/connection.cpp
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2011-02-09 01:33:33 +0000
committerrandomdan <randomdan@localhost>2011-02-09 01:33:33 +0000
commitcfa36aa5466d4c342c7de0f6cea7b8386984fd36 (patch)
tree17a13c13b3588101a146705839d5da6f280bddb9 /libpqpp/connection.cpp
parentCentralise the ICE splicer (diff)
downloadlibdbpp-postgresql-cfa36aa5466d4c342c7de0f6cea7b8386984fd36.tar.bz2
libdbpp-postgresql-cfa36aa5466d4c342c7de0f6cea7b8386984fd36.tar.xz
libdbpp-postgresql-cfa36aa5466d4c342c7de0f6cea7b8386984fd36.zip
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
Diffstat (limited to 'libpqpp/connection.cpp')
-rw-r--r--libpqpp/connection.cpp96
1 files changed, 96 insertions, 0 deletions
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);
+}
+