sq_sqlite3.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338
  1. /* Code generated by script */
  2. #include "squirrel.h"
  3. #include "sqlite3.h"
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include "sqstdblobimpl.h"
  7. typedef struct sq_sqlite3_sdb sq_sqlite3_sdb;
  8. typedef struct sq_sqlite3_sdb_func sq_sqlite3_sdb_func;
  9. /* to use as C user data so i know what function sqlite is calling */
  10. struct sq_sqlite3_sdb_func
  11. {
  12. /* references to associated lua values */
  13. HSQOBJECT fn_step;
  14. HSQOBJECT fn_finalize;
  15. HSQOBJECT udata;
  16. sq_sqlite3_sdb *sdb;
  17. sq_sqlite3_sdb_func *next;
  18. };
  19. /* information about database */
  20. struct sq_sqlite3_sdb
  21. {
  22. /* associated squirrel vm */
  23. HSQUIRRELVM v;
  24. /* sqlite database handle */
  25. sqlite3 *db;
  26. /* sql functions stack usage */
  27. sq_sqlite3_sdb_func *func; /* top SQL function being called */
  28. /* references */
  29. HSQOBJECT busy_cb; /* busy callback */
  30. HSQOBJECT busy_udata;
  31. HSQOBJECT progress_cb; /* progress handler */
  32. HSQOBJECT progress_udata;
  33. HSQOBJECT trace_cb; /* trace callback */
  34. HSQOBJECT trace_udata;
  35. HSQOBJECT null_value;
  36. };
  37. SQ_OPT_STRING_STRLEN();
  38. static const SQChar *SQLite3_TAG = "SQLite3";
  39. static const SQChar *SQLite3_Stmt_TAG = "SQLite3Stmt";
  40. static const SQChar sqlite3_NULL_Name[] = _SC("sqlite3_NULL");
  41. static const SQChar nullName[] = _SC("Null");
  42. static SQRESULT sqlite3_NULL_tostring(HSQUIRRELVM v)
  43. {
  44. sq_pushstring(v, "", 0);
  45. return 1;
  46. }
  47. SQRegFunction sqlite3_NULL_methods[]=
  48. {
  49. {_SC("_tostring"),sqlite3_NULL_tostring,1, _SC(".")},
  50. {0,0}
  51. };
  52. static SQRESULT get_sqlite3_instance(HSQUIRRELVM v, SQInteger idx, sq_sqlite3_sdb **sdb)
  53. {
  54. SQRESULT _rc_;
  55. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)sdb,(void*)SQLite3_TAG)) < 0) return _rc_;
  56. if(!*sdb) return sq_throwerror(v, _SC("database is closed"));
  57. return _rc_;
  58. }
  59. static SQRESULT get_sqlite3_stmt_instance(HSQUIRRELVM v, SQInteger idx, sqlite3_stmt **stmt)
  60. {
  61. SQRESULT _rc_;
  62. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)stmt,(void*)SQLite3_Stmt_TAG)) < 0) return _rc_;
  63. if(!*stmt) return sq_throwerror(v, _SC("statement is closed"));
  64. return _rc_;
  65. }
  66. //#define push_sqlite3_null(v) sq_getonregistrytable(v, sqlite3_NULL_Name, sizeof(sqlite3_NULL_Name)-1);
  67. //#define push_sqlite3_null(v) sq_pushobject(v, sqlite3_NULL);
  68. //#define push_sqlite3_null(v) sq_getbyname(v, 1, nullName, sizeof(nullName)-1)
  69. //#define push_sqlite3_null(v) sq_pushfromregistrytable(v, nullName, sizeof(nullName)-1)
  70. //#define push_sqlite3_null(v) {sq_pushregistrytable(v);sq_pushstring(v, sqlite3_NULL_Name, sizeof(sqlite3_NULL_Name)-1);sq_get(v, -2);sq_remove(v,-2);}
  71. //the line bellow is the fastest one, any other attempt till now was slower
  72. #define push_sqlite3_null(v) {sq_pushstring(v, nullName, sizeof(nullName)-1);sq_get(v, 1);}
  73. #define GET_sqlite3_INSTANCE_AT(idx) \
  74. sq_sqlite3_sdb *sdb=NULL; \
  75. if((_rc_ = get_sqlite3_instance(v,idx,&sdb)) < 0) return _rc_;\
  76. sqlite3 *self = sdb->db;
  77. #define GET_sqlite3_INSTANCE() GET_sqlite3_INSTANCE_AT(1)
  78. //#define GET_sqlite3_INSTANCE() SQ_GET_INSTANCE(v, 1, sqlite3, SQLite3_TAG)
  79. #define GET_sqlite3_stmt_INSTANCE() \
  80. sqlite3_stmt *self=NULL; \
  81. if((_rc_ = get_sqlite3_stmt_instance(v,1,&self)) < 0) return _rc_;
  82. enum e_type_result {tr_first_row_first_col, tr_first_row, tr_all_rows, tr_ddml};
  83. #define AS_STRING_ALWAYS 0x01
  84. #define NULL_AS_EMPTY_STR 0x02
  85. #define WITH_COL_NAMES 0x04
  86. static void sqlite3_stmt_push_string(HSQUIRRELVM v, sqlite3_stmt *stmt, int col, int flags)
  87. {
  88. const char *value = (const char*) sqlite3_column_text(stmt, col);
  89. if(value) sq_pushstring(v, value, sqlite3_column_bytes(stmt, col));
  90. else push_sqlite3_null(v);
  91. }
  92. static SQRESULT sqlite3_stmt_push_value(HSQUIRRELVM v, sqlite3_stmt *stmt, int col, int flags)
  93. {
  94. const char *value;
  95. if(flags & AS_STRING_ALWAYS) sqlite3_stmt_push_string(v, stmt, col, flags);
  96. else
  97. {
  98. switch (sqlite3_column_type(stmt, col))
  99. {
  100. case SQLITE_INTEGER:
  101. {
  102. sqlite_int64 i64 = sqlite3_column_int64(stmt, col);
  103. SQInteger n = (SQInteger)i64;
  104. if (n == i64)
  105. sq_pushinteger(v, n);
  106. else
  107. sq_pushstring(v, (const char*) sqlite3_column_text(stmt, col), sqlite3_column_bytes(stmt, col));
  108. }
  109. break;
  110. case SQLITE_FLOAT:
  111. sq_pushfloat(v, sqlite3_column_double(stmt, col));
  112. break;
  113. case SQLITE_TEXT:
  114. sqlite3_stmt_push_string(v, stmt, col, flags);
  115. break;
  116. case SQLITE_BLOB:
  117. value = (const char*) sqlite3_column_blob(stmt, col);
  118. if(value) sq_pushstring(v, value, sqlite3_column_bytes(stmt, col));
  119. else push_sqlite3_null(v);
  120. break;
  121. case SQLITE_NULL:
  122. push_sqlite3_null(v);
  123. break;
  124. default:
  125. push_sqlite3_null(v);
  126. break;
  127. }
  128. }
  129. return 1;
  130. }
  131. static void sqlite3_stmt_row_asArray(HSQUIRRELVM v, sqlite3_stmt *stmt, int flags)
  132. {
  133. int col_count = sqlite3_column_count(stmt);
  134. sq_newarray(v, col_count);
  135. for(int i=0; i<col_count; ++i)
  136. {
  137. sqlite3_stmt_push_value(v, stmt, i, flags);
  138. sq_arrayset(v, -2, i);
  139. }
  140. }
  141. static void sqlite3_stmt_row_asTable(HSQUIRRELVM v, sqlite3_stmt *stmt, int flags)
  142. {
  143. int col_count = sqlite3_column_count(stmt);
  144. sq_newtable(v);
  145. for(int i=0; i<col_count; ++i)
  146. {
  147. sq_pushstring(v, sqlite3_column_name(stmt, i), -1);
  148. sqlite3_stmt_push_value(v, stmt, i, flags);
  149. sq_rawset(v, -3);
  150. }
  151. }
  152. static void sqlite3_stmt_asArrayOfArrays(HSQUIRRELVM v, sqlite3_stmt *stmt, int flags)
  153. {
  154. sq_newarray(v, 0);
  155. if(flags & WITH_COL_NAMES)
  156. {
  157. int col_count = sqlite3_column_count(stmt);
  158. sq_newarray(v, col_count);
  159. for(int i=0; i<col_count; ++i)
  160. {
  161. sq_pushstring(v, sqlite3_column_name(stmt, i), -1);
  162. sq_arrayset(v, -2, i);
  163. }
  164. sq_arrayappend(v, -2);
  165. }
  166. while(sqlite3_step(stmt) == SQLITE_ROW)
  167. {
  168. sqlite3_stmt_row_asArray(v, stmt, flags);
  169. sq_arrayappend(v, -2);
  170. }
  171. }
  172. static void sqlite3_stmt_asArrayOfTables(HSQUIRRELVM v, sqlite3_stmt *stmt, int flags)
  173. {
  174. sq_newarray(v, 0);
  175. while(sqlite3_step(stmt) == SQLITE_ROW)
  176. {
  177. sqlite3_stmt_row_asTable(v, stmt, flags);
  178. sq_arrayappend(v, -2);
  179. }
  180. }
  181. static SQRESULT sqlite3_stmt_bind_value(HSQUIRRELVM v, sqlite3_stmt *stmt, int npar, int argn)
  182. {
  183. int _rc_ = 0;
  184. SQObjectType ptype = sq_gettype(v, argn);
  185. switch(ptype)
  186. {
  187. case OT_BOOL:
  188. SQ_GET_BOOL(v, argn, param_bool);
  189. _rc_ = sqlite3_bind_int(stmt, npar, param_bool == SQTrue ? 1 : 0);
  190. break;
  191. case OT_INTEGER:
  192. SQ_GET_INTEGER(v, argn, param_integer);
  193. _rc_ = sqlite3_bind_int64(stmt, npar, param_integer);
  194. break;
  195. case OT_FLOAT:
  196. SQ_GET_FLOAT(v, argn, param_float);
  197. _rc_ = sqlite3_bind_double(stmt, npar, param_float);
  198. break;
  199. case OT_STRING:
  200. {
  201. SQ_GET_STRING(v, argn, param_string);
  202. _rc_ = sqlite3_bind_text(stmt, npar, param_string, param_string_size, SQLITE_TRANSIENT);
  203. }
  204. break;
  205. case OT_NULL:
  206. _rc_ = sqlite3_bind_null(stmt, npar);
  207. break;
  208. default:
  209. return sq_throwerror(v, "Invalid bind parameter %d", npar);
  210. }
  211. if(_rc_ != SQLITE_OK)
  212. {
  213. sqlite3 *db = sqlite3_db_handle(stmt);
  214. return sq_throwerror(v, sqlite3_errmsg(db));
  215. }
  216. return SQ_OK;
  217. }
  218. static SQRESULT sq_sqlite3_stmt_prepare_aux(HSQUIRRELVM v, sqlite3 *db, sqlite3_stmt **stmt, int params_start)
  219. {
  220. SQ_FUNC_VARS(v);
  221. SQ_GET_STRING(v, params_start, szSQL);
  222. if(sqlite3_prepare_v2(db, szSQL, szSQL_size, stmt, 0) != SQLITE_OK)
  223. {
  224. return sq_throwerror(v, sqlite3_errmsg(db));
  225. }
  226. if(_top_ > params_start)
  227. {
  228. int nparams = sqlite3_bind_parameter_count(*stmt);
  229. int nparams_given = _top_-params_start;
  230. if(nparams != nparams_given)
  231. {
  232. return sq_throwerror(v, "expect %d parameters but only %d given", nparams, nparams_given);
  233. }
  234. for(int i=1; i <= nparams; ++i)
  235. {
  236. int argn = i+params_start;
  237. _rc_ = sqlite3_stmt_bind_value(v, *stmt, i, argn);
  238. if(_rc_ < 0) return _rc_;
  239. }
  240. }
  241. return SQ_OK;
  242. }
  243. static SQRESULT sq_sqlite3_stmt_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  244. {
  245. sqlite3_stmt *stmt = ((sqlite3_stmt *)p);
  246. if(stmt) sqlite3_finalize(stmt);
  247. return 0;
  248. }
  249. static SQRESULT sq_sqlite3_stmt_constructor(HSQUIRRELVM v)
  250. {
  251. SQ_FUNC_VARS(v);
  252. _rc_ = SQ_ERROR;
  253. GET_sqlite3_INSTANCE_AT(2);
  254. sqlite3_stmt *stmt = 0;
  255. if(_top_ > 2)
  256. {
  257. _rc_ = sq_sqlite3_stmt_prepare_aux(v, self, &stmt, 3);
  258. }
  259. else
  260. {
  261. if(sqlite3_prepare_v2(self, "select 'statement not prepared';", -1, &stmt, 0) != SQLITE_OK)
  262. {
  263. _rc_ = sq_throwerror(v, sqlite3_errmsg(self));
  264. }
  265. else _rc_ = SQ_OK;
  266. }
  267. sq_setinstanceup(v, 1, stmt); //replace self for this instance with this new sqlite3_stmt
  268. sq_setreleasehook(v,1, sq_sqlite3_stmt_releasehook);
  269. return _rc_;
  270. }
  271. static SQRESULT sq_sqlite3_stmt_get_db(HSQUIRRELVM v)
  272. {
  273. SQ_FUNC_VARS_NO_TOP(v);
  274. GET_sqlite3_stmt_INSTANCE();
  275. sqlite3 *db = sqlite3_db_handle(self);
  276. sq_pushuserpointer(v, db);
  277. if(sq_getonregistrytable(v) == SQ_OK)
  278. {
  279. sq_getweakrefval(v, -1);
  280. }
  281. else return sq_throwerror(v, _SC("could not retrive database, maybe it was already closed"));
  282. return 1;
  283. }
  284. static SQRESULT sq_sqlite3_stmt_stmt_ptr(HSQUIRRELVM v)
  285. {
  286. SQ_FUNC_VARS_NO_TOP(v);
  287. GET_sqlite3_stmt_INSTANCE();
  288. sq_pushuserpointer(v, self);
  289. return 1;
  290. }
  291. static SQRESULT sq_sqlite3_stmt_finalize(HSQUIRRELVM v)
  292. {
  293. SQ_FUNC_VARS_NO_TOP(v);
  294. GET_sqlite3_stmt_INSTANCE();
  295. sq_setinstanceup(v, 1, 0); //next calls will fail with "statement is closed"
  296. sq_pushinteger(v, sqlite3_finalize(self));
  297. return 1;
  298. }
  299. static SQRESULT sq_sqlite3_stmt_prepare(HSQUIRRELVM v)
  300. {
  301. SQ_FUNC_VARS_NO_TOP(v);
  302. GET_sqlite3_stmt_INSTANCE();
  303. sqlite3 *db = sqlite3_db_handle(self);
  304. sqlite3_stmt *stmt = 0;
  305. _rc_ = sq_sqlite3_stmt_prepare_aux(v, db, &stmt, 2);
  306. if(stmt)
  307. {
  308. sqlite3_finalize(self); //finalize the previous sqlite3_stmt
  309. sq_setinstanceup(v, 1, stmt); //replace self for this instance with this new sqlite3_stmt
  310. }
  311. return _rc_;
  312. }
  313. static SQRESULT sq_sqlite3_stmt_get_sql(HSQUIRRELVM v)
  314. {
  315. SQ_FUNC_VARS_NO_TOP(v);
  316. GET_sqlite3_stmt_INSTANCE();
  317. sq_pushstring(v, sqlite3_sql(self), -1);
  318. return 1;
  319. }
  320. static SQRESULT sq_sqlite3_stmt_bind(HSQUIRRELVM v)
  321. {
  322. SQ_FUNC_VARS_NO_TOP(v);
  323. GET_sqlite3_stmt_INSTANCE();
  324. SQ_GET_INTEGER(v, 2, npar);
  325. return sqlite3_stmt_bind_value(v, self, npar, 3);
  326. }
  327. static SQRESULT sq_sqlite3_stmt_bind_empty_null(HSQUIRRELVM v)
  328. {
  329. SQ_FUNC_VARS_NO_TOP(v);
  330. GET_sqlite3_stmt_INSTANCE();
  331. SQ_GET_INTEGER(v, 2, npar);
  332. if(sq_gettype(v, 3) == OT_STRING && sq_getsize(v, 3) == 0)
  333. {
  334. sq_pushnull(v);
  335. sq_replace(v, 3);
  336. }
  337. return sqlite3_stmt_bind_value(v, self, npar, 3);
  338. }
  339. static SQRESULT sq_sqlite3_stmt_bind_blob(HSQUIRRELVM v)
  340. {
  341. SQ_FUNC_VARS_NO_TOP(v);
  342. GET_sqlite3_stmt_INSTANCE();
  343. SQ_GET_INTEGER(v, 2, npar);
  344. SQ_GET_STRING(v, 3, blob);
  345. if(sqlite3_bind_blob(self, npar, blob, blob_size, SQLITE_TRANSIENT) != SQLITE_OK)
  346. {
  347. sqlite3 *db = sqlite3_db_handle(self);
  348. return sq_throwerror(v, sqlite3_errmsg(db));
  349. }
  350. return SQ_OK;
  351. }
  352. static SQRESULT sq_sqlite3_stmt_bind_values(HSQUIRRELVM v)
  353. {
  354. SQ_FUNC_VARS(v);
  355. GET_sqlite3_stmt_INSTANCE();
  356. if (_top_ - 1 != sqlite3_bind_parameter_count(self))
  357. return sq_throwerror(v,
  358. _SC("incorrect number of parameters to bind (%d given, %d to bind)"),
  359. _top_ - 1,
  360. sqlite3_bind_parameter_count(self));
  361. for (int n = 2; n <= _top_; ++n)
  362. {
  363. if (sqlite3_stmt_bind_value(v, self, n-1, n) != SQ_OK) return SQ_ERROR;
  364. }
  365. return 0;
  366. }
  367. static SQRESULT sq_sqlite3_stmt_bind_names(HSQUIRRELVM v)
  368. {
  369. SQ_FUNC_VARS_NO_TOP(v);
  370. GET_sqlite3_stmt_INSTANCE();
  371. SQObjectType ptype = sq_gettype(v, 2);
  372. int count = sqlite3_bind_parameter_count(self);
  373. int result;
  374. for (int n = 1; n <= count; ++n)
  375. {
  376. const char *name = sqlite3_bind_parameter_name(self, n);
  377. if (name && (name[0] == ':' || name[0] == '$' || name[0] == '@'))
  378. {
  379. if(ptype == OT_TABLE)
  380. {
  381. sq_pushstring(v, ++name, -1);
  382. if(sq_get(v, 2) == SQ_OK)
  383. {
  384. result = sqlite3_stmt_bind_value(v, self, n, -1);
  385. sq_poptop(v);
  386. }
  387. else return sq_throwerror(v, _SC("bind parameter (%s) not found"), name);
  388. }
  389. else return sq_throwerror(v, _SC("table expected to bind named parameters"));
  390. }
  391. else
  392. {
  393. if(ptype == OT_ARRAY)
  394. {
  395. sq_pushinteger(v, n);
  396. if(sq_get(v, 2) == SQ_OK)
  397. {
  398. result = sqlite3_stmt_bind_value(v, self, n, -1);
  399. sq_poptop(v);
  400. }
  401. else return sq_throwerror(v, _SC("bind parameter (%d) not found"), n);
  402. }
  403. else return sq_throwerror(v, _SC("array expected to bind numbered parameters"));
  404. }
  405. if (result != SQ_OK) return result;
  406. }
  407. return 0;
  408. }
  409. static SQRESULT sq_sqlite3_stmt_bind_parameter_index(HSQUIRRELVM v)
  410. {
  411. SQ_FUNC_VARS_NO_TOP(v);
  412. GET_sqlite3_stmt_INSTANCE();
  413. SQ_GET_STRING(v, 2, spar);
  414. sq_pushinteger(v, sqlite3_bind_parameter_index(self, spar));
  415. return 1;
  416. }
  417. static SQRESULT sq_sqlite3_stmt_reset(HSQUIRRELVM v)
  418. {
  419. SQ_FUNC_VARS_NO_TOP(v);
  420. GET_sqlite3_stmt_INSTANCE();
  421. sq_pushinteger(v, sqlite3_reset(self));
  422. return 1;
  423. }
  424. static SQRESULT sq_sqlite3_stmt_step(HSQUIRRELVM v)
  425. {
  426. SQ_FUNC_VARS_NO_TOP(v);
  427. GET_sqlite3_stmt_INSTANCE();
  428. sq_pushinteger(v, sqlite3_step(self));
  429. return 1;
  430. }
  431. static SQRESULT sq_sqlite3_stmt_next_row(HSQUIRRELVM v)
  432. {
  433. SQ_FUNC_VARS_NO_TOP(v);
  434. GET_sqlite3_stmt_INSTANCE();
  435. sq_pushbool(v, sqlite3_step(self) == SQLITE_ROW);
  436. return 1;
  437. }
  438. static SQRESULT sq_sqlite3_stmt_col_count(HSQUIRRELVM v)
  439. {
  440. SQ_FUNC_VARS_NO_TOP(v);
  441. GET_sqlite3_stmt_INSTANCE();
  442. sq_pushinteger(v, sqlite3_column_count(self));
  443. return 1;
  444. }
  445. static SQRESULT sq_sqlite3_stmt_colsAsArray(HSQUIRRELVM v)
  446. {
  447. SQ_FUNC_VARS_NO_TOP(v);
  448. GET_sqlite3_stmt_INSTANCE();
  449. int col_count = sqlite3_column_count(self);
  450. sq_newarray(v, col_count);
  451. for(int i=0; i<col_count; ++i)
  452. {
  453. sq_pushstring(v, sqlite3_column_name(self, i), -1);
  454. sq_arrayset(v, -2, i);
  455. }
  456. return 1;
  457. }
  458. static SQRESULT sq_sqlite3_stmt_colsAsTable(HSQUIRRELVM v)
  459. {
  460. SQ_FUNC_VARS_NO_TOP(v);
  461. GET_sqlite3_stmt_INSTANCE();
  462. int col_count = sqlite3_column_count(self);
  463. sq_newtable(v);
  464. for(int i=0; i<col_count; ++i)
  465. {
  466. sq_pushstring(v, sqlite3_column_name(self, i), -1);
  467. sq_pushinteger(v, i);
  468. sq_rawset(v, -3);
  469. }
  470. return 1;
  471. }
  472. static SQRESULT sq_sqlite3_stmt_col_name(HSQUIRRELVM v)
  473. {
  474. SQ_FUNC_VARS_NO_TOP(v);
  475. GET_sqlite3_stmt_INSTANCE();
  476. SQ_GET_INTEGER(v, 2, col);
  477. sq_pushstring(v, sqlite3_column_name(self, col), -1);
  478. return 1;
  479. }
  480. static SQRESULT sq_sqlite3_stmt_col_type(HSQUIRRELVM v)
  481. {
  482. SQ_FUNC_VARS_NO_TOP(v);
  483. GET_sqlite3_stmt_INSTANCE();
  484. SQ_GET_INTEGER(v, 2, col);
  485. sq_pushinteger(v, sqlite3_column_type(self, col));
  486. return 1;
  487. }
  488. static SQRESULT sq_sqlite3_stmt_col_declared_type(HSQUIRRELVM v)
  489. {
  490. SQ_FUNC_VARS_NO_TOP(v);
  491. GET_sqlite3_stmt_INSTANCE();
  492. SQ_GET_INTEGER(v, 2, col);
  493. sq_pushstring(v, sqlite3_column_decltype(self, col), -1);
  494. return 1;
  495. }
  496. static int get_col_index(HSQUIRRELVM v, sqlite3_stmt *self)
  497. {
  498. SQInteger _rc_, col;
  499. switch(sq_gettype(v, 2))
  500. {
  501. case OT_INTEGER:
  502. sq_getinteger(v, 2, &col);
  503. break;
  504. case OT_STRING:
  505. {
  506. SQ_GET_STRING(v, 2, col_name);
  507. if(col_name_size == 0) return sq_throwerror(v, "column name can not be empty");
  508. col = -1;
  509. for(int i=0, len=sqlite3_column_count(self); i<len; ++i)
  510. {
  511. if(strcasecmp(col_name, sqlite3_column_name(self, i)) == 0)
  512. {
  513. col = i;
  514. break;
  515. }
  516. }
  517. if(col == -1) return sq_throwerror(v, "column name not found \"%s\"", col_name);
  518. }
  519. break;
  520. default:
  521. return sq_throwerror(v, "wrong parameter expected integer|string");
  522. }
  523. return col;
  524. }
  525. static SQRESULT sq_sqlite3_stmt_col(HSQUIRRELVM v)
  526. {
  527. SQ_FUNC_VARS_NO_TOP(v);
  528. GET_sqlite3_stmt_INSTANCE();
  529. int col = get_col_index(v, self);
  530. if(col < 0) return col;
  531. sqlite3_stmt_push_value(v, self, col, 0);
  532. return 1;
  533. }
  534. static SQRESULT sq_sqlite3_stmt_asBool(HSQUIRRELVM v)
  535. {
  536. SQ_FUNC_VARS_NO_TOP(v);
  537. GET_sqlite3_stmt_INSTANCE();
  538. int col = get_col_index(v, self);
  539. if(col < 0) return col;
  540. const unsigned char *b = sqlite3_column_text(self, col);
  541. sq_pushbool(v, b && *b == '1');
  542. return 1;
  543. }
  544. static SQRESULT sq_sqlite3_stmt_asInteger(HSQUIRRELVM v)
  545. {
  546. SQ_FUNC_VARS_NO_TOP(v);
  547. GET_sqlite3_stmt_INSTANCE();
  548. int col = get_col_index(v, self);
  549. if(col < 0) return col;
  550. sq_pushinteger(v, sqlite3_column_int64(self, col));
  551. return 1;
  552. }
  553. static SQRESULT sq_sqlite3_stmt_asFloat(HSQUIRRELVM v)
  554. {
  555. SQ_FUNC_VARS_NO_TOP(v);
  556. GET_sqlite3_stmt_INSTANCE();
  557. int col = get_col_index(v, self);
  558. if(col < 0) return col;
  559. sq_pushfloat(v, sqlite3_column_double(self, col));
  560. return 1;
  561. }
  562. static SQRESULT sq_sqlite3_stmt_StringOrNull(HSQUIRRELVM v, int withNull)
  563. {
  564. SQ_FUNC_VARS_NO_TOP(v);
  565. GET_sqlite3_stmt_INSTANCE();
  566. int col = get_col_index(v, self);
  567. if(col < 0) return col;
  568. sqlite3_stmt_push_string(v, self, col, withNull ? 0 : NULL_AS_EMPTY_STR);
  569. return 1;
  570. }
  571. static SQRESULT sq_sqlite3_stmt_asString(HSQUIRRELVM v)
  572. {
  573. return sq_sqlite3_stmt_StringOrNull(v, 0);
  574. }
  575. static SQRESULT sq_sqlite3_stmt_asStringOrNull(HSQUIRRELVM v)
  576. {
  577. return sq_sqlite3_stmt_StringOrNull(v, 1);
  578. }
  579. static SQRESULT sq_sqlite3_stmt_asTable(HSQUIRRELVM v)
  580. {
  581. SQ_FUNC_VARS(v);
  582. GET_sqlite3_stmt_INSTANCE();
  583. SQ_OPT_INTEGER(v, 2, flags, 0);
  584. sqlite3_stmt_row_asTable(v, self, flags);
  585. return 1;
  586. }
  587. static SQRESULT sq_sqlite3_stmt_asArray(HSQUIRRELVM v)
  588. {
  589. SQ_FUNC_VARS(v);
  590. GET_sqlite3_stmt_INSTANCE();
  591. SQ_OPT_INTEGER(v, 2, flags, 0);
  592. sqlite3_stmt_row_asArray(v, self, flags);
  593. return 1;
  594. }
  595. static SQRESULT sq_sqlite3_stmt_asArrayOfArrays(HSQUIRRELVM v)
  596. {
  597. SQ_FUNC_VARS(v);
  598. GET_sqlite3_stmt_INSTANCE();
  599. SQ_OPT_INTEGER(v, 2, flags, 0);
  600. sqlite3_stmt_asArrayOfArrays(v, self, flags);
  601. sqlite3_reset(self);
  602. return 1;
  603. }
  604. static SQRESULT sq_sqlite3_stmt_asArrayOfTables(HSQUIRRELVM v)
  605. {
  606. SQ_FUNC_VARS(v);
  607. GET_sqlite3_stmt_INSTANCE();
  608. SQ_OPT_INTEGER(v, 2, flags, 0);
  609. sqlite3_stmt_asArrayOfTables(v, self, flags);
  610. sqlite3_reset(self);
  611. return 1;
  612. }
  613. /*utility functions*/
  614. static void append_escaping_json(SQBlob &json, const char *sz)
  615. {
  616. if(sz) //escape string
  617. {
  618. int last_idx, idx;
  619. idx = last_idx = 0;
  620. for(; sz[idx]; ++idx)
  621. {
  622. char c = sz[idx];
  623. switch(c)
  624. {
  625. case '"':
  626. case '\n':
  627. case '\r':
  628. case '\\':
  629. {
  630. json.Write(sz+last_idx, idx-last_idx);
  631. last_idx = idx;
  632. if(c == '\n')
  633. {
  634. json.Write("\\n", 2);
  635. ++last_idx;
  636. }
  637. else if(c == '\r')
  638. {
  639. //skip it
  640. ++last_idx;
  641. }
  642. else
  643. {
  644. json.Write("\\", 1);
  645. }
  646. }
  647. }
  648. }
  649. //last part
  650. json.Write(sz+last_idx, idx-last_idx);
  651. }
  652. }
  653. static SQRESULT sq_sqlite3_stmt_asJsonArray(HSQUIRRELVM v)
  654. {
  655. SQ_FUNC_VARS(v);
  656. GET_sqlite3_stmt_INSTANCE();
  657. SQ_OPT_INTEGER(v, 3, withMetadata, 0);
  658. int col_count = sqlite3_column_count(self);
  659. int i;
  660. const char* value;
  661. SQBlob json(0, 8192);
  662. if(withMetadata)
  663. {
  664. json.WriteZstr("{\n\"columns\":[\n");
  665. for(i=0; i < col_count; ++i)
  666. {
  667. json.WriteZstr((i == 0 ? "\"" : ",\""));
  668. json.WriteZstr(sqlite3_column_name(self, i));
  669. json.WriteZstr("\"");
  670. }
  671. json.WriteZstr("\n],\n\"column_types\":[\n");
  672. for(i=0; i < col_count; ++i)
  673. {
  674. json.WriteZstr((i == 0 ? "\"" : ",\""));
  675. json.WriteZstr(sqlite3_column_decltype(self, i));
  676. json.WriteZstr("\"");
  677. }
  678. json.WriteZstr("\n],\n\"rows\":[\n");
  679. }
  680. else
  681. {
  682. json.WriteZstr("[\n");
  683. }
  684. int row_count = 0;
  685. while(sqlite3_step(self) == SQLITE_ROW)
  686. {
  687. json.WriteZstr((row_count++ == 0 ? "[" : ",["));
  688. for(i=0; i < col_count; ++i)
  689. {
  690. json.WriteZstr((i == 0 ? "\"" : ",\""));
  691. value = (const char*)sqlite3_column_text(self, i);
  692. if(value)
  693. {
  694. switch(sqlite3_column_type(self, i))
  695. {
  696. case SQLITE_TEXT:
  697. {
  698. append_escaping_json(json, value);
  699. }
  700. break;
  701. default:
  702. json.WriteZstr(value);
  703. }
  704. }
  705. json.WriteZstr("\"");
  706. }
  707. json.WriteZstr("]\n");
  708. }
  709. json.WriteZstr("]");
  710. if(withMetadata) json.WriteZstr("\n}");
  711. sq_pushstring(v, (const SQChar*)json.GetBuf(), json.Len());
  712. return 1;
  713. }
  714. static SQRESULT sq_sqlite3_stmt_asJsonObject(HSQUIRRELVM v)
  715. {
  716. SQ_FUNC_VARS_NO_TOP(v);
  717. GET_sqlite3_stmt_INSTANCE();
  718. int col_count = sqlite3_column_count(self);
  719. int i;
  720. const char* value;
  721. SQBlob json(0, 8192);
  722. json.WriteZstr("{");
  723. if(sqlite3_step(self) == SQLITE_ROW)
  724. {
  725. for(i=0; i < col_count; ++i)
  726. {
  727. json.WriteZstr((i == 0 ? "\"" : "\",\""));
  728. append_escaping_json(json, (const char*)sqlite3_column_name(self, i));
  729. json.WriteZstr("\":\"");
  730. value = (const char*)sqlite3_column_text(self, i);
  731. if(value)
  732. {
  733. switch(sqlite3_column_type(self, i))
  734. {
  735. case SQLITE_TEXT:
  736. {
  737. append_escaping_json(json, value);
  738. }
  739. break;
  740. default:
  741. json.WriteZstr(value);
  742. }
  743. }
  744. }
  745. json.WriteZstr("\"");
  746. }
  747. json.WriteZstr("}");
  748. sq_pushstring(v, (const SQChar*)json.GetBuf(), json.Len());
  749. return 1;
  750. }
  751. #define UNSIGNED(n) n##U
  752. #define IBYTE1 UNSIGNED(255)
  753. #define IBYTE2 UNSIGNED(255*255)
  754. #define IBYTE3 UNSIGNED(255*255*255)
  755. #define IBYTE4 UNSIGNED(255*255*255*255)
  756. #define SIZE1BYTE 250
  757. #define SIZE2BYTE 251
  758. #define SIZE3BYTE 252
  759. #define SIZE4BYTE 253
  760. #define SLEMARK 254
  761. #define SLEEND 255
  762. static SQRESULT sle2array(HSQUIRRELVM v, const unsigned char *p, size_t sle_size, const unsigned char **next)
  763. {
  764. size_t size, data_len = 0;
  765. if(sle_size == 0)
  766. {
  767. *next = 0;
  768. return 0;
  769. }
  770. while(*p != SLEEND) //data finished now follow digest
  771. {
  772. size = *p++;
  773. if(size >= SIZE1BYTE)
  774. {
  775. if(size == SIZE1BYTE)
  776. {
  777. //data bigger than 250 and less 500 next byte has more info
  778. size += *p++;
  779. }
  780. else if(size == SIZE2BYTE)
  781. {
  782. //data bigger than 250 next two bytes has more info
  783. size = (*p++ * IBYTE1);
  784. size += *p++;
  785. }
  786. else if(size == SIZE3BYTE)
  787. {
  788. //data bigger than 250 next three bytes has more info
  789. size = (*p++ * IBYTE2);
  790. size += (*p++ * IBYTE1);
  791. size += *p++;
  792. }
  793. else if(size == SIZE4BYTE)
  794. {
  795. //data bigger than 250 next four bytes has more info
  796. size = (*p++ * IBYTE3);
  797. size += (*p++ * IBYTE2);
  798. size += (*p++ * IBYTE1);
  799. size += *p++;
  800. }
  801. else if(size == SLEMARK)
  802. {
  803. //reserved can be used for multi part data, metadata, digest, ...
  804. break;
  805. }
  806. }
  807. sq_pushstring(v, (const char*)p, size);
  808. sq_arrayappend(v, -2);
  809. p += size;
  810. data_len += size;
  811. if(data_len > sle_size)
  812. {
  813. //something went wrong here
  814. return sq_throwerror(v, "We got more data than expected !");
  815. }
  816. }
  817. *next = ++p;
  818. return 0;
  819. }
  820. static SQRESULT sle2vec(HSQUIRRELVM v, const unsigned char *sle,
  821. size_t sle_size,
  822. const unsigned char **next)
  823. {
  824. int rc;
  825. sq_newarray(v, 0);
  826. if(sle[0] != '[')
  827. {
  828. return sq_throwerror(v, "Invalid encoded vector !");
  829. }
  830. if(sle[1] == ']')
  831. {
  832. *next = sle+2; //empty array
  833. return 0;
  834. }
  835. if((rc = sle2array(v, sle+1, sle_size-1, next)) < 0) return rc;
  836. if(!*next || *next[0] != ']')
  837. {
  838. return sq_throwerror(v, "Invalid end of vector !");
  839. }
  840. (*next)++;
  841. return 0;
  842. }
  843. static SQRESULT sq_sle2vecOfvec(HSQUIRRELVM v)
  844. {
  845. SQ_FUNC_VARS(v);
  846. SQBlob *blob = NULL;
  847. const SQChar *sle, *sle_start;
  848. SQInteger sle_size;
  849. SQObjectType ptype = sq_gettype(v, 2);
  850. switch(ptype)
  851. {
  852. case OT_STRING:
  853. sq_getstr_and_size(v, 2, &sle, &sle_size);
  854. break;
  855. case OT_INSTANCE:
  856. if(SQ_FAILED(sq_getinstanceup(v,2,(SQUserPointer*)&blob,(SQUserPointer)SQBlob::SQBlob_TAG)))
  857. return sq_throwerror(v,_SC("invalid type tag"));
  858. if(!blob || !blob->IsValid())
  859. return sq_throwerror(v,_SC("the blob is invalid"));
  860. sle = (const SQChar*)blob->GetBuf();
  861. sle_size = blob->Len();
  862. break;
  863. default:
  864. return sq_throwerror(v,_SC("invalid parameter 1 of type %s"), sq_gettypename(v, 2));
  865. }
  866. if(sq_gettype(v, 3) != OT_ARRAY)
  867. return sq_throwerror(v,_SC("invalid parameter 2 of type %s"), sq_gettypename(v, 3));
  868. SQ_OPT_INTEGER(v, 4, start, 0);
  869. if(start < 0) return sq_throwerror(v, _SC("invalid start position (%d)"), start);
  870. sq_push(v, 3);
  871. int rc, data_count = 1;
  872. const unsigned char *next;
  873. sle_start = sle + start;
  874. sle_size -= start;
  875. if(sle_start[0] != '[')
  876. {
  877. return sq_throwerror(v, "Invalid encoded vector !");
  878. }
  879. if(sle_start[1] == ']')
  880. {
  881. //sq_pushinteger(v, start+2);
  882. return 1; //empty array
  883. }
  884. int saved_top = sq_gettop(v);
  885. if((rc=sle2vec(v, (const unsigned char *)sle_start+1, sle_size-1, &next)) < 0) return rc;
  886. while(next && *next == '[')
  887. {
  888. sq_arrayappend(v, -2);
  889. data_count++;
  890. if((rc=sle2vec(v, next, sle_size - (next - (const unsigned char *)sle_start), &next)) < 0) return rc;
  891. }
  892. if(sq_gettop(v) != saved_top)
  893. {
  894. //last added table left
  895. sq_arrayappend(v, -2);
  896. data_count++;
  897. }
  898. if(!next || *next != ']')
  899. {
  900. return sq_throwerror(v, "Invalid end of vector !");
  901. }
  902. sq_pushinteger(v, ((const SQChar*)(++next)) - sle);
  903. return 1;
  904. }
  905. typedef unsigned char slebuf_t[8];
  906. static int sleSize2buf(slebuf_t sle, size_t size)
  907. {
  908. size_t next_size, tmp_size;
  909. if(size < SIZE1BYTE)
  910. {
  911. sle[0] = size;
  912. return 1;
  913. }
  914. else if(size < (SIZE1BYTE*2))
  915. {
  916. sle[0] = SIZE1BYTE;
  917. next_size = size - 250;
  918. sle[1] = next_size;
  919. return 2;
  920. }
  921. else if(size < IBYTE2)
  922. {
  923. sle[0] = SIZE2BYTE;
  924. next_size = size/IBYTE1;
  925. sle[1] = next_size;
  926. next_size = size%IBYTE1;
  927. sle[2] = next_size;
  928. return 3;
  929. }
  930. else if(size < IBYTE3)
  931. {
  932. sle[0] = SIZE3BYTE;
  933. next_size = size/IBYTE2;
  934. sle[1] = next_size;
  935. tmp_size = size%IBYTE2;
  936. next_size = tmp_size/IBYTE1;
  937. sle[2] = next_size;
  938. next_size = tmp_size%IBYTE1;
  939. sle[3] = next_size;
  940. return 4;
  941. }
  942. else if(size < IBYTE4)
  943. {
  944. sle[0] = SIZE4BYTE;
  945. next_size = size/IBYTE3;
  946. sle[1] = next_size;
  947. tmp_size = size%IBYTE3;
  948. next_size = tmp_size/IBYTE2;
  949. sle[2] = next_size;
  950. tmp_size = tmp_size%IBYTE2;
  951. next_size = tmp_size/IBYTE1;
  952. sle[3] = next_size;
  953. next_size = tmp_size%IBYTE1;
  954. sle[5] = next_size;
  955. return 5;
  956. }
  957. return 0;
  958. }
  959. static SQRESULT sq_get_sle_size(HSQUIRRELVM v)
  960. {
  961. SQ_FUNC_VARS_NO_TOP(v);
  962. slebuf_t slebuf;
  963. SQ_GET_INTEGER(v, 2, size);
  964. int slesize = sleSize2buf(slebuf, size);
  965. if(slesize)
  966. {
  967. sq_pushstring(v, (const char *)slebuf, slesize);
  968. return 1;
  969. }
  970. return 0;
  971. }
  972. static int add2sle(SQBlob &sle, const char *str, size_t size)
  973. {
  974. slebuf_t slebuf;
  975. int slesize = sleSize2buf(slebuf, size);
  976. if(slesize) sle.Write((const char *)slebuf, slesize);
  977. if(size) sle.Write(str, size);
  978. return size;
  979. }
  980. static SQRESULT sq_add2sle(HSQUIRRELVM v)
  981. {
  982. SQ_FUNC_VARS(v);
  983. SQ_GET_INSTANCE_VAR(v, 2, SQBlob, blob, SQBlob::SQBlob_TAG);
  984. SQ_GET_STRING(v, 3, str);
  985. SQ_OPT_INTEGER(v, 4, size, str_size);
  986. if(size < 0) return sq_throwerror(v, _SC("invalid size value (%d)"), size);
  987. sq_pushinteger(v, add2sle(*blob, str, size));
  988. return 1;
  989. }
  990. static SQRESULT sq_sqlite3_stmt_asSleArray(HSQUIRRELVM v)
  991. {
  992. SQ_FUNC_VARS_NO_TOP(v);
  993. GET_sqlite3_stmt_INSTANCE();
  994. int col_count = sqlite3_column_count(self);
  995. int i;
  996. const char *value;
  997. SQBlob sle(0, 8192);
  998. sle.WriteZstr("[[");
  999. for(i=0; i < col_count; ++i)
  1000. {
  1001. value = sqlite3_column_name(self, i);
  1002. add2sle(sle, value, strlen(value));
  1003. }
  1004. sle.WriteChar(SLEEND);
  1005. sle.WriteChar(']');
  1006. while(sqlite3_step(self) == SQLITE_ROW)
  1007. {
  1008. sle.WriteChar('[');
  1009. for(i=0; i < col_count; ++i)
  1010. {
  1011. switch(sqlite3_column_type(self, i))
  1012. {
  1013. case SQLITE_BLOB:
  1014. {
  1015. add2sle(sle, (const char*)sqlite3_column_blob(self, i),
  1016. sqlite3_column_bytes(self, i));
  1017. }
  1018. break;
  1019. default:
  1020. add2sle(sle, (const char*)sqlite3_column_text(self, i),
  1021. sqlite3_column_bytes(self, i));
  1022. }
  1023. }
  1024. sle.WriteChar(SLEEND);
  1025. sle.WriteChar(']');
  1026. }
  1027. sle.WriteChar(']');
  1028. sq_pushstring(v, (const SQChar*)sle.GetBuf(), sle.Len());
  1029. return 1;
  1030. }
  1031. #define _DECL_FUNC(name,nparams,tycheck, isStatic) {_SC(#name), sq_sqlite3_stmt_##name,nparams,tycheck, isStatic}
  1032. static SQRegFunction sq_sqlite3_stmt_methods[] =
  1033. {
  1034. _DECL_FUNC(constructor, -2, _SC("xxs s|n|b|o"), SQFalse),
  1035. _DECL_FUNC(stmt_ptr, 1, _SC("x"), SQFalse),
  1036. _DECL_FUNC(get_db, 1, _SC("x"), SQFalse),
  1037. _DECL_FUNC(finalize, 1, _SC("x"), SQFalse),
  1038. _DECL_FUNC(prepare, -2, _SC("xs s|n|b|o"), SQFalse),
  1039. _DECL_FUNC(get_sql, 1, _SC("x"), SQFalse),
  1040. _DECL_FUNC(bind, 3, _SC("xi s|n|b|o"), SQFalse),
  1041. _DECL_FUNC(bind_empty_null, 3, _SC("xi s|n|b|o"), SQFalse),
  1042. _DECL_FUNC(bind_blob, 3, _SC("xis"), SQFalse),
  1043. _DECL_FUNC(bind_values, -2, _SC("x s|n|b|o"), SQFalse),
  1044. _DECL_FUNC(bind_names, 2, _SC("x t|a"), SQFalse),
  1045. _DECL_FUNC(bind_parameter_index, 2, _SC("xs"), SQFalse),
  1046. _DECL_FUNC(step, 1, _SC("x"), SQFalse),
  1047. _DECL_FUNC(reset, 1, _SC("x"), SQFalse),
  1048. _DECL_FUNC(next_row, 1, _SC("x"), SQFalse),
  1049. _DECL_FUNC(colsAsArray, 1, _SC("x"), SQFalse),
  1050. _DECL_FUNC(colsAsTable, 1, _SC("x"), SQFalse),
  1051. _DECL_FUNC(col_count, 1, _SC("x"), SQFalse),
  1052. _DECL_FUNC(col_name, 2, _SC("xi"), SQFalse),
  1053. _DECL_FUNC(col_type, 2, _SC("xi"), SQFalse),
  1054. _DECL_FUNC(col_declared_type, 2, _SC("xi"), SQFalse),
  1055. _DECL_FUNC(asArray, -1, _SC("xi"), SQFalse),
  1056. _DECL_FUNC(asTable, -1, _SC("xi"), SQFalse),
  1057. _DECL_FUNC(asArrayOfArrays, -1, _SC("xi"), SQFalse),
  1058. _DECL_FUNC(asArrayOfTables, -1, _SC("xi"), SQFalse),
  1059. _DECL_FUNC(asSleArray, 1, _SC("x"), SQFalse),
  1060. _DECL_FUNC(col, 2, _SC("x i|s"), SQFalse),
  1061. _DECL_FUNC(asString, 2, _SC("x i|s"), SQFalse),
  1062. _DECL_FUNC(asStringOrNull, 2, _SC("x i|s"), SQFalse),
  1063. _DECL_FUNC(asInteger, 2, _SC("x i|s"), SQFalse),
  1064. _DECL_FUNC(asBool, 2, _SC("x i|s"), SQFalse),
  1065. _DECL_FUNC(asFloat, 2, _SC("x i|s"), SQFalse),
  1066. {0,0}
  1067. };
  1068. #undef _DECL_FUNC
  1069. static SQRESULT sqlite3_exec_fmt(HSQUIRRELVM v, e_type_result type_result, int null_as_empty_str=1)
  1070. {
  1071. SQ_FUNC_VARS_NO_TOP(v);
  1072. GET_sqlite3_INSTANCE();
  1073. //SQ_GET_STRING(v, 2, szSQL);
  1074. sqlite3_stmt *stmt = 0;
  1075. _rc_ = sq_sqlite3_stmt_prepare_aux(v, self, &stmt, 2);
  1076. if(_rc_ < 0)
  1077. {
  1078. if(stmt) sqlite3_finalize(stmt);
  1079. return _rc_;
  1080. }
  1081. int rc = sqlite3_step(stmt);
  1082. if(type_result == tr_ddml)
  1083. {
  1084. if(rc != SQLITE_DONE)
  1085. {
  1086. sqlite3_finalize(stmt);
  1087. return sq_throwerror(v, "SQL is not a DDML %d %s", rc, sqlite3_errmsg(self));
  1088. }
  1089. sq_pushinteger(v, sqlite3_changes(sqlite3_db_handle(stmt)));
  1090. }
  1091. else
  1092. {
  1093. if(rc != SQLITE_ROW)
  1094. {
  1095. sqlite3_finalize(stmt);
  1096. return sq_throwerror(v, "%d %s", rc, sqlite3_errmsg(self));
  1097. }
  1098. switch(type_result)
  1099. {
  1100. case tr_first_row_first_col:
  1101. sqlite3_stmt_push_value(v, stmt, 0, 0);
  1102. break;
  1103. case tr_first_row:
  1104. sqlite3_stmt_row_asArray(v, stmt, NULL_AS_EMPTY_STR|AS_STRING_ALWAYS);
  1105. break;
  1106. case tr_all_rows:
  1107. sqlite3_stmt_asArrayOfArrays(v, stmt, NULL_AS_EMPTY_STR|AS_STRING_ALWAYS);
  1108. break;
  1109. default:
  1110. sqlite3_finalize(stmt);
  1111. return sq_throwerror(v, "Unknown requested return type %d", type_result);
  1112. }
  1113. }
  1114. sqlite3_finalize(stmt);
  1115. return 1;
  1116. }
  1117. static SQRESULT sq_sqlite3_close_release(HSQUIRRELVM v, sq_sqlite3_sdb *sdb)
  1118. {
  1119. SQRESULT rc = SQ_ERROR;
  1120. if(sdb)
  1121. {
  1122. sqlite3 *db = sdb->db;
  1123. sqlite3_stmt* statement = NULL;
  1124. int count = 0;
  1125. while ((statement = sqlite3_next_stmt(db, statement)))
  1126. {
  1127. sqlite3_finalize(statement);
  1128. count++;
  1129. }
  1130. if (count) return sq_throwerror(v, _SC("closing database with %d statements not closed."), count);
  1131. if(sqlite3_close_v2(db) == SQLITE_OK)
  1132. {
  1133. rc = SQ_OK;
  1134. //remove waekref from registrytable
  1135. sq_pushregistrytable(sdb->v);
  1136. sq_pushuserpointer(sdb->v, db);
  1137. sq_deleteslot(sdb->v, -2, SQFalse);
  1138. sq_poptop(sdb->v);
  1139. if(sdb->func)
  1140. {
  1141. sq_sqlite3_sdb_func *func, *func_next;
  1142. func = sdb->func;
  1143. while (func)
  1144. {
  1145. func_next = func->next;
  1146. sq_release(sdb->v, &func->fn_step);
  1147. sq_release(sdb->v, &func->fn_finalize);
  1148. sq_release(sdb->v, &func->udata);
  1149. sq_free(func, sizeof(sq_sqlite3_sdb_func));
  1150. func = func_next;
  1151. }
  1152. sdb->func = NULL;
  1153. }
  1154. /* 'free' all references */
  1155. sq_release(sdb->v, &sdb->busy_cb);
  1156. sq_release(sdb->v, &sdb->busy_udata);
  1157. sq_release(sdb->v, &sdb->progress_cb);
  1158. sq_release(sdb->v, &sdb->progress_udata);
  1159. sq_release(sdb->v, &sdb->trace_cb);
  1160. sq_release(sdb->v, &sdb->trace_udata);
  1161. sq_release(sdb->v, &sdb->null_value);
  1162. sq_free(sdb, sizeof(sq_sqlite3_sdb));
  1163. }
  1164. else return sq_throwerror(v, sqlite3_errmsg(db));
  1165. }
  1166. return rc;
  1167. }
  1168. static SQRESULT sq_sqlite3_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  1169. {
  1170. sq_sqlite3_sdb *sdb = ((sq_sqlite3_sdb *)p);
  1171. sq_sqlite3_close_release(v, sdb);
  1172. return 0;
  1173. }
  1174. static SQRESULT sq_sqlite3_constructor(HSQUIRRELVM v)
  1175. {
  1176. SQ_FUNC_VARS(v);
  1177. SQ_GET_STRING(v, 2, dbname);
  1178. SQ_OPT_INTEGER(v, 3, flags, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_SUBLATIN_NA_LIKE);
  1179. sqlite3 *db;
  1180. int rc = sqlite3_open_v2(dbname, &db, flags, 0);
  1181. if(rc != SQLITE_OK) return sq_throwerror(v, "Failed to open database ! %d", rc);
  1182. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb *)sq_malloc(sizeof(sq_sqlite3_sdb));
  1183. memset(sdb, 0, sizeof(sq_sqlite3_sdb));
  1184. sdb->db = db;
  1185. sdb->v = v;
  1186. sq_resetobject(&sdb->busy_cb);
  1187. sq_resetobject(&sdb->busy_udata);
  1188. sq_resetobject(&sdb->progress_cb);
  1189. sq_resetobject(&sdb->progress_udata);
  1190. sq_resetobject(&sdb->trace_cb);
  1191. sq_resetobject(&sdb->trace_udata);
  1192. sq_resetobject(&sdb->null_value);
  1193. sq_setinstanceup(v, 1, sdb);
  1194. sq_setreleasehook(v,1, sq_sqlite3_releasehook);
  1195. //save a weakref to allow statement return it's db
  1196. sq_pushuserpointer(v, sdb->db);
  1197. sq_weakref(v, 1);
  1198. sq_setonregistrytable(v);
  1199. return 1;
  1200. }
  1201. static SQRESULT sq_sqlite3_close(HSQUIRRELVM v)
  1202. {
  1203. SQ_FUNC_VARS_NO_TOP(v);
  1204. GET_sqlite3_INSTANCE();
  1205. if(sq_sqlite3_close_release(v, sdb) != SQ_OK) return SQ_ERROR;
  1206. sq_setinstanceup(v, 1, 0); //next calls will fail with "database is closed"
  1207. return 0;
  1208. }
  1209. static SQRESULT sq_sqlite3_db_ptr(HSQUIRRELVM v)
  1210. {
  1211. SQ_FUNC_VARS_NO_TOP(v);
  1212. GET_sqlite3_INSTANCE();
  1213. sq_pushuserpointer(v, self);
  1214. return 1;
  1215. }
  1216. /* bool IsAutoCommitOn( ) */
  1217. static SQRESULT sq_sqlite3_IsAutoCommitOn(HSQUIRRELVM v)
  1218. {
  1219. SQ_FUNC_VARS_NO_TOP(v);
  1220. GET_sqlite3_INSTANCE();
  1221. sq_pushbool(v, sqlite3_get_autocommit(self) ? SQTrue : SQFalse);
  1222. return 1;
  1223. }
  1224. /* int changes( ) */
  1225. static SQRESULT sq_sqlite3_changes(HSQUIRRELVM v)
  1226. {
  1227. SQ_FUNC_VARS_NO_TOP(v);
  1228. GET_sqlite3_INSTANCE();
  1229. sq_pushinteger(v, sqlite3_total_changes(self));
  1230. return 1;
  1231. }
  1232. static SQRESULT sq_sqlite3_exec(HSQUIRRELVM v)
  1233. {
  1234. return sqlite3_exec_fmt(v, tr_all_rows);
  1235. }
  1236. /* int exec_dml( const char * szSQL ) */
  1237. static SQRESULT sq_sqlite3_exec_dml(HSQUIRRELVM v)
  1238. {
  1239. SQ_FUNC_VARS_NO_TOP(v);
  1240. GET_sqlite3_INSTANCE();
  1241. SQ_GET_STRING(v, 2, szSQL);
  1242. char *errmsg;
  1243. if(sqlite3_exec(self, szSQL, NULL, NULL, &errmsg) != SQLITE_OK){
  1244. sq_throwerror(v, errmsg);
  1245. sqlite3_free(errmsg);
  1246. return SQ_ERROR;
  1247. }
  1248. sq_pushinteger(v, sqlite3_changes(self));
  1249. return 1;
  1250. }
  1251. /* bool exec_get_one( std::string & result , const char * szSQL ) */
  1252. static SQRESULT sq_sqlite3_exec_get_one(HSQUIRRELVM v)
  1253. {
  1254. return sqlite3_exec_fmt(v, tr_first_row_first_col);
  1255. }
  1256. static SQRESULT sq_sqlite3_exec_get_first_row(HSQUIRRELVM v)
  1257. {
  1258. return sqlite3_exec_fmt(v, tr_first_row);
  1259. }
  1260. /* const char * get_db_name( ) */
  1261. static SQRESULT sq_sqlite3_get_db_name(HSQUIRRELVM v)
  1262. {
  1263. SQ_FUNC_VARS_NO_TOP(v);
  1264. GET_sqlite3_INSTANCE();
  1265. sq_pushstring(v, sqlite3_db_filename(self, "main"), -1);
  1266. return 1;
  1267. }
  1268. /* sqlite3_int64 last_row_id( ) */
  1269. static SQRESULT sq_sqlite3_last_row_id(HSQUIRRELVM v)
  1270. {
  1271. SQ_FUNC_VARS_NO_TOP(v);
  1272. GET_sqlite3_INSTANCE();
  1273. sq_pushinteger(v, sqlite3_last_insert_rowid(self));
  1274. return 1;
  1275. }
  1276. /* stmt * prepare( const char * sql ) */
  1277. static SQRESULT sq_sqlite3_prepare(HSQUIRRELVM v)
  1278. {
  1279. SQ_FUNC_VARS_NO_TOP(v);
  1280. //GET_sqlite3_INSTANCE();
  1281. SQ_GET_STRING(v, 2, sql);
  1282. sq_pushstring(v, SQLite3_Stmt_TAG, -1);
  1283. if(sq_getonroottable(v) == SQ_ERROR) return SQ_ERROR;
  1284. sq_pushroottable(v);
  1285. sq_push(v, 1);
  1286. sq_push(v, 2);
  1287. if(sq_call(v, 3, SQTrue, SQFalse) != SQ_OK) return SQ_ERROR;
  1288. return 1;
  1289. }
  1290. /* void set_busy_timeout( int nMillisecs ) */
  1291. static SQRESULT sq_sqlite3_set_busy_timeout(HSQUIRRELVM v)
  1292. {
  1293. SQ_FUNC_VARS_NO_TOP(v);
  1294. //GET_sqlite3_INSTANCE();
  1295. SQ_GET_INTEGER(v, 2, nMillisecs);
  1296. //self->set_busy_timeout(nMillisecs);
  1297. return 0;
  1298. }
  1299. /* int total_changes( ) */
  1300. static SQRESULT sq_sqlite3_total_changes(HSQUIRRELVM v)
  1301. {
  1302. SQ_FUNC_VARS_NO_TOP(v);
  1303. GET_sqlite3_INSTANCE();
  1304. sq_pushinteger(v, sqlite3_total_changes(self));
  1305. return 1;
  1306. }
  1307. static SQRESULT sq_sqlite3_enable_shared_cache(HSQUIRRELVM v)
  1308. {
  1309. SQ_FUNC_VARS_NO_TOP(v);
  1310. SQ_GET_BOOL(v, 2, bhow);
  1311. sq_pushbool(v, sqlite3_enable_shared_cache(bhow) == SQLITE_OK);
  1312. return 1;
  1313. }
  1314. static SQRESULT sq_sqlite3_sleep(HSQUIRRELVM v)
  1315. {
  1316. SQ_FUNC_VARS_NO_TOP(v);
  1317. SQ_GET_INTEGER(v, 2, n);
  1318. sqlite3_sleep(n);
  1319. return 0;
  1320. }
  1321. static SQRESULT sq_sqlite3_version(HSQUIRRELVM v)
  1322. {
  1323. sq_pushstring(v, sqlite3_libversion(), -1);
  1324. return 1;
  1325. }
  1326. static SQRESULT sq_sqlite3_errcode(HSQUIRRELVM v)
  1327. {
  1328. SQ_FUNC_VARS_NO_TOP(v);
  1329. GET_sqlite3_INSTANCE();
  1330. sq_pushinteger(v, sqlite3_errcode(self));
  1331. return 1;
  1332. }
  1333. static SQRESULT sq_sqlite3_errmsg(HSQUIRRELVM v)
  1334. {
  1335. SQ_FUNC_VARS_NO_TOP(v);
  1336. GET_sqlite3_INSTANCE();
  1337. sq_pushstring(v, sqlite3_errmsg(self), -1);
  1338. return 1;
  1339. }
  1340. #ifndef WIN32
  1341. static SQRESULT sq_sqlite3_temp_directory(HSQUIRRELVM v)
  1342. {
  1343. SQ_FUNC_VARS(v);
  1344. const char *oldtemp = sqlite3_temp_directory;
  1345. if (_top_ > 1)
  1346. {
  1347. SQ_GET_STRING(v, 2, temp);
  1348. if (sqlite3_temp_directory)
  1349. {
  1350. sqlite3_free((char*)sqlite3_temp_directory);
  1351. }
  1352. if (temp && temp_size)
  1353. {
  1354. sqlite3_temp_directory = sqlite3_mprintf("%s", temp);
  1355. }
  1356. else
  1357. {
  1358. sqlite3_temp_directory = NULL;
  1359. }
  1360. }
  1361. sq_pushstring(v, oldtemp, -1);
  1362. return 1;
  1363. }
  1364. #endif
  1365. #ifdef SQLITE_HAS_CODEC
  1366. //#include "codecext.h"
  1367. /*
  1368. ** Params: db, sql
  1369. ** returns: code, compiled length or error message
  1370. */
  1371. static SQRESULT sq_sqlite3_key(HSQUIRRELVM v)
  1372. {
  1373. SQ_FUNC_VARS_NO_TOP(v);
  1374. GET_sqlite3_INSTANCE();
  1375. SQ_GET_STRING(v, 2, key);
  1376. sq_pushinteger(v, sqlite3_key(self, key, key_size));
  1377. return 1;
  1378. }
  1379. /*
  1380. ** Params: db, sql
  1381. ** returns: code, compiled length or error message
  1382. */
  1383. static SQRESULT sq_sqlite3_rekey(HSQUIRRELVM v)
  1384. {
  1385. SQ_FUNC_VARS_NO_TOP(v);
  1386. GET_sqlite3_INSTANCE();
  1387. SQ_GET_STRING(v, 2, key);
  1388. sq_pushinteger(v, sqlite3_rekey(self, key, key_size));
  1389. return 1;
  1390. }
  1391. #endif
  1392. #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK
  1393. /*
  1394. ** progress handler:
  1395. ** Params: database, number of opcodes, callback function, userdata
  1396. **
  1397. ** callback function:
  1398. ** Params: userdata
  1399. ** returns: 0 to return immediatly and return SQLITE_ABORT, non-zero to continue
  1400. */
  1401. static int db_progress_callback(void *user)
  1402. {
  1403. SQInteger result = 1; /* abort by default */
  1404. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)user;
  1405. HSQUIRRELVM v = sdb->v;
  1406. int top = sq_gettop(v);
  1407. sq_pushobject(v, sdb->progress_cb);
  1408. sq_pushroottable(v); //this
  1409. sq_pushobject(v, sdb->progress_udata);
  1410. /* call lua function */
  1411. if (sq_call(v, 2, SQTrue, SQFalse) == SQ_OK)
  1412. sq_getinteger(v, -1, &result);
  1413. sq_settop(v, top);
  1414. return result;
  1415. }
  1416. static SQRESULT sq_sqlite3_progress_handler(HSQUIRRELVM v)
  1417. {
  1418. SQ_FUNC_VARS(v);
  1419. GET_sqlite3_INSTANCE();
  1420. if (_top_ < 2 || sq_gettype(v, 2) == OT_NULL)
  1421. {
  1422. sq_release(v, &sdb->progress_cb);
  1423. sq_release(v, &sdb->progress_udata);
  1424. sq_resetobject(&sdb->progress_cb);
  1425. sq_resetobject(&sdb->progress_udata);
  1426. /* clear busy handler */
  1427. sqlite3_progress_handler(self, 0, NULL, NULL);
  1428. }
  1429. else
  1430. {
  1431. SQ_GET_INTEGER(v, 2, nop);
  1432. if(sq_gettype(v, 3) != OT_CLOSURE)
  1433. return sq_throwerror(v, _SC("invalid second parameter expected closure"));
  1434. sq_getstackobj(v, 3, &sdb->progress_cb);
  1435. sq_addref(v, &sdb->progress_cb);
  1436. if(_top_ > 3)
  1437. {
  1438. sq_getstackobj(v, 4, &sdb->progress_udata);
  1439. sq_addref(v, &sdb->progress_udata);
  1440. }
  1441. /* set progress callback */
  1442. sqlite3_progress_handler(self, nop, db_progress_callback, sdb);
  1443. }
  1444. return 0;
  1445. }
  1446. #else /* #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK */
  1447. static SQRESULT sq_sqlite3_progress_handler(HSQUIRRELVM v)
  1448. {
  1449. return sq_throwerror(v, _SC("progress callback support disabled at compile time"));
  1450. return 0;
  1451. }
  1452. #endif /* #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK */
  1453. /*
  1454. ** trace callback:
  1455. ** Params: database, callback function, userdata
  1456. **
  1457. ** callback function:
  1458. ** Params: userdata, sql
  1459. */
  1460. static void db_trace_callback(void *user, const char *sql)
  1461. {
  1462. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)user;
  1463. HSQUIRRELVM v = sdb->v;
  1464. int top = sq_gettop(v);
  1465. /* setup squirrel callback call */
  1466. sq_pushobject(v, sdb->trace_cb);
  1467. sq_pushroottable(v);
  1468. sq_pushobject(v, sdb->trace_udata);
  1469. sq_pushstring(v, sql, -1); /* traced sql statement */
  1470. /* call squirrel function */
  1471. sq_call(v, 3, SQFalse, SQFalse);
  1472. /* ignore any error generated by this function */
  1473. sq_settop(v, top);
  1474. }
  1475. static SQRESULT sq_sqlite3_trace(HSQUIRRELVM v)
  1476. {
  1477. SQ_FUNC_VARS(v);
  1478. GET_sqlite3_INSTANCE();
  1479. if (_top_ < 2 || sq_gettype(v, 2) == OT_NULL)
  1480. {
  1481. sq_release(v, &sdb->trace_cb);
  1482. sq_release(v, &sdb->trace_udata);
  1483. sq_resetobject(&sdb->trace_cb);
  1484. sq_resetobject(&sdb->trace_udata);
  1485. /* clear trace handler */
  1486. sqlite3_trace(self, NULL, NULL);
  1487. }
  1488. else
  1489. {
  1490. if(sq_gettype(v, 2) != OT_CLOSURE)
  1491. return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
  1492. sq_getstackobj(v, 2, &sdb->trace_cb);
  1493. sq_addref(v, &sdb->trace_cb);
  1494. if(_top_ > 2)
  1495. {
  1496. sq_getstackobj(v, 3, &sdb->trace_udata);
  1497. sq_addref(v, &sdb->trace_udata);
  1498. }
  1499. /* set trace callback */
  1500. sqlite3_trace(self, db_trace_callback, sdb);
  1501. }
  1502. return 0;
  1503. }
  1504. /*
  1505. ** busy handler:
  1506. ** Params: database, callback function, userdata
  1507. **
  1508. ** callback function:
  1509. ** Params: userdata, number of tries
  1510. ** returns: 0 to return immediatly and return SQLITE_BUSY, non-zero to try again
  1511. */
  1512. static int db_busy_callback(void *user, int tries)
  1513. {
  1514. SQBool retry = SQFalse; /* abort by default */
  1515. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)user;
  1516. HSQUIRRELVM v = sdb->v;
  1517. int top = sq_gettop(v);
  1518. sq_pushobject(v, sdb->busy_cb);
  1519. sq_pushroottable(v);
  1520. sq_pushobject(v, sdb->busy_udata);
  1521. sq_pushinteger(v, tries);
  1522. /* call lua function */
  1523. if (sq_call(v, 3, SQTrue, SQFalse) == SQ_OK)
  1524. sq_getbool(v, -1, &retry);
  1525. sq_settop(v, top);
  1526. return retry == SQTrue;
  1527. }
  1528. static SQRESULT sq_sqlite3_busy_handler(HSQUIRRELVM v)
  1529. {
  1530. SQ_FUNC_VARS(v);
  1531. GET_sqlite3_INSTANCE();
  1532. if (_top_ < 2 || sq_gettype(v, 2) == OT_NULL)
  1533. {
  1534. sq_release(v, &sdb->busy_cb);
  1535. sq_release(v, &sdb->busy_udata);
  1536. sq_resetobject(&sdb->busy_cb);
  1537. sq_resetobject(&sdb->busy_udata);
  1538. /* clear busy handler */
  1539. sqlite3_busy_handler(self, NULL, NULL);
  1540. }
  1541. else
  1542. {
  1543. if(sq_gettype(v, 2) != OT_CLOSURE)
  1544. return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
  1545. sq_getstackobj(v, 2, &sdb->busy_cb);
  1546. sq_addref(v, &sdb->busy_cb);
  1547. if(_top_ > 2)
  1548. {
  1549. sq_getstackobj(v, 3, &sdb->busy_udata);
  1550. sq_addref(v, &sdb->busy_udata);
  1551. }
  1552. /* set busy callback */
  1553. sqlite3_busy_handler(self, db_busy_callback, sdb);
  1554. }
  1555. return 0;
  1556. }
  1557. static SQRESULT sq_sqlite3_busy_timeout(HSQUIRRELVM v)
  1558. {
  1559. SQ_FUNC_VARS_NO_TOP(v);
  1560. GET_sqlite3_INSTANCE();
  1561. SQ_GET_INTEGER(v, 2, timeout);
  1562. sqlite3_busy_timeout(self, timeout);
  1563. /* if there was a timeout callback registered, it is now
  1564. ** invalid/useless. free any references we may have */
  1565. sq_release(v, &sdb->busy_cb);
  1566. sq_resetobject(&sdb->busy_cb);
  1567. sq_release(v, &sdb->busy_udata);
  1568. sq_resetobject(&sdb->busy_udata);
  1569. return 0;
  1570. }
  1571. static SQRESULT sq_sqlite3_interrupt(HSQUIRRELVM v)
  1572. {
  1573. SQ_FUNC_VARS_NO_TOP(v);
  1574. GET_sqlite3_INSTANCE();
  1575. sqlite3_interrupt(self);
  1576. return 0;
  1577. }
  1578. /*
  1579. ** =======================================================
  1580. ** User Defined Functions - Context Methods
  1581. ** =======================================================
  1582. */
  1583. typedef struct
  1584. {
  1585. sqlite3_context *ctx;
  1586. HSQOBJECT udata;
  1587. } sq_sqlite3_context_st;
  1588. static const SQChar sq_sqlite3_context_TAG[] = _SC(":sqlite3:ctx");
  1589. static SQRESULT sq_sqlite3_context_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  1590. {
  1591. sq_sqlite3_context_st *ctx = ((sq_sqlite3_context_st *)p);
  1592. /* 'free' all references */
  1593. sq_free(ctx, sizeof(sq_sqlite3_context_st));
  1594. return 0;
  1595. }
  1596. static SQRESULT sq_sqlite3_context_constructor(HSQUIRRELVM v)
  1597. {
  1598. sq_sqlite3_context_st *ctx = (sq_sqlite3_context_st*)sq_malloc(sizeof(sq_sqlite3_context_st));
  1599. ctx->ctx = NULL;
  1600. sq_resetobject(&ctx->udata);
  1601. sq_setinstanceup(v, 1, ctx);
  1602. sq_setreleasehook(v,1, sq_sqlite3_context_releasehook);
  1603. return 1;
  1604. }
  1605. #define SQ_SQLITE3_GETCONTEXT(v, idx) \
  1606. sq_sqlite3_context_st *self;\
  1607. if((_rc_ = sq_getinstanceup(v, idx, (void**)&self, (void*)sq_sqlite3_context_TAG)) < 0) return _rc_;
  1608. #define GET_sqlite3_context_INSTANCE() SQ_SQLITE3_GETCONTEXT(v,1)
  1609. static SQRESULT sq_sqlite3_context__tostring(HSQUIRRELVM v)
  1610. {
  1611. SQ_FUNC_VARS_NO_TOP(v);
  1612. GET_sqlite3_context_INSTANCE();
  1613. char buff[64];
  1614. snprintf(buff, sizeof(buff), "sqlite function context (%p)", self);
  1615. sq_pushstring(v, buff, -1);
  1616. return 1;
  1617. }
  1618. static SQRESULT sq_sqlite3_context_check_aggregate(HSQUIRRELVM v, sq_sqlite3_context_st *ctx)
  1619. {
  1620. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(ctx->ctx);
  1621. if (sq_isclosure(func->fn_finalize))
  1622. {
  1623. return sq_throwerror(v, "attempt to call aggregate method from scalar function");
  1624. }
  1625. return 1;
  1626. }
  1627. static SQRESULT sq_sqlite3_context_user_data(HSQUIRRELVM v)
  1628. {
  1629. SQ_FUNC_VARS_NO_TOP(v);
  1630. GET_sqlite3_context_INSTANCE();
  1631. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(self->ctx);
  1632. sq_pushobject(v, func->udata);
  1633. return 1;
  1634. }
  1635. static SQRESULT sq_sqlite3_context_aggregate_data(HSQUIRRELVM v)
  1636. {
  1637. SQ_FUNC_VARS(v);
  1638. GET_sqlite3_context_INSTANCE();
  1639. sq_sqlite3_context_check_aggregate(v, self);
  1640. if(_top_ < 2)
  1641. {
  1642. sq_pushobject(v, self->udata);
  1643. }
  1644. else
  1645. {
  1646. sq_release(v, &self->udata);
  1647. sq_getstackobj(v, 2, &self->udata);
  1648. sq_addref(v, &self->udata);
  1649. }
  1650. return 0;
  1651. }
  1652. static SQRESULT sq_sqlite3_context_aggregate_count(HSQUIRRELVM v)
  1653. {
  1654. SQ_FUNC_VARS_NO_TOP(v);
  1655. GET_sqlite3_context_INSTANCE();
  1656. sq_sqlite3_context_check_aggregate(v, self);
  1657. sq_pushinteger(v, sqlite3_aggregate_count(self->ctx));
  1658. return 1;
  1659. }
  1660. #if 0
  1661. void *sqlite3_get_auxdata(sqlite3_context*, int);
  1662. void sqlite3_set_auxdata(sqlite3_context*, int, void*, void (*)(void*));
  1663. #endif
  1664. static SQRESULT sq_sqlite3_context_result_blob(HSQUIRRELVM v)
  1665. {
  1666. SQ_FUNC_VARS_NO_TOP(v);
  1667. GET_sqlite3_context_INSTANCE();
  1668. SQ_GET_STRING(v, 2, blob);
  1669. sqlite3_result_blob(self->ctx, (const void*)blob, blob_size, SQLITE_TRANSIENT);
  1670. return 0;
  1671. }
  1672. static SQRESULT sq_sqlite3_context_result_double(HSQUIRRELVM v)
  1673. {
  1674. SQ_FUNC_VARS_NO_TOP(v);
  1675. GET_sqlite3_context_INSTANCE();
  1676. SQ_GET_FLOAT(v, 2, d);
  1677. sqlite3_result_double(self->ctx, d);
  1678. return 0;
  1679. }
  1680. static SQRESULT sq_sqlite3_context_result_error(HSQUIRRELVM v)
  1681. {
  1682. SQ_FUNC_VARS_NO_TOP(v);
  1683. GET_sqlite3_context_INSTANCE();
  1684. SQ_GET_STRING(v,2, err);
  1685. sqlite3_result_error(self->ctx, err, err_size);
  1686. return 0;
  1687. }
  1688. static SQRESULT sq_sqlite3_context_result_int(HSQUIRRELVM v)
  1689. {
  1690. SQ_FUNC_VARS_NO_TOP(v);
  1691. GET_sqlite3_context_INSTANCE();
  1692. SQ_GET_INTEGER(v, 2, i);
  1693. sqlite3_result_int(self->ctx, i);
  1694. return 0;
  1695. }
  1696. static SQRESULT sq_sqlite3_context_result_null(HSQUIRRELVM v)
  1697. {
  1698. SQ_FUNC_VARS_NO_TOP(v);
  1699. GET_sqlite3_context_INSTANCE();
  1700. sqlite3_result_null(self->ctx);
  1701. return 0;
  1702. }
  1703. static SQRESULT sq_sqlite3_context_result_text(HSQUIRRELVM v)
  1704. {
  1705. SQ_FUNC_VARS_NO_TOP(v);
  1706. GET_sqlite3_context_INSTANCE();
  1707. SQ_GET_STRING(v, 2, text);
  1708. sqlite3_result_text(self->ctx, text, text_size, SQLITE_TRANSIENT);
  1709. return 0;
  1710. }
  1711. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_sqlite3_context_##name,nparams,tycheck}
  1712. static SQRegFunction sq_sqlite3_context_methods[] =
  1713. {
  1714. _DECL_FUNC(constructor, 1, _SC("x")),
  1715. _DECL_FUNC(user_data, 1, _SC("x")),
  1716. _DECL_FUNC(aggregate_data, -1, _SC("x.")),
  1717. _DECL_FUNC(aggregate_count, 1, _SC("x")),
  1718. _DECL_FUNC(result_null, 1, _SC("x")),
  1719. _DECL_FUNC(result_double, 2, _SC("xn")),
  1720. _DECL_FUNC(result_int, 2, _SC("xi")),
  1721. _DECL_FUNC(result_text, 2, _SC("xs")),
  1722. _DECL_FUNC(result_blob, 2, _SC("xs")),
  1723. _DECL_FUNC(result_error, 2, _SC("xs")),
  1724. _DECL_FUNC(_tostring, -1, _SC("x")),
  1725. {0,0}
  1726. };
  1727. #undef _DECL_FUNC
  1728. /*
  1729. ** callback functions used when calling registered sql functions
  1730. */
  1731. static void sqlite3_push_value(HSQUIRRELVM v, sqlite3_value *value)
  1732. {
  1733. const char *tmp_value;
  1734. switch (sqlite3_value_type(value))
  1735. {
  1736. case SQLITE_TEXT:
  1737. tmp_value = (const char*) sqlite3_value_text(value);
  1738. if(tmp_value) sq_pushstring(v, tmp_value, sqlite3_value_bytes(value));
  1739. else push_sqlite3_null(v);
  1740. break;
  1741. case SQLITE_INTEGER:
  1742. {
  1743. sqlite_int64 i64 = sqlite3_value_int64(value);
  1744. SQInteger n = (SQInteger)i64;
  1745. if (n == i64)
  1746. sq_pushinteger(v, n);
  1747. else
  1748. sq_pushstring(v, (const char*) sqlite3_value_text(value), sqlite3_value_bytes(value));
  1749. }
  1750. break;
  1751. case SQLITE_FLOAT:
  1752. sq_pushfloat(v, sqlite3_value_double(value));
  1753. break;
  1754. case SQLITE_BLOB:
  1755. tmp_value = (const char*) sqlite3_value_blob(value);
  1756. if(tmp_value) sq_pushstring(v, tmp_value, sqlite3_value_bytes(value));
  1757. else push_sqlite3_null(v);
  1758. break;
  1759. case SQLITE_NULL:
  1760. push_sqlite3_null(v);
  1761. break;
  1762. default:
  1763. /* things done properly (SQLite + Lua SQLite)
  1764. ** this should never happen */
  1765. push_sqlite3_null(v);
  1766. break;
  1767. }
  1768. }
  1769. //#if 0
  1770. static SQRESULT new_context_instance(HSQUIRRELVM v, sq_sqlite3_context_st **ctx)
  1771. {
  1772. sq_pushregistrytable(v);
  1773. sq_pushstring(v,_SC("sqite3_context"),-1);
  1774. int rc = sq_rawget(v, -2);
  1775. sq_remove(v, -2); //remove registrytable
  1776. sq_pushroottable(v);
  1777. rc = sq_call(v, 1, SQTrue, SQFalse);
  1778. sq_remove(v, -2); //class
  1779. rc = sq_getinstanceup(v, -1, (void**)ctx, (void*)sq_sqlite3_context_TAG);
  1780. return rc;
  1781. }
  1782. /* scalar function to be called
  1783. ** callback params: context, values... */
  1784. static void db_sql_normal_function(sqlite3_context *context, int argc, sqlite3_value **argv)
  1785. {
  1786. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(context);
  1787. HSQUIRRELVM v = func->sdb->v;
  1788. int n;
  1789. sq_sqlite3_context_st *ctx;
  1790. int top = sq_gettop(v);
  1791. /* ensure there is enough space in the stack */
  1792. sq_reservestack(v, argc + 5);
  1793. sq_pushobject(v, func->fn_step);
  1794. sq_pushroottable(v);
  1795. if (!sq_isclosure(func->fn_finalize))
  1796. {
  1797. new_context_instance(v, &ctx);
  1798. }
  1799. else
  1800. {
  1801. /* reuse context userdata value */
  1802. void *p = sqlite3_aggregate_context(context, 1);
  1803. /* i think it is OK to use assume that using a light user data
  1804. ** as an entry on LUA REGISTRY table will be unique */
  1805. sq_pushregistrytable(v);
  1806. sq_pushuserpointer(v, p);
  1807. /* context table */
  1808. if(sq_rawget(v, -2) != SQ_OK)
  1809. {
  1810. /* not yet created? */
  1811. sq_poptop(v); //remove null
  1812. new_context_instance(v, &ctx);
  1813. sq_pushuserpointer(v, p);
  1814. sq_push(v, -2);
  1815. sq_rawset(v, -4); //insert into registrytable
  1816. }
  1817. sq_remove(v, -2); //remove registrytable
  1818. }
  1819. /* push params */
  1820. for (n = 0; n < argc; ++n)
  1821. {
  1822. sqlite3_push_value(v, argv[n]);
  1823. }
  1824. /* set context */
  1825. ctx->ctx = context;
  1826. if (sq_call(v, argc + 2, SQFalse, SQFalse) != SQ_OK) //2 = roottable + ctx
  1827. {
  1828. sqlite3_result_error(context, sq_getlasterror_str(v), -1);
  1829. }
  1830. /* invalidate context */
  1831. ctx->ctx = NULL;
  1832. if (!sq_isclosure(func->fn_finalize))
  1833. {
  1834. sq_release(v, &ctx->udata);
  1835. sq_resetobject(&ctx->udata);
  1836. }
  1837. sq_settop(v, top);
  1838. }
  1839. static void db_sql_finalize_function(sqlite3_context *context)
  1840. {
  1841. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(context);
  1842. HSQUIRRELVM v = func->sdb->v;
  1843. void *p = sqlite3_aggregate_context(context, 1); /* minimal mem usage */
  1844. sq_sqlite3_context_st *ctx;
  1845. int top = sq_gettop(v);
  1846. sq_pushobject(v, func->fn_finalize);
  1847. sq_pushroottable(v);
  1848. /* i think it is OK to use assume that using a light user data
  1849. ** as an entry on LUA REGISTRY table will be unique */
  1850. sq_pushregistrytable(v);
  1851. sq_pushuserpointer(v, p);
  1852. /* remove it from registry but we'll use it last time here */
  1853. /* context table */
  1854. if(sq_deleteslot(v, -2, SQTrue) != SQ_OK)
  1855. {
  1856. /* not yet created? - shouldn't happen in finalize function */
  1857. sq_pop(v, 1);
  1858. new_context_instance(v, &ctx);
  1859. sq_pushuserpointer(v, p);
  1860. sq_push(v, -2);
  1861. sq_rawset(v, -4);
  1862. }
  1863. sq_remove(v, -2); //registrytable
  1864. /* set context */
  1865. ctx->ctx = context;
  1866. if (sq_call(v, 1, SQFalse, SQFalse) != SQ_OK)
  1867. {
  1868. sqlite3_result_error(context, sq_getlasterror_str(v), -1);
  1869. }
  1870. /* invalidate context */
  1871. ctx->ctx = NULL;
  1872. /* cleanup context */
  1873. sq_release(v, &ctx->udata);
  1874. sq_resetobject(&ctx->udata);
  1875. sq_settop(v, top);
  1876. }
  1877. /*
  1878. ** Register a normal function
  1879. ** Params: db, function name, number arguments, [ callback | step, finalize], user data
  1880. ** Returns: true on sucess
  1881. **
  1882. ** Normal function:
  1883. ** Params: context, params
  1884. **
  1885. ** Aggregate function:
  1886. ** Params of step: context, params
  1887. ** Params of finalize: context
  1888. */
  1889. static SQRESULT db_register_function(HSQUIRRELVM v, int aggregate)
  1890. {
  1891. SQ_FUNC_VARS(v);
  1892. GET_sqlite3_INSTANCE();
  1893. SQ_GET_STRING(v, 2, name);
  1894. SQ_GET_INTEGER(v, 3, nargs);
  1895. if(sq_gettype(v, 4) != OT_CLOSURE)
  1896. return sq_throwerror(v, "invalid parameter 3 expected closure");
  1897. if (aggregate)
  1898. {
  1899. if(sq_gettype(v,5) != OT_CLOSURE)
  1900. return sq_throwerror(v, "invalid parameter 4 expected closure");
  1901. }
  1902. sq_sqlite3_sdb_func *func;
  1903. /* maybe an alternative way to allocate memory should be used/avoided */
  1904. func = (sq_sqlite3_sdb_func*)sq_malloc(sizeof(sq_sqlite3_sdb_func));
  1905. memset(func, 0, sizeof(sq_sqlite3_sdb_func));
  1906. _rc_ = sqlite3_create_function(
  1907. self, name, nargs, SQLITE_UTF8, func,
  1908. aggregate ? NULL : db_sql_normal_function,
  1909. aggregate ? db_sql_normal_function : NULL,
  1910. aggregate ? db_sql_finalize_function : NULL
  1911. );
  1912. if (_rc_ == SQLITE_OK)
  1913. {
  1914. /* save registered function in db function list */
  1915. func->sdb = sdb;
  1916. func->next = sdb->func;
  1917. sdb->func = func;
  1918. /* save the setp/normal function callback */
  1919. sq_resetobject(&func->fn_step);
  1920. sq_getstackobj(v, 4, &func->fn_step);
  1921. sq_addref(v, &func->fn_step);
  1922. /* save the finalize function callback */
  1923. sq_resetobject(&func->fn_finalize);
  1924. if (aggregate)
  1925. {
  1926. sq_getstackobj(v, 5, &func->fn_finalize);
  1927. sq_addref(v, &func->fn_finalize);
  1928. }
  1929. /* save user data */
  1930. sq_resetobject(&func->udata);
  1931. int udata_idx = aggregate ? 6 : 5;
  1932. if(_top_ >= udata_idx)
  1933. {
  1934. sq_getstackobj(v, udata_idx, &func->udata);
  1935. sq_addref(v, &func->udata);
  1936. }
  1937. }
  1938. else
  1939. {
  1940. /* free allocated memory */
  1941. sq_free(func, sizeof(sq_sqlite3_sdb_func));
  1942. }
  1943. sq_pushbool(v, _rc_ == SQLITE_OK ? 1 : 0);
  1944. return 1;
  1945. }
  1946. //#endif
  1947. static SQRESULT sq_sqlite3_create_function(HSQUIRRELVM v)
  1948. {
  1949. return db_register_function(v, 0);
  1950. }
  1951. static SQRESULT sq_sqlite3_create_aggregate(HSQUIRRELVM v)
  1952. {
  1953. return db_register_function(v, 1);
  1954. }
  1955. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_sqlite3_##name,nparams,tycheck}
  1956. static SQRegFunction sq_sqlite3_methods[] =
  1957. {
  1958. _DECL_FUNC(constructor, -2, _SC("xsi")),
  1959. _DECL_FUNC(close, 1, _SC("x")),
  1960. _DECL_FUNC(db_ptr, 1, _SC("x")),
  1961. _DECL_FUNC(IsAutoCommitOn, 1, _SC("x")),
  1962. _DECL_FUNC(close, 1, _SC("x")),
  1963. _DECL_FUNC(version, 1, _SC("x")),
  1964. _DECL_FUNC(errcode, 1, _SC("x")),
  1965. _DECL_FUNC(errmsg, 1, _SC("x")),
  1966. _DECL_FUNC(sleep, 1, _SC("x")),
  1967. _DECL_FUNC(interrupt, 1, _SC("x")),
  1968. _DECL_FUNC(progress_handler, -2, _SC("x i|o c .")),
  1969. _DECL_FUNC(trace, -2, _SC("x c|o .")),
  1970. _DECL_FUNC(busy_handler, -2, _SC("x c|o .")),
  1971. _DECL_FUNC(busy_timeout, 2, _SC("xi")),
  1972. _DECL_FUNC(create_function, -4, _SC("xsic.")),
  1973. _DECL_FUNC(create_aggregate, -5, _SC("xsicc.")),
  1974. #ifndef WIN32
  1975. _DECL_FUNC(temp_directory, 2, _SC("xs")),
  1976. #endif
  1977. _DECL_FUNC(enable_shared_cache, 2, _SC("xb")),
  1978. _DECL_FUNC(changes, 1, _SC("x")),
  1979. _DECL_FUNC(exec, -2, _SC("xs")),
  1980. _DECL_FUNC(exec_dml, 2, _SC("xs")),
  1981. _DECL_FUNC(exec_get_first_row, 2, _SC("xs")),
  1982. _DECL_FUNC(exec_get_one, 2, _SC("xs")),
  1983. _DECL_FUNC(get_db_name, 1, _SC("x")),
  1984. _DECL_FUNC(last_row_id, 1, _SC("x")),
  1985. _DECL_FUNC(prepare, 2, _SC("xs")),
  1986. _DECL_FUNC(set_busy_timeout, -1, _SC("xi")),
  1987. _DECL_FUNC(total_changes, 1, _SC("x")),
  1988. #ifdef SQLITE_HAS_CODEC
  1989. _DECL_FUNC(key, 2, _SC("xs")),
  1990. _DECL_FUNC(rekey, 2, _SC("xs")),
  1991. #endif
  1992. {0,0}
  1993. };
  1994. #undef _DECL_FUNC
  1995. #define INT_CONST(v,num) sq_pushstring(v,_SC(#num),-1);sq_pushinteger(v,num);sq_newslot(v,-3,SQTrue);
  1996. #ifdef __cplusplus
  1997. extern "C" {
  1998. #endif
  1999. SQRESULT sqext_register_SQLite3(HSQUIRRELVM v)
  2000. {
  2001. sq_insertfunc(v, _SC("sle2vecOfvec"), sq_sle2vecOfvec, -2, _SC(". s|x a i"), SQTrue);
  2002. sq_insertfunc(v, _SC("get_sle_size"), sq_get_sle_size, -2, _SC(".i"), SQTrue);
  2003. sq_insertfunc(v, _SC("add2sle"), sq_add2sle, -3, _SC(".xsi"), SQTrue);
  2004. sq_pushconsttable(v);
  2005. #define INT_SLE_CONST(num) sq_pushstring(v,_SC("SLE_" #num),-1);sq_pushinteger(v,num);sq_newslot(v,-3,SQTrue);
  2006. INT_SLE_CONST(IBYTE1);
  2007. INT_SLE_CONST(IBYTE2);
  2008. INT_SLE_CONST(IBYTE3);
  2009. INT_SLE_CONST(IBYTE4);
  2010. INT_SLE_CONST(SIZE1BYTE);
  2011. INT_SLE_CONST(SIZE2BYTE);
  2012. INT_SLE_CONST(SIZE3BYTE);
  2013. INT_SLE_CONST(SIZE4BYTE);
  2014. INT_SLE_CONST(SLEMARK);
  2015. INT_SLE_CONST(SLEEND);
  2016. sq_poptop(v); //remove const table
  2017. HSQOBJECT sqlite3_NULL;
  2018. sq_pushregistrytable(v);
  2019. sq_pushstring(v, sqlite3_NULL_Name,-1);
  2020. sq_newuserdata(v, sizeof(void*));
  2021. sq_resetobject(&sqlite3_NULL);
  2022. sq_getstackobj(v, -1, &sqlite3_NULL);
  2023. sq_newtable(v);
  2024. sq_insert_reg_funcs(v, sqlite3_NULL_methods);
  2025. sq_setdelegate(v, -2);
  2026. sq_newslot(v,-3,SQTrue);
  2027. sq_pushstring(v,_SC("sqite3_context"),-1);
  2028. sq_newclass(v,SQFalse);
  2029. sq_settypetag(v,-1,(void*)sq_sqlite3_context_TAG);
  2030. sq_insert_reg_funcs(v, sq_sqlite3_context_methods);
  2031. sq_newslot(v,-3,SQTrue);
  2032. sq_poptop(v); //remove registrytable
  2033. sq_pushstring(v,SQLite3_TAG,-1);
  2034. sq_newclass(v,SQFalse);
  2035. sq_settypetag(v,-1,(void*)SQLite3_TAG);
  2036. sq_insert_reg_funcs(v, sq_sqlite3_methods);
  2037. INT_CONST(v,SQLITE_OPEN_CREATE);
  2038. INT_CONST(v,SQLITE_OPEN_READWRITE);
  2039. INT_CONST(v,SQLITE_OPEN_SHAREDCACHE);
  2040. INT_CONST(v,SQLITE_OPEN_SUBLATIN_NA_LIKE);
  2041. INT_CONST(v,SQLITE_OK);
  2042. INT_CONST(v,SQLITE_INTERRUPT);
  2043. //push sqlite3_NULL as a member
  2044. sq_pushstring(v, nullName,-1);
  2045. sq_pushobject(v, sqlite3_NULL);
  2046. sq_newslot(v,-3,SQTrue);
  2047. sq_newslot(v,-3,SQTrue);
  2048. sq_pushstring(v, SQLite3_Stmt_TAG,-1);
  2049. sq_newclass(v,SQFalse);
  2050. sq_settypetag(v,-1,(void*)SQLite3_Stmt_TAG);
  2051. sq_insert_reg_funcs(v, sq_sqlite3_stmt_methods);
  2052. INT_CONST(v,SQLITE_OK);
  2053. INT_CONST(v,SQLITE_ROW);
  2054. INT_CONST(v,SQLITE_DONE);
  2055. INT_CONST(v,AS_STRING_ALWAYS);
  2056. INT_CONST(v,NULL_AS_EMPTY_STR);
  2057. INT_CONST(v,WITH_COL_NAMES);
  2058. //push sqlite3_NULL as a member
  2059. sq_pushstring(v, nullName,-1);
  2060. sq_pushobject(v, sqlite3_NULL);
  2061. sq_newslot(v,-3,SQTrue);
  2062. sq_newslot(v,-3,SQTrue);
  2063. return 1;
  2064. }
  2065. #ifdef __cplusplus
  2066. }
  2067. #endif