-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathls_sqlite3.c
1371 lines (1171 loc) · 36 KB
/
ls_sqlite3.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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** LuaSQL, SQLite driver
** Author: Tiago Dionizio, Eduardo Quintao
** See Copyright Notice in license.html
** $Id: ls_sqlite3.c 3786 2015-11-06 22:54:28Z wini $
*/
#include "barracuda.h"
#include "luasql.h"
#include "sqlite3.h"
#ifdef _WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#ifndef BA_LIB
#define BA_LIB
#endif
#ifndef LUA_LIB
#define LUA_LIB
#endif
#define BASQLWEAKKEY "_BASQL3WK"
/*
The BA_BUILD macro, when enabled, releases the BA mutex when calling
SQLite functions. This makes it possible to have multiple concurrent
BA requests working on the database.
*/
#ifndef BA_BUILD
#define BA_BUILD
#endif
#ifdef BA_BUILD
#define GET_BAMUTEX ThreadMutex* m = baluaENV_getmutex(L)
#else
#define GET_BAMUTEX
#endif
#define LUASQL_ENVIRONMENT_SQLITE "SQLite3 environment"
#define LUASQL_CONNECTION_SQLITE "SQLite3 connection"
#define LUASQL_CURSOR_SQLITE "SQLite3 cursor"
/*
** because we are running in a web server we want to make sure that
** we collect each sql ennvironment when the request environment is
** ended. so we have changed the original luasql code to store the
** env,conn and cursor handles in the request environment rather than
** the registry. This means that these handles SHOULD collect when the
** http request is completed.
*/
#define LUASQL_REF(L) luaL_ref((L), SQL_ENV_IX);
#define LUASQL_UNREF(L,i) luaL_unref((L), SQL_ENV_IX, (i));
#define LUASQL_GETREF(L,i) lua_rawgeti(L, SQL_ENV_IX, (i));
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;
sqlite3 *sql_conn;
} connl_data;
typedef struct
{
short closed;
int conn; /* reference to connection */
int numcols; /* number of columns */
int colnames, coltypes; /* reference to column information tables */
sqlite3_stmt *sql_vm;
} cur_data;
typedef struct
{
short closed;
sqlite3_blob* sql_blob;
connl_data *conn;
int connref; /* reference to connection */
} blob_data;
#define LUASQLITE_BLOB "LUASQLITE BLOB"
LUASQL_API int luaopen_luasqlsqlite3(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 connl_data *getconnection(lua_State *L) {
connl_data *conn = (connl_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;
}
static const char* beginTrans(lua_State *L, connl_data *conn,
int strix, const char* optSql)
{
luaL_Buffer b;
const char* transType = "DEFERRED";
if (conn->auto_commit == 0)
transType = luaL_optstring(L, strix, transType);
luaL_buffinit(L, &b);
if(optSql)
luaL_addstring(&b, optSql);
luaL_addstring(&b, "BEGIN ");
luaL_addstring(&b, transType);
luaL_addstring(&b, " TRANSACTION");
luaL_pushresult(&b);
return lua_tostring(L, -1);
}
/*
** 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;
}
static const char* sherr[] = {
"OK" , /* SQLITE_OK 0 Successful result */
"ERROR" , /* SQLITE_ERROR 1 SQL error or missing database */
"INTERNAL" , /* SQLITE_INTERNAL 2 NOT USED. Internal logic error in SQLite */
"PERM" , /* SQLITE_PERM 3 Access permission denied */
"ABORT" , /* SQLITE_ABORT 4 Callback routine requested an abort */
"BUSY" , /* SQLITE_BUSY 5 The database file is locked */
"LOCKED" , /* SQLITE_LOCKED 6 A table in the database is locked */
"NOMEM" , /* SQLITE_NOMEM 7 A malloc() failed */
"READONLY" , /* SQLITE_READONLY 8 Attempt to write a readonly database */
"INTERRUPT" , /* SQLITE_INTERRUPT 9 Operation terminated by sqlite3_interrupt()*/
"IOERR" , /* SQLITE_IOERR 10 Some kind of disk I/O error occurred */
"CORRUPT" , /* SQLITE_CORRUPT 11 The database disk image is malformed */
"NOTFOUND" , /* SQLITE_NOTFOUND 12 NOT USED. Table or record not found */
"FULL" , /* SQLITE_FULL 13 Insertion failed because database is full */
"CANTOPEN" , /* SQLITE_CANTOPEN 14 Unable to open the database file */
"PROTOCOL" , /* SQLITE_PROTOCOL 15 NOT USED. Database lock protocol error */
"EMPTY" , /* SQLITE_EMPTY 16 Database is empty */
"SCHEMA" , /* SQLITE_SCHEMA 17 The database schema changed */
"TOOBIG" , /* SQLITE_TOOBIG 18 String or BLOB exceeds size limit */
"CONSTRAINT", /* SQLITE_CONSTRAINT 19 Abort due to contraint violation */
"MISMATCH" , /* SQLITE_MISMATCH 20 Data type mismatch */
"MISUSE" , /* SQLITE_MISUSE 21 Library used incorrectly */
"NOLFS" , /* SQLITE_NOLFS 22 Uses OS features not supported on host */
"AUTH" , /* SQLITE_AUTH 23 Authorization denied */
"FORMAT" , /* SQLITE_FORMAT 24 Auxiliary database format error */
"RANGE" , /* SQLITE_RANGE 25 2nd parameter to sqlite3_bind out of range */
"NOTADB" /* SQLITE_NOTADB 26 File opened that is not a database file */
};
static const char* getsherr(int i)
{
if (i >=0 && i <=26) return sherr[i];
if (i == SQLITE_ROW) return "ROW";
if(i == SQLITE_DONE) return "DONE";
return "?";
}
static int pusherr(lua_State* L, int err, const char* errmsg)
{
lua_pushnil(L);
lua_pushstring(L, getsherr(err));
lua_pushstring(L, errmsg);
return 3;
}
static int
doSqliteExec(lua_State *L, connl_data *conn, const char* sql, int tmoix)
{
int timeout;
int res;
char *errmsg = 0;
GET_BAMUTEX;
if(tmoix)
{
timeout=(int)luaL_optnumber(L,tmoix,0);
if(timeout) timeout = timeout/50+1; /* Convert to 50 millisec ticks */
sqlite3_busy_timeout(conn->sql_conn, 50);
}
else
timeout=0;
L_tryAgain:
balua_releasemutex(m);
res = sqlite3_exec(conn->sql_conn, sql, NULL, NULL, &errmsg);
balua_setmutex(m);
if(res == SQLITE_BUSY && timeout > 0)
{
lua_gc(L, LUA_GCCOLLECT, 0);
timeout -= 1;
goto L_tryAgain;
}
if(res != SQLITE_OK)
{
int ret = pusherr(L, res, errmsg ? errmsg : "?");
if(errmsg)
sqlite3_free(errmsg);
return ret;
}
lua_pushboolean(L, 1);
return 1;
}
/*
** Finalizes the vm
** Return nil + errmsg or nil in case of sucess
*/
static int finalize(lua_State *L, cur_data *cur)
{
const char *errmsg;
int res;
if (cur->sql_vm && sqlite3_finalize(cur->sql_vm) != SQLITE_OK) {
errmsg = sqlite3_errmsg(sqlite3_db_handle(cur->sql_vm));
res = sqlite3_errcode(sqlite3_db_handle(cur->sql_vm));
cur->sql_vm = NULL;
return pusherr(L, res, errmsg);
}
cur->sql_vm = NULL;
lua_pushnil(L);
return 1;
}
static void pushcol(lua_State* L, sqlite3_stmt *vm, int i)
{
int len;
switch (sqlite3_column_type(vm,i)) {
case SQLITE_BLOB:
len = sqlite3_column_bytes(vm,i);
lua_pushlstring(L, (char*)sqlite3_column_blob(vm,i), len);
break;
case SQLITE_NULL:
lua_pushnil(L);
break;
default:
lua_pushstring(L, (char*)sqlite3_column_text(vm, i));
}
}
/*
** Get another row of the given cursor.
*/
static int cur_fetch (lua_State *L) {
cur_data *cur = getcursor(L);
sqlite3_stmt *vm = cur->sql_vm;
int res;
GET_BAMUTEX;
if (vm == NULL)
return 0;
balua_releasemutex(m);
res = sqlite3_step(vm);
balua_setmutex(m);
if(cur->closed)
return luaL_error(L,LUASQL_PREFIX"cur closed");
/* 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;)
{
pushcol(L, vm, i);
lua_rawseti(L, 2, ++i);
}
}
if (strchr(opts, 'a') != NULL)
{
/* Copy values to alphanumerical indices */
LUASQL_GETREF(L, cur->colnames);
for (i = 0; i < cur->numcols; i++)
{
lua_rawgeti(L, -1, i+1);
pushcol(L, vm, 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)
pushcol(L, vm, i);
return cur->numcols; /* return #numcols values */
}
}
/*
** Close the cursor on top of the stack.
** Return 1
*/
static int cur_close(lua_State *L)
{
connl_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;
if (cur->sql_vm)
sqlite3_finalize(cur->sql_vm);
cur->sql_vm = NULL;
/* Decrement cursor counter on connection object */
LUASQL_GETREF(L, cur->conn);
conn = lua_touserdata (L, -1);
conn->cur_counter--;
/* because the cursor table is weak keyed/valued we should not
need to remove it from the table - maybe.
but the conn:close() might call this function while iterating
so we trash the key to avoid infinite loops */
lua_getuservalue(L, -1); /* env of the conn userdata */
lua_pushvalue(L,1);
lua_pushnil(L);
lua_rawset(L, -3);
lua_pop(L,1);
LUASQL_UNREF(L, cur->conn);
if (cur->colnames >= 0)
LUASQL_UNREF(L, cur->colnames);
if (cur->coltypes >= 0)
LUASQL_UNREF(L, 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);
LUASQL_GETREF(L, cur->colnames);
return 1;
}
/*
** Return the list of field types.
*/
static int cur_getcoltypes(lua_State *L)
{
cur_data *cur = getcursor(L);
LUASQL_GETREF(L, cur->coltypes);
return 1;
}
static cur_data* init_cursor(lua_State *L, int o, connl_data *conn,
sqlite3_stmt *sql_vm)
{
cur_data *cur = (cur_data*)lua_newuserdata(L, sizeof(cur_data));
luasql_setmeta (L, LUASQL_CURSOR_SQLITE);
/* add to connection env */
lua_getuservalue(L,o); /* we just happen to know this is the connection */
lua_pushvalue(L,-2); /* key is the userdata */
lua_pushlightuserdata(L,cur);
lua_rawset(L, -3);
lua_pop(L,1);
/* increment cursor count for the connection creating this cursor */
conn->cur_counter++;
/* fill in structure */
cur->closed = 0;
cur->numcols = -1;
cur->colnames = LUA_NOREF;
cur->coltypes = LUA_NOREF;
cur->sql_vm = sql_vm;
lua_pushvalue(L, o);
cur->conn = LUASQL_REF(L);
return cur;
}
static void create_namesntypes(lua_State *L, /* userdata cur is TOS */
sqlite3_stmt *sql_vm, int numcols, cur_data* cur)
{
int i;
/* fill in structure */
cur->numcols = numcols;
/* create table with column names */
lua_createtable(L,numcols,0);
for (i = 0; i < numcols;) {
lua_pushstring(L, sqlite3_column_name(sql_vm, i));
lua_rawseti(L, -2, ++i);
}
cur->colnames = LUASQL_REF(L);
/* create table with column types */
lua_createtable(L,numcols,0);
for (i = 0; i < numcols;) {
lua_pushstring(L, sqlite3_column_decltype(sql_vm, i));
lua_rawseti(L, -2, ++i);
}
cur->coltypes = LUASQL_REF(L);
}
/*
** Create a new Cursor object and push it on top of the stack.
*/
static int create_cursor(lua_State *L, int o, connl_data *conn,
sqlite3_stmt *sql_vm, int numcols)
{
cur_data *cur = (cur_data*)init_cursor(L, o, conn, sql_vm);
create_namesntypes(L, sql_vm, numcols, cur);
return 1;
}
#if 0
/*
** Collect a Connection object.
*/
static int conn_gc(lua_State *L)
{
connl_data *conn = (connl_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"); */
lua_pushboolean(L, 0);
return 1;
}
/* Nullify structure fields. */
conn->closed = 1;
LUASQL_UNREF(L, conn->env);
balua_releasemutex(m);
sqlite3_close(conn->sql_conn);
balua_setmutex(m);
lua_pushboolean(L, 1);
return 1;
}
#endif
/*
** Close a Connection object.
*/
static int conn_close(lua_State *L)
{
connl_data *conn=(connl_data *)luaL_checkudata(L,1,LUASQL_CONNECTION_SQLITE);
int rc,envix;
GET_BAMUTEX;
luaL_argcheck (L, conn != NULL, 1, LUASQL_PREFIX"connection expected");
if (conn->closed) {
lua_pushboolean(L, 0);
return 1;
}
lua_getuservalue(L, -1); /* env of the userdata */
envix = lua_gettop(L);
lua_pushvalue(L,BA_ENV_IX);
lua_pushvalue(L,SQL_ENV_IX);
lua_pushcclosure(L, cur_close,2);
while (lua_pushnil(L), lua_next(L, envix)) {
/* uses 'key' (at index -2) and 'value' (at index -1) */
lua_pushvalue(L, -3); /* the closure */
lua_pushvalue(L, -3); /* the key - (userdata) */
lua_pcall(L, 1, 0, 0);
lua_settop(L,envix + 1);
}
lua_settop(L,envix - 1);
/* if this happens in a GC then we are in trouble because
Lua has collected the object and we have no way of
calling close again. Also the sqlite3_close will also
fail and return SQLITE_BUSY.
*/
if (conn->cur_counter > 0)
return luaL_error (L, LUASQL_PREFIX"there are open cursors");
/* Nullify structure fields. */
conn->closed = 1;
LUASQL_UNREF(L, conn->env);
balua_releasemutex(m);
rc = sqlite3_close(conn->sql_conn);
balua_setmutex(m);
lua_pushboolean(L, rc == SQLITE_OK ? 1 : 0);
return 1;
}
/*
** Parsee an SQL statement.
** Return a Cursor object
*/
static int conn_prepare(lua_State *L)
{
connl_data *conn = getconnection(L);
const char *statement = luaL_checkstring(L, 2);
int res;
sqlite3_stmt *vm;
cur_data *cur;
const char *tail;
GET_BAMUTEX;
balua_releasemutex(m); /* release and pushes the acquire */
res = sqlite3_prepare_v2(conn->sql_conn, statement, -1, &vm, &tail);
balua_setmutex(m);/* acquire the mutex*/
if (res != SQLITE_OK)
return pusherr(L, res,
sqlite3_errmsg(conn->sql_conn));
cur = (cur_data*)init_cursor(L, 1, conn, vm);
return 1;
}
/*
** ::bind{ {"BLOB","X"|n},{"TEXT","X"},{"INTEGER",1},... }
** FLOAT, NULL
*/
static int stmt_bind(lua_State *L)
{
cur_data *cur = getcursor(L);
sqlite3_stmt *vm = cur->sql_vm;
int bcount = sqlite3_bind_parameter_count(vm);
int i;
luaL_checktype(L, 2, LUA_TTABLE);
sqlite3_reset(vm);
cur->numcols = -1;
if (cur->colnames != LUA_NOREF) {
LUASQL_UNREF(L, cur->colnames);
cur->colnames = LUA_NOREF;
}
if (cur->coltypes != LUA_NOREF) {
LUASQL_UNREF(L, cur->coltypes);
cur->coltypes = LUA_NOREF;
}
for (i=1; i <= bcount; ++i) {
const char* tp;
lua_rawgeti(L,2, i);
luaL_checktype(L, -1, LUA_TTABLE);
lua_rawgeti(L, -1, 1); /* type */
luaL_checktype(L, -1, LUA_TSTRING);
lua_rawgeti(L, -2, 2); /* value */
lua_remove(L,-3);
tp = lua_tostring(L, -2);
if (strcmp("BLOB",tp) == 0) {
if (lua_isnumber(L,-1)) { /* treat as zero blob */
int n = (int)lua_tointeger(L, -1);
sqlite3_bind_zeroblob(vm, i, n);
}
else {
size_t l;
const void* val = lua_tolstring(L, -1, &l);
sqlite3_bind_blob(vm, i, val, l, SQLITE_TRANSIENT);
}
}
else if (strcmp("TEXT",tp) == 0) {
size_t l;
const char* val = lua_tolstring(L, -1, &l);
sqlite3_bind_text(vm, i, val, l, SQLITE_TRANSIENT);
}
else if (strcmp("INTEGER",tp) == 0) {
lua_Number val = lua_tonumber(L, -1);
sqlite3_bind_int64(vm, i, (sqlite3_int64)val);
}
#ifndef SQLITE_OMIT_FLOATING_POINT
else if (strcmp("FLOAT",tp) == 0) {
lua_Number val = lua_tonumber(L, -1);
sqlite3_bind_double(vm, i, val);
}
#endif
else if (strcmp("NULL",tp) == 0) {
sqlite3_bind_null(vm, i);
}
else {
luaL_error(L, "invalid bind type parameter #u",i);
return 0;
}
lua_pop(L,2);
}
lua_settop(L,1);
return 1;
}
static int stmt_unbind(lua_State *L)
{
cur_data *cur = getcursor(L);
sqlite3_stmt *vm = cur->sql_vm;
int rc = sqlite3_clear_bindings(vm);
if (rc != SQLITE_OK) {
connl_data *conn;
LUASQL_GETREF(L, cur->conn);
conn = lua_touserdata (L, -1);
return pusherr(L, rc, sqlite3_errmsg(conn->sql_conn));
}
lua_pushboolean(L,1);
return 1;
}
static int stmt_execute(lua_State *L)
{
cur_data *cur = getcursor(L);
sqlite3_stmt *vm = cur->sql_vm;
connl_data *conn;
int numcols,res;
const char *errmsg;
GET_BAMUTEX;
LUASQL_GETREF(L, cur->conn);
conn = lua_touserdata (L, -1);
balua_releasemutex(m);
/* process first reirst result to retrive query information and type */
res = sqlite3_step(vm);
numcols = sqlite3_column_count(vm);
balua_setmutex(m);
/* real query? if empty, must have numcols!=0 */
if ((res == SQLITE_ROW) || ((res == SQLITE_DONE) && numcols)) {
sqlite3_reset(vm);
create_namesntypes(L, vm, numcols, cur);
lua_pushvalue(L,1);
return 1;
}
if (res == SQLITE_DONE) { /* and numcols==0, INSERT,UPDATE,DELETE statement */
// sqlite3_finalize(vm);
// cur->sql_vm = NULL;
/* return number of columns changed */
lua_pushnumber(L, sqlite3_changes(conn->sql_conn));
return 1;
}
/* error */
errmsg = sqlite3_errmsg(conn->sql_conn);
if (cur->sql_vm) {
sqlite3_finalize(vm);
cur->sql_vm = NULL;
}
return pusherr(L, res, errmsg);
}
/*
** 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)
{
connl_data *conn = getconnection(L);
const char *statement;
int res;
sqlite3_stmt *vm;
const char *errmsg;
int numcols;
const char *tail;
int timeout;
GET_BAMUTEX;
statement = luaL_checkstring(L, 2);
timeout=(int)luaL_optnumber(L,3,conn->auto_commit ? 150 : 0);
if(timeout)
{
timeout = timeout/50+1; /* Convert to 50 millisec ticks */
sqlite3_busy_timeout(conn->sql_conn, 50);
}
balua_releasemutex(m);
res = sqlite3_prepare_v2(conn->sql_conn, statement, -1, &vm, &tail);
balua_setmutex(m);
if (res != SQLITE_OK) {
return pusherr(L, res, sqlite3_errmsg(conn->sql_conn));
}
L_tryAgain:
balua_releasemutex(m);
/* process first result to retrive query information and type */
res = sqlite3_step(vm);
numcols = sqlite3_column_count(vm);
balua_setmutex(m);
if(res == SQLITE_BUSY && timeout > 0)
{
lua_gc(L, LUA_GCCOLLECT, 0);
timeout -= 1;
goto L_tryAgain;
}
/* real query? if empty, must have numcols!=0 */
if ((res == SQLITE_ROW) || ((res == SQLITE_DONE) && numcols))
{
sqlite3_reset(vm);
return create_cursor(L, 1, conn, vm, numcols);
}
if (res == SQLITE_DONE) /* and numcols==0, INSERT,UPDATE,DELETE statement */
{
sqlite3_finalize(vm);
/* return number of columns changed */
lua_pushnumber(L, sqlite3_changes(conn->sql_conn));
return 1;
}
/* error */
errmsg = sqlite3_errmsg(conn->sql_conn);
sqlite3_finalize(vm);
return pusherr(L, res, errmsg);
}
/*
** Commit the current transaction.
*/
static int conn_commit(lua_State *L)
{
connl_data *conn=getconnection(L);
return doSqliteExec(L, conn, beginTrans(L,conn,2,"COMMIT;"),0);
}
/*
** Rollback the current transaction.
*/
static int conn_rollback(lua_State *L)
{
connl_data *conn=getconnection(L);
return doSqliteExec(L, conn, beginTrans(L,conn,2,"ROLLBACK;"),0);
}
/*
** muli statement execute
*/
struct cbdata {
lua_State* L;
const char* opt;
int ix;
};
static int execcallback(void* vl,int ncols, char** data, char** names) /* Callback function */
{
struct cbdata* cb = (struct cbdata*)vl;
lua_State *L = (lua_State*)cb->L;
char opt = cb->opt[0];
int i;
//GET_BAMUTEX;
//balua_setmutex(m);/* Was released by conn_multiexec */
lua_newtable(L);
for (i=0; i < ncols; i++, names++, data++) {
if (opt == 'a') {
lua_pushstring(L, *names);
lua_pushstring(L, *data);
lua_rawset(L, -3);
}
else {
int n = lua_rawlen(L,-1) + 1;
lua_pushstring(L, *data);
lua_rawseti(L, -2, n);
}
}
i = lua_rawlen(L,cb->ix) + 1;
lua_rawseti(L, cb->ix, i);
//balua_releasemutex(m);
return 0;
}
static int conn_multiexec(lua_State *L)
{
char *errmsg;
connl_data *conn = getconnection(L);
const char* s = luaL_checkstring(L,2);
const char* opt = luaL_optstring(L,3,"n");
int res;
struct cbdata cb;
/* Disabled for now: The execcallback must set the BA mutex, but the
* internal SQLite mutex is set at this point. This can lead to a
* deadlock since not all sqlite3_xxx calls in this file releases
* the mutex. The code can be re-enabled if all sqlite3_xxx calls
* are designed as: release; sqlite3_xxx; set;
*/
//GET_BAMUTEX;
cb.L = L;
cb.opt = opt;
lua_settop(L,3);
lua_newtable(L);
cb.ix=lua_gettop(L);
//balua_releasemutex(m);
res = sqlite3_exec(conn->sql_conn, s, execcallback, &cb, &errmsg);
//balua_setmutex(m);
if (res != SQLITE_OK) {
int ret = pusherr(L, res, errmsg);
sqlite3_free(errmsg);
return ret;
}
lua_pushnil(L); /* first key */
if (lua_next(L, cb.ix) == 0) {
lua_pushboolean(L,1);
return 1;
}
lua_pop(L,1);
lua_pushboolean(L,1);
lua_pushvalue(L,cb.ix);
return 2;
}
static int conn_getlastautoid(lua_State *L)
{
connl_data *conn = getconnection(L);
lua_pushnumber(L, (lua_Number)sqlite3_last_insert_rowid(conn->sql_conn));
return 1;
}
static int conn_setbusytimeout(lua_State *L)
{
connl_data *conn = getconnection(L);
int ms = (int)(luaL_checknumber(L,2));
luaL_argcheck(L, ms >= 0, 2, "invalid timeout (secs)");
sqlite3_busy_timeout(conn->sql_conn, ms);
return 0;
}
static int conn_tablelist(lua_State *L) {
const char* sql = "SELECT name FROM sqlite_master "
"WHERE type='table' ORDER BY name;";
int rc,res;
cur_data *cur;
sqlite3_stmt *vm;
int i=0;
lua_settop(L,1);
lua_pushstring(L, sql);
rc = conn_execute(L);
if (rc!=1 || !lua_isuserdata(L,lua_gettop(L))) return rc;
lua_replace(L,1);
lua_settop(L,1);
lua_newtable(L);
cur = getcursor(L);
vm = cur->sql_vm;
if (vm == NULL) return 0;
for(;;) {
res = sqlite3_step(vm);
/* no more results? */
if (res == SQLITE_DONE) break;
if (res != SQLITE_ROW) break;
lua_pushstring(L, (char*)sqlite3_column_text(vm, 0));
lua_rawseti(L, 2, ++i);
}
cur_close(L);
lua_settop(L,2);
return 1;
}
/*
** 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)
{
int tmoix;
connl_data *conn = getconnection(L);
int isBool=lua_isboolean(L,2);
if(isBool && lua_toboolean(L, 2))
{
conn->auto_commit = 1;
return doSqliteExec(L, conn, "ROLLBACK",0);
}
conn->auto_commit = 0;
tmoix=isBool?4:3;
return doSqliteExec(L, conn, beginTrans(L,conn,isBool?3:2, 0),
lua_isnumber(L,tmoix) ? tmoix : 0);
}
static int blob_close(lua_State* L)
{
blob_data *blob = (blob_data *)luaL_checkudata(L, 1, LUASQLITE_BLOB);
int rc;
sqlite3_blob* sql_blob;
luaL_argcheck (L, blob != NULL, 1, LUASQL_PREFIX"blob expected");
sql_blob = blob->sql_blob;
blob->closed = 1;
if (!sql_blob) {
lua_pushboolean(L, 0);
return 1;
}
LUASQL_UNREF(L, blob->connref);
/* Nullify structure fields. */
blob->sql_blob = NULL;
rc = sqlite3_blob_close(sql_blob);
lua_pushboolean(L, rc == SQLITE_OK ? 1 : 0);
return 1;
}
static int blob_write(lua_State* L)
{
blob_data *blob = (blob_data *)luaL_checkudata(L, 1, LUASQLITE_BLOB);
int rc;
sqlite3_blob* sql_blob;
size_t l;
const char* p = luaL_checklstring(L,2,&l);
int bofs = (int)luaL_optinteger(L,3,0);
luaL_argcheck (L, blob != NULL&& blob->sql_blob != NULL, 1,
LUASQL_PREFIX"blob expected");
sql_blob = blob->sql_blob;