1 /* $NetBSD: sqlite.c,v 1.6 2013/10/27 12:38:08 mbalmer Exp $ */
4 * Copyright (c) 2011, 2013 Marc Balmer <marc@msys.ch>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 */
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
, ...)
53 len
= vasprintf(&msg
, fmt
, ap
);
57 lua_pushstring(L
, msg
);
60 lua_pushstring(L
, "vasprintf failed");
65 sqlite_initialize(lua_State
*L
)
67 lua_pushinteger(L
, sqlite3_initialize());
72 sqlite_shutdown(lua_State
*L
)
74 lua_pushinteger(L
, sqlite3_shutdown());
79 sqlite_open(lua_State
*L
)
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
));
91 lua_pushinteger(L
, sqlite3_open(luaL_checkstring(L
, -2), db
));
97 sqlite_libversion(lua_State
*L
)
99 lua_pushstring(L
, sqlite3_libversion());
104 sqlite_libversion_number(lua_State
*L
)
106 lua_pushinteger(L
, sqlite3_libversion_number());
111 sqlite_sourceid(lua_State
*L
)
113 lua_pushstring(L
, sqlite3_sourceid());
118 db_close(lua_State
*L
)
122 db
= luaL_checkudata(L
, 1, SQLITE_DB_METATABLE
);
123 lua_pushinteger(L
, sqlite3_close(*db
));
129 db_prepare(lua_State
*L
)
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);
147 db_exec(lua_State
*L
)
151 db
= luaL_checkudata(L
, 1, SQLITE_DB_METATABLE
);
152 lua_pushinteger(L
, sqlite3_exec(*db
, lua_tostring(L
, 2), NULL
,
158 db_errcode(lua_State
*L
)
162 db
= luaL_checkudata(L
, 1, SQLITE_DB_METATABLE
);
163 lua_pushinteger(L
, sqlite3_errcode(*db
));
168 db_errmsg(lua_State
*L
)
172 db
= luaL_checkudata(L
, 1, SQLITE_DB_METATABLE
);
173 lua_pushstring(L
, sqlite3_errmsg(*db
));
178 db_get_autocommit(lua_State
*L
)
182 db
= luaL_checkudata(L
, 1, SQLITE_DB_METATABLE
);
183 lua_pushboolean(L
, sqlite3_get_autocommit(*db
));
188 db_changes(lua_State
*L
)
192 db
= luaL_checkudata(L
, 1, SQLITE_DB_METATABLE
);
193 lua_pushinteger(L
, sqlite3_changes(*db
));
198 stmt_bind(lua_State
*L
)
203 stmt
= luaL_checkudata(L
, 1, SQLITE_STMT_METATABLE
);
204 pidx
= (int)luaL_checkinteger(L
, 2);
206 switch (lua_type(L
, 3)) {
208 lua_pushinteger(L
, sqlite3_bind_double(*stmt
, pidx
,
209 lua_tonumber(L
, 3)));
212 lua_pushinteger(L
, sqlite3_bind_text(*stmt
, pidx
,
213 lua_tostring(L
, 3), -1, SQLITE_TRANSIENT
));
216 lua_pushinteger(L
, sqlite3_bind_null(*stmt
, pidx
));
219 sqlite_error(L
, "unsupported data type %s",
220 luaL_typename(L
, 3));
226 stmt_bind_parameter_count(lua_State
*L
)
230 stmt
= luaL_checkudata(L
, 1, SQLITE_STMT_METATABLE
);
231 lua_pushinteger(L
, sqlite3_bind_parameter_count(*stmt
));
236 stmt_bind_parameter_index(lua_State
*L
)
240 stmt
= luaL_checkudata(L
, 1, SQLITE_STMT_METATABLE
);
241 lua_pushinteger(L
, sqlite3_bind_parameter_index(*stmt
,
242 lua_tostring(L
, 2)));
247 stmt_bind_parameter_name(lua_State
*L
)
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
));
259 stmt_step(lua_State
*L
)
263 stmt
= luaL_checkudata(L
, 1, SQLITE_STMT_METATABLE
);
264 lua_pushinteger(L
, sqlite3_step(*stmt
));
269 stmt_column_name(lua_State
*L
)
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
));
282 stmt_column_count(lua_State
*L
)
286 stmt
= luaL_checkudata(L
, 1, SQLITE_STMT_METATABLE
);
287 lua_pushinteger(L
, sqlite3_column_count(*stmt
));
292 stmt_column(lua_State
*L
)
297 stmt
= luaL_checkudata(L
, 1, SQLITE_STMT_METATABLE
);
298 cidx
= (int)luaL_checkinteger(L
, 2) - 1;
300 switch (sqlite3_column_type(*stmt
, cidx
)) {
302 lua_pushinteger(L
, sqlite3_column_int(*stmt
, cidx
));
305 lua_pushnumber(L
, sqlite3_column_double(*stmt
, cidx
));
308 lua_pushstring(L
, (const char *)sqlite3_column_text(*stmt
,
320 stmt_reset(lua_State
*L
)
324 stmt
= luaL_checkudata(L
, 1, SQLITE_STMT_METATABLE
);
325 sqlite3_reset(*stmt
);
330 stmt_clear_bindings(lua_State
*L
)
334 stmt
= luaL_checkudata(L
, 1, SQLITE_STMT_METATABLE
);
335 sqlite3_clear_bindings(*stmt
);
340 stmt_finalize(lua_State
*L
)
344 stmt
= luaL_checkudata(L
, 1, SQLITE_STMT_METATABLE
);
345 sqlite3_finalize(*stmt
);
354 static const struct constant sqlite_constant
[] = {
355 /* SQLite return codes */
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
},
387 { "OPEN_READONLY", SQLITE_OPEN_READONLY
},
388 { "OPEN_READWRITE", SQLITE_OPEN_READWRITE
},
389 { "OPEN_CREATE", SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
},
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>");
401 lua_pushliteral(L
, "_DESCRIPTION");
402 lua_pushliteral(L
, "SQLite interface for Lua");
404 lua_pushliteral(L
, "_VERSION");
405 lua_pushliteral(L
, "sqlite 1.0.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
},
421 static const struct luaL_Reg db_methods
[] = {
422 { "close", db_close
},
423 { "prepare", db_prepare
},
425 { "errcode", db_errcode
},
426 { "errmsg", db_errmsg
},
427 { "get_autocommit", db_get_autocommit
},
428 { "changes", db_changes
},
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
},
447 sqlite3_initialize();
449 luaL_register(L
, "sqlite", sqlite_methods
);
450 luaL_register(L
, NULL
, db_methods
);
451 luaL_register(L
, NULL
, stmt_methods
);
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
);
462 lua_pushliteral(L
, "__index");
463 lua_pushvalue(L
, -2);
466 lua_pushliteral(L
, "__metatable");
467 lua_pushliteral(L
, "must not access this metatable");
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
);
480 lua_pushliteral(L
, "__index");
481 lua_pushvalue(L
, -2);
484 lua_pushliteral(L
, "__metatable");
485 lua_pushliteral(L
, "must not access this metatable");
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
);