vdbe.c 200 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273
  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. ** The code in this file implements execution method of the
  13. ** Virtual Database Engine (VDBE). A separate file ("vdbeaux.c")
  14. ** handles housekeeping details such as creating and deleting
  15. ** VDBE instances. This file is solely interested in executing
  16. ** the VDBE program.
  17. **
  18. ** In the external interface, an "sqlite3_stmt*" is an opaque pointer
  19. ** to a VDBE.
  20. **
  21. ** The SQL parser generates a program which is then executed by
  22. ** the VDBE to do the work of the SQL statement. VDBE programs are
  23. ** similar in form to assembly language. The program consists of
  24. ** a linear sequence of operations. Each operation has an opcode
  25. ** and 5 operands. Operands P1, P2, and P3 are integers. Operand P4
  26. ** is a null-terminated string. Operand P5 is an unsigned character.
  27. ** Few opcodes use all 5 operands.
  28. **
  29. ** Computation results are stored on a set of registers numbered beginning
  30. ** with 1 and going up to Vdbe.nMem. Each register can store
  31. ** either an integer, a null-terminated string, a floating point
  32. ** number, or the SQL "NULL" value. An implicit conversion from one
  33. ** type to the other occurs as necessary.
  34. **
  35. ** Most of the code in this file is taken up by the sqlite3VdbeExec()
  36. ** function which does the work of interpreting a VDBE program.
  37. ** But other routines are also provided to help in building up
  38. ** a program instruction by instruction.
  39. **
  40. ** Various scripts scan this source file in order to generate HTML
  41. ** documentation, headers files, or other derived files. The formatting
  42. ** of the code in this file is, therefore, important. See other comments
  43. ** in this file for details. If in doubt, do not deviate from existing
  44. ** commenting and indentation practices when changing or adding code.
  45. */
  46. #include "sqliteInt.h"
  47. #include "vdbeInt.h"
  48. /*
  49. ** Invoke this macro on memory cells just prior to changing the
  50. ** value of the cell. This macro verifies that shallow copies are
  51. ** not misused.
  52. */
  53. #ifdef SQLITE_DEBUG
  54. # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
  55. #else
  56. # define memAboutToChange(P,M)
  57. #endif
  58. /*
  59. ** The following global variable is incremented every time a cursor
  60. ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
  61. ** procedures use this information to make sure that indices are
  62. ** working correctly. This variable has no function other than to
  63. ** help verify the correct operation of the library.
  64. */
  65. #ifdef SQLITE_TEST
  66. int sqlite3_search_count = 0;
  67. #endif
  68. /*
  69. ** When this global variable is positive, it gets decremented once before
  70. ** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
  71. ** field of the sqlite3 structure is set in order to simulate an interrupt.
  72. **
  73. ** This facility is used for testing purposes only. It does not function
  74. ** in an ordinary build.
  75. */
  76. #ifdef SQLITE_TEST
  77. int sqlite3_interrupt_count = 0;
  78. #endif
  79. /*
  80. ** The next global variable is incremented each type the OP_Sort opcode
  81. ** is executed. The test procedures use this information to make sure that
  82. ** sorting is occurring or not occurring at appropriate times. This variable
  83. ** has no function other than to help verify the correct operation of the
  84. ** library.
  85. */
  86. #ifdef SQLITE_TEST
  87. int sqlite3_sort_count = 0;
  88. #endif
  89. /*
  90. ** The next global variable records the size of the largest MEM_Blob
  91. ** or MEM_Str that has been used by a VDBE opcode. The test procedures
  92. ** use this information to make sure that the zero-blob functionality
  93. ** is working correctly. This variable has no function other than to
  94. ** help verify the correct operation of the library.
  95. */
  96. #ifdef SQLITE_TEST
  97. int sqlite3_max_blobsize = 0;
  98. static void updateMaxBlobsize(Mem *p){
  99. if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
  100. sqlite3_max_blobsize = p->n;
  101. }
  102. }
  103. #endif
  104. /*
  105. ** The next global variable is incremented each type the OP_Found opcode
  106. ** is executed. This is used to test whether or not the foreign key
  107. ** operation implemented using OP_FkIsZero is working. This variable
  108. ** has no function other than to help verify the correct operation of the
  109. ** library.
  110. */
  111. #ifdef SQLITE_TEST
  112. int sqlite3_found_count = 0;
  113. #endif
  114. /*
  115. ** Test a register to see if it exceeds the current maximum blob size.
  116. ** If it does, record the new maximum blob size.
  117. */
  118. #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
  119. # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
  120. #else
  121. # define UPDATE_MAX_BLOBSIZE(P)
  122. #endif
  123. /*
  124. ** Convert the given register into a string if it isn't one
  125. ** already. Return non-zero if a malloc() fails.
  126. */
  127. #define Stringify(P, enc) \
  128. if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
  129. { goto no_mem; }
  130. /*
  131. ** An ephemeral string value (signified by the MEM_Ephem flag) contains
  132. ** a pointer to a dynamically allocated string where some other entity
  133. ** is responsible for deallocating that string. Because the register
  134. ** does not control the string, it might be deleted without the register
  135. ** knowing it.
  136. **
  137. ** This routine converts an ephemeral string into a dynamically allocated
  138. ** string that the register itself controls. In other words, it
  139. ** converts an MEM_Ephem string into an MEM_Dyn string.
  140. */
  141. #define Deephemeralize(P) \
  142. if( ((P)->flags&MEM_Ephem)!=0 \
  143. && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
  144. /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
  145. # define isSorter(x) ((x)->pSorter!=0)
  146. /*
  147. ** Argument pMem points at a register that will be passed to a
  148. ** user-defined function or returned to the user as the result of a query.
  149. ** This routine sets the pMem->type variable used by the sqlite3_value_*()
  150. ** routines.
  151. */
  152. void sqlite3VdbeMemStoreType(Mem *pMem){
  153. int flags = pMem->flags;
  154. if( flags & MEM_Null ){
  155. pMem->type = SQLITE_NULL;
  156. }
  157. else if( flags & MEM_Int ){
  158. pMem->type = SQLITE_INTEGER;
  159. }
  160. else if( flags & MEM_Real ){
  161. pMem->type = SQLITE_FLOAT;
  162. }
  163. else if( flags & MEM_Str ){
  164. pMem->type = SQLITE_TEXT;
  165. }else{
  166. pMem->type = SQLITE_BLOB;
  167. }
  168. }
  169. /*
  170. ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
  171. ** if we run out of memory.
  172. */
  173. static VdbeCursor *allocateCursor(
  174. Vdbe *p, /* The virtual machine */
  175. int iCur, /* Index of the new VdbeCursor */
  176. int nField, /* Number of fields in the table or index */
  177. int iDb, /* Database the cursor belongs to, or -1 */
  178. int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */
  179. ){
  180. /* Find the memory cell that will be used to store the blob of memory
  181. ** required for this VdbeCursor structure. It is convenient to use a
  182. ** vdbe memory cell to manage the memory allocation required for a
  183. ** VdbeCursor structure for the following reasons:
  184. **
  185. ** * Sometimes cursor numbers are used for a couple of different
  186. ** purposes in a vdbe program. The different uses might require
  187. ** different sized allocations. Memory cells provide growable
  188. ** allocations.
  189. **
  190. ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
  191. ** be freed lazily via the sqlite3_release_memory() API. This
  192. ** minimizes the number of malloc calls made by the system.
  193. **
  194. ** Memory cells for cursors are allocated at the top of the address
  195. ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
  196. ** cursor 1 is managed by memory cell (p->nMem-1), etc.
  197. */
  198. Mem *pMem = &p->aMem[p->nMem-iCur];
  199. int nByte;
  200. VdbeCursor *pCx = 0;
  201. nByte =
  202. ROUND8(sizeof(VdbeCursor)) +
  203. (isBtreeCursor?sqlite3BtreeCursorSize():0) +
  204. 2*nField*sizeof(u32);
  205. assert( iCur<p->nCursor );
  206. if( p->apCsr[iCur] ){
  207. sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
  208. p->apCsr[iCur] = 0;
  209. }
  210. if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
  211. p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
  212. memset(pCx, 0, sizeof(VdbeCursor));
  213. pCx->iDb = iDb;
  214. pCx->nField = nField;
  215. if( nField ){
  216. pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
  217. }
  218. if( isBtreeCursor ){
  219. pCx->pCursor = (BtCursor*)
  220. &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
  221. sqlite3BtreeCursorZero(pCx->pCursor);
  222. }
  223. }
  224. return pCx;
  225. }
  226. /*
  227. ** Try to convert a value into a numeric representation if we can
  228. ** do so without loss of information. In other words, if the string
  229. ** looks like a number, convert it into a number. If it does not
  230. ** look like a number, leave it alone.
  231. */
  232. static void applyNumericAffinity(Mem *pRec){
  233. if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
  234. double rValue;
  235. i64 iValue;
  236. u8 enc = pRec->enc;
  237. if( (pRec->flags&MEM_Str)==0 ) return;
  238. if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
  239. if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
  240. pRec->u.i = iValue;
  241. pRec->flags |= MEM_Int;
  242. }else{
  243. pRec->r = rValue;
  244. pRec->flags |= MEM_Real;
  245. }
  246. }
  247. }
  248. /*
  249. ** Processing is determine by the affinity parameter:
  250. **
  251. ** SQLITE_AFF_INTEGER:
  252. ** SQLITE_AFF_REAL:
  253. ** SQLITE_AFF_NUMERIC:
  254. ** Try to convert pRec to an integer representation or a
  255. ** floating-point representation if an integer representation
  256. ** is not possible. Note that the integer representation is
  257. ** always preferred, even if the affinity is REAL, because
  258. ** an integer representation is more space efficient on disk.
  259. **
  260. ** SQLITE_AFF_TEXT:
  261. ** Convert pRec to a text representation.
  262. **
  263. ** SQLITE_AFF_NONE:
  264. ** No-op. pRec is unchanged.
  265. */
  266. static void applyAffinity(
  267. Mem *pRec, /* The value to apply affinity to */
  268. char affinity, /* The affinity to be applied */
  269. u8 enc /* Use this text encoding */
  270. ){
  271. if( affinity==SQLITE_AFF_TEXT ){
  272. /* Only attempt the conversion to TEXT if there is an integer or real
  273. ** representation (blob and NULL do not get converted) but no string
  274. ** representation.
  275. */
  276. if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
  277. sqlite3VdbeMemStringify(pRec, enc);
  278. }
  279. pRec->flags &= ~(MEM_Real|MEM_Int);
  280. }else if( affinity!=SQLITE_AFF_NONE ){
  281. assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
  282. || affinity==SQLITE_AFF_NUMERIC );
  283. applyNumericAffinity(pRec);
  284. if( pRec->flags & MEM_Real ){
  285. sqlite3VdbeIntegerAffinity(pRec);
  286. }
  287. }
  288. }
  289. /*
  290. ** Try to convert the type of a function argument or a result column
  291. ** into a numeric representation. Use either INTEGER or REAL whichever
  292. ** is appropriate. But only do the conversion if it is possible without
  293. ** loss of information and return the revised type of the argument.
  294. */
  295. int sqlite3_value_numeric_type(sqlite3_value *pVal){
  296. Mem *pMem = (Mem*)pVal;
  297. if( pMem->type==SQLITE_TEXT ){
  298. applyNumericAffinity(pMem);
  299. sqlite3VdbeMemStoreType(pMem);
  300. }
  301. return pMem->type;
  302. }
  303. /*
  304. ** Exported version of applyAffinity(). This one works on sqlite3_value*,
  305. ** not the internal Mem* type.
  306. */
  307. void sqlite3ValueApplyAffinity(
  308. sqlite3_value *pVal,
  309. u8 affinity,
  310. u8 enc
  311. ){
  312. applyAffinity((Mem *)pVal, affinity, enc);
  313. }
  314. #ifdef SQLITE_DEBUG
  315. /*
  316. ** Write a nice string representation of the contents of cell pMem
  317. ** into buffer zBuf, length nBuf.
  318. */
  319. void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
  320. char *zCsr = zBuf;
  321. int f = pMem->flags;
  322. static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
  323. if( f&MEM_Blob ){
  324. int i;
  325. char c;
  326. if( f & MEM_Dyn ){
  327. c = 'z';
  328. assert( (f & (MEM_Static|MEM_Ephem))==0 );
  329. }else if( f & MEM_Static ){
  330. c = 't';
  331. assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
  332. }else if( f & MEM_Ephem ){
  333. c = 'e';
  334. assert( (f & (MEM_Static|MEM_Dyn))==0 );
  335. }else{
  336. c = 's';
  337. }
  338. sqlite3_snprintf(100, zCsr, "%c", c);
  339. zCsr += sqlite3Strlen30(zCsr);
  340. sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
  341. zCsr += sqlite3Strlen30(zCsr);
  342. for(i=0; i<16 && i<pMem->n; i++){
  343. sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
  344. zCsr += sqlite3Strlen30(zCsr);
  345. }
  346. for(i=0; i<16 && i<pMem->n; i++){
  347. char z = pMem->z[i];
  348. if( z<32 || z>126 ) *zCsr++ = '.';
  349. else *zCsr++ = z;
  350. }
  351. sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
  352. zCsr += sqlite3Strlen30(zCsr);
  353. if( f & MEM_Zero ){
  354. sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
  355. zCsr += sqlite3Strlen30(zCsr);
  356. }
  357. *zCsr = '\0';
  358. }else if( f & MEM_Str ){
  359. int j, k;
  360. zBuf[0] = ' ';
  361. if( f & MEM_Dyn ){
  362. zBuf[1] = 'z';
  363. assert( (f & (MEM_Static|MEM_Ephem))==0 );
  364. }else if( f & MEM_Static ){
  365. zBuf[1] = 't';
  366. assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
  367. }else if( f & MEM_Ephem ){
  368. zBuf[1] = 'e';
  369. assert( (f & (MEM_Static|MEM_Dyn))==0 );
  370. }else{
  371. zBuf[1] = 's';
  372. }
  373. k = 2;
  374. sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
  375. k += sqlite3Strlen30(&zBuf[k]);
  376. zBuf[k++] = '[';
  377. for(j=0; j<15 && j<pMem->n; j++){
  378. u8 c = pMem->z[j];
  379. if( c>=0x20 && c<0x7f ){
  380. zBuf[k++] = c;
  381. }else{
  382. zBuf[k++] = '.';
  383. }
  384. }
  385. zBuf[k++] = ']';
  386. sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
  387. k += sqlite3Strlen30(&zBuf[k]);
  388. zBuf[k++] = 0;
  389. }
  390. }
  391. #endif
  392. #ifdef SQLITE_DEBUG
  393. /*
  394. ** Print the value of a register for tracing purposes:
  395. */
  396. static void memTracePrint(FILE *out, Mem *p){
  397. if( p->flags & MEM_Invalid ){
  398. fprintf(out, " undefined");
  399. }else if( p->flags & MEM_Null ){
  400. fprintf(out, " NULL");
  401. }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
  402. fprintf(out, " si:%lld", p->u.i);
  403. }else if( p->flags & MEM_Int ){
  404. fprintf(out, " i:%lld", p->u.i);
  405. #ifndef SQLITE_OMIT_FLOATING_POINT
  406. }else if( p->flags & MEM_Real ){
  407. fprintf(out, " r:%g", p->r);
  408. #endif
  409. }else if( p->flags & MEM_RowSet ){
  410. fprintf(out, " (rowset)");
  411. }else{
  412. char zBuf[200];
  413. sqlite3VdbeMemPrettyPrint(p, zBuf);
  414. fprintf(out, " ");
  415. fprintf(out, "%s", zBuf);
  416. }
  417. }
  418. static void registerTrace(FILE *out, int iReg, Mem *p){
  419. fprintf(out, "REG[%d] = ", iReg);
  420. memTracePrint(out, p);
  421. fprintf(out, "\n");
  422. }
  423. #endif
  424. #ifdef SQLITE_DEBUG
  425. # define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
  426. #else
  427. # define REGISTER_TRACE(R,M)
  428. #endif
  429. #ifdef VDBE_PROFILE
  430. /*
  431. ** hwtime.h contains inline assembler code for implementing
  432. ** high-performance timing routines.
  433. */
  434. #include "hwtime.h"
  435. #endif
  436. /*
  437. ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
  438. ** sqlite3_interrupt() routine has been called. If it has been, then
  439. ** processing of the VDBE program is interrupted.
  440. **
  441. ** This macro added to every instruction that does a jump in order to
  442. ** implement a loop. This test used to be on every single instruction,
  443. ** but that meant we more testing than we needed. By only testing the
  444. ** flag on jump instructions, we get a (small) speed improvement.
  445. */
  446. #define CHECK_FOR_INTERRUPT \
  447. if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
  448. #ifndef NDEBUG
  449. /*
  450. ** This function is only called from within an assert() expression. It
  451. ** checks that the sqlite3.nTransaction variable is correctly set to
  452. ** the number of non-transaction savepoints currently in the
  453. ** linked list starting at sqlite3.pSavepoint.
  454. **
  455. ** Usage:
  456. **
  457. ** assert( checkSavepointCount(db) );
  458. */
  459. static int checkSavepointCount(sqlite3 *db){
  460. int n = 0;
  461. Savepoint *p;
  462. for(p=db->pSavepoint; p; p=p->pNext) n++;
  463. assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
  464. return 1;
  465. }
  466. #endif
  467. /*
  468. ** Execute as much of a VDBE program as we can then return.
  469. **
  470. ** sqlite3VdbeMakeReady() must be called before this routine in order to
  471. ** close the program with a final OP_Halt and to set up the callbacks
  472. ** and the error message pointer.
  473. **
  474. ** Whenever a row or result data is available, this routine will either
  475. ** invoke the result callback (if there is one) or return with
  476. ** SQLITE_ROW.
  477. **
  478. ** If an attempt is made to open a locked database, then this routine
  479. ** will either invoke the busy callback (if there is one) or it will
  480. ** return SQLITE_BUSY.
  481. **
  482. ** If an error occurs, an error message is written to memory obtained
  483. ** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
  484. ** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
  485. **
  486. ** If the callback ever returns non-zero, then the program exits
  487. ** immediately. There will be no error message but the p->rc field is
  488. ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
  489. **
  490. ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
  491. ** routine to return SQLITE_ERROR.
  492. **
  493. ** Other fatal errors return SQLITE_ERROR.
  494. **
  495. ** After this routine has finished, sqlite3VdbeFinalize() should be
  496. ** used to clean up the mess that was left behind.
  497. */
  498. int sqlite3VdbeExec(
  499. Vdbe *p /* The VDBE */
  500. ){
  501. int pc=0; /* The program counter */
  502. Op *aOp = p->aOp; /* Copy of p->aOp */
  503. Op *pOp; /* Current operation */
  504. int rc = SQLITE_OK; /* Value to return */
  505. sqlite3 *db = p->db; /* The database */
  506. u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
  507. u8 encoding = ENC(db); /* The database encoding */
  508. int iCompare = 0; /* Result of last OP_Compare operation */
  509. unsigned nVmStep = 0; /* Number of virtual machine steps */
  510. #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  511. unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
  512. #endif
  513. Mem *aMem = p->aMem; /* Copy of p->aMem */
  514. Mem *pIn1 = 0; /* 1st input operand */
  515. Mem *pIn2 = 0; /* 2nd input operand */
  516. Mem *pIn3 = 0; /* 3rd input operand */
  517. Mem *pOut = 0; /* Output operand */
  518. int *aPermute = 0; /* Permutation of columns for OP_Compare */
  519. i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
  520. #ifdef VDBE_PROFILE
  521. u64 start; /* CPU clock count at start of opcode */
  522. int origPc; /* Program counter at start of opcode */
  523. #endif
  524. /*** INSERT STACK UNION HERE ***/
  525. assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
  526. sqlite3VdbeEnter(p);
  527. if( p->rc==SQLITE_NOMEM ){
  528. /* This happens if a malloc() inside a call to sqlite3_column_text() or
  529. ** sqlite3_column_text16() failed. */
  530. goto no_mem;
  531. }
  532. assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
  533. assert( p->bIsReader || p->readOnly!=0 );
  534. p->rc = SQLITE_OK;
  535. p->iCurrentTime = 0;
  536. assert( p->explain==0 );
  537. p->pResultSet = 0;
  538. db->busyHandler.nBusy = 0;
  539. CHECK_FOR_INTERRUPT;
  540. sqlite3VdbeIOTraceSql(p);
  541. #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  542. if( db->xProgress ){
  543. assert( 0 < db->nProgressOps );
  544. nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
  545. if( nProgressLimit==0 ){
  546. nProgressLimit = db->nProgressOps;
  547. }else{
  548. nProgressLimit %= (unsigned)db->nProgressOps;
  549. }
  550. }
  551. #endif
  552. #ifdef SQLITE_DEBUG
  553. sqlite3BeginBenignMalloc();
  554. if( p->pc==0 && (p->db->flags & SQLITE_VdbeListing)!=0 ){
  555. int i;
  556. printf("VDBE Program Listing:\n");
  557. sqlite3VdbePrintSql(p);
  558. for(i=0; i<p->nOp; i++){
  559. sqlite3VdbePrintOp(stdout, i, &aOp[i]);
  560. }
  561. }
  562. sqlite3EndBenignMalloc();
  563. #endif
  564. for(pc=p->pc; rc==SQLITE_OK; pc++){
  565. assert( pc>=0 && pc<p->nOp );
  566. if( db->mallocFailed ) goto no_mem;
  567. #ifdef VDBE_PROFILE
  568. origPc = pc;
  569. start = sqlite3Hwtime();
  570. #endif
  571. nVmStep++;
  572. pOp = &aOp[pc];
  573. /* Only allow tracing if SQLITE_DEBUG is defined.
  574. */
  575. #ifdef SQLITE_DEBUG
  576. if( p->trace ){
  577. if( pc==0 ){
  578. printf("VDBE Execution Trace:\n");
  579. sqlite3VdbePrintSql(p);
  580. }
  581. sqlite3VdbePrintOp(p->trace, pc, pOp);
  582. }
  583. #endif
  584. /* Check to see if we need to simulate an interrupt. This only happens
  585. ** if we have a special test build.
  586. */
  587. #ifdef SQLITE_TEST
  588. if( sqlite3_interrupt_count>0 ){
  589. sqlite3_interrupt_count--;
  590. if( sqlite3_interrupt_count==0 ){
  591. sqlite3_interrupt(db);
  592. }
  593. }
  594. #endif
  595. /* On any opcode with the "out2-prerelease" tag, free any
  596. ** external allocations out of mem[p2] and set mem[p2] to be
  597. ** an undefined integer. Opcodes will either fill in the integer
  598. ** value or convert mem[p2] to a different type.
  599. */
  600. assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
  601. if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
  602. assert( pOp->p2>0 );
  603. assert( pOp->p2<=(p->nMem-p->nCursor) );
  604. pOut = &aMem[pOp->p2];
  605. memAboutToChange(p, pOut);
  606. VdbeMemRelease(pOut);
  607. pOut->flags = MEM_Int;
  608. }
  609. /* Sanity checking on other operands */
  610. #ifdef SQLITE_DEBUG
  611. if( (pOp->opflags & OPFLG_IN1)!=0 ){
  612. assert( pOp->p1>0 );
  613. assert( pOp->p1<=(p->nMem-p->nCursor) );
  614. assert( memIsValid(&aMem[pOp->p1]) );
  615. REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
  616. }
  617. if( (pOp->opflags & OPFLG_IN2)!=0 ){
  618. assert( pOp->p2>0 );
  619. assert( pOp->p2<=(p->nMem-p->nCursor) );
  620. assert( memIsValid(&aMem[pOp->p2]) );
  621. REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
  622. }
  623. if( (pOp->opflags & OPFLG_IN3)!=0 ){
  624. assert( pOp->p3>0 );
  625. assert( pOp->p3<=(p->nMem-p->nCursor) );
  626. assert( memIsValid(&aMem[pOp->p3]) );
  627. REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
  628. }
  629. if( (pOp->opflags & OPFLG_OUT2)!=0 ){
  630. assert( pOp->p2>0 );
  631. assert( pOp->p2<=(p->nMem-p->nCursor) );
  632. memAboutToChange(p, &aMem[pOp->p2]);
  633. }
  634. if( (pOp->opflags & OPFLG_OUT3)!=0 ){
  635. assert( pOp->p3>0 );
  636. assert( pOp->p3<=(p->nMem-p->nCursor) );
  637. memAboutToChange(p, &aMem[pOp->p3]);
  638. }
  639. #endif
  640. switch( pOp->opcode ){
  641. /*****************************************************************************
  642. ** What follows is a massive switch statement where each case implements a
  643. ** separate instruction in the virtual machine. If we follow the usual
  644. ** indentation conventions, each case should be indented by 6 spaces. But
  645. ** that is a lot of wasted space on the left margin. So the code within
  646. ** the switch statement will break with convention and be flush-left. Another
  647. ** big comment (similar to this one) will mark the point in the code where
  648. ** we transition back to normal indentation.
  649. **
  650. ** The formatting of each case is important. The makefile for SQLite
  651. ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
  652. ** file looking for lines that begin with "case OP_". The opcodes.h files
  653. ** will be filled with #defines that give unique integer values to each
  654. ** opcode and the opcodes.c file is filled with an array of strings where
  655. ** each string is the symbolic name for the corresponding opcode. If the
  656. ** case statement is followed by a comment of the form "/# same as ... #/"
  657. ** that comment is used to determine the particular value of the opcode.
  658. **
  659. ** Other keywords in the comment that follows each case are used to
  660. ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
  661. ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
  662. ** the mkopcodeh.awk script for additional information.
  663. **
  664. ** Documentation about VDBE opcodes is generated by scanning this file
  665. ** for lines of that contain "Opcode:". That line and all subsequent
  666. ** comment lines are used in the generation of the opcode.html documentation
  667. ** file.
  668. **
  669. ** SUMMARY:
  670. **
  671. ** Formatting is important to scripts that scan this file.
  672. ** Do not deviate from the formatting style currently in use.
  673. **
  674. *****************************************************************************/
  675. /* Opcode: Goto * P2 * * *
  676. **
  677. ** An unconditional jump to address P2.
  678. ** The next instruction executed will be
  679. ** the one at index P2 from the beginning of
  680. ** the program.
  681. */
  682. case OP_Goto: { /* jump */
  683. pc = pOp->p2 - 1;
  684. /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
  685. ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
  686. ** completion. Check to see if sqlite3_interrupt() has been called
  687. ** or if the progress callback needs to be invoked.
  688. **
  689. ** This code uses unstructured "goto" statements and does not look clean.
  690. ** But that is not due to sloppy coding habits. The code is written this
  691. ** way for performance, to avoid having to run the interrupt and progress
  692. ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
  693. ** faster according to "valgrind --tool=cachegrind" */
  694. check_for_interrupt:
  695. CHECK_FOR_INTERRUPT;
  696. #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  697. /* Call the progress callback if it is configured and the required number
  698. ** of VDBE ops have been executed (either since this invocation of
  699. ** sqlite3VdbeExec() or since last time the progress callback was called).
  700. ** If the progress callback returns non-zero, exit the virtual machine with
  701. ** a return code SQLITE_ABORT.
  702. */
  703. if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
  704. int prc;
  705. prc = db->xProgress(db->pProgressArg);
  706. if( prc!=0 ){
  707. rc = SQLITE_INTERRUPT;
  708. goto vdbe_error_halt;
  709. }
  710. if( db->xProgress!=0 ){
  711. nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
  712. }
  713. }
  714. #endif
  715. break;
  716. }
  717. /* Opcode: Gosub P1 P2 * * *
  718. **
  719. ** Write the current address onto register P1
  720. ** and then jump to address P2.
  721. */
  722. case OP_Gosub: { /* jump */
  723. assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
  724. pIn1 = &aMem[pOp->p1];
  725. assert( (pIn1->flags & MEM_Dyn)==0 );
  726. memAboutToChange(p, pIn1);
  727. pIn1->flags = MEM_Int;
  728. pIn1->u.i = pc;
  729. REGISTER_TRACE(pOp->p1, pIn1);
  730. pc = pOp->p2 - 1;
  731. break;
  732. }
  733. /* Opcode: Return P1 * * * *
  734. **
  735. ** Jump to the next instruction after the address in register P1.
  736. */
  737. case OP_Return: { /* in1 */
  738. pIn1 = &aMem[pOp->p1];
  739. assert( pIn1->flags & MEM_Int );
  740. pc = (int)pIn1->u.i;
  741. break;
  742. }
  743. /* Opcode: Yield P1 * * * *
  744. **
  745. ** Swap the program counter with the value in register P1.
  746. */
  747. case OP_Yield: { /* in1 */
  748. int pcDest;
  749. pIn1 = &aMem[pOp->p1];
  750. assert( (pIn1->flags & MEM_Dyn)==0 );
  751. pIn1->flags = MEM_Int;
  752. pcDest = (int)pIn1->u.i;
  753. pIn1->u.i = pc;
  754. REGISTER_TRACE(pOp->p1, pIn1);
  755. pc = pcDest;
  756. break;
  757. }
  758. /* Opcode: HaltIfNull P1 P2 P3 P4 *
  759. **
  760. ** Check the value in register P3. If it is NULL then Halt using
  761. ** parameter P1, P2, and P4 as if this were a Halt instruction. If the
  762. ** value in register P3 is not NULL, then this routine is a no-op.
  763. */
  764. case OP_HaltIfNull: { /* in3 */
  765. pIn3 = &aMem[pOp->p3];
  766. if( (pIn3->flags & MEM_Null)==0 ) break;
  767. /* Fall through into OP_Halt */
  768. }
  769. /* Opcode: Halt P1 P2 * P4 *
  770. **
  771. ** Exit immediately. All open cursors, etc are closed
  772. ** automatically.
  773. **
  774. ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
  775. ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
  776. ** For errors, it can be some other value. If P1!=0 then P2 will determine
  777. ** whether or not to rollback the current transaction. Do not rollback
  778. ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
  779. ** then back out all changes that have occurred during this execution of the
  780. ** VDBE, but do not rollback the transaction.
  781. **
  782. ** If P4 is not null then it is an error message string.
  783. **
  784. ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
  785. ** every program. So a jump past the last instruction of the program
  786. ** is the same as executing Halt.
  787. */
  788. case OP_Halt: {
  789. if( pOp->p1==SQLITE_OK && p->pFrame ){
  790. /* Halt the sub-program. Return control to the parent frame. */
  791. VdbeFrame *pFrame = p->pFrame;
  792. p->pFrame = pFrame->pParent;
  793. p->nFrame--;
  794. sqlite3VdbeSetChanges(db, p->nChange);
  795. pc = sqlite3VdbeFrameRestore(pFrame);
  796. lastRowid = db->lastRowid;
  797. if( pOp->p2==OE_Ignore ){
  798. /* Instruction pc is the OP_Program that invoked the sub-program
  799. ** currently being halted. If the p2 instruction of this OP_Halt
  800. ** instruction is set to OE_Ignore, then the sub-program is throwing
  801. ** an IGNORE exception. In this case jump to the address specified
  802. ** as the p2 of the calling OP_Program. */
  803. pc = p->aOp[pc].p2-1;
  804. }
  805. aOp = p->aOp;
  806. aMem = p->aMem;
  807. break;
  808. }
  809. p->rc = pOp->p1;
  810. p->errorAction = (u8)pOp->p2;
  811. p->pc = pc;
  812. if( pOp->p4.z ){
  813. assert( p->rc!=SQLITE_OK );
  814. sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
  815. testcase( sqlite3GlobalConfig.xLog!=0 );
  816. sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
  817. }else if( p->rc ){
  818. testcase( sqlite3GlobalConfig.xLog!=0 );
  819. sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
  820. }
  821. rc = sqlite3VdbeHalt(p);
  822. assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
  823. if( rc==SQLITE_BUSY ){
  824. p->rc = rc = SQLITE_BUSY;
  825. }else{
  826. assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
  827. assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
  828. rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
  829. }
  830. goto vdbe_return;
  831. }
  832. /* Opcode: Integer P1 P2 * * *
  833. **
  834. ** The 32-bit integer value P1 is written into register P2.
  835. */
  836. case OP_Integer: { /* out2-prerelease */
  837. pOut->u.i = pOp->p1;
  838. break;
  839. }
  840. /* Opcode: Int64 * P2 * P4 *
  841. **
  842. ** P4 is a pointer to a 64-bit integer value.
  843. ** Write that value into register P2.
  844. */
  845. case OP_Int64: { /* out2-prerelease */
  846. assert( pOp->p4.pI64!=0 );
  847. pOut->u.i = *pOp->p4.pI64;
  848. break;
  849. }
  850. #ifndef SQLITE_OMIT_FLOATING_POINT
  851. /* Opcode: Real * P2 * P4 *
  852. **
  853. ** P4 is a pointer to a 64-bit floating point value.
  854. ** Write that value into register P2.
  855. */
  856. case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
  857. pOut->flags = MEM_Real;
  858. assert( !sqlite3IsNaN(*pOp->p4.pReal) );
  859. pOut->r = *pOp->p4.pReal;
  860. break;
  861. }
  862. #endif
  863. /* Opcode: String8 * P2 * P4 *
  864. **
  865. ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
  866. ** into an OP_String before it is executed for the first time.
  867. */
  868. case OP_String8: { /* same as TK_STRING, out2-prerelease */
  869. assert( pOp->p4.z!=0 );
  870. pOp->opcode = OP_String;
  871. pOp->p1 = sqlite3Strlen30(pOp->p4.z);
  872. #ifndef SQLITE_OMIT_UTF16
  873. if( encoding!=SQLITE_UTF8 ){
  874. rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
  875. if( rc==SQLITE_TOOBIG ) goto too_big;
  876. if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
  877. assert( pOut->zMalloc==pOut->z );
  878. assert( pOut->flags & MEM_Dyn );
  879. pOut->zMalloc = 0;
  880. pOut->flags |= MEM_Static;
  881. pOut->flags &= ~MEM_Dyn;
  882. if( pOp->p4type==P4_DYNAMIC ){
  883. sqlite3DbFree(db, pOp->p4.z);
  884. }
  885. pOp->p4type = P4_DYNAMIC;
  886. pOp->p4.z = pOut->z;
  887. pOp->p1 = pOut->n;
  888. }
  889. #endif
  890. if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  891. goto too_big;
  892. }
  893. /* Fall through to the next case, OP_String */
  894. }
  895. /* Opcode: String P1 P2 * P4 *
  896. **
  897. ** The string value P4 of length P1 (bytes) is stored in register P2.
  898. */
  899. case OP_String: { /* out2-prerelease */
  900. assert( pOp->p4.z!=0 );
  901. pOut->flags = MEM_Str|MEM_Static|MEM_Term;
  902. pOut->z = pOp->p4.z;
  903. pOut->n = pOp->p1;
  904. pOut->enc = encoding;
  905. UPDATE_MAX_BLOBSIZE(pOut);
  906. break;
  907. }
  908. /* Opcode: Null P1 P2 P3 * *
  909. **
  910. ** Write a NULL into registers P2. If P3 greater than P2, then also write
  911. ** NULL into register P3 and every register in between P2 and P3. If P3
  912. ** is less than P2 (typically P3 is zero) then only register P2 is
  913. ** set to NULL.
  914. **
  915. ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
  916. ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
  917. ** OP_Ne or OP_Eq.
  918. */
  919. case OP_Null: { /* out2-prerelease */
  920. int cnt;
  921. u16 nullFlag;
  922. cnt = pOp->p3-pOp->p2;
  923. assert( pOp->p3<=(p->nMem-p->nCursor) );
  924. pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
  925. while( cnt>0 ){
  926. pOut++;
  927. memAboutToChange(p, pOut);
  928. VdbeMemRelease(pOut);
  929. pOut->flags = nullFlag;
  930. cnt--;
  931. }
  932. break;
  933. }
  934. /* Opcode: Blob P1 P2 * P4
  935. **
  936. ** P4 points to a blob of data P1 bytes long. Store this
  937. ** blob in register P2.
  938. */
  939. case OP_Blob: { /* out2-prerelease */
  940. assert( pOp->p1 <= SQLITE_MAX_LENGTH );
  941. sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
  942. pOut->enc = encoding;
  943. UPDATE_MAX_BLOBSIZE(pOut);
  944. break;
  945. }
  946. /* Opcode: Variable P1 P2 * P4 *
  947. **
  948. ** Transfer the values of bound parameter P1 into register P2
  949. **
  950. ** If the parameter is named, then its name appears in P4 and P3==1.
  951. ** The P4 value is used by sqlite3_bind_parameter_name().
  952. */
  953. case OP_Variable: { /* out2-prerelease */
  954. Mem *pVar; /* Value being transferred */
  955. assert( pOp->p1>0 && pOp->p1<=p->nVar );
  956. assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
  957. pVar = &p->aVar[pOp->p1 - 1];
  958. if( sqlite3VdbeMemTooBig(pVar) ){
  959. goto too_big;
  960. }
  961. sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
  962. UPDATE_MAX_BLOBSIZE(pOut);
  963. break;
  964. }
  965. /* Opcode: Move P1 P2 P3 * *
  966. **
  967. ** Move the values in register P1..P1+P3 over into
  968. ** registers P2..P2+P3. Registers P1..P1+P3 are
  969. ** left holding a NULL. It is an error for register ranges
  970. ** P1..P1+P3 and P2..P2+P3 to overlap.
  971. */
  972. case OP_Move: {
  973. char *zMalloc; /* Holding variable for allocated memory */
  974. int n; /* Number of registers left to copy */
  975. int p1; /* Register to copy from */
  976. int p2; /* Register to copy to */
  977. n = pOp->p3 + 1;
  978. p1 = pOp->p1;
  979. p2 = pOp->p2;
  980. assert( n>0 && p1>0 && p2>0 );
  981. assert( p1+n<=p2 || p2+n<=p1 );
  982. pIn1 = &aMem[p1];
  983. pOut = &aMem[p2];
  984. while( n-- ){
  985. assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
  986. assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
  987. assert( memIsValid(pIn1) );
  988. memAboutToChange(p, pOut);
  989. zMalloc = pOut->zMalloc;
  990. pOut->zMalloc = 0;
  991. sqlite3VdbeMemMove(pOut, pIn1);
  992. #ifdef SQLITE_DEBUG
  993. if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
  994. pOut->pScopyFrom += p1 - pOp->p2;
  995. }
  996. #endif
  997. pIn1->zMalloc = zMalloc;
  998. REGISTER_TRACE(p2++, pOut);
  999. pIn1++;
  1000. pOut++;
  1001. }
  1002. break;
  1003. }
  1004. /* Opcode: Copy P1 P2 P3 * *
  1005. **
  1006. ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
  1007. **
  1008. ** This instruction makes a deep copy of the value. A duplicate
  1009. ** is made of any string or blob constant. See also OP_SCopy.
  1010. */
  1011. case OP_Copy: {
  1012. int n;
  1013. n = pOp->p3;
  1014. pIn1 = &aMem[pOp->p1];
  1015. pOut = &aMem[pOp->p2];
  1016. assert( pOut!=pIn1 );
  1017. while( 1 ){
  1018. sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
  1019. Deephemeralize(pOut);
  1020. #ifdef SQLITE_DEBUG
  1021. pOut->pScopyFrom = 0;
  1022. #endif
  1023. REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
  1024. if( (n--)==0 ) break;
  1025. pOut++;
  1026. pIn1++;
  1027. }
  1028. break;
  1029. }
  1030. /* Opcode: SCopy P1 P2 * * *
  1031. **
  1032. ** Make a shallow copy of register P1 into register P2.
  1033. **
  1034. ** This instruction makes a shallow copy of the value. If the value
  1035. ** is a string or blob, then the copy is only a pointer to the
  1036. ** original and hence if the original changes so will the copy.
  1037. ** Worse, if the original is deallocated, the copy becomes invalid.
  1038. ** Thus the program must guarantee that the original will not change
  1039. ** during the lifetime of the copy. Use OP_Copy to make a complete
  1040. ** copy.
  1041. */
  1042. case OP_SCopy: { /* in1, out2 */
  1043. pIn1 = &aMem[pOp->p1];
  1044. pOut = &aMem[pOp->p2];
  1045. assert( pOut!=pIn1 );
  1046. sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
  1047. #ifdef SQLITE_DEBUG
  1048. if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
  1049. #endif
  1050. REGISTER_TRACE(pOp->p2, pOut);
  1051. break;
  1052. }
  1053. /* Opcode: ResultRow P1 P2 * * *
  1054. **
  1055. ** The registers P1 through P1+P2-1 contain a single row of
  1056. ** results. This opcode causes the sqlite3_step() call to terminate
  1057. ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
  1058. ** structure to provide access to the top P1 values as the result
  1059. ** row.
  1060. */
  1061. case OP_ResultRow: {
  1062. Mem *pMem;
  1063. int i;
  1064. assert( p->nResColumn==pOp->p2 );
  1065. assert( pOp->p1>0 );
  1066. assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
  1067. /* If this statement has violated immediate foreign key constraints, do
  1068. ** not return the number of rows modified. And do not RELEASE the statement
  1069. ** transaction. It needs to be rolled back. */
  1070. if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
  1071. assert( db->flags&SQLITE_CountRows );
  1072. assert( p->usesStmtJournal );
  1073. break;
  1074. }
  1075. /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
  1076. ** DML statements invoke this opcode to return the number of rows
  1077. ** modified to the user. This is the only way that a VM that
  1078. ** opens a statement transaction may invoke this opcode.
  1079. **
  1080. ** In case this is such a statement, close any statement transaction
  1081. ** opened by this VM before returning control to the user. This is to
  1082. ** ensure that statement-transactions are always nested, not overlapping.
  1083. ** If the open statement-transaction is not closed here, then the user
  1084. ** may step another VM that opens its own statement transaction. This
  1085. ** may lead to overlapping statement transactions.
  1086. **
  1087. ** The statement transaction is never a top-level transaction. Hence
  1088. ** the RELEASE call below can never fail.
  1089. */
  1090. assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
  1091. rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
  1092. if( NEVER(rc!=SQLITE_OK) ){
  1093. break;
  1094. }
  1095. /* Invalidate all ephemeral cursor row caches */
  1096. p->cacheCtr = (p->cacheCtr + 2)|1;
  1097. /* Make sure the results of the current row are \000 terminated
  1098. ** and have an assigned type. The results are de-ephemeralized as
  1099. ** a side effect.
  1100. */
  1101. pMem = p->pResultSet = &aMem[pOp->p1];
  1102. for(i=0; i<pOp->p2; i++){
  1103. assert( memIsValid(&pMem[i]) );
  1104. Deephemeralize(&pMem[i]);
  1105. assert( (pMem[i].flags & MEM_Ephem)==0
  1106. || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
  1107. sqlite3VdbeMemNulTerminate(&pMem[i]);
  1108. sqlite3VdbeMemStoreType(&pMem[i]);
  1109. REGISTER_TRACE(pOp->p1+i, &pMem[i]);
  1110. }
  1111. if( db->mallocFailed ) goto no_mem;
  1112. /* Return SQLITE_ROW
  1113. */
  1114. p->pc = pc + 1;
  1115. rc = SQLITE_ROW;
  1116. goto vdbe_return;
  1117. }
  1118. /* Opcode: Concat P1 P2 P3 * *
  1119. **
  1120. ** Add the text in register P1 onto the end of the text in
  1121. ** register P2 and store the result in register P3.
  1122. ** If either the P1 or P2 text are NULL then store NULL in P3.
  1123. **
  1124. ** P3 = P2 || P1
  1125. **
  1126. ** It is illegal for P1 and P3 to be the same register. Sometimes,
  1127. ** if P3 is the same register as P2, the implementation is able
  1128. ** to avoid a memcpy().
  1129. */
  1130. case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
  1131. i64 nByte;
  1132. pIn1 = &aMem[pOp->p1];
  1133. pIn2 = &aMem[pOp->p2];
  1134. pOut = &aMem[pOp->p3];
  1135. assert( pIn1!=pOut );
  1136. if( (pIn1->flags | pIn2->flags) & MEM_Null ){
  1137. sqlite3VdbeMemSetNull(pOut);
  1138. break;
  1139. }
  1140. if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
  1141. Stringify(pIn1, encoding);
  1142. Stringify(pIn2, encoding);
  1143. nByte = pIn1->n + pIn2->n;
  1144. if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1145. goto too_big;
  1146. }
  1147. MemSetTypeFlag(pOut, MEM_Str);
  1148. if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
  1149. goto no_mem;
  1150. }
  1151. if( pOut!=pIn2 ){
  1152. memcpy(pOut->z, pIn2->z, pIn2->n);
  1153. }
  1154. memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
  1155. pOut->z[nByte] = 0;
  1156. pOut->z[nByte+1] = 0;
  1157. pOut->flags |= MEM_Term;
  1158. pOut->n = (int)nByte;
  1159. pOut->enc = encoding;
  1160. UPDATE_MAX_BLOBSIZE(pOut);
  1161. break;
  1162. }
  1163. /* Opcode: Add P1 P2 P3 * *
  1164. **
  1165. ** Add the value in register P1 to the value in register P2
  1166. ** and store the result in register P3.
  1167. ** If either input is NULL, the result is NULL.
  1168. */
  1169. /* Opcode: Multiply P1 P2 P3 * *
  1170. **
  1171. **
  1172. ** Multiply the value in register P1 by the value in register P2
  1173. ** and store the result in register P3.
  1174. ** If either input is NULL, the result is NULL.
  1175. */
  1176. /* Opcode: Subtract P1 P2 P3 * *
  1177. **
  1178. ** Subtract the value in register P1 from the value in register P2
  1179. ** and store the result in register P3.
  1180. ** If either input is NULL, the result is NULL.
  1181. */
  1182. /* Opcode: Divide P1 P2 P3 * *
  1183. **
  1184. ** Divide the value in register P1 by the value in register P2
  1185. ** and store the result in register P3 (P3=P2/P1). If the value in
  1186. ** register P1 is zero, then the result is NULL. If either input is
  1187. ** NULL, the result is NULL.
  1188. */
  1189. /* Opcode: Remainder P1 P2 P3 * *
  1190. **
  1191. ** Compute the remainder after integer division of the value in
  1192. ** register P1 by the value in register P2 and store the result in P3.
  1193. ** If the value in register P2 is zero the result is NULL.
  1194. ** If either operand is NULL, the result is NULL.
  1195. */
  1196. case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
  1197. case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
  1198. case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
  1199. case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
  1200. case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
  1201. char bIntint; /* Started out as two integer operands */
  1202. int flags; /* Combined MEM_* flags from both inputs */
  1203. i64 iA; /* Integer value of left operand */
  1204. i64 iB; /* Integer value of right operand */
  1205. double rA; /* Real value of left operand */
  1206. double rB; /* Real value of right operand */
  1207. pIn1 = &aMem[pOp->p1];
  1208. applyNumericAffinity(pIn1);
  1209. pIn2 = &aMem[pOp->p2];
  1210. applyNumericAffinity(pIn2);
  1211. pOut = &aMem[pOp->p3];
  1212. flags = pIn1->flags | pIn2->flags;
  1213. if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
  1214. if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
  1215. iA = pIn1->u.i;
  1216. iB = pIn2->u.i;
  1217. bIntint = 1;
  1218. switch( pOp->opcode ){
  1219. case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
  1220. case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
  1221. case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
  1222. case OP_Divide: {
  1223. if( iA==0 ) goto arithmetic_result_is_null;
  1224. if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
  1225. iB /= iA;
  1226. break;
  1227. }
  1228. default: {
  1229. if( iA==0 ) goto arithmetic_result_is_null;
  1230. if( iA==-1 ) iA = 1;
  1231. iB %= iA;
  1232. break;
  1233. }
  1234. }
  1235. pOut->u.i = iB;
  1236. MemSetTypeFlag(pOut, MEM_Int);
  1237. }else{
  1238. bIntint = 0;
  1239. fp_math:
  1240. rA = sqlite3VdbeRealValue(pIn1);
  1241. rB = sqlite3VdbeRealValue(pIn2);
  1242. switch( pOp->opcode ){
  1243. case OP_Add: rB += rA; break;
  1244. case OP_Subtract: rB -= rA; break;
  1245. case OP_Multiply: rB *= rA; break;
  1246. case OP_Divide: {
  1247. /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
  1248. if( rA==(double)0 ) goto arithmetic_result_is_null;
  1249. rB /= rA;
  1250. break;
  1251. }
  1252. default: {
  1253. iA = (i64)rA;
  1254. iB = (i64)rB;
  1255. if( iA==0 ) goto arithmetic_result_is_null;
  1256. if( iA==-1 ) iA = 1;
  1257. rB = (double)(iB % iA);
  1258. break;
  1259. }
  1260. }
  1261. #ifdef SQLITE_OMIT_FLOATING_POINT
  1262. pOut->u.i = rB;
  1263. MemSetTypeFlag(pOut, MEM_Int);
  1264. #else
  1265. if( sqlite3IsNaN(rB) ){
  1266. goto arithmetic_result_is_null;
  1267. }
  1268. pOut->r = rB;
  1269. MemSetTypeFlag(pOut, MEM_Real);
  1270. if( (flags & MEM_Real)==0 && !bIntint ){
  1271. sqlite3VdbeIntegerAffinity(pOut);
  1272. }
  1273. #endif
  1274. }
  1275. break;
  1276. arithmetic_result_is_null:
  1277. sqlite3VdbeMemSetNull(pOut);
  1278. break;
  1279. }
  1280. /* Opcode: CollSeq P1 * * P4
  1281. **
  1282. ** P4 is a pointer to a CollSeq struct. If the next call to a user function
  1283. ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
  1284. ** be returned. This is used by the built-in min(), max() and nullif()
  1285. ** functions.
  1286. **
  1287. ** If P1 is not zero, then it is a register that a subsequent min() or
  1288. ** max() aggregate will set to 1 if the current row is not the minimum or
  1289. ** maximum. The P1 register is initialized to 0 by this instruction.
  1290. **
  1291. ** The interface used by the implementation of the aforementioned functions
  1292. ** to retrieve the collation sequence set by this opcode is not available
  1293. ** publicly, only to user functions defined in func.c.
  1294. */
  1295. case OP_CollSeq: {
  1296. assert( pOp->p4type==P4_COLLSEQ );
  1297. if( pOp->p1 ){
  1298. sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
  1299. }
  1300. break;
  1301. }
  1302. /* Opcode: Function P1 P2 P3 P4 P5
  1303. **
  1304. ** Invoke a user function (P4 is a pointer to a Function structure that
  1305. ** defines the function) with P5 arguments taken from register P2 and
  1306. ** successors. The result of the function is stored in register P3.
  1307. ** Register P3 must not be one of the function inputs.
  1308. **
  1309. ** P1 is a 32-bit bitmask indicating whether or not each argument to the
  1310. ** function was determined to be constant at compile time. If the first
  1311. ** argument was constant then bit 0 of P1 is set. This is used to determine
  1312. ** whether meta data associated with a user function argument using the
  1313. ** sqlite3_set_auxdata() API may be safely retained until the next
  1314. ** invocation of this opcode.
  1315. **
  1316. ** See also: AggStep and AggFinal
  1317. */
  1318. case OP_Function: {
  1319. int i;
  1320. Mem *pArg;
  1321. sqlite3_context ctx;
  1322. sqlite3_value **apVal;
  1323. int n;
  1324. n = pOp->p5;
  1325. apVal = p->apArg;
  1326. assert( apVal || n==0 );
  1327. assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  1328. pOut = &aMem[pOp->p3];
  1329. memAboutToChange(p, pOut);
  1330. assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
  1331. assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
  1332. pArg = &aMem[pOp->p2];
  1333. for(i=0; i<n; i++, pArg++){
  1334. assert( memIsValid(pArg) );
  1335. apVal[i] = pArg;
  1336. Deephemeralize(pArg);
  1337. sqlite3VdbeMemStoreType(pArg);
  1338. REGISTER_TRACE(pOp->p2+i, pArg);
  1339. }
  1340. assert( pOp->p4type==P4_FUNCDEF );
  1341. ctx.pFunc = pOp->p4.pFunc;
  1342. ctx.s.flags = MEM_Null;
  1343. ctx.s.db = db;
  1344. ctx.s.xDel = 0;
  1345. ctx.s.zMalloc = 0;
  1346. ctx.iOp = pc;
  1347. ctx.pVdbe = p;
  1348. /* The output cell may already have a buffer allocated. Move
  1349. ** the pointer to ctx.s so in case the user-function can use
  1350. ** the already allocated buffer instead of allocating a new one.
  1351. */
  1352. sqlite3VdbeMemMove(&ctx.s, pOut);
  1353. MemSetTypeFlag(&ctx.s, MEM_Null);
  1354. ctx.fErrorOrAux = 0;
  1355. if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  1356. assert( pOp>aOp );
  1357. assert( pOp[-1].p4type==P4_COLLSEQ );
  1358. assert( pOp[-1].opcode==OP_CollSeq );
  1359. ctx.pColl = pOp[-1].p4.pColl;
  1360. }
  1361. db->lastRowid = lastRowid;
  1362. (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  1363. lastRowid = db->lastRowid;
  1364. if( db->mallocFailed ){
  1365. /* Even though a malloc() has failed, the implementation of the
  1366. ** user function may have called an sqlite3_result_XXX() function
  1367. ** to return a value. The following call releases any resources
  1368. ** associated with such a value.
  1369. */
  1370. sqlite3VdbeMemRelease(&ctx.s);
  1371. goto no_mem;
  1372. }
  1373. /* If the function returned an error, throw an exception */
  1374. if( ctx.fErrorOrAux ){
  1375. if( ctx.isError ){
  1376. sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
  1377. rc = ctx.isError;
  1378. }
  1379. sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
  1380. }
  1381. /* Copy the result of the function into register P3 */
  1382. sqlite3VdbeChangeEncoding(&ctx.s, encoding);
  1383. sqlite3VdbeMemMove(pOut, &ctx.s);
  1384. if( sqlite3VdbeMemTooBig(pOut) ){
  1385. goto too_big;
  1386. }
  1387. #if 0
  1388. /* The app-defined function has done something that as caused this
  1389. ** statement to expire. (Perhaps the function called sqlite3_exec()
  1390. ** with a CREATE TABLE statement.)
  1391. */
  1392. if( p->expired ) rc = SQLITE_ABORT;
  1393. #endif
  1394. REGISTER_TRACE(pOp->p3, pOut);
  1395. UPDATE_MAX_BLOBSIZE(pOut);
  1396. break;
  1397. }
  1398. /* Opcode: BitAnd P1 P2 P3 * *
  1399. **
  1400. ** Take the bit-wise AND of the values in register P1 and P2 and
  1401. ** store the result in register P3.
  1402. ** If either input is NULL, the result is NULL.
  1403. */
  1404. /* Opcode: BitOr P1 P2 P3 * *
  1405. **
  1406. ** Take the bit-wise OR of the values in register P1 and P2 and
  1407. ** store the result in register P3.
  1408. ** If either input is NULL, the result is NULL.
  1409. */
  1410. /* Opcode: ShiftLeft P1 P2 P3 * *
  1411. **
  1412. ** Shift the integer value in register P2 to the left by the
  1413. ** number of bits specified by the integer in register P1.
  1414. ** Store the result in register P3.
  1415. ** If either input is NULL, the result is NULL.
  1416. */
  1417. /* Opcode: ShiftRight P1 P2 P3 * *
  1418. **
  1419. ** Shift the integer value in register P2 to the right by the
  1420. ** number of bits specified by the integer in register P1.
  1421. ** Store the result in register P3.
  1422. ** If either input is NULL, the result is NULL.
  1423. */
  1424. case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
  1425. case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
  1426. case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
  1427. case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
  1428. i64 iA;
  1429. u64 uA;
  1430. i64 iB;
  1431. u8 op;
  1432. pIn1 = &aMem[pOp->p1];
  1433. pIn2 = &aMem[pOp->p2];
  1434. pOut = &aMem[pOp->p3];
  1435. if( (pIn1->flags | pIn2->flags) & MEM_Null ){
  1436. sqlite3VdbeMemSetNull(pOut);
  1437. break;
  1438. }
  1439. iA = sqlite3VdbeIntValue(pIn2);
  1440. iB = sqlite3VdbeIntValue(pIn1);
  1441. op = pOp->opcode;
  1442. if( op==OP_BitAnd ){
  1443. iA &= iB;
  1444. }else if( op==OP_BitOr ){
  1445. iA |= iB;
  1446. }else if( iB!=0 ){
  1447. assert( op==OP_ShiftRight || op==OP_ShiftLeft );
  1448. /* If shifting by a negative amount, shift in the other direction */
  1449. if( iB<0 ){
  1450. assert( OP_ShiftRight==OP_ShiftLeft+1 );
  1451. op = 2*OP_ShiftLeft + 1 - op;
  1452. iB = iB>(-64) ? -iB : 64;
  1453. }
  1454. if( iB>=64 ){
  1455. iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
  1456. }else{
  1457. memcpy(&uA, &iA, sizeof(uA));
  1458. if( op==OP_ShiftLeft ){
  1459. uA <<= iB;
  1460. }else{
  1461. uA >>= iB;
  1462. /* Sign-extend on a right shift of a negative number */
  1463. if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
  1464. }
  1465. memcpy(&iA, &uA, sizeof(iA));
  1466. }
  1467. }
  1468. pOut->u.i = iA;
  1469. MemSetTypeFlag(pOut, MEM_Int);
  1470. break;
  1471. }
  1472. /* Opcode: AddImm P1 P2 * * *
  1473. **
  1474. ** Add the constant P2 to the value in register P1.
  1475. ** The result is always an integer.
  1476. **
  1477. ** To force any register to be an integer, just add 0.
  1478. */
  1479. case OP_AddImm: { /* in1 */
  1480. pIn1 = &aMem[pOp->p1];
  1481. memAboutToChange(p, pIn1);
  1482. sqlite3VdbeMemIntegerify(pIn1);
  1483. pIn1->u.i += pOp->p2;
  1484. break;
  1485. }
  1486. /* Opcode: MustBeInt P1 P2 * * *
  1487. **
  1488. ** Force the value in register P1 to be an integer. If the value
  1489. ** in P1 is not an integer and cannot be converted into an integer
  1490. ** without data loss, then jump immediately to P2, or if P2==0
  1491. ** raise an SQLITE_MISMATCH exception.
  1492. */
  1493. case OP_MustBeInt: { /* jump, in1 */
  1494. pIn1 = &aMem[pOp->p1];
  1495. applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
  1496. if( (pIn1->flags & MEM_Int)==0 ){
  1497. if( pOp->p2==0 ){
  1498. rc = SQLITE_MISMATCH;
  1499. goto abort_due_to_error;
  1500. }else{
  1501. pc = pOp->p2 - 1;
  1502. }
  1503. }else{
  1504. MemSetTypeFlag(pIn1, MEM_Int);
  1505. }
  1506. break;
  1507. }
  1508. #ifndef SQLITE_OMIT_FLOATING_POINT
  1509. /* Opcode: RealAffinity P1 * * * *
  1510. **
  1511. ** If register P1 holds an integer convert it to a real value.
  1512. **
  1513. ** This opcode is used when extracting information from a column that
  1514. ** has REAL affinity. Such column values may still be stored as
  1515. ** integers, for space efficiency, but after extraction we want them
  1516. ** to have only a real value.
  1517. */
  1518. case OP_RealAffinity: { /* in1 */
  1519. pIn1 = &aMem[pOp->p1];
  1520. if( pIn1->flags & MEM_Int ){
  1521. sqlite3VdbeMemRealify(pIn1);
  1522. }
  1523. break;
  1524. }
  1525. #endif
  1526. #ifndef SQLITE_OMIT_CAST
  1527. /* Opcode: ToText P1 * * * *
  1528. **
  1529. ** Force the value in register P1 to be text.
  1530. ** If the value is numeric, convert it to a string using the
  1531. ** equivalent of printf(). Blob values are unchanged and
  1532. ** are afterwards simply interpreted as text.
  1533. **
  1534. ** A NULL value is not changed by this routine. It remains NULL.
  1535. */
  1536. case OP_ToText: { /* same as TK_TO_TEXT, in1 */
  1537. pIn1 = &aMem[pOp->p1];
  1538. memAboutToChange(p, pIn1);
  1539. if( pIn1->flags & MEM_Null ) break;
  1540. assert( MEM_Str==(MEM_Blob>>3) );
  1541. pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
  1542. applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
  1543. rc = ExpandBlob(pIn1);
  1544. assert( pIn1->flags & MEM_Str || db->mallocFailed );
  1545. pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
  1546. UPDATE_MAX_BLOBSIZE(pIn1);
  1547. break;
  1548. }
  1549. /* Opcode: ToBlob P1 * * * *
  1550. **
  1551. ** Force the value in register P1 to be a BLOB.
  1552. ** If the value is numeric, convert it to a string first.
  1553. ** Strings are simply reinterpreted as blobs with no change
  1554. ** to the underlying data.
  1555. **
  1556. ** A NULL value is not changed by this routine. It remains NULL.
  1557. */
  1558. case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
  1559. pIn1 = &aMem[pOp->p1];
  1560. if( pIn1->flags & MEM_Null ) break;
  1561. if( (pIn1->flags & MEM_Blob)==0 ){
  1562. applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
  1563. assert( pIn1->flags & MEM_Str || db->mallocFailed );
  1564. MemSetTypeFlag(pIn1, MEM_Blob);
  1565. }else{
  1566. pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
  1567. }
  1568. UPDATE_MAX_BLOBSIZE(pIn1);
  1569. break;
  1570. }
  1571. /* Opcode: ToNumeric P1 * * * *
  1572. **
  1573. ** Force the value in register P1 to be numeric (either an
  1574. ** integer or a floating-point number.)
  1575. ** If the value is text or blob, try to convert it to an using the
  1576. ** equivalent of atoi() or atof() and store 0 if no such conversion
  1577. ** is possible.
  1578. **
  1579. ** A NULL value is not changed by this routine. It remains NULL.
  1580. */
  1581. case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
  1582. pIn1 = &aMem[pOp->p1];
  1583. sqlite3VdbeMemNumerify(pIn1);
  1584. break;
  1585. }
  1586. #endif /* SQLITE_OMIT_CAST */
  1587. /* Opcode: ToInt P1 * * * *
  1588. **
  1589. ** Force the value in register P1 to be an integer. If
  1590. ** The value is currently a real number, drop its fractional part.
  1591. ** If the value is text or blob, try to convert it to an integer using the
  1592. ** equivalent of atoi() and store 0 if no such conversion is possible.
  1593. **
  1594. ** A NULL value is not changed by this routine. It remains NULL.
  1595. */
  1596. case OP_ToInt: { /* same as TK_TO_INT, in1 */
  1597. pIn1 = &aMem[pOp->p1];
  1598. if( (pIn1->flags & MEM_Null)==0 ){
  1599. sqlite3VdbeMemIntegerify(pIn1);
  1600. }
  1601. break;
  1602. }
  1603. #if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
  1604. /* Opcode: ToReal P1 * * * *
  1605. **
  1606. ** Force the value in register P1 to be a floating point number.
  1607. ** If The value is currently an integer, convert it.
  1608. ** If the value is text or blob, try to convert it to an integer using the
  1609. ** equivalent of atoi() and store 0.0 if no such conversion is possible.
  1610. **
  1611. ** A NULL value is not changed by this routine. It remains NULL.
  1612. */
  1613. case OP_ToReal: { /* same as TK_TO_REAL, in1 */
  1614. pIn1 = &aMem[pOp->p1];
  1615. memAboutToChange(p, pIn1);
  1616. if( (pIn1->flags & MEM_Null)==0 ){
  1617. sqlite3VdbeMemRealify(pIn1);
  1618. }
  1619. break;
  1620. }
  1621. #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
  1622. /* Opcode: Lt P1 P2 P3 P4 P5
  1623. **
  1624. ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
  1625. ** jump to address P2.
  1626. **
  1627. ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
  1628. ** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
  1629. ** bit is clear then fall through if either operand is NULL.
  1630. **
  1631. ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
  1632. ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
  1633. ** to coerce both inputs according to this affinity before the
  1634. ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
  1635. ** affinity is used. Note that the affinity conversions are stored
  1636. ** back into the input registers P1 and P3. So this opcode can cause
  1637. ** persistent changes to registers P1 and P3.
  1638. **
  1639. ** Once any conversions have taken place, and neither value is NULL,
  1640. ** the values are compared. If both values are blobs then memcmp() is
  1641. ** used to determine the results of the comparison. If both values
  1642. ** are text, then the appropriate collating function specified in
  1643. ** P4 is used to do the comparison. If P4 is not specified then
  1644. ** memcmp() is used to compare text string. If both values are
  1645. ** numeric, then a numeric comparison is used. If the two values
  1646. ** are of different types, then numbers are considered less than
  1647. ** strings and strings are considered less than blobs.
  1648. **
  1649. ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
  1650. ** store a boolean result (either 0, or 1, or NULL) in register P2.
  1651. **
  1652. ** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
  1653. ** equal to one another, provided that they do not have their MEM_Cleared
  1654. ** bit set.
  1655. */
  1656. /* Opcode: Ne P1 P2 P3 P4 P5
  1657. **
  1658. ** This works just like the Lt opcode except that the jump is taken if
  1659. ** the operands in registers P1 and P3 are not equal. See the Lt opcode for
  1660. ** additional information.
  1661. **
  1662. ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
  1663. ** true or false and is never NULL. If both operands are NULL then the result
  1664. ** of comparison is false. If either operand is NULL then the result is true.
  1665. ** If neither operand is NULL the result is the same as it would be if
  1666. ** the SQLITE_NULLEQ flag were omitted from P5.
  1667. */
  1668. /* Opcode: Eq P1 P2 P3 P4 P5
  1669. **
  1670. ** This works just like the Lt opcode except that the jump is taken if
  1671. ** the operands in registers P1 and P3 are equal.
  1672. ** See the Lt opcode for additional information.
  1673. **
  1674. ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
  1675. ** true or false and is never NULL. If both operands are NULL then the result
  1676. ** of comparison is true. If either operand is NULL then the result is false.
  1677. ** If neither operand is NULL the result is the same as it would be if
  1678. ** the SQLITE_NULLEQ flag were omitted from P5.
  1679. */
  1680. /* Opcode: Le P1 P2 P3 P4 P5
  1681. **
  1682. ** This works just like the Lt opcode except that the jump is taken if
  1683. ** the content of register P3 is less than or equal to the content of
  1684. ** register P1. See the Lt opcode for additional information.
  1685. */
  1686. /* Opcode: Gt P1 P2 P3 P4 P5
  1687. **
  1688. ** This works just like the Lt opcode except that the jump is taken if
  1689. ** the content of register P3 is greater than the content of
  1690. ** register P1. See the Lt opcode for additional information.
  1691. */
  1692. /* Opcode: Ge P1 P2 P3 P4 P5
  1693. **
  1694. ** This works just like the Lt opcode except that the jump is taken if
  1695. ** the content of register P3 is greater than or equal to the content of
  1696. ** register P1. See the Lt opcode for additional information.
  1697. */
  1698. case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
  1699. case OP_Ne: /* same as TK_NE, jump, in1, in3 */
  1700. case OP_Lt: /* same as TK_LT, jump, in1, in3 */
  1701. case OP_Le: /* same as TK_LE, jump, in1, in3 */
  1702. case OP_Gt: /* same as TK_GT, jump, in1, in3 */
  1703. case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
  1704. int res; /* Result of the comparison of pIn1 against pIn3 */
  1705. char affinity; /* Affinity to use for comparison */
  1706. u16 flags1; /* Copy of initial value of pIn1->flags */
  1707. u16 flags3; /* Copy of initial value of pIn3->flags */
  1708. pIn1 = &aMem[pOp->p1];
  1709. pIn3 = &aMem[pOp->p3];
  1710. flags1 = pIn1->flags;
  1711. flags3 = pIn3->flags;
  1712. if( (flags1 | flags3)&MEM_Null ){
  1713. /* One or both operands are NULL */
  1714. if( pOp->p5 & SQLITE_NULLEQ ){
  1715. /* If SQLITE_NULLEQ is set (which will only happen if the operator is
  1716. ** OP_Eq or OP_Ne) then take the jump or not depending on whether
  1717. ** or not both operands are null.
  1718. */
  1719. assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
  1720. assert( (flags1 & MEM_Cleared)==0 );
  1721. if( (flags1&MEM_Null)!=0
  1722. && (flags3&MEM_Null)!=0
  1723. && (flags3&MEM_Cleared)==0
  1724. ){
  1725. res = 0; /* Results are equal */
  1726. }else{
  1727. res = 1; /* Results are not equal */
  1728. }
  1729. }else{
  1730. /* SQLITE_NULLEQ is clear and at least one operand is NULL,
  1731. ** then the result is always NULL.
  1732. ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
  1733. */
  1734. if( pOp->p5 & SQLITE_JUMPIFNULL ){
  1735. pc = pOp->p2-1;
  1736. }else if( pOp->p5 & SQLITE_STOREP2 ){
  1737. pOut = &aMem[pOp->p2];
  1738. MemSetTypeFlag(pOut, MEM_Null);
  1739. REGISTER_TRACE(pOp->p2, pOut);
  1740. }
  1741. break;
  1742. }
  1743. }else{
  1744. /* Neither operand is NULL. Do a comparison. */
  1745. affinity = pOp->p5 & SQLITE_AFF_MASK;
  1746. if( affinity ){
  1747. applyAffinity(pIn1, affinity, encoding);
  1748. applyAffinity(pIn3, affinity, encoding);
  1749. if( db->mallocFailed ) goto no_mem;
  1750. }
  1751. assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
  1752. ExpandBlob(pIn1);
  1753. ExpandBlob(pIn3);
  1754. res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
  1755. }
  1756. switch( pOp->opcode ){
  1757. case OP_Eq: res = res==0; break;
  1758. case OP_Ne: res = res!=0; break;
  1759. case OP_Lt: res = res<0; break;
  1760. case OP_Le: res = res<=0; break;
  1761. case OP_Gt: res = res>0; break;
  1762. default: res = res>=0; break;
  1763. }
  1764. if( pOp->p5 & SQLITE_STOREP2 ){
  1765. pOut = &aMem[pOp->p2];
  1766. memAboutToChange(p, pOut);
  1767. MemSetTypeFlag(pOut, MEM_Int);
  1768. pOut->u.i = res;
  1769. REGISTER_TRACE(pOp->p2, pOut);
  1770. }else if( res ){
  1771. pc = pOp->p2-1;
  1772. }
  1773. /* Undo any changes made by applyAffinity() to the input registers. */
  1774. pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
  1775. pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
  1776. break;
  1777. }
  1778. /* Opcode: Permutation * * * P4 *
  1779. **
  1780. ** Set the permutation used by the OP_Compare operator to be the array
  1781. ** of integers in P4.
  1782. **
  1783. ** The permutation is only valid until the next OP_Compare that has
  1784. ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
  1785. ** occur immediately prior to the OP_Compare.
  1786. */
  1787. case OP_Permutation: {
  1788. assert( pOp->p4type==P4_INTARRAY );
  1789. assert( pOp->p4.ai );
  1790. aPermute = pOp->p4.ai;
  1791. break;
  1792. }
  1793. /* Opcode: Compare P1 P2 P3 P4 P5
  1794. **
  1795. ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
  1796. ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
  1797. ** the comparison for use by the next OP_Jump instruct.
  1798. **
  1799. ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
  1800. ** determined by the most recent OP_Permutation operator. If the
  1801. ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
  1802. ** order.
  1803. **
  1804. ** P4 is a KeyInfo structure that defines collating sequences and sort
  1805. ** orders for the comparison. The permutation applies to registers
  1806. ** only. The KeyInfo elements are used sequentially.
  1807. **
  1808. ** The comparison is a sort comparison, so NULLs compare equal,
  1809. ** NULLs are less than numbers, numbers are less than strings,
  1810. ** and strings are less than blobs.
  1811. */
  1812. case OP_Compare: {
  1813. int n;
  1814. int i;
  1815. int p1;
  1816. int p2;
  1817. const KeyInfo *pKeyInfo;
  1818. int idx;
  1819. CollSeq *pColl; /* Collating sequence to use on this term */
  1820. int bRev; /* True for DESCENDING sort order */
  1821. if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
  1822. n = pOp->p3;
  1823. pKeyInfo = pOp->p4.pKeyInfo;
  1824. assert( n>0 );
  1825. assert( pKeyInfo!=0 );
  1826. p1 = pOp->p1;
  1827. p2 = pOp->p2;
  1828. #if SQLITE_DEBUG
  1829. if( aPermute ){
  1830. int k, mx = 0;
  1831. for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
  1832. assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 );
  1833. assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 );
  1834. }else{
  1835. assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 );
  1836. assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 );
  1837. }
  1838. #endif /* SQLITE_DEBUG */
  1839. for(i=0; i<n; i++){
  1840. idx = aPermute ? aPermute[i] : i;
  1841. assert( memIsValid(&aMem[p1+idx]) );
  1842. assert( memIsValid(&aMem[p2+idx]) );
  1843. REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
  1844. REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
  1845. assert( i<pKeyInfo->nField );
  1846. pColl = pKeyInfo->aColl[i];
  1847. bRev = pKeyInfo->aSortOrder[i];
  1848. iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
  1849. if( iCompare ){
  1850. if( bRev ) iCompare = -iCompare;
  1851. break;
  1852. }
  1853. }
  1854. aPermute = 0;
  1855. break;
  1856. }
  1857. /* Opcode: Jump P1 P2 P3 * *
  1858. **
  1859. ** Jump to the instruction at address P1, P2, or P3 depending on whether
  1860. ** in the most recent OP_Compare instruction the P1 vector was less than
  1861. ** equal to, or greater than the P2 vector, respectively.
  1862. */
  1863. case OP_Jump: { /* jump */
  1864. if( iCompare<0 ){
  1865. pc = pOp->p1 - 1;
  1866. }else if( iCompare==0 ){
  1867. pc = pOp->p2 - 1;
  1868. }else{
  1869. pc = pOp->p3 - 1;
  1870. }
  1871. break;
  1872. }
  1873. /* Opcode: And P1 P2 P3 * *
  1874. **
  1875. ** Take the logical AND of the values in registers P1 and P2 and
  1876. ** write the result into register P3.
  1877. **
  1878. ** If either P1 or P2 is 0 (false) then the result is 0 even if
  1879. ** the other input is NULL. A NULL and true or two NULLs give
  1880. ** a NULL output.
  1881. */
  1882. /* Opcode: Or P1 P2 P3 * *
  1883. **
  1884. ** Take the logical OR of the values in register P1 and P2 and
  1885. ** store the answer in register P3.
  1886. **
  1887. ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
  1888. ** even if the other input is NULL. A NULL and false or two NULLs
  1889. ** give a NULL output.
  1890. */
  1891. case OP_And: /* same as TK_AND, in1, in2, out3 */
  1892. case OP_Or: { /* same as TK_OR, in1, in2, out3 */
  1893. int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
  1894. int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
  1895. pIn1 = &aMem[pOp->p1];
  1896. if( pIn1->flags & MEM_Null ){
  1897. v1 = 2;
  1898. }else{
  1899. v1 = sqlite3VdbeIntValue(pIn1)!=0;
  1900. }
  1901. pIn2 = &aMem[pOp->p2];
  1902. if( pIn2->flags & MEM_Null ){
  1903. v2 = 2;
  1904. }else{
  1905. v2 = sqlite3VdbeIntValue(pIn2)!=0;
  1906. }
  1907. if( pOp->opcode==OP_And ){
  1908. static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
  1909. v1 = and_logic[v1*3+v2];
  1910. }else{
  1911. static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
  1912. v1 = or_logic[v1*3+v2];
  1913. }
  1914. pOut = &aMem[pOp->p3];
  1915. if( v1==2 ){
  1916. MemSetTypeFlag(pOut, MEM_Null);
  1917. }else{
  1918. pOut->u.i = v1;
  1919. MemSetTypeFlag(pOut, MEM_Int);
  1920. }
  1921. break;
  1922. }
  1923. /* Opcode: Not P1 P2 * * *
  1924. **
  1925. ** Interpret the value in register P1 as a boolean value. Store the
  1926. ** boolean complement in register P2. If the value in register P1 is
  1927. ** NULL, then a NULL is stored in P2.
  1928. */
  1929. case OP_Not: { /* same as TK_NOT, in1, out2 */
  1930. pIn1 = &aMem[pOp->p1];
  1931. pOut = &aMem[pOp->p2];
  1932. if( pIn1->flags & MEM_Null ){
  1933. sqlite3VdbeMemSetNull(pOut);
  1934. }else{
  1935. sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
  1936. }
  1937. break;
  1938. }
  1939. /* Opcode: BitNot P1 P2 * * *
  1940. **
  1941. ** Interpret the content of register P1 as an integer. Store the
  1942. ** ones-complement of the P1 value into register P2. If P1 holds
  1943. ** a NULL then store a NULL in P2.
  1944. */
  1945. case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
  1946. pIn1 = &aMem[pOp->p1];
  1947. pOut = &aMem[pOp->p2];
  1948. if( pIn1->flags & MEM_Null ){
  1949. sqlite3VdbeMemSetNull(pOut);
  1950. }else{
  1951. sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
  1952. }
  1953. break;
  1954. }
  1955. /* Opcode: Once P1 P2 * * *
  1956. **
  1957. ** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
  1958. ** set the flag and fall through to the next instruction.
  1959. */
  1960. case OP_Once: { /* jump */
  1961. assert( pOp->p1<p->nOnceFlag );
  1962. if( p->aOnceFlag[pOp->p1] ){
  1963. pc = pOp->p2-1;
  1964. }else{
  1965. p->aOnceFlag[pOp->p1] = 1;
  1966. }
  1967. break;
  1968. }
  1969. /* Opcode: If P1 P2 P3 * *
  1970. **
  1971. ** Jump to P2 if the value in register P1 is true. The value
  1972. ** is considered true if it is numeric and non-zero. If the value
  1973. ** in P1 is NULL then take the jump if P3 is non-zero.
  1974. */
  1975. /* Opcode: IfNot P1 P2 P3 * *
  1976. **
  1977. ** Jump to P2 if the value in register P1 is False. The value
  1978. ** is considered false if it has a numeric value of zero. If the value
  1979. ** in P1 is NULL then take the jump if P3 is zero.
  1980. */
  1981. case OP_If: /* jump, in1 */
  1982. case OP_IfNot: { /* jump, in1 */
  1983. int c;
  1984. pIn1 = &aMem[pOp->p1];
  1985. if( pIn1->flags & MEM_Null ){
  1986. c = pOp->p3;
  1987. }else{
  1988. #ifdef SQLITE_OMIT_FLOATING_POINT
  1989. c = sqlite3VdbeIntValue(pIn1)!=0;
  1990. #else
  1991. c = sqlite3VdbeRealValue(pIn1)!=0.0;
  1992. #endif
  1993. if( pOp->opcode==OP_IfNot ) c = !c;
  1994. }
  1995. if( c ){
  1996. pc = pOp->p2-1;
  1997. }
  1998. break;
  1999. }
  2000. /* Opcode: IsNull P1 P2 * * *
  2001. **
  2002. ** Jump to P2 if the value in register P1 is NULL.
  2003. */
  2004. case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
  2005. pIn1 = &aMem[pOp->p1];
  2006. if( (pIn1->flags & MEM_Null)!=0 ){
  2007. pc = pOp->p2 - 1;
  2008. }
  2009. break;
  2010. }
  2011. /* Opcode: NotNull P1 P2 * * *
  2012. **
  2013. ** Jump to P2 if the value in register P1 is not NULL.
  2014. */
  2015. case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
  2016. pIn1 = &aMem[pOp->p1];
  2017. if( (pIn1->flags & MEM_Null)==0 ){
  2018. pc = pOp->p2 - 1;
  2019. }
  2020. break;
  2021. }
  2022. /* Opcode: Column P1 P2 P3 P4 P5
  2023. **
  2024. ** Interpret the data that cursor P1 points to as a structure built using
  2025. ** the MakeRecord instruction. (See the MakeRecord opcode for additional
  2026. ** information about the format of the data.) Extract the P2-th column
  2027. ** from this record. If there are less that (P2+1)
  2028. ** values in the record, extract a NULL.
  2029. **
  2030. ** The value extracted is stored in register P3.
  2031. **
  2032. ** If the column contains fewer than P2 fields, then extract a NULL. Or,
  2033. ** if the P4 argument is a P4_MEM use the value of the P4 argument as
  2034. ** the result.
  2035. **
  2036. ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
  2037. ** then the cache of the cursor is reset prior to extracting the column.
  2038. ** The first OP_Column against a pseudo-table after the value of the content
  2039. ** register has changed should have this bit set.
  2040. **
  2041. ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
  2042. ** the result is guaranteed to only be used as the argument of a length()
  2043. ** or typeof() function, respectively. The loading of large blobs can be
  2044. ** skipped for length() and all content loading can be skipped for typeof().
  2045. */
  2046. case OP_Column: {
  2047. u32 payloadSize; /* Number of bytes in the record */
  2048. i64 payloadSize64; /* Number of bytes in the record */
  2049. int p1; /* P1 value of the opcode */
  2050. int p2; /* column number to retrieve */
  2051. VdbeCursor *pC; /* The VDBE cursor */
  2052. char *zRec; /* Pointer to complete record-data */
  2053. BtCursor *pCrsr; /* The BTree cursor */
  2054. u32 *aType; /* aType[i] holds the numeric type of the i-th column */
  2055. u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
  2056. int nField; /* number of fields in the record */
  2057. int len; /* The length of the serialized data for the column */
  2058. int i; /* Loop counter */
  2059. char *zData; /* Part of the record being decoded */
  2060. Mem *pDest; /* Where to write the extracted value */
  2061. Mem sMem; /* For storing the record being decoded */
  2062. u8 *zIdx; /* Index into header */
  2063. u8 *zEndHdr; /* Pointer to first byte after the header */
  2064. u32 offset; /* Offset into the data */
  2065. u32 szField; /* Number of bytes in the content of a field */
  2066. int szHdr; /* Size of the header size field at start of record */
  2067. int avail; /* Number of bytes of available data */
  2068. u32 t; /* A type code from the record header */
  2069. Mem *pReg; /* PseudoTable input register */
  2070. p1 = pOp->p1;
  2071. p2 = pOp->p2;
  2072. pC = 0;
  2073. memset(&sMem, 0, sizeof(sMem));
  2074. assert( p1<p->nCursor );
  2075. assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  2076. pDest = &aMem[pOp->p3];
  2077. memAboutToChange(p, pDest);
  2078. zRec = 0;
  2079. /* This block sets the variable payloadSize to be the total number of
  2080. ** bytes in the record.
  2081. **
  2082. ** zRec is set to be the complete text of the record if it is available.
  2083. ** The complete record text is always available for pseudo-tables
  2084. ** If the record is stored in a cursor, the complete record text
  2085. ** might be available in the pC->aRow cache. Or it might not be.
  2086. ** If the data is unavailable, zRec is set to NULL.
  2087. **
  2088. ** We also compute the number of columns in the record. For cursors,
  2089. ** the number of columns is stored in the VdbeCursor.nField element.
  2090. */
  2091. pC = p->apCsr[p1];
  2092. assert( pC!=0 );
  2093. #ifndef SQLITE_OMIT_VIRTUALTABLE
  2094. assert( pC->pVtabCursor==0 );
  2095. #endif
  2096. pCrsr = pC->pCursor;
  2097. if( pCrsr!=0 ){
  2098. /* The record is stored in a B-Tree */
  2099. rc = sqlite3VdbeCursorMoveto(pC);
  2100. if( rc ) goto abort_due_to_error;
  2101. if( pC->nullRow ){
  2102. payloadSize = 0;
  2103. }else if( pC->cacheStatus==p->cacheCtr ){
  2104. payloadSize = pC->payloadSize;
  2105. zRec = (char*)pC->aRow;
  2106. }else if( pC->isIndex ){
  2107. assert( sqlite3BtreeCursorIsValid(pCrsr) );
  2108. VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
  2109. assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
  2110. /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
  2111. ** payload size, so it is impossible for payloadSize64 to be
  2112. ** larger than 32 bits. */
  2113. assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
  2114. payloadSize = (u32)payloadSize64;
  2115. }else{
  2116. assert( sqlite3BtreeCursorIsValid(pCrsr) );
  2117. VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &payloadSize);
  2118. assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
  2119. }
  2120. }else if( ALWAYS(pC->pseudoTableReg>0) ){
  2121. pReg = &aMem[pC->pseudoTableReg];
  2122. if( pC->multiPseudo ){
  2123. sqlite3VdbeMemShallowCopy(pDest, pReg+p2, MEM_Ephem);
  2124. Deephemeralize(pDest);
  2125. goto op_column_out;
  2126. }
  2127. assert( pReg->flags & MEM_Blob );
  2128. assert( memIsValid(pReg) );
  2129. payloadSize = pReg->n;
  2130. zRec = pReg->z;
  2131. pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
  2132. assert( payloadSize==0 || zRec!=0 );
  2133. }else{
  2134. /* Consider the row to be NULL */
  2135. payloadSize = 0;
  2136. }
  2137. /* If payloadSize is 0, then just store a NULL. This can happen because of
  2138. ** nullRow or because of a corrupt database. */
  2139. if( payloadSize==0 ){
  2140. MemSetTypeFlag(pDest, MEM_Null);
  2141. goto op_column_out;
  2142. }
  2143. assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
  2144. if( payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
  2145. goto too_big;
  2146. }
  2147. nField = pC->nField;
  2148. assert( p2<nField );
  2149. /* Read and parse the table header. Store the results of the parse
  2150. ** into the record header cache fields of the cursor.
  2151. */
  2152. aType = pC->aType;
  2153. if( pC->cacheStatus==p->cacheCtr ){
  2154. aOffset = pC->aOffset;
  2155. }else{
  2156. assert(aType);
  2157. avail = 0;
  2158. pC->aOffset = aOffset = &aType[nField];
  2159. pC->payloadSize = payloadSize;
  2160. pC->cacheStatus = p->cacheCtr;
  2161. /* Figure out how many bytes are in the header */
  2162. if( zRec ){
  2163. zData = zRec;
  2164. }else{
  2165. if( pC->isIndex ){
  2166. zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
  2167. }else{
  2168. zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
  2169. }
  2170. /* If KeyFetch()/DataFetch() managed to get the entire payload,
  2171. ** save the payload in the pC->aRow cache. That will save us from
  2172. ** having to make additional calls to fetch the content portion of
  2173. ** the record.
  2174. */
  2175. assert( avail>=0 );
  2176. if( payloadSize <= (u32)avail ){
  2177. zRec = zData;
  2178. pC->aRow = (u8*)zData;
  2179. }else{
  2180. pC->aRow = 0;
  2181. }
  2182. }
  2183. /* The following assert is true in all cases except when
  2184. ** the database file has been corrupted externally.
  2185. ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
  2186. szHdr = getVarint32((u8*)zData, offset);
  2187. /* Make sure a corrupt database has not given us an oversize header.
  2188. ** Do this now to avoid an oversize memory allocation.
  2189. **
  2190. ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
  2191. ** types use so much data space that there can only be 4096 and 32 of
  2192. ** them, respectively. So the maximum header length results from a
  2193. ** 3-byte type for each of the maximum of 32768 columns plus three
  2194. ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
  2195. */
  2196. if( offset > 98307 ){
  2197. rc = SQLITE_CORRUPT_BKPT;
  2198. goto op_column_out;
  2199. }
  2200. /* Compute in len the number of bytes of data we need to read in order
  2201. ** to get nField type values. offset is an upper bound on this. But
  2202. ** nField might be significantly less than the true number of columns
  2203. ** in the table, and in that case, 5*nField+3 might be smaller than offset.
  2204. ** We want to minimize len in order to limit the size of the memory
  2205. ** allocation, especially if a corrupt database file has caused offset
  2206. ** to be oversized. Offset is limited to 98307 above. But 98307 might
  2207. ** still exceed Robson memory allocation limits on some configurations.
  2208. ** On systems that cannot tolerate large memory allocations, nField*5+3
  2209. ** will likely be much smaller since nField will likely be less than
  2210. ** 20 or so. This insures that Robson memory allocation limits are
  2211. ** not exceeded even for corrupt database files.
  2212. */
  2213. len = nField*5 + 3;
  2214. if( len > (int)offset ) len = (int)offset;
  2215. /* The KeyFetch() or DataFetch() above are fast and will get the entire
  2216. ** record header in most cases. But they will fail to get the complete
  2217. ** record header if the record header does not fit on a single page
  2218. ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
  2219. ** acquire the complete header text.
  2220. */
  2221. if( !zRec && avail<len ){
  2222. sMem.flags = 0;
  2223. sMem.db = 0;
  2224. rc = sqlite3VdbeMemFromBtree(pCrsr, 0, len, pC->isIndex, &sMem);
  2225. if( rc!=SQLITE_OK ){
  2226. goto op_column_out;
  2227. }
  2228. zData = sMem.z;
  2229. }
  2230. zEndHdr = (u8 *)&zData[len];
  2231. zIdx = (u8 *)&zData[szHdr];
  2232. /* Scan the header and use it to fill in the aType[] and aOffset[]
  2233. ** arrays. aType[i] will contain the type integer for the i-th
  2234. ** column and aOffset[i] will contain the offset from the beginning
  2235. ** of the record to the start of the data for the i-th column
  2236. */
  2237. for(i=0; i<nField; i++){
  2238. if( zIdx<zEndHdr ){
  2239. aOffset[i] = offset;
  2240. if( zIdx[0]<0x80 ){
  2241. t = zIdx[0];
  2242. zIdx++;
  2243. }else{
  2244. zIdx += sqlite3GetVarint32(zIdx, &t);
  2245. }
  2246. aType[i] = t;
  2247. szField = sqlite3VdbeSerialTypeLen(t);
  2248. offset += szField;
  2249. if( offset<szField ){ /* True if offset overflows */
  2250. zIdx = &zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
  2251. break;
  2252. }
  2253. }else{
  2254. /* If i is less that nField, then there are fewer fields in this
  2255. ** record than SetNumColumns indicated there are columns in the
  2256. ** table. Set the offset for any extra columns not present in
  2257. ** the record to 0. This tells code below to store the default value
  2258. ** for the column instead of deserializing a value from the record.
  2259. */
  2260. aOffset[i] = 0;
  2261. }
  2262. }
  2263. sqlite3VdbeMemRelease(&sMem);
  2264. sMem.flags = MEM_Null;
  2265. /* If we have read more header data than was contained in the header,
  2266. ** or if the end of the last field appears to be past the end of the
  2267. ** record, or if the end of the last field appears to be before the end
  2268. ** of the record (when all fields present), then we must be dealing
  2269. ** with a corrupt database.
  2270. */
  2271. if( (zIdx > zEndHdr) || (offset > payloadSize)
  2272. || (zIdx==zEndHdr && offset!=payloadSize) ){
  2273. rc = SQLITE_CORRUPT_BKPT;
  2274. goto op_column_out;
  2275. }
  2276. }
  2277. /* Get the column information. If aOffset[p2] is non-zero, then
  2278. ** deserialize the value from the record. If aOffset[p2] is zero,
  2279. ** then there are not enough fields in the record to satisfy the
  2280. ** request. In this case, set the value NULL or to P4 if P4 is
  2281. ** a pointer to a Mem object.
  2282. */
  2283. if( aOffset[p2] ){
  2284. assert( rc==SQLITE_OK );
  2285. if( zRec ){
  2286. /* This is the common case where the whole row fits on a single page */
  2287. VdbeMemRelease(pDest);
  2288. sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
  2289. }else{
  2290. /* This branch happens only when the row overflows onto multiple pages */
  2291. t = aType[p2];
  2292. if( (pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
  2293. && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)
  2294. ){
  2295. /* Content is irrelevant for the typeof() function and for
  2296. ** the length(X) function if X is a blob. So we might as well use
  2297. ** bogus content rather than reading content from disk. NULL works
  2298. ** for text and blob and whatever is in the payloadSize64 variable
  2299. ** will work for everything else. */
  2300. zData = t<12 ? (char*)&payloadSize64 : 0;
  2301. }else{
  2302. len = sqlite3VdbeSerialTypeLen(t);
  2303. sqlite3VdbeMemMove(&sMem, pDest);
  2304. rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex,
  2305. &sMem);
  2306. if( rc!=SQLITE_OK ){
  2307. goto op_column_out;
  2308. }
  2309. zData = sMem.z;
  2310. }
  2311. sqlite3VdbeSerialGet((u8*)zData, t, pDest);
  2312. }
  2313. pDest->enc = encoding;
  2314. }else{
  2315. if( pOp->p4type==P4_MEM ){
  2316. sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
  2317. }else{
  2318. MemSetTypeFlag(pDest, MEM_Null);
  2319. }
  2320. }
  2321. /* If we dynamically allocated space to hold the data (in the
  2322. ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
  2323. ** dynamically allocated space over to the pDest structure.
  2324. ** This prevents a memory copy.
  2325. */
  2326. if( sMem.zMalloc ){
  2327. assert( sMem.z==sMem.zMalloc );
  2328. assert( !(pDest->flags & MEM_Dyn) );
  2329. assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
  2330. pDest->flags &= ~(MEM_Ephem|MEM_Static);
  2331. pDest->flags |= MEM_Term;
  2332. pDest->z = sMem.z;
  2333. pDest->zMalloc = sMem.zMalloc;
  2334. }
  2335. rc = sqlite3VdbeMemMakeWriteable(pDest);
  2336. op_column_out:
  2337. UPDATE_MAX_BLOBSIZE(pDest);
  2338. REGISTER_TRACE(pOp->p3, pDest);
  2339. break;
  2340. }
  2341. /* Opcode: Affinity P1 P2 * P4 *
  2342. **
  2343. ** Apply affinities to a range of P2 registers starting with P1.
  2344. **
  2345. ** P4 is a string that is P2 characters long. The nth character of the
  2346. ** string indicates the column affinity that should be used for the nth
  2347. ** memory cell in the range.
  2348. */
  2349. case OP_Affinity: {
  2350. const char *zAffinity; /* The affinity to be applied */
  2351. char cAff; /* A single character of affinity */
  2352. zAffinity = pOp->p4.z;
  2353. assert( zAffinity!=0 );
  2354. assert( zAffinity[pOp->p2]==0 );
  2355. pIn1 = &aMem[pOp->p1];
  2356. while( (cAff = *(zAffinity++))!=0 ){
  2357. assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
  2358. assert( memIsValid(pIn1) );
  2359. ExpandBlob(pIn1);
  2360. applyAffinity(pIn1, cAff, encoding);
  2361. pIn1++;
  2362. }
  2363. break;
  2364. }
  2365. /* Opcode: MakeRecord P1 P2 P3 P4 *
  2366. **
  2367. ** Convert P2 registers beginning with P1 into the [record format]
  2368. ** use as a data record in a database table or as a key
  2369. ** in an index. The OP_Column opcode can decode the record later.
  2370. **
  2371. ** P4 may be a string that is P2 characters long. The nth character of the
  2372. ** string indicates the column affinity that should be used for the nth
  2373. ** field of the index key.
  2374. **
  2375. ** The mapping from character to affinity is given by the SQLITE_AFF_
  2376. ** macros defined in sqliteInt.h.
  2377. **
  2378. ** If P4 is NULL then all index fields have the affinity NONE.
  2379. */
  2380. case OP_MakeRecord: {
  2381. u8 *zNewRecord; /* A buffer to hold the data for the new record */
  2382. Mem *pRec; /* The new record */
  2383. u64 nData; /* Number of bytes of data space */
  2384. int nHdr; /* Number of bytes of header space */
  2385. i64 nByte; /* Data space required for this record */
  2386. int nZero; /* Number of zero bytes at the end of the record */
  2387. int nVarint; /* Number of bytes in a varint */
  2388. u32 serial_type; /* Type field */
  2389. Mem *pData0; /* First field to be combined into the record */
  2390. Mem *pLast; /* Last field of the record */
  2391. int nField; /* Number of fields in the record */
  2392. char *zAffinity; /* The affinity string for the record */
  2393. int file_format; /* File format to use for encoding */
  2394. int i; /* Space used in zNewRecord[] */
  2395. int len; /* Length of a field */
  2396. /* Assuming the record contains N fields, the record format looks
  2397. ** like this:
  2398. **
  2399. ** ------------------------------------------------------------------------
  2400. ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
  2401. ** ------------------------------------------------------------------------
  2402. **
  2403. ** Data(0) is taken from register P1. Data(1) comes from register P1+1
  2404. ** and so froth.
  2405. **
  2406. ** Each type field is a varint representing the serial type of the
  2407. ** corresponding data element (see sqlite3VdbeSerialType()). The
  2408. ** hdr-size field is also a varint which is the offset from the beginning
  2409. ** of the record to data0.
  2410. */
  2411. nData = 0; /* Number of bytes of data space */
  2412. nHdr = 0; /* Number of bytes of header space */
  2413. nZero = 0; /* Number of zero bytes at the end of the record */
  2414. nField = pOp->p1;
  2415. zAffinity = pOp->p4.z;
  2416. assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 );
  2417. pData0 = &aMem[nField];
  2418. nField = pOp->p2;
  2419. pLast = &pData0[nField-1];
  2420. file_format = p->minWriteFileFormat;
  2421. /* Identify the output register */
  2422. assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
  2423. pOut = &aMem[pOp->p3];
  2424. memAboutToChange(p, pOut);
  2425. /* Loop through the elements that will make up the record to figure
  2426. ** out how much space is required for the new record.
  2427. */
  2428. for(pRec=pData0; pRec<=pLast; pRec++){
  2429. assert( memIsValid(pRec) );
  2430. if( zAffinity ){
  2431. applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
  2432. }
  2433. if( pRec->flags&MEM_Zero && pRec->n>0 ){
  2434. sqlite3VdbeMemExpandBlob(pRec);
  2435. }
  2436. serial_type = sqlite3VdbeSerialType(pRec, file_format);
  2437. len = sqlite3VdbeSerialTypeLen(serial_type);
  2438. nData += len;
  2439. nHdr += sqlite3VarintLen(serial_type);
  2440. if( pRec->flags & MEM_Zero ){
  2441. /* Only pure zero-filled BLOBs can be input to this Opcode.
  2442. ** We do not allow blobs with a prefix and a zero-filled tail. */
  2443. nZero += pRec->u.nZero;
  2444. }else if( len ){
  2445. nZero = 0;
  2446. }
  2447. }
  2448. /* Add the initial header varint and total the size */
  2449. nHdr += nVarint = sqlite3VarintLen(nHdr);
  2450. if( nVarint<sqlite3VarintLen(nHdr) ){
  2451. nHdr++;
  2452. }
  2453. nByte = nHdr+nData-nZero;
  2454. if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  2455. goto too_big;
  2456. }
  2457. /* Make sure the output register has a buffer large enough to store
  2458. ** the new record. The output register (pOp->p3) is not allowed to
  2459. ** be one of the input registers (because the following call to
  2460. ** sqlite3VdbeMemGrow() could clobber the value before it is used).
  2461. */
  2462. if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
  2463. goto no_mem;
  2464. }
  2465. zNewRecord = (u8 *)pOut->z;
  2466. /* Write the record */
  2467. i = putVarint32(zNewRecord, nHdr);
  2468. for(pRec=pData0; pRec<=pLast; pRec++){
  2469. serial_type = sqlite3VdbeSerialType(pRec, file_format);
  2470. i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
  2471. }
  2472. for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */
  2473. i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format);
  2474. }
  2475. assert( i==nByte );
  2476. assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  2477. pOut->n = (int)nByte;
  2478. pOut->flags = MEM_Blob | MEM_Dyn;
  2479. pOut->xDel = 0;
  2480. if( nZero ){
  2481. pOut->u.nZero = nZero;
  2482. pOut->flags |= MEM_Zero;
  2483. }
  2484. pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
  2485. REGISTER_TRACE(pOp->p3, pOut);
  2486. UPDATE_MAX_BLOBSIZE(pOut);
  2487. break;
  2488. }
  2489. /* Opcode: Count P1 P2 * * *
  2490. **
  2491. ** Store the number of entries (an integer value) in the table or index
  2492. ** opened by cursor P1 in register P2
  2493. */
  2494. #ifndef SQLITE_OMIT_BTREECOUNT
  2495. case OP_Count: { /* out2-prerelease */
  2496. i64 nEntry;
  2497. BtCursor *pCrsr;
  2498. pCrsr = p->apCsr[pOp->p1]->pCursor;
  2499. if( ALWAYS(pCrsr) ){
  2500. rc = sqlite3BtreeCount(pCrsr, &nEntry);
  2501. }else{
  2502. nEntry = 0;
  2503. }
  2504. pOut->u.i = nEntry;
  2505. break;
  2506. }
  2507. #endif
  2508. /* Opcode: Savepoint P1 * * P4 *
  2509. **
  2510. ** Open, release or rollback the savepoint named by parameter P4, depending
  2511. ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
  2512. ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
  2513. */
  2514. case OP_Savepoint: {
  2515. int p1; /* Value of P1 operand */
  2516. char *zName; /* Name of savepoint */
  2517. int nName;
  2518. Savepoint *pNew;
  2519. Savepoint *pSavepoint;
  2520. Savepoint *pTmp;
  2521. int iSavepoint;
  2522. int ii;
  2523. p1 = pOp->p1;
  2524. zName = pOp->p4.z;
  2525. /* Assert that the p1 parameter is valid. Also that if there is no open
  2526. ** transaction, then there cannot be any savepoints.
  2527. */
  2528. assert( db->pSavepoint==0 || db->autoCommit==0 );
  2529. assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
  2530. assert( db->pSavepoint || db->isTransactionSavepoint==0 );
  2531. assert( checkSavepointCount(db) );
  2532. assert( p->bIsReader );
  2533. if( p1==SAVEPOINT_BEGIN ){
  2534. if( db->nVdbeWrite>0 ){
  2535. /* A new savepoint cannot be created if there are active write
  2536. ** statements (i.e. open read/write incremental blob handles).
  2537. */
  2538. sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
  2539. "SQL statements in progress");
  2540. rc = SQLITE_BUSY;
  2541. }else{
  2542. nName = sqlite3Strlen30(zName);
  2543. #ifndef SQLITE_OMIT_VIRTUALTABLE
  2544. /* This call is Ok even if this savepoint is actually a transaction
  2545. ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
  2546. ** If this is a transaction savepoint being opened, it is guaranteed
  2547. ** that the db->aVTrans[] array is empty. */
  2548. assert( db->autoCommit==0 || db->nVTrans==0 );
  2549. rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
  2550. db->nStatement+db->nSavepoint);
  2551. if( rc!=SQLITE_OK ) goto abort_due_to_error;
  2552. #endif
  2553. /* Create a new savepoint structure. */
  2554. pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
  2555. if( pNew ){
  2556. pNew->zName = (char *)&pNew[1];
  2557. memcpy(pNew->zName, zName, nName+1);
  2558. /* If there is no open transaction, then mark this as a special
  2559. ** "transaction savepoint". */
  2560. if( db->autoCommit ){
  2561. db->autoCommit = 0;
  2562. db->isTransactionSavepoint = 1;
  2563. }else{
  2564. db->nSavepoint++;
  2565. }
  2566. /* Link the new savepoint into the database handle's list. */
  2567. pNew->pNext = db->pSavepoint;
  2568. db->pSavepoint = pNew;
  2569. pNew->nDeferredCons = db->nDeferredCons;
  2570. pNew->nDeferredImmCons = db->nDeferredImmCons;
  2571. }
  2572. }
  2573. }else{
  2574. iSavepoint = 0;
  2575. /* Find the named savepoint. If there is no such savepoint, then an
  2576. ** an error is returned to the user. */
  2577. for(
  2578. pSavepoint = db->pSavepoint;
  2579. pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
  2580. pSavepoint = pSavepoint->pNext
  2581. ){
  2582. iSavepoint++;
  2583. }
  2584. if( !pSavepoint ){
  2585. sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
  2586. rc = SQLITE_ERROR;
  2587. }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
  2588. /* It is not possible to release (commit) a savepoint if there are
  2589. ** active write statements.
  2590. */
  2591. sqlite3SetString(&p->zErrMsg, db,
  2592. "cannot release savepoint - SQL statements in progress"
  2593. );
  2594. rc = SQLITE_BUSY;
  2595. }else{
  2596. /* Determine whether or not this is a transaction savepoint. If so,
  2597. ** and this is a RELEASE command, then the current transaction
  2598. ** is committed.
  2599. */
  2600. int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
  2601. if( isTransaction && p1==SAVEPOINT_RELEASE ){
  2602. if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
  2603. goto vdbe_return;
  2604. }
  2605. db->autoCommit = 1;
  2606. if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
  2607. p->pc = pc;
  2608. db->autoCommit = 0;
  2609. p->rc = rc = SQLITE_BUSY;
  2610. goto vdbe_return;
  2611. }
  2612. db->isTransactionSavepoint = 0;
  2613. rc = p->rc;
  2614. }else{
  2615. iSavepoint = db->nSavepoint - iSavepoint - 1;
  2616. if( p1==SAVEPOINT_ROLLBACK ){
  2617. for(ii=0; ii<db->nDb; ii++){
  2618. sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT);
  2619. }
  2620. }
  2621. for(ii=0; ii<db->nDb; ii++){
  2622. rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
  2623. if( rc!=SQLITE_OK ){
  2624. goto abort_due_to_error;
  2625. }
  2626. }
  2627. if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
  2628. sqlite3ExpirePreparedStatements(db);
  2629. sqlite3ResetAllSchemasOfConnection(db);
  2630. db->flags = (db->flags | SQLITE_InternChanges);
  2631. }
  2632. }
  2633. /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
  2634. ** savepoints nested inside of the savepoint being operated on. */
  2635. while( db->pSavepoint!=pSavepoint ){
  2636. pTmp = db->pSavepoint;
  2637. db->pSavepoint = pTmp->pNext;
  2638. sqlite3DbFree(db, pTmp);
  2639. db->nSavepoint--;
  2640. }
  2641. /* If it is a RELEASE, then destroy the savepoint being operated on
  2642. ** too. If it is a ROLLBACK TO, then set the number of deferred
  2643. ** constraint violations present in the database to the value stored
  2644. ** when the savepoint was created. */
  2645. if( p1==SAVEPOINT_RELEASE ){
  2646. assert( pSavepoint==db->pSavepoint );
  2647. db->pSavepoint = pSavepoint->pNext;
  2648. sqlite3DbFree(db, pSavepoint);
  2649. if( !isTransaction ){
  2650. db->nSavepoint--;
  2651. }
  2652. }else{
  2653. db->nDeferredCons = pSavepoint->nDeferredCons;
  2654. db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
  2655. }
  2656. if( !isTransaction ){
  2657. rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
  2658. if( rc!=SQLITE_OK ) goto abort_due_to_error;
  2659. }
  2660. }
  2661. }
  2662. break;
  2663. }
  2664. /* Opcode: AutoCommit P1 P2 * * *
  2665. **
  2666. ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
  2667. ** back any currently active btree transactions. If there are any active
  2668. ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
  2669. ** there are active writing VMs or active VMs that use shared cache.
  2670. **
  2671. ** This instruction causes the VM to halt.
  2672. */
  2673. case OP_AutoCommit: {
  2674. int desiredAutoCommit;
  2675. int iRollback;
  2676. int turnOnAC;
  2677. desiredAutoCommit = pOp->p1;
  2678. iRollback = pOp->p2;
  2679. turnOnAC = desiredAutoCommit && !db->autoCommit;
  2680. assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
  2681. assert( desiredAutoCommit==1 || iRollback==0 );
  2682. assert( db->nVdbeActive>0 ); /* At least this one VM is active */
  2683. assert( p->bIsReader );
  2684. #if 0
  2685. if( turnOnAC && iRollback && db->nVdbeActive>1 ){
  2686. /* If this instruction implements a ROLLBACK and other VMs are
  2687. ** still running, and a transaction is active, return an error indicating
  2688. ** that the other VMs must complete first.
  2689. */
  2690. sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
  2691. "SQL statements in progress");
  2692. rc = SQLITE_BUSY;
  2693. }else
  2694. #endif
  2695. if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
  2696. /* If this instruction implements a COMMIT and other VMs are writing
  2697. ** return an error indicating that the other VMs must complete first.
  2698. */
  2699. sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
  2700. "SQL statements in progress");
  2701. rc = SQLITE_BUSY;
  2702. }else if( desiredAutoCommit!=db->autoCommit ){
  2703. if( iRollback ){
  2704. assert( desiredAutoCommit==1 );
  2705. sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
  2706. db->autoCommit = 1;
  2707. }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
  2708. goto vdbe_return;
  2709. }else{
  2710. db->autoCommit = (u8)desiredAutoCommit;
  2711. if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
  2712. p->pc = pc;
  2713. db->autoCommit = (u8)(1-desiredAutoCommit);
  2714. p->rc = rc = SQLITE_BUSY;
  2715. goto vdbe_return;
  2716. }
  2717. }
  2718. assert( db->nStatement==0 );
  2719. sqlite3CloseSavepoints(db);
  2720. if( p->rc==SQLITE_OK ){
  2721. rc = SQLITE_DONE;
  2722. }else{
  2723. rc = SQLITE_ERROR;
  2724. }
  2725. goto vdbe_return;
  2726. }else{
  2727. sqlite3SetString(&p->zErrMsg, db,
  2728. (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
  2729. (iRollback)?"cannot rollback - no transaction is active":
  2730. "cannot commit - no transaction is active"));
  2731. rc = SQLITE_ERROR;
  2732. }
  2733. break;
  2734. }
  2735. /* Opcode: Transaction P1 P2 * * *
  2736. **
  2737. ** Begin a transaction. The transaction ends when a Commit or Rollback
  2738. ** opcode is encountered. Depending on the ON CONFLICT setting, the
  2739. ** transaction might also be rolled back if an error is encountered.
  2740. **
  2741. ** P1 is the index of the database file on which the transaction is
  2742. ** started. Index 0 is the main database file and index 1 is the
  2743. ** file used for temporary tables. Indices of 2 or more are used for
  2744. ** attached databases.
  2745. **
  2746. ** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
  2747. ** obtained on the database file when a write-transaction is started. No
  2748. ** other process can start another write transaction while this transaction is
  2749. ** underway. Starting a write transaction also creates a rollback journal. A
  2750. ** write transaction must be started before any changes can be made to the
  2751. ** database. If P2 is greater than or equal to 2 then an EXCLUSIVE lock is
  2752. ** also obtained on the file.
  2753. **
  2754. ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
  2755. ** true (this flag is set if the Vdbe may modify more than one row and may
  2756. ** throw an ABORT exception), a statement transaction may also be opened.
  2757. ** More specifically, a statement transaction is opened iff the database
  2758. ** connection is currently not in autocommit mode, or if there are other
  2759. ** active statements. A statement transaction allows the changes made by this
  2760. ** VDBE to be rolled back after an error without having to roll back the
  2761. ** entire transaction. If no error is encountered, the statement transaction
  2762. ** will automatically commit when the VDBE halts.
  2763. **
  2764. ** If P2 is zero, then a read-lock is obtained on the database file.
  2765. */
  2766. case OP_Transaction: {
  2767. Btree *pBt;
  2768. assert( p->bIsReader );
  2769. assert( p->readOnly==0 || pOp->p2==0 );
  2770. assert( pOp->p1>=0 && pOp->p1<db->nDb );
  2771. assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  2772. if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
  2773. rc = SQLITE_READONLY;
  2774. goto abort_due_to_error;
  2775. }
  2776. pBt = db->aDb[pOp->p1].pBt;
  2777. if( pBt ){
  2778. rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
  2779. if( rc==SQLITE_BUSY ){
  2780. p->pc = pc;
  2781. p->rc = rc = SQLITE_BUSY;
  2782. goto vdbe_return;
  2783. }
  2784. if( rc!=SQLITE_OK ){
  2785. goto abort_due_to_error;
  2786. }
  2787. if( pOp->p2 && p->usesStmtJournal
  2788. && (db->autoCommit==0 || db->nVdbeRead>1)
  2789. ){
  2790. assert( sqlite3BtreeIsInTrans(pBt) );
  2791. if( p->iStatement==0 ){
  2792. assert( db->nStatement>=0 && db->nSavepoint>=0 );
  2793. db->nStatement++;
  2794. p->iStatement = db->nSavepoint + db->nStatement;
  2795. }
  2796. rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
  2797. if( rc==SQLITE_OK ){
  2798. rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
  2799. }
  2800. /* Store the current value of the database handles deferred constraint
  2801. ** counter. If the statement transaction needs to be rolled back,
  2802. ** the value of this counter needs to be restored too. */
  2803. p->nStmtDefCons = db->nDeferredCons;
  2804. p->nStmtDefImmCons = db->nDeferredImmCons;
  2805. }
  2806. }
  2807. break;
  2808. }
  2809. /* Opcode: ReadCookie P1 P2 P3 * *
  2810. **
  2811. ** Read cookie number P3 from database P1 and write it into register P2.
  2812. ** P3==1 is the schema version. P3==2 is the database format.
  2813. ** P3==3 is the recommended pager cache size, and so forth. P1==0 is
  2814. ** the main database file and P1==1 is the database file used to store
  2815. ** temporary tables.
  2816. **
  2817. ** There must be a read-lock on the database (either a transaction
  2818. ** must be started or there must be an open cursor) before
  2819. ** executing this instruction.
  2820. */
  2821. case OP_ReadCookie: { /* out2-prerelease */
  2822. int iMeta;
  2823. int iDb;
  2824. int iCookie;
  2825. assert( p->bIsReader );
  2826. iDb = pOp->p1;
  2827. iCookie = pOp->p3;
  2828. assert( pOp->p3<SQLITE_N_BTREE_META );
  2829. assert( iDb>=0 && iDb<db->nDb );
  2830. assert( db->aDb[iDb].pBt!=0 );
  2831. assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
  2832. sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
  2833. pOut->u.i = iMeta;
  2834. break;
  2835. }
  2836. /* Opcode: SetCookie P1 P2 P3 * *
  2837. **
  2838. ** Write the content of register P3 (interpreted as an integer)
  2839. ** into cookie number P2 of database P1. P2==1 is the schema version.
  2840. ** P2==2 is the database format. P2==3 is the recommended pager cache
  2841. ** size, and so forth. P1==0 is the main database file and P1==1 is the
  2842. ** database file used to store temporary tables.
  2843. **
  2844. ** A transaction must be started before executing this opcode.
  2845. */
  2846. case OP_SetCookie: { /* in3 */
  2847. Db *pDb;
  2848. assert( pOp->p2<SQLITE_N_BTREE_META );
  2849. assert( pOp->p1>=0 && pOp->p1<db->nDb );
  2850. assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  2851. assert( p->readOnly==0 );
  2852. pDb = &db->aDb[pOp->p1];
  2853. assert( pDb->pBt!=0 );
  2854. assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
  2855. pIn3 = &aMem[pOp->p3];
  2856. sqlite3VdbeMemIntegerify(pIn3);
  2857. /* See note about index shifting on OP_ReadCookie */
  2858. rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
  2859. if( pOp->p2==BTREE_SCHEMA_VERSION ){
  2860. /* When the schema cookie changes, record the new cookie internally */
  2861. pDb->pSchema->schema_cookie = (int)pIn3->u.i;
  2862. db->flags |= SQLITE_InternChanges;
  2863. }else if( pOp->p2==BTREE_FILE_FORMAT ){
  2864. /* Record changes in the file format */
  2865. pDb->pSchema->file_format = (u8)pIn3->u.i;
  2866. }
  2867. if( pOp->p1==1 ){
  2868. /* Invalidate all prepared statements whenever the TEMP database
  2869. ** schema is changed. Ticket #1644 */
  2870. sqlite3ExpirePreparedStatements(db);
  2871. p->expired = 0;
  2872. }
  2873. break;
  2874. }
  2875. /* Opcode: VerifyCookie P1 P2 P3 * *
  2876. **
  2877. ** Check the value of global database parameter number 0 (the
  2878. ** schema version) and make sure it is equal to P2 and that the
  2879. ** generation counter on the local schema parse equals P3.
  2880. **
  2881. ** P1 is the database number which is 0 for the main database file
  2882. ** and 1 for the file holding temporary tables and some higher number
  2883. ** for auxiliary databases.
  2884. **
  2885. ** The cookie changes its value whenever the database schema changes.
  2886. ** This operation is used to detect when that the cookie has changed
  2887. ** and that the current process needs to reread the schema.
  2888. **
  2889. ** Either a transaction needs to have been started or an OP_Open needs
  2890. ** to be executed (to establish a read lock) before this opcode is
  2891. ** invoked.
  2892. */
  2893. case OP_VerifyCookie: {
  2894. int iMeta;
  2895. int iGen;
  2896. Btree *pBt;
  2897. assert( pOp->p1>=0 && pOp->p1<db->nDb );
  2898. assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  2899. assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
  2900. assert( p->bIsReader );
  2901. pBt = db->aDb[pOp->p1].pBt;
  2902. if( pBt ){
  2903. sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
  2904. iGen = db->aDb[pOp->p1].pSchema->iGeneration;
  2905. }else{
  2906. iGen = iMeta = 0;
  2907. }
  2908. if( iMeta!=pOp->p2 || iGen!=pOp->p3 ){
  2909. sqlite3DbFree(db, p->zErrMsg);
  2910. p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
  2911. /* If the schema-cookie from the database file matches the cookie
  2912. ** stored with the in-memory representation of the schema, do
  2913. ** not reload the schema from the database file.
  2914. **
  2915. ** If virtual-tables are in use, this is not just an optimization.
  2916. ** Often, v-tables store their data in other SQLite tables, which
  2917. ** are queried from within xNext() and other v-table methods using
  2918. ** prepared queries. If such a query is out-of-date, we do not want to
  2919. ** discard the database schema, as the user code implementing the
  2920. ** v-table would have to be ready for the sqlite3_vtab structure itself
  2921. ** to be invalidated whenever sqlite3_step() is called from within
  2922. ** a v-table method.
  2923. */
  2924. if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
  2925. sqlite3ResetOneSchema(db, pOp->p1);
  2926. }
  2927. p->expired = 1;
  2928. rc = SQLITE_SCHEMA;
  2929. }
  2930. break;
  2931. }
  2932. /* Opcode: OpenRead P1 P2 P3 P4 P5
  2933. **
  2934. ** Open a read-only cursor for the database table whose root page is
  2935. ** P2 in a database file. The database file is determined by P3.
  2936. ** P3==0 means the main database, P3==1 means the database used for
  2937. ** temporary tables, and P3>1 means used the corresponding attached
  2938. ** database. Give the new cursor an identifier of P1. The P1
  2939. ** values need not be contiguous but all P1 values should be small integers.
  2940. ** It is an error for P1 to be negative.
  2941. **
  2942. ** If P5!=0 then use the content of register P2 as the root page, not
  2943. ** the value of P2 itself.
  2944. **
  2945. ** There will be a read lock on the database whenever there is an
  2946. ** open cursor. If the database was unlocked prior to this instruction
  2947. ** then a read lock is acquired as part of this instruction. A read
  2948. ** lock allows other processes to read the database but prohibits
  2949. ** any other process from modifying the database. The read lock is
  2950. ** released when all cursors are closed. If this instruction attempts
  2951. ** to get a read lock but fails, the script terminates with an
  2952. ** SQLITE_BUSY error code.
  2953. **
  2954. ** The P4 value may be either an integer (P4_INT32) or a pointer to
  2955. ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
  2956. ** structure, then said structure defines the content and collating
  2957. ** sequence of the index being opened. Otherwise, if P4 is an integer
  2958. ** value, it is set to the number of columns in the table.
  2959. **
  2960. ** See also OpenWrite.
  2961. */
  2962. /* Opcode: OpenWrite P1 P2 P3 P4 P5
  2963. **
  2964. ** Open a read/write cursor named P1 on the table or index whose root
  2965. ** page is P2. Or if P5!=0 use the content of register P2 to find the
  2966. ** root page.
  2967. **
  2968. ** The P4 value may be either an integer (P4_INT32) or a pointer to
  2969. ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
  2970. ** structure, then said structure defines the content and collating
  2971. ** sequence of the index being opened. Otherwise, if P4 is an integer
  2972. ** value, it is set to the number of columns in the table, or to the
  2973. ** largest index of any column of the table that is actually used.
  2974. **
  2975. ** This instruction works just like OpenRead except that it opens the cursor
  2976. ** in read/write mode. For a given table, there can be one or more read-only
  2977. ** cursors or a single read/write cursor but not both.
  2978. **
  2979. ** See also OpenRead.
  2980. */
  2981. case OP_OpenRead:
  2982. case OP_OpenWrite: {
  2983. int nField;
  2984. KeyInfo *pKeyInfo;
  2985. int p2;
  2986. int iDb;
  2987. int wrFlag;
  2988. Btree *pX;
  2989. VdbeCursor *pCur;
  2990. Db *pDb;
  2991. assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
  2992. assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
  2993. assert( p->bIsReader );
  2994. assert( pOp->opcode==OP_OpenRead || p->readOnly==0 );
  2995. if( p->expired ){
  2996. rc = SQLITE_ABORT;
  2997. break;
  2998. }
  2999. nField = 0;
  3000. pKeyInfo = 0;
  3001. p2 = pOp->p2;
  3002. iDb = pOp->p3;
  3003. assert( iDb>=0 && iDb<db->nDb );
  3004. assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
  3005. pDb = &db->aDb[iDb];
  3006. pX = pDb->pBt;
  3007. assert( pX!=0 );
  3008. if( pOp->opcode==OP_OpenWrite ){
  3009. wrFlag = 1;
  3010. assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  3011. if( pDb->pSchema->file_format < p->minWriteFileFormat ){
  3012. p->minWriteFileFormat = pDb->pSchema->file_format;
  3013. }
  3014. }else{
  3015. wrFlag = 0;
  3016. }
  3017. if( pOp->p5 & OPFLAG_P2ISREG ){
  3018. assert( p2>0 );
  3019. assert( p2<=(p->nMem-p->nCursor) );
  3020. pIn2 = &aMem[p2];
  3021. assert( memIsValid(pIn2) );
  3022. assert( (pIn2->flags & MEM_Int)!=0 );
  3023. sqlite3VdbeMemIntegerify(pIn2);
  3024. p2 = (int)pIn2->u.i;
  3025. /* The p2 value always comes from a prior OP_CreateTable opcode and
  3026. ** that opcode will always set the p2 value to 2 or more or else fail.
  3027. ** If there were a failure, the prepared statement would have halted
  3028. ** before reaching this instruction. */
  3029. if( NEVER(p2<2) ) {
  3030. rc = SQLITE_CORRUPT_BKPT;
  3031. goto abort_due_to_error;
  3032. }
  3033. }
  3034. if( pOp->p4type==P4_KEYINFO ){
  3035. pKeyInfo = pOp->p4.pKeyInfo;
  3036. pKeyInfo->enc = ENC(p->db);
  3037. nField = pKeyInfo->nField+1;
  3038. }else if( pOp->p4type==P4_INT32 ){
  3039. nField = pOp->p4.i;
  3040. }
  3041. assert( pOp->p1>=0 );
  3042. pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
  3043. if( pCur==0 ) goto no_mem;
  3044. pCur->nullRow = 1;
  3045. pCur->isOrdered = 1;
  3046. rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
  3047. pCur->pKeyInfo = pKeyInfo;
  3048. assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
  3049. sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
  3050. /* Since it performs no memory allocation or IO, the only value that
  3051. ** sqlite3BtreeCursor() may return is SQLITE_OK. */
  3052. assert( rc==SQLITE_OK );
  3053. /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
  3054. ** SQLite used to check if the root-page flags were sane at this point
  3055. ** and report database corruption if they were not, but this check has
  3056. ** since moved into the btree layer. */
  3057. pCur->isTable = pOp->p4type!=P4_KEYINFO;
  3058. pCur->isIndex = !pCur->isTable;
  3059. break;
  3060. }
  3061. /* Opcode: OpenEphemeral P1 P2 * P4 P5
  3062. **
  3063. ** Open a new cursor P1 to a transient table.
  3064. ** The cursor is always opened read/write even if
  3065. ** the main database is read-only. The ephemeral
  3066. ** table is deleted automatically when the cursor is closed.
  3067. **
  3068. ** P2 is the number of columns in the ephemeral table.
  3069. ** The cursor points to a BTree table if P4==0 and to a BTree index
  3070. ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
  3071. ** that defines the format of keys in the index.
  3072. **
  3073. ** This opcode was once called OpenTemp. But that created
  3074. ** confusion because the term "temp table", might refer either
  3075. ** to a TEMP table at the SQL level, or to a table opened by
  3076. ** this opcode. Then this opcode was call OpenVirtual. But
  3077. ** that created confusion with the whole virtual-table idea.
  3078. **
  3079. ** The P5 parameter can be a mask of the BTREE_* flags defined
  3080. ** in btree.h. These flags control aspects of the operation of
  3081. ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
  3082. ** added automatically.
  3083. */
  3084. /* Opcode: OpenAutoindex P1 P2 * P4 *
  3085. **
  3086. ** This opcode works the same as OP_OpenEphemeral. It has a
  3087. ** different name to distinguish its use. Tables created using
  3088. ** by this opcode will be used for automatically created transient
  3089. ** indices in joins.
  3090. */
  3091. case OP_OpenAutoindex:
  3092. case OP_OpenEphemeral: {
  3093. VdbeCursor *pCx;
  3094. static const int vfsFlags =
  3095. SQLITE_OPEN_READWRITE |
  3096. SQLITE_OPEN_CREATE |
  3097. SQLITE_OPEN_EXCLUSIVE |
  3098. SQLITE_OPEN_DELETEONCLOSE |
  3099. SQLITE_OPEN_TRANSIENT_DB;
  3100. assert( pOp->p1>=0 );
  3101. pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
  3102. if( pCx==0 ) goto no_mem;
  3103. pCx->nullRow = 1;
  3104. rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
  3105. BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
  3106. if( rc==SQLITE_OK ){
  3107. rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
  3108. }
  3109. if( rc==SQLITE_OK ){
  3110. /* If a transient index is required, create it by calling
  3111. ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
  3112. ** opening it. If a transient table is required, just use the
  3113. ** automatically created table with root-page 1 (an BLOB_INTKEY table).
  3114. */
  3115. if( pOp->p4.pKeyInfo ){
  3116. int pgno;
  3117. assert( pOp->p4type==P4_KEYINFO );
  3118. rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
  3119. if( rc==SQLITE_OK ){
  3120. assert( pgno==MASTER_ROOT+1 );
  3121. rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1,
  3122. (KeyInfo*)pOp->p4.z, pCx->pCursor);
  3123. pCx->pKeyInfo = pOp->p4.pKeyInfo;
  3124. pCx->pKeyInfo->enc = ENC(p->db);
  3125. }
  3126. pCx->isTable = 0;
  3127. }else{
  3128. rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
  3129. pCx->isTable = 1;
  3130. }
  3131. }
  3132. pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
  3133. pCx->isIndex = !pCx->isTable;
  3134. break;
  3135. }
  3136. /* Opcode: SorterOpen P1 P2 * P4 *
  3137. **
  3138. ** This opcode works like OP_OpenEphemeral except that it opens
  3139. ** a transient index that is specifically designed to sort large
  3140. ** tables using an external merge-sort algorithm.
  3141. */
  3142. case OP_SorterOpen: {
  3143. VdbeCursor *pCx;
  3144. pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
  3145. if( pCx==0 ) goto no_mem;
  3146. pCx->pKeyInfo = pOp->p4.pKeyInfo;
  3147. pCx->pKeyInfo->enc = ENC(p->db);
  3148. pCx->isSorter = 1;
  3149. rc = sqlite3VdbeSorterInit(db, pCx);
  3150. break;
  3151. }
  3152. /* Opcode: OpenPseudo P1 P2 P3 * P5
  3153. **
  3154. ** Open a new cursor that points to a fake table that contains a single
  3155. ** row of data. The content of that one row in the content of memory
  3156. ** register P2 when P5==0. In other words, cursor P1 becomes an alias for the
  3157. ** MEM_Blob content contained in register P2. When P5==1, then the
  3158. ** row is represented by P3 consecutive registers beginning with P2.
  3159. **
  3160. ** A pseudo-table created by this opcode is used to hold a single
  3161. ** row output from the sorter so that the row can be decomposed into
  3162. ** individual columns using the OP_Column opcode. The OP_Column opcode
  3163. ** is the only cursor opcode that works with a pseudo-table.
  3164. **
  3165. ** P3 is the number of fields in the records that will be stored by
  3166. ** the pseudo-table.
  3167. */
  3168. case OP_OpenPseudo: {
  3169. VdbeCursor *pCx;
  3170. assert( pOp->p1>=0 );
  3171. pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
  3172. if( pCx==0 ) goto no_mem;
  3173. pCx->nullRow = 1;
  3174. pCx->pseudoTableReg = pOp->p2;
  3175. pCx->isTable = 1;
  3176. pCx->isIndex = 0;
  3177. pCx->multiPseudo = pOp->p5;
  3178. break;
  3179. }
  3180. /* Opcode: Close P1 * * * *
  3181. **
  3182. ** Close a cursor previously opened as P1. If P1 is not
  3183. ** currently open, this instruction is a no-op.
  3184. */
  3185. case OP_Close: {
  3186. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3187. sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
  3188. p->apCsr[pOp->p1] = 0;
  3189. break;
  3190. }
  3191. /* Opcode: SeekGe P1 P2 P3 P4 *
  3192. **
  3193. ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
  3194. ** use the value in register P3 as the key. If cursor P1 refers
  3195. ** to an SQL index, then P3 is the first in an array of P4 registers
  3196. ** that are used as an unpacked index key.
  3197. **
  3198. ** Reposition cursor P1 so that it points to the smallest entry that
  3199. ** is greater than or equal to the key value. If there are no records
  3200. ** greater than or equal to the key and P2 is not zero, then jump to P2.
  3201. **
  3202. ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
  3203. */
  3204. /* Opcode: SeekGt P1 P2 P3 P4 *
  3205. **
  3206. ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
  3207. ** use the value in register P3 as a key. If cursor P1 refers
  3208. ** to an SQL index, then P3 is the first in an array of P4 registers
  3209. ** that are used as an unpacked index key.
  3210. **
  3211. ** Reposition cursor P1 so that it points to the smallest entry that
  3212. ** is greater than the key value. If there are no records greater than
  3213. ** the key and P2 is not zero, then jump to P2.
  3214. **
  3215. ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
  3216. */
  3217. /* Opcode: SeekLt P1 P2 P3 P4 *
  3218. **
  3219. ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
  3220. ** use the value in register P3 as a key. If cursor P1 refers
  3221. ** to an SQL index, then P3 is the first in an array of P4 registers
  3222. ** that are used as an unpacked index key.
  3223. **
  3224. ** Reposition cursor P1 so that it points to the largest entry that
  3225. ** is less than the key value. If there are no records less than
  3226. ** the key and P2 is not zero, then jump to P2.
  3227. **
  3228. ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
  3229. */
  3230. /* Opcode: SeekLe P1 P2 P3 P4 *
  3231. **
  3232. ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
  3233. ** use the value in register P3 as a key. If cursor P1 refers
  3234. ** to an SQL index, then P3 is the first in an array of P4 registers
  3235. ** that are used as an unpacked index key.
  3236. **
  3237. ** Reposition cursor P1 so that it points to the largest entry that
  3238. ** is less than or equal to the key value. If there are no records
  3239. ** less than or equal to the key and P2 is not zero, then jump to P2.
  3240. **
  3241. ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
  3242. */
  3243. case OP_SeekLt: /* jump, in3 */
  3244. case OP_SeekLe: /* jump, in3 */
  3245. case OP_SeekGe: /* jump, in3 */
  3246. case OP_SeekGt: { /* jump, in3 */
  3247. int res;
  3248. int oc;
  3249. VdbeCursor *pC;
  3250. UnpackedRecord r;
  3251. int nField;
  3252. i64 iKey; /* The rowid we are to seek to */
  3253. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3254. assert( pOp->p2!=0 );
  3255. pC = p->apCsr[pOp->p1];
  3256. assert( pC!=0 );
  3257. assert( pC->pseudoTableReg==0 );
  3258. assert( OP_SeekLe == OP_SeekLt+1 );
  3259. assert( OP_SeekGe == OP_SeekLt+2 );
  3260. assert( OP_SeekGt == OP_SeekLt+3 );
  3261. assert( pC->isOrdered );
  3262. if( ALWAYS(pC->pCursor!=0) ){
  3263. oc = pOp->opcode;
  3264. pC->nullRow = 0;
  3265. if( pC->isTable ){
  3266. /* The input value in P3 might be of any type: integer, real, string,
  3267. ** blob, or NULL. But it needs to be an integer before we can do
  3268. ** the seek, so covert it. */
  3269. pIn3 = &aMem[pOp->p3];
  3270. applyNumericAffinity(pIn3);
  3271. iKey = sqlite3VdbeIntValue(pIn3);
  3272. pC->rowidIsValid = 0;
  3273. /* If the P3 value could not be converted into an integer without
  3274. ** loss of information, then special processing is required... */
  3275. if( (pIn3->flags & MEM_Int)==0 ){
  3276. if( (pIn3->flags & MEM_Real)==0 ){
  3277. /* If the P3 value cannot be converted into any kind of a number,
  3278. ** then the seek is not possible, so jump to P2 */
  3279. pc = pOp->p2 - 1;
  3280. break;
  3281. }
  3282. /* If we reach this point, then the P3 value must be a floating
  3283. ** point number. */
  3284. assert( (pIn3->flags & MEM_Real)!=0 );
  3285. if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
  3286. /* The P3 value is too large in magnitude to be expressed as an
  3287. ** integer. */
  3288. res = 1;
  3289. if( pIn3->r<0 ){
  3290. if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
  3291. rc = sqlite3BtreeFirst(pC->pCursor, &res);
  3292. if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3293. }
  3294. }else{
  3295. if( oc<=OP_SeekLe ){ assert( oc==OP_SeekLt || oc==OP_SeekLe );
  3296. rc = sqlite3BtreeLast(pC->pCursor, &res);
  3297. if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3298. }
  3299. }
  3300. if( res ){
  3301. pc = pOp->p2 - 1;
  3302. }
  3303. break;
  3304. }else if( oc==OP_SeekLt || oc==OP_SeekGe ){
  3305. /* Use the ceiling() function to convert real->int */
  3306. if( pIn3->r > (double)iKey ) iKey++;
  3307. }else{
  3308. /* Use the floor() function to convert real->int */
  3309. assert( oc==OP_SeekLe || oc==OP_SeekGt );
  3310. if( pIn3->r < (double)iKey ) iKey--;
  3311. }
  3312. }
  3313. rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
  3314. if( rc!=SQLITE_OK ){
  3315. goto abort_due_to_error;
  3316. }
  3317. if( res==0 ){
  3318. pC->rowidIsValid = 1;
  3319. pC->lastRowid = iKey;
  3320. }
  3321. }else{
  3322. nField = pOp->p4.i;
  3323. assert( pOp->p4type==P4_INT32 );
  3324. assert( nField>0 );
  3325. r.pKeyInfo = pC->pKeyInfo;
  3326. r.nField = (u16)nField;
  3327. /* The next line of code computes as follows, only faster:
  3328. ** if( oc==OP_SeekGt || oc==OP_SeekLe ){
  3329. ** r.flags = UNPACKED_INCRKEY;
  3330. ** }else{
  3331. ** r.flags = 0;
  3332. ** }
  3333. */
  3334. r.flags = (u8)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt)));
  3335. assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY );
  3336. assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY );
  3337. assert( oc!=OP_SeekGe || r.flags==0 );
  3338. assert( oc!=OP_SeekLt || r.flags==0 );
  3339. r.aMem = &aMem[pOp->p3];
  3340. #ifdef SQLITE_DEBUG
  3341. { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  3342. #endif
  3343. ExpandBlob(r.aMem);
  3344. rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
  3345. if( rc!=SQLITE_OK ){
  3346. goto abort_due_to_error;
  3347. }
  3348. pC->rowidIsValid = 0;
  3349. }
  3350. pC->deferredMoveto = 0;
  3351. pC->cacheStatus = CACHE_STALE;
  3352. #ifdef SQLITE_TEST
  3353. sqlite3_search_count++;
  3354. #endif
  3355. if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
  3356. if( res<0 || (res==0 && oc==OP_SeekGt) ){
  3357. rc = sqlite3BtreeNext(pC->pCursor, &res);
  3358. if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3359. pC->rowidIsValid = 0;
  3360. }else{
  3361. res = 0;
  3362. }
  3363. }else{
  3364. assert( oc==OP_SeekLt || oc==OP_SeekLe );
  3365. if( res>0 || (res==0 && oc==OP_SeekLt) ){
  3366. rc = sqlite3BtreePrevious(pC->pCursor, &res);
  3367. if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3368. pC->rowidIsValid = 0;
  3369. }else{
  3370. /* res might be negative because the table is empty. Check to
  3371. ** see if this is the case.
  3372. */
  3373. res = sqlite3BtreeEof(pC->pCursor);
  3374. }
  3375. }
  3376. assert( pOp->p2>0 );
  3377. if( res ){
  3378. pc = pOp->p2 - 1;
  3379. }
  3380. }else{
  3381. /* This happens when attempting to open the sqlite3_master table
  3382. ** for read access returns SQLITE_EMPTY. In this case always
  3383. ** take the jump (since there are no records in the table).
  3384. */
  3385. pc = pOp->p2 - 1;
  3386. }
  3387. break;
  3388. }
  3389. /* Opcode: Seek P1 P2 * * *
  3390. **
  3391. ** P1 is an open table cursor and P2 is a rowid integer. Arrange
  3392. ** for P1 to move so that it points to the rowid given by P2.
  3393. **
  3394. ** This is actually a deferred seek. Nothing actually happens until
  3395. ** the cursor is used to read a record. That way, if no reads
  3396. ** occur, no unnecessary I/O happens.
  3397. */
  3398. case OP_Seek: { /* in2 */
  3399. VdbeCursor *pC;
  3400. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3401. pC = p->apCsr[pOp->p1];
  3402. assert( pC!=0 );
  3403. if( ALWAYS(pC->pCursor!=0) ){
  3404. assert( pC->isTable );
  3405. pC->nullRow = 0;
  3406. pIn2 = &aMem[pOp->p2];
  3407. pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
  3408. pC->rowidIsValid = 0;
  3409. pC->deferredMoveto = 1;
  3410. }
  3411. break;
  3412. }
  3413. /* Opcode: Found P1 P2 P3 P4 *
  3414. **
  3415. ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
  3416. ** P4>0 then register P3 is the first of P4 registers that form an unpacked
  3417. ** record.
  3418. **
  3419. ** Cursor P1 is on an index btree. If the record identified by P3 and P4
  3420. ** is a prefix of any entry in P1 then a jump is made to P2 and
  3421. ** P1 is left pointing at the matching entry.
  3422. */
  3423. /* Opcode: NotFound P1 P2 P3 P4 *
  3424. **
  3425. ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
  3426. ** P4>0 then register P3 is the first of P4 registers that form an unpacked
  3427. ** record.
  3428. **
  3429. ** Cursor P1 is on an index btree. If the record identified by P3 and P4
  3430. ** is not the prefix of any entry in P1 then a jump is made to P2. If P1
  3431. ** does contain an entry whose prefix matches the P3/P4 record then control
  3432. ** falls through to the next instruction and P1 is left pointing at the
  3433. ** matching entry.
  3434. **
  3435. ** See also: Found, NotExists, IsUnique
  3436. */
  3437. case OP_NotFound: /* jump, in3 */
  3438. case OP_Found: { /* jump, in3 */
  3439. int alreadyExists;
  3440. VdbeCursor *pC;
  3441. int res;
  3442. char *pFree;
  3443. UnpackedRecord *pIdxKey;
  3444. UnpackedRecord r;
  3445. char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
  3446. #ifdef SQLITE_TEST
  3447. sqlite3_found_count++;
  3448. #endif
  3449. alreadyExists = 0;
  3450. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3451. assert( pOp->p4type==P4_INT32 );
  3452. pC = p->apCsr[pOp->p1];
  3453. assert( pC!=0 );
  3454. pIn3 = &aMem[pOp->p3];
  3455. if( ALWAYS(pC->pCursor!=0) ){
  3456. assert( pC->isTable==0 );
  3457. if( pOp->p4.i>0 ){
  3458. r.pKeyInfo = pC->pKeyInfo;
  3459. r.nField = (u16)pOp->p4.i;
  3460. r.aMem = pIn3;
  3461. #ifdef SQLITE_DEBUG
  3462. { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  3463. #endif
  3464. r.flags = UNPACKED_PREFIX_MATCH;
  3465. pIdxKey = &r;
  3466. }else{
  3467. pIdxKey = sqlite3VdbeAllocUnpackedRecord(
  3468. pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
  3469. );
  3470. if( pIdxKey==0 ) goto no_mem;
  3471. assert( pIn3->flags & MEM_Blob );
  3472. assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
  3473. sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
  3474. pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
  3475. }
  3476. rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
  3477. if( pOp->p4.i==0 ){
  3478. sqlite3DbFree(db, pFree);
  3479. }
  3480. if( rc!=SQLITE_OK ){
  3481. break;
  3482. }
  3483. alreadyExists = (res==0);
  3484. pC->deferredMoveto = 0;
  3485. pC->cacheStatus = CACHE_STALE;
  3486. }
  3487. if( pOp->opcode==OP_Found ){
  3488. if( alreadyExists ) pc = pOp->p2 - 1;
  3489. }else{
  3490. if( !alreadyExists ) pc = pOp->p2 - 1;
  3491. }
  3492. break;
  3493. }
  3494. /* Opcode: IsUnique P1 P2 P3 P4 *
  3495. **
  3496. ** Cursor P1 is open on an index b-tree - that is to say, a btree which
  3497. ** no data and where the key are records generated by OP_MakeRecord with
  3498. ** the list field being the integer ROWID of the entry that the index
  3499. ** entry refers to.
  3500. **
  3501. ** The P3 register contains an integer record number. Call this record
  3502. ** number R. Register P4 is the first in a set of N contiguous registers
  3503. ** that make up an unpacked index key that can be used with cursor P1.
  3504. ** The value of N can be inferred from the cursor. N includes the rowid
  3505. ** value appended to the end of the index record. This rowid value may
  3506. ** or may not be the same as R.
  3507. **
  3508. ** If any of the N registers beginning with register P4 contains a NULL
  3509. ** value, jump immediately to P2.
  3510. **
  3511. ** Otherwise, this instruction checks if cursor P1 contains an entry
  3512. ** where the first (N-1) fields match but the rowid value at the end
  3513. ** of the index entry is not R. If there is no such entry, control jumps
  3514. ** to instruction P2. Otherwise, the rowid of the conflicting index
  3515. ** entry is copied to register P3 and control falls through to the next
  3516. ** instruction.
  3517. **
  3518. ** See also: NotFound, NotExists, Found
  3519. */
  3520. case OP_IsUnique: { /* jump, in3 */
  3521. u16 ii;
  3522. VdbeCursor *pCx;
  3523. BtCursor *pCrsr;
  3524. u16 nField;
  3525. Mem *aMx;
  3526. UnpackedRecord r; /* B-Tree index search key */
  3527. i64 R; /* Rowid stored in register P3 */
  3528. pIn3 = &aMem[pOp->p3];
  3529. aMx = &aMem[pOp->p4.i];
  3530. /* Assert that the values of parameters P1 and P4 are in range. */
  3531. assert( pOp->p4type==P4_INT32 );
  3532. assert( pOp->p4.i>0 && pOp->p4.i<=(p->nMem-p->nCursor) );
  3533. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3534. /* Find the index cursor. */
  3535. pCx = p->apCsr[pOp->p1];
  3536. assert( pCx->deferredMoveto==0 );
  3537. pCx->seekResult = 0;
  3538. pCx->cacheStatus = CACHE_STALE;
  3539. pCrsr = pCx->pCursor;
  3540. /* If any of the values are NULL, take the jump. */
  3541. nField = pCx->pKeyInfo->nField;
  3542. for(ii=0; ii<nField; ii++){
  3543. if( aMx[ii].flags & MEM_Null ){
  3544. pc = pOp->p2 - 1;
  3545. pCrsr = 0;
  3546. break;
  3547. }
  3548. }
  3549. assert( (aMx[nField].flags & MEM_Null)==0 );
  3550. if( pCrsr!=0 ){
  3551. /* Populate the index search key. */
  3552. r.pKeyInfo = pCx->pKeyInfo;
  3553. r.nField = nField + 1;
  3554. r.flags = UNPACKED_PREFIX_SEARCH;
  3555. r.aMem = aMx;
  3556. #ifdef SQLITE_DEBUG
  3557. { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  3558. #endif
  3559. /* Extract the value of R from register P3. */
  3560. sqlite3VdbeMemIntegerify(pIn3);
  3561. R = pIn3->u.i;
  3562. /* Search the B-Tree index. If no conflicting record is found, jump
  3563. ** to P2. Otherwise, copy the rowid of the conflicting record to
  3564. ** register P3 and fall through to the next instruction. */
  3565. rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &pCx->seekResult);
  3566. if( (r.flags & UNPACKED_PREFIX_SEARCH) || r.rowid==R ){
  3567. pc = pOp->p2 - 1;
  3568. }else{
  3569. pIn3->u.i = r.rowid;
  3570. }
  3571. }
  3572. break;
  3573. }
  3574. /* Opcode: NotExists P1 P2 P3 * *
  3575. **
  3576. ** Use the content of register P3 as an integer key. If a record
  3577. ** with that key does not exist in table of P1, then jump to P2.
  3578. ** If the record does exist, then fall through. The cursor is left
  3579. ** pointing to the record if it exists.
  3580. **
  3581. ** The difference between this operation and NotFound is that this
  3582. ** operation assumes the key is an integer and that P1 is a table whereas
  3583. ** NotFound assumes key is a blob constructed from MakeRecord and
  3584. ** P1 is an index.
  3585. **
  3586. ** See also: Found, NotFound, IsUnique
  3587. */
  3588. case OP_NotExists: { /* jump, in3 */
  3589. VdbeCursor *pC;
  3590. BtCursor *pCrsr;
  3591. int res;
  3592. u64 iKey;
  3593. pIn3 = &aMem[pOp->p3];
  3594. assert( pIn3->flags & MEM_Int );
  3595. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3596. pC = p->apCsr[pOp->p1];
  3597. assert( pC!=0 );
  3598. assert( pC->isTable );
  3599. assert( pC->pseudoTableReg==0 );
  3600. pCrsr = pC->pCursor;
  3601. if( ALWAYS(pCrsr!=0) ){
  3602. res = 0;
  3603. iKey = pIn3->u.i;
  3604. rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
  3605. pC->lastRowid = pIn3->u.i;
  3606. pC->rowidIsValid = res==0 ?1:0;
  3607. pC->nullRow = 0;
  3608. pC->cacheStatus = CACHE_STALE;
  3609. pC->deferredMoveto = 0;
  3610. if( res!=0 ){
  3611. pc = pOp->p2 - 1;
  3612. assert( pC->rowidIsValid==0 );
  3613. }
  3614. pC->seekResult = res;
  3615. }else{
  3616. /* This happens when an attempt to open a read cursor on the
  3617. ** sqlite_master table returns SQLITE_EMPTY.
  3618. */
  3619. pc = pOp->p2 - 1;
  3620. assert( pC->rowidIsValid==0 );
  3621. pC->seekResult = 0;
  3622. }
  3623. break;
  3624. }
  3625. /* Opcode: Sequence P1 P2 * * *
  3626. **
  3627. ** Find the next available sequence number for cursor P1.
  3628. ** Write the sequence number into register P2.
  3629. ** The sequence number on the cursor is incremented after this
  3630. ** instruction.
  3631. */
  3632. case OP_Sequence: { /* out2-prerelease */
  3633. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3634. assert( p->apCsr[pOp->p1]!=0 );
  3635. pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
  3636. break;
  3637. }
  3638. /* Opcode: NewRowid P1 P2 P3 * *
  3639. **
  3640. ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
  3641. ** The record number is not previously used as a key in the database
  3642. ** table that cursor P1 points to. The new record number is written
  3643. ** written to register P2.
  3644. **
  3645. ** If P3>0 then P3 is a register in the root frame of this VDBE that holds
  3646. ** the largest previously generated record number. No new record numbers are
  3647. ** allowed to be less than this value. When this value reaches its maximum,
  3648. ** an SQLITE_FULL error is generated. The P3 register is updated with the '
  3649. ** generated record number. This P3 mechanism is used to help implement the
  3650. ** AUTOINCREMENT feature.
  3651. */
  3652. case OP_NewRowid: { /* out2-prerelease */
  3653. i64 v; /* The new rowid */
  3654. VdbeCursor *pC; /* Cursor of table to get the new rowid */
  3655. int res; /* Result of an sqlite3BtreeLast() */
  3656. int cnt; /* Counter to limit the number of searches */
  3657. Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
  3658. VdbeFrame *pFrame; /* Root frame of VDBE */
  3659. v = 0;
  3660. res = 0;
  3661. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3662. pC = p->apCsr[pOp->p1];
  3663. assert( pC!=0 );
  3664. if( NEVER(pC->pCursor==0) ){
  3665. /* The zero initialization above is all that is needed */
  3666. }else{
  3667. /* The next rowid or record number (different terms for the same
  3668. ** thing) is obtained in a two-step algorithm.
  3669. **
  3670. ** First we attempt to find the largest existing rowid and add one
  3671. ** to that. But if the largest existing rowid is already the maximum
  3672. ** positive integer, we have to fall through to the second
  3673. ** probabilistic algorithm
  3674. **
  3675. ** The second algorithm is to select a rowid at random and see if
  3676. ** it already exists in the table. If it does not exist, we have
  3677. ** succeeded. If the random rowid does exist, we select a new one
  3678. ** and try again, up to 100 times.
  3679. */
  3680. assert( pC->isTable );
  3681. #ifdef SQLITE_32BIT_ROWID
  3682. # define MAX_ROWID 0x7fffffff
  3683. #else
  3684. /* Some compilers complain about constants of the form 0x7fffffffffffffff.
  3685. ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
  3686. ** to provide the constant while making all compilers happy.
  3687. */
  3688. # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
  3689. #endif
  3690. if( !pC->useRandomRowid ){
  3691. v = sqlite3BtreeGetCachedRowid(pC->pCursor);
  3692. if( v==0 ){
  3693. rc = sqlite3BtreeLast(pC->pCursor, &res);
  3694. if( rc!=SQLITE_OK ){
  3695. goto abort_due_to_error;
  3696. }
  3697. if( res ){
  3698. v = 1; /* IMP: R-61914-48074 */
  3699. }else{
  3700. assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
  3701. rc = sqlite3BtreeKeySize(pC->pCursor, &v);
  3702. assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
  3703. if( v>=MAX_ROWID ){
  3704. pC->useRandomRowid = 1;
  3705. }else{
  3706. v++; /* IMP: R-29538-34987 */
  3707. }
  3708. }
  3709. }
  3710. #ifndef SQLITE_OMIT_AUTOINCREMENT
  3711. if( pOp->p3 ){
  3712. /* Assert that P3 is a valid memory cell. */
  3713. assert( pOp->p3>0 );
  3714. if( p->pFrame ){
  3715. for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
  3716. /* Assert that P3 is a valid memory cell. */
  3717. assert( pOp->p3<=pFrame->nMem );
  3718. pMem = &pFrame->aMem[pOp->p3];
  3719. }else{
  3720. /* Assert that P3 is a valid memory cell. */
  3721. assert( pOp->p3<=(p->nMem-p->nCursor) );
  3722. pMem = &aMem[pOp->p3];
  3723. memAboutToChange(p, pMem);
  3724. }
  3725. assert( memIsValid(pMem) );
  3726. REGISTER_TRACE(pOp->p3, pMem);
  3727. sqlite3VdbeMemIntegerify(pMem);
  3728. assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
  3729. if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
  3730. rc = SQLITE_FULL; /* IMP: R-12275-61338 */
  3731. goto abort_due_to_error;
  3732. }
  3733. if( v<pMem->u.i+1 ){
  3734. v = pMem->u.i + 1;
  3735. }
  3736. pMem->u.i = v;
  3737. }
  3738. #endif
  3739. sqlite3BtreeSetCachedRowid(pC->pCursor, v<MAX_ROWID ? v+1 : 0);
  3740. }
  3741. if( pC->useRandomRowid ){
  3742. /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
  3743. ** largest possible integer (9223372036854775807) then the database
  3744. ** engine starts picking positive candidate ROWIDs at random until
  3745. ** it finds one that is not previously used. */
  3746. assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
  3747. ** an AUTOINCREMENT table. */
  3748. /* on the first attempt, simply do one more than previous */
  3749. v = lastRowid;
  3750. v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
  3751. v++; /* ensure non-zero */
  3752. cnt = 0;
  3753. while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
  3754. 0, &res))==SQLITE_OK)
  3755. && (res==0)
  3756. && (++cnt<100)){
  3757. /* collision - try another random rowid */
  3758. sqlite3_randomness(sizeof(v), &v);
  3759. if( cnt<5 ){
  3760. /* try "small" random rowids for the initial attempts */
  3761. v &= 0xffffff;
  3762. }else{
  3763. v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
  3764. }
  3765. v++; /* ensure non-zero */
  3766. }
  3767. if( rc==SQLITE_OK && res==0 ){
  3768. rc = SQLITE_FULL; /* IMP: R-38219-53002 */
  3769. goto abort_due_to_error;
  3770. }
  3771. assert( v>0 ); /* EV: R-40812-03570 */
  3772. }
  3773. pC->rowidIsValid = 0;
  3774. pC->deferredMoveto = 0;
  3775. pC->cacheStatus = CACHE_STALE;
  3776. }
  3777. pOut->u.i = v;
  3778. break;
  3779. }
  3780. /* Opcode: Insert P1 P2 P3 P4 P5
  3781. **
  3782. ** Write an entry into the table of cursor P1. A new entry is
  3783. ** created if it doesn't already exist or the data for an existing
  3784. ** entry is overwritten. The data is the value MEM_Blob stored in register
  3785. ** number P2. The key is stored in register P3. The key must
  3786. ** be a MEM_Int.
  3787. **
  3788. ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
  3789. ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
  3790. ** then rowid is stored for subsequent return by the
  3791. ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
  3792. **
  3793. ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
  3794. ** the last seek operation (OP_NotExists) was a success, then this
  3795. ** operation will not attempt to find the appropriate row before doing
  3796. ** the insert but will instead overwrite the row that the cursor is
  3797. ** currently pointing to. Presumably, the prior OP_NotExists opcode
  3798. ** has already positioned the cursor correctly. This is an optimization
  3799. ** that boosts performance by avoiding redundant seeks.
  3800. **
  3801. ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
  3802. ** UPDATE operation. Otherwise (if the flag is clear) then this opcode
  3803. ** is part of an INSERT operation. The difference is only important to
  3804. ** the update hook.
  3805. **
  3806. ** Parameter P4 may point to a string containing the table-name, or
  3807. ** may be NULL. If it is not NULL, then the update-hook
  3808. ** (sqlite3.xUpdateCallback) is invoked following a successful insert.
  3809. **
  3810. ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
  3811. ** allocated, then ownership of P2 is transferred to the pseudo-cursor
  3812. ** and register P2 becomes ephemeral. If the cursor is changed, the
  3813. ** value of register P2 will then change. Make sure this does not
  3814. ** cause any problems.)
  3815. **
  3816. ** This instruction only works on tables. The equivalent instruction
  3817. ** for indices is OP_IdxInsert.
  3818. */
  3819. /* Opcode: InsertInt P1 P2 P3 P4 P5
  3820. **
  3821. ** This works exactly like OP_Insert except that the key is the
  3822. ** integer value P3, not the value of the integer stored in register P3.
  3823. */
  3824. case OP_Insert:
  3825. case OP_InsertInt: {
  3826. Mem *pData; /* MEM cell holding data for the record to be inserted */
  3827. Mem *pKey; /* MEM cell holding key for the record */
  3828. i64 iKey; /* The integer ROWID or key for the record to be inserted */
  3829. VdbeCursor *pC; /* Cursor to table into which insert is written */
  3830. int nZero; /* Number of zero-bytes to append */
  3831. int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
  3832. const char *zDb; /* database name - used by the update hook */
  3833. const char *zTbl; /* Table name - used by the opdate hook */
  3834. int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
  3835. pData = &aMem[pOp->p2];
  3836. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3837. assert( memIsValid(pData) );
  3838. pC = p->apCsr[pOp->p1];
  3839. assert( pC!=0 );
  3840. assert( pC->pCursor!=0 );
  3841. assert( pC->pseudoTableReg==0 );
  3842. assert( pC->isTable );
  3843. REGISTER_TRACE(pOp->p2, pData);
  3844. if( pOp->opcode==OP_Insert ){
  3845. pKey = &aMem[pOp->p3];
  3846. assert( pKey->flags & MEM_Int );
  3847. assert( memIsValid(pKey) );
  3848. REGISTER_TRACE(pOp->p3, pKey);
  3849. iKey = pKey->u.i;
  3850. }else{
  3851. assert( pOp->opcode==OP_InsertInt );
  3852. iKey = pOp->p3;
  3853. }
  3854. if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
  3855. if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
  3856. if( pData->flags & MEM_Null ){
  3857. pData->z = 0;
  3858. pData->n = 0;
  3859. }else{
  3860. assert( pData->flags & (MEM_Blob|MEM_Str) );
  3861. }
  3862. seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
  3863. if( pData->flags & MEM_Zero ){
  3864. nZero = pData->u.nZero;
  3865. }else{
  3866. nZero = 0;
  3867. }
  3868. sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
  3869. rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
  3870. pData->z, pData->n, nZero,
  3871. pOp->p5 & OPFLAG_APPEND, seekResult
  3872. );
  3873. pC->rowidIsValid = 0;
  3874. pC->deferredMoveto = 0;
  3875. pC->cacheStatus = CACHE_STALE;
  3876. /* Invoke the update-hook if required. */
  3877. if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
  3878. zDb = db->aDb[pC->iDb].zName;
  3879. zTbl = pOp->p4.z;
  3880. op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
  3881. assert( pC->isTable );
  3882. db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
  3883. assert( pC->iDb>=0 );
  3884. }
  3885. break;
  3886. }
  3887. /* Opcode: Delete P1 P2 * P4 *
  3888. **
  3889. ** Delete the record at which the P1 cursor is currently pointing.
  3890. **
  3891. ** The cursor will be left pointing at either the next or the previous
  3892. ** record in the table. If it is left pointing at the next record, then
  3893. ** the next Next instruction will be a no-op. Hence it is OK to delete
  3894. ** a record from within an Next loop.
  3895. **
  3896. ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
  3897. ** incremented (otherwise not).
  3898. **
  3899. ** P1 must not be pseudo-table. It has to be a real table with
  3900. ** multiple rows.
  3901. **
  3902. ** If P4 is not NULL, then it is the name of the table that P1 is
  3903. ** pointing to. The update hook will be invoked, if it exists.
  3904. ** If P4 is not NULL then the P1 cursor must have been positioned
  3905. ** using OP_NotFound prior to invoking this opcode.
  3906. */
  3907. case OP_Delete: {
  3908. i64 iKey;
  3909. VdbeCursor *pC;
  3910. iKey = 0;
  3911. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3912. pC = p->apCsr[pOp->p1];
  3913. assert( pC!=0 );
  3914. assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
  3915. /* If the update-hook will be invoked, set iKey to the rowid of the
  3916. ** row being deleted.
  3917. */
  3918. if( db->xUpdateCallback && pOp->p4.z ){
  3919. assert( pC->isTable );
  3920. assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
  3921. iKey = pC->lastRowid;
  3922. }
  3923. /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
  3924. ** OP_Column on the same table without any intervening operations that
  3925. ** might move or invalidate the cursor. Hence cursor pC is always pointing
  3926. ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
  3927. ** below is always a no-op and cannot fail. We will run it anyhow, though,
  3928. ** to guard against future changes to the code generator.
  3929. **/
  3930. assert( pC->deferredMoveto==0 );
  3931. rc = sqlite3VdbeCursorMoveto(pC);
  3932. if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
  3933. sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
  3934. rc = sqlite3BtreeDelete(pC->pCursor);
  3935. pC->cacheStatus = CACHE_STALE;
  3936. /* Invoke the update-hook if required. */
  3937. if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
  3938. const char *zDb = db->aDb[pC->iDb].zName;
  3939. const char *zTbl = pOp->p4.z;
  3940. db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
  3941. assert( pC->iDb>=0 );
  3942. }
  3943. if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
  3944. break;
  3945. }
  3946. /* Opcode: ResetCount * * * * *
  3947. **
  3948. ** The value of the change counter is copied to the database handle
  3949. ** change counter (returned by subsequent calls to sqlite3_changes()).
  3950. ** Then the VMs internal change counter resets to 0.
  3951. ** This is used by trigger programs.
  3952. */
  3953. case OP_ResetCount: {
  3954. sqlite3VdbeSetChanges(db, p->nChange);
  3955. p->nChange = 0;
  3956. break;
  3957. }
  3958. /* Opcode: SorterCompare P1 P2 P3
  3959. **
  3960. ** P1 is a sorter cursor. This instruction compares the record blob in
  3961. ** register P3 with the entry that the sorter cursor currently points to.
  3962. ** If, excluding the rowid fields at the end, the two records are a match,
  3963. ** fall through to the next instruction. Otherwise, jump to instruction P2.
  3964. */
  3965. case OP_SorterCompare: {
  3966. VdbeCursor *pC;
  3967. int res;
  3968. pC = p->apCsr[pOp->p1];
  3969. assert( isSorter(pC) );
  3970. pIn3 = &aMem[pOp->p3];
  3971. rc = sqlite3VdbeSorterCompare(pC, pIn3, &res);
  3972. if( res ){
  3973. pc = pOp->p2-1;
  3974. }
  3975. break;
  3976. };
  3977. /* Opcode: SorterData P1 P2 * * *
  3978. **
  3979. ** Write into register P2 the current sorter data for sorter cursor P1.
  3980. */
  3981. case OP_SorterData: {
  3982. VdbeCursor *pC;
  3983. pOut = &aMem[pOp->p2];
  3984. pC = p->apCsr[pOp->p1];
  3985. assert( pC->isSorter );
  3986. rc = sqlite3VdbeSorterRowkey(pC, pOut);
  3987. break;
  3988. }
  3989. /* Opcode: RowData P1 P2 * * *
  3990. **
  3991. ** Write into register P2 the complete row data for cursor P1.
  3992. ** There is no interpretation of the data.
  3993. ** It is just copied onto the P2 register exactly as
  3994. ** it is found in the database file.
  3995. **
  3996. ** If the P1 cursor must be pointing to a valid row (not a NULL row)
  3997. ** of a real table, not a pseudo-table.
  3998. */
  3999. /* Opcode: RowKey P1 P2 * * *
  4000. **
  4001. ** Write into register P2 the complete row key for cursor P1.
  4002. ** There is no interpretation of the data.
  4003. ** The key is copied onto the P3 register exactly as
  4004. ** it is found in the database file.
  4005. **
  4006. ** If the P1 cursor must be pointing to a valid row (not a NULL row)
  4007. ** of a real table, not a pseudo-table.
  4008. */
  4009. case OP_RowKey:
  4010. case OP_RowData: {
  4011. VdbeCursor *pC;
  4012. BtCursor *pCrsr;
  4013. u32 n;
  4014. i64 n64;
  4015. pOut = &aMem[pOp->p2];
  4016. memAboutToChange(p, pOut);
  4017. /* Note that RowKey and RowData are really exactly the same instruction */
  4018. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4019. pC = p->apCsr[pOp->p1];
  4020. assert( pC->isSorter==0 );
  4021. assert( pC->isTable || pOp->opcode!=OP_RowData );
  4022. assert( pC->isIndex || pOp->opcode==OP_RowData );
  4023. assert( pC!=0 );
  4024. assert( pC->nullRow==0 );
  4025. assert( pC->pseudoTableReg==0 );
  4026. assert( pC->pCursor!=0 );
  4027. pCrsr = pC->pCursor;
  4028. assert( sqlite3BtreeCursorIsValid(pCrsr) );
  4029. /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
  4030. ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
  4031. ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
  4032. ** a no-op and can never fail. But we leave it in place as a safety.
  4033. */
  4034. assert( pC->deferredMoveto==0 );
  4035. rc = sqlite3VdbeCursorMoveto(pC);
  4036. if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
  4037. if( pC->isIndex ){
  4038. assert( !pC->isTable );
  4039. VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
  4040. assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
  4041. if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  4042. goto too_big;
  4043. }
  4044. n = (u32)n64;
  4045. }else{
  4046. VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
  4047. assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
  4048. if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
  4049. goto too_big;
  4050. }
  4051. }
  4052. if( sqlite3VdbeMemGrow(pOut, n, 0) ){
  4053. goto no_mem;
  4054. }
  4055. pOut->n = n;
  4056. MemSetTypeFlag(pOut, MEM_Blob);
  4057. if( pC->isIndex ){
  4058. rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
  4059. }else{
  4060. rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
  4061. }
  4062. pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
  4063. UPDATE_MAX_BLOBSIZE(pOut);
  4064. break;
  4065. }
  4066. /* Opcode: Rowid P1 P2 * * *
  4067. **
  4068. ** Store in register P2 an integer which is the key of the table entry that
  4069. ** P1 is currently point to.
  4070. **
  4071. ** P1 can be either an ordinary table or a virtual table. There used to
  4072. ** be a separate OP_VRowid opcode for use with virtual tables, but this
  4073. ** one opcode now works for both table types.
  4074. */
  4075. case OP_Rowid: { /* out2-prerelease */
  4076. VdbeCursor *pC;
  4077. i64 v;
  4078. sqlite3_vtab *pVtab;
  4079. const sqlite3_module *pModule;
  4080. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4081. pC = p->apCsr[pOp->p1];
  4082. assert( pC!=0 );
  4083. assert( pC->pseudoTableReg==0 || pC->nullRow );
  4084. if( pC->nullRow ){
  4085. pOut->flags = MEM_Null;
  4086. break;
  4087. }else if( pC->deferredMoveto ){
  4088. v = pC->movetoTarget;
  4089. #ifndef SQLITE_OMIT_VIRTUALTABLE
  4090. }else if( pC->pVtabCursor ){
  4091. pVtab = pC->pVtabCursor->pVtab;
  4092. pModule = pVtab->pModule;
  4093. assert( pModule->xRowid );
  4094. rc = pModule->xRowid(pC->pVtabCursor, &v);
  4095. sqlite3VtabImportErrmsg(p, pVtab);
  4096. #endif /* SQLITE_OMIT_VIRTUALTABLE */
  4097. }else{
  4098. assert( pC->pCursor!=0 );
  4099. rc = sqlite3VdbeCursorMoveto(pC);
  4100. if( rc ) goto abort_due_to_error;
  4101. if( pC->rowidIsValid ){
  4102. v = pC->lastRowid;
  4103. }else{
  4104. rc = sqlite3BtreeKeySize(pC->pCursor, &v);
  4105. assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
  4106. }
  4107. }
  4108. pOut->u.i = v;
  4109. break;
  4110. }
  4111. /* Opcode: NullRow P1 * * * *
  4112. **
  4113. ** Move the cursor P1 to a null row. Any OP_Column operations
  4114. ** that occur while the cursor is on the null row will always
  4115. ** write a NULL.
  4116. */
  4117. case OP_NullRow: {
  4118. VdbeCursor *pC;
  4119. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4120. pC = p->apCsr[pOp->p1];
  4121. assert( pC!=0 );
  4122. pC->nullRow = 1;
  4123. pC->rowidIsValid = 0;
  4124. assert( pC->pCursor || pC->pVtabCursor );
  4125. if( pC->pCursor ){
  4126. sqlite3BtreeClearCursor(pC->pCursor);
  4127. }
  4128. break;
  4129. }
  4130. /* Opcode: Last P1 P2 * * *
  4131. **
  4132. ** The next use of the Rowid or Column or Next instruction for P1
  4133. ** will refer to the last entry in the database table or index.
  4134. ** If the table or index is empty and P2>0, then jump immediately to P2.
  4135. ** If P2 is 0 or if the table or index is not empty, fall through
  4136. ** to the following instruction.
  4137. */
  4138. case OP_Last: { /* jump */
  4139. VdbeCursor *pC;
  4140. BtCursor *pCrsr;
  4141. int res;
  4142. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4143. pC = p->apCsr[pOp->p1];
  4144. assert( pC!=0 );
  4145. pCrsr = pC->pCursor;
  4146. res = 0;
  4147. if( ALWAYS(pCrsr!=0) ){
  4148. rc = sqlite3BtreeLast(pCrsr, &res);
  4149. }
  4150. pC->nullRow = (u8)res;
  4151. pC->deferredMoveto = 0;
  4152. pC->rowidIsValid = 0;
  4153. pC->cacheStatus = CACHE_STALE;
  4154. if( pOp->p2>0 && res ){
  4155. pc = pOp->p2 - 1;
  4156. }
  4157. break;
  4158. }
  4159. /* Opcode: Sort P1 P2 * * *
  4160. **
  4161. ** This opcode does exactly the same thing as OP_Rewind except that
  4162. ** it increments an undocumented global variable used for testing.
  4163. **
  4164. ** Sorting is accomplished by writing records into a sorting index,
  4165. ** then rewinding that index and playing it back from beginning to
  4166. ** end. We use the OP_Sort opcode instead of OP_Rewind to do the
  4167. ** rewinding so that the global variable will be incremented and
  4168. ** regression tests can determine whether or not the optimizer is
  4169. ** correctly optimizing out sorts.
  4170. */
  4171. case OP_SorterSort: /* jump */
  4172. case OP_Sort: { /* jump */
  4173. #ifdef SQLITE_TEST
  4174. sqlite3_sort_count++;
  4175. sqlite3_search_count--;
  4176. #endif
  4177. p->aCounter[SQLITE_STMTSTATUS_SORT]++;
  4178. /* Fall through into OP_Rewind */
  4179. }
  4180. /* Opcode: Rewind P1 P2 * * *
  4181. **
  4182. ** The next use of the Rowid or Column or Next instruction for P1
  4183. ** will refer to the first entry in the database table or index.
  4184. ** If the table or index is empty and P2>0, then jump immediately to P2.
  4185. ** If P2 is 0 or if the table or index is not empty, fall through
  4186. ** to the following instruction.
  4187. */
  4188. case OP_Rewind: { /* jump */
  4189. VdbeCursor *pC;
  4190. BtCursor *pCrsr;
  4191. int res;
  4192. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4193. pC = p->apCsr[pOp->p1];
  4194. assert( pC!=0 );
  4195. assert( pC->isSorter==(pOp->opcode==OP_SorterSort) );
  4196. res = 1;
  4197. if( isSorter(pC) ){
  4198. rc = sqlite3VdbeSorterRewind(db, pC, &res);
  4199. }else{
  4200. pCrsr = pC->pCursor;
  4201. assert( pCrsr );
  4202. rc = sqlite3BtreeFirst(pCrsr, &res);
  4203. pC->atFirst = res==0 ?1:0;
  4204. pC->deferredMoveto = 0;
  4205. pC->cacheStatus = CACHE_STALE;
  4206. pC->rowidIsValid = 0;
  4207. }
  4208. pC->nullRow = (u8)res;
  4209. assert( pOp->p2>0 && pOp->p2<p->nOp );
  4210. if( res ){
  4211. pc = pOp->p2 - 1;
  4212. }
  4213. break;
  4214. }
  4215. /* Opcode: Next P1 P2 * P4 P5
  4216. **
  4217. ** Advance cursor P1 so that it points to the next key/data pair in its
  4218. ** table or index. If there are no more key/value pairs then fall through
  4219. ** to the following instruction. But if the cursor advance was successful,
  4220. ** jump immediately to P2.
  4221. **
  4222. ** The P1 cursor must be for a real table, not a pseudo-table.
  4223. **
  4224. ** P4 is always of type P4_ADVANCE. The function pointer points to
  4225. ** sqlite3BtreeNext().
  4226. **
  4227. ** If P5 is positive and the jump is taken, then event counter
  4228. ** number P5-1 in the prepared statement is incremented.
  4229. **
  4230. ** See also: Prev
  4231. */
  4232. /* Opcode: Prev P1 P2 * * P5
  4233. **
  4234. ** Back up cursor P1 so that it points to the previous key/data pair in its
  4235. ** table or index. If there is no previous key/value pairs then fall through
  4236. ** to the following instruction. But if the cursor backup was successful,
  4237. ** jump immediately to P2.
  4238. **
  4239. ** The P1 cursor must be for a real table, not a pseudo-table.
  4240. **
  4241. ** P4 is always of type P4_ADVANCE. The function pointer points to
  4242. ** sqlite3BtreePrevious().
  4243. **
  4244. ** If P5 is positive and the jump is taken, then event counter
  4245. ** number P5-1 in the prepared statement is incremented.
  4246. */
  4247. case OP_SorterNext: /* jump */
  4248. case OP_Prev: /* jump */
  4249. case OP_Next: { /* jump */
  4250. VdbeCursor *pC;
  4251. int res;
  4252. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4253. assert( pOp->p5<ArraySize(p->aCounter) );
  4254. pC = p->apCsr[pOp->p1];
  4255. if( pC==0 ){
  4256. break; /* See ticket #2273 */
  4257. }
  4258. assert( pC->isSorter==(pOp->opcode==OP_SorterNext) );
  4259. if( isSorter(pC) ){
  4260. assert( pOp->opcode==OP_SorterNext );
  4261. rc = sqlite3VdbeSorterNext(db, pC, &res);
  4262. }else{
  4263. /* res = 1; // Always initialized by the xAdvance() call */
  4264. assert( pC->deferredMoveto==0 );
  4265. assert( pC->pCursor );
  4266. assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
  4267. assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
  4268. rc = pOp->p4.xAdvance(pC->pCursor, &res);
  4269. }
  4270. pC->nullRow = (u8)res;
  4271. pC->cacheStatus = CACHE_STALE;
  4272. if( res==0 ){
  4273. pc = pOp->p2 - 1;
  4274. p->aCounter[pOp->p5]++;
  4275. #ifdef SQLITE_TEST
  4276. sqlite3_search_count++;
  4277. #endif
  4278. }
  4279. pC->rowidIsValid = 0;
  4280. goto check_for_interrupt;
  4281. }
  4282. /* Opcode: IdxInsert P1 P2 P3 * P5
  4283. **
  4284. ** Register P2 holds an SQL index key made using the
  4285. ** MakeRecord instructions. This opcode writes that key
  4286. ** into the index P1. Data for the entry is nil.
  4287. **
  4288. ** P3 is a flag that provides a hint to the b-tree layer that this
  4289. ** insert is likely to be an append.
  4290. **
  4291. ** This instruction only works for indices. The equivalent instruction
  4292. ** for tables is OP_Insert.
  4293. */
  4294. case OP_SorterInsert: /* in2 */
  4295. case OP_IdxInsert: { /* in2 */
  4296. VdbeCursor *pC;
  4297. BtCursor *pCrsr;
  4298. int nKey;
  4299. const char *zKey;
  4300. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4301. pC = p->apCsr[pOp->p1];
  4302. assert( pC!=0 );
  4303. assert( pC->isSorter==(pOp->opcode==OP_SorterInsert) );
  4304. pIn2 = &aMem[pOp->p2];
  4305. assert( pIn2->flags & MEM_Blob );
  4306. pCrsr = pC->pCursor;
  4307. if( ALWAYS(pCrsr!=0) ){
  4308. assert( pC->isTable==0 );
  4309. rc = ExpandBlob(pIn2);
  4310. if( rc==SQLITE_OK ){
  4311. if( isSorter(pC) ){
  4312. rc = sqlite3VdbeSorterWrite(db, pC, pIn2);
  4313. }else{
  4314. nKey = pIn2->n;
  4315. zKey = pIn2->z;
  4316. rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
  4317. ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
  4318. );
  4319. assert( pC->deferredMoveto==0 );
  4320. pC->cacheStatus = CACHE_STALE;
  4321. }
  4322. }
  4323. }
  4324. break;
  4325. }
  4326. /* Opcode: IdxDelete P1 P2 P3 * *
  4327. **
  4328. ** The content of P3 registers starting at register P2 form
  4329. ** an unpacked index key. This opcode removes that entry from the
  4330. ** index opened by cursor P1.
  4331. */
  4332. case OP_IdxDelete: {
  4333. VdbeCursor *pC;
  4334. BtCursor *pCrsr;
  4335. int res;
  4336. UnpackedRecord r;
  4337. assert( pOp->p3>0 );
  4338. assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
  4339. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4340. pC = p->apCsr[pOp->p1];
  4341. assert( pC!=0 );
  4342. pCrsr = pC->pCursor;
  4343. if( ALWAYS(pCrsr!=0) ){
  4344. r.pKeyInfo = pC->pKeyInfo;
  4345. r.nField = (u16)pOp->p3;
  4346. r.flags = 0;
  4347. r.aMem = &aMem[pOp->p2];
  4348. #ifdef SQLITE_DEBUG
  4349. { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  4350. #endif
  4351. rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
  4352. if( rc==SQLITE_OK && res==0 ){
  4353. rc = sqlite3BtreeDelete(pCrsr);
  4354. }
  4355. assert( pC->deferredMoveto==0 );
  4356. pC->cacheStatus = CACHE_STALE;
  4357. }
  4358. break;
  4359. }
  4360. /* Opcode: IdxRowid P1 P2 * * *
  4361. **
  4362. ** Write into register P2 an integer which is the last entry in the record at
  4363. ** the end of the index key pointed to by cursor P1. This integer should be
  4364. ** the rowid of the table entry to which this index entry points.
  4365. **
  4366. ** See also: Rowid, MakeRecord.
  4367. */
  4368. case OP_IdxRowid: { /* out2-prerelease */
  4369. BtCursor *pCrsr;
  4370. VdbeCursor *pC;
  4371. i64 rowid;
  4372. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4373. pC = p->apCsr[pOp->p1];
  4374. assert( pC!=0 );
  4375. pCrsr = pC->pCursor;
  4376. pOut->flags = MEM_Null;
  4377. if( ALWAYS(pCrsr!=0) ){
  4378. rc = sqlite3VdbeCursorMoveto(pC);
  4379. if( NEVER(rc) ) goto abort_due_to_error;
  4380. assert( pC->deferredMoveto==0 );
  4381. assert( pC->isTable==0 );
  4382. if( !pC->nullRow ){
  4383. rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
  4384. if( rc!=SQLITE_OK ){
  4385. goto abort_due_to_error;
  4386. }
  4387. pOut->u.i = rowid;
  4388. pOut->flags = MEM_Int;
  4389. }
  4390. }
  4391. break;
  4392. }
  4393. /* Opcode: IdxGE P1 P2 P3 P4 P5
  4394. **
  4395. ** The P4 register values beginning with P3 form an unpacked index
  4396. ** key that omits the ROWID. Compare this key value against the index
  4397. ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
  4398. **
  4399. ** If the P1 index entry is greater than or equal to the key value
  4400. ** then jump to P2. Otherwise fall through to the next instruction.
  4401. **
  4402. ** If P5 is non-zero then the key value is increased by an epsilon
  4403. ** prior to the comparison. This make the opcode work like IdxGT except
  4404. ** that if the key from register P3 is a prefix of the key in the cursor,
  4405. ** the result is false whereas it would be true with IdxGT.
  4406. */
  4407. /* Opcode: IdxLT P1 P2 P3 P4 P5
  4408. **
  4409. ** The P4 register values beginning with P3 form an unpacked index
  4410. ** key that omits the ROWID. Compare this key value against the index
  4411. ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
  4412. **
  4413. ** If the P1 index entry is less than the key value then jump to P2.
  4414. ** Otherwise fall through to the next instruction.
  4415. **
  4416. ** If P5 is non-zero then the key value is increased by an epsilon prior
  4417. ** to the comparison. This makes the opcode work like IdxLE.
  4418. */
  4419. case OP_IdxLT: /* jump */
  4420. case OP_IdxGE: { /* jump */
  4421. VdbeCursor *pC;
  4422. int res;
  4423. UnpackedRecord r;
  4424. assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4425. pC = p->apCsr[pOp->p1];
  4426. assert( pC!=0 );
  4427. assert( pC->isOrdered );
  4428. if( ALWAYS(pC->pCursor!=0) ){
  4429. assert( pC->deferredMoveto==0 );
  4430. assert( pOp->p5==0 || pOp->p5==1 );
  4431. assert( pOp->p4type==P4_INT32 );
  4432. r.pKeyInfo = pC->pKeyInfo;
  4433. r.nField = (u16)pOp->p4.i;
  4434. if( pOp->p5 ){
  4435. r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
  4436. }else{
  4437. r.flags = UNPACKED_PREFIX_MATCH;
  4438. }
  4439. r.aMem = &aMem[pOp->p3];
  4440. #ifdef SQLITE_DEBUG
  4441. { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  4442. #endif
  4443. rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
  4444. if( pOp->opcode==OP_IdxLT ){
  4445. res = -res;
  4446. }else{
  4447. assert( pOp->opcode==OP_IdxGE );
  4448. res++;
  4449. }
  4450. if( res>0 ){
  4451. pc = pOp->p2 - 1 ;
  4452. }
  4453. }
  4454. break;
  4455. }
  4456. /* Opcode: Destroy P1 P2 P3 * *
  4457. **
  4458. ** Delete an entire database table or index whose root page in the database
  4459. ** file is given by P1.
  4460. **
  4461. ** The table being destroyed is in the main database file if P3==0. If
  4462. ** P3==1 then the table to be clear is in the auxiliary database file
  4463. ** that is used to store tables create using CREATE TEMPORARY TABLE.
  4464. **
  4465. ** If AUTOVACUUM is enabled then it is possible that another root page
  4466. ** might be moved into the newly deleted root page in order to keep all
  4467. ** root pages contiguous at the beginning of the database. The former
  4468. ** value of the root page that moved - its value before the move occurred -
  4469. ** is stored in register P2. If no page
  4470. ** movement was required (because the table being dropped was already
  4471. ** the last one in the database) then a zero is stored in register P2.
  4472. ** If AUTOVACUUM is disabled then a zero is stored in register P2.
  4473. **
  4474. ** See also: Clear
  4475. */
  4476. case OP_Destroy: { /* out2-prerelease */
  4477. int iMoved;
  4478. int iCnt;
  4479. Vdbe *pVdbe;
  4480. int iDb;
  4481. assert( p->readOnly==0 );
  4482. #ifndef SQLITE_OMIT_VIRTUALTABLE
  4483. iCnt = 0;
  4484. for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
  4485. if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader
  4486. && pVdbe->inVtabMethod<2 && pVdbe->pc>=0
  4487. ){
  4488. iCnt++;
  4489. }
  4490. }
  4491. #else
  4492. iCnt = db->nVdbeRead;
  4493. #endif
  4494. pOut->flags = MEM_Null;
  4495. if( iCnt>1 ){
  4496. rc = SQLITE_LOCKED;
  4497. p->errorAction = OE_Abort;
  4498. }else{
  4499. iDb = pOp->p3;
  4500. assert( iCnt==1 );
  4501. assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
  4502. rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
  4503. pOut->flags = MEM_Int;
  4504. pOut->u.i = iMoved;
  4505. #ifndef SQLITE_OMIT_AUTOVACUUM
  4506. if( rc==SQLITE_OK && iMoved!=0 ){
  4507. sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
  4508. /* All OP_Destroy operations occur on the same btree */
  4509. assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
  4510. resetSchemaOnFault = iDb+1;
  4511. }
  4512. #endif
  4513. }
  4514. break;
  4515. }
  4516. /* Opcode: Clear P1 P2 P3
  4517. **
  4518. ** Delete all contents of the database table or index whose root page
  4519. ** in the database file is given by P1. But, unlike Destroy, do not
  4520. ** remove the table or index from the database file.
  4521. **
  4522. ** The table being clear is in the main database file if P2==0. If
  4523. ** P2==1 then the table to be clear is in the auxiliary database file
  4524. ** that is used to store tables create using CREATE TEMPORARY TABLE.
  4525. **
  4526. ** If the P3 value is non-zero, then the table referred to must be an
  4527. ** intkey table (an SQL table, not an index). In this case the row change
  4528. ** count is incremented by the number of rows in the table being cleared.
  4529. ** If P3 is greater than zero, then the value stored in register P3 is
  4530. ** also incremented by the number of rows in the table being cleared.
  4531. **
  4532. ** See also: Destroy
  4533. */
  4534. case OP_Clear: {
  4535. int nChange;
  4536. nChange = 0;
  4537. assert( p->readOnly==0 );
  4538. assert( pOp->p1!=1 );
  4539. assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
  4540. rc = sqlite3BtreeClearTable(
  4541. db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
  4542. );
  4543. if( pOp->p3 ){
  4544. p->nChange += nChange;
  4545. if( pOp->p3>0 ){
  4546. assert( memIsValid(&aMem[pOp->p3]) );
  4547. memAboutToChange(p, &aMem[pOp->p3]);
  4548. aMem[pOp->p3].u.i += nChange;
  4549. }
  4550. }
  4551. break;
  4552. }
  4553. /* Opcode: CreateTable P1 P2 * * *
  4554. **
  4555. ** Allocate a new table in the main database file if P1==0 or in the
  4556. ** auxiliary database file if P1==1 or in an attached database if
  4557. ** P1>1. Write the root page number of the new table into
  4558. ** register P2
  4559. **
  4560. ** The difference between a table and an index is this: A table must
  4561. ** have a 4-byte integer key and can have arbitrary data. An index
  4562. ** has an arbitrary key but no data.
  4563. **
  4564. ** See also: CreateIndex
  4565. */
  4566. /* Opcode: CreateIndex P1 P2 * * *
  4567. **
  4568. ** Allocate a new index in the main database file if P1==0 or in the
  4569. ** auxiliary database file if P1==1 or in an attached database if
  4570. ** P1>1. Write the root page number of the new table into
  4571. ** register P2.
  4572. **
  4573. ** See documentation on OP_CreateTable for additional information.
  4574. */
  4575. case OP_CreateIndex: /* out2-prerelease */
  4576. case OP_CreateTable: { /* out2-prerelease */
  4577. int pgno;
  4578. int flags;
  4579. Db *pDb;
  4580. pgno = 0;
  4581. assert( pOp->p1>=0 && pOp->p1<db->nDb );
  4582. assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  4583. assert( p->readOnly==0 );
  4584. pDb = &db->aDb[pOp->p1];
  4585. assert( pDb->pBt!=0 );
  4586. if( pOp->opcode==OP_CreateTable ){
  4587. /* flags = BTREE_INTKEY; */
  4588. flags = BTREE_INTKEY;
  4589. }else{
  4590. flags = BTREE_BLOBKEY;
  4591. }
  4592. rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
  4593. pOut->u.i = pgno;
  4594. break;
  4595. }
  4596. /* Opcode: ParseSchema P1 * * P4 *
  4597. **
  4598. ** Read and parse all entries from the SQLITE_MASTER table of database P1
  4599. ** that match the WHERE clause P4.
  4600. **
  4601. ** This opcode invokes the parser to create a new virtual machine,
  4602. ** then runs the new virtual machine. It is thus a re-entrant opcode.
  4603. */
  4604. case OP_ParseSchema: {
  4605. int iDb;
  4606. const char *zMaster;
  4607. char *zSql;
  4608. InitData initData;
  4609. /* Any prepared statement that invokes this opcode will hold mutexes
  4610. ** on every btree. This is a prerequisite for invoking
  4611. ** sqlite3InitCallback().
  4612. */
  4613. #ifdef SQLITE_DEBUG
  4614. for(iDb=0; iDb<db->nDb; iDb++){
  4615. assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
  4616. }
  4617. #endif
  4618. iDb = pOp->p1;
  4619. assert( iDb>=0 && iDb<db->nDb );
  4620. assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
  4621. /* Used to be a conditional */ {
  4622. zMaster = SCHEMA_TABLE(iDb);
  4623. initData.db = db;
  4624. initData.iDb = pOp->p1;
  4625. initData.pzErrMsg = &p->zErrMsg;
  4626. zSql = sqlite3MPrintf(db,
  4627. "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
  4628. db->aDb[iDb].zName, zMaster, pOp->p4.z);
  4629. if( zSql==0 ){
  4630. rc = SQLITE_NOMEM;
  4631. }else{
  4632. assert( db->init.busy==0 );
  4633. db->init.busy = 1;
  4634. initData.rc = SQLITE_OK;
  4635. assert( !db->mallocFailed );
  4636. rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
  4637. if( rc==SQLITE_OK ) rc = initData.rc;
  4638. sqlite3DbFree(db, zSql);
  4639. db->init.busy = 0;
  4640. }
  4641. }
  4642. if( rc ) sqlite3ResetAllSchemasOfConnection(db);
  4643. if( rc==SQLITE_NOMEM ){
  4644. goto no_mem;
  4645. }
  4646. break;
  4647. }
  4648. #if !defined(SQLITE_OMIT_ANALYZE)
  4649. /* Opcode: LoadAnalysis P1 * * * *
  4650. **
  4651. ** Read the sqlite_stat1 table for database P1 and load the content
  4652. ** of that table into the internal index hash table. This will cause
  4653. ** the analysis to be used when preparing all subsequent queries.
  4654. */
  4655. case OP_LoadAnalysis: {
  4656. assert( pOp->p1>=0 && pOp->p1<db->nDb );
  4657. rc = sqlite3AnalysisLoad(db, pOp->p1);
  4658. break;
  4659. }
  4660. #endif /* !defined(SQLITE_OMIT_ANALYZE) */
  4661. /* Opcode: DropTable P1 * * P4 *
  4662. **
  4663. ** Remove the internal (in-memory) data structures that describe
  4664. ** the table named P4 in database P1. This is called after a table
  4665. ** is dropped in order to keep the internal representation of the
  4666. ** schema consistent with what is on disk.
  4667. */
  4668. case OP_DropTable: {
  4669. sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
  4670. break;
  4671. }
  4672. /* Opcode: DropIndex P1 * * P4 *
  4673. **
  4674. ** Remove the internal (in-memory) data structures that describe
  4675. ** the index named P4 in database P1. This is called after an index
  4676. ** is dropped in order to keep the internal representation of the
  4677. ** schema consistent with what is on disk.
  4678. */
  4679. case OP_DropIndex: {
  4680. sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
  4681. break;
  4682. }
  4683. /* Opcode: DropTrigger P1 * * P4 *
  4684. **
  4685. ** Remove the internal (in-memory) data structures that describe
  4686. ** the trigger named P4 in database P1. This is called after a trigger
  4687. ** is dropped in order to keep the internal representation of the
  4688. ** schema consistent with what is on disk.
  4689. */
  4690. case OP_DropTrigger: {
  4691. sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
  4692. break;
  4693. }
  4694. #ifndef SQLITE_OMIT_INTEGRITY_CHECK
  4695. /* Opcode: IntegrityCk P1 P2 P3 * P5
  4696. **
  4697. ** Do an analysis of the currently open database. Store in
  4698. ** register P1 the text of an error message describing any problems.
  4699. ** If no problems are found, store a NULL in register P1.
  4700. **
  4701. ** The register P3 contains the maximum number of allowed errors.
  4702. ** At most reg(P3) errors will be reported.
  4703. ** In other words, the analysis stops as soon as reg(P1) errors are
  4704. ** seen. Reg(P1) is updated with the number of errors remaining.
  4705. **
  4706. ** The root page numbers of all tables in the database are integer
  4707. ** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
  4708. ** total.
  4709. **
  4710. ** If P5 is not zero, the check is done on the auxiliary database
  4711. ** file, not the main database file.
  4712. **
  4713. ** This opcode is used to implement the integrity_check pragma.
  4714. */
  4715. case OP_IntegrityCk: {
  4716. int nRoot; /* Number of tables to check. (Number of root pages.) */
  4717. int *aRoot; /* Array of rootpage numbers for tables to be checked */
  4718. int j; /* Loop counter */
  4719. int nErr; /* Number of errors reported */
  4720. char *z; /* Text of the error report */
  4721. Mem *pnErr; /* Register keeping track of errors remaining */
  4722. assert( p->bIsReader );
  4723. nRoot = pOp->p2;
  4724. assert( nRoot>0 );
  4725. aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
  4726. if( aRoot==0 ) goto no_mem;
  4727. assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  4728. pnErr = &aMem[pOp->p3];
  4729. assert( (pnErr->flags & MEM_Int)!=0 );
  4730. assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
  4731. pIn1 = &aMem[pOp->p1];
  4732. for(j=0; j<nRoot; j++){
  4733. aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
  4734. }
  4735. aRoot[j] = 0;
  4736. assert( pOp->p5<db->nDb );
  4737. assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
  4738. z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
  4739. (int)pnErr->u.i, &nErr);
  4740. sqlite3DbFree(db, aRoot);
  4741. pnErr->u.i -= nErr;
  4742. sqlite3VdbeMemSetNull(pIn1);
  4743. if( nErr==0 ){
  4744. assert( z==0 );
  4745. }else if( z==0 ){
  4746. goto no_mem;
  4747. }else{
  4748. sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
  4749. }
  4750. UPDATE_MAX_BLOBSIZE(pIn1);
  4751. sqlite3VdbeChangeEncoding(pIn1, encoding);
  4752. break;
  4753. }
  4754. #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
  4755. /* Opcode: RowSetAdd P1 P2 * * *
  4756. **
  4757. ** Insert the integer value held by register P2 into a boolean index
  4758. ** held in register P1.
  4759. **
  4760. ** An assertion fails if P2 is not an integer.
  4761. */
  4762. case OP_RowSetAdd: { /* in1, in2 */
  4763. pIn1 = &aMem[pOp->p1];
  4764. pIn2 = &aMem[pOp->p2];
  4765. assert( (pIn2->flags & MEM_Int)!=0 );
  4766. if( (pIn1->flags & MEM_RowSet)==0 ){
  4767. sqlite3VdbeMemSetRowSet(pIn1);
  4768. if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
  4769. }
  4770. sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
  4771. break;
  4772. }
  4773. /* Opcode: RowSetRead P1 P2 P3 * *
  4774. **
  4775. ** Extract the smallest value from boolean index P1 and put that value into
  4776. ** register P3. Or, if boolean index P1 is initially empty, leave P3
  4777. ** unchanged and jump to instruction P2.
  4778. */
  4779. case OP_RowSetRead: { /* jump, in1, out3 */
  4780. i64 val;
  4781. pIn1 = &aMem[pOp->p1];
  4782. if( (pIn1->flags & MEM_RowSet)==0
  4783. || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
  4784. ){
  4785. /* The boolean index is empty */
  4786. sqlite3VdbeMemSetNull(pIn1);
  4787. pc = pOp->p2 - 1;
  4788. }else{
  4789. /* A value was pulled from the index */
  4790. sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
  4791. }
  4792. goto check_for_interrupt;
  4793. }
  4794. /* Opcode: RowSetTest P1 P2 P3 P4
  4795. **
  4796. ** Register P3 is assumed to hold a 64-bit integer value. If register P1
  4797. ** contains a RowSet object and that RowSet object contains
  4798. ** the value held in P3, jump to register P2. Otherwise, insert the
  4799. ** integer in P3 into the RowSet and continue on to the
  4800. ** next opcode.
  4801. **
  4802. ** The RowSet object is optimized for the case where successive sets
  4803. ** of integers, where each set contains no duplicates. Each set
  4804. ** of values is identified by a unique P4 value. The first set
  4805. ** must have P4==0, the final set P4=-1. P4 must be either -1 or
  4806. ** non-negative. For non-negative values of P4 only the lower 4
  4807. ** bits are significant.
  4808. **
  4809. ** This allows optimizations: (a) when P4==0 there is no need to test
  4810. ** the rowset object for P3, as it is guaranteed not to contain it,
  4811. ** (b) when P4==-1 there is no need to insert the value, as it will
  4812. ** never be tested for, and (c) when a value that is part of set X is
  4813. ** inserted, there is no need to search to see if the same value was
  4814. ** previously inserted as part of set X (only if it was previously
  4815. ** inserted as part of some other set).
  4816. */
  4817. case OP_RowSetTest: { /* jump, in1, in3 */
  4818. int iSet;
  4819. int exists;
  4820. pIn1 = &aMem[pOp->p1];
  4821. pIn3 = &aMem[pOp->p3];
  4822. iSet = pOp->p4.i;
  4823. assert( pIn3->flags&MEM_Int );
  4824. /* If there is anything other than a rowset object in memory cell P1,
  4825. ** delete it now and initialize P1 with an empty rowset
  4826. */
  4827. if( (pIn1->flags & MEM_RowSet)==0 ){
  4828. sqlite3VdbeMemSetRowSet(pIn1);
  4829. if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
  4830. }
  4831. assert( pOp->p4type==P4_INT32 );
  4832. assert( iSet==-1 || iSet>=0 );
  4833. if( iSet ){
  4834. exists = sqlite3RowSetTest(pIn1->u.pRowSet,
  4835. (u8)(iSet>=0 ? iSet & 0xf : 0xff),
  4836. pIn3->u.i);
  4837. if( exists ){
  4838. pc = pOp->p2 - 1;
  4839. break;
  4840. }
  4841. }
  4842. if( iSet>=0 ){
  4843. sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
  4844. }
  4845. break;
  4846. }
  4847. #ifndef SQLITE_OMIT_TRIGGER
  4848. /* Opcode: Program P1 P2 P3 P4 *
  4849. **
  4850. ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
  4851. **
  4852. ** P1 contains the address of the memory cell that contains the first memory
  4853. ** cell in an array of values used as arguments to the sub-program. P2
  4854. ** contains the address to jump to if the sub-program throws an IGNORE
  4855. ** exception using the RAISE() function. Register P3 contains the address
  4856. ** of a memory cell in this (the parent) VM that is used to allocate the
  4857. ** memory required by the sub-vdbe at runtime.
  4858. **
  4859. ** P4 is a pointer to the VM containing the trigger program.
  4860. */
  4861. case OP_Program: { /* jump */
  4862. int nMem; /* Number of memory registers for sub-program */
  4863. int nByte; /* Bytes of runtime space required for sub-program */
  4864. Mem *pRt; /* Register to allocate runtime space */
  4865. Mem *pMem; /* Used to iterate through memory cells */
  4866. Mem *pEnd; /* Last memory cell in new array */
  4867. VdbeFrame *pFrame; /* New vdbe frame to execute in */
  4868. SubProgram *pProgram; /* Sub-program to execute */
  4869. void *t; /* Token identifying trigger */
  4870. pProgram = pOp->p4.pProgram;
  4871. pRt = &aMem[pOp->p3];
  4872. assert( pProgram->nOp>0 );
  4873. /* If the p5 flag is clear, then recursive invocation of triggers is
  4874. ** disabled for backwards compatibility (p5 is set if this sub-program
  4875. ** is really a trigger, not a foreign key action, and the flag set
  4876. ** and cleared by the "PRAGMA recursive_triggers" command is clear).
  4877. **
  4878. ** It is recursive invocation of triggers, at the SQL level, that is
  4879. ** disabled. In some cases a single trigger may generate more than one
  4880. ** SubProgram (if the trigger may be executed with more than one different
  4881. ** ON CONFLICT algorithm). SubProgram structures associated with a
  4882. ** single trigger all have the same value for the SubProgram.token
  4883. ** variable. */
  4884. if( pOp->p5 ){
  4885. t = pProgram->token;
  4886. for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
  4887. if( pFrame ) break;
  4888. }
  4889. if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
  4890. rc = SQLITE_ERROR;
  4891. sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
  4892. break;
  4893. }
  4894. /* Register pRt is used to store the memory required to save the state
  4895. ** of the current program, and the memory required at runtime to execute
  4896. ** the trigger program. If this trigger has been fired before, then pRt
  4897. ** is already allocated. Otherwise, it must be initialized. */
  4898. if( (pRt->flags&MEM_Frame)==0 ){
  4899. /* SubProgram.nMem is set to the number of memory cells used by the
  4900. ** program stored in SubProgram.aOp. As well as these, one memory
  4901. ** cell is required for each cursor used by the program. Set local
  4902. ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
  4903. */
  4904. nMem = pProgram->nMem + pProgram->nCsr;
  4905. nByte = ROUND8(sizeof(VdbeFrame))
  4906. + nMem * sizeof(Mem)
  4907. + pProgram->nCsr * sizeof(VdbeCursor *)
  4908. + pProgram->nOnce * sizeof(u8);
  4909. pFrame = sqlite3DbMallocZero(db, nByte);
  4910. if( !pFrame ){
  4911. goto no_mem;
  4912. }
  4913. sqlite3VdbeMemRelease(pRt);
  4914. pRt->flags = MEM_Frame;
  4915. pRt->u.pFrame = pFrame;
  4916. pFrame->v = p;
  4917. pFrame->nChildMem = nMem;
  4918. pFrame->nChildCsr = pProgram->nCsr;
  4919. pFrame->pc = pc;
  4920. pFrame->aMem = p->aMem;
  4921. pFrame->nMem = p->nMem;
  4922. pFrame->apCsr = p->apCsr;
  4923. pFrame->nCursor = p->nCursor;
  4924. pFrame->aOp = p->aOp;
  4925. pFrame->nOp = p->nOp;
  4926. pFrame->token = pProgram->token;
  4927. pFrame->aOnceFlag = p->aOnceFlag;
  4928. pFrame->nOnceFlag = p->nOnceFlag;
  4929. pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
  4930. for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
  4931. pMem->flags = MEM_Invalid;
  4932. pMem->db = db;
  4933. }
  4934. }else{
  4935. pFrame = pRt->u.pFrame;
  4936. assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
  4937. assert( pProgram->nCsr==pFrame->nChildCsr );
  4938. assert( pc==pFrame->pc );
  4939. }
  4940. p->nFrame++;
  4941. pFrame->pParent = p->pFrame;
  4942. pFrame->lastRowid = lastRowid;
  4943. pFrame->nChange = p->nChange;
  4944. p->nChange = 0;
  4945. p->pFrame = pFrame;
  4946. p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
  4947. p->nMem = pFrame->nChildMem;
  4948. p->nCursor = (u16)pFrame->nChildCsr;
  4949. p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
  4950. p->aOp = aOp = pProgram->aOp;
  4951. p->nOp = pProgram->nOp;
  4952. p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
  4953. p->nOnceFlag = pProgram->nOnce;
  4954. pc = -1;
  4955. memset(p->aOnceFlag, 0, p->nOnceFlag);
  4956. break;
  4957. }
  4958. /* Opcode: Param P1 P2 * * *
  4959. **
  4960. ** This opcode is only ever present in sub-programs called via the
  4961. ** OP_Program instruction. Copy a value currently stored in a memory
  4962. ** cell of the calling (parent) frame to cell P2 in the current frames
  4963. ** address space. This is used by trigger programs to access the new.*
  4964. ** and old.* values.
  4965. **
  4966. ** The address of the cell in the parent frame is determined by adding
  4967. ** the value of the P1 argument to the value of the P1 argument to the
  4968. ** calling OP_Program instruction.
  4969. */
  4970. case OP_Param: { /* out2-prerelease */
  4971. VdbeFrame *pFrame;
  4972. Mem *pIn;
  4973. pFrame = p->pFrame;
  4974. pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
  4975. sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
  4976. break;
  4977. }
  4978. #endif /* #ifndef SQLITE_OMIT_TRIGGER */
  4979. #ifndef SQLITE_OMIT_FOREIGN_KEY
  4980. /* Opcode: FkCounter P1 P2 * * *
  4981. **
  4982. ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
  4983. ** If P1 is non-zero, the database constraint counter is incremented
  4984. ** (deferred foreign key constraints). Otherwise, if P1 is zero, the
  4985. ** statement counter is incremented (immediate foreign key constraints).
  4986. */
  4987. case OP_FkCounter: {
  4988. if( db->flags & SQLITE_DeferFKs ){
  4989. db->nDeferredImmCons += pOp->p2;
  4990. }else if( pOp->p1 ){
  4991. db->nDeferredCons += pOp->p2;
  4992. }else{
  4993. p->nFkConstraint += pOp->p2;
  4994. }
  4995. break;
  4996. }
  4997. /* Opcode: FkIfZero P1 P2 * * *
  4998. **
  4999. ** This opcode tests if a foreign key constraint-counter is currently zero.
  5000. ** If so, jump to instruction P2. Otherwise, fall through to the next
  5001. ** instruction.
  5002. **
  5003. ** If P1 is non-zero, then the jump is taken if the database constraint-counter
  5004. ** is zero (the one that counts deferred constraint violations). If P1 is
  5005. ** zero, the jump is taken if the statement constraint-counter is zero
  5006. ** (immediate foreign key constraint violations).
  5007. */
  5008. case OP_FkIfZero: { /* jump */
  5009. if( pOp->p1 ){
  5010. if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
  5011. }else{
  5012. if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
  5013. }
  5014. break;
  5015. }
  5016. #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
  5017. #ifndef SQLITE_OMIT_AUTOINCREMENT
  5018. /* Opcode: MemMax P1 P2 * * *
  5019. **
  5020. ** P1 is a register in the root frame of this VM (the root frame is
  5021. ** different from the current frame if this instruction is being executed
  5022. ** within a sub-program). Set the value of register P1 to the maximum of
  5023. ** its current value and the value in register P2.
  5024. **
  5025. ** This instruction throws an error if the memory cell is not initially
  5026. ** an integer.
  5027. */
  5028. case OP_MemMax: { /* in2 */
  5029. Mem *pIn1;
  5030. VdbeFrame *pFrame;
  5031. if( p->pFrame ){
  5032. for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
  5033. pIn1 = &pFrame->aMem[pOp->p1];
  5034. }else{
  5035. pIn1 = &aMem[pOp->p1];
  5036. }
  5037. assert( memIsValid(pIn1) );
  5038. sqlite3VdbeMemIntegerify(pIn1);
  5039. pIn2 = &aMem[pOp->p2];
  5040. sqlite3VdbeMemIntegerify(pIn2);
  5041. if( pIn1->u.i<pIn2->u.i){
  5042. pIn1->u.i = pIn2->u.i;
  5043. }
  5044. break;
  5045. }
  5046. #endif /* SQLITE_OMIT_AUTOINCREMENT */
  5047. /* Opcode: IfPos P1 P2 * * *
  5048. **
  5049. ** If the value of register P1 is 1 or greater, jump to P2.
  5050. **
  5051. ** It is illegal to use this instruction on a register that does
  5052. ** not contain an integer. An assertion fault will result if you try.
  5053. */
  5054. case OP_IfPos: { /* jump, in1 */
  5055. pIn1 = &aMem[pOp->p1];
  5056. assert( pIn1->flags&MEM_Int );
  5057. if( pIn1->u.i>0 ){
  5058. pc = pOp->p2 - 1;
  5059. }
  5060. break;
  5061. }
  5062. /* Opcode: IfNeg P1 P2 * * *
  5063. **
  5064. ** If the value of register P1 is less than zero, jump to P2.
  5065. **
  5066. ** It is illegal to use this instruction on a register that does
  5067. ** not contain an integer. An assertion fault will result if you try.
  5068. */
  5069. case OP_IfNeg: { /* jump, in1 */
  5070. pIn1 = &aMem[pOp->p1];
  5071. assert( pIn1->flags&MEM_Int );
  5072. if( pIn1->u.i<0 ){
  5073. pc = pOp->p2 - 1;
  5074. }
  5075. break;
  5076. }
  5077. /* Opcode: IfZero P1 P2 P3 * *
  5078. **
  5079. ** The register P1 must contain an integer. Add literal P3 to the
  5080. ** value in register P1. If the result is exactly 0, jump to P2.
  5081. **
  5082. ** It is illegal to use this instruction on a register that does
  5083. ** not contain an integer. An assertion fault will result if you try.
  5084. */
  5085. case OP_IfZero: { /* jump, in1 */
  5086. pIn1 = &aMem[pOp->p1];
  5087. assert( pIn1->flags&MEM_Int );
  5088. pIn1->u.i += pOp->p3;
  5089. if( pIn1->u.i==0 ){
  5090. pc = pOp->p2 - 1;
  5091. }
  5092. break;
  5093. }
  5094. /* Opcode: AggStep * P2 P3 P4 P5
  5095. **
  5096. ** Execute the step function for an aggregate. The
  5097. ** function has P5 arguments. P4 is a pointer to the FuncDef
  5098. ** structure that specifies the function. Use register
  5099. ** P3 as the accumulator.
  5100. **
  5101. ** The P5 arguments are taken from register P2 and its
  5102. ** successors.
  5103. */
  5104. case OP_AggStep: {
  5105. int n;
  5106. int i;
  5107. Mem *pMem;
  5108. Mem *pRec;
  5109. sqlite3_context ctx;
  5110. sqlite3_value **apVal;
  5111. n = pOp->p5;
  5112. assert( n>=0 );
  5113. pRec = &aMem[pOp->p2];
  5114. apVal = p->apArg;
  5115. assert( apVal || n==0 );
  5116. for(i=0; i<n; i++, pRec++){
  5117. assert( memIsValid(pRec) );
  5118. apVal[i] = pRec;
  5119. memAboutToChange(p, pRec);
  5120. sqlite3VdbeMemStoreType(pRec);
  5121. }
  5122. ctx.pFunc = pOp->p4.pFunc;
  5123. assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  5124. ctx.pMem = pMem = &aMem[pOp->p3];
  5125. pMem->n++;
  5126. ctx.s.flags = MEM_Null;
  5127. ctx.s.z = 0;
  5128. ctx.s.zMalloc = 0;
  5129. ctx.s.xDel = 0;
  5130. ctx.s.db = db;
  5131. ctx.isError = 0;
  5132. ctx.pColl = 0;
  5133. ctx.skipFlag = 0;
  5134. if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  5135. assert( pOp>p->aOp );
  5136. assert( pOp[-1].p4type==P4_COLLSEQ );
  5137. assert( pOp[-1].opcode==OP_CollSeq );
  5138. ctx.pColl = pOp[-1].p4.pColl;
  5139. }
  5140. (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  5141. if( ctx.isError ){
  5142. sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
  5143. rc = ctx.isError;
  5144. }
  5145. if( ctx.skipFlag ){
  5146. assert( pOp[-1].opcode==OP_CollSeq );
  5147. i = pOp[-1].p1;
  5148. if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
  5149. }
  5150. sqlite3VdbeMemRelease(&ctx.s);
  5151. break;
  5152. }
  5153. /* Opcode: AggFinal P1 P2 * P4 *
  5154. **
  5155. ** Execute the finalizer function for an aggregate. P1 is
  5156. ** the memory location that is the accumulator for the aggregate.
  5157. **
  5158. ** P2 is the number of arguments that the step function takes and
  5159. ** P4 is a pointer to the FuncDef for this function. The P2
  5160. ** argument is not used by this opcode. It is only there to disambiguate
  5161. ** functions that can take varying numbers of arguments. The
  5162. ** P4 argument is only needed for the degenerate case where
  5163. ** the step function was not previously called.
  5164. */
  5165. case OP_AggFinal: {
  5166. Mem *pMem;
  5167. assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
  5168. pMem = &aMem[pOp->p1];
  5169. assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
  5170. rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
  5171. if( rc ){
  5172. sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
  5173. }
  5174. sqlite3VdbeChangeEncoding(pMem, encoding);
  5175. UPDATE_MAX_BLOBSIZE(pMem);
  5176. if( sqlite3VdbeMemTooBig(pMem) ){
  5177. goto too_big;
  5178. }
  5179. break;
  5180. }
  5181. #ifndef SQLITE_OMIT_WAL
  5182. /* Opcode: Checkpoint P1 P2 P3 * *
  5183. **
  5184. ** Checkpoint database P1. This is a no-op if P1 is not currently in
  5185. ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
  5186. ** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns
  5187. ** SQLITE_BUSY or not, respectively. Write the number of pages in the
  5188. ** WAL after the checkpoint into mem[P3+1] and the number of pages
  5189. ** in the WAL that have been checkpointed after the checkpoint
  5190. ** completes into mem[P3+2]. However on an error, mem[P3+1] and
  5191. ** mem[P3+2] are initialized to -1.
  5192. */
  5193. case OP_Checkpoint: {
  5194. int i; /* Loop counter */
  5195. int aRes[3]; /* Results */
  5196. Mem *pMem; /* Write results here */
  5197. assert( p->readOnly==0 );
  5198. aRes[0] = 0;
  5199. aRes[1] = aRes[2] = -1;
  5200. assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
  5201. || pOp->p2==SQLITE_CHECKPOINT_FULL
  5202. || pOp->p2==SQLITE_CHECKPOINT_RESTART
  5203. );
  5204. rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
  5205. if( rc==SQLITE_BUSY ){
  5206. rc = SQLITE_OK;
  5207. aRes[0] = 1;
  5208. }
  5209. for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
  5210. sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
  5211. }
  5212. break;
  5213. };
  5214. #endif
  5215. #ifndef SQLITE_OMIT_PRAGMA
  5216. /* Opcode: JournalMode P1 P2 P3 * P5
  5217. **
  5218. ** Change the journal mode of database P1 to P3. P3 must be one of the
  5219. ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
  5220. ** modes (delete, truncate, persist, off and memory), this is a simple
  5221. ** operation. No IO is required.
  5222. **
  5223. ** If changing into or out of WAL mode the procedure is more complicated.
  5224. **
  5225. ** Write a string containing the final journal-mode to register P2.
  5226. */
  5227. case OP_JournalMode: { /* out2-prerelease */
  5228. Btree *pBt; /* Btree to change journal mode of */
  5229. Pager *pPager; /* Pager associated with pBt */
  5230. int eNew; /* New journal mode */
  5231. int eOld; /* The old journal mode */
  5232. #ifndef SQLITE_OMIT_WAL
  5233. const char *zFilename; /* Name of database file for pPager */
  5234. #endif
  5235. eNew = pOp->p3;
  5236. assert( eNew==PAGER_JOURNALMODE_DELETE
  5237. || eNew==PAGER_JOURNALMODE_TRUNCATE
  5238. || eNew==PAGER_JOURNALMODE_PERSIST
  5239. || eNew==PAGER_JOURNALMODE_OFF
  5240. || eNew==PAGER_JOURNALMODE_MEMORY
  5241. || eNew==PAGER_JOURNALMODE_WAL
  5242. || eNew==PAGER_JOURNALMODE_QUERY
  5243. );
  5244. assert( pOp->p1>=0 && pOp->p1<db->nDb );
  5245. assert( p->readOnly==0 );
  5246. pBt = db->aDb[pOp->p1].pBt;
  5247. pPager = sqlite3BtreePager(pBt);
  5248. eOld = sqlite3PagerGetJournalMode(pPager);
  5249. if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
  5250. if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
  5251. #ifndef SQLITE_OMIT_WAL
  5252. zFilename = sqlite3PagerFilename(pPager, 1);
  5253. /* Do not allow a transition to journal_mode=WAL for a database
  5254. ** in temporary storage or if the VFS does not support shared memory
  5255. */
  5256. if( eNew==PAGER_JOURNALMODE_WAL
  5257. && (sqlite3Strlen30(zFilename)==0 /* Temp file */
  5258. || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
  5259. ){
  5260. eNew = eOld;
  5261. }
  5262. if( (eNew!=eOld)
  5263. && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
  5264. ){
  5265. if( !db->autoCommit || db->nVdbeRead>1 ){
  5266. rc = SQLITE_ERROR;
  5267. sqlite3SetString(&p->zErrMsg, db,
  5268. "cannot change %s wal mode from within a transaction",
  5269. (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
  5270. );
  5271. break;
  5272. }else{
  5273. if( eOld==PAGER_JOURNALMODE_WAL ){
  5274. /* If leaving WAL mode, close the log file. If successful, the call
  5275. ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
  5276. ** file. An EXCLUSIVE lock may still be held on the database file
  5277. ** after a successful return.
  5278. */
  5279. rc = sqlite3PagerCloseWal(pPager);
  5280. if( rc==SQLITE_OK ){
  5281. sqlite3PagerSetJournalMode(pPager, eNew);
  5282. }
  5283. }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
  5284. /* Cannot transition directly from MEMORY to WAL. Use mode OFF
  5285. ** as an intermediate */
  5286. sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
  5287. }
  5288. /* Open a transaction on the database file. Regardless of the journal
  5289. ** mode, this transaction always uses a rollback journal.
  5290. */
  5291. assert( sqlite3BtreeIsInTrans(pBt)==0 );
  5292. if( rc==SQLITE_OK ){
  5293. rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
  5294. }
  5295. }
  5296. }
  5297. #endif /* ifndef SQLITE_OMIT_WAL */
  5298. if( rc ){
  5299. eNew = eOld;
  5300. }
  5301. eNew = sqlite3PagerSetJournalMode(pPager, eNew);
  5302. pOut = &aMem[pOp->p2];
  5303. pOut->flags = MEM_Str|MEM_Static|MEM_Term;
  5304. pOut->z = (char *)sqlite3JournalModename(eNew);
  5305. pOut->n = sqlite3Strlen30(pOut->z);
  5306. pOut->enc = SQLITE_UTF8;
  5307. sqlite3VdbeChangeEncoding(pOut, encoding);
  5308. break;
  5309. };
  5310. #endif /* SQLITE_OMIT_PRAGMA */
  5311. #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
  5312. /* Opcode: Vacuum * * * * *
  5313. **
  5314. ** Vacuum the entire database. This opcode will cause other virtual
  5315. ** machines to be created and run. It may not be called from within
  5316. ** a transaction.
  5317. */
  5318. case OP_Vacuum: {
  5319. assert( p->readOnly==0 );
  5320. rc = sqlite3RunVacuum(&p->zErrMsg, db);
  5321. break;
  5322. }
  5323. #endif
  5324. #if !defined(SQLITE_OMIT_AUTOVACUUM)
  5325. /* Opcode: IncrVacuum P1 P2 * * *
  5326. **
  5327. ** Perform a single step of the incremental vacuum procedure on
  5328. ** the P1 database. If the vacuum has finished, jump to instruction
  5329. ** P2. Otherwise, fall through to the next instruction.
  5330. */
  5331. case OP_IncrVacuum: { /* jump */
  5332. Btree *pBt;
  5333. assert( pOp->p1>=0 && pOp->p1<db->nDb );
  5334. assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  5335. assert( p->readOnly==0 );
  5336. pBt = db->aDb[pOp->p1].pBt;
  5337. rc = sqlite3BtreeIncrVacuum(pBt);
  5338. if( rc==SQLITE_DONE ){
  5339. pc = pOp->p2 - 1;
  5340. rc = SQLITE_OK;
  5341. }
  5342. break;
  5343. }
  5344. #endif
  5345. /* Opcode: Expire P1 * * * *
  5346. **
  5347. ** Cause precompiled statements to become expired. An expired statement
  5348. ** fails with an error code of SQLITE_SCHEMA if it is ever executed
  5349. ** (via sqlite3_step()).
  5350. **
  5351. ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
  5352. ** then only the currently executing statement is affected.
  5353. */
  5354. case OP_Expire: {
  5355. if( !pOp->p1 ){
  5356. sqlite3ExpirePreparedStatements(db);
  5357. }else{
  5358. p->expired = 1;
  5359. }
  5360. break;
  5361. }
  5362. #ifndef SQLITE_OMIT_SHARED_CACHE
  5363. /* Opcode: TableLock P1 P2 P3 P4 *
  5364. **
  5365. ** Obtain a lock on a particular table. This instruction is only used when
  5366. ** the shared-cache feature is enabled.
  5367. **
  5368. ** P1 is the index of the database in sqlite3.aDb[] of the database
  5369. ** on which the lock is acquired. A readlock is obtained if P3==0 or
  5370. ** a write lock if P3==1.
  5371. **
  5372. ** P2 contains the root-page of the table to lock.
  5373. **
  5374. ** P4 contains a pointer to the name of the table being locked. This is only
  5375. ** used to generate an error message if the lock cannot be obtained.
  5376. */
  5377. case OP_TableLock: {
  5378. u8 isWriteLock = (u8)pOp->p3;
  5379. if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
  5380. int p1 = pOp->p1;
  5381. assert( p1>=0 && p1<db->nDb );
  5382. assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
  5383. assert( isWriteLock==0 || isWriteLock==1 );
  5384. rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
  5385. if( (rc&0xFF)==SQLITE_LOCKED ){
  5386. const char *z = pOp->p4.z;
  5387. sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
  5388. }
  5389. }
  5390. break;
  5391. }
  5392. #endif /* SQLITE_OMIT_SHARED_CACHE */
  5393. #ifndef SQLITE_OMIT_VIRTUALTABLE
  5394. /* Opcode: VBegin * * * P4 *
  5395. **
  5396. ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
  5397. ** xBegin method for that table.
  5398. **
  5399. ** Also, whether or not P4 is set, check that this is not being called from
  5400. ** within a callback to a virtual table xSync() method. If it is, the error
  5401. ** code will be set to SQLITE_LOCKED.
  5402. */
  5403. case OP_VBegin: {
  5404. VTable *pVTab;
  5405. pVTab = pOp->p4.pVtab;
  5406. rc = sqlite3VtabBegin(db, pVTab);
  5407. if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
  5408. break;
  5409. }
  5410. #endif /* SQLITE_OMIT_VIRTUALTABLE */
  5411. #ifndef SQLITE_OMIT_VIRTUALTABLE
  5412. /* Opcode: VCreate P1 * * P4 *
  5413. **
  5414. ** P4 is the name of a virtual table in database P1. Call the xCreate method
  5415. ** for that table.
  5416. */
  5417. case OP_VCreate: {
  5418. rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
  5419. break;
  5420. }
  5421. #endif /* SQLITE_OMIT_VIRTUALTABLE */
  5422. #ifndef SQLITE_OMIT_VIRTUALTABLE
  5423. /* Opcode: VDestroy P1 * * P4 *
  5424. **
  5425. ** P4 is the name of a virtual table in database P1. Call the xDestroy method
  5426. ** of that table.
  5427. */
  5428. case OP_VDestroy: {
  5429. p->inVtabMethod = 2;
  5430. rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
  5431. p->inVtabMethod = 0;
  5432. break;
  5433. }
  5434. #endif /* SQLITE_OMIT_VIRTUALTABLE */
  5435. #ifndef SQLITE_OMIT_VIRTUALTABLE
  5436. /* Opcode: VOpen P1 * * P4 *
  5437. **
  5438. ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
  5439. ** P1 is a cursor number. This opcode opens a cursor to the virtual
  5440. ** table and stores that cursor in P1.
  5441. */
  5442. case OP_VOpen: {
  5443. VdbeCursor *pCur;
  5444. sqlite3_vtab_cursor *pVtabCursor;
  5445. sqlite3_vtab *pVtab;
  5446. sqlite3_module *pModule;
  5447. assert( p->bIsReader );
  5448. pCur = 0;
  5449. pVtabCursor = 0;
  5450. pVtab = pOp->p4.pVtab->pVtab;
  5451. pModule = (sqlite3_module *)pVtab->pModule;
  5452. assert(pVtab && pModule);
  5453. rc = pModule->xOpen(pVtab, &pVtabCursor);
  5454. sqlite3VtabImportErrmsg(p, pVtab);
  5455. if( SQLITE_OK==rc ){
  5456. /* Initialize sqlite3_vtab_cursor base class */
  5457. pVtabCursor->pVtab = pVtab;
  5458. /* Initialize vdbe cursor object */
  5459. pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
  5460. if( pCur ){
  5461. pCur->pVtabCursor = pVtabCursor;
  5462. pCur->pModule = pVtabCursor->pVtab->pModule;
  5463. }else{
  5464. db->mallocFailed = 1;
  5465. pModule->xClose(pVtabCursor);
  5466. }
  5467. }
  5468. break;
  5469. }
  5470. #endif /* SQLITE_OMIT_VIRTUALTABLE */
  5471. #ifndef SQLITE_OMIT_VIRTUALTABLE
  5472. /* Opcode: VFilter P1 P2 P3 P4 *
  5473. **
  5474. ** P1 is a cursor opened using VOpen. P2 is an address to jump to if
  5475. ** the filtered result set is empty.
  5476. **
  5477. ** P4 is either NULL or a string that was generated by the xBestIndex
  5478. ** method of the module. The interpretation of the P4 string is left
  5479. ** to the module implementation.
  5480. **
  5481. ** This opcode invokes the xFilter method on the virtual table specified
  5482. ** by P1. The integer query plan parameter to xFilter is stored in register
  5483. ** P3. Register P3+1 stores the argc parameter to be passed to the
  5484. ** xFilter method. Registers P3+2..P3+1+argc are the argc
  5485. ** additional parameters which are passed to
  5486. ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
  5487. **
  5488. ** A jump is made to P2 if the result set after filtering would be empty.
  5489. */
  5490. case OP_VFilter: { /* jump */
  5491. int nArg;
  5492. int iQuery;
  5493. const sqlite3_module *pModule;
  5494. Mem *pQuery;
  5495. Mem *pArgc;
  5496. sqlite3_vtab_cursor *pVtabCursor;
  5497. sqlite3_vtab *pVtab;
  5498. VdbeCursor *pCur;
  5499. int res;
  5500. int i;
  5501. Mem **apArg;
  5502. pQuery = &aMem[pOp->p3];
  5503. pArgc = &pQuery[1];
  5504. pCur = p->apCsr[pOp->p1];
  5505. assert( memIsValid(pQuery) );
  5506. REGISTER_TRACE(pOp->p3, pQuery);
  5507. assert( pCur->pVtabCursor );
  5508. pVtabCursor = pCur->pVtabCursor;
  5509. pVtab = pVtabCursor->pVtab;
  5510. pModule = pVtab->pModule;
  5511. /* Grab the index number and argc parameters */
  5512. assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
  5513. nArg = (int)pArgc->u.i;
  5514. iQuery = (int)pQuery->u.i;
  5515. /* Invoke the xFilter method */
  5516. {
  5517. res = 0;
  5518. apArg = p->apArg;
  5519. for(i = 0; i<nArg; i++){
  5520. apArg[i] = &pArgc[i+1];
  5521. sqlite3VdbeMemStoreType(apArg[i]);
  5522. }
  5523. p->inVtabMethod = 1;
  5524. rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
  5525. p->inVtabMethod = 0;
  5526. sqlite3VtabImportErrmsg(p, pVtab);
  5527. if( rc==SQLITE_OK ){
  5528. res = pModule->xEof(pVtabCursor);
  5529. }
  5530. if( res ){
  5531. pc = pOp->p2 - 1;
  5532. }
  5533. }
  5534. pCur->nullRow = 0;
  5535. break;
  5536. }
  5537. #endif /* SQLITE_OMIT_VIRTUALTABLE */
  5538. #ifndef SQLITE_OMIT_VIRTUALTABLE
  5539. /* Opcode: VColumn P1 P2 P3 * *
  5540. **
  5541. ** Store the value of the P2-th column of
  5542. ** the row of the virtual-table that the
  5543. ** P1 cursor is pointing to into register P3.
  5544. */
  5545. case OP_VColumn: {
  5546. sqlite3_vtab *pVtab;
  5547. const sqlite3_module *pModule;
  5548. Mem *pDest;
  5549. sqlite3_context sContext;
  5550. VdbeCursor *pCur = p->apCsr[pOp->p1];
  5551. assert( pCur->pVtabCursor );
  5552. assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  5553. pDest = &aMem[pOp->p3];
  5554. memAboutToChange(p, pDest);
  5555. if( pCur->nullRow ){
  5556. sqlite3VdbeMemSetNull(pDest);
  5557. break;
  5558. }
  5559. pVtab = pCur->pVtabCursor->pVtab;
  5560. pModule = pVtab->pModule;
  5561. assert( pModule->xColumn );
  5562. memset(&sContext, 0, sizeof(sContext));
  5563. /* The output cell may already have a buffer allocated. Move
  5564. ** the current contents to sContext.s so in case the user-function
  5565. ** can use the already allocated buffer instead of allocating a
  5566. ** new one.
  5567. */
  5568. sqlite3VdbeMemMove(&sContext.s, pDest);
  5569. MemSetTypeFlag(&sContext.s, MEM_Null);
  5570. rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
  5571. sqlite3VtabImportErrmsg(p, pVtab);
  5572. if( sContext.isError ){
  5573. rc = sContext.isError;
  5574. }
  5575. /* Copy the result of the function to the P3 register. We
  5576. ** do this regardless of whether or not an error occurred to ensure any
  5577. ** dynamic allocation in sContext.s (a Mem struct) is released.
  5578. */
  5579. sqlite3VdbeChangeEncoding(&sContext.s, encoding);
  5580. sqlite3VdbeMemMove(pDest, &sContext.s);
  5581. REGISTER_TRACE(pOp->p3, pDest);
  5582. UPDATE_MAX_BLOBSIZE(pDest);
  5583. if( sqlite3VdbeMemTooBig(pDest) ){
  5584. goto too_big;
  5585. }
  5586. break;
  5587. }
  5588. #endif /* SQLITE_OMIT_VIRTUALTABLE */
  5589. #ifndef SQLITE_OMIT_VIRTUALTABLE
  5590. /* Opcode: VNext P1 P2 * * *
  5591. **
  5592. ** Advance virtual table P1 to the next row in its result set and
  5593. ** jump to instruction P2. Or, if the virtual table has reached
  5594. ** the end of its result set, then fall through to the next instruction.
  5595. */
  5596. case OP_VNext: { /* jump */
  5597. sqlite3_vtab *pVtab;
  5598. const sqlite3_module *pModule;
  5599. int res;
  5600. VdbeCursor *pCur;
  5601. res = 0;
  5602. pCur = p->apCsr[pOp->p1];
  5603. assert( pCur->pVtabCursor );
  5604. if( pCur->nullRow ){
  5605. break;
  5606. }
  5607. pVtab = pCur->pVtabCursor->pVtab;
  5608. pModule = pVtab->pModule;
  5609. assert( pModule->xNext );
  5610. /* Invoke the xNext() method of the module. There is no way for the
  5611. ** underlying implementation to return an error if one occurs during
  5612. ** xNext(). Instead, if an error occurs, true is returned (indicating that
  5613. ** data is available) and the error code returned when xColumn or
  5614. ** some other method is next invoked on the save virtual table cursor.
  5615. */
  5616. p->inVtabMethod = 1;
  5617. rc = pModule->xNext(pCur->pVtabCursor);
  5618. p->inVtabMethod = 0;
  5619. sqlite3VtabImportErrmsg(p, pVtab);
  5620. if( rc==SQLITE_OK ){
  5621. res = pModule->xEof(pCur->pVtabCursor);
  5622. }
  5623. if( !res ){
  5624. /* If there is data, jump to P2 */
  5625. pc = pOp->p2 - 1;
  5626. }
  5627. goto check_for_interrupt;
  5628. }
  5629. #endif /* SQLITE_OMIT_VIRTUALTABLE */
  5630. #ifndef SQLITE_OMIT_VIRTUALTABLE
  5631. /* Opcode: VRename P1 * * P4 *
  5632. **
  5633. ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
  5634. ** This opcode invokes the corresponding xRename method. The value
  5635. ** in register P1 is passed as the zName argument to the xRename method.
  5636. */
  5637. case OP_VRename: {
  5638. sqlite3_vtab *pVtab;
  5639. Mem *pName;
  5640. pVtab = pOp->p4.pVtab->pVtab;
  5641. pName = &aMem[pOp->p1];
  5642. assert( pVtab->pModule->xRename );
  5643. assert( memIsValid(pName) );
  5644. assert( p->readOnly==0 );
  5645. REGISTER_TRACE(pOp->p1, pName);
  5646. assert( pName->flags & MEM_Str );
  5647. testcase( pName->enc==SQLITE_UTF8 );
  5648. testcase( pName->enc==SQLITE_UTF16BE );
  5649. testcase( pName->enc==SQLITE_UTF16LE );
  5650. rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
  5651. if( rc==SQLITE_OK ){
  5652. rc = pVtab->pModule->xRename(pVtab, pName->z);
  5653. sqlite3VtabImportErrmsg(p, pVtab);
  5654. p->expired = 0;
  5655. }
  5656. break;
  5657. }
  5658. #endif
  5659. #ifndef SQLITE_OMIT_VIRTUALTABLE
  5660. /* Opcode: VUpdate P1 P2 P3 P4 *
  5661. **
  5662. ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
  5663. ** This opcode invokes the corresponding xUpdate method. P2 values
  5664. ** are contiguous memory cells starting at P3 to pass to the xUpdate
  5665. ** invocation. The value in register (P3+P2-1) corresponds to the
  5666. ** p2th element of the argv array passed to xUpdate.
  5667. **
  5668. ** The xUpdate method will do a DELETE or an INSERT or both.
  5669. ** The argv[0] element (which corresponds to memory cell P3)
  5670. ** is the rowid of a row to delete. If argv[0] is NULL then no
  5671. ** deletion occurs. The argv[1] element is the rowid of the new
  5672. ** row. This can be NULL to have the virtual table select the new
  5673. ** rowid for itself. The subsequent elements in the array are
  5674. ** the values of columns in the new row.
  5675. **
  5676. ** If P2==1 then no insert is performed. argv[0] is the rowid of
  5677. ** a row to delete.
  5678. **
  5679. ** P1 is a boolean flag. If it is set to true and the xUpdate call
  5680. ** is successful, then the value returned by sqlite3_last_insert_rowid()
  5681. ** is set to the value of the rowid for the row just inserted.
  5682. */
  5683. case OP_VUpdate: {
  5684. sqlite3_vtab *pVtab;
  5685. sqlite3_module *pModule;
  5686. int nArg;
  5687. int i;
  5688. sqlite_int64 rowid;
  5689. Mem **apArg;
  5690. Mem *pX;
  5691. assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
  5692. || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
  5693. );
  5694. assert( p->readOnly==0 );
  5695. pVtab = pOp->p4.pVtab->pVtab;
  5696. pModule = (sqlite3_module *)pVtab->pModule;
  5697. nArg = pOp->p2;
  5698. assert( pOp->p4type==P4_VTAB );
  5699. if( ALWAYS(pModule->xUpdate) ){
  5700. u8 vtabOnConflict = db->vtabOnConflict;
  5701. apArg = p->apArg;
  5702. pX = &aMem[pOp->p3];
  5703. for(i=0; i<nArg; i++){
  5704. assert( memIsValid(pX) );
  5705. memAboutToChange(p, pX);
  5706. sqlite3VdbeMemStoreType(pX);
  5707. apArg[i] = pX;
  5708. pX++;
  5709. }
  5710. db->vtabOnConflict = pOp->p5;
  5711. rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
  5712. db->vtabOnConflict = vtabOnConflict;
  5713. sqlite3VtabImportErrmsg(p, pVtab);
  5714. if( rc==SQLITE_OK && pOp->p1 ){
  5715. assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
  5716. db->lastRowid = lastRowid = rowid;
  5717. }
  5718. if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
  5719. if( pOp->p5==OE_Ignore ){
  5720. rc = SQLITE_OK;
  5721. }else{
  5722. p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
  5723. }
  5724. }else{
  5725. p->nChange++;
  5726. }
  5727. }
  5728. break;
  5729. }
  5730. #endif /* SQLITE_OMIT_VIRTUALTABLE */
  5731. #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  5732. /* Opcode: Pagecount P1 P2 * * *
  5733. **
  5734. ** Write the current number of pages in database P1 to memory cell P2.
  5735. */
  5736. case OP_Pagecount: { /* out2-prerelease */
  5737. pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
  5738. break;
  5739. }
  5740. #endif
  5741. #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  5742. /* Opcode: MaxPgcnt P1 P2 P3 * *
  5743. **
  5744. ** Try to set the maximum page count for database P1 to the value in P3.
  5745. ** Do not let the maximum page count fall below the current page count and
  5746. ** do not change the maximum page count value if P3==0.
  5747. **
  5748. ** Store the maximum page count after the change in register P2.
  5749. */
  5750. case OP_MaxPgcnt: { /* out2-prerelease */
  5751. unsigned int newMax;
  5752. Btree *pBt;
  5753. pBt = db->aDb[pOp->p1].pBt;
  5754. newMax = 0;
  5755. if( pOp->p3 ){
  5756. newMax = sqlite3BtreeLastPage(pBt);
  5757. if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
  5758. }
  5759. pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
  5760. break;
  5761. }
  5762. #endif
  5763. #ifndef SQLITE_OMIT_TRACE
  5764. /* Opcode: Trace * * * P4 *
  5765. **
  5766. ** If tracing is enabled (by the sqlite3_trace()) interface, then
  5767. ** the UTF-8 string contained in P4 is emitted on the trace callback.
  5768. */
  5769. case OP_Trace: {
  5770. char *zTrace;
  5771. char *z;
  5772. if( db->xTrace
  5773. && !p->doingRerun
  5774. && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
  5775. ){
  5776. z = sqlite3VdbeExpandSql(p, zTrace);
  5777. db->xTrace(db->pTraceArg, z);
  5778. sqlite3DbFree(db, z);
  5779. }
  5780. #ifdef SQLITE_DEBUG
  5781. if( (db->flags & SQLITE_SqlTrace)!=0
  5782. && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
  5783. ){
  5784. sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
  5785. }
  5786. #endif /* SQLITE_DEBUG */
  5787. break;
  5788. }
  5789. #endif
  5790. /* Opcode: Noop * * * * *
  5791. **
  5792. ** Do nothing. This instruction is often useful as a jump
  5793. ** destination.
  5794. */
  5795. /*
  5796. ** The magic Explain opcode are only inserted when explain==2 (which
  5797. ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
  5798. ** This opcode records information from the optimizer. It is the
  5799. ** the same as a no-op. This opcodesnever appears in a real VM program.
  5800. */
  5801. default: { /* This is really OP_Noop and OP_Explain */
  5802. assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
  5803. break;
  5804. }
  5805. /*****************************************************************************
  5806. ** The cases of the switch statement above this line should all be indented
  5807. ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
  5808. ** readability. From this point on down, the normal indentation rules are
  5809. ** restored.
  5810. *****************************************************************************/
  5811. }
  5812. #ifdef VDBE_PROFILE
  5813. {
  5814. u64 elapsed = sqlite3Hwtime() - start;
  5815. pOp->cycles += elapsed;
  5816. pOp->cnt++;
  5817. #if 0
  5818. fprintf(stdout, "%10llu ", elapsed);
  5819. sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
  5820. #endif
  5821. }
  5822. #endif
  5823. /* The following code adds nothing to the actual functionality
  5824. ** of the program. It is only here for testing and debugging.
  5825. ** On the other hand, it does burn CPU cycles every time through
  5826. ** the evaluator loop. So we can leave it out when NDEBUG is defined.
  5827. */
  5828. #ifndef NDEBUG
  5829. assert( pc>=-1 && pc<p->nOp );
  5830. #ifdef SQLITE_DEBUG
  5831. if( p->trace ){
  5832. if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
  5833. if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
  5834. registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
  5835. }
  5836. if( pOp->opflags & OPFLG_OUT3 ){
  5837. registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
  5838. }
  5839. }
  5840. #endif /* SQLITE_DEBUG */
  5841. #endif /* NDEBUG */
  5842. } /* The end of the for(;;) loop the loops through opcodes */
  5843. /* If we reach this point, it means that execution is finished with
  5844. ** an error of some kind.
  5845. */
  5846. vdbe_error_halt:
  5847. assert( rc );
  5848. p->rc = rc;
  5849. testcase( sqlite3GlobalConfig.xLog!=0 );
  5850. sqlite3_log(rc, "statement aborts at %d: [%s] %s",
  5851. pc, p->zSql, p->zErrMsg);
  5852. sqlite3VdbeHalt(p);
  5853. if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
  5854. rc = SQLITE_ERROR;
  5855. if( resetSchemaOnFault>0 ){
  5856. sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
  5857. }
  5858. /* This is the only way out of this procedure. We have to
  5859. ** release the mutexes on btrees that were acquired at the
  5860. ** top. */
  5861. vdbe_return:
  5862. db->lastRowid = lastRowid;
  5863. testcase( nVmStep>0 );
  5864. p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
  5865. sqlite3VdbeLeave(p);
  5866. return rc;
  5867. /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
  5868. ** is encountered.
  5869. */
  5870. too_big:
  5871. sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
  5872. rc = SQLITE_TOOBIG;
  5873. goto vdbe_error_halt;
  5874. /* Jump to here if a malloc() fails.
  5875. */
  5876. no_mem:
  5877. db->mallocFailed = 1;
  5878. sqlite3SetString(&p->zErrMsg, db, "out of memory");
  5879. rc = SQLITE_NOMEM;
  5880. goto vdbe_error_halt;
  5881. /* Jump to here for any other kind of fatal error. The "rc" variable
  5882. ** should hold the error number.
  5883. */
  5884. abort_due_to_error:
  5885. assert( p->zErrMsg==0 );
  5886. if( db->mallocFailed ) rc = SQLITE_NOMEM;
  5887. if( rc!=SQLITE_IOERR_NOMEM ){
  5888. sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
  5889. }
  5890. goto vdbe_error_halt;
  5891. /* Jump to here if the sqlite3_interrupt() API sets the interrupt
  5892. ** flag.
  5893. */
  5894. abort_due_to_interrupt:
  5895. assert( db->u1.isInterrupted );
  5896. rc = SQLITE_INTERRUPT;
  5897. p->rc = rc;
  5898. sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
  5899. goto vdbe_error_halt;
  5900. }