forked from mambaru/libslave
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfield.h
325 lines (260 loc) · 7.94 KB
/
field.h
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* Copyright 2011 ZAO "Begun".
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SLAVE_FIELD_H_
#define __SLAVE_FIELD_H_
#include <string>
#include <vector>
#include <list>
#include "collate.h"
#include "types.h"
// conflict with macro defined in mysql
#ifdef test
#undef test
#endif /* test */
#include <my_time.h>
#include <m_string.h>
#include <binary_log_funcs.h>
#include <decimal.h>
#include "Logging.h"
namespace slave
{
// ----- base --------------------------------------------------------------------------------------
class Field
{
public:
const std::string field_name;
const std::string field_type;
boost::any field_data;
Field(const std::string& name, const std::string& type) :
field_name(name), field_type(type)
{}
virtual ~Field() {}
virtual const char* unpack(const char *from) = 0;
virtual void unpack_str(const std::string& from) {
field_data = from;
}
const std::string& getFieldName() const {
return field_name;
}
};
// ----- numbers -----------------------------------------------------------------------------------
template<typename T, const unsigned length = sizeof(T)>
class Field_num : public Field
{
using Field::Field;
public:
const char* unpack(const char* from);
void unpack_str(const std::string& from);
private:
inline T get_value(const char *from);
};
template class Field_num<uint16, 1>;
template class Field_num<uint16>;
template class Field_num<uint32, 3>;
template class Field_num<uint32>;
template class Field_num<ulonglong>;
template class Field_num<int16, 1>;
template class Field_num<int16>;
template class Field_num<int32, 3>;
template class Field_num<int32>;
template class Field_num<longlong>;
template class Field_num<float>;
template class Field_num<double>;
class Field_decimal : public Field
{
public:
Field_decimal(
const std::string& name,
const std::string& type,
const unsigned length_,
const unsigned scale_,
const bool is_unsigned
) :
Field(name, type),
scale(scale_),
// see my_decimal_length_to_precision() @ my_decimal.h
precision(length_ - (scale_ ? 1 : 0) - (is_unsigned || !length_ ? 0 : 1)),
length(::decimal_bin_size(precision, scale_))
{}
const char* unpack(const char *from);
private:
const unsigned scale, precision, length;
static const bool zerofill = false;
};
// ----- date & time -------------------------------------------------------------------------------
class Field_temporal : public Field
{
public:
Field_temporal(
const std::string& name,
const std::string& type,
const unsigned precision_
) :
Field(name, type),
precision(precision_)
{}
// set length
virtual void reset(const bool is_old_storage_, const bool ctor_call = false) = 0;
protected:
bool is_old_storage;
const unsigned precision;
unsigned length;
};
class Field_timestamp : public Field_temporal
{
public:
Field_timestamp(
const std::string& name,
const std::string& type,
const unsigned precision,
const bool is_old_storage_
) :
Field_temporal(name, type, precision)
{
reset(is_old_storage_, true);
}
const char* unpack(const char* from);
void reset(const bool is_old_storage_, const bool ctor_call);
};
class Field_time : public Field_temporal
{
public:
Field_time(
const std::string& name,
const std::string& type,
const unsigned precision,
const bool is_old_storage_
) :
Field_temporal(name, type, precision)
{
reset(is_old_storage_, true);
}
const char* unpack(const char* from);
void reset(const bool is_old_storage_, const bool ctor_call);
};
class Field_datetime : public Field_temporal
{
public:
Field_datetime(
const std::string& name,
const std::string& type,
const unsigned precision,
const bool is_old_storage_
) :
Field_temporal(name, type, precision)
{
reset(is_old_storage_, true);
}
const char* unpack(const char* from);
void reset(const bool is_old_storage_, const bool ctor_call);
};
class Field_date : public Field
{
using Field::Field;
public:
const char* unpack(const char* from);
};
class Field_year : public Field
{
using Field::Field;
public:
const char* unpack(const char* from);
void unpack_str(const std::string& from);
};
// ----- string ------------------------------------------------------------------------------------
class Field_string : public Field
{
public:
Field_string(
const std::string& name,
const std::string& type,
const unsigned length_
) :
Field(name, type),
length(length_)
{}
const char* unpack(const char* from);
void set_length(const unsigned x) {
LOG_TRACE(log, "field " << field_name << " new string length: " << x);
length = x;
}
private:
unsigned length;
};
// ----- enums -------------------------------------------------------------------------------------
class Field_bitset : public Field
{
public:
Field_bitset(const std::string& name, const std::string& type);
protected:
unsigned length;
std::vector<std::string> str_values;
};
class Field_enum : public Field_bitset
{
public:
Field_enum(const std::string& name, const std::string& type) :
Field_bitset(name, type)
{
// see get_enum_pack_length() @ field.h
length = str_values.size() < 256 ? 1 : 2;
}
const char* unpack(const char* from);
};
class Field_set: public Field_bitset
{
public:
Field_set(const std::string& name, const std::string& type) :
Field_bitset(name, type)
{
// see get_set_pack_length() @ field.h
const unsigned len = (str_values.size() + 7) / 8;
length = len > 4 ? 8 : len;
}
const char* unpack(const char* from);
};
class Field_bit : public Field
{
public:
Field_bit(
const std::string& name,
const std::string& type,
const unsigned length_
) :
Field(name, type),
length(length_)
{}
const char* unpack(const char *from);
void unpack_str(const std::string& from);
private:
const unsigned length;
};
// ----- blob --------------------------------------------------------------------------------------
class Field_blob : public Field {
public:
Field_blob(
const std::string& name,
const std::string& type,
const unsigned length
);
const char* unpack(const char* from);
void set_size(const unsigned x) {
LOG_TRACE(log, "field " << field_name << " new blob size: " << x);
size = x;
}
private:
unsigned size;
};
}
#endif