lsqlite3.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  1. /************************************************************************
  2. * lsqlite3 *
  3. * Copyright (C) 2002-2007 Tiago Dionizio, Doug Currie *
  4. * All rights reserved. *
  5. * Author : Tiago Dionizio <tiago.dionizio@ist.utl.pt> *
  6. * Author : Doug Currie <doug.currie@alum.mit.edu> *
  7. * Library : lsqlite3 - a SQLite 3 database binding for Lua 5 *
  8. * *
  9. * Permission is hereby granted, free of charge, to any person obtaining *
  10. * a copy of this software and associated documentation files (the *
  11. * "Software"), to deal in the Software without restriction, including *
  12. * without limitation the rights to use, copy, modify, merge, publish, *
  13. * distribute, sublicense, and/or sell copies of the Software, and to *
  14. * permit persons to whom the Software is furnished to do so, subject to *
  15. * the following conditions: *
  16. * *
  17. * The above copyright notice and this permission notice shall be *
  18. * included in all copies or substantial portions of the Software. *
  19. * *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
  21. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  22. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
  23. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY *
  24. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, *
  25. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE *
  26. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  27. ************************************************************************/
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <assert.h>
  31. #define LUA_LIB
  32. #include "lua.h"
  33. #include "lauxlib.h"
  34. #include "sqlite3.h"
  35. /* compile time features */
  36. #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK)
  37. #define SQLITE_OMIT_PROGRESS_CALLBACK 0
  38. #endif
  39. typedef struct sdb sdb;
  40. typedef struct sdb_vm sdb_vm;
  41. typedef struct sdb_func sdb_func;
  42. /* to use as C user data so i know what function sqlite is calling */
  43. struct sdb_func {
  44. /* references to associated lua values */
  45. int fn_step;
  46. int fn_finalize;
  47. int udata;
  48. sdb *db;
  49. char aggregate;
  50. sdb_func *next;
  51. };
  52. /* information about database */
  53. struct sdb {
  54. /* associated lua state */
  55. lua_State *L;
  56. /* sqlite database handle */
  57. sqlite3 *db;
  58. /* sql functions stack usage */
  59. sdb_func *func; /* top SQL function being called */
  60. /* references */
  61. int busy_cb; /* busy callback */
  62. int busy_udata;
  63. int progress_cb; /* progress handler */
  64. int progress_udata;
  65. int trace_cb; /* trace callback */
  66. int trace_udata;
  67. };
  68. static const char *sqlite_meta = ":sqlite3";
  69. static const char *sqlite_vm_meta = ":sqlite3:vm";
  70. static const char *sqlite_ctx_meta = ":sqlite3:ctx";
  71. static int sqlite_ctx_meta_ref;
  72. /*
  73. ** =======================================================
  74. ** Database Virtual Machine Operations
  75. ** =======================================================
  76. */
  77. static void vm_push_column(lua_State *L, sqlite3_stmt *vm, int idx) {
  78. switch (sqlite3_column_type(vm, idx)) {
  79. case SQLITE_INTEGER:
  80. {
  81. sqlite_int64 i64 = sqlite3_column_int64(vm, idx);
  82. lua_Number n = (lua_Number)i64;
  83. if (n == i64)
  84. lua_pushnumber(L, n);
  85. else
  86. lua_pushlstring(L, (const char*)sqlite3_column_text(vm, idx), sqlite3_column_bytes(vm, idx));
  87. }
  88. break;
  89. case SQLITE_FLOAT:
  90. lua_pushnumber(L, sqlite3_column_double(vm, idx));
  91. break;
  92. case SQLITE_TEXT:
  93. lua_pushlstring(L, (const char*)sqlite3_column_text(vm, idx), sqlite3_column_bytes(vm, idx));
  94. break;
  95. case SQLITE_BLOB:
  96. lua_pushlstring(L, sqlite3_column_blob(vm, idx), sqlite3_column_bytes(vm, idx));
  97. break;
  98. case SQLITE_NULL:
  99. lua_pushnil(L);
  100. break;
  101. default:
  102. lua_pushnil(L);
  103. break;
  104. }
  105. }
  106. /* virtual machine information */
  107. struct sdb_vm {
  108. sdb *db; /* associated database handle */
  109. sqlite3_stmt *vm; /* virtual machine */
  110. /* sqlite3_step info */
  111. int columns; /* number of columns in result */
  112. char has_values; /* true when step succeeds */
  113. char temp; /* temporary vm used in db:rows */
  114. };
  115. /* called with sql text on the lua stack */
  116. static sdb_vm *newvm(lua_State *L, sdb *db) {
  117. sdb_vm *svm = (sdb_vm*)lua_newuserdata(L, sizeof(sdb_vm));
  118. luaL_getmetatable(L, sqlite_vm_meta);
  119. lua_setmetatable(L, -2); /* set metatable */
  120. svm->db = db;
  121. svm->columns = 0;
  122. svm->has_values = 0;
  123. svm->vm = NULL;
  124. svm->temp = 0;
  125. /* add an entry on the database table: svm -> sql text */
  126. lua_pushlightuserdata(L, db);
  127. lua_rawget(L, LUA_REGISTRYINDEX);
  128. lua_pushlightuserdata(L, svm);
  129. lua_pushvalue(L, -4); /* the sql text */
  130. lua_rawset(L, -3);
  131. lua_pop(L, 1);
  132. return svm;
  133. }
  134. static int cleanupvm(lua_State *L, sdb_vm *svm) {
  135. /* remove entry in database table - no harm if not present in the table */
  136. lua_pushlightuserdata(L, svm->db);
  137. lua_rawget(L, LUA_REGISTRYINDEX);
  138. lua_pushlightuserdata(L, svm);
  139. lua_pushnil(L);
  140. lua_rawset(L, -3);
  141. lua_pop(L, 1);
  142. svm->columns = 0;
  143. svm->has_values = 0;
  144. if (!svm->vm) return 0;
  145. lua_pushnumber(L, sqlite3_finalize(svm->vm));
  146. svm->vm = NULL;
  147. return 1;
  148. }
  149. static int stepvm(lua_State *L, sdb_vm *svm) {
  150. int result;
  151. int loop_limit = 3;
  152. while ( loop_limit-- ) {
  153. result = sqlite3_step(svm->vm);
  154. if ( result==SQLITE_ERROR ) {
  155. result = sqlite3_reset (svm->vm);
  156. }
  157. if ( result==SQLITE_SCHEMA ) {
  158. sqlite3_stmt *vn;
  159. const char *sql;
  160. /* recover sql text */
  161. lua_pushlightuserdata(L, svm->db);
  162. lua_rawget(L, LUA_REGISTRYINDEX);
  163. lua_pushlightuserdata(L, svm);
  164. lua_rawget(L, -2); /* sql text */
  165. sql = luaL_checkstring(L, -1);
  166. /* re-prepare */
  167. result = sqlite3_prepare(svm->db->db, sql, -1, &vn, NULL);
  168. if (result != SQLITE_OK) break;
  169. sqlite3_transfer_bindings(svm->vm, vn);
  170. sqlite3_finalize(svm->vm);
  171. svm->vm = vn;
  172. lua_pop(L,2);
  173. } else {
  174. break;
  175. }
  176. }
  177. return result;
  178. }
  179. static sdb_vm *lsqlite_getvm(lua_State *L, int index) {
  180. sdb_vm *svm = (sdb_vm*)luaL_checkudata(L, index, sqlite_vm_meta);
  181. if (svm == NULL) luaL_argerror(L, index, "bad sqlite virtual machine");
  182. return svm;
  183. }
  184. static sdb_vm *lsqlite_checkvm(lua_State *L, int index) {
  185. sdb_vm *svm = lsqlite_getvm(L, index);
  186. if (svm->vm == NULL) luaL_argerror(L, index, "attempt to use closed sqlite virtual machine");
  187. return svm;
  188. }
  189. static int dbvm_isopen(lua_State *L) {
  190. sdb_vm *svm = lsqlite_getvm(L, 1);
  191. lua_pushboolean(L, svm->vm != NULL ? 1 : 0);
  192. return 1;
  193. }
  194. static int dbvm_tostring(lua_State *L) {
  195. char buff[39];
  196. sdb_vm *svm = lsqlite_getvm(L, 1);
  197. if (svm->vm == NULL)
  198. strcpy(buff, "closed");
  199. else
  200. sprintf(buff, "%p", svm);
  201. lua_pushfstring(L, "sqlite virtual machine (%s)", buff);
  202. return 1;
  203. }
  204. static int dbvm_gc(lua_State *L) {
  205. sdb_vm *svm = lsqlite_getvm(L, 1);
  206. if (svm->vm != NULL) /* ignore closed vms */
  207. cleanupvm(L, svm);
  208. return 0;
  209. }
  210. static int dbvm_step(lua_State *L) {
  211. int result;
  212. sdb_vm *svm = lsqlite_checkvm(L, 1);
  213. result = stepvm(L, svm);
  214. svm->has_values = result == SQLITE_ROW ? 1 : 0;
  215. svm->columns = sqlite3_data_count(svm->vm);
  216. lua_pushnumber(L, result);
  217. return 1;
  218. }
  219. static int dbvm_finalize(lua_State *L) {
  220. sdb_vm *svm = lsqlite_checkvm(L, 1);
  221. return cleanupvm(L, svm);
  222. }
  223. static int dbvm_reset(lua_State *L) {
  224. sdb_vm *svm = lsqlite_checkvm(L, 1);
  225. sqlite3_reset(svm->vm);
  226. lua_pushnumber(L, sqlite3_errcode(svm->db->db));
  227. return 1;
  228. }
  229. static void dbvm_check_contents(lua_State *L, sdb_vm *svm) {
  230. if (!svm->has_values) {
  231. luaL_error(L, "misuse of function");
  232. }
  233. }
  234. static void dbvm_check_index(lua_State *L, sdb_vm *svm, int index) {
  235. if (index < 0 || index >= svm->columns) {
  236. luaL_error(L, "index out of range [0..%d]", svm->columns - 1);
  237. }
  238. }
  239. static void dbvm_check_bind_index(lua_State *L, sdb_vm *svm, int index) {
  240. if (index < 1 || index > sqlite3_bind_parameter_count(svm->vm)) {
  241. luaL_error(L, "bind index out of range [1..%d]", sqlite3_bind_parameter_count(svm->vm));
  242. }
  243. }
  244. /*
  245. ** =======================================================
  246. ** Virtual Machine - generic info
  247. ** =======================================================
  248. */
  249. static int dbvm_columns(lua_State *L) {
  250. sdb_vm *svm = lsqlite_checkvm(L, 1);
  251. lua_pushnumber(L, sqlite3_column_count(svm->vm));
  252. return 1;
  253. }
  254. /*
  255. ** =======================================================
  256. ** Virtual Machine - getters
  257. ** =======================================================
  258. */
  259. static int dbvm_get_value(lua_State *L) {
  260. sdb_vm *svm = lsqlite_checkvm(L, 1);
  261. int index = luaL_checkint(L, 2);
  262. dbvm_check_contents(L, svm);
  263. dbvm_check_index(L, svm, index);
  264. vm_push_column(L, svm->vm, index);
  265. return 1;
  266. }
  267. static int dbvm_get_name(lua_State *L) {
  268. sdb_vm *svm = lsqlite_checkvm(L, 1);
  269. int index = luaL_checknumber(L, 2);
  270. dbvm_check_index(L, svm, index);
  271. lua_pushstring(L, sqlite3_column_name(svm->vm, index));
  272. return 1;
  273. }
  274. static int dbvm_get_type(lua_State *L) {
  275. sdb_vm *svm = lsqlite_checkvm(L, 1);
  276. int index = luaL_checknumber(L, 2);
  277. dbvm_check_index(L, svm, index);
  278. lua_pushstring(L, sqlite3_column_decltype(svm->vm, index));
  279. return 1;
  280. }
  281. static int dbvm_get_values(lua_State *L) {
  282. sdb_vm *svm = lsqlite_checkvm(L, 1);
  283. sqlite3_stmt *vm = svm->vm;
  284. int columns = svm->columns;
  285. int n;
  286. dbvm_check_contents(L, svm);
  287. lua_newtable(L);
  288. for (n = 0; n < columns;) {
  289. vm_push_column(L, vm, n++);
  290. lua_rawseti(L, -2, n);
  291. }
  292. return 1;
  293. }
  294. static int dbvm_get_names(lua_State *L) {
  295. sdb_vm *svm = lsqlite_checkvm(L, 1);
  296. sqlite3_stmt *vm = svm->vm;
  297. int columns = sqlite3_column_count(vm); /* valid as soon as statement prepared */
  298. int n;
  299. lua_newtable(L);
  300. for (n = 0; n < columns;) {
  301. lua_pushstring(L, sqlite3_column_name(vm, n++));
  302. lua_rawseti(L, -2, n);
  303. }
  304. return 1;
  305. }
  306. static int dbvm_get_types(lua_State *L) {
  307. sdb_vm *svm = lsqlite_checkvm(L, 1);
  308. sqlite3_stmt *vm = svm->vm;
  309. int columns = sqlite3_column_count(vm); /* valid as soon as statement prepared */
  310. int n;
  311. lua_newtable(L);
  312. for (n = 0; n < columns;) {
  313. lua_pushstring(L, sqlite3_column_decltype(vm, n++));
  314. lua_rawseti(L, -2, n);
  315. }
  316. return 1;
  317. }
  318. static int dbvm_get_uvalues(lua_State *L) {
  319. sdb_vm *svm = lsqlite_checkvm(L, 1);
  320. sqlite3_stmt *vm = svm->vm;
  321. int columns = svm->columns;
  322. int n;
  323. dbvm_check_contents(L, svm);
  324. lua_checkstack(L, columns);
  325. for (n = 0; n < columns; ++n)
  326. vm_push_column(L, vm, n);
  327. return columns;
  328. }
  329. static int dbvm_get_unames(lua_State *L) {
  330. sdb_vm *svm = lsqlite_checkvm(L, 1);
  331. sqlite3_stmt *vm = svm->vm;
  332. int columns = sqlite3_column_count(vm); /* valid as soon as statement prepared */
  333. int n;
  334. lua_checkstack(L, columns);
  335. for (n = 0; n < columns; ++n)
  336. lua_pushstring(L, sqlite3_column_name(vm, n));
  337. return columns;
  338. }
  339. static int dbvm_get_utypes(lua_State *L) {
  340. sdb_vm *svm = lsqlite_checkvm(L, 1);
  341. sqlite3_stmt *vm = svm->vm;
  342. int columns = sqlite3_column_count(vm); /* valid as soon as statement prepared */
  343. int n;
  344. lua_checkstack(L, columns);
  345. for (n = 0; n < columns; ++n)
  346. lua_pushstring(L, sqlite3_column_decltype(vm, n));
  347. return columns;
  348. }
  349. static int dbvm_get_named_values(lua_State *L) {
  350. sdb_vm *svm = lsqlite_checkvm(L, 1);
  351. sqlite3_stmt *vm = svm->vm;
  352. int columns = svm->columns;
  353. int n;
  354. dbvm_check_contents(L, svm);
  355. lua_newtable(L);
  356. for (n = 0; n < columns; ++n) {
  357. lua_pushstring(L, sqlite3_column_name(vm, n));
  358. vm_push_column(L, vm, n);
  359. lua_rawset(L, -3);
  360. }
  361. return 1;
  362. }
  363. static int dbvm_get_named_types(lua_State *L) {
  364. sdb_vm *svm = lsqlite_checkvm(L, 1);
  365. sqlite3_stmt *vm = svm->vm;
  366. int columns = sqlite3_column_count(vm);
  367. int n;
  368. lua_newtable(L);
  369. for (n = 0; n < columns; ++n) {
  370. lua_pushstring(L, sqlite3_column_name(vm, n));
  371. lua_pushstring(L, sqlite3_column_decltype(vm, n));
  372. lua_rawset(L, -3);
  373. }
  374. return 1;
  375. }
  376. /*
  377. ** =======================================================
  378. ** Virtual Machine - Bind
  379. ** =======================================================
  380. */
  381. static int dbvm_bind_index(lua_State *L, sqlite3_stmt *vm, int index, int lindex) {
  382. switch (lua_type(L, lindex)) {
  383. case LUA_TSTRING:
  384. return sqlite3_bind_text(vm, index, lua_tostring(L, lindex), lua_strlen(L, lindex), SQLITE_TRANSIENT);
  385. case LUA_TNUMBER:
  386. return sqlite3_bind_double(vm, index, lua_tonumber(L, lindex));
  387. case LUA_TBOOLEAN:
  388. return sqlite3_bind_int(vm, index, lua_toboolean(L, lindex) ? 1 : 0);
  389. case LUA_TNONE:
  390. case LUA_TNIL:
  391. return sqlite3_bind_null(vm, index);
  392. default:
  393. luaL_error(L, "index (%d) - invalid data type for bind (%s)", index, lua_typename(L, lua_type(L, lindex)));
  394. return SQLITE_MISUSE; /*!*/
  395. }
  396. }
  397. static int dbvm_bind_parameter_count(lua_State *L) {
  398. sdb_vm *svm = lsqlite_checkvm(L, 1);
  399. lua_pushnumber(L, sqlite3_bind_parameter_count(svm->vm));
  400. return 1;
  401. }
  402. static int dbvm_bind_parameter_name(lua_State *L) {
  403. sdb_vm *svm = lsqlite_checkvm(L, 1);
  404. int index = luaL_checknumber(L, 2);
  405. dbvm_check_bind_index(L, svm, index);
  406. lua_pushstring(L, sqlite3_bind_parameter_name(svm->vm, index));
  407. return 1;
  408. }
  409. static int dbvm_bind(lua_State *L) {
  410. sdb_vm *svm = lsqlite_checkvm(L, 1);
  411. sqlite3_stmt *vm = svm->vm;
  412. int index = luaL_checkint(L, 2);
  413. int result;
  414. dbvm_check_bind_index(L, svm, index);
  415. result = dbvm_bind_index(L, vm, index, 3);
  416. lua_pushnumber(L, result);
  417. return 1;
  418. }
  419. static int dbvm_bind_blob(lua_State *L) {
  420. sdb_vm *svm = lsqlite_checkvm(L, 1);
  421. int index = luaL_checkint(L, 2);
  422. const char *value = luaL_checkstring(L, 3);
  423. int len = lua_strlen(L, 3);
  424. lua_pushnumber(L, sqlite3_bind_blob(svm->vm, index, value, len, SQLITE_TRANSIENT));
  425. return 1;
  426. }
  427. static int dbvm_bind_values(lua_State *L) {
  428. sdb_vm *svm = lsqlite_checkvm(L, 1);
  429. sqlite3_stmt *vm = svm->vm;
  430. int top = lua_gettop(L);
  431. int result, n;
  432. if (top - 1 != sqlite3_bind_parameter_count(vm))
  433. luaL_error(L,
  434. "incorrect number of parameters to bind (%d given, %d to bind)",
  435. top - 1,
  436. sqlite3_bind_parameter_count(vm)
  437. );
  438. for (n = 2; n <= top; ++n) {
  439. if ((result = dbvm_bind_index(L, vm, n - 1, n)) != SQLITE_OK) {
  440. lua_pushnumber(L, result);
  441. return 1;
  442. }
  443. }
  444. lua_pushnumber(L, SQLITE_OK);
  445. return 1;
  446. }
  447. static int dbvm_bind_names(lua_State *L) {
  448. sdb_vm *svm = lsqlite_checkvm(L, 1);
  449. sqlite3_stmt *vm = svm->vm;
  450. int count = sqlite3_bind_parameter_count(vm);
  451. const char *name;
  452. int result, n;
  453. luaL_checktype(L, 2, LUA_TTABLE);
  454. for (n = 1; n <= count; ++n) {
  455. name = sqlite3_bind_parameter_name(vm, n);
  456. if (name && (name[0] == ':' || name[0] == '$')) {
  457. lua_pushstring(L, ++name);
  458. lua_gettable(L, 2);
  459. result = dbvm_bind_index(L, vm, n, -1);
  460. lua_pop(L, 1);
  461. }
  462. else {
  463. lua_pushnumber(L, n);
  464. lua_gettable(L, 2);
  465. result = dbvm_bind_index(L, vm, n, -1);
  466. lua_pop(L, 1);
  467. }
  468. if (result != SQLITE_OK) {
  469. lua_pushnumber(L, result);
  470. return 1;
  471. }
  472. }
  473. lua_pushnumber(L, SQLITE_OK);
  474. return 1;
  475. }
  476. /*
  477. ** =======================================================
  478. ** Database (internal management)
  479. ** =======================================================
  480. */
  481. /*
  482. ** When creating database handles, always creates a `closed' database handle
  483. ** before opening the actual database; so, if there is a memory error, the
  484. ** database is not left opened.
  485. **
  486. ** Creates a new 'table' and leaves it in the stack
  487. */
  488. static sdb *newdb (lua_State *L) {
  489. sdb *db = (sdb*)lua_newuserdata(L, sizeof(sdb));
  490. db->L = L;
  491. db->db = NULL; /* database handle is currently `closed' */
  492. db->func = NULL;
  493. db->busy_cb =
  494. db->busy_udata =
  495. db->progress_cb =
  496. db->progress_udata =
  497. db->trace_cb =
  498. db->trace_udata = LUA_NOREF;
  499. luaL_getmetatable(L, sqlite_meta);
  500. lua_setmetatable(L, -2); /* set metatable */
  501. /* to keep track of 'open' virtual machines */
  502. lua_pushlightuserdata(L, db);
  503. lua_newtable(L);
  504. lua_rawset(L, LUA_REGISTRYINDEX);
  505. return db;
  506. }
  507. static int cleanupdb(lua_State *L, sdb *db) {
  508. sdb_func *func;
  509. sdb_func *func_next;
  510. int top;
  511. int result;
  512. /* free associated virtual machines */
  513. lua_pushlightuserdata(L, db);
  514. lua_rawget(L, LUA_REGISTRYINDEX);
  515. /* close all used handles */
  516. top = lua_gettop(L);
  517. lua_pushnil(L);
  518. while (lua_next(L, -2)) {
  519. sdb_vm *svm = lua_touserdata(L, -2); /* key: vm; val: sql text */
  520. cleanupvm(L, svm);
  521. lua_settop(L, top);
  522. lua_pushnil(L);
  523. }
  524. lua_pop(L, 1); /* pop vm table */
  525. /* remove entry in lua registry table */
  526. lua_pushlightuserdata(L, db);
  527. lua_pushnil(L);
  528. lua_rawset(L, LUA_REGISTRYINDEX);
  529. /* 'free' all references */
  530. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_cb);
  531. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_udata);
  532. luaL_unref(L, LUA_REGISTRYINDEX, db->progress_cb);
  533. luaL_unref(L, LUA_REGISTRYINDEX, db->progress_udata);
  534. luaL_unref(L, LUA_REGISTRYINDEX, db->trace_cb);
  535. luaL_unref(L, LUA_REGISTRYINDEX, db->trace_udata);
  536. /* close database */
  537. result = sqlite3_close(db->db);
  538. db->db = NULL;
  539. /* free associated memory with created functions */
  540. func = db->func;
  541. while (func) {
  542. func_next = func->next;
  543. luaL_unref(L, LUA_REGISTRYINDEX, func->fn_step);
  544. luaL_unref(L, LUA_REGISTRYINDEX, func->fn_finalize);
  545. luaL_unref(L, LUA_REGISTRYINDEX, func->udata);
  546. free(func);
  547. func = func_next;
  548. }
  549. db->func = NULL;
  550. return result;
  551. }
  552. static sdb *lsqlite_getdb(lua_State *L, int index) {
  553. sdb *db = (sdb*)luaL_checkudata(L, index, sqlite_meta);
  554. // TODO lsm if (db == NULL) luaL_typerror(L, index, "sqlite database");
  555. return db;
  556. }
  557. static sdb *lsqlite_checkdb(lua_State *L, int index) {
  558. sdb *db = lsqlite_getdb(L, index);
  559. if (db->db == NULL) luaL_argerror(L, index, "attempt to use closed sqlite database");
  560. return db;
  561. }
  562. /*
  563. ** =======================================================
  564. ** User Defined Functions - Context Methods
  565. ** =======================================================
  566. */
  567. typedef struct {
  568. sqlite3_context *ctx;
  569. int ud;
  570. } lcontext;
  571. static lcontext *lsqlite_make_context(lua_State *L) {
  572. lcontext *ctx = (lcontext*)lua_newuserdata(L, sizeof(lcontext));
  573. lua_rawgeti(L, LUA_REGISTRYINDEX, sqlite_ctx_meta_ref);
  574. lua_setmetatable(L, -2);
  575. ctx->ctx = NULL;
  576. ctx->ud = LUA_NOREF;
  577. return ctx;
  578. }
  579. static lcontext *lsqlite_getcontext(lua_State *L, int index) {
  580. lcontext *ctx = (lcontext*)luaL_checkudata(L, index, sqlite_ctx_meta);
  581. // TODO lsm if (ctx == NULL) luaL_typerror(L, index, "sqlite context");
  582. return ctx;
  583. }
  584. static lcontext *lsqlite_checkcontext(lua_State *L, int index) {
  585. lcontext *ctx = lsqlite_getcontext(L, index);
  586. if (ctx->ctx == NULL) luaL_argerror(L, index, "invalid sqlite context");
  587. return ctx;
  588. }
  589. static int lcontext_tostring(lua_State *L) {
  590. char buff[39];
  591. lcontext *ctx = lsqlite_getcontext(L, 1);
  592. if (ctx->ctx == NULL)
  593. strcpy(buff, "closed");
  594. else
  595. sprintf(buff, "%p", ctx->ctx);
  596. lua_pushfstring(L, "sqlite function context (%s)", buff);
  597. return 1;
  598. }
  599. static void lcontext_check_aggregate(lua_State *L, lcontext *ctx) {
  600. sdb_func *func = (sdb_func*)sqlite3_user_data(ctx->ctx);
  601. if (!func->aggregate) {
  602. luaL_error(L, "attempt to call aggregate method from scalar function");
  603. }
  604. }
  605. static int lcontext_user_data(lua_State *L) {
  606. lcontext *ctx = lsqlite_checkcontext(L, 1);
  607. sdb_func *func = (sdb_func*)sqlite3_user_data(ctx->ctx);
  608. lua_rawgeti(L, LUA_REGISTRYINDEX, func->udata);
  609. return 1;
  610. }
  611. static int lcontext_get_aggregate_context(lua_State *L) {
  612. lcontext *ctx = lsqlite_checkcontext(L, 1);
  613. lcontext_check_aggregate(L, ctx);
  614. lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->ud);
  615. return 1;
  616. }
  617. static int lcontext_set_aggregate_context(lua_State *L) {
  618. lcontext *ctx = lsqlite_checkcontext(L, 1);
  619. lcontext_check_aggregate(L, ctx);
  620. lua_settop(L, 2);
  621. luaL_unref(L, LUA_REGISTRYINDEX, ctx->ud);
  622. ctx->ud = luaL_ref(L, LUA_REGISTRYINDEX);
  623. return 0;
  624. }
  625. static int lcontext_aggregate_count(lua_State *L) {
  626. lcontext *ctx = lsqlite_checkcontext(L, 1);
  627. lcontext_check_aggregate(L, ctx);
  628. lua_pushnumber(L, sqlite3_aggregate_count(ctx->ctx));
  629. return 1;
  630. }
  631. #if 0
  632. void *sqlite3_get_auxdata(sqlite3_context*, int);
  633. void sqlite3_set_auxdata(sqlite3_context*, int, void*, void (*)(void*));
  634. #endif
  635. static int lcontext_result(lua_State *L) {
  636. lcontext *ctx = lsqlite_checkcontext(L, 1);
  637. switch (lua_type(L, 2)) {
  638. case LUA_TNUMBER:
  639. sqlite3_result_double(ctx->ctx, luaL_checknumber(L, 2));
  640. break;
  641. case LUA_TSTRING:
  642. sqlite3_result_text(ctx->ctx, luaL_checkstring(L, 2), lua_strlen(L, 2), SQLITE_TRANSIENT);
  643. break;
  644. case LUA_TNIL:
  645. case LUA_TNONE:
  646. sqlite3_result_null(ctx->ctx);
  647. break;
  648. default:
  649. luaL_error(L, "invalid result type %s", lua_typename(L, 2));
  650. break;
  651. }
  652. return 0;
  653. }
  654. static int lcontext_result_blob(lua_State *L) {
  655. lcontext *ctx = lsqlite_checkcontext(L, 1);
  656. const char *blob = luaL_checkstring(L, 2);
  657. int size = lua_strlen(L, 2);
  658. sqlite3_result_blob(ctx->ctx, (const void*)blob, size, SQLITE_TRANSIENT);
  659. return 0;
  660. }
  661. static int lcontext_result_double(lua_State *L) {
  662. lcontext *ctx = lsqlite_checkcontext(L, 1);
  663. double d = luaL_checknumber(L, 2);
  664. sqlite3_result_double(ctx->ctx, d);
  665. return 0;
  666. }
  667. static int lcontext_result_error(lua_State *L) {
  668. lcontext *ctx = lsqlite_checkcontext(L, 1);
  669. const char *err = luaL_checkstring(L, 2);
  670. int size = lua_strlen(L, 2);
  671. sqlite3_result_error(ctx->ctx, err, size);
  672. return 0;
  673. }
  674. static int lcontext_result_int(lua_State *L) {
  675. lcontext *ctx = lsqlite_checkcontext(L, 1);
  676. int i = luaL_checkint(L, 2);
  677. sqlite3_result_int(ctx->ctx, i);
  678. return 0;
  679. }
  680. static int lcontext_result_null(lua_State *L) {
  681. lcontext *ctx = lsqlite_checkcontext(L, 1);
  682. sqlite3_result_null(ctx->ctx);
  683. return 0;
  684. }
  685. static int lcontext_result_text(lua_State *L) {
  686. lcontext *ctx = lsqlite_checkcontext(L, 1);
  687. const char *text = luaL_checkstring(L, 2);
  688. int size = lua_strlen(L, 2);
  689. sqlite3_result_text(ctx->ctx, text, size, SQLITE_TRANSIENT);
  690. return 0;
  691. }
  692. /*
  693. ** =======================================================
  694. ** Database Methods
  695. ** =======================================================
  696. */
  697. static int db_isopen(lua_State *L) {
  698. sdb *db = lsqlite_getdb(L, 1);
  699. lua_pushboolean(L, db->db != NULL ? 1 : 0);
  700. return 1;
  701. }
  702. static int db_last_insert_rowid(lua_State *L) {
  703. sdb *db = lsqlite_checkdb(L, 1);
  704. /* conversion warning: int64 -> luaNumber */
  705. sqlite_int64 rowid = sqlite3_last_insert_rowid(db->db);
  706. lua_Number n = (lua_Number)rowid;
  707. if (n == rowid)
  708. lua_pushnumber(L, n);
  709. else
  710. lua_pushfstring(L, "%ll", rowid);
  711. return 1;
  712. }
  713. static int db_changes(lua_State *L) {
  714. sdb *db = lsqlite_checkdb(L, 1);
  715. lua_pushnumber(L, sqlite3_changes(db->db));
  716. return 1;
  717. }
  718. static int db_total_changes(lua_State *L) {
  719. sdb *db = lsqlite_checkdb(L, 1);
  720. lua_pushnumber(L, sqlite3_total_changes(db->db));
  721. return 1;
  722. }
  723. static int db_errcode(lua_State *L) {
  724. sdb *db = lsqlite_checkdb(L, 1);
  725. lua_pushnumber(L, sqlite3_errcode(db->db));
  726. return 1;
  727. }
  728. static int db_errmsg(lua_State *L) {
  729. sdb *db = lsqlite_checkdb(L, 1);
  730. lua_pushstring(L, sqlite3_errmsg(db->db));
  731. return 1;
  732. }
  733. static int db_interrupt(lua_State *L) {
  734. sdb *db = lsqlite_checkdb(L, 1);
  735. sqlite3_interrupt(db->db);
  736. return 0;
  737. }
  738. /*
  739. ** Registering SQL functions:
  740. */
  741. static void db_push_value(lua_State *L, sqlite3_value *value) {
  742. switch (sqlite3_value_type(value)) {
  743. case SQLITE_TEXT:
  744. lua_pushlstring(L, (const char*)sqlite3_value_text(value), sqlite3_value_bytes(value));
  745. break;
  746. case SQLITE_INTEGER:
  747. {
  748. sqlite_int64 i64 = sqlite3_value_int64(value);
  749. lua_Number n = (lua_Number)i64;
  750. if (n == i64)
  751. lua_pushnumber(L, n);
  752. else
  753. lua_pushlstring(L, (const char*)sqlite3_value_text(value), sqlite3_value_bytes(value));
  754. }
  755. break;
  756. case SQLITE_FLOAT:
  757. lua_pushnumber(L, sqlite3_value_double(value));
  758. break;
  759. case SQLITE_BLOB:
  760. lua_pushlstring(L, sqlite3_value_blob(value), sqlite3_value_bytes(value));
  761. break;
  762. case SQLITE_NULL:
  763. lua_pushnil(L);
  764. break;
  765. default:
  766. /* things done properly (SQLite + Lua SQLite)
  767. ** this should never happen */
  768. lua_pushnil(L);
  769. break;
  770. }
  771. }
  772. /*
  773. ** callback functions used when calling registered sql functions
  774. */
  775. /* scalar function to be called
  776. ** callback params: context, values... */
  777. static void db_sql_normal_function(sqlite3_context *context, int argc, sqlite3_value **argv) {
  778. sdb_func *func = (sdb_func*)sqlite3_user_data(context);
  779. lua_State *L = func->db->L;
  780. int n;
  781. lcontext *ctx;
  782. int top = lua_gettop(L);
  783. /* ensure there is enough space in the stack */
  784. lua_checkstack(L, argc + 3);
  785. lua_rawgeti(L, LUA_REGISTRYINDEX, func->fn_step); /* function to call */
  786. if (!func->aggregate) {
  787. ctx = lsqlite_make_context(L); /* push context - used to set results */
  788. }
  789. else {
  790. /* reuse context userdata value */
  791. void *p = sqlite3_aggregate_context(context, 1);
  792. /* i think it is OK to use assume that using a light user data
  793. ** as an entry on LUA REGISTRY table will be unique */
  794. lua_pushlightuserdata(L, p);
  795. lua_rawget(L, LUA_REGISTRYINDEX); /* context table */
  796. if (lua_isnil(L, -1)) { /* not yet created? */
  797. lua_pop(L, 1);
  798. ctx = lsqlite_make_context(L);
  799. lua_pushlightuserdata(L, p);
  800. lua_pushvalue(L, -2);
  801. lua_rawset(L, LUA_REGISTRYINDEX);
  802. }
  803. else
  804. ctx = lsqlite_getcontext(L, -1);
  805. }
  806. /* push params */
  807. for (n = 0; n < argc; ++n) {
  808. db_push_value(L, argv[n]);
  809. }
  810. /* set context */
  811. ctx->ctx = context;
  812. if (lua_pcall(L, argc + 1, 0, 0)) {
  813. const char *errmsg = lua_tostring(L, -1);
  814. int size = lua_strlen(L, -1);
  815. sqlite3_result_error(context, errmsg, size);
  816. }
  817. /* invalidate context */
  818. ctx->ctx = NULL;
  819. if (!func->aggregate) {
  820. luaL_unref(L, LUA_REGISTRYINDEX, ctx->ud);
  821. }
  822. lua_settop(L, top);
  823. }
  824. static void db_sql_finalize_function(sqlite3_context *context) {
  825. sdb_func *func = (sdb_func*)sqlite3_user_data(context);
  826. lua_State *L = func->db->L;
  827. void *p = sqlite3_aggregate_context(context, 1); /* minimal mem usage */
  828. lcontext *ctx;
  829. int top = lua_gettop(L);
  830. lua_rawgeti(L, LUA_REGISTRYINDEX, func->fn_finalize); /* function to call */
  831. /* i think it is OK to use assume that using a light user data
  832. ** as an entry on LUA REGISTRY table will be unique */
  833. lua_pushlightuserdata(L, p);
  834. lua_rawget(L, LUA_REGISTRYINDEX); /* context table */
  835. if (lua_isnil(L, -1)) { /* not yet created? - shouldn't happen in finalize function */
  836. lua_pop(L, 1);
  837. ctx = lsqlite_make_context(L);
  838. lua_pushlightuserdata(L, p);
  839. lua_pushvalue(L, -2);
  840. lua_rawset(L, LUA_REGISTRYINDEX);
  841. }
  842. else
  843. ctx = lsqlite_getcontext(L, -1);
  844. /* set context */
  845. ctx->ctx = context;
  846. if (lua_pcall(L, 1, 0, 0)) {
  847. sqlite3_result_error(context, lua_tostring(L, -1), -1);
  848. }
  849. /* invalidate context */
  850. ctx->ctx = NULL;
  851. /* cleanup context */
  852. luaL_unref(L, LUA_REGISTRYINDEX, ctx->ud);
  853. /* remove it from registry */
  854. lua_pushlightuserdata(L, p);
  855. lua_pushnil(L);
  856. lua_rawset(L, LUA_REGISTRYINDEX);
  857. lua_settop(L, top);
  858. }
  859. /*
  860. ** Register a normal function
  861. ** Params: db, function name, number arguments, [ callback | step, finalize], user data
  862. ** Returns: true on sucess
  863. **
  864. ** Normal function:
  865. ** Params: context, params
  866. **
  867. ** Aggregate function:
  868. ** Params of step: context, params
  869. ** Params of finalize: context
  870. */
  871. static int db_register_function(lua_State *L, int aggregate) {
  872. sdb *db = lsqlite_checkdb(L, 1);
  873. const char *name;
  874. int args;
  875. int result;
  876. sdb_func *func;
  877. /* safety measure */
  878. if (aggregate) aggregate = 1;
  879. name = luaL_checkstring(L, 2);
  880. args = luaL_checkint(L, 3);
  881. luaL_checktype(L, 4, LUA_TFUNCTION);
  882. if (aggregate) luaL_checktype(L, 5, LUA_TFUNCTION);
  883. /* maybe an alternative way to allocate memory should be used/avoided */
  884. func = (sdb_func*)malloc(sizeof(sdb_func));
  885. if (func == NULL) {
  886. luaL_error(L, "out of memory");
  887. }
  888. result = sqlite3_create_function(
  889. db->db, name, args, SQLITE_UTF8, func,
  890. aggregate ? NULL : db_sql_normal_function,
  891. aggregate ? db_sql_normal_function : NULL,
  892. aggregate ? db_sql_finalize_function : NULL
  893. );
  894. if (result == SQLITE_OK) {
  895. /* safety measures for userdata field to be present in the stack */
  896. lua_settop(L, 5 + aggregate);
  897. /* save registered function in db function list */
  898. func->db = db;
  899. func->aggregate = aggregate;
  900. func->next = db->func;
  901. db->func = func;
  902. /* save the setp/normal function callback */
  903. lua_pushvalue(L, 4);
  904. func->fn_step = luaL_ref(L, LUA_REGISTRYINDEX);
  905. /* save user data */
  906. lua_pushvalue(L, 5+aggregate);
  907. func->udata = luaL_ref(L, LUA_REGISTRYINDEX);
  908. if (aggregate) {
  909. lua_pushvalue(L, 5);
  910. func->fn_finalize = luaL_ref(L, LUA_REGISTRYINDEX);
  911. }
  912. else
  913. func->fn_finalize = LUA_NOREF;
  914. }
  915. else {
  916. /* free allocated memory */
  917. free(func);
  918. }
  919. lua_pushboolean(L, result == SQLITE_OK ? 1 : 0);
  920. return 1;
  921. }
  922. static int db_create_function(lua_State *L) {
  923. return db_register_function(L, 0);
  924. }
  925. static int db_create_aggregate(lua_State *L) {
  926. return db_register_function(L, 1);
  927. }
  928. /* create_collation; contributed by Thomas Lauer
  929. */
  930. typedef struct {
  931. lua_State *L;
  932. int ref;
  933. } scc;
  934. static int collwrapper(scc *co,int l1,const void *p1,
  935. int l2,const void *p2) {
  936. int res=0;
  937. lua_State *L=co->L;
  938. lua_rawgeti(L,LUA_REGISTRYINDEX,co->ref);
  939. lua_pushlstring(L,p1,l1);
  940. lua_pushlstring(L,p2,l2);
  941. if (lua_pcall(L,2,1,0)==0) res=(int)lua_tonumber(L,-1);
  942. lua_pop(L,1);
  943. return res;
  944. }
  945. static void collfree(scc *co) {
  946. if (co) {
  947. luaL_unref(co->L,LUA_REGISTRYINDEX,co->ref);
  948. free(co);
  949. }
  950. }
  951. static int db_create_collation(lua_State *L) {
  952. sdb *db=lsqlite_checkdb(L,1);
  953. const char *collname=luaL_checkstring(L,2);
  954. scc *co=NULL;
  955. int (*collfunc)(scc *,int,const void *,int,const void *)=NULL;
  956. lua_settop(L,3); /* default args to nil, and exclude extras */
  957. if (lua_isfunction(L,3)) collfunc=collwrapper;
  958. else if (!lua_isnil(L,3))
  959. luaL_error(L,"create_collation: function or nil expected");
  960. if (collfunc != NULL) {
  961. co=(scc *)malloc(sizeof(scc)); /* userdata is a no-no as it
  962. will be garbage-collected */
  963. if (co) {
  964. co->L=L;
  965. /* lua_settop(L,3) above means we don't need: lua_pushvalue(L,3); */
  966. co->ref=luaL_ref(L,LUA_REGISTRYINDEX);
  967. }
  968. else luaL_error(L,"create_collation: could not allocate callback");
  969. }
  970. sqlite3_create_collation_v2(db->db, collname, SQLITE_UTF8,
  971. (void *)co,
  972. (int(*)(void*,int,const void*,int,const void*))collfunc,
  973. (void(*)(void*))collfree);
  974. return 0;
  975. }
  976. /*
  977. ** trace callback:
  978. ** Params: database, callback function, userdata
  979. **
  980. ** callback function:
  981. ** Params: userdata, sql
  982. */
  983. static void db_trace_callback(void *user, const char *sql) {
  984. sdb *db = (sdb*)user;
  985. lua_State *L = db->L;
  986. int top = lua_gettop(L);
  987. /* setup lua callback call */
  988. lua_rawgeti(L, LUA_REGISTRYINDEX, db->trace_cb); /* get callback */
  989. lua_rawgeti(L, LUA_REGISTRYINDEX, db->trace_udata); /* get callback user data */
  990. lua_pushstring(L, sql); /* traced sql statement */
  991. /* call lua function */
  992. lua_pcall(L, 2, 0, 0);
  993. /* ignore any error generated by this function */
  994. lua_settop(L, top);
  995. }
  996. static int db_trace(lua_State *L) {
  997. sdb *db = lsqlite_checkdb(L, 1);
  998. if (lua_gettop(L) < 2 || lua_isnil(L, 2)) {
  999. luaL_unref(L, LUA_REGISTRYINDEX, db->trace_cb);
  1000. luaL_unref(L, LUA_REGISTRYINDEX, db->trace_udata);
  1001. db->trace_cb =
  1002. db->trace_udata = LUA_NOREF;
  1003. /* clear busy handler */
  1004. sqlite3_trace(db->db, NULL, NULL);
  1005. }
  1006. else {
  1007. luaL_checktype(L, 2, LUA_TFUNCTION);
  1008. /* make sure we have an userdata field (even if nil) */
  1009. lua_settop(L, 3);
  1010. luaL_unref(L, LUA_REGISTRYINDEX, db->trace_cb);
  1011. luaL_unref(L, LUA_REGISTRYINDEX, db->trace_udata);
  1012. db->trace_udata = luaL_ref(L, LUA_REGISTRYINDEX);
  1013. db->trace_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  1014. /* set busy handler */
  1015. sqlite3_trace(db->db, db_trace_callback, db);
  1016. }
  1017. return 0;
  1018. }
  1019. #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK
  1020. /*
  1021. ** progress handler:
  1022. ** Params: database, number of opcodes, callback function, userdata
  1023. **
  1024. ** callback function:
  1025. ** Params: userdata
  1026. ** returns: 0 to return immediatly and return SQLITE_ABORT, non-zero to continue
  1027. */
  1028. static int db_progress_callback(void *user) {
  1029. int result = 1; /* abort by default */
  1030. sdb *db = (sdb*)user;
  1031. lua_State *L = db->L;
  1032. int top = lua_gettop(L);
  1033. lua_rawgeti(L, LUA_REGISTRYINDEX, db->progress_cb);
  1034. lua_rawgeti(L, LUA_REGISTRYINDEX, db->progress_udata);
  1035. /* call lua function */
  1036. if (!lua_pcall(L, 1, 1, 0))
  1037. result = lua_toboolean(L, -1);
  1038. lua_settop(L, top);
  1039. return result;
  1040. }
  1041. static int db_progress_handler(lua_State *L) {
  1042. sdb *db = lsqlite_checkdb(L, 1);
  1043. if (lua_gettop(L) < 2 || lua_isnil(L, 2)) {
  1044. luaL_unref(L, LUA_REGISTRYINDEX, db->progress_cb);
  1045. luaL_unref(L, LUA_REGISTRYINDEX, db->progress_udata);
  1046. db->progress_cb =
  1047. db->progress_udata = LUA_NOREF;
  1048. /* clear busy handler */
  1049. sqlite3_progress_handler(db->db, 0, NULL, NULL);
  1050. }
  1051. else {
  1052. int nop = luaL_checkint(L, 2); /* number of opcodes */
  1053. luaL_checktype(L, 3, LUA_TFUNCTION);
  1054. /* make sure we have an userdata field (even if nil) */
  1055. lua_settop(L, 4);
  1056. luaL_unref(L, LUA_REGISTRYINDEX, db->progress_cb);
  1057. luaL_unref(L, LUA_REGISTRYINDEX, db->progress_udata);
  1058. db->progress_udata = luaL_ref(L, LUA_REGISTRYINDEX);
  1059. db->progress_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  1060. /* set progress callback */
  1061. sqlite3_progress_handler(db->db, nop, db_progress_callback, db);
  1062. }
  1063. return 0;
  1064. }
  1065. #else /* #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK */
  1066. static int db_progress_handler(lua_State *L) {
  1067. lua_pushliteral(L, "progress callback support disabled at compile time");
  1068. lua_error(L);
  1069. return 0;
  1070. }
  1071. #endif /* #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK */
  1072. /*
  1073. ** busy handler:
  1074. ** Params: database, callback function, userdata
  1075. **
  1076. ** callback function:
  1077. ** Params: userdata, number of tries
  1078. ** returns: 0 to return immediatly and return SQLITE_BUSY, non-zero to try again
  1079. */
  1080. static int db_busy_callback(void *user, int tries) {
  1081. int retry = 0; /* abort by default */
  1082. sdb *db = (sdb*)user;
  1083. lua_State *L = db->L;
  1084. int top = lua_gettop(L);
  1085. lua_rawgeti(L, LUA_REGISTRYINDEX, db->busy_cb);
  1086. lua_rawgeti(L, LUA_REGISTRYINDEX, db->busy_udata);
  1087. lua_pushnumber(L, tries);
  1088. /* call lua function */
  1089. if (!lua_pcall(L, 2, 1, 0))
  1090. retry = lua_toboolean(L, -1);
  1091. lua_settop(L, top);
  1092. return retry;
  1093. }
  1094. static int db_busy_handler(lua_State *L) {
  1095. sdb *db = lsqlite_checkdb(L, 1);
  1096. if (lua_gettop(L) < 2 || lua_isnil(L, 2)) {
  1097. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_cb);
  1098. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_udata);
  1099. db->busy_cb =
  1100. db->busy_udata = LUA_NOREF;
  1101. /* clear busy handler */
  1102. sqlite3_busy_handler(db->db, NULL, NULL);
  1103. }
  1104. else {
  1105. luaL_checktype(L, 2, LUA_TFUNCTION);
  1106. /* make sure we have an userdata field (even if nil) */
  1107. lua_settop(L, 3);
  1108. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_cb);
  1109. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_udata);
  1110. db->busy_udata = luaL_ref(L, LUA_REGISTRYINDEX);
  1111. db->busy_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  1112. /* set busy handler */
  1113. sqlite3_busy_handler(db->db, db_busy_callback, db);
  1114. }
  1115. return 0;
  1116. }
  1117. static int db_busy_timeout(lua_State *L) {
  1118. sdb *db = lsqlite_checkdb(L, 1);
  1119. int timeout = luaL_checkint(L, 2);
  1120. sqlite3_busy_timeout(db->db, timeout);
  1121. /* if there was a timeout callback registered, it is now
  1122. ** invalid/useless. free any references we may have */
  1123. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_cb);
  1124. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_udata);
  1125. db->busy_cb =
  1126. db->busy_udata = LUA_NOREF;
  1127. return 0;
  1128. }
  1129. /*
  1130. ** Params: db, sql, callback, user
  1131. ** returns: code [, errmsg]
  1132. **
  1133. ** Callback:
  1134. ** Params: user, number of columns, values, names
  1135. ** Returns: 0 to continue, other value will cause abort
  1136. */
  1137. static int db_exec_callback(void* user, int columns, char **data, char **names) {
  1138. int result = SQLITE_ABORT; /* abort by default */
  1139. lua_State *L = (lua_State*)user;
  1140. int n;
  1141. int top = lua_gettop(L);
  1142. lua_pushvalue(L, 3); /* function to call */
  1143. lua_pushvalue(L, 4); /* user data */
  1144. lua_pushnumber(L, columns); /* total number of rows in result */
  1145. /* column values */
  1146. lua_pushvalue(L, 6);
  1147. for (n = 0; n < columns;) {
  1148. lua_pushstring(L, data[n++]);
  1149. lua_rawseti(L, -2, n);
  1150. }
  1151. /* columns names */
  1152. lua_pushvalue(L, 5);
  1153. if (lua_isnil(L, -1)) {
  1154. lua_pop(L, 1);
  1155. lua_newtable(L);
  1156. lua_pushvalue(L, -1);
  1157. lua_replace(L, 5);
  1158. for (n = 0; n < columns;) {
  1159. lua_pushstring(L, names[n++]);
  1160. lua_rawseti(L, -2, n);
  1161. }
  1162. }
  1163. /* call lua function */
  1164. if (!lua_pcall(L, 4, 1, 0)) {
  1165. if (lua_isnumber(L, -1))
  1166. result = lua_tonumber(L, -1);
  1167. }
  1168. lua_settop(L, top);
  1169. return result;
  1170. }
  1171. static int db_exec(lua_State *L) {
  1172. sdb *db = lsqlite_checkdb(L, 1);
  1173. const char *sql = luaL_checkstring(L, 2);
  1174. int result;
  1175. if (!lua_isnoneornil(L, 3)) {
  1176. /* stack:
  1177. ** 3: callback function
  1178. ** 4: userdata
  1179. ** 5: column names
  1180. ** 6: reusable column values
  1181. */
  1182. luaL_checktype(L, 3, LUA_TFUNCTION);
  1183. lua_settop(L, 4); /* 'trap' userdata - nil extra parameters */
  1184. lua_pushnil(L); /* column names not known at this point */
  1185. lua_newtable(L); /* column values table */
  1186. result = sqlite3_exec(db->db, sql, db_exec_callback, L, NULL);
  1187. }
  1188. else {
  1189. /* no callbacks */
  1190. result = sqlite3_exec(db->db, sql, NULL, NULL, NULL);
  1191. }
  1192. lua_pushnumber(L, result);
  1193. return 1;
  1194. }
  1195. /*
  1196. ** Params: db, sql
  1197. ** returns: code, compiled length or error message
  1198. */
  1199. static int db_prepare(lua_State *L) {
  1200. sdb *db = lsqlite_checkdb(L, 1);
  1201. const char *sql = luaL_checkstring(L, 2);
  1202. int sql_len = lua_strlen(L, 2);
  1203. const char *sqltail;
  1204. sdb_vm *svm;
  1205. lua_settop(L,2); /* sql is on top of stack for call to newvm */
  1206. svm = newvm(L, db);
  1207. if (sqlite3_prepare(db->db, sql, sql_len, &svm->vm, &sqltail) != SQLITE_OK) {
  1208. cleanupvm(L, svm);
  1209. lua_pushnil(L);
  1210. lua_pushnumber(L, sqlite3_errcode(db->db));
  1211. return 2;
  1212. }
  1213. /* vm already in the stack */
  1214. lua_pushstring(L, sqltail);
  1215. return 2;
  1216. }
  1217. static int db_do_next_row(lua_State *L, int packed) {
  1218. int result;
  1219. sdb_vm *svm = lsqlite_checkvm(L, 1);
  1220. sqlite3_stmt *vm;
  1221. int columns;
  1222. int i;
  1223. result = stepvm(L, svm);
  1224. vm = svm->vm; /* stepvm may change svm->vm if re-prepare is needed */
  1225. svm->has_values = result == SQLITE_ROW ? 1 : 0;
  1226. svm->columns = columns = sqlite3_data_count(vm);
  1227. if (result == SQLITE_ROW) {
  1228. if (packed) {
  1229. lua_newtable(L);
  1230. if (packed == 1) {
  1231. for (i = 0; i < columns;) {
  1232. vm_push_column(L, vm, i);
  1233. lua_rawseti(L, -2, ++i);
  1234. }
  1235. }
  1236. else {
  1237. for (i = 0; i < columns; ++i) {
  1238. lua_pushstring(L, sqlite3_column_name(vm, i));
  1239. vm_push_column(L, vm, i);
  1240. lua_rawset(L, -3);
  1241. }
  1242. }
  1243. return 1;
  1244. }
  1245. else {
  1246. lua_checkstack(L, columns);
  1247. for (i = 0; i < columns; ++i)
  1248. vm_push_column(L, vm, i);
  1249. return svm->columns;
  1250. }
  1251. }
  1252. if (svm->temp) {
  1253. /* finalize and check for errors */
  1254. result = sqlite3_finalize(vm);
  1255. svm->vm = NULL;
  1256. cleanupvm(L, svm);
  1257. }
  1258. else if (result == SQLITE_DONE) {
  1259. result = sqlite3_reset(vm);
  1260. }
  1261. if (result != SQLITE_OK) {
  1262. lua_pushstring(L, sqlite3_errmsg(svm->db->db));
  1263. lua_error(L);
  1264. }
  1265. return 0;
  1266. }
  1267. static int db_next_row(lua_State *L) {
  1268. return db_do_next_row(L, 0);
  1269. }
  1270. static int db_next_packed_row(lua_State *L) {
  1271. return db_do_next_row(L, 1);
  1272. }
  1273. static int db_next_named_row(lua_State *L) {
  1274. return db_do_next_row(L, 2);
  1275. }
  1276. static int dbvm_do_rows(lua_State *L, int(*f)(lua_State *)) {
  1277. /* sdb_vm *svm = */
  1278. lsqlite_checkvm(L, 1);
  1279. lua_pushvalue(L,1);
  1280. lua_pushcfunction(L, f);
  1281. lua_insert(L, -2);
  1282. return 2;
  1283. }
  1284. static int dbvm_rows(lua_State *L) {
  1285. return dbvm_do_rows(L, db_next_packed_row);
  1286. }
  1287. static int dbvm_nrows(lua_State *L) {
  1288. return dbvm_do_rows(L, db_next_named_row);
  1289. }
  1290. static int dbvm_urows(lua_State *L) {
  1291. return dbvm_do_rows(L, db_next_row);
  1292. }
  1293. static int db_do_rows(lua_State *L, int(*f)(lua_State *)) {
  1294. sdb *db = lsqlite_checkdb(L, 1);
  1295. const char *sql = luaL_checkstring(L, 2);
  1296. sdb_vm *svm;
  1297. lua_settop(L,2); /* sql is on top of stack for call to newvm */
  1298. svm = newvm(L, db);
  1299. svm->temp = 1;
  1300. if (sqlite3_prepare(db->db, sql, -1, &svm->vm, NULL) != SQLITE_OK) {
  1301. cleanupvm(L, svm);
  1302. lua_pushstring(L, sqlite3_errmsg(svm->db->db));
  1303. lua_error(L);
  1304. }
  1305. lua_pushcfunction(L, f);
  1306. lua_insert(L, -2);
  1307. return 2;
  1308. }
  1309. static int db_rows(lua_State *L) {
  1310. return db_do_rows(L, db_next_packed_row);
  1311. }
  1312. static int db_nrows(lua_State *L) {
  1313. return db_do_rows(L, db_next_named_row);
  1314. }
  1315. /* unpacked version of db:rows */
  1316. static int db_urows(lua_State *L) {
  1317. return db_do_rows(L, db_next_row);
  1318. }
  1319. static int db_tostring(lua_State *L) {
  1320. char buff[32];
  1321. sdb *db = lsqlite_getdb(L, 1);
  1322. if (db->db == NULL)
  1323. strcpy(buff, "closed");
  1324. else
  1325. sprintf(buff, "%p", lua_touserdata(L, 1));
  1326. lua_pushfstring(L, "sqlite database (%s)", buff);
  1327. return 1;
  1328. }
  1329. static int db_close(lua_State *L) {
  1330. sdb *db = lsqlite_checkdb(L, 1);
  1331. lua_pushnumber(L, cleanupdb(L, db));
  1332. return 1;
  1333. }
  1334. static int db_close_vm(lua_State *L) {
  1335. sdb *db = lsqlite_checkdb(L, 1);
  1336. /* cleanup temporary only tables? */
  1337. int temp = lua_toboolean(L, 2);
  1338. /* free associated virtual machines */
  1339. lua_pushlightuserdata(L, db);
  1340. lua_rawget(L, LUA_REGISTRYINDEX);
  1341. /* close all used handles */
  1342. lua_pushnil(L);
  1343. while (lua_next(L, -2)) {
  1344. sdb_vm *svm = lua_touserdata(L, -2); /* key: vm; val: sql text */
  1345. if ((!temp || svm->temp) && svm->vm)
  1346. {
  1347. sqlite3_finalize(svm->vm);
  1348. svm->vm = NULL;
  1349. }
  1350. /* leave key in the stack */
  1351. lua_pop(L, 1);
  1352. }
  1353. return 0;
  1354. }
  1355. static int db_gc(lua_State *L) {
  1356. sdb *db = lsqlite_getdb(L, 1);
  1357. if (db->db != NULL) /* ignore closed databases */
  1358. cleanupdb(L, db);
  1359. return 0;
  1360. }
  1361. /*
  1362. ** =======================================================
  1363. ** General library functions
  1364. ** =======================================================
  1365. */
  1366. static int lsqlite_version(lua_State *L) {
  1367. lua_pushstring(L, sqlite3_libversion());
  1368. return 1;
  1369. }
  1370. static int lsqlite_complete(lua_State *L) {
  1371. const char *sql = luaL_checkstring(L, 1);
  1372. lua_pushboolean(L, sqlite3_complete(sql));
  1373. return 1;
  1374. }
  1375. #ifndef WIN32
  1376. static int lsqlite_temp_directory(lua_State *L) {
  1377. const char *oldtemp = sqlite3_temp_directory;
  1378. if (!lua_isnone(L, 1)) {
  1379. const char *temp = luaL_optstring(L, 1, NULL);
  1380. if (sqlite3_temp_directory) {
  1381. sqlite3_free((char*)sqlite3_temp_directory);
  1382. }
  1383. if (temp) {
  1384. sqlite3_temp_directory = sqlite3_mprintf("%s", temp);
  1385. }
  1386. else {
  1387. sqlite3_temp_directory = NULL;
  1388. }
  1389. }
  1390. lua_pushstring(L, oldtemp);
  1391. return 1;
  1392. }
  1393. #endif
  1394. static int lsqlite_do_open(lua_State *L, const char *filename) {
  1395. sdb *db = newdb(L); /* create and leave in stack */
  1396. if (sqlite3_open(filename, &db->db) == SQLITE_OK) {
  1397. /* database handle already in the stack - return it */
  1398. return 1;
  1399. }
  1400. /* failed to open database */
  1401. lua_pushnil(L); /* push nil */
  1402. lua_pushnumber(L, sqlite3_errcode(db->db));
  1403. lua_pushstring(L, sqlite3_errmsg(db->db)); /* push error message */
  1404. /* clean things up */
  1405. cleanupdb(L, db);
  1406. /* return */
  1407. return 3;
  1408. }
  1409. static int lsqlite_open(lua_State *L) {
  1410. const char *filename = luaL_checkstring(L, 1);
  1411. return lsqlite_do_open(L, filename);
  1412. }
  1413. static int lsqlite_open_memory(lua_State *L) {
  1414. return lsqlite_do_open(L, ":memory:");
  1415. }
  1416. static int lsqlite_newindex(lua_State *L) {
  1417. lua_pushliteral(L, "attempt to change readonly table");
  1418. lua_error(L);
  1419. return 0;
  1420. }
  1421. /*
  1422. ** =======================================================
  1423. ** Register functions
  1424. ** =======================================================
  1425. */
  1426. #define SC(s) { #s, SQLITE_ ## s },
  1427. #define LSC(s) { #s, LSQLITE_ ## s },
  1428. static const struct {
  1429. const char* name;
  1430. int value;
  1431. } sqlite_constants[] = {
  1432. /* error codes */
  1433. SC(OK) SC(ERROR) SC(INTERNAL) SC(PERM)
  1434. SC(ABORT) SC(BUSY) SC(LOCKED) SC(NOMEM)
  1435. SC(READONLY) SC(INTERRUPT) SC(IOERR) SC(CORRUPT)
  1436. SC(NOTFOUND) SC(FULL) SC(CANTOPEN) SC(PROTOCOL)
  1437. SC(EMPTY) SC(SCHEMA) SC(TOOBIG) SC(CONSTRAINT)
  1438. SC(MISMATCH) SC(MISUSE) SC(NOLFS)
  1439. SC(FORMAT) SC(NOTADB)
  1440. /* sqlite_step specific return values */
  1441. SC(RANGE) SC(ROW) SC(DONE)
  1442. /* column types */
  1443. SC(INTEGER) SC(FLOAT) SC(TEXT) SC(BLOB)
  1444. SC(NULL)
  1445. /* terminator */
  1446. { NULL, 0 }
  1447. };
  1448. /* ======================================================= */
  1449. static const luaL_Reg dblib[] = {
  1450. {"isopen", db_isopen },
  1451. {"last_insert_rowid", db_last_insert_rowid },
  1452. {"changes", db_changes },
  1453. {"total_changes", db_total_changes },
  1454. {"errcode", db_errcode },
  1455. {"error_code", db_errcode },
  1456. {"errmsg", db_errmsg },
  1457. {"error_message", db_errmsg },
  1458. {"interrupt", db_interrupt },
  1459. {"create_function", db_create_function },
  1460. {"create_aggregate", db_create_aggregate },
  1461. {"create_collation", db_create_collation },
  1462. {"trace", db_trace },
  1463. {"progress_handler", db_progress_handler },
  1464. {"busy_timeout", db_busy_timeout },
  1465. {"busy_handler", db_busy_handler },
  1466. {"prepare", db_prepare },
  1467. {"rows", db_rows },
  1468. {"urows", db_urows },
  1469. {"nrows", db_nrows },
  1470. {"exec", db_exec },
  1471. {"execute", db_exec },
  1472. {"close", db_close },
  1473. {"close_vm", db_close_vm },
  1474. {"__tostring", db_tostring },
  1475. {"__gc", db_gc },
  1476. {NULL, NULL}
  1477. };
  1478. static const luaL_Reg vmlib[] = {
  1479. {"isopen", dbvm_isopen },
  1480. {"step", dbvm_step },
  1481. {"reset", dbvm_reset },
  1482. {"finalize", dbvm_finalize },
  1483. {"columns", dbvm_columns },
  1484. {"bind", dbvm_bind },
  1485. {"bind_values", dbvm_bind_values },
  1486. {"bind_names", dbvm_bind_names },
  1487. {"bind_blob", dbvm_bind_blob },
  1488. {"bind_parameter_count",dbvm_bind_parameter_count},
  1489. {"bind_parameter_name", dbvm_bind_parameter_name},
  1490. {"get_value", dbvm_get_value },
  1491. {"get_values", dbvm_get_values },
  1492. {"get_name", dbvm_get_name },
  1493. {"get_names", dbvm_get_names },
  1494. {"get_type", dbvm_get_type },
  1495. {"get_types", dbvm_get_types },
  1496. {"get_uvalues", dbvm_get_uvalues },
  1497. {"get_unames", dbvm_get_unames },
  1498. {"get_utypes", dbvm_get_utypes },
  1499. {"get_named_values", dbvm_get_named_values },
  1500. {"get_named_types", dbvm_get_named_types },
  1501. {"rows", dbvm_rows },
  1502. {"urows", dbvm_urows },
  1503. {"nrows", dbvm_nrows },
  1504. /* compatibility names (added by request) */
  1505. {"idata", dbvm_get_values },
  1506. {"inames", dbvm_get_names },
  1507. {"itypes", dbvm_get_types },
  1508. {"data", dbvm_get_named_values },
  1509. {"type", dbvm_get_named_types },
  1510. {"__tostring", dbvm_tostring },
  1511. {"__gc", dbvm_gc },
  1512. { NULL, NULL }
  1513. };
  1514. static const luaL_Reg ctxlib[] = {
  1515. {"user_data", lcontext_user_data },
  1516. {"get_aggregate_data", lcontext_get_aggregate_context },
  1517. {"set_aggregate_data", lcontext_set_aggregate_context },
  1518. {"aggregate_count", lcontext_aggregate_count },
  1519. {"result", lcontext_result },
  1520. {"result_null", lcontext_result_null },
  1521. {"result_number", lcontext_result_double },
  1522. {"result_double", lcontext_result_double },
  1523. {"result_int", lcontext_result_int },
  1524. {"result_text", lcontext_result_text },
  1525. {"result_blob", lcontext_result_blob },
  1526. {"result_error", lcontext_result_error },
  1527. {"__tostring", lcontext_tostring },
  1528. {NULL, NULL}
  1529. };
  1530. static const luaL_Reg sqlitelib[] = {
  1531. {"version", lsqlite_version },
  1532. {"complete", lsqlite_complete },
  1533. #ifndef WIN32
  1534. {"temp_directory", lsqlite_temp_directory },
  1535. #endif
  1536. {"open", lsqlite_open },
  1537. {"open_memory", lsqlite_open_memory },
  1538. {"__newindex", lsqlite_newindex },
  1539. {NULL, NULL}
  1540. };
  1541. static void create_meta(lua_State *L, const char *name, const luaL_Reg *lib) {
  1542. luaL_newmetatable(L, name);
  1543. lua_pushstring(L, "__index");
  1544. lua_pushvalue(L, -2); /* push metatable */
  1545. lua_rawset(L, -3); /* metatable.__index = metatable */
  1546. /* register metatable functions */
  1547. luaL_openlib(L, NULL, lib, 0);
  1548. /* remove metatable from stack */
  1549. lua_pop(L, 1);
  1550. }
  1551. LUALIB_API int luaopen_lsqlite3(lua_State *L) {
  1552. create_meta(L, sqlite_meta, dblib);
  1553. create_meta(L, sqlite_vm_meta, vmlib);
  1554. create_meta(L, sqlite_ctx_meta, ctxlib);
  1555. luaL_getmetatable(L, sqlite_ctx_meta);
  1556. sqlite_ctx_meta_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1557. /* register (local) sqlite metatable */
  1558. luaL_register(L, "sqlite3", sqlitelib);
  1559. {
  1560. int i = 0;
  1561. /* add constants to global table */
  1562. while (sqlite_constants[i].name) {
  1563. lua_pushstring(L, sqlite_constants[i].name);
  1564. lua_pushnumber(L, sqlite_constants[i].value);
  1565. lua_rawset(L, -3);
  1566. ++i;
  1567. }
  1568. }
  1569. /* set sqlite's metatable to itself - set as readonly (__newindex) */
  1570. lua_pushvalue(L, -1);
  1571. lua_setmetatable(L, -2);
  1572. return 1;
  1573. }