1 /* $NetBSD: sqlite.c,v 1.7 2014/07/19 18:38:34 lneto Exp $ */
2
3 /*
4 * Copyright (c) 2011, 2013 Marc Balmer <marc@msys.ch>
5 * All rights reserved.
6 *
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.
15 *
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.
26 */
27
28 /* SQLite interface for Lua */
29
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <sqlite3.h>
35
36 #include <lua.h>
37 #include <lauxlib.h>
38 #include <lualib.h>
39
40 #define SQLITE_DB_METATABLE "SQLite database connection methods"
41 #define SQLITE_STMT_METATABLE "SQLite statement methods"
42
43 int luaopen_sqlite(lua_State*);
44
45 static __printflike(2, 3) void
sqlite_error(lua_State * L,const char * fmt,...)46 sqlite_error(lua_State *L, const char *fmt, ...)
47 {
48 va_list ap;
49 int len;
50 char *msg;
51
52 va_start(ap, fmt);
53 len = vasprintf(&msg, fmt, ap);
54 va_end(ap);
55
56 if (len != -1) {
57 lua_pushstring(L, msg);
58 free(msg);
59 } else
60 lua_pushstring(L, "vasprintf failed");
61 lua_error(L);
62 }
63
64 static int
sqlite_initialize(lua_State * L)65 sqlite_initialize(lua_State *L)
66 {
67 lua_pushinteger(L, sqlite3_initialize());
68 return 1;
69 }
70
71 static int
sqlite_shutdown(lua_State * L)72 sqlite_shutdown(lua_State *L)
73 {
74 lua_pushinteger(L, sqlite3_shutdown());
75 return 1;
76 }
77
78 static int
sqlite_open(lua_State * L)79 sqlite_open(lua_State *L)
80 {
81 sqlite3 **db;
82
83 db = lua_newuserdata(L, sizeof(sqlite3 *));
84 luaL_getmetatable(L, SQLITE_DB_METATABLE);
85 lua_setmetatable(L, -2);
86
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;
93
94 }
95
96 static int
sqlite_libversion(lua_State * L)97 sqlite_libversion(lua_State *L)
98 {
99 lua_pushstring(L, sqlite3_libversion());
100 return 1;
101 }
102
103 static int
sqlite_libversion_number(lua_State * L)104 sqlite_libversion_number(lua_State *L)
105 {
106 lua_pushinteger(L, sqlite3_libversion_number());
107 return 1;
108 }
109
110 static int
sqlite_sourceid(lua_State * L)111 sqlite_sourceid(lua_State *L)
112 {
113 lua_pushstring(L, sqlite3_sourceid());
114 return 1;
115 }
116
117 static int
db_close(lua_State * L)118 db_close(lua_State *L)
119 {
120 sqlite3 **db;
121
122 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
123 lua_pushinteger(L, sqlite3_close(*db));
124 return 1;
125
126 }
127
128 static int
db_prepare(lua_State * L)129 db_prepare(lua_State *L)
130 {
131 sqlite3 **db;
132 sqlite3_stmt **stmt;
133 const char *sql;
134
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;
143
144 }
145
146 static int
db_exec(lua_State * L)147 db_exec(lua_State *L)
148 {
149 sqlite3 **db;
150
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;
155 }
156
157 static int
db_errcode(lua_State * L)158 db_errcode(lua_State *L)
159 {
160 sqlite3 **db;
161
162 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
163 lua_pushinteger(L, sqlite3_errcode(*db));
164 return 1;
165 }
166
167 static int
db_errmsg(lua_State * L)168 db_errmsg(lua_State *L)
169 {
170 sqlite3 **db;
171
172 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
173 lua_pushstring(L, sqlite3_errmsg(*db));
174 return 1;
175 }
176
177 static int
db_get_autocommit(lua_State * L)178 db_get_autocommit(lua_State *L)
179 {
180 sqlite3 **db;
181
182 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
183 lua_pushboolean(L, sqlite3_get_autocommit(*db));
184 return 1;
185 }
186
187 static int
db_changes(lua_State * L)188 db_changes(lua_State *L)
189 {
190 sqlite3 **db;
191
192 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE);
193 lua_pushinteger(L, sqlite3_changes(*db));
194 return 1;
195 }
196
197 static int
stmt_bind(lua_State * L)198 stmt_bind(lua_State *L)
199 {
200 sqlite3_stmt **stmt;
201 int pidx;
202
203 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
204 pidx = (int)luaL_checkinteger(L, 2);
205
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));
221 }
222 return 1;
223 }
224
225 static int
stmt_bind_parameter_count(lua_State * L)226 stmt_bind_parameter_count(lua_State *L)
227 {
228 sqlite3_stmt **stmt;
229
230 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
231 lua_pushinteger(L, sqlite3_bind_parameter_count(*stmt));
232 return 1;
233 }
234
235 static int
stmt_bind_parameter_index(lua_State * L)236 stmt_bind_parameter_index(lua_State *L)
237 {
238 sqlite3_stmt **stmt;
239
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;
244 }
245
246 static int
stmt_bind_parameter_name(lua_State * L)247 stmt_bind_parameter_name(lua_State *L)
248 {
249 sqlite3_stmt **stmt;
250 int pidx;
251
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;
256 }
257
258 static int
stmt_step(lua_State * L)259 stmt_step(lua_State *L)
260 {
261 sqlite3_stmt **stmt;
262
263 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
264 lua_pushinteger(L, sqlite3_step(*stmt));
265 return 1;
266 }
267
268 static int
stmt_column_name(lua_State * L)269 stmt_column_name(lua_State *L)
270 {
271 sqlite3_stmt **stmt;
272 int cidx;
273
274 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
275 cidx = (int)luaL_checkinteger(L, 2) - 1;
276
277 lua_pushstring(L, sqlite3_column_name(*stmt, cidx));
278 return 1;
279 }
280
281 static int
stmt_column_count(lua_State * L)282 stmt_column_count(lua_State *L)
283 {
284 sqlite3_stmt **stmt;
285
286 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
287 lua_pushinteger(L, sqlite3_column_count(*stmt));
288 return 1;
289 }
290
291 static int
stmt_column(lua_State * L)292 stmt_column(lua_State *L)
293 {
294 sqlite3_stmt **stmt;
295 int cidx;
296
297 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
298 cidx = (int)luaL_checkinteger(L, 2) - 1;
299
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;
315 }
316 return 1;
317 }
318
319 static int
stmt_reset(lua_State * L)320 stmt_reset(lua_State *L)
321 {
322 sqlite3_stmt **stmt;
323
324 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
325 sqlite3_reset(*stmt);
326 return 0;
327 }
328
329 static int
stmt_clear_bindings(lua_State * L)330 stmt_clear_bindings(lua_State *L)
331 {
332 sqlite3_stmt **stmt;
333
334 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
335 sqlite3_clear_bindings(*stmt);
336 return 0;
337 }
338
339 static int
stmt_finalize(lua_State * L)340 stmt_finalize(lua_State *L)
341 {
342 sqlite3_stmt **stmt;
343
344 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE);
345 sqlite3_finalize(*stmt);
346 return 0;
347 }
348
349 struct constant {
350 const char *name;
351 int value;
352 };
353
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 },
385
386 /* File modes */
387 { "OPEN_READONLY", SQLITE_OPEN_READONLY },
388 { "OPEN_READWRITE", SQLITE_OPEN_READWRITE },
389 { "OPEN_CREATE", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
390
391 { NULL, 0 }
392 };
393
394 static void
gpio_set_info(lua_State * L)395 gpio_set_info(lua_State *L)
396 {
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);
407 }
408
409 int
luaopen_sqlite(lua_State * L)410 luaopen_sqlite(lua_State* L)
411 {
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 }
420 };
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 }
430 };
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 }
444 };
445 int n;
446
447 sqlite3_initialize();
448
449 luaL_newlib(L, sqlite_methods);
450 luaL_setfuncs(L, db_methods, 0);
451 luaL_setfuncs(L, stmt_methods, 0);
452 gpio_set_info(L);
453
454 /* The database connection metatable */
455 if (luaL_newmetatable(L, SQLITE_DB_METATABLE)) {
456 luaL_setfuncs(L, db_methods, 0);
457
458 lua_pushliteral(L, "__gc");
459 lua_pushcfunction(L, db_close);
460 lua_settable(L, -3);
461
462 lua_pushliteral(L, "__index");
463 lua_pushvalue(L, -2);
464 lua_settable(L, -3);
465
466 lua_pushliteral(L, "__metatable");
467 lua_pushliteral(L, "must not access this metatable");
468 lua_settable(L, -3);
469 }
470 lua_pop(L, 1);
471
472 /* The statement metatable */
473 if (luaL_newmetatable(L, SQLITE_STMT_METATABLE)) {
474 luaL_setfuncs(L, stmt_methods, 0);
475
476 lua_pushliteral(L, "__gc");
477 lua_pushcfunction(L, stmt_finalize);
478 lua_settable(L, -3);
479
480 lua_pushliteral(L, "__index");
481 lua_pushvalue(L, -2);
482 lua_settable(L, -3);
483
484 lua_pushliteral(L, "__metatable");
485 lua_pushliteral(L, "must not access this metatable");
486 lua_settable(L, -3);
487 }
488 lua_pop(L, 1);
489
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);
493 };
494 return 1;
495 }
496