From ef89db97d3acf1b3573ff6097ff8eb4288c845da Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 8 Oct 2023 17:23:41 +0100 Subject: Simplify checkResult Becomes template for however many acceptable status there are and tests as a fold expression. --- libpqpp/pq-connection.cpp | 11 +++-------- libpqpp/pq-connection.h | 6 ++---- 2 files changed, 5 insertions(+), 12 deletions(-) (limited to 'libpqpp') 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(this, sql, opts); } -bool -PQ::Connection::checkResultInt(PGresult * res, int expected, int alt) -{ - return (PQresultStatus(res) == expected) || (alt != -1 && (PQresultStatus(res) == alt)); -} - +template... 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... Expected> + ResultPtr checkResult(PGresult * res, Expected... expected) const; PGconn * conn; mutable PreparedStatements preparedStatements; - - private: - static bool checkResultInt(PGresult * res, int expected, int alternative); }; } -- cgit v1.2.3