000001 /* 000002 ** 2001 September 15 000003 ** 000004 ** The author disclaims copyright to this source code. In place of 000005 ** a legal notice, here is a blessing: 000006 ** 000007 ** May you do good and not evil. 000008 ** May you find forgiveness for yourself and forgive others. 000009 ** May you share freely, never taking more than you give. 000010 ** 000011 ************************************************************************* 000012 ** The code in this file implements the function that runs the 000013 ** bytecode of a prepared statement. 000014 ** 000015 ** Various scripts scan this source file in order to generate HTML 000016 ** documentation, headers files, or other derived files. The formatting 000017 ** of the code in this file is, therefore, important. See other comments 000018 ** in this file for details. If in doubt, do not deviate from existing 000019 ** commenting and indentation practices when changing or adding code. 000020 */ 000021 #include "sqliteInt.h" 000022 #include "vdbeInt.h" 000023 000024 /* 000025 ** Invoke this macro on memory cells just prior to changing the 000026 ** value of the cell. This macro verifies that shallow copies are 000027 ** not misused. A shallow copy of a string or blob just copies a 000028 ** pointer to the string or blob, not the content. If the original 000029 ** is changed while the copy is still in use, the string or blob might 000030 ** be changed out from under the copy. This macro verifies that nothing 000031 ** like that ever happens. 000032 */ 000033 #ifdef SQLITE_DEBUG 000034 # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M) 000035 #else 000036 # define memAboutToChange(P,M) 000037 #endif 000038 000039 /* 000040 ** The following global variable is incremented every time a cursor 000041 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test 000042 ** procedures use this information to make sure that indices are 000043 ** working correctly. This variable has no function other than to 000044 ** help verify the correct operation of the library. 000045 */ 000046 #ifdef SQLITE_TEST 000047 int sqlite3_search_count = 0; 000048 #endif 000049 000050 /* 000051 ** When this global variable is positive, it gets decremented once before 000052 ** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted 000053 ** field of the sqlite3 structure is set in order to simulate an interrupt. 000054 ** 000055 ** This facility is used for testing purposes only. It does not function 000056 ** in an ordinary build. 000057 */ 000058 #ifdef SQLITE_TEST 000059 int sqlite3_interrupt_count = 0; 000060 #endif 000061 000062 /* 000063 ** The next global variable is incremented each type the OP_Sort opcode 000064 ** is executed. The test procedures use this information to make sure that 000065 ** sorting is occurring or not occurring at appropriate times. This variable 000066 ** has no function other than to help verify the correct operation of the 000067 ** library. 000068 */ 000069 #ifdef SQLITE_TEST 000070 int sqlite3_sort_count = 0; 000071 #endif 000072 000073 /* 000074 ** The next global variable records the size of the largest MEM_Blob 000075 ** or MEM_Str that has been used by a VDBE opcode. The test procedures 000076 ** use this information to make sure that the zero-blob functionality 000077 ** is working correctly. This variable has no function other than to 000078 ** help verify the correct operation of the library. 000079 */ 000080 #ifdef SQLITE_TEST 000081 int sqlite3_max_blobsize = 0; 000082 static void updateMaxBlobsize(Mem *p){ 000083 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ 000084 sqlite3_max_blobsize = p->n; 000085 } 000086 } 000087 #endif 000088 000089 /* 000090 ** This macro evaluates to true if either the update hook or the preupdate 000091 ** hook are enabled for database connect DB. 000092 */ 000093 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 000094 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) 000095 #else 000096 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback) 000097 #endif 000098 000099 /* 000100 ** The next global variable is incremented each time the OP_Found opcode 000101 ** is executed. This is used to test whether or not the foreign key 000102 ** operation implemented using OP_FkIsZero is working. This variable 000103 ** has no function other than to help verify the correct operation of the 000104 ** library. 000105 */ 000106 #ifdef SQLITE_TEST 000107 int sqlite3_found_count = 0; 000108 #endif 000109 000110 /* 000111 ** Test a register to see if it exceeds the current maximum blob size. 000112 ** If it does, record the new maximum blob size. 000113 */ 000114 #if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE) 000115 # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) 000116 #else 000117 # define UPDATE_MAX_BLOBSIZE(P) 000118 #endif 000119 000120 #ifdef SQLITE_DEBUG 000121 /* This routine provides a convenient place to set a breakpoint during 000122 ** tracing with PRAGMA vdbe_trace=on. The breakpoint fires right after 000123 ** each opcode is printed. Variables "pc" (program counter) and pOp are 000124 ** available to add conditionals to the breakpoint. GDB example: 000125 ** 000126 ** break test_trace_breakpoint if pc=22 000127 ** 000128 ** Other useful labels for breakpoints include: 000129 ** test_addop_breakpoint(pc,pOp) 000130 ** sqlite3CorruptError(lineno) 000131 ** sqlite3MisuseError(lineno) 000132 ** sqlite3CantopenError(lineno) 000133 */ 000134 static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){ 000135 static u64 n = 0; 000136 (void)pc; 000137 (void)pOp; 000138 (void)v; 000139 n++; 000140 if( n==LARGEST_UINT64 ) abort(); /* So that n is used, preventing a warning */ 000141 } 000142 #endif 000143 000144 /* 000145 ** Invoke the VDBE coverage callback, if that callback is defined. This 000146 ** feature is used for test suite validation only and does not appear an 000147 ** production builds. 000148 ** 000149 ** M is the type of branch. I is the direction taken for this instance of 000150 ** the branch. 000151 ** 000152 ** M: 2 - two-way branch (I=0: fall-thru 1: jump ) 000153 ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL ) 000154 ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3) 000155 ** 000156 ** In other words, if M is 2, then I is either 0 (for fall-through) or 000157 ** 1 (for when the branch is taken). If M is 3, the I is 0 for an 000158 ** ordinary fall-through, I is 1 if the branch was taken, and I is 2 000159 ** if the result of comparison is NULL. For M=3, I=2 the jump may or 000160 ** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5. 000161 ** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2 000162 ** depending on if the operands are less than, equal, or greater than. 000163 ** 000164 ** iSrcLine is the source code line (from the __LINE__ macro) that 000165 ** generated the VDBE instruction combined with flag bits. The source 000166 ** code line number is in the lower 24 bits of iSrcLine and the upper 000167 ** 8 bytes are flags. The lower three bits of the flags indicate 000168 ** values for I that should never occur. For example, if the branch is 000169 ** always taken, the flags should be 0x05 since the fall-through and 000170 ** alternate branch are never taken. If a branch is never taken then 000171 ** flags should be 0x06 since only the fall-through approach is allowed. 000172 ** 000173 ** Bit 0x08 of the flags indicates an OP_Jump opcode that is only 000174 ** interested in equal or not-equal. In other words, I==0 and I==2 000175 ** should be treated as equivalent 000176 ** 000177 ** Since only a line number is retained, not the filename, this macro 000178 ** only works for amalgamation builds. But that is ok, since these macros 000179 ** should be no-ops except for special builds used to measure test coverage. 000180 */ 000181 #if !defined(SQLITE_VDBE_COVERAGE) 000182 # define VdbeBranchTaken(I,M) 000183 #else 000184 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M) 000185 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){ 000186 u8 mNever; 000187 assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */ 000188 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */ 000189 assert( I<M ); /* I can only be 2 if M is 3 or 4 */ 000190 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */ 000191 I = 1<<I; 000192 /* The upper 8 bits of iSrcLine are flags. The lower three bits of 000193 ** the flags indicate directions that the branch can never go. If 000194 ** a branch really does go in one of those directions, assert right 000195 ** away. */ 000196 mNever = iSrcLine >> 24; 000197 assert( (I & mNever)==0 ); 000198 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/ 000199 /* Invoke the branch coverage callback with three arguments: 000200 ** iSrcLine - the line number of the VdbeCoverage() macro, with 000201 ** flags removed. 000202 ** I - Mask of bits 0x07 indicating which cases are are 000203 ** fulfilled by this instance of the jump. 0x01 means 000204 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any 000205 ** impossible cases (ex: if the comparison is never NULL) 000206 ** are filled in automatically so that the coverage 000207 ** measurement logic does not flag those impossible cases 000208 ** as missed coverage. 000209 ** M - Type of jump. Same as M argument above 000210 */ 000211 I |= mNever; 000212 if( M==2 ) I |= 0x04; 000213 if( M==4 ){ 000214 I |= 0x08; 000215 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/ 000216 } 000217 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, 000218 iSrcLine&0xffffff, I, M); 000219 } 000220 #endif 000221 000222 /* 000223 ** An ephemeral string value (signified by the MEM_Ephem flag) contains 000224 ** a pointer to a dynamically allocated string where some other entity 000225 ** is responsible for deallocating that string. Because the register 000226 ** does not control the string, it might be deleted without the register 000227 ** knowing it. 000228 ** 000229 ** This routine converts an ephemeral string into a dynamically allocated 000230 ** string that the register itself controls. In other words, it 000231 ** converts an MEM_Ephem string into a string with P.z==P.zMalloc. 000232 */ 000233 #define Deephemeralize(P) \ 000234 if( ((P)->flags&MEM_Ephem)!=0 \ 000235 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} 000236 000237 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */ 000238 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER) 000239 000240 /* 000241 ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL 000242 ** if we run out of memory. 000243 */ 000244 static VdbeCursor *allocateCursor( 000245 Vdbe *p, /* The virtual machine */ 000246 int iCur, /* Index of the new VdbeCursor */ 000247 int nField, /* Number of fields in the table or index */ 000248 u8 eCurType /* Type of the new cursor */ 000249 ){ 000250 /* Find the memory cell that will be used to store the blob of memory 000251 ** required for this VdbeCursor structure. It is convenient to use a 000252 ** vdbe memory cell to manage the memory allocation required for a 000253 ** VdbeCursor structure for the following reasons: 000254 ** 000255 ** * Sometimes cursor numbers are used for a couple of different 000256 ** purposes in a vdbe program. The different uses might require 000257 ** different sized allocations. Memory cells provide growable 000258 ** allocations. 000259 ** 000260 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can 000261 ** be freed lazily via the sqlite3_release_memory() API. This 000262 ** minimizes the number of malloc calls made by the system. 000263 ** 000264 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from 000265 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1]. 000266 ** Cursor 2 is at Mem[p->nMem-2]. And so forth. 000267 */ 000268 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem; 000269 000270 int nByte; 000271 VdbeCursor *pCx = 0; 000272 nByte = 000273 ROUND8P(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + 000274 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0); 000275 000276 assert( iCur>=0 && iCur<p->nCursor ); 000277 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/ 000278 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]); 000279 p->apCsr[iCur] = 0; 000280 } 000281 000282 /* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure 000283 ** the pMem used to hold space for the cursor has enough storage available 000284 ** in pMem->zMalloc. But for the special case of the aMem[] entries used 000285 ** to hold cursors, it is faster to in-line the logic. */ 000286 assert( pMem->flags==MEM_Undefined ); 000287 assert( (pMem->flags & MEM_Dyn)==0 ); 000288 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc ); 000289 if( pMem->szMalloc<nByte ){ 000290 if( pMem->szMalloc>0 ){ 000291 sqlite3DbFreeNN(pMem->db, pMem->zMalloc); 000292 } 000293 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte); 000294 if( pMem->zMalloc==0 ){ 000295 pMem->szMalloc = 0; 000296 return 0; 000297 } 000298 pMem->szMalloc = nByte; 000299 } 000300 000301 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc; 000302 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor)); 000303 pCx->eCurType = eCurType; 000304 pCx->nField = nField; 000305 pCx->aOffset = &pCx->aType[nField]; 000306 if( eCurType==CURTYPE_BTREE ){ 000307 pCx->uc.pCursor = (BtCursor*) 000308 &pMem->z[ROUND8P(sizeof(VdbeCursor))+2*sizeof(u32)*nField]; 000309 sqlite3BtreeCursorZero(pCx->uc.pCursor); 000310 } 000311 return pCx; 000312 } 000313 000314 /* 000315 ** The string in pRec is known to look like an integer and to have a 000316 ** floating point value of rValue. Return true and set *piValue to the 000317 ** integer value if the string is in range to be an integer. Otherwise, 000318 ** return false. 000319 */ 000320 static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){ 000321 i64 iValue; 000322 iValue = sqlite3RealToI64(rValue); 000323 if( sqlite3RealSameAsInt(rValue,iValue) ){ 000324 *piValue = iValue; 000325 return 1; 000326 } 000327 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc); 000328 } 000329 000330 /* 000331 ** Try to convert a value into a numeric representation if we can 000332 ** do so without loss of information. In other words, if the string 000333 ** looks like a number, convert it into a number. If it does not 000334 ** look like a number, leave it alone. 000335 ** 000336 ** If the bTryForInt flag is true, then extra effort is made to give 000337 ** an integer representation. Strings that look like floating point 000338 ** values but which have no fractional component (example: '48.00') 000339 ** will have a MEM_Int representation when bTryForInt is true. 000340 ** 000341 ** If bTryForInt is false, then if the input string contains a decimal 000342 ** point or exponential notation, the result is only MEM_Real, even 000343 ** if there is an exact integer representation of the quantity. 000344 */ 000345 static void applyNumericAffinity(Mem *pRec, int bTryForInt){ 000346 double rValue; 000347 u8 enc = pRec->enc; 000348 int rc; 000349 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str ); 000350 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc); 000351 if( rc<=0 ) return; 000352 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){ 000353 pRec->flags |= MEM_Int; 000354 }else{ 000355 pRec->u.r = rValue; 000356 pRec->flags |= MEM_Real; 000357 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec); 000358 } 000359 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the 000360 ** string representation after computing a numeric equivalent, because the 000361 ** string representation might not be the canonical representation for the 000362 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */ 000363 pRec->flags &= ~MEM_Str; 000364 } 000365 000366 /* 000367 ** Processing is determine by the affinity parameter: 000368 ** 000369 ** SQLITE_AFF_INTEGER: 000370 ** SQLITE_AFF_REAL: 000371 ** SQLITE_AFF_NUMERIC: 000372 ** Try to convert pRec to an integer representation or a 000373 ** floating-point representation if an integer representation 000374 ** is not possible. Note that the integer representation is 000375 ** always preferred, even if the affinity is REAL, because 000376 ** an integer representation is more space efficient on disk. 000377 ** 000378 ** SQLITE_AFF_FLEXNUM: 000379 ** If the value is text, then try to convert it into a number of 000380 ** some kind (integer or real) but do not make any other changes. 000381 ** 000382 ** SQLITE_AFF_TEXT: 000383 ** Convert pRec to a text representation. 000384 ** 000385 ** SQLITE_AFF_BLOB: 000386 ** SQLITE_AFF_NONE: 000387 ** No-op. pRec is unchanged. 000388 */ 000389 static void applyAffinity( 000390 Mem *pRec, /* The value to apply affinity to */ 000391 char affinity, /* The affinity to be applied */ 000392 u8 enc /* Use this text encoding */ 000393 ){ 000394 if( affinity>=SQLITE_AFF_NUMERIC ){ 000395 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL 000396 || affinity==SQLITE_AFF_NUMERIC || affinity==SQLITE_AFF_FLEXNUM ); 000397 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/ 000398 if( (pRec->flags & (MEM_Real|MEM_IntReal))==0 ){ 000399 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); 000400 }else if( affinity<=SQLITE_AFF_REAL ){ 000401 sqlite3VdbeIntegerAffinity(pRec); 000402 } 000403 } 000404 }else if( affinity==SQLITE_AFF_TEXT ){ 000405 /* Only attempt the conversion to TEXT if there is an integer or real 000406 ** representation (blob and NULL do not get converted) but no string 000407 ** representation. It would be harmless to repeat the conversion if 000408 ** there is already a string rep, but it is pointless to waste those 000409 ** CPU cycles. */ 000410 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/ 000411 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){ 000412 testcase( pRec->flags & MEM_Int ); 000413 testcase( pRec->flags & MEM_Real ); 000414 testcase( pRec->flags & MEM_IntReal ); 000415 sqlite3VdbeMemStringify(pRec, enc, 1); 000416 } 000417 } 000418 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal); 000419 } 000420 } 000421 000422 /* 000423 ** Try to convert the type of a function argument or a result column 000424 ** into a numeric representation. Use either INTEGER or REAL whichever 000425 ** is appropriate. But only do the conversion if it is possible without 000426 ** loss of information and return the revised type of the argument. 000427 */ 000428 int sqlite3_value_numeric_type(sqlite3_value *pVal){ 000429 int eType = sqlite3_value_type(pVal); 000430 if( eType==SQLITE_TEXT ){ 000431 Mem *pMem = (Mem*)pVal; 000432 applyNumericAffinity(pMem, 0); 000433 eType = sqlite3_value_type(pVal); 000434 } 000435 return eType; 000436 } 000437 000438 /* 000439 ** Exported version of applyAffinity(). This one works on sqlite3_value*, 000440 ** not the internal Mem* type. 000441 */ 000442 void sqlite3ValueApplyAffinity( 000443 sqlite3_value *pVal, 000444 u8 affinity, 000445 u8 enc 000446 ){ 000447 applyAffinity((Mem *)pVal, affinity, enc); 000448 } 000449 000450 /* 000451 ** pMem currently only holds a string type (or maybe a BLOB that we can 000452 ** interpret as a string if we want to). Compute its corresponding 000453 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields 000454 ** accordingly. 000455 */ 000456 static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ 000457 int rc; 000458 sqlite3_int64 ix; 000459 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ); 000460 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); 000461 if( ExpandBlob(pMem) ){ 000462 pMem->u.i = 0; 000463 return MEM_Int; 000464 } 000465 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc); 000466 if( rc<=0 ){ 000467 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){ 000468 pMem->u.i = ix; 000469 return MEM_Int; 000470 }else{ 000471 return MEM_Real; 000472 } 000473 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){ 000474 pMem->u.i = ix; 000475 return MEM_Int; 000476 } 000477 return MEM_Real; 000478 } 000479 000480 /* 000481 ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or 000482 ** none. 000483 ** 000484 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. 000485 ** But it does set pMem->u.r and pMem->u.i appropriately. 000486 */ 000487 static u16 numericType(Mem *pMem){ 000488 assert( (pMem->flags & MEM_Null)==0 000489 || pMem->db==0 || pMem->db->mallocFailed ); 000490 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){ 000491 testcase( pMem->flags & MEM_Int ); 000492 testcase( pMem->flags & MEM_Real ); 000493 testcase( pMem->flags & MEM_IntReal ); 000494 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null); 000495 } 000496 assert( pMem->flags & (MEM_Str|MEM_Blob) ); 000497 testcase( pMem->flags & MEM_Str ); 000498 testcase( pMem->flags & MEM_Blob ); 000499 return computeNumericType(pMem); 000500 return 0; 000501 } 000502 000503 #ifdef SQLITE_DEBUG 000504 /* 000505 ** Write a nice string representation of the contents of cell pMem 000506 ** into buffer zBuf, length nBuf. 000507 */ 000508 void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){ 000509 int f = pMem->flags; 000510 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; 000511 if( f&MEM_Blob ){ 000512 int i; 000513 char c; 000514 if( f & MEM_Dyn ){ 000515 c = 'z'; 000516 assert( (f & (MEM_Static|MEM_Ephem))==0 ); 000517 }else if( f & MEM_Static ){ 000518 c = 't'; 000519 assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); 000520 }else if( f & MEM_Ephem ){ 000521 c = 'e'; 000522 assert( (f & (MEM_Static|MEM_Dyn))==0 ); 000523 }else{ 000524 c = 's'; 000525 } 000526 sqlite3_str_appendf(pStr, "%cx[", c); 000527 for(i=0; i<25 && i<pMem->n; i++){ 000528 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF)); 000529 } 000530 sqlite3_str_appendf(pStr, "|"); 000531 for(i=0; i<25 && i<pMem->n; i++){ 000532 char z = pMem->z[i]; 000533 sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z); 000534 } 000535 sqlite3_str_appendf(pStr,"]"); 000536 if( f & MEM_Zero ){ 000537 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero); 000538 } 000539 }else if( f & MEM_Str ){ 000540 int j; 000541 u8 c; 000542 if( f & MEM_Dyn ){ 000543 c = 'z'; 000544 assert( (f & (MEM_Static|MEM_Ephem))==0 ); 000545 }else if( f & MEM_Static ){ 000546 c = 't'; 000547 assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); 000548 }else if( f & MEM_Ephem ){ 000549 c = 'e'; 000550 assert( (f & (MEM_Static|MEM_Dyn))==0 ); 000551 }else{ 000552 c = 's'; 000553 } 000554 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n); 000555 for(j=0; j<25 && j<pMem->n; j++){ 000556 c = pMem->z[j]; 000557 sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.'); 000558 } 000559 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]); 000560 if( f & MEM_Term ){ 000561 sqlite3_str_appendf(pStr, "(0-term)"); 000562 } 000563 } 000564 } 000565 #endif 000566 000567 #ifdef SQLITE_DEBUG 000568 /* 000569 ** Print the value of a register for tracing purposes: 000570 */ 000571 static void memTracePrint(Mem *p){ 000572 if( p->flags & MEM_Undefined ){ 000573 printf(" undefined"); 000574 }else if( p->flags & MEM_Null ){ 000575 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL"); 000576 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ 000577 printf(" si:%lld", p->u.i); 000578 }else if( (p->flags & (MEM_IntReal))!=0 ){ 000579 printf(" ir:%lld", p->u.i); 000580 }else if( p->flags & MEM_Int ){ 000581 printf(" i:%lld", p->u.i); 000582 #ifndef SQLITE_OMIT_FLOATING_POINT 000583 }else if( p->flags & MEM_Real ){ 000584 printf(" r:%.17g", p->u.r); 000585 #endif 000586 }else if( sqlite3VdbeMemIsRowSet(p) ){ 000587 printf(" (rowset)"); 000588 }else{ 000589 StrAccum acc; 000590 char zBuf[1000]; 000591 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); 000592 sqlite3VdbeMemPrettyPrint(p, &acc); 000593 printf(" %s", sqlite3StrAccumFinish(&acc)); 000594 } 000595 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype); 000596 } 000597 static void registerTrace(int iReg, Mem *p){ 000598 printf("R[%d] = ", iReg); 000599 memTracePrint(p); 000600 if( p->pScopyFrom ){ 000601 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg])); 000602 } 000603 printf("\n"); 000604 sqlite3VdbeCheckMemInvariants(p); 000605 } 000606 /**/ void sqlite3PrintMem(Mem *pMem){ 000607 memTracePrint(pMem); 000608 printf("\n"); 000609 fflush(stdout); 000610 } 000611 #endif 000612 000613 #ifdef SQLITE_DEBUG 000614 /* 000615 ** Show the values of all registers in the virtual machine. Used for 000616 ** interactive debugging. 000617 */ 000618 void sqlite3VdbeRegisterDump(Vdbe *v){ 000619 int i; 000620 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i); 000621 } 000622 #endif /* SQLITE_DEBUG */ 000623 000624 000625 #ifdef SQLITE_DEBUG 000626 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) 000627 #else 000628 # define REGISTER_TRACE(R,M) 000629 #endif 000630 000631 #ifndef NDEBUG 000632 /* 000633 ** This function is only called from within an assert() expression. It 000634 ** checks that the sqlite3.nTransaction variable is correctly set to 000635 ** the number of non-transaction savepoints currently in the 000636 ** linked list starting at sqlite3.pSavepoint. 000637 ** 000638 ** Usage: 000639 ** 000640 ** assert( checkSavepointCount(db) ); 000641 */ 000642 static int checkSavepointCount(sqlite3 *db){ 000643 int n = 0; 000644 Savepoint *p; 000645 for(p=db->pSavepoint; p; p=p->pNext) n++; 000646 assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); 000647 return 1; 000648 } 000649 #endif 000650 000651 /* 000652 ** Return the register of pOp->p2 after first preparing it to be 000653 ** overwritten with an integer value. 000654 */ 000655 static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){ 000656 sqlite3VdbeMemSetNull(pOut); 000657 pOut->flags = MEM_Int; 000658 return pOut; 000659 } 000660 static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ 000661 Mem *pOut; 000662 assert( pOp->p2>0 ); 000663 assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); 000664 pOut = &p->aMem[pOp->p2]; 000665 memAboutToChange(p, pOut); 000666 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/ 000667 return out2PrereleaseWithClear(pOut); 000668 }else{ 000669 pOut->flags = MEM_Int; 000670 return pOut; 000671 } 000672 } 000673 000674 /* 000675 ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning 000676 ** with pOp->p3. Return the hash. 000677 */ 000678 static u64 filterHash(const Mem *aMem, const Op *pOp){ 000679 int i, mx; 000680 u64 h = 0; 000681 000682 assert( pOp->p4type==P4_INT32 ); 000683 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){ 000684 const Mem *p = &aMem[i]; 000685 if( p->flags & (MEM_Int|MEM_IntReal) ){ 000686 h += p->u.i; 000687 }else if( p->flags & MEM_Real ){ 000688 h += sqlite3VdbeIntValue(p); 000689 }else if( p->flags & (MEM_Str|MEM_Blob) ){ 000690 /* All strings have the same hash and all blobs have the same hash, 000691 ** though, at least, those hashes are different from each other and 000692 ** from NULL. */ 000693 h += 4093 + (p->flags & (MEM_Str|MEM_Blob)); 000694 } 000695 } 000696 return h; 000697 } 000698 000699 000700 /* 000701 ** For OP_Column, factor out the case where content is loaded from 000702 ** overflow pages, so that the code to implement this case is separate 000703 ** the common case where all content fits on the page. Factoring out 000704 ** the code reduces register pressure and helps the common case 000705 ** to run faster. 000706 */ 000707 static SQLITE_NOINLINE int vdbeColumnFromOverflow( 000708 VdbeCursor *pC, /* The BTree cursor from which we are reading */ 000709 int iCol, /* The column to read */ 000710 int t, /* The serial-type code for the column value */ 000711 i64 iOffset, /* Offset to the start of the content value */ 000712 u32 cacheStatus, /* Current Vdbe.cacheCtr value */ 000713 u32 colCacheCtr, /* Current value of the column cache counter */ 000714 Mem *pDest /* Store the value into this register. */ 000715 ){ 000716 int rc; 000717 sqlite3 *db = pDest->db; 000718 int encoding = pDest->enc; 000719 int len = sqlite3VdbeSerialTypeLen(t); 000720 assert( pC->eCurType==CURTYPE_BTREE ); 000721 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) return SQLITE_TOOBIG; 000722 if( len > 4000 && pC->pKeyInfo==0 ){ 000723 /* Cache large column values that are on overflow pages using 000724 ** an RCStr (reference counted string) so that if they are reloaded, 000725 ** that do not have to be copied a second time. The overhead of 000726 ** creating and managing the cache is such that this is only 000727 ** profitable for larger TEXT and BLOB values. 000728 ** 000729 ** Only do this on table-btrees so that writes to index-btrees do not 000730 ** need to clear the cache. This buys performance in the common case 000731 ** in exchange for generality. 000732 */ 000733 VdbeTxtBlbCache *pCache; 000734 char *pBuf; 000735 if( pC->colCache==0 ){ 000736 pC->pCache = sqlite3DbMallocZero(db, sizeof(VdbeTxtBlbCache) ); 000737 if( pC->pCache==0 ) return SQLITE_NOMEM; 000738 pC->colCache = 1; 000739 } 000740 pCache = pC->pCache; 000741 if( pCache->pCValue==0 000742 || pCache->iCol!=iCol 000743 || pCache->cacheStatus!=cacheStatus 000744 || pCache->colCacheCtr!=colCacheCtr 000745 || pCache->iOffset!=sqlite3BtreeOffset(pC->uc.pCursor) 000746 ){ 000747 if( pCache->pCValue ) sqlite3RCStrUnref(pCache->pCValue); 000748 pBuf = pCache->pCValue = sqlite3RCStrNew( len+3 ); 000749 if( pBuf==0 ) return SQLITE_NOMEM; 000750 rc = sqlite3BtreePayload(pC->uc.pCursor, iOffset, len, pBuf); 000751 if( rc ) return rc; 000752 pBuf[len] = 0; 000753 pBuf[len+1] = 0; 000754 pBuf[len+2] = 0; 000755 pCache->iCol = iCol; 000756 pCache->cacheStatus = cacheStatus; 000757 pCache->colCacheCtr = colCacheCtr; 000758 pCache->iOffset = sqlite3BtreeOffset(pC->uc.pCursor); 000759 }else{ 000760 pBuf = pCache->pCValue; 000761 } 000762 assert( t>=12 ); 000763 sqlite3RCStrRef(pBuf); 000764 if( t&1 ){ 000765 rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, encoding, 000766 sqlite3RCStrUnref); 000767 pDest->flags |= MEM_Term; 000768 }else{ 000769 rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, 0, 000770 sqlite3RCStrUnref); 000771 } 000772 }else{ 000773 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, iOffset, len, pDest); 000774 if( rc ) return rc; 000775 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); 000776 if( (t&1)!=0 && encoding==SQLITE_UTF8 ){ 000777 pDest->z[len] = 0; 000778 pDest->flags |= MEM_Term; 000779 } 000780 } 000781 pDest->flags &= ~MEM_Ephem; 000782 return rc; 000783 } 000784 000785 000786 /* 000787 ** Return the symbolic name for the data type of a pMem 000788 */ 000789 static const char *vdbeMemTypeName(Mem *pMem){ 000790 static const char *azTypes[] = { 000791 /* SQLITE_INTEGER */ "INT", 000792 /* SQLITE_FLOAT */ "REAL", 000793 /* SQLITE_TEXT */ "TEXT", 000794 /* SQLITE_BLOB */ "BLOB", 000795 /* SQLITE_NULL */ "NULL" 000796 }; 000797 return azTypes[sqlite3_value_type(pMem)-1]; 000798 } 000799 000800 /* 000801 ** Execute as much of a VDBE program as we can. 000802 ** This is the core of sqlite3_step(). 000803 */ 000804 int sqlite3VdbeExec( 000805 Vdbe *p /* The VDBE */ 000806 ){ 000807 Op *aOp = p->aOp; /* Copy of p->aOp */ 000808 Op *pOp = aOp; /* Current operation */ 000809 #ifdef SQLITE_DEBUG 000810 Op *pOrigOp; /* Value of pOp at the top of the loop */ 000811 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */ 000812 u8 iCompareIsInit = 0; /* iCompare is initialized */ 000813 #endif 000814 int rc = SQLITE_OK; /* Value to return */ 000815 sqlite3 *db = p->db; /* The database */ 000816 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ 000817 u8 encoding = ENC(db); /* The database encoding */ 000818 int iCompare = 0; /* Result of last comparison */ 000819 u64 nVmStep = 0; /* Number of virtual machine steps */ 000820 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 000821 u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */ 000822 #endif 000823 Mem *aMem = p->aMem; /* Copy of p->aMem */ 000824 Mem *pIn1 = 0; /* 1st input operand */ 000825 Mem *pIn2 = 0; /* 2nd input operand */ 000826 Mem *pIn3 = 0; /* 3rd input operand */ 000827 Mem *pOut = 0; /* Output operand */ 000828 u32 colCacheCtr = 0; /* Column cache counter */ 000829 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE) 000830 u64 *pnCycle = 0; 000831 int bStmtScanStatus = IS_STMT_SCANSTATUS(db)!=0; 000832 #endif 000833 /*** INSERT STACK UNION HERE ***/ 000834 000835 assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */ 000836 if( DbMaskNonZero(p->lockMask) ){ 000837 sqlite3VdbeEnter(p); 000838 } 000839 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 000840 if( db->xProgress ){ 000841 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; 000842 assert( 0 < db->nProgressOps ); 000843 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); 000844 }else{ 000845 nProgressLimit = LARGEST_UINT64; 000846 } 000847 #endif 000848 if( p->rc==SQLITE_NOMEM ){ 000849 /* This happens if a malloc() inside a call to sqlite3_column_text() or 000850 ** sqlite3_column_text16() failed. */ 000851 goto no_mem; 000852 } 000853 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); 000854 testcase( p->rc!=SQLITE_OK ); 000855 p->rc = SQLITE_OK; 000856 assert( p->bIsReader || p->readOnly!=0 ); 000857 p->iCurrentTime = 0; 000858 assert( p->explain==0 ); 000859 db->busyHandler.nBusy = 0; 000860 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt; 000861 sqlite3VdbeIOTraceSql(p); 000862 #ifdef SQLITE_DEBUG 000863 sqlite3BeginBenignMalloc(); 000864 if( p->pc==0 000865 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0 000866 ){ 000867 int i; 000868 int once = 1; 000869 sqlite3VdbePrintSql(p); 000870 if( p->db->flags & SQLITE_VdbeListing ){ 000871 printf("VDBE Program Listing:\n"); 000872 for(i=0; i<p->nOp; i++){ 000873 sqlite3VdbePrintOp(stdout, i, &aOp[i]); 000874 } 000875 } 000876 if( p->db->flags & SQLITE_VdbeEQP ){ 000877 for(i=0; i<p->nOp; i++){ 000878 if( aOp[i].opcode==OP_Explain ){ 000879 if( once ) printf("VDBE Query Plan:\n"); 000880 printf("%s\n", aOp[i].p4.z); 000881 once = 0; 000882 } 000883 } 000884 } 000885 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); 000886 } 000887 sqlite3EndBenignMalloc(); 000888 #endif 000889 for(pOp=&aOp[p->pc]; 1; pOp++){ 000890 /* Errors are detected by individual opcodes, with an immediate 000891 ** jumps to abort_due_to_error. */ 000892 assert( rc==SQLITE_OK ); 000893 000894 assert( pOp>=aOp && pOp<&aOp[p->nOp]); 000895 nVmStep++; 000896 000897 #if defined(VDBE_PROFILE) 000898 pOp->nExec++; 000899 pnCycle = &pOp->nCycle; 000900 if( sqlite3NProfileCnt==0 ) *pnCycle -= sqlite3Hwtime(); 000901 #elif defined(SQLITE_ENABLE_STMT_SCANSTATUS) 000902 if( bStmtScanStatus ){ 000903 pOp->nExec++; 000904 pnCycle = &pOp->nCycle; 000905 *pnCycle -= sqlite3Hwtime(); 000906 } 000907 #endif 000908 000909 /* Only allow tracing if SQLITE_DEBUG is defined. 000910 */ 000911 #ifdef SQLITE_DEBUG 000912 if( db->flags & SQLITE_VdbeTrace ){ 000913 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp); 000914 test_trace_breakpoint((int)(pOp - aOp),pOp,p); 000915 } 000916 #endif 000917 000918 000919 /* Check to see if we need to simulate an interrupt. This only happens 000920 ** if we have a special test build. 000921 */ 000922 #ifdef SQLITE_TEST 000923 if( sqlite3_interrupt_count>0 ){ 000924 sqlite3_interrupt_count--; 000925 if( sqlite3_interrupt_count==0 ){ 000926 sqlite3_interrupt(db); 000927 } 000928 } 000929 #endif 000930 000931 /* Sanity checking on other operands */ 000932 #ifdef SQLITE_DEBUG 000933 { 000934 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode]; 000935 if( (opProperty & OPFLG_IN1)!=0 ){ 000936 assert( pOp->p1>0 ); 000937 assert( pOp->p1<=(p->nMem+1 - p->nCursor) ); 000938 assert( memIsValid(&aMem[pOp->p1]) ); 000939 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); 000940 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); 000941 } 000942 if( (opProperty & OPFLG_IN2)!=0 ){ 000943 assert( pOp->p2>0 ); 000944 assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); 000945 assert( memIsValid(&aMem[pOp->p2]) ); 000946 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); 000947 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); 000948 } 000949 if( (opProperty & OPFLG_IN3)!=0 ){ 000950 assert( pOp->p3>0 ); 000951 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 000952 assert( memIsValid(&aMem[pOp->p3]) ); 000953 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); 000954 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); 000955 } 000956 if( (opProperty & OPFLG_OUT2)!=0 ){ 000957 assert( pOp->p2>0 ); 000958 assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); 000959 memAboutToChange(p, &aMem[pOp->p2]); 000960 } 000961 if( (opProperty & OPFLG_OUT3)!=0 ){ 000962 assert( pOp->p3>0 ); 000963 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 000964 memAboutToChange(p, &aMem[pOp->p3]); 000965 } 000966 } 000967 #endif 000968 #ifdef SQLITE_DEBUG 000969 pOrigOp = pOp; 000970 #endif 000971 000972 switch( pOp->opcode ){ 000973 000974 /***************************************************************************** 000975 ** What follows is a massive switch statement where each case implements a 000976 ** separate instruction in the virtual machine. If we follow the usual 000977 ** indentation conventions, each case should be indented by 6 spaces. But 000978 ** that is a lot of wasted space on the left margin. So the code within 000979 ** the switch statement will break with convention and be flush-left. Another 000980 ** big comment (similar to this one) will mark the point in the code where 000981 ** we transition back to normal indentation. 000982 ** 000983 ** The formatting of each case is important. The makefile for SQLite 000984 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this 000985 ** file looking for lines that begin with "case OP_". The opcodes.h files 000986 ** will be filled with #defines that give unique integer values to each 000987 ** opcode and the opcodes.c file is filled with an array of strings where 000988 ** each string is the symbolic name for the corresponding opcode. If the 000989 ** case statement is followed by a comment of the form "/# same as ... #/" 000990 ** that comment is used to determine the particular value of the opcode. 000991 ** 000992 ** Other keywords in the comment that follows each case are used to 000993 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. 000994 ** Keywords include: in1, in2, in3, out2, out3. See 000995 ** the mkopcodeh.awk script for additional information. 000996 ** 000997 ** Documentation about VDBE opcodes is generated by scanning this file 000998 ** for lines of that contain "Opcode:". That line and all subsequent 000999 ** comment lines are used in the generation of the opcode.html documentation 001000 ** file. 001001 ** 001002 ** SUMMARY: 001003 ** 001004 ** Formatting is important to scripts that scan this file. 001005 ** Do not deviate from the formatting style currently in use. 001006 ** 001007 *****************************************************************************/ 001008 001009 /* Opcode: Goto * P2 * * * 001010 ** 001011 ** An unconditional jump to address P2. 001012 ** The next instruction executed will be 001013 ** the one at index P2 from the beginning of 001014 ** the program. 001015 ** 001016 ** The P1 parameter is not actually used by this opcode. However, it 001017 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell 001018 ** that this Goto is the bottom of a loop and that the lines from P2 down 001019 ** to the current line should be indented for EXPLAIN output. 001020 */ 001021 case OP_Goto: { /* jump */ 001022 001023 #ifdef SQLITE_DEBUG 001024 /* In debugging mode, when the p5 flags is set on an OP_Goto, that 001025 ** means we should really jump back to the preceding OP_ReleaseReg 001026 ** instruction. */ 001027 if( pOp->p5 ){ 001028 assert( pOp->p2 < (int)(pOp - aOp) ); 001029 assert( pOp->p2 > 1 ); 001030 pOp = &aOp[pOp->p2 - 2]; 001031 assert( pOp[1].opcode==OP_ReleaseReg ); 001032 goto check_for_interrupt; 001033 } 001034 #endif 001035 001036 jump_to_p2_and_check_for_interrupt: 001037 pOp = &aOp[pOp->p2 - 1]; 001038 001039 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev, 001040 ** OP_VNext, or OP_SorterNext) all jump here upon 001041 ** completion. Check to see if sqlite3_interrupt() has been called 001042 ** or if the progress callback needs to be invoked. 001043 ** 001044 ** This code uses unstructured "goto" statements and does not look clean. 001045 ** But that is not due to sloppy coding habits. The code is written this 001046 ** way for performance, to avoid having to run the interrupt and progress 001047 ** checks on every opcode. This helps sqlite3_step() to run about 1.5% 001048 ** faster according to "valgrind --tool=cachegrind" */ 001049 check_for_interrupt: 001050 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt; 001051 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 001052 /* Call the progress callback if it is configured and the required number 001053 ** of VDBE ops have been executed (either since this invocation of 001054 ** sqlite3VdbeExec() or since last time the progress callback was called). 001055 ** If the progress callback returns non-zero, exit the virtual machine with 001056 ** a return code SQLITE_ABORT. 001057 */ 001058 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){ 001059 assert( db->nProgressOps!=0 ); 001060 nProgressLimit += db->nProgressOps; 001061 if( db->xProgress(db->pProgressArg) ){ 001062 nProgressLimit = LARGEST_UINT64; 001063 rc = SQLITE_INTERRUPT; 001064 goto abort_due_to_error; 001065 } 001066 } 001067 #endif 001068 001069 break; 001070 } 001071 001072 /* Opcode: Gosub P1 P2 * * * 001073 ** 001074 ** Write the current address onto register P1 001075 ** and then jump to address P2. 001076 */ 001077 case OP_Gosub: { /* jump */ 001078 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 001079 pIn1 = &aMem[pOp->p1]; 001080 assert( VdbeMemDynamic(pIn1)==0 ); 001081 memAboutToChange(p, pIn1); 001082 pIn1->flags = MEM_Int; 001083 pIn1->u.i = (int)(pOp-aOp); 001084 REGISTER_TRACE(pOp->p1, pIn1); 001085 goto jump_to_p2_and_check_for_interrupt; 001086 } 001087 001088 /* Opcode: Return P1 P2 P3 * * 001089 ** 001090 ** Jump to the address stored in register P1. If P1 is a return address 001091 ** register, then this accomplishes a return from a subroutine. 001092 ** 001093 ** If P3 is 1, then the jump is only taken if register P1 holds an integer 001094 ** values, otherwise execution falls through to the next opcode, and the 001095 ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an 001096 ** integer or else an assert() is raised. P3 should be set to 1 when 001097 ** this opcode is used in combination with OP_BeginSubrtn, and set to 0 001098 ** otherwise. 001099 ** 001100 ** The value in register P1 is unchanged by this opcode. 001101 ** 001102 ** P2 is not used by the byte-code engine. However, if P2 is positive 001103 ** and also less than the current address, then the "EXPLAIN" output 001104 ** formatter in the CLI will indent all opcodes from the P2 opcode up 001105 ** to be not including the current Return. P2 should be the first opcode 001106 ** in the subroutine from which this opcode is returning. Thus the P2 001107 ** value is a byte-code indentation hint. See tag-20220407a in 001108 ** wherecode.c and shell.c. 001109 */ 001110 case OP_Return: { /* in1 */ 001111 pIn1 = &aMem[pOp->p1]; 001112 if( pIn1->flags & MEM_Int ){ 001113 if( pOp->p3 ){ VdbeBranchTaken(1, 2); } 001114 pOp = &aOp[pIn1->u.i]; 001115 }else if( ALWAYS(pOp->p3) ){ 001116 VdbeBranchTaken(0, 2); 001117 } 001118 break; 001119 } 001120 001121 /* Opcode: InitCoroutine P1 P2 P3 * * 001122 ** 001123 ** Set up register P1 so that it will Yield to the coroutine 001124 ** located at address P3. 001125 ** 001126 ** If P2!=0 then the coroutine implementation immediately follows 001127 ** this opcode. So jump over the coroutine implementation to 001128 ** address P2. 001129 ** 001130 ** See also: EndCoroutine 001131 */ 001132 case OP_InitCoroutine: { /* jump */ 001133 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 001134 assert( pOp->p2>=0 && pOp->p2<p->nOp ); 001135 assert( pOp->p3>=0 && pOp->p3<p->nOp ); 001136 pOut = &aMem[pOp->p1]; 001137 assert( !VdbeMemDynamic(pOut) ); 001138 pOut->u.i = pOp->p3 - 1; 001139 pOut->flags = MEM_Int; 001140 if( pOp->p2==0 ) break; 001141 001142 /* Most jump operations do a goto to this spot in order to update 001143 ** the pOp pointer. */ 001144 jump_to_p2: 001145 assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */ 001146 assert( pOp->p2<p->nOp ); /* Jumps must be in range */ 001147 pOp = &aOp[pOp->p2 - 1]; 001148 break; 001149 } 001150 001151 /* Opcode: EndCoroutine P1 * * * * 001152 ** 001153 ** The instruction at the address in register P1 is a Yield. 001154 ** Jump to the P2 parameter of that Yield. 001155 ** After the jump, register P1 becomes undefined. 001156 ** 001157 ** See also: InitCoroutine 001158 */ 001159 case OP_EndCoroutine: { /* in1 */ 001160 VdbeOp *pCaller; 001161 pIn1 = &aMem[pOp->p1]; 001162 assert( pIn1->flags==MEM_Int ); 001163 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp ); 001164 pCaller = &aOp[pIn1->u.i]; 001165 assert( pCaller->opcode==OP_Yield ); 001166 assert( pCaller->p2>=0 && pCaller->p2<p->nOp ); 001167 pOp = &aOp[pCaller->p2 - 1]; 001168 pIn1->flags = MEM_Undefined; 001169 break; 001170 } 001171 001172 /* Opcode: Yield P1 P2 * * * 001173 ** 001174 ** Swap the program counter with the value in register P1. This 001175 ** has the effect of yielding to a coroutine. 001176 ** 001177 ** If the coroutine that is launched by this instruction ends with 001178 ** Yield or Return then continue to the next instruction. But if 001179 ** the coroutine launched by this instruction ends with 001180 ** EndCoroutine, then jump to P2 rather than continuing with the 001181 ** next instruction. 001182 ** 001183 ** See also: InitCoroutine 001184 */ 001185 case OP_Yield: { /* in1, jump */ 001186 int pcDest; 001187 pIn1 = &aMem[pOp->p1]; 001188 assert( VdbeMemDynamic(pIn1)==0 ); 001189 pIn1->flags = MEM_Int; 001190 pcDest = (int)pIn1->u.i; 001191 pIn1->u.i = (int)(pOp - aOp); 001192 REGISTER_TRACE(pOp->p1, pIn1); 001193 pOp = &aOp[pcDest]; 001194 break; 001195 } 001196 001197 /* Opcode: HaltIfNull P1 P2 P3 P4 P5 001198 ** Synopsis: if r[P3]=null halt 001199 ** 001200 ** Check the value in register P3. If it is NULL then Halt using 001201 ** parameter P1, P2, and P4 as if this were a Halt instruction. If the 001202 ** value in register P3 is not NULL, then this routine is a no-op. 001203 ** The P5 parameter should be 1. 001204 */ 001205 case OP_HaltIfNull: { /* in3 */ 001206 pIn3 = &aMem[pOp->p3]; 001207 #ifdef SQLITE_DEBUG 001208 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } 001209 #endif 001210 if( (pIn3->flags & MEM_Null)==0 ) break; 001211 /* Fall through into OP_Halt */ 001212 /* no break */ deliberate_fall_through 001213 } 001214 001215 /* Opcode: Halt P1 P2 * P4 P5 001216 ** 001217 ** Exit immediately. All open cursors, etc are closed 001218 ** automatically. 001219 ** 001220 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), 001221 ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). 001222 ** For errors, it can be some other value. If P1!=0 then P2 will determine 001223 ** whether or not to rollback the current transaction. Do not rollback 001224 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, 001225 ** then back out all changes that have occurred during this execution of the 001226 ** VDBE, but do not rollback the transaction. 001227 ** 001228 ** If P4 is not null then it is an error message string. 001229 ** 001230 ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string. 001231 ** 001232 ** 0: (no change) 001233 ** 1: NOT NULL constraint failed: P4 001234 ** 2: UNIQUE constraint failed: P4 001235 ** 3: CHECK constraint failed: P4 001236 ** 4: FOREIGN KEY constraint failed: P4 001237 ** 001238 ** If P5 is not zero and P4 is NULL, then everything after the ":" is 001239 ** omitted. 001240 ** 001241 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of 001242 ** every program. So a jump past the last instruction of the program 001243 ** is the same as executing Halt. 001244 */ 001245 case OP_Halt: { 001246 VdbeFrame *pFrame; 001247 int pcx; 001248 001249 #ifdef SQLITE_DEBUG 001250 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } 001251 #endif 001252 001253 /* A deliberately coded "OP_Halt SQLITE_INTERNAL * * * *" opcode indicates 001254 ** something is wrong with the code generator. Raise an assertion in order 001255 ** to bring this to the attention of fuzzers and other testing tools. */ 001256 assert( pOp->p1!=SQLITE_INTERNAL ); 001257 001258 if( p->pFrame && pOp->p1==SQLITE_OK ){ 001259 /* Halt the sub-program. Return control to the parent frame. */ 001260 pFrame = p->pFrame; 001261 p->pFrame = pFrame->pParent; 001262 p->nFrame--; 001263 sqlite3VdbeSetChanges(db, p->nChange); 001264 pcx = sqlite3VdbeFrameRestore(pFrame); 001265 if( pOp->p2==OE_Ignore ){ 001266 /* Instruction pcx is the OP_Program that invoked the sub-program 001267 ** currently being halted. If the p2 instruction of this OP_Halt 001268 ** instruction is set to OE_Ignore, then the sub-program is throwing 001269 ** an IGNORE exception. In this case jump to the address specified 001270 ** as the p2 of the calling OP_Program. */ 001271 pcx = p->aOp[pcx].p2-1; 001272 } 001273 aOp = p->aOp; 001274 aMem = p->aMem; 001275 pOp = &aOp[pcx]; 001276 break; 001277 } 001278 p->rc = pOp->p1; 001279 p->errorAction = (u8)pOp->p2; 001280 assert( pOp->p5<=4 ); 001281 if( p->rc ){ 001282 if( pOp->p5 ){ 001283 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", 001284 "FOREIGN KEY" }; 001285 testcase( pOp->p5==1 ); 001286 testcase( pOp->p5==2 ); 001287 testcase( pOp->p5==3 ); 001288 testcase( pOp->p5==4 ); 001289 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]); 001290 if( pOp->p4.z ){ 001291 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z); 001292 } 001293 }else{ 001294 sqlite3VdbeError(p, "%s", pOp->p4.z); 001295 } 001296 pcx = (int)(pOp - aOp); 001297 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg); 001298 } 001299 rc = sqlite3VdbeHalt(p); 001300 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); 001301 if( rc==SQLITE_BUSY ){ 001302 p->rc = SQLITE_BUSY; 001303 }else{ 001304 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); 001305 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); 001306 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; 001307 } 001308 goto vdbe_return; 001309 } 001310 001311 /* Opcode: Integer P1 P2 * * * 001312 ** Synopsis: r[P2]=P1 001313 ** 001314 ** The 32-bit integer value P1 is written into register P2. 001315 */ 001316 case OP_Integer: { /* out2 */ 001317 pOut = out2Prerelease(p, pOp); 001318 pOut->u.i = pOp->p1; 001319 break; 001320 } 001321 001322 /* Opcode: Int64 * P2 * P4 * 001323 ** Synopsis: r[P2]=P4 001324 ** 001325 ** P4 is a pointer to a 64-bit integer value. 001326 ** Write that value into register P2. 001327 */ 001328 case OP_Int64: { /* out2 */ 001329 pOut = out2Prerelease(p, pOp); 001330 assert( pOp->p4.pI64!=0 ); 001331 pOut->u.i = *pOp->p4.pI64; 001332 break; 001333 } 001334 001335 #ifndef SQLITE_OMIT_FLOATING_POINT 001336 /* Opcode: Real * P2 * P4 * 001337 ** Synopsis: r[P2]=P4 001338 ** 001339 ** P4 is a pointer to a 64-bit floating point value. 001340 ** Write that value into register P2. 001341 */ 001342 case OP_Real: { /* same as TK_FLOAT, out2 */ 001343 pOut = out2Prerelease(p, pOp); 001344 pOut->flags = MEM_Real; 001345 assert( !sqlite3IsNaN(*pOp->p4.pReal) ); 001346 pOut->u.r = *pOp->p4.pReal; 001347 break; 001348 } 001349 #endif 001350 001351 /* Opcode: String8 * P2 * P4 * 001352 ** Synopsis: r[P2]='P4' 001353 ** 001354 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 001355 ** into a String opcode before it is executed for the first time. During 001356 ** this transformation, the length of string P4 is computed and stored 001357 ** as the P1 parameter. 001358 */ 001359 case OP_String8: { /* same as TK_STRING, out2 */ 001360 assert( pOp->p4.z!=0 ); 001361 pOut = out2Prerelease(p, pOp); 001362 pOp->p1 = sqlite3Strlen30(pOp->p4.z); 001363 001364 #ifndef SQLITE_OMIT_UTF16 001365 if( encoding!=SQLITE_UTF8 ){ 001366 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); 001367 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG ); 001368 if( rc ) goto too_big; 001369 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; 001370 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); 001371 assert( VdbeMemDynamic(pOut)==0 ); 001372 pOut->szMalloc = 0; 001373 pOut->flags |= MEM_Static; 001374 if( pOp->p4type==P4_DYNAMIC ){ 001375 sqlite3DbFree(db, pOp->p4.z); 001376 } 001377 pOp->p4type = P4_DYNAMIC; 001378 pOp->p4.z = pOut->z; 001379 pOp->p1 = pOut->n; 001380 } 001381 #endif 001382 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 001383 goto too_big; 001384 } 001385 pOp->opcode = OP_String; 001386 assert( rc==SQLITE_OK ); 001387 /* Fall through to the next case, OP_String */ 001388 /* no break */ deliberate_fall_through 001389 } 001390 001391 /* Opcode: String P1 P2 P3 P4 P5 001392 ** Synopsis: r[P2]='P4' (len=P1) 001393 ** 001394 ** The string value P4 of length P1 (bytes) is stored in register P2. 001395 ** 001396 ** If P3 is not zero and the content of register P3 is equal to P5, then 001397 ** the datatype of the register P2 is converted to BLOB. The content is 001398 ** the same sequence of bytes, it is merely interpreted as a BLOB instead 001399 ** of a string, as if it had been CAST. In other words: 001400 ** 001401 ** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB) 001402 */ 001403 case OP_String: { /* out2 */ 001404 assert( pOp->p4.z!=0 ); 001405 pOut = out2Prerelease(p, pOp); 001406 pOut->flags = MEM_Str|MEM_Static|MEM_Term; 001407 pOut->z = pOp->p4.z; 001408 pOut->n = pOp->p1; 001409 pOut->enc = encoding; 001410 UPDATE_MAX_BLOBSIZE(pOut); 001411 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS 001412 if( pOp->p3>0 ){ 001413 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 001414 pIn3 = &aMem[pOp->p3]; 001415 assert( pIn3->flags & MEM_Int ); 001416 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term; 001417 } 001418 #endif 001419 break; 001420 } 001421 001422 /* Opcode: BeginSubrtn * P2 * * * 001423 ** Synopsis: r[P2]=NULL 001424 ** 001425 ** Mark the beginning of a subroutine that can be entered in-line 001426 ** or that can be called using OP_Gosub. The subroutine should 001427 ** be terminated by an OP_Return instruction that has a P1 operand that 001428 ** is the same as the P2 operand to this opcode and that has P3 set to 1. 001429 ** If the subroutine is entered in-line, then the OP_Return will simply 001430 ** fall through. But if the subroutine is entered using OP_Gosub, then 001431 ** the OP_Return will jump back to the first instruction after the OP_Gosub. 001432 ** 001433 ** This routine works by loading a NULL into the P2 register. When the 001434 ** return address register contains a NULL, the OP_Return instruction is 001435 ** a no-op that simply falls through to the next instruction (assuming that 001436 ** the OP_Return opcode has a P3 value of 1). Thus if the subroutine is 001437 ** entered in-line, then the OP_Return will cause in-line execution to 001438 ** continue. But if the subroutine is entered via OP_Gosub, then the 001439 ** OP_Return will cause a return to the address following the OP_Gosub. 001440 ** 001441 ** This opcode is identical to OP_Null. It has a different name 001442 ** only to make the byte code easier to read and verify. 001443 */ 001444 /* Opcode: Null P1 P2 P3 * * 001445 ** Synopsis: r[P2..P3]=NULL 001446 ** 001447 ** Write a NULL into registers P2. If P3 greater than P2, then also write 001448 ** NULL into register P3 and every register in between P2 and P3. If P3 001449 ** is less than P2 (typically P3 is zero) then only register P2 is 001450 ** set to NULL. 001451 ** 001452 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that 001453 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on 001454 ** OP_Ne or OP_Eq. 001455 */ 001456 case OP_BeginSubrtn: 001457 case OP_Null: { /* out2 */ 001458 int cnt; 001459 u16 nullFlag; 001460 pOut = out2Prerelease(p, pOp); 001461 cnt = pOp->p3-pOp->p2; 001462 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 001463 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; 001464 pOut->n = 0; 001465 #ifdef SQLITE_DEBUG 001466 pOut->uTemp = 0; 001467 #endif 001468 while( cnt>0 ){ 001469 pOut++; 001470 memAboutToChange(p, pOut); 001471 sqlite3VdbeMemSetNull(pOut); 001472 pOut->flags = nullFlag; 001473 pOut->n = 0; 001474 cnt--; 001475 } 001476 break; 001477 } 001478 001479 /* Opcode: SoftNull P1 * * * * 001480 ** Synopsis: r[P1]=NULL 001481 ** 001482 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord 001483 ** instruction, but do not free any string or blob memory associated with 001484 ** the register, so that if the value was a string or blob that was 001485 ** previously copied using OP_SCopy, the copies will continue to be valid. 001486 */ 001487 case OP_SoftNull: { 001488 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 001489 pOut = &aMem[pOp->p1]; 001490 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null; 001491 break; 001492 } 001493 001494 /* Opcode: Blob P1 P2 * P4 * 001495 ** Synopsis: r[P2]=P4 (len=P1) 001496 ** 001497 ** P4 points to a blob of data P1 bytes long. Store this 001498 ** blob in register P2. If P4 is a NULL pointer, then construct 001499 ** a zero-filled blob that is P1 bytes long in P2. 001500 */ 001501 case OP_Blob: { /* out2 */ 001502 assert( pOp->p1 <= SQLITE_MAX_LENGTH ); 001503 pOut = out2Prerelease(p, pOp); 001504 if( pOp->p4.z==0 ){ 001505 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1); 001506 if( sqlite3VdbeMemExpandBlob(pOut) ) goto no_mem; 001507 }else{ 001508 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); 001509 } 001510 pOut->enc = encoding; 001511 UPDATE_MAX_BLOBSIZE(pOut); 001512 break; 001513 } 001514 001515 /* Opcode: Variable P1 P2 * P4 * 001516 ** Synopsis: r[P2]=parameter(P1,P4) 001517 ** 001518 ** Transfer the values of bound parameter P1 into register P2 001519 ** 001520 ** If the parameter is named, then its name appears in P4. 001521 ** The P4 value is used by sqlite3_bind_parameter_name(). 001522 */ 001523 case OP_Variable: { /* out2 */ 001524 Mem *pVar; /* Value being transferred */ 001525 001526 assert( pOp->p1>0 && pOp->p1<=p->nVar ); 001527 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) ); 001528 pVar = &p->aVar[pOp->p1 - 1]; 001529 if( sqlite3VdbeMemTooBig(pVar) ){ 001530 goto too_big; 001531 } 001532 pOut = &aMem[pOp->p2]; 001533 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut); 001534 memcpy(pOut, pVar, MEMCELLSIZE); 001535 pOut->flags &= ~(MEM_Dyn|MEM_Ephem); 001536 pOut->flags |= MEM_Static|MEM_FromBind; 001537 UPDATE_MAX_BLOBSIZE(pOut); 001538 break; 001539 } 001540 001541 /* Opcode: Move P1 P2 P3 * * 001542 ** Synopsis: r[P2@P3]=r[P1@P3] 001543 ** 001544 ** Move the P3 values in register P1..P1+P3-1 over into 001545 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are 001546 ** left holding a NULL. It is an error for register ranges 001547 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error 001548 ** for P3 to be less than 1. 001549 */ 001550 case OP_Move: { 001551 int n; /* Number of registers left to copy */ 001552 int p1; /* Register to copy from */ 001553 int p2; /* Register to copy to */ 001554 001555 n = pOp->p3; 001556 p1 = pOp->p1; 001557 p2 = pOp->p2; 001558 assert( n>0 && p1>0 && p2>0 ); 001559 assert( p1+n<=p2 || p2+n<=p1 ); 001560 001561 pIn1 = &aMem[p1]; 001562 pOut = &aMem[p2]; 001563 do{ 001564 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] ); 001565 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] ); 001566 assert( memIsValid(pIn1) ); 001567 memAboutToChange(p, pOut); 001568 sqlite3VdbeMemMove(pOut, pIn1); 001569 #ifdef SQLITE_DEBUG 001570 pIn1->pScopyFrom = 0; 001571 { int i; 001572 for(i=1; i<p->nMem; i++){ 001573 if( aMem[i].pScopyFrom==pIn1 ){ 001574 aMem[i].pScopyFrom = pOut; 001575 } 001576 } 001577 } 001578 #endif 001579 Deephemeralize(pOut); 001580 REGISTER_TRACE(p2++, pOut); 001581 pIn1++; 001582 pOut++; 001583 }while( --n ); 001584 break; 001585 } 001586 001587 /* Opcode: Copy P1 P2 P3 * P5 001588 ** Synopsis: r[P2@P3+1]=r[P1@P3+1] 001589 ** 001590 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. 001591 ** 001592 ** If the 0x0002 bit of P5 is set then also clear the MEM_Subtype flag in the 001593 ** destination. The 0x0001 bit of P5 indicates that this Copy opcode cannot 001594 ** be merged. The 0x0001 bit is used by the query planner and does not 001595 ** come into play during query execution. 001596 ** 001597 ** This instruction makes a deep copy of the value. A duplicate 001598 ** is made of any string or blob constant. See also OP_SCopy. 001599 */ 001600 case OP_Copy: { 001601 int n; 001602 001603 n = pOp->p3; 001604 pIn1 = &aMem[pOp->p1]; 001605 pOut = &aMem[pOp->p2]; 001606 assert( pOut!=pIn1 ); 001607 while( 1 ){ 001608 memAboutToChange(p, pOut); 001609 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); 001610 Deephemeralize(pOut); 001611 if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){ 001612 pOut->flags &= ~MEM_Subtype; 001613 } 001614 #ifdef SQLITE_DEBUG 001615 pOut->pScopyFrom = 0; 001616 #endif 001617 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut); 001618 if( (n--)==0 ) break; 001619 pOut++; 001620 pIn1++; 001621 } 001622 break; 001623 } 001624 001625 /* Opcode: SCopy P1 P2 * * * 001626 ** Synopsis: r[P2]=r[P1] 001627 ** 001628 ** Make a shallow copy of register P1 into register P2. 001629 ** 001630 ** This instruction makes a shallow copy of the value. If the value 001631 ** is a string or blob, then the copy is only a pointer to the 001632 ** original and hence if the original changes so will the copy. 001633 ** Worse, if the original is deallocated, the copy becomes invalid. 001634 ** Thus the program must guarantee that the original will not change 001635 ** during the lifetime of the copy. Use OP_Copy to make a complete 001636 ** copy. 001637 */ 001638 case OP_SCopy: { /* out2 */ 001639 pIn1 = &aMem[pOp->p1]; 001640 pOut = &aMem[pOp->p2]; 001641 assert( pOut!=pIn1 ); 001642 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); 001643 #ifdef SQLITE_DEBUG 001644 pOut->pScopyFrom = pIn1; 001645 pOut->mScopyFlags = pIn1->flags; 001646 #endif 001647 break; 001648 } 001649 001650 /* Opcode: IntCopy P1 P2 * * * 001651 ** Synopsis: r[P2]=r[P1] 001652 ** 001653 ** Transfer the integer value held in register P1 into register P2. 001654 ** 001655 ** This is an optimized version of SCopy that works only for integer 001656 ** values. 001657 */ 001658 case OP_IntCopy: { /* out2 */ 001659 pIn1 = &aMem[pOp->p1]; 001660 assert( (pIn1->flags & MEM_Int)!=0 ); 001661 pOut = &aMem[pOp->p2]; 001662 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i); 001663 break; 001664 } 001665 001666 /* Opcode: FkCheck * * * * * 001667 ** 001668 ** Halt with an SQLITE_CONSTRAINT error if there are any unresolved 001669 ** foreign key constraint violations. If there are no foreign key 001670 ** constraint violations, this is a no-op. 001671 ** 001672 ** FK constraint violations are also checked when the prepared statement 001673 ** exits. This opcode is used to raise foreign key constraint errors prior 001674 ** to returning results such as a row change count or the result of a 001675 ** RETURNING clause. 001676 */ 001677 case OP_FkCheck: { 001678 if( (rc = sqlite3VdbeCheckFk(p,0))!=SQLITE_OK ){ 001679 goto abort_due_to_error; 001680 } 001681 break; 001682 } 001683 001684 /* Opcode: ResultRow P1 P2 * * * 001685 ** Synopsis: output=r[P1@P2] 001686 ** 001687 ** The registers P1 through P1+P2-1 contain a single row of 001688 ** results. This opcode causes the sqlite3_step() call to terminate 001689 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt 001690 ** structure to provide access to the r(P1)..r(P1+P2-1) values as 001691 ** the result row. 001692 */ 001693 case OP_ResultRow: { 001694 assert( p->nResColumn==pOp->p2 ); 001695 assert( pOp->p1>0 || CORRUPT_DB ); 001696 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); 001697 001698 p->cacheCtr = (p->cacheCtr + 2)|1; 001699 p->pResultRow = &aMem[pOp->p1]; 001700 #ifdef SQLITE_DEBUG 001701 { 001702 Mem *pMem = p->pResultRow; 001703 int i; 001704 for(i=0; i<pOp->p2; i++){ 001705 assert( memIsValid(&pMem[i]) ); 001706 REGISTER_TRACE(pOp->p1+i, &pMem[i]); 001707 /* The registers in the result will not be used again when the 001708 ** prepared statement restarts. This is because sqlite3_column() 001709 ** APIs might have caused type conversions of made other changes to 001710 ** the register values. Therefore, we can go ahead and break any 001711 ** OP_SCopy dependencies. */ 001712 pMem[i].pScopyFrom = 0; 001713 } 001714 } 001715 #endif 001716 if( db->mallocFailed ) goto no_mem; 001717 if( db->mTrace & SQLITE_TRACE_ROW ){ 001718 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0); 001719 } 001720 p->pc = (int)(pOp - aOp) + 1; 001721 rc = SQLITE_ROW; 001722 goto vdbe_return; 001723 } 001724 001725 /* Opcode: Concat P1 P2 P3 * * 001726 ** Synopsis: r[P3]=r[P2]+r[P1] 001727 ** 001728 ** Add the text in register P1 onto the end of the text in 001729 ** register P2 and store the result in register P3. 001730 ** If either the P1 or P2 text are NULL then store NULL in P3. 001731 ** 001732 ** P3 = P2 || P1 001733 ** 001734 ** It is illegal for P1 and P3 to be the same register. Sometimes, 001735 ** if P3 is the same register as P2, the implementation is able 001736 ** to avoid a memcpy(). 001737 */ 001738 case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ 001739 i64 nByte; /* Total size of the output string or blob */ 001740 u16 flags1; /* Initial flags for P1 */ 001741 u16 flags2; /* Initial flags for P2 */ 001742 001743 pIn1 = &aMem[pOp->p1]; 001744 pIn2 = &aMem[pOp->p2]; 001745 pOut = &aMem[pOp->p3]; 001746 testcase( pOut==pIn2 ); 001747 assert( pIn1!=pOut ); 001748 flags1 = pIn1->flags; 001749 testcase( flags1 & MEM_Null ); 001750 testcase( pIn2->flags & MEM_Null ); 001751 if( (flags1 | pIn2->flags) & MEM_Null ){ 001752 sqlite3VdbeMemSetNull(pOut); 001753 break; 001754 } 001755 if( (flags1 & (MEM_Str|MEM_Blob))==0 ){ 001756 if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem; 001757 flags1 = pIn1->flags & ~MEM_Str; 001758 }else if( (flags1 & MEM_Zero)!=0 ){ 001759 if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem; 001760 flags1 = pIn1->flags & ~MEM_Str; 001761 } 001762 flags2 = pIn2->flags; 001763 if( (flags2 & (MEM_Str|MEM_Blob))==0 ){ 001764 if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem; 001765 flags2 = pIn2->flags & ~MEM_Str; 001766 }else if( (flags2 & MEM_Zero)!=0 ){ 001767 if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem; 001768 flags2 = pIn2->flags & ~MEM_Str; 001769 } 001770 nByte = pIn1->n + pIn2->n; 001771 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 001772 goto too_big; 001773 } 001774 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ 001775 goto no_mem; 001776 } 001777 MemSetTypeFlag(pOut, MEM_Str); 001778 if( pOut!=pIn2 ){ 001779 memcpy(pOut->z, pIn2->z, pIn2->n); 001780 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) ); 001781 pIn2->flags = flags2; 001782 } 001783 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); 001784 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); 001785 pIn1->flags = flags1; 001786 if( encoding>SQLITE_UTF8 ) nByte &= ~1; 001787 pOut->z[nByte]=0; 001788 pOut->z[nByte+1] = 0; 001789 pOut->flags |= MEM_Term; 001790 pOut->n = (int)nByte; 001791 pOut->enc = encoding; 001792 UPDATE_MAX_BLOBSIZE(pOut); 001793 break; 001794 } 001795 001796 /* Opcode: Add P1 P2 P3 * * 001797 ** Synopsis: r[P3]=r[P1]+r[P2] 001798 ** 001799 ** Add the value in register P1 to the value in register P2 001800 ** and store the result in register P3. 001801 ** If either input is NULL, the result is NULL. 001802 */ 001803 /* Opcode: Multiply P1 P2 P3 * * 001804 ** Synopsis: r[P3]=r[P1]*r[P2] 001805 ** 001806 ** 001807 ** Multiply the value in register P1 by the value in register P2 001808 ** and store the result in register P3. 001809 ** If either input is NULL, the result is NULL. 001810 */ 001811 /* Opcode: Subtract P1 P2 P3 * * 001812 ** Synopsis: r[P3]=r[P2]-r[P1] 001813 ** 001814 ** Subtract the value in register P1 from the value in register P2 001815 ** and store the result in register P3. 001816 ** If either input is NULL, the result is NULL. 001817 */ 001818 /* Opcode: Divide P1 P2 P3 * * 001819 ** Synopsis: r[P3]=r[P2]/r[P1] 001820 ** 001821 ** Divide the value in register P1 by the value in register P2 001822 ** and store the result in register P3 (P3=P2/P1). If the value in 001823 ** register P1 is zero, then the result is NULL. If either input is 001824 ** NULL, the result is NULL. 001825 */ 001826 /* Opcode: Remainder P1 P2 P3 * * 001827 ** Synopsis: r[P3]=r[P2]%r[P1] 001828 ** 001829 ** Compute the remainder after integer register P2 is divided by 001830 ** register P1 and store the result in register P3. 001831 ** If the value in register P1 is zero the result is NULL. 001832 ** If either operand is NULL, the result is NULL. 001833 */ 001834 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ 001835 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ 001836 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ 001837 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ 001838 case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ 001839 u16 type1; /* Numeric type of left operand */ 001840 u16 type2; /* Numeric type of right operand */ 001841 i64 iA; /* Integer value of left operand */ 001842 i64 iB; /* Integer value of right operand */ 001843 double rA; /* Real value of left operand */ 001844 double rB; /* Real value of right operand */ 001845 001846 pIn1 = &aMem[pOp->p1]; 001847 type1 = pIn1->flags; 001848 pIn2 = &aMem[pOp->p2]; 001849 type2 = pIn2->flags; 001850 pOut = &aMem[pOp->p3]; 001851 if( (type1 & type2 & MEM_Int)!=0 ){ 001852 int_math: 001853 iA = pIn1->u.i; 001854 iB = pIn2->u.i; 001855 switch( pOp->opcode ){ 001856 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; 001857 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; 001858 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break; 001859 case OP_Divide: { 001860 if( iA==0 ) goto arithmetic_result_is_null; 001861 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math; 001862 iB /= iA; 001863 break; 001864 } 001865 default: { 001866 if( iA==0 ) goto arithmetic_result_is_null; 001867 if( iA==-1 ) iA = 1; 001868 iB %= iA; 001869 break; 001870 } 001871 } 001872 pOut->u.i = iB; 001873 MemSetTypeFlag(pOut, MEM_Int); 001874 }else if( ((type1 | type2) & MEM_Null)!=0 ){ 001875 goto arithmetic_result_is_null; 001876 }else{ 001877 type1 = numericType(pIn1); 001878 type2 = numericType(pIn2); 001879 if( (type1 & type2 & MEM_Int)!=0 ) goto int_math; 001880 fp_math: 001881 rA = sqlite3VdbeRealValue(pIn1); 001882 rB = sqlite3VdbeRealValue(pIn2); 001883 switch( pOp->opcode ){ 001884 case OP_Add: rB += rA; break; 001885 case OP_Subtract: rB -= rA; break; 001886 case OP_Multiply: rB *= rA; break; 001887 case OP_Divide: { 001888 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ 001889 if( rA==(double)0 ) goto arithmetic_result_is_null; 001890 rB /= rA; 001891 break; 001892 } 001893 default: { 001894 iA = sqlite3VdbeIntValue(pIn1); 001895 iB = sqlite3VdbeIntValue(pIn2); 001896 if( iA==0 ) goto arithmetic_result_is_null; 001897 if( iA==-1 ) iA = 1; 001898 rB = (double)(iB % iA); 001899 break; 001900 } 001901 } 001902 #ifdef SQLITE_OMIT_FLOATING_POINT 001903 pOut->u.i = rB; 001904 MemSetTypeFlag(pOut, MEM_Int); 001905 #else 001906 if( sqlite3IsNaN(rB) ){ 001907 goto arithmetic_result_is_null; 001908 } 001909 pOut->u.r = rB; 001910 MemSetTypeFlag(pOut, MEM_Real); 001911 #endif 001912 } 001913 break; 001914 001915 arithmetic_result_is_null: 001916 sqlite3VdbeMemSetNull(pOut); 001917 break; 001918 } 001919 001920 /* Opcode: CollSeq P1 * * P4 001921 ** 001922 ** P4 is a pointer to a CollSeq object. If the next call to a user function 001923 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will 001924 ** be returned. This is used by the built-in min(), max() and nullif() 001925 ** functions. 001926 ** 001927 ** If P1 is not zero, then it is a register that a subsequent min() or 001928 ** max() aggregate will set to 1 if the current row is not the minimum or 001929 ** maximum. The P1 register is initialized to 0 by this instruction. 001930 ** 001931 ** The interface used by the implementation of the aforementioned functions 001932 ** to retrieve the collation sequence set by this opcode is not available 001933 ** publicly. Only built-in functions have access to this feature. 001934 */ 001935 case OP_CollSeq: { 001936 assert( pOp->p4type==P4_COLLSEQ ); 001937 if( pOp->p1 ){ 001938 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); 001939 } 001940 break; 001941 } 001942 001943 /* Opcode: BitAnd P1 P2 P3 * * 001944 ** Synopsis: r[P3]=r[P1]&r[P2] 001945 ** 001946 ** Take the bit-wise AND of the values in register P1 and P2 and 001947 ** store the result in register P3. 001948 ** If either input is NULL, the result is NULL. 001949 */ 001950 /* Opcode: BitOr P1 P2 P3 * * 001951 ** Synopsis: r[P3]=r[P1]|r[P2] 001952 ** 001953 ** Take the bit-wise OR of the values in register P1 and P2 and 001954 ** store the result in register P3. 001955 ** If either input is NULL, the result is NULL. 001956 */ 001957 /* Opcode: ShiftLeft P1 P2 P3 * * 001958 ** Synopsis: r[P3]=r[P2]<<r[P1] 001959 ** 001960 ** Shift the integer value in register P2 to the left by the 001961 ** number of bits specified by the integer in register P1. 001962 ** Store the result in register P3. 001963 ** If either input is NULL, the result is NULL. 001964 */ 001965 /* Opcode: ShiftRight P1 P2 P3 * * 001966 ** Synopsis: r[P3]=r[P2]>>r[P1] 001967 ** 001968 ** Shift the integer value in register P2 to the right by the 001969 ** number of bits specified by the integer in register P1. 001970 ** Store the result in register P3. 001971 ** If either input is NULL, the result is NULL. 001972 */ 001973 case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ 001974 case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ 001975 case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ 001976 case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ 001977 i64 iA; 001978 u64 uA; 001979 i64 iB; 001980 u8 op; 001981 001982 pIn1 = &aMem[pOp->p1]; 001983 pIn2 = &aMem[pOp->p2]; 001984 pOut = &aMem[pOp->p3]; 001985 if( (pIn1->flags | pIn2->flags) & MEM_Null ){ 001986 sqlite3VdbeMemSetNull(pOut); 001987 break; 001988 } 001989 iA = sqlite3VdbeIntValue(pIn2); 001990 iB = sqlite3VdbeIntValue(pIn1); 001991 op = pOp->opcode; 001992 if( op==OP_BitAnd ){ 001993 iA &= iB; 001994 }else if( op==OP_BitOr ){ 001995 iA |= iB; 001996 }else if( iB!=0 ){ 001997 assert( op==OP_ShiftRight || op==OP_ShiftLeft ); 001998 001999 /* If shifting by a negative amount, shift in the other direction */ 002000 if( iB<0 ){ 002001 assert( OP_ShiftRight==OP_ShiftLeft+1 ); 002002 op = 2*OP_ShiftLeft + 1 - op; 002003 iB = iB>(-64) ? -iB : 64; 002004 } 002005 002006 if( iB>=64 ){ 002007 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1; 002008 }else{ 002009 memcpy(&uA, &iA, sizeof(uA)); 002010 if( op==OP_ShiftLeft ){ 002011 uA <<= iB; 002012 }else{ 002013 uA >>= iB; 002014 /* Sign-extend on a right shift of a negative number */ 002015 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB); 002016 } 002017 memcpy(&iA, &uA, sizeof(iA)); 002018 } 002019 } 002020 pOut->u.i = iA; 002021 MemSetTypeFlag(pOut, MEM_Int); 002022 break; 002023 } 002024 002025 /* Opcode: AddImm P1 P2 * * * 002026 ** Synopsis: r[P1]=r[P1]+P2 002027 ** 002028 ** Add the constant P2 to the value in register P1. 002029 ** The result is always an integer. 002030 ** 002031 ** To force any register to be an integer, just add 0. 002032 */ 002033 case OP_AddImm: { /* in1 */ 002034 pIn1 = &aMem[pOp->p1]; 002035 memAboutToChange(p, pIn1); 002036 sqlite3VdbeMemIntegerify(pIn1); 002037 *(u64*)&pIn1->u.i += (u64)pOp->p2; 002038 break; 002039 } 002040 002041 /* Opcode: MustBeInt P1 P2 * * * 002042 ** 002043 ** Force the value in register P1 to be an integer. If the value 002044 ** in P1 is not an integer and cannot be converted into an integer 002045 ** without data loss, then jump immediately to P2, or if P2==0 002046 ** raise an SQLITE_MISMATCH exception. 002047 */ 002048 case OP_MustBeInt: { /* jump, in1 */ 002049 pIn1 = &aMem[pOp->p1]; 002050 if( (pIn1->flags & MEM_Int)==0 ){ 002051 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); 002052 if( (pIn1->flags & MEM_Int)==0 ){ 002053 VdbeBranchTaken(1, 2); 002054 if( pOp->p2==0 ){ 002055 rc = SQLITE_MISMATCH; 002056 goto abort_due_to_error; 002057 }else{ 002058 goto jump_to_p2; 002059 } 002060 } 002061 } 002062 VdbeBranchTaken(0, 2); 002063 MemSetTypeFlag(pIn1, MEM_Int); 002064 break; 002065 } 002066 002067 #ifndef SQLITE_OMIT_FLOATING_POINT 002068 /* Opcode: RealAffinity P1 * * * * 002069 ** 002070 ** If register P1 holds an integer convert it to a real value. 002071 ** 002072 ** This opcode is used when extracting information from a column that 002073 ** has REAL affinity. Such column values may still be stored as 002074 ** integers, for space efficiency, but after extraction we want them 002075 ** to have only a real value. 002076 */ 002077 case OP_RealAffinity: { /* in1 */ 002078 pIn1 = &aMem[pOp->p1]; 002079 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){ 002080 testcase( pIn1->flags & MEM_Int ); 002081 testcase( pIn1->flags & MEM_IntReal ); 002082 sqlite3VdbeMemRealify(pIn1); 002083 REGISTER_TRACE(pOp->p1, pIn1); 002084 } 002085 break; 002086 } 002087 #endif 002088 002089 #ifndef SQLITE_OMIT_CAST 002090 /* Opcode: Cast P1 P2 * * * 002091 ** Synopsis: affinity(r[P1]) 002092 ** 002093 ** Force the value in register P1 to be the type defined by P2. 002094 ** 002095 ** <ul> 002096 ** <li> P2=='A' → BLOB 002097 ** <li> P2=='B' → TEXT 002098 ** <li> P2=='C' → NUMERIC 002099 ** <li> P2=='D' → INTEGER 002100 ** <li> P2=='E' → REAL 002101 ** </ul> 002102 ** 002103 ** A NULL value is not changed by this routine. It remains NULL. 002104 */ 002105 case OP_Cast: { /* in1 */ 002106 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL ); 002107 testcase( pOp->p2==SQLITE_AFF_TEXT ); 002108 testcase( pOp->p2==SQLITE_AFF_BLOB ); 002109 testcase( pOp->p2==SQLITE_AFF_NUMERIC ); 002110 testcase( pOp->p2==SQLITE_AFF_INTEGER ); 002111 testcase( pOp->p2==SQLITE_AFF_REAL ); 002112 pIn1 = &aMem[pOp->p1]; 002113 memAboutToChange(p, pIn1); 002114 rc = ExpandBlob(pIn1); 002115 if( rc ) goto abort_due_to_error; 002116 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); 002117 if( rc ) goto abort_due_to_error; 002118 UPDATE_MAX_BLOBSIZE(pIn1); 002119 REGISTER_TRACE(pOp->p1, pIn1); 002120 break; 002121 } 002122 #endif /* SQLITE_OMIT_CAST */ 002123 002124 /* Opcode: Eq P1 P2 P3 P4 P5 002125 ** Synopsis: IF r[P3]==r[P1] 002126 ** 002127 ** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then 002128 ** jump to address P2. 002129 ** 002130 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - 002131 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 002132 ** to coerce both inputs according to this affinity before the 002133 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric 002134 ** affinity is used. Note that the affinity conversions are stored 002135 ** back into the input registers P1 and P3. So this opcode can cause 002136 ** persistent changes to registers P1 and P3. 002137 ** 002138 ** Once any conversions have taken place, and neither value is NULL, 002139 ** the values are compared. If both values are blobs then memcmp() is 002140 ** used to determine the results of the comparison. If both values 002141 ** are text, then the appropriate collating function specified in 002142 ** P4 is used to do the comparison. If P4 is not specified then 002143 ** memcmp() is used to compare text string. If both values are 002144 ** numeric, then a numeric comparison is used. If the two values 002145 ** are of different types, then numbers are considered less than 002146 ** strings and strings are considered less than blobs. 002147 ** 002148 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either 002149 ** true or false and is never NULL. If both operands are NULL then the result 002150 ** of comparison is true. If either operand is NULL then the result is false. 002151 ** If neither operand is NULL the result is the same as it would be if 002152 ** the SQLITE_NULLEQ flag were omitted from P5. 002153 ** 002154 ** This opcode saves the result of comparison for use by the new 002155 ** OP_Jump opcode. 002156 */ 002157 /* Opcode: Ne P1 P2 P3 P4 P5 002158 ** Synopsis: IF r[P3]!=r[P1] 002159 ** 002160 ** This works just like the Eq opcode except that the jump is taken if 002161 ** the operands in registers P1 and P3 are not equal. See the Eq opcode for 002162 ** additional information. 002163 */ 002164 /* Opcode: Lt P1 P2 P3 P4 P5 002165 ** Synopsis: IF r[P3]<r[P1] 002166 ** 002167 ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then 002168 ** jump to address P2. 002169 ** 002170 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or 002171 ** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL 002172 ** bit is clear then fall through if either operand is NULL. 002173 ** 002174 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - 002175 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 002176 ** to coerce both inputs according to this affinity before the 002177 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric 002178 ** affinity is used. Note that the affinity conversions are stored 002179 ** back into the input registers P1 and P3. So this opcode can cause 002180 ** persistent changes to registers P1 and P3. 002181 ** 002182 ** Once any conversions have taken place, and neither value is NULL, 002183 ** the values are compared. If both values are blobs then memcmp() is 002184 ** used to determine the results of the comparison. If both values 002185 ** are text, then the appropriate collating function specified in 002186 ** P4 is used to do the comparison. If P4 is not specified then 002187 ** memcmp() is used to compare text string. If both values are 002188 ** numeric, then a numeric comparison is used. If the two values 002189 ** are of different types, then numbers are considered less than 002190 ** strings and strings are considered less than blobs. 002191 ** 002192 ** This opcode saves the result of comparison for use by the new 002193 ** OP_Jump opcode. 002194 */ 002195 /* Opcode: Le P1 P2 P3 P4 P5 002196 ** Synopsis: IF r[P3]<=r[P1] 002197 ** 002198 ** This works just like the Lt opcode except that the jump is taken if 002199 ** the content of register P3 is less than or equal to the content of 002200 ** register P1. See the Lt opcode for additional information. 002201 */ 002202 /* Opcode: Gt P1 P2 P3 P4 P5 002203 ** Synopsis: IF r[P3]>r[P1] 002204 ** 002205 ** This works just like the Lt opcode except that the jump is taken if 002206 ** the content of register P3 is greater than the content of 002207 ** register P1. See the Lt opcode for additional information. 002208 */ 002209 /* Opcode: Ge P1 P2 P3 P4 P5 002210 ** Synopsis: IF r[P3]>=r[P1] 002211 ** 002212 ** This works just like the Lt opcode except that the jump is taken if 002213 ** the content of register P3 is greater than or equal to the content of 002214 ** register P1. See the Lt opcode for additional information. 002215 */ 002216 case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ 002217 case OP_Ne: /* same as TK_NE, jump, in1, in3 */ 002218 case OP_Lt: /* same as TK_LT, jump, in1, in3 */ 002219 case OP_Le: /* same as TK_LE, jump, in1, in3 */ 002220 case OP_Gt: /* same as TK_GT, jump, in1, in3 */ 002221 case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ 002222 int res, res2; /* Result of the comparison of pIn1 against pIn3 */ 002223 char affinity; /* Affinity to use for comparison */ 002224 u16 flags1; /* Copy of initial value of pIn1->flags */ 002225 u16 flags3; /* Copy of initial value of pIn3->flags */ 002226 002227 pIn1 = &aMem[pOp->p1]; 002228 pIn3 = &aMem[pOp->p3]; 002229 flags1 = pIn1->flags; 002230 flags3 = pIn3->flags; 002231 if( (flags1 & flags3 & MEM_Int)!=0 ){ 002232 /* Common case of comparison of two integers */ 002233 if( pIn3->u.i > pIn1->u.i ){ 002234 if( sqlite3aGTb[pOp->opcode] ){ 002235 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); 002236 goto jump_to_p2; 002237 } 002238 iCompare = +1; 002239 VVA_ONLY( iCompareIsInit = 1; ) 002240 }else if( pIn3->u.i < pIn1->u.i ){ 002241 if( sqlite3aLTb[pOp->opcode] ){ 002242 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); 002243 goto jump_to_p2; 002244 } 002245 iCompare = -1; 002246 VVA_ONLY( iCompareIsInit = 1; ) 002247 }else{ 002248 if( sqlite3aEQb[pOp->opcode] ){ 002249 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); 002250 goto jump_to_p2; 002251 } 002252 iCompare = 0; 002253 VVA_ONLY( iCompareIsInit = 1; ) 002254 } 002255 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3); 002256 break; 002257 } 002258 if( (flags1 | flags3)&MEM_Null ){ 002259 /* One or both operands are NULL */ 002260 if( pOp->p5 & SQLITE_NULLEQ ){ 002261 /* If SQLITE_NULLEQ is set (which will only happen if the operator is 002262 ** OP_Eq or OP_Ne) then take the jump or not depending on whether 002263 ** or not both operands are null. 002264 */ 002265 assert( (flags1 & MEM_Cleared)==0 ); 002266 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB ); 002267 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 ); 002268 if( (flags1&flags3&MEM_Null)!=0 002269 && (flags3&MEM_Cleared)==0 002270 ){ 002271 res = 0; /* Operands are equal */ 002272 }else{ 002273 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */ 002274 } 002275 }else{ 002276 /* SQLITE_NULLEQ is clear and at least one operand is NULL, 002277 ** then the result is always NULL. 002278 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. 002279 */ 002280 VdbeBranchTaken(2,3); 002281 if( pOp->p5 & SQLITE_JUMPIFNULL ){ 002282 goto jump_to_p2; 002283 } 002284 iCompare = 1; /* Operands are not equal */ 002285 VVA_ONLY( iCompareIsInit = 1; ) 002286 break; 002287 } 002288 }else{ 002289 /* Neither operand is NULL and we couldn't do the special high-speed 002290 ** integer comparison case. So do a general-case comparison. */ 002291 affinity = pOp->p5 & SQLITE_AFF_MASK; 002292 if( affinity>=SQLITE_AFF_NUMERIC ){ 002293 if( (flags1 | flags3)&MEM_Str ){ 002294 if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ 002295 applyNumericAffinity(pIn1,0); 002296 assert( flags3==pIn3->flags || CORRUPT_DB ); 002297 flags3 = pIn3->flags; 002298 } 002299 if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ 002300 applyNumericAffinity(pIn3,0); 002301 } 002302 } 002303 }else if( affinity==SQLITE_AFF_TEXT && ((flags1 | flags3) & MEM_Str)!=0 ){ 002304 if( (flags1 & MEM_Str)!=0 ){ 002305 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal); 002306 }else if( (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ 002307 testcase( pIn1->flags & MEM_Int ); 002308 testcase( pIn1->flags & MEM_Real ); 002309 testcase( pIn1->flags & MEM_IntReal ); 002310 sqlite3VdbeMemStringify(pIn1, encoding, 1); 002311 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); 002312 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); 002313 if( NEVER(pIn1==pIn3) ) flags3 = flags1 | MEM_Str; 002314 } 002315 if( (flags3 & MEM_Str)!=0 ){ 002316 pIn3->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal); 002317 }else if( (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ 002318 testcase( pIn3->flags & MEM_Int ); 002319 testcase( pIn3->flags & MEM_Real ); 002320 testcase( pIn3->flags & MEM_IntReal ); 002321 sqlite3VdbeMemStringify(pIn3, encoding, 1); 002322 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); 002323 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); 002324 } 002325 } 002326 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); 002327 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); 002328 } 002329 002330 /* At this point, res is negative, zero, or positive if reg[P1] is 002331 ** less than, equal to, or greater than reg[P3], respectively. Compute 002332 ** the answer to this operator in res2, depending on what the comparison 002333 ** operator actually is. The next block of code depends on the fact 002334 ** that the 6 comparison operators are consecutive integers in this 002335 ** order: NE, EQ, GT, LE, LT, GE */ 002336 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 ); 002337 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 ); 002338 if( res<0 ){ 002339 res2 = sqlite3aLTb[pOp->opcode]; 002340 }else if( res==0 ){ 002341 res2 = sqlite3aEQb[pOp->opcode]; 002342 }else{ 002343 res2 = sqlite3aGTb[pOp->opcode]; 002344 } 002345 iCompare = res; 002346 VVA_ONLY( iCompareIsInit = 1; ) 002347 002348 /* Undo any changes made by applyAffinity() to the input registers. */ 002349 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); 002350 pIn3->flags = flags3; 002351 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); 002352 pIn1->flags = flags1; 002353 002354 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); 002355 if( res2 ){ 002356 goto jump_to_p2; 002357 } 002358 break; 002359 } 002360 002361 /* Opcode: ElseEq * P2 * * * 002362 ** 002363 ** This opcode must follow an OP_Lt or OP_Gt comparison operator. There 002364 ** can be zero or more OP_ReleaseReg opcodes intervening, but no other 002365 ** opcodes are allowed to occur between this instruction and the previous 002366 ** OP_Lt or OP_Gt. 002367 ** 002368 ** If the result of an OP_Eq comparison on the same two operands as 002369 ** the prior OP_Lt or OP_Gt would have been true, then jump to P2. If 002370 ** the result of an OP_Eq comparison on the two previous operands 002371 ** would have been false or NULL, then fall through. 002372 */ 002373 case OP_ElseEq: { /* same as TK_ESCAPE, jump */ 002374 002375 #ifdef SQLITE_DEBUG 002376 /* Verify the preconditions of this opcode - that it follows an OP_Lt or 002377 ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */ 002378 int iAddr; 002379 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){ 002380 if( aOp[iAddr].opcode==OP_ReleaseReg ) continue; 002381 assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt ); 002382 break; 002383 } 002384 #endif /* SQLITE_DEBUG */ 002385 assert( iCompareIsInit ); 002386 VdbeBranchTaken(iCompare==0, 2); 002387 if( iCompare==0 ) goto jump_to_p2; 002388 break; 002389 } 002390 002391 002392 /* Opcode: Permutation * * * P4 * 002393 ** 002394 ** Set the permutation used by the OP_Compare operator in the next 002395 ** instruction. The permutation is stored in the P4 operand. 002396 ** 002397 ** The permutation is only valid for the next opcode which must be 002398 ** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5. 002399 ** 002400 ** The first integer in the P4 integer array is the length of the array 002401 ** and does not become part of the permutation. 002402 */ 002403 case OP_Permutation: { 002404 assert( pOp->p4type==P4_INTARRAY ); 002405 assert( pOp->p4.ai ); 002406 assert( pOp[1].opcode==OP_Compare ); 002407 assert( pOp[1].p5 & OPFLAG_PERMUTE ); 002408 break; 002409 } 002410 002411 /* Opcode: Compare P1 P2 P3 P4 P5 002412 ** Synopsis: r[P1@P3] <-> r[P2@P3] 002413 ** 002414 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this 002415 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of 002416 ** the comparison for use by the next OP_Jump instruct. 002417 ** 002418 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is 002419 ** determined by the most recent OP_Permutation operator. If the 002420 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential 002421 ** order. 002422 ** 002423 ** P4 is a KeyInfo structure that defines collating sequences and sort 002424 ** orders for the comparison. The permutation applies to registers 002425 ** only. The KeyInfo elements are used sequentially. 002426 ** 002427 ** The comparison is a sort comparison, so NULLs compare equal, 002428 ** NULLs are less than numbers, numbers are less than strings, 002429 ** and strings are less than blobs. 002430 ** 002431 ** This opcode must be immediately followed by an OP_Jump opcode. 002432 */ 002433 case OP_Compare: { 002434 int n; 002435 int i; 002436 int p1; 002437 int p2; 002438 const KeyInfo *pKeyInfo; 002439 u32 idx; 002440 CollSeq *pColl; /* Collating sequence to use on this term */ 002441 int bRev; /* True for DESCENDING sort order */ 002442 u32 *aPermute; /* The permutation */ 002443 002444 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){ 002445 aPermute = 0; 002446 }else{ 002447 assert( pOp>aOp ); 002448 assert( pOp[-1].opcode==OP_Permutation ); 002449 assert( pOp[-1].p4type==P4_INTARRAY ); 002450 aPermute = pOp[-1].p4.ai + 1; 002451 assert( aPermute!=0 ); 002452 } 002453 n = pOp->p3; 002454 pKeyInfo = pOp->p4.pKeyInfo; 002455 assert( n>0 ); 002456 assert( pKeyInfo!=0 ); 002457 p1 = pOp->p1; 002458 p2 = pOp->p2; 002459 #ifdef SQLITE_DEBUG 002460 if( aPermute ){ 002461 int k, mx = 0; 002462 for(k=0; k<n; k++) if( aPermute[k]>(u32)mx ) mx = aPermute[k]; 002463 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 ); 002464 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 ); 002465 }else{ 002466 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 ); 002467 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 ); 002468 } 002469 #endif /* SQLITE_DEBUG */ 002470 for(i=0; i<n; i++){ 002471 idx = aPermute ? aPermute[i] : (u32)i; 002472 assert( memIsValid(&aMem[p1+idx]) ); 002473 assert( memIsValid(&aMem[p2+idx]) ); 002474 REGISTER_TRACE(p1+idx, &aMem[p1+idx]); 002475 REGISTER_TRACE(p2+idx, &aMem[p2+idx]); 002476 assert( i<pKeyInfo->nKeyField ); 002477 pColl = pKeyInfo->aColl[i]; 002478 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC); 002479 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); 002480 VVA_ONLY( iCompareIsInit = 1; ) 002481 if( iCompare ){ 002482 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) 002483 && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null)) 002484 ){ 002485 iCompare = -iCompare; 002486 } 002487 if( bRev ) iCompare = -iCompare; 002488 break; 002489 } 002490 } 002491 assert( pOp[1].opcode==OP_Jump ); 002492 break; 002493 } 002494 002495 /* Opcode: Jump P1 P2 P3 * * 002496 ** 002497 ** Jump to the instruction at address P1, P2, or P3 depending on whether 002498 ** in the most recent OP_Compare instruction the P1 vector was less than, 002499 ** equal to, or greater than the P2 vector, respectively. 002500 ** 002501 ** This opcode must immediately follow an OP_Compare opcode. 002502 */ 002503 case OP_Jump: { /* jump */ 002504 assert( pOp>aOp && pOp[-1].opcode==OP_Compare ); 002505 assert( iCompareIsInit ); 002506 if( iCompare<0 ){ 002507 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1]; 002508 }else if( iCompare==0 ){ 002509 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1]; 002510 }else{ 002511 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1]; 002512 } 002513 break; 002514 } 002515 002516 /* Opcode: And P1 P2 P3 * * 002517 ** Synopsis: r[P3]=(r[P1] && r[P2]) 002518 ** 002519 ** Take the logical AND of the values in registers P1 and P2 and 002520 ** write the result into register P3. 002521 ** 002522 ** If either P1 or P2 is 0 (false) then the result is 0 even if 002523 ** the other input is NULL. A NULL and true or two NULLs give 002524 ** a NULL output. 002525 */ 002526 /* Opcode: Or P1 P2 P3 * * 002527 ** Synopsis: r[P3]=(r[P1] || r[P2]) 002528 ** 002529 ** Take the logical OR of the values in register P1 and P2 and 002530 ** store the answer in register P3. 002531 ** 002532 ** If either P1 or P2 is nonzero (true) then the result is 1 (true) 002533 ** even if the other input is NULL. A NULL and false or two NULLs 002534 ** give a NULL output. 002535 */ 002536 case OP_And: /* same as TK_AND, in1, in2, out3 */ 002537 case OP_Or: { /* same as TK_OR, in1, in2, out3 */ 002538 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ 002539 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ 002540 002541 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2); 002542 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2); 002543 if( pOp->opcode==OP_And ){ 002544 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; 002545 v1 = and_logic[v1*3+v2]; 002546 }else{ 002547 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 }; 002548 v1 = or_logic[v1*3+v2]; 002549 } 002550 pOut = &aMem[pOp->p3]; 002551 if( v1==2 ){ 002552 MemSetTypeFlag(pOut, MEM_Null); 002553 }else{ 002554 pOut->u.i = v1; 002555 MemSetTypeFlag(pOut, MEM_Int); 002556 } 002557 break; 002558 } 002559 002560 /* Opcode: IsTrue P1 P2 P3 P4 * 002561 ** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 002562 ** 002563 ** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and 002564 ** IS NOT FALSE operators. 002565 ** 002566 ** Interpret the value in register P1 as a boolean value. Store that 002567 ** boolean (a 0 or 1) in register P2. Or if the value in register P1 is 002568 ** NULL, then the P3 is stored in register P2. Invert the answer if P4 002569 ** is 1. 002570 ** 002571 ** The logic is summarized like this: 002572 ** 002573 ** <ul> 002574 ** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE 002575 ** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE 002576 ** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE 002577 ** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE 002578 ** </ul> 002579 */ 002580 case OP_IsTrue: { /* in1, out2 */ 002581 assert( pOp->p4type==P4_INT32 ); 002582 assert( pOp->p4.i==0 || pOp->p4.i==1 ); 002583 assert( pOp->p3==0 || pOp->p3==1 ); 002584 sqlite3VdbeMemSetInt64(&aMem[pOp->p2], 002585 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i); 002586 break; 002587 } 002588 002589 /* Opcode: Not P1 P2 * * * 002590 ** Synopsis: r[P2]= !r[P1] 002591 ** 002592 ** Interpret the value in register P1 as a boolean value. Store the 002593 ** boolean complement in register P2. If the value in register P1 is 002594 ** NULL, then a NULL is stored in P2. 002595 */ 002596 case OP_Not: { /* same as TK_NOT, in1, out2 */ 002597 pIn1 = &aMem[pOp->p1]; 002598 pOut = &aMem[pOp->p2]; 002599 if( (pIn1->flags & MEM_Null)==0 ){ 002600 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0)); 002601 }else{ 002602 sqlite3VdbeMemSetNull(pOut); 002603 } 002604 break; 002605 } 002606 002607 /* Opcode: BitNot P1 P2 * * * 002608 ** Synopsis: r[P2]= ~r[P1] 002609 ** 002610 ** Interpret the content of register P1 as an integer. Store the 002611 ** ones-complement of the P1 value into register P2. If P1 holds 002612 ** a NULL then store a NULL in P2. 002613 */ 002614 case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ 002615 pIn1 = &aMem[pOp->p1]; 002616 pOut = &aMem[pOp->p2]; 002617 sqlite3VdbeMemSetNull(pOut); 002618 if( (pIn1->flags & MEM_Null)==0 ){ 002619 pOut->flags = MEM_Int; 002620 pOut->u.i = ~sqlite3VdbeIntValue(pIn1); 002621 } 002622 break; 002623 } 002624 002625 /* Opcode: Once P1 P2 * * * 002626 ** 002627 ** Fall through to the next instruction the first time this opcode is 002628 ** encountered on each invocation of the byte-code program. Jump to P2 002629 ** on the second and all subsequent encounters during the same invocation. 002630 ** 002631 ** Top-level programs determine first invocation by comparing the P1 002632 ** operand against the P1 operand on the OP_Init opcode at the beginning 002633 ** of the program. If the P1 values differ, then fall through and make 002634 ** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are 002635 ** the same then take the jump. 002636 ** 002637 ** For subprograms, there is a bitmask in the VdbeFrame that determines 002638 ** whether or not the jump should be taken. The bitmask is necessary 002639 ** because the self-altering code trick does not work for recursive 002640 ** triggers. 002641 */ 002642 case OP_Once: { /* jump */ 002643 u32 iAddr; /* Address of this instruction */ 002644 assert( p->aOp[0].opcode==OP_Init ); 002645 if( p->pFrame ){ 002646 iAddr = (int)(pOp - p->aOp); 002647 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){ 002648 VdbeBranchTaken(1, 2); 002649 goto jump_to_p2; 002650 } 002651 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7); 002652 }else{ 002653 if( p->aOp[0].p1==pOp->p1 ){ 002654 VdbeBranchTaken(1, 2); 002655 goto jump_to_p2; 002656 } 002657 } 002658 VdbeBranchTaken(0, 2); 002659 pOp->p1 = p->aOp[0].p1; 002660 break; 002661 } 002662 002663 /* Opcode: If P1 P2 P3 * * 002664 ** 002665 ** Jump to P2 if the value in register P1 is true. The value 002666 ** is considered true if it is numeric and non-zero. If the value 002667 ** in P1 is NULL then take the jump if and only if P3 is non-zero. 002668 */ 002669 case OP_If: { /* jump, in1 */ 002670 int c; 002671 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3); 002672 VdbeBranchTaken(c!=0, 2); 002673 if( c ) goto jump_to_p2; 002674 break; 002675 } 002676 002677 /* Opcode: IfNot P1 P2 P3 * * 002678 ** 002679 ** Jump to P2 if the value in register P1 is False. The value 002680 ** is considered false if it has a numeric value of zero. If the value 002681 ** in P1 is NULL then take the jump if and only if P3 is non-zero. 002682 */ 002683 case OP_IfNot: { /* jump, in1 */ 002684 int c; 002685 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3); 002686 VdbeBranchTaken(c!=0, 2); 002687 if( c ) goto jump_to_p2; 002688 break; 002689 } 002690 002691 /* Opcode: IsNull P1 P2 * * * 002692 ** Synopsis: if r[P1]==NULL goto P2 002693 ** 002694 ** Jump to P2 if the value in register P1 is NULL. 002695 */ 002696 case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ 002697 pIn1 = &aMem[pOp->p1]; 002698 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); 002699 if( (pIn1->flags & MEM_Null)!=0 ){ 002700 goto jump_to_p2; 002701 } 002702 break; 002703 } 002704 002705 /* Opcode: IsType P1 P2 P3 P4 P5 002706 ** Synopsis: if typeof(P1.P3) in P5 goto P2 002707 ** 002708 ** Jump to P2 if the type of a column in a btree is one of the types specified 002709 ** by the P5 bitmask. 002710 ** 002711 ** P1 is normally a cursor on a btree for which the row decode cache is 002712 ** valid through at least column P3. In other words, there should have been 002713 ** a prior OP_Column for column P3 or greater. If the cursor is not valid, 002714 ** then this opcode might give spurious results. 002715 ** The the btree row has fewer than P3 columns, then use P4 as the 002716 ** datatype. 002717 ** 002718 ** If P1 is -1, then P3 is a register number and the datatype is taken 002719 ** from the value in that register. 002720 ** 002721 ** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant 002722 ** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04. 002723 ** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10. 002724 ** 002725 ** WARNING: This opcode does not reliably distinguish between NULL and REAL 002726 ** when P1>=0. If the database contains a NaN value, this opcode will think 002727 ** that the datatype is REAL when it should be NULL. When P1<0 and the value 002728 ** is already stored in register P3, then this opcode does reliably 002729 ** distinguish between NULL and REAL. The problem only arises then P1>=0. 002730 ** 002731 ** Take the jump to address P2 if and only if the datatype of the 002732 ** value determined by P1 and P3 corresponds to one of the bits in the 002733 ** P5 bitmask. 002734 ** 002735 */ 002736 case OP_IsType: { /* jump */ 002737 VdbeCursor *pC; 002738 u16 typeMask; 002739 u32 serialType; 002740 002741 assert( pOp->p1>=(-1) && pOp->p1<p->nCursor ); 002742 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) ); 002743 if( pOp->p1>=0 ){ 002744 pC = p->apCsr[pOp->p1]; 002745 assert( pC!=0 ); 002746 assert( pOp->p3>=0 ); 002747 if( pOp->p3<pC->nHdrParsed ){ 002748 serialType = pC->aType[pOp->p3]; 002749 if( serialType>=12 ){ 002750 if( serialType&1 ){ 002751 typeMask = 0x04; /* SQLITE_TEXT */ 002752 }else{ 002753 typeMask = 0x08; /* SQLITE_BLOB */ 002754 } 002755 }else{ 002756 static const unsigned char aMask[] = { 002757 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2, 002758 0x01, 0x01, 0x10, 0x10 002759 }; 002760 testcase( serialType==0 ); 002761 testcase( serialType==1 ); 002762 testcase( serialType==2 ); 002763 testcase( serialType==3 ); 002764 testcase( serialType==4 ); 002765 testcase( serialType==5 ); 002766 testcase( serialType==6 ); 002767 testcase( serialType==7 ); 002768 testcase( serialType==8 ); 002769 testcase( serialType==9 ); 002770 testcase( serialType==10 ); 002771 testcase( serialType==11 ); 002772 typeMask = aMask[serialType]; 002773 } 002774 }else{ 002775 typeMask = 1 << (pOp->p4.i - 1); 002776 testcase( typeMask==0x01 ); 002777 testcase( typeMask==0x02 ); 002778 testcase( typeMask==0x04 ); 002779 testcase( typeMask==0x08 ); 002780 testcase( typeMask==0x10 ); 002781 } 002782 }else{ 002783 assert( memIsValid(&aMem[pOp->p3]) ); 002784 typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1); 002785 testcase( typeMask==0x01 ); 002786 testcase( typeMask==0x02 ); 002787 testcase( typeMask==0x04 ); 002788 testcase( typeMask==0x08 ); 002789 testcase( typeMask==0x10 ); 002790 } 002791 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2); 002792 if( typeMask & pOp->p5 ){ 002793 goto jump_to_p2; 002794 } 002795 break; 002796 } 002797 002798 /* Opcode: ZeroOrNull P1 P2 P3 * * 002799 ** Synopsis: r[P2] = 0 OR NULL 002800 ** 002801 ** If both registers P1 and P3 are NOT NULL, then store a zero in 002802 ** register P2. If either registers P1 or P3 are NULL then put 002803 ** a NULL in register P2. 002804 */ 002805 case OP_ZeroOrNull: { /* in1, in2, out2, in3 */ 002806 if( (aMem[pOp->p1].flags & MEM_Null)!=0 002807 || (aMem[pOp->p3].flags & MEM_Null)!=0 002808 ){ 002809 sqlite3VdbeMemSetNull(aMem + pOp->p2); 002810 }else{ 002811 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0); 002812 } 002813 break; 002814 } 002815 002816 /* Opcode: NotNull P1 P2 * * * 002817 ** Synopsis: if r[P1]!=NULL goto P2 002818 ** 002819 ** Jump to P2 if the value in register P1 is not NULL. 002820 */ 002821 case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ 002822 pIn1 = &aMem[pOp->p1]; 002823 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); 002824 if( (pIn1->flags & MEM_Null)==0 ){ 002825 goto jump_to_p2; 002826 } 002827 break; 002828 } 002829 002830 /* Opcode: IfNullRow P1 P2 P3 * * 002831 ** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2 002832 ** 002833 ** Check the cursor P1 to see if it is currently pointing at a NULL row. 002834 ** If it is, then set register P3 to NULL and jump immediately to P2. 002835 ** If P1 is not on a NULL row, then fall through without making any 002836 ** changes. 002837 ** 002838 ** If P1 is not an open cursor, then this opcode is a no-op. 002839 */ 002840 case OP_IfNullRow: { /* jump */ 002841 VdbeCursor *pC; 002842 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 002843 pC = p->apCsr[pOp->p1]; 002844 if( pC && pC->nullRow ){ 002845 sqlite3VdbeMemSetNull(aMem + pOp->p3); 002846 goto jump_to_p2; 002847 } 002848 break; 002849 } 002850 002851 #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC 002852 /* Opcode: Offset P1 P2 P3 * * 002853 ** Synopsis: r[P3] = sqlite_offset(P1) 002854 ** 002855 ** Store in register r[P3] the byte offset into the database file that is the 002856 ** start of the payload for the record at which that cursor P1 is currently 002857 ** pointing. 002858 ** 002859 ** P2 is the column number for the argument to the sqlite_offset() function. 002860 ** This opcode does not use P2 itself, but the P2 value is used by the 002861 ** code generator. The P1, P2, and P3 operands to this opcode are the 002862 ** same as for OP_Column. 002863 ** 002864 ** This opcode is only available if SQLite is compiled with the 002865 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option. 002866 */ 002867 case OP_Offset: { /* out3 */ 002868 VdbeCursor *pC; /* The VDBE cursor */ 002869 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 002870 pC = p->apCsr[pOp->p1]; 002871 pOut = &p->aMem[pOp->p3]; 002872 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){ 002873 sqlite3VdbeMemSetNull(pOut); 002874 }else{ 002875 if( pC->deferredMoveto ){ 002876 rc = sqlite3VdbeFinishMoveto(pC); 002877 if( rc ) goto abort_due_to_error; 002878 } 002879 if( sqlite3BtreeEof(pC->uc.pCursor) ){ 002880 sqlite3VdbeMemSetNull(pOut); 002881 }else{ 002882 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor)); 002883 } 002884 } 002885 break; 002886 } 002887 #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */ 002888 002889 /* Opcode: Column P1 P2 P3 P4 P5 002890 ** Synopsis: r[P3]=PX cursor P1 column P2 002891 ** 002892 ** Interpret the data that cursor P1 points to as a structure built using 002893 ** the MakeRecord instruction. (See the MakeRecord opcode for additional 002894 ** information about the format of the data.) Extract the P2-th column 002895 ** from this record. If there are less than (P2+1) 002896 ** values in the record, extract a NULL. 002897 ** 002898 ** The value extracted is stored in register P3. 002899 ** 002900 ** If the record contains fewer than P2 fields, then extract a NULL. Or, 002901 ** if the P4 argument is a P4_MEM use the value of the P4 argument as 002902 ** the result. 002903 ** 002904 ** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed 002905 ** to only be used by the length() function or the equivalent. The content 002906 ** of large blobs is not loaded, thus saving CPU cycles. If the 002907 ** OPFLAG_TYPEOFARG bit is set then the result will only be used by the 002908 ** typeof() function or the IS NULL or IS NOT NULL operators or the 002909 ** equivalent. In this case, all content loading can be omitted. 002910 */ 002911 case OP_Column: { /* ncycle */ 002912 u32 p2; /* column number to retrieve */ 002913 VdbeCursor *pC; /* The VDBE cursor */ 002914 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */ 002915 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ 002916 int len; /* The length of the serialized data for the column */ 002917 int i; /* Loop counter */ 002918 Mem *pDest; /* Where to write the extracted value */ 002919 Mem sMem; /* For storing the record being decoded */ 002920 const u8 *zData; /* Part of the record being decoded */ 002921 const u8 *zHdr; /* Next unparsed byte of the header */ 002922 const u8 *zEndHdr; /* Pointer to first byte after the header */ 002923 u64 offset64; /* 64-bit offset */ 002924 u32 t; /* A type code from the record header */ 002925 Mem *pReg; /* PseudoTable input register */ 002926 002927 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 002928 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 002929 pC = p->apCsr[pOp->p1]; 002930 p2 = (u32)pOp->p2; 002931 002932 op_column_restart: 002933 assert( pC!=0 ); 002934 assert( p2<(u32)pC->nField 002935 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) ); 002936 aOffset = pC->aOffset; 002937 assert( aOffset==pC->aType+pC->nField ); 002938 assert( pC->eCurType!=CURTYPE_VTAB ); 002939 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); 002940 assert( pC->eCurType!=CURTYPE_SORTER ); 002941 002942 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/ 002943 if( pC->nullRow ){ 002944 if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){ 002945 /* For the special case of as pseudo-cursor, the seekResult field 002946 ** identifies the register that holds the record */ 002947 pReg = &aMem[pC->seekResult]; 002948 assert( pReg->flags & MEM_Blob ); 002949 assert( memIsValid(pReg) ); 002950 pC->payloadSize = pC->szRow = pReg->n; 002951 pC->aRow = (u8*)pReg->z; 002952 }else{ 002953 pDest = &aMem[pOp->p3]; 002954 memAboutToChange(p, pDest); 002955 sqlite3VdbeMemSetNull(pDest); 002956 goto op_column_out; 002957 } 002958 }else{ 002959 pCrsr = pC->uc.pCursor; 002960 if( pC->deferredMoveto ){ 002961 u32 iMap; 002962 assert( !pC->isEphemeral ); 002963 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){ 002964 pC = pC->pAltCursor; 002965 p2 = iMap - 1; 002966 goto op_column_restart; 002967 } 002968 rc = sqlite3VdbeFinishMoveto(pC); 002969 if( rc ) goto abort_due_to_error; 002970 }else if( sqlite3BtreeCursorHasMoved(pCrsr) ){ 002971 rc = sqlite3VdbeHandleMovedCursor(pC); 002972 if( rc ) goto abort_due_to_error; 002973 goto op_column_restart; 002974 } 002975 assert( pC->eCurType==CURTYPE_BTREE ); 002976 assert( pCrsr ); 002977 assert( sqlite3BtreeCursorIsValid(pCrsr) ); 002978 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr); 002979 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow); 002980 assert( pC->szRow<=pC->payloadSize ); 002981 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */ 002982 } 002983 pC->cacheStatus = p->cacheCtr; 002984 if( (aOffset[0] = pC->aRow[0])<0x80 ){ 002985 pC->iHdrOffset = 1; 002986 }else{ 002987 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset); 002988 } 002989 pC->nHdrParsed = 0; 002990 002991 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/ 002992 /* pC->aRow does not have to hold the entire row, but it does at least 002993 ** need to cover the header of the record. If pC->aRow does not contain 002994 ** the complete header, then set it to zero, forcing the header to be 002995 ** dynamically allocated. */ 002996 pC->aRow = 0; 002997 pC->szRow = 0; 002998 002999 /* Make sure a corrupt database has not given us an oversize header. 003000 ** Do this now to avoid an oversize memory allocation. 003001 ** 003002 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte 003003 ** types use so much data space that there can only be 4096 and 32 of 003004 ** them, respectively. So the maximum header length results from a 003005 ** 3-byte type for each of the maximum of 32768 columns plus three 003006 ** extra bytes for the header length itself. 32768*3 + 3 = 98307. 003007 */ 003008 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){ 003009 goto op_column_corrupt; 003010 } 003011 }else{ 003012 /* This is an optimization. By skipping over the first few tests 003013 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a 003014 ** measurable performance gain. 003015 ** 003016 ** This branch is taken even if aOffset[0]==0. Such a record is never 003017 ** generated by SQLite, and could be considered corruption, but we 003018 ** accept it for historical reasons. When aOffset[0]==0, the code this 003019 ** branch jumps to reads past the end of the record, but never more 003020 ** than a few bytes. Even if the record occurs at the end of the page 003021 ** content area, the "page header" comes after the page content and so 003022 ** this overread is harmless. Similar overreads can occur for a corrupt 003023 ** database file. 003024 */ 003025 zData = pC->aRow; 003026 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ 003027 testcase( aOffset[0]==0 ); 003028 goto op_column_read_header; 003029 } 003030 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){ 003031 rc = sqlite3VdbeHandleMovedCursor(pC); 003032 if( rc ) goto abort_due_to_error; 003033 goto op_column_restart; 003034 } 003035 003036 /* Make sure at least the first p2+1 entries of the header have been 003037 ** parsed and valid information is in aOffset[] and pC->aType[]. 003038 */ 003039 if( pC->nHdrParsed<=p2 ){ 003040 /* If there is more header available for parsing in the record, try 003041 ** to extract additional fields up through the p2+1-th field 003042 */ 003043 if( pC->iHdrOffset<aOffset[0] ){ 003044 /* Make sure zData points to enough of the record to cover the header. */ 003045 if( pC->aRow==0 ){ 003046 memset(&sMem, 0, sizeof(sMem)); 003047 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem); 003048 if( rc!=SQLITE_OK ) goto abort_due_to_error; 003049 zData = (u8*)sMem.z; 003050 }else{ 003051 zData = pC->aRow; 003052 } 003053 003054 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */ 003055 op_column_read_header: 003056 i = pC->nHdrParsed; 003057 offset64 = aOffset[i]; 003058 zHdr = zData + pC->iHdrOffset; 003059 zEndHdr = zData + aOffset[0]; 003060 testcase( zHdr>=zEndHdr ); 003061 do{ 003062 if( (pC->aType[i] = t = zHdr[0])<0x80 ){ 003063 zHdr++; 003064 offset64 += sqlite3VdbeOneByteSerialTypeLen(t); 003065 }else{ 003066 zHdr += sqlite3GetVarint32(zHdr, &t); 003067 pC->aType[i] = t; 003068 offset64 += sqlite3VdbeSerialTypeLen(t); 003069 } 003070 aOffset[++i] = (u32)(offset64 & 0xffffffff); 003071 }while( (u32)i<=p2 && zHdr<zEndHdr ); 003072 003073 /* The record is corrupt if any of the following are true: 003074 ** (1) the bytes of the header extend past the declared header size 003075 ** (2) the entire header was used but not all data was used 003076 ** (3) the end of the data extends beyond the end of the record. 003077 */ 003078 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize)) 003079 || (offset64 > pC->payloadSize) 003080 ){ 003081 if( aOffset[0]==0 ){ 003082 i = 0; 003083 zHdr = zEndHdr; 003084 }else{ 003085 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); 003086 goto op_column_corrupt; 003087 } 003088 } 003089 003090 pC->nHdrParsed = i; 003091 pC->iHdrOffset = (u32)(zHdr - zData); 003092 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); 003093 }else{ 003094 t = 0; 003095 } 003096 003097 /* If after trying to extract new entries from the header, nHdrParsed is 003098 ** still not up to p2, that means that the record has fewer than p2 003099 ** columns. So the result will be either the default value or a NULL. 003100 */ 003101 if( pC->nHdrParsed<=p2 ){ 003102 pDest = &aMem[pOp->p3]; 003103 memAboutToChange(p, pDest); 003104 if( pOp->p4type==P4_MEM ){ 003105 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); 003106 }else{ 003107 sqlite3VdbeMemSetNull(pDest); 003108 } 003109 goto op_column_out; 003110 } 003111 }else{ 003112 t = pC->aType[p2]; 003113 } 003114 003115 /* Extract the content for the p2+1-th column. Control can only 003116 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are 003117 ** all valid. 003118 */ 003119 assert( p2<pC->nHdrParsed ); 003120 assert( rc==SQLITE_OK ); 003121 pDest = &aMem[pOp->p3]; 003122 memAboutToChange(p, pDest); 003123 assert( sqlite3VdbeCheckMemInvariants(pDest) ); 003124 if( VdbeMemDynamic(pDest) ){ 003125 sqlite3VdbeMemSetNull(pDest); 003126 } 003127 assert( t==pC->aType[p2] ); 003128 if( pC->szRow>=aOffset[p2+1] ){ 003129 /* This is the common case where the desired content fits on the original 003130 ** page - where the content is not on an overflow page */ 003131 zData = pC->aRow + aOffset[p2]; 003132 if( t<12 ){ 003133 sqlite3VdbeSerialGet(zData, t, pDest); 003134 }else{ 003135 /* If the column value is a string, we need a persistent value, not 003136 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent 003137 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize(). 003138 */ 003139 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term }; 003140 pDest->n = len = (t-12)/2; 003141 pDest->enc = encoding; 003142 if( pDest->szMalloc < len+2 ){ 003143 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big; 003144 pDest->flags = MEM_Null; 003145 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem; 003146 }else{ 003147 pDest->z = pDest->zMalloc; 003148 } 003149 memcpy(pDest->z, zData, len); 003150 pDest->z[len] = 0; 003151 pDest->z[len+1] = 0; 003152 pDest->flags = aFlag[t&1]; 003153 } 003154 }else{ 003155 u8 p5; 003156 pDest->enc = encoding; 003157 assert( pDest->db==db ); 003158 /* This branch happens only when content is on overflow pages */ 003159 if( ((p5 = (pOp->p5 & OPFLAG_BYTELENARG))!=0 003160 && (p5==OPFLAG_TYPEOFARG 003161 || (t>=12 && ((t&1)==0 || p5==OPFLAG_BYTELENARG)) 003162 ) 003163 ) 003164 || sqlite3VdbeSerialTypeLen(t)==0 003165 ){ 003166 /* Content is irrelevant for 003167 ** 1. the typeof() function, 003168 ** 2. the length(X) function if X is a blob, and 003169 ** 3. if the content length is zero. 003170 ** So we might as well use bogus content rather than reading 003171 ** content from disk. 003172 ** 003173 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the 003174 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may 003175 ** read more. Use the global constant sqlite3CtypeMap[] as the array, 003176 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint()) 003177 ** and it begins with a bunch of zeros. 003178 */ 003179 sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest); 003180 }else{ 003181 rc = vdbeColumnFromOverflow(pC, p2, t, aOffset[p2], 003182 p->cacheCtr, colCacheCtr, pDest); 003183 if( rc ){ 003184 if( rc==SQLITE_NOMEM ) goto no_mem; 003185 if( rc==SQLITE_TOOBIG ) goto too_big; 003186 goto abort_due_to_error; 003187 } 003188 } 003189 } 003190 003191 op_column_out: 003192 UPDATE_MAX_BLOBSIZE(pDest); 003193 REGISTER_TRACE(pOp->p3, pDest); 003194 break; 003195 003196 op_column_corrupt: 003197 if( aOp[0].p3>0 ){ 003198 pOp = &aOp[aOp[0].p3-1]; 003199 break; 003200 }else{ 003201 rc = SQLITE_CORRUPT_BKPT; 003202 goto abort_due_to_error; 003203 } 003204 } 003205 003206 /* Opcode: TypeCheck P1 P2 P3 P4 * 003207 ** Synopsis: typecheck(r[P1@P2]) 003208 ** 003209 ** Apply affinities to the range of P2 registers beginning with P1. 003210 ** Take the affinities from the Table object in P4. If any value 003211 ** cannot be coerced into the correct type, then raise an error. 003212 ** 003213 ** This opcode is similar to OP_Affinity except that this opcode 003214 ** forces the register type to the Table column type. This is used 003215 ** to implement "strict affinity". 003216 ** 003217 ** GENERATED ALWAYS AS ... STATIC columns are only checked if P3 003218 ** is zero. When P3 is non-zero, no type checking occurs for 003219 ** static generated columns. Virtual columns are computed at query time 003220 ** and so they are never checked. 003221 ** 003222 ** Preconditions: 003223 ** 003224 ** <ul> 003225 ** <li> P2 should be the number of non-virtual columns in the 003226 ** table of P4. 003227 ** <li> Table P4 should be a STRICT table. 003228 ** </ul> 003229 ** 003230 ** If any precondition is false, an assertion fault occurs. 003231 */ 003232 case OP_TypeCheck: { 003233 Table *pTab; 003234 Column *aCol; 003235 int i; 003236 003237 assert( pOp->p4type==P4_TABLE ); 003238 pTab = pOp->p4.pTab; 003239 assert( pTab->tabFlags & TF_Strict ); 003240 assert( pTab->nNVCol==pOp->p2 ); 003241 aCol = pTab->aCol; 003242 pIn1 = &aMem[pOp->p1]; 003243 for(i=0; i<pTab->nCol; i++){ 003244 if( aCol[i].colFlags & COLFLAG_GENERATED ){ 003245 if( aCol[i].colFlags & COLFLAG_VIRTUAL ) continue; 003246 if( pOp->p3 ){ pIn1++; continue; } 003247 } 003248 assert( pIn1 < &aMem[pOp->p1+pOp->p2] ); 003249 applyAffinity(pIn1, aCol[i].affinity, encoding); 003250 if( (pIn1->flags & MEM_Null)==0 ){ 003251 switch( aCol[i].eCType ){ 003252 case COLTYPE_BLOB: { 003253 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error; 003254 break; 003255 } 003256 case COLTYPE_INTEGER: 003257 case COLTYPE_INT: { 003258 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error; 003259 break; 003260 } 003261 case COLTYPE_TEXT: { 003262 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error; 003263 break; 003264 } 003265 case COLTYPE_REAL: { 003266 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real ); 003267 assert( (pIn1->flags & MEM_IntReal)==0 ); 003268 if( pIn1->flags & MEM_Int ){ 003269 /* When applying REAL affinity, if the result is still an MEM_Int 003270 ** that will fit in 6 bytes, then change the type to MEM_IntReal 003271 ** so that we keep the high-resolution integer value but know that 003272 ** the type really wants to be REAL. */ 003273 testcase( pIn1->u.i==140737488355328LL ); 003274 testcase( pIn1->u.i==140737488355327LL ); 003275 testcase( pIn1->u.i==-140737488355328LL ); 003276 testcase( pIn1->u.i==-140737488355329LL ); 003277 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){ 003278 pIn1->flags |= MEM_IntReal; 003279 pIn1->flags &= ~MEM_Int; 003280 }else{ 003281 pIn1->u.r = (double)pIn1->u.i; 003282 pIn1->flags |= MEM_Real; 003283 pIn1->flags &= ~MEM_Int; 003284 } 003285 }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){ 003286 goto vdbe_type_error; 003287 } 003288 break; 003289 } 003290 default: { 003291 /* COLTYPE_ANY. Accept anything. */ 003292 break; 003293 } 003294 } 003295 } 003296 REGISTER_TRACE((int)(pIn1-aMem), pIn1); 003297 pIn1++; 003298 } 003299 assert( pIn1 == &aMem[pOp->p1+pOp->p2] ); 003300 break; 003301 003302 vdbe_type_error: 003303 sqlite3VdbeError(p, "cannot store %s value in %s column %s.%s", 003304 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1], 003305 pTab->zName, aCol[i].zCnName); 003306 rc = SQLITE_CONSTRAINT_DATATYPE; 003307 goto abort_due_to_error; 003308 } 003309 003310 /* Opcode: Affinity P1 P2 * P4 * 003311 ** Synopsis: affinity(r[P1@P2]) 003312 ** 003313 ** Apply affinities to a range of P2 registers starting with P1. 003314 ** 003315 ** P4 is a string that is P2 characters long. The N-th character of the 003316 ** string indicates the column affinity that should be used for the N-th 003317 ** memory cell in the range. 003318 */ 003319 case OP_Affinity: { 003320 const char *zAffinity; /* The affinity to be applied */ 003321 003322 zAffinity = pOp->p4.z; 003323 assert( zAffinity!=0 ); 003324 assert( pOp->p2>0 ); 003325 assert( zAffinity[pOp->p2]==0 ); 003326 pIn1 = &aMem[pOp->p1]; 003327 while( 1 /*exit-by-break*/ ){ 003328 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); 003329 assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) ); 003330 applyAffinity(pIn1, zAffinity[0], encoding); 003331 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){ 003332 /* When applying REAL affinity, if the result is still an MEM_Int 003333 ** that will fit in 6 bytes, then change the type to MEM_IntReal 003334 ** so that we keep the high-resolution integer value but know that 003335 ** the type really wants to be REAL. */ 003336 testcase( pIn1->u.i==140737488355328LL ); 003337 testcase( pIn1->u.i==140737488355327LL ); 003338 testcase( pIn1->u.i==-140737488355328LL ); 003339 testcase( pIn1->u.i==-140737488355329LL ); 003340 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){ 003341 pIn1->flags |= MEM_IntReal; 003342 pIn1->flags &= ~MEM_Int; 003343 }else{ 003344 pIn1->u.r = (double)pIn1->u.i; 003345 pIn1->flags |= MEM_Real; 003346 pIn1->flags &= ~(MEM_Int|MEM_Str); 003347 } 003348 } 003349 REGISTER_TRACE((int)(pIn1-aMem), pIn1); 003350 zAffinity++; 003351 if( zAffinity[0]==0 ) break; 003352 pIn1++; 003353 } 003354 break; 003355 } 003356 003357 /* Opcode: MakeRecord P1 P2 P3 P4 * 003358 ** Synopsis: r[P3]=mkrec(r[P1@P2]) 003359 ** 003360 ** Convert P2 registers beginning with P1 into the [record format] 003361 ** use as a data record in a database table or as a key 003362 ** in an index. The OP_Column opcode can decode the record later. 003363 ** 003364 ** P4 may be a string that is P2 characters long. The N-th character of the 003365 ** string indicates the column affinity that should be used for the N-th 003366 ** field of the index key. 003367 ** 003368 ** The mapping from character to affinity is given by the SQLITE_AFF_ 003369 ** macros defined in sqliteInt.h. 003370 ** 003371 ** If P4 is NULL then all index fields have the affinity BLOB. 003372 ** 003373 ** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM 003374 ** compile-time option is enabled: 003375 ** 003376 ** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index 003377 ** of the right-most table that can be null-trimmed. 003378 ** 003379 ** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value 003380 ** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to 003381 ** accept no-change records with serial_type 10. This value is 003382 ** only used inside an assert() and does not affect the end result. 003383 */ 003384 case OP_MakeRecord: { 003385 Mem *pRec; /* The new record */ 003386 u64 nData; /* Number of bytes of data space */ 003387 int nHdr; /* Number of bytes of header space */ 003388 i64 nByte; /* Data space required for this record */ 003389 i64 nZero; /* Number of zero bytes at the end of the record */ 003390 int nVarint; /* Number of bytes in a varint */ 003391 u32 serial_type; /* Type field */ 003392 Mem *pData0; /* First field to be combined into the record */ 003393 Mem *pLast; /* Last field of the record */ 003394 int nField; /* Number of fields in the record */ 003395 char *zAffinity; /* The affinity string for the record */ 003396 u32 len; /* Length of a field */ 003397 u8 *zHdr; /* Where to write next byte of the header */ 003398 u8 *zPayload; /* Where to write next byte of the payload */ 003399 003400 /* Assuming the record contains N fields, the record format looks 003401 ** like this: 003402 ** 003403 ** ------------------------------------------------------------------------ 003404 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | 003405 ** ------------------------------------------------------------------------ 003406 ** 003407 ** Data(0) is taken from register P1. Data(1) comes from register P1+1 003408 ** and so forth. 003409 ** 003410 ** Each type field is a varint representing the serial type of the 003411 ** corresponding data element (see sqlite3VdbeSerialType()). The 003412 ** hdr-size field is also a varint which is the offset from the beginning 003413 ** of the record to data0. 003414 */ 003415 nData = 0; /* Number of bytes of data space */ 003416 nHdr = 0; /* Number of bytes of header space */ 003417 nZero = 0; /* Number of zero bytes at the end of the record */ 003418 nField = pOp->p1; 003419 zAffinity = pOp->p4.z; 003420 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 ); 003421 pData0 = &aMem[nField]; 003422 nField = pOp->p2; 003423 pLast = &pData0[nField-1]; 003424 003425 /* Identify the output register */ 003426 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); 003427 pOut = &aMem[pOp->p3]; 003428 memAboutToChange(p, pOut); 003429 003430 /* Apply the requested affinity to all inputs 003431 */ 003432 assert( pData0<=pLast ); 003433 if( zAffinity ){ 003434 pRec = pData0; 003435 do{ 003436 applyAffinity(pRec, zAffinity[0], encoding); 003437 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){ 003438 pRec->flags |= MEM_IntReal; 003439 pRec->flags &= ~(MEM_Int); 003440 } 003441 REGISTER_TRACE((int)(pRec-aMem), pRec); 003442 zAffinity++; 003443 pRec++; 003444 assert( zAffinity[0]==0 || pRec<=pLast ); 003445 }while( zAffinity[0] ); 003446 } 003447 003448 #ifdef SQLITE_ENABLE_NULL_TRIM 003449 /* NULLs can be safely trimmed from the end of the record, as long as 003450 ** as the schema format is 2 or more and none of the omitted columns 003451 ** have a non-NULL default value. Also, the record must be left with 003452 ** at least one field. If P5>0 then it will be one more than the 003453 ** index of the right-most column with a non-NULL default value */ 003454 if( pOp->p5 ){ 003455 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){ 003456 pLast--; 003457 nField--; 003458 } 003459 } 003460 #endif 003461 003462 /* Loop through the elements that will make up the record to figure 003463 ** out how much space is required for the new record. After this loop, 003464 ** the Mem.uTemp field of each term should hold the serial-type that will 003465 ** be used for that term in the generated record: 003466 ** 003467 ** Mem.uTemp value type 003468 ** --------------- --------------- 003469 ** 0 NULL 003470 ** 1 1-byte signed integer 003471 ** 2 2-byte signed integer 003472 ** 3 3-byte signed integer 003473 ** 4 4-byte signed integer 003474 ** 5 6-byte signed integer 003475 ** 6 8-byte signed integer 003476 ** 7 IEEE float 003477 ** 8 Integer constant 0 003478 ** 9 Integer constant 1 003479 ** 10,11 reserved for expansion 003480 ** N>=12 and even BLOB 003481 ** N>=13 and odd text 003482 ** 003483 ** The following additional values are computed: 003484 ** nHdr Number of bytes needed for the record header 003485 ** nData Number of bytes of data space needed for the record 003486 ** nZero Zero bytes at the end of the record 003487 */ 003488 pRec = pLast; 003489 do{ 003490 assert( memIsValid(pRec) ); 003491 if( pRec->flags & MEM_Null ){ 003492 if( pRec->flags & MEM_Zero ){ 003493 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual 003494 ** table methods that never invoke sqlite3_result_xxxxx() while 003495 ** computing an unchanging column value in an UPDATE statement. 003496 ** Give such values a special internal-use-only serial-type of 10 003497 ** so that they can be passed through to xUpdate and have 003498 ** a true sqlite3_value_nochange(). */ 003499 #ifndef SQLITE_ENABLE_NULL_TRIM 003500 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB ); 003501 #endif 003502 pRec->uTemp = 10; 003503 }else{ 003504 pRec->uTemp = 0; 003505 } 003506 nHdr++; 003507 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){ 003508 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ 003509 i64 i = pRec->u.i; 003510 u64 uu; 003511 testcase( pRec->flags & MEM_Int ); 003512 testcase( pRec->flags & MEM_IntReal ); 003513 if( i<0 ){ 003514 uu = ~i; 003515 }else{ 003516 uu = i; 003517 } 003518 nHdr++; 003519 testcase( uu==127 ); testcase( uu==128 ); 003520 testcase( uu==32767 ); testcase( uu==32768 ); 003521 testcase( uu==8388607 ); testcase( uu==8388608 ); 003522 testcase( uu==2147483647 ); testcase( uu==2147483648LL ); 003523 testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL ); 003524 if( uu<=127 ){ 003525 if( (i&1)==i && p->minWriteFileFormat>=4 ){ 003526 pRec->uTemp = 8+(u32)uu; 003527 }else{ 003528 nData++; 003529 pRec->uTemp = 1; 003530 } 003531 }else if( uu<=32767 ){ 003532 nData += 2; 003533 pRec->uTemp = 2; 003534 }else if( uu<=8388607 ){ 003535 nData += 3; 003536 pRec->uTemp = 3; 003537 }else if( uu<=2147483647 ){ 003538 nData += 4; 003539 pRec->uTemp = 4; 003540 }else if( uu<=140737488355327LL ){ 003541 nData += 6; 003542 pRec->uTemp = 5; 003543 }else{ 003544 nData += 8; 003545 if( pRec->flags & MEM_IntReal ){ 003546 /* If the value is IntReal and is going to take up 8 bytes to store 003547 ** as an integer, then we might as well make it an 8-byte floating 003548 ** point value */ 003549 pRec->u.r = (double)pRec->u.i; 003550 pRec->flags &= ~MEM_IntReal; 003551 pRec->flags |= MEM_Real; 003552 pRec->uTemp = 7; 003553 }else{ 003554 pRec->uTemp = 6; 003555 } 003556 } 003557 }else if( pRec->flags & MEM_Real ){ 003558 nHdr++; 003559 nData += 8; 003560 pRec->uTemp = 7; 003561 }else{ 003562 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) ); 003563 assert( pRec->n>=0 ); 003564 len = (u32)pRec->n; 003565 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0); 003566 if( pRec->flags & MEM_Zero ){ 003567 serial_type += pRec->u.nZero*2; 003568 if( nData ){ 003569 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; 003570 len += pRec->u.nZero; 003571 }else{ 003572 nZero += pRec->u.nZero; 003573 } 003574 } 003575 nData += len; 003576 nHdr += sqlite3VarintLen(serial_type); 003577 pRec->uTemp = serial_type; 003578 } 003579 if( pRec==pData0 ) break; 003580 pRec--; 003581 }while(1); 003582 003583 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint 003584 ** which determines the total number of bytes in the header. The varint 003585 ** value is the size of the header in bytes including the size varint 003586 ** itself. */ 003587 testcase( nHdr==126 ); 003588 testcase( nHdr==127 ); 003589 if( nHdr<=126 ){ 003590 /* The common case */ 003591 nHdr += 1; 003592 }else{ 003593 /* Rare case of a really large header */ 003594 nVarint = sqlite3VarintLen(nHdr); 003595 nHdr += nVarint; 003596 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++; 003597 } 003598 nByte = nHdr+nData; 003599 003600 /* Make sure the output register has a buffer large enough to store 003601 ** the new record. The output register (pOp->p3) is not allowed to 003602 ** be one of the input registers (because the following call to 003603 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). 003604 */ 003605 if( nByte+nZero<=pOut->szMalloc ){ 003606 /* The output register is already large enough to hold the record. 003607 ** No error checks or buffer enlargement is required */ 003608 pOut->z = pOut->zMalloc; 003609 }else{ 003610 /* Need to make sure that the output is not too big and then enlarge 003611 ** the output register to hold the full result */ 003612 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 003613 goto too_big; 003614 } 003615 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ 003616 goto no_mem; 003617 } 003618 } 003619 pOut->n = (int)nByte; 003620 pOut->flags = MEM_Blob; 003621 if( nZero ){ 003622 pOut->u.nZero = nZero; 003623 pOut->flags |= MEM_Zero; 003624 } 003625 UPDATE_MAX_BLOBSIZE(pOut); 003626 zHdr = (u8 *)pOut->z; 003627 zPayload = zHdr + nHdr; 003628 003629 /* Write the record */ 003630 if( nHdr<0x80 ){ 003631 *(zHdr++) = nHdr; 003632 }else{ 003633 zHdr += sqlite3PutVarint(zHdr,nHdr); 003634 } 003635 assert( pData0<=pLast ); 003636 pRec = pData0; 003637 while( 1 /*exit-by-break*/ ){ 003638 serial_type = pRec->uTemp; 003639 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more 003640 ** additional varints, one per column. 003641 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record 003642 ** immediately follow the header. */ 003643 if( serial_type<=7 ){ 003644 *(zHdr++) = serial_type; 003645 if( serial_type==0 ){ 003646 /* NULL value. No change in zPayload */ 003647 }else{ 003648 u64 v; 003649 if( serial_type==7 ){ 003650 assert( sizeof(v)==sizeof(pRec->u.r) ); 003651 memcpy(&v, &pRec->u.r, sizeof(v)); 003652 swapMixedEndianFloat(v); 003653 }else{ 003654 v = pRec->u.i; 003655 } 003656 len = sqlite3SmallTypeSizes[serial_type]; 003657 assert( len>=1 && len<=8 && len!=5 && len!=7 ); 003658 switch( len ){ 003659 default: zPayload[7] = (u8)(v&0xff); v >>= 8; 003660 zPayload[6] = (u8)(v&0xff); v >>= 8; 003661 case 6: zPayload[5] = (u8)(v&0xff); v >>= 8; 003662 zPayload[4] = (u8)(v&0xff); v >>= 8; 003663 case 4: zPayload[3] = (u8)(v&0xff); v >>= 8; 003664 case 3: zPayload[2] = (u8)(v&0xff); v >>= 8; 003665 case 2: zPayload[1] = (u8)(v&0xff); v >>= 8; 003666 case 1: zPayload[0] = (u8)(v&0xff); 003667 } 003668 zPayload += len; 003669 } 003670 }else if( serial_type<0x80 ){ 003671 *(zHdr++) = serial_type; 003672 if( serial_type>=14 && pRec->n>0 ){ 003673 assert( pRec->z!=0 ); 003674 memcpy(zPayload, pRec->z, pRec->n); 003675 zPayload += pRec->n; 003676 } 003677 }else{ 003678 zHdr += sqlite3PutVarint(zHdr, serial_type); 003679 if( pRec->n ){ 003680 assert( pRec->z!=0 ); 003681 memcpy(zPayload, pRec->z, pRec->n); 003682 zPayload += pRec->n; 003683 } 003684 } 003685 if( pRec==pLast ) break; 003686 pRec++; 003687 } 003688 assert( nHdr==(int)(zHdr - (u8*)pOut->z) ); 003689 assert( nByte==(int)(zPayload - (u8*)pOut->z) ); 003690 003691 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 003692 REGISTER_TRACE(pOp->p3, pOut); 003693 break; 003694 } 003695 003696 /* Opcode: Count P1 P2 P3 * * 003697 ** Synopsis: r[P2]=count() 003698 ** 003699 ** Store the number of entries (an integer value) in the table or index 003700 ** opened by cursor P1 in register P2. 003701 ** 003702 ** If P3==0, then an exact count is obtained, which involves visiting 003703 ** every btree page of the table. But if P3 is non-zero, an estimate 003704 ** is returned based on the current cursor position. 003705 */ 003706 case OP_Count: { /* out2 */ 003707 i64 nEntry; 003708 BtCursor *pCrsr; 003709 003710 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); 003711 pCrsr = p->apCsr[pOp->p1]->uc.pCursor; 003712 assert( pCrsr ); 003713 if( pOp->p3 ){ 003714 nEntry = sqlite3BtreeRowCountEst(pCrsr); 003715 }else{ 003716 nEntry = 0; /* Not needed. Only used to silence a warning. */ 003717 rc = sqlite3BtreeCount(db, pCrsr, &nEntry); 003718 if( rc ) goto abort_due_to_error; 003719 } 003720 pOut = out2Prerelease(p, pOp); 003721 pOut->u.i = nEntry; 003722 goto check_for_interrupt; 003723 } 003724 003725 /* Opcode: Savepoint P1 * * P4 * 003726 ** 003727 ** Open, release or rollback the savepoint named by parameter P4, depending 003728 ** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN). 003729 ** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE). 003730 ** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK). 003731 */ 003732 case OP_Savepoint: { 003733 int p1; /* Value of P1 operand */ 003734 char *zName; /* Name of savepoint */ 003735 int nName; 003736 Savepoint *pNew; 003737 Savepoint *pSavepoint; 003738 Savepoint *pTmp; 003739 int iSavepoint; 003740 int ii; 003741 003742 p1 = pOp->p1; 003743 zName = pOp->p4.z; 003744 003745 /* Assert that the p1 parameter is valid. Also that if there is no open 003746 ** transaction, then there cannot be any savepoints. 003747 */ 003748 assert( db->pSavepoint==0 || db->autoCommit==0 ); 003749 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); 003750 assert( db->pSavepoint || db->isTransactionSavepoint==0 ); 003751 assert( checkSavepointCount(db) ); 003752 assert( p->bIsReader ); 003753 003754 if( p1==SAVEPOINT_BEGIN ){ 003755 if( db->nVdbeWrite>0 ){ 003756 /* A new savepoint cannot be created if there are active write 003757 ** statements (i.e. open read/write incremental blob handles). 003758 */ 003759 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress"); 003760 rc = SQLITE_BUSY; 003761 }else{ 003762 nName = sqlite3Strlen30(zName); 003763 003764 #ifndef SQLITE_OMIT_VIRTUALTABLE 003765 /* This call is Ok even if this savepoint is actually a transaction 003766 ** savepoint (and therefore should not prompt xSavepoint()) callbacks. 003767 ** If this is a transaction savepoint being opened, it is guaranteed 003768 ** that the db->aVTrans[] array is empty. */ 003769 assert( db->autoCommit==0 || db->nVTrans==0 ); 003770 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, 003771 db->nStatement+db->nSavepoint); 003772 if( rc!=SQLITE_OK ) goto abort_due_to_error; 003773 #endif 003774 003775 /* Create a new savepoint structure. */ 003776 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1); 003777 if( pNew ){ 003778 pNew->zName = (char *)&pNew[1]; 003779 memcpy(pNew->zName, zName, nName+1); 003780 003781 /* If there is no open transaction, then mark this as a special 003782 ** "transaction savepoint". */ 003783 if( db->autoCommit ){ 003784 db->autoCommit = 0; 003785 db->isTransactionSavepoint = 1; 003786 }else{ 003787 db->nSavepoint++; 003788 } 003789 003790 /* Link the new savepoint into the database handle's list. */ 003791 pNew->pNext = db->pSavepoint; 003792 db->pSavepoint = pNew; 003793 pNew->nDeferredCons = db->nDeferredCons; 003794 pNew->nDeferredImmCons = db->nDeferredImmCons; 003795 } 003796 } 003797 }else{ 003798 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK ); 003799 iSavepoint = 0; 003800 003801 /* Find the named savepoint. If there is no such savepoint, then an 003802 ** an error is returned to the user. */ 003803 for( 003804 pSavepoint = db->pSavepoint; 003805 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); 003806 pSavepoint = pSavepoint->pNext 003807 ){ 003808 iSavepoint++; 003809 } 003810 if( !pSavepoint ){ 003811 sqlite3VdbeError(p, "no such savepoint: %s", zName); 003812 rc = SQLITE_ERROR; 003813 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ 003814 /* It is not possible to release (commit) a savepoint if there are 003815 ** active write statements. 003816 */ 003817 sqlite3VdbeError(p, "cannot release savepoint - " 003818 "SQL statements in progress"); 003819 rc = SQLITE_BUSY; 003820 }else{ 003821 003822 /* Determine whether or not this is a transaction savepoint. If so, 003823 ** and this is a RELEASE command, then the current transaction 003824 ** is committed. 003825 */ 003826 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; 003827 if( isTransaction && p1==SAVEPOINT_RELEASE ){ 003828 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ 003829 goto vdbe_return; 003830 } 003831 db->autoCommit = 1; 003832 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ 003833 p->pc = (int)(pOp - aOp); 003834 db->autoCommit = 0; 003835 p->rc = rc = SQLITE_BUSY; 003836 goto vdbe_return; 003837 } 003838 rc = p->rc; 003839 if( rc ){ 003840 db->autoCommit = 0; 003841 }else{ 003842 db->isTransactionSavepoint = 0; 003843 } 003844 }else{ 003845 int isSchemaChange; 003846 iSavepoint = db->nSavepoint - iSavepoint - 1; 003847 if( p1==SAVEPOINT_ROLLBACK ){ 003848 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0; 003849 for(ii=0; ii<db->nDb; ii++){ 003850 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, 003851 SQLITE_ABORT_ROLLBACK, 003852 isSchemaChange==0); 003853 if( rc!=SQLITE_OK ) goto abort_due_to_error; 003854 } 003855 }else{ 003856 assert( p1==SAVEPOINT_RELEASE ); 003857 isSchemaChange = 0; 003858 } 003859 for(ii=0; ii<db->nDb; ii++){ 003860 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); 003861 if( rc!=SQLITE_OK ){ 003862 goto abort_due_to_error; 003863 } 003864 } 003865 if( isSchemaChange ){ 003866 sqlite3ExpirePreparedStatements(db, 0); 003867 sqlite3ResetAllSchemasOfConnection(db); 003868 db->mDbFlags |= DBFLAG_SchemaChange; 003869 } 003870 } 003871 if( rc ) goto abort_due_to_error; 003872 003873 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all 003874 ** savepoints nested inside of the savepoint being operated on. */ 003875 while( db->pSavepoint!=pSavepoint ){ 003876 pTmp = db->pSavepoint; 003877 db->pSavepoint = pTmp->pNext; 003878 sqlite3DbFree(db, pTmp); 003879 db->nSavepoint--; 003880 } 003881 003882 /* If it is a RELEASE, then destroy the savepoint being operated on 003883 ** too. If it is a ROLLBACK TO, then set the number of deferred 003884 ** constraint violations present in the database to the value stored 003885 ** when the savepoint was created. */ 003886 if( p1==SAVEPOINT_RELEASE ){ 003887 assert( pSavepoint==db->pSavepoint ); 003888 db->pSavepoint = pSavepoint->pNext; 003889 sqlite3DbFree(db, pSavepoint); 003890 if( !isTransaction ){ 003891 db->nSavepoint--; 003892 } 003893 }else{ 003894 assert( p1==SAVEPOINT_ROLLBACK ); 003895 db->nDeferredCons = pSavepoint->nDeferredCons; 003896 db->nDeferredImmCons = pSavepoint->nDeferredImmCons; 003897 } 003898 003899 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ 003900 rc = sqlite3VtabSavepoint(db, p1, iSavepoint); 003901 if( rc!=SQLITE_OK ) goto abort_due_to_error; 003902 } 003903 } 003904 } 003905 if( rc ) goto abort_due_to_error; 003906 if( p->eVdbeState==VDBE_HALT_STATE ){ 003907 rc = SQLITE_DONE; 003908 goto vdbe_return; 003909 } 003910 break; 003911 } 003912 003913 /* Opcode: AutoCommit P1 P2 * * * 003914 ** 003915 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll 003916 ** back any currently active btree transactions. If there are any active 003917 ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if 003918 ** there are active writing VMs or active VMs that use shared cache. 003919 ** 003920 ** This instruction causes the VM to halt. 003921 */ 003922 case OP_AutoCommit: { 003923 int desiredAutoCommit; 003924 int iRollback; 003925 003926 desiredAutoCommit = pOp->p1; 003927 iRollback = pOp->p2; 003928 assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); 003929 assert( desiredAutoCommit==1 || iRollback==0 ); 003930 assert( db->nVdbeActive>0 ); /* At least this one VM is active */ 003931 assert( p->bIsReader ); 003932 003933 if( desiredAutoCommit!=db->autoCommit ){ 003934 if( iRollback ){ 003935 assert( desiredAutoCommit==1 ); 003936 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); 003937 db->autoCommit = 1; 003938 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){ 003939 /* If this instruction implements a COMMIT and other VMs are writing 003940 ** return an error indicating that the other VMs must complete first. 003941 */ 003942 sqlite3VdbeError(p, "cannot commit transaction - " 003943 "SQL statements in progress"); 003944 rc = SQLITE_BUSY; 003945 goto abort_due_to_error; 003946 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ 003947 goto vdbe_return; 003948 }else{ 003949 db->autoCommit = (u8)desiredAutoCommit; 003950 } 003951 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ 003952 p->pc = (int)(pOp - aOp); 003953 db->autoCommit = (u8)(1-desiredAutoCommit); 003954 p->rc = rc = SQLITE_BUSY; 003955 goto vdbe_return; 003956 } 003957 sqlite3CloseSavepoints(db); 003958 if( p->rc==SQLITE_OK ){ 003959 rc = SQLITE_DONE; 003960 }else{ 003961 rc = SQLITE_ERROR; 003962 } 003963 goto vdbe_return; 003964 }else{ 003965 sqlite3VdbeError(p, 003966 (!desiredAutoCommit)?"cannot start a transaction within a transaction":( 003967 (iRollback)?"cannot rollback - no transaction is active": 003968 "cannot commit - no transaction is active")); 003969 003970 rc = SQLITE_ERROR; 003971 goto abort_due_to_error; 003972 } 003973 /*NOTREACHED*/ assert(0); 003974 } 003975 003976 /* Opcode: Transaction P1 P2 P3 P4 P5 003977 ** 003978 ** Begin a transaction on database P1 if a transaction is not already 003979 ** active. 003980 ** If P2 is non-zero, then a write-transaction is started, or if a 003981 ** read-transaction is already active, it is upgraded to a write-transaction. 003982 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more 003983 ** then an exclusive transaction is started. 003984 ** 003985 ** P1 is the index of the database file on which the transaction is 003986 ** started. Index 0 is the main database file and index 1 is the 003987 ** file used for temporary tables. Indices of 2 or more are used for 003988 ** attached databases. 003989 ** 003990 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is 003991 ** true (this flag is set if the Vdbe may modify more than one row and may 003992 ** throw an ABORT exception), a statement transaction may also be opened. 003993 ** More specifically, a statement transaction is opened iff the database 003994 ** connection is currently not in autocommit mode, or if there are other 003995 ** active statements. A statement transaction allows the changes made by this 003996 ** VDBE to be rolled back after an error without having to roll back the 003997 ** entire transaction. If no error is encountered, the statement transaction 003998 ** will automatically commit when the VDBE halts. 003999 ** 004000 ** If P5!=0 then this opcode also checks the schema cookie against P3 004001 ** and the schema generation counter against P4. 004002 ** The cookie changes its value whenever the database schema changes. 004003 ** This operation is used to detect when that the cookie has changed 004004 ** and that the current process needs to reread the schema. If the schema 004005 ** cookie in P3 differs from the schema cookie in the database header or 004006 ** if the schema generation counter in P4 differs from the current 004007 ** generation counter, then an SQLITE_SCHEMA error is raised and execution 004008 ** halts. The sqlite3_step() wrapper function might then reprepare the 004009 ** statement and rerun it from the beginning. 004010 */ 004011 case OP_Transaction: { 004012 Btree *pBt; 004013 Db *pDb; 004014 int iMeta = 0; 004015 004016 assert( p->bIsReader ); 004017 assert( p->readOnly==0 || pOp->p2==0 ); 004018 assert( pOp->p2>=0 && pOp->p2<=2 ); 004019 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 004020 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 004021 assert( rc==SQLITE_OK ); 004022 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){ 004023 if( db->flags & SQLITE_QueryOnly ){ 004024 /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */ 004025 rc = SQLITE_READONLY; 004026 }else{ 004027 /* Writes prohibited due to a prior SQLITE_CORRUPT in the current 004028 ** transaction */ 004029 rc = SQLITE_CORRUPT; 004030 } 004031 goto abort_due_to_error; 004032 } 004033 pDb = &db->aDb[pOp->p1]; 004034 pBt = pDb->pBt; 004035 004036 if( pBt ){ 004037 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta); 004038 testcase( rc==SQLITE_BUSY_SNAPSHOT ); 004039 testcase( rc==SQLITE_BUSY_RECOVERY ); 004040 if( rc!=SQLITE_OK ){ 004041 if( (rc&0xff)==SQLITE_BUSY ){ 004042 p->pc = (int)(pOp - aOp); 004043 p->rc = rc; 004044 goto vdbe_return; 004045 } 004046 goto abort_due_to_error; 004047 } 004048 004049 if( p->usesStmtJournal 004050 && pOp->p2 004051 && (db->autoCommit==0 || db->nVdbeRead>1) 004052 ){ 004053 assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE ); 004054 if( p->iStatement==0 ){ 004055 assert( db->nStatement>=0 && db->nSavepoint>=0 ); 004056 db->nStatement++; 004057 p->iStatement = db->nSavepoint + db->nStatement; 004058 } 004059 004060 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); 004061 if( rc==SQLITE_OK ){ 004062 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); 004063 } 004064 004065 /* Store the current value of the database handles deferred constraint 004066 ** counter. If the statement transaction needs to be rolled back, 004067 ** the value of this counter needs to be restored too. */ 004068 p->nStmtDefCons = db->nDeferredCons; 004069 p->nStmtDefImmCons = db->nDeferredImmCons; 004070 } 004071 } 004072 assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); 004073 if( rc==SQLITE_OK 004074 && pOp->p5 004075 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i) 004076 ){ 004077 /* 004078 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema 004079 ** version is checked to ensure that the schema has not changed since the 004080 ** SQL statement was prepared. 004081 */ 004082 sqlite3DbFree(db, p->zErrMsg); 004083 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); 004084 /* If the schema-cookie from the database file matches the cookie 004085 ** stored with the in-memory representation of the schema, do 004086 ** not reload the schema from the database file. 004087 ** 004088 ** If virtual-tables are in use, this is not just an optimization. 004089 ** Often, v-tables store their data in other SQLite tables, which 004090 ** are queried from within xNext() and other v-table methods using 004091 ** prepared queries. If such a query is out-of-date, we do not want to 004092 ** discard the database schema, as the user code implementing the 004093 ** v-table would have to be ready for the sqlite3_vtab structure itself 004094 ** to be invalidated whenever sqlite3_step() is called from within 004095 ** a v-table method. 004096 */ 004097 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ 004098 sqlite3ResetOneSchema(db, pOp->p1); 004099 } 004100 p->expired = 1; 004101 rc = SQLITE_SCHEMA; 004102 004103 /* Set changeCntOn to 0 to prevent the value returned by sqlite3_changes() 004104 ** from being modified in sqlite3VdbeHalt(). If this statement is 004105 ** reprepared, changeCntOn will be set again. */ 004106 p->changeCntOn = 0; 004107 } 004108 if( rc ) goto abort_due_to_error; 004109 break; 004110 } 004111 004112 /* Opcode: ReadCookie P1 P2 P3 * * 004113 ** 004114 ** Read cookie number P3 from database P1 and write it into register P2. 004115 ** P3==1 is the schema version. P3==2 is the database format. 004116 ** P3==3 is the recommended pager cache size, and so forth. P1==0 is 004117 ** the main database file and P1==1 is the database file used to store 004118 ** temporary tables. 004119 ** 004120 ** There must be a read-lock on the database (either a transaction 004121 ** must be started or there must be an open cursor) before 004122 ** executing this instruction. 004123 */ 004124 case OP_ReadCookie: { /* out2 */ 004125 int iMeta; 004126 int iDb; 004127 int iCookie; 004128 004129 assert( p->bIsReader ); 004130 iDb = pOp->p1; 004131 iCookie = pOp->p3; 004132 assert( pOp->p3<SQLITE_N_BTREE_META ); 004133 assert( iDb>=0 && iDb<db->nDb ); 004134 assert( db->aDb[iDb].pBt!=0 ); 004135 assert( DbMaskTest(p->btreeMask, iDb) ); 004136 004137 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); 004138 pOut = out2Prerelease(p, pOp); 004139 pOut->u.i = iMeta; 004140 break; 004141 } 004142 004143 /* Opcode: SetCookie P1 P2 P3 * P5 004144 ** 004145 ** Write the integer value P3 into cookie number P2 of database P1. 004146 ** P2==1 is the schema version. P2==2 is the database format. 004147 ** P2==3 is the recommended pager cache 004148 ** size, and so forth. P1==0 is the main database file and P1==1 is the 004149 ** database file used to store temporary tables. 004150 ** 004151 ** A transaction must be started before executing this opcode. 004152 ** 004153 ** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal 004154 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement 004155 ** has P5 set to 1, so that the internal schema version will be different 004156 ** from the database schema version, resulting in a schema reset. 004157 */ 004158 case OP_SetCookie: { 004159 Db *pDb; 004160 004161 sqlite3VdbeIncrWriteCounter(p, 0); 004162 assert( pOp->p2<SQLITE_N_BTREE_META ); 004163 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 004164 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 004165 assert( p->readOnly==0 ); 004166 pDb = &db->aDb[pOp->p1]; 004167 assert( pDb->pBt!=0 ); 004168 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); 004169 /* See note about index shifting on OP_ReadCookie */ 004170 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3); 004171 if( pOp->p2==BTREE_SCHEMA_VERSION ){ 004172 /* When the schema cookie changes, record the new cookie internally */ 004173 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5; 004174 db->mDbFlags |= DBFLAG_SchemaChange; 004175 sqlite3FkClearTriggerCache(db, pOp->p1); 004176 }else if( pOp->p2==BTREE_FILE_FORMAT ){ 004177 /* Record changes in the file format */ 004178 pDb->pSchema->file_format = pOp->p3; 004179 } 004180 if( pOp->p1==1 ){ 004181 /* Invalidate all prepared statements whenever the TEMP database 004182 ** schema is changed. Ticket #1644 */ 004183 sqlite3ExpirePreparedStatements(db, 0); 004184 p->expired = 0; 004185 } 004186 if( rc ) goto abort_due_to_error; 004187 break; 004188 } 004189 004190 /* Opcode: OpenRead P1 P2 P3 P4 P5 004191 ** Synopsis: root=P2 iDb=P3 004192 ** 004193 ** Open a read-only cursor for the database table whose root page is 004194 ** P2 in a database file. The database file is determined by P3. 004195 ** P3==0 means the main database, P3==1 means the database used for 004196 ** temporary tables, and P3>1 means used the corresponding attached 004197 ** database. Give the new cursor an identifier of P1. The P1 004198 ** values need not be contiguous but all P1 values should be small integers. 004199 ** It is an error for P1 to be negative. 004200 ** 004201 ** Allowed P5 bits: 004202 ** <ul> 004203 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for 004204 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT 004205 ** of OP_SeekLE/OP_IdxLT) 004206 ** </ul> 004207 ** 004208 ** The P4 value may be either an integer (P4_INT32) or a pointer to 004209 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 004210 ** object, then table being opened must be an [index b-tree] where the 004211 ** KeyInfo object defines the content and collating 004212 ** sequence of that index b-tree. Otherwise, if P4 is an integer 004213 ** value, then the table being opened must be a [table b-tree] with a 004214 ** number of columns no less than the value of P4. 004215 ** 004216 ** See also: OpenWrite, ReopenIdx 004217 */ 004218 /* Opcode: ReopenIdx P1 P2 P3 P4 P5 004219 ** Synopsis: root=P2 iDb=P3 004220 ** 004221 ** The ReopenIdx opcode works like OP_OpenRead except that it first 004222 ** checks to see if the cursor on P1 is already open on the same 004223 ** b-tree and if it is this opcode becomes a no-op. In other words, 004224 ** if the cursor is already open, do not reopen it. 004225 ** 004226 ** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ 004227 ** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must 004228 ** be the same as every other ReopenIdx or OpenRead for the same cursor 004229 ** number. 004230 ** 004231 ** Allowed P5 bits: 004232 ** <ul> 004233 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for 004234 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT 004235 ** of OP_SeekLE/OP_IdxLT) 004236 ** </ul> 004237 ** 004238 ** See also: OP_OpenRead, OP_OpenWrite 004239 */ 004240 /* Opcode: OpenWrite P1 P2 P3 P4 P5 004241 ** Synopsis: root=P2 iDb=P3 004242 ** 004243 ** Open a read/write cursor named P1 on the table or index whose root 004244 ** page is P2 (or whose root page is held in register P2 if the 004245 ** OPFLAG_P2ISREG bit is set in P5 - see below). 004246 ** 004247 ** The P4 value may be either an integer (P4_INT32) or a pointer to 004248 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 004249 ** object, then table being opened must be an [index b-tree] where the 004250 ** KeyInfo object defines the content and collating 004251 ** sequence of that index b-tree. Otherwise, if P4 is an integer 004252 ** value, then the table being opened must be a [table b-tree] with a 004253 ** number of columns no less than the value of P4. 004254 ** 004255 ** Allowed P5 bits: 004256 ** <ul> 004257 ** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for 004258 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT 004259 ** of OP_SeekLE/OP_IdxLT) 004260 ** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek 004261 ** and subsequently delete entries in an index btree. This is a 004262 ** hint to the storage engine that the storage engine is allowed to 004263 ** ignore. The hint is not used by the official SQLite b*tree storage 004264 ** engine, but is used by COMDB2. 004265 ** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2 004266 ** as the root page, not the value of P2 itself. 004267 ** </ul> 004268 ** 004269 ** This instruction works like OpenRead except that it opens the cursor 004270 ** in read/write mode. 004271 ** 004272 ** See also: OP_OpenRead, OP_ReopenIdx 004273 */ 004274 case OP_ReopenIdx: { /* ncycle */ 004275 int nField; 004276 KeyInfo *pKeyInfo; 004277 u32 p2; 004278 int iDb; 004279 int wrFlag; 004280 Btree *pX; 004281 VdbeCursor *pCur; 004282 Db *pDb; 004283 004284 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); 004285 assert( pOp->p4type==P4_KEYINFO ); 004286 pCur = p->apCsr[pOp->p1]; 004287 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ 004288 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ 004289 assert( pCur->eCurType==CURTYPE_BTREE ); 004290 sqlite3BtreeClearCursor(pCur->uc.pCursor); 004291 goto open_cursor_set_hints; 004292 } 004293 /* If the cursor is not currently open or is open on a different 004294 ** index, then fall through into OP_OpenRead to force a reopen */ 004295 case OP_OpenRead: /* ncycle */ 004296 case OP_OpenWrite: 004297 004298 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); 004299 assert( p->bIsReader ); 004300 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx 004301 || p->readOnly==0 ); 004302 004303 if( p->expired==1 ){ 004304 rc = SQLITE_ABORT_ROLLBACK; 004305 goto abort_due_to_error; 004306 } 004307 004308 nField = 0; 004309 pKeyInfo = 0; 004310 p2 = (u32)pOp->p2; 004311 iDb = pOp->p3; 004312 assert( iDb>=0 && iDb<db->nDb ); 004313 assert( DbMaskTest(p->btreeMask, iDb) ); 004314 pDb = &db->aDb[iDb]; 004315 pX = pDb->pBt; 004316 assert( pX!=0 ); 004317 if( pOp->opcode==OP_OpenWrite ){ 004318 assert( OPFLAG_FORDELETE==BTREE_FORDELETE ); 004319 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE); 004320 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 004321 if( pDb->pSchema->file_format < p->minWriteFileFormat ){ 004322 p->minWriteFileFormat = pDb->pSchema->file_format; 004323 } 004324 }else{ 004325 wrFlag = 0; 004326 } 004327 if( pOp->p5 & OPFLAG_P2ISREG ){ 004328 assert( p2>0 ); 004329 assert( p2<=(u32)(p->nMem+1 - p->nCursor) ); 004330 assert( pOp->opcode==OP_OpenWrite ); 004331 pIn2 = &aMem[p2]; 004332 assert( memIsValid(pIn2) ); 004333 assert( (pIn2->flags & MEM_Int)!=0 ); 004334 sqlite3VdbeMemIntegerify(pIn2); 004335 p2 = (int)pIn2->u.i; 004336 /* The p2 value always comes from a prior OP_CreateBtree opcode and 004337 ** that opcode will always set the p2 value to 2 or more or else fail. 004338 ** If there were a failure, the prepared statement would have halted 004339 ** before reaching this instruction. */ 004340 assert( p2>=2 ); 004341 } 004342 if( pOp->p4type==P4_KEYINFO ){ 004343 pKeyInfo = pOp->p4.pKeyInfo; 004344 assert( pKeyInfo->enc==ENC(db) ); 004345 assert( pKeyInfo->db==db ); 004346 nField = pKeyInfo->nAllField; 004347 }else if( pOp->p4type==P4_INT32 ){ 004348 nField = pOp->p4.i; 004349 } 004350 assert( pOp->p1>=0 ); 004351 assert( nField>=0 ); 004352 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ 004353 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE); 004354 if( pCur==0 ) goto no_mem; 004355 pCur->iDb = iDb; 004356 pCur->nullRow = 1; 004357 pCur->isOrdered = 1; 004358 pCur->pgnoRoot = p2; 004359 #ifdef SQLITE_DEBUG 004360 pCur->wrFlag = wrFlag; 004361 #endif 004362 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor); 004363 pCur->pKeyInfo = pKeyInfo; 004364 /* Set the VdbeCursor.isTable variable. Previous versions of 004365 ** SQLite used to check if the root-page flags were sane at this point 004366 ** and report database corruption if they were not, but this check has 004367 ** since moved into the btree layer. */ 004368 pCur->isTable = pOp->p4type!=P4_KEYINFO; 004369 004370 open_cursor_set_hints: 004371 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); 004372 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); 004373 testcase( pOp->p5 & OPFLAG_BULKCSR ); 004374 testcase( pOp->p2 & OPFLAG_SEEKEQ ); 004375 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor, 004376 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); 004377 if( rc ) goto abort_due_to_error; 004378 break; 004379 } 004380 004381 /* Opcode: OpenDup P1 P2 * * * 004382 ** 004383 ** Open a new cursor P1 that points to the same ephemeral table as 004384 ** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral 004385 ** opcode. Only ephemeral cursors may be duplicated. 004386 ** 004387 ** Duplicate ephemeral cursors are used for self-joins of materialized views. 004388 */ 004389 case OP_OpenDup: { /* ncycle */ 004390 VdbeCursor *pOrig; /* The original cursor to be duplicated */ 004391 VdbeCursor *pCx; /* The new cursor */ 004392 004393 pOrig = p->apCsr[pOp->p2]; 004394 assert( pOrig ); 004395 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */ 004396 004397 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE); 004398 if( pCx==0 ) goto no_mem; 004399 pCx->nullRow = 1; 004400 pCx->isEphemeral = 1; 004401 pCx->pKeyInfo = pOrig->pKeyInfo; 004402 pCx->isTable = pOrig->isTable; 004403 pCx->pgnoRoot = pOrig->pgnoRoot; 004404 pCx->isOrdered = pOrig->isOrdered; 004405 pCx->ub.pBtx = pOrig->ub.pBtx; 004406 pCx->noReuse = 1; 004407 pOrig->noReuse = 1; 004408 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR, 004409 pCx->pKeyInfo, pCx->uc.pCursor); 004410 /* The sqlite3BtreeCursor() routine can only fail for the first cursor 004411 ** opened for a database. Since there is already an open cursor when this 004412 ** opcode is run, the sqlite3BtreeCursor() cannot fail */ 004413 assert( rc==SQLITE_OK ); 004414 break; 004415 } 004416 004417 004418 /* Opcode: OpenEphemeral P1 P2 P3 P4 P5 004419 ** Synopsis: nColumn=P2 004420 ** 004421 ** Open a new cursor P1 to a transient table. 004422 ** The cursor is always opened read/write even if 004423 ** the main database is read-only. The ephemeral 004424 ** table is deleted automatically when the cursor is closed. 004425 ** 004426 ** If the cursor P1 is already opened on an ephemeral table, the table 004427 ** is cleared (all content is erased). 004428 ** 004429 ** P2 is the number of columns in the ephemeral table. 004430 ** The cursor points to a BTree table if P4==0 and to a BTree index 004431 ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure 004432 ** that defines the format of keys in the index. 004433 ** 004434 ** The P5 parameter can be a mask of the BTREE_* flags defined 004435 ** in btree.h. These flags control aspects of the operation of 004436 ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are 004437 ** added automatically. 004438 ** 004439 ** If P3 is positive, then reg[P3] is modified slightly so that it 004440 ** can be used as zero-length data for OP_Insert. This is an optimization 004441 ** that avoids an extra OP_Blob opcode to initialize that register. 004442 */ 004443 /* Opcode: OpenAutoindex P1 P2 * P4 * 004444 ** Synopsis: nColumn=P2 004445 ** 004446 ** This opcode works the same as OP_OpenEphemeral. It has a 004447 ** different name to distinguish its use. Tables created using 004448 ** by this opcode will be used for automatically created transient 004449 ** indices in joins. 004450 */ 004451 case OP_OpenAutoindex: /* ncycle */ 004452 case OP_OpenEphemeral: { /* ncycle */ 004453 VdbeCursor *pCx; 004454 KeyInfo *pKeyInfo; 004455 004456 static const int vfsFlags = 004457 SQLITE_OPEN_READWRITE | 004458 SQLITE_OPEN_CREATE | 004459 SQLITE_OPEN_EXCLUSIVE | 004460 SQLITE_OPEN_DELETEONCLOSE | 004461 SQLITE_OPEN_TRANSIENT_DB; 004462 assert( pOp->p1>=0 ); 004463 assert( pOp->p2>=0 ); 004464 if( pOp->p3>0 ){ 004465 /* Make register reg[P3] into a value that can be used as the data 004466 ** form sqlite3BtreeInsert() where the length of the data is zero. */ 004467 assert( pOp->p2==0 ); /* Only used when number of columns is zero */ 004468 assert( pOp->opcode==OP_OpenEphemeral ); 004469 assert( aMem[pOp->p3].flags & MEM_Null ); 004470 aMem[pOp->p3].n = 0; 004471 aMem[pOp->p3].z = ""; 004472 } 004473 pCx = p->apCsr[pOp->p1]; 004474 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){ 004475 /* If the ephemeral table is already open and has no duplicates from 004476 ** OP_OpenDup, then erase all existing content so that the table is 004477 ** empty again, rather than creating a new table. */ 004478 assert( pCx->isEphemeral ); 004479 pCx->seqCount = 0; 004480 pCx->cacheStatus = CACHE_STALE; 004481 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0); 004482 }else{ 004483 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE); 004484 if( pCx==0 ) goto no_mem; 004485 pCx->isEphemeral = 1; 004486 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx, 004487 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, 004488 vfsFlags); 004489 if( rc==SQLITE_OK ){ 004490 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0); 004491 if( rc==SQLITE_OK ){ 004492 /* If a transient index is required, create it by calling 004493 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before 004494 ** opening it. If a transient table is required, just use the 004495 ** automatically created table with root-page 1 (an BLOB_INTKEY table). 004496 */ 004497 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ 004498 assert( pOp->p4type==P4_KEYINFO ); 004499 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot, 004500 BTREE_BLOBKEY | pOp->p5); 004501 if( rc==SQLITE_OK ){ 004502 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 ); 004503 assert( pKeyInfo->db==db ); 004504 assert( pKeyInfo->enc==ENC(db) ); 004505 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR, 004506 pKeyInfo, pCx->uc.pCursor); 004507 } 004508 pCx->isTable = 0; 004509 }else{ 004510 pCx->pgnoRoot = SCHEMA_ROOT; 004511 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR, 004512 0, pCx->uc.pCursor); 004513 pCx->isTable = 1; 004514 } 004515 } 004516 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); 004517 if( rc ){ 004518 sqlite3BtreeClose(pCx->ub.pBtx); 004519 } 004520 } 004521 } 004522 if( rc ) goto abort_due_to_error; 004523 pCx->nullRow = 1; 004524 break; 004525 } 004526 004527 /* Opcode: SorterOpen P1 P2 P3 P4 * 004528 ** 004529 ** This opcode works like OP_OpenEphemeral except that it opens 004530 ** a transient index that is specifically designed to sort large 004531 ** tables using an external merge-sort algorithm. 004532 ** 004533 ** If argument P3 is non-zero, then it indicates that the sorter may 004534 ** assume that a stable sort considering the first P3 fields of each 004535 ** key is sufficient to produce the required results. 004536 */ 004537 case OP_SorterOpen: { 004538 VdbeCursor *pCx; 004539 004540 assert( pOp->p1>=0 ); 004541 assert( pOp->p2>=0 ); 004542 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER); 004543 if( pCx==0 ) goto no_mem; 004544 pCx->pKeyInfo = pOp->p4.pKeyInfo; 004545 assert( pCx->pKeyInfo->db==db ); 004546 assert( pCx->pKeyInfo->enc==ENC(db) ); 004547 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); 004548 if( rc ) goto abort_due_to_error; 004549 break; 004550 } 004551 004552 /* Opcode: SequenceTest P1 P2 * * * 004553 ** Synopsis: if( cursor[P1].ctr++ ) pc = P2 004554 ** 004555 ** P1 is a sorter cursor. If the sequence counter is currently zero, jump 004556 ** to P2. Regardless of whether or not the jump is taken, increment the 004557 ** the sequence value. 004558 */ 004559 case OP_SequenceTest: { 004560 VdbeCursor *pC; 004561 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004562 pC = p->apCsr[pOp->p1]; 004563 assert( isSorter(pC) ); 004564 if( (pC->seqCount++)==0 ){ 004565 goto jump_to_p2; 004566 } 004567 break; 004568 } 004569 004570 /* Opcode: OpenPseudo P1 P2 P3 * * 004571 ** Synopsis: P3 columns in r[P2] 004572 ** 004573 ** Open a new cursor that points to a fake table that contains a single 004574 ** row of data. The content of that one row is the content of memory 004575 ** register P2. In other words, cursor P1 becomes an alias for the 004576 ** MEM_Blob content contained in register P2. 004577 ** 004578 ** A pseudo-table created by this opcode is used to hold a single 004579 ** row output from the sorter so that the row can be decomposed into 004580 ** individual columns using the OP_Column opcode. The OP_Column opcode 004581 ** is the only cursor opcode that works with a pseudo-table. 004582 ** 004583 ** P3 is the number of fields in the records that will be stored by 004584 ** the pseudo-table. 004585 */ 004586 case OP_OpenPseudo: { 004587 VdbeCursor *pCx; 004588 004589 assert( pOp->p1>=0 ); 004590 assert( pOp->p3>=0 ); 004591 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO); 004592 if( pCx==0 ) goto no_mem; 004593 pCx->nullRow = 1; 004594 pCx->seekResult = pOp->p2; 004595 pCx->isTable = 1; 004596 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx 004597 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test 004598 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto() 004599 ** which is a performance optimization */ 004600 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor(); 004601 assert( pOp->p5==0 ); 004602 break; 004603 } 004604 004605 /* Opcode: Close P1 * * * * 004606 ** 004607 ** Close a cursor previously opened as P1. If P1 is not 004608 ** currently open, this instruction is a no-op. 004609 */ 004610 case OP_Close: { /* ncycle */ 004611 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004612 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); 004613 p->apCsr[pOp->p1] = 0; 004614 break; 004615 } 004616 004617 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK 004618 /* Opcode: ColumnsUsed P1 * * P4 * 004619 ** 004620 ** This opcode (which only exists if SQLite was compiled with 004621 ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the 004622 ** table or index for cursor P1 are used. P4 is a 64-bit integer 004623 ** (P4_INT64) in which the first 63 bits are one for each of the 004624 ** first 63 columns of the table or index that are actually used 004625 ** by the cursor. The high-order bit is set if any column after 004626 ** the 64th is used. 004627 */ 004628 case OP_ColumnsUsed: { 004629 VdbeCursor *pC; 004630 pC = p->apCsr[pOp->p1]; 004631 assert( pC->eCurType==CURTYPE_BTREE ); 004632 pC->maskUsed = *(u64*)pOp->p4.pI64; 004633 break; 004634 } 004635 #endif 004636 004637 /* Opcode: SeekGE P1 P2 P3 P4 * 004638 ** Synopsis: key=r[P3@P4] 004639 ** 004640 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 004641 ** use the value in register P3 as the key. If cursor P1 refers 004642 ** to an SQL index, then P3 is the first in an array of P4 registers 004643 ** that are used as an unpacked index key. 004644 ** 004645 ** Reposition cursor P1 so that it points to the smallest entry that 004646 ** is greater than or equal to the key value. If there are no records 004647 ** greater than or equal to the key and P2 is not zero, then jump to P2. 004648 ** 004649 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this 004650 ** opcode will either land on a record that exactly matches the key, or 004651 ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ, 004652 ** this opcode must be followed by an IdxLE opcode with the same arguments. 004653 ** The IdxGT opcode will be skipped if this opcode succeeds, but the 004654 ** IdxGT opcode will be used on subsequent loop iterations. The 004655 ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this 004656 ** is an equality search. 004657 ** 004658 ** This opcode leaves the cursor configured to move in forward order, 004659 ** from the beginning toward the end. In other words, the cursor is 004660 ** configured to use Next, not Prev. 004661 ** 004662 ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe 004663 */ 004664 /* Opcode: SeekGT P1 P2 P3 P4 * 004665 ** Synopsis: key=r[P3@P4] 004666 ** 004667 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 004668 ** use the value in register P3 as a key. If cursor P1 refers 004669 ** to an SQL index, then P3 is the first in an array of P4 registers 004670 ** that are used as an unpacked index key. 004671 ** 004672 ** Reposition cursor P1 so that it points to the smallest entry that 004673 ** is greater than the key value. If there are no records greater than 004674 ** the key and P2 is not zero, then jump to P2. 004675 ** 004676 ** This opcode leaves the cursor configured to move in forward order, 004677 ** from the beginning toward the end. In other words, the cursor is 004678 ** configured to use Next, not Prev. 004679 ** 004680 ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe 004681 */ 004682 /* Opcode: SeekLT P1 P2 P3 P4 * 004683 ** Synopsis: key=r[P3@P4] 004684 ** 004685 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 004686 ** use the value in register P3 as a key. If cursor P1 refers 004687 ** to an SQL index, then P3 is the first in an array of P4 registers 004688 ** that are used as an unpacked index key. 004689 ** 004690 ** Reposition cursor P1 so that it points to the largest entry that 004691 ** is less than the key value. If there are no records less than 004692 ** the key and P2 is not zero, then jump to P2. 004693 ** 004694 ** This opcode leaves the cursor configured to move in reverse order, 004695 ** from the end toward the beginning. In other words, the cursor is 004696 ** configured to use Prev, not Next. 004697 ** 004698 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe 004699 */ 004700 /* Opcode: SeekLE P1 P2 P3 P4 * 004701 ** Synopsis: key=r[P3@P4] 004702 ** 004703 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 004704 ** use the value in register P3 as a key. If cursor P1 refers 004705 ** to an SQL index, then P3 is the first in an array of P4 registers 004706 ** that are used as an unpacked index key. 004707 ** 004708 ** Reposition cursor P1 so that it points to the largest entry that 004709 ** is less than or equal to the key value. If there are no records 004710 ** less than or equal to the key and P2 is not zero, then jump to P2. 004711 ** 004712 ** This opcode leaves the cursor configured to move in reverse order, 004713 ** from the end toward the beginning. In other words, the cursor is 004714 ** configured to use Prev, not Next. 004715 ** 004716 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this 004717 ** opcode will either land on a record that exactly matches the key, or 004718 ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ, 004719 ** this opcode must be followed by an IdxLE opcode with the same arguments. 004720 ** The IdxGE opcode will be skipped if this opcode succeeds, but the 004721 ** IdxGE opcode will be used on subsequent loop iterations. The 004722 ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this 004723 ** is an equality search. 004724 ** 004725 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt 004726 */ 004727 case OP_SeekLT: /* jump, in3, group, ncycle */ 004728 case OP_SeekLE: /* jump, in3, group, ncycle */ 004729 case OP_SeekGE: /* jump, in3, group, ncycle */ 004730 case OP_SeekGT: { /* jump, in3, group, ncycle */ 004731 int res; /* Comparison result */ 004732 int oc; /* Opcode */ 004733 VdbeCursor *pC; /* The cursor to seek */ 004734 UnpackedRecord r; /* The key to seek for */ 004735 int nField; /* Number of columns or fields in the key */ 004736 i64 iKey; /* The rowid we are to seek to */ 004737 int eqOnly; /* Only interested in == results */ 004738 004739 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004740 assert( pOp->p2!=0 ); 004741 pC = p->apCsr[pOp->p1]; 004742 assert( pC!=0 ); 004743 assert( pC->eCurType==CURTYPE_BTREE ); 004744 assert( OP_SeekLE == OP_SeekLT+1 ); 004745 assert( OP_SeekGE == OP_SeekLT+2 ); 004746 assert( OP_SeekGT == OP_SeekLT+3 ); 004747 assert( pC->isOrdered ); 004748 assert( pC->uc.pCursor!=0 ); 004749 oc = pOp->opcode; 004750 eqOnly = 0; 004751 pC->nullRow = 0; 004752 #ifdef SQLITE_DEBUG 004753 pC->seekOp = pOp->opcode; 004754 #endif 004755 004756 pC->deferredMoveto = 0; 004757 pC->cacheStatus = CACHE_STALE; 004758 if( pC->isTable ){ 004759 u16 flags3, newType; 004760 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */ 004761 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 004762 || CORRUPT_DB ); 004763 004764 /* The input value in P3 might be of any type: integer, real, string, 004765 ** blob, or NULL. But it needs to be an integer before we can do 004766 ** the seek, so convert it. */ 004767 pIn3 = &aMem[pOp->p3]; 004768 flags3 = pIn3->flags; 004769 if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){ 004770 applyNumericAffinity(pIn3, 0); 004771 } 004772 iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */ 004773 newType = pIn3->flags; /* Record the type after applying numeric affinity */ 004774 pIn3->flags = flags3; /* But convert the type back to its original */ 004775 004776 /* If the P3 value could not be converted into an integer without 004777 ** loss of information, then special processing is required... */ 004778 if( (newType & (MEM_Int|MEM_IntReal))==0 ){ 004779 int c; 004780 if( (newType & MEM_Real)==0 ){ 004781 if( (newType & MEM_Null) || oc>=OP_SeekGE ){ 004782 VdbeBranchTaken(1,2); 004783 goto jump_to_p2; 004784 }else{ 004785 rc = sqlite3BtreeLast(pC->uc.pCursor, &res); 004786 if( rc!=SQLITE_OK ) goto abort_due_to_error; 004787 goto seek_not_found; 004788 } 004789 } 004790 c = sqlite3IntFloatCompare(iKey, pIn3->u.r); 004791 004792 /* If the approximation iKey is larger than the actual real search 004793 ** term, substitute >= for > and < for <=. e.g. if the search term 004794 ** is 4.9 and the integer approximation 5: 004795 ** 004796 ** (x > 4.9) -> (x >= 5) 004797 ** (x <= 4.9) -> (x < 5) 004798 */ 004799 if( c>0 ){ 004800 assert( OP_SeekGE==(OP_SeekGT-1) ); 004801 assert( OP_SeekLT==(OP_SeekLE-1) ); 004802 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) ); 004803 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--; 004804 } 004805 004806 /* If the approximation iKey is smaller than the actual real search 004807 ** term, substitute <= for < and > for >=. */ 004808 else if( c<0 ){ 004809 assert( OP_SeekLE==(OP_SeekLT+1) ); 004810 assert( OP_SeekGT==(OP_SeekGE+1) ); 004811 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); 004812 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; 004813 } 004814 } 004815 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res); 004816 pC->movetoTarget = iKey; /* Used by OP_Delete */ 004817 if( rc!=SQLITE_OK ){ 004818 goto abort_due_to_error; 004819 } 004820 }else{ 004821 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the 004822 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be 004823 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively, 004824 ** with the same key. 004825 */ 004826 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){ 004827 eqOnly = 1; 004828 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE ); 004829 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); 004830 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT ); 004831 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT ); 004832 assert( pOp[1].p1==pOp[0].p1 ); 004833 assert( pOp[1].p2==pOp[0].p2 ); 004834 assert( pOp[1].p3==pOp[0].p3 ); 004835 assert( pOp[1].p4.i==pOp[0].p4.i ); 004836 } 004837 004838 nField = pOp->p4.i; 004839 assert( pOp->p4type==P4_INT32 ); 004840 assert( nField>0 ); 004841 r.pKeyInfo = pC->pKeyInfo; 004842 r.nField = (u16)nField; 004843 004844 /* The next line of code computes as follows, only faster: 004845 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ 004846 ** r.default_rc = -1; 004847 ** }else{ 004848 ** r.default_rc = +1; 004849 ** } 004850 */ 004851 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); 004852 assert( oc!=OP_SeekGT || r.default_rc==-1 ); 004853 assert( oc!=OP_SeekLE || r.default_rc==-1 ); 004854 assert( oc!=OP_SeekGE || r.default_rc==+1 ); 004855 assert( oc!=OP_SeekLT || r.default_rc==+1 ); 004856 004857 r.aMem = &aMem[pOp->p3]; 004858 #ifdef SQLITE_DEBUG 004859 { 004860 int i; 004861 for(i=0; i<r.nField; i++){ 004862 assert( memIsValid(&r.aMem[i]) ); 004863 if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]); 004864 } 004865 } 004866 #endif 004867 r.eqSeen = 0; 004868 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res); 004869 if( rc!=SQLITE_OK ){ 004870 goto abort_due_to_error; 004871 } 004872 if( eqOnly && r.eqSeen==0 ){ 004873 assert( res!=0 ); 004874 goto seek_not_found; 004875 } 004876 } 004877 #ifdef SQLITE_TEST 004878 sqlite3_search_count++; 004879 #endif 004880 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); 004881 if( res<0 || (res==0 && oc==OP_SeekGT) ){ 004882 res = 0; 004883 rc = sqlite3BtreeNext(pC->uc.pCursor, 0); 004884 if( rc!=SQLITE_OK ){ 004885 if( rc==SQLITE_DONE ){ 004886 rc = SQLITE_OK; 004887 res = 1; 004888 }else{ 004889 goto abort_due_to_error; 004890 } 004891 } 004892 }else{ 004893 res = 0; 004894 } 004895 }else{ 004896 assert( oc==OP_SeekLT || oc==OP_SeekLE ); 004897 if( res>0 || (res==0 && oc==OP_SeekLT) ){ 004898 res = 0; 004899 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0); 004900 if( rc!=SQLITE_OK ){ 004901 if( rc==SQLITE_DONE ){ 004902 rc = SQLITE_OK; 004903 res = 1; 004904 }else{ 004905 goto abort_due_to_error; 004906 } 004907 } 004908 }else{ 004909 /* res might be negative because the table is empty. Check to 004910 ** see if this is the case. 004911 */ 004912 res = sqlite3BtreeEof(pC->uc.pCursor); 004913 } 004914 } 004915 seek_not_found: 004916 assert( pOp->p2>0 ); 004917 VdbeBranchTaken(res!=0,2); 004918 if( res ){ 004919 goto jump_to_p2; 004920 }else if( eqOnly ){ 004921 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); 004922 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */ 004923 } 004924 break; 004925 } 004926 004927 004928 /* Opcode: SeekScan P1 P2 * * P5 004929 ** Synopsis: Scan-ahead up to P1 rows 004930 ** 004931 ** This opcode is a prefix opcode to OP_SeekGE. In other words, this 004932 ** opcode must be immediately followed by OP_SeekGE. This constraint is 004933 ** checked by assert() statements. 004934 ** 004935 ** This opcode uses the P1 through P4 operands of the subsequent 004936 ** OP_SeekGE. In the text that follows, the operands of the subsequent 004937 ** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only 004938 ** the P1, P2 and P5 operands of this opcode are also used, and are called 004939 ** This.P1, This.P2 and This.P5. 004940 ** 004941 ** This opcode helps to optimize IN operators on a multi-column index 004942 ** where the IN operator is on the later terms of the index by avoiding 004943 ** unnecessary seeks on the btree, substituting steps to the next row 004944 ** of the b-tree instead. A correct answer is obtained if this opcode 004945 ** is omitted or is a no-op. 004946 ** 004947 ** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which 004948 ** is the desired entry that we want the cursor SeekGE.P1 to be pointing 004949 ** to. Call this SeekGE.P3/P4 row the "target". 004950 ** 004951 ** If the SeekGE.P1 cursor is not currently pointing to a valid row, 004952 ** then this opcode is a no-op and control passes through into the OP_SeekGE. 004953 ** 004954 ** If the SeekGE.P1 cursor is pointing to a valid row, then that row 004955 ** might be the target row, or it might be near and slightly before the 004956 ** target row, or it might be after the target row. If the cursor is 004957 ** currently before the target row, then this opcode attempts to position 004958 ** the cursor on or after the target row by invoking sqlite3BtreeStep() 004959 ** on the cursor between 1 and This.P1 times. 004960 ** 004961 ** The This.P5 parameter is a flag that indicates what to do if the 004962 ** cursor ends up pointing at a valid row that is past the target 004963 ** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If 004964 ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0 004965 ** case occurs when there are no inequality constraints to the right of 004966 ** the IN constraint. The jump to SeekGE.P2 ends the loop. The P5!=0 case 004967 ** occurs when there are inequality constraints to the right of the IN 004968 ** operator. In that case, the This.P2 will point either directly to or 004969 ** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for 004970 ** loop terminate. 004971 ** 004972 ** Possible outcomes from this opcode:<ol> 004973 ** 004974 ** <li> If the cursor is initially not pointed to any valid row, then 004975 ** fall through into the subsequent OP_SeekGE opcode. 004976 ** 004977 ** <li> If the cursor is left pointing to a row that is before the target 004978 ** row, even after making as many as This.P1 calls to 004979 ** sqlite3BtreeNext(), then also fall through into OP_SeekGE. 004980 ** 004981 ** <li> If the cursor is left pointing at the target row, either because it 004982 ** was at the target row to begin with or because one or more 004983 ** sqlite3BtreeNext() calls moved the cursor to the target row, 004984 ** then jump to This.P2.., 004985 ** 004986 ** <li> If the cursor started out before the target row and a call to 004987 ** to sqlite3BtreeNext() moved the cursor off the end of the index 004988 ** (indicating that the target row definitely does not exist in the 004989 ** btree) then jump to SeekGE.P2, ending the loop. 004990 ** 004991 ** <li> If the cursor ends up on a valid row that is past the target row 004992 ** (indicating that the target row does not exist in the btree) then 004993 ** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0. 004994 ** </ol> 004995 */ 004996 case OP_SeekScan: { /* ncycle */ 004997 VdbeCursor *pC; 004998 int res; 004999 int nStep; 005000 UnpackedRecord r; 005001 005002 assert( pOp[1].opcode==OP_SeekGE ); 005003 005004 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the 005005 ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first 005006 ** opcode past the OP_SeekGE itself. */ 005007 assert( pOp->p2>=(int)(pOp-aOp)+2 ); 005008 #ifdef SQLITE_DEBUG 005009 if( pOp->p5==0 ){ 005010 /* There are no inequality constraints following the IN constraint. */ 005011 assert( pOp[1].p1==aOp[pOp->p2-1].p1 ); 005012 assert( pOp[1].p2==aOp[pOp->p2-1].p2 ); 005013 assert( pOp[1].p3==aOp[pOp->p2-1].p3 ); 005014 assert( aOp[pOp->p2-1].opcode==OP_IdxGT 005015 || aOp[pOp->p2-1].opcode==OP_IdxGE ); 005016 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE ); 005017 }else{ 005018 /* There are inequality constraints. */ 005019 assert( pOp->p2==(int)(pOp-aOp)+2 ); 005020 assert( aOp[pOp->p2-1].opcode==OP_SeekGE ); 005021 } 005022 #endif 005023 005024 assert( pOp->p1>0 ); 005025 pC = p->apCsr[pOp[1].p1]; 005026 assert( pC!=0 ); 005027 assert( pC->eCurType==CURTYPE_BTREE ); 005028 assert( !pC->isTable ); 005029 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){ 005030 #ifdef SQLITE_DEBUG 005031 if( db->flags&SQLITE_VdbeTrace ){ 005032 printf("... cursor not valid - fall through\n"); 005033 } 005034 #endif 005035 break; 005036 } 005037 nStep = pOp->p1; 005038 assert( nStep>=1 ); 005039 r.pKeyInfo = pC->pKeyInfo; 005040 r.nField = (u16)pOp[1].p4.i; 005041 r.default_rc = 0; 005042 r.aMem = &aMem[pOp[1].p3]; 005043 #ifdef SQLITE_DEBUG 005044 { 005045 int i; 005046 for(i=0; i<r.nField; i++){ 005047 assert( memIsValid(&r.aMem[i]) ); 005048 REGISTER_TRACE(pOp[1].p3+i, &aMem[pOp[1].p3+i]); 005049 } 005050 } 005051 #endif 005052 res = 0; /* Not needed. Only used to silence a warning. */ 005053 while(1){ 005054 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); 005055 if( rc ) goto abort_due_to_error; 005056 if( res>0 && pOp->p5==0 ){ 005057 seekscan_search_fail: 005058 /* Jump to SeekGE.P2, ending the loop */ 005059 #ifdef SQLITE_DEBUG 005060 if( db->flags&SQLITE_VdbeTrace ){ 005061 printf("... %d steps and then skip\n", pOp->p1 - nStep); 005062 } 005063 #endif 005064 VdbeBranchTaken(1,3); 005065 pOp++; 005066 goto jump_to_p2; 005067 } 005068 if( res>=0 ){ 005069 /* Jump to This.P2, bypassing the OP_SeekGE opcode */ 005070 #ifdef SQLITE_DEBUG 005071 if( db->flags&SQLITE_VdbeTrace ){ 005072 printf("... %d steps and then success\n", pOp->p1 - nStep); 005073 } 005074 #endif 005075 VdbeBranchTaken(2,3); 005076 goto jump_to_p2; 005077 break; 005078 } 005079 if( nStep<=0 ){ 005080 #ifdef SQLITE_DEBUG 005081 if( db->flags&SQLITE_VdbeTrace ){ 005082 printf("... fall through after %d steps\n", pOp->p1); 005083 } 005084 #endif 005085 VdbeBranchTaken(0,3); 005086 break; 005087 } 005088 nStep--; 005089 pC->cacheStatus = CACHE_STALE; 005090 rc = sqlite3BtreeNext(pC->uc.pCursor, 0); 005091 if( rc ){ 005092 if( rc==SQLITE_DONE ){ 005093 rc = SQLITE_OK; 005094 goto seekscan_search_fail; 005095 }else{ 005096 goto abort_due_to_error; 005097 } 005098 } 005099 } 005100 005101 break; 005102 } 005103 005104 005105 /* Opcode: SeekHit P1 P2 P3 * * 005106 ** Synopsis: set P2<=seekHit<=P3 005107 ** 005108 ** Increase or decrease the seekHit value for cursor P1, if necessary, 005109 ** so that it is no less than P2 and no greater than P3. 005110 ** 005111 ** The seekHit integer represents the maximum of terms in an index for which 005112 ** there is known to be at least one match. If the seekHit value is smaller 005113 ** than the total number of equality terms in an index lookup, then the 005114 ** OP_IfNoHope opcode might run to see if the IN loop can be abandoned 005115 ** early, thus saving work. This is part of the IN-early-out optimization. 005116 ** 005117 ** P1 must be a valid b-tree cursor. 005118 */ 005119 case OP_SeekHit: { /* ncycle */ 005120 VdbeCursor *pC; 005121 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005122 pC = p->apCsr[pOp->p1]; 005123 assert( pC!=0 ); 005124 assert( pOp->p3>=pOp->p2 ); 005125 if( pC->seekHit<pOp->p2 ){ 005126 #ifdef SQLITE_DEBUG 005127 if( db->flags&SQLITE_VdbeTrace ){ 005128 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2); 005129 } 005130 #endif 005131 pC->seekHit = pOp->p2; 005132 }else if( pC->seekHit>pOp->p3 ){ 005133 #ifdef SQLITE_DEBUG 005134 if( db->flags&SQLITE_VdbeTrace ){ 005135 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3); 005136 } 005137 #endif 005138 pC->seekHit = pOp->p3; 005139 } 005140 break; 005141 } 005142 005143 /* Opcode: IfNotOpen P1 P2 * * * 005144 ** Synopsis: if( !csr[P1] ) goto P2 005145 ** 005146 ** If cursor P1 is not open or if P1 is set to a NULL row using the 005147 ** OP_NullRow opcode, then jump to instruction P2. Otherwise, fall through. 005148 */ 005149 case OP_IfNotOpen: { /* jump */ 005150 VdbeCursor *pCur; 005151 005152 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005153 pCur = p->apCsr[pOp->p1]; 005154 VdbeBranchTaken(pCur==0 || pCur->nullRow, 2); 005155 if( pCur==0 || pCur->nullRow ){ 005156 goto jump_to_p2_and_check_for_interrupt; 005157 } 005158 break; 005159 } 005160 005161 /* Opcode: Found P1 P2 P3 P4 * 005162 ** Synopsis: key=r[P3@P4] 005163 ** 005164 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If 005165 ** P4>0 then register P3 is the first of P4 registers that form an unpacked 005166 ** record. 005167 ** 005168 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 005169 ** is a prefix of any entry in P1 then a jump is made to P2 and 005170 ** P1 is left pointing at the matching entry. 005171 ** 005172 ** This operation leaves the cursor in a state where it can be 005173 ** advanced in the forward direction. The Next instruction will work, 005174 ** but not the Prev instruction. 005175 ** 005176 ** See also: NotFound, NoConflict, NotExists. SeekGe 005177 */ 005178 /* Opcode: NotFound P1 P2 P3 P4 * 005179 ** Synopsis: key=r[P3@P4] 005180 ** 005181 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If 005182 ** P4>0 then register P3 is the first of P4 registers that form an unpacked 005183 ** record. 005184 ** 005185 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 005186 ** is not the prefix of any entry in P1 then a jump is made to P2. If P1 005187 ** does contain an entry whose prefix matches the P3/P4 record then control 005188 ** falls through to the next instruction and P1 is left pointing at the 005189 ** matching entry. 005190 ** 005191 ** This operation leaves the cursor in a state where it cannot be 005192 ** advanced in either direction. In other words, the Next and Prev 005193 ** opcodes do not work after this operation. 005194 ** 005195 ** See also: Found, NotExists, NoConflict, IfNoHope 005196 */ 005197 /* Opcode: IfNoHope P1 P2 P3 P4 * 005198 ** Synopsis: key=r[P3@P4] 005199 ** 005200 ** Register P3 is the first of P4 registers that form an unpacked 005201 ** record. Cursor P1 is an index btree. P2 is a jump destination. 005202 ** In other words, the operands to this opcode are the same as the 005203 ** operands to OP_NotFound and OP_IdxGT. 005204 ** 005205 ** This opcode is an optimization attempt only. If this opcode always 005206 ** falls through, the correct answer is still obtained, but extra work 005207 ** is performed. 005208 ** 005209 ** A value of N in the seekHit flag of cursor P1 means that there exists 005210 ** a key P3:N that will match some record in the index. We want to know 005211 ** if it is possible for a record P3:P4 to match some record in the 005212 ** index. If it is not possible, we can skip some work. So if seekHit 005213 ** is less than P4, attempt to find out if a match is possible by running 005214 ** OP_NotFound. 005215 ** 005216 ** This opcode is used in IN clause processing for a multi-column key. 005217 ** If an IN clause is attached to an element of the key other than the 005218 ** left-most element, and if there are no matches on the most recent 005219 ** seek over the whole key, then it might be that one of the key element 005220 ** to the left is prohibiting a match, and hence there is "no hope" of 005221 ** any match regardless of how many IN clause elements are checked. 005222 ** In such a case, we abandon the IN clause search early, using this 005223 ** opcode. The opcode name comes from the fact that the 005224 ** jump is taken if there is "no hope" of achieving a match. 005225 ** 005226 ** See also: NotFound, SeekHit 005227 */ 005228 /* Opcode: NoConflict P1 P2 P3 P4 * 005229 ** Synopsis: key=r[P3@P4] 005230 ** 005231 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If 005232 ** P4>0 then register P3 is the first of P4 registers that form an unpacked 005233 ** record. 005234 ** 005235 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 005236 ** contains any NULL value, jump immediately to P2. If all terms of the 005237 ** record are not-NULL then a check is done to determine if any row in the 005238 ** P1 index btree has a matching key prefix. If there are no matches, jump 005239 ** immediately to P2. If there is a match, fall through and leave the P1 005240 ** cursor pointing to the matching row. 005241 ** 005242 ** This opcode is similar to OP_NotFound with the exceptions that the 005243 ** branch is always taken if any part of the search key input is NULL. 005244 ** 005245 ** This operation leaves the cursor in a state where it cannot be 005246 ** advanced in either direction. In other words, the Next and Prev 005247 ** opcodes do not work after this operation. 005248 ** 005249 ** See also: NotFound, Found, NotExists 005250 */ 005251 case OP_IfNoHope: { /* jump, in3, ncycle */ 005252 VdbeCursor *pC; 005253 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005254 pC = p->apCsr[pOp->p1]; 005255 assert( pC!=0 ); 005256 #ifdef SQLITE_DEBUG 005257 if( db->flags&SQLITE_VdbeTrace ){ 005258 printf("seekHit is %d\n", pC->seekHit); 005259 } 005260 #endif 005261 if( pC->seekHit>=pOp->p4.i ) break; 005262 /* Fall through into OP_NotFound */ 005263 /* no break */ deliberate_fall_through 005264 } 005265 case OP_NoConflict: /* jump, in3, ncycle */ 005266 case OP_NotFound: /* jump, in3, ncycle */ 005267 case OP_Found: { /* jump, in3, ncycle */ 005268 int alreadyExists; 005269 int ii; 005270 VdbeCursor *pC; 005271 UnpackedRecord *pIdxKey; 005272 UnpackedRecord r; 005273 005274 #ifdef SQLITE_TEST 005275 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; 005276 #endif 005277 005278 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005279 assert( pOp->p4type==P4_INT32 ); 005280 pC = p->apCsr[pOp->p1]; 005281 assert( pC!=0 ); 005282 #ifdef SQLITE_DEBUG 005283 pC->seekOp = pOp->opcode; 005284 #endif 005285 r.aMem = &aMem[pOp->p3]; 005286 assert( pC->eCurType==CURTYPE_BTREE ); 005287 assert( pC->uc.pCursor!=0 ); 005288 assert( pC->isTable==0 ); 005289 r.nField = (u16)pOp->p4.i; 005290 if( r.nField>0 ){ 005291 /* Key values in an array of registers */ 005292 r.pKeyInfo = pC->pKeyInfo; 005293 r.default_rc = 0; 005294 #ifdef SQLITE_DEBUG 005295 for(ii=0; ii<r.nField; ii++){ 005296 assert( memIsValid(&r.aMem[ii]) ); 005297 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 ); 005298 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); 005299 } 005300 #endif 005301 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult); 005302 }else{ 005303 /* Composite key generated by OP_MakeRecord */ 005304 assert( r.aMem->flags & MEM_Blob ); 005305 assert( pOp->opcode!=OP_NoConflict ); 005306 rc = ExpandBlob(r.aMem); 005307 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); 005308 if( rc ) goto no_mem; 005309 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo); 005310 if( pIdxKey==0 ) goto no_mem; 005311 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey); 005312 pIdxKey->default_rc = 0; 005313 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult); 005314 sqlite3DbFreeNN(db, pIdxKey); 005315 } 005316 if( rc!=SQLITE_OK ){ 005317 goto abort_due_to_error; 005318 } 005319 alreadyExists = (pC->seekResult==0); 005320 pC->nullRow = 1-alreadyExists; 005321 pC->deferredMoveto = 0; 005322 pC->cacheStatus = CACHE_STALE; 005323 if( pOp->opcode==OP_Found ){ 005324 VdbeBranchTaken(alreadyExists!=0,2); 005325 if( alreadyExists ) goto jump_to_p2; 005326 }else{ 005327 if( !alreadyExists ){ 005328 VdbeBranchTaken(1,2); 005329 goto jump_to_p2; 005330 } 005331 if( pOp->opcode==OP_NoConflict ){ 005332 /* For the OP_NoConflict opcode, take the jump if any of the 005333 ** input fields are NULL, since any key with a NULL will not 005334 ** conflict */ 005335 for(ii=0; ii<r.nField; ii++){ 005336 if( r.aMem[ii].flags & MEM_Null ){ 005337 VdbeBranchTaken(1,2); 005338 goto jump_to_p2; 005339 } 005340 } 005341 } 005342 VdbeBranchTaken(0,2); 005343 if( pOp->opcode==OP_IfNoHope ){ 005344 pC->seekHit = pOp->p4.i; 005345 } 005346 } 005347 break; 005348 } 005349 005350 /* Opcode: SeekRowid P1 P2 P3 * * 005351 ** Synopsis: intkey=r[P3] 005352 ** 005353 ** P1 is the index of a cursor open on an SQL table btree (with integer 005354 ** keys). If register P3 does not contain an integer or if P1 does not 005355 ** contain a record with rowid P3 then jump immediately to P2. 005356 ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain 005357 ** a record with rowid P3 then 005358 ** leave the cursor pointing at that record and fall through to the next 005359 ** instruction. 005360 ** 005361 ** The OP_NotExists opcode performs the same operation, but with OP_NotExists 005362 ** the P3 register must be guaranteed to contain an integer value. With this 005363 ** opcode, register P3 might not contain an integer. 005364 ** 005365 ** The OP_NotFound opcode performs the same operation on index btrees 005366 ** (with arbitrary multi-value keys). 005367 ** 005368 ** This opcode leaves the cursor in a state where it cannot be advanced 005369 ** in either direction. In other words, the Next and Prev opcodes will 005370 ** not work following this opcode. 005371 ** 005372 ** See also: Found, NotFound, NoConflict, SeekRowid 005373 */ 005374 /* Opcode: NotExists P1 P2 P3 * * 005375 ** Synopsis: intkey=r[P3] 005376 ** 005377 ** P1 is the index of a cursor open on an SQL table btree (with integer 005378 ** keys). P3 is an integer rowid. If P1 does not contain a record with 005379 ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an 005380 ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then 005381 ** leave the cursor pointing at that record and fall through to the next 005382 ** instruction. 005383 ** 005384 ** The OP_SeekRowid opcode performs the same operation but also allows the 005385 ** P3 register to contain a non-integer value, in which case the jump is 005386 ** always taken. This opcode requires that P3 always contain an integer. 005387 ** 005388 ** The OP_NotFound opcode performs the same operation on index btrees 005389 ** (with arbitrary multi-value keys). 005390 ** 005391 ** This opcode leaves the cursor in a state where it cannot be advanced 005392 ** in either direction. In other words, the Next and Prev opcodes will 005393 ** not work following this opcode. 005394 ** 005395 ** See also: Found, NotFound, NoConflict, SeekRowid 005396 */ 005397 case OP_SeekRowid: { /* jump, in3, ncycle */ 005398 VdbeCursor *pC; 005399 BtCursor *pCrsr; 005400 int res; 005401 u64 iKey; 005402 005403 pIn3 = &aMem[pOp->p3]; 005404 testcase( pIn3->flags & MEM_Int ); 005405 testcase( pIn3->flags & MEM_IntReal ); 005406 testcase( pIn3->flags & MEM_Real ); 005407 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str ); 005408 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){ 005409 /* If pIn3->u.i does not contain an integer, compute iKey as the 005410 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted 005411 ** into an integer without loss of information. Take care to avoid 005412 ** changing the datatype of pIn3, however, as it is used by other 005413 ** parts of the prepared statement. */ 005414 Mem x = pIn3[0]; 005415 applyAffinity(&x, SQLITE_AFF_NUMERIC, encoding); 005416 if( (x.flags & MEM_Int)==0 ) goto jump_to_p2; 005417 iKey = x.u.i; 005418 goto notExistsWithKey; 005419 } 005420 /* Fall through into OP_NotExists */ 005421 /* no break */ deliberate_fall_through 005422 case OP_NotExists: /* jump, in3, ncycle */ 005423 pIn3 = &aMem[pOp->p3]; 005424 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid ); 005425 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005426 iKey = pIn3->u.i; 005427 notExistsWithKey: 005428 pC = p->apCsr[pOp->p1]; 005429 assert( pC!=0 ); 005430 #ifdef SQLITE_DEBUG 005431 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid; 005432 #endif 005433 assert( pC->isTable ); 005434 assert( pC->eCurType==CURTYPE_BTREE ); 005435 pCrsr = pC->uc.pCursor; 005436 assert( pCrsr!=0 ); 005437 res = 0; 005438 rc = sqlite3BtreeTableMoveto(pCrsr, iKey, 0, &res); 005439 assert( rc==SQLITE_OK || res==0 ); 005440 pC->movetoTarget = iKey; /* Used by OP_Delete */ 005441 pC->nullRow = 0; 005442 pC->cacheStatus = CACHE_STALE; 005443 pC->deferredMoveto = 0; 005444 VdbeBranchTaken(res!=0,2); 005445 pC->seekResult = res; 005446 if( res!=0 ){ 005447 assert( rc==SQLITE_OK ); 005448 if( pOp->p2==0 ){ 005449 rc = SQLITE_CORRUPT_BKPT; 005450 }else{ 005451 goto jump_to_p2; 005452 } 005453 } 005454 if( rc ) goto abort_due_to_error; 005455 break; 005456 } 005457 005458 /* Opcode: Sequence P1 P2 * * * 005459 ** Synopsis: r[P2]=cursor[P1].ctr++ 005460 ** 005461 ** Find the next available sequence number for cursor P1. 005462 ** Write the sequence number into register P2. 005463 ** The sequence number on the cursor is incremented after this 005464 ** instruction. 005465 */ 005466 case OP_Sequence: { /* out2 */ 005467 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005468 assert( p->apCsr[pOp->p1]!=0 ); 005469 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB ); 005470 pOut = out2Prerelease(p, pOp); 005471 pOut->u.i = p->apCsr[pOp->p1]->seqCount++; 005472 break; 005473 } 005474 005475 005476 /* Opcode: NewRowid P1 P2 P3 * * 005477 ** Synopsis: r[P2]=rowid 005478 ** 005479 ** Get a new integer record number (a.k.a "rowid") used as the key to a table. 005480 ** The record number is not previously used as a key in the database 005481 ** table that cursor P1 points to. The new record number is written 005482 ** written to register P2. 005483 ** 005484 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds 005485 ** the largest previously generated record number. No new record numbers are 005486 ** allowed to be less than this value. When this value reaches its maximum, 005487 ** an SQLITE_FULL error is generated. The P3 register is updated with the ' 005488 ** generated record number. This P3 mechanism is used to help implement the 005489 ** AUTOINCREMENT feature. 005490 */ 005491 case OP_NewRowid: { /* out2 */ 005492 i64 v; /* The new rowid */ 005493 VdbeCursor *pC; /* Cursor of table to get the new rowid */ 005494 int res; /* Result of an sqlite3BtreeLast() */ 005495 int cnt; /* Counter to limit the number of searches */ 005496 #ifndef SQLITE_OMIT_AUTOINCREMENT 005497 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ 005498 VdbeFrame *pFrame; /* Root frame of VDBE */ 005499 #endif 005500 005501 v = 0; 005502 res = 0; 005503 pOut = out2Prerelease(p, pOp); 005504 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005505 pC = p->apCsr[pOp->p1]; 005506 assert( pC!=0 ); 005507 assert( pC->isTable ); 005508 assert( pC->eCurType==CURTYPE_BTREE ); 005509 assert( pC->uc.pCursor!=0 ); 005510 { 005511 /* The next rowid or record number (different terms for the same 005512 ** thing) is obtained in a two-step algorithm. 005513 ** 005514 ** First we attempt to find the largest existing rowid and add one 005515 ** to that. But if the largest existing rowid is already the maximum 005516 ** positive integer, we have to fall through to the second 005517 ** probabilistic algorithm 005518 ** 005519 ** The second algorithm is to select a rowid at random and see if 005520 ** it already exists in the table. If it does not exist, we have 005521 ** succeeded. If the random rowid does exist, we select a new one 005522 ** and try again, up to 100 times. 005523 */ 005524 assert( pC->isTable ); 005525 005526 #ifdef SQLITE_32BIT_ROWID 005527 # define MAX_ROWID 0x7fffffff 005528 #else 005529 /* Some compilers complain about constants of the form 0x7fffffffffffffff. 005530 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems 005531 ** to provide the constant while making all compilers happy. 005532 */ 005533 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) 005534 #endif 005535 005536 if( !pC->useRandomRowid ){ 005537 rc = sqlite3BtreeLast(pC->uc.pCursor, &res); 005538 if( rc!=SQLITE_OK ){ 005539 goto abort_due_to_error; 005540 } 005541 if( res ){ 005542 v = 1; /* IMP: R-61914-48074 */ 005543 }else{ 005544 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) ); 005545 v = sqlite3BtreeIntegerKey(pC->uc.pCursor); 005546 if( v>=MAX_ROWID ){ 005547 pC->useRandomRowid = 1; 005548 }else{ 005549 v++; /* IMP: R-29538-34987 */ 005550 } 005551 } 005552 } 005553 005554 #ifndef SQLITE_OMIT_AUTOINCREMENT 005555 if( pOp->p3 ){ 005556 /* Assert that P3 is a valid memory cell. */ 005557 assert( pOp->p3>0 ); 005558 if( p->pFrame ){ 005559 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); 005560 /* Assert that P3 is a valid memory cell. */ 005561 assert( pOp->p3<=pFrame->nMem ); 005562 pMem = &pFrame->aMem[pOp->p3]; 005563 }else{ 005564 /* Assert that P3 is a valid memory cell. */ 005565 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 005566 pMem = &aMem[pOp->p3]; 005567 memAboutToChange(p, pMem); 005568 } 005569 assert( memIsValid(pMem) ); 005570 005571 REGISTER_TRACE(pOp->p3, pMem); 005572 sqlite3VdbeMemIntegerify(pMem); 005573 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ 005574 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ 005575 rc = SQLITE_FULL; /* IMP: R-17817-00630 */ 005576 goto abort_due_to_error; 005577 } 005578 if( v<pMem->u.i+1 ){ 005579 v = pMem->u.i + 1; 005580 } 005581 pMem->u.i = v; 005582 } 005583 #endif 005584 if( pC->useRandomRowid ){ 005585 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the 005586 ** largest possible integer (9223372036854775807) then the database 005587 ** engine starts picking positive candidate ROWIDs at random until 005588 ** it finds one that is not previously used. */ 005589 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is 005590 ** an AUTOINCREMENT table. */ 005591 cnt = 0; 005592 do{ 005593 sqlite3_randomness(sizeof(v), &v); 005594 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ 005595 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v, 005596 0, &res))==SQLITE_OK) 005597 && (res==0) 005598 && (++cnt<100)); 005599 if( rc ) goto abort_due_to_error; 005600 if( res==0 ){ 005601 rc = SQLITE_FULL; /* IMP: R-38219-53002 */ 005602 goto abort_due_to_error; 005603 } 005604 assert( v>0 ); /* EV: R-40812-03570 */ 005605 } 005606 pC->deferredMoveto = 0; 005607 pC->cacheStatus = CACHE_STALE; 005608 } 005609 pOut->u.i = v; 005610 break; 005611 } 005612 005613 /* Opcode: Insert P1 P2 P3 P4 P5 005614 ** Synopsis: intkey=r[P3] data=r[P2] 005615 ** 005616 ** Write an entry into the table of cursor P1. A new entry is 005617 ** created if it doesn't already exist or the data for an existing 005618 ** entry is overwritten. The data is the value MEM_Blob stored in register 005619 ** number P2. The key is stored in register P3. The key must 005620 ** be a MEM_Int. 005621 ** 005622 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is 005623 ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, 005624 ** then rowid is stored for subsequent return by the 005625 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). 005626 ** 005627 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might 005628 ** run faster by avoiding an unnecessary seek on cursor P1. However, 005629 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior 005630 ** seeks on the cursor or if the most recent seek used a key equal to P3. 005631 ** 005632 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an 005633 ** UPDATE operation. Otherwise (if the flag is clear) then this opcode 005634 ** is part of an INSERT operation. The difference is only important to 005635 ** the update hook. 005636 ** 005637 ** Parameter P4 may point to a Table structure, or may be NULL. If it is 005638 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked 005639 ** following a successful insert. 005640 ** 005641 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically 005642 ** allocated, then ownership of P2 is transferred to the pseudo-cursor 005643 ** and register P2 becomes ephemeral. If the cursor is changed, the 005644 ** value of register P2 will then change. Make sure this does not 005645 ** cause any problems.) 005646 ** 005647 ** This instruction only works on tables. The equivalent instruction 005648 ** for indices is OP_IdxInsert. 005649 */ 005650 case OP_Insert: { 005651 Mem *pData; /* MEM cell holding data for the record to be inserted */ 005652 Mem *pKey; /* MEM cell holding key for the record */ 005653 VdbeCursor *pC; /* Cursor to table into which insert is written */ 005654 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ 005655 const char *zDb; /* database name - used by the update hook */ 005656 Table *pTab; /* Table structure - used by update and pre-update hooks */ 005657 BtreePayload x; /* Payload to be inserted */ 005658 005659 pData = &aMem[pOp->p2]; 005660 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005661 assert( memIsValid(pData) ); 005662 pC = p->apCsr[pOp->p1]; 005663 assert( pC!=0 ); 005664 assert( pC->eCurType==CURTYPE_BTREE ); 005665 assert( pC->deferredMoveto==0 ); 005666 assert( pC->uc.pCursor!=0 ); 005667 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable ); 005668 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); 005669 REGISTER_TRACE(pOp->p2, pData); 005670 sqlite3VdbeIncrWriteCounter(p, pC); 005671 005672 pKey = &aMem[pOp->p3]; 005673 assert( pKey->flags & MEM_Int ); 005674 assert( memIsValid(pKey) ); 005675 REGISTER_TRACE(pOp->p3, pKey); 005676 x.nKey = pKey->u.i; 005677 005678 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ 005679 assert( pC->iDb>=0 ); 005680 zDb = db->aDb[pC->iDb].zDbSName; 005681 pTab = pOp->p4.pTab; 005682 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) ); 005683 }else{ 005684 pTab = 0; 005685 zDb = 0; 005686 } 005687 005688 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 005689 /* Invoke the pre-update hook, if any */ 005690 if( pTab ){ 005691 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){ 005692 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1); 005693 } 005694 if( db->xUpdateCallback==0 || pTab->aCol==0 ){ 005695 /* Prevent post-update hook from running in cases when it should not */ 005696 pTab = 0; 005697 } 005698 } 005699 if( pOp->p5 & OPFLAG_ISNOOP ) break; 005700 #endif 005701 005702 assert( (pOp->p5 & OPFLAG_LASTROWID)==0 || (pOp->p5 & OPFLAG_NCHANGE)!=0 ); 005703 if( pOp->p5 & OPFLAG_NCHANGE ){ 005704 p->nChange++; 005705 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey; 005706 } 005707 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 ); 005708 x.pData = pData->z; 005709 x.nData = pData->n; 005710 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); 005711 if( pData->flags & MEM_Zero ){ 005712 x.nZero = pData->u.nZero; 005713 }else{ 005714 x.nZero = 0; 005715 } 005716 x.pKey = 0; 005717 assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT ); 005718 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, 005719 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)), 005720 seekResult 005721 ); 005722 pC->deferredMoveto = 0; 005723 pC->cacheStatus = CACHE_STALE; 005724 colCacheCtr++; 005725 005726 /* Invoke the update-hook if required. */ 005727 if( rc ) goto abort_due_to_error; 005728 if( pTab ){ 005729 assert( db->xUpdateCallback!=0 ); 005730 assert( pTab->aCol!=0 ); 005731 db->xUpdateCallback(db->pUpdateArg, 005732 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT, 005733 zDb, pTab->zName, x.nKey); 005734 } 005735 break; 005736 } 005737 005738 /* Opcode: RowCell P1 P2 P3 * * 005739 ** 005740 ** P1 and P2 are both open cursors. Both must be opened on the same type 005741 ** of table - intkey or index. This opcode is used as part of copying 005742 ** the current row from P2 into P1. If the cursors are opened on intkey 005743 ** tables, register P3 contains the rowid to use with the new record in 005744 ** P1. If they are opened on index tables, P3 is not used. 005745 ** 005746 ** This opcode must be followed by either an Insert or InsertIdx opcode 005747 ** with the OPFLAG_PREFORMAT flag set to complete the insert operation. 005748 */ 005749 case OP_RowCell: { 005750 VdbeCursor *pDest; /* Cursor to write to */ 005751 VdbeCursor *pSrc; /* Cursor to read from */ 005752 i64 iKey; /* Rowid value to insert with */ 005753 assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert ); 005754 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 ); 005755 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 ); 005756 assert( pOp[1].p5 & OPFLAG_PREFORMAT ); 005757 pDest = p->apCsr[pOp->p1]; 005758 pSrc = p->apCsr[pOp->p2]; 005759 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0; 005760 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey); 005761 if( rc!=SQLITE_OK ) goto abort_due_to_error; 005762 break; 005763 }; 005764 005765 /* Opcode: Delete P1 P2 P3 P4 P5 005766 ** 005767 ** Delete the record at which the P1 cursor is currently pointing. 005768 ** 005769 ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then 005770 ** the cursor will be left pointing at either the next or the previous 005771 ** record in the table. If it is left pointing at the next record, then 005772 ** the next Next instruction will be a no-op. As a result, in this case 005773 ** it is ok to delete a record from within a Next loop. If 005774 ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be 005775 ** left in an undefined state. 005776 ** 005777 ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this 005778 ** delete is one of several associated with deleting a table row and 005779 ** all its associated index entries. Exactly one of those deletes is 005780 ** the "primary" delete. The others are all on OPFLAG_FORDELETE 005781 ** cursors or else are marked with the AUXDELETE flag. 005782 ** 005783 ** If the OPFLAG_NCHANGE (0x01) flag of P2 (NB: P2 not P5) is set, then 005784 ** the row change count is incremented (otherwise not). 005785 ** 005786 ** If the OPFLAG_ISNOOP (0x40) flag of P2 (not P5!) is set, then the 005787 ** pre-update-hook for deletes is run, but the btree is otherwise unchanged. 005788 ** This happens when the OP_Delete is to be shortly followed by an OP_Insert 005789 ** with the same key, causing the btree entry to be overwritten. 005790 ** 005791 ** P1 must not be pseudo-table. It has to be a real table with 005792 ** multiple rows. 005793 ** 005794 ** If P4 is not NULL then it points to a Table object. In this case either 005795 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must 005796 ** have been positioned using OP_NotFound prior to invoking this opcode in 005797 ** this case. Specifically, if one is configured, the pre-update hook is 005798 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured, 005799 ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2. 005800 ** 005801 ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address 005802 ** of the memory cell that contains the value that the rowid of the row will 005803 ** be set to by the update. 005804 */ 005805 case OP_Delete: { 005806 VdbeCursor *pC; 005807 const char *zDb; 005808 Table *pTab; 005809 int opflags; 005810 005811 opflags = pOp->p2; 005812 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005813 pC = p->apCsr[pOp->p1]; 005814 assert( pC!=0 ); 005815 assert( pC->eCurType==CURTYPE_BTREE ); 005816 assert( pC->uc.pCursor!=0 ); 005817 assert( pC->deferredMoveto==0 ); 005818 sqlite3VdbeIncrWriteCounter(p, pC); 005819 005820 #ifdef SQLITE_DEBUG 005821 if( pOp->p4type==P4_TABLE 005822 && HasRowid(pOp->p4.pTab) 005823 && pOp->p5==0 005824 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) 005825 ){ 005826 /* If p5 is zero, the seek operation that positioned the cursor prior to 005827 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of 005828 ** the row that is being deleted */ 005829 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor); 005830 assert( CORRUPT_DB || pC->movetoTarget==iKey ); 005831 } 005832 #endif 005833 005834 /* If the update-hook or pre-update-hook will be invoked, set zDb to 005835 ** the name of the db to pass as to it. Also set local pTab to a copy 005836 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was 005837 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set 005838 ** VdbeCursor.movetoTarget to the current rowid. */ 005839 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ 005840 assert( pC->iDb>=0 ); 005841 assert( pOp->p4.pTab!=0 ); 005842 zDb = db->aDb[pC->iDb].zDbSName; 005843 pTab = pOp->p4.pTab; 005844 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){ 005845 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor); 005846 } 005847 }else{ 005848 zDb = 0; 005849 pTab = 0; 005850 } 005851 005852 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 005853 /* Invoke the pre-update-hook if required. */ 005854 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab ); 005855 if( db->xPreUpdateCallback && pTab ){ 005856 assert( !(opflags & OPFLAG_ISUPDATE) 005857 || HasRowid(pTab)==0 005858 || (aMem[pOp->p3].flags & MEM_Int) 005859 ); 005860 sqlite3VdbePreUpdateHook(p, pC, 005861 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, 005862 zDb, pTab, pC->movetoTarget, 005863 pOp->p3, -1 005864 ); 005865 } 005866 if( opflags & OPFLAG_ISNOOP ) break; 005867 #endif 005868 005869 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ 005870 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 ); 005871 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION ); 005872 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE ); 005873 005874 #ifdef SQLITE_DEBUG 005875 if( p->pFrame==0 ){ 005876 if( pC->isEphemeral==0 005877 && (pOp->p5 & OPFLAG_AUXDELETE)==0 005878 && (pC->wrFlag & OPFLAG_FORDELETE)==0 005879 ){ 005880 nExtraDelete++; 005881 } 005882 if( pOp->p2 & OPFLAG_NCHANGE ){ 005883 nExtraDelete--; 005884 } 005885 } 005886 #endif 005887 005888 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); 005889 pC->cacheStatus = CACHE_STALE; 005890 colCacheCtr++; 005891 pC->seekResult = 0; 005892 if( rc ) goto abort_due_to_error; 005893 005894 /* Invoke the update-hook if required. */ 005895 if( opflags & OPFLAG_NCHANGE ){ 005896 p->nChange++; 005897 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){ 005898 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName, 005899 pC->movetoTarget); 005900 assert( pC->iDb>=0 ); 005901 } 005902 } 005903 005904 break; 005905 } 005906 /* Opcode: ResetCount * * * * * 005907 ** 005908 ** The value of the change counter is copied to the database handle 005909 ** change counter (returned by subsequent calls to sqlite3_changes()). 005910 ** Then the VMs internal change counter resets to 0. 005911 ** This is used by trigger programs. 005912 */ 005913 case OP_ResetCount: { 005914 sqlite3VdbeSetChanges(db, p->nChange); 005915 p->nChange = 0; 005916 break; 005917 } 005918 005919 /* Opcode: SorterCompare P1 P2 P3 P4 005920 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 005921 ** 005922 ** P1 is a sorter cursor. This instruction compares a prefix of the 005923 ** record blob in register P3 against a prefix of the entry that 005924 ** the sorter cursor currently points to. Only the first P4 fields 005925 ** of r[P3] and the sorter record are compared. 005926 ** 005927 ** If either P3 or the sorter contains a NULL in one of their significant 005928 ** fields (not counting the P4 fields at the end which are ignored) then 005929 ** the comparison is assumed to be equal. 005930 ** 005931 ** Fall through to next instruction if the two records compare equal to 005932 ** each other. Jump to P2 if they are different. 005933 */ 005934 case OP_SorterCompare: { 005935 VdbeCursor *pC; 005936 int res; 005937 int nKeyCol; 005938 005939 pC = p->apCsr[pOp->p1]; 005940 assert( isSorter(pC) ); 005941 assert( pOp->p4type==P4_INT32 ); 005942 pIn3 = &aMem[pOp->p3]; 005943 nKeyCol = pOp->p4.i; 005944 res = 0; 005945 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); 005946 VdbeBranchTaken(res!=0,2); 005947 if( rc ) goto abort_due_to_error; 005948 if( res ) goto jump_to_p2; 005949 break; 005950 }; 005951 005952 /* Opcode: SorterData P1 P2 P3 * * 005953 ** Synopsis: r[P2]=data 005954 ** 005955 ** Write into register P2 the current sorter data for sorter cursor P1. 005956 ** Then clear the column header cache on cursor P3. 005957 ** 005958 ** This opcode is normally used to move a record out of the sorter and into 005959 ** a register that is the source for a pseudo-table cursor created using 005960 ** OpenPseudo. That pseudo-table cursor is the one that is identified by 005961 ** parameter P3. Clearing the P3 column cache as part of this opcode saves 005962 ** us from having to issue a separate NullRow instruction to clear that cache. 005963 */ 005964 case OP_SorterData: { /* ncycle */ 005965 VdbeCursor *pC; 005966 005967 pOut = &aMem[pOp->p2]; 005968 pC = p->apCsr[pOp->p1]; 005969 assert( isSorter(pC) ); 005970 rc = sqlite3VdbeSorterRowkey(pC, pOut); 005971 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); 005972 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005973 if( rc ) goto abort_due_to_error; 005974 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; 005975 break; 005976 } 005977 005978 /* Opcode: RowData P1 P2 P3 * * 005979 ** Synopsis: r[P2]=data 005980 ** 005981 ** Write into register P2 the complete row content for the row at 005982 ** which cursor P1 is currently pointing. 005983 ** There is no interpretation of the data. 005984 ** It is just copied onto the P2 register exactly as 005985 ** it is found in the database file. 005986 ** 005987 ** If cursor P1 is an index, then the content is the key of the row. 005988 ** If cursor P2 is a table, then the content extracted is the data. 005989 ** 005990 ** If the P1 cursor must be pointing to a valid row (not a NULL row) 005991 ** of a real table, not a pseudo-table. 005992 ** 005993 ** If P3!=0 then this opcode is allowed to make an ephemeral pointer 005994 ** into the database page. That means that the content of the output 005995 ** register will be invalidated as soon as the cursor moves - including 005996 ** moves caused by other cursors that "save" the current cursors 005997 ** position in order that they can write to the same table. If P3==0 005998 ** then a copy of the data is made into memory. P3!=0 is faster, but 005999 ** P3==0 is safer. 006000 ** 006001 ** If P3!=0 then the content of the P2 register is unsuitable for use 006002 ** in OP_Result and any OP_Result will invalidate the P2 register content. 006003 ** The P2 register content is invalidated by opcodes like OP_Function or 006004 ** by any use of another cursor pointing to the same table. 006005 */ 006006 case OP_RowData: { 006007 VdbeCursor *pC; 006008 BtCursor *pCrsr; 006009 u32 n; 006010 006011 pOut = out2Prerelease(p, pOp); 006012 006013 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006014 pC = p->apCsr[pOp->p1]; 006015 assert( pC!=0 ); 006016 assert( pC->eCurType==CURTYPE_BTREE ); 006017 assert( isSorter(pC)==0 ); 006018 assert( pC->nullRow==0 ); 006019 assert( pC->uc.pCursor!=0 ); 006020 pCrsr = pC->uc.pCursor; 006021 006022 /* The OP_RowData opcodes always follow OP_NotExists or 006023 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions 006024 ** that might invalidate the cursor. 006025 ** If this where not the case, on of the following assert()s 006026 ** would fail. Should this ever change (because of changes in the code 006027 ** generator) then the fix would be to insert a call to 006028 ** sqlite3VdbeCursorMoveto(). 006029 */ 006030 assert( pC->deferredMoveto==0 ); 006031 assert( sqlite3BtreeCursorIsValid(pCrsr) ); 006032 006033 n = sqlite3BtreePayloadSize(pCrsr); 006034 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ 006035 goto too_big; 006036 } 006037 testcase( n==0 ); 006038 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut); 006039 if( rc ) goto abort_due_to_error; 006040 if( !pOp->p3 ) Deephemeralize(pOut); 006041 UPDATE_MAX_BLOBSIZE(pOut); 006042 REGISTER_TRACE(pOp->p2, pOut); 006043 break; 006044 } 006045 006046 /* Opcode: Rowid P1 P2 * * * 006047 ** Synopsis: r[P2]=PX rowid of P1 006048 ** 006049 ** Store in register P2 an integer which is the key of the table entry that 006050 ** P1 is currently point to. 006051 ** 006052 ** P1 can be either an ordinary table or a virtual table. There used to 006053 ** be a separate OP_VRowid opcode for use with virtual tables, but this 006054 ** one opcode now works for both table types. 006055 */ 006056 case OP_Rowid: { /* out2, ncycle */ 006057 VdbeCursor *pC; 006058 i64 v; 006059 sqlite3_vtab *pVtab; 006060 const sqlite3_module *pModule; 006061 006062 pOut = out2Prerelease(p, pOp); 006063 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006064 pC = p->apCsr[pOp->p1]; 006065 assert( pC!=0 ); 006066 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); 006067 if( pC->nullRow ){ 006068 pOut->flags = MEM_Null; 006069 break; 006070 }else if( pC->deferredMoveto ){ 006071 v = pC->movetoTarget; 006072 #ifndef SQLITE_OMIT_VIRTUALTABLE 006073 }else if( pC->eCurType==CURTYPE_VTAB ){ 006074 assert( pC->uc.pVCur!=0 ); 006075 pVtab = pC->uc.pVCur->pVtab; 006076 pModule = pVtab->pModule; 006077 assert( pModule->xRowid ); 006078 rc = pModule->xRowid(pC->uc.pVCur, &v); 006079 sqlite3VtabImportErrmsg(p, pVtab); 006080 if( rc ) goto abort_due_to_error; 006081 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 006082 }else{ 006083 assert( pC->eCurType==CURTYPE_BTREE ); 006084 assert( pC->uc.pCursor!=0 ); 006085 rc = sqlite3VdbeCursorRestore(pC); 006086 if( rc ) goto abort_due_to_error; 006087 if( pC->nullRow ){ 006088 pOut->flags = MEM_Null; 006089 break; 006090 } 006091 v = sqlite3BtreeIntegerKey(pC->uc.pCursor); 006092 } 006093 pOut->u.i = v; 006094 break; 006095 } 006096 006097 /* Opcode: NullRow P1 * * * * 006098 ** 006099 ** Move the cursor P1 to a null row. Any OP_Column operations 006100 ** that occur while the cursor is on the null row will always 006101 ** write a NULL. 006102 ** 006103 ** If cursor P1 is not previously opened, open it now to a special 006104 ** pseudo-cursor that always returns NULL for every column. 006105 */ 006106 case OP_NullRow: { 006107 VdbeCursor *pC; 006108 006109 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006110 pC = p->apCsr[pOp->p1]; 006111 if( pC==0 ){ 006112 /* If the cursor is not already open, create a special kind of 006113 ** pseudo-cursor that always gives null rows. */ 006114 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO); 006115 if( pC==0 ) goto no_mem; 006116 pC->seekResult = 0; 006117 pC->isTable = 1; 006118 pC->noReuse = 1; 006119 pC->uc.pCursor = sqlite3BtreeFakeValidCursor(); 006120 } 006121 pC->nullRow = 1; 006122 pC->cacheStatus = CACHE_STALE; 006123 if( pC->eCurType==CURTYPE_BTREE ){ 006124 assert( pC->uc.pCursor!=0 ); 006125 sqlite3BtreeClearCursor(pC->uc.pCursor); 006126 } 006127 #ifdef SQLITE_DEBUG 006128 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow; 006129 #endif 006130 break; 006131 } 006132 006133 /* Opcode: SeekEnd P1 * * * * 006134 ** 006135 ** Position cursor P1 at the end of the btree for the purpose of 006136 ** appending a new entry onto the btree. 006137 ** 006138 ** It is assumed that the cursor is used only for appending and so 006139 ** if the cursor is valid, then the cursor must already be pointing 006140 ** at the end of the btree and so no changes are made to 006141 ** the cursor. 006142 */ 006143 /* Opcode: Last P1 P2 * * * 006144 ** 006145 ** The next use of the Rowid or Column or Prev instruction for P1 006146 ** will refer to the last entry in the database table or index. 006147 ** If the table or index is empty and P2>0, then jump immediately to P2. 006148 ** If P2 is 0 or if the table or index is not empty, fall through 006149 ** to the following instruction. 006150 ** 006151 ** This opcode leaves the cursor configured to move in reverse order, 006152 ** from the end toward the beginning. In other words, the cursor is 006153 ** configured to use Prev, not Next. 006154 */ 006155 case OP_SeekEnd: /* ncycle */ 006156 case OP_Last: { /* jump, ncycle */ 006157 VdbeCursor *pC; 006158 BtCursor *pCrsr; 006159 int res; 006160 006161 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006162 pC = p->apCsr[pOp->p1]; 006163 assert( pC!=0 ); 006164 assert( pC->eCurType==CURTYPE_BTREE ); 006165 pCrsr = pC->uc.pCursor; 006166 res = 0; 006167 assert( pCrsr!=0 ); 006168 #ifdef SQLITE_DEBUG 006169 pC->seekOp = pOp->opcode; 006170 #endif 006171 if( pOp->opcode==OP_SeekEnd ){ 006172 assert( pOp->p2==0 ); 006173 pC->seekResult = -1; 006174 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){ 006175 break; 006176 } 006177 } 006178 rc = sqlite3BtreeLast(pCrsr, &res); 006179 pC->nullRow = (u8)res; 006180 pC->deferredMoveto = 0; 006181 pC->cacheStatus = CACHE_STALE; 006182 if( rc ) goto abort_due_to_error; 006183 if( pOp->p2>0 ){ 006184 VdbeBranchTaken(res!=0,2); 006185 if( res ) goto jump_to_p2; 006186 } 006187 break; 006188 } 006189 006190 /* Opcode: IfSmaller P1 P2 P3 * * 006191 ** 006192 ** Estimate the number of rows in the table P1. Jump to P2 if that 006193 ** estimate is less than approximately 2**(0.1*P3). 006194 */ 006195 case OP_IfSmaller: { /* jump */ 006196 VdbeCursor *pC; 006197 BtCursor *pCrsr; 006198 int res; 006199 i64 sz; 006200 006201 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006202 pC = p->apCsr[pOp->p1]; 006203 assert( pC!=0 ); 006204 pCrsr = pC->uc.pCursor; 006205 assert( pCrsr ); 006206 rc = sqlite3BtreeFirst(pCrsr, &res); 006207 if( rc ) goto abort_due_to_error; 006208 if( res==0 ){ 006209 sz = sqlite3BtreeRowCountEst(pCrsr); 006210 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1; 006211 } 006212 VdbeBranchTaken(res!=0,2); 006213 if( res ) goto jump_to_p2; 006214 break; 006215 } 006216 006217 006218 /* Opcode: SorterSort P1 P2 * * * 006219 ** 006220 ** After all records have been inserted into the Sorter object 006221 ** identified by P1, invoke this opcode to actually do the sorting. 006222 ** Jump to P2 if there are no records to be sorted. 006223 ** 006224 ** This opcode is an alias for OP_Sort and OP_Rewind that is used 006225 ** for Sorter objects. 006226 */ 006227 /* Opcode: Sort P1 P2 * * * 006228 ** 006229 ** This opcode does exactly the same thing as OP_Rewind except that 006230 ** it increments an undocumented global variable used for testing. 006231 ** 006232 ** Sorting is accomplished by writing records into a sorting index, 006233 ** then rewinding that index and playing it back from beginning to 006234 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the 006235 ** rewinding so that the global variable will be incremented and 006236 ** regression tests can determine whether or not the optimizer is 006237 ** correctly optimizing out sorts. 006238 */ 006239 case OP_SorterSort: /* jump ncycle */ 006240 case OP_Sort: { /* jump ncycle */ 006241 #ifdef SQLITE_TEST 006242 sqlite3_sort_count++; 006243 sqlite3_search_count--; 006244 #endif 006245 p->aCounter[SQLITE_STMTSTATUS_SORT]++; 006246 /* Fall through into OP_Rewind */ 006247 /* no break */ deliberate_fall_through 006248 } 006249 /* Opcode: Rewind P1 P2 * * * 006250 ** 006251 ** The next use of the Rowid or Column or Next instruction for P1 006252 ** will refer to the first entry in the database table or index. 006253 ** If the table or index is empty, jump immediately to P2. 006254 ** If the table or index is not empty, fall through to the following 006255 ** instruction. 006256 ** 006257 ** If P2 is zero, that is an assertion that the P1 table is never 006258 ** empty and hence the jump will never be taken. 006259 ** 006260 ** This opcode leaves the cursor configured to move in forward order, 006261 ** from the beginning toward the end. In other words, the cursor is 006262 ** configured to use Next, not Prev. 006263 */ 006264 case OP_Rewind: { /* jump, ncycle */ 006265 VdbeCursor *pC; 006266 BtCursor *pCrsr; 006267 int res; 006268 006269 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006270 assert( pOp->p5==0 ); 006271 assert( pOp->p2>=0 && pOp->p2<p->nOp ); 006272 006273 pC = p->apCsr[pOp->p1]; 006274 assert( pC!=0 ); 006275 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) ); 006276 res = 1; 006277 #ifdef SQLITE_DEBUG 006278 pC->seekOp = OP_Rewind; 006279 #endif 006280 if( isSorter(pC) ){ 006281 rc = sqlite3VdbeSorterRewind(pC, &res); 006282 }else{ 006283 assert( pC->eCurType==CURTYPE_BTREE ); 006284 pCrsr = pC->uc.pCursor; 006285 assert( pCrsr ); 006286 rc = sqlite3BtreeFirst(pCrsr, &res); 006287 pC->deferredMoveto = 0; 006288 pC->cacheStatus = CACHE_STALE; 006289 } 006290 if( rc ) goto abort_due_to_error; 006291 pC->nullRow = (u8)res; 006292 if( pOp->p2>0 ){ 006293 VdbeBranchTaken(res!=0,2); 006294 if( res ) goto jump_to_p2; 006295 } 006296 break; 006297 } 006298 006299 /* Opcode: Next P1 P2 P3 * P5 006300 ** 006301 ** Advance cursor P1 so that it points to the next key/data pair in its 006302 ** table or index. If there are no more key/value pairs then fall through 006303 ** to the following instruction. But if the cursor advance was successful, 006304 ** jump immediately to P2. 006305 ** 006306 ** The Next opcode is only valid following an SeekGT, SeekGE, or 006307 ** OP_Rewind opcode used to position the cursor. Next is not allowed 006308 ** to follow SeekLT, SeekLE, or OP_Last. 006309 ** 006310 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have 006311 ** been opened prior to this opcode or the program will segfault. 006312 ** 006313 ** The P3 value is a hint to the btree implementation. If P3==1, that 006314 ** means P1 is an SQL index and that this instruction could have been 006315 ** omitted if that index had been unique. P3 is usually 0. P3 is 006316 ** always either 0 or 1. 006317 ** 006318 ** If P5 is positive and the jump is taken, then event counter 006319 ** number P5-1 in the prepared statement is incremented. 006320 ** 006321 ** See also: Prev 006322 */ 006323 /* Opcode: Prev P1 P2 P3 * P5 006324 ** 006325 ** Back up cursor P1 so that it points to the previous key/data pair in its 006326 ** table or index. If there is no previous key/value pairs then fall through 006327 ** to the following instruction. But if the cursor backup was successful, 006328 ** jump immediately to P2. 006329 ** 006330 ** 006331 ** The Prev opcode is only valid following an SeekLT, SeekLE, or 006332 ** OP_Last opcode used to position the cursor. Prev is not allowed 006333 ** to follow SeekGT, SeekGE, or OP_Rewind. 006334 ** 006335 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is 006336 ** not open then the behavior is undefined. 006337 ** 006338 ** The P3 value is a hint to the btree implementation. If P3==1, that 006339 ** means P1 is an SQL index and that this instruction could have been 006340 ** omitted if that index had been unique. P3 is usually 0. P3 is 006341 ** always either 0 or 1. 006342 ** 006343 ** If P5 is positive and the jump is taken, then event counter 006344 ** number P5-1 in the prepared statement is incremented. 006345 */ 006346 /* Opcode: SorterNext P1 P2 * * P5 006347 ** 006348 ** This opcode works just like OP_Next except that P1 must be a 006349 ** sorter object for which the OP_SorterSort opcode has been 006350 ** invoked. This opcode advances the cursor to the next sorted 006351 ** record, or jumps to P2 if there are no more sorted records. 006352 */ 006353 case OP_SorterNext: { /* jump */ 006354 VdbeCursor *pC; 006355 006356 pC = p->apCsr[pOp->p1]; 006357 assert( isSorter(pC) ); 006358 rc = sqlite3VdbeSorterNext(db, pC); 006359 goto next_tail; 006360 006361 case OP_Prev: /* jump, ncycle */ 006362 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006363 assert( pOp->p5==0 006364 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP 006365 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX); 006366 pC = p->apCsr[pOp->p1]; 006367 assert( pC!=0 ); 006368 assert( pC->deferredMoveto==0 ); 006369 assert( pC->eCurType==CURTYPE_BTREE ); 006370 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE 006371 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope 006372 || pC->seekOp==OP_NullRow); 006373 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3); 006374 goto next_tail; 006375 006376 case OP_Next: /* jump, ncycle */ 006377 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006378 assert( pOp->p5==0 006379 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP 006380 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX); 006381 pC = p->apCsr[pOp->p1]; 006382 assert( pC!=0 ); 006383 assert( pC->deferredMoveto==0 ); 006384 assert( pC->eCurType==CURTYPE_BTREE ); 006385 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE 006386 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found 006387 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid 006388 || pC->seekOp==OP_IfNoHope); 006389 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3); 006390 006391 next_tail: 006392 pC->cacheStatus = CACHE_STALE; 006393 VdbeBranchTaken(rc==SQLITE_OK,2); 006394 if( rc==SQLITE_OK ){ 006395 pC->nullRow = 0; 006396 p->aCounter[pOp->p5]++; 006397 #ifdef SQLITE_TEST 006398 sqlite3_search_count++; 006399 #endif 006400 goto jump_to_p2_and_check_for_interrupt; 006401 } 006402 if( rc!=SQLITE_DONE ) goto abort_due_to_error; 006403 rc = SQLITE_OK; 006404 pC->nullRow = 1; 006405 goto check_for_interrupt; 006406 } 006407 006408 /* Opcode: IdxInsert P1 P2 P3 P4 P5 006409 ** Synopsis: key=r[P2] 006410 ** 006411 ** Register P2 holds an SQL index key made using the 006412 ** MakeRecord instructions. This opcode writes that key 006413 ** into the index P1. Data for the entry is nil. 006414 ** 006415 ** If P4 is not zero, then it is the number of values in the unpacked 006416 ** key of reg(P2). In that case, P3 is the index of the first register 006417 ** for the unpacked key. The availability of the unpacked key can sometimes 006418 ** be an optimization. 006419 ** 006420 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer 006421 ** that this insert is likely to be an append. 006422 ** 006423 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is 006424 ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, 006425 ** then the change counter is unchanged. 006426 ** 006427 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might 006428 ** run faster by avoiding an unnecessary seek on cursor P1. However, 006429 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior 006430 ** seeks on the cursor or if the most recent seek used a key equivalent 006431 ** to P2. 006432 ** 006433 ** This instruction only works for indices. The equivalent instruction 006434 ** for tables is OP_Insert. 006435 */ 006436 case OP_IdxInsert: { /* in2 */ 006437 VdbeCursor *pC; 006438 BtreePayload x; 006439 006440 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006441 pC = p->apCsr[pOp->p1]; 006442 sqlite3VdbeIncrWriteCounter(p, pC); 006443 assert( pC!=0 ); 006444 assert( !isSorter(pC) ); 006445 pIn2 = &aMem[pOp->p2]; 006446 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) ); 006447 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; 006448 assert( pC->eCurType==CURTYPE_BTREE ); 006449 assert( pC->isTable==0 ); 006450 rc = ExpandBlob(pIn2); 006451 if( rc ) goto abort_due_to_error; 006452 x.nKey = pIn2->n; 006453 x.pKey = pIn2->z; 006454 x.aMem = aMem + pOp->p3; 006455 x.nMem = (u16)pOp->p4.i; 006456 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, 006457 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)), 006458 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) 006459 ); 006460 assert( pC->deferredMoveto==0 ); 006461 pC->cacheStatus = CACHE_STALE; 006462 if( rc) goto abort_due_to_error; 006463 break; 006464 } 006465 006466 /* Opcode: SorterInsert P1 P2 * * * 006467 ** Synopsis: key=r[P2] 006468 ** 006469 ** Register P2 holds an SQL index key made using the 006470 ** MakeRecord instructions. This opcode writes that key 006471 ** into the sorter P1. Data for the entry is nil. 006472 */ 006473 case OP_SorterInsert: { /* in2 */ 006474 VdbeCursor *pC; 006475 006476 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006477 pC = p->apCsr[pOp->p1]; 006478 sqlite3VdbeIncrWriteCounter(p, pC); 006479 assert( pC!=0 ); 006480 assert( isSorter(pC) ); 006481 pIn2 = &aMem[pOp->p2]; 006482 assert( pIn2->flags & MEM_Blob ); 006483 assert( pC->isTable==0 ); 006484 rc = ExpandBlob(pIn2); 006485 if( rc ) goto abort_due_to_error; 006486 rc = sqlite3VdbeSorterWrite(pC, pIn2); 006487 if( rc) goto abort_due_to_error; 006488 break; 006489 } 006490 006491 /* Opcode: IdxDelete P1 P2 P3 * P5 006492 ** Synopsis: key=r[P2@P3] 006493 ** 006494 ** The content of P3 registers starting at register P2 form 006495 ** an unpacked index key. This opcode removes that entry from the 006496 ** index opened by cursor P1. 006497 ** 006498 ** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error 006499 ** if no matching index entry is found. This happens when running 006500 ** an UPDATE or DELETE statement and the index entry to be updated 006501 ** or deleted is not found. For some uses of IdxDelete 006502 ** (example: the EXCEPT operator) it does not matter that no matching 006503 ** entry is found. For those cases, P5 is zero. Also, do not raise 006504 ** this (self-correcting and non-critical) error if in writable_schema mode. 006505 */ 006506 case OP_IdxDelete: { 006507 VdbeCursor *pC; 006508 BtCursor *pCrsr; 006509 int res; 006510 UnpackedRecord r; 006511 006512 assert( pOp->p3>0 ); 006513 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 ); 006514 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006515 pC = p->apCsr[pOp->p1]; 006516 assert( pC!=0 ); 006517 assert( pC->eCurType==CURTYPE_BTREE ); 006518 sqlite3VdbeIncrWriteCounter(p, pC); 006519 pCrsr = pC->uc.pCursor; 006520 assert( pCrsr!=0 ); 006521 r.pKeyInfo = pC->pKeyInfo; 006522 r.nField = (u16)pOp->p3; 006523 r.default_rc = 0; 006524 r.aMem = &aMem[pOp->p2]; 006525 rc = sqlite3BtreeIndexMoveto(pCrsr, &r, &res); 006526 if( rc ) goto abort_due_to_error; 006527 if( res==0 ){ 006528 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); 006529 if( rc ) goto abort_due_to_error; 006530 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){ 006531 rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX, __LINE__, "index corruption"); 006532 goto abort_due_to_error; 006533 } 006534 assert( pC->deferredMoveto==0 ); 006535 pC->cacheStatus = CACHE_STALE; 006536 pC->seekResult = 0; 006537 break; 006538 } 006539 006540 /* Opcode: DeferredSeek P1 * P3 P4 * 006541 ** Synopsis: Move P3 to P1.rowid if needed 006542 ** 006543 ** P1 is an open index cursor and P3 is a cursor on the corresponding 006544 ** table. This opcode does a deferred seek of the P3 table cursor 006545 ** to the row that corresponds to the current row of P1. 006546 ** 006547 ** This is a deferred seek. Nothing actually happens until 006548 ** the cursor is used to read a record. That way, if no reads 006549 ** occur, no unnecessary I/O happens. 006550 ** 006551 ** P4 may be an array of integers (type P4_INTARRAY) containing 006552 ** one entry for each column in the P3 table. If array entry a(i) 006553 ** is non-zero, then reading column a(i)-1 from cursor P3 is 006554 ** equivalent to performing the deferred seek and then reading column i 006555 ** from P1. This information is stored in P3 and used to redirect 006556 ** reads against P3 over to P1, thus possibly avoiding the need to 006557 ** seek and read cursor P3. 006558 */ 006559 /* Opcode: IdxRowid P1 P2 * * * 006560 ** Synopsis: r[P2]=rowid 006561 ** 006562 ** Write into register P2 an integer which is the last entry in the record at 006563 ** the end of the index key pointed to by cursor P1. This integer should be 006564 ** the rowid of the table entry to which this index entry points. 006565 ** 006566 ** See also: Rowid, MakeRecord. 006567 */ 006568 case OP_DeferredSeek: /* ncycle */ 006569 case OP_IdxRowid: { /* out2, ncycle */ 006570 VdbeCursor *pC; /* The P1 index cursor */ 006571 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */ 006572 i64 rowid; /* Rowid that P1 current points to */ 006573 006574 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006575 pC = p->apCsr[pOp->p1]; 006576 assert( pC!=0 ); 006577 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) ); 006578 assert( pC->uc.pCursor!=0 ); 006579 assert( pC->isTable==0 || IsNullCursor(pC) ); 006580 assert( pC->deferredMoveto==0 ); 006581 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid ); 006582 006583 /* The IdxRowid and Seek opcodes are combined because of the commonality 006584 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */ 006585 rc = sqlite3VdbeCursorRestore(pC); 006586 006587 /* sqlite3VdbeCursorRestore() may fail if the cursor has been disturbed 006588 ** since it was last positioned and an error (e.g. OOM or an IO error) 006589 ** occurs while trying to reposition it. */ 006590 if( rc!=SQLITE_OK ) goto abort_due_to_error; 006591 006592 if( !pC->nullRow ){ 006593 rowid = 0; /* Not needed. Only used to silence a warning. */ 006594 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid); 006595 if( rc!=SQLITE_OK ){ 006596 goto abort_due_to_error; 006597 } 006598 if( pOp->opcode==OP_DeferredSeek ){ 006599 assert( pOp->p3>=0 && pOp->p3<p->nCursor ); 006600 pTabCur = p->apCsr[pOp->p3]; 006601 assert( pTabCur!=0 ); 006602 assert( pTabCur->eCurType==CURTYPE_BTREE ); 006603 assert( pTabCur->uc.pCursor!=0 ); 006604 assert( pTabCur->isTable ); 006605 pTabCur->nullRow = 0; 006606 pTabCur->movetoTarget = rowid; 006607 pTabCur->deferredMoveto = 1; 006608 pTabCur->cacheStatus = CACHE_STALE; 006609 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 ); 006610 assert( !pTabCur->isEphemeral ); 006611 pTabCur->ub.aAltMap = pOp->p4.ai; 006612 assert( !pC->isEphemeral ); 006613 pTabCur->pAltCursor = pC; 006614 }else{ 006615 pOut = out2Prerelease(p, pOp); 006616 pOut->u.i = rowid; 006617 } 006618 }else{ 006619 assert( pOp->opcode==OP_IdxRowid ); 006620 sqlite3VdbeMemSetNull(&aMem[pOp->p2]); 006621 } 006622 break; 006623 } 006624 006625 /* Opcode: FinishSeek P1 * * * * 006626 ** 006627 ** If cursor P1 was previously moved via OP_DeferredSeek, complete that 006628 ** seek operation now, without further delay. If the cursor seek has 006629 ** already occurred, this instruction is a no-op. 006630 */ 006631 case OP_FinishSeek: { /* ncycle */ 006632 VdbeCursor *pC; /* The P1 index cursor */ 006633 006634 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006635 pC = p->apCsr[pOp->p1]; 006636 if( pC->deferredMoveto ){ 006637 rc = sqlite3VdbeFinishMoveto(pC); 006638 if( rc ) goto abort_due_to_error; 006639 } 006640 break; 006641 } 006642 006643 /* Opcode: IdxGE P1 P2 P3 P4 * 006644 ** Synopsis: key=r[P3@P4] 006645 ** 006646 ** The P4 register values beginning with P3 form an unpacked index 006647 ** key that omits the PRIMARY KEY. Compare this key value against the index 006648 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID 006649 ** fields at the end. 006650 ** 006651 ** If the P1 index entry is greater than or equal to the key value 006652 ** then jump to P2. Otherwise fall through to the next instruction. 006653 */ 006654 /* Opcode: IdxGT P1 P2 P3 P4 * 006655 ** Synopsis: key=r[P3@P4] 006656 ** 006657 ** The P4 register values beginning with P3 form an unpacked index 006658 ** key that omits the PRIMARY KEY. Compare this key value against the index 006659 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID 006660 ** fields at the end. 006661 ** 006662 ** If the P1 index entry is greater than the key value 006663 ** then jump to P2. Otherwise fall through to the next instruction. 006664 */ 006665 /* Opcode: IdxLT P1 P2 P3 P4 * 006666 ** Synopsis: key=r[P3@P4] 006667 ** 006668 ** The P4 register values beginning with P3 form an unpacked index 006669 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against 006670 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or 006671 ** ROWID on the P1 index. 006672 ** 006673 ** If the P1 index entry is less than the key value then jump to P2. 006674 ** Otherwise fall through to the next instruction. 006675 */ 006676 /* Opcode: IdxLE P1 P2 P3 P4 * 006677 ** Synopsis: key=r[P3@P4] 006678 ** 006679 ** The P4 register values beginning with P3 form an unpacked index 006680 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against 006681 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or 006682 ** ROWID on the P1 index. 006683 ** 006684 ** If the P1 index entry is less than or equal to the key value then jump 006685 ** to P2. Otherwise fall through to the next instruction. 006686 */ 006687 case OP_IdxLE: /* jump, ncycle */ 006688 case OP_IdxGT: /* jump, ncycle */ 006689 case OP_IdxLT: /* jump, ncycle */ 006690 case OP_IdxGE: { /* jump, ncycle */ 006691 VdbeCursor *pC; 006692 int res; 006693 UnpackedRecord r; 006694 006695 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006696 pC = p->apCsr[pOp->p1]; 006697 assert( pC!=0 ); 006698 assert( pC->isOrdered ); 006699 assert( pC->eCurType==CURTYPE_BTREE ); 006700 assert( pC->uc.pCursor!=0); 006701 assert( pC->deferredMoveto==0 ); 006702 assert( pOp->p4type==P4_INT32 ); 006703 r.pKeyInfo = pC->pKeyInfo; 006704 r.nField = (u16)pOp->p4.i; 006705 if( pOp->opcode<OP_IdxLT ){ 006706 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); 006707 r.default_rc = -1; 006708 }else{ 006709 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); 006710 r.default_rc = 0; 006711 } 006712 r.aMem = &aMem[pOp->p3]; 006713 #ifdef SQLITE_DEBUG 006714 { 006715 int i; 006716 for(i=0; i<r.nField; i++){ 006717 assert( memIsValid(&r.aMem[i]) ); 006718 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]); 006719 } 006720 } 006721 #endif 006722 006723 /* Inlined version of sqlite3VdbeIdxKeyCompare() */ 006724 { 006725 i64 nCellKey = 0; 006726 BtCursor *pCur; 006727 Mem m; 006728 006729 assert( pC->eCurType==CURTYPE_BTREE ); 006730 pCur = pC->uc.pCursor; 006731 assert( sqlite3BtreeCursorIsValid(pCur) ); 006732 nCellKey = sqlite3BtreePayloadSize(pCur); 006733 /* nCellKey will always be between 0 and 0xffffffff because of the way 006734 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ 006735 if( nCellKey<=0 || nCellKey>0x7fffffff ){ 006736 rc = SQLITE_CORRUPT_BKPT; 006737 goto abort_due_to_error; 006738 } 006739 sqlite3VdbeMemInit(&m, db, 0); 006740 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m); 006741 if( rc ) goto abort_due_to_error; 006742 res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, &r, 0); 006743 sqlite3VdbeMemReleaseMalloc(&m); 006744 } 006745 /* End of inlined sqlite3VdbeIdxKeyCompare() */ 006746 006747 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); 006748 if( (pOp->opcode&1)==(OP_IdxLT&1) ){ 006749 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); 006750 res = -res; 006751 }else{ 006752 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); 006753 res++; 006754 } 006755 VdbeBranchTaken(res>0,2); 006756 assert( rc==SQLITE_OK ); 006757 if( res>0 ) goto jump_to_p2; 006758 break; 006759 } 006760 006761 /* Opcode: Destroy P1 P2 P3 * * 006762 ** 006763 ** Delete an entire database table or index whose root page in the database 006764 ** file is given by P1. 006765 ** 006766 ** The table being destroyed is in the main database file if P3==0. If 006767 ** P3==1 then the table to be destroyed is in the auxiliary database file 006768 ** that is used to store tables create using CREATE TEMPORARY TABLE. 006769 ** 006770 ** If AUTOVACUUM is enabled then it is possible that another root page 006771 ** might be moved into the newly deleted root page in order to keep all 006772 ** root pages contiguous at the beginning of the database. The former 006773 ** value of the root page that moved - its value before the move occurred - 006774 ** is stored in register P2. If no page movement was required (because the 006775 ** table being dropped was already the last one in the database) then a 006776 ** zero is stored in register P2. If AUTOVACUUM is disabled then a zero 006777 ** is stored in register P2. 006778 ** 006779 ** This opcode throws an error if there are any active reader VMs when 006780 ** it is invoked. This is done to avoid the difficulty associated with 006781 ** updating existing cursors when a root page is moved in an AUTOVACUUM 006782 ** database. This error is thrown even if the database is not an AUTOVACUUM 006783 ** db in order to avoid introducing an incompatibility between autovacuum 006784 ** and non-autovacuum modes. 006785 ** 006786 ** See also: Clear 006787 */ 006788 case OP_Destroy: { /* out2 */ 006789 int iMoved; 006790 int iDb; 006791 006792 sqlite3VdbeIncrWriteCounter(p, 0); 006793 assert( p->readOnly==0 ); 006794 assert( pOp->p1>1 ); 006795 pOut = out2Prerelease(p, pOp); 006796 pOut->flags = MEM_Null; 006797 if( db->nVdbeRead > db->nVDestroy+1 ){ 006798 rc = SQLITE_LOCKED; 006799 p->errorAction = OE_Abort; 006800 goto abort_due_to_error; 006801 }else{ 006802 iDb = pOp->p3; 006803 assert( DbMaskTest(p->btreeMask, iDb) ); 006804 iMoved = 0; /* Not needed. Only to silence a warning. */ 006805 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); 006806 pOut->flags = MEM_Int; 006807 pOut->u.i = iMoved; 006808 if( rc ) goto abort_due_to_error; 006809 #ifndef SQLITE_OMIT_AUTOVACUUM 006810 if( iMoved!=0 ){ 006811 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); 006812 /* All OP_Destroy operations occur on the same btree */ 006813 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); 006814 resetSchemaOnFault = iDb+1; 006815 } 006816 #endif 006817 } 006818 break; 006819 } 006820 006821 /* Opcode: Clear P1 P2 P3 006822 ** 006823 ** Delete all contents of the database table or index whose root page 006824 ** in the database file is given by P1. But, unlike Destroy, do not 006825 ** remove the table or index from the database file. 006826 ** 006827 ** The table being cleared is in the main database file if P2==0. If 006828 ** P2==1 then the table to be cleared is in the auxiliary database file 006829 ** that is used to store tables create using CREATE TEMPORARY TABLE. 006830 ** 006831 ** If the P3 value is non-zero, then the row change count is incremented 006832 ** by the number of rows in the table being cleared. If P3 is greater 006833 ** than zero, then the value stored in register P3 is also incremented 006834 ** by the number of rows in the table being cleared. 006835 ** 006836 ** See also: Destroy 006837 */ 006838 case OP_Clear: { 006839 i64 nChange; 006840 006841 sqlite3VdbeIncrWriteCounter(p, 0); 006842 nChange = 0; 006843 assert( p->readOnly==0 ); 006844 assert( DbMaskTest(p->btreeMask, pOp->p2) ); 006845 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange); 006846 if( pOp->p3 ){ 006847 p->nChange += nChange; 006848 if( pOp->p3>0 ){ 006849 assert( memIsValid(&aMem[pOp->p3]) ); 006850 memAboutToChange(p, &aMem[pOp->p3]); 006851 aMem[pOp->p3].u.i += nChange; 006852 } 006853 } 006854 if( rc ) goto abort_due_to_error; 006855 break; 006856 } 006857 006858 /* Opcode: ResetSorter P1 * * * * 006859 ** 006860 ** Delete all contents from the ephemeral table or sorter 006861 ** that is open on cursor P1. 006862 ** 006863 ** This opcode only works for cursors used for sorting and 006864 ** opened with OP_OpenEphemeral or OP_SorterOpen. 006865 */ 006866 case OP_ResetSorter: { 006867 VdbeCursor *pC; 006868 006869 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006870 pC = p->apCsr[pOp->p1]; 006871 assert( pC!=0 ); 006872 if( isSorter(pC) ){ 006873 sqlite3VdbeSorterReset(db, pC->uc.pSorter); 006874 }else{ 006875 assert( pC->eCurType==CURTYPE_BTREE ); 006876 assert( pC->isEphemeral ); 006877 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor); 006878 if( rc ) goto abort_due_to_error; 006879 } 006880 break; 006881 } 006882 006883 /* Opcode: CreateBtree P1 P2 P3 * * 006884 ** Synopsis: r[P2]=root iDb=P1 flags=P3 006885 ** 006886 ** Allocate a new b-tree in the main database file if P1==0 or in the 006887 ** TEMP database file if P1==1 or in an attached database if 006888 ** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table 006889 ** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table. 006890 ** The root page number of the new b-tree is stored in register P2. 006891 */ 006892 case OP_CreateBtree: { /* out2 */ 006893 Pgno pgno; 006894 Db *pDb; 006895 006896 sqlite3VdbeIncrWriteCounter(p, 0); 006897 pOut = out2Prerelease(p, pOp); 006898 pgno = 0; 006899 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY ); 006900 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 006901 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 006902 assert( p->readOnly==0 ); 006903 pDb = &db->aDb[pOp->p1]; 006904 assert( pDb->pBt!=0 ); 006905 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3); 006906 if( rc ) goto abort_due_to_error; 006907 pOut->u.i = pgno; 006908 break; 006909 } 006910 006911 /* Opcode: SqlExec * * * P4 * 006912 ** 006913 ** Run the SQL statement or statements specified in the P4 string. 006914 ** Disable Auth and Trace callbacks while those statements are running if 006915 ** P1 is true. 006916 */ 006917 case OP_SqlExec: { 006918 char *zErr; 006919 #ifndef SQLITE_OMIT_AUTHORIZATION 006920 sqlite3_xauth xAuth; 006921 #endif 006922 u8 mTrace; 006923 006924 sqlite3VdbeIncrWriteCounter(p, 0); 006925 db->nSqlExec++; 006926 zErr = 0; 006927 #ifndef SQLITE_OMIT_AUTHORIZATION 006928 xAuth = db->xAuth; 006929 #endif 006930 mTrace = db->mTrace; 006931 if( pOp->p1 ){ 006932 #ifndef SQLITE_OMIT_AUTHORIZATION 006933 db->xAuth = 0; 006934 #endif 006935 db->mTrace = 0; 006936 } 006937 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, &zErr); 006938 db->nSqlExec--; 006939 #ifndef SQLITE_OMIT_AUTHORIZATION 006940 db->xAuth = xAuth; 006941 #endif 006942 db->mTrace = mTrace; 006943 if( zErr || rc ){ 006944 sqlite3VdbeError(p, "%s", zErr); 006945 sqlite3_free(zErr); 006946 if( rc==SQLITE_NOMEM ) goto no_mem; 006947 goto abort_due_to_error; 006948 } 006949 break; 006950 } 006951 006952 /* Opcode: ParseSchema P1 * * P4 * 006953 ** 006954 ** Read and parse all entries from the schema table of database P1 006955 ** that match the WHERE clause P4. If P4 is a NULL pointer, then the 006956 ** entire schema for P1 is reparsed. 006957 ** 006958 ** This opcode invokes the parser to create a new virtual machine, 006959 ** then runs the new virtual machine. It is thus a re-entrant opcode. 006960 */ 006961 case OP_ParseSchema: { 006962 int iDb; 006963 const char *zSchema; 006964 char *zSql; 006965 InitData initData; 006966 006967 /* Any prepared statement that invokes this opcode will hold mutexes 006968 ** on every btree. This is a prerequisite for invoking 006969 ** sqlite3InitCallback(). 006970 */ 006971 #ifdef SQLITE_DEBUG 006972 for(iDb=0; iDb<db->nDb; iDb++){ 006973 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); 006974 } 006975 #endif 006976 006977 iDb = pOp->p1; 006978 assert( iDb>=0 && iDb<db->nDb ); 006979 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) 006980 || db->mallocFailed 006981 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) ); 006982 006983 #ifndef SQLITE_OMIT_ALTERTABLE 006984 if( pOp->p4.z==0 ){ 006985 sqlite3SchemaClear(db->aDb[iDb].pSchema); 006986 db->mDbFlags &= ~DBFLAG_SchemaKnownOk; 006987 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5); 006988 db->mDbFlags |= DBFLAG_SchemaChange; 006989 p->expired = 0; 006990 }else 006991 #endif 006992 { 006993 zSchema = LEGACY_SCHEMA_TABLE; 006994 initData.db = db; 006995 initData.iDb = iDb; 006996 initData.pzErrMsg = &p->zErrMsg; 006997 initData.mInitFlags = 0; 006998 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt); 006999 zSql = sqlite3MPrintf(db, 007000 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid", 007001 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z); 007002 if( zSql==0 ){ 007003 rc = SQLITE_NOMEM_BKPT; 007004 }else{ 007005 assert( db->init.busy==0 ); 007006 db->init.busy = 1; 007007 initData.rc = SQLITE_OK; 007008 initData.nInitRow = 0; 007009 assert( !db->mallocFailed ); 007010 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); 007011 if( rc==SQLITE_OK ) rc = initData.rc; 007012 if( rc==SQLITE_OK && initData.nInitRow==0 ){ 007013 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse 007014 ** at least one SQL statement. Any less than that indicates that 007015 ** the sqlite_schema table is corrupt. */ 007016 rc = SQLITE_CORRUPT_BKPT; 007017 } 007018 sqlite3DbFreeNN(db, zSql); 007019 db->init.busy = 0; 007020 } 007021 } 007022 if( rc ){ 007023 sqlite3ResetAllSchemasOfConnection(db); 007024 if( rc==SQLITE_NOMEM ){ 007025 goto no_mem; 007026 } 007027 goto abort_due_to_error; 007028 } 007029 break; 007030 } 007031 007032 #if !defined(SQLITE_OMIT_ANALYZE) 007033 /* Opcode: LoadAnalysis P1 * * * * 007034 ** 007035 ** Read the sqlite_stat1 table for database P1 and load the content 007036 ** of that table into the internal index hash table. This will cause 007037 ** the analysis to be used when preparing all subsequent queries. 007038 */ 007039 case OP_LoadAnalysis: { 007040 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 007041 rc = sqlite3AnalysisLoad(db, pOp->p1); 007042 if( rc ) goto abort_due_to_error; 007043 break; 007044 } 007045 #endif /* !defined(SQLITE_OMIT_ANALYZE) */ 007046 007047 /* Opcode: DropTable P1 * * P4 * 007048 ** 007049 ** Remove the internal (in-memory) data structures that describe 007050 ** the table named P4 in database P1. This is called after a table 007051 ** is dropped from disk (using the Destroy opcode) in order to keep 007052 ** the internal representation of the 007053 ** schema consistent with what is on disk. 007054 */ 007055 case OP_DropTable: { 007056 sqlite3VdbeIncrWriteCounter(p, 0); 007057 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); 007058 break; 007059 } 007060 007061 /* Opcode: DropIndex P1 * * P4 * 007062 ** 007063 ** Remove the internal (in-memory) data structures that describe 007064 ** the index named P4 in database P1. This is called after an index 007065 ** is dropped from disk (using the Destroy opcode) 007066 ** in order to keep the internal representation of the 007067 ** schema consistent with what is on disk. 007068 */ 007069 case OP_DropIndex: { 007070 sqlite3VdbeIncrWriteCounter(p, 0); 007071 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); 007072 break; 007073 } 007074 007075 /* Opcode: DropTrigger P1 * * P4 * 007076 ** 007077 ** Remove the internal (in-memory) data structures that describe 007078 ** the trigger named P4 in database P1. This is called after a trigger 007079 ** is dropped from disk (using the Destroy opcode) in order to keep 007080 ** the internal representation of the 007081 ** schema consistent with what is on disk. 007082 */ 007083 case OP_DropTrigger: { 007084 sqlite3VdbeIncrWriteCounter(p, 0); 007085 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); 007086 break; 007087 } 007088 007089 007090 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 007091 /* Opcode: IntegrityCk P1 P2 P3 P4 P5 007092 ** 007093 ** Do an analysis of the currently open database. Store in 007094 ** register P1 the text of an error message describing any problems. 007095 ** If no problems are found, store a NULL in register P1. 007096 ** 007097 ** The register P3 contains one less than the maximum number of allowed errors. 007098 ** At most reg(P3) errors will be reported. 007099 ** In other words, the analysis stops as soon as reg(P1) errors are 007100 ** seen. Reg(P1) is updated with the number of errors remaining. 007101 ** 007102 ** The root page numbers of all tables in the database are integers 007103 ** stored in P4_INTARRAY argument. 007104 ** 007105 ** If P5 is not zero, the check is done on the auxiliary database 007106 ** file, not the main database file. 007107 ** 007108 ** This opcode is used to implement the integrity_check pragma. 007109 */ 007110 case OP_IntegrityCk: { 007111 int nRoot; /* Number of tables to check. (Number of root pages.) */ 007112 Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */ 007113 int nErr; /* Number of errors reported */ 007114 char *z; /* Text of the error report */ 007115 Mem *pnErr; /* Register keeping track of errors remaining */ 007116 007117 assert( p->bIsReader ); 007118 nRoot = pOp->p2; 007119 aRoot = pOp->p4.ai; 007120 assert( nRoot>0 ); 007121 assert( aRoot[0]==(Pgno)nRoot ); 007122 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 007123 pnErr = &aMem[pOp->p3]; 007124 assert( (pnErr->flags & MEM_Int)!=0 ); 007125 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); 007126 pIn1 = &aMem[pOp->p1]; 007127 assert( pOp->p5<db->nDb ); 007128 assert( DbMaskTest(p->btreeMask, pOp->p5) ); 007129 rc = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot, 007130 (int)pnErr->u.i+1, &nErr, &z); 007131 sqlite3VdbeMemSetNull(pIn1); 007132 if( nErr==0 ){ 007133 assert( z==0 ); 007134 }else if( rc ){ 007135 sqlite3_free(z); 007136 goto abort_due_to_error; 007137 }else{ 007138 pnErr->u.i -= nErr-1; 007139 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); 007140 } 007141 UPDATE_MAX_BLOBSIZE(pIn1); 007142 sqlite3VdbeChangeEncoding(pIn1, encoding); 007143 goto check_for_interrupt; 007144 } 007145 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 007146 007147 /* Opcode: RowSetAdd P1 P2 * * * 007148 ** Synopsis: rowset(P1)=r[P2] 007149 ** 007150 ** Insert the integer value held by register P2 into a RowSet object 007151 ** held in register P1. 007152 ** 007153 ** An assertion fails if P2 is not an integer. 007154 */ 007155 case OP_RowSetAdd: { /* in1, in2 */ 007156 pIn1 = &aMem[pOp->p1]; 007157 pIn2 = &aMem[pOp->p2]; 007158 assert( (pIn2->flags & MEM_Int)!=0 ); 007159 if( (pIn1->flags & MEM_Blob)==0 ){ 007160 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem; 007161 } 007162 assert( sqlite3VdbeMemIsRowSet(pIn1) ); 007163 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i); 007164 break; 007165 } 007166 007167 /* Opcode: RowSetRead P1 P2 P3 * * 007168 ** Synopsis: r[P3]=rowset(P1) 007169 ** 007170 ** Extract the smallest value from the RowSet object in P1 007171 ** and put that value into register P3. 007172 ** Or, if RowSet object P1 is initially empty, leave P3 007173 ** unchanged and jump to instruction P2. 007174 */ 007175 case OP_RowSetRead: { /* jump, in1, out3 */ 007176 i64 val; 007177 007178 pIn1 = &aMem[pOp->p1]; 007179 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) ); 007180 if( (pIn1->flags & MEM_Blob)==0 007181 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0 007182 ){ 007183 /* The boolean index is empty */ 007184 sqlite3VdbeMemSetNull(pIn1); 007185 VdbeBranchTaken(1,2); 007186 goto jump_to_p2_and_check_for_interrupt; 007187 }else{ 007188 /* A value was pulled from the index */ 007189 VdbeBranchTaken(0,2); 007190 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); 007191 } 007192 goto check_for_interrupt; 007193 } 007194 007195 /* Opcode: RowSetTest P1 P2 P3 P4 007196 ** Synopsis: if r[P3] in rowset(P1) goto P2 007197 ** 007198 ** Register P3 is assumed to hold a 64-bit integer value. If register P1 007199 ** contains a RowSet object and that RowSet object contains 007200 ** the value held in P3, jump to register P2. Otherwise, insert the 007201 ** integer in P3 into the RowSet and continue on to the 007202 ** next opcode. 007203 ** 007204 ** The RowSet object is optimized for the case where sets of integers 007205 ** are inserted in distinct phases, which each set contains no duplicates. 007206 ** Each set is identified by a unique P4 value. The first set 007207 ** must have P4==0, the final set must have P4==-1, and for all other sets 007208 ** must have P4>0. 007209 ** 007210 ** This allows optimizations: (a) when P4==0 there is no need to test 007211 ** the RowSet object for P3, as it is guaranteed not to contain it, 007212 ** (b) when P4==-1 there is no need to insert the value, as it will 007213 ** never be tested for, and (c) when a value that is part of set X is 007214 ** inserted, there is no need to search to see if the same value was 007215 ** previously inserted as part of set X (only if it was previously 007216 ** inserted as part of some other set). 007217 */ 007218 case OP_RowSetTest: { /* jump, in1, in3 */ 007219 int iSet; 007220 int exists; 007221 007222 pIn1 = &aMem[pOp->p1]; 007223 pIn3 = &aMem[pOp->p3]; 007224 iSet = pOp->p4.i; 007225 assert( pIn3->flags&MEM_Int ); 007226 007227 /* If there is anything other than a rowset object in memory cell P1, 007228 ** delete it now and initialize P1 with an empty rowset 007229 */ 007230 if( (pIn1->flags & MEM_Blob)==0 ){ 007231 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem; 007232 } 007233 assert( sqlite3VdbeMemIsRowSet(pIn1) ); 007234 assert( pOp->p4type==P4_INT32 ); 007235 assert( iSet==-1 || iSet>=0 ); 007236 if( iSet ){ 007237 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i); 007238 VdbeBranchTaken(exists!=0,2); 007239 if( exists ) goto jump_to_p2; 007240 } 007241 if( iSet>=0 ){ 007242 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i); 007243 } 007244 break; 007245 } 007246 007247 007248 #ifndef SQLITE_OMIT_TRIGGER 007249 007250 /* Opcode: Program P1 P2 P3 P4 P5 007251 ** 007252 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). 007253 ** 007254 ** P1 contains the address of the memory cell that contains the first memory 007255 ** cell in an array of values used as arguments to the sub-program. P2 007256 ** contains the address to jump to if the sub-program throws an IGNORE 007257 ** exception using the RAISE() function. Register P3 contains the address 007258 ** of a memory cell in this (the parent) VM that is used to allocate the 007259 ** memory required by the sub-vdbe at runtime. 007260 ** 007261 ** P4 is a pointer to the VM containing the trigger program. 007262 ** 007263 ** If P5 is non-zero, then recursive program invocation is enabled. 007264 */ 007265 case OP_Program: { /* jump */ 007266 int nMem; /* Number of memory registers for sub-program */ 007267 int nByte; /* Bytes of runtime space required for sub-program */ 007268 Mem *pRt; /* Register to allocate runtime space */ 007269 Mem *pMem; /* Used to iterate through memory cells */ 007270 Mem *pEnd; /* Last memory cell in new array */ 007271 VdbeFrame *pFrame; /* New vdbe frame to execute in */ 007272 SubProgram *pProgram; /* Sub-program to execute */ 007273 void *t; /* Token identifying trigger */ 007274 007275 pProgram = pOp->p4.pProgram; 007276 pRt = &aMem[pOp->p3]; 007277 assert( pProgram->nOp>0 ); 007278 007279 /* If the p5 flag is clear, then recursive invocation of triggers is 007280 ** disabled for backwards compatibility (p5 is set if this sub-program 007281 ** is really a trigger, not a foreign key action, and the flag set 007282 ** and cleared by the "PRAGMA recursive_triggers" command is clear). 007283 ** 007284 ** It is recursive invocation of triggers, at the SQL level, that is 007285 ** disabled. In some cases a single trigger may generate more than one 007286 ** SubProgram (if the trigger may be executed with more than one different 007287 ** ON CONFLICT algorithm). SubProgram structures associated with a 007288 ** single trigger all have the same value for the SubProgram.token 007289 ** variable. */ 007290 if( pOp->p5 ){ 007291 t = pProgram->token; 007292 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); 007293 if( pFrame ) break; 007294 } 007295 007296 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ 007297 rc = SQLITE_ERROR; 007298 sqlite3VdbeError(p, "too many levels of trigger recursion"); 007299 goto abort_due_to_error; 007300 } 007301 007302 /* Register pRt is used to store the memory required to save the state 007303 ** of the current program, and the memory required at runtime to execute 007304 ** the trigger program. If this trigger has been fired before, then pRt 007305 ** is already allocated. Otherwise, it must be initialized. */ 007306 if( (pRt->flags&MEM_Blob)==0 ){ 007307 /* SubProgram.nMem is set to the number of memory cells used by the 007308 ** program stored in SubProgram.aOp. As well as these, one memory 007309 ** cell is required for each cursor used by the program. Set local 007310 ** variable nMem (and later, VdbeFrame.nChildMem) to this value. 007311 */ 007312 nMem = pProgram->nMem + pProgram->nCsr; 007313 assert( nMem>0 ); 007314 if( pProgram->nCsr==0 ) nMem++; 007315 nByte = ROUND8(sizeof(VdbeFrame)) 007316 + nMem * sizeof(Mem) 007317 + pProgram->nCsr * sizeof(VdbeCursor*) 007318 + (pProgram->nOp + 7)/8; 007319 pFrame = sqlite3DbMallocZero(db, nByte); 007320 if( !pFrame ){ 007321 goto no_mem; 007322 } 007323 sqlite3VdbeMemRelease(pRt); 007324 pRt->flags = MEM_Blob|MEM_Dyn; 007325 pRt->z = (char*)pFrame; 007326 pRt->n = nByte; 007327 pRt->xDel = sqlite3VdbeFrameMemDel; 007328 007329 pFrame->v = p; 007330 pFrame->nChildMem = nMem; 007331 pFrame->nChildCsr = pProgram->nCsr; 007332 pFrame->pc = (int)(pOp - aOp); 007333 pFrame->aMem = p->aMem; 007334 pFrame->nMem = p->nMem; 007335 pFrame->apCsr = p->apCsr; 007336 pFrame->nCursor = p->nCursor; 007337 pFrame->aOp = p->aOp; 007338 pFrame->nOp = p->nOp; 007339 pFrame->token = pProgram->token; 007340 #ifdef SQLITE_DEBUG 007341 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC; 007342 #endif 007343 007344 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; 007345 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ 007346 pMem->flags = MEM_Undefined; 007347 pMem->db = db; 007348 } 007349 }else{ 007350 pFrame = (VdbeFrame*)pRt->z; 007351 assert( pRt->xDel==sqlite3VdbeFrameMemDel ); 007352 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem 007353 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) ); 007354 assert( pProgram->nCsr==pFrame->nChildCsr ); 007355 assert( (int)(pOp - aOp)==pFrame->pc ); 007356 } 007357 007358 p->nFrame++; 007359 pFrame->pParent = p->pFrame; 007360 pFrame->lastRowid = db->lastRowid; 007361 pFrame->nChange = p->nChange; 007362 pFrame->nDbChange = p->db->nChange; 007363 assert( pFrame->pAuxData==0 ); 007364 pFrame->pAuxData = p->pAuxData; 007365 p->pAuxData = 0; 007366 p->nChange = 0; 007367 p->pFrame = pFrame; 007368 p->aMem = aMem = VdbeFrameMem(pFrame); 007369 p->nMem = pFrame->nChildMem; 007370 p->nCursor = (u16)pFrame->nChildCsr; 007371 p->apCsr = (VdbeCursor **)&aMem[p->nMem]; 007372 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr]; 007373 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8); 007374 p->aOp = aOp = pProgram->aOp; 007375 p->nOp = pProgram->nOp; 007376 #ifdef SQLITE_DEBUG 007377 /* Verify that second and subsequent executions of the same trigger do not 007378 ** try to reuse register values from the first use. */ 007379 { 007380 int i; 007381 for(i=0; i<p->nMem; i++){ 007382 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */ 007383 MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */ 007384 } 007385 } 007386 #endif 007387 pOp = &aOp[-1]; 007388 goto check_for_interrupt; 007389 } 007390 007391 /* Opcode: Param P1 P2 * * * 007392 ** 007393 ** This opcode is only ever present in sub-programs called via the 007394 ** OP_Program instruction. Copy a value currently stored in a memory 007395 ** cell of the calling (parent) frame to cell P2 in the current frames 007396 ** address space. This is used by trigger programs to access the new.* 007397 ** and old.* values. 007398 ** 007399 ** The address of the cell in the parent frame is determined by adding 007400 ** the value of the P1 argument to the value of the P1 argument to the 007401 ** calling OP_Program instruction. 007402 */ 007403 case OP_Param: { /* out2 */ 007404 VdbeFrame *pFrame; 007405 Mem *pIn; 007406 pOut = out2Prerelease(p, pOp); 007407 pFrame = p->pFrame; 007408 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; 007409 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); 007410 break; 007411 } 007412 007413 #endif /* #ifndef SQLITE_OMIT_TRIGGER */ 007414 007415 #ifndef SQLITE_OMIT_FOREIGN_KEY 007416 /* Opcode: FkCounter P1 P2 * * * 007417 ** Synopsis: fkctr[P1]+=P2 007418 ** 007419 ** Increment a "constraint counter" by P2 (P2 may be negative or positive). 007420 ** If P1 is non-zero, the database constraint counter is incremented 007421 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the 007422 ** statement counter is incremented (immediate foreign key constraints). 007423 */ 007424 case OP_FkCounter: { 007425 if( db->flags & SQLITE_DeferFKs ){ 007426 db->nDeferredImmCons += pOp->p2; 007427 }else if( pOp->p1 ){ 007428 db->nDeferredCons += pOp->p2; 007429 }else{ 007430 p->nFkConstraint += pOp->p2; 007431 } 007432 break; 007433 } 007434 007435 /* Opcode: FkIfZero P1 P2 * * * 007436 ** Synopsis: if fkctr[P1]==0 goto P2 007437 ** 007438 ** This opcode tests if a foreign key constraint-counter is currently zero. 007439 ** If so, jump to instruction P2. Otherwise, fall through to the next 007440 ** instruction. 007441 ** 007442 ** If P1 is non-zero, then the jump is taken if the database constraint-counter 007443 ** is zero (the one that counts deferred constraint violations). If P1 is 007444 ** zero, the jump is taken if the statement constraint-counter is zero 007445 ** (immediate foreign key constraint violations). 007446 */ 007447 case OP_FkIfZero: { /* jump */ 007448 if( pOp->p1 ){ 007449 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2); 007450 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; 007451 }else{ 007452 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2); 007453 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; 007454 } 007455 break; 007456 } 007457 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ 007458 007459 #ifndef SQLITE_OMIT_AUTOINCREMENT 007460 /* Opcode: MemMax P1 P2 * * * 007461 ** Synopsis: r[P1]=max(r[P1],r[P2]) 007462 ** 007463 ** P1 is a register in the root frame of this VM (the root frame is 007464 ** different from the current frame if this instruction is being executed 007465 ** within a sub-program). Set the value of register P1 to the maximum of 007466 ** its current value and the value in register P2. 007467 ** 007468 ** This instruction throws an error if the memory cell is not initially 007469 ** an integer. 007470 */ 007471 case OP_MemMax: { /* in2 */ 007472 VdbeFrame *pFrame; 007473 if( p->pFrame ){ 007474 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); 007475 pIn1 = &pFrame->aMem[pOp->p1]; 007476 }else{ 007477 pIn1 = &aMem[pOp->p1]; 007478 } 007479 assert( memIsValid(pIn1) ); 007480 sqlite3VdbeMemIntegerify(pIn1); 007481 pIn2 = &aMem[pOp->p2]; 007482 sqlite3VdbeMemIntegerify(pIn2); 007483 if( pIn1->u.i<pIn2->u.i){ 007484 pIn1->u.i = pIn2->u.i; 007485 } 007486 break; 007487 } 007488 #endif /* SQLITE_OMIT_AUTOINCREMENT */ 007489 007490 /* Opcode: IfPos P1 P2 P3 * * 007491 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 007492 ** 007493 ** Register P1 must contain an integer. 007494 ** If the value of register P1 is 1 or greater, subtract P3 from the 007495 ** value in P1 and jump to P2. 007496 ** 007497 ** If the initial value of register P1 is less than 1, then the 007498 ** value is unchanged and control passes through to the next instruction. 007499 */ 007500 case OP_IfPos: { /* jump, in1 */ 007501 pIn1 = &aMem[pOp->p1]; 007502 assert( pIn1->flags&MEM_Int ); 007503 VdbeBranchTaken( pIn1->u.i>0, 2); 007504 if( pIn1->u.i>0 ){ 007505 pIn1->u.i -= pOp->p3; 007506 goto jump_to_p2; 007507 } 007508 break; 007509 } 007510 007511 /* Opcode: OffsetLimit P1 P2 P3 * * 007512 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) 007513 ** 007514 ** This opcode performs a commonly used computation associated with 007515 ** LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3] 007516 ** holds the offset counter. The opcode computes the combined value 007517 ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] 007518 ** value computed is the total number of rows that will need to be 007519 ** visited in order to complete the query. 007520 ** 007521 ** If r[P3] is zero or negative, that means there is no OFFSET 007522 ** and r[P2] is set to be the value of the LIMIT, r[P1]. 007523 ** 007524 ** if r[P1] is zero or negative, that means there is no LIMIT 007525 ** and r[P2] is set to -1. 007526 ** 007527 ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3]. 007528 */ 007529 case OP_OffsetLimit: { /* in1, out2, in3 */ 007530 i64 x; 007531 pIn1 = &aMem[pOp->p1]; 007532 pIn3 = &aMem[pOp->p3]; 007533 pOut = out2Prerelease(p, pOp); 007534 assert( pIn1->flags & MEM_Int ); 007535 assert( pIn3->flags & MEM_Int ); 007536 x = pIn1->u.i; 007537 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){ 007538 /* If the LIMIT is less than or equal to zero, loop forever. This 007539 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then 007540 ** also loop forever. This is undocumented. In fact, one could argue 007541 ** that the loop should terminate. But assuming 1 billion iterations 007542 ** per second (far exceeding the capabilities of any current hardware) 007543 ** it would take nearly 300 years to actually reach the limit. So 007544 ** looping forever is a reasonable approximation. */ 007545 pOut->u.i = -1; 007546 }else{ 007547 pOut->u.i = x; 007548 } 007549 break; 007550 } 007551 007552 /* Opcode: IfNotZero P1 P2 * * * 007553 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2 007554 ** 007555 ** Register P1 must contain an integer. If the content of register P1 is 007556 ** initially greater than zero, then decrement the value in register P1. 007557 ** If it is non-zero (negative or positive) and then also jump to P2. 007558 ** If register P1 is initially zero, leave it unchanged and fall through. 007559 */ 007560 case OP_IfNotZero: { /* jump, in1 */ 007561 pIn1 = &aMem[pOp->p1]; 007562 assert( pIn1->flags&MEM_Int ); 007563 VdbeBranchTaken(pIn1->u.i<0, 2); 007564 if( pIn1->u.i ){ 007565 if( pIn1->u.i>0 ) pIn1->u.i--; 007566 goto jump_to_p2; 007567 } 007568 break; 007569 } 007570 007571 /* Opcode: DecrJumpZero P1 P2 * * * 007572 ** Synopsis: if (--r[P1])==0 goto P2 007573 ** 007574 ** Register P1 must hold an integer. Decrement the value in P1 007575 ** and jump to P2 if the new value is exactly zero. 007576 */ 007577 case OP_DecrJumpZero: { /* jump, in1 */ 007578 pIn1 = &aMem[pOp->p1]; 007579 assert( pIn1->flags&MEM_Int ); 007580 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--; 007581 VdbeBranchTaken(pIn1->u.i==0, 2); 007582 if( pIn1->u.i==0 ) goto jump_to_p2; 007583 break; 007584 } 007585 007586 007587 /* Opcode: AggStep * P2 P3 P4 P5 007588 ** Synopsis: accum=r[P3] step(r[P2@P5]) 007589 ** 007590 ** Execute the xStep function for an aggregate. 007591 ** The function has P5 arguments. P4 is a pointer to the 007592 ** FuncDef structure that specifies the function. Register P3 is the 007593 ** accumulator. 007594 ** 007595 ** The P5 arguments are taken from register P2 and its 007596 ** successors. 007597 */ 007598 /* Opcode: AggInverse * P2 P3 P4 P5 007599 ** Synopsis: accum=r[P3] inverse(r[P2@P5]) 007600 ** 007601 ** Execute the xInverse function for an aggregate. 007602 ** The function has P5 arguments. P4 is a pointer to the 007603 ** FuncDef structure that specifies the function. Register P3 is the 007604 ** accumulator. 007605 ** 007606 ** The P5 arguments are taken from register P2 and its 007607 ** successors. 007608 */ 007609 /* Opcode: AggStep1 P1 P2 P3 P4 P5 007610 ** Synopsis: accum=r[P3] step(r[P2@P5]) 007611 ** 007612 ** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an 007613 ** aggregate. The function has P5 arguments. P4 is a pointer to the 007614 ** FuncDef structure that specifies the function. Register P3 is the 007615 ** accumulator. 007616 ** 007617 ** The P5 arguments are taken from register P2 and its 007618 ** successors. 007619 ** 007620 ** This opcode is initially coded as OP_AggStep0. On first evaluation, 007621 ** the FuncDef stored in P4 is converted into an sqlite3_context and 007622 ** the opcode is changed. In this way, the initialization of the 007623 ** sqlite3_context only happens once, instead of on each call to the 007624 ** step function. 007625 */ 007626 case OP_AggInverse: 007627 case OP_AggStep: { 007628 int n; 007629 sqlite3_context *pCtx; 007630 007631 assert( pOp->p4type==P4_FUNCDEF ); 007632 n = pOp->p5; 007633 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 007634 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) ); 007635 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); 007636 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) + 007637 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*))); 007638 if( pCtx==0 ) goto no_mem; 007639 pCtx->pMem = 0; 007640 pCtx->pOut = (Mem*)&(pCtx->argv[n]); 007641 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null); 007642 pCtx->pFunc = pOp->p4.pFunc; 007643 pCtx->iOp = (int)(pOp - aOp); 007644 pCtx->pVdbe = p; 007645 pCtx->skipFlag = 0; 007646 pCtx->isError = 0; 007647 pCtx->enc = encoding; 007648 pCtx->argc = n; 007649 pOp->p4type = P4_FUNCCTX; 007650 pOp->p4.pCtx = pCtx; 007651 007652 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */ 007653 assert( pOp->p1==(pOp->opcode==OP_AggInverse) ); 007654 007655 pOp->opcode = OP_AggStep1; 007656 /* Fall through into OP_AggStep */ 007657 /* no break */ deliberate_fall_through 007658 } 007659 case OP_AggStep1: { 007660 int i; 007661 sqlite3_context *pCtx; 007662 Mem *pMem; 007663 007664 assert( pOp->p4type==P4_FUNCCTX ); 007665 pCtx = pOp->p4.pCtx; 007666 pMem = &aMem[pOp->p3]; 007667 007668 #ifdef SQLITE_DEBUG 007669 if( pOp->p1 ){ 007670 /* This is an OP_AggInverse call. Verify that xStep has always 007671 ** been called at least once prior to any xInverse call. */ 007672 assert( pMem->uTemp==0x1122e0e3 ); 007673 }else{ 007674 /* This is an OP_AggStep call. Mark it as such. */ 007675 pMem->uTemp = 0x1122e0e3; 007676 } 007677 #endif 007678 007679 /* If this function is inside of a trigger, the register array in aMem[] 007680 ** might change from one evaluation to the next. The next block of code 007681 ** checks to see if the register array has changed, and if so it 007682 ** reinitializes the relevant parts of the sqlite3_context object */ 007683 if( pCtx->pMem != pMem ){ 007684 pCtx->pMem = pMem; 007685 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; 007686 } 007687 007688 #ifdef SQLITE_DEBUG 007689 for(i=0; i<pCtx->argc; i++){ 007690 assert( memIsValid(pCtx->argv[i]) ); 007691 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); 007692 } 007693 #endif 007694 007695 pMem->n++; 007696 assert( pCtx->pOut->flags==MEM_Null ); 007697 assert( pCtx->isError==0 ); 007698 assert( pCtx->skipFlag==0 ); 007699 #ifndef SQLITE_OMIT_WINDOWFUNC 007700 if( pOp->p1 ){ 007701 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv); 007702 }else 007703 #endif 007704 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ 007705 007706 if( pCtx->isError ){ 007707 if( pCtx->isError>0 ){ 007708 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); 007709 rc = pCtx->isError; 007710 } 007711 if( pCtx->skipFlag ){ 007712 assert( pOp[-1].opcode==OP_CollSeq ); 007713 i = pOp[-1].p1; 007714 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); 007715 pCtx->skipFlag = 0; 007716 } 007717 sqlite3VdbeMemRelease(pCtx->pOut); 007718 pCtx->pOut->flags = MEM_Null; 007719 pCtx->isError = 0; 007720 if( rc ) goto abort_due_to_error; 007721 } 007722 assert( pCtx->pOut->flags==MEM_Null ); 007723 assert( pCtx->skipFlag==0 ); 007724 break; 007725 } 007726 007727 /* Opcode: AggFinal P1 P2 * P4 * 007728 ** Synopsis: accum=r[P1] N=P2 007729 ** 007730 ** P1 is the memory location that is the accumulator for an aggregate 007731 ** or window function. Execute the finalizer function 007732 ** for an aggregate and store the result in P1. 007733 ** 007734 ** P2 is the number of arguments that the step function takes and 007735 ** P4 is a pointer to the FuncDef for this function. The P2 007736 ** argument is not used by this opcode. It is only there to disambiguate 007737 ** functions that can take varying numbers of arguments. The 007738 ** P4 argument is only needed for the case where 007739 ** the step function was not previously called. 007740 */ 007741 /* Opcode: AggValue * P2 P3 P4 * 007742 ** Synopsis: r[P3]=value N=P2 007743 ** 007744 ** Invoke the xValue() function and store the result in register P3. 007745 ** 007746 ** P2 is the number of arguments that the step function takes and 007747 ** P4 is a pointer to the FuncDef for this function. The P2 007748 ** argument is not used by this opcode. It is only there to disambiguate 007749 ** functions that can take varying numbers of arguments. The 007750 ** P4 argument is only needed for the case where 007751 ** the step function was not previously called. 007752 */ 007753 case OP_AggValue: 007754 case OP_AggFinal: { 007755 Mem *pMem; 007756 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 007757 assert( pOp->p3==0 || pOp->opcode==OP_AggValue ); 007758 pMem = &aMem[pOp->p1]; 007759 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); 007760 #ifndef SQLITE_OMIT_WINDOWFUNC 007761 if( pOp->p3 ){ 007762 memAboutToChange(p, &aMem[pOp->p3]); 007763 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc); 007764 pMem = &aMem[pOp->p3]; 007765 }else 007766 #endif 007767 { 007768 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); 007769 } 007770 007771 if( rc ){ 007772 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); 007773 goto abort_due_to_error; 007774 } 007775 sqlite3VdbeChangeEncoding(pMem, encoding); 007776 UPDATE_MAX_BLOBSIZE(pMem); 007777 REGISTER_TRACE((int)(pMem-aMem), pMem); 007778 break; 007779 } 007780 007781 #ifndef SQLITE_OMIT_WAL 007782 /* Opcode: Checkpoint P1 P2 P3 * * 007783 ** 007784 ** Checkpoint database P1. This is a no-op if P1 is not currently in 007785 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL, 007786 ** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns 007787 ** SQLITE_BUSY or not, respectively. Write the number of pages in the 007788 ** WAL after the checkpoint into mem[P3+1] and the number of pages 007789 ** in the WAL that have been checkpointed after the checkpoint 007790 ** completes into mem[P3+2]. However on an error, mem[P3+1] and 007791 ** mem[P3+2] are initialized to -1. 007792 */ 007793 case OP_Checkpoint: { 007794 int i; /* Loop counter */ 007795 int aRes[3]; /* Results */ 007796 Mem *pMem; /* Write results here */ 007797 007798 assert( p->readOnly==0 ); 007799 aRes[0] = 0; 007800 aRes[1] = aRes[2] = -1; 007801 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE 007802 || pOp->p2==SQLITE_CHECKPOINT_FULL 007803 || pOp->p2==SQLITE_CHECKPOINT_RESTART 007804 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE 007805 ); 007806 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); 007807 if( rc ){ 007808 if( rc!=SQLITE_BUSY ) goto abort_due_to_error; 007809 rc = SQLITE_OK; 007810 aRes[0] = 1; 007811 } 007812 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ 007813 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); 007814 } 007815 break; 007816 }; 007817 #endif 007818 007819 #ifndef SQLITE_OMIT_PRAGMA 007820 /* Opcode: JournalMode P1 P2 P3 * * 007821 ** 007822 ** Change the journal mode of database P1 to P3. P3 must be one of the 007823 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback 007824 ** modes (delete, truncate, persist, off and memory), this is a simple 007825 ** operation. No IO is required. 007826 ** 007827 ** If changing into or out of WAL mode the procedure is more complicated. 007828 ** 007829 ** Write a string containing the final journal-mode to register P2. 007830 */ 007831 case OP_JournalMode: { /* out2 */ 007832 Btree *pBt; /* Btree to change journal mode of */ 007833 Pager *pPager; /* Pager associated with pBt */ 007834 int eNew; /* New journal mode */ 007835 int eOld; /* The old journal mode */ 007836 #ifndef SQLITE_OMIT_WAL 007837 const char *zFilename; /* Name of database file for pPager */ 007838 #endif 007839 007840 pOut = out2Prerelease(p, pOp); 007841 eNew = pOp->p3; 007842 assert( eNew==PAGER_JOURNALMODE_DELETE 007843 || eNew==PAGER_JOURNALMODE_TRUNCATE 007844 || eNew==PAGER_JOURNALMODE_PERSIST 007845 || eNew==PAGER_JOURNALMODE_OFF 007846 || eNew==PAGER_JOURNALMODE_MEMORY 007847 || eNew==PAGER_JOURNALMODE_WAL 007848 || eNew==PAGER_JOURNALMODE_QUERY 007849 ); 007850 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 007851 assert( p->readOnly==0 ); 007852 007853 pBt = db->aDb[pOp->p1].pBt; 007854 pPager = sqlite3BtreePager(pBt); 007855 eOld = sqlite3PagerGetJournalMode(pPager); 007856 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; 007857 assert( sqlite3BtreeHoldsMutex(pBt) ); 007858 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; 007859 007860 #ifndef SQLITE_OMIT_WAL 007861 zFilename = sqlite3PagerFilename(pPager, 1); 007862 007863 /* Do not allow a transition to journal_mode=WAL for a database 007864 ** in temporary storage or if the VFS does not support shared memory 007865 */ 007866 if( eNew==PAGER_JOURNALMODE_WAL 007867 && (sqlite3Strlen30(zFilename)==0 /* Temp file */ 007868 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ 007869 ){ 007870 eNew = eOld; 007871 } 007872 007873 if( (eNew!=eOld) 007874 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) 007875 ){ 007876 if( !db->autoCommit || db->nVdbeRead>1 ){ 007877 rc = SQLITE_ERROR; 007878 sqlite3VdbeError(p, 007879 "cannot change %s wal mode from within a transaction", 007880 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") 007881 ); 007882 goto abort_due_to_error; 007883 }else{ 007884 007885 if( eOld==PAGER_JOURNALMODE_WAL ){ 007886 /* If leaving WAL mode, close the log file. If successful, the call 007887 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log 007888 ** file. An EXCLUSIVE lock may still be held on the database file 007889 ** after a successful return. 007890 */ 007891 rc = sqlite3PagerCloseWal(pPager, db); 007892 if( rc==SQLITE_OK ){ 007893 sqlite3PagerSetJournalMode(pPager, eNew); 007894 } 007895 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ 007896 /* Cannot transition directly from MEMORY to WAL. Use mode OFF 007897 ** as an intermediate */ 007898 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); 007899 } 007900 007901 /* Open a transaction on the database file. Regardless of the journal 007902 ** mode, this transaction always uses a rollback journal. 007903 */ 007904 assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE ); 007905 if( rc==SQLITE_OK ){ 007906 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); 007907 } 007908 } 007909 } 007910 #endif /* ifndef SQLITE_OMIT_WAL */ 007911 007912 if( rc ) eNew = eOld; 007913 eNew = sqlite3PagerSetJournalMode(pPager, eNew); 007914 007915 pOut->flags = MEM_Str|MEM_Static|MEM_Term; 007916 pOut->z = (char *)sqlite3JournalModename(eNew); 007917 pOut->n = sqlite3Strlen30(pOut->z); 007918 pOut->enc = SQLITE_UTF8; 007919 sqlite3VdbeChangeEncoding(pOut, encoding); 007920 if( rc ) goto abort_due_to_error; 007921 break; 007922 }; 007923 #endif /* SQLITE_OMIT_PRAGMA */ 007924 007925 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) 007926 /* Opcode: Vacuum P1 P2 * * * 007927 ** 007928 ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more 007929 ** for an attached database. The "temp" database may not be vacuumed. 007930 ** 007931 ** If P2 is not zero, then it is a register holding a string which is 007932 ** the file into which the result of vacuum should be written. When 007933 ** P2 is zero, the vacuum overwrites the original database. 007934 */ 007935 case OP_Vacuum: { 007936 assert( p->readOnly==0 ); 007937 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1, 007938 pOp->p2 ? &aMem[pOp->p2] : 0); 007939 if( rc ) goto abort_due_to_error; 007940 break; 007941 } 007942 #endif 007943 007944 #if !defined(SQLITE_OMIT_AUTOVACUUM) 007945 /* Opcode: IncrVacuum P1 P2 * * * 007946 ** 007947 ** Perform a single step of the incremental vacuum procedure on 007948 ** the P1 database. If the vacuum has finished, jump to instruction 007949 ** P2. Otherwise, fall through to the next instruction. 007950 */ 007951 case OP_IncrVacuum: { /* jump */ 007952 Btree *pBt; 007953 007954 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 007955 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 007956 assert( p->readOnly==0 ); 007957 pBt = db->aDb[pOp->p1].pBt; 007958 rc = sqlite3BtreeIncrVacuum(pBt); 007959 VdbeBranchTaken(rc==SQLITE_DONE,2); 007960 if( rc ){ 007961 if( rc!=SQLITE_DONE ) goto abort_due_to_error; 007962 rc = SQLITE_OK; 007963 goto jump_to_p2; 007964 } 007965 break; 007966 } 007967 #endif 007968 007969 /* Opcode: Expire P1 P2 * * * 007970 ** 007971 ** Cause precompiled statements to expire. When an expired statement 007972 ** is executed using sqlite3_step() it will either automatically 007973 ** reprepare itself (if it was originally created using sqlite3_prepare_v2()) 007974 ** or it will fail with SQLITE_SCHEMA. 007975 ** 007976 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, 007977 ** then only the currently executing statement is expired. 007978 ** 007979 ** If P2 is 0, then SQL statements are expired immediately. If P2 is 1, 007980 ** then running SQL statements are allowed to continue to run to completion. 007981 ** The P2==1 case occurs when a CREATE INDEX or similar schema change happens 007982 ** that might help the statement run faster but which does not affect the 007983 ** correctness of operation. 007984 */ 007985 case OP_Expire: { 007986 assert( pOp->p2==0 || pOp->p2==1 ); 007987 if( !pOp->p1 ){ 007988 sqlite3ExpirePreparedStatements(db, pOp->p2); 007989 }else{ 007990 p->expired = pOp->p2+1; 007991 } 007992 break; 007993 } 007994 007995 /* Opcode: CursorLock P1 * * * * 007996 ** 007997 ** Lock the btree to which cursor P1 is pointing so that the btree cannot be 007998 ** written by an other cursor. 007999 */ 008000 case OP_CursorLock: { 008001 VdbeCursor *pC; 008002 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 008003 pC = p->apCsr[pOp->p1]; 008004 assert( pC!=0 ); 008005 assert( pC->eCurType==CURTYPE_BTREE ); 008006 sqlite3BtreeCursorPin(pC->uc.pCursor); 008007 break; 008008 } 008009 008010 /* Opcode: CursorUnlock P1 * * * * 008011 ** 008012 ** Unlock the btree to which cursor P1 is pointing so that it can be 008013 ** written by other cursors. 008014 */ 008015 case OP_CursorUnlock: { 008016 VdbeCursor *pC; 008017 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 008018 pC = p->apCsr[pOp->p1]; 008019 assert( pC!=0 ); 008020 assert( pC->eCurType==CURTYPE_BTREE ); 008021 sqlite3BtreeCursorUnpin(pC->uc.pCursor); 008022 break; 008023 } 008024 008025 #ifndef SQLITE_OMIT_SHARED_CACHE 008026 /* Opcode: TableLock P1 P2 P3 P4 * 008027 ** Synopsis: iDb=P1 root=P2 write=P3 008028 ** 008029 ** Obtain a lock on a particular table. This instruction is only used when 008030 ** the shared-cache feature is enabled. 008031 ** 008032 ** P1 is the index of the database in sqlite3.aDb[] of the database 008033 ** on which the lock is acquired. A readlock is obtained if P3==0 or 008034 ** a write lock if P3==1. 008035 ** 008036 ** P2 contains the root-page of the table to lock. 008037 ** 008038 ** P4 contains a pointer to the name of the table being locked. This is only 008039 ** used to generate an error message if the lock cannot be obtained. 008040 */ 008041 case OP_TableLock: { 008042 u8 isWriteLock = (u8)pOp->p3; 008043 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){ 008044 int p1 = pOp->p1; 008045 assert( p1>=0 && p1<db->nDb ); 008046 assert( DbMaskTest(p->btreeMask, p1) ); 008047 assert( isWriteLock==0 || isWriteLock==1 ); 008048 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); 008049 if( rc ){ 008050 if( (rc&0xFF)==SQLITE_LOCKED ){ 008051 const char *z = pOp->p4.z; 008052 sqlite3VdbeError(p, "database table is locked: %s", z); 008053 } 008054 goto abort_due_to_error; 008055 } 008056 } 008057 break; 008058 } 008059 #endif /* SQLITE_OMIT_SHARED_CACHE */ 008060 008061 #ifndef SQLITE_OMIT_VIRTUALTABLE 008062 /* Opcode: VBegin * * * P4 * 008063 ** 008064 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the 008065 ** xBegin method for that table. 008066 ** 008067 ** Also, whether or not P4 is set, check that this is not being called from 008068 ** within a callback to a virtual table xSync() method. If it is, the error 008069 ** code will be set to SQLITE_LOCKED. 008070 */ 008071 case OP_VBegin: { 008072 VTable *pVTab; 008073 pVTab = pOp->p4.pVtab; 008074 rc = sqlite3VtabBegin(db, pVTab); 008075 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); 008076 if( rc ) goto abort_due_to_error; 008077 break; 008078 } 008079 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 008080 008081 #ifndef SQLITE_OMIT_VIRTUALTABLE 008082 /* Opcode: VCreate P1 P2 * * * 008083 ** 008084 ** P2 is a register that holds the name of a virtual table in database 008085 ** P1. Call the xCreate method for that table. 008086 */ 008087 case OP_VCreate: { 008088 Mem sMem; /* For storing the record being decoded */ 008089 const char *zTab; /* Name of the virtual table */ 008090 008091 memset(&sMem, 0, sizeof(sMem)); 008092 sMem.db = db; 008093 /* Because P2 is always a static string, it is impossible for the 008094 ** sqlite3VdbeMemCopy() to fail */ 008095 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 ); 008096 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 ); 008097 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]); 008098 assert( rc==SQLITE_OK ); 008099 zTab = (const char*)sqlite3_value_text(&sMem); 008100 assert( zTab || db->mallocFailed ); 008101 if( zTab ){ 008102 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); 008103 } 008104 sqlite3VdbeMemRelease(&sMem); 008105 if( rc ) goto abort_due_to_error; 008106 break; 008107 } 008108 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 008109 008110 #ifndef SQLITE_OMIT_VIRTUALTABLE 008111 /* Opcode: VDestroy P1 * * P4 * 008112 ** 008113 ** P4 is the name of a virtual table in database P1. Call the xDestroy method 008114 ** of that table. 008115 */ 008116 case OP_VDestroy: { 008117 db->nVDestroy++; 008118 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); 008119 db->nVDestroy--; 008120 assert( p->errorAction==OE_Abort && p->usesStmtJournal ); 008121 if( rc ) goto abort_due_to_error; 008122 break; 008123 } 008124 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 008125 008126 #ifndef SQLITE_OMIT_VIRTUALTABLE 008127 /* Opcode: VOpen P1 * * P4 * 008128 ** 008129 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. 008130 ** P1 is a cursor number. This opcode opens a cursor to the virtual 008131 ** table and stores that cursor in P1. 008132 */ 008133 case OP_VOpen: { /* ncycle */ 008134 VdbeCursor *pCur; 008135 sqlite3_vtab_cursor *pVCur; 008136 sqlite3_vtab *pVtab; 008137 const sqlite3_module *pModule; 008138 008139 assert( p->bIsReader ); 008140 pCur = 0; 008141 pVCur = 0; 008142 pVtab = pOp->p4.pVtab->pVtab; 008143 if( pVtab==0 || NEVER(pVtab->pModule==0) ){ 008144 rc = SQLITE_LOCKED; 008145 goto abort_due_to_error; 008146 } 008147 pModule = pVtab->pModule; 008148 rc = pModule->xOpen(pVtab, &pVCur); 008149 sqlite3VtabImportErrmsg(p, pVtab); 008150 if( rc ) goto abort_due_to_error; 008151 008152 /* Initialize sqlite3_vtab_cursor base class */ 008153 pVCur->pVtab = pVtab; 008154 008155 /* Initialize vdbe cursor object */ 008156 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB); 008157 if( pCur ){ 008158 pCur->uc.pVCur = pVCur; 008159 pVtab->nRef++; 008160 }else{ 008161 assert( db->mallocFailed ); 008162 pModule->xClose(pVCur); 008163 goto no_mem; 008164 } 008165 break; 008166 } 008167 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 008168 008169 #ifndef SQLITE_OMIT_VIRTUALTABLE 008170 /* Opcode: VCheck P1 P2 P3 P4 * 008171 ** 008172 ** P4 is a pointer to a Table object that is a virtual table in schema P1 008173 ** that supports the xIntegrity() method. This opcode runs the xIntegrity() 008174 ** method for that virtual table, using P3 as the integer argument. If 008175 ** an error is reported back, the table name is prepended to the error 008176 ** message and that message is stored in P2. If no errors are seen, 008177 ** register P2 is set to NULL. 008178 */ 008179 case OP_VCheck: { /* out2 */ 008180 Table *pTab; 008181 sqlite3_vtab *pVtab; 008182 const sqlite3_module *pModule; 008183 char *zErr = 0; 008184 008185 pOut = &aMem[pOp->p2]; 008186 sqlite3VdbeMemSetNull(pOut); /* Innocent until proven guilty */ 008187 assert( pOp->p4type==P4_TABLEREF ); 008188 pTab = pOp->p4.pTab; 008189 assert( pTab!=0 ); 008190 assert( pTab->nTabRef>0 ); 008191 assert( IsVirtual(pTab) ); 008192 if( pTab->u.vtab.p==0 ) break; 008193 pVtab = pTab->u.vtab.p->pVtab; 008194 assert( pVtab!=0 ); 008195 pModule = pVtab->pModule; 008196 assert( pModule!=0 ); 008197 assert( pModule->iVersion>=4 ); 008198 assert( pModule->xIntegrity!=0 ); 008199 sqlite3VtabLock(pTab->u.vtab.p); 008200 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 008201 rc = pModule->xIntegrity(pVtab, db->aDb[pOp->p1].zDbSName, pTab->zName, 008202 pOp->p3, &zErr); 008203 sqlite3VtabUnlock(pTab->u.vtab.p); 008204 if( rc ){ 008205 sqlite3_free(zErr); 008206 goto abort_due_to_error; 008207 } 008208 if( zErr ){ 008209 sqlite3VdbeMemSetStr(pOut, zErr, -1, SQLITE_UTF8, sqlite3_free); 008210 } 008211 break; 008212 } 008213 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 008214 008215 #ifndef SQLITE_OMIT_VIRTUALTABLE 008216 /* Opcode: VInitIn P1 P2 P3 * * 008217 ** Synopsis: r[P2]=ValueList(P1,P3) 008218 ** 008219 ** Set register P2 to be a pointer to a ValueList object for cursor P1 008220 ** with cache register P3 and output register P3+1. This ValueList object 008221 ** can be used as the first argument to sqlite3_vtab_in_first() and 008222 ** sqlite3_vtab_in_next() to extract all of the values stored in the P1 008223 ** cursor. Register P3 is used to hold the values returned by 008224 ** sqlite3_vtab_in_first() and sqlite3_vtab_in_next(). 008225 */ 008226 case OP_VInitIn: { /* out2, ncycle */ 008227 VdbeCursor *pC; /* The cursor containing the RHS values */ 008228 ValueList *pRhs; /* New ValueList object to put in reg[P2] */ 008229 008230 pC = p->apCsr[pOp->p1]; 008231 pRhs = sqlite3_malloc64( sizeof(*pRhs) ); 008232 if( pRhs==0 ) goto no_mem; 008233 pRhs->pCsr = pC->uc.pCursor; 008234 pRhs->pOut = &aMem[pOp->p3]; 008235 pOut = out2Prerelease(p, pOp); 008236 pOut->flags = MEM_Null; 008237 sqlite3VdbeMemSetPointer(pOut, pRhs, "ValueList", sqlite3VdbeValueListFree); 008238 break; 008239 } 008240 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 008241 008242 008243 #ifndef SQLITE_OMIT_VIRTUALTABLE 008244 /* Opcode: VFilter P1 P2 P3 P4 * 008245 ** Synopsis: iplan=r[P3] zplan='P4' 008246 ** 008247 ** P1 is a cursor opened using VOpen. P2 is an address to jump to if 008248 ** the filtered result set is empty. 008249 ** 008250 ** P4 is either NULL or a string that was generated by the xBestIndex 008251 ** method of the module. The interpretation of the P4 string is left 008252 ** to the module implementation. 008253 ** 008254 ** This opcode invokes the xFilter method on the virtual table specified 008255 ** by P1. The integer query plan parameter to xFilter is stored in register 008256 ** P3. Register P3+1 stores the argc parameter to be passed to the 008257 ** xFilter method. Registers P3+2..P3+1+argc are the argc 008258 ** additional parameters which are passed to 008259 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. 008260 ** 008261 ** A jump is made to P2 if the result set after filtering would be empty. 008262 */ 008263 case OP_VFilter: { /* jump, ncycle */ 008264 int nArg; 008265 int iQuery; 008266 const sqlite3_module *pModule; 008267 Mem *pQuery; 008268 Mem *pArgc; 008269 sqlite3_vtab_cursor *pVCur; 008270 sqlite3_vtab *pVtab; 008271 VdbeCursor *pCur; 008272 int res; 008273 int i; 008274 Mem **apArg; 008275 008276 pQuery = &aMem[pOp->p3]; 008277 pArgc = &pQuery[1]; 008278 pCur = p->apCsr[pOp->p1]; 008279 assert( memIsValid(pQuery) ); 008280 REGISTER_TRACE(pOp->p3, pQuery); 008281 assert( pCur!=0 ); 008282 assert( pCur->eCurType==CURTYPE_VTAB ); 008283 pVCur = pCur->uc.pVCur; 008284 pVtab = pVCur->pVtab; 008285 pModule = pVtab->pModule; 008286 008287 /* Grab the index number and argc parameters */ 008288 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); 008289 nArg = (int)pArgc->u.i; 008290 iQuery = (int)pQuery->u.i; 008291 008292 /* Invoke the xFilter method */ 008293 apArg = p->apArg; 008294 for(i = 0; i<nArg; i++){ 008295 apArg[i] = &pArgc[i+1]; 008296 } 008297 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg); 008298 sqlite3VtabImportErrmsg(p, pVtab); 008299 if( rc ) goto abort_due_to_error; 008300 res = pModule->xEof(pVCur); 008301 pCur->nullRow = 0; 008302 VdbeBranchTaken(res!=0,2); 008303 if( res ) goto jump_to_p2; 008304 break; 008305 } 008306 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 008307 008308 #ifndef SQLITE_OMIT_VIRTUALTABLE 008309 /* Opcode: VColumn P1 P2 P3 * P5 008310 ** Synopsis: r[P3]=vcolumn(P2) 008311 ** 008312 ** Store in register P3 the value of the P2-th column of 008313 ** the current row of the virtual-table of cursor P1. 008314 ** 008315 ** If the VColumn opcode is being used to fetch the value of 008316 ** an unchanging column during an UPDATE operation, then the P5 008317 ** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange() 008318 ** function to return true inside the xColumn method of the virtual 008319 ** table implementation. The P5 column might also contain other 008320 ** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are 008321 ** unused by OP_VColumn. 008322 */ 008323 case OP_VColumn: { /* ncycle */ 008324 sqlite3_vtab *pVtab; 008325 const sqlite3_module *pModule; 008326 Mem *pDest; 008327 sqlite3_context sContext; 008328 FuncDef nullFunc; 008329 008330 VdbeCursor *pCur = p->apCsr[pOp->p1]; 008331 assert( pCur!=0 ); 008332 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 008333 pDest = &aMem[pOp->p3]; 008334 memAboutToChange(p, pDest); 008335 if( pCur->nullRow ){ 008336 sqlite3VdbeMemSetNull(pDest); 008337 break; 008338 } 008339 assert( pCur->eCurType==CURTYPE_VTAB ); 008340 pVtab = pCur->uc.pVCur->pVtab; 008341 pModule = pVtab->pModule; 008342 assert( pModule->xColumn ); 008343 memset(&sContext, 0, sizeof(sContext)); 008344 sContext.pOut = pDest; 008345 sContext.enc = encoding; 008346 nullFunc.pUserData = 0; 008347 nullFunc.funcFlags = SQLITE_RESULT_SUBTYPE; 008348 sContext.pFunc = &nullFunc; 008349 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 ); 008350 if( pOp->p5 & OPFLAG_NOCHNG ){ 008351 sqlite3VdbeMemSetNull(pDest); 008352 pDest->flags = MEM_Null|MEM_Zero; 008353 pDest->u.nZero = 0; 008354 }else{ 008355 MemSetTypeFlag(pDest, MEM_Null); 008356 } 008357 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2); 008358 sqlite3VtabImportErrmsg(p, pVtab); 008359 if( sContext.isError>0 ){ 008360 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest)); 008361 rc = sContext.isError; 008362 } 008363 sqlite3VdbeChangeEncoding(pDest, encoding); 008364 REGISTER_TRACE(pOp->p3, pDest); 008365 UPDATE_MAX_BLOBSIZE(pDest); 008366 008367 if( rc ) goto abort_due_to_error; 008368 break; 008369 } 008370 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 008371 008372 #ifndef SQLITE_OMIT_VIRTUALTABLE 008373 /* Opcode: VNext P1 P2 * * * 008374 ** 008375 ** Advance virtual table P1 to the next row in its result set and 008376 ** jump to instruction P2. Or, if the virtual table has reached 008377 ** the end of its result set, then fall through to the next instruction. 008378 */ 008379 case OP_VNext: { /* jump, ncycle */ 008380 sqlite3_vtab *pVtab; 008381 const sqlite3_module *pModule; 008382 int res; 008383 VdbeCursor *pCur; 008384 008385 pCur = p->apCsr[pOp->p1]; 008386 assert( pCur!=0 ); 008387 assert( pCur->eCurType==CURTYPE_VTAB ); 008388 if( pCur->nullRow ){ 008389 break; 008390 } 008391 pVtab = pCur->uc.pVCur->pVtab; 008392 pModule = pVtab->pModule; 008393 assert( pModule->xNext ); 008394 008395 /* Invoke the xNext() method of the module. There is no way for the 008396 ** underlying implementation to return an error if one occurs during 008397 ** xNext(). Instead, if an error occurs, true is returned (indicating that 008398 ** data is available) and the error code returned when xColumn or 008399 ** some other method is next invoked on the save virtual table cursor. 008400 */ 008401 rc = pModule->xNext(pCur->uc.pVCur); 008402 sqlite3VtabImportErrmsg(p, pVtab); 008403 if( rc ) goto abort_due_to_error; 008404 res = pModule->xEof(pCur->uc.pVCur); 008405 VdbeBranchTaken(!res,2); 008406 if( !res ){ 008407 /* If there is data, jump to P2 */ 008408 goto jump_to_p2_and_check_for_interrupt; 008409 } 008410 goto check_for_interrupt; 008411 } 008412 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 008413 008414 #ifndef SQLITE_OMIT_VIRTUALTABLE 008415 /* Opcode: VRename P1 * * P4 * 008416 ** 008417 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. 008418 ** This opcode invokes the corresponding xRename method. The value 008419 ** in register P1 is passed as the zName argument to the xRename method. 008420 */ 008421 case OP_VRename: { 008422 sqlite3_vtab *pVtab; 008423 Mem *pName; 008424 int isLegacy; 008425 008426 isLegacy = (db->flags & SQLITE_LegacyAlter); 008427 db->flags |= SQLITE_LegacyAlter; 008428 pVtab = pOp->p4.pVtab->pVtab; 008429 pName = &aMem[pOp->p1]; 008430 assert( pVtab->pModule->xRename ); 008431 assert( memIsValid(pName) ); 008432 assert( p->readOnly==0 ); 008433 REGISTER_TRACE(pOp->p1, pName); 008434 assert( pName->flags & MEM_Str ); 008435 testcase( pName->enc==SQLITE_UTF8 ); 008436 testcase( pName->enc==SQLITE_UTF16BE ); 008437 testcase( pName->enc==SQLITE_UTF16LE ); 008438 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); 008439 if( rc ) goto abort_due_to_error; 008440 rc = pVtab->pModule->xRename(pVtab, pName->z); 008441 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter; 008442 sqlite3VtabImportErrmsg(p, pVtab); 008443 p->expired = 0; 008444 if( rc ) goto abort_due_to_error; 008445 break; 008446 } 008447 #endif 008448 008449 #ifndef SQLITE_OMIT_VIRTUALTABLE 008450 /* Opcode: VUpdate P1 P2 P3 P4 P5 008451 ** Synopsis: data=r[P3@P2] 008452 ** 008453 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. 008454 ** This opcode invokes the corresponding xUpdate method. P2 values 008455 ** are contiguous memory cells starting at P3 to pass to the xUpdate 008456 ** invocation. The value in register (P3+P2-1) corresponds to the 008457 ** p2th element of the argv array passed to xUpdate. 008458 ** 008459 ** The xUpdate method will do a DELETE or an INSERT or both. 008460 ** The argv[0] element (which corresponds to memory cell P3) 008461 ** is the rowid of a row to delete. If argv[0] is NULL then no 008462 ** deletion occurs. The argv[1] element is the rowid of the new 008463 ** row. This can be NULL to have the virtual table select the new 008464 ** rowid for itself. The subsequent elements in the array are 008465 ** the values of columns in the new row. 008466 ** 008467 ** If P2==1 then no insert is performed. argv[0] is the rowid of 008468 ** a row to delete. 008469 ** 008470 ** P1 is a boolean flag. If it is set to true and the xUpdate call 008471 ** is successful, then the value returned by sqlite3_last_insert_rowid() 008472 ** is set to the value of the rowid for the row just inserted. 008473 ** 008474 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to 008475 ** apply in the case of a constraint failure on an insert or update. 008476 */ 008477 case OP_VUpdate: { 008478 sqlite3_vtab *pVtab; 008479 const sqlite3_module *pModule; 008480 int nArg; 008481 int i; 008482 sqlite_int64 rowid = 0; 008483 Mem **apArg; 008484 Mem *pX; 008485 008486 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback 008487 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace 008488 ); 008489 assert( p->readOnly==0 ); 008490 if( db->mallocFailed ) goto no_mem; 008491 sqlite3VdbeIncrWriteCounter(p, 0); 008492 pVtab = pOp->p4.pVtab->pVtab; 008493 if( pVtab==0 || NEVER(pVtab->pModule==0) ){ 008494 rc = SQLITE_LOCKED; 008495 goto abort_due_to_error; 008496 } 008497 pModule = pVtab->pModule; 008498 nArg = pOp->p2; 008499 assert( pOp->p4type==P4_VTAB ); 008500 if( ALWAYS(pModule->xUpdate) ){ 008501 u8 vtabOnConflict = db->vtabOnConflict; 008502 apArg = p->apArg; 008503 pX = &aMem[pOp->p3]; 008504 for(i=0; i<nArg; i++){ 008505 assert( memIsValid(pX) ); 008506 memAboutToChange(p, pX); 008507 apArg[i] = pX; 008508 pX++; 008509 } 008510 db->vtabOnConflict = pOp->p5; 008511 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); 008512 db->vtabOnConflict = vtabOnConflict; 008513 sqlite3VtabImportErrmsg(p, pVtab); 008514 if( rc==SQLITE_OK && pOp->p1 ){ 008515 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); 008516 db->lastRowid = rowid; 008517 } 008518 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ 008519 if( pOp->p5==OE_Ignore ){ 008520 rc = SQLITE_OK; 008521 }else{ 008522 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); 008523 } 008524 }else{ 008525 p->nChange++; 008526 } 008527 if( rc ) goto abort_due_to_error; 008528 } 008529 break; 008530 } 008531 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 008532 008533 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 008534 /* Opcode: Pagecount P1 P2 * * * 008535 ** 008536 ** Write the current number of pages in database P1 to memory cell P2. 008537 */ 008538 case OP_Pagecount: { /* out2 */ 008539 pOut = out2Prerelease(p, pOp); 008540 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); 008541 break; 008542 } 008543 #endif 008544 008545 008546 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 008547 /* Opcode: MaxPgcnt P1 P2 P3 * * 008548 ** 008549 ** Try to set the maximum page count for database P1 to the value in P3. 008550 ** Do not let the maximum page count fall below the current page count and 008551 ** do not change the maximum page count value if P3==0. 008552 ** 008553 ** Store the maximum page count after the change in register P2. 008554 */ 008555 case OP_MaxPgcnt: { /* out2 */ 008556 unsigned int newMax; 008557 Btree *pBt; 008558 008559 pOut = out2Prerelease(p, pOp); 008560 pBt = db->aDb[pOp->p1].pBt; 008561 newMax = 0; 008562 if( pOp->p3 ){ 008563 newMax = sqlite3BtreeLastPage(pBt); 008564 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; 008565 } 008566 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); 008567 break; 008568 } 008569 #endif 008570 008571 /* Opcode: Function P1 P2 P3 P4 * 008572 ** Synopsis: r[P3]=func(r[P2@NP]) 008573 ** 008574 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that 008575 ** contains a pointer to the function to be run) with arguments taken 008576 ** from register P2 and successors. The number of arguments is in 008577 ** the sqlite3_context object that P4 points to. 008578 ** The result of the function is stored 008579 ** in register P3. Register P3 must not be one of the function inputs. 008580 ** 008581 ** P1 is a 32-bit bitmask indicating whether or not each argument to the 008582 ** function was determined to be constant at compile time. If the first 008583 ** argument was constant then bit 0 of P1 is set. This is used to determine 008584 ** whether meta data associated with a user function argument using the 008585 ** sqlite3_set_auxdata() API may be safely retained until the next 008586 ** invocation of this opcode. 008587 ** 008588 ** See also: AggStep, AggFinal, PureFunc 008589 */ 008590 /* Opcode: PureFunc P1 P2 P3 P4 * 008591 ** Synopsis: r[P3]=func(r[P2@NP]) 008592 ** 008593 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that 008594 ** contains a pointer to the function to be run) with arguments taken 008595 ** from register P2 and successors. The number of arguments is in 008596 ** the sqlite3_context object that P4 points to. 008597 ** The result of the function is stored 008598 ** in register P3. Register P3 must not be one of the function inputs. 008599 ** 008600 ** P1 is a 32-bit bitmask indicating whether or not each argument to the 008601 ** function was determined to be constant at compile time. If the first 008602 ** argument was constant then bit 0 of P1 is set. This is used to determine 008603 ** whether meta data associated with a user function argument using the 008604 ** sqlite3_set_auxdata() API may be safely retained until the next 008605 ** invocation of this opcode. 008606 ** 008607 ** This opcode works exactly like OP_Function. The only difference is in 008608 ** its name. This opcode is used in places where the function must be 008609 ** purely non-deterministic. Some built-in date/time functions can be 008610 ** either deterministic of non-deterministic, depending on their arguments. 008611 ** When those function are used in a non-deterministic way, they will check 008612 ** to see if they were called using OP_PureFunc instead of OP_Function, and 008613 ** if they were, they throw an error. 008614 ** 008615 ** See also: AggStep, AggFinal, Function 008616 */ 008617 case OP_PureFunc: /* group */ 008618 case OP_Function: { /* group */ 008619 int i; 008620 sqlite3_context *pCtx; 008621 008622 assert( pOp->p4type==P4_FUNCCTX ); 008623 pCtx = pOp->p4.pCtx; 008624 008625 /* If this function is inside of a trigger, the register array in aMem[] 008626 ** might change from one evaluation to the next. The next block of code 008627 ** checks to see if the register array has changed, and if so it 008628 ** reinitializes the relevant parts of the sqlite3_context object */ 008629 pOut = &aMem[pOp->p3]; 008630 if( pCtx->pOut != pOut ){ 008631 pCtx->pVdbe = p; 008632 pCtx->pOut = pOut; 008633 pCtx->enc = encoding; 008634 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; 008635 } 008636 assert( pCtx->pVdbe==p ); 008637 008638 memAboutToChange(p, pOut); 008639 #ifdef SQLITE_DEBUG 008640 for(i=0; i<pCtx->argc; i++){ 008641 assert( memIsValid(pCtx->argv[i]) ); 008642 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); 008643 } 008644 #endif 008645 MemSetTypeFlag(pOut, MEM_Null); 008646 assert( pCtx->isError==0 ); 008647 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ 008648 008649 /* If the function returned an error, throw an exception */ 008650 if( pCtx->isError ){ 008651 if( pCtx->isError>0 ){ 008652 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut)); 008653 rc = pCtx->isError; 008654 } 008655 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1); 008656 pCtx->isError = 0; 008657 if( rc ) goto abort_due_to_error; 008658 } 008659 008660 assert( (pOut->flags&MEM_Str)==0 008661 || pOut->enc==encoding 008662 || db->mallocFailed ); 008663 assert( !sqlite3VdbeMemTooBig(pOut) ); 008664 008665 REGISTER_TRACE(pOp->p3, pOut); 008666 UPDATE_MAX_BLOBSIZE(pOut); 008667 break; 008668 } 008669 008670 /* Opcode: ClrSubtype P1 * * * * 008671 ** Synopsis: r[P1].subtype = 0 008672 ** 008673 ** Clear the subtype from register P1. 008674 */ 008675 case OP_ClrSubtype: { /* in1 */ 008676 pIn1 = &aMem[pOp->p1]; 008677 pIn1->flags &= ~MEM_Subtype; 008678 break; 008679 } 008680 008681 /* Opcode: GetSubtype P1 P2 * * * 008682 ** Synopsis: r[P2] = r[P1].subtype 008683 ** 008684 ** Extract the subtype value from register P1 and write that subtype 008685 ** into register P2. If P1 has no subtype, then P1 gets a NULL. 008686 */ 008687 case OP_GetSubtype: { /* in1 out2 */ 008688 pIn1 = &aMem[pOp->p1]; 008689 pOut = &aMem[pOp->p2]; 008690 if( pIn1->flags & MEM_Subtype ){ 008691 sqlite3VdbeMemSetInt64(pOut, pIn1->eSubtype); 008692 }else{ 008693 sqlite3VdbeMemSetNull(pOut); 008694 } 008695 break; 008696 } 008697 008698 /* Opcode: SetSubtype P1 P2 * * * 008699 ** Synopsis: r[P2].subtype = r[P1] 008700 ** 008701 ** Set the subtype value of register P2 to the integer from register P1. 008702 ** If P1 is NULL, clear the subtype from p2. 008703 */ 008704 case OP_SetSubtype: { /* in1 out2 */ 008705 pIn1 = &aMem[pOp->p1]; 008706 pOut = &aMem[pOp->p2]; 008707 if( pIn1->flags & MEM_Null ){ 008708 pOut->flags &= ~MEM_Subtype; 008709 }else{ 008710 assert( pIn1->flags & MEM_Int ); 008711 pOut->flags |= MEM_Subtype; 008712 pOut->eSubtype = (u8)(pIn1->u.i & 0xff); 008713 } 008714 break; 008715 } 008716 008717 /* Opcode: FilterAdd P1 * P3 P4 * 008718 ** Synopsis: filter(P1) += key(P3@P4) 008719 ** 008720 ** Compute a hash on the P4 registers starting with r[P3] and 008721 ** add that hash to the bloom filter contained in r[P1]. 008722 */ 008723 case OP_FilterAdd: { 008724 u64 h; 008725 008726 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 008727 pIn1 = &aMem[pOp->p1]; 008728 assert( pIn1->flags & MEM_Blob ); 008729 assert( pIn1->n>0 ); 008730 h = filterHash(aMem, pOp); 008731 #ifdef SQLITE_DEBUG 008732 if( db->flags&SQLITE_VdbeTrace ){ 008733 int ii; 008734 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){ 008735 registerTrace(ii, &aMem[ii]); 008736 } 008737 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n)); 008738 } 008739 #endif 008740 h %= (pIn1->n*8); 008741 pIn1->z[h/8] |= 1<<(h&7); 008742 break; 008743 } 008744 008745 /* Opcode: Filter P1 P2 P3 P4 * 008746 ** Synopsis: if key(P3@P4) not in filter(P1) goto P2 008747 ** 008748 ** Compute a hash on the key contained in the P4 registers starting 008749 ** with r[P3]. Check to see if that hash is found in the 008750 ** bloom filter hosted by register P1. If it is not present then 008751 ** maybe jump to P2. Otherwise fall through. 008752 ** 008753 ** False negatives are harmless. It is always safe to fall through, 008754 ** even if the value is in the bloom filter. A false negative causes 008755 ** more CPU cycles to be used, but it should still yield the correct 008756 ** answer. However, an incorrect answer may well arise from a 008757 ** false positive - if the jump is taken when it should fall through. 008758 */ 008759 case OP_Filter: { /* jump */ 008760 u64 h; 008761 008762 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 008763 pIn1 = &aMem[pOp->p1]; 008764 assert( (pIn1->flags & MEM_Blob)!=0 ); 008765 assert( pIn1->n >= 1 ); 008766 h = filterHash(aMem, pOp); 008767 #ifdef SQLITE_DEBUG 008768 if( db->flags&SQLITE_VdbeTrace ){ 008769 int ii; 008770 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){ 008771 registerTrace(ii, &aMem[ii]); 008772 } 008773 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n)); 008774 } 008775 #endif 008776 h %= (pIn1->n*8); 008777 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){ 008778 VdbeBranchTaken(1, 2); 008779 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++; 008780 goto jump_to_p2; 008781 }else{ 008782 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++; 008783 VdbeBranchTaken(0, 2); 008784 } 008785 break; 008786 } 008787 008788 /* Opcode: Trace P1 P2 * P4 * 008789 ** 008790 ** Write P4 on the statement trace output if statement tracing is 008791 ** enabled. 008792 ** 008793 ** Operand P1 must be 0x7fffffff and P2 must positive. 008794 */ 008795 /* Opcode: Init P1 P2 P3 P4 * 008796 ** Synopsis: Start at P2 008797 ** 008798 ** Programs contain a single instance of this opcode as the very first 008799 ** opcode. 008800 ** 008801 ** If tracing is enabled (by the sqlite3_trace()) interface, then 008802 ** the UTF-8 string contained in P4 is emitted on the trace callback. 008803 ** Or if P4 is blank, use the string returned by sqlite3_sql(). 008804 ** 008805 ** If P2 is not zero, jump to instruction P2. 008806 ** 008807 ** Increment the value of P1 so that OP_Once opcodes will jump the 008808 ** first time they are evaluated for this run. 008809 ** 008810 ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT 008811 ** error is encountered. 008812 */ 008813 case OP_Trace: 008814 case OP_Init: { /* jump */ 008815 int i; 008816 #ifndef SQLITE_OMIT_TRACE 008817 char *zTrace; 008818 #endif 008819 008820 /* If the P4 argument is not NULL, then it must be an SQL comment string. 008821 ** The "--" string is broken up to prevent false-positives with srcck1.c. 008822 ** 008823 ** This assert() provides evidence for: 008824 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that 008825 ** would have been returned by the legacy sqlite3_trace() interface by 008826 ** using the X argument when X begins with "--" and invoking 008827 ** sqlite3_expanded_sql(P) otherwise. 008828 */ 008829 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 ); 008830 008831 /* OP_Init is always instruction 0 */ 008832 assert( pOp==p->aOp || pOp->opcode==OP_Trace ); 008833 008834 #ifndef SQLITE_OMIT_TRACE 008835 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0 008836 && p->minWriteFileFormat!=254 /* tag-20220401a */ 008837 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 008838 ){ 008839 #ifndef SQLITE_OMIT_DEPRECATED 008840 if( db->mTrace & SQLITE_TRACE_LEGACY ){ 008841 char *z = sqlite3VdbeExpandSql(p, zTrace); 008842 db->trace.xLegacy(db->pTraceArg, z); 008843 sqlite3_free(z); 008844 }else 008845 #endif 008846 if( db->nVdbeExec>1 ){ 008847 char *z = sqlite3MPrintf(db, "-- %s", zTrace); 008848 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z); 008849 sqlite3DbFree(db, z); 008850 }else{ 008851 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace); 008852 } 008853 } 008854 #ifdef SQLITE_USE_FCNTL_TRACE 008855 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); 008856 if( zTrace ){ 008857 int j; 008858 for(j=0; j<db->nDb; j++){ 008859 if( DbMaskTest(p->btreeMask, j)==0 ) continue; 008860 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace); 008861 } 008862 } 008863 #endif /* SQLITE_USE_FCNTL_TRACE */ 008864 #ifdef SQLITE_DEBUG 008865 if( (db->flags & SQLITE_SqlTrace)!=0 008866 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 008867 ){ 008868 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); 008869 } 008870 #endif /* SQLITE_DEBUG */ 008871 #endif /* SQLITE_OMIT_TRACE */ 008872 assert( pOp->p2>0 ); 008873 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){ 008874 if( pOp->opcode==OP_Trace ) break; 008875 for(i=1; i<p->nOp; i++){ 008876 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0; 008877 } 008878 pOp->p1 = 0; 008879 } 008880 pOp->p1++; 008881 p->aCounter[SQLITE_STMTSTATUS_RUN]++; 008882 goto jump_to_p2; 008883 } 008884 008885 #ifdef SQLITE_ENABLE_CURSOR_HINTS 008886 /* Opcode: CursorHint P1 * * P4 * 008887 ** 008888 ** Provide a hint to cursor P1 that it only needs to return rows that 008889 ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer 008890 ** to values currently held in registers. TK_COLUMN terms in the P4 008891 ** expression refer to columns in the b-tree to which cursor P1 is pointing. 008892 */ 008893 case OP_CursorHint: { 008894 VdbeCursor *pC; 008895 008896 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 008897 assert( pOp->p4type==P4_EXPR ); 008898 pC = p->apCsr[pOp->p1]; 008899 if( pC ){ 008900 assert( pC->eCurType==CURTYPE_BTREE ); 008901 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE, 008902 pOp->p4.pExpr, aMem); 008903 } 008904 break; 008905 } 008906 #endif /* SQLITE_ENABLE_CURSOR_HINTS */ 008907 008908 #ifdef SQLITE_DEBUG 008909 /* Opcode: Abortable * * * * * 008910 ** 008911 ** Verify that an Abort can happen. Assert if an Abort at this point 008912 ** might cause database corruption. This opcode only appears in debugging 008913 ** builds. 008914 ** 008915 ** An Abort is safe if either there have been no writes, or if there is 008916 ** an active statement journal. 008917 */ 008918 case OP_Abortable: { 008919 sqlite3VdbeAssertAbortable(p); 008920 break; 008921 } 008922 #endif 008923 008924 #ifdef SQLITE_DEBUG 008925 /* Opcode: ReleaseReg P1 P2 P3 * P5 008926 ** Synopsis: release r[P1@P2] mask P3 008927 ** 008928 ** Release registers from service. Any content that was in the 008929 ** the registers is unreliable after this opcode completes. 008930 ** 008931 ** The registers released will be the P2 registers starting at P1, 008932 ** except if bit ii of P3 set, then do not release register P1+ii. 008933 ** In other words, P3 is a mask of registers to preserve. 008934 ** 008935 ** Releasing a register clears the Mem.pScopyFrom pointer. That means 008936 ** that if the content of the released register was set using OP_SCopy, 008937 ** a change to the value of the source register for the OP_SCopy will no longer 008938 ** generate an assertion fault in sqlite3VdbeMemAboutToChange(). 008939 ** 008940 ** If P5 is set, then all released registers have their type set 008941 ** to MEM_Undefined so that any subsequent attempt to read the released 008942 ** register (before it is reinitialized) will generate an assertion fault. 008943 ** 008944 ** P5 ought to be set on every call to this opcode. 008945 ** However, there are places in the code generator will release registers 008946 ** before their are used, under the (valid) assumption that the registers 008947 ** will not be reallocated for some other purpose before they are used and 008948 ** hence are safe to release. 008949 ** 008950 ** This opcode is only available in testing and debugging builds. It is 008951 ** not generated for release builds. The purpose of this opcode is to help 008952 ** validate the generated bytecode. This opcode does not actually contribute 008953 ** to computing an answer. 008954 */ 008955 case OP_ReleaseReg: { 008956 Mem *pMem; 008957 int i; 008958 u32 constMask; 008959 assert( pOp->p1>0 ); 008960 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); 008961 pMem = &aMem[pOp->p1]; 008962 constMask = pOp->p3; 008963 for(i=0; i<pOp->p2; i++, pMem++){ 008964 if( i>=32 || (constMask & MASKBIT32(i))==0 ){ 008965 pMem->pScopyFrom = 0; 008966 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined); 008967 } 008968 } 008969 break; 008970 } 008971 #endif 008972 008973 /* Opcode: Noop * * * * * 008974 ** 008975 ** Do nothing. This instruction is often useful as a jump 008976 ** destination. 008977 */ 008978 /* 008979 ** The magic Explain opcode are only inserted when explain==2 (which 008980 ** is to say when the EXPLAIN QUERY PLAN syntax is used.) 008981 ** This opcode records information from the optimizer. It is the 008982 ** the same as a no-op. This opcodesnever appears in a real VM program. 008983 */ 008984 default: { /* This is really OP_Noop, OP_Explain */ 008985 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); 008986 008987 break; 008988 } 008989 008990 /***************************************************************************** 008991 ** The cases of the switch statement above this line should all be indented 008992 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the 008993 ** readability. From this point on down, the normal indentation rules are 008994 ** restored. 008995 *****************************************************************************/ 008996 } 008997 008998 #if defined(VDBE_PROFILE) 008999 *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); 009000 pnCycle = 0; 009001 #elif defined(SQLITE_ENABLE_STMT_SCANSTATUS) 009002 if( pnCycle ){ 009003 *pnCycle += sqlite3Hwtime(); 009004 pnCycle = 0; 009005 } 009006 #endif 009007 009008 /* The following code adds nothing to the actual functionality 009009 ** of the program. It is only here for testing and debugging. 009010 ** On the other hand, it does burn CPU cycles every time through 009011 ** the evaluator loop. So we can leave it out when NDEBUG is defined. 009012 */ 009013 #ifndef NDEBUG 009014 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] ); 009015 009016 #ifdef SQLITE_DEBUG 009017 if( db->flags & SQLITE_VdbeTrace ){ 009018 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode]; 009019 if( rc!=0 ) printf("rc=%d\n",rc); 009020 if( opProperty & (OPFLG_OUT2) ){ 009021 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); 009022 } 009023 if( opProperty & OPFLG_OUT3 ){ 009024 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); 009025 } 009026 if( opProperty==0xff ){ 009027 /* Never happens. This code exists to avoid a harmless linkage 009028 ** warning about sqlite3VdbeRegisterDump() being defined but not 009029 ** used. */ 009030 sqlite3VdbeRegisterDump(p); 009031 } 009032 } 009033 #endif /* SQLITE_DEBUG */ 009034 #endif /* NDEBUG */ 009035 } /* The end of the for(;;) loop the loops through opcodes */ 009036 009037 /* If we reach this point, it means that execution is finished with 009038 ** an error of some kind. 009039 */ 009040 abort_due_to_error: 009041 if( db->mallocFailed ){ 009042 rc = SQLITE_NOMEM_BKPT; 009043 }else if( rc==SQLITE_IOERR_CORRUPTFS ){ 009044 rc = SQLITE_CORRUPT_BKPT; 009045 } 009046 assert( rc ); 009047 #ifdef SQLITE_DEBUG 009048 if( db->flags & SQLITE_VdbeTrace ){ 009049 const char *zTrace = p->zSql; 009050 if( zTrace==0 ){ 009051 if( aOp[0].opcode==OP_Trace ){ 009052 zTrace = aOp[0].p4.z; 009053 } 009054 if( zTrace==0 ) zTrace = "???"; 009055 } 009056 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace); 009057 } 009058 #endif 009059 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){ 009060 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); 009061 } 009062 p->rc = rc; 009063 sqlite3SystemError(db, rc); 009064 testcase( sqlite3GlobalConfig.xLog!=0 ); 009065 sqlite3_log(rc, "statement aborts at %d: [%s] %s", 009066 (int)(pOp - aOp), p->zSql, p->zErrMsg); 009067 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p); 009068 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db); 009069 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){ 009070 db->flags |= SQLITE_CorruptRdOnly; 009071 } 009072 rc = SQLITE_ERROR; 009073 if( resetSchemaOnFault>0 ){ 009074 sqlite3ResetOneSchema(db, resetSchemaOnFault-1); 009075 } 009076 009077 /* This is the only way out of this procedure. We have to 009078 ** release the mutexes on btrees that were acquired at the 009079 ** top. */ 009080 vdbe_return: 009081 #if defined(VDBE_PROFILE) 009082 if( pnCycle ){ 009083 *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); 009084 pnCycle = 0; 009085 } 009086 #elif defined(SQLITE_ENABLE_STMT_SCANSTATUS) 009087 if( pnCycle ){ 009088 *pnCycle += sqlite3Hwtime(); 009089 pnCycle = 0; 009090 } 009091 #endif 009092 009093 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 009094 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){ 009095 nProgressLimit += db->nProgressOps; 009096 if( db->xProgress(db->pProgressArg) ){ 009097 nProgressLimit = LARGEST_UINT64; 009098 rc = SQLITE_INTERRUPT; 009099 goto abort_due_to_error; 009100 } 009101 } 009102 #endif 009103 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; 009104 if( DbMaskNonZero(p->lockMask) ){ 009105 sqlite3VdbeLeave(p); 009106 } 009107 assert( rc!=SQLITE_OK || nExtraDelete==0 009108 || sqlite3_strlike("DELETE%",p->zSql,0)!=0 009109 ); 009110 return rc; 009111 009112 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH 009113 ** is encountered. 009114 */ 009115 too_big: 009116 sqlite3VdbeError(p, "string or blob too big"); 009117 rc = SQLITE_TOOBIG; 009118 goto abort_due_to_error; 009119 009120 /* Jump to here if a malloc() fails. 009121 */ 009122 no_mem: 009123 sqlite3OomFault(db); 009124 sqlite3VdbeError(p, "out of memory"); 009125 rc = SQLITE_NOMEM_BKPT; 009126 goto abort_due_to_error; 009127 009128 /* Jump to here if the sqlite3_interrupt() API sets the interrupt 009129 ** flag. 009130 */ 009131 abort_due_to_interrupt: 009132 assert( AtomicLoad(&db->u1.isInterrupted) ); 009133 rc = SQLITE_INTERRUPT; 009134 goto abort_due_to_error; 009135 }