sq_sqlite3.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339
  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. //do no close statements because garbage collector will do it
  1128. //on MacOSX we get segfaults finalizing statements here
  1129. //sqlite3_finalize(statement);
  1130. count++;
  1131. }
  1132. if (count) return sq_throwerror(v, _SC("closing database with %d statements not closed."), count);
  1133. if(sqlite3_close_v2(db) == SQLITE_OK)
  1134. {
  1135. rc = SQ_OK;
  1136. //remove waekref from registrytable
  1137. sq_pushregistrytable(sdb->v);
  1138. sq_pushuserpointer(sdb->v, db);
  1139. sq_deleteslot(sdb->v, -2, SQFalse);
  1140. sq_poptop(sdb->v);
  1141. if(sdb->func)
  1142. {
  1143. sq_sqlite3_sdb_func *func, *func_next;
  1144. func = sdb->func;
  1145. while (func)
  1146. {
  1147. func_next = func->next;
  1148. sq_release(sdb->v, &func->fn_step);
  1149. sq_release(sdb->v, &func->fn_finalize);
  1150. sq_release(sdb->v, &func->udata);
  1151. sq_free(func, sizeof(sq_sqlite3_sdb_func));
  1152. func = func_next;
  1153. }
  1154. sdb->func = NULL;
  1155. }
  1156. /* 'free' all references */
  1157. sq_release(sdb->v, &sdb->busy_cb);
  1158. sq_release(sdb->v, &sdb->busy_udata);
  1159. sq_release(sdb->v, &sdb->progress_cb);
  1160. sq_release(sdb->v, &sdb->progress_udata);
  1161. sq_release(sdb->v, &sdb->trace_cb);
  1162. sq_release(sdb->v, &sdb->trace_udata);
  1163. sq_release(sdb->v, &sdb->null_value);
  1164. sq_free(sdb, sizeof(sq_sqlite3_sdb));
  1165. }
  1166. else return sq_throwerror(v, sqlite3_errmsg(db));
  1167. }
  1168. return rc;
  1169. }
  1170. static SQRESULT sq_sqlite3_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  1171. {
  1172. sq_sqlite3_sdb *sdb = ((sq_sqlite3_sdb *)p);
  1173. sq_sqlite3_close_release(v, sdb);
  1174. return 0;
  1175. }
  1176. static SQRESULT sq_sqlite3_constructor(HSQUIRRELVM v)
  1177. {
  1178. SQ_FUNC_VARS(v);
  1179. SQ_GET_STRING(v, 2, dbname);
  1180. SQ_OPT_INTEGER(v, 3, flags, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_SUBLATIN_NA_LIKE);
  1181. sqlite3 *db;
  1182. int rc = sqlite3_open_v2(dbname, &db, flags, 0);
  1183. if(rc != SQLITE_OK) return sq_throwerror(v, "Failed to open database ! %d", rc);
  1184. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb *)sq_malloc(sizeof(sq_sqlite3_sdb));
  1185. memset(sdb, 0, sizeof(sq_sqlite3_sdb));
  1186. sdb->db = db;
  1187. sdb->v = v;
  1188. sq_resetobject(&sdb->busy_cb);
  1189. sq_resetobject(&sdb->busy_udata);
  1190. sq_resetobject(&sdb->progress_cb);
  1191. sq_resetobject(&sdb->progress_udata);
  1192. sq_resetobject(&sdb->trace_cb);
  1193. sq_resetobject(&sdb->trace_udata);
  1194. sq_resetobject(&sdb->null_value);
  1195. sq_setinstanceup(v, 1, sdb);
  1196. sq_setreleasehook(v,1, sq_sqlite3_releasehook);
  1197. //save a weakref to allow statement return it's db
  1198. sq_pushuserpointer(v, sdb->db);
  1199. sq_weakref(v, 1);
  1200. sq_setonregistrytable(v);
  1201. return 1;
  1202. }
  1203. static SQRESULT sq_sqlite3_close(HSQUIRRELVM v)
  1204. {
  1205. SQ_FUNC_VARS_NO_TOP(v);
  1206. GET_sqlite3_INSTANCE();
  1207. if(sq_sqlite3_close_release(v, sdb) != SQ_OK) return SQ_ERROR;
  1208. sq_setinstanceup(v, 1, 0); //next calls will fail with "database is closed"
  1209. return 0;
  1210. }
  1211. static SQRESULT sq_sqlite3_db_ptr(HSQUIRRELVM v)
  1212. {
  1213. SQ_FUNC_VARS_NO_TOP(v);
  1214. GET_sqlite3_INSTANCE();
  1215. sq_pushuserpointer(v, self);
  1216. return 1;
  1217. }
  1218. /* bool IsAutoCommitOn( ) */
  1219. static SQRESULT sq_sqlite3_IsAutoCommitOn(HSQUIRRELVM v)
  1220. {
  1221. SQ_FUNC_VARS_NO_TOP(v);
  1222. GET_sqlite3_INSTANCE();
  1223. sq_pushbool(v, sqlite3_get_autocommit(self) ? SQTrue : SQFalse);
  1224. return 1;
  1225. }
  1226. /* int changes( ) */
  1227. static SQRESULT sq_sqlite3_changes(HSQUIRRELVM v)
  1228. {
  1229. SQ_FUNC_VARS_NO_TOP(v);
  1230. GET_sqlite3_INSTANCE();
  1231. sq_pushinteger(v, sqlite3_total_changes(self));
  1232. return 1;
  1233. }
  1234. static SQRESULT sq_sqlite3_exec(HSQUIRRELVM v)
  1235. {
  1236. return sqlite3_exec_fmt(v, tr_all_rows);
  1237. }
  1238. /* int exec_dml( const char * szSQL ) */
  1239. static SQRESULT sq_sqlite3_exec_dml(HSQUIRRELVM v)
  1240. {
  1241. SQ_FUNC_VARS_NO_TOP(v);
  1242. GET_sqlite3_INSTANCE();
  1243. SQ_GET_STRING(v, 2, szSQL);
  1244. char *errmsg;
  1245. if(sqlite3_exec(self, szSQL, NULL, NULL, &errmsg) != SQLITE_OK){
  1246. sq_throwerror(v, errmsg);
  1247. sqlite3_free(errmsg);
  1248. return SQ_ERROR;
  1249. }
  1250. sq_pushinteger(v, sqlite3_changes(self));
  1251. return 1;
  1252. }
  1253. /* bool exec_get_one( std::string & result , const char * szSQL ) */
  1254. static SQRESULT sq_sqlite3_exec_get_one(HSQUIRRELVM v)
  1255. {
  1256. return sqlite3_exec_fmt(v, tr_first_row_first_col);
  1257. }
  1258. static SQRESULT sq_sqlite3_exec_get_first_row(HSQUIRRELVM v)
  1259. {
  1260. return sqlite3_exec_fmt(v, tr_first_row);
  1261. }
  1262. /* const char * get_db_name( ) */
  1263. static SQRESULT sq_sqlite3_get_db_name(HSQUIRRELVM v)
  1264. {
  1265. SQ_FUNC_VARS_NO_TOP(v);
  1266. GET_sqlite3_INSTANCE();
  1267. sq_pushstring(v, sqlite3_db_filename(self, "main"), -1);
  1268. return 1;
  1269. }
  1270. /* sqlite3_int64 last_row_id( ) */
  1271. static SQRESULT sq_sqlite3_last_row_id(HSQUIRRELVM v)
  1272. {
  1273. SQ_FUNC_VARS_NO_TOP(v);
  1274. GET_sqlite3_INSTANCE();
  1275. sq_pushinteger(v, sqlite3_last_insert_rowid(self));
  1276. return 1;
  1277. }
  1278. /* stmt * prepare( const char * sql ) */
  1279. static SQRESULT sq_sqlite3_prepare(HSQUIRRELVM v)
  1280. {
  1281. SQ_FUNC_VARS_NO_TOP(v);
  1282. //GET_sqlite3_INSTANCE();
  1283. SQ_GET_STRING(v, 2, sql);
  1284. sq_pushstring(v, SQLite3_Stmt_TAG, -1);
  1285. if(sq_getonroottable(v) == SQ_ERROR) return SQ_ERROR;
  1286. sq_pushroottable(v);
  1287. sq_push(v, 1);
  1288. sq_push(v, 2);
  1289. if(sq_call(v, 3, SQTrue, SQFalse) != SQ_OK) return SQ_ERROR;
  1290. return 1;
  1291. }
  1292. /* void set_busy_timeout( int nMillisecs ) */
  1293. static SQRESULT sq_sqlite3_set_busy_timeout(HSQUIRRELVM v)
  1294. {
  1295. SQ_FUNC_VARS_NO_TOP(v);
  1296. //GET_sqlite3_INSTANCE();
  1297. SQ_GET_INTEGER(v, 2, nMillisecs);
  1298. //self->set_busy_timeout(nMillisecs);
  1299. return 0;
  1300. }
  1301. /* int total_changes( ) */
  1302. static SQRESULT sq_sqlite3_total_changes(HSQUIRRELVM v)
  1303. {
  1304. SQ_FUNC_VARS_NO_TOP(v);
  1305. GET_sqlite3_INSTANCE();
  1306. sq_pushinteger(v, sqlite3_total_changes(self));
  1307. return 1;
  1308. }
  1309. static SQRESULT sq_sqlite3_enable_shared_cache(HSQUIRRELVM v)
  1310. {
  1311. SQ_FUNC_VARS_NO_TOP(v);
  1312. SQ_GET_BOOL(v, 2, bhow);
  1313. sq_pushbool(v, sqlite3_enable_shared_cache(bhow) == SQLITE_OK);
  1314. return 1;
  1315. }
  1316. static SQRESULT sq_sqlite3_sleep(HSQUIRRELVM v)
  1317. {
  1318. SQ_FUNC_VARS_NO_TOP(v);
  1319. SQ_GET_INTEGER(v, 2, n);
  1320. sqlite3_sleep(n);
  1321. return 0;
  1322. }
  1323. static SQRESULT sq_sqlite3_version(HSQUIRRELVM v)
  1324. {
  1325. sq_pushstring(v, sqlite3_libversion(), -1);
  1326. return 1;
  1327. }
  1328. static SQRESULT sq_sqlite3_errcode(HSQUIRRELVM v)
  1329. {
  1330. SQ_FUNC_VARS_NO_TOP(v);
  1331. GET_sqlite3_INSTANCE();
  1332. sq_pushinteger(v, sqlite3_errcode(self));
  1333. return 1;
  1334. }
  1335. static SQRESULT sq_sqlite3_errmsg(HSQUIRRELVM v)
  1336. {
  1337. SQ_FUNC_VARS_NO_TOP(v);
  1338. GET_sqlite3_INSTANCE();
  1339. sq_pushstring(v, sqlite3_errmsg(self), -1);
  1340. return 1;
  1341. }
  1342. #ifndef WIN32
  1343. static SQRESULT sq_sqlite3_temp_directory(HSQUIRRELVM v)
  1344. {
  1345. SQ_FUNC_VARS(v);
  1346. const char *oldtemp = sqlite3_temp_directory;
  1347. if (_top_ > 1)
  1348. {
  1349. SQ_GET_STRING(v, 2, temp);
  1350. if (sqlite3_temp_directory)
  1351. {
  1352. sqlite3_free((char*)sqlite3_temp_directory);
  1353. }
  1354. if (temp && temp_size)
  1355. {
  1356. sqlite3_temp_directory = sqlite3_mprintf("%s", temp);
  1357. }
  1358. else
  1359. {
  1360. sqlite3_temp_directory = NULL;
  1361. }
  1362. }
  1363. sq_pushstring(v, oldtemp, -1);
  1364. return 1;
  1365. }
  1366. #endif
  1367. #ifdef SQLITE_HAS_CODEC
  1368. //#include "codecext.h"
  1369. /*
  1370. ** Params: db, sql
  1371. ** returns: code, compiled length or error message
  1372. */
  1373. static SQRESULT sq_sqlite3_key(HSQUIRRELVM v)
  1374. {
  1375. SQ_FUNC_VARS_NO_TOP(v);
  1376. GET_sqlite3_INSTANCE();
  1377. SQ_GET_STRING(v, 2, key);
  1378. sq_pushinteger(v, sqlite3_key(self, key, key_size));
  1379. return 1;
  1380. }
  1381. /*
  1382. ** Params: db, sql
  1383. ** returns: code, compiled length or error message
  1384. */
  1385. static SQRESULT sq_sqlite3_rekey(HSQUIRRELVM v)
  1386. {
  1387. SQ_FUNC_VARS_NO_TOP(v);
  1388. GET_sqlite3_INSTANCE();
  1389. SQ_GET_STRING(v, 2, key);
  1390. sq_pushinteger(v, sqlite3_rekey(self, key, key_size));
  1391. return 1;
  1392. }
  1393. #endif
  1394. #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK
  1395. /*
  1396. ** progress handler:
  1397. ** Params: database, number of opcodes, callback function, userdata
  1398. **
  1399. ** callback function:
  1400. ** Params: userdata
  1401. ** returns: 0 to return immediatly and return SQLITE_ABORT, non-zero to continue
  1402. */
  1403. static int db_progress_callback(void *user)
  1404. {
  1405. SQInteger result = 1; /* abort by default */
  1406. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)user;
  1407. HSQUIRRELVM v = sdb->v;
  1408. int top = sq_gettop(v);
  1409. sq_pushobject(v, sdb->progress_cb);
  1410. sq_pushroottable(v); //this
  1411. sq_pushobject(v, sdb->progress_udata);
  1412. /* call lua function */
  1413. if (sq_call(v, 2, SQTrue, SQFalse) == SQ_OK)
  1414. sq_getinteger(v, -1, &result);
  1415. sq_settop(v, top);
  1416. return result;
  1417. }
  1418. static SQRESULT sq_sqlite3_progress_handler(HSQUIRRELVM v)
  1419. {
  1420. SQ_FUNC_VARS(v);
  1421. GET_sqlite3_INSTANCE();
  1422. if (_top_ < 2 || sq_gettype(v, 2) == OT_NULL)
  1423. {
  1424. sq_release(v, &sdb->progress_cb);
  1425. sq_release(v, &sdb->progress_udata);
  1426. sq_resetobject(&sdb->progress_cb);
  1427. sq_resetobject(&sdb->progress_udata);
  1428. /* clear busy handler */
  1429. sqlite3_progress_handler(self, 0, NULL, NULL);
  1430. }
  1431. else
  1432. {
  1433. SQ_GET_INTEGER(v, 2, nop);
  1434. if(sq_gettype(v, 3) != OT_CLOSURE)
  1435. return sq_throwerror(v, _SC("invalid second parameter expected closure"));
  1436. sq_getstackobj(v, 3, &sdb->progress_cb);
  1437. sq_addref(v, &sdb->progress_cb);
  1438. if(_top_ > 3)
  1439. {
  1440. sq_getstackobj(v, 4, &sdb->progress_udata);
  1441. sq_addref(v, &sdb->progress_udata);
  1442. }
  1443. /* set progress callback */
  1444. sqlite3_progress_handler(self, nop, db_progress_callback, sdb);
  1445. }
  1446. return 0;
  1447. }
  1448. #else /* #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK */
  1449. static SQRESULT sq_sqlite3_progress_handler(HSQUIRRELVM v)
  1450. {
  1451. return sq_throwerror(v, _SC("progress callback support disabled at compile time"));
  1452. return 0;
  1453. }
  1454. #endif /* #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK */
  1455. /*
  1456. ** trace callback:
  1457. ** Params: database, callback function, userdata
  1458. **
  1459. ** callback function:
  1460. ** Params: userdata, sql
  1461. */
  1462. static void db_trace_callback(void *user, const char *sql)
  1463. {
  1464. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)user;
  1465. HSQUIRRELVM v = sdb->v;
  1466. int top = sq_gettop(v);
  1467. /* setup squirrel callback call */
  1468. sq_pushobject(v, sdb->trace_cb);
  1469. sq_pushroottable(v);
  1470. sq_pushobject(v, sdb->trace_udata);
  1471. sq_pushstring(v, sql, -1); /* traced sql statement */
  1472. /* call squirrel function */
  1473. sq_call(v, 3, SQFalse, SQFalse);
  1474. /* ignore any error generated by this function */
  1475. sq_settop(v, top);
  1476. }
  1477. static SQRESULT sq_sqlite3_trace(HSQUIRRELVM v)
  1478. {
  1479. SQ_FUNC_VARS(v);
  1480. GET_sqlite3_INSTANCE();
  1481. if (_top_ < 2 || sq_gettype(v, 2) == OT_NULL)
  1482. {
  1483. sq_release(v, &sdb->trace_cb);
  1484. sq_release(v, &sdb->trace_udata);
  1485. sq_resetobject(&sdb->trace_cb);
  1486. sq_resetobject(&sdb->trace_udata);
  1487. /* clear trace handler */
  1488. sqlite3_trace(self, NULL, NULL);
  1489. }
  1490. else
  1491. {
  1492. if(sq_gettype(v, 2) != OT_CLOSURE)
  1493. return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
  1494. sq_getstackobj(v, 2, &sdb->trace_cb);
  1495. sq_addref(v, &sdb->trace_cb);
  1496. if(_top_ > 2)
  1497. {
  1498. sq_getstackobj(v, 3, &sdb->trace_udata);
  1499. sq_addref(v, &sdb->trace_udata);
  1500. }
  1501. /* set trace callback */
  1502. sqlite3_trace(self, db_trace_callback, sdb);
  1503. }
  1504. return 0;
  1505. }
  1506. /*
  1507. ** busy handler:
  1508. ** Params: database, callback function, userdata
  1509. **
  1510. ** callback function:
  1511. ** Params: userdata, number of tries
  1512. ** returns: 0 to return immediatly and return SQLITE_BUSY, non-zero to try again
  1513. */
  1514. static int db_busy_callback(void *user, int tries)
  1515. {
  1516. SQBool retry = SQFalse; /* abort by default */
  1517. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)user;
  1518. HSQUIRRELVM v = sdb->v;
  1519. int top = sq_gettop(v);
  1520. sq_pushobject(v, sdb->busy_cb);
  1521. sq_pushroottable(v);
  1522. sq_pushobject(v, sdb->busy_udata);
  1523. sq_pushinteger(v, tries);
  1524. /* call lua function */
  1525. if (sq_call(v, 3, SQTrue, SQFalse) == SQ_OK)
  1526. sq_getbool(v, -1, &retry);
  1527. sq_settop(v, top);
  1528. return retry == SQTrue;
  1529. }
  1530. static SQRESULT sq_sqlite3_busy_handler(HSQUIRRELVM v)
  1531. {
  1532. SQ_FUNC_VARS(v);
  1533. GET_sqlite3_INSTANCE();
  1534. if (_top_ < 2 || sq_gettype(v, 2) == OT_NULL)
  1535. {
  1536. sq_release(v, &sdb->busy_cb);
  1537. sq_release(v, &sdb->busy_udata);
  1538. sq_resetobject(&sdb->busy_cb);
  1539. sq_resetobject(&sdb->busy_udata);
  1540. /* clear busy handler */
  1541. sqlite3_busy_handler(self, NULL, NULL);
  1542. }
  1543. else
  1544. {
  1545. if(sq_gettype(v, 2) != OT_CLOSURE)
  1546. return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
  1547. sq_getstackobj(v, 2, &sdb->busy_cb);
  1548. sq_addref(v, &sdb->busy_cb);
  1549. if(_top_ > 2)
  1550. {
  1551. sq_getstackobj(v, 3, &sdb->busy_udata);
  1552. sq_addref(v, &sdb->busy_udata);
  1553. }
  1554. /* set busy callback */
  1555. sqlite3_busy_handler(self, db_busy_callback, sdb);
  1556. }
  1557. return 0;
  1558. }
  1559. static SQRESULT sq_sqlite3_busy_timeout(HSQUIRRELVM v)
  1560. {
  1561. SQ_FUNC_VARS_NO_TOP(v);
  1562. GET_sqlite3_INSTANCE();
  1563. SQ_GET_INTEGER(v, 2, timeout);
  1564. sqlite3_busy_timeout(self, timeout);
  1565. /* if there was a timeout callback registered, it is now
  1566. ** invalid/useless. free any references we may have */
  1567. sq_release(v, &sdb->busy_cb);
  1568. sq_resetobject(&sdb->busy_cb);
  1569. sq_release(v, &sdb->busy_udata);
  1570. sq_resetobject(&sdb->busy_udata);
  1571. return 0;
  1572. }
  1573. static SQRESULT sq_sqlite3_interrupt(HSQUIRRELVM v)
  1574. {
  1575. SQ_FUNC_VARS_NO_TOP(v);
  1576. GET_sqlite3_INSTANCE();
  1577. sqlite3_interrupt(self);
  1578. return 0;
  1579. }
  1580. /*
  1581. ** =======================================================
  1582. ** User Defined Functions - Context Methods
  1583. ** =======================================================
  1584. */
  1585. typedef struct
  1586. {
  1587. sqlite3_context *ctx;
  1588. HSQOBJECT udata;
  1589. } sq_sqlite3_context_st;
  1590. static const SQChar sq_sqlite3_context_TAG[] = _SC(":sqlite3:ctx");
  1591. static SQRESULT sq_sqlite3_context_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  1592. {
  1593. sq_sqlite3_context_st *ctx = ((sq_sqlite3_context_st *)p);
  1594. /* 'free' all references */
  1595. sq_free(ctx, sizeof(sq_sqlite3_context_st));
  1596. return 0;
  1597. }
  1598. static SQRESULT sq_sqlite3_context_constructor(HSQUIRRELVM v)
  1599. {
  1600. sq_sqlite3_context_st *ctx = (sq_sqlite3_context_st*)sq_malloc(sizeof(sq_sqlite3_context_st));
  1601. ctx->ctx = NULL;
  1602. sq_resetobject(&ctx->udata);
  1603. sq_setinstanceup(v, 1, ctx);
  1604. sq_setreleasehook(v,1, sq_sqlite3_context_releasehook);
  1605. return 1;
  1606. }
  1607. #define SQ_SQLITE3_GETCONTEXT(v, idx) \
  1608. sq_sqlite3_context_st *self;\
  1609. if((_rc_ = sq_getinstanceup(v, idx, (void**)&self, (void*)sq_sqlite3_context_TAG)) < 0) return _rc_;
  1610. #define GET_sqlite3_context_INSTANCE() SQ_SQLITE3_GETCONTEXT(v,1)
  1611. static SQRESULT sq_sqlite3_context__tostring(HSQUIRRELVM v)
  1612. {
  1613. SQ_FUNC_VARS_NO_TOP(v);
  1614. GET_sqlite3_context_INSTANCE();
  1615. char buff[64];
  1616. snprintf(buff, sizeof(buff), "sqlite function context (%p)", self);
  1617. sq_pushstring(v, buff, -1);
  1618. return 1;
  1619. }
  1620. static SQRESULT sq_sqlite3_context_check_aggregate(HSQUIRRELVM v, sq_sqlite3_context_st *ctx)
  1621. {
  1622. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(ctx->ctx);
  1623. if (sq_isclosure(func->fn_finalize))
  1624. {
  1625. return sq_throwerror(v, "attempt to call aggregate method from scalar function");
  1626. }
  1627. return 1;
  1628. }
  1629. static SQRESULT sq_sqlite3_context_user_data(HSQUIRRELVM v)
  1630. {
  1631. SQ_FUNC_VARS_NO_TOP(v);
  1632. GET_sqlite3_context_INSTANCE();
  1633. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(self->ctx);
  1634. sq_pushobject(v, func->udata);
  1635. return 1;
  1636. }
  1637. static SQRESULT sq_sqlite3_context_aggregate_data(HSQUIRRELVM v)
  1638. {
  1639. SQ_FUNC_VARS(v);
  1640. GET_sqlite3_context_INSTANCE();
  1641. sq_sqlite3_context_check_aggregate(v, self);
  1642. if(_top_ < 2)
  1643. {
  1644. sq_pushobject(v, self->udata);
  1645. }
  1646. else
  1647. {
  1648. sq_release(v, &self->udata);
  1649. sq_getstackobj(v, 2, &self->udata);
  1650. sq_addref(v, &self->udata);
  1651. }
  1652. return 0;
  1653. }
  1654. static SQRESULT sq_sqlite3_context_aggregate_count(HSQUIRRELVM v)
  1655. {
  1656. SQ_FUNC_VARS_NO_TOP(v);
  1657. GET_sqlite3_context_INSTANCE();
  1658. sq_sqlite3_context_check_aggregate(v, self);
  1659. sq_pushinteger(v, sqlite3_aggregate_count(self->ctx));
  1660. return 1;
  1661. }
  1662. #if 0
  1663. void *sqlite3_get_auxdata(sqlite3_context*, int);
  1664. void sqlite3_set_auxdata(sqlite3_context*, int, void*, void (*)(void*));
  1665. #endif
  1666. static SQRESULT sq_sqlite3_context_result_blob(HSQUIRRELVM v)
  1667. {
  1668. SQ_FUNC_VARS_NO_TOP(v);
  1669. GET_sqlite3_context_INSTANCE();
  1670. SQ_GET_STRING(v, 2, blob);
  1671. sqlite3_result_blob(self->ctx, (const void*)blob, blob_size, SQLITE_TRANSIENT);
  1672. return 0;
  1673. }
  1674. static SQRESULT sq_sqlite3_context_result_double(HSQUIRRELVM v)
  1675. {
  1676. SQ_FUNC_VARS_NO_TOP(v);
  1677. GET_sqlite3_context_INSTANCE();
  1678. SQ_GET_FLOAT(v, 2, d);
  1679. sqlite3_result_double(self->ctx, d);
  1680. return 0;
  1681. }
  1682. static SQRESULT sq_sqlite3_context_result_error(HSQUIRRELVM v)
  1683. {
  1684. SQ_FUNC_VARS_NO_TOP(v);
  1685. GET_sqlite3_context_INSTANCE();
  1686. SQ_GET_STRING(v,2, err);
  1687. sqlite3_result_error(self->ctx, err, err_size);
  1688. return 0;
  1689. }
  1690. static SQRESULT sq_sqlite3_context_result_int(HSQUIRRELVM v)
  1691. {
  1692. SQ_FUNC_VARS_NO_TOP(v);
  1693. GET_sqlite3_context_INSTANCE();
  1694. SQ_GET_INTEGER(v, 2, i);
  1695. sqlite3_result_int(self->ctx, i);
  1696. return 0;
  1697. }
  1698. static SQRESULT sq_sqlite3_context_result_null(HSQUIRRELVM v)
  1699. {
  1700. SQ_FUNC_VARS_NO_TOP(v);
  1701. GET_sqlite3_context_INSTANCE();
  1702. sqlite3_result_null(self->ctx);
  1703. return 0;
  1704. }
  1705. static SQRESULT sq_sqlite3_context_result_text(HSQUIRRELVM v)
  1706. {
  1707. SQ_FUNC_VARS_NO_TOP(v);
  1708. GET_sqlite3_context_INSTANCE();
  1709. SQ_GET_STRING(v, 2, text);
  1710. sqlite3_result_text(self->ctx, text, text_size, SQLITE_TRANSIENT);
  1711. return 0;
  1712. }
  1713. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_sqlite3_context_##name,nparams,tycheck}
  1714. static SQRegFunction sq_sqlite3_context_methods[] =
  1715. {
  1716. _DECL_FUNC(constructor, 1, _SC("x")),
  1717. _DECL_FUNC(user_data, 1, _SC("x")),
  1718. _DECL_FUNC(aggregate_data, -1, _SC("x.")),
  1719. _DECL_FUNC(aggregate_count, 1, _SC("x")),
  1720. _DECL_FUNC(result_null, 1, _SC("x")),
  1721. _DECL_FUNC(result_double, 2, _SC("xn")),
  1722. _DECL_FUNC(result_int, 2, _SC("xi")),
  1723. _DECL_FUNC(result_text, 2, _SC("xs")),
  1724. _DECL_FUNC(result_blob, 2, _SC("xs")),
  1725. _DECL_FUNC(result_error, 2, _SC("xs")),
  1726. _DECL_FUNC(_tostring, -1, _SC("x")),
  1727. {0,0}
  1728. };
  1729. #undef _DECL_FUNC
  1730. /*
  1731. ** callback functions used when calling registered sql functions
  1732. */
  1733. static void sqlite3_push_value(HSQUIRRELVM v, sqlite3_value *value)
  1734. {
  1735. const char *tmp_value;
  1736. switch (sqlite3_value_type(value))
  1737. {
  1738. case SQLITE_TEXT:
  1739. tmp_value = (const char*) sqlite3_value_text(value);
  1740. if(tmp_value) sq_pushstring(v, tmp_value, sqlite3_value_bytes(value));
  1741. else push_sqlite3_null(v);
  1742. break;
  1743. case SQLITE_INTEGER:
  1744. {
  1745. sqlite_int64 i64 = sqlite3_value_int64(value);
  1746. SQInteger n = (SQInteger)i64;
  1747. if (n == i64)
  1748. sq_pushinteger(v, n);
  1749. else
  1750. sq_pushstring(v, (const char*) sqlite3_value_text(value), sqlite3_value_bytes(value));
  1751. }
  1752. break;
  1753. case SQLITE_FLOAT:
  1754. sq_pushfloat(v, sqlite3_value_double(value));
  1755. break;
  1756. case SQLITE_BLOB:
  1757. tmp_value = (const char*) sqlite3_value_blob(value);
  1758. if(tmp_value) sq_pushstring(v, tmp_value, sqlite3_value_bytes(value));
  1759. else push_sqlite3_null(v);
  1760. break;
  1761. case SQLITE_NULL:
  1762. push_sqlite3_null(v);
  1763. break;
  1764. default:
  1765. /* things done properly (SQLite + Lua SQLite)
  1766. ** this should never happen */
  1767. push_sqlite3_null(v);
  1768. break;
  1769. }
  1770. }
  1771. //#if 0
  1772. static SQRESULT new_context_instance(HSQUIRRELVM v, sq_sqlite3_context_st **ctx)
  1773. {
  1774. sq_pushregistrytable(v);
  1775. sq_pushstring(v,_SC("sqite3_context"),-1);
  1776. int rc = sq_rawget(v, -2);
  1777. sq_remove(v, -2); //remove registrytable
  1778. sq_pushroottable(v);
  1779. rc = sq_call(v, 1, SQTrue, SQFalse);
  1780. sq_remove(v, -2); //class
  1781. rc = sq_getinstanceup(v, -1, (void**)ctx, (void*)sq_sqlite3_context_TAG);
  1782. return rc;
  1783. }
  1784. /* scalar function to be called
  1785. ** callback params: context, values... */
  1786. static void db_sql_normal_function(sqlite3_context *context, int argc, sqlite3_value **argv)
  1787. {
  1788. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(context);
  1789. HSQUIRRELVM v = func->sdb->v;
  1790. int n;
  1791. sq_sqlite3_context_st *ctx;
  1792. int top = sq_gettop(v);
  1793. /* ensure there is enough space in the stack */
  1794. sq_reservestack(v, argc + 5);
  1795. sq_pushobject(v, func->fn_step);
  1796. sq_pushroottable(v);
  1797. if (!sq_isclosure(func->fn_finalize))
  1798. {
  1799. new_context_instance(v, &ctx);
  1800. }
  1801. else
  1802. {
  1803. /* reuse context userdata value */
  1804. void *p = sqlite3_aggregate_context(context, 1);
  1805. /* i think it is OK to use assume that using a light user data
  1806. ** as an entry on LUA REGISTRY table will be unique */
  1807. sq_pushregistrytable(v);
  1808. sq_pushuserpointer(v, p);
  1809. /* context table */
  1810. if(sq_rawget(v, -2) != SQ_OK)
  1811. {
  1812. /* not yet created? */
  1813. sq_poptop(v); //remove null
  1814. new_context_instance(v, &ctx);
  1815. sq_pushuserpointer(v, p);
  1816. sq_push(v, -2);
  1817. sq_rawset(v, -4); //insert into registrytable
  1818. }
  1819. sq_remove(v, -2); //remove registrytable
  1820. }
  1821. /* push params */
  1822. for (n = 0; n < argc; ++n)
  1823. {
  1824. sqlite3_push_value(v, argv[n]);
  1825. }
  1826. /* set context */
  1827. ctx->ctx = context;
  1828. if (sq_call(v, argc + 2, SQFalse, SQFalse) != SQ_OK) //2 = roottable + ctx
  1829. {
  1830. sqlite3_result_error(context, sq_getlasterror_str(v), -1);
  1831. }
  1832. /* invalidate context */
  1833. ctx->ctx = NULL;
  1834. if (!sq_isclosure(func->fn_finalize))
  1835. {
  1836. sq_release(v, &ctx->udata);
  1837. sq_resetobject(&ctx->udata);
  1838. }
  1839. sq_settop(v, top);
  1840. }
  1841. static void db_sql_finalize_function(sqlite3_context *context)
  1842. {
  1843. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(context);
  1844. HSQUIRRELVM v = func->sdb->v;
  1845. void *p = sqlite3_aggregate_context(context, 1); /* minimal mem usage */
  1846. sq_sqlite3_context_st *ctx;
  1847. int top = sq_gettop(v);
  1848. sq_pushobject(v, func->fn_finalize);
  1849. sq_pushroottable(v);
  1850. /* i think it is OK to use assume that using a light user data
  1851. ** as an entry on LUA REGISTRY table will be unique */
  1852. sq_pushregistrytable(v);
  1853. sq_pushuserpointer(v, p);
  1854. /* remove it from registry but we'll use it last time here */
  1855. /* context table */
  1856. if(sq_deleteslot(v, -2, SQTrue) != SQ_OK)
  1857. {
  1858. /* not yet created? - shouldn't happen in finalize function */
  1859. sq_pop(v, 1);
  1860. new_context_instance(v, &ctx);
  1861. sq_pushuserpointer(v, p);
  1862. sq_push(v, -2);
  1863. sq_rawset(v, -4);
  1864. }
  1865. sq_remove(v, -2); //registrytable
  1866. /* set context */
  1867. ctx->ctx = context;
  1868. if (sq_call(v, 1, SQFalse, SQFalse) != SQ_OK)
  1869. {
  1870. sqlite3_result_error(context, sq_getlasterror_str(v), -1);
  1871. }
  1872. /* invalidate context */
  1873. ctx->ctx = NULL;
  1874. /* cleanup context */
  1875. sq_release(v, &ctx->udata);
  1876. sq_resetobject(&ctx->udata);
  1877. sq_settop(v, top);
  1878. }
  1879. /*
  1880. ** Register a normal function
  1881. ** Params: db, function name, number arguments, [ callback | step, finalize], user data
  1882. ** Returns: true on sucess
  1883. **
  1884. ** Normal function:
  1885. ** Params: context, params
  1886. **
  1887. ** Aggregate function:
  1888. ** Params of step: context, params
  1889. ** Params of finalize: context
  1890. */
  1891. static SQRESULT db_register_function(HSQUIRRELVM v, int aggregate)
  1892. {
  1893. SQ_FUNC_VARS(v);
  1894. GET_sqlite3_INSTANCE();
  1895. SQ_GET_STRING(v, 2, name);
  1896. SQ_GET_INTEGER(v, 3, nargs);
  1897. if(sq_gettype(v, 4) != OT_CLOSURE)
  1898. return sq_throwerror(v, "invalid parameter 3 expected closure");
  1899. if (aggregate)
  1900. {
  1901. if(sq_gettype(v,5) != OT_CLOSURE)
  1902. return sq_throwerror(v, "invalid parameter 4 expected closure");
  1903. }
  1904. sq_sqlite3_sdb_func *func;
  1905. /* maybe an alternative way to allocate memory should be used/avoided */
  1906. func = (sq_sqlite3_sdb_func*)sq_malloc(sizeof(sq_sqlite3_sdb_func));
  1907. memset(func, 0, sizeof(sq_sqlite3_sdb_func));
  1908. _rc_ = sqlite3_create_function(
  1909. self, name, nargs, SQLITE_UTF8, func,
  1910. aggregate ? NULL : db_sql_normal_function,
  1911. aggregate ? db_sql_normal_function : NULL,
  1912. aggregate ? db_sql_finalize_function : NULL
  1913. );
  1914. if (_rc_ == SQLITE_OK)
  1915. {
  1916. /* save registered function in db function list */
  1917. func->sdb = sdb;
  1918. func->next = sdb->func;
  1919. sdb->func = func;
  1920. /* save the setp/normal function callback */
  1921. sq_resetobject(&func->fn_step);
  1922. sq_getstackobj(v, 4, &func->fn_step);
  1923. sq_addref(v, &func->fn_step);
  1924. /* save the finalize function callback */
  1925. sq_resetobject(&func->fn_finalize);
  1926. if (aggregate)
  1927. {
  1928. sq_getstackobj(v, 5, &func->fn_finalize);
  1929. sq_addref(v, &func->fn_finalize);
  1930. }
  1931. /* save user data */
  1932. sq_resetobject(&func->udata);
  1933. int udata_idx = aggregate ? 6 : 5;
  1934. if(_top_ >= udata_idx)
  1935. {
  1936. sq_getstackobj(v, udata_idx, &func->udata);
  1937. sq_addref(v, &func->udata);
  1938. }
  1939. }
  1940. else
  1941. {
  1942. /* free allocated memory */
  1943. sq_free(func, sizeof(sq_sqlite3_sdb_func));
  1944. }
  1945. sq_pushbool(v, _rc_ == SQLITE_OK ? 1 : 0);
  1946. return 1;
  1947. }
  1948. //#endif
  1949. static SQRESULT sq_sqlite3_create_function(HSQUIRRELVM v)
  1950. {
  1951. return db_register_function(v, 0);
  1952. }
  1953. static SQRESULT sq_sqlite3_create_aggregate(HSQUIRRELVM v)
  1954. {
  1955. return db_register_function(v, 1);
  1956. }
  1957. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_sqlite3_##name,nparams,tycheck}
  1958. static SQRegFunction sq_sqlite3_methods[] =
  1959. {
  1960. _DECL_FUNC(constructor, -2, _SC("xsi")),
  1961. _DECL_FUNC(close, 1, _SC("x")),
  1962. _DECL_FUNC(db_ptr, 1, _SC("x")),
  1963. _DECL_FUNC(IsAutoCommitOn, 1, _SC("x")),
  1964. _DECL_FUNC(close, 1, _SC("x")),
  1965. _DECL_FUNC(version, 1, _SC("x")),
  1966. _DECL_FUNC(errcode, 1, _SC("x")),
  1967. _DECL_FUNC(errmsg, 1, _SC("x")),
  1968. _DECL_FUNC(sleep, 1, _SC("x")),
  1969. _DECL_FUNC(interrupt, 1, _SC("x")),
  1970. _DECL_FUNC(progress_handler, -2, _SC("x i|o c .")),
  1971. _DECL_FUNC(trace, -2, _SC("x c|o .")),
  1972. _DECL_FUNC(busy_handler, -2, _SC("x c|o .")),
  1973. _DECL_FUNC(busy_timeout, 2, _SC("xi")),
  1974. _DECL_FUNC(create_function, -4, _SC("xsic.")),
  1975. _DECL_FUNC(create_aggregate, -5, _SC("xsicc.")),
  1976. #ifndef WIN32
  1977. _DECL_FUNC(temp_directory, 2, _SC("xs")),
  1978. #endif
  1979. _DECL_FUNC(enable_shared_cache, 2, _SC("xb")),
  1980. _DECL_FUNC(changes, 1, _SC("x")),
  1981. _DECL_FUNC(exec, -2, _SC("xs")),
  1982. _DECL_FUNC(exec_dml, 2, _SC("xs")),
  1983. _DECL_FUNC(exec_get_first_row, 2, _SC("xs")),
  1984. _DECL_FUNC(exec_get_one, 2, _SC("xs")),
  1985. _DECL_FUNC(get_db_name, 1, _SC("x")),
  1986. _DECL_FUNC(last_row_id, 1, _SC("x")),
  1987. _DECL_FUNC(prepare, 2, _SC("xs")),
  1988. _DECL_FUNC(set_busy_timeout, -1, _SC("xi")),
  1989. _DECL_FUNC(total_changes, 1, _SC("x")),
  1990. #ifdef SQLITE_HAS_CODEC
  1991. _DECL_FUNC(key, 2, _SC("xs")),
  1992. _DECL_FUNC(rekey, 2, _SC("xs")),
  1993. #endif
  1994. {0,0}
  1995. };
  1996. #undef _DECL_FUNC
  1997. #define INT_CONST(v,num) sq_pushstring(v,_SC(#num),-1);sq_pushinteger(v,num);sq_newslot(v,-3,SQTrue);
  1998. #ifdef __cplusplus
  1999. extern "C" {
  2000. #endif
  2001. SQRESULT sqext_register_SQLite3(HSQUIRRELVM v)
  2002. {
  2003. sq_insertfunc(v, _SC("sle2vecOfvec"), sq_sle2vecOfvec, -2, _SC(". s|x a i"), SQTrue);
  2004. sq_insertfunc(v, _SC("get_sle_size"), sq_get_sle_size, -2, _SC(".i"), SQTrue);
  2005. sq_insertfunc(v, _SC("add2sle"), sq_add2sle, -3, _SC(".xsi"), SQTrue);
  2006. sq_pushconsttable(v);
  2007. #define INT_SLE_CONST(num) sq_pushstring(v,_SC("SLE_" #num),-1);sq_pushinteger(v,num);sq_newslot(v,-3,SQTrue);
  2008. INT_SLE_CONST(IBYTE1);
  2009. INT_SLE_CONST(IBYTE2);
  2010. INT_SLE_CONST(IBYTE3);
  2011. INT_SLE_CONST(IBYTE4);
  2012. INT_SLE_CONST(SIZE1BYTE);
  2013. INT_SLE_CONST(SIZE2BYTE);
  2014. INT_SLE_CONST(SIZE3BYTE);
  2015. INT_SLE_CONST(SIZE4BYTE);
  2016. INT_SLE_CONST(SLEMARK);
  2017. INT_SLE_CONST(SLEEND);
  2018. sq_poptop(v); //remove const table
  2019. HSQOBJECT sqlite3_NULL;
  2020. sq_pushregistrytable(v);
  2021. sq_pushstring(v, sqlite3_NULL_Name,-1);
  2022. sq_newuserdata(v, sizeof(void*));
  2023. sq_resetobject(&sqlite3_NULL);
  2024. sq_getstackobj(v, -1, &sqlite3_NULL);
  2025. sq_newtable(v);
  2026. sq_insert_reg_funcs(v, sqlite3_NULL_methods);
  2027. sq_setdelegate(v, -2);
  2028. sq_newslot(v,-3,SQTrue);
  2029. sq_pushstring(v,_SC("sqite3_context"),-1);
  2030. sq_newclass(v,SQFalse);
  2031. sq_settypetag(v,-1,(void*)sq_sqlite3_context_TAG);
  2032. sq_insert_reg_funcs(v, sq_sqlite3_context_methods);
  2033. sq_newslot(v,-3,SQTrue);
  2034. sq_poptop(v); //remove registrytable
  2035. sq_pushstring(v,SQLite3_TAG,-1);
  2036. sq_newclass(v,SQFalse);
  2037. sq_settypetag(v,-1,(void*)SQLite3_TAG);
  2038. sq_insert_reg_funcs(v, sq_sqlite3_methods);
  2039. INT_CONST(v,SQLITE_OPEN_CREATE);
  2040. INT_CONST(v,SQLITE_OPEN_READWRITE);
  2041. INT_CONST(v,SQLITE_OPEN_SHAREDCACHE);
  2042. INT_CONST(v,SQLITE_OPEN_SUBLATIN_NA_LIKE);
  2043. INT_CONST(v,SQLITE_OK);
  2044. INT_CONST(v,SQLITE_INTERRUPT);
  2045. //push sqlite3_NULL as a member
  2046. sq_pushstring(v, nullName,-1);
  2047. sq_pushobject(v, sqlite3_NULL);
  2048. sq_newslot(v,-3,SQTrue);
  2049. sq_newslot(v,-3,SQTrue);
  2050. sq_pushstring(v, SQLite3_Stmt_TAG,-1);
  2051. sq_newclass(v,SQFalse);
  2052. sq_settypetag(v,-1,(void*)SQLite3_Stmt_TAG);
  2053. sq_insert_reg_funcs(v, sq_sqlite3_stmt_methods);
  2054. INT_CONST(v,SQLITE_OK);
  2055. INT_CONST(v,SQLITE_ROW);
  2056. INT_CONST(v,SQLITE_DONE);
  2057. INT_CONST(v,AS_STRING_ALWAYS);
  2058. INT_CONST(v,NULL_AS_EMPTY_STR);
  2059. INT_CONST(v,WITH_COL_NAMES);
  2060. //push sqlite3_NULL as a member
  2061. sq_pushstring(v, nullName,-1);
  2062. sq_pushobject(v, sqlite3_NULL);
  2063. sq_newslot(v,-3,SQTrue);
  2064. sq_newslot(v,-3,SQTrue);
  2065. return 1;
  2066. }
  2067. #ifdef __cplusplus
  2068. }
  2069. #endif