summaryrefslogtreecommitdiff
path: root/lib/output/pq/pqConn.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/output/pq/pqConn.cpp')
-rw-r--r--lib/output/pq/pqConn.cpp73
1 files changed, 14 insertions, 59 deletions
diff --git a/lib/output/pq/pqConn.cpp b/lib/output/pq/pqConn.cpp
index 4f55ba8..81d9610 100644
--- a/lib/output/pq/pqConn.cpp
+++ b/lib/output/pq/pqConn.cpp
@@ -1,89 +1,44 @@
#include "pqConn.h"
+#include "pqBindings.h"
+#include "pqStmt.h"
+#include <dbConn.h>
#include <dbTypes.h>
#include <helpers.h>
#include <libpq-fe.h>
#include <memory>
-#include <sstream>
#include <stdexcept>
#include <string>
-#include <variant>
#include <vector>
namespace MyGrate::Output::Pq {
- using ResPtr = std::unique_ptr<PGresult, decltype(&PQclear)>;
-
- PqConn::PqConn(const char * const str) : conn {PQconnectdb(str)}
+ PqConn::PqConn(const char * const str) : conn {PQconnectdb(str), PQfinish}
{
- verify<std::runtime_error>(PQstatus(conn) == CONNECTION_OK, "Connection failure");
- PQsetNoticeProcessor(conn, notice_processor, this);
- }
-
- PqConn::~PqConn()
- {
- PQfinish(conn);
+ verify<std::runtime_error>(PQstatus(conn.get()) == CONNECTION_OK, "Connection failure");
+ PQsetNoticeProcessor(conn.get(), notice_processor, this);
}
void
PqConn::query(const char * const q)
{
- ResPtr res {PQexec(conn, q), &PQclear};
+ ResPtr res {PQexec(conn.get(), q), &PQclear};
verify<std::runtime_error>(PQresultStatus(res.get()) == PGRES_COMMAND_OK, q);
}
- struct Bindings {
- // NOLINTNEXTLINE(hicpp-explicit-conversions)
- explicit Bindings(const std::initializer_list<DbValue> & vs)
- {
- bufs.reserve(vs.size());
- values.reserve(vs.size());
- lengths.reserve(vs.size());
- for (const auto & v : vs) {
- std::visit(*this, v);
- }
- }
- template<Stringable T>
- void
- operator()(const T & v)
- {
- bufs.emplace_back(std::to_string(v));
- const auto & vw {bufs.back()};
- values.emplace_back(vw.data());
- lengths.emplace_back(vw.length());
- }
- template<Viewable T>
- void
- operator()(const T & v)
- {
- values.emplace_back(v.data());
- lengths.emplace_back(v.size());
- }
- template<typename T>
- void
- operator()(const T &)
- {
- throw std::runtime_error("Not implemented");
- }
- void
- operator()(const std::nullptr_t &)
- {
- values.emplace_back(nullptr);
- lengths.emplace_back(0);
- }
-
- std::vector<std::string> bufs;
- std::vector<const char *> values;
- std::vector<int> lengths;
- };
-
void
PqConn::query(const char * const q, const std::initializer_list<DbValue> & vs)
{
Bindings b {vs};
- ResPtr res {PQexecParams(conn, q, (int)vs.size(), nullptr, b.values.data(), b.lengths.data(), nullptr, 0),
+ ResPtr res {PQexecParams(conn.get(), q, (int)vs.size(), nullptr, b.values.data(), b.lengths.data(), nullptr, 0),
&PQclear};
verify<std::runtime_error>(PQresultStatus(res.get()) == PGRES_COMMAND_OK, q);
}
+ DbPrepStmtPtr
+ PqConn::prepare(const char * const q, std::size_t n)
+ {
+ return std::make_unique<PqPrepStmt>(q, n, this);
+ }
+
void
PqConn::notice_processor(void * p, const char * n)
{