diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-06-12 20:54:28 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-06-12 20:54:28 +0100 |
commit | afd243cc87827c94f0cea73748ffc5c1fdf1880a (patch) | |
tree | ad3f9c39902a78026b24d9a32e8baf6ff2858281 /lib/output | |
parent | Create a basic MySQL testing database class (diff) | |
download | mygrate-afd243cc87827c94f0cea73748ffc5c1fdf1880a.tar.bz2 mygrate-afd243cc87827c94f0cea73748ffc5c1fdf1880a.tar.xz mygrate-afd243cc87827c94f0cea73748ffc5c1fdf1880a.zip |
Avoid direct use of runtime_error in PostgreSQL stuff
Adds proper exception which extends it and gets the PostgreSQL error
message.
Diffstat (limited to 'lib/output')
-rw-r--r-- | lib/output/pq/pqBindings.h | 2 | ||||
-rw-r--r-- | lib/output/pq/pqConn.cpp | 11 | ||||
-rw-r--r-- | lib/output/pq/pqConn.h | 6 | ||||
-rw-r--r-- | lib/output/pq/pqStmt.cpp | 6 | ||||
-rw-r--r-- | lib/output/pq/updateDatabase.cpp | 2 | ||||
-rw-r--r-- | lib/output/pq/updateDatabase.h | 4 |
6 files changed, 23 insertions, 8 deletions
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 <vector> 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<std::runtime_error>(PQstatus(conn.get()) == CONNECTION_OK, "Connection failure"); + verify<PqErr>(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<std::runtime_error>(PQresultStatus(res.get()) == PGRES_COMMAND_OK, q); + verify<PqErr>(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<std::runtime_error>(PQresultStatus(res.get()) == PGRES_COMMAND_OK, q); + verify<PqErr>(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<std::runtime_error>( - PQresultStatus(res.get()) == PGRES_COMMAND_OK || PQresultStatus(res.get()) == PGRES_TUPLES_OK, name); + verify<PqErr>(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<std::runtime_error>(PQresultStatus(res.get()) == PGRES_COMMAND_OK, q); + verify<PqErr>(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<std::runtime_error>(srcrec->rows() == 1, "Wrong number of source config rows"); + verify<ConfigError>(srcrec->rows() == 1, "Wrong number of source config rows"); return (*srcrec)[0].create<Input::ReplicationStream, 7>(); } 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<TableDef>; + 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); |