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