test1.c 188 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549
  1. /*
  2. ** 2001 September 15
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. ** Code for testing all sorts of SQLite interfaces. This code
  13. ** is not included in the SQLite library. It is used for automated
  14. ** testing of the SQLite library.
  15. */
  16. #include "sqliteInt.h"
  17. #include "vdbeInt.h"
  18. #include "tcl.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. /*
  22. ** This is a copy of the first part of the SqliteDb structure in
  23. ** tclsqlite.c. We need it here so that the get_sqlite_pointer routine
  24. ** can extract the sqlite3* pointer from an existing Tcl SQLite
  25. ** connection.
  26. */
  27. struct SqliteDb {
  28. sqlite3 *db;
  29. };
  30. /*
  31. ** Convert text generated by the "%p" conversion format back into
  32. ** a pointer.
  33. */
  34. static int testHexToInt(int h){
  35. if( h>='0' && h<='9' ){
  36. return h - '0';
  37. }else if( h>='a' && h<='f' ){
  38. return h - 'a' + 10;
  39. }else{
  40. assert( h>='A' && h<='F' );
  41. return h - 'A' + 10;
  42. }
  43. }
  44. void *sqlite3TestTextToPtr(const char *z){
  45. void *p;
  46. u64 v;
  47. u32 v2;
  48. if( z[0]=='0' && z[1]=='x' ){
  49. z += 2;
  50. }
  51. v = 0;
  52. while( *z ){
  53. v = (v<<4) + testHexToInt(*z);
  54. z++;
  55. }
  56. if( sizeof(p)==sizeof(v) ){
  57. memcpy(&p, &v, sizeof(p));
  58. }else{
  59. assert( sizeof(p)==sizeof(v2) );
  60. v2 = (u32)v;
  61. memcpy(&p, &v2, sizeof(p));
  62. }
  63. return p;
  64. }
  65. /*
  66. ** A TCL command that returns the address of the sqlite* pointer
  67. ** for an sqlite connection instance. Bad things happen if the
  68. ** input is not an sqlite connection.
  69. */
  70. static int get_sqlite_pointer(
  71. void * clientData,
  72. Tcl_Interp *interp,
  73. int objc,
  74. Tcl_Obj *CONST objv[]
  75. ){
  76. struct SqliteDb *p;
  77. Tcl_CmdInfo cmdInfo;
  78. char zBuf[100];
  79. if( objc!=2 ){
  80. Tcl_WrongNumArgs(interp, 1, objv, "SQLITE-CONNECTION");
  81. return TCL_ERROR;
  82. }
  83. if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
  84. Tcl_AppendResult(interp, "command not found: ",
  85. Tcl_GetString(objv[1]), (char*)0);
  86. return TCL_ERROR;
  87. }
  88. p = (struct SqliteDb*)cmdInfo.objClientData;
  89. sprintf(zBuf, "%p", p->db);
  90. if( strncmp(zBuf,"0x",2) ){
  91. sprintf(zBuf, "0x%p", p->db);
  92. }
  93. Tcl_AppendResult(interp, zBuf, 0);
  94. return TCL_OK;
  95. }
  96. /*
  97. ** Decode a pointer to an sqlite3 object.
  98. */
  99. int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
  100. struct SqliteDb *p;
  101. Tcl_CmdInfo cmdInfo;
  102. if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){
  103. p = (struct SqliteDb*)cmdInfo.objClientData;
  104. *ppDb = p->db;
  105. }else{
  106. *ppDb = (sqlite3*)sqlite3TestTextToPtr(zA);
  107. }
  108. return TCL_OK;
  109. }
  110. extern const char *sqlite3ErrName(int);
  111. #define t1ErrorName sqlite3ErrName
  112. /*
  113. ** Convert an sqlite3_stmt* into an sqlite3*. This depends on the
  114. ** fact that the sqlite3* is the first field in the Vdbe structure.
  115. */
  116. #define StmtToDb(X) sqlite3_db_handle(X)
  117. /*
  118. ** Check a return value to make sure it agrees with the results
  119. ** from sqlite3_errcode.
  120. */
  121. int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){
  122. if( sqlite3_threadsafe()==0 && rc!=SQLITE_MISUSE && rc!=SQLITE_OK
  123. && sqlite3_errcode(db)!=rc ){
  124. char zBuf[200];
  125. int r2 = sqlite3_errcode(db);
  126. sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)",
  127. t1ErrorName(rc), rc, t1ErrorName(r2), r2);
  128. Tcl_ResetResult(interp);
  129. Tcl_AppendResult(interp, zBuf, 0);
  130. return 1;
  131. }
  132. return 0;
  133. }
  134. /*
  135. ** Decode a pointer to an sqlite3_stmt object.
  136. */
  137. static int getStmtPointer(
  138. Tcl_Interp *interp,
  139. const char *zArg,
  140. sqlite3_stmt **ppStmt
  141. ){
  142. *ppStmt = (sqlite3_stmt*)sqlite3TestTextToPtr(zArg);
  143. return TCL_OK;
  144. }
  145. /*
  146. ** Generate a text representation of a pointer that can be understood
  147. ** by the getDbPointer and getVmPointer routines above.
  148. **
  149. ** The problem is, on some machines (Solaris) if you do a printf with
  150. ** "%p" you cannot turn around and do a scanf with the same "%p" and
  151. ** get your pointer back. You have to prepend a "0x" before it will
  152. ** work. Or at least that is what is reported to me (drh). But this
  153. ** behavior varies from machine to machine. The solution used her is
  154. ** to test the string right after it is generated to see if it can be
  155. ** understood by scanf, and if not, try prepending an "0x" to see if
  156. ** that helps. If nothing works, a fatal error is generated.
  157. */
  158. int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p){
  159. sqlite3_snprintf(100, zPtr, "%p", p);
  160. return TCL_OK;
  161. }
  162. /*
  163. ** The callback routine for sqlite3_exec_printf().
  164. */
  165. static int exec_printf_cb(void *pArg, int argc, char **argv, char **name){
  166. Tcl_DString *str = (Tcl_DString*)pArg;
  167. int i;
  168. if( Tcl_DStringLength(str)==0 ){
  169. for(i=0; i<argc; i++){
  170. Tcl_DStringAppendElement(str, name[i] ? name[i] : "NULL");
  171. }
  172. }
  173. for(i=0; i<argc; i++){
  174. Tcl_DStringAppendElement(str, argv[i] ? argv[i] : "NULL");
  175. }
  176. return 0;
  177. }
  178. /*
  179. ** The I/O tracing callback.
  180. */
  181. #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
  182. static FILE *iotrace_file = 0;
  183. static void io_trace_callback(const char *zFormat, ...){
  184. va_list ap;
  185. va_start(ap, zFormat);
  186. vfprintf(iotrace_file, zFormat, ap);
  187. va_end(ap);
  188. fflush(iotrace_file);
  189. }
  190. #endif
  191. /*
  192. ** Usage: io_trace FILENAME
  193. **
  194. ** Turn I/O tracing on or off. If FILENAME is not an empty string,
  195. ** I/O tracing begins going into FILENAME. If FILENAME is an empty
  196. ** string, I/O tracing is turned off.
  197. */
  198. static int test_io_trace(
  199. void *NotUsed,
  200. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  201. int argc, /* Number of arguments */
  202. char **argv /* Text of each argument */
  203. ){
  204. #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
  205. if( argc!=2 ){
  206. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  207. " FILENAME\"", 0);
  208. return TCL_ERROR;
  209. }
  210. if( iotrace_file ){
  211. if( iotrace_file!=stdout && iotrace_file!=stderr ){
  212. fclose(iotrace_file);
  213. }
  214. iotrace_file = 0;
  215. sqlite3IoTrace = 0;
  216. }
  217. if( argv[1][0] ){
  218. if( strcmp(argv[1],"stdout")==0 ){
  219. iotrace_file = stdout;
  220. }else if( strcmp(argv[1],"stderr")==0 ){
  221. iotrace_file = stderr;
  222. }else{
  223. iotrace_file = fopen(argv[1], "w");
  224. }
  225. sqlite3IoTrace = io_trace_callback;
  226. }
  227. #endif
  228. return TCL_OK;
  229. }
  230. /*
  231. ** Usage: sqlite3_exec_printf DB FORMAT STRING
  232. **
  233. ** Invoke the sqlite3_exec_printf() interface using the open database
  234. ** DB. The SQL is the string FORMAT. The format string should contain
  235. ** one %s or %q. STRING is the value inserted into %s or %q.
  236. */
  237. static int test_exec_printf(
  238. void *NotUsed,
  239. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  240. int argc, /* Number of arguments */
  241. char **argv /* Text of each argument */
  242. ){
  243. sqlite3 *db;
  244. Tcl_DString str;
  245. int rc;
  246. char *zErr = 0;
  247. char *zSql;
  248. char zBuf[30];
  249. if( argc!=4 ){
  250. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  251. " DB FORMAT STRING", 0);
  252. return TCL_ERROR;
  253. }
  254. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  255. Tcl_DStringInit(&str);
  256. zSql = sqlite3_mprintf(argv[2], argv[3]);
  257. rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
  258. sqlite3_free(zSql);
  259. sprintf(zBuf, "%d", rc);
  260. Tcl_AppendElement(interp, zBuf);
  261. Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
  262. Tcl_DStringFree(&str);
  263. if( zErr ) sqlite3_free(zErr);
  264. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  265. return TCL_OK;
  266. }
  267. /*
  268. ** Usage: sqlite3_exec_hex DB HEX
  269. **
  270. ** Invoke the sqlite3_exec() on a string that is obtained by translating
  271. ** HEX into ASCII. Most characters are translated as is. %HH becomes
  272. ** a hex character.
  273. */
  274. static int test_exec_hex(
  275. void *NotUsed,
  276. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  277. int argc, /* Number of arguments */
  278. char **argv /* Text of each argument */
  279. ){
  280. sqlite3 *db;
  281. Tcl_DString str;
  282. int rc, i, j;
  283. char *zErr = 0;
  284. char *zHex;
  285. char zSql[500];
  286. char zBuf[30];
  287. if( argc!=3 ){
  288. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  289. " DB HEX", 0);
  290. return TCL_ERROR;
  291. }
  292. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  293. zHex = argv[2];
  294. for(i=j=0; i<sizeof(zSql) && zHex[j]; i++, j++){
  295. if( zHex[j]=='%' && zHex[j+2] && zHex[j+2] ){
  296. zSql[i] = (testHexToInt(zHex[j+1])<<4) + testHexToInt(zHex[j+2]);
  297. j += 2;
  298. }else{
  299. zSql[i] = zHex[j];
  300. }
  301. }
  302. zSql[i] = 0;
  303. Tcl_DStringInit(&str);
  304. rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
  305. sprintf(zBuf, "%d", rc);
  306. Tcl_AppendElement(interp, zBuf);
  307. Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
  308. Tcl_DStringFree(&str);
  309. if( zErr ) sqlite3_free(zErr);
  310. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  311. return TCL_OK;
  312. }
  313. /*
  314. ** Usage: db_enter DB
  315. ** db_leave DB
  316. **
  317. ** Enter or leave the mutex on a database connection.
  318. */
  319. static int db_enter(
  320. void *NotUsed,
  321. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  322. int argc, /* Number of arguments */
  323. char **argv /* Text of each argument */
  324. ){
  325. sqlite3 *db;
  326. if( argc!=2 ){
  327. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  328. " DB", 0);
  329. return TCL_ERROR;
  330. }
  331. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  332. sqlite3_mutex_enter(db->mutex);
  333. return TCL_OK;
  334. }
  335. static int db_leave(
  336. void *NotUsed,
  337. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  338. int argc, /* Number of arguments */
  339. char **argv /* Text of each argument */
  340. ){
  341. sqlite3 *db;
  342. if( argc!=2 ){
  343. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  344. " DB", 0);
  345. return TCL_ERROR;
  346. }
  347. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  348. sqlite3_mutex_leave(db->mutex);
  349. return TCL_OK;
  350. }
  351. /*
  352. ** Usage: sqlite3_exec DB SQL
  353. **
  354. ** Invoke the sqlite3_exec interface using the open database DB
  355. */
  356. static int test_exec(
  357. void *NotUsed,
  358. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  359. int argc, /* Number of arguments */
  360. char **argv /* Text of each argument */
  361. ){
  362. sqlite3 *db;
  363. Tcl_DString str;
  364. int rc;
  365. char *zErr = 0;
  366. char *zSql;
  367. int i, j;
  368. char zBuf[30];
  369. if( argc!=3 ){
  370. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  371. " DB SQL", 0);
  372. return TCL_ERROR;
  373. }
  374. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  375. Tcl_DStringInit(&str);
  376. zSql = sqlite3_mprintf("%s", argv[2]);
  377. for(i=j=0; zSql[i];){
  378. if( zSql[i]=='%' ){
  379. zSql[j++] = (testHexToInt(zSql[i+1])<<4) + testHexToInt(zSql[i+2]);
  380. i += 3;
  381. }else{
  382. zSql[j++] = zSql[i++];
  383. }
  384. }
  385. zSql[j] = 0;
  386. rc = sqlite3_exec(db, zSql, exec_printf_cb, &str, &zErr);
  387. sqlite3_free(zSql);
  388. sprintf(zBuf, "%d", rc);
  389. Tcl_AppendElement(interp, zBuf);
  390. Tcl_AppendElement(interp, rc==SQLITE_OK ? Tcl_DStringValue(&str) : zErr);
  391. Tcl_DStringFree(&str);
  392. if( zErr ) sqlite3_free(zErr);
  393. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  394. return TCL_OK;
  395. }
  396. /*
  397. ** Usage: sqlite3_exec_nr DB SQL
  398. **
  399. ** Invoke the sqlite3_exec interface using the open database DB. Discard
  400. ** all results
  401. */
  402. static int test_exec_nr(
  403. void *NotUsed,
  404. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  405. int argc, /* Number of arguments */
  406. char **argv /* Text of each argument */
  407. ){
  408. sqlite3 *db;
  409. int rc;
  410. char *zErr = 0;
  411. if( argc!=3 ){
  412. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  413. " DB SQL", 0);
  414. return TCL_ERROR;
  415. }
  416. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  417. rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
  418. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  419. return TCL_OK;
  420. }
  421. /*
  422. ** Usage: sqlite3_mprintf_z_test SEPARATOR ARG0 ARG1 ...
  423. **
  424. ** Test the %z format of sqlite_mprintf(). Use multiple mprintf() calls to
  425. ** concatenate arg0 through argn using separator as the separator.
  426. ** Return the result.
  427. */
  428. static int test_mprintf_z(
  429. void *NotUsed,
  430. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  431. int argc, /* Number of arguments */
  432. char **argv /* Text of each argument */
  433. ){
  434. char *zResult = 0;
  435. int i;
  436. for(i=2; i<argc && (i==2 || zResult); i++){
  437. zResult = sqlite3_mprintf("%z%s%s", zResult, argv[1], argv[i]);
  438. }
  439. Tcl_AppendResult(interp, zResult, 0);
  440. sqlite3_free(zResult);
  441. return TCL_OK;
  442. }
  443. /*
  444. ** Usage: sqlite3_mprintf_n_test STRING
  445. **
  446. ** Test the %n format of sqlite_mprintf(). Return the length of the
  447. ** input string.
  448. */
  449. static int test_mprintf_n(
  450. void *NotUsed,
  451. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  452. int argc, /* Number of arguments */
  453. char **argv /* Text of each argument */
  454. ){
  455. char *zStr;
  456. int n = 0;
  457. zStr = sqlite3_mprintf("%s%n", argv[1], &n);
  458. sqlite3_free(zStr);
  459. Tcl_SetObjResult(interp, Tcl_NewIntObj(n));
  460. return TCL_OK;
  461. }
  462. /*
  463. ** Usage: sqlite3_snprintf_int SIZE FORMAT INT
  464. **
  465. ** Test the of sqlite3_snprintf() routine. SIZE is the size of the
  466. ** output buffer in bytes. The maximum size is 100. FORMAT is the
  467. ** format string. INT is a single integer argument. The FORMAT
  468. ** string must require no more than this one integer argument. If
  469. ** You pass in a format string that requires more than one argument,
  470. ** bad things will happen.
  471. */
  472. static int test_snprintf_int(
  473. void *NotUsed,
  474. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  475. int argc, /* Number of arguments */
  476. char **argv /* Text of each argument */
  477. ){
  478. char zStr[100];
  479. int n = atoi(argv[1]);
  480. const char *zFormat = argv[2];
  481. int a1 = atoi(argv[3]);
  482. if( n>sizeof(zStr) ) n = sizeof(zStr);
  483. sqlite3_snprintf(sizeof(zStr), zStr, "abcdefghijklmnopqrstuvwxyz");
  484. sqlite3_snprintf(n, zStr, zFormat, a1);
  485. Tcl_AppendResult(interp, zStr, 0);
  486. return TCL_OK;
  487. }
  488. #ifndef SQLITE_OMIT_GET_TABLE
  489. /*
  490. ** Usage: sqlite3_get_table_printf DB FORMAT STRING ?--no-counts?
  491. **
  492. ** Invoke the sqlite3_get_table_printf() interface using the open database
  493. ** DB. The SQL is the string FORMAT. The format string should contain
  494. ** one %s or %q. STRING is the value inserted into %s or %q.
  495. */
  496. static int test_get_table_printf(
  497. void *NotUsed,
  498. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  499. int argc, /* Number of arguments */
  500. char **argv /* Text of each argument */
  501. ){
  502. sqlite3 *db;
  503. Tcl_DString str;
  504. int rc;
  505. char *zErr = 0;
  506. int nRow, nCol;
  507. char **aResult;
  508. int i;
  509. char zBuf[30];
  510. char *zSql;
  511. int resCount = -1;
  512. if( argc==5 ){
  513. if( Tcl_GetInt(interp, argv[4], &resCount) ) return TCL_ERROR;
  514. }
  515. if( argc!=4 && argc!=5 ){
  516. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  517. " DB FORMAT STRING ?COUNT?", 0);
  518. return TCL_ERROR;
  519. }
  520. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  521. Tcl_DStringInit(&str);
  522. zSql = sqlite3_mprintf(argv[2],argv[3]);
  523. if( argc==5 ){
  524. rc = sqlite3_get_table(db, zSql, &aResult, 0, 0, &zErr);
  525. }else{
  526. rc = sqlite3_get_table(db, zSql, &aResult, &nRow, &nCol, &zErr);
  527. resCount = (nRow+1)*nCol;
  528. }
  529. sqlite3_free(zSql);
  530. sprintf(zBuf, "%d", rc);
  531. Tcl_AppendElement(interp, zBuf);
  532. if( rc==SQLITE_OK ){
  533. if( argc==4 ){
  534. sprintf(zBuf, "%d", nRow);
  535. Tcl_AppendElement(interp, zBuf);
  536. sprintf(zBuf, "%d", nCol);
  537. Tcl_AppendElement(interp, zBuf);
  538. }
  539. for(i=0; i<resCount; i++){
  540. Tcl_AppendElement(interp, aResult[i] ? aResult[i] : "NULL");
  541. }
  542. }else{
  543. Tcl_AppendElement(interp, zErr);
  544. }
  545. sqlite3_free_table(aResult);
  546. if( zErr ) sqlite3_free(zErr);
  547. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  548. return TCL_OK;
  549. }
  550. #endif /* SQLITE_OMIT_GET_TABLE */
  551. /*
  552. ** Usage: sqlite3_last_insert_rowid DB
  553. **
  554. ** Returns the integer ROWID of the most recent insert.
  555. */
  556. static int test_last_rowid(
  557. void *NotUsed,
  558. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  559. int argc, /* Number of arguments */
  560. char **argv /* Text of each argument */
  561. ){
  562. sqlite3 *db;
  563. char zBuf[30];
  564. if( argc!=2 ){
  565. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0);
  566. return TCL_ERROR;
  567. }
  568. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  569. sprintf(zBuf, "%lld", sqlite3_last_insert_rowid(db));
  570. Tcl_AppendResult(interp, zBuf, 0);
  571. return SQLITE_OK;
  572. }
  573. /*
  574. ** Usage: sqlite3_key DB KEY
  575. **
  576. ** Set the codec key.
  577. */
  578. static int test_key(
  579. void *NotUsed,
  580. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  581. int argc, /* Number of arguments */
  582. char **argv /* Text of each argument */
  583. ){
  584. #ifdef SQLITE_HAS_CODEC
  585. sqlite3 *db;
  586. const char *zKey;
  587. int nKey;
  588. if( argc!=3 ){
  589. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  590. " FILENAME\"", 0);
  591. return TCL_ERROR;
  592. }
  593. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  594. zKey = argv[2];
  595. nKey = strlen(zKey);
  596. sqlite3_key(db, zKey, nKey);
  597. #endif
  598. return TCL_OK;
  599. }
  600. /*
  601. ** Usage: sqlite3_rekey DB KEY
  602. **
  603. ** Change the codec key.
  604. */
  605. static int test_rekey(
  606. void *NotUsed,
  607. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  608. int argc, /* Number of arguments */
  609. char **argv /* Text of each argument */
  610. ){
  611. #ifdef SQLITE_HAS_CODEC
  612. sqlite3 *db;
  613. const char *zKey;
  614. int nKey;
  615. if( argc!=3 ){
  616. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  617. " FILENAME\"", 0);
  618. return TCL_ERROR;
  619. }
  620. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  621. zKey = argv[2];
  622. nKey = strlen(zKey);
  623. sqlite3_rekey(db, zKey, nKey);
  624. #endif
  625. return TCL_OK;
  626. }
  627. /*
  628. ** Usage: sqlite3_close DB
  629. **
  630. ** Closes the database opened by sqlite3_open.
  631. */
  632. static int sqlite_test_close(
  633. void *NotUsed,
  634. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  635. int argc, /* Number of arguments */
  636. char **argv /* Text of each argument */
  637. ){
  638. sqlite3 *db;
  639. int rc;
  640. if( argc!=2 ){
  641. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  642. " FILENAME\"", 0);
  643. return TCL_ERROR;
  644. }
  645. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  646. rc = sqlite3_close(db);
  647. Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  648. return TCL_OK;
  649. }
  650. /*
  651. ** Usage: sqlite3_close_v2 DB
  652. **
  653. ** Closes the database opened by sqlite3_open.
  654. */
  655. static int sqlite_test_close_v2(
  656. void *NotUsed,
  657. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  658. int argc, /* Number of arguments */
  659. char **argv /* Text of each argument */
  660. ){
  661. sqlite3 *db;
  662. int rc;
  663. if( argc!=2 ){
  664. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  665. " FILENAME\"", 0);
  666. return TCL_ERROR;
  667. }
  668. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  669. rc = sqlite3_close_v2(db);
  670. Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  671. return TCL_OK;
  672. }
  673. /*
  674. ** Implementation of the x_coalesce() function.
  675. ** Return the first argument non-NULL argument.
  676. */
  677. static void t1_ifnullFunc(
  678. sqlite3_context *context,
  679. int argc,
  680. sqlite3_value **argv
  681. ){
  682. int i;
  683. for(i=0; i<argc; i++){
  684. if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
  685. int n = sqlite3_value_bytes(argv[i]);
  686. sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
  687. n, SQLITE_TRANSIENT);
  688. break;
  689. }
  690. }
  691. }
  692. /*
  693. ** These are test functions. hex8() interprets its argument as
  694. ** UTF8 and returns a hex encoding. hex16le() interprets its argument
  695. ** as UTF16le and returns a hex encoding.
  696. */
  697. static void hex8Func(sqlite3_context *p, int argc, sqlite3_value **argv){
  698. const unsigned char *z;
  699. int i;
  700. char zBuf[200];
  701. z = sqlite3_value_text(argv[0]);
  702. for(i=0; i<sizeof(zBuf)/2 - 2 && z[i]; i++){
  703. sprintf(&zBuf[i*2], "%02x", z[i]&0xff);
  704. }
  705. zBuf[i*2] = 0;
  706. sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
  707. }
  708. #ifndef SQLITE_OMIT_UTF16
  709. static void hex16Func(sqlite3_context *p, int argc, sqlite3_value **argv){
  710. const unsigned short int *z;
  711. int i;
  712. char zBuf[400];
  713. z = sqlite3_value_text16(argv[0]);
  714. for(i=0; i<sizeof(zBuf)/4 - 4 && z[i]; i++){
  715. sprintf(&zBuf[i*4], "%04x", z[i]&0xff);
  716. }
  717. zBuf[i*4] = 0;
  718. sqlite3_result_text(p, (char*)zBuf, -1, SQLITE_TRANSIENT);
  719. }
  720. #endif
  721. /*
  722. ** A structure into which to accumulate text.
  723. */
  724. struct dstr {
  725. int nAlloc; /* Space allocated */
  726. int nUsed; /* Space used */
  727. char *z; /* The space */
  728. };
  729. /*
  730. ** Append text to a dstr
  731. */
  732. static void dstrAppend(struct dstr *p, const char *z, int divider){
  733. int n = (int)strlen(z);
  734. if( p->nUsed + n + 2 > p->nAlloc ){
  735. char *zNew;
  736. p->nAlloc = p->nAlloc*2 + n + 200;
  737. zNew = sqlite3_realloc(p->z, p->nAlloc);
  738. if( zNew==0 ){
  739. sqlite3_free(p->z);
  740. memset(p, 0, sizeof(*p));
  741. return;
  742. }
  743. p->z = zNew;
  744. }
  745. if( divider && p->nUsed>0 ){
  746. p->z[p->nUsed++] = divider;
  747. }
  748. memcpy(&p->z[p->nUsed], z, n+1);
  749. p->nUsed += n;
  750. }
  751. /*
  752. ** Invoked for each callback from sqlite3ExecFunc
  753. */
  754. static int execFuncCallback(void *pData, int argc, char **argv, char **NotUsed){
  755. struct dstr *p = (struct dstr*)pData;
  756. int i;
  757. for(i=0; i<argc; i++){
  758. if( argv[i]==0 ){
  759. dstrAppend(p, "NULL", ' ');
  760. }else{
  761. dstrAppend(p, argv[i], ' ');
  762. }
  763. }
  764. return 0;
  765. }
  766. /*
  767. ** Implementation of the x_sqlite_exec() function. This function takes
  768. ** a single argument and attempts to execute that argument as SQL code.
  769. ** This is illegal and should set the SQLITE_MISUSE flag on the database.
  770. **
  771. ** 2004-Jan-07: We have changed this to make it legal to call sqlite3_exec()
  772. ** from within a function call.
  773. **
  774. ** This routine simulates the effect of having two threads attempt to
  775. ** use the same database at the same time.
  776. */
  777. static void sqlite3ExecFunc(
  778. sqlite3_context *context,
  779. int argc,
  780. sqlite3_value **argv
  781. ){
  782. struct dstr x;
  783. memset(&x, 0, sizeof(x));
  784. (void)sqlite3_exec((sqlite3*)sqlite3_user_data(context),
  785. (char*)sqlite3_value_text(argv[0]),
  786. execFuncCallback, &x, 0);
  787. sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT);
  788. sqlite3_free(x.z);
  789. }
  790. /*
  791. ** Implementation of tkt2213func(), a scalar function that takes exactly
  792. ** one argument. It has two interesting features:
  793. **
  794. ** * It calls sqlite3_value_text() 3 times on the argument sqlite3_value*.
  795. ** If the three pointers returned are not the same an SQL error is raised.
  796. **
  797. ** * Otherwise it returns a copy of the text representation of its
  798. ** argument in such a way as the VDBE representation is a Mem* cell
  799. ** with the MEM_Term flag clear.
  800. **
  801. ** Ticket #2213 can therefore be tested by evaluating the following
  802. ** SQL expression:
  803. **
  804. ** tkt2213func(tkt2213func('a string'));
  805. */
  806. static void tkt2213Function(
  807. sqlite3_context *context,
  808. int argc,
  809. sqlite3_value **argv
  810. ){
  811. int nText;
  812. unsigned char const *zText1;
  813. unsigned char const *zText2;
  814. unsigned char const *zText3;
  815. nText = sqlite3_value_bytes(argv[0]);
  816. zText1 = sqlite3_value_text(argv[0]);
  817. zText2 = sqlite3_value_text(argv[0]);
  818. zText3 = sqlite3_value_text(argv[0]);
  819. if( zText1!=zText2 || zText2!=zText3 ){
  820. sqlite3_result_error(context, "tkt2213 is not fixed", -1);
  821. }else{
  822. char *zCopy = (char *)sqlite3_malloc(nText);
  823. memcpy(zCopy, zText1, nText);
  824. sqlite3_result_text(context, zCopy, nText, sqlite3_free);
  825. }
  826. }
  827. /*
  828. ** The following SQL function takes 4 arguments. The 2nd and
  829. ** 4th argument must be one of these strings: 'text', 'text16',
  830. ** or 'blob' corresponding to API functions
  831. **
  832. ** sqlite3_value_text()
  833. ** sqlite3_value_text16()
  834. ** sqlite3_value_blob()
  835. **
  836. ** The third argument is a string, either 'bytes' or 'bytes16' or 'noop',
  837. ** corresponding to APIs:
  838. **
  839. ** sqlite3_value_bytes()
  840. ** sqlite3_value_bytes16()
  841. ** noop
  842. **
  843. ** The APIs designated by the 2nd through 4th arguments are applied
  844. ** to the first argument in order. If the pointers returned by the
  845. ** second and fourth are different, this routine returns 1. Otherwise,
  846. ** this routine returns 0.
  847. **
  848. ** This function is used to test to see when returned pointers from
  849. ** the _text(), _text16() and _blob() APIs become invalidated.
  850. */
  851. static void ptrChngFunction(
  852. sqlite3_context *context,
  853. int argc,
  854. sqlite3_value **argv
  855. ){
  856. const void *p1, *p2;
  857. const char *zCmd;
  858. if( argc!=4 ) return;
  859. zCmd = (const char*)sqlite3_value_text(argv[1]);
  860. if( zCmd==0 ) return;
  861. if( strcmp(zCmd,"text")==0 ){
  862. p1 = (const void*)sqlite3_value_text(argv[0]);
  863. #ifndef SQLITE_OMIT_UTF16
  864. }else if( strcmp(zCmd, "text16")==0 ){
  865. p1 = (const void*)sqlite3_value_text16(argv[0]);
  866. #endif
  867. }else if( strcmp(zCmd, "blob")==0 ){
  868. p1 = (const void*)sqlite3_value_blob(argv[0]);
  869. }else{
  870. return;
  871. }
  872. zCmd = (const char*)sqlite3_value_text(argv[2]);
  873. if( zCmd==0 ) return;
  874. if( strcmp(zCmd,"bytes")==0 ){
  875. sqlite3_value_bytes(argv[0]);
  876. #ifndef SQLITE_OMIT_UTF16
  877. }else if( strcmp(zCmd, "bytes16")==0 ){
  878. sqlite3_value_bytes16(argv[0]);
  879. #endif
  880. }else if( strcmp(zCmd, "noop")==0 ){
  881. /* do nothing */
  882. }else{
  883. return;
  884. }
  885. zCmd = (const char*)sqlite3_value_text(argv[3]);
  886. if( zCmd==0 ) return;
  887. if( strcmp(zCmd,"text")==0 ){
  888. p2 = (const void*)sqlite3_value_text(argv[0]);
  889. #ifndef SQLITE_OMIT_UTF16
  890. }else if( strcmp(zCmd, "text16")==0 ){
  891. p2 = (const void*)sqlite3_value_text16(argv[0]);
  892. #endif
  893. }else if( strcmp(zCmd, "blob")==0 ){
  894. p2 = (const void*)sqlite3_value_blob(argv[0]);
  895. }else{
  896. return;
  897. }
  898. sqlite3_result_int(context, p1!=p2);
  899. }
  900. /*
  901. ** Usage: sqlite_test_create_function DB
  902. **
  903. ** Call the sqlite3_create_function API on the given database in order
  904. ** to create a function named "x_coalesce". This function does the same thing
  905. ** as the "coalesce" function. This function also registers an SQL function
  906. ** named "x_sqlite_exec" that invokes sqlite3_exec(). Invoking sqlite3_exec()
  907. ** in this way is illegal recursion and should raise an SQLITE_MISUSE error.
  908. ** The effect is similar to trying to use the same database connection from
  909. ** two threads at the same time.
  910. **
  911. ** The original motivation for this routine was to be able to call the
  912. ** sqlite3_create_function function while a query is in progress in order
  913. ** to test the SQLITE_MISUSE detection logic.
  914. */
  915. static int test_create_function(
  916. void *NotUsed,
  917. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  918. int argc, /* Number of arguments */
  919. char **argv /* Text of each argument */
  920. ){
  921. int rc;
  922. sqlite3 *db;
  923. if( argc!=2 ){
  924. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  925. " DB\"", 0);
  926. return TCL_ERROR;
  927. }
  928. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  929. rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0,
  930. t1_ifnullFunc, 0, 0);
  931. if( rc==SQLITE_OK ){
  932. rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0,
  933. hex8Func, 0, 0);
  934. }
  935. #ifndef SQLITE_OMIT_UTF16
  936. if( rc==SQLITE_OK ){
  937. rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0,
  938. hex16Func, 0, 0);
  939. }
  940. #endif
  941. if( rc==SQLITE_OK ){
  942. rc = sqlite3_create_function(db, "tkt2213func", 1, SQLITE_ANY, 0,
  943. tkt2213Function, 0, 0);
  944. }
  945. if( rc==SQLITE_OK ){
  946. rc = sqlite3_create_function(db, "pointer_change", 4, SQLITE_ANY, 0,
  947. ptrChngFunction, 0, 0);
  948. }
  949. #ifndef SQLITE_OMIT_UTF16
  950. /* Use the sqlite3_create_function16() API here. Mainly for fun, but also
  951. ** because it is not tested anywhere else. */
  952. if( rc==SQLITE_OK ){
  953. const void *zUtf16;
  954. sqlite3_value *pVal;
  955. sqlite3_mutex_enter(db->mutex);
  956. pVal = sqlite3ValueNew(db);
  957. sqlite3ValueSetStr(pVal, -1, "x_sqlite_exec", SQLITE_UTF8, SQLITE_STATIC);
  958. zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
  959. if( db->mallocFailed ){
  960. rc = SQLITE_NOMEM;
  961. }else{
  962. rc = sqlite3_create_function16(db, zUtf16,
  963. 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
  964. }
  965. sqlite3ValueFree(pVal);
  966. sqlite3_mutex_leave(db->mutex);
  967. }
  968. #endif
  969. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  970. Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
  971. return TCL_OK;
  972. }
  973. /*
  974. ** Routines to implement the x_count() aggregate function.
  975. **
  976. ** x_count() counts the number of non-null arguments. But there are
  977. ** some twists for testing purposes.
  978. **
  979. ** If the argument to x_count() is 40 then a UTF-8 error is reported
  980. ** on the step function. If x_count(41) is seen, then a UTF-16 error
  981. ** is reported on the step function. If the total count is 42, then
  982. ** a UTF-8 error is reported on the finalize function.
  983. */
  984. typedef struct t1CountCtx t1CountCtx;
  985. struct t1CountCtx {
  986. int n;
  987. };
  988. static void t1CountStep(
  989. sqlite3_context *context,
  990. int argc,
  991. sqlite3_value **argv
  992. ){
  993. t1CountCtx *p;
  994. p = sqlite3_aggregate_context(context, sizeof(*p));
  995. if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
  996. p->n++;
  997. }
  998. if( argc>0 ){
  999. int v = sqlite3_value_int(argv[0]);
  1000. if( v==40 ){
  1001. sqlite3_result_error(context, "value of 40 handed to x_count", -1);
  1002. #ifndef SQLITE_OMIT_UTF16
  1003. }else if( v==41 ){
  1004. const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
  1005. sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
  1006. #endif
  1007. }
  1008. }
  1009. }
  1010. static void t1CountFinalize(sqlite3_context *context){
  1011. t1CountCtx *p;
  1012. p = sqlite3_aggregate_context(context, sizeof(*p));
  1013. if( p ){
  1014. if( p->n==42 ){
  1015. sqlite3_result_error(context, "x_count totals to 42", -1);
  1016. }else{
  1017. sqlite3_result_int(context, p ? p->n : 0);
  1018. }
  1019. }
  1020. }
  1021. #ifndef SQLITE_OMIT_DEPRECATED
  1022. static void legacyCountStep(
  1023. sqlite3_context *context,
  1024. int argc,
  1025. sqlite3_value **argv
  1026. ){
  1027. /* no-op */
  1028. }
  1029. static void legacyCountFinalize(sqlite3_context *context){
  1030. sqlite3_result_int(context, sqlite3_aggregate_count(context));
  1031. }
  1032. #endif
  1033. /*
  1034. ** Usage: sqlite3_create_aggregate DB
  1035. **
  1036. ** Call the sqlite3_create_function API on the given database in order
  1037. ** to create a function named "x_count". This function is similar
  1038. ** to the built-in count() function, with a few special quirks
  1039. ** for testing the sqlite3_result_error() APIs.
  1040. **
  1041. ** The original motivation for this routine was to be able to call the
  1042. ** sqlite3_create_aggregate function while a query is in progress in order
  1043. ** to test the SQLITE_MISUSE detection logic. See misuse.test.
  1044. **
  1045. ** This routine was later extended to test the use of sqlite3_result_error()
  1046. ** within aggregate functions.
  1047. **
  1048. ** Later: It is now also extended to register the aggregate function
  1049. ** "legacy_count()" with the supplied database handle. This is used
  1050. ** to test the deprecated sqlite3_aggregate_count() API.
  1051. */
  1052. static int test_create_aggregate(
  1053. void *NotUsed,
  1054. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1055. int argc, /* Number of arguments */
  1056. char **argv /* Text of each argument */
  1057. ){
  1058. sqlite3 *db;
  1059. int rc;
  1060. if( argc!=2 ){
  1061. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1062. " FILENAME\"", 0);
  1063. return TCL_ERROR;
  1064. }
  1065. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  1066. rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
  1067. t1CountStep,t1CountFinalize);
  1068. if( rc==SQLITE_OK ){
  1069. rc = sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
  1070. t1CountStep,t1CountFinalize);
  1071. }
  1072. #ifndef SQLITE_OMIT_DEPRECATED
  1073. if( rc==SQLITE_OK ){
  1074. rc = sqlite3_create_function(db, "legacy_count", 0, SQLITE_ANY, 0, 0,
  1075. legacyCountStep, legacyCountFinalize
  1076. );
  1077. }
  1078. #endif
  1079. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  1080. Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
  1081. return TCL_OK;
  1082. }
  1083. /*
  1084. ** Usage: printf TEXT
  1085. **
  1086. ** Send output to printf. Use this rather than puts to merge the output
  1087. ** in the correct sequence with debugging printfs inserted into C code.
  1088. ** Puts uses a separate buffer and debugging statements will be out of
  1089. ** sequence if it is used.
  1090. */
  1091. static int test_printf(
  1092. void *NotUsed,
  1093. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1094. int argc, /* Number of arguments */
  1095. char **argv /* Text of each argument */
  1096. ){
  1097. if( argc!=2 ){
  1098. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1099. " TEXT\"", 0);
  1100. return TCL_ERROR;
  1101. }
  1102. printf("%s\n", argv[1]);
  1103. return TCL_OK;
  1104. }
  1105. /*
  1106. ** Usage: sqlite3_mprintf_int FORMAT INTEGER INTEGER INTEGER
  1107. **
  1108. ** Call mprintf with three integer arguments
  1109. */
  1110. static int sqlite3_mprintf_int(
  1111. void *NotUsed,
  1112. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1113. int argc, /* Number of arguments */
  1114. char **argv /* Text of each argument */
  1115. ){
  1116. int a[3], i;
  1117. char *z;
  1118. if( argc!=5 ){
  1119. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1120. " FORMAT INT INT INT\"", 0);
  1121. return TCL_ERROR;
  1122. }
  1123. for(i=2; i<5; i++){
  1124. if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
  1125. }
  1126. z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
  1127. Tcl_AppendResult(interp, z, 0);
  1128. sqlite3_free(z);
  1129. return TCL_OK;
  1130. }
  1131. /*
  1132. ** Usage: sqlite3_mprintf_int64 FORMAT INTEGER INTEGER INTEGER
  1133. **
  1134. ** Call mprintf with three 64-bit integer arguments
  1135. */
  1136. static int sqlite3_mprintf_int64(
  1137. void *NotUsed,
  1138. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1139. int argc, /* Number of arguments */
  1140. char **argv /* Text of each argument */
  1141. ){
  1142. int i;
  1143. sqlite_int64 a[3];
  1144. char *z;
  1145. if( argc!=5 ){
  1146. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1147. " FORMAT INT INT INT\"", 0);
  1148. return TCL_ERROR;
  1149. }
  1150. for(i=2; i<5; i++){
  1151. if( sqlite3Atoi64(argv[i], &a[i-2], 1000000, SQLITE_UTF8) ){
  1152. Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0);
  1153. return TCL_ERROR;
  1154. }
  1155. }
  1156. z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
  1157. Tcl_AppendResult(interp, z, 0);
  1158. sqlite3_free(z);
  1159. return TCL_OK;
  1160. }
  1161. /*
  1162. ** Usage: sqlite3_mprintf_long FORMAT INTEGER INTEGER INTEGER
  1163. **
  1164. ** Call mprintf with three long integer arguments. This might be the
  1165. ** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on
  1166. ** platform.
  1167. */
  1168. static int sqlite3_mprintf_long(
  1169. void *NotUsed,
  1170. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1171. int argc, /* Number of arguments */
  1172. char **argv /* Text of each argument */
  1173. ){
  1174. int i;
  1175. long int a[3];
  1176. int b[3];
  1177. char *z;
  1178. if( argc!=5 ){
  1179. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1180. " FORMAT INT INT INT\"", 0);
  1181. return TCL_ERROR;
  1182. }
  1183. for(i=2; i<5; i++){
  1184. if( Tcl_GetInt(interp, argv[i], &b[i-2]) ) return TCL_ERROR;
  1185. a[i-2] = (long int)b[i-2];
  1186. a[i-2] &= (((u64)1)<<(sizeof(int)*8))-1;
  1187. }
  1188. z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]);
  1189. Tcl_AppendResult(interp, z, 0);
  1190. sqlite3_free(z);
  1191. return TCL_OK;
  1192. }
  1193. /*
  1194. ** Usage: sqlite3_mprintf_str FORMAT INTEGER INTEGER STRING
  1195. **
  1196. ** Call mprintf with two integer arguments and one string argument
  1197. */
  1198. static int sqlite3_mprintf_str(
  1199. void *NotUsed,
  1200. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1201. int argc, /* Number of arguments */
  1202. char **argv /* Text of each argument */
  1203. ){
  1204. int a[3], i;
  1205. char *z;
  1206. if( argc<4 || argc>5 ){
  1207. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1208. " FORMAT INT INT ?STRING?\"", 0);
  1209. return TCL_ERROR;
  1210. }
  1211. for(i=2; i<4; i++){
  1212. if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
  1213. }
  1214. z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL);
  1215. Tcl_AppendResult(interp, z, 0);
  1216. sqlite3_free(z);
  1217. return TCL_OK;
  1218. }
  1219. /*
  1220. ** Usage: sqlite3_snprintf_str INTEGER FORMAT INTEGER INTEGER STRING
  1221. **
  1222. ** Call mprintf with two integer arguments and one string argument
  1223. */
  1224. static int sqlite3_snprintf_str(
  1225. void *NotUsed,
  1226. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1227. int argc, /* Number of arguments */
  1228. char **argv /* Text of each argument */
  1229. ){
  1230. int a[3], i;
  1231. int n;
  1232. char *z;
  1233. if( argc<5 || argc>6 ){
  1234. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1235. " INT FORMAT INT INT ?STRING?\"", 0);
  1236. return TCL_ERROR;
  1237. }
  1238. if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
  1239. if( n<0 ){
  1240. Tcl_AppendResult(interp, "N must be non-negative", 0);
  1241. return TCL_ERROR;
  1242. }
  1243. for(i=3; i<5; i++){
  1244. if( Tcl_GetInt(interp, argv[i], &a[i-3]) ) return TCL_ERROR;
  1245. }
  1246. z = sqlite3_malloc( n+1 );
  1247. sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL);
  1248. Tcl_AppendResult(interp, z, 0);
  1249. sqlite3_free(z);
  1250. return TCL_OK;
  1251. }
  1252. /*
  1253. ** Usage: sqlite3_mprintf_double FORMAT INTEGER INTEGER DOUBLE
  1254. **
  1255. ** Call mprintf with two integer arguments and one double argument
  1256. */
  1257. static int sqlite3_mprintf_double(
  1258. void *NotUsed,
  1259. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1260. int argc, /* Number of arguments */
  1261. char **argv /* Text of each argument */
  1262. ){
  1263. int a[3], i;
  1264. double r;
  1265. char *z;
  1266. if( argc!=5 ){
  1267. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1268. " FORMAT INT INT DOUBLE\"", 0);
  1269. return TCL_ERROR;
  1270. }
  1271. for(i=2; i<4; i++){
  1272. if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR;
  1273. }
  1274. if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR;
  1275. z = sqlite3_mprintf(argv[1], a[0], a[1], r);
  1276. Tcl_AppendResult(interp, z, 0);
  1277. sqlite3_free(z);
  1278. return TCL_OK;
  1279. }
  1280. /*
  1281. ** Usage: sqlite3_mprintf_scaled FORMAT DOUBLE DOUBLE
  1282. **
  1283. ** Call mprintf with a single double argument which is the product of the
  1284. ** two arguments given above. This is used to generate overflow and underflow
  1285. ** doubles to test that they are converted properly.
  1286. */
  1287. static int sqlite3_mprintf_scaled(
  1288. void *NotUsed,
  1289. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1290. int argc, /* Number of arguments */
  1291. char **argv /* Text of each argument */
  1292. ){
  1293. int i;
  1294. double r[2];
  1295. char *z;
  1296. if( argc!=4 ){
  1297. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1298. " FORMAT DOUBLE DOUBLE\"", 0);
  1299. return TCL_ERROR;
  1300. }
  1301. for(i=2; i<4; i++){
  1302. if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR;
  1303. }
  1304. z = sqlite3_mprintf(argv[1], r[0]*r[1]);
  1305. Tcl_AppendResult(interp, z, 0);
  1306. sqlite3_free(z);
  1307. return TCL_OK;
  1308. }
  1309. /*
  1310. ** Usage: sqlite3_mprintf_stronly FORMAT STRING
  1311. **
  1312. ** Call mprintf with a single double argument which is the product of the
  1313. ** two arguments given above. This is used to generate overflow and underflow
  1314. ** doubles to test that they are converted properly.
  1315. */
  1316. static int sqlite3_mprintf_stronly(
  1317. void *NotUsed,
  1318. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1319. int argc, /* Number of arguments */
  1320. char **argv /* Text of each argument */
  1321. ){
  1322. char *z;
  1323. if( argc!=3 ){
  1324. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1325. " FORMAT STRING\"", 0);
  1326. return TCL_ERROR;
  1327. }
  1328. z = sqlite3_mprintf(argv[1], argv[2]);
  1329. Tcl_AppendResult(interp, z, 0);
  1330. sqlite3_free(z);
  1331. return TCL_OK;
  1332. }
  1333. /*
  1334. ** Usage: sqlite3_mprintf_hexdouble FORMAT HEX
  1335. **
  1336. ** Call mprintf with a single double argument which is derived from the
  1337. ** hexadecimal encoding of an IEEE double.
  1338. */
  1339. static int sqlite3_mprintf_hexdouble(
  1340. void *NotUsed,
  1341. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1342. int argc, /* Number of arguments */
  1343. char **argv /* Text of each argument */
  1344. ){
  1345. char *z;
  1346. double r;
  1347. unsigned int x1, x2;
  1348. sqlite_uint64 d;
  1349. if( argc!=3 ){
  1350. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1351. " FORMAT STRING\"", 0);
  1352. return TCL_ERROR;
  1353. }
  1354. if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){
  1355. Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0);
  1356. return TCL_ERROR;
  1357. }
  1358. d = x2;
  1359. d = (d<<32) + x1;
  1360. memcpy(&r, &d, sizeof(r));
  1361. z = sqlite3_mprintf(argv[1], r);
  1362. Tcl_AppendResult(interp, z, 0);
  1363. sqlite3_free(z);
  1364. return TCL_OK;
  1365. }
  1366. /*
  1367. ** Usage: sqlite3_enable_shared_cache ?BOOLEAN?
  1368. **
  1369. */
  1370. #if !defined(SQLITE_OMIT_SHARED_CACHE)
  1371. static int test_enable_shared(
  1372. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1373. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1374. int objc, /* Number of arguments */
  1375. Tcl_Obj *CONST objv[] /* Command arguments */
  1376. ){
  1377. int rc;
  1378. int enable;
  1379. int ret = 0;
  1380. if( objc!=2 && objc!=1 ){
  1381. Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?");
  1382. return TCL_ERROR;
  1383. }
  1384. ret = sqlite3GlobalConfig.sharedCacheEnabled;
  1385. if( objc==2 ){
  1386. if( Tcl_GetBooleanFromObj(interp, objv[1], &enable) ){
  1387. return TCL_ERROR;
  1388. }
  1389. rc = sqlite3_enable_shared_cache(enable);
  1390. if( rc!=SQLITE_OK ){
  1391. Tcl_SetResult(interp, (char *)sqlite3ErrStr(rc), TCL_STATIC);
  1392. return TCL_ERROR;
  1393. }
  1394. }
  1395. Tcl_SetObjResult(interp, Tcl_NewBooleanObj(ret));
  1396. return TCL_OK;
  1397. }
  1398. #endif
  1399. /*
  1400. ** Usage: sqlite3_extended_result_codes DB BOOLEAN
  1401. **
  1402. */
  1403. static int test_extended_result_codes(
  1404. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1405. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1406. int objc, /* Number of arguments */
  1407. Tcl_Obj *CONST objv[] /* Command arguments */
  1408. ){
  1409. int enable;
  1410. sqlite3 *db;
  1411. if( objc!=3 ){
  1412. Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
  1413. return TCL_ERROR;
  1414. }
  1415. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1416. if( Tcl_GetBooleanFromObj(interp, objv[2], &enable) ) return TCL_ERROR;
  1417. sqlite3_extended_result_codes(db, enable);
  1418. return TCL_OK;
  1419. }
  1420. /*
  1421. ** Usage: sqlite3_libversion_number
  1422. **
  1423. */
  1424. static int test_libversion_number(
  1425. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1426. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1427. int objc, /* Number of arguments */
  1428. Tcl_Obj *CONST objv[] /* Command arguments */
  1429. ){
  1430. Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_libversion_number()));
  1431. return TCL_OK;
  1432. }
  1433. /*
  1434. ** Usage: sqlite3_table_column_metadata DB dbname tblname colname
  1435. **
  1436. */
  1437. #ifdef SQLITE_ENABLE_COLUMN_METADATA
  1438. static int test_table_column_metadata(
  1439. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  1440. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1441. int objc, /* Number of arguments */
  1442. Tcl_Obj *CONST objv[] /* Command arguments */
  1443. ){
  1444. sqlite3 *db;
  1445. const char *zDb;
  1446. const char *zTbl;
  1447. const char *zCol;
  1448. int rc;
  1449. Tcl_Obj *pRet;
  1450. const char *zDatatype;
  1451. const char *zCollseq;
  1452. int notnull;
  1453. int primarykey;
  1454. int autoincrement;
  1455. if( objc!=5 ){
  1456. Tcl_WrongNumArgs(interp, 1, objv, "DB dbname tblname colname");
  1457. return TCL_ERROR;
  1458. }
  1459. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1460. zDb = Tcl_GetString(objv[2]);
  1461. zTbl = Tcl_GetString(objv[3]);
  1462. zCol = Tcl_GetString(objv[4]);
  1463. if( strlen(zDb)==0 ) zDb = 0;
  1464. rc = sqlite3_table_column_metadata(db, zDb, zTbl, zCol,
  1465. &zDatatype, &zCollseq, &notnull, &primarykey, &autoincrement);
  1466. if( rc!=SQLITE_OK ){
  1467. Tcl_AppendResult(interp, sqlite3_errmsg(db), 0);
  1468. return TCL_ERROR;
  1469. }
  1470. pRet = Tcl_NewObj();
  1471. Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zDatatype, -1));
  1472. Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zCollseq, -1));
  1473. Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(notnull));
  1474. Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(primarykey));
  1475. Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(autoincrement));
  1476. Tcl_SetObjResult(interp, pRet);
  1477. return TCL_OK;
  1478. }
  1479. #endif
  1480. #ifndef SQLITE_OMIT_INCRBLOB
  1481. static int blobHandleFromObj(
  1482. Tcl_Interp *interp,
  1483. Tcl_Obj *pObj,
  1484. sqlite3_blob **ppBlob
  1485. ){
  1486. char *z;
  1487. int n;
  1488. z = Tcl_GetStringFromObj(pObj, &n);
  1489. if( n==0 ){
  1490. *ppBlob = 0;
  1491. }else{
  1492. int notUsed;
  1493. Tcl_Channel channel;
  1494. ClientData instanceData;
  1495. channel = Tcl_GetChannel(interp, z, &notUsed);
  1496. if( !channel ) return TCL_ERROR;
  1497. Tcl_Flush(channel);
  1498. Tcl_Seek(channel, 0, SEEK_SET);
  1499. instanceData = Tcl_GetChannelInstanceData(channel);
  1500. *ppBlob = *((sqlite3_blob **)instanceData);
  1501. }
  1502. return TCL_OK;
  1503. }
  1504. /*
  1505. ** sqlite3_blob_bytes CHANNEL
  1506. */
  1507. static int test_blob_bytes(
  1508. ClientData clientData, /* Not used */
  1509. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1510. int objc, /* Number of arguments */
  1511. Tcl_Obj *CONST objv[] /* Command arguments */
  1512. ){
  1513. sqlite3_blob *pBlob;
  1514. int nByte;
  1515. if( objc!=2 ){
  1516. Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL");
  1517. return TCL_ERROR;
  1518. }
  1519. if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
  1520. nByte = sqlite3_blob_bytes(pBlob);
  1521. Tcl_SetObjResult(interp, Tcl_NewIntObj(nByte));
  1522. return TCL_OK;
  1523. }
  1524. /*
  1525. ** sqlite3_blob_close CHANNEL
  1526. */
  1527. static int test_blob_close(
  1528. ClientData clientData, /* Not used */
  1529. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1530. int objc, /* Number of arguments */
  1531. Tcl_Obj *CONST objv[] /* Command arguments */
  1532. ){
  1533. sqlite3_blob *pBlob;
  1534. if( objc!=2 ){
  1535. Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL");
  1536. return TCL_ERROR;
  1537. }
  1538. if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
  1539. sqlite3_blob_close(pBlob);
  1540. return TCL_OK;
  1541. }
  1542. /*
  1543. ** sqlite3_blob_read CHANNEL OFFSET N
  1544. **
  1545. ** This command is used to test the sqlite3_blob_read() in ways that
  1546. ** the Tcl channel interface does not. The first argument should
  1547. ** be the name of a valid channel created by the [incrblob] method
  1548. ** of a database handle. This function calls sqlite3_blob_read()
  1549. ** to read N bytes from offset OFFSET from the underlying SQLite
  1550. ** blob handle.
  1551. **
  1552. ** On success, a byte-array object containing the read data is
  1553. ** returned. On failure, the interpreter result is set to the
  1554. ** text representation of the returned error code (i.e. "SQLITE_NOMEM")
  1555. ** and a Tcl exception is thrown.
  1556. */
  1557. static int test_blob_read(
  1558. ClientData clientData, /* Not used */
  1559. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1560. int objc, /* Number of arguments */
  1561. Tcl_Obj *CONST objv[] /* Command arguments */
  1562. ){
  1563. sqlite3_blob *pBlob;
  1564. int nByte;
  1565. int iOffset;
  1566. unsigned char *zBuf = 0;
  1567. int rc;
  1568. if( objc!=4 ){
  1569. Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET N");
  1570. return TCL_ERROR;
  1571. }
  1572. if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
  1573. if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset)
  1574. || TCL_OK!=Tcl_GetIntFromObj(interp, objv[3], &nByte)
  1575. ){
  1576. return TCL_ERROR;
  1577. }
  1578. if( nByte>0 ){
  1579. zBuf = (unsigned char *)Tcl_Alloc(nByte);
  1580. }
  1581. rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset);
  1582. if( rc==SQLITE_OK ){
  1583. Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zBuf, nByte));
  1584. }else{
  1585. Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
  1586. }
  1587. Tcl_Free((char *)zBuf);
  1588. return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
  1589. }
  1590. /*
  1591. ** sqlite3_blob_write CHANNEL OFFSET DATA ?NDATA?
  1592. **
  1593. ** This command is used to test the sqlite3_blob_write() in ways that
  1594. ** the Tcl channel interface does not. The first argument should
  1595. ** be the name of a valid channel created by the [incrblob] method
  1596. ** of a database handle. This function calls sqlite3_blob_write()
  1597. ** to write the DATA byte-array to the underlying SQLite blob handle.
  1598. ** at offset OFFSET.
  1599. **
  1600. ** On success, an empty string is returned. On failure, the interpreter
  1601. ** result is set to the text representation of the returned error code
  1602. ** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown.
  1603. */
  1604. static int test_blob_write(
  1605. ClientData clientData, /* Not used */
  1606. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1607. int objc, /* Number of arguments */
  1608. Tcl_Obj *CONST objv[] /* Command arguments */
  1609. ){
  1610. sqlite3_blob *pBlob;
  1611. int iOffset;
  1612. int rc;
  1613. unsigned char *zBuf;
  1614. int nBuf;
  1615. if( objc!=4 && objc!=5 ){
  1616. Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL OFFSET DATA ?NDATA?");
  1617. return TCL_ERROR;
  1618. }
  1619. if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
  1620. if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &iOffset) ){
  1621. return TCL_ERROR;
  1622. }
  1623. zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf);
  1624. if( objc==5 && Tcl_GetIntFromObj(interp, objv[4], &nBuf) ){
  1625. return TCL_ERROR;
  1626. }
  1627. rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset);
  1628. if( rc!=SQLITE_OK ){
  1629. Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
  1630. }
  1631. return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
  1632. }
  1633. static int test_blob_reopen(
  1634. ClientData clientData, /* Not used */
  1635. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1636. int objc, /* Number of arguments */
  1637. Tcl_Obj *CONST objv[] /* Command arguments */
  1638. ){
  1639. Tcl_WideInt iRowid;
  1640. sqlite3_blob *pBlob;
  1641. int rc;
  1642. if( objc!=3 ){
  1643. Tcl_WrongNumArgs(interp, 1, objv, "CHANNEL ROWID");
  1644. return TCL_ERROR;
  1645. }
  1646. if( blobHandleFromObj(interp, objv[1], &pBlob) ) return TCL_ERROR;
  1647. if( Tcl_GetWideIntFromObj(interp, objv[2], &iRowid) ) return TCL_ERROR;
  1648. rc = sqlite3_blob_reopen(pBlob, iRowid);
  1649. if( rc!=SQLITE_OK ){
  1650. Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
  1651. }
  1652. return (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
  1653. }
  1654. #endif
  1655. /*
  1656. ** Usage: sqlite3_create_collation_v2 DB-HANDLE NAME CMP-PROC DEL-PROC
  1657. **
  1658. ** This Tcl proc is used for testing the experimental
  1659. ** sqlite3_create_collation_v2() interface.
  1660. */
  1661. struct TestCollationX {
  1662. Tcl_Interp *interp;
  1663. Tcl_Obj *pCmp;
  1664. Tcl_Obj *pDel;
  1665. };
  1666. typedef struct TestCollationX TestCollationX;
  1667. static void testCreateCollationDel(void *pCtx){
  1668. TestCollationX *p = (TestCollationX *)pCtx;
  1669. int rc = Tcl_EvalObjEx(p->interp, p->pDel, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
  1670. if( rc!=TCL_OK ){
  1671. Tcl_BackgroundError(p->interp);
  1672. }
  1673. Tcl_DecrRefCount(p->pCmp);
  1674. Tcl_DecrRefCount(p->pDel);
  1675. sqlite3_free((void *)p);
  1676. }
  1677. static int testCreateCollationCmp(
  1678. void *pCtx,
  1679. int nLeft,
  1680. const void *zLeft,
  1681. int nRight,
  1682. const void *zRight
  1683. ){
  1684. TestCollationX *p = (TestCollationX *)pCtx;
  1685. Tcl_Obj *pScript = Tcl_DuplicateObj(p->pCmp);
  1686. int iRes = 0;
  1687. Tcl_IncrRefCount(pScript);
  1688. Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zLeft, nLeft));
  1689. Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj((char *)zRight,nRight));
  1690. if( TCL_OK!=Tcl_EvalObjEx(p->interp, pScript, TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL)
  1691. || TCL_OK!=Tcl_GetIntFromObj(p->interp, Tcl_GetObjResult(p->interp), &iRes)
  1692. ){
  1693. Tcl_BackgroundError(p->interp);
  1694. }
  1695. Tcl_DecrRefCount(pScript);
  1696. return iRes;
  1697. }
  1698. static int test_create_collation_v2(
  1699. ClientData clientData, /* Not used */
  1700. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1701. int objc, /* Number of arguments */
  1702. Tcl_Obj *CONST objv[] /* Command arguments */
  1703. ){
  1704. TestCollationX *p;
  1705. sqlite3 *db;
  1706. int rc;
  1707. if( objc!=5 ){
  1708. Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE NAME CMP-PROC DEL-PROC");
  1709. return TCL_ERROR;
  1710. }
  1711. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1712. p = (TestCollationX *)sqlite3_malloc(sizeof(TestCollationX));
  1713. p->pCmp = objv[3];
  1714. p->pDel = objv[4];
  1715. p->interp = interp;
  1716. Tcl_IncrRefCount(p->pCmp);
  1717. Tcl_IncrRefCount(p->pDel);
  1718. rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), 16,
  1719. (void *)p, testCreateCollationCmp, testCreateCollationDel
  1720. );
  1721. if( rc!=SQLITE_MISUSE ){
  1722. Tcl_AppendResult(interp, "sqlite3_create_collate_v2() failed to detect "
  1723. "an invalid encoding", (char*)0);
  1724. return TCL_ERROR;
  1725. }
  1726. rc = sqlite3_create_collation_v2(db, Tcl_GetString(objv[2]), SQLITE_UTF8,
  1727. (void *)p, testCreateCollationCmp, testCreateCollationDel
  1728. );
  1729. return TCL_OK;
  1730. }
  1731. /*
  1732. ** USAGE: sqlite3_create_function_v2 DB NAME NARG ENC ?SWITCHES?
  1733. **
  1734. ** Available switches are:
  1735. **
  1736. ** -func SCRIPT
  1737. ** -step SCRIPT
  1738. ** -final SCRIPT
  1739. ** -destroy SCRIPT
  1740. */
  1741. typedef struct CreateFunctionV2 CreateFunctionV2;
  1742. struct CreateFunctionV2 {
  1743. Tcl_Interp *interp;
  1744. Tcl_Obj *pFunc; /* Script for function invocation */
  1745. Tcl_Obj *pStep; /* Script for agg. step invocation */
  1746. Tcl_Obj *pFinal; /* Script for agg. finalization invocation */
  1747. Tcl_Obj *pDestroy; /* Destructor script */
  1748. };
  1749. static void cf2Func(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
  1750. }
  1751. static void cf2Step(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
  1752. }
  1753. static void cf2Final(sqlite3_context *ctx){
  1754. }
  1755. static void cf2Destroy(void *pUser){
  1756. CreateFunctionV2 *p = (CreateFunctionV2 *)pUser;
  1757. if( p->interp && p->pDestroy ){
  1758. int rc = Tcl_EvalObjEx(p->interp, p->pDestroy, 0);
  1759. if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
  1760. }
  1761. if( p->pFunc ) Tcl_DecrRefCount(p->pFunc);
  1762. if( p->pStep ) Tcl_DecrRefCount(p->pStep);
  1763. if( p->pFinal ) Tcl_DecrRefCount(p->pFinal);
  1764. if( p->pDestroy ) Tcl_DecrRefCount(p->pDestroy);
  1765. sqlite3_free(p);
  1766. }
  1767. static int test_create_function_v2(
  1768. ClientData clientData, /* Not used */
  1769. Tcl_Interp *interp, /* The invoking TCL interpreter */
  1770. int objc, /* Number of arguments */
  1771. Tcl_Obj *CONST objv[] /* Command arguments */
  1772. ){
  1773. sqlite3 *db;
  1774. const char *zFunc;
  1775. int nArg;
  1776. int enc;
  1777. CreateFunctionV2 *p;
  1778. int i;
  1779. int rc;
  1780. struct EncTable {
  1781. const char *zEnc;
  1782. int enc;
  1783. } aEnc[] = {
  1784. {"utf8", SQLITE_UTF8 },
  1785. {"utf16", SQLITE_UTF16 },
  1786. {"utf16le", SQLITE_UTF16LE },
  1787. {"utf16be", SQLITE_UTF16BE },
  1788. {"any", SQLITE_ANY },
  1789. {"0", 0 }
  1790. };
  1791. if( objc<5 || (objc%2)==0 ){
  1792. Tcl_WrongNumArgs(interp, 1, objv, "DB NAME NARG ENC SWITCHES...");
  1793. return TCL_ERROR;
  1794. }
  1795. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1796. zFunc = Tcl_GetString(objv[2]);
  1797. if( Tcl_GetIntFromObj(interp, objv[3], &nArg) ) return TCL_ERROR;
  1798. if( Tcl_GetIndexFromObjStruct(interp, objv[4], aEnc, sizeof(aEnc[0]),
  1799. "encoding", 0, &enc)
  1800. ){
  1801. return TCL_ERROR;
  1802. }
  1803. enc = aEnc[enc].enc;
  1804. p = sqlite3_malloc(sizeof(CreateFunctionV2));
  1805. assert( p );
  1806. memset(p, 0, sizeof(CreateFunctionV2));
  1807. p->interp = interp;
  1808. for(i=5; i<objc; i+=2){
  1809. int iSwitch;
  1810. const char *azSwitch[] = {"-func", "-step", "-final", "-destroy", 0};
  1811. if( Tcl_GetIndexFromObj(interp, objv[i], azSwitch, "switch", 0, &iSwitch) ){
  1812. sqlite3_free(p);
  1813. return TCL_ERROR;
  1814. }
  1815. switch( iSwitch ){
  1816. case 0: p->pFunc = objv[i+1]; break;
  1817. case 1: p->pStep = objv[i+1]; break;
  1818. case 2: p->pFinal = objv[i+1]; break;
  1819. case 3: p->pDestroy = objv[i+1]; break;
  1820. }
  1821. }
  1822. if( p->pFunc ) p->pFunc = Tcl_DuplicateObj(p->pFunc);
  1823. if( p->pStep ) p->pStep = Tcl_DuplicateObj(p->pStep);
  1824. if( p->pFinal ) p->pFinal = Tcl_DuplicateObj(p->pFinal);
  1825. if( p->pDestroy ) p->pDestroy = Tcl_DuplicateObj(p->pDestroy);
  1826. if( p->pFunc ) Tcl_IncrRefCount(p->pFunc);
  1827. if( p->pStep ) Tcl_IncrRefCount(p->pStep);
  1828. if( p->pFinal ) Tcl_IncrRefCount(p->pFinal);
  1829. if( p->pDestroy ) Tcl_IncrRefCount(p->pDestroy);
  1830. rc = sqlite3_create_function_v2(db, zFunc, nArg, enc, (void *)p,
  1831. (p->pFunc ? cf2Func : 0),
  1832. (p->pStep ? cf2Step : 0),
  1833. (p->pFinal ? cf2Final : 0),
  1834. cf2Destroy
  1835. );
  1836. if( rc!=SQLITE_OK ){
  1837. Tcl_ResetResult(interp);
  1838. Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
  1839. return TCL_ERROR;
  1840. }
  1841. return TCL_OK;
  1842. }
  1843. /*
  1844. ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC?
  1845. */
  1846. static int test_load_extension(
  1847. ClientData clientData, /* Not used */
  1848. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1849. int objc, /* Number of arguments */
  1850. Tcl_Obj *CONST objv[] /* Command arguments */
  1851. ){
  1852. Tcl_CmdInfo cmdInfo;
  1853. sqlite3 *db;
  1854. int rc;
  1855. char *zDb;
  1856. char *zFile;
  1857. char *zProc = 0;
  1858. char *zErr = 0;
  1859. if( objc!=4 && objc!=3 ){
  1860. Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE FILE ?PROC?");
  1861. return TCL_ERROR;
  1862. }
  1863. zDb = Tcl_GetString(objv[1]);
  1864. zFile = Tcl_GetString(objv[2]);
  1865. if( objc==4 ){
  1866. zProc = Tcl_GetString(objv[3]);
  1867. }
  1868. /* Extract the C database handle from the Tcl command name */
  1869. if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
  1870. Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
  1871. return TCL_ERROR;
  1872. }
  1873. db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
  1874. assert(db);
  1875. /* Call the underlying C function. If an error occurs, set rc to
  1876. ** TCL_ERROR and load any error string into the interpreter. If no
  1877. ** error occurs, set rc to TCL_OK.
  1878. */
  1879. #ifdef SQLITE_OMIT_LOAD_EXTENSION
  1880. rc = SQLITE_ERROR;
  1881. zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()");
  1882. #else
  1883. rc = sqlite3_load_extension(db, zFile, zProc, &zErr);
  1884. #endif
  1885. if( rc!=SQLITE_OK ){
  1886. Tcl_SetResult(interp, zErr ? zErr : "", TCL_VOLATILE);
  1887. rc = TCL_ERROR;
  1888. }else{
  1889. rc = TCL_OK;
  1890. }
  1891. sqlite3_free(zErr);
  1892. return rc;
  1893. }
  1894. /*
  1895. ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF
  1896. */
  1897. static int test_enable_load(
  1898. ClientData clientData, /* Not used */
  1899. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1900. int objc, /* Number of arguments */
  1901. Tcl_Obj *CONST objv[] /* Command arguments */
  1902. ){
  1903. Tcl_CmdInfo cmdInfo;
  1904. sqlite3 *db;
  1905. char *zDb;
  1906. int onoff;
  1907. if( objc!=3 ){
  1908. Tcl_WrongNumArgs(interp, 1, objv, "DB-HANDLE ONOFF");
  1909. return TCL_ERROR;
  1910. }
  1911. zDb = Tcl_GetString(objv[1]);
  1912. /* Extract the C database handle from the Tcl command name */
  1913. if( !Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
  1914. Tcl_AppendResult(interp, "command not found: ", zDb, (char*)0);
  1915. return TCL_ERROR;
  1916. }
  1917. db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
  1918. assert(db);
  1919. /* Get the onoff parameter */
  1920. if( Tcl_GetBooleanFromObj(interp, objv[2], &onoff) ){
  1921. return TCL_ERROR;
  1922. }
  1923. #ifdef SQLITE_OMIT_LOAD_EXTENSION
  1924. Tcl_AppendResult(interp, "this build omits sqlite3_load_extension()");
  1925. return TCL_ERROR;
  1926. #else
  1927. sqlite3_enable_load_extension(db, onoff);
  1928. return TCL_OK;
  1929. #endif
  1930. }
  1931. /*
  1932. ** Usage: sqlite_abort
  1933. **
  1934. ** Shutdown the process immediately. This is not a clean shutdown.
  1935. ** This command is used to test the recoverability of a database in
  1936. ** the event of a program crash.
  1937. */
  1938. static int sqlite_abort(
  1939. void *NotUsed,
  1940. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1941. int argc, /* Number of arguments */
  1942. char **argv /* Text of each argument */
  1943. ){
  1944. #if defined(_MSC_VER)
  1945. /* We do this, otherwise the test will halt with a popup message
  1946. * that we have to click away before the test will continue.
  1947. */
  1948. _set_abort_behavior( 0, _CALL_REPORTFAULT );
  1949. #endif
  1950. exit(255);
  1951. assert( interp==0 ); /* This will always fail */
  1952. return TCL_OK;
  1953. }
  1954. /*
  1955. ** The following routine is a user-defined SQL function whose purpose
  1956. ** is to test the sqlite_set_result() API.
  1957. */
  1958. static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
  1959. while( argc>=2 ){
  1960. const char *zArg0 = (char*)sqlite3_value_text(argv[0]);
  1961. if( zArg0 ){
  1962. if( 0==sqlite3StrICmp(zArg0, "int") ){
  1963. sqlite3_result_int(context, sqlite3_value_int(argv[1]));
  1964. }else if( sqlite3StrICmp(zArg0,"int64")==0 ){
  1965. sqlite3_result_int64(context, sqlite3_value_int64(argv[1]));
  1966. }else if( sqlite3StrICmp(zArg0,"string")==0 ){
  1967. sqlite3_result_text(context, (char*)sqlite3_value_text(argv[1]), -1,
  1968. SQLITE_TRANSIENT);
  1969. }else if( sqlite3StrICmp(zArg0,"double")==0 ){
  1970. sqlite3_result_double(context, sqlite3_value_double(argv[1]));
  1971. }else if( sqlite3StrICmp(zArg0,"null")==0 ){
  1972. sqlite3_result_null(context);
  1973. }else if( sqlite3StrICmp(zArg0,"value")==0 ){
  1974. sqlite3_result_value(context, argv[sqlite3_value_int(argv[1])]);
  1975. }else{
  1976. goto error_out;
  1977. }
  1978. }else{
  1979. goto error_out;
  1980. }
  1981. argc -= 2;
  1982. argv += 2;
  1983. }
  1984. return;
  1985. error_out:
  1986. sqlite3_result_error(context,"first argument should be one of: "
  1987. "int int64 string double null value", -1);
  1988. }
  1989. /*
  1990. ** Usage: sqlite_register_test_function DB NAME
  1991. **
  1992. ** Register the test SQL function on the database DB under the name NAME.
  1993. */
  1994. static int test_register_func(
  1995. void *NotUsed,
  1996. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  1997. int argc, /* Number of arguments */
  1998. char **argv /* Text of each argument */
  1999. ){
  2000. sqlite3 *db;
  2001. int rc;
  2002. if( argc!=3 ){
  2003. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  2004. " DB FUNCTION-NAME", 0);
  2005. return TCL_ERROR;
  2006. }
  2007. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  2008. rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0,
  2009. testFunc, 0, 0);
  2010. if( rc!=0 ){
  2011. Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
  2012. return TCL_ERROR;
  2013. }
  2014. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  2015. return TCL_OK;
  2016. }
  2017. /*
  2018. ** Usage: sqlite3_finalize STMT
  2019. **
  2020. ** Finalize a statement handle.
  2021. */
  2022. static int test_finalize(
  2023. void * clientData,
  2024. Tcl_Interp *interp,
  2025. int objc,
  2026. Tcl_Obj *CONST objv[]
  2027. ){
  2028. sqlite3_stmt *pStmt;
  2029. int rc;
  2030. sqlite3 *db = 0;
  2031. if( objc!=2 ){
  2032. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2033. Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
  2034. return TCL_ERROR;
  2035. }
  2036. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2037. if( pStmt ){
  2038. db = StmtToDb(pStmt);
  2039. }
  2040. rc = sqlite3_finalize(pStmt);
  2041. Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  2042. if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  2043. return TCL_OK;
  2044. }
  2045. /*
  2046. ** Usage: sqlite3_stmt_status STMT CODE RESETFLAG
  2047. **
  2048. ** Get the value of a status counter from a statement.
  2049. */
  2050. static int test_stmt_status(
  2051. void * clientData,
  2052. Tcl_Interp *interp,
  2053. int objc,
  2054. Tcl_Obj *CONST objv[]
  2055. ){
  2056. int iValue;
  2057. int i, op, resetFlag;
  2058. const char *zOpName;
  2059. sqlite3_stmt *pStmt;
  2060. static const struct {
  2061. const char *zName;
  2062. int op;
  2063. } aOp[] = {
  2064. { "SQLITE_STMTSTATUS_FULLSCAN_STEP", SQLITE_STMTSTATUS_FULLSCAN_STEP },
  2065. { "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT },
  2066. { "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX },
  2067. { "SQLITE_STMTSTATUS_VM_STEP", SQLITE_STMTSTATUS_VM_STEP },
  2068. };
  2069. if( objc!=4 ){
  2070. Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG");
  2071. return TCL_ERROR;
  2072. }
  2073. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2074. zOpName = Tcl_GetString(objv[2]);
  2075. for(i=0; i<ArraySize(aOp); i++){
  2076. if( strcmp(aOp[i].zName, zOpName)==0 ){
  2077. op = aOp[i].op;
  2078. break;
  2079. }
  2080. }
  2081. if( i>=ArraySize(aOp) ){
  2082. if( Tcl_GetIntFromObj(interp, objv[2], &op) ) return TCL_ERROR;
  2083. }
  2084. if( Tcl_GetBooleanFromObj(interp, objv[3], &resetFlag) ) return TCL_ERROR;
  2085. iValue = sqlite3_stmt_status(pStmt, op, resetFlag);
  2086. Tcl_SetObjResult(interp, Tcl_NewIntObj(iValue));
  2087. return TCL_OK;
  2088. }
  2089. /*
  2090. ** Usage: sqlite3_next_stmt DB STMT
  2091. **
  2092. ** Return the next statment in sequence after STMT.
  2093. */
  2094. static int test_next_stmt(
  2095. void * clientData,
  2096. Tcl_Interp *interp,
  2097. int objc,
  2098. Tcl_Obj *CONST objv[]
  2099. ){
  2100. sqlite3_stmt *pStmt;
  2101. sqlite3 *db = 0;
  2102. char zBuf[50];
  2103. if( objc!=3 ){
  2104. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2105. Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0);
  2106. return TCL_ERROR;
  2107. }
  2108. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  2109. if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR;
  2110. pStmt = sqlite3_next_stmt(db, pStmt);
  2111. if( pStmt ){
  2112. if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
  2113. Tcl_AppendResult(interp, zBuf, 0);
  2114. }
  2115. return TCL_OK;
  2116. }
  2117. /*
  2118. ** Usage: sqlite3_stmt_readonly STMT
  2119. **
  2120. ** Return true if STMT is a NULL pointer or a pointer to a statement
  2121. ** that is guaranteed to leave the database unmodified.
  2122. */
  2123. static int test_stmt_readonly(
  2124. void * clientData,
  2125. Tcl_Interp *interp,
  2126. int objc,
  2127. Tcl_Obj *CONST objv[]
  2128. ){
  2129. sqlite3_stmt *pStmt;
  2130. int rc;
  2131. if( objc!=2 ){
  2132. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2133. Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
  2134. return TCL_ERROR;
  2135. }
  2136. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2137. rc = sqlite3_stmt_readonly(pStmt);
  2138. Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc));
  2139. return TCL_OK;
  2140. }
  2141. /*
  2142. ** Usage: sqlite3_stmt_busy STMT
  2143. **
  2144. ** Return true if STMT is a non-NULL pointer to a statement
  2145. ** that has been stepped but not to completion.
  2146. */
  2147. static int test_stmt_busy(
  2148. void * clientData,
  2149. Tcl_Interp *interp,
  2150. int objc,
  2151. Tcl_Obj *CONST objv[]
  2152. ){
  2153. sqlite3_stmt *pStmt;
  2154. int rc;
  2155. if( objc!=2 ){
  2156. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2157. Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
  2158. return TCL_ERROR;
  2159. }
  2160. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2161. rc = sqlite3_stmt_busy(pStmt);
  2162. Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc));
  2163. return TCL_OK;
  2164. }
  2165. /*
  2166. ** Usage: uses_stmt_journal STMT
  2167. **
  2168. ** Return true if STMT uses a statement journal.
  2169. */
  2170. static int uses_stmt_journal(
  2171. void * clientData,
  2172. Tcl_Interp *interp,
  2173. int objc,
  2174. Tcl_Obj *CONST objv[]
  2175. ){
  2176. sqlite3_stmt *pStmt;
  2177. if( objc!=2 ){
  2178. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2179. Tcl_GetStringFromObj(objv[0], 0), " STMT", 0);
  2180. return TCL_ERROR;
  2181. }
  2182. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2183. sqlite3_stmt_readonly(pStmt);
  2184. Tcl_SetObjResult(interp, Tcl_NewBooleanObj(((Vdbe *)pStmt)->usesStmtJournal));
  2185. return TCL_OK;
  2186. }
  2187. /*
  2188. ** Usage: sqlite3_reset STMT
  2189. **
  2190. ** Reset a statement handle.
  2191. */
  2192. static int test_reset(
  2193. void * clientData,
  2194. Tcl_Interp *interp,
  2195. int objc,
  2196. Tcl_Obj *CONST objv[]
  2197. ){
  2198. sqlite3_stmt *pStmt;
  2199. int rc;
  2200. if( objc!=2 ){
  2201. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2202. Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
  2203. return TCL_ERROR;
  2204. }
  2205. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2206. rc = sqlite3_reset(pStmt);
  2207. if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
  2208. return TCL_ERROR;
  2209. }
  2210. Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  2211. /*
  2212. if( rc ){
  2213. return TCL_ERROR;
  2214. }
  2215. */
  2216. return TCL_OK;
  2217. }
  2218. /*
  2219. ** Usage: sqlite3_expired STMT
  2220. **
  2221. ** Return TRUE if a recompilation of the statement is recommended.
  2222. */
  2223. static int test_expired(
  2224. void * clientData,
  2225. Tcl_Interp *interp,
  2226. int objc,
  2227. Tcl_Obj *CONST objv[]
  2228. ){
  2229. #ifndef SQLITE_OMIT_DEPRECATED
  2230. sqlite3_stmt *pStmt;
  2231. if( objc!=2 ){
  2232. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2233. Tcl_GetStringFromObj(objv[0], 0), " <STMT>", 0);
  2234. return TCL_ERROR;
  2235. }
  2236. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2237. Tcl_SetObjResult(interp, Tcl_NewBooleanObj(sqlite3_expired(pStmt)));
  2238. #endif
  2239. return TCL_OK;
  2240. }
  2241. /*
  2242. ** Usage: sqlite3_transfer_bindings FROMSTMT TOSTMT
  2243. **
  2244. ** Transfer all bindings from FROMSTMT over to TOSTMT
  2245. */
  2246. static int test_transfer_bind(
  2247. void * clientData,
  2248. Tcl_Interp *interp,
  2249. int objc,
  2250. Tcl_Obj *CONST objv[]
  2251. ){
  2252. #ifndef SQLITE_OMIT_DEPRECATED
  2253. sqlite3_stmt *pStmt1, *pStmt2;
  2254. if( objc!=3 ){
  2255. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2256. Tcl_GetStringFromObj(objv[0], 0), " FROM-STMT TO-STMT", 0);
  2257. return TCL_ERROR;
  2258. }
  2259. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt1)) return TCL_ERROR;
  2260. if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt2)) return TCL_ERROR;
  2261. Tcl_SetObjResult(interp,
  2262. Tcl_NewIntObj(sqlite3_transfer_bindings(pStmt1,pStmt2)));
  2263. #endif
  2264. return TCL_OK;
  2265. }
  2266. /*
  2267. ** Usage: sqlite3_changes DB
  2268. **
  2269. ** Return the number of changes made to the database by the last SQL
  2270. ** execution.
  2271. */
  2272. static int test_changes(
  2273. void * clientData,
  2274. Tcl_Interp *interp,
  2275. int objc,
  2276. Tcl_Obj *CONST objv[]
  2277. ){
  2278. sqlite3 *db;
  2279. if( objc!=2 ){
  2280. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2281. Tcl_GetString(objv[0]), " DB", 0);
  2282. return TCL_ERROR;
  2283. }
  2284. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  2285. Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_changes(db)));
  2286. return TCL_OK;
  2287. }
  2288. /*
  2289. ** This is the "static_bind_value" that variables are bound to when
  2290. ** the FLAG option of sqlite3_bind is "static"
  2291. */
  2292. static char *sqlite_static_bind_value = 0;
  2293. static int sqlite_static_bind_nbyte = 0;
  2294. /*
  2295. ** Usage: sqlite3_bind VM IDX VALUE FLAGS
  2296. **
  2297. ** Sets the value of the IDX-th occurrence of "?" in the original SQL
  2298. ** string. VALUE is the new value. If FLAGS=="null" then VALUE is
  2299. ** ignored and the value is set to NULL. If FLAGS=="static" then
  2300. ** the value is set to the value of a static variable named
  2301. ** "sqlite_static_bind_value". If FLAGS=="normal" then a copy
  2302. ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored
  2303. ** an a 10-byte blob "abc\000xyz\000pq" is inserted.
  2304. */
  2305. static int test_bind(
  2306. void *NotUsed,
  2307. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  2308. int argc, /* Number of arguments */
  2309. char **argv /* Text of each argument */
  2310. ){
  2311. sqlite3_stmt *pStmt;
  2312. int rc;
  2313. int idx;
  2314. if( argc!=5 ){
  2315. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  2316. " VM IDX VALUE (null|static|normal)\"", 0);
  2317. return TCL_ERROR;
  2318. }
  2319. if( getStmtPointer(interp, argv[1], &pStmt) ) return TCL_ERROR;
  2320. if( Tcl_GetInt(interp, argv[2], &idx) ) return TCL_ERROR;
  2321. if( strcmp(argv[4],"null")==0 ){
  2322. rc = sqlite3_bind_null(pStmt, idx);
  2323. }else if( strcmp(argv[4],"static")==0 ){
  2324. rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value, -1, 0);
  2325. }else if( strcmp(argv[4],"static-nbytes")==0 ){
  2326. rc = sqlite3_bind_text(pStmt, idx, sqlite_static_bind_value,
  2327. sqlite_static_bind_nbyte, 0);
  2328. }else if( strcmp(argv[4],"normal")==0 ){
  2329. rc = sqlite3_bind_text(pStmt, idx, argv[3], -1, SQLITE_TRANSIENT);
  2330. }else if( strcmp(argv[4],"blob10")==0 ){
  2331. rc = sqlite3_bind_text(pStmt, idx, "abc\000xyz\000pq", 10, SQLITE_STATIC);
  2332. }else{
  2333. Tcl_AppendResult(interp, "4th argument should be "
  2334. "\"null\" or \"static\" or \"normal\"", 0);
  2335. return TCL_ERROR;
  2336. }
  2337. if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  2338. if( rc ){
  2339. char zBuf[50];
  2340. sprintf(zBuf, "(%d) ", rc);
  2341. Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0);
  2342. return TCL_ERROR;
  2343. }
  2344. return TCL_OK;
  2345. }
  2346. #ifndef SQLITE_OMIT_UTF16
  2347. /*
  2348. ** Usage: add_test_collate <db ptr> <utf8> <utf16le> <utf16be>
  2349. **
  2350. ** This function is used to test that SQLite selects the correct collation
  2351. ** sequence callback when multiple versions (for different text encodings)
  2352. ** are available.
  2353. **
  2354. ** Calling this routine registers the collation sequence "test_collate"
  2355. ** with database handle <db>. The second argument must be a list of three
  2356. ** boolean values. If the first is true, then a version of test_collate is
  2357. ** registered for UTF-8, if the second is true, a version is registered for
  2358. ** UTF-16le, if the third is true, a UTF-16be version is available.
  2359. ** Previous versions of test_collate are deleted.
  2360. **
  2361. ** The collation sequence test_collate is implemented by calling the
  2362. ** following TCL script:
  2363. **
  2364. ** "test_collate <enc> <lhs> <rhs>"
  2365. **
  2366. ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
  2367. ** The <enc> parameter is the encoding of the collation function that
  2368. ** SQLite selected to call. The TCL test script implements the
  2369. ** "test_collate" proc.
  2370. **
  2371. ** Note that this will only work with one intepreter at a time, as the
  2372. ** interp pointer to use when evaluating the TCL script is stored in
  2373. ** pTestCollateInterp.
  2374. */
  2375. static Tcl_Interp* pTestCollateInterp;
  2376. static int test_collate_func(
  2377. void *pCtx,
  2378. int nA, const void *zA,
  2379. int nB, const void *zB
  2380. ){
  2381. Tcl_Interp *i = pTestCollateInterp;
  2382. int encin = SQLITE_PTR_TO_INT(pCtx);
  2383. int res;
  2384. int n;
  2385. sqlite3_value *pVal;
  2386. Tcl_Obj *pX;
  2387. pX = Tcl_NewStringObj("test_collate", -1);
  2388. Tcl_IncrRefCount(pX);
  2389. switch( encin ){
  2390. case SQLITE_UTF8:
  2391. Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-8",-1));
  2392. break;
  2393. case SQLITE_UTF16LE:
  2394. Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16LE",-1));
  2395. break;
  2396. case SQLITE_UTF16BE:
  2397. Tcl_ListObjAppendElement(i,pX,Tcl_NewStringObj("UTF-16BE",-1));
  2398. break;
  2399. default:
  2400. assert(0);
  2401. }
  2402. sqlite3BeginBenignMalloc();
  2403. pVal = sqlite3ValueNew(0);
  2404. if( pVal ){
  2405. sqlite3ValueSetStr(pVal, nA, zA, encin, SQLITE_STATIC);
  2406. n = sqlite3_value_bytes(pVal);
  2407. Tcl_ListObjAppendElement(i,pX,
  2408. Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
  2409. sqlite3ValueSetStr(pVal, nB, zB, encin, SQLITE_STATIC);
  2410. n = sqlite3_value_bytes(pVal);
  2411. Tcl_ListObjAppendElement(i,pX,
  2412. Tcl_NewStringObj((char*)sqlite3_value_text(pVal),n));
  2413. sqlite3ValueFree(pVal);
  2414. }
  2415. sqlite3EndBenignMalloc();
  2416. Tcl_EvalObjEx(i, pX, 0);
  2417. Tcl_DecrRefCount(pX);
  2418. Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res);
  2419. return res;
  2420. }
  2421. static int test_collate(
  2422. void * clientData,
  2423. Tcl_Interp *interp,
  2424. int objc,
  2425. Tcl_Obj *CONST objv[]
  2426. ){
  2427. sqlite3 *db;
  2428. int val;
  2429. sqlite3_value *pVal;
  2430. int rc;
  2431. if( objc!=5 ) goto bad_args;
  2432. pTestCollateInterp = interp;
  2433. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  2434. if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
  2435. rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF8,
  2436. (void *)SQLITE_UTF8, val?test_collate_func:0);
  2437. if( rc==SQLITE_OK ){
  2438. const void *zUtf16;
  2439. if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
  2440. rc = sqlite3_create_collation(db, "test_collate", SQLITE_UTF16LE,
  2441. (void *)SQLITE_UTF16LE, val?test_collate_func:0);
  2442. if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
  2443. #if 0
  2444. if( sqlite3_iMallocFail>0 ){
  2445. sqlite3_iMallocFail++;
  2446. }
  2447. #endif
  2448. sqlite3_mutex_enter(db->mutex);
  2449. pVal = sqlite3ValueNew(db);
  2450. sqlite3ValueSetStr(pVal, -1, "test_collate", SQLITE_UTF8, SQLITE_STATIC);
  2451. zUtf16 = sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
  2452. if( db->mallocFailed ){
  2453. rc = SQLITE_NOMEM;
  2454. }else{
  2455. rc = sqlite3_create_collation16(db, zUtf16, SQLITE_UTF16BE,
  2456. (void *)SQLITE_UTF16BE, val?test_collate_func:0);
  2457. }
  2458. sqlite3ValueFree(pVal);
  2459. sqlite3_mutex_leave(db->mutex);
  2460. }
  2461. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  2462. if( rc!=SQLITE_OK ){
  2463. Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
  2464. return TCL_ERROR;
  2465. }
  2466. return TCL_OK;
  2467. bad_args:
  2468. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2469. Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
  2470. return TCL_ERROR;
  2471. }
  2472. /*
  2473. ** When the collation needed callback is invoked, record the name of
  2474. ** the requested collating function here. The recorded name is linked
  2475. ** to a TCL variable and used to make sure that the requested collation
  2476. ** name is correct.
  2477. */
  2478. static char zNeededCollation[200];
  2479. static char *pzNeededCollation = zNeededCollation;
  2480. /*
  2481. ** Called when a collating sequence is needed. Registered using
  2482. ** sqlite3_collation_needed16().
  2483. */
  2484. static void test_collate_needed_cb(
  2485. void *pCtx,
  2486. sqlite3 *db,
  2487. int eTextRep,
  2488. const void *pName
  2489. ){
  2490. int enc = ENC(db);
  2491. int i;
  2492. char *z;
  2493. for(z = (char*)pName, i=0; *z || z[1]; z++){
  2494. if( *z ) zNeededCollation[i++] = *z;
  2495. }
  2496. zNeededCollation[i] = 0;
  2497. sqlite3_create_collation(
  2498. db, "test_collate", ENC(db), SQLITE_INT_TO_PTR(enc), test_collate_func);
  2499. }
  2500. /*
  2501. ** Usage: add_test_collate_needed DB
  2502. */
  2503. static int test_collate_needed(
  2504. void * clientData,
  2505. Tcl_Interp *interp,
  2506. int objc,
  2507. Tcl_Obj *CONST objv[]
  2508. ){
  2509. sqlite3 *db;
  2510. int rc;
  2511. if( objc!=2 ) goto bad_args;
  2512. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  2513. rc = sqlite3_collation_needed16(db, 0, test_collate_needed_cb);
  2514. zNeededCollation[0] = 0;
  2515. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  2516. return TCL_OK;
  2517. bad_args:
  2518. Tcl_WrongNumArgs(interp, 1, objv, "DB");
  2519. return TCL_ERROR;
  2520. }
  2521. /*
  2522. ** tclcmd: add_alignment_test_collations DB
  2523. **
  2524. ** Add two new collating sequences to the database DB
  2525. **
  2526. ** utf16_aligned
  2527. ** utf16_unaligned
  2528. **
  2529. ** Both collating sequences use the same sort order as BINARY.
  2530. ** The only difference is that the utf16_aligned collating
  2531. ** sequence is declared with the SQLITE_UTF16_ALIGNED flag.
  2532. ** Both collating functions increment the unaligned utf16 counter
  2533. ** whenever they see a string that begins on an odd byte boundary.
  2534. */
  2535. static int unaligned_string_counter = 0;
  2536. static int alignmentCollFunc(
  2537. void *NotUsed,
  2538. int nKey1, const void *pKey1,
  2539. int nKey2, const void *pKey2
  2540. ){
  2541. int rc, n;
  2542. n = nKey1<nKey2 ? nKey1 : nKey2;
  2543. if( nKey1>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey1))) ) unaligned_string_counter++;
  2544. if( nKey2>0 && 1==(1&(SQLITE_PTR_TO_INT(pKey2))) ) unaligned_string_counter++;
  2545. rc = memcmp(pKey1, pKey2, n);
  2546. if( rc==0 ){
  2547. rc = nKey1 - nKey2;
  2548. }
  2549. return rc;
  2550. }
  2551. static int add_alignment_test_collations(
  2552. void * clientData,
  2553. Tcl_Interp *interp,
  2554. int objc,
  2555. Tcl_Obj *CONST objv[]
  2556. ){
  2557. sqlite3 *db;
  2558. if( objc>=2 ){
  2559. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  2560. sqlite3_create_collation(db, "utf16_unaligned", SQLITE_UTF16,
  2561. 0, alignmentCollFunc);
  2562. sqlite3_create_collation(db, "utf16_aligned", SQLITE_UTF16_ALIGNED,
  2563. 0, alignmentCollFunc);
  2564. }
  2565. return SQLITE_OK;
  2566. }
  2567. #endif /* !defined(SQLITE_OMIT_UTF16) */
  2568. /*
  2569. ** Usage: add_test_function <db ptr> <utf8> <utf16le> <utf16be>
  2570. **
  2571. ** This function is used to test that SQLite selects the correct user
  2572. ** function callback when multiple versions (for different text encodings)
  2573. ** are available.
  2574. **
  2575. ** Calling this routine registers up to three versions of the user function
  2576. ** "test_function" with database handle <db>. If the second argument is
  2577. ** true, then a version of test_function is registered for UTF-8, if the
  2578. ** third is true, a version is registered for UTF-16le, if the fourth is
  2579. ** true, a UTF-16be version is available. Previous versions of
  2580. ** test_function are deleted.
  2581. **
  2582. ** The user function is implemented by calling the following TCL script:
  2583. **
  2584. ** "test_function <enc> <arg>"
  2585. **
  2586. ** Where <enc> is one of UTF-8, UTF-16LE or UTF16BE, and <arg> is the
  2587. ** single argument passed to the SQL function. The value returned by
  2588. ** the TCL script is used as the return value of the SQL function. It
  2589. ** is passed to SQLite using UTF-16BE for a UTF-8 test_function(), UTF-8
  2590. ** for a UTF-16LE test_function(), and UTF-16LE for an implementation that
  2591. ** prefers UTF-16BE.
  2592. */
  2593. #ifndef SQLITE_OMIT_UTF16
  2594. static void test_function_utf8(
  2595. sqlite3_context *pCtx,
  2596. int nArg,
  2597. sqlite3_value **argv
  2598. ){
  2599. Tcl_Interp *interp;
  2600. Tcl_Obj *pX;
  2601. sqlite3_value *pVal;
  2602. interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
  2603. pX = Tcl_NewStringObj("test_function", -1);
  2604. Tcl_IncrRefCount(pX);
  2605. Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-8", -1));
  2606. Tcl_ListObjAppendElement(interp, pX,
  2607. Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
  2608. Tcl_EvalObjEx(interp, pX, 0);
  2609. Tcl_DecrRefCount(pX);
  2610. sqlite3_result_text(pCtx, Tcl_GetStringResult(interp), -1, SQLITE_TRANSIENT);
  2611. pVal = sqlite3ValueNew(0);
  2612. sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
  2613. SQLITE_UTF8, SQLITE_STATIC);
  2614. sqlite3_result_text16be(pCtx, sqlite3_value_text16be(pVal),
  2615. -1, SQLITE_TRANSIENT);
  2616. sqlite3ValueFree(pVal);
  2617. }
  2618. static void test_function_utf16le(
  2619. sqlite3_context *pCtx,
  2620. int nArg,
  2621. sqlite3_value **argv
  2622. ){
  2623. Tcl_Interp *interp;
  2624. Tcl_Obj *pX;
  2625. sqlite3_value *pVal;
  2626. interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
  2627. pX = Tcl_NewStringObj("test_function", -1);
  2628. Tcl_IncrRefCount(pX);
  2629. Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16LE", -1));
  2630. Tcl_ListObjAppendElement(interp, pX,
  2631. Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
  2632. Tcl_EvalObjEx(interp, pX, 0);
  2633. Tcl_DecrRefCount(pX);
  2634. pVal = sqlite3ValueNew(0);
  2635. sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
  2636. SQLITE_UTF8, SQLITE_STATIC);
  2637. sqlite3_result_text(pCtx,(char*)sqlite3_value_text(pVal),-1,SQLITE_TRANSIENT);
  2638. sqlite3ValueFree(pVal);
  2639. }
  2640. static void test_function_utf16be(
  2641. sqlite3_context *pCtx,
  2642. int nArg,
  2643. sqlite3_value **argv
  2644. ){
  2645. Tcl_Interp *interp;
  2646. Tcl_Obj *pX;
  2647. sqlite3_value *pVal;
  2648. interp = (Tcl_Interp *)sqlite3_user_data(pCtx);
  2649. pX = Tcl_NewStringObj("test_function", -1);
  2650. Tcl_IncrRefCount(pX);
  2651. Tcl_ListObjAppendElement(interp, pX, Tcl_NewStringObj("UTF-16BE", -1));
  2652. Tcl_ListObjAppendElement(interp, pX,
  2653. Tcl_NewStringObj((char*)sqlite3_value_text(argv[0]), -1));
  2654. Tcl_EvalObjEx(interp, pX, 0);
  2655. Tcl_DecrRefCount(pX);
  2656. pVal = sqlite3ValueNew(0);
  2657. sqlite3ValueSetStr(pVal, -1, Tcl_GetStringResult(interp),
  2658. SQLITE_UTF8, SQLITE_STATIC);
  2659. sqlite3_result_text16(pCtx, sqlite3_value_text16le(pVal),
  2660. -1, SQLITE_TRANSIENT);
  2661. sqlite3_result_text16be(pCtx, sqlite3_value_text16le(pVal),
  2662. -1, SQLITE_TRANSIENT);
  2663. sqlite3_result_text16le(pCtx, sqlite3_value_text16le(pVal),
  2664. -1, SQLITE_TRANSIENT);
  2665. sqlite3ValueFree(pVal);
  2666. }
  2667. #endif /* SQLITE_OMIT_UTF16 */
  2668. static int test_function(
  2669. void * clientData,
  2670. Tcl_Interp *interp,
  2671. int objc,
  2672. Tcl_Obj *CONST objv[]
  2673. ){
  2674. #ifndef SQLITE_OMIT_UTF16
  2675. sqlite3 *db;
  2676. int val;
  2677. if( objc!=5 ) goto bad_args;
  2678. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  2679. if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
  2680. if( val ){
  2681. sqlite3_create_function(db, "test_function", 1, SQLITE_UTF8,
  2682. interp, test_function_utf8, 0, 0);
  2683. }
  2684. if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR;
  2685. if( val ){
  2686. sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16LE,
  2687. interp, test_function_utf16le, 0, 0);
  2688. }
  2689. if( TCL_OK!=Tcl_GetBooleanFromObj(interp, objv[4], &val) ) return TCL_ERROR;
  2690. if( val ){
  2691. sqlite3_create_function(db, "test_function", 1, SQLITE_UTF16BE,
  2692. interp, test_function_utf16be, 0, 0);
  2693. }
  2694. return TCL_OK;
  2695. bad_args:
  2696. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2697. Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0);
  2698. #endif /* SQLITE_OMIT_UTF16 */
  2699. return TCL_ERROR;
  2700. }
  2701. /*
  2702. ** Usage: sqlite3_test_errstr <err code>
  2703. **
  2704. ** Test that the english language string equivalents for sqlite error codes
  2705. ** are sane. The parameter is an integer representing an sqlite error code.
  2706. ** The result is a list of two elements, the string representation of the
  2707. ** error code and the english language explanation.
  2708. */
  2709. static int test_errstr(
  2710. void * clientData,
  2711. Tcl_Interp *interp,
  2712. int objc,
  2713. Tcl_Obj *CONST objv[]
  2714. ){
  2715. char *zCode;
  2716. int i;
  2717. if( objc!=1 ){
  2718. Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
  2719. }
  2720. zCode = Tcl_GetString(objv[1]);
  2721. for(i=0; i<200; i++){
  2722. if( 0==strcmp(t1ErrorName(i), zCode) ) break;
  2723. }
  2724. Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
  2725. return TCL_OK;
  2726. }
  2727. /*
  2728. ** Usage: breakpoint
  2729. **
  2730. ** This routine exists for one purpose - to provide a place to put a
  2731. ** breakpoint with GDB that can be triggered using TCL code. The use
  2732. ** for this is when a particular test fails on (say) the 1485th iteration.
  2733. ** In the TCL test script, we can add code like this:
  2734. **
  2735. ** if {$i==1485} breakpoint
  2736. **
  2737. ** Then run testfixture in the debugger and wait for the breakpoint to
  2738. ** fire. Then additional breakpoints can be set to trace down the bug.
  2739. */
  2740. static int test_breakpoint(
  2741. void *NotUsed,
  2742. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  2743. int argc, /* Number of arguments */
  2744. char **argv /* Text of each argument */
  2745. ){
  2746. return TCL_OK; /* Do nothing */
  2747. }
  2748. /*
  2749. ** Usage: sqlite3_bind_zeroblob STMT IDX N
  2750. **
  2751. ** Test the sqlite3_bind_zeroblob interface. STMT is a prepared statement.
  2752. ** IDX is the index of a wildcard in the prepared statement. This command
  2753. ** binds a N-byte zero-filled BLOB to the wildcard.
  2754. */
  2755. static int test_bind_zeroblob(
  2756. void * clientData,
  2757. Tcl_Interp *interp,
  2758. int objc,
  2759. Tcl_Obj *CONST objv[]
  2760. ){
  2761. sqlite3_stmt *pStmt;
  2762. int idx;
  2763. int n;
  2764. int rc;
  2765. if( objc!=4 ){
  2766. Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX N");
  2767. return TCL_ERROR;
  2768. }
  2769. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2770. if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
  2771. if( Tcl_GetIntFromObj(interp, objv[3], &n) ) return TCL_ERROR;
  2772. rc = sqlite3_bind_zeroblob(pStmt, idx, n);
  2773. if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  2774. if( rc!=SQLITE_OK ){
  2775. return TCL_ERROR;
  2776. }
  2777. return TCL_OK;
  2778. }
  2779. /*
  2780. ** Usage: sqlite3_bind_int STMT N VALUE
  2781. **
  2782. ** Test the sqlite3_bind_int interface. STMT is a prepared statement.
  2783. ** N is the index of a wildcard in the prepared statement. This command
  2784. ** binds a 32-bit integer VALUE to that wildcard.
  2785. */
  2786. static int test_bind_int(
  2787. void * clientData,
  2788. Tcl_Interp *interp,
  2789. int objc,
  2790. Tcl_Obj *CONST objv[]
  2791. ){
  2792. sqlite3_stmt *pStmt;
  2793. int idx;
  2794. int value;
  2795. int rc;
  2796. if( objc!=4 ){
  2797. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2798. Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
  2799. return TCL_ERROR;
  2800. }
  2801. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2802. if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
  2803. if( Tcl_GetIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
  2804. rc = sqlite3_bind_int(pStmt, idx, value);
  2805. if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  2806. if( rc!=SQLITE_OK ){
  2807. return TCL_ERROR;
  2808. }
  2809. return TCL_OK;
  2810. }
  2811. /*
  2812. ** Usage: sqlite3_bind_int64 STMT N VALUE
  2813. **
  2814. ** Test the sqlite3_bind_int64 interface. STMT is a prepared statement.
  2815. ** N is the index of a wildcard in the prepared statement. This command
  2816. ** binds a 64-bit integer VALUE to that wildcard.
  2817. */
  2818. static int test_bind_int64(
  2819. void * clientData,
  2820. Tcl_Interp *interp,
  2821. int objc,
  2822. Tcl_Obj *CONST objv[]
  2823. ){
  2824. sqlite3_stmt *pStmt;
  2825. int idx;
  2826. Tcl_WideInt value;
  2827. int rc;
  2828. if( objc!=4 ){
  2829. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2830. Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
  2831. return TCL_ERROR;
  2832. }
  2833. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2834. if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
  2835. if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR;
  2836. rc = sqlite3_bind_int64(pStmt, idx, value);
  2837. if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  2838. if( rc!=SQLITE_OK ){
  2839. return TCL_ERROR;
  2840. }
  2841. return TCL_OK;
  2842. }
  2843. /*
  2844. ** Usage: sqlite3_bind_double STMT N VALUE
  2845. **
  2846. ** Test the sqlite3_bind_double interface. STMT is a prepared statement.
  2847. ** N is the index of a wildcard in the prepared statement. This command
  2848. ** binds a 64-bit integer VALUE to that wildcard.
  2849. */
  2850. static int test_bind_double(
  2851. void * clientData,
  2852. Tcl_Interp *interp,
  2853. int objc,
  2854. Tcl_Obj *CONST objv[]
  2855. ){
  2856. sqlite3_stmt *pStmt;
  2857. int idx;
  2858. double value;
  2859. int rc;
  2860. const char *zVal;
  2861. int i;
  2862. static const struct {
  2863. const char *zName; /* Name of the special floating point value */
  2864. unsigned int iUpper; /* Upper 32 bits */
  2865. unsigned int iLower; /* Lower 32 bits */
  2866. } aSpecialFp[] = {
  2867. { "NaN", 0x7fffffff, 0xffffffff },
  2868. { "SNaN", 0x7ff7ffff, 0xffffffff },
  2869. { "-NaN", 0xffffffff, 0xffffffff },
  2870. { "-SNaN", 0xfff7ffff, 0xffffffff },
  2871. { "+Inf", 0x7ff00000, 0x00000000 },
  2872. { "-Inf", 0xfff00000, 0x00000000 },
  2873. { "Epsilon", 0x00000000, 0x00000001 },
  2874. { "-Epsilon", 0x80000000, 0x00000001 },
  2875. { "NaN0", 0x7ff80000, 0x00000000 },
  2876. { "-NaN0", 0xfff80000, 0x00000000 },
  2877. };
  2878. if( objc!=4 ){
  2879. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2880. Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0);
  2881. return TCL_ERROR;
  2882. }
  2883. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2884. if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
  2885. /* Intercept the string "NaN" and generate a NaN value for it.
  2886. ** All other strings are passed through to Tcl_GetDoubleFromObj().
  2887. ** Tcl_GetDoubleFromObj() should understand "NaN" but some versions
  2888. ** contain a bug.
  2889. */
  2890. zVal = Tcl_GetString(objv[3]);
  2891. for(i=0; i<sizeof(aSpecialFp)/sizeof(aSpecialFp[0]); i++){
  2892. if( strcmp(aSpecialFp[i].zName, zVal)==0 ){
  2893. sqlite3_uint64 x;
  2894. x = aSpecialFp[i].iUpper;
  2895. x <<= 32;
  2896. x |= aSpecialFp[i].iLower;
  2897. assert( sizeof(value)==8 );
  2898. assert( sizeof(x)==8 );
  2899. memcpy(&value, &x, 8);
  2900. break;
  2901. }
  2902. }
  2903. if( i>=sizeof(aSpecialFp)/sizeof(aSpecialFp[0]) &&
  2904. Tcl_GetDoubleFromObj(interp, objv[3], &value) ){
  2905. return TCL_ERROR;
  2906. }
  2907. rc = sqlite3_bind_double(pStmt, idx, value);
  2908. if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  2909. if( rc!=SQLITE_OK ){
  2910. return TCL_ERROR;
  2911. }
  2912. return TCL_OK;
  2913. }
  2914. /*
  2915. ** Usage: sqlite3_bind_null STMT N
  2916. **
  2917. ** Test the sqlite3_bind_null interface. STMT is a prepared statement.
  2918. ** N is the index of a wildcard in the prepared statement. This command
  2919. ** binds a NULL to the wildcard.
  2920. */
  2921. static int test_bind_null(
  2922. void * clientData,
  2923. Tcl_Interp *interp,
  2924. int objc,
  2925. Tcl_Obj *CONST objv[]
  2926. ){
  2927. sqlite3_stmt *pStmt;
  2928. int idx;
  2929. int rc;
  2930. if( objc!=3 ){
  2931. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2932. Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0);
  2933. return TCL_ERROR;
  2934. }
  2935. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2936. if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
  2937. rc = sqlite3_bind_null(pStmt, idx);
  2938. if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  2939. if( rc!=SQLITE_OK ){
  2940. return TCL_ERROR;
  2941. }
  2942. return TCL_OK;
  2943. }
  2944. /*
  2945. ** Usage: sqlite3_bind_text STMT N STRING BYTES
  2946. **
  2947. ** Test the sqlite3_bind_text interface. STMT is a prepared statement.
  2948. ** N is the index of a wildcard in the prepared statement. This command
  2949. ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes
  2950. ** long.
  2951. */
  2952. static int test_bind_text(
  2953. void * clientData,
  2954. Tcl_Interp *interp,
  2955. int objc,
  2956. Tcl_Obj *CONST objv[]
  2957. ){
  2958. sqlite3_stmt *pStmt;
  2959. int idx;
  2960. int bytes;
  2961. char *value;
  2962. int rc;
  2963. if( objc!=5 ){
  2964. Tcl_AppendResult(interp, "wrong # args: should be \"",
  2965. Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
  2966. return TCL_ERROR;
  2967. }
  2968. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2969. if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
  2970. value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes);
  2971. if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
  2972. rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT);
  2973. if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  2974. if( rc!=SQLITE_OK ){
  2975. Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
  2976. return TCL_ERROR;
  2977. }
  2978. return TCL_OK;
  2979. }
  2980. /*
  2981. ** Usage: sqlite3_bind_text16 ?-static? STMT N STRING BYTES
  2982. **
  2983. ** Test the sqlite3_bind_text16 interface. STMT is a prepared statement.
  2984. ** N is the index of a wildcard in the prepared statement. This command
  2985. ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes
  2986. ** long.
  2987. */
  2988. static int test_bind_text16(
  2989. void * clientData,
  2990. Tcl_Interp *interp,
  2991. int objc,
  2992. Tcl_Obj *CONST objv[]
  2993. ){
  2994. #ifndef SQLITE_OMIT_UTF16
  2995. sqlite3_stmt *pStmt;
  2996. int idx;
  2997. int bytes;
  2998. char *value;
  2999. int rc;
  3000. void (*xDel)(void*) = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT);
  3001. Tcl_Obj *oStmt = objv[objc-4];
  3002. Tcl_Obj *oN = objv[objc-3];
  3003. Tcl_Obj *oString = objv[objc-2];
  3004. Tcl_Obj *oBytes = objv[objc-1];
  3005. if( objc!=5 && objc!=6){
  3006. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3007. Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0);
  3008. return TCL_ERROR;
  3009. }
  3010. if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR;
  3011. if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR;
  3012. value = (char*)Tcl_GetByteArrayFromObj(oString, 0);
  3013. if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR;
  3014. rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel);
  3015. if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  3016. if( rc!=SQLITE_OK ){
  3017. Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
  3018. return TCL_ERROR;
  3019. }
  3020. #endif /* SQLITE_OMIT_UTF16 */
  3021. return TCL_OK;
  3022. }
  3023. /*
  3024. ** Usage: sqlite3_bind_blob ?-static? STMT N DATA BYTES
  3025. **
  3026. ** Test the sqlite3_bind_blob interface. STMT is a prepared statement.
  3027. ** N is the index of a wildcard in the prepared statement. This command
  3028. ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size.
  3029. */
  3030. static int test_bind_blob(
  3031. void * clientData,
  3032. Tcl_Interp *interp,
  3033. int objc,
  3034. Tcl_Obj *CONST objv[]
  3035. ){
  3036. sqlite3_stmt *pStmt;
  3037. int idx;
  3038. int bytes;
  3039. char *value;
  3040. int rc;
  3041. sqlite3_destructor_type xDestructor = SQLITE_TRANSIENT;
  3042. if( objc!=5 && objc!=6 ){
  3043. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3044. Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0);
  3045. return TCL_ERROR;
  3046. }
  3047. if( objc==6 ){
  3048. xDestructor = SQLITE_STATIC;
  3049. objv++;
  3050. }
  3051. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3052. if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR;
  3053. value = Tcl_GetString(objv[3]);
  3054. if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR;
  3055. rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor);
  3056. if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR;
  3057. if( rc!=SQLITE_OK ){
  3058. return TCL_ERROR;
  3059. }
  3060. return TCL_OK;
  3061. }
  3062. /*
  3063. ** Usage: sqlite3_bind_parameter_count STMT
  3064. **
  3065. ** Return the number of wildcards in the given statement.
  3066. */
  3067. static int test_bind_parameter_count(
  3068. void * clientData,
  3069. Tcl_Interp *interp,
  3070. int objc,
  3071. Tcl_Obj *CONST objv[]
  3072. ){
  3073. sqlite3_stmt *pStmt;
  3074. if( objc!=2 ){
  3075. Tcl_WrongNumArgs(interp, 1, objv, "STMT");
  3076. return TCL_ERROR;
  3077. }
  3078. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3079. Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_bind_parameter_count(pStmt)));
  3080. return TCL_OK;
  3081. }
  3082. /*
  3083. ** Usage: sqlite3_bind_parameter_name STMT N
  3084. **
  3085. ** Return the name of the Nth wildcard. The first wildcard is 1.
  3086. ** An empty string is returned if N is out of range or if the wildcard
  3087. ** is nameless.
  3088. */
  3089. static int test_bind_parameter_name(
  3090. void * clientData,
  3091. Tcl_Interp *interp,
  3092. int objc,
  3093. Tcl_Obj *CONST objv[]
  3094. ){
  3095. sqlite3_stmt *pStmt;
  3096. int i;
  3097. if( objc!=3 ){
  3098. Tcl_WrongNumArgs(interp, 1, objv, "STMT N");
  3099. return TCL_ERROR;
  3100. }
  3101. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3102. if( Tcl_GetIntFromObj(interp, objv[2], &i) ) return TCL_ERROR;
  3103. Tcl_SetObjResult(interp,
  3104. Tcl_NewStringObj(sqlite3_bind_parameter_name(pStmt,i),-1)
  3105. );
  3106. return TCL_OK;
  3107. }
  3108. /*
  3109. ** Usage: sqlite3_bind_parameter_index STMT NAME
  3110. **
  3111. ** Return the index of the wildcard called NAME. Return 0 if there is
  3112. ** no such wildcard.
  3113. */
  3114. static int test_bind_parameter_index(
  3115. void * clientData,
  3116. Tcl_Interp *interp,
  3117. int objc,
  3118. Tcl_Obj *CONST objv[]
  3119. ){
  3120. sqlite3_stmt *pStmt;
  3121. if( objc!=3 ){
  3122. Tcl_WrongNumArgs(interp, 1, objv, "STMT NAME");
  3123. return TCL_ERROR;
  3124. }
  3125. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3126. Tcl_SetObjResult(interp,
  3127. Tcl_NewIntObj(
  3128. sqlite3_bind_parameter_index(pStmt,Tcl_GetString(objv[2]))
  3129. )
  3130. );
  3131. return TCL_OK;
  3132. }
  3133. /*
  3134. ** Usage: sqlite3_clear_bindings STMT
  3135. **
  3136. */
  3137. static int test_clear_bindings(
  3138. void * clientData,
  3139. Tcl_Interp *interp,
  3140. int objc,
  3141. Tcl_Obj *CONST objv[]
  3142. ){
  3143. sqlite3_stmt *pStmt;
  3144. if( objc!=2 ){
  3145. Tcl_WrongNumArgs(interp, 1, objv, "STMT");
  3146. return TCL_ERROR;
  3147. }
  3148. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3149. Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_clear_bindings(pStmt)));
  3150. return TCL_OK;
  3151. }
  3152. /*
  3153. ** Usage: sqlite3_sleep MILLISECONDS
  3154. */
  3155. static int test_sleep(
  3156. void * clientData,
  3157. Tcl_Interp *interp,
  3158. int objc,
  3159. Tcl_Obj *CONST objv[]
  3160. ){
  3161. int ms;
  3162. if( objc!=2 ){
  3163. Tcl_WrongNumArgs(interp, 1, objv, "MILLISECONDS");
  3164. return TCL_ERROR;
  3165. }
  3166. if( Tcl_GetIntFromObj(interp, objv[1], &ms) ){
  3167. return TCL_ERROR;
  3168. }
  3169. Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_sleep(ms)));
  3170. return TCL_OK;
  3171. }
  3172. /*
  3173. ** Usage: sqlite3_extended_errcode DB
  3174. **
  3175. ** Return the string representation of the most recent sqlite3_* API
  3176. ** error code. e.g. "SQLITE_ERROR".
  3177. */
  3178. static int test_ex_errcode(
  3179. void * clientData,
  3180. Tcl_Interp *interp,
  3181. int objc,
  3182. Tcl_Obj *CONST objv[]
  3183. ){
  3184. sqlite3 *db;
  3185. int rc;
  3186. if( objc!=2 ){
  3187. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3188. Tcl_GetString(objv[0]), " DB", 0);
  3189. return TCL_ERROR;
  3190. }
  3191. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  3192. rc = sqlite3_extended_errcode(db);
  3193. Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
  3194. return TCL_OK;
  3195. }
  3196. /*
  3197. ** Usage: sqlite3_errcode DB
  3198. **
  3199. ** Return the string representation of the most recent sqlite3_* API
  3200. ** error code. e.g. "SQLITE_ERROR".
  3201. */
  3202. static int test_errcode(
  3203. void * clientData,
  3204. Tcl_Interp *interp,
  3205. int objc,
  3206. Tcl_Obj *CONST objv[]
  3207. ){
  3208. sqlite3 *db;
  3209. int rc;
  3210. if( objc!=2 ){
  3211. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3212. Tcl_GetString(objv[0]), " DB", 0);
  3213. return TCL_ERROR;
  3214. }
  3215. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  3216. rc = sqlite3_errcode(db);
  3217. Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0);
  3218. return TCL_OK;
  3219. }
  3220. /*
  3221. ** Usage: sqlite3_errmsg DB
  3222. **
  3223. ** Returns the UTF-8 representation of the error message string for the
  3224. ** most recent sqlite3_* API call.
  3225. */
  3226. static int test_errmsg(
  3227. void * clientData,
  3228. Tcl_Interp *interp,
  3229. int objc,
  3230. Tcl_Obj *CONST objv[]
  3231. ){
  3232. sqlite3 *db;
  3233. const char *zErr;
  3234. if( objc!=2 ){
  3235. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3236. Tcl_GetString(objv[0]), " DB", 0);
  3237. return TCL_ERROR;
  3238. }
  3239. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  3240. zErr = sqlite3_errmsg(db);
  3241. Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
  3242. return TCL_OK;
  3243. }
  3244. /*
  3245. ** Usage: test_errmsg16 DB
  3246. **
  3247. ** Returns the UTF-16 representation of the error message string for the
  3248. ** most recent sqlite3_* API call. This is a byte array object at the TCL
  3249. ** level, and it includes the 0x00 0x00 terminator bytes at the end of the
  3250. ** UTF-16 string.
  3251. */
  3252. static int test_errmsg16(
  3253. void * clientData,
  3254. Tcl_Interp *interp,
  3255. int objc,
  3256. Tcl_Obj *CONST objv[]
  3257. ){
  3258. #ifndef SQLITE_OMIT_UTF16
  3259. sqlite3 *db;
  3260. const void *zErr;
  3261. const char *z;
  3262. int bytes = 0;
  3263. if( objc!=2 ){
  3264. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3265. Tcl_GetString(objv[0]), " DB", 0);
  3266. return TCL_ERROR;
  3267. }
  3268. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  3269. zErr = sqlite3_errmsg16(db);
  3270. if( zErr ){
  3271. z = zErr;
  3272. for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){}
  3273. }
  3274. Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes));
  3275. #endif /* SQLITE_OMIT_UTF16 */
  3276. return TCL_OK;
  3277. }
  3278. /*
  3279. ** Usage: sqlite3_prepare DB sql bytes ?tailvar?
  3280. **
  3281. ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
  3282. ** database handle <DB>. The parameter <tailval> is the name of a global
  3283. ** variable that is set to the unused portion of <sql> (if any). A
  3284. ** STMT handle is returned.
  3285. */
  3286. static int test_prepare(
  3287. void * clientData,
  3288. Tcl_Interp *interp,
  3289. int objc,
  3290. Tcl_Obj *CONST objv[]
  3291. ){
  3292. sqlite3 *db;
  3293. const char *zSql;
  3294. int bytes;
  3295. const char *zTail = 0;
  3296. sqlite3_stmt *pStmt = 0;
  3297. char zBuf[50];
  3298. int rc;
  3299. if( objc!=5 && objc!=4 ){
  3300. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3301. Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
  3302. return TCL_ERROR;
  3303. }
  3304. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  3305. zSql = Tcl_GetString(objv[2]);
  3306. if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
  3307. rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
  3308. Tcl_ResetResult(interp);
  3309. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  3310. if( zTail && objc>=5 ){
  3311. if( bytes>=0 ){
  3312. bytes = bytes - (int)(zTail-zSql);
  3313. }
  3314. if( (int)strlen(zTail)<bytes ){
  3315. bytes = (int)strlen(zTail);
  3316. }
  3317. Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
  3318. }
  3319. if( rc!=SQLITE_OK ){
  3320. assert( pStmt==0 );
  3321. sprintf(zBuf, "(%d) ", rc);
  3322. Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
  3323. return TCL_ERROR;
  3324. }
  3325. if( pStmt ){
  3326. if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
  3327. Tcl_AppendResult(interp, zBuf, 0);
  3328. }
  3329. return TCL_OK;
  3330. }
  3331. /*
  3332. ** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar?
  3333. **
  3334. ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
  3335. ** database handle <DB>. The parameter <tailval> is the name of a global
  3336. ** variable that is set to the unused portion of <sql> (if any). A
  3337. ** STMT handle is returned.
  3338. */
  3339. static int test_prepare_v2(
  3340. void * clientData,
  3341. Tcl_Interp *interp,
  3342. int objc,
  3343. Tcl_Obj *CONST objv[]
  3344. ){
  3345. sqlite3 *db;
  3346. const char *zSql;
  3347. int bytes;
  3348. const char *zTail = 0;
  3349. sqlite3_stmt *pStmt = 0;
  3350. char zBuf[50];
  3351. int rc;
  3352. if( objc!=5 && objc!=4 ){
  3353. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3354. Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
  3355. return TCL_ERROR;
  3356. }
  3357. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  3358. zSql = Tcl_GetString(objv[2]);
  3359. if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
  3360. rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
  3361. assert(rc==SQLITE_OK || pStmt==0);
  3362. Tcl_ResetResult(interp);
  3363. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  3364. if( zTail && objc>=5 ){
  3365. if( bytes>=0 ){
  3366. bytes = bytes - (int)(zTail-zSql);
  3367. }
  3368. Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
  3369. }
  3370. if( rc!=SQLITE_OK ){
  3371. assert( pStmt==0 );
  3372. sprintf(zBuf, "(%d) ", rc);
  3373. Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
  3374. return TCL_ERROR;
  3375. }
  3376. if( pStmt ){
  3377. if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
  3378. Tcl_AppendResult(interp, zBuf, 0);
  3379. }
  3380. return TCL_OK;
  3381. }
  3382. /*
  3383. ** Usage: sqlite3_prepare_tkt3134 DB
  3384. **
  3385. ** Generate a prepared statement for a zero-byte string as a test
  3386. ** for ticket #3134. The string should be preceeded by a zero byte.
  3387. */
  3388. static int test_prepare_tkt3134(
  3389. void * clientData,
  3390. Tcl_Interp *interp,
  3391. int objc,
  3392. Tcl_Obj *CONST objv[]
  3393. ){
  3394. sqlite3 *db;
  3395. static const char zSql[] = "\000SELECT 1";
  3396. sqlite3_stmt *pStmt = 0;
  3397. char zBuf[50];
  3398. int rc;
  3399. if( objc!=2 ){
  3400. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3401. Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
  3402. return TCL_ERROR;
  3403. }
  3404. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  3405. rc = sqlite3_prepare_v2(db, &zSql[1], 0, &pStmt, 0);
  3406. assert(rc==SQLITE_OK || pStmt==0);
  3407. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  3408. if( rc!=SQLITE_OK ){
  3409. assert( pStmt==0 );
  3410. sprintf(zBuf, "(%d) ", rc);
  3411. Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
  3412. return TCL_ERROR;
  3413. }
  3414. if( pStmt ){
  3415. if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
  3416. Tcl_AppendResult(interp, zBuf, 0);
  3417. }
  3418. return TCL_OK;
  3419. }
  3420. /*
  3421. ** Usage: sqlite3_prepare16 DB sql bytes tailvar
  3422. **
  3423. ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
  3424. ** database handle <DB>. The parameter <tailval> is the name of a global
  3425. ** variable that is set to the unused portion of <sql> (if any). A
  3426. ** STMT handle is returned.
  3427. */
  3428. static int test_prepare16(
  3429. void * clientData,
  3430. Tcl_Interp *interp,
  3431. int objc,
  3432. Tcl_Obj *CONST objv[]
  3433. ){
  3434. #ifndef SQLITE_OMIT_UTF16
  3435. sqlite3 *db;
  3436. const void *zSql;
  3437. const void *zTail = 0;
  3438. Tcl_Obj *pTail = 0;
  3439. sqlite3_stmt *pStmt = 0;
  3440. char zBuf[50];
  3441. int rc;
  3442. int bytes; /* The integer specified as arg 3 */
  3443. int objlen; /* The byte-array length of arg 2 */
  3444. if( objc!=5 && objc!=4 ){
  3445. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3446. Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
  3447. return TCL_ERROR;
  3448. }
  3449. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  3450. zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
  3451. if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
  3452. rc = sqlite3_prepare16(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
  3453. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  3454. if( rc ){
  3455. return TCL_ERROR;
  3456. }
  3457. if( objc>=5 ){
  3458. if( zTail ){
  3459. objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql);
  3460. }else{
  3461. objlen = 0;
  3462. }
  3463. pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
  3464. Tcl_IncrRefCount(pTail);
  3465. Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
  3466. Tcl_DecrRefCount(pTail);
  3467. }
  3468. if( pStmt ){
  3469. if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
  3470. }
  3471. Tcl_AppendResult(interp, zBuf, 0);
  3472. #endif /* SQLITE_OMIT_UTF16 */
  3473. return TCL_OK;
  3474. }
  3475. /*
  3476. ** Usage: sqlite3_prepare16_v2 DB sql bytes ?tailvar?
  3477. **
  3478. ** Compile up to <bytes> bytes of the supplied SQL string <sql> using
  3479. ** database handle <DB>. The parameter <tailval> is the name of a global
  3480. ** variable that is set to the unused portion of <sql> (if any). A
  3481. ** STMT handle is returned.
  3482. */
  3483. static int test_prepare16_v2(
  3484. void * clientData,
  3485. Tcl_Interp *interp,
  3486. int objc,
  3487. Tcl_Obj *CONST objv[]
  3488. ){
  3489. #ifndef SQLITE_OMIT_UTF16
  3490. sqlite3 *db;
  3491. const void *zSql;
  3492. const void *zTail = 0;
  3493. Tcl_Obj *pTail = 0;
  3494. sqlite3_stmt *pStmt = 0;
  3495. char zBuf[50];
  3496. int rc;
  3497. int bytes; /* The integer specified as arg 3 */
  3498. int objlen; /* The byte-array length of arg 2 */
  3499. if( objc!=5 && objc!=4 ){
  3500. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3501. Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0);
  3502. return TCL_ERROR;
  3503. }
  3504. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  3505. zSql = Tcl_GetByteArrayFromObj(objv[2], &objlen);
  3506. if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
  3507. rc = sqlite3_prepare16_v2(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0);
  3508. if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  3509. if( rc ){
  3510. return TCL_ERROR;
  3511. }
  3512. if( objc>=5 ){
  3513. if( zTail ){
  3514. objlen = objlen - (int)((u8 *)zTail-(u8 *)zSql);
  3515. }else{
  3516. objlen = 0;
  3517. }
  3518. pTail = Tcl_NewByteArrayObj((u8 *)zTail, objlen);
  3519. Tcl_IncrRefCount(pTail);
  3520. Tcl_ObjSetVar2(interp, objv[4], 0, pTail, 0);
  3521. Tcl_DecrRefCount(pTail);
  3522. }
  3523. if( pStmt ){
  3524. if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
  3525. }
  3526. Tcl_AppendResult(interp, zBuf, 0);
  3527. #endif /* SQLITE_OMIT_UTF16 */
  3528. return TCL_OK;
  3529. }
  3530. /*
  3531. ** Usage: sqlite3_open filename ?options-list?
  3532. */
  3533. static int test_open(
  3534. void * clientData,
  3535. Tcl_Interp *interp,
  3536. int objc,
  3537. Tcl_Obj *CONST objv[]
  3538. ){
  3539. const char *zFilename;
  3540. sqlite3 *db;
  3541. char zBuf[100];
  3542. if( objc!=3 && objc!=2 && objc!=1 ){
  3543. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3544. Tcl_GetString(objv[0]), " filename options-list", 0);
  3545. return TCL_ERROR;
  3546. }
  3547. zFilename = objc>1 ? Tcl_GetString(objv[1]) : 0;
  3548. sqlite3_open(zFilename, &db);
  3549. if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
  3550. Tcl_AppendResult(interp, zBuf, 0);
  3551. return TCL_OK;
  3552. }
  3553. /*
  3554. ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS
  3555. */
  3556. static int test_open_v2(
  3557. void * clientData,
  3558. Tcl_Interp *interp,
  3559. int objc,
  3560. Tcl_Obj *CONST objv[]
  3561. ){
  3562. const char *zFilename;
  3563. const char *zVfs;
  3564. int flags = 0;
  3565. sqlite3 *db;
  3566. int rc;
  3567. char zBuf[100];
  3568. int nFlag;
  3569. Tcl_Obj **apFlag;
  3570. int i;
  3571. if( objc!=4 ){
  3572. Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS");
  3573. return TCL_ERROR;
  3574. }
  3575. zFilename = Tcl_GetString(objv[1]);
  3576. zVfs = Tcl_GetString(objv[3]);
  3577. if( zVfs[0]==0x00 ) zVfs = 0;
  3578. rc = Tcl_ListObjGetElements(interp, objv[2], &nFlag, &apFlag);
  3579. if( rc!=TCL_OK ) return rc;
  3580. for(i=0; i<nFlag; i++){
  3581. int iFlag;
  3582. struct OpenFlag {
  3583. const char *zFlag;
  3584. int flag;
  3585. } aFlag[] = {
  3586. { "SQLITE_OPEN_READONLY", SQLITE_OPEN_READONLY },
  3587. { "SQLITE_OPEN_READWRITE", SQLITE_OPEN_READWRITE },
  3588. { "SQLITE_OPEN_CREATE", SQLITE_OPEN_CREATE },
  3589. { "SQLITE_OPEN_DELETEONCLOSE", SQLITE_OPEN_DELETEONCLOSE },
  3590. { "SQLITE_OPEN_EXCLUSIVE", SQLITE_OPEN_EXCLUSIVE },
  3591. { "SQLITE_OPEN_AUTOPROXY", SQLITE_OPEN_AUTOPROXY },
  3592. { "SQLITE_OPEN_MAIN_DB", SQLITE_OPEN_MAIN_DB },
  3593. { "SQLITE_OPEN_TEMP_DB", SQLITE_OPEN_TEMP_DB },
  3594. { "SQLITE_OPEN_TRANSIENT_DB", SQLITE_OPEN_TRANSIENT_DB },
  3595. { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL },
  3596. { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL },
  3597. { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL },
  3598. { "SQLITE_OPEN_MASTER_JOURNAL", SQLITE_OPEN_MASTER_JOURNAL },
  3599. { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX },
  3600. { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX },
  3601. { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE },
  3602. { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE },
  3603. { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL },
  3604. { "SQLITE_OPEN_URI", SQLITE_OPEN_URI },
  3605. { 0, 0 }
  3606. };
  3607. rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]),
  3608. "flag", 0, &iFlag
  3609. );
  3610. if( rc!=TCL_OK ) return rc;
  3611. flags |= aFlag[iFlag].flag;
  3612. }
  3613. rc = sqlite3_open_v2(zFilename, &db, flags, zVfs);
  3614. if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
  3615. Tcl_AppendResult(interp, zBuf, 0);
  3616. return TCL_OK;
  3617. }
  3618. /*
  3619. ** Usage: sqlite3_open16 filename options
  3620. */
  3621. static int test_open16(
  3622. void * clientData,
  3623. Tcl_Interp *interp,
  3624. int objc,
  3625. Tcl_Obj *CONST objv[]
  3626. ){
  3627. #ifndef SQLITE_OMIT_UTF16
  3628. const void *zFilename;
  3629. sqlite3 *db;
  3630. char zBuf[100];
  3631. if( objc!=3 ){
  3632. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3633. Tcl_GetString(objv[0]), " filename options-list", 0);
  3634. return TCL_ERROR;
  3635. }
  3636. zFilename = Tcl_GetByteArrayFromObj(objv[1], 0);
  3637. sqlite3_open16(zFilename, &db);
  3638. if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
  3639. Tcl_AppendResult(interp, zBuf, 0);
  3640. #endif /* SQLITE_OMIT_UTF16 */
  3641. return TCL_OK;
  3642. }
  3643. /*
  3644. ** Usage: sqlite3_complete16 <UTF-16 string>
  3645. **
  3646. ** Return 1 if the supplied argument is a complete SQL statement, or zero
  3647. ** otherwise.
  3648. */
  3649. static int test_complete16(
  3650. void * clientData,
  3651. Tcl_Interp *interp,
  3652. int objc,
  3653. Tcl_Obj *CONST objv[]
  3654. ){
  3655. #if !defined(SQLITE_OMIT_COMPLETE) && !defined(SQLITE_OMIT_UTF16)
  3656. char *zBuf;
  3657. if( objc!=2 ){
  3658. Tcl_WrongNumArgs(interp, 1, objv, "<utf-16 sql>");
  3659. return TCL_ERROR;
  3660. }
  3661. zBuf = (char*)Tcl_GetByteArrayFromObj(objv[1], 0);
  3662. Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_complete16(zBuf)));
  3663. #endif /* SQLITE_OMIT_COMPLETE && SQLITE_OMIT_UTF16 */
  3664. return TCL_OK;
  3665. }
  3666. /*
  3667. ** Usage: sqlite3_step STMT
  3668. **
  3669. ** Advance the statement to the next row.
  3670. */
  3671. static int test_step(
  3672. void * clientData,
  3673. Tcl_Interp *interp,
  3674. int objc,
  3675. Tcl_Obj *CONST objv[]
  3676. ){
  3677. sqlite3_stmt *pStmt;
  3678. int rc;
  3679. if( objc!=2 ){
  3680. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3681. Tcl_GetString(objv[0]), " STMT", 0);
  3682. return TCL_ERROR;
  3683. }
  3684. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3685. rc = sqlite3_step(pStmt);
  3686. /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
  3687. Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
  3688. return TCL_OK;
  3689. }
  3690. static int test_sql(
  3691. void * clientData,
  3692. Tcl_Interp *interp,
  3693. int objc,
  3694. Tcl_Obj *CONST objv[]
  3695. ){
  3696. sqlite3_stmt *pStmt;
  3697. if( objc!=2 ){
  3698. Tcl_WrongNumArgs(interp, 1, objv, "STMT");
  3699. return TCL_ERROR;
  3700. }
  3701. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3702. Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE);
  3703. return TCL_OK;
  3704. }
  3705. /*
  3706. ** Usage: sqlite3_column_count STMT
  3707. **
  3708. ** Return the number of columns returned by the sql statement STMT.
  3709. */
  3710. static int test_column_count(
  3711. void * clientData,
  3712. Tcl_Interp *interp,
  3713. int objc,
  3714. Tcl_Obj *CONST objv[]
  3715. ){
  3716. sqlite3_stmt *pStmt;
  3717. if( objc!=2 ){
  3718. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3719. Tcl_GetString(objv[0]), " STMT column", 0);
  3720. return TCL_ERROR;
  3721. }
  3722. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3723. Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_column_count(pStmt)));
  3724. return TCL_OK;
  3725. }
  3726. /*
  3727. ** Usage: sqlite3_column_type STMT column
  3728. **
  3729. ** Return the type of the data in column 'column' of the current row.
  3730. */
  3731. static int test_column_type(
  3732. void * clientData,
  3733. Tcl_Interp *interp,
  3734. int objc,
  3735. Tcl_Obj *CONST objv[]
  3736. ){
  3737. sqlite3_stmt *pStmt;
  3738. int col;
  3739. int tp;
  3740. if( objc!=3 ){
  3741. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3742. Tcl_GetString(objv[0]), " STMT column", 0);
  3743. return TCL_ERROR;
  3744. }
  3745. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3746. if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  3747. tp = sqlite3_column_type(pStmt, col);
  3748. switch( tp ){
  3749. case SQLITE_INTEGER:
  3750. Tcl_SetResult(interp, "INTEGER", TCL_STATIC);
  3751. break;
  3752. case SQLITE_NULL:
  3753. Tcl_SetResult(interp, "NULL", TCL_STATIC);
  3754. break;
  3755. case SQLITE_FLOAT:
  3756. Tcl_SetResult(interp, "FLOAT", TCL_STATIC);
  3757. break;
  3758. case SQLITE_TEXT:
  3759. Tcl_SetResult(interp, "TEXT", TCL_STATIC);
  3760. break;
  3761. case SQLITE_BLOB:
  3762. Tcl_SetResult(interp, "BLOB", TCL_STATIC);
  3763. break;
  3764. default:
  3765. assert(0);
  3766. }
  3767. return TCL_OK;
  3768. }
  3769. /*
  3770. ** Usage: sqlite3_column_int64 STMT column
  3771. **
  3772. ** Return the data in column 'column' of the current row cast as an
  3773. ** wide (64-bit) integer.
  3774. */
  3775. static int test_column_int64(
  3776. void * clientData,
  3777. Tcl_Interp *interp,
  3778. int objc,
  3779. Tcl_Obj *CONST objv[]
  3780. ){
  3781. sqlite3_stmt *pStmt;
  3782. int col;
  3783. i64 iVal;
  3784. if( objc!=3 ){
  3785. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3786. Tcl_GetString(objv[0]), " STMT column", 0);
  3787. return TCL_ERROR;
  3788. }
  3789. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3790. if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  3791. iVal = sqlite3_column_int64(pStmt, col);
  3792. Tcl_SetObjResult(interp, Tcl_NewWideIntObj(iVal));
  3793. return TCL_OK;
  3794. }
  3795. /*
  3796. ** Usage: sqlite3_column_blob STMT column
  3797. */
  3798. static int test_column_blob(
  3799. void * clientData,
  3800. Tcl_Interp *interp,
  3801. int objc,
  3802. Tcl_Obj *CONST objv[]
  3803. ){
  3804. sqlite3_stmt *pStmt;
  3805. int col;
  3806. int len;
  3807. const void *pBlob;
  3808. if( objc!=3 ){
  3809. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3810. Tcl_GetString(objv[0]), " STMT column", 0);
  3811. return TCL_ERROR;
  3812. }
  3813. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3814. if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  3815. len = sqlite3_column_bytes(pStmt, col);
  3816. pBlob = sqlite3_column_blob(pStmt, col);
  3817. Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pBlob, len));
  3818. return TCL_OK;
  3819. }
  3820. /*
  3821. ** Usage: sqlite3_column_double STMT column
  3822. **
  3823. ** Return the data in column 'column' of the current row cast as a double.
  3824. */
  3825. static int test_column_double(
  3826. void * clientData,
  3827. Tcl_Interp *interp,
  3828. int objc,
  3829. Tcl_Obj *CONST objv[]
  3830. ){
  3831. sqlite3_stmt *pStmt;
  3832. int col;
  3833. double rVal;
  3834. if( objc!=3 ){
  3835. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3836. Tcl_GetString(objv[0]), " STMT column", 0);
  3837. return TCL_ERROR;
  3838. }
  3839. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3840. if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  3841. rVal = sqlite3_column_double(pStmt, col);
  3842. Tcl_SetObjResult(interp, Tcl_NewDoubleObj(rVal));
  3843. return TCL_OK;
  3844. }
  3845. /*
  3846. ** Usage: sqlite3_data_count STMT
  3847. **
  3848. ** Return the number of columns returned by the sql statement STMT.
  3849. */
  3850. static int test_data_count(
  3851. void * clientData,
  3852. Tcl_Interp *interp,
  3853. int objc,
  3854. Tcl_Obj *CONST objv[]
  3855. ){
  3856. sqlite3_stmt *pStmt;
  3857. if( objc!=2 ){
  3858. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3859. Tcl_GetString(objv[0]), " STMT column", 0);
  3860. return TCL_ERROR;
  3861. }
  3862. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3863. Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_data_count(pStmt)));
  3864. return TCL_OK;
  3865. }
  3866. /*
  3867. ** Usage: sqlite3_column_text STMT column
  3868. **
  3869. ** Usage: sqlite3_column_decltype STMT column
  3870. **
  3871. ** Usage: sqlite3_column_name STMT column
  3872. */
  3873. static int test_stmt_utf8(
  3874. void * clientData, /* Pointer to SQLite API function to be invoke */
  3875. Tcl_Interp *interp,
  3876. int objc,
  3877. Tcl_Obj *CONST objv[]
  3878. ){
  3879. sqlite3_stmt *pStmt;
  3880. int col;
  3881. const char *(*xFunc)(sqlite3_stmt*, int);
  3882. const char *zRet;
  3883. xFunc = (const char *(*)(sqlite3_stmt*, int))clientData;
  3884. if( objc!=3 ){
  3885. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3886. Tcl_GetString(objv[0]), " STMT column", 0);
  3887. return TCL_ERROR;
  3888. }
  3889. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3890. if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  3891. zRet = xFunc(pStmt, col);
  3892. if( zRet ){
  3893. Tcl_SetResult(interp, (char *)zRet, 0);
  3894. }
  3895. return TCL_OK;
  3896. }
  3897. static int test_global_recover(
  3898. void * clientData,
  3899. Tcl_Interp *interp,
  3900. int objc,
  3901. Tcl_Obj *CONST objv[]
  3902. ){
  3903. #ifndef SQLITE_OMIT_DEPRECATED
  3904. int rc;
  3905. if( objc!=1 ){
  3906. Tcl_WrongNumArgs(interp, 1, objv, "");
  3907. return TCL_ERROR;
  3908. }
  3909. rc = sqlite3_global_recover();
  3910. Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  3911. #endif
  3912. return TCL_OK;
  3913. }
  3914. /*
  3915. ** Usage: sqlite3_column_text STMT column
  3916. **
  3917. ** Usage: sqlite3_column_decltype STMT column
  3918. **
  3919. ** Usage: sqlite3_column_name STMT column
  3920. */
  3921. static int test_stmt_utf16(
  3922. void * clientData, /* Pointer to SQLite API function to be invoked */
  3923. Tcl_Interp *interp,
  3924. int objc,
  3925. Tcl_Obj *CONST objv[]
  3926. ){
  3927. #ifndef SQLITE_OMIT_UTF16
  3928. sqlite3_stmt *pStmt;
  3929. int col;
  3930. Tcl_Obj *pRet;
  3931. const void *zName16;
  3932. const void *(*xFunc)(sqlite3_stmt*, int);
  3933. xFunc = (const void *(*)(sqlite3_stmt*, int))clientData;
  3934. if( objc!=3 ){
  3935. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3936. Tcl_GetString(objv[0]), " STMT column", 0);
  3937. return TCL_ERROR;
  3938. }
  3939. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3940. if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  3941. zName16 = xFunc(pStmt, col);
  3942. if( zName16 ){
  3943. int n;
  3944. const char *z = zName16;
  3945. for(n=0; z[n] || z[n+1]; n+=2){}
  3946. pRet = Tcl_NewByteArrayObj(zName16, n+2);
  3947. Tcl_SetObjResult(interp, pRet);
  3948. }
  3949. #endif /* SQLITE_OMIT_UTF16 */
  3950. return TCL_OK;
  3951. }
  3952. /*
  3953. ** Usage: sqlite3_column_int STMT column
  3954. **
  3955. ** Usage: sqlite3_column_bytes STMT column
  3956. **
  3957. ** Usage: sqlite3_column_bytes16 STMT column
  3958. **
  3959. */
  3960. static int test_stmt_int(
  3961. void * clientData, /* Pointer to SQLite API function to be invoked */
  3962. Tcl_Interp *interp,
  3963. int objc,
  3964. Tcl_Obj *CONST objv[]
  3965. ){
  3966. sqlite3_stmt *pStmt;
  3967. int col;
  3968. int (*xFunc)(sqlite3_stmt*, int);
  3969. xFunc = (int (*)(sqlite3_stmt*, int))clientData;
  3970. if( objc!=3 ){
  3971. Tcl_AppendResult(interp, "wrong # args: should be \"",
  3972. Tcl_GetString(objv[0]), " STMT column", 0);
  3973. return TCL_ERROR;
  3974. }
  3975. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  3976. if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR;
  3977. Tcl_SetObjResult(interp, Tcl_NewIntObj(xFunc(pStmt, col)));
  3978. return TCL_OK;
  3979. }
  3980. /*
  3981. ** Usage: sqlite_set_magic DB MAGIC-NUMBER
  3982. **
  3983. ** Set the db->magic value. This is used to test error recovery logic.
  3984. */
  3985. static int sqlite_set_magic(
  3986. void * clientData,
  3987. Tcl_Interp *interp,
  3988. int argc,
  3989. char **argv
  3990. ){
  3991. sqlite3 *db;
  3992. if( argc!=3 ){
  3993. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  3994. " DB MAGIC", 0);
  3995. return TCL_ERROR;
  3996. }
  3997. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  3998. if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){
  3999. db->magic = SQLITE_MAGIC_OPEN;
  4000. }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){
  4001. db->magic = SQLITE_MAGIC_CLOSED;
  4002. }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){
  4003. db->magic = SQLITE_MAGIC_BUSY;
  4004. }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){
  4005. db->magic = SQLITE_MAGIC_ERROR;
  4006. }else if( Tcl_GetInt(interp, argv[2], (int*)&db->magic) ){
  4007. return TCL_ERROR;
  4008. }
  4009. return TCL_OK;
  4010. }
  4011. /*
  4012. ** Usage: sqlite3_interrupt DB
  4013. **
  4014. ** Trigger an interrupt on DB
  4015. */
  4016. static int test_interrupt(
  4017. void * clientData,
  4018. Tcl_Interp *interp,
  4019. int argc,
  4020. char **argv
  4021. ){
  4022. sqlite3 *db;
  4023. if( argc!=2 ){
  4024. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0);
  4025. return TCL_ERROR;
  4026. }
  4027. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  4028. sqlite3_interrupt(db);
  4029. return TCL_OK;
  4030. }
  4031. static u8 *sqlite3_stack_baseline = 0;
  4032. /*
  4033. ** Fill the stack with a known bitpattern.
  4034. */
  4035. static void prepStack(void){
  4036. int i;
  4037. u32 bigBuf[65536];
  4038. for(i=0; i<sizeof(bigBuf)/sizeof(bigBuf[0]); i++) bigBuf[i] = 0xdeadbeef;
  4039. sqlite3_stack_baseline = (u8*)&bigBuf[65536];
  4040. }
  4041. /*
  4042. ** Get the current stack depth. Used for debugging only.
  4043. */
  4044. u64 sqlite3StackDepth(void){
  4045. u8 x;
  4046. return (u64)(sqlite3_stack_baseline - &x);
  4047. }
  4048. /*
  4049. ** Usage: sqlite3_stack_used DB SQL
  4050. **
  4051. ** Try to measure the amount of stack space used by a call to sqlite3_exec
  4052. */
  4053. static int test_stack_used(
  4054. void * clientData,
  4055. Tcl_Interp *interp,
  4056. int argc,
  4057. char **argv
  4058. ){
  4059. sqlite3 *db;
  4060. int i;
  4061. if( argc!=3 ){
  4062. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  4063. " DB SQL", 0);
  4064. return TCL_ERROR;
  4065. }
  4066. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  4067. prepStack();
  4068. (void)sqlite3_exec(db, argv[2], 0, 0, 0);
  4069. for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){}
  4070. Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4));
  4071. return TCL_OK;
  4072. }
  4073. /*
  4074. ** Usage: sqlite_delete_function DB function-name
  4075. **
  4076. ** Delete the user function 'function-name' from database handle DB. It
  4077. ** is assumed that the user function was created as UTF8, any number of
  4078. ** arguments (the way the TCL interface does it).
  4079. */
  4080. static int delete_function(
  4081. void * clientData,
  4082. Tcl_Interp *interp,
  4083. int argc,
  4084. char **argv
  4085. ){
  4086. int rc;
  4087. sqlite3 *db;
  4088. if( argc!=3 ){
  4089. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  4090. " DB function-name", 0);
  4091. return TCL_ERROR;
  4092. }
  4093. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  4094. rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
  4095. Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  4096. return TCL_OK;
  4097. }
  4098. /*
  4099. ** Usage: sqlite_delete_collation DB collation-name
  4100. **
  4101. ** Delete the collation sequence 'collation-name' from database handle
  4102. ** DB. It is assumed that the collation sequence was created as UTF8 (the
  4103. ** way the TCL interface does it).
  4104. */
  4105. static int delete_collation(
  4106. void * clientData,
  4107. Tcl_Interp *interp,
  4108. int argc,
  4109. char **argv
  4110. ){
  4111. int rc;
  4112. sqlite3 *db;
  4113. if( argc!=3 ){
  4114. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  4115. " DB function-name", 0);
  4116. return TCL_ERROR;
  4117. }
  4118. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  4119. rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
  4120. Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  4121. return TCL_OK;
  4122. }
  4123. /*
  4124. ** Usage: sqlite3_get_autocommit DB
  4125. **
  4126. ** Return true if the database DB is currently in auto-commit mode.
  4127. ** Return false if not.
  4128. */
  4129. static int get_autocommit(
  4130. void * clientData,
  4131. Tcl_Interp *interp,
  4132. int argc,
  4133. char **argv
  4134. ){
  4135. char zBuf[30];
  4136. sqlite3 *db;
  4137. if( argc!=2 ){
  4138. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  4139. " DB", 0);
  4140. return TCL_ERROR;
  4141. }
  4142. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  4143. sprintf(zBuf, "%d", sqlite3_get_autocommit(db));
  4144. Tcl_AppendResult(interp, zBuf, 0);
  4145. return TCL_OK;
  4146. }
  4147. /*
  4148. ** Usage: sqlite3_busy_timeout DB MS
  4149. **
  4150. ** Set the busy timeout. This is more easily done using the timeout
  4151. ** method of the TCL interface. But we need a way to test the case
  4152. ** where it returns SQLITE_MISUSE.
  4153. */
  4154. static int test_busy_timeout(
  4155. void * clientData,
  4156. Tcl_Interp *interp,
  4157. int argc,
  4158. char **argv
  4159. ){
  4160. int rc, ms;
  4161. sqlite3 *db;
  4162. if( argc!=3 ){
  4163. Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  4164. " DB", 0);
  4165. return TCL_ERROR;
  4166. }
  4167. if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  4168. if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR;
  4169. rc = sqlite3_busy_timeout(db, ms);
  4170. Tcl_AppendResult(interp, sqlite3ErrName(rc), 0);
  4171. return TCL_OK;
  4172. }
  4173. /*
  4174. ** Usage: tcl_variable_type VARIABLENAME
  4175. **
  4176. ** Return the name of the internal representation for the
  4177. ** value of the given variable.
  4178. */
  4179. static int tcl_variable_type(
  4180. void * clientData,
  4181. Tcl_Interp *interp,
  4182. int objc,
  4183. Tcl_Obj *CONST objv[]
  4184. ){
  4185. Tcl_Obj *pVar;
  4186. if( objc!=2 ){
  4187. Tcl_WrongNumArgs(interp, 1, objv, "VARIABLE");
  4188. return TCL_ERROR;
  4189. }
  4190. pVar = Tcl_GetVar2Ex(interp, Tcl_GetString(objv[1]), 0, TCL_LEAVE_ERR_MSG);
  4191. if( pVar==0 ) return TCL_ERROR;
  4192. if( pVar->typePtr ){
  4193. Tcl_SetObjResult(interp, Tcl_NewStringObj(pVar->typePtr->name, -1));
  4194. }
  4195. return TCL_OK;
  4196. }
  4197. /*
  4198. ** Usage: sqlite3_release_memory ?N?
  4199. **
  4200. ** Attempt to release memory currently held but not actually required.
  4201. ** The integer N is the number of bytes we are trying to release. The
  4202. ** return value is the amount of memory actually released.
  4203. */
  4204. static int test_release_memory(
  4205. void * clientData,
  4206. Tcl_Interp *interp,
  4207. int objc,
  4208. Tcl_Obj *CONST objv[]
  4209. ){
  4210. #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
  4211. int N;
  4212. int amt;
  4213. if( objc!=1 && objc!=2 ){
  4214. Tcl_WrongNumArgs(interp, 1, objv, "?N?");
  4215. return TCL_ERROR;
  4216. }
  4217. if( objc==2 ){
  4218. if( Tcl_GetIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
  4219. }else{
  4220. N = -1;
  4221. }
  4222. amt = sqlite3_release_memory(N);
  4223. Tcl_SetObjResult(interp, Tcl_NewIntObj(amt));
  4224. #endif
  4225. return TCL_OK;
  4226. }
  4227. /*
  4228. ** Usage: sqlite3_db_release_memory DB
  4229. **
  4230. ** Attempt to release memory currently held by database DB. Return the
  4231. ** result code (which in the current implementation is always zero).
  4232. */
  4233. static int test_db_release_memory(
  4234. void * clientData,
  4235. Tcl_Interp *interp,
  4236. int objc,
  4237. Tcl_Obj *CONST objv[]
  4238. ){
  4239. sqlite3 *db;
  4240. int rc;
  4241. if( objc!=2 ){
  4242. Tcl_WrongNumArgs(interp, 1, objv, "DB");
  4243. return TCL_ERROR;
  4244. }
  4245. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  4246. rc = sqlite3_db_release_memory(db);
  4247. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  4248. return TCL_OK;
  4249. }
  4250. /*
  4251. ** Usage: sqlite3_db_filename DB DBNAME
  4252. **
  4253. ** Return the name of a file associated with a database.
  4254. */
  4255. static int test_db_filename(
  4256. void * clientData,
  4257. Tcl_Interp *interp,
  4258. int objc,
  4259. Tcl_Obj *CONST objv[]
  4260. ){
  4261. sqlite3 *db;
  4262. const char *zDbName;
  4263. if( objc!=3 ){
  4264. Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
  4265. return TCL_ERROR;
  4266. }
  4267. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  4268. zDbName = Tcl_GetString(objv[2]);
  4269. Tcl_AppendResult(interp, sqlite3_db_filename(db, zDbName), (void*)0);
  4270. return TCL_OK;
  4271. }
  4272. /*
  4273. ** Usage: sqlite3_db_readonly DB DBNAME
  4274. **
  4275. ** Return 1 or 0 if DBNAME is readonly or not. Return -1 if DBNAME does
  4276. ** not exist.
  4277. */
  4278. static int test_db_readonly(
  4279. void * clientData,
  4280. Tcl_Interp *interp,
  4281. int objc,
  4282. Tcl_Obj *CONST objv[]
  4283. ){
  4284. sqlite3 *db;
  4285. const char *zDbName;
  4286. if( objc!=3 ){
  4287. Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME");
  4288. return TCL_ERROR;
  4289. }
  4290. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  4291. zDbName = Tcl_GetString(objv[2]);
  4292. Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_db_readonly(db, zDbName)));
  4293. return TCL_OK;
  4294. }
  4295. /*
  4296. ** Usage: sqlite3_soft_heap_limit ?N?
  4297. **
  4298. ** Query or set the soft heap limit for the current thread. The
  4299. ** limit is only changed if the N is present. The previous limit
  4300. ** is returned.
  4301. */
  4302. static int test_soft_heap_limit(
  4303. void * clientData,
  4304. Tcl_Interp *interp,
  4305. int objc,
  4306. Tcl_Obj *CONST objv[]
  4307. ){
  4308. sqlite3_int64 amt;
  4309. Tcl_WideInt N = -1;
  4310. if( objc!=1 && objc!=2 ){
  4311. Tcl_WrongNumArgs(interp, 1, objv, "?N?");
  4312. return TCL_ERROR;
  4313. }
  4314. if( objc==2 ){
  4315. if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR;
  4316. }
  4317. amt = sqlite3_soft_heap_limit64(N);
  4318. Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt));
  4319. return TCL_OK;
  4320. }
  4321. /*
  4322. ** Usage: sqlite3_thread_cleanup
  4323. **
  4324. ** Call the sqlite3_thread_cleanup API.
  4325. */
  4326. static int test_thread_cleanup(
  4327. void * clientData,
  4328. Tcl_Interp *interp,
  4329. int objc,
  4330. Tcl_Obj *CONST objv[]
  4331. ){
  4332. #ifndef SQLITE_OMIT_DEPRECATED
  4333. sqlite3_thread_cleanup();
  4334. #endif
  4335. return TCL_OK;
  4336. }
  4337. /*
  4338. ** Usage: sqlite3_pager_refcounts DB
  4339. **
  4340. ** Return a list of numbers which are the PagerRefcount for all
  4341. ** pagers on each database connection.
  4342. */
  4343. static int test_pager_refcounts(
  4344. void * clientData,
  4345. Tcl_Interp *interp,
  4346. int objc,
  4347. Tcl_Obj *CONST objv[]
  4348. ){
  4349. sqlite3 *db;
  4350. int i;
  4351. int v, *a;
  4352. Tcl_Obj *pResult;
  4353. if( objc!=2 ){
  4354. Tcl_AppendResult(interp, "wrong # args: should be \"",
  4355. Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
  4356. return TCL_ERROR;
  4357. }
  4358. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  4359. pResult = Tcl_NewObj();
  4360. for(i=0; i<db->nDb; i++){
  4361. if( db->aDb[i].pBt==0 ){
  4362. v = -1;
  4363. }else{
  4364. sqlite3_mutex_enter(db->mutex);
  4365. a = sqlite3PagerStats(sqlite3BtreePager(db->aDb[i].pBt));
  4366. v = a[0];
  4367. sqlite3_mutex_leave(db->mutex);
  4368. }
  4369. Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(v));
  4370. }
  4371. Tcl_SetObjResult(interp, pResult);
  4372. return TCL_OK;
  4373. }
  4374. /*
  4375. ** tclcmd: working_64bit_int
  4376. **
  4377. ** Some TCL builds (ex: cygwin) do not support 64-bit integers. This
  4378. ** leads to a number of test failures. The present command checks the
  4379. ** TCL build to see whether or not it supports 64-bit integers. It
  4380. ** returns TRUE if it does and FALSE if not.
  4381. **
  4382. ** This command is used to warn users that their TCL build is defective
  4383. ** and that the errors they are seeing in the test scripts might be
  4384. ** a result of their defective TCL rather than problems in SQLite.
  4385. */
  4386. static int working_64bit_int(
  4387. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4388. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4389. int objc, /* Number of arguments */
  4390. Tcl_Obj *CONST objv[] /* Command arguments */
  4391. ){
  4392. Tcl_Obj *pTestObj;
  4393. int working = 0;
  4394. pTestObj = Tcl_NewWideIntObj(1000000*(i64)1234567890);
  4395. working = strcmp(Tcl_GetString(pTestObj), "1234567890000000")==0;
  4396. Tcl_DecrRefCount(pTestObj);
  4397. Tcl_SetObjResult(interp, Tcl_NewBooleanObj(working));
  4398. return TCL_OK;
  4399. }
  4400. /*
  4401. ** tclcmd: vfs_unlink_test
  4402. **
  4403. ** This TCL command unregisters the primary VFS and then registers
  4404. ** it back again. This is used to test the ability to register a
  4405. ** VFS when none are previously registered, and the ability to
  4406. ** unregister the only available VFS. Ticket #2738
  4407. */
  4408. static int vfs_unlink_test(
  4409. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4410. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4411. int objc, /* Number of arguments */
  4412. Tcl_Obj *CONST objv[] /* Command arguments */
  4413. ){
  4414. int i;
  4415. sqlite3_vfs *pMain;
  4416. sqlite3_vfs *apVfs[20];
  4417. sqlite3_vfs one, two;
  4418. sqlite3_vfs_unregister(0); /* Unregister of NULL is harmless */
  4419. one.zName = "__one";
  4420. two.zName = "__two";
  4421. /* Calling sqlite3_vfs_register with 2nd argument of 0 does not
  4422. ** change the default VFS
  4423. */
  4424. pMain = sqlite3_vfs_find(0);
  4425. sqlite3_vfs_register(&one, 0);
  4426. assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
  4427. sqlite3_vfs_register(&two, 0);
  4428. assert( pMain==0 || pMain==sqlite3_vfs_find(0) );
  4429. /* We can find a VFS by its name */
  4430. assert( sqlite3_vfs_find("__one")==&one );
  4431. assert( sqlite3_vfs_find("__two")==&two );
  4432. /* Calling sqlite_vfs_register with non-zero second parameter changes the
  4433. ** default VFS, even if the 1st parameter is an existig VFS that is
  4434. ** previously registered as the non-default.
  4435. */
  4436. sqlite3_vfs_register(&one, 1);
  4437. assert( sqlite3_vfs_find("__one")==&one );
  4438. assert( sqlite3_vfs_find("__two")==&two );
  4439. assert( sqlite3_vfs_find(0)==&one );
  4440. sqlite3_vfs_register(&two, 1);
  4441. assert( sqlite3_vfs_find("__one")==&one );
  4442. assert( sqlite3_vfs_find("__two")==&two );
  4443. assert( sqlite3_vfs_find(0)==&two );
  4444. if( pMain ){
  4445. sqlite3_vfs_register(pMain, 1);
  4446. assert( sqlite3_vfs_find("__one")==&one );
  4447. assert( sqlite3_vfs_find("__two")==&two );
  4448. assert( sqlite3_vfs_find(0)==pMain );
  4449. }
  4450. /* Unlink the default VFS. Repeat until there are no more VFSes
  4451. ** registered.
  4452. */
  4453. for(i=0; i<sizeof(apVfs)/sizeof(apVfs[0]); i++){
  4454. apVfs[i] = sqlite3_vfs_find(0);
  4455. if( apVfs[i] ){
  4456. assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
  4457. sqlite3_vfs_unregister(apVfs[i]);
  4458. assert( 0==sqlite3_vfs_find(apVfs[i]->zName) );
  4459. }
  4460. }
  4461. assert( 0==sqlite3_vfs_find(0) );
  4462. /* Register the main VFS as non-default (will be made default, since
  4463. ** it'll be the only one in existence).
  4464. */
  4465. sqlite3_vfs_register(pMain, 0);
  4466. assert( sqlite3_vfs_find(0)==pMain );
  4467. /* Un-register the main VFS again to restore an empty VFS list */
  4468. sqlite3_vfs_unregister(pMain);
  4469. assert( 0==sqlite3_vfs_find(0) );
  4470. /* Relink all VFSes in reverse order. */
  4471. for(i=sizeof(apVfs)/sizeof(apVfs[0])-1; i>=0; i--){
  4472. if( apVfs[i] ){
  4473. sqlite3_vfs_register(apVfs[i], 1);
  4474. assert( apVfs[i]==sqlite3_vfs_find(0) );
  4475. assert( apVfs[i]==sqlite3_vfs_find(apVfs[i]->zName) );
  4476. }
  4477. }
  4478. /* Unregister out sample VFSes. */
  4479. sqlite3_vfs_unregister(&one);
  4480. sqlite3_vfs_unregister(&two);
  4481. /* Unregistering a VFS that is not currently registered is harmless */
  4482. sqlite3_vfs_unregister(&one);
  4483. sqlite3_vfs_unregister(&two);
  4484. assert( sqlite3_vfs_find("__one")==0 );
  4485. assert( sqlite3_vfs_find("__two")==0 );
  4486. /* We should be left with the original default VFS back as the
  4487. ** original */
  4488. assert( sqlite3_vfs_find(0)==pMain );
  4489. return TCL_OK;
  4490. }
  4491. /*
  4492. ** tclcmd: vfs_initfail_test
  4493. **
  4494. ** This TCL command attempts to vfs_find and vfs_register when the
  4495. ** sqlite3_initialize() interface is failing. All calls should fail.
  4496. */
  4497. static int vfs_initfail_test(
  4498. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4499. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4500. int objc, /* Number of arguments */
  4501. Tcl_Obj *CONST objv[] /* Command arguments */
  4502. ){
  4503. sqlite3_vfs one;
  4504. one.zName = "__one";
  4505. if( sqlite3_vfs_find(0) ) return TCL_ERROR;
  4506. sqlite3_vfs_register(&one, 0);
  4507. if( sqlite3_vfs_find(0) ) return TCL_ERROR;
  4508. sqlite3_vfs_register(&one, 1);
  4509. if( sqlite3_vfs_find(0) ) return TCL_ERROR;
  4510. return TCL_OK;
  4511. }
  4512. /*
  4513. ** Saved VFSes
  4514. */
  4515. static sqlite3_vfs *apVfs[20];
  4516. static int nVfs = 0;
  4517. /*
  4518. ** tclcmd: vfs_unregister_all
  4519. **
  4520. ** Unregister all VFSes.
  4521. */
  4522. static int vfs_unregister_all(
  4523. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4524. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4525. int objc, /* Number of arguments */
  4526. Tcl_Obj *CONST objv[] /* Command arguments */
  4527. ){
  4528. int i;
  4529. for(i=0; i<ArraySize(apVfs); i++){
  4530. apVfs[i] = sqlite3_vfs_find(0);
  4531. if( apVfs[i]==0 ) break;
  4532. sqlite3_vfs_unregister(apVfs[i]);
  4533. }
  4534. nVfs = i;
  4535. return TCL_OK;
  4536. }
  4537. /*
  4538. ** tclcmd: vfs_reregister_all
  4539. **
  4540. ** Restore all VFSes that were removed using vfs_unregister_all
  4541. */
  4542. static int vfs_reregister_all(
  4543. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4544. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4545. int objc, /* Number of arguments */
  4546. Tcl_Obj *CONST objv[] /* Command arguments */
  4547. ){
  4548. int i;
  4549. for(i=0; i<nVfs; i++){
  4550. sqlite3_vfs_register(apVfs[i], i==0);
  4551. }
  4552. return TCL_OK;
  4553. }
  4554. /*
  4555. ** tclcmd: file_control_test DB
  4556. **
  4557. ** This TCL command runs the sqlite3_file_control interface and
  4558. ** verifies correct operation of the same.
  4559. */
  4560. static int file_control_test(
  4561. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4562. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4563. int objc, /* Number of arguments */
  4564. Tcl_Obj *CONST objv[] /* Command arguments */
  4565. ){
  4566. int iArg = 0;
  4567. sqlite3 *db;
  4568. int rc;
  4569. if( objc!=2 ){
  4570. Tcl_AppendResult(interp, "wrong # args: should be \"",
  4571. Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
  4572. return TCL_ERROR;
  4573. }
  4574. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  4575. rc = sqlite3_file_control(db, 0, 0, &iArg);
  4576. assert( rc==SQLITE_NOTFOUND );
  4577. rc = sqlite3_file_control(db, "notadatabase", SQLITE_FCNTL_LOCKSTATE, &iArg);
  4578. assert( rc==SQLITE_ERROR );
  4579. rc = sqlite3_file_control(db, "main", -1, &iArg);
  4580. assert( rc==SQLITE_NOTFOUND );
  4581. rc = sqlite3_file_control(db, "temp", -1, &iArg);
  4582. assert( rc==SQLITE_NOTFOUND || rc==SQLITE_ERROR );
  4583. return TCL_OK;
  4584. }
  4585. /*
  4586. ** tclcmd: file_control_lasterrno_test DB
  4587. **
  4588. ** This TCL command runs the sqlite3_file_control interface and
  4589. ** verifies correct operation of the SQLITE_LAST_ERRNO verb.
  4590. */
  4591. static int file_control_lasterrno_test(
  4592. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4593. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4594. int objc, /* Number of arguments */
  4595. Tcl_Obj *CONST objv[] /* Command arguments */
  4596. ){
  4597. int iArg = 0;
  4598. sqlite3 *db;
  4599. int rc;
  4600. if( objc!=2 ){
  4601. Tcl_AppendResult(interp, "wrong # args: should be \"",
  4602. Tcl_GetStringFromObj(objv[0], 0), " DB", 0);
  4603. return TCL_ERROR;
  4604. }
  4605. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
  4606. return TCL_ERROR;
  4607. }
  4608. rc = sqlite3_file_control(db, NULL, SQLITE_LAST_ERRNO, &iArg);
  4609. if( rc ){
  4610. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  4611. return TCL_ERROR;
  4612. }
  4613. if( iArg!=0 ) {
  4614. Tcl_AppendResult(interp, "Unexpected non-zero errno: ",
  4615. Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0);
  4616. return TCL_ERROR;
  4617. }
  4618. return TCL_OK;
  4619. }
  4620. /*
  4621. ** tclcmd: file_control_chunksize_test DB DBNAME SIZE
  4622. **
  4623. ** This TCL command runs the sqlite3_file_control interface and
  4624. ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
  4625. ** SQLITE_SET_LOCKPROXYFILE verbs.
  4626. */
  4627. static int file_control_chunksize_test(
  4628. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4629. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4630. int objc, /* Number of arguments */
  4631. Tcl_Obj *CONST objv[] /* Command arguments */
  4632. ){
  4633. int nSize; /* New chunk size */
  4634. char *zDb; /* Db name ("main", "temp" etc.) */
  4635. sqlite3 *db; /* Database handle */
  4636. int rc; /* file_control() return code */
  4637. if( objc!=4 ){
  4638. Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE");
  4639. return TCL_ERROR;
  4640. }
  4641. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
  4642. || Tcl_GetIntFromObj(interp, objv[3], &nSize)
  4643. ){
  4644. return TCL_ERROR;
  4645. }
  4646. zDb = Tcl_GetString(objv[2]);
  4647. if( zDb[0]=='\0' ) zDb = NULL;
  4648. rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_CHUNK_SIZE, (void *)&nSize);
  4649. if( rc ){
  4650. Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
  4651. return TCL_ERROR;
  4652. }
  4653. return TCL_OK;
  4654. }
  4655. /*
  4656. ** tclcmd: file_control_sizehint_test DB DBNAME SIZE
  4657. **
  4658. ** This TCL command runs the sqlite3_file_control interface
  4659. ** with SQLITE_FCNTL_SIZE_HINT
  4660. */
  4661. static int file_control_sizehint_test(
  4662. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4663. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4664. int objc, /* Number of arguments */
  4665. Tcl_Obj *CONST objv[] /* Command arguments */
  4666. ){
  4667. Tcl_WideInt nSize; /* Hinted size */
  4668. char *zDb; /* Db name ("main", "temp" etc.) */
  4669. sqlite3 *db; /* Database handle */
  4670. int rc; /* file_control() return code */
  4671. if( objc!=4 ){
  4672. Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SIZE");
  4673. return TCL_ERROR;
  4674. }
  4675. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
  4676. || Tcl_GetWideIntFromObj(interp, objv[3], &nSize)
  4677. ){
  4678. return TCL_ERROR;
  4679. }
  4680. zDb = Tcl_GetString(objv[2]);
  4681. if( zDb[0]=='\0' ) zDb = NULL;
  4682. rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_SIZE_HINT, (void *)&nSize);
  4683. if( rc ){
  4684. Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
  4685. return TCL_ERROR;
  4686. }
  4687. return TCL_OK;
  4688. }
  4689. /*
  4690. ** tclcmd: file_control_lockproxy_test DB PWD
  4691. **
  4692. ** This TCL command runs the sqlite3_file_control interface and
  4693. ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
  4694. ** SQLITE_SET_LOCKPROXYFILE verbs.
  4695. */
  4696. static int file_control_lockproxy_test(
  4697. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4698. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4699. int objc, /* Number of arguments */
  4700. Tcl_Obj *CONST objv[] /* Command arguments */
  4701. ){
  4702. sqlite3 *db;
  4703. if( objc!=3 ){
  4704. Tcl_AppendResult(interp, "wrong # args: should be \"",
  4705. Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0);
  4706. return TCL_ERROR;
  4707. }
  4708. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
  4709. return TCL_ERROR;
  4710. }
  4711. #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
  4712. # if defined(__APPLE__)
  4713. # define SQLITE_ENABLE_LOCKING_STYLE 1
  4714. # else
  4715. # define SQLITE_ENABLE_LOCKING_STYLE 0
  4716. # endif
  4717. #endif
  4718. #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
  4719. {
  4720. char *testPath;
  4721. int rc;
  4722. int nPwd;
  4723. const char *zPwd;
  4724. char proxyPath[400];
  4725. zPwd = Tcl_GetStringFromObj(objv[2], &nPwd);
  4726. if( sizeof(proxyPath)<nPwd+20 ){
  4727. Tcl_AppendResult(interp, "PWD too big", (void*)0);
  4728. return TCL_ERROR;
  4729. }
  4730. sprintf(proxyPath, "%s/test.proxy", zPwd);
  4731. rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
  4732. if( rc ){
  4733. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  4734. return TCL_ERROR;
  4735. }
  4736. rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath);
  4737. if( strncmp(proxyPath,testPath,11) ){
  4738. Tcl_AppendResult(interp, "Lock proxy file did not match the "
  4739. "previously assigned value", 0);
  4740. return TCL_ERROR;
  4741. }
  4742. if( rc ){
  4743. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  4744. return TCL_ERROR;
  4745. }
  4746. rc = sqlite3_file_control(db, NULL, SQLITE_SET_LOCKPROXYFILE, proxyPath);
  4747. if( rc ){
  4748. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  4749. return TCL_ERROR;
  4750. }
  4751. }
  4752. #endif
  4753. return TCL_OK;
  4754. }
  4755. /*
  4756. ** tclcmd: file_control_win32_av_retry DB NRETRY DELAY
  4757. **
  4758. ** This TCL command runs the sqlite3_file_control interface with
  4759. ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode.
  4760. */
  4761. static int file_control_win32_av_retry(
  4762. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4763. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4764. int objc, /* Number of arguments */
  4765. Tcl_Obj *CONST objv[] /* Command arguments */
  4766. ){
  4767. sqlite3 *db;
  4768. int rc;
  4769. int a[2];
  4770. char z[100];
  4771. if( objc!=4 ){
  4772. Tcl_AppendResult(interp, "wrong # args: should be \"",
  4773. Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", 0);
  4774. return TCL_ERROR;
  4775. }
  4776. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
  4777. return TCL_ERROR;
  4778. }
  4779. if( Tcl_GetIntFromObj(interp, objv[2], &a[0]) ) return TCL_ERROR;
  4780. if( Tcl_GetIntFromObj(interp, objv[3], &a[1]) ) return TCL_ERROR;
  4781. rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_AV_RETRY, (void*)a);
  4782. sqlite3_snprintf(sizeof(z), z, "%d %d %d", rc, a[0], a[1]);
  4783. Tcl_AppendResult(interp, z, (char*)0);
  4784. return TCL_OK;
  4785. }
  4786. /*
  4787. ** tclcmd: file_control_persist_wal DB PERSIST-FLAG
  4788. **
  4789. ** This TCL command runs the sqlite3_file_control interface with
  4790. ** the SQLITE_FCNTL_PERSIST_WAL opcode.
  4791. */
  4792. static int file_control_persist_wal(
  4793. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4794. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4795. int objc, /* Number of arguments */
  4796. Tcl_Obj *CONST objv[] /* Command arguments */
  4797. ){
  4798. sqlite3 *db;
  4799. int rc;
  4800. int bPersist;
  4801. char z[100];
  4802. if( objc!=3 ){
  4803. Tcl_AppendResult(interp, "wrong # args: should be \"",
  4804. Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0);
  4805. return TCL_ERROR;
  4806. }
  4807. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
  4808. return TCL_ERROR;
  4809. }
  4810. if( Tcl_GetIntFromObj(interp, objv[2], &bPersist) ) return TCL_ERROR;
  4811. rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_PERSIST_WAL, (void*)&bPersist);
  4812. sqlite3_snprintf(sizeof(z), z, "%d %d", rc, bPersist);
  4813. Tcl_AppendResult(interp, z, (char*)0);
  4814. return TCL_OK;
  4815. }
  4816. /*
  4817. ** tclcmd: file_control_powersafe_overwrite DB PSOW-FLAG
  4818. **
  4819. ** This TCL command runs the sqlite3_file_control interface with
  4820. ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode.
  4821. */
  4822. static int file_control_powersafe_overwrite(
  4823. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4824. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4825. int objc, /* Number of arguments */
  4826. Tcl_Obj *CONST objv[] /* Command arguments */
  4827. ){
  4828. sqlite3 *db;
  4829. int rc;
  4830. int b;
  4831. char z[100];
  4832. if( objc!=3 ){
  4833. Tcl_AppendResult(interp, "wrong # args: should be \"",
  4834. Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0);
  4835. return TCL_ERROR;
  4836. }
  4837. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
  4838. return TCL_ERROR;
  4839. }
  4840. if( Tcl_GetIntFromObj(interp, objv[2], &b) ) return TCL_ERROR;
  4841. rc = sqlite3_file_control(db,NULL,SQLITE_FCNTL_POWERSAFE_OVERWRITE,(void*)&b);
  4842. sqlite3_snprintf(sizeof(z), z, "%d %d", rc, b);
  4843. Tcl_AppendResult(interp, z, (char*)0);
  4844. return TCL_OK;
  4845. }
  4846. /*
  4847. ** tclcmd: file_control_vfsname DB ?AUXDB?
  4848. **
  4849. ** Return a string that describes the stack of VFSes.
  4850. */
  4851. static int file_control_vfsname(
  4852. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4853. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4854. int objc, /* Number of arguments */
  4855. Tcl_Obj *CONST objv[] /* Command arguments */
  4856. ){
  4857. sqlite3 *db;
  4858. const char *zDbName = "main";
  4859. char *zVfsName = 0;
  4860. if( objc!=2 && objc!=3 ){
  4861. Tcl_AppendResult(interp, "wrong # args: should be \"",
  4862. Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
  4863. return TCL_ERROR;
  4864. }
  4865. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
  4866. return TCL_ERROR;
  4867. }
  4868. if( objc==3 ){
  4869. zDbName = Tcl_GetString(objv[2]);
  4870. }
  4871. sqlite3_file_control(db, zDbName, SQLITE_FCNTL_VFSNAME,(void*)&zVfsName);
  4872. Tcl_AppendResult(interp, zVfsName, (char*)0);
  4873. sqlite3_free(zVfsName);
  4874. return TCL_OK;
  4875. }
  4876. /*
  4877. ** tclcmd: file_control_tempfilename DB ?AUXDB?
  4878. **
  4879. ** Return a string that is a temporary filename
  4880. */
  4881. static int file_control_tempfilename(
  4882. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4883. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4884. int objc, /* Number of arguments */
  4885. Tcl_Obj *CONST objv[] /* Command arguments */
  4886. ){
  4887. sqlite3 *db;
  4888. const char *zDbName = "main";
  4889. char *zTName = 0;
  4890. if( objc!=2 && objc!=3 ){
  4891. Tcl_AppendResult(interp, "wrong # args: should be \"",
  4892. Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0);
  4893. return TCL_ERROR;
  4894. }
  4895. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
  4896. return TCL_ERROR;
  4897. }
  4898. if( objc==3 ){
  4899. zDbName = Tcl_GetString(objv[2]);
  4900. }
  4901. sqlite3_file_control(db, zDbName, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTName);
  4902. Tcl_AppendResult(interp, zTName, (char*)0);
  4903. sqlite3_free(zTName);
  4904. return TCL_OK;
  4905. }
  4906. /*
  4907. ** tclcmd: sqlite3_vfs_list
  4908. **
  4909. ** Return a tcl list containing the names of all registered vfs's.
  4910. */
  4911. static int vfs_list(
  4912. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4913. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4914. int objc, /* Number of arguments */
  4915. Tcl_Obj *CONST objv[] /* Command arguments */
  4916. ){
  4917. sqlite3_vfs *pVfs;
  4918. Tcl_Obj *pRet = Tcl_NewObj();
  4919. if( objc!=1 ){
  4920. Tcl_WrongNumArgs(interp, 1, objv, "");
  4921. return TCL_ERROR;
  4922. }
  4923. for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
  4924. Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1));
  4925. }
  4926. Tcl_SetObjResult(interp, pRet);
  4927. return TCL_OK;
  4928. }
  4929. /*
  4930. ** tclcmd: sqlite3_limit DB ID VALUE
  4931. **
  4932. ** This TCL command runs the sqlite3_limit interface and
  4933. ** verifies correct operation of the same.
  4934. */
  4935. static int test_limit(
  4936. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4937. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4938. int objc, /* Number of arguments */
  4939. Tcl_Obj *CONST objv[] /* Command arguments */
  4940. ){
  4941. sqlite3 *db;
  4942. int rc;
  4943. static const struct {
  4944. char *zName;
  4945. int id;
  4946. } aId[] = {
  4947. { "SQLITE_LIMIT_LENGTH", SQLITE_LIMIT_LENGTH },
  4948. { "SQLITE_LIMIT_SQL_LENGTH", SQLITE_LIMIT_SQL_LENGTH },
  4949. { "SQLITE_LIMIT_COLUMN", SQLITE_LIMIT_COLUMN },
  4950. { "SQLITE_LIMIT_EXPR_DEPTH", SQLITE_LIMIT_EXPR_DEPTH },
  4951. { "SQLITE_LIMIT_COMPOUND_SELECT", SQLITE_LIMIT_COMPOUND_SELECT },
  4952. { "SQLITE_LIMIT_VDBE_OP", SQLITE_LIMIT_VDBE_OP },
  4953. { "SQLITE_LIMIT_FUNCTION_ARG", SQLITE_LIMIT_FUNCTION_ARG },
  4954. { "SQLITE_LIMIT_ATTACHED", SQLITE_LIMIT_ATTACHED },
  4955. { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
  4956. { "SQLITE_LIMIT_VARIABLE_NUMBER", SQLITE_LIMIT_VARIABLE_NUMBER },
  4957. { "SQLITE_LIMIT_TRIGGER_DEPTH", SQLITE_LIMIT_TRIGGER_DEPTH },
  4958. /* Out of range test cases */
  4959. { "SQLITE_LIMIT_TOOSMALL", -1, },
  4960. { "SQLITE_LIMIT_TOOBIG", SQLITE_LIMIT_TRIGGER_DEPTH+1 },
  4961. };
  4962. int i, id;
  4963. int val;
  4964. const char *zId;
  4965. if( objc!=4 ){
  4966. Tcl_AppendResult(interp, "wrong # args: should be \"",
  4967. Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0);
  4968. return TCL_ERROR;
  4969. }
  4970. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  4971. zId = Tcl_GetString(objv[2]);
  4972. for(i=0; i<sizeof(aId)/sizeof(aId[0]); i++){
  4973. if( strcmp(zId, aId[i].zName)==0 ){
  4974. id = aId[i].id;
  4975. break;
  4976. }
  4977. }
  4978. if( i>=sizeof(aId)/sizeof(aId[0]) ){
  4979. Tcl_AppendResult(interp, "unknown limit type: ", zId, (char*)0);
  4980. return TCL_ERROR;
  4981. }
  4982. if( Tcl_GetIntFromObj(interp, objv[3], &val) ) return TCL_ERROR;
  4983. rc = sqlite3_limit(db, id, val);
  4984. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  4985. return TCL_OK;
  4986. }
  4987. /*
  4988. ** tclcmd: save_prng_state
  4989. **
  4990. ** Save the state of the pseudo-random number generator.
  4991. ** At the same time, verify that sqlite3_test_control works even when
  4992. ** called with an out-of-range opcode.
  4993. */
  4994. static int save_prng_state(
  4995. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  4996. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  4997. int objc, /* Number of arguments */
  4998. Tcl_Obj *CONST objv[] /* Command arguments */
  4999. ){
  5000. int rc = sqlite3_test_control(9999);
  5001. assert( rc==0 );
  5002. rc = sqlite3_test_control(-1);
  5003. assert( rc==0 );
  5004. sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SAVE);
  5005. return TCL_OK;
  5006. }
  5007. /*
  5008. ** tclcmd: restore_prng_state
  5009. */
  5010. static int restore_prng_state(
  5011. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  5012. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  5013. int objc, /* Number of arguments */
  5014. Tcl_Obj *CONST objv[] /* Command arguments */
  5015. ){
  5016. sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESTORE);
  5017. return TCL_OK;
  5018. }
  5019. /*
  5020. ** tclcmd: reset_prng_state
  5021. */
  5022. static int reset_prng_state(
  5023. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  5024. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  5025. int objc, /* Number of arguments */
  5026. Tcl_Obj *CONST objv[] /* Command arguments */
  5027. ){
  5028. sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET);
  5029. return TCL_OK;
  5030. }
  5031. /*
  5032. ** tclcmd: pcache_stats
  5033. */
  5034. static int test_pcache_stats(
  5035. ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  5036. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  5037. int objc, /* Number of arguments */
  5038. Tcl_Obj *CONST objv[] /* Command arguments */
  5039. ){
  5040. int nMin;
  5041. int nMax;
  5042. int nCurrent;
  5043. int nRecyclable;
  5044. Tcl_Obj *pRet;
  5045. sqlite3PcacheStats(&nCurrent, &nMax, &nMin, &nRecyclable);
  5046. pRet = Tcl_NewObj();
  5047. Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("current", -1));
  5048. Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCurrent));
  5049. Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("max", -1));
  5050. Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMax));
  5051. Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("min", -1));
  5052. Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nMin));
  5053. Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj("recyclable", -1));
  5054. Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nRecyclable));
  5055. Tcl_SetObjResult(interp, pRet);
  5056. return TCL_OK;
  5057. }
  5058. #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  5059. static void test_unlock_notify_cb(void **aArg, int nArg){
  5060. int ii;
  5061. for(ii=0; ii<nArg; ii++){
  5062. Tcl_EvalEx((Tcl_Interp *)aArg[ii], "unlock_notify", -1, TCL_EVAL_GLOBAL);
  5063. }
  5064. }
  5065. #endif /* SQLITE_ENABLE_UNLOCK_NOTIFY */
  5066. /*
  5067. ** tclcmd: sqlite3_unlock_notify db
  5068. */
  5069. #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  5070. static int test_unlock_notify(
  5071. ClientData clientData, /* Unused */
  5072. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  5073. int objc, /* Number of arguments */
  5074. Tcl_Obj *CONST objv[] /* Command arguments */
  5075. ){
  5076. sqlite3 *db;
  5077. int rc;
  5078. if( objc!=2 ){
  5079. Tcl_WrongNumArgs(interp, 1, objv, "DB");
  5080. return TCL_ERROR;
  5081. }
  5082. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
  5083. return TCL_ERROR;
  5084. }
  5085. rc = sqlite3_unlock_notify(db, test_unlock_notify_cb, (void *)interp);
  5086. Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  5087. return TCL_OK;
  5088. }
  5089. #endif
  5090. /*
  5091. ** tclcmd: sqlite3_wal_checkpoint db ?NAME?
  5092. */
  5093. static int test_wal_checkpoint(
  5094. ClientData clientData, /* Unused */
  5095. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  5096. int objc, /* Number of arguments */
  5097. Tcl_Obj *CONST objv[] /* Command arguments */
  5098. ){
  5099. char *zDb = 0;
  5100. sqlite3 *db;
  5101. int rc;
  5102. if( objc!=3 && objc!=2 ){
  5103. Tcl_WrongNumArgs(interp, 1, objv, "DB ?NAME?");
  5104. return TCL_ERROR;
  5105. }
  5106. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
  5107. return TCL_ERROR;
  5108. }
  5109. if( objc==3 ){
  5110. zDb = Tcl_GetString(objv[2]);
  5111. }
  5112. rc = sqlite3_wal_checkpoint(db, zDb);
  5113. Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  5114. return TCL_OK;
  5115. }
  5116. /*
  5117. ** tclcmd: sqlite3_wal_checkpoint_v2 db MODE ?NAME?
  5118. **
  5119. ** This command calls the wal_checkpoint_v2() function with the specified
  5120. ** mode argument (passive, full or restart). If present, the database name
  5121. ** NAME is passed as the second argument to wal_checkpoint_v2(). If it the
  5122. ** NAME argument is not present, a NULL pointer is passed instead.
  5123. **
  5124. ** If wal_checkpoint_v2() returns any value other than SQLITE_BUSY or
  5125. ** SQLITE_OK, then this command returns TCL_ERROR. The Tcl result is set
  5126. ** to the error message obtained from sqlite3_errmsg().
  5127. **
  5128. ** Otherwise, this command returns a list of three integers. The first integer
  5129. ** is 1 if SQLITE_BUSY was returned, or 0 otherwise. The following two integers
  5130. ** are the values returned via the output parameters by wal_checkpoint_v2() -
  5131. ** the number of frames in the log and the number of frames in the log
  5132. ** that have been checkpointed.
  5133. */
  5134. static int test_wal_checkpoint_v2(
  5135. ClientData clientData, /* Unused */
  5136. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  5137. int objc, /* Number of arguments */
  5138. Tcl_Obj *CONST objv[] /* Command arguments */
  5139. ){
  5140. char *zDb = 0;
  5141. sqlite3 *db;
  5142. int rc;
  5143. int eMode;
  5144. int nLog = -555;
  5145. int nCkpt = -555;
  5146. Tcl_Obj *pRet;
  5147. const char * aMode[] = { "passive", "full", "restart", 0 };
  5148. assert( SQLITE_CHECKPOINT_PASSIVE==0 );
  5149. assert( SQLITE_CHECKPOINT_FULL==1 );
  5150. assert( SQLITE_CHECKPOINT_RESTART==2 );
  5151. if( objc!=3 && objc!=4 ){
  5152. Tcl_WrongNumArgs(interp, 1, objv, "DB MODE ?NAME?");
  5153. return TCL_ERROR;
  5154. }
  5155. if( objc==4 ){
  5156. zDb = Tcl_GetString(objv[3]);
  5157. }
  5158. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db)
  5159. || Tcl_GetIndexFromObj(interp, objv[2], aMode, "mode", 0, &eMode)
  5160. ){
  5161. return TCL_ERROR;
  5162. }
  5163. rc = sqlite3_wal_checkpoint_v2(db, zDb, eMode, &nLog, &nCkpt);
  5164. if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
  5165. Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
  5166. return TCL_ERROR;
  5167. }
  5168. pRet = Tcl_NewObj();
  5169. Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(rc==SQLITE_BUSY?1:0));
  5170. Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nLog));
  5171. Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(nCkpt));
  5172. Tcl_SetObjResult(interp, pRet);
  5173. return TCL_OK;
  5174. }
  5175. /*
  5176. ** tclcmd: test_sqlite3_log ?SCRIPT?
  5177. */
  5178. static struct LogCallback {
  5179. Tcl_Interp *pInterp;
  5180. Tcl_Obj *pObj;
  5181. } logcallback = {0, 0};
  5182. static void xLogcallback(void *unused, int err, char *zMsg){
  5183. Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj);
  5184. Tcl_IncrRefCount(pNew);
  5185. Tcl_ListObjAppendElement(
  5186. 0, pNew, Tcl_NewStringObj(sqlite3ErrName(err), -1)
  5187. );
  5188. Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zMsg, -1));
  5189. Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT);
  5190. Tcl_DecrRefCount(pNew);
  5191. }
  5192. static int test_sqlite3_log(
  5193. ClientData clientData,
  5194. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  5195. int objc, /* Number of arguments */
  5196. Tcl_Obj *CONST objv[] /* Command arguments */
  5197. ){
  5198. if( objc>2 ){
  5199. Tcl_WrongNumArgs(interp, 1, objv, "SCRIPT");
  5200. return TCL_ERROR;
  5201. }
  5202. if( logcallback.pObj ){
  5203. Tcl_DecrRefCount(logcallback.pObj);
  5204. logcallback.pObj = 0;
  5205. logcallback.pInterp = 0;
  5206. sqlite3_config(SQLITE_CONFIG_LOG, 0, 0);
  5207. }
  5208. if( objc>1 ){
  5209. logcallback.pObj = objv[1];
  5210. Tcl_IncrRefCount(logcallback.pObj);
  5211. logcallback.pInterp = interp;
  5212. sqlite3_config(SQLITE_CONFIG_LOG, xLogcallback, 0);
  5213. }
  5214. return TCL_OK;
  5215. }
  5216. /*
  5217. ** tcl_objproc COMMANDNAME ARGS...
  5218. **
  5219. ** Run a TCL command using its objProc interface. Throw an error if
  5220. ** the command has no objProc interface.
  5221. */
  5222. static int runAsObjProc(
  5223. void * clientData,
  5224. Tcl_Interp *interp,
  5225. int objc,
  5226. Tcl_Obj *CONST objv[]
  5227. ){
  5228. Tcl_CmdInfo cmdInfo;
  5229. if( objc<2 ){
  5230. Tcl_WrongNumArgs(interp, 1, objv, "COMMAND ...");
  5231. return TCL_ERROR;
  5232. }
  5233. if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
  5234. Tcl_AppendResult(interp, "command not found: ",
  5235. Tcl_GetString(objv[1]), (char*)0);
  5236. return TCL_ERROR;
  5237. }
  5238. if( cmdInfo.objProc==0 ){
  5239. Tcl_AppendResult(interp, "command has no objProc: ",
  5240. Tcl_GetString(objv[1]), (char*)0);
  5241. return TCL_ERROR;
  5242. }
  5243. return cmdInfo.objProc(cmdInfo.objClientData, interp, objc-1, objv+1);
  5244. }
  5245. #ifndef SQLITE_OMIT_EXPLAIN
  5246. /*
  5247. ** WARNING: The following function, printExplainQueryPlan() is an exact
  5248. ** copy of example code from eqp.in (eqp.html). If this code is modified,
  5249. ** then the documentation copy needs to be modified as well.
  5250. */
  5251. /*
  5252. ** Argument pStmt is a prepared SQL statement. This function compiles
  5253. ** an EXPLAIN QUERY PLAN command to report on the prepared statement,
  5254. ** and prints the report to stdout using printf().
  5255. */
  5256. int printExplainQueryPlan(sqlite3_stmt *pStmt){
  5257. const char *zSql; /* Input SQL */
  5258. char *zExplain; /* SQL with EXPLAIN QUERY PLAN prepended */
  5259. sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */
  5260. int rc; /* Return code from sqlite3_prepare_v2() */
  5261. zSql = sqlite3_sql(pStmt);
  5262. if( zSql==0 ) return SQLITE_ERROR;
  5263. zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql);
  5264. if( zExplain==0 ) return SQLITE_NOMEM;
  5265. rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0);
  5266. sqlite3_free(zExplain);
  5267. if( rc!=SQLITE_OK ) return rc;
  5268. while( SQLITE_ROW==sqlite3_step(pExplain) ){
  5269. int iSelectid = sqlite3_column_int(pExplain, 0);
  5270. int iOrder = sqlite3_column_int(pExplain, 1);
  5271. int iFrom = sqlite3_column_int(pExplain, 2);
  5272. const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3);
  5273. printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail);
  5274. }
  5275. return sqlite3_finalize(pExplain);
  5276. }
  5277. static int test_print_eqp(
  5278. void * clientData,
  5279. Tcl_Interp *interp,
  5280. int objc,
  5281. Tcl_Obj *CONST objv[]
  5282. ){
  5283. int rc;
  5284. sqlite3_stmt *pStmt;
  5285. if( objc!=2 ){
  5286. Tcl_WrongNumArgs(interp, 1, objv, "STMT");
  5287. return TCL_ERROR;
  5288. }
  5289. if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  5290. rc = printExplainQueryPlan(pStmt);
  5291. /* This is needed on Windows so that a test case using this
  5292. ** function can open a read pipe and get the output of
  5293. ** printExplainQueryPlan() immediately.
  5294. */
  5295. fflush(stdout);
  5296. Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
  5297. return TCL_OK;
  5298. }
  5299. #endif /* SQLITE_OMIT_EXPLAIN */
  5300. /*
  5301. ** sqlite3_test_control VERB ARGS...
  5302. */
  5303. static int test_test_control(
  5304. void * clientData,
  5305. Tcl_Interp *interp,
  5306. int objc,
  5307. Tcl_Obj *CONST objv[]
  5308. ){
  5309. struct Verb {
  5310. const char *zName;
  5311. int i;
  5312. } aVerb[] = {
  5313. { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT },
  5314. };
  5315. int iVerb;
  5316. int iFlag;
  5317. int rc;
  5318. if( objc<2 ){
  5319. Tcl_WrongNumArgs(interp, 1, objv, "VERB ARGS...");
  5320. return TCL_ERROR;
  5321. }
  5322. rc = Tcl_GetIndexFromObjStruct(
  5323. interp, objv[1], aVerb, sizeof(aVerb[0]), "VERB", 0, &iVerb
  5324. );
  5325. if( rc!=TCL_OK ) return rc;
  5326. iFlag = aVerb[iVerb].i;
  5327. switch( iFlag ){
  5328. case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
  5329. int val;
  5330. if( objc!=3 ){
  5331. Tcl_WrongNumArgs(interp, 2, objv, "ONOFF");
  5332. return TCL_ERROR;
  5333. }
  5334. if( Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR;
  5335. sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, val);
  5336. break;
  5337. }
  5338. }
  5339. Tcl_ResetResult(interp);
  5340. return TCL_OK;
  5341. }
  5342. #if SQLITE_OS_UNIX
  5343. #include <sys/time.h>
  5344. #include <sys/resource.h>
  5345. static int test_getrusage(
  5346. void * clientData,
  5347. Tcl_Interp *interp,
  5348. int objc,
  5349. Tcl_Obj *CONST objv[]
  5350. ){
  5351. char buf[1024];
  5352. struct rusage r;
  5353. memset(&r, 0, sizeof(r));
  5354. getrusage(RUSAGE_SELF, &r);
  5355. sprintf(buf, "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d",
  5356. (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec,
  5357. (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec,
  5358. (int)r.ru_minflt, (int)r.ru_majflt
  5359. );
  5360. Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1));
  5361. return TCL_OK;
  5362. }
  5363. #endif
  5364. #if SQLITE_OS_WIN
  5365. /*
  5366. ** Information passed from the main thread into the windows file locker
  5367. ** background thread.
  5368. */
  5369. struct win32FileLocker {
  5370. char *evName; /* Name of event to signal thread startup */
  5371. HANDLE h; /* Handle of the file to be locked */
  5372. int delay1; /* Delay before locking */
  5373. int delay2; /* Delay before unlocking */
  5374. int ok; /* Finished ok */
  5375. int err; /* True if an error occurs */
  5376. };
  5377. #endif
  5378. #if SQLITE_OS_WIN
  5379. #include <process.h>
  5380. /*
  5381. ** The background thread that does file locking.
  5382. */
  5383. static void win32_file_locker(void *pAppData){
  5384. struct win32FileLocker *p = (struct win32FileLocker*)pAppData;
  5385. if( p->evName ){
  5386. HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName);
  5387. if ( ev ){
  5388. SetEvent(ev);
  5389. CloseHandle(ev);
  5390. }
  5391. }
  5392. if( p->delay1 ) Sleep(p->delay1);
  5393. if( LockFile(p->h, 0, 0, 100000000, 0) ){
  5394. Sleep(p->delay2);
  5395. UnlockFile(p->h, 0, 0, 100000000, 0);
  5396. p->ok = 1;
  5397. }else{
  5398. p->err = 1;
  5399. }
  5400. CloseHandle(p->h);
  5401. p->h = 0;
  5402. p->delay1 = 0;
  5403. p->delay2 = 0;
  5404. }
  5405. #endif
  5406. #if SQLITE_OS_WIN
  5407. /*
  5408. ** lock_win32_file FILENAME DELAY1 DELAY2
  5409. **
  5410. ** Get an exclusive manditory lock on file for DELAY2 milliseconds.
  5411. ** Wait DELAY1 milliseconds before acquiring the lock.
  5412. */
  5413. static int win32_file_lock(
  5414. void * clientData,
  5415. Tcl_Interp *interp,
  5416. int objc,
  5417. Tcl_Obj *CONST objv[]
  5418. ){
  5419. static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 };
  5420. const char *zFilename;
  5421. char zBuf[200];
  5422. int retry = 0;
  5423. HANDLE ev;
  5424. DWORD wResult;
  5425. if( objc!=4 && objc!=1 ){
  5426. Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2");
  5427. return TCL_ERROR;
  5428. }
  5429. if( objc==1 ){
  5430. sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d",
  5431. x.ok, x.err, x.delay1, x.delay2, x.h);
  5432. Tcl_AppendResult(interp, zBuf, (char*)0);
  5433. return TCL_OK;
  5434. }
  5435. while( x.h && retry<30 ){
  5436. retry++;
  5437. Sleep(100);
  5438. }
  5439. if( x.h ){
  5440. Tcl_AppendResult(interp, "busy", (char*)0);
  5441. return TCL_ERROR;
  5442. }
  5443. if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR;
  5444. if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR;
  5445. zFilename = Tcl_GetString(objv[1]);
  5446. x.h = CreateFile(zFilename, GENERIC_READ|GENERIC_WRITE,
  5447. FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS,
  5448. FILE_ATTRIBUTE_NORMAL, 0);
  5449. if( !x.h ){
  5450. Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0);
  5451. return TCL_ERROR;
  5452. }
  5453. ev = CreateEvent(NULL, TRUE, FALSE, x.evName);
  5454. if ( !ev ){
  5455. Tcl_AppendResult(interp, "cannot create event: ", x.evName, (char*)0);
  5456. return TCL_ERROR;
  5457. }
  5458. _beginthread(win32_file_locker, 0, (void*)&x);
  5459. Sleep(0);
  5460. if ( (wResult = WaitForSingleObject(ev, 10000))!=WAIT_OBJECT_0 ){
  5461. sqlite3_snprintf(sizeof(zBuf), zBuf, "0x%x", wResult);
  5462. Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0);
  5463. CloseHandle(ev);
  5464. return TCL_ERROR;
  5465. }
  5466. CloseHandle(ev);
  5467. return TCL_OK;
  5468. }
  5469. /*
  5470. ** exists_win32_path PATH
  5471. **
  5472. ** Returns non-zero if the specified path exists, whose fully qualified name
  5473. ** may exceed 260 characters if it is prefixed with "\\?\".
  5474. */
  5475. static int win32_exists_path(
  5476. void *clientData,
  5477. Tcl_Interp *interp,
  5478. int objc,
  5479. Tcl_Obj *CONST objv[]
  5480. ){
  5481. if( objc!=2 ){
  5482. Tcl_WrongNumArgs(interp, 1, objv, "PATH");
  5483. return TCL_ERROR;
  5484. }
  5485. Tcl_SetObjResult(interp, Tcl_NewBooleanObj(
  5486. GetFileAttributesW( Tcl_GetUnicode(objv[1]))!=INVALID_FILE_ATTRIBUTES ));
  5487. return TCL_OK;
  5488. }
  5489. /*
  5490. ** find_win32_file PATTERN
  5491. **
  5492. ** Returns a list of entries in a directory that match the specified pattern,
  5493. ** whose fully qualified name may exceed 248 characters if it is prefixed with
  5494. ** "\\?\".
  5495. */
  5496. static int win32_find_file(
  5497. void *clientData,
  5498. Tcl_Interp *interp,
  5499. int objc,
  5500. Tcl_Obj *CONST objv[]
  5501. ){
  5502. HANDLE hFindFile = INVALID_HANDLE_VALUE;
  5503. WIN32_FIND_DATAW findData;
  5504. Tcl_Obj *listObj;
  5505. DWORD lastErrno;
  5506. if( objc!=2 ){
  5507. Tcl_WrongNumArgs(interp, 1, objv, "PATTERN");
  5508. return TCL_ERROR;
  5509. }
  5510. hFindFile = FindFirstFileW(Tcl_GetUnicode(objv[1]), &findData);
  5511. if( hFindFile==INVALID_HANDLE_VALUE ){
  5512. Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
  5513. return TCL_ERROR;
  5514. }
  5515. listObj = Tcl_NewObj();
  5516. Tcl_IncrRefCount(listObj);
  5517. do {
  5518. Tcl_ListObjAppendElement(interp, listObj, Tcl_NewUnicodeObj(
  5519. findData.cFileName, -1));
  5520. Tcl_ListObjAppendElement(interp, listObj, Tcl_NewWideIntObj(
  5521. findData.dwFileAttributes));
  5522. } while( FindNextFileW(hFindFile, &findData) );
  5523. lastErrno = GetLastError();
  5524. if( lastErrno!=NO_ERROR && lastErrno!=ERROR_NO_MORE_FILES ){
  5525. FindClose(hFindFile);
  5526. Tcl_DecrRefCount(listObj);
  5527. Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
  5528. return TCL_ERROR;
  5529. }
  5530. FindClose(hFindFile);
  5531. Tcl_SetObjResult(interp, listObj);
  5532. return TCL_OK;
  5533. }
  5534. /*
  5535. ** delete_win32_file FILENAME
  5536. **
  5537. ** Deletes the specified file, whose fully qualified name may exceed 260
  5538. ** characters if it is prefixed with "\\?\".
  5539. */
  5540. static int win32_delete_file(
  5541. void *clientData,
  5542. Tcl_Interp *interp,
  5543. int objc,
  5544. Tcl_Obj *CONST objv[]
  5545. ){
  5546. if( objc!=2 ){
  5547. Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
  5548. return TCL_ERROR;
  5549. }
  5550. if( !DeleteFileW(Tcl_GetUnicode(objv[1])) ){
  5551. Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
  5552. return TCL_ERROR;
  5553. }
  5554. Tcl_ResetResult(interp);
  5555. return TCL_OK;
  5556. }
  5557. /*
  5558. ** make_win32_dir DIRECTORY
  5559. **
  5560. ** Creates the specified directory, whose fully qualified name may exceed 248
  5561. ** characters if it is prefixed with "\\?\".
  5562. */
  5563. static int win32_mkdir(
  5564. void *clientData,
  5565. Tcl_Interp *interp,
  5566. int objc,
  5567. Tcl_Obj *CONST objv[]
  5568. ){
  5569. if( objc!=2 ){
  5570. Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
  5571. return TCL_ERROR;
  5572. }
  5573. if( !CreateDirectoryW(Tcl_GetUnicode(objv[1]), NULL) ){
  5574. Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
  5575. return TCL_ERROR;
  5576. }
  5577. Tcl_ResetResult(interp);
  5578. return TCL_OK;
  5579. }
  5580. /*
  5581. ** remove_win32_dir DIRECTORY
  5582. **
  5583. ** Removes the specified directory, whose fully qualified name may exceed 248
  5584. ** characters if it is prefixed with "\\?\".
  5585. */
  5586. static int win32_rmdir(
  5587. void *clientData,
  5588. Tcl_Interp *interp,
  5589. int objc,
  5590. Tcl_Obj *CONST objv[]
  5591. ){
  5592. if( objc!=2 ){
  5593. Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
  5594. return TCL_ERROR;
  5595. }
  5596. if( !RemoveDirectoryW(Tcl_GetUnicode(objv[1])) ){
  5597. Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
  5598. return TCL_ERROR;
  5599. }
  5600. Tcl_ResetResult(interp);
  5601. return TCL_OK;
  5602. }
  5603. #endif
  5604. /*
  5605. ** optimization_control DB OPT BOOLEAN
  5606. **
  5607. ** Enable or disable query optimizations using the sqlite3_test_control()
  5608. ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true.
  5609. ** OPT is the name of the optimization to be disabled.
  5610. */
  5611. static int optimization_control(
  5612. void * clientData,
  5613. Tcl_Interp *interp,
  5614. int objc,
  5615. Tcl_Obj *CONST objv[]
  5616. ){
  5617. int i;
  5618. sqlite3 *db;
  5619. const char *zOpt;
  5620. int onoff;
  5621. int mask = 0;
  5622. static const struct {
  5623. const char *zOptName;
  5624. int mask;
  5625. } aOpt[] = {
  5626. { "all", SQLITE_AllOpts },
  5627. { "none", 0 },
  5628. { "query-flattener", SQLITE_QueryFlattener },
  5629. { "column-cache", SQLITE_ColumnCache },
  5630. { "groupby-order", SQLITE_GroupByOrder },
  5631. { "factor-constants", SQLITE_FactorOutConst },
  5632. { "real-as-int", SQLITE_IdxRealAsInt },
  5633. { "distinct-opt", SQLITE_DistinctOpt },
  5634. { "cover-idx-scan", SQLITE_CoverIdxScan },
  5635. { "order-by-idx-join", SQLITE_OrderByIdxJoin },
  5636. { "transitive", SQLITE_Transitive },
  5637. { "subquery-coroutine", SQLITE_SubqCoroutine },
  5638. { "omit-noop-join", SQLITE_OmitNoopJoin },
  5639. { "stat3", SQLITE_Stat3 },
  5640. };
  5641. if( objc!=4 ){
  5642. Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN");
  5643. return TCL_ERROR;
  5644. }
  5645. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  5646. if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR;
  5647. zOpt = Tcl_GetString(objv[2]);
  5648. for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
  5649. if( strcmp(zOpt, aOpt[i].zOptName)==0 ){
  5650. mask = aOpt[i].mask;
  5651. break;
  5652. }
  5653. }
  5654. if( onoff ) mask = ~mask;
  5655. if( i>=sizeof(aOpt)/sizeof(aOpt[0]) ){
  5656. Tcl_AppendResult(interp, "unknown optimization - should be one of:",
  5657. (char*)0);
  5658. for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
  5659. Tcl_AppendResult(interp, " ", aOpt[i].zOptName, (char*)0);
  5660. }
  5661. return TCL_ERROR;
  5662. }
  5663. sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask);
  5664. return TCL_OK;
  5665. }
  5666. typedef struct sqlite3_api_routines sqlite3_api_routines;
  5667. /*
  5668. ** load_static_extension DB NAME ...
  5669. **
  5670. ** Load one or more statically linked extensions.
  5671. */
  5672. static int tclLoadStaticExtensionCmd(
  5673. void * clientData,
  5674. Tcl_Interp *interp,
  5675. int objc,
  5676. Tcl_Obj *CONST objv[]
  5677. ){
  5678. extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*);
  5679. extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*);
  5680. extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*);
  5681. extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*);
  5682. extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*);
  5683. extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*);
  5684. extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*);
  5685. extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*);
  5686. extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*);
  5687. extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*);
  5688. static const struct {
  5689. const char *zExtName;
  5690. int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*);
  5691. } aExtension[] = {
  5692. { "amatch", sqlite3_amatch_init },
  5693. { "closure", sqlite3_closure_init },
  5694. { "fuzzer", sqlite3_fuzzer_init },
  5695. { "ieee754", sqlite3_ieee_init },
  5696. { "nextchar", sqlite3_nextchar_init },
  5697. { "percentile", sqlite3_percentile_init },
  5698. { "regexp", sqlite3_regexp_init },
  5699. { "spellfix", sqlite3_spellfix_init },
  5700. { "totype", sqlite3_totype_init },
  5701. { "wholenumber", sqlite3_wholenumber_init },
  5702. };
  5703. sqlite3 *db;
  5704. const char *zName;
  5705. int i, j, rc;
  5706. char *zErrMsg = 0;
  5707. if( objc<3 ){
  5708. Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ...");
  5709. return TCL_ERROR;
  5710. }
  5711. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  5712. for(j=2; j<objc; j++){
  5713. zName = Tcl_GetString(objv[j]);
  5714. for(i=0; i<ArraySize(aExtension); i++){
  5715. if( strcmp(zName, aExtension[i].zExtName)==0 ) break;
  5716. }
  5717. if( i>=ArraySize(aExtension) ){
  5718. Tcl_AppendResult(interp, "no such extension: ", zName, (char*)0);
  5719. return TCL_ERROR;
  5720. }
  5721. rc = aExtension[i].pInit(db, &zErrMsg, 0);
  5722. if( rc!=SQLITE_OK || zErrMsg ){
  5723. Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg,
  5724. (char*)0);
  5725. sqlite3_free(zErrMsg);
  5726. return TCL_ERROR;
  5727. }
  5728. }
  5729. return TCL_OK;
  5730. }
  5731. /*
  5732. ** Register commands with the TCL interpreter.
  5733. */
  5734. int Sqlitetest1_Init(Tcl_Interp *interp){
  5735. extern int sqlite3_search_count;
  5736. extern int sqlite3_found_count;
  5737. extern int sqlite3_interrupt_count;
  5738. extern int sqlite3_open_file_count;
  5739. extern int sqlite3_sort_count;
  5740. extern int sqlite3_current_time;
  5741. #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
  5742. extern int sqlite3_hostid_num;
  5743. #endif
  5744. extern int sqlite3_max_blobsize;
  5745. extern int sqlite3BtreeSharedCacheReport(void*,
  5746. Tcl_Interp*,int,Tcl_Obj*CONST*);
  5747. static struct {
  5748. char *zName;
  5749. Tcl_CmdProc *xProc;
  5750. } aCmd[] = {
  5751. { "db_enter", (Tcl_CmdProc*)db_enter },
  5752. { "db_leave", (Tcl_CmdProc*)db_leave },
  5753. { "sqlite3_mprintf_int", (Tcl_CmdProc*)sqlite3_mprintf_int },
  5754. { "sqlite3_mprintf_int64", (Tcl_CmdProc*)sqlite3_mprintf_int64 },
  5755. { "sqlite3_mprintf_long", (Tcl_CmdProc*)sqlite3_mprintf_long },
  5756. { "sqlite3_mprintf_str", (Tcl_CmdProc*)sqlite3_mprintf_str },
  5757. { "sqlite3_snprintf_str", (Tcl_CmdProc*)sqlite3_snprintf_str },
  5758. { "sqlite3_mprintf_stronly", (Tcl_CmdProc*)sqlite3_mprintf_stronly},
  5759. { "sqlite3_mprintf_double", (Tcl_CmdProc*)sqlite3_mprintf_double },
  5760. { "sqlite3_mprintf_scaled", (Tcl_CmdProc*)sqlite3_mprintf_scaled },
  5761. { "sqlite3_mprintf_hexdouble", (Tcl_CmdProc*)sqlite3_mprintf_hexdouble},
  5762. { "sqlite3_mprintf_z_test", (Tcl_CmdProc*)test_mprintf_z },
  5763. { "sqlite3_mprintf_n_test", (Tcl_CmdProc*)test_mprintf_n },
  5764. { "sqlite3_snprintf_int", (Tcl_CmdProc*)test_snprintf_int },
  5765. { "sqlite3_last_insert_rowid", (Tcl_CmdProc*)test_last_rowid },
  5766. { "sqlite3_exec_printf", (Tcl_CmdProc*)test_exec_printf },
  5767. { "sqlite3_exec_hex", (Tcl_CmdProc*)test_exec_hex },
  5768. { "sqlite3_exec", (Tcl_CmdProc*)test_exec },
  5769. { "sqlite3_exec_nr", (Tcl_CmdProc*)test_exec_nr },
  5770. #ifndef SQLITE_OMIT_GET_TABLE
  5771. { "sqlite3_get_table_printf", (Tcl_CmdProc*)test_get_table_printf },
  5772. #endif
  5773. { "sqlite3_close", (Tcl_CmdProc*)sqlite_test_close },
  5774. { "sqlite3_close_v2", (Tcl_CmdProc*)sqlite_test_close_v2 },
  5775. { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function },
  5776. { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate },
  5777. { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func },
  5778. { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort },
  5779. { "sqlite_bind", (Tcl_CmdProc*)test_bind },
  5780. { "breakpoint", (Tcl_CmdProc*)test_breakpoint },
  5781. { "sqlite3_key", (Tcl_CmdProc*)test_key },
  5782. { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey },
  5783. { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic },
  5784. { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt },
  5785. { "sqlite_delete_function", (Tcl_CmdProc*)delete_function },
  5786. { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation },
  5787. { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit },
  5788. { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used },
  5789. { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout },
  5790. { "printf", (Tcl_CmdProc*)test_printf },
  5791. { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace },
  5792. };
  5793. static struct {
  5794. char *zName;
  5795. Tcl_ObjCmdProc *xProc;
  5796. void *clientData;
  5797. } aObjCmd[] = {
  5798. { "sqlite3_connection_pointer", get_sqlite_pointer, 0 },
  5799. { "sqlite3_bind_int", test_bind_int, 0 },
  5800. { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 },
  5801. { "sqlite3_bind_int64", test_bind_int64, 0 },
  5802. { "sqlite3_bind_double", test_bind_double, 0 },
  5803. { "sqlite3_bind_null", test_bind_null ,0 },
  5804. { "sqlite3_bind_text", test_bind_text ,0 },
  5805. { "sqlite3_bind_text16", test_bind_text16 ,0 },
  5806. { "sqlite3_bind_blob", test_bind_blob ,0 },
  5807. { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0},
  5808. { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0},
  5809. { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0},
  5810. { "sqlite3_clear_bindings", test_clear_bindings, 0},
  5811. { "sqlite3_sleep", test_sleep, 0},
  5812. { "sqlite3_errcode", test_errcode ,0 },
  5813. { "sqlite3_extended_errcode", test_ex_errcode ,0 },
  5814. { "sqlite3_errmsg", test_errmsg ,0 },
  5815. { "sqlite3_errmsg16", test_errmsg16 ,0 },
  5816. { "sqlite3_open", test_open ,0 },
  5817. { "sqlite3_open16", test_open16 ,0 },
  5818. { "sqlite3_open_v2", test_open_v2 ,0 },
  5819. { "sqlite3_complete16", test_complete16 ,0 },
  5820. { "sqlite3_prepare", test_prepare ,0 },
  5821. { "sqlite3_prepare16", test_prepare16 ,0 },
  5822. { "sqlite3_prepare_v2", test_prepare_v2 ,0 },
  5823. { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0},
  5824. { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 },
  5825. { "sqlite3_finalize", test_finalize ,0 },
  5826. { "sqlite3_stmt_status", test_stmt_status ,0 },
  5827. { "sqlite3_reset", test_reset ,0 },
  5828. { "sqlite3_expired", test_expired ,0 },
  5829. { "sqlite3_transfer_bindings", test_transfer_bind ,0 },
  5830. { "sqlite3_changes", test_changes ,0 },
  5831. { "sqlite3_step", test_step ,0 },
  5832. { "sqlite3_sql", test_sql ,0 },
  5833. { "sqlite3_next_stmt", test_next_stmt ,0 },
  5834. { "sqlite3_stmt_readonly", test_stmt_readonly ,0 },
  5835. { "sqlite3_stmt_busy", test_stmt_busy ,0 },
  5836. { "uses_stmt_journal", uses_stmt_journal ,0 },
  5837. { "sqlite3_release_memory", test_release_memory, 0},
  5838. { "sqlite3_db_release_memory", test_db_release_memory, 0},
  5839. { "sqlite3_db_filename", test_db_filename, 0},
  5840. { "sqlite3_db_readonly", test_db_readonly, 0},
  5841. { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0},
  5842. { "sqlite3_thread_cleanup", test_thread_cleanup, 0},
  5843. { "sqlite3_pager_refcounts", test_pager_refcounts, 0},
  5844. { "sqlite3_load_extension", test_load_extension, 0},
  5845. { "sqlite3_enable_load_extension", test_enable_load, 0},
  5846. { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
  5847. { "sqlite3_limit", test_limit, 0},
  5848. { "save_prng_state", save_prng_state, 0 },
  5849. { "restore_prng_state", restore_prng_state, 0 },
  5850. { "reset_prng_state", reset_prng_state, 0 },
  5851. { "optimization_control", optimization_control,0},
  5852. #if SQLITE_OS_WIN
  5853. { "lock_win32_file", win32_file_lock, 0 },
  5854. { "exists_win32_path", win32_exists_path, 0 },
  5855. { "find_win32_file", win32_find_file, 0 },
  5856. { "delete_win32_file", win32_delete_file, 0 },
  5857. { "make_win32_dir", win32_mkdir, 0 },
  5858. { "remove_win32_dir", win32_rmdir, 0 },
  5859. #endif
  5860. { "tcl_objproc", runAsObjProc, 0 },
  5861. /* sqlite3_column_*() API */
  5862. { "sqlite3_column_count", test_column_count ,0 },
  5863. { "sqlite3_data_count", test_data_count ,0 },
  5864. { "sqlite3_column_type", test_column_type ,0 },
  5865. { "sqlite3_column_blob", test_column_blob ,0 },
  5866. { "sqlite3_column_double", test_column_double ,0 },
  5867. { "sqlite3_column_int64", test_column_int64 ,0 },
  5868. { "sqlite3_column_text", test_stmt_utf8, (void*)sqlite3_column_text },
  5869. { "sqlite3_column_name", test_stmt_utf8, (void*)sqlite3_column_name },
  5870. { "sqlite3_column_int", test_stmt_int, (void*)sqlite3_column_int },
  5871. { "sqlite3_column_bytes", test_stmt_int, (void*)sqlite3_column_bytes},
  5872. #ifndef SQLITE_OMIT_DECLTYPE
  5873. { "sqlite3_column_decltype",test_stmt_utf8,(void*)sqlite3_column_decltype},
  5874. #endif
  5875. #ifdef SQLITE_ENABLE_COLUMN_METADATA
  5876. { "sqlite3_column_database_name",test_stmt_utf8,(void*)sqlite3_column_database_name},
  5877. { "sqlite3_column_table_name",test_stmt_utf8,(void*)sqlite3_column_table_name},
  5878. { "sqlite3_column_origin_name",test_stmt_utf8,(void*)sqlite3_column_origin_name},
  5879. #endif
  5880. #ifndef SQLITE_OMIT_UTF16
  5881. { "sqlite3_column_bytes16", test_stmt_int, (void*)sqlite3_column_bytes16 },
  5882. { "sqlite3_column_text16", test_stmt_utf16, (void*)sqlite3_column_text16},
  5883. { "sqlite3_column_name16", test_stmt_utf16, (void*)sqlite3_column_name16},
  5884. { "add_alignment_test_collations", add_alignment_test_collations, 0 },
  5885. #ifndef SQLITE_OMIT_DECLTYPE
  5886. { "sqlite3_column_decltype16",test_stmt_utf16,(void*)sqlite3_column_decltype16},
  5887. #endif
  5888. #ifdef SQLITE_ENABLE_COLUMN_METADATA
  5889. {"sqlite3_column_database_name16",
  5890. test_stmt_utf16, (void*)sqlite3_column_database_name16},
  5891. {"sqlite3_column_table_name16", test_stmt_utf16, (void*)sqlite3_column_table_name16},
  5892. {"sqlite3_column_origin_name16", test_stmt_utf16, (void*)sqlite3_column_origin_name16},
  5893. #endif
  5894. #endif
  5895. { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
  5896. { "sqlite3_global_recover", test_global_recover, 0 },
  5897. { "working_64bit_int", working_64bit_int, 0 },
  5898. { "vfs_unlink_test", vfs_unlink_test, 0 },
  5899. { "vfs_initfail_test", vfs_initfail_test, 0 },
  5900. { "vfs_unregister_all", vfs_unregister_all, 0 },
  5901. { "vfs_reregister_all", vfs_reregister_all, 0 },
  5902. { "file_control_test", file_control_test, 0 },
  5903. { "file_control_lasterrno_test", file_control_lasterrno_test, 0 },
  5904. { "file_control_lockproxy_test", file_control_lockproxy_test, 0 },
  5905. { "file_control_chunksize_test", file_control_chunksize_test, 0 },
  5906. { "file_control_sizehint_test", file_control_sizehint_test, 0 },
  5907. { "file_control_win32_av_retry", file_control_win32_av_retry, 0 },
  5908. { "file_control_persist_wal", file_control_persist_wal, 0 },
  5909. { "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0},
  5910. { "file_control_vfsname", file_control_vfsname, 0 },
  5911. { "file_control_tempfilename", file_control_tempfilename, 0 },
  5912. { "sqlite3_vfs_list", vfs_list, 0 },
  5913. { "sqlite3_create_function_v2", test_create_function_v2, 0 },
  5914. /* Functions from os.h */
  5915. #ifndef SQLITE_OMIT_UTF16
  5916. { "add_test_collate", test_collate, 0 },
  5917. { "add_test_collate_needed", test_collate_needed, 0 },
  5918. { "add_test_function", test_function, 0 },
  5919. #endif
  5920. { "sqlite3_test_errstr", test_errstr, 0 },
  5921. { "tcl_variable_type", tcl_variable_type, 0 },
  5922. #ifndef SQLITE_OMIT_SHARED_CACHE
  5923. { "sqlite3_enable_shared_cache", test_enable_shared, 0 },
  5924. { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0},
  5925. #endif
  5926. { "sqlite3_libversion_number", test_libversion_number, 0 },
  5927. #ifdef SQLITE_ENABLE_COLUMN_METADATA
  5928. { "sqlite3_table_column_metadata", test_table_column_metadata, 0 },
  5929. #endif
  5930. #ifndef SQLITE_OMIT_INCRBLOB
  5931. { "sqlite3_blob_read", test_blob_read, 0 },
  5932. { "sqlite3_blob_write", test_blob_write, 0 },
  5933. { "sqlite3_blob_reopen", test_blob_reopen, 0 },
  5934. { "sqlite3_blob_bytes", test_blob_bytes, 0 },
  5935. { "sqlite3_blob_close", test_blob_close, 0 },
  5936. #endif
  5937. { "pcache_stats", test_pcache_stats, 0 },
  5938. #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  5939. { "sqlite3_unlock_notify", test_unlock_notify, 0 },
  5940. #endif
  5941. { "sqlite3_wal_checkpoint", test_wal_checkpoint, 0 },
  5942. { "sqlite3_wal_checkpoint_v2",test_wal_checkpoint_v2, 0 },
  5943. { "test_sqlite3_log", test_sqlite3_log, 0 },
  5944. #ifndef SQLITE_OMIT_EXPLAIN
  5945. { "print_explain_query_plan", test_print_eqp, 0 },
  5946. #endif
  5947. { "sqlite3_test_control", test_test_control },
  5948. #if SQLITE_OS_UNIX
  5949. { "getrusage", test_getrusage },
  5950. #endif
  5951. { "load_static_extension", tclLoadStaticExtensionCmd },
  5952. };
  5953. static int bitmask_size = sizeof(Bitmask)*8;
  5954. int i;
  5955. extern int sqlite3_sync_count, sqlite3_fullsync_count;
  5956. extern int sqlite3_opentemp_count;
  5957. extern int sqlite3_like_count;
  5958. extern int sqlite3_xferopt_count;
  5959. extern int sqlite3_pager_readdb_count;
  5960. extern int sqlite3_pager_writedb_count;
  5961. extern int sqlite3_pager_writej_count;
  5962. #if SQLITE_OS_WIN
  5963. extern int sqlite3_os_type;
  5964. #endif
  5965. #ifdef SQLITE_DEBUG
  5966. extern int sqlite3WhereTrace;
  5967. extern int sqlite3OSTrace;
  5968. extern int sqlite3WalTrace;
  5969. #endif
  5970. #ifdef SQLITE_TEST
  5971. #ifdef SQLITE_ENABLE_FTS3
  5972. extern int sqlite3_fts3_enable_parentheses;
  5973. #endif
  5974. #endif
  5975. for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
  5976. Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
  5977. }
  5978. for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
  5979. Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
  5980. aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
  5981. }
  5982. Tcl_LinkVar(interp, "sqlite_search_count",
  5983. (char*)&sqlite3_search_count, TCL_LINK_INT);
  5984. Tcl_LinkVar(interp, "sqlite_found_count",
  5985. (char*)&sqlite3_found_count, TCL_LINK_INT);
  5986. Tcl_LinkVar(interp, "sqlite_sort_count",
  5987. (char*)&sqlite3_sort_count, TCL_LINK_INT);
  5988. Tcl_LinkVar(interp, "sqlite3_max_blobsize",
  5989. (char*)&sqlite3_max_blobsize, TCL_LINK_INT);
  5990. Tcl_LinkVar(interp, "sqlite_like_count",
  5991. (char*)&sqlite3_like_count, TCL_LINK_INT);
  5992. Tcl_LinkVar(interp, "sqlite_interrupt_count",
  5993. (char*)&sqlite3_interrupt_count, TCL_LINK_INT);
  5994. Tcl_LinkVar(interp, "sqlite_open_file_count",
  5995. (char*)&sqlite3_open_file_count, TCL_LINK_INT);
  5996. Tcl_LinkVar(interp, "sqlite_current_time",
  5997. (char*)&sqlite3_current_time, TCL_LINK_INT);
  5998. #if SQLITE_OS_UNIX && defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
  5999. Tcl_LinkVar(interp, "sqlite_hostid_num",
  6000. (char*)&sqlite3_hostid_num, TCL_LINK_INT);
  6001. #endif
  6002. Tcl_LinkVar(interp, "sqlite3_xferopt_count",
  6003. (char*)&sqlite3_xferopt_count, TCL_LINK_INT);
  6004. Tcl_LinkVar(interp, "sqlite3_pager_readdb_count",
  6005. (char*)&sqlite3_pager_readdb_count, TCL_LINK_INT);
  6006. Tcl_LinkVar(interp, "sqlite3_pager_writedb_count",
  6007. (char*)&sqlite3_pager_writedb_count, TCL_LINK_INT);
  6008. Tcl_LinkVar(interp, "sqlite3_pager_writej_count",
  6009. (char*)&sqlite3_pager_writej_count, TCL_LINK_INT);
  6010. #ifndef SQLITE_OMIT_UTF16
  6011. Tcl_LinkVar(interp, "unaligned_string_counter",
  6012. (char*)&unaligned_string_counter, TCL_LINK_INT);
  6013. #endif
  6014. #ifndef SQLITE_OMIT_UTF16
  6015. Tcl_LinkVar(interp, "sqlite_last_needed_collation",
  6016. (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
  6017. #endif
  6018. #if SQLITE_OS_WIN
  6019. Tcl_LinkVar(interp, "sqlite_os_type",
  6020. (char*)&sqlite3_os_type, TCL_LINK_INT);
  6021. #endif
  6022. #ifdef SQLITE_TEST
  6023. {
  6024. static const char *query_plan = "*** OBSOLETE VARIABLE ***";
  6025. Tcl_LinkVar(interp, "sqlite_query_plan",
  6026. (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
  6027. }
  6028. #endif
  6029. #ifdef SQLITE_DEBUG
  6030. Tcl_LinkVar(interp, "sqlite_where_trace",
  6031. (char*)&sqlite3WhereTrace, TCL_LINK_INT);
  6032. Tcl_LinkVar(interp, "sqlite_os_trace",
  6033. (char*)&sqlite3OSTrace, TCL_LINK_INT);
  6034. #ifndef SQLITE_OMIT_WAL
  6035. Tcl_LinkVar(interp, "sqlite_wal_trace",
  6036. (char*)&sqlite3WalTrace, TCL_LINK_INT);
  6037. #endif
  6038. #endif
  6039. #ifndef SQLITE_OMIT_DISKIO
  6040. Tcl_LinkVar(interp, "sqlite_opentemp_count",
  6041. (char*)&sqlite3_opentemp_count, TCL_LINK_INT);
  6042. #endif
  6043. Tcl_LinkVar(interp, "sqlite_static_bind_value",
  6044. (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
  6045. Tcl_LinkVar(interp, "sqlite_static_bind_nbyte",
  6046. (char*)&sqlite_static_bind_nbyte, TCL_LINK_INT);
  6047. Tcl_LinkVar(interp, "sqlite_temp_directory",
  6048. (char*)&sqlite3_temp_directory, TCL_LINK_STRING);
  6049. Tcl_LinkVar(interp, "sqlite_data_directory",
  6050. (char*)&sqlite3_data_directory, TCL_LINK_STRING);
  6051. Tcl_LinkVar(interp, "bitmask_size",
  6052. (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY);
  6053. Tcl_LinkVar(interp, "sqlite_sync_count",
  6054. (char*)&sqlite3_sync_count, TCL_LINK_INT);
  6055. Tcl_LinkVar(interp, "sqlite_fullsync_count",
  6056. (char*)&sqlite3_fullsync_count, TCL_LINK_INT);
  6057. #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST)
  6058. Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses",
  6059. (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT);
  6060. #endif
  6061. return TCL_OK;
  6062. }