summaryrefslogtreecommitdiff
path: root/lib/input/mysqlBindings.h
blob: ccf876e92da1643736ec2997803bbd5703898137 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef MYGRATE_INPUT_MYSQLBINDINGS_H
#define MYGRATE_INPUT_MYSQLBINDINGS_H

#include <dbTypes.h>
#include <helpers.h>
#include <initializer_list>
#include <mysql.h>
#include <mysql_types.h>
#include <variant>
#include <vector>

namespace MyGrate::Input {
	struct BingingData {
		explicit BingingData(unsigned long l, my_bool n = 0) : len {l}, null {n} { }
		unsigned long len;
		my_bool null;
	};

	struct Bindings {
		explicit Bindings(const std::span<const DbValue> vs)
		{
			binds.reserve(vs.size());
			data.reserve(vs.size());
			for (const auto & v : vs) {
				v.visit(*this);
			}
		}
		template<std::integral T>
		void
		operator()(const T & v)
		{
			auto & b = binds.emplace_back();
			b.buffer_type = MySQL::CType<T>::type;
			b.buffer = const_cast<T *>(&v);
			b.is_unsigned = std::unsigned_integral<T>;
		}
		template<std::floating_point T>
		void
		operator()(const T & v)
		{
			auto & b = binds.emplace_back();
			b.buffer_type = MySQL::CType<T>::type;
			b.buffer = const_cast<T *>(&v);
		}
		template<Viewable T>
		void
		operator()(const T & v)
		{
			auto & b = binds.emplace_back();
			b.buffer_type = MySQL::CType<T>::type;
			b.buffer = const_cast<typename T::value_type *>(v.data());
			b.length = &data.emplace_back(v.size(), 0).len;
		}
		void
		operator()(const std::nullptr_t &)
		{
			auto & b = binds.emplace_back();
			b.buffer = nullptr;
			b.is_null = &data.emplace_back(0, 1).null;
		}
		template<typename T>
		void
		operator()(const T &)
		{
			throw std::logic_error("Not implemented");
		}
		std::vector<MYSQL_BIND> binds;
		std::vector<BingingData> data;
	};

	class ResultData : public BingingData {
	public:
		ResultData() : BingingData {0} { }
		virtual ~ResultData() = default;

		[[nodiscard]] virtual DbValue getValue() const = 0;
	};

	template<typename T> class ResultDataT : public ResultData {
	public:
		ResultDataT(MYSQL_BIND & b, const MYSQL_FIELD & f)
		{
			b.buffer = &buf;
			b.buffer_length = sizeof(T);
			b.is_null = &this->null;
			b.length = &this->len;
			b.is_unsigned = std::is_unsigned_v<T>;
			b.buffer_type = f.type;
		}

		[[nodiscard]] DbValue
		getValue() const override
		{
			return buf;
		}

	private:
		T buf {};
	};

	template<> class ResultDataT<std::string_view> : public ResultData {
	public:
		ResultDataT(MYSQL_BIND & b, const MYSQL_FIELD & f) : buf(f.length)
		{
			b.buffer_length = buf.size();
			b.buffer = buf.data();
			b.is_null = &this->null;
			b.length = &this->len;
			b.buffer_type = f.type;
		}

		[[nodiscard]] DbValue
		getValue() const override
		{
			return std::string_view {buf.data(), this->len};
		}

	private:
		std::vector<char> buf;
	};

	template<> class ResultDataT<Blob> : public ResultData {
	public:
		ResultDataT(MYSQL_BIND & b, const MYSQL_FIELD & f) : buf(f.length)
		{
			b.buffer_length = buf.size();
			b.buffer = buf.data();
			b.is_null = &this->null;
			b.length = &this->len;
			b.buffer_type = f.type;
		}

		[[nodiscard]] DbValue
		getValue() const override
		{
			return Blob {buf.data(), this->len};
		}

	private:
		std::vector<std::byte> buf;
	};

	template<typename Out> class ResultDataTime : public ResultData {
	public:
		ResultDataTime(MYSQL_BIND & b, const MYSQL_FIELD & f)
		{
			b.buffer_length = sizeof(MYSQL_TIME);
			b.buffer = &buf;
			b.is_null = &this->null;
			b.length = &this->len;
			b.buffer_type = f.type;
		}

		[[nodiscard]] DbValue
		getValue() const override
		{
			return Out {*this};
		}

	private:
		operator Date() const
		{
			return Date(buf.year, buf.month, buf.day);
		}
		operator Time() const
		{
			return Time(buf.hour, buf.minute, buf.second);
		}
		operator DateTime() const
		{
			return DateTime(*this, *this);
		}
		MYSQL_TIME buf;
	};
}

#endif