tools/llvm: Do not build with symbols
[minix3.git] / lib / lua / sqlite / sqlite.c
blobfe33a13d2683bc18b689cc26a21567e4eea11b58
1 /* $NetBSD: sqlite.c,v 1.6 2013/10/27 12:38:08 mbalmer Exp $ */
3 /*
4 * Copyright (c) 2011, 2013 Marc Balmer <marc@msys.ch>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 /* SQLite interface for Lua */
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <sqlite3.h>
36 #include <lua.h>
37 #include <lauxlib.h>
38 #include <lualib.h>
40 #define SQLITE_DB_METATABLE "SQLite database connection methods"
41 #define SQLITE_STMT_METATABLE "SQLite statement methods"
43 int luaopen_sqlite(lua_State*);
45 static __printflike(2, 3) void
46 sqlite_error(lua_State *L, const char *fmt, ...)
48 va_list ap;
49 int len;
50 char *msg;
52 va_start(ap, fmt);
53 len = vasprintf(&msg, fmt, ap);
54 va_end(ap);
56 if (len != -1) {
57 lua_pushstring(L, msg);
58 free(msg);
59 } else
60 lua_pushstring(L, "vasprintf failed");
61 lua_error(L);
64 static int
65 sqlite_initialize(lua_State *L)
67 lua_pushinteger(L, sqlite3_initialize());
68 return 1;
71 static int
72 sqlite_shutdown(lua_State *L)
74 lua_pushinteger(L, sqlite3_shutdown());
75 return 1;
78 static int
79 sqlite_open(lua_State *L)
81 sqlite3 **db;
83 db = lua_newuserdata(L, sizeof(sqlite3 *));
84 luaL_getmetatable(L, SQLITE_DB_METATABLE);
85 lua_setmetatable(L, -2);
87 if (lua_gettop(L) > 2)
88 lua_pushinteger(L, sqlite3_open_v2(luaL_checkstring(L, -3), db,
89 (int)luaL_checkinteger(L, -2), NULL));
90 else
91 lua_pushinteger(L, sqlite3_open(luaL_checkstring(L, -2), db));
92 return 2;
96 static int
97 sqlite_libversion(lua_State *L)
99 lua_pushstring(L, sqlite3_libversion());
100 return 1;
103 static int
104 sqlite_libversion_number(lua_State *L)
106 lua_pushinteger(L, sqlite3_libversion_number());
107 return 1;
110 static int
111 sqlite_sourceid(lua_State *L)
113 lua_pushstring(L, sqlite3_sourceid());
114 return 1;
117 static int
118 db_close(lua_State *L)
120 sqlite3 **db;
122 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
123 lua_pushinteger(L, sqlite3_close(*db));
124 return 1;
128 static int
129 db_prepare(lua_State *L)
131 sqlite3 **db;
132 sqlite3_stmt **stmt;
133 const char *sql;
135 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
136 stmt = lua_newuserdata(L, sizeof(sqlite3_stmt *));
137 sql = luaL_checkstring(L, 2);
138 lua_pushinteger(L, sqlite3_prepare_v2(*db, sql,
139 (int)strlen(sql) + 1, stmt, NULL));
140 luaL_getmetatable(L, SQLITE_STMT_METATABLE);
141 lua_setmetatable(L, -3);
142 return 2;
146 static int
147 db_exec(lua_State *L)
149 sqlite3 **db;
151 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
152 lua_pushinteger(L, sqlite3_exec(*db, lua_tostring(L, 2), NULL,
153 NULL, NULL));
154 return 1;
157 static int
158 db_errcode(lua_State *L)
160 sqlite3 **db;
162 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
163 lua_pushinteger(L, sqlite3_errcode(*db));
164 return 1;
167 static int
168 db_errmsg(lua_State *L)
170 sqlite3 **db;
172 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
173 lua_pushstring(L, sqlite3_errmsg(*db));
174 return 1;
177 static int
178 db_get_autocommit(lua_State *L)
180 sqlite3 **db;
182 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
183 lua_pushboolean(L, sqlite3_get_autocommit(*db));
184 return 1;
187 static int
188 db_changes(lua_State *L)
190 sqlite3 **db;
192 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
193 lua_pushinteger(L, sqlite3_changes(*db));
194 return 1;
197 static int
198 stmt_bind(lua_State *L)
200 sqlite3_stmt **stmt;
201 int pidx;
203 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
204 pidx = (int)luaL_checkinteger(L, 2);
206 switch (lua_type(L, 3)) {
207 case LUA_TNUMBER:
208 lua_pushinteger(L, sqlite3_bind_double(*stmt, pidx,
209 lua_tonumber(L, 3)));
210 break;
211 case LUA_TSTRING:
212 lua_pushinteger(L, sqlite3_bind_text(*stmt, pidx,
213 lua_tostring(L, 3), -1, SQLITE_TRANSIENT));
214 break;
215 case LUA_TNIL:
216 lua_pushinteger(L, sqlite3_bind_null(*stmt, pidx));
217 break;
218 default:
219 sqlite_error(L, "unsupported data type %s",
220 luaL_typename(L, 3));
222 return 1;
225 static int
226 stmt_bind_parameter_count(lua_State *L)
228 sqlite3_stmt **stmt;
230 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
231 lua_pushinteger(L, sqlite3_bind_parameter_count(*stmt));
232 return 1;
235 static int
236 stmt_bind_parameter_index(lua_State *L)
238 sqlite3_stmt **stmt;
240 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
241 lua_pushinteger(L, sqlite3_bind_parameter_index(*stmt,
242 lua_tostring(L, 2)));
243 return 1;
246 static int
247 stmt_bind_parameter_name(lua_State *L)
249 sqlite3_stmt **stmt;
250 int pidx;
252 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
253 pidx = (int)luaL_checkinteger(L, 2);
254 lua_pushstring(L, sqlite3_bind_parameter_name(*stmt, pidx));
255 return 1;
258 static int
259 stmt_step(lua_State *L)
261 sqlite3_stmt **stmt;
263 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
264 lua_pushinteger(L, sqlite3_step(*stmt));
265 return 1;
268 static int
269 stmt_column_name(lua_State *L)
271 sqlite3_stmt **stmt;
272 int cidx;
274 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
275 cidx = (int)luaL_checkinteger(L, 2) - 1;
277 lua_pushstring(L, sqlite3_column_name(*stmt, cidx));
278 return 1;
281 static int
282 stmt_column_count(lua_State *L)
284 sqlite3_stmt **stmt;
286 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
287 lua_pushinteger(L, sqlite3_column_count(*stmt));
288 return 1;
291 static int
292 stmt_column(lua_State *L)
294 sqlite3_stmt **stmt;
295 int cidx;
297 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
298 cidx = (int)luaL_checkinteger(L, 2) - 1;
300 switch (sqlite3_column_type(*stmt, cidx)) {
301 case SQLITE_INTEGER:
302 lua_pushinteger(L, sqlite3_column_int(*stmt, cidx));
303 break;
304 case SQLITE_FLOAT:
305 lua_pushnumber(L, sqlite3_column_double(*stmt, cidx));
306 break;
307 case SQLITE_TEXT:
308 lua_pushstring(L, (const char *)sqlite3_column_text(*stmt,
309 cidx));
310 break;
311 case SQLITE_BLOB:
312 case SQLITE_NULL:
313 lua_pushnil(L);
314 break;
316 return 1;
319 static int
320 stmt_reset(lua_State *L)
322 sqlite3_stmt **stmt;
324 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
325 sqlite3_reset(*stmt);
326 return 0;
329 static int
330 stmt_clear_bindings(lua_State *L)
332 sqlite3_stmt **stmt;
334 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
335 sqlite3_clear_bindings(*stmt);
336 return 0;
339 static int
340 stmt_finalize(lua_State *L)
342 sqlite3_stmt **stmt;
344 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
345 sqlite3_finalize(*stmt);
346 return 0;
349 struct constant {
350 const char *name;
351 int value;
354 static const struct constant sqlite_constant[] = {
355 /* SQLite return codes */
356 { "OK", SQLITE_OK },
357 { "ERROR", SQLITE_ERROR },
358 { "INTERNAL", SQLITE_INTERNAL },
359 { "PERM", SQLITE_PERM },
360 { "ABORT", SQLITE_ABORT },
361 { "BUSY", SQLITE_BUSY },
362 { "LOCKED", SQLITE_LOCKED },
363 { "NOMEM", SQLITE_NOMEM },
364 { "READONLY", SQLITE_READONLY },
365 { "INTERRUPT", SQLITE_INTERRUPT },
366 { "IOERR", SQLITE_IOERR },
367 { "CORRUPT", SQLITE_CORRUPT },
368 { "NOTFOUND", SQLITE_NOTFOUND },
369 { "FULL", SQLITE_FULL },
370 { "CANTOPEN", SQLITE_CANTOPEN },
371 { "PROTOCOL", SQLITE_PROTOCOL },
372 { "EMPTY", SQLITE_EMPTY },
373 { "SCHEMA", SQLITE_SCHEMA },
374 { "TOOBIG", SQLITE_TOOBIG },
375 { "CONSTRAINT", SQLITE_CONSTRAINT },
376 { "MISMATCH", SQLITE_MISMATCH },
377 { "MISUSE", SQLITE_MISUSE },
378 { "NOLFS", SQLITE_NOLFS },
379 { "AUTH", SQLITE_AUTH },
380 { "FORMAT", SQLITE_FORMAT },
381 { "RANGE", SQLITE_RANGE },
382 { "NOTADB", SQLITE_NOTADB },
383 { "ROW", SQLITE_ROW },
384 { "DONE", SQLITE_DONE },
386 /* File modes */
387 { "OPEN_READONLY", SQLITE_OPEN_READONLY },
388 { "OPEN_READWRITE", SQLITE_OPEN_READWRITE },
389 { "OPEN_CREATE", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
391 { NULL, 0 }
394 static void
395 gpio_set_info(lua_State *L)
397 lua_pushliteral(L, "_COPYRIGHT");
398 lua_pushliteral(L, "Copyright (C) 2011, 2012, 2013 by "
399 "Marc Balmer <marc@msys.ch>");
400 lua_settable(L, -3);
401 lua_pushliteral(L, "_DESCRIPTION");
402 lua_pushliteral(L, "SQLite interface for Lua");
403 lua_settable(L, -3);
404 lua_pushliteral(L, "_VERSION");
405 lua_pushliteral(L, "sqlite 1.0.3");
406 lua_settable(L, -3);
410 luaopen_sqlite(lua_State* L)
412 static const struct luaL_Reg sqlite_methods[] = {
413 { "initialize", sqlite_initialize },
414 { "shutdown", sqlite_shutdown },
415 { "open", sqlite_open },
416 { "libversion", sqlite_libversion },
417 { "libversion_number", sqlite_libversion_number },
418 { "sourceid", sqlite_sourceid },
419 { NULL, NULL }
421 static const struct luaL_Reg db_methods[] = {
422 { "close", db_close },
423 { "prepare", db_prepare },
424 { "exec", db_exec },
425 { "errcode", db_errcode },
426 { "errmsg", db_errmsg },
427 { "get_autocommit", db_get_autocommit },
428 { "changes", db_changes },
429 { NULL, NULL }
431 static const struct luaL_Reg stmt_methods[] = {
432 { "bind", stmt_bind },
433 { "bind_parameter_count", stmt_bind_parameter_count },
434 { "bind_parameter_index", stmt_bind_parameter_index },
435 { "bind_parameter_name", stmt_bind_parameter_name },
436 { "step", stmt_step },
437 { "column", stmt_column },
438 { "reset", stmt_reset },
439 { "clear_bindings", stmt_clear_bindings },
440 { "finalize", stmt_finalize },
441 { "column_name", stmt_column_name },
442 { "column_count", stmt_column_count },
443 { NULL, NULL }
445 int n;
447 sqlite3_initialize();
449 luaL_register(L, "sqlite", sqlite_methods);
450 luaL_register(L, NULL, db_methods);
451 luaL_register(L, NULL, stmt_methods);
452 gpio_set_info(L);
454 /* The database connection metatable */
455 if (luaL_newmetatable(L, SQLITE_DB_METATABLE)) {
456 luaL_register(L, NULL, db_methods);
458 lua_pushliteral(L, "__gc");
459 lua_pushcfunction(L, db_close);
460 lua_settable(L, -3);
462 lua_pushliteral(L, "__index");
463 lua_pushvalue(L, -2);
464 lua_settable(L, -3);
466 lua_pushliteral(L, "__metatable");
467 lua_pushliteral(L, "must not access this metatable");
468 lua_settable(L, -3);
470 lua_pop(L, 1);
472 /* The statement metatable */
473 if (luaL_newmetatable(L, SQLITE_STMT_METATABLE)) {
474 luaL_register(L, NULL, stmt_methods);
476 lua_pushliteral(L, "__gc");
477 lua_pushcfunction(L, stmt_finalize);
478 lua_settable(L, -3);
480 lua_pushliteral(L, "__index");
481 lua_pushvalue(L, -2);
482 lua_settable(L, -3);
484 lua_pushliteral(L, "__metatable");
485 lua_pushliteral(L, "must not access this metatable");
486 lua_settable(L, -3);
488 lua_pop(L, 1);
490 for (n = 0; sqlite_constant[n].name != NULL; n++) {
491 lua_pushinteger(L, sqlite_constant[n].value);
492 lua_setfield(L, -2, sqlite_constant[n].name);
494 return 1;