-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmysql.c
441 lines (389 loc) · 10.5 KB
/
mysql.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
// This file is part of SmallBASIC
//
// SmallBASIC - MySQL client module based on mysql command-line client
//
// This program is distributed under the terms of the GPL v2.0 or later
// Download the GNU Public License (GPL) from www.gnu.org
//
// Copyright(C) 2003 Nicholas Christopoulos
#include <string.h>
#include <stdio.h>
#include <mysql/mysql.h>
#include "config.h"
#include "mod_utils.h"
typedef struct {
MYSQL *dbf;
} dbnode_t;
#define MAX_OPEN_DB 256
#define MESSAGE_LEN 128
static dbnode_t table[MAX_OPEN_DB];
static int hcount;
/*
* find and return a free handle
*/
static int get_free_handle() {
int i;
if (hcount == 0) {
// we avoid to use handle 0, because we want to use 0 for FALSE
hcount = 2;
return 1;
}
for (i = 1; i < hcount; i++) {
if (table[i].dbf == NULL) {
return i;
}
}
if (hcount + 1 >= MAX_OPEN_DB) {
return -1;
}
hcount++;
return hcount - 1;
}
/*
* returns true if the 'handle' is a valid handle
*/
static int is_valid_handle(int handle) {
if (handle < 1 || handle >= hcount) {
return 0;
}
if (table[handle].dbf == NULL) {
return 0;
}
return 1;
}
/*
* handle <- CONNECT(host, database, user[, password])
*/
int mq_connect(slib_par_t * params, int pcount, var_t * retval) {
char *host, *dbname, *user, *paswd;
int success = 1;
success = mod_parstr_ptr(0, params, pcount, &host);
success = mod_parstr_ptr(1, params, pcount, &dbname);
success = mod_parstr_ptr(2, params, pcount, &user);
success = mod_opt_parstr_ptr(3, params, pcount, &paswd, NULL);
if (success) {
int handle;
handle = get_free_handle();
if (handle >= 0) {
table[handle].dbf = malloc(sizeof(MYSQL));
mysql_init(table[handle].dbf);
mysql_options(table[handle].dbf, MYSQL_READ_DEFAULT_GROUP, "SmallBASIC");
if (!mysql_real_connect
(table[handle].dbf, host, user, paswd, dbname, 0, NULL, 0)) {
success = 0;
char buffer[MESSAGE_LEN];
sprintf(buffer, "MySQL/CONNECT: %s", mysql_error(table[handle].dbf));
v_setstr(retval, buffer);
free(table[handle].dbf);
}
else
v_setint(retval, handle);
}
else {
success = 0;
v_setstr(retval, "MySQL: NO FREE HANDLES");
}
}
else {
v_setstr(retval, "MySQL: ARGUMENT ERROR");
}
return success;
}
/*
* DISCONNECT handle
*/
int mq_disconnect(slib_par_t * params, int pcount, var_t * retval) {
int success, handle;
success = mod_parint(0, params, pcount, &handle);
if (success) {
if (is_valid_handle(handle)) {
mysql_close(table[handle].dbf);
free(table[handle].dbf);
table[handle].dbf = NULL;
success = 1;
}
else {
success = 0;
v_setstr(retval, "MYSQL: INVALID HANDLE");
}
}
else {
v_setstr(retval, "MYSQL: ARGUMENT ERROR");
}
return success;
}
/*
* USE handle, database
*/
int mq_use(slib_par_t * params, int pcount, var_t * retval) {
int success, handle;
char *dbname;
success = mod_parint(0, params, pcount, &handle);
success = mod_parstr_ptr(1, params, pcount, &dbname);
if (success) {
if (is_valid_handle(handle)) {
if (mysql_select_db(table[handle].dbf, dbname)) {
char buffer[MESSAGE_LEN];
sprintf(buffer, "MySQL/USE: %s\n", mysql_error(table[handle].dbf));
v_setstr(retval, buffer);
success = 0;
}
}
else {
success = 0;
v_setstr(retval, "MySQL/USE: INVALID HANDLE");
}
}
else {
v_setstr(retval, "MySQL/USE: ARGUMENT ERROR");
}
return success;
}
/*
* build the return-variable as array filled with the query-result's rows x fields
*/
void mq_build_result_array(MYSQL * mysql, MYSQL_RES * result, var_t * retval) {
MYSQL_ROW row;
int num_fields, i;
int num_rows, crow = 0;
num_fields = mysql_num_fields(result);
num_rows = mysql_num_rows(result);
v_tomatrix(retval, num_rows, num_fields);
// retrieve rows, then call mysql_free_result(result)
while ((row = mysql_fetch_row(result))) {
unsigned long *lengths;
lengths = mysql_fetch_lengths(result);
for (i = 0; i < num_fields; i++) {
var_t *e;
e = (var_t *) (retval->v.a.data + (sizeof(var_t) * (crow * num_fields + i)));
if (row[i]) {
char *buf;
buf = (char *)malloc(lengths[i] + 1);
strncpy(buf, row[i], lengths[i]);
buf[lengths[i]] = '\0';
v_setstr(e, buf);
free(buf);
}
else {
v_setstr(e, "");
}
}
crow++;
}
}
/*
* array <- QUERY(handle, query)
*/
int mq_query(slib_par_t * params, int pcount, var_t * retval) {
int success = 1, handle;
char *query;
MYSQL_RES *result;
MYSQL *mysql;
success = mod_parint(0, params, pcount, &handle);
success = mod_parstr_ptr(1, params, pcount, &query);
if (success) {
if (is_valid_handle(handle)) {
int err;
mysql = table[handle].dbf;
err = mysql_query(mysql, query);
if (err) {
success = 0;
v_setstr(retval, mysql_error(mysql));
}
else {
result = mysql_store_result(mysql);
if (result) { // there are rows
mq_build_result_array(mysql, result, retval);
}
else { // mysql_store_result() returned nothing;
// should it have?
if (mysql_field_count(mysql) == 0) {
// query does not return data (it was not a SELECT)
int num_rows;
num_rows = mysql_affected_rows(mysql);
v_setint(retval, num_rows);
}
else { // mysql_store_result() should have returned
// data
char buffer[MESSAGE_LEN];
sprintf(buffer, "MySQL/QUERY(): %s\n", mysql_error(mysql));
v_setstr(retval, buffer);
}
}
}
}
else {
success = 0;
v_setstr(retval, "MySQL: INVALID HANDLE");
}
}
else {
v_setstr(retval, "MySQL: ARGUMENT ERROR");
}
return success;
}
/*
* array <- TABLES(handle[, wildcards])
*/
int mq_listtbls(slib_par_t * params, int pcount, var_t * retval) {
int success = 1, handle;
char *wc;
MYSQL_RES *result;
MYSQL *mysql;
success = mod_parint(0, params, pcount, &handle);
success = mod_opt_parstr_ptr(1, params, pcount, &wc, NULL);
if (success) {
if (is_valid_handle(handle)) {
mysql = table[handle].dbf;
result = mysql_list_tables(mysql, wc);
if (result) { // there are rows
mq_build_result_array(mysql, result, retval);
mysql_free_result(result);
}
else {
char buffer[MESSAGE_LEN];
sprintf(buffer, "MySQL/LISTTABLES: %s\n", mysql_error(mysql));
v_setstr(retval, buffer);
}
}
else {
success = 0;
v_setstr(retval, "MySQL/LISTTABLES: INVALID HANDLE");
}
}
else {
v_setstr(retval, "MySQL/LISTTABLES: ARGUMENT ERROR");
}
return success;
}
/*
* array <- DBS(handle[, wildcards])
*/
int mq_listdbs(slib_par_t * params, int pcount, var_t * retval) {
int success = 1, handle;
char *wc;
MYSQL_RES *result;
MYSQL *mysql;
success = mod_parint(0, params, pcount, &handle);
success = mod_opt_parstr_ptr(1, params, pcount, &wc, NULL);
if (success) {
if (is_valid_handle(handle)) {
mysql = table[handle].dbf;
result = mysql_list_dbs(mysql, wc);
if (result) { // there are rows
mq_build_result_array(mysql, result, retval);
mysql_free_result(result);
}
else {
char buffer[MESSAGE_LEN];
sprintf(buffer, "MySQL/LISTDBS(): %s\n", mysql_error(mysql));
v_setstr(retval, buffer);
}
}
else {
success = 0;
v_setstr(retval, "MySQL/LISTDBS: INVALID HANDLE");
}
}
else {
v_setstr(retval, "MySQL/LISTDBS: ARGUMENT ERROR");
}
return success;
}
/*
* array <- FIELDS(handle, table[, wildcards])
*/
int mq_listflds(slib_par_t * params, int pcount, var_t * retval) {
int success = 1, handle;
char *dbtable, *wc;
MYSQL_RES *result;
MYSQL *mysql;
success = mod_parint(0, params, pcount, &handle);
success = mod_parstr_ptr(1, params, pcount, &dbtable);
success = mod_opt_parstr_ptr(2, params, pcount, &wc, NULL);
if (success) {
if (is_valid_handle(handle)) {
mysql = table[handle].dbf;
result = mysql_list_fields(mysql, dbtable, wc);
if (result) { // there are rows
mq_build_result_array(mysql, result, retval);
mysql_free_result(result);
}
else {
char buffer[MESSAGE_LEN];
sprintf(buffer, "MySQL/LISTFIELDS(): %s\n", mysql_error(mysql));
v_setstr(retval, buffer);
}
}
else {
success = 0;
v_setstr(retval, "MySQL/LISTFIELDS: INVALID HANDLE");
}
}
else {
v_setstr(retval, "MySQL/LISTFIELDS: ARGUMENT ERROR");
}
return success;
}
/* --- SmallBASIC interface ----------------------------------------------------------------------------------------------- */
typedef struct {
char *name;
int (*command) (slib_par_t *, int, var_t *);
} mod_kw;
// SB function names
static mod_kw func_names[] = {
{"CONNECT", mq_connect}, // connects/reconnects to the server
{"QUERY", mq_query}, // Send command to mysql server
{"DBS", mq_listdbs}, // get a list of the databases
{"TABLES", mq_listtbls}, // get a list of the tables
{"FIELDS", mq_listflds}, // get a list of the fields of a table
{NULL, NULL}
};
// SB procedure names
static mod_kw proc_names[] = {
{"DISCONNECT", mq_disconnect}, // disconnects
{"USE", mq_use}, // changes the current database
{NULL, NULL}
};
/*
* returns the number of the procedures
*/
int sblib_proc_count(void) {
int i;
for (i = 0; proc_names[i].name; i++);
return i;
}
/*
* returns the number of the functions
*/
int sblib_func_count(void) {
int i;
for (i = 0; func_names[i].name; i++);
return i;
}
/*
* returns the 'index' procedure name
*/
int sblib_proc_getname(int index, char *proc_name) {
strcpy(proc_name, proc_names[index].name);
return 1;
}
/*
* returns the 'index' function name
*/
int sblib_func_getname(int index, char *proc_name) {
strcpy(proc_name, func_names[index].name);
return 1;
}
/*
* execute the 'index' procedure
*/
int sblib_proc_exec(int index, int param_count, slib_par_t * params, var_t * retval) {
return proc_names[index].command(params, param_count, retval);
}
/*
* execute the 'index' function
*/
int sblib_func_exec(int index, int param_count, slib_par_t * params, var_t * retval) {
return func_names[index].command(params, param_count, retval);
}