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