summaryrefslogtreecommitdiff
path: root/lib/output/pq/pqBindings.h
blob: 2a4ac8cb61f865e8823dbacce835c2595d746e0b (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
#ifndef MYGRATE_OUTPUT_PQ_PQBINDINGS
#define MYGRATE_OUTPUT_PQ_PQBINDINGS

#include <compileTimeFormatter.h>
#include <dbTypes.h>
#include <helpers.h>
#include <initializer_list>
#include <streamSupport.h>
#include <variant>
#include <vector>

namespace MyGrate::Output::Pq {
	struct Bindings {
		explicit Bindings(const std::span<const DbValue> vs)
		{
			bufs.reserve(vs.size());
			values.reserve(vs.size());
			lengths.reserve(vs.size());
			formats.reserve(vs.size());
			for (const auto & v : vs) {
				v.visit(*this);
			}
		}
		template<Stringable T>
		void
		operator()(const T & v)
		{
			addBuf(std::to_string(v));
		}
		template<Viewable T>
		void
		operator()(const T & v)
		{
			values.emplace_back(v.data());
			lengths.emplace_back(v.size());
			formats.push_back(1);
		}
		template<typename T>
		void
		operator()(const T & v)
		{
			addBuf(scprintf<"%?">(v));
		}
		void
		operator()(const timespec &)
		{
			throw std::logic_error("Not implemented");
		}
		void
		operator()(const std::nullptr_t &)
		{
			values.emplace_back(nullptr);
			lengths.emplace_back(0);
			formats.push_back(0);
		}

		std::vector<std::string> bufs;
		std::vector<const char *> values;
		std::vector<int> lengths;
		std::vector<int> formats;

	private:
		void
		addBuf(std::string str)
		{
			const auto & vw = bufs.emplace_back(std::move(str));
			values.emplace_back(vw.data());
			lengths.emplace_back(vw.length());
			formats.push_back(0);
		}
	};
}

#endif