sl@0: /* sl@0: ** 2001 September 15 sl@0: ** sl@0: ** The author disclaims copyright to this source code. In place of sl@0: ** a legal notice, here is a blessing: sl@0: ** sl@0: ** May you do good and not evil. sl@0: ** May you find forgiveness for yourself and forgive others. sl@0: ** May you share freely, never taking more than you give. sl@0: ** sl@0: ************************************************************************* sl@0: ** The code in this file implements execution method of the sl@0: ** Virtual Database Engine (VDBE). A separate file ("vdbeaux.c") sl@0: ** handles housekeeping details such as creating and deleting sl@0: ** VDBE instances. This file is solely interested in executing sl@0: ** the VDBE program. sl@0: ** sl@0: ** In the external interface, an "sqlite3_stmt*" is an opaque pointer sl@0: ** to a VDBE. sl@0: ** sl@0: ** The SQL parser generates a program which is then executed by sl@0: ** the VDBE to do the work of the SQL statement. VDBE programs are sl@0: ** similar in form to assembly language. The program consists of sl@0: ** a linear sequence of operations. Each operation has an opcode sl@0: ** and 5 operands. Operands P1, P2, and P3 are integers. Operand P4 sl@0: ** is a null-terminated string. Operand P5 is an unsigned character. sl@0: ** Few opcodes use all 5 operands. sl@0: ** sl@0: ** Computation results are stored on a set of registers numbered beginning sl@0: ** with 1 and going up to Vdbe.nMem. Each register can store sl@0: ** either an integer, a null-terminated string, a floating point sl@0: ** number, or the SQL "NULL" value. An implicit conversion from one sl@0: ** type to the other occurs as necessary. sl@0: ** sl@0: ** Most of the code in this file is taken up by the sqlite3VdbeExec() sl@0: ** function which does the work of interpreting a VDBE program. sl@0: ** But other routines are also provided to help in building up sl@0: ** a program instruction by instruction. sl@0: ** sl@0: ** Various scripts scan this source file in order to generate HTML sl@0: ** documentation, headers files, or other derived files. The formatting sl@0: ** of the code in this file is, therefore, important. See other comments sl@0: ** in this file for details. If in doubt, do not deviate from existing sl@0: ** commenting and indentation practices when changing or adding code. sl@0: ** sl@0: ** $Id: vdbe.c,v 1.772 2008/08/02 15:10:09 danielk1977 Exp $ sl@0: */ sl@0: #include "sqliteInt.h" sl@0: #include sl@0: #include "vdbeInt.h" sl@0: sl@0: /* sl@0: ** The following global variable is incremented every time a cursor sl@0: ** moves, either by the OP_MoveXX, OP_Next, or OP_Prev opcodes. The test sl@0: ** procedures use this information to make sure that indices are sl@0: ** working correctly. This variable has no function other than to sl@0: ** help verify the correct operation of the library. sl@0: */ sl@0: #ifdef SQLITE_TEST sl@0: int sqlite3_search_count = 0; sl@0: #endif sl@0: sl@0: /* sl@0: ** When this global variable is positive, it gets decremented once before sl@0: ** each instruction in the VDBE. When reaches zero, the u1.isInterrupted sl@0: ** field of the sqlite3 structure is set in order to simulate and interrupt. sl@0: ** sl@0: ** This facility is used for testing purposes only. It does not function sl@0: ** in an ordinary build. sl@0: */ sl@0: #ifdef SQLITE_TEST sl@0: int sqlite3_interrupt_count = 0; sl@0: #endif sl@0: sl@0: /* sl@0: ** The next global variable is incremented each type the OP_Sort opcode sl@0: ** is executed. The test procedures use this information to make sure that sl@0: ** sorting is occurring or not occurring at appropriate times. This variable sl@0: ** has no function other than to help verify the correct operation of the sl@0: ** library. sl@0: */ sl@0: #ifdef SQLITE_TEST sl@0: int sqlite3_sort_count = 0; sl@0: #endif sl@0: sl@0: /* sl@0: ** The next global variable records the size of the largest MEM_Blob sl@0: ** or MEM_Str that has been used by a VDBE opcode. The test procedures sl@0: ** use this information to make sure that the zero-blob functionality sl@0: ** is working correctly. This variable has no function other than to sl@0: ** help verify the correct operation of the library. sl@0: */ sl@0: #ifdef SQLITE_TEST sl@0: int sqlite3_max_blobsize = 0; sl@0: static void updateMaxBlobsize(Mem *p){ sl@0: if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ sl@0: sqlite3_max_blobsize = p->n; sl@0: } sl@0: } sl@0: #endif sl@0: sl@0: /* sl@0: ** Test a register to see if it exceeds the current maximum blob size. sl@0: ** If it does, record the new maximum blob size. sl@0: */ sl@0: #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST) sl@0: # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) sl@0: #else sl@0: # define UPDATE_MAX_BLOBSIZE(P) sl@0: #endif sl@0: sl@0: /* sl@0: ** Release the memory associated with a register. This sl@0: ** leaves the Mem.flags field in an inconsistent state. sl@0: */ sl@0: #define Release(P) if((P)->flags&MEM_Dyn){ sqlite3VdbeMemRelease(P); } sl@0: sl@0: /* sl@0: ** Convert the given register into a string if it isn't one sl@0: ** already. Return non-zero if a malloc() fails. sl@0: */ sl@0: #define Stringify(P, enc) \ sl@0: if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \ sl@0: { goto no_mem; } sl@0: sl@0: /* sl@0: ** An ephemeral string value (signified by the MEM_Ephem flag) contains sl@0: ** a pointer to a dynamically allocated string where some other entity sl@0: ** is responsible for deallocating that string. Because the register sl@0: ** does not control the string, it might be deleted without the register sl@0: ** knowing it. sl@0: ** sl@0: ** This routine converts an ephemeral string into a dynamically allocated sl@0: ** string that the register itself controls. In other words, it sl@0: ** converts an MEM_Ephem string into an MEM_Dyn string. sl@0: */ sl@0: #define Deephemeralize(P) \ sl@0: if( ((P)->flags&MEM_Ephem)!=0 \ sl@0: && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} sl@0: sl@0: /* sl@0: ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*) sl@0: ** P if required. sl@0: */ sl@0: #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0) sl@0: sl@0: /* sl@0: ** Argument pMem points at a register that will be passed to a sl@0: ** user-defined function or returned to the user as the result of a query. sl@0: ** The second argument, 'db_enc' is the text encoding used by the vdbe for sl@0: ** register variables. This routine sets the pMem->enc and pMem->type sl@0: ** variables used by the sqlite3_value_*() routines. sl@0: */ sl@0: #define storeTypeInfo(A,B) _storeTypeInfo(A) sl@0: static void _storeTypeInfo(Mem *pMem){ sl@0: int flags = pMem->flags; sl@0: if( flags & MEM_Null ){ sl@0: pMem->type = SQLITE_NULL; sl@0: } sl@0: else if( flags & MEM_Int ){ sl@0: pMem->type = SQLITE_INTEGER; sl@0: } sl@0: else if( flags & MEM_Real ){ sl@0: pMem->type = SQLITE_FLOAT; sl@0: } sl@0: else if( flags & MEM_Str ){ sl@0: pMem->type = SQLITE_TEXT; sl@0: }else{ sl@0: pMem->type = SQLITE_BLOB; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: ** Properties of opcodes. The OPFLG_INITIALIZER macro is sl@0: ** created by mkopcodeh.awk during compilation. Data is obtained sl@0: ** from the comments following the "case OP_xxxx:" statements in sl@0: ** this file. sl@0: */ sl@0: static unsigned char opcodeProperty[] = OPFLG_INITIALIZER; sl@0: sl@0: /* sl@0: ** Return true if an opcode has any of the OPFLG_xxx properties sl@0: ** specified by mask. sl@0: */ sl@0: int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){ sl@0: assert( opcode>0 && opcodenMem) corresponds to cursor 0. Space for sl@0: ** cursor 1 is managed by memory cell (p->nMem-1), etc. sl@0: */ sl@0: Mem *pMem = &p->aMem[p->nMem-iCur]; sl@0: sl@0: int nByte; sl@0: Cursor *pCx = 0; sl@0: /* If the opcode of pOp is OP_SetNumColumns, then pOp->p2 contains sl@0: ** the number of fields in the records contained in the table or sl@0: ** index being opened. Use this to reserve space for the sl@0: ** Cursor.aType[] array. sl@0: */ sl@0: int nField = 0; sl@0: if( pOp->opcode==OP_SetNumColumns || pOp->opcode==OP_OpenEphemeral ){ sl@0: nField = pOp->p2; sl@0: } sl@0: nByte = sl@0: sizeof(Cursor) + sl@0: (isBtreeCursor?sqlite3BtreeCursorSize():0) + sl@0: 2*nField*sizeof(u32); sl@0: sl@0: assert( iCurnCursor ); sl@0: if( p->apCsr[iCur] ){ sl@0: sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); sl@0: p->apCsr[iCur] = 0; sl@0: } sl@0: if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){ sl@0: p->apCsr[iCur] = pCx = (Cursor *)pMem->z; sl@0: memset(pMem->z, 0, nByte); sl@0: pCx->iDb = iDb; sl@0: pCx->nField = nField; sl@0: if( nField ){ sl@0: pCx->aType = (u32 *)&pMem->z[sizeof(Cursor)]; sl@0: } sl@0: if( isBtreeCursor ){ sl@0: pCx->pCursor = (BtCursor *)&pMem->z[sizeof(Cursor)+2*nField*sizeof(u32)]; sl@0: } sl@0: } sl@0: return pCx; sl@0: } sl@0: sl@0: /* sl@0: ** Try to convert a value into a numeric representation if we can sl@0: ** do so without loss of information. In other words, if the string sl@0: ** looks like a number, convert it into a number. If it does not sl@0: ** look like a number, leave it alone. sl@0: */ sl@0: static void applyNumericAffinity(Mem *pRec){ sl@0: if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){ sl@0: int realnum; sl@0: sqlite3VdbeMemNulTerminate(pRec); sl@0: if( (pRec->flags&MEM_Str) sl@0: && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){ sl@0: i64 value; sl@0: sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8); sl@0: if( !realnum && sqlite3Atoi64(pRec->z, &value) ){ sl@0: pRec->u.i = value; sl@0: MemSetTypeFlag(pRec, MEM_Int); sl@0: }else{ sl@0: sqlite3VdbeMemRealify(pRec); sl@0: } sl@0: } sl@0: } sl@0: } sl@0: sl@0: /* sl@0: ** Processing is determine by the affinity parameter: sl@0: ** sl@0: ** SQLITE_AFF_INTEGER: sl@0: ** SQLITE_AFF_REAL: sl@0: ** SQLITE_AFF_NUMERIC: sl@0: ** Try to convert pRec to an integer representation or a sl@0: ** floating-point representation if an integer representation sl@0: ** is not possible. Note that the integer representation is sl@0: ** always preferred, even if the affinity is REAL, because sl@0: ** an integer representation is more space efficient on disk. sl@0: ** sl@0: ** SQLITE_AFF_TEXT: sl@0: ** Convert pRec to a text representation. sl@0: ** sl@0: ** SQLITE_AFF_NONE: sl@0: ** No-op. pRec is unchanged. sl@0: */ sl@0: static void applyAffinity( sl@0: Mem *pRec, /* The value to apply affinity to */ sl@0: char affinity, /* The affinity to be applied */ sl@0: u8 enc /* Use this text encoding */ sl@0: ){ sl@0: if( affinity==SQLITE_AFF_TEXT ){ sl@0: /* Only attempt the conversion to TEXT if there is an integer or real sl@0: ** representation (blob and NULL do not get converted) but no string sl@0: ** representation. sl@0: */ sl@0: if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ sl@0: sqlite3VdbeMemStringify(pRec, enc); sl@0: } sl@0: pRec->flags &= ~(MEM_Real|MEM_Int); sl@0: }else if( affinity!=SQLITE_AFF_NONE ){ sl@0: assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL sl@0: || affinity==SQLITE_AFF_NUMERIC ); sl@0: applyNumericAffinity(pRec); sl@0: if( pRec->flags & MEM_Real ){ sl@0: sqlite3VdbeIntegerAffinity(pRec); sl@0: } sl@0: } sl@0: } sl@0: sl@0: /* sl@0: ** Try to convert the type of a function argument or a result column sl@0: ** into a numeric representation. Use either INTEGER or REAL whichever sl@0: ** is appropriate. But only do the conversion if it is possible without sl@0: ** loss of information and return the revised type of the argument. sl@0: ** sl@0: ** This is an EXPERIMENTAL api and is subject to change or removal. sl@0: */ sl@0: int sqlite3_value_numeric_type(sqlite3_value *pVal){ sl@0: Mem *pMem = (Mem*)pVal; sl@0: applyNumericAffinity(pMem); sl@0: storeTypeInfo(pMem, 0); sl@0: return pMem->type; sl@0: } sl@0: sl@0: /* sl@0: ** Exported version of applyAffinity(). This one works on sqlite3_value*, sl@0: ** not the internal Mem* type. sl@0: */ sl@0: void sqlite3ValueApplyAffinity( sl@0: sqlite3_value *pVal, sl@0: u8 affinity, sl@0: u8 enc sl@0: ){ sl@0: applyAffinity((Mem *)pVal, affinity, enc); sl@0: } sl@0: sl@0: #ifdef SQLITE_DEBUG sl@0: /* sl@0: ** Write a nice string representation of the contents of cell pMem sl@0: ** into buffer zBuf, length nBuf. sl@0: */ sl@0: void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ sl@0: char *zCsr = zBuf; sl@0: int f = pMem->flags; sl@0: sl@0: static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; sl@0: sl@0: if( f&MEM_Blob ){ sl@0: int i; sl@0: char c; sl@0: if( f & MEM_Dyn ){ sl@0: c = 'z'; sl@0: assert( (f & (MEM_Static|MEM_Ephem))==0 ); sl@0: }else if( f & MEM_Static ){ sl@0: c = 't'; sl@0: assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); sl@0: }else if( f & MEM_Ephem ){ sl@0: c = 'e'; sl@0: assert( (f & (MEM_Static|MEM_Dyn))==0 ); sl@0: }else{ sl@0: c = 's'; sl@0: } sl@0: sl@0: sqlite3_snprintf(100, zCsr, "%c", c); sl@0: zCsr += strlen(zCsr); sl@0: sqlite3_snprintf(100, zCsr, "%d[", pMem->n); sl@0: zCsr += strlen(zCsr); sl@0: for(i=0; i<16 && in; i++){ sl@0: sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); sl@0: zCsr += strlen(zCsr); sl@0: } sl@0: for(i=0; i<16 && in; i++){ sl@0: char z = pMem->z[i]; sl@0: if( z<32 || z>126 ) *zCsr++ = '.'; sl@0: else *zCsr++ = z; sl@0: } sl@0: sl@0: sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]); sl@0: zCsr += strlen(zCsr); sl@0: if( f & MEM_Zero ){ sl@0: sqlite3_snprintf(100, zCsr,"+%lldz",pMem->u.i); sl@0: zCsr += strlen(zCsr); sl@0: } sl@0: *zCsr = '\0'; sl@0: }else if( f & MEM_Str ){ sl@0: int j, k; sl@0: zBuf[0] = ' '; sl@0: if( f & MEM_Dyn ){ sl@0: zBuf[1] = 'z'; sl@0: assert( (f & (MEM_Static|MEM_Ephem))==0 ); sl@0: }else if( f & MEM_Static ){ sl@0: zBuf[1] = 't'; sl@0: assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); sl@0: }else if( f & MEM_Ephem ){ sl@0: zBuf[1] = 'e'; sl@0: assert( (f & (MEM_Static|MEM_Dyn))==0 ); sl@0: }else{ sl@0: zBuf[1] = 's'; sl@0: } sl@0: k = 2; sl@0: sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); sl@0: k += strlen(&zBuf[k]); sl@0: zBuf[k++] = '['; sl@0: for(j=0; j<15 && jn; j++){ sl@0: u8 c = pMem->z[j]; sl@0: if( c>=0x20 && c<0x7f ){ sl@0: zBuf[k++] = c; sl@0: }else{ sl@0: zBuf[k++] = '.'; sl@0: } sl@0: } sl@0: zBuf[k++] = ']'; sl@0: sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); sl@0: k += strlen(&zBuf[k]); sl@0: zBuf[k++] = 0; sl@0: } sl@0: } sl@0: #endif sl@0: sl@0: #ifdef SQLITE_DEBUG sl@0: /* sl@0: ** Print the value of a register for tracing purposes: sl@0: */ sl@0: static void memTracePrint(FILE *out, Mem *p){ sl@0: if( p->flags & MEM_Null ){ sl@0: fprintf(out, " NULL"); sl@0: }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ sl@0: fprintf(out, " si:%lld", p->u.i); sl@0: }else if( p->flags & MEM_Int ){ sl@0: fprintf(out, " i:%lld", p->u.i); sl@0: }else if( p->flags & MEM_Real ){ sl@0: fprintf(out, " r:%g", p->r); sl@0: }else{ sl@0: char zBuf[200]; sl@0: sqlite3VdbeMemPrettyPrint(p, zBuf); sl@0: fprintf(out, " "); sl@0: fprintf(out, "%s", zBuf); sl@0: } sl@0: } sl@0: static void registerTrace(FILE *out, int iReg, Mem *p){ sl@0: fprintf(out, "REG[%d] = ", iReg); sl@0: memTracePrint(out, p); sl@0: fprintf(out, "\n"); sl@0: } sl@0: #endif sl@0: sl@0: #ifdef SQLITE_DEBUG sl@0: # define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M) sl@0: #else sl@0: # define REGISTER_TRACE(R,M) sl@0: #endif sl@0: sl@0: sl@0: #ifdef VDBE_PROFILE sl@0: sl@0: /* sl@0: ** hwtime.h contains inline assembler code for implementing sl@0: ** high-performance timing routines. sl@0: */ sl@0: #include "hwtime.h" sl@0: sl@0: #endif sl@0: sl@0: /* sl@0: ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the sl@0: ** sqlite3_interrupt() routine has been called. If it has been, then sl@0: ** processing of the VDBE program is interrupted. sl@0: ** sl@0: ** This macro added to every instruction that does a jump in order to sl@0: ** implement a loop. This test used to be on every single instruction, sl@0: ** but that meant we more testing that we needed. By only testing the sl@0: ** flag on jump instructions, we get a (small) speed improvement. sl@0: */ sl@0: #define CHECK_FOR_INTERRUPT \ sl@0: if( db->u1.isInterrupted ) goto abort_due_to_interrupt; sl@0: sl@0: #ifdef SQLITE_DEBUG sl@0: static int fileExists(sqlite3 *db, const char *zFile){ sl@0: int res = 0; sl@0: int rc = SQLITE_OK; sl@0: #ifdef SQLITE_TEST sl@0: /* If we are currently testing IO errors, then do not call OsAccess() to sl@0: ** test for the presence of zFile. This is because any IO error that sl@0: ** occurs here will not be reported, causing the test to fail. sl@0: */ sl@0: extern int sqlite3_io_error_pending; sl@0: if( sqlite3_io_error_pending<=0 ) sl@0: #endif sl@0: rc = sqlite3OsAccess(db->pVfs, zFile, SQLITE_ACCESS_EXISTS, &res); sl@0: return (res && rc==SQLITE_OK); sl@0: } sl@0: #endif sl@0: sl@0: /* sl@0: ** Execute as much of a VDBE program as we can then return. sl@0: ** sl@0: ** sqlite3VdbeMakeReady() must be called before this routine in order to sl@0: ** close the program with a final OP_Halt and to set up the callbacks sl@0: ** and the error message pointer. sl@0: ** sl@0: ** Whenever a row or result data is available, this routine will either sl@0: ** invoke the result callback (if there is one) or return with sl@0: ** SQLITE_ROW. sl@0: ** sl@0: ** If an attempt is made to open a locked database, then this routine sl@0: ** will either invoke the busy callback (if there is one) or it will sl@0: ** return SQLITE_BUSY. sl@0: ** sl@0: ** If an error occurs, an error message is written to memory obtained sl@0: ** from sqlite3_malloc() and p->zErrMsg is made to point to that memory. sl@0: ** The error code is stored in p->rc and this routine returns SQLITE_ERROR. sl@0: ** sl@0: ** If the callback ever returns non-zero, then the program exits sl@0: ** immediately. There will be no error message but the p->rc field is sl@0: ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR. sl@0: ** sl@0: ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this sl@0: ** routine to return SQLITE_ERROR. sl@0: ** sl@0: ** Other fatal errors return SQLITE_ERROR. sl@0: ** sl@0: ** After this routine has finished, sqlite3VdbeFinalize() should be sl@0: ** used to clean up the mess that was left behind. sl@0: */ sl@0: int sqlite3VdbeExec( sl@0: Vdbe *p /* The VDBE */ sl@0: ){ sl@0: int pc; /* The program counter */ sl@0: Op *pOp; /* Current operation */ sl@0: int rc = SQLITE_OK; /* Value to return */ sl@0: sqlite3 *db = p->db; /* The database */ sl@0: u8 encoding = ENC(db); /* The database encoding */ sl@0: Mem *pIn1 = 0; /* Input operands */ sl@0: Mem *pIn2 = 0; /* Input operands */ sl@0: Mem *pIn3 = 0; /* Input operands */ sl@0: Mem *pOut = 0; /* Output operand */ sl@0: u8 opProperty; sl@0: int iCompare = 0; /* Result of last OP_Compare operation */ sl@0: int *aPermute = 0; /* Permuation of columns for OP_Compare */ sl@0: #ifdef VDBE_PROFILE sl@0: u64 start; /* CPU clock count at start of opcode */ sl@0: int origPc; /* Program counter at start of opcode */ sl@0: #endif sl@0: #ifndef SQLITE_OMIT_PROGRESS_CALLBACK sl@0: int nProgressOps = 0; /* Opcodes executed since progress callback. */ sl@0: #endif sl@0: sl@0: assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ sl@0: assert( db->magic==SQLITE_MAGIC_BUSY ); sl@0: sqlite3BtreeMutexArrayEnter(&p->aMutex); sl@0: if( p->rc==SQLITE_NOMEM ){ sl@0: /* This happens if a malloc() inside a call to sqlite3_column_text() or sl@0: ** sqlite3_column_text16() failed. */ sl@0: goto no_mem; sl@0: } sl@0: assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); sl@0: p->rc = SQLITE_OK; sl@0: assert( p->explain==0 ); sl@0: p->pResultSet = 0; sl@0: db->busyHandler.nBusy = 0; sl@0: CHECK_FOR_INTERRUPT; sl@0: sqlite3VdbeIOTraceSql(p); sl@0: #ifdef SQLITE_DEBUG sl@0: sqlite3BeginBenignMalloc(); sl@0: if( p->pc==0 sl@0: && ((p->db->flags & SQLITE_VdbeListing) || fileExists(db, "vdbe_explain")) sl@0: ){ sl@0: int i; sl@0: printf("VDBE Program Listing:\n"); sl@0: sqlite3VdbePrintSql(p); sl@0: for(i=0; inOp; i++){ sl@0: sqlite3VdbePrintOp(stdout, i, &p->aOp[i]); sl@0: } sl@0: } sl@0: if( fileExists(db, "vdbe_trace") ){ sl@0: p->trace = stdout; sl@0: } sl@0: sqlite3EndBenignMalloc(); sl@0: #endif sl@0: for(pc=p->pc; rc==SQLITE_OK; pc++){ sl@0: assert( pc>=0 && pcnOp ); sl@0: if( db->mallocFailed ) goto no_mem; sl@0: #ifdef VDBE_PROFILE sl@0: origPc = pc; sl@0: start = sqlite3Hwtime(); sl@0: #endif sl@0: pOp = &p->aOp[pc]; sl@0: sl@0: /* Only allow tracing if SQLITE_DEBUG is defined. sl@0: */ sl@0: #ifdef SQLITE_DEBUG sl@0: if( p->trace ){ sl@0: if( pc==0 ){ sl@0: printf("VDBE Execution Trace:\n"); sl@0: sqlite3VdbePrintSql(p); sl@0: } sl@0: sqlite3VdbePrintOp(p->trace, pc, pOp); sl@0: } sl@0: if( p->trace==0 && pc==0 ){ sl@0: sqlite3BeginBenignMalloc(); sl@0: if( fileExists(db, "vdbe_sqltrace") ){ sl@0: sqlite3VdbePrintSql(p); sl@0: } sl@0: sqlite3EndBenignMalloc(); sl@0: } sl@0: #endif sl@0: sl@0: sl@0: /* Check to see if we need to simulate an interrupt. This only happens sl@0: ** if we have a special test build. sl@0: */ sl@0: #ifdef SQLITE_TEST sl@0: if( sqlite3_interrupt_count>0 ){ sl@0: sqlite3_interrupt_count--; sl@0: if( sqlite3_interrupt_count==0 ){ sl@0: sqlite3_interrupt(db); sl@0: } sl@0: } sl@0: #endif sl@0: sl@0: #ifndef SQLITE_OMIT_PROGRESS_CALLBACK sl@0: /* Call the progress callback if it is configured and the required number sl@0: ** of VDBE ops have been executed (either since this invocation of sl@0: ** sqlite3VdbeExec() or since last time the progress callback was called). sl@0: ** If the progress callback returns non-zero, exit the virtual machine with sl@0: ** a return code SQLITE_ABORT. sl@0: */ sl@0: if( db->xProgress ){ sl@0: if( db->nProgressOps==nProgressOps ){ sl@0: int prc; sl@0: if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; sl@0: prc =db->xProgress(db->pProgressArg); sl@0: if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; sl@0: if( prc!=0 ){ sl@0: rc = SQLITE_INTERRUPT; sl@0: goto vdbe_error_halt; sl@0: } sl@0: nProgressOps = 0; sl@0: } sl@0: nProgressOps++; sl@0: } sl@0: #endif sl@0: sl@0: /* Do common setup processing for any opcode that is marked sl@0: ** with the "out2-prerelease" tag. Such opcodes have a single sl@0: ** output which is specified by the P2 parameter. The P2 register sl@0: ** is initialized to a NULL. sl@0: */ sl@0: opProperty = opcodeProperty[pOp->opcode]; sl@0: if( (opProperty & OPFLG_OUT2_PRERELEASE)!=0 ){ sl@0: assert( pOp->p2>0 ); sl@0: assert( pOp->p2<=p->nMem ); sl@0: pOut = &p->aMem[pOp->p2]; sl@0: sqlite3VdbeMemReleaseExternal(pOut); sl@0: pOut->flags = MEM_Null; sl@0: }else sl@0: sl@0: /* Do common setup for opcodes marked with one of the following sl@0: ** combinations of properties. sl@0: ** sl@0: ** in1 sl@0: ** in1 in2 sl@0: ** in1 in2 out3 sl@0: ** in1 in3 sl@0: ** sl@0: ** Variables pIn1, pIn2, and pIn3 are made to point to appropriate sl@0: ** registers for inputs. Variable pOut points to the output register. sl@0: */ sl@0: if( (opProperty & OPFLG_IN1)!=0 ){ sl@0: assert( pOp->p1>0 ); sl@0: assert( pOp->p1<=p->nMem ); sl@0: pIn1 = &p->aMem[pOp->p1]; sl@0: REGISTER_TRACE(pOp->p1, pIn1); sl@0: if( (opProperty & OPFLG_IN2)!=0 ){ sl@0: assert( pOp->p2>0 ); sl@0: assert( pOp->p2<=p->nMem ); sl@0: pIn2 = &p->aMem[pOp->p2]; sl@0: REGISTER_TRACE(pOp->p2, pIn2); sl@0: if( (opProperty & OPFLG_OUT3)!=0 ){ sl@0: assert( pOp->p3>0 ); sl@0: assert( pOp->p3<=p->nMem ); sl@0: pOut = &p->aMem[pOp->p3]; sl@0: } sl@0: }else if( (opProperty & OPFLG_IN3)!=0 ){ sl@0: assert( pOp->p3>0 ); sl@0: assert( pOp->p3<=p->nMem ); sl@0: pIn3 = &p->aMem[pOp->p3]; sl@0: REGISTER_TRACE(pOp->p3, pIn3); sl@0: } sl@0: }else if( (opProperty & OPFLG_IN2)!=0 ){ sl@0: assert( pOp->p2>0 ); sl@0: assert( pOp->p2<=p->nMem ); sl@0: pIn2 = &p->aMem[pOp->p2]; sl@0: REGISTER_TRACE(pOp->p2, pIn2); sl@0: }else if( (opProperty & OPFLG_IN3)!=0 ){ sl@0: assert( pOp->p3>0 ); sl@0: assert( pOp->p3<=p->nMem ); sl@0: pIn3 = &p->aMem[pOp->p3]; sl@0: REGISTER_TRACE(pOp->p3, pIn3); sl@0: } sl@0: sl@0: switch( pOp->opcode ){ sl@0: sl@0: /***************************************************************************** sl@0: ** What follows is a massive switch statement where each case implements a sl@0: ** separate instruction in the virtual machine. If we follow the usual sl@0: ** indentation conventions, each case should be indented by 6 spaces. But sl@0: ** that is a lot of wasted space on the left margin. So the code within sl@0: ** the switch statement will break with convention and be flush-left. Another sl@0: ** big comment (similar to this one) will mark the point in the code where sl@0: ** we transition back to normal indentation. sl@0: ** sl@0: ** The formatting of each case is important. The makefile for SQLite sl@0: ** generates two C files "opcodes.h" and "opcodes.c" by scanning this sl@0: ** file looking for lines that begin with "case OP_". The opcodes.h files sl@0: ** will be filled with #defines that give unique integer values to each sl@0: ** opcode and the opcodes.c file is filled with an array of strings where sl@0: ** each string is the symbolic name for the corresponding opcode. If the sl@0: ** case statement is followed by a comment of the form "/# same as ... #/" sl@0: ** that comment is used to determine the particular value of the opcode. sl@0: ** sl@0: ** Other keywords in the comment that follows each case are used to sl@0: ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. sl@0: ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See sl@0: ** the mkopcodeh.awk script for additional information. sl@0: ** sl@0: ** Documentation about VDBE opcodes is generated by scanning this file sl@0: ** for lines of that contain "Opcode:". That line and all subsequent sl@0: ** comment lines are used in the generation of the opcode.html documentation sl@0: ** file. sl@0: ** sl@0: ** SUMMARY: sl@0: ** sl@0: ** Formatting is important to scripts that scan this file. sl@0: ** Do not deviate from the formatting style currently in use. sl@0: ** sl@0: *****************************************************************************/ sl@0: sl@0: /* Opcode: Goto * P2 * * * sl@0: ** sl@0: ** An unconditional jump to address P2. sl@0: ** The next instruction executed will be sl@0: ** the one at index P2 from the beginning of sl@0: ** the program. sl@0: */ sl@0: case OP_Goto: { /* jump */ sl@0: CHECK_FOR_INTERRUPT; sl@0: pc = pOp->p2 - 1; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Gosub P1 P2 * * * sl@0: ** sl@0: ** Write the current address onto register P1 sl@0: ** and then jump to address P2. sl@0: */ sl@0: case OP_Gosub: { /* jump */ sl@0: assert( pOp->p1>0 ); sl@0: assert( pOp->p1<=p->nMem ); sl@0: pIn1 = &p->aMem[pOp->p1]; sl@0: assert( (pIn1->flags & MEM_Dyn)==0 ); sl@0: pIn1->flags = MEM_Int; sl@0: pIn1->u.i = pc; sl@0: REGISTER_TRACE(pOp->p1, pIn1); sl@0: pc = pOp->p2 - 1; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Return P1 * * * * sl@0: ** sl@0: ** Jump to the next instruction after the address in register P1. sl@0: */ sl@0: case OP_Return: { /* in1 */ sl@0: assert( pIn1->flags & MEM_Int ); sl@0: pc = pIn1->u.i; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Yield P1 * * * * sl@0: ** sl@0: ** Swap the program counter with the value in register P1. sl@0: */ sl@0: case OP_Yield: { sl@0: int pcDest; sl@0: assert( pOp->p1>0 ); sl@0: assert( pOp->p1<=p->nMem ); sl@0: pIn1 = &p->aMem[pOp->p1]; sl@0: assert( (pIn1->flags & MEM_Dyn)==0 ); sl@0: pIn1->flags = MEM_Int; sl@0: pcDest = pIn1->u.i; sl@0: pIn1->u.i = pc; sl@0: REGISTER_TRACE(pOp->p1, pIn1); sl@0: pc = pcDest; sl@0: break; sl@0: } sl@0: sl@0: sl@0: /* Opcode: Halt P1 P2 * P4 * sl@0: ** sl@0: ** Exit immediately. All open cursors, Fifos, etc are closed sl@0: ** automatically. sl@0: ** sl@0: ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), sl@0: ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). sl@0: ** For errors, it can be some other value. If P1!=0 then P2 will determine sl@0: ** whether or not to rollback the current transaction. Do not rollback sl@0: ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, sl@0: ** then back out all changes that have occurred during this execution of the sl@0: ** VDBE, but do not rollback the transaction. sl@0: ** sl@0: ** If P4 is not null then it is an error message string. sl@0: ** sl@0: ** There is an implied "Halt 0 0 0" instruction inserted at the very end of sl@0: ** every program. So a jump past the last instruction of the program sl@0: ** is the same as executing Halt. sl@0: */ sl@0: case OP_Halt: { sl@0: p->rc = pOp->p1; sl@0: p->pc = pc; sl@0: p->errorAction = pOp->p2; sl@0: if( pOp->p4.z ){ sl@0: sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z); sl@0: } sl@0: rc = sqlite3VdbeHalt(p); sl@0: assert( rc==SQLITE_BUSY || rc==SQLITE_OK ); sl@0: if( rc==SQLITE_BUSY ){ sl@0: p->rc = rc = SQLITE_BUSY; sl@0: }else{ sl@0: rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; sl@0: } sl@0: goto vdbe_return; sl@0: } sl@0: sl@0: /* Opcode: Integer P1 P2 * * * sl@0: ** sl@0: ** The 32-bit integer value P1 is written into register P2. sl@0: */ sl@0: case OP_Integer: { /* out2-prerelease */ sl@0: pOut->flags = MEM_Int; sl@0: pOut->u.i = pOp->p1; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Int64 * P2 * P4 * sl@0: ** sl@0: ** P4 is a pointer to a 64-bit integer value. sl@0: ** Write that value into register P2. sl@0: */ sl@0: case OP_Int64: { /* out2-prerelease */ sl@0: assert( pOp->p4.pI64!=0 ); sl@0: pOut->flags = MEM_Int; sl@0: pOut->u.i = *pOp->p4.pI64; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Real * P2 * P4 * sl@0: ** sl@0: ** P4 is a pointer to a 64-bit floating point value. sl@0: ** Write that value into register P2. sl@0: */ sl@0: case OP_Real: { /* same as TK_FLOAT, out2-prerelease */ sl@0: pOut->flags = MEM_Real; sl@0: assert( !sqlite3IsNaN(*pOp->p4.pReal) ); sl@0: pOut->r = *pOp->p4.pReal; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: String8 * P2 * P4 * sl@0: ** sl@0: ** P4 points to a nul terminated UTF-8 string. This opcode is transformed sl@0: ** into an OP_String before it is executed for the first time. sl@0: */ sl@0: case OP_String8: { /* same as TK_STRING, out2-prerelease */ sl@0: assert( pOp->p4.z!=0 ); sl@0: pOp->opcode = OP_String; sl@0: pOp->p1 = strlen(pOp->p4.z); sl@0: sl@0: #ifndef SQLITE_OMIT_UTF16 sl@0: if( encoding!=SQLITE_UTF8 ){ sl@0: sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); sl@0: if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; sl@0: if( SQLITE_OK!=sqlite3VdbeMemMakeWriteable(pOut) ) goto no_mem; sl@0: pOut->zMalloc = 0; sl@0: pOut->flags |= MEM_Static; sl@0: pOut->flags &= ~MEM_Dyn; sl@0: if( pOp->p4type==P4_DYNAMIC ){ sl@0: sqlite3DbFree(db, pOp->p4.z); sl@0: } sl@0: pOp->p4type = P4_DYNAMIC; sl@0: pOp->p4.z = pOut->z; sl@0: pOp->p1 = pOut->n; sl@0: if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ sl@0: goto too_big; sl@0: } sl@0: UPDATE_MAX_BLOBSIZE(pOut); sl@0: break; sl@0: } sl@0: #endif sl@0: if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ sl@0: goto too_big; sl@0: } sl@0: /* Fall through to the next case, OP_String */ sl@0: } sl@0: sl@0: /* Opcode: String P1 P2 * P4 * sl@0: ** sl@0: ** The string value P4 of length P1 (bytes) is stored in register P2. sl@0: */ sl@0: case OP_String: { /* out2-prerelease */ sl@0: assert( pOp->p4.z!=0 ); sl@0: pOut->flags = MEM_Str|MEM_Static|MEM_Term; sl@0: pOut->z = pOp->p4.z; sl@0: pOut->n = pOp->p1; sl@0: pOut->enc = encoding; sl@0: UPDATE_MAX_BLOBSIZE(pOut); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Null * P2 * * * sl@0: ** sl@0: ** Write a NULL into register P2. sl@0: */ sl@0: case OP_Null: { /* out2-prerelease */ sl@0: break; sl@0: } sl@0: sl@0: sl@0: #ifndef SQLITE_OMIT_BLOB_LITERAL sl@0: /* Opcode: Blob P1 P2 * P4 sl@0: ** sl@0: ** P4 points to a blob of data P1 bytes long. Store this sl@0: ** blob in register P2. This instruction is not coded directly sl@0: ** by the compiler. Instead, the compiler layer specifies sl@0: ** an OP_HexBlob opcode, with the hex string representation of sl@0: ** the blob as P4. This opcode is transformed to an OP_Blob sl@0: ** the first time it is executed. sl@0: */ sl@0: case OP_Blob: { /* out2-prerelease */ sl@0: assert( pOp->p1 <= SQLITE_MAX_LENGTH ); sl@0: sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); sl@0: pOut->enc = encoding; sl@0: UPDATE_MAX_BLOBSIZE(pOut); sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_BLOB_LITERAL */ sl@0: sl@0: /* Opcode: Variable P1 P2 * * * sl@0: ** sl@0: ** The value of variable P1 is written into register P2. A variable is sl@0: ** an unknown in the original SQL string as handed to sqlite3_compile(). sl@0: ** Any occurrence of the '?' character in the original SQL is considered sl@0: ** a variable. Variables in the SQL string are number from left to sl@0: ** right beginning with 1. The values of variables are set using the sl@0: ** sqlite3_bind() API. sl@0: */ sl@0: case OP_Variable: { /* out2-prerelease */ sl@0: int j = pOp->p1 - 1; sl@0: Mem *pVar; sl@0: assert( j>=0 && jnVar ); sl@0: sl@0: pVar = &p->aVar[j]; sl@0: if( sqlite3VdbeMemTooBig(pVar) ){ sl@0: goto too_big; sl@0: } sl@0: sqlite3VdbeMemShallowCopy(pOut, &p->aVar[j], MEM_Static); sl@0: UPDATE_MAX_BLOBSIZE(pOut); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Move P1 P2 P3 * * sl@0: ** sl@0: ** Move the values in register P1..P1+P3-1 over into sl@0: ** registers P2..P2+P3-1. Registers P1..P1+P1-1 are sl@0: ** left holding a NULL. It is an error for register ranges sl@0: ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. sl@0: */ sl@0: case OP_Move: { sl@0: char *zMalloc; sl@0: int n = pOp->p3; sl@0: int p1 = pOp->p1; sl@0: int p2 = pOp->p2; sl@0: assert( n>0 ); sl@0: assert( p1>0 ); sl@0: assert( p1+nnMem ); sl@0: pIn1 = &p->aMem[p1]; sl@0: assert( p2>0 ); sl@0: assert( p2+nnMem ); sl@0: pOut = &p->aMem[p2]; sl@0: assert( p1+n<=p2 || p2+n<=p1 ); sl@0: while( n-- ){ sl@0: zMalloc = pOut->zMalloc; sl@0: pOut->zMalloc = 0; sl@0: sqlite3VdbeMemMove(pOut, pIn1); sl@0: pIn1->zMalloc = zMalloc; sl@0: REGISTER_TRACE(p2++, pOut); sl@0: pIn1++; sl@0: pOut++; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Copy P1 P2 * * * sl@0: ** sl@0: ** Make a copy of register P1 into register P2. sl@0: ** sl@0: ** This instruction makes a deep copy of the value. A duplicate sl@0: ** is made of any string or blob constant. See also OP_SCopy. sl@0: */ sl@0: case OP_Copy: { sl@0: assert( pOp->p1>0 ); sl@0: assert( pOp->p1<=p->nMem ); sl@0: pIn1 = &p->aMem[pOp->p1]; sl@0: assert( pOp->p2>0 ); sl@0: assert( pOp->p2<=p->nMem ); sl@0: pOut = &p->aMem[pOp->p2]; sl@0: assert( pOut!=pIn1 ); sl@0: sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); sl@0: Deephemeralize(pOut); sl@0: REGISTER_TRACE(pOp->p2, pOut); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: SCopy P1 P2 * * * sl@0: ** sl@0: ** Make a shallow copy of register P1 into register P2. sl@0: ** sl@0: ** This instruction makes a shallow copy of the value. If the value sl@0: ** is a string or blob, then the copy is only a pointer to the sl@0: ** original and hence if the original changes so will the copy. sl@0: ** Worse, if the original is deallocated, the copy becomes invalid. sl@0: ** Thus the program must guarantee that the original will not change sl@0: ** during the lifetime of the copy. Use OP_Copy to make a complete sl@0: ** copy. sl@0: */ sl@0: case OP_SCopy: { sl@0: assert( pOp->p1>0 ); sl@0: assert( pOp->p1<=p->nMem ); sl@0: pIn1 = &p->aMem[pOp->p1]; sl@0: REGISTER_TRACE(pOp->p1, pIn1); sl@0: assert( pOp->p2>0 ); sl@0: assert( pOp->p2<=p->nMem ); sl@0: pOut = &p->aMem[pOp->p2]; sl@0: assert( pOut!=pIn1 ); sl@0: sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); sl@0: REGISTER_TRACE(pOp->p2, pOut); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: ResultRow P1 P2 * * * sl@0: ** sl@0: ** The registers P1 through P1+P2-1 contain a single row of sl@0: ** results. This opcode causes the sqlite3_step() call to terminate sl@0: ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt sl@0: ** structure to provide access to the top P1 values as the result sl@0: ** row. sl@0: */ sl@0: case OP_ResultRow: { sl@0: Mem *pMem; sl@0: int i; sl@0: assert( p->nResColumn==pOp->p2 ); sl@0: assert( pOp->p1>0 ); sl@0: assert( pOp->p1+pOp->p2<=p->nMem ); sl@0: sl@0: /* Invalidate all ephemeral cursor row caches */ sl@0: p->cacheCtr = (p->cacheCtr + 2)|1; sl@0: sl@0: /* Make sure the results of the current row are \000 terminated sl@0: ** and have an assigned type. The results are de-ephemeralized as sl@0: ** as side effect. sl@0: */ sl@0: pMem = p->pResultSet = &p->aMem[pOp->p1]; sl@0: for(i=0; ip2; i++){ sl@0: sqlite3VdbeMemNulTerminate(&pMem[i]); sl@0: storeTypeInfo(&pMem[i], encoding); sl@0: REGISTER_TRACE(pOp->p1+i, &pMem[i]); sl@0: } sl@0: if( db->mallocFailed ) goto no_mem; sl@0: sl@0: /* Return SQLITE_ROW sl@0: */ sl@0: p->nCallback++; sl@0: p->pc = pc + 1; sl@0: rc = SQLITE_ROW; sl@0: goto vdbe_return; sl@0: } sl@0: sl@0: /* Opcode: Concat P1 P2 P3 * * sl@0: ** sl@0: ** Add the text in register P1 onto the end of the text in sl@0: ** register P2 and store the result in register P3. sl@0: ** If either the P1 or P2 text are NULL then store NULL in P3. sl@0: ** sl@0: ** P3 = P2 || P1 sl@0: ** sl@0: ** It is illegal for P1 and P3 to be the same register. Sometimes, sl@0: ** if P3 is the same register as P2, the implementation is able sl@0: ** to avoid a memcpy(). sl@0: */ sl@0: case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ sl@0: i64 nByte; sl@0: sl@0: assert( pIn1!=pOut ); sl@0: if( (pIn1->flags | pIn2->flags) & MEM_Null ){ sl@0: sqlite3VdbeMemSetNull(pOut); sl@0: break; sl@0: } sl@0: ExpandBlob(pIn1); sl@0: Stringify(pIn1, encoding); sl@0: ExpandBlob(pIn2); sl@0: Stringify(pIn2, encoding); sl@0: nByte = pIn1->n + pIn2->n; sl@0: if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ sl@0: goto too_big; sl@0: } sl@0: MemSetTypeFlag(pOut, MEM_Str); sl@0: if( sqlite3VdbeMemGrow(pOut, nByte+2, pOut==pIn2) ){ sl@0: goto no_mem; sl@0: } sl@0: if( pOut!=pIn2 ){ sl@0: memcpy(pOut->z, pIn2->z, pIn2->n); sl@0: } sl@0: memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); sl@0: pOut->z[nByte] = 0; sl@0: pOut->z[nByte+1] = 0; sl@0: pOut->flags |= MEM_Term; sl@0: pOut->n = nByte; sl@0: pOut->enc = encoding; sl@0: UPDATE_MAX_BLOBSIZE(pOut); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Add P1 P2 P3 * * sl@0: ** sl@0: ** Add the value in register P1 to the value in register P2 sl@0: ** and store the result in register P3. sl@0: ** If either input is NULL, the result is NULL. sl@0: */ sl@0: /* Opcode: Multiply P1 P2 P3 * * sl@0: ** sl@0: ** sl@0: ** Multiply the value in register P1 by the value in register P2 sl@0: ** and store the result in register P3. sl@0: ** If either input is NULL, the result is NULL. sl@0: */ sl@0: /* Opcode: Subtract P1 P2 P3 * * sl@0: ** sl@0: ** Subtract the value in register P1 from the value in register P2 sl@0: ** and store the result in register P3. sl@0: ** If either input is NULL, the result is NULL. sl@0: */ sl@0: /* Opcode: Divide P1 P2 P3 * * sl@0: ** sl@0: ** Divide the value in register P1 by the value in register P2 sl@0: ** and store the result in register P3. If the value in register P2 sl@0: ** is zero, then the result is NULL. sl@0: ** If either input is NULL, the result is NULL. sl@0: */ sl@0: /* Opcode: Remainder P1 P2 P3 * * sl@0: ** sl@0: ** Compute the remainder after integer division of the value in sl@0: ** register P1 by the value in register P2 and store the result in P3. sl@0: ** If the value in register P2 is zero the result is NULL. sl@0: ** If either operand is NULL, the result is NULL. sl@0: */ sl@0: case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ sl@0: case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ sl@0: case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ sl@0: case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ sl@0: case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ sl@0: int flags; sl@0: applyNumericAffinity(pIn1); sl@0: applyNumericAffinity(pIn2); sl@0: flags = pIn1->flags | pIn2->flags; sl@0: if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null; sl@0: if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){ sl@0: i64 a, b; sl@0: a = pIn1->u.i; sl@0: b = pIn2->u.i; sl@0: switch( pOp->opcode ){ sl@0: case OP_Add: b += a; break; sl@0: case OP_Subtract: b -= a; break; sl@0: case OP_Multiply: b *= a; break; sl@0: case OP_Divide: { sl@0: if( a==0 ) goto arithmetic_result_is_null; sl@0: /* Dividing the largest possible negative 64-bit integer (1<<63) by sl@0: ** -1 returns an integer too large to store in a 64-bit data-type. On sl@0: ** some architectures, the value overflows to (1<<63). On others, sl@0: ** a SIGFPE is issued. The following statement normalizes this sl@0: ** behavior so that all architectures behave as if integer sl@0: ** overflow occurred. sl@0: */ sl@0: if( a==-1 && b==SMALLEST_INT64 ) a = 1; sl@0: b /= a; sl@0: break; sl@0: } sl@0: default: { sl@0: if( a==0 ) goto arithmetic_result_is_null; sl@0: if( a==-1 ) a = 1; sl@0: b %= a; sl@0: break; sl@0: } sl@0: } sl@0: pOut->u.i = b; sl@0: MemSetTypeFlag(pOut, MEM_Int); sl@0: }else{ sl@0: double a, b; sl@0: a = sqlite3VdbeRealValue(pIn1); sl@0: b = sqlite3VdbeRealValue(pIn2); sl@0: switch( pOp->opcode ){ sl@0: case OP_Add: b += a; break; sl@0: case OP_Subtract: b -= a; break; sl@0: case OP_Multiply: b *= a; break; sl@0: case OP_Divide: { sl@0: if( a==0.0 ) goto arithmetic_result_is_null; sl@0: b /= a; sl@0: break; sl@0: } sl@0: default: { sl@0: i64 ia = (i64)a; sl@0: i64 ib = (i64)b; sl@0: if( ia==0 ) goto arithmetic_result_is_null; sl@0: if( ia==-1 ) ia = 1; sl@0: b = ib % ia; sl@0: break; sl@0: } sl@0: } sl@0: if( sqlite3IsNaN(b) ){ sl@0: goto arithmetic_result_is_null; sl@0: } sl@0: pOut->r = b; sl@0: MemSetTypeFlag(pOut, MEM_Real); sl@0: if( (flags & MEM_Real)==0 ){ sl@0: sqlite3VdbeIntegerAffinity(pOut); sl@0: } sl@0: } sl@0: break; sl@0: sl@0: arithmetic_result_is_null: sl@0: sqlite3VdbeMemSetNull(pOut); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: CollSeq * * P4 sl@0: ** sl@0: ** P4 is a pointer to a CollSeq struct. If the next call to a user function sl@0: ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will sl@0: ** be returned. This is used by the built-in min(), max() and nullif() sl@0: ** functions. sl@0: ** sl@0: ** The interface used by the implementation of the aforementioned functions sl@0: ** to retrieve the collation sequence set by this opcode is not available sl@0: ** publicly, only to user functions defined in func.c. sl@0: */ sl@0: case OP_CollSeq: { sl@0: assert( pOp->p4type==P4_COLLSEQ ); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Function P1 P2 P3 P4 P5 sl@0: ** sl@0: ** Invoke a user function (P4 is a pointer to a Function structure that sl@0: ** defines the function) with P5 arguments taken from register P2 and sl@0: ** successors. The result of the function is stored in register P3. sl@0: ** Register P3 must not be one of the function inputs. sl@0: ** sl@0: ** P1 is a 32-bit bitmask indicating whether or not each argument to the sl@0: ** function was determined to be constant at compile time. If the first sl@0: ** argument was constant then bit 0 of P1 is set. This is used to determine sl@0: ** whether meta data associated with a user function argument using the sl@0: ** sqlite3_set_auxdata() API may be safely retained until the next sl@0: ** invocation of this opcode. sl@0: ** sl@0: ** See also: AggStep and AggFinal sl@0: */ sl@0: case OP_Function: { sl@0: int i; sl@0: Mem *pArg; sl@0: sqlite3_context ctx; sl@0: sqlite3_value **apVal; sl@0: int n = pOp->p5; sl@0: sl@0: apVal = p->apArg; sl@0: assert( apVal || n==0 ); sl@0: sl@0: assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem) ); sl@0: assert( pOp->p3p2 || pOp->p3>=pOp->p2+n ); sl@0: pArg = &p->aMem[pOp->p2]; sl@0: for(i=0; ip2, pArg); sl@0: } sl@0: sl@0: assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC ); sl@0: if( pOp->p4type==P4_FUNCDEF ){ sl@0: ctx.pFunc = pOp->p4.pFunc; sl@0: ctx.pVdbeFunc = 0; sl@0: }else{ sl@0: ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc; sl@0: ctx.pFunc = ctx.pVdbeFunc->pFunc; sl@0: } sl@0: sl@0: assert( pOp->p3>0 && pOp->p3<=p->nMem ); sl@0: pOut = &p->aMem[pOp->p3]; sl@0: ctx.s.flags = MEM_Null; sl@0: ctx.s.db = db; sl@0: ctx.s.xDel = 0; sl@0: ctx.s.zMalloc = 0; sl@0: sl@0: /* The output cell may already have a buffer allocated. Move sl@0: ** the pointer to ctx.s so in case the user-function can use sl@0: ** the already allocated buffer instead of allocating a new one. sl@0: */ sl@0: sqlite3VdbeMemMove(&ctx.s, pOut); sl@0: MemSetTypeFlag(&ctx.s, MEM_Null); sl@0: sl@0: ctx.isError = 0; sl@0: if( ctx.pFunc->needCollSeq ){ sl@0: assert( pOp>p->aOp ); sl@0: assert( pOp[-1].p4type==P4_COLLSEQ ); sl@0: assert( pOp[-1].opcode==OP_CollSeq ); sl@0: ctx.pColl = pOp[-1].p4.pColl; sl@0: } sl@0: if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; sl@0: (*ctx.pFunc->xFunc)(&ctx, n, apVal); sl@0: if( sqlite3SafetyOn(db) ){ sl@0: sqlite3VdbeMemRelease(&ctx.s); sl@0: goto abort_due_to_misuse; sl@0: } sl@0: if( db->mallocFailed ){ sl@0: /* Even though a malloc() has failed, the implementation of the sl@0: ** user function may have called an sqlite3_result_XXX() function sl@0: ** to return a value. The following call releases any resources sl@0: ** associated with such a value. sl@0: ** sl@0: ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn() sl@0: ** fails also (the if(...) statement above). But if people are sl@0: ** misusing sqlite, they have bigger problems than a leaked value. sl@0: */ sl@0: sqlite3VdbeMemRelease(&ctx.s); sl@0: goto no_mem; sl@0: } sl@0: sl@0: /* If any auxiliary data functions have been called by this user function, sl@0: ** immediately call the destructor for any non-static values. sl@0: */ sl@0: if( ctx.pVdbeFunc ){ sl@0: sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1); sl@0: pOp->p4.pVdbeFunc = ctx.pVdbeFunc; sl@0: pOp->p4type = P4_VDBEFUNC; sl@0: } sl@0: sl@0: /* If the function returned an error, throw an exception */ sl@0: if( ctx.isError ){ sl@0: sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s)); sl@0: rc = ctx.isError; sl@0: } sl@0: sl@0: /* Copy the result of the function into register P3 */ sl@0: sqlite3VdbeChangeEncoding(&ctx.s, encoding); sl@0: sqlite3VdbeMemMove(pOut, &ctx.s); sl@0: if( sqlite3VdbeMemTooBig(pOut) ){ sl@0: goto too_big; sl@0: } sl@0: REGISTER_TRACE(pOp->p3, pOut); sl@0: UPDATE_MAX_BLOBSIZE(pOut); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: BitAnd P1 P2 P3 * * sl@0: ** sl@0: ** Take the bit-wise AND of the values in register P1 and P2 and sl@0: ** store the result in register P3. sl@0: ** If either input is NULL, the result is NULL. sl@0: */ sl@0: /* Opcode: BitOr P1 P2 P3 * * sl@0: ** sl@0: ** Take the bit-wise OR of the values in register P1 and P2 and sl@0: ** store the result in register P3. sl@0: ** If either input is NULL, the result is NULL. sl@0: */ sl@0: /* Opcode: ShiftLeft P1 P2 P3 * * sl@0: ** sl@0: ** Shift the integer value in register P2 to the left by the sl@0: ** number of bits specified by the integer in regiser P1. sl@0: ** Store the result in register P3. sl@0: ** If either input is NULL, the result is NULL. sl@0: */ sl@0: /* Opcode: ShiftRight P1 P2 P3 * * sl@0: ** sl@0: ** Shift the integer value in register P2 to the right by the sl@0: ** number of bits specified by the integer in register P1. sl@0: ** Store the result in register P3. sl@0: ** If either input is NULL, the result is NULL. sl@0: */ sl@0: case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ sl@0: case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ sl@0: case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ sl@0: case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ sl@0: i64 a, b; sl@0: sl@0: if( (pIn1->flags | pIn2->flags) & MEM_Null ){ sl@0: sqlite3VdbeMemSetNull(pOut); sl@0: break; sl@0: } sl@0: a = sqlite3VdbeIntValue(pIn2); sl@0: b = sqlite3VdbeIntValue(pIn1); sl@0: switch( pOp->opcode ){ sl@0: case OP_BitAnd: a &= b; break; sl@0: case OP_BitOr: a |= b; break; sl@0: case OP_ShiftLeft: a <<= b; break; sl@0: default: assert( pOp->opcode==OP_ShiftRight ); sl@0: a >>= b; break; sl@0: } sl@0: pOut->u.i = a; sl@0: MemSetTypeFlag(pOut, MEM_Int); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: AddImm P1 P2 * * * sl@0: ** sl@0: ** Add the constant P2 to the value in register P1. sl@0: ** The result is always an integer. sl@0: ** sl@0: ** To force any register to be an integer, just add 0. sl@0: */ sl@0: case OP_AddImm: { /* in1 */ sl@0: sqlite3VdbeMemIntegerify(pIn1); sl@0: pIn1->u.i += pOp->p2; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: ForceInt P1 P2 P3 * * sl@0: ** sl@0: ** Convert value in register P1 into an integer. If the value sl@0: ** in P1 is not numeric (meaning that is is a NULL or a string that sl@0: ** does not look like an integer or floating point number) then sl@0: ** jump to P2. If the value in P1 is numeric then sl@0: ** convert it into the least integer that is greater than or equal to its sl@0: ** current value if P3==0, or to the least integer that is strictly sl@0: ** greater than its current value if P3==1. sl@0: */ sl@0: case OP_ForceInt: { /* jump, in1 */ sl@0: i64 v; sl@0: applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); sl@0: if( (pIn1->flags & (MEM_Int|MEM_Real))==0 ){ sl@0: pc = pOp->p2 - 1; sl@0: break; sl@0: } sl@0: if( pIn1->flags & MEM_Int ){ sl@0: v = pIn1->u.i + (pOp->p3!=0); sl@0: }else{ sl@0: assert( pIn1->flags & MEM_Real ); sl@0: v = (sqlite3_int64)pIn1->r; sl@0: if( pIn1->r>(double)v ) v++; sl@0: if( pOp->p3 && pIn1->r==(double)v ) v++; sl@0: } sl@0: pIn1->u.i = v; sl@0: MemSetTypeFlag(pIn1, MEM_Int); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: MustBeInt P1 P2 * * * sl@0: ** sl@0: ** Force the value in register P1 to be an integer. If the value sl@0: ** in P1 is not an integer and cannot be converted into an integer sl@0: ** without data loss, then jump immediately to P2, or if P2==0 sl@0: ** raise an SQLITE_MISMATCH exception. sl@0: */ sl@0: case OP_MustBeInt: { /* jump, in1 */ sl@0: applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); sl@0: if( (pIn1->flags & MEM_Int)==0 ){ sl@0: if( pOp->p2==0 ){ sl@0: rc = SQLITE_MISMATCH; sl@0: goto abort_due_to_error; sl@0: }else{ sl@0: pc = pOp->p2 - 1; sl@0: } sl@0: }else{ sl@0: MemSetTypeFlag(pIn1, MEM_Int); sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: RealAffinity P1 * * * * sl@0: ** sl@0: ** If register P1 holds an integer convert it to a real value. sl@0: ** sl@0: ** This opcode is used when extracting information from a column that sl@0: ** has REAL affinity. Such column values may still be stored as sl@0: ** integers, for space efficiency, but after extraction we want them sl@0: ** to have only a real value. sl@0: */ sl@0: case OP_RealAffinity: { /* in1 */ sl@0: if( pIn1->flags & MEM_Int ){ sl@0: sqlite3VdbeMemRealify(pIn1); sl@0: } sl@0: break; sl@0: } sl@0: sl@0: #ifndef SQLITE_OMIT_CAST sl@0: /* Opcode: ToText P1 * * * * sl@0: ** sl@0: ** Force the value in register P1 to be text. sl@0: ** If the value is numeric, convert it to a string using the sl@0: ** equivalent of printf(). Blob values are unchanged and sl@0: ** are afterwards simply interpreted as text. sl@0: ** sl@0: ** A NULL value is not changed by this routine. It remains NULL. sl@0: */ sl@0: case OP_ToText: { /* same as TK_TO_TEXT, in1 */ sl@0: if( pIn1->flags & MEM_Null ) break; sl@0: assert( MEM_Str==(MEM_Blob>>3) ); sl@0: pIn1->flags |= (pIn1->flags&MEM_Blob)>>3; sl@0: applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); sl@0: rc = ExpandBlob(pIn1); sl@0: assert( pIn1->flags & MEM_Str || db->mallocFailed ); sl@0: pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob); sl@0: UPDATE_MAX_BLOBSIZE(pIn1); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: ToBlob P1 * * * * sl@0: ** sl@0: ** Force the value in register P1 to be a BLOB. sl@0: ** If the value is numeric, convert it to a string first. sl@0: ** Strings are simply reinterpreted as blobs with no change sl@0: ** to the underlying data. sl@0: ** sl@0: ** A NULL value is not changed by this routine. It remains NULL. sl@0: */ sl@0: case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */ sl@0: if( pIn1->flags & MEM_Null ) break; sl@0: if( (pIn1->flags & MEM_Blob)==0 ){ sl@0: applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); sl@0: assert( pIn1->flags & MEM_Str || db->mallocFailed ); sl@0: } sl@0: MemSetTypeFlag(pIn1, MEM_Blob); sl@0: UPDATE_MAX_BLOBSIZE(pIn1); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: ToNumeric P1 * * * * sl@0: ** sl@0: ** Force the value in register P1 to be numeric (either an sl@0: ** integer or a floating-point number.) sl@0: ** If the value is text or blob, try to convert it to an using the sl@0: ** equivalent of atoi() or atof() and store 0 if no such conversion sl@0: ** is possible. sl@0: ** sl@0: ** A NULL value is not changed by this routine. It remains NULL. sl@0: */ sl@0: case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */ sl@0: if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){ sl@0: sqlite3VdbeMemNumerify(pIn1); sl@0: } sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_CAST */ sl@0: sl@0: /* Opcode: ToInt P1 * * * * sl@0: ** sl@0: ** Force the value in register P1 be an integer. If sl@0: ** The value is currently a real number, drop its fractional part. sl@0: ** If the value is text or blob, try to convert it to an integer using the sl@0: ** equivalent of atoi() and store 0 if no such conversion is possible. sl@0: ** sl@0: ** A NULL value is not changed by this routine. It remains NULL. sl@0: */ sl@0: case OP_ToInt: { /* same as TK_TO_INT, in1 */ sl@0: if( (pIn1->flags & MEM_Null)==0 ){ sl@0: sqlite3VdbeMemIntegerify(pIn1); sl@0: } sl@0: break; sl@0: } sl@0: sl@0: #ifndef SQLITE_OMIT_CAST sl@0: /* Opcode: ToReal P1 * * * * sl@0: ** sl@0: ** Force the value in register P1 to be a floating point number. sl@0: ** If The value is currently an integer, convert it. sl@0: ** If the value is text or blob, try to convert it to an integer using the sl@0: ** equivalent of atoi() and store 0.0 if no such conversion is possible. sl@0: ** sl@0: ** A NULL value is not changed by this routine. It remains NULL. sl@0: */ sl@0: case OP_ToReal: { /* same as TK_TO_REAL, in1 */ sl@0: if( (pIn1->flags & MEM_Null)==0 ){ sl@0: sqlite3VdbeMemRealify(pIn1); sl@0: } sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_CAST */ sl@0: sl@0: /* Opcode: Lt P1 P2 P3 P4 P5 sl@0: ** sl@0: ** Compare the values in register P1 and P3. If reg(P3)flags|pIn3->flags; sl@0: sl@0: if( flags&MEM_Null ){ sl@0: /* If either operand is NULL then the result is always NULL. sl@0: ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. sl@0: */ sl@0: if( pOp->p5 & SQLITE_STOREP2 ){ sl@0: pOut = &p->aMem[pOp->p2]; sl@0: MemSetTypeFlag(pOut, MEM_Null); sl@0: REGISTER_TRACE(pOp->p2, pOut); sl@0: }else if( pOp->p5 & SQLITE_JUMPIFNULL ){ sl@0: pc = pOp->p2-1; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: affinity = pOp->p5 & SQLITE_AFF_MASK; sl@0: if( affinity ){ sl@0: applyAffinity(pIn1, affinity, encoding); sl@0: applyAffinity(pIn3, affinity, encoding); sl@0: } sl@0: sl@0: assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); sl@0: ExpandBlob(pIn1); sl@0: ExpandBlob(pIn3); sl@0: res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); sl@0: switch( pOp->opcode ){ sl@0: case OP_Eq: res = res==0; break; sl@0: case OP_Ne: res = res!=0; break; sl@0: case OP_Lt: res = res<0; break; sl@0: case OP_Le: res = res<=0; break; sl@0: case OP_Gt: res = res>0; break; sl@0: default: res = res>=0; break; sl@0: } sl@0: sl@0: if( pOp->p5 & SQLITE_STOREP2 ){ sl@0: pOut = &p->aMem[pOp->p2]; sl@0: MemSetTypeFlag(pOut, MEM_Int); sl@0: pOut->u.i = res; sl@0: REGISTER_TRACE(pOp->p2, pOut); sl@0: }else if( res ){ sl@0: pc = pOp->p2-1; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Permutation * * * P4 * sl@0: ** sl@0: ** Set the permuation used by the OP_Compare operator to be the array sl@0: ** of integers in P4. sl@0: ** sl@0: ** The permutation is only valid until the next OP_Permutation, OP_Compare, sl@0: ** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur sl@0: ** immediately prior to the OP_Compare. sl@0: */ sl@0: case OP_Permutation: { sl@0: assert( pOp->p4type==P4_INTARRAY ); sl@0: assert( pOp->p4.ai ); sl@0: aPermute = pOp->p4.ai; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Compare P1 P2 P3 P4 * sl@0: ** sl@0: ** Compare to vectors of registers in reg(P1)..reg(P1+P3-1) (all this sl@0: ** one "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of sl@0: ** the comparison for use by the next OP_Jump instruct. sl@0: ** sl@0: ** P4 is a KeyInfo structure that defines collating sequences and sort sl@0: ** orders for the comparison. The permutation applies to registers sl@0: ** only. The KeyInfo elements are used sequentially. sl@0: ** sl@0: ** The comparison is a sort comparison, so NULLs compare equal, sl@0: ** NULLs are less than numbers, numbers are less than strings, sl@0: ** and strings are less than blobs. sl@0: */ sl@0: case OP_Compare: { sl@0: int n = pOp->p3; sl@0: int i, p1, p2; sl@0: const KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; sl@0: assert( n>0 ); sl@0: assert( pKeyInfo!=0 ); sl@0: p1 = pOp->p1; sl@0: assert( p1>0 && p1+n-1nMem ); sl@0: p2 = pOp->p2; sl@0: assert( p2>0 && p2+n-1nMem ); sl@0: for(i=0; iaMem[p1+idx]); sl@0: REGISTER_TRACE(p2+idx, &p->aMem[p2+idx]); sl@0: assert( inField ); sl@0: pColl = pKeyInfo->aColl[i]; sl@0: bRev = pKeyInfo->aSortOrder[i]; sl@0: iCompare = sqlite3MemCompare(&p->aMem[p1+idx], &p->aMem[p2+idx], pColl); sl@0: if( iCompare ){ sl@0: if( bRev ) iCompare = -iCompare; sl@0: break; sl@0: } sl@0: } sl@0: aPermute = 0; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Jump P1 P2 P3 * * sl@0: ** sl@0: ** Jump to the instruction at address P1, P2, or P3 depending on whether sl@0: ** in the most recent OP_Compare instruction the P1 vector was less than sl@0: ** equal to, or greater than the P2 vector, respectively. sl@0: */ sl@0: case OP_Jump: { /* jump */ sl@0: if( iCompare<0 ){ sl@0: pc = pOp->p1 - 1; sl@0: }else if( iCompare==0 ){ sl@0: pc = pOp->p2 - 1; sl@0: }else{ sl@0: pc = pOp->p3 - 1; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: And P1 P2 P3 * * sl@0: ** sl@0: ** Take the logical AND of the values in registers P1 and P2 and sl@0: ** write the result into register P3. sl@0: ** sl@0: ** If either P1 or P2 is 0 (false) then the result is 0 even if sl@0: ** the other input is NULL. A NULL and true or two NULLs give sl@0: ** a NULL output. sl@0: */ sl@0: /* Opcode: Or P1 P2 P3 * * sl@0: ** sl@0: ** Take the logical OR of the values in register P1 and P2 and sl@0: ** store the answer in register P3. sl@0: ** sl@0: ** If either P1 or P2 is nonzero (true) then the result is 1 (true) sl@0: ** even if the other input is NULL. A NULL and false or two NULLs sl@0: ** give a NULL output. sl@0: */ sl@0: case OP_And: /* same as TK_AND, in1, in2, out3 */ sl@0: case OP_Or: { /* same as TK_OR, in1, in2, out3 */ sl@0: int v1, v2; /* 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ sl@0: sl@0: if( pIn1->flags & MEM_Null ){ sl@0: v1 = 2; sl@0: }else{ sl@0: v1 = sqlite3VdbeIntValue(pIn1)!=0; sl@0: } sl@0: if( pIn2->flags & MEM_Null ){ sl@0: v2 = 2; sl@0: }else{ sl@0: v2 = sqlite3VdbeIntValue(pIn2)!=0; sl@0: } sl@0: if( pOp->opcode==OP_And ){ sl@0: static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; sl@0: v1 = and_logic[v1*3+v2]; sl@0: }else{ sl@0: static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 }; sl@0: v1 = or_logic[v1*3+v2]; sl@0: } sl@0: if( v1==2 ){ sl@0: MemSetTypeFlag(pOut, MEM_Null); sl@0: }else{ sl@0: pOut->u.i = v1; sl@0: MemSetTypeFlag(pOut, MEM_Int); sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Not P1 * * * * sl@0: ** sl@0: ** Interpret the value in register P1 as a boolean value. Replace it sl@0: ** with its complement. If the value in register P1 is NULL its value sl@0: ** is unchanged. sl@0: */ sl@0: case OP_Not: { /* same as TK_NOT, in1 */ sl@0: if( pIn1->flags & MEM_Null ) break; /* Do nothing to NULLs */ sl@0: sqlite3VdbeMemIntegerify(pIn1); sl@0: pIn1->u.i = !pIn1->u.i; sl@0: assert( pIn1->flags&MEM_Int ); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: BitNot P1 * * * * sl@0: ** sl@0: ** Interpret the content of register P1 as an integer. Replace it sl@0: ** with its ones-complement. If the value is originally NULL, leave sl@0: ** it unchanged. sl@0: */ sl@0: case OP_BitNot: { /* same as TK_BITNOT, in1 */ sl@0: if( pIn1->flags & MEM_Null ) break; /* Do nothing to NULLs */ sl@0: sqlite3VdbeMemIntegerify(pIn1); sl@0: pIn1->u.i = ~pIn1->u.i; sl@0: assert( pIn1->flags&MEM_Int ); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: If P1 P2 P3 * * sl@0: ** sl@0: ** Jump to P2 if the value in register P1 is true. The value is sl@0: ** is considered true if it is numeric and non-zero. If the value sl@0: ** in P1 is NULL then take the jump if P3 is true. sl@0: */ sl@0: /* Opcode: IfNot P1 P2 P3 * * sl@0: ** sl@0: ** Jump to P2 if the value in register P1 is False. The value is sl@0: ** is considered true if it has a numeric value of zero. If the value sl@0: ** in P1 is NULL then take the jump if P3 is true. sl@0: */ sl@0: case OP_If: /* jump, in1 */ sl@0: case OP_IfNot: { /* jump, in1 */ sl@0: int c; sl@0: if( pIn1->flags & MEM_Null ){ sl@0: c = pOp->p3; sl@0: }else{ sl@0: #ifdef SQLITE_OMIT_FLOATING_POINT sl@0: c = sqlite3VdbeIntValue(pIn1); sl@0: #else sl@0: c = sqlite3VdbeRealValue(pIn1)!=0.0; sl@0: #endif sl@0: if( pOp->opcode==OP_IfNot ) c = !c; sl@0: } sl@0: if( c ){ sl@0: pc = pOp->p2-1; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: IsNull P1 P2 P3 * * sl@0: ** sl@0: ** Jump to P2 if the value in register P1 is NULL. If P3 is greater sl@0: ** than zero, then check all values reg(P1), reg(P1+1), sl@0: ** reg(P1+2), ..., reg(P1+P3-1). sl@0: */ sl@0: case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ sl@0: int n = pOp->p3; sl@0: assert( pOp->p3==0 || pOp->p1>0 ); sl@0: do{ sl@0: if( (pIn1->flags & MEM_Null)!=0 ){ sl@0: pc = pOp->p2 - 1; sl@0: break; sl@0: } sl@0: pIn1++; sl@0: }while( --n > 0 ); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: NotNull P1 P2 * * * sl@0: ** sl@0: ** Jump to P2 if the value in register P1 is not NULL. sl@0: */ sl@0: case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ sl@0: if( (pIn1->flags & MEM_Null)==0 ){ sl@0: pc = pOp->p2 - 1; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: SetNumColumns * P2 * * * sl@0: ** sl@0: ** This opcode sets the number of columns for the cursor opened by the sl@0: ** following instruction to P2. sl@0: ** sl@0: ** An OP_SetNumColumns is only useful if it occurs immediately before sl@0: ** one of the following opcodes: sl@0: ** sl@0: ** OpenRead sl@0: ** OpenWrite sl@0: ** OpenPseudo sl@0: ** sl@0: ** If the OP_Column opcode is to be executed on a cursor, then sl@0: ** this opcode must be present immediately before the opcode that sl@0: ** opens the cursor. sl@0: */ sl@0: case OP_SetNumColumns: { sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Column P1 P2 P3 P4 * sl@0: ** sl@0: ** Interpret the data that cursor P1 points to as a structure built using sl@0: ** the MakeRecord instruction. (See the MakeRecord opcode for additional sl@0: ** information about the format of the data.) Extract the P2-th column sl@0: ** from this record. If there are less that (P2+1) sl@0: ** values in the record, extract a NULL. sl@0: ** sl@0: ** The value extracted is stored in register P3. sl@0: ** sl@0: ** If the KeyAsData opcode has previously executed on this cursor, then the sl@0: ** field might be extracted from the key rather than the data. sl@0: ** sl@0: ** If the column contains fewer than P2 fields, then extract a NULL. Or, sl@0: ** if the P4 argument is a P4_MEM use the value of the P4 argument as sl@0: ** the result. sl@0: */ sl@0: case OP_Column: { sl@0: u32 payloadSize; /* Number of bytes in the record */ sl@0: int p1 = pOp->p1; /* P1 value of the opcode */ sl@0: int p2 = pOp->p2; /* column number to retrieve */ sl@0: Cursor *pC = 0; /* The VDBE cursor */ sl@0: char *zRec; /* Pointer to complete record-data */ sl@0: BtCursor *pCrsr; /* The BTree cursor */ sl@0: u32 *aType; /* aType[i] holds the numeric type of the i-th column */ sl@0: u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ sl@0: u32 nField; /* number of fields in the record */ sl@0: int len; /* The length of the serialized data for the column */ sl@0: int i; /* Loop counter */ sl@0: char *zData; /* Part of the record being decoded */ sl@0: Mem *pDest; /* Where to write the extracted value */ sl@0: Mem sMem; /* For storing the record being decoded */ sl@0: sl@0: sMem.flags = 0; sl@0: sMem.db = 0; sl@0: sMem.zMalloc = 0; sl@0: assert( p1nCursor ); sl@0: assert( pOp->p3>0 && pOp->p3<=p->nMem ); sl@0: pDest = &p->aMem[pOp->p3]; sl@0: MemSetTypeFlag(pDest, MEM_Null); sl@0: sl@0: /* This block sets the variable payloadSize to be the total number of sl@0: ** bytes in the record. sl@0: ** sl@0: ** zRec is set to be the complete text of the record if it is available. sl@0: ** The complete record text is always available for pseudo-tables sl@0: ** If the record is stored in a cursor, the complete record text sl@0: ** might be available in the pC->aRow cache. Or it might not be. sl@0: ** If the data is unavailable, zRec is set to NULL. sl@0: ** sl@0: ** We also compute the number of columns in the record. For cursors, sl@0: ** the number of columns is stored in the Cursor.nField element. sl@0: */ sl@0: pC = p->apCsr[p1]; sl@0: assert( pC!=0 ); sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: assert( pC->pVtabCursor==0 ); sl@0: #endif sl@0: if( pC->pCursor!=0 ){ sl@0: /* The record is stored in a B-Tree */ sl@0: rc = sqlite3VdbeCursorMoveto(pC); sl@0: if( rc ) goto abort_due_to_error; sl@0: zRec = 0; sl@0: pCrsr = pC->pCursor; sl@0: if( pC->nullRow ){ sl@0: payloadSize = 0; sl@0: }else if( pC->cacheStatus==p->cacheCtr ){ sl@0: payloadSize = pC->payloadSize; sl@0: zRec = (char*)pC->aRow; sl@0: }else if( pC->isIndex ){ sl@0: i64 payloadSize64; sl@0: sqlite3BtreeKeySize(pCrsr, &payloadSize64); sl@0: payloadSize = payloadSize64; sl@0: }else{ sl@0: sqlite3BtreeDataSize(pCrsr, &payloadSize); sl@0: } sl@0: nField = pC->nField; sl@0: }else{ sl@0: assert( pC->pseudoTable ); sl@0: /* The record is the sole entry of a pseudo-table */ sl@0: payloadSize = pC->nData; sl@0: zRec = pC->pData; sl@0: pC->cacheStatus = CACHE_STALE; sl@0: assert( payloadSize==0 || zRec!=0 ); sl@0: nField = pC->nField; sl@0: pCrsr = 0; sl@0: } sl@0: sl@0: /* If payloadSize is 0, then just store a NULL */ sl@0: if( payloadSize==0 ){ sl@0: assert( pDest->flags&MEM_Null ); sl@0: goto op_column_out; sl@0: } sl@0: if( payloadSize>db->aLimit[SQLITE_LIMIT_LENGTH] ){ sl@0: goto too_big; sl@0: } sl@0: sl@0: assert( p2aType; sl@0: if( pC->cacheStatus==p->cacheCtr ){ sl@0: aOffset = pC->aOffset; sl@0: }else{ sl@0: u8 *zIdx; /* Index into header */ sl@0: u8 *zEndHdr; /* Pointer to first byte after the header */ sl@0: u32 offset; /* Offset into the data */ sl@0: int szHdrSz; /* Size of the header size field at start of record */ sl@0: int avail; /* Number of bytes of available data */ sl@0: sl@0: assert(aType); sl@0: pC->aOffset = aOffset = &aType[nField]; sl@0: pC->payloadSize = payloadSize; sl@0: pC->cacheStatus = p->cacheCtr; sl@0: sl@0: /* Figure out how many bytes are in the header */ sl@0: if( zRec ){ sl@0: zData = zRec; sl@0: }else{ sl@0: if( pC->isIndex ){ sl@0: zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail); sl@0: }else{ sl@0: zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail); sl@0: } sl@0: /* If KeyFetch()/DataFetch() managed to get the entire payload, sl@0: ** save the payload in the pC->aRow cache. That will save us from sl@0: ** having to make additional calls to fetch the content portion of sl@0: ** the record. sl@0: */ sl@0: if( avail>=payloadSize ){ sl@0: zRec = zData; sl@0: pC->aRow = (u8*)zData; sl@0: }else{ sl@0: pC->aRow = 0; sl@0: } sl@0: } sl@0: /* The following assert is true in all cases accept when sl@0: ** the database file has been corrupted externally. sl@0: ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */ sl@0: szHdrSz = getVarint32((u8*)zData, offset); sl@0: sl@0: /* The KeyFetch() or DataFetch() above are fast and will get the entire sl@0: ** record header in most cases. But they will fail to get the complete sl@0: ** record header if the record header does not fit on a single page sl@0: ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to sl@0: ** acquire the complete header text. sl@0: */ sl@0: if( !zRec && availisIndex, &sMem); sl@0: if( rc!=SQLITE_OK ){ sl@0: goto op_column_out; sl@0: } sl@0: zData = sMem.z; sl@0: } sl@0: zEndHdr = (u8 *)&zData[offset]; sl@0: zIdx = (u8 *)&zData[szHdrSz]; sl@0: sl@0: /* Scan the header and use it to fill in the aType[] and aOffset[] sl@0: ** arrays. aType[i] will contain the type integer for the i-th sl@0: ** column and aOffset[i] will contain the offset from the beginning sl@0: ** of the record to the start of the data for the i-th column sl@0: */ sl@0: for(i=0; izEndHdr || offset>payloadSize || (zIdx==zEndHdr && offset!=payloadSize) ){ sl@0: rc = SQLITE_CORRUPT_BKPT; sl@0: goto op_column_out; sl@0: } sl@0: } sl@0: sl@0: /* Get the column information. If aOffset[p2] is non-zero, then sl@0: ** deserialize the value from the record. If aOffset[p2] is zero, sl@0: ** then there are not enough fields in the record to satisfy the sl@0: ** request. In this case, set the value NULL or to P4 if P4 is sl@0: ** a pointer to a Mem object. sl@0: */ sl@0: if( aOffset[p2] ){ sl@0: assert( rc==SQLITE_OK ); sl@0: if( zRec ){ sl@0: sqlite3VdbeMemReleaseExternal(pDest); sl@0: sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest); sl@0: }else{ sl@0: len = sqlite3VdbeSerialTypeLen(aType[p2]); sl@0: sqlite3VdbeMemMove(&sMem, pDest); sl@0: rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem); sl@0: if( rc!=SQLITE_OK ){ sl@0: goto op_column_out; sl@0: } sl@0: zData = sMem.z; sl@0: sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest); sl@0: } sl@0: pDest->enc = encoding; sl@0: }else{ sl@0: if( pOp->p4type==P4_MEM ){ sl@0: sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); sl@0: }else{ sl@0: assert( pDest->flags&MEM_Null ); sl@0: } sl@0: } sl@0: sl@0: /* If we dynamically allocated space to hold the data (in the sl@0: ** sqlite3VdbeMemFromBtree() call above) then transfer control of that sl@0: ** dynamically allocated space over to the pDest structure. sl@0: ** This prevents a memory copy. sl@0: */ sl@0: if( sMem.zMalloc ){ sl@0: assert( sMem.z==sMem.zMalloc ); sl@0: assert( !(pDest->flags & MEM_Dyn) ); sl@0: assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z ); sl@0: pDest->flags &= ~(MEM_Ephem|MEM_Static); sl@0: pDest->flags |= MEM_Term; sl@0: pDest->z = sMem.z; sl@0: pDest->zMalloc = sMem.zMalloc; sl@0: } sl@0: sl@0: rc = sqlite3VdbeMemMakeWriteable(pDest); sl@0: sl@0: op_column_out: sl@0: UPDATE_MAX_BLOBSIZE(pDest); sl@0: REGISTER_TRACE(pOp->p3, pDest); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Affinity P1 P2 * P4 * sl@0: ** sl@0: ** Apply affinities to a range of P2 registers starting with P1. sl@0: ** sl@0: ** P4 is a string that is P2 characters long. The nth character of the sl@0: ** string indicates the column affinity that should be used for the nth sl@0: ** memory cell in the range. sl@0: */ sl@0: case OP_Affinity: { sl@0: char *zAffinity = pOp->p4.z; sl@0: Mem *pData0 = &p->aMem[pOp->p1]; sl@0: Mem *pLast = &pData0[pOp->p2-1]; sl@0: Mem *pRec; sl@0: sl@0: for(pRec=pData0; pRec<=pLast; pRec++){ sl@0: ExpandBlob(pRec); sl@0: applyAffinity(pRec, zAffinity[pRec-pData0], encoding); sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: MakeRecord P1 P2 P3 P4 * sl@0: ** sl@0: ** Convert P2 registers beginning with P1 into a single entry sl@0: ** suitable for use as a data record in a database table or as a key sl@0: ** in an index. The details of the format are irrelevant as long as sl@0: ** the OP_Column opcode can decode the record later. sl@0: ** Refer to source code comments for the details of the record sl@0: ** format. sl@0: ** sl@0: ** P4 may be a string that is P2 characters long. The nth character of the sl@0: ** string indicates the column affinity that should be used for the nth sl@0: ** field of the index key. sl@0: ** sl@0: ** The mapping from character to affinity is given by the SQLITE_AFF_ sl@0: ** macros defined in sqliteInt.h. sl@0: ** sl@0: ** If P4 is NULL then all index fields have the affinity NONE. sl@0: */ sl@0: case OP_MakeRecord: { sl@0: /* Assuming the record contains N fields, the record format looks sl@0: ** like this: sl@0: ** sl@0: ** ------------------------------------------------------------------------ sl@0: ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | sl@0: ** ------------------------------------------------------------------------ sl@0: ** sl@0: ** Data(0) is taken from register P1. Data(1) comes from register P1+1 sl@0: ** and so froth. sl@0: ** sl@0: ** Each type field is a varint representing the serial type of the sl@0: ** corresponding data element (see sqlite3VdbeSerialType()). The sl@0: ** hdr-size field is also a varint which is the offset from the beginning sl@0: ** of the record to data0. sl@0: */ sl@0: u8 *zNewRecord; /* A buffer to hold the data for the new record */ sl@0: Mem *pRec; /* The new record */ sl@0: u64 nData = 0; /* Number of bytes of data space */ sl@0: int nHdr = 0; /* Number of bytes of header space */ sl@0: u64 nByte = 0; /* Data space required for this record */ sl@0: int nZero = 0; /* Number of zero bytes at the end of the record */ sl@0: int nVarint; /* Number of bytes in a varint */ sl@0: u32 serial_type; /* Type field */ sl@0: Mem *pData0; /* First field to be combined into the record */ sl@0: Mem *pLast; /* Last field of the record */ sl@0: int nField; /* Number of fields in the record */ sl@0: char *zAffinity; /* The affinity string for the record */ sl@0: int file_format; /* File format to use for encoding */ sl@0: int i; /* Space used in zNewRecord[] */ sl@0: sl@0: nField = pOp->p1; sl@0: zAffinity = pOp->p4.z; sl@0: assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem ); sl@0: pData0 = &p->aMem[nField]; sl@0: nField = pOp->p2; sl@0: pLast = &pData0[nField-1]; sl@0: file_format = p->minWriteFileFormat; sl@0: sl@0: /* Loop through the elements that will make up the record to figure sl@0: ** out how much space is required for the new record. sl@0: */ sl@0: for(pRec=pData0; pRec<=pLast; pRec++){ sl@0: int len; sl@0: if( zAffinity ){ sl@0: applyAffinity(pRec, zAffinity[pRec-pData0], encoding); sl@0: } sl@0: if( pRec->flags&MEM_Zero && pRec->n>0 ){ sl@0: sqlite3VdbeMemExpandBlob(pRec); sl@0: } sl@0: serial_type = sqlite3VdbeSerialType(pRec, file_format); sl@0: len = sqlite3VdbeSerialTypeLen(serial_type); sl@0: nData += len; sl@0: nHdr += sqlite3VarintLen(serial_type); sl@0: if( pRec->flags & MEM_Zero ){ sl@0: /* Only pure zero-filled BLOBs can be input to this Opcode. sl@0: ** We do not allow blobs with a prefix and a zero-filled tail. */ sl@0: nZero += pRec->u.i; sl@0: }else if( len ){ sl@0: nZero = 0; sl@0: } sl@0: } sl@0: sl@0: /* Add the initial header varint and total the size */ sl@0: nHdr += nVarint = sqlite3VarintLen(nHdr); sl@0: if( nVarintdb->aLimit[SQLITE_LIMIT_LENGTH] ){ sl@0: goto too_big; sl@0: } sl@0: sl@0: /* Make sure the output register has a buffer large enough to store sl@0: ** the new record. The output register (pOp->p3) is not allowed to sl@0: ** be one of the input registers (because the following call to sl@0: ** sqlite3VdbeMemGrow() could clobber the value before it is used). sl@0: */ sl@0: assert( pOp->p3p1 || pOp->p3>=pOp->p1+pOp->p2 ); sl@0: pOut = &p->aMem[pOp->p3]; sl@0: if( sqlite3VdbeMemGrow(pOut, nByte, 0) ){ sl@0: goto no_mem; sl@0: } sl@0: zNewRecord = (u8 *)pOut->z; sl@0: sl@0: /* Write the record */ sl@0: i = putVarint32(zNewRecord, nHdr); sl@0: for(pRec=pData0; pRec<=pLast; pRec++){ sl@0: serial_type = sqlite3VdbeSerialType(pRec, file_format); sl@0: i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ sl@0: } sl@0: for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */ sl@0: i += sqlite3VdbeSerialPut(&zNewRecord[i], nByte-i, pRec, file_format); sl@0: } sl@0: assert( i==nByte ); sl@0: sl@0: assert( pOp->p3>0 && pOp->p3<=p->nMem ); sl@0: pOut->n = nByte; sl@0: pOut->flags = MEM_Blob | MEM_Dyn; sl@0: pOut->xDel = 0; sl@0: if( nZero ){ sl@0: pOut->u.i = nZero; sl@0: pOut->flags |= MEM_Zero; sl@0: } sl@0: pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ sl@0: REGISTER_TRACE(pOp->p3, pOut); sl@0: UPDATE_MAX_BLOBSIZE(pOut); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Statement P1 * * * * sl@0: ** sl@0: ** Begin an individual statement transaction which is part of a larger sl@0: ** transaction. This is needed so that the statement sl@0: ** can be rolled back after an error without having to roll back the sl@0: ** entire transaction. The statement transaction will automatically sl@0: ** commit when the VDBE halts. sl@0: ** sl@0: ** If the database connection is currently in autocommit mode (that sl@0: ** is to say, if it is in between BEGIN and COMMIT) sl@0: ** and if there are no other active statements on the same database sl@0: ** connection, then this operation is a no-op. No statement transaction sl@0: ** is needed since any error can use the normal ROLLBACK process to sl@0: ** undo changes. sl@0: ** sl@0: ** If a statement transaction is started, then a statement journal file sl@0: ** will be allocated and initialized. sl@0: ** sl@0: ** The statement is begun on the database file with index P1. The main sl@0: ** database file has an index of 0 and the file used for temporary tables sl@0: ** has an index of 1. sl@0: */ sl@0: case OP_Statement: { sl@0: if( db->autoCommit==0 || db->activeVdbeCnt>1 ){ sl@0: int i = pOp->p1; sl@0: Btree *pBt; sl@0: assert( i>=0 && inDb ); sl@0: assert( db->aDb[i].pBt!=0 ); sl@0: pBt = db->aDb[i].pBt; sl@0: assert( sqlite3BtreeIsInTrans(pBt) ); sl@0: assert( (p->btreeMask & (1<openedStatement = 1; sl@0: } sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: AutoCommit P1 P2 * * * sl@0: ** sl@0: ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll sl@0: ** back any currently active btree transactions. If there are any active sl@0: ** VMs (apart from this one), then the COMMIT or ROLLBACK statement fails. sl@0: ** sl@0: ** This instruction causes the VM to halt. sl@0: */ sl@0: case OP_AutoCommit: { sl@0: u8 i = pOp->p1; sl@0: u8 rollback = pOp->p2; sl@0: sl@0: assert( i==1 || i==0 ); sl@0: assert( i==1 || rollback==0 ); sl@0: sl@0: assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */ sl@0: sl@0: if( db->activeVdbeCnt>1 && i && !db->autoCommit ){ sl@0: /* If this instruction implements a COMMIT or ROLLBACK, other VMs are sl@0: ** still running, and a transaction is active, return an error indicating sl@0: ** that the other VMs must complete first. sl@0: */ sl@0: sqlite3SetString(&p->zErrMsg, db, "cannot %s transaction - " sl@0: "SQL statements in progress", sl@0: rollback ? "rollback" : "commit"); sl@0: rc = SQLITE_ERROR; sl@0: }else if( i!=db->autoCommit ){ sl@0: if( pOp->p2 ){ sl@0: assert( i==1 ); sl@0: sqlite3RollbackAll(db); sl@0: db->autoCommit = 1; sl@0: }else{ sl@0: db->autoCommit = i; sl@0: if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ sl@0: p->pc = pc; sl@0: db->autoCommit = 1-i; sl@0: p->rc = rc = SQLITE_BUSY; sl@0: goto vdbe_return; sl@0: } sl@0: } sl@0: if( p->rc==SQLITE_OK ){ sl@0: rc = SQLITE_DONE; sl@0: }else{ sl@0: rc = SQLITE_ERROR; sl@0: } sl@0: goto vdbe_return; sl@0: }else{ sl@0: sqlite3SetString(&p->zErrMsg, db, sl@0: (!i)?"cannot start a transaction within a transaction":( sl@0: (rollback)?"cannot rollback - no transaction is active": sl@0: "cannot commit - no transaction is active")); sl@0: sl@0: rc = SQLITE_ERROR; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Transaction P1 P2 * * * sl@0: ** sl@0: ** Begin a transaction. The transaction ends when a Commit or Rollback sl@0: ** opcode is encountered. Depending on the ON CONFLICT setting, the sl@0: ** transaction might also be rolled back if an error is encountered. sl@0: ** sl@0: ** P1 is the index of the database file on which the transaction is sl@0: ** started. Index 0 is the main database file and index 1 is the sl@0: ** file used for temporary tables. Indices of 2 or more are used for sl@0: ** attached databases. sl@0: ** sl@0: ** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is sl@0: ** obtained on the database file when a write-transaction is started. No sl@0: ** other process can start another write transaction while this transaction is sl@0: ** underway. Starting a write transaction also creates a rollback journal. A sl@0: ** write transaction must be started before any changes can be made to the sl@0: ** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained sl@0: ** on the file. sl@0: ** sl@0: ** If P2 is zero, then a read-lock is obtained on the database file. sl@0: */ sl@0: case OP_Transaction: { sl@0: int i = pOp->p1; sl@0: Btree *pBt; sl@0: sl@0: assert( i>=0 && inDb ); sl@0: assert( (p->btreeMask & (1<aDb[i].pBt; sl@0: sl@0: if( pBt ){ sl@0: rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); sl@0: if( rc==SQLITE_BUSY ){ sl@0: p->pc = pc; sl@0: p->rc = rc = SQLITE_BUSY; sl@0: goto vdbe_return; sl@0: } sl@0: if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){ sl@0: goto abort_due_to_error; sl@0: } sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: ReadCookie P1 P2 P3 * * sl@0: ** sl@0: ** Read cookie number P3 from database P1 and write it into register P2. sl@0: ** P3==0 is the schema version. P3==1 is the database format. sl@0: ** P3==2 is the recommended pager cache size, and so forth. P1==0 is sl@0: ** the main database file and P1==1 is the database file used to store sl@0: ** temporary tables. sl@0: ** sl@0: ** If P1 is negative, then this is a request to read the size of a sl@0: ** databases free-list. P3 must be set to 1 in this case. The actual sl@0: ** database accessed is ((P1+1)*-1). For example, a P1 parameter of -1 sl@0: ** corresponds to database 0 ("main"), a P1 of -2 is database 1 ("temp"). sl@0: ** sl@0: ** There must be a read-lock on the database (either a transaction sl@0: ** must be started or there must be an open cursor) before sl@0: ** executing this instruction. sl@0: */ sl@0: case OP_ReadCookie: { /* out2-prerelease */ sl@0: int iMeta; sl@0: int iDb = pOp->p1; sl@0: int iCookie = pOp->p3; sl@0: sl@0: assert( pOp->p3=0 && iDbnDb ); sl@0: assert( db->aDb[iDb].pBt!=0 ); sl@0: assert( (p->btreeMask & (1<aDb[iDb].pBt, 1 + iCookie, (u32 *)&iMeta); sl@0: pOut->u.i = iMeta; sl@0: MemSetTypeFlag(pOut, MEM_Int); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: SetCookie P1 P2 P3 * * sl@0: ** sl@0: ** Write the content of register P3 (interpreted as an integer) sl@0: ** into cookie number P2 of database P1. sl@0: ** P2==0 is the schema version. P2==1 is the database format. sl@0: ** P2==2 is the recommended pager cache size, and so forth. P1==0 is sl@0: ** the main database file and P1==1 is the database file used to store sl@0: ** temporary tables. sl@0: ** sl@0: ** A transaction must be started before executing this opcode. sl@0: */ sl@0: case OP_SetCookie: { /* in3 */ sl@0: Db *pDb; sl@0: assert( pOp->p2p1>=0 && pOp->p1nDb ); sl@0: assert( (p->btreeMask & (1<p1))!=0 ); sl@0: pDb = &db->aDb[pOp->p1]; sl@0: assert( pDb->pBt!=0 ); sl@0: sqlite3VdbeMemIntegerify(pIn3); sl@0: /* See note about index shifting on OP_ReadCookie */ sl@0: rc = sqlite3BtreeUpdateMeta(pDb->pBt, 1+pOp->p2, (int)pIn3->u.i); sl@0: if( pOp->p2==0 ){ sl@0: /* When the schema cookie changes, record the new cookie internally */ sl@0: pDb->pSchema->schema_cookie = pIn3->u.i; sl@0: db->flags |= SQLITE_InternChanges; sl@0: }else if( pOp->p2==1 ){ sl@0: /* Record changes in the file format */ sl@0: pDb->pSchema->file_format = pIn3->u.i; sl@0: } sl@0: if( pOp->p1==1 ){ sl@0: /* Invalidate all prepared statements whenever the TEMP database sl@0: ** schema is changed. Ticket #1644 */ sl@0: sqlite3ExpirePreparedStatements(db); sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: VerifyCookie P1 P2 * sl@0: ** sl@0: ** Check the value of global database parameter number 0 (the sl@0: ** schema version) and make sure it is equal to P2. sl@0: ** P1 is the database number which is 0 for the main database file sl@0: ** and 1 for the file holding temporary tables and some higher number sl@0: ** for auxiliary databases. sl@0: ** sl@0: ** The cookie changes its value whenever the database schema changes. sl@0: ** This operation is used to detect when that the cookie has changed sl@0: ** and that the current process needs to reread the schema. sl@0: ** sl@0: ** Either a transaction needs to have been started or an OP_Open needs sl@0: ** to be executed (to establish a read lock) before this opcode is sl@0: ** invoked. sl@0: */ sl@0: case OP_VerifyCookie: { sl@0: int iMeta; sl@0: Btree *pBt; sl@0: assert( pOp->p1>=0 && pOp->p1nDb ); sl@0: assert( (p->btreeMask & (1<p1))!=0 ); sl@0: pBt = db->aDb[pOp->p1].pBt; sl@0: if( pBt ){ sl@0: rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&iMeta); sl@0: }else{ sl@0: rc = SQLITE_OK; sl@0: iMeta = 0; sl@0: } sl@0: if( rc==SQLITE_OK && iMeta!=pOp->p2 ){ sl@0: sqlite3DbFree(db, p->zErrMsg); sl@0: p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); sl@0: /* If the schema-cookie from the database file matches the cookie sl@0: ** stored with the in-memory representation of the schema, do sl@0: ** not reload the schema from the database file. sl@0: ** sl@0: ** If virtual-tables are in use, this is not just an optimization. sl@0: ** Often, v-tables store their data in other SQLite tables, which sl@0: ** are queried from within xNext() and other v-table methods using sl@0: ** prepared queries. If such a query is out-of-date, we do not want to sl@0: ** discard the database schema, as the user code implementing the sl@0: ** v-table would have to be ready for the sqlite3_vtab structure itself sl@0: ** to be invalidated whenever sqlite3_step() is called from within sl@0: ** a v-table method. sl@0: */ sl@0: if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ sl@0: sqlite3ResetInternalSchema(db, pOp->p1); sl@0: } sl@0: sl@0: sqlite3ExpirePreparedStatements(db); sl@0: rc = SQLITE_SCHEMA; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: OpenRead P1 P2 P3 P4 P5 sl@0: ** sl@0: ** Open a read-only cursor for the database table whose root page is sl@0: ** P2 in a database file. The database file is determined by P3. sl@0: ** P3==0 means the main database, P3==1 means the database used for sl@0: ** temporary tables, and P3>1 means used the corresponding attached sl@0: ** database. Give the new cursor an identifier of P1. The P1 sl@0: ** values need not be contiguous but all P1 values should be small integers. sl@0: ** It is an error for P1 to be negative. sl@0: ** sl@0: ** If P5!=0 then use the content of register P2 as the root page, not sl@0: ** the value of P2 itself. sl@0: ** sl@0: ** There will be a read lock on the database whenever there is an sl@0: ** open cursor. If the database was unlocked prior to this instruction sl@0: ** then a read lock is acquired as part of this instruction. A read sl@0: ** lock allows other processes to read the database but prohibits sl@0: ** any other process from modifying the database. The read lock is sl@0: ** released when all cursors are closed. If this instruction attempts sl@0: ** to get a read lock but fails, the script terminates with an sl@0: ** SQLITE_BUSY error code. sl@0: ** sl@0: ** The P4 value is a pointer to a KeyInfo structure that defines the sl@0: ** content and collating sequence of indices. P4 is NULL for cursors sl@0: ** that are not pointing to indices. sl@0: ** sl@0: ** See also OpenWrite. sl@0: */ sl@0: /* Opcode: OpenWrite P1 P2 P3 P4 P5 sl@0: ** sl@0: ** Open a read/write cursor named P1 on the table or index whose root sl@0: ** page is P2. Or if P5!=0 use the content of register P2 to find the sl@0: ** root page. sl@0: ** sl@0: ** The P4 value is a pointer to a KeyInfo structure that defines the sl@0: ** content and collating sequence of indices. P4 is NULL for cursors sl@0: ** that are not pointing to indices. sl@0: ** sl@0: ** This instruction works just like OpenRead except that it opens the cursor sl@0: ** in read/write mode. For a given table, there can be one or more read-only sl@0: ** cursors or a single read/write cursor but not both. sl@0: ** sl@0: ** See also OpenRead. sl@0: */ sl@0: case OP_OpenRead: sl@0: case OP_OpenWrite: { sl@0: int i = pOp->p1; sl@0: int p2 = pOp->p2; sl@0: int iDb = pOp->p3; sl@0: int wrFlag; sl@0: Btree *pX; sl@0: Cursor *pCur; sl@0: Db *pDb; sl@0: sl@0: assert( iDb>=0 && iDbnDb ); sl@0: assert( (p->btreeMask & (1<aDb[iDb]; sl@0: pX = pDb->pBt; sl@0: assert( pX!=0 ); sl@0: if( pOp->opcode==OP_OpenWrite ){ sl@0: wrFlag = 1; sl@0: if( pDb->pSchema->file_format < p->minWriteFileFormat ){ sl@0: p->minWriteFileFormat = pDb->pSchema->file_format; sl@0: } sl@0: }else{ sl@0: wrFlag = 0; sl@0: } sl@0: if( pOp->p5 ){ sl@0: assert( p2>0 ); sl@0: assert( p2<=p->nMem ); sl@0: pIn2 = &p->aMem[p2]; sl@0: sqlite3VdbeMemIntegerify(pIn2); sl@0: p2 = pIn2->u.i; sl@0: assert( p2>=2 ); sl@0: } sl@0: assert( i>=0 ); sl@0: pCur = allocateCursor(p, i, &pOp[-1], iDb, 1); sl@0: if( pCur==0 ) goto no_mem; sl@0: pCur->nullRow = 1; sl@0: rc = sqlite3BtreeCursor(pX, p2, wrFlag, pOp->p4.p, pCur->pCursor); sl@0: if( pOp->p4type==P4_KEYINFO ){ sl@0: pCur->pKeyInfo = pOp->p4.pKeyInfo; sl@0: pCur->pIncrKey = &pCur->pKeyInfo->incrKey; sl@0: pCur->pKeyInfo->enc = ENC(p->db); sl@0: }else{ sl@0: pCur->pKeyInfo = 0; sl@0: pCur->pIncrKey = &pCur->bogusIncrKey; sl@0: } sl@0: switch( rc ){ sl@0: case SQLITE_BUSY: { sl@0: p->pc = pc; sl@0: p->rc = rc = SQLITE_BUSY; sl@0: goto vdbe_return; sl@0: } sl@0: case SQLITE_OK: { sl@0: int flags = sqlite3BtreeFlags(pCur->pCursor); sl@0: /* Sanity checking. Only the lower four bits of the flags byte should sl@0: ** be used. Bit 3 (mask 0x08) is unpredictable. The lower 3 bits sl@0: ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or sl@0: ** 2 (zerodata for indices). If these conditions are not met it can sl@0: ** only mean that we are dealing with a corrupt database file sl@0: */ sl@0: if( (flags & 0xf0)!=0 || ((flags & 0x07)!=5 && (flags & 0x07)!=2) ){ sl@0: rc = SQLITE_CORRUPT_BKPT; sl@0: goto abort_due_to_error; sl@0: } sl@0: pCur->isTable = (flags & BTREE_INTKEY)!=0; sl@0: pCur->isIndex = (flags & BTREE_ZERODATA)!=0; sl@0: /* If P4==0 it means we are expected to open a table. If P4!=0 then sl@0: ** we expect to be opening an index. If this is not what happened, sl@0: ** then the database is corrupt sl@0: */ sl@0: if( (pCur->isTable && pOp->p4type==P4_KEYINFO) sl@0: || (pCur->isIndex && pOp->p4type!=P4_KEYINFO) ){ sl@0: rc = SQLITE_CORRUPT_BKPT; sl@0: goto abort_due_to_error; sl@0: } sl@0: break; sl@0: } sl@0: case SQLITE_EMPTY: { sl@0: pCur->isTable = pOp->p4type!=P4_KEYINFO; sl@0: pCur->isIndex = !pCur->isTable; sl@0: pCur->pCursor = 0; sl@0: rc = SQLITE_OK; sl@0: break; sl@0: } sl@0: default: { sl@0: goto abort_due_to_error; sl@0: } sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: OpenEphemeral P1 P2 * P4 * sl@0: ** sl@0: ** Open a new cursor P1 to a transient table. sl@0: ** The cursor is always opened read/write even if sl@0: ** the main database is read-only. The transient or virtual sl@0: ** table is deleted automatically when the cursor is closed. sl@0: ** sl@0: ** P2 is the number of columns in the virtual table. sl@0: ** The cursor points to a BTree table if P4==0 and to a BTree index sl@0: ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure sl@0: ** that defines the format of keys in the index. sl@0: ** sl@0: ** This opcode was once called OpenTemp. But that created sl@0: ** confusion because the term "temp table", might refer either sl@0: ** to a TEMP table at the SQL level, or to a table opened by sl@0: ** this opcode. Then this opcode was call OpenVirtual. But sl@0: ** that created confusion with the whole virtual-table idea. sl@0: */ sl@0: case OP_OpenEphemeral: { sl@0: int i = pOp->p1; sl@0: Cursor *pCx; sl@0: static const int openFlags = sl@0: SQLITE_OPEN_READWRITE | sl@0: SQLITE_OPEN_CREATE | sl@0: SQLITE_OPEN_EXCLUSIVE | sl@0: SQLITE_OPEN_DELETEONCLOSE | sl@0: SQLITE_OPEN_TRANSIENT_DB; sl@0: sl@0: assert( i>=0 ); sl@0: pCx = allocateCursor(p, i, pOp, -1, 1); sl@0: if( pCx==0 ) goto no_mem; sl@0: pCx->nullRow = 1; sl@0: rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags, sl@0: &pCx->pBt); sl@0: if( rc==SQLITE_OK ){ sl@0: rc = sqlite3BtreeBeginTrans(pCx->pBt, 1); sl@0: } sl@0: if( rc==SQLITE_OK ){ sl@0: /* If a transient index is required, create it by calling sl@0: ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before sl@0: ** opening it. If a transient table is required, just use the sl@0: ** automatically created table with root-page 1 (an INTKEY table). sl@0: */ sl@0: if( pOp->p4.pKeyInfo ){ sl@0: int pgno; sl@0: assert( pOp->p4type==P4_KEYINFO ); sl@0: rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA); sl@0: if( rc==SQLITE_OK ){ sl@0: assert( pgno==MASTER_ROOT+1 ); sl@0: rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, sl@0: (KeyInfo*)pOp->p4.z, pCx->pCursor); sl@0: pCx->pKeyInfo = pOp->p4.pKeyInfo; sl@0: pCx->pKeyInfo->enc = ENC(p->db); sl@0: pCx->pIncrKey = &pCx->pKeyInfo->incrKey; sl@0: } sl@0: pCx->isTable = 0; sl@0: }else{ sl@0: rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor); sl@0: pCx->isTable = 1; sl@0: pCx->pIncrKey = &pCx->bogusIncrKey; sl@0: } sl@0: } sl@0: pCx->isIndex = !pCx->isTable; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: OpenPseudo P1 P2 * * * sl@0: ** sl@0: ** Open a new cursor that points to a fake table that contains a single sl@0: ** row of data. Any attempt to write a second row of data causes the sl@0: ** first row to be deleted. All data is deleted when the cursor is sl@0: ** closed. sl@0: ** sl@0: ** A pseudo-table created by this opcode is useful for holding the sl@0: ** NEW or OLD tables in a trigger. Also used to hold the a single sl@0: ** row output from the sorter so that the row can be decomposed into sl@0: ** individual columns using the OP_Column opcode. sl@0: ** sl@0: ** When OP_Insert is executed to insert a row in to the pseudo table, sl@0: ** the pseudo-table cursor may or may not make it's own copy of the sl@0: ** original row data. If P2 is 0, then the pseudo-table will copy the sl@0: ** original row data. Otherwise, a pointer to the original memory cell sl@0: ** is stored. In this case, the vdbe program must ensure that the sl@0: ** memory cell containing the row data is not overwritten until the sl@0: ** pseudo table is closed (or a new row is inserted into it). sl@0: */ sl@0: case OP_OpenPseudo: { sl@0: int i = pOp->p1; sl@0: Cursor *pCx; sl@0: assert( i>=0 ); sl@0: pCx = allocateCursor(p, i, &pOp[-1], -1, 0); sl@0: if( pCx==0 ) goto no_mem; sl@0: pCx->nullRow = 1; sl@0: pCx->pseudoTable = 1; sl@0: pCx->ephemPseudoTable = pOp->p2; sl@0: pCx->pIncrKey = &pCx->bogusIncrKey; sl@0: pCx->isTable = 1; sl@0: pCx->isIndex = 0; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Close P1 * * * * sl@0: ** sl@0: ** Close a cursor previously opened as P1. If P1 is not sl@0: ** currently open, this instruction is a no-op. sl@0: */ sl@0: case OP_Close: { sl@0: int i = pOp->p1; sl@0: assert( i>=0 && inCursor ); sl@0: sqlite3VdbeFreeCursor(p, p->apCsr[i]); sl@0: p->apCsr[i] = 0; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: MoveGe P1 P2 P3 P4 * sl@0: ** sl@0: ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), sl@0: ** use the integer value in register P3 as a key. If cursor P1 refers sl@0: ** to an SQL index, then P3 is the first in an array of P4 registers sl@0: ** that are used as an unpacked index key. sl@0: ** sl@0: ** Reposition cursor P1 so that it points to the smallest entry that sl@0: ** is greater than or equal to the key value. If there are no records sl@0: ** greater than or equal to the key and P2 is not zero, then jump to P2. sl@0: ** sl@0: ** A special feature of this opcode (and different from the sl@0: ** related OP_MoveGt, OP_MoveLt, and OP_MoveLe) is that if P2 is sl@0: ** zero and P1 is an SQL table (a b-tree with integer keys) then sl@0: ** the seek is deferred until it is actually needed. It might be sl@0: ** the case that the cursor is never accessed. By deferring the sl@0: ** seek, we avoid unnecessary seeks. sl@0: ** sl@0: ** See also: Found, NotFound, Distinct, MoveLt, MoveGt, MoveLe sl@0: */ sl@0: /* Opcode: MoveGt P1 P2 P3 P4 * sl@0: ** sl@0: ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), sl@0: ** use the integer value in register P3 as a key. If cursor P1 refers sl@0: ** to an SQL index, then P3 is the first in an array of P4 registers sl@0: ** that are used as an unpacked index key. sl@0: ** sl@0: ** Reposition cursor P1 so that it points to the smallest entry that sl@0: ** is greater than the key value. If there are no records greater than sl@0: ** the key and P2 is not zero, then jump to P2. sl@0: ** sl@0: ** See also: Found, NotFound, Distinct, MoveLt, MoveGe, MoveLe sl@0: */ sl@0: /* Opcode: MoveLt P1 P2 P3 P4 * sl@0: ** sl@0: ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), sl@0: ** use the integer value in register P3 as a key. If cursor P1 refers sl@0: ** to an SQL index, then P3 is the first in an array of P4 registers sl@0: ** that are used as an unpacked index key. sl@0: ** sl@0: ** Reposition cursor P1 so that it points to the largest entry that sl@0: ** is less than the key value. If there are no records less than sl@0: ** the key and P2 is not zero, then jump to P2. sl@0: ** sl@0: ** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLe sl@0: */ sl@0: /* Opcode: MoveLe P1 P2 P3 P4 * sl@0: ** sl@0: ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), sl@0: ** use the integer value in register P3 as a key. If cursor P1 refers sl@0: ** to an SQL index, then P3 is the first in an array of P4 registers sl@0: ** that are used as an unpacked index key. sl@0: ** sl@0: ** Reposition cursor P1 so that it points to the largest entry that sl@0: ** is less than or equal to the key value. If there are no records sl@0: ** less than or equal to the key and P2 is not zero, then jump to P2. sl@0: ** sl@0: ** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLt sl@0: */ sl@0: case OP_MoveLt: /* jump, in3 */ sl@0: case OP_MoveLe: /* jump, in3 */ sl@0: case OP_MoveGe: /* jump, in3 */ sl@0: case OP_MoveGt: { /* jump, in3 */ sl@0: int i = pOp->p1; sl@0: Cursor *pC; sl@0: sl@0: assert( i>=0 && inCursor ); sl@0: pC = p->apCsr[i]; sl@0: assert( pC!=0 ); sl@0: if( pC->pCursor!=0 ){ sl@0: int res, oc; sl@0: oc = pOp->opcode; sl@0: pC->nullRow = 0; sl@0: *pC->pIncrKey = oc==OP_MoveGt || oc==OP_MoveLe; sl@0: if( pC->isTable ){ sl@0: i64 iKey = sqlite3VdbeIntValue(pIn3); sl@0: if( pOp->p2==0 ){ sl@0: assert( pOp->opcode==OP_MoveGe ); sl@0: pC->movetoTarget = iKey; sl@0: pC->rowidIsValid = 0; sl@0: pC->deferredMoveto = 1; sl@0: break; sl@0: } sl@0: rc = sqlite3BtreeMoveto(pC->pCursor, 0, 0, (u64)iKey, 0, &res); sl@0: if( rc!=SQLITE_OK ){ sl@0: goto abort_due_to_error; sl@0: } sl@0: pC->lastRowid = iKey; sl@0: pC->rowidIsValid = res==0; sl@0: }else{ sl@0: UnpackedRecord r; sl@0: int nField = pOp->p4.i; sl@0: assert( pOp->p4type==P4_INT32 ); sl@0: assert( nField>0 ); sl@0: r.pKeyInfo = pC->pKeyInfo; sl@0: r.nField = nField; sl@0: r.needFree = 0; sl@0: r.needDestroy = 0; sl@0: r.aMem = &p->aMem[pOp->p3]; sl@0: rc = sqlite3BtreeMoveto(pC->pCursor, 0, &r, 0, 0, &res); sl@0: if( rc!=SQLITE_OK ){ sl@0: goto abort_due_to_error; sl@0: } sl@0: pC->rowidIsValid = 0; sl@0: } sl@0: pC->deferredMoveto = 0; sl@0: pC->cacheStatus = CACHE_STALE; sl@0: *pC->pIncrKey = 0; sl@0: #ifdef SQLITE_TEST sl@0: sqlite3_search_count++; sl@0: #endif sl@0: if( oc==OP_MoveGe || oc==OP_MoveGt ){ sl@0: if( res<0 ){ sl@0: rc = sqlite3BtreeNext(pC->pCursor, &res); sl@0: if( rc!=SQLITE_OK ) goto abort_due_to_error; sl@0: pC->rowidIsValid = 0; sl@0: }else{ sl@0: res = 0; sl@0: } sl@0: }else{ sl@0: assert( oc==OP_MoveLt || oc==OP_MoveLe ); sl@0: if( res>=0 ){ sl@0: rc = sqlite3BtreePrevious(pC->pCursor, &res); sl@0: if( rc!=SQLITE_OK ) goto abort_due_to_error; sl@0: pC->rowidIsValid = 0; sl@0: }else{ sl@0: /* res might be negative because the table is empty. Check to sl@0: ** see if this is the case. sl@0: */ sl@0: res = sqlite3BtreeEof(pC->pCursor); sl@0: } sl@0: } sl@0: assert( pOp->p2>0 ); sl@0: if( res ){ sl@0: pc = pOp->p2 - 1; sl@0: } sl@0: }else if( !pC->pseudoTable ){ sl@0: /* This happens when attempting to open the sqlite3_master table sl@0: ** for read access returns SQLITE_EMPTY. In this case always sl@0: ** take the jump (since there are no records in the table). sl@0: */ sl@0: pc = pOp->p2 - 1; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Found P1 P2 P3 * * sl@0: ** sl@0: ** Register P3 holds a blob constructed by MakeRecord. P1 is an index. sl@0: ** If an entry that matches the value in register p3 exists in P1 then sl@0: ** jump to P2. If the P3 value does not match any entry in P1 sl@0: ** then fall thru. The P1 cursor is left pointing at the matching entry sl@0: ** if it exists. sl@0: ** sl@0: ** This instruction is used to implement the IN operator where the sl@0: ** left-hand side is a SELECT statement. P1 may be a true index, or it sl@0: ** may be a temporary index that holds the results of the SELECT sl@0: ** statement. This instruction is also used to implement the sl@0: ** DISTINCT keyword in SELECT statements. sl@0: ** sl@0: ** This instruction checks if index P1 contains a record for which sl@0: ** the first N serialized values exactly match the N serialized values sl@0: ** in the record in register P3, where N is the total number of values in sl@0: ** the P3 record (the P3 record is a prefix of the P1 record). sl@0: ** sl@0: ** See also: NotFound, MoveTo, IsUnique, NotExists sl@0: */ sl@0: /* Opcode: NotFound P1 P2 P3 * * sl@0: ** sl@0: ** Register P3 holds a blob constructed by MakeRecord. P1 is sl@0: ** an index. If no entry exists in P1 that matches the blob then jump sl@0: ** to P2. If an entry does existing, fall through. The cursor is left sl@0: ** pointing to the entry that matches. sl@0: ** sl@0: ** See also: Found, MoveTo, NotExists, IsUnique sl@0: */ sl@0: case OP_NotFound: /* jump, in3 */ sl@0: case OP_Found: { /* jump, in3 */ sl@0: int i = pOp->p1; sl@0: int alreadyExists = 0; sl@0: Cursor *pC; sl@0: assert( i>=0 && inCursor ); sl@0: assert( p->apCsr[i]!=0 ); sl@0: if( (pC = p->apCsr[i])->pCursor!=0 ){ sl@0: int res; sl@0: assert( pC->isTable==0 ); sl@0: assert( pIn3->flags & MEM_Blob ); sl@0: if( pOp->opcode==OP_Found ){ sl@0: pC->pKeyInfo->prefixIsEqual = 1; sl@0: } sl@0: rc = sqlite3BtreeMoveto(pC->pCursor, pIn3->z, 0, pIn3->n, 0, &res); sl@0: pC->pKeyInfo->prefixIsEqual = 0; sl@0: if( rc!=SQLITE_OK ){ sl@0: break; sl@0: } sl@0: alreadyExists = (res==0); sl@0: pC->deferredMoveto = 0; sl@0: pC->cacheStatus = CACHE_STALE; sl@0: } sl@0: if( pOp->opcode==OP_Found ){ sl@0: if( alreadyExists ) pc = pOp->p2 - 1; sl@0: }else{ sl@0: if( !alreadyExists ) pc = pOp->p2 - 1; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: IsUnique P1 P2 P3 P4 * sl@0: ** sl@0: ** The P3 register contains an integer record number. Call this sl@0: ** record number R. The P4 register contains an index key created sl@0: ** using MakeIdxRec. Call it K. sl@0: ** sl@0: ** P1 is an index. So it has no data and its key consists of a sl@0: ** record generated by OP_MakeRecord where the last field is the sl@0: ** rowid of the entry that the index refers to. sl@0: ** sl@0: ** This instruction asks if there is an entry in P1 where the sl@0: ** fields matches K but the rowid is different from R. sl@0: ** If there is no such entry, then there is an immediate sl@0: ** jump to P2. If any entry does exist where the index string sl@0: ** matches K but the record number is not R, then the record sl@0: ** number for that entry is written into P3 and control sl@0: ** falls through to the next instruction. sl@0: ** sl@0: ** See also: NotFound, NotExists, Found sl@0: */ sl@0: case OP_IsUnique: { /* jump, in3 */ sl@0: int i = pOp->p1; sl@0: Cursor *pCx; sl@0: BtCursor *pCrsr; sl@0: Mem *pK; sl@0: i64 R; sl@0: sl@0: /* Pop the value R off the top of the stack sl@0: */ sl@0: assert( pOp->p4type==P4_INT32 ); sl@0: assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem ); sl@0: pK = &p->aMem[pOp->p4.i]; sl@0: sqlite3VdbeMemIntegerify(pIn3); sl@0: R = pIn3->u.i; sl@0: assert( i>=0 && inCursor ); sl@0: pCx = p->apCsr[i]; sl@0: assert( pCx!=0 ); sl@0: pCrsr = pCx->pCursor; sl@0: if( pCrsr!=0 ){ sl@0: int res; sl@0: i64 v; /* The record number on the P1 entry that matches K */ sl@0: char *zKey; /* The value of K */ sl@0: int nKey; /* Number of bytes in K */ sl@0: int len; /* Number of bytes in K without the rowid at the end */ sl@0: int szRowid; /* Size of the rowid column at the end of zKey */ sl@0: sl@0: /* Make sure K is a string and make zKey point to K sl@0: */ sl@0: assert( pK->flags & MEM_Blob ); sl@0: zKey = pK->z; sl@0: nKey = pK->n; sl@0: sl@0: /* sqlite3VdbeIdxRowidLen() only returns other than SQLITE_OK when the sl@0: ** record passed as an argument corrupt. Since the record in this case sl@0: ** has just been created by an OP_MakeRecord instruction, and not loaded sl@0: ** from the database file, it is not possible for it to be corrupt. sl@0: ** Therefore, assert(rc==SQLITE_OK). sl@0: */ sl@0: rc = sqlite3VdbeIdxRowidLen((u8*)zKey, nKey, &szRowid); sl@0: assert(rc==SQLITE_OK); sl@0: len = nKey-szRowid; sl@0: sl@0: /* Search for an entry in P1 where all but the last four bytes match K. sl@0: ** If there is no such entry, jump immediately to P2. sl@0: */ sl@0: assert( pCx->deferredMoveto==0 ); sl@0: pCx->cacheStatus = CACHE_STALE; sl@0: rc = sqlite3BtreeMoveto(pCrsr, zKey, 0, len, 0, &res); sl@0: if( rc!=SQLITE_OK ){ sl@0: goto abort_due_to_error; sl@0: } sl@0: if( res<0 ){ sl@0: rc = sqlite3BtreeNext(pCrsr, &res); sl@0: if( res ){ sl@0: pc = pOp->p2 - 1; sl@0: break; sl@0: } sl@0: } sl@0: rc = sqlite3VdbeIdxKeyCompare(pCx, 0, len, (u8*)zKey, &res); sl@0: if( rc!=SQLITE_OK ) goto abort_due_to_error; sl@0: if( res>0 ){ sl@0: pc = pOp->p2 - 1; sl@0: break; sl@0: } sl@0: sl@0: /* At this point, pCrsr is pointing to an entry in P1 where all but sl@0: ** the final entry (the rowid) matches K. Check to see if the sl@0: ** final rowid column is different from R. If it equals R then jump sl@0: ** immediately to P2. sl@0: */ sl@0: rc = sqlite3VdbeIdxRowid(pCrsr, &v); sl@0: if( rc!=SQLITE_OK ){ sl@0: goto abort_due_to_error; sl@0: } sl@0: if( v==R ){ sl@0: pc = pOp->p2 - 1; sl@0: break; sl@0: } sl@0: sl@0: /* The final varint of the key is different from R. Store it back sl@0: ** into register R3. (The record number of an entry that violates sl@0: ** a UNIQUE constraint.) sl@0: */ sl@0: pIn3->u.i = v; sl@0: assert( pIn3->flags&MEM_Int ); sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: NotExists P1 P2 P3 * * sl@0: ** sl@0: ** Use the content of register P3 as a integer key. If a record sl@0: ** with that key does not exist in table of P1, then jump to P2. sl@0: ** If the record does exist, then fall thru. The cursor is left sl@0: ** pointing to the record if it exists. sl@0: ** sl@0: ** The difference between this operation and NotFound is that this sl@0: ** operation assumes the key is an integer and that P1 is a table whereas sl@0: ** NotFound assumes key is a blob constructed from MakeRecord and sl@0: ** P1 is an index. sl@0: ** sl@0: ** See also: Found, MoveTo, NotFound, IsUnique sl@0: */ sl@0: case OP_NotExists: { /* jump, in3 */ sl@0: int i = pOp->p1; sl@0: Cursor *pC; sl@0: BtCursor *pCrsr; sl@0: assert( i>=0 && inCursor ); sl@0: assert( p->apCsr[i]!=0 ); sl@0: if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ sl@0: int res; sl@0: u64 iKey; sl@0: assert( pIn3->flags & MEM_Int ); sl@0: assert( p->apCsr[i]->isTable ); sl@0: iKey = intToKey(pIn3->u.i); sl@0: rc = sqlite3BtreeMoveto(pCrsr, 0, 0, iKey, 0,&res); sl@0: pC->lastRowid = pIn3->u.i; sl@0: pC->rowidIsValid = res==0; sl@0: pC->nullRow = 0; sl@0: pC->cacheStatus = CACHE_STALE; sl@0: /* res might be uninitialized if rc!=SQLITE_OK. But if rc!=SQLITE_OK sl@0: ** processing is about to abort so we really do not care whether or not sl@0: ** the following jump is taken. (In other words, do not stress over sl@0: ** the error that valgrind sometimes shows on the next statement when sl@0: ** running ioerr.test and similar failure-recovery test scripts.) */ sl@0: if( res!=0 ){ sl@0: pc = pOp->p2 - 1; sl@0: assert( pC->rowidIsValid==0 ); sl@0: } sl@0: }else if( !pC->pseudoTable ){ sl@0: /* This happens when an attempt to open a read cursor on the sl@0: ** sqlite_master table returns SQLITE_EMPTY. sl@0: */ sl@0: assert( pC->isTable ); sl@0: pc = pOp->p2 - 1; sl@0: assert( pC->rowidIsValid==0 ); sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Sequence P1 P2 * * * sl@0: ** sl@0: ** Find the next available sequence number for cursor P1. sl@0: ** Write the sequence number into register P2. sl@0: ** The sequence number on the cursor is incremented after this sl@0: ** instruction. sl@0: */ sl@0: case OP_Sequence: { /* out2-prerelease */ sl@0: int i = pOp->p1; sl@0: assert( i>=0 && inCursor ); sl@0: assert( p->apCsr[i]!=0 ); sl@0: pOut->u.i = p->apCsr[i]->seqCount++; sl@0: MemSetTypeFlag(pOut, MEM_Int); sl@0: break; sl@0: } sl@0: sl@0: sl@0: /* Opcode: NewRowid P1 P2 P3 * * sl@0: ** sl@0: ** Get a new integer record number (a.k.a "rowid") used as the key to a table. sl@0: ** The record number is not previously used as a key in the database sl@0: ** table that cursor P1 points to. The new record number is written sl@0: ** written to register P2. sl@0: ** sl@0: ** If P3>0 then P3 is a register that holds the largest previously sl@0: ** generated record number. No new record numbers are allowed to be less sl@0: ** than this value. When this value reaches its maximum, a SQLITE_FULL sl@0: ** error is generated. The P3 register is updated with the generated sl@0: ** record number. This P3 mechanism is used to help implement the sl@0: ** AUTOINCREMENT feature. sl@0: */ sl@0: case OP_NewRowid: { /* out2-prerelease */ sl@0: int i = pOp->p1; sl@0: i64 v = 0; sl@0: Cursor *pC; sl@0: assert( i>=0 && inCursor ); sl@0: assert( p->apCsr[i]!=0 ); sl@0: if( (pC = p->apCsr[i])->pCursor==0 ){ sl@0: /* The zero initialization above is all that is needed */ sl@0: }else{ sl@0: /* The next rowid or record number (different terms for the same sl@0: ** thing) is obtained in a two-step algorithm. sl@0: ** sl@0: ** First we attempt to find the largest existing rowid and add one sl@0: ** to that. But if the largest existing rowid is already the maximum sl@0: ** positive integer, we have to fall through to the second sl@0: ** probabilistic algorithm sl@0: ** sl@0: ** The second algorithm is to select a rowid at random and see if sl@0: ** it already exists in the table. If it does not exist, we have sl@0: ** succeeded. If the random rowid does exist, we select a new one sl@0: ** and try again, up to 1000 times. sl@0: ** sl@0: ** For a table with less than 2 billion entries, the probability sl@0: ** of not finding a unused rowid is about 1.0e-300. This is a sl@0: ** non-zero probability, but it is still vanishingly small and should sl@0: ** never cause a problem. You are much, much more likely to have a sl@0: ** hardware failure than for this algorithm to fail. sl@0: ** sl@0: ** The analysis in the previous paragraph assumes that you have a good sl@0: ** source of random numbers. Is a library function like lrand48() sl@0: ** good enough? Maybe. Maybe not. It's hard to know whether there sl@0: ** might be subtle bugs is some implementations of lrand48() that sl@0: ** could cause problems. To avoid uncertainty, SQLite uses its own sl@0: ** random number generator based on the RC4 algorithm. sl@0: ** sl@0: ** To promote locality of reference for repetitive inserts, the sl@0: ** first few attempts at choosing a random rowid pick values just a little sl@0: ** larger than the previous rowid. This has been shown experimentally sl@0: ** to double the speed of the COPY operation. sl@0: */ sl@0: int res, rx=SQLITE_OK, cnt; sl@0: i64 x; sl@0: cnt = 0; sl@0: if( (sqlite3BtreeFlags(pC->pCursor)&(BTREE_INTKEY|BTREE_ZERODATA)) != sl@0: BTREE_INTKEY ){ sl@0: rc = SQLITE_CORRUPT_BKPT; sl@0: goto abort_due_to_error; sl@0: } sl@0: assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 ); sl@0: assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 ); sl@0: sl@0: #ifdef SQLITE_32BIT_ROWID sl@0: # define MAX_ROWID 0x7fffffff sl@0: #else sl@0: /* Some compilers complain about constants of the form 0x7fffffffffffffff. sl@0: ** Others complain about 0x7ffffffffffffffffLL. The following macro seems sl@0: ** to provide the constant while making all compilers happy. sl@0: */ sl@0: # define MAX_ROWID ( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) sl@0: #endif sl@0: sl@0: if( !pC->useRandomRowid ){ sl@0: if( pC->nextRowidValid ){ sl@0: v = pC->nextRowid; sl@0: }else{ sl@0: rc = sqlite3BtreeLast(pC->pCursor, &res); sl@0: if( rc!=SQLITE_OK ){ sl@0: goto abort_due_to_error; sl@0: } sl@0: if( res ){ sl@0: v = 1; sl@0: }else{ sl@0: sqlite3BtreeKeySize(pC->pCursor, &v); sl@0: v = keyToInt(v); sl@0: if( v==MAX_ROWID ){ sl@0: pC->useRandomRowid = 1; sl@0: }else{ sl@0: v++; sl@0: } sl@0: } sl@0: } sl@0: sl@0: #ifndef SQLITE_OMIT_AUTOINCREMENT sl@0: if( pOp->p3 ){ sl@0: Mem *pMem; sl@0: assert( pOp->p3>0 && pOp->p3<=p->nMem ); /* P3 is a valid memory cell */ sl@0: pMem = &p->aMem[pOp->p3]; sl@0: REGISTER_TRACE(pOp->p3, pMem); sl@0: sqlite3VdbeMemIntegerify(pMem); sl@0: assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ sl@0: if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ sl@0: rc = SQLITE_FULL; sl@0: goto abort_due_to_error; sl@0: } sl@0: if( vu.i+1 ){ sl@0: v = pMem->u.i + 1; sl@0: } sl@0: pMem->u.i = v; sl@0: } sl@0: #endif sl@0: sl@0: if( vnextRowidValid = 1; sl@0: pC->nextRowid = v+1; sl@0: }else{ sl@0: pC->nextRowidValid = 0; sl@0: } sl@0: } sl@0: if( pC->useRandomRowid ){ sl@0: assert( pOp->p3==0 ); /* SQLITE_FULL must have occurred prior to this */ sl@0: v = db->priorNewRowid; sl@0: cnt = 0; sl@0: do{ sl@0: if( cnt==0 && (v&0xffffff)==v ){ sl@0: v++; sl@0: }else{ sl@0: sqlite3_randomness(sizeof(v), &v); sl@0: if( cnt<5 ) v &= 0xffffff; sl@0: } sl@0: if( v==0 ) continue; sl@0: x = intToKey(v); sl@0: rx = sqlite3BtreeMoveto(pC->pCursor, 0, 0, (u64)x, 0, &res); sl@0: cnt++; sl@0: }while( cnt<100 && rx==SQLITE_OK && res==0 ); sl@0: db->priorNewRowid = v; sl@0: if( rx==SQLITE_OK && res==0 ){ sl@0: rc = SQLITE_FULL; sl@0: goto abort_due_to_error; sl@0: } sl@0: } sl@0: pC->rowidIsValid = 0; sl@0: pC->deferredMoveto = 0; sl@0: pC->cacheStatus = CACHE_STALE; sl@0: } sl@0: MemSetTypeFlag(pOut, MEM_Int); sl@0: pOut->u.i = v; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Insert P1 P2 P3 P4 P5 sl@0: ** sl@0: ** Write an entry into the table of cursor P1. A new entry is sl@0: ** created if it doesn't already exist or the data for an existing sl@0: ** entry is overwritten. The data is the value stored register sl@0: ** number P2. The key is stored in register P3. The key must sl@0: ** be an integer. sl@0: ** sl@0: ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is sl@0: ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, sl@0: ** then rowid is stored for subsequent return by the sl@0: ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). sl@0: ** sl@0: ** Parameter P4 may point to a string containing the table-name, or sl@0: ** may be NULL. If it is not NULL, then the update-hook sl@0: ** (sqlite3.xUpdateCallback) is invoked following a successful insert. sl@0: ** sl@0: ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically sl@0: ** allocated, then ownership of P2 is transferred to the pseudo-cursor sl@0: ** and register P2 becomes ephemeral. If the cursor is changed, the sl@0: ** value of register P2 will then change. Make sure this does not sl@0: ** cause any problems.) sl@0: ** sl@0: ** This instruction only works on tables. The equivalent instruction sl@0: ** for indices is OP_IdxInsert. sl@0: */ sl@0: case OP_Insert: { sl@0: Mem *pData = &p->aMem[pOp->p2]; sl@0: Mem *pKey = &p->aMem[pOp->p3]; sl@0: sl@0: i64 iKey; /* The integer ROWID or key for the record to be inserted */ sl@0: int i = pOp->p1; sl@0: Cursor *pC; sl@0: assert( i>=0 && inCursor ); sl@0: pC = p->apCsr[i]; sl@0: assert( pC!=0 ); sl@0: assert( pC->pCursor!=0 || pC->pseudoTable ); sl@0: assert( pKey->flags & MEM_Int ); sl@0: assert( pC->isTable ); sl@0: REGISTER_TRACE(pOp->p2, pData); sl@0: REGISTER_TRACE(pOp->p3, pKey); sl@0: sl@0: iKey = intToKey(pKey->u.i); sl@0: if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; sl@0: if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = pKey->u.i; sl@0: if( pC->nextRowidValid && pKey->u.i>=pC->nextRowid ){ sl@0: pC->nextRowidValid = 0; sl@0: } sl@0: if( pData->flags & MEM_Null ){ sl@0: pData->z = 0; sl@0: pData->n = 0; sl@0: }else{ sl@0: assert( pData->flags & (MEM_Blob|MEM_Str) ); sl@0: } sl@0: if( pC->pseudoTable ){ sl@0: if( !pC->ephemPseudoTable ){ sl@0: sqlite3DbFree(db, pC->pData); sl@0: } sl@0: pC->iKey = iKey; sl@0: pC->nData = pData->n; sl@0: if( pData->z==pData->zMalloc || pC->ephemPseudoTable ){ sl@0: pC->pData = pData->z; sl@0: if( !pC->ephemPseudoTable ){ sl@0: pData->flags &= ~MEM_Dyn; sl@0: pData->flags |= MEM_Ephem; sl@0: pData->zMalloc = 0; sl@0: } sl@0: }else{ sl@0: pC->pData = sqlite3Malloc( pC->nData+2 ); sl@0: if( !pC->pData ) goto no_mem; sl@0: memcpy(pC->pData, pData->z, pC->nData); sl@0: pC->pData[pC->nData] = 0; sl@0: pC->pData[pC->nData+1] = 0; sl@0: } sl@0: pC->nullRow = 0; sl@0: }else{ sl@0: int nZero; sl@0: if( pData->flags & MEM_Zero ){ sl@0: nZero = pData->u.i; sl@0: }else{ sl@0: nZero = 0; sl@0: } sl@0: rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey, sl@0: pData->z, pData->n, nZero, sl@0: pOp->p5 & OPFLAG_APPEND); sl@0: } sl@0: sl@0: pC->rowidIsValid = 0; sl@0: pC->deferredMoveto = 0; sl@0: pC->cacheStatus = CACHE_STALE; sl@0: sl@0: /* Invoke the update-hook if required. */ sl@0: if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ sl@0: const char *zDb = db->aDb[pC->iDb].zName; sl@0: const char *zTbl = pOp->p4.z; sl@0: int op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); sl@0: assert( pC->isTable ); sl@0: db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey); sl@0: assert( pC->iDb>=0 ); sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Delete P1 P2 * P4 * sl@0: ** sl@0: ** Delete the record at which the P1 cursor is currently pointing. sl@0: ** sl@0: ** The cursor will be left pointing at either the next or the previous sl@0: ** record in the table. If it is left pointing at the next record, then sl@0: ** the next Next instruction will be a no-op. Hence it is OK to delete sl@0: ** a record from within an Next loop. sl@0: ** sl@0: ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is sl@0: ** incremented (otherwise not). sl@0: ** sl@0: ** P1 must not be pseudo-table. It has to be a real table with sl@0: ** multiple rows. sl@0: ** sl@0: ** If P4 is not NULL, then it is the name of the table that P1 is sl@0: ** pointing to. The update hook will be invoked, if it exists. sl@0: ** If P4 is not NULL then the P1 cursor must have been positioned sl@0: ** using OP_NotFound prior to invoking this opcode. sl@0: */ sl@0: case OP_Delete: { sl@0: int i = pOp->p1; sl@0: i64 iKey = 0; sl@0: Cursor *pC; sl@0: sl@0: assert( i>=0 && inCursor ); sl@0: pC = p->apCsr[i]; sl@0: assert( pC!=0 ); sl@0: assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */ sl@0: sl@0: /* If the update-hook will be invoked, set iKey to the rowid of the sl@0: ** row being deleted. sl@0: */ sl@0: if( db->xUpdateCallback && pOp->p4.z ){ sl@0: assert( pC->isTable ); sl@0: assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */ sl@0: iKey = pC->lastRowid; sl@0: } sl@0: sl@0: rc = sqlite3VdbeCursorMoveto(pC); sl@0: if( rc ) goto abort_due_to_error; sl@0: rc = sqlite3BtreeDelete(pC->pCursor); sl@0: pC->nextRowidValid = 0; sl@0: pC->cacheStatus = CACHE_STALE; sl@0: sl@0: /* Invoke the update-hook if required. */ sl@0: if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ sl@0: const char *zDb = db->aDb[pC->iDb].zName; sl@0: const char *zTbl = pOp->p4.z; sl@0: db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey); sl@0: assert( pC->iDb>=0 ); sl@0: } sl@0: if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: ResetCount P1 * * sl@0: ** sl@0: ** This opcode resets the VMs internal change counter to 0. If P1 is true, sl@0: ** then the value of the change counter is copied to the database handle sl@0: ** change counter (returned by subsequent calls to sqlite3_changes()) sl@0: ** before it is reset. This is used by trigger programs. sl@0: */ sl@0: case OP_ResetCount: { sl@0: if( pOp->p1 ){ sl@0: sqlite3VdbeSetChanges(db, p->nChange); sl@0: } sl@0: p->nChange = 0; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: RowData P1 P2 * * * sl@0: ** sl@0: ** Write into register P2 the complete row data for cursor P1. sl@0: ** There is no interpretation of the data. sl@0: ** It is just copied onto the P2 register exactly as sl@0: ** it is found in the database file. sl@0: ** sl@0: ** If the P1 cursor must be pointing to a valid row (not a NULL row) sl@0: ** of a real table, not a pseudo-table. sl@0: */ sl@0: /* Opcode: RowKey P1 P2 * * * sl@0: ** sl@0: ** Write into register P2 the complete row key for cursor P1. sl@0: ** There is no interpretation of the data. sl@0: ** The key is copied onto the P3 register exactly as sl@0: ** it is found in the database file. sl@0: ** sl@0: ** If the P1 cursor must be pointing to a valid row (not a NULL row) sl@0: ** of a real table, not a pseudo-table. sl@0: */ sl@0: case OP_RowKey: sl@0: case OP_RowData: { sl@0: int i = pOp->p1; sl@0: Cursor *pC; sl@0: BtCursor *pCrsr; sl@0: u32 n; sl@0: sl@0: pOut = &p->aMem[pOp->p2]; sl@0: sl@0: /* Note that RowKey and RowData are really exactly the same instruction */ sl@0: assert( i>=0 && inCursor ); sl@0: pC = p->apCsr[i]; sl@0: assert( pC->isTable || pOp->opcode==OP_RowKey ); sl@0: assert( pC->isIndex || pOp->opcode==OP_RowData ); sl@0: assert( pC!=0 ); sl@0: assert( pC->nullRow==0 ); sl@0: assert( pC->pseudoTable==0 ); sl@0: assert( pC->pCursor!=0 ); sl@0: pCrsr = pC->pCursor; sl@0: rc = sqlite3VdbeCursorMoveto(pC); sl@0: if( rc ) goto abort_due_to_error; sl@0: if( pC->isIndex ){ sl@0: i64 n64; sl@0: assert( !pC->isTable ); sl@0: sqlite3BtreeKeySize(pCrsr, &n64); sl@0: if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){ sl@0: goto too_big; sl@0: } sl@0: n = n64; sl@0: }else{ sl@0: sqlite3BtreeDataSize(pCrsr, &n); sl@0: if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){ sl@0: goto too_big; sl@0: } sl@0: } sl@0: if( sqlite3VdbeMemGrow(pOut, n, 0) ){ sl@0: goto no_mem; sl@0: } sl@0: pOut->n = n; sl@0: MemSetTypeFlag(pOut, MEM_Blob); sl@0: if( pC->isIndex ){ sl@0: rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z); sl@0: }else{ sl@0: rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z); sl@0: } sl@0: pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ sl@0: UPDATE_MAX_BLOBSIZE(pOut); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Rowid P1 P2 * * * sl@0: ** sl@0: ** Store in register P2 an integer which is the key of the table entry that sl@0: ** P1 is currently point to. sl@0: */ sl@0: case OP_Rowid: { /* out2-prerelease */ sl@0: int i = pOp->p1; sl@0: Cursor *pC; sl@0: i64 v; sl@0: sl@0: assert( i>=0 && inCursor ); sl@0: pC = p->apCsr[i]; sl@0: assert( pC!=0 ); sl@0: rc = sqlite3VdbeCursorMoveto(pC); sl@0: if( rc ) goto abort_due_to_error; sl@0: if( pC->rowidIsValid ){ sl@0: v = pC->lastRowid; sl@0: }else if( pC->pseudoTable ){ sl@0: v = keyToInt(pC->iKey); sl@0: }else if( pC->nullRow ){ sl@0: /* Leave the rowid set to a NULL */ sl@0: break; sl@0: }else{ sl@0: assert( pC->pCursor!=0 ); sl@0: sqlite3BtreeKeySize(pC->pCursor, &v); sl@0: v = keyToInt(v); sl@0: } sl@0: pOut->u.i = v; sl@0: MemSetTypeFlag(pOut, MEM_Int); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: NullRow P1 * * * * sl@0: ** sl@0: ** Move the cursor P1 to a null row. Any OP_Column operations sl@0: ** that occur while the cursor is on the null row will always sl@0: ** write a NULL. sl@0: */ sl@0: case OP_NullRow: { sl@0: int i = pOp->p1; sl@0: Cursor *pC; sl@0: sl@0: assert( i>=0 && inCursor ); sl@0: pC = p->apCsr[i]; sl@0: assert( pC!=0 ); sl@0: pC->nullRow = 1; sl@0: pC->rowidIsValid = 0; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Last P1 P2 * * * sl@0: ** sl@0: ** The next use of the Rowid or Column or Next instruction for P1 sl@0: ** will refer to the last entry in the database table or index. sl@0: ** If the table or index is empty and P2>0, then jump immediately to P2. sl@0: ** If P2 is 0 or if the table or index is not empty, fall through sl@0: ** to the following instruction. sl@0: */ sl@0: case OP_Last: { /* jump */ sl@0: int i = pOp->p1; sl@0: Cursor *pC; sl@0: BtCursor *pCrsr; sl@0: int res; sl@0: sl@0: assert( i>=0 && inCursor ); sl@0: pC = p->apCsr[i]; sl@0: assert( pC!=0 ); sl@0: pCrsr = pC->pCursor; sl@0: assert( pCrsr!=0 ); sl@0: rc = sqlite3BtreeLast(pCrsr, &res); sl@0: pC->nullRow = res; sl@0: pC->deferredMoveto = 0; sl@0: pC->cacheStatus = CACHE_STALE; sl@0: if( res && pOp->p2>0 ){ sl@0: pc = pOp->p2 - 1; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: sl@0: /* Opcode: Sort P1 P2 * * * sl@0: ** sl@0: ** This opcode does exactly the same thing as OP_Rewind except that sl@0: ** it increments an undocumented global variable used for testing. sl@0: ** sl@0: ** Sorting is accomplished by writing records into a sorting index, sl@0: ** then rewinding that index and playing it back from beginning to sl@0: ** end. We use the OP_Sort opcode instead of OP_Rewind to do the sl@0: ** rewinding so that the global variable will be incremented and sl@0: ** regression tests can determine whether or not the optimizer is sl@0: ** correctly optimizing out sorts. sl@0: */ sl@0: case OP_Sort: { /* jump */ sl@0: #ifdef SQLITE_TEST sl@0: sqlite3_sort_count++; sl@0: sqlite3_search_count--; sl@0: #endif sl@0: /* Fall through into OP_Rewind */ sl@0: } sl@0: /* Opcode: Rewind P1 P2 * * * sl@0: ** sl@0: ** The next use of the Rowid or Column or Next instruction for P1 sl@0: ** will refer to the first entry in the database table or index. sl@0: ** If the table or index is empty and P2>0, then jump immediately to P2. sl@0: ** If P2 is 0 or if the table or index is not empty, fall through sl@0: ** to the following instruction. sl@0: */ sl@0: case OP_Rewind: { /* jump */ sl@0: int i = pOp->p1; sl@0: Cursor *pC; sl@0: BtCursor *pCrsr; sl@0: int res; sl@0: sl@0: assert( i>=0 && inCursor ); sl@0: pC = p->apCsr[i]; sl@0: assert( pC!=0 ); sl@0: if( (pCrsr = pC->pCursor)!=0 ){ sl@0: rc = sqlite3BtreeFirst(pCrsr, &res); sl@0: pC->atFirst = res==0; sl@0: pC->deferredMoveto = 0; sl@0: pC->cacheStatus = CACHE_STALE; sl@0: }else{ sl@0: res = 1; sl@0: } sl@0: pC->nullRow = res; sl@0: assert( pOp->p2>0 && pOp->p2nOp ); sl@0: if( res ){ sl@0: pc = pOp->p2 - 1; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Next P1 P2 * * * sl@0: ** sl@0: ** Advance cursor P1 so that it points to the next key/data pair in its sl@0: ** table or index. If there are no more key/value pairs then fall through sl@0: ** to the following instruction. But if the cursor advance was successful, sl@0: ** jump immediately to P2. sl@0: ** sl@0: ** The P1 cursor must be for a real table, not a pseudo-table. sl@0: ** sl@0: ** See also: Prev sl@0: */ sl@0: /* Opcode: Prev P1 P2 * * * sl@0: ** sl@0: ** Back up cursor P1 so that it points to the previous key/data pair in its sl@0: ** table or index. If there is no previous key/value pairs then fall through sl@0: ** to the following instruction. But if the cursor backup was successful, sl@0: ** jump immediately to P2. sl@0: ** sl@0: ** The P1 cursor must be for a real table, not a pseudo-table. sl@0: */ sl@0: case OP_Prev: /* jump */ sl@0: case OP_Next: { /* jump */ sl@0: Cursor *pC; sl@0: BtCursor *pCrsr; sl@0: int res; sl@0: sl@0: CHECK_FOR_INTERRUPT; sl@0: assert( pOp->p1>=0 && pOp->p1nCursor ); sl@0: pC = p->apCsr[pOp->p1]; sl@0: if( pC==0 ){ sl@0: break; /* See ticket #2273 */ sl@0: } sl@0: pCrsr = pC->pCursor; sl@0: assert( pCrsr ); sl@0: res = 1; sl@0: assert( pC->deferredMoveto==0 ); sl@0: rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) : sl@0: sqlite3BtreePrevious(pCrsr, &res); sl@0: pC->nullRow = res; sl@0: pC->cacheStatus = CACHE_STALE; sl@0: if( res==0 ){ sl@0: pc = pOp->p2 - 1; sl@0: #ifdef SQLITE_TEST sl@0: sqlite3_search_count++; sl@0: #endif sl@0: } sl@0: pC->rowidIsValid = 0; sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: IdxInsert P1 P2 P3 * * sl@0: ** sl@0: ** Register P2 holds a SQL index key made using the sl@0: ** MakeIdxRec instructions. This opcode writes that key sl@0: ** into the index P1. Data for the entry is nil. sl@0: ** sl@0: ** P3 is a flag that provides a hint to the b-tree layer that this sl@0: ** insert is likely to be an append. sl@0: ** sl@0: ** This instruction only works for indices. The equivalent instruction sl@0: ** for tables is OP_Insert. sl@0: */ sl@0: case OP_IdxInsert: { /* in2 */ sl@0: int i = pOp->p1; sl@0: Cursor *pC; sl@0: BtCursor *pCrsr; sl@0: assert( i>=0 && inCursor ); sl@0: assert( p->apCsr[i]!=0 ); sl@0: assert( pIn2->flags & MEM_Blob ); sl@0: if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ sl@0: assert( pC->isTable==0 ); sl@0: rc = ExpandBlob(pIn2); sl@0: if( rc==SQLITE_OK ){ sl@0: int nKey = pIn2->n; sl@0: const char *zKey = pIn2->z; sl@0: rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3); sl@0: assert( pC->deferredMoveto==0 ); sl@0: pC->cacheStatus = CACHE_STALE; sl@0: } sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: IdxDeleteM P1 P2 P3 * * sl@0: ** sl@0: ** The content of P3 registers starting at register P2 form sl@0: ** an unpacked index key. This opcode removes that entry from the sl@0: ** index opened by cursor P1. sl@0: */ sl@0: case OP_IdxDelete: { sl@0: int i = pOp->p1; sl@0: Cursor *pC; sl@0: BtCursor *pCrsr; sl@0: assert( pOp->p3>0 ); sl@0: assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem ); sl@0: assert( i>=0 && inCursor ); sl@0: assert( p->apCsr[i]!=0 ); sl@0: if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ sl@0: int res; sl@0: UnpackedRecord r; sl@0: r.pKeyInfo = pC->pKeyInfo; sl@0: r.nField = pOp->p3; sl@0: r.needFree = 0; sl@0: r.needDestroy = 0; sl@0: r.aMem = &p->aMem[pOp->p2]; sl@0: rc = sqlite3BtreeMoveto(pCrsr, 0, &r, 0, 0, &res); sl@0: if( rc==SQLITE_OK && res==0 ){ sl@0: rc = sqlite3BtreeDelete(pCrsr); sl@0: } sl@0: assert( pC->deferredMoveto==0 ); sl@0: pC->cacheStatus = CACHE_STALE; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: IdxRowid P1 P2 * * * sl@0: ** sl@0: ** Write into register P2 an integer which is the last entry in the record at sl@0: ** the end of the index key pointed to by cursor P1. This integer should be sl@0: ** the rowid of the table entry to which this index entry points. sl@0: ** sl@0: ** See also: Rowid, MakeIdxRec. sl@0: */ sl@0: case OP_IdxRowid: { /* out2-prerelease */ sl@0: int i = pOp->p1; sl@0: BtCursor *pCrsr; sl@0: Cursor *pC; sl@0: sl@0: assert( i>=0 && inCursor ); sl@0: assert( p->apCsr[i]!=0 ); sl@0: if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ sl@0: i64 rowid; sl@0: sl@0: assert( pC->deferredMoveto==0 ); sl@0: assert( pC->isTable==0 ); sl@0: if( !pC->nullRow ){ sl@0: rc = sqlite3VdbeIdxRowid(pCrsr, &rowid); sl@0: if( rc!=SQLITE_OK ){ sl@0: goto abort_due_to_error; sl@0: } sl@0: MemSetTypeFlag(pOut, MEM_Int); sl@0: pOut->u.i = rowid; sl@0: } sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: IdxGE P1 P2 P3 P4 P5 sl@0: ** sl@0: ** The P4 register values beginning with P3 form an unpacked index sl@0: ** key that omits the ROWID. Compare this key value against the index sl@0: ** that P1 is currently pointing to, ignoring the ROWID on the P1 index. sl@0: ** sl@0: ** If the P1 index entry is greater than or equal to the key value sl@0: ** then jump to P2. Otherwise fall through to the next instruction. sl@0: ** sl@0: ** If P5 is non-zero then the key value is increased by an epsilon sl@0: ** prior to the comparison. This make the opcode work like IdxGT except sl@0: ** that if the key from register P3 is a prefix of the key in the cursor, sl@0: ** the result is false whereas it would be true with IdxGT. sl@0: */ sl@0: /* Opcode: IdxLT P1 P2 P3 * P5 sl@0: ** sl@0: ** The P4 register values beginning with P3 form an unpacked index sl@0: ** key that omits the ROWID. Compare this key value against the index sl@0: ** that P1 is currently pointing to, ignoring the ROWID on the P1 index. sl@0: ** sl@0: ** If the P1 index entry is less than the key value then jump to P2. sl@0: ** Otherwise fall through to the next instruction. sl@0: ** sl@0: ** If P5 is non-zero then the key value is increased by an epsilon prior sl@0: ** to the comparison. This makes the opcode work like IdxLE. sl@0: */ sl@0: case OP_IdxLT: /* jump, in3 */ sl@0: case OP_IdxGE: { /* jump, in3 */ sl@0: int i= pOp->p1; sl@0: Cursor *pC; sl@0: sl@0: assert( i>=0 && inCursor ); sl@0: assert( p->apCsr[i]!=0 ); sl@0: if( (pC = p->apCsr[i])->pCursor!=0 ){ sl@0: int res; sl@0: UnpackedRecord r; sl@0: assert( pC->deferredMoveto==0 ); sl@0: assert( pOp->p5==0 || pOp->p5==1 ); sl@0: assert( pOp->p4type==P4_INT32 ); sl@0: r.pKeyInfo = pC->pKeyInfo; sl@0: r.nField = pOp->p4.i; sl@0: r.needFree = 0; sl@0: r.needDestroy = 0; sl@0: r.aMem = &p->aMem[pOp->p3]; sl@0: *pC->pIncrKey = pOp->p5; sl@0: rc = sqlite3VdbeIdxKeyCompare(pC, &r, 0, 0, &res); sl@0: *pC->pIncrKey = 0; sl@0: if( pOp->opcode==OP_IdxLT ){ sl@0: res = -res; sl@0: }else{ sl@0: assert( pOp->opcode==OP_IdxGE ); sl@0: res++; sl@0: } sl@0: if( res>0 ){ sl@0: pc = pOp->p2 - 1 ; sl@0: } sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Destroy P1 P2 P3 * * sl@0: ** sl@0: ** Delete an entire database table or index whose root page in the database sl@0: ** file is given by P1. sl@0: ** sl@0: ** The table being destroyed is in the main database file if P3==0. If sl@0: ** P3==1 then the table to be clear is in the auxiliary database file sl@0: ** that is used to store tables create using CREATE TEMPORARY TABLE. sl@0: ** sl@0: ** If AUTOVACUUM is enabled then it is possible that another root page sl@0: ** might be moved into the newly deleted root page in order to keep all sl@0: ** root pages contiguous at the beginning of the database. The former sl@0: ** value of the root page that moved - its value before the move occurred - sl@0: ** is stored in register P2. If no page sl@0: ** movement was required (because the table being dropped was already sl@0: ** the last one in the database) then a zero is stored in register P2. sl@0: ** If AUTOVACUUM is disabled then a zero is stored in register P2. sl@0: ** sl@0: ** See also: Clear sl@0: */ sl@0: case OP_Destroy: { /* out2-prerelease */ sl@0: int iMoved; sl@0: int iCnt; sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: Vdbe *pVdbe; sl@0: iCnt = 0; sl@0: for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){ sl@0: if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){ sl@0: iCnt++; sl@0: } sl@0: } sl@0: #else sl@0: iCnt = db->activeVdbeCnt; sl@0: #endif sl@0: if( iCnt>1 ){ sl@0: rc = SQLITE_LOCKED; sl@0: p->errorAction = OE_Abort; sl@0: }else{ sl@0: int iDb = pOp->p3; sl@0: assert( iCnt==1 ); sl@0: assert( (p->btreeMask & (1<aDb[iDb].pBt, pOp->p1, &iMoved); sl@0: MemSetTypeFlag(pOut, MEM_Int); sl@0: pOut->u.i = iMoved; sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM sl@0: if( rc==SQLITE_OK && iMoved!=0 ){ sl@0: sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1); sl@0: } sl@0: #endif sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: Clear P1 P2 * sl@0: ** sl@0: ** Delete all contents of the database table or index whose root page sl@0: ** in the database file is given by P1. But, unlike Destroy, do not sl@0: ** remove the table or index from the database file. sl@0: ** sl@0: ** The table being clear is in the main database file if P2==0. If sl@0: ** P2==1 then the table to be clear is in the auxiliary database file sl@0: ** that is used to store tables create using CREATE TEMPORARY TABLE. sl@0: ** sl@0: ** See also: Destroy sl@0: */ sl@0: case OP_Clear: { sl@0: assert( (p->btreeMask & (1<p2))!=0 ); sl@0: rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: CreateTable P1 P2 * * * sl@0: ** sl@0: ** Allocate a new table in the main database file if P1==0 or in the sl@0: ** auxiliary database file if P1==1 or in an attached database if sl@0: ** P1>1. Write the root page number of the new table into sl@0: ** register P2 sl@0: ** sl@0: ** The difference between a table and an index is this: A table must sl@0: ** have a 4-byte integer key and can have arbitrary data. An index sl@0: ** has an arbitrary key but no data. sl@0: ** sl@0: ** See also: CreateIndex sl@0: */ sl@0: /* Opcode: CreateIndex P1 P2 * * * sl@0: ** sl@0: ** Allocate a new index in the main database file if P1==0 or in the sl@0: ** auxiliary database file if P1==1 or in an attached database if sl@0: ** P1>1. Write the root page number of the new table into sl@0: ** register P2. sl@0: ** sl@0: ** See documentation on OP_CreateTable for additional information. sl@0: */ sl@0: case OP_CreateIndex: /* out2-prerelease */ sl@0: case OP_CreateTable: { /* out2-prerelease */ sl@0: int pgno; sl@0: int flags; sl@0: Db *pDb; sl@0: assert( pOp->p1>=0 && pOp->p1nDb ); sl@0: assert( (p->btreeMask & (1<p1))!=0 ); sl@0: pDb = &db->aDb[pOp->p1]; sl@0: assert( pDb->pBt!=0 ); sl@0: if( pOp->opcode==OP_CreateTable ){ sl@0: /* flags = BTREE_INTKEY; */ sl@0: flags = BTREE_LEAFDATA|BTREE_INTKEY; sl@0: }else{ sl@0: flags = BTREE_ZERODATA; sl@0: } sl@0: rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); sl@0: if( rc==SQLITE_OK ){ sl@0: pOut->u.i = pgno; sl@0: MemSetTypeFlag(pOut, MEM_Int); sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: ParseSchema P1 P2 * P4 * sl@0: ** sl@0: ** Read and parse all entries from the SQLITE_MASTER table of database P1 sl@0: ** that match the WHERE clause P4. P2 is the "force" flag. Always do sl@0: ** the parsing if P2 is true. If P2 is false, then this routine is a sl@0: ** no-op if the schema is not currently loaded. In other words, if P2 sl@0: ** is false, the SQLITE_MASTER table is only parsed if the rest of the sl@0: ** schema is already loaded into the symbol table. sl@0: ** sl@0: ** This opcode invokes the parser to create a new virtual machine, sl@0: ** then runs the new virtual machine. It is thus a re-entrant opcode. sl@0: */ sl@0: case OP_ParseSchema: { sl@0: char *zSql; sl@0: int iDb = pOp->p1; sl@0: const char *zMaster; sl@0: InitData initData; sl@0: sl@0: assert( iDb>=0 && iDbnDb ); sl@0: if( !pOp->p2 && !DbHasProperty(db, iDb, DB_SchemaLoaded) ){ sl@0: break; sl@0: } sl@0: zMaster = SCHEMA_TABLE(iDb); sl@0: initData.db = db; sl@0: initData.iDb = pOp->p1; sl@0: initData.pzErrMsg = &p->zErrMsg; sl@0: zSql = sqlite3MPrintf(db, sl@0: "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s", sl@0: db->aDb[iDb].zName, zMaster, pOp->p4.z); sl@0: if( zSql==0 ) goto no_mem; sl@0: (void)sqlite3SafetyOff(db); sl@0: assert( db->init.busy==0 ); sl@0: db->init.busy = 1; sl@0: assert( !db->mallocFailed ); sl@0: rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); sl@0: if( rc==SQLITE_ABORT ) rc = initData.rc; sl@0: sqlite3DbFree(db, zSql); sl@0: db->init.busy = 0; sl@0: (void)sqlite3SafetyOn(db); sl@0: if( rc==SQLITE_NOMEM ){ sl@0: goto no_mem; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: #if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) sl@0: /* Opcode: LoadAnalysis P1 * * * * sl@0: ** sl@0: ** Read the sqlite_stat1 table for database P1 and load the content sl@0: ** of that table into the internal index hash table. This will cause sl@0: ** the analysis to be used when preparing all subsequent queries. sl@0: */ sl@0: case OP_LoadAnalysis: { sl@0: int iDb = pOp->p1; sl@0: assert( iDb>=0 && iDbnDb ); sl@0: rc = sqlite3AnalysisLoad(db, iDb); sl@0: break; sl@0: } sl@0: #endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) */ sl@0: sl@0: /* Opcode: DropTable P1 * * P4 * sl@0: ** sl@0: ** Remove the internal (in-memory) data structures that describe sl@0: ** the table named P4 in database P1. This is called after a table sl@0: ** is dropped in order to keep the internal representation of the sl@0: ** schema consistent with what is on disk. sl@0: */ sl@0: case OP_DropTable: { sl@0: sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: DropIndex P1 * * P4 * sl@0: ** sl@0: ** Remove the internal (in-memory) data structures that describe sl@0: ** the index named P4 in database P1. This is called after an index sl@0: ** is dropped in order to keep the internal representation of the sl@0: ** schema consistent with what is on disk. sl@0: */ sl@0: case OP_DropIndex: { sl@0: sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: DropTrigger P1 * * P4 * sl@0: ** sl@0: ** Remove the internal (in-memory) data structures that describe sl@0: ** the trigger named P4 in database P1. This is called after a trigger sl@0: ** is dropped in order to keep the internal representation of the sl@0: ** schema consistent with what is on disk. sl@0: */ sl@0: case OP_DropTrigger: { sl@0: sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); sl@0: break; sl@0: } sl@0: sl@0: sl@0: #ifndef SQLITE_OMIT_INTEGRITY_CHECK sl@0: /* Opcode: IntegrityCk P1 P2 P3 * P5 sl@0: ** sl@0: ** Do an analysis of the currently open database. Store in sl@0: ** register P1 the text of an error message describing any problems. sl@0: ** If no problems are found, store a NULL in register P1. sl@0: ** sl@0: ** The register P3 contains the maximum number of allowed errors. sl@0: ** At most reg(P3) errors will be reported. sl@0: ** In other words, the analysis stops as soon as reg(P1) errors are sl@0: ** seen. Reg(P1) is updated with the number of errors remaining. sl@0: ** sl@0: ** The root page numbers of all tables in the database are integer sl@0: ** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables sl@0: ** total. sl@0: ** sl@0: ** If P5 is not zero, the check is done on the auxiliary database sl@0: ** file, not the main database file. sl@0: ** sl@0: ** This opcode is used to implement the integrity_check pragma. sl@0: */ sl@0: case OP_IntegrityCk: { sl@0: int nRoot; /* Number of tables to check. (Number of root pages.) */ sl@0: int *aRoot; /* Array of rootpage numbers for tables to be checked */ sl@0: int j; /* Loop counter */ sl@0: int nErr; /* Number of errors reported */ sl@0: char *z; /* Text of the error report */ sl@0: Mem *pnErr; /* Register keeping track of errors remaining */ sl@0: sl@0: nRoot = pOp->p2; sl@0: assert( nRoot>0 ); sl@0: aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) ); sl@0: if( aRoot==0 ) goto no_mem; sl@0: assert( pOp->p3>0 && pOp->p3<=p->nMem ); sl@0: pnErr = &p->aMem[pOp->p3]; sl@0: assert( (pnErr->flags & MEM_Int)!=0 ); sl@0: assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); sl@0: pIn1 = &p->aMem[pOp->p1]; sl@0: for(j=0; jp5nDb ); sl@0: assert( (p->btreeMask & (1<p5))!=0 ); sl@0: z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, sl@0: pnErr->u.i, &nErr); sl@0: sqlite3DbFree(db, aRoot); sl@0: pnErr->u.i -= nErr; sl@0: sqlite3VdbeMemSetNull(pIn1); sl@0: if( nErr==0 ){ sl@0: assert( z==0 ); sl@0: }else if( z==0 ){ sl@0: goto no_mem; sl@0: }else{ sl@0: sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); sl@0: } sl@0: UPDATE_MAX_BLOBSIZE(pIn1); sl@0: sqlite3VdbeChangeEncoding(pIn1, encoding); sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ sl@0: sl@0: /* Opcode: FifoWrite P1 * * * * sl@0: ** sl@0: ** Write the integer from register P1 into the Fifo. sl@0: */ sl@0: case OP_FifoWrite: { /* in1 */ sl@0: p->sFifo.db = db; sl@0: if( sqlite3VdbeFifoPush(&p->sFifo, sqlite3VdbeIntValue(pIn1))==SQLITE_NOMEM ){ sl@0: goto no_mem; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: FifoRead P1 P2 * * * sl@0: ** sl@0: ** Attempt to read a single integer from the Fifo. Store that sl@0: ** integer in register P1. sl@0: ** sl@0: ** If the Fifo is empty jump to P2. sl@0: */ sl@0: case OP_FifoRead: { /* jump */ sl@0: CHECK_FOR_INTERRUPT; sl@0: assert( pOp->p1>0 && pOp->p1<=p->nMem ); sl@0: pOut = &p->aMem[pOp->p1]; sl@0: MemSetTypeFlag(pOut, MEM_Int); sl@0: if( sqlite3VdbeFifoPop(&p->sFifo, &pOut->u.i)==SQLITE_DONE ){ sl@0: pc = pOp->p2 - 1; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: #ifndef SQLITE_OMIT_TRIGGER sl@0: /* Opcode: ContextPush * * * sl@0: ** sl@0: ** Save the current Vdbe context such that it can be restored by a ContextPop sl@0: ** opcode. The context stores the last insert row id, the last statement change sl@0: ** count, and the current statement change count. sl@0: */ sl@0: case OP_ContextPush: { sl@0: int i = p->contextStackTop++; sl@0: Context *pContext; sl@0: sl@0: assert( i>=0 ); sl@0: /* FIX ME: This should be allocated as part of the vdbe at compile-time */ sl@0: if( i>=p->contextStackDepth ){ sl@0: p->contextStackDepth = i+1; sl@0: p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack, sl@0: sizeof(Context)*(i+1)); sl@0: if( p->contextStack==0 ) goto no_mem; sl@0: } sl@0: pContext = &p->contextStack[i]; sl@0: pContext->lastRowid = db->lastRowid; sl@0: pContext->nChange = p->nChange; sl@0: pContext->sFifo = p->sFifo; sl@0: sqlite3VdbeFifoInit(&p->sFifo, db); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: ContextPop * * * sl@0: ** sl@0: ** Restore the Vdbe context to the state it was in when contextPush was last sl@0: ** executed. The context stores the last insert row id, the last statement sl@0: ** change count, and the current statement change count. sl@0: */ sl@0: case OP_ContextPop: { sl@0: Context *pContext = &p->contextStack[--p->contextStackTop]; sl@0: assert( p->contextStackTop>=0 ); sl@0: db->lastRowid = pContext->lastRowid; sl@0: p->nChange = pContext->nChange; sl@0: sqlite3VdbeFifoClear(&p->sFifo); sl@0: p->sFifo = pContext->sFifo; sl@0: break; sl@0: } sl@0: #endif /* #ifndef SQLITE_OMIT_TRIGGER */ sl@0: sl@0: #ifndef SQLITE_OMIT_AUTOINCREMENT sl@0: /* Opcode: MemMax P1 P2 * * * sl@0: ** sl@0: ** Set the value of register P1 to the maximum of its current value sl@0: ** and the value in register P2. sl@0: ** sl@0: ** This instruction throws an error if the memory cell is not initially sl@0: ** an integer. sl@0: */ sl@0: case OP_MemMax: { /* in1, in2 */ sl@0: sqlite3VdbeMemIntegerify(pIn1); sl@0: sqlite3VdbeMemIntegerify(pIn2); sl@0: if( pIn1->u.iu.i){ sl@0: pIn1->u.i = pIn2->u.i; sl@0: } sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_AUTOINCREMENT */ sl@0: sl@0: /* Opcode: IfPos P1 P2 * * * sl@0: ** sl@0: ** If the value of register P1 is 1 or greater, jump to P2. sl@0: ** sl@0: ** It is illegal to use this instruction on a register that does sl@0: ** not contain an integer. An assertion fault will result if you try. sl@0: */ sl@0: case OP_IfPos: { /* jump, in1 */ sl@0: assert( pIn1->flags&MEM_Int ); sl@0: if( pIn1->u.i>0 ){ sl@0: pc = pOp->p2 - 1; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: IfNeg P1 P2 * * * sl@0: ** sl@0: ** If the value of register P1 is less than zero, jump to P2. sl@0: ** sl@0: ** It is illegal to use this instruction on a register that does sl@0: ** not contain an integer. An assertion fault will result if you try. sl@0: */ sl@0: case OP_IfNeg: { /* jump, in1 */ sl@0: assert( pIn1->flags&MEM_Int ); sl@0: if( pIn1->u.i<0 ){ sl@0: pc = pOp->p2 - 1; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: IfZero P1 P2 * * * sl@0: ** sl@0: ** If the value of register P1 is exactly 0, jump to P2. sl@0: ** sl@0: ** It is illegal to use this instruction on a register that does sl@0: ** not contain an integer. An assertion fault will result if you try. sl@0: */ sl@0: case OP_IfZero: { /* jump, in1 */ sl@0: assert( pIn1->flags&MEM_Int ); sl@0: if( pIn1->u.i==0 ){ sl@0: pc = pOp->p2 - 1; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: AggStep * P2 P3 P4 P5 sl@0: ** sl@0: ** Execute the step function for an aggregate. The sl@0: ** function has P5 arguments. P4 is a pointer to the FuncDef sl@0: ** structure that specifies the function. Use register sl@0: ** P3 as the accumulator. sl@0: ** sl@0: ** The P5 arguments are taken from register P2 and its sl@0: ** successors. sl@0: */ sl@0: case OP_AggStep: { sl@0: int n = pOp->p5; sl@0: int i; sl@0: Mem *pMem, *pRec; sl@0: sqlite3_context ctx; sl@0: sqlite3_value **apVal; sl@0: sl@0: assert( n>=0 ); sl@0: pRec = &p->aMem[pOp->p2]; sl@0: apVal = p->apArg; sl@0: assert( apVal || n==0 ); sl@0: for(i=0; ip4.pFunc; sl@0: assert( pOp->p3>0 && pOp->p3<=p->nMem ); sl@0: ctx.pMem = pMem = &p->aMem[pOp->p3]; sl@0: pMem->n++; sl@0: ctx.s.flags = MEM_Null; sl@0: ctx.s.z = 0; sl@0: ctx.s.zMalloc = 0; sl@0: ctx.s.xDel = 0; sl@0: ctx.s.db = db; sl@0: ctx.isError = 0; sl@0: ctx.pColl = 0; sl@0: if( ctx.pFunc->needCollSeq ){ sl@0: assert( pOp>p->aOp ); sl@0: assert( pOp[-1].p4type==P4_COLLSEQ ); sl@0: assert( pOp[-1].opcode==OP_CollSeq ); sl@0: ctx.pColl = pOp[-1].p4.pColl; sl@0: } sl@0: (ctx.pFunc->xStep)(&ctx, n, apVal); sl@0: if( ctx.isError ){ sl@0: sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s)); sl@0: rc = ctx.isError; sl@0: } sl@0: sqlite3VdbeMemRelease(&ctx.s); sl@0: break; sl@0: } sl@0: sl@0: /* Opcode: AggFinal P1 P2 * P4 * sl@0: ** sl@0: ** Execute the finalizer function for an aggregate. P1 is sl@0: ** the memory location that is the accumulator for the aggregate. sl@0: ** sl@0: ** P2 is the number of arguments that the step function takes and sl@0: ** P4 is a pointer to the FuncDef for this function. The P2 sl@0: ** argument is not used by this opcode. It is only there to disambiguate sl@0: ** functions that can take varying numbers of arguments. The sl@0: ** P4 argument is only needed for the degenerate case where sl@0: ** the step function was not previously called. sl@0: */ sl@0: case OP_AggFinal: { sl@0: Mem *pMem; sl@0: assert( pOp->p1>0 && pOp->p1<=p->nMem ); sl@0: pMem = &p->aMem[pOp->p1]; sl@0: assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); sl@0: rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); sl@0: if( rc==SQLITE_ERROR ){ sl@0: sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem)); sl@0: } sl@0: sqlite3VdbeChangeEncoding(pMem, encoding); sl@0: UPDATE_MAX_BLOBSIZE(pMem); sl@0: if( sqlite3VdbeMemTooBig(pMem) ){ sl@0: goto too_big; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: sl@0: #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) sl@0: /* Opcode: Vacuum * * * * * sl@0: ** sl@0: ** Vacuum the entire database. This opcode will cause other virtual sl@0: ** machines to be created and run. It may not be called from within sl@0: ** a transaction. sl@0: */ sl@0: case OP_Vacuum: { sl@0: if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; sl@0: rc = sqlite3RunVacuum(&p->zErrMsg, db); sl@0: if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; sl@0: break; sl@0: } sl@0: #endif sl@0: sl@0: #if !defined(SQLITE_OMIT_AUTOVACUUM) sl@0: /* Opcode: IncrVacuum P1 P2 * * * sl@0: ** sl@0: ** Perform a single step of the incremental vacuum procedure on sl@0: ** the P1 database. If the vacuum has finished, jump to instruction sl@0: ** P2. Otherwise, fall through to the next instruction. sl@0: */ sl@0: case OP_IncrVacuum: { /* jump */ sl@0: Btree *pBt; sl@0: sl@0: assert( pOp->p1>=0 && pOp->p1nDb ); sl@0: assert( (p->btreeMask & (1<p1))!=0 ); sl@0: pBt = db->aDb[pOp->p1].pBt; sl@0: rc = sqlite3BtreeIncrVacuum(pBt); sl@0: if( rc==SQLITE_DONE ){ sl@0: pc = pOp->p2 - 1; sl@0: rc = SQLITE_OK; sl@0: } sl@0: break; sl@0: } sl@0: #endif sl@0: sl@0: /* Opcode: Expire P1 * * * * sl@0: ** sl@0: ** Cause precompiled statements to become expired. An expired statement sl@0: ** fails with an error code of SQLITE_SCHEMA if it is ever executed sl@0: ** (via sqlite3_step()). sl@0: ** sl@0: ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, sl@0: ** then only the currently executing statement is affected. sl@0: */ sl@0: case OP_Expire: { sl@0: if( !pOp->p1 ){ sl@0: sqlite3ExpirePreparedStatements(db); sl@0: }else{ sl@0: p->expired = 1; sl@0: } sl@0: break; sl@0: } sl@0: sl@0: #ifndef SQLITE_OMIT_SHARED_CACHE sl@0: /* Opcode: TableLock P1 P2 P3 P4 * sl@0: ** sl@0: ** Obtain a lock on a particular table. This instruction is only used when sl@0: ** the shared-cache feature is enabled. sl@0: ** sl@0: ** If P1 is the index of the database in sqlite3.aDb[] of the database sl@0: ** on which the lock is acquired. A readlock is obtained if P3==0 or sl@0: ** a write lock if P3==1. sl@0: ** sl@0: ** P2 contains the root-page of the table to lock. sl@0: ** sl@0: ** P4 contains a pointer to the name of the table being locked. This is only sl@0: ** used to generate an error message if the lock cannot be obtained. sl@0: */ sl@0: case OP_TableLock: { sl@0: int p1 = pOp->p1; sl@0: u8 isWriteLock = pOp->p3; sl@0: assert( p1>=0 && p1nDb ); sl@0: assert( (p->btreeMask & (1<aDb[p1].pBt, pOp->p2, isWriteLock); sl@0: if( rc==SQLITE_LOCKED ){ sl@0: const char *z = pOp->p4.z; sl@0: sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z); sl@0: } sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_SHARED_CACHE */ sl@0: sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: /* Opcode: VBegin * * * P4 * sl@0: ** sl@0: ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the sl@0: ** xBegin method for that table. sl@0: ** sl@0: ** Also, whether or not P4 is set, check that this is not being called from sl@0: ** within a callback to a virtual table xSync() method. If it is, set the sl@0: ** error code to SQLITE_LOCKED. sl@0: */ sl@0: case OP_VBegin: { sl@0: sqlite3_vtab *pVtab = pOp->p4.pVtab; sl@0: rc = sqlite3VtabBegin(db, pVtab); sl@0: if( pVtab ){ sl@0: sqlite3DbFree(db, p->zErrMsg); sl@0: p->zErrMsg = pVtab->zErrMsg; sl@0: pVtab->zErrMsg = 0; sl@0: } sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_VIRTUALTABLE */ sl@0: sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: /* Opcode: VCreate P1 * * P4 * sl@0: ** sl@0: ** P4 is the name of a virtual table in database P1. Call the xCreate method sl@0: ** for that table. sl@0: */ sl@0: case OP_VCreate: { sl@0: rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg); sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_VIRTUALTABLE */ sl@0: sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: /* Opcode: VDestroy P1 * * P4 * sl@0: ** sl@0: ** P4 is the name of a virtual table in database P1. Call the xDestroy method sl@0: ** of that table. sl@0: */ sl@0: case OP_VDestroy: { sl@0: p->inVtabMethod = 2; sl@0: rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); sl@0: p->inVtabMethod = 0; sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_VIRTUALTABLE */ sl@0: sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: /* Opcode: VOpen P1 * * P4 * sl@0: ** sl@0: ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. sl@0: ** P1 is a cursor number. This opcode opens a cursor to the virtual sl@0: ** table and stores that cursor in P1. sl@0: */ sl@0: case OP_VOpen: { sl@0: Cursor *pCur = 0; sl@0: sqlite3_vtab_cursor *pVtabCursor = 0; sl@0: sl@0: sqlite3_vtab *pVtab = pOp->p4.pVtab; sl@0: sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule; sl@0: sl@0: assert(pVtab && pModule); sl@0: if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; sl@0: rc = pModule->xOpen(pVtab, &pVtabCursor); sl@0: sqlite3DbFree(db, p->zErrMsg); sl@0: p->zErrMsg = pVtab->zErrMsg; sl@0: pVtab->zErrMsg = 0; sl@0: if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; sl@0: if( SQLITE_OK==rc ){ sl@0: /* Initialize sqlite3_vtab_cursor base class */ sl@0: pVtabCursor->pVtab = pVtab; sl@0: sl@0: /* Initialise vdbe cursor object */ sl@0: pCur = allocateCursor(p, pOp->p1, &pOp[-1], -1, 0); sl@0: if( pCur ){ sl@0: pCur->pVtabCursor = pVtabCursor; sl@0: pCur->pModule = pVtabCursor->pVtab->pModule; sl@0: }else{ sl@0: db->mallocFailed = 1; sl@0: pModule->xClose(pVtabCursor); sl@0: } sl@0: } sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_VIRTUALTABLE */ sl@0: sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: /* Opcode: VFilter P1 P2 P3 P4 * sl@0: ** sl@0: ** P1 is a cursor opened using VOpen. P2 is an address to jump to if sl@0: ** the filtered result set is empty. sl@0: ** sl@0: ** P4 is either NULL or a string that was generated by the xBestIndex sl@0: ** method of the module. The interpretation of the P4 string is left sl@0: ** to the module implementation. sl@0: ** sl@0: ** This opcode invokes the xFilter method on the virtual table specified sl@0: ** by P1. The integer query plan parameter to xFilter is stored in register sl@0: ** P3. Register P3+1 stores the argc parameter to be passed to the sl@0: ** xFilter method. Registers P3+2..P3+1+argc are the argc sl@0: ** additional parameters which are passed to sl@0: ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. sl@0: ** sl@0: ** A jump is made to P2 if the result set after filtering would be empty. sl@0: */ sl@0: case OP_VFilter: { /* jump */ sl@0: int nArg; sl@0: int iQuery; sl@0: const sqlite3_module *pModule; sl@0: Mem *pQuery = &p->aMem[pOp->p3]; sl@0: Mem *pArgc = &pQuery[1]; sl@0: sqlite3_vtab_cursor *pVtabCursor; sl@0: sqlite3_vtab *pVtab; sl@0: sl@0: Cursor *pCur = p->apCsr[pOp->p1]; sl@0: sl@0: REGISTER_TRACE(pOp->p3, pQuery); sl@0: assert( pCur->pVtabCursor ); sl@0: pVtabCursor = pCur->pVtabCursor; sl@0: pVtab = pVtabCursor->pVtab; sl@0: pModule = pVtab->pModule; sl@0: sl@0: /* Grab the index number and argc parameters */ sl@0: assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); sl@0: nArg = pArgc->u.i; sl@0: iQuery = pQuery->u.i; sl@0: sl@0: /* Invoke the xFilter method */ sl@0: { sl@0: int res = 0; sl@0: int i; sl@0: Mem **apArg = p->apArg; sl@0: for(i = 0; iinVtabMethod = 1; sl@0: rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg); sl@0: p->inVtabMethod = 0; sl@0: sqlite3DbFree(db, p->zErrMsg); sl@0: p->zErrMsg = pVtab->zErrMsg; sl@0: pVtab->zErrMsg = 0; sl@0: sqlite3VtabUnlock(db, pVtab); sl@0: if( rc==SQLITE_OK ){ sl@0: res = pModule->xEof(pVtabCursor); sl@0: } sl@0: if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; sl@0: sl@0: if( res ){ sl@0: pc = pOp->p2 - 1; sl@0: } sl@0: } sl@0: pCur->nullRow = 0; sl@0: sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_VIRTUALTABLE */ sl@0: sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: /* Opcode: VRowid P1 P2 * * * sl@0: ** sl@0: ** Store into register P2 the rowid of sl@0: ** the virtual-table that the P1 cursor is pointing to. sl@0: */ sl@0: case OP_VRowid: { /* out2-prerelease */ sl@0: sqlite3_vtab *pVtab; sl@0: const sqlite3_module *pModule; sl@0: sqlite_int64 iRow; sl@0: Cursor *pCur = p->apCsr[pOp->p1]; sl@0: sl@0: assert( pCur->pVtabCursor ); sl@0: if( pCur->nullRow ){ sl@0: break; sl@0: } sl@0: pVtab = pCur->pVtabCursor->pVtab; sl@0: pModule = pVtab->pModule; sl@0: assert( pModule->xRowid ); sl@0: if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; sl@0: rc = pModule->xRowid(pCur->pVtabCursor, &iRow); sl@0: sqlite3DbFree(db, p->zErrMsg); sl@0: p->zErrMsg = pVtab->zErrMsg; sl@0: pVtab->zErrMsg = 0; sl@0: if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; sl@0: MemSetTypeFlag(pOut, MEM_Int); sl@0: pOut->u.i = iRow; sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_VIRTUALTABLE */ sl@0: sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: /* Opcode: VColumn P1 P2 P3 * * sl@0: ** sl@0: ** Store the value of the P2-th column of sl@0: ** the row of the virtual-table that the sl@0: ** P1 cursor is pointing to into register P3. sl@0: */ sl@0: case OP_VColumn: { sl@0: sqlite3_vtab *pVtab; sl@0: const sqlite3_module *pModule; sl@0: Mem *pDest; sl@0: sqlite3_context sContext; sl@0: sl@0: Cursor *pCur = p->apCsr[pOp->p1]; sl@0: assert( pCur->pVtabCursor ); sl@0: assert( pOp->p3>0 && pOp->p3<=p->nMem ); sl@0: pDest = &p->aMem[pOp->p3]; sl@0: if( pCur->nullRow ){ sl@0: sqlite3VdbeMemSetNull(pDest); sl@0: break; sl@0: } sl@0: pVtab = pCur->pVtabCursor->pVtab; sl@0: pModule = pVtab->pModule; sl@0: assert( pModule->xColumn ); sl@0: memset(&sContext, 0, sizeof(sContext)); sl@0: sl@0: /* The output cell may already have a buffer allocated. Move sl@0: ** the current contents to sContext.s so in case the user-function sl@0: ** can use the already allocated buffer instead of allocating a sl@0: ** new one. sl@0: */ sl@0: sqlite3VdbeMemMove(&sContext.s, pDest); sl@0: MemSetTypeFlag(&sContext.s, MEM_Null); sl@0: sl@0: if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; sl@0: rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2); sl@0: sqlite3DbFree(db, p->zErrMsg); sl@0: p->zErrMsg = pVtab->zErrMsg; sl@0: pVtab->zErrMsg = 0; sl@0: sl@0: /* Copy the result of the function to the P3 register. We sl@0: ** do this regardless of whether or not an error occured to ensure any sl@0: ** dynamic allocation in sContext.s (a Mem struct) is released. sl@0: */ sl@0: sqlite3VdbeChangeEncoding(&sContext.s, encoding); sl@0: REGISTER_TRACE(pOp->p3, pDest); sl@0: sqlite3VdbeMemMove(pDest, &sContext.s); sl@0: UPDATE_MAX_BLOBSIZE(pDest); sl@0: sl@0: if( sqlite3SafetyOn(db) ){ sl@0: goto abort_due_to_misuse; sl@0: } sl@0: if( sqlite3VdbeMemTooBig(pDest) ){ sl@0: goto too_big; sl@0: } sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_VIRTUALTABLE */ sl@0: sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: /* Opcode: VNext P1 P2 * * * sl@0: ** sl@0: ** Advance virtual table P1 to the next row in its result set and sl@0: ** jump to instruction P2. Or, if the virtual table has reached sl@0: ** the end of its result set, then fall through to the next instruction. sl@0: */ sl@0: case OP_VNext: { /* jump */ sl@0: sqlite3_vtab *pVtab; sl@0: const sqlite3_module *pModule; sl@0: int res = 0; sl@0: sl@0: Cursor *pCur = p->apCsr[pOp->p1]; sl@0: assert( pCur->pVtabCursor ); sl@0: if( pCur->nullRow ){ sl@0: break; sl@0: } sl@0: pVtab = pCur->pVtabCursor->pVtab; sl@0: pModule = pVtab->pModule; sl@0: assert( pModule->xNext ); sl@0: sl@0: /* Invoke the xNext() method of the module. There is no way for the sl@0: ** underlying implementation to return an error if one occurs during sl@0: ** xNext(). Instead, if an error occurs, true is returned (indicating that sl@0: ** data is available) and the error code returned when xColumn or sl@0: ** some other method is next invoked on the save virtual table cursor. sl@0: */ sl@0: if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; sl@0: sqlite3VtabLock(pVtab); sl@0: p->inVtabMethod = 1; sl@0: rc = pModule->xNext(pCur->pVtabCursor); sl@0: p->inVtabMethod = 0; sl@0: sqlite3DbFree(db, p->zErrMsg); sl@0: p->zErrMsg = pVtab->zErrMsg; sl@0: pVtab->zErrMsg = 0; sl@0: sqlite3VtabUnlock(db, pVtab); sl@0: if( rc==SQLITE_OK ){ sl@0: res = pModule->xEof(pCur->pVtabCursor); sl@0: } sl@0: if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; sl@0: sl@0: if( !res ){ sl@0: /* If there is data, jump to P2 */ sl@0: pc = pOp->p2 - 1; sl@0: } sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_VIRTUALTABLE */ sl@0: sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: /* Opcode: VRename P1 * * P4 * sl@0: ** sl@0: ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. sl@0: ** This opcode invokes the corresponding xRename method. The value sl@0: ** in register P1 is passed as the zName argument to the xRename method. sl@0: */ sl@0: case OP_VRename: { sl@0: sqlite3_vtab *pVtab = pOp->p4.pVtab; sl@0: Mem *pName = &p->aMem[pOp->p1]; sl@0: assert( pVtab->pModule->xRename ); sl@0: REGISTER_TRACE(pOp->p1, pName); sl@0: sl@0: Stringify(pName, encoding); sl@0: sl@0: if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; sl@0: sqlite3VtabLock(pVtab); sl@0: rc = pVtab->pModule->xRename(pVtab, pName->z); sl@0: sqlite3DbFree(db, p->zErrMsg); sl@0: p->zErrMsg = pVtab->zErrMsg; sl@0: pVtab->zErrMsg = 0; sl@0: sqlite3VtabUnlock(db, pVtab); sl@0: if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; sl@0: sl@0: break; sl@0: } sl@0: #endif sl@0: sl@0: #ifndef SQLITE_OMIT_VIRTUALTABLE sl@0: /* Opcode: VUpdate P1 P2 P3 P4 * sl@0: ** sl@0: ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. sl@0: ** This opcode invokes the corresponding xUpdate method. P2 values sl@0: ** are contiguous memory cells starting at P3 to pass to the xUpdate sl@0: ** invocation. The value in register (P3+P2-1) corresponds to the sl@0: ** p2th element of the argv array passed to xUpdate. sl@0: ** sl@0: ** The xUpdate method will do a DELETE or an INSERT or both. sl@0: ** The argv[0] element (which corresponds to memory cell P3) sl@0: ** is the rowid of a row to delete. If argv[0] is NULL then no sl@0: ** deletion occurs. The argv[1] element is the rowid of the new sl@0: ** row. This can be NULL to have the virtual table select the new sl@0: ** rowid for itself. The subsequent elements in the array are sl@0: ** the values of columns in the new row. sl@0: ** sl@0: ** If P2==1 then no insert is performed. argv[0] is the rowid of sl@0: ** a row to delete. sl@0: ** sl@0: ** P1 is a boolean flag. If it is set to true and the xUpdate call sl@0: ** is successful, then the value returned by sqlite3_last_insert_rowid() sl@0: ** is set to the value of the rowid for the row just inserted. sl@0: */ sl@0: case OP_VUpdate: { sl@0: sqlite3_vtab *pVtab = pOp->p4.pVtab; sl@0: sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule; sl@0: int nArg = pOp->p2; sl@0: assert( pOp->p4type==P4_VTAB ); sl@0: if( pModule->xUpdate==0 ){ sl@0: sqlite3SetString(&p->zErrMsg, db, "read-only table"); sl@0: rc = SQLITE_ERROR; sl@0: }else{ sl@0: int i; sl@0: sqlite_int64 rowid; sl@0: Mem **apArg = p->apArg; sl@0: Mem *pX = &p->aMem[pOp->p3]; sl@0: for(i=0; ixUpdate(pVtab, nArg, apArg, &rowid); sl@0: sqlite3DbFree(db, p->zErrMsg); sl@0: p->zErrMsg = pVtab->zErrMsg; sl@0: pVtab->zErrMsg = 0; sl@0: sqlite3VtabUnlock(db, pVtab); sl@0: if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; sl@0: if( pOp->p1 && rc==SQLITE_OK ){ sl@0: assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); sl@0: db->lastRowid = rowid; sl@0: } sl@0: p->nChange++; sl@0: } sl@0: break; sl@0: } sl@0: #endif /* SQLITE_OMIT_VIRTUALTABLE */ sl@0: sl@0: #ifndef SQLITE_OMIT_PAGER_PRAGMAS sl@0: /* Opcode: Pagecount P1 P2 * * * sl@0: ** sl@0: ** Write the current number of pages in database P1 to memory cell P2. sl@0: */ sl@0: case OP_Pagecount: { /* out2-prerelease */ sl@0: int p1 = pOp->p1; sl@0: int nPage; sl@0: Pager *pPager = sqlite3BtreePager(db->aDb[p1].pBt); sl@0: sl@0: rc = sqlite3PagerPagecount(pPager, &nPage); sl@0: if( rc==SQLITE_OK ){ sl@0: pOut->flags = MEM_Int; sl@0: pOut->u.i = nPage; sl@0: } sl@0: break; sl@0: } sl@0: #endif sl@0: sl@0: #ifndef SQLITE_OMIT_TRACE sl@0: /* Opcode: Trace * * * P4 * sl@0: ** sl@0: ** If tracing is enabled (by the sqlite3_trace()) interface, then sl@0: ** the UTF-8 string contained in P4 is emitted on the trace callback. sl@0: */ sl@0: case OP_Trace: { sl@0: if( pOp->p4.z ){ sl@0: if( db->xTrace ){ sl@0: db->xTrace(db->pTraceArg, pOp->p4.z); sl@0: } sl@0: #ifdef SQLITE_DEBUG sl@0: if( (db->flags & SQLITE_SqlTrace)!=0 ){ sl@0: sqlite3DebugPrintf("SQL-trace: %s\n", pOp->p4.z); sl@0: } sl@0: #endif /* SQLITE_DEBUG */ sl@0: } sl@0: break; sl@0: } sl@0: #endif sl@0: sl@0: sl@0: /* Opcode: Noop * * * * * sl@0: ** sl@0: ** Do nothing. This instruction is often useful as a jump sl@0: ** destination. sl@0: */ sl@0: /* sl@0: ** The magic Explain opcode are only inserted when explain==2 (which sl@0: ** is to say when the EXPLAIN QUERY PLAN syntax is used.) sl@0: ** This opcode records information from the optimizer. It is the sl@0: ** the same as a no-op. This opcodesnever appears in a real VM program. sl@0: */ sl@0: default: { /* This is really OP_Noop and OP_Explain */ sl@0: break; sl@0: } sl@0: sl@0: /***************************************************************************** sl@0: ** The cases of the switch statement above this line should all be indented sl@0: ** by 6 spaces. But the left-most 6 spaces have been removed to improve the sl@0: ** readability. From this point on down, the normal indentation rules are sl@0: ** restored. sl@0: *****************************************************************************/ sl@0: } sl@0: sl@0: #ifdef VDBE_PROFILE sl@0: { sl@0: u64 elapsed = sqlite3Hwtime() - start; sl@0: pOp->cycles += elapsed; sl@0: pOp->cnt++; sl@0: #if 0 sl@0: fprintf(stdout, "%10llu ", elapsed); sl@0: sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]); sl@0: #endif sl@0: } sl@0: #endif sl@0: sl@0: /* The following code adds nothing to the actual functionality sl@0: ** of the program. It is only here for testing and debugging. sl@0: ** On the other hand, it does burn CPU cycles every time through sl@0: ** the evaluator loop. So we can leave it out when NDEBUG is defined. sl@0: */ sl@0: #ifndef NDEBUG sl@0: assert( pc>=-1 && pcnOp ); sl@0: sl@0: #ifdef SQLITE_DEBUG sl@0: if( p->trace ){ sl@0: if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc); sl@0: if( opProperty & OPFLG_OUT2_PRERELEASE ){ sl@0: registerTrace(p->trace, pOp->p2, pOut); sl@0: } sl@0: if( opProperty & OPFLG_OUT3 ){ sl@0: registerTrace(p->trace, pOp->p3, pOut); sl@0: } sl@0: } sl@0: #endif /* SQLITE_DEBUG */ sl@0: #endif /* NDEBUG */ sl@0: } /* The end of the for(;;) loop the loops through opcodes */ sl@0: sl@0: /* If we reach this point, it means that execution is finished with sl@0: ** an error of some kind. sl@0: */ sl@0: vdbe_error_halt: sl@0: assert( rc ); sl@0: p->rc = rc; sl@0: sqlite3VdbeHalt(p); sl@0: if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1; sl@0: rc = SQLITE_ERROR; sl@0: sl@0: /* This is the only way out of this procedure. We have to sl@0: ** release the mutexes on btrees that were acquired at the sl@0: ** top. */ sl@0: vdbe_return: sl@0: sqlite3BtreeMutexArrayLeave(&p->aMutex); sl@0: return rc; sl@0: sl@0: /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH sl@0: ** is encountered. sl@0: */ sl@0: too_big: sl@0: sqlite3SetString(&p->zErrMsg, db, "string or blob too big"); sl@0: rc = SQLITE_TOOBIG; sl@0: goto vdbe_error_halt; sl@0: sl@0: /* Jump to here if a malloc() fails. sl@0: */ sl@0: no_mem: sl@0: db->mallocFailed = 1; sl@0: sqlite3SetString(&p->zErrMsg, db, "out of memory"); sl@0: rc = SQLITE_NOMEM; sl@0: goto vdbe_error_halt; sl@0: sl@0: /* Jump to here for an SQLITE_MISUSE error. sl@0: */ sl@0: abort_due_to_misuse: sl@0: rc = SQLITE_MISUSE; sl@0: /* Fall thru into abort_due_to_error */ sl@0: sl@0: /* Jump to here for any other kind of fatal error. The "rc" variable sl@0: ** should hold the error number. sl@0: */ sl@0: abort_due_to_error: sl@0: assert( p->zErrMsg==0 ); sl@0: if( db->mallocFailed ) rc = SQLITE_NOMEM; sl@0: if( rc!=SQLITE_IOERR_NOMEM ){ sl@0: sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); sl@0: } sl@0: goto vdbe_error_halt; sl@0: sl@0: /* Jump to here if the sqlite3_interrupt() API sets the interrupt sl@0: ** flag. sl@0: */ sl@0: abort_due_to_interrupt: sl@0: assert( db->u1.isInterrupted ); sl@0: rc = SQLITE_INTERRUPT; sl@0: p->rc = rc; sl@0: sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); sl@0: goto vdbe_error_halt; sl@0: }