From da82e9c8f09f3e471044f8b52ede478ac4d5bc04 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 29 Dec 2015 02:42:43 +0000 Subject: Reshuffle and add new exceptions --- libpqpp/pq-connection.cpp | 15 ++++++++++----- libpqpp/pq-connection.h | 6 ++++++ libpqpp/pq-error.cpp | 30 +++++------------------------- libpqpp/pq-error.h | 17 +++++------------ libpqpp/pq-modifycommand.cpp | 2 +- libpqpp/unittests/testpq.cpp | 10 +++++----- 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 #include #include +#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 -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 #include -#include +#include namespace PQ { - class DLL_PUBLIC Error : public DB::Error { + class Error : public AdHoc::Exception { 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 #include -#include -#include -#include +#include +#include +#include #include #include #include @@ -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 ) -- cgit v1.2.3