1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sql/SQLite364/vdbe.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,5126 @@
1.4 +/*
1.5 +** 2001 September 15
1.6 +**
1.7 +** The author disclaims copyright to this source code. In place of
1.8 +** a legal notice, here is a blessing:
1.9 +**
1.10 +** May you do good and not evil.
1.11 +** May you find forgiveness for yourself and forgive others.
1.12 +** May you share freely, never taking more than you give.
1.13 +**
1.14 +*************************************************************************
1.15 +** The code in this file implements execution method of the
1.16 +** Virtual Database Engine (VDBE). A separate file ("vdbeaux.c")
1.17 +** handles housekeeping details such as creating and deleting
1.18 +** VDBE instances. This file is solely interested in executing
1.19 +** the VDBE program.
1.20 +**
1.21 +** In the external interface, an "sqlite3_stmt*" is an opaque pointer
1.22 +** to a VDBE.
1.23 +**
1.24 +** The SQL parser generates a program which is then executed by
1.25 +** the VDBE to do the work of the SQL statement. VDBE programs are
1.26 +** similar in form to assembly language. The program consists of
1.27 +** a linear sequence of operations. Each operation has an opcode
1.28 +** and 5 operands. Operands P1, P2, and P3 are integers. Operand P4
1.29 +** is a null-terminated string. Operand P5 is an unsigned character.
1.30 +** Few opcodes use all 5 operands.
1.31 +**
1.32 +** Computation results are stored on a set of registers numbered beginning
1.33 +** with 1 and going up to Vdbe.nMem. Each register can store
1.34 +** either an integer, a null-terminated string, a floating point
1.35 +** number, or the SQL "NULL" value. An implicit conversion from one
1.36 +** type to the other occurs as necessary.
1.37 +**
1.38 +** Most of the code in this file is taken up by the sqlite3VdbeExec()
1.39 +** function which does the work of interpreting a VDBE program.
1.40 +** But other routines are also provided to help in building up
1.41 +** a program instruction by instruction.
1.42 +**
1.43 +** Various scripts scan this source file in order to generate HTML
1.44 +** documentation, headers files, or other derived files. The formatting
1.45 +** of the code in this file is, therefore, important. See other comments
1.46 +** in this file for details. If in doubt, do not deviate from existing
1.47 +** commenting and indentation practices when changing or adding code.
1.48 +**
1.49 +** $Id: vdbe.c,v 1.782 2008/10/08 17:58:49 danielk1977 Exp $
1.50 +*/
1.51 +#include "sqliteInt.h"
1.52 +#include <ctype.h>
1.53 +#include "vdbeInt.h"
1.54 +
1.55 +/*
1.56 +** The following global variable is incremented every time a cursor
1.57 +** moves, either by the OP_MoveXX, OP_Next, or OP_Prev opcodes. The test
1.58 +** procedures use this information to make sure that indices are
1.59 +** working correctly. This variable has no function other than to
1.60 +** help verify the correct operation of the library.
1.61 +*/
1.62 +#ifdef SQLITE_TEST
1.63 +int sqlite3_search_count = 0;
1.64 +#endif
1.65 +
1.66 +/*
1.67 +** When this global variable is positive, it gets decremented once before
1.68 +** each instruction in the VDBE. When reaches zero, the u1.isInterrupted
1.69 +** field of the sqlite3 structure is set in order to simulate and interrupt.
1.70 +**
1.71 +** This facility is used for testing purposes only. It does not function
1.72 +** in an ordinary build.
1.73 +*/
1.74 +#ifdef SQLITE_TEST
1.75 +int sqlite3_interrupt_count = 0;
1.76 +#endif
1.77 +
1.78 +/*
1.79 +** The next global variable is incremented each type the OP_Sort opcode
1.80 +** is executed. The test procedures use this information to make sure that
1.81 +** sorting is occurring or not occurring at appropriate times. This variable
1.82 +** has no function other than to help verify the correct operation of the
1.83 +** library.
1.84 +*/
1.85 +#ifdef SQLITE_TEST
1.86 +int sqlite3_sort_count = 0;
1.87 +#endif
1.88 +
1.89 +/*
1.90 +** The next global variable records the size of the largest MEM_Blob
1.91 +** or MEM_Str that has been used by a VDBE opcode. The test procedures
1.92 +** use this information to make sure that the zero-blob functionality
1.93 +** is working correctly. This variable has no function other than to
1.94 +** help verify the correct operation of the library.
1.95 +*/
1.96 +#ifdef SQLITE_TEST
1.97 +int sqlite3_max_blobsize = 0;
1.98 +static void updateMaxBlobsize(Mem *p){
1.99 + if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
1.100 + sqlite3_max_blobsize = p->n;
1.101 + }
1.102 +}
1.103 +#endif
1.104 +
1.105 +/*
1.106 +** Test a register to see if it exceeds the current maximum blob size.
1.107 +** If it does, record the new maximum blob size.
1.108 +*/
1.109 +#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
1.110 +# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
1.111 +#else
1.112 +# define UPDATE_MAX_BLOBSIZE(P)
1.113 +#endif
1.114 +
1.115 +/*
1.116 +** Convert the given register into a string if it isn't one
1.117 +** already. Return non-zero if a malloc() fails.
1.118 +*/
1.119 +#define Stringify(P, enc) \
1.120 + if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
1.121 + { goto no_mem; }
1.122 +
1.123 +/*
1.124 +** An ephemeral string value (signified by the MEM_Ephem flag) contains
1.125 +** a pointer to a dynamically allocated string where some other entity
1.126 +** is responsible for deallocating that string. Because the register
1.127 +** does not control the string, it might be deleted without the register
1.128 +** knowing it.
1.129 +**
1.130 +** This routine converts an ephemeral string into a dynamically allocated
1.131 +** string that the register itself controls. In other words, it
1.132 +** converts an MEM_Ephem string into an MEM_Dyn string.
1.133 +*/
1.134 +#define Deephemeralize(P) \
1.135 + if( ((P)->flags&MEM_Ephem)!=0 \
1.136 + && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
1.137 +
1.138 +/*
1.139 +** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
1.140 +** P if required.
1.141 +*/
1.142 +#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
1.143 +
1.144 +/*
1.145 +** Argument pMem points at a register that will be passed to a
1.146 +** user-defined function or returned to the user as the result of a query.
1.147 +** The second argument, 'db_enc' is the text encoding used by the vdbe for
1.148 +** register variables. This routine sets the pMem->enc and pMem->type
1.149 +** variables used by the sqlite3_value_*() routines.
1.150 +*/
1.151 +#define storeTypeInfo(A,B) _storeTypeInfo(A)
1.152 +static void _storeTypeInfo(Mem *pMem){
1.153 + int flags = pMem->flags;
1.154 + if( flags & MEM_Null ){
1.155 + pMem->type = SQLITE_NULL;
1.156 + }
1.157 + else if( flags & MEM_Int ){
1.158 + pMem->type = SQLITE_INTEGER;
1.159 + }
1.160 + else if( flags & MEM_Real ){
1.161 + pMem->type = SQLITE_FLOAT;
1.162 + }
1.163 + else if( flags & MEM_Str ){
1.164 + pMem->type = SQLITE_TEXT;
1.165 + }else{
1.166 + pMem->type = SQLITE_BLOB;
1.167 + }
1.168 +}
1.169 +
1.170 +/*
1.171 +** Properties of opcodes. The OPFLG_INITIALIZER macro is
1.172 +** created by mkopcodeh.awk during compilation. Data is obtained
1.173 +** from the comments following the "case OP_xxxx:" statements in
1.174 +** this file.
1.175 +*/
1.176 +static const unsigned char opcodeProperty[] = OPFLG_INITIALIZER;
1.177 +
1.178 +/*
1.179 +** Return true if an opcode has any of the OPFLG_xxx properties
1.180 +** specified by mask.
1.181 +*/
1.182 +int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){
1.183 + assert( opcode>0 && opcode<sizeof(opcodeProperty) );
1.184 + return (opcodeProperty[opcode]&mask)!=0;
1.185 +}
1.186 +
1.187 +/*
1.188 +** Allocate cursor number iCur. Return a pointer to it. Return NULL
1.189 +** if we run out of memory.
1.190 +*/
1.191 +static Cursor *allocateCursor(
1.192 + Vdbe *p,
1.193 + int iCur,
1.194 + Op *pOp,
1.195 + int iDb,
1.196 + int isBtreeCursor
1.197 +){
1.198 + /* Find the memory cell that will be used to store the blob of memory
1.199 + ** required for this Cursor structure. It is convenient to use a
1.200 + ** vdbe memory cell to manage the memory allocation required for a
1.201 + ** Cursor structure for the following reasons:
1.202 + **
1.203 + ** * Sometimes cursor numbers are used for a couple of different
1.204 + ** purposes in a vdbe program. The different uses might require
1.205 + ** different sized allocations. Memory cells provide growable
1.206 + ** allocations.
1.207 + **
1.208 + ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
1.209 + ** be freed lazily via the sqlite3_release_memory() API. This
1.210 + ** minimizes the number of malloc calls made by the system.
1.211 + **
1.212 + ** Memory cells for cursors are allocated at the top of the address
1.213 + ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
1.214 + ** cursor 1 is managed by memory cell (p->nMem-1), etc.
1.215 + */
1.216 + Mem *pMem = &p->aMem[p->nMem-iCur];
1.217 +
1.218 + int nByte;
1.219 + Cursor *pCx = 0;
1.220 + /* If the opcode of pOp is OP_SetNumColumns, then pOp->p2 contains
1.221 + ** the number of fields in the records contained in the table or
1.222 + ** index being opened. Use this to reserve space for the
1.223 + ** Cursor.aType[] array.
1.224 + */
1.225 + int nField = 0;
1.226 + if( pOp->opcode==OP_SetNumColumns || pOp->opcode==OP_OpenEphemeral ){
1.227 + nField = pOp->p2;
1.228 + }
1.229 + nByte =
1.230 + sizeof(Cursor) +
1.231 + (isBtreeCursor?sqlite3BtreeCursorSize():0) +
1.232 + 2*nField*sizeof(u32);
1.233 +
1.234 + assert( iCur<p->nCursor );
1.235 + if( p->apCsr[iCur] ){
1.236 + sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
1.237 + p->apCsr[iCur] = 0;
1.238 + }
1.239 + if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
1.240 + p->apCsr[iCur] = pCx = (Cursor *)pMem->z;
1.241 + memset(pMem->z, 0, nByte);
1.242 + pCx->iDb = iDb;
1.243 + pCx->nField = nField;
1.244 + if( nField ){
1.245 + pCx->aType = (u32 *)&pMem->z[sizeof(Cursor)];
1.246 + }
1.247 + if( isBtreeCursor ){
1.248 + pCx->pCursor = (BtCursor *)&pMem->z[sizeof(Cursor)+2*nField*sizeof(u32)];
1.249 + }
1.250 + }
1.251 + return pCx;
1.252 +}
1.253 +
1.254 +/*
1.255 +** Try to convert a value into a numeric representation if we can
1.256 +** do so without loss of information. In other words, if the string
1.257 +** looks like a number, convert it into a number. If it does not
1.258 +** look like a number, leave it alone.
1.259 +*/
1.260 +static void applyNumericAffinity(Mem *pRec){
1.261 + if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
1.262 + int realnum;
1.263 + sqlite3VdbeMemNulTerminate(pRec);
1.264 + if( (pRec->flags&MEM_Str)
1.265 + && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){
1.266 + i64 value;
1.267 + sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8);
1.268 + if( !realnum && sqlite3Atoi64(pRec->z, &value) ){
1.269 + pRec->u.i = value;
1.270 + MemSetTypeFlag(pRec, MEM_Int);
1.271 + }else{
1.272 + sqlite3VdbeMemRealify(pRec);
1.273 + }
1.274 + }
1.275 + }
1.276 +}
1.277 +
1.278 +/*
1.279 +** Processing is determine by the affinity parameter:
1.280 +**
1.281 +** SQLITE_AFF_INTEGER:
1.282 +** SQLITE_AFF_REAL:
1.283 +** SQLITE_AFF_NUMERIC:
1.284 +** Try to convert pRec to an integer representation or a
1.285 +** floating-point representation if an integer representation
1.286 +** is not possible. Note that the integer representation is
1.287 +** always preferred, even if the affinity is REAL, because
1.288 +** an integer representation is more space efficient on disk.
1.289 +**
1.290 +** SQLITE_AFF_TEXT:
1.291 +** Convert pRec to a text representation.
1.292 +**
1.293 +** SQLITE_AFF_NONE:
1.294 +** No-op. pRec is unchanged.
1.295 +*/
1.296 +static void applyAffinity(
1.297 + Mem *pRec, /* The value to apply affinity to */
1.298 + char affinity, /* The affinity to be applied */
1.299 + u8 enc /* Use this text encoding */
1.300 +){
1.301 + if( affinity==SQLITE_AFF_TEXT ){
1.302 + /* Only attempt the conversion to TEXT if there is an integer or real
1.303 + ** representation (blob and NULL do not get converted) but no string
1.304 + ** representation.
1.305 + */
1.306 + if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
1.307 + sqlite3VdbeMemStringify(pRec, enc);
1.308 + }
1.309 + pRec->flags &= ~(MEM_Real|MEM_Int);
1.310 + }else if( affinity!=SQLITE_AFF_NONE ){
1.311 + assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
1.312 + || affinity==SQLITE_AFF_NUMERIC );
1.313 + applyNumericAffinity(pRec);
1.314 + if( pRec->flags & MEM_Real ){
1.315 + sqlite3VdbeIntegerAffinity(pRec);
1.316 + }
1.317 + }
1.318 +}
1.319 +
1.320 +/*
1.321 +** Try to convert the type of a function argument or a result column
1.322 +** into a numeric representation. Use either INTEGER or REAL whichever
1.323 +** is appropriate. But only do the conversion if it is possible without
1.324 +** loss of information and return the revised type of the argument.
1.325 +**
1.326 +** This is an EXPERIMENTAL api and is subject to change or removal.
1.327 +*/
1.328 +int sqlite3_value_numeric_type(sqlite3_value *pVal){
1.329 + Mem *pMem = (Mem*)pVal;
1.330 + applyNumericAffinity(pMem);
1.331 + storeTypeInfo(pMem, 0);
1.332 + return pMem->type;
1.333 +}
1.334 +
1.335 +/*
1.336 +** Exported version of applyAffinity(). This one works on sqlite3_value*,
1.337 +** not the internal Mem* type.
1.338 +*/
1.339 +void sqlite3ValueApplyAffinity(
1.340 + sqlite3_value *pVal,
1.341 + u8 affinity,
1.342 + u8 enc
1.343 +){
1.344 + applyAffinity((Mem *)pVal, affinity, enc);
1.345 +}
1.346 +
1.347 +#ifdef SQLITE_DEBUG
1.348 +/*
1.349 +** Write a nice string representation of the contents of cell pMem
1.350 +** into buffer zBuf, length nBuf.
1.351 +*/
1.352 +void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
1.353 + char *zCsr = zBuf;
1.354 + int f = pMem->flags;
1.355 +
1.356 + static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
1.357 +
1.358 + if( f&MEM_Blob ){
1.359 + int i;
1.360 + char c;
1.361 + if( f & MEM_Dyn ){
1.362 + c = 'z';
1.363 + assert( (f & (MEM_Static|MEM_Ephem))==0 );
1.364 + }else if( f & MEM_Static ){
1.365 + c = 't';
1.366 + assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
1.367 + }else if( f & MEM_Ephem ){
1.368 + c = 'e';
1.369 + assert( (f & (MEM_Static|MEM_Dyn))==0 );
1.370 + }else{
1.371 + c = 's';
1.372 + }
1.373 +
1.374 + sqlite3_snprintf(100, zCsr, "%c", c);
1.375 + zCsr += strlen(zCsr);
1.376 + sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
1.377 + zCsr += strlen(zCsr);
1.378 + for(i=0; i<16 && i<pMem->n; i++){
1.379 + sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
1.380 + zCsr += strlen(zCsr);
1.381 + }
1.382 + for(i=0; i<16 && i<pMem->n; i++){
1.383 + char z = pMem->z[i];
1.384 + if( z<32 || z>126 ) *zCsr++ = '.';
1.385 + else *zCsr++ = z;
1.386 + }
1.387 +
1.388 + sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
1.389 + zCsr += strlen(zCsr);
1.390 + if( f & MEM_Zero ){
1.391 + sqlite3_snprintf(100, zCsr,"+%lldz",pMem->u.i);
1.392 + zCsr += strlen(zCsr);
1.393 + }
1.394 + *zCsr = '\0';
1.395 + }else if( f & MEM_Str ){
1.396 + int j, k;
1.397 + zBuf[0] = ' ';
1.398 + if( f & MEM_Dyn ){
1.399 + zBuf[1] = 'z';
1.400 + assert( (f & (MEM_Static|MEM_Ephem))==0 );
1.401 + }else if( f & MEM_Static ){
1.402 + zBuf[1] = 't';
1.403 + assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
1.404 + }else if( f & MEM_Ephem ){
1.405 + zBuf[1] = 'e';
1.406 + assert( (f & (MEM_Static|MEM_Dyn))==0 );
1.407 + }else{
1.408 + zBuf[1] = 's';
1.409 + }
1.410 + k = 2;
1.411 + sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
1.412 + k += strlen(&zBuf[k]);
1.413 + zBuf[k++] = '[';
1.414 + for(j=0; j<15 && j<pMem->n; j++){
1.415 + u8 c = pMem->z[j];
1.416 + if( c>=0x20 && c<0x7f ){
1.417 + zBuf[k++] = c;
1.418 + }else{
1.419 + zBuf[k++] = '.';
1.420 + }
1.421 + }
1.422 + zBuf[k++] = ']';
1.423 + sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
1.424 + k += strlen(&zBuf[k]);
1.425 + zBuf[k++] = 0;
1.426 + }
1.427 +}
1.428 +#endif
1.429 +
1.430 +#ifdef SQLITE_DEBUG
1.431 +/*
1.432 +** Print the value of a register for tracing purposes:
1.433 +*/
1.434 +static void memTracePrint(FILE *out, Mem *p){
1.435 + if( p->flags & MEM_Null ){
1.436 + fprintf(out, " NULL");
1.437 + }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
1.438 + fprintf(out, " si:%lld", p->u.i);
1.439 + }else if( p->flags & MEM_Int ){
1.440 + fprintf(out, " i:%lld", p->u.i);
1.441 + }else if( p->flags & MEM_Real ){
1.442 + fprintf(out, " r:%g", p->r);
1.443 + }else{
1.444 + char zBuf[200];
1.445 + sqlite3VdbeMemPrettyPrint(p, zBuf);
1.446 + fprintf(out, " ");
1.447 + fprintf(out, "%s", zBuf);
1.448 + }
1.449 +}
1.450 +static void registerTrace(FILE *out, int iReg, Mem *p){
1.451 + fprintf(out, "REG[%d] = ", iReg);
1.452 + memTracePrint(out, p);
1.453 + fprintf(out, "\n");
1.454 +}
1.455 +#endif
1.456 +
1.457 +#ifdef SQLITE_DEBUG
1.458 +# define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
1.459 +#else
1.460 +# define REGISTER_TRACE(R,M)
1.461 +#endif
1.462 +
1.463 +
1.464 +#ifdef VDBE_PROFILE
1.465 +
1.466 +/*
1.467 +** hwtime.h contains inline assembler code for implementing
1.468 +** high-performance timing routines.
1.469 +*/
1.470 +#include "hwtime.h"
1.471 +
1.472 +#endif
1.473 +
1.474 +/*
1.475 +** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
1.476 +** sqlite3_interrupt() routine has been called. If it has been, then
1.477 +** processing of the VDBE program is interrupted.
1.478 +**
1.479 +** This macro added to every instruction that does a jump in order to
1.480 +** implement a loop. This test used to be on every single instruction,
1.481 +** but that meant we more testing that we needed. By only testing the
1.482 +** flag on jump instructions, we get a (small) speed improvement.
1.483 +*/
1.484 +#define CHECK_FOR_INTERRUPT \
1.485 + if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
1.486 +
1.487 +#ifdef SQLITE_DEBUG
1.488 +static int fileExists(sqlite3 *db, const char *zFile){
1.489 + int res = 0;
1.490 + int rc = SQLITE_OK;
1.491 +#ifdef SQLITE_TEST
1.492 + /* If we are currently testing IO errors, then do not call OsAccess() to
1.493 + ** test for the presence of zFile. This is because any IO error that
1.494 + ** occurs here will not be reported, causing the test to fail.
1.495 + */
1.496 + extern int sqlite3_io_error_pending;
1.497 + if( sqlite3_io_error_pending<=0 )
1.498 +#endif
1.499 + rc = sqlite3OsAccess(db->pVfs, zFile, SQLITE_ACCESS_EXISTS, &res);
1.500 + return (res && rc==SQLITE_OK);
1.501 +}
1.502 +#endif
1.503 +
1.504 +/*
1.505 +** Execute as much of a VDBE program as we can then return.
1.506 +**
1.507 +** sqlite3VdbeMakeReady() must be called before this routine in order to
1.508 +** close the program with a final OP_Halt and to set up the callbacks
1.509 +** and the error message pointer.
1.510 +**
1.511 +** Whenever a row or result data is available, this routine will either
1.512 +** invoke the result callback (if there is one) or return with
1.513 +** SQLITE_ROW.
1.514 +**
1.515 +** If an attempt is made to open a locked database, then this routine
1.516 +** will either invoke the busy callback (if there is one) or it will
1.517 +** return SQLITE_BUSY.
1.518 +**
1.519 +** If an error occurs, an error message is written to memory obtained
1.520 +** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
1.521 +** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
1.522 +**
1.523 +** If the callback ever returns non-zero, then the program exits
1.524 +** immediately. There will be no error message but the p->rc field is
1.525 +** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
1.526 +**
1.527 +** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
1.528 +** routine to return SQLITE_ERROR.
1.529 +**
1.530 +** Other fatal errors return SQLITE_ERROR.
1.531 +**
1.532 +** After this routine has finished, sqlite3VdbeFinalize() should be
1.533 +** used to clean up the mess that was left behind.
1.534 +*/
1.535 +int sqlite3VdbeExec(
1.536 + Vdbe *p /* The VDBE */
1.537 +){
1.538 + int pc; /* The program counter */
1.539 + Op *pOp; /* Current operation */
1.540 + int rc = SQLITE_OK; /* Value to return */
1.541 + sqlite3 *db = p->db; /* The database */
1.542 + u8 encoding = ENC(db); /* The database encoding */
1.543 + Mem *pIn1, *pIn2, *pIn3; /* Input operands */
1.544 + Mem *pOut; /* Output operand */
1.545 + u8 opProperty;
1.546 + int iCompare = 0; /* Result of last OP_Compare operation */
1.547 + int *aPermute = 0; /* Permuation of columns for OP_Compare */
1.548 +#ifdef VDBE_PROFILE
1.549 + u64 start; /* CPU clock count at start of opcode */
1.550 + int origPc; /* Program counter at start of opcode */
1.551 +#endif
1.552 +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1.553 + int nProgressOps = 0; /* Opcodes executed since progress callback. */
1.554 +#endif
1.555 + UnpackedRecord aTempRec[16]; /* Space to hold a transient UnpackedRecord */
1.556 +
1.557 +
1.558 + assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
1.559 + assert( db->magic==SQLITE_MAGIC_BUSY );
1.560 + sqlite3BtreeMutexArrayEnter(&p->aMutex);
1.561 + if( p->rc==SQLITE_NOMEM ){
1.562 + /* This happens if a malloc() inside a call to sqlite3_column_text() or
1.563 + ** sqlite3_column_text16() failed. */
1.564 + goto no_mem;
1.565 + }
1.566 + assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
1.567 + p->rc = SQLITE_OK;
1.568 + assert( p->explain==0 );
1.569 + p->pResultSet = 0;
1.570 + db->busyHandler.nBusy = 0;
1.571 + CHECK_FOR_INTERRUPT;
1.572 + sqlite3VdbeIOTraceSql(p);
1.573 +#ifdef SQLITE_DEBUG
1.574 + sqlite3BeginBenignMalloc();
1.575 + if( p->pc==0
1.576 + && ((p->db->flags & SQLITE_VdbeListing) || fileExists(db, "vdbe_explain"))
1.577 + ){
1.578 + int i;
1.579 + printf("VDBE Program Listing:\n");
1.580 + sqlite3VdbePrintSql(p);
1.581 + for(i=0; i<p->nOp; i++){
1.582 + sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
1.583 + }
1.584 + }
1.585 + if( fileExists(db, "vdbe_trace") ){
1.586 + p->trace = stdout;
1.587 + }
1.588 + sqlite3EndBenignMalloc();
1.589 +#endif
1.590 + for(pc=p->pc; rc==SQLITE_OK; pc++){
1.591 + assert( pc>=0 && pc<p->nOp );
1.592 + if( db->mallocFailed ) goto no_mem;
1.593 +#ifdef VDBE_PROFILE
1.594 + origPc = pc;
1.595 + start = sqlite3Hwtime();
1.596 +#endif
1.597 + pOp = &p->aOp[pc];
1.598 +
1.599 + /* Only allow tracing if SQLITE_DEBUG is defined.
1.600 + */
1.601 +#ifdef SQLITE_DEBUG
1.602 + if( p->trace ){
1.603 + if( pc==0 ){
1.604 + printf("VDBE Execution Trace:\n");
1.605 + sqlite3VdbePrintSql(p);
1.606 + }
1.607 + sqlite3VdbePrintOp(p->trace, pc, pOp);
1.608 + }
1.609 + if( p->trace==0 && pc==0 ){
1.610 + sqlite3BeginBenignMalloc();
1.611 + if( fileExists(db, "vdbe_sqltrace") ){
1.612 + sqlite3VdbePrintSql(p);
1.613 + }
1.614 + sqlite3EndBenignMalloc();
1.615 + }
1.616 +#endif
1.617 +
1.618 +
1.619 + /* Check to see if we need to simulate an interrupt. This only happens
1.620 + ** if we have a special test build.
1.621 + */
1.622 +#ifdef SQLITE_TEST
1.623 + if( sqlite3_interrupt_count>0 ){
1.624 + sqlite3_interrupt_count--;
1.625 + if( sqlite3_interrupt_count==0 ){
1.626 + sqlite3_interrupt(db);
1.627 + }
1.628 + }
1.629 +#endif
1.630 +
1.631 +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1.632 + /* Call the progress callback if it is configured and the required number
1.633 + ** of VDBE ops have been executed (either since this invocation of
1.634 + ** sqlite3VdbeExec() or since last time the progress callback was called).
1.635 + ** If the progress callback returns non-zero, exit the virtual machine with
1.636 + ** a return code SQLITE_ABORT.
1.637 + */
1.638 + if( db->xProgress ){
1.639 + if( db->nProgressOps==nProgressOps ){
1.640 + int prc;
1.641 + if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1.642 + prc =db->xProgress(db->pProgressArg);
1.643 + if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
1.644 + if( prc!=0 ){
1.645 + rc = SQLITE_INTERRUPT;
1.646 + goto vdbe_error_halt;
1.647 + }
1.648 + nProgressOps = 0;
1.649 + }
1.650 + nProgressOps++;
1.651 + }
1.652 +#endif
1.653 +
1.654 + /* Do common setup processing for any opcode that is marked
1.655 + ** with the "out2-prerelease" tag. Such opcodes have a single
1.656 + ** output which is specified by the P2 parameter. The P2 register
1.657 + ** is initialized to a NULL.
1.658 + */
1.659 + opProperty = opcodeProperty[pOp->opcode];
1.660 + if( (opProperty & OPFLG_OUT2_PRERELEASE)!=0 ){
1.661 + assert( pOp->p2>0 );
1.662 + assert( pOp->p2<=p->nMem );
1.663 + pOut = &p->aMem[pOp->p2];
1.664 + sqlite3VdbeMemReleaseExternal(pOut);
1.665 + pOut->flags = MEM_Null;
1.666 + }else
1.667 +
1.668 + /* Do common setup for opcodes marked with one of the following
1.669 + ** combinations of properties.
1.670 + **
1.671 + ** in1
1.672 + ** in1 in2
1.673 + ** in1 in2 out3
1.674 + ** in1 in3
1.675 + **
1.676 + ** Variables pIn1, pIn2, and pIn3 are made to point to appropriate
1.677 + ** registers for inputs. Variable pOut points to the output register.
1.678 + */
1.679 + if( (opProperty & OPFLG_IN1)!=0 ){
1.680 + assert( pOp->p1>0 );
1.681 + assert( pOp->p1<=p->nMem );
1.682 + pIn1 = &p->aMem[pOp->p1];
1.683 + REGISTER_TRACE(pOp->p1, pIn1);
1.684 + if( (opProperty & OPFLG_IN2)!=0 ){
1.685 + assert( pOp->p2>0 );
1.686 + assert( pOp->p2<=p->nMem );
1.687 + pIn2 = &p->aMem[pOp->p2];
1.688 + REGISTER_TRACE(pOp->p2, pIn2);
1.689 + if( (opProperty & OPFLG_OUT3)!=0 ){
1.690 + assert( pOp->p3>0 );
1.691 + assert( pOp->p3<=p->nMem );
1.692 + pOut = &p->aMem[pOp->p3];
1.693 + }
1.694 + }else if( (opProperty & OPFLG_IN3)!=0 ){
1.695 + assert( pOp->p3>0 );
1.696 + assert( pOp->p3<=p->nMem );
1.697 + pIn3 = &p->aMem[pOp->p3];
1.698 + REGISTER_TRACE(pOp->p3, pIn3);
1.699 + }
1.700 + }else if( (opProperty & OPFLG_IN2)!=0 ){
1.701 + assert( pOp->p2>0 );
1.702 + assert( pOp->p2<=p->nMem );
1.703 + pIn2 = &p->aMem[pOp->p2];
1.704 + REGISTER_TRACE(pOp->p2, pIn2);
1.705 + }else if( (opProperty & OPFLG_IN3)!=0 ){
1.706 + assert( pOp->p3>0 );
1.707 + assert( pOp->p3<=p->nMem );
1.708 + pIn3 = &p->aMem[pOp->p3];
1.709 + REGISTER_TRACE(pOp->p3, pIn3);
1.710 + }
1.711 +
1.712 + switch( pOp->opcode ){
1.713 +
1.714 +/*****************************************************************************
1.715 +** What follows is a massive switch statement where each case implements a
1.716 +** separate instruction in the virtual machine. If we follow the usual
1.717 +** indentation conventions, each case should be indented by 6 spaces. But
1.718 +** that is a lot of wasted space on the left margin. So the code within
1.719 +** the switch statement will break with convention and be flush-left. Another
1.720 +** big comment (similar to this one) will mark the point in the code where
1.721 +** we transition back to normal indentation.
1.722 +**
1.723 +** The formatting of each case is important. The makefile for SQLite
1.724 +** generates two C files "opcodes.h" and "opcodes.c" by scanning this
1.725 +** file looking for lines that begin with "case OP_". The opcodes.h files
1.726 +** will be filled with #defines that give unique integer values to each
1.727 +** opcode and the opcodes.c file is filled with an array of strings where
1.728 +** each string is the symbolic name for the corresponding opcode. If the
1.729 +** case statement is followed by a comment of the form "/# same as ... #/"
1.730 +** that comment is used to determine the particular value of the opcode.
1.731 +**
1.732 +** Other keywords in the comment that follows each case are used to
1.733 +** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
1.734 +** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
1.735 +** the mkopcodeh.awk script for additional information.
1.736 +**
1.737 +** Documentation about VDBE opcodes is generated by scanning this file
1.738 +** for lines of that contain "Opcode:". That line and all subsequent
1.739 +** comment lines are used in the generation of the opcode.html documentation
1.740 +** file.
1.741 +**
1.742 +** SUMMARY:
1.743 +**
1.744 +** Formatting is important to scripts that scan this file.
1.745 +** Do not deviate from the formatting style currently in use.
1.746 +**
1.747 +*****************************************************************************/
1.748 +
1.749 +/* Opcode: Goto * P2 * * *
1.750 +**
1.751 +** An unconditional jump to address P2.
1.752 +** The next instruction executed will be
1.753 +** the one at index P2 from the beginning of
1.754 +** the program.
1.755 +*/
1.756 +case OP_Goto: { /* jump */
1.757 + CHECK_FOR_INTERRUPT;
1.758 + pc = pOp->p2 - 1;
1.759 + break;
1.760 +}
1.761 +
1.762 +/* Opcode: Gosub P1 P2 * * *
1.763 +**
1.764 +** Write the current address onto register P1
1.765 +** and then jump to address P2.
1.766 +*/
1.767 +case OP_Gosub: { /* jump */
1.768 + assert( pOp->p1>0 );
1.769 + assert( pOp->p1<=p->nMem );
1.770 + pIn1 = &p->aMem[pOp->p1];
1.771 + assert( (pIn1->flags & MEM_Dyn)==0 );
1.772 + pIn1->flags = MEM_Int;
1.773 + pIn1->u.i = pc;
1.774 + REGISTER_TRACE(pOp->p1, pIn1);
1.775 + pc = pOp->p2 - 1;
1.776 + break;
1.777 +}
1.778 +
1.779 +/* Opcode: Return P1 * * * *
1.780 +**
1.781 +** Jump to the next instruction after the address in register P1.
1.782 +*/
1.783 +case OP_Return: { /* in1 */
1.784 + assert( pIn1->flags & MEM_Int );
1.785 + pc = pIn1->u.i;
1.786 + break;
1.787 +}
1.788 +
1.789 +/* Opcode: Yield P1 * * * *
1.790 +**
1.791 +** Swap the program counter with the value in register P1.
1.792 +*/
1.793 +case OP_Yield: {
1.794 + int pcDest;
1.795 + assert( pOp->p1>0 );
1.796 + assert( pOp->p1<=p->nMem );
1.797 + pIn1 = &p->aMem[pOp->p1];
1.798 + assert( (pIn1->flags & MEM_Dyn)==0 );
1.799 + pIn1->flags = MEM_Int;
1.800 + pcDest = pIn1->u.i;
1.801 + pIn1->u.i = pc;
1.802 + REGISTER_TRACE(pOp->p1, pIn1);
1.803 + pc = pcDest;
1.804 + break;
1.805 +}
1.806 +
1.807 +
1.808 +/* Opcode: Halt P1 P2 * P4 *
1.809 +**
1.810 +** Exit immediately. All open cursors, Fifos, etc are closed
1.811 +** automatically.
1.812 +**
1.813 +** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
1.814 +** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
1.815 +** For errors, it can be some other value. If P1!=0 then P2 will determine
1.816 +** whether or not to rollback the current transaction. Do not rollback
1.817 +** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
1.818 +** then back out all changes that have occurred during this execution of the
1.819 +** VDBE, but do not rollback the transaction.
1.820 +**
1.821 +** If P4 is not null then it is an error message string.
1.822 +**
1.823 +** There is an implied "Halt 0 0 0" instruction inserted at the very end of
1.824 +** every program. So a jump past the last instruction of the program
1.825 +** is the same as executing Halt.
1.826 +*/
1.827 +case OP_Halt: {
1.828 + p->rc = pOp->p1;
1.829 + p->pc = pc;
1.830 + p->errorAction = pOp->p2;
1.831 + if( pOp->p4.z ){
1.832 + sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
1.833 + }
1.834 + rc = sqlite3VdbeHalt(p);
1.835 + assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
1.836 + if( rc==SQLITE_BUSY ){
1.837 + p->rc = rc = SQLITE_BUSY;
1.838 + }else{
1.839 + rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
1.840 + }
1.841 + goto vdbe_return;
1.842 +}
1.843 +
1.844 +/* Opcode: Integer P1 P2 * * *
1.845 +**
1.846 +** The 32-bit integer value P1 is written into register P2.
1.847 +*/
1.848 +case OP_Integer: { /* out2-prerelease */
1.849 + pOut->flags = MEM_Int;
1.850 + pOut->u.i = pOp->p1;
1.851 + break;
1.852 +}
1.853 +
1.854 +/* Opcode: Int64 * P2 * P4 *
1.855 +**
1.856 +** P4 is a pointer to a 64-bit integer value.
1.857 +** Write that value into register P2.
1.858 +*/
1.859 +case OP_Int64: { /* out2-prerelease */
1.860 + assert( pOp->p4.pI64!=0 );
1.861 + pOut->flags = MEM_Int;
1.862 + pOut->u.i = *pOp->p4.pI64;
1.863 + break;
1.864 +}
1.865 +
1.866 +/* Opcode: Real * P2 * P4 *
1.867 +**
1.868 +** P4 is a pointer to a 64-bit floating point value.
1.869 +** Write that value into register P2.
1.870 +*/
1.871 +case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
1.872 + pOut->flags = MEM_Real;
1.873 + assert( !sqlite3IsNaN(*pOp->p4.pReal) );
1.874 + pOut->r = *pOp->p4.pReal;
1.875 + break;
1.876 +}
1.877 +
1.878 +/* Opcode: String8 * P2 * P4 *
1.879 +**
1.880 +** P4 points to a nul terminated UTF-8 string. This opcode is transformed
1.881 +** into an OP_String before it is executed for the first time.
1.882 +*/
1.883 +case OP_String8: { /* same as TK_STRING, out2-prerelease */
1.884 + assert( pOp->p4.z!=0 );
1.885 + pOp->opcode = OP_String;
1.886 + pOp->p1 = strlen(pOp->p4.z);
1.887 +
1.888 +#ifndef SQLITE_OMIT_UTF16
1.889 + if( encoding!=SQLITE_UTF8 ){
1.890 + sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
1.891 + if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
1.892 + if( SQLITE_OK!=sqlite3VdbeMemMakeWriteable(pOut) ) goto no_mem;
1.893 + pOut->zMalloc = 0;
1.894 + pOut->flags |= MEM_Static;
1.895 + pOut->flags &= ~MEM_Dyn;
1.896 + if( pOp->p4type==P4_DYNAMIC ){
1.897 + sqlite3DbFree(db, pOp->p4.z);
1.898 + }
1.899 + pOp->p4type = P4_DYNAMIC;
1.900 + pOp->p4.z = pOut->z;
1.901 + pOp->p1 = pOut->n;
1.902 + if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
1.903 + goto too_big;
1.904 + }
1.905 + UPDATE_MAX_BLOBSIZE(pOut);
1.906 + break;
1.907 + }
1.908 +#endif
1.909 + if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
1.910 + goto too_big;
1.911 + }
1.912 + /* Fall through to the next case, OP_String */
1.913 +}
1.914 +
1.915 +/* Opcode: String P1 P2 * P4 *
1.916 +**
1.917 +** The string value P4 of length P1 (bytes) is stored in register P2.
1.918 +*/
1.919 +case OP_String: { /* out2-prerelease */
1.920 + assert( pOp->p4.z!=0 );
1.921 + pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1.922 + pOut->z = pOp->p4.z;
1.923 + pOut->n = pOp->p1;
1.924 + pOut->enc = encoding;
1.925 + UPDATE_MAX_BLOBSIZE(pOut);
1.926 + break;
1.927 +}
1.928 +
1.929 +/* Opcode: Null * P2 * * *
1.930 +**
1.931 +** Write a NULL into register P2.
1.932 +*/
1.933 +case OP_Null: { /* out2-prerelease */
1.934 + break;
1.935 +}
1.936 +
1.937 +
1.938 +#ifndef SQLITE_OMIT_BLOB_LITERAL
1.939 +/* Opcode: Blob P1 P2 * P4
1.940 +**
1.941 +** P4 points to a blob of data P1 bytes long. Store this
1.942 +** blob in register P2. This instruction is not coded directly
1.943 +** by the compiler. Instead, the compiler layer specifies
1.944 +** an OP_HexBlob opcode, with the hex string representation of
1.945 +** the blob as P4. This opcode is transformed to an OP_Blob
1.946 +** the first time it is executed.
1.947 +*/
1.948 +case OP_Blob: { /* out2-prerelease */
1.949 + assert( pOp->p1 <= SQLITE_MAX_LENGTH );
1.950 + sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
1.951 + pOut->enc = encoding;
1.952 + UPDATE_MAX_BLOBSIZE(pOut);
1.953 + break;
1.954 +}
1.955 +#endif /* SQLITE_OMIT_BLOB_LITERAL */
1.956 +
1.957 +/* Opcode: Variable P1 P2 * * *
1.958 +**
1.959 +** The value of variable P1 is written into register P2. A variable is
1.960 +** an unknown in the original SQL string as handed to sqlite3_compile().
1.961 +** Any occurrence of the '?' character in the original SQL is considered
1.962 +** a variable. Variables in the SQL string are number from left to
1.963 +** right beginning with 1. The values of variables are set using the
1.964 +** sqlite3_bind() API.
1.965 +*/
1.966 +case OP_Variable: { /* out2-prerelease */
1.967 + int j = pOp->p1 - 1;
1.968 + Mem *pVar;
1.969 + assert( j>=0 && j<p->nVar );
1.970 +
1.971 + pVar = &p->aVar[j];
1.972 + if( sqlite3VdbeMemTooBig(pVar) ){
1.973 + goto too_big;
1.974 + }
1.975 + sqlite3VdbeMemShallowCopy(pOut, &p->aVar[j], MEM_Static);
1.976 + UPDATE_MAX_BLOBSIZE(pOut);
1.977 + break;
1.978 +}
1.979 +
1.980 +/* Opcode: Move P1 P2 P3 * *
1.981 +**
1.982 +** Move the values in register P1..P1+P3-1 over into
1.983 +** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
1.984 +** left holding a NULL. It is an error for register ranges
1.985 +** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
1.986 +*/
1.987 +case OP_Move: {
1.988 + char *zMalloc;
1.989 + int n = pOp->p3;
1.990 + int p1 = pOp->p1;
1.991 + int p2 = pOp->p2;
1.992 + assert( n>0 );
1.993 + assert( p1>0 );
1.994 + assert( p1+n<p->nMem );
1.995 + pIn1 = &p->aMem[p1];
1.996 + assert( p2>0 );
1.997 + assert( p2+n<p->nMem );
1.998 + pOut = &p->aMem[p2];
1.999 + assert( p1+n<=p2 || p2+n<=p1 );
1.1000 + while( n-- ){
1.1001 + zMalloc = pOut->zMalloc;
1.1002 + pOut->zMalloc = 0;
1.1003 + sqlite3VdbeMemMove(pOut, pIn1);
1.1004 + pIn1->zMalloc = zMalloc;
1.1005 + REGISTER_TRACE(p2++, pOut);
1.1006 + pIn1++;
1.1007 + pOut++;
1.1008 + }
1.1009 + break;
1.1010 +}
1.1011 +
1.1012 +/* Opcode: Copy P1 P2 * * *
1.1013 +**
1.1014 +** Make a copy of register P1 into register P2.
1.1015 +**
1.1016 +** This instruction makes a deep copy of the value. A duplicate
1.1017 +** is made of any string or blob constant. See also OP_SCopy.
1.1018 +*/
1.1019 +case OP_Copy: {
1.1020 + assert( pOp->p1>0 );
1.1021 + assert( pOp->p1<=p->nMem );
1.1022 + pIn1 = &p->aMem[pOp->p1];
1.1023 + assert( pOp->p2>0 );
1.1024 + assert( pOp->p2<=p->nMem );
1.1025 + pOut = &p->aMem[pOp->p2];
1.1026 + assert( pOut!=pIn1 );
1.1027 + sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1.1028 + Deephemeralize(pOut);
1.1029 + REGISTER_TRACE(pOp->p2, pOut);
1.1030 + break;
1.1031 +}
1.1032 +
1.1033 +/* Opcode: SCopy P1 P2 * * *
1.1034 +**
1.1035 +** Make a shallow copy of register P1 into register P2.
1.1036 +**
1.1037 +** This instruction makes a shallow copy of the value. If the value
1.1038 +** is a string or blob, then the copy is only a pointer to the
1.1039 +** original and hence if the original changes so will the copy.
1.1040 +** Worse, if the original is deallocated, the copy becomes invalid.
1.1041 +** Thus the program must guarantee that the original will not change
1.1042 +** during the lifetime of the copy. Use OP_Copy to make a complete
1.1043 +** copy.
1.1044 +*/
1.1045 +case OP_SCopy: {
1.1046 + assert( pOp->p1>0 );
1.1047 + assert( pOp->p1<=p->nMem );
1.1048 + pIn1 = &p->aMem[pOp->p1];
1.1049 + REGISTER_TRACE(pOp->p1, pIn1);
1.1050 + assert( pOp->p2>0 );
1.1051 + assert( pOp->p2<=p->nMem );
1.1052 + pOut = &p->aMem[pOp->p2];
1.1053 + assert( pOut!=pIn1 );
1.1054 + sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1.1055 + REGISTER_TRACE(pOp->p2, pOut);
1.1056 + break;
1.1057 +}
1.1058 +
1.1059 +/* Opcode: ResultRow P1 P2 * * *
1.1060 +**
1.1061 +** The registers P1 through P1+P2-1 contain a single row of
1.1062 +** results. This opcode causes the sqlite3_step() call to terminate
1.1063 +** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1.1064 +** structure to provide access to the top P1 values as the result
1.1065 +** row.
1.1066 +*/
1.1067 +case OP_ResultRow: {
1.1068 + Mem *pMem;
1.1069 + int i;
1.1070 + assert( p->nResColumn==pOp->p2 );
1.1071 + assert( pOp->p1>0 );
1.1072 + assert( pOp->p1+pOp->p2<=p->nMem );
1.1073 +
1.1074 + /* Invalidate all ephemeral cursor row caches */
1.1075 + p->cacheCtr = (p->cacheCtr + 2)|1;
1.1076 +
1.1077 + /* Make sure the results of the current row are \000 terminated
1.1078 + ** and have an assigned type. The results are de-ephemeralized as
1.1079 + ** as side effect.
1.1080 + */
1.1081 + pMem = p->pResultSet = &p->aMem[pOp->p1];
1.1082 + for(i=0; i<pOp->p2; i++){
1.1083 + sqlite3VdbeMemNulTerminate(&pMem[i]);
1.1084 + storeTypeInfo(&pMem[i], encoding);
1.1085 + REGISTER_TRACE(pOp->p1+i, &pMem[i]);
1.1086 + }
1.1087 + if( db->mallocFailed ) goto no_mem;
1.1088 +
1.1089 + /* Return SQLITE_ROW
1.1090 + */
1.1091 + p->nCallback++;
1.1092 + p->pc = pc + 1;
1.1093 + rc = SQLITE_ROW;
1.1094 + goto vdbe_return;
1.1095 +}
1.1096 +
1.1097 +/* Opcode: Concat P1 P2 P3 * *
1.1098 +**
1.1099 +** Add the text in register P1 onto the end of the text in
1.1100 +** register P2 and store the result in register P3.
1.1101 +** If either the P1 or P2 text are NULL then store NULL in P3.
1.1102 +**
1.1103 +** P3 = P2 || P1
1.1104 +**
1.1105 +** It is illegal for P1 and P3 to be the same register. Sometimes,
1.1106 +** if P3 is the same register as P2, the implementation is able
1.1107 +** to avoid a memcpy().
1.1108 +*/
1.1109 +case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
1.1110 + i64 nByte;
1.1111 +
1.1112 + assert( pIn1!=pOut );
1.1113 + if( (pIn1->flags | pIn2->flags) & MEM_Null ){
1.1114 + sqlite3VdbeMemSetNull(pOut);
1.1115 + break;
1.1116 + }
1.1117 + ExpandBlob(pIn1);
1.1118 + Stringify(pIn1, encoding);
1.1119 + ExpandBlob(pIn2);
1.1120 + Stringify(pIn2, encoding);
1.1121 + nByte = pIn1->n + pIn2->n;
1.1122 + if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
1.1123 + goto too_big;
1.1124 + }
1.1125 + MemSetTypeFlag(pOut, MEM_Str);
1.1126 + if( sqlite3VdbeMemGrow(pOut, nByte+2, pOut==pIn2) ){
1.1127 + goto no_mem;
1.1128 + }
1.1129 + if( pOut!=pIn2 ){
1.1130 + memcpy(pOut->z, pIn2->z, pIn2->n);
1.1131 + }
1.1132 + memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
1.1133 + pOut->z[nByte] = 0;
1.1134 + pOut->z[nByte+1] = 0;
1.1135 + pOut->flags |= MEM_Term;
1.1136 + pOut->n = nByte;
1.1137 + pOut->enc = encoding;
1.1138 + UPDATE_MAX_BLOBSIZE(pOut);
1.1139 + break;
1.1140 +}
1.1141 +
1.1142 +/* Opcode: Add P1 P2 P3 * *
1.1143 +**
1.1144 +** Add the value in register P1 to the value in register P2
1.1145 +** and store the result in register P3.
1.1146 +** If either input is NULL, the result is NULL.
1.1147 +*/
1.1148 +/* Opcode: Multiply P1 P2 P3 * *
1.1149 +**
1.1150 +**
1.1151 +** Multiply the value in register P1 by the value in register P2
1.1152 +** and store the result in register P3.
1.1153 +** If either input is NULL, the result is NULL.
1.1154 +*/
1.1155 +/* Opcode: Subtract P1 P2 P3 * *
1.1156 +**
1.1157 +** Subtract the value in register P1 from the value in register P2
1.1158 +** and store the result in register P3.
1.1159 +** If either input is NULL, the result is NULL.
1.1160 +*/
1.1161 +/* Opcode: Divide P1 P2 P3 * *
1.1162 +**
1.1163 +** Divide the value in register P1 by the value in register P2
1.1164 +** and store the result in register P3. If the value in register P2
1.1165 +** is zero, then the result is NULL.
1.1166 +** If either input is NULL, the result is NULL.
1.1167 +*/
1.1168 +/* Opcode: Remainder P1 P2 P3 * *
1.1169 +**
1.1170 +** Compute the remainder after integer division of the value in
1.1171 +** register P1 by the value in register P2 and store the result in P3.
1.1172 +** If the value in register P2 is zero the result is NULL.
1.1173 +** If either operand is NULL, the result is NULL.
1.1174 +*/
1.1175 +case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1.1176 +case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1.1177 +case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1.1178 +case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1.1179 +case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
1.1180 + int flags;
1.1181 + applyNumericAffinity(pIn1);
1.1182 + applyNumericAffinity(pIn2);
1.1183 + flags = pIn1->flags | pIn2->flags;
1.1184 + if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
1.1185 + if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
1.1186 + i64 a, b;
1.1187 + a = pIn1->u.i;
1.1188 + b = pIn2->u.i;
1.1189 + switch( pOp->opcode ){
1.1190 + case OP_Add: b += a; break;
1.1191 + case OP_Subtract: b -= a; break;
1.1192 + case OP_Multiply: b *= a; break;
1.1193 + case OP_Divide: {
1.1194 + if( a==0 ) goto arithmetic_result_is_null;
1.1195 + /* Dividing the largest possible negative 64-bit integer (1<<63) by
1.1196 + ** -1 returns an integer too large to store in a 64-bit data-type. On
1.1197 + ** some architectures, the value overflows to (1<<63). On others,
1.1198 + ** a SIGFPE is issued. The following statement normalizes this
1.1199 + ** behavior so that all architectures behave as if integer
1.1200 + ** overflow occurred.
1.1201 + */
1.1202 + if( a==-1 && b==SMALLEST_INT64 ) a = 1;
1.1203 + b /= a;
1.1204 + break;
1.1205 + }
1.1206 + default: {
1.1207 + if( a==0 ) goto arithmetic_result_is_null;
1.1208 + if( a==-1 ) a = 1;
1.1209 + b %= a;
1.1210 + break;
1.1211 + }
1.1212 + }
1.1213 + pOut->u.i = b;
1.1214 + MemSetTypeFlag(pOut, MEM_Int);
1.1215 + }else{
1.1216 + double a, b;
1.1217 + a = sqlite3VdbeRealValue(pIn1);
1.1218 + b = sqlite3VdbeRealValue(pIn2);
1.1219 + switch( pOp->opcode ){
1.1220 + case OP_Add: b += a; break;
1.1221 + case OP_Subtract: b -= a; break;
1.1222 + case OP_Multiply: b *= a; break;
1.1223 + case OP_Divide: {
1.1224 + if( a==0.0 ) goto arithmetic_result_is_null;
1.1225 + b /= a;
1.1226 + break;
1.1227 + }
1.1228 + default: {
1.1229 + i64 ia = (i64)a;
1.1230 + i64 ib = (i64)b;
1.1231 + if( ia==0 ) goto arithmetic_result_is_null;
1.1232 + if( ia==-1 ) ia = 1;
1.1233 + b = ib % ia;
1.1234 + break;
1.1235 + }
1.1236 + }
1.1237 + if( sqlite3IsNaN(b) ){
1.1238 + goto arithmetic_result_is_null;
1.1239 + }
1.1240 + pOut->r = b;
1.1241 + MemSetTypeFlag(pOut, MEM_Real);
1.1242 + if( (flags & MEM_Real)==0 ){
1.1243 + sqlite3VdbeIntegerAffinity(pOut);
1.1244 + }
1.1245 + }
1.1246 + break;
1.1247 +
1.1248 +arithmetic_result_is_null:
1.1249 + sqlite3VdbeMemSetNull(pOut);
1.1250 + break;
1.1251 +}
1.1252 +
1.1253 +/* Opcode: CollSeq * * P4
1.1254 +**
1.1255 +** P4 is a pointer to a CollSeq struct. If the next call to a user function
1.1256 +** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1.1257 +** be returned. This is used by the built-in min(), max() and nullif()
1.1258 +** functions.
1.1259 +**
1.1260 +** The interface used by the implementation of the aforementioned functions
1.1261 +** to retrieve the collation sequence set by this opcode is not available
1.1262 +** publicly, only to user functions defined in func.c.
1.1263 +*/
1.1264 +case OP_CollSeq: {
1.1265 + assert( pOp->p4type==P4_COLLSEQ );
1.1266 + break;
1.1267 +}
1.1268 +
1.1269 +/* Opcode: Function P1 P2 P3 P4 P5
1.1270 +**
1.1271 +** Invoke a user function (P4 is a pointer to a Function structure that
1.1272 +** defines the function) with P5 arguments taken from register P2 and
1.1273 +** successors. The result of the function is stored in register P3.
1.1274 +** Register P3 must not be one of the function inputs.
1.1275 +**
1.1276 +** P1 is a 32-bit bitmask indicating whether or not each argument to the
1.1277 +** function was determined to be constant at compile time. If the first
1.1278 +** argument was constant then bit 0 of P1 is set. This is used to determine
1.1279 +** whether meta data associated with a user function argument using the
1.1280 +** sqlite3_set_auxdata() API may be safely retained until the next
1.1281 +** invocation of this opcode.
1.1282 +**
1.1283 +** See also: AggStep and AggFinal
1.1284 +*/
1.1285 +case OP_Function: {
1.1286 + int i;
1.1287 + Mem *pArg;
1.1288 + sqlite3_context ctx;
1.1289 + sqlite3_value **apVal;
1.1290 + int n = pOp->p5;
1.1291 +
1.1292 + apVal = p->apArg;
1.1293 + assert( apVal || n==0 );
1.1294 +
1.1295 + assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem) );
1.1296 + assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
1.1297 + pArg = &p->aMem[pOp->p2];
1.1298 + for(i=0; i<n; i++, pArg++){
1.1299 + apVal[i] = pArg;
1.1300 + storeTypeInfo(pArg, encoding);
1.1301 + REGISTER_TRACE(pOp->p2, pArg);
1.1302 + }
1.1303 +
1.1304 + assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
1.1305 + if( pOp->p4type==P4_FUNCDEF ){
1.1306 + ctx.pFunc = pOp->p4.pFunc;
1.1307 + ctx.pVdbeFunc = 0;
1.1308 + }else{
1.1309 + ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
1.1310 + ctx.pFunc = ctx.pVdbeFunc->pFunc;
1.1311 + }
1.1312 +
1.1313 + assert( pOp->p3>0 && pOp->p3<=p->nMem );
1.1314 + pOut = &p->aMem[pOp->p3];
1.1315 + ctx.s.flags = MEM_Null;
1.1316 + ctx.s.db = db;
1.1317 + ctx.s.xDel = 0;
1.1318 + ctx.s.zMalloc = 0;
1.1319 +
1.1320 + /* The output cell may already have a buffer allocated. Move
1.1321 + ** the pointer to ctx.s so in case the user-function can use
1.1322 + ** the already allocated buffer instead of allocating a new one.
1.1323 + */
1.1324 + sqlite3VdbeMemMove(&ctx.s, pOut);
1.1325 + MemSetTypeFlag(&ctx.s, MEM_Null);
1.1326 +
1.1327 + ctx.isError = 0;
1.1328 + if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
1.1329 + assert( pOp>p->aOp );
1.1330 + assert( pOp[-1].p4type==P4_COLLSEQ );
1.1331 + assert( pOp[-1].opcode==OP_CollSeq );
1.1332 + ctx.pColl = pOp[-1].p4.pColl;
1.1333 + }
1.1334 + if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1.1335 + (*ctx.pFunc->xFunc)(&ctx, n, apVal);
1.1336 + if( sqlite3SafetyOn(db) ){
1.1337 + sqlite3VdbeMemRelease(&ctx.s);
1.1338 + goto abort_due_to_misuse;
1.1339 + }
1.1340 + if( db->mallocFailed ){
1.1341 + /* Even though a malloc() has failed, the implementation of the
1.1342 + ** user function may have called an sqlite3_result_XXX() function
1.1343 + ** to return a value. The following call releases any resources
1.1344 + ** associated with such a value.
1.1345 + **
1.1346 + ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn()
1.1347 + ** fails also (the if(...) statement above). But if people are
1.1348 + ** misusing sqlite, they have bigger problems than a leaked value.
1.1349 + */
1.1350 + sqlite3VdbeMemRelease(&ctx.s);
1.1351 + goto no_mem;
1.1352 + }
1.1353 +
1.1354 + /* If any auxiliary data functions have been called by this user function,
1.1355 + ** immediately call the destructor for any non-static values.
1.1356 + */
1.1357 + if( ctx.pVdbeFunc ){
1.1358 + sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
1.1359 + pOp->p4.pVdbeFunc = ctx.pVdbeFunc;
1.1360 + pOp->p4type = P4_VDBEFUNC;
1.1361 + }
1.1362 +
1.1363 + /* If the function returned an error, throw an exception */
1.1364 + if( ctx.isError ){
1.1365 + sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
1.1366 + rc = ctx.isError;
1.1367 + }
1.1368 +
1.1369 + /* Copy the result of the function into register P3 */
1.1370 + sqlite3VdbeChangeEncoding(&ctx.s, encoding);
1.1371 + sqlite3VdbeMemMove(pOut, &ctx.s);
1.1372 + if( sqlite3VdbeMemTooBig(pOut) ){
1.1373 + goto too_big;
1.1374 + }
1.1375 + REGISTER_TRACE(pOp->p3, pOut);
1.1376 + UPDATE_MAX_BLOBSIZE(pOut);
1.1377 + break;
1.1378 +}
1.1379 +
1.1380 +/* Opcode: BitAnd P1 P2 P3 * *
1.1381 +**
1.1382 +** Take the bit-wise AND of the values in register P1 and P2 and
1.1383 +** store the result in register P3.
1.1384 +** If either input is NULL, the result is NULL.
1.1385 +*/
1.1386 +/* Opcode: BitOr P1 P2 P3 * *
1.1387 +**
1.1388 +** Take the bit-wise OR of the values in register P1 and P2 and
1.1389 +** store the result in register P3.
1.1390 +** If either input is NULL, the result is NULL.
1.1391 +*/
1.1392 +/* Opcode: ShiftLeft P1 P2 P3 * *
1.1393 +**
1.1394 +** Shift the integer value in register P2 to the left by the
1.1395 +** number of bits specified by the integer in regiser P1.
1.1396 +** Store the result in register P3.
1.1397 +** If either input is NULL, the result is NULL.
1.1398 +*/
1.1399 +/* Opcode: ShiftRight P1 P2 P3 * *
1.1400 +**
1.1401 +** Shift the integer value in register P2 to the right by the
1.1402 +** number of bits specified by the integer in register P1.
1.1403 +** Store the result in register P3.
1.1404 +** If either input is NULL, the result is NULL.
1.1405 +*/
1.1406 +case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1.1407 +case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1.1408 +case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1.1409 +case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
1.1410 + i64 a, b;
1.1411 +
1.1412 + if( (pIn1->flags | pIn2->flags) & MEM_Null ){
1.1413 + sqlite3VdbeMemSetNull(pOut);
1.1414 + break;
1.1415 + }
1.1416 + a = sqlite3VdbeIntValue(pIn2);
1.1417 + b = sqlite3VdbeIntValue(pIn1);
1.1418 + switch( pOp->opcode ){
1.1419 + case OP_BitAnd: a &= b; break;
1.1420 + case OP_BitOr: a |= b; break;
1.1421 + case OP_ShiftLeft: a <<= b; break;
1.1422 + default: assert( pOp->opcode==OP_ShiftRight );
1.1423 + a >>= b; break;
1.1424 + }
1.1425 + pOut->u.i = a;
1.1426 + MemSetTypeFlag(pOut, MEM_Int);
1.1427 + break;
1.1428 +}
1.1429 +
1.1430 +/* Opcode: AddImm P1 P2 * * *
1.1431 +**
1.1432 +** Add the constant P2 to the value in register P1.
1.1433 +** The result is always an integer.
1.1434 +**
1.1435 +** To force any register to be an integer, just add 0.
1.1436 +*/
1.1437 +case OP_AddImm: { /* in1 */
1.1438 + sqlite3VdbeMemIntegerify(pIn1);
1.1439 + pIn1->u.i += pOp->p2;
1.1440 + break;
1.1441 +}
1.1442 +
1.1443 +/* Opcode: ForceInt P1 P2 P3 * *
1.1444 +**
1.1445 +** Convert value in register P1 into an integer. If the value
1.1446 +** in P1 is not numeric (meaning that is is a NULL or a string that
1.1447 +** does not look like an integer or floating point number) then
1.1448 +** jump to P2. If the value in P1 is numeric then
1.1449 +** convert it into the least integer that is greater than or equal to its
1.1450 +** current value if P3==0, or to the least integer that is strictly
1.1451 +** greater than its current value if P3==1.
1.1452 +*/
1.1453 +case OP_ForceInt: { /* jump, in1 */
1.1454 + i64 v;
1.1455 + applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1.1456 + if( (pIn1->flags & (MEM_Int|MEM_Real))==0 ){
1.1457 + pc = pOp->p2 - 1;
1.1458 + break;
1.1459 + }
1.1460 + if( pIn1->flags & MEM_Int ){
1.1461 + v = pIn1->u.i + (pOp->p3!=0);
1.1462 + }else{
1.1463 + assert( pIn1->flags & MEM_Real );
1.1464 + v = (sqlite3_int64)pIn1->r;
1.1465 + if( pIn1->r>(double)v ) v++;
1.1466 + if( pOp->p3 && pIn1->r==(double)v ) v++;
1.1467 + }
1.1468 + pIn1->u.i = v;
1.1469 + MemSetTypeFlag(pIn1, MEM_Int);
1.1470 + break;
1.1471 +}
1.1472 +
1.1473 +/* Opcode: MustBeInt P1 P2 * * *
1.1474 +**
1.1475 +** Force the value in register P1 to be an integer. If the value
1.1476 +** in P1 is not an integer and cannot be converted into an integer
1.1477 +** without data loss, then jump immediately to P2, or if P2==0
1.1478 +** raise an SQLITE_MISMATCH exception.
1.1479 +*/
1.1480 +case OP_MustBeInt: { /* jump, in1 */
1.1481 + applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1.1482 + if( (pIn1->flags & MEM_Int)==0 ){
1.1483 + if( pOp->p2==0 ){
1.1484 + rc = SQLITE_MISMATCH;
1.1485 + goto abort_due_to_error;
1.1486 + }else{
1.1487 + pc = pOp->p2 - 1;
1.1488 + }
1.1489 + }else{
1.1490 + MemSetTypeFlag(pIn1, MEM_Int);
1.1491 + }
1.1492 + break;
1.1493 +}
1.1494 +
1.1495 +/* Opcode: RealAffinity P1 * * * *
1.1496 +**
1.1497 +** If register P1 holds an integer convert it to a real value.
1.1498 +**
1.1499 +** This opcode is used when extracting information from a column that
1.1500 +** has REAL affinity. Such column values may still be stored as
1.1501 +** integers, for space efficiency, but after extraction we want them
1.1502 +** to have only a real value.
1.1503 +*/
1.1504 +case OP_RealAffinity: { /* in1 */
1.1505 + if( pIn1->flags & MEM_Int ){
1.1506 + sqlite3VdbeMemRealify(pIn1);
1.1507 + }
1.1508 + break;
1.1509 +}
1.1510 +
1.1511 +#ifndef SQLITE_OMIT_CAST
1.1512 +/* Opcode: ToText P1 * * * *
1.1513 +**
1.1514 +** Force the value in register P1 to be text.
1.1515 +** If the value is numeric, convert it to a string using the
1.1516 +** equivalent of printf(). Blob values are unchanged and
1.1517 +** are afterwards simply interpreted as text.
1.1518 +**
1.1519 +** A NULL value is not changed by this routine. It remains NULL.
1.1520 +*/
1.1521 +case OP_ToText: { /* same as TK_TO_TEXT, in1 */
1.1522 + if( pIn1->flags & MEM_Null ) break;
1.1523 + assert( MEM_Str==(MEM_Blob>>3) );
1.1524 + pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
1.1525 + applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
1.1526 + rc = ExpandBlob(pIn1);
1.1527 + assert( pIn1->flags & MEM_Str || db->mallocFailed );
1.1528 + pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob);
1.1529 + UPDATE_MAX_BLOBSIZE(pIn1);
1.1530 + break;
1.1531 +}
1.1532 +
1.1533 +/* Opcode: ToBlob P1 * * * *
1.1534 +**
1.1535 +** Force the value in register P1 to be a BLOB.
1.1536 +** If the value is numeric, convert it to a string first.
1.1537 +** Strings are simply reinterpreted as blobs with no change
1.1538 +** to the underlying data.
1.1539 +**
1.1540 +** A NULL value is not changed by this routine. It remains NULL.
1.1541 +*/
1.1542 +case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
1.1543 + if( pIn1->flags & MEM_Null ) break;
1.1544 + if( (pIn1->flags & MEM_Blob)==0 ){
1.1545 + applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
1.1546 + assert( pIn1->flags & MEM_Str || db->mallocFailed );
1.1547 + }
1.1548 + MemSetTypeFlag(pIn1, MEM_Blob);
1.1549 + UPDATE_MAX_BLOBSIZE(pIn1);
1.1550 + break;
1.1551 +}
1.1552 +
1.1553 +/* Opcode: ToNumeric P1 * * * *
1.1554 +**
1.1555 +** Force the value in register P1 to be numeric (either an
1.1556 +** integer or a floating-point number.)
1.1557 +** If the value is text or blob, try to convert it to an using the
1.1558 +** equivalent of atoi() or atof() and store 0 if no such conversion
1.1559 +** is possible.
1.1560 +**
1.1561 +** A NULL value is not changed by this routine. It remains NULL.
1.1562 +*/
1.1563 +case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
1.1564 + if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){
1.1565 + sqlite3VdbeMemNumerify(pIn1);
1.1566 + }
1.1567 + break;
1.1568 +}
1.1569 +#endif /* SQLITE_OMIT_CAST */
1.1570 +
1.1571 +/* Opcode: ToInt P1 * * * *
1.1572 +**
1.1573 +** Force the value in register P1 be an integer. If
1.1574 +** The value is currently a real number, drop its fractional part.
1.1575 +** If the value is text or blob, try to convert it to an integer using the
1.1576 +** equivalent of atoi() and store 0 if no such conversion is possible.
1.1577 +**
1.1578 +** A NULL value is not changed by this routine. It remains NULL.
1.1579 +*/
1.1580 +case OP_ToInt: { /* same as TK_TO_INT, in1 */
1.1581 + if( (pIn1->flags & MEM_Null)==0 ){
1.1582 + sqlite3VdbeMemIntegerify(pIn1);
1.1583 + }
1.1584 + break;
1.1585 +}
1.1586 +
1.1587 +#ifndef SQLITE_OMIT_CAST
1.1588 +/* Opcode: ToReal P1 * * * *
1.1589 +**
1.1590 +** Force the value in register P1 to be a floating point number.
1.1591 +** If The value is currently an integer, convert it.
1.1592 +** If the value is text or blob, try to convert it to an integer using the
1.1593 +** equivalent of atoi() and store 0.0 if no such conversion is possible.
1.1594 +**
1.1595 +** A NULL value is not changed by this routine. It remains NULL.
1.1596 +*/
1.1597 +case OP_ToReal: { /* same as TK_TO_REAL, in1 */
1.1598 + if( (pIn1->flags & MEM_Null)==0 ){
1.1599 + sqlite3VdbeMemRealify(pIn1);
1.1600 + }
1.1601 + break;
1.1602 +}
1.1603 +#endif /* SQLITE_OMIT_CAST */
1.1604 +
1.1605 +/* Opcode: Lt P1 P2 P3 P4 P5
1.1606 +**
1.1607 +** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1.1608 +** jump to address P2.
1.1609 +**
1.1610 +** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1.1611 +** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
1.1612 +** bit is clear then fall thru if either operand is NULL.
1.1613 +**
1.1614 +** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1.1615 +** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1.1616 +** to coerce both inputs according to this affinity before the
1.1617 +** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1.1618 +** affinity is used. Note that the affinity conversions are stored
1.1619 +** back into the input registers P1 and P3. So this opcode can cause
1.1620 +** persistent changes to registers P1 and P3.
1.1621 +**
1.1622 +** Once any conversions have taken place, and neither value is NULL,
1.1623 +** the values are compared. If both values are blobs then memcmp() is
1.1624 +** used to determine the results of the comparison. If both values
1.1625 +** are text, then the appropriate collating function specified in
1.1626 +** P4 is used to do the comparison. If P4 is not specified then
1.1627 +** memcmp() is used to compare text string. If both values are
1.1628 +** numeric, then a numeric comparison is used. If the two values
1.1629 +** are of different types, then numbers are considered less than
1.1630 +** strings and strings are considered less than blobs.
1.1631 +**
1.1632 +** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1.1633 +** store a boolean result (either 0, or 1, or NULL) in register P2.
1.1634 +*/
1.1635 +/* Opcode: Ne P1 P2 P3 P4 P5
1.1636 +**
1.1637 +** This works just like the Lt opcode except that the jump is taken if
1.1638 +** the operands in registers P1 and P3 are not equal. See the Lt opcode for
1.1639 +** additional information.
1.1640 +*/
1.1641 +/* Opcode: Eq P1 P2 P3 P4 P5
1.1642 +**
1.1643 +** This works just like the Lt opcode except that the jump is taken if
1.1644 +** the operands in registers P1 and P3 are equal.
1.1645 +** See the Lt opcode for additional information.
1.1646 +*/
1.1647 +/* Opcode: Le P1 P2 P3 P4 P5
1.1648 +**
1.1649 +** This works just like the Lt opcode except that the jump is taken if
1.1650 +** the content of register P3 is less than or equal to the content of
1.1651 +** register P1. See the Lt opcode for additional information.
1.1652 +*/
1.1653 +/* Opcode: Gt P1 P2 P3 P4 P5
1.1654 +**
1.1655 +** This works just like the Lt opcode except that the jump is taken if
1.1656 +** the content of register P3 is greater than the content of
1.1657 +** register P1. See the Lt opcode for additional information.
1.1658 +*/
1.1659 +/* Opcode: Ge P1 P2 P3 P4 P5
1.1660 +**
1.1661 +** This works just like the Lt opcode except that the jump is taken if
1.1662 +** the content of register P3 is greater than or equal to the content of
1.1663 +** register P1. See the Lt opcode for additional information.
1.1664 +*/
1.1665 +case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1.1666 +case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1.1667 +case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1.1668 +case OP_Le: /* same as TK_LE, jump, in1, in3 */
1.1669 +case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1.1670 +case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
1.1671 + int flags;
1.1672 + int res;
1.1673 + char affinity;
1.1674 +
1.1675 + flags = pIn1->flags|pIn3->flags;
1.1676 +
1.1677 + if( flags&MEM_Null ){
1.1678 + /* If either operand is NULL then the result is always NULL.
1.1679 + ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1.1680 + */
1.1681 + if( pOp->p5 & SQLITE_STOREP2 ){
1.1682 + pOut = &p->aMem[pOp->p2];
1.1683 + MemSetTypeFlag(pOut, MEM_Null);
1.1684 + REGISTER_TRACE(pOp->p2, pOut);
1.1685 + }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
1.1686 + pc = pOp->p2-1;
1.1687 + }
1.1688 + break;
1.1689 + }
1.1690 +
1.1691 + affinity = pOp->p5 & SQLITE_AFF_MASK;
1.1692 + if( affinity ){
1.1693 + applyAffinity(pIn1, affinity, encoding);
1.1694 + applyAffinity(pIn3, affinity, encoding);
1.1695 + }
1.1696 +
1.1697 + assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
1.1698 + ExpandBlob(pIn1);
1.1699 + ExpandBlob(pIn3);
1.1700 + res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
1.1701 + switch( pOp->opcode ){
1.1702 + case OP_Eq: res = res==0; break;
1.1703 + case OP_Ne: res = res!=0; break;
1.1704 + case OP_Lt: res = res<0; break;
1.1705 + case OP_Le: res = res<=0; break;
1.1706 + case OP_Gt: res = res>0; break;
1.1707 + default: res = res>=0; break;
1.1708 + }
1.1709 +
1.1710 + if( pOp->p5 & SQLITE_STOREP2 ){
1.1711 + pOut = &p->aMem[pOp->p2];
1.1712 + MemSetTypeFlag(pOut, MEM_Int);
1.1713 + pOut->u.i = res;
1.1714 + REGISTER_TRACE(pOp->p2, pOut);
1.1715 + }else if( res ){
1.1716 + pc = pOp->p2-1;
1.1717 + }
1.1718 + break;
1.1719 +}
1.1720 +
1.1721 +/* Opcode: Permutation * * * P4 *
1.1722 +**
1.1723 +** Set the permuation used by the OP_Compare operator to be the array
1.1724 +** of integers in P4.
1.1725 +**
1.1726 +** The permutation is only valid until the next OP_Permutation, OP_Compare,
1.1727 +** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur
1.1728 +** immediately prior to the OP_Compare.
1.1729 +*/
1.1730 +case OP_Permutation: {
1.1731 + assert( pOp->p4type==P4_INTARRAY );
1.1732 + assert( pOp->p4.ai );
1.1733 + aPermute = pOp->p4.ai;
1.1734 + break;
1.1735 +}
1.1736 +
1.1737 +/* Opcode: Compare P1 P2 P3 P4 *
1.1738 +**
1.1739 +** Compare to vectors of registers in reg(P1)..reg(P1+P3-1) (all this
1.1740 +** one "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
1.1741 +** the comparison for use by the next OP_Jump instruct.
1.1742 +**
1.1743 +** P4 is a KeyInfo structure that defines collating sequences and sort
1.1744 +** orders for the comparison. The permutation applies to registers
1.1745 +** only. The KeyInfo elements are used sequentially.
1.1746 +**
1.1747 +** The comparison is a sort comparison, so NULLs compare equal,
1.1748 +** NULLs are less than numbers, numbers are less than strings,
1.1749 +** and strings are less than blobs.
1.1750 +*/
1.1751 +case OP_Compare: {
1.1752 + int n = pOp->p3;
1.1753 + int i, p1, p2;
1.1754 + const KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
1.1755 + assert( n>0 );
1.1756 + assert( pKeyInfo!=0 );
1.1757 + p1 = pOp->p1;
1.1758 + assert( p1>0 && p1+n-1<p->nMem );
1.1759 + p2 = pOp->p2;
1.1760 + assert( p2>0 && p2+n-1<p->nMem );
1.1761 + for(i=0; i<n; i++){
1.1762 + int idx = aPermute ? aPermute[i] : i;
1.1763 + CollSeq *pColl; /* Collating sequence to use on this term */
1.1764 + int bRev; /* True for DESCENDING sort order */
1.1765 + REGISTER_TRACE(p1+idx, &p->aMem[p1+idx]);
1.1766 + REGISTER_TRACE(p2+idx, &p->aMem[p2+idx]);
1.1767 + assert( i<pKeyInfo->nField );
1.1768 + pColl = pKeyInfo->aColl[i];
1.1769 + bRev = pKeyInfo->aSortOrder[i];
1.1770 + iCompare = sqlite3MemCompare(&p->aMem[p1+idx], &p->aMem[p2+idx], pColl);
1.1771 + if( iCompare ){
1.1772 + if( bRev ) iCompare = -iCompare;
1.1773 + break;
1.1774 + }
1.1775 + }
1.1776 + aPermute = 0;
1.1777 + break;
1.1778 +}
1.1779 +
1.1780 +/* Opcode: Jump P1 P2 P3 * *
1.1781 +**
1.1782 +** Jump to the instruction at address P1, P2, or P3 depending on whether
1.1783 +** in the most recent OP_Compare instruction the P1 vector was less than
1.1784 +** equal to, or greater than the P2 vector, respectively.
1.1785 +*/
1.1786 +case OP_Jump: { /* jump */
1.1787 + if( iCompare<0 ){
1.1788 + pc = pOp->p1 - 1;
1.1789 + }else if( iCompare==0 ){
1.1790 + pc = pOp->p2 - 1;
1.1791 + }else{
1.1792 + pc = pOp->p3 - 1;
1.1793 + }
1.1794 + break;
1.1795 +}
1.1796 +
1.1797 +/* Opcode: And P1 P2 P3 * *
1.1798 +**
1.1799 +** Take the logical AND of the values in registers P1 and P2 and
1.1800 +** write the result into register P3.
1.1801 +**
1.1802 +** If either P1 or P2 is 0 (false) then the result is 0 even if
1.1803 +** the other input is NULL. A NULL and true or two NULLs give
1.1804 +** a NULL output.
1.1805 +*/
1.1806 +/* Opcode: Or P1 P2 P3 * *
1.1807 +**
1.1808 +** Take the logical OR of the values in register P1 and P2 and
1.1809 +** store the answer in register P3.
1.1810 +**
1.1811 +** If either P1 or P2 is nonzero (true) then the result is 1 (true)
1.1812 +** even if the other input is NULL. A NULL and false or two NULLs
1.1813 +** give a NULL output.
1.1814 +*/
1.1815 +case OP_And: /* same as TK_AND, in1, in2, out3 */
1.1816 +case OP_Or: { /* same as TK_OR, in1, in2, out3 */
1.1817 + int v1, v2; /* 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
1.1818 +
1.1819 + if( pIn1->flags & MEM_Null ){
1.1820 + v1 = 2;
1.1821 + }else{
1.1822 + v1 = sqlite3VdbeIntValue(pIn1)!=0;
1.1823 + }
1.1824 + if( pIn2->flags & MEM_Null ){
1.1825 + v2 = 2;
1.1826 + }else{
1.1827 + v2 = sqlite3VdbeIntValue(pIn2)!=0;
1.1828 + }
1.1829 + if( pOp->opcode==OP_And ){
1.1830 + static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
1.1831 + v1 = and_logic[v1*3+v2];
1.1832 + }else{
1.1833 + static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
1.1834 + v1 = or_logic[v1*3+v2];
1.1835 + }
1.1836 + if( v1==2 ){
1.1837 + MemSetTypeFlag(pOut, MEM_Null);
1.1838 + }else{
1.1839 + pOut->u.i = v1;
1.1840 + MemSetTypeFlag(pOut, MEM_Int);
1.1841 + }
1.1842 + break;
1.1843 +}
1.1844 +
1.1845 +
1.1846 +/* Opcode: Not P1 P2 * * *
1.1847 +**
1.1848 +** Interpret the value in register P1 as a boolean value. Store the
1.1849 +** boolean complement in register P2. If the value in register P1 is
1.1850 +** NULL, then a NULL is stored in P2.
1.1851 +*/
1.1852 +case OP_Not: { /* same as TK_NOT, in1 */
1.1853 + pOut = &p->aMem[pOp->p2];
1.1854 + if( pIn1->flags & MEM_Null ){
1.1855 + sqlite3VdbeMemSetNull(pOut);
1.1856 + }else{
1.1857 + sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
1.1858 + }
1.1859 + break;
1.1860 +}
1.1861 +
1.1862 +/* Opcode: BitNot P1 P2 * * *
1.1863 +**
1.1864 +** Interpret the content of register P1 as an integer. Store the
1.1865 +** ones-complement of the P1 value into register P2. If P1 holds
1.1866 +** a NULL then store a NULL in P2.
1.1867 +*/
1.1868 +case OP_BitNot: { /* same as TK_BITNOT, in1 */
1.1869 + pOut = &p->aMem[pOp->p2];
1.1870 + if( pIn1->flags & MEM_Null ){
1.1871 + sqlite3VdbeMemSetNull(pOut);
1.1872 + }else{
1.1873 + sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
1.1874 + }
1.1875 + break;
1.1876 +}
1.1877 +
1.1878 +/* Opcode: If P1 P2 P3 * *
1.1879 +**
1.1880 +** Jump to P2 if the value in register P1 is true. The value is
1.1881 +** is considered true if it is numeric and non-zero. If the value
1.1882 +** in P1 is NULL then take the jump if P3 is true.
1.1883 +*/
1.1884 +/* Opcode: IfNot P1 P2 P3 * *
1.1885 +**
1.1886 +** Jump to P2 if the value in register P1 is False. The value is
1.1887 +** is considered true if it has a numeric value of zero. If the value
1.1888 +** in P1 is NULL then take the jump if P3 is true.
1.1889 +*/
1.1890 +case OP_If: /* jump, in1 */
1.1891 +case OP_IfNot: { /* jump, in1 */
1.1892 + int c;
1.1893 + if( pIn1->flags & MEM_Null ){
1.1894 + c = pOp->p3;
1.1895 + }else{
1.1896 +#ifdef SQLITE_OMIT_FLOATING_POINT
1.1897 + c = sqlite3VdbeIntValue(pIn1);
1.1898 +#else
1.1899 + c = sqlite3VdbeRealValue(pIn1)!=0.0;
1.1900 +#endif
1.1901 + if( pOp->opcode==OP_IfNot ) c = !c;
1.1902 + }
1.1903 + if( c ){
1.1904 + pc = pOp->p2-1;
1.1905 + }
1.1906 + break;
1.1907 +}
1.1908 +
1.1909 +/* Opcode: IsNull P1 P2 P3 * *
1.1910 +**
1.1911 +** Jump to P2 if the value in register P1 is NULL. If P3 is greater
1.1912 +** than zero, then check all values reg(P1), reg(P1+1),
1.1913 +** reg(P1+2), ..., reg(P1+P3-1).
1.1914 +*/
1.1915 +case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
1.1916 + int n = pOp->p3;
1.1917 + assert( pOp->p3==0 || pOp->p1>0 );
1.1918 + do{
1.1919 + if( (pIn1->flags & MEM_Null)!=0 ){
1.1920 + pc = pOp->p2 - 1;
1.1921 + break;
1.1922 + }
1.1923 + pIn1++;
1.1924 + }while( --n > 0 );
1.1925 + break;
1.1926 +}
1.1927 +
1.1928 +/* Opcode: NotNull P1 P2 * * *
1.1929 +**
1.1930 +** Jump to P2 if the value in register P1 is not NULL.
1.1931 +*/
1.1932 +case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
1.1933 + if( (pIn1->flags & MEM_Null)==0 ){
1.1934 + pc = pOp->p2 - 1;
1.1935 + }
1.1936 + break;
1.1937 +}
1.1938 +
1.1939 +/* Opcode: SetNumColumns * P2 * * *
1.1940 +**
1.1941 +** This opcode sets the number of columns for the cursor opened by the
1.1942 +** following instruction to P2.
1.1943 +**
1.1944 +** An OP_SetNumColumns is only useful if it occurs immediately before
1.1945 +** one of the following opcodes:
1.1946 +**
1.1947 +** OpenRead
1.1948 +** OpenWrite
1.1949 +** OpenPseudo
1.1950 +**
1.1951 +** If the OP_Column opcode is to be executed on a cursor, then
1.1952 +** this opcode must be present immediately before the opcode that
1.1953 +** opens the cursor.
1.1954 +*/
1.1955 +case OP_SetNumColumns: {
1.1956 + break;
1.1957 +}
1.1958 +
1.1959 +/* Opcode: Column P1 P2 P3 P4 *
1.1960 +**
1.1961 +** Interpret the data that cursor P1 points to as a structure built using
1.1962 +** the MakeRecord instruction. (See the MakeRecord opcode for additional
1.1963 +** information about the format of the data.) Extract the P2-th column
1.1964 +** from this record. If there are less that (P2+1)
1.1965 +** values in the record, extract a NULL.
1.1966 +**
1.1967 +** The value extracted is stored in register P3.
1.1968 +**
1.1969 +** If the KeyAsData opcode has previously executed on this cursor, then the
1.1970 +** field might be extracted from the key rather than the data.
1.1971 +**
1.1972 +** If the column contains fewer than P2 fields, then extract a NULL. Or,
1.1973 +** if the P4 argument is a P4_MEM use the value of the P4 argument as
1.1974 +** the result.
1.1975 +*/
1.1976 +case OP_Column: {
1.1977 + u32 payloadSize; /* Number of bytes in the record */
1.1978 + int p1 = pOp->p1; /* P1 value of the opcode */
1.1979 + int p2 = pOp->p2; /* column number to retrieve */
1.1980 + Cursor *pC = 0; /* The VDBE cursor */
1.1981 + char *zRec; /* Pointer to complete record-data */
1.1982 + BtCursor *pCrsr; /* The BTree cursor */
1.1983 + u32 *aType; /* aType[i] holds the numeric type of the i-th column */
1.1984 + u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
1.1985 + u32 nField; /* number of fields in the record */
1.1986 + int len; /* The length of the serialized data for the column */
1.1987 + int i; /* Loop counter */
1.1988 + char *zData; /* Part of the record being decoded */
1.1989 + Mem *pDest; /* Where to write the extracted value */
1.1990 + Mem sMem; /* For storing the record being decoded */
1.1991 +
1.1992 + sMem.flags = 0;
1.1993 + sMem.db = 0;
1.1994 + sMem.zMalloc = 0;
1.1995 + assert( p1<p->nCursor );
1.1996 + assert( pOp->p3>0 && pOp->p3<=p->nMem );
1.1997 + pDest = &p->aMem[pOp->p3];
1.1998 + MemSetTypeFlag(pDest, MEM_Null);
1.1999 +
1.2000 + /* This block sets the variable payloadSize to be the total number of
1.2001 + ** bytes in the record.
1.2002 + **
1.2003 + ** zRec is set to be the complete text of the record if it is available.
1.2004 + ** The complete record text is always available for pseudo-tables
1.2005 + ** If the record is stored in a cursor, the complete record text
1.2006 + ** might be available in the pC->aRow cache. Or it might not be.
1.2007 + ** If the data is unavailable, zRec is set to NULL.
1.2008 + **
1.2009 + ** We also compute the number of columns in the record. For cursors,
1.2010 + ** the number of columns is stored in the Cursor.nField element.
1.2011 + */
1.2012 + pC = p->apCsr[p1];
1.2013 + assert( pC!=0 );
1.2014 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.2015 + assert( pC->pVtabCursor==0 );
1.2016 +#endif
1.2017 + if( pC->pCursor!=0 ){
1.2018 + /* The record is stored in a B-Tree */
1.2019 + rc = sqlite3VdbeCursorMoveto(pC);
1.2020 + if( rc ) goto abort_due_to_error;
1.2021 + zRec = 0;
1.2022 + pCrsr = pC->pCursor;
1.2023 + if( pC->nullRow ){
1.2024 + payloadSize = 0;
1.2025 + }else if( pC->cacheStatus==p->cacheCtr ){
1.2026 + payloadSize = pC->payloadSize;
1.2027 + zRec = (char*)pC->aRow;
1.2028 + }else if( pC->isIndex ){
1.2029 + i64 payloadSize64;
1.2030 + sqlite3BtreeKeySize(pCrsr, &payloadSize64);
1.2031 + payloadSize = payloadSize64;
1.2032 + }else{
1.2033 + sqlite3BtreeDataSize(pCrsr, &payloadSize);
1.2034 + }
1.2035 + nField = pC->nField;
1.2036 + }else{
1.2037 + assert( pC->pseudoTable );
1.2038 + /* The record is the sole entry of a pseudo-table */
1.2039 + payloadSize = pC->nData;
1.2040 + zRec = pC->pData;
1.2041 + pC->cacheStatus = CACHE_STALE;
1.2042 + assert( payloadSize==0 || zRec!=0 );
1.2043 + nField = pC->nField;
1.2044 + pCrsr = 0;
1.2045 + }
1.2046 +
1.2047 + /* If payloadSize is 0, then just store a NULL */
1.2048 + if( payloadSize==0 ){
1.2049 + assert( pDest->flags&MEM_Null );
1.2050 + goto op_column_out;
1.2051 + }
1.2052 + if( payloadSize>db->aLimit[SQLITE_LIMIT_LENGTH] ){
1.2053 + goto too_big;
1.2054 + }
1.2055 +
1.2056 + assert( p2<nField );
1.2057 +
1.2058 + /* Read and parse the table header. Store the results of the parse
1.2059 + ** into the record header cache fields of the cursor.
1.2060 + */
1.2061 + aType = pC->aType;
1.2062 + if( pC->cacheStatus==p->cacheCtr ){
1.2063 + aOffset = pC->aOffset;
1.2064 + }else{
1.2065 + u8 *zIdx; /* Index into header */
1.2066 + u8 *zEndHdr; /* Pointer to first byte after the header */
1.2067 + u32 offset; /* Offset into the data */
1.2068 + int szHdrSz; /* Size of the header size field at start of record */
1.2069 + int avail; /* Number of bytes of available data */
1.2070 +
1.2071 + assert(aType);
1.2072 + pC->aOffset = aOffset = &aType[nField];
1.2073 + pC->payloadSize = payloadSize;
1.2074 + pC->cacheStatus = p->cacheCtr;
1.2075 +
1.2076 + /* Figure out how many bytes are in the header */
1.2077 + if( zRec ){
1.2078 + zData = zRec;
1.2079 + }else{
1.2080 + if( pC->isIndex ){
1.2081 + zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
1.2082 + }else{
1.2083 + zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
1.2084 + }
1.2085 + /* If KeyFetch()/DataFetch() managed to get the entire payload,
1.2086 + ** save the payload in the pC->aRow cache. That will save us from
1.2087 + ** having to make additional calls to fetch the content portion of
1.2088 + ** the record.
1.2089 + */
1.2090 + if( avail>=payloadSize ){
1.2091 + zRec = zData;
1.2092 + pC->aRow = (u8*)zData;
1.2093 + }else{
1.2094 + pC->aRow = 0;
1.2095 + }
1.2096 + }
1.2097 + /* The following assert is true in all cases accept when
1.2098 + ** the database file has been corrupted externally.
1.2099 + ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
1.2100 + szHdrSz = getVarint32((u8*)zData, offset);
1.2101 +
1.2102 + /* The KeyFetch() or DataFetch() above are fast and will get the entire
1.2103 + ** record header in most cases. But they will fail to get the complete
1.2104 + ** record header if the record header does not fit on a single page
1.2105 + ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
1.2106 + ** acquire the complete header text.
1.2107 + */
1.2108 + if( !zRec && avail<offset ){
1.2109 + sMem.flags = 0;
1.2110 + sMem.db = 0;
1.2111 + rc = sqlite3VdbeMemFromBtree(pCrsr, 0, offset, pC->isIndex, &sMem);
1.2112 + if( rc!=SQLITE_OK ){
1.2113 + goto op_column_out;
1.2114 + }
1.2115 + zData = sMem.z;
1.2116 + }
1.2117 + zEndHdr = (u8 *)&zData[offset];
1.2118 + zIdx = (u8 *)&zData[szHdrSz];
1.2119 +
1.2120 + /* Scan the header and use it to fill in the aType[] and aOffset[]
1.2121 + ** arrays. aType[i] will contain the type integer for the i-th
1.2122 + ** column and aOffset[i] will contain the offset from the beginning
1.2123 + ** of the record to the start of the data for the i-th column
1.2124 + */
1.2125 + for(i=0; i<nField; i++){
1.2126 + if( zIdx<zEndHdr ){
1.2127 + aOffset[i] = offset;
1.2128 + zIdx += getVarint32(zIdx, aType[i]);
1.2129 + offset += sqlite3VdbeSerialTypeLen(aType[i]);
1.2130 + }else{
1.2131 + /* If i is less that nField, then there are less fields in this
1.2132 + ** record than SetNumColumns indicated there are columns in the
1.2133 + ** table. Set the offset for any extra columns not present in
1.2134 + ** the record to 0. This tells code below to store a NULL
1.2135 + ** instead of deserializing a value from the record.
1.2136 + */
1.2137 + aOffset[i] = 0;
1.2138 + }
1.2139 + }
1.2140 + sqlite3VdbeMemRelease(&sMem);
1.2141 + sMem.flags = MEM_Null;
1.2142 +
1.2143 + /* If we have read more header data than was contained in the header,
1.2144 + ** or if the end of the last field appears to be past the end of the
1.2145 + ** record, or if the end of the last field appears to be before the end
1.2146 + ** of the record (when all fields present), then we must be dealing
1.2147 + ** with a corrupt database.
1.2148 + */
1.2149 + if( zIdx>zEndHdr || offset>payloadSize
1.2150 + || (zIdx==zEndHdr && offset!=payloadSize) ){
1.2151 + rc = SQLITE_CORRUPT_BKPT;
1.2152 + goto op_column_out;
1.2153 + }
1.2154 + }
1.2155 +
1.2156 + /* Get the column information. If aOffset[p2] is non-zero, then
1.2157 + ** deserialize the value from the record. If aOffset[p2] is zero,
1.2158 + ** then there are not enough fields in the record to satisfy the
1.2159 + ** request. In this case, set the value NULL or to P4 if P4 is
1.2160 + ** a pointer to a Mem object.
1.2161 + */
1.2162 + if( aOffset[p2] ){
1.2163 + assert( rc==SQLITE_OK );
1.2164 + if( zRec ){
1.2165 + sqlite3VdbeMemReleaseExternal(pDest);
1.2166 + sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
1.2167 + }else{
1.2168 + len = sqlite3VdbeSerialTypeLen(aType[p2]);
1.2169 + sqlite3VdbeMemMove(&sMem, pDest);
1.2170 + rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
1.2171 + if( rc!=SQLITE_OK ){
1.2172 + goto op_column_out;
1.2173 + }
1.2174 + zData = sMem.z;
1.2175 + sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest);
1.2176 + }
1.2177 + pDest->enc = encoding;
1.2178 + }else{
1.2179 + if( pOp->p4type==P4_MEM ){
1.2180 + sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
1.2181 + }else{
1.2182 + assert( pDest->flags&MEM_Null );
1.2183 + }
1.2184 + }
1.2185 +
1.2186 + /* If we dynamically allocated space to hold the data (in the
1.2187 + ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
1.2188 + ** dynamically allocated space over to the pDest structure.
1.2189 + ** This prevents a memory copy.
1.2190 + */
1.2191 + if( sMem.zMalloc ){
1.2192 + assert( sMem.z==sMem.zMalloc );
1.2193 + assert( !(pDest->flags & MEM_Dyn) );
1.2194 + assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
1.2195 + pDest->flags &= ~(MEM_Ephem|MEM_Static);
1.2196 + pDest->flags |= MEM_Term;
1.2197 + pDest->z = sMem.z;
1.2198 + pDest->zMalloc = sMem.zMalloc;
1.2199 + }
1.2200 +
1.2201 + rc = sqlite3VdbeMemMakeWriteable(pDest);
1.2202 +
1.2203 +op_column_out:
1.2204 + UPDATE_MAX_BLOBSIZE(pDest);
1.2205 + REGISTER_TRACE(pOp->p3, pDest);
1.2206 + break;
1.2207 +}
1.2208 +
1.2209 +/* Opcode: Affinity P1 P2 * P4 *
1.2210 +**
1.2211 +** Apply affinities to a range of P2 registers starting with P1.
1.2212 +**
1.2213 +** P4 is a string that is P2 characters long. The nth character of the
1.2214 +** string indicates the column affinity that should be used for the nth
1.2215 +** memory cell in the range.
1.2216 +*/
1.2217 +case OP_Affinity: {
1.2218 + char *zAffinity = pOp->p4.z;
1.2219 + Mem *pData0 = &p->aMem[pOp->p1];
1.2220 + Mem *pLast = &pData0[pOp->p2-1];
1.2221 + Mem *pRec;
1.2222 +
1.2223 + for(pRec=pData0; pRec<=pLast; pRec++){
1.2224 + ExpandBlob(pRec);
1.2225 + applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
1.2226 + }
1.2227 + break;
1.2228 +}
1.2229 +
1.2230 +/* Opcode: MakeRecord P1 P2 P3 P4 *
1.2231 +**
1.2232 +** Convert P2 registers beginning with P1 into a single entry
1.2233 +** suitable for use as a data record in a database table or as a key
1.2234 +** in an index. The details of the format are irrelevant as long as
1.2235 +** the OP_Column opcode can decode the record later.
1.2236 +** Refer to source code comments for the details of the record
1.2237 +** format.
1.2238 +**
1.2239 +** P4 may be a string that is P2 characters long. The nth character of the
1.2240 +** string indicates the column affinity that should be used for the nth
1.2241 +** field of the index key.
1.2242 +**
1.2243 +** The mapping from character to affinity is given by the SQLITE_AFF_
1.2244 +** macros defined in sqliteInt.h.
1.2245 +**
1.2246 +** If P4 is NULL then all index fields have the affinity NONE.
1.2247 +*/
1.2248 +case OP_MakeRecord: {
1.2249 + /* Assuming the record contains N fields, the record format looks
1.2250 + ** like this:
1.2251 + **
1.2252 + ** ------------------------------------------------------------------------
1.2253 + ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
1.2254 + ** ------------------------------------------------------------------------
1.2255 + **
1.2256 + ** Data(0) is taken from register P1. Data(1) comes from register P1+1
1.2257 + ** and so froth.
1.2258 + **
1.2259 + ** Each type field is a varint representing the serial type of the
1.2260 + ** corresponding data element (see sqlite3VdbeSerialType()). The
1.2261 + ** hdr-size field is also a varint which is the offset from the beginning
1.2262 + ** of the record to data0.
1.2263 + */
1.2264 + u8 *zNewRecord; /* A buffer to hold the data for the new record */
1.2265 + Mem *pRec; /* The new record */
1.2266 + u64 nData = 0; /* Number of bytes of data space */
1.2267 + int nHdr = 0; /* Number of bytes of header space */
1.2268 + u64 nByte = 0; /* Data space required for this record */
1.2269 + int nZero = 0; /* Number of zero bytes at the end of the record */
1.2270 + int nVarint; /* Number of bytes in a varint */
1.2271 + u32 serial_type; /* Type field */
1.2272 + Mem *pData0; /* First field to be combined into the record */
1.2273 + Mem *pLast; /* Last field of the record */
1.2274 + int nField; /* Number of fields in the record */
1.2275 + char *zAffinity; /* The affinity string for the record */
1.2276 + int file_format; /* File format to use for encoding */
1.2277 + int i; /* Space used in zNewRecord[] */
1.2278 +
1.2279 + nField = pOp->p1;
1.2280 + zAffinity = pOp->p4.z;
1.2281 + assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem );
1.2282 + pData0 = &p->aMem[nField];
1.2283 + nField = pOp->p2;
1.2284 + pLast = &pData0[nField-1];
1.2285 + file_format = p->minWriteFileFormat;
1.2286 +
1.2287 + /* Loop through the elements that will make up the record to figure
1.2288 + ** out how much space is required for the new record.
1.2289 + */
1.2290 + for(pRec=pData0; pRec<=pLast; pRec++){
1.2291 + int len;
1.2292 + if( zAffinity ){
1.2293 + applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
1.2294 + }
1.2295 + if( pRec->flags&MEM_Zero && pRec->n>0 ){
1.2296 + sqlite3VdbeMemExpandBlob(pRec);
1.2297 + }
1.2298 + serial_type = sqlite3VdbeSerialType(pRec, file_format);
1.2299 + len = sqlite3VdbeSerialTypeLen(serial_type);
1.2300 + nData += len;
1.2301 + nHdr += sqlite3VarintLen(serial_type);
1.2302 + if( pRec->flags & MEM_Zero ){
1.2303 + /* Only pure zero-filled BLOBs can be input to this Opcode.
1.2304 + ** We do not allow blobs with a prefix and a zero-filled tail. */
1.2305 + nZero += pRec->u.i;
1.2306 + }else if( len ){
1.2307 + nZero = 0;
1.2308 + }
1.2309 + }
1.2310 +
1.2311 + /* Add the initial header varint and total the size */
1.2312 + nHdr += nVarint = sqlite3VarintLen(nHdr);
1.2313 + if( nVarint<sqlite3VarintLen(nHdr) ){
1.2314 + nHdr++;
1.2315 + }
1.2316 + nByte = nHdr+nData-nZero;
1.2317 + if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
1.2318 + goto too_big;
1.2319 + }
1.2320 +
1.2321 + /* Make sure the output register has a buffer large enough to store
1.2322 + ** the new record. The output register (pOp->p3) is not allowed to
1.2323 + ** be one of the input registers (because the following call to
1.2324 + ** sqlite3VdbeMemGrow() could clobber the value before it is used).
1.2325 + */
1.2326 + assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
1.2327 + pOut = &p->aMem[pOp->p3];
1.2328 + if( sqlite3VdbeMemGrow(pOut, nByte, 0) ){
1.2329 + goto no_mem;
1.2330 + }
1.2331 + zNewRecord = (u8 *)pOut->z;
1.2332 +
1.2333 + /* Write the record */
1.2334 + i = putVarint32(zNewRecord, nHdr);
1.2335 + for(pRec=pData0; pRec<=pLast; pRec++){
1.2336 + serial_type = sqlite3VdbeSerialType(pRec, file_format);
1.2337 + i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
1.2338 + }
1.2339 + for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */
1.2340 + i += sqlite3VdbeSerialPut(&zNewRecord[i], nByte-i, pRec, file_format);
1.2341 + }
1.2342 + assert( i==nByte );
1.2343 +
1.2344 + assert( pOp->p3>0 && pOp->p3<=p->nMem );
1.2345 + pOut->n = nByte;
1.2346 + pOut->flags = MEM_Blob | MEM_Dyn;
1.2347 + pOut->xDel = 0;
1.2348 + if( nZero ){
1.2349 + pOut->u.i = nZero;
1.2350 + pOut->flags |= MEM_Zero;
1.2351 + }
1.2352 + pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
1.2353 + REGISTER_TRACE(pOp->p3, pOut);
1.2354 + UPDATE_MAX_BLOBSIZE(pOut);
1.2355 + break;
1.2356 +}
1.2357 +
1.2358 +/* Opcode: Statement P1 * * * *
1.2359 +**
1.2360 +** Begin an individual statement transaction which is part of a larger
1.2361 +** transaction. This is needed so that the statement
1.2362 +** can be rolled back after an error without having to roll back the
1.2363 +** entire transaction. The statement transaction will automatically
1.2364 +** commit when the VDBE halts.
1.2365 +**
1.2366 +** If the database connection is currently in autocommit mode (that
1.2367 +** is to say, if it is in between BEGIN and COMMIT)
1.2368 +** and if there are no other active statements on the same database
1.2369 +** connection, then this operation is a no-op. No statement transaction
1.2370 +** is needed since any error can use the normal ROLLBACK process to
1.2371 +** undo changes.
1.2372 +**
1.2373 +** If a statement transaction is started, then a statement journal file
1.2374 +** will be allocated and initialized.
1.2375 +**
1.2376 +** The statement is begun on the database file with index P1. The main
1.2377 +** database file has an index of 0 and the file used for temporary tables
1.2378 +** has an index of 1.
1.2379 +*/
1.2380 +case OP_Statement: {
1.2381 + if( db->autoCommit==0 || db->activeVdbeCnt>1 ){
1.2382 + int i = pOp->p1;
1.2383 + Btree *pBt;
1.2384 + assert( i>=0 && i<db->nDb );
1.2385 + assert( db->aDb[i].pBt!=0 );
1.2386 + pBt = db->aDb[i].pBt;
1.2387 + assert( sqlite3BtreeIsInTrans(pBt) );
1.2388 + assert( (p->btreeMask & (1<<i))!=0 );
1.2389 + if( !sqlite3BtreeIsInStmt(pBt) ){
1.2390 + rc = sqlite3BtreeBeginStmt(pBt);
1.2391 + p->openedStatement = 1;
1.2392 + }
1.2393 + }
1.2394 + break;
1.2395 +}
1.2396 +
1.2397 +/* Opcode: AutoCommit P1 P2 * * *
1.2398 +**
1.2399 +** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
1.2400 +** back any currently active btree transactions. If there are any active
1.2401 +** VMs (apart from this one), then the COMMIT or ROLLBACK statement fails.
1.2402 +**
1.2403 +** This instruction causes the VM to halt.
1.2404 +*/
1.2405 +case OP_AutoCommit: {
1.2406 + u8 i = pOp->p1;
1.2407 + u8 rollback = pOp->p2;
1.2408 +
1.2409 + assert( i==1 || i==0 );
1.2410 + assert( i==1 || rollback==0 );
1.2411 +
1.2412 + assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
1.2413 +
1.2414 + if( db->activeVdbeCnt>1 && i && !db->autoCommit ){
1.2415 + /* If this instruction implements a COMMIT or ROLLBACK, other VMs are
1.2416 + ** still running, and a transaction is active, return an error indicating
1.2417 + ** that the other VMs must complete first.
1.2418 + */
1.2419 + sqlite3SetString(&p->zErrMsg, db, "cannot %s transaction - "
1.2420 + "SQL statements in progress",
1.2421 + rollback ? "rollback" : "commit");
1.2422 + rc = SQLITE_ERROR;
1.2423 + }else if( i!=db->autoCommit ){
1.2424 + if( pOp->p2 ){
1.2425 + assert( i==1 );
1.2426 + sqlite3RollbackAll(db);
1.2427 + db->autoCommit = 1;
1.2428 + }else{
1.2429 + db->autoCommit = i;
1.2430 + if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
1.2431 + p->pc = pc;
1.2432 + db->autoCommit = 1-i;
1.2433 + p->rc = rc = SQLITE_BUSY;
1.2434 + goto vdbe_return;
1.2435 + }
1.2436 + }
1.2437 + if( p->rc==SQLITE_OK ){
1.2438 + rc = SQLITE_DONE;
1.2439 + }else{
1.2440 + rc = SQLITE_ERROR;
1.2441 + }
1.2442 + goto vdbe_return;
1.2443 + }else{
1.2444 + sqlite3SetString(&p->zErrMsg, db,
1.2445 + (!i)?"cannot start a transaction within a transaction":(
1.2446 + (rollback)?"cannot rollback - no transaction is active":
1.2447 + "cannot commit - no transaction is active"));
1.2448 +
1.2449 + rc = SQLITE_ERROR;
1.2450 + }
1.2451 + break;
1.2452 +}
1.2453 +
1.2454 +/* Opcode: Transaction P1 P2 * * *
1.2455 +**
1.2456 +** Begin a transaction. The transaction ends when a Commit or Rollback
1.2457 +** opcode is encountered. Depending on the ON CONFLICT setting, the
1.2458 +** transaction might also be rolled back if an error is encountered.
1.2459 +**
1.2460 +** P1 is the index of the database file on which the transaction is
1.2461 +** started. Index 0 is the main database file and index 1 is the
1.2462 +** file used for temporary tables. Indices of 2 or more are used for
1.2463 +** attached databases.
1.2464 +**
1.2465 +** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
1.2466 +** obtained on the database file when a write-transaction is started. No
1.2467 +** other process can start another write transaction while this transaction is
1.2468 +** underway. Starting a write transaction also creates a rollback journal. A
1.2469 +** write transaction must be started before any changes can be made to the
1.2470 +** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
1.2471 +** on the file.
1.2472 +**
1.2473 +** If P2 is zero, then a read-lock is obtained on the database file.
1.2474 +*/
1.2475 +case OP_Transaction: {
1.2476 + int i = pOp->p1;
1.2477 + Btree *pBt;
1.2478 +
1.2479 + assert( i>=0 && i<db->nDb );
1.2480 + assert( (p->btreeMask & (1<<i))!=0 );
1.2481 + pBt = db->aDb[i].pBt;
1.2482 +
1.2483 + if( pBt ){
1.2484 + rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
1.2485 + if( rc==SQLITE_BUSY ){
1.2486 + p->pc = pc;
1.2487 + p->rc = rc = SQLITE_BUSY;
1.2488 + goto vdbe_return;
1.2489 + }
1.2490 + if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){
1.2491 + goto abort_due_to_error;
1.2492 + }
1.2493 + }
1.2494 + break;
1.2495 +}
1.2496 +
1.2497 +/* Opcode: ReadCookie P1 P2 P3 * *
1.2498 +**
1.2499 +** Read cookie number P3 from database P1 and write it into register P2.
1.2500 +** P3==0 is the schema version. P3==1 is the database format.
1.2501 +** P3==2 is the recommended pager cache size, and so forth. P1==0 is
1.2502 +** the main database file and P1==1 is the database file used to store
1.2503 +** temporary tables.
1.2504 +**
1.2505 +** If P1 is negative, then this is a request to read the size of a
1.2506 +** databases free-list. P3 must be set to 1 in this case. The actual
1.2507 +** database accessed is ((P1+1)*-1). For example, a P1 parameter of -1
1.2508 +** corresponds to database 0 ("main"), a P1 of -2 is database 1 ("temp").
1.2509 +**
1.2510 +** There must be a read-lock on the database (either a transaction
1.2511 +** must be started or there must be an open cursor) before
1.2512 +** executing this instruction.
1.2513 +*/
1.2514 +case OP_ReadCookie: { /* out2-prerelease */
1.2515 + int iMeta;
1.2516 + int iDb = pOp->p1;
1.2517 + int iCookie = pOp->p3;
1.2518 +
1.2519 + assert( pOp->p3<SQLITE_N_BTREE_META );
1.2520 + if( iDb<0 ){
1.2521 + iDb = (-1*(iDb+1));
1.2522 + iCookie *= -1;
1.2523 + }
1.2524 + assert( iDb>=0 && iDb<db->nDb );
1.2525 + assert( db->aDb[iDb].pBt!=0 );
1.2526 + assert( (p->btreeMask & (1<<iDb))!=0 );
1.2527 + /* The indexing of meta values at the schema layer is off by one from
1.2528 + ** the indexing in the btree layer. The btree considers meta[0] to
1.2529 + ** be the number of free pages in the database (a read-only value)
1.2530 + ** and meta[1] to be the schema cookie. The schema layer considers
1.2531 + ** meta[1] to be the schema cookie. So we have to shift the index
1.2532 + ** by one in the following statement.
1.2533 + */
1.2534 + rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, 1 + iCookie, (u32 *)&iMeta);
1.2535 + pOut->u.i = iMeta;
1.2536 + MemSetTypeFlag(pOut, MEM_Int);
1.2537 + break;
1.2538 +}
1.2539 +
1.2540 +/* Opcode: SetCookie P1 P2 P3 * *
1.2541 +**
1.2542 +** Write the content of register P3 (interpreted as an integer)
1.2543 +** into cookie number P2 of database P1.
1.2544 +** P2==0 is the schema version. P2==1 is the database format.
1.2545 +** P2==2 is the recommended pager cache size, and so forth. P1==0 is
1.2546 +** the main database file and P1==1 is the database file used to store
1.2547 +** temporary tables.
1.2548 +**
1.2549 +** A transaction must be started before executing this opcode.
1.2550 +*/
1.2551 +case OP_SetCookie: { /* in3 */
1.2552 + Db *pDb;
1.2553 + assert( pOp->p2<SQLITE_N_BTREE_META );
1.2554 + assert( pOp->p1>=0 && pOp->p1<db->nDb );
1.2555 + assert( (p->btreeMask & (1<<pOp->p1))!=0 );
1.2556 + pDb = &db->aDb[pOp->p1];
1.2557 + assert( pDb->pBt!=0 );
1.2558 + sqlite3VdbeMemIntegerify(pIn3);
1.2559 + /* See note about index shifting on OP_ReadCookie */
1.2560 + rc = sqlite3BtreeUpdateMeta(pDb->pBt, 1+pOp->p2, (int)pIn3->u.i);
1.2561 + if( pOp->p2==0 ){
1.2562 + /* When the schema cookie changes, record the new cookie internally */
1.2563 + pDb->pSchema->schema_cookie = pIn3->u.i;
1.2564 + db->flags |= SQLITE_InternChanges;
1.2565 + }else if( pOp->p2==1 ){
1.2566 + /* Record changes in the file format */
1.2567 + pDb->pSchema->file_format = pIn3->u.i;
1.2568 + }
1.2569 + if( pOp->p1==1 ){
1.2570 + /* Invalidate all prepared statements whenever the TEMP database
1.2571 + ** schema is changed. Ticket #1644 */
1.2572 + sqlite3ExpirePreparedStatements(db);
1.2573 + }
1.2574 + break;
1.2575 +}
1.2576 +
1.2577 +/* Opcode: VerifyCookie P1 P2 *
1.2578 +**
1.2579 +** Check the value of global database parameter number 0 (the
1.2580 +** schema version) and make sure it is equal to P2.
1.2581 +** P1 is the database number which is 0 for the main database file
1.2582 +** and 1 for the file holding temporary tables and some higher number
1.2583 +** for auxiliary databases.
1.2584 +**
1.2585 +** The cookie changes its value whenever the database schema changes.
1.2586 +** This operation is used to detect when that the cookie has changed
1.2587 +** and that the current process needs to reread the schema.
1.2588 +**
1.2589 +** Either a transaction needs to have been started or an OP_Open needs
1.2590 +** to be executed (to establish a read lock) before this opcode is
1.2591 +** invoked.
1.2592 +*/
1.2593 +case OP_VerifyCookie: {
1.2594 + int iMeta;
1.2595 + Btree *pBt;
1.2596 + assert( pOp->p1>=0 && pOp->p1<db->nDb );
1.2597 + assert( (p->btreeMask & (1<<pOp->p1))!=0 );
1.2598 + pBt = db->aDb[pOp->p1].pBt;
1.2599 + if( pBt ){
1.2600 + rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&iMeta);
1.2601 + }else{
1.2602 + rc = SQLITE_OK;
1.2603 + iMeta = 0;
1.2604 + }
1.2605 + if( rc==SQLITE_OK && iMeta!=pOp->p2 ){
1.2606 + sqlite3DbFree(db, p->zErrMsg);
1.2607 + p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
1.2608 + /* If the schema-cookie from the database file matches the cookie
1.2609 + ** stored with the in-memory representation of the schema, do
1.2610 + ** not reload the schema from the database file.
1.2611 + **
1.2612 + ** If virtual-tables are in use, this is not just an optimization.
1.2613 + ** Often, v-tables store their data in other SQLite tables, which
1.2614 + ** are queried from within xNext() and other v-table methods using
1.2615 + ** prepared queries. If such a query is out-of-date, we do not want to
1.2616 + ** discard the database schema, as the user code implementing the
1.2617 + ** v-table would have to be ready for the sqlite3_vtab structure itself
1.2618 + ** to be invalidated whenever sqlite3_step() is called from within
1.2619 + ** a v-table method.
1.2620 + */
1.2621 + if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
1.2622 + sqlite3ResetInternalSchema(db, pOp->p1);
1.2623 + }
1.2624 +
1.2625 + sqlite3ExpirePreparedStatements(db);
1.2626 + rc = SQLITE_SCHEMA;
1.2627 + }
1.2628 + break;
1.2629 +}
1.2630 +
1.2631 +/* Opcode: OpenRead P1 P2 P3 P4 P5
1.2632 +**
1.2633 +** Open a read-only cursor for the database table whose root page is
1.2634 +** P2 in a database file. The database file is determined by P3.
1.2635 +** P3==0 means the main database, P3==1 means the database used for
1.2636 +** temporary tables, and P3>1 means used the corresponding attached
1.2637 +** database. Give the new cursor an identifier of P1. The P1
1.2638 +** values need not be contiguous but all P1 values should be small integers.
1.2639 +** It is an error for P1 to be negative.
1.2640 +**
1.2641 +** If P5!=0 then use the content of register P2 as the root page, not
1.2642 +** the value of P2 itself.
1.2643 +**
1.2644 +** There will be a read lock on the database whenever there is an
1.2645 +** open cursor. If the database was unlocked prior to this instruction
1.2646 +** then a read lock is acquired as part of this instruction. A read
1.2647 +** lock allows other processes to read the database but prohibits
1.2648 +** any other process from modifying the database. The read lock is
1.2649 +** released when all cursors are closed. If this instruction attempts
1.2650 +** to get a read lock but fails, the script terminates with an
1.2651 +** SQLITE_BUSY error code.
1.2652 +**
1.2653 +** The P4 value is a pointer to a KeyInfo structure that defines the
1.2654 +** content and collating sequence of indices. P4 is NULL for cursors
1.2655 +** that are not pointing to indices.
1.2656 +**
1.2657 +** See also OpenWrite.
1.2658 +*/
1.2659 +/* Opcode: OpenWrite P1 P2 P3 P4 P5
1.2660 +**
1.2661 +** Open a read/write cursor named P1 on the table or index whose root
1.2662 +** page is P2. Or if P5!=0 use the content of register P2 to find the
1.2663 +** root page.
1.2664 +**
1.2665 +** The P4 value is a pointer to a KeyInfo structure that defines the
1.2666 +** content and collating sequence of indices. P4 is NULL for cursors
1.2667 +** that are not pointing to indices.
1.2668 +**
1.2669 +** This instruction works just like OpenRead except that it opens the cursor
1.2670 +** in read/write mode. For a given table, there can be one or more read-only
1.2671 +** cursors or a single read/write cursor but not both.
1.2672 +**
1.2673 +** See also OpenRead.
1.2674 +*/
1.2675 +case OP_OpenRead:
1.2676 +case OP_OpenWrite: {
1.2677 + int i = pOp->p1;
1.2678 + int p2 = pOp->p2;
1.2679 + int iDb = pOp->p3;
1.2680 + int wrFlag;
1.2681 + Btree *pX;
1.2682 + Cursor *pCur;
1.2683 + Db *pDb;
1.2684 +
1.2685 + assert( iDb>=0 && iDb<db->nDb );
1.2686 + assert( (p->btreeMask & (1<<iDb))!=0 );
1.2687 + pDb = &db->aDb[iDb];
1.2688 + pX = pDb->pBt;
1.2689 + assert( pX!=0 );
1.2690 + if( pOp->opcode==OP_OpenWrite ){
1.2691 + wrFlag = 1;
1.2692 + if( pDb->pSchema->file_format < p->minWriteFileFormat ){
1.2693 + p->minWriteFileFormat = pDb->pSchema->file_format;
1.2694 + }
1.2695 + }else{
1.2696 + wrFlag = 0;
1.2697 + }
1.2698 + if( pOp->p5 ){
1.2699 + assert( p2>0 );
1.2700 + assert( p2<=p->nMem );
1.2701 + pIn2 = &p->aMem[p2];
1.2702 + sqlite3VdbeMemIntegerify(pIn2);
1.2703 + p2 = pIn2->u.i;
1.2704 + if( p2<2 ) {
1.2705 + rc = SQLITE_CORRUPT_BKPT;
1.2706 + goto abort_due_to_error;
1.2707 + }
1.2708 + }
1.2709 + assert( i>=0 );
1.2710 + pCur = allocateCursor(p, i, &pOp[-1], iDb, 1);
1.2711 + if( pCur==0 ) goto no_mem;
1.2712 + pCur->nullRow = 1;
1.2713 + rc = sqlite3BtreeCursor(pX, p2, wrFlag, pOp->p4.p, pCur->pCursor);
1.2714 + if( pOp->p4type==P4_KEYINFO ){
1.2715 + pCur->pKeyInfo = pOp->p4.pKeyInfo;
1.2716 + pCur->pKeyInfo->enc = ENC(p->db);
1.2717 + }else{
1.2718 + pCur->pKeyInfo = 0;
1.2719 + }
1.2720 + switch( rc ){
1.2721 + case SQLITE_BUSY: {
1.2722 + p->pc = pc;
1.2723 + p->rc = rc = SQLITE_BUSY;
1.2724 + goto vdbe_return;
1.2725 + }
1.2726 + case SQLITE_OK: {
1.2727 + int flags = sqlite3BtreeFlags(pCur->pCursor);
1.2728 + /* Sanity checking. Only the lower four bits of the flags byte should
1.2729 + ** be used. Bit 3 (mask 0x08) is unpredictable. The lower 3 bits
1.2730 + ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
1.2731 + ** 2 (zerodata for indices). If these conditions are not met it can
1.2732 + ** only mean that we are dealing with a corrupt database file
1.2733 + */
1.2734 + if( (flags & 0xf0)!=0 || ((flags & 0x07)!=5 && (flags & 0x07)!=2) ){
1.2735 + rc = SQLITE_CORRUPT_BKPT;
1.2736 + goto abort_due_to_error;
1.2737 + }
1.2738 + pCur->isTable = (flags & BTREE_INTKEY)!=0;
1.2739 + pCur->isIndex = (flags & BTREE_ZERODATA)!=0;
1.2740 + /* If P4==0 it means we are expected to open a table. If P4!=0 then
1.2741 + ** we expect to be opening an index. If this is not what happened,
1.2742 + ** then the database is corrupt
1.2743 + */
1.2744 + if( (pCur->isTable && pOp->p4type==P4_KEYINFO)
1.2745 + || (pCur->isIndex && pOp->p4type!=P4_KEYINFO) ){
1.2746 + rc = SQLITE_CORRUPT_BKPT;
1.2747 + goto abort_due_to_error;
1.2748 + }
1.2749 + break;
1.2750 + }
1.2751 + case SQLITE_EMPTY: {
1.2752 + pCur->isTable = pOp->p4type!=P4_KEYINFO;
1.2753 + pCur->isIndex = !pCur->isTable;
1.2754 + pCur->pCursor = 0;
1.2755 + rc = SQLITE_OK;
1.2756 + break;
1.2757 + }
1.2758 + default: {
1.2759 + goto abort_due_to_error;
1.2760 + }
1.2761 + }
1.2762 + break;
1.2763 +}
1.2764 +
1.2765 +/* Opcode: OpenEphemeral P1 P2 * P4 *
1.2766 +**
1.2767 +** Open a new cursor P1 to a transient table.
1.2768 +** The cursor is always opened read/write even if
1.2769 +** the main database is read-only. The transient or virtual
1.2770 +** table is deleted automatically when the cursor is closed.
1.2771 +**
1.2772 +** P2 is the number of columns in the virtual table.
1.2773 +** The cursor points to a BTree table if P4==0 and to a BTree index
1.2774 +** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
1.2775 +** that defines the format of keys in the index.
1.2776 +**
1.2777 +** This opcode was once called OpenTemp. But that created
1.2778 +** confusion because the term "temp table", might refer either
1.2779 +** to a TEMP table at the SQL level, or to a table opened by
1.2780 +** this opcode. Then this opcode was call OpenVirtual. But
1.2781 +** that created confusion with the whole virtual-table idea.
1.2782 +*/
1.2783 +case OP_OpenEphemeral: {
1.2784 + int i = pOp->p1;
1.2785 + Cursor *pCx;
1.2786 + static const int openFlags =
1.2787 + SQLITE_OPEN_READWRITE |
1.2788 + SQLITE_OPEN_CREATE |
1.2789 + SQLITE_OPEN_EXCLUSIVE |
1.2790 + SQLITE_OPEN_DELETEONCLOSE |
1.2791 + SQLITE_OPEN_TRANSIENT_DB;
1.2792 +
1.2793 + assert( i>=0 );
1.2794 + pCx = allocateCursor(p, i, pOp, -1, 1);
1.2795 + if( pCx==0 ) goto no_mem;
1.2796 + pCx->nullRow = 1;
1.2797 + rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags,
1.2798 + &pCx->pBt);
1.2799 + if( rc==SQLITE_OK ){
1.2800 + rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
1.2801 + }
1.2802 + if( rc==SQLITE_OK ){
1.2803 + /* If a transient index is required, create it by calling
1.2804 + ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
1.2805 + ** opening it. If a transient table is required, just use the
1.2806 + ** automatically created table with root-page 1 (an INTKEY table).
1.2807 + */
1.2808 + if( pOp->p4.pKeyInfo ){
1.2809 + int pgno;
1.2810 + assert( pOp->p4type==P4_KEYINFO );
1.2811 + rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA);
1.2812 + if( rc==SQLITE_OK ){
1.2813 + assert( pgno==MASTER_ROOT+1 );
1.2814 + rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1,
1.2815 + (KeyInfo*)pOp->p4.z, pCx->pCursor);
1.2816 + pCx->pKeyInfo = pOp->p4.pKeyInfo;
1.2817 + pCx->pKeyInfo->enc = ENC(p->db);
1.2818 + }
1.2819 + pCx->isTable = 0;
1.2820 + }else{
1.2821 + rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
1.2822 + pCx->isTable = 1;
1.2823 + }
1.2824 + }
1.2825 + pCx->isIndex = !pCx->isTable;
1.2826 + break;
1.2827 +}
1.2828 +
1.2829 +/* Opcode: OpenPseudo P1 P2 * * *
1.2830 +**
1.2831 +** Open a new cursor that points to a fake table that contains a single
1.2832 +** row of data. Any attempt to write a second row of data causes the
1.2833 +** first row to be deleted. All data is deleted when the cursor is
1.2834 +** closed.
1.2835 +**
1.2836 +** A pseudo-table created by this opcode is useful for holding the
1.2837 +** NEW or OLD tables in a trigger. Also used to hold the a single
1.2838 +** row output from the sorter so that the row can be decomposed into
1.2839 +** individual columns using the OP_Column opcode.
1.2840 +**
1.2841 +** When OP_Insert is executed to insert a row in to the pseudo table,
1.2842 +** the pseudo-table cursor may or may not make it's own copy of the
1.2843 +** original row data. If P2 is 0, then the pseudo-table will copy the
1.2844 +** original row data. Otherwise, a pointer to the original memory cell
1.2845 +** is stored. In this case, the vdbe program must ensure that the
1.2846 +** memory cell containing the row data is not overwritten until the
1.2847 +** pseudo table is closed (or a new row is inserted into it).
1.2848 +*/
1.2849 +case OP_OpenPseudo: {
1.2850 + int i = pOp->p1;
1.2851 + Cursor *pCx;
1.2852 + assert( i>=0 );
1.2853 + pCx = allocateCursor(p, i, &pOp[-1], -1, 0);
1.2854 + if( pCx==0 ) goto no_mem;
1.2855 + pCx->nullRow = 1;
1.2856 + pCx->pseudoTable = 1;
1.2857 + pCx->ephemPseudoTable = pOp->p2;
1.2858 + pCx->isTable = 1;
1.2859 + pCx->isIndex = 0;
1.2860 + break;
1.2861 +}
1.2862 +
1.2863 +/* Opcode: Close P1 * * * *
1.2864 +**
1.2865 +** Close a cursor previously opened as P1. If P1 is not
1.2866 +** currently open, this instruction is a no-op.
1.2867 +*/
1.2868 +case OP_Close: {
1.2869 + int i = pOp->p1;
1.2870 + assert( i>=0 && i<p->nCursor );
1.2871 + sqlite3VdbeFreeCursor(p, p->apCsr[i]);
1.2872 + p->apCsr[i] = 0;
1.2873 + break;
1.2874 +}
1.2875 +
1.2876 +/* Opcode: MoveGe P1 P2 P3 P4 *
1.2877 +**
1.2878 +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
1.2879 +** use the integer value in register P3 as a key. If cursor P1 refers
1.2880 +** to an SQL index, then P3 is the first in an array of P4 registers
1.2881 +** that are used as an unpacked index key.
1.2882 +**
1.2883 +** Reposition cursor P1 so that it points to the smallest entry that
1.2884 +** is greater than or equal to the key value. If there are no records
1.2885 +** greater than or equal to the key and P2 is not zero, then jump to P2.
1.2886 +**
1.2887 +** A special feature of this opcode (and different from the
1.2888 +** related OP_MoveGt, OP_MoveLt, and OP_MoveLe) is that if P2 is
1.2889 +** zero and P1 is an SQL table (a b-tree with integer keys) then
1.2890 +** the seek is deferred until it is actually needed. It might be
1.2891 +** the case that the cursor is never accessed. By deferring the
1.2892 +** seek, we avoid unnecessary seeks.
1.2893 +**
1.2894 +** See also: Found, NotFound, Distinct, MoveLt, MoveGt, MoveLe
1.2895 +*/
1.2896 +/* Opcode: MoveGt P1 P2 P3 P4 *
1.2897 +**
1.2898 +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
1.2899 +** use the integer value in register P3 as a key. If cursor P1 refers
1.2900 +** to an SQL index, then P3 is the first in an array of P4 registers
1.2901 +** that are used as an unpacked index key.
1.2902 +**
1.2903 +** Reposition cursor P1 so that it points to the smallest entry that
1.2904 +** is greater than the key value. If there are no records greater than
1.2905 +** the key and P2 is not zero, then jump to P2.
1.2906 +**
1.2907 +** See also: Found, NotFound, Distinct, MoveLt, MoveGe, MoveLe
1.2908 +*/
1.2909 +/* Opcode: MoveLt P1 P2 P3 P4 *
1.2910 +**
1.2911 +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
1.2912 +** use the integer value in register P3 as a key. If cursor P1 refers
1.2913 +** to an SQL index, then P3 is the first in an array of P4 registers
1.2914 +** that are used as an unpacked index key.
1.2915 +**
1.2916 +** Reposition cursor P1 so that it points to the largest entry that
1.2917 +** is less than the key value. If there are no records less than
1.2918 +** the key and P2 is not zero, then jump to P2.
1.2919 +**
1.2920 +** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLe
1.2921 +*/
1.2922 +/* Opcode: MoveLe P1 P2 P3 P4 *
1.2923 +**
1.2924 +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
1.2925 +** use the integer value in register P3 as a key. If cursor P1 refers
1.2926 +** to an SQL index, then P3 is the first in an array of P4 registers
1.2927 +** that are used as an unpacked index key.
1.2928 +**
1.2929 +** Reposition cursor P1 so that it points to the largest entry that
1.2930 +** is less than or equal to the key value. If there are no records
1.2931 +** less than or equal to the key and P2 is not zero, then jump to P2.
1.2932 +**
1.2933 +** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLt
1.2934 +*/
1.2935 +case OP_MoveLt: /* jump, in3 */
1.2936 +case OP_MoveLe: /* jump, in3 */
1.2937 +case OP_MoveGe: /* jump, in3 */
1.2938 +case OP_MoveGt: { /* jump, in3 */
1.2939 + int i = pOp->p1;
1.2940 + Cursor *pC;
1.2941 +
1.2942 + assert( i>=0 && i<p->nCursor );
1.2943 + pC = p->apCsr[i];
1.2944 + assert( pC!=0 );
1.2945 + if( pC->pCursor!=0 ){
1.2946 + int res, oc;
1.2947 + oc = pOp->opcode;
1.2948 + pC->nullRow = 0;
1.2949 + if( pC->isTable ){
1.2950 + i64 iKey = sqlite3VdbeIntValue(pIn3);
1.2951 + if( pOp->p2==0 ){
1.2952 + assert( pOp->opcode==OP_MoveGe );
1.2953 + pC->movetoTarget = iKey;
1.2954 + pC->rowidIsValid = 0;
1.2955 + pC->deferredMoveto = 1;
1.2956 + break;
1.2957 + }
1.2958 + rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
1.2959 + if( rc!=SQLITE_OK ){
1.2960 + goto abort_due_to_error;
1.2961 + }
1.2962 + pC->lastRowid = iKey;
1.2963 + pC->rowidIsValid = res==0;
1.2964 + }else{
1.2965 + UnpackedRecord r;
1.2966 + int nField = pOp->p4.i;
1.2967 + assert( pOp->p4type==P4_INT32 );
1.2968 + assert( nField>0 );
1.2969 + r.pKeyInfo = pC->pKeyInfo;
1.2970 + r.nField = nField;
1.2971 + if( oc==OP_MoveGt || oc==OP_MoveLe ){
1.2972 + r.flags = UNPACKED_INCRKEY;
1.2973 + }else{
1.2974 + r.flags = 0;
1.2975 + }
1.2976 + r.aMem = &p->aMem[pOp->p3];
1.2977 + rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
1.2978 + if( rc!=SQLITE_OK ){
1.2979 + goto abort_due_to_error;
1.2980 + }
1.2981 + pC->rowidIsValid = 0;
1.2982 + }
1.2983 + pC->deferredMoveto = 0;
1.2984 + pC->cacheStatus = CACHE_STALE;
1.2985 +#ifdef SQLITE_TEST
1.2986 + sqlite3_search_count++;
1.2987 +#endif
1.2988 + if( oc==OP_MoveGe || oc==OP_MoveGt ){
1.2989 + if( res<0 ){
1.2990 + rc = sqlite3BtreeNext(pC->pCursor, &res);
1.2991 + if( rc!=SQLITE_OK ) goto abort_due_to_error;
1.2992 + pC->rowidIsValid = 0;
1.2993 + }else{
1.2994 + res = 0;
1.2995 + }
1.2996 + }else{
1.2997 + assert( oc==OP_MoveLt || oc==OP_MoveLe );
1.2998 + if( res>=0 ){
1.2999 + rc = sqlite3BtreePrevious(pC->pCursor, &res);
1.3000 + if( rc!=SQLITE_OK ) goto abort_due_to_error;
1.3001 + pC->rowidIsValid = 0;
1.3002 + }else{
1.3003 + /* res might be negative because the table is empty. Check to
1.3004 + ** see if this is the case.
1.3005 + */
1.3006 + res = sqlite3BtreeEof(pC->pCursor);
1.3007 + }
1.3008 + }
1.3009 + assert( pOp->p2>0 );
1.3010 + if( res ){
1.3011 + pc = pOp->p2 - 1;
1.3012 + }
1.3013 + }else if( !pC->pseudoTable ){
1.3014 + /* This happens when attempting to open the sqlite3_master table
1.3015 + ** for read access returns SQLITE_EMPTY. In this case always
1.3016 + ** take the jump (since there are no records in the table).
1.3017 + */
1.3018 + pc = pOp->p2 - 1;
1.3019 + }
1.3020 + break;
1.3021 +}
1.3022 +
1.3023 +/* Opcode: Found P1 P2 P3 * *
1.3024 +**
1.3025 +** Register P3 holds a blob constructed by MakeRecord. P1 is an index.
1.3026 +** If an entry that matches the value in register p3 exists in P1 then
1.3027 +** jump to P2. If the P3 value does not match any entry in P1
1.3028 +** then fall thru. The P1 cursor is left pointing at the matching entry
1.3029 +** if it exists.
1.3030 +**
1.3031 +** This instruction is used to implement the IN operator where the
1.3032 +** left-hand side is a SELECT statement. P1 may be a true index, or it
1.3033 +** may be a temporary index that holds the results of the SELECT
1.3034 +** statement. This instruction is also used to implement the
1.3035 +** DISTINCT keyword in SELECT statements.
1.3036 +**
1.3037 +** This instruction checks if index P1 contains a record for which
1.3038 +** the first N serialized values exactly match the N serialized values
1.3039 +** in the record in register P3, where N is the total number of values in
1.3040 +** the P3 record (the P3 record is a prefix of the P1 record).
1.3041 +**
1.3042 +** See also: NotFound, IsUnique, NotExists
1.3043 +*/
1.3044 +/* Opcode: NotFound P1 P2 P3 * *
1.3045 +**
1.3046 +** Register P3 holds a blob constructed by MakeRecord. P1 is
1.3047 +** an index. If no entry exists in P1 that matches the blob then jump
1.3048 +** to P2. If an entry does existing, fall through. The cursor is left
1.3049 +** pointing to the entry that matches.
1.3050 +**
1.3051 +** See also: Found, NotExists, IsUnique
1.3052 +*/
1.3053 +case OP_NotFound: /* jump, in3 */
1.3054 +case OP_Found: { /* jump, in3 */
1.3055 + int i = pOp->p1;
1.3056 + int alreadyExists = 0;
1.3057 + Cursor *pC;
1.3058 + assert( i>=0 && i<p->nCursor );
1.3059 + assert( p->apCsr[i]!=0 );
1.3060 + if( (pC = p->apCsr[i])->pCursor!=0 ){
1.3061 + int res;
1.3062 + UnpackedRecord *pIdxKey;
1.3063 +
1.3064 + assert( pC->isTable==0 );
1.3065 + assert( pIn3->flags & MEM_Blob );
1.3066 + pIdxKey = sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z,
1.3067 + aTempRec, sizeof(aTempRec));
1.3068 + if( pIdxKey==0 ){
1.3069 + goto no_mem;
1.3070 + }
1.3071 + if( pOp->opcode==OP_Found ){
1.3072 + pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
1.3073 + }
1.3074 + rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
1.3075 + sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
1.3076 + if( rc!=SQLITE_OK ){
1.3077 + break;
1.3078 + }
1.3079 + alreadyExists = (res==0);
1.3080 + pC->deferredMoveto = 0;
1.3081 + pC->cacheStatus = CACHE_STALE;
1.3082 + }
1.3083 + if( pOp->opcode==OP_Found ){
1.3084 + if( alreadyExists ) pc = pOp->p2 - 1;
1.3085 + }else{
1.3086 + if( !alreadyExists ) pc = pOp->p2 - 1;
1.3087 + }
1.3088 + break;
1.3089 +}
1.3090 +
1.3091 +/* Opcode: IsUnique P1 P2 P3 P4 *
1.3092 +**
1.3093 +** The P3 register contains an integer record number. Call this
1.3094 +** record number R. The P4 register contains an index key created
1.3095 +** using MakeRecord. Call it K.
1.3096 +**
1.3097 +** P1 is an index. So it has no data and its key consists of a
1.3098 +** record generated by OP_MakeRecord where the last field is the
1.3099 +** rowid of the entry that the index refers to.
1.3100 +**
1.3101 +** This instruction asks if there is an entry in P1 where the
1.3102 +** fields matches K but the rowid is different from R.
1.3103 +** If there is no such entry, then there is an immediate
1.3104 +** jump to P2. If any entry does exist where the index string
1.3105 +** matches K but the record number is not R, then the record
1.3106 +** number for that entry is written into P3 and control
1.3107 +** falls through to the next instruction.
1.3108 +**
1.3109 +** See also: NotFound, NotExists, Found
1.3110 +*/
1.3111 +case OP_IsUnique: { /* jump, in3 */
1.3112 + int i = pOp->p1;
1.3113 + Cursor *pCx;
1.3114 + BtCursor *pCrsr;
1.3115 + Mem *pK;
1.3116 + i64 R;
1.3117 +
1.3118 + /* Pop the value R off the top of the stack
1.3119 + */
1.3120 + assert( pOp->p4type==P4_INT32 );
1.3121 + assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
1.3122 + pK = &p->aMem[pOp->p4.i];
1.3123 + sqlite3VdbeMemIntegerify(pIn3);
1.3124 + R = pIn3->u.i;
1.3125 + assert( i>=0 && i<p->nCursor );
1.3126 + pCx = p->apCsr[i];
1.3127 + assert( pCx!=0 );
1.3128 + pCrsr = pCx->pCursor;
1.3129 + if( pCrsr!=0 ){
1.3130 + int res;
1.3131 + i64 v; /* The record number that matches K */
1.3132 + UnpackedRecord *pIdxKey; /* Unpacked version of P4 */
1.3133 +
1.3134 + /* Make sure K is a string and make zKey point to K
1.3135 + */
1.3136 + assert( pK->flags & MEM_Blob );
1.3137 + pIdxKey = sqlite3VdbeRecordUnpack(pCx->pKeyInfo, pK->n, pK->z,
1.3138 + aTempRec, sizeof(aTempRec));
1.3139 + if( pIdxKey==0 ){
1.3140 + goto no_mem;
1.3141 + }
1.3142 + pIdxKey->flags |= UNPACKED_IGNORE_ROWID;
1.3143 +
1.3144 + /* Search for an entry in P1 where all but the last rowid match K
1.3145 + ** If there is no such entry, jump immediately to P2.
1.3146 + */
1.3147 + assert( pCx->deferredMoveto==0 );
1.3148 + pCx->cacheStatus = CACHE_STALE;
1.3149 + rc = sqlite3BtreeMovetoUnpacked(pCrsr, pIdxKey, 0, 0, &res);
1.3150 + if( rc!=SQLITE_OK ){
1.3151 + sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
1.3152 + goto abort_due_to_error;
1.3153 + }
1.3154 + if( res<0 ){
1.3155 + rc = sqlite3BtreeNext(pCrsr, &res);
1.3156 + if( res ){
1.3157 + pc = pOp->p2 - 1;
1.3158 + sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
1.3159 + break;
1.3160 + }
1.3161 + }
1.3162 + rc = sqlite3VdbeIdxKeyCompare(pCx, pIdxKey, &res);
1.3163 + sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
1.3164 + if( rc!=SQLITE_OK ) goto abort_due_to_error;
1.3165 + if( res>0 ){
1.3166 + pc = pOp->p2 - 1;
1.3167 + break;
1.3168 + }
1.3169 +
1.3170 + /* At this point, pCrsr is pointing to an entry in P1 where all but
1.3171 + ** the final entry (the rowid) matches K. Check to see if the
1.3172 + ** final rowid column is different from R. If it equals R then jump
1.3173 + ** immediately to P2.
1.3174 + */
1.3175 + rc = sqlite3VdbeIdxRowid(pCrsr, &v);
1.3176 + if( rc!=SQLITE_OK ){
1.3177 + goto abort_due_to_error;
1.3178 + }
1.3179 + if( v==R ){
1.3180 + pc = pOp->p2 - 1;
1.3181 + break;
1.3182 + }
1.3183 +
1.3184 + /* The final varint of the key is different from R. Store it back
1.3185 + ** into register R3. (The record number of an entry that violates
1.3186 + ** a UNIQUE constraint.)
1.3187 + */
1.3188 + pIn3->u.i = v;
1.3189 + assert( pIn3->flags&MEM_Int );
1.3190 + }
1.3191 + break;
1.3192 +}
1.3193 +
1.3194 +/* Opcode: NotExists P1 P2 P3 * *
1.3195 +**
1.3196 +** Use the content of register P3 as a integer key. If a record
1.3197 +** with that key does not exist in table of P1, then jump to P2.
1.3198 +** If the record does exist, then fall thru. The cursor is left
1.3199 +** pointing to the record if it exists.
1.3200 +**
1.3201 +** The difference between this operation and NotFound is that this
1.3202 +** operation assumes the key is an integer and that P1 is a table whereas
1.3203 +** NotFound assumes key is a blob constructed from MakeRecord and
1.3204 +** P1 is an index.
1.3205 +**
1.3206 +** See also: Found, NotFound, IsUnique
1.3207 +*/
1.3208 +case OP_NotExists: { /* jump, in3 */
1.3209 + int i = pOp->p1;
1.3210 + Cursor *pC;
1.3211 + BtCursor *pCrsr;
1.3212 + assert( i>=0 && i<p->nCursor );
1.3213 + assert( p->apCsr[i]!=0 );
1.3214 + if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
1.3215 + int res;
1.3216 + u64 iKey;
1.3217 + assert( pIn3->flags & MEM_Int );
1.3218 + assert( p->apCsr[i]->isTable );
1.3219 + iKey = intToKey(pIn3->u.i);
1.3220 + rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0,&res);
1.3221 + pC->lastRowid = pIn3->u.i;
1.3222 + pC->rowidIsValid = res==0;
1.3223 + pC->nullRow = 0;
1.3224 + pC->cacheStatus = CACHE_STALE;
1.3225 + /* res might be uninitialized if rc!=SQLITE_OK. But if rc!=SQLITE_OK
1.3226 + ** processing is about to abort so we really do not care whether or not
1.3227 + ** the following jump is taken. (In other words, do not stress over
1.3228 + ** the error that valgrind sometimes shows on the next statement when
1.3229 + ** running ioerr.test and similar failure-recovery test scripts.) */
1.3230 + if( res!=0 ){
1.3231 + pc = pOp->p2 - 1;
1.3232 + assert( pC->rowidIsValid==0 );
1.3233 + }
1.3234 + }else if( !pC->pseudoTable ){
1.3235 + /* This happens when an attempt to open a read cursor on the
1.3236 + ** sqlite_master table returns SQLITE_EMPTY.
1.3237 + */
1.3238 + assert( pC->isTable );
1.3239 + pc = pOp->p2 - 1;
1.3240 + assert( pC->rowidIsValid==0 );
1.3241 + }
1.3242 + break;
1.3243 +}
1.3244 +
1.3245 +/* Opcode: Sequence P1 P2 * * *
1.3246 +**
1.3247 +** Find the next available sequence number for cursor P1.
1.3248 +** Write the sequence number into register P2.
1.3249 +** The sequence number on the cursor is incremented after this
1.3250 +** instruction.
1.3251 +*/
1.3252 +case OP_Sequence: { /* out2-prerelease */
1.3253 + int i = pOp->p1;
1.3254 + assert( i>=0 && i<p->nCursor );
1.3255 + assert( p->apCsr[i]!=0 );
1.3256 + pOut->u.i = p->apCsr[i]->seqCount++;
1.3257 + MemSetTypeFlag(pOut, MEM_Int);
1.3258 + break;
1.3259 +}
1.3260 +
1.3261 +
1.3262 +/* Opcode: NewRowid P1 P2 P3 * *
1.3263 +**
1.3264 +** Get a new integer record number (a.k.a "rowid") used as the key to a table.
1.3265 +** The record number is not previously used as a key in the database
1.3266 +** table that cursor P1 points to. The new record number is written
1.3267 +** written to register P2.
1.3268 +**
1.3269 +** If P3>0 then P3 is a register that holds the largest previously
1.3270 +** generated record number. No new record numbers are allowed to be less
1.3271 +** than this value. When this value reaches its maximum, a SQLITE_FULL
1.3272 +** error is generated. The P3 register is updated with the generated
1.3273 +** record number. This P3 mechanism is used to help implement the
1.3274 +** AUTOINCREMENT feature.
1.3275 +*/
1.3276 +case OP_NewRowid: { /* out2-prerelease */
1.3277 + int i = pOp->p1;
1.3278 + i64 v = 0;
1.3279 + Cursor *pC;
1.3280 + assert( i>=0 && i<p->nCursor );
1.3281 + assert( p->apCsr[i]!=0 );
1.3282 + if( (pC = p->apCsr[i])->pCursor==0 ){
1.3283 + /* The zero initialization above is all that is needed */
1.3284 + }else{
1.3285 + /* The next rowid or record number (different terms for the same
1.3286 + ** thing) is obtained in a two-step algorithm.
1.3287 + **
1.3288 + ** First we attempt to find the largest existing rowid and add one
1.3289 + ** to that. But if the largest existing rowid is already the maximum
1.3290 + ** positive integer, we have to fall through to the second
1.3291 + ** probabilistic algorithm
1.3292 + **
1.3293 + ** The second algorithm is to select a rowid at random and see if
1.3294 + ** it already exists in the table. If it does not exist, we have
1.3295 + ** succeeded. If the random rowid does exist, we select a new one
1.3296 + ** and try again, up to 1000 times.
1.3297 + **
1.3298 + ** For a table with less than 2 billion entries, the probability
1.3299 + ** of not finding a unused rowid is about 1.0e-300. This is a
1.3300 + ** non-zero probability, but it is still vanishingly small and should
1.3301 + ** never cause a problem. You are much, much more likely to have a
1.3302 + ** hardware failure than for this algorithm to fail.
1.3303 + **
1.3304 + ** The analysis in the previous paragraph assumes that you have a good
1.3305 + ** source of random numbers. Is a library function like lrand48()
1.3306 + ** good enough? Maybe. Maybe not. It's hard to know whether there
1.3307 + ** might be subtle bugs is some implementations of lrand48() that
1.3308 + ** could cause problems. To avoid uncertainty, SQLite uses its own
1.3309 + ** random number generator based on the RC4 algorithm.
1.3310 + **
1.3311 + ** To promote locality of reference for repetitive inserts, the
1.3312 + ** first few attempts at choosing a random rowid pick values just a little
1.3313 + ** larger than the previous rowid. This has been shown experimentally
1.3314 + ** to double the speed of the COPY operation.
1.3315 + */
1.3316 + int res, rx=SQLITE_OK, cnt;
1.3317 + i64 x;
1.3318 + cnt = 0;
1.3319 + if( (sqlite3BtreeFlags(pC->pCursor)&(BTREE_INTKEY|BTREE_ZERODATA)) !=
1.3320 + BTREE_INTKEY ){
1.3321 + rc = SQLITE_CORRUPT_BKPT;
1.3322 + goto abort_due_to_error;
1.3323 + }
1.3324 + assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 );
1.3325 + assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 );
1.3326 +
1.3327 +#ifdef SQLITE_32BIT_ROWID
1.3328 +# define MAX_ROWID 0x7fffffff
1.3329 +#else
1.3330 + /* Some compilers complain about constants of the form 0x7fffffffffffffff.
1.3331 + ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
1.3332 + ** to provide the constant while making all compilers happy.
1.3333 + */
1.3334 +# define MAX_ROWID ( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
1.3335 +#endif
1.3336 +
1.3337 + if( !pC->useRandomRowid ){
1.3338 + if( pC->nextRowidValid ){
1.3339 + v = pC->nextRowid;
1.3340 + }else{
1.3341 + rc = sqlite3BtreeLast(pC->pCursor, &res);
1.3342 + if( rc!=SQLITE_OK ){
1.3343 + goto abort_due_to_error;
1.3344 + }
1.3345 + if( res ){
1.3346 + v = 1;
1.3347 + }else{
1.3348 + sqlite3BtreeKeySize(pC->pCursor, &v);
1.3349 + v = keyToInt(v);
1.3350 + if( v==MAX_ROWID ){
1.3351 + pC->useRandomRowid = 1;
1.3352 + }else{
1.3353 + v++;
1.3354 + }
1.3355 + }
1.3356 + }
1.3357 +
1.3358 +#ifndef SQLITE_OMIT_AUTOINCREMENT
1.3359 + if( pOp->p3 ){
1.3360 + Mem *pMem;
1.3361 + assert( pOp->p3>0 && pOp->p3<=p->nMem ); /* P3 is a valid memory cell */
1.3362 + pMem = &p->aMem[pOp->p3];
1.3363 + REGISTER_TRACE(pOp->p3, pMem);
1.3364 + sqlite3VdbeMemIntegerify(pMem);
1.3365 + assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
1.3366 + if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
1.3367 + rc = SQLITE_FULL;
1.3368 + goto abort_due_to_error;
1.3369 + }
1.3370 + if( v<pMem->u.i+1 ){
1.3371 + v = pMem->u.i + 1;
1.3372 + }
1.3373 + pMem->u.i = v;
1.3374 + }
1.3375 +#endif
1.3376 +
1.3377 + if( v<MAX_ROWID ){
1.3378 + pC->nextRowidValid = 1;
1.3379 + pC->nextRowid = v+1;
1.3380 + }else{
1.3381 + pC->nextRowidValid = 0;
1.3382 + }
1.3383 + }
1.3384 + if( pC->useRandomRowid ){
1.3385 + assert( pOp->p3==0 ); /* SQLITE_FULL must have occurred prior to this */
1.3386 + v = db->priorNewRowid;
1.3387 + cnt = 0;
1.3388 + do{
1.3389 + if( cnt==0 && (v&0xffffff)==v ){
1.3390 + v++;
1.3391 + }else{
1.3392 + sqlite3_randomness(sizeof(v), &v);
1.3393 + if( cnt<5 ) v &= 0xffffff;
1.3394 + }
1.3395 + if( v==0 ) continue;
1.3396 + x = intToKey(v);
1.3397 + rx = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)x, 0, &res);
1.3398 + cnt++;
1.3399 + }while( cnt<100 && rx==SQLITE_OK && res==0 );
1.3400 + db->priorNewRowid = v;
1.3401 + if( rx==SQLITE_OK && res==0 ){
1.3402 + rc = SQLITE_FULL;
1.3403 + goto abort_due_to_error;
1.3404 + }
1.3405 + }
1.3406 + pC->rowidIsValid = 0;
1.3407 + pC->deferredMoveto = 0;
1.3408 + pC->cacheStatus = CACHE_STALE;
1.3409 + }
1.3410 + MemSetTypeFlag(pOut, MEM_Int);
1.3411 + pOut->u.i = v;
1.3412 + break;
1.3413 +}
1.3414 +
1.3415 +/* Opcode: Insert P1 P2 P3 P4 P5
1.3416 +**
1.3417 +** Write an entry into the table of cursor P1. A new entry is
1.3418 +** created if it doesn't already exist or the data for an existing
1.3419 +** entry is overwritten. The data is the value stored register
1.3420 +** number P2. The key is stored in register P3. The key must
1.3421 +** be an integer.
1.3422 +**
1.3423 +** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
1.3424 +** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
1.3425 +** then rowid is stored for subsequent return by the
1.3426 +** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
1.3427 +**
1.3428 +** Parameter P4 may point to a string containing the table-name, or
1.3429 +** may be NULL. If it is not NULL, then the update-hook
1.3430 +** (sqlite3.xUpdateCallback) is invoked following a successful insert.
1.3431 +**
1.3432 +** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
1.3433 +** allocated, then ownership of P2 is transferred to the pseudo-cursor
1.3434 +** and register P2 becomes ephemeral. If the cursor is changed, the
1.3435 +** value of register P2 will then change. Make sure this does not
1.3436 +** cause any problems.)
1.3437 +**
1.3438 +** This instruction only works on tables. The equivalent instruction
1.3439 +** for indices is OP_IdxInsert.
1.3440 +*/
1.3441 +case OP_Insert: {
1.3442 + Mem *pData = &p->aMem[pOp->p2];
1.3443 + Mem *pKey = &p->aMem[pOp->p3];
1.3444 +
1.3445 + i64 iKey; /* The integer ROWID or key for the record to be inserted */
1.3446 + int i = pOp->p1;
1.3447 + Cursor *pC;
1.3448 + assert( i>=0 && i<p->nCursor );
1.3449 + pC = p->apCsr[i];
1.3450 + assert( pC!=0 );
1.3451 + assert( pC->pCursor!=0 || pC->pseudoTable );
1.3452 + assert( pKey->flags & MEM_Int );
1.3453 + assert( pC->isTable );
1.3454 + REGISTER_TRACE(pOp->p2, pData);
1.3455 + REGISTER_TRACE(pOp->p3, pKey);
1.3456 +
1.3457 + iKey = intToKey(pKey->u.i);
1.3458 + if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
1.3459 + if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = pKey->u.i;
1.3460 + if( pC->nextRowidValid && pKey->u.i>=pC->nextRowid ){
1.3461 + pC->nextRowidValid = 0;
1.3462 + }
1.3463 + if( pData->flags & MEM_Null ){
1.3464 + pData->z = 0;
1.3465 + pData->n = 0;
1.3466 + }else{
1.3467 + assert( pData->flags & (MEM_Blob|MEM_Str) );
1.3468 + }
1.3469 + if( pC->pseudoTable ){
1.3470 + if( !pC->ephemPseudoTable ){
1.3471 + sqlite3DbFree(db, pC->pData);
1.3472 + }
1.3473 + pC->iKey = iKey;
1.3474 + pC->nData = pData->n;
1.3475 + if( pData->z==pData->zMalloc || pC->ephemPseudoTable ){
1.3476 + pC->pData = pData->z;
1.3477 + if( !pC->ephemPseudoTable ){
1.3478 + pData->flags &= ~MEM_Dyn;
1.3479 + pData->flags |= MEM_Ephem;
1.3480 + pData->zMalloc = 0;
1.3481 + }
1.3482 + }else{
1.3483 + pC->pData = sqlite3Malloc( pC->nData+2 );
1.3484 + if( !pC->pData ) goto no_mem;
1.3485 + memcpy(pC->pData, pData->z, pC->nData);
1.3486 + pC->pData[pC->nData] = 0;
1.3487 + pC->pData[pC->nData+1] = 0;
1.3488 + }
1.3489 + pC->nullRow = 0;
1.3490 + }else{
1.3491 + int nZero;
1.3492 + if( pData->flags & MEM_Zero ){
1.3493 + nZero = pData->u.i;
1.3494 + }else{
1.3495 + nZero = 0;
1.3496 + }
1.3497 + rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
1.3498 + pData->z, pData->n, nZero,
1.3499 + pOp->p5 & OPFLAG_APPEND);
1.3500 + }
1.3501 +
1.3502 + pC->rowidIsValid = 0;
1.3503 + pC->deferredMoveto = 0;
1.3504 + pC->cacheStatus = CACHE_STALE;
1.3505 +
1.3506 + /* Invoke the update-hook if required. */
1.3507 + if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
1.3508 + const char *zDb = db->aDb[pC->iDb].zName;
1.3509 + const char *zTbl = pOp->p4.z;
1.3510 + int op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
1.3511 + assert( pC->isTable );
1.3512 + db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
1.3513 + assert( pC->iDb>=0 );
1.3514 + }
1.3515 + break;
1.3516 +}
1.3517 +
1.3518 +/* Opcode: Delete P1 P2 * P4 *
1.3519 +**
1.3520 +** Delete the record at which the P1 cursor is currently pointing.
1.3521 +**
1.3522 +** The cursor will be left pointing at either the next or the previous
1.3523 +** record in the table. If it is left pointing at the next record, then
1.3524 +** the next Next instruction will be a no-op. Hence it is OK to delete
1.3525 +** a record from within an Next loop.
1.3526 +**
1.3527 +** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
1.3528 +** incremented (otherwise not).
1.3529 +**
1.3530 +** P1 must not be pseudo-table. It has to be a real table with
1.3531 +** multiple rows.
1.3532 +**
1.3533 +** If P4 is not NULL, then it is the name of the table that P1 is
1.3534 +** pointing to. The update hook will be invoked, if it exists.
1.3535 +** If P4 is not NULL then the P1 cursor must have been positioned
1.3536 +** using OP_NotFound prior to invoking this opcode.
1.3537 +*/
1.3538 +case OP_Delete: {
1.3539 + int i = pOp->p1;
1.3540 + i64 iKey;
1.3541 + Cursor *pC;
1.3542 +
1.3543 + assert( i>=0 && i<p->nCursor );
1.3544 + pC = p->apCsr[i];
1.3545 + assert( pC!=0 );
1.3546 + assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
1.3547 +
1.3548 + /* If the update-hook will be invoked, set iKey to the rowid of the
1.3549 + ** row being deleted.
1.3550 + */
1.3551 + if( db->xUpdateCallback && pOp->p4.z ){
1.3552 + assert( pC->isTable );
1.3553 + assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
1.3554 + iKey = pC->lastRowid;
1.3555 + }
1.3556 +
1.3557 + rc = sqlite3VdbeCursorMoveto(pC);
1.3558 + if( rc ) goto abort_due_to_error;
1.3559 + rc = sqlite3BtreeDelete(pC->pCursor);
1.3560 + pC->nextRowidValid = 0;
1.3561 + pC->cacheStatus = CACHE_STALE;
1.3562 +
1.3563 + /* Invoke the update-hook if required. */
1.3564 + if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
1.3565 + const char *zDb = db->aDb[pC->iDb].zName;
1.3566 + const char *zTbl = pOp->p4.z;
1.3567 + db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
1.3568 + assert( pC->iDb>=0 );
1.3569 + }
1.3570 + if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
1.3571 + break;
1.3572 +}
1.3573 +
1.3574 +/* Opcode: ResetCount P1 * *
1.3575 +**
1.3576 +** This opcode resets the VMs internal change counter to 0. If P1 is true,
1.3577 +** then the value of the change counter is copied to the database handle
1.3578 +** change counter (returned by subsequent calls to sqlite3_changes())
1.3579 +** before it is reset. This is used by trigger programs.
1.3580 +*/
1.3581 +case OP_ResetCount: {
1.3582 + if( pOp->p1 ){
1.3583 + sqlite3VdbeSetChanges(db, p->nChange);
1.3584 + }
1.3585 + p->nChange = 0;
1.3586 + break;
1.3587 +}
1.3588 +
1.3589 +/* Opcode: RowData P1 P2 * * *
1.3590 +**
1.3591 +** Write into register P2 the complete row data for cursor P1.
1.3592 +** There is no interpretation of the data.
1.3593 +** It is just copied onto the P2 register exactly as
1.3594 +** it is found in the database file.
1.3595 +**
1.3596 +** If the P1 cursor must be pointing to a valid row (not a NULL row)
1.3597 +** of a real table, not a pseudo-table.
1.3598 +*/
1.3599 +/* Opcode: RowKey P1 P2 * * *
1.3600 +**
1.3601 +** Write into register P2 the complete row key for cursor P1.
1.3602 +** There is no interpretation of the data.
1.3603 +** The key is copied onto the P3 register exactly as
1.3604 +** it is found in the database file.
1.3605 +**
1.3606 +** If the P1 cursor must be pointing to a valid row (not a NULL row)
1.3607 +** of a real table, not a pseudo-table.
1.3608 +*/
1.3609 +case OP_RowKey:
1.3610 +case OP_RowData: {
1.3611 + int i = pOp->p1;
1.3612 + Cursor *pC;
1.3613 + BtCursor *pCrsr;
1.3614 + u32 n;
1.3615 +
1.3616 + pOut = &p->aMem[pOp->p2];
1.3617 +
1.3618 + /* Note that RowKey and RowData are really exactly the same instruction */
1.3619 + assert( i>=0 && i<p->nCursor );
1.3620 + pC = p->apCsr[i];
1.3621 + assert( pC->isTable || pOp->opcode==OP_RowKey );
1.3622 + assert( pC->isIndex || pOp->opcode==OP_RowData );
1.3623 + assert( pC!=0 );
1.3624 + assert( pC->nullRow==0 );
1.3625 + assert( pC->pseudoTable==0 );
1.3626 + assert( pC->pCursor!=0 );
1.3627 + pCrsr = pC->pCursor;
1.3628 + rc = sqlite3VdbeCursorMoveto(pC);
1.3629 + if( rc ) goto abort_due_to_error;
1.3630 + if( pC->isIndex ){
1.3631 + i64 n64;
1.3632 + assert( !pC->isTable );
1.3633 + sqlite3BtreeKeySize(pCrsr, &n64);
1.3634 + if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
1.3635 + goto too_big;
1.3636 + }
1.3637 + n = n64;
1.3638 + }else{
1.3639 + sqlite3BtreeDataSize(pCrsr, &n);
1.3640 + if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
1.3641 + goto too_big;
1.3642 + }
1.3643 + }
1.3644 + if( sqlite3VdbeMemGrow(pOut, n, 0) ){
1.3645 + goto no_mem;
1.3646 + }
1.3647 + pOut->n = n;
1.3648 + MemSetTypeFlag(pOut, MEM_Blob);
1.3649 + if( pC->isIndex ){
1.3650 + rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
1.3651 + }else{
1.3652 + rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
1.3653 + }
1.3654 + pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
1.3655 + UPDATE_MAX_BLOBSIZE(pOut);
1.3656 + break;
1.3657 +}
1.3658 +
1.3659 +/* Opcode: Rowid P1 P2 * * *
1.3660 +**
1.3661 +** Store in register P2 an integer which is the key of the table entry that
1.3662 +** P1 is currently point to.
1.3663 +*/
1.3664 +case OP_Rowid: { /* out2-prerelease */
1.3665 + int i = pOp->p1;
1.3666 + Cursor *pC;
1.3667 + i64 v;
1.3668 +
1.3669 + assert( i>=0 && i<p->nCursor );
1.3670 + pC = p->apCsr[i];
1.3671 + assert( pC!=0 );
1.3672 + rc = sqlite3VdbeCursorMoveto(pC);
1.3673 + if( rc ) goto abort_due_to_error;
1.3674 + if( pC->rowidIsValid ){
1.3675 + v = pC->lastRowid;
1.3676 + }else if( pC->pseudoTable ){
1.3677 + v = keyToInt(pC->iKey);
1.3678 + }else if( pC->nullRow ){
1.3679 + /* Leave the rowid set to a NULL */
1.3680 + break;
1.3681 + }else{
1.3682 + assert( pC->pCursor!=0 );
1.3683 + sqlite3BtreeKeySize(pC->pCursor, &v);
1.3684 + v = keyToInt(v);
1.3685 + }
1.3686 + pOut->u.i = v;
1.3687 + MemSetTypeFlag(pOut, MEM_Int);
1.3688 + break;
1.3689 +}
1.3690 +
1.3691 +/* Opcode: NullRow P1 * * * *
1.3692 +**
1.3693 +** Move the cursor P1 to a null row. Any OP_Column operations
1.3694 +** that occur while the cursor is on the null row will always
1.3695 +** write a NULL.
1.3696 +*/
1.3697 +case OP_NullRow: {
1.3698 + int i = pOp->p1;
1.3699 + Cursor *pC;
1.3700 +
1.3701 + assert( i>=0 && i<p->nCursor );
1.3702 + pC = p->apCsr[i];
1.3703 + assert( pC!=0 );
1.3704 + pC->nullRow = 1;
1.3705 + pC->rowidIsValid = 0;
1.3706 + if( pC->pCursor ){
1.3707 + sqlite3BtreeClearCursor(pC->pCursor);
1.3708 + }
1.3709 + break;
1.3710 +}
1.3711 +
1.3712 +/* Opcode: Last P1 P2 * * *
1.3713 +**
1.3714 +** The next use of the Rowid or Column or Next instruction for P1
1.3715 +** will refer to the last entry in the database table or index.
1.3716 +** If the table or index is empty and P2>0, then jump immediately to P2.
1.3717 +** If P2 is 0 or if the table or index is not empty, fall through
1.3718 +** to the following instruction.
1.3719 +*/
1.3720 +case OP_Last: { /* jump */
1.3721 + int i = pOp->p1;
1.3722 + Cursor *pC;
1.3723 + BtCursor *pCrsr;
1.3724 + int res;
1.3725 +
1.3726 + assert( i>=0 && i<p->nCursor );
1.3727 + pC = p->apCsr[i];
1.3728 + assert( pC!=0 );
1.3729 + pCrsr = pC->pCursor;
1.3730 + assert( pCrsr!=0 );
1.3731 + rc = sqlite3BtreeLast(pCrsr, &res);
1.3732 + pC->nullRow = res;
1.3733 + pC->deferredMoveto = 0;
1.3734 + pC->cacheStatus = CACHE_STALE;
1.3735 + if( res && pOp->p2>0 ){
1.3736 + pc = pOp->p2 - 1;
1.3737 + }
1.3738 + break;
1.3739 +}
1.3740 +
1.3741 +
1.3742 +/* Opcode: Sort P1 P2 * * *
1.3743 +**
1.3744 +** This opcode does exactly the same thing as OP_Rewind except that
1.3745 +** it increments an undocumented global variable used for testing.
1.3746 +**
1.3747 +** Sorting is accomplished by writing records into a sorting index,
1.3748 +** then rewinding that index and playing it back from beginning to
1.3749 +** end. We use the OP_Sort opcode instead of OP_Rewind to do the
1.3750 +** rewinding so that the global variable will be incremented and
1.3751 +** regression tests can determine whether or not the optimizer is
1.3752 +** correctly optimizing out sorts.
1.3753 +*/
1.3754 +case OP_Sort: { /* jump */
1.3755 +#ifdef SQLITE_TEST
1.3756 + sqlite3_sort_count++;
1.3757 + sqlite3_search_count--;
1.3758 +#endif
1.3759 + p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
1.3760 + /* Fall through into OP_Rewind */
1.3761 +}
1.3762 +/* Opcode: Rewind P1 P2 * * *
1.3763 +**
1.3764 +** The next use of the Rowid or Column or Next instruction for P1
1.3765 +** will refer to the first entry in the database table or index.
1.3766 +** If the table or index is empty and P2>0, then jump immediately to P2.
1.3767 +** If P2 is 0 or if the table or index is not empty, fall through
1.3768 +** to the following instruction.
1.3769 +*/
1.3770 +case OP_Rewind: { /* jump */
1.3771 + int i = pOp->p1;
1.3772 + Cursor *pC;
1.3773 + BtCursor *pCrsr;
1.3774 + int res;
1.3775 +
1.3776 + assert( i>=0 && i<p->nCursor );
1.3777 + pC = p->apCsr[i];
1.3778 + assert( pC!=0 );
1.3779 + if( (pCrsr = pC->pCursor)!=0 ){
1.3780 + rc = sqlite3BtreeFirst(pCrsr, &res);
1.3781 + pC->atFirst = res==0;
1.3782 + pC->deferredMoveto = 0;
1.3783 + pC->cacheStatus = CACHE_STALE;
1.3784 + }else{
1.3785 + res = 1;
1.3786 + }
1.3787 + pC->nullRow = res;
1.3788 + assert( pOp->p2>0 && pOp->p2<p->nOp );
1.3789 + if( res ){
1.3790 + pc = pOp->p2 - 1;
1.3791 + }
1.3792 + break;
1.3793 +}
1.3794 +
1.3795 +/* Opcode: Next P1 P2 * * *
1.3796 +**
1.3797 +** Advance cursor P1 so that it points to the next key/data pair in its
1.3798 +** table or index. If there are no more key/value pairs then fall through
1.3799 +** to the following instruction. But if the cursor advance was successful,
1.3800 +** jump immediately to P2.
1.3801 +**
1.3802 +** The P1 cursor must be for a real table, not a pseudo-table.
1.3803 +**
1.3804 +** See also: Prev
1.3805 +*/
1.3806 +/* Opcode: Prev P1 P2 * * *
1.3807 +**
1.3808 +** Back up cursor P1 so that it points to the previous key/data pair in its
1.3809 +** table or index. If there is no previous key/value pairs then fall through
1.3810 +** to the following instruction. But if the cursor backup was successful,
1.3811 +** jump immediately to P2.
1.3812 +**
1.3813 +** The P1 cursor must be for a real table, not a pseudo-table.
1.3814 +*/
1.3815 +case OP_Prev: /* jump */
1.3816 +case OP_Next: { /* jump */
1.3817 + Cursor *pC;
1.3818 + BtCursor *pCrsr;
1.3819 + int res;
1.3820 +
1.3821 + CHECK_FOR_INTERRUPT;
1.3822 + assert( pOp->p1>=0 && pOp->p1<p->nCursor );
1.3823 + pC = p->apCsr[pOp->p1];
1.3824 + if( pC==0 ){
1.3825 + break; /* See ticket #2273 */
1.3826 + }
1.3827 + pCrsr = pC->pCursor;
1.3828 + assert( pCrsr );
1.3829 + res = 1;
1.3830 + assert( pC->deferredMoveto==0 );
1.3831 + rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
1.3832 + sqlite3BtreePrevious(pCrsr, &res);
1.3833 + pC->nullRow = res;
1.3834 + pC->cacheStatus = CACHE_STALE;
1.3835 + if( res==0 ){
1.3836 + pc = pOp->p2 - 1;
1.3837 + if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
1.3838 +#ifdef SQLITE_TEST
1.3839 + sqlite3_search_count++;
1.3840 +#endif
1.3841 + }
1.3842 + pC->rowidIsValid = 0;
1.3843 + break;
1.3844 +}
1.3845 +
1.3846 +/* Opcode: IdxInsert P1 P2 P3 * *
1.3847 +**
1.3848 +** Register P2 holds a SQL index key made using the
1.3849 +** MakeIdxRec instructions. This opcode writes that key
1.3850 +** into the index P1. Data for the entry is nil.
1.3851 +**
1.3852 +** P3 is a flag that provides a hint to the b-tree layer that this
1.3853 +** insert is likely to be an append.
1.3854 +**
1.3855 +** This instruction only works for indices. The equivalent instruction
1.3856 +** for tables is OP_Insert.
1.3857 +*/
1.3858 +case OP_IdxInsert: { /* in2 */
1.3859 + int i = pOp->p1;
1.3860 + Cursor *pC;
1.3861 + BtCursor *pCrsr;
1.3862 + assert( i>=0 && i<p->nCursor );
1.3863 + assert( p->apCsr[i]!=0 );
1.3864 + assert( pIn2->flags & MEM_Blob );
1.3865 + if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
1.3866 + assert( pC->isTable==0 );
1.3867 + rc = ExpandBlob(pIn2);
1.3868 + if( rc==SQLITE_OK ){
1.3869 + int nKey = pIn2->n;
1.3870 + const char *zKey = pIn2->z;
1.3871 + rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3);
1.3872 + assert( pC->deferredMoveto==0 );
1.3873 + pC->cacheStatus = CACHE_STALE;
1.3874 + }
1.3875 + }
1.3876 + break;
1.3877 +}
1.3878 +
1.3879 +/* Opcode: IdxDelete P1 P2 P3 * *
1.3880 +**
1.3881 +** The content of P3 registers starting at register P2 form
1.3882 +** an unpacked index key. This opcode removes that entry from the
1.3883 +** index opened by cursor P1.
1.3884 +*/
1.3885 +case OP_IdxDelete: {
1.3886 + int i = pOp->p1;
1.3887 + Cursor *pC;
1.3888 + BtCursor *pCrsr;
1.3889 + assert( pOp->p3>0 );
1.3890 + assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem );
1.3891 + assert( i>=0 && i<p->nCursor );
1.3892 + assert( p->apCsr[i]!=0 );
1.3893 + if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
1.3894 + int res;
1.3895 + UnpackedRecord r;
1.3896 + r.pKeyInfo = pC->pKeyInfo;
1.3897 + r.nField = pOp->p3;
1.3898 + r.flags = 0;
1.3899 + r.aMem = &p->aMem[pOp->p2];
1.3900 + rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
1.3901 + if( rc==SQLITE_OK && res==0 ){
1.3902 + rc = sqlite3BtreeDelete(pCrsr);
1.3903 + }
1.3904 + assert( pC->deferredMoveto==0 );
1.3905 + pC->cacheStatus = CACHE_STALE;
1.3906 + }
1.3907 + break;
1.3908 +}
1.3909 +
1.3910 +/* Opcode: IdxRowid P1 P2 * * *
1.3911 +**
1.3912 +** Write into register P2 an integer which is the last entry in the record at
1.3913 +** the end of the index key pointed to by cursor P1. This integer should be
1.3914 +** the rowid of the table entry to which this index entry points.
1.3915 +**
1.3916 +** See also: Rowid, MakeIdxRec.
1.3917 +*/
1.3918 +case OP_IdxRowid: { /* out2-prerelease */
1.3919 + int i = pOp->p1;
1.3920 + BtCursor *pCrsr;
1.3921 + Cursor *pC;
1.3922 +
1.3923 + assert( i>=0 && i<p->nCursor );
1.3924 + assert( p->apCsr[i]!=0 );
1.3925 + if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
1.3926 + i64 rowid;
1.3927 +
1.3928 + assert( pC->deferredMoveto==0 );
1.3929 + assert( pC->isTable==0 );
1.3930 + if( !pC->nullRow ){
1.3931 + rc = sqlite3VdbeIdxRowid(pCrsr, &rowid);
1.3932 + if( rc!=SQLITE_OK ){
1.3933 + goto abort_due_to_error;
1.3934 + }
1.3935 + MemSetTypeFlag(pOut, MEM_Int);
1.3936 + pOut->u.i = rowid;
1.3937 + }
1.3938 + }
1.3939 + break;
1.3940 +}
1.3941 +
1.3942 +/* Opcode: IdxGE P1 P2 P3 P4 P5
1.3943 +**
1.3944 +** The P4 register values beginning with P3 form an unpacked index
1.3945 +** key that omits the ROWID. Compare this key value against the index
1.3946 +** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
1.3947 +**
1.3948 +** If the P1 index entry is greater than or equal to the key value
1.3949 +** then jump to P2. Otherwise fall through to the next instruction.
1.3950 +**
1.3951 +** If P5 is non-zero then the key value is increased by an epsilon
1.3952 +** prior to the comparison. This make the opcode work like IdxGT except
1.3953 +** that if the key from register P3 is a prefix of the key in the cursor,
1.3954 +** the result is false whereas it would be true with IdxGT.
1.3955 +*/
1.3956 +/* Opcode: IdxLT P1 P2 P3 * P5
1.3957 +**
1.3958 +** The P4 register values beginning with P3 form an unpacked index
1.3959 +** key that omits the ROWID. Compare this key value against the index
1.3960 +** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
1.3961 +**
1.3962 +** If the P1 index entry is less than the key value then jump to P2.
1.3963 +** Otherwise fall through to the next instruction.
1.3964 +**
1.3965 +** If P5 is non-zero then the key value is increased by an epsilon prior
1.3966 +** to the comparison. This makes the opcode work like IdxLE.
1.3967 +*/
1.3968 +case OP_IdxLT: /* jump, in3 */
1.3969 +case OP_IdxGE: { /* jump, in3 */
1.3970 + int i= pOp->p1;
1.3971 + Cursor *pC;
1.3972 +
1.3973 + assert( i>=0 && i<p->nCursor );
1.3974 + assert( p->apCsr[i]!=0 );
1.3975 + if( (pC = p->apCsr[i])->pCursor!=0 ){
1.3976 + int res;
1.3977 + UnpackedRecord r;
1.3978 + assert( pC->deferredMoveto==0 );
1.3979 + assert( pOp->p5==0 || pOp->p5==1 );
1.3980 + assert( pOp->p4type==P4_INT32 );
1.3981 + r.pKeyInfo = pC->pKeyInfo;
1.3982 + r.nField = pOp->p4.i;
1.3983 + if( pOp->p5 ){
1.3984 + r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
1.3985 + }else{
1.3986 + r.flags = UNPACKED_IGNORE_ROWID;
1.3987 + }
1.3988 + r.aMem = &p->aMem[pOp->p3];
1.3989 + rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
1.3990 + if( pOp->opcode==OP_IdxLT ){
1.3991 + res = -res;
1.3992 + }else{
1.3993 + assert( pOp->opcode==OP_IdxGE );
1.3994 + res++;
1.3995 + }
1.3996 + if( res>0 ){
1.3997 + pc = pOp->p2 - 1 ;
1.3998 + }
1.3999 + }
1.4000 + break;
1.4001 +}
1.4002 +
1.4003 +/* Opcode: Destroy P1 P2 P3 * *
1.4004 +**
1.4005 +** Delete an entire database table or index whose root page in the database
1.4006 +** file is given by P1.
1.4007 +**
1.4008 +** The table being destroyed is in the main database file if P3==0. If
1.4009 +** P3==1 then the table to be clear is in the auxiliary database file
1.4010 +** that is used to store tables create using CREATE TEMPORARY TABLE.
1.4011 +**
1.4012 +** If AUTOVACUUM is enabled then it is possible that another root page
1.4013 +** might be moved into the newly deleted root page in order to keep all
1.4014 +** root pages contiguous at the beginning of the database. The former
1.4015 +** value of the root page that moved - its value before the move occurred -
1.4016 +** is stored in register P2. If no page
1.4017 +** movement was required (because the table being dropped was already
1.4018 +** the last one in the database) then a zero is stored in register P2.
1.4019 +** If AUTOVACUUM is disabled then a zero is stored in register P2.
1.4020 +**
1.4021 +** See also: Clear
1.4022 +*/
1.4023 +case OP_Destroy: { /* out2-prerelease */
1.4024 + int iMoved;
1.4025 + int iCnt;
1.4026 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.4027 + Vdbe *pVdbe;
1.4028 + iCnt = 0;
1.4029 + for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
1.4030 + if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){
1.4031 + iCnt++;
1.4032 + }
1.4033 + }
1.4034 +#else
1.4035 + iCnt = db->activeVdbeCnt;
1.4036 +#endif
1.4037 + if( iCnt>1 ){
1.4038 + rc = SQLITE_LOCKED;
1.4039 + p->errorAction = OE_Abort;
1.4040 + }else{
1.4041 + int iDb = pOp->p3;
1.4042 + assert( iCnt==1 );
1.4043 + assert( (p->btreeMask & (1<<iDb))!=0 );
1.4044 + rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
1.4045 + MemSetTypeFlag(pOut, MEM_Int);
1.4046 + pOut->u.i = iMoved;
1.4047 +#ifndef SQLITE_OMIT_AUTOVACUUM
1.4048 + if( rc==SQLITE_OK && iMoved!=0 ){
1.4049 + sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1);
1.4050 + }
1.4051 +#endif
1.4052 + }
1.4053 + break;
1.4054 +}
1.4055 +
1.4056 +/* Opcode: Clear P1 P2 *
1.4057 +**
1.4058 +** Delete all contents of the database table or index whose root page
1.4059 +** in the database file is given by P1. But, unlike Destroy, do not
1.4060 +** remove the table or index from the database file.
1.4061 +**
1.4062 +** The table being clear is in the main database file if P2==0. If
1.4063 +** P2==1 then the table to be clear is in the auxiliary database file
1.4064 +** that is used to store tables create using CREATE TEMPORARY TABLE.
1.4065 +**
1.4066 +** See also: Destroy
1.4067 +*/
1.4068 +case OP_Clear: {
1.4069 + assert( (p->btreeMask & (1<<pOp->p2))!=0 );
1.4070 + rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1);
1.4071 + break;
1.4072 +}
1.4073 +
1.4074 +/* Opcode: CreateTable P1 P2 * * *
1.4075 +**
1.4076 +** Allocate a new table in the main database file if P1==0 or in the
1.4077 +** auxiliary database file if P1==1 or in an attached database if
1.4078 +** P1>1. Write the root page number of the new table into
1.4079 +** register P2
1.4080 +**
1.4081 +** The difference between a table and an index is this: A table must
1.4082 +** have a 4-byte integer key and can have arbitrary data. An index
1.4083 +** has an arbitrary key but no data.
1.4084 +**
1.4085 +** See also: CreateIndex
1.4086 +*/
1.4087 +/* Opcode: CreateIndex P1 P2 * * *
1.4088 +**
1.4089 +** Allocate a new index in the main database file if P1==0 or in the
1.4090 +** auxiliary database file if P1==1 or in an attached database if
1.4091 +** P1>1. Write the root page number of the new table into
1.4092 +** register P2.
1.4093 +**
1.4094 +** See documentation on OP_CreateTable for additional information.
1.4095 +*/
1.4096 +case OP_CreateIndex: /* out2-prerelease */
1.4097 +case OP_CreateTable: { /* out2-prerelease */
1.4098 + int pgno;
1.4099 + int flags;
1.4100 + Db *pDb;
1.4101 + assert( pOp->p1>=0 && pOp->p1<db->nDb );
1.4102 + assert( (p->btreeMask & (1<<pOp->p1))!=0 );
1.4103 + pDb = &db->aDb[pOp->p1];
1.4104 + assert( pDb->pBt!=0 );
1.4105 + if( pOp->opcode==OP_CreateTable ){
1.4106 + /* flags = BTREE_INTKEY; */
1.4107 + flags = BTREE_LEAFDATA|BTREE_INTKEY;
1.4108 + }else{
1.4109 + flags = BTREE_ZERODATA;
1.4110 + }
1.4111 + rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
1.4112 + if( rc==SQLITE_OK ){
1.4113 + pOut->u.i = pgno;
1.4114 + MemSetTypeFlag(pOut, MEM_Int);
1.4115 + }
1.4116 + break;
1.4117 +}
1.4118 +
1.4119 +/* Opcode: ParseSchema P1 P2 * P4 *
1.4120 +**
1.4121 +** Read and parse all entries from the SQLITE_MASTER table of database P1
1.4122 +** that match the WHERE clause P4. P2 is the "force" flag. Always do
1.4123 +** the parsing if P2 is true. If P2 is false, then this routine is a
1.4124 +** no-op if the schema is not currently loaded. In other words, if P2
1.4125 +** is false, the SQLITE_MASTER table is only parsed if the rest of the
1.4126 +** schema is already loaded into the symbol table.
1.4127 +**
1.4128 +** This opcode invokes the parser to create a new virtual machine,
1.4129 +** then runs the new virtual machine. It is thus a re-entrant opcode.
1.4130 +*/
1.4131 +case OP_ParseSchema: {
1.4132 + char *zSql;
1.4133 + int iDb = pOp->p1;
1.4134 + const char *zMaster;
1.4135 + InitData initData;
1.4136 +
1.4137 + assert( iDb>=0 && iDb<db->nDb );
1.4138 + if( !pOp->p2 && !DbHasProperty(db, iDb, DB_SchemaLoaded) ){
1.4139 + break;
1.4140 + }
1.4141 + zMaster = SCHEMA_TABLE(iDb);
1.4142 + initData.db = db;
1.4143 + initData.iDb = pOp->p1;
1.4144 + initData.pzErrMsg = &p->zErrMsg;
1.4145 + zSql = sqlite3MPrintf(db,
1.4146 + "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
1.4147 + db->aDb[iDb].zName, zMaster, pOp->p4.z);
1.4148 + if( zSql==0 ) goto no_mem;
1.4149 + (void)sqlite3SafetyOff(db);
1.4150 + assert( db->init.busy==0 );
1.4151 + db->init.busy = 1;
1.4152 + initData.rc = SQLITE_OK;
1.4153 + assert( !db->mallocFailed );
1.4154 + rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
1.4155 + if( rc==SQLITE_OK ) rc = initData.rc;
1.4156 + sqlite3DbFree(db, zSql);
1.4157 + db->init.busy = 0;
1.4158 + (void)sqlite3SafetyOn(db);
1.4159 + if( rc==SQLITE_NOMEM ){
1.4160 + goto no_mem;
1.4161 + }
1.4162 + break;
1.4163 +}
1.4164 +
1.4165 +#if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER)
1.4166 +/* Opcode: LoadAnalysis P1 * * * *
1.4167 +**
1.4168 +** Read the sqlite_stat1 table for database P1 and load the content
1.4169 +** of that table into the internal index hash table. This will cause
1.4170 +** the analysis to be used when preparing all subsequent queries.
1.4171 +*/
1.4172 +case OP_LoadAnalysis: {
1.4173 + int iDb = pOp->p1;
1.4174 + assert( iDb>=0 && iDb<db->nDb );
1.4175 + rc = sqlite3AnalysisLoad(db, iDb);
1.4176 + break;
1.4177 +}
1.4178 +#endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) */
1.4179 +
1.4180 +/* Opcode: DropTable P1 * * P4 *
1.4181 +**
1.4182 +** Remove the internal (in-memory) data structures that describe
1.4183 +** the table named P4 in database P1. This is called after a table
1.4184 +** is dropped in order to keep the internal representation of the
1.4185 +** schema consistent with what is on disk.
1.4186 +*/
1.4187 +case OP_DropTable: {
1.4188 + sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
1.4189 + break;
1.4190 +}
1.4191 +
1.4192 +/* Opcode: DropIndex P1 * * P4 *
1.4193 +**
1.4194 +** Remove the internal (in-memory) data structures that describe
1.4195 +** the index named P4 in database P1. This is called after an index
1.4196 +** is dropped in order to keep the internal representation of the
1.4197 +** schema consistent with what is on disk.
1.4198 +*/
1.4199 +case OP_DropIndex: {
1.4200 + sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
1.4201 + break;
1.4202 +}
1.4203 +
1.4204 +/* Opcode: DropTrigger P1 * * P4 *
1.4205 +**
1.4206 +** Remove the internal (in-memory) data structures that describe
1.4207 +** the trigger named P4 in database P1. This is called after a trigger
1.4208 +** is dropped in order to keep the internal representation of the
1.4209 +** schema consistent with what is on disk.
1.4210 +*/
1.4211 +case OP_DropTrigger: {
1.4212 + sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
1.4213 + break;
1.4214 +}
1.4215 +
1.4216 +
1.4217 +#ifndef SQLITE_OMIT_INTEGRITY_CHECK
1.4218 +/* Opcode: IntegrityCk P1 P2 P3 * P5
1.4219 +**
1.4220 +** Do an analysis of the currently open database. Store in
1.4221 +** register P1 the text of an error message describing any problems.
1.4222 +** If no problems are found, store a NULL in register P1.
1.4223 +**
1.4224 +** The register P3 contains the maximum number of allowed errors.
1.4225 +** At most reg(P3) errors will be reported.
1.4226 +** In other words, the analysis stops as soon as reg(P1) errors are
1.4227 +** seen. Reg(P1) is updated with the number of errors remaining.
1.4228 +**
1.4229 +** The root page numbers of all tables in the database are integer
1.4230 +** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
1.4231 +** total.
1.4232 +**
1.4233 +** If P5 is not zero, the check is done on the auxiliary database
1.4234 +** file, not the main database file.
1.4235 +**
1.4236 +** This opcode is used to implement the integrity_check pragma.
1.4237 +*/
1.4238 +case OP_IntegrityCk: {
1.4239 + int nRoot; /* Number of tables to check. (Number of root pages.) */
1.4240 + int *aRoot; /* Array of rootpage numbers for tables to be checked */
1.4241 + int j; /* Loop counter */
1.4242 + int nErr; /* Number of errors reported */
1.4243 + char *z; /* Text of the error report */
1.4244 + Mem *pnErr; /* Register keeping track of errors remaining */
1.4245 +
1.4246 + nRoot = pOp->p2;
1.4247 + assert( nRoot>0 );
1.4248 + aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
1.4249 + if( aRoot==0 ) goto no_mem;
1.4250 + assert( pOp->p3>0 && pOp->p3<=p->nMem );
1.4251 + pnErr = &p->aMem[pOp->p3];
1.4252 + assert( (pnErr->flags & MEM_Int)!=0 );
1.4253 + assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
1.4254 + pIn1 = &p->aMem[pOp->p1];
1.4255 + for(j=0; j<nRoot; j++){
1.4256 + aRoot[j] = sqlite3VdbeIntValue(&pIn1[j]);
1.4257 + }
1.4258 + aRoot[j] = 0;
1.4259 + assert( pOp->p5<db->nDb );
1.4260 + assert( (p->btreeMask & (1<<pOp->p5))!=0 );
1.4261 + z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
1.4262 + pnErr->u.i, &nErr);
1.4263 + sqlite3DbFree(db, aRoot);
1.4264 + pnErr->u.i -= nErr;
1.4265 + sqlite3VdbeMemSetNull(pIn1);
1.4266 + if( nErr==0 ){
1.4267 + assert( z==0 );
1.4268 + }else if( z==0 ){
1.4269 + goto no_mem;
1.4270 + }else{
1.4271 + sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
1.4272 + }
1.4273 + UPDATE_MAX_BLOBSIZE(pIn1);
1.4274 + sqlite3VdbeChangeEncoding(pIn1, encoding);
1.4275 + break;
1.4276 +}
1.4277 +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1.4278 +
1.4279 +/* Opcode: FifoWrite P1 * * * *
1.4280 +**
1.4281 +** Write the integer from register P1 into the Fifo.
1.4282 +*/
1.4283 +case OP_FifoWrite: { /* in1 */
1.4284 + p->sFifo.db = db;
1.4285 + if( sqlite3VdbeFifoPush(&p->sFifo, sqlite3VdbeIntValue(pIn1))==SQLITE_NOMEM ){
1.4286 + goto no_mem;
1.4287 + }
1.4288 + break;
1.4289 +}
1.4290 +
1.4291 +/* Opcode: FifoRead P1 P2 * * *
1.4292 +**
1.4293 +** Attempt to read a single integer from the Fifo. Store that
1.4294 +** integer in register P1.
1.4295 +**
1.4296 +** If the Fifo is empty jump to P2.
1.4297 +*/
1.4298 +case OP_FifoRead: { /* jump */
1.4299 + CHECK_FOR_INTERRUPT;
1.4300 + assert( pOp->p1>0 && pOp->p1<=p->nMem );
1.4301 + pOut = &p->aMem[pOp->p1];
1.4302 + MemSetTypeFlag(pOut, MEM_Int);
1.4303 + if( sqlite3VdbeFifoPop(&p->sFifo, &pOut->u.i)==SQLITE_DONE ){
1.4304 + pc = pOp->p2 - 1;
1.4305 + }
1.4306 + break;
1.4307 +}
1.4308 +
1.4309 +#ifndef SQLITE_OMIT_TRIGGER
1.4310 +/* Opcode: ContextPush * * *
1.4311 +**
1.4312 +** Save the current Vdbe context such that it can be restored by a ContextPop
1.4313 +** opcode. The context stores the last insert row id, the last statement change
1.4314 +** count, and the current statement change count.
1.4315 +*/
1.4316 +case OP_ContextPush: {
1.4317 + int i = p->contextStackTop++;
1.4318 + Context *pContext;
1.4319 +
1.4320 + assert( i>=0 );
1.4321 + /* FIX ME: This should be allocated as part of the vdbe at compile-time */
1.4322 + if( i>=p->contextStackDepth ){
1.4323 + p->contextStackDepth = i+1;
1.4324 + p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack,
1.4325 + sizeof(Context)*(i+1));
1.4326 + if( p->contextStack==0 ) goto no_mem;
1.4327 + }
1.4328 + pContext = &p->contextStack[i];
1.4329 + pContext->lastRowid = db->lastRowid;
1.4330 + pContext->nChange = p->nChange;
1.4331 + pContext->sFifo = p->sFifo;
1.4332 + sqlite3VdbeFifoInit(&p->sFifo, db);
1.4333 + break;
1.4334 +}
1.4335 +
1.4336 +/* Opcode: ContextPop * * *
1.4337 +**
1.4338 +** Restore the Vdbe context to the state it was in when contextPush was last
1.4339 +** executed. The context stores the last insert row id, the last statement
1.4340 +** change count, and the current statement change count.
1.4341 +*/
1.4342 +case OP_ContextPop: {
1.4343 + Context *pContext = &p->contextStack[--p->contextStackTop];
1.4344 + assert( p->contextStackTop>=0 );
1.4345 + db->lastRowid = pContext->lastRowid;
1.4346 + p->nChange = pContext->nChange;
1.4347 + sqlite3VdbeFifoClear(&p->sFifo);
1.4348 + p->sFifo = pContext->sFifo;
1.4349 + break;
1.4350 +}
1.4351 +#endif /* #ifndef SQLITE_OMIT_TRIGGER */
1.4352 +
1.4353 +#ifndef SQLITE_OMIT_AUTOINCREMENT
1.4354 +/* Opcode: MemMax P1 P2 * * *
1.4355 +**
1.4356 +** Set the value of register P1 to the maximum of its current value
1.4357 +** and the value in register P2.
1.4358 +**
1.4359 +** This instruction throws an error if the memory cell is not initially
1.4360 +** an integer.
1.4361 +*/
1.4362 +case OP_MemMax: { /* in1, in2 */
1.4363 + sqlite3VdbeMemIntegerify(pIn1);
1.4364 + sqlite3VdbeMemIntegerify(pIn2);
1.4365 + if( pIn1->u.i<pIn2->u.i){
1.4366 + pIn1->u.i = pIn2->u.i;
1.4367 + }
1.4368 + break;
1.4369 +}
1.4370 +#endif /* SQLITE_OMIT_AUTOINCREMENT */
1.4371 +
1.4372 +/* Opcode: IfPos P1 P2 * * *
1.4373 +**
1.4374 +** If the value of register P1 is 1 or greater, jump to P2.
1.4375 +**
1.4376 +** It is illegal to use this instruction on a register that does
1.4377 +** not contain an integer. An assertion fault will result if you try.
1.4378 +*/
1.4379 +case OP_IfPos: { /* jump, in1 */
1.4380 + assert( pIn1->flags&MEM_Int );
1.4381 + if( pIn1->u.i>0 ){
1.4382 + pc = pOp->p2 - 1;
1.4383 + }
1.4384 + break;
1.4385 +}
1.4386 +
1.4387 +/* Opcode: IfNeg P1 P2 * * *
1.4388 +**
1.4389 +** If the value of register P1 is less than zero, jump to P2.
1.4390 +**
1.4391 +** It is illegal to use this instruction on a register that does
1.4392 +** not contain an integer. An assertion fault will result if you try.
1.4393 +*/
1.4394 +case OP_IfNeg: { /* jump, in1 */
1.4395 + assert( pIn1->flags&MEM_Int );
1.4396 + if( pIn1->u.i<0 ){
1.4397 + pc = pOp->p2 - 1;
1.4398 + }
1.4399 + break;
1.4400 +}
1.4401 +
1.4402 +/* Opcode: IfZero P1 P2 * * *
1.4403 +**
1.4404 +** If the value of register P1 is exactly 0, jump to P2.
1.4405 +**
1.4406 +** It is illegal to use this instruction on a register that does
1.4407 +** not contain an integer. An assertion fault will result if you try.
1.4408 +*/
1.4409 +case OP_IfZero: { /* jump, in1 */
1.4410 + assert( pIn1->flags&MEM_Int );
1.4411 + if( pIn1->u.i==0 ){
1.4412 + pc = pOp->p2 - 1;
1.4413 + }
1.4414 + break;
1.4415 +}
1.4416 +
1.4417 +/* Opcode: AggStep * P2 P3 P4 P5
1.4418 +**
1.4419 +** Execute the step function for an aggregate. The
1.4420 +** function has P5 arguments. P4 is a pointer to the FuncDef
1.4421 +** structure that specifies the function. Use register
1.4422 +** P3 as the accumulator.
1.4423 +**
1.4424 +** The P5 arguments are taken from register P2 and its
1.4425 +** successors.
1.4426 +*/
1.4427 +case OP_AggStep: {
1.4428 + int n = pOp->p5;
1.4429 + int i;
1.4430 + Mem *pMem, *pRec;
1.4431 + sqlite3_context ctx;
1.4432 + sqlite3_value **apVal;
1.4433 +
1.4434 + assert( n>=0 );
1.4435 + pRec = &p->aMem[pOp->p2];
1.4436 + apVal = p->apArg;
1.4437 + assert( apVal || n==0 );
1.4438 + for(i=0; i<n; i++, pRec++){
1.4439 + apVal[i] = pRec;
1.4440 + storeTypeInfo(pRec, encoding);
1.4441 + }
1.4442 + ctx.pFunc = pOp->p4.pFunc;
1.4443 + assert( pOp->p3>0 && pOp->p3<=p->nMem );
1.4444 + ctx.pMem = pMem = &p->aMem[pOp->p3];
1.4445 + pMem->n++;
1.4446 + ctx.s.flags = MEM_Null;
1.4447 + ctx.s.z = 0;
1.4448 + ctx.s.zMalloc = 0;
1.4449 + ctx.s.xDel = 0;
1.4450 + ctx.s.db = db;
1.4451 + ctx.isError = 0;
1.4452 + ctx.pColl = 0;
1.4453 + if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
1.4454 + assert( pOp>p->aOp );
1.4455 + assert( pOp[-1].p4type==P4_COLLSEQ );
1.4456 + assert( pOp[-1].opcode==OP_CollSeq );
1.4457 + ctx.pColl = pOp[-1].p4.pColl;
1.4458 + }
1.4459 + (ctx.pFunc->xStep)(&ctx, n, apVal);
1.4460 + if( ctx.isError ){
1.4461 + sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
1.4462 + rc = ctx.isError;
1.4463 + }
1.4464 + sqlite3VdbeMemRelease(&ctx.s);
1.4465 + break;
1.4466 +}
1.4467 +
1.4468 +/* Opcode: AggFinal P1 P2 * P4 *
1.4469 +**
1.4470 +** Execute the finalizer function for an aggregate. P1 is
1.4471 +** the memory location that is the accumulator for the aggregate.
1.4472 +**
1.4473 +** P2 is the number of arguments that the step function takes and
1.4474 +** P4 is a pointer to the FuncDef for this function. The P2
1.4475 +** argument is not used by this opcode. It is only there to disambiguate
1.4476 +** functions that can take varying numbers of arguments. The
1.4477 +** P4 argument is only needed for the degenerate case where
1.4478 +** the step function was not previously called.
1.4479 +*/
1.4480 +case OP_AggFinal: {
1.4481 + Mem *pMem;
1.4482 + assert( pOp->p1>0 && pOp->p1<=p->nMem );
1.4483 + pMem = &p->aMem[pOp->p1];
1.4484 + assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
1.4485 + rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
1.4486 + if( rc==SQLITE_ERROR ){
1.4487 + sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
1.4488 + }
1.4489 + sqlite3VdbeChangeEncoding(pMem, encoding);
1.4490 + UPDATE_MAX_BLOBSIZE(pMem);
1.4491 + if( sqlite3VdbeMemTooBig(pMem) ){
1.4492 + goto too_big;
1.4493 + }
1.4494 + break;
1.4495 +}
1.4496 +
1.4497 +
1.4498 +#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
1.4499 +/* Opcode: Vacuum * * * * *
1.4500 +**
1.4501 +** Vacuum the entire database. This opcode will cause other virtual
1.4502 +** machines to be created and run. It may not be called from within
1.4503 +** a transaction.
1.4504 +*/
1.4505 +case OP_Vacuum: {
1.4506 + if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1.4507 + rc = sqlite3RunVacuum(&p->zErrMsg, db);
1.4508 + if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
1.4509 + break;
1.4510 +}
1.4511 +#endif
1.4512 +
1.4513 +#if !defined(SQLITE_OMIT_AUTOVACUUM)
1.4514 +/* Opcode: IncrVacuum P1 P2 * * *
1.4515 +**
1.4516 +** Perform a single step of the incremental vacuum procedure on
1.4517 +** the P1 database. If the vacuum has finished, jump to instruction
1.4518 +** P2. Otherwise, fall through to the next instruction.
1.4519 +*/
1.4520 +case OP_IncrVacuum: { /* jump */
1.4521 + Btree *pBt;
1.4522 +
1.4523 + assert( pOp->p1>=0 && pOp->p1<db->nDb );
1.4524 + assert( (p->btreeMask & (1<<pOp->p1))!=0 );
1.4525 + pBt = db->aDb[pOp->p1].pBt;
1.4526 + rc = sqlite3BtreeIncrVacuum(pBt);
1.4527 + if( rc==SQLITE_DONE ){
1.4528 + pc = pOp->p2 - 1;
1.4529 + rc = SQLITE_OK;
1.4530 + }
1.4531 + break;
1.4532 +}
1.4533 +#endif
1.4534 +
1.4535 +/* Opcode: Expire P1 * * * *
1.4536 +**
1.4537 +** Cause precompiled statements to become expired. An expired statement
1.4538 +** fails with an error code of SQLITE_SCHEMA if it is ever executed
1.4539 +** (via sqlite3_step()).
1.4540 +**
1.4541 +** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
1.4542 +** then only the currently executing statement is affected.
1.4543 +*/
1.4544 +case OP_Expire: {
1.4545 + if( !pOp->p1 ){
1.4546 + sqlite3ExpirePreparedStatements(db);
1.4547 + }else{
1.4548 + p->expired = 1;
1.4549 + }
1.4550 + break;
1.4551 +}
1.4552 +
1.4553 +#ifndef SQLITE_OMIT_SHARED_CACHE
1.4554 +/* Opcode: TableLock P1 P2 P3 P4 *
1.4555 +**
1.4556 +** Obtain a lock on a particular table. This instruction is only used when
1.4557 +** the shared-cache feature is enabled.
1.4558 +**
1.4559 +** If P1 is the index of the database in sqlite3.aDb[] of the database
1.4560 +** on which the lock is acquired. A readlock is obtained if P3==0 or
1.4561 +** a write lock if P3==1.
1.4562 +**
1.4563 +** P2 contains the root-page of the table to lock.
1.4564 +**
1.4565 +** P4 contains a pointer to the name of the table being locked. This is only
1.4566 +** used to generate an error message if the lock cannot be obtained.
1.4567 +*/
1.4568 +case OP_TableLock: {
1.4569 + int p1 = pOp->p1;
1.4570 + u8 isWriteLock = pOp->p3;
1.4571 + assert( p1>=0 && p1<db->nDb );
1.4572 + assert( (p->btreeMask & (1<<p1))!=0 );
1.4573 + assert( isWriteLock==0 || isWriteLock==1 );
1.4574 + rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
1.4575 + if( rc==SQLITE_LOCKED ){
1.4576 + const char *z = pOp->p4.z;
1.4577 + sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
1.4578 + }
1.4579 + break;
1.4580 +}
1.4581 +#endif /* SQLITE_OMIT_SHARED_CACHE */
1.4582 +
1.4583 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.4584 +/* Opcode: VBegin * * * P4 *
1.4585 +**
1.4586 +** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
1.4587 +** xBegin method for that table.
1.4588 +**
1.4589 +** Also, whether or not P4 is set, check that this is not being called from
1.4590 +** within a callback to a virtual table xSync() method. If it is, set the
1.4591 +** error code to SQLITE_LOCKED.
1.4592 +*/
1.4593 +case OP_VBegin: {
1.4594 + sqlite3_vtab *pVtab = pOp->p4.pVtab;
1.4595 + rc = sqlite3VtabBegin(db, pVtab);
1.4596 + if( pVtab ){
1.4597 + sqlite3DbFree(db, p->zErrMsg);
1.4598 + p->zErrMsg = pVtab->zErrMsg;
1.4599 + pVtab->zErrMsg = 0;
1.4600 + }
1.4601 + break;
1.4602 +}
1.4603 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.4604 +
1.4605 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.4606 +/* Opcode: VCreate P1 * * P4 *
1.4607 +**
1.4608 +** P4 is the name of a virtual table in database P1. Call the xCreate method
1.4609 +** for that table.
1.4610 +*/
1.4611 +case OP_VCreate: {
1.4612 + rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
1.4613 + break;
1.4614 +}
1.4615 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.4616 +
1.4617 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.4618 +/* Opcode: VDestroy P1 * * P4 *
1.4619 +**
1.4620 +** P4 is the name of a virtual table in database P1. Call the xDestroy method
1.4621 +** of that table.
1.4622 +*/
1.4623 +case OP_VDestroy: {
1.4624 + p->inVtabMethod = 2;
1.4625 + rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
1.4626 + p->inVtabMethod = 0;
1.4627 + break;
1.4628 +}
1.4629 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.4630 +
1.4631 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.4632 +/* Opcode: VOpen P1 * * P4 *
1.4633 +**
1.4634 +** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
1.4635 +** P1 is a cursor number. This opcode opens a cursor to the virtual
1.4636 +** table and stores that cursor in P1.
1.4637 +*/
1.4638 +case OP_VOpen: {
1.4639 + Cursor *pCur = 0;
1.4640 + sqlite3_vtab_cursor *pVtabCursor = 0;
1.4641 +
1.4642 + sqlite3_vtab *pVtab = pOp->p4.pVtab;
1.4643 + sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
1.4644 +
1.4645 + assert(pVtab && pModule);
1.4646 + if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1.4647 + rc = pModule->xOpen(pVtab, &pVtabCursor);
1.4648 + sqlite3DbFree(db, p->zErrMsg);
1.4649 + p->zErrMsg = pVtab->zErrMsg;
1.4650 + pVtab->zErrMsg = 0;
1.4651 + if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
1.4652 + if( SQLITE_OK==rc ){
1.4653 + /* Initialize sqlite3_vtab_cursor base class */
1.4654 + pVtabCursor->pVtab = pVtab;
1.4655 +
1.4656 + /* Initialise vdbe cursor object */
1.4657 + pCur = allocateCursor(p, pOp->p1, &pOp[-1], -1, 0);
1.4658 + if( pCur ){
1.4659 + pCur->pVtabCursor = pVtabCursor;
1.4660 + pCur->pModule = pVtabCursor->pVtab->pModule;
1.4661 + }else{
1.4662 + db->mallocFailed = 1;
1.4663 + pModule->xClose(pVtabCursor);
1.4664 + }
1.4665 + }
1.4666 + break;
1.4667 +}
1.4668 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.4669 +
1.4670 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.4671 +/* Opcode: VFilter P1 P2 P3 P4 *
1.4672 +**
1.4673 +** P1 is a cursor opened using VOpen. P2 is an address to jump to if
1.4674 +** the filtered result set is empty.
1.4675 +**
1.4676 +** P4 is either NULL or a string that was generated by the xBestIndex
1.4677 +** method of the module. The interpretation of the P4 string is left
1.4678 +** to the module implementation.
1.4679 +**
1.4680 +** This opcode invokes the xFilter method on the virtual table specified
1.4681 +** by P1. The integer query plan parameter to xFilter is stored in register
1.4682 +** P3. Register P3+1 stores the argc parameter to be passed to the
1.4683 +** xFilter method. Registers P3+2..P3+1+argc are the argc
1.4684 +** additional parameters which are passed to
1.4685 +** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
1.4686 +**
1.4687 +** A jump is made to P2 if the result set after filtering would be empty.
1.4688 +*/
1.4689 +case OP_VFilter: { /* jump */
1.4690 + int nArg;
1.4691 + int iQuery;
1.4692 + const sqlite3_module *pModule;
1.4693 + Mem *pQuery = &p->aMem[pOp->p3];
1.4694 + Mem *pArgc = &pQuery[1];
1.4695 + sqlite3_vtab_cursor *pVtabCursor;
1.4696 + sqlite3_vtab *pVtab;
1.4697 +
1.4698 + Cursor *pCur = p->apCsr[pOp->p1];
1.4699 +
1.4700 + REGISTER_TRACE(pOp->p3, pQuery);
1.4701 + assert( pCur->pVtabCursor );
1.4702 + pVtabCursor = pCur->pVtabCursor;
1.4703 + pVtab = pVtabCursor->pVtab;
1.4704 + pModule = pVtab->pModule;
1.4705 +
1.4706 + /* Grab the index number and argc parameters */
1.4707 + assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
1.4708 + nArg = pArgc->u.i;
1.4709 + iQuery = pQuery->u.i;
1.4710 +
1.4711 + /* Invoke the xFilter method */
1.4712 + {
1.4713 + int res = 0;
1.4714 + int i;
1.4715 + Mem **apArg = p->apArg;
1.4716 + for(i = 0; i<nArg; i++){
1.4717 + apArg[i] = &pArgc[i+1];
1.4718 + storeTypeInfo(apArg[i], 0);
1.4719 + }
1.4720 +
1.4721 + if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1.4722 + sqlite3VtabLock(pVtab);
1.4723 + p->inVtabMethod = 1;
1.4724 + rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
1.4725 + p->inVtabMethod = 0;
1.4726 + sqlite3DbFree(db, p->zErrMsg);
1.4727 + p->zErrMsg = pVtab->zErrMsg;
1.4728 + pVtab->zErrMsg = 0;
1.4729 + sqlite3VtabUnlock(db, pVtab);
1.4730 + if( rc==SQLITE_OK ){
1.4731 + res = pModule->xEof(pVtabCursor);
1.4732 + }
1.4733 + if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
1.4734 +
1.4735 + if( res ){
1.4736 + pc = pOp->p2 - 1;
1.4737 + }
1.4738 + }
1.4739 + pCur->nullRow = 0;
1.4740 +
1.4741 + break;
1.4742 +}
1.4743 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.4744 +
1.4745 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.4746 +/* Opcode: VRowid P1 P2 * * *
1.4747 +**
1.4748 +** Store into register P2 the rowid of
1.4749 +** the virtual-table that the P1 cursor is pointing to.
1.4750 +*/
1.4751 +case OP_VRowid: { /* out2-prerelease */
1.4752 + sqlite3_vtab *pVtab;
1.4753 + const sqlite3_module *pModule;
1.4754 + sqlite_int64 iRow;
1.4755 + Cursor *pCur = p->apCsr[pOp->p1];
1.4756 +
1.4757 + assert( pCur->pVtabCursor );
1.4758 + if( pCur->nullRow ){
1.4759 + break;
1.4760 + }
1.4761 + pVtab = pCur->pVtabCursor->pVtab;
1.4762 + pModule = pVtab->pModule;
1.4763 + assert( pModule->xRowid );
1.4764 + if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1.4765 + rc = pModule->xRowid(pCur->pVtabCursor, &iRow);
1.4766 + sqlite3DbFree(db, p->zErrMsg);
1.4767 + p->zErrMsg = pVtab->zErrMsg;
1.4768 + pVtab->zErrMsg = 0;
1.4769 + if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
1.4770 + MemSetTypeFlag(pOut, MEM_Int);
1.4771 + pOut->u.i = iRow;
1.4772 + break;
1.4773 +}
1.4774 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.4775 +
1.4776 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.4777 +/* Opcode: VColumn P1 P2 P3 * *
1.4778 +**
1.4779 +** Store the value of the P2-th column of
1.4780 +** the row of the virtual-table that the
1.4781 +** P1 cursor is pointing to into register P3.
1.4782 +*/
1.4783 +case OP_VColumn: {
1.4784 + sqlite3_vtab *pVtab;
1.4785 + const sqlite3_module *pModule;
1.4786 + Mem *pDest;
1.4787 + sqlite3_context sContext;
1.4788 +
1.4789 + Cursor *pCur = p->apCsr[pOp->p1];
1.4790 + assert( pCur->pVtabCursor );
1.4791 + assert( pOp->p3>0 && pOp->p3<=p->nMem );
1.4792 + pDest = &p->aMem[pOp->p3];
1.4793 + if( pCur->nullRow ){
1.4794 + sqlite3VdbeMemSetNull(pDest);
1.4795 + break;
1.4796 + }
1.4797 + pVtab = pCur->pVtabCursor->pVtab;
1.4798 + pModule = pVtab->pModule;
1.4799 + assert( pModule->xColumn );
1.4800 + memset(&sContext, 0, sizeof(sContext));
1.4801 +
1.4802 + /* The output cell may already have a buffer allocated. Move
1.4803 + ** the current contents to sContext.s so in case the user-function
1.4804 + ** can use the already allocated buffer instead of allocating a
1.4805 + ** new one.
1.4806 + */
1.4807 + sqlite3VdbeMemMove(&sContext.s, pDest);
1.4808 + MemSetTypeFlag(&sContext.s, MEM_Null);
1.4809 +
1.4810 + if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1.4811 + rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
1.4812 + sqlite3DbFree(db, p->zErrMsg);
1.4813 + p->zErrMsg = pVtab->zErrMsg;
1.4814 + pVtab->zErrMsg = 0;
1.4815 +
1.4816 + /* Copy the result of the function to the P3 register. We
1.4817 + ** do this regardless of whether or not an error occured to ensure any
1.4818 + ** dynamic allocation in sContext.s (a Mem struct) is released.
1.4819 + */
1.4820 + sqlite3VdbeChangeEncoding(&sContext.s, encoding);
1.4821 + REGISTER_TRACE(pOp->p3, pDest);
1.4822 + sqlite3VdbeMemMove(pDest, &sContext.s);
1.4823 + UPDATE_MAX_BLOBSIZE(pDest);
1.4824 +
1.4825 + if( sqlite3SafetyOn(db) ){
1.4826 + goto abort_due_to_misuse;
1.4827 + }
1.4828 + if( sqlite3VdbeMemTooBig(pDest) ){
1.4829 + goto too_big;
1.4830 + }
1.4831 + break;
1.4832 +}
1.4833 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.4834 +
1.4835 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.4836 +/* Opcode: VNext P1 P2 * * *
1.4837 +**
1.4838 +** Advance virtual table P1 to the next row in its result set and
1.4839 +** jump to instruction P2. Or, if the virtual table has reached
1.4840 +** the end of its result set, then fall through to the next instruction.
1.4841 +*/
1.4842 +case OP_VNext: { /* jump */
1.4843 + sqlite3_vtab *pVtab;
1.4844 + const sqlite3_module *pModule;
1.4845 + int res = 0;
1.4846 +
1.4847 + Cursor *pCur = p->apCsr[pOp->p1];
1.4848 + assert( pCur->pVtabCursor );
1.4849 + if( pCur->nullRow ){
1.4850 + break;
1.4851 + }
1.4852 + pVtab = pCur->pVtabCursor->pVtab;
1.4853 + pModule = pVtab->pModule;
1.4854 + assert( pModule->xNext );
1.4855 +
1.4856 + /* Invoke the xNext() method of the module. There is no way for the
1.4857 + ** underlying implementation to return an error if one occurs during
1.4858 + ** xNext(). Instead, if an error occurs, true is returned (indicating that
1.4859 + ** data is available) and the error code returned when xColumn or
1.4860 + ** some other method is next invoked on the save virtual table cursor.
1.4861 + */
1.4862 + if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1.4863 + sqlite3VtabLock(pVtab);
1.4864 + p->inVtabMethod = 1;
1.4865 + rc = pModule->xNext(pCur->pVtabCursor);
1.4866 + p->inVtabMethod = 0;
1.4867 + sqlite3DbFree(db, p->zErrMsg);
1.4868 + p->zErrMsg = pVtab->zErrMsg;
1.4869 + pVtab->zErrMsg = 0;
1.4870 + sqlite3VtabUnlock(db, pVtab);
1.4871 + if( rc==SQLITE_OK ){
1.4872 + res = pModule->xEof(pCur->pVtabCursor);
1.4873 + }
1.4874 + if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
1.4875 +
1.4876 + if( !res ){
1.4877 + /* If there is data, jump to P2 */
1.4878 + pc = pOp->p2 - 1;
1.4879 + }
1.4880 + break;
1.4881 +}
1.4882 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.4883 +
1.4884 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.4885 +/* Opcode: VRename P1 * * P4 *
1.4886 +**
1.4887 +** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
1.4888 +** This opcode invokes the corresponding xRename method. The value
1.4889 +** in register P1 is passed as the zName argument to the xRename method.
1.4890 +*/
1.4891 +case OP_VRename: {
1.4892 + sqlite3_vtab *pVtab = pOp->p4.pVtab;
1.4893 + Mem *pName = &p->aMem[pOp->p1];
1.4894 + assert( pVtab->pModule->xRename );
1.4895 + REGISTER_TRACE(pOp->p1, pName);
1.4896 +
1.4897 + Stringify(pName, encoding);
1.4898 +
1.4899 + if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1.4900 + sqlite3VtabLock(pVtab);
1.4901 + rc = pVtab->pModule->xRename(pVtab, pName->z);
1.4902 + sqlite3DbFree(db, p->zErrMsg);
1.4903 + p->zErrMsg = pVtab->zErrMsg;
1.4904 + pVtab->zErrMsg = 0;
1.4905 + sqlite3VtabUnlock(db, pVtab);
1.4906 + if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
1.4907 +
1.4908 + break;
1.4909 +}
1.4910 +#endif
1.4911 +
1.4912 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.4913 +/* Opcode: VUpdate P1 P2 P3 P4 *
1.4914 +**
1.4915 +** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
1.4916 +** This opcode invokes the corresponding xUpdate method. P2 values
1.4917 +** are contiguous memory cells starting at P3 to pass to the xUpdate
1.4918 +** invocation. The value in register (P3+P2-1) corresponds to the
1.4919 +** p2th element of the argv array passed to xUpdate.
1.4920 +**
1.4921 +** The xUpdate method will do a DELETE or an INSERT or both.
1.4922 +** The argv[0] element (which corresponds to memory cell P3)
1.4923 +** is the rowid of a row to delete. If argv[0] is NULL then no
1.4924 +** deletion occurs. The argv[1] element is the rowid of the new
1.4925 +** row. This can be NULL to have the virtual table select the new
1.4926 +** rowid for itself. The subsequent elements in the array are
1.4927 +** the values of columns in the new row.
1.4928 +**
1.4929 +** If P2==1 then no insert is performed. argv[0] is the rowid of
1.4930 +** a row to delete.
1.4931 +**
1.4932 +** P1 is a boolean flag. If it is set to true and the xUpdate call
1.4933 +** is successful, then the value returned by sqlite3_last_insert_rowid()
1.4934 +** is set to the value of the rowid for the row just inserted.
1.4935 +*/
1.4936 +case OP_VUpdate: {
1.4937 + sqlite3_vtab *pVtab = pOp->p4.pVtab;
1.4938 + sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
1.4939 + int nArg = pOp->p2;
1.4940 + assert( pOp->p4type==P4_VTAB );
1.4941 + if( pModule->xUpdate==0 ){
1.4942 + sqlite3SetString(&p->zErrMsg, db, "read-only table");
1.4943 + rc = SQLITE_ERROR;
1.4944 + }else{
1.4945 + int i;
1.4946 + sqlite_int64 rowid;
1.4947 + Mem **apArg = p->apArg;
1.4948 + Mem *pX = &p->aMem[pOp->p3];
1.4949 + for(i=0; i<nArg; i++){
1.4950 + storeTypeInfo(pX, 0);
1.4951 + apArg[i] = pX;
1.4952 + pX++;
1.4953 + }
1.4954 + if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1.4955 + sqlite3VtabLock(pVtab);
1.4956 + rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
1.4957 + sqlite3DbFree(db, p->zErrMsg);
1.4958 + p->zErrMsg = pVtab->zErrMsg;
1.4959 + pVtab->zErrMsg = 0;
1.4960 + sqlite3VtabUnlock(db, pVtab);
1.4961 + if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
1.4962 + if( pOp->p1 && rc==SQLITE_OK ){
1.4963 + assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
1.4964 + db->lastRowid = rowid;
1.4965 + }
1.4966 + p->nChange++;
1.4967 + }
1.4968 + break;
1.4969 +}
1.4970 +#endif /* SQLITE_OMIT_VIRTUALTABLE */
1.4971 +
1.4972 +#ifndef SQLITE_OMIT_PAGER_PRAGMAS
1.4973 +/* Opcode: Pagecount P1 P2 * * *
1.4974 +**
1.4975 +** Write the current number of pages in database P1 to memory cell P2.
1.4976 +*/
1.4977 +case OP_Pagecount: { /* out2-prerelease */
1.4978 + int p1 = pOp->p1;
1.4979 + int nPage;
1.4980 + Pager *pPager = sqlite3BtreePager(db->aDb[p1].pBt);
1.4981 +
1.4982 + rc = sqlite3PagerPagecount(pPager, &nPage);
1.4983 + if( rc==SQLITE_OK ){
1.4984 + pOut->flags = MEM_Int;
1.4985 + pOut->u.i = nPage;
1.4986 + }
1.4987 + break;
1.4988 +}
1.4989 +#endif
1.4990 +
1.4991 +#ifndef SQLITE_OMIT_TRACE
1.4992 +/* Opcode: Trace * * * P4 *
1.4993 +**
1.4994 +** If tracing is enabled (by the sqlite3_trace()) interface, then
1.4995 +** the UTF-8 string contained in P4 is emitted on the trace callback.
1.4996 +*/
1.4997 +case OP_Trace: {
1.4998 + if( pOp->p4.z ){
1.4999 + if( db->xTrace ){
1.5000 + db->xTrace(db->pTraceArg, pOp->p4.z);
1.5001 + }
1.5002 +#ifdef SQLITE_DEBUG
1.5003 + if( (db->flags & SQLITE_SqlTrace)!=0 ){
1.5004 + sqlite3DebugPrintf("SQL-trace: %s\n", pOp->p4.z);
1.5005 + }
1.5006 +#endif /* SQLITE_DEBUG */
1.5007 + }
1.5008 + break;
1.5009 +}
1.5010 +#endif
1.5011 +
1.5012 +
1.5013 +/* Opcode: Noop * * * * *
1.5014 +**
1.5015 +** Do nothing. This instruction is often useful as a jump
1.5016 +** destination.
1.5017 +*/
1.5018 +/*
1.5019 +** The magic Explain opcode are only inserted when explain==2 (which
1.5020 +** is to say when the EXPLAIN QUERY PLAN syntax is used.)
1.5021 +** This opcode records information from the optimizer. It is the
1.5022 +** the same as a no-op. This opcodesnever appears in a real VM program.
1.5023 +*/
1.5024 +default: { /* This is really OP_Noop and OP_Explain */
1.5025 + break;
1.5026 +}
1.5027 +
1.5028 +/*****************************************************************************
1.5029 +** The cases of the switch statement above this line should all be indented
1.5030 +** by 6 spaces. But the left-most 6 spaces have been removed to improve the
1.5031 +** readability. From this point on down, the normal indentation rules are
1.5032 +** restored.
1.5033 +*****************************************************************************/
1.5034 + }
1.5035 +
1.5036 +#ifdef VDBE_PROFILE
1.5037 + {
1.5038 + u64 elapsed = sqlite3Hwtime() - start;
1.5039 + pOp->cycles += elapsed;
1.5040 + pOp->cnt++;
1.5041 +#if 0
1.5042 + fprintf(stdout, "%10llu ", elapsed);
1.5043 + sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]);
1.5044 +#endif
1.5045 + }
1.5046 +#endif
1.5047 +
1.5048 + /* The following code adds nothing to the actual functionality
1.5049 + ** of the program. It is only here for testing and debugging.
1.5050 + ** On the other hand, it does burn CPU cycles every time through
1.5051 + ** the evaluator loop. So we can leave it out when NDEBUG is defined.
1.5052 + */
1.5053 +#ifndef NDEBUG
1.5054 + assert( pc>=-1 && pc<p->nOp );
1.5055 +
1.5056 +#ifdef SQLITE_DEBUG
1.5057 + if( p->trace ){
1.5058 + if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
1.5059 + if( opProperty & OPFLG_OUT2_PRERELEASE ){
1.5060 + registerTrace(p->trace, pOp->p2, pOut);
1.5061 + }
1.5062 + if( opProperty & OPFLG_OUT3 ){
1.5063 + registerTrace(p->trace, pOp->p3, pOut);
1.5064 + }
1.5065 + }
1.5066 +#endif /* SQLITE_DEBUG */
1.5067 +#endif /* NDEBUG */
1.5068 + } /* The end of the for(;;) loop the loops through opcodes */
1.5069 +
1.5070 + /* If we reach this point, it means that execution is finished with
1.5071 + ** an error of some kind.
1.5072 + */
1.5073 +vdbe_error_halt:
1.5074 + assert( rc );
1.5075 + p->rc = rc;
1.5076 + sqlite3VdbeHalt(p);
1.5077 + if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
1.5078 + rc = SQLITE_ERROR;
1.5079 +
1.5080 + /* This is the only way out of this procedure. We have to
1.5081 + ** release the mutexes on btrees that were acquired at the
1.5082 + ** top. */
1.5083 +vdbe_return:
1.5084 + sqlite3BtreeMutexArrayLeave(&p->aMutex);
1.5085 + return rc;
1.5086 +
1.5087 + /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
1.5088 + ** is encountered.
1.5089 + */
1.5090 +too_big:
1.5091 + sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
1.5092 + rc = SQLITE_TOOBIG;
1.5093 + goto vdbe_error_halt;
1.5094 +
1.5095 + /* Jump to here if a malloc() fails.
1.5096 + */
1.5097 +no_mem:
1.5098 + db->mallocFailed = 1;
1.5099 + sqlite3SetString(&p->zErrMsg, db, "out of memory");
1.5100 + rc = SQLITE_NOMEM;
1.5101 + goto vdbe_error_halt;
1.5102 +
1.5103 + /* Jump to here for an SQLITE_MISUSE error.
1.5104 + */
1.5105 +abort_due_to_misuse:
1.5106 + rc = SQLITE_MISUSE;
1.5107 + /* Fall thru into abort_due_to_error */
1.5108 +
1.5109 + /* Jump to here for any other kind of fatal error. The "rc" variable
1.5110 + ** should hold the error number.
1.5111 + */
1.5112 +abort_due_to_error:
1.5113 + assert( p->zErrMsg==0 );
1.5114 + if( db->mallocFailed ) rc = SQLITE_NOMEM;
1.5115 + if( rc!=SQLITE_IOERR_NOMEM ){
1.5116 + sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
1.5117 + }
1.5118 + goto vdbe_error_halt;
1.5119 +
1.5120 + /* Jump to here if the sqlite3_interrupt() API sets the interrupt
1.5121 + ** flag.
1.5122 + */
1.5123 +abort_due_to_interrupt:
1.5124 + assert( db->u1.isInterrupted );
1.5125 + rc = SQLITE_INTERRUPT;
1.5126 + p->rc = rc;
1.5127 + sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
1.5128 + goto vdbe_error_halt;
1.5129 +}