From 4cad208fcd57f1524312b4a9a4dbf4c416891ee7 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sat, 26 Apr 2025 15:27:48 +0200 Subject: [PATCH] exec as a separate function --- include/sqlgen/postgres/Connection.hpp | 5 ++--- include/sqlgen/postgres/exec.hpp | 19 +++++++++++++++++++ src/sqlgen/postgres/Connection.cpp | 15 --------------- src/sqlgen/postgres/exec.cpp | 26 ++++++++++++++++++++++++++ src/sqlgen_postgres.cpp | 1 + 5 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 include/sqlgen/postgres/exec.hpp create mode 100644 src/sqlgen/postgres/exec.cpp diff --git a/include/sqlgen/postgres/Connection.hpp b/include/sqlgen/postgres/Connection.hpp index d58ea53..e6cf72a 100644 --- a/include/sqlgen/postgres/Connection.hpp +++ b/include/sqlgen/postgres/Connection.hpp @@ -15,6 +15,7 @@ #include "../dynamic/Column.hpp" #include "../dynamic/Statement.hpp" #include "Credentials.hpp" +#include "exec.hpp" namespace sqlgen::postgres { @@ -33,7 +34,7 @@ class Connection : public sqlgen::Connection { Result commit() final { return execute("COMMIT;"); } Result execute(const std::string& _sql) noexcept final { - return exec(_sql).transform([](auto&&) { return Nothing{}; }); + return exec(conn_, _sql).transform([](auto&&) { return Nothing{}; }); } Result> read(const dynamic::SelectFrom& _query) final { @@ -61,8 +62,6 @@ class Connection : public sqlgen::Connection { std::string create_table_to_sql( const dynamic::CreateTable& _stmt) const noexcept; - Result> exec(const std::string& _sql) const noexcept; - static std::string get_name(const dynamic::Column& _col) { return _col.name; } std::vector get_primary_keys( diff --git a/include/sqlgen/postgres/exec.hpp b/include/sqlgen/postgres/exec.hpp new file mode 100644 index 0000000..d6a81ea --- /dev/null +++ b/include/sqlgen/postgres/exec.hpp @@ -0,0 +1,19 @@ +#ifndef SQLGEN_POSTGRES_EXEC_HPP_ +#define SQLGEN_POSTGRES_EXEC_HPP_ + +#include + +#include +#include + +#include "../Ref.hpp" +#include "../Result.hpp" + +namespace sqlgen::postgres { + +Result> exec(const Ref& _conn, + const std::string& _sql) noexcept; + +} // namespace sqlgen::postgres + +#endif diff --git a/src/sqlgen/postgres/Connection.cpp b/src/sqlgen/postgres/Connection.cpp index 92858d0..255f85d 100644 --- a/src/sqlgen/postgres/Connection.cpp +++ b/src/sqlgen/postgres/Connection.cpp @@ -69,21 +69,6 @@ Result Connection::end_write() { return Nothing{}; } -Result> 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::make(std::shared_ptr(res, PQclear)); -} - std::vector Connection::get_primary_keys( const dynamic::CreateTable& _stmt) const noexcept { using namespace std::ranges::views; diff --git a/src/sqlgen/postgres/exec.cpp b/src/sqlgen/postgres/exec.cpp new file mode 100644 index 0000000..26b50a5 --- /dev/null +++ b/src/sqlgen/postgres/exec.cpp @@ -0,0 +1,26 @@ +#include "sqlgen/postgres/exec.hpp" + +#include +#include +#include +#include + +namespace sqlgen::postgres { + +Result> exec(const Ref& _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::make(std::shared_ptr(res, PQclear)); +} + +} // namespace sqlgen::postgres diff --git a/src/sqlgen_postgres.cpp b/src/sqlgen_postgres.cpp index bca2e60..3bb9cab 100644 --- a/src/sqlgen_postgres.cpp +++ b/src/sqlgen_postgres.cpp @@ -1,2 +1,3 @@ #include "sqlgen/postgres/Connection.cpp" #include "sqlgen/postgres/Iterator.cpp" +#include "sqlgen/postgres/exec.cpp"