xref: /sqlite-3.40.0/src/vdbeaux.c (revision b43be55e)
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.)
14 */
15 #include "sqliteInt.h"
16 #include "vdbeInt.h"
17 
18 /*
19 ** Create a new virtual database engine.
20 */
21 Vdbe *sqlite3VdbeCreate(Parse *pParse){
22   sqlite3 *db = pParse->db;
23   Vdbe *p;
24   p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
25   if( p==0 ) return 0;
26   p->db = db;
27   if( db->pVdbe ){
28     db->pVdbe->pPrev = p;
29   }
30   p->pNext = db->pVdbe;
31   p->pPrev = 0;
32   db->pVdbe = p;
33   p->magic = VDBE_MAGIC_INIT;
34   p->pParse = pParse;
35   assert( pParse->aLabel==0 );
36   assert( pParse->nLabel==0 );
37   assert( pParse->nOpAlloc==0 );
38   return p;
39 }
40 
41 /*
42 ** Remember the SQL string for a prepared statement.
43 */
44 void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
45   assert( isPrepareV2==1 || isPrepareV2==0 );
46   if( p==0 ) return;
47 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
48   if( !isPrepareV2 ) return;
49 #endif
50   assert( p->zSql==0 );
51   p->zSql = sqlite3DbStrNDup(p->db, z, n);
52   p->isPrepareV2 = (u8)isPrepareV2;
53 }
54 
55 /*
56 ** Return the SQL associated with a prepared statement
57 */
58 const char *sqlite3_sql(sqlite3_stmt *pStmt){
59   Vdbe *p = (Vdbe *)pStmt;
60   return (p && p->isPrepareV2) ? p->zSql : 0;
61 }
62 
63 /*
64 ** Swap all content between two VDBE structures.
65 */
66 void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
67   Vdbe tmp, *pTmp;
68   char *zTmp;
69   tmp = *pA;
70   *pA = *pB;
71   *pB = tmp;
72   pTmp = pA->pNext;
73   pA->pNext = pB->pNext;
74   pB->pNext = pTmp;
75   pTmp = pA->pPrev;
76   pA->pPrev = pB->pPrev;
77   pB->pPrev = pTmp;
78   zTmp = pA->zSql;
79   pA->zSql = pB->zSql;
80   pB->zSql = zTmp;
81   pB->isPrepareV2 = pA->isPrepareV2;
82 }
83 
84 /*
85 ** Resize the Vdbe.aOp array so that it is at least nOp elements larger
86 ** than its current size. nOp is guaranteed to be less than or equal
87 ** to 1024/sizeof(Op).
88 **
89 ** If an out-of-memory error occurs while resizing the array, return
90 ** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain
91 ** unchanged (this is so that any opcodes already allocated can be
92 ** correctly deallocated along with the rest of the Vdbe).
93 */
94 static int growOpArray(Vdbe *v, int nOp){
95   VdbeOp *pNew;
96   Parse *p = v->pParse;
97 
98   /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
99   ** more frequent reallocs and hence provide more opportunities for
100   ** simulated OOM faults.  SQLITE_TEST_REALLOC_STRESS is generally used
101   ** during testing only.  With SQLITE_TEST_REALLOC_STRESS grow the op array
102   ** by the minimum* amount required until the size reaches 512.  Normal
103   ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
104   ** size of the op array or add 1KB of space, whichever is smaller. */
105 #ifdef SQLITE_TEST_REALLOC_STRESS
106   int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
107 #else
108   int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
109   UNUSED_PARAMETER(nOp);
110 #endif
111 
112   assert( nOp<=(1024/sizeof(Op)) );
113   assert( nNew>=(p->nOpAlloc+nOp) );
114   pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
115   if( pNew ){
116     p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
117     v->aOp = pNew;
118   }
119   return (pNew ? SQLITE_OK : SQLITE_NOMEM);
120 }
121 
122 #ifdef SQLITE_DEBUG
123 /* This routine is just a convenient place to set a breakpoint that will
124 ** fire after each opcode is inserted and displayed using
125 ** "PRAGMA vdbe_addoptrace=on".
126 */
127 static void test_addop_breakpoint(void){
128   static int n = 0;
129   n++;
130 }
131 #endif
132 
133 /*
134 ** Add a new instruction to the list of instructions current in the
135 ** VDBE.  Return the address of the new instruction.
136 **
137 ** Parameters:
138 **
139 **    p               Pointer to the VDBE
140 **
141 **    op              The opcode for this instruction
142 **
143 **    p1, p2, p3      Operands
144 **
145 ** Use the sqlite3VdbeResolveLabel() function to fix an address and
146 ** the sqlite3VdbeChangeP4() function to change the value of the P4
147 ** operand.
148 */
149 int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
150   int i;
151   VdbeOp *pOp;
152 
153   i = p->nOp;
154   assert( p->magic==VDBE_MAGIC_INIT );
155   assert( op>0 && op<0xff );
156   if( p->pParse->nOpAlloc<=i ){
157     if( growOpArray(p, 1) ){
158       return 1;
159     }
160   }
161   p->nOp++;
162   pOp = &p->aOp[i];
163   pOp->opcode = (u8)op;
164   pOp->p5 = 0;
165   pOp->p1 = p1;
166   pOp->p2 = p2;
167   pOp->p3 = p3;
168   pOp->p4.p = 0;
169   pOp->p4type = P4_NOTUSED;
170 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
171   pOp->zComment = 0;
172 #endif
173 #ifdef SQLITE_DEBUG
174   if( p->db->flags & SQLITE_VdbeAddopTrace ){
175     int jj, kk;
176     Parse *pParse = p->pParse;
177     for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){
178       struct yColCache *x = pParse->aColCache + jj;
179       if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue;
180       printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
181       kk++;
182     }
183     if( kk ) printf("\n");
184     sqlite3VdbePrintOp(0, i, &p->aOp[i]);
185     test_addop_breakpoint();
186   }
187 #endif
188 #ifdef VDBE_PROFILE
189   pOp->cycles = 0;
190   pOp->cnt = 0;
191 #endif
192 #ifdef SQLITE_VDBE_COVERAGE
193   pOp->iSrcLine = 0;
194 #endif
195   return i;
196 }
197 int sqlite3VdbeAddOp0(Vdbe *p, int op){
198   return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
199 }
200 int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
201   return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
202 }
203 int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
204   return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
205 }
206 
207 
208 /*
209 ** Add an opcode that includes the p4 value as a pointer.
210 */
211 int sqlite3VdbeAddOp4(
212   Vdbe *p,            /* Add the opcode to this VM */
213   int op,             /* The new opcode */
214   int p1,             /* The P1 operand */
215   int p2,             /* The P2 operand */
216   int p3,             /* The P3 operand */
217   const char *zP4,    /* The P4 operand */
218   int p4type          /* P4 operand type */
219 ){
220   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
221   sqlite3VdbeChangeP4(p, addr, zP4, p4type);
222   return addr;
223 }
224 
225 /*
226 ** Add an OP_ParseSchema opcode.  This routine is broken out from
227 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
228 ** as having been used.
229 **
230 ** The zWhere string must have been obtained from sqlite3_malloc().
231 ** This routine will take ownership of the allocated memory.
232 */
233 void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
234   int j;
235   int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
236   sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
237   for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
238 }
239 
240 /*
241 ** Add an opcode that includes the p4 value as an integer.
242 */
243 int sqlite3VdbeAddOp4Int(
244   Vdbe *p,            /* Add the opcode to this VM */
245   int op,             /* The new opcode */
246   int p1,             /* The P1 operand */
247   int p2,             /* The P2 operand */
248   int p3,             /* The P3 operand */
249   int p4              /* The P4 operand as an integer */
250 ){
251   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
252   sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
253   return addr;
254 }
255 
256 /*
257 ** Create a new symbolic label for an instruction that has yet to be
258 ** coded.  The symbolic label is really just a negative number.  The
259 ** label can be used as the P2 value of an operation.  Later, when
260 ** the label is resolved to a specific address, the VDBE will scan
261 ** through its operation list and change all values of P2 which match
262 ** the label into the resolved address.
263 **
264 ** The VDBE knows that a P2 value is a label because labels are
265 ** always negative and P2 values are suppose to be non-negative.
266 ** Hence, a negative P2 value is a label that has yet to be resolved.
267 **
268 ** Zero is returned if a malloc() fails.
269 */
270 int sqlite3VdbeMakeLabel(Vdbe *v){
271   Parse *p = v->pParse;
272   int i = p->nLabel++;
273   assert( v->magic==VDBE_MAGIC_INIT );
274   if( (i & (i-1))==0 ){
275     p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
276                                        (i*2+1)*sizeof(p->aLabel[0]));
277   }
278   if( p->aLabel ){
279     p->aLabel[i] = -1;
280   }
281   return -1-i;
282 }
283 
284 /*
285 ** Resolve label "x" to be the address of the next instruction to
286 ** be inserted.  The parameter "x" must have been obtained from
287 ** a prior call to sqlite3VdbeMakeLabel().
288 */
289 void sqlite3VdbeResolveLabel(Vdbe *v, int x){
290   Parse *p = v->pParse;
291   int j = -1-x;
292   assert( v->magic==VDBE_MAGIC_INIT );
293   assert( j<p->nLabel );
294   if( ALWAYS(j>=0) && p->aLabel ){
295     p->aLabel[j] = v->nOp;
296   }
297   p->iFixedOp = v->nOp - 1;
298 }
299 
300 /*
301 ** Mark the VDBE as one that can only be run one time.
302 */
303 void sqlite3VdbeRunOnlyOnce(Vdbe *p){
304   p->runOnlyOnce = 1;
305 }
306 
307 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
308 
309 /*
310 ** The following type and function are used to iterate through all opcodes
311 ** in a Vdbe main program and each of the sub-programs (triggers) it may
312 ** invoke directly or indirectly. It should be used as follows:
313 **
314 **   Op *pOp;
315 **   VdbeOpIter sIter;
316 **
317 **   memset(&sIter, 0, sizeof(sIter));
318 **   sIter.v = v;                            // v is of type Vdbe*
319 **   while( (pOp = opIterNext(&sIter)) ){
320 **     // Do something with pOp
321 **   }
322 **   sqlite3DbFree(v->db, sIter.apSub);
323 **
324 */
325 typedef struct VdbeOpIter VdbeOpIter;
326 struct VdbeOpIter {
327   Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
328   SubProgram **apSub;        /* Array of subprograms */
329   int nSub;                  /* Number of entries in apSub */
330   int iAddr;                 /* Address of next instruction to return */
331   int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
332 };
333 static Op *opIterNext(VdbeOpIter *p){
334   Vdbe *v = p->v;
335   Op *pRet = 0;
336   Op *aOp;
337   int nOp;
338 
339   if( p->iSub<=p->nSub ){
340 
341     if( p->iSub==0 ){
342       aOp = v->aOp;
343       nOp = v->nOp;
344     }else{
345       aOp = p->apSub[p->iSub-1]->aOp;
346       nOp = p->apSub[p->iSub-1]->nOp;
347     }
348     assert( p->iAddr<nOp );
349 
350     pRet = &aOp[p->iAddr];
351     p->iAddr++;
352     if( p->iAddr==nOp ){
353       p->iSub++;
354       p->iAddr = 0;
355     }
356 
357     if( pRet->p4type==P4_SUBPROGRAM ){
358       int nByte = (p->nSub+1)*sizeof(SubProgram*);
359       int j;
360       for(j=0; j<p->nSub; j++){
361         if( p->apSub[j]==pRet->p4.pProgram ) break;
362       }
363       if( j==p->nSub ){
364         p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
365         if( !p->apSub ){
366           pRet = 0;
367         }else{
368           p->apSub[p->nSub++] = pRet->p4.pProgram;
369         }
370       }
371     }
372   }
373 
374   return pRet;
375 }
376 
377 /*
378 ** Check if the program stored in the VM associated with pParse may
379 ** throw an ABORT exception (causing the statement, but not entire transaction
380 ** to be rolled back). This condition is true if the main program or any
381 ** sub-programs contains any of the following:
382 **
383 **   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
384 **   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
385 **   *  OP_Destroy
386 **   *  OP_VUpdate
387 **   *  OP_VRename
388 **   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
389 **
390 ** Then check that the value of Parse.mayAbort is true if an
391 ** ABORT may be thrown, or false otherwise. Return true if it does
392 ** match, or false otherwise. This function is intended to be used as
393 ** part of an assert statement in the compiler. Similar to:
394 **
395 **   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
396 */
397 int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
398   int hasAbort = 0;
399   int hasFkCounter = 0;
400   Op *pOp;
401   VdbeOpIter sIter;
402   memset(&sIter, 0, sizeof(sIter));
403   sIter.v = v;
404 
405   while( (pOp = opIterNext(&sIter))!=0 ){
406     int opcode = pOp->opcode;
407     if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
408      || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
409       && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
410     ){
411       hasAbort = 1;
412       break;
413     }
414 #ifndef SQLITE_OMIT_FOREIGN_KEY
415     if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
416       hasFkCounter = 1;
417     }
418 #endif
419   }
420   sqlite3DbFree(v->db, sIter.apSub);
421 
422   /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
423   ** If malloc failed, then the while() loop above may not have iterated
424   ** through all opcodes and hasAbort may be set incorrectly. Return
425   ** true for this case to prevent the assert() in the callers frame
426   ** from failing.  */
427   return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter );
428 }
429 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
430 
431 /*
432 ** Loop through the program looking for P2 values that are negative
433 ** on jump instructions.  Each such value is a label.  Resolve the
434 ** label by setting the P2 value to its correct non-zero value.
435 **
436 ** This routine is called once after all opcodes have been inserted.
437 **
438 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
439 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
440 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
441 **
442 ** The Op.opflags field is set on all opcodes.
443 */
444 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
445   int i;
446   int nMaxArgs = *pMaxFuncArgs;
447   Op *pOp;
448   Parse *pParse = p->pParse;
449   int *aLabel = pParse->aLabel;
450   p->readOnly = 1;
451   p->bIsReader = 0;
452   for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
453     u8 opcode = pOp->opcode;
454 
455     /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
456     ** cases from this switch! */
457     switch( opcode ){
458       case OP_Function:
459       case OP_AggStep: {
460         if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
461         break;
462       }
463       case OP_Transaction: {
464         if( pOp->p2!=0 ) p->readOnly = 0;
465         /* fall thru */
466       }
467       case OP_AutoCommit:
468       case OP_Savepoint: {
469         p->bIsReader = 1;
470         break;
471       }
472 #ifndef SQLITE_OMIT_WAL
473       case OP_Checkpoint:
474 #endif
475       case OP_Vacuum:
476       case OP_JournalMode: {
477         p->readOnly = 0;
478         p->bIsReader = 1;
479         break;
480       }
481 #ifndef SQLITE_OMIT_VIRTUALTABLE
482       case OP_VUpdate: {
483         if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
484         break;
485       }
486       case OP_VFilter: {
487         int n;
488         assert( p->nOp - i >= 3 );
489         assert( pOp[-1].opcode==OP_Integer );
490         n = pOp[-1].p1;
491         if( n>nMaxArgs ) nMaxArgs = n;
492         break;
493       }
494 #endif
495       case OP_Next:
496       case OP_NextIfOpen:
497       case OP_SorterNext: {
498         pOp->p4.xAdvance = sqlite3BtreeNext;
499         pOp->p4type = P4_ADVANCE;
500         break;
501       }
502       case OP_Prev:
503       case OP_PrevIfOpen: {
504         pOp->p4.xAdvance = sqlite3BtreePrevious;
505         pOp->p4type = P4_ADVANCE;
506         break;
507       }
508     }
509 
510     pOp->opflags = sqlite3OpcodeProperty[opcode];
511     if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
512       assert( -1-pOp->p2<pParse->nLabel );
513       pOp->p2 = aLabel[-1-pOp->p2];
514     }
515   }
516   sqlite3DbFree(p->db, pParse->aLabel);
517   pParse->aLabel = 0;
518   pParse->nLabel = 0;
519   *pMaxFuncArgs = nMaxArgs;
520   assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
521 }
522 
523 /*
524 ** Return the address of the next instruction to be inserted.
525 */
526 int sqlite3VdbeCurrentAddr(Vdbe *p){
527   assert( p->magic==VDBE_MAGIC_INIT );
528   return p->nOp;
529 }
530 
531 /*
532 ** This function returns a pointer to the array of opcodes associated with
533 ** the Vdbe passed as the first argument. It is the callers responsibility
534 ** to arrange for the returned array to be eventually freed using the
535 ** vdbeFreeOpArray() function.
536 **
537 ** Before returning, *pnOp is set to the number of entries in the returned
538 ** array. Also, *pnMaxArg is set to the larger of its current value and
539 ** the number of entries in the Vdbe.apArg[] array required to execute the
540 ** returned program.
541 */
542 VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
543   VdbeOp *aOp = p->aOp;
544   assert( aOp && !p->db->mallocFailed );
545 
546   /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
547   assert( DbMaskAllZero(p->btreeMask) );
548 
549   resolveP2Values(p, pnMaxArg);
550   *pnOp = p->nOp;
551   p->aOp = 0;
552   return aOp;
553 }
554 
555 /*
556 ** Add a whole list of operations to the operation stack.  Return the
557 ** address of the first operation added.
558 */
559 int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
560   int addr;
561   assert( p->magic==VDBE_MAGIC_INIT );
562   if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
563     return 0;
564   }
565   addr = p->nOp;
566   if( ALWAYS(nOp>0) ){
567     int i;
568     VdbeOpList const *pIn = aOp;
569     for(i=0; i<nOp; i++, pIn++){
570       int p2 = pIn->p2;
571       VdbeOp *pOut = &p->aOp[i+addr];
572       pOut->opcode = pIn->opcode;
573       pOut->p1 = pIn->p1;
574       if( p2<0 ){
575         assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP );
576         pOut->p2 = addr + ADDR(p2);
577       }else{
578         pOut->p2 = p2;
579       }
580       pOut->p3 = pIn->p3;
581       pOut->p4type = P4_NOTUSED;
582       pOut->p4.p = 0;
583       pOut->p5 = 0;
584 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
585       pOut->zComment = 0;
586 #endif
587 #ifdef SQLITE_VDBE_COVERAGE
588       pOut->iSrcLine = iLineno+i;
589 #else
590       (void)iLineno;
591 #endif
592 #ifdef SQLITE_DEBUG
593       if( p->db->flags & SQLITE_VdbeAddopTrace ){
594         sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
595       }
596 #endif
597     }
598     p->nOp += nOp;
599   }
600   return addr;
601 }
602 
603 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
604 /*
605 ** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
606 */
607 void sqlite3VdbeScanStatus(
608   Vdbe *p,                        /* VM to add scanstatus() to */
609   int addrExplain,                /* Address of OP_Explain (or 0) */
610   int addrLoop,                   /* Address of loop counter */
611   int addrVisit,                  /* Address of rows visited counter */
612   LogEst nEst,                    /* Estimated number of output rows */
613   const char *zName               /* Name of table or index being scanned */
614 ){
615   int nByte = (p->nScan+1) * sizeof(ScanStatus);
616   ScanStatus *aNew;
617   aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
618   if( aNew ){
619     ScanStatus *pNew = &aNew[p->nScan++];
620     pNew->addrExplain = addrExplain;
621     pNew->addrLoop = addrLoop;
622     pNew->addrVisit = addrVisit;
623     pNew->nEst = nEst;
624     pNew->zName = sqlite3DbStrDup(p->db, zName);
625     p->aScan = aNew;
626   }
627 }
628 #endif
629 
630 
631 /*
632 ** Change the value of the P1 operand for a specific instruction.
633 ** This routine is useful when a large program is loaded from a
634 ** static array using sqlite3VdbeAddOpList but we want to make a
635 ** few minor changes to the program.
636 */
637 void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
638   assert( p!=0 );
639   if( ((u32)p->nOp)>addr ){
640     p->aOp[addr].p1 = val;
641   }
642 }
643 
644 /*
645 ** Change the value of the P2 operand for a specific instruction.
646 ** This routine is useful for setting a jump destination.
647 */
648 void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
649   assert( p!=0 );
650   if( ((u32)p->nOp)>addr ){
651     p->aOp[addr].p2 = val;
652   }
653 }
654 
655 /*
656 ** Change the value of the P3 operand for a specific instruction.
657 */
658 void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
659   assert( p!=0 );
660   if( ((u32)p->nOp)>addr ){
661     p->aOp[addr].p3 = val;
662   }
663 }
664 
665 /*
666 ** Change the value of the P5 operand for the most recently
667 ** added operation.
668 */
669 void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
670   assert( p!=0 );
671   if( p->aOp ){
672     assert( p->nOp>0 );
673     p->aOp[p->nOp-1].p5 = val;
674   }
675 }
676 
677 /*
678 ** Change the P2 operand of instruction addr so that it points to
679 ** the address of the next instruction to be coded.
680 */
681 void sqlite3VdbeJumpHere(Vdbe *p, int addr){
682   sqlite3VdbeChangeP2(p, addr, p->nOp);
683   p->pParse->iFixedOp = p->nOp - 1;
684 }
685 
686 
687 /*
688 ** If the input FuncDef structure is ephemeral, then free it.  If
689 ** the FuncDef is not ephermal, then do nothing.
690 */
691 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
692   if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
693     sqlite3DbFree(db, pDef);
694   }
695 }
696 
697 static void vdbeFreeOpArray(sqlite3 *, Op *, int);
698 
699 /*
700 ** Delete a P4 value if necessary.
701 */
702 static void freeP4(sqlite3 *db, int p4type, void *p4){
703   if( p4 ){
704     assert( db );
705     switch( p4type ){
706       case P4_REAL:
707       case P4_INT64:
708       case P4_DYNAMIC:
709       case P4_INTARRAY: {
710         sqlite3DbFree(db, p4);
711         break;
712       }
713       case P4_KEYINFO: {
714         if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
715         break;
716       }
717       case P4_MPRINTF: {
718         if( db->pnBytesFreed==0 ) sqlite3_free(p4);
719         break;
720       }
721       case P4_FUNCDEF: {
722         freeEphemeralFunction(db, (FuncDef*)p4);
723         break;
724       }
725       case P4_MEM: {
726         if( db->pnBytesFreed==0 ){
727           sqlite3ValueFree((sqlite3_value*)p4);
728         }else{
729           Mem *p = (Mem*)p4;
730           if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
731           sqlite3DbFree(db, p);
732         }
733         break;
734       }
735       case P4_VTAB : {
736         if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
737         break;
738       }
739     }
740   }
741 }
742 
743 /*
744 ** Free the space allocated for aOp and any p4 values allocated for the
745 ** opcodes contained within. If aOp is not NULL it is assumed to contain
746 ** nOp entries.
747 */
748 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
749   if( aOp ){
750     Op *pOp;
751     for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
752       freeP4(db, pOp->p4type, pOp->p4.p);
753 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
754       sqlite3DbFree(db, pOp->zComment);
755 #endif
756     }
757   }
758   sqlite3DbFree(db, aOp);
759 }
760 
761 /*
762 ** Link the SubProgram object passed as the second argument into the linked
763 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
764 ** objects when the VM is no longer required.
765 */
766 void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
767   p->pNext = pVdbe->pProgram;
768   pVdbe->pProgram = p;
769 }
770 
771 /*
772 ** Change the opcode at addr into OP_Noop
773 */
774 void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
775   if( addr<p->nOp ){
776     VdbeOp *pOp = &p->aOp[addr];
777     sqlite3 *db = p->db;
778     freeP4(db, pOp->p4type, pOp->p4.p);
779     memset(pOp, 0, sizeof(pOp[0]));
780     pOp->opcode = OP_Noop;
781     if( addr==p->nOp-1 ) p->nOp--;
782   }
783 }
784 
785 /*
786 ** If the last opcode is "op" and it is not a jump destination,
787 ** then remove it.  Return true if and only if an opcode was removed.
788 */
789 int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
790   if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
791     sqlite3VdbeChangeToNoop(p, p->nOp-1);
792     return 1;
793   }else{
794     return 0;
795   }
796 }
797 
798 /*
799 ** Change the value of the P4 operand for a specific instruction.
800 ** This routine is useful when a large program is loaded from a
801 ** static array using sqlite3VdbeAddOpList but we want to make a
802 ** few minor changes to the program.
803 **
804 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
805 ** the string is made into memory obtained from sqlite3_malloc().
806 ** A value of n==0 means copy bytes of zP4 up to and including the
807 ** first null byte.  If n>0 then copy n+1 bytes of zP4.
808 **
809 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
810 ** to a string or structure that is guaranteed to exist for the lifetime of
811 ** the Vdbe. In these cases we can just copy the pointer.
812 **
813 ** If addr<0 then change P4 on the most recently inserted instruction.
814 */
815 void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
816   Op *pOp;
817   sqlite3 *db;
818   assert( p!=0 );
819   db = p->db;
820   assert( p->magic==VDBE_MAGIC_INIT );
821   if( p->aOp==0 || db->mallocFailed ){
822     if( n!=P4_VTAB ){
823       freeP4(db, n, (void*)*(char**)&zP4);
824     }
825     return;
826   }
827   assert( p->nOp>0 );
828   assert( addr<p->nOp );
829   if( addr<0 ){
830     addr = p->nOp - 1;
831   }
832   pOp = &p->aOp[addr];
833   assert( pOp->p4type==P4_NOTUSED
834        || pOp->p4type==P4_INT32
835        || pOp->p4type==P4_KEYINFO );
836   freeP4(db, pOp->p4type, pOp->p4.p);
837   pOp->p4.p = 0;
838   if( n==P4_INT32 ){
839     /* Note: this cast is safe, because the origin data point was an int
840     ** that was cast to a (const char *). */
841     pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
842     pOp->p4type = P4_INT32;
843   }else if( zP4==0 ){
844     pOp->p4.p = 0;
845     pOp->p4type = P4_NOTUSED;
846   }else if( n==P4_KEYINFO ){
847     pOp->p4.p = (void*)zP4;
848     pOp->p4type = P4_KEYINFO;
849   }else if( n==P4_VTAB ){
850     pOp->p4.p = (void*)zP4;
851     pOp->p4type = P4_VTAB;
852     sqlite3VtabLock((VTable *)zP4);
853     assert( ((VTable *)zP4)->db==p->db );
854   }else if( n<0 ){
855     pOp->p4.p = (void*)zP4;
856     pOp->p4type = (signed char)n;
857   }else{
858     if( n==0 ) n = sqlite3Strlen30(zP4);
859     pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
860     pOp->p4type = P4_DYNAMIC;
861   }
862 }
863 
864 /*
865 ** Set the P4 on the most recently added opcode to the KeyInfo for the
866 ** index given.
867 */
868 void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
869   Vdbe *v = pParse->pVdbe;
870   assert( v!=0 );
871   assert( pIdx!=0 );
872   sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx),
873                       P4_KEYINFO);
874 }
875 
876 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
877 /*
878 ** Change the comment on the most recently coded instruction.  Or
879 ** insert a No-op and add the comment to that new instruction.  This
880 ** makes the code easier to read during debugging.  None of this happens
881 ** in a production build.
882 */
883 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
884   assert( p->nOp>0 || p->aOp==0 );
885   assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
886   if( p->nOp ){
887     assert( p->aOp );
888     sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
889     p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
890   }
891 }
892 void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
893   va_list ap;
894   if( p ){
895     va_start(ap, zFormat);
896     vdbeVComment(p, zFormat, ap);
897     va_end(ap);
898   }
899 }
900 void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
901   va_list ap;
902   if( p ){
903     sqlite3VdbeAddOp0(p, OP_Noop);
904     va_start(ap, zFormat);
905     vdbeVComment(p, zFormat, ap);
906     va_end(ap);
907   }
908 }
909 #endif  /* NDEBUG */
910 
911 #ifdef SQLITE_VDBE_COVERAGE
912 /*
913 ** Set the value if the iSrcLine field for the previously coded instruction.
914 */
915 void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
916   sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
917 }
918 #endif /* SQLITE_VDBE_COVERAGE */
919 
920 /*
921 ** Return the opcode for a given address.  If the address is -1, then
922 ** return the most recently inserted opcode.
923 **
924 ** If a memory allocation error has occurred prior to the calling of this
925 ** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
926 ** is readable but not writable, though it is cast to a writable value.
927 ** The return of a dummy opcode allows the call to continue functioning
928 ** after an OOM fault without having to check to see if the return from
929 ** this routine is a valid pointer.  But because the dummy.opcode is 0,
930 ** dummy will never be written to.  This is verified by code inspection and
931 ** by running with Valgrind.
932 */
933 VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
934   /* C89 specifies that the constant "dummy" will be initialized to all
935   ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
936   static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
937   assert( p->magic==VDBE_MAGIC_INIT );
938   if( addr<0 ){
939     addr = p->nOp - 1;
940   }
941   assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
942   if( p->db->mallocFailed ){
943     return (VdbeOp*)&dummy;
944   }else{
945     return &p->aOp[addr];
946   }
947 }
948 
949 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
950 /*
951 ** Return an integer value for one of the parameters to the opcode pOp
952 ** determined by character c.
953 */
954 static int translateP(char c, const Op *pOp){
955   if( c=='1' ) return pOp->p1;
956   if( c=='2' ) return pOp->p2;
957   if( c=='3' ) return pOp->p3;
958   if( c=='4' ) return pOp->p4.i;
959   return pOp->p5;
960 }
961 
962 /*
963 ** Compute a string for the "comment" field of a VDBE opcode listing.
964 **
965 ** The Synopsis: field in comments in the vdbe.c source file gets converted
966 ** to an extra string that is appended to the sqlite3OpcodeName().  In the
967 ** absence of other comments, this synopsis becomes the comment on the opcode.
968 ** Some translation occurs:
969 **
970 **       "PX"      ->  "r[X]"
971 **       "PX@PY"   ->  "r[X..X+Y-1]"  or "r[x]" if y is 0 or 1
972 **       "PX@PY+1" ->  "r[X..X+Y]"    or "r[x]" if y is 0
973 **       "PY..PY"  ->  "r[X..Y]"      or "r[x]" if y<=x
974 */
975 static int displayComment(
976   const Op *pOp,     /* The opcode to be commented */
977   const char *zP4,   /* Previously obtained value for P4 */
978   char *zTemp,       /* Write result here */
979   int nTemp          /* Space available in zTemp[] */
980 ){
981   const char *zOpName;
982   const char *zSynopsis;
983   int nOpName;
984   int ii, jj;
985   zOpName = sqlite3OpcodeName(pOp->opcode);
986   nOpName = sqlite3Strlen30(zOpName);
987   if( zOpName[nOpName+1] ){
988     int seenCom = 0;
989     char c;
990     zSynopsis = zOpName += nOpName + 1;
991     for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
992       if( c=='P' ){
993         c = zSynopsis[++ii];
994         if( c=='4' ){
995           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
996         }else if( c=='X' ){
997           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
998           seenCom = 1;
999         }else{
1000           int v1 = translateP(c, pOp);
1001           int v2;
1002           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
1003           if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
1004             ii += 3;
1005             jj += sqlite3Strlen30(zTemp+jj);
1006             v2 = translateP(zSynopsis[ii], pOp);
1007             if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
1008               ii += 2;
1009               v2++;
1010             }
1011             if( v2>1 ){
1012               sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
1013             }
1014           }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
1015             ii += 4;
1016           }
1017         }
1018         jj += sqlite3Strlen30(zTemp+jj);
1019       }else{
1020         zTemp[jj++] = c;
1021       }
1022     }
1023     if( !seenCom && jj<nTemp-5 && pOp->zComment ){
1024       sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
1025       jj += sqlite3Strlen30(zTemp+jj);
1026     }
1027     if( jj<nTemp ) zTemp[jj] = 0;
1028   }else if( pOp->zComment ){
1029     sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
1030     jj = sqlite3Strlen30(zTemp);
1031   }else{
1032     zTemp[0] = 0;
1033     jj = 0;
1034   }
1035   return jj;
1036 }
1037 #endif /* SQLITE_DEBUG */
1038 
1039 
1040 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
1041      || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
1042 /*
1043 ** Compute a string that describes the P4 parameter for an opcode.
1044 ** Use zTemp for any required temporary buffer space.
1045 */
1046 static char *displayP4(Op *pOp, char *zTemp, int nTemp){
1047   char *zP4 = zTemp;
1048   assert( nTemp>=20 );
1049   switch( pOp->p4type ){
1050     case P4_KEYINFO: {
1051       int i, j;
1052       KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
1053       assert( pKeyInfo->aSortOrder!=0 );
1054       sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField);
1055       i = sqlite3Strlen30(zTemp);
1056       for(j=0; j<pKeyInfo->nField; j++){
1057         CollSeq *pColl = pKeyInfo->aColl[j];
1058         const char *zColl = pColl ? pColl->zName : "nil";
1059         int n = sqlite3Strlen30(zColl);
1060         if( n==6 && memcmp(zColl,"BINARY",6)==0 ){
1061           zColl = "B";
1062           n = 1;
1063         }
1064         if( i+n>nTemp-6 ){
1065           memcpy(&zTemp[i],",...",4);
1066           break;
1067         }
1068         zTemp[i++] = ',';
1069         if( pKeyInfo->aSortOrder[j] ){
1070           zTemp[i++] = '-';
1071         }
1072         memcpy(&zTemp[i], zColl, n+1);
1073         i += n;
1074       }
1075       zTemp[i++] = ')';
1076       zTemp[i] = 0;
1077       assert( i<nTemp );
1078       break;
1079     }
1080     case P4_COLLSEQ: {
1081       CollSeq *pColl = pOp->p4.pColl;
1082       sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName);
1083       break;
1084     }
1085     case P4_FUNCDEF: {
1086       FuncDef *pDef = pOp->p4.pFunc;
1087       sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
1088       break;
1089     }
1090     case P4_INT64: {
1091       sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
1092       break;
1093     }
1094     case P4_INT32: {
1095       sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
1096       break;
1097     }
1098     case P4_REAL: {
1099       sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
1100       break;
1101     }
1102     case P4_MEM: {
1103       Mem *pMem = pOp->p4.pMem;
1104       if( pMem->flags & MEM_Str ){
1105         zP4 = pMem->z;
1106       }else if( pMem->flags & MEM_Int ){
1107         sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
1108       }else if( pMem->flags & MEM_Real ){
1109         sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->u.r);
1110       }else if( pMem->flags & MEM_Null ){
1111         sqlite3_snprintf(nTemp, zTemp, "NULL");
1112       }else{
1113         assert( pMem->flags & MEM_Blob );
1114         zP4 = "(blob)";
1115       }
1116       break;
1117     }
1118 #ifndef SQLITE_OMIT_VIRTUALTABLE
1119     case P4_VTAB: {
1120       sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
1121       sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
1122       break;
1123     }
1124 #endif
1125     case P4_INTARRAY: {
1126       sqlite3_snprintf(nTemp, zTemp, "intarray");
1127       break;
1128     }
1129     case P4_SUBPROGRAM: {
1130       sqlite3_snprintf(nTemp, zTemp, "program");
1131       break;
1132     }
1133     case P4_ADVANCE: {
1134       zTemp[0] = 0;
1135       break;
1136     }
1137     default: {
1138       zP4 = pOp->p4.z;
1139       if( zP4==0 ){
1140         zP4 = zTemp;
1141         zTemp[0] = 0;
1142       }
1143     }
1144   }
1145   assert( zP4!=0 );
1146   return zP4;
1147 }
1148 #endif
1149 
1150 /*
1151 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
1152 **
1153 ** The prepared statements need to know in advance the complete set of
1154 ** attached databases that will be use.  A mask of these databases
1155 ** is maintained in p->btreeMask.  The p->lockMask value is the subset of
1156 ** p->btreeMask of databases that will require a lock.
1157 */
1158 void sqlite3VdbeUsesBtree(Vdbe *p, int i){
1159   assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
1160   assert( i<(int)sizeof(p->btreeMask)*8 );
1161   DbMaskSet(p->btreeMask, i);
1162   if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
1163     DbMaskSet(p->lockMask, i);
1164   }
1165 }
1166 
1167 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
1168 /*
1169 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
1170 ** this routine obtains the mutex associated with each BtShared structure
1171 ** that may be accessed by the VM passed as an argument. In doing so it also
1172 ** sets the BtShared.db member of each of the BtShared structures, ensuring
1173 ** that the correct busy-handler callback is invoked if required.
1174 **
1175 ** If SQLite is not threadsafe but does support shared-cache mode, then
1176 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
1177 ** of all of BtShared structures accessible via the database handle
1178 ** associated with the VM.
1179 **
1180 ** If SQLite is not threadsafe and does not support shared-cache mode, this
1181 ** function is a no-op.
1182 **
1183 ** The p->btreeMask field is a bitmask of all btrees that the prepared
1184 ** statement p will ever use.  Let N be the number of bits in p->btreeMask
1185 ** corresponding to btrees that use shared cache.  Then the runtime of
1186 ** this routine is N*N.  But as N is rarely more than 1, this should not
1187 ** be a problem.
1188 */
1189 void sqlite3VdbeEnter(Vdbe *p){
1190   int i;
1191   sqlite3 *db;
1192   Db *aDb;
1193   int nDb;
1194   if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
1195   db = p->db;
1196   aDb = db->aDb;
1197   nDb = db->nDb;
1198   for(i=0; i<nDb; i++){
1199     if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
1200       sqlite3BtreeEnter(aDb[i].pBt);
1201     }
1202   }
1203 }
1204 #endif
1205 
1206 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
1207 /*
1208 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
1209 */
1210 void sqlite3VdbeLeave(Vdbe *p){
1211   int i;
1212   sqlite3 *db;
1213   Db *aDb;
1214   int nDb;
1215   if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
1216   db = p->db;
1217   aDb = db->aDb;
1218   nDb = db->nDb;
1219   for(i=0; i<nDb; i++){
1220     if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
1221       sqlite3BtreeLeave(aDb[i].pBt);
1222     }
1223   }
1224 }
1225 #endif
1226 
1227 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
1228 /*
1229 ** Print a single opcode.  This routine is used for debugging only.
1230 */
1231 void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
1232   char *zP4;
1233   char zPtr[50];
1234   char zCom[100];
1235   static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
1236   if( pOut==0 ) pOut = stdout;
1237   zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
1238 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
1239   displayComment(pOp, zP4, zCom, sizeof(zCom));
1240 #else
1241   zCom[0] = 0;
1242 #endif
1243   /* NB:  The sqlite3OpcodeName() function is implemented by code created
1244   ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
1245   ** information from the vdbe.c source text */
1246   fprintf(pOut, zFormat1, pc,
1247       sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
1248       zCom
1249   );
1250   fflush(pOut);
1251 }
1252 #endif
1253 
1254 /*
1255 ** Release an array of N Mem elements
1256 */
1257 static void releaseMemArray(Mem *p, int N){
1258   if( p && N ){
1259     Mem *pEnd = &p[N];
1260     sqlite3 *db = p->db;
1261     u8 malloc_failed = db->mallocFailed;
1262     if( db->pnBytesFreed ){
1263       do{
1264         if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
1265       }while( (++p)<pEnd );
1266       return;
1267     }
1268     do{
1269       assert( (&p[1])==pEnd || p[0].db==p[1].db );
1270       assert( sqlite3VdbeCheckMemInvariants(p) );
1271 
1272       /* This block is really an inlined version of sqlite3VdbeMemRelease()
1273       ** that takes advantage of the fact that the memory cell value is
1274       ** being set to NULL after releasing any dynamic resources.
1275       **
1276       ** The justification for duplicating code is that according to
1277       ** callgrind, this causes a certain test case to hit the CPU 4.7
1278       ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
1279       ** sqlite3MemRelease() were called from here. With -O2, this jumps
1280       ** to 6.6 percent. The test case is inserting 1000 rows into a table
1281       ** with no indexes using a single prepared INSERT statement, bind()
1282       ** and reset(). Inserts are grouped into a transaction.
1283       */
1284       testcase( p->flags & MEM_Agg );
1285       testcase( p->flags & MEM_Dyn );
1286       testcase( p->flags & MEM_Frame );
1287       testcase( p->flags & MEM_RowSet );
1288       if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
1289         sqlite3VdbeMemRelease(p);
1290       }else if( p->szMalloc ){
1291         sqlite3DbFree(db, p->zMalloc);
1292         p->szMalloc = 0;
1293       }
1294 
1295       p->flags = MEM_Undefined;
1296     }while( (++p)<pEnd );
1297     db->mallocFailed = malloc_failed;
1298   }
1299 }
1300 
1301 /*
1302 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
1303 ** allocated by the OP_Program opcode in sqlite3VdbeExec().
1304 */
1305 void sqlite3VdbeFrameDelete(VdbeFrame *p){
1306   int i;
1307   Mem *aMem = VdbeFrameMem(p);
1308   VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
1309   for(i=0; i<p->nChildCsr; i++){
1310     sqlite3VdbeFreeCursor(p->v, apCsr[i]);
1311   }
1312   releaseMemArray(aMem, p->nChildMem);
1313   sqlite3DbFree(p->v->db, p);
1314 }
1315 
1316 #ifndef SQLITE_OMIT_EXPLAIN
1317 /*
1318 ** Give a listing of the program in the virtual machine.
1319 **
1320 ** The interface is the same as sqlite3VdbeExec().  But instead of
1321 ** running the code, it invokes the callback once for each instruction.
1322 ** This feature is used to implement "EXPLAIN".
1323 **
1324 ** When p->explain==1, each instruction is listed.  When
1325 ** p->explain==2, only OP_Explain instructions are listed and these
1326 ** are shown in a different format.  p->explain==2 is used to implement
1327 ** EXPLAIN QUERY PLAN.
1328 **
1329 ** When p->explain==1, first the main program is listed, then each of
1330 ** the trigger subprograms are listed one by one.
1331 */
1332 int sqlite3VdbeList(
1333   Vdbe *p                   /* The VDBE */
1334 ){
1335   int nRow;                            /* Stop when row count reaches this */
1336   int nSub = 0;                        /* Number of sub-vdbes seen so far */
1337   SubProgram **apSub = 0;              /* Array of sub-vdbes */
1338   Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
1339   sqlite3 *db = p->db;                 /* The database connection */
1340   int i;                               /* Loop counter */
1341   int rc = SQLITE_OK;                  /* Return code */
1342   Mem *pMem = &p->aMem[1];             /* First Mem of result set */
1343 
1344   assert( p->explain );
1345   assert( p->magic==VDBE_MAGIC_RUN );
1346   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
1347 
1348   /* Even though this opcode does not use dynamic strings for
1349   ** the result, result columns may become dynamic if the user calls
1350   ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
1351   */
1352   releaseMemArray(pMem, 8);
1353   p->pResultSet = 0;
1354 
1355   if( p->rc==SQLITE_NOMEM ){
1356     /* This happens if a malloc() inside a call to sqlite3_column_text() or
1357     ** sqlite3_column_text16() failed.  */
1358     db->mallocFailed = 1;
1359     return SQLITE_ERROR;
1360   }
1361 
1362   /* When the number of output rows reaches nRow, that means the
1363   ** listing has finished and sqlite3_step() should return SQLITE_DONE.
1364   ** nRow is the sum of the number of rows in the main program, plus
1365   ** the sum of the number of rows in all trigger subprograms encountered
1366   ** so far.  The nRow value will increase as new trigger subprograms are
1367   ** encountered, but p->pc will eventually catch up to nRow.
1368   */
1369   nRow = p->nOp;
1370   if( p->explain==1 ){
1371     /* The first 8 memory cells are used for the result set.  So we will
1372     ** commandeer the 9th cell to use as storage for an array of pointers
1373     ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
1374     ** cells.  */
1375     assert( p->nMem>9 );
1376     pSub = &p->aMem[9];
1377     if( pSub->flags&MEM_Blob ){
1378       /* On the first call to sqlite3_step(), pSub will hold a NULL.  It is
1379       ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
1380       nSub = pSub->n/sizeof(Vdbe*);
1381       apSub = (SubProgram **)pSub->z;
1382     }
1383     for(i=0; i<nSub; i++){
1384       nRow += apSub[i]->nOp;
1385     }
1386   }
1387 
1388   do{
1389     i = p->pc++;
1390   }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
1391   if( i>=nRow ){
1392     p->rc = SQLITE_OK;
1393     rc = SQLITE_DONE;
1394   }else if( db->u1.isInterrupted ){
1395     p->rc = SQLITE_INTERRUPT;
1396     rc = SQLITE_ERROR;
1397     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
1398   }else{
1399     char *zP4;
1400     Op *pOp;
1401     if( i<p->nOp ){
1402       /* The output line number is small enough that we are still in the
1403       ** main program. */
1404       pOp = &p->aOp[i];
1405     }else{
1406       /* We are currently listing subprograms.  Figure out which one and
1407       ** pick up the appropriate opcode. */
1408       int j;
1409       i -= p->nOp;
1410       for(j=0; i>=apSub[j]->nOp; j++){
1411         i -= apSub[j]->nOp;
1412       }
1413       pOp = &apSub[j]->aOp[i];
1414     }
1415     if( p->explain==1 ){
1416       pMem->flags = MEM_Int;
1417       pMem->u.i = i;                                /* Program counter */
1418       pMem++;
1419 
1420       pMem->flags = MEM_Static|MEM_Str|MEM_Term;
1421       pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
1422       assert( pMem->z!=0 );
1423       pMem->n = sqlite3Strlen30(pMem->z);
1424       pMem->enc = SQLITE_UTF8;
1425       pMem++;
1426 
1427       /* When an OP_Program opcode is encounter (the only opcode that has
1428       ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
1429       ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
1430       ** has not already been seen.
1431       */
1432       if( pOp->p4type==P4_SUBPROGRAM ){
1433         int nByte = (nSub+1)*sizeof(SubProgram*);
1434         int j;
1435         for(j=0; j<nSub; j++){
1436           if( apSub[j]==pOp->p4.pProgram ) break;
1437         }
1438         if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
1439           apSub = (SubProgram **)pSub->z;
1440           apSub[nSub++] = pOp->p4.pProgram;
1441           pSub->flags |= MEM_Blob;
1442           pSub->n = nSub*sizeof(SubProgram*);
1443         }
1444       }
1445     }
1446 
1447     pMem->flags = MEM_Int;
1448     pMem->u.i = pOp->p1;                          /* P1 */
1449     pMem++;
1450 
1451     pMem->flags = MEM_Int;
1452     pMem->u.i = pOp->p2;                          /* P2 */
1453     pMem++;
1454 
1455     pMem->flags = MEM_Int;
1456     pMem->u.i = pOp->p3;                          /* P3 */
1457     pMem++;
1458 
1459     if( sqlite3VdbeMemClearAndResize(pMem, 32) ){ /* P4 */
1460       assert( p->db->mallocFailed );
1461       return SQLITE_ERROR;
1462     }
1463     pMem->flags = MEM_Str|MEM_Term;
1464     zP4 = displayP4(pOp, pMem->z, 32);
1465     if( zP4!=pMem->z ){
1466       sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
1467     }else{
1468       assert( pMem->z!=0 );
1469       pMem->n = sqlite3Strlen30(pMem->z);
1470       pMem->enc = SQLITE_UTF8;
1471     }
1472     pMem++;
1473 
1474     if( p->explain==1 ){
1475       if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
1476         assert( p->db->mallocFailed );
1477         return SQLITE_ERROR;
1478       }
1479       pMem->flags = MEM_Str|MEM_Term;
1480       pMem->n = 2;
1481       sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
1482       pMem->enc = SQLITE_UTF8;
1483       pMem++;
1484 
1485 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
1486       if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
1487         assert( p->db->mallocFailed );
1488         return SQLITE_ERROR;
1489       }
1490       pMem->flags = MEM_Str|MEM_Term;
1491       pMem->n = displayComment(pOp, zP4, pMem->z, 500);
1492       pMem->enc = SQLITE_UTF8;
1493 #else
1494       pMem->flags = MEM_Null;                       /* Comment */
1495 #endif
1496     }
1497 
1498     p->nResColumn = 8 - 4*(p->explain-1);
1499     p->pResultSet = &p->aMem[1];
1500     p->rc = SQLITE_OK;
1501     rc = SQLITE_ROW;
1502   }
1503   return rc;
1504 }
1505 #endif /* SQLITE_OMIT_EXPLAIN */
1506 
1507 #ifdef SQLITE_DEBUG
1508 /*
1509 ** Print the SQL that was used to generate a VDBE program.
1510 */
1511 void sqlite3VdbePrintSql(Vdbe *p){
1512   const char *z = 0;
1513   if( p->zSql ){
1514     z = p->zSql;
1515   }else if( p->nOp>=1 ){
1516     const VdbeOp *pOp = &p->aOp[0];
1517     if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
1518       z = pOp->p4.z;
1519       while( sqlite3Isspace(*z) ) z++;
1520     }
1521   }
1522   if( z ) printf("SQL: [%s]\n", z);
1523 }
1524 #endif
1525 
1526 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
1527 /*
1528 ** Print an IOTRACE message showing SQL content.
1529 */
1530 void sqlite3VdbeIOTraceSql(Vdbe *p){
1531   int nOp = p->nOp;
1532   VdbeOp *pOp;
1533   if( sqlite3IoTrace==0 ) return;
1534   if( nOp<1 ) return;
1535   pOp = &p->aOp[0];
1536   if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
1537     int i, j;
1538     char z[1000];
1539     sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
1540     for(i=0; sqlite3Isspace(z[i]); i++){}
1541     for(j=0; z[i]; i++){
1542       if( sqlite3Isspace(z[i]) ){
1543         if( z[i-1]!=' ' ){
1544           z[j++] = ' ';
1545         }
1546       }else{
1547         z[j++] = z[i];
1548       }
1549     }
1550     z[j] = 0;
1551     sqlite3IoTrace("SQL %s\n", z);
1552   }
1553 }
1554 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
1555 
1556 /*
1557 ** Allocate space from a fixed size buffer and return a pointer to
1558 ** that space.  If insufficient space is available, return NULL.
1559 **
1560 ** The pBuf parameter is the initial value of a pointer which will
1561 ** receive the new memory.  pBuf is normally NULL.  If pBuf is not
1562 ** NULL, it means that memory space has already been allocated and that
1563 ** this routine should not allocate any new memory.  When pBuf is not
1564 ** NULL simply return pBuf.  Only allocate new memory space when pBuf
1565 ** is NULL.
1566 **
1567 ** nByte is the number of bytes of space needed.
1568 **
1569 ** *ppFrom points to available space and pEnd points to the end of the
1570 ** available space.  When space is allocated, *ppFrom is advanced past
1571 ** the end of the allocated space.
1572 **
1573 ** *pnByte is a counter of the number of bytes of space that have failed
1574 ** to allocate.  If there is insufficient space in *ppFrom to satisfy the
1575 ** request, then increment *pnByte by the amount of the request.
1576 */
1577 static void *allocSpace(
1578   void *pBuf,          /* Where return pointer will be stored */
1579   int nByte,           /* Number of bytes to allocate */
1580   u8 **ppFrom,         /* IN/OUT: Allocate from *ppFrom */
1581   u8 *pEnd,            /* Pointer to 1 byte past the end of *ppFrom buffer */
1582   int *pnByte          /* If allocation cannot be made, increment *pnByte */
1583 ){
1584   assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
1585   if( pBuf ) return pBuf;
1586   nByte = ROUND8(nByte);
1587   if( &(*ppFrom)[nByte] <= pEnd ){
1588     pBuf = (void*)*ppFrom;
1589     *ppFrom += nByte;
1590   }else{
1591     *pnByte += nByte;
1592   }
1593   return pBuf;
1594 }
1595 
1596 /*
1597 ** Rewind the VDBE back to the beginning in preparation for
1598 ** running it.
1599 */
1600 void sqlite3VdbeRewind(Vdbe *p){
1601 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
1602   int i;
1603 #endif
1604   assert( p!=0 );
1605   assert( p->magic==VDBE_MAGIC_INIT );
1606 
1607   /* There should be at least one opcode.
1608   */
1609   assert( p->nOp>0 );
1610 
1611   /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
1612   p->magic = VDBE_MAGIC_RUN;
1613 
1614 #ifdef SQLITE_DEBUG
1615   for(i=1; i<p->nMem; i++){
1616     assert( p->aMem[i].db==p->db );
1617   }
1618 #endif
1619   p->pc = -1;
1620   p->rc = SQLITE_OK;
1621   p->errorAction = OE_Abort;
1622   p->magic = VDBE_MAGIC_RUN;
1623   p->nChange = 0;
1624   p->cacheCtr = 1;
1625   p->minWriteFileFormat = 255;
1626   p->iStatement = 0;
1627   p->nFkConstraint = 0;
1628 #ifdef VDBE_PROFILE
1629   for(i=0; i<p->nOp; i++){
1630     p->aOp[i].cnt = 0;
1631     p->aOp[i].cycles = 0;
1632   }
1633 #endif
1634 }
1635 
1636 /*
1637 ** Prepare a virtual machine for execution for the first time after
1638 ** creating the virtual machine.  This involves things such
1639 ** as allocating registers and initializing the program counter.
1640 ** After the VDBE has be prepped, it can be executed by one or more
1641 ** calls to sqlite3VdbeExec().
1642 **
1643 ** This function may be called exactly once on each virtual machine.
1644 ** After this routine is called the VM has been "packaged" and is ready
1645 ** to run.  After this routine is called, further calls to
1646 ** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
1647 ** the Vdbe from the Parse object that helped generate it so that the
1648 ** the Vdbe becomes an independent entity and the Parse object can be
1649 ** destroyed.
1650 **
1651 ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
1652 ** to its initial state after it has been run.
1653 */
1654 void sqlite3VdbeMakeReady(
1655   Vdbe *p,                       /* The VDBE */
1656   Parse *pParse                  /* Parsing context */
1657 ){
1658   sqlite3 *db;                   /* The database connection */
1659   int nVar;                      /* Number of parameters */
1660   int nMem;                      /* Number of VM memory registers */
1661   int nCursor;                   /* Number of cursors required */
1662   int nArg;                      /* Number of arguments in subprograms */
1663   int nOnce;                     /* Number of OP_Once instructions */
1664   int n;                         /* Loop counter */
1665   u8 *zCsr;                      /* Memory available for allocation */
1666   u8 *zEnd;                      /* First byte past allocated memory */
1667   int nByte;                     /* How much extra memory is needed */
1668 
1669   assert( p!=0 );
1670   assert( p->nOp>0 );
1671   assert( pParse!=0 );
1672   assert( p->magic==VDBE_MAGIC_INIT );
1673   assert( pParse==p->pParse );
1674   db = p->db;
1675   assert( db->mallocFailed==0 );
1676   nVar = pParse->nVar;
1677   nMem = pParse->nMem;
1678   nCursor = pParse->nTab;
1679   nArg = pParse->nMaxArg;
1680   nOnce = pParse->nOnce;
1681   if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
1682 
1683   /* For each cursor required, also allocate a memory cell. Memory
1684   ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
1685   ** the vdbe program. Instead they are used to allocate space for
1686   ** VdbeCursor/BtCursor structures. The blob of memory associated with
1687   ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
1688   ** stores the blob of memory associated with cursor 1, etc.
1689   **
1690   ** See also: allocateCursor().
1691   */
1692   nMem += nCursor;
1693 
1694   /* Allocate space for memory registers, SQL variables, VDBE cursors and
1695   ** an array to marshal SQL function arguments in.
1696   */
1697   zCsr = (u8*)&p->aOp[p->nOp];            /* Memory avaliable for allocation */
1698   zEnd = (u8*)&p->aOp[pParse->nOpAlloc];  /* First byte past end of zCsr[] */
1699 
1700   resolveP2Values(p, &nArg);
1701   p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
1702   if( pParse->explain && nMem<10 ){
1703     nMem = 10;
1704   }
1705   memset(zCsr, 0, zEnd-zCsr);
1706   zCsr += (zCsr - (u8*)0)&7;
1707   assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
1708   p->expired = 0;
1709 
1710   /* Memory for registers, parameters, cursor, etc, is allocated in two
1711   ** passes.  On the first pass, we try to reuse unused space at the
1712   ** end of the opcode array.  If we are unable to satisfy all memory
1713   ** requirements by reusing the opcode array tail, then the second
1714   ** pass will fill in the rest using a fresh allocation.
1715   **
1716   ** This two-pass approach that reuses as much memory as possible from
1717   ** the leftover space at the end of the opcode array can significantly
1718   ** reduce the amount of memory held by a prepared statement.
1719   */
1720   do {
1721     nByte = 0;
1722     p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
1723     p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
1724     p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
1725     p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
1726     p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
1727                           &zCsr, zEnd, &nByte);
1728     p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
1729 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
1730     p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), &zCsr, zEnd, &nByte);
1731 #endif
1732     if( nByte ){
1733       p->pFree = sqlite3DbMallocZero(db, nByte);
1734     }
1735     zCsr = p->pFree;
1736     zEnd = &zCsr[nByte];
1737   }while( nByte && !db->mallocFailed );
1738 
1739   p->nCursor = nCursor;
1740   p->nOnceFlag = nOnce;
1741   if( p->aVar ){
1742     p->nVar = (ynVar)nVar;
1743     for(n=0; n<nVar; n++){
1744       p->aVar[n].flags = MEM_Null;
1745       p->aVar[n].db = db;
1746     }
1747   }
1748   if( p->azVar && pParse->nzVar>0 ){
1749     p->nzVar = pParse->nzVar;
1750     memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
1751     memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
1752   }
1753   if( p->aMem ){
1754     p->aMem--;                      /* aMem[] goes from 1..nMem */
1755     p->nMem = nMem;                 /*       not from 0..nMem-1 */
1756     for(n=1; n<=nMem; n++){
1757       p->aMem[n].flags = MEM_Undefined;
1758       p->aMem[n].db = db;
1759     }
1760   }
1761   p->explain = pParse->explain;
1762   sqlite3VdbeRewind(p);
1763 }
1764 
1765 /*
1766 ** Close a VDBE cursor and release all the resources that cursor
1767 ** happens to hold.
1768 */
1769 void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
1770   if( pCx==0 ){
1771     return;
1772   }
1773   sqlite3VdbeSorterClose(p->db, pCx);
1774   if( pCx->pBt ){
1775     sqlite3BtreeClose(pCx->pBt);
1776     /* The pCx->pCursor will be close automatically, if it exists, by
1777     ** the call above. */
1778   }else if( pCx->pCursor ){
1779     sqlite3BtreeCloseCursor(pCx->pCursor);
1780   }
1781 #ifndef SQLITE_OMIT_VIRTUALTABLE
1782   else if( pCx->pVtabCursor ){
1783     sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
1784     const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
1785     p->inVtabMethod = 1;
1786     pModule->xClose(pVtabCursor);
1787     p->inVtabMethod = 0;
1788   }
1789 #endif
1790 }
1791 
1792 /*
1793 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
1794 ** is used, for example, when a trigger sub-program is halted to restore
1795 ** control to the main program.
1796 */
1797 int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
1798   Vdbe *v = pFrame->v;
1799 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
1800   v->anExec = pFrame->anExec;
1801 #endif
1802   v->aOnceFlag = pFrame->aOnceFlag;
1803   v->nOnceFlag = pFrame->nOnceFlag;
1804   v->aOp = pFrame->aOp;
1805   v->nOp = pFrame->nOp;
1806   v->aMem = pFrame->aMem;
1807   v->nMem = pFrame->nMem;
1808   v->apCsr = pFrame->apCsr;
1809   v->nCursor = pFrame->nCursor;
1810   v->db->lastRowid = pFrame->lastRowid;
1811   v->nChange = pFrame->nChange;
1812   v->db->nChange = pFrame->nDbChange;
1813   return pFrame->pc;
1814 }
1815 
1816 /*
1817 ** Close all cursors.
1818 **
1819 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
1820 ** cell array. This is necessary as the memory cell array may contain
1821 ** pointers to VdbeFrame objects, which may in turn contain pointers to
1822 ** open cursors.
1823 */
1824 static void closeAllCursors(Vdbe *p){
1825   if( p->pFrame ){
1826     VdbeFrame *pFrame;
1827     for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
1828     sqlite3VdbeFrameRestore(pFrame);
1829     p->pFrame = 0;
1830     p->nFrame = 0;
1831   }
1832   assert( p->nFrame==0 );
1833 
1834   if( p->apCsr ){
1835     int i;
1836     for(i=0; i<p->nCursor; i++){
1837       VdbeCursor *pC = p->apCsr[i];
1838       if( pC ){
1839         sqlite3VdbeFreeCursor(p, pC);
1840         p->apCsr[i] = 0;
1841       }
1842     }
1843   }
1844   if( p->aMem ){
1845     releaseMemArray(&p->aMem[1], p->nMem);
1846   }
1847   while( p->pDelFrame ){
1848     VdbeFrame *pDel = p->pDelFrame;
1849     p->pDelFrame = pDel->pParent;
1850     sqlite3VdbeFrameDelete(pDel);
1851   }
1852 
1853   /* Delete any auxdata allocations made by the VM */
1854   if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p, -1, 0);
1855   assert( p->pAuxData==0 );
1856 }
1857 
1858 /*
1859 ** Clean up the VM after a single run.
1860 */
1861 static void Cleanup(Vdbe *p){
1862   sqlite3 *db = p->db;
1863 
1864 #ifdef SQLITE_DEBUG
1865   /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
1866   ** Vdbe.aMem[] arrays have already been cleaned up.  */
1867   int i;
1868   if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
1869   if( p->aMem ){
1870     for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
1871   }
1872 #endif
1873 
1874   sqlite3DbFree(db, p->zErrMsg);
1875   p->zErrMsg = 0;
1876   p->pResultSet = 0;
1877 }
1878 
1879 /*
1880 ** Set the number of result columns that will be returned by this SQL
1881 ** statement. This is now set at compile time, rather than during
1882 ** execution of the vdbe program so that sqlite3_column_count() can
1883 ** be called on an SQL statement before sqlite3_step().
1884 */
1885 void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
1886   Mem *pColName;
1887   int n;
1888   sqlite3 *db = p->db;
1889 
1890   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
1891   sqlite3DbFree(db, p->aColName);
1892   n = nResColumn*COLNAME_N;
1893   p->nResColumn = (u16)nResColumn;
1894   p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
1895   if( p->aColName==0 ) return;
1896   while( n-- > 0 ){
1897     pColName->flags = MEM_Null;
1898     pColName->db = p->db;
1899     pColName++;
1900   }
1901 }
1902 
1903 /*
1904 ** Set the name of the idx'th column to be returned by the SQL statement.
1905 ** zName must be a pointer to a nul terminated string.
1906 **
1907 ** This call must be made after a call to sqlite3VdbeSetNumCols().
1908 **
1909 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
1910 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
1911 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
1912 */
1913 int sqlite3VdbeSetColName(
1914   Vdbe *p,                         /* Vdbe being configured */
1915   int idx,                         /* Index of column zName applies to */
1916   int var,                         /* One of the COLNAME_* constants */
1917   const char *zName,               /* Pointer to buffer containing name */
1918   void (*xDel)(void*)              /* Memory management strategy for zName */
1919 ){
1920   int rc;
1921   Mem *pColName;
1922   assert( idx<p->nResColumn );
1923   assert( var<COLNAME_N );
1924   if( p->db->mallocFailed ){
1925     assert( !zName || xDel!=SQLITE_DYNAMIC );
1926     return SQLITE_NOMEM;
1927   }
1928   assert( p->aColName!=0 );
1929   pColName = &(p->aColName[idx+var*p->nResColumn]);
1930   rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
1931   assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
1932   return rc;
1933 }
1934 
1935 /*
1936 ** A read or write transaction may or may not be active on database handle
1937 ** db. If a transaction is active, commit it. If there is a
1938 ** write-transaction spanning more than one database file, this routine
1939 ** takes care of the master journal trickery.
1940 */
1941 static int vdbeCommit(sqlite3 *db, Vdbe *p){
1942   int i;
1943   int nTrans = 0;  /* Number of databases with an active write-transaction */
1944   int rc = SQLITE_OK;
1945   int needXcommit = 0;
1946 
1947 #ifdef SQLITE_OMIT_VIRTUALTABLE
1948   /* With this option, sqlite3VtabSync() is defined to be simply
1949   ** SQLITE_OK so p is not used.
1950   */
1951   UNUSED_PARAMETER(p);
1952 #endif
1953 
1954   /* Before doing anything else, call the xSync() callback for any
1955   ** virtual module tables written in this transaction. This has to
1956   ** be done before determining whether a master journal file is
1957   ** required, as an xSync() callback may add an attached database
1958   ** to the transaction.
1959   */
1960   rc = sqlite3VtabSync(db, p);
1961 
1962   /* This loop determines (a) if the commit hook should be invoked and
1963   ** (b) how many database files have open write transactions, not
1964   ** including the temp database. (b) is important because if more than
1965   ** one database file has an open write transaction, a master journal
1966   ** file is required for an atomic commit.
1967   */
1968   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1969     Btree *pBt = db->aDb[i].pBt;
1970     if( sqlite3BtreeIsInTrans(pBt) ){
1971       needXcommit = 1;
1972       if( i!=1 ) nTrans++;
1973       sqlite3BtreeEnter(pBt);
1974       rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
1975       sqlite3BtreeLeave(pBt);
1976     }
1977   }
1978   if( rc!=SQLITE_OK ){
1979     return rc;
1980   }
1981 
1982   /* If there are any write-transactions at all, invoke the commit hook */
1983   if( needXcommit && db->xCommitCallback ){
1984     rc = db->xCommitCallback(db->pCommitArg);
1985     if( rc ){
1986       return SQLITE_CONSTRAINT_COMMITHOOK;
1987     }
1988   }
1989 
1990   /* The simple case - no more than one database file (not counting the
1991   ** TEMP database) has a transaction active.   There is no need for the
1992   ** master-journal.
1993   **
1994   ** If the return value of sqlite3BtreeGetFilename() is a zero length
1995   ** string, it means the main database is :memory: or a temp file.  In
1996   ** that case we do not support atomic multi-file commits, so use the
1997   ** simple case then too.
1998   */
1999   if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
2000    || nTrans<=1
2001   ){
2002     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
2003       Btree *pBt = db->aDb[i].pBt;
2004       if( pBt ){
2005         rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
2006       }
2007     }
2008 
2009     /* Do the commit only if all databases successfully complete phase 1.
2010     ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
2011     ** IO error while deleting or truncating a journal file. It is unlikely,
2012     ** but could happen. In this case abandon processing and return the error.
2013     */
2014     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
2015       Btree *pBt = db->aDb[i].pBt;
2016       if( pBt ){
2017         rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
2018       }
2019     }
2020     if( rc==SQLITE_OK ){
2021       sqlite3VtabCommit(db);
2022     }
2023   }
2024 
2025   /* The complex case - There is a multi-file write-transaction active.
2026   ** This requires a master journal file to ensure the transaction is
2027   ** committed atomically.
2028   */
2029 #ifndef SQLITE_OMIT_DISKIO
2030   else{
2031     sqlite3_vfs *pVfs = db->pVfs;
2032     int needSync = 0;
2033     char *zMaster = 0;   /* File-name for the master journal */
2034     char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
2035     sqlite3_file *pMaster = 0;
2036     i64 offset = 0;
2037     int res;
2038     int retryCount = 0;
2039     int nMainFile;
2040 
2041     /* Select a master journal file name */
2042     nMainFile = sqlite3Strlen30(zMainFile);
2043     zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
2044     if( zMaster==0 ) return SQLITE_NOMEM;
2045     do {
2046       u32 iRandom;
2047       if( retryCount ){
2048         if( retryCount>100 ){
2049           sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
2050           sqlite3OsDelete(pVfs, zMaster, 0);
2051           break;
2052         }else if( retryCount==1 ){
2053           sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
2054         }
2055       }
2056       retryCount++;
2057       sqlite3_randomness(sizeof(iRandom), &iRandom);
2058       sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
2059                                (iRandom>>8)&0xffffff, iRandom&0xff);
2060       /* The antipenultimate character of the master journal name must
2061       ** be "9" to avoid name collisions when using 8+3 filenames. */
2062       assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
2063       sqlite3FileSuffix3(zMainFile, zMaster);
2064       rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
2065     }while( rc==SQLITE_OK && res );
2066     if( rc==SQLITE_OK ){
2067       /* Open the master journal. */
2068       rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
2069           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
2070           SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
2071       );
2072     }
2073     if( rc!=SQLITE_OK ){
2074       sqlite3DbFree(db, zMaster);
2075       return rc;
2076     }
2077 
2078     /* Write the name of each database file in the transaction into the new
2079     ** master journal file. If an error occurs at this point close
2080     ** and delete the master journal file. All the individual journal files
2081     ** still have 'null' as the master journal pointer, so they will roll
2082     ** back independently if a failure occurs.
2083     */
2084     for(i=0; i<db->nDb; i++){
2085       Btree *pBt = db->aDb[i].pBt;
2086       if( sqlite3BtreeIsInTrans(pBt) ){
2087         char const *zFile = sqlite3BtreeGetJournalname(pBt);
2088         if( zFile==0 ){
2089           continue;  /* Ignore TEMP and :memory: databases */
2090         }
2091         assert( zFile[0]!=0 );
2092         if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
2093           needSync = 1;
2094         }
2095         rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
2096         offset += sqlite3Strlen30(zFile)+1;
2097         if( rc!=SQLITE_OK ){
2098           sqlite3OsCloseFree(pMaster);
2099           sqlite3OsDelete(pVfs, zMaster, 0);
2100           sqlite3DbFree(db, zMaster);
2101           return rc;
2102         }
2103       }
2104     }
2105 
2106     /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
2107     ** flag is set this is not required.
2108     */
2109     if( needSync
2110      && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
2111      && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
2112     ){
2113       sqlite3OsCloseFree(pMaster);
2114       sqlite3OsDelete(pVfs, zMaster, 0);
2115       sqlite3DbFree(db, zMaster);
2116       return rc;
2117     }
2118 
2119     /* Sync all the db files involved in the transaction. The same call
2120     ** sets the master journal pointer in each individual journal. If
2121     ** an error occurs here, do not delete the master journal file.
2122     **
2123     ** If the error occurs during the first call to
2124     ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
2125     ** master journal file will be orphaned. But we cannot delete it,
2126     ** in case the master journal file name was written into the journal
2127     ** file before the failure occurred.
2128     */
2129     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
2130       Btree *pBt = db->aDb[i].pBt;
2131       if( pBt ){
2132         rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
2133       }
2134     }
2135     sqlite3OsCloseFree(pMaster);
2136     assert( rc!=SQLITE_BUSY );
2137     if( rc!=SQLITE_OK ){
2138       sqlite3DbFree(db, zMaster);
2139       return rc;
2140     }
2141 
2142     /* Delete the master journal file. This commits the transaction. After
2143     ** doing this the directory is synced again before any individual
2144     ** transaction files are deleted.
2145     */
2146     rc = sqlite3OsDelete(pVfs, zMaster, 1);
2147     sqlite3DbFree(db, zMaster);
2148     zMaster = 0;
2149     if( rc ){
2150       return rc;
2151     }
2152 
2153     /* All files and directories have already been synced, so the following
2154     ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
2155     ** deleting or truncating journals. If something goes wrong while
2156     ** this is happening we don't really care. The integrity of the
2157     ** transaction is already guaranteed, but some stray 'cold' journals
2158     ** may be lying around. Returning an error code won't help matters.
2159     */
2160     disable_simulated_io_errors();
2161     sqlite3BeginBenignMalloc();
2162     for(i=0; i<db->nDb; i++){
2163       Btree *pBt = db->aDb[i].pBt;
2164       if( pBt ){
2165         sqlite3BtreeCommitPhaseTwo(pBt, 1);
2166       }
2167     }
2168     sqlite3EndBenignMalloc();
2169     enable_simulated_io_errors();
2170 
2171     sqlite3VtabCommit(db);
2172   }
2173 #endif
2174 
2175   return rc;
2176 }
2177 
2178 /*
2179 ** This routine checks that the sqlite3.nVdbeActive count variable
2180 ** matches the number of vdbe's in the list sqlite3.pVdbe that are
2181 ** currently active. An assertion fails if the two counts do not match.
2182 ** This is an internal self-check only - it is not an essential processing
2183 ** step.
2184 **
2185 ** This is a no-op if NDEBUG is defined.
2186 */
2187 #ifndef NDEBUG
2188 static void checkActiveVdbeCnt(sqlite3 *db){
2189   Vdbe *p;
2190   int cnt = 0;
2191   int nWrite = 0;
2192   int nRead = 0;
2193   p = db->pVdbe;
2194   while( p ){
2195     if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
2196       cnt++;
2197       if( p->readOnly==0 ) nWrite++;
2198       if( p->bIsReader ) nRead++;
2199     }
2200     p = p->pNext;
2201   }
2202   assert( cnt==db->nVdbeActive );
2203   assert( nWrite==db->nVdbeWrite );
2204   assert( nRead==db->nVdbeRead );
2205 }
2206 #else
2207 #define checkActiveVdbeCnt(x)
2208 #endif
2209 
2210 /*
2211 ** If the Vdbe passed as the first argument opened a statement-transaction,
2212 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
2213 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
2214 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
2215 ** statement transaction is committed.
2216 **
2217 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
2218 ** Otherwise SQLITE_OK.
2219 */
2220 int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
2221   sqlite3 *const db = p->db;
2222   int rc = SQLITE_OK;
2223 
2224   /* If p->iStatement is greater than zero, then this Vdbe opened a
2225   ** statement transaction that should be closed here. The only exception
2226   ** is that an IO error may have occurred, causing an emergency rollback.
2227   ** In this case (db->nStatement==0), and there is nothing to do.
2228   */
2229   if( db->nStatement && p->iStatement ){
2230     int i;
2231     const int iSavepoint = p->iStatement-1;
2232 
2233     assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
2234     assert( db->nStatement>0 );
2235     assert( p->iStatement==(db->nStatement+db->nSavepoint) );
2236 
2237     for(i=0; i<db->nDb; i++){
2238       int rc2 = SQLITE_OK;
2239       Btree *pBt = db->aDb[i].pBt;
2240       if( pBt ){
2241         if( eOp==SAVEPOINT_ROLLBACK ){
2242           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
2243         }
2244         if( rc2==SQLITE_OK ){
2245           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
2246         }
2247         if( rc==SQLITE_OK ){
2248           rc = rc2;
2249         }
2250       }
2251     }
2252     db->nStatement--;
2253     p->iStatement = 0;
2254 
2255     if( rc==SQLITE_OK ){
2256       if( eOp==SAVEPOINT_ROLLBACK ){
2257         rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
2258       }
2259       if( rc==SQLITE_OK ){
2260         rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
2261       }
2262     }
2263 
2264     /* If the statement transaction is being rolled back, also restore the
2265     ** database handles deferred constraint counter to the value it had when
2266     ** the statement transaction was opened.  */
2267     if( eOp==SAVEPOINT_ROLLBACK ){
2268       db->nDeferredCons = p->nStmtDefCons;
2269       db->nDeferredImmCons = p->nStmtDefImmCons;
2270     }
2271   }
2272   return rc;
2273 }
2274 
2275 /*
2276 ** This function is called when a transaction opened by the database
2277 ** handle associated with the VM passed as an argument is about to be
2278 ** committed. If there are outstanding deferred foreign key constraint
2279 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
2280 **
2281 ** If there are outstanding FK violations and this function returns
2282 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
2283 ** and write an error message to it. Then return SQLITE_ERROR.
2284 */
2285 #ifndef SQLITE_OMIT_FOREIGN_KEY
2286 int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
2287   sqlite3 *db = p->db;
2288   if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
2289    || (!deferred && p->nFkConstraint>0)
2290   ){
2291     p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
2292     p->errorAction = OE_Abort;
2293     sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed");
2294     return SQLITE_ERROR;
2295   }
2296   return SQLITE_OK;
2297 }
2298 #endif
2299 
2300 /*
2301 ** This routine is called the when a VDBE tries to halt.  If the VDBE
2302 ** has made changes and is in autocommit mode, then commit those
2303 ** changes.  If a rollback is needed, then do the rollback.
2304 **
2305 ** This routine is the only way to move the state of a VM from
2306 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
2307 ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
2308 **
2309 ** Return an error code.  If the commit could not complete because of
2310 ** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
2311 ** means the close did not happen and needs to be repeated.
2312 */
2313 int sqlite3VdbeHalt(Vdbe *p){
2314   int rc;                         /* Used to store transient return codes */
2315   sqlite3 *db = p->db;
2316 
2317   /* This function contains the logic that determines if a statement or
2318   ** transaction will be committed or rolled back as a result of the
2319   ** execution of this virtual machine.
2320   **
2321   ** If any of the following errors occur:
2322   **
2323   **     SQLITE_NOMEM
2324   **     SQLITE_IOERR
2325   **     SQLITE_FULL
2326   **     SQLITE_INTERRUPT
2327   **
2328   ** Then the internal cache might have been left in an inconsistent
2329   ** state.  We need to rollback the statement transaction, if there is
2330   ** one, or the complete transaction if there is no statement transaction.
2331   */
2332 
2333   if( p->db->mallocFailed ){
2334     p->rc = SQLITE_NOMEM;
2335   }
2336   if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
2337   closeAllCursors(p);
2338   if( p->magic!=VDBE_MAGIC_RUN ){
2339     return SQLITE_OK;
2340   }
2341   checkActiveVdbeCnt(db);
2342 
2343   /* No commit or rollback needed if the program never started or if the
2344   ** SQL statement does not read or write a database file.  */
2345   if( p->pc>=0 && p->bIsReader ){
2346     int mrc;   /* Primary error code from p->rc */
2347     int eStatementOp = 0;
2348     int isSpecialError;            /* Set to true if a 'special' error */
2349 
2350     /* Lock all btrees used by the statement */
2351     sqlite3VdbeEnter(p);
2352 
2353     /* Check for one of the special errors */
2354     mrc = p->rc & 0xff;
2355     isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
2356                      || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
2357     if( isSpecialError ){
2358       /* If the query was read-only and the error code is SQLITE_INTERRUPT,
2359       ** no rollback is necessary. Otherwise, at least a savepoint
2360       ** transaction must be rolled back to restore the database to a
2361       ** consistent state.
2362       **
2363       ** Even if the statement is read-only, it is important to perform
2364       ** a statement or transaction rollback operation. If the error
2365       ** occurred while writing to the journal, sub-journal or database
2366       ** file as part of an effort to free up cache space (see function
2367       ** pagerStress() in pager.c), the rollback is required to restore
2368       ** the pager to a consistent state.
2369       */
2370       if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
2371         if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
2372           eStatementOp = SAVEPOINT_ROLLBACK;
2373         }else{
2374           /* We are forced to roll back the active transaction. Before doing
2375           ** so, abort any other statements this handle currently has active.
2376           */
2377           sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
2378           sqlite3CloseSavepoints(db);
2379           db->autoCommit = 1;
2380           p->nChange = 0;
2381         }
2382       }
2383     }
2384 
2385     /* Check for immediate foreign key violations. */
2386     if( p->rc==SQLITE_OK ){
2387       sqlite3VdbeCheckFk(p, 0);
2388     }
2389 
2390     /* If the auto-commit flag is set and this is the only active writer
2391     ** VM, then we do either a commit or rollback of the current transaction.
2392     **
2393     ** Note: This block also runs if one of the special errors handled
2394     ** above has occurred.
2395     */
2396     if( !sqlite3VtabInSync(db)
2397      && db->autoCommit
2398      && db->nVdbeWrite==(p->readOnly==0)
2399     ){
2400       if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
2401         rc = sqlite3VdbeCheckFk(p, 1);
2402         if( rc!=SQLITE_OK ){
2403           if( NEVER(p->readOnly) ){
2404             sqlite3VdbeLeave(p);
2405             return SQLITE_ERROR;
2406           }
2407           rc = SQLITE_CONSTRAINT_FOREIGNKEY;
2408         }else{
2409           /* The auto-commit flag is true, the vdbe program was successful
2410           ** or hit an 'OR FAIL' constraint and there are no deferred foreign
2411           ** key constraints to hold up the transaction. This means a commit
2412           ** is required. */
2413           rc = vdbeCommit(db, p);
2414         }
2415         if( rc==SQLITE_BUSY && p->readOnly ){
2416           sqlite3VdbeLeave(p);
2417           return SQLITE_BUSY;
2418         }else if( rc!=SQLITE_OK ){
2419           p->rc = rc;
2420           sqlite3RollbackAll(db, SQLITE_OK);
2421           p->nChange = 0;
2422         }else{
2423           db->nDeferredCons = 0;
2424           db->nDeferredImmCons = 0;
2425           db->flags &= ~SQLITE_DeferFKs;
2426           sqlite3CommitInternalChanges(db);
2427         }
2428       }else{
2429         sqlite3RollbackAll(db, SQLITE_OK);
2430         p->nChange = 0;
2431       }
2432       db->nStatement = 0;
2433     }else if( eStatementOp==0 ){
2434       if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
2435         eStatementOp = SAVEPOINT_RELEASE;
2436       }else if( p->errorAction==OE_Abort ){
2437         eStatementOp = SAVEPOINT_ROLLBACK;
2438       }else{
2439         sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
2440         sqlite3CloseSavepoints(db);
2441         db->autoCommit = 1;
2442         p->nChange = 0;
2443       }
2444     }
2445 
2446     /* If eStatementOp is non-zero, then a statement transaction needs to
2447     ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
2448     ** do so. If this operation returns an error, and the current statement
2449     ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
2450     ** current statement error code.
2451     */
2452     if( eStatementOp ){
2453       rc = sqlite3VdbeCloseStatement(p, eStatementOp);
2454       if( rc ){
2455         if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
2456           p->rc = rc;
2457           sqlite3DbFree(db, p->zErrMsg);
2458           p->zErrMsg = 0;
2459         }
2460         sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
2461         sqlite3CloseSavepoints(db);
2462         db->autoCommit = 1;
2463         p->nChange = 0;
2464       }
2465     }
2466 
2467     /* If this was an INSERT, UPDATE or DELETE and no statement transaction
2468     ** has been rolled back, update the database connection change-counter.
2469     */
2470     if( p->changeCntOn ){
2471       if( eStatementOp!=SAVEPOINT_ROLLBACK ){
2472         sqlite3VdbeSetChanges(db, p->nChange);
2473       }else{
2474         sqlite3VdbeSetChanges(db, 0);
2475       }
2476       p->nChange = 0;
2477     }
2478 
2479     /* Release the locks */
2480     sqlite3VdbeLeave(p);
2481   }
2482 
2483   /* We have successfully halted and closed the VM.  Record this fact. */
2484   if( p->pc>=0 ){
2485     db->nVdbeActive--;
2486     if( !p->readOnly ) db->nVdbeWrite--;
2487     if( p->bIsReader ) db->nVdbeRead--;
2488     assert( db->nVdbeActive>=db->nVdbeRead );
2489     assert( db->nVdbeRead>=db->nVdbeWrite );
2490     assert( db->nVdbeWrite>=0 );
2491   }
2492   p->magic = VDBE_MAGIC_HALT;
2493   checkActiveVdbeCnt(db);
2494   if( p->db->mallocFailed ){
2495     p->rc = SQLITE_NOMEM;
2496   }
2497 
2498   /* If the auto-commit flag is set to true, then any locks that were held
2499   ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
2500   ** to invoke any required unlock-notify callbacks.
2501   */
2502   if( db->autoCommit ){
2503     sqlite3ConnectionUnlocked(db);
2504   }
2505 
2506   assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
2507   return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
2508 }
2509 
2510 
2511 /*
2512 ** Each VDBE holds the result of the most recent sqlite3_step() call
2513 ** in p->rc.  This routine sets that result back to SQLITE_OK.
2514 */
2515 void sqlite3VdbeResetStepResult(Vdbe *p){
2516   p->rc = SQLITE_OK;
2517 }
2518 
2519 /*
2520 ** Copy the error code and error message belonging to the VDBE passed
2521 ** as the first argument to its database handle (so that they will be
2522 ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
2523 **
2524 ** This function does not clear the VDBE error code or message, just
2525 ** copies them to the database handle.
2526 */
2527 int sqlite3VdbeTransferError(Vdbe *p){
2528   sqlite3 *db = p->db;
2529   int rc = p->rc;
2530   if( p->zErrMsg ){
2531     u8 mallocFailed = db->mallocFailed;
2532     sqlite3BeginBenignMalloc();
2533     if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
2534     sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
2535     sqlite3EndBenignMalloc();
2536     db->mallocFailed = mallocFailed;
2537     db->errCode = rc;
2538   }else{
2539     sqlite3Error(db, rc);
2540   }
2541   return rc;
2542 }
2543 
2544 #ifdef SQLITE_ENABLE_SQLLOG
2545 /*
2546 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
2547 ** invoke it.
2548 */
2549 static void vdbeInvokeSqllog(Vdbe *v){
2550   if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
2551     char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
2552     assert( v->db->init.busy==0 );
2553     if( zExpanded ){
2554       sqlite3GlobalConfig.xSqllog(
2555           sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
2556       );
2557       sqlite3DbFree(v->db, zExpanded);
2558     }
2559   }
2560 }
2561 #else
2562 # define vdbeInvokeSqllog(x)
2563 #endif
2564 
2565 /*
2566 ** Clean up a VDBE after execution but do not delete the VDBE just yet.
2567 ** Write any error messages into *pzErrMsg.  Return the result code.
2568 **
2569 ** After this routine is run, the VDBE should be ready to be executed
2570 ** again.
2571 **
2572 ** To look at it another way, this routine resets the state of the
2573 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
2574 ** VDBE_MAGIC_INIT.
2575 */
2576 int sqlite3VdbeReset(Vdbe *p){
2577   sqlite3 *db;
2578   db = p->db;
2579 
2580   /* If the VM did not run to completion or if it encountered an
2581   ** error, then it might not have been halted properly.  So halt
2582   ** it now.
2583   */
2584   sqlite3VdbeHalt(p);
2585 
2586   /* If the VDBE has be run even partially, then transfer the error code
2587   ** and error message from the VDBE into the main database structure.  But
2588   ** if the VDBE has just been set to run but has not actually executed any
2589   ** instructions yet, leave the main database error information unchanged.
2590   */
2591   if( p->pc>=0 ){
2592     vdbeInvokeSqllog(p);
2593     sqlite3VdbeTransferError(p);
2594     sqlite3DbFree(db, p->zErrMsg);
2595     p->zErrMsg = 0;
2596     if( p->runOnlyOnce ) p->expired = 1;
2597   }else if( p->rc && p->expired ){
2598     /* The expired flag was set on the VDBE before the first call
2599     ** to sqlite3_step(). For consistency (since sqlite3_step() was
2600     ** called), set the database error in this case as well.
2601     */
2602     sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
2603     sqlite3DbFree(db, p->zErrMsg);
2604     p->zErrMsg = 0;
2605   }
2606 
2607   /* Reclaim all memory used by the VDBE
2608   */
2609   Cleanup(p);
2610 
2611   /* Save profiling information from this VDBE run.
2612   */
2613 #ifdef VDBE_PROFILE
2614   {
2615     FILE *out = fopen("vdbe_profile.out", "a");
2616     if( out ){
2617       int i;
2618       fprintf(out, "---- ");
2619       for(i=0; i<p->nOp; i++){
2620         fprintf(out, "%02x", p->aOp[i].opcode);
2621       }
2622       fprintf(out, "\n");
2623       if( p->zSql ){
2624         char c, pc = 0;
2625         fprintf(out, "-- ");
2626         for(i=0; (c = p->zSql[i])!=0; i++){
2627           if( pc=='\n' ) fprintf(out, "-- ");
2628           putc(c, out);
2629           pc = c;
2630         }
2631         if( pc!='\n' ) fprintf(out, "\n");
2632       }
2633       for(i=0; i<p->nOp; i++){
2634         char zHdr[100];
2635         sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
2636            p->aOp[i].cnt,
2637            p->aOp[i].cycles,
2638            p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
2639         );
2640         fprintf(out, "%s", zHdr);
2641         sqlite3VdbePrintOp(out, i, &p->aOp[i]);
2642       }
2643       fclose(out);
2644     }
2645   }
2646 #endif
2647   p->iCurrentTime = 0;
2648   p->magic = VDBE_MAGIC_INIT;
2649   return p->rc & db->errMask;
2650 }
2651 
2652 /*
2653 ** Clean up and delete a VDBE after execution.  Return an integer which is
2654 ** the result code.  Write any error message text into *pzErrMsg.
2655 */
2656 int sqlite3VdbeFinalize(Vdbe *p){
2657   int rc = SQLITE_OK;
2658   if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
2659     rc = sqlite3VdbeReset(p);
2660     assert( (rc & p->db->errMask)==rc );
2661   }
2662   sqlite3VdbeDelete(p);
2663   return rc;
2664 }
2665 
2666 /*
2667 ** If parameter iOp is less than zero, then invoke the destructor for
2668 ** all auxiliary data pointers currently cached by the VM passed as
2669 ** the first argument.
2670 **
2671 ** Or, if iOp is greater than or equal to zero, then the destructor is
2672 ** only invoked for those auxiliary data pointers created by the user
2673 ** function invoked by the OP_Function opcode at instruction iOp of
2674 ** VM pVdbe, and only then if:
2675 **
2676 **    * the associated function parameter is the 32nd or later (counting
2677 **      from left to right), or
2678 **
2679 **    * the corresponding bit in argument mask is clear (where the first
2680 **      function parameter corresponds to bit 0 etc.).
2681 */
2682 void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
2683   AuxData **pp = &pVdbe->pAuxData;
2684   while( *pp ){
2685     AuxData *pAux = *pp;
2686     if( (iOp<0)
2687      || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
2688     ){
2689       testcase( pAux->iArg==31 );
2690       if( pAux->xDelete ){
2691         pAux->xDelete(pAux->pAux);
2692       }
2693       *pp = pAux->pNext;
2694       sqlite3DbFree(pVdbe->db, pAux);
2695     }else{
2696       pp= &pAux->pNext;
2697     }
2698   }
2699 }
2700 
2701 /*
2702 ** Free all memory associated with the Vdbe passed as the second argument,
2703 ** except for object itself, which is preserved.
2704 **
2705 ** The difference between this function and sqlite3VdbeDelete() is that
2706 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
2707 ** the database connection and frees the object itself.
2708 */
2709 void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
2710   SubProgram *pSub, *pNext;
2711   int i;
2712   assert( p->db==0 || p->db==db );
2713   releaseMemArray(p->aVar, p->nVar);
2714   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
2715   for(pSub=p->pProgram; pSub; pSub=pNext){
2716     pNext = pSub->pNext;
2717     vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
2718     sqlite3DbFree(db, pSub);
2719   }
2720   for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
2721   vdbeFreeOpArray(db, p->aOp, p->nOp);
2722   sqlite3DbFree(db, p->aColName);
2723   sqlite3DbFree(db, p->zSql);
2724   sqlite3DbFree(db, p->pFree);
2725 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
2726   for(i=0; i<p->nScan; i++){
2727     sqlite3DbFree(db, p->aScan[i].zName);
2728   }
2729   sqlite3DbFree(db, p->aScan);
2730 #endif
2731 }
2732 
2733 /*
2734 ** Delete an entire VDBE.
2735 */
2736 void sqlite3VdbeDelete(Vdbe *p){
2737   sqlite3 *db;
2738 
2739   if( NEVER(p==0) ) return;
2740   db = p->db;
2741   assert( sqlite3_mutex_held(db->mutex) );
2742   sqlite3VdbeClearObject(db, p);
2743   if( p->pPrev ){
2744     p->pPrev->pNext = p->pNext;
2745   }else{
2746     assert( db->pVdbe==p );
2747     db->pVdbe = p->pNext;
2748   }
2749   if( p->pNext ){
2750     p->pNext->pPrev = p->pPrev;
2751   }
2752   p->magic = VDBE_MAGIC_DEAD;
2753   p->db = 0;
2754   sqlite3DbFree(db, p);
2755 }
2756 
2757 /*
2758 ** The cursor "p" has a pending seek operation that has not yet been
2759 ** carried out.  Seek the cursor now.  If an error occurs, return
2760 ** the appropriate error code.
2761 */
2762 static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){
2763   int res, rc;
2764 #ifdef SQLITE_TEST
2765   extern int sqlite3_search_count;
2766 #endif
2767   assert( p->deferredMoveto );
2768   assert( p->isTable );
2769   rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
2770   if( rc ) return rc;
2771   if( res!=0 ) return SQLITE_CORRUPT_BKPT;
2772 #ifdef SQLITE_TEST
2773   sqlite3_search_count++;
2774 #endif
2775   p->deferredMoveto = 0;
2776   p->cacheStatus = CACHE_STALE;
2777   return SQLITE_OK;
2778 }
2779 
2780 /*
2781 ** Something has moved cursor "p" out of place.  Maybe the row it was
2782 ** pointed to was deleted out from under it.  Or maybe the btree was
2783 ** rebalanced.  Whatever the cause, try to restore "p" to the place it
2784 ** is supposed to be pointing.  If the row was deleted out from under the
2785 ** cursor, set the cursor to point to a NULL row.
2786 */
2787 static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
2788   int isDifferentRow, rc;
2789   assert( p->pCursor!=0 );
2790   assert( sqlite3BtreeCursorHasMoved(p->pCursor) );
2791   rc = sqlite3BtreeCursorRestore(p->pCursor, &isDifferentRow);
2792   p->cacheStatus = CACHE_STALE;
2793   if( isDifferentRow ) p->nullRow = 1;
2794   return rc;
2795 }
2796 
2797 /*
2798 ** Check to ensure that the cursor is valid.  Restore the cursor
2799 ** if need be.  Return any I/O error from the restore operation.
2800 */
2801 int sqlite3VdbeCursorRestore(VdbeCursor *p){
2802   if( sqlite3BtreeCursorHasMoved(p->pCursor) ){
2803     return handleMovedCursor(p);
2804   }
2805   return SQLITE_OK;
2806 }
2807 
2808 /*
2809 ** Make sure the cursor p is ready to read or write the row to which it
2810 ** was last positioned.  Return an error code if an OOM fault or I/O error
2811 ** prevents us from positioning the cursor to its correct position.
2812 **
2813 ** If a MoveTo operation is pending on the given cursor, then do that
2814 ** MoveTo now.  If no move is pending, check to see if the row has been
2815 ** deleted out from under the cursor and if it has, mark the row as
2816 ** a NULL row.
2817 **
2818 ** If the cursor is already pointing to the correct row and that row has
2819 ** not been deleted out from under the cursor, then this routine is a no-op.
2820 */
2821 int sqlite3VdbeCursorMoveto(VdbeCursor *p){
2822   if( p->deferredMoveto ){
2823     return handleDeferredMoveto(p);
2824   }
2825   if( p->pCursor && sqlite3BtreeCursorHasMoved(p->pCursor) ){
2826     return handleMovedCursor(p);
2827   }
2828   return SQLITE_OK;
2829 }
2830 
2831 /*
2832 ** The following functions:
2833 **
2834 ** sqlite3VdbeSerialType()
2835 ** sqlite3VdbeSerialTypeLen()
2836 ** sqlite3VdbeSerialLen()
2837 ** sqlite3VdbeSerialPut()
2838 ** sqlite3VdbeSerialGet()
2839 **
2840 ** encapsulate the code that serializes values for storage in SQLite
2841 ** data and index records. Each serialized value consists of a
2842 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
2843 ** integer, stored as a varint.
2844 **
2845 ** In an SQLite index record, the serial type is stored directly before
2846 ** the blob of data that it corresponds to. In a table record, all serial
2847 ** types are stored at the start of the record, and the blobs of data at
2848 ** the end. Hence these functions allow the caller to handle the
2849 ** serial-type and data blob separately.
2850 **
2851 ** The following table describes the various storage classes for data:
2852 **
2853 **   serial type        bytes of data      type
2854 **   --------------     ---------------    ---------------
2855 **      0                     0            NULL
2856 **      1                     1            signed integer
2857 **      2                     2            signed integer
2858 **      3                     3            signed integer
2859 **      4                     4            signed integer
2860 **      5                     6            signed integer
2861 **      6                     8            signed integer
2862 **      7                     8            IEEE float
2863 **      8                     0            Integer constant 0
2864 **      9                     0            Integer constant 1
2865 **     10,11                               reserved for expansion
2866 **    N>=12 and even       (N-12)/2        BLOB
2867 **    N>=13 and odd        (N-13)/2        text
2868 **
2869 ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
2870 ** of SQLite will not understand those serial types.
2871 */
2872 
2873 /*
2874 ** Return the serial-type for the value stored in pMem.
2875 */
2876 u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
2877   int flags = pMem->flags;
2878   u32 n;
2879 
2880   if( flags&MEM_Null ){
2881     return 0;
2882   }
2883   if( flags&MEM_Int ){
2884     /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
2885 #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
2886     i64 i = pMem->u.i;
2887     u64 u;
2888     if( i<0 ){
2889       u = ~i;
2890     }else{
2891       u = i;
2892     }
2893     if( u<=127 ){
2894       return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
2895     }
2896     if( u<=32767 ) return 2;
2897     if( u<=8388607 ) return 3;
2898     if( u<=2147483647 ) return 4;
2899     if( u<=MAX_6BYTE ) return 5;
2900     return 6;
2901   }
2902   if( flags&MEM_Real ){
2903     return 7;
2904   }
2905   assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
2906   assert( pMem->n>=0 );
2907   n = (u32)pMem->n;
2908   if( flags & MEM_Zero ){
2909     n += pMem->u.nZero;
2910   }
2911   return ((n*2) + 12 + ((flags&MEM_Str)!=0));
2912 }
2913 
2914 /*
2915 ** Return the length of the data corresponding to the supplied serial-type.
2916 */
2917 u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
2918   if( serial_type>=12 ){
2919     return (serial_type-12)/2;
2920   }else{
2921     static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
2922     return aSize[serial_type];
2923   }
2924 }
2925 
2926 /*
2927 ** If we are on an architecture with mixed-endian floating
2928 ** points (ex: ARM7) then swap the lower 4 bytes with the
2929 ** upper 4 bytes.  Return the result.
2930 **
2931 ** For most architectures, this is a no-op.
2932 **
2933 ** (later):  It is reported to me that the mixed-endian problem
2934 ** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
2935 ** that early versions of GCC stored the two words of a 64-bit
2936 ** float in the wrong order.  And that error has been propagated
2937 ** ever since.  The blame is not necessarily with GCC, though.
2938 ** GCC might have just copying the problem from a prior compiler.
2939 ** I am also told that newer versions of GCC that follow a different
2940 ** ABI get the byte order right.
2941 **
2942 ** Developers using SQLite on an ARM7 should compile and run their
2943 ** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
2944 ** enabled, some asserts below will ensure that the byte order of
2945 ** floating point values is correct.
2946 **
2947 ** (2007-08-30)  Frank van Vugt has studied this problem closely
2948 ** and has send his findings to the SQLite developers.  Frank
2949 ** writes that some Linux kernels offer floating point hardware
2950 ** emulation that uses only 32-bit mantissas instead of a full
2951 ** 48-bits as required by the IEEE standard.  (This is the
2952 ** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
2953 ** byte swapping becomes very complicated.  To avoid problems,
2954 ** the necessary byte swapping is carried out using a 64-bit integer
2955 ** rather than a 64-bit float.  Frank assures us that the code here
2956 ** works for him.  We, the developers, have no way to independently
2957 ** verify this, but Frank seems to know what he is talking about
2958 ** so we trust him.
2959 */
2960 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
2961 static u64 floatSwap(u64 in){
2962   union {
2963     u64 r;
2964     u32 i[2];
2965   } u;
2966   u32 t;
2967 
2968   u.r = in;
2969   t = u.i[0];
2970   u.i[0] = u.i[1];
2971   u.i[1] = t;
2972   return u.r;
2973 }
2974 # define swapMixedEndianFloat(X)  X = floatSwap(X)
2975 #else
2976 # define swapMixedEndianFloat(X)
2977 #endif
2978 
2979 /*
2980 ** Write the serialized data blob for the value stored in pMem into
2981 ** buf. It is assumed that the caller has allocated sufficient space.
2982 ** Return the number of bytes written.
2983 **
2984 ** nBuf is the amount of space left in buf[].  The caller is responsible
2985 ** for allocating enough space to buf[] to hold the entire field, exclusive
2986 ** of the pMem->u.nZero bytes for a MEM_Zero value.
2987 **
2988 ** Return the number of bytes actually written into buf[].  The number
2989 ** of bytes in the zero-filled tail is included in the return value only
2990 ** if those bytes were zeroed in buf[].
2991 */
2992 u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
2993   u32 len;
2994 
2995   /* Integer and Real */
2996   if( serial_type<=7 && serial_type>0 ){
2997     u64 v;
2998     u32 i;
2999     if( serial_type==7 ){
3000       assert( sizeof(v)==sizeof(pMem->u.r) );
3001       memcpy(&v, &pMem->u.r, sizeof(v));
3002       swapMixedEndianFloat(v);
3003     }else{
3004       v = pMem->u.i;
3005     }
3006     len = i = sqlite3VdbeSerialTypeLen(serial_type);
3007     assert( i>0 );
3008     do{
3009       buf[--i] = (u8)(v&0xFF);
3010       v >>= 8;
3011     }while( i );
3012     return len;
3013   }
3014 
3015   /* String or blob */
3016   if( serial_type>=12 ){
3017     assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
3018              == (int)sqlite3VdbeSerialTypeLen(serial_type) );
3019     len = pMem->n;
3020     memcpy(buf, pMem->z, len);
3021     return len;
3022   }
3023 
3024   /* NULL or constants 0 or 1 */
3025   return 0;
3026 }
3027 
3028 /* Input "x" is a sequence of unsigned characters that represent a
3029 ** big-endian integer.  Return the equivalent native integer
3030 */
3031 #define ONE_BYTE_INT(x)    ((i8)(x)[0])
3032 #define TWO_BYTE_INT(x)    (256*(i8)((x)[0])|(x)[1])
3033 #define THREE_BYTE_INT(x)  (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
3034 #define FOUR_BYTE_UINT(x)  (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
3035 #define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
3036 
3037 /*
3038 ** Deserialize the data blob pointed to by buf as serial type serial_type
3039 ** and store the result in pMem.  Return the number of bytes read.
3040 **
3041 ** This function is implemented as two separate routines for performance.
3042 ** The few cases that require local variables are broken out into a separate
3043 ** routine so that in most cases the overhead of moving the stack pointer
3044 ** is avoided.
3045 */
3046 static u32 SQLITE_NOINLINE serialGet(
3047   const unsigned char *buf,     /* Buffer to deserialize from */
3048   u32 serial_type,              /* Serial type to deserialize */
3049   Mem *pMem                     /* Memory cell to write value into */
3050 ){
3051   u64 x = FOUR_BYTE_UINT(buf);
3052   u32 y = FOUR_BYTE_UINT(buf+4);
3053   x = (x<<32) + y;
3054   if( serial_type==6 ){
3055     /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
3056     ** twos-complement integer. */
3057     pMem->u.i = *(i64*)&x;
3058     pMem->flags = MEM_Int;
3059     testcase( pMem->u.i<0 );
3060   }else{
3061     /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
3062     ** floating point number. */
3063 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
3064     /* Verify that integers and floating point values use the same
3065     ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
3066     ** defined that 64-bit floating point values really are mixed
3067     ** endian.
3068     */
3069     static const u64 t1 = ((u64)0x3ff00000)<<32;
3070     static const double r1 = 1.0;
3071     u64 t2 = t1;
3072     swapMixedEndianFloat(t2);
3073     assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
3074 #endif
3075     assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
3076     swapMixedEndianFloat(x);
3077     memcpy(&pMem->u.r, &x, sizeof(x));
3078     pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real;
3079   }
3080   return 8;
3081 }
3082 u32 sqlite3VdbeSerialGet(
3083   const unsigned char *buf,     /* Buffer to deserialize from */
3084   u32 serial_type,              /* Serial type to deserialize */
3085   Mem *pMem                     /* Memory cell to write value into */
3086 ){
3087   switch( serial_type ){
3088     case 10:   /* Reserved for future use */
3089     case 11:   /* Reserved for future use */
3090     case 0: {  /* Null */
3091       /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
3092       pMem->flags = MEM_Null;
3093       break;
3094     }
3095     case 1: {
3096       /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
3097       ** integer. */
3098       pMem->u.i = ONE_BYTE_INT(buf);
3099       pMem->flags = MEM_Int;
3100       testcase( pMem->u.i<0 );
3101       return 1;
3102     }
3103     case 2: { /* 2-byte signed integer */
3104       /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
3105       ** twos-complement integer. */
3106       pMem->u.i = TWO_BYTE_INT(buf);
3107       pMem->flags = MEM_Int;
3108       testcase( pMem->u.i<0 );
3109       return 2;
3110     }
3111     case 3: { /* 3-byte signed integer */
3112       /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
3113       ** twos-complement integer. */
3114       pMem->u.i = THREE_BYTE_INT(buf);
3115       pMem->flags = MEM_Int;
3116       testcase( pMem->u.i<0 );
3117       return 3;
3118     }
3119     case 4: { /* 4-byte signed integer */
3120       /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
3121       ** twos-complement integer. */
3122       pMem->u.i = FOUR_BYTE_INT(buf);
3123       pMem->flags = MEM_Int;
3124       testcase( pMem->u.i<0 );
3125       return 4;
3126     }
3127     case 5: { /* 6-byte signed integer */
3128       /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
3129       ** twos-complement integer. */
3130       pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
3131       pMem->flags = MEM_Int;
3132       testcase( pMem->u.i<0 );
3133       return 6;
3134     }
3135     case 6:   /* 8-byte signed integer */
3136     case 7: { /* IEEE floating point */
3137       /* These use local variables, so do them in a separate routine
3138       ** to avoid having to move the frame pointer in the common case */
3139       return serialGet(buf,serial_type,pMem);
3140     }
3141     case 8:    /* Integer 0 */
3142     case 9: {  /* Integer 1 */
3143       /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
3144       /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
3145       pMem->u.i = serial_type-8;
3146       pMem->flags = MEM_Int;
3147       return 0;
3148     }
3149     default: {
3150       /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
3151       ** length.
3152       ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
3153       ** (N-13)/2 bytes in length. */
3154       static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
3155       pMem->z = (char *)buf;
3156       pMem->n = (serial_type-12)/2;
3157       pMem->flags = aFlag[serial_type&1];
3158       return pMem->n;
3159     }
3160   }
3161   return 0;
3162 }
3163 /*
3164 ** This routine is used to allocate sufficient space for an UnpackedRecord
3165 ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
3166 ** the first argument is a pointer to KeyInfo structure pKeyInfo.
3167 **
3168 ** The space is either allocated using sqlite3DbMallocRaw() or from within
3169 ** the unaligned buffer passed via the second and third arguments (presumably
3170 ** stack space). If the former, then *ppFree is set to a pointer that should
3171 ** be eventually freed by the caller using sqlite3DbFree(). Or, if the
3172 ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
3173 ** before returning.
3174 **
3175 ** If an OOM error occurs, NULL is returned.
3176 */
3177 UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
3178   KeyInfo *pKeyInfo,              /* Description of the record */
3179   char *pSpace,                   /* Unaligned space available */
3180   int szSpace,                    /* Size of pSpace[] in bytes */
3181   char **ppFree                   /* OUT: Caller should free this pointer */
3182 ){
3183   UnpackedRecord *p;              /* Unpacked record to return */
3184   int nOff;                       /* Increment pSpace by nOff to align it */
3185   int nByte;                      /* Number of bytes required for *p */
3186 
3187   /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
3188   ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
3189   ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
3190   */
3191   nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
3192   nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
3193   if( nByte>szSpace+nOff ){
3194     p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
3195     *ppFree = (char *)p;
3196     if( !p ) return 0;
3197   }else{
3198     p = (UnpackedRecord*)&pSpace[nOff];
3199     *ppFree = 0;
3200   }
3201 
3202   p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
3203   assert( pKeyInfo->aSortOrder!=0 );
3204   p->pKeyInfo = pKeyInfo;
3205   p->nField = pKeyInfo->nField + 1;
3206   return p;
3207 }
3208 
3209 /*
3210 ** Given the nKey-byte encoding of a record in pKey[], populate the
3211 ** UnpackedRecord structure indicated by the fourth argument with the
3212 ** contents of the decoded record.
3213 */
3214 void sqlite3VdbeRecordUnpack(
3215   KeyInfo *pKeyInfo,     /* Information about the record format */
3216   int nKey,              /* Size of the binary record */
3217   const void *pKey,      /* The binary record */
3218   UnpackedRecord *p      /* Populate this structure before returning. */
3219 ){
3220   const unsigned char *aKey = (const unsigned char *)pKey;
3221   int d;
3222   u32 idx;                        /* Offset in aKey[] to read from */
3223   u16 u;                          /* Unsigned loop counter */
3224   u32 szHdr;
3225   Mem *pMem = p->aMem;
3226 
3227   p->default_rc = 0;
3228   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
3229   idx = getVarint32(aKey, szHdr);
3230   d = szHdr;
3231   u = 0;
3232   while( idx<szHdr && d<=nKey ){
3233     u32 serial_type;
3234 
3235     idx += getVarint32(&aKey[idx], serial_type);
3236     pMem->enc = pKeyInfo->enc;
3237     pMem->db = pKeyInfo->db;
3238     /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
3239     pMem->szMalloc = 0;
3240     d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
3241     pMem++;
3242     if( (++u)>=p->nField ) break;
3243   }
3244   assert( u<=pKeyInfo->nField + 1 );
3245   p->nField = u;
3246 }
3247 
3248 #if SQLITE_DEBUG
3249 /*
3250 ** This function compares two index or table record keys in the same way
3251 ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
3252 ** this function deserializes and compares values using the
3253 ** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
3254 ** in assert() statements to ensure that the optimized code in
3255 ** sqlite3VdbeRecordCompare() returns results with these two primitives.
3256 **
3257 ** Return true if the result of comparison is equivalent to desiredResult.
3258 ** Return false if there is a disagreement.
3259 */
3260 static int vdbeRecordCompareDebug(
3261   int nKey1, const void *pKey1, /* Left key */
3262   const UnpackedRecord *pPKey2, /* Right key */
3263   int desiredResult             /* Correct answer */
3264 ){
3265   u32 d1;            /* Offset into aKey[] of next data element */
3266   u32 idx1;          /* Offset into aKey[] of next header element */
3267   u32 szHdr1;        /* Number of bytes in header */
3268   int i = 0;
3269   int rc = 0;
3270   const unsigned char *aKey1 = (const unsigned char *)pKey1;
3271   KeyInfo *pKeyInfo;
3272   Mem mem1;
3273 
3274   pKeyInfo = pPKey2->pKeyInfo;
3275   if( pKeyInfo->db==0 ) return 1;
3276   mem1.enc = pKeyInfo->enc;
3277   mem1.db = pKeyInfo->db;
3278   /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
3279   VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
3280 
3281   /* Compilers may complain that mem1.u.i is potentially uninitialized.
3282   ** We could initialize it, as shown here, to silence those complaints.
3283   ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
3284   ** the unnecessary initialization has a measurable negative performance
3285   ** impact, since this routine is a very high runner.  And so, we choose
3286   ** to ignore the compiler warnings and leave this variable uninitialized.
3287   */
3288   /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
3289 
3290   idx1 = getVarint32(aKey1, szHdr1);
3291   d1 = szHdr1;
3292   assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
3293   assert( pKeyInfo->aSortOrder!=0 );
3294   assert( pKeyInfo->nField>0 );
3295   assert( idx1<=szHdr1 || CORRUPT_DB );
3296   do{
3297     u32 serial_type1;
3298 
3299     /* Read the serial types for the next element in each key. */
3300     idx1 += getVarint32( aKey1+idx1, serial_type1 );
3301 
3302     /* Verify that there is enough key space remaining to avoid
3303     ** a buffer overread.  The "d1+serial_type1+2" subexpression will
3304     ** always be greater than or equal to the amount of required key space.
3305     ** Use that approximation to avoid the more expensive call to
3306     ** sqlite3VdbeSerialTypeLen() in the common case.
3307     */
3308     if( d1+serial_type1+2>(u32)nKey1
3309      && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
3310     ){
3311       break;
3312     }
3313 
3314     /* Extract the values to be compared.
3315     */
3316     d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
3317 
3318     /* Do the comparison
3319     */
3320     rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
3321     if( rc!=0 ){
3322       assert( mem1.szMalloc==0 );  /* See comment below */
3323       if( pKeyInfo->aSortOrder[i] ){
3324         rc = -rc;  /* Invert the result for DESC sort order. */
3325       }
3326       goto debugCompareEnd;
3327     }
3328     i++;
3329   }while( idx1<szHdr1 && i<pPKey2->nField );
3330 
3331   /* No memory allocation is ever used on mem1.  Prove this using
3332   ** the following assert().  If the assert() fails, it indicates a
3333   ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
3334   */
3335   assert( mem1.szMalloc==0 );
3336 
3337   /* rc==0 here means that one of the keys ran out of fields and
3338   ** all the fields up to that point were equal. Return the default_rc
3339   ** value.  */
3340   rc = pPKey2->default_rc;
3341 
3342 debugCompareEnd:
3343   if( desiredResult==0 && rc==0 ) return 1;
3344   if( desiredResult<0 && rc<0 ) return 1;
3345   if( desiredResult>0 && rc>0 ) return 1;
3346   if( CORRUPT_DB ) return 1;
3347   if( pKeyInfo->db->mallocFailed ) return 1;
3348   return 0;
3349 }
3350 #endif
3351 
3352 /*
3353 ** Both *pMem1 and *pMem2 contain string values. Compare the two values
3354 ** using the collation sequence pColl. As usual, return a negative , zero
3355 ** or positive value if *pMem1 is less than, equal to or greater than
3356 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
3357 */
3358 static int vdbeCompareMemString(
3359   const Mem *pMem1,
3360   const Mem *pMem2,
3361   const CollSeq *pColl,
3362   u8 *prcErr                      /* If an OOM occurs, set to SQLITE_NOMEM */
3363 ){
3364   if( pMem1->enc==pColl->enc ){
3365     /* The strings are already in the correct encoding.  Call the
3366      ** comparison function directly */
3367     return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
3368   }else{
3369     int rc;
3370     const void *v1, *v2;
3371     int n1, n2;
3372     Mem c1;
3373     Mem c2;
3374     sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
3375     sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
3376     sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
3377     sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
3378     v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
3379     n1 = v1==0 ? 0 : c1.n;
3380     v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
3381     n2 = v2==0 ? 0 : c2.n;
3382     rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
3383     sqlite3VdbeMemRelease(&c1);
3384     sqlite3VdbeMemRelease(&c2);
3385     if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM;
3386     return rc;
3387   }
3388 }
3389 
3390 /*
3391 ** Compare two blobs.  Return negative, zero, or positive if the first
3392 ** is less than, equal to, or greater than the second, respectively.
3393 ** If one blob is a prefix of the other, then the shorter is the lessor.
3394 */
3395 static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
3396   int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n);
3397   if( c ) return c;
3398   return pB1->n - pB2->n;
3399 }
3400 
3401 
3402 /*
3403 ** Compare the values contained by the two memory cells, returning
3404 ** negative, zero or positive if pMem1 is less than, equal to, or greater
3405 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
3406 ** and reals) sorted numerically, followed by text ordered by the collating
3407 ** sequence pColl and finally blob's ordered by memcmp().
3408 **
3409 ** Two NULL values are considered equal by this function.
3410 */
3411 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
3412   int f1, f2;
3413   int combined_flags;
3414 
3415   f1 = pMem1->flags;
3416   f2 = pMem2->flags;
3417   combined_flags = f1|f2;
3418   assert( (combined_flags & MEM_RowSet)==0 );
3419 
3420   /* If one value is NULL, it is less than the other. If both values
3421   ** are NULL, return 0.
3422   */
3423   if( combined_flags&MEM_Null ){
3424     return (f2&MEM_Null) - (f1&MEM_Null);
3425   }
3426 
3427   /* If one value is a number and the other is not, the number is less.
3428   ** If both are numbers, compare as reals if one is a real, or as integers
3429   ** if both values are integers.
3430   */
3431   if( combined_flags&(MEM_Int|MEM_Real) ){
3432     double r1, r2;
3433     if( (f1 & f2 & MEM_Int)!=0 ){
3434       if( pMem1->u.i < pMem2->u.i ) return -1;
3435       if( pMem1->u.i > pMem2->u.i ) return 1;
3436       return 0;
3437     }
3438     if( (f1&MEM_Real)!=0 ){
3439       r1 = pMem1->u.r;
3440     }else if( (f1&MEM_Int)!=0 ){
3441       r1 = (double)pMem1->u.i;
3442     }else{
3443       return 1;
3444     }
3445     if( (f2&MEM_Real)!=0 ){
3446       r2 = pMem2->u.r;
3447     }else if( (f2&MEM_Int)!=0 ){
3448       r2 = (double)pMem2->u.i;
3449     }else{
3450       return -1;
3451     }
3452     if( r1<r2 ) return -1;
3453     if( r1>r2 ) return 1;
3454     return 0;
3455   }
3456 
3457   /* If one value is a string and the other is a blob, the string is less.
3458   ** If both are strings, compare using the collating functions.
3459   */
3460   if( combined_flags&MEM_Str ){
3461     if( (f1 & MEM_Str)==0 ){
3462       return 1;
3463     }
3464     if( (f2 & MEM_Str)==0 ){
3465       return -1;
3466     }
3467 
3468     assert( pMem1->enc==pMem2->enc );
3469     assert( pMem1->enc==SQLITE_UTF8 ||
3470             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
3471 
3472     /* The collation sequence must be defined at this point, even if
3473     ** the user deletes the collation sequence after the vdbe program is
3474     ** compiled (this was not always the case).
3475     */
3476     assert( !pColl || pColl->xCmp );
3477 
3478     if( pColl ){
3479       return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
3480     }
3481     /* If a NULL pointer was passed as the collate function, fall through
3482     ** to the blob case and use memcmp().  */
3483   }
3484 
3485   /* Both values must be blobs.  Compare using memcmp().  */
3486   return sqlite3BlobCompare(pMem1, pMem2);
3487 }
3488 
3489 
3490 /*
3491 ** The first argument passed to this function is a serial-type that
3492 ** corresponds to an integer - all values between 1 and 9 inclusive
3493 ** except 7. The second points to a buffer containing an integer value
3494 ** serialized according to serial_type. This function deserializes
3495 ** and returns the value.
3496 */
3497 static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
3498   u32 y;
3499   assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
3500   switch( serial_type ){
3501     case 0:
3502     case 1:
3503       testcase( aKey[0]&0x80 );
3504       return ONE_BYTE_INT(aKey);
3505     case 2:
3506       testcase( aKey[0]&0x80 );
3507       return TWO_BYTE_INT(aKey);
3508     case 3:
3509       testcase( aKey[0]&0x80 );
3510       return THREE_BYTE_INT(aKey);
3511     case 4: {
3512       testcase( aKey[0]&0x80 );
3513       y = FOUR_BYTE_UINT(aKey);
3514       return (i64)*(int*)&y;
3515     }
3516     case 5: {
3517       testcase( aKey[0]&0x80 );
3518       return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
3519     }
3520     case 6: {
3521       u64 x = FOUR_BYTE_UINT(aKey);
3522       testcase( aKey[0]&0x80 );
3523       x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
3524       return (i64)*(i64*)&x;
3525     }
3526   }
3527 
3528   return (serial_type - 8);
3529 }
3530 
3531 /*
3532 ** This function compares the two table rows or index records
3533 ** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
3534 ** or positive integer if key1 is less than, equal to or
3535 ** greater than key2.  The {nKey1, pKey1} key must be a blob
3536 ** created by the OP_MakeRecord opcode of the VDBE.  The pPKey2
3537 ** key must be a parsed key such as obtained from
3538 ** sqlite3VdbeParseRecord.
3539 **
3540 ** If argument bSkip is non-zero, it is assumed that the caller has already
3541 ** determined that the first fields of the keys are equal.
3542 **
3543 ** Key1 and Key2 do not have to contain the same number of fields. If all
3544 ** fields that appear in both keys are equal, then pPKey2->default_rc is
3545 ** returned.
3546 **
3547 ** If database corruption is discovered, set pPKey2->errCode to
3548 ** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
3549 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
3550 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
3551 */
3552 static int vdbeRecordCompareWithSkip(
3553   int nKey1, const void *pKey1,   /* Left key */
3554   UnpackedRecord *pPKey2,         /* Right key */
3555   int bSkip                       /* If true, skip the first field */
3556 ){
3557   u32 d1;                         /* Offset into aKey[] of next data element */
3558   int i;                          /* Index of next field to compare */
3559   u32 szHdr1;                     /* Size of record header in bytes */
3560   u32 idx1;                       /* Offset of first type in header */
3561   int rc = 0;                     /* Return value */
3562   Mem *pRhs = pPKey2->aMem;       /* Next field of pPKey2 to compare */
3563   KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
3564   const unsigned char *aKey1 = (const unsigned char *)pKey1;
3565   Mem mem1;
3566 
3567   /* If bSkip is true, then the caller has already determined that the first
3568   ** two elements in the keys are equal. Fix the various stack variables so
3569   ** that this routine begins comparing at the second field. */
3570   if( bSkip ){
3571     u32 s1;
3572     idx1 = 1 + getVarint32(&aKey1[1], s1);
3573     szHdr1 = aKey1[0];
3574     d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
3575     i = 1;
3576     pRhs++;
3577   }else{
3578     idx1 = getVarint32(aKey1, szHdr1);
3579     d1 = szHdr1;
3580     if( d1>(unsigned)nKey1 ){
3581       pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
3582       return 0;  /* Corruption */
3583     }
3584     i = 0;
3585   }
3586 
3587   VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
3588   assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
3589        || CORRUPT_DB );
3590   assert( pPKey2->pKeyInfo->aSortOrder!=0 );
3591   assert( pPKey2->pKeyInfo->nField>0 );
3592   assert( idx1<=szHdr1 || CORRUPT_DB );
3593   do{
3594     u32 serial_type;
3595 
3596     /* RHS is an integer */
3597     if( pRhs->flags & MEM_Int ){
3598       serial_type = aKey1[idx1];
3599       testcase( serial_type==12 );
3600       if( serial_type>=12 ){
3601         rc = +1;
3602       }else if( serial_type==0 ){
3603         rc = -1;
3604       }else if( serial_type==7 ){
3605         double rhs = (double)pRhs->u.i;
3606         sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
3607         if( mem1.u.r<rhs ){
3608           rc = -1;
3609         }else if( mem1.u.r>rhs ){
3610           rc = +1;
3611         }
3612       }else{
3613         i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
3614         i64 rhs = pRhs->u.i;
3615         if( lhs<rhs ){
3616           rc = -1;
3617         }else if( lhs>rhs ){
3618           rc = +1;
3619         }
3620       }
3621     }
3622 
3623     /* RHS is real */
3624     else if( pRhs->flags & MEM_Real ){
3625       serial_type = aKey1[idx1];
3626       if( serial_type>=12 ){
3627         rc = +1;
3628       }else if( serial_type==0 ){
3629         rc = -1;
3630       }else{
3631         double rhs = pRhs->u.r;
3632         double lhs;
3633         sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
3634         if( serial_type==7 ){
3635           lhs = mem1.u.r;
3636         }else{
3637           lhs = (double)mem1.u.i;
3638         }
3639         if( lhs<rhs ){
3640           rc = -1;
3641         }else if( lhs>rhs ){
3642           rc = +1;
3643         }
3644       }
3645     }
3646 
3647     /* RHS is a string */
3648     else if( pRhs->flags & MEM_Str ){
3649       getVarint32(&aKey1[idx1], serial_type);
3650       testcase( serial_type==12 );
3651       if( serial_type<12 ){
3652         rc = -1;
3653       }else if( !(serial_type & 0x01) ){
3654         rc = +1;
3655       }else{
3656         mem1.n = (serial_type - 12) / 2;
3657         testcase( (d1+mem1.n)==(unsigned)nKey1 );
3658         testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
3659         if( (d1+mem1.n) > (unsigned)nKey1 ){
3660           pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
3661           return 0;                /* Corruption */
3662         }else if( pKeyInfo->aColl[i] ){
3663           mem1.enc = pKeyInfo->enc;
3664           mem1.db = pKeyInfo->db;
3665           mem1.flags = MEM_Str;
3666           mem1.z = (char*)&aKey1[d1];
3667           rc = vdbeCompareMemString(
3668               &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
3669           );
3670         }else{
3671           int nCmp = MIN(mem1.n, pRhs->n);
3672           rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
3673           if( rc==0 ) rc = mem1.n - pRhs->n;
3674         }
3675       }
3676     }
3677 
3678     /* RHS is a blob */
3679     else if( pRhs->flags & MEM_Blob ){
3680       getVarint32(&aKey1[idx1], serial_type);
3681       testcase( serial_type==12 );
3682       if( serial_type<12 || (serial_type & 0x01) ){
3683         rc = -1;
3684       }else{
3685         int nStr = (serial_type - 12) / 2;
3686         testcase( (d1+nStr)==(unsigned)nKey1 );
3687         testcase( (d1+nStr+1)==(unsigned)nKey1 );
3688         if( (d1+nStr) > (unsigned)nKey1 ){
3689           pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
3690           return 0;                /* Corruption */
3691         }else{
3692           int nCmp = MIN(nStr, pRhs->n);
3693           rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
3694           if( rc==0 ) rc = nStr - pRhs->n;
3695         }
3696       }
3697     }
3698 
3699     /* RHS is null */
3700     else{
3701       serial_type = aKey1[idx1];
3702       rc = (serial_type!=0);
3703     }
3704 
3705     if( rc!=0 ){
3706       if( pKeyInfo->aSortOrder[i] ){
3707         rc = -rc;
3708       }
3709       assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
3710       assert( mem1.szMalloc==0 );  /* See comment below */
3711       return rc;
3712     }
3713 
3714     i++;
3715     pRhs++;
3716     d1 += sqlite3VdbeSerialTypeLen(serial_type);
3717     idx1 += sqlite3VarintLen(serial_type);
3718   }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
3719 
3720   /* No memory allocation is ever used on mem1.  Prove this using
3721   ** the following assert().  If the assert() fails, it indicates a
3722   ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).  */
3723   assert( mem1.szMalloc==0 );
3724 
3725   /* rc==0 here means that one or both of the keys ran out of fields and
3726   ** all the fields up to that point were equal. Return the default_rc
3727   ** value.  */
3728   assert( CORRUPT_DB
3729        || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
3730        || pKeyInfo->db->mallocFailed
3731   );
3732   return pPKey2->default_rc;
3733 }
3734 int sqlite3VdbeRecordCompare(
3735   int nKey1, const void *pKey1,   /* Left key */
3736   UnpackedRecord *pPKey2          /* Right key */
3737 ){
3738   return vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
3739 }
3740 
3741 
3742 /*
3743 ** This function is an optimized version of sqlite3VdbeRecordCompare()
3744 ** that (a) the first field of pPKey2 is an integer, and (b) the
3745 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
3746 ** byte (i.e. is less than 128).
3747 **
3748 ** To avoid concerns about buffer overreads, this routine is only used
3749 ** on schemas where the maximum valid header size is 63 bytes or less.
3750 */
3751 static int vdbeRecordCompareInt(
3752   int nKey1, const void *pKey1, /* Left key */
3753   UnpackedRecord *pPKey2        /* Right key */
3754 ){
3755   const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
3756   int serial_type = ((const u8*)pKey1)[1];
3757   int res;
3758   u32 y;
3759   u64 x;
3760   i64 v = pPKey2->aMem[0].u.i;
3761   i64 lhs;
3762 
3763   assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
3764   switch( serial_type ){
3765     case 1: { /* 1-byte signed integer */
3766       lhs = ONE_BYTE_INT(aKey);
3767       testcase( lhs<0 );
3768       break;
3769     }
3770     case 2: { /* 2-byte signed integer */
3771       lhs = TWO_BYTE_INT(aKey);
3772       testcase( lhs<0 );
3773       break;
3774     }
3775     case 3: { /* 3-byte signed integer */
3776       lhs = THREE_BYTE_INT(aKey);
3777       testcase( lhs<0 );
3778       break;
3779     }
3780     case 4: { /* 4-byte signed integer */
3781       y = FOUR_BYTE_UINT(aKey);
3782       lhs = (i64)*(int*)&y;
3783       testcase( lhs<0 );
3784       break;
3785     }
3786     case 5: { /* 6-byte signed integer */
3787       lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
3788       testcase( lhs<0 );
3789       break;
3790     }
3791     case 6: { /* 8-byte signed integer */
3792       x = FOUR_BYTE_UINT(aKey);
3793       x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
3794       lhs = *(i64*)&x;
3795       testcase( lhs<0 );
3796       break;
3797     }
3798     case 8:
3799       lhs = 0;
3800       break;
3801     case 9:
3802       lhs = 1;
3803       break;
3804 
3805     /* This case could be removed without changing the results of running
3806     ** this code. Including it causes gcc to generate a faster switch
3807     ** statement (since the range of switch targets now starts at zero and
3808     ** is contiguous) but does not cause any duplicate code to be generated
3809     ** (as gcc is clever enough to combine the two like cases). Other
3810     ** compilers might be similar.  */
3811     case 0: case 7:
3812       return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
3813 
3814     default:
3815       return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
3816   }
3817 
3818   if( v>lhs ){
3819     res = pPKey2->r1;
3820   }else if( v<lhs ){
3821     res = pPKey2->r2;
3822   }else if( pPKey2->nField>1 ){
3823     /* The first fields of the two keys are equal. Compare the trailing
3824     ** fields.  */
3825     res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
3826   }else{
3827     /* The first fields of the two keys are equal and there are no trailing
3828     ** fields. Return pPKey2->default_rc in this case. */
3829     res = pPKey2->default_rc;
3830   }
3831 
3832   assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
3833   return res;
3834 }
3835 
3836 /*
3837 ** This function is an optimized version of sqlite3VdbeRecordCompare()
3838 ** that (a) the first field of pPKey2 is a string, that (b) the first field
3839 ** uses the collation sequence BINARY and (c) that the size-of-header varint
3840 ** at the start of (pKey1/nKey1) fits in a single byte.
3841 */
3842 static int vdbeRecordCompareString(
3843   int nKey1, const void *pKey1, /* Left key */
3844   UnpackedRecord *pPKey2        /* Right key */
3845 ){
3846   const u8 *aKey1 = (const u8*)pKey1;
3847   int serial_type;
3848   int res;
3849 
3850   getVarint32(&aKey1[1], serial_type);
3851   if( serial_type<12 ){
3852     res = pPKey2->r1;      /* (pKey1/nKey1) is a number or a null */
3853   }else if( !(serial_type & 0x01) ){
3854     res = pPKey2->r2;      /* (pKey1/nKey1) is a blob */
3855   }else{
3856     int nCmp;
3857     int nStr;
3858     int szHdr = aKey1[0];
3859 
3860     nStr = (serial_type-12) / 2;
3861     if( (szHdr + nStr) > nKey1 ){
3862       pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
3863       return 0;    /* Corruption */
3864     }
3865     nCmp = MIN( pPKey2->aMem[0].n, nStr );
3866     res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
3867 
3868     if( res==0 ){
3869       res = nStr - pPKey2->aMem[0].n;
3870       if( res==0 ){
3871         if( pPKey2->nField>1 ){
3872           res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
3873         }else{
3874           res = pPKey2->default_rc;
3875         }
3876       }else if( res>0 ){
3877         res = pPKey2->r2;
3878       }else{
3879         res = pPKey2->r1;
3880       }
3881     }else if( res>0 ){
3882       res = pPKey2->r2;
3883     }else{
3884       res = pPKey2->r1;
3885     }
3886   }
3887 
3888   assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)
3889        || CORRUPT_DB
3890        || pPKey2->pKeyInfo->db->mallocFailed
3891   );
3892   return res;
3893 }
3894 
3895 /*
3896 ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
3897 ** suitable for comparing serialized records to the unpacked record passed
3898 ** as the only argument.
3899 */
3900 RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
3901   /* varintRecordCompareInt() and varintRecordCompareString() both assume
3902   ** that the size-of-header varint that occurs at the start of each record
3903   ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
3904   ** also assumes that it is safe to overread a buffer by at least the
3905   ** maximum possible legal header size plus 8 bytes. Because there is
3906   ** guaranteed to be at least 74 (but not 136) bytes of padding following each
3907   ** buffer passed to varintRecordCompareInt() this makes it convenient to
3908   ** limit the size of the header to 64 bytes in cases where the first field
3909   ** is an integer.
3910   **
3911   ** The easiest way to enforce this limit is to consider only records with
3912   ** 13 fields or less. If the first field is an integer, the maximum legal
3913   ** header size is (12*5 + 1 + 1) bytes.  */
3914   if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
3915     int flags = p->aMem[0].flags;
3916     if( p->pKeyInfo->aSortOrder[0] ){
3917       p->r1 = 1;
3918       p->r2 = -1;
3919     }else{
3920       p->r1 = -1;
3921       p->r2 = 1;
3922     }
3923     if( (flags & MEM_Int) ){
3924       return vdbeRecordCompareInt;
3925     }
3926     testcase( flags & MEM_Real );
3927     testcase( flags & MEM_Null );
3928     testcase( flags & MEM_Blob );
3929     if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
3930       assert( flags & MEM_Str );
3931       return vdbeRecordCompareString;
3932     }
3933   }
3934 
3935   return sqlite3VdbeRecordCompare;
3936 }
3937 
3938 /*
3939 ** pCur points at an index entry created using the OP_MakeRecord opcode.
3940 ** Read the rowid (the last field in the record) and store it in *rowid.
3941 ** Return SQLITE_OK if everything works, or an error code otherwise.
3942 **
3943 ** pCur might be pointing to text obtained from a corrupt database file.
3944 ** So the content cannot be trusted.  Do appropriate checks on the content.
3945 */
3946 int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
3947   i64 nCellKey = 0;
3948   int rc;
3949   u32 szHdr;        /* Size of the header */
3950   u32 typeRowid;    /* Serial type of the rowid */
3951   u32 lenRowid;     /* Size of the rowid */
3952   Mem m, v;
3953 
3954   /* Get the size of the index entry.  Only indices entries of less
3955   ** than 2GiB are support - anything large must be database corruption.
3956   ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
3957   ** this code can safely assume that nCellKey is 32-bits
3958   */
3959   assert( sqlite3BtreeCursorIsValid(pCur) );
3960   VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
3961   assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
3962   assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
3963 
3964   /* Read in the complete content of the index entry */
3965   sqlite3VdbeMemInit(&m, db, 0);
3966   rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
3967   if( rc ){
3968     return rc;
3969   }
3970 
3971   /* The index entry must begin with a header size */
3972   (void)getVarint32((u8*)m.z, szHdr);
3973   testcase( szHdr==3 );
3974   testcase( szHdr==m.n );
3975   if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
3976     goto idx_rowid_corruption;
3977   }
3978 
3979   /* The last field of the index should be an integer - the ROWID.
3980   ** Verify that the last entry really is an integer. */
3981   (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
3982   testcase( typeRowid==1 );
3983   testcase( typeRowid==2 );
3984   testcase( typeRowid==3 );
3985   testcase( typeRowid==4 );
3986   testcase( typeRowid==5 );
3987   testcase( typeRowid==6 );
3988   testcase( typeRowid==8 );
3989   testcase( typeRowid==9 );
3990   if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
3991     goto idx_rowid_corruption;
3992   }
3993   lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
3994   testcase( (u32)m.n==szHdr+lenRowid );
3995   if( unlikely((u32)m.n<szHdr+lenRowid) ){
3996     goto idx_rowid_corruption;
3997   }
3998 
3999   /* Fetch the integer off the end of the index record */
4000   sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
4001   *rowid = v.u.i;
4002   sqlite3VdbeMemRelease(&m);
4003   return SQLITE_OK;
4004 
4005   /* Jump here if database corruption is detected after m has been
4006   ** allocated.  Free the m object and return SQLITE_CORRUPT. */
4007 idx_rowid_corruption:
4008   testcase( m.szMalloc!=0 );
4009   sqlite3VdbeMemRelease(&m);
4010   return SQLITE_CORRUPT_BKPT;
4011 }
4012 
4013 /*
4014 ** Compare the key of the index entry that cursor pC is pointing to against
4015 ** the key string in pUnpacked.  Write into *pRes a number
4016 ** that is negative, zero, or positive if pC is less than, equal to,
4017 ** or greater than pUnpacked.  Return SQLITE_OK on success.
4018 **
4019 ** pUnpacked is either created without a rowid or is truncated so that it
4020 ** omits the rowid at the end.  The rowid at the end of the index entry
4021 ** is ignored as well.  Hence, this routine only compares the prefixes
4022 ** of the keys prior to the final rowid, not the entire key.
4023 */
4024 int sqlite3VdbeIdxKeyCompare(
4025   sqlite3 *db,                     /* Database connection */
4026   VdbeCursor *pC,                  /* The cursor to compare against */
4027   UnpackedRecord *pUnpacked,       /* Unpacked version of key */
4028   int *res                         /* Write the comparison result here */
4029 ){
4030   i64 nCellKey = 0;
4031   int rc;
4032   BtCursor *pCur = pC->pCursor;
4033   Mem m;
4034 
4035   assert( sqlite3BtreeCursorIsValid(pCur) );
4036   VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
4037   assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
4038   /* nCellKey will always be between 0 and 0xffffffff because of the way
4039   ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
4040   if( nCellKey<=0 || nCellKey>0x7fffffff ){
4041     *res = 0;
4042     return SQLITE_CORRUPT_BKPT;
4043   }
4044   sqlite3VdbeMemInit(&m, db, 0);
4045   rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
4046   if( rc ){
4047     return rc;
4048   }
4049   *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
4050   sqlite3VdbeMemRelease(&m);
4051   return SQLITE_OK;
4052 }
4053 
4054 /*
4055 ** This routine sets the value to be returned by subsequent calls to
4056 ** sqlite3_changes() on the database handle 'db'.
4057 */
4058 void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
4059   assert( sqlite3_mutex_held(db->mutex) );
4060   db->nChange = nChange;
4061   db->nTotalChange += nChange;
4062 }
4063 
4064 /*
4065 ** Set a flag in the vdbe to update the change counter when it is finalised
4066 ** or reset.
4067 */
4068 void sqlite3VdbeCountChanges(Vdbe *v){
4069   v->changeCntOn = 1;
4070 }
4071 
4072 /*
4073 ** Mark every prepared statement associated with a database connection
4074 ** as expired.
4075 **
4076 ** An expired statement means that recompilation of the statement is
4077 ** recommend.  Statements expire when things happen that make their
4078 ** programs obsolete.  Removing user-defined functions or collating
4079 ** sequences, or changing an authorization function are the types of
4080 ** things that make prepared statements obsolete.
4081 */
4082 void sqlite3ExpirePreparedStatements(sqlite3 *db){
4083   Vdbe *p;
4084   for(p = db->pVdbe; p; p=p->pNext){
4085     p->expired = 1;
4086   }
4087 }
4088 
4089 /*
4090 ** Return the database associated with the Vdbe.
4091 */
4092 sqlite3 *sqlite3VdbeDb(Vdbe *v){
4093   return v->db;
4094 }
4095 
4096 /*
4097 ** Return a pointer to an sqlite3_value structure containing the value bound
4098 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return
4099 ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
4100 ** constants) to the value before returning it.
4101 **
4102 ** The returned value must be freed by the caller using sqlite3ValueFree().
4103 */
4104 sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
4105   assert( iVar>0 );
4106   if( v ){
4107     Mem *pMem = &v->aVar[iVar-1];
4108     if( 0==(pMem->flags & MEM_Null) ){
4109       sqlite3_value *pRet = sqlite3ValueNew(v->db);
4110       if( pRet ){
4111         sqlite3VdbeMemCopy((Mem *)pRet, pMem);
4112         sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
4113       }
4114       return pRet;
4115     }
4116   }
4117   return 0;
4118 }
4119 
4120 /*
4121 ** Configure SQL variable iVar so that binding a new value to it signals
4122 ** to sqlite3_reoptimize() that re-preparing the statement may result
4123 ** in a better query plan.
4124 */
4125 void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
4126   assert( iVar>0 );
4127   if( iVar>32 ){
4128     v->expmask = 0xffffffff;
4129   }else{
4130     v->expmask |= ((u32)1 << (iVar-1));
4131   }
4132 }
4133 
4134 #ifndef SQLITE_OMIT_VIRTUALTABLE
4135 /*
4136 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
4137 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
4138 ** in memory obtained from sqlite3DbMalloc).
4139 */
4140 void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
4141   sqlite3 *db = p->db;
4142   sqlite3DbFree(db, p->zErrMsg);
4143   p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
4144   sqlite3_free(pVtab->zErrMsg);
4145   pVtab->zErrMsg = 0;
4146 }
4147 #endif /* SQLITE_OMIT_VIRTUALTABLE */
4148