sq_sqlite3.cpp 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927
  1. /* Code generated by script */
  2. #include "sq_sqlite3.h"
  3. SQ_OPT_STRING_STRLEN();
  4. const SQChar *SQLite3_TAG = "SQLite3";
  5. static const SQChar *SQLite3_Stmt_TAG = "SQLite3Stmt";
  6. static const SQChar sqlite3_NULL_Name[] = _SC("sqlite3_NULL");
  7. static const SQChar nullName[] = _SC("Null");
  8. //return error if parameter exists but is not a blob
  9. //return 1 if the parameter exists and is a blob
  10. //return 0 if the parameter doesn't exists and creates a new blob
  11. static SQRESULT getOptionalBlob(HSQUIRRELVM v, SQInteger idx, SQBlob **blob)
  12. {
  13. if(sq_gettop(v) >= idx) {
  14. if(SQ_FAILED(sq_getinstanceup(v,3,(SQUserPointer*)blob,(SQUserPointer)SQBlob::SQBlob_TAG)))
  15. return sq_throwerror(v,_SC("expect a blob as second parameter"));
  16. if(!(*blob) || !(*blob)->IsValid())
  17. return sq_throwerror(v,_SC("the blob is invalid"));
  18. } else {
  19. *blob = new SQBlob(0, 8192);
  20. return 0;
  21. }
  22. return 1;
  23. }
  24. static SQRESULT sqlite3_NULL_tostring(HSQUIRRELVM v)
  25. {
  26. sq_pushstring(v, "", 0);
  27. return 1;
  28. }
  29. SQRegFunction sqlite3_NULL_methods[]=
  30. {
  31. {_SC("_tostring"),sqlite3_NULL_tostring,1, _SC("u")},
  32. {0,0}
  33. };
  34. static SQRESULT get_sqlite3_instance(HSQUIRRELVM v, SQInteger idx, sq_sqlite3_sdb **sdb)
  35. {
  36. SQRESULT _rc_;
  37. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)sdb,(void*)SQLite3_TAG)) < 0) return _rc_;
  38. if(!*sdb) return sq_throwerror(v, _SC("database is closed"));
  39. return _rc_;
  40. }
  41. static SQRESULT get_sqlite3_stmt_instance(HSQUIRRELVM v, SQInteger idx, sqlite3_stmt **stmt)
  42. {
  43. SQRESULT _rc_;
  44. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)stmt,(void*)SQLite3_Stmt_TAG)) < 0) return _rc_;
  45. if(!*stmt) return sq_throwerror(v, _SC("statement is closed"));
  46. return _rc_;
  47. }
  48. //#define push_sqlite3_null(v) sq_getonregistrytable(v, sqlite3_NULL_Name, sizeof(sqlite3_NULL_Name)-1);
  49. //#define push_sqlite3_null(v) sq_pushobject(v, sqlite3_NULL);
  50. //#define push_sqlite3_null(v) sq_getbyname(v, 1, nullName, sizeof(nullName)-1)
  51. //#define push_sqlite3_null(v) sq_pushfromregistrytable(v, nullName, sizeof(nullName)-1)
  52. //#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);}
  53. //the line bellow is the fastest one, any other attempt till now was slower
  54. #define push_sqlite3_null(v) {sq_pushstring(v, nullName, sizeof(nullName)-1);sq_get(v, 1);}
  55. #define GET_sqlite3_INSTANCE_AT(idx) \
  56. sq_sqlite3_sdb *sdb=NULL; \
  57. if((_rc_ = get_sqlite3_instance(v,idx,&sdb)) < 0) return _rc_;\
  58. sqlite3 *self = sdb->db;
  59. #define GET_sqlite3_INSTANCE() GET_sqlite3_INSTANCE_AT(1)
  60. //#define GET_sqlite3_INSTANCE() SQ_GET_INSTANCE(v, 1, sqlite3, SQLite3_TAG)
  61. #define GET_sqlite3_stmt_INSTANCE_AT(idx, var_name) \
  62. sqlite3_stmt *var_name=NULL; \
  63. if((_rc_ = get_sqlite3_stmt_instance(v,idx,&var_name)) < 0) return _rc_;
  64. #define GET_sqlite3_stmt_INSTANCE() GET_sqlite3_stmt_INSTANCE_AT(1, self)
  65. enum e_type_result {tr_first_row_first_col, tr_first_row, tr_all_rows, tr_ddml};
  66. #define AS_STRING_ALWAYS 0x01
  67. #define NULL_AS_EMPTY_STR 0x02
  68. #define WITH_COL_NAMES 0x04
  69. #define WITH_BLOB_AS_NULL 0x08
  70. #define WITH_SQ_NULL 0x100
  71. static void sqlite3_stmt_push_string(HSQUIRRELVM v, sqlite3_stmt *stmt, int col, int flags)
  72. {
  73. const char *value = (const char*) sqlite3_column_text(stmt, col);
  74. if(value) sq_pushstring(v, value, sqlite3_column_bytes(stmt, col));
  75. else
  76. {
  77. if(flags & AS_STRING_ALWAYS) sq_pushliteral(v, "");
  78. else push_sqlite3_null(v);
  79. }
  80. }
  81. static SQRESULT sqlite3_stmt_push_value(HSQUIRRELVM v, sqlite3_stmt *stmt, int col, int flags)
  82. {
  83. const char *value;
  84. int col_type = 0;
  85. if(flags & WITH_BLOB_AS_NULL)
  86. {
  87. col_type = sqlite3_column_type(stmt, col);
  88. if(col_type == SQLITE_BLOB)
  89. {
  90. if(flags & AS_STRING_ALWAYS) sq_pushliteral(v, "");
  91. else push_sqlite3_null(v);
  92. return 1;
  93. }
  94. }
  95. if(flags & AS_STRING_ALWAYS)
  96. {
  97. sqlite3_stmt_push_string(v, stmt, col, flags);
  98. }
  99. else
  100. {
  101. if(!col_type) col_type = sqlite3_column_type(stmt, col);
  102. switch (col_type)
  103. {
  104. case SQLITE_INTEGER:
  105. {
  106. sqlite_int64 i64 = sqlite3_column_int64(stmt, col);
  107. SQInteger n = (SQInteger)i64;
  108. if (n == i64)
  109. sq_pushinteger(v, n);
  110. else
  111. sq_pushstring(v, (const char*) sqlite3_column_text(stmt, col), sqlite3_column_bytes(stmt, col));
  112. }
  113. break;
  114. case SQLITE_FLOAT:
  115. sq_pushfloat(v, sqlite3_column_double(stmt, col));
  116. break;
  117. case SQLITE_TEXT:
  118. sqlite3_stmt_push_string(v, stmt, col, flags);
  119. break;
  120. case SQLITE_BLOB:
  121. value = (const char*) sqlite3_column_blob(stmt, col);
  122. if(value) sq_pushstring(v, value, sqlite3_column_bytes(stmt, col));
  123. else push_sqlite3_null(v);
  124. break;
  125. case SQLITE_NULL:
  126. if(flags & WITH_SQ_NULL) sq_pushnull(v);
  127. else push_sqlite3_null(v);
  128. break;
  129. default:
  130. if(flags & WITH_SQ_NULL) sq_pushnull(v);
  131. else push_sqlite3_null(v);
  132. break;
  133. }
  134. }
  135. return 1;
  136. }
  137. static void sqlite3_stmt_row_asArray(HSQUIRRELVM v, sqlite3_stmt *stmt, int flags)
  138. {
  139. int col_count = sqlite3_column_count(stmt);
  140. sq_newarray(v, col_count);
  141. for(int i=0; i<col_count; ++i)
  142. {
  143. sqlite3_stmt_push_value(v, stmt, i, flags);
  144. sq_arrayset(v, -2, i);
  145. }
  146. }
  147. static void sqlite3_stmt_row_asTable(HSQUIRRELVM v, sqlite3_stmt *stmt, int flags)
  148. {
  149. int col_count = sqlite3_column_count(stmt);
  150. sq_newtable(v);
  151. for(int i=0; i<col_count; ++i)
  152. {
  153. sq_pushstring(v, sqlite3_column_name(stmt, i), -1);
  154. sqlite3_stmt_push_value(v, stmt, i, flags);
  155. sq_rawset(v, -3);
  156. }
  157. }
  158. static void sqlite3_stmt_asArrayOfArrays(HSQUIRRELVM v, sqlite3_stmt *stmt, int flags)
  159. {
  160. sq_newarray(v, 0);
  161. if(flags & WITH_COL_NAMES)
  162. {
  163. int col_count = sqlite3_column_count(stmt);
  164. sq_newarray(v, col_count);
  165. for(int i=0; i<col_count; ++i)
  166. {
  167. sq_pushstring(v, sqlite3_column_name(stmt, i), -1);
  168. sq_arrayset(v, -2, i);
  169. }
  170. sq_arrayappend(v, -2);
  171. }
  172. while(sqlite3_step(stmt) == SQLITE_ROW)
  173. {
  174. sqlite3_stmt_row_asArray(v, stmt, flags);
  175. sq_arrayappend(v, -2);
  176. }
  177. }
  178. static void sqlite3_stmt_asArrayOfTables(HSQUIRRELVM v, sqlite3_stmt *stmt, int flags)
  179. {
  180. sq_newarray(v, 0);
  181. while(sqlite3_step(stmt) == SQLITE_ROW)
  182. {
  183. sqlite3_stmt_row_asTable(v, stmt, flags);
  184. sq_arrayappend(v, -2);
  185. }
  186. }
  187. static SQRESULT sqlite3_stmt_bind_value(HSQUIRRELVM v, sqlite3_stmt *stmt, int npar, int argn)
  188. {
  189. int _rc_ = 0;
  190. SQObjectType ptype = sq_gettype(v, argn);
  191. switch(ptype)
  192. {
  193. case OT_BOOL:
  194. SQ_GET_BOOL(v, argn, param_bool);
  195. _rc_ = sqlite3_bind_int(stmt, npar, param_bool == SQTrue ? 1 : 0);
  196. break;
  197. case OT_INTEGER:
  198. SQ_GET_INTEGER(v, argn, param_integer);
  199. _rc_ = sqlite3_bind_int64(stmt, npar, param_integer);
  200. break;
  201. case OT_FLOAT:
  202. SQ_GET_FLOAT(v, argn, param_float);
  203. _rc_ = sqlite3_bind_double(stmt, npar, param_float);
  204. break;
  205. case OT_STRING:
  206. {
  207. SQ_GET_STRING(v, argn, param_string);
  208. _rc_ = sqlite3_bind_text(stmt, npar, param_string, param_string_size, SQLITE_TRANSIENT);
  209. }
  210. break;
  211. case OT_USERDATA:
  212. {
  213. SQUserPointer ttag = 0;
  214. if((sq_gettypetag(v, argn, &ttag) != SQ_OK) || (ttag != sqlite3_NULL_Name))
  215. return sq_throwerror(v, "Invalid bind parameter %d", npar);
  216. }
  217. //fallthrough
  218. case OT_NULL:
  219. _rc_ = sqlite3_bind_null(stmt, npar);
  220. break;
  221. default:
  222. return sq_throwerror(v, "Invalid bind parameter %d", npar);
  223. }
  224. if(_rc_ != SQLITE_OK)
  225. {
  226. sqlite3 *db = sqlite3_db_handle(stmt);
  227. return sq_throwerror(v, sqlite3_errmsg(db));
  228. }
  229. return SQ_OK;
  230. }
  231. static SQRESULT sq_sqlite3_stmt_prepare_aux(HSQUIRRELVM v, sqlite3 *db, sqlite3_stmt **stmt, int params_start)
  232. {
  233. SQ_FUNC_VARS(v);
  234. SQ_GET_STRING(v, params_start, szSQL);
  235. if(sqlite3_prepare_v2(db, szSQL, szSQL_size, stmt, 0) != SQLITE_OK)
  236. {
  237. return sq_throwerror(v, sqlite3_errmsg(db));
  238. }
  239. if(_top_ > params_start)
  240. {
  241. int nparams = sqlite3_bind_parameter_count(*stmt);
  242. int nparams_given = _top_-params_start;
  243. if(nparams != nparams_given)
  244. {
  245. return sq_throwerror(v, "expect %d parameters but only %d given", nparams, nparams_given);
  246. }
  247. for(int i=1; i <= nparams; ++i)
  248. {
  249. int argn = i+params_start;
  250. _rc_ = sqlite3_stmt_bind_value(v, *stmt, i, argn);
  251. if(_rc_ < 0) return _rc_;
  252. }
  253. }
  254. return SQ_OK;
  255. }
  256. static SQRESULT sq_sqlite3_stmt_releasehook(SQUserPointer p, SQInteger size, void */*ep*/)
  257. {
  258. sqlite3_stmt *stmt = ((sqlite3_stmt *)p);
  259. if(stmt) sqlite3_finalize(stmt);
  260. return 0;
  261. }
  262. static SQRESULT sq_sqlite3_stmt_constructor(HSQUIRRELVM v)
  263. {
  264. SQ_FUNC_VARS(v);
  265. _rc_ = SQ_ERROR;
  266. GET_sqlite3_INSTANCE_AT(2);
  267. sqlite3_stmt *stmt = 0;
  268. if(_top_ > 2)
  269. {
  270. _rc_ = sq_sqlite3_stmt_prepare_aux(v, self, &stmt, 3);
  271. }
  272. else
  273. {
  274. if(sqlite3_prepare_v2(self, "select 'statement not prepared';", -1, &stmt, 0) != SQLITE_OK)
  275. {
  276. _rc_ = sq_throwerror(v, sqlite3_errmsg(self));
  277. }
  278. else _rc_ = SQ_OK;
  279. }
  280. sq_setinstanceup(v, 1, stmt); //replace self for this instance with this new sqlite3_stmt
  281. sq_setreleasehook(v,1, sq_sqlite3_stmt_releasehook);
  282. return _rc_;
  283. }
  284. static SQRESULT sq_sqlite3_stmt_get_db(HSQUIRRELVM v)
  285. {
  286. SQ_FUNC_VARS_NO_TOP(v);
  287. GET_sqlite3_stmt_INSTANCE();
  288. sqlite3 *db = sqlite3_db_handle(self);
  289. sq_pushuserpointer(v, db);
  290. if(sq_getonregistrytable(v) == SQ_OK)
  291. {
  292. sq_getweakrefval(v, -1);
  293. }
  294. else return sq_throwerror(v, _SC("could not retrive database, maybe it was already closed"));
  295. return 1;
  296. }
  297. static SQRESULT sq_sqlite3_stmt_stmt_ptr(HSQUIRRELVM v)
  298. {
  299. SQ_FUNC_VARS_NO_TOP(v);
  300. GET_sqlite3_stmt_INSTANCE();
  301. sq_pushuserpointer(v, self);
  302. return 1;
  303. }
  304. static SQRESULT sq_sqlite3_stmt_finalize(HSQUIRRELVM v)
  305. {
  306. SQ_FUNC_VARS_NO_TOP(v);
  307. GET_sqlite3_stmt_INSTANCE();
  308. sq_setinstanceup(v, 1, 0); //next calls will fail with "statement is closed"
  309. //printf("sq_sqlite3_stmt_finalize: %p : %s\n", self, sqlite3_sql(self));
  310. sq_pushinteger(v, sqlite3_finalize(self));
  311. return 1;
  312. }
  313. static SQRESULT sq_sqlite3_stmt_prepare(HSQUIRRELVM v)
  314. {
  315. SQ_FUNC_VARS_NO_TOP(v);
  316. GET_sqlite3_stmt_INSTANCE();
  317. sqlite3 *db = sqlite3_db_handle(self);
  318. sqlite3_stmt *stmt = 0;
  319. _rc_ = sq_sqlite3_stmt_prepare_aux(v, db, &stmt, 2);
  320. if(stmt)
  321. {
  322. sqlite3_finalize(self); //finalize the previous sqlite3_stmt
  323. sq_setinstanceup(v, 1, stmt); //replace self for this instance with this new sqlite3_stmt
  324. }
  325. return _rc_;
  326. }
  327. static SQRESULT sq_sqlite3_stmt_get_sql(HSQUIRRELVM v)
  328. {
  329. SQ_FUNC_VARS_NO_TOP(v);
  330. GET_sqlite3_stmt_INSTANCE();
  331. sq_pushstring(v, sqlite3_sql(self), -1);
  332. return 1;
  333. }
  334. static SQRESULT sq_sqlite3_stmt_bind(HSQUIRRELVM v)
  335. {
  336. SQ_FUNC_VARS_NO_TOP(v);
  337. GET_sqlite3_stmt_INSTANCE();
  338. SQ_GET_INTEGER(v, 2, npar);
  339. return sqlite3_stmt_bind_value(v, self, npar, 3);
  340. }
  341. /**
  342. The number/order of fields should be the same to use this function
  343. it'll save temporary data convertion.
  344. */
  345. static SQRESULT sq_sqlite3_stmt_bind_stmt(HSQUIRRELVM v)
  346. {
  347. SQ_FUNC_VARS_NO_TOP(v);
  348. GET_sqlite3_stmt_INSTANCE();
  349. GET_sqlite3_stmt_INSTANCE_AT(2, stmt_src);
  350. int bind_count = sqlite3_bind_parameter_count(self);
  351. for(int i=0; i < bind_count; ++i)
  352. {
  353. sqlite3_bind_value(self, i+1, sqlite3_column_value(stmt_src, i));
  354. }
  355. return 0;
  356. }
  357. static SQRESULT sq_sqlite3_stmt_bind_empty_null(HSQUIRRELVM v)
  358. {
  359. SQ_FUNC_VARS_NO_TOP(v);
  360. GET_sqlite3_stmt_INSTANCE();
  361. SQ_GET_INTEGER(v, 2, npar);
  362. if(sq_gettype(v, 3) == OT_STRING && sq_getsize(v, 3) == 0)
  363. {
  364. sq_pushnull(v);
  365. sq_replace(v, 3);
  366. }
  367. return sqlite3_stmt_bind_value(v, self, npar, 3);
  368. }
  369. static SQRESULT sq_sqlite3_stmt_bind_blob(HSQUIRRELVM v)
  370. {
  371. SQ_FUNC_VARS_NO_TOP(v);
  372. GET_sqlite3_stmt_INSTANCE();
  373. SQ_GET_INTEGER(v, 2, npar);
  374. SQ_GET_STRING(v, 3, blob);
  375. if(sqlite3_bind_blob(self, npar, blob, blob_size, SQLITE_TRANSIENT) != SQLITE_OK)
  376. {
  377. sqlite3 *db = sqlite3_db_handle(self);
  378. return sq_throwerror(v, sqlite3_errmsg(db));
  379. }
  380. return SQ_OK;
  381. }
  382. static SQRESULT sq_sqlite3_stmt_bind_values(HSQUIRRELVM v)
  383. {
  384. SQ_FUNC_VARS(v);
  385. GET_sqlite3_stmt_INSTANCE();
  386. if (_top_ - 1 != sqlite3_bind_parameter_count(self))
  387. return sq_throwerror(v,
  388. _SC("incorrect number of parameters to bind (%d given, %d to bind)"),
  389. _top_ - 1,
  390. sqlite3_bind_parameter_count(self));
  391. for (int n = 2; n <= _top_; ++n)
  392. {
  393. if (sqlite3_stmt_bind_value(v, self, n-1, n) != SQ_OK) return SQ_ERROR;
  394. }
  395. return 0;
  396. }
  397. static SQRESULT sq_sqlite3_stmt_bind_names(HSQUIRRELVM v)
  398. {
  399. SQ_FUNC_VARS_NO_TOP(v);
  400. GET_sqlite3_stmt_INSTANCE();
  401. SQObjectType ptype = sq_gettype(v, 2);
  402. int count = sqlite3_bind_parameter_count(self);
  403. int result;
  404. for (int n = 1; n <= count; ++n)
  405. {
  406. const char *name = sqlite3_bind_parameter_name(self, n);
  407. if (name && (name[0] == ':' || name[0] == '$' || name[0] == '@'))
  408. {
  409. if(ptype == OT_TABLE)
  410. {
  411. sq_pushstring(v, ++name, -1);
  412. if(sq_get(v, 2) == SQ_OK)
  413. {
  414. result = sqlite3_stmt_bind_value(v, self, n, -1);
  415. sq_poptop(v);
  416. }
  417. else return sq_throwerror(v, _SC("bind parameter (%s) not found"), name);
  418. }
  419. else return sq_throwerror(v, _SC("table expected to bind named parameters"));
  420. }
  421. else
  422. {
  423. if(ptype == OT_ARRAY)
  424. {
  425. sq_pushinteger(v, n);
  426. if(sq_get(v, 2) == SQ_OK)
  427. {
  428. result = sqlite3_stmt_bind_value(v, self, n, -1);
  429. sq_poptop(v);
  430. }
  431. else return sq_throwerror(v, _SC("bind parameter (%d) not found"), n);
  432. }
  433. else return sq_throwerror(v, _SC("array expected to bind numbered parameters"));
  434. }
  435. if (result != SQ_OK) return result;
  436. }
  437. return 0;
  438. }
  439. static SQRESULT sq_sqlite3_stmt_bind_parameter_index(HSQUIRRELVM v)
  440. {
  441. SQ_FUNC_VARS_NO_TOP(v);
  442. GET_sqlite3_stmt_INSTANCE();
  443. SQ_GET_STRING(v, 2, spar);
  444. sq_pushinteger(v, sqlite3_bind_parameter_index(self, spar));
  445. return 1;
  446. }
  447. static SQRESULT sq_sqlite3_stmt_bind_parameter_count(HSQUIRRELVM v)
  448. {
  449. SQ_FUNC_VARS_NO_TOP(v);
  450. GET_sqlite3_stmt_INSTANCE();
  451. sq_pushinteger(v, sqlite3_bind_parameter_count(self));
  452. return 1;
  453. }
  454. static SQRESULT sq_sqlite3_stmt_reset(HSQUIRRELVM v)
  455. {
  456. SQ_FUNC_VARS_NO_TOP(v);
  457. GET_sqlite3_stmt_INSTANCE();
  458. sq_pushinteger(v, sqlite3_reset(self));
  459. return 1;
  460. }
  461. static SQRESULT sq_sqlite3_stmt_step(HSQUIRRELVM v)
  462. {
  463. SQ_FUNC_VARS_NO_TOP(v);
  464. GET_sqlite3_stmt_INSTANCE();
  465. sq_pushinteger(v, sqlite3_step(self));
  466. return 1;
  467. }
  468. static SQRESULT sq_sqlite3_stmt_next_row(HSQUIRRELVM v)
  469. {
  470. SQ_FUNC_VARS_NO_TOP(v);
  471. GET_sqlite3_stmt_INSTANCE();
  472. sq_pushbool(v, sqlite3_step(self) == SQLITE_ROW);
  473. return 1;
  474. }
  475. static SQRESULT sq_sqlite3_stmt_col_count(HSQUIRRELVM v)
  476. {
  477. SQ_FUNC_VARS_NO_TOP(v);
  478. GET_sqlite3_stmt_INSTANCE();
  479. sq_pushinteger(v, sqlite3_column_count(self));
  480. return 1;
  481. }
  482. static SQRESULT sq_sqlite3_stmt_col_name(HSQUIRRELVM v)
  483. {
  484. SQ_FUNC_VARS_NO_TOP(v);
  485. GET_sqlite3_stmt_INSTANCE();
  486. SQ_GET_INTEGER(v, 2, col);
  487. sq_pushstring(v, sqlite3_column_name(self, col), -1);
  488. return 1;
  489. }
  490. static SQRESULT sq_sqlite3_stmt_col_type(HSQUIRRELVM v)
  491. {
  492. SQ_FUNC_VARS_NO_TOP(v);
  493. GET_sqlite3_stmt_INSTANCE();
  494. SQ_GET_INTEGER(v, 2, col);
  495. sq_pushinteger(v, sqlite3_column_type(self, col));
  496. return 1;
  497. }
  498. static const char *get_column_type(sqlite3_stmt *stmt, int col)
  499. {
  500. const char *column_type;
  501. int icol_type = sqlite3_column_type(stmt, col);
  502. switch(icol_type)
  503. {
  504. case SQLITE_INTEGER: column_type = "INTEGER"; break;
  505. case SQLITE_FLOAT: column_type = "FLOAT"; break;
  506. case SQLITE_BLOB: column_type = "BLOB"; break;
  507. case SQLITE_TEXT: column_type = "TEXT"; break;
  508. case SQLITE_NULL: column_type = "NULL"; break;
  509. default:
  510. column_type = "unknown";
  511. }
  512. return column_type;
  513. }
  514. static const char *get_column_dcltype(sqlite3_stmt *stmt, int col)
  515. {
  516. const char *column_type = sqlite3_column_decltype(stmt, col);
  517. if(!column_type) column_type = get_column_type(stmt, col);
  518. return column_type;
  519. }
  520. static SQRESULT sq_sqlite3_stmt_col_declared_type(HSQUIRRELVM v)
  521. {
  522. SQ_FUNC_VARS_NO_TOP(v);
  523. GET_sqlite3_stmt_INSTANCE();
  524. SQ_GET_INTEGER(v, 2, col);
  525. sq_pushstring(v, get_column_dcltype(self, col), -1);
  526. return 1;
  527. }
  528. static SQRESULT sq_sqlite3_stmt_colsAsArray(HSQUIRRELVM v)
  529. {
  530. SQ_FUNC_VARS_NO_TOP(v);
  531. GET_sqlite3_stmt_INSTANCE();
  532. int col_count = sqlite3_column_count(self);
  533. sq_newarray(v, col_count);
  534. for(int i=0; i<col_count; ++i)
  535. {
  536. sq_pushstring(v, sqlite3_column_name(self, i), -1);
  537. sq_arrayset(v, -2, i);
  538. }
  539. return 1;
  540. }
  541. static SQRESULT sq_sqlite3_stmt_colsTypeAsArray(HSQUIRRELVM v)
  542. {
  543. SQ_FUNC_VARS_NO_TOP(v);
  544. GET_sqlite3_stmt_INSTANCE();
  545. int col_count = sqlite3_column_count(self);
  546. sq_newarray(v, col_count);
  547. for(int i=0; i<col_count; ++i)
  548. {
  549. sq_pushstring(v, get_column_type(self, i), -1);
  550. sq_arrayset(v, -2, i);
  551. }
  552. return 1;
  553. }
  554. static SQRESULT sq_sqlite3_stmt_colsDeclTypeAsArray(HSQUIRRELVM v)
  555. {
  556. SQ_FUNC_VARS_NO_TOP(v);
  557. GET_sqlite3_stmt_INSTANCE();
  558. int col_count = sqlite3_column_count(self);
  559. sq_newarray(v, col_count);
  560. for(int i=0; i<col_count; ++i)
  561. {
  562. sq_pushstring(v, get_column_dcltype(self, i), -1);
  563. sq_arrayset(v, -2, i);
  564. }
  565. return 1;
  566. }
  567. static SQRESULT sq_sqlite3_stmt_colsNameAsArray(HSQUIRRELVM v)
  568. {
  569. SQ_FUNC_VARS_NO_TOP(v);
  570. GET_sqlite3_stmt_INSTANCE();
  571. int col_count = sqlite3_column_count(self);
  572. sq_newarray(v, col_count);
  573. for(int i=0; i<col_count; ++i)
  574. {
  575. sq_pushstring(v, sqlite3_column_name(self, i), -1);
  576. sq_arrayset(v, -2, i);
  577. }
  578. return 1;
  579. }
  580. static SQRESULT sq_sqlite3_stmt_colsAsTable(HSQUIRRELVM v)
  581. {
  582. SQ_FUNC_VARS_NO_TOP(v);
  583. GET_sqlite3_stmt_INSTANCE();
  584. int col_count = sqlite3_column_count(self);
  585. sq_newtable(v);
  586. for(int i=0; i<col_count; ++i)
  587. {
  588. sq_pushstring(v, sqlite3_column_name(self, i), -1);
  589. sq_pushinteger(v, i);
  590. sq_rawset(v, -3);
  591. }
  592. return 1;
  593. }
  594. static int get_col_index(HSQUIRRELVM v, sqlite3_stmt *self)
  595. {
  596. SQInteger _rc_, col;
  597. switch(sq_gettype(v, 2))
  598. {
  599. case OT_INTEGER:
  600. sq_getinteger(v, 2, &col);
  601. break;
  602. case OT_STRING:
  603. {
  604. SQ_GET_STRING(v, 2, col_name);
  605. if(col_name_size == 0) return sq_throwerror(v, "column name can not be empty");
  606. col = -1;
  607. for(int i=0, len=sqlite3_column_count(self); i<len; ++i)
  608. {
  609. if(strcasecmp(col_name, sqlite3_column_name(self, i)) == 0)
  610. {
  611. col = i;
  612. break;
  613. }
  614. }
  615. if(col == -1) return sq_throwerror(v, "column name not found \"%s\"", col_name);
  616. }
  617. break;
  618. default:
  619. return sq_throwerror(v, "wrong parameter expected integer|string");
  620. }
  621. return col;
  622. }
  623. static SQRESULT sq_sqlite3_stmt_col(HSQUIRRELVM v)
  624. {
  625. SQ_FUNC_VARS_NO_TOP(v);
  626. GET_sqlite3_stmt_INSTANCE();
  627. int col = get_col_index(v, self);
  628. if(col < 0) return col;
  629. sqlite3_stmt_push_value(v, self, col, 0);
  630. return 1;
  631. }
  632. static SQRESULT sq_sqlite3_stmt_asBool(HSQUIRRELVM v)
  633. {
  634. SQ_FUNC_VARS_NO_TOP(v);
  635. GET_sqlite3_stmt_INSTANCE();
  636. int col = get_col_index(v, self);
  637. if(col < 0) return col;
  638. const unsigned char *b = sqlite3_column_text(self, col);
  639. sq_pushbool(v, b && *b == '1');
  640. return 1;
  641. }
  642. static SQRESULT sq_sqlite3_stmt_asInteger(HSQUIRRELVM v)
  643. {
  644. SQ_FUNC_VARS_NO_TOP(v);
  645. GET_sqlite3_stmt_INSTANCE();
  646. int col = get_col_index(v, self);
  647. if(col < 0) return col;
  648. sq_pushinteger(v, sqlite3_column_int64(self, col));
  649. return 1;
  650. }
  651. static SQRESULT sq_sqlite3_stmt_asFloat(HSQUIRRELVM v)
  652. {
  653. SQ_FUNC_VARS_NO_TOP(v);
  654. GET_sqlite3_stmt_INSTANCE();
  655. int col = get_col_index(v, self);
  656. if(col < 0) return col;
  657. sq_pushfloat(v, sqlite3_column_double(self, col));
  658. return 1;
  659. }
  660. static SQRESULT sq_sqlite3_stmt_StringOrNull(HSQUIRRELVM v, int withNull)
  661. {
  662. SQ_FUNC_VARS_NO_TOP(v);
  663. GET_sqlite3_stmt_INSTANCE();
  664. int col = get_col_index(v, self);
  665. if(col < 0) return col;
  666. sqlite3_stmt_push_string(v, self, col, withNull ? 0 : NULL_AS_EMPTY_STR);
  667. return 1;
  668. }
  669. static SQRESULT sq_sqlite3_stmt_asString(HSQUIRRELVM v)
  670. {
  671. return sq_sqlite3_stmt_StringOrNull(v, 0);
  672. }
  673. static SQRESULT sq_sqlite3_stmt_asStringOrNull(HSQUIRRELVM v)
  674. {
  675. return sq_sqlite3_stmt_StringOrNull(v, 1);
  676. }
  677. static SQRESULT sq_sqlite3_stmt_asTable(HSQUIRRELVM v)
  678. {
  679. SQ_FUNC_VARS(v);
  680. GET_sqlite3_stmt_INSTANCE();
  681. SQ_OPT_INTEGER(v, 2, flags, 0);
  682. sqlite3_stmt_row_asTable(v, self, flags);
  683. return 1;
  684. }
  685. static SQRESULT sq_sqlite3_stmt_asArray(HSQUIRRELVM v)
  686. {
  687. SQ_FUNC_VARS(v);
  688. GET_sqlite3_stmt_INSTANCE();
  689. SQ_OPT_INTEGER(v, 2, flags, 0);
  690. sqlite3_stmt_row_asArray(v, self, flags);
  691. return 1;
  692. }
  693. static SQRESULT sq_sqlite3_stmt_asArrayOfArrays(HSQUIRRELVM v)
  694. {
  695. SQ_FUNC_VARS(v);
  696. GET_sqlite3_stmt_INSTANCE();
  697. SQ_OPT_INTEGER(v, 2, flags, 0);
  698. sqlite3_stmt_asArrayOfArrays(v, self, flags);
  699. sqlite3_reset(self);
  700. return 1;
  701. }
  702. static SQRESULT sq_sqlite3_stmt_asArrayOfTables(HSQUIRRELVM v)
  703. {
  704. SQ_FUNC_VARS(v);
  705. GET_sqlite3_stmt_INSTANCE();
  706. SQ_OPT_INTEGER(v, 2, flags, 0);
  707. sqlite3_stmt_asArrayOfTables(v, self, flags);
  708. sqlite3_reset(self);
  709. return 1;
  710. }
  711. /*utility functions*/
  712. static void append_escaping_json(SQBlob &json, const char *sz)
  713. {
  714. if(sz) //escape string
  715. {
  716. int last_idx, idx;
  717. idx = last_idx = 0;
  718. for(; sz[idx]; ++idx)
  719. {
  720. char c = sz[idx];
  721. switch(c)
  722. {
  723. case '"':
  724. case '\n':
  725. case '\r':
  726. case '\t':
  727. case '\b':
  728. case '\f':
  729. case '\\':
  730. {
  731. json.Write(sz+last_idx, idx-last_idx);
  732. last_idx = idx;
  733. if(c == '\n')
  734. {
  735. json.Write("\\n", 2);
  736. ++last_idx;
  737. }
  738. else if(c == '\t')
  739. {
  740. json.Write("\\t", 2);
  741. ++last_idx;
  742. }
  743. else if(c == '\b')
  744. {
  745. json.Write("\\b", 2);
  746. ++last_idx;
  747. }
  748. else if(c == '\f')
  749. {
  750. json.Write("\\f", 2);
  751. ++last_idx;
  752. }
  753. else if(c == '\r')
  754. {
  755. json.Write("\\r", 2);
  756. ++last_idx;
  757. }
  758. else
  759. {
  760. json.Write("\\", 1);
  761. }
  762. }
  763. }
  764. }
  765. //last part
  766. json.Write(sz+last_idx, idx-last_idx);
  767. }
  768. }
  769. static SQRESULT sq_sqlite3_stmt_asJsonArray(HSQUIRRELVM v)
  770. {
  771. SQ_FUNC_VARS(v);
  772. GET_sqlite3_stmt_INSTANCE();
  773. SQ_OPT_BOOL(v, 2, withMetadata, 0);
  774. int col_count = sqlite3_column_count(self);
  775. int i, value_type;
  776. const char* value;
  777. SQBlob *json = NULL;
  778. int has_blob = getOptionalBlob(v, 3, &json);
  779. if(has_blob == SQ_ERROR) return SQ_ERROR;
  780. if(withMetadata)
  781. {
  782. json->WriteZstr("{\n\"columns\":[\n");
  783. for(i=0; i < col_count; ++i)
  784. {
  785. json->WriteZstr((i == 0 ? "\"" : ",\""));
  786. const char *column_name = sqlite3_column_name(self, i);
  787. json->WriteZstr(column_name ? column_name : "unknown");
  788. json->WriteZstr("\"");
  789. }
  790. json->WriteZstr("\n],\n\"column_types\":[\n");
  791. for(i=0; i < col_count; ++i)
  792. {
  793. json->WriteZstr((i == 0 ? "\"" : ",\""));
  794. json->WriteZstr(get_column_dcltype(self, i));
  795. json->WriteZstr("\"");
  796. }
  797. json->WriteZstr("\n],\n\"rows\":[\n");
  798. }
  799. else
  800. {
  801. json->WriteZstr("[\n");
  802. }
  803. int row_count = 0;
  804. while(sqlite3_step(self) == SQLITE_ROW)
  805. {
  806. json->WriteZstr((row_count++ == 0 ? "[" : ",["));
  807. for(i=0; i < col_count; ++i)
  808. {
  809. json->WriteZstr((i == 0 ? "\"" : ",\""));
  810. //if get the type after sqlite3_column_text it always returns SQLITE_TEXT
  811. value_type = sqlite3_column_type(self, i);
  812. value = (const char*)sqlite3_column_text(self, i);
  813. if(value)
  814. {
  815. switch(value_type)
  816. {
  817. case SQLITE_BLOB:
  818. break; //no blobs in json
  819. case SQLITE_TEXT:
  820. {
  821. append_escaping_json(*json, value);
  822. }
  823. break;
  824. default:
  825. json->WriteZstr(value);
  826. }
  827. }
  828. json->WriteZstr("\"");
  829. }
  830. json->WriteZstr("]\n");
  831. }
  832. json->WriteZstr("]");
  833. if(withMetadata) json->WriteZstr("\n}");
  834. if(has_blob) return 0;
  835. sq_pushstring(v, (const SQChar*)json->GetBuf(), json->Len());
  836. delete json;
  837. return 1;
  838. }
  839. static SQRESULT sq_sqlite3_stmt_asJsonObject(HSQUIRRELVM v)
  840. {
  841. SQ_FUNC_VARS_NO_TOP(v);
  842. GET_sqlite3_stmt_INSTANCE();
  843. int col_count = sqlite3_column_count(self);
  844. int i, value_type, record_count = 0;
  845. const char* value;
  846. SQBlob *json = NULL;
  847. int has_blob = getOptionalBlob(v, 2, &json);
  848. if(has_blob == SQ_ERROR) return SQ_ERROR;
  849. json->WriteZstr("{");
  850. if(sqlite3_step(self) == SQLITE_ROW)
  851. {
  852. ++record_count;
  853. for(i=0; i < col_count; ++i)
  854. {
  855. json->WriteZstr((i == 0 ? "\"" : "\",\""));
  856. append_escaping_json(*json, (const char*)sqlite3_column_name(self, i));
  857. json->WriteZstr("\":\"");
  858. //if get the type after sqlite3_column_text it always returns SQLITE_TEXT
  859. value_type = sqlite3_column_type(self, i);
  860. value = (const char*)sqlite3_column_text(self, i);
  861. if(value)
  862. {
  863. switch(value_type)
  864. {
  865. case SQLITE_BLOB:
  866. break; //no blobs in json
  867. case SQLITE_TEXT:
  868. {
  869. append_escaping_json(*json, value);
  870. }
  871. break;
  872. default:
  873. json->WriteZstr(value);
  874. }
  875. }
  876. }
  877. json->WriteZstr("\"");
  878. }
  879. json->WriteZstr("}");
  880. if(has_blob) return 0;
  881. if(record_count) sq_pushstring(v, (const SQChar*)json->GetBuf(), json->Len());
  882. else sq_pushnull(v);
  883. delete json;
  884. return 1;
  885. }
  886. #define UNSIGNED(n) n##U
  887. #define IBYTE1 UNSIGNED(255)
  888. #define IBYTE2 UNSIGNED(255*255)
  889. #define IBYTE3 UNSIGNED(255*255*255)
  890. #define IBYTE4 UNSIGNED(255*255*255*255)
  891. #define SIZE1BYTE 250
  892. #define SIZE2BYTE 251
  893. #define SIZE3BYTE 252
  894. #define SIZE4BYTE 253
  895. #define SLEMARK 254
  896. #define SLEEND 255
  897. static SQRESULT sle2array(HSQUIRRELVM v, const unsigned char *p, size_t sle_size, const unsigned char **next)
  898. {
  899. size_t size, data_len = 0;
  900. if(sle_size == 0)
  901. {
  902. *next = 0;
  903. return 0;
  904. }
  905. while(*p != SLEEND) //data finished now follow digest
  906. {
  907. size = *p++;
  908. if(size >= SIZE1BYTE)
  909. {
  910. if(size == SIZE1BYTE)
  911. {
  912. //data bigger than 250 and less 500 next byte has more info
  913. size += *p++;
  914. }
  915. else if(size == SIZE2BYTE)
  916. {
  917. //data bigger than 250 next two bytes has more info
  918. size = (*p++ * IBYTE1);
  919. size += *p++;
  920. }
  921. else if(size == SIZE3BYTE)
  922. {
  923. //data bigger than 250 next three bytes has more info
  924. size = (*p++ * IBYTE2);
  925. size += (*p++ * IBYTE1);
  926. size += *p++;
  927. }
  928. else if(size == SIZE4BYTE)
  929. {
  930. //data bigger than 250 next four bytes has more info
  931. size = (*p++ * IBYTE3);
  932. size += (*p++ * IBYTE2);
  933. size += (*p++ * IBYTE1);
  934. size += *p++;
  935. }
  936. else if(size == SLEMARK)
  937. {
  938. //reserved can be used for multi part data, metadata, digest, ...
  939. break;
  940. }
  941. }
  942. sq_pushstring(v, (const char*)p, size);
  943. sq_arrayappend(v, -2);
  944. p += size;
  945. data_len += size;
  946. if(data_len > sle_size)
  947. {
  948. //something went wrong here
  949. return sq_throwerror(v, "We got more data than expected !");
  950. }
  951. }
  952. *next = ++p;
  953. return 0;
  954. }
  955. static SQRESULT sle2vec(HSQUIRRELVM v, const unsigned char *sle,
  956. size_t sle_size,
  957. const unsigned char **next)
  958. {
  959. int rc;
  960. sq_newarray(v, 0);
  961. if(sle[0] != '[')
  962. {
  963. return sq_throwerror(v, "Invalid encoded vector !");
  964. }
  965. if(sle[1] == ']')
  966. {
  967. *next = sle+2; //empty array
  968. return 0;
  969. }
  970. if((rc = sle2array(v, sle+1, sle_size-1, next)) < 0) return rc;
  971. if(!*next || *next[0] != ']')
  972. {
  973. return sq_throwerror(v, "Invalid end of vector !");
  974. }
  975. (*next)++;
  976. return 0;
  977. }
  978. static SQRESULT sq_sle2vecOfvec(HSQUIRRELVM v)
  979. {
  980. SQ_FUNC_VARS(v);
  981. SQBlob *blob = NULL;
  982. const SQChar *sle, *sle_start;
  983. SQInteger sle_size;
  984. SQObjectType ptype = sq_gettype(v, 2);
  985. switch(ptype)
  986. {
  987. case OT_STRING:
  988. sq_getstr_and_size(v, 2, &sle, &sle_size);
  989. break;
  990. case OT_INSTANCE:
  991. if(SQ_FAILED(sq_getinstanceup(v,2,(SQUserPointer*)&blob,(SQUserPointer)SQBlob::SQBlob_TAG)))
  992. return sq_throwerror(v,_SC("invalid type tag"));
  993. if(!blob || !blob->IsValid())
  994. return sq_throwerror(v,_SC("the blob is invalid"));
  995. sle = (const SQChar*)blob->GetBuf();
  996. sle_size = blob->Len();
  997. break;
  998. default:
  999. return sq_throwerror(v,_SC("invalid parameter 1 of type %s"), sq_gettypename(v, 2));
  1000. }
  1001. if(sq_gettype(v, 3) != OT_ARRAY)
  1002. return sq_throwerror(v,_SC("invalid parameter 2 of type %s"), sq_gettypename(v, 3));
  1003. SQ_OPT_INTEGER(v, 4, start, 0);
  1004. if(start < 0) return sq_throwerror(v, _SC("invalid start position (%d)"), start);
  1005. sq_push(v, 3);
  1006. int rc, data_count = 1;
  1007. const unsigned char *next;
  1008. sle_start = sle + start;
  1009. sle_size -= start;
  1010. if(sle_start[0] != '[')
  1011. {
  1012. return sq_throwerror(v, "Invalid encoded vector !");
  1013. }
  1014. if(sle_start[1] == ']')
  1015. {
  1016. //sq_pushinteger(v, start+2);
  1017. return 1; //empty array
  1018. }
  1019. int saved_top = sq_gettop(v);
  1020. if((rc=sle2vec(v, (const unsigned char *)sle_start+1, sle_size-1, &next)) < 0) return rc;
  1021. while(next && *next == '[')
  1022. {
  1023. sq_arrayappend(v, -2);
  1024. data_count++;
  1025. if((rc=sle2vec(v, next, sle_size - (next - (const unsigned char *)sle_start), &next)) < 0) return rc;
  1026. }
  1027. if(sq_gettop(v) != saved_top)
  1028. {
  1029. //last added table left
  1030. sq_arrayappend(v, -2);
  1031. data_count++;
  1032. }
  1033. if(!next || *next != ']')
  1034. {
  1035. return sq_throwerror(v, "Invalid end of vector !");
  1036. }
  1037. sq_pushinteger(v, ((const SQChar*)(++next)) - sle);
  1038. return 1;
  1039. }
  1040. typedef unsigned char slebuf_t[8];
  1041. static int sleSize2buf(slebuf_t sle, size_t size)
  1042. {
  1043. size_t next_size, tmp_size;
  1044. if(size < SIZE1BYTE)
  1045. {
  1046. sle[0] = size;
  1047. return 1;
  1048. }
  1049. else if(size < (SIZE1BYTE*2))
  1050. {
  1051. sle[0] = SIZE1BYTE;
  1052. next_size = size - 250;
  1053. sle[1] = next_size;
  1054. return 2;
  1055. }
  1056. else if(size < IBYTE2)
  1057. {
  1058. sle[0] = SIZE2BYTE;
  1059. next_size = size/IBYTE1;
  1060. sle[1] = next_size;
  1061. next_size = size%IBYTE1;
  1062. sle[2] = next_size;
  1063. return 3;
  1064. }
  1065. else if(size < IBYTE3)
  1066. {
  1067. sle[0] = SIZE3BYTE;
  1068. next_size = size/IBYTE2;
  1069. sle[1] = next_size;
  1070. tmp_size = size%IBYTE2;
  1071. next_size = tmp_size/IBYTE1;
  1072. sle[2] = next_size;
  1073. next_size = tmp_size%IBYTE1;
  1074. sle[3] = next_size;
  1075. return 4;
  1076. }
  1077. else if(size < IBYTE4)
  1078. {
  1079. sle[0] = SIZE4BYTE;
  1080. next_size = size/IBYTE3;
  1081. sle[1] = next_size;
  1082. tmp_size = size%IBYTE3;
  1083. next_size = tmp_size/IBYTE2;
  1084. sle[2] = next_size;
  1085. tmp_size = tmp_size%IBYTE2;
  1086. next_size = tmp_size/IBYTE1;
  1087. sle[3] = next_size;
  1088. next_size = tmp_size%IBYTE1;
  1089. sle[5] = next_size;
  1090. return 5;
  1091. }
  1092. return 0;
  1093. }
  1094. static SQRESULT sq_get_sle_size(HSQUIRRELVM v)
  1095. {
  1096. SQ_FUNC_VARS_NO_TOP(v);
  1097. slebuf_t slebuf;
  1098. SQ_GET_INTEGER(v, 2, size);
  1099. int slesize = sleSize2buf(slebuf, size);
  1100. if(slesize)
  1101. {
  1102. sq_pushstring(v, (const char *)slebuf, slesize);
  1103. return 1;
  1104. }
  1105. return 0;
  1106. }
  1107. static int add2sle(SQBlob &sle, const char *str, size_t size)
  1108. {
  1109. slebuf_t slebuf;
  1110. int slesize = sleSize2buf(slebuf, size);
  1111. if(slesize) sle.Write((const char *)slebuf, slesize);
  1112. if(size) sle.Write(str, size);
  1113. return size;
  1114. }
  1115. static SQRESULT sq_add2sle(HSQUIRRELVM v)
  1116. {
  1117. SQ_FUNC_VARS(v);
  1118. SQ_GET_INSTANCE_VAR(v, 2, SQBlob, blob, SQBlob::SQBlob_TAG);
  1119. SQ_GET_STRING(v, 3, str);
  1120. SQ_OPT_INTEGER(v, 4, size, str_size);
  1121. if(size < 0) return sq_throwerror(v, _SC("invalid size value (%d)"), size);
  1122. sq_pushinteger(v, add2sle(*blob, str, size));
  1123. return 1;
  1124. }
  1125. static SQRESULT sq_sqlite3_stmt_asSleArray(HSQUIRRELVM v)
  1126. {
  1127. SQ_FUNC_VARS_NO_TOP(v);
  1128. GET_sqlite3_stmt_INSTANCE();
  1129. int col_count = sqlite3_column_count(self);
  1130. int i;
  1131. const char *value;
  1132. SQBlob *sle = NULL;
  1133. int has_blob = getOptionalBlob(v, 2, &sle);
  1134. if(has_blob == SQ_ERROR) return SQ_ERROR;
  1135. sle->WriteZstr("[[");
  1136. for(i=0; i < col_count; ++i)
  1137. {
  1138. value = sqlite3_column_name(self, i);
  1139. add2sle(*sle, value, strlen(value));
  1140. }
  1141. sle->WriteChar(SLEEND);
  1142. sle->WriteChar(']');
  1143. while(sqlite3_step(self) == SQLITE_ROW)
  1144. {
  1145. sle->WriteChar('[');
  1146. for(i=0; i < col_count; ++i)
  1147. {
  1148. switch(sqlite3_column_type(self, i))
  1149. {
  1150. case SQLITE_BLOB:
  1151. {
  1152. add2sle(*sle, (const char*)sqlite3_column_blob(self, i),
  1153. sqlite3_column_bytes(self, i));
  1154. }
  1155. break;
  1156. default:
  1157. add2sle(*sle, (const char*)sqlite3_column_text(self, i),
  1158. sqlite3_column_bytes(self, i));
  1159. }
  1160. }
  1161. sle->WriteChar(SLEEND);
  1162. sle->WriteChar(']');
  1163. }
  1164. sle->WriteChar(']');
  1165. if(has_blob) return 0;
  1166. sq_pushstring(v, (const SQChar*)sle->GetBuf(), sle->Len());
  1167. delete sle;
  1168. return 1;
  1169. }
  1170. static SQRESULT sq_sqlite3_stmt_all_blobs_size(HSQUIRRELVM v)
  1171. {
  1172. SQ_FUNC_VARS_NO_TOP(v);
  1173. GET_sqlite3_stmt_INSTANCE();
  1174. int total_size = 0, col_count = sqlite3_column_count(self);
  1175. for(int i=0; i < col_count; ++i)
  1176. {
  1177. if(sqlite3_column_type(self, i) == SQLITE_BLOB)
  1178. {
  1179. total_size += sqlite3_column_bytes(self, i);
  1180. }
  1181. }
  1182. sq_pushinteger(v, total_size);
  1183. return 1;
  1184. }
  1185. static SQRESULT sq_sqlite3_stmt_colsSizeAsArray(HSQUIRRELVM v)
  1186. {
  1187. SQ_FUNC_VARS_NO_TOP(v);
  1188. GET_sqlite3_stmt_INSTANCE();
  1189. int col_count = sqlite3_column_count(self);
  1190. sq_newarray(v, col_count);
  1191. for(int i=0; i<col_count; ++i)
  1192. {
  1193. sq_pushinteger(v, sqlite3_column_bytes(self, i));
  1194. sq_arrayset(v, -2, i);
  1195. }
  1196. return 1;
  1197. }
  1198. #define _DECL_FUNC(name,nparams,tycheck, isStatic) {_SC(#name), sq_sqlite3_stmt_##name,nparams,tycheck, isStatic}
  1199. static SQRegFunction sq_sqlite3_stmt_methods[] =
  1200. {
  1201. _DECL_FUNC(constructor, -2, _SC("xxs s|n|b|o"), SQFalse),
  1202. _DECL_FUNC(stmt_ptr, 1, _SC("x"), SQFalse),
  1203. _DECL_FUNC(get_db, 1, _SC("x"), SQFalse),
  1204. _DECL_FUNC(finalize, 1, _SC("x"), SQFalse),
  1205. _DECL_FUNC(prepare, -2, _SC("xs s|n|b|o|u"), SQFalse),
  1206. _DECL_FUNC(get_sql, 1, _SC("x"), SQFalse),
  1207. _DECL_FUNC(bind, 3, _SC("xi s|n|b|o|u"), SQFalse),
  1208. _DECL_FUNC(bind_stmt, 2, _SC("xx"), SQFalse),
  1209. _DECL_FUNC(bind_empty_null, 3, _SC("xi s|n|b|o|u"), SQFalse),
  1210. _DECL_FUNC(bind_blob, 3, _SC("xis|u"), SQFalse),
  1211. _DECL_FUNC(bind_values, -2, _SC("x s|n|b|o|u"), SQFalse),
  1212. _DECL_FUNC(bind_names, 2, _SC("x t|a"), SQFalse),
  1213. _DECL_FUNC(bind_parameter_index, 2, _SC("xs"), SQFalse),
  1214. _DECL_FUNC(bind_parameter_count, 1, _SC("x"), SQFalse),
  1215. _DECL_FUNC(step, 1, _SC("x"), SQFalse),
  1216. _DECL_FUNC(reset, 1, _SC("x"), SQFalse),
  1217. _DECL_FUNC(next_row, 1, _SC("x"), SQFalse),
  1218. _DECL_FUNC(colsAsArray, 1, _SC("x"), SQFalse),
  1219. _DECL_FUNC(colsAsTable, 1, _SC("x"), SQFalse),
  1220. _DECL_FUNC(colsTypeAsArray, 1, _SC("x"), SQFalse),
  1221. _DECL_FUNC(colsDeclTypeAsArray, 1, _SC("x"), SQFalse),
  1222. _DECL_FUNC(col_count, 1, _SC("x"), SQFalse),
  1223. _DECL_FUNC(col_name, 2, _SC("xi"), SQFalse),
  1224. _DECL_FUNC(colsNameAsArray, 1, _SC("x"), SQFalse),
  1225. _DECL_FUNC(col_type, 2, _SC("xi"), SQFalse),
  1226. _DECL_FUNC(col_declared_type, 2, _SC("xi"), SQFalse),
  1227. _DECL_FUNC(asArray, -1, _SC("xi"), SQFalse),
  1228. _DECL_FUNC(asTable, -1, _SC("xi"), SQFalse),
  1229. _DECL_FUNC(asArrayOfArrays, -1, _SC("xi"), SQFalse),
  1230. _DECL_FUNC(asArrayOfTables, -1, _SC("xi"), SQFalse),
  1231. _DECL_FUNC(asSleArray, -1, _SC("xx"), SQFalse),
  1232. //todo manage/skip blobs for json
  1233. _DECL_FUNC(asJsonArray, -1, _SC("xbx"), SQFalse),
  1234. _DECL_FUNC(asJsonObject, -1, _SC("xx"), SQFalse),
  1235. _DECL_FUNC(col, 2, _SC("x i|s"), SQFalse),
  1236. _DECL_FUNC(asString, 2, _SC("x i|s"), SQFalse),
  1237. _DECL_FUNC(asStringOrNull, 2, _SC("x i|s"), SQFalse),
  1238. _DECL_FUNC(asInteger, 2, _SC("x i|s"), SQFalse),
  1239. _DECL_FUNC(asBool, 2, _SC("x i|s"), SQFalse),
  1240. _DECL_FUNC(asFloat, 2, _SC("x i|s"), SQFalse),
  1241. _DECL_FUNC(all_blobs_size, 1, _SC("x"), SQFalse),
  1242. _DECL_FUNC(colsSizeAsArray, 1, _SC("x"), SQFalse),
  1243. {0,0}
  1244. };
  1245. #undef _DECL_FUNC
  1246. static SQRESULT sqlite3_exec_fmt(HSQUIRRELVM v, e_type_result type_result, int null_as_empty_str=1)
  1247. {
  1248. SQ_FUNC_VARS_NO_TOP(v);
  1249. GET_sqlite3_INSTANCE();
  1250. //SQ_GET_STRING(v, 2, szSQL);
  1251. sqlite3_stmt *stmt = 0;
  1252. _rc_ = sq_sqlite3_stmt_prepare_aux(v, self, &stmt, 2);
  1253. if(_rc_ < 0)
  1254. {
  1255. if(stmt) sqlite3_finalize(stmt);
  1256. return _rc_;
  1257. }
  1258. int rc = sqlite3_step(stmt);
  1259. if(type_result == tr_ddml)
  1260. {
  1261. if(rc != SQLITE_DONE)
  1262. {
  1263. sqlite3_finalize(stmt);
  1264. return sq_throwerror(v, "SQL is not a DDML %d %s", rc, sqlite3_errmsg(self));
  1265. }
  1266. sq_pushinteger(v, sqlite3_changes(sqlite3_db_handle(stmt)));
  1267. }
  1268. else
  1269. {
  1270. if(rc != SQLITE_ROW)
  1271. {
  1272. sqlite3_finalize(stmt);
  1273. return sq_throwerror(v, "%d %s", rc, sqlite3_errmsg(self));
  1274. }
  1275. switch(type_result)
  1276. {
  1277. case tr_first_row_first_col:
  1278. sqlite3_stmt_push_value(v, stmt, 0, 0);
  1279. break;
  1280. case tr_first_row:
  1281. sqlite3_stmt_row_asArray(v, stmt, NULL_AS_EMPTY_STR|AS_STRING_ALWAYS);
  1282. break;
  1283. case tr_all_rows:
  1284. sqlite3_stmt_asArrayOfArrays(v, stmt, NULL_AS_EMPTY_STR|AS_STRING_ALWAYS);
  1285. break;
  1286. default:
  1287. sqlite3_finalize(stmt);
  1288. return sq_throwerror(v, "Unknown requested return type %d", type_result);
  1289. }
  1290. }
  1291. sqlite3_finalize(stmt);
  1292. return 1;
  1293. }
  1294. static SQRESULT sq_sqlite3_close_release(HSQUIRRELVM v, sq_sqlite3_sdb *sdb)
  1295. {
  1296. SQRESULT rc = SQ_ERROR;
  1297. //printf("sq_sqlite3_close_release:%p\n", sdb);
  1298. if(sdb && sdb->db)
  1299. {
  1300. sqlite3 *db = sdb->db;
  1301. sdb->db = NULL; //prevent double entry/free, see sq_pushregistrytable(sdb->v) bellow
  1302. /*
  1303. sqlite3_stmt* statement = NULL;
  1304. int count = 0;
  1305. while ((statement = sqlite3_next_stmt(db, NULL)))
  1306. {
  1307. printf("sq_sqlite3_close_release:stmt: %p : %s\n", statement, sqlite3_sql(statement));
  1308. sqlite3_finalize(statement);
  1309. statement = c;
  1310. count++;
  1311. }
  1312. if (count) return sq_throwerror(v, _SC("closing database with %d statements not closed."), count);
  1313. */
  1314. int close_rc = sqlite3_close_v2(db) == SQLITE_OK;
  1315. if(!v || (v && close_rc))
  1316. {
  1317. rc = SQ_OK;
  1318. //remove waekref from registrytable
  1319. sq_delete_on_registry_table(sdb->v, db);
  1320. if(sdb->func)
  1321. {
  1322. sq_sqlite3_sdb_func *func, *func_next;
  1323. func = sdb->func;
  1324. while (func)
  1325. {
  1326. func_next = func->next;
  1327. sq_release(sdb->v, &func->fn_step);
  1328. sq_release(sdb->v, &func->fn_finalize);
  1329. sq_release(sdb->v, &func->udata);
  1330. sq_free(func, sizeof(sq_sqlite3_sdb_func));
  1331. func = func_next;
  1332. }
  1333. sdb->func = NULL;
  1334. }
  1335. /* 'free' all references */
  1336. sq_release(sdb->v, &sdb->busy_cb);
  1337. sq_release(sdb->v, &sdb->busy_udata);
  1338. sq_release(sdb->v, &sdb->progress_cb);
  1339. sq_release(sdb->v, &sdb->progress_udata);
  1340. sq_release(sdb->v, &sdb->trace_cb);
  1341. sq_release(sdb->v, &sdb->trace_udata);
  1342. sq_release(sdb->v, &sdb->update_hook_cb);
  1343. sq_release(sdb->v, &sdb->update_hook_udata);
  1344. sq_release(sdb->v, &sdb->authorizer_hook_cb);
  1345. sq_release(sdb->v, &sdb->authorizer_hook_udata);
  1346. sq_release(sdb->v, &sdb->null_value);
  1347. sq_free(sdb, sizeof(sq_sqlite3_sdb));
  1348. }
  1349. else
  1350. {
  1351. if(v) return sq_throwerror(v, sqlite3_errmsg(db));
  1352. }
  1353. }
  1354. return rc;
  1355. }
  1356. static SQRESULT sq_sqlite3_releasehook(SQUserPointer p, SQInteger size, void */*ep*/)
  1357. {
  1358. sq_sqlite3_sdb *sdb = ((sq_sqlite3_sdb *)p);
  1359. sq_sqlite3_close_release(0, sdb);
  1360. return 0;
  1361. }
  1362. static SQRESULT sq_sqlite3_constructor(HSQUIRRELVM v)
  1363. {
  1364. SQ_FUNC_VARS(v);
  1365. SQ_GET_STRING(v, 2, dbname);
  1366. SQ_OPT_INTEGER(v, 3, flags, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_SUBLATIN_NA_LIKE);
  1367. sqlite3 *db;
  1368. int rc = sqlite3_open_v2(dbname, &db, flags, 0);
  1369. if(rc != SQLITE_OK) return sq_throwerror(v, "Failed to open database ! %d", rc);
  1370. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb *)sq_malloc(sizeof(sq_sqlite3_sdb));
  1371. memset(sdb, 0, sizeof(sq_sqlite3_sdb));
  1372. sdb->db = db;
  1373. sdb->v = v;
  1374. sq_resetobject(&sdb->busy_cb);
  1375. sq_resetobject(&sdb->busy_udata);
  1376. sq_resetobject(&sdb->progress_cb);
  1377. sq_resetobject(&sdb->progress_udata);
  1378. sq_resetobject(&sdb->trace_cb);
  1379. sq_resetobject(&sdb->trace_udata);
  1380. sq_resetobject(&sdb->update_hook_cb);
  1381. sq_resetobject(&sdb->update_hook_udata);
  1382. sq_resetobject(&sdb->authorizer_hook_cb);
  1383. sq_resetobject(&sdb->authorizer_hook_udata);
  1384. sq_resetobject(&sdb->null_value);
  1385. sq_setinstanceup(v, 1, sdb);
  1386. sq_setreleasehook(v,1, sq_sqlite3_releasehook);
  1387. //save a weakref to allow statement return it's db
  1388. sq_pushuserpointer(v, sdb->db);
  1389. sq_weakref(v, 1);
  1390. sq_setonregistrytable(v);
  1391. return 1;
  1392. }
  1393. static SQRESULT sq_sqlite3_close(HSQUIRRELVM v)
  1394. {
  1395. SQ_FUNC_VARS_NO_TOP(v);
  1396. GET_sqlite3_INSTANCE();
  1397. if(sq_sqlite3_close_release(v, sdb) != SQ_OK) return SQ_ERROR;
  1398. sq_setinstanceup(v, 1, 0); //next calls will fail with "database is closed"
  1399. return 0;
  1400. }
  1401. static SQRESULT sq_sqlite3_db_ptr(HSQUIRRELVM v)
  1402. {
  1403. SQ_FUNC_VARS_NO_TOP(v);
  1404. GET_sqlite3_INSTANCE();
  1405. sq_pushuserpointer(v, self);
  1406. return 1;
  1407. }
  1408. /* bool IsAutoCommitOn( ) */
  1409. static SQRESULT sq_sqlite3_IsAutoCommitOn(HSQUIRRELVM v)
  1410. {
  1411. SQ_FUNC_VARS_NO_TOP(v);
  1412. GET_sqlite3_INSTANCE();
  1413. sq_pushbool(v, sqlite3_get_autocommit(self) ? SQTrue : SQFalse);
  1414. return 1;
  1415. }
  1416. /* int changes( ) */
  1417. static SQRESULT sq_sqlite3_changes(HSQUIRRELVM v)
  1418. {
  1419. SQ_FUNC_VARS_NO_TOP(v);
  1420. GET_sqlite3_INSTANCE();
  1421. sq_pushinteger(v, sqlite3_changes(self));
  1422. return 1;
  1423. }
  1424. static SQRESULT sq_sqlite3_exec(HSQUIRRELVM v)
  1425. {
  1426. return sqlite3_exec_fmt(v, tr_all_rows);
  1427. }
  1428. /* int exec_dml( const char * szSQL ) */
  1429. static SQRESULT sq_sqlite3_exec_dml(HSQUIRRELVM v)
  1430. {
  1431. SQ_FUNC_VARS_NO_TOP(v);
  1432. GET_sqlite3_INSTANCE();
  1433. SQ_GET_STRING(v, 2, szSQL);
  1434. char *errmsg;
  1435. if(sqlite3_exec(self, szSQL, NULL, NULL, &errmsg) != SQLITE_OK){
  1436. sq_throwerror(v, errmsg);
  1437. sqlite3_free(errmsg);
  1438. return SQ_ERROR;
  1439. }
  1440. sq_pushinteger(v, sqlite3_changes(self));
  1441. return 1;
  1442. }
  1443. /* bool exec_get_one( std::string & result , const char * szSQL ) */
  1444. static SQRESULT sq_sqlite3_exec_get_one(HSQUIRRELVM v)
  1445. {
  1446. return sqlite3_exec_fmt(v, tr_first_row_first_col);
  1447. }
  1448. static SQRESULT sq_sqlite3_exec_get_first_row(HSQUIRRELVM v)
  1449. {
  1450. return sqlite3_exec_fmt(v, tr_first_row);
  1451. }
  1452. /* const char * get_db_name( ) */
  1453. static SQRESULT sq_sqlite3_get_db_name(HSQUIRRELVM v)
  1454. {
  1455. SQ_FUNC_VARS_NO_TOP(v);
  1456. GET_sqlite3_INSTANCE();
  1457. sq_pushstring(v, sqlite3_db_filename(self, "main"), -1);
  1458. return 1;
  1459. }
  1460. /* sqlite3_int64 last_row_id( ) */
  1461. static SQRESULT sq_sqlite3_last_row_id(HSQUIRRELVM v)
  1462. {
  1463. SQ_FUNC_VARS_NO_TOP(v);
  1464. GET_sqlite3_INSTANCE();
  1465. sq_pushinteger(v, sqlite3_last_insert_rowid(self));
  1466. return 1;
  1467. }
  1468. /* stmt * prepare( const char * sql ) */
  1469. static SQRESULT sq_sqlite3_prepare(HSQUIRRELVM v)
  1470. {
  1471. SQ_FUNC_VARS_NO_TOP(v);
  1472. //GET_sqlite3_INSTANCE();
  1473. SQ_GET_STRING(v, 2, sql);
  1474. sq_pushstring(v, SQLite3_Stmt_TAG, -1);
  1475. if(sq_getonroottable(v) == SQ_ERROR) return SQ_ERROR;
  1476. sq_pushroottable(v);
  1477. sq_push(v, 1);
  1478. sq_push(v, 2);
  1479. if(sq_call(v, 3, SQTrue, SQFalse) != SQ_OK) return SQ_ERROR;
  1480. return 1;
  1481. }
  1482. /* void set_busy_timeout( int nMillisecs ) */
  1483. static SQRESULT sq_sqlite3_set_busy_timeout(HSQUIRRELVM v)
  1484. {
  1485. SQ_FUNC_VARS_NO_TOP(v);
  1486. //GET_sqlite3_INSTANCE();
  1487. SQ_GET_INTEGER(v, 2, nMillisecs);
  1488. //self->set_busy_timeout(nMillisecs);
  1489. return 0;
  1490. }
  1491. /* int total_changes( ) */
  1492. static SQRESULT sq_sqlite3_total_changes(HSQUIRRELVM v)
  1493. {
  1494. SQ_FUNC_VARS_NO_TOP(v);
  1495. GET_sqlite3_INSTANCE();
  1496. sq_pushinteger(v, sqlite3_total_changes(self));
  1497. return 1;
  1498. }
  1499. static SQRESULT sq_sqlite3_enable_shared_cache(HSQUIRRELVM v)
  1500. {
  1501. SQ_FUNC_VARS_NO_TOP(v);
  1502. SQ_GET_BOOL(v, 2, bhow);
  1503. sq_pushbool(v, sqlite3_enable_shared_cache(bhow) == SQLITE_OK);
  1504. return 1;
  1505. }
  1506. static SQRESULT sq_sqlite3_sleep(HSQUIRRELVM v)
  1507. {
  1508. SQ_FUNC_VARS_NO_TOP(v);
  1509. SQ_GET_INTEGER(v, 2, n);
  1510. sqlite3_sleep(n);
  1511. return 0;
  1512. }
  1513. static SQRESULT sq_sqlite3_version(HSQUIRRELVM v)
  1514. {
  1515. sq_pushstring(v, sqlite3_libversion(), -1);
  1516. return 1;
  1517. }
  1518. static SQRESULT sq_sqlite3_threadsafe(HSQUIRRELVM v)
  1519. {
  1520. sq_pushinteger(v, sqlite3_threadsafe());
  1521. return 1;
  1522. }
  1523. static SQRESULT sq_sqlite3_config_single_thread(HSQUIRRELVM v)
  1524. {
  1525. sq_pushinteger(v, sqlite3_config(SQLITE_CONFIG_SINGLETHREAD));
  1526. return 1;
  1527. }
  1528. static SQRESULT sq_sqlite3_config_multi_thread(HSQUIRRELVM v)
  1529. {
  1530. sq_pushinteger(v, sqlite3_config(SQLITE_CONFIG_MULTITHREAD));
  1531. return 1;
  1532. }
  1533. static SQRESULT sq_sqlite3_config_serialized(HSQUIRRELVM v)
  1534. {
  1535. sq_pushinteger(v, sqlite3_config(SQLITE_CONFIG_SERIALIZED));
  1536. return 1;
  1537. }
  1538. static SQRESULT sq_sqlite3_errcode(HSQUIRRELVM v)
  1539. {
  1540. SQ_FUNC_VARS_NO_TOP(v);
  1541. GET_sqlite3_INSTANCE();
  1542. sq_pushinteger(v, sqlite3_errcode(self));
  1543. return 1;
  1544. }
  1545. static SQRESULT sq_sqlite3_errmsg(HSQUIRRELVM v)
  1546. {
  1547. SQ_FUNC_VARS_NO_TOP(v);
  1548. GET_sqlite3_INSTANCE();
  1549. sq_pushstring(v, sqlite3_errmsg(self), -1);
  1550. return 1;
  1551. }
  1552. #ifndef WIN32
  1553. static SQRESULT sq_sqlite3_temp_directory(HSQUIRRELVM v)
  1554. {
  1555. SQ_FUNC_VARS(v);
  1556. const char *oldtemp = sqlite3_temp_directory;
  1557. if (_top_ > 1)
  1558. {
  1559. SQ_GET_STRING(v, 2, temp);
  1560. if (sqlite3_temp_directory)
  1561. {
  1562. sqlite3_free((char*)sqlite3_temp_directory);
  1563. }
  1564. if (temp && temp_size)
  1565. {
  1566. sqlite3_temp_directory = sqlite3_mprintf("%s", temp);
  1567. }
  1568. else
  1569. {
  1570. sqlite3_temp_directory = NULL;
  1571. }
  1572. }
  1573. sq_pushstring(v, oldtemp, -1);
  1574. return 1;
  1575. }
  1576. #endif
  1577. #ifdef SQLITE_HAS_CODEC
  1578. //#include "codecext.h"
  1579. /*
  1580. ** Params: db, sql
  1581. ** returns: code, compiled length or error message
  1582. */
  1583. static SQRESULT sq_sqlite3_key(HSQUIRRELVM v)
  1584. {
  1585. SQ_FUNC_VARS_NO_TOP(v);
  1586. GET_sqlite3_INSTANCE();
  1587. SQ_GET_STRING(v, 2, key);
  1588. sq_pushinteger(v, sqlite3_key_v2(self, 0, key, key_size));
  1589. return 1;
  1590. }
  1591. /*
  1592. ** Params: db, sql
  1593. ** returns: code, compiled length or error message
  1594. */
  1595. static SQRESULT sq_sqlite3_rekey(HSQUIRRELVM v)
  1596. {
  1597. SQ_FUNC_VARS_NO_TOP(v);
  1598. GET_sqlite3_INSTANCE();
  1599. SQ_GET_STRING(v, 2, key);
  1600. sq_pushinteger(v, sqlite3_rekey_v2(self, 0, key, key_size));
  1601. return 1;
  1602. }
  1603. #endif
  1604. #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK
  1605. /*
  1606. ** progress handler:
  1607. ** Params: database, number of opcodes, callback function, userdata
  1608. **
  1609. ** callback function:
  1610. ** Params: userdata
  1611. ** returns: 0 to return immediatly and return SQLITE_ABORT, non-zero to continue
  1612. */
  1613. static int db_progress_callback(void *udata)
  1614. {
  1615. SQInteger result = 1; /* abort by default */
  1616. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)udata;
  1617. HSQUIRRELVM v = sdb->v;
  1618. int top = sq_gettop(v);
  1619. sq_pushobject(v, sdb->progress_cb);
  1620. sq_pushroottable(v); //this
  1621. sq_pushobject(v, sdb->progress_udata);
  1622. /* call squilu function */
  1623. if (sq_call(v, 2, SQTrue, SQFalse) == SQ_OK)
  1624. sq_getinteger(v, -1, &result);
  1625. sq_settop(v, top);
  1626. return result;
  1627. }
  1628. static SQRESULT sq_sqlite3_progress_handler(HSQUIRRELVM v)
  1629. {
  1630. SQ_FUNC_VARS(v);
  1631. GET_sqlite3_INSTANCE();
  1632. /* clear progress handler */
  1633. sqlite3_progress_handler(self, 0, NULL, NULL);
  1634. sq_release(v, &sdb->progress_cb);
  1635. sq_release(v, &sdb->progress_udata);
  1636. sq_resetobject(&sdb->progress_cb);
  1637. sq_resetobject(&sdb->progress_udata);
  1638. SQObjectType otype = sq_gettype(v, 2);
  1639. if (_top_ > 1 && (otype != OT_NULL))
  1640. {
  1641. SQ_GET_INTEGER(v, 2, nop);
  1642. if(sq_gettype(v, 3) != OT_CLOSURE)
  1643. return sq_throwerror(v, _SC("invalid second parameter expected closure"));
  1644. sq_getstackobj(v, 3, &sdb->progress_cb);
  1645. sq_addref(v, &sdb->progress_cb);
  1646. if(_top_ > 3)
  1647. {
  1648. sq_getstackobj(v, 4, &sdb->progress_udata);
  1649. sq_addref(v, &sdb->progress_udata);
  1650. }
  1651. /* set progress callback */
  1652. sqlite3_progress_handler(self, nop, db_progress_callback, sdb);
  1653. }
  1654. return 0;
  1655. }
  1656. #else /* #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK */
  1657. static SQRESULT sq_sqlite3_progress_handler(HSQUIRRELVM v)
  1658. {
  1659. return sq_throwerror(v, _SC("progress callback support disabled at compile time"));
  1660. return 0;
  1661. }
  1662. #endif /* #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK */
  1663. /*
  1664. ** trace callback:
  1665. ** Params: database, callback function, userdata
  1666. **
  1667. ** callback function:
  1668. ** Params: userdata, sql
  1669. */
  1670. static void db_trace_callback(void *udata, const char *sql)
  1671. {
  1672. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)udata;
  1673. HSQUIRRELVM v = sdb->v;
  1674. int top = sq_gettop(v);
  1675. /* setup squirrel callback call */
  1676. sq_pushobject(v, sdb->trace_cb);
  1677. sq_pushroottable(v);
  1678. sq_pushobject(v, sdb->trace_udata);
  1679. sq_pushstring(v, sql, -1); /* traced sql statement */
  1680. /* call squirrel function */
  1681. sq_call(v, 3, SQFalse, SQFalse);
  1682. /* ignore any error generated by this function */
  1683. sq_settop(v, top);
  1684. }
  1685. static SQRESULT sq_sqlite3_trace(HSQUIRRELVM v)
  1686. {
  1687. SQ_FUNC_VARS(v);
  1688. GET_sqlite3_INSTANCE();
  1689. /* clear trace handler */
  1690. sqlite3_trace(self, NULL, NULL);
  1691. sq_release(v, &sdb->trace_cb);
  1692. sq_release(v, &sdb->trace_udata);
  1693. sq_resetobject(&sdb->trace_cb);
  1694. sq_resetobject(&sdb->trace_udata);
  1695. SQObjectType otype = sq_gettype(v, 2);
  1696. if (_top_ > 1 && (otype != OT_NULL))
  1697. {
  1698. if(sq_gettype(v, 2) != OT_CLOSURE)
  1699. return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
  1700. sq_getstackobj(v, 2, &sdb->trace_cb);
  1701. sq_addref(v, &sdb->trace_cb);
  1702. if(_top_ > 2)
  1703. {
  1704. sq_getstackobj(v, 3, &sdb->trace_udata);
  1705. sq_addref(v, &sdb->trace_udata);
  1706. }
  1707. /* set trace callback */
  1708. if(_top_ > 3)
  1709. {
  1710. SQBool b;
  1711. sq_getbool(v, 4, &b);
  1712. sqlite3_trace_v2(self, db_trace_callback, sdb, b == SQTrue);
  1713. }
  1714. else sqlite3_trace(self, db_trace_callback, sdb);
  1715. }
  1716. return 0;
  1717. }
  1718. /*
  1719. ** update hook callback:
  1720. ** Params: database, callback function, userdata
  1721. **
  1722. ** callback function:
  1723. ** Params: userdata, sql
  1724. */
  1725. static void db_update_hook_callback(void *udata, int update_type,
  1726. char const *db_name, char const *table_name, sqlite3_int64 rowid)
  1727. {
  1728. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)udata;
  1729. HSQUIRRELVM v = sdb->v;
  1730. int top = sq_gettop(v);
  1731. /* setup squirrel callback call */
  1732. sq_pushobject(v, sdb->update_hook_cb);
  1733. sq_pushroottable(v);
  1734. sq_pushobject(v, sdb->update_hook_udata);
  1735. sq_pushinteger(v, update_type);
  1736. sq_pushstring(v, db_name, -1);
  1737. sq_pushstring(v, table_name, -1);
  1738. sq_pushinteger(v, rowid);
  1739. /* call squirrel function */
  1740. sq_call(v, 6, SQFalse, SQFalse);
  1741. /* ignore any error generated by this function */
  1742. sq_settop(v, top);
  1743. }
  1744. static SQRESULT sq_sqlite3_update_hook(HSQUIRRELVM v)
  1745. {
  1746. SQ_FUNC_VARS(v);
  1747. GET_sqlite3_INSTANCE();
  1748. /* clear update handler */
  1749. sqlite3_update_hook(self, NULL, NULL);
  1750. sq_release(v, &sdb->update_hook_cb);
  1751. sq_release(v, &sdb->update_hook_udata);
  1752. sq_resetobject(&sdb->update_hook_cb);
  1753. sq_resetobject(&sdb->update_hook_udata);
  1754. SQObjectType otype = sq_gettype(v, 2);
  1755. if (_top_ > 1 && (otype != OT_NULL))
  1756. {
  1757. if(sq_gettype(v, 2) != OT_CLOSURE)
  1758. return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
  1759. sq_getstackobj(v, 2, &sdb->update_hook_cb);
  1760. sq_addref(v, &sdb->update_hook_cb);
  1761. if(_top_ > 2)
  1762. {
  1763. sq_getstackobj(v, 3, &sdb->update_hook_udata);
  1764. sq_addref(v, &sdb->update_hook_udata);
  1765. }
  1766. /* set update_hook callback */
  1767. sqlite3_update_hook(self, db_update_hook_callback, sdb);
  1768. }
  1769. return 0;
  1770. }
  1771. /*
  1772. ** authorizer hook callback:
  1773. ** Params: database, callback function, userdata
  1774. **
  1775. ** callback function:
  1776. ** Params: userdata, sql
  1777. */
  1778. #if 0
  1779. /******************************************* 3rd ************ 4th ***********/
  1780. #define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */
  1781. #define SQLITE_CREATE_TABLE 2 /* Table Name NULL */
  1782. #define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */
  1783. #define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */
  1784. #define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
  1785. #define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */
  1786. #define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */
  1787. #define SQLITE_CREATE_VIEW 8 /* View Name NULL */
  1788. #define SQLITE_DELETE 9 /* Table Name NULL */
  1789. #define SQLITE_DROP_INDEX 10 /* Index Name Table Name */
  1790. #define SQLITE_DROP_TABLE 11 /* Table Name NULL */
  1791. #define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */
  1792. #define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */
  1793. #define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */
  1794. #define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */
  1795. #define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */
  1796. #define SQLITE_DROP_VIEW 17 /* View Name NULL */
  1797. #define SQLITE_INSERT 18 /* Table Name NULL */
  1798. #define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */
  1799. #define SQLITE_READ 20 /* Table Name Column Name */
  1800. #define SQLITE_SELECT 21 /* NULL NULL */
  1801. #define SQLITE_TRANSACTION 22 /* Operation NULL */
  1802. #define SQLITE_UPDATE 23 /* Table Name Column Name */
  1803. #define SQLITE_ATTACH 24 /* Filename NULL */
  1804. #define SQLITE_DETACH 25 /* Database Name NULL */
  1805. #define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
  1806. #define SQLITE_REINDEX 27 /* Index Name NULL */
  1807. #define SQLITE_ANALYZE 28 /* Table Name NULL */
  1808. #define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
  1809. #define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
  1810. #define SQLITE_FUNCTION 31 /* NULL Function Name */
  1811. #define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */
  1812. #define SQLITE_COPY 0 /* No longer used */
  1813. #define SQLITE_RECURSIVE 33 /* NULL NULL */
  1814. #endif
  1815. static int db_authorizer_hook_callback(void *udata, int action_code,
  1816. char const *param3, char const *param4,
  1817. char const *param5, char const *param6)
  1818. {
  1819. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)udata;
  1820. HSQUIRRELVM v = sdb->v;
  1821. int top = sq_gettop(v);
  1822. SQInteger result = SQLITE_OK;
  1823. /* setup squirrel callback call */
  1824. sq_pushobject(v, sdb->authorizer_hook_cb);
  1825. sq_pushroottable(v);
  1826. sq_pushobject(v, sdb->authorizer_hook_udata);
  1827. sq_pushinteger(v, action_code);
  1828. sq_pushstring(v, param3, -1);
  1829. sq_pushstring(v, param4, -1);
  1830. sq_pushstring(v, param5, -1);
  1831. sq_pushstring(v, param6, -1);
  1832. /* call squilu function */
  1833. /* ignore any error generated by this function */
  1834. if (sq_call(v, 7, SQTrue, SQFalse) == SQ_OK)
  1835. sq_getinteger(v, -1, &result);
  1836. sq_settop(v, top);
  1837. return result;
  1838. }
  1839. static SQRESULT sq_sqlite3_set_authorizer(HSQUIRRELVM v)
  1840. {
  1841. SQ_FUNC_VARS(v);
  1842. GET_sqlite3_INSTANCE();
  1843. /* clear trace handler */
  1844. sqlite3_set_authorizer(self, NULL, NULL);
  1845. sq_release(v, &sdb->authorizer_hook_cb);
  1846. sq_release(v, &sdb->authorizer_hook_udata);
  1847. sq_resetobject(&sdb->authorizer_hook_cb);
  1848. sq_resetobject(&sdb->authorizer_hook_udata);
  1849. SQObjectType otype = sq_gettype(v, 2);
  1850. if (_top_ > 1 && (otype != OT_NULL))
  1851. {
  1852. if(sq_gettype(v, 2) != OT_CLOSURE)
  1853. return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
  1854. sq_getstackobj(v, 2, &sdb->authorizer_hook_cb);
  1855. sq_addref(v, &sdb->authorizer_hook_cb);
  1856. if(_top_ > 2)
  1857. {
  1858. sq_getstackobj(v, 3, &sdb->authorizer_hook_udata);
  1859. sq_addref(v, &sdb->authorizer_hook_udata);
  1860. }
  1861. /* set authorizer_hook callback */
  1862. sqlite3_set_authorizer(self, db_authorizer_hook_callback, sdb);
  1863. }
  1864. return 0;
  1865. }
  1866. /*
  1867. ** busy handler:
  1868. ** Params: database, callback function, userdata
  1869. **
  1870. ** callback function:
  1871. ** Params: userdata, number of tries
  1872. ** returns: 0 to return immediatly and return SQLITE_BUSY, non-zero to try again
  1873. */
  1874. static int db_busy_callback(void *udata, int tries)
  1875. {
  1876. SQBool retry = SQFalse; /* abort by default */
  1877. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)udata;
  1878. HSQUIRRELVM v = sdb->v;
  1879. int top = sq_gettop(v);
  1880. sq_pushobject(v, sdb->busy_cb);
  1881. sq_pushroottable(v);
  1882. sq_pushobject(v, sdb->busy_udata);
  1883. sq_pushinteger(v, tries);
  1884. /* call squilu function */
  1885. if (sq_call(v, 3, SQTrue, SQFalse) == SQ_OK)
  1886. sq_getbool(v, -1, &retry);
  1887. sq_settop(v, top);
  1888. return retry == SQTrue;
  1889. }
  1890. static SQRESULT sq_sqlite3_busy_handler(HSQUIRRELVM v)
  1891. {
  1892. SQ_FUNC_VARS(v);
  1893. GET_sqlite3_INSTANCE();
  1894. /* clear busy handler */
  1895. sqlite3_busy_handler(self, NULL, NULL);
  1896. sq_release(v, &sdb->busy_cb);
  1897. sq_release(v, &sdb->busy_udata);
  1898. sq_resetobject(&sdb->busy_cb);
  1899. sq_resetobject(&sdb->busy_udata);
  1900. SQObjectType otype = sq_gettype(v, 2);
  1901. if (_top_ > 1 && (otype != OT_NULL))
  1902. {
  1903. if(sq_gettype(v, 2) != OT_CLOSURE)
  1904. return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
  1905. sq_getstackobj(v, 2, &sdb->busy_cb);
  1906. sq_addref(v, &sdb->busy_cb);
  1907. if(_top_ > 2)
  1908. {
  1909. sq_getstackobj(v, 3, &sdb->busy_udata);
  1910. sq_addref(v, &sdb->busy_udata);
  1911. }
  1912. /* set busy callback */
  1913. sqlite3_busy_handler(self, db_busy_callback, sdb);
  1914. }
  1915. return 0;
  1916. }
  1917. static SQRESULT sq_sqlite3_busy_timeout(HSQUIRRELVM v)
  1918. {
  1919. SQ_FUNC_VARS_NO_TOP(v);
  1920. GET_sqlite3_INSTANCE();
  1921. SQ_GET_INTEGER(v, 2, timeout);
  1922. sqlite3_busy_timeout(self, timeout);
  1923. /* if there was a timeout callback registered, it is now
  1924. ** invalid/useless. free any references we may have */
  1925. sq_release(v, &sdb->busy_cb);
  1926. sq_resetobject(&sdb->busy_cb);
  1927. sq_release(v, &sdb->busy_udata);
  1928. sq_resetobject(&sdb->busy_udata);
  1929. return 0;
  1930. }
  1931. static SQRESULT sq_sqlite3_interrupt(HSQUIRRELVM v)
  1932. {
  1933. SQ_FUNC_VARS_NO_TOP(v);
  1934. GET_sqlite3_INSTANCE();
  1935. sqlite3_interrupt(self);
  1936. return 0;
  1937. }
  1938. /*
  1939. ** =======================================================
  1940. ** User Defined Functions - Context Methods
  1941. ** =======================================================
  1942. */
  1943. typedef struct
  1944. {
  1945. sqlite3_context *ctx;
  1946. HSQOBJECT udata;
  1947. } sq_sqlite3_context_st;
  1948. static const SQChar sq_sqlite3_context_TAG[] = _SC(":sqlite3:ctx");
  1949. static SQRESULT sq_sqlite3_context_releasehook(SQUserPointer p, SQInteger size, void */*ep*/)
  1950. {
  1951. sq_sqlite3_context_st *ctx = ((sq_sqlite3_context_st *)p);
  1952. /* 'free' all references */
  1953. sq_free(ctx, sizeof(sq_sqlite3_context_st));
  1954. return 0;
  1955. }
  1956. static SQRESULT sq_sqlite3_context_constructor(HSQUIRRELVM v)
  1957. {
  1958. sq_sqlite3_context_st *ctx = (sq_sqlite3_context_st*)sq_malloc(sizeof(sq_sqlite3_context_st));
  1959. ctx->ctx = NULL;
  1960. sq_resetobject(&ctx->udata);
  1961. sq_setinstanceup(v, 1, ctx);
  1962. sq_setreleasehook(v,1, sq_sqlite3_context_releasehook);
  1963. return 1;
  1964. }
  1965. #define SQ_SQLITE3_GETCONTEXT(v, idx) \
  1966. sq_sqlite3_context_st *self;\
  1967. if((_rc_ = sq_getinstanceup(v, idx, (void**)&self, (void*)sq_sqlite3_context_TAG)) < 0) return _rc_;
  1968. #define GET_sqlite3_context_INSTANCE() SQ_SQLITE3_GETCONTEXT(v,1)
  1969. static SQRESULT sq_sqlite3_context__tostring(HSQUIRRELVM v)
  1970. {
  1971. SQ_FUNC_VARS_NO_TOP(v);
  1972. GET_sqlite3_context_INSTANCE();
  1973. char buff[64];
  1974. snprintf(buff, sizeof(buff), "sqlite function context (%p)", self);
  1975. sq_pushstring(v, buff, -1);
  1976. return 1;
  1977. }
  1978. static SQRESULT sq_sqlite3_context_check_aggregate(HSQUIRRELVM v, sq_sqlite3_context_st *ctx)
  1979. {
  1980. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(ctx->ctx);
  1981. if (sq_isclosure(func->fn_finalize))
  1982. {
  1983. return sq_throwerror(v, "attempt to call aggregate method from scalar function");
  1984. }
  1985. return 1;
  1986. }
  1987. static SQRESULT sq_sqlite3_context_user_data(HSQUIRRELVM v)
  1988. {
  1989. SQ_FUNC_VARS_NO_TOP(v);
  1990. GET_sqlite3_context_INSTANCE();
  1991. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(self->ctx);
  1992. sq_pushobject(v, func->udata);
  1993. return 1;
  1994. }
  1995. static SQRESULT sq_sqlite3_context_aggregate_data(HSQUIRRELVM v)
  1996. {
  1997. SQ_FUNC_VARS(v);
  1998. GET_sqlite3_context_INSTANCE();
  1999. sq_sqlite3_context_check_aggregate(v, self);
  2000. if(_top_ < 2)
  2001. {
  2002. sq_pushobject(v, self->udata);
  2003. }
  2004. else
  2005. {
  2006. sq_release(v, &self->udata);
  2007. sq_getstackobj(v, 2, &self->udata);
  2008. sq_addref(v, &self->udata);
  2009. }
  2010. return 0;
  2011. }
  2012. static SQRESULT sq_sqlite3_context_aggregate_count(HSQUIRRELVM v)
  2013. {
  2014. SQ_FUNC_VARS_NO_TOP(v);
  2015. GET_sqlite3_context_INSTANCE();
  2016. sq_sqlite3_context_check_aggregate(v, self);
  2017. sq_pushinteger(v, sqlite3_aggregate_count(self->ctx));
  2018. return 1;
  2019. }
  2020. #if 0
  2021. void *sqlite3_get_auxdata(sqlite3_context*, int);
  2022. void sqlite3_set_auxdata(sqlite3_context*, int, void*, void (*)(void*));
  2023. #endif
  2024. static SQRESULT sq_sqlite3_context_result_blob(HSQUIRRELVM v)
  2025. {
  2026. SQ_FUNC_VARS_NO_TOP(v);
  2027. GET_sqlite3_context_INSTANCE();
  2028. SQ_GET_STRING(v, 2, blob);
  2029. sqlite3_result_blob(self->ctx, (const void*)blob, blob_size, SQLITE_TRANSIENT);
  2030. return 0;
  2031. }
  2032. static SQRESULT sq_sqlite3_context_result_double(HSQUIRRELVM v)
  2033. {
  2034. SQ_FUNC_VARS_NO_TOP(v);
  2035. GET_sqlite3_context_INSTANCE();
  2036. SQ_GET_FLOAT(v, 2, d);
  2037. sqlite3_result_double(self->ctx, d);
  2038. return 0;
  2039. }
  2040. static SQRESULT sq_sqlite3_context_result_error(HSQUIRRELVM v)
  2041. {
  2042. SQ_FUNC_VARS_NO_TOP(v);
  2043. GET_sqlite3_context_INSTANCE();
  2044. SQ_GET_STRING(v,2, err);
  2045. sqlite3_result_error(self->ctx, err, err_size);
  2046. return 0;
  2047. }
  2048. static SQRESULT sq_sqlite3_context_result_int(HSQUIRRELVM v)
  2049. {
  2050. SQ_FUNC_VARS_NO_TOP(v);
  2051. GET_sqlite3_context_INSTANCE();
  2052. SQ_GET_INTEGER(v, 2, i);
  2053. sqlite3_result_int(self->ctx, i);
  2054. return 0;
  2055. }
  2056. static SQRESULT sq_sqlite3_context_result_null(HSQUIRRELVM v)
  2057. {
  2058. SQ_FUNC_VARS_NO_TOP(v);
  2059. GET_sqlite3_context_INSTANCE();
  2060. sqlite3_result_null(self->ctx);
  2061. return 0;
  2062. }
  2063. static SQRESULT sq_sqlite3_context_result_text(HSQUIRRELVM v)
  2064. {
  2065. SQ_FUNC_VARS_NO_TOP(v);
  2066. GET_sqlite3_context_INSTANCE();
  2067. SQ_GET_STRING(v, 2, text);
  2068. sqlite3_result_text(self->ctx, text, text_size, SQLITE_TRANSIENT);
  2069. return 0;
  2070. }
  2071. static SQRESULT sq_sqlite3_context_result_subtype(HSQUIRRELVM v)
  2072. {
  2073. SQ_FUNC_VARS_NO_TOP(v);
  2074. GET_sqlite3_context_INSTANCE();
  2075. SQ_GET_INTEGER(v, 2, subtype);
  2076. sqlite3_result_subtype(self->ctx, subtype);
  2077. return 0;
  2078. }
  2079. /* no way to recover parameter value after function call
  2080. static SQRESULT sq_sqlite3_context_value_subtype(HSQUIRRELVM v)
  2081. {
  2082. SQ_FUNC_VARS_NO_TOP(v);
  2083. GET_sqlite3_context_INSTANCE();
  2084. SQ_GET_INTEGER(v, 2, param_idx);
  2085. sq_pushinteger(v, sqlite3_value_subtype(self->ctx, subtype));
  2086. return 1;
  2087. }
  2088. */
  2089. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_sqlite3_context_##name,nparams,tycheck}
  2090. static SQRegFunction sq_sqlite3_context_methods[] =
  2091. {
  2092. _DECL_FUNC(constructor, 1, _SC("x")),
  2093. _DECL_FUNC(user_data, 1, _SC("x")),
  2094. _DECL_FUNC(aggregate_data, -1, _SC("x.")),
  2095. _DECL_FUNC(aggregate_count, 1, _SC("x")),
  2096. _DECL_FUNC(result_null, 1, _SC("x")),
  2097. _DECL_FUNC(result_double, 2, _SC("xn")),
  2098. _DECL_FUNC(result_int, 2, _SC("xi")),
  2099. _DECL_FUNC(result_text, 2, _SC("xs")),
  2100. _DECL_FUNC(result_blob, 2, _SC("xs")),
  2101. _DECL_FUNC(result_error, 2, _SC("xs")),
  2102. _DECL_FUNC(result_subtype, 2, _SC("xi")),
  2103. //_DECL_FUNC(value_subtype, 2, _SC("xi")),
  2104. _DECL_FUNC(_tostring, -1, _SC("x")),
  2105. {0,0}
  2106. };
  2107. #undef _DECL_FUNC
  2108. /*
  2109. ** callback functions used when calling registered sql functions
  2110. */
  2111. static void sqlite3_push_value(HSQUIRRELVM v, sqlite3_value *value)
  2112. {
  2113. const char *tmp_value;
  2114. switch (sqlite3_value_type(value))
  2115. {
  2116. case SQLITE_TEXT:
  2117. tmp_value = (const char*) sqlite3_value_text(value);
  2118. if(tmp_value) sq_pushstring(v, tmp_value, sqlite3_value_bytes(value));
  2119. else push_sqlite3_null(v);
  2120. break;
  2121. case SQLITE_INTEGER:
  2122. {
  2123. sqlite_int64 i64 = sqlite3_value_int64(value);
  2124. SQInteger n = (SQInteger)i64;
  2125. if (n == i64)
  2126. sq_pushinteger(v, n);
  2127. else
  2128. sq_pushstring(v, (const char*) sqlite3_value_text(value), sqlite3_value_bytes(value));
  2129. }
  2130. break;
  2131. case SQLITE_FLOAT:
  2132. sq_pushfloat(v, sqlite3_value_double(value));
  2133. break;
  2134. case SQLITE_BLOB:
  2135. tmp_value = (const char*) sqlite3_value_blob(value);
  2136. if(tmp_value) sq_pushstring(v, tmp_value, sqlite3_value_bytes(value));
  2137. else push_sqlite3_null(v);
  2138. break;
  2139. case SQLITE_NULL:
  2140. push_sqlite3_null(v);
  2141. break;
  2142. default:
  2143. /* things done properly (SQLite + SquiLu SQLite)
  2144. ** this should never happen */
  2145. push_sqlite3_null(v);
  2146. break;
  2147. }
  2148. }
  2149. //#if 0
  2150. static SQRESULT new_context_instance(HSQUIRRELVM v, sq_sqlite3_context_st **ctx)
  2151. {
  2152. sq_pushregistrytable(v);
  2153. sq_pushstring(v,_SC("sqite3_context"),-1);
  2154. int rc = sq_rawget(v, -2);
  2155. sq_remove(v, -2); //remove registrytable
  2156. sq_pushroottable(v);
  2157. rc = sq_call(v, 1, SQTrue, SQFalse);
  2158. sq_remove(v, -2); //class
  2159. rc = sq_getinstanceup(v, -1, (void**)ctx, (void*)sq_sqlite3_context_TAG);
  2160. return rc;
  2161. }
  2162. /* scalar function to be called
  2163. ** callback params: context, values... */
  2164. static void db_sql_normal_function(sqlite3_context *context, int argc, sqlite3_value **argv)
  2165. {
  2166. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(context);
  2167. HSQUIRRELVM v = func->sdb->v;
  2168. int n;
  2169. sq_sqlite3_context_st *ctx;
  2170. int top = sq_gettop(v);
  2171. /* ensure there is enough space in the stack */
  2172. sq_reservestack(v, argc + 5);
  2173. sq_pushobject(v, func->fn_step);
  2174. sq_pushroottable(v);
  2175. if (!sq_isclosure(func->fn_finalize))
  2176. {
  2177. new_context_instance(v, &ctx);
  2178. }
  2179. else
  2180. {
  2181. /* reuse context userdata value */
  2182. void *p = sqlite3_aggregate_context(context, 1);
  2183. /* i think it is OK to use assume that using a light user data
  2184. ** as an entry on SquiLu REGISTRY table will be unique */
  2185. sq_pushregistrytable(v);
  2186. sq_pushuserpointer(v, p);
  2187. /* context table */
  2188. if(sq_rawget(v, -2) != SQ_OK)
  2189. {
  2190. /* not yet created? */
  2191. sq_poptop(v); //remove null
  2192. new_context_instance(v, &ctx);
  2193. sq_pushuserpointer(v, p);
  2194. sq_push(v, -2);
  2195. sq_rawset(v, -4); //insert into registrytable
  2196. }
  2197. sq_remove(v, -2); //remove registrytable
  2198. }
  2199. /* push params */
  2200. for (n = 0; n < argc; ++n)
  2201. {
  2202. sqlite3_push_value(v, argv[n]);
  2203. }
  2204. /* set context */
  2205. ctx->ctx = context;
  2206. if (sq_call(v, argc + 2, SQFalse, SQFalse) != SQ_OK) //2 = roottable + ctx
  2207. {
  2208. sqlite3_result_error(context, sq_getlasterror_str(v), -1);
  2209. }
  2210. /* invalidate context */
  2211. ctx->ctx = NULL;
  2212. if (!sq_isclosure(func->fn_finalize))
  2213. {
  2214. sq_release(v, &ctx->udata);
  2215. sq_resetobject(&ctx->udata);
  2216. }
  2217. sq_settop(v, top);
  2218. }
  2219. static void db_sql_finalize_function(sqlite3_context *context)
  2220. {
  2221. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(context);
  2222. HSQUIRRELVM v = func->sdb->v;
  2223. void *p = sqlite3_aggregate_context(context, 1); /* minimal mem usage */
  2224. sq_sqlite3_context_st *ctx;
  2225. int top = sq_gettop(v);
  2226. sq_pushobject(v, func->fn_finalize);
  2227. sq_pushroottable(v);
  2228. /* i think it is OK to use assume that using a light user data
  2229. ** as an entry on SquiLu REGISTRY table will be unique */
  2230. sq_pushregistrytable(v);
  2231. sq_pushuserpointer(v, p);
  2232. /* remove it from registry but we'll use it last time here */
  2233. /* context table */
  2234. if(sq_deleteslot(v, -2, SQTrue) != SQ_OK)
  2235. {
  2236. /* not yet created? - shouldn't happen in finalize function */
  2237. sq_pop(v, 1);
  2238. new_context_instance(v, &ctx);
  2239. sq_pushuserpointer(v, p);
  2240. sq_push(v, -2);
  2241. sq_rawset(v, -4);
  2242. }
  2243. sq_remove(v, -2); //registrytable
  2244. /* set context */
  2245. ctx->ctx = context;
  2246. if (sq_call(v, 1, SQFalse, SQFalse) != SQ_OK)
  2247. {
  2248. sqlite3_result_error(context, sq_getlasterror_str(v), -1);
  2249. }
  2250. /* invalidate context */
  2251. ctx->ctx = NULL;
  2252. /* cleanup context */
  2253. sq_release(v, &ctx->udata);
  2254. sq_resetobject(&ctx->udata);
  2255. sq_settop(v, top);
  2256. }
  2257. /*
  2258. ** Register a normal function
  2259. ** Params: db, function name, number arguments, [ callback | step, finalize], user data
  2260. ** Returns: true on sucess
  2261. **
  2262. ** Normal function:
  2263. ** Params: context, params
  2264. **
  2265. ** Aggregate function:
  2266. ** Params of step: context, params
  2267. ** Params of finalize: context
  2268. */
  2269. static SQRESULT db_register_function(HSQUIRRELVM v, int aggregate)
  2270. {
  2271. SQ_FUNC_VARS(v);
  2272. SQInteger ftype = 0;
  2273. GET_sqlite3_INSTANCE();
  2274. SQ_GET_STRING(v, 2, name);
  2275. SQ_GET_INTEGER(v, 3, nargs);
  2276. if(sq_gettype(v, 4) != OT_CLOSURE)
  2277. return sq_throwerror(v, "invalid parameter 3 expected closure");
  2278. if (aggregate)
  2279. {
  2280. if(sq_gettype(v,5) != OT_CLOSURE)
  2281. return sq_throwerror(v, "invalid parameter 4 expected closure");
  2282. } else {
  2283. sq_optinteger(v, 5, &ftype, 0);
  2284. }
  2285. sq_sqlite3_sdb_func *func;
  2286. /* maybe an alternative way to allocate memory should be used/avoided */
  2287. func = (sq_sqlite3_sdb_func*)sq_malloc(sizeof(sq_sqlite3_sdb_func));
  2288. memset(func, 0, sizeof(sq_sqlite3_sdb_func));
  2289. _rc_ = sqlite3_create_function(
  2290. self, name, nargs, SQLITE_UTF8 | (ftype == SQLITE_DETERMINISTIC ? ftype : 0),
  2291. func,
  2292. aggregate ? NULL : db_sql_normal_function,
  2293. aggregate ? db_sql_normal_function : NULL,
  2294. aggregate ? db_sql_finalize_function : NULL
  2295. );
  2296. if (_rc_ == SQLITE_OK)
  2297. {
  2298. /* save registered function in db function list */
  2299. func->sdb = sdb;
  2300. func->next = sdb->func;
  2301. sdb->func = func;
  2302. /* save the setp/normal function callback */
  2303. sq_resetobject(&func->fn_step);
  2304. sq_getstackobj(v, 4, &func->fn_step);
  2305. sq_addref(v, &func->fn_step);
  2306. /* save the finalize function callback */
  2307. sq_resetobject(&func->fn_finalize);
  2308. if (aggregate)
  2309. {
  2310. sq_getstackobj(v, 5, &func->fn_finalize);
  2311. sq_addref(v, &func->fn_finalize);
  2312. }
  2313. /* save user data */
  2314. sq_resetobject(&func->udata);
  2315. int udata_idx = aggregate ? 6 : 5;
  2316. if(_top_ >= udata_idx)
  2317. {
  2318. sq_getstackobj(v, udata_idx, &func->udata);
  2319. sq_addref(v, &func->udata);
  2320. }
  2321. }
  2322. else
  2323. {
  2324. /* free allocated memory */
  2325. sq_free(func, sizeof(sq_sqlite3_sdb_func));
  2326. }
  2327. sq_pushbool(v, _rc_ == SQLITE_OK ? 1 : 0);
  2328. return 1;
  2329. }
  2330. //#endif
  2331. static SQRESULT sq_sqlite3_create_function(HSQUIRRELVM v)
  2332. {
  2333. return db_register_function(v, 0);
  2334. }
  2335. static SQRESULT sq_sqlite3_create_aggregate(HSQUIRRELVM v)
  2336. {
  2337. return db_register_function(v, 1);
  2338. }
  2339. static SQRESULT sq_sqlite3_backup(HSQUIRRELVM v)
  2340. {
  2341. SQ_FUNC_VARS(v);
  2342. GET_sqlite3_INSTANCE();
  2343. SQ_GET_STRING(v, 2, fname);
  2344. SQ_OPT_STRING(v, 3, dbname, _SC("main"));
  2345. sqlite3 *pDest;
  2346. sqlite3_backup *pBackup;
  2347. int rc = sqlite3_open(fname, &pDest);
  2348. if(rc!=SQLITE_OK)
  2349. {
  2350. sqlite3_close(pDest);
  2351. return sq_throwerror(v, _SC("SQLite can't open %s"), fname);
  2352. }
  2353. pBackup = sqlite3_backup_init(pDest, "main", self, dbname);
  2354. if( pBackup==0 ){
  2355. rc = sq_throwerror(v, _SC("Error: %s\n"), sqlite3_errmsg(pDest));
  2356. sqlite3_close(pDest);
  2357. }
  2358. while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
  2359. sqlite3_backup_finish(pBackup);
  2360. if( rc==SQLITE_DONE ){
  2361. rc = SQ_OK;
  2362. }else{
  2363. rc = sq_throwerror(v, _SC("Error: %s\n"), sqlite3_errmsg(pDest));
  2364. }
  2365. sqlite3_close(pDest);
  2366. return rc;
  2367. }
  2368. static SQRESULT sq_sqlite3_restore(HSQUIRRELVM v)
  2369. {
  2370. SQ_FUNC_VARS(v);
  2371. GET_sqlite3_INSTANCE();
  2372. SQ_GET_STRING(v, 2, fname);
  2373. SQ_OPT_STRING(v, 3, dbname, _SC("main"));
  2374. sqlite3 *pSrc;
  2375. sqlite3_backup *pBackup;
  2376. int nTimeout = 0;
  2377. int rc = sqlite3_open(fname, &pSrc);
  2378. if(rc!=SQLITE_OK)
  2379. {
  2380. sqlite3_close(pSrc);
  2381. return sq_throwerror(v, _SC("SQLite can't open %s"), fname);
  2382. }
  2383. pBackup = sqlite3_backup_init(self, "main", pSrc, dbname);
  2384. if( pBackup==0 ){
  2385. sqlite3_close(pSrc);
  2386. return sq_throwerror(v, _SC("Error: %s\n"), sqlite3_errmsg(self));
  2387. }
  2388. while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
  2389. || rc==SQLITE_BUSY ){
  2390. if( rc==SQLITE_BUSY ){
  2391. if( nTimeout++ >= 3 ) break;
  2392. sqlite3_sleep(100);
  2393. }
  2394. }
  2395. sqlite3_backup_finish(pBackup);
  2396. if( rc==SQLITE_DONE ){
  2397. rc = SQ_OK;
  2398. }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
  2399. rc = sq_throwerror(v, _SC("Error: source database is busy\n"));
  2400. }else{
  2401. rc = sq_throwerror(v, _SC("Error: %s\n"), sqlite3_errmsg(self));
  2402. }
  2403. sqlite3_close(pSrc);
  2404. return rc;
  2405. }
  2406. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_sqlite3_##name,nparams,tycheck}
  2407. static SQRegFunction sq_sqlite3_methods[] =
  2408. {
  2409. _DECL_FUNC(constructor, -2, _SC("xsi")),
  2410. _DECL_FUNC(close, 1, _SC("x")),
  2411. _DECL_FUNC(db_ptr, 1, _SC("x")),
  2412. _DECL_FUNC(IsAutoCommitOn, 1, _SC("x")),
  2413. _DECL_FUNC(version, 1, _SC("x|y")),
  2414. _DECL_FUNC(threadsafe, 1, _SC("x|y")),
  2415. _DECL_FUNC(config_single_thread, 1, _SC("y")),
  2416. _DECL_FUNC(config_multi_thread, 1, _SC("y")),
  2417. _DECL_FUNC(config_serialized, 1, _SC("y")),
  2418. _DECL_FUNC(errcode, 1, _SC("x")),
  2419. _DECL_FUNC(errmsg, 1, _SC("x")),
  2420. _DECL_FUNC(sleep, 1, _SC("x")),
  2421. _DECL_FUNC(interrupt, 1, _SC("x")),
  2422. _DECL_FUNC(progress_handler, -2, _SC("x i|o c .")),
  2423. _DECL_FUNC(trace, -2, _SC("x c|o .")),
  2424. _DECL_FUNC(update_hook, -2, _SC("x c|o .")),
  2425. _DECL_FUNC(set_authorizer, -2, _SC("x c|o .")),
  2426. _DECL_FUNC(busy_handler, -2, _SC("x c|o .")),
  2427. _DECL_FUNC(busy_timeout, 2, _SC("xi")),
  2428. _DECL_FUNC(create_function, -4, _SC("xsic.")),
  2429. _DECL_FUNC(create_aggregate, -5, _SC("xsicc.")),
  2430. #ifndef WIN32
  2431. _DECL_FUNC(temp_directory, 2, _SC("xs")),
  2432. #endif
  2433. _DECL_FUNC(enable_shared_cache, 2, _SC("xb")),
  2434. _DECL_FUNC(changes, 1, _SC("x")),
  2435. _DECL_FUNC(exec, -2, _SC("xs")),
  2436. _DECL_FUNC(exec_dml, 2, _SC("xs")),
  2437. _DECL_FUNC(exec_get_first_row, 2, _SC("xs")),
  2438. _DECL_FUNC(exec_get_one, 2, _SC("xs")),
  2439. _DECL_FUNC(get_db_name, 1, _SC("x")),
  2440. _DECL_FUNC(last_row_id, 1, _SC("x")),
  2441. _DECL_FUNC(prepare, 2, _SC("xs")),
  2442. _DECL_FUNC(set_busy_timeout, -1, _SC("xi")),
  2443. _DECL_FUNC(total_changes, 1, _SC("x")),
  2444. _DECL_FUNC(backup, -2, _SC("xss")),
  2445. _DECL_FUNC(restore, -2, _SC("xss")),
  2446. #ifdef SQLITE_HAS_CODEC
  2447. _DECL_FUNC(key, 2, _SC("xs")),
  2448. _DECL_FUNC(rekey, 2, _SC("xs")),
  2449. #endif
  2450. {0,0}
  2451. };
  2452. #undef _DECL_FUNC
  2453. #define INT_CONST(v,num) sq_pushstring(v,_SC(#num),-1);sq_pushinteger(v,num);sq_newslot(v,-3,SQTrue);
  2454. #ifdef __cplusplus
  2455. extern "C" {
  2456. #endif
  2457. SQRESULT sqext_register_SQLite3(HSQUIRRELVM v)
  2458. {
  2459. sq_insertfunc(v, _SC("sle2vecOfvec"), sq_sle2vecOfvec, -2, _SC(". s|x a i"), SQTrue);
  2460. sq_insertfunc(v, _SC("get_sle_size"), sq_get_sle_size, -2, _SC(".i"), SQTrue);
  2461. sq_insertfunc(v, _SC("add2sle"), sq_add2sle, -3, _SC(".xsi"), SQTrue);
  2462. sq_pushconsttable(v);
  2463. #define INT_SLE_CONST(num) sq_pushstring(v,_SC("SLE_" #num),-1);sq_pushinteger(v,num);sq_newslot(v,-3,SQTrue);
  2464. INT_SLE_CONST(IBYTE1);
  2465. INT_SLE_CONST(IBYTE2);
  2466. INT_SLE_CONST(IBYTE3);
  2467. INT_SLE_CONST(IBYTE4);
  2468. INT_SLE_CONST(SIZE1BYTE);
  2469. INT_SLE_CONST(SIZE2BYTE);
  2470. INT_SLE_CONST(SIZE3BYTE);
  2471. INT_SLE_CONST(SIZE4BYTE);
  2472. INT_SLE_CONST(SLEMARK);
  2473. INT_SLE_CONST(SLEEND);
  2474. sq_poptop(v); //remove const table
  2475. HSQOBJECT sqlite3_NULL;
  2476. sq_pushregistrytable(v);
  2477. sq_pushstring(v, sqlite3_NULL_Name,-1);
  2478. sq_newuserdata(v, sizeof(void*));
  2479. sq_settypetag(v, -1, (SQUserPointer)sqlite3_NULL_Name);
  2480. sq_resetobject(&sqlite3_NULL);
  2481. sq_getstackobj(v, -1, &sqlite3_NULL);
  2482. sq_newtable(v);
  2483. sq_insert_reg_funcs(v, sqlite3_NULL_methods);
  2484. sq_setdelegate(v, -2);
  2485. sq_newslot(v,-3,SQTrue);
  2486. sq_pushstring(v,_SC("sqite3_context"),-1);
  2487. sq_newclass(v,SQFalse);
  2488. sq_settypetag(v,-1,(SQUserPointer)sq_sqlite3_context_TAG);
  2489. sq_insert_reg_funcs(v, sq_sqlite3_context_methods);
  2490. sq_newslot(v,-3,SQTrue);
  2491. sq_poptop(v); //remove registrytable
  2492. sq_pushstring(v,SQLite3_TAG,-1);
  2493. sq_newclass(v,SQFalse);
  2494. sq_settypetag(v,-1,(SQUserPointer)SQLite3_TAG);
  2495. sq_insert_reg_funcs(v, sq_sqlite3_methods);
  2496. INT_CONST(v,SQLITE_OPEN_CREATE);
  2497. INT_CONST(v,SQLITE_OPEN_READWRITE);
  2498. INT_CONST(v,SQLITE_OPEN_SHAREDCACHE);
  2499. INT_CONST(v,SQLITE_OPEN_SUBLATIN_NA_LIKE);
  2500. INT_CONST(v,SQLITE_OK);
  2501. INT_CONST(v,SQLITE_BUSY);
  2502. INT_CONST(v,SQLITE_DONE);
  2503. INT_CONST(v,SQLITE_DETERMINISTIC);
  2504. INT_CONST(v,SQLITE_ERROR);
  2505. INT_CONST(v,SQLITE_INTERRUPT);
  2506. INT_CONST(v,SQLITE_MISUSE);
  2507. INT_CONST(v,SQLITE_ROW);
  2508. INT_CONST(v,SQLITE_LOCKED_SHAREDCACHE);
  2509. INT_CONST(v,SQLITE_BUSY_RECOVERY);
  2510. INT_CONST(v,SQLITE_BUSY_SNAPSHOT);
  2511. INT_CONST(v,SQLITE_CANTOPEN_NOTEMPDIR);
  2512. INT_CONST(v,SQLITE_CANTOPEN_ISDIR);
  2513. INT_CONST(v,SQLITE_CANTOPEN_FULLPATH);
  2514. INT_CONST(v,SQLITE_CANTOPEN_CONVPATH);
  2515. INT_CONST(v,SQLITE_CORRUPT_VTAB);
  2516. INT_CONST(v,SQLITE_READONLY_RECOVERY);
  2517. INT_CONST(v,SQLITE_READONLY_CANTLOCK);
  2518. INT_CONST(v,SQLITE_READONLY_ROLLBACK);
  2519. INT_CONST(v,SQLITE_READONLY_DBMOVED);
  2520. INT_CONST(v,SQLITE_ABORT_ROLLBACK);
  2521. INT_CONST(v,SQLITE_CONSTRAINT_CHECK);
  2522. INT_CONST(v,SQLITE_CONSTRAINT_COMMITHOOK);
  2523. INT_CONST(v,SQLITE_CONSTRAINT_FOREIGNKEY);
  2524. INT_CONST(v,SQLITE_CONSTRAINT_FUNCTION);
  2525. INT_CONST(v,SQLITE_CONSTRAINT_NOTNULL);
  2526. INT_CONST(v,SQLITE_CONSTRAINT_PRIMARYKEY);
  2527. INT_CONST(v,SQLITE_CONSTRAINT_TRIGGER);
  2528. INT_CONST(v,SQLITE_CONSTRAINT_UNIQUE);
  2529. INT_CONST(v,SQLITE_CONSTRAINT_VTAB);
  2530. INT_CONST(v,SQLITE_CONSTRAINT_ROWID);
  2531. INT_CONST(v,SQLITE_NOTICE_RECOVER_WAL);
  2532. INT_CONST(v,SQLITE_NOTICE_RECOVER_ROLLBACK);
  2533. INT_CONST(v,SQLITE_WARNING_AUTOINDEX);
  2534. /*Authorizer codes*/
  2535. INT_CONST(v, SQLITE_IGNORE);
  2536. INT_CONST(v, SQLITE_DENY);
  2537. INT_CONST(v, SQLITE_CREATE_INDEX);
  2538. INT_CONST(v, SQLITE_CREATE_TABLE);
  2539. INT_CONST(v, SQLITE_CREATE_TEMP_INDEX);
  2540. INT_CONST(v, SQLITE_CREATE_TEMP_TABLE);
  2541. INT_CONST(v, SQLITE_CREATE_TEMP_TRIGGER);
  2542. INT_CONST(v, SQLITE_CREATE_TEMP_VIEW);
  2543. INT_CONST(v, SQLITE_CREATE_TRIGGER);
  2544. INT_CONST(v, SQLITE_CREATE_VIEW);
  2545. INT_CONST(v, SQLITE_DELETE);
  2546. INT_CONST(v, SQLITE_DROP_INDEX);
  2547. INT_CONST(v, SQLITE_DROP_TABLE);
  2548. INT_CONST(v, SQLITE_DROP_TEMP_INDEX);
  2549. INT_CONST(v, SQLITE_DROP_TEMP_TABLE);
  2550. INT_CONST(v, SQLITE_DROP_TEMP_TRIGGER);
  2551. INT_CONST(v, SQLITE_DROP_TEMP_VIEW);
  2552. INT_CONST(v, SQLITE_DROP_TRIGGER);
  2553. INT_CONST(v, SQLITE_DROP_VIEW);
  2554. INT_CONST(v, SQLITE_INSERT);
  2555. INT_CONST(v, SQLITE_PRAGMA);
  2556. INT_CONST(v, SQLITE_READ);
  2557. INT_CONST(v, SQLITE_SELECT);
  2558. INT_CONST(v, SQLITE_TRANSACTION);
  2559. INT_CONST(v, SQLITE_UPDATE);
  2560. INT_CONST(v, SQLITE_ATTACH);
  2561. INT_CONST(v, SQLITE_DETACH);
  2562. INT_CONST(v, SQLITE_ALTER_TABLE);
  2563. INT_CONST(v, SQLITE_REINDEX);
  2564. INT_CONST(v, SQLITE_ANALYZE);
  2565. INT_CONST(v, SQLITE_CREATE_VTABLE);
  2566. INT_CONST(v, SQLITE_DROP_VTABLE);
  2567. INT_CONST(v, SQLITE_FUNCTION);
  2568. INT_CONST(v, SQLITE_SAVEPOINT);
  2569. INT_CONST(v, SQLITE_COPY);
  2570. INT_CONST(v, SQLITE_RECURSIVE);
  2571. //push sqlite3_NULL as a member
  2572. sq_pushstring(v, nullName,-1);
  2573. sq_pushobject(v, sqlite3_NULL);
  2574. sq_newslot(v,-3,SQTrue);
  2575. sq_newslot(v,-3,SQTrue);
  2576. sq_pushstring(v, SQLite3_Stmt_TAG,-1);
  2577. sq_newclass(v,SQFalse);
  2578. sq_settypetag(v,-1,(SQUserPointer)SQLite3_Stmt_TAG);
  2579. sq_insert_reg_funcs(v, sq_sqlite3_stmt_methods);
  2580. INT_CONST(v,SQLITE_INTEGER);
  2581. INT_CONST(v,SQLITE_FLOAT);
  2582. INT_CONST(v,SQLITE_NULL);
  2583. INT_CONST(v,SQLITE_TEXT);
  2584. INT_CONST(v,SQLITE_BLOB);
  2585. INT_CONST(v,SQLITE_OK);
  2586. INT_CONST(v,SQLITE_DONE);
  2587. INT_CONST(v,SQLITE_ROW);
  2588. INT_CONST(v,AS_STRING_ALWAYS);
  2589. INT_CONST(v,NULL_AS_EMPTY_STR);
  2590. INT_CONST(v,WITH_COL_NAMES);
  2591. INT_CONST(v,WITH_BLOB_AS_NULL);
  2592. INT_CONST(v,WITH_SQ_NULL);
  2593. //push sqlite3_NULL as a member
  2594. sq_pushstring(v, nullName,-1);
  2595. sq_pushobject(v, sqlite3_NULL);
  2596. sq_newslot(v,-3,SQTrue);
  2597. sq_newslot(v,-3,SQTrue);
  2598. return 1;
  2599. }
  2600. #ifdef __cplusplus
  2601. }
  2602. #endif