exec as a separate function

This commit is contained in:
Dr. Patrick Urbanke
2025-04-26 15:27:48 +02:00
parent 07829bef8f
commit 4cad208fcd
5 changed files with 48 additions and 18 deletions

View File

@@ -69,21 +69,6 @@ Result<Nothing> Connection::end_write() {
return Nothing{};
}
Result<Ref<PGresult>> Connection::exec(const std::string& _sql) const noexcept {
const auto res = PQexec(conn_.get(), _sql.c_str());
const auto status = PQresultStatus(res);
if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK &&
status != PGRES_COPY_IN) {
const auto err = error(PQresultErrorMessage(res));
PQclear(res);
return err;
}
return Ref<PGresult>::make(std::shared_ptr<PGresult>(res, PQclear));
}
std::vector<std::string> Connection::get_primary_keys(
const dynamic::CreateTable& _stmt) const noexcept {
using namespace std::ranges::views;

View File

@@ -0,0 +1,26 @@
#include "sqlgen/postgres/exec.hpp"
#include <ranges>
#include <rfl.hpp>
#include <sstream>
#include <stdexcept>
namespace sqlgen::postgres {
Result<Ref<PGresult>> exec(const Ref<PGconn>& _conn,
const std::string& _sql) noexcept {
const auto res = PQexec(_conn.get(), _sql.c_str());
const auto status = PQresultStatus(res);
if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK &&
status != PGRES_COPY_IN) {
const auto err = error(PQresultErrorMessage(res));
PQclear(res);
return err;
}
return Ref<PGresult>::make(std::shared_ptr<PGresult>(res, PQclear));
}
} // namespace sqlgen::postgres

View File

@@ -1,2 +1,3 @@
#include "sqlgen/postgres/Connection.cpp"
#include "sqlgen/postgres/Iterator.cpp"
#include "sqlgen/postgres/exec.cpp"