summaryrefslogtreecommitdiff
path: root/lib/mysql_types.cpp
blob: 7c530a7a3a7b386fa472bd8c0b18a47923017dbc (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "mysql_types.h"
#include "helpers.h"
#include "rawDataReader.h"
#include <iostream>
#include <stdexcept>

namespace MyGrate::MySQL {
	typename Type<MYSQL_TYPE_NULL, false>::C
	Type<MYSQL_TYPE_NULL, false>::read(RawDataReader &, RawDataReader &)
	{
		return nullptr;
	}

#define INTEGER_TYPE(ET) \
	typename Type<ET, false>::C Type<ET, false>::read(RawDataReader &, RawDataReader & data) \
	{ \
		return data.readValue<typename Type<ET>::C>(); \
	} \
	typename Type<ET, true>::C Type<ET, true>::read(RawDataReader &, RawDataReader & data) \
	{ \
		return data.readValue<typename Type<ET>::C>(); \
	}
	INTEGER_TYPE(MYSQL_TYPE_TINY);
	INTEGER_TYPE(MYSQL_TYPE_SHORT);
	INTEGER_TYPE(MYSQL_TYPE_LONG);
	INTEGER_TYPE(MYSQL_TYPE_LONGLONG);
#undef INTEGER_TYPE

#define FLOAT_TYPE(ET) \
	typename Type<ET, false>::C Type<ET, false>::read(RawDataReader & md, RawDataReader & data) \
	{ \
		verify<std::length_error>(sizeof(typename Type<ET>::C) == md.readValue<uint8_t>(), "Invalid " #ET " size"); \
		return data.readValue<typename Type<ET>::C>(); \
	}
	FLOAT_TYPE(MYSQL_TYPE_FLOAT);
	FLOAT_TYPE(MYSQL_TYPE_DOUBLE);
#undef FLOAT_TYPE

	typename Type<MYSQL_TYPE_DECIMAL>::C
	Type<MYSQL_TYPE_DECIMAL>::read(RawDataReader &, RawDataReader &)
	{
		throw std::logic_error("Not implemented");
	}

	typename Type<MYSQL_TYPE_NEWDECIMAL>::C
	Type<MYSQL_TYPE_NEWDECIMAL>::read(RawDataReader &, RawDataReader &)
	{
		throw std::logic_error("Not implemented");
	}

#define INTEGER_TYPE(ET, s, l) \
	typename Type<ET, s>::C Type<ET, s>::read(RawDataReader &, RawDataReader & data) \
	{ \
		return data.readValue<typename Type<ET, s>::C, l>(); \
	}
	INTEGER_TYPE(MYSQL_TYPE_INT24, false, 3);
	INTEGER_TYPE(MYSQL_TYPE_INT24, true, 3);
	INTEGER_TYPE(MYSQL_TYPE_YEAR, false, 2);
#undef INTEGER24_TYPE

	static Blob
	readBlob(RawDataReader & md, RawDataReader & data)
	{
		const auto lenlen {md.readValue<uint8_t>()};
		const auto len {data.readValue<uint64_t>(lenlen)};
		return data.viewValue<Blob>(len);
	}
#define BLOB_TYPE(ET) \
	typename Type<ET, false>::C Type<ET, false>::read(RawDataReader & md, RawDataReader & data) \
	{ \
		return readBlob(md, data); \
	}
	BLOB_TYPE(MYSQL_TYPE_TINY_BLOB);
	BLOB_TYPE(MYSQL_TYPE_MEDIUM_BLOB);
	BLOB_TYPE(MYSQL_TYPE_LONG_BLOB);
	BLOB_TYPE(MYSQL_TYPE_GEOMETRY); // Ummm, pass this to the target to handle?
#undef BLOB_TYPE

	typename Type<MYSQL_TYPE_BLOB, false>::C
	Type<MYSQL_TYPE_BLOB, false>::read(RawDataReader & md, RawDataReader & data)
	{
		const auto lenlen {md.readValue<uint8_t>()};
		const auto len {data.readValue<uint64_t>(lenlen)};
		return data.viewValue<std::string_view>(len);
	}

	typename Type<MYSQL_TYPE_BIT>::C
	Type<MYSQL_TYPE_BIT>::read(RawDataReader &, RawDataReader &)
	{
		throw std::logic_error("Not implemented");
	}

#define STRING_TYPE(ET) \
	typename Type<ET, false>::C Type<ET, false>::read(RawDataReader & md, RawDataReader & data) \
	{ \
		const auto realtype {md.readValue<enum_field_types, 1>()}; \
		const auto lenlen {md.readValue<uint8_t>()}; \
		switch (realtype) { \
			case MYSQL_TYPE_ENUM: { \
				return data.viewValue<std::string_view>(lenlen); \
				break; \
			} \
			default: \
				throw std::logic_error("Not implemented: sub-type: " + std::to_string(realtype)); \
				break; \
		} \
		throw std::logic_error("Didn't return a value: " + std::to_string(realtype)); \
	}
	STRING_TYPE(MYSQL_TYPE_STRING);
	STRING_TYPE(MYSQL_TYPE_JSON);
	STRING_TYPE(MYSQL_TYPE_SET);
	STRING_TYPE(MYSQL_TYPE_ENUM);
#undef STRING_TYPE

	typename Type<MYSQL_TYPE_VARCHAR>::C
	Type<MYSQL_TYPE_VARCHAR>::read(RawDataReader & md, RawDataReader & data)
	{
		const auto fieldlen {md.readValue<uint16_t>()};
		const auto len {data.readValue<uint16_t>(fieldlen > 255 ? 2 : 1)};
		return data.viewValue<std::string_view>(len);
	}

	typename Type<MYSQL_TYPE_VAR_STRING>::C
	Type<MYSQL_TYPE_VAR_STRING>::read(RawDataReader & md, RawDataReader & data)
	{
		const auto fieldlen {md.readValue<uint16_t>()};
		const auto len {md.readValue<uint16_t>(fieldlen > 255 ? 2 : 1)};
		return data.viewValue<std::string_view>(len);
	}

	typename Type<MYSQL_TYPE_DATETIME>::C
	Type<MYSQL_TYPE_DATETIME>::read(RawDataReader &, RawDataReader & data)
	{
		auto dtint {data.readValue<uint64_t>()};
		DateTime dt;
		dt.second = mod100_extract(dtint);
		dt.minute = mod100_extract(dtint);
		dt.hour = mod100_extract(dtint);
		dt.day = mod100_extract(dtint);
		dt.month = mod100_extract(dtint);
		dt.year = dtint;
		return dt;
	}

	typename Type<MYSQL_TYPE_TIME>::C
	Type<MYSQL_TYPE_TIME>::read(RawDataReader &, RawDataReader & data)
	{
		auto tint {data.readValue<uint32_t, 3>()};
		Time t;
		t.second = mod100_extract(tint);
		t.minute = mod100_extract(tint);
		t.hour = tint;
		return t;
	}

	typename Type<MYSQL_TYPE_TIME2>::C
	Type<MYSQL_TYPE_TIME2>::read(RawDataReader &, RawDataReader &)
	{
		throw std::logic_error("Not implemented");
	}

	typename Type<MYSQL_TYPE_DATE>::C
	Type<MYSQL_TYPE_DATE>::read(RawDataReader &, RawDataReader & data)
	{
		auto dint {data.readValue<uint32_t, 3>()};
		Date d;
		d.day = bitslice(dint, 0, 6);
		d.month = bitslice(dint, 6, 4);
		d.year = bitslice(dint, 10, 14);
		return d;
	}

	typename Type<MYSQL_TYPE_NEWDATE>::C
	Type<MYSQL_TYPE_NEWDATE>::read(RawDataReader &, RawDataReader &)
	{
		throw std::logic_error("Not implemented");
	}

	typename Type<MYSQL_TYPE_TIMESTAMP>::C
	Type<MYSQL_TYPE_TIMESTAMP>::read(RawDataReader &, RawDataReader & data)
	{
		return {data.readValue<int32_t>(), 0};
	}

	typename Type<MYSQL_TYPE_TIMESTAMP2>::C
	Type<MYSQL_TYPE_TIMESTAMP2>::read(RawDataReader & md, RawDataReader & data)
	{
		const auto decimals {md.readValue<uint8_t>()};
		return {data.readValue<int32_t>(), data.readValue<long>(decimals)};
	}

	typename Type<MYSQL_TYPE_DATETIME2>::C
	Type<MYSQL_TYPE_DATETIME2>::read(RawDataReader & md, RawDataReader & data)
	{
		md.discard(1);
		union {
			uint64_t i;
			std::array<uint8_t, 5> b;
		} r {};
		r.b = data.readValue<decltype(r.b)>();
		std::reverse(r.b.begin(), r.b.end());

		DateTime dt;
		dt.year = (bitslice(r.i, 22, 17) / 13);
		dt.month = (bitslice(r.i, 22, 17) % 13);
		dt.day = bitslice(r.i, 17, 5);
		dt.hour = bitslice(r.i, 12, 5);
		dt.minute = bitslice(r.i, 6, 6);
		dt.second = bitslice(r.i, 0, 6);
		return dt;
	}
}