added postgres backend

This commit is contained in:
2023-11-25 12:33:51 +01:00
parent c2a96f4cbd
commit 825e530a8d
20 changed files with 547 additions and 65 deletions
+34
View File
@@ -0,0 +1,34 @@
#include "postgres_error.hpp"
#include <sstream>
#include <libpq-fe.h>
namespace matador::backends::postgres {
void throw_postgres_error(const char *what, const std::string &source)
{
std::stringstream msg;
msg << "postgres error (" << source << "): " << what;
throw std::logic_error(msg.str());
}
void throw_postgres_error(PGconn *db, const std::string &source)
{
if (PQstatus(db) == CONNECTION_BAD) {
throw_postgres_error(PQerrorMessage(db), source);
}
}
void throw_postgres_error(PGresult *res, PGconn *db, const std::string &source, const std::string &sql)
{
if (res == nullptr ||
(PQresultStatus(res) != PGRES_COMMAND_OK &&
PQresultStatus(res) != PGRES_TUPLES_OK)) {
std::stringstream msg;
msg << "postgres error (" << source << ", " << PQresultErrorField(res, PG_DIAG_SQLSTATE) << ") " << PQerrorMessage(db) << ": " << sql;
throw std::logic_error(msg.str());
}
}
}