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