sqlite3.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431
  1. /************************************************************************
  2. * lsqlite3 *
  3. * Copyright (C) 2002-2016 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 - an 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. #define LSQLITE_VERSION "0.9.4"
  29. #define LSQLITE_OMIT_UPDATE_HOOK 1
  30. #define SQLITE_OMIT_PROGRESS_CALLBACK 1
  31. #include <c_stdlib.h>
  32. #include <c_string.h>
  33. #include <assert.h>
  34. #define LUA_LIB
  35. #include "module.h"
  36. #include "lua.h"
  37. #include "lauxlib.h"
  38. #if LUA_VERSION_NUM > 501
  39. /*
  40. ** Lua 5.2
  41. */
  42. #define lua_strlen lua_rawlen
  43. /* luaL_typerror always used with arg at ndx == NULL */
  44. #define luaL_typerror(L,ndx,str) luaL_error(L,"bad argument %d (%s expected, got nil)",ndx,str)
  45. /* luaL_register used once, so below expansion is OK for this case */
  46. #define luaL_register(L,name,reg) lua_newtable(L);luaL_setfuncs(L,reg,0)
  47. /* luaL_openlib always used with name == NULL */
  48. #define luaL_openlib(L,name,reg,nup) luaL_setfuncs(L,reg,nup)
  49. #if LUA_VERSION_NUM > 502
  50. /*
  51. ** Lua 5.3
  52. */
  53. #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
  54. #endif
  55. #endif
  56. #include "sqlite3.h"
  57. /* compile time features */
  58. #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK)
  59. #define SQLITE_OMIT_PROGRESS_CALLBACK 0
  60. #endif
  61. #if !defined(LSQLITE_OMIT_UPDATE_HOOK)
  62. #define LSQLITE_OMIT_UPDATE_HOOK 0
  63. #endif
  64. #if defined(LSQLITE_OMIT_OPEN_V2)
  65. #define SQLITE3_OPEN(L,filename,flags) sqlite3_open(L,filename)
  66. #else
  67. #define SQLITE3_OPEN(L,filename,flags) sqlite3_open_v2(L,filename,flags,NULL)
  68. #endif
  69. typedef struct sdb sdb;
  70. typedef struct sdb_vm sdb_vm;
  71. typedef struct sdb_bu sdb_bu;
  72. typedef struct sdb_func sdb_func;
  73. /* to use as C user data so i know what function sqlite is calling */
  74. struct sdb_func {
  75. /* references to associated lua values */
  76. int fn_step;
  77. int fn_finalize;
  78. int udata;
  79. sdb *db;
  80. char aggregate;
  81. sdb_func *next;
  82. };
  83. /* information about database */
  84. struct sdb {
  85. /* associated lua state */
  86. lua_State *L;
  87. /* sqlite database handle */
  88. sqlite3 *db;
  89. /* sql functions stack usage */
  90. sdb_func *func; /* top SQL function being called */
  91. /* references */
  92. int busy_cb; /* busy callback */
  93. int busy_udata;
  94. int progress_cb; /* progress handler */
  95. int progress_udata;
  96. int trace_cb; /* trace callback */
  97. int trace_udata;
  98. #if !defined(LSQLITE_OMIT_UPDATE_HOOK) || !LSQLITE_OMIT_UPDATE_HOOK
  99. int update_hook_cb; /* update_hook callback */
  100. int update_hook_udata;
  101. int commit_hook_cb; /* commit_hook callback */
  102. int commit_hook_udata;
  103. int rollback_hook_cb; /* rollback_hook callback */
  104. int rollback_hook_udata;
  105. #endif
  106. };
  107. static const char *sqlite_meta = ":sqlite3";
  108. static const char *sqlite_vm_meta = ":sqlite3:vm";
  109. static const char *sqlite_bu_meta = ":sqlite3:bu";
  110. static const char *sqlite_ctx_meta = ":sqlite3:ctx";
  111. static int sqlite_ctx_meta_ref;
  112. /* Lua 5.3 introduced an integer type, but depending on the implementation, it could be 32
  113. ** or 64 bits (or something else?). This helper macro tries to do "the right thing."
  114. */
  115. #if LUA_VERSION_NUM > 502
  116. #define PUSH_INT64(L,i64in,fallback) \
  117. do { \
  118. sqlite_int64 i64 = i64in; \
  119. lua_Integer i = (lua_Integer )i64; \
  120. if (i == i64) lua_pushinteger(L, i);\
  121. else { \
  122. lua_Number n = (lua_Number)i64; \
  123. if (n == i64) lua_pushnumber(L, n); \
  124. else fallback; \
  125. } \
  126. } while (0)
  127. #else
  128. #define PUSH_INT64(L,i64in,fallback) \
  129. do { \
  130. sqlite_int64 i64 = i64in; \
  131. lua_Number n = (lua_Number)i64; \
  132. if (n == i64) lua_pushnumber(L, n); \
  133. else fallback; \
  134. } while (0)
  135. #endif
  136. /*
  137. ** =======================================================
  138. ** Database Virtual Machine Operations
  139. ** =======================================================
  140. */
  141. static void vm_push_column(lua_State *L, sqlite3_stmt *vm, int idx) {
  142. switch (sqlite3_column_type(vm, idx)) {
  143. case SQLITE_INTEGER:
  144. PUSH_INT64(L, sqlite3_column_int64(vm, idx)
  145. , lua_pushlstring(L, (const char*)sqlite3_column_text(vm, idx)
  146. , sqlite3_column_bytes(vm, idx)));
  147. break;
  148. case SQLITE_FLOAT:
  149. lua_pushnumber(L, sqlite3_column_double(vm, idx));
  150. break;
  151. case SQLITE_TEXT:
  152. lua_pushlstring(L, (const char*)sqlite3_column_text(vm, idx), sqlite3_column_bytes(vm, idx));
  153. break;
  154. case SQLITE_BLOB:
  155. lua_pushlstring(L, sqlite3_column_blob(vm, idx), sqlite3_column_bytes(vm, idx));
  156. break;
  157. case SQLITE_NULL:
  158. lua_pushnil(L);
  159. break;
  160. default:
  161. lua_pushnil(L);
  162. break;
  163. }
  164. }
  165. /* virtual machine information */
  166. struct sdb_vm {
  167. sdb *db; /* associated database handle */
  168. sqlite3_stmt *vm; /* virtual machine */
  169. /* sqlite3_step info */
  170. int columns; /* number of columns in result */
  171. char has_values; /* true when step succeeds */
  172. char temp; /* temporary vm used in db:rows */
  173. };
  174. /* called with db,sql text on the lua stack */
  175. static sdb_vm *newvm(lua_State *L, sdb *db) {
  176. sdb_vm *svm = (sdb_vm*)lua_newuserdata(L, sizeof(sdb_vm)); /* db sql svm_ud -- */
  177. luaL_getmetatable(L, sqlite_vm_meta);
  178. lua_setmetatable(L, -2); /* set metatable */
  179. svm->db = db;
  180. svm->columns = 0;
  181. svm->has_values = 0;
  182. svm->vm = NULL;
  183. svm->temp = 0;
  184. /* add an entry on the database table: svm -> db to keep db live while svm is live */
  185. lua_pushlightuserdata(L, db); /* db sql svm_ud db_lud -- */
  186. lua_rawget(L, LUA_REGISTRYINDEX); /* db sql svm_ud reg[db_lud] -- */
  187. lua_pushlightuserdata(L, svm); /* db sql svm_ud reg[db_lud] svm_lud -- */
  188. lua_pushvalue(L, -5); /* db sql svm_ud reg[db_lud] svm_lud db -- */
  189. lua_rawset(L, -3); /* (reg[db_lud])[svm_lud] = db ; set the db for this vm */
  190. lua_pop(L, 1); /* db sql svm_ud -- */
  191. return svm;
  192. }
  193. static int cleanupvm(lua_State *L, sdb_vm *svm) {
  194. /* remove entry in database table - no harm if not present in the table */
  195. lua_pushlightuserdata(L, svm->db);
  196. lua_rawget(L, LUA_REGISTRYINDEX);
  197. lua_pushlightuserdata(L, svm);
  198. lua_pushnil(L);
  199. lua_rawset(L, -3);
  200. lua_pop(L, 1);
  201. svm->columns = 0;
  202. svm->has_values = 0;
  203. if (!svm->vm) return 0;
  204. lua_pushinteger(L, sqlite3_finalize(svm->vm));
  205. svm->vm = NULL;
  206. return 1;
  207. }
  208. static int stepvm(lua_State *L, sdb_vm *svm) {
  209. return sqlite3_step(svm->vm);
  210. }
  211. static sdb_vm *lsqlite_getvm(lua_State *L, int index) {
  212. sdb_vm *svm = (sdb_vm*)luaL_checkudata(L, index, sqlite_vm_meta);
  213. if (svm == NULL) luaL_argerror(L, index, "bad sqlite virtual machine");
  214. return svm;
  215. }
  216. static sdb_vm *lsqlite_checkvm(lua_State *L, int index) {
  217. sdb_vm *svm = lsqlite_getvm(L, index);
  218. if (svm->vm == NULL) luaL_argerror(L, index, "attempt to use closed sqlite virtual machine");
  219. return svm;
  220. }
  221. static int dbvm_isopen(lua_State *L) {
  222. sdb_vm *svm = lsqlite_getvm(L, 1);
  223. lua_pushboolean(L, svm->vm != NULL ? 1 : 0);
  224. return 1;
  225. }
  226. static int dbvm_tostring(lua_State *L) {
  227. char buff[39];
  228. sdb_vm *svm = lsqlite_getvm(L, 1);
  229. if (svm->vm == NULL)
  230. strcpy(buff, "closed");
  231. else
  232. c_sprintf(buff, "%p", svm);
  233. lua_pushfstring(L, "sqlite virtual machine (%s)", buff);
  234. return 1;
  235. }
  236. static int dbvm_gc(lua_State *L) {
  237. sdb_vm *svm = lsqlite_getvm(L, 1);
  238. if (svm->vm != NULL) /* ignore closed vms */
  239. cleanupvm(L, svm);
  240. return 0;
  241. }
  242. static int dbvm_step(lua_State *L) {
  243. int result;
  244. sdb_vm *svm = lsqlite_checkvm(L, 1);
  245. result = stepvm(L, svm);
  246. svm->has_values = result == SQLITE_ROW ? 1 : 0;
  247. svm->columns = sqlite3_data_count(svm->vm);
  248. lua_pushinteger(L, result);
  249. return 1;
  250. }
  251. static int dbvm_finalize(lua_State *L) {
  252. sdb_vm *svm = lsqlite_checkvm(L, 1);
  253. return cleanupvm(L, svm);
  254. }
  255. static int dbvm_reset(lua_State *L) {
  256. sdb_vm *svm = lsqlite_checkvm(L, 1);
  257. sqlite3_reset(svm->vm);
  258. lua_pushinteger(L, sqlite3_errcode(svm->db->db));
  259. return 1;
  260. }
  261. static void dbvm_check_contents(lua_State *L, sdb_vm *svm) {
  262. if (!svm->has_values) {
  263. luaL_error(L, "misuse of function");
  264. }
  265. }
  266. static void dbvm_check_index(lua_State *L, sdb_vm *svm, int index) {
  267. if (index < 0 || index >= svm->columns) {
  268. luaL_error(L, "index out of range [0..%d]", svm->columns - 1);
  269. }
  270. }
  271. static void dbvm_check_bind_index(lua_State *L, sdb_vm *svm, int index) {
  272. if (index < 1 || index > sqlite3_bind_parameter_count(svm->vm)) {
  273. luaL_error(L, "bind index out of range [1..%d]", sqlite3_bind_parameter_count(svm->vm));
  274. }
  275. }
  276. static int dbvm_last_insert_rowid(lua_State *L) {
  277. sdb_vm *svm = lsqlite_checkvm(L, 1);
  278. /* conversion warning: int64 -> luaNumber */
  279. sqlite_int64 rowid = sqlite3_last_insert_rowid(svm->db->db);
  280. PUSH_INT64(L, rowid, lua_pushfstring(L, "%ll", rowid));
  281. return 1;
  282. }
  283. /*
  284. ** =======================================================
  285. ** Virtual Machine - generic info
  286. ** =======================================================
  287. */
  288. static int dbvm_columns(lua_State *L) {
  289. sdb_vm *svm = lsqlite_checkvm(L, 1);
  290. lua_pushinteger(L, sqlite3_column_count(svm->vm));
  291. return 1;
  292. }
  293. /*
  294. ** =======================================================
  295. ** Virtual Machine - getters
  296. ** =======================================================
  297. */
  298. static int dbvm_get_value(lua_State *L) {
  299. sdb_vm *svm = lsqlite_checkvm(L, 1);
  300. int index = luaL_checkint(L, 2);
  301. dbvm_check_contents(L, svm);
  302. dbvm_check_index(L, svm, index);
  303. vm_push_column(L, svm->vm, index);
  304. return 1;
  305. }
  306. static int dbvm_get_name(lua_State *L) {
  307. sdb_vm *svm = lsqlite_checkvm(L, 1);
  308. int index = luaL_checknumber(L, 2);
  309. dbvm_check_index(L, svm, index);
  310. lua_pushstring(L, sqlite3_column_name(svm->vm, index));
  311. return 1;
  312. }
  313. #if 0
  314. static int dbvm_get_type(lua_State *L) {
  315. sdb_vm *svm = lsqlite_checkvm(L, 1);
  316. int index = luaL_checknumber(L, 2);
  317. dbvm_check_index(L, svm, index);
  318. lua_pushstring(L, sqlite3_column_decltype(svm->vm, index));
  319. return 1;
  320. }
  321. #else
  322. static int dbvm_get_type(lua_State *L) {
  323. lua_pushliteral(L, "column decltype support disabled at compile time");
  324. lua_error(L);
  325. return 0;
  326. }
  327. #endif
  328. static int dbvm_get_values(lua_State *L) {
  329. sdb_vm *svm = lsqlite_checkvm(L, 1);
  330. sqlite3_stmt *vm = svm->vm;
  331. int columns = svm->columns;
  332. int n;
  333. dbvm_check_contents(L, svm);
  334. lua_createtable(L, columns, 0);
  335. for (n = 0; n < columns;) {
  336. vm_push_column(L, vm, n++);
  337. lua_rawseti(L, -2, n);
  338. }
  339. return 1;
  340. }
  341. static int dbvm_get_names(lua_State *L) {
  342. sdb_vm *svm = lsqlite_checkvm(L, 1);
  343. sqlite3_stmt *vm = svm->vm;
  344. int columns = sqlite3_column_count(vm); /* valid as soon as statement prepared */
  345. int n;
  346. lua_createtable(L, columns, 0);
  347. for (n = 0; n < columns;) {
  348. lua_pushstring(L, sqlite3_column_name(vm, n++));
  349. lua_rawseti(L, -2, n);
  350. }
  351. return 1;
  352. }
  353. #if 0
  354. static int dbvm_get_types(lua_State *L) {
  355. sdb_vm *svm = lsqlite_checkvm(L, 1);
  356. sqlite3_stmt *vm = svm->vm;
  357. int columns = sqlite3_column_count(vm); /* valid as soon as statement prepared */
  358. int n;
  359. lua_createtable(L, columns, 0);
  360. for (n = 0; n < columns;) {
  361. lua_pushstring(L, sqlite3_column_decltype(vm, n++));
  362. lua_rawseti(L, -2, n);
  363. }
  364. return 1;
  365. }
  366. #else
  367. static int dbvm_get_types(lua_State *L) {
  368. lua_pushliteral(L, "column decltype support disabled at compile time");
  369. lua_error(L);
  370. return 0;
  371. }
  372. #endif
  373. static int dbvm_get_uvalues(lua_State *L) {
  374. sdb_vm *svm = lsqlite_checkvm(L, 1);
  375. sqlite3_stmt *vm = svm->vm;
  376. int columns = svm->columns;
  377. int n;
  378. dbvm_check_contents(L, svm);
  379. lua_checkstack(L, columns);
  380. for (n = 0; n < columns; ++n)
  381. vm_push_column(L, vm, n);
  382. return columns;
  383. }
  384. static int dbvm_get_unames(lua_State *L) {
  385. sdb_vm *svm = lsqlite_checkvm(L, 1);
  386. sqlite3_stmt *vm = svm->vm;
  387. int columns = sqlite3_column_count(vm); /* valid as soon as statement prepared */
  388. int n;
  389. lua_checkstack(L, columns);
  390. for (n = 0; n < columns; ++n)
  391. lua_pushstring(L, sqlite3_column_name(vm, n));
  392. return columns;
  393. }
  394. #if 0
  395. static int dbvm_get_utypes(lua_State *L) {
  396. sdb_vm *svm = lsqlite_checkvm(L, 1);
  397. sqlite3_stmt *vm = svm->vm;
  398. int columns = sqlite3_column_count(vm); /* valid as soon as statement prepared */
  399. int n;
  400. lua_checkstack(L, columns);
  401. for (n = 0; n < columns; ++n)
  402. lua_pushstring(L, sqlite3_column_decltype(vm, n));
  403. return columns;
  404. }
  405. #else
  406. static int dbvm_get_utypes(lua_State *L) {
  407. lua_pushliteral(L, "column decltype support disabled at compile time");
  408. lua_error(L);
  409. return 0;
  410. }
  411. #endif
  412. static int dbvm_get_named_values(lua_State *L) {
  413. sdb_vm *svm = lsqlite_checkvm(L, 1);
  414. sqlite3_stmt *vm = svm->vm;
  415. int columns = svm->columns;
  416. int n;
  417. dbvm_check_contents(L, svm);
  418. lua_createtable(L, 0, columns);
  419. for (n = 0; n < columns; ++n) {
  420. lua_pushstring(L, sqlite3_column_name(vm, n));
  421. vm_push_column(L, vm, n);
  422. lua_rawset(L, -3);
  423. }
  424. return 1;
  425. }
  426. #if 0
  427. static int dbvm_get_named_types(lua_State *L) {
  428. sdb_vm *svm = lsqlite_checkvm(L, 1);
  429. sqlite3_stmt *vm = svm->vm;
  430. int columns = sqlite3_column_count(vm);
  431. int n;
  432. lua_createtable(L, 0, columns);
  433. for (n = 0; n < columns; ++n) {
  434. lua_pushstring(L, sqlite3_column_name(vm, n));
  435. lua_pushstring(L, sqlite3_column_decltype(vm, n));
  436. lua_rawset(L, -3);
  437. }
  438. return 1;
  439. }
  440. #else
  441. static int dbvm_get_named_types(lua_State *L) {
  442. lua_pushliteral(L, "column decltype support disabled at compile time");
  443. lua_error(L);
  444. return 0;
  445. }
  446. #endif
  447. /*
  448. ** =======================================================
  449. ** Virtual Machine - Bind
  450. ** =======================================================
  451. */
  452. static int dbvm_bind_index(lua_State *L, sqlite3_stmt *vm, int index, int lindex) {
  453. switch (lua_type(L, lindex)) {
  454. case LUA_TSTRING:
  455. return sqlite3_bind_text(vm, index, lua_tostring(L, lindex), lua_strlen(L, lindex), SQLITE_TRANSIENT);
  456. case LUA_TNUMBER:
  457. #if LUA_VERSION_NUM > 502
  458. if (lua_isinteger(L, lindex))
  459. return sqlite3_bind_int64(vm, index, lua_tointeger(L, lindex));
  460. #endif
  461. return sqlite3_bind_double(vm, index, lua_tonumber(L, lindex));
  462. case LUA_TBOOLEAN:
  463. return sqlite3_bind_int(vm, index, lua_toboolean(L, lindex) ? 1 : 0);
  464. case LUA_TNONE:
  465. case LUA_TNIL:
  466. return sqlite3_bind_null(vm, index);
  467. default:
  468. luaL_error(L, "index (%d) - invalid data type for bind (%s)", index, lua_typename(L, lua_type(L, lindex)));
  469. return SQLITE_MISUSE; /*!*/
  470. }
  471. }
  472. static int dbvm_bind_parameter_count(lua_State *L) {
  473. sdb_vm *svm = lsqlite_checkvm(L, 1);
  474. lua_pushinteger(L, sqlite3_bind_parameter_count(svm->vm));
  475. return 1;
  476. }
  477. static int dbvm_bind_parameter_name(lua_State *L) {
  478. sdb_vm *svm = lsqlite_checkvm(L, 1);
  479. int index = luaL_checknumber(L, 2);
  480. dbvm_check_bind_index(L, svm, index);
  481. lua_pushstring(L, sqlite3_bind_parameter_name(svm->vm, index));
  482. return 1;
  483. }
  484. static int dbvm_bind(lua_State *L) {
  485. sdb_vm *svm = lsqlite_checkvm(L, 1);
  486. sqlite3_stmt *vm = svm->vm;
  487. int index = luaL_checkint(L, 2);
  488. int result;
  489. dbvm_check_bind_index(L, svm, index);
  490. result = dbvm_bind_index(L, vm, index, 3);
  491. lua_pushinteger(L, result);
  492. return 1;
  493. }
  494. static int dbvm_bind_blob(lua_State *L) {
  495. sdb_vm *svm = lsqlite_checkvm(L, 1);
  496. int index = luaL_checkint(L, 2);
  497. const char *value = luaL_checkstring(L, 3);
  498. int len = lua_strlen(L, 3);
  499. lua_pushinteger(L, sqlite3_bind_blob(svm->vm, index, value, len, SQLITE_TRANSIENT));
  500. return 1;
  501. }
  502. static int dbvm_bind_values(lua_State *L) {
  503. sdb_vm *svm = lsqlite_checkvm(L, 1);
  504. sqlite3_stmt *vm = svm->vm;
  505. int top = lua_gettop(L);
  506. int result, n;
  507. if (top - 1 != sqlite3_bind_parameter_count(vm))
  508. luaL_error(L,
  509. "incorrect number of parameters to bind (%d given, %d to bind)",
  510. top - 1,
  511. sqlite3_bind_parameter_count(vm)
  512. );
  513. for (n = 2; n <= top; ++n) {
  514. if ((result = dbvm_bind_index(L, vm, n - 1, n)) != SQLITE_OK) {
  515. lua_pushinteger(L, result);
  516. return 1;
  517. }
  518. }
  519. lua_pushinteger(L, SQLITE_OK);
  520. return 1;
  521. }
  522. static int dbvm_bind_names(lua_State *L) {
  523. sdb_vm *svm = lsqlite_checkvm(L, 1);
  524. sqlite3_stmt *vm = svm->vm;
  525. int count = sqlite3_bind_parameter_count(vm);
  526. const char *name;
  527. int result, n;
  528. luaL_checktype(L, 2, LUA_TTABLE);
  529. for (n = 1; n <= count; ++n) {
  530. name = sqlite3_bind_parameter_name(vm, n);
  531. if (name && (name[0] == ':' || name[0] == '$')) {
  532. lua_pushstring(L, ++name);
  533. lua_gettable(L, 2);
  534. result = dbvm_bind_index(L, vm, n, -1);
  535. lua_pop(L, 1);
  536. }
  537. else {
  538. lua_pushinteger(L, n);
  539. lua_gettable(L, 2);
  540. result = dbvm_bind_index(L, vm, n, -1);
  541. lua_pop(L, 1);
  542. }
  543. if (result != SQLITE_OK) {
  544. lua_pushinteger(L, result);
  545. return 1;
  546. }
  547. }
  548. lua_pushinteger(L, SQLITE_OK);
  549. return 1;
  550. }
  551. /*
  552. ** =======================================================
  553. ** Database (internal management)
  554. ** =======================================================
  555. */
  556. /*
  557. ** When creating database handles, always creates a `closed' database handle
  558. ** before opening the actual database; so, if there is a memory error, the
  559. ** database is not left opened.
  560. **
  561. ** Creates a new 'table' and leaves it in the stack
  562. */
  563. static sdb *newdb (lua_State *L) {
  564. sdb *db = (sdb*)lua_newuserdata(L, sizeof(sdb));
  565. db->L = L;
  566. db->db = NULL; /* database handle is currently `closed' */
  567. db->func = NULL;
  568. db->busy_cb =
  569. db->busy_udata =
  570. db->progress_cb =
  571. db->progress_udata =
  572. db->trace_cb =
  573. db->trace_udata =
  574. #if !defined(LSQLITE_OMIT_UPDATE_HOOK) || !LSQLITE_OMIT_UPDATE_HOOK
  575. db->update_hook_cb =
  576. db->update_hook_udata =
  577. db->commit_hook_cb =
  578. db->commit_hook_udata =
  579. db->rollback_hook_cb =
  580. db->rollback_hook_udata =
  581. #endif
  582. LUA_NOREF;
  583. luaL_getmetatable(L, sqlite_meta);
  584. lua_setmetatable(L, -2); /* set metatable */
  585. /* to keep track of 'open' virtual machines */
  586. lua_pushlightuserdata(L, db);
  587. lua_newtable(L);
  588. lua_rawset(L, LUA_REGISTRYINDEX);
  589. return db;
  590. }
  591. static int cleanupdb(lua_State *L, sdb *db) {
  592. sdb_func *func;
  593. sdb_func *func_next;
  594. int top;
  595. int result;
  596. /* free associated virtual machines */
  597. lua_pushlightuserdata(L, db);
  598. lua_rawget(L, LUA_REGISTRYINDEX);
  599. /* close all used handles */
  600. top = lua_gettop(L);
  601. lua_pushnil(L);
  602. while (lua_next(L, -2)) {
  603. sdb_vm *svm = lua_touserdata(L, -2); /* key: vm; val: sql text */
  604. cleanupvm(L, svm);
  605. lua_settop(L, top);
  606. lua_pushnil(L);
  607. }
  608. lua_pop(L, 1); /* pop vm table */
  609. /* remove entry in lua registry table */
  610. lua_pushlightuserdata(L, db);
  611. lua_pushnil(L);
  612. lua_rawset(L, LUA_REGISTRYINDEX);
  613. /* 'free' all references */
  614. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_cb);
  615. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_udata);
  616. luaL_unref(L, LUA_REGISTRYINDEX, db->progress_cb);
  617. luaL_unref(L, LUA_REGISTRYINDEX, db->progress_udata);
  618. luaL_unref(L, LUA_REGISTRYINDEX, db->trace_cb);
  619. luaL_unref(L, LUA_REGISTRYINDEX, db->trace_udata);
  620. #if !defined(LSQLITE_OMIT_UPDATE_HOOK) || !LSQLITE_OMIT_UPDATE_HOOK
  621. luaL_unref(L, LUA_REGISTRYINDEX, db->update_hook_cb);
  622. luaL_unref(L, LUA_REGISTRYINDEX, db->update_hook_udata);
  623. luaL_unref(L, LUA_REGISTRYINDEX, db->commit_hook_cb);
  624. luaL_unref(L, LUA_REGISTRYINDEX, db->commit_hook_udata);
  625. luaL_unref(L, LUA_REGISTRYINDEX, db->rollback_hook_cb);
  626. luaL_unref(L, LUA_REGISTRYINDEX, db->rollback_hook_udata);
  627. #endif
  628. /* close database */
  629. result = sqlite3_close(db->db);
  630. db->db = NULL;
  631. /* free associated memory with created functions */
  632. func = db->func;
  633. while (func) {
  634. func_next = func->next;
  635. luaL_unref(L, LUA_REGISTRYINDEX, func->fn_step);
  636. luaL_unref(L, LUA_REGISTRYINDEX, func->fn_finalize);
  637. luaL_unref(L, LUA_REGISTRYINDEX, func->udata);
  638. c_free(func);
  639. func = func_next;
  640. }
  641. db->func = NULL;
  642. return result;
  643. }
  644. static sdb *lsqlite_getdb(lua_State *L, int index) {
  645. sdb *db = (sdb*)luaL_checkudata(L, index, sqlite_meta);
  646. if (db == NULL) luaL_typerror(L, index, "invalid sqlite database");
  647. return db;
  648. }
  649. static sdb *lsqlite_checkdb(lua_State *L, int index) {
  650. sdb *db = lsqlite_getdb(L, index);
  651. if (db->db == NULL) luaL_argerror(L, index, "attempt to use closed sqlite database");
  652. return db;
  653. }
  654. /*
  655. ** =======================================================
  656. ** User Defined Functions - Context Methods
  657. ** =======================================================
  658. */
  659. typedef struct {
  660. sqlite3_context *ctx;
  661. int ud;
  662. } lcontext;
  663. static lcontext *lsqlite_make_context(lua_State *L) {
  664. lcontext *ctx = (lcontext*)lua_newuserdata(L, sizeof(lcontext));
  665. lua_rawgeti(L, LUA_REGISTRYINDEX, sqlite_ctx_meta_ref);
  666. lua_setmetatable(L, -2);
  667. ctx->ctx = NULL;
  668. ctx->ud = LUA_NOREF;
  669. return ctx;
  670. }
  671. static lcontext *lsqlite_getcontext(lua_State *L, int index) {
  672. lcontext *ctx = (lcontext*)luaL_checkudata(L, index, sqlite_ctx_meta);
  673. if (ctx == NULL) luaL_typerror(L, index, "sqlite context");
  674. return ctx;
  675. }
  676. static lcontext *lsqlite_checkcontext(lua_State *L, int index) {
  677. lcontext *ctx = lsqlite_getcontext(L, index);
  678. if (ctx->ctx == NULL) luaL_argerror(L, index, "invalid sqlite context");
  679. return ctx;
  680. }
  681. static int lcontext_tostring(lua_State *L) {
  682. char buff[39];
  683. lcontext *ctx = lsqlite_getcontext(L, 1);
  684. if (ctx->ctx == NULL)
  685. strcpy(buff, "closed");
  686. else
  687. c_sprintf(buff, "%p", ctx->ctx);
  688. lua_pushfstring(L, "sqlite function context (%s)", buff);
  689. return 1;
  690. }
  691. static void lcontext_check_aggregate(lua_State *L, lcontext *ctx) {
  692. sdb_func *func = (sdb_func*)sqlite3_user_data(ctx->ctx);
  693. if (!func->aggregate) {
  694. luaL_error(L, "attempt to call aggregate method from scalar function");
  695. }
  696. }
  697. static int lcontext_user_data(lua_State *L) {
  698. lcontext *ctx = lsqlite_checkcontext(L, 1);
  699. sdb_func *func = (sdb_func*)sqlite3_user_data(ctx->ctx);
  700. lua_rawgeti(L, LUA_REGISTRYINDEX, func->udata);
  701. return 1;
  702. }
  703. static int lcontext_get_aggregate_context(lua_State *L) {
  704. lcontext *ctx = lsqlite_checkcontext(L, 1);
  705. lcontext_check_aggregate(L, ctx);
  706. lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->ud);
  707. return 1;
  708. }
  709. static int lcontext_set_aggregate_context(lua_State *L) {
  710. lcontext *ctx = lsqlite_checkcontext(L, 1);
  711. lcontext_check_aggregate(L, ctx);
  712. lua_settop(L, 2);
  713. luaL_unref(L, LUA_REGISTRYINDEX, ctx->ud);
  714. ctx->ud = luaL_ref(L, LUA_REGISTRYINDEX);
  715. return 0;
  716. }
  717. #if 0
  718. static int lcontext_aggregate_count(lua_State *L) {
  719. lcontext *ctx = lsqlite_checkcontext(L, 1);
  720. lcontext_check_aggregate(L, ctx);
  721. lua_pushinteger(L, sqlite3_aggregate_count(ctx->ctx));
  722. return 1;
  723. }
  724. #else
  725. static int lcontext_aggregate_count(lua_State *L) {
  726. lua_pushliteral(L, "aggregate count support disabled at compile time");
  727. lua_error(L);
  728. return 0;
  729. }
  730. #endif
  731. #if 0
  732. void *sqlite3_get_auxdata(sqlite3_context*, int);
  733. void sqlite3_set_auxdata(sqlite3_context*, int, void*, void (*)(void*));
  734. #endif
  735. static int lcontext_result(lua_State *L) {
  736. lcontext *ctx = lsqlite_checkcontext(L, 1);
  737. switch (lua_type(L, 2)) {
  738. case LUA_TNUMBER:
  739. #if LUA_VERSION_NUM > 502
  740. if (lua_isinteger(L, 2))
  741. sqlite3_result_int64(ctx->ctx, luaL_checkinteger(L, 2));
  742. else
  743. #endif
  744. sqlite3_result_double(ctx->ctx, luaL_checknumber(L, 2));
  745. break;
  746. case LUA_TSTRING:
  747. sqlite3_result_text(ctx->ctx, luaL_checkstring(L, 2), lua_strlen(L, 2), SQLITE_TRANSIENT);
  748. break;
  749. case LUA_TNIL:
  750. case LUA_TNONE:
  751. sqlite3_result_null(ctx->ctx);
  752. break;
  753. default:
  754. luaL_error(L, "invalid result type %s", lua_typename(L, 2));
  755. break;
  756. }
  757. return 0;
  758. }
  759. static int lcontext_result_blob(lua_State *L) {
  760. lcontext *ctx = lsqlite_checkcontext(L, 1);
  761. const char *blob = luaL_checkstring(L, 2);
  762. int size = lua_strlen(L, 2);
  763. sqlite3_result_blob(ctx->ctx, (const void*)blob, size, SQLITE_TRANSIENT);
  764. return 0;
  765. }
  766. static int lcontext_result_double(lua_State *L) {
  767. lcontext *ctx = lsqlite_checkcontext(L, 1);
  768. double d = luaL_checknumber(L, 2);
  769. sqlite3_result_double(ctx->ctx, d);
  770. return 0;
  771. }
  772. static int lcontext_result_error(lua_State *L) {
  773. lcontext *ctx = lsqlite_checkcontext(L, 1);
  774. const char *err = luaL_checkstring(L, 2);
  775. int size = lua_strlen(L, 2);
  776. sqlite3_result_error(ctx->ctx, err, size);
  777. return 0;
  778. }
  779. static int lcontext_result_int(lua_State *L) {
  780. lcontext *ctx = lsqlite_checkcontext(L, 1);
  781. int i = luaL_checkint(L, 2);
  782. sqlite3_result_int(ctx->ctx, i);
  783. return 0;
  784. }
  785. static int lcontext_result_null(lua_State *L) {
  786. lcontext *ctx = lsqlite_checkcontext(L, 1);
  787. sqlite3_result_null(ctx->ctx);
  788. return 0;
  789. }
  790. static int lcontext_result_text(lua_State *L) {
  791. lcontext *ctx = lsqlite_checkcontext(L, 1);
  792. const char *text = luaL_checkstring(L, 2);
  793. int size = lua_strlen(L, 2);
  794. sqlite3_result_text(ctx->ctx, text, size, SQLITE_TRANSIENT);
  795. return 0;
  796. }
  797. /*
  798. ** =======================================================
  799. ** Database Methods
  800. ** =======================================================
  801. */
  802. static int db_isopen(lua_State *L) {
  803. sdb *db = lsqlite_getdb(L, 1);
  804. lua_pushboolean(L, db->db != NULL ? 1 : 0);
  805. return 1;
  806. }
  807. static int db_last_insert_rowid(lua_State *L) {
  808. sdb *db = lsqlite_checkdb(L, 1);
  809. /* conversion warning: int64 -> luaNumber */
  810. sqlite_int64 rowid = sqlite3_last_insert_rowid(db->db);
  811. PUSH_INT64(L, rowid, lua_pushfstring(L, "%ll", rowid));
  812. return 1;
  813. }
  814. static int db_changes(lua_State *L) {
  815. sdb *db = lsqlite_checkdb(L, 1);
  816. lua_pushinteger(L, sqlite3_changes(db->db));
  817. return 1;
  818. }
  819. static int db_total_changes(lua_State *L) {
  820. sdb *db = lsqlite_checkdb(L, 1);
  821. lua_pushinteger(L, sqlite3_total_changes(db->db));
  822. return 1;
  823. }
  824. static int db_errcode(lua_State *L) {
  825. sdb *db = lsqlite_checkdb(L, 1);
  826. lua_pushinteger(L, sqlite3_errcode(db->db));
  827. return 1;
  828. }
  829. static int db_errmsg(lua_State *L) {
  830. sdb *db = lsqlite_checkdb(L, 1);
  831. lua_pushstring(L, sqlite3_errmsg(db->db));
  832. return 1;
  833. }
  834. static int db_interrupt(lua_State *L) {
  835. sdb *db = lsqlite_checkdb(L, 1);
  836. sqlite3_interrupt(db->db);
  837. return 0;
  838. }
  839. static int db_db_filename(lua_State *L) {
  840. sdb *db = lsqlite_checkdb(L, 1);
  841. const char *db_name = luaL_checkstring(L, 2);
  842. // sqlite3_db_filename may return NULL, in that case Lua pushes nil...
  843. lua_pushstring(L, sqlite3_db_filename(db->db, db_name));
  844. return 1;
  845. }
  846. /*
  847. ** Registering SQL functions:
  848. */
  849. static void db_push_value(lua_State *L, sqlite3_value *value) {
  850. switch (sqlite3_value_type(value)) {
  851. case SQLITE_TEXT:
  852. lua_pushlstring(L, (const char*)sqlite3_value_text(value), sqlite3_value_bytes(value));
  853. break;
  854. case SQLITE_INTEGER:
  855. PUSH_INT64(L, sqlite3_value_int64(value)
  856. , lua_pushlstring(L, (const char*)sqlite3_value_text(value)
  857. , sqlite3_value_bytes(value)));
  858. break;
  859. case SQLITE_FLOAT:
  860. lua_pushnumber(L, sqlite3_value_double(value));
  861. break;
  862. case SQLITE_BLOB:
  863. lua_pushlstring(L, sqlite3_value_blob(value), sqlite3_value_bytes(value));
  864. break;
  865. case SQLITE_NULL:
  866. lua_pushnil(L);
  867. break;
  868. default:
  869. /* things done properly (SQLite + Lua SQLite)
  870. ** this should never happen */
  871. lua_pushnil(L);
  872. break;
  873. }
  874. }
  875. /*
  876. ** callback functions used when calling registered sql functions
  877. */
  878. /* scalar function to be called
  879. ** callback params: context, values... */
  880. static void db_sql_normal_function(sqlite3_context *context, int argc, sqlite3_value **argv) {
  881. sdb_func *func = (sdb_func*)sqlite3_user_data(context);
  882. lua_State *L = func->db->L;
  883. int n;
  884. lcontext *ctx;
  885. int top = lua_gettop(L);
  886. /* ensure there is enough space in the stack */
  887. lua_checkstack(L, argc + 3);
  888. lua_rawgeti(L, LUA_REGISTRYINDEX, func->fn_step); /* function to call */
  889. if (!func->aggregate) {
  890. ctx = lsqlite_make_context(L); /* push context - used to set results */
  891. }
  892. else {
  893. /* reuse context userdata value */
  894. void *p = sqlite3_aggregate_context(context, 1);
  895. /* i think it is OK to use assume that using a light user data
  896. ** as an entry on LUA REGISTRY table will be unique */
  897. lua_pushlightuserdata(L, p);
  898. lua_rawget(L, LUA_REGISTRYINDEX); /* context table */
  899. if (lua_isnil(L, -1)) { /* not yet created? */
  900. lua_pop(L, 1);
  901. ctx = lsqlite_make_context(L);
  902. lua_pushlightuserdata(L, p);
  903. lua_pushvalue(L, -2);
  904. lua_rawset(L, LUA_REGISTRYINDEX);
  905. }
  906. else
  907. ctx = lsqlite_getcontext(L, -1);
  908. }
  909. /* push params */
  910. for (n = 0; n < argc; ++n) {
  911. db_push_value(L, argv[n]);
  912. }
  913. /* set context */
  914. ctx->ctx = context;
  915. if (lua_pcall(L, argc + 1, 0, 0)) {
  916. const char *errmsg = lua_tostring(L, -1);
  917. int size = lua_strlen(L, -1);
  918. sqlite3_result_error(context, errmsg, size);
  919. }
  920. /* invalidate context */
  921. ctx->ctx = NULL;
  922. if (!func->aggregate) {
  923. luaL_unref(L, LUA_REGISTRYINDEX, ctx->ud);
  924. }
  925. lua_settop(L, top);
  926. }
  927. static void db_sql_finalize_function(sqlite3_context *context) {
  928. sdb_func *func = (sdb_func*)sqlite3_user_data(context);
  929. lua_State *L = func->db->L;
  930. void *p = sqlite3_aggregate_context(context, 1); /* minimal mem usage */
  931. lcontext *ctx;
  932. int top = lua_gettop(L);
  933. lua_rawgeti(L, LUA_REGISTRYINDEX, func->fn_finalize); /* function to call */
  934. /* i think it is OK to use assume that using a light user data
  935. ** as an entry on LUA REGISTRY table will be unique */
  936. lua_pushlightuserdata(L, p);
  937. lua_rawget(L, LUA_REGISTRYINDEX); /* context table */
  938. if (lua_isnil(L, -1)) { /* not yet created? - shouldn't happen in finalize function */
  939. lua_pop(L, 1);
  940. ctx = lsqlite_make_context(L);
  941. lua_pushlightuserdata(L, p);
  942. lua_pushvalue(L, -2);
  943. lua_rawset(L, LUA_REGISTRYINDEX);
  944. }
  945. else
  946. ctx = lsqlite_getcontext(L, -1);
  947. /* set context */
  948. ctx->ctx = context;
  949. if (lua_pcall(L, 1, 0, 0)) {
  950. sqlite3_result_error(context, lua_tostring(L, -1), -1);
  951. }
  952. /* invalidate context */
  953. ctx->ctx = NULL;
  954. /* cleanup context */
  955. luaL_unref(L, LUA_REGISTRYINDEX, ctx->ud);
  956. /* remove it from registry */
  957. lua_pushlightuserdata(L, p);
  958. lua_pushnil(L);
  959. lua_rawset(L, LUA_REGISTRYINDEX);
  960. lua_settop(L, top);
  961. }
  962. /*
  963. ** Register a normal function
  964. ** Params: db, function name, number arguments, [ callback | step, finalize], user data
  965. ** Returns: true on sucess
  966. **
  967. ** Normal function:
  968. ** Params: context, params
  969. **
  970. ** Aggregate function:
  971. ** Params of step: context, params
  972. ** Params of finalize: context
  973. */
  974. static int db_register_function(lua_State *L, int aggregate) {
  975. sdb *db = lsqlite_checkdb(L, 1);
  976. const char *name;
  977. int args;
  978. int result;
  979. sdb_func *func;
  980. /* safety measure */
  981. if (aggregate) aggregate = 1;
  982. name = luaL_checkstring(L, 2);
  983. args = luaL_checkint(L, 3);
  984. luaL_checktype(L, 4, LUA_TFUNCTION);
  985. if (aggregate) luaL_checktype(L, 5, LUA_TFUNCTION);
  986. /* maybe an alternative way to allocate memory should be used/avoided */
  987. func = (sdb_func*)c_malloc(sizeof(sdb_func));
  988. if (func == NULL) {
  989. luaL_error(L, "out of memory");
  990. }
  991. result = sqlite3_create_function(
  992. db->db, name, args, SQLITE_UTF8, func,
  993. aggregate ? NULL : db_sql_normal_function,
  994. aggregate ? db_sql_normal_function : NULL,
  995. aggregate ? db_sql_finalize_function : NULL
  996. );
  997. if (result == SQLITE_OK) {
  998. /* safety measures for userdata field to be present in the stack */
  999. lua_settop(L, 5 + aggregate);
  1000. /* save registered function in db function list */
  1001. func->db = db;
  1002. func->aggregate = aggregate;
  1003. func->next = db->func;
  1004. db->func = func;
  1005. /* save the setp/normal function callback */
  1006. lua_pushvalue(L, 4);
  1007. func->fn_step = luaL_ref(L, LUA_REGISTRYINDEX);
  1008. /* save user data */
  1009. lua_pushvalue(L, 5+aggregate);
  1010. func->udata = luaL_ref(L, LUA_REGISTRYINDEX);
  1011. if (aggregate) {
  1012. lua_pushvalue(L, 5);
  1013. func->fn_finalize = luaL_ref(L, LUA_REGISTRYINDEX);
  1014. }
  1015. else
  1016. func->fn_finalize = LUA_NOREF;
  1017. }
  1018. else {
  1019. /* free allocated memory */
  1020. c_free(func);
  1021. }
  1022. lua_pushboolean(L, result == SQLITE_OK ? 1 : 0);
  1023. return 1;
  1024. }
  1025. static int db_create_function(lua_State *L) {
  1026. return db_register_function(L, 0);
  1027. }
  1028. static int db_create_aggregate(lua_State *L) {
  1029. return db_register_function(L, 1);
  1030. }
  1031. /* create_collation; contributed by Thomas Lauer
  1032. */
  1033. typedef struct {
  1034. lua_State *L;
  1035. int ref;
  1036. } scc;
  1037. static int collwrapper(scc *co,int l1,const void *p1,
  1038. int l2,const void *p2) {
  1039. int res=0;
  1040. lua_State *L=co->L;
  1041. lua_rawgeti(L,LUA_REGISTRYINDEX,co->ref);
  1042. lua_pushlstring(L,p1,l1);
  1043. lua_pushlstring(L,p2,l2);
  1044. if (lua_pcall(L,2,1,0)==0) res=(int)lua_tonumber(L,-1);
  1045. lua_pop(L,1);
  1046. return res;
  1047. }
  1048. static void collfree(scc *co) {
  1049. if (co) {
  1050. luaL_unref(co->L,LUA_REGISTRYINDEX,co->ref);
  1051. c_free(co);
  1052. }
  1053. }
  1054. static int db_create_collation(lua_State *L) {
  1055. sdb *db=lsqlite_checkdb(L,1);
  1056. const char *collname=luaL_checkstring(L,2);
  1057. scc *co=NULL;
  1058. int (*collfunc)(scc *,int,const void *,int,const void *)=NULL;
  1059. lua_settop(L,3); /* default args to nil, and exclude extras */
  1060. if (lua_isfunction(L,3)) collfunc=collwrapper;
  1061. else if (!lua_isnil(L,3))
  1062. luaL_error(L,"create_collation: function or nil expected");
  1063. if (collfunc != NULL) {
  1064. co=(scc *)c_malloc(sizeof(scc)); /* userdata is a no-no as it
  1065. will be garbage-collected */
  1066. if (co) {
  1067. co->L=L;
  1068. /* lua_settop(L,3) above means we don't need: lua_pushvalue(L,3); */
  1069. co->ref=luaL_ref(L,LUA_REGISTRYINDEX);
  1070. }
  1071. else luaL_error(L,"create_collation: could not allocate callback");
  1072. }
  1073. sqlite3_create_collation_v2(db->db, collname, SQLITE_UTF8,
  1074. (void *)co,
  1075. (int(*)(void*,int,const void*,int,const void*))collfunc,
  1076. (void(*)(void*))collfree);
  1077. return 0;
  1078. }
  1079. /* Thanks to Wolfgang Oertl...
  1080. */
  1081. #if 0
  1082. static int db_load_extension(lua_State *L) {
  1083. sdb *db=lsqlite_checkdb(L,1);
  1084. const char *extname=luaL_optstring(L,2,NULL);
  1085. const char *entrypoint=luaL_optstring(L,3,NULL);
  1086. int result;
  1087. char *errmsg = NULL;
  1088. if (extname == NULL) {
  1089. result = sqlite3_enable_load_extension(db->db,0); /* disable extension loading */
  1090. }
  1091. else {
  1092. sqlite3_enable_load_extension(db->db,1); /* enable extension loading */
  1093. result = sqlite3_load_extension(db->db,extname,entrypoint,&errmsg);
  1094. }
  1095. if (result == SQLITE_OK) {
  1096. lua_pushboolean(L,1);
  1097. return 1;
  1098. }
  1099. lua_pushboolean(L,0); /* so, assert(load_extension(...)) works */
  1100. lua_pushstring(L,errmsg);
  1101. sqlite3_free(errmsg);
  1102. return 2;
  1103. }
  1104. #else
  1105. static int db_load_extension(lua_State *L) {
  1106. lua_pushliteral(L, "db_load_extension support disabled at compile time");
  1107. lua_error(L);
  1108. return 0;
  1109. }
  1110. #endif
  1111. /*
  1112. ** trace callback:
  1113. ** Params: database, callback function, userdata
  1114. **
  1115. ** callback function:
  1116. ** Params: userdata, sql
  1117. */
  1118. static void db_trace_callback(void *user, const char *sql) {
  1119. sdb *db = (sdb*)user;
  1120. lua_State *L = db->L;
  1121. int top = lua_gettop(L);
  1122. /* setup lua callback call */
  1123. lua_rawgeti(L, LUA_REGISTRYINDEX, db->trace_cb); /* get callback */
  1124. lua_rawgeti(L, LUA_REGISTRYINDEX, db->trace_udata); /* get callback user data */
  1125. lua_pushstring(L, sql); /* traced sql statement */
  1126. /* call lua function */
  1127. lua_pcall(L, 2, 0, 0);
  1128. /* ignore any error generated by this function */
  1129. lua_settop(L, top);
  1130. }
  1131. #if 0
  1132. static int db_trace(lua_State *L) {
  1133. sdb *db = lsqlite_checkdb(L, 1);
  1134. if (lua_gettop(L) < 2 || lua_isnil(L, 2)) {
  1135. luaL_unref(L, LUA_REGISTRYINDEX, db->trace_cb);
  1136. luaL_unref(L, LUA_REGISTRYINDEX, db->trace_udata);
  1137. db->trace_cb =
  1138. db->trace_udata = LUA_NOREF;
  1139. /* clear trace handler */
  1140. sqlite3_trace(db->db, NULL, NULL);
  1141. }
  1142. else {
  1143. luaL_checktype(L, 2, LUA_TFUNCTION);
  1144. /* make sure we have an userdata field (even if nil) */
  1145. lua_settop(L, 3);
  1146. luaL_unref(L, LUA_REGISTRYINDEX, db->trace_cb);
  1147. luaL_unref(L, LUA_REGISTRYINDEX, db->trace_udata);
  1148. db->trace_udata = luaL_ref(L, LUA_REGISTRYINDEX);
  1149. db->trace_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  1150. /* set trace handler */
  1151. sqlite3_trace(db->db, db_trace_callback, db);
  1152. }
  1153. return 0;
  1154. }
  1155. #else
  1156. static int db_trace(lua_State *L) {
  1157. lua_pushliteral(L, "db_trace support disabled at compile time");
  1158. lua_error(L);
  1159. return 0;
  1160. }
  1161. #endif
  1162. #if !defined(LSQLITE_OMIT_UPDATE_HOOK) || !LSQLITE_OMIT_UPDATE_HOOK
  1163. /*
  1164. ** update_hook callback:
  1165. ** Params: database, callback function, userdata
  1166. **
  1167. ** callback function:
  1168. ** Params: userdata, {one of SQLITE_INSERT, SQLITE_DELETE, or SQLITE_UPDATE},
  1169. ** database name, table name (containing the affected row), rowid of the row
  1170. */
  1171. static void db_update_hook_callback(void *user, int op, char const *dbname, char const *tblname, sqlite3_int64 rowid) {
  1172. sdb *db = (sdb*)user;
  1173. lua_State *L = db->L;
  1174. int top = lua_gettop(L);
  1175. lua_Number n;
  1176. /* setup lua callback call */
  1177. lua_rawgeti(L, LUA_REGISTRYINDEX, db->update_hook_cb); /* get callback */
  1178. lua_rawgeti(L, LUA_REGISTRYINDEX, db->update_hook_udata); /* get callback user data */
  1179. lua_pushinteger(L, op);
  1180. lua_pushstring(L, dbname); /* update_hook database name */
  1181. lua_pushstring(L, tblname); /* update_hook database name */
  1182. PUSH_INT64(L, rowid, lua_pushfstring(L, "%ll", rowid));
  1183. /* call lua function */
  1184. lua_pcall(L, 5, 0, 0);
  1185. /* ignore any error generated by this function */
  1186. lua_settop(L, top);
  1187. }
  1188. static int db_update_hook(lua_State *L) {
  1189. sdb *db = lsqlite_checkdb(L, 1);
  1190. if (lua_gettop(L) < 2 || lua_isnil(L, 2)) {
  1191. luaL_unref(L, LUA_REGISTRYINDEX, db->update_hook_cb);
  1192. luaL_unref(L, LUA_REGISTRYINDEX, db->update_hook_udata);
  1193. db->update_hook_cb =
  1194. db->update_hook_udata = LUA_NOREF;
  1195. /* clear update_hook handler */
  1196. sqlite3_update_hook(db->db, NULL, NULL);
  1197. }
  1198. else {
  1199. luaL_checktype(L, 2, LUA_TFUNCTION);
  1200. /* make sure we have an userdata field (even if nil) */
  1201. lua_settop(L, 3);
  1202. luaL_unref(L, LUA_REGISTRYINDEX, db->update_hook_cb);
  1203. luaL_unref(L, LUA_REGISTRYINDEX, db->update_hook_udata);
  1204. db->update_hook_udata = luaL_ref(L, LUA_REGISTRYINDEX);
  1205. db->update_hook_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  1206. /* set update_hook handler */
  1207. sqlite3_update_hook(db->db, db_update_hook_callback, db);
  1208. }
  1209. return 0;
  1210. }
  1211. /*
  1212. ** commit_hook callback:
  1213. ** Params: database, callback function, userdata
  1214. **
  1215. ** callback function:
  1216. ** Params: userdata
  1217. ** Returned value: Return false or nil to continue the COMMIT operation normally.
  1218. ** return true (non false, non nil), then the COMMIT is converted into a ROLLBACK.
  1219. */
  1220. static int db_commit_hook_callback(void *user) {
  1221. sdb *db = (sdb*)user;
  1222. lua_State *L = db->L;
  1223. int top = lua_gettop(L);
  1224. int rollback = 0;
  1225. /* setup lua callback call */
  1226. lua_rawgeti(L, LUA_REGISTRYINDEX, db->commit_hook_cb); /* get callback */
  1227. lua_rawgeti(L, LUA_REGISTRYINDEX, db->commit_hook_udata); /* get callback user data */
  1228. /* call lua function */
  1229. if (!lua_pcall(L, 1, 1, 0))
  1230. rollback = lua_toboolean(L, -1); /* use result if there was no error */
  1231. lua_settop(L, top);
  1232. return rollback;
  1233. }
  1234. static int db_commit_hook(lua_State *L) {
  1235. sdb *db = lsqlite_checkdb(L, 1);
  1236. if (lua_gettop(L) < 2 || lua_isnil(L, 2)) {
  1237. luaL_unref(L, LUA_REGISTRYINDEX, db->commit_hook_cb);
  1238. luaL_unref(L, LUA_REGISTRYINDEX, db->commit_hook_udata);
  1239. db->commit_hook_cb =
  1240. db->commit_hook_udata = LUA_NOREF;
  1241. /* clear commit_hook handler */
  1242. sqlite3_commit_hook(db->db, NULL, NULL);
  1243. }
  1244. else {
  1245. luaL_checktype(L, 2, LUA_TFUNCTION);
  1246. /* make sure we have an userdata field (even if nil) */
  1247. lua_settop(L, 3);
  1248. luaL_unref(L, LUA_REGISTRYINDEX, db->commit_hook_cb);
  1249. luaL_unref(L, LUA_REGISTRYINDEX, db->commit_hook_udata);
  1250. db->commit_hook_udata = luaL_ref(L, LUA_REGISTRYINDEX);
  1251. db->commit_hook_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  1252. /* set commit_hook handler */
  1253. sqlite3_commit_hook(db->db, db_commit_hook_callback, db);
  1254. }
  1255. return 0;
  1256. }
  1257. /*
  1258. ** rollback hook callback:
  1259. ** Params: database, callback function, userdata
  1260. **
  1261. ** callback function:
  1262. ** Params: userdata
  1263. */
  1264. static void db_rollback_hook_callback(void *user) {
  1265. sdb *db = (sdb*)user;
  1266. lua_State *L = db->L;
  1267. int top = lua_gettop(L);
  1268. /* setup lua callback call */
  1269. lua_rawgeti(L, LUA_REGISTRYINDEX, db->rollback_hook_cb); /* get callback */
  1270. lua_rawgeti(L, LUA_REGISTRYINDEX, db->rollback_hook_udata); /* get callback user data */
  1271. /* call lua function */
  1272. lua_pcall(L, 1, 0, 0);
  1273. /* ignore any error generated by this function */
  1274. lua_settop(L, top);
  1275. }
  1276. static int db_rollback_hook(lua_State *L) {
  1277. sdb *db = lsqlite_checkdb(L, 1);
  1278. if (lua_gettop(L) < 2 || lua_isnil(L, 2)) {
  1279. luaL_unref(L, LUA_REGISTRYINDEX, db->rollback_hook_cb);
  1280. luaL_unref(L, LUA_REGISTRYINDEX, db->rollback_hook_udata);
  1281. db->rollback_hook_cb =
  1282. db->rollback_hook_udata = LUA_NOREF;
  1283. /* clear rollback_hook handler */
  1284. sqlite3_rollback_hook(db->db, NULL, NULL);
  1285. }
  1286. else {
  1287. luaL_checktype(L, 2, LUA_TFUNCTION);
  1288. /* make sure we have an userdata field (even if nil) */
  1289. lua_settop(L, 3);
  1290. luaL_unref(L, LUA_REGISTRYINDEX, db->rollback_hook_cb);
  1291. luaL_unref(L, LUA_REGISTRYINDEX, db->rollback_hook_udata);
  1292. db->rollback_hook_udata = luaL_ref(L, LUA_REGISTRYINDEX);
  1293. db->rollback_hook_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  1294. /* set rollback_hook handler */
  1295. sqlite3_rollback_hook(db->db, db_rollback_hook_callback, db);
  1296. }
  1297. return 0;
  1298. }
  1299. #endif /* #if !defined(LSQLITE_OMIT_UPDATE_HOOK) || !LSQLITE_OMIT_UPDATE_HOOK */
  1300. #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK
  1301. /*
  1302. ** progress handler:
  1303. ** Params: database, number of opcodes, callback function, userdata
  1304. **
  1305. ** callback function:
  1306. ** Params: userdata
  1307. ** returns: 0 to return immediatly and return SQLITE_ABORT, non-zero to continue
  1308. */
  1309. static int db_progress_callback(void *user) {
  1310. int result = 1; /* abort by default */
  1311. sdb *db = (sdb*)user;
  1312. lua_State *L = db->L;
  1313. int top = lua_gettop(L);
  1314. lua_rawgeti(L, LUA_REGISTRYINDEX, db->progress_cb);
  1315. lua_rawgeti(L, LUA_REGISTRYINDEX, db->progress_udata);
  1316. /* call lua function */
  1317. if (!lua_pcall(L, 1, 1, 0))
  1318. result = lua_toboolean(L, -1);
  1319. lua_settop(L, top);
  1320. return result;
  1321. }
  1322. static int db_progress_handler(lua_State *L) {
  1323. sdb *db = lsqlite_checkdb(L, 1);
  1324. if (lua_gettop(L) < 2 || lua_isnil(L, 2)) {
  1325. luaL_unref(L, LUA_REGISTRYINDEX, db->progress_cb);
  1326. luaL_unref(L, LUA_REGISTRYINDEX, db->progress_udata);
  1327. db->progress_cb =
  1328. db->progress_udata = LUA_NOREF;
  1329. /* clear busy handler */
  1330. sqlite3_progress_handler(db->db, 0, NULL, NULL);
  1331. }
  1332. else {
  1333. int nop = luaL_checkint(L, 2); /* number of opcodes */
  1334. luaL_checktype(L, 3, LUA_TFUNCTION);
  1335. /* make sure we have an userdata field (even if nil) */
  1336. lua_settop(L, 4);
  1337. luaL_unref(L, LUA_REGISTRYINDEX, db->progress_cb);
  1338. luaL_unref(L, LUA_REGISTRYINDEX, db->progress_udata);
  1339. db->progress_udata = luaL_ref(L, LUA_REGISTRYINDEX);
  1340. db->progress_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  1341. /* set progress callback */
  1342. sqlite3_progress_handler(db->db, nop, db_progress_callback, db);
  1343. }
  1344. return 0;
  1345. }
  1346. #else /* #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK */
  1347. static int db_progress_handler(lua_State *L) {
  1348. lua_pushliteral(L, "progress callback support disabled at compile time");
  1349. lua_error(L);
  1350. return 0;
  1351. }
  1352. #endif /* #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK */
  1353. /* Online Backup API */
  1354. #if 0
  1355. sqlite3_backup *sqlite3_backup_init(
  1356. sqlite3 *pDest, /* Destination database handle */
  1357. const char *zDestName, /* Destination database name */
  1358. sqlite3 *pSource, /* Source database handle */
  1359. const char *zSourceName /* Source database name */
  1360. );
  1361. int sqlite3_backup_step(sqlite3_backup *p, int nPage);
  1362. int sqlite3_backup_finish(sqlite3_backup *p);
  1363. int sqlite3_backup_remaining(sqlite3_backup *p);
  1364. int sqlite3_backup_pagecount(sqlite3_backup *p);
  1365. #endif
  1366. struct sdb_bu {
  1367. sqlite3_backup *bu; /* backup structure */
  1368. };
  1369. static int cleanupbu(lua_State *L, sdb_bu *sbu) {
  1370. if (!sbu->bu) return 0; /* already finished */
  1371. /* remove table from registry */
  1372. lua_pushlightuserdata(L, sbu->bu);
  1373. lua_pushnil(L);
  1374. lua_rawset(L, LUA_REGISTRYINDEX);
  1375. lua_pushinteger(L, sqlite3_backup_finish(sbu->bu));
  1376. sbu->bu = NULL;
  1377. return 1;
  1378. }
  1379. static int lsqlite_backup_init(lua_State *L) {
  1380. sdb *target_db = lsqlite_checkdb(L, 1);
  1381. const char *target_nm = luaL_checkstring(L, 2);
  1382. sdb *source_db = lsqlite_checkdb(L, 3);
  1383. const char *source_nm = luaL_checkstring(L, 4);
  1384. sqlite3_backup *bu = sqlite3_backup_init(target_db->db, target_nm, source_db->db, source_nm);
  1385. if (NULL != bu) {
  1386. sdb_bu *sbu = (sdb_bu*)lua_newuserdata(L, sizeof(sdb_bu));
  1387. luaL_getmetatable(L, sqlite_bu_meta);
  1388. lua_setmetatable(L, -2); /* set metatable */
  1389. sbu->bu = bu;
  1390. /* create table from registry */
  1391. /* to prevent referenced databases from being garbage collected while bu is live */
  1392. lua_pushlightuserdata(L, bu);
  1393. lua_createtable(L, 2, 0);
  1394. /* add source and target dbs to table at indices 1 and 2 */
  1395. lua_pushvalue(L, 1); /* target db */
  1396. lua_rawseti(L, -2, 1);
  1397. lua_pushvalue(L, 3); /* source db */
  1398. lua_rawseti(L, -2, 2);
  1399. /* put table in registry with key lightuserdata bu */
  1400. lua_rawset(L, LUA_REGISTRYINDEX);
  1401. return 1;
  1402. }
  1403. else {
  1404. return 0;
  1405. }
  1406. }
  1407. static sdb_bu *lsqlite_getbu(lua_State *L, int index) {
  1408. sdb_bu *sbu = (sdb_bu*)luaL_checkudata(L, index, sqlite_bu_meta);
  1409. if (sbu == NULL) luaL_typerror(L, index, "sqlite database backup");
  1410. return sbu;
  1411. }
  1412. static sdb_bu *lsqlite_checkbu(lua_State *L, int index) {
  1413. sdb_bu *sbu = lsqlite_getbu(L, index);
  1414. if (sbu->bu == NULL) luaL_argerror(L, index, "attempt to use closed sqlite database backup");
  1415. return sbu;
  1416. }
  1417. static int dbbu_gc(lua_State *L) {
  1418. sdb_bu *sbu = lsqlite_getbu(L, 1);
  1419. if (sbu->bu != NULL) {
  1420. cleanupbu(L, sbu);
  1421. lua_pop(L, 1);
  1422. }
  1423. /* else ignore if already finished */
  1424. return 0;
  1425. }
  1426. static int dbbu_step(lua_State *L) {
  1427. sdb_bu *sbu = lsqlite_checkbu(L, 1);
  1428. int nPage = luaL_checkint(L, 2);
  1429. lua_pushinteger(L, sqlite3_backup_step(sbu->bu, nPage));
  1430. return 1;
  1431. }
  1432. static int dbbu_remaining(lua_State *L) {
  1433. sdb_bu *sbu = lsqlite_checkbu(L, 1);
  1434. lua_pushinteger(L, sqlite3_backup_remaining(sbu->bu));
  1435. return 1;
  1436. }
  1437. static int dbbu_pagecount(lua_State *L) {
  1438. sdb_bu *sbu = lsqlite_checkbu(L, 1);
  1439. lua_pushinteger(L, sqlite3_backup_pagecount(sbu->bu));
  1440. return 1;
  1441. }
  1442. static int dbbu_finish(lua_State *L) {
  1443. sdb_bu *sbu = lsqlite_checkbu(L, 1);
  1444. return cleanupbu(L, sbu);
  1445. }
  1446. /* end of Online Backup API */
  1447. /*
  1448. ** busy handler:
  1449. ** Params: database, callback function, userdata
  1450. **
  1451. ** callback function:
  1452. ** Params: userdata, number of tries
  1453. ** returns: 0 to return immediatly and return SQLITE_BUSY, non-zero to try again
  1454. */
  1455. static int db_busy_callback(void *user, int tries) {
  1456. int retry = 0; /* abort by default */
  1457. sdb *db = (sdb*)user;
  1458. lua_State *L = db->L;
  1459. int top = lua_gettop(L);
  1460. lua_rawgeti(L, LUA_REGISTRYINDEX, db->busy_cb);
  1461. lua_rawgeti(L, LUA_REGISTRYINDEX, db->busy_udata);
  1462. lua_pushinteger(L, tries);
  1463. /* call lua function */
  1464. if (!lua_pcall(L, 2, 1, 0))
  1465. retry = lua_toboolean(L, -1);
  1466. lua_settop(L, top);
  1467. return retry;
  1468. }
  1469. static int db_busy_handler(lua_State *L) {
  1470. sdb *db = lsqlite_checkdb(L, 1);
  1471. if (lua_gettop(L) < 2 || lua_isnil(L, 2)) {
  1472. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_cb);
  1473. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_udata);
  1474. db->busy_cb =
  1475. db->busy_udata = LUA_NOREF;
  1476. /* clear busy handler */
  1477. sqlite3_busy_handler(db->db, NULL, NULL);
  1478. }
  1479. else {
  1480. luaL_checktype(L, 2, LUA_TFUNCTION);
  1481. /* make sure we have an userdata field (even if nil) */
  1482. lua_settop(L, 3);
  1483. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_cb);
  1484. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_udata);
  1485. db->busy_udata = luaL_ref(L, LUA_REGISTRYINDEX);
  1486. db->busy_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  1487. /* set busy handler */
  1488. sqlite3_busy_handler(db->db, db_busy_callback, db);
  1489. }
  1490. return 0;
  1491. }
  1492. static int db_busy_timeout(lua_State *L) {
  1493. sdb *db = lsqlite_checkdb(L, 1);
  1494. int timeout = luaL_checkint(L, 2);
  1495. sqlite3_busy_timeout(db->db, timeout);
  1496. /* if there was a timeout callback registered, it is now
  1497. ** invalid/useless. free any references we may have */
  1498. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_cb);
  1499. luaL_unref(L, LUA_REGISTRYINDEX, db->busy_udata);
  1500. db->busy_cb =
  1501. db->busy_udata = LUA_NOREF;
  1502. return 0;
  1503. }
  1504. /*
  1505. ** Params: db, sql, callback, user
  1506. ** returns: code [, errmsg]
  1507. **
  1508. ** Callback:
  1509. ** Params: user, number of columns, values, names
  1510. ** Returns: 0 to continue, other value will cause abort
  1511. */
  1512. static int db_exec_callback(void* user, int columns, char **data, char **names) {
  1513. int result = SQLITE_ABORT; /* abort by default */
  1514. lua_State *L = (lua_State*)user;
  1515. int n;
  1516. int top = lua_gettop(L);
  1517. lua_pushvalue(L, 3); /* function to call */
  1518. lua_pushvalue(L, 4); /* user data */
  1519. lua_pushinteger(L, columns); /* total number of rows in result */
  1520. /* column values */
  1521. lua_pushvalue(L, 6);
  1522. for (n = 0; n < columns;) {
  1523. lua_pushstring(L, data[n++]);
  1524. lua_rawseti(L, -2, n);
  1525. }
  1526. /* columns names */
  1527. lua_pushvalue(L, 5);
  1528. if (lua_isnil(L, -1)) {
  1529. lua_pop(L, 1);
  1530. lua_createtable(L, columns, 0);
  1531. lua_pushvalue(L, -1);
  1532. lua_replace(L, 5);
  1533. for (n = 0; n < columns;) {
  1534. lua_pushstring(L, names[n++]);
  1535. lua_rawseti(L, -2, n);
  1536. }
  1537. }
  1538. /* call lua function */
  1539. if (!lua_pcall(L, 4, 1, 0)) {
  1540. #if LUA_VERSION_NUM > 502
  1541. if (lua_isinteger(L, -1))
  1542. result = lua_tointeger(L, -1);
  1543. else
  1544. #endif
  1545. if (lua_isnumber(L, -1))
  1546. result = lua_tonumber(L, -1);
  1547. }
  1548. lua_settop(L, top);
  1549. return result;
  1550. }
  1551. static int db_exec(lua_State *L) {
  1552. sdb *db = lsqlite_checkdb(L, 1);
  1553. const char *sql = luaL_checkstring(L, 2);
  1554. int result;
  1555. if (!lua_isnoneornil(L, 3)) {
  1556. /* stack:
  1557. ** 3: callback function
  1558. ** 4: userdata
  1559. ** 5: column names
  1560. ** 6: reusable column values
  1561. */
  1562. luaL_checktype(L, 3, LUA_TFUNCTION);
  1563. lua_settop(L, 4); /* 'trap' userdata - nil extra parameters */
  1564. lua_pushnil(L); /* column names not known at this point */
  1565. lua_newtable(L); /* column values table */
  1566. result = sqlite3_exec(db->db, sql, db_exec_callback, L, NULL);
  1567. }
  1568. else {
  1569. /* no callbacks */
  1570. result = sqlite3_exec(db->db, sql, NULL, NULL, NULL);
  1571. }
  1572. lua_pushinteger(L, result);
  1573. return 1;
  1574. }
  1575. /*
  1576. ** Params: db, sql
  1577. ** returns: code, compiled length or error message
  1578. */
  1579. static int db_prepare(lua_State *L) {
  1580. sdb *db = lsqlite_checkdb(L, 1);
  1581. const char *sql = luaL_checkstring(L, 2);
  1582. int sql_len = lua_strlen(L, 2);
  1583. const char *sqltail;
  1584. sdb_vm *svm;
  1585. lua_settop(L,2); /* db,sql is on top of stack for call to newvm */
  1586. svm = newvm(L, db);
  1587. if (sqlite3_prepare_v2(db->db, sql, sql_len, &svm->vm, &sqltail) != SQLITE_OK) {
  1588. lua_pushnil(L);
  1589. lua_pushinteger(L, sqlite3_errcode(db->db));
  1590. if (cleanupvm(L, svm) == 1)
  1591. lua_pop(L, 1); /* this should not happen since sqlite3_prepare_v2 will not set ->vm on error */
  1592. return 2;
  1593. }
  1594. /* vm already in the stack */
  1595. lua_pushstring(L, sqltail);
  1596. return 2;
  1597. }
  1598. static int db_do_next_row(lua_State *L, int packed) {
  1599. int result;
  1600. sdb_vm *svm = lsqlite_checkvm(L, 1);
  1601. sqlite3_stmt *vm;
  1602. int columns;
  1603. int i;
  1604. result = stepvm(L, svm);
  1605. vm = svm->vm; /* stepvm may change svm->vm if re-prepare is needed */
  1606. svm->has_values = result == SQLITE_ROW ? 1 : 0;
  1607. svm->columns = columns = sqlite3_data_count(vm);
  1608. if (result == SQLITE_ROW) {
  1609. if (packed) {
  1610. if (packed == 1) {
  1611. lua_createtable(L, columns, 0);
  1612. for (i = 0; i < columns;) {
  1613. vm_push_column(L, vm, i);
  1614. lua_rawseti(L, -2, ++i);
  1615. }
  1616. }
  1617. else {
  1618. lua_createtable(L, 0, columns);
  1619. for (i = 0; i < columns; ++i) {
  1620. lua_pushstring(L, sqlite3_column_name(vm, i));
  1621. vm_push_column(L, vm, i);
  1622. lua_rawset(L, -3);
  1623. }
  1624. }
  1625. return 1;
  1626. }
  1627. else {
  1628. lua_checkstack(L, columns);
  1629. for (i = 0; i < columns; ++i)
  1630. vm_push_column(L, vm, i);
  1631. return svm->columns;
  1632. }
  1633. }
  1634. if (svm->temp) {
  1635. /* finalize and check for errors */
  1636. result = sqlite3_finalize(vm);
  1637. svm->vm = NULL;
  1638. cleanupvm(L, svm);
  1639. }
  1640. else if (result == SQLITE_DONE) {
  1641. result = sqlite3_reset(vm);
  1642. }
  1643. if (result != SQLITE_OK) {
  1644. lua_pushstring(L, sqlite3_errmsg(svm->db->db));
  1645. lua_error(L);
  1646. }
  1647. return 0;
  1648. }
  1649. static int db_next_row(lua_State *L) {
  1650. return db_do_next_row(L, 0);
  1651. }
  1652. static int db_next_packed_row(lua_State *L) {
  1653. return db_do_next_row(L, 1);
  1654. }
  1655. static int db_next_named_row(lua_State *L) {
  1656. return db_do_next_row(L, 2);
  1657. }
  1658. static int dbvm_do_rows(lua_State *L, int(*f)(lua_State *)) {
  1659. /* sdb_vm *svm = */
  1660. lsqlite_checkvm(L, 1);
  1661. lua_pushvalue(L,1);
  1662. lua_pushcfunction(L, f);
  1663. lua_insert(L, -2);
  1664. return 2;
  1665. }
  1666. static int dbvm_rows(lua_State *L) {
  1667. return dbvm_do_rows(L, db_next_packed_row);
  1668. }
  1669. static int dbvm_nrows(lua_State *L) {
  1670. return dbvm_do_rows(L, db_next_named_row);
  1671. }
  1672. static int dbvm_urows(lua_State *L) {
  1673. return dbvm_do_rows(L, db_next_row);
  1674. }
  1675. static int db_do_rows(lua_State *L, int(*f)(lua_State *)) {
  1676. sdb *db = lsqlite_checkdb(L, 1);
  1677. const char *sql = luaL_checkstring(L, 2);
  1678. sdb_vm *svm;
  1679. lua_settop(L,2); /* db,sql is on top of stack for call to newvm */
  1680. svm = newvm(L, db);
  1681. svm->temp = 1;
  1682. if (sqlite3_prepare_v2(db->db, sql, -1, &svm->vm, NULL) != SQLITE_OK) {
  1683. lua_pushstring(L, sqlite3_errmsg(svm->db->db));
  1684. if (cleanupvm(L, svm) == 1)
  1685. lua_pop(L, 1); /* this should not happen since sqlite3_prepare_v2 will not set ->vm on error */
  1686. lua_error(L);
  1687. }
  1688. lua_pushcfunction(L, f);
  1689. lua_insert(L, -2);
  1690. return 2;
  1691. }
  1692. static int db_rows(lua_State *L) {
  1693. return db_do_rows(L, db_next_packed_row);
  1694. }
  1695. static int db_nrows(lua_State *L) {
  1696. return db_do_rows(L, db_next_named_row);
  1697. }
  1698. /* unpacked version of db:rows */
  1699. static int db_urows(lua_State *L) {
  1700. return db_do_rows(L, db_next_row);
  1701. }
  1702. static int db_tostring(lua_State *L) {
  1703. char buff[32];
  1704. sdb *db = lsqlite_getdb(L, 1);
  1705. if (db->db == NULL)
  1706. strcpy(buff, "closed");
  1707. else
  1708. c_sprintf(buff, "%p", lua_touserdata(L, 1));
  1709. lua_pushfstring(L, "sqlite database (%s)", buff);
  1710. return 1;
  1711. }
  1712. static int db_close(lua_State *L) {
  1713. sdb *db = lsqlite_checkdb(L, 1);
  1714. lua_pushinteger(L, cleanupdb(L, db));
  1715. return 1;
  1716. }
  1717. static int db_close_vm(lua_State *L) {
  1718. sdb *db = lsqlite_checkdb(L, 1);
  1719. /* cleanup temporary only tables? */
  1720. int temp = lua_toboolean(L, 2);
  1721. /* free associated virtual machines */
  1722. lua_pushlightuserdata(L, db);
  1723. lua_rawget(L, LUA_REGISTRYINDEX);
  1724. /* close all used handles */
  1725. lua_pushnil(L);
  1726. while (lua_next(L, -2)) {
  1727. sdb_vm *svm = lua_touserdata(L, -2); /* key: vm; val: sql text */
  1728. if ((!temp || svm->temp) && svm->vm)
  1729. {
  1730. sqlite3_finalize(svm->vm);
  1731. svm->vm = NULL;
  1732. }
  1733. /* leave key in the stack */
  1734. lua_pop(L, 1);
  1735. }
  1736. return 0;
  1737. }
  1738. /* From: Wolfgang Oertl
  1739. When using lsqlite3 in a multithreaded environment, each thread has a separate Lua
  1740. environment, but full userdata structures can't be passed from one thread to another.
  1741. This is possible with lightuserdata, however. See: lsqlite_open_ptr().
  1742. */
  1743. static int db_get_ptr(lua_State *L) {
  1744. sdb *db = lsqlite_checkdb(L, 1);
  1745. lua_pushlightuserdata(L, db->db);
  1746. return 1;
  1747. }
  1748. static int db_gc(lua_State *L) {
  1749. sdb *db = lsqlite_getdb(L, 1);
  1750. if (db->db != NULL) /* ignore closed databases */
  1751. cleanupdb(L, db);
  1752. return 0;
  1753. }
  1754. /*
  1755. ** =======================================================
  1756. ** General library functions
  1757. ** =======================================================
  1758. */
  1759. static int lsqlite_version(lua_State *L) {
  1760. lua_pushstring(L, sqlite3_libversion());
  1761. return 1;
  1762. }
  1763. static int lsqlite_complete(lua_State *L) {
  1764. const char *sql = luaL_checkstring(L, 1);
  1765. lua_pushboolean(L, sqlite3_complete(sql));
  1766. return 1;
  1767. }
  1768. #if 0
  1769. static int lsqlite_complete(lua_State *L) {
  1770. lua_pushliteral(L, "sql complete support disabled at compile time");
  1771. lua_error(L);
  1772. return 0;
  1773. }
  1774. #endif
  1775. #ifndef _WIN32
  1776. static int lsqlite_temp_directory(lua_State *L) {
  1777. const char *oldtemp = sqlite3_temp_directory;
  1778. if (!lua_isnone(L, 1)) {
  1779. const char *temp = luaL_optstring(L, 1, NULL);
  1780. if (sqlite3_temp_directory) {
  1781. sqlite3_free((char*)sqlite3_temp_directory);
  1782. }
  1783. if (temp) {
  1784. sqlite3_temp_directory = sqlite3_mprintf("%s", temp);
  1785. }
  1786. else {
  1787. sqlite3_temp_directory = NULL;
  1788. }
  1789. }
  1790. lua_pushstring(L, oldtemp);
  1791. return 1;
  1792. }
  1793. #endif
  1794. static int lsqlite_do_open(lua_State *L, const char *filename, int flags) {
  1795. sdb *db = newdb(L); /* create and leave in stack */
  1796. if (SQLITE3_OPEN(filename, &db->db, flags) == SQLITE_OK) {
  1797. /* database handle already in the stack - return it */
  1798. return 1;
  1799. }
  1800. /* failed to open database */
  1801. lua_pushnil(L); /* push nil */
  1802. lua_pushinteger(L, sqlite3_errcode(db->db));
  1803. lua_pushstring(L, sqlite3_errmsg(db->db)); /* push error message */
  1804. /* clean things up */
  1805. cleanupdb(L, db);
  1806. /* return */
  1807. return 3;
  1808. }
  1809. static int lsqlite_open(lua_State *L) {
  1810. const char *filename = luaL_checkstring(L, 1);
  1811. int flags = luaL_optinteger(L, 2, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
  1812. return lsqlite_do_open(L, filename, flags);
  1813. }
  1814. static int lsqlite_open_memory(lua_State *L) {
  1815. return lsqlite_do_open(L, ":memory:", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
  1816. }
  1817. /* From: Wolfgang Oertl
  1818. When using lsqlite3 in a multithreaded environment, each thread has a separate Lua
  1819. environment, but full userdata structures can't be passed from one thread to another.
  1820. This is possible with lightuserdata, however. See: db_get_ptr().
  1821. */
  1822. static int lsqlite_open_ptr(lua_State *L) {
  1823. sqlite3 *db_ptr;
  1824. sdb *db;
  1825. int rc;
  1826. luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
  1827. db_ptr = lua_touserdata(L, 1);
  1828. /* This is the only API function that runs sqlite3SafetyCheck regardless of
  1829. * SQLITE_ENABLE_API_ARMOR and does almost nothing (without an SQL
  1830. * statement) */
  1831. rc = sqlite3_exec(db_ptr, NULL, NULL, NULL, NULL);
  1832. if (rc != SQLITE_OK)
  1833. luaL_argerror(L, 1, "not a valid SQLite3 pointer");
  1834. db = newdb(L); /* create and leave in stack */
  1835. db->db = db_ptr;
  1836. return 1;
  1837. }
  1838. #ifndef LSQLITE_VERSION
  1839. /* should be defined in rockspec, but just in case... */
  1840. #define LSQLITE_VERSION "unknown"
  1841. #endif
  1842. /* Version number of this library
  1843. */
  1844. static int lsqlite_lversion(lua_State *L) {
  1845. lua_pushstring(L, LSQLITE_VERSION);
  1846. return 1;
  1847. }
  1848. /*
  1849. ** =======================================================
  1850. ** Register functions
  1851. ** =======================================================
  1852. */
  1853. /* ======================================================= */
  1854. static const LUA_REG_TYPE dblib[] = {
  1855. { LSTRKEY( "isopen" ), LFUNCVAL( db_isopen ) },
  1856. { LSTRKEY( "last_insert_rowid" ), LFUNCVAL ( db_last_insert_rowid ) },
  1857. { LSTRKEY( "changes" ), LFUNCVAL ( db_changes ) },
  1858. { LSTRKEY( "total_changes" ), LFUNCVAL ( db_total_changes ) },
  1859. { LSTRKEY( "errcode" ), LFUNCVAL ( db_errcode ) },
  1860. { LSTRKEY( "error_code" ), LFUNCVAL ( db_errcode ) },
  1861. { LSTRKEY( "errmsg" ), LFUNCVAL ( db_errmsg ) },
  1862. { LSTRKEY( "error_message" ), LFUNCVAL ( db_errmsg ) },
  1863. { LSTRKEY( "interrupt" ), LFUNCVAL ( db_interrupt ) },
  1864. { LSTRKEY( "db_filename" ), LFUNCVAL ( db_db_filename ) },
  1865. { LSTRKEY( "create_function" ), LFUNCVAL ( db_create_function ) },
  1866. { LSTRKEY( "create_aggregate" ), LFUNCVAL ( db_create_aggregate ) },
  1867. { LSTRKEY( "create_collation" ), LFUNCVAL ( db_create_collation ) },
  1868. { LSTRKEY( "load_extension" ), LFUNCVAL ( db_load_extension ) },
  1869. { LSTRKEY( "trace" ), LFUNCVAL ( db_trace ) },
  1870. { LSTRKEY( "progress_handler" ), LFUNCVAL ( db_progress_handler ) },
  1871. { LSTRKEY( "busy_timeout" ), LFUNCVAL ( db_busy_timeout ) },
  1872. { LSTRKEY( "busy_handler" ), LFUNCVAL ( db_busy_handler ) },
  1873. #if !defined(LSQLITE_OMIT_UPDATE_HOOK) || !LSQLITE_OMIT_UPDATE_HOOK
  1874. { LSTRKEY( "update_hook" ), LFUNCVAL ( db_update_hook ) },
  1875. { LSTRKEY( "commit_hook" ), LFUNCVAL ( db_commit_hook ) },
  1876. { LSTRKEY( "rollback_hook" ), LFUNCVAL ( db_rollback_hook ) },
  1877. #endif
  1878. { LSTRKEY( "prepare" ), LFUNCVAL ( db_prepare ) },
  1879. { LSTRKEY( "rows" ), LFUNCVAL ( db_rows ) },
  1880. { LSTRKEY( "urows" ), LFUNCVAL ( db_urows ) },
  1881. { LSTRKEY( "nrows" ), LFUNCVAL ( db_nrows ) },
  1882. { LSTRKEY( "exec" ), LFUNCVAL ( db_exec ) },
  1883. { LSTRKEY( "execute" ), LFUNCVAL ( db_exec ) },
  1884. { LSTRKEY( "close" ), LFUNCVAL ( db_close ) },
  1885. { LSTRKEY( "close_vm" ), LFUNCVAL ( db_close_vm ) },
  1886. { LSTRKEY( "get_ptr" ), LFUNCVAL ( db_get_ptr ) },
  1887. { LSTRKEY( "__tostring" ), LFUNCVAL ( db_tostring ) },
  1888. { LSTRKEY( "__gc" ), LFUNCVAL ( db_gc ) },
  1889. { LSTRKEY( "__index" ), LROVAL ( dblib ) },
  1890. { LNILKEY, LNILVAL }
  1891. };
  1892. static const LUA_REG_TYPE vmlib[] = {
  1893. { LSTRKEY( "isopen" ), LFUNCVAL ( dbvm_isopen ) },
  1894. { LSTRKEY( "step" ), LFUNCVAL ( dbvm_step ) },
  1895. { LSTRKEY( "reset" ), LFUNCVAL ( dbvm_reset ) },
  1896. { LSTRKEY( "finalize" ), LFUNCVAL ( dbvm_finalize ) },
  1897. { LSTRKEY( "columns" ), LFUNCVAL ( dbvm_columns ) },
  1898. { LSTRKEY( "bind" ), LFUNCVAL ( dbvm_bind ) },
  1899. { LSTRKEY( "bind_values" ), LFUNCVAL ( dbvm_bind_values ) },
  1900. { LSTRKEY( "bind_names" ), LFUNCVAL ( dbvm_bind_names ) },
  1901. { LSTRKEY( "bind_blob" ), LFUNCVAL ( dbvm_bind_blob ) },
  1902. { LSTRKEY( "bind_parameter_count" ),LFUNCVAL ( dbvm_bind_parameter_count )},
  1903. { LSTRKEY( "bind_parameter_name" ), LFUNCVAL ( dbvm_bind_parameter_name )},
  1904. { LSTRKEY( "get_value" ), LFUNCVAL ( dbvm_get_value ) },
  1905. { LSTRKEY( "get_values" ), LFUNCVAL ( dbvm_get_values ) },
  1906. { LSTRKEY( "get_name" ), LFUNCVAL ( dbvm_get_name ) },
  1907. { LSTRKEY( "get_names" ), LFUNCVAL ( dbvm_get_names ) },
  1908. { LSTRKEY( "get_type" ), LFUNCVAL ( dbvm_get_type ) },
  1909. { LSTRKEY( "get_types" ), LFUNCVAL ( dbvm_get_types ) },
  1910. { LSTRKEY( "get_uvalues" ), LFUNCVAL ( dbvm_get_uvalues ) },
  1911. { LSTRKEY( "get_unames" ), LFUNCVAL ( dbvm_get_unames ) },
  1912. { LSTRKEY( "get_utypes" ), LFUNCVAL ( dbvm_get_utypes ) },
  1913. { LSTRKEY( "get_named_values" ), LFUNCVAL ( dbvm_get_named_values ) },
  1914. { LSTRKEY( "get_named_types" ), LFUNCVAL ( dbvm_get_named_types ) },
  1915. { LSTRKEY( "rows" ), LFUNCVAL ( dbvm_rows ) },
  1916. { LSTRKEY( "urows" ), LFUNCVAL ( dbvm_urows ) },
  1917. { LSTRKEY( "nrows" ), LFUNCVAL ( dbvm_nrows ) },
  1918. { LSTRKEY( "last_insert_rowid" ), LFUNCVAL ( dbvm_last_insert_rowid ) },
  1919. /* compatibility names (added by request) */
  1920. { LSTRKEY( "idata" ), LFUNCVAL ( dbvm_get_values ) },
  1921. { LSTRKEY( "inames" ), LFUNCVAL ( dbvm_get_names ) },
  1922. { LSTRKEY( "itypes" ), LFUNCVAL ( dbvm_get_types ) },
  1923. { LSTRKEY( "data" ), LFUNCVAL ( dbvm_get_named_values ) },
  1924. { LSTRKEY( "type" ), LFUNCVAL ( dbvm_get_named_types ) },
  1925. { LSTRKEY( "__tostring" ), LFUNCVAL ( dbvm_tostring ) },
  1926. { LSTRKEY( "__gc" ), LFUNCVAL ( dbvm_gc ) },
  1927. { LSTRKEY( "__index" ), LROVAL ( vmlib ) },
  1928. { LNILKEY, LNILVAL }
  1929. };
  1930. static const LUA_REG_TYPE ctxlib[] = {
  1931. { LSTRKEY( "user_data" ), LFUNCVAL ( lcontext_user_data ) },
  1932. { LSTRKEY( "get_aggregate_data" ), LFUNCVAL ( lcontext_get_aggregate_context ) },
  1933. { LSTRKEY( "set_aggregate_data" ), LFUNCVAL ( lcontext_set_aggregate_context ) },
  1934. { LSTRKEY( "aggregate_count" ), LFUNCVAL ( lcontext_aggregate_count ) },
  1935. { LSTRKEY( "result" ), LFUNCVAL ( lcontext_result ) },
  1936. { LSTRKEY( "result_null" ), LFUNCVAL ( lcontext_result_null ) },
  1937. { LSTRKEY( "result_number" ), LFUNCVAL ( lcontext_result_double ) },
  1938. { LSTRKEY( "result_double" ), LFUNCVAL ( lcontext_result_double ) },
  1939. { LSTRKEY( "result_int" ), LFUNCVAL ( lcontext_result_int ) },
  1940. { LSTRKEY( "result_text" ), LFUNCVAL ( lcontext_result_text ) },
  1941. { LSTRKEY( "result_blob" ), LFUNCVAL ( lcontext_result_blob ) },
  1942. { LSTRKEY( "result_error" ), LFUNCVAL ( lcontext_result_error ) },
  1943. { LSTRKEY( "__tostring" ), LFUNCVAL ( lcontext_tostring ) },
  1944. { LSTRKEY( "__index" ), LROVAL ( ctxlib ) },
  1945. { LNILKEY, LNILVAL }
  1946. };
  1947. static const LUA_REG_TYPE dbbulib[] = {
  1948. { LSTRKEY( "step" ), LFUNCVAL ( dbbu_step ) },
  1949. { LSTRKEY( "remaining" ), LFUNCVAL ( dbbu_remaining ) },
  1950. { LSTRKEY( "pagecount" ), LFUNCVAL ( dbbu_pagecount ) },
  1951. { LSTRKEY( "finish" ), LFUNCVAL ( dbbu_finish ) },
  1952. // { LSTRKEY( "__tostring" ), LFUNCVAL ( dbbu_tostring ) },
  1953. { LSTRKEY( "__gc" ), LFUNCVAL ( dbbu_gc ) },
  1954. { LSTRKEY( "__index" ), LROVAL ( dbbulib ) },
  1955. { LNILKEY, LNILVAL }
  1956. };
  1957. #define SC(s) { LSTRKEY( #s ), LNUMVAL ( SQLITE_ ## s ) },
  1958. #define LSC(s) { LSTRKEY( #s ), LNUMVAL ( LSQLITE_ ## s ) },
  1959. static const LUA_REG_TYPE sqlitelib[] = {
  1960. { LSTRKEY( "lversion" ), LFUNCVAL ( lsqlite_lversion ) },
  1961. { LSTRKEY( "version" ), LFUNCVAL ( lsqlite_version ) },
  1962. { LSTRKEY( "complete" ), LFUNCVAL ( lsqlite_complete ) },
  1963. #ifndef _WIN32
  1964. { LSTRKEY( "temp_directory" ), LFUNCVAL ( lsqlite_temp_directory ) },
  1965. #endif
  1966. { LSTRKEY( "open" ), LFUNCVAL ( lsqlite_open ) },
  1967. { LSTRKEY( "open_memory" ), LFUNCVAL ( lsqlite_open_memory ) },
  1968. { LSTRKEY( "open_ptr" ), LFUNCVAL ( lsqlite_open_ptr ) },
  1969. { LSTRKEY( "backup_init" ), LFUNCVAL ( lsqlite_backup_init ) },
  1970. SC(OK) SC(ERROR) SC(INTERNAL) SC(PERM)
  1971. SC(ABORT) SC(BUSY) SC(LOCKED) SC(NOMEM)
  1972. SC(READONLY) SC(INTERRUPT) SC(IOERR) SC(CORRUPT)
  1973. SC(NOTFOUND) SC(FULL) SC(CANTOPEN) SC(PROTOCOL)
  1974. SC(EMPTY) SC(SCHEMA) SC(TOOBIG) SC(CONSTRAINT)
  1975. SC(MISMATCH) SC(MISUSE) SC(NOLFS)
  1976. SC(FORMAT) SC(NOTADB)
  1977. /* sqlite_step specific return values */
  1978. SC(RANGE) SC(ROW) SC(DONE)
  1979. /* column types */
  1980. SC(INTEGER) SC(FLOAT) SC(TEXT) SC(BLOB)
  1981. SC(NULL)
  1982. /* Authorizer Action Codes */
  1983. SC(CREATE_INDEX )
  1984. SC(CREATE_TABLE )
  1985. SC(CREATE_TEMP_INDEX )
  1986. SC(CREATE_TEMP_TABLE )
  1987. SC(CREATE_TEMP_TRIGGER)
  1988. SC(CREATE_TEMP_VIEW )
  1989. SC(CREATE_TRIGGER )
  1990. SC(CREATE_VIEW )
  1991. SC(DELETE )
  1992. SC(DROP_INDEX )
  1993. SC(DROP_TABLE )
  1994. SC(DROP_TEMP_INDEX )
  1995. SC(DROP_TEMP_TABLE )
  1996. SC(DROP_TEMP_TRIGGER )
  1997. SC(DROP_TEMP_VIEW )
  1998. SC(DROP_TRIGGER )
  1999. SC(DROP_VIEW )
  2000. SC(INSERT )
  2001. SC(PRAGMA )
  2002. SC(READ )
  2003. SC(SELECT )
  2004. SC(TRANSACTION )
  2005. SC(UPDATE )
  2006. SC(ATTACH )
  2007. SC(DETACH )
  2008. SC(ALTER_TABLE )
  2009. SC(REINDEX )
  2010. SC(ANALYZE )
  2011. SC(CREATE_VTABLE )
  2012. SC(DROP_VTABLE )
  2013. SC(FUNCTION )
  2014. SC(SAVEPOINT )
  2015. /* file open flags */
  2016. SC(OPEN_READONLY)
  2017. SC(OPEN_READWRITE)
  2018. SC(OPEN_CREATE)
  2019. SC(OPEN_URI)
  2020. SC(OPEN_MEMORY)
  2021. SC(OPEN_NOMUTEX)
  2022. SC(OPEN_FULLMUTEX)
  2023. SC(OPEN_SHAREDCACHE)
  2024. SC(OPEN_PRIVATECACHE)
  2025. { LNILKEY, LNILVAL }
  2026. };
  2027. LUALIB_API int luaopen_lsqlite3(lua_State *L) {
  2028. luaL_rometatable(L, sqlite_meta, (void *)dblib);
  2029. luaL_rometatable(L, sqlite_vm_meta, (void *)vmlib);
  2030. luaL_rometatable(L, sqlite_bu_meta, (void *)dbbulib);
  2031. luaL_rometatable(L, sqlite_ctx_meta, (void *)ctxlib);
  2032. return 1;
  2033. }
  2034. NODEMCU_MODULE(SQLITE3, "sqlite3", sqlitelib, luaopen_lsqlite3);