LuaSQLite3

Check-in [22341c42be]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add automatic re-prepare of stepped statements yielding schema errors.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 22341c42beb17865550965cf7e9908e5e425f125
User & Date: e@ecd9bf9a-ecc1-ed47-8102-3ce978d4cc14 2006-09-02 04:55:50
Context
2006-09-04
20:24
Reset persistent statements when for-loop terminates. check-in: 0da9110fc8 user: e@ecd9bf9a-ecc1-ed47-8102-3ce978d4cc14 tags: trunk
2006-09-02
04:55
Add automatic re-prepare of stepped statements yielding schema errors. check-in: 22341c42be user: e@ecd9bf9a-ecc1-ed47-8102-3ce978d4cc14 tags: trunk
03:30
Initial check-in from Tiago's 0.1 + e's recent changes. check-in: b2f0a11ca9 user: e@ecd9bf9a-ecc1-ed47-8102-3ce978d4cc14 tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to lsqlite3.c.

1
2

3
4
5
6
7
8
9
..
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
...
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
...
266
267
268
269
270
271
272






























273
274
275
276
277
278
279
...
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
...
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
....
1441
1442
1443
1444
1445
1446
1447


1448
1449
1450
1451
1452
1453
1454
1455
....
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470

1471
1472
1473
1474
1475
1476
1477
....
1546
1547
1548
1549
1550
1551
1552


1553
1554
1555
1556
1557
1558
1559
1560
....
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
/************************************************************************
* Author    : Tiago Dionizio (tngd@mega.ist.utl.pt)                     *

* Library   : lsqlite3 - a SQLite 3 database binding for Lua 5          *
*                                                                       *
* Permission is hereby granted, free of charge, to any person obtaining *
* a copy of this software and associated documentation files (the       *
* "Software"), to deal in the Software without restriction, including   *
* without limitation the rights to use, copy, modify, merge, publish,   *
* distribute, sublicense, and/or sell copies of the Software, and to    *
................................................................................
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                *
************************************************************************/

/************************************************************************
$Id: lsqlite3.c,v 1.3 2004/09/05 17:50:32 tngd Exp $

To consider:
------------

EXPERIMENTAL APIs

* sqlite3_progress_handler (implemented)
* sqlite3_commit_hook

TODO?

* sqlite3_create_collation

Changes:
04-09-2004
----------
    * changed second return value of db:compile to be the rest of the
    sql statement that was not processed instead of the number of
    characters of sql not processed (situation in case of success).
    * progress callback register function parameter order changed.
    number of opcodes is given before the callback now.

29-08-2004 e
------------
    * added version() (now supported in sqlite 3.0.5)
    * added db:errmsg db:errcode db:total_changes
    * rename vm:get_column to vm:get_value
    * merge in Tiago's v1.11 change in dbvm_tostring

23-06-2004 e
------------
    * heavily revised for SQLite3 C API
    * row values now returned as native type (not always text)
    * added db:nrows (named rows)
    * added vm:bind_blob
    * added vm:get_column
    * removed encode_binary decode_binary (no longer needed or supported)
    * removed version encoding error_string (unsupported in v 3.0.1 -- soon?)

09-04-2004
----------
    * renamed db:rows to db:urows
    * renamed db:prows to db:rows

    * added vm:get_unames()
    * added vm:get_utypes()
    * added vm:get_uvalues()

08-04-2004
----------
    * changed db:encoding() and db:version() to use sqlite_libencoding() and
    sqlite_libversion()

    * added vm:columns()
    * added vm:get_named_types()
    * added vm:get_named_values()

    * added db:prows - like db:rows but returns a table with the column values
    instead of returning multiple columns seperatly on each iteration

    * added compatibility functions idata,iname,itype,data,type

    * added luaopen_sqlite_module. allow the library to be loaded without
    setting a global variable. does the same as luaopen_sqlite, but does not
    set the global name "sqlite".

    * vm:bind now also returns an error string in case of error

31-03-2004 - 01-04-2004
-----------------------
    * changed most of the internals. now using references (luaL_ref) in
    most of the places

    * make the virtual machine interface seperate from the database
    handle. db:compile now returns a vm handle

    * added db:rows [for ... in db:rows(...) do ... end]

    * added db:close_vm

    * added sqlite.encode_binary and sqlite.decode_binary

    * attempt to do a strict checking on the return type of the user
    defined functions returned values

18-01-2004
----------
    * add check on sql function callback to ensure there is enough stack
    space to pass column values as parameters

03-12-2003
----------
    * callback functions now have to return boolean values to abort or
    continue operation instead of a zero or non-zero value

06-12-2003
----------
    * make version member of sqlite table a function instead of a string
************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "lua.h"
#include "lauxlib.h"

................................................................................
    /* sqlite3_step info */
    int columns;            /* number of columns in result */
    char has_values;        /* true when step succeeds */

    char temp;              /* temporary vm used in db:rows */
};


static sdb_vm *newvm(lua_State *L, sdb *db) {
    sdb_vm *svm = (sdb_vm*)lua_newuserdata(L, sizeof(sdb_vm));

    luaL_getmetatable(L, sqlite_vm_meta);
    lua_setmetatable(L, -2);        /* set metatable */

    svm->db = db;
    svm->columns = 0;
    svm->has_values = 0;
    svm->vm = NULL;
    svm->temp = 0;

    /* add an entry on the database table */
    lua_pushlightuserdata(L, db);
    lua_rawget(L, LUA_REGISTRYINDEX);
    lua_pushlightuserdata(L, svm);
    lua_pushvalue(L, -1);
    lua_rawset(L, -3);
    lua_pop(L, 1);

    return svm;
}

static int cleanupvm(lua_State *L, sdb_vm *svm) {
................................................................................
    if (!svm->vm) return 0;

    lua_pushnumber(L, sqlite3_finalize(svm->vm));
    svm->vm = NULL;
    return 1;
}
































static sdb_vm *lsqlite_getvm(lua_State *L, int index) {
    sdb_vm *svm = (sdb_vm*)luaL_checkudata(L, index, sqlite_vm_meta);
    if (svm == NULL) luaL_argerror(L, index, "bad sqlite virtual machine");
    return svm;
}

................................................................................
    return 0;
}

static int dbvm_step(lua_State *L) {
    int result;
    sdb_vm *svm = lsqlite_checkvm(L, 1);

    result = sqlite3_step(svm->vm);
    svm->has_values = result == SQLITE_ROW ? 1 : 0;
    svm->columns = sqlite3_data_count(svm->vm);

    lua_pushnumber(L, result);
    return 1;
}

................................................................................
    lua_pushlightuserdata(L, db);
    lua_rawget(L, LUA_REGISTRYINDEX);

    /* close all used handles */
    top = lua_gettop(L);
    lua_pushnil(L);
    while (lua_next(L, -2)) {
        sdb_vm *svm = lua_touserdata(L, -1);
        cleanupvm(L, svm);

        lua_settop(L, top);
        lua_pushnil(L);
    }

    lua_pop(L, 1); /* pop vm table */
................................................................................
** returns: code, compiled length or error message
*/
static int db_prepare(lua_State *L) {
    sdb *db = lsqlite_checkdb(L, 1);
    const char *sql = luaL_checkstring(L, 2);
    int sql_len = lua_strlen(L, 2);
    const char *sqltail;


    sdb_vm *svm = newvm(L, db);

    if (sqlite3_prepare(db->db, sql, sql_len, &svm->vm, &sqltail) != SQLITE_OK) {
        cleanupvm(L, svm);

        lua_pushnil(L);
        lua_pushnumber(L, sqlite3_errcode(db->db));
        return 2;
................................................................................
    lua_pushstring(L, sqltail);
    return 2;
}

static int db_do_next_row(lua_State *L, int packed) {
    int result;
    sdb_vm *svm = lsqlite_checkvm(L, 1);
    sqlite3_stmt *vm = svm->vm;
    int columns;
    int i;

    result = sqlite3_step(vm);

    svm->has_values = result == SQLITE_ROW ? 1 : 0;
    svm->columns = columns = sqlite3_data_count(vm);

    if (result == SQLITE_ROW) {
        if (packed) {
            lua_newtable(L);
            if (packed == 1) {
................................................................................
static int dbvm_urows(lua_State *L) {
    return dbvm_do_rows(L, db_next_row);
}

static int db_do_rows(lua_State *L, int(*f)(lua_State *)) {
    sdb *db = lsqlite_checkdb(L, 1);
    const char *sql = luaL_checkstring(L, 2);


    sdb_vm *svm = newvm(L, db);
    svm->temp = 1;

    if (sqlite3_prepare(db->db, sql, -1, &svm->vm, NULL) != SQLITE_OK) {
        cleanupvm(L, svm);

        lua_pushstring(L, sqlite3_errmsg(svm->db->db));
        lua_error(L);
................................................................................
    /* free associated virtual machines */
    lua_pushlightuserdata(L, db);
    lua_rawget(L, LUA_REGISTRYINDEX);

    /* close all used handles */
    lua_pushnil(L);
    while (lua_next(L, -2)) {
        sdb_vm *svm = lua_touserdata(L, -1);

        if ((!temp || svm->temp) && svm->vm)
        {
            sqlite3_finalize(svm->vm);
            svm->vm = NULL;
        }



>







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







>












|



|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







 







|







 







>
>
|







 







|



|
>







 







>
>
|







 







|







1
2
3
4
5
6
7
8
9
10
..
19
20
21
22
23
24
25





































































































26
27
28
29
30
31
32
...
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
...
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
...
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
...
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
....
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
....
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
....
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
....
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
/************************************************************************
* Author    : Tiago Dionizio (tngd@mega.ist.utl.pt)                     *
* Author    : Doug Currie (doug.currie@alum.mit.edu)                    *
* Library   : lsqlite3 - a SQLite 3 database binding for Lua 5          *
*                                                                       *
* Permission is hereby granted, free of charge, to any person obtaining *
* a copy of this software and associated documentation files (the       *
* "Software"), to deal in the Software without restriction, including   *
* without limitation the rights to use, copy, modify, merge, publish,   *
* distribute, sublicense, and/or sell copies of the Software, and to    *
................................................................................
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                *
************************************************************************/






































































































#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "lua.h"
#include "lauxlib.h"

................................................................................
    /* sqlite3_step info */
    int columns;            /* number of columns in result */
    char has_values;        /* true when step succeeds */

    char temp;              /* temporary vm used in db:rows */
};

/* called with sql text on the lua stack */
static sdb_vm *newvm(lua_State *L, sdb *db) {
    sdb_vm *svm = (sdb_vm*)lua_newuserdata(L, sizeof(sdb_vm));

    luaL_getmetatable(L, sqlite_vm_meta);
    lua_setmetatable(L, -2);        /* set metatable */

    svm->db = db;
    svm->columns = 0;
    svm->has_values = 0;
    svm->vm = NULL;
    svm->temp = 0;

    /* add an entry on the database table: svm -> sql text */
    lua_pushlightuserdata(L, db);
    lua_rawget(L, LUA_REGISTRYINDEX);
    lua_pushlightuserdata(L, svm);
    lua_pushvalue(L, -4); /* the sql text */
    lua_rawset(L, -3);
    lua_pop(L, 1);

    return svm;
}

static int cleanupvm(lua_State *L, sdb_vm *svm) {
................................................................................
    if (!svm->vm) return 0;

    lua_pushnumber(L, sqlite3_finalize(svm->vm));
    svm->vm = NULL;
    return 1;
}

static int stepvm(lua_State *L, sdb_vm *svm) {
    int result;
    int loop_limit = 3;
    while ( loop_limit-- ) {
        result = sqlite3_step(svm->vm);
        if ( result==SQLITE_ERROR ) {
          result = sqlite3_reset (svm->vm);
        }
        if ( result==SQLITE_SCHEMA ) {
            sqlite3_stmt *vn;
            const char *sql;
            /* recover sql text */
            lua_pushlightuserdata(L, svm->db);
            lua_rawget(L, LUA_REGISTRYINDEX);
            lua_pushlightuserdata(L, svm);
            lua_rawget(L, -2); /* sql text */
            sql = luaL_checkstring(L, -1);
            /* re-prepare */
            result = sqlite3_prepare(svm->db->db, sql, -1, &vn, NULL);
            if (result != SQLITE_OK) break;
            sqlite3_transfer_bindings(svm->vm, vn);
            sqlite3_finalize(svm->vm);
            svm->vm = vn;
            lua_pop(L,2);
        } else {
          break;
        }
    }
    return result;
}

static sdb_vm *lsqlite_getvm(lua_State *L, int index) {
    sdb_vm *svm = (sdb_vm*)luaL_checkudata(L, index, sqlite_vm_meta);
    if (svm == NULL) luaL_argerror(L, index, "bad sqlite virtual machine");
    return svm;
}

................................................................................
    return 0;
}

static int dbvm_step(lua_State *L) {
    int result;
    sdb_vm *svm = lsqlite_checkvm(L, 1);

    result = stepvm(L, svm);
    svm->has_values = result == SQLITE_ROW ? 1 : 0;
    svm->columns = sqlite3_data_count(svm->vm);

    lua_pushnumber(L, result);
    return 1;
}

................................................................................
    lua_pushlightuserdata(L, db);
    lua_rawget(L, LUA_REGISTRYINDEX);

    /* close all used handles */
    top = lua_gettop(L);
    lua_pushnil(L);
    while (lua_next(L, -2)) {
        sdb_vm *svm = lua_touserdata(L, -2); /* key: vm; val: sql text */
        cleanupvm(L, svm);

        lua_settop(L, top);
        lua_pushnil(L);
    }

    lua_pop(L, 1); /* pop vm table */
................................................................................
** returns: code, compiled length or error message
*/
static int db_prepare(lua_State *L) {
    sdb *db = lsqlite_checkdb(L, 1);
    const char *sql = luaL_checkstring(L, 2);
    int sql_len = lua_strlen(L, 2);
    const char *sqltail;
    sdb_vm *svm;
    lua_settop(L,2); /* sql is on top of stack for call to newvm */
    svm = newvm(L, db);

    if (sqlite3_prepare(db->db, sql, sql_len, &svm->vm, &sqltail) != SQLITE_OK) {
        cleanupvm(L, svm);

        lua_pushnil(L);
        lua_pushnumber(L, sqlite3_errcode(db->db));
        return 2;
................................................................................
    lua_pushstring(L, sqltail);
    return 2;
}

static int db_do_next_row(lua_State *L, int packed) {
    int result;
    sdb_vm *svm = lsqlite_checkvm(L, 1);
    sqlite3_stmt *vm;
    int columns;
    int i;

    result = stepvm(L, svm);
    vm = svm->vm; /* stepvm may change svm->vm if re-prepare is needed */
    svm->has_values = result == SQLITE_ROW ? 1 : 0;
    svm->columns = columns = sqlite3_data_count(vm);

    if (result == SQLITE_ROW) {
        if (packed) {
            lua_newtable(L);
            if (packed == 1) {
................................................................................
static int dbvm_urows(lua_State *L) {
    return dbvm_do_rows(L, db_next_row);
}

static int db_do_rows(lua_State *L, int(*f)(lua_State *)) {
    sdb *db = lsqlite_checkdb(L, 1);
    const char *sql = luaL_checkstring(L, 2);
    sdb_vm *svm;
    lua_settop(L,2); /* sql is on top of stack for call to newvm */
    svm = newvm(L, db);
    svm->temp = 1;

    if (sqlite3_prepare(db->db, sql, -1, &svm->vm, NULL) != SQLITE_OK) {
        cleanupvm(L, svm);

        lua_pushstring(L, sqlite3_errmsg(svm->db->db));
        lua_error(L);
................................................................................
    /* free associated virtual machines */
    lua_pushlightuserdata(L, db);
    lua_rawget(L, LUA_REGISTRYINDEX);

    /* close all used handles */
    lua_pushnil(L);
    while (lua_next(L, -2)) {
        sdb_vm *svm = lua_touserdata(L, -2); /* key: vm; val: sql text */

        if ((!temp || svm->temp) && svm->vm)
        {
            sqlite3_finalize(svm->vm);
            svm->vm = NULL;
        }