-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.h
210 lines (176 loc) · 6.75 KB
/
type.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
#ifndef COT_TYPE_H
#define COT_TYPE_H
// Classes containing the information about field types are held
// withing this file. The file is under development.
#include <cstring>
#include <string>
#include <stdint.h>
#include <mysql/mysql.h>
#include "cot/exception.h"
namespace cot {
class AbstractValue
{
public:
virtual ~AbstractValue() {}
virtual int get_mysql_in_type_code() const = 0;
virtual int get_mysql_out_type_code() const = 0;
virtual int get_max_length() const = 0;
virtual void bind(void *, int) = 0;
virtual bool is_bound() { return bound_; }
virtual void unbind() { bound_ = false; }
virtual void * get_bind_address() const { return bind_address_; }
virtual void * get_data_address() const { return data_address_; }
virtual int get_bind_length() const = 0;
virtual int get_data_length() const = 0;
virtual int get_max_data_length() const = 0;
protected:
void * bind_address_;
void * data_address_;
bool bound_;
};
template<class __CType, class __CppType, int __MysqlInType, int __MysqlOutType, int __MaxLength>
class Value: public AbstractValue
{
public:
typedef __CType ctype;
typedef __CppType cpptype;
static const int mysql_in_type_code = __MysqlInType;
static const int mysql_out_type_code = __MysqlOutType;
static const int max_length = __MaxLength;
virtual ~Value() {}
virtual int get_mysql_in_type_code() const {
return mysql_in_type_code;
}
virtual int get_mysql_out_type_code() const {
return mysql_out_type_code;
}
virtual int get_max_length() const {
return max_length;
}
};
#define GET_CPP_REPR(valuetype, value) \
((typename valuetype::cpptype)(*(typename valuetype::ctype *)(value->get_bind_address())))
class IntValue: public Value<int, int, MYSQL_TYPE_LONG, MYSQL_TYPE_LONG, sizeof(int)>
{
public:
static IntValue * from_ctype(int value) {
IntValue * result = new IntValue;
result->value_ = value;
return result;
}
static IntValue * from_cpptype(int value) {
IntValue * result = new IntValue;
result->value_ = value;
return result;
}
static IntValue * from_dump(void * mem) {
IntValue * result = new IntValue;
result->bind_address_ = mem;
result->data_address_ = mem;
result->value_ = *(int *)result->data_address_;
result->bound_ = true;
return result;
}
IntValue() {
value_ = 0;
bound_ = false;
}
virtual ~IntValue() {}
virtual void bind(void * mem, int bytes_available) {
if (bound_) {
throw new Exception("bind(): IntValue instance already bound");
}
if (bytes_available < get_bind_length()) {
throw new Exception("bind(): not enough memory passed to bind IntValue instance");
}
bind_address_ = mem;
data_address_ = mem;
*(int *)data_address_ = value_;
bound_ = true;
}
virtual int get_bind_length() const {
return max_length;
}
virtual int get_data_length() const {
return max_length;
}
virtual int get_max_data_length() const {
return max_length;
}
protected:
int value_;
};
// The auxiliary data includes null byte for C-string and the pointer
// to the begining of the actual string (in fact, the next byte after the
// pointer).
template<int __Length>
class StringValue: public Value<
char *,
std::string,
MYSQL_TYPE_STRING,
MYSQL_TYPE_VAR_STRING,
sizeof(char *) + __Length + 1>
{
public:
static StringValue<__Length> * from_ctype(char * value) {
StringValue<__Length> * result = new StringValue<__Length>;
result->value_ = std::string(value);
if ((int)result->value_.length() > StringValue<__Length>::max_length) {
throw new Exception("from_ctype(): "
"the passed value length is greater than the StringValue max_length parameter");
}
return result;
}
static StringValue<__Length> * from_cpptype(std::string value) {
StringValue<__Length> * result = new StringValue<__Length>;
result->value_ = value;
if ((int)result->value_.length() > StringValue<__Length>::max_length) {
throw new Exception("from_cpptype(): "
"the passed value length is greater than the StringValue max_length parameter");
}
return result;
}
static StringValue<__Length> * from_dump(void * mem) {
StringValue<__Length> * result = new StringValue<__Length>;
result->bind_address_ = mem;
result->data_address_ = (uint8_t *)mem + sizeof(char *);
result->value_ = std::string((char *)result->data_address_);
if ((int)result->value_.length() > StringValue<__Length>::max_length) {
throw new Exception("from_dump(): "
"the passed value length is greater than the StringValue max_length parameter");
}
result->bound_ = true;
return result;
}
StringValue() {
value_ = "";
this->bound_ = false;
}
virtual ~StringValue() {}
virtual void bind(void * mem, int bytes_available) {
if (this->bound_) {
throw new Exception("bind(): the StringValue instance is already bound");
}
if (bytes_available < get_bind_length()) {
throw new Exception("bind(): not enough memory passed to bind the StringValue instance");
}
this->bind_address_ = mem;
this->data_address_ = (uint8_t *)mem + sizeof(char *);
*(char **)this->bind_address_ = (char *)this->data_address_;
std::strncpy((char *)this->data_address_, value_.c_str(), get_data_length() + 1);
this->bound_ = true;
}
virtual int get_bind_length() const {
return StringValue<__Length>::max_length;
}
virtual int get_data_length() const {
return value_.length();
}
virtual int get_max_data_length() const {
return __Length;
}
protected:
std::string value_;
};
} // namespace cot
#endif // COT_TYPE_H