mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-06 01:19:58 -06:00
exec as a separate function
This commit is contained in:
@@ -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;
|
||||
|
||||
26
src/sqlgen/postgres/exec.cpp
Normal file
26
src/sqlgen/postgres/exec.cpp
Normal 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
|
||||
@@ -1,2 +1,3 @@
|
||||
#include "sqlgen/postgres/Connection.cpp"
|
||||
#include "sqlgen/postgres/Iterator.cpp"
|
||||
#include "sqlgen/postgres/exec.cpp"
|
||||
|
||||
Reference in New Issue
Block a user