-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathSqlResultSet.cpp
319 lines (278 loc) · 6.55 KB
/
SqlResultSet.cpp
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
#include "mysql.h"
#include "SqlResultSet.h"
#include "XLog.h"
#include "BasePacket.h"
static void allocateResultBuffer(MYSQL_BIND* bind, MYSQL_FIELD* field)
{
if (field)
{
bind->buffer_type = field->type;
bind->is_unsigned = field->flags & UNSIGNED_FLAG;
switch (bind->buffer_type)
{
case MYSQL_TYPE_TINY:
bind->buffer_length = 1;
bind->buffer = new char[bind->buffer_length];
break;
case MYSQL_TYPE_YEAR:
case MYSQL_TYPE_SHORT:
bind->buffer_length = 2;
bind->buffer = new char[bind->buffer_length];
break;
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG:
bind->buffer_length = 4;
bind->buffer = new char[bind->buffer_length];
break;
case MYSQL_TYPE_LONGLONG:
bind->buffer_length = 8;
bind->buffer = new char[bind->buffer_length];
break;
case MYSQL_TYPE_FLOAT:
bind->buffer_length = 4;
bind->buffer = new char[bind->buffer_length];
break;
case MYSQL_TYPE_DOUBLE:
bind->buffer_length = 8;
bind->buffer = new char[bind->buffer_length];
break;
case MYSQL_TYPE_NEWDECIMAL:
bind->buffer_length = 67;
bind->buffer = new char[bind->buffer_length];
break;
case MYSQL_TYPE_TIME:
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
bind->buffer_length = sizeof(MYSQL_TIME);
bind->buffer = new char[bind->buffer_length];
break;
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_VAR_STRING:
bind->buffer_length = field->length;
bind->buffer = new char[bind->buffer_length];
break;
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
bind->buffer_length = field->max_length;
bind->buffer = new char[bind->buffer_length];
break;
case MYSQL_TYPE_BIT:
bind->buffer_length = 1;
bind->buffer = new char[bind->buffer_length];
break;
default:
ERROR_LOG("allocateResultBuffer() failed: Unknown type for the result.");
}
}
else
{
bind->buffer_type = MYSQL_TYPE_NULL;
}
}
SqlResultSet::SqlResultSet():
m_currRowIdx(-1), m_currColIdx(0),m_rowCount(0),m_fieldCount(0)
{
}
SqlResultSet::~SqlResultSet()
{
}
void SqlResultSet::setResult(MYSQL_STMT* stmt, MYSQL_RES* pResult, uint64 pRowCount, uint32 pFieldCount)
{
m_rowCount = pRowCount;
m_fieldCount = pFieldCount;
if (pRowCount == 0) return;
MYSQL_BIND* resultBind = new MYSQL_BIND[pFieldCount];
unsigned long* resultLengths = new unsigned long[pFieldCount];
my_bool* isNulls = new my_bool[pFieldCount];
memset(resultBind, 0, sizeof(MYSQL_BIND) * pFieldCount);
uint32 nIndex = 0;
MYSQL_FIELD* pField = NULL;
while ((pField = mysql_fetch_field(pResult)))
{
allocateResultBuffer(&resultBind[nIndex], pField);
resultBind[nIndex].length = &resultLengths[nIndex];
resultBind[nIndex].is_null = &isNulls[nIndex];
nIndex++;
}
if (!mysql_stmt_bind_result(stmt, resultBind))
{
m_fields.resize(uint32(pRowCount) * pFieldCount);
// Ìî³ä×Ö¶ÎÊý¾Ý
int rowIndex = 0;
while (_fetch(stmt))
{
for (uint32 fIndex = 0; fIndex < pFieldCount; ++fIndex)
{
const char* pdata = NULL;
int len = 0;
if (!*resultBind[fIndex].is_null)
{
pdata = (const char*)resultBind[fIndex].buffer;
len = *resultBind[fIndex].length;
}
m_fields[rowIndex * pFieldCount + fIndex].make(pdata, len);
}
rowIndex++;
}
}
// delete
for (int i = 0; i< pFieldCount; ++i)
{
MYSQL_BIND* pBind = &resultBind[i];
if(pBind->buffer) delete [] pBind->buffer;
}
delete[] resultBind;
delete[] resultLengths;
delete[] isNulls;
}
bool SqlResultSet::fetch()
{
if (m_currRowIdx + 1 >= m_rowCount)
{
return false;
}
m_currRowIdx++;
m_currColIdx = 0;
return true;
}
bool SqlResultSet::isEmpty()
{
return m_rowCount == 0;
}
uint32 SqlResultSet::getRowCount()
{
return m_rowCount;
}
uint32 SqlResultSet::getFieldsCount()
{
return m_fieldCount;
}
const char* SqlResultSet::getData(int& len)
{
SqlField* field = getField(m_currColIdx++);
if (!field) return NULL;
len = field->length();
return field->data();
}
const char* SqlResultSet::getData()
{
SqlField* field = getField(m_currColIdx++);
if (!field) return NULL;
return field->data();
}
bool SqlResultSet::_fetch(MYSQL_STMT* stmt)
{
switch (mysql_stmt_fetch(stmt))
{
case 0:
case MYSQL_DATA_TRUNCATED:
return true;
case MYSQL_NO_DATA:
return false;
case 1: // the error
default:
ERROR_LOG(mysql_stmt_error(stmt));
}
return false;
}
SqlField* SqlResultSet::getField(int idx)
{
if (m_currRowIdx < 0 || m_currRowIdx >= m_rowCount || idx >= m_fieldCount)
{
return NULL;
}
SqlField* field = &m_fields[m_currRowIdx * m_fieldCount + idx];
if (field->isEmpty())
{
return NULL;
}
return field;
}
bool SqlResultSet::emptyField(int idx)
{
return getField(idx);
}
int8 SqlResultSet::getInt8()
{
SqlField* field = getField(m_currColIdx++);
if (!field) return 0;
return field->getValue<int8>();
}
uint8 SqlResultSet::getUint8()
{
SqlField* field = getField(m_currColIdx++);
if (!field) return 0;
return field->getValue<uint8>();
}
int16 SqlResultSet::getInt16()
{
SqlField* field = getField(m_currColIdx++);
if (!field) return 0;
return field->getValue<int16>();
}
uint16 SqlResultSet::getUint16()
{
SqlField* field = getField(m_currColIdx++);
if (!field) return 0;
return field->getValue<uint16>();
}
int32 SqlResultSet::getInt32()
{
SqlField* field = getField(m_currColIdx++);
if (!field) return 0;
return field->getValue<int32>();
}
uint32 SqlResultSet::getUint32()
{
SqlField* field = getField(m_currColIdx++);
if (!field) return 0;
return field->getValue<uint32>();
}
int64 SqlResultSet::getInt64()
{
SqlField* field = getField(m_currColIdx++);
if (!field) return 0;
return field->getValue<int64>();
}
uint64 SqlResultSet::getUint64()
{
SqlField* field = getField(m_currColIdx++);
if (!field) return 0;
return field->getValue<uint64>();
}
float SqlResultSet::getFloat()
{
SqlField* field = getField(m_currColIdx++);
if (!field) return 0.0f;
return field->getValue<float>();
}
double SqlResultSet::getDouble()
{
SqlField* field = getField(m_currColIdx++);
if (!field) return 0.0f;
return field->getValue<double>();
}
std::string SqlResultSet::getString()
{
SqlField* field = getField(m_currColIdx++);
if (!field) return "";
std::string val(field->data(), field->length());
return val;
}
int SqlResultSet::readBlob(BasePacket* packet)
{
SqlField* field = getField(m_currColIdx++);
if (!field) return 0;
packet->append(field->data(), field->length());
return field->length();
}
std::string_view SqlResultSet::getStrview() // lua call
{
SqlField* field = getField(m_currColIdx++);
if (!field) return "";
std::string_view val(field->data(), field->length());
return val;
}