vdbeaux.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261
  1. /*
  2. ** 2003 September 6
  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. ** This file contains code used for creating, destroying, and populating
  13. ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
  14. ** to version 2.8.7, all this code was combined into the vdbe.c source file.
  15. ** But that file was getting too big so this subroutines were split out.
  16. */
  17. #include "sqliteInt.h"
  18. #include <ctype.h>
  19. #include "vdbeInt.h"
  20. /*
  21. ** When debugging the code generator in a symbolic debugger, one can
  22. ** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed
  23. ** as they are added to the instruction stream.
  24. */
  25. #ifdef SQLITE_DEBUG
  26. int sqlite3_vdbe_addop_trace = 0;
  27. #endif
  28. /*
  29. ** Create a new virtual database engine.
  30. */
  31. Vdbe *sqlite3VdbeCreate(sqlite3 *db){
  32. Vdbe *p;
  33. p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
  34. if( p==0 ) return 0;
  35. p->db = db;
  36. if( db->pVdbe ){
  37. db->pVdbe->pPrev = p;
  38. }
  39. p->pNext = db->pVdbe;
  40. p->pPrev = 0;
  41. db->pVdbe = p;
  42. p->magic = VDBE_MAGIC_INIT;
  43. return p;
  44. }
  45. /*
  46. ** Remember the SQL string for a prepared statement.
  47. */
  48. void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){
  49. if( p==0 ) return;
  50. assert( p->zSql==0 );
  51. p->zSql = sqlite3DbStrNDup(p->db, z, n);
  52. }
  53. /*
  54. ** Return the SQL associated with a prepared statement
  55. */
  56. const char *sqlite3_sql(sqlite3_stmt *pStmt){
  57. return ((Vdbe *)pStmt)->zSql;
  58. }
  59. /*
  60. ** Swap all content between two VDBE structures.
  61. */
  62. void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
  63. Vdbe tmp, *pTmp;
  64. char *zTmp;
  65. int nTmp;
  66. tmp = *pA;
  67. *pA = *pB;
  68. *pB = tmp;
  69. pTmp = pA->pNext;
  70. pA->pNext = pB->pNext;
  71. pB->pNext = pTmp;
  72. pTmp = pA->pPrev;
  73. pA->pPrev = pB->pPrev;
  74. pB->pPrev = pTmp;
  75. zTmp = pA->zSql;
  76. pA->zSql = pB->zSql;
  77. pB->zSql = zTmp;
  78. nTmp = pA->nSql;
  79. pA->nSql = pB->nSql;
  80. pB->nSql = nTmp;
  81. }
  82. #ifdef SQLITE_DEBUG
  83. /*
  84. ** Turn tracing on or off
  85. */
  86. void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
  87. p->trace = trace;
  88. }
  89. #endif
  90. /*
  91. ** Resize the Vdbe.aOp array so that it contains at least N
  92. ** elements.
  93. **
  94. ** If an out-of-memory error occurs while resizing the array,
  95. ** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that
  96. ** any opcodes already allocated can be correctly deallocated
  97. ** along with the rest of the Vdbe).
  98. */
  99. static void resizeOpArray(Vdbe *p, int N){
  100. VdbeOp *pNew;
  101. int oldSize = p->nOpAlloc;
  102. pNew = sqlite3DbRealloc(p->db, p->aOp, N*sizeof(Op));
  103. if( pNew ){
  104. p->nOpAlloc = N;
  105. p->aOp = pNew;
  106. if( N>oldSize ){
  107. memset(&p->aOp[oldSize], 0, (N-oldSize)*sizeof(Op));
  108. }
  109. }
  110. }
  111. /*
  112. ** Add a new instruction to the list of instructions current in the
  113. ** VDBE. Return the address of the new instruction.
  114. **
  115. ** Parameters:
  116. **
  117. ** p Pointer to the VDBE
  118. **
  119. ** op The opcode for this instruction
  120. **
  121. ** p1, p2 First two of the three possible operands.
  122. **
  123. ** Use the sqlite3VdbeResolveLabel() function to fix an address and
  124. ** the sqlite3VdbeChangeP3() function to change the value of the P3
  125. ** operand.
  126. */
  127. int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
  128. int i;
  129. VdbeOp *pOp;
  130. i = p->nOp;
  131. assert( p->magic==VDBE_MAGIC_INIT );
  132. if( p->nOpAlloc<=i ){
  133. resizeOpArray(p, p->nOpAlloc*2 + 100);
  134. if( p->db->mallocFailed ){
  135. return 0;
  136. }
  137. }
  138. p->nOp++;
  139. pOp = &p->aOp[i];
  140. pOp->opcode = op;
  141. pOp->p1 = p1;
  142. pOp->p2 = p2;
  143. pOp->p3 = 0;
  144. pOp->p3type = P3_NOTUSED;
  145. p->expired = 0;
  146. #ifdef SQLITE_DEBUG
  147. if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
  148. #endif
  149. return i;
  150. }
  151. /*
  152. ** Add an opcode that includes the p3 value.
  153. */
  154. int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
  155. int addr = sqlite3VdbeAddOp(p, op, p1, p2);
  156. sqlite3VdbeChangeP3(p, addr, zP3, p3type);
  157. return addr;
  158. }
  159. /*
  160. ** Create a new symbolic label for an instruction that has yet to be
  161. ** coded. The symbolic label is really just a negative number. The
  162. ** label can be used as the P2 value of an operation. Later, when
  163. ** the label is resolved to a specific address, the VDBE will scan
  164. ** through its operation list and change all values of P2 which match
  165. ** the label into the resolved address.
  166. **
  167. ** The VDBE knows that a P2 value is a label because labels are
  168. ** always negative and P2 values are suppose to be non-negative.
  169. ** Hence, a negative P2 value is a label that has yet to be resolved.
  170. **
  171. ** Zero is returned if a malloc() fails.
  172. */
  173. int sqlite3VdbeMakeLabel(Vdbe *p){
  174. int i;
  175. i = p->nLabel++;
  176. assert( p->magic==VDBE_MAGIC_INIT );
  177. if( i>=p->nLabelAlloc ){
  178. p->nLabelAlloc = p->nLabelAlloc*2 + 10;
  179. p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
  180. p->nLabelAlloc*sizeof(p->aLabel[0]));
  181. }
  182. if( p->aLabel ){
  183. p->aLabel[i] = -1;
  184. }
  185. return -1-i;
  186. }
  187. /*
  188. ** Resolve label "x" to be the address of the next instruction to
  189. ** be inserted. The parameter "x" must have been obtained from
  190. ** a prior call to sqlite3VdbeMakeLabel().
  191. */
  192. void sqlite3VdbeResolveLabel(Vdbe *p, int x){
  193. int j = -1-x;
  194. assert( p->magic==VDBE_MAGIC_INIT );
  195. assert( j>=0 && j<p->nLabel );
  196. if( p->aLabel ){
  197. p->aLabel[j] = p->nOp;
  198. }
  199. }
  200. /*
  201. ** Return non-zero if opcode 'op' is guarenteed not to push more values
  202. ** onto the VDBE stack than it pops off.
  203. */
  204. static int opcodeNoPush(u8 op){
  205. /* The 10 NOPUSH_MASK_n constants are defined in the automatically
  206. ** generated header file opcodes.h. Each is a 16-bit bitmask, one
  207. ** bit corresponding to each opcode implemented by the virtual
  208. ** machine in vdbe.c. The bit is true if the word "no-push" appears
  209. ** in a comment on the same line as the "case OP_XXX:" in
  210. ** sqlite3VdbeExec() in vdbe.c.
  211. **
  212. ** If the bit is true, then the corresponding opcode is guarenteed not
  213. ** to grow the stack when it is executed. Otherwise, it may grow the
  214. ** stack by at most one entry.
  215. **
  216. ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains
  217. ** one bit for opcodes 16 to 31, and so on.
  218. **
  219. ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h
  220. ** because the file is generated by an awk program. Awk manipulates
  221. ** all numbers as floating-point and we don't want to risk a rounding
  222. ** error if someone builds with an awk that uses (for example) 32-bit
  223. ** IEEE floats.
  224. */
  225. static const u32 masks[5] = {
  226. NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16),
  227. NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16),
  228. NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16),
  229. NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16),
  230. NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16)
  231. };
  232. assert( op<32*5 );
  233. return (masks[op>>5] & (1<<(op&0x1F)));
  234. }
  235. #ifndef NDEBUG
  236. int sqlite3VdbeOpcodeNoPush(u8 op){
  237. return opcodeNoPush(op);
  238. }
  239. #endif
  240. /*
  241. ** Loop through the program looking for P2 values that are negative.
  242. ** Each such value is a label. Resolve the label by setting the P2
  243. ** value to its correct non-zero value.
  244. **
  245. ** This routine is called once after all opcodes have been inserted.
  246. **
  247. ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
  248. ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
  249. ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
  250. **
  251. ** The integer *pMaxStack is set to the maximum number of vdbe stack
  252. ** entries that static analysis reveals this program might need.
  253. **
  254. ** This routine also does the following optimization: It scans for
  255. ** instructions that might cause a statement rollback. Such instructions
  256. ** are:
  257. **
  258. ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
  259. ** * OP_Destroy
  260. ** * OP_VUpdate
  261. ** * OP_VRename
  262. **
  263. ** If no such instruction is found, then every Statement instruction
  264. ** is changed to a Noop. In this way, we avoid creating the statement
  265. ** journal file unnecessarily.
  266. */
  267. static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){
  268. int i;
  269. int nMaxArgs = 0;
  270. int nMaxStack = p->nOp;
  271. Op *pOp;
  272. int *aLabel = p->aLabel;
  273. int doesStatementRollback = 0;
  274. int hasStatementBegin = 0;
  275. for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
  276. u8 opcode = pOp->opcode;
  277. if( opcode==OP_Function || opcode==OP_AggStep
  278. #ifndef SQLITE_OMIT_VIRTUALTABLE
  279. || opcode==OP_VUpdate
  280. #endif
  281. ){
  282. if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
  283. }
  284. if( opcode==OP_Halt ){
  285. if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
  286. doesStatementRollback = 1;
  287. }
  288. }else if( opcode==OP_Statement ){
  289. hasStatementBegin = 1;
  290. }else if( opcode==OP_Destroy ){
  291. doesStatementRollback = 1;
  292. #ifndef SQLITE_OMIT_VIRTUALTABLE
  293. }else if( opcode==OP_VUpdate || opcode==OP_VRename ){
  294. doesStatementRollback = 1;
  295. }else if( opcode==OP_VFilter ){
  296. int n;
  297. assert( p->nOp - i >= 3 );
  298. assert( pOp[-2].opcode==OP_Integer );
  299. n = pOp[-2].p1;
  300. if( n>nMaxArgs ) nMaxArgs = n;
  301. #endif
  302. }
  303. if( opcodeNoPush(opcode) ){
  304. nMaxStack--;
  305. }
  306. if( pOp->p2>=0 ) continue;
  307. assert( -1-pOp->p2<p->nLabel );
  308. pOp->p2 = aLabel[-1-pOp->p2];
  309. }
  310. sqlite3_free(p->aLabel);
  311. p->aLabel = 0;
  312. *pMaxFuncArgs = nMaxArgs;
  313. *pMaxStack = nMaxStack;
  314. /* If we never rollback a statement transaction, then statement
  315. ** transactions are not needed. So change every OP_Statement
  316. ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
  317. ** which can be expensive on some platforms.
  318. */
  319. if( hasStatementBegin && !doesStatementRollback ){
  320. for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
  321. if( pOp->opcode==OP_Statement ){
  322. pOp->opcode = OP_Noop;
  323. }
  324. }
  325. }
  326. }
  327. /*
  328. ** Return the address of the next instruction to be inserted.
  329. */
  330. int sqlite3VdbeCurrentAddr(Vdbe *p){
  331. assert( p->magic==VDBE_MAGIC_INIT );
  332. return p->nOp;
  333. }
  334. /*
  335. ** Add a whole list of operations to the operation stack. Return the
  336. ** address of the first operation added.
  337. */
  338. int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
  339. int addr;
  340. assert( p->magic==VDBE_MAGIC_INIT );
  341. if( p->nOp + nOp > p->nOpAlloc ){
  342. resizeOpArray(p, p->nOp*2 + nOp);
  343. }
  344. if( p->db->mallocFailed ){
  345. return 0;
  346. }
  347. addr = p->nOp;
  348. if( nOp>0 ){
  349. int i;
  350. VdbeOpList const *pIn = aOp;
  351. for(i=0; i<nOp; i++, pIn++){
  352. int p2 = pIn->p2;
  353. VdbeOp *pOut = &p->aOp[i+addr];
  354. pOut->opcode = pIn->opcode;
  355. pOut->p1 = pIn->p1;
  356. pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
  357. pOut->p3 = pIn->p3;
  358. pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
  359. #ifdef SQLITE_DEBUG
  360. if( sqlite3_vdbe_addop_trace ){
  361. sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
  362. }
  363. #endif
  364. }
  365. p->nOp += nOp;
  366. }
  367. return addr;
  368. }
  369. /*
  370. ** Change the value of the P1 operand for a specific instruction.
  371. ** This routine is useful when a large program is loaded from a
  372. ** static array using sqlite3VdbeAddOpList but we want to make a
  373. ** few minor changes to the program.
  374. */
  375. void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
  376. assert( p==0 || p->magic==VDBE_MAGIC_INIT );
  377. if( p && addr>=0 && p->nOp>addr && p->aOp ){
  378. p->aOp[addr].p1 = val;
  379. }
  380. }
  381. /*
  382. ** Change the value of the P2 operand for a specific instruction.
  383. ** This routine is useful for setting a jump destination.
  384. */
  385. void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
  386. assert( val>=0 );
  387. assert( p==0 || p->magic==VDBE_MAGIC_INIT );
  388. if( p && addr>=0 && p->nOp>addr && p->aOp ){
  389. p->aOp[addr].p2 = val;
  390. }
  391. }
  392. /*
  393. ** Change the P2 operand of instruction addr so that it points to
  394. ** the address of the next instruction to be coded.
  395. */
  396. void sqlite3VdbeJumpHere(Vdbe *p, int addr){
  397. sqlite3VdbeChangeP2(p, addr, p->nOp);
  398. }
  399. /*
  400. ** If the input FuncDef structure is ephemeral, then free it. If
  401. ** the FuncDef is not ephermal, then do nothing.
  402. */
  403. static void freeEphemeralFunction(FuncDef *pDef){
  404. if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
  405. sqlite3_free(pDef);
  406. }
  407. }
  408. /*
  409. ** Delete a P3 value if necessary.
  410. */
  411. static void freeP3(int p3type, void *p3){
  412. if( p3 ){
  413. switch( p3type ){
  414. case P3_REAL:
  415. case P3_INT64:
  416. case P3_MPRINTF:
  417. case P3_DYNAMIC:
  418. case P3_KEYINFO:
  419. case P3_KEYINFO_HANDOFF: {
  420. sqlite3_free(p3);
  421. break;
  422. }
  423. case P3_VDBEFUNC: {
  424. VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
  425. freeEphemeralFunction(pVdbeFunc->pFunc);
  426. sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
  427. sqlite3_free(pVdbeFunc);
  428. break;
  429. }
  430. case P3_FUNCDEF: {
  431. freeEphemeralFunction((FuncDef*)p3);
  432. break;
  433. }
  434. case P3_MEM: {
  435. sqlite3ValueFree((sqlite3_value*)p3);
  436. break;
  437. }
  438. }
  439. }
  440. }
  441. /*
  442. ** Change N opcodes starting at addr to No-ops.
  443. */
  444. void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
  445. if( p && p->aOp ){
  446. VdbeOp *pOp = &p->aOp[addr];
  447. while( N-- ){
  448. freeP3(pOp->p3type, pOp->p3);
  449. memset(pOp, 0, sizeof(pOp[0]));
  450. pOp->opcode = OP_Noop;
  451. pOp++;
  452. }
  453. }
  454. }
  455. /*
  456. ** Change the value of the P3 operand for a specific instruction.
  457. ** This routine is useful when a large program is loaded from a
  458. ** static array using sqlite3VdbeAddOpList but we want to make a
  459. ** few minor changes to the program.
  460. **
  461. ** If n>=0 then the P3 operand is dynamic, meaning that a copy of
  462. ** the string is made into memory obtained from sqlite3_malloc().
  463. ** A value of n==0 means copy bytes of zP3 up to and including the
  464. ** first null byte. If n>0 then copy n+1 bytes of zP3.
  465. **
  466. ** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure.
  467. ** A copy is made of the KeyInfo structure into memory obtained from
  468. ** sqlite3_malloc, to be freed when the Vdbe is finalized.
  469. ** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure
  470. ** stored in memory that the caller has obtained from sqlite3_malloc. The
  471. ** caller should not free the allocation, it will be freed when the Vdbe is
  472. ** finalized.
  473. **
  474. ** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points
  475. ** to a string or structure that is guaranteed to exist for the lifetime of
  476. ** the Vdbe. In these cases we can just copy the pointer.
  477. **
  478. ** If addr<0 then change P3 on the most recently inserted instruction.
  479. */
  480. void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
  481. Op *pOp;
  482. assert( p==0 || p->magic==VDBE_MAGIC_INIT );
  483. if( p==0 || p->aOp==0 || p->db->mallocFailed ){
  484. if (n != P3_KEYINFO) {
  485. freeP3(n, (void*)*(char**)&zP3);
  486. }
  487. return;
  488. }
  489. if( addr<0 || addr>=p->nOp ){
  490. addr = p->nOp - 1;
  491. if( addr<0 ) return;
  492. }
  493. pOp = &p->aOp[addr];
  494. freeP3(pOp->p3type, pOp->p3);
  495. pOp->p3 = 0;
  496. if( zP3==0 ){
  497. pOp->p3 = 0;
  498. pOp->p3type = P3_NOTUSED;
  499. }else if( n==P3_KEYINFO ){
  500. KeyInfo *pKeyInfo;
  501. int nField, nByte;
  502. nField = ((KeyInfo*)zP3)->nField;
  503. nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
  504. pKeyInfo = sqlite3_malloc( nByte );
  505. pOp->p3 = (char*)pKeyInfo;
  506. if( pKeyInfo ){
  507. unsigned char *aSortOrder;
  508. memcpy(pKeyInfo, zP3, nByte);
  509. aSortOrder = pKeyInfo->aSortOrder;
  510. if( aSortOrder ){
  511. pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
  512. memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
  513. }
  514. pOp->p3type = P3_KEYINFO;
  515. }else{
  516. p->db->mallocFailed = 1;
  517. pOp->p3type = P3_NOTUSED;
  518. }
  519. }else if( n==P3_KEYINFO_HANDOFF ){
  520. pOp->p3 = (char*)zP3;
  521. pOp->p3type = P3_KEYINFO;
  522. }else if( n<0 ){
  523. pOp->p3 = (char*)zP3;
  524. pOp->p3type = n;
  525. }else{
  526. if( n==0 ) n = strlen(zP3);
  527. pOp->p3 = sqlite3DbStrNDup(p->db, zP3, n);
  528. pOp->p3type = P3_DYNAMIC;
  529. }
  530. }
  531. #ifndef NDEBUG
  532. /*
  533. ** Replace the P3 field of the most recently coded instruction with
  534. ** comment text.
  535. */
  536. void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
  537. va_list ap;
  538. assert( p->nOp>0 || p->aOp==0 );
  539. assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || p->db->mallocFailed );
  540. va_start(ap, zFormat);
  541. sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(p->db, zFormat, ap), P3_DYNAMIC);
  542. va_end(ap);
  543. }
  544. #endif
  545. /*
  546. ** Return the opcode for a given address.
  547. */
  548. VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
  549. assert( p->magic==VDBE_MAGIC_INIT );
  550. assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
  551. return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0);
  552. }
  553. #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
  554. || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
  555. /*
  556. ** Compute a string that describes the P3 parameter for an opcode.
  557. ** Use zTemp for any required temporary buffer space.
  558. */
  559. static char *displayP3(Op *pOp, char *zTemp, int nTemp){
  560. char *zP3;
  561. assert( nTemp>=20 );
  562. switch( pOp->p3type ){
  563. case P3_KEYINFO: {
  564. int i, j;
  565. KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
  566. sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
  567. i = strlen(zTemp);
  568. for(j=0; j<pKeyInfo->nField; j++){
  569. CollSeq *pColl = pKeyInfo->aColl[j];
  570. if( pColl ){
  571. int n = strlen(pColl->zName);
  572. if( i+n>nTemp-6 ){
  573. memcpy(&zTemp[i],",...",4);
  574. break;
  575. }
  576. zTemp[i++] = ',';
  577. if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
  578. zTemp[i++] = '-';
  579. }
  580. memcpy(&zTemp[i], pColl->zName,n+1);
  581. i += n;
  582. }else if( i+4<nTemp-6 ){
  583. memcpy(&zTemp[i],",nil",4);
  584. i += 4;
  585. }
  586. }
  587. zTemp[i++] = ')';
  588. zTemp[i] = 0;
  589. assert( i<nTemp );
  590. zP3 = zTemp;
  591. break;
  592. }
  593. case P3_COLLSEQ: {
  594. CollSeq *pColl = (CollSeq*)pOp->p3;
  595. sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
  596. zP3 = zTemp;
  597. break;
  598. }
  599. case P3_FUNCDEF: {
  600. FuncDef *pDef = (FuncDef*)pOp->p3;
  601. sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
  602. zP3 = zTemp;
  603. break;
  604. }
  605. case P3_INT64: {
  606. sqlite3_snprintf(nTemp, zTemp, "%lld", *(sqlite3_int64*)pOp->p3);
  607. zP3 = zTemp;
  608. break;
  609. }
  610. case P3_REAL: {
  611. sqlite3_snprintf(nTemp, zTemp, "%.16g", *(double*)pOp->p3);
  612. zP3 = zTemp;
  613. break;
  614. }
  615. #ifndef SQLITE_OMIT_VIRTUALTABLE
  616. case P3_VTAB: {
  617. sqlite3_vtab *pVtab = (sqlite3_vtab*)pOp->p3;
  618. sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
  619. zP3 = zTemp;
  620. break;
  621. }
  622. #endif
  623. default: {
  624. zP3 = pOp->p3;
  625. if( zP3==0 || pOp->opcode==OP_Noop ){
  626. zP3 = "";
  627. }
  628. }
  629. }
  630. assert( zP3!=0 );
  631. return zP3;
  632. }
  633. #endif
  634. /*
  635. ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
  636. **
  637. */
  638. void sqlite3VdbeUsesBtree(Vdbe *p, int i){
  639. int mask;
  640. assert( i>=0 && i<p->db->nDb );
  641. assert( i<sizeof(p->btreeMask)*8 );
  642. mask = 1<<i;
  643. if( (p->btreeMask & mask)==0 ){
  644. p->btreeMask |= mask;
  645. sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
  646. }
  647. }
  648. #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
  649. /*
  650. ** Print a single opcode. This routine is used for debugging only.
  651. */
  652. void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
  653. char *zP3;
  654. char zPtr[50];
  655. static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
  656. if( pOut==0 ) pOut = stdout;
  657. zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
  658. fprintf(pOut, zFormat1,
  659. pc, sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, zP3);
  660. fflush(pOut);
  661. }
  662. #endif
  663. /*
  664. ** Release an array of N Mem elements
  665. */
  666. static void releaseMemArray(Mem *p, int N){
  667. if( p ){
  668. while( N-->0 ){
  669. assert( N<2 || p[0].db==p[1].db );
  670. sqlite3VdbeMemRelease(p++);
  671. }
  672. }
  673. }
  674. #ifndef SQLITE_OMIT_EXPLAIN
  675. /*
  676. ** Give a listing of the program in the virtual machine.
  677. **
  678. ** The interface is the same as sqlite3VdbeExec(). But instead of
  679. ** running the code, it invokes the callback once for each instruction.
  680. ** This feature is used to implement "EXPLAIN".
  681. */
  682. int sqlite3VdbeList(
  683. Vdbe *p /* The VDBE */
  684. ){
  685. sqlite3 *db = p->db;
  686. int i;
  687. int rc = SQLITE_OK;
  688. assert( p->explain );
  689. if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
  690. assert( db->magic==SQLITE_MAGIC_BUSY );
  691. assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
  692. /* Even though this opcode does not put dynamic strings onto the
  693. ** the stack, they may become dynamic if the user calls
  694. ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
  695. */
  696. if( p->pTos==&p->aStack[4] ){
  697. releaseMemArray(p->aStack, 5);
  698. }
  699. p->resOnStack = 0;
  700. do{
  701. i = p->pc++;
  702. }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
  703. if( i>=p->nOp ){
  704. p->rc = SQLITE_OK;
  705. rc = SQLITE_DONE;
  706. }else if( db->u1.isInterrupted ){
  707. p->rc = SQLITE_INTERRUPT;
  708. rc = SQLITE_ERROR;
  709. sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
  710. }else{
  711. Op *pOp = &p->aOp[i];
  712. Mem *pMem = p->aStack;
  713. pMem->flags = MEM_Int;
  714. pMem->type = SQLITE_INTEGER;
  715. pMem->u.i = i; /* Program counter */
  716. pMem++;
  717. pMem->flags = MEM_Static|MEM_Str|MEM_Term;
  718. pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
  719. assert( pMem->z!=0 );
  720. pMem->n = strlen(pMem->z);
  721. pMem->type = SQLITE_TEXT;
  722. pMem->enc = SQLITE_UTF8;
  723. pMem++;
  724. pMem->flags = MEM_Int;
  725. pMem->u.i = pOp->p1; /* P1 */
  726. pMem->type = SQLITE_INTEGER;
  727. pMem++;
  728. pMem->flags = MEM_Int;
  729. pMem->u.i = pOp->p2; /* P2 */
  730. pMem->type = SQLITE_INTEGER;
  731. pMem++;
  732. pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P3 */
  733. pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
  734. assert( pMem->z!=0 );
  735. pMem->n = strlen(pMem->z);
  736. pMem->type = SQLITE_TEXT;
  737. pMem->enc = SQLITE_UTF8;
  738. p->nResColumn = 5 - 2*(p->explain-1);
  739. p->pTos = pMem;
  740. p->rc = SQLITE_OK;
  741. p->resOnStack = 1;
  742. rc = SQLITE_ROW;
  743. }
  744. return rc;
  745. }
  746. #endif /* SQLITE_OMIT_EXPLAIN */
  747. #ifdef SQLITE_DEBUG
  748. /*
  749. ** Print the SQL that was used to generate a VDBE program.
  750. */
  751. void sqlite3VdbePrintSql(Vdbe *p){
  752. int nOp = p->nOp;
  753. VdbeOp *pOp;
  754. if( nOp<1 ) return;
  755. pOp = &p->aOp[nOp-1];
  756. if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
  757. const char *z = pOp->p3;
  758. while( isspace(*(u8*)z) ) z++;
  759. printf("SQL: [%s]\n", z);
  760. }
  761. }
  762. #endif
  763. #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
  764. /*
  765. ** Print an IOTRACE message showing SQL content.
  766. */
  767. void sqlite3VdbeIOTraceSql(Vdbe *p){
  768. int nOp = p->nOp;
  769. VdbeOp *pOp;
  770. if( sqlite3_io_trace==0 ) return;
  771. if( nOp<1 ) return;
  772. pOp = &p->aOp[nOp-1];
  773. if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
  774. int i, j;
  775. char z[1000];
  776. sqlite3_snprintf(sizeof(z), z, "%s", pOp->p3);
  777. for(i=0; isspace((unsigned char)z[i]); i++){}
  778. for(j=0; z[i]; i++){
  779. if( isspace((unsigned char)z[i]) ){
  780. if( z[i-1]!=' ' ){
  781. z[j++] = ' ';
  782. }
  783. }else{
  784. z[j++] = z[i];
  785. }
  786. }
  787. z[j] = 0;
  788. sqlite3_io_trace("SQL %s\n", z);
  789. }
  790. }
  791. #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
  792. /*
  793. ** Prepare a virtual machine for execution. This involves things such
  794. ** as allocating stack space and initializing the program counter.
  795. ** After the VDBE has be prepped, it can be executed by one or more
  796. ** calls to sqlite3VdbeExec().
  797. **
  798. ** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
  799. ** VDBE_MAGIC_RUN.
  800. */
  801. void sqlite3VdbeMakeReady(
  802. Vdbe *p, /* The VDBE */
  803. int nVar, /* Number of '?' see in the SQL statement */
  804. int nMem, /* Number of memory cells to allocate */
  805. int nCursor, /* Number of cursors to allocate */
  806. int isExplain /* True if the EXPLAIN keywords is present */
  807. ){
  808. int n;
  809. sqlite3 *db = p->db;
  810. assert( p!=0 );
  811. assert( p->magic==VDBE_MAGIC_INIT );
  812. /* There should be at least one opcode.
  813. */
  814. assert( p->nOp>0 );
  815. /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
  816. * is because the call to resizeOpArray() below may shrink the
  817. * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
  818. * state.
  819. */
  820. p->magic = VDBE_MAGIC_RUN;
  821. /* No instruction ever pushes more than a single element onto the
  822. ** stack. And the stack never grows on successive executions of the
  823. ** same loop. So the total number of instructions is an upper bound
  824. ** on the maximum stack depth required. (Added later:) The
  825. ** resolveP2Values() call computes a tighter upper bound on the
  826. ** stack size.
  827. **
  828. ** Allocation all the stack space we will ever need.
  829. */
  830. if( p->aStack==0 ){
  831. int nArg; /* Maximum number of args passed to a user function. */
  832. int nStack; /* Maximum number of stack entries required */
  833. resolveP2Values(p, &nArg, &nStack);
  834. resizeOpArray(p, p->nOp);
  835. assert( nVar>=0 );
  836. assert( nStack<p->nOp );
  837. if( isExplain ){
  838. nStack = 10;
  839. }
  840. p->aStack = sqlite3DbMallocZero(db,
  841. nStack*sizeof(p->aStack[0]) /* aStack */
  842. + nArg*sizeof(Mem*) /* apArg */
  843. + nVar*sizeof(Mem) /* aVar */
  844. + nVar*sizeof(char*) /* azVar */
  845. + nMem*sizeof(Mem) /* aMem */
  846. + nCursor*sizeof(Cursor*) /* apCsr */
  847. );
  848. if( !db->mallocFailed ){
  849. p->aMem = &p->aStack[nStack];
  850. p->nMem = nMem;
  851. p->aVar = &p->aMem[nMem];
  852. p->nVar = nVar;
  853. p->okVar = 0;
  854. p->apArg = (Mem**)&p->aVar[nVar];
  855. p->azVar = (char**)&p->apArg[nArg];
  856. p->apCsr = (Cursor**)&p->azVar[nVar];
  857. p->nCursor = nCursor;
  858. for(n=0; n<nVar; n++){
  859. p->aVar[n].flags = MEM_Null;
  860. p->aVar[n].db = db;
  861. }
  862. for(n=0; n<nStack; n++){
  863. p->aStack[n].db = db;
  864. }
  865. }
  866. }
  867. for(n=0; n<p->nMem; n++){
  868. p->aMem[n].flags = MEM_Null;
  869. p->aMem[n].db = db;
  870. }
  871. p->pTos = &p->aStack[-1];
  872. p->pc = -1;
  873. p->rc = SQLITE_OK;
  874. p->uniqueCnt = 0;
  875. p->returnDepth = 0;
  876. p->errorAction = OE_Abort;
  877. p->popStack = 0;
  878. p->explain |= isExplain;
  879. p->magic = VDBE_MAGIC_RUN;
  880. p->nChange = 0;
  881. p->cacheCtr = 1;
  882. p->minWriteFileFormat = 255;
  883. p->openedStatement = 0;
  884. #ifdef VDBE_PROFILE
  885. {
  886. int i;
  887. for(i=0; i<p->nOp; i++){
  888. p->aOp[i].cnt = 0;
  889. p->aOp[i].cycles = 0;
  890. }
  891. }
  892. #endif
  893. }
  894. /*
  895. ** Close a VDBE cursor and release all the resources that cursor happens
  896. ** to hold.
  897. */
  898. void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){
  899. if( pCx==0 ){
  900. return;
  901. }
  902. if( pCx->pCursor ){
  903. sqlite3BtreeCloseCursor(pCx->pCursor);
  904. }
  905. if( pCx->pBt ){
  906. sqlite3BtreeClose(pCx->pBt);
  907. }
  908. #ifndef SQLITE_OMIT_VIRTUALTABLE
  909. if( pCx->pVtabCursor ){
  910. sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
  911. const sqlite3_module *pModule = pCx->pModule;
  912. p->inVtabMethod = 1;
  913. sqlite3SafetyOff(p->db);
  914. pModule->xClose(pVtabCursor);
  915. sqlite3SafetyOn(p->db);
  916. p->inVtabMethod = 0;
  917. }
  918. #endif
  919. sqlite3_free(pCx->pData);
  920. sqlite3_free(pCx->aType);
  921. sqlite3_free(pCx);
  922. }
  923. /*
  924. ** Close all cursors except for VTab cursors that are currently
  925. ** in use.
  926. */
  927. static void closeAllCursorsExceptActiveVtabs(Vdbe *p){
  928. int i;
  929. if( p->apCsr==0 ) return;
  930. for(i=0; i<p->nCursor; i++){
  931. Cursor *pC = p->apCsr[i];
  932. if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){
  933. sqlite3VdbeFreeCursor(p, pC);
  934. p->apCsr[i] = 0;
  935. }
  936. }
  937. }
  938. /*
  939. ** Clean up the VM after execution.
  940. **
  941. ** This routine will automatically close any cursors, lists, and/or
  942. ** sorters that were left open. It also deletes the values of
  943. ** variables in the aVar[] array.
  944. */
  945. static void Cleanup(Vdbe *p){
  946. int i;
  947. if( p->aStack ){
  948. releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
  949. p->pTos = &p->aStack[-1];
  950. }
  951. closeAllCursorsExceptActiveVtabs(p);
  952. releaseMemArray(p->aMem, p->nMem);
  953. sqlite3VdbeFifoClear(&p->sFifo);
  954. if( p->contextStack ){
  955. for(i=0; i<p->contextStackTop; i++){
  956. sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
  957. }
  958. sqlite3_free(p->contextStack);
  959. }
  960. p->contextStack = 0;
  961. p->contextStackDepth = 0;
  962. p->contextStackTop = 0;
  963. sqlite3_free(p->zErrMsg);
  964. p->zErrMsg = 0;
  965. p->resOnStack = 0;
  966. }
  967. /*
  968. ** Set the number of result columns that will be returned by this SQL
  969. ** statement. This is now set at compile time, rather than during
  970. ** execution of the vdbe program so that sqlite3_column_count() can
  971. ** be called on an SQL statement before sqlite3_step().
  972. */
  973. void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
  974. Mem *pColName;
  975. int n;
  976. releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
  977. sqlite3_free(p->aColName);
  978. n = nResColumn*COLNAME_N;
  979. p->nResColumn = nResColumn;
  980. p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n );
  981. if( p->aColName==0 ) return;
  982. while( n-- > 0 ){
  983. pColName->flags = MEM_Null;
  984. pColName->db = p->db;
  985. pColName++;
  986. }
  987. }
  988. /*
  989. ** Set the name of the idx'th column to be returned by the SQL statement.
  990. ** zName must be a pointer to a nul terminated string.
  991. **
  992. ** This call must be made after a call to sqlite3VdbeSetNumCols().
  993. **
  994. ** If N==P3_STATIC it means that zName is a pointer to a constant static
  995. ** string and we can just copy the pointer. If it is P3_DYNAMIC, then
  996. ** the string is freed using sqlite3_free() when the vdbe is finished with
  997. ** it. Otherwise, N bytes of zName are copied.
  998. */
  999. int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
  1000. int rc;
  1001. Mem *pColName;
  1002. assert( idx<p->nResColumn );
  1003. assert( var<COLNAME_N );
  1004. if( p->db->mallocFailed ) return SQLITE_NOMEM;
  1005. assert( p->aColName!=0 );
  1006. pColName = &(p->aColName[idx+var*p->nResColumn]);
  1007. if( N==P3_DYNAMIC || N==P3_STATIC ){
  1008. rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
  1009. }else{
  1010. rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
  1011. }
  1012. if( rc==SQLITE_OK && N==P3_DYNAMIC ){
  1013. pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
  1014. pColName->xDel = 0;
  1015. }
  1016. return rc;
  1017. }
  1018. /*
  1019. ** A read or write transaction may or may not be active on database handle
  1020. ** db. If a transaction is active, commit it. If there is a
  1021. ** write-transaction spanning more than one database file, this routine
  1022. ** takes care of the master journal trickery.
  1023. */
  1024. static int vdbeCommit(sqlite3 *db){
  1025. int i;
  1026. int nTrans = 0; /* Number of databases with an active write-transaction */
  1027. int rc = SQLITE_OK;
  1028. int needXcommit = 0;
  1029. /* Before doing anything else, call the xSync() callback for any
  1030. ** virtual module tables written in this transaction. This has to
  1031. ** be done before determining whether a master journal file is
  1032. ** required, as an xSync() callback may add an attached database
  1033. ** to the transaction.
  1034. */
  1035. rc = sqlite3VtabSync(db, rc);
  1036. if( rc!=SQLITE_OK ){
  1037. return rc;
  1038. }
  1039. /* This loop determines (a) if the commit hook should be invoked and
  1040. ** (b) how many database files have open write transactions, not
  1041. ** including the temp database. (b) is important because if more than
  1042. ** one database file has an open write transaction, a master journal
  1043. ** file is required for an atomic commit.
  1044. */
  1045. for(i=0; i<db->nDb; i++){
  1046. Btree *pBt = db->aDb[i].pBt;
  1047. if( sqlite3BtreeIsInTrans(pBt) ){
  1048. needXcommit = 1;
  1049. if( i!=1 ) nTrans++;
  1050. }
  1051. }
  1052. /* If there are any write-transactions at all, invoke the commit hook */
  1053. if( needXcommit && db->xCommitCallback ){
  1054. sqlite3SafetyOff(db);
  1055. rc = db->xCommitCallback(db->pCommitArg);
  1056. sqlite3SafetyOn(db);
  1057. if( rc ){
  1058. return SQLITE_CONSTRAINT;
  1059. }
  1060. }
  1061. /* The simple case - no more than one database file (not counting the
  1062. ** TEMP database) has a transaction active. There is no need for the
  1063. ** master-journal.
  1064. **
  1065. ** If the return value of sqlite3BtreeGetFilename() is a zero length
  1066. ** string, it means the main database is :memory:. In that case we do
  1067. ** not support atomic multi-file commits, so use the simple case then
  1068. ** too.
  1069. */
  1070. if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
  1071. for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
  1072. Btree *pBt = db->aDb[i].pBt;
  1073. if( pBt ){
  1074. rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
  1075. }
  1076. }
  1077. /* Do the commit only if all databases successfully complete phase 1.
  1078. ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
  1079. ** IO error while deleting or truncating a journal file. It is unlikely,
  1080. ** but could happen. In this case abandon processing and return the error.
  1081. */
  1082. for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
  1083. Btree *pBt = db->aDb[i].pBt;
  1084. if( pBt ){
  1085. rc = sqlite3BtreeCommitPhaseTwo(pBt);
  1086. }
  1087. }
  1088. if( rc==SQLITE_OK ){
  1089. sqlite3VtabCommit(db);
  1090. }
  1091. }
  1092. /* The complex case - There is a multi-file write-transaction active.
  1093. ** This requires a master journal file to ensure the transaction is
  1094. ** committed atomicly.
  1095. */
  1096. #ifndef SQLITE_OMIT_DISKIO
  1097. else{
  1098. sqlite3_vfs *pVfs = db->pVfs;
  1099. int needSync = 0;
  1100. char *zMaster = 0; /* File-name for the master journal */
  1101. char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
  1102. sqlite3_file *pMaster = 0;
  1103. i64 offset = 0;
  1104. /* Select a master journal file name */
  1105. do {
  1106. u32 random;
  1107. sqlite3_free(zMaster);
  1108. sqlite3Randomness(sizeof(random), &random);
  1109. zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff);
  1110. if( !zMaster ){
  1111. return SQLITE_NOMEM;
  1112. }
  1113. }while( sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS) );
  1114. /* Open the master journal. */
  1115. rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
  1116. SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
  1117. SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
  1118. );
  1119. if( rc!=SQLITE_OK ){
  1120. sqlite3_free(zMaster);
  1121. return rc;
  1122. }
  1123. /* Write the name of each database file in the transaction into the new
  1124. ** master journal file. If an error occurs at this point close
  1125. ** and delete the master journal file. All the individual journal files
  1126. ** still have 'null' as the master journal pointer, so they will roll
  1127. ** back independently if a failure occurs.
  1128. */
  1129. for(i=0; i<db->nDb; i++){
  1130. Btree *pBt = db->aDb[i].pBt;
  1131. if( i==1 ) continue; /* Ignore the TEMP database */
  1132. if( sqlite3BtreeIsInTrans(pBt) ){
  1133. char const *zFile = sqlite3BtreeGetJournalname(pBt);
  1134. if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
  1135. if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
  1136. needSync = 1;
  1137. }
  1138. rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset);
  1139. offset += strlen(zFile)+1;
  1140. if( rc!=SQLITE_OK ){
  1141. sqlite3OsCloseFree(pMaster);
  1142. sqlite3OsDelete(pVfs, zMaster, 0);
  1143. sqlite3_free(zMaster);
  1144. return rc;
  1145. }
  1146. }
  1147. }
  1148. /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
  1149. ** flag is set this is not required.
  1150. */
  1151. zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
  1152. if( (needSync
  1153. && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL))
  1154. && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){
  1155. sqlite3OsCloseFree(pMaster);
  1156. sqlite3OsDelete(pVfs, zMaster, 0);
  1157. sqlite3_free(zMaster);
  1158. return rc;
  1159. }
  1160. /* Sync all the db files involved in the transaction. The same call
  1161. ** sets the master journal pointer in each individual journal. If
  1162. ** an error occurs here, do not delete the master journal file.
  1163. **
  1164. ** If the error occurs during the first call to
  1165. ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
  1166. ** master journal file will be orphaned. But we cannot delete it,
  1167. ** in case the master journal file name was written into the journal
  1168. ** file before the failure occured.
  1169. */
  1170. for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
  1171. Btree *pBt = db->aDb[i].pBt;
  1172. if( pBt ){
  1173. rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
  1174. }
  1175. }
  1176. sqlite3OsCloseFree(pMaster);
  1177. if( rc!=SQLITE_OK ){
  1178. sqlite3_free(zMaster);
  1179. return rc;
  1180. }
  1181. /* Delete the master journal file. This commits the transaction. After
  1182. ** doing this the directory is synced again before any individual
  1183. ** transaction files are deleted.
  1184. */
  1185. rc = sqlite3OsDelete(pVfs, zMaster, 1);
  1186. sqlite3_free(zMaster);
  1187. zMaster = 0;
  1188. if( rc ){
  1189. return rc;
  1190. }
  1191. /* All files and directories have already been synced, so the following
  1192. ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
  1193. ** deleting or truncating journals. If something goes wrong while
  1194. ** this is happening we don't really care. The integrity of the
  1195. ** transaction is already guaranteed, but some stray 'cold' journals
  1196. ** may be lying around. Returning an error code won't help matters.
  1197. */
  1198. disable_simulated_io_errors();
  1199. for(i=0; i<db->nDb; i++){
  1200. Btree *pBt = db->aDb[i].pBt;
  1201. if( pBt ){
  1202. sqlite3BtreeCommitPhaseTwo(pBt);
  1203. }
  1204. }
  1205. enable_simulated_io_errors();
  1206. sqlite3VtabCommit(db);
  1207. }
  1208. #endif
  1209. return rc;
  1210. }
  1211. /*
  1212. ** This routine checks that the sqlite3.activeVdbeCnt count variable
  1213. ** matches the number of vdbe's in the list sqlite3.pVdbe that are
  1214. ** currently active. An assertion fails if the two counts do not match.
  1215. ** This is an internal self-check only - it is not an essential processing
  1216. ** step.
  1217. **
  1218. ** This is a no-op if NDEBUG is defined.
  1219. */
  1220. #ifndef NDEBUG
  1221. static void checkActiveVdbeCnt(sqlite3 *db){
  1222. Vdbe *p;
  1223. int cnt = 0;
  1224. p = db->pVdbe;
  1225. while( p ){
  1226. if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
  1227. cnt++;
  1228. }
  1229. p = p->pNext;
  1230. }
  1231. assert( cnt==db->activeVdbeCnt );
  1232. }
  1233. #else
  1234. #define checkActiveVdbeCnt(x)
  1235. #endif
  1236. /*
  1237. ** For every Btree that in database connection db which
  1238. ** has been modified, "trip" or invalidate each cursor in
  1239. ** that Btree might have been modified so that the cursor
  1240. ** can never be used again. This happens when a rollback
  1241. *** occurs. We have to trip all the other cursors, even
  1242. ** cursor from other VMs in different database connections,
  1243. ** so that none of them try to use the data at which they
  1244. ** were pointing and which now may have been changed due
  1245. ** to the rollback.
  1246. **
  1247. ** Remember that a rollback can delete tables complete and
  1248. ** reorder rootpages. So it is not sufficient just to save
  1249. ** the state of the cursor. We have to invalidate the cursor
  1250. ** so that it is never used again.
  1251. */
  1252. static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
  1253. int i;
  1254. for(i=0; i<db->nDb; i++){
  1255. Btree *p = db->aDb[i].pBt;
  1256. if( p && sqlite3BtreeIsInTrans(p) ){
  1257. sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
  1258. }
  1259. }
  1260. }
  1261. /*
  1262. ** This routine is called the when a VDBE tries to halt. If the VDBE
  1263. ** has made changes and is in autocommit mode, then commit those
  1264. ** changes. If a rollback is needed, then do the rollback.
  1265. **
  1266. ** This routine is the only way to move the state of a VM from
  1267. ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
  1268. ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
  1269. **
  1270. ** Return an error code. If the commit could not complete because of
  1271. ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
  1272. ** means the close did not happen and needs to be repeated.
  1273. */
  1274. int sqlite3VdbeHalt(Vdbe *p){
  1275. sqlite3 *db = p->db;
  1276. int i;
  1277. int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
  1278. int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
  1279. /* This function contains the logic that determines if a statement or
  1280. ** transaction will be committed or rolled back as a result of the
  1281. ** execution of this virtual machine.
  1282. **
  1283. ** If any of the following errors occur:
  1284. **
  1285. ** SQLITE_NOMEM
  1286. ** SQLITE_IOERR
  1287. ** SQLITE_FULL
  1288. ** SQLITE_INTERRUPT
  1289. **
  1290. ** Then the internal cache might have been left in an inconsistent
  1291. ** state. We need to rollback the statement transaction, if there is
  1292. ** one, or the complete transaction if there is no statement transaction.
  1293. */
  1294. if( p->db->mallocFailed ){
  1295. p->rc = SQLITE_NOMEM;
  1296. }
  1297. closeAllCursorsExceptActiveVtabs(p);
  1298. if( p->magic!=VDBE_MAGIC_RUN ){
  1299. return SQLITE_OK;
  1300. }
  1301. checkActiveVdbeCnt(db);
  1302. /* No commit or rollback needed if the program never started */
  1303. if( p->pc>=0 ){
  1304. int mrc; /* Primary error code from p->rc */
  1305. /* Lock all btrees used by the statement */
  1306. sqlite3BtreeMutexArrayEnter(&p->aMutex);
  1307. /* Check for one of the special errors */
  1308. mrc = p->rc & 0xff;
  1309. isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
  1310. || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
  1311. if( isSpecialError ){
  1312. /* This loop does static analysis of the query to see which of the
  1313. ** following three categories it falls into:
  1314. **
  1315. ** Read-only
  1316. ** Query with statement journal
  1317. ** Query without statement journal
  1318. **
  1319. ** We could do something more elegant than this static analysis (i.e.
  1320. ** store the type of query as part of the compliation phase), but
  1321. ** handling malloc() or IO failure is a fairly obscure edge case so
  1322. ** this is probably easier. Todo: Might be an opportunity to reduce
  1323. ** code size a very small amount though...
  1324. */
  1325. int notReadOnly = 0;
  1326. int isStatement = 0;
  1327. assert(p->aOp || p->nOp==0);
  1328. for(i=0; i<p->nOp; i++){
  1329. switch( p->aOp[i].opcode ){
  1330. case OP_Transaction:
  1331. notReadOnly |= p->aOp[i].p2;
  1332. break;
  1333. case OP_Statement:
  1334. isStatement = 1;
  1335. break;
  1336. }
  1337. }
  1338. /* If the query was read-only, we need do no rollback at all. Otherwise,
  1339. ** proceed with the special handling.
  1340. */
  1341. if( notReadOnly || mrc!=SQLITE_INTERRUPT ){
  1342. if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){
  1343. xFunc = sqlite3BtreeRollbackStmt;
  1344. p->rc = SQLITE_BUSY;
  1345. } else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && isStatement ){
  1346. xFunc = sqlite3BtreeRollbackStmt;
  1347. }else{
  1348. /* We are forced to roll back the active transaction. Before doing
  1349. ** so, abort any other statements this handle currently has active.
  1350. */
  1351. invalidateCursorsOnModifiedBtrees(db);
  1352. sqlite3RollbackAll(db);
  1353. db->autoCommit = 1;
  1354. }
  1355. }
  1356. }
  1357. /* If the auto-commit flag is set and this is the only active vdbe, then
  1358. ** we do either a commit or rollback of the current transaction.
  1359. **
  1360. ** Note: This block also runs if one of the special errors handled
  1361. ** above has occured.
  1362. */
  1363. if( db->autoCommit && db->activeVdbeCnt==1 ){
  1364. if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
  1365. /* The auto-commit flag is true, and the vdbe program was
  1366. ** successful or hit an 'OR FAIL' constraint. This means a commit
  1367. ** is required.
  1368. */
  1369. int rc = vdbeCommit(db);
  1370. if( rc==SQLITE_BUSY ){
  1371. sqlite3BtreeMutexArrayLeave(&p->aMutex);
  1372. return SQLITE_BUSY;
  1373. }else if( rc!=SQLITE_OK ){
  1374. p->rc = rc;
  1375. sqlite3RollbackAll(db);
  1376. }else{
  1377. sqlite3CommitInternalChanges(db);
  1378. }
  1379. }else{
  1380. sqlite3RollbackAll(db);
  1381. }
  1382. }else if( !xFunc ){
  1383. if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
  1384. if( p->openedStatement ){
  1385. xFunc = sqlite3BtreeCommitStmt;
  1386. }
  1387. }else if( p->errorAction==OE_Abort ){
  1388. xFunc = sqlite3BtreeRollbackStmt;
  1389. }else{
  1390. invalidateCursorsOnModifiedBtrees(db);
  1391. sqlite3RollbackAll(db);
  1392. db->autoCommit = 1;
  1393. }
  1394. }
  1395. /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
  1396. ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
  1397. ** and the return code is still SQLITE_OK, set the return code to the new
  1398. ** error value.
  1399. */
  1400. assert(!xFunc ||
  1401. xFunc==sqlite3BtreeCommitStmt ||
  1402. xFunc==sqlite3BtreeRollbackStmt
  1403. );
  1404. for(i=0; xFunc && i<db->nDb; i++){
  1405. int rc;
  1406. Btree *pBt = db->aDb[i].pBt;
  1407. if( pBt ){
  1408. rc = xFunc(pBt);
  1409. if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
  1410. p->rc = rc;
  1411. sqlite3SetString(&p->zErrMsg, 0);
  1412. }
  1413. }
  1414. }
  1415. /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
  1416. ** set the change counter.
  1417. */
  1418. if( p->changeCntOn && p->pc>=0 ){
  1419. if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
  1420. sqlite3VdbeSetChanges(db, p->nChange);
  1421. }else{
  1422. sqlite3VdbeSetChanges(db, 0);
  1423. }
  1424. p->nChange = 0;
  1425. }
  1426. /* Rollback or commit any schema changes that occurred. */
  1427. if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
  1428. sqlite3ResetInternalSchema(db, 0);
  1429. db->flags = (db->flags | SQLITE_InternChanges);
  1430. }
  1431. /* Release the locks */
  1432. sqlite3BtreeMutexArrayLeave(&p->aMutex);
  1433. }
  1434. /* We have successfully halted and closed the VM. Record this fact. */
  1435. if( p->pc>=0 ){
  1436. db->activeVdbeCnt--;
  1437. }
  1438. p->magic = VDBE_MAGIC_HALT;
  1439. checkActiveVdbeCnt(db);
  1440. if( p->db->mallocFailed ){
  1441. p->rc = SQLITE_NOMEM;
  1442. }
  1443. checkActiveVdbeCnt(db);
  1444. return SQLITE_OK;
  1445. }
  1446. /*
  1447. ** Each VDBE holds the result of the most recent sqlite3_step() call
  1448. ** in p->rc. This routine sets that result back to SQLITE_OK.
  1449. */
  1450. void sqlite3VdbeResetStepResult(Vdbe *p){
  1451. p->rc = SQLITE_OK;
  1452. }
  1453. /*
  1454. ** Clean up a VDBE after execution but do not delete the VDBE just yet.
  1455. ** Write any error messages into *pzErrMsg. Return the result code.
  1456. **
  1457. ** After this routine is run, the VDBE should be ready to be executed
  1458. ** again.
  1459. **
  1460. ** To look at it another way, this routine resets the state of the
  1461. ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
  1462. ** VDBE_MAGIC_INIT.
  1463. */
  1464. int sqlite3VdbeReset(Vdbe *p){
  1465. sqlite3 *db;
  1466. db = p->db;
  1467. /* If the VM did not run to completion or if it encountered an
  1468. ** error, then it might not have been halted properly. So halt
  1469. ** it now.
  1470. */
  1471. sqlite3SafetyOn(db);
  1472. sqlite3VdbeHalt(p);
  1473. sqlite3SafetyOff(db);
  1474. /* If the VDBE has be run even partially, then transfer the error code
  1475. ** and error message from the VDBE into the main database structure. But
  1476. ** if the VDBE has just been set to run but has not actually executed any
  1477. ** instructions yet, leave the main database error information unchanged.
  1478. */
  1479. if( p->pc>=0 ){
  1480. if( p->zErrMsg ){
  1481. sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,sqlite3_free);
  1482. db->errCode = p->rc;
  1483. p->zErrMsg = 0;
  1484. }else if( p->rc ){
  1485. sqlite3Error(db, p->rc, 0);
  1486. }else{
  1487. sqlite3Error(db, SQLITE_OK, 0);
  1488. }
  1489. }else if( p->rc && p->expired ){
  1490. /* The expired flag was set on the VDBE before the first call
  1491. ** to sqlite3_step(). For consistency (since sqlite3_step() was
  1492. ** called), set the database error in this case as well.
  1493. */
  1494. sqlite3Error(db, p->rc, 0);
  1495. sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3_free);
  1496. p->zErrMsg = 0;
  1497. }
  1498. /* Reclaim all memory used by the VDBE
  1499. */
  1500. Cleanup(p);
  1501. /* Save profiling information from this VDBE run.
  1502. */
  1503. assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack );
  1504. #ifdef VDBE_PROFILE
  1505. {
  1506. FILE *out = fopen("vdbe_profile.out", "a");
  1507. if( out ){
  1508. int i;
  1509. fprintf(out, "---- ");
  1510. for(i=0; i<p->nOp; i++){
  1511. fprintf(out, "%02x", p->aOp[i].opcode);
  1512. }
  1513. fprintf(out, "\n");
  1514. for(i=0; i<p->nOp; i++){
  1515. fprintf(out, "%6d %10lld %8lld ",
  1516. p->aOp[i].cnt,
  1517. p->aOp[i].cycles,
  1518. p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
  1519. );
  1520. sqlite3VdbePrintOp(out, i, &p->aOp[i]);
  1521. }
  1522. fclose(out);
  1523. }
  1524. }
  1525. #endif
  1526. p->magic = VDBE_MAGIC_INIT;
  1527. p->aborted = 0;
  1528. return p->rc & db->errMask;
  1529. }
  1530. /*
  1531. ** Clean up and delete a VDBE after execution. Return an integer which is
  1532. ** the result code. Write any error message text into *pzErrMsg.
  1533. */
  1534. int sqlite3VdbeFinalize(Vdbe *p){
  1535. int rc = SQLITE_OK;
  1536. if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
  1537. rc = sqlite3VdbeReset(p);
  1538. assert( (rc & p->db->errMask)==rc );
  1539. }else if( p->magic!=VDBE_MAGIC_INIT ){
  1540. return SQLITE_MISUSE;
  1541. }
  1542. sqlite3VdbeDelete(p);
  1543. return rc;
  1544. }
  1545. /*
  1546. ** Call the destructor for each auxdata entry in pVdbeFunc for which
  1547. ** the corresponding bit in mask is clear. Auxdata entries beyond 31
  1548. ** are always destroyed. To destroy all auxdata entries, call this
  1549. ** routine with mask==0.
  1550. */
  1551. void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
  1552. int i;
  1553. for(i=0; i<pVdbeFunc->nAux; i++){
  1554. struct AuxData *pAux = &pVdbeFunc->apAux[i];
  1555. if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
  1556. if( pAux->xDelete ){
  1557. pAux->xDelete(pAux->pAux);
  1558. }
  1559. pAux->pAux = 0;
  1560. }
  1561. }
  1562. }
  1563. /*
  1564. ** Delete an entire VDBE.
  1565. */
  1566. void sqlite3VdbeDelete(Vdbe *p){
  1567. int i;
  1568. if( p==0 ) return;
  1569. Cleanup(p);
  1570. if( p->pPrev ){
  1571. p->pPrev->pNext = p->pNext;
  1572. }else{
  1573. assert( p->db->pVdbe==p );
  1574. p->db->pVdbe = p->pNext;
  1575. }
  1576. if( p->pNext ){
  1577. p->pNext->pPrev = p->pPrev;
  1578. }
  1579. if( p->aOp ){
  1580. for(i=0; i<p->nOp; i++){
  1581. Op *pOp = &p->aOp[i];
  1582. freeP3(pOp->p3type, pOp->p3);
  1583. }
  1584. sqlite3_free(p->aOp);
  1585. }
  1586. releaseMemArray(p->aVar, p->nVar);
  1587. sqlite3_free(p->aLabel);
  1588. sqlite3_free(p->aStack);
  1589. releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
  1590. sqlite3_free(p->aColName);
  1591. sqlite3_free(p->zSql);
  1592. p->magic = VDBE_MAGIC_DEAD;
  1593. sqlite3_free(p);
  1594. }
  1595. /*
  1596. ** If a MoveTo operation is pending on the given cursor, then do that
  1597. ** MoveTo now. Return an error code. If no MoveTo is pending, this
  1598. ** routine does nothing and returns SQLITE_OK.
  1599. */
  1600. int sqlite3VdbeCursorMoveto(Cursor *p){
  1601. if( p->deferredMoveto ){
  1602. int res, rc;
  1603. #ifdef SQLITE_TEST
  1604. extern int sqlite3_search_count;
  1605. #endif
  1606. assert( p->isTable );
  1607. rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, 0, &res);
  1608. if( rc ) return rc;
  1609. *p->pIncrKey = 0;
  1610. p->lastRowid = keyToInt(p->movetoTarget);
  1611. p->rowidIsValid = res==0;
  1612. if( res<0 ){
  1613. rc = sqlite3BtreeNext(p->pCursor, &res);
  1614. if( rc ) return rc;
  1615. }
  1616. #ifdef SQLITE_TEST
  1617. sqlite3_search_count++;
  1618. #endif
  1619. p->deferredMoveto = 0;
  1620. p->cacheStatus = CACHE_STALE;
  1621. }
  1622. return SQLITE_OK;
  1623. }
  1624. /*
  1625. ** The following functions:
  1626. **
  1627. ** sqlite3VdbeSerialType()
  1628. ** sqlite3VdbeSerialTypeLen()
  1629. ** sqlite3VdbeSerialRead()
  1630. ** sqlite3VdbeSerialLen()
  1631. ** sqlite3VdbeSerialWrite()
  1632. **
  1633. ** encapsulate the code that serializes values for storage in SQLite
  1634. ** data and index records. Each serialized value consists of a
  1635. ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
  1636. ** integer, stored as a varint.
  1637. **
  1638. ** In an SQLite index record, the serial type is stored directly before
  1639. ** the blob of data that it corresponds to. In a table record, all serial
  1640. ** types are stored at the start of the record, and the blobs of data at
  1641. ** the end. Hence these functions allow the caller to handle the
  1642. ** serial-type and data blob seperately.
  1643. **
  1644. ** The following table describes the various storage classes for data:
  1645. **
  1646. ** serial type bytes of data type
  1647. ** -------------- --------------- ---------------
  1648. ** 0 0 NULL
  1649. ** 1 1 signed integer
  1650. ** 2 2 signed integer
  1651. ** 3 3 signed integer
  1652. ** 4 4 signed integer
  1653. ** 5 6 signed integer
  1654. ** 6 8 signed integer
  1655. ** 7 8 IEEE float
  1656. ** 8 0 Integer constant 0
  1657. ** 9 0 Integer constant 1
  1658. ** 10,11 reserved for expansion
  1659. ** N>=12 and even (N-12)/2 BLOB
  1660. ** N>=13 and odd (N-13)/2 text
  1661. **
  1662. ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
  1663. ** of SQLite will not understand those serial types.
  1664. */
  1665. /*
  1666. ** Return the serial-type for the value stored in pMem.
  1667. */
  1668. u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
  1669. int flags = pMem->flags;
  1670. int n;
  1671. if( flags&MEM_Null ){
  1672. return 0;
  1673. }
  1674. if( flags&MEM_Int ){
  1675. /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
  1676. # define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
  1677. i64 i = pMem->u.i;
  1678. u64 u;
  1679. if( file_format>=4 && (i&1)==i ){
  1680. return 8+i;
  1681. }
  1682. u = i<0 ? -i : i;
  1683. if( u<=127 ) return 1;
  1684. if( u<=32767 ) return 2;
  1685. if( u<=8388607 ) return 3;
  1686. if( u<=2147483647 ) return 4;
  1687. if( u<=MAX_6BYTE ) return 5;
  1688. return 6;
  1689. }
  1690. if( flags&MEM_Real ){
  1691. return 7;
  1692. }
  1693. assert( flags&(MEM_Str|MEM_Blob) );
  1694. n = pMem->n;
  1695. if( flags & MEM_Zero ){
  1696. n += pMem->u.i;
  1697. }
  1698. assert( n>=0 );
  1699. return ((n*2) + 12 + ((flags&MEM_Str)!=0));
  1700. }
  1701. /*
  1702. ** Return the length of the data corresponding to the supplied serial-type.
  1703. */
  1704. int sqlite3VdbeSerialTypeLen(u32 serial_type){
  1705. if( serial_type>=12 ){
  1706. return (serial_type-12)/2;
  1707. }else{
  1708. static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
  1709. return aSize[serial_type];
  1710. }
  1711. }
  1712. /*
  1713. ** If we are on an architecture with mixed-endian floating
  1714. ** points (ex: ARM7) then swap the lower 4 bytes with the
  1715. ** upper 4 bytes. Return the result.
  1716. **
  1717. ** For most architectures, this is a no-op.
  1718. **
  1719. ** (later): It is reported to me that the mixed-endian problem
  1720. ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
  1721. ** that early versions of GCC stored the two words of a 64-bit
  1722. ** float in the wrong order. And that error has been propagated
  1723. ** ever since. The blame is not necessarily with GCC, though.
  1724. ** GCC might have just copying the problem from a prior compiler.
  1725. ** I am also told that newer versions of GCC that follow a different
  1726. ** ABI get the byte order right.
  1727. **
  1728. ** Developers using SQLite on an ARM7 should compile and run their
  1729. ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
  1730. ** enabled, some asserts below will ensure that the byte order of
  1731. ** floating point values is correct.
  1732. **
  1733. ** (2007-08-30) Frank van Vugt has studied this problem closely
  1734. ** and has send his findings to the SQLite developers. Frank
  1735. ** writes that some Linux kernels offer floating point hardware
  1736. ** emulation that uses only 32-bit mantissas instead of a full
  1737. ** 48-bits as required by the IEEE standard. (This is the
  1738. ** CONFIG_FPE_FASTFPE option.) On such systems, floating point
  1739. ** byte swapping becomes very complicated. To avoid problems,
  1740. ** the necessary byte swapping is carried out using a 64-bit integer
  1741. ** rather than a 64-bit float. Frank assures us that the code here
  1742. ** works for him. We, the developers, have no way to independently
  1743. ** verify this, but Frank seems to know what he is talking about
  1744. ** so we trust him.
  1745. */
  1746. #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
  1747. static u64 floatSwap(u64 in){
  1748. union {
  1749. u64 r;
  1750. u32 i[2];
  1751. } u;
  1752. u32 t;
  1753. u.r = in;
  1754. t = u.i[0];
  1755. u.i[0] = u.i[1];
  1756. u.i[1] = t;
  1757. return u.r;
  1758. }
  1759. # define swapMixedEndianFloat(X) X = floatSwap(X)
  1760. #else
  1761. # define swapMixedEndianFloat(X)
  1762. #endif
  1763. /*
  1764. ** Write the serialized data blob for the value stored in pMem into
  1765. ** buf. It is assumed that the caller has allocated sufficient space.
  1766. ** Return the number of bytes written.
  1767. **
  1768. ** nBuf is the amount of space left in buf[]. nBuf must always be
  1769. ** large enough to hold the entire field. Except, if the field is
  1770. ** a blob with a zero-filled tail, then buf[] might be just the right
  1771. ** size to hold everything except for the zero-filled tail. If buf[]
  1772. ** is only big enough to hold the non-zero prefix, then only write that
  1773. ** prefix into buf[]. But if buf[] is large enough to hold both the
  1774. ** prefix and the tail then write the prefix and set the tail to all
  1775. ** zeros.
  1776. **
  1777. ** Return the number of bytes actually written into buf[]. The number
  1778. ** of bytes in the zero-filled tail is included in the return value only
  1779. ** if those bytes were zeroed in buf[].
  1780. */
  1781. int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
  1782. u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
  1783. int len;
  1784. /* Integer and Real */
  1785. if( serial_type<=7 && serial_type>0 ){
  1786. u64 v;
  1787. int i;
  1788. if( serial_type==7 ){
  1789. assert( sizeof(v)==sizeof(pMem->r) );
  1790. memcpy(&v, &pMem->r, sizeof(v));
  1791. swapMixedEndianFloat(v);
  1792. }else{
  1793. v = pMem->u.i;
  1794. }
  1795. len = i = sqlite3VdbeSerialTypeLen(serial_type);
  1796. assert( len<=nBuf );
  1797. while( i-- ){
  1798. buf[i] = (v&0xFF);
  1799. v >>= 8;
  1800. }
  1801. return len;
  1802. }
  1803. /* String or blob */
  1804. if( serial_type>=12 ){
  1805. assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0)
  1806. == sqlite3VdbeSerialTypeLen(serial_type) );
  1807. assert( pMem->n<=nBuf );
  1808. len = pMem->n;
  1809. memcpy(buf, pMem->z, len);
  1810. if( pMem->flags & MEM_Zero ){
  1811. len += pMem->u.i;
  1812. if( len>nBuf ){
  1813. len = nBuf;
  1814. }
  1815. memset(&buf[pMem->n], 0, len-pMem->n);
  1816. }
  1817. return len;
  1818. }
  1819. /* NULL or constants 0 or 1 */
  1820. return 0;
  1821. }
  1822. /*
  1823. ** Deserialize the data blob pointed to by buf as serial type serial_type
  1824. ** and store the result in pMem. Return the number of bytes read.
  1825. */
  1826. int sqlite3VdbeSerialGet(
  1827. const unsigned char *buf, /* Buffer to deserialize from */
  1828. u32 serial_type, /* Serial type to deserialize */
  1829. Mem *pMem /* Memory cell to write value into */
  1830. ){
  1831. switch( serial_type ){
  1832. case 10: /* Reserved for future use */
  1833. case 11: /* Reserved for future use */
  1834. case 0: { /* NULL */
  1835. pMem->flags = MEM_Null;
  1836. break;
  1837. }
  1838. case 1: { /* 1-byte signed integer */
  1839. pMem->u.i = (signed char)buf[0];
  1840. pMem->flags = MEM_Int;
  1841. return 1;
  1842. }
  1843. case 2: { /* 2-byte signed integer */
  1844. pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
  1845. pMem->flags = MEM_Int;
  1846. return 2;
  1847. }
  1848. case 3: { /* 3-byte signed integer */
  1849. pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
  1850. pMem->flags = MEM_Int;
  1851. return 3;
  1852. }
  1853. case 4: { /* 4-byte signed integer */
  1854. pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
  1855. pMem->flags = MEM_Int;
  1856. return 4;
  1857. }
  1858. case 5: { /* 6-byte signed integer */
  1859. u64 x = (((signed char)buf[0])<<8) | buf[1];
  1860. u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
  1861. x = (x<<32) | y;
  1862. pMem->u.i = *(i64*)&x;
  1863. pMem->flags = MEM_Int;
  1864. return 6;
  1865. }
  1866. case 6: /* 8-byte signed integer */
  1867. case 7: { /* IEEE floating point */
  1868. u64 x;
  1869. u32 y;
  1870. #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
  1871. /* Verify that integers and floating point values use the same
  1872. ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
  1873. ** defined that 64-bit floating point values really are mixed
  1874. ** endian.
  1875. */
  1876. static const u64 t1 = ((u64)0x3ff00000)<<32;
  1877. static const double r1 = 1.0;
  1878. u64 t2 = t1;
  1879. swapMixedEndianFloat(t2);
  1880. assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
  1881. #endif
  1882. x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
  1883. y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
  1884. x = (x<<32) | y;
  1885. if( serial_type==6 ){
  1886. pMem->u.i = *(i64*)&x;
  1887. pMem->flags = MEM_Int;
  1888. }else{
  1889. assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
  1890. swapMixedEndianFloat(x);
  1891. memcpy(&pMem->r, &x, sizeof(x));
  1892. pMem->flags = MEM_Real;
  1893. }
  1894. return 8;
  1895. }
  1896. case 8: /* Integer 0 */
  1897. case 9: { /* Integer 1 */
  1898. pMem->u.i = serial_type-8;
  1899. pMem->flags = MEM_Int;
  1900. return 0;
  1901. }
  1902. default: {
  1903. int len = (serial_type-12)/2;
  1904. pMem->z = (char *)buf;
  1905. pMem->n = len;
  1906. pMem->xDel = 0;
  1907. if( serial_type&0x01 ){
  1908. pMem->flags = MEM_Str | MEM_Ephem;
  1909. }else{
  1910. pMem->flags = MEM_Blob | MEM_Ephem;
  1911. }
  1912. return len;
  1913. }
  1914. }
  1915. return 0;
  1916. }
  1917. /*
  1918. ** The header of a record consists of a sequence variable-length integers.
  1919. ** These integers are almost always small and are encoded as a single byte.
  1920. ** The following macro takes advantage this fact to provide a fast decode
  1921. ** of the integers in a record header. It is faster for the common case
  1922. ** where the integer is a single byte. It is a little slower when the
  1923. ** integer is two or more bytes. But overall it is faster.
  1924. **
  1925. ** The following expressions are equivalent:
  1926. **
  1927. ** x = sqlite3GetVarint32( A, &B );
  1928. **
  1929. ** x = GetVarint( A, B );
  1930. **
  1931. */
  1932. #define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
  1933. /*
  1934. ** This function compares the two table rows or index records specified by
  1935. ** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
  1936. ** or positive integer if {nKey1, pKey1} is less than, equal to or
  1937. ** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
  1938. ** composed by the OP_MakeRecord opcode of the VDBE.
  1939. */
  1940. int sqlite3VdbeRecordCompare(
  1941. void *userData,
  1942. int nKey1, const void *pKey1,
  1943. int nKey2, const void *pKey2
  1944. ){
  1945. KeyInfo *pKeyInfo = (KeyInfo*)userData;
  1946. u32 d1, d2; /* Offset into aKey[] of next data element */
  1947. u32 idx1, idx2; /* Offset into aKey[] of next header element */
  1948. u32 szHdr1, szHdr2; /* Number of bytes in header */
  1949. int i = 0;
  1950. int nField;
  1951. int rc = 0;
  1952. const unsigned char *aKey1 = (const unsigned char *)pKey1;
  1953. const unsigned char *aKey2 = (const unsigned char *)pKey2;
  1954. Mem mem1;
  1955. Mem mem2;
  1956. mem1.enc = pKeyInfo->enc;
  1957. mem1.db = pKeyInfo->db;
  1958. mem2.enc = pKeyInfo->enc;
  1959. mem2.db = pKeyInfo->db;
  1960. idx1 = GetVarint(aKey1, szHdr1);
  1961. d1 = szHdr1;
  1962. idx2 = GetVarint(aKey2, szHdr2);
  1963. d2 = szHdr2;
  1964. nField = pKeyInfo->nField;
  1965. while( idx1<szHdr1 && idx2<szHdr2 ){
  1966. u32 serial_type1;
  1967. u32 serial_type2;
  1968. /* Read the serial types for the next element in each key. */
  1969. idx1 += GetVarint( aKey1+idx1, serial_type1 );
  1970. if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
  1971. idx2 += GetVarint( aKey2+idx2, serial_type2 );
  1972. if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
  1973. /* Extract the values to be compared.
  1974. */
  1975. d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
  1976. d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
  1977. /* Do the comparison
  1978. */
  1979. rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
  1980. if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
  1981. if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
  1982. if( rc!=0 ){
  1983. break;
  1984. }
  1985. i++;
  1986. }
  1987. /* One of the keys ran out of fields, but all the fields up to that point
  1988. ** were equal. If the incrKey flag is true, then the second key is
  1989. ** treated as larger.
  1990. */
  1991. if( rc==0 ){
  1992. if( pKeyInfo->incrKey ){
  1993. rc = -1;
  1994. }else if( !pKeyInfo->prefixIsEqual ){
  1995. if( d1<nKey1 ){
  1996. rc = 1;
  1997. }else if( d2<nKey2 ){
  1998. rc = -1;
  1999. }
  2000. }
  2001. }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
  2002. && pKeyInfo->aSortOrder[i] ){
  2003. rc = -rc;
  2004. }
  2005. return rc;
  2006. }
  2007. /*
  2008. ** The argument is an index entry composed using the OP_MakeRecord opcode.
  2009. ** The last entry in this record should be an integer (specifically
  2010. ** an integer rowid). This routine returns the number of bytes in
  2011. ** that integer.
  2012. */
  2013. int sqlite3VdbeIdxRowidLen(const u8 *aKey){
  2014. u32 szHdr; /* Size of the header */
  2015. u32 typeRowid; /* Serial type of the rowid */
  2016. sqlite3GetVarint32(aKey, &szHdr);
  2017. sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
  2018. return sqlite3VdbeSerialTypeLen(typeRowid);
  2019. }
  2020. /*
  2021. ** pCur points at an index entry created using the OP_MakeRecord opcode.
  2022. ** Read the rowid (the last field in the record) and store it in *rowid.
  2023. ** Return SQLITE_OK if everything works, or an error code otherwise.
  2024. */
  2025. int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
  2026. i64 nCellKey = 0;
  2027. int rc;
  2028. u32 szHdr; /* Size of the header */
  2029. u32 typeRowid; /* Serial type of the rowid */
  2030. u32 lenRowid; /* Size of the rowid */
  2031. Mem m, v;
  2032. sqlite3BtreeKeySize(pCur, &nCellKey);
  2033. if( nCellKey<=0 ){
  2034. return SQLITE_CORRUPT_BKPT;
  2035. }
  2036. rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
  2037. if( rc ){
  2038. return rc;
  2039. }
  2040. sqlite3GetVarint32((u8*)m.z, &szHdr);
  2041. sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid);
  2042. lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
  2043. sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
  2044. *rowid = v.u.i;
  2045. sqlite3VdbeMemRelease(&m);
  2046. return SQLITE_OK;
  2047. }
  2048. /*
  2049. ** Compare the key of the index entry that cursor pC is point to against
  2050. ** the key string in pKey (of length nKey). Write into *pRes a number
  2051. ** that is negative, zero, or positive if pC is less than, equal to,
  2052. ** or greater than pKey. Return SQLITE_OK on success.
  2053. **
  2054. ** pKey is either created without a rowid or is truncated so that it
  2055. ** omits the rowid at the end. The rowid at the end of the index entry
  2056. ** is ignored as well.
  2057. */
  2058. int sqlite3VdbeIdxKeyCompare(
  2059. Cursor *pC, /* The cursor to compare against */
  2060. int nKey, const u8 *pKey, /* The key to compare */
  2061. int *res /* Write the comparison result here */
  2062. ){
  2063. i64 nCellKey = 0;
  2064. int rc;
  2065. BtCursor *pCur = pC->pCursor;
  2066. int lenRowid;
  2067. Mem m;
  2068. sqlite3BtreeKeySize(pCur, &nCellKey);
  2069. if( nCellKey<=0 ){
  2070. *res = 0;
  2071. return SQLITE_OK;
  2072. }
  2073. rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
  2074. if( rc ){
  2075. return rc;
  2076. }
  2077. lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
  2078. *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
  2079. sqlite3VdbeMemRelease(&m);
  2080. return SQLITE_OK;
  2081. }
  2082. /*
  2083. ** This routine sets the value to be returned by subsequent calls to
  2084. ** sqlite3_changes() on the database handle 'db'.
  2085. */
  2086. void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
  2087. assert( sqlite3_mutex_held(db->mutex) );
  2088. db->nChange = nChange;
  2089. db->nTotalChange += nChange;
  2090. }
  2091. /*
  2092. ** Set a flag in the vdbe to update the change counter when it is finalised
  2093. ** or reset.
  2094. */
  2095. void sqlite3VdbeCountChanges(Vdbe *v){
  2096. v->changeCntOn = 1;
  2097. }
  2098. /*
  2099. ** Mark every prepared statement associated with a database connection
  2100. ** as expired.
  2101. **
  2102. ** An expired statement means that recompilation of the statement is
  2103. ** recommend. Statements expire when things happen that make their
  2104. ** programs obsolete. Removing user-defined functions or collating
  2105. ** sequences, or changing an authorization function are the types of
  2106. ** things that make prepared statements obsolete.
  2107. */
  2108. void sqlite3ExpirePreparedStatements(sqlite3 *db){
  2109. Vdbe *p;
  2110. for(p = db->pVdbe; p; p=p->pNext){
  2111. p->expired = 1;
  2112. }
  2113. }
  2114. /*
  2115. ** Return the database associated with the Vdbe.
  2116. */
  2117. sqlite3 *sqlite3VdbeDb(Vdbe *v){
  2118. return v->db;
  2119. }