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