From afd243cc87827c94f0cea73748ffc5c1fdf1880a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 12 Jun 2021 20:54:28 +0100 Subject: Avoid direct use of runtime_error in PostgreSQL stuff Adds proper exception which extends it and gets the PostgreSQL error message. --- lib/output/pq/pqBindings.h | 2 +- lib/output/pq/pqConn.cpp | 11 ++++++++--- lib/output/pq/pqConn.h | 6 ++++++ lib/output/pq/pqStmt.cpp | 6 +++--- lib/output/pq/updateDatabase.cpp | 2 +- lib/output/pq/updateDatabase.h | 4 ++++ 6 files changed, 23 insertions(+), 8 deletions(-) (limited to 'lib/output') diff --git a/lib/output/pq/pqBindings.h b/lib/output/pq/pqBindings.h index c63b286..5df1a34 100644 --- a/lib/output/pq/pqBindings.h +++ b/lib/output/pq/pqBindings.h @@ -39,7 +39,7 @@ namespace MyGrate::Output::Pq { void operator()(const T &) { - throw std::runtime_error("Not implemented"); + throw std::logic_error("Not implemented"); } void operator()(const std::nullptr_t &) diff --git a/lib/output/pq/pqConn.cpp b/lib/output/pq/pqConn.cpp index b3a6ca6..c1a7362 100644 --- a/lib/output/pq/pqConn.cpp +++ b/lib/output/pq/pqConn.cpp @@ -10,9 +10,14 @@ #include namespace MyGrate::Output::Pq { + PqErr::PqErr(const std::string & when, PGconn * c) : std::runtime_error(when + ": " + PQerrorMessage((c))) { } + PqErr::PqErr(const std::string & when, PGresult * r) : std::runtime_error(when + ": " + PQresultErrorMessage((r))) + { + } + PqConn::PqConn(const char * const str) : conn {PQconnectdb(str), PQfinish} { - verify(PQstatus(conn.get()) == CONNECTION_OK, "Connection failure"); + verify(PQstatus(conn.get()) == CONNECTION_OK, "Connection failure", conn.get()); PQsetNoticeProcessor(conn.get(), notice_processor, this); } @@ -20,7 +25,7 @@ namespace MyGrate::Output::Pq { PqConn::query(const char * const q) { ResPtr res {PQexec(conn.get(), q), &PQclear}; - verify(PQresultStatus(res.get()) == PGRES_COMMAND_OK, q); + verify(PQresultStatus(res.get()) == PGRES_COMMAND_OK, q, res.get()); } void @@ -29,7 +34,7 @@ namespace MyGrate::Output::Pq { Bindings b {vs}; ResPtr res {PQexecParams(conn.get(), q, (int)vs.size(), nullptr, b.values.data(), b.lengths.data(), nullptr, 0), &PQclear}; - verify(PQresultStatus(res.get()) == PGRES_COMMAND_OK, q); + verify(PQresultStatus(res.get()) == PGRES_COMMAND_OK, q, res.get()); } DbPrepStmtPtr diff --git a/lib/output/pq/pqConn.h b/lib/output/pq/pqConn.h index 5c9effb..b8609ea 100644 --- a/lib/output/pq/pqConn.h +++ b/lib/output/pq/pqConn.h @@ -14,6 +14,12 @@ namespace MyGrate { class DbValue; } namespace MyGrate::Output::Pq { + class PqErr : public std::runtime_error { + public: + PqErr(const std::string & when, PGconn *); + PqErr(const std::string & when, PGresult *); + }; + class PqConn : public DbConn { public: static constexpr auto paramMode {ParamMode::DollarNum}; diff --git a/lib/output/pq/pqStmt.cpp b/lib/output/pq/pqStmt.cpp index 078c239..dc258b6 100644 --- a/lib/output/pq/pqStmt.cpp +++ b/lib/output/pq/pqStmt.cpp @@ -24,8 +24,8 @@ namespace MyGrate::Output::Pq { Bindings b {vs}; res = {PQexecPrepared(conn, name.c_str(), (int)vs.size(), b.values.data(), b.lengths.data(), nullptr, 0), &PQclear}; - verify( - PQresultStatus(res.get()) == PGRES_COMMAND_OK || PQresultStatus(res.get()) == PGRES_TUPLES_OK, name); + verify(PQresultStatus(res.get()) == PGRES_COMMAND_OK || PQresultStatus(res.get()) == PGRES_TUPLES_OK, + name, conn); } std::size_t @@ -48,7 +48,7 @@ namespace MyGrate::Output::Pq { } auto nam {scprintf<"pst%0x">(c->stmts.size())}; ResPtr res {PQprepare(c->conn.get(), nam.c_str(), q, (int)n, nullptr), PQclear}; - verify(PQresultStatus(res.get()) == PGRES_COMMAND_OK, q); + verify(PQresultStatus(res.get()) == PGRES_COMMAND_OK, q, c->conn.get()); return c->stmts.emplace(q, std::move(nam)).first->second; } } diff --git a/lib/output/pq/updateDatabase.cpp b/lib/output/pq/updateDatabase.cpp index 8b4d5e3..4d60fee 100644 --- a/lib/output/pq/updateDatabase.cpp +++ b/lib/output/pq/updateDatabase.cpp @@ -25,7 +25,7 @@ namespace MyGrate::Output::Pq { UpdateDatabase::getSource() { auto srcrec = output::pq::sql::selectSource::execute(this, source); - verify(srcrec->rows() == 1, "Wrong number of source config rows"); + verify(srcrec->rows() == 1, "Wrong number of source config rows"); return (*srcrec)[0].create(); } diff --git a/lib/output/pq/updateDatabase.h b/lib/output/pq/updateDatabase.h index 2e48dd6..e3e27ea 100644 --- a/lib/output/pq/updateDatabase.h +++ b/lib/output/pq/updateDatabase.h @@ -22,6 +22,10 @@ namespace MyGrate::Output::Pq { }; using TableDefPtr = std::unique_ptr; + class ConfigError : public std::runtime_error { + using std::runtime_error::runtime_error; + }; + class UpdateDatabase : public PqConn, public EventHandlerBase { public: UpdateDatabase(const char * const str, uint64_t source); -- cgit v1.2.3