PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - pdo_sqlite/sqlite/src - vdbeaux.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 791
Code covered: 58.2 % Executed lines: 460
Legend: not executed executed

       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                 : #include "sqliteInt.h"
      18                 : #include "os.h"
      19                 : #include <ctype.h>
      20                 : #include "vdbeInt.h"
      21                 : 
      22                 : 
      23                 : /*
      24                 : ** When debugging the code generator in a symbolic debugger, one can
      25                 : ** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed
      26                 : ** as they are added to the instruction stream.
      27                 : */
      28                 : #ifdef SQLITE_DEBUG
      29                 : int sqlite3_vdbe_addop_trace = 0;
      30                 : #endif
      31                 : 
      32                 : 
      33                 : /*
      34                 : ** Create a new virtual database engine.
      35                 : */
      36             620 : Vdbe *sqlite3VdbeCreate(sqlite3 *db){
      37                 :   Vdbe *p;
      38             620 :   p = sqliteMalloc( sizeof(Vdbe) );
      39             620 :   if( p==0 ) return 0;
      40             620 :   p->db = db;
      41             620 :   if( db->pVdbe ){
      42             148 :     db->pVdbe->pPrev = p;
      43                 :   }
      44             620 :   p->pNext = db->pVdbe;
      45             620 :   p->pPrev = 0;
      46             620 :   db->pVdbe = p;
      47             620 :   p->magic = VDBE_MAGIC_INIT;
      48             620 :   return p;
      49                 : }
      50                 : 
      51                 : /*
      52                 : ** Remember the SQL string for a prepared statement.
      53                 : */
      54               0 : void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){
      55               0 :   if( p==0 ) return;
      56                 :   assert( p->zSql==0 );
      57               0 :   p->zSql = sqlite3StrNDup(z, n);
      58                 : }
      59                 : 
      60                 : /*
      61                 : ** Return the SQL associated with a prepared statement
      62                 : */
      63               0 : const char *sqlite3VdbeGetSql(Vdbe *p){
      64               0 :   return p->zSql;
      65                 : }
      66                 : 
      67                 : /*
      68                 : ** Swap all content between two VDBE structures.
      69                 : */
      70               0 : void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
      71                 :   Vdbe tmp, *pTmp;
      72                 :   char *zTmp;
      73                 :   int nTmp;
      74               0 :   tmp = *pA;
      75               0 :   *pA = *pB;
      76               0 :   *pB = tmp;
      77               0 :   pTmp = pA->pNext;
      78               0 :   pA->pNext = pB->pNext;
      79               0 :   pB->pNext = pTmp;
      80               0 :   pTmp = pA->pPrev;
      81               0 :   pA->pPrev = pB->pPrev;
      82               0 :   pB->pPrev = pTmp;
      83               0 :   zTmp = pA->zSql;
      84               0 :   pA->zSql = pB->zSql;
      85               0 :   pB->zSql = zTmp;
      86               0 :   nTmp = pA->nSql;
      87               0 :   pA->nSql = pB->nSql;
      88               0 :   pB->nSql = nTmp;
      89               0 : }
      90                 : 
      91                 : /*
      92                 : ** Turn tracing on or off
      93                 : */
      94             620 : void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
      95             620 :   p->trace = trace;
      96             620 : }
      97                 : 
      98                 : /*
      99                 : ** Resize the Vdbe.aOp array so that it contains at least N
     100                 : ** elements. If the Vdbe is in VDBE_MAGIC_RUN state, then
     101                 : ** the Vdbe.aOp array will be sized to contain exactly N
     102                 : ** elements. Vdbe.nOpAlloc is set to reflect the new size of
     103                 : ** the array.
     104                 : **
     105                 : ** If an out-of-memory error occurs while resizing the array,
     106                 : ** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that
     107                 : ** any opcodes already allocated can be correctly deallocated
     108                 : ** along with the rest of the Vdbe).
     109                 : */
     110            1240 : static void resizeOpArray(Vdbe *p, int N){
     111            1240 :   int runMode = p->magic==VDBE_MAGIC_RUN;
     112            1240 :   if( runMode || p->nOpAlloc<N ){
     113                 :     VdbeOp *pNew;
     114            1240 :     int nNew = N + 100*(!runMode);
     115            1240 :     int oldSize = p->nOpAlloc;
     116            1240 :     pNew = sqliteRealloc(p->aOp, nNew*sizeof(Op));
     117            1240 :     if( pNew ){
     118            1240 :       p->nOpAlloc = nNew;
     119            1240 :       p->aOp = pNew;
     120            1240 :       if( nNew>oldSize ){
     121             620 :         memset(&p->aOp[oldSize], 0, (nNew-oldSize)*sizeof(Op));
     122                 :       }
     123                 :     }
     124                 :   }
     125            1240 : }
     126                 : 
     127                 : /*
     128                 : ** Add a new instruction to the list of instructions current in the
     129                 : ** VDBE.  Return the address of the new instruction.
     130                 : **
     131                 : ** Parameters:
     132                 : **
     133                 : **    p               Pointer to the VDBE
     134                 : **
     135                 : **    op              The opcode for this instruction
     136                 : **
     137                 : **    p1, p2          First two of the three possible operands.
     138                 : **
     139                 : ** Use the sqlite3VdbeResolveLabel() function to fix an address and
     140                 : ** the sqlite3VdbeChangeP3() function to change the value of the P3
     141                 : ** operand.
     142                 : */
     143           10511 : int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
     144                 :   int i;
     145                 :   VdbeOp *pOp;
     146                 : 
     147           10511 :   i = p->nOp;
     148                 :   assert( p->magic==VDBE_MAGIC_INIT );
     149           10511 :   if( p->nOpAlloc<=i ){
     150             620 :     resizeOpArray(p, i+1);
     151             620 :     if( sqlite3MallocFailed() ){
     152               0 :       return 0;
     153                 :     }
     154                 :   }
     155           10511 :   p->nOp++;
     156           10511 :   pOp = &p->aOp[i];
     157           10511 :   pOp->opcode = op;
     158           10511 :   pOp->p1 = p1;
     159           10511 :   pOp->p2 = p2;
     160           10511 :   pOp->p3 = 0;
     161           10511 :   pOp->p3type = P3_NOTUSED;
     162           10511 :   p->expired = 0;
     163                 : #ifdef SQLITE_DEBUG
     164                 :   if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
     165                 : #endif
     166           10511 :   return i;
     167                 : }
     168                 : 
     169                 : /*
     170                 : ** Add an opcode that includes the p3 value.
     171                 : */
     172            1578 : int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
     173            1578 :   int addr = sqlite3VdbeAddOp(p, op, p1, p2);
     174            1578 :   sqlite3VdbeChangeP3(p, addr, zP3, p3type);
     175            1578 :   return addr;
     176                 : }
     177                 : 
     178                 : /*
     179                 : ** Create a new symbolic label for an instruction that has yet to be
     180                 : ** coded.  The symbolic label is really just a negative number.  The
     181                 : ** label can be used as the P2 value of an operation.  Later, when
     182                 : ** the label is resolved to a specific address, the VDBE will scan
     183                 : ** through its operation list and change all values of P2 which match
     184                 : ** the label into the resolved address.
     185                 : **
     186                 : ** The VDBE knows that a P2 value is a label because labels are
     187                 : ** always negative and P2 values are suppose to be non-negative.
     188                 : ** Hence, a negative P2 value is a label that has yet to be resolved.
     189                 : **
     190                 : ** Zero is returned if a malloc() fails.
     191                 : */
     192            1045 : int sqlite3VdbeMakeLabel(Vdbe *p){
     193                 :   int i;
     194            1045 :   i = p->nLabel++;
     195                 :   assert( p->magic==VDBE_MAGIC_INIT );
     196            1045 :   if( i>=p->nLabelAlloc ){
     197             336 :     p->nLabelAlloc = p->nLabelAlloc*2 + 10;
     198             336 :     p->aLabel = sqliteReallocOrFree(p->aLabel,
     199                 :                                     p->nLabelAlloc*sizeof(p->aLabel[0]));
     200                 :   }
     201            1045 :   if( p->aLabel ){
     202            1045 :     p->aLabel[i] = -1;
     203                 :   }
     204            1045 :   return -1-i;
     205                 : }
     206                 : 
     207                 : /*
     208                 : ** Resolve label "x" to be the address of the next instruction to
     209                 : ** be inserted.  The parameter "x" must have been obtained from
     210                 : ** a prior call to sqlite3VdbeMakeLabel().
     211                 : */
     212            1045 : void sqlite3VdbeResolveLabel(Vdbe *p, int x){
     213            1045 :   int j = -1-x;
     214                 :   assert( p->magic==VDBE_MAGIC_INIT );
     215                 :   assert( j>=0 && j<p->nLabel );
     216            1045 :   if( p->aLabel ){
     217            1045 :     p->aLabel[j] = p->nOp;
     218                 :   }
     219            1045 : }
     220                 : 
     221                 : /*
     222                 : ** Return non-zero if opcode 'op' is guarenteed not to push more values
     223                 : ** onto the VDBE stack than it pops off.
     224                 : */
     225           10511 : static int opcodeNoPush(u8 op){
     226                 :   /* The 10 NOPUSH_MASK_n constants are defined in the automatically
     227                 :   ** generated header file opcodes.h. Each is a 16-bit bitmask, one
     228                 :   ** bit corresponding to each opcode implemented by the virtual
     229                 :   ** machine in vdbe.c. The bit is true if the word "no-push" appears
     230                 :   ** in a comment on the same line as the "case OP_XXX:" in 
     231                 :   ** sqlite3VdbeExec() in vdbe.c.
     232                 :   **
     233                 :   ** If the bit is true, then the corresponding opcode is guarenteed not
     234                 :   ** to grow the stack when it is executed. Otherwise, it may grow the
     235                 :   ** stack by at most one entry.
     236                 :   **
     237                 :   ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains
     238                 :   ** one bit for opcodes 16 to 31, and so on.
     239                 :   **
     240                 :   ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h 
     241                 :   ** because the file is generated by an awk program. Awk manipulates
     242                 :   ** all numbers as floating-point and we don't want to risk a rounding
     243                 :   ** error if someone builds with an awk that uses (for example) 32-bit 
     244                 :   ** IEEE floats.
     245                 :   */ 
     246                 :   static const u32 masks[5] = {
     247                 :     NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16),
     248                 :     NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16),
     249                 :     NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16),
     250                 :     NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16),
     251                 :     NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16)
     252                 :   };
     253                 :   assert( op<32*5 );
     254           10511 :   return (masks[op>>5] & (1<<(op&0x1F)));
     255                 : }
     256                 : 
     257                 : #ifndef NDEBUG
     258                 : int sqlite3VdbeOpcodeNoPush(u8 op){
     259                 :   return opcodeNoPush(op);
     260                 : }
     261                 : #endif
     262                 : 
     263                 : /*
     264                 : ** Loop through the program looking for P2 values that are negative.
     265                 : ** Each such value is a label.  Resolve the label by setting the P2
     266                 : ** value to its correct non-zero value.
     267                 : **
     268                 : ** This routine is called once after all opcodes have been inserted.
     269                 : **
     270                 : ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument 
     271                 : ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by 
     272                 : ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
     273                 : **
     274                 : ** The integer *pMaxStack is set to the maximum number of vdbe stack
     275                 : ** entries that static analysis reveals this program might need.
     276                 : **
     277                 : ** This routine also does the following optimization:  It scans for
     278                 : ** Halt instructions where P1==SQLITE_CONSTRAINT or P2==OE_Abort or for
     279                 : ** IdxInsert instructions where P2!=0.  If no such instruction is
     280                 : ** found, then every Statement instruction is changed to a Noop.  In
     281                 : ** this way, we avoid creating the statement journal file unnecessarily.
     282                 : */
     283             620 : static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){
     284                 :   int i;
     285             620 :   int nMaxArgs = 0;
     286             620 :   int nMaxStack = p->nOp;
     287                 :   Op *pOp;
     288             620 :   int *aLabel = p->aLabel;
     289             620 :   int doesStatementRollback = 0;
     290             620 :   int hasStatementBegin = 0;
     291           11131 :   for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
     292           10511 :     u8 opcode = pOp->opcode;
     293                 : 
     294           10526 :     if( opcode==OP_Function || opcode==OP_AggStep 
     295                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
     296                 :         || opcode==OP_VUpdate
     297                 : #endif
     298                 :     ){
     299              15 :       if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
     300           10496 :     }else if( opcode==OP_Halt ){
     301             814 :       if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
     302             194 :         doesStatementRollback = 1;
     303                 :       }
     304            9682 :     }else if( opcode==OP_Statement ){
     305              40 :       hasStatementBegin = 1;
     306            9642 :     }else if( opcode==OP_VFilter ){
     307                 :       int n;
     308                 :       assert( p->nOp - i >= 3 );
     309                 :       assert( pOp[-2].opcode==OP_Integer );
     310               0 :       n = pOp[-2].p1;
     311               0 :       if( n>nMaxArgs ) nMaxArgs = n;
     312                 :     }
     313           10511 :     if( opcodeNoPush(opcode) ){
     314            6734 :       nMaxStack--;
     315                 :     }
     316                 : 
     317           10511 :     if( pOp->p2>=0 ) continue;
     318                 :     assert( -1-pOp->p2<p->nLabel );
     319             441 :     pOp->p2 = aLabel[-1-pOp->p2];
     320                 :   }
     321             620 :   sqliteFree(p->aLabel);
     322             620 :   p->aLabel = 0;
     323                 : 
     324             620 :   *pMaxFuncArgs = nMaxArgs;
     325             620 :   *pMaxStack = nMaxStack;
     326                 : 
     327                 :   /* If we never rollback a statement transaction, then statement
     328                 :   ** transactions are not needed.  So change every OP_Statement
     329                 :   ** opcode into an OP_Noop.  This avoid a call to sqlite3OsOpenExclusive()
     330                 :   ** which can be expensive on some platforms.
     331                 :   */
     332             620 :   if( hasStatementBegin && !doesStatementRollback ){
     333            2709 :     for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
     334            2672 :       if( pOp->opcode==OP_Statement ){
     335              40 :         pOp->opcode = OP_Noop;
     336                 :       }
     337                 :     }
     338                 :   }
     339             620 : }
     340                 : 
     341                 : /*
     342                 : ** Return the address of the next instruction to be inserted.
     343                 : */
     344             453 : int sqlite3VdbeCurrentAddr(Vdbe *p){
     345                 :   assert( p->magic==VDBE_MAGIC_INIT );
     346             453 :   return p->nOp;
     347                 : }
     348                 : 
     349                 : /*
     350                 : ** Add a whole list of operations to the operation stack.  Return the
     351                 : ** address of the first operation added.
     352                 : */
     353               0 : int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
     354                 :   int addr;
     355                 :   assert( p->magic==VDBE_MAGIC_INIT );
     356               0 :   resizeOpArray(p, p->nOp + nOp);
     357               0 :   if( sqlite3MallocFailed() ){
     358               0 :     return 0;
     359                 :   }
     360               0 :   addr = p->nOp;
     361               0 :   if( nOp>0 ){
     362                 :     int i;
     363               0 :     VdbeOpList const *pIn = aOp;
     364               0 :     for(i=0; i<nOp; i++, pIn++){
     365               0 :       int p2 = pIn->p2;
     366               0 :       VdbeOp *pOut = &p->aOp[i+addr];
     367               0 :       pOut->opcode = pIn->opcode;
     368               0 :       pOut->p1 = pIn->p1;
     369               0 :       pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
     370               0 :       pOut->p3 = pIn->p3;
     371               0 :       pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
     372                 : #ifdef SQLITE_DEBUG
     373                 :       if( sqlite3_vdbe_addop_trace ){
     374                 :         sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
     375                 :       }
     376                 : #endif
     377                 :     }
     378               0 :     p->nOp += nOp;
     379                 :   }
     380               0 :   return addr;
     381                 : }
     382                 : 
     383                 : /*
     384                 : ** Change the value of the P1 operand for a specific instruction.
     385                 : ** This routine is useful when a large program is loaded from a
     386                 : ** static array using sqlite3VdbeAddOpList but we want to make a
     387                 : ** few minor changes to the program.
     388                 : */
     389               0 : void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
     390                 :   assert( p==0 || p->magic==VDBE_MAGIC_INIT );
     391               0 :   if( p && addr>=0 && p->nOp>addr && p->aOp ){
     392               0 :     p->aOp[addr].p1 = val;
     393                 :   }
     394               0 : }
     395                 : 
     396                 : /*
     397                 : ** Change the value of the P2 operand for a specific instruction.
     398                 : ** This routine is useful for setting a jump destination.
     399                 : */
     400             914 : void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
     401                 :   assert( val>=0 );
     402                 :   assert( p==0 || p->magic==VDBE_MAGIC_INIT );
     403             914 :   if( p && addr>=0 && p->nOp>addr && p->aOp ){
     404             914 :     p->aOp[addr].p2 = val;
     405                 :   }
     406             914 : }
     407                 : 
     408                 : /*
     409                 : ** Change the P2 operand of instruction addr so that it points to
     410                 : ** the address of the next instruction to be coded.
     411                 : */
     412             699 : void sqlite3VdbeJumpHere(Vdbe *p, int addr){
     413             699 :   sqlite3VdbeChangeP2(p, addr, p->nOp);
     414             699 : }
     415                 : 
     416                 : 
     417                 : /*
     418                 : ** If the input FuncDef structure is ephemeral, then free it.  If
     419                 : ** the FuncDef is not ephermal, then do nothing.
     420                 : */
     421              29 : static void freeEphemeralFunction(FuncDef *pDef){
     422              29 :   if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
     423               0 :     sqliteFree(pDef);
     424                 :   }
     425              29 : }
     426                 : 
     427                 : /*
     428                 : ** Delete a P3 value if necessary.
     429                 : */
     430           12646 : static void freeP3(int p3type, void *p3){
     431           12646 :   if( p3 ){
     432            2146 :     switch( p3type ){
     433                 :       case P3_DYNAMIC:
     434                 :       case P3_KEYINFO:
     435                 :       case P3_KEYINFO_HANDOFF: {
     436            1904 :         sqliteFree(p3);
     437            1904 :         break;
     438                 :       }
     439                 :       case P3_MPRINTF: {
     440               0 :         sqlite3_free(p3);
     441               0 :         break;
     442                 :       }
     443                 :       case P3_VDBEFUNC: {
     444               0 :         VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
     445               0 :         freeEphemeralFunction(pVdbeFunc->pFunc);
     446               0 :         sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
     447               0 :         sqliteFree(pVdbeFunc);
     448               0 :         break;
     449                 :       }
     450                 :       case P3_FUNCDEF: {
     451              29 :         freeEphemeralFunction((FuncDef*)p3);
     452              29 :         break;
     453                 :       }
     454                 :       case P3_MEM: {
     455               0 :         sqlite3ValueFree((sqlite3_value*)p3);
     456                 :         break;
     457                 :       }
     458                 :     }
     459                 :   }
     460           12646 : }
     461                 : 
     462                 : 
     463                 : /*
     464                 : ** Change N opcodes starting at addr to No-ops.
     465                 : */
     466               4 : void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
     467               4 :   VdbeOp *pOp = &p->aOp[addr];
     468              12 :   while( N-- ){
     469               4 :     freeP3(pOp->p3type, pOp->p3);
     470               4 :     memset(pOp, 0, sizeof(pOp[0]));
     471               4 :     pOp->opcode = OP_Noop;
     472               4 :     pOp++;
     473                 :   }
     474               4 : }
     475                 : 
     476                 : /*
     477                 : ** Change the value of the P3 operand for a specific instruction.
     478                 : ** This routine is useful when a large program is loaded from a
     479                 : ** static array using sqlite3VdbeAddOpList but we want to make a
     480                 : ** few minor changes to the program.
     481                 : **
     482                 : ** If n>=0 then the P3 operand is dynamic, meaning that a copy of
     483                 : ** the string is made into memory obtained from sqliteMalloc().
     484                 : ** A value of n==0 means copy bytes of zP3 up to and including the
     485                 : ** first null byte.  If n>0 then copy n+1 bytes of zP3.
     486                 : **
     487                 : ** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure.
     488                 : ** A copy is made of the KeyInfo structure into memory obtained from
     489                 : ** sqliteMalloc, to be freed when the Vdbe is finalized.
     490                 : ** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure
     491                 : ** stored in memory that the caller has obtained from sqliteMalloc. The 
     492                 : ** caller should not free the allocation, it will be freed when the Vdbe is
     493                 : ** finalized.
     494                 : ** 
     495                 : ** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points
     496                 : ** to a string or structure that is guaranteed to exist for the lifetime of
     497                 : ** the Vdbe. In these cases we can just copy the pointer.
     498                 : **
     499                 : ** If addr<0 then change P3 on the most recently inserted instruction.
     500                 : */
     501            2147 : void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
     502                 :   Op *pOp;
     503                 :   assert( p==0 || p->magic==VDBE_MAGIC_INIT );
     504            2147 :   if( p==0 || p->aOp==0 || sqlite3MallocFailed() ){
     505               0 :     if (n != P3_KEYINFO) {
     506               0 :       freeP3(n, (void*)*(char**)&zP3);
     507                 :     }
     508               0 :     return;
     509                 :   }
     510            2147 :   if( addr<0 || addr>=p->nOp ){
     511             569 :     addr = p->nOp - 1;
     512             569 :     if( addr<0 ) return;
     513                 :   }
     514            2147 :   pOp = &p->aOp[addr];
     515            2147 :   freeP3(pOp->p3type, pOp->p3);
     516            2147 :   pOp->p3 = 0;
     517            2147 :   if( zP3==0 ){
     518               0 :     pOp->p3 = 0;
     519               0 :     pOp->p3type = P3_NOTUSED;
     520            2147 :   }else if( n==P3_KEYINFO ){
     521                 :     KeyInfo *pKeyInfo;
     522                 :     int nField, nByte;
     523                 : 
     524               0 :     nField = ((KeyInfo*)zP3)->nField;
     525               0 :     nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
     526               0 :     pKeyInfo = sqliteMallocRaw( nByte );
     527               0 :     pOp->p3 = (char*)pKeyInfo;
     528               0 :     if( pKeyInfo ){
     529                 :       unsigned char *aSortOrder;
     530               0 :       memcpy(pKeyInfo, zP3, nByte);
     531               0 :       aSortOrder = pKeyInfo->aSortOrder;
     532               0 :       if( aSortOrder ){
     533               0 :         pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
     534               0 :         memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
     535                 :       }
     536               0 :       pOp->p3type = P3_KEYINFO;
     537                 :     }else{
     538               0 :       pOp->p3type = P3_NOTUSED;
     539                 :     }
     540            2147 :   }else if( n==P3_KEYINFO_HANDOFF ){
     541             112 :     pOp->p3 = (char*)zP3;
     542             112 :     pOp->p3type = P3_KEYINFO;
     543            2035 :   }else if( n<0 ){
     544             395 :     pOp->p3 = (char*)zP3;
     545             395 :     pOp->p3type = n;
     546                 :   }else{
     547            1640 :     if( n==0 ) n = strlen(zP3);
     548            1640 :     pOp->p3 = sqliteStrNDup(zP3, n);
     549            1640 :     pOp->p3type = P3_DYNAMIC;
     550                 :   }
     551                 : }
     552                 : 
     553                 : #ifndef NDEBUG
     554                 : /*
     555                 : ** Replace the P3 field of the most recently coded instruction with
     556                 : ** comment text.
     557                 : */
     558                 : void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
     559                 :   va_list ap;
     560                 :   assert( p->nOp>0 || p->aOp==0 );
     561                 :   assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || sqlite3MallocFailed() );
     562                 :   va_start(ap, zFormat);
     563                 :   sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(zFormat, ap), P3_DYNAMIC);
     564                 :   va_end(ap);
     565                 : }
     566                 : #endif
     567                 : 
     568                 : /*
     569                 : ** Return the opcode for a given address.
     570                 : */
     571               5 : VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
     572                 :   assert( p->magic==VDBE_MAGIC_INIT );
     573                 :   assert( (addr>=0 && addr<p->nOp) || sqlite3MallocFailed() );
     574               5 :   return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0);
     575                 : }
     576                 : 
     577                 : #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
     578                 :      || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
     579                 : /*
     580                 : ** Compute a string that describes the P3 parameter for an opcode.
     581                 : ** Use zTemp for any required temporary buffer space.
     582                 : */
     583               0 : static char *displayP3(Op *pOp, char *zTemp, int nTemp){
     584                 :   char *zP3;
     585                 :   assert( nTemp>=20 );
     586               0 :   switch( pOp->p3type ){
     587                 :     case P3_KEYINFO: {
     588                 :       int i, j;
     589               0 :       KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
     590               0 :       sprintf(zTemp, "keyinfo(%d", pKeyInfo->nField);
     591               0 :       i = strlen(zTemp);
     592               0 :       for(j=0; j<pKeyInfo->nField; j++){
     593               0 :         CollSeq *pColl = pKeyInfo->aColl[j];
     594               0 :         if( pColl ){
     595               0 :           int n = strlen(pColl->zName);
     596               0 :           if( i+n>nTemp-6 ){
     597               0 :             strcpy(&zTemp[i],",...");
     598               0 :             break;
     599                 :           }
     600               0 :           zTemp[i++] = ',';
     601               0 :           if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
     602               0 :             zTemp[i++] = '-';
     603                 :           }
     604               0 :           strcpy(&zTemp[i], pColl->zName);
     605               0 :           i += n;
     606               0 :         }else if( i+4<nTemp-6 ){
     607               0 :           strcpy(&zTemp[i],",nil");
     608               0 :           i += 4;
     609                 :         }
     610                 :       }
     611               0 :       zTemp[i++] = ')';
     612               0 :       zTemp[i] = 0;
     613                 :       assert( i<nTemp );
     614               0 :       zP3 = zTemp;
     615               0 :       break;
     616                 :     }
     617                 :     case P3_COLLSEQ: {
     618               0 :       CollSeq *pColl = (CollSeq*)pOp->p3;
     619               0 :       sprintf(zTemp, "collseq(%.20s)", pColl->zName);
     620               0 :       zP3 = zTemp;
     621               0 :       break;
     622                 :     }
     623                 :     case P3_FUNCDEF: {
     624               0 :       FuncDef *pDef = (FuncDef*)pOp->p3;
     625               0 :       sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
     626               0 :       zP3 = zTemp;
     627               0 :       break;
     628                 :     }
     629                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
     630                 :     case P3_VTAB: {
     631               0 :       sqlite3_vtab *pVtab = (sqlite3_vtab*)pOp->p3;
     632               0 :       sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
     633               0 :       zP3 = zTemp;
     634               0 :       break;
     635                 :     }
     636                 : #endif
     637                 :     default: {
     638               0 :       zP3 = pOp->p3;
     639               0 :       if( zP3==0 || pOp->opcode==OP_Noop ){
     640               0 :         zP3 = "";
     641                 :       }
     642                 :     }
     643                 :   }
     644                 :   assert( zP3!=0 );
     645               0 :   return zP3;
     646                 : }
     647                 : #endif
     648                 : 
     649                 : 
     650                 : #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
     651                 : /*
     652                 : ** Print a single opcode.  This routine is used for debugging only.
     653                 : */
     654                 : void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
     655                 :   char *zP3;
     656                 :   char zPtr[50];
     657                 :   static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
     658                 :   if( pOut==0 ) pOut = stdout;
     659                 :   zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
     660                 :   fprintf(pOut, zFormat1,
     661                 :       pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3);
     662                 :   fflush(pOut);
     663                 : }
     664                 : #endif
     665                 : 
     666                 : /*
     667                 : ** Release an array of N Mem elements
     668                 : */
     669            4223 : static void releaseMemArray(Mem *p, int N){
     670            4223 :   if( p ){
     671           14003 :     while( N-->0 ){
     672            6797 :       sqlite3VdbeMemRelease(p++);
     673                 :     }
     674                 :   }
     675            4223 : }
     676                 : 
     677                 : #ifndef SQLITE_OMIT_EXPLAIN
     678                 : /*
     679                 : ** Give a listing of the program in the virtual machine.
     680                 : **
     681                 : ** The interface is the same as sqlite3VdbeExec().  But instead of
     682                 : ** running the code, it invokes the callback once for each instruction.
     683                 : ** This feature is used to implement "EXPLAIN".
     684                 : */
     685                 : int sqlite3VdbeList(
     686                 :   Vdbe *p                   /* The VDBE */
     687               0 : ){
     688               0 :   sqlite3 *db = p->db;
     689                 :   int i;
     690               0 :   int rc = SQLITE_OK;
     691                 : 
     692                 :   assert( p->explain );
     693               0 :   if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
     694                 :   assert( db->magic==SQLITE_MAGIC_BUSY );
     695                 :   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
     696                 : 
     697                 :   /* Even though this opcode does not put dynamic strings onto the
     698                 :   ** the stack, they may become dynamic if the user calls
     699                 :   ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
     700                 :   */
     701               0 :   if( p->pTos==&p->aStack[4] ){
     702               0 :     releaseMemArray(p->aStack, 5);
     703                 :   }
     704               0 :   p->resOnStack = 0;
     705                 : 
     706                 :   do{
     707               0 :     i = p->pc++;
     708               0 :   }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
     709               0 :   if( i>=p->nOp ){
     710               0 :     p->rc = SQLITE_OK;
     711               0 :     rc = SQLITE_DONE;
     712               0 :   }else if( db->u1.isInterrupted ){
     713               0 :     p->rc = SQLITE_INTERRUPT;
     714               0 :     rc = SQLITE_ERROR;
     715               0 :     sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
     716                 :   }else{
     717               0 :     Op *pOp = &p->aOp[i];
     718               0 :     Mem *pMem = p->aStack;
     719               0 :     pMem->flags = MEM_Int;
     720               0 :     pMem->type = SQLITE_INTEGER;
     721               0 :     pMem->u.i = i;                                /* Program counter */
     722               0 :     pMem++;
     723                 : 
     724               0 :     pMem->flags = MEM_Static|MEM_Str|MEM_Term;
     725               0 :     pMem->z = (char*)sqlite3OpcodeNames[pOp->opcode];  /* Opcode */
     726                 :     assert( pMem->z!=0 );
     727               0 :     pMem->n = strlen(pMem->z);
     728               0 :     pMem->type = SQLITE_TEXT;
     729               0 :     pMem->enc = SQLITE_UTF8;
     730               0 :     pMem++;
     731                 : 
     732               0 :     pMem->flags = MEM_Int;
     733               0 :     pMem->u.i = pOp->p1;                          /* P1 */
     734               0 :     pMem->type = SQLITE_INTEGER;
     735               0 :     pMem++;
     736                 : 
     737               0 :     pMem->flags = MEM_Int;
     738               0 :     pMem->u.i = pOp->p2;                          /* P2 */
     739               0 :     pMem->type = SQLITE_INTEGER;
     740               0 :     pMem++;
     741                 : 
     742               0 :     pMem->flags = MEM_Ephem|MEM_Str|MEM_Term;   /* P3 */
     743               0 :     pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
     744                 :     assert( pMem->z!=0 );
     745               0 :     pMem->n = strlen(pMem->z);
     746               0 :     pMem->type = SQLITE_TEXT;
     747               0 :     pMem->enc = SQLITE_UTF8;
     748                 : 
     749               0 :     p->nResColumn = 5 - 2*(p->explain-1);
     750               0 :     p->pTos = pMem;
     751               0 :     p->rc = SQLITE_OK;
     752               0 :     p->resOnStack = 1;
     753               0 :     rc = SQLITE_ROW;
     754                 :   }
     755               0 :   return rc;
     756                 : }
     757                 : #endif /* SQLITE_OMIT_EXPLAIN */
     758                 : 
     759                 : #ifdef SQLITE_DEBUG
     760                 : /*
     761                 : ** Print the SQL that was used to generate a VDBE program.
     762                 : */
     763                 : void sqlite3VdbePrintSql(Vdbe *p){
     764                 :   int nOp = p->nOp;
     765                 :   VdbeOp *pOp;
     766                 :   if( nOp<1 ) return;
     767                 :   pOp = &p->aOp[nOp-1];
     768                 :   if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
     769                 :     const char *z = pOp->p3;
     770                 :     while( isspace(*(u8*)z) ) z++;
     771                 :     printf("SQL: [%s]\n", z);
     772                 :   }
     773                 : }
     774                 : #endif
     775                 : 
     776                 : #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
     777                 : /*
     778                 : ** Print an IOTRACE message showing SQL content.
     779                 : */
     780                 : void sqlite3VdbeIOTraceSql(Vdbe *p){
     781                 :   int nOp = p->nOp;
     782                 :   VdbeOp *pOp;
     783                 :   if( sqlite3_io_trace==0 ) return;
     784                 :   if( nOp<1 ) return;
     785                 :   pOp = &p->aOp[nOp-1];
     786                 :   if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
     787                 :     char *z = sqlite3StrDup(pOp->p3);
     788                 :     int i, j;
     789                 :     for(i=0; isspace(z[i]); i++){}
     790                 :     for(j=0; z[i]; i++){
     791                 :       if( isspace(z[i]) ){
     792                 :         if( z[i-1]!=' ' ){
     793                 :           z[j++] = ' ';
     794                 :         }
     795                 :       }else{
     796                 :         z[j++] = z[i];
     797                 :       }
     798                 :     }
     799                 :     z[j] = 0;
     800                 :     sqlite3_io_trace("SQL %s\n", z);
     801                 :     sqliteFree(z);
     802                 :   }
     803                 : }
     804                 : #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
     805                 : 
     806                 : 
     807                 : /*
     808                 : ** Prepare a virtual machine for execution.  This involves things such
     809                 : ** as allocating stack space and initializing the program counter.
     810                 : ** After the VDBE has be prepped, it can be executed by one or more
     811                 : ** calls to sqlite3VdbeExec().  
     812                 : **
     813                 : ** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
     814                 : ** VDBE_MAGIC_RUN.
     815                 : */
     816                 : void sqlite3VdbeMakeReady(
     817                 :   Vdbe *p,                       /* The VDBE */
     818                 :   int nVar,                      /* Number of '?' see in the SQL statement */
     819                 :   int nMem,                      /* Number of memory cells to allocate */
     820                 :   int nCursor,                   /* Number of cursors to allocate */
     821                 :   int isExplain                  /* True if the EXPLAIN keywords is present */
     822             798 : ){
     823                 :   int n;
     824                 : 
     825                 :   assert( p!=0 );
     826                 :   assert( p->magic==VDBE_MAGIC_INIT );
     827                 : 
     828                 :   /* There should be at least one opcode.
     829                 :   */
     830                 :   assert( p->nOp>0 );
     831                 : 
     832                 :   /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
     833                 :    * is because the call to resizeOpArray() below may shrink the
     834                 :    * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN 
     835                 :    * state.
     836                 :    */
     837             798 :   p->magic = VDBE_MAGIC_RUN;
     838                 : 
     839                 :   /* No instruction ever pushes more than a single element onto the
     840                 :   ** stack.  And the stack never grows on successive executions of the
     841                 :   ** same loop.  So the total number of instructions is an upper bound
     842                 :   ** on the maximum stack depth required.  (Added later:)  The
     843                 :   ** resolveP2Values() call computes a tighter upper bound on the
     844                 :   ** stack size.
     845                 :   **
     846                 :   ** Allocation all the stack space we will ever need.
     847                 :   */
     848             798 :   if( p->aStack==0 ){
     849                 :     int nArg;       /* Maximum number of args passed to a user function. */
     850                 :     int nStack;     /* Maximum number of stack entries required */
     851             620 :     resolveP2Values(p, &nArg, &nStack);
     852             620 :     resizeOpArray(p, p->nOp);
     853                 :     assert( nVar>=0 );
     854                 :     assert( nStack<p->nOp );
     855             620 :     if( isExplain ){
     856               0 :       nStack = 10;
     857                 :     }
     858             620 :     p->aStack = sqliteMalloc(
     859                 :         nStack*sizeof(p->aStack[0])    /* aStack */
     860                 :       + nArg*sizeof(Mem*)              /* apArg */
     861                 :       + nVar*sizeof(Mem)               /* aVar */
     862                 :       + nVar*sizeof(char*)             /* azVar */
     863                 :       + nMem*sizeof(Mem)               /* aMem */
     864                 :       + nCursor*sizeof(Cursor*)        /* apCsr */
     865                 :     );
     866             620 :     if( !sqlite3MallocFailed() ){
     867             620 :       p->aMem = &p->aStack[nStack];
     868             620 :       p->nMem = nMem;
     869             620 :       p->aVar = &p->aMem[nMem];
     870             620 :       p->nVar = nVar;
     871             620 :       p->okVar = 0;
     872             620 :       p->apArg = (Mem**)&p->aVar[nVar];
     873             620 :       p->azVar = (char**)&p->apArg[nArg];
     874             620 :       p->apCsr = (Cursor**)&p->azVar[nVar];
     875             620 :       p->nCursor = nCursor;
     876             670 :       for(n=0; n<nVar; n++){
     877              50 :         p->aVar[n].flags = MEM_Null;
     878                 :       }
     879                 :     }
     880                 :   }
     881            3505 :   for(n=0; n<p->nMem; n++){
     882            2707 :     p->aMem[n].flags = MEM_Null;
     883                 :   }
     884                 : 
     885             798 :   p->pTos = &p->aStack[-1];
     886             798 :   p->pc = -1;
     887             798 :   p->rc = SQLITE_OK;
     888             798 :   p->uniqueCnt = 0;
     889             798 :   p->returnDepth = 0;
     890             798 :   p->errorAction = OE_Abort;
     891             798 :   p->popStack =  0;
     892             798 :   p->explain |= isExplain;
     893             798 :   p->magic = VDBE_MAGIC_RUN;
     894             798 :   p->nChange = 0;
     895             798 :   p->cacheCtr = 1;
     896             798 :   p->minWriteFileFormat = 255;
     897                 : #ifdef VDBE_PROFILE
     898                 :   {
     899                 :     int i;
     900                 :     for(i=0; i<p->nOp; i++){
     901                 :       p->aOp[i].cnt = 0;
     902                 :       p->aOp[i].cycles = 0;
     903                 :     }
     904                 :   }
     905                 : #endif
     906             798 : }
     907                 : 
     908                 : /*
     909                 : ** Close a cursor and release all the resources that cursor happens
     910                 : ** to hold.
     911                 : */
     912           11795 : void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){
     913           11795 :   if( pCx==0 ){
     914           11066 :     return;
     915                 :   }
     916             729 :   if( pCx->pCursor ){
     917             729 :     sqlite3BtreeCloseCursor(pCx->pCursor);
     918                 :   }
     919             729 :   if( pCx->pBt ){
     920               0 :     sqlite3BtreeClose(pCx->pBt);
     921                 :   }
     922                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
     923             729 :   if( pCx->pVtabCursor ){
     924               0 :     sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
     925               0 :     const sqlite3_module *pModule = pCx->pModule;
     926               0 :     p->inVtabMethod = 1;
     927               0 :     sqlite3SafetyOff(p->db);
     928               0 :     pModule->xClose(pVtabCursor);
     929               0 :     sqlite3SafetyOn(p->db);
     930               0 :     p->inVtabMethod = 0;
     931                 :   }
     932                 : #endif
     933             729 :   sqliteFree(pCx->pData);
     934             729 :   sqliteFree(pCx->aType);
     935             729 :   sqliteFree(pCx);
     936                 : }
     937                 : 
     938                 : /*
     939                 : ** Close all cursors
     940                 : */
     941            2843 : static void closeAllCursors(Vdbe *p){
     942                 :   int i;
     943            2843 :   if( p->apCsr==0 ) return;
     944           13910 :   for(i=0; i<p->nCursor; i++){
     945           11067 :     if( !p->inVtabMethod || (p->apCsr[i] && !p->apCsr[i]->pVtabCursor) ){
     946           11067 :       sqlite3VdbeFreeCursor(p, p->apCsr[i]);
     947           11067 :       p->apCsr[i] = 0;
     948                 :     }
     949                 :   }
     950                 : }
     951                 : 
     952                 : /*
     953                 : ** Clean up the VM after execution.
     954                 : **
     955                 : ** This routine will automatically close any cursors, lists, and/or
     956                 : ** sorters that were left open.  It also deletes the values of
     957                 : ** variables in the aVar[] array.
     958                 : */
     959            1416 : static void Cleanup(Vdbe *p){
     960                 :   int i;
     961            1416 :   if( p->aStack ){
     962            1416 :     releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
     963            1416 :     p->pTos = &p->aStack[-1];
     964                 :   }
     965            1416 :   closeAllCursors(p);
     966            1416 :   releaseMemArray(p->aMem, p->nMem);
     967            1416 :   sqlite3VdbeFifoClear(&p->sFifo);
     968            1416 :   if( p->contextStack ){
     969               0 :     for(i=0; i<p->contextStackTop; i++){
     970               0 :       sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
     971                 :     }
     972               0 :     sqliteFree(p->contextStack);
     973                 :   }
     974            1416 :   p->contextStack = 0;
     975            1416 :   p->contextStackDepth = 0;
     976            1416 :   p->contextStackTop = 0;
     977            1416 :   sqliteFree(p->zErrMsg);
     978            1416 :   p->zErrMsg = 0;
     979            1416 : }
     980                 : 
     981                 : /*
     982                 : ** Set the number of result columns that will be returned by this SQL
     983                 : ** statement. This is now set at compile time, rather than during
     984                 : ** execution of the vdbe program so that sqlite3_column_count() can
     985                 : ** be called on an SQL statement before sqlite3_step().
     986                 : */
     987             153 : void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
     988                 :   Mem *pColName;
     989                 :   int n;
     990             153 :   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
     991             153 :   sqliteFree(p->aColName);
     992             153 :   n = nResColumn*COLNAME_N;
     993             153 :   p->nResColumn = nResColumn;
     994             153 :   p->aColName = pColName = (Mem*)sqliteMalloc( sizeof(Mem)*n );
     995             153 :   if( p->aColName==0 ) return;
     996            2071 :   while( n-- > 0 ){
     997            1765 :     (pColName++)->flags = MEM_Null;
     998                 :   }
     999                 : }
    1000                 : 
    1001                 : /*
    1002                 : ** Set the name of the idx'th column to be returned by the SQL statement.
    1003                 : ** zName must be a pointer to a nul terminated string.
    1004                 : **
    1005                 : ** This call must be made after a call to sqlite3VdbeSetNumCols().
    1006                 : **
    1007                 : ** If N==P3_STATIC  it means that zName is a pointer to a constant static
    1008                 : ** string and we can just copy the pointer. If it is P3_DYNAMIC, then 
    1009                 : ** the string is freed using sqliteFree() when the vdbe is finished with
    1010                 : ** it. Otherwise, N bytes of zName are copied.
    1011                 : */
    1012            1765 : int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
    1013                 :   int rc;
    1014                 :   Mem *pColName;
    1015                 :   assert( idx<p->nResColumn );
    1016                 :   assert( var<COLNAME_N );
    1017            1765 :   if( sqlite3MallocFailed() ) return SQLITE_NOMEM;
    1018                 :   assert( p->aColName!=0 );
    1019            1765 :   pColName = &(p->aColName[idx+var*p->nResColumn]);
    1020            1765 :   if( N==P3_DYNAMIC || N==P3_STATIC ){
    1021               0 :     rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
    1022                 :   }else{
    1023            1765 :     rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
    1024                 :   }
    1025            1765 :   if( rc==SQLITE_OK && N==P3_DYNAMIC ){
    1026               0 :     pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
    1027               0 :     pColName->xDel = 0;
    1028                 :   }
    1029            1765 :   return rc;
    1030                 : }
    1031                 : 
    1032                 : /*
    1033                 : ** A read or write transaction may or may not be active on database handle
    1034                 : ** db. If a transaction is active, commit it. If there is a
    1035                 : ** write-transaction spanning more than one database file, this routine
    1036                 : ** takes care of the master journal trickery.
    1037                 : */
    1038             562 : static int vdbeCommit(sqlite3 *db){
    1039                 :   int i;
    1040             562 :   int nTrans = 0;  /* Number of databases with an active write-transaction */
    1041             562 :   int rc = SQLITE_OK;
    1042             562 :   int needXcommit = 0;
    1043                 : 
    1044                 :   /* Before doing anything else, call the xSync() callback for any
    1045                 :   ** virtual module tables written in this transaction. This has to
    1046                 :   ** be done before determining whether a master journal file is 
    1047                 :   ** required, as an xSync() callback may add an attached database
    1048                 :   ** to the transaction.
    1049                 :   */
    1050             562 :   rc = sqlite3VtabSync(db, rc);
    1051             562 :   if( rc!=SQLITE_OK ){
    1052               0 :     return rc;
    1053                 :   }
    1054                 : 
    1055                 :   /* This loop determines (a) if the commit hook should be invoked and
    1056                 :   ** (b) how many database files have open write transactions, not 
    1057                 :   ** including the temp database. (b) is important because if more than 
    1058                 :   ** one database file has an open write transaction, a master journal
    1059                 :   ** file is required for an atomic commit.
    1060                 :   */ 
    1061            1686 :   for(i=0; i<db->nDb; i++){ 
    1062            1124 :     Btree *pBt = db->aDb[i].pBt;
    1063            1124 :     if( pBt && sqlite3BtreeIsInTrans(pBt) ){
    1064             209 :       needXcommit = 1;
    1065             209 :       if( i!=1 ) nTrans++;
    1066                 :     }
    1067                 :   }
    1068                 : 
    1069                 :   /* If there are any write-transactions at all, invoke the commit hook */
    1070             562 :   if( needXcommit && db->xCommitCallback ){
    1071               0 :     sqlite3SafetyOff(db);
    1072               0 :     rc = db->xCommitCallback(db->pCommitArg);
    1073               0 :     sqlite3SafetyOn(db);
    1074               0 :     if( rc ){
    1075               0 :       return SQLITE_CONSTRAINT;
    1076                 :     }
    1077                 :   }
    1078                 : 
    1079                 :   /* The simple case - no more than one database file (not counting the
    1080                 :   ** TEMP database) has a transaction active.   There is no need for the
    1081                 :   ** master-journal.
    1082                 :   **
    1083                 :   ** If the return value of sqlite3BtreeGetFilename() is a zero length
    1084                 :   ** string, it means the main database is :memory:.  In that case we do
    1085                 :   ** not support atomic multi-file commits, so use the simple case then
    1086                 :   ** too.
    1087                 :   */
    1088            1124 :   if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
    1089            1686 :     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
    1090            1124 :       Btree *pBt = db->aDb[i].pBt;
    1091            1124 :       if( pBt ){
    1092             562 :         rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
    1093                 :       }
    1094                 :     }
    1095                 : 
    1096                 :     /* Do the commit only if all databases successfully complete phase 1. 
    1097                 :     ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
    1098                 :     ** IO error while deleting or truncating a journal file. It is unlikely,
    1099                 :     ** but could happen. In this case abandon processing and return the error.
    1100                 :     */
    1101            1686 :     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
    1102            1124 :       Btree *pBt = db->aDb[i].pBt;
    1103            1124 :       if( pBt ){
    1104             562 :         rc = sqlite3BtreeCommitPhaseTwo(pBt);
    1105                 :       }
    1106                 :     }
    1107             562 :     if( rc==SQLITE_OK ){
    1108             562 :       sqlite3VtabCommit(db);
    1109                 :     }
    1110                 :   }
    1111                 : 
    1112                 :   /* The complex case - There is a multi-file write-transaction active.
    1113                 :   ** This requires a master journal file to ensure the transaction is
    1114                 :   ** committed atomicly.
    1115                 :   */
    1116                 : #ifndef SQLITE_OMIT_DISKIO
    1117                 :   else{
    1118               0 :     int needSync = 0;
    1119               0 :     char *zMaster = 0;   /* File-name for the master journal */
    1120               0 :     char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
    1121               0 :     OsFile *master = 0;
    1122                 : 
    1123                 :     /* Select a master journal file name */
    1124                 :     do {
    1125                 :       u32 random;
    1126               0 :       sqliteFree(zMaster);
    1127               0 :       sqlite3Randomness(sizeof(random), &random);
    1128               0 :       zMaster = sqlite3MPrintf("%s-mj%08X", zMainFile, random&0x7fffffff);
    1129               0 :       if( !zMaster ){
    1130               0 :         return SQLITE_NOMEM;
    1131                 :       }
    1132               0 :     }while( sqlite3OsFileExists(zMaster) );
    1133                 : 
    1134                 :     /* Open the master journal. */
    1135               0 :     rc = sqlite3OsOpenExclusive(zMaster, &master, 0);
    1136               0 :     if( rc!=SQLITE_OK ){
    1137               0 :       sqliteFree(zMaster);
    1138               0 :       return rc;
    1139                 :     }
    1140                 :  
    1141                 :     /* Write the name of each database file in the transaction into the new
    1142                 :     ** master journal file. If an error occurs at this point close
    1143                 :     ** and delete the master journal file. All the individual journal files
    1144                 :     ** still have 'null' as the master journal pointer, so they will roll
    1145                 :     ** back independently if a failure occurs.
    1146                 :     */
    1147               0 :     for(i=0; i<db->nDb; i++){ 
    1148               0 :       Btree *pBt = db->aDb[i].pBt;
    1149               0 :       if( i==1 ) continue;   /* Ignore the TEMP database */
    1150               0 :       if( pBt && sqlite3BtreeIsInTrans(pBt) ){
    1151               0 :         char const *zFile = sqlite3BtreeGetJournalname(pBt);
    1152               0 :         if( zFile[0]==0 ) continue;  /* Ignore :memory: databases */
    1153               0 :         if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
    1154               0 :           needSync = 1;
    1155                 :         }
    1156               0 :         rc = sqlite3OsWrite(master, zFile, strlen(zFile)+1);
    1157               0 :         if( rc!=SQLITE_OK ){
    1158               0 :           sqlite3OsClose(&master);
    1159               0 :           sqlite3OsDelete(zMaster);
    1160               0 :           sqliteFree(zMaster);
    1161               0 :           return rc;
    1162                 :         }
    1163                 :       }
    1164                 :     }
    1165                 : 
    1166                 : 
    1167                 :     /* Sync the master journal file. Before doing this, open the directory
    1168                 :     ** the master journal file is store in so that it gets synced too.
    1169                 :     */
    1170               0 :     zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
    1171               0 :     rc = sqlite3OsOpenDirectory(master, zMainFile);
    1172               0 :     if( rc!=SQLITE_OK ||
    1173                 :           (needSync && (rc=sqlite3OsSync(master,0))!=SQLITE_OK) ){
    1174               0 :       sqlite3OsClose(&master);
    1175               0 :       sqlite3OsDelete(zMaster);
    1176               0 :       sqliteFree(zMaster);
    1177               0 :       return rc;
    1178                 :     }
    1179                 : 
    1180                 :     /* Sync all the db files involved in the transaction. The same call
    1181                 :     ** sets the master journal pointer in each individual journal. If
    1182                 :     ** an error occurs here, do not delete the master journal file.
    1183                 :     **
    1184                 :     ** If the error occurs during the first call to
    1185                 :     ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
    1186                 :     ** master journal file will be orphaned. But we cannot delete it,
    1187                 :     ** in case the master journal file name was written into the journal
    1188                 :     ** file before the failure occured.
    1189                 :     */
    1190               0 :     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
    1191               0 :       Btree *pBt = db->aDb[i].pBt;
    1192               0 :       if( pBt && sqlite3BtreeIsInTrans(pBt) ){
    1193               0 :         rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
    1194                 :       }
    1195                 :     }
    1196               0 :     sqlite3OsClose(&master);
    1197               0 :     if( rc!=SQLITE_OK ){
    1198               0 :       sqliteFree(zMaster);
    1199               0 :       return rc;
    1200                 :     }
    1201                 : 
    1202                 :     /* Delete the master journal file. This commits the transaction. After
    1203                 :     ** doing this the directory is synced again before any individual
    1204                 :     ** transaction files are deleted.
    1205                 :     */
    1206               0 :     rc = sqlite3OsDelete(zMaster);
    1207               0 :     sqliteFree(zMaster);
    1208               0 :     zMaster = 0;
    1209               0 :     if( rc ){
    1210               0 :       return rc;
    1211                 :     }
    1212               0 :     rc = sqlite3OsSyncDirectory(zMainFile);
    1213               0 :     if( rc!=SQLITE_OK ){
    1214                 :       /* This is not good. The master journal file has been deleted, but
    1215                 :       ** the directory sync failed. There is no completely safe course of
    1216                 :       ** action from here. The individual journals contain the name of the
    1217                 :       ** master journal file, but there is no way of knowing if that
    1218                 :       ** master journal exists now or if it will exist after the operating
    1219                 :       ** system crash that may follow the fsync() failure.
    1220                 :       */
    1221               0 :       return rc;
    1222                 :     }
    1223                 : 
    1224                 :     /* All files and directories have already been synced, so the following
    1225                 :     ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
    1226                 :     ** deleting or truncating journals. If something goes wrong while
    1227                 :     ** this is happening we don't really care. The integrity of the
    1228                 :     ** transaction is already guaranteed, but some stray 'cold' journals
    1229                 :     ** may be lying around. Returning an error code won't help matters.
    1230                 :     */
    1231                 :     disable_simulated_io_errors();
    1232               0 :     for(i=0; i<db->nDb; i++){ 
    1233               0 :       Btree *pBt = db->aDb[i].pBt;
    1234               0 :       if( pBt ){
    1235               0 :         sqlite3BtreeCommitPhaseTwo(pBt);
    1236                 :       }
    1237                 :     }
    1238                 :     enable_simulated_io_errors();
    1239                 : 
    1240               0 :     sqlite3VtabCommit(db);
    1241                 :   }
    1242                 : #endif
    1243                 : 
    1244             562 :   return rc;
    1245                 : }
    1246                 : 
    1247                 : /* 
    1248                 : ** This routine checks that the sqlite3.activeVdbeCnt count variable
    1249                 : ** matches the number of vdbe's in the list sqlite3.pVdbe that are
    1250                 : ** currently active. An assertion fails if the two counts do not match.
    1251                 : ** This is an internal self-check only - it is not an essential processing
    1252                 : ** step.
    1253                 : **
    1254                 : ** This is a no-op if NDEBUG is defined.
    1255                 : */
    1256                 : #ifndef NDEBUG
    1257                 : static void checkActiveVdbeCnt(sqlite3 *db){
    1258                 :   Vdbe *p;
    1259                 :   int cnt = 0;
    1260                 :   p = db->pVdbe;
    1261                 :   while( p ){
    1262                 :     if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
    1263                 :       cnt++;
    1264                 :     }
    1265                 :     p = p->pNext;
    1266                 :   }
    1267                 :   assert( cnt==db->activeVdbeCnt );
    1268                 : }
    1269                 : #else
    1270                 : #define checkActiveVdbeCnt(x)
    1271                 : #endif
    1272                 : 
    1273                 : /*
    1274                 : ** Find every active VM other than pVdbe and change its status to
    1275                 : ** aborted.  This happens when one VM causes a rollback due to an
    1276                 : ** ON CONFLICT ROLLBACK clause (for example).  The other VMs must be
    1277                 : ** aborted so that they do not have data rolled out from underneath
    1278                 : ** them leading to a segfault.
    1279                 : */
    1280               0 : void sqlite3AbortOtherActiveVdbes(sqlite3 *db, Vdbe *pExcept){
    1281                 :   Vdbe *pOther;
    1282               0 :   for(pOther=db->pVdbe; pOther; pOther=pOther->pNext){
    1283               0 :     if( pOther==pExcept ) continue;
    1284               0 :     if( pOther->magic!=VDBE_MAGIC_RUN || pOther->pc<0 ) continue;
    1285                 :     checkActiveVdbeCnt(db);
    1286               0 :     closeAllCursors(pOther);
    1287                 :     checkActiveVdbeCnt(db);
    1288               0 :     pOther->aborted = 1;
    1289                 :   }
    1290               0 : }
    1291                 : 
    1292                 : /*
    1293                 : ** This routine is called the when a VDBE tries to halt.  If the VDBE
    1294                 : ** has made changes and is in autocommit mode, then commit those
    1295                 : ** changes.  If a rollback is needed, then do the rollback.
    1296                 : **
    1297                 : ** This routine is the only way to move the state of a VM from
    1298                 : ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.
    1299                 : **
    1300                 : ** Return an error code.  If the commit could not complete because of
    1301                 : ** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
    1302                 : ** means the close did not happen and needs to be repeated.
    1303                 : */
    1304            1427 : int sqlite3VdbeHalt(Vdbe *p){
    1305            1427 :   sqlite3 *db = p->db;
    1306                 :   int i;
    1307            1427 :   int (*xFunc)(Btree *pBt) = 0;  /* Function to call on each btree backend */
    1308                 :   int isSpecialError;            /* Set to true if SQLITE_NOMEM or IOERR */
    1309                 : 
    1310                 :   /* This function contains the logic that determines if a statement or
    1311                 :   ** transaction will be committed or rolled back as a result of the
    1312                 :   ** execution of this virtual machine. 
    1313                 :   **
    1314                 :   ** Special errors:
    1315                 :   **
    1316                 :   **     If an SQLITE_NOMEM error has occured in a statement that writes to
    1317                 :   **     the database, then either a statement or transaction must be rolled
    1318                 :   **     back to ensure the tree-structures are in a consistent state. A
    1319                 :   **     statement transaction is rolled back if one is open, otherwise the
    1320                 :   **     entire transaction must be rolled back.
    1321                 :   **
    1322                 :   **     If an SQLITE_IOERR error has occured in a statement that writes to
    1323                 :   **     the database, then the entire transaction must be rolled back. The
    1324                 :   **     I/O error may have caused garbage to be written to the journal 
    1325                 :   **     file. Were the transaction to continue and eventually be rolled 
    1326                 :   **     back that garbage might end up in the database file.
    1327                 :   **     
    1328                 :   **     In both of the above cases, the Vdbe.errorAction variable is 
    1329                 :   **     ignored. If the sqlite3.autoCommit flag is false and a transaction
    1330                 :   **     is rolled back, it will be set to true.
    1331                 :   **
    1332                 :   ** Other errors:
    1333                 :   **
    1334                 :   ** No error:
    1335                 :   **
    1336                 :   */
    1337                 : 
    1338            1427 :   if( sqlite3MallocFailed() ){
    1339               0 :     p->rc = SQLITE_NOMEM;
    1340                 :   }
    1341            1427 :   if( p->magic!=VDBE_MAGIC_RUN ){
    1342                 :     /* Already halted.  Nothing to do. */
    1343                 :     assert( p->magic==VDBE_MAGIC_HALT );
    1344                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
    1345             630 :     closeAllCursors(p);
    1346                 : #endif
    1347             630 :     return SQLITE_OK;
    1348                 :   }
    1349             797 :   closeAllCursors(p);
    1350                 :   checkActiveVdbeCnt(db);
    1351                 : 
    1352                 :   /* No commit or rollback needed if the program never started */
    1353             797 :   if( p->pc>=0 ){
    1354                 :     int mrc;   /* Primary error code from p->rc */
    1355                 :     /* Check for one of the special errors - SQLITE_NOMEM or SQLITE_IOERR */
    1356             694 :     mrc = p->rc & 0xff;
    1357             694 :     isSpecialError = ((mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR)?1:0);
    1358             694 :     if( isSpecialError ){
    1359                 :       /* This loop does static analysis of the query to see which of the
    1360                 :       ** following three categories it falls into:
    1361                 :       **
    1362                 :       **     Read-only
    1363                 :       **     Query with statement journal
    1364                 :       **     Query without statement journal
    1365                 :       **
    1366                 :       ** We could do something more elegant than this static analysis (i.e.
    1367                 :       ** store the type of query as part of the compliation phase), but 
    1368                 :       ** handling malloc() or IO failure is a fairly obscure edge case so 
    1369                 :       ** this is probably easier. Todo: Might be an opportunity to reduce 
    1370                 :       ** code size a very small amount though...
    1371                 :       */
    1372               0 :       int isReadOnly = 1;
    1373               0 :       int isStatement = 0;
    1374                 :       assert(p->aOp || p->nOp==0);
    1375               0 :       for(i=0; i<p->nOp; i++){ 
    1376               0 :         switch( p->aOp[i].opcode ){
    1377                 :           case OP_Transaction:
    1378               0 :             isReadOnly = 0;
    1379               0 :             break;
    1380                 :           case OP_Statement:
    1381               0 :             isStatement = 1;
    1382                 :             break;
    1383                 :         }
    1384                 :       }
    1385                 :   
    1386                 :       /* If the query was read-only, we need do no rollback at all. Otherwise,
    1387                 :       ** proceed with the special handling.
    1388                 :       */
    1389               0 :       if( !isReadOnly ){
    1390               0 :         if( p->rc==SQLITE_NOMEM && isStatement ){
    1391               0 :           xFunc = sqlite3BtreeRollbackStmt;
    1392                 :         }else{
    1393                 :           /* We are forced to roll back the active transaction. Before doing
    1394                 :           ** so, abort any other statements this handle currently has active.
    1395                 :           */
    1396               0 :           sqlite3AbortOtherActiveVdbes(db, p);
    1397               0 :           sqlite3RollbackAll(db);
    1398               0 :           db->autoCommit = 1;
    1399                 :         }
    1400                 :       }
    1401                 :     }
    1402                 :   
    1403                 :     /* If the auto-commit flag is set and this is the only active vdbe, then
    1404                 :     ** we do either a commit or rollback of the current transaction. 
    1405                 :     **
    1406                 :     ** Note: This block also runs if one of the special errors handled 
    1407                 :     ** above has occured. 
    1408                 :     */
    1409            1256 :     if( db->autoCommit && db->activeVdbeCnt==1 ){
    1410            1124 :       if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
    1411                 :         /* The auto-commit flag is true, and the vdbe program was 
    1412                 :         ** successful or hit an 'OR FAIL' constraint. This means a commit 
    1413                 :         ** is required.
    1414                 :         */
    1415             562 :         int rc = vdbeCommit(db);
    1416             562 :         if( rc==SQLITE_BUSY ){
    1417               0 :           return SQLITE_BUSY;
    1418             562 :         }else if( rc!=SQLITE_OK ){
    1419               0 :           p->rc = rc;
    1420               0 :           sqlite3RollbackAll(db);
    1421                 :         }else{
    1422             562 :           sqlite3CommitInternalChanges(db);
    1423                 :         }
    1424                 :       }else{
    1425               0 :         sqlite3RollbackAll(db);
    1426                 :       }
    1427             132 :     }else if( !xFunc ){
    1428             263 :       if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
    1429             131 :         xFunc = sqlite3BtreeCommitStmt;
    1430               1 :       }else if( p->errorAction==OE_Abort ){
    1431               1 :         xFunc = sqlite3BtreeRollbackStmt;
    1432                 :       }else{
    1433               0 :         sqlite3AbortOtherActiveVdbes(db, p);
    1434               0 :         sqlite3RollbackAll(db);
    1435               0 :         db->autoCommit = 1;
    1436                 :       }
    1437                 :     }
    1438                 :   
    1439                 :     /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
    1440                 :     ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
    1441                 :     ** and the return code is still SQLITE_OK, set the return code to the new
    1442                 :     ** error value.
    1443                 :     */
    1444                 :     assert(!xFunc ||
    1445                 :       xFunc==sqlite3BtreeCommitStmt ||
    1446                 :       xFunc==sqlite3BtreeRollbackStmt
    1447                 :     );
    1448             958 :     for(i=0; xFunc && i<db->nDb; i++){ 
    1449                 :       int rc;
    1450             264 :       Btree *pBt = db->aDb[i].pBt;
    1451             264 :       if( pBt ){
    1452             132 :         rc = xFunc(pBt);
    1453             132 :         if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
    1454               0 :           p->rc = rc;
    1455               0 :           sqlite3SetString(&p->zErrMsg, 0);
    1456                 :         }
    1457                 :       }
    1458                 :     }
    1459                 :   
    1460                 :     /* If this was an INSERT, UPDATE or DELETE and the statement was committed, 
    1461                 :     ** set the change counter. 
    1462                 :     */
    1463             694 :     if( p->changeCntOn && p->pc>=0 ){
    1464             300 :       if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
    1465             150 :         sqlite3VdbeSetChanges(db, p->nChange);
    1466                 :       }else{
    1467               0 :         sqlite3VdbeSetChanges(db, 0);
    1468                 :       }
    1469             150 :       p->nChange = 0;
    1470                 :     }
    1471                 :   
    1472                 :     /* Rollback or commit any schema changes that occurred. */
    1473             694 :     if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
    1474               0 :       sqlite3ResetInternalSchema(db, 0);
    1475               0 :       db->flags = (db->flags | SQLITE_InternChanges);
    1476                 :     }
    1477                 :   }
    1478                 : 
    1479                 :   /* We have successfully halted and closed the VM.  Record this fact. */
    1480             797 :   if( p->pc>=0 ){
    1481             694 :     db->activeVdbeCnt--;
    1482                 :   }
    1483             797 :   p->magic = VDBE_MAGIC_HALT;
    1484                 :   checkActiveVdbeCnt(db);
    1485                 : 
    1486             797 :   return SQLITE_OK;
    1487                 : }
    1488                 : 
    1489                 : /*
    1490                 : ** Each VDBE holds the result of the most recent sqlite3_step() call
    1491                 : ** in p->rc.  This routine sets that result back to SQLITE_OK.
    1492                 : */
    1493               0 : void sqlite3VdbeResetStepResult(Vdbe *p){
    1494               0 :   p->rc = SQLITE_OK;
    1495               0 : }
    1496                 : 
    1497                 : /*
    1498                 : ** Clean up a VDBE after execution but do not delete the VDBE just yet.
    1499                 : ** Write any error messages into *pzErrMsg.  Return the result code.
    1500                 : **
    1501                 : ** After this routine is run, the VDBE should be ready to be executed
    1502                 : ** again.
    1503                 : **
    1504                 : ** To look at it another way, this routine resets the state of the
    1505                 : ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
    1506                 : ** VDBE_MAGIC_INIT.
    1507                 : */
    1508             797 : int sqlite3VdbeReset(Vdbe *p){
    1509                 :   sqlite3 *db;
    1510             797 :   db = p->db;
    1511                 : 
    1512                 :   /* If the VM did not run to completion or if it encountered an
    1513                 :   ** error, then it might not have been halted properly.  So halt
    1514                 :   ** it now.
    1515                 :   */
    1516             797 :   sqlite3SafetyOn(db);
    1517             797 :   sqlite3VdbeHalt(p);
    1518             797 :   sqlite3SafetyOff(db);
    1519                 : 
    1520                 :   /* If the VDBE has be run even partially, then transfer the error code
    1521                 :   ** and error message from the VDBE into the main database structure.  But
    1522                 :   ** if the VDBE has just been set to run but has not actually executed any
    1523                 :   ** instructions yet, leave the main database error information unchanged.
    1524                 :   */
    1525             797 :   if( p->pc>=0 ){
    1526             694 :     if( p->zErrMsg ){
    1527               0 :       sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3FreeX);
    1528               0 :       db->errCode = p->rc;
    1529               0 :       p->zErrMsg = 0;
    1530             694 :     }else if( p->rc ){
    1531               1 :       sqlite3Error(db, p->rc, 0);
    1532                 :     }else{
    1533             693 :       sqlite3Error(db, SQLITE_OK, 0);
    1534                 :     }
    1535             103 :   }else if( p->rc && p->expired ){
    1536                 :     /* The expired flag was set on the VDBE before the first call
    1537                 :     ** to sqlite3_step(). For consistency (since sqlite3_step() was
    1538                 :     ** called), set the database error in this case as well.
    1539                 :     */
    1540               0 :     sqlite3Error(db, p->rc, 0);
    1541                 :   }
    1542                 : 
    1543                 :   /* Reclaim all memory used by the VDBE
    1544                 :   */
    1545             797 :   Cleanup(p);
    1546                 : 
    1547                 :   /* Save profiling information from this VDBE run.
    1548                 :   */
    1549                 :   assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack );
    1550                 : #ifdef VDBE_PROFILE
    1551                 :   {
    1552                 :     FILE *out = fopen("vdbe_profile.out", "a");
    1553                 :     if( out ){
    1554                 :       int i;
    1555                 :       fprintf(out, "---- ");
    1556                 :       for(i=0; i<p->nOp; i++){
    1557                 :         fprintf(out, "%02x", p->aOp[i].opcode);
    1558                 :       }
    1559                 :       fprintf(out, "\n");
    1560                 :       for(i=0; i<p->nOp; i++){
    1561                 :         fprintf(out, "%6d %10lld %8lld ",
    1562                 :            p->aOp[i].cnt,
    1563                 :            p->aOp[i].cycles,
    1564                 :            p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
    1565                 :         );
    1566                 :         sqlite3VdbePrintOp(out, i, &p->aOp[i]);
    1567                 :       }
    1568                 :       fclose(out);
    1569                 :     }
    1570                 :   }
    1571                 : #endif
    1572             797 :   p->magic = VDBE_MAGIC_INIT;
    1573             797 :   p->aborted = 0;
    1574             797 :   if( p->rc==SQLITE_SCHEMA ){
    1575               0 :     sqlite3ResetInternalSchema(db, 0);
    1576                 :   }
    1577             797 :   return p->rc & db->errMask;
    1578                 : }
    1579                 :  
    1580                 : /*
    1581                 : ** Clean up and delete a VDBE after execution.  Return an integer which is
    1582                 : ** the result code.  Write any error message text into *pzErrMsg.
    1583                 : */
    1584             619 : int sqlite3VdbeFinalize(Vdbe *p){
    1585             619 :   int rc = SQLITE_OK;
    1586            1238 :   if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
    1587             619 :     rc = sqlite3VdbeReset(p);
    1588                 :     assert( (rc & p->db->errMask)==rc );
    1589               0 :   }else if( p->magic!=VDBE_MAGIC_INIT ){
    1590               0 :     return SQLITE_MISUSE;
    1591                 :   }
    1592             619 :   sqlite3VdbeDelete(p);
    1593             619 :   return rc;
    1594                 : }
    1595                 : 
    1596                 : /*
    1597                 : ** Call the destructor for each auxdata entry in pVdbeFunc for which
    1598                 : ** the corresponding bit in mask is clear.  Auxdata entries beyond 31
    1599                 : ** are always destroyed.  To destroy all auxdata entries, call this
    1600                 : ** routine with mask==0.
    1601                 : */
    1602               0 : void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
    1603                 :   int i;
    1604               0 :   for(i=0; i<pVdbeFunc->nAux; i++){
    1605               0 :     struct AuxData *pAux = &pVdbeFunc->apAux[i];
    1606               0 :     if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
    1607               0 :       if( pAux->xDelete ){
    1608               0 :         pAux->xDelete(pAux->pAux);
    1609                 :       }
    1610               0 :       pAux->pAux = 0;
    1611                 :     }
    1612                 :   }
    1613               0 : }
    1614                 : 
    1615                 : /*
    1616                 : ** Delete an entire VDBE.
    1617                 : */
    1618             619 : void sqlite3VdbeDelete(Vdbe *p){
    1619                 :   int i;
    1620             619 :   if( p==0 ) return;
    1621             619 :   Cleanup(p);
    1622             619 :   if( p->pPrev ){
    1623              11 :     p->pPrev->pNext = p->pNext;
    1624                 :   }else{
    1625                 :     assert( p->db->pVdbe==p );
    1626             608 :     p->db->pVdbe = p->pNext;
    1627                 :   }
    1628             619 :   if( p->pNext ){
    1629             137 :     p->pNext->pPrev = p->pPrev;
    1630                 :   }
    1631             619 :   if( p->aOp ){
    1632           11114 :     for(i=0; i<p->nOp; i++){
    1633           10495 :       Op *pOp = &p->aOp[i];
    1634           10495 :       freeP3(pOp->p3type, pOp->p3);
    1635                 :     }
    1636             619 :     sqliteFree(p->aOp);
    1637                 :   }
    1638             619 :   releaseMemArray(p->aVar, p->nVar);
    1639             619 :   sqliteFree(p->aLabel);
    1640             619 :   sqliteFree(p->aStack);
    1641             619 :   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
    1642             619 :   sqliteFree(p->aColName);
    1643             619 :   sqliteFree(p->zSql);
    1644             619 :   p->magic = VDBE_MAGIC_DEAD;
    1645             619 :   sqliteFree(p);
    1646                 : }
    1647                 : 
    1648                 : /*
    1649                 : ** If a MoveTo operation is pending on the given cursor, then do that
    1650                 : ** MoveTo now.  Return an error code.  If no MoveTo is pending, this
    1651                 : ** routine does nothing and returns SQLITE_OK.
    1652                 : */
    1653            1102 : int sqlite3VdbeCursorMoveto(Cursor *p){
    1654            1102 :   if( p->deferredMoveto ){
    1655                 :     int res, rc;
    1656                 : #ifdef SQLITE_TEST
    1657                 :     extern int sqlite3_search_count;
    1658                 : #endif
    1659                 :     assert( p->isTable );
    1660              63 :     rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, 0, &res);
    1661              63 :     if( rc ) return rc;
    1662              63 :     *p->pIncrKey = 0;
    1663              63 :     p->lastRowid = keyToInt(p->movetoTarget);
    1664              63 :     p->rowidIsValid = res==0;
    1665              63 :     if( res<0 ){
    1666               0 :       rc = sqlite3BtreeNext(p->pCursor, &res);
    1667               0 :       if( rc ) return rc;
    1668                 :     }
    1669                 : #ifdef SQLITE_TEST
    1670                 :     sqlite3_search_count++;
    1671                 : #endif
    1672              63 :     p->deferredMoveto = 0;
    1673              63 :     p->cacheStatus = CACHE_STALE;
    1674                 :   }
    1675            1102 :   return SQLITE_OK;
    1676                 : }
    1677                 : 
    1678                 : /*
    1679                 : ** The following functions:
    1680                 : **
    1681                 : ** sqlite3VdbeSerialType()
    1682                 : ** sqlite3VdbeSerialTypeLen()
    1683                 : ** sqlite3VdbeSerialRead()
    1684                 : ** sqlite3VdbeSerialLen()
    1685                 : ** sqlite3VdbeSerialWrite()
    1686                 : **
    1687                 : ** encapsulate the code that serializes values for storage in SQLite
    1688                 : ** data and index records. Each serialized value consists of a
    1689                 : ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
    1690                 : ** integer, stored as a varint.
    1691                 : **
    1692                 : ** In an SQLite index record, the serial type is stored directly before
    1693                 : ** the blob of data that it corresponds to. In a table record, all serial
    1694                 : ** types are stored at the start of the record, and the blobs of data at
    1695                 : ** the end. Hence these functions allow the caller to handle the
    1696                 : ** serial-type and data blob seperately.
    1697                 : **
    1698                 : ** The following table describes the various storage classes for data:
    1699                 : **
    1700                 : **   serial type        bytes of data      type
    1701                 : **   --------------     ---------------    ---------------
    1702                 : **      0                     0            NULL
    1703                 : **      1                     1            signed integer
    1704                 : **      2                     2            signed integer
    1705                 : **      3                     3            signed integer
    1706                 : **      4                     4            signed integer
    1707                 : **      5                     6            signed integer
    1708                 : **      6                     8            signed integer
    1709                 : **      7                     8            IEEE float
    1710                 : **      8                     0            Integer constant 0
    1711                 : **      9                     0            Integer constant 1
    1712                 : **     10,11                               reserved for expansion
    1713                 : **    N>=12 and even       (N-12)/2        BLOB
    1714                 : **    N>=13 and odd        (N-13)/2        text
    1715                 : **
    1716                 : ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
    1717                 : ** of SQLite will not understand those serial types.
    1718                 : */
    1719                 : 
    1720                 : /*
    1721                 : ** Return the serial-type for the value stored in pMem.
    1722                 : */
    1723            3402 : u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
    1724            3402 :   int flags = pMem->flags;
    1725                 : 
    1726            3402 :   if( flags&MEM_Null ){
    1727             159 :     return 0;
    1728                 :   }
    1729            3243 :   if( flags&MEM_Int ){
    1730                 :     /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
    1731                 : #   define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
    1732            1473 :     i64 i = pMem->u.i;
    1733                 :     u64 u;
    1734            1473 :     if( file_format>=4 && (i&1)==i ){
    1735              72 :       return 8+i;
    1736                 :     }
    1737            1401 :     u = i<0 ? -i : i;
    1738            1401 :     if( u<=127 ) return 1;
    1739               0 :     if( u<=32767 ) return 2;
    1740               0 :     if( u<=8388607 ) return 3;
    1741               0 :     if( u<=2147483647 ) return 4;
    1742               0 :     if( u<=MAX_6BYTE ) return 5;
    1743               0 :     return 6;
    1744                 :   }
    1745            1770 :   if( flags&MEM_Real ){
    1746               0 :     return 7;
    1747                 :   }
    1748            1770 :   if( flags&MEM_Str ){
    1749            1770 :     int n = pMem->n;
    1750                 :     assert( n>=0 );
    1751            1770 :     return ((n*2) + 13);
    1752                 :   }
    1753                 :   assert( (flags & MEM_Blob)!=0 );
    1754               0 :   return (pMem->n*2 + 12);
    1755                 : }
    1756                 : 
    1757                 : /*
    1758                 : ** Return the length of the data corresponding to the supplied serial-type.
    1759                 : */
    1760            3766 : int sqlite3VdbeSerialTypeLen(u32 serial_type){
    1761            3766 :   if( serial_type>=12 ){
    1762            1946 :     return (serial_type-12)/2;
    1763                 :   }else{
    1764                 :     static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
    1765            1820 :     return aSize[serial_type];
    1766                 :   }
    1767                 : }
    1768                 : 
    1769                 : /*
    1770                 : ** Write the serialized data blob for the value stored in pMem into 
    1771                 : ** buf. It is assumed that the caller has allocated sufficient space.
    1772                 : ** Return the number of bytes written.
    1773                 : */ 
    1774            1134 : int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem, int file_format){
    1775            1134 :   u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
    1776                 :   int len;
    1777                 : 
    1778                 :   /* Integer and Real */
    1779            1134 :   if( serial_type<=7 && serial_type>0 ){
    1780                 :     u64 v;
    1781                 :     int i;
    1782             467 :     if( serial_type==7 ){
    1783                 :       assert( sizeof(v)==sizeof(pMem->r) );
    1784               0 :       memcpy(&v, &pMem->r, sizeof(v));
    1785                 :     }else{
    1786             467 :       v = pMem->u.i;
    1787                 :     }
    1788             467 :     len = i = sqlite3VdbeSerialTypeLen(serial_type);
    1789            1401 :     while( i-- ){
    1790             467 :       buf[i] = (v&0xFF);
    1791             467 :       v >>= 8;
    1792                 :     }
    1793             467 :     return len;
    1794                 :   }
    1795                 : 
    1796                 :   /* String or blob */
    1797             667 :   if( serial_type>=12 ){
    1798             590 :     len = sqlite3VdbeSerialTypeLen(serial_type);
    1799             590 :     memcpy(buf, pMem->z, len);
    1800             590 :     return len;
    1801                 :   }
    1802                 : 
    1803                 :   /* NULL or constants 0 or 1 */
    1804              77 :   return 0;
    1805                 : }
    1806                 : 
    1807                 : /*
    1808                 : ** Deserialize the data blob pointed to by buf as serial type serial_type
    1809                 : ** and store the result in pMem.  Return the number of bytes read.
    1810                 : */ 
    1811                 : int sqlite3VdbeSerialGet(
    1812                 :   const unsigned char *buf,     /* Buffer to deserialize from */
    1813                 :   u32 serial_type,              /* Serial type to deserialize */
    1814                 :   Mem *pMem                     /* Memory cell to write value into */
    1815            1945 : ){
    1816            1945 :   switch( serial_type ){
    1817                 :     case 10:   /* Reserved for future use */
    1818                 :     case 11:   /* Reserved for future use */
    1819                 :     case 0: {  /* NULL */
    1820              44 :       pMem->flags = MEM_Null;
    1821                 :       break;
    1822                 :     }
    1823                 :     case 1: { /* 1-byte signed integer */
    1824            1081 :       pMem->u.i = (signed char)buf[0];
    1825            1081 :       pMem->flags = MEM_Int;
    1826            1081 :       return 1;
    1827                 :     }
    1828                 :     case 2: { /* 2-byte signed integer */
    1829               0 :       pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
    1830               0 :       pMem->flags = MEM_Int;
    1831               0 :       return 2;
    1832                 :     }
    1833                 :     case 3: { /* 3-byte signed integer */
    1834               0 :       pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
    1835               0 :       pMem->flags = MEM_Int;
    1836               0 :       return 3;
    1837                 :     }
    1838                 :     case 4: { /* 4-byte signed integer */
    1839               0 :       pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
    1840               0 :       pMem->flags = MEM_Int;
    1841               0 :       return 4;
    1842                 :     }
    1843                 :     case 5: { /* 6-byte signed integer */
    1844               0 :       u64 x = (((signed char)buf[0])<<8) | buf[1];
    1845               0 :       u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
    1846               0 :       x = (x<<32) | y;
    1847               0 :       pMem->u.i = *(i64*)&x;
    1848               0 :       pMem->flags = MEM_Int;
    1849               0 :       return 6;
    1850                 :     }
    1851                 :     case 6:   /* 8-byte signed integer */
    1852                 :     case 7: { /* IEEE floating point */
    1853                 :       u64 x;
    1854                 :       u32 y;
    1855                 : #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
    1856                 :       /* Verify that integers and floating point values use the same
    1857                 :       ** byte order.  The byte order differs on some (broken) architectures.
    1858                 :       */
    1859                 :       static const u64 t1 = ((u64)0x3ff00000)<<32;
    1860                 :       static const double r1 = 1.0;
    1861                 :       assert( sizeof(r1)==sizeof(t1) && memcmp(&r1, &t1, sizeof(r1))==0 );
    1862                 : #endif
    1863                 : 
    1864               0 :       x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
    1865               0 :       y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
    1866               0 :       x = (x<<32) | y;
    1867               0 :       if( serial_type==6 ){
    1868               0 :         pMem->u.i = *(i64*)&x;
    1869               0 :         pMem->flags = MEM_Int;
    1870                 :       }else{
    1871                 :         assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
    1872               0 :         memcpy(&pMem->r, &x, sizeof(x));
    1873                 :         /* pMem->r = *(double*)&x; */
    1874               0 :         pMem->flags = MEM_Real;
    1875                 :       }
    1876               0 :       return 8;
    1877                 :     }
    1878                 :     case 8:    /* Integer 0 */
    1879                 :     case 9: {  /* Integer 1 */
    1880              85 :       pMem->u.i = serial_type-8;
    1881              85 :       pMem->flags = MEM_Int;
    1882              85 :       return 0;
    1883                 :     }
    1884                 :     default: {
    1885             735 :       int len = (serial_type-12)/2;
    1886             735 :       pMem->z = (char *)buf;
    1887             735 :       pMem->n = len;
    1888             735 :       pMem->xDel = 0;
    1889             735 :       if( serial_type&0x01 ){
    1890             735 :         pMem->flags = MEM_Str | MEM_Ephem;
    1891                 :       }else{
    1892               0 :         pMem->flags = MEM_Blob | MEM_Ephem;
    1893                 :       }
    1894             735 :       return len;
    1895                 :     }
    1896                 :   }
    1897              44 :   return 0;
    1898                 : }
    1899                 : 
    1900                 : /*
    1901                 : ** The header of a record consists of a sequence variable-length integers.
    1902                 : ** These integers are almost always small and are encoded as a single byte.
    1903                 : ** The following macro takes advantage this fact to provide a fast decode
    1904                 : ** of the integers in a record header.  It is faster for the common case
    1905                 : ** where the integer is a single byte.  It is a little slower when the
    1906                 : ** integer is two or more bytes.  But overall it is faster.
    1907                 : **
    1908                 : ** The following expressions are equivalent:
    1909                 : **
    1910                 : **     x = sqlite3GetVarint32( A, &B );
    1911                 : **
    1912                 : **     x = GetVarint( A, B );
    1913                 : **
    1914                 : */
    1915                 : #define GetVarint(A,B)  ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
    1916                 : 
    1917                 : /*
    1918                 : ** This function compares the two table rows or index records specified by 
    1919                 : ** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
    1920                 : ** or positive integer if {nKey1, pKey1} is less than, equal to or 
    1921                 : ** greater than {nKey2, pKey2}.  Both Key1 and Key2 must be byte strings
    1922                 : ** composed by the OP_MakeRecord opcode of the VDBE.
    1923                 : */
    1924                 : int sqlite3VdbeRecordCompare(
    1925                 :   void *userData,
    1926                 :   int nKey1, const void *pKey1, 
    1927                 :   int nKey2, const void *pKey2
    1928             431 : ){
    1929             431 :   KeyInfo *pKeyInfo = (KeyInfo*)userData;
    1930                 :   u32 d1, d2;          /* Offset into aKey[] of next data element */
    1931                 :   u32 idx1, idx2;      /* Offset into aKey[] of next header element */
    1932                 :   u32 szHdr1, szHdr2;  /* Number of bytes in header */
    1933             431 :   int i = 0;
    1934                 :   int nField;
    1935             431 :   int rc = 0;
    1936             431 :   const unsigned char *aKey1 = (const unsigned char *)pKey1;
    1937             431 :   const unsigned char *aKey2 = (const unsigned char *)pKey2;
    1938                 : 
    1939                 :   Mem mem1;
    1940                 :   Mem mem2;
    1941             431 :   mem1.enc = pKeyInfo->enc;
    1942             431 :   mem2.enc = pKeyInfo->enc;
    1943                 :   
    1944             431 :   idx1 = GetVarint(aKey1, szHdr1);
    1945             431 :   d1 = szHdr1;
    1946             431 :   idx2 = GetVarint(aKey2, szHdr2);
    1947             431 :   d2 = szHdr2;
    1948             431 :   nField = pKeyInfo->nField;
    1949             938 :   while( idx1<szHdr1 && idx2<szHdr2 ){
    1950                 :     u32 serial_type1;
    1951                 :     u32 serial_type2;
    1952                 : 
    1953                 :     /* Read the serial types for the next element in each key. */
    1954             431 :     idx1 += GetVarint( aKey1+idx1, serial_type1 );
    1955             431 :     if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
    1956             431 :     idx2 += GetVarint( aKey2+idx2, serial_type2 );
    1957             431 :     if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
    1958                 : 
    1959                 :     /* Extract the values to be compared.
    1960                 :     */
    1961             431 :     d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
    1962             431 :     d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
    1963                 : 
    1964                 :     /* Do the comparison
    1965                 :     */
    1966             431 :     rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
    1967             431 :     if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
    1968             431 :     if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
    1969             431 :     if( rc!=0 ){
    1970             355 :       break;
    1971                 :     }
    1972              76 :     i++;
    1973                 :   }
    1974                 : 
    1975                 :   /* One of the keys ran out of fields, but all the fields up to that point
    1976                 :   ** were equal. If the incrKey flag is true, then the second key is
    1977                 :   ** treated as larger.
    1978                 :   */
    1979             431 :   if( rc==0 ){
    1980              76 :     if( pKeyInfo->incrKey ){
    1981              38 :       rc = -1;
    1982              38 :     }else if( d1<nKey1 ){
    1983              38 :       rc = 1;
    1984               0 :     }else if( d2<nKey2 ){
    1985               0 :       rc = -1;
    1986                 :     }
    1987             355 :   }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
    1988                 :                && pKeyInfo->aSortOrder[i] ){
    1989               0 :     rc = -rc;
    1990                 :   }
    1991                 : 
    1992             431 :   return rc;
    1993                 : }
    1994                 : 
    1995                 : /*
    1996                 : ** The argument is an index entry composed using the OP_MakeRecord opcode.
    1997                 : ** The last entry in this record should be an integer (specifically
    1998                 : ** an integer rowid).  This routine returns the number of bytes in
    1999                 : ** that integer.
    2000                 : */
    2001             185 : int sqlite3VdbeIdxRowidLen(const u8 *aKey){
    2002                 :   u32 szHdr;        /* Size of the header */
    2003                 :   u32 typeRowid;    /* Serial type of the rowid */
    2004                 : 
    2005             185 :   sqlite3GetVarint32(aKey, &szHdr);
    2006             185 :   sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
    2007             185 :   return sqlite3VdbeSerialTypeLen(typeRowid);
    2008                 : }
    2009                 :   
    2010                 : 
    2011                 : /*
    2012                 : ** pCur points at an index entry created using the OP_MakeRecord opcode.
    2013                 : ** Read the rowid (the last field in the record) and store it in *rowid.
    2014                 : ** Return SQLITE_OK if everything works, or an error code otherwise.
    2015                 : */
    2016              63 : int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
    2017              63 :   i64 nCellKey = 0;
    2018                 :   int rc;
    2019                 :   u32 szHdr;        /* Size of the header */
    2020                 :   u32 typeRowid;    /* Serial type of the rowid */
    2021                 :   u32 lenRowid;     /* Size of the rowid */
    2022                 :   Mem m, v;
    2023                 : 
    2024              63 :   sqlite3BtreeKeySize(pCur, &nCellKey);
    2025              63 :   if( nCellKey<=0 ){
    2026               0 :     return SQLITE_CORRUPT_BKPT;
    2027                 :   }
    2028              63 :   rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
    2029              63 :   if( rc ){
    2030               0 :     return rc;
    2031                 :   }
    2032              63 :   sqlite3GetVarint32((u8*)m.z, &szHdr);
    2033              63 :   sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid);
    2034              63 :   lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
    2035              63 :   sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
    2036              63 :   *rowid = v.u.i;
    2037              63 :   sqlite3VdbeMemRelease(&m);
    2038              63 :   return SQLITE_OK;
    2039                 : }
    2040                 : 
    2041                 : /*
    2042                 : ** Compare the key of the index entry that cursor pC is point to against
    2043                 : ** the key string in pKey (of length nKey).  Write into *pRes a number
    2044                 : ** that is negative, zero, or positive if pC is less than, equal to,
    2045                 : ** or greater than pKey.  Return SQLITE_OK on success.
    2046                 : **
    2047                 : ** pKey is either created without a rowid or is truncated so that it
    2048                 : ** omits the rowid at the end.  The rowid at the end of the index entry
    2049                 : ** is ignored as well.
    2050                 : */
    2051                 : int sqlite3VdbeIdxKeyCompare(
    2052                 :   Cursor *pC,                 /* The cursor to compare against */
    2053                 :   int nKey, const u8 *pKey,   /* The key to compare */
    2054                 :   int *res                    /* Write the comparison result here */
    2055              64 : ){
    2056              64 :   i64 nCellKey = 0;
    2057                 :   int rc;
    2058              64 :   BtCursor *pCur = pC->pCursor;
    2059                 :   int lenRowid;
    2060                 :   Mem m;
    2061                 : 
    2062              64 :   sqlite3BtreeKeySize(pCur, &nCellKey);
    2063              64 :   if( nCellKey<=0 ){
    2064               0 :     *res = 0;
    2065               0 :     return SQLITE_OK;
    2066                 :   }
    2067              64 :   rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
    2068              64 :   if( rc ){
    2069               0 :     return rc;
    2070                 :   }
    2071              64 :   lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
    2072              64 :   *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
    2073              64 :   sqlite3VdbeMemRelease(&m);
    2074              64 :   return SQLITE_OK;
    2075                 : }
    2076                 : 
    2077                 : /*
    2078                 : ** This routine sets the value to be returned by subsequent calls to
    2079                 : ** sqlite3_changes() on the database handle 'db'. 
    2080                 : */
    2081             150 : void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
    2082             150 :   db->nChange = nChange;
    2083             150 :   db->nTotalChange += nChange;
    2084             150 : }
    2085                 : 
    2086                 : /*
    2087                 : ** Set a flag in the vdbe to update the change counter when it is finalised
    2088                 : ** or reset.
    2089                 : */
    2090             122 : void sqlite3VdbeCountChanges(Vdbe *v){
    2091             122 :   v->changeCntOn = 1;
    2092             122 : }
    2093                 : 
    2094                 : /*
    2095                 : ** Mark every prepared statement associated with a database connection
    2096                 : ** as expired.
    2097                 : **
    2098                 : ** An expired statement means that recompilation of the statement is
    2099                 : ** recommend.  Statements expire when things happen that make their
    2100                 : ** programs obsolete.  Removing user-defined functions or collating
    2101                 : ** sequences, or changing an authorization function are the types of
    2102                 : ** things that make prepared statements obsolete.
    2103                 : */
    2104               2 : void sqlite3ExpirePreparedStatements(sqlite3 *db){
    2105                 :   Vdbe *p;
    2106               2 :   for(p = db->pVdbe; p; p=p->pNext){
    2107               0 :     p->expired = 1;
    2108                 :   }
    2109               2 : }
    2110                 : 
    2111                 : /*
    2112                 : ** Return the database associated with the Vdbe.
    2113                 : */
    2114             428 : sqlite3 *sqlite3VdbeDb(Vdbe *v){
    2115             428 :   return v->db;
    2116                 : }

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:16 +0000 (5 days ago)

Copyright © 2005-2009 The PHP Group
All rights reserved.