forked from M-itch/libcod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsc_mysql.cpp
556 lines (495 loc) · 11.9 KB
/
gsc_mysql.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include "gsc_mysql.hpp"
#if COMPILE_MYSQL == 1
#include <mysql/mysql.h>
#include <pthread.h>
struct mysql_async_task
{
mysql_async_task *prev;
mysql_async_task *next;
int id;
MYSQL_RES *result;
bool done;
bool started;
bool save;
char query[COD2_MAX_STRINGLENGTH + 1];
};
struct mysql_async_connection
{
mysql_async_connection *prev;
mysql_async_connection *next;
mysql_async_task* task;
MYSQL *connection;
};
mysql_async_connection *first_async_connection = NULL;
mysql_async_task *first_async_task = NULL;
MYSQL *cod_mysql_connection = NULL;
pthread_mutex_t lock_async_mysql;
void *mysql_async_execute_query(void *input_c) //cannot be called from gsc, is threaded.
{
mysql_async_connection *c = (mysql_async_connection *) input_c;
int res = mysql_query(c->connection, c->task->query);
if(!res && c->task->save)
c->task->result = mysql_store_result(c->connection);
else if(res)
{
//mysql show error here?
}
c->task->done = true;
c->task = NULL;
return NULL;
}
void *mysql_async_query_handler(void* input_nothing) //is threaded after initialize
{
static bool started = false;
if(started)
{
Com_DPrintf("mysql_async_query_handler() async handler already started. Returning\n");
return NULL;
}
started = true;
mysql_async_connection *c = first_async_connection;
if(c == NULL)
{
Com_DPrintf("mysql_async_query_handler() async handler started before any connection was initialized\n"); //this should never happen
started = false;
return NULL;
}
mysql_async_task *q;
while(true)
{
pthread_mutex_lock(&lock_async_mysql);
q = first_async_task;
c = first_async_connection;
while(q != NULL)
{
if(!q->started)
{
while(c != NULL && c->task != NULL)
c = c->next;
if(c == NULL)
{
//out of free connections
break;
}
q->started = true;
c->task = q;
pthread_t query_doer;
int error = pthread_create(&query_doer, NULL, mysql_async_execute_query, c);
if(error)
{
Com_DPrintf("error: %i\n", error);
Com_DPrintf("Error detaching async handler thread\n");
return NULL;
}
pthread_detach(query_doer);
c = c->next;
}
q = q->next;
}
pthread_mutex_unlock(&lock_async_mysql);
usleep(10000);
}
return NULL;
}
int mysql_async_query_initializer(char *sql, bool save) //cannot be called from gsc, helper function
{
static int id = 0;
id++;
pthread_mutex_lock(&lock_async_mysql);
mysql_async_task *current = first_async_task;
while(current != NULL && current->next != NULL)
current = current->next;
mysql_async_task *newtask = new mysql_async_task;
newtask->id = id;
strncpy(newtask->query, sql, COD2_MAX_STRINGLENGTH);
newtask->prev = current;
newtask->result = NULL;
newtask->save = save;
newtask->done = false;
newtask->next = NULL;
newtask->started = false;
if(current != NULL)
current->next = newtask;
else
first_async_task = newtask;
pthread_mutex_unlock(&lock_async_mysql);
return id;
}
void gsc_mysql_async_create_query_nosave()
{
char *query;
if ( ! stackGetParams("s", &query))
{
stackError("gsc_mysql_async_create_query_nosave() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
int id = mysql_async_query_initializer(query, false);
stackPushInt(id);
return;
}
void gsc_mysql_async_create_query()
{
char *query;
if ( ! stackGetParams("s", &query))
{
stackError("gsc_mysql_async_create_query() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
int id = mysql_async_query_initializer(query, true);
stackPushInt(id);
return;
}
void gsc_mysql_async_getdone_list()
{
pthread_mutex_lock(&lock_async_mysql);
mysql_async_task *current = first_async_task;
stackPushArray();
while(current != NULL)
{
if(current->done)
{
stackPushInt((int)current->id);
stackPushArrayLast();
}
current = current->next;
}
pthread_mutex_unlock(&lock_async_mysql);
}
void gsc_mysql_async_getresult_and_free() //same as above, but takes the id of a function instead and returns 0 (not done), undefined (not found) or the mem address of result
{
int id;
if(!stackGetParams("i", &id))
{
stackError("gsc_mysql_async_getresult_and_free() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
pthread_mutex_lock(&lock_async_mysql);
mysql_async_task *c = first_async_task;
if(c != NULL)
{
while(c != NULL && c->id != id)
c = c->next;
}
if(c != NULL)
{
if(!c->done)
{
stackPushUndefined(); //should never happend, query not done yet
pthread_mutex_unlock(&lock_async_mysql);
return;
}
if(c->next != NULL)
c->next->prev = c->prev;
if(c->prev != NULL)
c->prev->next = c->next;
else
first_async_task = c->next;
if(c->save)
{
int ret = (int)c->result;
stackPushInt(ret);
}
else
stackPushInt(0);
delete c;
pthread_mutex_unlock(&lock_async_mysql);
return;
}
else
{
stackError("gsc_mysql_async_getresult_and_free() mysql async query id not found");
stackPushUndefined();
pthread_mutex_unlock(&lock_async_mysql);
return;
}
}
void gsc_mysql_async_initializer()//returns array with mysql connection handlers
{
if(first_async_connection != NULL)
{
Com_DPrintf("gsc_mysql_async_initializer() async mysql already initialized. Returning before adding additional connections\n");
stackPushUndefined();
return;
}
if(pthread_mutex_init(&lock_async_mysql, NULL) != 0)
{
Com_DPrintf("Async mutex initialization failed\n");
stackPushUndefined();
return;
}
int port, connection_count;
char *host, *user, *pass, *db;
if ( ! stackGetParams("ssssii", &host, &user, &pass, &db, &port, &connection_count))
{
stackError("gsc_mysql_async_initializer() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
if(connection_count <= 0)
{
stackError("gsc_mysql_async_initializer() need a positive connection_count in mysql_async_initializer");
stackPushUndefined();
return;
}
int i;
stackPushArray();
mysql_async_connection *current = first_async_connection;
for(i = 0; i < connection_count; i++)
{
mysql_async_connection *newconnection = new mysql_async_connection;
newconnection->next = NULL;
newconnection->connection = mysql_init(NULL);
newconnection->connection = mysql_real_connect((MYSQL*)newconnection->connection, host, user, pass, db, port, NULL, 0);
bool reconnect = true;
mysql_options(newconnection->connection, MYSQL_OPT_RECONNECT, &reconnect);
newconnection->task = NULL;
if(current == NULL)
{
newconnection->prev = NULL;
first_async_connection = newconnection;
}
else
{
while(current->next != NULL)
current = current->next;
current->next = newconnection;
newconnection->prev = current;
}
current = newconnection;
stackPushInt((int)newconnection->connection);
stackPushArrayLast();
}
pthread_t async_handler;
if(pthread_create(&async_handler, NULL, mysql_async_query_handler, NULL))
{
stackError("gsc_mysql_async_initializer() error detaching async handler thread");
return;
}
pthread_detach(async_handler);
}
void gsc_mysql_init()
{
MYSQL *my = mysql_init(NULL);
stackPushInt((int) my);
}
void gsc_mysql_reuse_connection()
{
if(cod_mysql_connection == NULL)
{
stackPushUndefined();
return;
}
else
{
stackPushInt((int) cod_mysql_connection);
return;
}
}
void gsc_mysql_real_connect()
{
int mysql, port;
char *host, *user, *pass, *db;
if ( ! stackGetParams("issssi", &mysql, &host, &user, &pass, &db, &port))
{
stackError("gsc_mysql_real_connect() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
mysql = (int) mysql_real_connect((MYSQL *)mysql, host, user, pass, db, port, NULL, 0);
bool reconnect = true;
mysql_options((MYSQL*)mysql, MYSQL_OPT_RECONNECT, &reconnect);
if(cod_mysql_connection == NULL)
cod_mysql_connection = (MYSQL*) mysql;
stackPushInt(mysql);
}
void gsc_mysql_close()
{
int mysql;
if ( ! stackGetParams("i", &mysql))
{
stackError("gsc_mysql_close() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
mysql_close((MYSQL *)mysql);
stackPushInt(0);
}
void gsc_mysql_query()
{
int mysql;
char *query;
if ( ! stackGetParams("is", &mysql, &query))
{
stackError("gsc_mysql_query() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
int ret = mysql_query((MYSQL *)mysql, query);
stackPushInt(ret);
}
void gsc_mysql_errno()
{
int mysql;
if ( ! stackGetParams("i", &mysql))
{
stackError("gsc_mysql_errno() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
int ret = mysql_errno((MYSQL *)mysql);
stackPushInt(ret);
}
void gsc_mysql_error()
{
int mysql;
if ( ! stackGetParams("i", &mysql))
{
stackError("gsc_mysql_error() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
char *ret = (char *)mysql_error((MYSQL *)mysql);
stackPushString(ret);
}
void gsc_mysql_affected_rows()
{
int mysql;
if ( ! stackGetParams("i", &mysql))
{
stackError("gsc_mysql_affected_rows() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
int ret = mysql_affected_rows((MYSQL *)mysql);
stackPushInt(ret);
}
void gsc_mysql_store_result()
{
int mysql;
if ( ! stackGetParams("i", &mysql))
{
stackError("gsc_mysql_store_result() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
MYSQL_RES *result = mysql_store_result((MYSQL *)mysql);
stackPushInt((int) result);
}
void gsc_mysql_num_rows()
{
int result;
if ( ! stackGetParams("i", &result))
{
stackError("gsc_mysql_num_rows() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
int ret = mysql_num_rows((MYSQL_RES *)result);
stackPushInt(ret);
}
void gsc_mysql_num_fields()
{
int result;
if ( ! stackGetParams("i", &result))
{
stackError("gsc_mysql_num_fields() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
int ret = mysql_num_fields((MYSQL_RES *)result);
stackPushInt(ret);
}
void gsc_mysql_field_seek()
{
int result;
int offset;
if ( ! stackGetParams("ii", &result, &offset))
{
stackError("gsc_mysql_field_seek() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
int ret = mysql_field_seek((MYSQL_RES *)result, offset);
stackPushInt(ret);
}
void gsc_mysql_fetch_field()
{
int result;
if ( ! stackGetParams("i", &result))
{
stackError("gsc_mysql_fetch_field() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
MYSQL_FIELD *field = mysql_fetch_field((MYSQL_RES *)result);
if (field == NULL)
{
stackPushUndefined();
return;
}
char *ret = field->name;
stackPushString(ret);
}
void gsc_mysql_fetch_row()
{
int result;
if ( ! stackGetParams("i", &result))
{
stackError("gsc_mysql_fetch_row() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
MYSQL_ROW row = mysql_fetch_row((MYSQL_RES *)result);
if (!row)
{
stackPushUndefined();
return;
}
stackPushArray();
int numfields = mysql_num_fields((MYSQL_RES *)result);
for (int i=0; i<numfields; i++)
{
if (row[i] == NULL)
stackPushUndefined();
else
stackPushString(row[i]);
stackPushArrayLast();
}
}
void gsc_mysql_free_result()
{
int result;
if ( ! stackGetParams("i", &result))
{
stackError("gsc_mysql_free_result() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
if(result == 0)
{
stackError("mysql_free_result() input is a NULL-pointer");
stackPushUndefined();
return;
}
mysql_free_result((MYSQL_RES *)result);
stackPushUndefined();
}
void gsc_mysql_real_escape_string()
{
int mysql;
char *str;
if ( ! stackGetParams("is", &mysql, &str))
{
stackError("gsc_mysql_real_escape_string() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
char *to = (char *) malloc(strlen(str) * 2 + 1);
mysql_real_escape_string((MYSQL *)mysql, to, str, strlen(str));
stackPushString(to);
free(to);
}
#endif