-
Notifications
You must be signed in to change notification settings - Fork 13
/
cgo.c
373 lines (305 loc) · 9.68 KB
/
cgo.c
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "cgo.h"
#include <stdio.h>
#include <string.h>
// All functions must call mysql_thread_init before calling mysql. This is
// because the go runtime controls thread creation, and we don't control
// which thread these functions will be called from.
// this macro produces a compilation-time check for a condition
// if the condition is different than zero, this will abort
// if the condition is zero, this won't generate any code
// (this was imported from Linux kernel source tree)
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
void my_library_init(void) {
// we depend on linking with the 64 bits version of the MySQL library:
// the go code depends on mysql_fetch_lengths() returning 64 bits unsigned.
BUILD_BUG_ON(sizeof(unsigned long) - 8);
mysql_library_init(0, 0, 0);
}
int my_open(
MYSQL *mysql,
const char *host,
const char *user,
const char *passwd,
const char *db,
unsigned int port,
const char *unix_socket,
unsigned long client_flag
) {
mysql_thread_init();
mysql_init(mysql);
if (!mysql_real_connect(mysql, host, user, passwd, db, port, unix_socket, client_flag)) {
return 1;
}
return 0;
}
void my_close(MYSQL *mysql) {
mysql_thread_init();
mysql_close(mysql);
}
unsigned long my_thread_id(MYSQL *mysql) {
mysql_thread_init();
return mysql_thread_id(mysql);
}
unsigned int my_errno(MYSQL *mysql) {
mysql_thread_init();
return mysql_errno(mysql);
}
const char *my_error(MYSQL *mysql) {
mysql_thread_init();
return mysql_error(mysql);
}
int my_autocommit(MYSQL *mysql, my_bool mode) {
mysql_thread_init();
return mysql_autocommit(mysql, mode);
}
int my_commit(MYSQL *mysql) {
mysql_thread_init();
return mysql_commit(mysql);
}
int my_rollback(MYSQL *mysql) {
mysql_thread_init();
return mysql_rollback(mysql);
}
unsigned long my_real_escape_string(MYSQL *mysql, char *to, const char *from, unsigned long length) {
mysql_thread_init();
return mysql_real_escape_string(mysql, to, from, length);
}
int my_query(MYSQL *mysql, MY_RES *res, const char *sql_str, unsigned long sql_len, MY_MODE mode) {
mysql_thread_init();
if (mysql_real_query(mysql, sql_str, sql_len) != 0) {
return 1;
}
if (mode != MY_MODE_NONE) {
if (mode == MY_MODE_TABLE) {
res->result = mysql_store_result(mysql);
} else {
res->result = mysql_use_result(mysql);
}
if (res->result == NULL) {
return 1;
}
res->meta.num_fields = mysql_num_fields(res->result);
res->meta.fields = mysql_fetch_fields(res->result);
}
res->affected_rows = mysql_affected_rows(mysql);
res->insert_id = mysql_insert_id(mysql);
return 0;
}
MY_ROW my_fetch_next(MYSQL *mysql, MY_RES *res) {
MY_ROW row = {0, 0, 0};
if(res->meta.num_fields == 0) {
return row;
}
mysql_thread_init();
row.mysql_row = mysql_fetch_row(res->result);
if (!row.mysql_row) {
if(mysql_errno(mysql)) {
row.has_error = 1;
return row;
}
} else {
row.lengths = mysql_fetch_lengths(res->result);
}
return row;
}
void my_close_result(MYSQL *mysql, MY_RES *res) {
MYSQL_RES *result;
mysql_thread_init();
if (res->result) {
mysql_free_result(res->result);
}
// Ignore subsequent results if any. We only
// return the first set of results for now.
while (mysql_next_result(mysql) == 0) {
result = mysql_store_result(mysql);
if (result) {
// while(mysql_fetch_row(result)) {
// }
mysql_free_result(result);
}
}
}
int my_prepare(MY_STMT **stmt, MYSQL_BIND **binds, MYSQL *mysql, const char *sql_str, unsigned long sql_len) {
mysql_thread_init();
*stmt = (MY_STMT*)calloc(1, sizeof(MY_STMT));
MY_STMT* s = *stmt;
s->s = mysql_stmt_init(mysql);
if (mysql_stmt_prepare(s->s, sql_str, sql_len) != 0) {
return 1;
}
s->param_count = mysql_stmt_param_count(s->s);
*binds = (MYSQL_BIND*)calloc(s->param_count, sizeof(MYSQL_BIND));
return 0;
}
int my_stmt_errno(MY_STMT *stmt) {
mysql_thread_init();
return mysql_stmt_errno(stmt->s);
}
const char *my_stmt_error(MY_STMT *stmt) {
mysql_thread_init();
return mysql_stmt_error(stmt->s);
}
int my_stmt_execute(MY_STMT *stmt, MYSQL_BIND *binds, MY_STMT_RES *res, MY_MODE mode) {
mysql_thread_init();
if (binds != NULL) {
if (mysql_stmt_bind_param(stmt->s, binds) != 0) {
return 1;
}
}
if (mode != MY_MODE_NONE && stmt->meta_init == 0) {
stmt->meta_init = 1;
MYSQL_RES *meta = mysql_stmt_result_metadata(stmt->s);
if (meta == NULL) {
return 1;
}
stmt->meta.num_fields = mysql_num_fields(meta);
stmt->meta.fields = mysql_fetch_fields(meta);
mysql_free_result(meta);
my_bool enable = 1;
if (mysql_stmt_attr_set(stmt->s, STMT_ATTR_UPDATE_MAX_LENGTH, (void*)&enable) != 0) {
return 1;
}
}
if (mysql_stmt_execute(stmt->s) != 0) {
return 1;
}
if (mode != MY_MODE_NONE) {
if (mode == MY_MODE_TABLE) {
if (mysql_stmt_store_result(stmt->s) != 0) {
return 1;
}
}
if (stmt->row_cache == NULL) {
stmt->row_cache = calloc(sizeof(char*), stmt->meta.num_fields);
stmt->row_cache_len = calloc(sizeof(size_t), stmt->meta.num_fields);
}
if (stmt->outputs == NULL) {
stmt->outputs = calloc(sizeof(MYSQL_BIND), stmt->meta.num_fields);
stmt->output_lengths = calloc(sizeof(unsigned long), stmt->meta.num_fields);
} else {
memset(stmt->outputs, 0, sizeof(MYSQL_BIND) * stmt->meta.num_fields);
memset(stmt->output_lengths, 0, sizeof(unsigned long) * stmt->meta.num_fields);
}
for (int i = 0; i < stmt->meta.num_fields; i ++) {
size_t size = 0;
switch (stmt->meta.fields[i].type) {
case MYSQL_TYPE_TINY: size = sizeof(signed char); break; // TINYINT
case MYSQL_TYPE_SHORT: size = sizeof(short int); break; // SMALLINT
case MYSQL_TYPE_INT24: size = sizeof(int); break; // MEDIUMINT
case MYSQL_TYPE_LONG: size = sizeof(int); break; // INT
case MYSQL_TYPE_LONGLONG: size = sizeof(long long int); break; // BIGINT
case MYSQL_TYPE_FLOAT: size = sizeof(float); break; // FLOAT
case MYSQL_TYPE_DOUBLE: size = sizeof(double); break; // DOUBLE
case MYSQL_TYPE_YEAR: size = sizeof(short int); break; // YEAR
case MYSQL_TYPE_TIME: size = sizeof(MYSQL_TIME); break; // TIME
case MYSQL_TYPE_DATE: size = sizeof(MYSQL_TIME); break; // DATE
case MYSQL_TYPE_DATETIME: size = sizeof(MYSQL_TIME); break; // DATETIME
case MYSQL_TYPE_TIMESTAMP: size = sizeof(MYSQL_TIME); break; // TIMESTAMP
// Those type are dynamic length, see my_stmt_fetch_next()
case MYSQL_TYPE_DECIMAL: // DECIMAL
case MYSQL_TYPE_NEWDATE: // MYSQL_TYPE_NEWDATE
case MYSQL_TYPE_NEWDECIMAL: // DECIMAL
case MYSQL_TYPE_STRING: // CHAR, BINARY
case MYSQL_TYPE_VAR_STRING: // VARCHAR, VARBINARY
case MYSQL_TYPE_TINY_BLOB: // TINYBLOB, TINYTEXT
case MYSQL_TYPE_BLOB: // BLOB, TEXT
case MYSQL_TYPE_MEDIUM_BLOB: // MEDIUMBLOB, MEDIUMTEXT
case MYSQL_TYPE_LONG_BLOB: // LONGBLOB, LONGTEXT
case MYSQL_TYPE_BIT: // BIT
default: break;
}
if (size != 0 && stmt->row_cache[i] == NULL) {
stmt->row_cache[i] = malloc(size);
stmt->row_cache_len[i] = size;
}
stmt->outputs[i].buffer = size != 0 ? stmt->row_cache[i] : NULL;
stmt->outputs[i].buffer_length = size;
stmt->outputs[i].buffer_type = stmt->meta.fields[i].type;
stmt->outputs[i].length = &(stmt->output_lengths[i]);
}
if (mysql_stmt_bind_result(stmt->s, stmt->outputs) != 0) {
return 1;
}
}
res->affected_rows = mysql_stmt_affected_rows(stmt->s);
res->insert_id = mysql_stmt_insert_id(stmt->s);
return 0;
}
int my_stmt_close(MY_STMT *stmt, MYSQL_BIND *binds) {
mysql_thread_init();
if (stmt->row_cache != NULL) {
for (int i = 0; i < stmt->meta.num_fields; i ++) {
if (stmt->row_cache[i] != NULL) {
free(stmt->row_cache[i]);
}
}
free(stmt->row_cache);
free(stmt->row_cache_len);
}
if (stmt->outputs != NULL) {
free(stmt->outputs);
free(stmt->output_lengths);
}
if (mysql_stmt_close(stmt->s) != 0) {
return 1;
}
if (binds != NULL) {
free(binds);
}
free(stmt);
return 0;
}
MY_ROW my_stmt_fetch_next(MY_STMT *stmt, MY_STMT_RES *res) {
MY_ROW row = {0, 0, 0};
if (stmt->meta.num_fields == 0) {
return row;
}
mysql_thread_init();
int t = mysql_stmt_fetch(stmt->s);
if (t != 0 && t != MYSQL_DATA_TRUNCATED) {
if (t != MYSQL_NO_DATA) {
row.has_error = 1;
}
return row;
}
for (int i = 0; i < stmt->meta.num_fields; i ++) {
if (stmt->output_lengths[i] == 0) {
continue;
}
switch (stmt->meta.fields[i].type) {
case MYSQL_TYPE_DECIMAL: // DECIMAL
case MYSQL_TYPE_NEWDATE: // MYSQL_TYPE_NEWDATE
case MYSQL_TYPE_NEWDECIMAL: // DECIMAL
case MYSQL_TYPE_STRING: // CHAR, BINARY
case MYSQL_TYPE_VAR_STRING: // VARCHAR, VARBINARY
case MYSQL_TYPE_TINY_BLOB: // TINYBLOB, TINYTEXT
case MYSQL_TYPE_BLOB: // BLOB, TEXT
case MYSQL_TYPE_MEDIUM_BLOB: // MEDIUMBLOB, MEDIUMTEXT
case MYSQL_TYPE_LONG_BLOB: // LONGBLOB, LONGTEXT
case MYSQL_TYPE_BIT: // BIT
if (stmt->row_cache[i] == NULL || stmt->row_cache_len[i] < stmt->output_lengths[i]) {
if (stmt->row_cache[i] != NULL) {
free(stmt->row_cache[i]);
}
stmt->row_cache[i] = malloc(stmt->output_lengths[i]);
stmt->row_cache_len[i] = stmt->output_lengths[i];
}
stmt->outputs[i].buffer = stmt->row_cache[i];
stmt->outputs[i].buffer_length = stmt->output_lengths[i];
if (mysql_stmt_fetch_column(stmt->s, &stmt->outputs[i], i, 0) != 0) {
row.has_error = 1;
return row;
}
break;
default:
break;
}
}
row.mysql_row = (MYSQL_ROW)stmt->row_cache;
row.lengths = stmt->output_lengths;
return row;
}
void my_stmt_close_result(MY_STMT *stmt, MY_STMT_RES *res) {
mysql_thread_init();
mysql_stmt_free_result(stmt->s);
}