sq_sqlite3.cpp 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930
  1. /* Code generated by script */
  2. #include "sq_sqlite3.h"
  3. #include <assert.h>
  4. #include <stdlib.h>
  5. #include "lua-regex.h"
  6. #include "squtils.h"
  7. SQ_OPT_STRING_STRLEN();
  8. const SQChar *SQLite3_TAG = "SQLite3";
  9. static const SQChar *SQLite3_Stmt_TAG = "SQLite3Stmt";
  10. static const SQChar *SQLite3_Blob_TAG = "SQLite3Blob";
  11. static const SQChar sqlite3_NULL_Name[] = _SC("sqlite3_NULL");
  12. static const SQChar nullName[] = _SC("Null");
  13. //return error if parameter exists but is not a blob
  14. //return 1 if the parameter exists and is a blob
  15. //return 0 if the parameter doesn't exists and creates a new blob
  16. static SQRESULT getOptionalBlob(HSQUIRRELVM v, SQInteger idx, SQBlob **blob)
  17. {
  18. if(sq_gettop(v) >= idx) {
  19. if(SQ_FAILED(sq_getinstanceup(v,3,(SQUserPointer*)blob,(SQUserPointer)SQBlob::SQBlob_TAG)))
  20. return sq_throwerror(v,_SC("expect a blob as second parameter"));
  21. if(!(*blob) || !(*blob)->IsValid())
  22. return sq_throwerror(v,_SC("the blob is invalid"));
  23. } else {
  24. SQBlob *nblob;
  25. sq_new(nblob, SQBlob);
  26. *blob = nblob;
  27. return 0;
  28. }
  29. return 1;
  30. }
  31. static SQRESULT sqlite3_NULL_tostring(HSQUIRRELVM v)
  32. {
  33. sq_pushstring(v, "", 0);
  34. return 1;
  35. }
  36. SQRegFunction sqlite3_NULL_methods[]=
  37. {
  38. {_SC("_tostring"),sqlite3_NULL_tostring,1, _SC("u")},
  39. {0,0}
  40. };
  41. static SQRESULT get_sqlite3_instance(HSQUIRRELVM v, SQInteger idx, sq_sqlite3_sdb **sdb)
  42. {
  43. SQRESULT _rc_;
  44. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)sdb,(void*)SQLite3_TAG)) < 0) return _rc_;
  45. if(!*sdb) return sq_throwerror(v, _SC("database is closed"));
  46. return _rc_;
  47. }
  48. static SQRESULT get_sqlite3_stmt_instance(HSQUIRRELVM v, SQInteger idx, sqlite3_stmt **stmt)
  49. {
  50. SQRESULT _rc_;
  51. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)stmt,(void*)SQLite3_Stmt_TAG)) < 0) return _rc_;
  52. if(!*stmt) return sq_throwerror(v, _SC("statement is closed"));
  53. return _rc_;
  54. }
  55. static SQRESULT get_sqlite3_blob_instance(HSQUIRRELVM v, SQInteger idx, sqlite3_blob **blob)
  56. {
  57. SQRESULT _rc_;
  58. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)blob,(void*)SQLite3_Blob_TAG)) < 0) return _rc_;
  59. if(!*blob) return sq_throwerror(v, _SC("blob is closed"));
  60. return _rc_;
  61. }
  62. //#define push_sqlite3_null(v) sq_getonregistrytable(v, sqlite3_NULL_Name, sizeof(sqlite3_NULL_Name)-1);
  63. //#define push_sqlite3_null(v) sq_pushobject(v, sqlite3_NULL);
  64. //#define push_sqlite3_null(v) sq_getbyname(v, 1, nullName, sizeof(nullName)-1)
  65. //#define push_sqlite3_null(v) sq_pushfromregistrytable(v, nullName, sizeof(nullName)-1)
  66. //#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);}
  67. //the line bellow is the fastest one, any other attempt till now was slower
  68. #define push_sqlite3_null(v) {sq_pushstring(v, nullName, sizeof(nullName)-1);sq_get(v, 1);}
  69. #define GET_sqlite3_INSTANCE_AT_VAR(idx, self) \
  70. sq_sqlite3_sdb *sdb=NULL; \
  71. if((_rc_ = get_sqlite3_instance(v,idx,&sdb)) < 0) return _rc_;\
  72. sqlite3 *self = sdb->db;
  73. #define GET_sqlite3_INSTANCE_AT(idx) GET_sqlite3_INSTANCE_AT_VAR(idx, self)
  74. #define GET_sqlite3_INSTANCE() GET_sqlite3_INSTANCE_AT(1)
  75. //#define GET_sqlite3_INSTANCE() SQ_GET_INSTANCE(v, 1, sqlite3, SQLite3_TAG)
  76. #define GET_sqlite3_stmt_INSTANCE_AT(idx, var_name) \
  77. sqlite3_stmt *var_name=NULL; \
  78. if((_rc_ = get_sqlite3_stmt_instance(v,idx,&var_name)) < 0) return _rc_;
  79. #define GET_sqlite3_stmt_INSTANCE() GET_sqlite3_stmt_INSTANCE_AT(1, self)
  80. #define GET_sqlite3_blob_INSTANCE_AT(idx, var_name) \
  81. sqlite3_blob *var_name=NULL; \
  82. if((_rc_ = get_sqlite3_blob_instance(v,idx,&var_name)) < 0) return _rc_;
  83. #define GET_sqlite3_blob_INSTANCE() GET_sqlite3_blob_INSTANCE_AT(1, self)
  84. enum e_type_result {tr_first_row_first_col, tr_first_row, tr_all_rows, tr_ddml};
  85. #define AS_STRING_ALWAYS 0x01
  86. #define NULL_AS_EMPTY_STR 0x02
  87. #define WITH_COL_NAMES 0x04
  88. #define WITH_BLOB_AS_NULL 0x08
  89. #define WITH_SQ_NULL 0x100
  90. static void sqlite3_stmt_push_string(HSQUIRRELVM v, sqlite3_stmt *stmt, int col, int flags)
  91. {
  92. const char *value = (const char*) sqlite3_column_text(stmt, col);
  93. if(value) sq_pushstring(v, value, sqlite3_column_bytes(stmt, col));
  94. else
  95. {
  96. if(flags & AS_STRING_ALWAYS) sq_pushliteral(v, "");
  97. else push_sqlite3_null(v);
  98. }
  99. }
  100. static SQRESULT sqlite3_stmt_push_value(HSQUIRRELVM v, sqlite3_stmt *stmt, int col, int flags)
  101. {
  102. const char *value;
  103. int col_type = 0;
  104. if(flags & WITH_BLOB_AS_NULL)
  105. {
  106. col_type = sqlite3_column_type(stmt, col);
  107. if(col_type == SQLITE_BLOB)
  108. {
  109. if(flags & AS_STRING_ALWAYS) sq_pushliteral(v, "");
  110. else push_sqlite3_null(v);
  111. return 1;
  112. }
  113. }
  114. if(flags & AS_STRING_ALWAYS)
  115. {
  116. sqlite3_stmt_push_string(v, stmt, col, flags);
  117. }
  118. else
  119. {
  120. if(!col_type) col_type = sqlite3_column_type(stmt, col);
  121. switch (col_type)
  122. {
  123. case SQLITE_INTEGER:
  124. {
  125. sqlite_int64 i64 = sqlite3_column_int64(stmt, col);
  126. SQInteger n = (SQInteger)i64;
  127. if (n == i64)
  128. sq_pushinteger(v, n);
  129. else
  130. sq_pushstring(v, (const char*) sqlite3_column_text(stmt, col), sqlite3_column_bytes(stmt, col));
  131. }
  132. break;
  133. case SQLITE_FLOAT:
  134. sq_pushfloat(v, sqlite3_column_double(stmt, col));
  135. break;
  136. case SQLITE_TEXT:
  137. sqlite3_stmt_push_string(v, stmt, col, flags);
  138. break;
  139. case SQLITE_BLOB:
  140. value = (const char*) sqlite3_column_blob(stmt, col);
  141. if(value) sq_pushstring(v, value, sqlite3_column_bytes(stmt, col));
  142. else push_sqlite3_null(v);
  143. break;
  144. case SQLITE_NULL:
  145. if(flags & WITH_SQ_NULL) sq_pushnull(v);
  146. else push_sqlite3_null(v);
  147. break;
  148. default:
  149. if(flags & WITH_SQ_NULL) sq_pushnull(v);
  150. else push_sqlite3_null(v);
  151. break;
  152. }
  153. }
  154. return 1;
  155. }
  156. static void sqlite3_stmt_row_asArray(HSQUIRRELVM v, sqlite3_stmt *stmt, int flags)
  157. {
  158. int col_count = sqlite3_column_count(stmt);
  159. sq_newarray(v, col_count);
  160. for(int i=0; i<col_count; ++i)
  161. {
  162. sqlite3_stmt_push_value(v, stmt, i, flags);
  163. sq_arrayset(v, -2, i);
  164. }
  165. }
  166. static void sqlite3_stmt_row_asTable(HSQUIRRELVM v, sqlite3_stmt *stmt, int flags)
  167. {
  168. int col_count = sqlite3_column_count(stmt);
  169. sq_newtable(v);
  170. for(int i=0; i<col_count; ++i)
  171. {
  172. sq_pushstring(v, sqlite3_column_name(stmt, i), -1);
  173. sqlite3_stmt_push_value(v, stmt, i, flags);
  174. sq_rawset(v, -3);
  175. }
  176. }
  177. static void sqlite3_stmt_asArrayOfArrays(HSQUIRRELVM v, sqlite3_stmt *stmt, int flags)
  178. {
  179. sq_newarray(v, 0);
  180. if(flags & WITH_COL_NAMES)
  181. {
  182. int col_count = sqlite3_column_count(stmt);
  183. sq_newarray(v, col_count);
  184. for(int i=0; i<col_count; ++i)
  185. {
  186. sq_pushstring(v, sqlite3_column_name(stmt, i), -1);
  187. sq_arrayset(v, -2, i);
  188. }
  189. sq_arrayappend(v, -2);
  190. }
  191. while(sqlite3_step(stmt) == SQLITE_ROW)
  192. {
  193. sqlite3_stmt_row_asArray(v, stmt, flags);
  194. sq_arrayappend(v, -2);
  195. }
  196. }
  197. static void sqlite3_stmt_asArrayOfTables(HSQUIRRELVM v, sqlite3_stmt *stmt, int flags)
  198. {
  199. sq_newarray(v, 0);
  200. while(sqlite3_step(stmt) == SQLITE_ROW)
  201. {
  202. sqlite3_stmt_row_asTable(v, stmt, flags);
  203. sq_arrayappend(v, -2);
  204. }
  205. }
  206. static SQRESULT sqlite3_stmt_bind_value(HSQUIRRELVM v, sqlite3_stmt *stmt, int npar, int argn)
  207. {
  208. int _rc_ = 0;
  209. SQObjectType ptype = sq_gettype(v, argn);
  210. switch(ptype)
  211. {
  212. case OT_BOOL:
  213. SQ_GET_BOOL(v, argn, param_bool);
  214. _rc_ = sqlite3_bind_int(stmt, npar, param_bool == SQTrue ? 1 : 0);
  215. break;
  216. case OT_INTEGER:
  217. SQ_GET_INTEGER(v, argn, param_integer);
  218. _rc_ = sqlite3_bind_int64(stmt, npar, param_integer);
  219. break;
  220. case OT_FLOAT:
  221. SQ_GET_FLOAT(v, argn, param_float);
  222. _rc_ = sqlite3_bind_double(stmt, npar, param_float);
  223. break;
  224. case OT_STRING:
  225. {
  226. SQ_GET_STRING(v, argn, param_string);
  227. _rc_ = sqlite3_bind_text(stmt, npar, param_string, param_string_size, SQLITE_TRANSIENT);
  228. }
  229. break;
  230. case OT_USERDATA:
  231. {
  232. SQUserPointer ttag = 0;
  233. if((sq_gettypetag(v, argn, &ttag) != SQ_OK) || (ttag != sqlite3_NULL_Name))
  234. return sq_throwerror(v, "Invalid bind parameter %d", npar);
  235. }
  236. //fallthrough
  237. case OT_NULL:
  238. _rc_ = sqlite3_bind_null(stmt, npar);
  239. break;
  240. default:
  241. return sq_throwerror(v, "Invalid bind parameter %d", npar);
  242. }
  243. if(_rc_ != SQLITE_OK)
  244. {
  245. sqlite3 *db = sqlite3_db_handle(stmt);
  246. return sq_throwerror(v, sqlite3_errmsg(db));
  247. }
  248. return SQ_OK;
  249. }
  250. static SQRESULT sq_sqlite3_stmt_bind_vargv(HSQUIRRELVM v, sqlite3 *db, sqlite3_stmt *stmt, int params_start)
  251. {
  252. SQ_FUNC_VARS(v);
  253. if(_top_ > params_start)
  254. {
  255. int nparams = sqlite3_bind_parameter_count(stmt);
  256. int nparams_given = _top_-params_start;
  257. if(nparams != nparams_given)
  258. {
  259. return sq_throwerror(v, "expect %d parameters but only %d given", nparams, nparams_given);
  260. }
  261. for(int i=1; i <= nparams; ++i)
  262. {
  263. int argn = i+params_start;
  264. _rc_ = sqlite3_stmt_bind_value(v, stmt, i, argn);
  265. if(_rc_ < 0) return _rc_;
  266. }
  267. }
  268. return SQ_OK;
  269. }
  270. static SQRESULT sq_sqlite3_stmt_prepare_aux(HSQUIRRELVM v, sqlite3 *db, sqlite3_stmt **stmt, int params_start)
  271. {
  272. SQ_FUNC_VARS_NO_TOP(v);
  273. SQ_GET_STRING(v, params_start, szSQL);
  274. if(sqlite3_prepare_v2(db, szSQL, szSQL_size, stmt, 0) != SQLITE_OK)
  275. {
  276. return sq_throwerror(v, sqlite3_errmsg(db));
  277. }
  278. return sq_sqlite3_stmt_bind_vargv(v, db, *stmt, params_start);
  279. }
  280. static SQRESULT sq_sqlite3_stmt_releasehook(SQUserPointer p, SQInteger size, void */*ep*/)
  281. {
  282. sqlite3_stmt *stmt = ((sqlite3_stmt *)p);
  283. if(stmt) sqlite3_finalize(stmt);
  284. return 0;
  285. }
  286. static SQRESULT sq_sqlite3_stmt_constructor(HSQUIRRELVM v)
  287. {
  288. SQ_FUNC_VARS(v);
  289. _rc_ = SQ_ERROR;
  290. GET_sqlite3_INSTANCE_AT(2);
  291. sqlite3_stmt *stmt = 0;
  292. if(_top_ > 2)
  293. {
  294. _rc_ = sq_sqlite3_stmt_prepare_aux(v, self, &stmt, 3);
  295. }
  296. else
  297. {
  298. if(sqlite3_prepare_v2(self, "select 'statement not prepared';", -1, &stmt, 0) != SQLITE_OK)
  299. {
  300. _rc_ = sq_throwerror(v, sqlite3_errmsg(self));
  301. }
  302. else _rc_ = SQ_OK;
  303. }
  304. sq_setinstanceup(v, 1, stmt); //replace self for this instance with this new sqlite3_stmt
  305. sq_setreleasehook(v,1, sq_sqlite3_stmt_releasehook);
  306. return _rc_;
  307. }
  308. static SQRESULT sq_sqlite3_stmt_get_db(HSQUIRRELVM v)
  309. {
  310. SQ_FUNC_VARS_NO_TOP(v);
  311. GET_sqlite3_stmt_INSTANCE();
  312. sqlite3 *db = sqlite3_db_handle(self);
  313. sq_pushuserpointer(v, db);
  314. if(sq_getonregistrytable(v) == SQ_OK)
  315. {
  316. sq_getweakrefval(v, -1);
  317. }
  318. else return sq_throwerror(v, _SC("could not retrive database, maybe it was already closed"));
  319. return 1;
  320. }
  321. static SQRESULT sq_sqlite3_stmt_stmt_ptr(HSQUIRRELVM v)
  322. {
  323. SQ_FUNC_VARS_NO_TOP(v);
  324. GET_sqlite3_stmt_INSTANCE();
  325. sq_pushuserpointer(v, self);
  326. return 1;
  327. }
  328. static SQRESULT sq_sqlite3_stmt_finalize(HSQUIRRELVM v)
  329. {
  330. SQ_FUNC_VARS_NO_TOP(v);
  331. GET_sqlite3_stmt_INSTANCE();
  332. sq_setinstanceup(v, 1, 0); //next calls will fail with "statement is closed"
  333. //printf("sq_sqlite3_stmt_finalize: %p : %s\n", self, sqlite3_sql(self));
  334. sq_pushinteger(v, sqlite3_finalize(self));
  335. return 1;
  336. }
  337. static SQRESULT sq_sqlite3_stmt_prepare(HSQUIRRELVM v)
  338. {
  339. SQ_FUNC_VARS_NO_TOP(v);
  340. GET_sqlite3_stmt_INSTANCE();
  341. sqlite3 *db = sqlite3_db_handle(self);
  342. sqlite3_stmt *stmt = 0;
  343. _rc_ = sq_sqlite3_stmt_prepare_aux(v, db, &stmt, 2);
  344. if(stmt)
  345. {
  346. sqlite3_finalize(self); //finalize the previous sqlite3_stmt
  347. sq_setinstanceup(v, 1, stmt); //replace self for this instance with this new sqlite3_stmt
  348. }
  349. return _rc_;
  350. }
  351. static SQRESULT sq_sqlite3_stmt_get_sql(HSQUIRRELVM v)
  352. {
  353. SQ_FUNC_VARS_NO_TOP(v);
  354. GET_sqlite3_stmt_INSTANCE();
  355. sq_pushstring(v, sqlite3_sql(self), -1);
  356. return 1;
  357. }
  358. static SQRESULT sq_sqlite3_stmt_bind(HSQUIRRELVM v)
  359. {
  360. SQ_FUNC_VARS_NO_TOP(v);
  361. GET_sqlite3_stmt_INSTANCE();
  362. SQ_GET_INTEGER(v, 2, npar);
  363. return sqlite3_stmt_bind_value(v, self, npar, 3);
  364. }
  365. /**
  366. The number/order of fields should be the same to use this function
  367. it'll save temporary data convertion.
  368. */
  369. static SQRESULT sq_sqlite3_stmt_bind_stmt0(HSQUIRRELVM v, int doExec)
  370. {
  371. SQ_FUNC_VARS_NO_TOP(v);
  372. GET_sqlite3_stmt_INSTANCE();
  373. GET_sqlite3_stmt_INSTANCE_AT(2, stmt_src);
  374. int rc = SQLITE_OK, bind_count = sqlite3_bind_parameter_count(self);
  375. for(int i=0; i < bind_count; ++i)
  376. {
  377. rc = sqlite3_bind_value(self, i+1, sqlite3_column_value(stmt_src, i));
  378. if(rc != SQLITE_OK) break;
  379. }
  380. if(doExec && rc == SQLITE_OK)
  381. {
  382. rc = sqlite3_step(self);
  383. sqlite3_reset(self);
  384. }
  385. sq_pushinteger(v, rc);
  386. return 1;
  387. }
  388. static SQRESULT sq_sqlite3_stmt_bind_stmt(HSQUIRRELVM v)
  389. {
  390. return sq_sqlite3_stmt_bind_stmt0(v, 0);
  391. }
  392. static SQRESULT sq_sqlite3_stmt_bind_exec_stmt(HSQUIRRELVM v)
  393. {
  394. return sq_sqlite3_stmt_bind_stmt0(v, 1);
  395. }
  396. static SQRESULT sq_sqlite3_stmt_bind_empty_null(HSQUIRRELVM v)
  397. {
  398. SQ_FUNC_VARS_NO_TOP(v);
  399. GET_sqlite3_stmt_INSTANCE();
  400. SQ_GET_INTEGER(v, 2, npar);
  401. if(sq_gettype(v, 3) == OT_STRING && sq_getsize(v, 3) == 0)
  402. {
  403. sq_pushnull(v);
  404. sq_replace(v, 3);
  405. }
  406. return sqlite3_stmt_bind_value(v, self, npar, 3);
  407. }
  408. static SQRESULT sq_sqlite3_stmt_bind_blob(HSQUIRRELVM v)
  409. {
  410. SQ_FUNC_VARS_NO_TOP(v);
  411. GET_sqlite3_stmt_INSTANCE();
  412. SQ_GET_INTEGER(v, 2, npar);
  413. SQ_GET_STRING(v, 3, blob);
  414. if(sqlite3_bind_blob(self, npar, blob, blob_size, SQLITE_TRANSIENT) != SQLITE_OK)
  415. {
  416. sqlite3 *db = sqlite3_db_handle(self);
  417. return sq_throwerror(v, sqlite3_errmsg(db));
  418. }
  419. return SQ_OK;
  420. }
  421. static SQRESULT sq_sqlite3_stmt_bind_zeroblob(HSQUIRRELVM v)
  422. {
  423. SQ_FUNC_VARS_NO_TOP(v);
  424. GET_sqlite3_stmt_INSTANCE();
  425. SQ_GET_INTEGER(v, 2, npar);
  426. SQ_GET_INTEGER(v, 3, nsize);
  427. if(sqlite3_bind_zeroblob(self, npar, nsize))
  428. {
  429. sqlite3 *db = sqlite3_db_handle(self);
  430. return sq_throwerror(v, sqlite3_errmsg(db));
  431. }
  432. return SQ_OK;
  433. }
  434. static SQRESULT sq_sqlite3_stmt_bind_values0(HSQUIRRELVM v, int isExec)
  435. {
  436. SQ_FUNC_VARS(v);
  437. GET_sqlite3_stmt_INSTANCE();
  438. if (_top_ - 1 != sqlite3_bind_parameter_count(self))
  439. return sq_throwerror(v,
  440. _SC("incorrect number of parameters to bind (%d given, %d to bind)"),
  441. _top_ - 1,
  442. sqlite3_bind_parameter_count(self));
  443. for (int n = 2; n <= _top_; ++n)
  444. {
  445. if (sqlite3_stmt_bind_value(v, self, n-1, n) != SQ_OK) return SQ_ERROR;
  446. }
  447. if(isExec)
  448. {
  449. int rc = sqlite3_step(self);
  450. sqlite3_reset(self);
  451. sq_pushinteger(v, rc);
  452. return 1;
  453. }
  454. return 0;
  455. }
  456. static SQRESULT sq_sqlite3_stmt_bind_values(HSQUIRRELVM v)
  457. {
  458. return sq_sqlite3_stmt_bind_values0(v, 0);
  459. }
  460. static SQRESULT sq_sqlite3_stmt_bind_exec(HSQUIRRELVM v)
  461. {
  462. return sq_sqlite3_stmt_bind_values0(v, 1);
  463. }
  464. static SQRESULT sq_sqlite3_stmt_bind_exec_get_one(HSQUIRRELVM v)
  465. {
  466. SQ_FUNC_VARS_NO_TOP(v);
  467. GET_sqlite3_stmt_INSTANCE();
  468. SQInteger rc = sq_sqlite3_stmt_bind_values0(v, 0);
  469. if(rc == SQ_ERROR) return rc;
  470. rc = sqlite3_step(self);
  471. if(rc == SQLITE_ROW) sqlite3_stmt_push_value(v, self, 0, 0);
  472. else sq_pushnull(v);
  473. sqlite3_reset(self);
  474. return 1;
  475. }
  476. static SQRESULT sq_sqlite3_stmt_bind_exec_get_first_row(HSQUIRRELVM v)
  477. {
  478. SQ_FUNC_VARS_NO_TOP(v);
  479. GET_sqlite3_stmt_INSTANCE();
  480. SQInteger rc = sq_sqlite3_stmt_bind_values0(v, 0);
  481. if(rc == SQ_ERROR) return rc;
  482. rc = sqlite3_step(self);
  483. if(rc == SQLITE_ROW) sqlite3_stmt_row_asArray(v, self, NULL_AS_EMPTY_STR|AS_STRING_ALWAYS);
  484. else sq_pushnull(v);
  485. sqlite3_reset(self);
  486. return 1;
  487. }
  488. static SQRESULT sq_sqlite3_stmt_bind_names(HSQUIRRELVM v)
  489. {
  490. SQ_FUNC_VARS_NO_TOP(v);
  491. GET_sqlite3_stmt_INSTANCE();
  492. SQObjectType ptype = sq_gettype(v, 2);
  493. int count = sqlite3_bind_parameter_count(self);
  494. int result;
  495. for (int n = 1; n <= count; ++n)
  496. {
  497. const char *name = sqlite3_bind_parameter_name(self, n);
  498. if (name && (name[0] == ':' || name[0] == '$' || name[0] == '@'))
  499. {
  500. if(ptype == OT_TABLE)
  501. {
  502. sq_pushstring(v, ++name, -1);
  503. if(sq_get(v, 2) == SQ_OK)
  504. {
  505. result = sqlite3_stmt_bind_value(v, self, n, -1);
  506. sq_poptop(v);
  507. }
  508. else return sq_throwerror(v, _SC("bind parameter (%s) not found"), name);
  509. }
  510. else return sq_throwerror(v, _SC("table expected to bind named parameters"));
  511. }
  512. else
  513. {
  514. if(ptype == OT_ARRAY)
  515. {
  516. sq_pushinteger(v, n);
  517. if(sq_get(v, 2) == SQ_OK)
  518. {
  519. result = sqlite3_stmt_bind_value(v, self, n, -1);
  520. sq_poptop(v);
  521. }
  522. else return sq_throwerror(v, _SC("bind parameter (%d) not found"), n);
  523. }
  524. else return sq_throwerror(v, _SC("array expected to bind numbered parameters"));
  525. }
  526. if (result != SQ_OK) return result;
  527. }
  528. return 0;
  529. }
  530. static SQRESULT sq_sqlite3_stmt_bind_parameter_index(HSQUIRRELVM v)
  531. {
  532. SQ_FUNC_VARS_NO_TOP(v);
  533. GET_sqlite3_stmt_INSTANCE();
  534. SQ_GET_STRING(v, 2, spar);
  535. sq_pushinteger(v, sqlite3_bind_parameter_index(self, spar));
  536. return 1;
  537. }
  538. static SQRESULT sq_sqlite3_stmt_bind_parameter_count(HSQUIRRELVM v)
  539. {
  540. SQ_FUNC_VARS_NO_TOP(v);
  541. GET_sqlite3_stmt_INSTANCE();
  542. sq_pushinteger(v, sqlite3_bind_parameter_count(self));
  543. return 1;
  544. }
  545. static SQRESULT sq_sqlite3_stmt_reset(HSQUIRRELVM v)
  546. {
  547. SQ_FUNC_VARS_NO_TOP(v);
  548. GET_sqlite3_stmt_INSTANCE();
  549. sq_pushinteger(v, sqlite3_reset(self));
  550. return 1;
  551. }
  552. static SQRESULT sq_sqlite3_stmt_step(HSQUIRRELVM v)
  553. {
  554. SQ_FUNC_VARS_NO_TOP(v);
  555. GET_sqlite3_stmt_INSTANCE();
  556. sq_pushinteger(v, sqlite3_step(self));
  557. return 1;
  558. }
  559. static SQRESULT sq_sqlite3_stmt_next_row(HSQUIRRELVM v)
  560. {
  561. SQ_FUNC_VARS_NO_TOP(v);
  562. GET_sqlite3_stmt_INSTANCE();
  563. sq_pushbool(v, sqlite3_step(self) == SQLITE_ROW);
  564. return 1;
  565. }
  566. static SQRESULT sq_sqlite3_stmt_col_count(HSQUIRRELVM v)
  567. {
  568. SQ_FUNC_VARS_NO_TOP(v);
  569. GET_sqlite3_stmt_INSTANCE();
  570. sq_pushinteger(v, sqlite3_column_count(self));
  571. return 1;
  572. }
  573. static SQRESULT sq_sqlite3_stmt_col_name(HSQUIRRELVM v)
  574. {
  575. SQ_FUNC_VARS_NO_TOP(v);
  576. GET_sqlite3_stmt_INSTANCE();
  577. SQ_GET_INTEGER(v, 2, col);
  578. sq_pushstring(v, sqlite3_column_name(self, col), -1);
  579. return 1;
  580. }
  581. static SQRESULT sq_sqlite3_stmt_col_type(HSQUIRRELVM v)
  582. {
  583. SQ_FUNC_VARS_NO_TOP(v);
  584. GET_sqlite3_stmt_INSTANCE();
  585. SQ_GET_INTEGER(v, 2, col);
  586. sq_pushinteger(v, sqlite3_column_type(self, col));
  587. return 1;
  588. }
  589. static const char *get_column_type(sqlite3_stmt *stmt, int col)
  590. {
  591. const char *column_type;
  592. int icol_type = sqlite3_column_type(stmt, col);
  593. switch(icol_type)
  594. {
  595. case SQLITE_INTEGER: column_type = "INTEGER"; break;
  596. case SQLITE_FLOAT: column_type = "FLOAT"; break;
  597. case SQLITE_BLOB: column_type = "BLOB"; break;
  598. case SQLITE_TEXT: column_type = "TEXT"; break;
  599. case SQLITE_NULL: column_type = "NULL"; break;
  600. default:
  601. column_type = "unknown";
  602. }
  603. return column_type;
  604. }
  605. static const char *get_column_dcltype(sqlite3_stmt *stmt, int col)
  606. {
  607. const char *column_type = sqlite3_column_decltype(stmt, col);
  608. if(!column_type) column_type = get_column_type(stmt, col);
  609. return column_type;
  610. }
  611. static SQRESULT sq_sqlite3_stmt_col_declared_type(HSQUIRRELVM v)
  612. {
  613. SQ_FUNC_VARS_NO_TOP(v);
  614. GET_sqlite3_stmt_INSTANCE();
  615. SQ_GET_INTEGER(v, 2, col);
  616. sq_pushstring(v, get_column_dcltype(self, col), -1);
  617. return 1;
  618. }
  619. static SQRESULT sq_sqlite3_stmt_colsAsArray(HSQUIRRELVM v)
  620. {
  621. SQ_FUNC_VARS_NO_TOP(v);
  622. GET_sqlite3_stmt_INSTANCE();
  623. int col_count = sqlite3_column_count(self);
  624. sq_newarray(v, col_count);
  625. for(int i=0; i<col_count; ++i)
  626. {
  627. sq_pushstring(v, sqlite3_column_name(self, i), -1);
  628. sq_arrayset(v, -2, i);
  629. }
  630. return 1;
  631. }
  632. static SQRESULT sq_sqlite3_stmt_colsTypeAsArray(HSQUIRRELVM v)
  633. {
  634. SQ_FUNC_VARS_NO_TOP(v);
  635. GET_sqlite3_stmt_INSTANCE();
  636. int col_count = sqlite3_column_count(self);
  637. sq_newarray(v, col_count);
  638. for(int i=0; i<col_count; ++i)
  639. {
  640. sq_pushstring(v, get_column_type(self, i), -1);
  641. sq_arrayset(v, -2, i);
  642. }
  643. return 1;
  644. }
  645. static SQRESULT sq_sqlite3_stmt_colsDeclTypeAsArray(HSQUIRRELVM v)
  646. {
  647. SQ_FUNC_VARS_NO_TOP(v);
  648. GET_sqlite3_stmt_INSTANCE();
  649. int col_count = sqlite3_column_count(self);
  650. sq_newarray(v, col_count);
  651. for(int i=0; i<col_count; ++i)
  652. {
  653. sq_pushstring(v, get_column_dcltype(self, i), -1);
  654. sq_arrayset(v, -2, i);
  655. }
  656. return 1;
  657. }
  658. static SQRESULT sq_sqlite3_stmt_colsNameAsArray(HSQUIRRELVM v)
  659. {
  660. SQ_FUNC_VARS_NO_TOP(v);
  661. GET_sqlite3_stmt_INSTANCE();
  662. int col_count = sqlite3_column_count(self);
  663. sq_newarray(v, col_count);
  664. for(int i=0; i<col_count; ++i)
  665. {
  666. sq_pushstring(v, sqlite3_column_name(self, i), -1);
  667. sq_arrayset(v, -2, i);
  668. }
  669. return 1;
  670. }
  671. static SQRESULT sq_sqlite3_stmt_colsAsTable(HSQUIRRELVM v)
  672. {
  673. SQ_FUNC_VARS_NO_TOP(v);
  674. GET_sqlite3_stmt_INSTANCE();
  675. int col_count = sqlite3_column_count(self);
  676. sq_newtable(v);
  677. for(int i=0; i<col_count; ++i)
  678. {
  679. sq_pushstring(v, sqlite3_column_name(self, i), -1);
  680. sq_pushinteger(v, i);
  681. sq_rawset(v, -3);
  682. }
  683. return 1;
  684. }
  685. static int get_col_index(HSQUIRRELVM v, sqlite3_stmt *self)
  686. {
  687. SQInteger _rc_, col;
  688. switch(sq_gettype(v, 2))
  689. {
  690. case OT_INTEGER:
  691. sq_getinteger(v, 2, &col);
  692. break;
  693. case OT_STRING:
  694. {
  695. SQ_GET_STRING(v, 2, col_name);
  696. if(col_name_size == 0) return sq_throwerror(v, "column name can not be empty");
  697. col = -1;
  698. for(int i=0, len=sqlite3_column_count(self); i<len; ++i)
  699. {
  700. if(strcasecmp(col_name, sqlite3_column_name(self, i)) == 0)
  701. {
  702. col = i;
  703. break;
  704. }
  705. }
  706. if(col == -1) return sq_throwerror(v, "column name not found \"%s\"", col_name);
  707. }
  708. break;
  709. default:
  710. return sq_throwerror(v, "wrong parameter expected integer|string");
  711. }
  712. return col;
  713. }
  714. static SQRESULT sq_sqlite3_stmt_col(HSQUIRRELVM v)
  715. {
  716. SQ_FUNC_VARS_NO_TOP(v);
  717. GET_sqlite3_stmt_INSTANCE();
  718. int col = get_col_index(v, self);
  719. if(col < 0) return col;
  720. sqlite3_stmt_push_value(v, self, col, 0);
  721. return 1;
  722. }
  723. static SQRESULT sq_sqlite3_stmt_col_slice(HSQUIRRELVM v)
  724. {
  725. SQ_FUNC_VARS_NO_TOP(v);
  726. GET_sqlite3_stmt_INSTANCE();
  727. int col = get_col_index(v, self);
  728. if(col < 0) return col;
  729. SQ_GET_INTEGER(v, 3, start_pos);
  730. SQ_GET_INTEGER(v, 4, size);
  731. if((start_pos < 0) || (size < 0))
  732. return sq_throwerror(v, _SC("slice start_pos or size can't be negative"));
  733. const char *value = NULL;
  734. int col_type = sqlite3_column_type(self, col);
  735. switch (col_type)
  736. {
  737. case SQLITE_TEXT:
  738. value = (const char*) sqlite3_column_text(self, col);
  739. break;
  740. case SQLITE_BLOB:
  741. value = (const char*) sqlite3_column_blob(self, col);
  742. break;
  743. case SQLITE_NULL:
  744. break;
  745. default:
  746. return sq_throwerror(v, _SC("column type do not allow slice %d:%d"), col, col_type);
  747. }
  748. if(value)
  749. {
  750. int col_size = sqlite3_column_bytes(self, col);
  751. if(col_size > start_pos)
  752. {
  753. int space_left = col_size - start_pos;
  754. sq_pushstring(v, value + start_pos, (space_left > size) ? size : space_left);
  755. }
  756. else value = NULL;
  757. }
  758. if(!value) push_sqlite3_null(v);
  759. return 1;
  760. }
  761. static SQRESULT sq_sqlite3_stmt_asBool(HSQUIRRELVM v)
  762. {
  763. SQ_FUNC_VARS_NO_TOP(v);
  764. GET_sqlite3_stmt_INSTANCE();
  765. int col = get_col_index(v, self);
  766. if(col < 0) return col;
  767. const unsigned char *b = sqlite3_column_text(self, col);
  768. sq_pushbool(v, b && *b == '1');
  769. return 1;
  770. }
  771. static SQRESULT sq_sqlite3_stmt_asInteger(HSQUIRRELVM v)
  772. {
  773. SQ_FUNC_VARS_NO_TOP(v);
  774. GET_sqlite3_stmt_INSTANCE();
  775. int col = get_col_index(v, self);
  776. if(col < 0) return col;
  777. sq_pushinteger(v, sqlite3_column_int64(self, col));
  778. return 1;
  779. }
  780. static SQRESULT sq_sqlite3_stmt_asFloat(HSQUIRRELVM v)
  781. {
  782. SQ_FUNC_VARS_NO_TOP(v);
  783. GET_sqlite3_stmt_INSTANCE();
  784. int col = get_col_index(v, self);
  785. if(col < 0) return col;
  786. sq_pushfloat(v, sqlite3_column_double(self, col));
  787. return 1;
  788. }
  789. static SQRESULT sq_sqlite3_stmt_StringOrNull(HSQUIRRELVM v, int withNull)
  790. {
  791. SQ_FUNC_VARS_NO_TOP(v);
  792. GET_sqlite3_stmt_INSTANCE();
  793. int col = get_col_index(v, self);
  794. if(col < 0) return col;
  795. sqlite3_stmt_push_string(v, self, col, withNull ? 0 : NULL_AS_EMPTY_STR);
  796. return 1;
  797. }
  798. static SQRESULT sq_sqlite3_stmt_asString(HSQUIRRELVM v)
  799. {
  800. return sq_sqlite3_stmt_StringOrNull(v, 0);
  801. }
  802. static SQRESULT sq_sqlite3_stmt_asStringOrNull(HSQUIRRELVM v)
  803. {
  804. return sq_sqlite3_stmt_StringOrNull(v, 1);
  805. }
  806. static SQRESULT sq_sqlite3_stmt_asTable(HSQUIRRELVM v)
  807. {
  808. SQ_FUNC_VARS(v);
  809. GET_sqlite3_stmt_INSTANCE();
  810. SQ_OPT_INTEGER(v, 2, flags, 0);
  811. sqlite3_stmt_row_asTable(v, self, flags);
  812. return 1;
  813. }
  814. static SQRESULT sq_sqlite3_stmt_asArray(HSQUIRRELVM v)
  815. {
  816. SQ_FUNC_VARS(v);
  817. GET_sqlite3_stmt_INSTANCE();
  818. SQ_OPT_INTEGER(v, 2, flags, 0);
  819. sqlite3_stmt_row_asArray(v, self, flags);
  820. return 1;
  821. }
  822. static SQRESULT sq_sqlite3_stmt_asArrayOfArrays(HSQUIRRELVM v)
  823. {
  824. SQ_FUNC_VARS(v);
  825. GET_sqlite3_stmt_INSTANCE();
  826. SQ_OPT_INTEGER(v, 2, flags, 0);
  827. sqlite3_stmt_asArrayOfArrays(v, self, flags);
  828. sqlite3_reset(self);
  829. return 1;
  830. }
  831. static SQRESULT sq_sqlite3_stmt_asArrayOfTables(HSQUIRRELVM v)
  832. {
  833. SQ_FUNC_VARS(v);
  834. GET_sqlite3_stmt_INSTANCE();
  835. SQ_OPT_INTEGER(v, 2, flags, 0);
  836. sqlite3_stmt_asArrayOfTables(v, self, flags);
  837. sqlite3_reset(self);
  838. return 1;
  839. }
  840. /*utility functions*/
  841. static void append_escaping_json(SQBlob &json, const char *sz)
  842. {
  843. if(sz) //escape string
  844. {
  845. int last_idx, idx;
  846. idx = last_idx = 0;
  847. for(; sz[idx]; ++idx)
  848. {
  849. char c = sz[idx];
  850. switch(c)
  851. {
  852. case '"':
  853. case '\n':
  854. case '\r':
  855. case '\t':
  856. case '\b':
  857. case '\f':
  858. case '\\':
  859. {
  860. json.Write(sz+last_idx, idx-last_idx);
  861. last_idx = idx;
  862. if(c == '\n')
  863. {
  864. json.Write("\\n", 2);
  865. ++last_idx;
  866. }
  867. else if(c == '\t')
  868. {
  869. json.Write("\\t", 2);
  870. ++last_idx;
  871. }
  872. else if(c == '\b')
  873. {
  874. json.Write("\\b", 2);
  875. ++last_idx;
  876. }
  877. else if(c == '\f')
  878. {
  879. json.Write("\\f", 2);
  880. ++last_idx;
  881. }
  882. else if(c == '\r')
  883. {
  884. json.Write("\\r", 2);
  885. ++last_idx;
  886. }
  887. else
  888. {
  889. json.Write("\\", 1);
  890. }
  891. }
  892. }
  893. }
  894. //last part
  895. json.Write(sz+last_idx, idx-last_idx);
  896. }
  897. }
  898. static SQRESULT sq_sqlite3_stmt_asJsonArray(HSQUIRRELVM v)
  899. {
  900. SQ_FUNC_VARS(v);
  901. GET_sqlite3_stmt_INSTANCE();
  902. SQ_OPT_BOOL(v, 2, withMetadata, 0);
  903. int col_count = sqlite3_column_count(self);
  904. int i, value_type;
  905. const char* value;
  906. SQBlob *json = NULL;
  907. int has_blob = getOptionalBlob(v, 3, &json);
  908. if(has_blob == SQ_ERROR) return SQ_ERROR;
  909. if(withMetadata)
  910. {
  911. json->WriteZstr("{\n\"columns\":[\n");
  912. for(i=0; i < col_count; ++i)
  913. {
  914. json->WriteZstr((i == 0 ? "\"" : ",\""));
  915. const char *column_name = sqlite3_column_name(self, i);
  916. json->WriteZstr(column_name ? column_name : "unknown");
  917. json->WriteZstr("\"");
  918. }
  919. json->WriteZstr("\n],\n\"column_types\":[\n");
  920. for(i=0; i < col_count; ++i)
  921. {
  922. json->WriteZstr((i == 0 ? "\"" : ",\""));
  923. json->WriteZstr(get_column_dcltype(self, i));
  924. json->WriteZstr("\"");
  925. }
  926. json->WriteZstr("\n],\n\"rows\":[\n");
  927. }
  928. else
  929. {
  930. json->WriteZstr("[\n");
  931. }
  932. int row_count = 0;
  933. while(sqlite3_step(self) == SQLITE_ROW)
  934. {
  935. json->WriteZstr((row_count++ == 0 ? "[" : ",["));
  936. for(i=0; i < col_count; ++i)
  937. {
  938. json->WriteZstr((i == 0 ? "\"" : ",\""));
  939. //if get the type after sqlite3_column_text it always returns SQLITE_TEXT
  940. value_type = sqlite3_column_type(self, i);
  941. value = (const char*)sqlite3_column_text(self, i);
  942. if(value)
  943. {
  944. switch(value_type)
  945. {
  946. case SQLITE_BLOB:
  947. break; //no blobs in json
  948. case SQLITE_TEXT:
  949. {
  950. append_escaping_json(*json, value);
  951. }
  952. break;
  953. default:
  954. json->WriteZstr(value);
  955. }
  956. }
  957. json->WriteZstr("\"");
  958. }
  959. json->WriteZstr("]\n");
  960. }
  961. json->WriteZstr("]");
  962. if(withMetadata) json->WriteZstr("\n}");
  963. if(has_blob) return 0;
  964. sq_pushstring(v, (const SQChar*)json->GetBuf(), json->Len());
  965. if(json) sq_delete(json, SQBlob);
  966. return 1;
  967. }
  968. static SQRESULT sq_sqlite3_stmt_asJsonObject(HSQUIRRELVM v)
  969. {
  970. SQ_FUNC_VARS_NO_TOP(v);
  971. GET_sqlite3_stmt_INSTANCE();
  972. int col_count = sqlite3_column_count(self);
  973. int i, value_type, record_count = 0;
  974. const char* value;
  975. SQBlob *json = NULL;
  976. int has_blob = getOptionalBlob(v, 2, &json);
  977. if(has_blob == SQ_ERROR) return SQ_ERROR;
  978. json->WriteZstr("{");
  979. if(sqlite3_step(self) == SQLITE_ROW)
  980. {
  981. ++record_count;
  982. for(i=0; i < col_count; ++i)
  983. {
  984. json->WriteZstr((i == 0 ? "\"" : "\",\""));
  985. append_escaping_json(*json, (const char*)sqlite3_column_name(self, i));
  986. json->WriteZstr("\":\"");
  987. //if get the type after sqlite3_column_text it always returns SQLITE_TEXT
  988. value_type = sqlite3_column_type(self, i);
  989. value = (const char*)sqlite3_column_text(self, i);
  990. if(value)
  991. {
  992. switch(value_type)
  993. {
  994. case SQLITE_BLOB:
  995. break; //no blobs in json
  996. case SQLITE_TEXT:
  997. {
  998. append_escaping_json(*json, value);
  999. }
  1000. break;
  1001. default:
  1002. json->WriteZstr(value);
  1003. }
  1004. }
  1005. }
  1006. json->WriteZstr("\"");
  1007. }
  1008. json->WriteZstr("}");
  1009. if(has_blob) return 0;
  1010. if(record_count) sq_pushstring(v, (const SQChar*)json->GetBuf(), json->Len());
  1011. else sq_pushnull(v);
  1012. if(json) sq_delete(json, SQBlob);
  1013. return 1;
  1014. }
  1015. #define UNSIGNED(n) n##U
  1016. #define IBYTE_MAX UNSIGNED(256)
  1017. #define IBYTE1 UNSIGNED(240)
  1018. #define IBYTE2 UNSIGNED(2287)
  1019. #define IBYTE3 UNSIGNED(67823)
  1020. #define IBYTE4 UNSIGNED(16777215)
  1021. #define SLEMARK 254
  1022. #define SLEEND 255
  1023. static SQRESULT sle2array(HSQUIRRELVM v, const unsigned char *p, size_t sle_size, const unsigned char **next)
  1024. {
  1025. size_t size, data_len = 0;
  1026. const unsigned char *p_end = p + sle_size;
  1027. if(sle_size == 0)
  1028. {
  1029. *next = 0;
  1030. return 0;
  1031. }
  1032. while((*p != SLEEND) && (p <= p_end)) //data finished now follow digest
  1033. {
  1034. size = *p++;
  1035. if(size > IBYTE1)
  1036. {
  1037. if(size <= 248)
  1038. {
  1039. size = (size-241)*IBYTE_MAX + (*p++) + IBYTE1;
  1040. }
  1041. else if(size == 249)
  1042. {
  1043. const unsigned char *p1 = p++, *p2 = p++;
  1044. size = 2288 + (IBYTE_MAX * (*p1)) + (*p2);
  1045. }
  1046. else if(size == 250)
  1047. {
  1048. const unsigned char *p1 = p++, *p2 = p++, *p3 = p++;
  1049. size = ((*p1)<<16) + ((*p2)<<8) + (*p3);
  1050. }
  1051. else if(size == 251)
  1052. {
  1053. const unsigned char *p1 = p++, *p2 = p++, *p3 = p++, *p4 = p++;
  1054. size = ((*p1)<<24) + ((*p2)<<16) + ((*p3)<<8) + (*p4);
  1055. }
  1056. else if(size == SLEMARK)
  1057. {
  1058. //reserved can be used for multi part data, metadata, digest, ...
  1059. break;
  1060. }
  1061. }
  1062. sq_pushstring(v, (const char*)p, size);
  1063. sq_arrayappend(v, -2);
  1064. p += size;
  1065. data_len += size;
  1066. if(data_len > sle_size)
  1067. {
  1068. //something went wrong here
  1069. return sq_throwerror(v, "We got more data than expected !");
  1070. }
  1071. }
  1072. *next = ++p;
  1073. return 0;
  1074. }
  1075. static SQRESULT sle2vec(HSQUIRRELVM v, const unsigned char *sle,
  1076. size_t sle_size,
  1077. const unsigned char **next)
  1078. {
  1079. int rc;
  1080. sq_newarray(v, 0);
  1081. if(sle[0] != '[')
  1082. {
  1083. return sq_throwerror(v, "Invalid encoded vector !");
  1084. }
  1085. if(sle[1] == ']')
  1086. {
  1087. *next = sle+2; //empty array
  1088. return 0;
  1089. }
  1090. if((rc = sle2array(v, sle+1, sle_size-1, next)) < 0) return rc;
  1091. if(!*next || *next[0] != ']')
  1092. {
  1093. return sq_throwerror(v, "Invalid end of vector !");
  1094. }
  1095. (*next)++;
  1096. return 0;
  1097. }
  1098. static SQRESULT sq_sle2vecOfvec(HSQUIRRELVM v)
  1099. {
  1100. SQ_FUNC_VARS(v);
  1101. SQBlob *blob = NULL;
  1102. const SQChar *sle, *sle_start;
  1103. SQInteger sle_size;
  1104. SQObjectType ptype = sq_gettype(v, 2);
  1105. switch(ptype)
  1106. {
  1107. case OT_STRING:
  1108. sq_getstr_and_size(v, 2, &sle, &sle_size);
  1109. break;
  1110. case OT_INSTANCE:
  1111. if(SQ_FAILED(sq_getinstanceup(v,2,(SQUserPointer*)&blob,(SQUserPointer)SQBlob::SQBlob_TAG)))
  1112. return sq_throwerror(v,_SC("invalid type tag"));
  1113. if(!blob || !blob->IsValid())
  1114. return sq_throwerror(v,_SC("the blob is invalid"));
  1115. sle = (const SQChar*)blob->GetBuf();
  1116. sle_size = blob->Len();
  1117. break;
  1118. default:
  1119. return sq_throwerror(v,_SC("invalid parameter 1 of type %s"), sq_gettypename(v, 2));
  1120. }
  1121. if(sq_gettype(v, 3) != OT_ARRAY)
  1122. return sq_throwerror(v,_SC("invalid parameter 2 of type %s"), sq_gettypename(v, 3));
  1123. SQ_OPT_INTEGER(v, 4, start, 0);
  1124. if(start < 0) return sq_throwerror(v, _SC("invalid start position (%d)"), start);
  1125. sq_push(v, 3);
  1126. int rc, data_count = 1;
  1127. const unsigned char *next;
  1128. sle_start = sle + start;
  1129. sle_size -= start;
  1130. if(sle_start[0] != '[')
  1131. {
  1132. return sq_throwerror(v, "Invalid encoded vector !");
  1133. }
  1134. if(sle_start[1] == ']')
  1135. {
  1136. //sq_pushinteger(v, start+2);
  1137. return 1; //empty array
  1138. }
  1139. int saved_top = sq_gettop(v);
  1140. if((rc=sle2vec(v, (const unsigned char *)sle_start+1, sle_size-1, &next)) < 0) return rc;
  1141. while(next && *next == '[')
  1142. {
  1143. sq_arrayappend(v, -2);
  1144. data_count++;
  1145. if((rc=sle2vec(v, next, sle_size - (next - (const unsigned char *)sle_start), &next)) < 0) return rc;
  1146. }
  1147. if(sq_gettop(v) != saved_top)
  1148. {
  1149. //last added table left
  1150. sq_arrayappend(v, -2);
  1151. data_count++;
  1152. }
  1153. if(!next || *next != ']')
  1154. {
  1155. return sq_throwerror(v, "Invalid end of vector !");
  1156. }
  1157. sq_pushinteger(v, ((const SQChar*)(++next)) - sle);
  1158. return 1;
  1159. }
  1160. typedef unsigned char slebuf_t[8];
  1161. static int sleSize2buf(slebuf_t sle, size_t size)
  1162. {
  1163. size_t next_size;
  1164. if(size <= IBYTE1)
  1165. {
  1166. sle[0] = size;
  1167. return 1;
  1168. }
  1169. else if(size <= IBYTE2)
  1170. {
  1171. next_size = size - IBYTE1;
  1172. sle[0] = next_size/IBYTE_MAX + 241;
  1173. sle[1] = next_size%IBYTE_MAX;
  1174. return 2;
  1175. }
  1176. else if(size <= IBYTE3)
  1177. {
  1178. sle[0] = 249;
  1179. next_size = size - 2288;
  1180. sle[1] = next_size/IBYTE_MAX;
  1181. sle[2] = next_size%IBYTE_MAX;
  1182. return 3;
  1183. }
  1184. else if(size <= IBYTE4)
  1185. {
  1186. sle[0] = 250;
  1187. sle[1] = size >> 16;
  1188. sle[2] = size >> 8;
  1189. sle[3] = size;
  1190. return 4;
  1191. }
  1192. else
  1193. {
  1194. sle[0] = 251;
  1195. sle[1] = size >> 24;
  1196. sle[2] = size >> 16;
  1197. sle[3] = size >> 8;
  1198. sle[4] = size;
  1199. return 5;
  1200. }
  1201. return 0;
  1202. }
  1203. static SQRESULT sq_get_sle_size(HSQUIRRELVM v)
  1204. {
  1205. SQ_FUNC_VARS_NO_TOP(v);
  1206. slebuf_t slebuf;
  1207. SQ_GET_INTEGER(v, 2, size);
  1208. int slesize = sleSize2buf(slebuf, size);
  1209. if(slesize)
  1210. {
  1211. sq_pushstring(v, (const char *)slebuf, slesize);
  1212. return 1;
  1213. }
  1214. return 0;
  1215. }
  1216. static int add2sle(SQBlob &sle, const char *str, size_t size)
  1217. {
  1218. slebuf_t slebuf;
  1219. int slesize = sleSize2buf(slebuf, size);
  1220. if(slesize) sle.Write((const char *)slebuf, slesize);
  1221. if(size) sle.Write(str, size);
  1222. return size;
  1223. }
  1224. static SQRESULT sq_add2sle(HSQUIRRELVM v)
  1225. {
  1226. SQ_FUNC_VARS(v);
  1227. SQ_GET_INSTANCE_VAR(v, 2, SQBlob, blob, SQBlob::SQBlob_TAG);
  1228. SQ_GET_STRING(v, 3, str);
  1229. SQ_OPT_INTEGER(v, 4, size, str_size);
  1230. if(size < 0) return sq_throwerror(v, _SC("invalid size value (%d)"), size);
  1231. sq_pushinteger(v, add2sle(*blob, str, size));
  1232. return 1;
  1233. }
  1234. static SQRESULT sq_sqlite3_stmt_asSleArray(HSQUIRRELVM v)
  1235. {
  1236. SQ_FUNC_VARS_NO_TOP(v);
  1237. GET_sqlite3_stmt_INSTANCE();
  1238. int col_count = sqlite3_column_count(self);
  1239. int i;
  1240. const char *value;
  1241. SQBlob *sle = NULL;
  1242. int has_blob = getOptionalBlob(v, 2, &sle);
  1243. if(has_blob == SQ_ERROR) return SQ_ERROR;
  1244. sle->WriteZstr("[[");
  1245. for(i=0; i < col_count; ++i)
  1246. {
  1247. value = sqlite3_column_name(self, i);
  1248. add2sle(*sle, value, strlen(value));
  1249. }
  1250. sle->WriteChar(SLEEND);
  1251. sle->WriteChar(']');
  1252. while(sqlite3_step(self) == SQLITE_ROW)
  1253. {
  1254. sle->WriteChar('[');
  1255. for(i=0; i < col_count; ++i)
  1256. {
  1257. switch(sqlite3_column_type(self, i))
  1258. {
  1259. case SQLITE_BLOB:
  1260. {
  1261. add2sle(*sle, (const char*)sqlite3_column_blob(self, i),
  1262. sqlite3_column_bytes(self, i));
  1263. }
  1264. break;
  1265. default:
  1266. add2sle(*sle, (const char*)sqlite3_column_text(self, i),
  1267. sqlite3_column_bytes(self, i));
  1268. }
  1269. }
  1270. sle->WriteChar(SLEEND);
  1271. sle->WriteChar(']');
  1272. }
  1273. sle->WriteChar(']');
  1274. if(has_blob) return 0;
  1275. sq_pushstring(v, (const SQChar*)sle->GetBuf(), sle->Len());
  1276. if(sle) sq_delete(sle, SQBlob);
  1277. return 1;
  1278. }
  1279. static SQRESULT sq_sqlite3_stmt_all_blobs_size(HSQUIRRELVM v)
  1280. {
  1281. SQ_FUNC_VARS_NO_TOP(v);
  1282. GET_sqlite3_stmt_INSTANCE();
  1283. int total_size = 0, col_count = sqlite3_column_count(self);
  1284. for(int i=0; i < col_count; ++i)
  1285. {
  1286. if(sqlite3_column_type(self, i) == SQLITE_BLOB)
  1287. {
  1288. total_size += sqlite3_column_bytes(self, i);
  1289. }
  1290. }
  1291. sq_pushinteger(v, total_size);
  1292. return 1;
  1293. }
  1294. static SQRESULT sq_sqlite3_stmt_colsSizeAsArray(HSQUIRRELVM v)
  1295. {
  1296. SQ_FUNC_VARS_NO_TOP(v);
  1297. GET_sqlite3_stmt_INSTANCE();
  1298. int col_count = sqlite3_column_count(self);
  1299. sq_newarray(v, col_count);
  1300. for(int i=0; i<col_count; ++i)
  1301. {
  1302. sq_pushinteger(v, sqlite3_column_bytes(self, i));
  1303. sq_arrayset(v, -2, i);
  1304. }
  1305. return 1;
  1306. }
  1307. #define _DECL_FUNC(name,nparams,tycheck, isStatic) {_SC(#name), sq_sqlite3_stmt_##name,nparams,tycheck, isStatic}
  1308. static SQRegFunction sq_sqlite3_stmt_methods[] =
  1309. {
  1310. _DECL_FUNC(constructor, -2, _SC("xxs s|n|b|o"), SQFalse),
  1311. _DECL_FUNC(stmt_ptr, 1, _SC("x"), SQFalse),
  1312. _DECL_FUNC(get_db, 1, _SC("x"), SQFalse),
  1313. _DECL_FUNC(finalize, 1, _SC("x"), SQFalse),
  1314. _DECL_FUNC(prepare, -2, _SC("xs s|n|b|o|u"), SQFalse),
  1315. _DECL_FUNC(get_sql, 1, _SC("x"), SQFalse),
  1316. _DECL_FUNC(bind, 3, _SC("xi s|n|b|o|u"), SQFalse),
  1317. _DECL_FUNC(bind_stmt, 2, _SC("xx"), SQFalse),
  1318. _DECL_FUNC(bind_exec_stmt, 2, _SC("xx"), SQFalse),
  1319. _DECL_FUNC(bind_empty_null, 3, _SC("xi s|n|b|o|u"), SQFalse),
  1320. _DECL_FUNC(bind_blob, 3, _SC("xis|u"), SQFalse),
  1321. _DECL_FUNC(bind_zeroblob, 3, _SC("xii"), SQFalse),
  1322. _DECL_FUNC(bind_values, -2, _SC("x s|n|b|o|u"), SQFalse),
  1323. _DECL_FUNC(bind_exec, -1, _SC("x s|n|b|o|u"), SQFalse),
  1324. _DECL_FUNC(bind_exec_get_one, -1, _SC("x s|n|b|o|u"), SQFalse),
  1325. _DECL_FUNC(bind_exec_get_first_row, -1, _SC("x s|n|b|o|u"), SQFalse),
  1326. _DECL_FUNC(bind_names, 2, _SC("x t|a"), SQFalse),
  1327. _DECL_FUNC(bind_parameter_index, 2, _SC("xs"), SQFalse),
  1328. _DECL_FUNC(bind_parameter_count, 1, _SC("x"), SQFalse),
  1329. _DECL_FUNC(step, 1, _SC("x"), SQFalse),
  1330. _DECL_FUNC(reset, 1, _SC("x"), SQFalse),
  1331. _DECL_FUNC(next_row, 1, _SC("x"), SQFalse),
  1332. _DECL_FUNC(colsAsArray, 1, _SC("x"), SQFalse),
  1333. _DECL_FUNC(colsAsTable, 1, _SC("x"), SQFalse),
  1334. _DECL_FUNC(colsTypeAsArray, 1, _SC("x"), SQFalse),
  1335. _DECL_FUNC(colsDeclTypeAsArray, 1, _SC("x"), SQFalse),
  1336. _DECL_FUNC(col_count, 1, _SC("x"), SQFalse),
  1337. _DECL_FUNC(col_name, 2, _SC("xi"), SQFalse),
  1338. _DECL_FUNC(colsNameAsArray, 1, _SC("x"), SQFalse),
  1339. _DECL_FUNC(col_type, 2, _SC("xi"), SQFalse),
  1340. _DECL_FUNC(col_declared_type, 2, _SC("xi"), SQFalse),
  1341. _DECL_FUNC(asArray, -1, _SC("xi"), SQFalse),
  1342. _DECL_FUNC(asTable, -1, _SC("xi"), SQFalse),
  1343. _DECL_FUNC(asArrayOfArrays, -1, _SC("xi"), SQFalse),
  1344. _DECL_FUNC(asArrayOfTables, -1, _SC("xi"), SQFalse),
  1345. _DECL_FUNC(asSleArray, -1, _SC("xx"), SQFalse),
  1346. //todo manage/skip blobs for json
  1347. _DECL_FUNC(asJsonArray, -1, _SC("xbx"), SQFalse),
  1348. _DECL_FUNC(asJsonObject, -1, _SC("xx"), SQFalse),
  1349. _DECL_FUNC(col, 2, _SC("x i|s"), SQFalse),
  1350. _DECL_FUNC(col_slice, 4, _SC("x i|s ii"), SQFalse),
  1351. _DECL_FUNC(asString, 2, _SC("x i|s"), SQFalse),
  1352. _DECL_FUNC(asStringOrNull, 2, _SC("x i|s"), SQFalse),
  1353. _DECL_FUNC(asInteger, 2, _SC("x i|s"), SQFalse),
  1354. _DECL_FUNC(asBool, 2, _SC("x i|s"), SQFalse),
  1355. _DECL_FUNC(asFloat, 2, _SC("x i|s"), SQFalse),
  1356. _DECL_FUNC(all_blobs_size, 1, _SC("x"), SQFalse),
  1357. _DECL_FUNC(colsSizeAsArray, 1, _SC("x"), SQFalse),
  1358. {0,0}
  1359. };
  1360. #undef _DECL_FUNC
  1361. //Statement scope reset
  1362. static const SQChar *SQLite3_StmtScopeReset_TAG = "SQLite3StmtScopeReset";
  1363. static SQRESULT sq_sqlite3_stmt_scope_reset_releasehook(SQUserPointer p, SQInteger size, void */*ep*/)
  1364. {
  1365. sqlite3_stmt *stmt = ((sqlite3_stmt *)p);
  1366. if(stmt) sqlite3_reset(stmt);
  1367. return 0;
  1368. }
  1369. static SQRESULT sq_sqlite3_stmt_scope_reset_constructor(HSQUIRRELVM v)
  1370. {
  1371. SQRESULT _rc_ = SQ_ERROR;
  1372. GET_sqlite3_stmt_INSTANCE_AT(2, stmt);
  1373. sq_setinstanceup(v, 1, stmt); //replace self for this instance with this new sqlite3_stmt
  1374. sq_setreleasehook(v,1, sq_sqlite3_stmt_scope_reset_releasehook);
  1375. return 1;
  1376. }
  1377. #define _DECL_FUNC(name,nparams,tycheck, isStatic) {_SC(#name), sq_sqlite3_stmt_scope_reset_##name,nparams,tycheck, isStatic}
  1378. static SQRegFunction sq_sqlite3_stmt_scope_reset_methods[] =
  1379. {
  1380. _DECL_FUNC(constructor, 2, _SC("xx"), SQFalse),
  1381. {0,0}
  1382. };
  1383. #undef _DECL_FUNC
  1384. //Transaction Class
  1385. static const SQChar *SQLite3_Transaction_TAG = "SQLite3Transaction";
  1386. static SQRESULT get_sqlite3_transaction_instance(HSQUIRRELVM v, SQInteger idx, sq_sqlite3_sdb **sdb)
  1387. {
  1388. SQRESULT _rc_;
  1389. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)sdb,(void*)SQLite3_Transaction_TAG)) < 0) return _rc_;
  1390. if(!*sdb) return sq_throwerror(v, _SC("transaction is closed"));
  1391. return _rc_;
  1392. }
  1393. #define GET_sqlite3_transaction_INSTANCE_AT(idx, var_name) \
  1394. sq_sqlite3_sdb *var_name=NULL; \
  1395. if((_rc_ = get_sqlite3_transaction_instance(v,idx,&var_name)) < 0) return _rc_;
  1396. #define GET_sqlite3_transaction_INSTANCE() GET_sqlite3_transaction_INSTANCE_AT(1, self)
  1397. enum db_transaction_op {e_transaction_begin = 1, e_transaction_commit, e_transaction_rollback};
  1398. static SQRESULT sq_sqlite3_transaction_exec_sq(sq_sqlite3_sdb *sdb, const char *sql)
  1399. {
  1400. char *errmsg;
  1401. if(sqlite3_exec(sdb->db, sql, NULL, NULL, &errmsg) != SQLITE_OK){
  1402. sq_throwerror(sdb->v, errmsg);
  1403. sqlite3_free(errmsg);
  1404. return SQ_ERROR;
  1405. }
  1406. return SQ_OK;
  1407. }
  1408. static SQRESULT sq_sqlite3_transaction_exec(sq_sqlite3_sdb *sdb, db_transaction_op op)
  1409. {
  1410. if(sdb)
  1411. {
  1412. SQRESULT rc;
  1413. //printf("Now doing %d : %d\n", op, sdb->nested_transaction_count);
  1414. switch(op)
  1415. {
  1416. case e_transaction_begin:
  1417. if(sdb->nested_transaction_count++ == 0)
  1418. {
  1419. return sq_sqlite3_transaction_exec_sq(sdb, "begin");
  1420. }
  1421. return SQ_OK;
  1422. break;
  1423. case e_transaction_commit:
  1424. if(sdb->nested_transaction_count > 0)
  1425. {
  1426. rc = sq_sqlite3_transaction_exec_sq(sdb, "commit");
  1427. if(rc == SQ_OK) --sdb->nested_transaction_count;
  1428. return rc;
  1429. }
  1430. break;
  1431. case e_transaction_rollback:
  1432. if(sdb->nested_transaction_count > 0)
  1433. {
  1434. rc = sq_sqlite3_transaction_exec_sq(sdb, "rollback");
  1435. if(rc == SQ_OK) sdb->nested_transaction_count = 0;
  1436. return rc;
  1437. }
  1438. break;
  1439. }
  1440. return sq_throwerror(sdb->v, _SC("no transaction active"));
  1441. }
  1442. return SQ_ERROR;
  1443. }
  1444. static SQRESULT sq_sqlite3_transaction_releasehook(SQUserPointer p, SQInteger size, void */*ep*/)
  1445. {
  1446. sq_sqlite3_sdb *sdb = ((sq_sqlite3_sdb *)p);
  1447. return sq_sqlite3_transaction_exec(sdb, e_transaction_rollback);
  1448. }
  1449. static SQRESULT sq_sqlite3_transaction_constructor(HSQUIRRELVM v)
  1450. {
  1451. SQRESULT _rc_ = SQ_ERROR;
  1452. GET_sqlite3_INSTANCE_AT(2);
  1453. _rc_ = sq_sqlite3_transaction_exec(sdb, e_transaction_begin);
  1454. if(_rc_ != SQ_OK) return SQ_ERROR;
  1455. sq_setinstanceup(v, 1, sdb); //replace self for this instance with this new sqlite3_stmt
  1456. sq_setreleasehook(v,1, sq_sqlite3_transaction_releasehook);
  1457. return 1;
  1458. }
  1459. static SQRESULT sq_sqlite3_transaction_commit(HSQUIRRELVM v)
  1460. {
  1461. SQ_FUNC_VARS_NO_TOP(v);
  1462. GET_sqlite3_transaction_INSTANCE();
  1463. _rc_ = sq_sqlite3_transaction_exec(self, e_transaction_commit);
  1464. sq_setinstanceup(v, 1, NULL); //avoid reuse it
  1465. return _rc_;
  1466. }
  1467. static SQRESULT sq_sqlite3_transaction_rollback(HSQUIRRELVM v)
  1468. {
  1469. SQ_FUNC_VARS_NO_TOP(v);
  1470. GET_sqlite3_transaction_INSTANCE();
  1471. _rc_ = sq_sqlite3_transaction_exec(self, e_transaction_rollback);
  1472. sq_setinstanceup(v, 1, NULL); //avoid reuse it
  1473. return _rc_;
  1474. }
  1475. #define _DECL_FUNC(name,nparams,tycheck, isStatic) {_SC(#name), sq_sqlite3_transaction_##name,nparams,tycheck, isStatic}
  1476. static SQRegFunction sq_sqlite3_transaction_methods[] =
  1477. {
  1478. _DECL_FUNC(constructor, 2, _SC("xx"), SQFalse),
  1479. _DECL_FUNC(commit, 1, _SC("x"), SQFalse),
  1480. _DECL_FUNC(rollback, 1, _SC("x"), SQFalse),
  1481. {0,0}
  1482. };
  1483. #undef _DECL_FUNC
  1484. #ifdef SQLITE_ENABLE_SESSION
  1485. static const SQChar *SQLite3_Session_TAG = "SQLite3Session";
  1486. static SQRESULT get_sqlite3_session_instance(HSQUIRRELVM v, SQInteger idx, sqlite3_session **session)
  1487. {
  1488. SQRESULT _rc_;
  1489. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)session,(void*)SQLite3_Session_TAG)) < 0) return _rc_;
  1490. if(!*session) return sq_throwerror(v, _SC("session is closed"));
  1491. return _rc_;
  1492. }
  1493. #define GET_sqlite3_session_INSTANCE_AT(idx, var_name) \
  1494. sqlite3_session *var_name=NULL; \
  1495. if((_rc_ = get_sqlite3_session_instance(v,idx,&var_name)) < 0) return _rc_;
  1496. #define GET_sqlite3_session_INSTANCE() GET_sqlite3_session_INSTANCE_AT(1, self)
  1497. static SQRESULT sq_sqlite3_session_releasehook(SQUserPointer p, SQInteger size, void */*ep*/)
  1498. {
  1499. sqlite3_session *session = ((sqlite3_session *)p);
  1500. if(session) sqlite3session_delete(session);
  1501. return 0;
  1502. }
  1503. static SQRESULT sq_sqlite3_session_constructor(HSQUIRRELVM v)
  1504. {
  1505. SQ_FUNC_VARS(v);
  1506. _rc_ = SQ_ERROR;
  1507. GET_sqlite3_INSTANCE_AT(2);
  1508. SQ_OPT_STRING(v, 3, dbname, _SC("main"))
  1509. sqlite3_session *session = 0;
  1510. if(sqlite3session_create(self, dbname, &session) != SQLITE_OK)
  1511. {
  1512. _rc_ = sq_throwerror(v, sqlite3_errmsg(self));
  1513. }
  1514. else _rc_ = SQ_OK;
  1515. sq_setinstanceup(v, 1, session); //replace self for this instance with this new sqlite3_stmt
  1516. sq_setreleasehook(v,1, sq_sqlite3_session_releasehook);
  1517. return _rc_;
  1518. }
  1519. static SQRESULT sq_sqlite3_session_attach(HSQUIRRELVM v)
  1520. {
  1521. SQ_FUNC_VARS(v);
  1522. GET_sqlite3_session_INSTANCE();
  1523. SQ_OPT_STRING(v, 2, tables, NULL);
  1524. sq_pushinteger(v, sqlite3session_attach(self, tables));
  1525. return 1;
  1526. }
  1527. static SQRESULT sq_sqlite3_session_enable(HSQUIRRELVM v)
  1528. {
  1529. SQ_FUNC_VARS_NO_TOP(v);
  1530. GET_sqlite3_session_INSTANCE();
  1531. SQ_GET_BOOL(v, 2, enable);
  1532. sq_pushinteger(v, sqlite3session_enable(self, enable));
  1533. return 1;
  1534. }
  1535. static SQRESULT sq_sqlite3_session_isempty(HSQUIRRELVM v)
  1536. {
  1537. SQ_FUNC_VARS_NO_TOP(v);
  1538. GET_sqlite3_session_INSTANCE();
  1539. sq_pushinteger(v, sqlite3session_isempty(self));
  1540. return 1;
  1541. }
  1542. static SQRESULT sq_sqlite3_session_xdelete(HSQUIRRELVM v)
  1543. {
  1544. SQ_FUNC_VARS_NO_TOP(v);
  1545. GET_sqlite3_session_INSTANCE();
  1546. sqlite3session_delete(self);
  1547. sq_setinstanceup(v, 1, 0); //next calls will fail with "database is closed"
  1548. return 0;
  1549. }
  1550. static SQRESULT sq_sqlite3_session_changeset(HSQUIRRELVM v)
  1551. {
  1552. SQ_FUNC_VARS_NO_TOP(v);
  1553. GET_sqlite3_session_INSTANCE();
  1554. int nChangeset; /* OUT: Size of changeset blob in bytes */
  1555. void *pChangeset; /* OUT: Pointer to changeset blob */
  1556. if(sqlite3session_changeset(self, &nChangeset, &pChangeset) != SQ_OK) return SQ_ERROR;
  1557. sq_pushstring(v, (SQChar*)pChangeset, nChangeset);
  1558. sqlite3_free(pChangeset);
  1559. return 1;
  1560. }
  1561. static SQRESULT sq_sqlite3_session_invert(HSQUIRRELVM v)
  1562. {
  1563. SQ_FUNC_VARS_NO_TOP(v);
  1564. GET_sqlite3_session_INSTANCE();
  1565. SQ_GET_STRING(v, 2, changeset);
  1566. int nOutChangeset; /* OUT: Size of changeset blob in bytes */
  1567. void *pOutChangeset; /* OUT: Pointer to changeset blob */
  1568. if(sqlite3changeset_invert(changeset_size, changeset, &nOutChangeset, &pOutChangeset) != SQ_OK) return SQ_ERROR;
  1569. sq_pushstring(v, (SQChar*)pOutChangeset, nOutChangeset);
  1570. sqlite3_free(pOutChangeset);
  1571. return 1;
  1572. }
  1573. static SQRESULT sq_sqlite3_session_concat(HSQUIRRELVM v)
  1574. {
  1575. SQ_FUNC_VARS_NO_TOP(v);
  1576. GET_sqlite3_session_INSTANCE();
  1577. SQ_GET_STRING(v, 2, changeset1);
  1578. SQ_GET_STRING(v, 3, changeset2);
  1579. int nOutChangeset; /* OUT: Size of changeset blob in bytes */
  1580. void *pOutChangeset; /* OUT: Pointer to changeset blob */
  1581. if(sqlite3changeset_concat(changeset1_size, changeset1, changeset2_size, changeset2, &nOutChangeset, &pOutChangeset) != SQ_OK) return SQ_ERROR;
  1582. sq_pushstring(v, (SQChar*)pOutChangeset, nOutChangeset);
  1583. sqlite3_free(pOutChangeset);
  1584. return 1;
  1585. }
  1586. static SQRESULT sq_sqlite3_session_diff(HSQUIRRELVM v)
  1587. {
  1588. SQ_FUNC_VARS_NO_TOP(v);
  1589. GET_sqlite3_session_INSTANCE();
  1590. SQ_GET_STRING(v, 2, fromDB);
  1591. SQ_GET_STRING(v, 3, tbl);
  1592. sq_pushinteger(v, sqlite3session_diff(self, fromDB, tbl, NULL));
  1593. return 1;
  1594. }
  1595. static int xConflict(void *pCtx, int eConflict, sqlite3_changeset_iter *pIter){
  1596. int ret = (int)(ptrdiff_t)pCtx;
  1597. return ret;
  1598. }
  1599. static SQRESULT sq_sqlite3_session_apply(HSQUIRRELVM v)
  1600. {
  1601. SQ_FUNC_VARS_NO_TOP(v);
  1602. GET_sqlite3_session_INSTANCE();
  1603. GET_sqlite3_INSTANCE_AT_VAR(2, db);
  1604. SQ_GET_STRING(v, 3, changeset);
  1605. if(sqlite3changeset_apply(db, changeset_size, (void*)changeset, 0, xConflict, (void*)0) != SQ_OK) return SQ_ERROR;
  1606. return 0;
  1607. }
  1608. #define _DECL_FUNC(name,nparams,tycheck, isStatic) {_SC(#name), sq_sqlite3_session_##name,nparams,tycheck, isStatic}
  1609. static SQRegFunction sq_sqlite3_session_methods[] =
  1610. {
  1611. _DECL_FUNC(constructor, -2, _SC("xxs"), SQFalse),
  1612. _DECL_FUNC(attach, -1, _SC("xs"), SQFalse),
  1613. _DECL_FUNC(enable, 2, _SC("xb"), SQFalse),
  1614. _DECL_FUNC(isempty, 1, _SC("x"), SQFalse),
  1615. _DECL_FUNC(invert, 2, _SC("xs"), SQFalse),
  1616. _DECL_FUNC(concat, 3, _SC("xss"), SQFalse),
  1617. _DECL_FUNC(diff, 3, _SC("xss"), SQFalse),
  1618. _DECL_FUNC(changeset, 1, _SC("x"), SQFalse),
  1619. _DECL_FUNC(xdelete, 1, _SC("x"), SQFalse),
  1620. _DECL_FUNC(apply, 3, _SC("xxs"), SQFalse),
  1621. {0,0}
  1622. };
  1623. #undef _DECL_FUNC
  1624. static const SQChar *SQLite3_Session_Iterator_TAG = "SQLite3SessionIterator";
  1625. static SQRESULT get_sqlite3_session_iterator_instance(HSQUIRRELVM v, SQInteger idx, sqlite3_changeset_iter **ppIter)
  1626. {
  1627. SQRESULT _rc_;
  1628. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)ppIter,(void*)SQLite3_Session_Iterator_TAG)) < 0) return _rc_;
  1629. if(!*ppIter) return sq_throwerror(v, _SC("changeset iterator is closed"));
  1630. return _rc_;
  1631. }
  1632. #define GET_sqlite3_session_iterator_INSTANCE_AT(idx, var_name) \
  1633. sqlite3_changeset_iter *var_name=NULL; \
  1634. if((_rc_ = get_sqlite3_session_iterator_instance(v,idx,&var_name)) < 0) return _rc_;
  1635. #define GET_sqlite3_session_iterator_INSTANCE() GET_sqlite3_session_iterator_INSTANCE_AT(1, self)
  1636. static SQRESULT sq_sqlite3_session_iterator_releasehook(SQUserPointer p, SQInteger size, void */*ep*/)
  1637. {
  1638. sqlite3_changeset_iter *pIter = ((sqlite3_changeset_iter *)p);
  1639. if(pIter) sqlite3changeset_finalize(pIter);
  1640. return 0;
  1641. }
  1642. static SQRESULT sq_sqlite3_session_iterator_constructor(HSQUIRRELVM v)
  1643. {
  1644. SQ_FUNC_VARS_NO_TOP(v);
  1645. _rc_ = SQ_ERROR;
  1646. SQ_GET_STRING(v, 2, changeset)
  1647. sqlite3_changeset_iter *pIter = 0;
  1648. if(sqlite3changeset_start(&pIter, changeset_size, changeset) != SQLITE_OK)
  1649. {
  1650. _rc_ = sq_throwerror(v, _SC("sqlite3_changeset_iter error %d"), _rc_);
  1651. }
  1652. else _rc_ = SQ_OK;
  1653. sq_setinstanceup(v, 1, pIter); //replace self for this instance with this new sqlite3_stmt
  1654. sq_setreleasehook(v,1, sq_sqlite3_session_iterator_releasehook);
  1655. return _rc_;
  1656. }
  1657. static SQRESULT sq_sqlite3_session_iterator_next(HSQUIRRELVM v)
  1658. {
  1659. SQ_FUNC_VARS_NO_TOP(v);
  1660. GET_sqlite3_session_iterator_INSTANCE();
  1661. sq_pushinteger(v, sqlite3changeset_next(self));
  1662. return 1;
  1663. }
  1664. static SQRESULT sq_sqlite3_session_iterator_op(HSQUIRRELVM v)
  1665. {
  1666. SQ_FUNC_VARS_NO_TOP(v);
  1667. GET_sqlite3_session_iterator_INSTANCE();
  1668. const char *pzTab; /* OUT: Pointer to table name */
  1669. int nCol; /* OUT: Number of columns in table */
  1670. int Op; /* OUT: SQLITE_INSERT, DELETE or UPDATE */
  1671. int bIndirect; /* OUT: True for an 'indirect' change */
  1672. if(sqlite3changeset_op(self, &pzTab, &nCol, &Op, &bIndirect) == SQLITE_OK)
  1673. {
  1674. sq_pushstring(v, _SC("table"), -1);
  1675. sq_pushstring(v, pzTab, -1);
  1676. sq_rawset(v, 3);
  1677. sq_pushstring(v, _SC("ncol"), -1);
  1678. sq_pushinteger(v, nCol);
  1679. sq_rawset(v, 3);
  1680. sq_pushstring(v, _SC("op"), -1);
  1681. sq_pushinteger(v, Op);
  1682. sq_rawset(v, 3);
  1683. sq_pushstring(v, _SC("indirect"), -1);
  1684. sq_pushinteger(v, bIndirect);
  1685. sq_rawset(v, 3);
  1686. sq_pushbool(v, SQTrue);
  1687. }
  1688. sq_pushbool(v, SQFalse);
  1689. return 1;
  1690. }
  1691. static SQRESULT sq_sqlite3_session_iterator_push_value(HSQUIRRELVM v, int isNew)
  1692. {
  1693. SQ_FUNC_VARS_NO_TOP(v);
  1694. GET_sqlite3_session_iterator_INSTANCE();
  1695. SQ_GET_INTEGER(v, 2, col);
  1696. sqlite3_value *pVal;
  1697. int rc;
  1698. if(isNew) rc = sqlite3changeset_new(self, col, &pVal);
  1699. else rc = sqlite3changeset_old(self, col, &pVal);
  1700. if(rc != SQLITE_OK) return sq_throwerror(v, _SC("sqlite3changeset_(old/new) %d"), rc);
  1701. switch( sqlite3_value_type(pVal) ){
  1702. case SQLITE_FLOAT: {
  1703. sq_pushfloat(v, sqlite3_value_double(pVal));
  1704. break;
  1705. }
  1706. case SQLITE_INTEGER: {
  1707. sq_pushinteger(v, sqlite3_value_int64(pVal));
  1708. break;
  1709. }
  1710. case SQLITE_BLOB:
  1711. case SQLITE_TEXT: {
  1712. sq_pushstring(v, (const SQChar*)sqlite3_value_text(pVal), sqlite3_value_bytes(pVal));
  1713. break;
  1714. }
  1715. default: {
  1716. assert( sqlite3_value_type(pVal)==SQLITE_NULL );
  1717. sq_pushnull(v);
  1718. break;
  1719. }
  1720. }
  1721. return 1;
  1722. }
  1723. static SQRESULT sq_sqlite3_session_iterator_new(HSQUIRRELVM v)
  1724. {
  1725. return sq_sqlite3_session_iterator_push_value(v, 1);
  1726. }
  1727. static SQRESULT sq_sqlite3_session_iterator_old(HSQUIRRELVM v)
  1728. {
  1729. return sq_sqlite3_session_iterator_push_value(v, 0);
  1730. }
  1731. #define _DECL_FUNC(name,nparams,tycheck, isStatic) {_SC(#name), sq_sqlite3_session_iterator_##name,nparams,tycheck, isStatic}
  1732. static SQRegFunction sq_sqlite3_session_iterator_methods[] =
  1733. {
  1734. _DECL_FUNC(constructor, 2, _SC("xs"), SQFalse),
  1735. _DECL_FUNC(next, 1, _SC("x"), SQFalse),
  1736. _DECL_FUNC(op, 2, _SC("xt"), SQFalse),
  1737. _DECL_FUNC(new, 2, _SC("xi"), SQFalse),
  1738. _DECL_FUNC(old, 2, _SC("xi"), SQFalse),
  1739. {0,0}
  1740. };
  1741. #undef _DECL_FUNC
  1742. #endif // SQLITE_ENABLE_SESSION
  1743. static SQRESULT sqlite3_exec_fmt(HSQUIRRELVM v, e_type_result type_result, int null_as_empty_str=1)
  1744. {
  1745. SQ_FUNC_VARS_NO_TOP(v);
  1746. GET_sqlite3_INSTANCE();
  1747. //SQ_GET_STRING(v, 2, szSQL);
  1748. sqlite3_stmt *stmt = 0;
  1749. SQObjectType ptype = sq_gettype(v, 2);
  1750. switch(ptype)
  1751. {
  1752. case OT_STRING:
  1753. _rc_ = sq_sqlite3_stmt_prepare_aux(v, self, &stmt, 2);
  1754. if(_rc_ < 0)
  1755. {
  1756. if(stmt) sqlite3_finalize(stmt);
  1757. return _rc_;
  1758. }
  1759. break;
  1760. case OT_INSTANCE:
  1761. _rc_ = get_sqlite3_stmt_instance(v, 2, &stmt);
  1762. if(_rc_ < 0) return _rc_;
  1763. _rc_ = sq_sqlite3_stmt_bind_vargv(v, self, stmt, 2);
  1764. if(_rc_ < 0) return _rc_;
  1765. break;
  1766. }
  1767. int rc = sqlite3_step(stmt);
  1768. if(type_result == tr_ddml)
  1769. {
  1770. if(rc != SQLITE_DONE)
  1771. {
  1772. if(ptype == OT_STRING) sqlite3_finalize(stmt);
  1773. else sqlite3_reset(stmt);
  1774. return sq_throwerror(v, "SQL is not a DDML %d %s", rc, sqlite3_errmsg(self));
  1775. }
  1776. sq_pushinteger(v, sqlite3_changes(sqlite3_db_handle(stmt)));
  1777. }
  1778. else
  1779. {
  1780. if(rc != SQLITE_ROW)
  1781. {
  1782. if(ptype == OT_STRING) sqlite3_finalize(stmt);
  1783. else sqlite3_reset(stmt);
  1784. return sq_throwerror(v, "%d %s", rc, sqlite3_errmsg(self));
  1785. }
  1786. switch(type_result)
  1787. {
  1788. case tr_first_row_first_col:
  1789. sqlite3_stmt_push_value(v, stmt, 0, 0);
  1790. break;
  1791. case tr_first_row:
  1792. sqlite3_stmt_row_asArray(v, stmt, NULL_AS_EMPTY_STR|AS_STRING_ALWAYS);
  1793. break;
  1794. case tr_all_rows:
  1795. sqlite3_stmt_asArrayOfArrays(v, stmt, NULL_AS_EMPTY_STR|AS_STRING_ALWAYS);
  1796. break;
  1797. default:
  1798. if(ptype == OT_STRING) sqlite3_finalize(stmt);
  1799. else sqlite3_reset(stmt);
  1800. return sq_throwerror(v, "Unknown requested return type %d", type_result);
  1801. }
  1802. }
  1803. if(ptype == OT_STRING) sqlite3_finalize(stmt);
  1804. else sqlite3_reset(stmt);
  1805. return 1;
  1806. }
  1807. //lua_regex
  1808. #define FUNC_IS_REGEXP 1
  1809. #define FUNC_IS_FIND 2
  1810. #define FUNC_IS_FIND_COUNT 3
  1811. #define FUNC_IS_MATCH 4
  1812. static inline void push_match_capture(sqlite3_context *context, int i, LuaMatchState *ms)
  1813. {
  1814. ptrdiff_t len = ms->capture[i].len;
  1815. if(len == CAP_POSITION) sqlite3_result_int(context, ms->capture[i].init - ms->src_init);
  1816. else sqlite3_result_text(context, ms->capture[0].init, ms->capture[0].len, SQLITE_TRANSIENT);
  1817. }
  1818. static void sqlite3_lua_find_base( sqlite3_context *context, int argc, sqlite3_value **argv, int funcType ) {
  1819. assert(argc == 2);
  1820. LuaMatchState ms;
  1821. int isRegexp = funcType == FUNC_IS_REGEXP;
  1822. const char *pattern = (const char *)sqlite3_value_text(argv[isRegexp ? 0 : 1]);
  1823. const char *subject = (const char *)sqlite3_value_text(argv[isRegexp ? 1 : 0]);
  1824. assert(argc == 2);
  1825. if(!subject || !pattern){
  1826. sqlite3_result_null(context);
  1827. return;
  1828. }
  1829. size_t pattern_len = strlen(pattern);
  1830. size_t subject_len = strlen(subject);
  1831. switch(funcType)
  1832. {
  1833. case FUNC_IS_MATCH:
  1834. {
  1835. ptrdiff_t rc = lua_str_match(&ms, subject, subject_len, pattern, pattern_len, 0, 0, 0, 0);
  1836. if(ms.error) sqlite3_result_error(context, ms.error, strlen(ms.error));
  1837. else if(rc < 0) sqlite3_result_null(context);
  1838. else if(ms.level){
  1839. if(ms.level == 1) push_match_capture(context, 0, &ms);
  1840. else {
  1841. //no way to differentiate more than one match
  1842. push_match_capture(context, 0, &ms);
  1843. }
  1844. } else {
  1845. sqlite3_result_text(context, subject + ms.start_pos, ms.end_pos-ms.start_pos, SQLITE_TRANSIENT);
  1846. }
  1847. }
  1848. break;
  1849. case FUNC_IS_FIND_COUNT:
  1850. {
  1851. int count = 0;
  1852. ms.end_pos = 0;
  1853. ptrdiff_t idx = -1;
  1854. while((idx = lua_str_find(&ms, subject, subject_len, pattern, pattern_len, ms.end_pos, 0, 0, 0)) >= 0) ++count;
  1855. sqlite3_result_int(context, count);
  1856. }
  1857. break;
  1858. case FUNC_IS_FIND:
  1859. case FUNC_IS_REGEXP:
  1860. {
  1861. ptrdiff_t idx = lua_str_find(&ms, subject, subject_len, pattern, pattern_len, 0, 0, NULL, NULL);
  1862. if(idx >= 0)
  1863. {
  1864. sqlite3_result_int(context, funcType == FUNC_IS_FIND ? idx : 1);
  1865. }
  1866. else
  1867. {
  1868. if(ms.error) sqlite3_result_error(context, ms.error, strlen(ms.error));
  1869. else sqlite3_result_int(context, funcType == FUNC_IS_FIND ? idx : 0);
  1870. }
  1871. }
  1872. break;
  1873. }
  1874. }
  1875. static void sqlite3_lua_regexp( sqlite3_context *context, int argc, sqlite3_value **argv ) {
  1876. sqlite3_lua_find_base(context, argc, argv, FUNC_IS_REGEXP);
  1877. }
  1878. static void sqlite3_lua_find( sqlite3_context *context, int argc, sqlite3_value **argv ) {
  1879. sqlite3_lua_find_base(context, argc, argv, FUNC_IS_FIND);
  1880. }
  1881. static void sqlite3_lua_find_count( sqlite3_context *context, int argc, sqlite3_value **argv ) {
  1882. sqlite3_lua_find_base(context, argc, argv, FUNC_IS_FIND_COUNT);
  1883. }
  1884. static void sqlite3_lua_match( sqlite3_context *context, int argc, sqlite3_value **argv ) {
  1885. sqlite3_lua_find_base(context, argc, argv, FUNC_IS_MATCH);
  1886. }
  1887. static void sqlite3_lua_gsub( sqlite3_context *context, int argc, sqlite3_value **argv ) {
  1888. assert(argc == 4);
  1889. lua_char_buffer_st *result;
  1890. const char *error_ptr;
  1891. const char *subject = (const char *)sqlite3_value_text(argv[0]);
  1892. const char *pattern = (const char *)sqlite3_value_text(argv[1]);
  1893. const char *replacement = (const char *)sqlite3_value_text(argv[2]);
  1894. int ntimes = sqlite3_value_int(argv[3]);
  1895. if(ntimes < 0) ntimes = 0;
  1896. if(!subject || !pattern || !replacement){
  1897. sqlite3_result_null(context);
  1898. return;
  1899. }
  1900. result = lua_str_gsub(subject, strlen(subject), pattern, strlen(pattern),
  1901. replacement, strlen(replacement), ntimes, &error_ptr, NULL, NULL);
  1902. if(result){
  1903. sqlite3_result_text(context, result->buf, result->used, SQLITE_TRANSIENT);
  1904. free(result);
  1905. }
  1906. else sqlite3_result_error(context, error_ptr, strlen(error_ptr));
  1907. }
  1908. int set_sqlite3_lua_regex_func(sqlite3 *db)
  1909. {
  1910. int nErr = sqlite3_create_function(db, "lua_find", 2, SQLITE_UTF8, 0, sqlite3_lua_find, 0, 0);
  1911. nErr += sqlite3_create_function(db, "lua_find_count", 2, SQLITE_UTF8, 0, sqlite3_lua_find_count, 0, 0);
  1912. nErr += sqlite3_create_function(db, "lua_match", 2, SQLITE_UTF8, 0, sqlite3_lua_match, 0, 0);
  1913. nErr += sqlite3_create_function(db, "lua_gsub", 4, SQLITE_UTF8, 0, sqlite3_lua_gsub, 0, 0);
  1914. nErr += sqlite3_create_function(db, "lua_regexp", 2, SQLITE_UTF8, 0, sqlite3_lua_regexp, 0, 0);
  1915. return nErr ? SQLITE_ERROR : SQLITE_OK;
  1916. }
  1917. static SQRESULT sq_sqlite3_close_release(HSQUIRRELVM v, sq_sqlite3_sdb *sdb)
  1918. {
  1919. SQRESULT rc = SQ_ERROR;
  1920. //printf("sq_sqlite3_close_release:%p\n", sdb);
  1921. if(sdb && sdb->db)
  1922. {
  1923. sqlite3 *db = sdb->db;
  1924. sdb->db = NULL; //prevent double entry/free, see sq_pushregistrytable(sdb->v) bellow
  1925. /*
  1926. sqlite3_stmt* statement = NULL;
  1927. int count = 0;
  1928. while ((statement = sqlite3_next_stmt(db, NULL)))
  1929. {
  1930. printf("sq_sqlite3_close_release:stmt: %p : %s\n", statement, sqlite3_sql(statement));
  1931. sqlite3_finalize(statement);
  1932. statement = c;
  1933. count++;
  1934. }
  1935. if (count) return sq_throwerror(v, _SC("closing database with %d statements not closed."), count);
  1936. */
  1937. int close_rc = sqlite3_close_v2(db) == SQLITE_OK;
  1938. if(!v || (v && close_rc))
  1939. {
  1940. rc = SQ_OK;
  1941. //remove waekref from registrytable
  1942. //printf("Closing %s\n", sqlite3_db_filename(db, "main"));
  1943. sq_delete_on_registry_table(sdb->v, db);
  1944. if(sdb->func)
  1945. {
  1946. sq_sqlite3_sdb_func *func, *func_next;
  1947. func = sdb->func;
  1948. while (func)
  1949. {
  1950. func_next = func->next;
  1951. sq_release(sdb->v, &func->fn_step);
  1952. sq_release(sdb->v, &func->fn_finalize);
  1953. sq_release(sdb->v, &func->udata);
  1954. sq_free(func, sizeof(sq_sqlite3_sdb_func));
  1955. func = func_next;
  1956. }
  1957. sdb->func = NULL;
  1958. }
  1959. /* 'free' all references */
  1960. sq_release(sdb->v, &sdb->busy_cb);
  1961. sq_release(sdb->v, &sdb->busy_udata);
  1962. sq_release(sdb->v, &sdb->progress_cb);
  1963. sq_release(sdb->v, &sdb->progress_udata);
  1964. sq_release(sdb->v, &sdb->trace_cb);
  1965. sq_release(sdb->v, &sdb->trace_udata);
  1966. sq_release(sdb->v, &sdb->update_hook_cb);
  1967. sq_release(sdb->v, &sdb->update_hook_udata);
  1968. sq_release(sdb->v, &sdb->authorizer_hook_cb);
  1969. sq_release(sdb->v, &sdb->authorizer_hook_udata);
  1970. sq_release(sdb->v, &sdb->null_value);
  1971. sq_free(sdb, sizeof(sq_sqlite3_sdb));
  1972. }
  1973. else
  1974. {
  1975. if(v) return sq_throwerror(v, sqlite3_errmsg(db));
  1976. }
  1977. }
  1978. return rc;
  1979. }
  1980. static SQRESULT sq_sqlite3_releasehook(SQUserPointer p, SQInteger size, void */*ep*/)
  1981. {
  1982. sq_sqlite3_sdb *sdb = ((sq_sqlite3_sdb *)p);
  1983. sq_sqlite3_close_release(0, sdb);
  1984. return 0;
  1985. }
  1986. static SQRESULT sq_sqlite3_constructor(HSQUIRRELVM v)
  1987. {
  1988. SQ_FUNC_VARS(v);
  1989. SQ_GET_STRING(v, 2, dbname);
  1990. SQ_OPT_INTEGER(v, 3, flags, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_SUBLATIN_NA_LIKE);
  1991. //SQ_OPT_INTEGER(v, 3, flags, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE);
  1992. sqlite3 *db;
  1993. int rc = sqlite3_open_v2(dbname, &db, flags, 0);
  1994. if(rc != SQLITE_OK) return sq_throwerror(v, "Failed to open database ! %d", rc);
  1995. set_sqlite3_lua_regex_func(db);
  1996. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb *)sq_malloc(sizeof(sq_sqlite3_sdb));
  1997. memset(sdb, 0, sizeof(sq_sqlite3_sdb));
  1998. sdb->db = db;
  1999. sdb->v = v;
  2000. sq_resetobject(&sdb->busy_cb);
  2001. sq_resetobject(&sdb->busy_udata);
  2002. sq_resetobject(&sdb->progress_cb);
  2003. sq_resetobject(&sdb->progress_udata);
  2004. sq_resetobject(&sdb->trace_cb);
  2005. sq_resetobject(&sdb->trace_udata);
  2006. sq_resetobject(&sdb->update_hook_cb);
  2007. sq_resetobject(&sdb->update_hook_udata);
  2008. sq_resetobject(&sdb->authorizer_hook_cb);
  2009. sq_resetobject(&sdb->authorizer_hook_udata);
  2010. sq_resetobject(&sdb->null_value);
  2011. sq_setinstanceup(v, 1, sdb);
  2012. sq_setreleasehook(v,1, sq_sqlite3_releasehook);
  2013. //save a weakref to allow statement return it's db
  2014. sq_pushuserpointer(v, sdb->db);
  2015. sq_weakref(v, 1);
  2016. sq_setonregistrytable(v);
  2017. return 1;
  2018. }
  2019. static SQRESULT sq_sqlite3_close(HSQUIRRELVM v)
  2020. {
  2021. SQ_FUNC_VARS_NO_TOP(v);
  2022. GET_sqlite3_INSTANCE();
  2023. if(sq_sqlite3_close_release(v, sdb) != SQ_OK) return SQ_ERROR;
  2024. sq_setinstanceup(v, 1, 0); //next calls will fail with "database is closed"
  2025. return 0;
  2026. }
  2027. static SQRESULT sq_sqlite3_db_ptr(HSQUIRRELVM v)
  2028. {
  2029. SQ_FUNC_VARS_NO_TOP(v);
  2030. GET_sqlite3_INSTANCE();
  2031. sq_pushuserpointer(v, self);
  2032. return 1;
  2033. }
  2034. /* bool IsAutoCommitOn( ) */
  2035. static SQRESULT sq_sqlite3_IsAutoCommitOn(HSQUIRRELVM v)
  2036. {
  2037. SQ_FUNC_VARS_NO_TOP(v);
  2038. GET_sqlite3_INSTANCE();
  2039. sq_pushbool(v, sqlite3_get_autocommit(self) ? SQTrue : SQFalse);
  2040. return 1;
  2041. }
  2042. /* int changes( ) */
  2043. static SQRESULT sq_sqlite3_changes(HSQUIRRELVM v)
  2044. {
  2045. SQ_FUNC_VARS_NO_TOP(v);
  2046. GET_sqlite3_INSTANCE();
  2047. sq_pushinteger(v, sqlite3_changes(self));
  2048. return 1;
  2049. }
  2050. static SQRESULT sq_sqlite3_exec(HSQUIRRELVM v)
  2051. {
  2052. return sqlite3_exec_fmt(v, tr_all_rows);
  2053. }
  2054. /* int exec_one_dml( const char * szSQL, ... ) */
  2055. static SQRESULT sq_sqlite3_exec_one_dml(HSQUIRRELVM v)
  2056. {
  2057. return sqlite3_exec_fmt(v, tr_ddml);
  2058. }
  2059. /* int exec_dml( const char * szSQL ) */
  2060. static SQRESULT sq_sqlite3_exec_dml(HSQUIRRELVM v)
  2061. {
  2062. SQ_FUNC_VARS_NO_TOP(v);
  2063. GET_sqlite3_INSTANCE();
  2064. SQ_GET_STRING(v, 2, szSQL);
  2065. char *errmsg;
  2066. if(sqlite3_exec(self, szSQL, NULL, NULL, &errmsg) != SQLITE_OK){
  2067. sq_throwerror(v, errmsg);
  2068. sqlite3_free(errmsg);
  2069. return SQ_ERROR;
  2070. }
  2071. sq_pushinteger(v, sqlite3_changes(self));
  2072. return 1;
  2073. }
  2074. /* bool exec_get_one( std::string & result , const char * szSQL ) */
  2075. static SQRESULT sq_sqlite3_exec_get_one(HSQUIRRELVM v)
  2076. {
  2077. return sqlite3_exec_fmt(v, tr_first_row_first_col);
  2078. }
  2079. static SQRESULT sq_sqlite3_exec_get_first_row(HSQUIRRELVM v)
  2080. {
  2081. return sqlite3_exec_fmt(v, tr_first_row);
  2082. }
  2083. /* const char * get_db_name( ) */
  2084. static SQRESULT sq_sqlite3_get_db_name(HSQUIRRELVM v)
  2085. {
  2086. SQ_FUNC_VARS_NO_TOP(v);
  2087. GET_sqlite3_INSTANCE();
  2088. sq_pushstring(v, sqlite3_db_filename(self, "main"), -1);
  2089. return 1;
  2090. }
  2091. /* sqlite3_int64 last_row_id( ) */
  2092. static SQRESULT sq_sqlite3_last_row_id(HSQUIRRELVM v)
  2093. {
  2094. SQ_FUNC_VARS_NO_TOP(v);
  2095. GET_sqlite3_INSTANCE();
  2096. sq_pushinteger(v, sqlite3_last_insert_rowid(self));
  2097. return 1;
  2098. }
  2099. /* stmt * prepare( const char * sql ) */
  2100. static SQRESULT sq_sqlite3_prepare(HSQUIRRELVM v)
  2101. {
  2102. SQ_FUNC_VARS_NO_TOP(v);
  2103. //GET_sqlite3_INSTANCE();
  2104. SQ_GET_STRING(v, 2, sql);
  2105. sq_pushstring(v, SQLite3_Stmt_TAG, -1);
  2106. if(sq_getonroottable(v) == SQ_ERROR) return SQ_ERROR;
  2107. sq_pushroottable(v);
  2108. sq_push(v, 1);
  2109. sq_push(v, 2);
  2110. if(sq_call(v, 3, SQTrue, SQFalse) != SQ_OK) return SQ_ERROR;
  2111. return 1;
  2112. }
  2113. /* stmt_rest * prepare( stmt *p ) */
  2114. static SQRESULT sq_sqlite3_stmt_scope_reset(HSQUIRRELVM v)
  2115. {
  2116. SQ_FUNC_VARS_NO_TOP(v);
  2117. GET_sqlite3_stmt_INSTANCE_AT(2, stmt);
  2118. sq_pushstring(v, SQLite3_StmtScopeReset_TAG, -1);
  2119. if(sq_getonroottable(v) == SQ_ERROR) return SQ_ERROR;
  2120. sq_pushroottable(v);
  2121. sq_push(v, 2);
  2122. if(sq_call(v, 2, SQTrue, SQFalse) != SQ_OK) return SQ_ERROR;
  2123. return 1;
  2124. }
  2125. /* transaction */
  2126. static SQRESULT sq_sqlite3_transaction(HSQUIRRELVM v)
  2127. {
  2128. sq_pushstring(v, SQLite3_Transaction_TAG, -1);
  2129. if(sq_getonroottable(v) == SQ_ERROR) return SQ_ERROR;
  2130. sq_pushroottable(v);
  2131. sq_push(v, 1);
  2132. if(sq_call(v, 2, SQTrue, SQFalse) != SQ_OK) return SQ_ERROR;
  2133. return 1;
  2134. }
  2135. //sqlite3_blob
  2136. static SQRESULT sq_sqlite3_blob_releasehook(SQUserPointer p, SQInteger size, void */*ep*/)
  2137. {
  2138. sqlite3_blob *self = ((sqlite3_blob *)p);
  2139. if(self) sqlite3_blob_close(self);
  2140. return 0;
  2141. }
  2142. static SQRESULT sq_sqlite3_blob_close(HSQUIRRELVM v)
  2143. {
  2144. SQ_FUNC_VARS_NO_TOP(v);
  2145. GET_sqlite3_blob_INSTANCE();
  2146. sq_setinstanceup(v, 1, 0); //next calls will fail with "blob is closed"
  2147. sq_pushinteger(v, sqlite3_blob_close(self));
  2148. return 1;
  2149. }
  2150. static SQRESULT sq_sqlite3_blob_reopen(HSQUIRRELVM v)
  2151. {
  2152. SQ_FUNC_VARS_NO_TOP(v);
  2153. GET_sqlite3_blob_INSTANCE();
  2154. SQ_GET_INTEGER(v, 2, iRow);
  2155. int rc = sqlite3_blob_reopen(self, iRow);
  2156. if(rc != SQLITE_OK) return sq_sqlite3_blob_close(v);
  2157. sq_pushinteger(v, rc);
  2158. return 1;
  2159. }
  2160. static SQRESULT sq_sqlite3_blob_bytes(HSQUIRRELVM v)
  2161. {
  2162. SQ_FUNC_VARS_NO_TOP(v);
  2163. GET_sqlite3_blob_INSTANCE();
  2164. sq_pushinteger(v, sqlite3_blob_bytes(self));
  2165. return 1;
  2166. }
  2167. static SQRESULT sq_sqlite3_blob_read(HSQUIRRELVM v)
  2168. {
  2169. SQ_FUNC_VARS(v);
  2170. GET_sqlite3_blob_INSTANCE();
  2171. SQ_OPT_INTEGER(v, 2, size, 0);
  2172. SQ_OPT_INTEGER(v, 3, offset, 0);
  2173. if(!size) size = sqlite3_blob_bytes(self);
  2174. SQChar *buf = sq_getscratchpad(v, size);
  2175. int rc = sqlite3_blob_read(self, buf, size, offset);
  2176. if(rc == SQLITE_OK) sq_pushstring(v, buf, size);
  2177. else sq_pushnull(v);
  2178. return 1;
  2179. }
  2180. static SQRESULT sq_sqlite3_blob_write(HSQUIRRELVM v)
  2181. {
  2182. SQ_FUNC_VARS(v);
  2183. GET_sqlite3_blob_INSTANCE();
  2184. SQ_GET_STRING(v, 2, buf);
  2185. SQ_OPT_INTEGER(v, 3, size, buf_size);
  2186. SQ_OPT_INTEGER(v, 4, offset, 0);
  2187. int rc = sqlite3_blob_write(self, buf, size, offset);
  2188. sq_pushinteger(v, rc);
  2189. return 1;
  2190. }
  2191. static SQRESULT sq_sqlite3_blob_open(HSQUIRRELVM v)
  2192. {
  2193. SQ_FUNC_VARS(v);
  2194. GET_sqlite3_INSTANCE();
  2195. SQ_GET_STRING(v, 2, zDb);
  2196. SQ_GET_STRING(v, 3, zTable);
  2197. SQ_GET_STRING(v, 4, zColumn);
  2198. SQ_GET_INTEGER(v, 5, iRow);
  2199. SQ_OPT_INTEGER(v, 6, flags, 0);
  2200. sqlite3_blob *pBlob;
  2201. int rc = sqlite3_blob_open(self, zDb, zTable, zColumn, iRow, flags, &pBlob);
  2202. if(rc == SQLITE_OK)
  2203. {
  2204. sq_pushstring(v, SQLite3_Blob_TAG, -1);
  2205. if(sq_getonregistrytable(v) == SQ_ERROR) return SQ_ERROR;
  2206. sq_createinstance(v, -1);
  2207. sq_setinstanceup(v, -1, pBlob);
  2208. sq_setreleasehook(v, -1, sq_sqlite3_blob_releasehook);
  2209. }
  2210. else sq_pushnull(v);
  2211. return 1;
  2212. }
  2213. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_sqlite3_blob_##name,nparams,tycheck}
  2214. static SQRegFunction sq_sqlite3_blob_methods[] =
  2215. {
  2216. _DECL_FUNC(close, 1, _SC("x")),
  2217. _DECL_FUNC(reopen, 2, _SC("xi")),
  2218. _DECL_FUNC(bytes, 1, _SC("x")),
  2219. _DECL_FUNC(read, -1, _SC("xii")),
  2220. _DECL_FUNC(write, -2, _SC("xsii")),
  2221. {0,0}
  2222. };
  2223. #undef _DECL_FUNC
  2224. /* void set_busy_timeout( int nMillisecs ) */
  2225. static SQRESULT sq_sqlite3_set_busy_timeout(HSQUIRRELVM v)
  2226. {
  2227. SQ_FUNC_VARS_NO_TOP(v);
  2228. //GET_sqlite3_INSTANCE();
  2229. SQ_GET_INTEGER(v, 2, nMillisecs);
  2230. //self->set_busy_timeout(nMillisecs);
  2231. return 0;
  2232. }
  2233. /* int total_changes( ) */
  2234. static SQRESULT sq_sqlite3_total_changes(HSQUIRRELVM v)
  2235. {
  2236. SQ_FUNC_VARS_NO_TOP(v);
  2237. GET_sqlite3_INSTANCE();
  2238. sq_pushinteger(v, sqlite3_total_changes(self));
  2239. return 1;
  2240. }
  2241. static SQRESULT sq_sqlite3_enable_shared_cache(HSQUIRRELVM v)
  2242. {
  2243. SQ_FUNC_VARS_NO_TOP(v);
  2244. SQ_GET_BOOL(v, 2, bhow);
  2245. sq_pushbool(v, sqlite3_enable_shared_cache(bhow) == SQLITE_OK);
  2246. return 1;
  2247. }
  2248. static SQRESULT sq_sqlite3_sleep(HSQUIRRELVM v)
  2249. {
  2250. SQ_FUNC_VARS_NO_TOP(v);
  2251. SQ_GET_INTEGER(v, 2, n);
  2252. sqlite3_sleep(n);
  2253. return 0;
  2254. }
  2255. static SQRESULT sq_sqlite3_version(HSQUIRRELVM v)
  2256. {
  2257. sq_pushstring(v, sqlite3_libversion(), -1);
  2258. return 1;
  2259. }
  2260. static SQRESULT sq_sqlite3_threadsafe(HSQUIRRELVM v)
  2261. {
  2262. sq_pushinteger(v, sqlite3_threadsafe());
  2263. return 1;
  2264. }
  2265. static SQRESULT sq_sqlite3_config_single_thread(HSQUIRRELVM v)
  2266. {
  2267. sq_pushinteger(v, sqlite3_config(SQLITE_CONFIG_SINGLETHREAD));
  2268. return 1;
  2269. }
  2270. static SQRESULT sq_sqlite3_config_multi_thread(HSQUIRRELVM v)
  2271. {
  2272. sq_pushinteger(v, sqlite3_config(SQLITE_CONFIG_MULTITHREAD));
  2273. return 1;
  2274. }
  2275. static SQRESULT sq_sqlite3_config_serialized(HSQUIRRELVM v)
  2276. {
  2277. sq_pushinteger(v, sqlite3_config(SQLITE_CONFIG_SERIALIZED));
  2278. return 1;
  2279. }
  2280. static SQRESULT sq_sqlite3_errcode(HSQUIRRELVM v)
  2281. {
  2282. SQ_FUNC_VARS_NO_TOP(v);
  2283. GET_sqlite3_INSTANCE();
  2284. sq_pushinteger(v, sqlite3_errcode(self));
  2285. return 1;
  2286. }
  2287. static SQRESULT sq_sqlite3_errmsg(HSQUIRRELVM v)
  2288. {
  2289. SQ_FUNC_VARS_NO_TOP(v);
  2290. GET_sqlite3_INSTANCE();
  2291. sq_pushstring(v, sqlite3_errmsg(self), -1);
  2292. return 1;
  2293. }
  2294. #ifndef WIN32
  2295. static SQRESULT sq_sqlite3_temp_directory(HSQUIRRELVM v)
  2296. {
  2297. SQ_FUNC_VARS(v);
  2298. const char *oldtemp = sqlite3_temp_directory;
  2299. sq_pushstring(v, oldtemp, -1);
  2300. if (_top_ > 1)
  2301. {
  2302. SQ_GET_STRING(v, 2, temp);
  2303. if (sqlite3_temp_directory)
  2304. {
  2305. sqlite3_free((char*)sqlite3_temp_directory);
  2306. }
  2307. if (temp && temp_size)
  2308. {
  2309. sqlite3_temp_directory = sqlite3_mprintf("%s", temp);
  2310. }
  2311. else
  2312. {
  2313. sqlite3_temp_directory = NULL;
  2314. }
  2315. }
  2316. return 1;
  2317. }
  2318. #endif
  2319. #ifdef SQLITE_HAS_CODEC
  2320. //#include "codecext.h"
  2321. /*
  2322. ** Params: db, sql
  2323. ** returns: code, compiled length or error message
  2324. */
  2325. static SQRESULT sq_sqlite3_key(HSQUIRRELVM v)
  2326. {
  2327. SQ_FUNC_VARS_NO_TOP(v);
  2328. GET_sqlite3_INSTANCE();
  2329. SQ_GET_STRING(v, 2, key);
  2330. sq_pushinteger(v, sqlite3_key_v2(self, 0, key, key_size));
  2331. return 1;
  2332. }
  2333. /*
  2334. ** Params: db, sql
  2335. ** returns: code, compiled length or error message
  2336. */
  2337. static SQRESULT sq_sqlite3_rekey(HSQUIRRELVM v)
  2338. {
  2339. SQ_FUNC_VARS_NO_TOP(v);
  2340. GET_sqlite3_INSTANCE();
  2341. SQ_GET_STRING(v, 2, key);
  2342. sq_pushinteger(v, sqlite3_rekey_v2(self, 0, key, key_size));
  2343. return 1;
  2344. }
  2345. #endif
  2346. #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK
  2347. /*
  2348. ** progress handler:
  2349. ** Params: database, number of opcodes, callback function, userdata
  2350. **
  2351. ** callback function:
  2352. ** Params: userdata
  2353. ** returns: 0 to return immediatly and return SQLITE_ABORT, non-zero to continue
  2354. */
  2355. static int db_progress_callback(void *udata)
  2356. {
  2357. SQInteger result = 1; /* abort by default */
  2358. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)udata;
  2359. HSQUIRRELVM v = sdb->v;
  2360. int top = sq_gettop(v);
  2361. sq_pushobject(v, sdb->progress_cb);
  2362. sq_pushroottable(v); //this
  2363. sq_pushobject(v, sdb->progress_udata);
  2364. /* call squilu function */
  2365. if (sq_call(v, 2, SQTrue, SQFalse) == SQ_OK)
  2366. sq_getinteger(v, -1, &result);
  2367. sq_settop(v, top);
  2368. return result;
  2369. }
  2370. static SQRESULT sq_sqlite3_progress_handler(HSQUIRRELVM v)
  2371. {
  2372. SQ_FUNC_VARS(v);
  2373. GET_sqlite3_INSTANCE();
  2374. /* clear progress handler */
  2375. sqlite3_progress_handler(self, 0, NULL, NULL);
  2376. sq_release(v, &sdb->progress_cb);
  2377. sq_release(v, &sdb->progress_udata);
  2378. sq_resetobject(&sdb->progress_cb);
  2379. sq_resetobject(&sdb->progress_udata);
  2380. SQObjectType otype = sq_gettype(v, 2);
  2381. if (_top_ > 1 && (otype != OT_NULL))
  2382. {
  2383. SQ_GET_INTEGER(v, 2, nop);
  2384. if(sq_gettype(v, 3) != OT_CLOSURE)
  2385. return sq_throwerror(v, _SC("invalid second parameter expected closure"));
  2386. sq_getstackobj(v, 3, &sdb->progress_cb);
  2387. sq_addref(v, &sdb->progress_cb);
  2388. if(_top_ > 3)
  2389. {
  2390. sq_getstackobj(v, 4, &sdb->progress_udata);
  2391. sq_addref(v, &sdb->progress_udata);
  2392. }
  2393. /* set progress callback */
  2394. sqlite3_progress_handler(self, nop, db_progress_callback, sdb);
  2395. }
  2396. return 0;
  2397. }
  2398. #else /* #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK */
  2399. static SQRESULT sq_sqlite3_progress_handler(HSQUIRRELVM v)
  2400. {
  2401. return sq_throwerror(v, _SC("progress callback support disabled at compile time"));
  2402. return 0;
  2403. }
  2404. #endif /* #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) || !SQLITE_OMIT_PROGRESS_CALLBACK */
  2405. /*
  2406. ** trace callback:
  2407. ** Params: database, callback function, userdata
  2408. **
  2409. ** callback function:
  2410. ** Params: userdata, sql
  2411. */
  2412. static void db_trace_callback(void *udata, const char *sql)
  2413. {
  2414. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)udata;
  2415. HSQUIRRELVM v = sdb->v;
  2416. int top = sq_gettop(v);
  2417. /* setup squirrel callback call */
  2418. sq_pushobject(v, sdb->trace_cb);
  2419. sq_pushroottable(v);
  2420. sq_pushobject(v, sdb->trace_udata);
  2421. sq_pushstring(v, sql, -1); /* traced sql statement */
  2422. /* call squirrel function */
  2423. sq_call(v, 3, SQFalse, SQFalse);
  2424. /* ignore any error generated by this function */
  2425. sq_settop(v, top);
  2426. }
  2427. static SQRESULT sq_sqlite3_trace(HSQUIRRELVM v)
  2428. {
  2429. SQ_FUNC_VARS(v);
  2430. GET_sqlite3_INSTANCE();
  2431. /* clear trace handler */
  2432. sqlite3_trace(self, NULL, NULL);
  2433. sq_release(v, &sdb->trace_cb);
  2434. sq_release(v, &sdb->trace_udata);
  2435. sq_resetobject(&sdb->trace_cb);
  2436. sq_resetobject(&sdb->trace_udata);
  2437. SQObjectType otype = sq_gettype(v, 2);
  2438. if (_top_ > 1 && (otype != OT_NULL))
  2439. {
  2440. if(sq_gettype(v, 2) != OT_CLOSURE)
  2441. return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
  2442. sq_getstackobj(v, 2, &sdb->trace_cb);
  2443. sq_addref(v, &sdb->trace_cb);
  2444. if(_top_ > 2)
  2445. {
  2446. sq_getstackobj(v, 3, &sdb->trace_udata);
  2447. sq_addref(v, &sdb->trace_udata);
  2448. }
  2449. /* set trace callback */
  2450. if(_top_ > 3)
  2451. {
  2452. SQBool b;
  2453. sq_getbool(v, 4, &b);
  2454. sqlite3_trace_v0(self, db_trace_callback, sdb, b == SQTrue);
  2455. }
  2456. else sqlite3_trace(self, db_trace_callback, sdb);
  2457. }
  2458. return 0;
  2459. }
  2460. /*
  2461. ** update hook callback:
  2462. ** Params: database, callback function, userdata
  2463. **
  2464. ** callback function:
  2465. ** Params: userdata, sql
  2466. */
  2467. static void db_update_hook_callback(void *udata, int update_type,
  2468. char const *db_name, char const *table_name, sqlite3_int64 rowid)
  2469. {
  2470. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)udata;
  2471. HSQUIRRELVM v = sdb->v;
  2472. int top = sq_gettop(v);
  2473. /* setup squirrel callback call */
  2474. sq_pushobject(v, sdb->update_hook_cb);
  2475. sq_pushroottable(v);
  2476. sq_pushobject(v, sdb->update_hook_udata);
  2477. sq_pushinteger(v, update_type);
  2478. sq_pushstring(v, db_name, -1);
  2479. sq_pushstring(v, table_name, -1);
  2480. sq_pushinteger(v, rowid);
  2481. /* call squirrel function */
  2482. sq_call(v, 6, SQFalse, SQFalse);
  2483. /* ignore any error generated by this function */
  2484. sq_settop(v, top);
  2485. }
  2486. static SQRESULT sq_sqlite3_update_hook(HSQUIRRELVM v)
  2487. {
  2488. SQ_FUNC_VARS(v);
  2489. GET_sqlite3_INSTANCE();
  2490. /* clear update handler */
  2491. sqlite3_update_hook(self, NULL, NULL);
  2492. sq_release(v, &sdb->update_hook_cb);
  2493. sq_release(v, &sdb->update_hook_udata);
  2494. sq_resetobject(&sdb->update_hook_cb);
  2495. sq_resetobject(&sdb->update_hook_udata);
  2496. SQObjectType otype = sq_gettype(v, 2);
  2497. if (_top_ > 1 && (otype != OT_NULL))
  2498. {
  2499. if(sq_gettype(v, 2) != OT_CLOSURE)
  2500. return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
  2501. sq_getstackobj(v, 2, &sdb->update_hook_cb);
  2502. sq_addref(v, &sdb->update_hook_cb);
  2503. if(_top_ > 2)
  2504. {
  2505. sq_getstackobj(v, 3, &sdb->update_hook_udata);
  2506. sq_addref(v, &sdb->update_hook_udata);
  2507. }
  2508. /* set update_hook callback */
  2509. sqlite3_update_hook(self, db_update_hook_callback, sdb);
  2510. }
  2511. return 0;
  2512. }
  2513. /*
  2514. ** authorizer hook callback:
  2515. ** Params: database, callback function, userdata
  2516. **
  2517. ** callback function:
  2518. ** Params: userdata, sql
  2519. */
  2520. #if 0
  2521. /******************************************* 3rd ************ 4th ***********/
  2522. #define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */
  2523. #define SQLITE_CREATE_TABLE 2 /* Table Name NULL */
  2524. #define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */
  2525. #define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */
  2526. #define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
  2527. #define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */
  2528. #define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */
  2529. #define SQLITE_CREATE_VIEW 8 /* View Name NULL */
  2530. #define SQLITE_DELETE 9 /* Table Name NULL */
  2531. #define SQLITE_DROP_INDEX 10 /* Index Name Table Name */
  2532. #define SQLITE_DROP_TABLE 11 /* Table Name NULL */
  2533. #define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */
  2534. #define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */
  2535. #define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */
  2536. #define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */
  2537. #define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */
  2538. #define SQLITE_DROP_VIEW 17 /* View Name NULL */
  2539. #define SQLITE_INSERT 18 /* Table Name NULL */
  2540. #define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */
  2541. #define SQLITE_READ 20 /* Table Name Column Name */
  2542. #define SQLITE_SELECT 21 /* NULL NULL */
  2543. #define SQLITE_TRANSACTION 22 /* Operation NULL */
  2544. #define SQLITE_UPDATE 23 /* Table Name Column Name */
  2545. #define SQLITE_ATTACH 24 /* Filename NULL */
  2546. #define SQLITE_DETACH 25 /* Database Name NULL */
  2547. #define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
  2548. #define SQLITE_REINDEX 27 /* Index Name NULL */
  2549. #define SQLITE_ANALYZE 28 /* Table Name NULL */
  2550. #define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
  2551. #define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
  2552. #define SQLITE_FUNCTION 31 /* NULL Function Name */
  2553. #define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */
  2554. #define SQLITE_COPY 0 /* No longer used */
  2555. #define SQLITE_RECURSIVE 33 /* NULL NULL */
  2556. #endif
  2557. static int db_authorizer_hook_callback(void *udata, int action_code,
  2558. char const *param3, char const *param4,
  2559. char const *param5, char const *param6)
  2560. {
  2561. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)udata;
  2562. HSQUIRRELVM v = sdb->v;
  2563. int top = sq_gettop(v);
  2564. SQInteger result = SQLITE_OK;
  2565. /* setup squirrel callback call */
  2566. sq_pushobject(v, sdb->authorizer_hook_cb);
  2567. sq_pushroottable(v);
  2568. sq_pushobject(v, sdb->authorizer_hook_udata);
  2569. sq_pushinteger(v, action_code);
  2570. sq_pushstring(v, param3, -1);
  2571. sq_pushstring(v, param4, -1);
  2572. sq_pushstring(v, param5, -1);
  2573. sq_pushstring(v, param6, -1);
  2574. /* call squilu function */
  2575. /* ignore any error generated by this function */
  2576. if (sq_call(v, 7, SQTrue, SQFalse) == SQ_OK)
  2577. sq_getinteger(v, -1, &result);
  2578. sq_settop(v, top);
  2579. return result;
  2580. }
  2581. static SQRESULT sq_sqlite3_set_authorizer(HSQUIRRELVM v)
  2582. {
  2583. SQ_FUNC_VARS(v);
  2584. GET_sqlite3_INSTANCE();
  2585. /* clear trace handler */
  2586. sqlite3_set_authorizer(self, NULL, NULL);
  2587. sq_release(v, &sdb->authorizer_hook_cb);
  2588. sq_release(v, &sdb->authorizer_hook_udata);
  2589. sq_resetobject(&sdb->authorizer_hook_cb);
  2590. sq_resetobject(&sdb->authorizer_hook_udata);
  2591. SQObjectType otype = sq_gettype(v, 2);
  2592. if (_top_ > 1 && (otype != OT_NULL))
  2593. {
  2594. if(sq_gettype(v, 2) != OT_CLOSURE)
  2595. return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
  2596. sq_getstackobj(v, 2, &sdb->authorizer_hook_cb);
  2597. sq_addref(v, &sdb->authorizer_hook_cb);
  2598. if(_top_ > 2)
  2599. {
  2600. sq_getstackobj(v, 3, &sdb->authorizer_hook_udata);
  2601. sq_addref(v, &sdb->authorizer_hook_udata);
  2602. }
  2603. /* set authorizer_hook callback */
  2604. sqlite3_set_authorizer(self, db_authorizer_hook_callback, sdb);
  2605. }
  2606. return 0;
  2607. }
  2608. /*
  2609. ** busy handler:
  2610. ** Params: database, callback function, userdata
  2611. **
  2612. ** callback function:
  2613. ** Params: userdata, number of tries
  2614. ** returns: 0 to return immediatly and return SQLITE_BUSY, non-zero to try again
  2615. */
  2616. static int db_busy_callback(void *udata, int tries)
  2617. {
  2618. SQBool retry = SQFalse; /* abort by default */
  2619. sq_sqlite3_sdb *sdb = (sq_sqlite3_sdb*)udata;
  2620. HSQUIRRELVM v = sdb->v;
  2621. int top = sq_gettop(v);
  2622. sq_pushobject(v, sdb->busy_cb);
  2623. sq_pushroottable(v);
  2624. sq_pushobject(v, sdb->busy_udata);
  2625. sq_pushinteger(v, tries);
  2626. /* call squilu function */
  2627. if (sq_call(v, 3, SQTrue, SQFalse) == SQ_OK)
  2628. sq_getbool(v, -1, &retry);
  2629. sq_settop(v, top);
  2630. return retry == SQTrue;
  2631. }
  2632. static SQRESULT sq_sqlite3_busy_handler(HSQUIRRELVM v)
  2633. {
  2634. SQ_FUNC_VARS(v);
  2635. GET_sqlite3_INSTANCE();
  2636. /* clear busy handler */
  2637. sqlite3_busy_handler(self, NULL, NULL);
  2638. sq_release(v, &sdb->busy_cb);
  2639. sq_release(v, &sdb->busy_udata);
  2640. sq_resetobject(&sdb->busy_cb);
  2641. sq_resetobject(&sdb->busy_udata);
  2642. SQObjectType otype = sq_gettype(v, 2);
  2643. if (_top_ > 1 && (otype != OT_NULL))
  2644. {
  2645. if(sq_gettype(v, 2) != OT_CLOSURE)
  2646. return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
  2647. sq_getstackobj(v, 2, &sdb->busy_cb);
  2648. sq_addref(v, &sdb->busy_cb);
  2649. if(_top_ > 2)
  2650. {
  2651. sq_getstackobj(v, 3, &sdb->busy_udata);
  2652. sq_addref(v, &sdb->busy_udata);
  2653. }
  2654. /* set busy callback */
  2655. sqlite3_busy_handler(self, db_busy_callback, sdb);
  2656. }
  2657. return 0;
  2658. }
  2659. static SQRESULT sq_sqlite3_busy_timeout(HSQUIRRELVM v)
  2660. {
  2661. SQ_FUNC_VARS_NO_TOP(v);
  2662. GET_sqlite3_INSTANCE();
  2663. SQ_GET_INTEGER(v, 2, timeout);
  2664. sqlite3_busy_timeout(self, timeout);
  2665. /* if there was a timeout callback registered, it is now
  2666. ** invalid/useless. free any references we may have */
  2667. sq_release(v, &sdb->busy_cb);
  2668. sq_resetobject(&sdb->busy_cb);
  2669. sq_release(v, &sdb->busy_udata);
  2670. sq_resetobject(&sdb->busy_udata);
  2671. return 0;
  2672. }
  2673. static SQRESULT sq_sqlite3_interrupt(HSQUIRRELVM v)
  2674. {
  2675. SQ_FUNC_VARS_NO_TOP(v);
  2676. GET_sqlite3_INSTANCE();
  2677. sqlite3_interrupt(self);
  2678. return 0;
  2679. }
  2680. /*
  2681. ** =======================================================
  2682. ** User Defined Functions - Context Methods
  2683. ** =======================================================
  2684. */
  2685. typedef struct
  2686. {
  2687. sqlite3_context *ctx;
  2688. HSQOBJECT udata;
  2689. HSQUIRRELVM v;
  2690. } sq_sqlite3_context_st;
  2691. static const SQChar sq_sqlite3_context_TAG[] = _SC("sqite3_context");
  2692. static SQRESULT sq_sqlite3_context_releasehook(SQUserPointer p, SQInteger size, void */*ep*/)
  2693. {
  2694. sq_sqlite3_context_st *ctx = ((sq_sqlite3_context_st *)p);
  2695. /* 'free' all references */
  2696. sq_release(ctx->v, &ctx->udata);
  2697. sq_free(ctx, sizeof(sq_sqlite3_context_st));
  2698. return 0;
  2699. }
  2700. static SQRESULT sq_sqlite3_context_constructor(HSQUIRRELVM v)
  2701. {
  2702. sq_sqlite3_context_st *ctx = (sq_sqlite3_context_st*)sq_malloc(sizeof(sq_sqlite3_context_st));
  2703. ctx->ctx = NULL;
  2704. ctx->v = v;
  2705. sq_resetobject(&ctx->udata);
  2706. sq_setinstanceup(v, 1, ctx);
  2707. sq_setreleasehook(v,1, sq_sqlite3_context_releasehook);
  2708. return 1;
  2709. }
  2710. #define SQ_SQLITE3_GETCONTEXT(v, idx) \
  2711. sq_sqlite3_context_st *self;\
  2712. if((_rc_ = sq_getinstanceup(v, idx, (void**)&self, (void*)sq_sqlite3_context_TAG)) < 0) return _rc_;
  2713. #define GET_sqlite3_context_INSTANCE() SQ_SQLITE3_GETCONTEXT(v,1)
  2714. static SQRESULT sq_sqlite3_context__tostring(HSQUIRRELVM v)
  2715. {
  2716. SQ_FUNC_VARS_NO_TOP(v);
  2717. GET_sqlite3_context_INSTANCE();
  2718. char buff[64];
  2719. snprintf(buff, sizeof(buff), "sqlite function context (%p)", self);
  2720. sq_pushstring(v, buff, -1);
  2721. return 1;
  2722. }
  2723. static SQRESULT sq_sqlite3_context_check_aggregate(HSQUIRRELVM v, sq_sqlite3_context_st *ctx)
  2724. {
  2725. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(ctx->ctx);
  2726. if (sq_isclosure(func->fn_finalize))
  2727. {
  2728. return sq_throwerror(v, "attempt to call aggregate method from scalar function");
  2729. }
  2730. return 1;
  2731. }
  2732. static SQRESULT sq_sqlite3_context_user_data(HSQUIRRELVM v)
  2733. {
  2734. SQ_FUNC_VARS_NO_TOP(v);
  2735. GET_sqlite3_context_INSTANCE();
  2736. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(self->ctx);
  2737. sq_pushobject(v, func->udata);
  2738. return 1;
  2739. }
  2740. static SQRESULT sq_sqlite3_context_aggregate_data(HSQUIRRELVM v)
  2741. {
  2742. SQ_FUNC_VARS(v);
  2743. GET_sqlite3_context_INSTANCE();
  2744. sq_sqlite3_context_check_aggregate(v, self);
  2745. if(_top_ < 2)
  2746. {
  2747. sq_pushobject(v, self->udata);
  2748. }
  2749. else
  2750. {
  2751. sq_release(v, &self->udata);
  2752. sq_getstackobj(v, 2, &self->udata);
  2753. sq_addref(v, &self->udata);
  2754. }
  2755. return 0;
  2756. }
  2757. static SQRESULT sq_sqlite3_context_aggregate_count(HSQUIRRELVM v)
  2758. {
  2759. SQ_FUNC_VARS_NO_TOP(v);
  2760. GET_sqlite3_context_INSTANCE();
  2761. sq_sqlite3_context_check_aggregate(v, self);
  2762. sq_pushinteger(v, sqlite3_aggregate_count(self->ctx));
  2763. return 1;
  2764. }
  2765. struct sq_sqlite3_context_auxdata_st
  2766. {
  2767. HSQUIRRELVM vm;
  2768. HSQOBJECT data;
  2769. };
  2770. static SQRESULT sq_sqlite3_context_get_auxdata(HSQUIRRELVM v)
  2771. {
  2772. SQ_FUNC_VARS_NO_TOP(v);
  2773. GET_sqlite3_context_INSTANCE();
  2774. SQ_GET_INTEGER(v, 2, argn);
  2775. sq_sqlite3_context_auxdata_st *data = (sq_sqlite3_context_auxdata_st*)sqlite3_get_auxdata(self->ctx, argn);
  2776. if(data)
  2777. {
  2778. sq_pushobject(v, data->data);
  2779. }
  2780. else
  2781. {
  2782. sq_pushnull(v);
  2783. }
  2784. return 1;
  2785. }
  2786. static void sq_sqlite3_context_free_auxdata(void *aux_data)
  2787. {
  2788. sq_sqlite3_context_auxdata_st *data = (sq_sqlite3_context_auxdata_st*)aux_data;
  2789. if(data)
  2790. {
  2791. sq_release(data->vm, &data->data);
  2792. sqlite3_free(data);
  2793. }
  2794. }
  2795. static SQRESULT sq_sqlite3_context_set_auxdata(HSQUIRRELVM v)
  2796. {
  2797. SQ_FUNC_VARS_NO_TOP(v);
  2798. GET_sqlite3_context_INSTANCE();
  2799. SQ_GET_INTEGER(v, 2, argn);
  2800. sq_sqlite3_context_auxdata_st *data = (sq_sqlite3_context_auxdata_st*)sqlite3_malloc(sizeof(*data));
  2801. if(data)
  2802. {
  2803. data->vm = v;
  2804. sq_resetobject(&data->data);
  2805. sq_getstackobj(v, 3, &data->data);
  2806. sq_addref(v, &data->data);
  2807. sqlite3_set_auxdata(self->ctx, argn, data, sq_sqlite3_context_free_auxdata);
  2808. }
  2809. return 0;
  2810. }
  2811. static SQRESULT sq_sqlite3_context_result_blob(HSQUIRRELVM v)
  2812. {
  2813. SQ_FUNC_VARS_NO_TOP(v);
  2814. GET_sqlite3_context_INSTANCE();
  2815. SQ_GET_STRING(v, 2, blob);
  2816. sqlite3_result_blob(self->ctx, (const void*)blob, blob_size, SQLITE_TRANSIENT);
  2817. return 0;
  2818. }
  2819. static SQRESULT sq_sqlite3_context_result_double(HSQUIRRELVM v)
  2820. {
  2821. SQ_FUNC_VARS_NO_TOP(v);
  2822. GET_sqlite3_context_INSTANCE();
  2823. SQ_GET_FLOAT(v, 2, d);
  2824. sqlite3_result_double(self->ctx, d);
  2825. return 0;
  2826. }
  2827. static SQRESULT sq_sqlite3_context_result_error(HSQUIRRELVM v)
  2828. {
  2829. SQ_FUNC_VARS_NO_TOP(v);
  2830. GET_sqlite3_context_INSTANCE();
  2831. SQ_GET_STRING(v,2, err);
  2832. sqlite3_result_error(self->ctx, err, err_size);
  2833. return 0;
  2834. }
  2835. static SQRESULT sq_sqlite3_context_result_int(HSQUIRRELVM v)
  2836. {
  2837. SQ_FUNC_VARS_NO_TOP(v);
  2838. GET_sqlite3_context_INSTANCE();
  2839. SQ_GET_INTEGER(v, 2, i);
  2840. sqlite3_result_int(self->ctx, i);
  2841. return 0;
  2842. }
  2843. static SQRESULT sq_sqlite3_context_result_null(HSQUIRRELVM v)
  2844. {
  2845. SQ_FUNC_VARS_NO_TOP(v);
  2846. GET_sqlite3_context_INSTANCE();
  2847. sqlite3_result_null(self->ctx);
  2848. return 0;
  2849. }
  2850. static SQRESULT sq_sqlite3_context_result_text(HSQUIRRELVM v)
  2851. {
  2852. SQ_FUNC_VARS_NO_TOP(v);
  2853. GET_sqlite3_context_INSTANCE();
  2854. SQ_GET_STRING(v, 2, text);
  2855. sqlite3_result_text(self->ctx, text, text_size, SQLITE_TRANSIENT);
  2856. return 0;
  2857. }
  2858. static SQRESULT sq_sqlite3_context_result_subtype(HSQUIRRELVM v)
  2859. {
  2860. SQ_FUNC_VARS_NO_TOP(v);
  2861. GET_sqlite3_context_INSTANCE();
  2862. SQ_GET_INTEGER(v, 2, subtype);
  2863. sqlite3_result_subtype(self->ctx, subtype);
  2864. return 0;
  2865. }
  2866. /* no way to recover parameter value after function call
  2867. static SQRESULT sq_sqlite3_context_value_subtype(HSQUIRRELVM v)
  2868. {
  2869. SQ_FUNC_VARS_NO_TOP(v);
  2870. GET_sqlite3_context_INSTANCE();
  2871. SQ_GET_INTEGER(v, 2, param_idx);
  2872. sq_pushinteger(v, sqlite3_value_subtype(self->ctx, subtype));
  2873. return 1;
  2874. }
  2875. */
  2876. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_sqlite3_context_##name,nparams,tycheck}
  2877. static SQRegFunction sq_sqlite3_context_methods[] =
  2878. {
  2879. _DECL_FUNC(constructor, 1, _SC("x")),
  2880. _DECL_FUNC(user_data, 1, _SC("x")),
  2881. _DECL_FUNC(aggregate_data, -1, _SC("x.")),
  2882. _DECL_FUNC(aggregate_count, 1, _SC("x")),
  2883. _DECL_FUNC(result_null, 1, _SC("x")),
  2884. _DECL_FUNC(result_double, 2, _SC("xn")),
  2885. _DECL_FUNC(result_int, 2, _SC("xi")),
  2886. _DECL_FUNC(result_text, 2, _SC("xs")),
  2887. _DECL_FUNC(result_blob, 2, _SC("xs")),
  2888. _DECL_FUNC(result_error, 2, _SC("xs")),
  2889. _DECL_FUNC(result_subtype, 2, _SC("xi")),
  2890. //_DECL_FUNC(value_subtype, 2, _SC("xi")),
  2891. _DECL_FUNC(get_auxdata, 2, _SC("xi")),
  2892. _DECL_FUNC(set_auxdata, 3, _SC("xi.")),
  2893. _DECL_FUNC(_tostring, -1, _SC("x")),
  2894. {0,0}
  2895. };
  2896. #undef _DECL_FUNC
  2897. /*
  2898. ** callback functions used when calling registered sql functions
  2899. */
  2900. static void sqlite3_push_value(HSQUIRRELVM v, sqlite3_value *value)
  2901. {
  2902. const char *tmp_value;
  2903. switch (sqlite3_value_type(value))
  2904. {
  2905. case SQLITE_TEXT:
  2906. tmp_value = (const char*) sqlite3_value_text(value);
  2907. if(tmp_value) sq_pushstring(v, tmp_value, sqlite3_value_bytes(value));
  2908. else push_sqlite3_null(v);
  2909. break;
  2910. case SQLITE_INTEGER:
  2911. {
  2912. sqlite_int64 i64 = sqlite3_value_int64(value);
  2913. SQInteger n = (SQInteger)i64;
  2914. if (n == i64)
  2915. sq_pushinteger(v, n);
  2916. else
  2917. sq_pushstring(v, (const char*) sqlite3_value_text(value), sqlite3_value_bytes(value));
  2918. }
  2919. break;
  2920. case SQLITE_FLOAT:
  2921. sq_pushfloat(v, sqlite3_value_double(value));
  2922. break;
  2923. case SQLITE_BLOB:
  2924. tmp_value = (const char*) sqlite3_value_blob(value);
  2925. if(tmp_value) sq_pushstring(v, tmp_value, sqlite3_value_bytes(value));
  2926. else push_sqlite3_null(v);
  2927. break;
  2928. case SQLITE_NULL:
  2929. push_sqlite3_null(v);
  2930. break;
  2931. default:
  2932. /* things done properly (SQLite + SquiLu SQLite)
  2933. ** this should never happen */
  2934. push_sqlite3_null(v);
  2935. break;
  2936. }
  2937. }
  2938. //#if 0
  2939. static SQRESULT new_context_instance(HSQUIRRELVM v, sq_sqlite3_context_st **ctx)
  2940. {
  2941. sq_pushregistrytable(v);
  2942. sq_pushstring(v, sq_sqlite3_context_TAG,-1);
  2943. int rc = sq_rawget(v, -2);
  2944. sq_remove(v, -2); //remove registrytable
  2945. sq_pushroottable(v);
  2946. rc = sq_call(v, 1, SQTrue, SQFalse);
  2947. sq_remove(v, -2); //class
  2948. rc = sq_getinstanceup(v, -1, (void**)ctx, (void*)sq_sqlite3_context_TAG);
  2949. return rc;
  2950. }
  2951. /* scalar function to be called
  2952. ** callback params: context, values... */
  2953. static void db_sql_normal_function(sqlite3_context *context, int argc, sqlite3_value **argv)
  2954. {
  2955. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(context);
  2956. HSQUIRRELVM v = func->sdb->v;
  2957. int n;
  2958. sq_sqlite3_context_st *ctx;
  2959. int top = sq_gettop(v);
  2960. /* ensure there is enough space in the stack */
  2961. sq_reservestack(v, argc + 5);
  2962. sq_pushobject(v, func->fn_step);
  2963. sq_pushroottable(v);
  2964. if (!sq_isclosure(func->fn_finalize))
  2965. {
  2966. if(new_context_instance(v, &ctx) != SQ_OK)
  2967. sqlite3_result_error(context, sq_getlasterror_str(v), -1);
  2968. }
  2969. else
  2970. {
  2971. /* reuse context userdata value */
  2972. void *p = sqlite3_aggregate_context(context, 1);
  2973. /* i think it is OK to use assume that using a light user data
  2974. ** as an entry on SquiLu REGISTRY table will be unique */
  2975. sq_pushregistrytable(v);
  2976. sq_pushuserpointer(v, p);
  2977. /* context table */
  2978. if(sq_rawget(v, -2) != SQ_OK)
  2979. {
  2980. /* not yet created? */
  2981. sq_poptop(v); //remove null
  2982. new_context_instance(v, &ctx);
  2983. sq_pushuserpointer(v, p);
  2984. sq_push(v, -2);
  2985. sq_rawset(v, -4); //insert into registrytable
  2986. }
  2987. sq_remove(v, -2); //remove registrytable
  2988. }
  2989. /* push params */
  2990. for (n = 0; n < argc; ++n)
  2991. {
  2992. sqlite3_push_value(v, argv[n]);
  2993. }
  2994. /* set context */
  2995. ctx->ctx = context;
  2996. if (sq_call(v, argc + 2, SQFalse, SQFalse) != SQ_OK) //2 = roottable + ctx
  2997. {
  2998. sqlite3_result_error(context, sq_getlasterror_str(v), -1);
  2999. }
  3000. sq_settop(v, top);
  3001. }
  3002. static void db_sql_finalize_function(sqlite3_context *context)
  3003. {
  3004. sq_sqlite3_sdb_func *func = (sq_sqlite3_sdb_func*)sqlite3_user_data(context);
  3005. HSQUIRRELVM v = func->sdb->v;
  3006. void *p = sqlite3_aggregate_context(context, 1); /* minimal mem usage */
  3007. sq_sqlite3_context_st *ctx;
  3008. int top = sq_gettop(v);
  3009. sq_pushobject(v, func->fn_finalize);
  3010. sq_pushroottable(v);
  3011. /* i think it is OK to use assume that using a light user data
  3012. ** as an entry on SquiLu REGISTRY table will be unique */
  3013. sq_pushregistrytable(v);
  3014. sq_pushuserpointer(v, p);
  3015. /* remove it from registry but we'll use it last time here */
  3016. /* context table */
  3017. if(sq_deleteslot(v, -2, SQTrue) != SQ_OK)
  3018. {
  3019. /* not yet created? - shouldn't happen in finalize function */
  3020. sq_pop(v, 1);
  3021. new_context_instance(v, &ctx);
  3022. sq_pushuserpointer(v, p);
  3023. sq_push(v, -2);
  3024. sq_rawset(v, -4);
  3025. }
  3026. sq_remove(v, -2); //registrytable
  3027. /* set context */
  3028. ctx->ctx = context;
  3029. if (sq_call(v, 1, SQFalse, SQFalse) != SQ_OK)
  3030. {
  3031. sqlite3_result_error(context, sq_getlasterror_str(v), -1);
  3032. }
  3033. sq_settop(v, top);
  3034. }
  3035. /*
  3036. ** Register a normal function
  3037. ** Params: db, function name, number arguments, [ callback | step, finalize], user data
  3038. ** Returns: true on sucess
  3039. **
  3040. ** Normal function:
  3041. ** Params: context, params
  3042. **
  3043. ** Aggregate function:
  3044. ** Params of step: context, params
  3045. ** Params of finalize: context
  3046. */
  3047. #define CREATE_FUNCTION_SIMPLE 0
  3048. #define CREATE_FUNCTION_AGGREGATE 1
  3049. #define CREATE_FUNCTION_WINDOW 2
  3050. static SQRESULT db_register_function(HSQUIRRELVM v, int fcreate_type)
  3051. {
  3052. SQ_FUNC_VARS(v);
  3053. SQInteger ftype = 0;
  3054. SQBool isAggregate = fcreate_type == CREATE_FUNCTION_AGGREGATE;
  3055. GET_sqlite3_INSTANCE();
  3056. SQ_GET_STRING(v, 2, name);
  3057. SQ_GET_INTEGER(v, 3, nargs);
  3058. if(sq_gettype(v, 4) != OT_CLOSURE)
  3059. return sq_throwerror(v, "invalid parameter 3 expected closure");
  3060. if (isAggregate)
  3061. {
  3062. if(sq_gettype(v,5) != OT_CLOSURE)
  3063. return sq_throwerror(v, "invalid parameter 4 expected closure");
  3064. } else {
  3065. sq_optinteger(v, 5, &ftype, 0);
  3066. }
  3067. sq_sqlite3_sdb_func *func;
  3068. /* maybe an alternative way to allocate memory should be used/avoided */
  3069. func = (sq_sqlite3_sdb_func*)sq_malloc(sizeof(sq_sqlite3_sdb_func));
  3070. memset(func, 0, sizeof(sq_sqlite3_sdb_func));
  3071. _rc_ = sqlite3_create_function(
  3072. self, name, nargs, SQLITE_UTF8 | (ftype == SQLITE_DETERMINISTIC ? ftype : 0),
  3073. func,
  3074. isAggregate ? NULL : db_sql_normal_function,
  3075. isAggregate ? db_sql_normal_function : NULL,
  3076. isAggregate ? db_sql_finalize_function : NULL
  3077. );
  3078. if (_rc_ == SQLITE_OK)
  3079. {
  3080. /* save registered function in db function list */
  3081. func->sdb = sdb;
  3082. func->next = sdb->func;
  3083. sdb->func = func;
  3084. /* save the setp/normal function callback */
  3085. sq_resetobject(&func->fn_step);
  3086. sq_getstackobj(v, 4, &func->fn_step);
  3087. sq_addref(v, &func->fn_step);
  3088. /* save the finalize function callback */
  3089. sq_resetobject(&func->fn_finalize);
  3090. if (isAggregate)
  3091. {
  3092. sq_getstackobj(v, 5, &func->fn_finalize);
  3093. sq_addref(v, &func->fn_finalize);
  3094. }
  3095. /* save user data */
  3096. sq_resetobject(&func->udata);
  3097. int udata_idx = isAggregate ? 6 : 5;
  3098. if(_top_ >= udata_idx)
  3099. {
  3100. sq_getstackobj(v, udata_idx, &func->udata);
  3101. sq_addref(v, &func->udata);
  3102. }
  3103. }
  3104. else
  3105. {
  3106. /* free allocated memory */
  3107. sq_free(func, sizeof(sq_sqlite3_sdb_func));
  3108. }
  3109. sq_pushbool(v, _rc_ == SQLITE_OK ? 1 : 0);
  3110. return 1;
  3111. }
  3112. //#endif
  3113. static SQRESULT sq_sqlite3_create_function(HSQUIRRELVM v)
  3114. {
  3115. return db_register_function(v, CREATE_FUNCTION_SIMPLE);
  3116. }
  3117. static SQRESULT sq_sqlite3_create_aggregate(HSQUIRRELVM v)
  3118. {
  3119. return db_register_function(v, CREATE_FUNCTION_AGGREGATE);
  3120. }
  3121. /*
  3122. static SQRESULT sq_sqlite3_create_window_function(HSQUIRRELVM v)
  3123. {
  3124. return db_register_function(v, CREATE_FUNCTION_WINDOW);
  3125. }
  3126. */
  3127. static SQRESULT sq_sqlite3_backup(HSQUIRRELVM v)
  3128. {
  3129. SQ_FUNC_VARS(v);
  3130. GET_sqlite3_INSTANCE();
  3131. SQ_GET_STRING(v, 2, fname);
  3132. SQ_OPT_STRING(v, 3, dbSrcName, _SC("main"));
  3133. SQ_OPT_STRING(v, 4, dbDestName, _SC("main"));
  3134. sqlite3 *pDest;
  3135. sqlite3_backup *pBackup;
  3136. int rc = sqlite3_open(fname, &pDest);
  3137. if(rc!=SQLITE_OK)
  3138. {
  3139. sqlite3_close(pDest);
  3140. return sq_throwerror(v, _SC("SQLite can't open %s"), fname);
  3141. }
  3142. pBackup = sqlite3_backup_init(pDest, dbDestName, self, dbSrcName);
  3143. if( pBackup==0 ){
  3144. rc = sq_throwerror(v, _SC("Error: %s\n"), sqlite3_errmsg(pDest));
  3145. sqlite3_close(pDest);
  3146. }
  3147. while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
  3148. sqlite3_backup_finish(pBackup);
  3149. if( rc==SQLITE_DONE ){
  3150. rc = SQ_OK;
  3151. }else{
  3152. rc = sq_throwerror(v, _SC("Error: %s\n"), sqlite3_errmsg(pDest));
  3153. }
  3154. sqlite3_close(pDest);
  3155. return rc;
  3156. }
  3157. static SQRESULT sq_sqlite3_restore(HSQUIRRELVM v)
  3158. {
  3159. SQ_FUNC_VARS(v);
  3160. GET_sqlite3_INSTANCE();
  3161. SQ_GET_STRING(v, 2, fname);
  3162. SQ_OPT_STRING(v, 3, dbSrcName, _SC("main"));
  3163. SQ_OPT_STRING(v, 4, dbDestName, _SC("main"));
  3164. sqlite3 *pSrc;
  3165. sqlite3_backup *pBackup;
  3166. int nTimeout = 0;
  3167. int rc = sqlite3_open(fname, &pSrc);
  3168. if(rc!=SQLITE_OK)
  3169. {
  3170. sqlite3_close(pSrc);
  3171. return sq_throwerror(v, _SC("SQLite can't open %s"), fname);
  3172. }
  3173. pBackup = sqlite3_backup_init(self, dbDestName, pSrc, dbSrcName);
  3174. if( pBackup==0 ){
  3175. sqlite3_close(pSrc);
  3176. return sq_throwerror(v, _SC("Error: %s\n"), sqlite3_errmsg(self));
  3177. }
  3178. while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
  3179. || rc==SQLITE_BUSY ){
  3180. if( rc==SQLITE_BUSY ){
  3181. if( nTimeout++ >= 3 ) break;
  3182. sqlite3_sleep(100);
  3183. }
  3184. }
  3185. sqlite3_backup_finish(pBackup);
  3186. if( rc==SQLITE_DONE ){
  3187. rc = SQ_OK;
  3188. }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
  3189. rc = sq_throwerror(v, _SC("Error: source database is busy\n"));
  3190. }else{
  3191. rc = sq_throwerror(v, _SC("Error: %s\n"), sqlite3_errmsg(self));
  3192. }
  3193. sqlite3_close(pSrc);
  3194. return rc;
  3195. }
  3196. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  3197. static SQRESULT sq_sqlite3_enable_load_extension(HSQUIRRELVM v)
  3198. {
  3199. SQ_FUNC_VARS_NO_TOP(v);
  3200. GET_sqlite3_INSTANCE();
  3201. SQ_GET_BOOL(v, 2, onOff);
  3202. sqlite3_enable_load_extension(self, onOff);
  3203. return 0;
  3204. }
  3205. #endif
  3206. #if 0
  3207. /**
  3208. The number/order of fields should be the same to use this function
  3209. it'll save temporary data convertion.
  3210. */
  3211. static SQRESULT sq_sqlite3_exec_loop(HSQUIRRELVM v)
  3212. {
  3213. SQ_FUNC_VARS(v);
  3214. GET_sqlite3_INSTANCE();
  3215. SQ_GET_STRING(v, 2, outer_sql);
  3216. SQ_GET_STRING(v, 3, inner1_sql);
  3217. SQ_OPT_STRING(v, 4, inner2_sql, NULL);
  3218. sqlite3_stmt *stmt_outer = NULL, *stmt_inner1 = NULL, *stmt_inner2 = NULL;
  3219. int rc = sqlite3_prepare_v2(self, outer_sql, outer_sql_size, &stmt_outer, NULL);
  3220. if(rc == SQLITE_OK)
  3221. {
  3222. rc = sqlite3_prepare_v2(self, inner1_sql, inner1_sql_size, &stmt_inner1, NULL);
  3223. if(rc == SQLITE_OK)
  3224. {
  3225. if(inner2_sql)
  3226. {
  3227. rc = sqlite3_prepare_v2(self, inner2_sql, inner2_sql_size, &stmt_inner2, NULL);
  3228. }
  3229. if(rc == SQLITE_OK)
  3230. {
  3231. int outer_col_count = sqlite3_column_count(stmt_outer);
  3232. int inner1_param_count = sqlite3_bind_parameter_count(stmt_inner1);
  3233. int inner2_param_count = stmt_inner2 ? sqlite3_bind_parameter_count(stmt_inner2) : -1;
  3234. if( (inner1_param_count <= outer_col_count) && ((inner2_param_count > 0)
  3235. && (inner2_param_count <= outer_col_count) ) )
  3236. {
  3237. while(sqlite3_step(stmt_outer) == SQLITE_ROW)
  3238. {
  3239. for(int i=0; i < inner1_param_count; ++i)
  3240. {
  3241. rc = sqlite3_bind_value(stmt_inner1, i+1, sqlite3_column_value(stmt_outer, i));
  3242. if(rc != SQLITE_OK) break;
  3243. }
  3244. if(rc == SQLITE_OK)
  3245. {
  3246. rc = sqlite3_step(stmt_inner1);
  3247. sqlite3_reset(stmt_inner1);
  3248. }
  3249. if(rc != SQLITE_DONE) break;
  3250. if((inner2_param_count > 0) && (sqlite3_changes(self) == 0))
  3251. {
  3252. for(int i=0; i < inner2_param_count; ++i)
  3253. {
  3254. rc = sqlite3_bind_value(stmt_inner2, i+1, sqlite3_column_value(stmt_outer, i));
  3255. if(rc != SQLITE_OK) break;
  3256. }
  3257. if(rc == SQLITE_OK)
  3258. {
  3259. rc = sqlite3_step(stmt_inner2);
  3260. sqlite3_reset(stmt_inner2);
  3261. }
  3262. if(rc != SQLITE_DONE) break;
  3263. }
  3264. }
  3265. }
  3266. else
  3267. {
  3268. return sq_throwerror(v, _SC("bind parameter count mismatch expect %d : %d but got outer %d"),
  3269. inner1_param_count, inner2_param_count, outer_col_count);
  3270. }
  3271. }
  3272. }
  3273. }
  3274. if(stmt_outer) sqlite3_finalize(stmt_outer);
  3275. if(stmt_inner1) sqlite3_finalize(stmt_inner1);
  3276. if(stmt_inner2) sqlite3_finalize(stmt_inner2);
  3277. sq_pushinteger(v, rc);
  3278. return 1;
  3279. }
  3280. #endif // 0
  3281. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sq_sqlite3_##name,nparams,tycheck}
  3282. static SQRegFunction sq_sqlite3_methods[] =
  3283. {
  3284. _DECL_FUNC(constructor, -2, _SC("xsi")),
  3285. _DECL_FUNC(close, 1, _SC("x")),
  3286. _DECL_FUNC(db_ptr, 1, _SC("x")),
  3287. _DECL_FUNC(IsAutoCommitOn, 1, _SC("x")),
  3288. _DECL_FUNC(version, 1, _SC("x|y")),
  3289. _DECL_FUNC(threadsafe, 1, _SC("x|y")),
  3290. _DECL_FUNC(config_single_thread, 1, _SC("y")),
  3291. _DECL_FUNC(config_multi_thread, 1, _SC("y")),
  3292. _DECL_FUNC(config_serialized, 1, _SC("y")),
  3293. _DECL_FUNC(errcode, 1, _SC("x")),
  3294. _DECL_FUNC(errmsg, 1, _SC("x")),
  3295. _DECL_FUNC(sleep, 1, _SC("x")),
  3296. _DECL_FUNC(interrupt, 1, _SC("x")),
  3297. _DECL_FUNC(progress_handler, -2, _SC("x i|o c .")),
  3298. _DECL_FUNC(trace, -2, _SC("x c|o .")),
  3299. _DECL_FUNC(update_hook, -2, _SC("x c|o .")),
  3300. _DECL_FUNC(set_authorizer, -2, _SC("x c|o .")),
  3301. _DECL_FUNC(busy_handler, -2, _SC("x c|o .")),
  3302. _DECL_FUNC(busy_timeout, 2, _SC("xi")),
  3303. _DECL_FUNC(create_function, -4, _SC("xsic.")),
  3304. _DECL_FUNC(create_aggregate, -5, _SC("xsicc.")),
  3305. // _DECL_FUNC(create_window_function, -6, _SC("xsiccc.")),
  3306. #ifndef WIN32
  3307. _DECL_FUNC(temp_directory, 2, _SC("xs")),
  3308. #endif
  3309. _DECL_FUNC(enable_shared_cache, 2, _SC("xb")),
  3310. _DECL_FUNC(changes, 1, _SC("x")),
  3311. _DECL_FUNC(exec, -2, _SC("x s|x")),
  3312. _DECL_FUNC(exec_dml, 2, _SC("xs")),
  3313. _DECL_FUNC(exec_one_dml, -2, _SC("x s|x")),
  3314. _DECL_FUNC(exec_get_first_row, -2, _SC("x s|x")),
  3315. _DECL_FUNC(exec_get_one, -2, _SC("x s|x")),
  3316. //_DECL_FUNC(exec_loop, -3, _SC("x sss")),
  3317. _DECL_FUNC(get_db_name, 1, _SC("x")),
  3318. _DECL_FUNC(last_row_id, 1, _SC("x")),
  3319. _DECL_FUNC(prepare, 2, _SC("xs")),
  3320. _DECL_FUNC(stmt_scope_reset, 2, _SC("xx")),
  3321. _DECL_FUNC(transaction, 1, _SC("x")),
  3322. _DECL_FUNC(blob_open, -5, _SC("xsssii")),
  3323. _DECL_FUNC(set_busy_timeout, -1, _SC("xi")),
  3324. _DECL_FUNC(total_changes, 1, _SC("x")),
  3325. _DECL_FUNC(backup, -2, _SC("xsss")),
  3326. _DECL_FUNC(restore, -2, _SC("xsss")),
  3327. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  3328. _DECL_FUNC(enable_load_extension, 2, _SC("xb")),
  3329. #endif
  3330. #ifdef SQLITE_HAS_CODEC
  3331. _DECL_FUNC(key, 2, _SC("xs")),
  3332. _DECL_FUNC(rekey, 2, _SC("xs")),
  3333. #endif
  3334. {0,0}
  3335. };
  3336. #undef _DECL_FUNC
  3337. #define INT_CONST(v,num) sq_pushstring(v,_SC(#num),-1);sq_pushinteger(v,num);sq_newslot(v,-3,SQTrue);
  3338. #ifdef __cplusplus
  3339. extern "C" {
  3340. #endif
  3341. SQRESULT sqext_register_SQLite3(HSQUIRRELVM v)
  3342. {
  3343. sq_insertfunc(v, _SC("sle2vecOfvec"), sq_sle2vecOfvec, -2, _SC(". s|x a i"), SQTrue);
  3344. sq_insertfunc(v, _SC("get_sle_size"), sq_get_sle_size, -2, _SC(".i"), SQTrue);
  3345. sq_insertfunc(v, _SC("add2sle"), sq_add2sle, -3, _SC(".xsi"), SQTrue);
  3346. sq_pushconsttable(v);
  3347. #define INT_SLE_CONST(num) sq_pushstring(v,_SC("SLE_" #num),-1);sq_pushinteger(v,num);sq_newslot(v,-3,SQTrue);
  3348. INT_SLE_CONST(IBYTE1);
  3349. INT_SLE_CONST(IBYTE2);
  3350. INT_SLE_CONST(IBYTE3);
  3351. INT_SLE_CONST(IBYTE4);
  3352. INT_SLE_CONST(IBYTE_MAX);
  3353. INT_SLE_CONST(SLEMARK);
  3354. INT_SLE_CONST(SLEEND);
  3355. sq_poptop(v); //remove const table
  3356. HSQOBJECT sqlite3_NULL;
  3357. sq_pushregistrytable(v);
  3358. sq_pushstring(v, sqlite3_NULL_Name,-1);
  3359. sq_newuserdata(v, sizeof(void*));
  3360. sq_settypetag(v, -1, (SQUserPointer)sqlite3_NULL_Name);
  3361. sq_resetobject(&sqlite3_NULL);
  3362. sq_getstackobj(v, -1, &sqlite3_NULL);
  3363. sq_newtable(v);
  3364. sq_insert_reg_funcs(v, sqlite3_NULL_methods);
  3365. sq_setdelegate(v, -2);
  3366. sq_newslot(v,-3,SQTrue);
  3367. sq_pushstring(v,sq_sqlite3_context_TAG,-1);
  3368. sq_newclass(v,SQFalse);
  3369. sq_settypetag(v,-1,(SQUserPointer)sq_sqlite3_context_TAG);
  3370. sq_insert_reg_funcs(v, sq_sqlite3_context_methods);
  3371. sq_newslot(v,-3,SQTrue);
  3372. sq_pushstring(v,SQLite3_Blob_TAG,-1);
  3373. sq_newclass(v,SQFalse);
  3374. sq_settypetag(v,-1,(SQUserPointer)SQLite3_Blob_TAG);
  3375. sq_insert_reg_funcs(v, sq_sqlite3_blob_methods);
  3376. sq_newslot(v,-3,SQTrue);
  3377. sq_poptop(v); //remove registrytable
  3378. sq_pushstring(v,SQLite3_TAG,-1);
  3379. sq_newclass(v,SQFalse);
  3380. sq_settypetag(v,-1,(SQUserPointer)SQLite3_TAG);
  3381. sq_insert_reg_funcs(v, sq_sqlite3_methods);
  3382. INT_CONST(v,SQLITE_OPEN_CREATE);
  3383. INT_CONST(v,SQLITE_OPEN_READWRITE);
  3384. INT_CONST(v,SQLITE_OPEN_SHAREDCACHE);
  3385. INT_CONST(v,SQLITE_OPEN_SUBLATIN_NA_LIKE);
  3386. INT_CONST(v,SQLITE_OK);
  3387. INT_CONST(v,SQLITE_BUSY);
  3388. INT_CONST(v,SQLITE_DONE);
  3389. INT_CONST(v,SQLITE_DETERMINISTIC);
  3390. INT_CONST(v,SQLITE_ERROR);
  3391. INT_CONST(v,SQLITE_INTERRUPT);
  3392. INT_CONST(v,SQLITE_MISUSE);
  3393. INT_CONST(v,SQLITE_ROW);
  3394. INT_CONST(v,SQLITE_LOCKED_SHAREDCACHE);
  3395. INT_CONST(v,SQLITE_BUSY_RECOVERY);
  3396. INT_CONST(v,SQLITE_BUSY_SNAPSHOT);
  3397. INT_CONST(v,SQLITE_CANTOPEN_NOTEMPDIR);
  3398. INT_CONST(v,SQLITE_CANTOPEN_ISDIR);
  3399. INT_CONST(v,SQLITE_CANTOPEN_FULLPATH);
  3400. INT_CONST(v,SQLITE_CANTOPEN_CONVPATH);
  3401. INT_CONST(v,SQLITE_CORRUPT_VTAB);
  3402. INT_CONST(v,SQLITE_READONLY_RECOVERY);
  3403. INT_CONST(v,SQLITE_READONLY_CANTLOCK);
  3404. INT_CONST(v,SQLITE_READONLY_ROLLBACK);
  3405. INT_CONST(v,SQLITE_READONLY_DBMOVED);
  3406. INT_CONST(v,SQLITE_ABORT_ROLLBACK);
  3407. INT_CONST(v,SQLITE_CONSTRAINT_CHECK);
  3408. INT_CONST(v,SQLITE_CONSTRAINT_COMMITHOOK);
  3409. INT_CONST(v,SQLITE_CONSTRAINT_FOREIGNKEY);
  3410. INT_CONST(v,SQLITE_CONSTRAINT_FUNCTION);
  3411. INT_CONST(v,SQLITE_CONSTRAINT_NOTNULL);
  3412. INT_CONST(v,SQLITE_CONSTRAINT_PRIMARYKEY);
  3413. INT_CONST(v,SQLITE_CONSTRAINT_TRIGGER);
  3414. INT_CONST(v,SQLITE_CONSTRAINT_UNIQUE);
  3415. INT_CONST(v,SQLITE_CONSTRAINT_VTAB);
  3416. INT_CONST(v,SQLITE_CONSTRAINT_ROWID);
  3417. INT_CONST(v,SQLITE_NOTICE_RECOVER_WAL);
  3418. INT_CONST(v,SQLITE_NOTICE_RECOVER_ROLLBACK);
  3419. INT_CONST(v,SQLITE_WARNING_AUTOINDEX);
  3420. /*Authorizer codes*/
  3421. INT_CONST(v, SQLITE_IGNORE);
  3422. INT_CONST(v, SQLITE_DENY);
  3423. INT_CONST(v, SQLITE_CREATE_INDEX);
  3424. INT_CONST(v, SQLITE_CREATE_TABLE);
  3425. INT_CONST(v, SQLITE_CREATE_TEMP_INDEX);
  3426. INT_CONST(v, SQLITE_CREATE_TEMP_TABLE);
  3427. INT_CONST(v, SQLITE_CREATE_TEMP_TRIGGER);
  3428. INT_CONST(v, SQLITE_CREATE_TEMP_VIEW);
  3429. INT_CONST(v, SQLITE_CREATE_TRIGGER);
  3430. INT_CONST(v, SQLITE_CREATE_VIEW);
  3431. INT_CONST(v, SQLITE_DELETE);
  3432. INT_CONST(v, SQLITE_DROP_INDEX);
  3433. INT_CONST(v, SQLITE_DROP_TABLE);
  3434. INT_CONST(v, SQLITE_DROP_TEMP_INDEX);
  3435. INT_CONST(v, SQLITE_DROP_TEMP_TABLE);
  3436. INT_CONST(v, SQLITE_DROP_TEMP_TRIGGER);
  3437. INT_CONST(v, SQLITE_DROP_TEMP_VIEW);
  3438. INT_CONST(v, SQLITE_DROP_TRIGGER);
  3439. INT_CONST(v, SQLITE_DROP_VIEW);
  3440. INT_CONST(v, SQLITE_INSERT);
  3441. INT_CONST(v, SQLITE_PRAGMA);
  3442. INT_CONST(v, SQLITE_READ);
  3443. INT_CONST(v, SQLITE_SELECT);
  3444. INT_CONST(v, SQLITE_TRANSACTION);
  3445. INT_CONST(v, SQLITE_UPDATE);
  3446. INT_CONST(v, SQLITE_ATTACH);
  3447. INT_CONST(v, SQLITE_DETACH);
  3448. INT_CONST(v, SQLITE_ALTER_TABLE);
  3449. INT_CONST(v, SQLITE_REINDEX);
  3450. INT_CONST(v, SQLITE_ANALYZE);
  3451. INT_CONST(v, SQLITE_CREATE_VTABLE);
  3452. INT_CONST(v, SQLITE_DROP_VTABLE);
  3453. INT_CONST(v, SQLITE_FUNCTION);
  3454. INT_CONST(v, SQLITE_SAVEPOINT);
  3455. INT_CONST(v, SQLITE_COPY);
  3456. INT_CONST(v, SQLITE_RECURSIVE);
  3457. //push sqlite3_NULL as a member
  3458. sq_pushstring(v, nullName,-1);
  3459. sq_pushobject(v, sqlite3_NULL);
  3460. sq_newslot(v,-3,SQTrue);
  3461. sq_newslot(v,-3,SQTrue); //insert SQLite3 on root table
  3462. #ifdef SQLITE_ENABLE_SESSION
  3463. sq_pushstring(v, SQLite3_Session_TAG,-1);
  3464. sq_newclass(v,SQFalse);
  3465. sq_settypetag(v,-1,(SQUserPointer)SQLite3_Session_TAG);
  3466. sq_insert_reg_funcs(v, sq_sqlite3_session_methods);
  3467. sq_newslot(v,-3,SQTrue);
  3468. sq_pushstring(v, SQLite3_Session_Iterator_TAG,-1);
  3469. sq_newclass(v,SQFalse);
  3470. sq_settypetag(v,-1,(SQUserPointer)SQLite3_Session_Iterator_TAG);
  3471. sq_insert_reg_funcs(v, sq_sqlite3_session_iterator_methods);
  3472. sq_newslot(v,-3,SQTrue); //insert SQLiteSession in root table
  3473. #endif // SQLITE_ENABLE_SESSION
  3474. sq_pushstring(v, SQLite3_Stmt_TAG,-1);
  3475. sq_newclass(v,SQFalse);
  3476. sq_settypetag(v,-1,(SQUserPointer)SQLite3_Stmt_TAG);
  3477. sq_insert_reg_funcs(v, sq_sqlite3_stmt_methods);
  3478. INT_CONST(v,SQLITE_INTEGER);
  3479. INT_CONST(v,SQLITE_FLOAT);
  3480. INT_CONST(v,SQLITE_NULL);
  3481. INT_CONST(v,SQLITE_TEXT);
  3482. INT_CONST(v,SQLITE_BLOB);
  3483. INT_CONST(v,SQLITE_OK);
  3484. INT_CONST(v,SQLITE_DONE);
  3485. INT_CONST(v,SQLITE_ROW);
  3486. INT_CONST(v,AS_STRING_ALWAYS);
  3487. INT_CONST(v,NULL_AS_EMPTY_STR);
  3488. INT_CONST(v,WITH_COL_NAMES);
  3489. INT_CONST(v,WITH_BLOB_AS_NULL);
  3490. INT_CONST(v,WITH_SQ_NULL);
  3491. //push sqlite3_NULL as a member
  3492. sq_pushstring(v, nullName,-1);
  3493. sq_pushobject(v, sqlite3_NULL);
  3494. sq_newslot(v,-3,SQTrue);
  3495. sq_newslot(v,-3,SQTrue); //insert SQLiteStatement in root table
  3496. sq_pushstring(v,SQLite3_Transaction_TAG,-1);
  3497. sq_newclass(v,SQFalse);
  3498. sq_settypetag(v,-1,(SQUserPointer)SQLite3_Transaction_TAG);
  3499. sq_insert_reg_funcs(v, sq_sqlite3_transaction_methods);
  3500. sq_newslot(v,-3,SQTrue);//insert SQLiteTransactio in root table
  3501. sq_pushstring(v,SQLite3_StmtScopeReset_TAG,-1);
  3502. sq_newclass(v,SQFalse);
  3503. sq_settypetag(v,-1,(SQUserPointer)SQLite3_StmtScopeReset_TAG);
  3504. sq_insert_reg_funcs(v, sq_sqlite3_stmt_scope_reset_methods);
  3505. sq_newslot(v,-3,SQTrue);//insert SQLiteStmtScopeReset in root table
  3506. return 0;
  3507. }
  3508. #ifdef __cplusplus
  3509. }
  3510. #endif