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