000001  /*
000002  ** 2003 September 6
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  ** This file contains code used for creating, destroying, and populating
000013  ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) 
000014  */
000015  #include "sqliteInt.h"
000016  #include "vdbeInt.h"
000017  
000018  /*
000019  ** Create a new virtual database engine.
000020  */
000021  Vdbe *sqlite3VdbeCreate(Parse *pParse){
000022    sqlite3 *db = pParse->db;
000023    Vdbe *p;
000024    p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
000025    if( p==0 ) return 0;
000026    memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
000027    p->db = db;
000028    if( db->pVdbe ){
000029      db->pVdbe->pPrev = p;
000030    }
000031    p->pNext = db->pVdbe;
000032    p->pPrev = 0;
000033    db->pVdbe = p;
000034    p->magic = VDBE_MAGIC_INIT;
000035    p->pParse = pParse;
000036    assert( pParse->aLabel==0 );
000037    assert( pParse->nLabel==0 );
000038    assert( pParse->nOpAlloc==0 );
000039    assert( pParse->szOpAlloc==0 );
000040    return p;
000041  }
000042  
000043  /*
000044  ** Change the error string stored in Vdbe.zErrMsg
000045  */
000046  void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
000047    va_list ap;
000048    sqlite3DbFree(p->db, p->zErrMsg);
000049    va_start(ap, zFormat);
000050    p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
000051    va_end(ap);
000052  }
000053  
000054  /*
000055  ** Remember the SQL string for a prepared statement.
000056  */
000057  void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
000058    assert( isPrepareV2==1 || isPrepareV2==0 );
000059    if( p==0 ) return;
000060  #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
000061    if( !isPrepareV2 ) return;
000062  #endif
000063    assert( p->zSql==0 );
000064    p->zSql = sqlite3DbStrNDup(p->db, z, n);
000065    p->isPrepareV2 = (u8)isPrepareV2;
000066  }
000067  
000068  /*
000069  ** Swap all content between two VDBE structures.
000070  */
000071  void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
000072    Vdbe tmp, *pTmp;
000073    char *zTmp;
000074    assert( pA->db==pB->db );
000075    tmp = *pA;
000076    *pA = *pB;
000077    *pB = tmp;
000078    pTmp = pA->pNext;
000079    pA->pNext = pB->pNext;
000080    pB->pNext = pTmp;
000081    pTmp = pA->pPrev;
000082    pA->pPrev = pB->pPrev;
000083    pB->pPrev = pTmp;
000084    zTmp = pA->zSql;
000085    pA->zSql = pB->zSql;
000086    pB->zSql = zTmp;
000087    pB->isPrepareV2 = pA->isPrepareV2;
000088  }
000089  
000090  /*
000091  ** Resize the Vdbe.aOp array so that it is at least nOp elements larger 
000092  ** than its current size. nOp is guaranteed to be less than or equal
000093  ** to 1024/sizeof(Op).
000094  **
000095  ** If an out-of-memory error occurs while resizing the array, return
000096  ** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain 
000097  ** unchanged (this is so that any opcodes already allocated can be 
000098  ** correctly deallocated along with the rest of the Vdbe).
000099  */
000100  static int growOpArray(Vdbe *v, int nOp){
000101    VdbeOp *pNew;
000102    Parse *p = v->pParse;
000103  
000104    /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
000105    ** more frequent reallocs and hence provide more opportunities for 
000106    ** simulated OOM faults.  SQLITE_TEST_REALLOC_STRESS is generally used
000107    ** during testing only.  With SQLITE_TEST_REALLOC_STRESS grow the op array
000108    ** by the minimum* amount required until the size reaches 512.  Normal
000109    ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
000110    ** size of the op array or add 1KB of space, whichever is smaller. */
000111  #ifdef SQLITE_TEST_REALLOC_STRESS
000112    int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
000113  #else
000114    int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
000115    UNUSED_PARAMETER(nOp);
000116  #endif
000117  
000118    assert( nOp<=(1024/sizeof(Op)) );
000119    assert( nNew>=(p->nOpAlloc+nOp) );
000120    pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
000121    if( pNew ){
000122      p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
000123      p->nOpAlloc = p->szOpAlloc/sizeof(Op);
000124      v->aOp = pNew;
000125    }
000126    return (pNew ? SQLITE_OK : SQLITE_NOMEM_BKPT);
000127  }
000128  
000129  #ifdef SQLITE_DEBUG
000130  /* This routine is just a convenient place to set a breakpoint that will
000131  ** fire after each opcode is inserted and displayed using
000132  ** "PRAGMA vdbe_addoptrace=on".
000133  */
000134  static void test_addop_breakpoint(void){
000135    static int n = 0;
000136    n++;
000137  }
000138  #endif
000139  
000140  /*
000141  ** Add a new instruction to the list of instructions current in the
000142  ** VDBE.  Return the address of the new instruction.
000143  **
000144  ** Parameters:
000145  **
000146  **    p               Pointer to the VDBE
000147  **
000148  **    op              The opcode for this instruction
000149  **
000150  **    p1, p2, p3      Operands
000151  **
000152  ** Use the sqlite3VdbeResolveLabel() function to fix an address and
000153  ** the sqlite3VdbeChangeP4() function to change the value of the P4
000154  ** operand.
000155  */
000156  static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
000157    assert( p->pParse->nOpAlloc<=p->nOp );
000158    if( growOpArray(p, 1) ) return 1;
000159    assert( p->pParse->nOpAlloc>p->nOp );
000160    return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
000161  }
000162  int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
000163    int i;
000164    VdbeOp *pOp;
000165  
000166    i = p->nOp;
000167    assert( p->magic==VDBE_MAGIC_INIT );
000168    assert( op>=0 && op<0xff );
000169    if( p->pParse->nOpAlloc<=i ){
000170      return growOp3(p, op, p1, p2, p3);
000171    }
000172    p->nOp++;
000173    pOp = &p->aOp[i];
000174    pOp->opcode = (u8)op;
000175    pOp->p5 = 0;
000176    pOp->p1 = p1;
000177    pOp->p2 = p2;
000178    pOp->p3 = p3;
000179    pOp->p4.p = 0;
000180    pOp->p4type = P4_NOTUSED;
000181  #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
000182    pOp->zComment = 0;
000183  #endif
000184  #ifdef SQLITE_DEBUG
000185    if( p->db->flags & SQLITE_VdbeAddopTrace ){
000186      int jj, kk;
000187      Parse *pParse = p->pParse;
000188      for(jj=kk=0; jj<pParse->nColCache; jj++){
000189        struct yColCache *x = pParse->aColCache + jj;
000190        printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
000191        kk++;
000192      }
000193      if( kk ) printf("\n");
000194      sqlite3VdbePrintOp(0, i, &p->aOp[i]);
000195      test_addop_breakpoint();
000196    }
000197  #endif
000198  #ifdef VDBE_PROFILE
000199    pOp->cycles = 0;
000200    pOp->cnt = 0;
000201  #endif
000202  #ifdef SQLITE_VDBE_COVERAGE
000203    pOp->iSrcLine = 0;
000204  #endif
000205    return i;
000206  }
000207  int sqlite3VdbeAddOp0(Vdbe *p, int op){
000208    return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
000209  }
000210  int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
000211    return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
000212  }
000213  int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
000214    return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
000215  }
000216  
000217  /* Generate code for an unconditional jump to instruction iDest
000218  */
000219  int sqlite3VdbeGoto(Vdbe *p, int iDest){
000220    return sqlite3VdbeAddOp3(p, OP_Goto, 0, iDest, 0);
000221  }
000222  
000223  /* Generate code to cause the string zStr to be loaded into
000224  ** register iDest
000225  */
000226  int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
000227    return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zStr, 0);
000228  }
000229  
000230  /*
000231  ** Generate code that initializes multiple registers to string or integer
000232  ** constants.  The registers begin with iDest and increase consecutively.
000233  ** One register is initialized for each characgter in zTypes[].  For each
000234  ** "s" character in zTypes[], the register is a string if the argument is
000235  ** not NULL, or OP_Null if the value is a null pointer.  For each "i" character
000236  ** in zTypes[], the register is initialized to an integer.
000237  */
000238  void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
000239    va_list ap;
000240    int i;
000241    char c;
000242    va_start(ap, zTypes);
000243    for(i=0; (c = zTypes[i])!=0; i++){
000244      if( c=='s' ){
000245        const char *z = va_arg(ap, const char*);
000246        sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest++, 0, z, 0);
000247      }else{
000248        assert( c=='i' );
000249        sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest++);
000250      }
000251    }
000252    va_end(ap);
000253  }
000254  
000255  /*
000256  ** Add an opcode that includes the p4 value as a pointer.
000257  */
000258  int sqlite3VdbeAddOp4(
000259    Vdbe *p,            /* Add the opcode to this VM */
000260    int op,             /* The new opcode */
000261    int p1,             /* The P1 operand */
000262    int p2,             /* The P2 operand */
000263    int p3,             /* The P3 operand */
000264    const char *zP4,    /* The P4 operand */
000265    int p4type          /* P4 operand type */
000266  ){
000267    int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
000268    sqlite3VdbeChangeP4(p, addr, zP4, p4type);
000269    return addr;
000270  }
000271  
000272  /*
000273  ** Add an opcode that includes the p4 value with a P4_INT64 or
000274  ** P4_REAL type.
000275  */
000276  int sqlite3VdbeAddOp4Dup8(
000277    Vdbe *p,            /* Add the opcode to this VM */
000278    int op,             /* The new opcode */
000279    int p1,             /* The P1 operand */
000280    int p2,             /* The P2 operand */
000281    int p3,             /* The P3 operand */
000282    const u8 *zP4,      /* The P4 operand */
000283    int p4type          /* P4 operand type */
000284  ){
000285    char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
000286    if( p4copy ) memcpy(p4copy, zP4, 8);
000287    return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
000288  }
000289  
000290  /*
000291  ** Add an OP_ParseSchema opcode.  This routine is broken out from
000292  ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
000293  ** as having been used.
000294  **
000295  ** The zWhere string must have been obtained from sqlite3_malloc().
000296  ** This routine will take ownership of the allocated memory.
000297  */
000298  void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
000299    int j;
000300    sqlite3VdbeAddOp4(p, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
000301    for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
000302  }
000303  
000304  /*
000305  ** Add an opcode that includes the p4 value as an integer.
000306  */
000307  int sqlite3VdbeAddOp4Int(
000308    Vdbe *p,            /* Add the opcode to this VM */
000309    int op,             /* The new opcode */
000310    int p1,             /* The P1 operand */
000311    int p2,             /* The P2 operand */
000312    int p3,             /* The P3 operand */
000313    int p4              /* The P4 operand as an integer */
000314  ){
000315    int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
000316    if( p->db->mallocFailed==0 ){
000317      VdbeOp *pOp = &p->aOp[addr];
000318      pOp->p4type = P4_INT32;
000319      pOp->p4.i = p4;
000320    }
000321    return addr;
000322  }
000323  
000324  /* Insert the end of a co-routine
000325  */
000326  void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
000327    sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
000328  
000329    /* Clear the temporary register cache, thereby ensuring that each
000330    ** co-routine has its own independent set of registers, because co-routines
000331    ** might expect their registers to be preserved across an OP_Yield, and
000332    ** that could cause problems if two or more co-routines are using the same
000333    ** temporary register.
000334    */
000335    v->pParse->nTempReg = 0;
000336    v->pParse->nRangeReg = 0;
000337  }
000338  
000339  /*
000340  ** Create a new symbolic label for an instruction that has yet to be
000341  ** coded.  The symbolic label is really just a negative number.  The
000342  ** label can be used as the P2 value of an operation.  Later, when
000343  ** the label is resolved to a specific address, the VDBE will scan
000344  ** through its operation list and change all values of P2 which match
000345  ** the label into the resolved address.
000346  **
000347  ** The VDBE knows that a P2 value is a label because labels are
000348  ** always negative and P2 values are suppose to be non-negative.
000349  ** Hence, a negative P2 value is a label that has yet to be resolved.
000350  **
000351  ** Zero is returned if a malloc() fails.
000352  */
000353  int sqlite3VdbeMakeLabel(Vdbe *v){
000354    Parse *p = v->pParse;
000355    int i = p->nLabel++;
000356    assert( v->magic==VDBE_MAGIC_INIT );
000357    if( (i & (i-1))==0 ){
000358      p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, 
000359                                         (i*2+1)*sizeof(p->aLabel[0]));
000360    }
000361    if( p->aLabel ){
000362      p->aLabel[i] = -1;
000363    }
000364    return ADDR(i);
000365  }
000366  
000367  /*
000368  ** Resolve label "x" to be the address of the next instruction to
000369  ** be inserted.  The parameter "x" must have been obtained from
000370  ** a prior call to sqlite3VdbeMakeLabel().
000371  */
000372  void sqlite3VdbeResolveLabel(Vdbe *v, int x){
000373    Parse *p = v->pParse;
000374    int j = ADDR(x);
000375    assert( v->magic==VDBE_MAGIC_INIT );
000376    assert( j<p->nLabel );
000377    assert( j>=0 );
000378    if( p->aLabel ){
000379      p->aLabel[j] = v->nOp;
000380    }
000381  }
000382  
000383  /*
000384  ** Mark the VDBE as one that can only be run one time.
000385  */
000386  void sqlite3VdbeRunOnlyOnce(Vdbe *p){
000387    p->runOnlyOnce = 1;
000388  }
000389  
000390  /*
000391  ** Mark the VDBE as one that can only be run multiple times.
000392  */
000393  void sqlite3VdbeReusable(Vdbe *p){
000394    p->runOnlyOnce = 0;
000395  }
000396  
000397  #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
000398  
000399  /*
000400  ** The following type and function are used to iterate through all opcodes
000401  ** in a Vdbe main program and each of the sub-programs (triggers) it may 
000402  ** invoke directly or indirectly. It should be used as follows:
000403  **
000404  **   Op *pOp;
000405  **   VdbeOpIter sIter;
000406  **
000407  **   memset(&sIter, 0, sizeof(sIter));
000408  **   sIter.v = v;                            // v is of type Vdbe* 
000409  **   while( (pOp = opIterNext(&sIter)) ){
000410  **     // Do something with pOp
000411  **   }
000412  **   sqlite3DbFree(v->db, sIter.apSub);
000413  ** 
000414  */
000415  typedef struct VdbeOpIter VdbeOpIter;
000416  struct VdbeOpIter {
000417    Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
000418    SubProgram **apSub;        /* Array of subprograms */
000419    int nSub;                  /* Number of entries in apSub */
000420    int iAddr;                 /* Address of next instruction to return */
000421    int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
000422  };
000423  static Op *opIterNext(VdbeOpIter *p){
000424    Vdbe *v = p->v;
000425    Op *pRet = 0;
000426    Op *aOp;
000427    int nOp;
000428  
000429    if( p->iSub<=p->nSub ){
000430  
000431      if( p->iSub==0 ){
000432        aOp = v->aOp;
000433        nOp = v->nOp;
000434      }else{
000435        aOp = p->apSub[p->iSub-1]->aOp;
000436        nOp = p->apSub[p->iSub-1]->nOp;
000437      }
000438      assert( p->iAddr<nOp );
000439  
000440      pRet = &aOp[p->iAddr];
000441      p->iAddr++;
000442      if( p->iAddr==nOp ){
000443        p->iSub++;
000444        p->iAddr = 0;
000445      }
000446    
000447      if( pRet->p4type==P4_SUBPROGRAM ){
000448        int nByte = (p->nSub+1)*sizeof(SubProgram*);
000449        int j;
000450        for(j=0; j<p->nSub; j++){
000451          if( p->apSub[j]==pRet->p4.pProgram ) break;
000452        }
000453        if( j==p->nSub ){
000454          p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
000455          if( !p->apSub ){
000456            pRet = 0;
000457          }else{
000458            p->apSub[p->nSub++] = pRet->p4.pProgram;
000459          }
000460        }
000461      }
000462    }
000463  
000464    return pRet;
000465  }
000466  
000467  /*
000468  ** Check if the program stored in the VM associated with pParse may
000469  ** throw an ABORT exception (causing the statement, but not entire transaction
000470  ** to be rolled back). This condition is true if the main program or any
000471  ** sub-programs contains any of the following:
000472  **
000473  **   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
000474  **   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
000475  **   *  OP_Destroy
000476  **   *  OP_VUpdate
000477  **   *  OP_VRename
000478  **   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
000479  **   *  OP_CreateTable and OP_InitCoroutine (for CREATE TABLE AS SELECT ...)
000480  **
000481  ** Then check that the value of Parse.mayAbort is true if an
000482  ** ABORT may be thrown, or false otherwise. Return true if it does
000483  ** match, or false otherwise. This function is intended to be used as
000484  ** part of an assert statement in the compiler. Similar to:
000485  **
000486  **   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
000487  */
000488  int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
000489    int hasAbort = 0;
000490    int hasFkCounter = 0;
000491    int hasCreateTable = 0;
000492    int hasInitCoroutine = 0;
000493    Op *pOp;
000494    VdbeOpIter sIter;
000495    memset(&sIter, 0, sizeof(sIter));
000496    sIter.v = v;
000497  
000498    while( (pOp = opIterNext(&sIter))!=0 ){
000499      int opcode = pOp->opcode;
000500      if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 
000501       || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 
000502        && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
000503      ){
000504        hasAbort = 1;
000505        break;
000506      }
000507      if( opcode==OP_CreateTable ) hasCreateTable = 1;
000508      if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
000509  #ifndef SQLITE_OMIT_FOREIGN_KEY
000510      if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
000511        hasFkCounter = 1;
000512      }
000513  #endif
000514    }
000515    sqlite3DbFree(v->db, sIter.apSub);
000516  
000517    /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
000518    ** If malloc failed, then the while() loop above may not have iterated
000519    ** through all opcodes and hasAbort may be set incorrectly. Return
000520    ** true for this case to prevent the assert() in the callers frame
000521    ** from failing.  */
000522    return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
000523                || (hasCreateTable && hasInitCoroutine) );
000524  }
000525  #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
000526  
000527  /*
000528  ** This routine is called after all opcodes have been inserted.  It loops
000529  ** through all the opcodes and fixes up some details.
000530  **
000531  ** (1) For each jump instruction with a negative P2 value (a label)
000532  **     resolve the P2 value to an actual address.
000533  **
000534  ** (2) Compute the maximum number of arguments used by any SQL function
000535  **     and store that value in *pMaxFuncArgs.
000536  **
000537  ** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
000538  **     indicate what the prepared statement actually does.
000539  **
000540  ** (4) Initialize the p4.xAdvance pointer on opcodes that use it.
000541  **
000542  ** (5) Reclaim the memory allocated for storing labels.
000543  **
000544  ** This routine will only function correctly if the mkopcodeh.tcl generator
000545  ** script numbers the opcodes correctly.  Changes to this routine must be
000546  ** coordinated with changes to mkopcodeh.tcl.
000547  */
000548  static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
000549    int nMaxArgs = *pMaxFuncArgs;
000550    Op *pOp;
000551    Parse *pParse = p->pParse;
000552    int *aLabel = pParse->aLabel;
000553    p->readOnly = 1;
000554    p->bIsReader = 0;
000555    pOp = &p->aOp[p->nOp-1];
000556    while(1){
000557  
000558      /* Only JUMP opcodes and the short list of special opcodes in the switch
000559      ** below need to be considered.  The mkopcodeh.tcl generator script groups
000560      ** all these opcodes together near the front of the opcode list.  Skip
000561      ** any opcode that does not need processing by virtual of the fact that
000562      ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
000563      */
000564      if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
000565        /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
000566        ** cases from this switch! */
000567        switch( pOp->opcode ){
000568          case OP_Transaction: {
000569            if( pOp->p2!=0 ) p->readOnly = 0;
000570            /* fall thru */
000571          }
000572          case OP_AutoCommit:
000573          case OP_Savepoint: {
000574            p->bIsReader = 1;
000575            break;
000576          }
000577  #ifndef SQLITE_OMIT_WAL
000578          case OP_Checkpoint:
000579  #endif
000580          case OP_Vacuum:
000581          case OP_JournalMode: {
000582            p->readOnly = 0;
000583            p->bIsReader = 1;
000584            break;
000585          }
000586  #ifndef SQLITE_OMIT_VIRTUALTABLE
000587          case OP_VUpdate: {
000588            if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
000589            break;
000590          }
000591          case OP_VFilter: {
000592            int n;
000593            assert( (pOp - p->aOp) >= 3 );
000594            assert( pOp[-1].opcode==OP_Integer );
000595            n = pOp[-1].p1;
000596            if( n>nMaxArgs ) nMaxArgs = n;
000597            break;
000598          }
000599  #endif
000600          case OP_Next:
000601          case OP_NextIfOpen:
000602          case OP_SorterNext: {
000603            pOp->p4.xAdvance = sqlite3BtreeNext;
000604            pOp->p4type = P4_ADVANCE;
000605            break;
000606          }
000607          case OP_Prev:
000608          case OP_PrevIfOpen: {
000609            pOp->p4.xAdvance = sqlite3BtreePrevious;
000610            pOp->p4type = P4_ADVANCE;
000611            break;
000612          }
000613        }
000614        if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 && pOp->p2<0 ){
000615          assert( ADDR(pOp->p2)<pParse->nLabel );
000616          pOp->p2 = aLabel[ADDR(pOp->p2)];
000617        }
000618      }
000619      if( pOp==p->aOp ) break;
000620      pOp--;
000621    }
000622    sqlite3DbFree(p->db, pParse->aLabel);
000623    pParse->aLabel = 0;
000624    pParse->nLabel = 0;
000625    *pMaxFuncArgs = nMaxArgs;
000626    assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
000627  }
000628  
000629  /*
000630  ** Return the address of the next instruction to be inserted.
000631  */
000632  int sqlite3VdbeCurrentAddr(Vdbe *p){
000633    assert( p->magic==VDBE_MAGIC_INIT );
000634    return p->nOp;
000635  }
000636  
000637  /*
000638  ** Verify that at least N opcode slots are available in p without
000639  ** having to malloc for more space (except when compiled using
000640  ** SQLITE_TEST_REALLOC_STRESS).  This interface is used during testing
000641  ** to verify that certain calls to sqlite3VdbeAddOpList() can never
000642  ** fail due to a OOM fault and hence that the return value from
000643  ** sqlite3VdbeAddOpList() will always be non-NULL.
000644  */
000645  #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
000646  void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
000647    assert( p->nOp + N <= p->pParse->nOpAlloc );
000648  }
000649  #endif
000650  
000651  /*
000652  ** This function returns a pointer to the array of opcodes associated with
000653  ** the Vdbe passed as the first argument. It is the callers responsibility
000654  ** to arrange for the returned array to be eventually freed using the 
000655  ** vdbeFreeOpArray() function.
000656  **
000657  ** Before returning, *pnOp is set to the number of entries in the returned
000658  ** array. Also, *pnMaxArg is set to the larger of its current value and 
000659  ** the number of entries in the Vdbe.apArg[] array required to execute the 
000660  ** returned program.
000661  */
000662  VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
000663    VdbeOp *aOp = p->aOp;
000664    assert( aOp && !p->db->mallocFailed );
000665  
000666    /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
000667    assert( DbMaskAllZero(p->btreeMask) );
000668  
000669    resolveP2Values(p, pnMaxArg);
000670    *pnOp = p->nOp;
000671    p->aOp = 0;
000672    return aOp;
000673  }
000674  
000675  /*
000676  ** Add a whole list of operations to the operation stack.  Return a
000677  ** pointer to the first operation inserted.
000678  **
000679  ** Non-zero P2 arguments to jump instructions are automatically adjusted
000680  ** so that the jump target is relative to the first operation inserted.
000681  */
000682  VdbeOp *sqlite3VdbeAddOpList(
000683    Vdbe *p,                     /* Add opcodes to the prepared statement */
000684    int nOp,                     /* Number of opcodes to add */
000685    VdbeOpList const *aOp,       /* The opcodes to be added */
000686    int iLineno                  /* Source-file line number of first opcode */
000687  ){
000688    int i;
000689    VdbeOp *pOut, *pFirst;
000690    assert( nOp>0 );
000691    assert( p->magic==VDBE_MAGIC_INIT );
000692    if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
000693      return 0;
000694    }
000695    pFirst = pOut = &p->aOp[p->nOp];
000696    for(i=0; i<nOp; i++, aOp++, pOut++){
000697      pOut->opcode = aOp->opcode;
000698      pOut->p1 = aOp->p1;
000699      pOut->p2 = aOp->p2;
000700      assert( aOp->p2>=0 );
000701      if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
000702        pOut->p2 += p->nOp;
000703      }
000704      pOut->p3 = aOp->p3;
000705      pOut->p4type = P4_NOTUSED;
000706      pOut->p4.p = 0;
000707      pOut->p5 = 0;
000708  #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
000709      pOut->zComment = 0;
000710  #endif
000711  #ifdef SQLITE_VDBE_COVERAGE
000712      pOut->iSrcLine = iLineno+i;
000713  #else
000714      (void)iLineno;
000715  #endif
000716  #ifdef SQLITE_DEBUG
000717      if( p->db->flags & SQLITE_VdbeAddopTrace ){
000718        sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
000719      }
000720  #endif
000721    }
000722    p->nOp += nOp;
000723    return pFirst;
000724  }
000725  
000726  #if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
000727  /*
000728  ** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
000729  */
000730  void sqlite3VdbeScanStatus(
000731    Vdbe *p,                        /* VM to add scanstatus() to */
000732    int addrExplain,                /* Address of OP_Explain (or 0) */
000733    int addrLoop,                   /* Address of loop counter */ 
000734    int addrVisit,                  /* Address of rows visited counter */
000735    LogEst nEst,                    /* Estimated number of output rows */
000736    const char *zName               /* Name of table or index being scanned */
000737  ){
000738    int nByte = (p->nScan+1) * sizeof(ScanStatus);
000739    ScanStatus *aNew;
000740    aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
000741    if( aNew ){
000742      ScanStatus *pNew = &aNew[p->nScan++];
000743      pNew->addrExplain = addrExplain;
000744      pNew->addrLoop = addrLoop;
000745      pNew->addrVisit = addrVisit;
000746      pNew->nEst = nEst;
000747      pNew->zName = sqlite3DbStrDup(p->db, zName);
000748      p->aScan = aNew;
000749    }
000750  }
000751  #endif
000752  
000753  
000754  /*
000755  ** Change the value of the opcode, or P1, P2, P3, or P5 operands
000756  ** for a specific instruction.
000757  */
000758  void sqlite3VdbeChangeOpcode(Vdbe *p, u32 addr, u8 iNewOpcode){
000759    sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
000760  }
000761  void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
000762    sqlite3VdbeGetOp(p,addr)->p1 = val;
000763  }
000764  void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
000765    sqlite3VdbeGetOp(p,addr)->p2 = val;
000766  }
000767  void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
000768    sqlite3VdbeGetOp(p,addr)->p3 = val;
000769  }
000770  void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){
000771    assert( p->nOp>0 || p->db->mallocFailed );
000772    if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
000773  }
000774  
000775  /*
000776  ** Change the P2 operand of instruction addr so that it points to
000777  ** the address of the next instruction to be coded.
000778  */
000779  void sqlite3VdbeJumpHere(Vdbe *p, int addr){
000780    sqlite3VdbeChangeP2(p, addr, p->nOp);
000781  }
000782  
000783  
000784  /*
000785  ** If the input FuncDef structure is ephemeral, then free it.  If
000786  ** the FuncDef is not ephermal, then do nothing.
000787  */
000788  static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
000789    if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
000790      sqlite3DbFree(db, pDef);
000791    }
000792  }
000793  
000794  static void vdbeFreeOpArray(sqlite3 *, Op *, int);
000795  
000796  /*
000797  ** Delete a P4 value if necessary.
000798  */
000799  static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
000800    if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
000801    sqlite3DbFree(db, p);
000802  }
000803  static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
000804    freeEphemeralFunction(db, p->pFunc);
000805    sqlite3DbFree(db, p);
000806  }
000807  static void freeP4(sqlite3 *db, int p4type, void *p4){
000808    assert( db );
000809    switch( p4type ){
000810      case P4_FUNCCTX: {
000811        freeP4FuncCtx(db, (sqlite3_context*)p4);
000812        break;
000813      }
000814      case P4_REAL:
000815      case P4_INT64:
000816      case P4_DYNAMIC:
000817      case P4_INTARRAY: {
000818        sqlite3DbFree(db, p4);
000819        break;
000820      }
000821      case P4_KEYINFO: {
000822        if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
000823        break;
000824      }
000825  #ifdef SQLITE_ENABLE_CURSOR_HINTS
000826      case P4_EXPR: {
000827        sqlite3ExprDelete(db, (Expr*)p4);
000828        break;
000829      }
000830  #endif
000831      case P4_FUNCDEF: {
000832        freeEphemeralFunction(db, (FuncDef*)p4);
000833        break;
000834      }
000835      case P4_MEM: {
000836        if( db->pnBytesFreed==0 ){
000837          sqlite3ValueFree((sqlite3_value*)p4);
000838        }else{
000839          freeP4Mem(db, (Mem*)p4);
000840        }
000841        break;
000842      }
000843      case P4_VTAB : {
000844        if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
000845        break;
000846      }
000847    }
000848  }
000849  
000850  /*
000851  ** Free the space allocated for aOp and any p4 values allocated for the
000852  ** opcodes contained within. If aOp is not NULL it is assumed to contain 
000853  ** nOp entries. 
000854  */
000855  static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
000856    if( aOp ){
000857      Op *pOp;
000858      for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
000859        if( pOp->p4type ) freeP4(db, pOp->p4type, pOp->p4.p);
000860  #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
000861        sqlite3DbFree(db, pOp->zComment);
000862  #endif     
000863      }
000864    }
000865    sqlite3DbFree(db, aOp);
000866  }
000867  
000868  /*
000869  ** Link the SubProgram object passed as the second argument into the linked
000870  ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
000871  ** objects when the VM is no longer required.
000872  */
000873  void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
000874    p->pNext = pVdbe->pProgram;
000875    pVdbe->pProgram = p;
000876  }
000877  
000878  /*
000879  ** Change the opcode at addr into OP_Noop
000880  */
000881  int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
000882    VdbeOp *pOp;
000883    if( p->db->mallocFailed ) return 0;
000884    assert( addr>=0 && addr<p->nOp );
000885    pOp = &p->aOp[addr];
000886    freeP4(p->db, pOp->p4type, pOp->p4.p);
000887    pOp->p4type = P4_NOTUSED;
000888    pOp->p4.z = 0;
000889    pOp->opcode = OP_Noop;
000890    return 1;
000891  }
000892  
000893  /*
000894  ** If the last opcode is "op" and it is not a jump destination,
000895  ** then remove it.  Return true if and only if an opcode was removed.
000896  */
000897  int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
000898    if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
000899      return sqlite3VdbeChangeToNoop(p, p->nOp-1);
000900    }else{
000901      return 0;
000902    }
000903  }
000904  
000905  /*
000906  ** Change the value of the P4 operand for a specific instruction.
000907  ** This routine is useful when a large program is loaded from a
000908  ** static array using sqlite3VdbeAddOpList but we want to make a
000909  ** few minor changes to the program.
000910  **
000911  ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
000912  ** the string is made into memory obtained from sqlite3_malloc().
000913  ** A value of n==0 means copy bytes of zP4 up to and including the
000914  ** first null byte.  If n>0 then copy n+1 bytes of zP4.
000915  ** 
000916  ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
000917  ** to a string or structure that is guaranteed to exist for the lifetime of
000918  ** the Vdbe. In these cases we can just copy the pointer.
000919  **
000920  ** If addr<0 then change P4 on the most recently inserted instruction.
000921  */
000922  static void SQLITE_NOINLINE vdbeChangeP4Full(
000923    Vdbe *p,
000924    Op *pOp,
000925    const char *zP4,
000926    int n
000927  ){
000928    if( pOp->p4type ){
000929      freeP4(p->db, pOp->p4type, pOp->p4.p);
000930      pOp->p4type = 0;
000931      pOp->p4.p = 0;
000932    }
000933    if( n<0 ){
000934      sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
000935    }else{
000936      if( n==0 ) n = sqlite3Strlen30(zP4);
000937      pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
000938      pOp->p4type = P4_DYNAMIC;
000939    }
000940  }
000941  void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
000942    Op *pOp;
000943    sqlite3 *db;
000944    assert( p!=0 );
000945    db = p->db;
000946    assert( p->magic==VDBE_MAGIC_INIT );
000947    assert( p->aOp!=0 || db->mallocFailed );
000948    if( db->mallocFailed ){
000949      if( n!=P4_VTAB ) freeP4(db, n, (void*)*(char**)&zP4);
000950      return;
000951    }
000952    assert( p->nOp>0 );
000953    assert( addr<p->nOp );
000954    if( addr<0 ){
000955      addr = p->nOp - 1;
000956    }
000957    pOp = &p->aOp[addr];
000958    if( n>=0 || pOp->p4type ){
000959      vdbeChangeP4Full(p, pOp, zP4, n);
000960      return;
000961    }
000962    if( n==P4_INT32 ){
000963      /* Note: this cast is safe, because the origin data point was an int
000964      ** that was cast to a (const char *). */
000965      pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
000966      pOp->p4type = P4_INT32;
000967    }else if( zP4!=0 ){
000968      assert( n<0 );
000969      pOp->p4.p = (void*)zP4;
000970      pOp->p4type = (signed char)n;
000971      if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4);
000972    }
000973  }
000974  
000975  /*
000976  ** Change the P4 operand of the most recently coded instruction 
000977  ** to the value defined by the arguments.  This is a high-speed
000978  ** version of sqlite3VdbeChangeP4().
000979  **
000980  ** The P4 operand must not have been previously defined.  And the new
000981  ** P4 must not be P4_INT32.  Use sqlite3VdbeChangeP4() in either of
000982  ** those cases.
000983  */
000984  void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
000985    VdbeOp *pOp;
000986    assert( n!=P4_INT32 && n!=P4_VTAB );
000987    assert( n<=0 );
000988    if( p->db->mallocFailed ){
000989      freeP4(p->db, n, pP4);
000990    }else{
000991      assert( pP4!=0 );
000992      assert( p->nOp>0 );
000993      pOp = &p->aOp[p->nOp-1];
000994      assert( pOp->p4type==P4_NOTUSED );
000995      pOp->p4type = n;
000996      pOp->p4.p = pP4;
000997    }
000998  }
000999  
001000  /*
001001  ** Set the P4 on the most recently added opcode to the KeyInfo for the
001002  ** index given.
001003  */
001004  void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
001005    Vdbe *v = pParse->pVdbe;
001006    KeyInfo *pKeyInfo;
001007    assert( v!=0 );
001008    assert( pIdx!=0 );
001009    pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
001010    if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
001011  }
001012  
001013  #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
001014  /*
001015  ** Change the comment on the most recently coded instruction.  Or
001016  ** insert a No-op and add the comment to that new instruction.  This
001017  ** makes the code easier to read during debugging.  None of this happens
001018  ** in a production build.
001019  */
001020  static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
001021    assert( p->nOp>0 || p->aOp==0 );
001022    assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
001023    if( p->nOp ){
001024      assert( p->aOp );
001025      sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
001026      p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
001027    }
001028  }
001029  void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
001030    va_list ap;
001031    if( p ){
001032      va_start(ap, zFormat);
001033      vdbeVComment(p, zFormat, ap);
001034      va_end(ap);
001035    }
001036  }
001037  void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
001038    va_list ap;
001039    if( p ){
001040      sqlite3VdbeAddOp0(p, OP_Noop);
001041      va_start(ap, zFormat);
001042      vdbeVComment(p, zFormat, ap);
001043      va_end(ap);
001044    }
001045  }
001046  #endif  /* NDEBUG */
001047  
001048  #ifdef SQLITE_VDBE_COVERAGE
001049  /*
001050  ** Set the value if the iSrcLine field for the previously coded instruction.
001051  */
001052  void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
001053    sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
001054  }
001055  #endif /* SQLITE_VDBE_COVERAGE */
001056  
001057  /*
001058  ** Return the opcode for a given address.  If the address is -1, then
001059  ** return the most recently inserted opcode.
001060  **
001061  ** If a memory allocation error has occurred prior to the calling of this
001062  ** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
001063  ** is readable but not writable, though it is cast to a writable value.
001064  ** The return of a dummy opcode allows the call to continue functioning
001065  ** after an OOM fault without having to check to see if the return from 
001066  ** this routine is a valid pointer.  But because the dummy.opcode is 0,
001067  ** dummy will never be written to.  This is verified by code inspection and
001068  ** by running with Valgrind.
001069  */
001070  VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
001071    /* C89 specifies that the constant "dummy" will be initialized to all
001072    ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
001073    static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
001074    assert( p->magic==VDBE_MAGIC_INIT );
001075    if( addr<0 ){
001076      addr = p->nOp - 1;
001077    }
001078    assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
001079    if( p->db->mallocFailed ){
001080      return (VdbeOp*)&dummy;
001081    }else{
001082      return &p->aOp[addr];
001083    }
001084  }
001085  
001086  #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
001087  /*
001088  ** Return an integer value for one of the parameters to the opcode pOp
001089  ** determined by character c.
001090  */
001091  static int translateP(char c, const Op *pOp){
001092    if( c=='1' ) return pOp->p1;
001093    if( c=='2' ) return pOp->p2;
001094    if( c=='3' ) return pOp->p3;
001095    if( c=='4' ) return pOp->p4.i;
001096    return pOp->p5;
001097  }
001098  
001099  /*
001100  ** Compute a string for the "comment" field of a VDBE opcode listing.
001101  **
001102  ** The Synopsis: field in comments in the vdbe.c source file gets converted
001103  ** to an extra string that is appended to the sqlite3OpcodeName().  In the
001104  ** absence of other comments, this synopsis becomes the comment on the opcode.
001105  ** Some translation occurs:
001106  **
001107  **       "PX"      ->  "r[X]"
001108  **       "PX@PY"   ->  "r[X..X+Y-1]"  or "r[x]" if y is 0 or 1
001109  **       "PX@PY+1" ->  "r[X..X+Y]"    or "r[x]" if y is 0
001110  **       "PY..PY"  ->  "r[X..Y]"      or "r[x]" if y<=x
001111  */
001112  static int displayComment(
001113    const Op *pOp,     /* The opcode to be commented */
001114    const char *zP4,   /* Previously obtained value for P4 */
001115    char *zTemp,       /* Write result here */
001116    int nTemp          /* Space available in zTemp[] */
001117  ){
001118    const char *zOpName;
001119    const char *zSynopsis;
001120    int nOpName;
001121    int ii, jj;
001122    char zAlt[50];
001123    zOpName = sqlite3OpcodeName(pOp->opcode);
001124    nOpName = sqlite3Strlen30(zOpName);
001125    if( zOpName[nOpName+1] ){
001126      int seenCom = 0;
001127      char c;
001128      zSynopsis = zOpName += nOpName + 1;
001129      if( strncmp(zSynopsis,"IF ",3)==0 ){
001130        if( pOp->p5 & SQLITE_STOREP2 ){
001131          sqlite3_snprintf(sizeof(zAlt), zAlt, "r[P2] = (%s)", zSynopsis+3);
001132        }else{
001133          sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
001134        }
001135        zSynopsis = zAlt;
001136      }
001137      for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
001138        if( c=='P' ){
001139          c = zSynopsis[++ii];
001140          if( c=='4' ){
001141            sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
001142          }else if( c=='X' ){
001143            sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
001144            seenCom = 1;
001145          }else{
001146            int v1 = translateP(c, pOp);
001147            int v2;
001148            sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
001149            if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
001150              ii += 3;
001151              jj += sqlite3Strlen30(zTemp+jj);
001152              v2 = translateP(zSynopsis[ii], pOp);
001153              if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
001154                ii += 2;
001155                v2++;
001156              }
001157              if( v2>1 ){
001158                sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
001159              }
001160            }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
001161              ii += 4;
001162            }
001163          }
001164          jj += sqlite3Strlen30(zTemp+jj);
001165        }else{
001166          zTemp[jj++] = c;
001167        }
001168      }
001169      if( !seenCom && jj<nTemp-5 && pOp->zComment ){
001170        sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
001171        jj += sqlite3Strlen30(zTemp+jj);
001172      }
001173      if( jj<nTemp ) zTemp[jj] = 0;
001174    }else if( pOp->zComment ){
001175      sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
001176      jj = sqlite3Strlen30(zTemp);
001177    }else{
001178      zTemp[0] = 0;
001179      jj = 0;
001180    }
001181    return jj;
001182  }
001183  #endif /* SQLITE_DEBUG */
001184  
001185  #if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
001186  /*
001187  ** Translate the P4.pExpr value for an OP_CursorHint opcode into text
001188  ** that can be displayed in the P4 column of EXPLAIN output.
001189  */
001190  static void displayP4Expr(StrAccum *p, Expr *pExpr){
001191    const char *zOp = 0;
001192    switch( pExpr->op ){
001193      case TK_STRING:
001194        sqlite3XPrintf(p, "%Q", pExpr->u.zToken);
001195        break;
001196      case TK_INTEGER:
001197        sqlite3XPrintf(p, "%d", pExpr->u.iValue);
001198        break;
001199      case TK_NULL:
001200        sqlite3XPrintf(p, "NULL");
001201        break;
001202      case TK_REGISTER: {
001203        sqlite3XPrintf(p, "r[%d]", pExpr->iTable);
001204        break;
001205      }
001206      case TK_COLUMN: {
001207        if( pExpr->iColumn<0 ){
001208          sqlite3XPrintf(p, "rowid");
001209        }else{
001210          sqlite3XPrintf(p, "c%d", (int)pExpr->iColumn);
001211        }
001212        break;
001213      }
001214      case TK_LT:      zOp = "LT";      break;
001215      case TK_LE:      zOp = "LE";      break;
001216      case TK_GT:      zOp = "GT";      break;
001217      case TK_GE:      zOp = "GE";      break;
001218      case TK_NE:      zOp = "NE";      break;
001219      case TK_EQ:      zOp = "EQ";      break;
001220      case TK_IS:      zOp = "IS";      break;
001221      case TK_ISNOT:   zOp = "ISNOT";   break;
001222      case TK_AND:     zOp = "AND";     break;
001223      case TK_OR:      zOp = "OR";      break;
001224      case TK_PLUS:    zOp = "ADD";     break;
001225      case TK_STAR:    zOp = "MUL";     break;
001226      case TK_MINUS:   zOp = "SUB";     break;
001227      case TK_REM:     zOp = "REM";     break;
001228      case TK_BITAND:  zOp = "BITAND";  break;
001229      case TK_BITOR:   zOp = "BITOR";   break;
001230      case TK_SLASH:   zOp = "DIV";     break;
001231      case TK_LSHIFT:  zOp = "LSHIFT";  break;
001232      case TK_RSHIFT:  zOp = "RSHIFT";  break;
001233      case TK_CONCAT:  zOp = "CONCAT";  break;
001234      case TK_UMINUS:  zOp = "MINUS";   break;
001235      case TK_UPLUS:   zOp = "PLUS";    break;
001236      case TK_BITNOT:  zOp = "BITNOT";  break;
001237      case TK_NOT:     zOp = "NOT";     break;
001238      case TK_ISNULL:  zOp = "ISNULL";  break;
001239      case TK_NOTNULL: zOp = "NOTNULL"; break;
001240  
001241      default:
001242        sqlite3XPrintf(p, "%s", "expr");
001243        break;
001244    }
001245  
001246    if( zOp ){
001247      sqlite3XPrintf(p, "%s(", zOp);
001248      displayP4Expr(p, pExpr->pLeft);
001249      if( pExpr->pRight ){
001250        sqlite3StrAccumAppend(p, ",", 1);
001251        displayP4Expr(p, pExpr->pRight);
001252      }
001253      sqlite3StrAccumAppend(p, ")", 1);
001254    }
001255  }
001256  #endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
001257  
001258  
001259  #if VDBE_DISPLAY_P4
001260  /*
001261  ** Compute a string that describes the P4 parameter for an opcode.
001262  ** Use zTemp for any required temporary buffer space.
001263  */
001264  static char *displayP4(Op *pOp, char *zTemp, int nTemp){
001265    char *zP4 = zTemp;
001266    StrAccum x;
001267    assert( nTemp>=20 );
001268    sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0);
001269    switch( pOp->p4type ){
001270      case P4_KEYINFO: {
001271        int j;
001272        KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
001273        assert( pKeyInfo->aSortOrder!=0 );
001274        sqlite3XPrintf(&x, "k(%d", pKeyInfo->nField);
001275        for(j=0; j<pKeyInfo->nField; j++){
001276          CollSeq *pColl = pKeyInfo->aColl[j];
001277          const char *zColl = pColl ? pColl->zName : "";
001278          if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
001279          sqlite3XPrintf(&x, ",%s%s", pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
001280        }
001281        sqlite3StrAccumAppend(&x, ")", 1);
001282        break;
001283      }
001284  #ifdef SQLITE_ENABLE_CURSOR_HINTS
001285      case P4_EXPR: {
001286        displayP4Expr(&x, pOp->p4.pExpr);
001287        break;
001288      }
001289  #endif
001290      case P4_COLLSEQ: {
001291        CollSeq *pColl = pOp->p4.pColl;
001292        sqlite3XPrintf(&x, "(%.20s)", pColl->zName);
001293        break;
001294      }
001295      case P4_FUNCDEF: {
001296        FuncDef *pDef = pOp->p4.pFunc;
001297        sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
001298        break;
001299      }
001300  #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
001301      case P4_FUNCCTX: {
001302        FuncDef *pDef = pOp->p4.pCtx->pFunc;
001303        sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
001304        break;
001305      }
001306  #endif
001307      case P4_INT64: {
001308        sqlite3XPrintf(&x, "%lld", *pOp->p4.pI64);
001309        break;
001310      }
001311      case P4_INT32: {
001312        sqlite3XPrintf(&x, "%d", pOp->p4.i);
001313        break;
001314      }
001315      case P4_REAL: {
001316        sqlite3XPrintf(&x, "%.16g", *pOp->p4.pReal);
001317        break;
001318      }
001319      case P4_MEM: {
001320        Mem *pMem = pOp->p4.pMem;
001321        if( pMem->flags & MEM_Str ){
001322          zP4 = pMem->z;
001323        }else if( pMem->flags & MEM_Int ){
001324          sqlite3XPrintf(&x, "%lld", pMem->u.i);
001325        }else if( pMem->flags & MEM_Real ){
001326          sqlite3XPrintf(&x, "%.16g", pMem->u.r);
001327        }else if( pMem->flags & MEM_Null ){
001328          zP4 = "NULL";
001329        }else{
001330          assert( pMem->flags & MEM_Blob );
001331          zP4 = "(blob)";
001332        }
001333        break;
001334      }
001335  #ifndef SQLITE_OMIT_VIRTUALTABLE
001336      case P4_VTAB: {
001337        sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
001338        sqlite3XPrintf(&x, "vtab:%p", pVtab);
001339        break;
001340      }
001341  #endif
001342      case P4_INTARRAY: {
001343        int i;
001344        int *ai = pOp->p4.ai;
001345        int n = ai[0];   /* The first element of an INTARRAY is always the
001346                         ** count of the number of elements to follow */
001347        for(i=1; i<n; i++){
001348          sqlite3XPrintf(&x, ",%d", ai[i]);
001349        }
001350        zTemp[0] = '[';
001351        sqlite3StrAccumAppend(&x, "]", 1);
001352        break;
001353      }
001354      case P4_SUBPROGRAM: {
001355        sqlite3XPrintf(&x, "program");
001356        break;
001357      }
001358      case P4_ADVANCE: {
001359        zTemp[0] = 0;
001360        break;
001361      }
001362      case P4_TABLE: {
001363        sqlite3XPrintf(&x, "%s", pOp->p4.pTab->zName);
001364        break;
001365      }
001366      default: {
001367        zP4 = pOp->p4.z;
001368        if( zP4==0 ){
001369          zP4 = zTemp;
001370          zTemp[0] = 0;
001371        }
001372      }
001373    }
001374    sqlite3StrAccumFinish(&x);
001375    assert( zP4!=0 );
001376    return zP4;
001377  }
001378  #endif /* VDBE_DISPLAY_P4 */
001379  
001380  /*
001381  ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
001382  **
001383  ** The prepared statements need to know in advance the complete set of
001384  ** attached databases that will be use.  A mask of these databases
001385  ** is maintained in p->btreeMask.  The p->lockMask value is the subset of
001386  ** p->btreeMask of databases that will require a lock.
001387  */
001388  void sqlite3VdbeUsesBtree(Vdbe *p, int i){
001389    assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
001390    assert( i<(int)sizeof(p->btreeMask)*8 );
001391    DbMaskSet(p->btreeMask, i);
001392    if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
001393      DbMaskSet(p->lockMask, i);
001394    }
001395  }
001396  
001397  #if !defined(SQLITE_OMIT_SHARED_CACHE)
001398  /*
001399  ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
001400  ** this routine obtains the mutex associated with each BtShared structure
001401  ** that may be accessed by the VM passed as an argument. In doing so it also
001402  ** sets the BtShared.db member of each of the BtShared structures, ensuring
001403  ** that the correct busy-handler callback is invoked if required.
001404  **
001405  ** If SQLite is not threadsafe but does support shared-cache mode, then
001406  ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
001407  ** of all of BtShared structures accessible via the database handle 
001408  ** associated with the VM.
001409  **
001410  ** If SQLite is not threadsafe and does not support shared-cache mode, this
001411  ** function is a no-op.
001412  **
001413  ** The p->btreeMask field is a bitmask of all btrees that the prepared 
001414  ** statement p will ever use.  Let N be the number of bits in p->btreeMask
001415  ** corresponding to btrees that use shared cache.  Then the runtime of
001416  ** this routine is N*N.  But as N is rarely more than 1, this should not
001417  ** be a problem.
001418  */
001419  void sqlite3VdbeEnter(Vdbe *p){
001420    int i;
001421    sqlite3 *db;
001422    Db *aDb;
001423    int nDb;
001424    if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
001425    db = p->db;
001426    aDb = db->aDb;
001427    nDb = db->nDb;
001428    for(i=0; i<nDb; i++){
001429      if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
001430        sqlite3BtreeEnter(aDb[i].pBt);
001431      }
001432    }
001433  }
001434  #endif
001435  
001436  #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
001437  /*
001438  ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
001439  */
001440  static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
001441    int i;
001442    sqlite3 *db;
001443    Db *aDb;
001444    int nDb;
001445    db = p->db;
001446    aDb = db->aDb;
001447    nDb = db->nDb;
001448    for(i=0; i<nDb; i++){
001449      if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
001450        sqlite3BtreeLeave(aDb[i].pBt);
001451      }
001452    }
001453  }
001454  void sqlite3VdbeLeave(Vdbe *p){
001455    if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
001456    vdbeLeave(p);
001457  }
001458  #endif
001459  
001460  #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
001461  /*
001462  ** Print a single opcode.  This routine is used for debugging only.
001463  */
001464  void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
001465    char *zP4;
001466    char zPtr[50];
001467    char zCom[100];
001468    static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
001469    if( pOut==0 ) pOut = stdout;
001470    zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
001471  #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
001472    displayComment(pOp, zP4, zCom, sizeof(zCom));
001473  #else
001474    zCom[0] = 0;
001475  #endif
001476    /* NB:  The sqlite3OpcodeName() function is implemented by code created
001477    ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
001478    ** information from the vdbe.c source text */
001479    fprintf(pOut, zFormat1, pc, 
001480        sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
001481        zCom
001482    );
001483    fflush(pOut);
001484  }
001485  #endif
001486  
001487  /*
001488  ** Initialize an array of N Mem element.
001489  */
001490  static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
001491    while( (N--)>0 ){
001492      p->db = db;
001493      p->flags = flags;
001494      p->szMalloc = 0;
001495  #ifdef SQLITE_DEBUG
001496      p->pScopyFrom = 0;
001497  #endif
001498      p++;
001499    }
001500  }
001501  
001502  /*
001503  ** Release an array of N Mem elements
001504  */
001505  static void releaseMemArray(Mem *p, int N){
001506    if( p && N ){
001507      Mem *pEnd = &p[N];
001508      sqlite3 *db = p->db;
001509      if( db->pnBytesFreed ){
001510        do{
001511          if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
001512        }while( (++p)<pEnd );
001513        return;
001514      }
001515      do{
001516        assert( (&p[1])==pEnd || p[0].db==p[1].db );
001517        assert( sqlite3VdbeCheckMemInvariants(p) );
001518  
001519        /* This block is really an inlined version of sqlite3VdbeMemRelease()
001520        ** that takes advantage of the fact that the memory cell value is 
001521        ** being set to NULL after releasing any dynamic resources.
001522        **
001523        ** The justification for duplicating code is that according to 
001524        ** callgrind, this causes a certain test case to hit the CPU 4.7 
001525        ** percent less (x86 linux, gcc version 4.1.2, -O6) than if 
001526        ** sqlite3MemRelease() were called from here. With -O2, this jumps
001527        ** to 6.6 percent. The test case is inserting 1000 rows into a table 
001528        ** with no indexes using a single prepared INSERT statement, bind() 
001529        ** and reset(). Inserts are grouped into a transaction.
001530        */
001531        testcase( p->flags & MEM_Agg );
001532        testcase( p->flags & MEM_Dyn );
001533        testcase( p->flags & MEM_Frame );
001534        testcase( p->flags & MEM_RowSet );
001535        if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
001536          sqlite3VdbeMemRelease(p);
001537        }else if( p->szMalloc ){
001538          sqlite3DbFree(db, p->zMalloc);
001539          p->szMalloc = 0;
001540        }
001541  
001542        p->flags = MEM_Undefined;
001543      }while( (++p)<pEnd );
001544    }
001545  }
001546  
001547  /*
001548  ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
001549  ** allocated by the OP_Program opcode in sqlite3VdbeExec().
001550  */
001551  void sqlite3VdbeFrameDelete(VdbeFrame *p){
001552    int i;
001553    Mem *aMem = VdbeFrameMem(p);
001554    VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
001555    for(i=0; i<p->nChildCsr; i++){
001556      sqlite3VdbeFreeCursor(p->v, apCsr[i]);
001557    }
001558    releaseMemArray(aMem, p->nChildMem);
001559    sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
001560    sqlite3DbFree(p->v->db, p);
001561  }
001562  
001563  #ifndef SQLITE_OMIT_EXPLAIN
001564  /*
001565  ** Give a listing of the program in the virtual machine.
001566  **
001567  ** The interface is the same as sqlite3VdbeExec().  But instead of
001568  ** running the code, it invokes the callback once for each instruction.
001569  ** This feature is used to implement "EXPLAIN".
001570  **
001571  ** When p->explain==1, each instruction is listed.  When
001572  ** p->explain==2, only OP_Explain instructions are listed and these
001573  ** are shown in a different format.  p->explain==2 is used to implement
001574  ** EXPLAIN QUERY PLAN.
001575  **
001576  ** When p->explain==1, first the main program is listed, then each of
001577  ** the trigger subprograms are listed one by one.
001578  */
001579  int sqlite3VdbeList(
001580    Vdbe *p                   /* The VDBE */
001581  ){
001582    int nRow;                            /* Stop when row count reaches this */
001583    int nSub = 0;                        /* Number of sub-vdbes seen so far */
001584    SubProgram **apSub = 0;              /* Array of sub-vdbes */
001585    Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
001586    sqlite3 *db = p->db;                 /* The database connection */
001587    int i;                               /* Loop counter */
001588    int rc = SQLITE_OK;                  /* Return code */
001589    Mem *pMem = &p->aMem[1];             /* First Mem of result set */
001590  
001591    assert( p->explain );
001592    assert( p->magic==VDBE_MAGIC_RUN );
001593    assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
001594  
001595    /* Even though this opcode does not use dynamic strings for
001596    ** the result, result columns may become dynamic if the user calls
001597    ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
001598    */
001599    releaseMemArray(pMem, 8);
001600    p->pResultSet = 0;
001601  
001602    if( p->rc==SQLITE_NOMEM_BKPT ){
001603      /* This happens if a malloc() inside a call to sqlite3_column_text() or
001604      ** sqlite3_column_text16() failed.  */
001605      sqlite3OomFault(db);
001606      return SQLITE_ERROR;
001607    }
001608  
001609    /* When the number of output rows reaches nRow, that means the
001610    ** listing has finished and sqlite3_step() should return SQLITE_DONE.
001611    ** nRow is the sum of the number of rows in the main program, plus
001612    ** the sum of the number of rows in all trigger subprograms encountered
001613    ** so far.  The nRow value will increase as new trigger subprograms are
001614    ** encountered, but p->pc will eventually catch up to nRow.
001615    */
001616    nRow = p->nOp;
001617    if( p->explain==1 ){
001618      /* The first 8 memory cells are used for the result set.  So we will
001619      ** commandeer the 9th cell to use as storage for an array of pointers
001620      ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
001621      ** cells.  */
001622      assert( p->nMem>9 );
001623      pSub = &p->aMem[9];
001624      if( pSub->flags&MEM_Blob ){
001625        /* On the first call to sqlite3_step(), pSub will hold a NULL.  It is
001626        ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
001627        nSub = pSub->n/sizeof(Vdbe*);
001628        apSub = (SubProgram **)pSub->z;
001629      }
001630      for(i=0; i<nSub; i++){
001631        nRow += apSub[i]->nOp;
001632      }
001633    }
001634  
001635    do{
001636      i = p->pc++;
001637    }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
001638    if( i>=nRow ){
001639      p->rc = SQLITE_OK;
001640      rc = SQLITE_DONE;
001641    }else if( db->u1.isInterrupted ){
001642      p->rc = SQLITE_INTERRUPT;
001643      rc = SQLITE_ERROR;
001644      sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
001645    }else{
001646      char *zP4;
001647      Op *pOp;
001648      if( i<p->nOp ){
001649        /* The output line number is small enough that we are still in the
001650        ** main program. */
001651        pOp = &p->aOp[i];
001652      }else{
001653        /* We are currently listing subprograms.  Figure out which one and
001654        ** pick up the appropriate opcode. */
001655        int j;
001656        i -= p->nOp;
001657        for(j=0; i>=apSub[j]->nOp; j++){
001658          i -= apSub[j]->nOp;
001659        }
001660        pOp = &apSub[j]->aOp[i];
001661      }
001662      if( p->explain==1 ){
001663        pMem->flags = MEM_Int;
001664        pMem->u.i = i;                                /* Program counter */
001665        pMem++;
001666    
001667        pMem->flags = MEM_Static|MEM_Str|MEM_Term;
001668        pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
001669        assert( pMem->z!=0 );
001670        pMem->n = sqlite3Strlen30(pMem->z);
001671        pMem->enc = SQLITE_UTF8;
001672        pMem++;
001673  
001674        /* When an OP_Program opcode is encounter (the only opcode that has
001675        ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
001676        ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
001677        ** has not already been seen.
001678        */
001679        if( pOp->p4type==P4_SUBPROGRAM ){
001680          int nByte = (nSub+1)*sizeof(SubProgram*);
001681          int j;
001682          for(j=0; j<nSub; j++){
001683            if( apSub[j]==pOp->p4.pProgram ) break;
001684          }
001685          if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
001686            apSub = (SubProgram **)pSub->z;
001687            apSub[nSub++] = pOp->p4.pProgram;
001688            pSub->flags |= MEM_Blob;
001689            pSub->n = nSub*sizeof(SubProgram*);
001690          }
001691        }
001692      }
001693  
001694      pMem->flags = MEM_Int;
001695      pMem->u.i = pOp->p1;                          /* P1 */
001696      pMem++;
001697  
001698      pMem->flags = MEM_Int;
001699      pMem->u.i = pOp->p2;                          /* P2 */
001700      pMem++;
001701  
001702      pMem->flags = MEM_Int;
001703      pMem->u.i = pOp->p3;                          /* P3 */
001704      pMem++;
001705  
001706      if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */
001707        assert( p->db->mallocFailed );
001708        return SQLITE_ERROR;
001709      }
001710      pMem->flags = MEM_Str|MEM_Term;
001711      zP4 = displayP4(pOp, pMem->z, pMem->szMalloc);
001712      if( zP4!=pMem->z ){
001713        pMem->n = 0;
001714        sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
001715      }else{
001716        assert( pMem->z!=0 );
001717        pMem->n = sqlite3Strlen30(pMem->z);
001718        pMem->enc = SQLITE_UTF8;
001719      }
001720      pMem++;
001721  
001722      if( p->explain==1 ){
001723        if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
001724          assert( p->db->mallocFailed );
001725          return SQLITE_ERROR;
001726        }
001727        pMem->flags = MEM_Str|MEM_Term;
001728        pMem->n = 2;
001729        sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
001730        pMem->enc = SQLITE_UTF8;
001731        pMem++;
001732    
001733  #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
001734        if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
001735          assert( p->db->mallocFailed );
001736          return SQLITE_ERROR;
001737        }
001738        pMem->flags = MEM_Str|MEM_Term;
001739        pMem->n = displayComment(pOp, zP4, pMem->z, 500);
001740        pMem->enc = SQLITE_UTF8;
001741  #else
001742        pMem->flags = MEM_Null;                       /* Comment */
001743  #endif
001744      }
001745  
001746      p->nResColumn = 8 - 4*(p->explain-1);
001747      p->pResultSet = &p->aMem[1];
001748      p->rc = SQLITE_OK;
001749      rc = SQLITE_ROW;
001750    }
001751    return rc;
001752  }
001753  #endif /* SQLITE_OMIT_EXPLAIN */
001754  
001755  #ifdef SQLITE_DEBUG
001756  /*
001757  ** Print the SQL that was used to generate a VDBE program.
001758  */
001759  void sqlite3VdbePrintSql(Vdbe *p){
001760    const char *z = 0;
001761    if( p->zSql ){
001762      z = p->zSql;
001763    }else if( p->nOp>=1 ){
001764      const VdbeOp *pOp = &p->aOp[0];
001765      if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
001766        z = pOp->p4.z;
001767        while( sqlite3Isspace(*z) ) z++;
001768      }
001769    }
001770    if( z ) printf("SQL: [%s]\n", z);
001771  }
001772  #endif
001773  
001774  #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
001775  /*
001776  ** Print an IOTRACE message showing SQL content.
001777  */
001778  void sqlite3VdbeIOTraceSql(Vdbe *p){
001779    int nOp = p->nOp;
001780    VdbeOp *pOp;
001781    if( sqlite3IoTrace==0 ) return;
001782    if( nOp<1 ) return;
001783    pOp = &p->aOp[0];
001784    if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
001785      int i, j;
001786      char z[1000];
001787      sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
001788      for(i=0; sqlite3Isspace(z[i]); i++){}
001789      for(j=0; z[i]; i++){
001790        if( sqlite3Isspace(z[i]) ){
001791          if( z[i-1]!=' ' ){
001792            z[j++] = ' ';
001793          }
001794        }else{
001795          z[j++] = z[i];
001796        }
001797      }
001798      z[j] = 0;
001799      sqlite3IoTrace("SQL %s\n", z);
001800    }
001801  }
001802  #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
001803  
001804  /* An instance of this object describes bulk memory available for use
001805  ** by subcomponents of a prepared statement.  Space is allocated out
001806  ** of a ReusableSpace object by the allocSpace() routine below.
001807  */
001808  struct ReusableSpace {
001809    u8 *pSpace;          /* Available memory */
001810    int nFree;           /* Bytes of available memory */
001811    int nNeeded;         /* Total bytes that could not be allocated */
001812  };
001813  
001814  /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
001815  ** from the ReusableSpace object.  Return a pointer to the allocated
001816  ** memory on success.  If insufficient memory is available in the
001817  ** ReusableSpace object, increase the ReusableSpace.nNeeded
001818  ** value by the amount needed and return NULL.
001819  **
001820  ** If pBuf is not initially NULL, that means that the memory has already
001821  ** been allocated by a prior call to this routine, so just return a copy
001822  ** of pBuf and leave ReusableSpace unchanged.
001823  **
001824  ** This allocator is employed to repurpose unused slots at the end of the
001825  ** opcode array of prepared state for other memory needs of the prepared
001826  ** statement.
001827  */
001828  static void *allocSpace(
001829    struct ReusableSpace *p,  /* Bulk memory available for allocation */
001830    void *pBuf,               /* Pointer to a prior allocation */
001831    int nByte                 /* Bytes of memory needed */
001832  ){
001833    assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
001834    if( pBuf==0 ){
001835      nByte = ROUND8(nByte);
001836      if( nByte <= p->nFree ){
001837        p->nFree -= nByte;
001838        pBuf = &p->pSpace[p->nFree];
001839      }else{
001840        p->nNeeded += nByte;
001841      }
001842    }
001843    assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
001844    return pBuf;
001845  }
001846  
001847  /*
001848  ** Rewind the VDBE back to the beginning in preparation for
001849  ** running it.
001850  */
001851  void sqlite3VdbeRewind(Vdbe *p){
001852  #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
001853    int i;
001854  #endif
001855    assert( p!=0 );
001856    assert( p->magic==VDBE_MAGIC_INIT || p->magic==VDBE_MAGIC_RESET );
001857  
001858    /* There should be at least one opcode.
001859    */
001860    assert( p->nOp>0 );
001861  
001862    /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
001863    p->magic = VDBE_MAGIC_RUN;
001864  
001865  #ifdef SQLITE_DEBUG
001866    for(i=0; i<p->nMem; i++){
001867      assert( p->aMem[i].db==p->db );
001868    }
001869  #endif
001870    p->pc = -1;
001871    p->rc = SQLITE_OK;
001872    p->errorAction = OE_Abort;
001873    p->nChange = 0;
001874    p->cacheCtr = 1;
001875    p->minWriteFileFormat = 255;
001876    p->iStatement = 0;
001877    p->nFkConstraint = 0;
001878  #ifdef VDBE_PROFILE
001879    for(i=0; i<p->nOp; i++){
001880      p->aOp[i].cnt = 0;
001881      p->aOp[i].cycles = 0;
001882    }
001883  #endif
001884  }
001885  
001886  /*
001887  ** Prepare a virtual machine for execution for the first time after
001888  ** creating the virtual machine.  This involves things such
001889  ** as allocating registers and initializing the program counter.
001890  ** After the VDBE has be prepped, it can be executed by one or more
001891  ** calls to sqlite3VdbeExec().  
001892  **
001893  ** This function may be called exactly once on each virtual machine.
001894  ** After this routine is called the VM has been "packaged" and is ready
001895  ** to run.  After this routine is called, further calls to 
001896  ** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
001897  ** the Vdbe from the Parse object that helped generate it so that the
001898  ** the Vdbe becomes an independent entity and the Parse object can be
001899  ** destroyed.
001900  **
001901  ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
001902  ** to its initial state after it has been run.
001903  */
001904  void sqlite3VdbeMakeReady(
001905    Vdbe *p,                       /* The VDBE */
001906    Parse *pParse                  /* Parsing context */
001907  ){
001908    sqlite3 *db;                   /* The database connection */
001909    int nVar;                      /* Number of parameters */
001910    int nMem;                      /* Number of VM memory registers */
001911    int nCursor;                   /* Number of cursors required */
001912    int nArg;                      /* Number of arguments in subprograms */
001913    int n;                         /* Loop counter */
001914    struct ReusableSpace x;        /* Reusable bulk memory */
001915  
001916    assert( p!=0 );
001917    assert( p->nOp>0 );
001918    assert( pParse!=0 );
001919    assert( p->magic==VDBE_MAGIC_INIT );
001920    assert( pParse==p->pParse );
001921    db = p->db;
001922    assert( db->mallocFailed==0 );
001923    nVar = pParse->nVar;
001924    nMem = pParse->nMem;
001925    nCursor = pParse->nTab;
001926    nArg = pParse->nMaxArg;
001927    
001928    /* Each cursor uses a memory cell.  The first cursor (cursor 0) can
001929    ** use aMem[0] which is not otherwise used by the VDBE program.  Allocate
001930    ** space at the end of aMem[] for cursors 1 and greater.
001931    ** See also: allocateCursor().
001932    */
001933    nMem += nCursor;
001934    if( nCursor==0 && nMem>0 ) nMem++;  /* Space for aMem[0] even if not used */
001935  
001936    /* Figure out how much reusable memory is available at the end of the
001937    ** opcode array.  This extra memory will be reallocated for other elements
001938    ** of the prepared statement.
001939    */
001940    n = ROUND8(sizeof(Op)*p->nOp);              /* Bytes of opcode memory used */
001941    x.pSpace = &((u8*)p->aOp)[n];               /* Unused opcode memory */
001942    assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) );
001943    x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n);  /* Bytes of unused memory */
001944    assert( x.nFree>=0 );
001945    assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
001946  
001947    resolveP2Values(p, &nArg);
001948    p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
001949    if( pParse->explain && nMem<10 ){
001950      nMem = 10;
001951    }
001952    p->expired = 0;
001953  
001954    /* Memory for registers, parameters, cursor, etc, is allocated in one or two
001955    ** passes.  On the first pass, we try to reuse unused memory at the 
001956    ** end of the opcode array.  If we are unable to satisfy all memory
001957    ** requirements by reusing the opcode array tail, then the second
001958    ** pass will fill in the remainder using a fresh memory allocation.  
001959    **
001960    ** This two-pass approach that reuses as much memory as possible from
001961    ** the leftover memory at the end of the opcode array.  This can significantly
001962    ** reduce the amount of memory held by a prepared statement.
001963    */
001964    do {
001965      x.nNeeded = 0;
001966      p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
001967      p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
001968      p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
001969      p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
001970  #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
001971      p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
001972  #endif
001973      if( x.nNeeded==0 ) break;
001974      x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
001975      x.nFree = x.nNeeded;
001976    }while( !db->mallocFailed );
001977  
001978    p->pVList = pParse->pVList;
001979    pParse->pVList =  0;
001980    p->explain = pParse->explain;
001981    if( db->mallocFailed ){
001982      p->nVar = 0;
001983      p->nCursor = 0;
001984      p->nMem = 0;
001985    }else{
001986      p->nCursor = nCursor;
001987      p->nVar = (ynVar)nVar;
001988      initMemArray(p->aVar, nVar, db, MEM_Null);
001989      p->nMem = nMem;
001990      initMemArray(p->aMem, nMem, db, MEM_Undefined);
001991      memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
001992  #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
001993      memset(p->anExec, 0, p->nOp*sizeof(i64));
001994  #endif
001995    }
001996    sqlite3VdbeRewind(p);
001997  }
001998  
001999  /*
002000  ** Close a VDBE cursor and release all the resources that cursor 
002001  ** happens to hold.
002002  */
002003  void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
002004    if( pCx==0 ){
002005      return;
002006    }
002007    assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE );
002008    switch( pCx->eCurType ){
002009      case CURTYPE_SORTER: {
002010        sqlite3VdbeSorterClose(p->db, pCx);
002011        break;
002012      }
002013      case CURTYPE_BTREE: {
002014        if( pCx->pBtx ){
002015          sqlite3BtreeClose(pCx->pBtx);
002016          /* The pCx->pCursor will be close automatically, if it exists, by
002017          ** the call above. */
002018        }else{
002019          assert( pCx->uc.pCursor!=0 );
002020          sqlite3BtreeCloseCursor(pCx->uc.pCursor);
002021        }
002022        break;
002023      }
002024  #ifndef SQLITE_OMIT_VIRTUALTABLE
002025      case CURTYPE_VTAB: {
002026        sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
002027        const sqlite3_module *pModule = pVCur->pVtab->pModule;
002028        assert( pVCur->pVtab->nRef>0 );
002029        pVCur->pVtab->nRef--;
002030        pModule->xClose(pVCur);
002031        break;
002032      }
002033  #endif
002034    }
002035  }
002036  
002037  /*
002038  ** Close all cursors in the current frame.
002039  */
002040  static void closeCursorsInFrame(Vdbe *p){
002041    if( p->apCsr ){
002042      int i;
002043      for(i=0; i<p->nCursor; i++){
002044        VdbeCursor *pC = p->apCsr[i];
002045        if( pC ){
002046          sqlite3VdbeFreeCursor(p, pC);
002047          p->apCsr[i] = 0;
002048        }
002049      }
002050    }
002051  }
002052  
002053  /*
002054  ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
002055  ** is used, for example, when a trigger sub-program is halted to restore
002056  ** control to the main program.
002057  */
002058  int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
002059    Vdbe *v = pFrame->v;
002060    closeCursorsInFrame(v);
002061  #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
002062    v->anExec = pFrame->anExec;
002063  #endif
002064    v->aOp = pFrame->aOp;
002065    v->nOp = pFrame->nOp;
002066    v->aMem = pFrame->aMem;
002067    v->nMem = pFrame->nMem;
002068    v->apCsr = pFrame->apCsr;
002069    v->nCursor = pFrame->nCursor;
002070    v->db->lastRowid = pFrame->lastRowid;
002071    v->nChange = pFrame->nChange;
002072    v->db->nChange = pFrame->nDbChange;
002073    sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
002074    v->pAuxData = pFrame->pAuxData;
002075    pFrame->pAuxData = 0;
002076    return pFrame->pc;
002077  }
002078  
002079  /*
002080  ** Close all cursors.
002081  **
002082  ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory 
002083  ** cell array. This is necessary as the memory cell array may contain
002084  ** pointers to VdbeFrame objects, which may in turn contain pointers to
002085  ** open cursors.
002086  */
002087  static void closeAllCursors(Vdbe *p){
002088    if( p->pFrame ){
002089      VdbeFrame *pFrame;
002090      for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
002091      sqlite3VdbeFrameRestore(pFrame);
002092      p->pFrame = 0;
002093      p->nFrame = 0;
002094    }
002095    assert( p->nFrame==0 );
002096    closeCursorsInFrame(p);
002097    if( p->aMem ){
002098      releaseMemArray(p->aMem, p->nMem);
002099    }
002100    while( p->pDelFrame ){
002101      VdbeFrame *pDel = p->pDelFrame;
002102      p->pDelFrame = pDel->pParent;
002103      sqlite3VdbeFrameDelete(pDel);
002104    }
002105  
002106    /* Delete any auxdata allocations made by the VM */
002107    if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
002108    assert( p->pAuxData==0 );
002109  }
002110  
002111  /*
002112  ** Clean up the VM after a single run.
002113  */
002114  static void Cleanup(Vdbe *p){
002115    sqlite3 *db = p->db;
002116  
002117  #ifdef SQLITE_DEBUG
002118    /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
002119    ** Vdbe.aMem[] arrays have already been cleaned up.  */
002120    int i;
002121    if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
002122    if( p->aMem ){
002123      for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
002124    }
002125  #endif
002126  
002127    sqlite3DbFree(db, p->zErrMsg);
002128    p->zErrMsg = 0;
002129    p->pResultSet = 0;
002130  }
002131  
002132  /*
002133  ** Set the number of result columns that will be returned by this SQL
002134  ** statement. This is now set at compile time, rather than during
002135  ** execution of the vdbe program so that sqlite3_column_count() can
002136  ** be called on an SQL statement before sqlite3_step().
002137  */
002138  void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
002139    Mem *pColName;
002140    int n;
002141    sqlite3 *db = p->db;
002142  
002143    releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
002144    sqlite3DbFree(db, p->aColName);
002145    n = nResColumn*COLNAME_N;
002146    p->nResColumn = (u16)nResColumn;
002147    p->aColName = pColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
002148    if( p->aColName==0 ) return;
002149    initMemArray(p->aColName, n, p->db, MEM_Null);
002150  }
002151  
002152  /*
002153  ** Set the name of the idx'th column to be returned by the SQL statement.
002154  ** zName must be a pointer to a nul terminated string.
002155  **
002156  ** This call must be made after a call to sqlite3VdbeSetNumCols().
002157  **
002158  ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
002159  ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
002160  ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
002161  */
002162  int sqlite3VdbeSetColName(
002163    Vdbe *p,                         /* Vdbe being configured */
002164    int idx,                         /* Index of column zName applies to */
002165    int var,                         /* One of the COLNAME_* constants */
002166    const char *zName,               /* Pointer to buffer containing name */
002167    void (*xDel)(void*)              /* Memory management strategy for zName */
002168  ){
002169    int rc;
002170    Mem *pColName;
002171    assert( idx<p->nResColumn );
002172    assert( var<COLNAME_N );
002173    if( p->db->mallocFailed ){
002174      assert( !zName || xDel!=SQLITE_DYNAMIC );
002175      return SQLITE_NOMEM_BKPT;
002176    }
002177    assert( p->aColName!=0 );
002178    pColName = &(p->aColName[idx+var*p->nResColumn]);
002179    rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
002180    assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
002181    return rc;
002182  }
002183  
002184  /*
002185  ** A read or write transaction may or may not be active on database handle
002186  ** db. If a transaction is active, commit it. If there is a
002187  ** write-transaction spanning more than one database file, this routine
002188  ** takes care of the master journal trickery.
002189  */
002190  static int vdbeCommit(sqlite3 *db, Vdbe *p){
002191    int i;
002192    int nTrans = 0;  /* Number of databases with an active write-transaction
002193                     ** that are candidates for a two-phase commit using a
002194                     ** master-journal */
002195    int rc = SQLITE_OK;
002196    int needXcommit = 0;
002197  
002198  #ifdef SQLITE_OMIT_VIRTUALTABLE
002199    /* With this option, sqlite3VtabSync() is defined to be simply 
002200    ** SQLITE_OK so p is not used. 
002201    */
002202    UNUSED_PARAMETER(p);
002203  #endif
002204  
002205    /* Before doing anything else, call the xSync() callback for any
002206    ** virtual module tables written in this transaction. This has to
002207    ** be done before determining whether a master journal file is 
002208    ** required, as an xSync() callback may add an attached database
002209    ** to the transaction.
002210    */
002211    rc = sqlite3VtabSync(db, p);
002212  
002213    /* This loop determines (a) if the commit hook should be invoked and
002214    ** (b) how many database files have open write transactions, not 
002215    ** including the temp database. (b) is important because if more than 
002216    ** one database file has an open write transaction, a master journal
002217    ** file is required for an atomic commit.
002218    */ 
002219    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
002220      Btree *pBt = db->aDb[i].pBt;
002221      if( sqlite3BtreeIsInTrans(pBt) ){
002222        /* Whether or not a database might need a master journal depends upon
002223        ** its journal mode (among other things).  This matrix determines which
002224        ** journal modes use a master journal and which do not */
002225        static const u8 aMJNeeded[] = {
002226          /* DELETE   */  1,
002227          /* PERSIST   */ 1,
002228          /* OFF       */ 0,
002229          /* TRUNCATE  */ 1,
002230          /* MEMORY    */ 0,
002231          /* WAL       */ 0
002232        };
002233        Pager *pPager;   /* Pager associated with pBt */
002234        needXcommit = 1;
002235        sqlite3BtreeEnter(pBt);
002236        pPager = sqlite3BtreePager(pBt);
002237        if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
002238         && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
002239        ){ 
002240          assert( i!=1 );
002241          nTrans++;
002242        }
002243        rc = sqlite3PagerExclusiveLock(pPager);
002244        sqlite3BtreeLeave(pBt);
002245      }
002246    }
002247    if( rc!=SQLITE_OK ){
002248      return rc;
002249    }
002250  
002251    /* If there are any write-transactions at all, invoke the commit hook */
002252    if( needXcommit && db->xCommitCallback ){
002253      rc = db->xCommitCallback(db->pCommitArg);
002254      if( rc ){
002255        return SQLITE_CONSTRAINT_COMMITHOOK;
002256      }
002257    }
002258  
002259    /* The simple case - no more than one database file (not counting the
002260    ** TEMP database) has a transaction active.   There is no need for the
002261    ** master-journal.
002262    **
002263    ** If the return value of sqlite3BtreeGetFilename() is a zero length
002264    ** string, it means the main database is :memory: or a temp file.  In 
002265    ** that case we do not support atomic multi-file commits, so use the 
002266    ** simple case then too.
002267    */
002268    if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
002269     || nTrans<=1
002270    ){
002271      for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
002272        Btree *pBt = db->aDb[i].pBt;
002273        if( pBt ){
002274          rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
002275        }
002276      }
002277  
002278      /* Do the commit only if all databases successfully complete phase 1. 
002279      ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
002280      ** IO error while deleting or truncating a journal file. It is unlikely,
002281      ** but could happen. In this case abandon processing and return the error.
002282      */
002283      for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
002284        Btree *pBt = db->aDb[i].pBt;
002285        if( pBt ){
002286          rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
002287        }
002288      }
002289      if( rc==SQLITE_OK ){
002290        sqlite3VtabCommit(db);
002291      }
002292    }
002293  
002294    /* The complex case - There is a multi-file write-transaction active.
002295    ** This requires a master journal file to ensure the transaction is
002296    ** committed atomically.
002297    */
002298  #ifndef SQLITE_OMIT_DISKIO
002299    else{
002300      sqlite3_vfs *pVfs = db->pVfs;
002301      char *zMaster = 0;   /* File-name for the master journal */
002302      char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
002303      sqlite3_file *pMaster = 0;
002304      i64 offset = 0;
002305      int res;
002306      int retryCount = 0;
002307      int nMainFile;
002308  
002309      /* Select a master journal file name */
002310      nMainFile = sqlite3Strlen30(zMainFile);
002311      zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
002312      if( zMaster==0 ) return SQLITE_NOMEM_BKPT;
002313      do {
002314        u32 iRandom;
002315        if( retryCount ){
002316          if( retryCount>100 ){
002317            sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
002318            sqlite3OsDelete(pVfs, zMaster, 0);
002319            break;
002320          }else if( retryCount==1 ){
002321            sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
002322          }
002323        }
002324        retryCount++;
002325        sqlite3_randomness(sizeof(iRandom), &iRandom);
002326        sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
002327                                 (iRandom>>8)&0xffffff, iRandom&0xff);
002328        /* The antipenultimate character of the master journal name must
002329        ** be "9" to avoid name collisions when using 8+3 filenames. */
002330        assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
002331        sqlite3FileSuffix3(zMainFile, zMaster);
002332        rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
002333      }while( rc==SQLITE_OK && res );
002334      if( rc==SQLITE_OK ){
002335        /* Open the master journal. */
002336        rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, 
002337            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
002338            SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
002339        );
002340      }
002341      if( rc!=SQLITE_OK ){
002342        sqlite3DbFree(db, zMaster);
002343        return rc;
002344      }
002345   
002346      /* Write the name of each database file in the transaction into the new
002347      ** master journal file. If an error occurs at this point close
002348      ** and delete the master journal file. All the individual journal files
002349      ** still have 'null' as the master journal pointer, so they will roll
002350      ** back independently if a failure occurs.
002351      */
002352      for(i=0; i<db->nDb; i++){
002353        Btree *pBt = db->aDb[i].pBt;
002354        if( sqlite3BtreeIsInTrans(pBt) ){
002355          char const *zFile = sqlite3BtreeGetJournalname(pBt);
002356          if( zFile==0 ){
002357            continue;  /* Ignore TEMP and :memory: databases */
002358          }
002359          assert( zFile[0]!=0 );
002360          rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
002361          offset += sqlite3Strlen30(zFile)+1;
002362          if( rc!=SQLITE_OK ){
002363            sqlite3OsCloseFree(pMaster);
002364            sqlite3OsDelete(pVfs, zMaster, 0);
002365            sqlite3DbFree(db, zMaster);
002366            return rc;
002367          }
002368        }
002369      }
002370  
002371      /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
002372      ** flag is set this is not required.
002373      */
002374      if( 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
002375       && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
002376      ){
002377        sqlite3OsCloseFree(pMaster);
002378        sqlite3OsDelete(pVfs, zMaster, 0);
002379        sqlite3DbFree(db, zMaster);
002380        return rc;
002381      }
002382  
002383      /* Sync all the db files involved in the transaction. The same call
002384      ** sets the master journal pointer in each individual journal. If
002385      ** an error occurs here, do not delete the master journal file.
002386      **
002387      ** If the error occurs during the first call to
002388      ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
002389      ** master journal file will be orphaned. But we cannot delete it,
002390      ** in case the master journal file name was written into the journal
002391      ** file before the failure occurred.
002392      */
002393      for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
002394        Btree *pBt = db->aDb[i].pBt;
002395        if( pBt ){
002396          rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
002397        }
002398      }
002399      sqlite3OsCloseFree(pMaster);
002400      assert( rc!=SQLITE_BUSY );
002401      if( rc!=SQLITE_OK ){
002402        sqlite3DbFree(db, zMaster);
002403        return rc;
002404      }
002405  
002406      /* Delete the master journal file. This commits the transaction. After
002407      ** doing this the directory is synced again before any individual
002408      ** transaction files are deleted.
002409      */
002410      rc = sqlite3OsDelete(pVfs, zMaster, 1);
002411      sqlite3DbFree(db, zMaster);
002412      zMaster = 0;
002413      if( rc ){
002414        return rc;
002415      }
002416  
002417      /* All files and directories have already been synced, so the following
002418      ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
002419      ** deleting or truncating journals. If something goes wrong while
002420      ** this is happening we don't really care. The integrity of the
002421      ** transaction is already guaranteed, but some stray 'cold' journals
002422      ** may be lying around. Returning an error code won't help matters.
002423      */
002424      disable_simulated_io_errors();
002425      sqlite3BeginBenignMalloc();
002426      for(i=0; i<db->nDb; i++){ 
002427        Btree *pBt = db->aDb[i].pBt;
002428        if( pBt ){
002429          sqlite3BtreeCommitPhaseTwo(pBt, 1);
002430        }
002431      }
002432      sqlite3EndBenignMalloc();
002433      enable_simulated_io_errors();
002434  
002435      sqlite3VtabCommit(db);
002436    }
002437  #endif
002438  
002439    return rc;
002440  }
002441  
002442  /* 
002443  ** This routine checks that the sqlite3.nVdbeActive count variable
002444  ** matches the number of vdbe's in the list sqlite3.pVdbe that are
002445  ** currently active. An assertion fails if the two counts do not match.
002446  ** This is an internal self-check only - it is not an essential processing
002447  ** step.
002448  **
002449  ** This is a no-op if NDEBUG is defined.
002450  */
002451  #ifndef NDEBUG
002452  static void checkActiveVdbeCnt(sqlite3 *db){
002453    Vdbe *p;
002454    int cnt = 0;
002455    int nWrite = 0;
002456    int nRead = 0;
002457    p = db->pVdbe;
002458    while( p ){
002459      if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
002460        cnt++;
002461        if( p->readOnly==0 ) nWrite++;
002462        if( p->bIsReader ) nRead++;
002463      }
002464      p = p->pNext;
002465    }
002466    assert( cnt==db->nVdbeActive );
002467    assert( nWrite==db->nVdbeWrite );
002468    assert( nRead==db->nVdbeRead );
002469  }
002470  #else
002471  #define checkActiveVdbeCnt(x)
002472  #endif
002473  
002474  /*
002475  ** If the Vdbe passed as the first argument opened a statement-transaction,
002476  ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
002477  ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
002478  ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the 
002479  ** statement transaction is committed.
002480  **
002481  ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. 
002482  ** Otherwise SQLITE_OK.
002483  */
002484  int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
002485    sqlite3 *const db = p->db;
002486    int rc = SQLITE_OK;
002487  
002488    /* If p->iStatement is greater than zero, then this Vdbe opened a 
002489    ** statement transaction that should be closed here. The only exception
002490    ** is that an IO error may have occurred, causing an emergency rollback.
002491    ** In this case (db->nStatement==0), and there is nothing to do.
002492    */
002493    if( db->nStatement && p->iStatement ){
002494      int i;
002495      const int iSavepoint = p->iStatement-1;
002496  
002497      assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
002498      assert( db->nStatement>0 );
002499      assert( p->iStatement==(db->nStatement+db->nSavepoint) );
002500  
002501      for(i=0; i<db->nDb; i++){ 
002502        int rc2 = SQLITE_OK;
002503        Btree *pBt = db->aDb[i].pBt;
002504        if( pBt ){
002505          if( eOp==SAVEPOINT_ROLLBACK ){
002506            rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
002507          }
002508          if( rc2==SQLITE_OK ){
002509            rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
002510          }
002511          if( rc==SQLITE_OK ){
002512            rc = rc2;
002513          }
002514        }
002515      }
002516      db->nStatement--;
002517      p->iStatement = 0;
002518  
002519      if( rc==SQLITE_OK ){
002520        if( eOp==SAVEPOINT_ROLLBACK ){
002521          rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
002522        }
002523        if( rc==SQLITE_OK ){
002524          rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
002525        }
002526      }
002527  
002528      /* If the statement transaction is being rolled back, also restore the 
002529      ** database handles deferred constraint counter to the value it had when 
002530      ** the statement transaction was opened.  */
002531      if( eOp==SAVEPOINT_ROLLBACK ){
002532        db->nDeferredCons = p->nStmtDefCons;
002533        db->nDeferredImmCons = p->nStmtDefImmCons;
002534      }
002535    }
002536    return rc;
002537  }
002538  
002539  /*
002540  ** This function is called when a transaction opened by the database 
002541  ** handle associated with the VM passed as an argument is about to be 
002542  ** committed. If there are outstanding deferred foreign key constraint
002543  ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
002544  **
002545  ** If there are outstanding FK violations and this function returns 
002546  ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
002547  ** and write an error message to it. Then return SQLITE_ERROR.
002548  */
002549  #ifndef SQLITE_OMIT_FOREIGN_KEY
002550  int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
002551    sqlite3 *db = p->db;
002552    if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0) 
002553     || (!deferred && p->nFkConstraint>0) 
002554    ){
002555      p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
002556      p->errorAction = OE_Abort;
002557      sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
002558      return SQLITE_ERROR;
002559    }
002560    return SQLITE_OK;
002561  }
002562  #endif
002563  
002564  /*
002565  ** This routine is called the when a VDBE tries to halt.  If the VDBE
002566  ** has made changes and is in autocommit mode, then commit those
002567  ** changes.  If a rollback is needed, then do the rollback.
002568  **
002569  ** This routine is the only way to move the state of a VM from
002570  ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
002571  ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
002572  **
002573  ** Return an error code.  If the commit could not complete because of
002574  ** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
002575  ** means the close did not happen and needs to be repeated.
002576  */
002577  int sqlite3VdbeHalt(Vdbe *p){
002578    int rc;                         /* Used to store transient return codes */
002579    sqlite3 *db = p->db;
002580  
002581    /* This function contains the logic that determines if a statement or
002582    ** transaction will be committed or rolled back as a result of the
002583    ** execution of this virtual machine. 
002584    **
002585    ** If any of the following errors occur:
002586    **
002587    **     SQLITE_NOMEM
002588    **     SQLITE_IOERR
002589    **     SQLITE_FULL
002590    **     SQLITE_INTERRUPT
002591    **
002592    ** Then the internal cache might have been left in an inconsistent
002593    ** state.  We need to rollback the statement transaction, if there is
002594    ** one, or the complete transaction if there is no statement transaction.
002595    */
002596  
002597    if( db->mallocFailed ){
002598      p->rc = SQLITE_NOMEM_BKPT;
002599    }
002600    closeAllCursors(p);
002601    if( p->magic!=VDBE_MAGIC_RUN ){
002602      return SQLITE_OK;
002603    }
002604    checkActiveVdbeCnt(db);
002605  
002606    /* No commit or rollback needed if the program never started or if the
002607    ** SQL statement does not read or write a database file.  */
002608    if( p->pc>=0 && p->bIsReader ){
002609      int mrc;   /* Primary error code from p->rc */
002610      int eStatementOp = 0;
002611      int isSpecialError;            /* Set to true if a 'special' error */
002612  
002613      /* Lock all btrees used by the statement */
002614      sqlite3VdbeEnter(p);
002615  
002616      /* Check for one of the special errors */
002617      mrc = p->rc & 0xff;
002618      isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
002619                       || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
002620      if( isSpecialError ){
002621        /* If the query was read-only and the error code is SQLITE_INTERRUPT, 
002622        ** no rollback is necessary. Otherwise, at least a savepoint 
002623        ** transaction must be rolled back to restore the database to a 
002624        ** consistent state.
002625        **
002626        ** Even if the statement is read-only, it is important to perform
002627        ** a statement or transaction rollback operation. If the error 
002628        ** occurred while writing to the journal, sub-journal or database
002629        ** file as part of an effort to free up cache space (see function
002630        ** pagerStress() in pager.c), the rollback is required to restore 
002631        ** the pager to a consistent state.
002632        */
002633        if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
002634          if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
002635            eStatementOp = SAVEPOINT_ROLLBACK;
002636          }else{
002637            /* We are forced to roll back the active transaction. Before doing
002638            ** so, abort any other statements this handle currently has active.
002639            */
002640            sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
002641            sqlite3CloseSavepoints(db);
002642            db->autoCommit = 1;
002643            p->nChange = 0;
002644          }
002645        }
002646      }
002647  
002648      /* Check for immediate foreign key violations. */
002649      if( p->rc==SQLITE_OK ){
002650        sqlite3VdbeCheckFk(p, 0);
002651      }
002652    
002653      /* If the auto-commit flag is set and this is the only active writer 
002654      ** VM, then we do either a commit or rollback of the current transaction. 
002655      **
002656      ** Note: This block also runs if one of the special errors handled 
002657      ** above has occurred. 
002658      */
002659      if( !sqlite3VtabInSync(db) 
002660       && db->autoCommit 
002661       && db->nVdbeWrite==(p->readOnly==0) 
002662      ){
002663        if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
002664          rc = sqlite3VdbeCheckFk(p, 1);
002665          if( rc!=SQLITE_OK ){
002666            if( NEVER(p->readOnly) ){
002667              sqlite3VdbeLeave(p);
002668              return SQLITE_ERROR;
002669            }
002670            rc = SQLITE_CONSTRAINT_FOREIGNKEY;
002671          }else{ 
002672            /* The auto-commit flag is true, the vdbe program was successful 
002673            ** or hit an 'OR FAIL' constraint and there are no deferred foreign
002674            ** key constraints to hold up the transaction. This means a commit 
002675            ** is required. */
002676            rc = vdbeCommit(db, p);
002677          }
002678          if( rc==SQLITE_BUSY && p->readOnly ){
002679            sqlite3VdbeLeave(p);
002680            return SQLITE_BUSY;
002681          }else if( rc!=SQLITE_OK ){
002682            p->rc = rc;
002683            sqlite3RollbackAll(db, SQLITE_OK);
002684            p->nChange = 0;
002685          }else{
002686            db->nDeferredCons = 0;
002687            db->nDeferredImmCons = 0;
002688            db->flags &= ~SQLITE_DeferFKs;
002689            sqlite3CommitInternalChanges(db);
002690          }
002691        }else{
002692          sqlite3RollbackAll(db, SQLITE_OK);
002693          p->nChange = 0;
002694        }
002695        db->nStatement = 0;
002696      }else if( eStatementOp==0 ){
002697        if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
002698          eStatementOp = SAVEPOINT_RELEASE;
002699        }else if( p->errorAction==OE_Abort ){
002700          eStatementOp = SAVEPOINT_ROLLBACK;
002701        }else{
002702          sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
002703          sqlite3CloseSavepoints(db);
002704          db->autoCommit = 1;
002705          p->nChange = 0;
002706        }
002707      }
002708    
002709      /* If eStatementOp is non-zero, then a statement transaction needs to
002710      ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
002711      ** do so. If this operation returns an error, and the current statement
002712      ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
002713      ** current statement error code.
002714      */
002715      if( eStatementOp ){
002716        rc = sqlite3VdbeCloseStatement(p, eStatementOp);
002717        if( rc ){
002718          if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
002719            p->rc = rc;
002720            sqlite3DbFree(db, p->zErrMsg);
002721            p->zErrMsg = 0;
002722          }
002723          sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
002724          sqlite3CloseSavepoints(db);
002725          db->autoCommit = 1;
002726          p->nChange = 0;
002727        }
002728      }
002729    
002730      /* If this was an INSERT, UPDATE or DELETE and no statement transaction
002731      ** has been rolled back, update the database connection change-counter. 
002732      */
002733      if( p->changeCntOn ){
002734        if( eStatementOp!=SAVEPOINT_ROLLBACK ){
002735          sqlite3VdbeSetChanges(db, p->nChange);
002736        }else{
002737          sqlite3VdbeSetChanges(db, 0);
002738        }
002739        p->nChange = 0;
002740      }
002741  
002742      /* Release the locks */
002743      sqlite3VdbeLeave(p);
002744    }
002745  
002746    /* We have successfully halted and closed the VM.  Record this fact. */
002747    if( p->pc>=0 ){
002748      db->nVdbeActive--;
002749      if( !p->readOnly ) db->nVdbeWrite--;
002750      if( p->bIsReader ) db->nVdbeRead--;
002751      assert( db->nVdbeActive>=db->nVdbeRead );
002752      assert( db->nVdbeRead>=db->nVdbeWrite );
002753      assert( db->nVdbeWrite>=0 );
002754    }
002755    p->magic = VDBE_MAGIC_HALT;
002756    checkActiveVdbeCnt(db);
002757    if( db->mallocFailed ){
002758      p->rc = SQLITE_NOMEM_BKPT;
002759    }
002760  
002761    /* If the auto-commit flag is set to true, then any locks that were held
002762    ** by connection db have now been released. Call sqlite3ConnectionUnlocked() 
002763    ** to invoke any required unlock-notify callbacks.
002764    */
002765    if( db->autoCommit ){
002766      sqlite3ConnectionUnlocked(db);
002767    }
002768  
002769    assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
002770    return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
002771  }
002772  
002773  
002774  /*
002775  ** Each VDBE holds the result of the most recent sqlite3_step() call
002776  ** in p->rc.  This routine sets that result back to SQLITE_OK.
002777  */
002778  void sqlite3VdbeResetStepResult(Vdbe *p){
002779    p->rc = SQLITE_OK;
002780  }
002781  
002782  /*
002783  ** Copy the error code and error message belonging to the VDBE passed
002784  ** as the first argument to its database handle (so that they will be 
002785  ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
002786  **
002787  ** This function does not clear the VDBE error code or message, just
002788  ** copies them to the database handle.
002789  */
002790  int sqlite3VdbeTransferError(Vdbe *p){
002791    sqlite3 *db = p->db;
002792    int rc = p->rc;
002793    if( p->zErrMsg ){
002794      db->bBenignMalloc++;
002795      sqlite3BeginBenignMalloc();
002796      if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
002797      sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
002798      sqlite3EndBenignMalloc();
002799      db->bBenignMalloc--;
002800      db->errCode = rc;
002801    }else{
002802      sqlite3Error(db, rc);
002803    }
002804    return rc;
002805  }
002806  
002807  #ifdef SQLITE_ENABLE_SQLLOG
002808  /*
002809  ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, 
002810  ** invoke it.
002811  */
002812  static void vdbeInvokeSqllog(Vdbe *v){
002813    if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
002814      char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
002815      assert( v->db->init.busy==0 );
002816      if( zExpanded ){
002817        sqlite3GlobalConfig.xSqllog(
002818            sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
002819        );
002820        sqlite3DbFree(v->db, zExpanded);
002821      }
002822    }
002823  }
002824  #else
002825  # define vdbeInvokeSqllog(x)
002826  #endif
002827  
002828  /*
002829  ** Clean up a VDBE after execution but do not delete the VDBE just yet.
002830  ** Write any error messages into *pzErrMsg.  Return the result code.
002831  **
002832  ** After this routine is run, the VDBE should be ready to be executed
002833  ** again.
002834  **
002835  ** To look at it another way, this routine resets the state of the
002836  ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
002837  ** VDBE_MAGIC_INIT.
002838  */
002839  int sqlite3VdbeReset(Vdbe *p){
002840    sqlite3 *db;
002841    db = p->db;
002842  
002843    /* If the VM did not run to completion or if it encountered an
002844    ** error, then it might not have been halted properly.  So halt
002845    ** it now.
002846    */
002847    sqlite3VdbeHalt(p);
002848  
002849    /* If the VDBE has be run even partially, then transfer the error code
002850    ** and error message from the VDBE into the main database structure.  But
002851    ** if the VDBE has just been set to run but has not actually executed any
002852    ** instructions yet, leave the main database error information unchanged.
002853    */
002854    if( p->pc>=0 ){
002855      vdbeInvokeSqllog(p);
002856      sqlite3VdbeTransferError(p);
002857      sqlite3DbFree(db, p->zErrMsg);
002858      p->zErrMsg = 0;
002859      if( p->runOnlyOnce ) p->expired = 1;
002860    }else if( p->rc && p->expired ){
002861      /* The expired flag was set on the VDBE before the first call
002862      ** to sqlite3_step(). For consistency (since sqlite3_step() was
002863      ** called), set the database error in this case as well.
002864      */
002865      sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
002866      sqlite3DbFree(db, p->zErrMsg);
002867      p->zErrMsg = 0;
002868    }
002869  
002870    /* Reclaim all memory used by the VDBE
002871    */
002872    Cleanup(p);
002873  
002874    /* Save profiling information from this VDBE run.
002875    */
002876  #ifdef VDBE_PROFILE
002877    {
002878      FILE *out = fopen("vdbe_profile.out", "a");
002879      if( out ){
002880        int i;
002881        fprintf(out, "---- ");
002882        for(i=0; i<p->nOp; i++){
002883          fprintf(out, "%02x", p->aOp[i].opcode);
002884        }
002885        fprintf(out, "\n");
002886        if( p->zSql ){
002887          char c, pc = 0;
002888          fprintf(out, "-- ");
002889          for(i=0; (c = p->zSql[i])!=0; i++){
002890            if( pc=='\n' ) fprintf(out, "-- ");
002891            putc(c, out);
002892            pc = c;
002893          }
002894          if( pc!='\n' ) fprintf(out, "\n");
002895        }
002896        for(i=0; i<p->nOp; i++){
002897          char zHdr[100];
002898          sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
002899             p->aOp[i].cnt,
002900             p->aOp[i].cycles,
002901             p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
002902          );
002903          fprintf(out, "%s", zHdr);
002904          sqlite3VdbePrintOp(out, i, &p->aOp[i]);
002905        }
002906        fclose(out);
002907      }
002908    }
002909  #endif
002910    p->iCurrentTime = 0;
002911    p->magic = VDBE_MAGIC_RESET;
002912    return p->rc & db->errMask;
002913  }
002914   
002915  /*
002916  ** Clean up and delete a VDBE after execution.  Return an integer which is
002917  ** the result code.  Write any error message text into *pzErrMsg.
002918  */
002919  int sqlite3VdbeFinalize(Vdbe *p){
002920    int rc = SQLITE_OK;
002921    if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
002922      rc = sqlite3VdbeReset(p);
002923      assert( (rc & p->db->errMask)==rc );
002924    }
002925    sqlite3VdbeDelete(p);
002926    return rc;
002927  }
002928  
002929  /*
002930  ** If parameter iOp is less than zero, then invoke the destructor for
002931  ** all auxiliary data pointers currently cached by the VM passed as
002932  ** the first argument.
002933  **
002934  ** Or, if iOp is greater than or equal to zero, then the destructor is
002935  ** only invoked for those auxiliary data pointers created by the user 
002936  ** function invoked by the OP_Function opcode at instruction iOp of 
002937  ** VM pVdbe, and only then if:
002938  **
002939  **    * the associated function parameter is the 32nd or later (counting
002940  **      from left to right), or
002941  **
002942  **    * the corresponding bit in argument mask is clear (where the first
002943  **      function parameter corresponds to bit 0 etc.).
002944  */
002945  void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
002946    while( *pp ){
002947      AuxData *pAux = *pp;
002948      if( (iOp<0)
002949       || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
002950      ){
002951        testcase( pAux->iArg==31 );
002952        if( pAux->xDelete ){
002953          pAux->xDelete(pAux->pAux);
002954        }
002955        *pp = pAux->pNext;
002956        sqlite3DbFree(db, pAux);
002957      }else{
002958        pp= &pAux->pNext;
002959      }
002960    }
002961  }
002962  
002963  /*
002964  ** Free all memory associated with the Vdbe passed as the second argument,
002965  ** except for object itself, which is preserved.
002966  **
002967  ** The difference between this function and sqlite3VdbeDelete() is that
002968  ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
002969  ** the database connection and frees the object itself.
002970  */
002971  void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
002972    SubProgram *pSub, *pNext;
002973    assert( p->db==0 || p->db==db );
002974    releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
002975    for(pSub=p->pProgram; pSub; pSub=pNext){
002976      pNext = pSub->pNext;
002977      vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
002978      sqlite3DbFree(db, pSub);
002979    }
002980    if( p->magic!=VDBE_MAGIC_INIT ){
002981      releaseMemArray(p->aVar, p->nVar);
002982      sqlite3DbFree(db, p->pVList);
002983      sqlite3DbFree(db, p->pFree);
002984    }
002985    vdbeFreeOpArray(db, p->aOp, p->nOp);
002986    sqlite3DbFree(db, p->aColName);
002987    sqlite3DbFree(db, p->zSql);
002988  #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
002989    {
002990      int i;
002991      for(i=0; i<p->nScan; i++){
002992        sqlite3DbFree(db, p->aScan[i].zName);
002993      }
002994      sqlite3DbFree(db, p->aScan);
002995    }
002996  #endif
002997  }
002998  
002999  /*
003000  ** Delete an entire VDBE.
003001  */
003002  void sqlite3VdbeDelete(Vdbe *p){
003003    sqlite3 *db;
003004  
003005    if( NEVER(p==0) ) return;
003006    db = p->db;
003007    assert( sqlite3_mutex_held(db->mutex) );
003008    sqlite3VdbeClearObject(db, p);
003009    if( p->pPrev ){
003010      p->pPrev->pNext = p->pNext;
003011    }else{
003012      assert( db->pVdbe==p );
003013      db->pVdbe = p->pNext;
003014    }
003015    if( p->pNext ){
003016      p->pNext->pPrev = p->pPrev;
003017    }
003018    p->magic = VDBE_MAGIC_DEAD;
003019    p->db = 0;
003020    sqlite3DbFree(db, p);
003021  }
003022  
003023  /*
003024  ** The cursor "p" has a pending seek operation that has not yet been
003025  ** carried out.  Seek the cursor now.  If an error occurs, return
003026  ** the appropriate error code.
003027  */
003028  static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){
003029    int res, rc;
003030  #ifdef SQLITE_TEST
003031    extern int sqlite3_search_count;
003032  #endif
003033    assert( p->deferredMoveto );
003034    assert( p->isTable );
003035    assert( p->eCurType==CURTYPE_BTREE );
003036    rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res);
003037    if( rc ) return rc;
003038    if( res!=0 ) return SQLITE_CORRUPT_BKPT;
003039  #ifdef SQLITE_TEST
003040    sqlite3_search_count++;
003041  #endif
003042    p->deferredMoveto = 0;
003043    p->cacheStatus = CACHE_STALE;
003044    return SQLITE_OK;
003045  }
003046  
003047  /*
003048  ** Something has moved cursor "p" out of place.  Maybe the row it was
003049  ** pointed to was deleted out from under it.  Or maybe the btree was
003050  ** rebalanced.  Whatever the cause, try to restore "p" to the place it
003051  ** is supposed to be pointing.  If the row was deleted out from under the
003052  ** cursor, set the cursor to point to a NULL row.
003053  */
003054  static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
003055    int isDifferentRow, rc;
003056    assert( p->eCurType==CURTYPE_BTREE );
003057    assert( p->uc.pCursor!=0 );
003058    assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
003059    rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
003060    p->cacheStatus = CACHE_STALE;
003061    if( isDifferentRow ) p->nullRow = 1;
003062    return rc;
003063  }
003064  
003065  /*
003066  ** Check to ensure that the cursor is valid.  Restore the cursor
003067  ** if need be.  Return any I/O error from the restore operation.
003068  */
003069  int sqlite3VdbeCursorRestore(VdbeCursor *p){
003070    assert( p->eCurType==CURTYPE_BTREE );
003071    if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
003072      return handleMovedCursor(p);
003073    }
003074    return SQLITE_OK;
003075  }
003076  
003077  /*
003078  ** Make sure the cursor p is ready to read or write the row to which it
003079  ** was last positioned.  Return an error code if an OOM fault or I/O error
003080  ** prevents us from positioning the cursor to its correct position.
003081  **
003082  ** If a MoveTo operation is pending on the given cursor, then do that
003083  ** MoveTo now.  If no move is pending, check to see if the row has been
003084  ** deleted out from under the cursor and if it has, mark the row as
003085  ** a NULL row.
003086  **
003087  ** If the cursor is already pointing to the correct row and that row has
003088  ** not been deleted out from under the cursor, then this routine is a no-op.
003089  */
003090  int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){
003091    VdbeCursor *p = *pp;
003092    if( p->eCurType==CURTYPE_BTREE ){
003093      if( p->deferredMoveto ){
003094        int iMap;
003095        if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){
003096          *pp = p->pAltCursor;
003097          *piCol = iMap - 1;
003098          return SQLITE_OK;
003099        }
003100        return handleDeferredMoveto(p);
003101      }
003102      if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
003103        return handleMovedCursor(p);
003104      }
003105    }
003106    return SQLITE_OK;
003107  }
003108  
003109  /*
003110  ** The following functions:
003111  **
003112  ** sqlite3VdbeSerialType()
003113  ** sqlite3VdbeSerialTypeLen()
003114  ** sqlite3VdbeSerialLen()
003115  ** sqlite3VdbeSerialPut()
003116  ** sqlite3VdbeSerialGet()
003117  **
003118  ** encapsulate the code that serializes values for storage in SQLite
003119  ** data and index records. Each serialized value consists of a
003120  ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
003121  ** integer, stored as a varint.
003122  **
003123  ** In an SQLite index record, the serial type is stored directly before
003124  ** the blob of data that it corresponds to. In a table record, all serial
003125  ** types are stored at the start of the record, and the blobs of data at
003126  ** the end. Hence these functions allow the caller to handle the
003127  ** serial-type and data blob separately.
003128  **
003129  ** The following table describes the various storage classes for data:
003130  **
003131  **   serial type        bytes of data      type
003132  **   --------------     ---------------    ---------------
003133  **      0                     0            NULL
003134  **      1                     1            signed integer
003135  **      2                     2            signed integer
003136  **      3                     3            signed integer
003137  **      4                     4            signed integer
003138  **      5                     6            signed integer
003139  **      6                     8            signed integer
003140  **      7                     8            IEEE float
003141  **      8                     0            Integer constant 0
003142  **      9                     0            Integer constant 1
003143  **     10,11                               reserved for expansion
003144  **    N>=12 and even       (N-12)/2        BLOB
003145  **    N>=13 and odd        (N-13)/2        text
003146  **
003147  ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
003148  ** of SQLite will not understand those serial types.
003149  */
003150  
003151  /*
003152  ** Return the serial-type for the value stored in pMem.
003153  */
003154  u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
003155    int flags = pMem->flags;
003156    u32 n;
003157  
003158    assert( pLen!=0 );
003159    if( flags&MEM_Null ){
003160      *pLen = 0;
003161      return 0;
003162    }
003163    if( flags&MEM_Int ){
003164      /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
003165  #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
003166      i64 i = pMem->u.i;
003167      u64 u;
003168      if( i<0 ){
003169        u = ~i;
003170      }else{
003171        u = i;
003172      }
003173      if( u<=127 ){
003174        if( (i&1)==i && file_format>=4 ){
003175          *pLen = 0;
003176          return 8+(u32)u;
003177        }else{
003178          *pLen = 1;
003179          return 1;
003180        }
003181      }
003182      if( u<=32767 ){ *pLen = 2; return 2; }
003183      if( u<=8388607 ){ *pLen = 3; return 3; }
003184      if( u<=2147483647 ){ *pLen = 4; return 4; }
003185      if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
003186      *pLen = 8;
003187      return 6;
003188    }
003189    if( flags&MEM_Real ){
003190      *pLen = 8;
003191      return 7;
003192    }
003193    assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
003194    assert( pMem->n>=0 );
003195    n = (u32)pMem->n;
003196    if( flags & MEM_Zero ){
003197      n += pMem->u.nZero;
003198    }
003199    *pLen = n;
003200    return ((n*2) + 12 + ((flags&MEM_Str)!=0));
003201  }
003202  
003203  /*
003204  ** The sizes for serial types less than 128
003205  */
003206  static const u8 sqlite3SmallTypeSizes[] = {
003207          /*  0   1   2   3   4   5   6   7   8   9 */   
003208  /*   0 */   0,  1,  2,  3,  4,  6,  8,  8,  0,  0,
003209  /*  10 */   0,  0,  0,  0,  1,  1,  2,  2,  3,  3,
003210  /*  20 */   4,  4,  5,  5,  6,  6,  7,  7,  8,  8,
003211  /*  30 */   9,  9, 10, 10, 11, 11, 12, 12, 13, 13,
003212  /*  40 */  14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
003213  /*  50 */  19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
003214  /*  60 */  24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
003215  /*  70 */  29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
003216  /*  80 */  34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
003217  /*  90 */  39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
003218  /* 100 */  44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
003219  /* 110 */  49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
003220  /* 120 */  54, 54, 55, 55, 56, 56, 57, 57
003221  };
003222  
003223  /*
003224  ** Return the length of the data corresponding to the supplied serial-type.
003225  */
003226  u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
003227    if( serial_type>=128 ){
003228      return (serial_type-12)/2;
003229    }else{
003230      assert( serial_type<12 
003231              || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
003232      return sqlite3SmallTypeSizes[serial_type];
003233    }
003234  }
003235  u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
003236    assert( serial_type<128 );
003237    return sqlite3SmallTypeSizes[serial_type];  
003238  }
003239  
003240  /*
003241  ** If we are on an architecture with mixed-endian floating 
003242  ** points (ex: ARM7) then swap the lower 4 bytes with the 
003243  ** upper 4 bytes.  Return the result.
003244  **
003245  ** For most architectures, this is a no-op.
003246  **
003247  ** (later):  It is reported to me that the mixed-endian problem
003248  ** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
003249  ** that early versions of GCC stored the two words of a 64-bit
003250  ** float in the wrong order.  And that error has been propagated
003251  ** ever since.  The blame is not necessarily with GCC, though.
003252  ** GCC might have just copying the problem from a prior compiler.
003253  ** I am also told that newer versions of GCC that follow a different
003254  ** ABI get the byte order right.
003255  **
003256  ** Developers using SQLite on an ARM7 should compile and run their
003257  ** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
003258  ** enabled, some asserts below will ensure that the byte order of
003259  ** floating point values is correct.
003260  **
003261  ** (2007-08-30)  Frank van Vugt has studied this problem closely
003262  ** and has send his findings to the SQLite developers.  Frank
003263  ** writes that some Linux kernels offer floating point hardware
003264  ** emulation that uses only 32-bit mantissas instead of a full 
003265  ** 48-bits as required by the IEEE standard.  (This is the
003266  ** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
003267  ** byte swapping becomes very complicated.  To avoid problems,
003268  ** the necessary byte swapping is carried out using a 64-bit integer
003269  ** rather than a 64-bit float.  Frank assures us that the code here
003270  ** works for him.  We, the developers, have no way to independently
003271  ** verify this, but Frank seems to know what he is talking about
003272  ** so we trust him.
003273  */
003274  #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
003275  static u64 floatSwap(u64 in){
003276    union {
003277      u64 r;
003278      u32 i[2];
003279    } u;
003280    u32 t;
003281  
003282    u.r = in;
003283    t = u.i[0];
003284    u.i[0] = u.i[1];
003285    u.i[1] = t;
003286    return u.r;
003287  }
003288  # define swapMixedEndianFloat(X)  X = floatSwap(X)
003289  #else
003290  # define swapMixedEndianFloat(X)
003291  #endif
003292  
003293  /*
003294  ** Write the serialized data blob for the value stored in pMem into 
003295  ** buf. It is assumed that the caller has allocated sufficient space.
003296  ** Return the number of bytes written.
003297  **
003298  ** nBuf is the amount of space left in buf[].  The caller is responsible
003299  ** for allocating enough space to buf[] to hold the entire field, exclusive
003300  ** of the pMem->u.nZero bytes for a MEM_Zero value.
003301  **
003302  ** Return the number of bytes actually written into buf[].  The number
003303  ** of bytes in the zero-filled tail is included in the return value only
003304  ** if those bytes were zeroed in buf[].
003305  */ 
003306  u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
003307    u32 len;
003308  
003309    /* Integer and Real */
003310    if( serial_type<=7 && serial_type>0 ){
003311      u64 v;
003312      u32 i;
003313      if( serial_type==7 ){
003314        assert( sizeof(v)==sizeof(pMem->u.r) );
003315        memcpy(&v, &pMem->u.r, sizeof(v));
003316        swapMixedEndianFloat(v);
003317      }else{
003318        v = pMem->u.i;
003319      }
003320      len = i = sqlite3SmallTypeSizes[serial_type];
003321      assert( i>0 );
003322      do{
003323        buf[--i] = (u8)(v&0xFF);
003324        v >>= 8;
003325      }while( i );
003326      return len;
003327    }
003328  
003329    /* String or blob */
003330    if( serial_type>=12 ){
003331      assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
003332               == (int)sqlite3VdbeSerialTypeLen(serial_type) );
003333      len = pMem->n;
003334      if( len>0 ) memcpy(buf, pMem->z, len);
003335      return len;
003336    }
003337  
003338    /* NULL or constants 0 or 1 */
003339    return 0;
003340  }
003341  
003342  /* Input "x" is a sequence of unsigned characters that represent a
003343  ** big-endian integer.  Return the equivalent native integer
003344  */
003345  #define ONE_BYTE_INT(x)    ((i8)(x)[0])
003346  #define TWO_BYTE_INT(x)    (256*(i8)((x)[0])|(x)[1])
003347  #define THREE_BYTE_INT(x)  (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
003348  #define FOUR_BYTE_UINT(x)  (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
003349  #define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
003350  
003351  /*
003352  ** Deserialize the data blob pointed to by buf as serial type serial_type
003353  ** and store the result in pMem.  Return the number of bytes read.
003354  **
003355  ** This function is implemented as two separate routines for performance.
003356  ** The few cases that require local variables are broken out into a separate
003357  ** routine so that in most cases the overhead of moving the stack pointer
003358  ** is avoided.
003359  */ 
003360  static u32 SQLITE_NOINLINE serialGet(
003361    const unsigned char *buf,     /* Buffer to deserialize from */
003362    u32 serial_type,              /* Serial type to deserialize */
003363    Mem *pMem                     /* Memory cell to write value into */
003364  ){
003365    u64 x = FOUR_BYTE_UINT(buf);
003366    u32 y = FOUR_BYTE_UINT(buf+4);
003367    x = (x<<32) + y;
003368    if( serial_type==6 ){
003369      /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
003370      ** twos-complement integer. */
003371      pMem->u.i = *(i64*)&x;
003372      pMem->flags = MEM_Int;
003373      testcase( pMem->u.i<0 );
003374    }else{
003375      /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
003376      ** floating point number. */
003377  #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
003378      /* Verify that integers and floating point values use the same
003379      ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
003380      ** defined that 64-bit floating point values really are mixed
003381      ** endian.
003382      */
003383      static const u64 t1 = ((u64)0x3ff00000)<<32;
003384      static const double r1 = 1.0;
003385      u64 t2 = t1;
003386      swapMixedEndianFloat(t2);
003387      assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
003388  #endif
003389      assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
003390      swapMixedEndianFloat(x);
003391      memcpy(&pMem->u.r, &x, sizeof(x));
003392      pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real;
003393    }
003394    return 8;
003395  }
003396  u32 sqlite3VdbeSerialGet(
003397    const unsigned char *buf,     /* Buffer to deserialize from */
003398    u32 serial_type,              /* Serial type to deserialize */
003399    Mem *pMem                     /* Memory cell to write value into */
003400  ){
003401    switch( serial_type ){
003402      case 10:   /* Reserved for future use */
003403      case 11:   /* Reserved for future use */
003404      case 0: {  /* Null */
003405        /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
003406        pMem->flags = MEM_Null;
003407        break;
003408      }
003409      case 1: {
003410        /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
003411        ** integer. */
003412        pMem->u.i = ONE_BYTE_INT(buf);
003413        pMem->flags = MEM_Int;
003414        testcase( pMem->u.i<0 );
003415        return 1;
003416      }
003417      case 2: { /* 2-byte signed integer */
003418        /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
003419        ** twos-complement integer. */
003420        pMem->u.i = TWO_BYTE_INT(buf);
003421        pMem->flags = MEM_Int;
003422        testcase( pMem->u.i<0 );
003423        return 2;
003424      }
003425      case 3: { /* 3-byte signed integer */
003426        /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
003427        ** twos-complement integer. */
003428        pMem->u.i = THREE_BYTE_INT(buf);
003429        pMem->flags = MEM_Int;
003430        testcase( pMem->u.i<0 );
003431        return 3;
003432      }
003433      case 4: { /* 4-byte signed integer */
003434        /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
003435        ** twos-complement integer. */
003436        pMem->u.i = FOUR_BYTE_INT(buf);
003437  #ifdef __HP_cc 
003438        /* Work around a sign-extension bug in the HP compiler for HP/UX */
003439        if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
003440  #endif
003441        pMem->flags = MEM_Int;
003442        testcase( pMem->u.i<0 );
003443        return 4;
003444      }
003445      case 5: { /* 6-byte signed integer */
003446        /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
003447        ** twos-complement integer. */
003448        pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
003449        pMem->flags = MEM_Int;
003450        testcase( pMem->u.i<0 );
003451        return 6;
003452      }
003453      case 6:   /* 8-byte signed integer */
003454      case 7: { /* IEEE floating point */
003455        /* These use local variables, so do them in a separate routine
003456        ** to avoid having to move the frame pointer in the common case */
003457        return serialGet(buf,serial_type,pMem);
003458      }
003459      case 8:    /* Integer 0 */
003460      case 9: {  /* Integer 1 */
003461        /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
003462        /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
003463        pMem->u.i = serial_type-8;
003464        pMem->flags = MEM_Int;
003465        return 0;
003466      }
003467      default: {
003468        /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
003469        ** length.
003470        ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
003471        ** (N-13)/2 bytes in length. */
003472        static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
003473        pMem->z = (char *)buf;
003474        pMem->n = (serial_type-12)/2;
003475        pMem->flags = aFlag[serial_type&1];
003476        return pMem->n;
003477      }
003478    }
003479    return 0;
003480  }
003481  /*
003482  ** This routine is used to allocate sufficient space for an UnpackedRecord
003483  ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
003484  ** the first argument is a pointer to KeyInfo structure pKeyInfo.
003485  **
003486  ** The space is either allocated using sqlite3DbMallocRaw() or from within
003487  ** the unaligned buffer passed via the second and third arguments (presumably
003488  ** stack space). If the former, then *ppFree is set to a pointer that should
003489  ** be eventually freed by the caller using sqlite3DbFree(). Or, if the 
003490  ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
003491  ** before returning.
003492  **
003493  ** If an OOM error occurs, NULL is returned.
003494  */
003495  UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
003496    KeyInfo *pKeyInfo               /* Description of the record */
003497  ){
003498    UnpackedRecord *p;              /* Unpacked record to return */
003499    int nByte;                      /* Number of bytes required for *p */
003500    nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
003501    p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
003502    if( !p ) return 0;
003503    p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
003504    assert( pKeyInfo->aSortOrder!=0 );
003505    p->pKeyInfo = pKeyInfo;
003506    p->nField = pKeyInfo->nField + 1;
003507    return p;
003508  }
003509  
003510  /*
003511  ** Given the nKey-byte encoding of a record in pKey[], populate the 
003512  ** UnpackedRecord structure indicated by the fourth argument with the
003513  ** contents of the decoded record.
003514  */ 
003515  void sqlite3VdbeRecordUnpack(
003516    KeyInfo *pKeyInfo,     /* Information about the record format */
003517    int nKey,              /* Size of the binary record */
003518    const void *pKey,      /* The binary record */
003519    UnpackedRecord *p      /* Populate this structure before returning. */
003520  ){
003521    const unsigned char *aKey = (const unsigned char *)pKey;
003522    int d; 
003523    u32 idx;                        /* Offset in aKey[] to read from */
003524    u16 u;                          /* Unsigned loop counter */
003525    u32 szHdr;
003526    Mem *pMem = p->aMem;
003527  
003528    p->default_rc = 0;
003529    assert( EIGHT_BYTE_ALIGNMENT(pMem) );
003530    idx = getVarint32(aKey, szHdr);
003531    d = szHdr;
003532    u = 0;
003533    while( idx<szHdr && d<=nKey ){
003534      u32 serial_type;
003535  
003536      idx += getVarint32(&aKey[idx], serial_type);
003537      pMem->enc = pKeyInfo->enc;
003538      pMem->db = pKeyInfo->db;
003539      /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
003540      pMem->szMalloc = 0;
003541      pMem->z = 0;
003542      d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
003543      pMem++;
003544      if( (++u)>=p->nField ) break;
003545    }
003546    assert( u<=pKeyInfo->nField + 1 );
003547    p->nField = u;
003548  }
003549  
003550  #if SQLITE_DEBUG
003551  /*
003552  ** This function compares two index or table record keys in the same way
003553  ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
003554  ** this function deserializes and compares values using the
003555  ** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
003556  ** in assert() statements to ensure that the optimized code in
003557  ** sqlite3VdbeRecordCompare() returns results with these two primitives.
003558  **
003559  ** Return true if the result of comparison is equivalent to desiredResult.
003560  ** Return false if there is a disagreement.
003561  */
003562  static int vdbeRecordCompareDebug(
003563    int nKey1, const void *pKey1, /* Left key */
003564    const UnpackedRecord *pPKey2, /* Right key */
003565    int desiredResult             /* Correct answer */
003566  ){
003567    u32 d1;            /* Offset into aKey[] of next data element */
003568    u32 idx1;          /* Offset into aKey[] of next header element */
003569    u32 szHdr1;        /* Number of bytes in header */
003570    int i = 0;
003571    int rc = 0;
003572    const unsigned char *aKey1 = (const unsigned char *)pKey1;
003573    KeyInfo *pKeyInfo;
003574    Mem mem1;
003575  
003576    pKeyInfo = pPKey2->pKeyInfo;
003577    if( pKeyInfo->db==0 ) return 1;
003578    mem1.enc = pKeyInfo->enc;
003579    mem1.db = pKeyInfo->db;
003580    /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
003581    VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
003582  
003583    /* Compilers may complain that mem1.u.i is potentially uninitialized.
003584    ** We could initialize it, as shown here, to silence those complaints.
003585    ** But in fact, mem1.u.i will never actually be used uninitialized, and doing 
003586    ** the unnecessary initialization has a measurable negative performance
003587    ** impact, since this routine is a very high runner.  And so, we choose
003588    ** to ignore the compiler warnings and leave this variable uninitialized.
003589    */
003590    /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
003591    
003592    idx1 = getVarint32(aKey1, szHdr1);
003593    if( szHdr1>98307 ) return SQLITE_CORRUPT;
003594    d1 = szHdr1;
003595    assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
003596    assert( pKeyInfo->aSortOrder!=0 );
003597    assert( pKeyInfo->nField>0 );
003598    assert( idx1<=szHdr1 || CORRUPT_DB );
003599    do{
003600      u32 serial_type1;
003601  
003602      /* Read the serial types for the next element in each key. */
003603      idx1 += getVarint32( aKey1+idx1, serial_type1 );
003604  
003605      /* Verify that there is enough key space remaining to avoid
003606      ** a buffer overread.  The "d1+serial_type1+2" subexpression will
003607      ** always be greater than or equal to the amount of required key space.
003608      ** Use that approximation to avoid the more expensive call to
003609      ** sqlite3VdbeSerialTypeLen() in the common case.
003610      */
003611      if( d1+serial_type1+2>(u32)nKey1
003612       && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1 
003613      ){
003614        break;
003615      }
003616  
003617      /* Extract the values to be compared.
003618      */
003619      d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
003620  
003621      /* Do the comparison
003622      */
003623      rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
003624      if( rc!=0 ){
003625        assert( mem1.szMalloc==0 );  /* See comment below */
003626        if( pKeyInfo->aSortOrder[i] ){
003627          rc = -rc;  /* Invert the result for DESC sort order. */
003628        }
003629        goto debugCompareEnd;
003630      }
003631      i++;
003632    }while( idx1<szHdr1 && i<pPKey2->nField );
003633  
003634    /* No memory allocation is ever used on mem1.  Prove this using
003635    ** the following assert().  If the assert() fails, it indicates a
003636    ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
003637    */
003638    assert( mem1.szMalloc==0 );
003639  
003640    /* rc==0 here means that one of the keys ran out of fields and
003641    ** all the fields up to that point were equal. Return the default_rc
003642    ** value.  */
003643    rc = pPKey2->default_rc;
003644  
003645  debugCompareEnd:
003646    if( desiredResult==0 && rc==0 ) return 1;
003647    if( desiredResult<0 && rc<0 ) return 1;
003648    if( desiredResult>0 && rc>0 ) return 1;
003649    if( CORRUPT_DB ) return 1;
003650    if( pKeyInfo->db->mallocFailed ) return 1;
003651    return 0;
003652  }
003653  #endif
003654  
003655  #if SQLITE_DEBUG
003656  /*
003657  ** Count the number of fields (a.k.a. columns) in the record given by
003658  ** pKey,nKey.  The verify that this count is less than or equal to the
003659  ** limit given by pKeyInfo->nField + pKeyInfo->nXField.
003660  **
003661  ** If this constraint is not satisfied, it means that the high-speed
003662  ** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
003663  ** not work correctly.  If this assert() ever fires, it probably means
003664  ** that the KeyInfo.nField or KeyInfo.nXField values were computed
003665  ** incorrectly.
003666  */
003667  static void vdbeAssertFieldCountWithinLimits(
003668    int nKey, const void *pKey,   /* The record to verify */ 
003669    const KeyInfo *pKeyInfo       /* Compare size with this KeyInfo */
003670  ){
003671    int nField = 0;
003672    u32 szHdr;
003673    u32 idx;
003674    u32 notUsed;
003675    const unsigned char *aKey = (const unsigned char*)pKey;
003676  
003677    if( CORRUPT_DB ) return;
003678    idx = getVarint32(aKey, szHdr);
003679    assert( nKey>=0 );
003680    assert( szHdr<=(u32)nKey );
003681    while( idx<szHdr ){
003682      idx += getVarint32(aKey+idx, notUsed);
003683      nField++;
003684    }
003685    assert( nField <= pKeyInfo->nField+pKeyInfo->nXField );
003686  }
003687  #else
003688  # define vdbeAssertFieldCountWithinLimits(A,B,C)
003689  #endif
003690  
003691  /*
003692  ** Both *pMem1 and *pMem2 contain string values. Compare the two values
003693  ** using the collation sequence pColl. As usual, return a negative , zero
003694  ** or positive value if *pMem1 is less than, equal to or greater than 
003695  ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
003696  */
003697  static int vdbeCompareMemString(
003698    const Mem *pMem1,
003699    const Mem *pMem2,
003700    const CollSeq *pColl,
003701    u8 *prcErr                      /* If an OOM occurs, set to SQLITE_NOMEM */
003702  ){
003703    if( pMem1->enc==pColl->enc ){
003704      /* The strings are already in the correct encoding.  Call the
003705       ** comparison function directly */
003706      return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
003707    }else{
003708      int rc;
003709      const void *v1, *v2;
003710      int n1, n2;
003711      Mem c1;
003712      Mem c2;
003713      sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
003714      sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
003715      sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
003716      sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
003717      v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
003718      n1 = v1==0 ? 0 : c1.n;
003719      v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
003720      n2 = v2==0 ? 0 : c2.n;
003721      rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
003722      if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM_BKPT;
003723      sqlite3VdbeMemRelease(&c1);
003724      sqlite3VdbeMemRelease(&c2);
003725      return rc;
003726    }
003727  }
003728  
003729  /*
003730  ** The input pBlob is guaranteed to be a Blob that is not marked
003731  ** with MEM_Zero.  Return true if it could be a zero-blob.
003732  */
003733  static int isAllZero(const char *z, int n){
003734    int i;
003735    for(i=0; i<n; i++){
003736      if( z[i] ) return 0;
003737    }
003738    return 1;
003739  }
003740  
003741  /*
003742  ** Compare two blobs.  Return negative, zero, or positive if the first
003743  ** is less than, equal to, or greater than the second, respectively.
003744  ** If one blob is a prefix of the other, then the shorter is the lessor.
003745  */
003746  static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
003747    int c;
003748    int n1 = pB1->n;
003749    int n2 = pB2->n;
003750  
003751    /* It is possible to have a Blob value that has some non-zero content
003752    ** followed by zero content.  But that only comes up for Blobs formed
003753    ** by the OP_MakeRecord opcode, and such Blobs never get passed into
003754    ** sqlite3MemCompare(). */
003755    assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
003756    assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
003757  
003758    if( (pB1->flags|pB2->flags) & MEM_Zero ){
003759      if( pB1->flags & pB2->flags & MEM_Zero ){
003760        return pB1->u.nZero - pB2->u.nZero;
003761      }else if( pB1->flags & MEM_Zero ){
003762        if( !isAllZero(pB2->z, pB2->n) ) return -1;
003763        return pB1->u.nZero - n2;
003764      }else{
003765        if( !isAllZero(pB1->z, pB1->n) ) return +1;
003766        return n1 - pB2->u.nZero;
003767      }
003768    }
003769    c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
003770    if( c ) return c;
003771    return n1 - n2;
003772  }
003773  
003774  /*
003775  ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
003776  ** number.  Return negative, zero, or positive if the first (i64) is less than,
003777  ** equal to, or greater than the second (double).
003778  */
003779  static int sqlite3IntFloatCompare(i64 i, double r){
003780    if( sizeof(LONGDOUBLE_TYPE)>8 ){
003781      LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i;
003782      if( x<r ) return -1;
003783      if( x>r ) return +1;
003784      return 0;
003785    }else{
003786      i64 y;
003787      double s;
003788      if( r<-9223372036854775808.0 ) return +1;
003789      if( r>9223372036854775807.0 ) return -1;
003790      y = (i64)r;
003791      if( i<y ) return -1;
003792      if( i>y ){
003793        if( y==SMALLEST_INT64 && r>0.0 ) return -1;
003794        return +1;
003795      }
003796      s = (double)i;
003797      if( s<r ) return -1;
003798      if( s>r ) return +1;
003799      return 0;
003800    }
003801  }
003802  
003803  /*
003804  ** Compare the values contained by the two memory cells, returning
003805  ** negative, zero or positive if pMem1 is less than, equal to, or greater
003806  ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
003807  ** and reals) sorted numerically, followed by text ordered by the collating
003808  ** sequence pColl and finally blob's ordered by memcmp().
003809  **
003810  ** Two NULL values are considered equal by this function.
003811  */
003812  int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
003813    int f1, f2;
003814    int combined_flags;
003815  
003816    f1 = pMem1->flags;
003817    f2 = pMem2->flags;
003818    combined_flags = f1|f2;
003819    assert( (combined_flags & MEM_RowSet)==0 );
003820   
003821    /* If one value is NULL, it is less than the other. If both values
003822    ** are NULL, return 0.
003823    */
003824    if( combined_flags&MEM_Null ){
003825      return (f2&MEM_Null) - (f1&MEM_Null);
003826    }
003827  
003828    /* At least one of the two values is a number
003829    */
003830    if( combined_flags&(MEM_Int|MEM_Real) ){
003831      if( (f1 & f2 & MEM_Int)!=0 ){
003832        if( pMem1->u.i < pMem2->u.i ) return -1;
003833        if( pMem1->u.i > pMem2->u.i ) return +1;
003834        return 0;
003835      }
003836      if( (f1 & f2 & MEM_Real)!=0 ){
003837        if( pMem1->u.r < pMem2->u.r ) return -1;
003838        if( pMem1->u.r > pMem2->u.r ) return +1;
003839        return 0;
003840      }
003841      if( (f1&MEM_Int)!=0 ){
003842        if( (f2&MEM_Real)!=0 ){
003843          return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
003844        }else{
003845          return -1;
003846        }
003847      }
003848      if( (f1&MEM_Real)!=0 ){
003849        if( (f2&MEM_Int)!=0 ){
003850          return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
003851        }else{
003852          return -1;
003853        }
003854      }
003855      return +1;
003856    }
003857  
003858    /* If one value is a string and the other is a blob, the string is less.
003859    ** If both are strings, compare using the collating functions.
003860    */
003861    if( combined_flags&MEM_Str ){
003862      if( (f1 & MEM_Str)==0 ){
003863        return 1;
003864      }
003865      if( (f2 & MEM_Str)==0 ){
003866        return -1;
003867      }
003868  
003869      assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
003870      assert( pMem1->enc==SQLITE_UTF8 || 
003871              pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
003872  
003873      /* The collation sequence must be defined at this point, even if
003874      ** the user deletes the collation sequence after the vdbe program is
003875      ** compiled (this was not always the case).
003876      */
003877      assert( !pColl || pColl->xCmp );
003878  
003879      if( pColl ){
003880        return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
003881      }
003882      /* If a NULL pointer was passed as the collate function, fall through
003883      ** to the blob case and use memcmp().  */
003884    }
003885   
003886    /* Both values must be blobs.  Compare using memcmp().  */
003887    return sqlite3BlobCompare(pMem1, pMem2);
003888  }
003889  
003890  
003891  /*
003892  ** The first argument passed to this function is a serial-type that
003893  ** corresponds to an integer - all values between 1 and 9 inclusive 
003894  ** except 7. The second points to a buffer containing an integer value
003895  ** serialized according to serial_type. This function deserializes
003896  ** and returns the value.
003897  */
003898  static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
003899    u32 y;
003900    assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
003901    switch( serial_type ){
003902      case 0:
003903      case 1:
003904        testcase( aKey[0]&0x80 );
003905        return ONE_BYTE_INT(aKey);
003906      case 2:
003907        testcase( aKey[0]&0x80 );
003908        return TWO_BYTE_INT(aKey);
003909      case 3:
003910        testcase( aKey[0]&0x80 );
003911        return THREE_BYTE_INT(aKey);
003912      case 4: {
003913        testcase( aKey[0]&0x80 );
003914        y = FOUR_BYTE_UINT(aKey);
003915        return (i64)*(int*)&y;
003916      }
003917      case 5: {
003918        testcase( aKey[0]&0x80 );
003919        return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
003920      }
003921      case 6: {
003922        u64 x = FOUR_BYTE_UINT(aKey);
003923        testcase( aKey[0]&0x80 );
003924        x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
003925        return (i64)*(i64*)&x;
003926      }
003927    }
003928  
003929    return (serial_type - 8);
003930  }
003931  
003932  /*
003933  ** This function compares the two table rows or index records
003934  ** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
003935  ** or positive integer if key1 is less than, equal to or 
003936  ** greater than key2.  The {nKey1, pKey1} key must be a blob
003937  ** created by the OP_MakeRecord opcode of the VDBE.  The pPKey2
003938  ** key must be a parsed key such as obtained from
003939  ** sqlite3VdbeParseRecord.
003940  **
003941  ** If argument bSkip is non-zero, it is assumed that the caller has already
003942  ** determined that the first fields of the keys are equal.
003943  **
003944  ** Key1 and Key2 do not have to contain the same number of fields. If all 
003945  ** fields that appear in both keys are equal, then pPKey2->default_rc is 
003946  ** returned.
003947  **
003948  ** If database corruption is discovered, set pPKey2->errCode to 
003949  ** SQLITE_CORRUPT and return 0. If an OOM error is encountered, 
003950  ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
003951  ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
003952  */
003953  int sqlite3VdbeRecordCompareWithSkip(
003954    int nKey1, const void *pKey1,   /* Left key */
003955    UnpackedRecord *pPKey2,         /* Right key */
003956    int bSkip                       /* If true, skip the first field */
003957  ){
003958    u32 d1;                         /* Offset into aKey[] of next data element */
003959    int i;                          /* Index of next field to compare */
003960    u32 szHdr1;                     /* Size of record header in bytes */
003961    u32 idx1;                       /* Offset of first type in header */
003962    int rc = 0;                     /* Return value */
003963    Mem *pRhs = pPKey2->aMem;       /* Next field of pPKey2 to compare */
003964    KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
003965    const unsigned char *aKey1 = (const unsigned char *)pKey1;
003966    Mem mem1;
003967  
003968    /* If bSkip is true, then the caller has already determined that the first
003969    ** two elements in the keys are equal. Fix the various stack variables so
003970    ** that this routine begins comparing at the second field. */
003971    if( bSkip ){
003972      u32 s1;
003973      idx1 = 1 + getVarint32(&aKey1[1], s1);
003974      szHdr1 = aKey1[0];
003975      d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
003976      i = 1;
003977      pRhs++;
003978    }else{
003979      idx1 = getVarint32(aKey1, szHdr1);
003980      d1 = szHdr1;
003981      if( d1>(unsigned)nKey1 ){ 
003982        pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
003983        return 0;  /* Corruption */
003984      }
003985      i = 0;
003986    }
003987  
003988    VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
003989    assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField 
003990         || CORRUPT_DB );
003991    assert( pPKey2->pKeyInfo->aSortOrder!=0 );
003992    assert( pPKey2->pKeyInfo->nField>0 );
003993    assert( idx1<=szHdr1 || CORRUPT_DB );
003994    do{
003995      u32 serial_type;
003996  
003997      /* RHS is an integer */
003998      if( pRhs->flags & MEM_Int ){
003999        serial_type = aKey1[idx1];
004000        testcase( serial_type==12 );
004001        if( serial_type>=10 ){
004002          rc = +1;
004003        }else if( serial_type==0 ){
004004          rc = -1;
004005        }else if( serial_type==7 ){
004006          sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
004007          rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
004008        }else{
004009          i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
004010          i64 rhs = pRhs->u.i;
004011          if( lhs<rhs ){
004012            rc = -1;
004013          }else if( lhs>rhs ){
004014            rc = +1;
004015          }
004016        }
004017      }
004018  
004019      /* RHS is real */
004020      else if( pRhs->flags & MEM_Real ){
004021        serial_type = aKey1[idx1];
004022        if( serial_type>=10 ){
004023          /* Serial types 12 or greater are strings and blobs (greater than
004024          ** numbers). Types 10 and 11 are currently "reserved for future 
004025          ** use", so it doesn't really matter what the results of comparing
004026          ** them to numberic values are.  */
004027          rc = +1;
004028        }else if( serial_type==0 ){
004029          rc = -1;
004030        }else{
004031          sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
004032          if( serial_type==7 ){
004033            if( mem1.u.r<pRhs->u.r ){
004034              rc = -1;
004035            }else if( mem1.u.r>pRhs->u.r ){
004036              rc = +1;
004037            }
004038          }else{
004039            rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
004040          }
004041        }
004042      }
004043  
004044      /* RHS is a string */
004045      else if( pRhs->flags & MEM_Str ){
004046        getVarint32(&aKey1[idx1], serial_type);
004047        testcase( serial_type==12 );
004048        if( serial_type<12 ){
004049          rc = -1;
004050        }else if( !(serial_type & 0x01) ){
004051          rc = +1;
004052        }else{
004053          mem1.n = (serial_type - 12) / 2;
004054          testcase( (d1+mem1.n)==(unsigned)nKey1 );
004055          testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
004056          if( (d1+mem1.n) > (unsigned)nKey1 ){
004057            pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
004058            return 0;                /* Corruption */
004059          }else if( pKeyInfo->aColl[i] ){
004060            mem1.enc = pKeyInfo->enc;
004061            mem1.db = pKeyInfo->db;
004062            mem1.flags = MEM_Str;
004063            mem1.z = (char*)&aKey1[d1];
004064            rc = vdbeCompareMemString(
004065                &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
004066            );
004067          }else{
004068            int nCmp = MIN(mem1.n, pRhs->n);
004069            rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
004070            if( rc==0 ) rc = mem1.n - pRhs->n; 
004071          }
004072        }
004073      }
004074  
004075      /* RHS is a blob */
004076      else if( pRhs->flags & MEM_Blob ){
004077        assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
004078        getVarint32(&aKey1[idx1], serial_type);
004079        testcase( serial_type==12 );
004080        if( serial_type<12 || (serial_type & 0x01) ){
004081          rc = -1;
004082        }else{
004083          int nStr = (serial_type - 12) / 2;
004084          testcase( (d1+nStr)==(unsigned)nKey1 );
004085          testcase( (d1+nStr+1)==(unsigned)nKey1 );
004086          if( (d1+nStr) > (unsigned)nKey1 ){
004087            pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
004088            return 0;                /* Corruption */
004089          }else if( pRhs->flags & MEM_Zero ){
004090            if( !isAllZero((const char*)&aKey1[d1],nStr) ){
004091              rc = 1;
004092            }else{
004093              rc = nStr - pRhs->u.nZero;
004094            }
004095          }else{
004096            int nCmp = MIN(nStr, pRhs->n);
004097            rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
004098            if( rc==0 ) rc = nStr - pRhs->n;
004099          }
004100        }
004101      }
004102  
004103      /* RHS is null */
004104      else{
004105        serial_type = aKey1[idx1];
004106        rc = (serial_type!=0);
004107      }
004108  
004109      if( rc!=0 ){
004110        if( pKeyInfo->aSortOrder[i] ){
004111          rc = -rc;
004112        }
004113        assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
004114        assert( mem1.szMalloc==0 );  /* See comment below */
004115        return rc;
004116      }
004117  
004118      i++;
004119      pRhs++;
004120      d1 += sqlite3VdbeSerialTypeLen(serial_type);
004121      idx1 += sqlite3VarintLen(serial_type);
004122    }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
004123  
004124    /* No memory allocation is ever used on mem1.  Prove this using
004125    ** the following assert().  If the assert() fails, it indicates a
004126    ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).  */
004127    assert( mem1.szMalloc==0 );
004128  
004129    /* rc==0 here means that one or both of the keys ran out of fields and
004130    ** all the fields up to that point were equal. Return the default_rc
004131    ** value.  */
004132    assert( CORRUPT_DB 
004133         || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc) 
004134         || pKeyInfo->db->mallocFailed
004135    );
004136    pPKey2->eqSeen = 1;
004137    return pPKey2->default_rc;
004138  }
004139  int sqlite3VdbeRecordCompare(
004140    int nKey1, const void *pKey1,   /* Left key */
004141    UnpackedRecord *pPKey2          /* Right key */
004142  ){
004143    return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
004144  }
004145  
004146  
004147  /*
004148  ** This function is an optimized version of sqlite3VdbeRecordCompare() 
004149  ** that (a) the first field of pPKey2 is an integer, and (b) the 
004150  ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
004151  ** byte (i.e. is less than 128).
004152  **
004153  ** To avoid concerns about buffer overreads, this routine is only used
004154  ** on schemas where the maximum valid header size is 63 bytes or less.
004155  */
004156  static int vdbeRecordCompareInt(
004157    int nKey1, const void *pKey1, /* Left key */
004158    UnpackedRecord *pPKey2        /* Right key */
004159  ){
004160    const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
004161    int serial_type = ((const u8*)pKey1)[1];
004162    int res;
004163    u32 y;
004164    u64 x;
004165    i64 v;
004166    i64 lhs;
004167  
004168    vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
004169    assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
004170    switch( serial_type ){
004171      case 1: { /* 1-byte signed integer */
004172        lhs = ONE_BYTE_INT(aKey);
004173        testcase( lhs<0 );
004174        break;
004175      }
004176      case 2: { /* 2-byte signed integer */
004177        lhs = TWO_BYTE_INT(aKey);
004178        testcase( lhs<0 );
004179        break;
004180      }
004181      case 3: { /* 3-byte signed integer */
004182        lhs = THREE_BYTE_INT(aKey);
004183        testcase( lhs<0 );
004184        break;
004185      }
004186      case 4: { /* 4-byte signed integer */
004187        y = FOUR_BYTE_UINT(aKey);
004188        lhs = (i64)*(int*)&y;
004189        testcase( lhs<0 );
004190        break;
004191      }
004192      case 5: { /* 6-byte signed integer */
004193        lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
004194        testcase( lhs<0 );
004195        break;
004196      }
004197      case 6: { /* 8-byte signed integer */
004198        x = FOUR_BYTE_UINT(aKey);
004199        x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
004200        lhs = *(i64*)&x;
004201        testcase( lhs<0 );
004202        break;
004203      }
004204      case 8: 
004205        lhs = 0;
004206        break;
004207      case 9:
004208        lhs = 1;
004209        break;
004210  
004211      /* This case could be removed without changing the results of running
004212      ** this code. Including it causes gcc to generate a faster switch 
004213      ** statement (since the range of switch targets now starts at zero and
004214      ** is contiguous) but does not cause any duplicate code to be generated
004215      ** (as gcc is clever enough to combine the two like cases). Other 
004216      ** compilers might be similar.  */ 
004217      case 0: case 7:
004218        return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
004219  
004220      default:
004221        return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
004222    }
004223  
004224    v = pPKey2->aMem[0].u.i;
004225    if( v>lhs ){
004226      res = pPKey2->r1;
004227    }else if( v<lhs ){
004228      res = pPKey2->r2;
004229    }else if( pPKey2->nField>1 ){
004230      /* The first fields of the two keys are equal. Compare the trailing 
004231      ** fields.  */
004232      res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
004233    }else{
004234      /* The first fields of the two keys are equal and there are no trailing
004235      ** fields. Return pPKey2->default_rc in this case. */
004236      res = pPKey2->default_rc;
004237      pPKey2->eqSeen = 1;
004238    }
004239  
004240    assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
004241    return res;
004242  }
004243  
004244  /*
004245  ** This function is an optimized version of sqlite3VdbeRecordCompare() 
004246  ** that (a) the first field of pPKey2 is a string, that (b) the first field
004247  ** uses the collation sequence BINARY and (c) that the size-of-header varint 
004248  ** at the start of (pKey1/nKey1) fits in a single byte.
004249  */
004250  static int vdbeRecordCompareString(
004251    int nKey1, const void *pKey1, /* Left key */
004252    UnpackedRecord *pPKey2        /* Right key */
004253  ){
004254    const u8 *aKey1 = (const u8*)pKey1;
004255    int serial_type;
004256    int res;
004257  
004258    assert( pPKey2->aMem[0].flags & MEM_Str );
004259    vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
004260    getVarint32(&aKey1[1], serial_type);
004261    if( serial_type<12 ){
004262      res = pPKey2->r1;      /* (pKey1/nKey1) is a number or a null */
004263    }else if( !(serial_type & 0x01) ){ 
004264      res = pPKey2->r2;      /* (pKey1/nKey1) is a blob */
004265    }else{
004266      int nCmp;
004267      int nStr;
004268      int szHdr = aKey1[0];
004269  
004270      nStr = (serial_type-12) / 2;
004271      if( (szHdr + nStr) > nKey1 ){
004272        pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
004273        return 0;    /* Corruption */
004274      }
004275      nCmp = MIN( pPKey2->aMem[0].n, nStr );
004276      res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
004277  
004278      if( res==0 ){
004279        res = nStr - pPKey2->aMem[0].n;
004280        if( res==0 ){
004281          if( pPKey2->nField>1 ){
004282            res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
004283          }else{
004284            res = pPKey2->default_rc;
004285            pPKey2->eqSeen = 1;
004286          }
004287        }else if( res>0 ){
004288          res = pPKey2->r2;
004289        }else{
004290          res = pPKey2->r1;
004291        }
004292      }else if( res>0 ){
004293        res = pPKey2->r2;
004294      }else{
004295        res = pPKey2->r1;
004296      }
004297    }
004298  
004299    assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)
004300         || CORRUPT_DB
004301         || pPKey2->pKeyInfo->db->mallocFailed
004302    );
004303    return res;
004304  }
004305  
004306  /*
004307  ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
004308  ** suitable for comparing serialized records to the unpacked record passed
004309  ** as the only argument.
004310  */
004311  RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
004312    /* varintRecordCompareInt() and varintRecordCompareString() both assume
004313    ** that the size-of-header varint that occurs at the start of each record
004314    ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
004315    ** also assumes that it is safe to overread a buffer by at least the 
004316    ** maximum possible legal header size plus 8 bytes. Because there is
004317    ** guaranteed to be at least 74 (but not 136) bytes of padding following each
004318    ** buffer passed to varintRecordCompareInt() this makes it convenient to
004319    ** limit the size of the header to 64 bytes in cases where the first field
004320    ** is an integer.
004321    **
004322    ** The easiest way to enforce this limit is to consider only records with
004323    ** 13 fields or less. If the first field is an integer, the maximum legal
004324    ** header size is (12*5 + 1 + 1) bytes.  */
004325    if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
004326      int flags = p->aMem[0].flags;
004327      if( p->pKeyInfo->aSortOrder[0] ){
004328        p->r1 = 1;
004329        p->r2 = -1;
004330      }else{
004331        p->r1 = -1;
004332        p->r2 = 1;
004333      }
004334      if( (flags & MEM_Int) ){
004335        return vdbeRecordCompareInt;
004336      }
004337      testcase( flags & MEM_Real );
004338      testcase( flags & MEM_Null );
004339      testcase( flags & MEM_Blob );
004340      if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
004341        assert( flags & MEM_Str );
004342        return vdbeRecordCompareString;
004343      }
004344    }
004345  
004346    return sqlite3VdbeRecordCompare;
004347  }
004348  
004349  /*
004350  ** pCur points at an index entry created using the OP_MakeRecord opcode.
004351  ** Read the rowid (the last field in the record) and store it in *rowid.
004352  ** Return SQLITE_OK if everything works, or an error code otherwise.
004353  **
004354  ** pCur might be pointing to text obtained from a corrupt database file.
004355  ** So the content cannot be trusted.  Do appropriate checks on the content.
004356  */
004357  int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
004358    i64 nCellKey = 0;
004359    int rc;
004360    u32 szHdr;        /* Size of the header */
004361    u32 typeRowid;    /* Serial type of the rowid */
004362    u32 lenRowid;     /* Size of the rowid */
004363    Mem m, v;
004364  
004365    /* Get the size of the index entry.  Only indices entries of less
004366    ** than 2GiB are support - anything large must be database corruption.
004367    ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
004368    ** this code can safely assume that nCellKey is 32-bits  
004369    */
004370    assert( sqlite3BtreeCursorIsValid(pCur) );
004371    nCellKey = sqlite3BtreePayloadSize(pCur);
004372    assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
004373  
004374    /* Read in the complete content of the index entry */
004375    sqlite3VdbeMemInit(&m, db, 0);
004376    rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m);
004377    if( rc ){
004378      return rc;
004379    }
004380  
004381    /* The index entry must begin with a header size */
004382    (void)getVarint32((u8*)m.z, szHdr);
004383    testcase( szHdr==3 );
004384    testcase( szHdr==m.n );
004385    if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
004386      goto idx_rowid_corruption;
004387    }
004388  
004389    /* The last field of the index should be an integer - the ROWID.
004390    ** Verify that the last entry really is an integer. */
004391    (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
004392    testcase( typeRowid==1 );
004393    testcase( typeRowid==2 );
004394    testcase( typeRowid==3 );
004395    testcase( typeRowid==4 );
004396    testcase( typeRowid==5 );
004397    testcase( typeRowid==6 );
004398    testcase( typeRowid==8 );
004399    testcase( typeRowid==9 );
004400    if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
004401      goto idx_rowid_corruption;
004402    }
004403    lenRowid = sqlite3SmallTypeSizes[typeRowid];
004404    testcase( (u32)m.n==szHdr+lenRowid );
004405    if( unlikely((u32)m.n<szHdr+lenRowid) ){
004406      goto idx_rowid_corruption;
004407    }
004408  
004409    /* Fetch the integer off the end of the index record */
004410    sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
004411    *rowid = v.u.i;
004412    sqlite3VdbeMemRelease(&m);
004413    return SQLITE_OK;
004414  
004415    /* Jump here if database corruption is detected after m has been
004416    ** allocated.  Free the m object and return SQLITE_CORRUPT. */
004417  idx_rowid_corruption:
004418    testcase( m.szMalloc!=0 );
004419    sqlite3VdbeMemRelease(&m);
004420    return SQLITE_CORRUPT_BKPT;
004421  }
004422  
004423  /*
004424  ** Compare the key of the index entry that cursor pC is pointing to against
004425  ** the key string in pUnpacked.  Write into *pRes a number
004426  ** that is negative, zero, or positive if pC is less than, equal to,
004427  ** or greater than pUnpacked.  Return SQLITE_OK on success.
004428  **
004429  ** pUnpacked is either created without a rowid or is truncated so that it
004430  ** omits the rowid at the end.  The rowid at the end of the index entry
004431  ** is ignored as well.  Hence, this routine only compares the prefixes 
004432  ** of the keys prior to the final rowid, not the entire key.
004433  */
004434  int sqlite3VdbeIdxKeyCompare(
004435    sqlite3 *db,                     /* Database connection */
004436    VdbeCursor *pC,                  /* The cursor to compare against */
004437    UnpackedRecord *pUnpacked,       /* Unpacked version of key */
004438    int *res                         /* Write the comparison result here */
004439  ){
004440    i64 nCellKey = 0;
004441    int rc;
004442    BtCursor *pCur;
004443    Mem m;
004444  
004445    assert( pC->eCurType==CURTYPE_BTREE );
004446    pCur = pC->uc.pCursor;
004447    assert( sqlite3BtreeCursorIsValid(pCur) );
004448    nCellKey = sqlite3BtreePayloadSize(pCur);
004449    /* nCellKey will always be between 0 and 0xffffffff because of the way
004450    ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
004451    if( nCellKey<=0 || nCellKey>0x7fffffff ){
004452      *res = 0;
004453      return SQLITE_CORRUPT_BKPT;
004454    }
004455    sqlite3VdbeMemInit(&m, db, 0);
004456    rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m);
004457    if( rc ){
004458      return rc;
004459    }
004460    *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
004461    sqlite3VdbeMemRelease(&m);
004462    return SQLITE_OK;
004463  }
004464  
004465  /*
004466  ** This routine sets the value to be returned by subsequent calls to
004467  ** sqlite3_changes() on the database handle 'db'. 
004468  */
004469  void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
004470    assert( sqlite3_mutex_held(db->mutex) );
004471    db->nChange = nChange;
004472    db->nTotalChange += nChange;
004473  }
004474  
004475  /*
004476  ** Set a flag in the vdbe to update the change counter when it is finalised
004477  ** or reset.
004478  */
004479  void sqlite3VdbeCountChanges(Vdbe *v){
004480    v->changeCntOn = 1;
004481  }
004482  
004483  /*
004484  ** Mark every prepared statement associated with a database connection
004485  ** as expired.
004486  **
004487  ** An expired statement means that recompilation of the statement is
004488  ** recommend.  Statements expire when things happen that make their
004489  ** programs obsolete.  Removing user-defined functions or collating
004490  ** sequences, or changing an authorization function are the types of
004491  ** things that make prepared statements obsolete.
004492  */
004493  void sqlite3ExpirePreparedStatements(sqlite3 *db){
004494    Vdbe *p;
004495    for(p = db->pVdbe; p; p=p->pNext){
004496      p->expired = 1;
004497    }
004498  }
004499  
004500  /*
004501  ** Return the database associated with the Vdbe.
004502  */
004503  sqlite3 *sqlite3VdbeDb(Vdbe *v){
004504    return v->db;
004505  }
004506  
004507  /*
004508  ** Return a pointer to an sqlite3_value structure containing the value bound
004509  ** parameter iVar of VM v. Except, if the value is an SQL NULL, return 
004510  ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
004511  ** constants) to the value before returning it.
004512  **
004513  ** The returned value must be freed by the caller using sqlite3ValueFree().
004514  */
004515  sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
004516    assert( iVar>0 );
004517    if( v ){
004518      Mem *pMem = &v->aVar[iVar-1];
004519      if( 0==(pMem->flags & MEM_Null) ){
004520        sqlite3_value *pRet = sqlite3ValueNew(v->db);
004521        if( pRet ){
004522          sqlite3VdbeMemCopy((Mem *)pRet, pMem);
004523          sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
004524        }
004525        return pRet;
004526      }
004527    }
004528    return 0;
004529  }
004530  
004531  /*
004532  ** Configure SQL variable iVar so that binding a new value to it signals
004533  ** to sqlite3_reoptimize() that re-preparing the statement may result
004534  ** in a better query plan.
004535  */
004536  void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
004537    assert( iVar>0 );
004538    if( iVar>32 ){
004539      v->expmask = 0xffffffff;
004540    }else{
004541      v->expmask |= ((u32)1 << (iVar-1));
004542    }
004543  }
004544  
004545  #ifndef SQLITE_OMIT_VIRTUALTABLE
004546  /*
004547  ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
004548  ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
004549  ** in memory obtained from sqlite3DbMalloc).
004550  */
004551  void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
004552    if( pVtab->zErrMsg ){
004553      sqlite3 *db = p->db;
004554      sqlite3DbFree(db, p->zErrMsg);
004555      p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
004556      sqlite3_free(pVtab->zErrMsg);
004557      pVtab->zErrMsg = 0;
004558    }
004559  }
004560  #endif /* SQLITE_OMIT_VIRTUALTABLE */
004561  
004562  #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
004563  
004564  /*
004565  ** If the second argument is not NULL, release any allocations associated 
004566  ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
004567  ** structure itself, using sqlite3DbFree().
004568  **
004569  ** This function is used to free UnpackedRecord structures allocated by
004570  ** the vdbeUnpackRecord() function found in vdbeapi.c.
004571  */
004572  static void vdbeFreeUnpacked(sqlite3 *db, UnpackedRecord *p){
004573    if( p ){
004574      int i;
004575      for(i=0; i<p->nField; i++){
004576        Mem *pMem = &p->aMem[i];
004577        if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
004578      }
004579      sqlite3DbFree(db, p);
004580    }
004581  }
004582  #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
004583  
004584  #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
004585  /*
004586  ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
004587  ** then cursor passed as the second argument should point to the row about
004588  ** to be update or deleted. If the application calls sqlite3_preupdate_old(),
004589  ** the required value will be read from the row the cursor points to.
004590  */
004591  void sqlite3VdbePreUpdateHook(
004592    Vdbe *v,                        /* Vdbe pre-update hook is invoked by */
004593    VdbeCursor *pCsr,               /* Cursor to grab old.* values from */
004594    int op,                         /* SQLITE_INSERT, UPDATE or DELETE */
004595    const char *zDb,                /* Database name */
004596    Table *pTab,                    /* Modified table */
004597    i64 iKey1,                      /* Initial key value */
004598    int iReg                        /* Register for new.* record */
004599  ){
004600    sqlite3 *db = v->db;
004601    i64 iKey2;
004602    PreUpdate preupdate;
004603    const char *zTbl = pTab->zName;
004604    static const u8 fakeSortOrder = 0;
004605  
004606    assert( db->pPreUpdate==0 );
004607    memset(&preupdate, 0, sizeof(PreUpdate));
004608    if( op==SQLITE_UPDATE ){
004609      iKey2 = v->aMem[iReg].u.i;
004610    }else{
004611      iKey2 = iKey1;
004612    }
004613  
004614    assert( pCsr->nField==pTab->nCol 
004615         || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
004616    );
004617  
004618    preupdate.v = v;
004619    preupdate.pCsr = pCsr;
004620    preupdate.op = op;
004621    preupdate.iNewReg = iReg;
004622    preupdate.keyinfo.db = db;
004623    preupdate.keyinfo.enc = ENC(db);
004624    preupdate.keyinfo.nField = pTab->nCol;
004625    preupdate.keyinfo.aSortOrder = (u8*)&fakeSortOrder;
004626    preupdate.iKey1 = iKey1;
004627    preupdate.iKey2 = iKey2;
004628    preupdate.pTab = pTab;
004629  
004630    db->pPreUpdate = &preupdate;
004631    db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
004632    db->pPreUpdate = 0;
004633    sqlite3DbFree(db, preupdate.aRecord);
004634    vdbeFreeUnpacked(db, preupdate.pUnpacked);
004635    vdbeFreeUnpacked(db, preupdate.pNewUnpacked);
004636    if( preupdate.aNew ){
004637      int i;
004638      for(i=0; i<pCsr->nField; i++){
004639        sqlite3VdbeMemRelease(&preupdate.aNew[i]);
004640      }
004641      sqlite3DbFree(db, preupdate.aNew);
004642    }
004643  }
004644  #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */