1*0a6a1f1dSLionel Sambuc /* $NetBSD: sqlite.c,v 1.7 2014/07/19 18:38:34 lneto Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*
484d9c625SLionel Sambuc * Copyright (c) 2011, 2013 Marc Balmer <marc@msys.ch>
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc * are met:
1011be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc *
1611be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1711be35a1SLionel Sambuc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1811be35a1SLionel Sambuc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1911be35a1SLionel Sambuc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2011be35a1SLionel Sambuc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2111be35a1SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2211be35a1SLionel Sambuc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2311be35a1SLionel Sambuc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2411be35a1SLionel Sambuc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2511be35a1SLionel Sambuc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2611be35a1SLionel Sambuc */
2711be35a1SLionel Sambuc
2811be35a1SLionel Sambuc /* SQLite interface for Lua */
2911be35a1SLionel Sambuc
3011be35a1SLionel Sambuc #include <stdarg.h>
3111be35a1SLionel Sambuc #include <stdio.h>
3211be35a1SLionel Sambuc #include <string.h>
3311be35a1SLionel Sambuc #include <stdlib.h>
3411be35a1SLionel Sambuc #include <sqlite3.h>
3511be35a1SLionel Sambuc
3611be35a1SLionel Sambuc #include <lua.h>
3711be35a1SLionel Sambuc #include <lauxlib.h>
3811be35a1SLionel Sambuc #include <lualib.h>
3911be35a1SLionel Sambuc
4011be35a1SLionel Sambuc #define SQLITE_DB_METATABLE "SQLite database connection methods"
4111be35a1SLionel Sambuc #define SQLITE_STMT_METATABLE "SQLite statement methods"
4211be35a1SLionel Sambuc
4311be35a1SLionel Sambuc int luaopen_sqlite(lua_State*);
4411be35a1SLionel Sambuc
4511be35a1SLionel Sambuc static __printflike(2, 3) void
sqlite_error(lua_State * L,const char * fmt,...)4611be35a1SLionel Sambuc sqlite_error(lua_State *L, const char *fmt, ...)
4711be35a1SLionel Sambuc {
4811be35a1SLionel Sambuc va_list ap;
4911be35a1SLionel Sambuc int len;
5011be35a1SLionel Sambuc char *msg;
5111be35a1SLionel Sambuc
5211be35a1SLionel Sambuc va_start(ap, fmt);
5311be35a1SLionel Sambuc len = vasprintf(&msg, fmt, ap);
5411be35a1SLionel Sambuc va_end(ap);
5511be35a1SLionel Sambuc
5611be35a1SLionel Sambuc if (len != -1) {
5711be35a1SLionel Sambuc lua_pushstring(L, msg);
5811be35a1SLionel Sambuc free(msg);
5911be35a1SLionel Sambuc } else
6011be35a1SLionel Sambuc lua_pushstring(L, "vasprintf failed");
6111be35a1SLionel Sambuc lua_error(L);
6211be35a1SLionel Sambuc }
6311be35a1SLionel Sambuc
6411be35a1SLionel Sambuc static int
sqlite_initialize(lua_State * L)6511be35a1SLionel Sambuc sqlite_initialize(lua_State *L)
6611be35a1SLionel Sambuc {
6711be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_initialize());
6811be35a1SLionel Sambuc return 1;
6911be35a1SLionel Sambuc }
7011be35a1SLionel Sambuc
7111be35a1SLionel Sambuc static int
sqlite_shutdown(lua_State * L)7211be35a1SLionel Sambuc sqlite_shutdown(lua_State *L)
7311be35a1SLionel Sambuc {
7411be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_shutdown());
7511be35a1SLionel Sambuc return 1;
7611be35a1SLionel Sambuc }
7711be35a1SLionel Sambuc
7811be35a1SLionel Sambuc static int
sqlite_open(lua_State * L)7911be35a1SLionel Sambuc sqlite_open(lua_State *L)
8011be35a1SLionel Sambuc {
8111be35a1SLionel Sambuc sqlite3 **db;
8211be35a1SLionel Sambuc
8311be35a1SLionel Sambuc db = lua_newuserdata(L, sizeof(sqlite3 *));
8484d9c625SLionel Sambuc luaL_getmetatable(L, SQLITE_DB_METATABLE);
8584d9c625SLionel Sambuc lua_setmetatable(L, -2);
8684d9c625SLionel Sambuc
8784d9c625SLionel Sambuc if (lua_gettop(L) > 2)
8811be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_open_v2(luaL_checkstring(L, -3), db,
8911be35a1SLionel Sambuc (int)luaL_checkinteger(L, -2), NULL));
9084d9c625SLionel Sambuc else
9184d9c625SLionel Sambuc lua_pushinteger(L, sqlite3_open(luaL_checkstring(L, -2), db));
9211be35a1SLionel Sambuc return 2;
9311be35a1SLionel Sambuc
9411be35a1SLionel Sambuc }
9511be35a1SLionel Sambuc
9611be35a1SLionel Sambuc static int
sqlite_libversion(lua_State * L)9711be35a1SLionel Sambuc sqlite_libversion(lua_State *L)
9811be35a1SLionel Sambuc {
9911be35a1SLionel Sambuc lua_pushstring(L, sqlite3_libversion());
10011be35a1SLionel Sambuc return 1;
10111be35a1SLionel Sambuc }
10211be35a1SLionel Sambuc
10311be35a1SLionel Sambuc static int
sqlite_libversion_number(lua_State * L)10411be35a1SLionel Sambuc sqlite_libversion_number(lua_State *L)
10511be35a1SLionel Sambuc {
10611be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_libversion_number());
10711be35a1SLionel Sambuc return 1;
10811be35a1SLionel Sambuc }
10911be35a1SLionel Sambuc
11011be35a1SLionel Sambuc static int
sqlite_sourceid(lua_State * L)11111be35a1SLionel Sambuc sqlite_sourceid(lua_State *L)
11211be35a1SLionel Sambuc {
11311be35a1SLionel Sambuc lua_pushstring(L, sqlite3_sourceid());
11411be35a1SLionel Sambuc return 1;
11511be35a1SLionel Sambuc }
11611be35a1SLionel Sambuc
11711be35a1SLionel Sambuc static int
db_close(lua_State * L)11811be35a1SLionel Sambuc db_close(lua_State *L)
11911be35a1SLionel Sambuc {
12011be35a1SLionel Sambuc sqlite3 **db;
12111be35a1SLionel Sambuc
12211be35a1SLionel Sambuc db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
12311be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_close(*db));
12411be35a1SLionel Sambuc return 1;
12511be35a1SLionel Sambuc
12611be35a1SLionel Sambuc }
12711be35a1SLionel Sambuc
12811be35a1SLionel Sambuc static int
db_prepare(lua_State * L)12911be35a1SLionel Sambuc db_prepare(lua_State *L)
13011be35a1SLionel Sambuc {
13111be35a1SLionel Sambuc sqlite3 **db;
13211be35a1SLionel Sambuc sqlite3_stmt **stmt;
13311be35a1SLionel Sambuc const char *sql;
13411be35a1SLionel Sambuc
13511be35a1SLionel Sambuc db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
13611be35a1SLionel Sambuc stmt = lua_newuserdata(L, sizeof(sqlite3_stmt *));
13711be35a1SLionel Sambuc sql = luaL_checkstring(L, 2);
13811be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_prepare_v2(*db, sql,
13911be35a1SLionel Sambuc (int)strlen(sql) + 1, stmt, NULL));
14011be35a1SLionel Sambuc luaL_getmetatable(L, SQLITE_STMT_METATABLE);
14111be35a1SLionel Sambuc lua_setmetatable(L, -3);
14211be35a1SLionel Sambuc return 2;
14311be35a1SLionel Sambuc
14411be35a1SLionel Sambuc }
14511be35a1SLionel Sambuc
14611be35a1SLionel Sambuc static int
db_exec(lua_State * L)14711be35a1SLionel Sambuc db_exec(lua_State *L)
14811be35a1SLionel Sambuc {
14911be35a1SLionel Sambuc sqlite3 **db;
15011be35a1SLionel Sambuc
15111be35a1SLionel Sambuc db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
15211be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_exec(*db, lua_tostring(L, 2), NULL,
15311be35a1SLionel Sambuc NULL, NULL));
15411be35a1SLionel Sambuc return 1;
15511be35a1SLionel Sambuc }
15611be35a1SLionel Sambuc
15711be35a1SLionel Sambuc static int
db_errcode(lua_State * L)15811be35a1SLionel Sambuc db_errcode(lua_State *L)
15911be35a1SLionel Sambuc {
16011be35a1SLionel Sambuc sqlite3 **db;
16111be35a1SLionel Sambuc
16211be35a1SLionel Sambuc db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
16311be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_errcode(*db));
16411be35a1SLionel Sambuc return 1;
16511be35a1SLionel Sambuc }
16611be35a1SLionel Sambuc
16711be35a1SLionel Sambuc static int
db_errmsg(lua_State * L)16811be35a1SLionel Sambuc db_errmsg(lua_State *L)
16911be35a1SLionel Sambuc {
17011be35a1SLionel Sambuc sqlite3 **db;
17111be35a1SLionel Sambuc
17211be35a1SLionel Sambuc db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
17311be35a1SLionel Sambuc lua_pushstring(L, sqlite3_errmsg(*db));
17411be35a1SLionel Sambuc return 1;
17511be35a1SLionel Sambuc }
17611be35a1SLionel Sambuc
17711be35a1SLionel Sambuc static int
db_get_autocommit(lua_State * L)17811be35a1SLionel Sambuc db_get_autocommit(lua_State *L)
17911be35a1SLionel Sambuc {
18011be35a1SLionel Sambuc sqlite3 **db;
18111be35a1SLionel Sambuc
18211be35a1SLionel Sambuc db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
18311be35a1SLionel Sambuc lua_pushboolean(L, sqlite3_get_autocommit(*db));
18411be35a1SLionel Sambuc return 1;
18511be35a1SLionel Sambuc }
18611be35a1SLionel Sambuc
18711be35a1SLionel Sambuc static int
db_changes(lua_State * L)18811be35a1SLionel Sambuc db_changes(lua_State *L)
18911be35a1SLionel Sambuc {
19011be35a1SLionel Sambuc sqlite3 **db;
19111be35a1SLionel Sambuc
19211be35a1SLionel Sambuc db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
19311be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_changes(*db));
19411be35a1SLionel Sambuc return 1;
19511be35a1SLionel Sambuc }
19611be35a1SLionel Sambuc
19711be35a1SLionel Sambuc static int
stmt_bind(lua_State * L)19811be35a1SLionel Sambuc stmt_bind(lua_State *L)
19911be35a1SLionel Sambuc {
20011be35a1SLionel Sambuc sqlite3_stmt **stmt;
20111be35a1SLionel Sambuc int pidx;
20211be35a1SLionel Sambuc
20311be35a1SLionel Sambuc stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
20411be35a1SLionel Sambuc pidx = (int)luaL_checkinteger(L, 2);
20511be35a1SLionel Sambuc
20611be35a1SLionel Sambuc switch (lua_type(L, 3)) {
20711be35a1SLionel Sambuc case LUA_TNUMBER:
20811be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_bind_double(*stmt, pidx,
20911be35a1SLionel Sambuc lua_tonumber(L, 3)));
21011be35a1SLionel Sambuc break;
21111be35a1SLionel Sambuc case LUA_TSTRING:
21211be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_bind_text(*stmt, pidx,
21311be35a1SLionel Sambuc lua_tostring(L, 3), -1, SQLITE_TRANSIENT));
21411be35a1SLionel Sambuc break;
21511be35a1SLionel Sambuc case LUA_TNIL:
21611be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_bind_null(*stmt, pidx));
21711be35a1SLionel Sambuc break;
21811be35a1SLionel Sambuc default:
21911be35a1SLionel Sambuc sqlite_error(L, "unsupported data type %s",
22011be35a1SLionel Sambuc luaL_typename(L, 3));
22111be35a1SLionel Sambuc }
22211be35a1SLionel Sambuc return 1;
22311be35a1SLionel Sambuc }
22411be35a1SLionel Sambuc
22511be35a1SLionel Sambuc static int
stmt_bind_parameter_count(lua_State * L)22611be35a1SLionel Sambuc stmt_bind_parameter_count(lua_State *L)
22711be35a1SLionel Sambuc {
22811be35a1SLionel Sambuc sqlite3_stmt **stmt;
22911be35a1SLionel Sambuc
23011be35a1SLionel Sambuc stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
23111be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_bind_parameter_count(*stmt));
23211be35a1SLionel Sambuc return 1;
23311be35a1SLionel Sambuc }
23411be35a1SLionel Sambuc
23511be35a1SLionel Sambuc static int
stmt_bind_parameter_index(lua_State * L)23611be35a1SLionel Sambuc stmt_bind_parameter_index(lua_State *L)
23711be35a1SLionel Sambuc {
23811be35a1SLionel Sambuc sqlite3_stmt **stmt;
23911be35a1SLionel Sambuc
24011be35a1SLionel Sambuc stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
24111be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_bind_parameter_index(*stmt,
24211be35a1SLionel Sambuc lua_tostring(L, 2)));
24311be35a1SLionel Sambuc return 1;
24411be35a1SLionel Sambuc }
24511be35a1SLionel Sambuc
24611be35a1SLionel Sambuc static int
stmt_bind_parameter_name(lua_State * L)24711be35a1SLionel Sambuc stmt_bind_parameter_name(lua_State *L)
24811be35a1SLionel Sambuc {
24911be35a1SLionel Sambuc sqlite3_stmt **stmt;
25011be35a1SLionel Sambuc int pidx;
25111be35a1SLionel Sambuc
25211be35a1SLionel Sambuc stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
25311be35a1SLionel Sambuc pidx = (int)luaL_checkinteger(L, 2);
25411be35a1SLionel Sambuc lua_pushstring(L, sqlite3_bind_parameter_name(*stmt, pidx));
25511be35a1SLionel Sambuc return 1;
25611be35a1SLionel Sambuc }
25711be35a1SLionel Sambuc
25811be35a1SLionel Sambuc static int
stmt_step(lua_State * L)25911be35a1SLionel Sambuc stmt_step(lua_State *L)
26011be35a1SLionel Sambuc {
26111be35a1SLionel Sambuc sqlite3_stmt **stmt;
26211be35a1SLionel Sambuc
26311be35a1SLionel Sambuc stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
26411be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_step(*stmt));
26511be35a1SLionel Sambuc return 1;
26611be35a1SLionel Sambuc }
26711be35a1SLionel Sambuc
26811be35a1SLionel Sambuc static int
stmt_column_name(lua_State * L)26911be35a1SLionel Sambuc stmt_column_name(lua_State *L)
27011be35a1SLionel Sambuc {
27111be35a1SLionel Sambuc sqlite3_stmt **stmt;
27211be35a1SLionel Sambuc int cidx;
27311be35a1SLionel Sambuc
27411be35a1SLionel Sambuc stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
27511be35a1SLionel Sambuc cidx = (int)luaL_checkinteger(L, 2) - 1;
27611be35a1SLionel Sambuc
27711be35a1SLionel Sambuc lua_pushstring(L, sqlite3_column_name(*stmt, cidx));
27811be35a1SLionel Sambuc return 1;
27911be35a1SLionel Sambuc }
28011be35a1SLionel Sambuc
28111be35a1SLionel Sambuc static int
stmt_column_count(lua_State * L)28211be35a1SLionel Sambuc stmt_column_count(lua_State *L)
28311be35a1SLionel Sambuc {
28411be35a1SLionel Sambuc sqlite3_stmt **stmt;
28511be35a1SLionel Sambuc
28611be35a1SLionel Sambuc stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
28711be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_column_count(*stmt));
28811be35a1SLionel Sambuc return 1;
28911be35a1SLionel Sambuc }
29011be35a1SLionel Sambuc
29111be35a1SLionel Sambuc static int
stmt_column(lua_State * L)29211be35a1SLionel Sambuc stmt_column(lua_State *L)
29311be35a1SLionel Sambuc {
29411be35a1SLionel Sambuc sqlite3_stmt **stmt;
29511be35a1SLionel Sambuc int cidx;
29611be35a1SLionel Sambuc
29711be35a1SLionel Sambuc stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
29811be35a1SLionel Sambuc cidx = (int)luaL_checkinteger(L, 2) - 1;
29911be35a1SLionel Sambuc
30011be35a1SLionel Sambuc switch (sqlite3_column_type(*stmt, cidx)) {
30111be35a1SLionel Sambuc case SQLITE_INTEGER:
30211be35a1SLionel Sambuc lua_pushinteger(L, sqlite3_column_int(*stmt, cidx));
30311be35a1SLionel Sambuc break;
30411be35a1SLionel Sambuc case SQLITE_FLOAT:
30511be35a1SLionel Sambuc lua_pushnumber(L, sqlite3_column_double(*stmt, cidx));
30611be35a1SLionel Sambuc break;
30711be35a1SLionel Sambuc case SQLITE_TEXT:
30811be35a1SLionel Sambuc lua_pushstring(L, (const char *)sqlite3_column_text(*stmt,
30911be35a1SLionel Sambuc cidx));
31011be35a1SLionel Sambuc break;
31111be35a1SLionel Sambuc case SQLITE_BLOB:
31211be35a1SLionel Sambuc case SQLITE_NULL:
31311be35a1SLionel Sambuc lua_pushnil(L);
31411be35a1SLionel Sambuc break;
31511be35a1SLionel Sambuc }
31611be35a1SLionel Sambuc return 1;
31711be35a1SLionel Sambuc }
31811be35a1SLionel Sambuc
31911be35a1SLionel Sambuc static int
stmt_reset(lua_State * L)32011be35a1SLionel Sambuc stmt_reset(lua_State *L)
32111be35a1SLionel Sambuc {
32211be35a1SLionel Sambuc sqlite3_stmt **stmt;
32311be35a1SLionel Sambuc
32411be35a1SLionel Sambuc stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
32511be35a1SLionel Sambuc sqlite3_reset(*stmt);
32611be35a1SLionel Sambuc return 0;
32711be35a1SLionel Sambuc }
32811be35a1SLionel Sambuc
32911be35a1SLionel Sambuc static int
stmt_clear_bindings(lua_State * L)33011be35a1SLionel Sambuc stmt_clear_bindings(lua_State *L)
33111be35a1SLionel Sambuc {
33211be35a1SLionel Sambuc sqlite3_stmt **stmt;
33311be35a1SLionel Sambuc
33411be35a1SLionel Sambuc stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
33511be35a1SLionel Sambuc sqlite3_clear_bindings(*stmt);
33611be35a1SLionel Sambuc return 0;
33711be35a1SLionel Sambuc }
33811be35a1SLionel Sambuc
33911be35a1SLionel Sambuc static int
stmt_finalize(lua_State * L)34011be35a1SLionel Sambuc stmt_finalize(lua_State *L)
34111be35a1SLionel Sambuc {
34211be35a1SLionel Sambuc sqlite3_stmt **stmt;
34311be35a1SLionel Sambuc
34411be35a1SLionel Sambuc stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
34511be35a1SLionel Sambuc sqlite3_finalize(*stmt);
34611be35a1SLionel Sambuc return 0;
34711be35a1SLionel Sambuc }
34811be35a1SLionel Sambuc
34911be35a1SLionel Sambuc struct constant {
35011be35a1SLionel Sambuc const char *name;
35111be35a1SLionel Sambuc int value;
35211be35a1SLionel Sambuc };
35311be35a1SLionel Sambuc
35411be35a1SLionel Sambuc static const struct constant sqlite_constant[] = {
35511be35a1SLionel Sambuc /* SQLite return codes */
35611be35a1SLionel Sambuc { "OK", SQLITE_OK },
35711be35a1SLionel Sambuc { "ERROR", SQLITE_ERROR },
35811be35a1SLionel Sambuc { "INTERNAL", SQLITE_INTERNAL },
35911be35a1SLionel Sambuc { "PERM", SQLITE_PERM },
36011be35a1SLionel Sambuc { "ABORT", SQLITE_ABORT },
36111be35a1SLionel Sambuc { "BUSY", SQLITE_BUSY },
36211be35a1SLionel Sambuc { "LOCKED", SQLITE_LOCKED },
36311be35a1SLionel Sambuc { "NOMEM", SQLITE_NOMEM },
36411be35a1SLionel Sambuc { "READONLY", SQLITE_READONLY },
36511be35a1SLionel Sambuc { "INTERRUPT", SQLITE_INTERRUPT },
36611be35a1SLionel Sambuc { "IOERR", SQLITE_IOERR },
36711be35a1SLionel Sambuc { "CORRUPT", SQLITE_CORRUPT },
36884d9c625SLionel Sambuc { "NOTFOUND", SQLITE_NOTFOUND },
36911be35a1SLionel Sambuc { "FULL", SQLITE_FULL },
37011be35a1SLionel Sambuc { "CANTOPEN", SQLITE_CANTOPEN },
37184d9c625SLionel Sambuc { "PROTOCOL", SQLITE_PROTOCOL },
37211be35a1SLionel Sambuc { "EMPTY", SQLITE_EMPTY },
37311be35a1SLionel Sambuc { "SCHEMA", SQLITE_SCHEMA },
37411be35a1SLionel Sambuc { "TOOBIG", SQLITE_TOOBIG },
37511be35a1SLionel Sambuc { "CONSTRAINT", SQLITE_CONSTRAINT },
37611be35a1SLionel Sambuc { "MISMATCH", SQLITE_MISMATCH },
37711be35a1SLionel Sambuc { "MISUSE", SQLITE_MISUSE },
37811be35a1SLionel Sambuc { "NOLFS", SQLITE_NOLFS },
37911be35a1SLionel Sambuc { "AUTH", SQLITE_AUTH },
38011be35a1SLionel Sambuc { "FORMAT", SQLITE_FORMAT },
38111be35a1SLionel Sambuc { "RANGE", SQLITE_RANGE },
38211be35a1SLionel Sambuc { "NOTADB", SQLITE_NOTADB },
38311be35a1SLionel Sambuc { "ROW", SQLITE_ROW },
38411be35a1SLionel Sambuc { "DONE", SQLITE_DONE },
38511be35a1SLionel Sambuc
38611be35a1SLionel Sambuc /* File modes */
38711be35a1SLionel Sambuc { "OPEN_READONLY", SQLITE_OPEN_READONLY },
38811be35a1SLionel Sambuc { "OPEN_READWRITE", SQLITE_OPEN_READWRITE },
38984d9c625SLionel Sambuc { "OPEN_CREATE", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
39011be35a1SLionel Sambuc
39111be35a1SLionel Sambuc { NULL, 0 }
39211be35a1SLionel Sambuc };
39311be35a1SLionel Sambuc
39411be35a1SLionel Sambuc static void
gpio_set_info(lua_State * L)39511be35a1SLionel Sambuc gpio_set_info(lua_State *L)
39611be35a1SLionel Sambuc {
39711be35a1SLionel Sambuc lua_pushliteral(L, "_COPYRIGHT");
39884d9c625SLionel Sambuc lua_pushliteral(L, "Copyright (C) 2011, 2012, 2013 by "
39911be35a1SLionel Sambuc "Marc Balmer <marc@msys.ch>");
40011be35a1SLionel Sambuc lua_settable(L, -3);
40111be35a1SLionel Sambuc lua_pushliteral(L, "_DESCRIPTION");
40211be35a1SLionel Sambuc lua_pushliteral(L, "SQLite interface for Lua");
40311be35a1SLionel Sambuc lua_settable(L, -3);
40411be35a1SLionel Sambuc lua_pushliteral(L, "_VERSION");
40584d9c625SLionel Sambuc lua_pushliteral(L, "sqlite 1.0.3");
40611be35a1SLionel Sambuc lua_settable(L, -3);
40711be35a1SLionel Sambuc }
40811be35a1SLionel Sambuc
40911be35a1SLionel Sambuc int
luaopen_sqlite(lua_State * L)41011be35a1SLionel Sambuc luaopen_sqlite(lua_State* L)
41111be35a1SLionel Sambuc {
41211be35a1SLionel Sambuc static const struct luaL_Reg sqlite_methods[] = {
41311be35a1SLionel Sambuc { "initialize", sqlite_initialize },
41411be35a1SLionel Sambuc { "shutdown", sqlite_shutdown },
41511be35a1SLionel Sambuc { "open", sqlite_open },
41611be35a1SLionel Sambuc { "libversion", sqlite_libversion },
41711be35a1SLionel Sambuc { "libversion_number", sqlite_libversion_number },
41811be35a1SLionel Sambuc { "sourceid", sqlite_sourceid },
41911be35a1SLionel Sambuc { NULL, NULL }
42011be35a1SLionel Sambuc };
42111be35a1SLionel Sambuc static const struct luaL_Reg db_methods[] = {
42211be35a1SLionel Sambuc { "close", db_close },
42311be35a1SLionel Sambuc { "prepare", db_prepare },
42411be35a1SLionel Sambuc { "exec", db_exec },
42511be35a1SLionel Sambuc { "errcode", db_errcode },
42611be35a1SLionel Sambuc { "errmsg", db_errmsg },
42711be35a1SLionel Sambuc { "get_autocommit", db_get_autocommit },
42811be35a1SLionel Sambuc { "changes", db_changes },
42911be35a1SLionel Sambuc { NULL, NULL }
43011be35a1SLionel Sambuc };
43111be35a1SLionel Sambuc static const struct luaL_Reg stmt_methods[] = {
43211be35a1SLionel Sambuc { "bind", stmt_bind },
43311be35a1SLionel Sambuc { "bind_parameter_count", stmt_bind_parameter_count },
43411be35a1SLionel Sambuc { "bind_parameter_index", stmt_bind_parameter_index },
43511be35a1SLionel Sambuc { "bind_parameter_name", stmt_bind_parameter_name },
43611be35a1SLionel Sambuc { "step", stmt_step },
43711be35a1SLionel Sambuc { "column", stmt_column },
43811be35a1SLionel Sambuc { "reset", stmt_reset },
43911be35a1SLionel Sambuc { "clear_bindings", stmt_clear_bindings },
44011be35a1SLionel Sambuc { "finalize", stmt_finalize },
44111be35a1SLionel Sambuc { "column_name", stmt_column_name },
44211be35a1SLionel Sambuc { "column_count", stmt_column_count },
44311be35a1SLionel Sambuc { NULL, NULL }
44411be35a1SLionel Sambuc };
44511be35a1SLionel Sambuc int n;
44611be35a1SLionel Sambuc
44711be35a1SLionel Sambuc sqlite3_initialize();
44811be35a1SLionel Sambuc
449*0a6a1f1dSLionel Sambuc luaL_newlib(L, sqlite_methods);
450*0a6a1f1dSLionel Sambuc luaL_setfuncs(L, db_methods, 0);
451*0a6a1f1dSLionel Sambuc luaL_setfuncs(L, stmt_methods, 0);
45211be35a1SLionel Sambuc gpio_set_info(L);
45311be35a1SLionel Sambuc
45411be35a1SLionel Sambuc /* The database connection metatable */
45511be35a1SLionel Sambuc if (luaL_newmetatable(L, SQLITE_DB_METATABLE)) {
456*0a6a1f1dSLionel Sambuc luaL_setfuncs(L, db_methods, 0);
45711be35a1SLionel Sambuc
45811be35a1SLionel Sambuc lua_pushliteral(L, "__gc");
45911be35a1SLionel Sambuc lua_pushcfunction(L, db_close);
46011be35a1SLionel Sambuc lua_settable(L, -3);
46111be35a1SLionel Sambuc
46211be35a1SLionel Sambuc lua_pushliteral(L, "__index");
46311be35a1SLionel Sambuc lua_pushvalue(L, -2);
46411be35a1SLionel Sambuc lua_settable(L, -3);
46511be35a1SLionel Sambuc
46611be35a1SLionel Sambuc lua_pushliteral(L, "__metatable");
46711be35a1SLionel Sambuc lua_pushliteral(L, "must not access this metatable");
46811be35a1SLionel Sambuc lua_settable(L, -3);
46911be35a1SLionel Sambuc }
47011be35a1SLionel Sambuc lua_pop(L, 1);
47111be35a1SLionel Sambuc
47211be35a1SLionel Sambuc /* The statement metatable */
47311be35a1SLionel Sambuc if (luaL_newmetatable(L, SQLITE_STMT_METATABLE)) {
474*0a6a1f1dSLionel Sambuc luaL_setfuncs(L, stmt_methods, 0);
47511be35a1SLionel Sambuc
47611be35a1SLionel Sambuc lua_pushliteral(L, "__gc");
47711be35a1SLionel Sambuc lua_pushcfunction(L, stmt_finalize);
47811be35a1SLionel Sambuc lua_settable(L, -3);
47911be35a1SLionel Sambuc
48011be35a1SLionel Sambuc lua_pushliteral(L, "__index");
48111be35a1SLionel Sambuc lua_pushvalue(L, -2);
48211be35a1SLionel Sambuc lua_settable(L, -3);
48311be35a1SLionel Sambuc
48411be35a1SLionel Sambuc lua_pushliteral(L, "__metatable");
48511be35a1SLionel Sambuc lua_pushliteral(L, "must not access this metatable");
48611be35a1SLionel Sambuc lua_settable(L, -3);
48711be35a1SLionel Sambuc }
48811be35a1SLionel Sambuc lua_pop(L, 1);
48911be35a1SLionel Sambuc
49011be35a1SLionel Sambuc for (n = 0; sqlite_constant[n].name != NULL; n++) {
49111be35a1SLionel Sambuc lua_pushinteger(L, sqlite_constant[n].value);
49211be35a1SLionel Sambuc lua_setfield(L, -2, sqlite_constant[n].name);
49311be35a1SLionel Sambuc };
49411be35a1SLionel Sambuc return 1;
49511be35a1SLionel Sambuc }
496