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 - sqlite/libsqlite/src - vdbe.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 1789
Code covered: 45.0 % Executed lines: 805
Legend: not executed executed

       1                 : /*
       2                 : ** 2001 September 15
       3                 : **
       4                 : ** The author disclaims copyright to this source code.  In place of
       5                 : ** a legal notice, here is a blessing:
       6                 : **
       7                 : **    May you do good and not evil.
       8                 : **    May you find forgiveness for yourself and forgive others.
       9                 : **    May you share freely, never taking more than you give.
      10                 : **
      11                 : *************************************************************************
      12                 : ** The code in this file implements execution method of the 
      13                 : ** Virtual Database Engine (VDBE).  A separate file ("vdbeaux.c")
      14                 : ** handles housekeeping details such as creating and deleting
      15                 : ** VDBE instances.  This file is solely interested in executing
      16                 : ** the VDBE program.
      17                 : **
      18                 : ** In the external interface, an "sqlite_vm*" is an opaque pointer
      19                 : ** to a VDBE.
      20                 : **
      21                 : ** The SQL parser generates a program which is then executed by
      22                 : ** the VDBE to do the work of the SQL statement.  VDBE programs are 
      23                 : ** similar in form to assembly language.  The program consists of
      24                 : ** a linear sequence of operations.  Each operation has an opcode 
      25                 : ** and 3 operands.  Operands P1 and P2 are integers.  Operand P3 
      26                 : ** is a null-terminated string.   The P2 operand must be non-negative.
      27                 : ** Opcodes will typically ignore one or more operands.  Many opcodes
      28                 : ** ignore all three operands.
      29                 : **
      30                 : ** Computation results are stored on a stack.  Each entry on the
      31                 : ** stack is either an integer, a null-terminated string, a floating point
      32                 : ** number, or the SQL "NULL" value.  An inplicit conversion from one
      33                 : ** type to the other occurs as necessary.
      34                 : ** 
      35                 : ** Most of the code in this file is taken up by the sqliteVdbeExec()
      36                 : ** function which does the work of interpreting a VDBE program.
      37                 : ** But other routines are also provided to help in building up
      38                 : ** a program instruction by instruction.
      39                 : **
      40                 : ** Various scripts scan this source file in order to generate HTML
      41                 : ** documentation, headers files, or other derived files.  The formatting
      42                 : ** of the code in this file is, therefore, important.  See other comments
      43                 : ** in this file for details.  If in doubt, do not deviate from existing
      44                 : ** commenting and indentation practices when changing or adding code.
      45                 : **
      46                 : ** $Id: vdbe.c 219681 2006-09-09 10:59:05Z tony2001 $
      47                 : */
      48                 : #include "sqliteInt.h"
      49                 : #include "os.h"
      50                 : #include <ctype.h>
      51                 : #include "vdbeInt.h"
      52                 : 
      53                 : /*
      54                 : ** The following global variable is incremented every time a cursor
      55                 : ** moves, either by the OP_MoveTo or the OP_Next opcode.  The test
      56                 : ** procedures use this information to make sure that indices are
      57                 : ** working correctly.  This variable has no function other than to
      58                 : ** help verify the correct operation of the library.
      59                 : */
      60                 : int sqlite_search_count = 0;
      61                 : 
      62                 : /*
      63                 : ** When this global variable is positive, it gets decremented once before
      64                 : ** each instruction in the VDBE.  When reaches zero, the SQLITE_Interrupt
      65                 : ** of the db.flags field is set in order to simulate an interrupt.
      66                 : **
      67                 : ** This facility is used for testing purposes only.  It does not function
      68                 : ** in an ordinary build.
      69                 : */
      70                 : int sqlite_interrupt_count = 0;
      71                 : 
      72                 : /*
      73                 : ** Advance the virtual machine to the next output row.
      74                 : **
      75                 : ** The return vale will be either SQLITE_BUSY, SQLITE_DONE, 
      76                 : ** SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE.
      77                 : **
      78                 : ** SQLITE_BUSY means that the virtual machine attempted to open
      79                 : ** a locked database and there is no busy callback registered.
      80                 : ** Call sqlite_step() again to retry the open.  *pN is set to 0
      81                 : ** and *pazColName and *pazValue are both set to NULL.
      82                 : **
      83                 : ** SQLITE_DONE means that the virtual machine has finished
      84                 : ** executing.  sqlite_step() should not be called again on this
      85                 : ** virtual machine.  *pN and *pazColName are set appropriately
      86                 : ** but *pazValue is set to NULL.
      87                 : **
      88                 : ** SQLITE_ROW means that the virtual machine has generated another
      89                 : ** row of the result set.  *pN is set to the number of columns in
      90                 : ** the row.  *pazColName is set to the names of the columns followed
      91                 : ** by the column datatypes.  *pazValue is set to the values of each
      92                 : ** column in the row.  The value of the i-th column is (*pazValue)[i].
      93                 : ** The name of the i-th column is (*pazColName)[i] and the datatype
      94                 : ** of the i-th column is (*pazColName)[i+*pN].
      95                 : **
      96                 : ** SQLITE_ERROR means that a run-time error (such as a constraint
      97                 : ** violation) has occurred.  The details of the error will be returned
      98                 : ** by the next call to sqlite_finalize().  sqlite_step() should not
      99                 : ** be called again on the VM.
     100                 : **
     101                 : ** SQLITE_MISUSE means that the this routine was called inappropriately.
     102                 : ** Perhaps it was called on a virtual machine that had already been
     103                 : ** finalized or on one that had previously returned SQLITE_ERROR or
     104                 : ** SQLITE_DONE.  Or it could be the case the the same database connection
     105                 : ** is being used simulataneously by two or more threads.
     106                 : */
     107                 : int sqlite_step(
     108                 :   sqlite_vm *pVm,              /* The virtual machine to execute */
     109                 :   int *pN,                     /* OUT: Number of columns in result */
     110                 :   const char ***pazValue,      /* OUT: Column data */
     111                 :   const char ***pazColName     /* OUT: Column names and datatypes */
     112            1593 : ){
     113            1593 :   Vdbe *p = (Vdbe*)pVm;
     114                 :   sqlite *db;
     115                 :   int rc;
     116                 : 
     117            1593 :   if( !p || p->magic!=VDBE_MAGIC_RUN ){
     118               1 :     return SQLITE_MISUSE;
     119                 :   }
     120            1592 :   db = p->db;
     121            1592 :   if( sqliteSafetyOn(db) ){
     122               0 :     p->rc = SQLITE_MISUSE;
     123               0 :     return SQLITE_MISUSE;
     124                 :   }
     125            1592 :   if( p->explain ){
     126               0 :     rc = sqliteVdbeList(p);
     127                 :   }else{
     128            1592 :     rc = sqliteVdbeExec(p);
     129                 :   }
     130            3183 :   if( rc==SQLITE_DONE || rc==SQLITE_ROW ){
     131            1591 :     if( pazColName ) *pazColName = (const char**)p->azColName;
     132            1591 :     if( pN ) *pN = p->nResColumn;
     133                 :   }else{
     134               1 :     if( pazColName) *pazColName = 0;
     135               1 :     if( pN ) *pN = 0;
     136                 :   }
     137            1592 :   if( pazValue ){
     138            1592 :     if( rc==SQLITE_ROW ){
     139             425 :       *pazValue = (const char**)p->azResColumn;
     140                 :     }else{
     141            1167 :       *pazValue = 0;
     142                 :     }
     143                 :   }
     144            1592 :   if( sqliteSafetyOff(db) ){
     145               0 :     return SQLITE_MISUSE;
     146                 :   }
     147            1592 :   return rc;
     148                 : }
     149                 : 
     150                 : /*
     151                 : ** Insert a new aggregate element and make it the element that
     152                 : ** has focus.
     153                 : **
     154                 : ** Return 0 on success and 1 if memory is exhausted.
     155                 : */
     156              14 : static int AggInsert(Agg *p, char *zKey, int nKey){
     157                 :   AggElem *pElem, *pOld;
     158                 :   int i;
     159                 :   Mem *pMem;
     160              14 :   pElem = sqliteMalloc( sizeof(AggElem) + nKey +
     161                 :                         (p->nMem-1)*sizeof(pElem->aMem[0]) );
     162              14 :   if( pElem==0 ) return 1;
     163              14 :   pElem->zKey = (char*)&pElem->aMem[p->nMem];
     164              14 :   memcpy(pElem->zKey, zKey, nKey);
     165              14 :   pElem->nKey = nKey;
     166              14 :   pOld = sqliteHashInsert(&p->hash, pElem->zKey, pElem->nKey, pElem);
     167              14 :   if( pOld!=0 ){
     168                 :     assert( pOld==pElem );  /* Malloc failed on insert */
     169               0 :     sqliteFree(pOld);
     170               0 :     return 0;
     171                 :   }
     172              28 :   for(i=0, pMem=pElem->aMem; i<p->nMem; i++, pMem++){
     173              14 :     pMem->flags = MEM_Null;
     174                 :   }
     175              14 :   p->pCurrent = pElem;
     176              14 :   return 0;
     177                 : }
     178                 : 
     179                 : /*
     180                 : ** Get the AggElem currently in focus
     181                 : */
     182                 : #define AggInFocus(P)   ((P).pCurrent ? (P).pCurrent : _AggInFocus(&(P)))
     183               0 : static AggElem *_AggInFocus(Agg *p){
     184               0 :   HashElem *pElem = sqliteHashFirst(&p->hash);
     185               0 :   if( pElem==0 ){
     186               0 :     AggInsert(p,"",1);
     187               0 :     pElem = sqliteHashFirst(&p->hash);
     188                 :   }
     189               0 :   return pElem ? sqliteHashData(pElem) : 0;
     190                 : }
     191                 : 
     192                 : /*
     193                 : ** Convert the given stack entity into a string if it isn't one
     194                 : ** already.
     195                 : */
     196                 : #define Stringify(P) if(((P)->flags & MEM_Str)==0){hardStringify(P);}
     197             183 : static int hardStringify(Mem *pStack){
     198             183 :   int fg = pStack->flags;
     199             183 :   if( fg & MEM_Real ){
     200               0 :     sqlite_snprintf(sizeof(pStack->zShort),pStack->zShort,"%.15g",pStack->r);
     201             183 :   }else if( fg & MEM_Int ){
     202             169 :     sqlite_snprintf(sizeof(pStack->zShort),pStack->zShort,"%d",pStack->i);
     203                 :   }else{
     204              14 :     pStack->zShort[0] = 0;
     205                 :   }
     206             183 :   pStack->z = pStack->zShort;
     207             183 :   pStack->n = strlen(pStack->zShort)+1;
     208             183 :   pStack->flags = MEM_Str | MEM_Short;
     209             183 :   return 0;
     210                 : }
     211                 : 
     212                 : /*
     213                 : ** Convert the given stack entity into a string that has been obtained
     214                 : ** from sqliteMalloc().  This is different from Stringify() above in that
     215                 : ** Stringify() will use the NBFS bytes of static string space if the string
     216                 : ** will fit but this routine always mallocs for space.
     217                 : ** Return non-zero if we run out of memory.
     218                 : */
     219                 : #define Dynamicify(P) (((P)->flags & MEM_Dyn)==0 ? hardDynamicify(P):0)
     220               0 : static int hardDynamicify(Mem *pStack){
     221               0 :   int fg = pStack->flags;
     222                 :   char *z;
     223               0 :   if( (fg & MEM_Str)==0 ){
     224               0 :     hardStringify(pStack);
     225                 :   }
     226                 :   assert( (fg & MEM_Dyn)==0 );
     227               0 :   z = sqliteMallocRaw( pStack->n );
     228               0 :   if( z==0 ) return 1;
     229               0 :   memcpy(z, pStack->z, pStack->n);
     230               0 :   pStack->z = z;
     231               0 :   pStack->flags |= MEM_Dyn;
     232               0 :   return 0;
     233                 : }
     234                 : 
     235                 : /*
     236                 : ** An ephemeral string value (signified by the MEM_Ephem flag) contains
     237                 : ** a pointer to a dynamically allocated string where some other entity
     238                 : ** is responsible for deallocating that string.  Because the stack entry
     239                 : ** does not control the string, it might be deleted without the stack
     240                 : ** entry knowing it.
     241                 : **
     242                 : ** This routine converts an ephemeral string into a dynamically allocated
     243                 : ** string that the stack entry itself controls.  In other words, it
     244                 : ** converts an MEM_Ephem string into an MEM_Dyn string.
     245                 : */
     246                 : #define Deephemeralize(P) \
     247                 :    if( ((P)->flags&MEM_Ephem)!=0 && hardDeephem(P) ){ goto no_mem;}
     248               0 : static int hardDeephem(Mem *pStack){
     249                 :   char *z;
     250                 :   assert( (pStack->flags & MEM_Ephem)!=0 );
     251               0 :   z = sqliteMallocRaw( pStack->n );
     252               0 :   if( z==0 ) return 1;
     253               0 :   memcpy(z, pStack->z, pStack->n);
     254               0 :   pStack->z = z;
     255               0 :   pStack->flags &= ~MEM_Ephem;
     256               0 :   pStack->flags |= MEM_Dyn;
     257               0 :   return 0;
     258                 : }
     259                 : 
     260                 : /*
     261                 : ** Release the memory associated with the given stack level.  This
     262                 : ** leaves the Mem.flags field in an inconsistent state.
     263                 : */
     264                 : #define Release(P) if((P)->flags&MEM_Dyn){ sqliteFree((P)->z); }
     265                 : 
     266                 : /*
     267                 : ** Pop the stack N times.
     268                 : */
     269            1786 : static void popStack(Mem **ppTos, int N){
     270            1786 :   Mem *pTos = *ppTos;
     271            7297 :   while( N>0 ){
     272            3725 :     N--;
     273            3725 :     Release(pTos);
     274            3725 :     pTos--;
     275                 :   }
     276            1786 :   *ppTos = pTos;
     277            1786 : }
     278                 : 
     279                 : /*
     280                 : ** Return TRUE if zNum is a 32-bit signed integer and write
     281                 : ** the value of the integer into *pNum.  If zNum is not an integer
     282                 : ** or is an integer that is too large to be expressed with just 32
     283                 : ** bits, then return false.
     284                 : **
     285                 : ** Under Linux (RedHat 7.2) this routine is much faster than atoi()
     286                 : ** for converting strings into integers.
     287                 : */
     288              11 : static int toInt(const char *zNum, int *pNum){
     289              11 :   int v = 0;
     290                 :   int neg;
     291                 :   int i, c;
     292              11 :   if( *zNum=='-' ){
     293               0 :     neg = 1;
     294               0 :     zNum++;
     295              11 :   }else if( *zNum=='+' ){
     296               0 :     neg = 0;
     297               0 :     zNum++;
     298                 :   }else{
     299              11 :     neg = 0;
     300                 :   }
     301              22 :   for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
     302              11 :     v = v*10 + c - '0';
     303                 :   }
     304              11 :   *pNum = neg ? -v : v;
     305              11 :   return c==0 && i>0 && (i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0));
     306                 : }
     307                 : 
     308                 : /*
     309                 : ** Convert the given stack entity into a integer if it isn't one
     310                 : ** already.
     311                 : **
     312                 : ** Any prior string or real representation is invalidated.  
     313                 : ** NULLs are converted into 0.
     314                 : */
     315                 : #define Integerify(P) if(((P)->flags&MEM_Int)==0){ hardIntegerify(P); }
     316               0 : static void hardIntegerify(Mem *pStack){
     317               0 :   if( pStack->flags & MEM_Real ){
     318               0 :     pStack->i = (int)pStack->r;
     319               0 :     Release(pStack);
     320               0 :   }else if( pStack->flags & MEM_Str ){
     321               0 :     toInt(pStack->z, &pStack->i);
     322               0 :     Release(pStack);
     323                 :   }else{
     324               0 :     pStack->i = 0;
     325                 :   }
     326               0 :   pStack->flags = MEM_Int;
     327               0 : }
     328                 : 
     329                 : /*
     330                 : ** Get a valid Real representation for the given stack element.
     331                 : **
     332                 : ** Any prior string or integer representation is retained.
     333                 : ** NULLs are converted into 0.0.
     334                 : */
     335                 : #define Realify(P) if(((P)->flags&MEM_Real)==0){ hardRealify(P); }
     336               0 : static void hardRealify(Mem *pStack){
     337               0 :   if( pStack->flags & MEM_Str ){
     338               0 :     pStack->r = sqliteAtoF(pStack->z, 0);
     339               0 :   }else if( pStack->flags & MEM_Int ){
     340               0 :     pStack->r = pStack->i;
     341                 :   }else{
     342               0 :     pStack->r = 0.0;
     343                 :   }
     344               0 :   pStack->flags |= MEM_Real;
     345               0 : }
     346                 : 
     347                 : /*
     348                 : ** The parameters are pointers to the head of two sorted lists
     349                 : ** of Sorter structures.  Merge these two lists together and return
     350                 : ** a single sorted list.  This routine forms the core of the merge-sort
     351                 : ** algorithm.
     352                 : **
     353                 : ** In the case of a tie, left sorts in front of right.
     354                 : */
     355               0 : static Sorter *Merge(Sorter *pLeft, Sorter *pRight){
     356                 :   Sorter sHead;
     357                 :   Sorter *pTail;
     358               0 :   pTail = &sHead;
     359               0 :   pTail->pNext = 0;
     360               0 :   while( pLeft && pRight ){
     361               0 :     int c = sqliteSortCompare(pLeft->zKey, pRight->zKey);
     362               0 :     if( c<=0 ){
     363               0 :       pTail->pNext = pLeft;
     364               0 :       pLeft = pLeft->pNext;
     365                 :     }else{
     366               0 :       pTail->pNext = pRight;
     367               0 :       pRight = pRight->pNext;
     368                 :     }
     369               0 :     pTail = pTail->pNext;
     370                 :   }
     371               0 :   if( pLeft ){
     372               0 :     pTail->pNext = pLeft;
     373               0 :   }else if( pRight ){
     374               0 :     pTail->pNext = pRight;
     375                 :   }
     376               0 :   return sHead.pNext;
     377                 : }
     378                 : 
     379                 : /*
     380                 : ** The following routine works like a replacement for the standard
     381                 : ** library routine fgets().  The difference is in how end-of-line (EOL)
     382                 : ** is handled.  Standard fgets() uses LF for EOL under unix, CRLF
     383                 : ** under windows, and CR under mac.  This routine accepts any of these
     384                 : ** character sequences as an EOL mark.  The EOL mark is replaced by
     385                 : ** a single LF character in zBuf.
     386                 : */
     387               0 : static char *vdbe_fgets(char *zBuf, int nBuf, FILE *in){
     388                 :   int i, c;
     389               0 :   for(i=0; i<nBuf-1 && (c=getc(in))!=EOF; i++){
     390               0 :     zBuf[i] = c;
     391               0 :     if( c=='\r' || c=='\n' ){
     392               0 :       if( c=='\r' ){
     393               0 :         zBuf[i] = '\n';
     394               0 :         c = getc(in);
     395               0 :         if( c!=EOF && c!='\n' ) ungetc(c, in);
     396                 :       }
     397               0 :       i++;
     398               0 :       break;
     399                 :     }
     400                 :   }
     401               0 :   zBuf[i]  = 0;
     402               0 :   return i>0 ? zBuf : 0;
     403                 : }
     404                 : 
     405                 : /*
     406                 : ** Make sure there is space in the Vdbe structure to hold at least
     407                 : ** mxCursor cursors.  If there is not currently enough space, then
     408                 : ** allocate more.
     409                 : **
     410                 : ** If a memory allocation error occurs, return 1.  Return 0 if
     411                 : ** everything works.
     412                 : */
     413            1079 : static int expandCursorArraySize(Vdbe *p, int mxCursor){
     414            1079 :   if( mxCursor>=p->nCursor ){
     415            1076 :     Cursor *aCsr = sqliteRealloc( p->aCsr, (mxCursor+1)*sizeof(Cursor) );
     416            1076 :     if( aCsr==0 ) return 1;
     417            1076 :     p->aCsr = aCsr;
     418            1076 :     memset(&p->aCsr[p->nCursor], 0, sizeof(Cursor)*(mxCursor+1-p->nCursor));
     419            1076 :     p->nCursor = mxCursor+1;
     420                 :   }
     421            1079 :   return 0;
     422                 : }
     423                 : 
     424                 : #ifdef VDBE_PROFILE
     425                 : /*
     426                 : ** The following routine only works on pentium-class processors.
     427                 : ** It uses the RDTSC opcode to read cycle count value out of the
     428                 : ** processor and returns that value.  This can be used for high-res
     429                 : ** profiling.
     430                 : */
     431                 : __inline__ unsigned long long int hwtime(void){
     432                 :   unsigned long long int x;
     433                 :   __asm__("rdtsc\n\t"
     434                 :           "mov %%edx, %%ecx\n\t"
     435                 :           :"=A" (x));
     436                 :   return x;
     437                 : }
     438                 : #endif
     439                 : 
     440                 : /*
     441                 : ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
     442                 : ** sqlite_interrupt() routine has been called.  If it has been, then
     443                 : ** processing of the VDBE program is interrupted.
     444                 : **
     445                 : ** This macro added to every instruction that does a jump in order to
     446                 : ** implement a loop.  This test used to be on every single instruction,
     447                 : ** but that meant we more testing that we needed.  By only testing the
     448                 : ** flag on jump instructions, we get a (small) speed improvement.
     449                 : */
     450                 : #define CHECK_FOR_INTERRUPT \
     451                 :    if( db->flags & SQLITE_Interrupt ) goto abort_due_to_interrupt;
     452                 : 
     453                 : 
     454                 : /*
     455                 : ** Execute as much of a VDBE program as we can then return.
     456                 : **
     457                 : ** sqliteVdbeMakeReady() must be called before this routine in order to
     458                 : ** close the program with a final OP_Halt and to set up the callbacks
     459                 : ** and the error message pointer.
     460                 : **
     461                 : ** Whenever a row or result data is available, this routine will either
     462                 : ** invoke the result callback (if there is one) or return with
     463                 : ** SQLITE_ROW.
     464                 : **
     465                 : ** If an attempt is made to open a locked database, then this routine
     466                 : ** will either invoke the busy callback (if there is one) or it will
     467                 : ** return SQLITE_BUSY.
     468                 : **
     469                 : ** If an error occurs, an error message is written to memory obtained
     470                 : ** from sqliteMalloc() and p->zErrMsg is made to point to that memory.
     471                 : ** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
     472                 : **
     473                 : ** If the callback ever returns non-zero, then the program exits
     474                 : ** immediately.  There will be no error message but the p->rc field is
     475                 : ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
     476                 : **
     477                 : ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
     478                 : ** routine to return SQLITE_ERROR.
     479                 : **
     480                 : ** Other fatal errors return SQLITE_ERROR.
     481                 : **
     482                 : ** After this routine has finished, sqliteVdbeFinalize() should be
     483                 : ** used to clean up the mess that was left behind.
     484                 : */
     485                 : int sqliteVdbeExec(
     486                 :   Vdbe *p                    /* The VDBE */
     487            1592 : ){
     488                 :   int pc;                    /* The program counter */
     489                 :   Op *pOp;                   /* Current operation */
     490            1592 :   int rc = SQLITE_OK;        /* Value to return */
     491            1592 :   sqlite *db = p->db;        /* The database */
     492                 :   Mem *pTos;                 /* Top entry in the operand stack */
     493                 :   char zBuf[100];            /* Space to sprintf() an integer */
     494                 : #ifdef VDBE_PROFILE
     495                 :   unsigned long long start;  /* CPU clock count at start of opcode */
     496                 :   int origPc;                /* Program counter at start of opcode */
     497                 : #endif
     498                 : #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
     499            1592 :   int nProgressOps = 0;      /* Opcodes executed since progress callback. */
     500                 : #endif
     501                 : 
     502            1592 :   if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
     503                 :   assert( db->magic==SQLITE_MAGIC_BUSY );
     504                 :   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
     505            1592 :   p->rc = SQLITE_OK;
     506                 :   assert( p->explain==0 );
     507            1592 :   if( sqlite_malloc_failed ) goto no_mem;
     508            1592 :   pTos = p->pTos;
     509            1592 :   if( p->popStack ){
     510             396 :     popStack(&pTos, p->popStack);
     511             396 :     p->popStack = 0;
     512                 :   }
     513            1592 :   CHECK_FOR_INTERRUPT;
     514           18406 :   for(pc=p->pc; rc==SQLITE_OK; pc++){
     515                 :     assert( pc>=0 && pc<p->nOp );
     516                 :     assert( pTos<=&p->aStack[pc] );
     517                 : #ifdef VDBE_PROFILE
     518                 :     origPc = pc;
     519                 :     start = hwtime();
     520                 : #endif
     521           18405 :     pOp = &p->aOp[pc];
     522                 : 
     523                 :     /* Only allow tracing if NDEBUG is not defined.
     524                 :     */
     525                 : #ifndef NDEBUG
     526                 :     if( p->trace ){
     527                 :       sqliteVdbePrintOp(p->trace, pc, pOp);
     528                 :     }
     529                 : #endif
     530                 : 
     531                 :     /* Check to see if we need to simulate an interrupt.  This only happens
     532                 :     ** if we have a special test build.
     533                 :     */
     534                 : #ifdef SQLITE_TEST
     535                 :     if( sqlite_interrupt_count>0 ){
     536                 :       sqlite_interrupt_count--;
     537                 :       if( sqlite_interrupt_count==0 ){
     538                 :         sqlite_interrupt(db);
     539                 :       }
     540                 :     }
     541                 : #endif
     542                 : 
     543                 : #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
     544                 :     /* Call the progress callback if it is configured and the required number
     545                 :     ** of VDBE ops have been executed (either since this invocation of
     546                 :     ** sqliteVdbeExec() or since last time the progress callback was called).
     547                 :     ** If the progress callback returns non-zero, exit the virtual machine with
     548                 :     ** a return code SQLITE_ABORT.
     549                 :     */
     550           18405 :     if( db->xProgress ){
     551               0 :       if( db->nProgressOps==nProgressOps ){
     552               0 :         if( db->xProgress(db->pProgressArg)!=0 ){
     553               0 :           rc = SQLITE_ABORT;
     554               0 :           continue; /* skip to the next iteration of the for loop */
     555                 :         }
     556               0 :         nProgressOps = 0;
     557                 :       }
     558               0 :       nProgressOps++;
     559                 :     }
     560                 : #endif
     561                 : 
     562           18405 :     switch( pOp->opcode ){
     563                 : 
     564                 : /*****************************************************************************
     565                 : ** What follows is a massive switch statement where each case implements a
     566                 : ** separate instruction in the virtual machine.  If we follow the usual
     567                 : ** indentation conventions, each case should be indented by 6 spaces.  But
     568                 : ** that is a lot of wasted space on the left margin.  So the code within
     569                 : ** the switch statement will break with convention and be flush-left. Another
     570                 : ** big comment (similar to this one) will mark the point in the code where
     571                 : ** we transition back to normal indentation.
     572                 : **
     573                 : ** The formatting of each case is important.  The makefile for SQLite
     574                 : ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
     575                 : ** file looking for lines that begin with "case OP_".  The opcodes.h files
     576                 : ** will be filled with #defines that give unique integer values to each
     577                 : ** opcode and the opcodes.c file is filled with an array of strings where
     578                 : ** each string is the symbolic name for the corresponding opcode.
     579                 : **
     580                 : ** Documentation about VDBE opcodes is generated by scanning this file
     581                 : ** for lines of that contain "Opcode:".  That line and all subsequent
     582                 : ** comment lines are used in the generation of the opcode.html documentation
     583                 : ** file.
     584                 : **
     585                 : ** SUMMARY:
     586                 : **
     587                 : **     Formatting is important to scripts that scan this file.
     588                 : **     Do not deviate from the formatting style currently in use.
     589                 : **
     590                 : *****************************************************************************/
     591                 : 
     592                 : /* Opcode:  Goto * P2 *
     593                 : **
     594                 : ** An unconditional jump to address P2.
     595                 : ** The next instruction executed will be 
     596                 : ** the one at index P2 from the beginning of
     597                 : ** the program.
     598                 : */
     599                 : case OP_Goto: {
     600              17 :   CHECK_FOR_INTERRUPT;
     601              17 :   pc = pOp->p2 - 1;
     602              17 :   break;
     603                 : }
     604                 : 
     605                 : /* Opcode:  Gosub * P2 *
     606                 : **
     607                 : ** Push the current address plus 1 onto the return address stack
     608                 : ** and then jump to address P2.
     609                 : **
     610                 : ** The return address stack is of limited depth.  If too many
     611                 : ** OP_Gosub operations occur without intervening OP_Returns, then
     612                 : ** the return address stack will fill up and processing will abort
     613                 : ** with a fatal error.
     614                 : */
     615                 : case OP_Gosub: {
     616               0 :   if( p->returnDepth>=sizeof(p->returnStack)/sizeof(p->returnStack[0]) ){
     617               0 :     sqliteSetString(&p->zErrMsg, "return address stack overflow", (char*)0);
     618               0 :     p->rc = SQLITE_INTERNAL;
     619               0 :     return SQLITE_ERROR;
     620                 :   }
     621               0 :   p->returnStack[p->returnDepth++] = pc+1;
     622               0 :   pc = pOp->p2 - 1;
     623               0 :   break;
     624                 : }
     625                 : 
     626                 : /* Opcode:  Return * * *
     627                 : **
     628                 : ** Jump immediately to the next instruction after the last unreturned
     629                 : ** OP_Gosub.  If an OP_Return has occurred for all OP_Gosubs, then
     630                 : ** processing aborts with a fatal error.
     631                 : */
     632                 : case OP_Return: {
     633               0 :   if( p->returnDepth<=0 ){
     634               0 :     sqliteSetString(&p->zErrMsg, "return address stack underflow", (char*)0);
     635               0 :     p->rc = SQLITE_INTERNAL;
     636               0 :     return SQLITE_ERROR;
     637                 :   }
     638               0 :   p->returnDepth--;
     639               0 :   pc = p->returnStack[p->returnDepth] - 1;
     640               0 :   break;
     641                 : }
     642                 : 
     643                 : /* Opcode:  Halt P1 P2 *
     644                 : **
     645                 : ** Exit immediately.  All open cursors, Lists, Sorts, etc are closed
     646                 : ** automatically.
     647                 : **
     648                 : ** P1 is the result code returned by sqlite_exec().  For a normal
     649                 : ** halt, this should be SQLITE_OK (0).  For errors, it can be some
     650                 : ** other value.  If P1!=0 then P2 will determine whether or not to
     651                 : ** rollback the current transaction.  Do not rollback if P2==OE_Fail.
     652                 : ** Do the rollback if P2==OE_Rollback.  If P2==OE_Abort, then back
     653                 : ** out all changes that have occurred during this execution of the
     654                 : ** VDBE, but do not rollback the transaction. 
     655                 : **
     656                 : ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
     657                 : ** every program.  So a jump past the last instruction of the program
     658                 : ** is the same as executing Halt.
     659                 : */
     660                 : case OP_Halt: {
     661            1166 :   p->magic = VDBE_MAGIC_HALT;
     662            1166 :   p->pTos = pTos;
     663            1166 :   if( pOp->p1!=SQLITE_OK ){
     664               0 :     p->rc = pOp->p1;
     665               0 :     p->errorAction = pOp->p2;
     666               0 :     if( pOp->p3 ){
     667               0 :       sqliteSetString(&p->zErrMsg, pOp->p3, (char*)0);
     668                 :     }
     669               0 :     return SQLITE_ERROR;
     670                 :   }else{
     671            1166 :     p->rc = SQLITE_OK;
     672            1166 :     return SQLITE_DONE;
     673                 :   }
     674                 : }
     675                 : 
     676                 : /* Opcode: Integer P1 * P3
     677                 : **
     678                 : ** The integer value P1 is pushed onto the stack.  If P3 is not zero
     679                 : ** then it is assumed to be a string representation of the same integer.
     680                 : */
     681                 : case OP_Integer: {
     682            1524 :   pTos++;
     683            1524 :   pTos->i = pOp->p1;
     684            1524 :   pTos->flags = MEM_Int;
     685            1524 :   if( pOp->p3 ){
     686             177 :     pTos->z = pOp->p3;
     687             177 :     pTos->flags |= MEM_Str | MEM_Static;
     688             177 :     pTos->n = strlen(pOp->p3)+1;
     689                 :   }
     690            1524 :   break;
     691                 : }
     692                 : 
     693                 : /* Opcode: String * * P3
     694                 : **
     695                 : ** The string value P3 is pushed onto the stack.  If P3==0 then a
     696                 : ** NULL is pushed onto the stack.
     697                 : */
     698                 : case OP_String: {
     699            1191 :   char *z = pOp->p3;
     700            1191 :   pTos++;
     701            1191 :   if( z==0 ){
     702             215 :     pTos->flags = MEM_Null;
     703                 :   }else{
     704             976 :     pTos->z = z;
     705             976 :     pTos->n = strlen(z) + 1;
     706             976 :     pTos->flags = MEM_Str | MEM_Static;
     707                 :   }
     708            1191 :   break;
     709                 : }
     710                 : 
     711                 : /* Opcode: Variable P1 * *
     712                 : **
     713                 : ** Push the value of variable P1 onto the stack.  A variable is
     714                 : ** an unknown in the original SQL string as handed to sqlite_compile().
     715                 : ** Any occurance of the '?' character in the original SQL is considered
     716                 : ** a variable.  Variables in the SQL string are number from left to
     717                 : ** right beginning with 1.  The values of variables are set using the
     718                 : ** sqlite_bind() API.
     719                 : */
     720                 : case OP_Variable: {
     721               0 :   int j = pOp->p1 - 1;
     722               0 :   pTos++;
     723               0 :   if( j>=0 && j<p->nVar && p->azVar[j]!=0 ){
     724               0 :     pTos->z = p->azVar[j];
     725               0 :     pTos->n = p->anVar[j];
     726               0 :     pTos->flags = MEM_Str | MEM_Static;
     727                 :   }else{
     728               0 :     pTos->flags = MEM_Null;
     729                 :   }
     730               0 :   break;
     731                 : }
     732                 : 
     733                 : /* Opcode: Pop P1 * *
     734                 : **
     735                 : ** P1 elements are popped off of the top of stack and discarded.
     736                 : */
     737                 : case OP_Pop: {
     738                 :   assert( pOp->p1>=0 );
     739               3 :   popStack(&pTos, pOp->p1);
     740                 :   assert( pTos>=&p->aStack[-1] );
     741               3 :   break;
     742                 : }
     743                 : 
     744                 : /* Opcode: Dup P1 P2 *
     745                 : **
     746                 : ** A copy of the P1-th element of the stack 
     747                 : ** is made and pushed onto the top of the stack.
     748                 : ** The top of the stack is element 0.  So the
     749                 : ** instruction "Dup 0 0 0" will make a copy of the
     750                 : ** top of the stack.
     751                 : **
     752                 : ** If the content of the P1-th element is a dynamically
     753                 : ** allocated string, then a new copy of that string
     754                 : ** is made if P2==0.  If P2!=0, then just a pointer
     755                 : ** to the string is copied.
     756                 : **
     757                 : ** Also see the Pull instruction.
     758                 : */
     759                 : case OP_Dup: {
     760             752 :   Mem *pFrom = &pTos[-pOp->p1];
     761                 :   assert( pFrom<=pTos && pFrom>=p->aStack );
     762             752 :   pTos++;
     763             752 :   memcpy(pTos, pFrom, sizeof(*pFrom)-NBFS);
     764             752 :   if( pTos->flags & MEM_Str ){
     765             267 :     if( pOp->p2 && (pTos->flags & (MEM_Dyn|MEM_Ephem)) ){
     766               0 :       pTos->flags &= ~MEM_Dyn;
     767               0 :       pTos->flags |= MEM_Ephem;
     768             267 :     }else if( pTos->flags & MEM_Short ){
     769               0 :       memcpy(pTos->zShort, pFrom->zShort, pTos->n);
     770               0 :       pTos->z = pTos->zShort;
     771             267 :     }else if( (pTos->flags & MEM_Static)==0 ){
     772               0 :       pTos->z = sqliteMallocRaw(pFrom->n);
     773               0 :       if( sqlite_malloc_failed ) goto no_mem;
     774               0 :       memcpy(pTos->z, pFrom->z, pFrom->n);
     775               0 :       pTos->flags &= ~(MEM_Static|MEM_Ephem|MEM_Short);
     776               0 :       pTos->flags |= MEM_Dyn;
     777                 :     }
     778                 :   }
     779             752 :   break;
     780                 : }
     781                 : 
     782                 : /* Opcode: Pull P1 * *
     783                 : **
     784                 : ** The P1-th element is removed from its current location on 
     785                 : ** the stack and pushed back on top of the stack.  The
     786                 : ** top of the stack is element 0, so "Pull 0 0 0" is
     787                 : ** a no-op.  "Pull 1 0 0" swaps the top two elements of
     788                 : ** the stack.
     789                 : **
     790                 : ** See also the Dup instruction.
     791                 : */
     792                 : case OP_Pull: {
     793              95 :   Mem *pFrom = &pTos[-pOp->p1];
     794                 :   int i;
     795                 :   Mem ts;
     796                 : 
     797              95 :   ts = *pFrom;
     798              95 :   Deephemeralize(pTos);
     799             190 :   for(i=0; i<pOp->p1; i++, pFrom++){
     800              95 :     Deephemeralize(&pFrom[1]);
     801              95 :     *pFrom = pFrom[1];
     802                 :     assert( (pFrom->flags & MEM_Ephem)==0 );
     803              95 :     if( pFrom->flags & MEM_Short ){
     804                 :       assert( pFrom->flags & MEM_Str );
     805                 :       assert( pFrom->z==pFrom[1].zShort );
     806               0 :       pFrom->z = pFrom->zShort;
     807                 :     }
     808                 :   }
     809              95 :   *pTos = ts;
     810              95 :   if( pTos->flags & MEM_Short ){
     811                 :     assert( pTos->flags & MEM_Str );
     812                 :     assert( pTos->z==pTos[-pOp->p1].zShort );
     813               0 :     pTos->z = pTos->zShort;
     814                 :   }
     815              95 :   break;
     816                 : }
     817                 : 
     818                 : /* Opcode: Push P1 * *
     819                 : **
     820                 : ** Overwrite the value of the P1-th element down on the
     821                 : ** stack (P1==0 is the top of the stack) with the value
     822                 : ** of the top of the stack.  Then pop the top of the stack.
     823                 : */
     824                 : case OP_Push: {
     825               0 :   Mem *pTo = &pTos[-pOp->p1];
     826                 : 
     827                 :   assert( pTo>=p->aStack );
     828               0 :   Deephemeralize(pTos);
     829               0 :   Release(pTo);
     830               0 :   *pTo = *pTos;
     831               0 :   if( pTo->flags & MEM_Short ){
     832                 :     assert( pTo->z==pTos->zShort );
     833               0 :     pTo->z = pTo->zShort;
     834                 :   }
     835               0 :   pTos--;
     836               0 :   break;
     837                 : }
     838                 : 
     839                 : 
     840                 : /* Opcode: ColumnName P1 P2 P3
     841                 : **
     842                 : ** P3 becomes the P1-th column name (first is 0).  An array of pointers
     843                 : ** to all column names is passed as the 4th parameter to the callback.
     844                 : ** If P2==1 then this is the last column in the result set and thus the
     845                 : ** number of columns in the result set will be P1.  There must be at least
     846                 : ** one OP_ColumnName with a P2==1 before invoking OP_Callback and the
     847                 : ** number of columns specified in OP_Callback must one more than the P1
     848                 : ** value of the OP_ColumnName that has P2==1.
     849                 : */
     850                 : case OP_ColumnName: {
     851                 :   assert( pOp->p1>=0 && pOp->p1<p->nOp );
     852            3834 :   p->azColName[pOp->p1] = pOp->p3;
     853            3834 :   p->nCallback = 0;
     854            3834 :   if( pOp->p2 ) p->nResColumn = pOp->p1+1;
     855            3834 :   break;
     856                 : }
     857                 : 
     858                 : /* Opcode: Callback P1 * *
     859                 : **
     860                 : ** Pop P1 values off the stack and form them into an array.  Then
     861                 : ** invoke the callback function using the newly formed array as the
     862                 : ** 3rd parameter.
     863                 : */
     864                 : case OP_Callback: {
     865                 :   int i;
     866             425 :   char **azArgv = p->zArgv;
     867                 :   Mem *pCol;
     868                 : 
     869             425 :   pCol = &pTos[1-pOp->p1];
     870                 :   assert( pCol>=p->aStack );
     871            1224 :   for(i=0; i<pOp->p1; i++, pCol++){
     872             799 :     if( pCol->flags & MEM_Null ){
     873              21 :       azArgv[i] = 0;
     874                 :     }else{
     875             778 :       Stringify(pCol);
     876             778 :       azArgv[i] = pCol->z;
     877                 :     }
     878                 :   }
     879             425 :   azArgv[i] = 0;
     880             425 :   p->nCallback++;
     881             425 :   p->azResColumn = azArgv;
     882                 :   assert( p->nResColumn==pOp->p1 );
     883             425 :   p->popStack = pOp->p1;
     884             425 :   p->pc = pc + 1;
     885             425 :   p->pTos = pTos;
     886             425 :   return SQLITE_ROW;
     887                 : }
     888                 : 
     889                 : /* Opcode: Concat P1 P2 P3
     890                 : **
     891                 : ** Look at the first P1 elements of the stack.  Append them all 
     892                 : ** together with the lowest element first.  Use P3 as a separator.  
     893                 : ** Put the result on the top of the stack.  The original P1 elements
     894                 : ** are popped from the stack if P2==0 and retained if P2==1.  If
     895                 : ** any element of the stack is NULL, then the result is NULL.
     896                 : **
     897                 : ** If P3 is NULL, then use no separator.  When P1==1, this routine
     898                 : ** makes a copy of the top stack element into memory obtained
     899                 : ** from sqliteMalloc().
     900                 : */
     901                 : case OP_Concat: {
     902                 :   char *zNew;
     903                 :   int nByte;
     904                 :   int nField;
     905                 :   int i, j;
     906                 :   char *zSep;
     907                 :   int nSep;
     908                 :   Mem *pTerm;
     909                 : 
     910               0 :   nField = pOp->p1;
     911               0 :   zSep = pOp->p3;
     912               0 :   if( zSep==0 ) zSep = "";
     913               0 :   nSep = strlen(zSep);
     914                 :   assert( &pTos[1-nField] >= p->aStack );
     915               0 :   nByte = 1 - nSep;
     916               0 :   pTerm = &pTos[1-nField];
     917               0 :   for(i=0; i<nField; i++, pTerm++){
     918               0 :     if( pTerm->flags & MEM_Null ){
     919               0 :       nByte = -1;
     920               0 :       break;
     921                 :     }else{
     922               0 :       Stringify(pTerm);
     923               0 :       nByte += pTerm->n - 1 + nSep;
     924                 :     }
     925                 :   }
     926               0 :   if( nByte<0 ){
     927               0 :     if( pOp->p2==0 ){
     928               0 :       popStack(&pTos, nField);
     929                 :     }
     930               0 :     pTos++;
     931               0 :     pTos->flags = MEM_Null;
     932               0 :     break;
     933                 :   }
     934               0 :   zNew = sqliteMallocRaw( nByte );
     935               0 :   if( zNew==0 ) goto no_mem;
     936               0 :   j = 0;
     937               0 :   pTerm = &pTos[1-nField];
     938               0 :   for(i=j=0; i<nField; i++, pTerm++){
     939                 :     assert( pTerm->flags & MEM_Str );
     940               0 :     memcpy(&zNew[j], pTerm->z, pTerm->n-1);
     941               0 :     j += pTerm->n-1;
     942               0 :     if( nSep>0 && i<nField-1 ){
     943               0 :       memcpy(&zNew[j], zSep, nSep);
     944               0 :       j += nSep;
     945                 :     }
     946                 :   }
     947               0 :   zNew[j] = 0;
     948               0 :   if( pOp->p2==0 ){
     949               0 :     popStack(&pTos, nField);
     950                 :   }
     951               0 :   pTos++;
     952               0 :   pTos->n = nByte;
     953               0 :   pTos->flags = MEM_Str|MEM_Dyn;
     954               0 :   pTos->z = zNew;
     955               0 :   break;
     956                 : }
     957                 : 
     958                 : /* Opcode: Add * * *
     959                 : **
     960                 : ** Pop the top two elements from the stack, add them together,
     961                 : ** and push the result back onto the stack.  If either element
     962                 : ** is a string then it is converted to a double using the atof()
     963                 : ** function before the addition.
     964                 : ** If either operand is NULL, the result is NULL.
     965                 : */
     966                 : /* Opcode: Multiply * * *
     967                 : **
     968                 : ** Pop the top two elements from the stack, multiply them together,
     969                 : ** and push the result back onto the stack.  If either element
     970                 : ** is a string then it is converted to a double using the atof()
     971                 : ** function before the multiplication.
     972                 : ** If either operand is NULL, the result is NULL.
     973                 : */
     974                 : /* Opcode: Subtract * * *
     975                 : **
     976                 : ** Pop the top two elements from the stack, subtract the
     977                 : ** first (what was on top of the stack) from the second (the
     978                 : ** next on stack)
     979                 : ** and push the result back onto the stack.  If either element
     980                 : ** is a string then it is converted to a double using the atof()
     981                 : ** function before the subtraction.
     982                 : ** If either operand is NULL, the result is NULL.
     983                 : */
     984                 : /* Opcode: Divide * * *
     985                 : **
     986                 : ** Pop the top two elements from the stack, divide the
     987                 : ** first (what was on top of the stack) from the second (the
     988                 : ** next on stack)
     989                 : ** and push the result back onto the stack.  If either element
     990                 : ** is a string then it is converted to a double using the atof()
     991                 : ** function before the division.  Division by zero returns NULL.
     992                 : ** If either operand is NULL, the result is NULL.
     993                 : */
     994                 : /* Opcode: Remainder * * *
     995                 : **
     996                 : ** Pop the top two elements from the stack, divide the
     997                 : ** first (what was on top of the stack) from the second (the
     998                 : ** next on stack)
     999                 : ** and push the remainder after division onto the stack.  If either element
    1000                 : ** is a string then it is converted to a double using the atof()
    1001                 : ** function before the division.  Division by zero returns NULL.
    1002                 : ** If either operand is NULL, the result is NULL.
    1003                 : */
    1004                 : case OP_Add:
    1005                 : case OP_Subtract:
    1006                 : case OP_Multiply:
    1007                 : case OP_Divide:
    1008                 : case OP_Remainder: {
    1009               0 :   Mem *pNos = &pTos[-1];
    1010                 :   assert( pNos>=p->aStack );
    1011               0 :   if( ((pTos->flags | pNos->flags) & MEM_Null)!=0 ){
    1012               0 :     Release(pTos);
    1013               0 :     pTos--;
    1014               0 :     Release(pTos);
    1015               0 :     pTos->flags = MEM_Null;
    1016               0 :   }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){
    1017                 :     int a, b;
    1018               0 :     a = pTos->i;
    1019               0 :     b = pNos->i;
    1020               0 :     switch( pOp->opcode ){
    1021               0 :       case OP_Add:         b += a;       break;
    1022               0 :       case OP_Subtract:    b -= a;       break;
    1023               0 :       case OP_Multiply:    b *= a;       break;
    1024                 :       case OP_Divide: {
    1025               0 :         if( a==0 ) goto divide_by_zero;
    1026               0 :         b /= a;
    1027               0 :         break;
    1028                 :       }
    1029                 :       default: {
    1030               0 :         if( a==0 ) goto divide_by_zero;
    1031               0 :         b %= a;
    1032                 :         break;
    1033                 :       }
    1034                 :     }
    1035               0 :     Release(pTos);
    1036               0 :     pTos--;
    1037               0 :     Release(pTos);
    1038               0 :     pTos->i = b;
    1039               0 :     pTos->flags = MEM_Int;
    1040                 :   }else{
    1041                 :     double a, b;
    1042               0 :     Realify(pTos);
    1043               0 :     Realify(pNos);
    1044               0 :     a = pTos->r;
    1045               0 :     b = pNos->r;
    1046               0 :     switch( pOp->opcode ){
    1047               0 :       case OP_Add:         b += a;       break;
    1048               0 :       case OP_Subtract:    b -= a;       break;
    1049               0 :       case OP_Multiply:    b *= a;       break;
    1050                 :       case OP_Divide: {
    1051               0 :         if( a==0.0 ) goto divide_by_zero;
    1052               0 :         b /= a;
    1053               0 :         break;
    1054                 :       }
    1055                 :       default: {
    1056               0 :         int ia = (int)a;
    1057               0 :         int ib = (int)b;
    1058               0 :         if( ia==0.0 ) goto divide_by_zero;
    1059               0 :         b = ib % ia;
    1060                 :         break;
    1061                 :       }
    1062                 :     }
    1063               0 :     Release(pTos);
    1064               0 :     pTos--;
    1065               0 :     Release(pTos);
    1066               0 :     pTos->r = b;
    1067               0 :     pTos->flags = MEM_Real;
    1068                 :   }
    1069               0 :   break;
    1070                 : 
    1071               0 : divide_by_zero:
    1072               0 :   Release(pTos);
    1073               0 :   pTos--;
    1074               0 :   Release(pTos);
    1075               0 :   pTos->flags = MEM_Null;
    1076               0 :   break;
    1077                 : }
    1078                 : 
    1079                 : /* Opcode: Function P1 * P3
    1080                 : **
    1081                 : ** Invoke a user function (P3 is a pointer to a Function structure that
    1082                 : ** defines the function) with P1 string arguments taken from the stack.
    1083                 : ** Pop all arguments from the stack and push back the result.
    1084                 : **
    1085                 : ** See also: AggFunc
    1086                 : */
    1087                 : case OP_Function: {
    1088                 :   int n, i;
    1089                 :   Mem *pArg;
    1090                 :   char **azArgv;
    1091                 :   sqlite_func ctx;
    1092                 : 
    1093              11 :   n = pOp->p1;
    1094              11 :   pArg = &pTos[1-n];
    1095              11 :   azArgv = p->zArgv;
    1096              32 :   for(i=0; i<n; i++, pArg++){
    1097              21 :     if( pArg->flags & MEM_Null ){
    1098               0 :       azArgv[i] = 0;
    1099                 :     }else{
    1100              21 :       Stringify(pArg);
    1101              21 :       azArgv[i] = pArg->z;
    1102                 :     }
    1103                 :   }
    1104              11 :   ctx.pFunc = (FuncDef*)pOp->p3;
    1105              11 :   ctx.s.flags = MEM_Null;
    1106              11 :   ctx.s.z = 0;
    1107              11 :   ctx.isError = 0;
    1108              11 :   ctx.isStep = 0;
    1109              11 :   if( sqliteSafetyOff(db) ) goto abort_due_to_misuse;
    1110              11 :   (*ctx.pFunc->xFunc)(&ctx, n, (const char**)azArgv);
    1111              11 :   if( sqliteSafetyOn(db) ) goto abort_due_to_misuse;
    1112              11 :   popStack(&pTos, n);
    1113              11 :   pTos++;
    1114              11 :   *pTos = ctx.s;
    1115              11 :   if( pTos->flags & MEM_Short ){
    1116               8 :     pTos->z = pTos->zShort;
    1117                 :   }
    1118              11 :   if( ctx.isError ){
    1119               1 :     sqliteSetString(&p->zErrMsg, 
    1120                 :        (pTos->flags & MEM_Str)!=0 ? pTos->z : "user function error", (char*)0);
    1121               1 :     rc = SQLITE_ERROR;
    1122                 :   }
    1123              11 :   break;
    1124                 : }
    1125                 : 
    1126                 : /* Opcode: BitAnd * * *
    1127                 : **
    1128                 : ** Pop the top two elements from the stack.  Convert both elements
    1129                 : ** to integers.  Push back onto the stack the bit-wise AND of the
    1130                 : ** two elements.
    1131                 : ** If either operand is NULL, the result is NULL.
    1132                 : */
    1133                 : /* Opcode: BitOr * * *
    1134                 : **
    1135                 : ** Pop the top two elements from the stack.  Convert both elements
    1136                 : ** to integers.  Push back onto the stack the bit-wise OR of the
    1137                 : ** two elements.
    1138                 : ** If either operand is NULL, the result is NULL.
    1139                 : */
    1140                 : /* Opcode: ShiftLeft * * *
    1141                 : **
    1142                 : ** Pop the top two elements from the stack.  Convert both elements
    1143                 : ** to integers.  Push back onto the stack the top element shifted
    1144                 : ** left by N bits where N is the second element on the stack.
    1145                 : ** If either operand is NULL, the result is NULL.
    1146                 : */
    1147                 : /* Opcode: ShiftRight * * *
    1148                 : **
    1149                 : ** Pop the top two elements from the stack.  Convert both elements
    1150                 : ** to integers.  Push back onto the stack the top element shifted
    1151                 : ** right by N bits where N is the second element on the stack.
    1152                 : ** If either operand is NULL, the result is NULL.
    1153                 : */
    1154                 : case OP_BitAnd:
    1155                 : case OP_BitOr:
    1156                 : case OP_ShiftLeft:
    1157                 : case OP_ShiftRight: {
    1158               0 :   Mem *pNos = &pTos[-1];
    1159                 :   int a, b;
    1160                 : 
    1161                 :   assert( pNos>=p->aStack );
    1162               0 :   if( (pTos->flags | pNos->flags) & MEM_Null ){
    1163               0 :     popStack(&pTos, 2);
    1164               0 :     pTos++;
    1165               0 :     pTos->flags = MEM_Null;
    1166               0 :     break;
    1167                 :   }
    1168               0 :   Integerify(pTos);
    1169               0 :   Integerify(pNos);
    1170               0 :   a = pTos->i;
    1171               0 :   b = pNos->i;
    1172               0 :   switch( pOp->opcode ){
    1173               0 :     case OP_BitAnd:      a &= b;     break;
    1174               0 :     case OP_BitOr:       a |= b;     break;
    1175               0 :     case OP_ShiftLeft:   a <<= b;    break;
    1176               0 :     case OP_ShiftRight:  a >>= b;    break;
    1177                 :     default:   /* CANT HAPPEN */     break;
    1178                 :   }
    1179                 :   assert( (pTos->flags & MEM_Dyn)==0 );
    1180                 :   assert( (pNos->flags & MEM_Dyn)==0 );
    1181               0 :   pTos--;
    1182               0 :   Release(pTos);
    1183               0 :   pTos->i = a;
    1184               0 :   pTos->flags = MEM_Int;
    1185               0 :   break;
    1186                 : }
    1187                 : 
    1188                 : /* Opcode: AddImm  P1 * *
    1189                 : ** 
    1190                 : ** Add the value P1 to whatever is on top of the stack.  The result
    1191                 : ** is always an integer.
    1192                 : **
    1193                 : ** To force the top of the stack to be an integer, just add 0.
    1194                 : */
    1195                 : case OP_AddImm: {
    1196                 :   assert( pTos>=p->aStack );
    1197               0 :   Integerify(pTos);
    1198               0 :   pTos->i += pOp->p1;
    1199               0 :   break;
    1200                 : }
    1201                 : 
    1202                 : /* Opcode: ForceInt P1 P2 *
    1203                 : **
    1204                 : ** Convert the top of the stack into an integer.  If the current top of
    1205                 : ** the stack is not numeric (meaning that is is a NULL or a string that
    1206                 : ** does not look like an integer or floating point number) then pop the
    1207                 : ** stack and jump to P2.  If the top of the stack is numeric then
    1208                 : ** convert it into the least integer that is greater than or equal to its
    1209                 : ** current value if P1==0, or to the least integer that is strictly
    1210                 : ** greater than its current value if P1==1.
    1211                 : */
    1212                 : case OP_ForceInt: {
    1213                 :   int v;
    1214                 :   assert( pTos>=p->aStack );
    1215               0 :   if( (pTos->flags & (MEM_Int|MEM_Real))==0
    1216                 :          && ((pTos->flags & MEM_Str)==0 || sqliteIsNumber(pTos->z)==0) ){
    1217               0 :     Release(pTos);
    1218               0 :     pTos--;
    1219               0 :     pc = pOp->p2 - 1;
    1220               0 :     break;
    1221                 :   }
    1222               0 :   if( pTos->flags & MEM_Int ){
    1223               0 :     v = pTos->i + (pOp->p1!=0);
    1224                 :   }else{
    1225               0 :     Realify(pTos);
    1226               0 :     v = (int)pTos->r;
    1227               0 :     if( pTos->r>(double)v ) v++;
    1228               0 :     if( pOp->p1 && pTos->r==(double)v ) v++;
    1229                 :   }
    1230               0 :   Release(pTos);
    1231               0 :   pTos->i = v;
    1232               0 :   pTos->flags = MEM_Int;
    1233               0 :   break;
    1234                 : }
    1235                 : 
    1236                 : /* Opcode: MustBeInt P1 P2 *
    1237                 : ** 
    1238                 : ** Force the top of the stack to be an integer.  If the top of the
    1239                 : ** stack is not an integer and cannot be converted into an integer
    1240                 : ** with out data loss, then jump immediately to P2, or if P2==0
    1241                 : ** raise an SQLITE_MISMATCH exception.
    1242                 : **
    1243                 : ** If the top of the stack is not an integer and P2 is not zero and
    1244                 : ** P1 is 1, then the stack is popped.  In all other cases, the depth
    1245                 : ** of the stack is unchanged.
    1246                 : */
    1247                 : case OP_MustBeInt: {
    1248                 :   assert( pTos>=p->aStack );
    1249              15 :   if( pTos->flags & MEM_Int ){
    1250                 :     /* Do nothing */
    1251               1 :   }else if( pTos->flags & MEM_Real ){
    1252               0 :     int i = (int)pTos->r;
    1253               0 :     double r = (double)i;
    1254               0 :     if( r!=pTos->r ){
    1255               0 :       goto mismatch;
    1256                 :     }
    1257               0 :     pTos->i = i;
    1258               1 :   }else if( pTos->flags & MEM_Str ){
    1259                 :     int v;
    1260               1 :     if( !toInt(pTos->z, &v) ){
    1261                 :       double r;
    1262               0 :       if( !sqliteIsNumber(pTos->z) ){
    1263               0 :         goto mismatch;
    1264                 :       }
    1265               0 :       Realify(pTos);
    1266               0 :       v = (int)pTos->r;
    1267               0 :       r = (double)v;
    1268               0 :       if( r!=pTos->r ){
    1269               0 :         goto mismatch;
    1270                 :       }
    1271                 :     }
    1272               1 :     pTos->i = v;
    1273                 :   }else{
    1274               0 :     goto mismatch;
    1275                 :   }
    1276              15 :   Release(pTos);
    1277              15 :   pTos->flags = MEM_Int;
    1278              15 :   break;
    1279                 : 
    1280               0 : mismatch:
    1281               0 :   if( pOp->p2==0 ){
    1282               0 :     rc = SQLITE_MISMATCH;
    1283               0 :     goto abort_due_to_error;
    1284                 :   }else{
    1285               0 :     if( pOp->p1 ) popStack(&pTos, 1);
    1286               0 :     pc = pOp->p2 - 1;
    1287                 :   }
    1288               0 :   break;
    1289                 : }
    1290                 : 
    1291                 : /* Opcode: Eq P1 P2 *
    1292                 : **
    1293                 : ** Pop the top two elements from the stack.  If they are equal, then
    1294                 : ** jump to instruction P2.  Otherwise, continue to the next instruction.
    1295                 : **
    1296                 : ** If either operand is NULL (and thus if the result is unknown) then
    1297                 : ** take the jump if P1 is true.
    1298                 : **
    1299                 : ** If both values are numeric, they are converted to doubles using atof()
    1300                 : ** and compared for equality that way.  Otherwise the strcmp() library
    1301                 : ** routine is used for the comparison.  For a pure text comparison
    1302                 : ** use OP_StrEq.
    1303                 : **
    1304                 : ** If P2 is zero, do not jump.  Instead, push an integer 1 onto the
    1305                 : ** stack if the jump would have been taken, or a 0 if not.  Push a
    1306                 : ** NULL if either operand was NULL.
    1307                 : */
    1308                 : /* Opcode: Ne P1 P2 *
    1309                 : **
    1310                 : ** Pop the top two elements from the stack.  If they are not equal, then
    1311                 : ** jump to instruction P2.  Otherwise, continue to the next instruction.
    1312                 : **
    1313                 : ** If either operand is NULL (and thus if the result is unknown) then
    1314                 : ** take the jump if P1 is true.
    1315                 : **
    1316                 : ** If both values are numeric, they are converted to doubles using atof()
    1317                 : ** and compared in that format.  Otherwise the strcmp() library
    1318                 : ** routine is used for the comparison.  For a pure text comparison
    1319                 : ** use OP_StrNe.
    1320                 : **
    1321                 : ** If P2 is zero, do not jump.  Instead, push an integer 1 onto the
    1322                 : ** stack if the jump would have been taken, or a 0 if not.  Push a
    1323                 : ** NULL if either operand was NULL.
    1324                 : */
    1325                 : /* Opcode: Lt P1 P2 *
    1326                 : **
    1327                 : ** Pop the top two elements from the stack.  If second element (the
    1328                 : ** next on stack) is less than the first (the top of stack), then
    1329                 : ** jump to instruction P2.  Otherwise, continue to the next instruction.
    1330                 : ** In other words, jump if NOS<TOS.
    1331                 : **
    1332                 : ** If either operand is NULL (and thus if the result is unknown) then
    1333                 : ** take the jump if P1 is true.
    1334                 : **
    1335                 : ** If both values are numeric, they are converted to doubles using atof()
    1336                 : ** and compared in that format.  Numeric values are always less than
    1337                 : ** non-numeric values.  If both operands are non-numeric, the strcmp() library
    1338                 : ** routine is used for the comparison.  For a pure text comparison
    1339                 : ** use OP_StrLt.
    1340                 : **
    1341                 : ** If P2 is zero, do not jump.  Instead, push an integer 1 onto the
    1342                 : ** stack if the jump would have been taken, or a 0 if not.  Push a
    1343                 : ** NULL if either operand was NULL.
    1344                 : */
    1345                 : /* Opcode: Le P1 P2 *
    1346                 : **
    1347                 : ** Pop the top two elements from the stack.  If second element (the
    1348                 : ** next on stack) is less than or equal to the first (the top of stack),
    1349                 : ** then jump to instruction P2. In other words, jump if NOS<=TOS.
    1350                 : **
    1351                 : ** If either operand is NULL (and thus if the result is unknown) then
    1352                 : ** take the jump if P1 is true.
    1353                 : **
    1354                 : ** If both values are numeric, they are converted to doubles using atof()
    1355                 : ** and compared in that format.  Numeric values are always less than
    1356                 : ** non-numeric values.  If both operands are non-numeric, the strcmp() library
    1357                 : ** routine is used for the comparison.  For a pure text comparison
    1358                 : ** use OP_StrLe.
    1359                 : **
    1360                 : ** If P2 is zero, do not jump.  Instead, push an integer 1 onto the
    1361                 : ** stack if the jump would have been taken, or a 0 if not.  Push a
    1362                 : ** NULL if either operand was NULL.
    1363                 : */
    1364                 : /* Opcode: Gt P1 P2 *
    1365                 : **
    1366                 : ** Pop the top two elements from the stack.  If second element (the
    1367                 : ** next on stack) is greater than the first (the top of stack),
    1368                 : ** then jump to instruction P2. In other words, jump if NOS>TOS.
    1369                 : **
    1370                 : ** If either operand is NULL (and thus if the result is unknown) then
    1371                 : ** take the jump if P1 is true.
    1372                 : **
    1373                 : ** If both values are numeric, they are converted to doubles using atof()
    1374                 : ** and compared in that format.  Numeric values are always less than
    1375                 : ** non-numeric values.  If both operands are non-numeric, the strcmp() library
    1376                 : ** routine is used for the comparison.  For a pure text comparison
    1377                 : ** use OP_StrGt.
    1378                 : **
    1379                 : ** If P2 is zero, do not jump.  Instead, push an integer 1 onto the
    1380                 : ** stack if the jump would have been taken, or a 0 if not.  Push a
    1381                 : ** NULL if either operand was NULL.
    1382                 : */
    1383                 : /* Opcode: Ge P1 P2 *
    1384                 : **
    1385                 : ** Pop the top two elements from the stack.  If second element (the next
    1386                 : ** on stack) is greater than or equal to the first (the top of stack),
    1387                 : ** then jump to instruction P2. In other words, jump if NOS>=TOS.
    1388                 : **
    1389                 : ** If either operand is NULL (and thus if the result is unknown) then
    1390                 : ** take the jump if P1 is true.
    1391                 : **
    1392                 : ** If both values are numeric, they are converted to doubles using atof()
    1393                 : ** and compared in that format.  Numeric values are always less than
    1394                 : ** non-numeric values.  If both operands are non-numeric, the strcmp() library
    1395                 : ** routine is used for the comparison.  For a pure text comparison
    1396                 : ** use OP_StrGe.
    1397                 : **
    1398                 : ** If P2 is zero, do not jump.  Instead, push an integer 1 onto the
    1399                 : ** stack if the jump would have been taken, or a 0 if not.  Push a
    1400                 : ** NULL if either operand was NULL.
    1401                 : */
    1402                 : case OP_Eq:
    1403                 : case OP_Ne:
    1404                 : case OP_Lt:
    1405                 : case OP_Le:
    1406                 : case OP_Gt:
    1407                 : case OP_Ge: {
    1408              22 :   Mem *pNos = &pTos[-1];
    1409                 :   int c, v;
    1410                 :   int ft, fn;
    1411                 :   assert( pNos>=p->aStack );
    1412              22 :   ft = pTos->flags;
    1413              22 :   fn = pNos->flags;
    1414              22 :   if( (ft | fn) & MEM_Null ){
    1415               0 :     popStack(&pTos, 2);
    1416               0 :     if( pOp->p2 ){
    1417               0 :       if( pOp->p1 ) pc = pOp->p2-1;
    1418                 :     }else{
    1419               0 :       pTos++;
    1420               0 :       pTos->flags = MEM_Null;
    1421                 :     }
    1422               0 :     break;
    1423              22 :   }else if( (ft & fn & MEM_Int)==MEM_Int ){
    1424              10 :     c = pNos->i - pTos->i;
    1425              22 :   }else if( (ft & MEM_Int)!=0 && (fn & MEM_Str)!=0 && toInt(pNos->z,&v) ){
    1426              10 :     c = v - pTos->i;
    1427               2 :   }else if( (fn & MEM_Int)!=0 && (ft & MEM_Str)!=0 && toInt(pTos->z,&v) ){
    1428               0 :     c = pNos->i - v;
    1429                 :   }else{
    1430               2 :     Stringify(pTos);
    1431               2 :     Stringify(pNos);
    1432               2 :     c = sqliteCompare(pNos->z, pTos->z);
    1433                 :   }
    1434              22 :   switch( pOp->opcode ){
    1435               0 :     case OP_Eq:    c = c==0;     break;
    1436               3 :     case OP_Ne:    c = c!=0;     break;
    1437               0 :     case OP_Lt:    c = c<0;      break;
    1438               9 :     case OP_Le:    c = c<=0;     break;
    1439               0 :     case OP_Gt:    c = c>0;      break;
    1440              10 :     default:       c = c>=0;     break;
    1441                 :   }
    1442              22 :   popStack(&pTos, 2);
    1443              22 :   if( pOp->p2 ){
    1444              22 :     if( c ) pc = pOp->p2-1;
    1445                 :   }else{
    1446               0 :     pTos++;
    1447               0 :     pTos->i = c;
    1448               0 :     pTos->flags = MEM_Int;
    1449                 :   }
    1450              22 :   break;
    1451                 : }
    1452                 : /* INSERT NO CODE HERE!
    1453                 : **
    1454                 : ** The opcode numbers are extracted from this source file by doing
    1455                 : **
    1456                 : **    grep '^case OP_' vdbe.c | ... >opcodes.h
    1457                 : **
    1458                 : ** The opcodes are numbered in the order that they appear in this file.
    1459                 : ** But in order for the expression generating code to work right, the
    1460                 : ** string comparison operators that follow must be numbered exactly 6
    1461                 : ** greater than the numeric comparison opcodes above.  So no other
    1462                 : ** cases can appear between the two.
    1463                 : */
    1464                 : /* Opcode: StrEq P1 P2 *
    1465                 : **
    1466                 : ** Pop the top two elements from the stack.  If they are equal, then
    1467                 : ** jump to instruction P2.  Otherwise, continue to the next instruction.
    1468                 : **
    1469                 : ** If either operand is NULL (and thus if the result is unknown) then
    1470                 : ** take the jump if P1 is true.
    1471                 : **
    1472                 : ** The strcmp() library routine is used for the comparison.  For a
    1473                 : ** numeric comparison, use OP_Eq.
    1474                 : **
    1475                 : ** If P2 is zero, do not jump.  Instead, push an integer 1 onto the
    1476                 : ** stack if the jump would have been taken, or a 0 if not.  Push a
    1477                 : ** NULL if either operand was NULL.
    1478                 : */
    1479                 : /* Opcode: StrNe P1 P2 *
    1480                 : **
    1481                 : ** Pop the top two elements from the stack.  If they are not equal, then
    1482                 : ** jump to instruction P2.  Otherwise, continue to the next instruction.
    1483                 : **
    1484                 : ** If either operand is NULL (and thus if the result is unknown) then
    1485                 : ** take the jump if P1 is true.
    1486                 : **
    1487                 : ** The strcmp() library routine is used for the comparison.  For a
    1488                 : ** numeric comparison, use OP_Ne.
    1489                 : **
    1490                 : ** If P2 is zero, do not jump.  Instead, push an integer 1 onto the
    1491                 : ** stack if the jump would have been taken, or a 0 if not.  Push a
    1492                 : ** NULL if either operand was NULL.
    1493                 : */
    1494                 : /* Opcode: StrLt P1 P2 *
    1495                 : **
    1496                 : ** Pop the top two elements from the stack.  If second element (the
    1497                 : ** next on stack) is less than the first (the top of stack), then
    1498                 : ** jump to instruction P2.  Otherwise, continue to the next instruction.
    1499                 : ** In other words, jump if NOS<TOS.
    1500                 : **
    1501                 : ** If either operand is NULL (and thus if the result is unknown) then
    1502                 : ** take the jump if P1 is true.
    1503                 : **
    1504                 : ** The strcmp() library routine is used for the comparison.  For a
    1505                 : ** numeric comparison, use OP_Lt.
    1506                 : **
    1507                 : ** If P2 is zero, do not jump.  Instead, push an integer 1 onto the
    1508                 : ** stack if the jump would have been taken, or a 0 if not.  Push a
    1509                 : ** NULL if either operand was NULL.
    1510                 : */
    1511                 : /* Opcode: StrLe P1 P2 *
    1512                 : **
    1513                 : ** Pop the top two elements from the stack.  If second element (the
    1514                 : ** next on stack) is less than or equal to the first (the top of stack),
    1515                 : ** then jump to instruction P2. In other words, jump if NOS<=TOS.
    1516                 : **
    1517                 : ** If either operand is NULL (and thus if the result is unknown) then
    1518                 : ** take the jump if P1 is true.
    1519                 : **
    1520                 : ** The strcmp() library routine is used for the comparison.  For a
    1521                 : ** numeric comparison, use OP_Le.
    1522                 : **
    1523                 : ** If P2 is zero, do not jump.  Instead, push an integer 1 onto the
    1524                 : ** stack if the jump would have been taken, or a 0 if not.  Push a
    1525                 : ** NULL if either operand was NULL.
    1526                 : */
    1527                 : /* Opcode: StrGt P1 P2 *
    1528                 : **
    1529                 : ** Pop the top two elements from the stack.  If second element (the
    1530                 : ** next on stack) is greater than the first (the top of stack),
    1531                 : ** then jump to instruction P2. In other words, jump if NOS>TOS.
    1532                 : **
    1533                 : ** If either operand is NULL (and thus if the result is unknown) then
    1534                 : ** take the jump if P1 is true.
    1535                 : **
    1536                 : ** The strcmp() library routine is used for the comparison.  For a
    1537                 : ** numeric comparison, use OP_Gt.
    1538                 : **
    1539                 : ** If P2 is zero, do not jump.  Instead, push an integer 1 onto the
    1540                 : ** stack if the jump would have been taken, or a 0 if not.  Push a
    1541                 : ** NULL if either operand was NULL.
    1542                 : */
    1543                 : /* Opcode: StrGe P1 P2 *
    1544                 : **
    1545                 : ** Pop the top two elements from the stack.  If second element (the next
    1546                 : ** on stack) is greater than or equal to the first (the top of stack),
    1547                 : ** then jump to instruction P2. In other words, jump if NOS>=TOS.
    1548                 : **
    1549                 : ** If either operand is NULL (and thus if the result is unknown) then
    1550                 : ** take the jump if P1 is true.
    1551                 : **
    1552                 : ** The strcmp() library routine is used for the comparison.  For a
    1553                 : ** numeric comparison, use OP_Ge.
    1554                 : **
    1555                 : ** If P2 is zero, do not jump.  Instead, push an integer 1 onto the
    1556                 : ** stack if the jump would have been taken, or a 0 if not.  Push a
    1557                 : ** NULL if either operand was NULL.
    1558                 : */
    1559                 : case OP_StrEq:
    1560                 : case OP_StrNe:
    1561                 : case OP_StrLt:
    1562                 : case OP_StrLe:
    1563                 : case OP_StrGt:
    1564                 : case OP_StrGe: {
    1565              40 :   Mem *pNos = &pTos[-1];
    1566                 :   int c;
    1567                 :   assert( pNos>=p->aStack );
    1568              40 :   if( (pNos->flags | pTos->flags) & MEM_Null ){
    1569               0 :     popStack(&pTos, 2);
    1570               0 :     if( pOp->p2 ){
    1571               0 :       if( pOp->p1 ) pc = pOp->p2-1;
    1572                 :     }else{
    1573               0 :       pTos++;
    1574               0 :       pTos->flags = MEM_Null;
    1575                 :     }
    1576               0 :     break;
    1577                 :   }else{
    1578              40 :     Stringify(pTos);
    1579              40 :     Stringify(pNos);
    1580              40 :     c = strcmp(pNos->z, pTos->z);
    1581                 :   }
    1582                 :   /* The asserts on each case of the following switch are there to verify
    1583                 :   ** that string comparison opcodes are always exactly 6 greater than the
    1584                 :   ** corresponding numeric comparison opcodes.  The code generator depends
    1585                 :   ** on this fact.
    1586                 :   */
    1587              40 :   switch( pOp->opcode ){
    1588               3 :     case OP_StrEq:    c = c==0;    assert( pOp->opcode-6==OP_Eq );   break;
    1589              37 :     case OP_StrNe:    c = c!=0;    assert( pOp->opcode-6==OP_Ne );   break;
    1590               0 :     case OP_StrLt:    c = c<0;     assert( pOp->opcode-6==OP_Lt );   break;
    1591               0 :     case OP_StrLe:    c = c<=0;    assert( pOp->opcode-6==OP_Le );   break;
    1592               0 :     case OP_StrGt:    c = c>0;     assert( pOp->opcode-6==OP_Gt );   break;
    1593               0 :     default:          c = c>=0;    assert( pOp->opcode-6==OP_Ge );   break;
    1594                 :   }
    1595              40 :   popStack(&pTos, 2);
    1596              40 :   if( pOp->p2 ){
    1597              40 :     if( c ) pc = pOp->p2-1;
    1598                 :   }else{
    1599               0 :     pTos++;
    1600               0 :     pTos->flags = MEM_Int;
    1601               0 :     pTos->i = c;
    1602                 :   }
    1603              40 :   break;
    1604                 : }
    1605                 : 
    1606                 : /* Opcode: And * * *
    1607                 : **
    1608                 : ** Pop two values off the stack.  Take the logical AND of the
    1609                 : ** two values and push the resulting boolean value back onto the
    1610                 : ** stack. 
    1611                 : */
    1612                 : /* Opcode: Or * * *
    1613                 : **
    1614                 : ** Pop two values off the stack.  Take the logical OR of the
    1615                 : ** two values and push the resulting boolean value back onto the
    1616                 : ** stack. 
    1617                 : */
    1618                 : case OP_And:
    1619                 : case OP_Or: {
    1620               0 :   Mem *pNos = &pTos[-1];
    1621                 :   int v1, v2;    /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */
    1622                 : 
    1623                 :   assert( pNos>=p->aStack );
    1624               0 :   if( pTos->flags & MEM_Null ){
    1625               0 :     v1 = 2;
    1626                 :   }else{
    1627               0 :     Integerify(pTos);
    1628               0 :     v1 = pTos->i==0;
    1629                 :   }
    1630               0 :   if( pNos->flags & MEM_Null ){
    1631               0 :     v2 = 2;
    1632                 :   }else{
    1633               0 :     Integerify(pNos);
    1634               0 :     v2 = pNos->i==0;
    1635                 :   }
    1636               0 :   if( pOp->opcode==OP_And ){
    1637                 :     static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
    1638               0 :     v1 = and_logic[v1*3+v2];
    1639                 :   }else{
    1640                 :     static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
    1641               0 :     v1 = or_logic[v1*3+v2];
    1642                 :   }
    1643               0 :   popStack(&pTos, 2);
    1644               0 :   pTos++;
    1645               0 :   if( v1==2 ){
    1646               0 :     pTos->flags = MEM_Null;
    1647                 :   }else{
    1648               0 :     pTos->i = v1==0;
    1649               0 :     pTos->flags = MEM_Int;
    1650                 :   }
    1651               0 :   break;
    1652                 : }
    1653                 : 
    1654                 : /* Opcode: Negative * * *
    1655                 : **
    1656                 : ** Treat the top of the stack as a numeric quantity.  Replace it
    1657                 : ** with its additive inverse.  If the top of the stack is NULL
    1658                 : ** its value is unchanged.
    1659                 : */
    1660                 : /* Opcode: AbsValue * * *
    1661                 : **
    1662                 : ** Treat the top of the stack as a numeric quantity.  Replace it
    1663                 : ** with its absolute value. If the top of the stack is NULL
    1664                 : ** its value is unchanged.
    1665                 : */
    1666                 : case OP_Negative:
    1667                 : case OP_AbsValue: {
    1668                 :   assert( pTos>=p->aStack );
    1669               0 :   if( pTos->flags & MEM_Real ){
    1670               0 :     Release(pTos);
    1671               0 :     if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
    1672               0 :       pTos->r = -pTos->r;
    1673                 :     }
    1674               0 :     pTos->flags = MEM_Real;
    1675               0 :   }else if( pTos->flags & MEM_Int ){
    1676               0 :     Release(pTos);
    1677               0 :     if( pOp->opcode==OP_Negative || pTos->i<0 ){
    1678               0 :       pTos->i = -pTos->i;
    1679                 :     }
    1680               0 :     pTos->flags = MEM_Int;
    1681               0 :   }else if( pTos->flags & MEM_Null ){
    1682                 :     /* Do nothing */
    1683                 :   }else{
    1684               0 :     Realify(pTos);
    1685               0 :     Release(pTos);
    1686               0 :     if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
    1687               0 :       pTos->r = -pTos->r;
    1688                 :     }
    1689               0 :     pTos->flags = MEM_Real;
    1690                 :   }
    1691               0 :   break;
    1692                 : }
    1693                 : 
    1694                 : /* Opcode: Not * * *
    1695                 : **
    1696                 : ** Interpret the top of the stack as a boolean value.  Replace it
    1697                 : ** with its complement.  If the top of the stack is NULL its value
    1698                 : ** is unchanged.
    1699                 : */
    1700                 : case OP_Not: {
    1701                 :   assert( pTos>=p->aStack );
    1702               0 :   if( pTos->flags & MEM_Null ) break;  /* Do nothing to NULLs */
    1703               0 :   Integerify(pTos);
    1704               0 :   Release(pTos);
    1705               0 :   pTos->i = !pTos->i;
    1706               0 :   pTos->flags = MEM_Int;
    1707               0 :   break;
    1708                 : }
    1709                 : 
    1710                 : /* Opcode: BitNot * * *
    1711                 : **
    1712                 : ** Interpret the top of the stack as an value.  Replace it
    1713                 : ** with its ones-complement.  If the top of the stack is NULL its
    1714                 : ** value is unchanged.
    1715                 : */
    1716                 : case OP_BitNot: {
    1717                 :   assert( pTos>=p->aStack );
    1718               0 :   if( pTos->flags & MEM_Null ) break;  /* Do nothing to NULLs */
    1719               0 :   Integerify(pTos);
    1720               0 :   Release(pTos);
    1721               0 :   pTos->i = ~pTos->i;
    1722               0 :   pTos->flags = MEM_Int;
    1723               0 :   break;
    1724                 : }
    1725                 : 
    1726                 : /* Opcode: Noop * * *
    1727                 : **
    1728                 : ** Do nothing.  This instruction is often useful as a jump
    1729                 : ** destination.
    1730                 : */
    1731                 : case OP_Noop: {
    1732               2 :   break;
    1733                 : }
    1734                 : 
    1735                 : /* Opcode: If P1 P2 *
    1736                 : **
    1737                 : ** Pop a single boolean from the stack.  If the boolean popped is
    1738                 : ** true, then jump to p2.  Otherwise continue to the next instruction.
    1739                 : ** An integer is false if zero and true otherwise.  A string is
    1740                 : ** false if it has zero length and true otherwise.
    1741                 : **
    1742                 : ** If the value popped of the stack is NULL, then take the jump if P1
    1743                 : ** is true and fall through if P1 is false.
    1744                 : */
    1745                 : /* Opcode: IfNot P1 P2 *
    1746                 : **
    1747                 : ** Pop a single boolean from the stack.  If the boolean popped is
    1748                 : ** false, then jump to p2.  Otherwise continue to the next instruction.
    1749                 : ** An integer is false if zero and true otherwise.  A string is
    1750                 : ** false if it has zero length and true otherwise.
    1751                 : **
    1752                 : ** If the value popped of the stack is NULL, then take the jump if P1
    1753                 : ** is true and fall through if P1 is false.
    1754                 : */
    1755                 : case OP_If:
    1756                 : case OP_IfNot: {
    1757                 :   int c;
    1758                 :   assert( pTos>=p->aStack );
    1759               4 :   if( pTos->flags & MEM_Null ){
    1760               0 :     c = pOp->p1;
    1761                 :   }else{
    1762               4 :     Integerify(pTos);
    1763               4 :     c = pTos->i;
    1764               4 :     if( pOp->opcode==OP_IfNot ) c = !c;
    1765                 :   }
    1766                 :   assert( (pTos->flags & MEM_Dyn)==0 );
    1767               4 :   pTos--;
    1768               4 :   if( c ) pc = pOp->p2-1;
    1769               4 :   break;
    1770                 : }
    1771                 : 
    1772                 : /* Opcode: IsNull P1 P2 *
    1773                 : **
    1774                 : ** If any of the top abs(P1) values on the stack are NULL, then jump
    1775                 : ** to P2.  Pop the stack P1 times if P1>0.   If P1<0 leave the stack
    1776                 : ** unchanged.
    1777                 : */
    1778                 : case OP_IsNull: {
    1779                 :   int i, cnt;
    1780                 :   Mem *pTerm;
    1781              12 :   cnt = pOp->p1;
    1782              12 :   if( cnt<0 ) cnt = -cnt;
    1783              12 :   pTerm = &pTos[1-cnt];
    1784                 :   assert( pTerm>=p->aStack );
    1785              21 :   for(i=0; i<cnt; i++, pTerm++){
    1786              12 :     if( pTerm->flags & MEM_Null ){
    1787               3 :       pc = pOp->p2-1;
    1788               3 :       break;
    1789                 :     }
    1790                 :   }
    1791              12 :   if( pOp->p1>0 ) popStack(&pTos, cnt);
    1792              12 :   break;
    1793                 : }
    1794                 : 
    1795                 : /* Opcode: NotNull P1 P2 *
    1796                 : **
    1797                 : ** Jump to P2 if the top P1 values on the stack are all not NULL.  Pop the
    1798                 : ** stack if P1 times if P1 is greater than zero.  If P1 is less than
    1799                 : ** zero then leave the stack unchanged.
    1800                 : */
    1801                 : case OP_NotNull: {
    1802                 :   int i, cnt;
    1803             220 :   cnt = pOp->p1;
    1804             220 :   if( cnt<0 ) cnt = -cnt;
    1805                 :   assert( &pTos[1-cnt] >= p->aStack );
    1806             220 :   for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){}
    1807             220 :   if( i>=cnt ) pc = pOp->p2-1;
    1808             220 :   if( pOp->p1>0 ) popStack(&pTos, cnt);
    1809             220 :   break;
    1810                 : }
    1811                 : 
    1812                 : /* Opcode: MakeRecord P1 P2 *
    1813                 : **
    1814                 : ** Convert the top P1 entries of the stack into a single entry
    1815                 : ** suitable for use as a data record in a database table.  The
    1816                 : ** details of the format are irrelavant as long as the OP_Column
    1817                 : ** opcode can decode the record later.  Refer to source code
    1818                 : ** comments for the details of the record format.
    1819                 : **
    1820                 : ** If P2 is true (non-zero) and one or more of the P1 entries
    1821                 : ** that go into building the record is NULL, then add some extra
    1822                 : ** bytes to the record to make it distinct for other entries created
    1823                 : ** during the same run of the VDBE.  The extra bytes added are a
    1824                 : ** counter that is reset with each run of the VDBE, so records
    1825                 : ** created this way will not necessarily be distinct across runs.
    1826                 : ** But they should be distinct for transient tables (created using
    1827                 : ** OP_OpenTemp) which is what they are intended for.
    1828                 : **
    1829                 : ** (Later:) The P2==1 option was intended to make NULLs distinct
    1830                 : ** for the UNION operator.  But I have since discovered that NULLs
    1831                 : ** are indistinct for UNION.  So this option is never used.
    1832                 : */
    1833                 : case OP_MakeRecord: {
    1834                 :   char *zNewRecord;
    1835                 :   int nByte;
    1836                 :   int nField;
    1837                 :   int i, j;
    1838                 :   int idxWidth;
    1839                 :   u32 addr;
    1840                 :   Mem *pRec;
    1841             401 :   int addUnique = 0;   /* True to cause bytes to be added to make the
    1842                 :                        ** generated record distinct */
    1843                 :   char zTemp[NBFS];    /* Temp space for small records */
    1844                 : 
    1845                 :   /* Assuming the record contains N fields, the record format looks
    1846                 :   ** like this:
    1847                 :   **
    1848                 :   **   -------------------------------------------------------------------
    1849                 :   **   | idx0 | idx1 | ... | idx(N-1) | idx(N) | data0 | ... | data(N-1) |
    1850                 :   **   -------------------------------------------------------------------
    1851                 :   **
    1852                 :   ** All data fields are converted to strings before being stored and
    1853                 :   ** are stored with their null terminators.  NULL entries omit the
    1854                 :   ** null terminator.  Thus an empty string uses 1 byte and a NULL uses
    1855                 :   ** zero bytes.  Data(0) is taken from the lowest element of the stack
    1856                 :   ** and data(N-1) is the top of the stack.
    1857                 :   **
    1858                 :   ** Each of the idx() entries is either 1, 2, or 3 bytes depending on
    1859                 :   ** how big the total record is.  Idx(0) contains the offset to the start
    1860                 :   ** of data(0).  Idx(k) contains the offset to the start of data(k).
    1861                 :   ** Idx(N) contains the total number of bytes in the record.
    1862                 :   */
    1863             401 :   nField = pOp->p1;
    1864             401 :   pRec = &pTos[1-nField];
    1865                 :   assert( pRec>=p->aStack );
    1866             401 :   nByte = 0;
    1867            1645 :   for(i=0; i<nField; i++, pRec++){
    1868            1244 :     if( pRec->flags & MEM_Null ){
    1869              77 :       addUnique = pOp->p2;
    1870                 :     }else{
    1871            1167 :       Stringify(pRec);
    1872            1167 :       nByte += pRec->n;
    1873                 :     }
    1874                 :   }
    1875             401 :   if( addUnique ) nByte += sizeof(p->uniqueCnt);
    1876             401 :   if( nByte + nField + 1 < 256 ){
    1877             401 :     idxWidth = 1;
    1878               0 :   }else if( nByte + 2*nField + 2 < 65536 ){
    1879               0 :     idxWidth = 2;
    1880                 :   }else{
    1881               0 :     idxWidth = 3;
    1882                 :   }
    1883             401 :   nByte += idxWidth*(nField + 1);
    1884             401 :   if( nByte>MAX_BYTES_PER_ROW ){
    1885               0 :     rc = SQLITE_TOOBIG;
    1886               0 :     goto abort_due_to_error;
    1887                 :   }
    1888             401 :   if( nByte<=NBFS ){
    1889             259 :     zNewRecord = zTemp;
    1890                 :   }else{
    1891             142 :     zNewRecord = sqliteMallocRaw( nByte );
    1892             142 :     if( zNewRecord==0 ) goto no_mem;
    1893                 :   }
    1894             401 :   j = 0;
    1895             401 :   addr = idxWidth*(nField+1) + addUnique*sizeof(p->uniqueCnt);
    1896            1645 :   for(i=0, pRec=&pTos[1-nField]; i<nField; i++, pRec++){
    1897            1244 :     zNewRecord[j++] = addr & 0xff;
    1898            1244 :     if( idxWidth>1 ){
    1899               0 :       zNewRecord[j++] = (addr>>8)&0xff;
    1900               0 :       if( idxWidth>2 ){
    1901               0 :         zNewRecord[j++] = (addr>>16)&0xff;
    1902                 :       }
    1903                 :     }
    1904            1244 :     if( (pRec->flags & MEM_Null)==0 ){
    1905            1167 :       addr += pRec->n;
    1906                 :     }
    1907                 :   }
    1908             401 :   zNewRecord[j++] = addr & 0xff;
    1909             401 :   if( idxWidth>1 ){
    1910               0 :     zNewRecord[j++] = (addr>>8)&0xff;
    1911               0 :     if( idxWidth>2 ){
    1912               0 :       zNewRecord[j++] = (addr>>16)&0xff;
    1913                 :     }
    1914                 :   }
    1915             401 :   if( addUnique ){
    1916               0 :     memcpy(&zNewRecord[j], &p->uniqueCnt, sizeof(p->uniqueCnt));
    1917               0 :     p->uniqueCnt++;
    1918               0 :     j += sizeof(p->uniqueCnt);
    1919                 :   }
    1920            1645 :   for(i=0, pRec=&pTos[1-nField]; i<nField; i++, pRec++){
    1921            1244 :     if( (pRec->flags & MEM_Null)==0 ){
    1922            1167 :       memcpy(&zNewRecord[j], pRec->z, pRec->n);
    1923            1167 :       j += pRec->n;
    1924                 :     }
    1925                 :   }
    1926             401 :   popStack(&pTos, nField);
    1927             401 :   pTos++;
    1928             401 :   pTos->n = nByte;
    1929             401 :   if( nByte<=NBFS ){
    1930                 :     assert( zNewRecord==zTemp );
    1931             259 :     memcpy(pTos->zShort, zTemp, nByte);
    1932             259 :     pTos->z = pTos->zShort;
    1933             259 :     pTos->flags = MEM_Str | MEM_Short;
    1934                 :   }else{
    1935                 :     assert( zNewRecord!=zTemp );
    1936             142 :     pTos->z = zNewRecord;
    1937             142 :     pTos->flags = MEM_Str | MEM_Dyn;
    1938                 :   }
    1939             401 :   break;
    1940                 : }
    1941                 : 
    1942                 : /* Opcode: MakeKey P1 P2 P3
    1943                 : **
    1944                 : ** Convert the top P1 entries of the stack into a single entry suitable
    1945                 : ** for use as the key in an index.  The top P1 records are
    1946                 : ** converted to strings and merged.  The null-terminators 
    1947                 : ** are retained and used as separators.
    1948                 : ** The lowest entry in the stack is the first field and the top of the
    1949                 : ** stack becomes the last.
    1950                 : **
    1951                 : ** If P2 is not zero, then the original entries remain on the stack
    1952                 : ** and the new key is pushed on top.  If P2 is zero, the original
    1953                 : ** data is popped off the stack first then the new key is pushed
    1954                 : ** back in its place.
    1955                 : **
    1956                 : ** P3 is a string that is P1 characters long.  Each character is either
    1957                 : ** an 'n' or a 't' to indicates if the argument should be intepreted as
    1958                 : ** numeric or text type.  The first character of P3 corresponds to the
    1959                 : ** lowest element on the stack.  If P3 is NULL then all arguments are
    1960                 : ** assumed to be of the numeric type.
    1961                 : **
    1962                 : ** The type makes a difference in that text-type fields may not be 
    1963                 : ** introduced by 'b' (as described in the next paragraph).  The
    1964                 : ** first character of a text-type field must be either 'a' (if it is NULL)
    1965                 : ** or 'c'.  Numeric fields will be introduced by 'b' if their content
    1966                 : ** looks like a well-formed number.  Otherwise the 'a' or 'c' will be
    1967                 : ** used.
    1968                 : **
    1969                 : ** The key is a concatenation of fields.  Each field is terminated by
    1970                 : ** a single 0x00 character.  A NULL field is introduced by an 'a' and
    1971                 : ** is followed immediately by its 0x00 terminator.  A numeric field is
    1972                 : ** introduced by a single character 'b' and is followed by a sequence
    1973                 : ** of characters that represent the number such that a comparison of
    1974                 : ** the character string using memcpy() sorts the numbers in numerical
    1975                 : ** order.  The character strings for numbers are generated using the
    1976                 : ** sqliteRealToSortable() function.  A text field is introduced by a
    1977                 : ** 'c' character and is followed by the exact text of the field.  The
    1978                 : ** use of an 'a', 'b', or 'c' character at the beginning of each field
    1979                 : ** guarantees that NULLs sort before numbers and that numbers sort
    1980                 : ** before text.  0x00 characters do not occur except as separators
    1981                 : ** between fields.
    1982                 : **
    1983                 : ** See also: MakeIdxKey, SortMakeKey
    1984                 : */
    1985                 : /* Opcode: MakeIdxKey P1 P2 P3
    1986                 : **
    1987                 : ** Convert the top P1 entries of the stack into a single entry suitable
    1988                 : ** for use as the key in an index.  In addition, take one additional integer
    1989                 : ** off of the stack, treat that integer as a four-byte record number, and
    1990                 : ** append the four bytes to the key.  Thus a total of P1+1 entries are
    1991                 : ** popped from the stack for this instruction and a single entry is pushed
    1992                 : ** back.  The first P1 entries that are popped are strings and the last
    1993                 : ** entry (the lowest on the stack) is an integer record number.
    1994                 : **
    1995                 : ** The converstion of the first P1 string entries occurs just like in
    1996                 : ** MakeKey.  Each entry is separated from the others by a null.
    1997                 : ** The entire concatenation is null-terminated.  The lowest entry
    1998                 : ** in the stack is the first field and the top of the stack becomes the
    1999                 : ** last.
    2000                 : **
    2001                 : ** If P2 is not zero and one or more of the P1 entries that go into the
    2002                 : ** generated key is NULL, then jump to P2 after the new key has been
    2003                 : ** pushed on the stack.  In other words, jump to P2 if the key is
    2004                 : ** guaranteed to be unique.  This jump can be used to skip a subsequent
    2005                 : ** uniqueness test.
    2006                 : **
    2007                 : ** P3 is a string that is P1 characters long.  Each character is either
    2008                 : ** an 'n' or a 't' to indicates if the argument should be numeric or
    2009                 : ** text.  The first character corresponds to the lowest element on the
    2010                 : ** stack.  If P3 is null then all arguments are assumed to be numeric.
    2011                 : **
    2012                 : ** See also:  MakeKey, SortMakeKey
    2013                 : */
    2014                 : case OP_MakeIdxKey:
    2015                 : case OP_MakeKey: {
    2016                 :   char *zNewKey;
    2017                 :   int nByte;
    2018                 :   int nField;
    2019                 :   int addRowid;
    2020                 :   int i, j;
    2021             202 :   int containsNull = 0;
    2022                 :   Mem *pRec;
    2023                 :   char zTemp[NBFS];
    2024                 : 
    2025             202 :   addRowid = pOp->opcode==OP_MakeIdxKey;
    2026             202 :   nField = pOp->p1;
    2027             202 :   pRec = &pTos[1-nField];
    2028                 :   assert( pRec>=p->aStack );
    2029             202 :   nByte = 0;
    2030             404 :   for(j=0, i=0; i<nField; i++, j++, pRec++){
    2031             202 :     int flags = pRec->flags;
    2032                 :     int len;
    2033                 :     char *z;
    2034             202 :     if( flags & MEM_Null ){
    2035               0 :       nByte += 2;
    2036               0 :       containsNull = 1;
    2037             221 :     }else if( pOp->p3 && pOp->p3[j]=='t' ){
    2038              19 :       Stringify(pRec);
    2039              19 :       pRec->flags &= ~(MEM_Int|MEM_Real);
    2040              19 :       nByte += pRec->n+1;
    2041             366 :     }else if( (flags & (MEM_Real|MEM_Int))!=0 || sqliteIsNumber(pRec->z) ){
    2042             183 :       if( (flags & (MEM_Real|MEM_Int))==MEM_Int ){
    2043             126 :         pRec->r = pRec->i;
    2044              57 :       }else if( (flags & (MEM_Real|MEM_Int))==0 ){
    2045              57 :         pRec->r = sqliteAtoF(pRec->z, 0);
    2046                 :       }
    2047             183 :       Release(pRec);
    2048             183 :       z = pRec->zShort;
    2049             183 :       sqliteRealToSortable(pRec->r, z);
    2050             183 :       len = strlen(z);
    2051             183 :       pRec->z = 0;
    2052             183 :       pRec->flags = MEM_Real;
    2053             183 :       pRec->n = len+1;
    2054             183 :       nByte += pRec->n+1;
    2055                 :     }else{
    2056               0 :       nByte += pRec->n+1;
    2057                 :     }
    2058                 :   }
    2059             202 :   if( nByte+sizeof(u32)>MAX_BYTES_PER_ROW ){
    2060               0 :     rc = SQLITE_TOOBIG;
    2061               0 :     goto abort_due_to_error;
    2062                 :   }
    2063             202 :   if( addRowid ) nByte += sizeof(u32);
    2064             202 :   if( nByte<=NBFS ){
    2065             202 :     zNewKey = zTemp;
    2066                 :   }else{
    2067               0 :     zNewKey = sqliteMallocRaw( nByte );
    2068               0 :     if( zNewKey==0 ) goto no_mem;
    2069                 :   }
    2070             202 :   j = 0;
    2071             202 :   pRec = &pTos[1-nField];
    2072             404 :   for(i=0; i<nField; i++, pRec++){
    2073             202 :     if( pRec->flags & MEM_Null ){
    2074               0 :       zNewKey[j++] = 'a';
    2075               0 :       zNewKey[j++] = 0;
    2076             202 :     }else if( pRec->flags==MEM_Real ){
    2077             183 :       zNewKey[j++] = 'b';
    2078             183 :       memcpy(&zNewKey[j], pRec->zShort, pRec->n);
    2079             183 :       j += pRec->n;
    2080                 :     }else{
    2081                 :       assert( pRec->flags & MEM_Str );
    2082              19 :       zNewKey[j++] = 'c';
    2083              19 :       memcpy(&zNewKey[j], pRec->z, pRec->n);
    2084              19 :       j += pRec->n;
    2085                 :     }
    2086                 :   }
    2087             202 :   if( addRowid ){
    2088                 :     u32 iKey;
    2089             145 :     pRec = &pTos[-nField];
    2090                 :     assert( pRec>=p->aStack );
    2091             145 :     Integerify(pRec);
    2092             145 :     iKey = intToKey(pRec->i);
    2093             145 :     memcpy(&zNewKey[j], &iKey, sizeof(u32));
    2094             145 :     popStack(&pTos, nField+1);
    2095             145 :     if( pOp->p2 && containsNull ) pc = pOp->p2 - 1;
    2096                 :   }else{
    2097              57 :     if( pOp->p2==0 ) popStack(&pTos, nField);
    2098                 :   }
    2099             202 :   pTos++;
    2100             202 :   pTos->n = nByte;
    2101             202 :   if( nByte<=NBFS ){
    2102                 :     assert( zNewKey==zTemp );
    2103             202 :     pTos->z = pTos->zShort;
    2104             202 :     memcpy(pTos->zShort, zTemp, nByte);
    2105             202 :     pTos->flags = MEM_Str | MEM_Short;
    2106                 :   }else{
    2107               0 :     pTos->z = zNewKey;
    2108               0 :     pTos->flags = MEM_Str | MEM_Dyn;
    2109                 :   }
    2110             202 :   break;
    2111                 : }
    2112                 : 
    2113                 : /* Opcode: IncrKey * * *
    2114                 : **
    2115                 : ** The top of the stack should contain an index key generated by
    2116                 : ** The MakeKey opcode.  This routine increases the least significant
    2117                 : ** byte of that key by one.  This is used so that the MoveTo opcode
    2118                 : ** will move to the first entry greater than the key rather than to
    2119                 : ** the key itself.
    2120                 : */
    2121                 : case OP_IncrKey: {
    2122                 :   assert( pTos>=p->aStack );
    2123                 :   /* The IncrKey opcode is only applied to keys generated by
    2124                 :   ** MakeKey or MakeIdxKey and the results of those operands
    2125                 :   ** are always dynamic strings or zShort[] strings.  So we
    2126                 :   ** are always free to modify the string in place.
    2127                 :   */
    2128                 :   assert( pTos->flags & (MEM_Dyn|MEM_Short) );
    2129               0 :   pTos->z[pTos->n-1]++;
    2130               0 :   break;
    2131                 : }
    2132                 : 
    2133                 : /* Opcode: Checkpoint P1 * *
    2134                 : **
    2135                 : ** Begin a checkpoint.  A checkpoint is the beginning of a operation that
    2136                 : ** is part of a larger transaction but which might need to be rolled back
    2137                 : ** itself without effecting the containing transaction.  A checkpoint will
    2138                 : ** be automatically committed or rollback when the VDBE halts.
    2139                 : **
    2140                 : ** The checkpoint is begun on the database file with index P1.  The main
    2141                 : ** database file has an index of 0 and the file used for temporary tables
    2142                 : ** has an index of 1.
    2143                 : */
    2144                 : case OP_Checkpoint: {
    2145               0 :   int i = pOp->p1;
    2146               0 :   if( i>=0 && i<db->nDb && db->aDb[i].pBt && db->aDb[i].inTrans==1 ){
    2147               0 :     rc = sqliteBtreeBeginCkpt(db->aDb[i].pBt);
    2148               0 :     if( rc==SQLITE_OK ) db->aDb[i].inTrans = 2;
    2149                 :   }
    2150               0 :   break;
    2151                 : }
    2152                 : 
    2153                 : /* Opcode: Transaction P1 * *
    2154                 : **
    2155                 : ** Begin a transaction.  The transaction ends when a Commit or Rollback
    2156                 : ** opcode is encountered.  Depending on the ON CONFLICT setting, the
    2157                 : ** transaction might also be rolled back if an error is encountered.
    2158                 : **
    2159                 : ** P1 is the index of the database file on which the transaction is
    2160                 : ** started.  Index 0 is the main database file and index 1 is the
    2161                 : ** file used for temporary tables.
    2162                 : **
    2163                 : ** A write lock is obtained on the database file when a transaction is
    2164                 : ** started.  No other process can read or write the file while the
    2165                 : ** transaction is underway.  Starting a transaction also creates a
    2166                 : ** rollback journal.  A transaction must be started before any changes
    2167                 : ** can be made to the database.
    2168                 : */
    2169                 : case OP_Transaction: {
    2170             724 :   int busy = 1;
    2171             724 :   int i = pOp->p1;
    2172                 :   assert( i>=0 && i<db->nDb );
    2173             724 :   if( db->aDb[i].inTrans ) break;
    2174            2172 :   while( db->aDb[i].pBt!=0 && busy ){
    2175             724 :     rc = sqliteBtreeBeginTrans(db->aDb[i].pBt);
    2176             724 :     switch( rc ){
    2177                 :       case SQLITE_BUSY: {
    2178               0 :         if( db->xBusyCallback==0 ){
    2179               0 :           p->pc = pc;
    2180               0 :           p->undoTransOnError = 1;
    2181               0 :           p->rc = SQLITE_BUSY;
    2182               0 :           p->pTos = pTos;
    2183               0 :           return SQLITE_BUSY;
    2184               0 :         }else if( (*db->xBusyCallback)(db->pBusyArg, "", busy++)==0 ){
    2185               0 :           sqliteSetString(&p->zErrMsg, sqlite_error_string(rc), (char*)0);
    2186               0 :           busy = 0;
    2187                 :         }
    2188               0 :         break;
    2189                 :       }
    2190                 :       case SQLITE_READONLY: {
    2191               0 :         rc = SQLITE_OK;
    2192                 :         /* Fall thru into the next case */
    2193                 :       }
    2194                 :       case SQLITE_OK: {
    2195             724 :         p->inTempTrans = 0;
    2196             724 :         busy = 0;
    2197             724 :         break;
    2198                 :       }
    2199                 :       default: {
    2200               0 :         goto abort_due_to_error;
    2201                 :       }
    2202                 :     }
    2203                 :   }
    2204             724 :   db->aDb[i].inTrans = 1;
    2205             724 :   p->undoTransOnError = 1;
    2206             724 :   break;
    2207                 : }
    2208                 : 
    2209                 : /* Opcode: Commit * * *
    2210                 : **
    2211                 : ** Cause all modifications to the database that have been made since the
    2212                 : ** last Transaction to actually take effect.  No additional modifications
    2213                 : ** are allowed until another transaction is started.  The Commit instruction
    2214                 : ** deletes the journal file and releases the write lock on the database.
    2215                 : ** A read lock continues to be held if there are still cursors open.
    2216                 : */
    2217                 : case OP_Commit: {
    2218                 :   int i;
    2219             360 :   if( db->xCommitCallback!=0 ){
    2220               0 :     if( sqliteSafetyOff(db) ) goto abort_due_to_misuse; 
    2221               0 :     if( db->xCommitCallback(db->pCommitArg)!=0 ){
    2222               0 :       rc = SQLITE_CONSTRAINT;
    2223                 :     }
    2224               0 :     if( sqliteSafetyOn(db) ) goto abort_due_to_misuse;
    2225                 :   }
    2226            1080 :   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
    2227             720 :     if( db->aDb[i].inTrans ){
    2228             720 :       rc = sqliteBtreeCommit(db->aDb[i].pBt);
    2229             720 :       db->aDb[i].inTrans = 0;
    2230                 :     }
    2231                 :   }
    2232             360 :   if( rc==SQLITE_OK ){
    2233             360 :     sqliteCommitInternalChanges(db);
    2234                 :   }else{
    2235               0 :     sqliteRollbackAll(db);
    2236                 :   }
    2237             360 :   break;
    2238                 : }
    2239                 : 
    2240                 : /* Opcode: Rollback P1 * *
    2241                 : **
    2242                 : ** Cause all modifications to the database that have been made since the
    2243                 : ** last Transaction to be undone. The database is restored to its state
    2244                 : ** before the Transaction opcode was executed.  No additional modifications
    2245                 : ** are allowed until another transaction is started.
    2246                 : **
    2247                 : ** P1 is the index of the database file that is committed.  An index of 0
    2248                 : ** is used for the main database and an index of 1 is used for the file used
    2249                 : ** to hold temporary tables.
    2250                 : **
    2251                 : ** This instruction automatically closes all cursors and releases both
    2252                 : ** the read and write locks on the indicated database.
    2253                 : */
    2254                 : case OP_Rollback: {
    2255               2 :   sqliteRollbackAll(db);
    2256               2 :   break;
    2257                 : }
    2258                 : 
    2259                 : /* Opcode: ReadCookie P1 P2 *
    2260                 : **
    2261                 : ** Read cookie number P2 from database P1 and push it onto the stack.
    2262                 : ** P2==0 is the schema version.  P2==1 is the database format.
    2263                 : ** P2==2 is the recommended pager cache size, and so forth.  P1==0 is
    2264                 : ** the main database file and P1==1 is the database file used to store
    2265                 : ** temporary tables.
    2266                 : **
    2267                 : ** There must be a read-lock on the database (either a transaction
    2268                 : ** must be started or there must be an open cursor) before
    2269                 : ** executing this instruction.
    2270                 : */
    2271                 : case OP_ReadCookie: {
    2272                 :   int aMeta[SQLITE_N_BTREE_META];
    2273                 :   assert( pOp->p2<SQLITE_N_BTREE_META );
    2274                 :   assert( pOp->p1>=0 && pOp->p1<db->nDb );
    2275                 :   assert( db->aDb[pOp->p1].pBt!=0 );
    2276               0 :   rc = sqliteBtreeGetMeta(db->aDb[pOp->p1].pBt, aMeta);
    2277               0 :   pTos++;
    2278               0 :   pTos->i = aMeta[1+pOp->p2];
    2279               0 :   pTos->flags = MEM_Int;
    2280               0 :   break;
    2281                 : }
    2282                 : 
    2283                 : /* Opcode: SetCookie P1 P2 *
    2284                 : **
    2285                 : ** Write the top of the stack into cookie number P2 of database P1.
    2286                 : ** P2==0 is the schema version.  P2==1 is the database format.
    2287                 : ** P2==2 is the recommended pager cache size, and so forth.  P1==0 is
    2288                 : ** the main database file and P1==1 is the database file used to store
    2289                 : ** temporary tables.
    2290                 : **
    2291                 : ** A transaction must be started before executing this opcode.
    2292                 : */
    2293                 : case OP_SetCookie: {
    2294                 :   int aMeta[SQLITE_N_BTREE_META];
    2295                 :   assert( pOp->p2<SQLITE_N_BTREE_META );
    2296                 :   assert( pOp->p1>=0 && pOp->p1<db->nDb );
    2297                 :   assert( db->aDb[pOp->p1].pBt!=0 );
    2298                 :   assert( pTos>=p->aStack );
    2299             191 :   Integerify(pTos)
    2300             191 :   rc = sqliteBtreeGetMeta(db->aDb[pOp->p1].pBt, aMeta);
    2301             191 :   if( rc==SQLITE_OK ){
    2302             191 :     aMeta[1+pOp->p2] = pTos->i;
    2303             191 :     rc = sqliteBtreeUpdateMeta(db->aDb[pOp->p1].pBt, aMeta);
    2304                 :   }
    2305             191 :   Release(pTos);
    2306             191 :   pTos--;
    2307             191 :   break;
    2308                 : }
    2309                 : 
    2310                 : /* Opcode: VerifyCookie P1 P2 *
    2311                 : **
    2312                 : ** Check the value of global database parameter number 0 (the
    2313                 : ** schema version) and make sure it is equal to P2.  
    2314                 : ** P1 is the database number which is 0 for the main database file
    2315                 : ** and 1 for the file holding temporary tables and some higher number
    2316                 : ** for auxiliary databases.
    2317                 : **
    2318                 : ** The cookie changes its value whenever the database schema changes.
    2319                 : ** This operation is used to detect when that the cookie has changed
    2320                 : ** and that the current process needs to reread the schema.
    2321                 : **
    2322                 : ** Either a transaction needs to have been started or an OP_Open needs
    2323                 : ** to be executed (to establish a read lock) before this opcode is
    2324                 : ** invoked.
    2325                 : */
    2326                 : case OP_VerifyCookie: {
    2327                 :   int aMeta[SQLITE_N_BTREE_META];
    2328                 :   assert( pOp->p1>=0 && pOp->p1<db->nDb );
    2329             723 :   rc = sqliteBtreeGetMeta(db->aDb[pOp->p1].pBt, aMeta);
    2330             723 :   if( rc==SQLITE_OK && aMeta[1]!=pOp->p2 ){
    2331               0 :     sqliteSetString(&p->zErrMsg, "database schema has changed", (char*)0);
    2332               0 :     rc = SQLITE_SCHEMA;
    2333                 :   }
    2334             723 :   break;
    2335                 : }
    2336                 : 
    2337                 : /* Opcode: OpenRead P1 P2 P3
    2338                 : **
    2339                 : ** Open a read-only cursor for the database table whose root page is
    2340                 : ** P2 in a database file.  The database file is determined by an 
    2341                 : ** integer from the top of the stack.  0 means the main database and
    2342                 : ** 1 means the database used for temporary tables.  Give the new 
    2343                 : ** cursor an identifier of P1.  The P1 values need not be contiguous
    2344                 : ** but all P1 values should be small integers.  It is an error for
    2345                 : ** P1 to be negative.
    2346                 : **
    2347                 : ** If P2==0 then take the root page number from the next of the stack.
    2348                 : **
    2349                 : ** There will be a read lock on the database whenever there is an
    2350                 : ** open cursor.  If the database was unlocked prior to this instruction
    2351                 : ** then a read lock is acquired as part of this instruction.  A read
    2352                 : ** lock allows other processes to read the database but prohibits
    2353                 : ** any other process from modifying the database.  The read lock is
    2354                 : ** released when all cursors are closed.  If this instruction attempts
    2355                 : ** to get a read lock but fails, the script terminates with an
    2356                 : ** SQLITE_BUSY error code.
    2357                 : **
    2358                 : ** The P3 value is the name of the table or index being opened.
    2359                 : ** The P3 value is not actually used by this opcode and may be
    2360                 : ** omitted.  But the code generator usually inserts the index or
    2361                 : ** table name into P3 to make the code easier to read.
    2362                 : **
    2363                 : ** See also OpenWrite.
    2364                 : */
    2365                 : /* Opcode: OpenWrite P1 P2 P3
    2366                 : **
    2367                 : ** Open a read/write cursor named P1 on the table or index whose root
    2368                 : ** page is P2.  If P2==0 then take the root page number from the stack.
    2369                 : **
    2370                 : ** The P3 value is the name of the table or index being opened.
    2371                 : ** The P3 value is not actually used by this opcode and may be
    2372                 : ** omitted.  But the code generator usually inserts the index or
    2373                 : ** table name into P3 to make the code easier to read.
    2374                 : **
    2375                 : ** This instruction works just like OpenRead except that it opens the cursor
    2376                 : ** in read/write mode.  For a given table, there can be one or more read-only
    2377                 : ** cursors or a single read/write cursor but not both.
    2378                 : **
    2379                 : ** See also OpenRead.
    2380                 : */
    2381                 : case OP_OpenRead:
    2382                 : case OP_OpenWrite: {
    2383            1079 :   int busy = 0;
    2384            1079 :   int i = pOp->p1;
    2385            1079 :   int p2 = pOp->p2;
    2386                 :   int wrFlag;
    2387                 :   Btree *pX;
    2388                 :   int iDb;
    2389                 :   
    2390                 :   assert( pTos>=p->aStack );
    2391            1079 :   Integerify(pTos);
    2392            1079 :   iDb = pTos->i;
    2393            1079 :   pTos--;
    2394                 :   assert( iDb>=0 && iDb<db->nDb );
    2395            1079 :   pX = db->aDb[iDb].pBt;
    2396                 :   assert( pX!=0 );
    2397            1079 :   wrFlag = pOp->opcode==OP_OpenWrite;
    2398            1079 :   if( p2<=0 ){
    2399                 :     assert( pTos>=p->aStack );
    2400               0 :     Integerify(pTos);
    2401               0 :     p2 = pTos->i;
    2402               0 :     pTos--;
    2403               0 :     if( p2<2 ){
    2404               0 :       sqliteSetString(&p->zErrMsg, "root page number less than 2", (char*)0);
    2405               0 :       rc = SQLITE_INTERNAL;
    2406               0 :       break;
    2407                 :     }
    2408                 :   }
    2409                 :   assert( i>=0 );
    2410            1079 :   if( expandCursorArraySize(p, i) ) goto no_mem;
    2411            1079 :   sqliteVdbeCleanupCursor(&p->aCsr[i]);
    2412            1079 :   memset(&p->aCsr[i], 0, sizeof(Cursor));
    2413            1079 :   p->aCsr[i].nullRow = 1;
    2414            1079 :   if( pX==0 ) break;
    2415                 :   do{
    2416            1079 :     rc = sqliteBtreeCursor(pX, p2, wrFlag, &p->aCsr[i].pCursor);
    2417            1079 :     switch( rc ){
    2418                 :       case SQLITE_BUSY: {
    2419               0 :         if( db->xBusyCallback==0 ){
    2420               0 :           p->pc = pc;
    2421               0 :           p->rc = SQLITE_BUSY;
    2422               0 :           p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */
    2423               0 :           return SQLITE_BUSY;
    2424               0 :         }else if( (*db->xBusyCallback)(db->pBusyArg, pOp->p3, ++busy)==0 ){
    2425               0 :           sqliteSetString(&p->zErrMsg, sqlite_error_string(rc), (char*)0);
    2426               0 :           busy = 0;
    2427                 :         }
    2428               0 :         break;
    2429                 :       }
    2430                 :       case SQLITE_OK: {
    2431            1079 :         busy = 0;
    2432            1079 :         break;
    2433                 :       }
    2434                 :       default: {
    2435               0 :         goto abort_due_to_error;
    2436                 :       }
    2437                 :     }
    2438            1079 :   }while( busy );
    2439            1079 :   break;
    2440                 : }
    2441                 : 
    2442                 : /* Opcode: OpenTemp P1 P2 *
    2443                 : **
    2444                 : ** Open a new cursor to a transient table.
    2445                 : ** The transient cursor is always opened read/write even if 
    2446                 : ** the main database is read-only.  The transient table is deleted
    2447                 : ** automatically when the cursor is closed.
    2448                 : **
    2449                 : ** The cursor points to a BTree table if P2==0 and to a BTree index
    2450                 : ** if P2==1.  A BTree table must have an integer key and can have arbitrary
    2451                 : ** data.  A BTree index has no data but can have an arbitrary key.
    2452                 : **
    2453                 : ** This opcode is used for tables that exist for the duration of a single
    2454                 : ** SQL statement only.  Tables created using CREATE TEMPORARY TABLE
    2455                 : ** are opened using OP_OpenRead or OP_OpenWrite.  "Temporary" in the
    2456                 : ** context of this opcode means for the duration of a single SQL statement
    2457                 : ** whereas "Temporary" in the context of CREATE TABLE means for the duration
    2458                 : ** of the connection to the database.  Same word; different meanings.
    2459                 : */
    2460                 : case OP_OpenTemp: {
    2461               0 :   int i = pOp->p1;
    2462                 :   Cursor *pCx;
    2463                 :   assert( i>=0 );
    2464               0 :   if( expandCursorArraySize(p, i) ) goto no_mem;
    2465               0 :   pCx = &p->aCsr[i];
    2466               0 :   sqliteVdbeCleanupCursor(pCx);
    2467               0 :   memset(pCx, 0, sizeof(*pCx));
    2468               0 :   pCx->nullRow = 1;
    2469               0 :   rc = sqliteBtreeFactory(db, 0, 1, TEMP_PAGES, &pCx->pBt);
    2470                 : 
    2471               0 :   if( rc==SQLITE_OK ){
    2472               0 :     rc = sqliteBtreeBeginTrans(pCx->pBt);
    2473                 :   }
    2474               0 :   if( rc==SQLITE_OK ){
    2475               0 :     if( pOp->p2 ){
    2476                 :       int pgno;
    2477               0 :       rc = sqliteBtreeCreateIndex(pCx->pBt, &pgno);
    2478               0 :       if( rc==SQLITE_OK ){
    2479               0 :         rc = sqliteBtreeCursor(pCx->pBt, pgno, 1, &pCx->pCursor);
    2480                 :       }
    2481                 :     }else{
    2482               0 :       rc = sqliteBtreeCursor(pCx->pBt, 2, 1, &pCx->pCursor);
    2483                 :     }
    2484                 :   }
    2485               0 :   break;
    2486                 : }
    2487                 : 
    2488                 : /* Opcode: OpenPseudo P1 * *
    2489                 : **
    2490                 : ** Open a new cursor that points to a fake table that contains a single
    2491                 : ** row of data.  Any attempt to write a second row of data causes the
    2492                 : ** first row to be deleted.  All data is deleted when the cursor is
    2493                 : ** closed.
    2494                 : **
    2495                 : ** A pseudo-table created by this opcode is useful for holding the
    2496                 : ** NEW or OLD tables in a trigger.
    2497                 : */
    2498                 : case OP_OpenPseudo: {
    2499               0 :   int i = pOp->p1;
    2500                 :   Cursor *pCx;
    2501                 :   assert( i>=0 );
    2502               0 :   if( expandCursorArraySize(p, i) ) goto no_mem;
    2503               0 :   pCx = &p->aCsr[i];
    2504               0 :   sqliteVdbeCleanupCursor(pCx);
    2505               0 :   memset(pCx, 0, sizeof(*pCx));
    2506               0 :   pCx->nullRow = 1;
    2507               0 :   pCx->pseudoTable = 1;
    2508               0 :   break;
    2509                 : }
    2510                 : 
    2511                 : /* Opcode: Close P1 * *
    2512                 : **
    2513                 : ** Close a cursor previously opened as P1.  If P1 is not
    2514                 : ** currently open, this instruction is a no-op.
    2515                 : */
    2516                 : case OP_Close: {
    2517            1055 :   int i = pOp->p1;
    2518            1055 :   if( i>=0 && i<p->nCursor ){
    2519            1055 :     sqliteVdbeCleanupCursor(&p->aCsr[i]);
    2520                 :   }
    2521            1055 :   break;
    2522                 : }
    2523                 : 
    2524                 : /* Opcode: MoveTo P1 P2 *
    2525                 : **
    2526                 : ** Pop the top of the stack and use its value as a key.  Reposition
    2527                 : ** cursor P1 so that it points to an entry with a matching key.  If
    2528                 : ** the table contains no record with a matching key, then the cursor
    2529                 : ** is left pointing at the first record that is greater than the key.
    2530                 : ** If there are no records greater than the key and P2 is not zero,
    2531                 : ** then an immediate jump to P2 is made.
    2532                 : **
    2533                 : ** See also: Found, NotFound, Distinct, MoveLt
    2534                 : */
    2535                 : /* Opcode: MoveLt P1 P2 *
    2536                 : **
    2537                 : ** Pop the top of the stack and use its value as a key.  Reposition
    2538                 : ** cursor P1 so that it points to the entry with the largest key that is
    2539                 : ** less than the key popped from the stack.
    2540                 : ** If there are no records less than than the key and P2
    2541                 : ** is not zero then an immediate jump to P2 is made.
    2542                 : **
    2543                 : ** See also: MoveTo
    2544                 : */
    2545                 : case OP_MoveLt:
    2546                 : case OP_MoveTo: {
    2547             134 :   int i = pOp->p1;
    2548                 :   Cursor *pC;
    2549                 : 
    2550                 :   assert( pTos>=p->aStack );
    2551                 :   assert( i>=0 && i<p->nCursor );
    2552             134 :   pC = &p->aCsr[i];
    2553             134 :   if( pC->pCursor!=0 ){
    2554                 :     int res, oc;
    2555             134 :     pC->nullRow = 0;
    2556             134 :     if( pTos->flags & MEM_Int ){
    2557              77 :       int iKey = intToKey(pTos->i);
    2558              77 :       if( pOp->p2==0 && pOp->opcode==OP_MoveTo ){
    2559              77 :         pC->movetoTarget = iKey;
    2560              77 :         pC->deferredMoveto = 1;
    2561              77 :         Release(pTos);
    2562              77 :         pTos--;
    2563              77 :         break;
    2564                 :       }
    2565               0 :       sqliteBtreeMoveto(pC->pCursor, (char*)&iKey, sizeof(int), &res);
    2566               0 :       pC->lastRecno = pTos->i;
    2567               0 :       pC->recnoIsValid = res==0;
    2568                 :     }else{
    2569              57 :       Stringify(pTos);
    2570              57 :       sqliteBtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
    2571              57 :       pC->recnoIsValid = 0;
    2572                 :     }
    2573              57 :     pC->deferredMoveto = 0;
    2574              57 :     sqlite_search_count++;
    2575              57 :     oc = pOp->opcode;
    2576              79 :     if( oc==OP_MoveTo && res<0 ){
    2577              22 :       sqliteBtreeNext(pC->pCursor, &res);
    2578              22 :       pC->recnoIsValid = 0;
    2579              22 :       if( res && pOp->p2>0 ){
    2580               6 :         pc = pOp->p2 - 1;
    2581                 :       }
    2582              35 :     }else if( oc==OP_MoveLt ){
    2583               0 :       if( res>=0 ){
    2584               0 :         sqliteBtreePrevious(pC->pCursor, &res);
    2585               0 :         pC->recnoIsValid = 0;
    2586                 :       }else{
    2587                 :         /* res might be negative because the table is empty.  Check to
    2588                 :         ** see if this is the case.
    2589                 :         */
    2590                 :         int keysize;
    2591               0 :         res = sqliteBtreeKeySize(pC->pCursor,&keysize)!=0 || keysize==0;
    2592                 :       }
    2593               0 :       if( res && pOp->p2>0 ){
    2594               0 :         pc = pOp->p2 - 1;
    2595                 :       }
    2596                 :     }
    2597                 :   }
    2598              57 :   Release(pTos);
    2599              57 :   pTos--;
    2600              57 :   break;
    2601                 : }
    2602                 : 
    2603                 : /* Opcode: Distinct P1 P2 *
    2604                 : **
    2605                 : ** Use the top of the stack as a string key.  If a record with that key does
    2606                 : ** not exist in the table of cursor P1, then jump to P2.  If the record
    2607                 : ** does already exist, then fall thru.  The cursor is left pointing
    2608                 : ** at the record if it exists. The key is not popped from the stack.
    2609                 : **
    2610                 : ** This operation is similar to NotFound except that this operation
    2611                 : ** does not pop the key from the stack.
    2612                 : **
    2613                 : ** See also: Found, NotFound, MoveTo, IsUnique, NotExists
    2614                 : */
    2615                 : /* Opcode: Found P1 P2 *
    2616                 : **
    2617                 : ** Use the top of the stack as a string key.  If a record with that key
    2618                 : ** does exist in table of P1, then jump to P2.  If the record
    2619                 : ** does not exist, then fall thru.  The cursor is left pointing
    2620                 : ** to the record if it exists.  The key is popped from the stack.
    2621                 : **
    2622                 : ** See also: Distinct, NotFound, MoveTo, IsUnique, NotExists
    2623                 : */
    2624                 : /* Opcode: NotFound P1 P2 *
    2625                 : **
    2626                 : ** Use the top of the stack as a string key.  If a record with that key
    2627                 : ** does not exist in table of P1, then jump to P2.  If the record
    2628                 : ** does exist, then fall thru.  The cursor is left pointing to the
    2629                 : ** record if it exists.  The key is popped from the stack.
    2630                 : **
    2631                 : ** The difference between this operation and Distinct is that
    2632                 : ** Distinct does not pop the key from the stack.
    2633                 : **
    2634                 : ** See also: Distinct, Found, MoveTo, NotExists, IsUnique
    2635                 : */
    2636                 : case OP_Distinct:
    2637                 : case OP_NotFound:
    2638                 : case OP_Found: {
    2639               0 :   int i = pOp->p1;
    2640               0 :   int alreadyExists = 0;
    2641                 :   Cursor *pC;
    2642                 :   assert( pTos>=p->aStack );
    2643                 :   assert( i>=0 && i<p->nCursor );
    2644               0 :   if( (pC = &p->aCsr[i])->pCursor!=0 ){
    2645                 :     int res, rx;
    2646               0 :     Stringify(pTos);
    2647               0 :     rx = sqliteBtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
    2648               0 :     alreadyExists = rx==SQLITE_OK && res==0;
    2649               0 :     pC->deferredMoveto = 0;
    2650                 :   }
    2651               0 :   if( pOp->opcode==OP_Found ){
    2652               0 :     if( alreadyExists ) pc = pOp->p2 - 1;
    2653                 :   }else{
    2654               0 :     if( !alreadyExists ) pc = pOp->p2 - 1;
    2655                 :   }
    2656               0 :   if( pOp->opcode!=OP_Distinct ){
    2657               0 :     Release(pTos);
    2658               0 :     pTos--;
    2659                 :   }
    2660               0 :   break;
    2661                 : }
    2662                 : 
    2663                 : /* Opcode: IsUnique P1 P2 *
    2664                 : **
    2665                 : ** The top of the stack is an integer record number.  Call this
    2666                 : ** record number R.  The next on the stack is an index key created
    2667                 : ** using MakeIdxKey.  Call it K.  This instruction pops R from the
    2668                 : ** stack but it leaves K unchanged.
    2669                 : **
    2670                 : ** P1 is an index.  So all but the last four bytes of K are an
    2671                 : ** index string.  The last four bytes of K are a record number.
    2672                 : **
    2673                 : ** This instruction asks if there is an entry in P1 where the
    2674                 : ** index string matches K but the record number is different
    2675                 : ** from R.  If there is no such entry, then there is an immediate
    2676                 : ** jump to P2.  If any entry does exist where the index string
    2677                 : ** matches K but the record number is not R, then the record
    2678                 : ** number for that entry is pushed onto the stack and control
    2679                 : ** falls through to the next instruction.
    2680                 : **
    2681                 : ** See also: Distinct, NotFound, NotExists, Found
    2682                 : */
    2683                 : case OP_IsUnique: {
    2684             145 :   int i = pOp->p1;
    2685             145 :   Mem *pNos = &pTos[-1];
    2686                 :   BtCursor *pCrsr;
    2687                 :   int R;
    2688                 : 
    2689                 :   /* Pop the value R off the top of the stack
    2690                 :   */
    2691                 :   assert( pNos>=p->aStack );
    2692             145 :   Integerify(pTos);
    2693             145 :   R = pTos->i;
    2694             145 :   pTos--;
    2695                 :   assert( i>=0 && i<=p->nCursor );
    2696             145 :   if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
    2697                 :     int res, rc;
    2698                 :     int v;         /* The record number on the P1 entry that matches K */
    2699                 :     char *zKey;    /* The value of K */
    2700                 :     int nKey;      /* Number of bytes in K */
    2701                 : 
    2702                 :     /* Make sure K is a string and make zKey point to K
    2703                 :     */
    2704             145 :     Stringify(pNos);
    2705             145 :     zKey = pNos->z;
    2706             145 :     nKey = pNos->n;
    2707                 :     assert( nKey >= 4 );
    2708                 : 
    2709                 :     /* Search for an entry in P1 where all but the last four bytes match K.
    2710                 :     ** If there is no such entry, jump immediately to P2.
    2711                 :     */
    2712                 :     assert( p->aCsr[i].deferredMoveto==0 );
    2713             145 :     rc = sqliteBtreeMoveto(pCrsr, zKey, nKey-4, &res);
    2714             145 :     if( rc!=SQLITE_OK ) goto abort_due_to_error;
    2715             145 :     if( res<0 ){
    2716             131 :       rc = sqliteBtreeNext(pCrsr, &res);
    2717             131 :       if( res ){
    2718             118 :         pc = pOp->p2 - 1;
    2719             118 :         break;
    2720                 :       }
    2721                 :     }
    2722              27 :     rc = sqliteBtreeKeyCompare(pCrsr, zKey, nKey-4, 4, &res);
    2723              27 :     if( rc!=SQLITE_OK ) goto abort_due_to_error;
    2724              27 :     if( res>0 ){
    2725              27 :       pc = pOp->p2 - 1;
    2726              27 :       break;
    2727                 :     }
    2728                 : 
    2729                 :     /* At this point, pCrsr is pointing to an entry in P1 where all but
    2730                 :     ** the last for bytes of the key match K.  Check to see if the last
    2731                 :     ** four bytes of the key are different from R.  If the last four
    2732                 :     ** bytes equal R then jump immediately to P2.
    2733                 :     */
    2734               0 :     sqliteBtreeKey(pCrsr, nKey - 4, 4, (char*)&v);
    2735               0 :     v = keyToInt(v);
    2736               0 :     if( v==R ){
    2737               0 :       pc = pOp->p2 - 1;
    2738               0 :       break;
    2739                 :     }
    2740                 : 
    2741                 :     /* The last four bytes of the key are different from R.  Convert the
    2742                 :     ** last four bytes of the key into an integer and push it onto the
    2743                 :     ** stack.  (These bytes are the record number of an entry that
    2744                 :     ** violates a UNIQUE constraint.)
    2745                 :     */
    2746               0 :     pTos++;
    2747               0 :     pTos->i = v;
    2748               0 :     pTos->flags = MEM_Int;
    2749                 :   }
    2750               0 :   break;
    2751                 : }
    2752                 : 
    2753                 : /* Opcode: NotExists P1 P2 *
    2754                 : **
    2755                 : ** Use the top of the stack as a integer key.  If a record with that key
    2756                 : ** does not exist in table of P1, then jump to P2.  If the record
    2757                 : ** does exist, then fall thru.  The cursor is left pointing to the
    2758                 : ** record if it exists.  The integer key is popped from the stack.
    2759                 : **
    2760                 : ** The difference between this operation and NotFound is that this
    2761                 : ** operation assumes the key is an integer and NotFound assumes it
    2762                 : ** is a string.
    2763                 : **
    2764                 : ** See also: Distinct, Found, MoveTo, NotFound, IsUnique
    2765                 : */
    2766                 : case OP_NotExists: {
    2767              20 :   int i = pOp->p1;
    2768                 :   BtCursor *pCrsr;
    2769                 :   assert( pTos>=p->aStack );
    2770                 :   assert( i>=0 && i<p->nCursor );
    2771              20 :   if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
    2772                 :     int res, rx, iKey;
    2773                 :     assert( pTos->flags & MEM_Int );
    2774              20 :     iKey = intToKey(pTos->i);
    2775              20 :     rx = sqliteBtreeMoveto(pCrsr, (char*)&iKey, sizeof(int), &res);
    2776              20 :     p->aCsr[i].lastRecno = pTos->i;
    2777              20 :     p->aCsr[i].recnoIsValid = res==0;
    2778              20 :     p->aCsr[i].nullRow = 0;
    2779              20 :     if( rx!=SQLITE_OK || res!=0 ){
    2780               5 :       pc = pOp->p2 - 1;
    2781               5 :       p->aCsr[i].recnoIsValid = 0;
    2782                 :     }
    2783                 :   }
    2784              20 :   Release(pTos);
    2785              20 :   pTos--;
    2786              20 :   break;
    2787                 : }
    2788                 : 
    2789                 : /* Opcode: NewRecno P1 * *
    2790                 : **
    2791                 : ** Get a new integer record number used as the key to a table.
    2792                 : ** The record number is not previously used as a key in the database
    2793                 : ** table that cursor P1 points to.  The new record number is pushed 
    2794                 : ** onto the stack.
    2795                 : */
    2796                 : case OP_NewRecno: {
    2797             396 :   int i = pOp->p1;
    2798             396 :   int v = 0;
    2799                 :   Cursor *pC;
    2800                 :   assert( i>=0 && i<p->nCursor );
    2801             396 :   if( (pC = &p->aCsr[i])->pCursor==0 ){
    2802               0 :     v = 0;
    2803                 :   }else{
    2804                 :     /* The next rowid or record number (different terms for the same
    2805                 :     ** thing) is obtained in a two-step algorithm.
    2806                 :     **
    2807                 :     ** First we attempt to find the largest existing rowid and add one
    2808                 :     ** to that.  But if the largest existing rowid is already the maximum
    2809                 :     ** positive integer, we have to fall through to the second
    2810                 :     ** probabilistic algorithm
    2811                 :     **
    2812                 :     ** The second algorithm is to select a rowid at random and see if
    2813                 :     ** it already exists in the table.  If it does not exist, we have
    2814                 :     ** succeeded.  If the random rowid does exist, we select a new one
    2815                 :     ** and try again, up to 1000 times.
    2816                 :     **
    2817                 :     ** For a table with less than 2 billion entries, the probability
    2818                 :     ** of not finding a unused rowid is about 1.0e-300.  This is a 
    2819                 :     ** non-zero probability, but it is still vanishingly small and should
    2820                 :     ** never cause a problem.  You are much, much more likely to have a
    2821                 :     ** hardware failure than for this algorithm to fail.
    2822                 :     **
    2823                 :     ** The analysis in the previous paragraph assumes that you have a good
    2824                 :     ** source of random numbers.  Is a library function like lrand48()
    2825                 :     ** good enough?  Maybe. Maybe not. It's hard to know whether there
    2826                 :     ** might be subtle bugs is some implementations of lrand48() that
    2827                 :     ** could cause problems. To avoid uncertainty, SQLite uses its own 
    2828                 :     ** random number generator based on the RC4 algorithm.
    2829                 :     **
    2830                 :     ** To promote locality of reference for repetitive inserts, the
    2831                 :     ** first few attempts at chosing a random rowid pick values just a little
    2832                 :     ** larger than the previous rowid.  This has been shown experimentally
    2833                 :     ** to double the speed of the COPY operation.
    2834                 :     */
    2835                 :     int res, rx, cnt, x;
    2836             396 :     cnt = 0;
    2837             396 :     if( !pC->useRandomRowid ){
    2838             396 :       if( pC->nextRowidValid ){
    2839              43 :         v = pC->nextRowid;
    2840                 :       }else{
    2841             353 :         rx = sqliteBtreeLast(pC->pCursor, &res);
    2842             353 :         if( res ){
    2843             180 :           v = 1;
    2844                 :         }else{
    2845             173 :           sqliteBtreeKey(pC->pCursor, 0, sizeof(v), (void*)&v);
    2846             173 :           v = keyToInt(v);
    2847             173 :           if( v==0x7fffffff ){
    2848               0 :             pC->useRandomRowid = 1;
    2849                 :           }else{
    2850             173 :             v++;
    2851                 :           }
    2852                 :         }
    2853                 :       }
    2854             396 :       if( v<0x7fffffff ){
    2855             396 :         pC->nextRowidValid = 1;
    2856             396 :         pC->nextRowid = v+1;
    2857                 :       }else{
    2858               0 :         pC->nextRowidValid = 0;
    2859                 :       }
    2860                 :     }
    2861             396 :     if( pC->useRandomRowid ){
    2862               0 :       v = db->priorNewRowid;
    2863               0 :       cnt = 0;
    2864                 :       do{
    2865               0 :         if( v==0 || cnt>2 ){
    2866               0 :           sqliteRandomness(sizeof(v), &v);
    2867               0 :           if( cnt<5 ) v &= 0xffffff;
    2868                 :         }else{
    2869                 :           unsigned char r;
    2870               0 :           sqliteRandomness(1, &r);
    2871               0 :           v += r + 1;
    2872                 :         }
    2873               0 :         if( v==0 ) continue;
    2874               0 :         x = intToKey(v);
    2875               0 :         rx = sqliteBtreeMoveto(pC->pCursor, &x, sizeof(int), &res);
    2876               0 :         cnt++;
    2877               0 :       }while( cnt<1000 && rx==SQLITE_OK && res==0 );
    2878               0 :       db->priorNewRowid = v;
    2879               0 :       if( rx==SQLITE_OK && res==0 ){
    2880               0 :         rc = SQLITE_FULL;
    2881               0 :         goto abort_due_to_error;
    2882                 :       }
    2883                 :     }
    2884             396 :     pC->recnoIsValid = 0;
    2885             396 :     pC->deferredMoveto = 0;
    2886                 :   }
    2887             396 :   pTos++;
    2888             396 :   pTos->i = v;
    2889             396 :   pTos->flags = MEM_Int;
    2890             396 :   break;
    2891                 : }
    2892                 : 
    2893                 : /* Opcode: PutIntKey P1 P2 *
    2894                 : **
    2895                 : ** Write an entry into the table of cursor P1.  A new entry is
    2896                 : ** created if it doesn't already exist or the data for an existing
    2897                 : ** entry is overwritten.  The data is the value on the top of the
    2898                 : ** stack.  The key is the next value down on the stack.  The key must
    2899                 : ** be an integer.  The stack is popped twice by this instruction.
    2900                 : **
    2901                 : ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
    2902                 : ** incremented (otherwise not).  If the OPFLAG_CSCHANGE flag is set,
    2903                 : ** then the current statement change count is incremented (otherwise not).
    2904                 : ** If the OPFLAG_LASTROWID flag of P2 is set, then rowid is
    2905                 : ** stored for subsequent return by the sqlite_last_insert_rowid() function
    2906                 : ** (otherwise it's unmodified).
    2907                 : */
    2908                 : /* Opcode: PutStrKey P1 * *
    2909                 : **
    2910                 : ** Write an entry into the table of cursor P1.  A new entry is
    2911                 : ** created if it doesn't already exist or the data for an existing
    2912                 : ** entry is overwritten.  The data is the value on the top of the
    2913                 : ** stack.  The key is the next value down on the stack.  The key must
    2914                 : ** be a string.  The stack is popped twice by this instruction.
    2915                 : **
    2916                 : ** P1 may not be a pseudo-table opened using the OpenPseudo opcode.
    2917                 : */
    2918                 : case OP_PutIntKey:
    2919                 : case OP_PutStrKey: {
    2920             496 :   Mem *pNos = &pTos[-1];
    2921             496 :   int i = pOp->p1;
    2922                 :   Cursor *pC;
    2923                 :   assert( pNos>=p->aStack );
    2924                 :   assert( i>=0 && i<p->nCursor );
    2925             496 :   if( ((pC = &p->aCsr[i])->pCursor!=0 || pC->pseudoTable) ){
    2926                 :     char *zKey;
    2927                 :     int nKey, iKey;
    2928             496 :     if( pOp->opcode==OP_PutStrKey ){
    2929               0 :       Stringify(pNos);
    2930               0 :       nKey = pNos->n;
    2931               0 :       zKey = pNos->z;
    2932                 :     }else{
    2933                 :       assert( pNos->flags & MEM_Int );
    2934             496 :       nKey = sizeof(int);
    2935             496 :       iKey = intToKey(pNos->i);
    2936             496 :       zKey = (char*)&iKey;
    2937             496 :       if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++;
    2938             496 :       if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->i;
    2939             496 :       if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++;
    2940             496 :       if( pC->nextRowidValid && pTos->i>=pC->nextRowid ){
    2941               4 :         pC->nextRowidValid = 0;
    2942                 :       }
    2943                 :     }
    2944             496 :     if( pTos->flags & MEM_Null ){
    2945              95 :       pTos->z = 0;
    2946              95 :       pTos->n = 0;
    2947                 :     }else{
    2948                 :       assert( pTos->flags & MEM_Str );
    2949                 :     }
    2950             496 :     if( pC->pseudoTable ){
    2951                 :       /* PutStrKey does not work for pseudo-tables.
    2952                 :       ** The following assert makes sure we are not trying to use
    2953                 :       ** PutStrKey on a pseudo-table
    2954                 :       */
    2955                 :       assert( pOp->opcode==OP_PutIntKey );
    2956               0 :       sqliteFree(pC->pData);
    2957               0 :       pC->iKey = iKey;
    2958               0 :       pC->nData = pTos->n;
    2959               0 :       if( pTos->flags & MEM_Dyn ){
    2960               0 :         pC->pData = pTos->z;
    2961               0 :         pTos->flags = MEM_Null;
    2962                 :       }else{
    2963               0 :         pC->pData = sqliteMallocRaw( pC->nData );
    2964               0 :         if( pC->pData ){
    2965               0 :           memcpy(pC->pData, pTos->z, pC->nData);
    2966                 :         }
    2967                 :       }
    2968               0 :       pC->nullRow = 0;
    2969                 :     }else{
    2970             496 :       rc = sqliteBtreeInsert(pC->pCursor, zKey, nKey, pTos->z, pTos->n);
    2971                 :     }
    2972             496 :     pC->recnoIsValid = 0;
    2973             496 :     pC->deferredMoveto = 0;
    2974                 :   }
    2975             496 :   popStack(&pTos, 2);
    2976             496 :   break;
    2977                 : }
    2978                 : 
    2979                 : /* Opcode: Delete P1 P2 *
    2980                 : **
    2981                 : ** Delete the record at which the P1 cursor is currently pointing.
    2982                 : **
    2983                 : ** The cursor will be left pointing at either the next or the previous
    2984                 : ** record in the table. If it is left pointing at the next record, then
    2985                 : ** the next Next instruction will be a no-op.  Hence it is OK to delete
    2986                 : ** a record from within an Next loop.
    2987                 : **
    2988                 : ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
    2989                 : ** incremented (otherwise not).  If OPFLAG_CSCHANGE flag is set,
    2990                 : ** then the current statement change count is incremented (otherwise not).
    2991                 : **
    2992                 : ** If P1 is a pseudo-table, then this instruction is a no-op.
    2993                 : */
    2994                 : case OP_Delete: {
    2995               4 :   int i = pOp->p1;
    2996                 :   Cursor *pC;
    2997                 :   assert( i>=0 && i<p->nCursor );
    2998               4 :   pC = &p->aCsr[i];
    2999               4 :   if( pC->pCursor!=0 ){
    3000               4 :     sqliteVdbeCursorMoveto(pC);
    3001               4 :     rc = sqliteBtreeDelete(pC->pCursor);
    3002               4 :     pC->nextRowidValid = 0;
    3003                 :   }
    3004               4 :   if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++;
    3005               4 :   if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++;
    3006               4 :   break;
    3007                 : }
    3008                 : 
    3009                 : /* Opcode: SetCounts * * *
    3010                 : **
    3011                 : ** Called at end of statement.  Updates lsChange (last statement change count)
    3012                 : ** and resets csChange (current statement change count) to 0.
    3013                 : */
    3014                 : case OP_SetCounts: {
    3015             265 :   db->lsChange=db->csChange;
    3016             265 :   db->csChange=0;
    3017             265 :   break;
    3018                 : }
    3019                 : 
    3020                 : /* Opcode: KeyAsData P1 P2 *
    3021                 : **
    3022                 : ** Turn the key-as-data mode for cursor P1 either on (if P2==1) or
    3023                 : ** off (if P2==0).  In key-as-data mode, the OP_Column opcode pulls
    3024                 : ** data off of the key rather than the data.  This is used for
    3025                 : ** processing compound selects.
    3026                 : */
    3027                 : case OP_KeyAsData: {
    3028               0 :   int i = pOp->p1;
    3029                 :   assert( i>=0 && i<p->nCursor );
    3030               0 :   p->aCsr[i].keyAsData = pOp->p2;
    3031               0 :   break;
    3032                 : }
    3033                 : 
    3034                 : /* Opcode: RowData P1 * *
    3035                 : **
    3036                 : ** Push onto the stack the complete row data for cursor P1.
    3037                 : ** There is no interpretation of the data.  It is just copied
    3038                 : ** onto the stack exactly as it is found in the database file.
    3039                 : **
    3040                 : ** If the cursor is not pointing to a valid row, a NULL is pushed
    3041                 : ** onto the stack.
    3042                 : */
    3043                 : /* Opcode: RowKey P1 * *
    3044                 : **
    3045                 : ** Push onto the stack the complete row key for cursor P1.
    3046                 : ** There is no interpretation of the key.  It is just copied
    3047                 : ** onto the stack exactly as it is found in the database file.
    3048                 : **
    3049                 : ** If the cursor is not pointing to a valid row, a NULL is pushed
    3050                 : ** onto the stack.
    3051                 : */
    3052                 : case OP_RowKey:
    3053                 : case OP_RowData: {
    3054              77 :   int i = pOp->p1;
    3055                 :   Cursor *pC;
    3056                 :   int n;
    3057                 : 
    3058              77 :   pTos++;
    3059                 :   assert( i>=0 && i<p->nCursor );
    3060              77 :   pC = &p->aCsr[i];
    3061              77 :   if( pC->nullRow ){
    3062               0 :     pTos->flags = MEM_Null;
    3063              77 :   }else if( pC->pCursor!=0 ){
    3064              77 :     BtCursor *pCrsr = pC->pCursor;
    3065              77 :     sqliteVdbeCursorMoveto(pC);
    3066              77 :     if( pC->nullRow ){
    3067               0 :       pTos->flags = MEM_Null;
    3068               0 :       break;
    3069             154 :     }else if( pC->keyAsData || pOp->opcode==OP_RowKey ){
    3070              77 :       sqliteBtreeKeySize(pCrsr, &n);
    3071                 :     }else{
    3072               0 :       sqliteBtreeDataSize(pCrsr, &n);
    3073                 :     }
    3074              77 :     pTos->n = n;
    3075              77 :     if( n<=NBFS ){
    3076              77 :       pTos->flags = MEM_Str | MEM_Short;
    3077              77 :       pTos->z = pTos->zShort;
    3078                 :     }else{
    3079               0 :       char *z = sqliteMallocRaw( n );
    3080               0 :       if( z==0 ) goto no_mem;
    3081               0 :       pTos->flags = MEM_Str | MEM_Dyn;
    3082               0 :       pTos->z = z;
    3083                 :     }
    3084             154 :     if( pC->keyAsData || pOp->opcode==OP_RowKey ){
    3085              77 :       sqliteBtreeKey(pCrsr, 0, n, pTos->z);
    3086                 :     }else{
    3087               0 :       sqliteBtreeData(pCrsr, 0, n, pTos->z);
    3088                 :     }
    3089               0 :   }else if( pC->pseudoTable ){
    3090               0 :     pTos->n = pC->nData;
    3091               0 :     pTos->z = pC->pData;
    3092               0 :     pTos->flags = MEM_Str|MEM_Ephem;
    3093                 :   }else{
    3094               0 :     pTos->flags = MEM_Null;
    3095                 :   }
    3096              77 :   break;
    3097                 : }
    3098                 : 
    3099                 : /* Opcode: Column P1 P2 *
    3100                 : **
    3101                 : ** Interpret the data that cursor P1 points to as
    3102                 : ** a structure built using the MakeRecord instruction.
    3103                 : ** (See the MakeRecord opcode for additional information about
    3104                 : ** the format of the data.)
    3105                 : ** Push onto the stack the value of the P2-th column contained
    3106                 : ** in the data.
    3107                 : **
    3108                 : ** If the KeyAsData opcode has previously executed on this cursor,
    3109                 : ** then the field might be extracted from the key rather than the
    3110                 : ** data.
    3111                 : **
    3112                 : ** If P1 is negative, then the record is stored on the stack rather
    3113                 : ** than in a table.  For P1==-1, the top of the stack is used.
    3114                 : ** For P1==-2, the next on the stack is used.  And so forth.  The
    3115                 : ** value pushed is always just a pointer into the record which is
    3116                 : ** stored further down on the stack.  The column value is not copied.
    3117                 : */
    3118                 : case OP_Column: {
    3119                 :   int amt, offset, end, payloadSize;
    3120             878 :   int i = pOp->p1;
    3121             878 :   int p2 = pOp->p2;
    3122                 :   Cursor *pC;
    3123                 :   char *zRec;
    3124                 :   BtCursor *pCrsr;
    3125                 :   int idxWidth;
    3126                 :   unsigned char aHdr[10];
    3127                 : 
    3128                 :   assert( i<p->nCursor );
    3129             878 :   pTos++;
    3130             878 :   if( i<0 ){
    3131                 :     assert( &pTos[i]>=p->aStack );
    3132                 :     assert( pTos[i].flags & MEM_Str );
    3133               0 :     zRec = pTos[i].z;
    3134               0 :     payloadSize = pTos[i].n;
    3135             878 :   }else if( (pC = &p->aCsr[i])->pCursor!=0 ){
    3136             878 :     sqliteVdbeCursorMoveto(pC);
    3137             878 :     zRec = 0;
    3138             878 :     pCrsr = pC->pCursor;
    3139             878 :     if( pC->nullRow ){
    3140               9 :       payloadSize = 0;
    3141             869 :     }else if( pC->keyAsData ){
    3142               0 :       sqliteBtreeKeySize(pCrsr, &payloadSize);
    3143                 :     }else{
    3144             869 :       sqliteBtreeDataSize(pCrsr, &payloadSize);
    3145                 :     }
    3146               0 :   }else if( pC->pseudoTable ){
    3147               0 :     payloadSize = pC->nData;
    3148               0 :     zRec = pC->pData;
    3149                 :     assert( payloadSize==0 || zRec!=0 );
    3150                 :   }else{
    3151               0 :     payloadSize = 0;
    3152                 :   }
    3153                 : 
    3154                 :   /* Figure out how many bytes in the column data and where the column
    3155                 :   ** data begins.
    3156                 :   */
    3157             878 :   if( payloadSize==0 ){
    3158               9 :     pTos->flags = MEM_Null;
    3159               9 :     break;
    3160             869 :   }else if( payloadSize<256 ){
    3161             869 :     idxWidth = 1;
    3162               0 :   }else if( payloadSize<65536 ){
    3163               0 :     idxWidth = 2;
    3164                 :   }else{
    3165               0 :     idxWidth = 3;
    3166                 :   }
    3167                 : 
    3168                 :   /* Figure out where the requested column is stored and how big it is.
    3169                 :   */
    3170             869 :   if( payloadSize < idxWidth*(p2+1) ){
    3171               0 :     rc = SQLITE_CORRUPT;
    3172               0 :     goto abort_due_to_error;
    3173                 :   }
    3174             869 :   if( zRec ){
    3175               0 :     memcpy(aHdr, &zRec[idxWidth*p2], idxWidth*2);
    3176             869 :   }else if( pC->keyAsData ){
    3177               0 :     sqliteBtreeKey(pCrsr, idxWidth*p2, idxWidth*2, (char*)aHdr);
    3178                 :   }else{
    3179             869 :     sqliteBtreeData(pCrsr, idxWidth*p2, idxWidth*2, (char*)aHdr);
    3180                 :   }
    3181             869 :   offset = aHdr[0];
    3182             869 :   end = aHdr[idxWidth];
    3183             869 :   if( idxWidth>1 ){
    3184               0 :     offset |= aHdr[1]<<8;
    3185               0 :     end |= aHdr[idxWidth+1]<<8;
    3186               0 :     if( idxWidth>2 ){
    3187               0 :       offset |= aHdr[2]<<16;
    3188               0 :       end |= aHdr[idxWidth+2]<<16;
    3189                 :     }
    3190                 :   }
    3191             869 :   amt = end - offset;
    3192             869 :   if( amt<0 || offset<0 || end>payloadSize ){
    3193               0 :     rc = SQLITE_CORRUPT;
    3194               0 :     goto abort_due_to_error;
    3195                 :   }
    3196                 : 
    3197                 :   /* amt and offset now hold the offset to the start of data and the
    3198                 :   ** amount of data.  Go get the data and put it on the stack.
    3199                 :   */
    3200             869 :   pTos->n = amt;
    3201             869 :   if( amt==0 ){
    3202              16 :     pTos->flags = MEM_Null;
    3203             853 :   }else if( zRec ){
    3204               0 :     pTos->flags = MEM_Str | MEM_Ephem;
    3205               0 :     pTos->z = &zRec[offset];
    3206                 :   }else{
    3207             853 :     if( amt<=NBFS ){
    3208             842 :       pTos->flags = MEM_Str | MEM_Short;
    3209             842 :       pTos->z = pTos->zShort;
    3210                 :     }else{
    3211              11 :       char *z = sqliteMallocRaw( amt );
    3212              11 :       if( z==0 ) goto no_mem;
    3213              11 :       pTos->flags = MEM_Str | MEM_Dyn;
    3214              11 :       pTos->z = z;
    3215                 :     }
    3216             853 :     if( pC->keyAsData ){
    3217               0 :       sqliteBtreeKey(pCrsr, offset, amt, pTos->z);
    3218                 :     }else{
    3219             853 :       sqliteBtreeData(pCrsr, offset, amt, pTos->z);
    3220                 :     }
    3221                 :   }
    3222             869 :   break;
    3223                 : }
    3224                 : 
    3225                 : /* Opcode: Recno P1 * *
    3226                 : **
    3227                 : ** Push onto the stack an integer which is the first 4 bytes of the
    3228                 : ** the key to the current entry in a sequential scan of the database
    3229                 : ** file P1.  The sequential scan should have been started using the 
    3230                 : ** Next opcode.
    3231                 : */
    3232                 : case OP_Recno: {
    3233              33 :   int i = pOp->p1;
    3234                 :   Cursor *pC;
    3235                 :   int v;
    3236                 : 
    3237                 :   assert( i>=0 && i<p->nCursor );
    3238              33 :   pC = &p->aCsr[i];
    3239              33 :   sqliteVdbeCursorMoveto(pC);
    3240              33 :   pTos++;
    3241              33 :   if( pC->recnoIsValid ){
    3242               8 :     v = pC->lastRecno;
    3243              25 :   }else if( pC->pseudoTable ){
    3244               0 :     v = keyToInt(pC->iKey);
    3245              25 :   }else if( pC->nullRow || pC->pCursor==0 ){
    3246               0 :     pTos->flags = MEM_Null;
    3247               0 :     break;
    3248                 :   }else{
    3249                 :     assert( pC->pCursor!=0 );
    3250              25 :     sqliteBtreeKey(pC->pCursor, 0, sizeof(u32), (char*)&v);
    3251              25 :     v = keyToInt(v);
    3252                 :   }
    3253              33 :   pTos->i = v;
    3254              33 :   pTos->flags = MEM_Int;
    3255              33 :   break;
    3256                 : }
    3257                 : 
    3258                 : /* Opcode: FullKey P1 * *
    3259                 : **
    3260                 : ** Extract the complete key from the record that cursor P1 is currently
    3261                 : ** pointing to and push the key onto the stack as a string.
    3262                 : **
    3263                 : ** Compare this opcode to Recno.  The Recno opcode extracts the first
    3264                 : ** 4 bytes of the key and pushes those bytes onto the stack as an
    3265                 : ** integer.  This instruction pushes the entire key as a string.
    3266                 : **
    3267                 : ** This opcode may not be used on a pseudo-table.
    3268                 : */
    3269                 : case OP_FullKey: {
    3270               0 :   int i = pOp->p1;
    3271                 :   BtCursor *pCrsr;
    3272                 : 
    3273                 :   assert( p->aCsr[i].keyAsData );
    3274                 :   assert( !p->aCsr[i].pseudoTable );
    3275                 :   assert( i>=0 && i<p->nCursor );
    3276               0 :   pTos++;
    3277               0 :   if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
    3278                 :     int amt;
    3279                 :     char *z;
    3280                 : 
    3281               0 :     sqliteVdbeCursorMoveto(&p->aCsr[i]);
    3282               0 :     sqliteBtreeKeySize(pCrsr, &amt);
    3283               0 :     if( amt<=0 ){
    3284               0 :       rc = SQLITE_CORRUPT;
    3285               0 :       goto abort_due_to_error;
    3286                 :     }
    3287               0 :     if( amt>NBFS ){
    3288               0 :       z = sqliteMallocRaw( amt );
    3289               0 :       if( z==0 ) goto no_mem;
    3290               0 :       pTos->flags = MEM_Str | MEM_Dyn;
    3291                 :     }else{
    3292               0 :       z = pTos->zShort;
    3293               0 :       pTos->flags = MEM_Str | MEM_Short;
    3294                 :     }
    3295               0 :     sqliteBtreeKey(pCrsr, 0, amt, z);
    3296               0 :     pTos->z = z;
    3297               0 :     pTos->n = amt;
    3298                 :   }
    3299               0 :   break;
    3300                 : }
    3301                 : 
    3302                 : /* Opcode: NullRow P1 * *
    3303                 : **
    3304                 : ** Move the cursor P1 to a null row.  Any OP_Column operations
    3305                 : ** that occur while the cursor is on the null row will always push 
    3306                 : ** a NULL onto the stack.
    3307                 : */
    3308                 : case OP_NullRow: {
    3309              14 :   int i = pOp->p1;
    3310                 : 
    3311                 :   assert( i>=0 && i<p->nCursor );
    3312              14 :   p->aCsr[i].nullRow = 1;
    3313              14 :   p->aCsr[i].recnoIsValid = 0;
    3314              14 :   break;
    3315                 : }
    3316                 : 
    3317                 : /* Opcode: Last P1 P2 *
    3318                 : **
    3319                 : ** The next use of the Recno or Column or Next instruction for P1 
    3320                 : ** will refer to the last entry in the database table or index.
    3321                 : ** If the table or index is empty and P2>0, then jump immediately to P2.
    3322                 : ** If P2 is 0 or if the table or index is not empty, fall through
    3323                 : ** to the following instruction.
    3324                 : */
    3325                 : case OP_Last: {
    3326               0 :   int i = pOp->p1;
    3327                 :   Cursor *pC;
    3328                 :   BtCursor *pCrsr;
    3329                 : 
    3330                 :   assert( i>=0 && i<p->nCursor );
    3331               0 :   pC = &p->aCsr[i];
    3332               0 :   if( (pCrsr = pC->pCursor)!=0 ){
    3333                 :     int res;
    3334               0 :     rc = sqliteBtreeLast(pCrsr, &res);
    3335               0 :     pC->nullRow = res;
    3336               0 :     pC->deferredMoveto = 0;
    3337               0 :     if( res && pOp->p2>0 ){
    3338               0 :       pc = pOp->p2 - 1;
    3339                 :     }
    3340                 :   }else{
    3341               0 :     pC->nullRow = 0;
    3342                 :   }
    3343               0 :   break;
    3344                 : }
    3345                 : 
    3346                 : /* Opcode: Rewind P1 P2 *
    3347                 : **
    3348                 : ** The next use of the Recno or Column or Next instruction for P1 
    3349                 : ** will refer to the first entry in the database table or index.
    3350                 : ** If the table or index is empty and P2>0, then jump immediately to P2.
    3351                 : ** If P2 is 0 or if the table or index is not empty, fall through
    3352                 : ** to the following instruction.
    3353                 : */
    3354                 : case OP_Rewind: {
    3355             477 :   int i = pOp->p1;
    3356                 :   Cursor *pC;
    3357                 :   BtCursor *pCrsr;
    3358                 : 
    3359                 :   assert( i>=0 && i<p->nCursor );
    3360             477 :   pC = &p->aCsr[i];
    3361             477 :   if( (pCrsr = pC->pCursor)!=0 ){
    3362                 :     int res;
    3363             477 :     rc = sqliteBtreeFirst(pCrsr, &res);
    3364             477 :     pC->atFirst = res==0;
    3365             477 :     pC->nullRow = res;
    3366             477 :     pC->deferredMoveto = 0;
    3367             477 :     if( res && pOp->p2>0 ){
    3368             309 :       pc = pOp->p2 - 1;
    3369                 :     }
    3370                 :   }else{
    3371               0 :     pC->nullRow = 0;
    3372                 :   }
    3373             477 :   break;
    3374                 : }
    3375                 : 
    3376                 : /* Opcode: Next P1 P2 *
    3377                 : **
    3378                 : ** Advance cursor P1 so that it points to the next key/data pair in its
    3379                 : ** table or index.  If there are no more key/value pairs then fall through
    3380                 : ** to the following instruction.  But if the cursor advance was successful,
    3381                 : ** jump immediately to P2.
    3382                 : **
    3383                 : ** See also: Prev
    3384                 : */
    3385                 : /* Opcode: Prev P1 P2 *
    3386                 : **
    3387                 : ** Back up cursor P1 so that it points to the previous key/data pair in its
    3388                 : ** table or index.  If there is no previous key/value pairs then fall through
    3389                 : ** to the following instruction.  But if the cursor backup was successful,
    3390                 : ** jump immediately to P2.
    3391                 : */
    3392                 : case OP_Prev:
    3393                 : case OP_Next: {
    3394                 :   Cursor *pC;
    3395                 :   BtCursor *pCrsr;
    3396                 : 
    3397             488 :   CHECK_FOR_INTERRUPT;
    3398                 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
    3399             488 :   pC = &p->aCsr[pOp->p1];
    3400             488 :   if( (pCrsr = pC->pCursor)!=0 ){
    3401                 :     int res;
    3402             488 :     if( pC->nullRow ){
    3403               7 :       res = 1;
    3404                 :     }else{
    3405                 :       assert( pC->deferredMoveto==0 );
    3406             481 :       rc = pOp->opcode==OP_Next ? sqliteBtreeNext(pCrsr, &res) :
    3407                 :                                   sqliteBtreePrevious(pCrsr, &res);
    3408             481 :       pC->nullRow = res;
    3409                 :     }
    3410             488 :     if( res==0 ){
    3411             315 :       pc = pOp->p2 - 1;
    3412             315 :       sqlite_search_count++;
    3413                 :     }
    3414                 :   }else{
    3415               0 :     pC->nullRow = 1;
    3416                 :   }
    3417             488 :   pC->recnoIsValid = 0;
    3418             488 :   break;
    3419                 : }
    3420                 : 
    3421                 : /* Opcode: IdxPut P1 P2 P3
    3422                 : **
    3423                 : ** The top of the stack holds a SQL index key made using the
    3424                 : ** MakeIdxKey instruction.  This opcode writes that key into the
    3425                 : ** index P1.  Data for the entry is nil.
    3426                 : **
    3427                 : ** If P2==1, then the key must be unique.  If the key is not unique,
    3428                 : ** the program aborts with a SQLITE_CONSTRAINT error and the database
    3429                 : ** is rolled back.  If P3 is not null, then it becomes part of the
    3430                 : ** error message returned with the SQLITE_CONSTRAINT.
    3431                 : */
    3432                 : case OP_IdxPut: {
    3433             145 :   int i = pOp->p1;
    3434                 :   BtCursor *pCrsr;
    3435                 :   assert( pTos>=p->aStack );
    3436                 :   assert( i>=0 && i<p->nCursor );
    3437                 :   assert( pTos->flags & MEM_Str );
    3438             145 :   if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
    3439             145 :     int nKey = pTos->n;
    3440             145 :     const char *zKey = pTos->z;
    3441             145 :     if( pOp->p2 ){
    3442                 :       int res, n;
    3443                 :       assert( nKey >= 4 );
    3444               0 :       rc = sqliteBtreeMoveto(pCrsr, zKey, nKey-4, &res);
    3445               0 :       if( rc!=SQLITE_OK ) goto abort_due_to_error;
    3446               0 :       while( res!=0 ){
    3447                 :         int c;
    3448               0 :         sqliteBtreeKeySize(pCrsr, &n);
    3449               0 :         if( n==nKey
    3450                 :            && sqliteBtreeKeyCompare(pCrsr, zKey, nKey-4, 4, &c)==SQLITE_OK
    3451                 :            && c==0
    3452                 :         ){
    3453               0 :           rc = SQLITE_CONSTRAINT;
    3454               0 :           if( pOp->p3 && pOp->p3[0] ){
    3455               0 :             sqliteSetString(&p->zErrMsg, pOp->p3, (char*)0);
    3456                 :           }
    3457               0 :           goto abort_due_to_error;
    3458                 :         }
    3459               0 :         if( res<0 ){
    3460               0 :           sqliteBtreeNext(pCrsr, &res);
    3461               0 :           res = +1;
    3462                 :         }else{
    3463               0 :           break;
    3464                 :         }
    3465                 :       }
    3466                 :     }
    3467             145 :     rc = sqliteBtreeInsert(pCrsr, zKey, nKey, "", 0);
    3468                 :     assert( p->aCsr[i].deferredMoveto==0 );
    3469                 :   }
    3470             145 :   Release(pTos);
    3471             145 :   pTos--;
    3472             145 :   break;
    3473                 : }
    3474                 : 
    3475                 : /* Opcode: IdxDelete P1 * *
    3476                 : **
    3477                 : ** The top of the stack is an index key built using the MakeIdxKey opcode.
    3478                 : ** This opcode removes that entry from the index.
    3479                 : */
    3480                 : case OP_IdxDelete: {
    3481               0 :   int i = pOp->p1;
    3482                 :   BtCursor *pCrsr;
    3483                 :   assert( pTos>=p->aStack );
    3484                 :   assert( pTos->flags & MEM_Str );
    3485                 :   assert( i>=0 && i<p->nCursor );
    3486               0 :   if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
    3487                 :     int rx, res;
    3488               0 :     rx = sqliteBtreeMoveto(pCrsr, pTos->z, pTos->n, &res);
    3489               0 :     if( rx==SQLITE_OK && res==0 ){
    3490               0 :       rc = sqliteBtreeDelete(pCrsr);
    3491                 :     }
    3492                 :     assert( p->aCsr[i].deferredMoveto==0 );
    3493                 :   }
    3494               0 :   Release(pTos);
    3495               0 :   pTos--;
    3496               0 :   break;
    3497                 : }
    3498                 : 
    3499                 : /* Opcode: IdxRecno P1 * *
    3500                 : **
    3501                 : ** Push onto the stack an integer which is the last 4 bytes of the
    3502                 : ** the key to the current entry in index P1.  These 4 bytes should
    3503                 : ** be the record number of the table entry to which this index entry
    3504                 : ** points.
    3505                 : **
    3506                 : ** See also: Recno, MakeIdxKey.
    3507                 : */
    3508                 : case OP_IdxRecno: {
    3509              77 :   int i = pOp->p1;
    3510                 :   BtCursor *pCrsr;
    3511                 : 
    3512                 :   assert( i>=0 && i<p->nCursor );
    3513              77 :   pTos++;
    3514              77 :   if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
    3515                 :     int v;
    3516                 :     int sz;
    3517                 :     assert( p->aCsr[i].deferredMoveto==0 );
    3518              77 :     sqliteBtreeKeySize(pCrsr, &sz);
    3519              77 :     if( sz<sizeof(u32) ){
    3520               0 :       pTos->flags = MEM_Null;
    3521                 :     }else{
    3522              77 :       sqliteBtreeKey(pCrsr, sz - sizeof(u32), sizeof(u32), (char*)&v);
    3523              77 :       v = keyToInt(v);
    3524              77 :       pTos->i = v;
    3525              77 :       pTos->flags = MEM_Int;
    3526                 :     }
    3527                 :   }else{
    3528               0 :     pTos->flags = MEM_Null;
    3529                 :   }
    3530              77 :   break;
    3531                 : }
    3532                 : 
    3533                 : /* Opcode: IdxGT P1 P2 *
    3534                 : **
    3535                 : ** Compare the top of the stack against the key on the index entry that
    3536                 : ** cursor P1 is currently pointing to.  Ignore the last 4 bytes of the
    3537                 : ** index entry.  If the index entry is greater than the top of the stack
    3538                 : ** then jump to P2.  Otherwise fall through to the next instruction.
    3539                 : ** In either case, the stack is popped once.
    3540                 : */
    3541                 : /* Opcode: IdxGE P1 P2 *
    3542                 : **
    3543                 : ** Compare the top of the stack against the key on the index entry that
    3544                 : ** cursor P1 is currently pointing to.  Ignore the last 4 bytes of the
    3545                 : ** index entry.  If the index entry is greater than or equal to 
    3546                 : ** the top of the stack
    3547                 : ** then jump to P2.  Otherwise fall through to the next instruction.
    3548                 : ** In either case, the stack is popped once.
    3549                 : */
    3550                 : /* Opcode: IdxLT P1 P2 *
    3551                 : **
    3552                 : ** Compare the top of the stack against the key on the index entry that
    3553                 : ** cursor P1 is currently pointing to.  Ignore the last 4 bytes of the
    3554                 : ** index entry.  If the index entry is less than the top of the stack
    3555                 : ** then jump to P2.  Otherwise fall through to the next instruction.
    3556                 : ** In either case, the stack is popped once.
    3557                 : */
    3558                 : case OP_IdxLT:
    3559                 : case OP_IdxGT:
    3560                 : case OP_IdxGE: {
    3561              82 :   int i= pOp->p1;
    3562                 :   BtCursor *pCrsr;
    3563                 : 
    3564                 :   assert( i>=0 && i<p->nCursor );
    3565                 :   assert( pTos>=p->aStack );
    3566              82 :   if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
    3567                 :     int res, rc;
    3568                 :  
    3569              82 :     Stringify(pTos);
    3570                 :     assert( p->aCsr[i].deferredMoveto==0 );
    3571              82 :     rc = sqliteBtreeKeyCompare(pCrsr, pTos->z, pTos->n, 4, &res);
    3572              82 :     if( rc!=SQLITE_OK ){
    3573               0 :       break;
    3574                 :     }
    3575              82 :     if( pOp->opcode==OP_IdxLT ){
    3576               0 :       res = -res;
    3577              82 :     }else if( pOp->opcode==OP_IdxGE ){
    3578               0 :       res++;
    3579                 :     }
    3580              82 :     if( res>0 ){
    3581              34 :       pc = pOp->p2 - 1 ;
    3582                 :     }
    3583                 :   }
    3584              82 :   Release(pTos);
    3585              82 :   pTos--;
    3586              82 :   break;
    3587                 : }
    3588                 : 
    3589                 : /* Opcode: IdxIsNull P1 P2 *
    3590                 : **
    3591                 : ** The top of the stack contains an index entry such as might be generated
    3592                 : ** by the MakeIdxKey opcode.  This routine looks at the first P1 fields of
    3593                 : ** that key.  If any of the first P1 fields are NULL, then a jump is made
    3594                 : ** to address P2.  Otherwise we fall straight through.
    3595                 : **
    3596                 : ** The index entry is always popped from the stack.
    3597                 : */
    3598                 : case OP_IdxIsNull: {
    3599              77 :   int i = pOp->p1;
    3600                 :   int k, n;
    3601                 :   const char *z;
    3602                 : 
    3603                 :   assert( pTos>=p->aStack );
    3604                 :   assert( pTos->flags & MEM_Str );
    3605              77 :   z = pTos->z;
    3606              77 :   n = pTos->n;
    3607             125 :   for(k=0; k<n && i>0; i--){
    3608              48 :     if( z[k]=='a' ){
    3609               0 :       pc = pOp->p2-1;
    3610               0 :       break;
    3611                 :     }
    3612              48 :     while( k<n && z[k] ){ k++; }
    3613              48 :     k++;
    3614                 :   }
    3615              77 :   Release(pTos);
    3616              77 :   pTos--;
    3617              77 :   break;
    3618                 : }
    3619                 : 
    3620                 : /* Opcode: Destroy P1 P2 *
    3621                 : **
    3622                 : ** Delete an entire database table or index whose root page in the database
    3623                 : ** file is given by P1.
    3624                 : **
    3625                 : ** The table being destroyed is in the main database file if P2==0.  If
    3626                 : ** P2==1 then the table to be clear is in the auxiliary database file
    3627                 : ** that is used to store tables create using CREATE TEMPORARY TABLE.
    3628                 : **
    3629                 : ** See also: Clear
    3630                 : */
    3631                 : case OP_Destroy: {
    3632               1 :   rc = sqliteBtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1);
    3633               1 :   break;
    3634                 : }
    3635                 : 
    3636                 : /* Opcode: Clear P1 P2 *
    3637                 : **
    3638                 : ** Delete all contents of the database table or index whose root page
    3639                 : ** in the database file is given by P1.  But, unlike Destroy, do not
    3640                 : ** remove the table or index from the database file.
    3641                 : **
    3642                 : ** The table being clear is in the main database file if P2==0.  If
    3643                 : ** P2==1 then the table to be clear is in the auxiliary database file
    3644                 : ** that is used to store tables create using CREATE TEMPORARY TABLE.
    3645                 : **
    3646                 : ** See also: Destroy
    3647                 : */
    3648                 : case OP_Clear: {
    3649               4 :   rc = sqliteBtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1);
    3650               4 :   break;
    3651                 : }
    3652                 : 
    3653                 : /* Opcode: CreateTable * P2 P3
    3654                 : **
    3655                 : ** Allocate a new table in the main database file if P2==0 or in the
    3656                 : ** auxiliary database file if P2==1.  Push the page number
    3657                 : ** for the root page of the new table onto the stack.
    3658                 : **
    3659                 : ** The root page number is also written to a memory location that P3
    3660                 : ** points to.  This is the mechanism is used to write the root page
    3661                 : ** number into the parser's internal data structures that describe the
    3662                 : ** new table.
    3663                 : **
    3664                 : ** The difference between a table and an index is this:  A table must
    3665                 : ** have a 4-byte integer key and can have arbitrary data.  An index
    3666                 : ** has an arbitrary key but no data.
    3667                 : **
    3668                 : ** See also: CreateIndex
    3669                 : */
    3670                 : /* Opcode: CreateIndex * P2 P3
    3671                 : **
    3672                 : ** Allocate a new index in the main database file if P2==0 or in the
    3673                 : ** auxiliary database file if P2==1.  Push the page number of the
    3674                 : ** root page of the new index onto the stack.
    3675                 : **
    3676                 : ** See documentation on OP_CreateTable for additional information.
    3677                 : */
    3678                 : case OP_CreateIndex:
    3679                 : case OP_CreateTable: {
    3680                 :   int pgno;
    3681                 :   assert( pOp->p3!=0 && pOp->p3type==P3_POINTER );
    3682                 :   assert( pOp->p2>=0 && pOp->p2<db->nDb );
    3683                 :   assert( db->aDb[pOp->p2].pBt!=0 );
    3684             138 :   if( pOp->opcode==OP_CreateTable ){
    3685              95 :     rc = sqliteBtreeCreateTable(db->aDb[pOp->p2].pBt, &pgno);
    3686                 :   }else{
    3687              43 :     rc = sqliteBtreeCreateIndex(db->aDb[pOp->p2].pBt, &pgno);
    3688                 :   }
    3689             138 :   pTos++;
    3690             138 :   if( rc==SQLITE_OK ){
    3691             138 :     pTos->i = pgno;
    3692             138 :     pTos->flags = MEM_Int;
    3693             138 :     *(u32*)pOp->p3 = pgno;
    3694             138 :     pOp->p3 = 0;
    3695                 :   }else{
    3696               0 :     pTos->flags = MEM_Null;
    3697                 :   }
    3698             138 :   break;
    3699                 : }
    3700                 : 
    3701                 : /* Opcode: IntegrityCk P1 P2 *
    3702                 : **
    3703                 : ** Do an analysis of the currently open database.  Push onto the
    3704                 : ** stack the text of an error message describing any problems.
    3705                 : ** If there are no errors, push a "ok" onto the stack.
    3706                 : **
    3707                 : ** P1 is the index of a set that contains the root page numbers
    3708                 : ** for all tables and indices in the main database file.  The set
    3709                 : ** is cleared by this opcode.  In other words, after this opcode
    3710                 : ** has executed, the set will be empty.
    3711                 : **
    3712                 : ** If P2 is not zero, the check is done on the auxiliary database
    3713                 : ** file, not the main database file.
    3714                 : **
    3715                 : ** This opcode is used for testing purposes only.
    3716                 : */
    3717                 : case OP_IntegrityCk: {
    3718                 :   int nRoot;
    3719                 :   int *aRoot;
    3720               0 :   int iSet = pOp->p1;
    3721                 :   Set *pSet;
    3722                 :   int j;
    3723                 :   HashElem *i;
    3724                 :   char *z;
    3725                 : 
    3726                 :   assert( iSet>=0 && iSet<p->nSet );
    3727               0 :   pTos++;
    3728               0 :   pSet = &p->aSet[iSet];
    3729               0 :   nRoot = sqliteHashCount(&pSet->hash);
    3730               0 :   aRoot = sqliteMallocRaw( sizeof(int)*(nRoot+1) );
    3731               0 :   if( aRoot==0 ) goto no_mem;
    3732               0 :   for(j=0, i=sqliteHashFirst(&pSet->hash); i; i=sqliteHashNext(i), j++){
    3733               0 :     toInt((char*)sqliteHashKey(i), &aRoot[j]);
    3734                 :   }
    3735               0 :   aRoot[j] = 0;
    3736               0 :   sqliteHashClear(&pSet->hash);
    3737               0 :   pSet->prev = 0;
    3738               0 :   z = sqliteBtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot);
    3739               0 :   if( z==0 || z[0]==0 ){
    3740               0 :     if( z ) sqliteFree(z);
    3741               0 :     pTos->z = "ok";
    3742               0 :     pTos->n = 3;
    3743               0 :     pTos->flags = MEM_Str | MEM_Static;
    3744                 :   }else{
    3745               0 :     pTos->z = z;
    3746               0 :     pTos->n = strlen(z) + 1;
    3747               0 :     pTos->flags = MEM_Str | MEM_Dyn;
    3748                 :   }
    3749               0 :   sqliteFree(aRoot);
    3750               0 :   break;
    3751                 : }
    3752                 : 
    3753                 : /* Opcode: ListWrite * * *
    3754                 : **
    3755                 : ** Write the integer on the top of the stack
    3756                 : ** into the temporary storage list.
    3757                 : */
    3758                 : case OP_ListWrite: {
    3759                 :   Keylist *pKeylist;
    3760                 :   assert( pTos>=p->aStack );
    3761               5 :   pKeylist = p->pList;
    3762               5 :   if( pKeylist==0 || pKeylist->nUsed>=pKeylist->nKey ){
    3763               2 :     pKeylist = sqliteMallocRaw( sizeof(Keylist)+999*sizeof(pKeylist->aKey[0]) );
    3764               2 :     if( pKeylist==0 ) goto no_mem;
    3765               2 :     pKeylist->nKey = 1000;
    3766               2 :     pKeylist->nRead = 0;
    3767               2 :     pKeylist->nUsed = 0;
    3768               2 :     pKeylist->pNext = p->pList;
    3769               2 :     p->pList = pKeylist;
    3770                 :   }
    3771               5 :   Integerify(pTos);
    3772               5 :   pKeylist->aKey[pKeylist->nUsed++] = pTos->i;
    3773               5 :   Release(pTos);
    3774               5 :   pTos--;
    3775               5 :   break;
    3776                 : }
    3777                 : 
    3778                 : /* Opcode: ListRewind * * *
    3779                 : **
    3780                 : ** Rewind the temporary buffer back to the beginning.
    3781                 : */
    3782                 : case OP_ListRewind: {
    3783                 :   /* What this opcode codes, really, is reverse the order of the
    3784                 :   ** linked list of Keylist structures so that they are read out
    3785                 :   ** in the same order that they were read in. */
    3786                 :   Keylist *pRev, *pTop;
    3787               2 :   pRev = 0;
    3788               6 :   while( p->pList ){
    3789               2 :     pTop = p->pList;
    3790               2 :     p->pList = pTop->pNext;
    3791               2 :     pTop->pNext = pRev;
    3792               2 :     pRev = pTop;
    3793                 :   }
    3794               2 :   p->pList = pRev;
    3795               2 :   break;
    3796                 : }
    3797                 : 
    3798                 : /* Opcode: ListRead * P2 *
    3799                 : **
    3800                 : ** Attempt to read an integer from the temporary storage buffer
    3801                 : ** and push it onto the stack.  If the storage buffer is empty, 
    3802                 : ** push nothing but instead jump to P2.
    3803                 : */
    3804                 : case OP_ListRead: {
    3805                 :   Keylist *pKeylist;
    3806               7 :   CHECK_FOR_INTERRUPT;
    3807               7 :   pKeylist = p->pList;
    3808               7 :   if( pKeylist!=0 ){
    3809                 :     assert( pKeylist->nRead>=0 );
    3810                 :     assert( pKeylist->nRead<pKeylist->nUsed );
    3811                 :     assert( pKeylist->nRead<pKeylist->nKey );
    3812               5 :     pTos++;
    3813               5 :     pTos->i = pKeylist->aKey[pKeylist->nRead++];
    3814               5 :     pTos->flags = MEM_Int;
    3815               5 :     if( pKeylist->nRead>=pKeylist->nUsed ){
    3816               2 :       p->pList = pKeylist->pNext;
    3817               2 :       sqliteFree(pKeylist);
    3818                 :     }
    3819                 :   }else{
    3820               2 :     pc = pOp->p2 - 1;
    3821                 :   }
    3822               7 :   break;
    3823                 : }
    3824                 : 
    3825                 : /* Opcode: ListReset * * *
    3826                 : **
    3827                 : ** Reset the temporary storage buffer so that it holds nothing.
    3828                 : */
    3829                 : case OP_ListReset: {
    3830               2 :   if( p->pList ){
    3831               0 :     sqliteVdbeKeylistFree(p->pList);
    3832               0 :     p->pList = 0;
    3833                 :   }
    3834               2 :   break;
    3835                 : }
    3836                 : 
    3837                 : /* Opcode: ListPush * * * 
    3838                 : **
    3839                 : ** Save the current Vdbe list such that it can be restored by a ListPop
    3840                 : ** opcode. The list is empty after this is executed.
    3841                 : */
    3842                 : case OP_ListPush: {
    3843               0 :   p->keylistStackDepth++;
    3844                 :   assert(p->keylistStackDepth > 0);
    3845               0 :   p->keylistStack = sqliteRealloc(p->keylistStack, 
    3846                 :           sizeof(Keylist *) * p->keylistStackDepth);
    3847               0 :   if( p->keylistStack==0 ) goto no_mem;
    3848               0 :   p->keylistStack[p->keylistStackDepth - 1] = p->pList;
    3849               0 :   p->pList = 0;
    3850               0 :   break;
    3851                 : }
    3852                 : 
    3853                 : /* Opcode: ListPop * * * 
    3854                 : **
    3855                 : ** Restore the Vdbe list to the state it was in when ListPush was last
    3856                 : ** executed.
    3857                 : */
    3858                 : case OP_ListPop: {
    3859                 :   assert(p->keylistStackDepth > 0);
    3860               0 :   p->keylistStackDepth--;
    3861               0 :   sqliteVdbeKeylistFree(p->pList);
    3862               0 :   p->pList = p->keylistStack[p->keylistStackDepth];
    3863               0 :   p->keylistStack[p->keylistStackDepth] = 0;
    3864               0 :   if( p->keylistStackDepth == 0 ){
    3865               0 :     sqliteFree(p->keylistStack);
    3866               0 :     p->keylistStack = 0;
    3867                 :   }
    3868               0 :   break;
    3869                 : }
    3870                 : 
    3871                 : /* Opcode: ContextPush * * * 
    3872                 : **
    3873                 : ** Save the current Vdbe context such that it can be restored by a ContextPop
    3874                 : ** opcode. The context stores the last insert row id, the last statement change
    3875                 : ** count, and the current statement change count.
    3876                 : */
    3877                 : case OP_ContextPush: {
    3878               0 :   p->contextStackDepth++;
    3879                 :   assert(p->contextStackDepth > 0);
    3880               0 :   p->contextStack = sqliteRealloc(p->contextStack, 
    3881                 :           sizeof(Context) * p->contextStackDepth);
    3882               0 :   if( p->contextStack==0 ) goto no_mem;
    3883               0 :   p->contextStack[p->contextStackDepth - 1].lastRowid = p->db->lastRowid;
    3884               0 :   p->contextStack[p->contextStackDepth - 1].lsChange = p->db->lsChange;
    3885               0 :   p->contextStack[p->contextStackDepth - 1].csChange = p->db->csChange;
    3886               0 :   break;
    3887                 : }
    3888                 : 
    3889                 : /* Opcode: ContextPop * * * 
    3890                 : **
    3891                 : ** Restore the Vdbe context to the state it was in when contextPush was last
    3892                 : ** executed. The context stores the last insert row id, the last statement
    3893                 : ** change count, and the current statement change count.
    3894                 : */
    3895                 : case OP_ContextPop: {
    3896                 :   assert(p->contextStackDepth > 0);
    3897               0 :   p->contextStackDepth--;
    3898               0 :   p->db->lastRowid = p->contextStack[p->contextStackDepth].lastRowid;
    3899               0 :   p->db->lsChange = p->contextStack[p->contextStackDepth].lsChange;
    3900               0 :   p->db->csChange = p->contextStack[p->contextStackDepth].csChange;
    3901               0 :   if( p->contextStackDepth == 0 ){
    3902               0 :     sqliteFree(p->contextStack);
    3903               0 :     p->contextStack = 0;
    3904                 :   }
    3905               0 :   break;
    3906                 : }
    3907                 : 
    3908                 : /* Opcode: SortPut * * *
    3909                 : **
    3910                 : ** The TOS is the key and the NOS is the data.  Pop both from the stack
    3911                 : ** and put them on the sorter.  The key and data should have been
    3912                 : ** made using SortMakeKey and SortMakeRec, respectively.
    3913                 : */
    3914                 : case OP_SortPut: {
    3915               0 :   Mem *pNos = &pTos[-1];
    3916                 :   Sorter *pSorter;
    3917                 :   assert( pNos>=p->aStack );
    3918               0 :   if( Dynamicify(pTos) || Dynamicify(pNos) ) goto no_mem;
    3919               0 :   pSorter = sqliteMallocRaw( sizeof(Sorter) );
    3920               0 :   if( pSorter==0 ) goto no_mem;
    3921               0 :   pSorter->pNext = p->pSort;
    3922               0 :   p->pSort = pSorter;
    3923                 :   assert( pTos->flags & MEM_Dyn );
    3924               0 :   pSorter->nKey = pTos->n;
    3925               0 :   pSorter->zKey = pTos->z;
    3926                 :   assert( pNos->flags & MEM_Dyn );
    3927               0 :   pSorter->nData = pNos->n;
    3928               0 :   pSorter->pData = pNos->z;
    3929               0 :   pTos -= 2;
    3930               0 :   break;
    3931                 : }
    3932                 : 
    3933                 : /* Opcode: SortMakeRec P1 * *
    3934                 : **
    3935                 : ** The top P1 elements are the arguments to a callback.  Form these
    3936                 : ** elements into a single data entry that can be stored on a sorter
    3937                 : ** using SortPut and later fed to a callback using SortCallback.
    3938                 : */
    3939                 : case OP_SortMakeRec: {
    3940                 :   char *z;
    3941                 :   char **azArg;
    3942                 :   int nByte;
    3943                 :   int nField;
    3944                 :   int i;
    3945                 :   Mem *pRec;
    3946                 : 
    3947               0 :   nField = pOp->p1;
    3948               0 :   pRec = &pTos[1-nField];
    3949                 :   assert( pRec>=p->aStack );
    3950               0 :   nByte = 0;
    3951               0 :   for(i=0; i<nField; i++, pRec++){
    3952               0 :     if( (pRec->flags & MEM_Null)==0 ){
    3953               0 :       Stringify(pRec);
    3954               0 :       nByte += pRec->n;
    3955                 :     }
    3956                 :   }
    3957               0 :   nByte += sizeof(char*)*(nField+1);
    3958               0 :   azArg = sqliteMallocRaw( nByte );
    3959               0 :   if( azArg==0 ) goto no_mem;
    3960               0 :   z = (char*)&azArg[nField+1];
    3961               0 :   for(pRec=&pTos[1-nField], i=0; i<nField; i++, pRec++){
    3962               0 :     if( pRec->flags & MEM_Null ){
    3963               0 :       azArg[i] = 0;
    3964                 :     }else{
    3965               0 :       azArg[i] = z;
    3966               0 :       memcpy(z, pRec->z, pRec->n);
    3967               0 :       z += pRec->n;
    3968                 :     }
    3969                 :   }
    3970               0 :   popStack(&pTos, nField);
    3971               0 :   pTos++;
    3972               0 :   pTos->n = nByte;
    3973               0 :   pTos->z = (char*)azArg;
    3974               0 :   pTos->flags = MEM_Str | MEM_Dyn;
    3975               0 :   break;
    3976                 : }
    3977                 : 
    3978                 : /* Opcode: SortMakeKey * * P3
    3979                 : **
    3980                 : ** Convert the top few entries of the stack into a sort key.  The
    3981                 : ** number of stack entries consumed is the number of characters in 
    3982                 : ** the string P3.  One character from P3 is prepended to each entry.
    3983                 : ** The first character of P3 is prepended to the element lowest in
    3984                 : ** the stack and the last character of P3 is prepended to the top of
    3985                 : ** the stack.  All stack entries are separated by a \000 character
    3986                 : ** in the result.  The whole key is terminated by two \000 characters
    3987                 : ** in a row.
    3988                 : **
    3989                 : ** "N" is substituted in place of the P3 character for NULL values.
    3990                 : **
    3991                 : ** See also the MakeKey and MakeIdxKey opcodes.
    3992                 : */
    3993                 : case OP_SortMakeKey: {
    3994                 :   char *zNewKey;
    3995                 :   int nByte;
    3996                 :   int nField;
    3997                 :   int i, j, k;
    3998                 :   Mem *pRec;
    3999                 : 
    4000               0 :   nField = strlen(pOp->p3);
    4001               0 :   pRec = &pTos[1-nField];
    4002               0 :   nByte = 1;
    4003               0 :   for(i=0; i<nField; i++, pRec++){
    4004               0 :     if( pRec->flags & MEM_Null ){
    4005               0 :       nByte += 2;
    4006                 :     }else{
    4007               0 :       Stringify(pRec);
    4008               0 :       nByte += pRec->n+2;
    4009                 :     }
    4010                 :   }
    4011               0 :   zNewKey = sqliteMallocRaw( nByte );
    4012               0 :   if( zNewKey==0 ) goto no_mem;
    4013               0 :   j = 0;
    4014               0 :   k = 0;
    4015               0 :   for(pRec=&pTos[1-nField], i=0; i<nField; i++, pRec++){
    4016               0 :     if( pRec->flags & MEM_Null ){
    4017               0 :       zNewKey[j++] = 'N';
    4018               0 :       zNewKey[j++] = 0;
    4019               0 :       k++;
    4020                 :     }else{
    4021               0 :       zNewKey[j++] = pOp->p3[k++];
    4022               0 :       memcpy(&zNewKey[j], pRec->z, pRec->n-1);
    4023               0 :       j += pRec->n-1;
    4024               0 :       zNewKey[j++] = 0;
    4025                 :     }
    4026                 :   }
    4027               0 :   zNewKey[j] = 0;
    4028                 :   assert( j<nByte );
    4029               0 :   popStack(&pTos, nField);
    4030               0 :   pTos++;
    4031               0 :   pTos->n = nByte;
    4032               0 :   pTos->flags = MEM_Str|MEM_Dyn;
    4033               0 :   pTos->z = zNewKey;
    4034               0 :   break;
    4035                 : }
    4036                 : 
    4037                 : /* Opcode: Sort * * *
    4038                 : **
    4039                 : ** Sort all elements on the sorter.  The algorithm is a
    4040                 : ** mergesort.
    4041                 : */
    4042                 : case OP_Sort: {
    4043                 :   int i;
    4044                 :   Sorter *pElem;
    4045                 :   Sorter *apSorter[NSORT];
    4046               0 :   for(i=0; i<NSORT; i++){
    4047               0 :     apSorter[i] = 0;
    4048                 :   }
    4049               0 :   while( p->pSort ){
    4050               0 :     pElem = p->pSort;
    4051               0 :     p->pSort = pElem->pNext;
    4052               0 :     pElem->pNext = 0;
    4053               0 :     for(i=0; i<NSORT-1; i++){
    4054               0 :     if( apSorter[i]==0 ){
    4055               0 :         apSorter[i] = pElem;
    4056               0 :         break;
    4057                 :       }else{
    4058               0 :         pElem = Merge(apSorter[i], pElem);
    4059               0 :         apSorter[i] = 0;
    4060                 :       }
    4061                 :     }
    4062               0 :     if( i>=NSORT-1 ){
    4063               0 :       apSorter[NSORT-1] = Merge(apSorter[NSORT-1],pElem);
    4064                 :     }
    4065                 :   }
    4066               0 :   pElem = 0;
    4067               0 :   for(i=0; i<NSORT; i++){
    4068               0 :     pElem = Merge(apSorter[i], pElem);
    4069                 :   }
    4070               0 :   p->pSort = pElem;
    4071               0 :   break;
    4072                 : }
    4073                 : 
    4074                 : /* Opcode: SortNext * P2 *
    4075                 : **
    4076                 : ** Push the data for the topmost element in the sorter onto the
    4077                 : ** stack, then remove the element from the sorter.  If the sorter
    4078                 : ** is empty, push nothing on the stack and instead jump immediately 
    4079                 : ** to instruction P2.
    4080                 : */
    4081                 : case OP_SortNext: {
    4082               0 :   Sorter *pSorter = p->pSort;
    4083               0 :   CHECK_FOR_INTERRUPT;
    4084               0 :   if( pSorter!=0 ){
    4085               0 :     p->pSort = pSorter->pNext;
    4086               0 :     pTos++;
    4087               0 :     pTos->z = pSorter->pData;
    4088               0 :     pTos->n = pSorter->nData;
    4089               0 :     pTos->flags = MEM_Str|MEM_Dyn;
    4090               0 :     sqliteFree(pSorter->zKey);
    4091               0 :     sqliteFree(pSorter);
    4092                 :   }else{
    4093               0 :     pc = pOp->p2 - 1;
    4094                 :   }
    4095               0 :   break;
    4096                 : }
    4097                 : 
    4098                 : /* Opcode: SortCallback P1 * *
    4099                 : **
    4100                 : ** The top of the stack contains a callback record built using
    4101                 : ** the SortMakeRec operation with the same P1 value as this
    4102                 : ** instruction.  Pop this record from the stack and invoke the
    4103                 : ** callback on it.
    4104                 : */
    4105                 : case OP_SortCallback: {
    4106                 :   assert( pTos>=p->aStack );
    4107                 :   assert( pTos->flags & MEM_Str );
    4108               0 :   p->nCallback++;
    4109               0 :   p->pc = pc+1;
    4110               0 :   p->azResColumn = (char**)pTos->z;
    4111                 :   assert( p->nResColumn==pOp->p1 );
    4112               0 :   p->popStack = 1;
    4113               0 :   p->pTos = pTos;
    4114               0 :   return SQLITE_ROW;
    4115                 : }
    4116                 : 
    4117                 : /* Opcode: SortReset * * *
    4118                 : **
    4119                 : ** Remove any elements that remain on the sorter.
    4120                 : */
    4121                 : case OP_SortReset: {
    4122               0 :   sqliteVdbeSorterReset(p);
    4123               0 :   break;
    4124                 : }
    4125                 : 
    4126                 : /* Opcode: FileOpen * * P3
    4127                 : **
    4128                 : ** Open the file named by P3 for reading using the FileRead opcode.
    4129                 : ** If P3 is "stdin" then open standard input for reading.
    4130                 : */
    4131                 : case OP_FileOpen: {
    4132                 :   assert( pOp->p3!=0 );
    4133               0 :   if( p->pFile ){
    4134               0 :     if( p->pFile!=stdin ) fclose(p->pFile);
    4135               0 :     p->pFile = 0;
    4136                 :   }
    4137               0 :   if( sqliteStrICmp(pOp->p3,"stdin")==0 ){
    4138               0 :     p->pFile = stdin;
    4139                 :   }else{
    4140               0 :     p->pFile = fopen(pOp->p3, "r");
    4141                 :   }
    4142               0 :   if( p->pFile==0 ){
    4143               0 :     sqliteSetString(&p->zErrMsg,"unable to open file: ", pOp->p3, (char*)0);
    4144               0 :     rc = SQLITE_ERROR;
    4145                 :   }
    4146               0 :   break;
    4147                 : }
    4148                 : 
    4149                 : /* Opcode: FileRead P1 P2 P3
    4150                 : **
    4151                 : ** Read a single line of input from the open file (the file opened using
    4152                 : ** FileOpen).  If we reach end-of-file, jump immediately to P2.  If
    4153                 : ** we are able to get another line, split the line apart using P3 as
    4154                 : ** a delimiter.  There should be P1 fields.  If the input line contains
    4155                 : ** more than P1 fields, ignore the excess.  If the input line contains
    4156                 : ** fewer than P1 fields, assume the remaining fields contain NULLs.
    4157                 : **
    4158                 : ** Input ends if a line consists of just "\.".  A field containing only
    4159                 : ** "\N" is a null field.  The backslash \ character can be used be used
    4160                 : ** to escape newlines or the delimiter.
    4161                 : */
    4162                 : case OP_FileRead: {
    4163                 :   int n, eol, nField, i, c, nDelim;
    4164                 :   char *zDelim, *z;
    4165               0 :   CHECK_FOR_INTERRUPT;
    4166               0 :   if( p->pFile==0 ) goto fileread_jump;
    4167               0 :   nField = pOp->p1;
    4168               0 :   if( nField<=0 ) goto fileread_jump;
    4169               0 :   if( nField!=p->nField || p->azField==0 ){
    4170               0 :     char **azField = sqliteRealloc(p->azField, sizeof(char*)*nField+1);
    4171               0 :     if( azField==0 ){ goto no_mem; }
    4172               0 :     p->azField = azField;
    4173               0 :     p->nField = nField;
    4174                 :   }
    4175               0 :   n = 0;
    4176               0 :   eol = 0;
    4177               0 :   while( eol==0 ){
    4178               0 :     if( p->zLine==0 || n+200>p->nLineAlloc ){
    4179                 :       char *zLine;
    4180               0 :       p->nLineAlloc = p->nLineAlloc*2 + 300;
    4181               0 :       zLine = sqliteRealloc(p->zLine, p->nLineAlloc);
    4182               0 :       if( zLine==0 ){
    4183               0 :         p->nLineAlloc = 0;
    4184               0 :         sqliteFree(p->zLine);
    4185               0 :         p->zLine = 0;
    4186               0 :         goto no_mem;
    4187                 :       }
    4188               0 :       p->zLine = zLine;
    4189                 :     }
    4190               0 :     if( vdbe_fgets(&p->zLine[n], p->nLineAlloc-n, p->pFile)==0 ){
    4191               0 :       eol = 1;
    4192               0 :       p->zLine[n] = 0;
    4193                 :     }else{
    4194                 :       int c;
    4195               0 :       while( (c = p->zLine[n])!=0 ){
    4196               0 :         if( c=='\\' ){
    4197               0 :           if( p->zLine[n+1]==0 ) break;
    4198               0 :           n += 2;
    4199               0 :         }else if( c=='\n' ){
    4200               0 :           p->zLine[n] = 0;
    4201               0 :           eol = 1;
    4202               0 :           break;
    4203                 :         }else{
    4204               0 :           n++;
    4205                 :         }
    4206                 :       }
    4207                 :     }
    4208                 :   }
    4209               0 :   if( n==0 ) goto fileread_jump;
    4210               0 :   z = p->zLine;
    4211               0 :   if( z[0]=='\\' && z[1]=='.' && z[2]==0 ){
    4212               0 :     goto fileread_jump;
    4213                 :   }
    4214               0 :   zDelim = pOp->p3;
    4215               0 :   if( zDelim==0 ) zDelim = "\t";
    4216               0 :   c = zDelim[0];
    4217               0 :   nDelim = strlen(zDelim);
    4218               0 :   p->azField[0] = z;
    4219               0 :   for(i=1; *z!=0 && i<=nField; i++){
    4220                 :     int from, to;
    4221               0 :     from = to = 0;
    4222               0 :     if( z[0]=='\\' && z[1]=='N' 
    4223                 :        && (z[2]==0 || strncmp(&z[2],zDelim,nDelim)==0) ){
    4224               0 :       if( i<=nField ) p->azField[i-1] = 0;
    4225               0 :       z += 2 + nDelim;
    4226               0 :       if( i<nField ) p->azField[i] = z;
    4227               0 :       continue;
    4228                 :     }
    4229               0 :     while( z[from] ){
    4230               0 :       if( z[from]=='\\' && z[from+1]!=0 ){
    4231               0 :         int tx = z[from+1];
    4232               0 :         switch( tx ){
    4233               0 :           case 'b':  tx = '\b'; break;
    4234               0 :           case 'f':  tx = '\f'; break;
    4235               0 :           case 'n':  tx = '\n'; break;
    4236               0 :           case 'r':  tx = '\r'; break;
    4237               0 :           case 't':  tx = '\t'; break;
    4238               0 :           case 'v':  tx = '\v'; break;
    4239                 :           default:   break;
    4240                 :         }
    4241               0 :         z[to++] = tx;
    4242               0 :         from += 2;
    4243               0 :         continue;
    4244                 :       }
    4245               0 :       if( z[from]==c && strncmp(&z[from],zDelim,nDelim)==0 ) break;
    4246               0 :       z[to++] = z[from++];
    4247                 :     }
    4248               0 :     if( z[from] ){
    4249               0 :       z[to] = 0;
    4250               0 :       z += from + nDelim;
    4251               0 :       if( i<nField ) p->azField[i] = z;
    4252                 :     }else{
    4253               0 :       z[to] = 0;
    4254               0 :       z = "";
    4255                 :     }
    4256                 :   }
    4257               0 :   while( i<nField ){
    4258               0 :     p->azField[i++] = 0;
    4259                 :   }
    4260               0 :   break;
    4261                 : 
    4262                 :   /* If we reach end-of-file, or if anything goes wrong, jump here.
    4263                 :   ** This code will cause a jump to P2 */
    4264               0 : fileread_jump:
    4265               0 :   pc = pOp->p2 - 1;
    4266               0 :   break;
    4267                 : }
    4268                 : 
    4269                 : /* Opcode: FileColumn P1 * *
    4270                 : **
    4271                 : ** Push onto the stack the P1-th column of the most recently read line
    4272                 : ** from the input file.
    4273                 : */
    4274                 : case OP_FileColumn: {
    4275               0 :   int i = pOp->p1;
    4276                 :   char *z;
    4277                 :   assert( i>=0 && i<p->nField );
    4278               0 :   if( p->azField ){
    4279               0 :     z = p->azField[i];
    4280                 :   }else{
    4281               0 :     z = 0;
    4282                 :   }
    4283               0 :   pTos++;
    4284               0 :   if( z ){
    4285               0 :     pTos->n = strlen(z) + 1;
    4286               0 :     pTos->z = z;
    4287               0 :     pTos->flags = MEM_Str | MEM_Ephem;
    4288                 :   }else{
    4289               0 :     pTos->flags = MEM_Null;
    4290                 :   }
    4291               0 :   break;
    4292                 : }
    4293                 : 
    4294                 : /* Opcode: MemStore P1 P2 *
    4295                 : **
    4296                 : ** Write the top of the stack into memory location P1.
    4297                 : ** P1 should be a small integer since space is allocated
    4298                 : ** for all memory locations between 0 and P1 inclusive.
    4299                 : **
    4300                 : ** After the data is stored in the memory location, the
    4301                 : ** stack is popped once if P2 is 1.  If P2 is zero, then
    4302                 : ** the original data remains on the stack.
    4303                 : */
    4304                 : case OP_MemStore: {
    4305             120 :   int i = pOp->p1;
    4306                 :   Mem *pMem;
    4307                 :   assert( pTos>=p->aStack );
    4308             120 :   if( i>=p->nMem ){
    4309              44 :     int nOld = p->nMem;
    4310                 :     Mem *aMem;
    4311              44 :     p->nMem = i + 5;
    4312              44 :     aMem = sqliteRealloc(p->aMem, p->nMem*sizeof(p->aMem[0]));
    4313              44 :     if( aMem==0 ) goto no_mem;
    4314              44 :     if( aMem!=p->aMem ){
    4315                 :       int j;
    4316              44 :       for(j=0; j<nOld; j++){
    4317               0 :         if( aMem[j].flags & MEM_Short ){
    4318               0 :           aMem[j].z = aMem[j].zShort;
    4319                 :         }
    4320                 :       }
    4321                 :     }
    4322              44 :     p->aMem = aMem;
    4323              44 :     if( nOld<p->nMem ){
    4324              44 :       memset(&p->aMem[nOld], 0, sizeof(p->aMem[0])*(p->nMem-nOld));
    4325                 :     }
    4326                 :   }
    4327             120 :   Deephemeralize(pTos);
    4328             120 :   pMem = &p->aMem[i];
    4329             120 :   Release(pMem);
    4330             120 :   *pMem = *pTos;
    4331             120 :   if( pMem->flags & MEM_Dyn ){
    4332               0 :     if( pOp->p2 ){
    4333               0 :       pTos->flags = MEM_Null;
    4334                 :     }else{
    4335               0 :       pMem->z = sqliteMallocRaw( pMem->n );
    4336               0 :       if( pMem->z==0 ) goto no_mem;
    4337               0 :       memcpy(pMem->z, pTos->z, pMem->n);
    4338                 :     }
    4339             120 :   }else if( pMem->flags & MEM_Short ){
    4340              57 :     pMem->z = pMem->zShort;
    4341                 :   }
    4342             120 :   if( pOp->p2 ){
    4343              63 :     Release(pTos);
    4344              63 :     pTos--;
    4345                 :   }
    4346             120 :   break;
    4347                 : }
    4348                 : 
    4349                 : /* Opcode: MemLoad P1 * *
    4350                 : **
    4351                 : ** Push a copy of the value in memory location P1 onto the stack.
    4352                 : **
    4353                 : ** If the value is a string, then the value pushed is a pointer to
    4354                 : ** the string that is stored in the memory location.  If the memory
    4355                 : ** location is subsequently changed (using OP_MemStore) then the
    4356                 : ** value pushed onto the stack will change too.
    4357                 : */
    4358                 : case OP_MemLoad: {
    4359             128 :   int i = pOp->p1;
    4360                 :   assert( i>=0 && i<p->nMem );
    4361             128 :   pTos++;
    4362             128 :   memcpy(pTos, &p->aMem[i], sizeof(pTos[0])-NBFS);;
    4363             128 :   if( pTos->flags & MEM_Str ){
    4364              93 :     pTos->flags |= MEM_Ephem;
    4365              93 :     pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short);
    4366                 :   }
    4367             128 :   break;
    4368                 : }
    4369                 : 
    4370                 : /* Opcode: MemIncr P1 P2 *
    4371                 : **
    4372                 : ** Increment the integer valued memory cell P1 by 1.  If P2 is not zero
    4373                 : ** and the result after the increment is greater than zero, then jump
    4374                 : ** to P2.
    4375                 : **
    4376                 : ** This instruction throws an error if the memory cell is not initially
    4377                 : ** an integer.
    4378                 : */
    4379                 : case OP_MemIncr: {
    4380               2 :   int i = pOp->p1;
    4381                 :   Mem *pMem;
    4382                 :   assert( i>=0 && i<p->nMem );
    4383               2 :   pMem = &p->aMem[i];
    4384                 :   assert( pMem->flags==MEM_Int );
    4385               2 :   pMem->i++;
    4386               2 :   if( pOp->p2>0 && pMem->i>0 ){
    4387               0 :      pc = pOp->p2 - 1;
    4388                 :   }
    4389               2 :   break;
    4390                 : }
    4391                 : 
    4392                 : /* Opcode: AggReset * P2 *
    4393                 : **
    4394                 : ** Reset the aggregator so that it no longer contains any data.
    4395                 : ** Future aggregator elements will contain P2 values each.
    4396                 : */
    4397                 : case OP_AggReset: {
    4398              14 :   sqliteVdbeAggReset(&p->agg);
    4399              14 :   p->agg.nMem = pOp->p2;
    4400              14 :   p->agg.apFunc = sqliteMalloc( p->agg.nMem*sizeof(p->agg.apFunc[0]) );
    4401              14 :   if( p->agg.apFunc==0 ) goto no_mem;
    4402              14 :   break;
    4403                 : }
    4404                 : 
    4405                 : /* Opcode: AggInit * P2 P3
    4406                 : **
    4407                 : ** Initialize the function parameters for an aggregate function.
    4408                 : ** The aggregate will operate out of aggregate column P2.
    4409                 : ** P3 is a pointer to the FuncDef structure for the function.
    4410                 : */
    4411                 : case OP_AggInit: {
    4412              14 :   int i = pOp->p2;
    4413                 :   assert( i>=0 && i<p->agg.nMem );
    4414              14 :   p->agg.apFunc[i] = (FuncDef*)pOp->p3;
    4415              14 :   break;
    4416                 : }
    4417                 : 
    4418                 : /* Opcode: AggFunc * P2 P3
    4419                 : **
    4420                 : ** Execute the step function for an aggregate.  The
    4421                 : ** function has P2 arguments.  P3 is a pointer to the FuncDef
    4422                 : ** structure that specifies the function.
    4423                 : **
    4424                 : ** The top of the stack must be an integer which is the index of
    4425                 : ** the aggregate column that corresponds to this aggregate function.
    4426                 : ** Ideally, this index would be another parameter, but there are
    4427                 : ** no free parameters left.  The integer is popped from the stack.
    4428                 : */
    4429                 : case OP_AggFunc: {
    4430              46 :   int n = pOp->p2;
    4431                 :   int i;
    4432                 :   Mem *pMem, *pRec;
    4433              46 :   char **azArgv = p->zArgv;
    4434                 :   sqlite_func ctx;
    4435                 : 
    4436                 :   assert( n>=0 );
    4437                 :   assert( pTos->flags==MEM_Int );
    4438              46 :   pRec = &pTos[-n];
    4439                 :   assert( pRec>=p->aStack );
    4440              73 :   for(i=0; i<n; i++, pRec++){
    4441              27 :     if( pRec->flags & MEM_Null ){
    4442               0 :       azArgv[i] = 0;
    4443                 :     }else{
    4444              27 :       Stringify(pRec);
    4445              27 :       azArgv[i] = pRec->z;
    4446                 :     }
    4447                 :   }
    4448              46 :   i = pTos->i;
    4449                 :   assert( i>=0 && i<p->agg.nMem );
    4450              46 :   ctx.pFunc = (FuncDef*)pOp->p3;
    4451              46 :   pMem = &p->agg.pCurrent->aMem[i];
    4452              46 :   ctx.s.z = pMem->zShort;  /* Space used for small aggregate contexts */
    4453              46 :   ctx.pAgg = pMem->z;
    4454              46 :   ctx.cnt = ++pMem->i;
    4455              46 :   ctx.isError = 0;
    4456              46 :   ctx.isStep = 1;
    4457              46 :   (ctx.pFunc->xStep)(&ctx, n, (const char**)azArgv);
    4458              46 :   pMem->z = ctx.pAgg;
    4459              46 :   pMem->flags = MEM_AggCtx;
    4460              46 :   popStack(&pTos, n+1);
    4461              46 :   if( ctx.isError ){
    4462               0 :     rc = SQLITE_ERROR;
    4463                 :   }
    4464              46 :   break;
    4465                 : }
    4466                 : 
    4467                 : /* Opcode: AggFocus * P2 *
    4468                 : **
    4469                 : ** Pop the top of the stack and use that as an aggregator key.  If
    4470                 : ** an aggregator with that same key already exists, then make the
    4471                 : ** aggregator the current aggregator and jump to P2.  If no aggregator
    4472                 : ** with the given key exists, create one and make it current but
    4473                 : ** do not jump.
    4474                 : **
    4475                 : ** The order of aggregator opcodes is important.  The order is:
    4476                 : ** AggReset AggFocus AggNext.  In other words, you must execute
    4477                 : ** AggReset first, then zero or more AggFocus operations, then
    4478                 : ** zero or more AggNext operations.  You must not execute an AggFocus
    4479                 : ** in between an AggNext and an AggReset.
    4480                 : */
    4481                 : case OP_AggFocus: {
    4482                 :   AggElem *pElem;
    4483                 :   char *zKey;
    4484                 :   int nKey;
    4485                 : 
    4486                 :   assert( pTos>=p->aStack );
    4487              14 :   Stringify(pTos);
    4488              14 :   zKey = pTos->z;
    4489              14 :   nKey = pTos->n;
    4490              14 :   pElem = sqliteHashFind(&p->agg.hash, zKey, nKey);
    4491              14 :   if( pElem ){
    4492               0 :     p->agg.pCurrent = pElem;
    4493               0 :     pc = pOp->p2 - 1;
    4494                 :   }else{
    4495              14 :     AggInsert(&p->agg, zKey, nKey);
    4496              14 :     if( sqlite_malloc_failed ) goto no_mem;
    4497                 :   }
    4498              14 :   Release(pTos);
    4499              14 :   pTos--;
    4500              14 :   break; 
    4501                 : }
    4502                 : 
    4503                 : /* Opcode: AggSet * P2 *
    4504                 : **
    4505                 : ** Move the top of the stack into the P2-th field of the current
    4506                 : ** aggregate.  String values are duplicated into new memory.
    4507                 : */
    4508                 : case OP_AggSet: {
    4509               0 :   AggElem *pFocus = AggInFocus(p->agg);
    4510                 :   Mem *pMem;
    4511               0 :   int i = pOp->p2;
    4512                 :   assert( pTos>=p->aStack );
    4513               0 :   if( pFocus==0 ) goto no_mem;
    4514                 :   assert( i>=0 && i<p->agg.nMem );
    4515               0 :   Deephemeralize(pTos);
    4516               0 :   pMem = &pFocus->aMem[i];
    4517               0 :   Release(pMem);
    4518               0 :   *pMem = *pTos;
    4519               0 :   if( pMem->flags & MEM_Dyn ){
    4520               0 :     pTos->flags = MEM_Null;
    4521               0 :   }else if( pMem->flags & MEM_Short ){
    4522               0 :     pMem->z = pMem->zShort;
    4523                 :   }
    4524               0 :   Release(pTos);
    4525               0 :   pTos--;
    4526               0 :   break;
    4527                 : }
    4528                 : 
    4529                 : /* Opcode: AggGet * P2 *
    4530                 : **
    4531                 : ** Push a new entry onto the stack which is a copy of the P2-th field
    4532                 : ** of the current aggregate.  Strings are not duplicated so
    4533                 : ** string values will be ephemeral.
    4534                 : */
    4535                 : case OP_AggGet: {
    4536              14 :   AggElem *pFocus = AggInFocus(p->agg);
    4537                 :   Mem *pMem;
    4538              14 :   int i = pOp->p2;
    4539              14 :   if( pFocus==0 ) goto no_mem;
    4540                 :   assert( i>=0 && i<p->agg.nMem );
    4541              14 :   pTos++;
    4542              14 :   pMem = &pFocus->aMem[i];
    4543              14 :   *pTos = *pMem;
    4544              14 :   if( pTos->flags & MEM_Str ){
    4545               1 :     pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short);
    4546               1 :     pTos->flags |= MEM_Ephem;
    4547                 :   }
    4548              14 :   if( pTos->flags & MEM_AggCtx ){
    4549               0 :     Release(pTos);
    4550               0 :     pTos->flags = MEM_Null;
    4551                 :   }
    4552              14 :   break;
    4553                 : }
    4554                 : 
    4555                 : /* Opcode: AggNext * P2 *
    4556                 : **
    4557                 : ** Make the next aggregate value the current aggregate.  The prior
    4558                 : ** aggregate is deleted.  If all aggregate values have been consumed,
    4559                 : ** jump to P2.
    4560                 : **
    4561                 : ** The order of aggregator opcodes is important.  The order is:
    4562                 : ** AggReset AggFocus AggNext.  In other words, you must execute
    4563                 : ** AggReset first, then zero or more AggFocus operations, then
    4564                 : ** zero or more AggNext operations.  You must not execute an AggFocus
    4565                 : ** in between an AggNext and an AggReset.
    4566                 : */
    4567                 : case OP_AggNext: {
    4568              16 :   CHECK_FOR_INTERRUPT;
    4569              16 :   if( p->agg.pSearch==0 ){
    4570              14 :     p->agg.pSearch = sqliteHashFirst(&p->agg.hash);
    4571                 :   }else{
    4572               2 :     p->agg.pSearch = sqliteHashNext(p->agg.pSearch);
    4573                 :   }
    4574              16 :   if( p->agg.pSearch==0 ){
    4575               2 :     pc = pOp->p2 - 1;
    4576                 :   } else {
    4577                 :     int i;
    4578                 :     sqlite_func ctx;
    4579                 :     Mem *aMem;
    4580              14 :     p->agg.pCurrent = sqliteHashData(p->agg.pSearch);
    4581              14 :     aMem = p->agg.pCurrent->aMem;
    4582              28 :     for(i=0; i<p->agg.nMem; i++){
    4583                 :       int freeCtx;
    4584              14 :       if( p->agg.apFunc[i]==0 ) continue;
    4585              14 :       if( p->agg.apFunc[i]->xFinalize==0 ) continue;
    4586              14 :       ctx.s.flags = MEM_Null;
    4587              14 :       ctx.s.z = aMem[i].zShort;
    4588              14 :       ctx.pAgg = (void*)aMem[i].z;
    4589              14 :       freeCtx = aMem[i].z && aMem[i].z!=aMem[i].zShort;
    4590              14 :       ctx.cnt = aMem[i].i;
    4591              14 :       ctx.isStep = 0;
    4592              14 :       ctx.pFunc = p->agg.apFunc[i];
    4593              14 :       (*p->agg.apFunc[i]->xFinalize)(&ctx);
    4594              14 :       if( freeCtx ){
    4595               0 :         sqliteFree( aMem[i].z );
    4596                 :       }
    4597              14 :       aMem[i] = ctx.s;
    4598              14 :       if( aMem[i].flags & MEM_Short ){
    4599               1 :         aMem[i].z = aMem[i].zShort;
    4600                 :       }
    4601                 :     }
    4602                 :   }
    4603              16 :   break;
    4604                 : }
    4605                 : 
    4606                 : /* Opcode: SetInsert P1 * P3
    4607                 : **
    4608                 : ** If Set P1 does not exist then create it.  Then insert value
    4609                 : ** P3 into that set.  If P3 is NULL, then insert the top of the
    4610                 : ** stack into the set.
    4611                 : */
    4612                 : case OP_SetInsert: {
    4613               0 :   int i = pOp->p1;
    4614               0 :   if( p->nSet<=i ){
    4615                 :     int k;
    4616               0 :     Set *aSet = sqliteRealloc(p->aSet, (i+1)*sizeof(p->aSet[0]) );
    4617               0 :     if( aSet==0 ) goto no_mem;
    4618               0 :     p->aSet = aSet;
    4619               0 :     for(k=p->nSet; k<=i; k++){
    4620               0 :       sqliteHashInit(&p->aSet[k].hash, SQLITE_HASH_BINARY, 1);
    4621                 :     }
    4622               0 :     p->nSet = i+1;
    4623                 :   }
    4624               0 :   if( pOp->p3 ){
    4625               0 :     sqliteHashInsert(&p->aSet[i].hash, pOp->p3, strlen(pOp->p3)+1, p);
    4626                 :   }else{
    4627                 :     assert( pTos>=p->aStack );
    4628               0 :     Stringify(pTos);
    4629               0 :     sqliteHashInsert(&p->aSet[i].hash, pTos->z, pTos->n, p);
    4630               0 :     Release(pTos);
    4631               0 :     pTos--;
    4632                 :   }
    4633               0 :   if( sqlite_malloc_failed ) goto no_mem;
    4634               0 :   break;
    4635                 : }
    4636                 : 
    4637                 : /* Opcode: SetFound P1 P2 *
    4638                 : **
    4639                 : ** Pop the stack once and compare the value popped off with the
    4640                 : ** contents of set P1.  If the element popped exists in set P1,
    4641                 : ** then jump to P2.  Otherwise fall through.
    4642                 : */
    4643                 : case OP_SetFound: {
    4644               0 :   int i = pOp->p1;
    4645                 :   assert( pTos>=p->aStack );
    4646               0 :   Stringify(pTos);
    4647               0 :   if( i>=0 && i<p->nSet && sqliteHashFind(&p->aSet[i].hash, pTos->z, pTos->n)){
    4648               0 :     pc = pOp->p2 - 1;
    4649                 :   }
    4650               0 :   Release(pTos);
    4651               0 :   pTos--;
    4652               0 :   break;
    4653                 : }
    4654                 : 
    4655                 : /* Opcode: SetNotFound P1 P2 *
    4656                 : **
    4657                 : ** Pop the stack once and compare the value popped off with the
    4658                 : ** contents of set P1.  If the element popped does not exists in 
    4659                 : ** set P1, then jump to P2.  Otherwise fall through.
    4660                 : */
    4661                 : case OP_SetNotFound: {
    4662               0 :   int i = pOp->p1;
    4663                 :   assert( pTos>=p->aStack );
    4664               0 :   Stringify(pTos);
    4665               0 :   if( i<0 || i>=p->nSet ||
    4666                 :        sqliteHashFind(&p->aSet[i].hash, pTos->z, pTos->n)==0 ){
    4667               0 :     pc = pOp->p2 - 1;
    4668                 :   }
    4669               0 :   Release(pTos);
    4670               0 :   pTos--;
    4671               0 :   break;
    4672                 : }
    4673                 : 
    4674                 : /* Opcode: SetFirst P1 P2 *
    4675                 : **
    4676                 : ** Read the first element from set P1 and push it onto the stack.  If the
    4677                 : ** set is empty, push nothing and jump immediately to P2.  This opcode is
    4678                 : ** used in combination with OP_SetNext to loop over all elements of a set.
    4679                 : */
    4680                 : /* Opcode: SetNext P1 P2 *
    4681                 : **
    4682                 : ** Read the next element from set P1 and push it onto the stack.  If there
    4683                 : ** are no more elements in the set, do not do the push and fall through.
    4684                 : ** Otherwise, jump to P2 after pushing the next set element.
    4685                 : */
    4686                 : case OP_SetFirst: 
    4687                 : case OP_SetNext: {
    4688                 :   Set *pSet;
    4689               0 :   CHECK_FOR_INTERRUPT;
    4690               0 :   if( pOp->p1<0 || pOp->p1>=p->nSet ){
    4691               0 :     if( pOp->opcode==OP_SetFirst ) pc = pOp->p2 - 1;
    4692               0 :     break;
    4693                 :   }
    4694               0 :   pSet = &p->aSet[pOp->p1];
    4695               0 :   if( pOp->opcode==OP_SetFirst ){
    4696               0 :     pSet->prev = sqliteHashFirst(&pSet->hash);
    4697               0 :     if( pSet->prev==0 ){
    4698               0 :       pc = pOp->p2 - 1;
    4699               0 :       break;
    4700                 :     }
    4701                 :   }else{
    4702               0 :     if( pSet->prev ){
    4703               0 :       pSet->prev = sqliteHashNext(pSet->prev);
    4704                 :     }
    4705               0 :     if( pSet->prev==0 ){
    4706               0 :       break;
    4707                 :     }else{
    4708               0 :       pc = pOp->p2 - 1;
    4709                 :     }
    4710                 :   }
    4711               0 :   pTos++;
    4712               0 :   pTos->z = sqliteHashKey(pSet->prev);
    4713               0 :   pTos->n = sqliteHashKeysize(pSet->prev);
    4714               0 :   pTos->flags = MEM_Str | MEM_Ephem;
    4715               0 :   break;
    4716                 : }
    4717                 : 
    4718                 : /* Opcode: Vacuum * * *
    4719                 : **
    4720                 : ** Vacuum the entire database.  This opcode will cause other virtual
    4721                 : ** machines to be created and run.  It may not be called from within
    4722                 : ** a transaction.
    4723                 : */
    4724                 : case OP_Vacuum: {
    4725               0 :   if( sqliteSafetyOff(db) ) goto abort_due_to_misuse; 
    4726               0 :   rc = sqliteRunVacuum(&p->zErrMsg, db);
    4727               0 :   if( sqliteSafetyOn(db) ) goto abort_due_to_misuse;
    4728               0 :   break;
    4729                 : }
    4730                 : 
    4731                 : /* Opcode: StackDepth * * *
    4732                 : **
    4733                 : ** Push an integer onto the stack which is the depth of the stack prior
    4734                 : ** to that integer being pushed.
    4735                 : */
    4736                 : case OP_StackDepth: {
    4737               0 :   int depth = (&pTos[1]) - p->aStack;
    4738               0 :   pTos++;
    4739               0 :   pTos->i = depth;
    4740               0 :   pTos->flags = MEM_Int;
    4741               0 :   break;
    4742                 : }
    4743                 : 
    4744                 : /* Opcode: StackReset * * *
    4745                 : **
    4746                 : ** Pop a single integer off of the stack.  Then pop the stack
    4747                 : ** as many times as necessary to get the depth of the stack down
    4748                 : ** to the value of the integer that was popped.
    4749                 : */
    4750                 : case OP_StackReset: {
    4751                 :   int depth, goal;
    4752                 :   assert( pTos>=p->aStack );
    4753               0 :   Integerify(pTos);
    4754               0 :   goal = pTos->i;
    4755               0 :   depth = (&pTos[1]) - p->aStack;
    4756                 :   assert( goal<depth );
    4757               0 :   popStack(&pTos, depth-goal);
    4758               0 :   break;
    4759                 : }
    4760                 : 
    4761                 : /* An other opcode is illegal...
    4762                 : */
    4763                 : default: {
    4764               0 :   sqlite_snprintf(sizeof(zBuf),zBuf,"%d",pOp->opcode);
    4765               0 :   sqliteSetString(&p->zErrMsg, "unknown opcode ", zBuf, (char*)0);
    4766               0 :   rc = SQLITE_INTERNAL;
    4767                 :   break;
    4768                 : }
    4769                 : 
    4770                 : /*****************************************************************************
    4771                 : ** The cases of the switch statement above this line should all be indented
    4772                 : ** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
    4773                 : ** readability.  From this point on down, the normal indentation rules are
    4774                 : ** restored.
    4775                 : *****************************************************************************/
    4776                 :     }
    4777                 : 
    4778                 : #ifdef VDBE_PROFILE
    4779                 :     {
    4780                 :       long long elapse = hwtime() - start;
    4781                 :       pOp->cycles += elapse;
    4782                 :       pOp->cnt++;
    4783                 : #if 0
    4784                 :         fprintf(stdout, "%10lld ", elapse);
    4785                 :         sqliteVdbePrintOp(stdout, origPc, &p->aOp[origPc]);
    4786                 : #endif
    4787                 :     }
    4788                 : #endif
    4789                 : 
    4790                 :     /* The following code adds nothing to the actual functionality
    4791                 :     ** of the program.  It is only here for testing and debugging.
    4792                 :     ** On the other hand, it does burn CPU cycles every time through
    4793                 :     ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
    4794                 :     */
    4795                 : #ifndef NDEBUG
    4796                 :     /* Sanity checking on the top element of the stack */
    4797                 :     if( pTos>=p->aStack ){
    4798                 :       assert( pTos->flags!=0 );  /* Must define some type */
    4799                 :       if( pTos->flags & MEM_Str ){
    4800                 :         int x = pTos->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
    4801                 :         assert( x!=0 );            /* Strings must define a string subtype */
    4802                 :         assert( (x & (x-1))==0 );  /* Only one string subtype can be defined */
    4803                 :         assert( pTos->z!=0 );      /* Strings must have a value */
    4804                 :         /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
    4805                 :         assert( (pTos->flags & MEM_Short)==0 || pTos->z==pTos->zShort );
    4806                 :         assert( (pTos->flags & MEM_Short)!=0 || pTos->z!=pTos->zShort );
    4807                 :       }else{
    4808                 :         /* Cannot define a string subtype for non-string objects */
    4809                 :         assert( (pTos->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
    4810                 :       }
    4811                 :       /* MEM_Null excludes all other types */
    4812                 :       assert( pTos->flags==MEM_Null || (pTos->flags&MEM_Null)==0 );
    4813                 :     }
    4814                 :     if( pc<-1 || pc>=p->nOp ){
    4815                 :       sqliteSetString(&p->zErrMsg, "jump destination out of range", (char*)0);
    4816                 :       rc = SQLITE_INTERNAL;
    4817                 :     }
    4818                 :     if( p->trace && pTos>=p->aStack ){
    4819                 :       int i;
    4820                 :       fprintf(p->trace, "Stack:");
    4821                 :       for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){
    4822                 :         if( pTos[i].flags & MEM_Null ){
    4823                 :           fprintf(p->trace, " NULL");
    4824                 :         }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
    4825                 :           fprintf(p->trace, " si:%d", pTos[i].i);
    4826                 :         }else if( pTos[i].flags & MEM_Int ){
    4827                 :           fprintf(p->trace, " i:%d", pTos[i].i);
    4828                 :         }else if( pTos[i].flags & MEM_Real ){
    4829                 :           fprintf(p->trace, " r:%g", pTos[i].r);
    4830                 :         }else if( pTos[i].flags & MEM_Str ){
    4831                 :           int j, k;
    4832                 :           char zBuf[100];
    4833                 :           zBuf[0] = ' ';
    4834                 :           if( pTos[i].flags & MEM_Dyn ){
    4835                 :             zBuf[1] = 'z';
    4836                 :             assert( (pTos[i].flags & (MEM_Static|MEM_Ephem))==0 );
    4837                 :           }else if( pTos[i].flags & MEM_Static ){
    4838                 :             zBuf[1] = 't';
    4839                 :             assert( (pTos[i].flags & (MEM_Dyn|MEM_Ephem))==0 );
    4840                 :           }else if( pTos[i].flags & MEM_Ephem ){
    4841                 :             zBuf[1] = 'e';
    4842                 :             assert( (pTos[i].flags & (MEM_Static|MEM_Dyn))==0 );
    4843                 :           }else{
    4844                 :             zBuf[1] = 's';
    4845                 :           }
    4846                 :           zBuf[2] = '[';
    4847                 :           k = 3;
    4848                 :           for(j=0; j<20 && j<pTos[i].n; j++){
    4849                 :             int c = pTos[i].z[j];
    4850                 :             if( c==0 && j==pTos[i].n-1 ) break;
    4851                 :             if( isprint(c) && !isspace(c) ){
    4852                 :               zBuf[k++] = c;
    4853                 :             }else{
    4854                 :               zBuf[k++] = '.';
    4855                 :             }
    4856                 :           }
    4857                 :           zBuf[k++] = ']';
    4858                 :           zBuf[k++] = 0;
    4859                 :           fprintf(p->trace, "%s", zBuf);
    4860                 :         }else{
    4861                 :           fprintf(p->trace, " ???");
    4862                 :         }
    4863                 :       }
    4864                 :       if( rc!=0 ) fprintf(p->trace," rc=%d",rc);
    4865                 :       fprintf(p->trace,"\n");
    4866                 :     }
    4867                 : #endif
    4868                 :   }  /* The end of the for(;;) loop the loops through opcodes */
    4869                 : 
    4870                 :   /* If we reach this point, it means that execution is finished.
    4871                 :   */
    4872               1 : vdbe_halt:
    4873               1 :   CHECK_FOR_INTERRUPT
    4874               1 :   if( rc ){
    4875               1 :     p->rc = rc;
    4876               1 :     rc = SQLITE_ERROR;
    4877                 :   }else{
    4878               0 :     rc = SQLITE_DONE;
    4879                 :   }
    4880               1 :   p->magic = VDBE_MAGIC_HALT;
    4881               1 :   p->pTos = pTos;
    4882               1 :   return rc;
    4883                 : 
    4884                 :   /* Jump to here if a malloc() fails.  It's hard to get a malloc()
    4885                 :   ** to fail on a modern VM computer, so this code is untested.
    4886                 :   */
    4887               0 : no_mem:
    4888               0 :   sqliteSetString(&p->zErrMsg, "out of memory", (char*)0);
    4889               0 :   rc = SQLITE_NOMEM;
    4890               0 :   goto vdbe_halt;
    4891                 : 
    4892                 :   /* Jump to here for an SQLITE_MISUSE error.
    4893                 :   */
    4894               0 : abort_due_to_misuse:
    4895               0 :   rc = SQLITE_MISUSE;
    4896                 :   /* Fall thru into abort_due_to_error */
    4897                 : 
    4898                 :   /* Jump to here for any other kind of fatal error.  The "rc" variable
    4899                 :   ** should hold the error number.
    4900                 :   */
    4901               0 : abort_due_to_error:
    4902               0 :   if( p->zErrMsg==0 ){
    4903               0 :     if( sqlite_malloc_failed ) rc = SQLITE_NOMEM;
    4904               0 :     sqliteSetString(&p->zErrMsg, sqlite_error_string(rc), (char*)0);
    4905                 :   }
    4906               0 :   goto vdbe_halt;
    4907                 : 
    4908                 :   /* Jump to here if the sqlite_interrupt() API sets the interrupt
    4909                 :   ** flag.
    4910                 :   */
    4911               0 : abort_due_to_interrupt:
    4912                 :   assert( db->flags & SQLITE_Interrupt );
    4913               0 :   db->flags &= ~SQLITE_Interrupt;
    4914               0 :   if( db->magic!=SQLITE_MAGIC_BUSY ){
    4915               0 :     rc = SQLITE_MISUSE;
    4916                 :   }else{
    4917               0 :     rc = SQLITE_INTERRUPT;
    4918                 :   }
    4919               0 :   sqliteSetString(&p->zErrMsg, sqlite_error_string(rc), (char*)0);
    4920               0 :   goto vdbe_halt;
    4921                 : }

Generated by: LTP GCOV extension version 1.5

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

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