sqlite3.c 73 KB

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