sq_sqlite3.cpp 111 KB

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