sq_sqlite3.cpp 68 KB

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