forked from lunarmodules/luasql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls_mysql.c
821 lines (693 loc) · 18.6 KB
/
ls_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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/*
** LuaSQL, MySQL driver
** Authors: Eduardo Quintao
** See Copyright Notice in license.html
** $Id: ls_mysql.c,v 1.31 2009/02/07 23:16:23 tomas Exp $
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef WIN32
#include <winsock2.h>
#define NO_CLIENT_LONG_LONG
#endif
#include <mysql.h>
#include "lua.h"
#include "lauxlib.h"
#include "luasql.h"
/* MSVC still doesn't support C99 properly until 2015 */
#if defined(_MSC_VER) && _MSC_VER<1900
#pragma warning(disable:4996) /* and complains if you try to work around it */
#define snprintf _snprintf
#endif
#define LUASQL_ENVIRONMENT_MYSQL "MySQL environment"
#define LUASQL_CONNECTION_MYSQL "MySQL connection"
#define LUASQL_STATEMENT_MYSQL "MySQL statement"
#define LUASQL_CURSOR_MYSQL "MySQL cursor"
/* For compat with old version 4.0 */
#if (MYSQL_VERSION_ID < 40100)
#define MYSQL_TYPE_VAR_STRING FIELD_TYPE_VAR_STRING
#define MYSQL_TYPE_STRING FIELD_TYPE_STRING
#define MYSQL_TYPE_DECIMAL FIELD_TYPE_DECIMAL
#define MYSQL_TYPE_SHORT FIELD_TYPE_SHORT
#define MYSQL_TYPE_LONG FIELD_TYPE_LONG
#define MYSQL_TYPE_FLOAT FIELD_TYPE_FLOAT
#define MYSQL_TYPE_DOUBLE FIELD_TYPE_DOUBLE
#define MYSQL_TYPE_LONGLONG FIELD_TYPE_LONGLONG
#define MYSQL_TYPE_INT24 FIELD_TYPE_INT24
#define MYSQL_TYPE_YEAR FIELD_TYPE_YEAR
#define MYSQL_TYPE_TINY FIELD_TYPE_TINY
#define MYSQL_TYPE_TINY_BLOB FIELD_TYPE_TINY_BLOB
#define MYSQL_TYPE_MEDIUM_BLOB FIELD_TYPE_MEDIUM_BLOB
#define MYSQL_TYPE_LONG_BLOB FIELD_TYPE_LONG_BLOB
#define MYSQL_TYPE_BLOB FIELD_TYPE_BLOB
#define MYSQL_TYPE_DATE FIELD_TYPE_DATE
#define MYSQL_TYPE_NEWDATE FIELD_TYPE_NEWDATE
#define MYSQL_TYPE_DATETIME FIELD_TYPE_DATETIME
#define MYSQL_TYPE_TIME FIELD_TYPE_TIME
#define MYSQL_TYPE_TIMESTAMP FIELD_TYPE_TIMESTAMP
#define MYSQL_TYPE_ENUM FIELD_TYPE_ENUM
#define MYSQL_TYPE_SET FIELD_TYPE_SET
#define MYSQL_TYPE_NULL FIELD_TYPE_NULL
#define mysql_commit(_) ((void)_)
#define mysql_rollback(_) ((void)_)
#define mysql_autocommit(_,__) ((void)_)
#endif
typedef struct {
short closed;
int lock; /* lock count for open connections */
} env_data;
typedef struct {
short closed;
int lock; /* lock count for open cursors */
int env; /* reference to environment */
MYSQL *my_conn;
} conn_data;
typedef struct {
short closed;
int lock; /* lock count for open statements */
unsigned char hidden; /* statement was used interally i.e. from a
direct con:execute */
MYSQL_STMT *my_stmt; /* statement handle */
} stmt_data;
typedef struct {
short closed;
int stmt; /* reference to statement */
int numcols; /* number of columns */
int colnames, coltypes; /* reference to column information tables */
MYSQL_RES *my_res;
} cur_data;
LUASQL_API int luaopen_luasql_mysql (lua_State *L);
/*
** Check for valid environment.
*/
static env_data *getenvironment (lua_State *L)
{
env_data *env = (env_data *)luaL_checkudata (L, 1, LUASQL_ENVIRONMENT_MYSQL);
luaL_argcheck (L, env != NULL, 1, "environment expected");
luaL_argcheck (L, !env->closed, 1, "environment is closed");
return env;
}
/*
** Check for valid connection.
*/
static conn_data *getconnection (lua_State *L)
{
conn_data *conn = (conn_data *)luaL_checkudata (L, 1, LUASQL_CONNECTION_MYSQL);
luaL_argcheck (L, conn != NULL, 1, "connection expected");
luaL_argcheck (L, !conn->closed, 1, "connection is closed");
return conn;
}
/*
** Check for valid statement.
*/
static stmt_data *getstatement (lua_State *L)
{
stmt_data *stmt = (stmt_data *)luaL_checkudata (L, 1, LUASQL_STATEMENT_MYSQL);
luaL_argcheck (L, stmt != NULL, 1, "statement expected");
luaL_argcheck (L, !stmt->closed, 1, "statement is closed");
return stmt;
}
/*
** Check for valid cursor.
*/
static cur_data *getcursor (lua_State *L)
{
cur_data *cur = (cur_data *)luaL_checkudata (L, 1, LUASQL_CURSOR_MYSQL);
luaL_argcheck (L, cur != NULL, 1, "cursor expected");
luaL_argcheck (L, !cur->closed, 1, "cursor is closed");
return cur;
}
/*
** Shut an active statement object
*/
static int stmt_shut(lua_State *L, stmt_data *stmt)
{
if(!stmt->closed) {
mysql_stmt_close(stmt->my_stmt);
stmt->closed = 1;
}
return 0;
}
/*
** Push the value of #i field of #tuple row.
*/
static void pushvalue (lua_State *L, void *row, long int len)
{
if (row == NULL) {
lua_pushnil (L);
} else {
lua_pushlstring (L, row, len);
}
}
/*
** Get the internal database type of the given column.
*/
static char *getcolumntype (enum enum_field_types type)
{
switch (type) {
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_STRING:
return "string";
case MYSQL_TYPE_DECIMAL:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_LONG:
case MYSQL_TYPE_FLOAT:
case MYSQL_TYPE_DOUBLE:
case MYSQL_TYPE_LONGLONG:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_YEAR:
case MYSQL_TYPE_TINY:
return "number";
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB:
return "binary";
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_NEWDATE:
return "date";
case MYSQL_TYPE_DATETIME:
return "datetime";
case MYSQL_TYPE_TIME:
return "time";
case MYSQL_TYPE_TIMESTAMP:
return "timestamp";
case MYSQL_TYPE_ENUM:
case MYSQL_TYPE_SET:
return "set";
case MYSQL_TYPE_NULL:
return "null";
default:
return "undefined";
}
}
/*
** Creates the lists of fields names and fields types.
*/
static void create_colinfo(lua_State *L, cur_data *cur)
{
char name[50];
int i;
MYSQL_FIELD *fields = mysql_fetch_fields(cur->my_res);
lua_newtable(L); /* names */
lua_newtable(L); /* types */
for(i = 1; i <= cur->numcols; i++) {
lua_pushstring(L, fields[i-1].name);
lua_rawseti(L, -3, i);
snprintf(name, sizeof(name), "%.20s(%ld)", getcolumntype (fields[i-1].type),
fields[i-1].length);
lua_pushstring(L, name);
lua_rawseti(L, -2, i);
}
/* Stores the references in the cursor structure */
cur->coltypes = luaL_ref(L, LUA_REGISTRYINDEX);
cur->colnames = luaL_ref(L, LUA_REGISTRYINDEX);
}
/*
** Closes the cursos and nullify all structure fields.
*/
static int cur_nullify (lua_State *L, cur_data *cur)
{
/* Nullify structure fields. */
cur->closed = 1;
mysql_free_result(cur->my_res);
luaL_unref (L, LUA_REGISTRYINDEX, cur->stmt);
luaL_unref (L, LUA_REGISTRYINDEX, cur->colnames);
luaL_unref (L, LUA_REGISTRYINDEX, cur->coltypes);
return 0;
}
/*
** Get another row of the given cursor.
*/
static int cur_fetch (lua_State *L)
{
cur_data *cur = getcursor (L);
MYSQL_RES *res = cur->my_res;
unsigned long *lengths;
MYSQL_ROW row = mysql_fetch_row(res);
if (row == NULL) {
cur_nullify (L, cur);
lua_pushnil(L); /* no more results */
return 1;
}
lengths = mysql_fetch_lengths(res);
if (lua_istable (L, 2)) {
const char *opts = luaL_optstring (L, 3, "n");
if (strchr (opts, 'n') != NULL) {
/* Copy values to numerical indices */
int i;
for (i = 0; i < cur->numcols; i++) {
pushvalue (L, row[i], lengths[i]);
lua_rawseti (L, 2, i+1);
}
}
if (strchr (opts, 'a') != NULL) {
int i;
/* Check if colnames exists */
if (cur->colnames == LUA_NOREF) {
create_colinfo(L, cur);
}
/* Push colnames */
lua_rawgeti (L, LUA_REGISTRYINDEX, cur->colnames);
/* Copy values to alphanumerical indices */
for (i = 0; i < cur->numcols; i++) {
lua_rawgeti(L, -1, i+1); /* push the field name */
/* Actually push the value */
pushvalue (L, row[i], lengths[i]);
lua_rawset (L, 2);
}
/* lua_pop(L, 1); Pops colnames table. Not needed */
}
lua_pushvalue(L, 2);
return 1; /* return table */
} else {
int i;
luaL_checkstack (L, cur->numcols, LUASQL_PREFIX"too many columns");
for (i = 0; i < cur->numcols; i++) {
pushvalue (L, row[i], lengths[i]);
}
return cur->numcols; /* return #numcols values */
}
}
/*
** Cursor object collector function
*/
static int cur_gc (lua_State *L)
{
cur_data *cur = (cur_data *)luaL_checkudata (L, 1, LUASQL_CURSOR_MYSQL);
if (cur != NULL && !(cur->closed)) {
cur_nullify (L, cur);
}
return 0;
}
/*
** Close the cursor on top of the stack.
** Return 1
*/
static int cur_close (lua_State *L)
{
cur_data *cur = (cur_data *)luaL_checkudata (L, 1, LUASQL_CURSOR_MYSQL);
luaL_argcheck (L, cur != NULL, 1, LUASQL_PREFIX"cursor expected");
if (cur->closed) {
lua_pushboolean (L, 0);
return 1;
}
cur_nullify (L, cur);
lua_pushboolean (L, 1);
return 1;
}
/*
** Pushes a column information table on top of the stack.
** If the table isn't built yet, call the creator function and stores
** a reference to it on the cursor structure.
*/
static void _pushtable (lua_State *L, cur_data *cur, size_t off)
{
int *ref = (int *)((char *)cur + off);
/* If colnames or coltypes do not exist, create both. */
if (*ref == LUA_NOREF) {
create_colinfo(L, cur);
}
/* Pushes the right table (colnames or coltypes) */
lua_rawgeti (L, LUA_REGISTRYINDEX, *ref);
}
#define pushtable(L,c,m) (_pushtable(L,c,offsetof(cur_data,m)))
/*
** Return the list of field names.
*/
static int cur_getcolnames (lua_State *L)
{
pushtable (L, getcursor(L), colnames);
return 1;
}
/*
** Return the list of field types.
*/
static int cur_getcoltypes (lua_State *L)
{
pushtable (L, getcursor(L), coltypes);
return 1;
}
/*
** Push the number of rows.
*/
static int cur_numrows (lua_State *L)
{
lua_pushinteger (L, (lua_Integer)mysql_num_rows (getcursor(L)->my_res));
return 1;
}
/*
** Create a new Cursor object and push it on top of the stack.
*/
static int create_cursor (lua_State *L, int stmt, MYSQL_RES *result, int cols)
{
cur_data *cur = (cur_data *)lua_newuserdata(L, sizeof(cur_data));
luasql_setmeta (L, LUASQL_CURSOR_MYSQL);
/* fill in structure */
cur->closed = 0;
cur->stmt = LUA_NOREF;
cur->numcols = cols;
cur->colnames = LUA_NOREF;
cur->coltypes = LUA_NOREF;
cur->my_res = result;
lua_pushvalue (L, stmt);
cur->stmt = luaL_ref (L, LUA_REGISTRYINDEX);
return 1;
}
static int stmt_gc (lua_State *L)
{
stmt_shut(L, getstatement(L));
return 0;
}
static int stmt_close(lua_State *L)
{
stmt_data *stmt = getstatement(L);
if (stmt->closed) {
lua_pushboolean (L, 0);
return 1;
}
stmt_shut(L, stmt);
lua_pushboolean (L, 1);
return 1;
}
static int conn_gc (lua_State *L)
{
conn_data *conn = (conn_data *)luaL_checkudata (L, 1, LUASQL_CONNECTION_MYSQL);
if (conn != NULL && !(conn->closed)) {
/* Nullify structure fields. */
conn->closed = 1;
luaL_unref (L, LUA_REGISTRYINDEX, conn->env);
mysql_close (conn->my_conn);
}
return 0;
}
/*
** Close a Connection object.
*/
static int conn_close (lua_State *L)
{
conn_data *conn=(conn_data *)luaL_checkudata(L, 1, LUASQL_CONNECTION_MYSQL);
luaL_argcheck (L, conn != NULL, 1, LUASQL_PREFIX"connection expected");
if (conn->closed) {
lua_pushboolean (L, 0);
return 1;
}
conn_gc (L);
lua_pushboolean (L, 1);
return 1;
}
static int escape_string (lua_State *L)
{
size_t size, new_size;
conn_data *conn = getconnection (L);
const char *from = luaL_checklstring(L, 2, &size);
char *to;
to = (char *)malloc(sizeof(char) * (2 * size + 1));
if(to) {
new_size = mysql_real_escape_string(conn->my_conn, to, from, size);
lua_pushlstring(L, to, new_size);
free(to);
return 1;
}
luaL_error(L, "could not allocate escaped string");
return 0;
}
/*
** Prepares an SQL statement
** Returns a Statement object
*/
static int conn_prepare (lua_State *L)
{
conn_data *conn = getconnection (L);
size_t st_len;
const char *statement = luaL_checklstring (L, 2, &st_len);
stmt_data *stmt = (stmt_data *)lua_newuserdata(L, sizeof(stmt_data));
memset(stmt, 0, sizeof(stmt_data));
luasql_setmeta (L, LUASQL_STATEMENT_MYSQL);
stmt->my_stmt = mysql_stmt_init(conn->my_conn);
if(stmt->my_stmt == NULL) {
return luasql_failmsg(L, "error preparing query. MySQL: ",
"out of memory");
}
if(mysql_stmt_prepare(stmt->my_stmt, statement, st_len) < 0) {
return luasql_failmsg(L, "error preparing query. MySQL: ",
mysql_stmt_error(stmt->my_stmt));
}
return 1;
}
static int stmt_execute(lua_State *L)
{
stmt_data *stmt = getstatement(L);
int numcols;
if(mysql_stmt_execute(stmt->my_stmt) < 0) {
return luasql_failmsg(L, "error executing statement. MySQL: ",
mysql_stmt_error(stmt->my_stmt));
}
if((numcols = mysql_stmt_field_count(stmt->my_stmt)) != 0) {
/* SELECT */
MYSQL_RES *res = mysql_stmt_result_metadata(stmt->my_stmt);
if(res != NULL) {
return create_cursor(L, 1, res, numcols);
}
return luasql_failmsg(L, "error creating cursor. MySQL: ",
mysql_stmt_error(stmt->my_stmt));
} else {
/* INSERT, DELETE, UPDATE, etc */
lua_pushinteger(L, (lua_Integer)mysql_stmt_affected_rows(stmt->my_stmt));
return 1;
}
}
/*
** Execute an SQL statement.
** Return a Cursor object if the statement is a query, otherwise
** return the number of tuples affected by the statement.
*/
static int conn_execute (lua_State *L)
{
int res;
/* prepare statement */
if((res = conn_prepare(L)) != 1) {
return res;
}
/* execute statement */
lua_insert(L, 1);
return stmt_execute(L);
}
/*
** Commit the current transaction.
*/
static int conn_commit (lua_State *L)
{
conn_data *conn = getconnection (L);
lua_pushboolean(L, !mysql_commit(conn->my_conn));
return 1;
}
/*
** Rollback the current transaction.
*/
static int conn_rollback (lua_State *L)
{
conn_data *conn = getconnection (L);
lua_pushboolean(L, !mysql_rollback(conn->my_conn));
return 1;
}
/*
** Set "auto commit" property of the connection. Modes ON/OFF
*/
static int conn_setautocommit (lua_State *L)
{
conn_data *conn = getconnection (L);
mysql_autocommit(conn->my_conn, lua_toboolean (L, 2) ? 1 : 0);
lua_pushboolean(L, 1);
return 1;
}
/*
** Get Last auto-increment id generated
*/
static int conn_getlastautoid (lua_State *L)
{
conn_data *conn = getconnection (L);
lua_pushinteger (L, (lua_Integer)mysql_insert_id(conn->my_conn));
return 1;
}
/*
** Create a new Connection object and push it on top of the stack.
*/
static int create_connection (lua_State *L, int env, MYSQL *my_conn)
{
conn_data *conn = (conn_data *)lua_newuserdata(L, sizeof(conn_data));
luasql_setmeta (L, LUASQL_CONNECTION_MYSQL);
/* fill in structure */
conn->closed = 0;
conn->env = LUA_NOREF;
conn->my_conn = my_conn;
lua_pushvalue (L, env);
conn->env = luaL_ref (L, LUA_REGISTRYINDEX);
return 1;
}
/*
** Reforms old connection style to new one
** Lua Input: source, [user], [pass], [host], [port]
** source: data source
** user, pass: data source authentication information
** Lua Returns:
** new connection details table
*/
static void env_connect_fix_old (lua_State *L)
{
static const char *const opt_names[] = {
"source",
"user",
"password",
"host",
"port",
NULL
};
int i, t = lua_gettop(L)-1;
lua_newtable(L);
lua_insert(L, 2);
for(i=0; opt_names[i] != NULL && i<t; ++i) {
lua_pushstring(L, opt_names[i]);
lua_pushvalue(L, i+3);
lua_settable(L, 2);
}
lua_settop(L, 2);
}
/*
** Connects to a data source.
** param: one string for each connection parameter, said
** datasource, username, password, host and port.
*/
static int env_connect (lua_State *L)
{
const char *sourcename;
MYSQL *conn;
getenvironment(L); /* validade environment */
if (lua_gettop(L) < 2) {
return luasql_faildirect(L, "No connection details provided");
}
if (!lua_istable(L, 2)) {
env_connect_fix_old(L);
}
if ((sourcename = luasql_table_optstring(L, 2, "source", NULL)) == NULL) {
return luasql_faildirect(L, "connection details table missing 'source'");
}
/* Try to init the connection object. */
conn = mysql_init(NULL);
if (conn == NULL) {
return luasql_faildirect(L, "error connecting: Out of memory.");
}
if (!mysql_real_connect(conn,
luasql_table_optstring(L, 2, "host", NULL),
luasql_table_optstring(L, 2, "user", NULL),
luasql_table_optstring(L, 2, "password", NULL),
sourcename,
(int)luasql_table_optnumber(L, 2, "port", 0),
NULL, 0)) {
char error_msg[100];
strncpy (error_msg, mysql_error(conn), 99);
mysql_close (conn); /* Close conn if connect failed */
return luasql_failmsg (L, "error connecting to database. MySQL: ", error_msg);
}
return create_connection(L, 1, conn);
}
/*
**
*/
static int env_gc (lua_State *L)
{
env_data *env = getenvironment(L);
if (env != NULL && !(env->closed)) {
env->closed = 1;
}
return 0;
}
/*
** Close environment object.
*/
static int env_close (lua_State *L)
{
env_data *env = getenvironment(L);
if (env->closed) {
lua_pushboolean (L, 0);
return 1;
}
env->closed = 1;
lua_pushboolean (L, 1);
return 1;
}
/*
** Create metatables for each class of object.
*/
static void create_metatables (lua_State *L)
{
struct luaL_Reg environment_methods[] = {
{"__gc", env_gc},
{"close", env_close},
{"connect", env_connect},
{NULL, NULL},
};
struct luaL_Reg connection_methods[] = {
{"__gc", conn_gc},
{"close", conn_close},
{"escape", escape_string},
{"prepare", conn_prepare},
{"execute", conn_execute},
{"commit", conn_commit},
{"rollback", conn_rollback},
{"setautocommit", conn_setautocommit},
{"getlastautoid", conn_getlastautoid},
{NULL, NULL},
};
struct luaL_Reg statement_methods[] = {
{"__gc", stmt_gc},
{"close", stmt_close},
{"execute", stmt_execute},
{NULL, NULL},
};
struct luaL_Reg cursor_methods[] = {
{"__gc", cur_gc},
{"close", cur_close},
{"getcolnames", cur_getcolnames},
{"getcoltypes", cur_getcoltypes},
{"fetch", cur_fetch},
{"numrows", cur_numrows},
{NULL, NULL},
};
luasql_createmeta (L, LUASQL_ENVIRONMENT_MYSQL, environment_methods);
luasql_createmeta (L, LUASQL_CONNECTION_MYSQL, connection_methods);
luasql_createmeta (L, LUASQL_STATEMENT_MYSQL, statement_methods);
luasql_createmeta (L, LUASQL_CURSOR_MYSQL, cursor_methods);
lua_pop (L, 4);
}
/*
** Creates an Environment and returns it.
*/
static int create_environment (lua_State *L)
{
env_data *env = (env_data *)lua_newuserdata(L, sizeof(env_data));
luasql_setmeta (L, LUASQL_ENVIRONMENT_MYSQL);
/* fill in structure */
env->closed = 0;
return 1;
}
/*
** Creates the metatables for the objects and registers the
** driver open method.
*/
LUASQL_API int luaopen_luasql_mysql (lua_State *L)
{
struct luaL_Reg driver[] = {
{"mysql", create_environment},
{NULL, NULL},
};
create_metatables (L);
luasql_reg_driver(L, driver);
lua_pushliteral (L, "_MYSQLVERSION");
lua_pushliteral (L, MYSQL_SERVER_VERSION);
lua_settable (L, -3);
return 1;
}