forked from Begun/libslave
-
Notifications
You must be signed in to change notification settings - Fork 13
/
field.h
289 lines (213 loc) · 7.17 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
/* 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 */
namespace slave
{
class Field
{
public:
const std::string field_type;
const std::string field_name;
FieldValue field_data;
virtual const char* unpack(const char *from) = 0;
Field(const std::string& field_name_arg, const std::string& type) :
field_type(type),
field_name(field_name_arg)
{}
virtual ~Field() {}
virtual unsigned int pack_length() const = 0;
const std::string getFieldName() {
return field_name;
}
};
class Field_num: public Field {
public:
Field_num(const std::string& field_name_arg, const std::string& type);
};
class Field_str: public Field {
public:
Field_str(const std::string& field_name_arg, const std::string& type);
};
class Field_longstr: public Field_str {
protected:
unsigned int field_length;
public:
Field_longstr(const std::string& field_name_arg, const std::string& type);
unsigned int pack_length() const {
return field_length;
}
};
class Field_real: public Field_num {
public:
Field_real(const std::string& field_name_arg, const std::string& type);
};
class Field_tiny: public Field_num {
unsigned int pack_length() const { return 1; }
public:
Field_tiny(const std::string& field_name_arg, const std::string& type);
const char* unpack(const char* from);
};
class Field_short: public Field_num {
unsigned int pack_length() const { return 2; }
public:
Field_short(const std::string& field_name_arg, const std::string& type);
const char* unpack(const char* from);
};
class Field_medium: public Field_num {
unsigned int pack_length() const { return 3; }
public:
Field_medium(const std::string& field_name_arg, const std::string& type);
const char* unpack(const char* from);
};
class Field_long: public Field_num {
unsigned int pack_length() const { return 4; }
public:
Field_long(const std::string& field_name_arg, const std::string& type);
const char* unpack(const char* from);
};
class Field_longlong: public Field_num {
unsigned int pack_length() const { return 8; }
public:
Field_longlong(const std::string& field_name_arg, const std::string& type);
const char* unpack(const char* from);
};
class Field_float: public Field_real {
unsigned int pack_length() const { return sizeof(float); }
public:
Field_float(const std::string& field_name_arg, const std::string& type);
const char* unpack(const char* from);
};
class Field_double: public Field_real {
unsigned int pack_length() const { return sizeof(double); }
public:
Field_double(const std::string& field_name_arg, const std::string& type);
const char* unpack(const char* from);
};
class Field_temporal: public Field_longstr {
protected:
bool is_old_storage;
public:
Field_temporal(const std::string& field_name_arg, const std::string& type, bool old_storage);
virtual ~Field_temporal() {}
virtual void reset(bool old_storage, bool ctor_call = false) = 0;
};
class Field_timestamp: public Field_temporal {
public:
Field_timestamp(const std::string& field_name_arg, const std::string& type, bool old_storage);
void reset(bool old_storage, bool ctor_call = false);
const char* unpack(const char* from);
};
class Field_year: public Field_tiny {
public:
Field_year(const std::string& field_name_arg, const std::string& type);
};
class Field_date: public Field_str {
unsigned int pack_length() const { return 3; }
public:
Field_date(const std::string& field_name_arg, const std::string& type);
const char* unpack(const char* from);
};
class Field_time: public Field_temporal {
public:
Field_time(const std::string& field_name_arg, const std::string& type, bool old_storage);
void reset(bool old_storage, bool ctor_call = false);
const char* unpack(const char* from);
};
class Field_datetime: public Field_temporal {
public:
Field_datetime(const std::string& field_name_arg, const std::string& type, bool old_storage);
void reset(bool old_storage, bool ctor_call = false);
const char* unpack(const char* from);
};
class Field_varstring: public Field_longstr {
// How many bytes are needed for holding the length
unsigned int length_bytes;
unsigned int pack_length() const { return (unsigned int) field_length+length_bytes; }
public:
Field_varstring(const std::string& field_name_arg, const std::string& type,
const collate_info& collate);
const char* unpack(const char* from);
};
class Field_blob: public Field_longstr {
unsigned int get_length(const char *ptr);
public:
Field_blob(const std::string& field_name_arg, const std::string& type);
const char* unpack(const char* from);
protected:
// Number of bytes for holding the data length
unsigned int packlength;
};
class Field_tinyblob: public Field_blob {
public:
Field_tinyblob(const std::string& field_name_arg, const std::string& type);
};
class Field_mediumblob: public Field_blob {
public:
Field_mediumblob(const std::string& field_name_arg, const std::string& type);
};
class Field_longblob: public Field_blob {
public:
Field_longblob(const std::string& field_name_arg, const std::string& type);
};
class Field_enum: public Field_str {
unsigned int pack_length() const {
return (unsigned int)(count_elements < 255) ? 1 : 2;
}
public:
Field_enum(const std::string& field_name_arg, const std::string& type);
const char* unpack(const char* from);
protected:
unsigned int packlength;
// Number of elements in enum
unsigned short count_elements;
};
class Field_set: public Field_enum {
unsigned int pack_length() const {
unsigned int x = (unsigned int) ((count_elements + 7)/8);
x = (x > 4 ? 8 : x);
return x;
}
public:
Field_set(const std::string& field_name_arg, const std::string& type);
const char* unpack(const char* from);
};
class Field_decimal : public Field_longstr {
int intg;
int frac;
public:
Field_decimal(const std::string& field_name_arg, const std::string& type);
const char* unpack(const char *from);
};
class Field_bit : public Field
{
unsigned int _pack_length;
public:
Field_bit(const std::string& field_name_arg, const std::string& type);
const char* unpack(const char *from);
unsigned int pack_length() const {
return _pack_length;
}
};
}
#endif