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
|
/*
* MySQL port driver.
*
* Copyright (C) 2008, Brian Cully <bjc@kublai.com>
*/
#include "io.h"
#include "log.h"
#include "msg.h"
#include <errno.h>
#include <mysql.h>
#include <string.h>
const char *QUERY_MSG = "sql_query";
const char *PARAM_QUERY_MSG = "sql_param_query";
const char *SELECT_COUNT_MSG = "sql_select_count";
const char *SELECT_MSG = "sql_select";
const char *FIRST_MSG = "sql_first";
const char *LAST_MSG = "sql_last";
const char *NEXT_MSG = "sql_next";
const char *PREV_MSG = "sql_prev";
MYSQL_RES *results = NULL;
my_ulonglong resultoffset = 0, numrows = 0;
void
usage()
{
fprintf(stderr, "Usage: mysqlerl host port db_name user passwd\n");
exit(1);
}
void
set_mysql_results(MYSQL_RES *res)
{
if (results)
mysql_free_result(results);
results = res;
resultoffset = 0;
numrows = mysql_num_rows(results);
}
ETERM *
make_cols(MYSQL_FIELD *fields, unsigned int num_fields)
{
ETERM **cols, *rc;
unsigned int i;
cols = (ETERM **)malloc(num_fields * sizeof(ETERM *));
if (cols == NULL) {
logmsg("ERROR: Couldn't allocate %d bytes for columns: %s",
strerror(errno));
exit(3);
}
for (i = 0; i < num_fields; i++)
cols[i] = erl_mk_string(fields[i].name);
rc = erl_mk_list(cols, num_fields);
for (i = 0; i < num_fields; i++)
erl_free_term(cols[i]);
free(cols);
return rc;
}
ETERM *
make_row(MYSQL_ROW row, unsigned long *lengths, unsigned int num_fields)
{
ETERM **rowtup, *rc;
unsigned int i;
rowtup = (ETERM **)malloc(num_fields * sizeof(ETERM *));
if (rowtup == NULL) {
logmsg("ERROR: Couldn't allocate %d bytes for row: %s",
strerror(errno));
exit(3);
}
for (i = 0; i < num_fields; i++) {
if (row[i])
rowtup[i] = erl_mk_estring(row[i], lengths[i]);
else
rowtup[i] = erl_mk_atom("null");
}
rc = erl_mk_tuple(rowtup, num_fields);
if (rc == NULL) {
logmsg("ERROR: couldn't allocate %d-tuple", num_fields);
exit(3);
}
for (i = 0; i < num_fields; i++)
erl_free_term(rowtup[i]);
free(rowtup);
return rc;
}
ETERM *
make_rows(unsigned int num_rows, unsigned int num_fields)
{
ETERM **rows, *rc;
unsigned int i;
rows = (ETERM **)malloc(num_rows * sizeof(ETERM *));
if (rows == NULL) {
logmsg("ERROR: Couldn't allocate %d bytes for rows: %s",
strerror(errno));
exit(3);
}
for (i = 0; i < num_rows; i++) {
ETERM *rt;
unsigned long *lengths;
MYSQL_ROW row;
row = mysql_fetch_row(results);
resultoffset++;
lengths = mysql_fetch_lengths(results);
rt = make_row(row, lengths, num_fields);
rows[i] = erl_format("~w", rt);
erl_free_term(rt);
}
rc = erl_mk_list(rows, num_rows);
for (i = 0; i < num_rows; i++)
erl_free_term(rows[i]);
free(rows);
return rc;
}
ETERM *
handle_mysql_result()
{
MYSQL_FIELD *fields;
ETERM *ecols, *erows, *resp;
unsigned int num_fields;
num_fields = mysql_num_fields(results);
fields = mysql_fetch_fields(results);
ecols = make_cols(fields, num_fields);
erows = make_rows(numrows, num_fields);
resp = erl_format("{selected, ~w, ~w}", ecols, erows);
erl_free_term(ecols);
erl_free_term(erows);
return resp;
}
void
handle_query(MYSQL *dbh, ETERM *cmd)
{
ETERM *query, *resp;
char *q;
query = erl_element(2, cmd);
q = erl_iolist_to_string(query);
erl_free_term(query);
logmsg("DEBUG: got query msg: %s.", q);
if (mysql_query(dbh, q)) {
resp = erl_format("{error, {mysql_error, ~i, ~s}}",
mysql_errno(dbh), mysql_error(dbh));
} else {
set_mysql_results(mysql_store_result(dbh));
if (results) {
resp = handle_mysql_result(results);
set_mysql_results(NULL);
} else {
if (mysql_field_count(dbh) == 0)
resp = erl_format("{updated, ~i}", mysql_affected_rows(dbh));
else
resp = erl_format("{error, {mysql_error, ~i, ~s}}",
mysql_errno(dbh), mysql_error(dbh));
}
}
erl_free(q);
write_msg(resp);
erl_free_term(resp);
}
/*
* http://dev.mysql.com/doc/refman/5.1/en/mysql-stmt-execute.html
*
* 6 > odbc:param_query(Ref,
* "INSERT INTO EMPLOYEE (NR, FIRSTNAME, "
* "LASTNAME, GENDER) VALUES(?, ?, ?, ?)",
* [{sql_integer,[2,3,4,5,6,7,8]},
* {{sql_varchar, 20},
* ["John", "Monica", "Ross", "Rachel",
* "Piper", "Prue", "Louise"]},
* {{sql_varchar, 20},
* ["Doe","Geller","Geller", "Green",
* "Halliwell", "Halliwell", "Lane"]},
* {{sql_char, 1}, ["M","F","M","F","T","F","F"]}]).
* {updated, 7}
*/
void
handle_param_query(MYSQL *dbh, ETERM *msg)
{
ETERM *query, *params;
char *q;
query = erl_element(2, msg);
q = erl_iolist_to_string(query);
erl_free_term(query);
params = erl_element(3, msg);
erl_free_term(params);
logmsg("DEBUG: got param query msg: %s.", q);
erl_free(q);
}
void
handle_select_count(MYSQL *dbh, ETERM *msg)
{
ETERM *query, *resp;
char *q;
query = erl_element(2, msg);
q = erl_iolist_to_string(query);
erl_free_term(query);
logmsg("DEBUG: got select count msg: %s.", q);
if (mysql_query(dbh, q)) {
resp = erl_format("{error, {mysql_error, ~i, ~s}}",
mysql_errno(dbh), mysql_error(dbh));
} else {
set_mysql_results(mysql_store_result(dbh));
if (results) {
resp = erl_format("{ok, ~i}", numrows);
} else {
if (mysql_field_count(dbh) == 0)
resp = erl_format("{ok, ~i}", mysql_affected_rows(dbh));
else
resp = erl_format("{error, {mysql_error, ~i, ~s}}",
mysql_errno(dbh), mysql_error(dbh));
}
}
erl_free(q);
write_msg(resp);
erl_free_term(resp);
}
void
handle_select(MYSQL *dbh, ETERM *msg)
{
MYSQL_FIELD *fields;
ETERM *epos, *enum_items, *ecols, *erows, *resp;
my_ulonglong pos, num_items;
unsigned int num_fields;
epos = erl_element(2, msg);
enum_items = erl_element(3, msg);
pos = ERL_INT_UVALUE(epos);
num_items = ERL_INT_UVALUE(enum_items);
erl_free_term(enum_items);
erl_free_term(epos);
logmsg("DEBUG: got select pos: %d, n: %d.", erl_size(msg), pos, num_items);
if (results == NULL) {
logmsg("ERROR: select message w/o cursor.");
exit(2);
}
num_fields = mysql_num_fields(results);
fields = mysql_fetch_fields(results);
resultoffset = pos - 1;
if (resultoffset < 0)
resultoffset = 0;
if (num_items > numrows - resultoffset)
num_items = numrows - resultoffset;
mysql_data_seek(results, resultoffset);
ecols = make_cols(fields, num_fields);
erows = make_rows(num_items, num_fields);
resp = erl_format("{selected, ~w, ~w}", ecols, erows);
erl_free_term(erows);
erl_free_term(ecols);
write_msg(resp);
erl_free_term(resp);
}
void
handle_first(MYSQL *dbh, ETERM *msg)
{
MYSQL_FIELD *fields;
ETERM *ecols, *erows, *resp;
unsigned int num_fields;
logmsg("DEBUG: got first msg.");
if (results == NULL) {
logmsg("ERROR: got first message w/o cursor.");
exit(2);
}
num_fields = mysql_num_fields(results);
fields = mysql_fetch_fields(results);
resultoffset = 0;
mysql_data_seek(results, resultoffset);
ecols = make_cols(fields, num_fields);
erows = make_rows(1, num_fields);
resp = erl_format("{selected, ~w, ~w}", ecols, erows);
erl_free_term(erows);
erl_free_term(ecols);
write_msg(resp);
erl_free_term(resp);
}
void
handle_last(MYSQL *dbh, ETERM *msg)
{
MYSQL_FIELD *fields;
ETERM *ecols, *erows, *resp;
unsigned int num_fields;
logmsg("DEBUG: got last msg.");
if (results == NULL) {
logmsg("ERROR: got last message w/o cursor.");
exit(2);
}
num_fields = mysql_num_fields(results);
fields = mysql_fetch_fields(results);
resultoffset = numrows - 1;
mysql_data_seek(results, resultoffset);
ecols = make_cols(fields, num_fields);
erows = make_rows(1, num_fields);
resp = erl_format("{selected, ~w, ~w}", ecols, erows);
erl_free_term(erows);
erl_free_term(ecols);
write_msg(resp);
erl_free_term(resp);
}
void
handle_next(MYSQL *dbh, ETERM *msg)
{
MYSQL_FIELD *fields;
ETERM *ecols, *erows, *resp;
unsigned int num_fields;
logmsg("DEBUG: got next msg.");
if (results == NULL) {
logmsg("ERROR: got next message w/o cursor.");
exit(2);
}
num_fields = mysql_num_fields(results);
fields = mysql_fetch_fields(results);
ecols = make_cols(fields, num_fields);
logmsg("resultoffset: %d, num_rows: %d", resultoffset, numrows);
if (resultoffset == numrows) {
resp = erl_format("{selected, ~w, []}", ecols);
} else {
erows = make_rows(1, num_fields);
resp = erl_format("{selected, ~w, ~w}", ecols, erows);
erl_free_term(erows);
}
erl_free_term(ecols);
write_msg(resp);
erl_free_term(resp);
}
void
handle_prev(MYSQL *dbh, ETERM *msg)
{
MYSQL_FIELD *fields;
ETERM *ecols, *erows, *resp;
unsigned int num_fields;
logmsg("DEBUG: got prev msg.");
if (results == NULL) {
logmsg("ERROR: got prev message w/o cursor.");
exit(2);
}
num_fields = mysql_num_fields(results);
fields = mysql_fetch_fields(results);
ecols = make_cols(fields, num_fields);
logmsg("resultoffset: %d, num_rows: %d", resultoffset, numrows);
if (resultoffset == 0) {
resp = erl_format("{selected, ~w, []}", ecols);
} else {
resultoffset = resultoffset - 1;
mysql_data_seek(results, resultoffset);
erows = make_rows(1, num_fields);
/* Rewind to position at the point we returned. */
resultoffset = resultoffset - 1;
mysql_data_seek(results, resultoffset);
resp = erl_format("{selected, ~w, ~w}", ecols, erows);
erl_free_term(erows);
}
erl_free_term(ecols);
write_msg(resp);
erl_free_term(resp);
}
void
dispatch_db_cmd(MYSQL *dbh, ETERM *msg)
{
ETERM *tag;
tag = erl_element(1, msg);
if (strncmp((char *)ERL_ATOM_PTR(tag),
QUERY_MSG, strlen(QUERY_MSG)) == 0) {
handle_query(dbh, msg);
} else if (strncmp((char *)ERL_ATOM_PTR(tag),
PARAM_QUERY_MSG, strlen(PARAM_QUERY_MSG)) == 0) {
handle_param_query(dbh, msg);
} else if (strncmp((char *)ERL_ATOM_PTR(tag),
SELECT_COUNT_MSG, strlen(SELECT_COUNT_MSG)) == 0) {
handle_select_count(dbh, msg);
} else if (strncmp((char *)ERL_ATOM_PTR(tag),
SELECT_MSG, strlen(SELECT_MSG)) == 0) {
handle_select(dbh, msg);
} else if (strncmp((char *)ERL_ATOM_PTR(tag),
FIRST_MSG, strlen(FIRST_MSG)) == 0) {
handle_first(dbh, msg);
} else if (strncmp((char *)ERL_ATOM_PTR(tag),
LAST_MSG, strlen(LAST_MSG)) == 0) {
handle_last(dbh, msg);
} else if (strncmp((char *)ERL_ATOM_PTR(tag),
NEXT_MSG, strlen(NEXT_MSG)) == 0) {
handle_next(dbh, msg);
} else if (strncmp((char *)ERL_ATOM_PTR(tag),
PREV_MSG, strlen(PREV_MSG)) == 0) {
handle_prev(dbh, msg);
} else {
logmsg("WARNING: message type %s unknown.", (char *)ERL_ATOM_PTR(tag));
erl_free_term(tag);
exit(3);
}
erl_free_term(tag);
}
int
main(int argc, char *argv[])
{
MYSQL dbh;
char *host, *port, *db_name, *user, *passwd;
ETERM *msg;
openlog();
logmsg("INFO: starting up.");
if (argc < 6)
usage();
host = argv[1];
port = argv[2];
db_name = argv[3];
user = argv[4];
passwd = argv[5];
erl_init(NULL, 0);
mysql_init(&dbh);
if (mysql_real_connect(&dbh, host, user, passwd,
db_name, atoi(port), NULL, 0) == NULL) {
logmsg("ERROR: Failed to connect to database %s: %s (%s:%s).",
db_name, mysql_error(&dbh), user, passwd);
exit(2);
}
while ((msg = read_msg()) != NULL) {
dispatch_db_cmd(&dbh, msg);
erl_free_term(msg);
}
mysql_close(&dbh);
logmsg("INFO: shutting down.");
closelog();
return 0;
}
|