-
Notifications
You must be signed in to change notification settings - Fork 0
/
moosql.c
486 lines (447 loc) · 13.2 KB
/
moosql.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
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
/************************** MOO SQL v 1.0 ****************************************
* copyright 2008, michael munson. this program is released under the BSD license *
*/
#include "moosql.h"
/* this function takes the integer given by do_mysql_connect() and returns a MYSQL_CONN associated with that */
MYSQL_CONN* resolve_mysql_connection(int id)
{
int lcv=0;
MYSQL_CONN *ptr=NULL;
for (lcv=0;lcv<MOOSQL_MAX_CON;lcv++)
{
if (SQL_CONNECTIONS[lcv].id == id)
return &SQL_CONNECTIONS[lcv];
}
return (NULL);
}
void sanitize_result_string(char *string)
{
/* I just learned the hard way that it is a really bad idea to have newlines in your LambdaMOO database file */
/* We'll convert each newline to a tab character instead, which is relatively harmless but still identifiable */
/* see moosql.h to undefine SANITIZE_STRINGS if your version of LambdaMOO can handle newlines */
#ifdef SANITIZE_STRINGS
int lcv=0;
char *ptr=string;
for (lcv=0;lcv<strlen(string);lcv++)
{
if (*ptr == '\n')
*ptr = '\t';
ptr++; /* go to next character */
}
#endif
}
int connection_array_index()
{
/* This function returns the index for the first blank entry in SQL_CONNECTIONS. Returns -1 if none are available */
int lcv=0;
for (lcv=0; lcv < MOOSQL_MAX_CON; lcv++)
{
if (!SQL_CONNECTIONS[lcv].active)
return lcv;
else if (SQL_CONNECTIONS[lcv].conn == NULL)
{
/* Somehow the connection is null but the active flag hasn't been reset. Oh well, we'll do that. */
SQL_CONNECTIONS[lcv].active = 0;
SQL_CONNECTIONS[lcv].id = 0;
SQL_CONNECTIONS[lcv].port = 0;
return lcv;
}
}
return -1;
}
MYSQL_CONN *do_mysql_connect (const char *host_name, const char *user_name, const char *password, const char *db_name,
const unsigned int port_num, const char *socket_name, const unsigned int flags, char *error_string)
{
MYSQL *conn; /* pointer to connection handler */
int ndx=0;
ndx = connection_array_index(); /* find out where it should go. */
if (ndx == -1) /* No room in the index! */
{
snprintf(error_string, MOOSQL_ERROR_LEN, "no mysql connections available, close some?");
return (NULL);
}
conn = mysql_init (NULL); /* allocate the handler */
if (conn == NULL)
{
snprintf (error_string, MOOSQL_ERROR_LEN, "mysql_init failed");
return (NULL);
}
if (mysql_real_connect (conn, host_name, user_name, password,
db_name, port_num, socket_name, flags) == NULL)
{
snprintf (error_string, MOOSQL_ERROR_LEN, "Error %u (%s)",
mysql_errno (conn), mysql_error (conn));
return (NULL);
}
SQL_CONNECTIONS[ndx].active = 1;
SQL_CONNECTIONS[ndx].id = next_mysql_connection--;
SQL_CONNECTIONS[ndx].port = port_num;
SQL_CONNECTIONS[ndx].connect_time = time(0);
SQL_CONNECTIONS[ndx].last_query_time = time(0);
snprintf(SQL_CONNECTIONS[ndx].server,MOOSQL_STRING_LEN,host_name, "%s");
snprintf(SQL_CONNECTIONS[ndx].username,MOOSQL_STRING_LEN,user_name, "%s");
snprintf(SQL_CONNECTIONS[ndx].database,MOOSQL_STRING_LEN,db_name, "%s");
SQL_CONNECTIONS[ndx].conn = conn;
return &SQL_CONNECTIONS[ndx];
}
void do_mysql_disconnect (MYSQL_CONN *conn_wrapper)
{
conn_wrapper->active = 0;
conn_wrapper->id = 0;
conn_wrapper->port = 0;
conn_wrapper->connect_time = 0;
conn_wrapper->last_query_time = 0;
mysql_close (conn_wrapper->conn);
conn_wrapper->conn = NULL;
/* we could zero up the strings on the wrapper if we wanted to here */
}
Var process_row(MYSQL_RES *res_set, const MYSQL_ROW *row)
{
Var r;
int len=0;
int i=0;
double dval=0.0;
len = mysql_num_fields(res_set);
r.type = TYPE_LIST;
r = new_list(len);
for (i=0;i<len;i++)
{
r.v.list[i+1].type = TYPE_STR;
if (row[i] == NULL)
r.v.list[i+1].v.str = str_dup("");
else
{
sanitize_result_string((char*)row[i]);
r.v.list[i+1].v.str = str_dup((char*)row[i]);
}
}
return r;
}
Var process_result_set (MYSQL_CONN *conn, MYSQL_RES *res_set)
{
MYSQL_ROW row;
unsigned int i;
int len = 0;
int count = 1;
int lcv = 1;
len = mysql_num_rows(res_set);
Var r;
r.type = TYPE_LIST;
if (len < 1)
{
r = new_list(0);
mysql_free_result (res_set);
return r;
}
r = new_list(len);
while ((row = mysql_fetch_row (res_set)) != NULL)
{
r.v.list[count].type = TYPE_LIST;
r.v.list[count].v.list = process_row(res_set,(const MYSQL_ROW*)row).v.list;
count++;
}
mysql_free_result (res_set);
return r;
}
#if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224
#define mysql_field_count mysql_num_fields
#endif
MYSQL_RES* process_mysql_query (MYSQL_CONN *conn, char * res_string)
{
MYSQL_RES *res_set;
unsigned int field_count;
/* query succeeded err */
res_set = mysql_store_result (conn->conn);
if (res_set == NULL) /* no result */
{
/* check if its an error or just a query with no result */
if (mysql_field_count (conn->conn) > 0)
{
snprintf(res_string,MOOSQL_ERROR_LEN,"Error processing SQL result set");
return NULL;
}
else
{
snprintf (res_string,MOOSQL_ERROR_LEN, "%lu rows affected.",(unsigned long) mysql_affected_rows (conn->conn));
return NULL;
}
}
else
{
return res_set;
}
}
/* our MOO functions are below */
int mysql_connection_status(MYSQL_CONN *wrapper)
{
int ping;
if (wrapper == NULL || wrapper->conn == NULL)
return 0;
ping = mysql_ping(wrapper->conn);
if (ping == 0)
return 1;
else
return 0;
}
static package bf_mysql_ping(Var arglist, Byte next, void *vdata, Objid progr)
{
/* we will return 1 if the connection is active, 0 if it is not */
Var r;
Objid oid = arglist.v.list[1].v.obj;
free_var(arglist);
if (!is_wizard(progr))
return make_error_pack(E_PERM);
MYSQL_CONN *wrapper;
wrapper = resolve_mysql_connection(oid);
r.type = TYPE_INT;
r.v.num = mysql_connection_status(wrapper);
return make_var_pack(r);
}
static package bf_mysql_status(Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
Objid oid = arglist.v.list[1].v.obj;
free_var(arglist);
if (!is_wizard(progr))
return make_error_pack(E_PERM);
MYSQL_CONN *wrapper;
wrapper = resolve_mysql_connection(oid);
if (wrapper == NULL || wrapper->active == 0)
return make_error_pack(E_INVARG);
r = new_list(7);
r.v.list[1].type = TYPE_STR;
r.v.list[1].v.str = str_dup(wrapper->server);
r.v.list[2].type = TYPE_INT;
r.v.list[2].v.num = wrapper->port;
r.v.list[3].type = TYPE_STR;
r.v.list[3].v.str = str_dup(wrapper->username);
r.v.list[4].type = TYPE_STR;
r.v.list[4].v.str = str_dup(wrapper->database);
r.v.list[5].type = TYPE_INT;
r.v.list[5].v.num = wrapper->connect_time;
r.v.list[6].type = TYPE_INT;
r.v.list[6].v.num = wrapper->last_query_time;
r.v.list[7].type = TYPE_INT;
r.v.list[7].v.num = mysql_get_server_version(wrapper->conn);
return make_var_pack(r);
}
static package bf_mysql_connections(Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
free_var(arglist);
if (!is_wizard(progr))
return make_error_pack(E_PERM);
int count=0;
int lcv=0;
for (lcv=0;lcv<MOOSQL_MAX_CON;lcv++)
if (SQL_CONNECTIONS[lcv].active == 1)
count++;
r = new_list(count); /* needed to know how many to allocate the list firstly */
count = 0;
for (lcv=0;lcv<MOOSQL_MAX_CON;lcv++)
{
if (SQL_CONNECTIONS[lcv].active == 1)
{
count++;
r.v.list[count].type = TYPE_OBJ;
r.v.list[count].v.obj = SQL_CONNECTIONS[lcv].id;
}
}
return make_var_pack(r);
}
static package bf_mysql_close(Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
Objid oid = arglist.v.list[1].v.obj;
r.type = TYPE_INT;
free_var(arglist);
MYSQL_CONN *wrapper;
if (!is_wizard(progr))
return make_error_pack(E_PERM);
wrapper = resolve_mysql_connection(oid);
if (wrapper == NULL)
{
r.v.num = 0;
return make_var_pack(r);
}
else
{
wrapper->active = 0;
wrapper->id = 0;
wrapper->port = 0;
wrapper->connect_time = 0;
wrapper->last_query_time = 0;
r.v.num = 1;
}
if (mysql_connection_status(wrapper) != 0)
{
do_mysql_disconnect(wrapper);
wrapper->conn = NULL;
r.v.num = 1;
}
return make_var_pack(r);
}
static package bf_mysql_escape( Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
MYSQL_CONN *wrapper;
if (!is_wizard(progr))
{
free_var(arglist);
return make_error_pack(E_PERM);
}
Objid oid = arglist.v.list[1].v.obj;
wrapper = resolve_mysql_connection(oid);
if (wrapper == NULL || wrapper->conn == NULL || wrapper->active == 0)
{
free_var(arglist);
return make_error_pack(E_INVARG);
}
const char *string = arglist.v.list[2].v.str;
free_var(arglist);
ulong bytes = strlen(string);
char ostring[(2 * bytes)+1];
mysql_real_escape_string(wrapper->conn,ostring,string,bytes);
r.type = TYPE_STR;
r.v.str = str_dup(ostring);
return make_var_pack(r);
}
static package bf_mysql_query( Var arglist, Byte next, void *vdata, Objid progr)
{
Var r;
char error_string[MOOSQL_ERROR_LEN];
MYSQL_CONN *wrapper;
MYSQL_RES *res_set;
#ifdef MOOSQL_MULTIPLE_STATEMENTS
Var tmp;
Var end;
int len = 0;
int continu = 1;
#endif
if (!is_wizard(progr))
{
free_var(arglist);
return make_error_pack(E_PERM);
}
Objid oid = arglist.v.list[1].v.obj;
wrapper = resolve_mysql_connection(oid);
if (wrapper == NULL || wrapper->conn == NULL || wrapper->active == 0)
{
free_var(arglist);
return make_error_pack(E_INVARG);
}
const char *query = arglist.v.list[2].v.str;
free_var(arglist);
/* we do the query now. */
if (mysql_query (wrapper->conn, query) != 0) /* failed */
{
/* there is an error, so we will return that string. similar to below which
* returns a string for a successful query with no result set which is handled in
* process_mysql_query */
snprintf(error_string,MOOSQL_ERROR_LEN,"ERR: %s",mysql_error(wrapper->conn));
r.type = TYPE_STR;
r.v.str = str_dup(error_string);
return make_var_pack(r);
}
wrapper->last_query_time = time(0);
#ifdef MOOSQL_MULTIPLE_STATEMENTS
r = new_list(1);
r.v.list[1].type = TYPE_INT;
r.v.list[1].v.num = 0;
end = new_list(0);
while (continu)
{
len++;
res_set = process_mysql_query(wrapper,error_string);
if (res_set == NULL) /* there was no result on this query */
{
tmp.type = TYPE_STR;
tmp.v.str = str_dup(error_string);
end = listappend(end,var_dup(tmp));
}
else
{
tmp = process_result_set(wrapper,res_set);
end = listappend(end,var_dup(tmp));
}
if (mysql_more_results(wrapper->conn))
{
mysql_next_result(wrapper->conn);
continu = 1;
}
else
continu = 0;
}
if (len <= 1) /* if there is only one result return it like in previous versions, a list of rows */
return make_var_pack(end.v.list[1]);
r.v.list[1].v.num = len;
/* if there are more return it in this format {X, Y} where X is the number of results and Y is a list of results */
r = listappend(r,end);
return make_var_pack(r);
#else
res_set = process_mysql_query(wrapper,error_string);
if (res_set == NULL) /* there was either an error / no result on this query */
{
r.type = TYPE_STR;
r.v.str = str_dup(error_string);
return make_var_pack(r);
}
r = process_result_set(wrapper,res_set);
return make_var_pack(r);
#endif
}
static package
bf_mysql_connect(Var arglist, Byte next, void *vdata, Objid progr)
{
#ifdef OUTBOUND_NETWORK
Var r;
char error_string[MOOSQL_ERROR_LEN];
MYSQL_CONN *wrapper;
if (!is_wizard(progr))
{
free_var(arglist);
return make_error_pack(E_PERM);
}
const char *hostname = arglist.v.list[1].v.str;
const int port = arglist.v.list[2].v.num;
const char *username = arglist.v.list[3].v.str;
const char *password = arglist.v.list[4].v.str;
const char *dbname = arglist.v.list[5].v.str;
free_var(arglist); /* get rid of that now */
/* try to connect to mysql server */
/* check if we have enough connection slots. */
int index=-1;
index = connection_array_index();
if (index == -1)
return make_error_pack(E_QUOTA);
#ifdef MOOSQL_MULTIPLE_STATEMENTS
wrapper = do_mysql_connect(hostname, username, password, dbname, port, NULL, CLIENT_MULTI_STATEMENTS, error_string);
#else
wrapper = do_mysql_connect(hostname, username, password, dbname, port, NULL, 0, error_string);
#endif
if (wrapper == NULL || wrapper->conn == NULL)
{
/* an error happened in the connect, return that as a STR */
r.type = TYPE_STR;
r.v.str = str_dup(error_string);
return make_var_pack(r);
}
r.type = TYPE_OBJ;
r.v.obj = wrapper->id;
return make_var_pack(r);
#else /* !OUTBOUND_NETWORK */
/* This function is disabled in this server. */
free_var(arglist);
return make_error_pack(E_PERM);
#endif
}
void register_mysql(void)
{
(void) register_function("mysql_connect", 5, 5, bf_mysql_connect, TYPE_STR, TYPE_INT, TYPE_STR, TYPE_STR, TYPE_STR);
(void) register_function("mysql_close", 1, 1, bf_mysql_close, TYPE_OBJ);
(void) register_function("mysql_status",1,1,bf_mysql_status,TYPE_OBJ);
(void) register_function("mysql_ping",1,1,bf_mysql_ping,TYPE_OBJ);
(void) register_function("mysql_query",2,2,bf_mysql_query,TYPE_OBJ,TYPE_STR);
(void) register_function("mysql_connections",0,0,bf_mysql_connections);
(void) register_function("mysql_escape",2,2,bf_mysql_escape,TYPE_OBJ,TYPE_STR);
}