summaryrefslogtreecommitdiff
path: root/lib/output/pq/pqConn.cpp
blob: 4f55ba8c914becd173d2a8a0ad9a8579fe11cff3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "pqConn.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)}
	{
		verify<std::runtime_error>(PQstatus(conn) == CONNECTION_OK, "Connection failure");
		PQsetNoticeProcessor(conn, notice_processor, this);
	}

	PqConn::~PqConn()
	{
		PQfinish(conn);
	}

	void
	PqConn::query(const char * const q)
	{
		ResPtr res {PQexec(conn, 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),
				&PQclear};
		verify<std::runtime_error>(PQresultStatus(res.get()) == PGRES_COMMAND_OK, q);
	}

	void
	PqConn::notice_processor(void * p, const char * n)
	{
		return static_cast<PqConn *>(p)->notice_processor(n);
	}

	void
	PqConn::notice_processor(const char *) const
	{
	}
}