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