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