diff options
-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 | ||||
-rw-r--r-- | test/test-postgresql.cpp | 6 |
7 files changed, 26 insertions, 11 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); diff --git a/test/test-postgresql.cpp b/test/test-postgresql.cpp index 3a12f43..eaf6d58 100644 --- a/test/test-postgresql.cpp +++ b/test/test-postgresql.cpp @@ -22,11 +22,11 @@ BOOST_AUTO_TEST_CASE(simple) BOOST_CHECK_THROW(([]() { MyGrate::Output::Pq::PqConn {"nonsense"}; }()), - std::runtime_error); + MyGrate::Output::Pq::PqErr); MyGrate::Output::Pq::PqConn c {CONNSTR}; BOOST_CHECK_NO_THROW(c.query("SET application_name = ''")); BOOST_CHECK_NO_THROW(c.query("SET application_name = 'something'")); - BOOST_CHECK_THROW(c.query("SET application_name = "), std::runtime_error); + BOOST_CHECK_THROW(c.query("SET application_name = "), MyGrate::Output::Pq::PqErr); // BOOST_CHECK_THROW(c.query("SET application_name = $1", {}), std::logic_error); BOOST_CHECK_NO_THROW(c.query("SET application_name = 'something'", {})); c.query("DROP TABLE IF EXISTS test"); @@ -35,7 +35,7 @@ BOOST_AUTO_TEST_CASE(simple) BOOST_CHECK_NO_THROW(c.query("INSERT INTO test VALUES($1)", {"string_view"})); BOOST_CHECK_NO_THROW(c.query("INSERT INTO test VALUES($1)", {nullptr})); BOOST_CHECK_NO_THROW(c.query("INSERT INTO test VALUES($1)", {1.2})); - BOOST_CHECK_THROW(c.query("INSERT INTO test VALUES($1)", {MyGrate::Time {}}), std::runtime_error); + BOOST_CHECK_THROW(c.query("INSERT INTO test VALUES($1)", {MyGrate::Time {}}), std::logic_error); c.query("DROP TABLE test"); } |