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