summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-10-08 17:23:41 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-10-08 17:23:41 +0100
commitef89db97d3acf1b3573ff6097ff8eb4288c845da (patch)
treee4c5480e122401436716e21ca8eee8610f0d665c
parentUse unique_ptr for execution results (diff)
downloadlibdbpp-postgresql-ef89db97d3acf1b3573ff6097ff8eb4288c845da.tar.bz2
libdbpp-postgresql-ef89db97d3acf1b3573ff6097ff8eb4288c845da.tar.xz
libdbpp-postgresql-ef89db97d3acf1b3573ff6097ff8eb4288c845da.zip
Simplify checkResult
Becomes template for however many acceptable status there are and tests as a fold expression.
-rw-r--r--libpqpp/pq-connection.cpp11
-rw-r--r--libpqpp/pq-connection.h6
2 files changed, 5 insertions, 12 deletions
diff --git a/libpqpp/pq-connection.cpp b/libpqpp/pq-connection.cpp
index dc3cf13..ed00d8d 100644
--- a/libpqpp/pq-connection.cpp
+++ b/libpqpp/pq-connection.cpp
@@ -124,16 +124,11 @@ PQ::Connection::modify(const std::string & sql, const DB::CommandOptionsCPtr & o
return std::make_shared<ModifyCommand>(this, sql, opts);
}
-bool
-PQ::Connection::checkResultInt(PGresult * res, int expected, int alt)
-{
- return (PQresultStatus(res) == expected) || (alt != -1 && (PQresultStatus(res) == alt));
-}
-
+template<std::same_as<ExecStatusType>... Expected>
PQ::ResultPtr
-PQ::Connection::checkResult(PGresult * res, int expected, int alt) const
+PQ::Connection::checkResult(PGresult * res, Expected... expected) const
{
- if (!checkResultInt(res, expected, alt)) {
+ if (const auto status = PQresultStatus(res); (... && (status != expected))) {
PQclear(res);
throw Error(conn);
}
diff --git a/libpqpp/pq-connection.h b/libpqpp/pq-connection.h
index af013ab..39f7f2a 100644
--- a/libpqpp/pq-connection.h
+++ b/libpqpp/pq-connection.h
@@ -47,13 +47,11 @@ namespace PQ {
void endBulkUpload(const char *) override;
size_t bulkUploadData(const char *, size_t) const override;
- ResultPtr checkResult(PGresult * res, int expected, int alternative = -1) const;
+ template<std::same_as<ExecStatusType>... Expected>
+ ResultPtr checkResult(PGresult * res, Expected... expected) const;
PGconn * conn;
mutable PreparedStatements preparedStatements;
-
- private:
- static bool checkResultInt(PGresult * res, int expected, int alternative);
};
}