summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libpqpp/pq-connection.cpp15
-rw-r--r--libpqpp/pq-connection.h6
-rw-r--r--libpqpp/pq-error.cpp30
-rw-r--r--libpqpp/pq-error.h17
-rw-r--r--libpqpp/pq-modifycommand.cpp2
-rw-r--r--libpqpp/unittests/testpq.cpp10
6 files changed, 32 insertions, 48 deletions
diff --git a/libpqpp/pq-connection.cpp b/libpqpp/pq-connection.cpp
index 74fce9e..3de0071 100644
--- a/libpqpp/pq-connection.cpp
+++ b/libpqpp/pq-connection.cpp
@@ -20,6 +20,11 @@ noNoticeProcessor(void *, const char *)
{
}
+PQ::ConnectionError::ConnectionError(const PGconn * conn) :
+ PQ::Error(conn)
+{
+}
+
PQ::Connection::Connection(const std::string & info) :
conn(PQconnectdb(info.c_str())),
txDepth(0),
@@ -44,7 +49,7 @@ PQ::Connection::finish() const
{
if (txDepth != 0) {
rollbackTx();
- throw Error("Transaction still open");
+ throw DB::TransactionStillOpen();
}
}
@@ -145,7 +150,7 @@ PQ::Connection::checkResult(PGresult * res, int expected, int alt) const
{
if (!checkResultInt(res, expected, alt)) {
PQclear(res);
- throw Error(PQerrorMessage(conn));
+ throw Error(conn);
}
return res;
}
@@ -155,7 +160,7 @@ PQ::Connection::checkResultFree(PGresult * res, int expected, int alt) const
{
if (!checkResultInt(res, expected, alt)) {
PQclear(res);
- throw Error(PQerrorMessage(conn));
+ throw Error(conn);
}
PQclear(res);
}
@@ -180,7 +185,7 @@ PQ::Connection::endBulkUpload(const char * msg) const
checkResultFree(PQgetResult(conn), PGRES_COMMAND_OK);
return;
default:// -1 is error
- throw Error(PQerrorMessage(conn));
+ throw Error(conn);
}
}
@@ -194,7 +199,7 @@ PQ::Connection::bulkUploadData(const char * data, size_t len) const
case 1:// success
return len;
default:// -1 is error
- throw Error(PQerrorMessage(conn));
+ throw Error(conn);
}
}
diff --git a/libpqpp/pq-connection.h b/libpqpp/pq-connection.h
index 618c875..b7687cb 100644
--- a/libpqpp/pq-connection.h
+++ b/libpqpp/pq-connection.h
@@ -4,8 +4,14 @@
#include <connection.h>
#include <libpq-fe.h>
#include <visibility.h>
+#include "pq-error.h"
namespace PQ {
+ class ConnectionError : public virtual Error, public virtual DB::ConnectionError {
+ public:
+ ConnectionError(const PGconn *);
+ };
+
class DLL_PUBLIC Connection : public DB::Connection {
public:
Connection(const std::string & info);
diff --git a/libpqpp/pq-error.cpp b/libpqpp/pq-error.cpp
index 0d0299c..3a16185 100644
--- a/libpqpp/pq-error.cpp
+++ b/libpqpp/pq-error.cpp
@@ -1,34 +1,14 @@
#include "pq-error.h"
#include <string.h>
-PQ::Error::Error() :
- msg(NULL)
+PQ::Error::Error(const PGconn * conn) :
+ msg(PQerrorMessage(conn))
{
}
-PQ::Error::Error(const PQ::Error & e) :
- msg(e.msg ? strdup(e.msg) : NULL)
-{
-}
-
-PQ::Error::Error(const char * e) :
- msg(e ? strdup(e) : NULL)
-{
-}
-
-PQ::Error::~Error() throw()
-{
- free(msg);
-}
-
-const char *
-PQ::Error::what() const throw()
-{
- return msg ? msg : "No message";
-}
-
-PQ::ConnectionError::ConnectionError(const PGconn * conn) :
- PQ::Error(PQerrorMessage(conn))
+std::string
+PQ::Error::message() const throw()
{
+ return msg;
}
diff --git a/libpqpp/pq-error.h b/libpqpp/pq-error.h
index 8e7c4bc..fdc855b 100644
--- a/libpqpp/pq-error.h
+++ b/libpqpp/pq-error.h
@@ -3,24 +3,17 @@
#include <error.h>
#include <libpq-fe.h>
-#include <visibility.h>
+#include <exception.h>
namespace PQ {
- class DLL_PUBLIC Error : public DB::Error {
+ class Error : public AdHoc::Exception<DB::Error> {
public:
- Error();
- Error(const Error &);
- Error(const char *);
- ~Error() throw();
+ Error(const PGconn *);
- const char * what() const throw();
+ std::string message() const throw() override;
private:
- char * msg;
- };
- class DLL_PUBLIC ConnectionError : public Error, public virtual DB::ConnectionError {
- public:
- ConnectionError(const PGconn *);
+ std::string msg;
};
}
diff --git a/libpqpp/pq-modifycommand.cpp b/libpqpp/pq-modifycommand.cpp
index e07af0b..1e9f434 100644
--- a/libpqpp/pq-modifycommand.cpp
+++ b/libpqpp/pq-modifycommand.cpp
@@ -37,7 +37,7 @@ PQ::ModifyCommand::execute(bool anc)
unsigned int rows = atoi(PQcmdTuples(res));
PQclear(res);
if (rows == 0 && !anc) {
- throw Error("No rows affected");
+ throw DB::NoRowsAffected();
}
return rows;
}
diff --git a/libpqpp/unittests/testpq.cpp b/libpqpp/unittests/testpq.cpp
index 2ff3235..e6721ec 100644
--- a/libpqpp/unittests/testpq.cpp
+++ b/libpqpp/unittests/testpq.cpp
@@ -2,9 +2,9 @@
#include <boost/test/unit_test.hpp>
#include <definedDirs.h>
-#include <dbpp/modifycommand.h>
-#include <dbpp/selectcommand.h>
-#include <dbpp/column.h>
+#include <modifycommand.h>
+#include <selectcommand.h>
+#include <column.h>
#include <pq-mock.h>
#include <testCore.h>
#include <fstream>
@@ -204,7 +204,7 @@ BOOST_AUTO_TEST_CASE( reconnectInTx )
kil->execute();
delete kil;
usleep(5000);
- BOOST_REQUIRE_THROW(ro->ping(), PQ::ConnectionError);
+ BOOST_REQUIRE_THROW(ro->ping(), DB::ConnectionError);
delete ro;
delete rok;
}
@@ -213,7 +213,7 @@ BOOST_AUTO_TEST_SUITE_END();
BOOST_AUTO_TEST_CASE( connfail )
{
- BOOST_REQUIRE_THROW(DB::ConnectionFactory::createNew("postgresql", "host=localhost user=no"), PQ::ConnectionError);
+ BOOST_REQUIRE_THROW(DB::ConnectionFactory::createNew("postgresql", "host=localhost user=no"), DB::ConnectionError);
}
BOOST_AUTO_TEST_CASE( ssl )