1 /* $NetBSD: sqlite.c,v 1.1 2011/10/15 10:26:30 mbalmer Exp $ */ 2 3 /* 4 * Copyright (c) 2011 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 <errno.h> 31 #include <fcntl.h> 32 #include <stdarg.h> 33 #include <stdio.h> 34 #include <string.h> 35 #include <ctype.h> 36 #include <stdlib.h> 37 #include <sqlite3.h> 38 #include <unistd.h> 39 40 #include <lua.h> 41 #include <lauxlib.h> 42 #include <lualib.h> 43 44 #define SQLITE_DB_METATABLE "SQLite database connection methods" 45 #define SQLITE_STMT_METATABLE "SQLite statement methods" 46 47 int luaopen_sqlite(lua_State*); 48 49 static void 50 sqlite_error(lua_State *L, const char *fmt, ...) 51 { 52 va_list ap; 53 int len; 54 char *msg; 55 56 va_start(ap, fmt); 57 len = vasprintf(&msg, fmt, ap); 58 va_end(ap); 59 60 if (len != -1) { 61 lua_pushstring(L, msg); 62 free(msg); 63 } else 64 lua_pushstring(L, "vasprintf failed"); 65 lua_error(L); 66 } 67 68 static int 69 sqlite_initialize(lua_State *L) 70 { 71 lua_pushinteger(L, sqlite3_initialize()); 72 return 1; 73 } 74 75 static int 76 sqlite_shutdown(lua_State *L) 77 { 78 lua_pushinteger(L, sqlite3_shutdown()); 79 return 1; 80 } 81 82 static int 83 sqlite_open(lua_State *L) 84 { 85 sqlite3 **db; 86 87 db = lua_newuserdata(L, sizeof(sqlite3 *)); 88 lua_pushinteger(L, sqlite3_open_v2(luaL_checkstring(L, -3), db, 89 (int)luaL_checkinteger(L, -2), NULL)); 90 91 luaL_getmetatable(L, SQLITE_DB_METATABLE); 92 lua_setmetatable(L, -3); 93 return 2; 94 95 } 96 97 static int 98 sqlite_libversion(lua_State *L) 99 { 100 lua_pushstring(L, sqlite3_libversion()); 101 return 1; 102 } 103 104 static int 105 sqlite_libversion_number(lua_State *L) 106 { 107 lua_pushinteger(L, sqlite3_libversion_number()); 108 return 1; 109 } 110 111 static int 112 sqlite_sourceid(lua_State *L) 113 { 114 lua_pushstring(L, sqlite3_sourceid()); 115 return 1; 116 } 117 118 static int 119 db_close(lua_State *L) 120 { 121 sqlite3 **db; 122 123 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE); 124 lua_pushinteger(L, sqlite3_close(*db)); 125 return 1; 126 127 } 128 129 static int 130 db_prepare(lua_State *L) 131 { 132 sqlite3 **db; 133 sqlite3_stmt **stmt; 134 const char *sql; 135 136 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE); 137 stmt = lua_newuserdata(L, sizeof(sqlite3_stmt *)); 138 sql = luaL_checkstring(L, 2); 139 lua_pushinteger(L, sqlite3_prepare_v2(*db, sql, 140 (int)strlen(sql) + 1, stmt, NULL)); 141 luaL_getmetatable(L, SQLITE_STMT_METATABLE); 142 lua_setmetatable(L, -3); 143 return 2; 144 145 } 146 147 static int 148 db_exec(lua_State *L) 149 { 150 sqlite3 **db; 151 152 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE); 153 lua_pushinteger(L, sqlite3_exec(*db, lua_tostring(L, 2), NULL, 154 NULL, NULL)); 155 return 1; 156 } 157 158 static int 159 db_errcode(lua_State *L) 160 { 161 sqlite3 **db; 162 163 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE); 164 lua_pushinteger(L, sqlite3_errcode(*db)); 165 return 1; 166 } 167 168 static int 169 db_errmsg(lua_State *L) 170 { 171 sqlite3 **db; 172 173 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE); 174 lua_pushstring(L, sqlite3_errmsg(*db)); 175 return 1; 176 } 177 178 static int 179 db_get_autocommit(lua_State *L) 180 { 181 sqlite3 **db; 182 183 db = luaL_checkudata(L, 1, SQLITE_DB_METATABLE); 184 lua_pushboolean(L, sqlite3_get_autocommit(*db)); 185 return 1; 186 } 187 188 static int 189 stmt_bind(lua_State *L) 190 { 191 sqlite3_stmt **stmt; 192 int pidx; 193 194 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE); 195 pidx = (int)luaL_checkinteger(L, 2); 196 197 switch (lua_type(L, 3)) { 198 case LUA_TNUMBER: 199 lua_pushinteger(L, sqlite3_bind_double(*stmt, pidx, 200 lua_tonumber(L, 3))); 201 break; 202 case LUA_TSTRING: 203 lua_pushinteger(L, sqlite3_bind_text(*stmt, pidx, 204 lua_tostring(L, 3), -1, NULL)); 205 break; 206 case LUA_TNIL: 207 lua_pushinteger(L, sqlite3_bind_null(*stmt, pidx)); 208 break; 209 default: 210 sqlite_error(L, "unsupported data type %s", 211 luaL_typename(L, 3)); 212 } 213 return 1; 214 } 215 216 static int 217 stmt_bind_parameter_count(lua_State *L) 218 { 219 sqlite3_stmt **stmt; 220 221 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE); 222 lua_pushinteger(L, sqlite3_bind_parameter_count(*stmt)); 223 return 1; 224 } 225 226 static int 227 stmt_bind_parameter_index(lua_State *L) 228 { 229 sqlite3_stmt **stmt; 230 231 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE); 232 lua_pushinteger(L, sqlite3_bind_parameter_index(*stmt, 233 lua_tostring(L, 2))); 234 return 1; 235 } 236 237 static int 238 stmt_bind_parameter_name(lua_State *L) 239 { 240 sqlite3_stmt **stmt; 241 int pidx; 242 243 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE); 244 pidx = (int)luaL_checkinteger(L, 2); 245 lua_pushstring(L, sqlite3_bind_parameter_name(*stmt, pidx)); 246 return 1; 247 } 248 249 static int 250 stmt_step(lua_State *L) 251 { 252 sqlite3_stmt **stmt; 253 254 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE); 255 lua_pushinteger(L, sqlite3_step(*stmt)); 256 return 1; 257 } 258 259 static int 260 stmt_column_name(lua_State *L) 261 { 262 sqlite3_stmt **stmt; 263 int cidx; 264 265 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE); 266 cidx = (int)luaL_checkinteger(L, 2) - 1; 267 268 lua_pushstring(L, sqlite3_column_name(*stmt, cidx)); 269 return 1; 270 } 271 272 static int 273 stmt_column_count(lua_State *L) 274 { 275 sqlite3_stmt **stmt; 276 277 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE); 278 lua_pushinteger(L, sqlite3_column_count(*stmt)); 279 return 1; 280 } 281 282 static int 283 stmt_column(lua_State *L) 284 { 285 sqlite3_stmt **stmt; 286 int cidx; 287 288 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE); 289 cidx = (int)luaL_checkinteger(L, 2) - 1; 290 291 switch (sqlite3_column_type(*stmt, cidx)) { 292 case SQLITE_INTEGER: 293 lua_pushinteger(L, sqlite3_column_int(*stmt, cidx)); 294 break; 295 case SQLITE_FLOAT: 296 lua_pushnumber(L, sqlite3_column_double(*stmt, cidx)); 297 break; 298 case SQLITE_TEXT: 299 lua_pushstring(L, (const char *)sqlite3_column_text(*stmt, 300 cidx)); 301 break; 302 case SQLITE_BLOB: 303 case SQLITE_NULL: 304 lua_pushnil(L); 305 break; 306 } 307 return 1; 308 } 309 310 static int 311 stmt_reset(lua_State *L) 312 { 313 sqlite3_stmt **stmt; 314 315 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE); 316 sqlite3_reset(*stmt); 317 return 0; 318 } 319 320 static int 321 stmt_clear_bindings(lua_State *L) 322 { 323 sqlite3_stmt **stmt; 324 325 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE); 326 sqlite3_clear_bindings(*stmt); 327 return 0; 328 } 329 330 static int 331 stmt_finalize(lua_State *L) 332 { 333 sqlite3_stmt **stmt; 334 335 stmt = luaL_checkudata(L, 1, SQLITE_STMT_METATABLE); 336 sqlite3_finalize(*stmt); 337 return 0; 338 } 339 340 struct constant { 341 const char *name; 342 int value; 343 }; 344 345 static const struct constant sqlite_constant[] = { 346 /* SQLite return codes */ 347 { "OK", SQLITE_OK }, 348 { "ERROR", SQLITE_ERROR }, 349 { "INTERNAL", SQLITE_INTERNAL }, 350 { "PERM", SQLITE_PERM }, 351 { "ABORT", SQLITE_ABORT }, 352 { "BUSY", SQLITE_BUSY }, 353 { "LOCKED", SQLITE_LOCKED }, 354 { "NOMEM", SQLITE_NOMEM }, 355 { "READONLY", SQLITE_READONLY }, 356 { "INTERRUPT", SQLITE_INTERRUPT }, 357 { "IOERR", SQLITE_IOERR }, 358 { "CORRUPT", SQLITE_CORRUPT }, 359 { "FULL", SQLITE_FULL }, 360 { "CANTOPEN", SQLITE_CANTOPEN }, 361 { "EMPTY", SQLITE_EMPTY }, 362 { "SCHEMA", SQLITE_SCHEMA }, 363 { "TOOBIG", SQLITE_TOOBIG }, 364 { "CONSTRAINT", SQLITE_CONSTRAINT }, 365 { "MISMATCH", SQLITE_MISMATCH }, 366 { "MISUSE", SQLITE_MISUSE }, 367 { "NOLFS", SQLITE_NOLFS }, 368 { "AUTH", SQLITE_AUTH }, 369 { "FORMAT", SQLITE_FORMAT }, 370 { "RANGE", SQLITE_RANGE }, 371 { "NOTADB", SQLITE_NOTADB }, 372 373 { "ROW", SQLITE_ROW }, 374 { "DONE", SQLITE_DONE }, 375 376 /* File modes */ 377 { "OPEN_READONLY", SQLITE_OPEN_READONLY }, 378 { "OPEN_READWRITE", SQLITE_OPEN_READWRITE }, 379 { "OPEN_CREATE", SQLITE_OPEN_CREATE }, 380 381 { NULL, 0 } 382 }; 383 384 static void 385 gpio_set_info(lua_State *L) 386 { 387 lua_pushliteral(L, "_COPYRIGHT"); 388 lua_pushliteral(L, "Copyright (C) 2011 Marc Balmer <marc@msys.ch>"); 389 lua_settable(L, -3); 390 lua_pushliteral(L, "_DESCRIPTION"); 391 lua_pushliteral(L, "SQLite interface for Lua"); 392 lua_settable(L, -3); 393 lua_pushliteral(L, "_VERSION"); 394 lua_pushliteral(L, "sqlite 1.0.0"); 395 lua_settable(L, -3); 396 } 397 398 int 399 luaopen_sqlite(lua_State* L) 400 { 401 static const struct luaL_Reg sqlite_methods[] = { 402 { "initialize", sqlite_initialize }, 403 { "shutdown", sqlite_shutdown }, 404 { "open", sqlite_open }, 405 { "libversion", sqlite_libversion }, 406 { "libversion_number", sqlite_libversion_number }, 407 { "sourceid", sqlite_sourceid }, 408 { NULL, NULL } 409 }; 410 static const struct luaL_Reg db_methods[] = { 411 { "close", db_close }, 412 { "prepare", db_prepare }, 413 { "exec", db_exec }, 414 { "errcode", db_errcode }, 415 { "errmsg", db_errmsg }, 416 { "get_autocommit", db_get_autocommit }, 417 { NULL, NULL } 418 }; 419 static const struct luaL_Reg stmt_methods[] = { 420 { "bind", stmt_bind }, 421 { "bind_parameter_count", stmt_bind_parameter_count }, 422 { "bind_parameter_index", stmt_bind_parameter_index }, 423 { "bind_parameter_name", stmt_bind_parameter_name }, 424 { "step", stmt_step }, 425 { "column", stmt_column }, 426 { "reset", stmt_reset }, 427 { "clear_bindings", stmt_clear_bindings }, 428 { "finalize", stmt_finalize }, 429 { "column_name", stmt_column_name }, 430 { "column_count", stmt_column_count }, 431 { NULL, NULL } 432 }; 433 int n; 434 435 sqlite3_initialize(); 436 437 luaL_register(L, "sqlite", sqlite_methods); 438 gpio_set_info(L); 439 440 /* The database connection metatable */ 441 if (luaL_newmetatable(L, SQLITE_DB_METATABLE)) { 442 luaL_register(L, NULL, db_methods); 443 444 lua_pushliteral(L, "__gc"); 445 lua_pushcfunction(L, db_close); 446 lua_settable(L, -3); 447 448 lua_pushliteral(L, "__index"); 449 lua_pushvalue(L, -2); 450 lua_settable(L, -3); 451 452 lua_pushliteral(L, "__metatable"); 453 lua_pushliteral(L, "must not access this metatable"); 454 lua_settable(L, -3); 455 } 456 lua_pop(L, 1); 457 458 /* The statement metatable */ 459 if (luaL_newmetatable(L, SQLITE_STMT_METATABLE)) { 460 luaL_register(L, NULL, stmt_methods); 461 462 lua_pushliteral(L, "__gc"); 463 lua_pushcfunction(L, stmt_finalize); 464 lua_settable(L, -3); 465 466 lua_pushliteral(L, "__index"); 467 lua_pushvalue(L, -2); 468 lua_settable(L, -3); 469 470 lua_pushliteral(L, "__metatable"); 471 lua_pushliteral(L, "must not access this metatable"); 472 lua_settable(L, -3); 473 } 474 lua_pop(L, 1); 475 476 for (n = 0; sqlite_constant[n].name != NULL; n++) { 477 lua_pushinteger(L, sqlite_constant[n].value); 478 lua_setfield(L, -2, sqlite_constant[n].name); 479 }; 480 return 1; 481 } 482