Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
559 lines (477 loc) · 12.8 KB

File metadata and controls

559 lines (477 loc) · 12.8 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
** LuaSQL, SQLite driver
** Author: Tiago Dionizio, Eduardo Quintao
** See Copyright Notice in license.html
** $Id: ls_sqlite.c,v 1.11 2007/08/22 18:37:06 tomas Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "sqlite.h"
#include "lua.h"
#include "lauxlib.h"
#if ! defined (LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
#include "compat-5.1.h"
#endif
#include "luasql.h"
#define LUASQL_ENVIRONMENT_SQLITE "SQLite environment"
#define LUASQL_CONNECTION_SQLITE "SQLite connection"
#define LUASQL_CURSOR_SQLITE "SQLite cursor"
typedef struct
{
short closed;
} env_data;
typedef struct
{
short closed;
int env; /* reference to environment */
short auto_commit; /* 0 for manual commit */
unsigned int cur_counter;
sqlite *sql_conn;
} conn_data;
typedef struct
{
short closed;
int conn; /* reference to connection */
int numcols; /* number of columns */
int colnames, coltypes; /* reference to column information tables */
sqlite_vm *sql_vm;
} cur_data;
LUASQL_API int luaopen_luasql_sqlite(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_SQLITE);
luaL_argcheck(L, env != NULL, 1, LUASQL_PREFIX"environment expected");
luaL_argcheck(L, !env->closed, 1, LUASQL_PREFIX"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_SQLITE);
luaL_argcheck(L, conn != NULL, 1, LUASQL_PREFIX"connection expected");
luaL_argcheck(L, !conn->closed, 1, LUASQL_PREFIX"connection is closed");
return conn;
}
/*
** Check for valid cursor.
*/
static cur_data *getcursor(lua_State *L) {
cur_data *cur = (cur_data *)luaL_checkudata (L, 1, LUASQL_CURSOR_SQLITE);
luaL_argcheck(L, cur != NULL, 1, LUASQL_PREFIX"cursor expected");
luaL_argcheck(L, !cur->closed, 1, LUASQL_PREFIX"cursor is closed");
return cur;
}
/*
** Finalizes the vm
** Return nil + errmsg or nil in case of sucess
*/
static int finalize(lua_State *L, cur_data *cur) {
char *errmsg;
if (sqlite_finalize(cur->sql_vm, &errmsg) != SQLITE_OK)
{
cur->sql_vm = NULL;
lua_pushnil(L);
lua_pushliteral(L, LUASQL_PREFIX);
lua_pushstring(L, errmsg);
sqlite_freemem(errmsg);
lua_concat(L, 2);
return 2;
}
lua_pushnil(L);
return 1;
}
/*
** Get another row of the given cursor.
*/
static int cur_fetch (lua_State *L) {
cur_data *cur = getcursor(L);
sqlite_vm *vm = cur->sql_vm;
const char **row = NULL;
int res;
if (vm == NULL)
return 0;
res = sqlite_step(vm, NULL, &row, NULL);
/* no more results? */
if (res == SQLITE_DONE)
return finalize(L, cur);
if (res != SQLITE_ROW)
return finalize(L, cur);
if (lua_istable (L, 2))
{
int i;
const char *opts = luaL_optstring(L, 3, "n");
if (strchr(opts, 'n') != NULL)
{
/* Copy values to numerical indices */
for (i = 0; i < cur->numcols;)
{
lua_pushstring(L, row[i]);
lua_rawseti(L, 2, ++i);
}
}
if (strchr(opts, 'a') != NULL)
{
/* Copy values to alphanumerical indices */
lua_rawgeti(L, LUA_REGISTRYINDEX, cur->colnames);
for (i = 0; i < cur->numcols; i++)
{
lua_rawgeti(L, -1, i+1);
lua_pushstring(L, row[i]);
lua_rawset (L, 2);
}
}
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)
lua_pushstring(L, row[i]);
return cur->numcols; /* return #numcols values */
}
}
/*
** Close the cursor on top of the stack.
** Return 1
*/
static int cur_close(lua_State *L)
{
conn_data *conn;
cur_data *cur = (cur_data *)luaL_checkudata(L, 1, LUASQL_CURSOR_SQLITE);
luaL_argcheck(L, cur != NULL, 1, LUASQL_PREFIX"cursor expected");
if (cur->closed) {
lua_pushboolean(L, 0);
return 1;
}
/* Nullify structure fields. */
cur->closed = 1;
sqlite_finalize(cur->sql_vm, NULL);
/* Decrement cursor counter on connection object */
lua_rawgeti (L, LUA_REGISTRYINDEX, cur->conn);
conn = lua_touserdata (L, -1);
conn->cur_counter--;
luaL_unref(L, LUA_REGISTRYINDEX, cur->conn);
luaL_unref(L, LUA_REGISTRYINDEX, cur->colnames);
luaL_unref(L, LUA_REGISTRYINDEX, cur->coltypes);
lua_pushboolean(L, 1);
return 1;
}
/*
** Return the list of field names.
*/
static int cur_getcolnames(lua_State *L)
{
cur_data *cur = getcursor(L);
lua_rawgeti(L, LUA_REGISTRYINDEX, cur->colnames);
return 1;
}
/*
** Return the list of field types.
*/
static int cur_getcoltypes(lua_State *L)
{
cur_data *cur = getcursor(L);
lua_rawgeti(L, LUA_REGISTRYINDEX, cur->coltypes);
return 1;
}
/*
** Create a new Cursor object and push it on top of the stack.
*/
/* static int create_cursor(lua_State *L, int conn, sqlite_vm *sql_vm,
int numcols, const char **row, const char **col_info)*/
static int create_cursor(lua_State *L, int o, conn_data *conn,
sqlite_vm *sql_vm, int numcols, const char **col_info)
{
int i;
cur_data *cur = (cur_data*)lua_newuserdata(L, sizeof(cur_data));
luasql_setmeta (L, LUASQL_CURSOR_SQLITE);
/* increment cursor count for the connection creating this cursor */
conn->cur_counter++;
/* fill in structure */
cur->closed = 0;
cur->conn = LUA_NOREF;
cur->numcols = numcols;
cur->colnames = LUA_NOREF;
cur->coltypes = LUA_NOREF;
cur->sql_vm = sql_vm;
lua_pushvalue(L, o);
cur->conn = luaL_ref(L, LUA_REGISTRYINDEX);
/* create table with column names */
lua_newtable(L);
for (i = 0; i < numcols;)
{
lua_pushstring(L, col_info[i]);
lua_rawseti(L, -2, ++i);
}
cur->colnames = luaL_ref(L, LUA_REGISTRYINDEX);
/* create table with column types */
lua_newtable(L);
for (i = 0; i < numcols;)
{
lua_pushstring(L, col_info[numcols+i]);
lua_rawseti(L, -2, ++i);
}
cur->coltypes = luaL_ref(L, LUA_REGISTRYINDEX);
return 1;
}
/*
** Close a Connection object.
*/
static int conn_close(lua_State *L)
{
conn_data *conn = (conn_data *)luaL_checkudata(L, 1, LUASQL_CONNECTION_SQLITE);
luaL_argcheck (L, conn != NULL, 1, LUASQL_PREFIX"connection expected");
if (conn->closed) {
lua_pushboolean(L, 0);
return 1;
}
if (conn->cur_counter > 0)
return luaL_error (L, LUASQL_PREFIX"there are open cursors");
/* Nullify structure fields. */
conn->closed = 1;
luaL_unref(L, LUA_REGISTRYINDEX, conn->env);
sqlite_close(conn->sql_conn);
lua_pushboolean(L, 1);
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)
{
conn_data *conn = getconnection(L);
const char *statement = luaL_checkstring(L, 2);
int res;
sqlite_vm *vm;
char *errmsg;
int numcols;
const char **col_info;
res = sqlite_compile(conn->sql_conn, statement, NULL, &vm, &errmsg);
if (res != SQLITE_OK)
{
lua_pushnil(L);
lua_pushliteral(L, LUASQL_PREFIX);
lua_pushstring(L, errmsg);
sqlite_freemem(errmsg);
lua_concat(L, 2);
return 2;
}
/* process first result to retrive query information and type */
res = sqlite_step(vm, &numcols, NULL, &col_info);
/* real query? if empty, must have numcols!=0 */
if ((res == SQLITE_ROW) || ((res == SQLITE_DONE) && numcols))
{
sqlite_reset(vm, NULL);
return create_cursor(L, 1, conn, vm, numcols, col_info);
}
if (res == SQLITE_DONE) /* and numcols==0, INSERT,UPDATE,DELETE statement */
{
sqlite_finalize(vm, NULL);
/* return number of columns changed */
lua_pushnumber(L, sqlite_changes(conn->sql_conn));
return 1;
}
/* error */
sqlite_finalize(vm, &errmsg);
lua_pushnil(L);
lua_pushliteral(L, LUASQL_PREFIX);
lua_pushstring(L, errmsg);
sqlite_freemem(errmsg);
lua_concat(L, 2);
return 2;
}
/*
** Commit the current transaction.
*/
static int conn_commit(lua_State *L)
{
char *errmsg;
conn_data *conn = getconnection(L);
int res;
const char *sql = "COMMIT";
if (conn->auto_commit == 0) sql = "COMMIT;BEGIN";
res = sqlite_exec(conn->sql_conn, sql, NULL, NULL, &errmsg);
if (res != SQLITE_OK)
{
lua_pushnil(L);
lua_pushliteral(L, LUASQL_PREFIX);
lua_pushstring(L, errmsg);
sqlite_freemem(errmsg);
lua_concat(L, 2);
return 2;
}
return 0;
}
/*
** Rollback the current transaction.
*/
static int conn_rollback(lua_State *L)
{
char *errmsg;
conn_data *conn = getconnection(L);
int res;
const char *sql = "ROLLBACK";
if (conn->auto_commit == 0) sql = "ROLLBACK;BEGIN";
res = sqlite_exec(conn->sql_conn, sql, NULL, NULL, &errmsg);
if (res != SQLITE_OK)
{
lua_pushnil(L);
lua_pushliteral(L, LUASQL_PREFIX);
lua_pushstring(L, errmsg);
sqlite_freemem(errmsg);
lua_concat(L, 2);
return 2;
}
return 0;
}
/*
** Set "auto commit" property of the connection.
** If 'true', then rollback current transaction.
** If 'false', then start a new transaction.
*/
static int conn_setautocommit(lua_State *L)
{
conn_data *conn = getconnection(L);
if (lua_toboolean(L, 2))
{
conn->auto_commit = 1;
/* undo active transaction - ignore errors */
sqlite_exec(conn->sql_conn, "ROLLBACK", NULL, NULL, NULL);
}
else
{
char *errmsg;
int res;
conn->auto_commit = 0;
res = sqlite_exec(conn->sql_conn, "BEGIN", NULL, NULL, &errmsg);
if (res != SQLITE_OK)
{
lua_pushliteral(L, LUASQL_PREFIX);
lua_pushstring(L, errmsg);
sqlite_freemem(errmsg);
lua_concat(L, 2);
lua_error(L);
}
}
lua_pushboolean(L, 1);
return 1;
}
/*
** Create a new Connection object and push it on top of the stack.
*/
static int create_connection(lua_State *L, int env, sqlite *sql_conn)
{
conn_data *conn = (conn_data*)lua_newuserdata(L, sizeof(conn_data));
luasql_setmeta(L, LUASQL_CONNECTION_SQLITE);
/* fill in structure */
conn->closed = 0;
conn->env = LUA_NOREF;
conn->auto_commit = 1;
conn->sql_conn = sql_conn;
conn->cur_counter = 0;
lua_pushvalue (L, env);
conn->env = luaL_ref (L, LUA_REGISTRYINDEX);
return 1;
}
/*
** Connects to a data source.
*/
static int env_connect(lua_State *L)
{
const char *sourcename;
sqlite *conn;
char *errmsg;
getenvironment(L); /* validate environment */
sourcename = luaL_checkstring(L, 2);
conn = sqlite_open(sourcename, 0, &errmsg);
if (conn == NULL)
{
lua_pushnil(L);
lua_pushliteral(L, LUASQL_PREFIX);
lua_pushstring(L, errmsg);
sqlite_freemem(errmsg);
lua_concat(L, 2);
return 2;
}
return create_connection(L, 1, conn);
}
/*
** Close environment object.
*/
static int env_close (lua_State *L)
{
env_data *env = (env_data *)luaL_checkudata(L, 1, LUASQL_ENVIRONMENT_SQLITE);
luaL_argcheck(L, env != NULL, 1, LUASQL_PREFIX"environment expected");
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[] = {
{"close", env_close},
{"connect", env_connect},
{NULL, NULL},
};
struct luaL_reg connection_methods[] = {
{"close", conn_close},
{"execute", conn_execute},
{"commit", conn_commit},
{"rollback", conn_rollback},
{"setautocommit", conn_setautocommit},
{NULL, NULL},
};
struct luaL_reg cursor_methods[] = {
{"close", cur_close},
{"getcolnames", cur_getcolnames},
{"getcoltypes", cur_getcoltypes},
{"fetch", cur_fetch},
{NULL, NULL},
};
luasql_createmeta(L, LUASQL_ENVIRONMENT_SQLITE, environment_methods);
luasql_createmeta(L, LUASQL_CONNECTION_SQLITE, connection_methods);
luasql_createmeta(L, LUASQL_CURSOR_SQLITE, cursor_methods);
lua_pop (L, 3);
}
/*
** 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_SQLITE);
/* 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_sqlite(lua_State *L)
{
struct luaL_reg driver[] = {
{"sqlite", create_environment},
{NULL, NULL},
};
create_metatables (L);
luaL_openlib (L, LUASQL_TABLENAME, driver, 0);
luasql_set_info (L);
return 1;
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.