Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/src/vdbe.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / src / vdbe.c` 9658 lines ยท 328620 bytes Raw Blame History 1 /* 2 ** 2001 September 15 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** The code in this file implements the function that runs the 13 ** bytecode of a prepared statement. 14 ** 15 ** Various scripts scan this source file in order to generate HTML 16 ** documentation, headers files, or other derived files. The formatting 17 ** of the code in this file is, therefore, important. See other comments 18 ** in this file for details. If in doubt, do not deviate from existing 19 ** commenting and indentation practices when changing or adding code. 20 */ 21 #include "sqliteInt.h" 22 #include "vdbeInt.h" 23 24 /* 25 ** High-resolution hardware timer used for debugging and testing only. 26 */ 27 #if defined(VDBE_PROFILE) \ 28 || defined(SQLITE_PERFORMANCE_TRACE) \ 29 || defined(SQLITE_ENABLE_STMT_SCANSTATUS) 30 # include "hwtime.h" 31 #endif 32 33 /* 34 ** Invoke this macro on memory cells just prior to changing the 35 ** value of the cell. This macro verifies that shallow copies are 36 ** not misused. A shallow copy of a string or blob just copies a 37 ** pointer to the string or blob, not the content. If the original 38 ** is changed while the copy is still in use, the string or blob might 39 ** be changed out from under the copy. This macro verifies that nothing 40 ** like that ever happens. 41 */ 42 #ifdef SQLITE_DEBUG 43 # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M) 44 #else 45 # define memAboutToChange(P,M) 46 #endif 47 48 /* 49 ** The following global variable is incremented every time a cursor 50 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test 51 ** procedures use this information to make sure that indices are 52 ** working correctly. This variable has no function other than to 53 ** help verify the correct operation of the library. 54 */ 55 #ifdef SQLITE_TEST 56 int sqlite3_search_count = 0; 57 #endif 58 59 /* 60 ** When this global variable is positive, it gets decremented once before 61 ** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted 62 ** field of the sqlite3 structure is set in order to simulate an interrupt. 63 ** 64 ** This facility is used for testing purposes only. It does not function 65 ** in an ordinary build. 66 */ 67 #ifdef SQLITE_TEST 68 int sqlite3_interrupt_count = 0; 69 #endif 70 71 /* 72 ** The next global variable is incremented each type the OP_Sort opcode 73 ** is executed. The test procedures use this information to make sure that 74 ** sorting is occurring or not occurring at appropriate times. This variable 75 ** has no function other than to help verify the correct operation of the 76 ** library. 77 */ 78 #ifdef SQLITE_TEST 79 int sqlite3_sort_count = 0; 80 #endif 81 82 /* 83 ** The next global variable records the size of the largest MEM_Blob 84 ** or MEM_Str that has been used by a VDBE opcode. The test procedures 85 ** use this information to make sure that the zero-blob functionality 86 ** is working correctly. This variable has no function other than to 87 ** help verify the correct operation of the library. 88 */ 89 #ifdef SQLITE_TEST 90 int sqlite3_max_blobsize = 0; 91 static void updateMaxBlobsize(Mem *p){ 92 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ 93 sqlite3_max_blobsize = p->n; 94 } 95 } 96 #endif 97 98 /* 99 ** This macro evaluates to true if either the update hook or the preupdate 100 ** hook are enabled for database connect DB. 101 */ 102 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 103 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) 104 #else 105 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback) 106 #endif 107 108 /* 109 ** The next global variable is incremented each time the OP_Found opcode 110 ** is executed. This is used to test whether or not the foreign key 111 ** operation implemented using OP_FkIsZero is working. This variable 112 ** has no function other than to help verify the correct operation of the 113 ** library. 114 */ 115 #ifdef SQLITE_TEST 116 int sqlite3_found_count = 0; 117 #endif 118 119 /* 120 ** Test a register to see if it exceeds the current maximum blob size. 121 ** If it does, record the new maximum blob size. 122 */ 123 #if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE) 124 # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) 125 #else 126 # define UPDATE_MAX_BLOBSIZE(P) 127 #endif 128 129 #ifdef SQLITE_DEBUG 130 /* This routine provides a convenient place to set a breakpoint during 131 ** tracing with PRAGMA vdbe_trace=on. The breakpoint fires right after 132 ** each opcode is printed. Variables "pc" (program counter) and pOp are 133 ** available to add conditionals to the breakpoint. GDB example: 134 ** 135 ** break test_trace_breakpoint if pc=22 136 ** 137 ** Other useful labels for breakpoints include: 138 ** test_addop_breakpoint(pc,pOp) 139 ** sqlite3CorruptError(lineno) 140 ** sqlite3MisuseError(lineno) 141 ** sqlite3CantopenError(lineno) 142 */ 143 static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){ 144 static u64 n = 0; 145 (void)pc; 146 (void)pOp; 147 (void)v; 148 n++; 149 if( n==LARGEST_UINT64 ) abort(); /* So that n is used, preventing a warning */ 150 } 151 #endif 152 153 /* 154 ** Invoke the VDBE coverage callback, if that callback is defined. This 155 ** feature is used for test suite validation only and does not appear an 156 ** production builds. 157 ** 158 ** M is the type of branch. I is the direction taken for this instance of 159 ** the branch. 160 ** 161 ** M: 2 - two-way branch (I=0: fall-thru 1: jump ) 162 ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL ) 163 ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3) 164 ** 165 ** In other words, if M is 2, then I is either 0 (for fall-through) or 166 ** 1 (for when the branch is taken). If M is 3, the I is 0 for an 167 ** ordinary fall-through, I is 1 if the branch was taken, and I is 2 168 ** if the result of comparison is NULL. For M=3, I=2 the jump may or 169 ** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5. 170 ** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2 171 ** depending on if the operands are less than, equal, or greater than. 172 ** 173 ** iSrcLine is the source code line (from the __LINE__ macro) that 174 ** generated the VDBE instruction combined with flag bits. The source 175 ** code line number is in the lower 24 bits of iSrcLine and the upper 176 ** 8 bytes are flags. The lower three bits of the flags indicate 177 ** values for I that should never occur. For example, if the branch is 178 ** always taken, the flags should be 0x05 since the fall-through and 179 ** alternate branch are never taken. If a branch is never taken then 180 ** flags should be 0x06 since only the fall-through approach is allowed. 181 ** 182 ** Bit 0x08 of the flags indicates an OP_Jump opcode that is only 183 ** interested in equal or not-equal. In other words, I==0 and I==2 184 ** should be treated as equivalent 185 ** 186 ** Since only a line number is retained, not the filename, this macro 187 ** only works for amalgamation builds. But that is ok, since these macros 188 ** should be no-ops except for special builds used to measure test coverage. 189 */ 190 #if !defined(SQLITE_VDBE_COVERAGE) 191 # define VdbeBranchTaken(I,M) 192 #else 193 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M) 194 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){ 195 u8 mNever; 196 assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */ 197 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */ 198 assert( I> 24; 206 assert( (I & mNever)==0 ); 207 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/ 208 /* Invoke the branch coverage callback with three arguments: 209 ** iSrcLine - the line number of the VdbeCoverage() macro, with 210 ** flags removed. 211 ** I - Mask of bits 0x07 indicating which cases are are 212 ** fulfilled by this instance of the jump. 0x01 means 213 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any 214 ** impossible cases (ex: if the comparison is never NULL) 215 ** are filled in automatically so that the coverage 216 ** measurement logic does not flag those impossible cases 217 ** as missed coverage. 218 ** M - Type of jump. Same as M argument above 219 */ 220 I |= mNever; 221 if( M==2 ) I |= 0x04; 222 if( M==4 ){ 223 I |= 0x08; 224 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/ 225 } 226 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, 227 iSrcLine&0xffffff, I, M); 228 } 229 #endif 230 231 /* 232 ** An ephemeral string value (signified by the MEM_Ephem flag) contains 233 ** a pointer to a dynamically allocated string where some other entity 234 ** is responsible for deallocating that string. Because the register 235 ** does not control the string, it might be deleted without the register 236 ** knowing it. 237 ** 238 ** This routine converts an ephemeral string into a dynamically allocated 239 ** string that the register itself controls. In other words, it 240 ** converts an MEM_Ephem string into a string with P.z==P.zMalloc. 241 */ 242 #define Deephemeralize(P) \ 243 if( ((P)->flags&MEM_Ephem)!=0 \ 244 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} 245 246 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */ 247 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER) 248 249 /* 250 ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL 251 ** if we run out of memory. 252 */ 253 static VdbeCursor *allocateCursor( 254 Vdbe *p, /* The virtual machine */ 255 int iCur, /* Index of the new VdbeCursor */ 256 int nField, /* Number of fields in the table or index */ 257 u8 eCurType /* Type of the new cursor */ 258 ){ 259 /* Find the memory cell that will be used to store the blob of memory 260 ** required for this VdbeCursor structure. It is convenient to use a 261 ** vdbe memory cell to manage the memory allocation required for a 262 ** VdbeCursor structure for the following reasons: 263 ** 264 ** * Sometimes cursor numbers are used for a couple of different 265 ** purposes in a vdbe program. The different uses might require 266 ** different sized allocations. Memory cells provide growable 267 ** allocations. 268 ** 269 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can 270 ** be freed lazily via the sqlite3_release_memory() API. This 271 ** minimizes the number of malloc calls made by the system. 272 ** 273 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from 274 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1]. 275 ** Cursor 2 is at Mem[p->nMem-2]. And so forth. 276 */ 277 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem; 278 279 i64 nByte; 280 VdbeCursor *pCx = 0; 281 nByte = SZ_VDBECURSOR(nField); 282 assert( ROUND8(nByte)==nByte ); 283 if( eCurType==CURTYPE_BTREE ) nByte += sqlite3BtreeCursorSize(); 284 285 assert( iCur>=0 && iCurnCursor ); 286 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/ 287 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]); 288 p->apCsr[iCur] = 0; 289 } 290 291 /* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure 292 ** the pMem used to hold space for the cursor has enough storage available 293 ** in pMem->zMalloc. But for the special case of the aMem[] entries used 294 ** to hold cursors, it is faster to in-line the logic. */ 295 assert( pMem->flags==MEM_Undefined ); 296 assert( (pMem->flags & MEM_Dyn)==0 ); 297 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc ); 298 if( pMem->szMallocszMalloc>0 ){ 300 sqlite3DbFreeNN(pMem->db, pMem->zMalloc); 301 } 302 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte); 303 if( pMem->zMalloc==0 ){ 304 pMem->szMalloc = 0; 305 return 0; 306 } 307 pMem->szMalloc = (int)nByte; 308 } 309 310 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc; 311 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor)); 312 pCx->eCurType = eCurType; 313 pCx->nField = nField; 314 pCx->aOffset = &pCx->aType[nField]; 315 if( eCurType==CURTYPE_BTREE ){ 316 assert( ROUND8(SZ_VDBECURSOR(nField))==SZ_VDBECURSOR(nField) ); 317 pCx->uc.pCursor = (BtCursor*)&pMem->z[SZ_VDBECURSOR(nField)]; 318 sqlite3BtreeCursorZero(pCx->uc.pCursor); 319 } 320 return pCx; 321 } 322 323 /* 324 ** The string in pRec is known to look like an integer and to have a 325 ** floating point value of rValue. Return true and set *piValue to the 326 ** integer value if the string is in range to be an integer. Otherwise, 327 ** return false. 328 */ 329 static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){ 330 i64 iValue; 331 iValue = sqlite3RealToI64(rValue); 332 if( sqlite3RealSameAsInt(rValue,iValue) ){ 333 *piValue = iValue; 334 return 1; 335 } 336 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc); 337 } 338 339 /* 340 ** Try to convert a value into a numeric representation if we can 341 ** do so without loss of information. In other words, if the string 342 ** looks like a number, convert it into a number. If it does not 343 ** look like a number, leave it alone. 344 ** 345 ** If the bTryForInt flag is true, then extra effort is made to give 346 ** an integer representation. Strings that look like floating point 347 ** values but which have no fractional component (example: '48.00') 348 ** will have a MEM_Int representation when bTryForInt is true. 349 ** 350 ** If bTryForInt is false, then if the input string contains a decimal 351 ** point or exponential notation, the result is only MEM_Real, even 352 ** if there is an exact integer representation of the quantity. 353 */ 354 static void applyNumericAffinity(Mem *pRec, int bTryForInt){ 355 double rValue; 356 int rc; 357 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str ); 358 rc = sqlite3MemRealValueRC(pRec, &rValue); 359 if( rc<=0 ) return; 360 if( (rc&2)==0 && alsoAnInt(pRec, rValue, &pRec->u.i) ){ 361 pRec->flags |= MEM_Int; 362 }else{ 363 pRec->u.r = rValue; 364 pRec->flags |= MEM_Real; 365 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec, 1); 366 } 367 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the 368 ** string representation after computing a numeric equivalent, because the 369 ** string representation might not be the canonical representation for the 370 ** numeric value. Ticket [`343634942dd54ab57b7024`] 2018-01-31. */ 371 pRec->flags &= ~MEM_Str; 372 } 373 374 /* 375 ** Processing is determine by the affinity parameter: 376 ** 377 ** SQLITE_AFF_INTEGER: 378 ** SQLITE_AFF_REAL: 379 ** SQLITE_AFF_NUMERIC: 380 ** Try to convert pRec to an integer representation or a 381 ** floating-point representation if an integer representation 382 ** is not possible. Note that the integer representation is 383 ** always preferred, even if the affinity is REAL, because 384 ** an integer representation is more space efficient on disk. 385 ** 386 ** SQLITE_AFF_FLEXNUM: 387 ** If the value is text, then try to convert it into a number of 388 ** some kind (integer or real) but do not make any other changes. 389 ** 390 ** SQLITE_AFF_TEXT: 391 ** Convert pRec to a text representation. 392 ** 393 ** SQLITE_AFF_BLOB: 394 ** SQLITE_AFF_NONE: 395 ** No-op. pRec is unchanged. 396 */ 397 static void applyAffinity( 398 Mem *pRec, /* The value to apply affinity to */ 399 char affinity, /* The affinity to be applied */ 400 u8 enc /* Use this text encoding */ 401 ){ 402 if( affinity>=SQLITE_AFF_NUMERIC ){ 403 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL 404 || affinity==SQLITE_AFF_NUMERIC || affinity==SQLITE_AFF_FLEXNUM ); 405 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/ 406 if( (pRec->flags & (MEM_Real|MEM_IntReal))==0 ){ 407 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); 408 }else if( affinity<=SQLITE_AFF_REAL ){ 409 sqlite3VdbeIntegerAffinity(pRec, 0); 410 } 411 } 412 }else if( affinity==SQLITE_AFF_TEXT ){ 413 /* Only attempt the conversion to TEXT if there is an integer or real 414 ** representation (blob and NULL do not get converted) but no string 415 ** representation. It would be harmless to repeat the conversion if 416 ** there is already a string rep, but it is pointless to waste those 417 ** CPU cycles. */ 418 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/ 419 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){ 420 testcase( pRec->flags & MEM_Int ); 421 testcase( pRec->flags & MEM_Real ); 422 testcase( pRec->flags & MEM_IntReal ); 423 sqlite3VdbeMemStringify(pRec, enc, 1); 424 } 425 } 426 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal); 427 } 428 } 429 430 /* 431 ** Try to convert the type of a function argument or a result column 432 ** into a numeric representation. Use either INTEGER or REAL whichever 433 ** is appropriate. But only do the conversion if it is possible without 434 ** loss of information and return the revised type of the argument. 435 */ 436 int sqlite3_value_numeric_type(sqlite3_value *pVal){ 437 int eType = sqlite3_value_type(pVal); 438 if( eType==SQLITE_TEXT ){ 439 Mem *pMem = (Mem*)pVal; 440 #if SQLITE_THREADSAFE>0 441 sqlite3_mutex *pMutex = pMem->db ? pMem->db->mutex : 0; 442 #endif 443 sqlite3_mutex_enter(pMutex); 444 applyNumericAffinity(pMem, 0); 445 sqlite3_mutex_leave(pMutex); 446 eType = sqlite3_value_type(pVal); 447 } 448 return eType; 449 } 450 451 /* 452 ** Exported version of applyAffinity(). This one works on sqlite3_value*, 453 ** not the internal Mem* type. 454 */ 455 void sqlite3ValueApplyAffinity( 456 sqlite3_value *pVal, 457 u8 affinity, 458 u8 enc 459 ){ 460 applyAffinity((Mem *)pVal, affinity, enc); 461 } 462 463 /* 464 ** pMem currently only holds a string type (or maybe a BLOB that we can 465 ** interpret as a string if we want to). Compute its corresponding 466 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields 467 ** accordingly. 468 */ 469 static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ 470 int rc; 471 sqlite3_int64 ix; 472 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ); 473 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); 474 if( ExpandBlob(pMem) ){ 475 pMem->u.i = 0; 476 return MEM_Int; 477 } 478 rc = sqlite3MemRealValueRC(pMem, &pMem->u.r); 479 if( rc<=0 ){ 480 if( (rc&2)==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){ 481 pMem->u.i = ix; 482 return MEM_Int; 483 }else{ 484 return MEM_Real; 485 } 486 }else if( (rc&2)==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){ 487 pMem->u.i = ix; 488 return MEM_Int; 489 } 490 return MEM_Real; 491 } 492 493 /* 494 ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or 495 ** none. 496 ** 497 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. 498 ** But it does set pMem->u.r and pMem->u.i appropriately. 499 */ 500 static u16 numericType(Mem *pMem){ 501 assert( (pMem->flags & MEM_Null)==0 502 || pMem->db==0 || pMem->db->mallocFailed ); 503 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){ 504 testcase( pMem->flags & MEM_Int ); 505 testcase( pMem->flags & MEM_Real ); 506 testcase( pMem->flags & MEM_IntReal ); 507 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null); 508 } 509 assert( pMem->flags & (MEM_Str|MEM_Blob) ); 510 testcase( pMem->flags & MEM_Str ); 511 testcase( pMem->flags & MEM_Blob ); 512 return computeNumericType(pMem); 513 return 0; 514 } 515 516 #ifdef SQLITE_DEBUG 517 /* 518 ** Write a nice string representation of the contents of cell pMem 519 ** into buffer zBuf, length nBuf. 520 */ 521 void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){ 522 int f = pMem->flags; 523 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; 524 if( f&MEM_Blob ){ 525 int i; 526 char c; 527 if( f & MEM_Dyn ){ 528 c = 'z'; 529 assert( (f & (MEM_Static|MEM_Ephem))==0 ); 530 }else if( f & MEM_Static ){ 531 c = 't'; 532 assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); 533 }else if( f & MEM_Ephem ){ 534 c = 'e'; 535 assert( (f & (MEM_Static|MEM_Dyn))==0 ); 536 }else{ 537 c = 's'; 538 } 539 sqlite3_str_appendf(pStr, "%cx[", c); 540 for(i=0; i<25 && in; i++){ 541 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF)); 542 } 543 sqlite3_str_appendf(pStr, "|"); 544 for(i=0; i<25 && in; i++){ 545 char z = pMem->z[i]; 546 sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z); 547 } 548 sqlite3_str_appendf(pStr,"]"); 549 if( f & MEM_Zero ){ 550 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero); 551 } 552 }else if( f & MEM_Str ){ 553 int j; 554 u8 c; 555 if( f & MEM_Dyn ){ 556 c = 'z'; 557 assert( (f & (MEM_Static|MEM_Ephem))==0 ); 558 }else if( f & MEM_Static ){ 559 c = 't'; 560 assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); 561 }else if( f & MEM_Ephem ){ 562 c = 'e'; 563 assert( (f & (MEM_Static|MEM_Dyn))==0 ); 564 }else{ 565 c = 's'; 566 } 567 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n); 568 for(j=0; j<25 && jn; j++){ 569 c = pMem->z[j]; 570 sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.'); 571 } 572 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]); 573 if( f & MEM_Term ){ 574 sqlite3_str_appendf(pStr, "(0-term)"); 575 } 576 } 577 } 578 #endif 579 580 #ifdef SQLITE_DEBUG 581 /* 582 ** Print the value of a register for tracing purposes: 583 */ 584 static void memTracePrint(Mem *p){ 585 if( p->flags & MEM_Undefined ){ 586 printf(" undefined"); 587 }else if( p->flags & MEM_Null ){ 588 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL"); 589 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ 590 printf(" si:%lld", p->u.i); 591 }else if( (p->flags & (MEM_IntReal))!=0 ){ 592 printf(" ir:%lld", p->u.i); 593 }else if( p->flags & MEM_Int ){ 594 printf(" i:%lld", p->u.i); 595 #ifndef SQLITE_OMIT_FLOATING_POINT 596 }else if( p->flags & MEM_Real ){ 597 printf(" r:%.17g", p->u.r); 598 #endif 599 }else if( sqlite3VdbeMemIsRowSet(p) ){ 600 printf(" (rowset)"); 601 }else{ 602 StrAccum acc; 603 char zBuf[1000]; 604 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); 605 sqlite3VdbeMemPrettyPrint(p, &acc); 606 printf(" %s", sqlite3StrAccumFinish(&acc)); 607 } 608 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype); 609 } 610 static void registerTrace(int iReg, Mem *p){ 611 printf("R[%d] = ", iReg); 612 memTracePrint(p); 613 if( p->pScopyFrom ){ 614 assert( p->pScopyFrom->bScopy ); 615 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg])); 616 } 617 printf("\n"); 618 sqlite3VdbeCheckMemInvariants(p); 619 } 620 /**/ void sqlite3PrintMem(Mem *pMem){ 621 memTracePrint(pMem); 622 printf("\n"); 623 fflush(stdout); 624 } 625 #endif 626 627 #ifdef SQLITE_DEBUG 628 /* 629 ** Show the values of all registers in the virtual machine. Used for 630 ** interactive debugging. 631 */ 632 void sqlite3VdbeRegisterDump(Vdbe *v){ 633 int i; 634 for(i=1; inMem; i++) registerTrace(i, v->aMem+i); 635 } 636 #endif /* SQLITE_DEBUG */ 637 638 639 #ifdef SQLITE_DEBUG 640 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) 641 #else 642 # define REGISTER_TRACE(R,M) 643 #endif 644 645 #ifndef NDEBUG 646 /* 647 ** This function is only called from within an assert() expression. It 648 ** checks that the sqlite3.nTransaction variable is correctly set to 649 ** the number of non-transaction savepoints currently in the 650 ** linked list starting at sqlite3.pSavepoint. 651 ** 652 ** Usage: 653 ** 654 ** assert( checkSavepointCount(db) ); 655 */ 656 static int checkSavepointCount(sqlite3 *db){ 657 int n = 0; 658 Savepoint *p; 659 for(p=db->pSavepoint; p; p=p->pNext) n++; 660 assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); 661 return 1; 662 } 663 #endif 664 665 /* 666 ** Return the register of pOp->p2 after first preparing it to be 667 ** overwritten with an integer value. 668 */ 669 static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){ 670 sqlite3VdbeMemSetNull(pOut); 671 pOut->flags = MEM_Int; 672 return pOut; 673 } 674 static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ 675 Mem *pOut; 676 assert( pOp->p2>0 ); 677 assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); 678 pOut = &p->aMem[pOp->p2]; 679 memAboutToChange(p, pOut); 680 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/ 681 return out2PrereleaseWithClear(pOut); 682 }else{ 683 pOut->flags = MEM_Int; 684 return pOut; 685 } 686 } 687 688 /* 689 ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning 690 ** with pOp->p3. Return the hash. 691 ** 692 ** IMPORTANT RESTRICTION (tag-`202607231411`): This hash is only valid if the 693 ** collating sequence for TEXT is BINARY. Hence, Bloom filters that use this 694 ** hash will not work for look-ups that use any other collating sequence. 695 */ 696 static u64 filterHash(const Mem *aMem, const Op *pOp){ 697 int i, mx; 698 u64 h = 0; 699 700 assert( pOp->p4type==P4_INT32 ); 701 for(i=pOp->p3, mx=i+pOp->p4.i; iflags & (MEM_Int|MEM_IntReal) ){ 704 h += p->u.i; 705 }else if( p->flags & MEM_Real ){ 706 h += sqlite3VdbeIntValue(p); 707 }else if( p->flags & MEM_Str ){ 708 u64 x; 709 h += p->n; 710 if( p->n >= (int)sizeof(x) ){ 711 memcpy(&x, p->z, sizeof(x)); 712 h += x; 713 memcpy(&x, p->z + p->n - sizeof(x), sizeof(x)); 714 h += x; 715 }else{ 716 x = 0; 717 memcpy(&x, p->z, p->n); 718 h += x; 719 } 720 }else if( p->flags & MEM_Blob ){ 721 int n = p->n; 722 u64 x = 0; 723 if( n ){ 724 memcpy(&x, p->z, MIN(n, (int)sizeof(x))); 725 h += x; 726 } 727 h += n; 728 if( p->flags & MEM_Zero ) h += p->u.nZero; 729 } 730 } 731 return h; 732 } 733 734 /* 735 ** For OP_Column, factor out the case where content is loaded from 736 ** overflow pages, so that the code to implement this case is separate 737 ** the common case where all content fits on the page. Factoring out 738 ** the code reduces register pressure and helps the common case 739 ** to run faster. 740 */ 741 static SQLITE_NOINLINE int vdbeColumnFromOverflow( 742 VdbeCursor *pC, /* The BTree cursor from which we are reading */ 743 int iCol, /* The column to read */ 744 u32 t, /* The serial-type code for the column value */ 745 i64 iOffset, /* Offset to the start of the content value */ 746 u32 cacheStatus, /* Current Vdbe.cacheCtr value */ 747 u32 colCacheCtr, /* Current value of the column cache counter */ 748 Mem *pDest /* Store the value into this register. */ 749 ){ 750 int rc; 751 sqlite3 *db = pDest->db; 752 int encoding = pDest->enc; 753 int len = sqlite3VdbeSerialTypeLen(t); 754 assert( pC->eCurType==CURTYPE_BTREE ); 755 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) return SQLITE_TOOBIG; 756 if( len > 4000 && pC->pKeyInfo==0 ){ 757 /* Cache large column values that are on overflow pages using 758 ** an RCStr (reference counted string) so that if they are reloaded, 759 ** that do not have to be copied a second time. The overhead of 760 ** creating and managing the cache is such that this is only 761 ** profitable for larger TEXT and BLOB values. 762 ** 763 ** Only do this on table-btrees so that writes to index-btrees do not 764 ** need to clear the cache. This buys performance in the common case 765 ** in exchange for generality. 766 */ 767 VdbeTxtBlbCache *pCache; 768 char *pBuf; 769 if( pC->colCache==0 ){ 770 pC->pCache = sqlite3DbMallocZero(db, sizeof(VdbeTxtBlbCache) ); 771 if( pC->pCache==0 ) return SQLITE_NOMEM; 772 pC->colCache = 1; 773 } 774 pCache = pC->pCache; 775 if( pCache->pCValue==0 776 || pCache->iCol!=iCol 777 || pCache->cacheStatus!=cacheStatus 778 || pCache->colCacheCtr!=colCacheCtr 779 || pCache->iOffset!=sqlite3BtreeOffset(pC->uc.pCursor) 780 ){ 781 if( pCache->pCValue ) sqlite3RCStrUnref(pCache->pCValue); 782 pBuf = pCache->pCValue = sqlite3RCStrNew( len+3 ); 783 if( pBuf==0 ) return SQLITE_NOMEM; 784 rc = sqlite3BtreePayload(pC->uc.pCursor, iOffset, len, pBuf); 785 if( rc ) return rc; 786 pBuf[len] = 0; 787 pBuf[len+1] = 0; 788 pBuf[len+2] = 0; 789 pCache->iCol = iCol; 790 pCache->cacheStatus = cacheStatus; 791 pCache->colCacheCtr = colCacheCtr; 792 pCache->iOffset = sqlite3BtreeOffset(pC->uc.pCursor); 793 }else{ 794 pBuf = pCache->pCValue; 795 } 796 assert( t>=12 ); 797 sqlite3RCStrRef(pBuf); 798 if( t&1 ){ 799 rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, encoding|ENC_KEEPBOM, 800 sqlite3RCStrUnref); 801 pDest->flags |= MEM_Term; 802 }else{ 803 rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, 0, 804 sqlite3RCStrUnref); 805 pDest->enc = encoding; 806 } 807 }else{ 808 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, iOffset, len, pDest); 809 if( rc ) return rc; 810 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); 811 if( (t&1)!=0 && encoding==SQLITE_UTF8 ){ 812 pDest->z[len] = 0; 813 pDest->flags |= MEM_Term; 814 } 815 } 816 pDest->flags &= ~MEM_Ephem; 817 return rc; 818 } 819 820 /* 821 ** Memory cell pMem may contain a blob or a NULL value. Cursor pCsr is 822 ** open on an index. If the current index entry matches the blob value in 823 ** pMem byte-for-byte, set pMem to NULL and return 1. Otherwise, return 0. 824 ** 825 ** If an error occurs, set (*pRc) to an SQLite error code. Return 1 in this 826 ** case as well. 827 */ 828 static SQLITE_NOINLINE int vdbeIndexKeyCompare( 829 BtCursor *pCsr, /* Cursor to compare key to */ 830 Mem *pMem, 831 int *pRc 832 ){ 833 int ret = 0; 834 u32 nKey = 0; 835 836 assert( pMem->flags & (MEM_Blob|MEM_Null) ); 837 nKey = sqlite3BtreePayloadSize(pCsr); 838 if( nKey==(u32)pMem->n && ALWAYS((pMem->flags & MEM_Blob)!=0) ){ 839 /* This code could just use sqlite3BtreePayloadFetch(). But calling that 840 ** function here apparently prevents compilers from inlining it in other, 841 ** more performance critical, places. So this code uses 842 ** MemFromBtreeZeroOffset(), which is just as fast in most cases, but also 843 ** handles the case where the index record uses overflow pages. */ 844 Mem m; 845 memset(&m, 0, sizeof(m)); 846 *pRc = sqlite3VdbeMemFromBtreeZeroOffset(pCsr, nKey, &m); 847 ret = (*pRc!=SQLITE_OK || 0==memcmp(pMem->z, m.z, nKey)); 848 sqlite3VdbeMemReleaseMalloc(&m); 849 } 850 851 return ret; 852 } 853 854 /* 855 ** Send a "statement aborts" message to the error log. 856 */ 857 static SQLITE_NOINLINE void sqlite3VdbeLogAbort( 858 Vdbe *p, /* The statement that is running at the time of failure */ 859 int rc, /* Error code */ 860 Op *pOp, /* Opcode that filed */ 861 Op *aOp /* All opcodes */ 862 ){ 863 const char *zSql = p->zSql; /* Original SQL text */ 864 const char *zPrefix = ""; /* Prefix added to SQL text */ 865 int pc; /* Opcode address */ 866 char zXtra[100]; /* Buffer space to store zPrefix */ 867 868 if( p->pFrame ){ 869 assert( aOp[0].opcode==OP_Init ); 870 if( aOp[0].p4.z!=0 ){ 871 assert( aOp[0].p4.z[0]=='-' 872 && aOp[0].p4.z[1]=='-' 873 && aOp[0].p4.z[2]==' ' ); 874 sqlite3_snprintf(sizeof(zXtra), zXtra,"/* %s */ ",aOp[0].p4.z+3); 875 zPrefix = zXtra; 876 }else{ 877 zPrefix = "/* unknown trigger */ "; 878 } 879 } 880 pc = (int)(pOp - aOp); 881 sqlite3_log(rc, "statement aborts at %d: %s; [%s%s]", 882 pc, p->zErrMsg, zPrefix, zSql); 883 } 884 885 /* 886 ** Return the symbolic name for the data type of a pMem 887 */ 888 static const char *vdbeMemTypeName(Mem *pMem){ 889 static const char *azTypes[] = { 890 /* SQLITE_INTEGER */ "INT", 891 /* SQLITE_FLOAT */ "REAL", 892 /* SQLITE_TEXT */ "TEXT", 893 /* SQLITE_BLOB */ "BLOB", 894 /* SQLITE_NULL */ "NULL" 895 }; 896 return azTypes[sqlite3_value_type(pMem)-1]; 897 } 898 899 /* 900 ** Execute as much of a VDBE program as we can. 901 ** This is the core of sqlite3_step(). 902 */ 903 int sqlite3VdbeExec( 904 Vdbe *p /* The VDBE */ 905 ){ 906 Op *aOp = p->aOp; /* Copy of p->aOp */ 907 Op *pOp = aOp; /* Current operation */ 908 #ifdef SQLITE_DEBUG 909 Op *pOrigOp; /* Value of pOp at the top of the loop */ 910 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */ 911 u8 iCompareIsInit = 0; /* iCompare is initialized */ 912 #endif 913 int rc = SQLITE_OK; /* Value to return */ 914 sqlite3 *db = p->db; /* The database */ 915 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ 916 u8 encoding = ENC(db); /* The database encoding */ 917 int iCompare = 0; /* Result of last comparison */ 918 u64 nVmStep = 0; /* Number of virtual machine steps */ 919 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 920 u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */ 921 #endif 922 Mem *aMem = p->aMem; /* Copy of p->aMem */ 923 Mem *pIn1 = 0; /* 1st input operand */ 924 Mem *pIn2 = 0; /* 2nd input operand */ 925 Mem *pIn3 = 0; /* 3rd input operand */ 926 Mem *pOut = 0; /* Output operand */ 927 u32 colCacheCtr = 0; /* Column cache counter */ 928 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE) 929 u64 *pnCycle = 0; 930 int bStmtScanStatus = IS_STMT_SCANSTATUS(db)!=0; 931 #endif 932 /*** INSERT STACK UNION HERE ***/ 933 934 assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */ 935 if( DbMaskNonZero(p->lockMask) ){ 936 sqlite3VdbeEnter(p); 937 } 938 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 939 if( db->xProgress ){ 940 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; 941 assert( 0 < db->nProgressOps ); 942 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); 943 }else{ 944 nProgressLimit = LARGEST_UINT64; 945 } 946 #endif 947 if( p->rc==SQLITE_NOMEM ){ 948 /* This happens if a malloc() inside a call to sqlite3_column_text() or 949 ** sqlite3_column_text16() failed. */ 950 goto no_mem; 951 } 952 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); 953 testcase( p->rc!=SQLITE_OK ); 954 p->rc = SQLITE_OK; 955 assert( p->bIsReader || p->readOnly!=0 ); 956 p->iCurrentTime = 0; 957 assert( p->explain==0 ); 958 db->busyHandler.nBusy = 0; 959 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt; 960 sqlite3VdbeIOTraceSql(p); 961 #ifdef SQLITE_DEBUG 962 sqlite3BeginBenignMalloc(); 963 if( p->pc==0 964 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0 965 ){ 966 int i; 967 int once = 1; 968 sqlite3VdbePrintSql(p); 969 if( p->db->flags & SQLITE_VdbeListing ){ 970 printf("VDBE Program Listing:\n"); 971 for(i=0; inOp; i++){ 972 sqlite3VdbePrintOp(stdout, i, &aOp[i]); 973 } 974 } 975 if( p->db->flags & SQLITE_VdbeEQP ){ 976 for(i=0; inOp; i++){ 977 if( aOp[i].opcode==OP_Explain ){ 978 if( once ) printf("VDBE Query Plan:\n"); 979 printf("%s\n", aOp[i].p4.z); 980 once = 0; 981 } 982 } 983 } 984 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); 985 } 986 sqlite3EndBenignMalloc(); 987 #endif 988 for(pOp=&aOp[p->pc]; 1; pOp++){ 989 /* Errors are detected by individual opcodes, with an immediate 990 ** jumps to abort_due_to_error. */ 991 assert( rc==SQLITE_OK ); 992 993 assert( pOp>=aOp && pOp<&aOp[p->nOp]); 994 nVmStep++; 995 996 #if defined(VDBE_PROFILE) 997 pOp->nExec++; 998 pnCycle = &pOp->nCycle; 999 if( sqlite3NProfileCnt==0 ) *pnCycle -= sqlite3Hwtime(); 1000 #elif defined(SQLITE_ENABLE_STMT_SCANSTATUS) 1001 if( bStmtScanStatus ){ 1002 pOp->nExec++; 1003 pnCycle = &pOp->nCycle; 1004 *pnCycle -= sqlite3Hwtime(); 1005 } 1006 #endif 1007 1008 /* Only allow tracing if SQLITE_DEBUG is defined. 1009 */ 1010 #ifdef SQLITE_DEBUG 1011 if( db->flags & SQLITE_VdbeTrace ){ 1012 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp); 1013 test_trace_breakpoint((int)(pOp - aOp),pOp,p); 1014 } 1015 #endif 1016 1017 1018 /* Check to see if we need to simulate an interrupt. This only happens 1019 ** if we have a special test build. 1020 */ 1021 #ifdef SQLITE_TEST 1022 if( sqlite3_interrupt_count>0 ){ 1023 sqlite3_interrupt_count--; 1024 if( sqlite3_interrupt_count==0 ){ 1025 sqlite3_interrupt(db); 1026 } 1027 } 1028 #endif 1029 1030 /* Sanity checking on other operands */ 1031 #ifdef SQLITE_DEBUG 1032 { 1033 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode]; 1034 if( (opProperty & OPFLG_IN1)!=0 ){ 1035 assert( pOp->p1>0 ); 1036 assert( pOp->p1<=(p->nMem+1 - p->nCursor) ); 1037 assert( memIsValid(&aMem[pOp->p1]) ); 1038 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); 1039 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); 1040 } 1041 if( (opProperty & OPFLG_IN2)!=0 ){ 1042 assert( pOp->p2>0 ); 1043 assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); 1044 assert( memIsValid(&aMem[pOp->p2]) ); 1045 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); 1046 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); 1047 } 1048 if( (opProperty & OPFLG_IN3)!=0 ){ 1049 assert( pOp->p3>0 ); 1050 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 1051 assert( memIsValid(&aMem[pOp->p3]) ); 1052 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); 1053 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); 1054 } 1055 if( (opProperty & OPFLG_OUT2)!=0 ){ 1056 assert( pOp->p2>0 ); 1057 assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); 1058 memAboutToChange(p, &aMem[pOp->p2]); 1059 } 1060 if( (opProperty & OPFLG_OUT3)!=0 ){ 1061 assert( pOp->p3>0 ); 1062 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 1063 memAboutToChange(p, &aMem[pOp->p3]); 1064 } 1065 } 1066 #endif 1067 #ifdef SQLITE_DEBUG 1068 pOrigOp = pOp; 1069 #endif 1070 1071 switch( pOp->opcode ){ 1072 1073 /***************************************************************************** 1074 ** What follows is a massive switch statement where each case implements a 1075 ** separate instruction in the virtual machine. If we follow the usual 1076 ** indentation conventions, each case should be indented by 6 spaces. But 1077 ** that is a lot of wasted space on the left margin. So the code within 1078 ** the switch statement will break with convention and be flush-left. Another 1079 ** big comment (similar to this one) will mark the point in the code where 1080 ** we transition back to normal indentation. 1081 ** 1082 ** The formatting of each case is important. The makefile for SQLite 1083 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this 1084 ** file looking for lines that begin with "case OP_". The opcodes.h files 1085 ** will be filled with #defines that give unique integer values to each 1086 ** opcode and the opcodes.c file is filled with an array of strings where 1087 ** each string is the symbolic name for the corresponding opcode. If the 1088 ** case statement is followed by a comment of the form "/# same as ... #/" 1089 ** that comment is used to determine the particular value of the opcode. 1090 ** 1091 ** Other keywords in the comment that follows each case are used to 1092 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. 1093 ** Keywords include: in1, in2, in3, out2, out3. See 1094 ** the mkopcodeh.awk script for additional information. 1095 ** 1096 ** Documentation about VDBE opcodes is generated by scanning this file 1097 ** for lines of that contain "Opcode:". That line and all subsequent 1098 ** comment lines are used in the generation of the opcode.html documentation 1099 ** file. 1100 ** 1101 ** SUMMARY: 1102 ** 1103 ** Formatting is important to scripts that scan this file. 1104 ** Do not deviate from the formatting style currently in use. 1105 ** 1106 *****************************************************************************/ 1107 1108 /* Opcode: Goto * P2 * * * 1109 ** 1110 ** An unconditional jump to address P2. 1111 ** The next instruction executed will be 1112 ** the one at index P2 from the beginning of 1113 ** the program. 1114 ** 1115 ** The P1 parameter is not actually used by this opcode. However, it 1116 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell 1117 ** that this Goto is the bottom of a loop and that the lines from P2 down 1118 ** to the current line should be indented for EXPLAIN output. 1119 */ 1120 case OP_Goto: { /* jump */ 1121 1122 #ifdef SQLITE_DEBUG 1123 /* In debugging mode, when the p5 flags is set on an OP_Goto, that 1124 ** means we should really jump back to the preceding OP_ReleaseReg 1125 ** instruction. */ 1126 if( pOp->p5 ){ 1127 assert( pOp->p2 < (int)(pOp - aOp) ); 1128 assert( pOp->p2 > 1 ); 1129 pOp = &aOp[pOp->p2 - 2]; 1130 assert( pOp[1].opcode==OP_ReleaseReg ); 1131 goto check_for_interrupt; 1132 } 1133 #endif 1134 1135 jump_to_p2_and_check_for_interrupt: 1136 pOp = &aOp[pOp->p2 - 1]; 1137 1138 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev, 1139 ** OP_VNext, or OP_SorterNext) all jump here upon 1140 ** completion. Check to see if sqlite3_interrupt() has been called 1141 ** or if the progress callback needs to be invoked. 1142 ** 1143 ** This code uses unstructured "goto" statements and does not look clean. 1144 ** But that is not due to sloppy coding habits. The code is written this 1145 ** way for performance, to avoid having to run the interrupt and progress 1146 ** checks on every opcode. This helps sqlite3_step() to run about 1.5% 1147 ** faster according to "valgrind --tool=cachegrind" */ 1148 check_for_interrupt: 1149 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt; 1150 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 1151 /* Call the progress callback if it is configured and the required number 1152 ** of VDBE ops have been executed (either since this invocation of 1153 ** sqlite3VdbeExec() or since last time the progress callback was called). 1154 ** If the progress callback returns non-zero, exit the virtual machine with 1155 ** a return code SQLITE_ABORT. 1156 */ 1157 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){ 1158 assert( db->nProgressOps!=0 ); 1159 nProgressLimit += db->nProgressOps; 1160 if( db->xProgress(db->pProgressArg) ){ 1161 nProgressLimit = LARGEST_UINT64; 1162 rc = SQLITE_INTERRUPT; 1163 goto abort_due_to_error; 1164 } 1165 } 1166 #endif 1167 1168 break; 1169 } 1170 1171 /* Opcode: Gosub P1 P2 * * * 1172 ** 1173 ** Write the current address onto register P1 1174 ** and then jump to address P2. 1175 */ 1176 case OP_Gosub: { /* jump */ 1177 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 1178 pIn1 = &aMem[pOp->p1]; 1179 assert( VdbeMemDynamic(pIn1)==0 ); 1180 memAboutToChange(p, pIn1); 1181 pIn1->flags = MEM_Int; 1182 pIn1->u.i = (int)(pOp-aOp); 1183 REGISTER_TRACE(pOp->p1, pIn1); 1184 goto jump_to_p2_and_check_for_interrupt; 1185 } 1186 1187 /* Opcode: Return P1 P2 P3 * * 1188 ** 1189 ** Jump to the address stored in register P1. If P1 is a return address 1190 ** register, then this accomplishes a return from a subroutine. 1191 ** 1192 ** If P3 is 1, then the jump is only taken if register P1 holds an integer 1193 ** values, otherwise execution falls through to the next opcode, and the 1194 ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an 1195 ** integer or else an assert() is raised. P3 should be set to 1 when 1196 ** this opcode is used in combination with OP_BeginSubrtn, and set to 0 1197 ** otherwise. 1198 ** 1199 ** The value in register P1 is unchanged by this opcode. 1200 ** 1201 ** P2 is not used by the byte-code engine. However, if P2 is positive 1202 ** and also less than the current address, then the "EXPLAIN" output 1203 ** formatter in the CLI will indent all opcodes from the P2 opcode up 1204 ** to be not including the current Return. P2 should be the first opcode 1205 ** in the subroutine from which this opcode is returning. Thus the P2 1206 ** value is a byte-code indentation hint. See tag-`20220407a` in 1207 ** wherecode.c and shell.c. 1208 */ 1209 case OP_Return: { /* in1 */ 1210 pIn1 = &aMem[pOp->p1]; 1211 if( pIn1->flags & MEM_Int ){ 1212 if( pOp->p3 ){ VdbeBranchTaken(1, 2); } 1213 pOp = &aOp[pIn1->u.i]; 1214 }else if( ALWAYS(pOp->p3) ){ 1215 VdbeBranchTaken(0, 2); 1216 } 1217 break; 1218 } 1219 1220 /* Opcode: InitCoroutine P1 P2 P3 * * 1221 ** 1222 ** Set up register P1 so that it will Yield to the coroutine 1223 ** located at address P3. 1224 ** 1225 ** If P2!=0 then the coroutine implementation immediately follows 1226 ** this opcode. So jump over the coroutine implementation to 1227 ** address P2. 1228 ** 1229 ** See also: EndCoroutine 1230 */ 1231 case OP_InitCoroutine: { /* jump0 */ 1232 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 1233 assert( pOp->p2>=0 && pOp->p2nOp ); 1234 assert( pOp->p3>=0 && pOp->p3nOp ); 1235 pOut = &aMem[pOp->p1]; 1236 assert( !VdbeMemDynamic(pOut) ); 1237 pOut->u.i = pOp->p3 - 1; 1238 pOut->flags = MEM_Int; 1239 if( pOp->p2==0 ) break; 1240 1241 /* Most jump operations do a goto to this spot in order to update 1242 ** the pOp pointer. */ 1243 jump_to_p2: 1244 assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */ 1245 assert( pOp->p2nOp ); /* Jumps must be in range */ 1246 pOp = &aOp[pOp->p2 - 1]; 1247 break; 1248 } 1249 1250 /* Opcode: EndCoroutine P1 * * * * 1251 ** 1252 ** The instruction at the address in register P1 is a Yield. 1253 ** Jump to the P2 parameter of that Yield. 1254 ** After the jump, the value register P1 is left with a value 1255 ** such that subsequent OP_Yields go back to the this same 1256 ** OP_EndCoroutine instruction. 1257 ** 1258 ** See also: InitCoroutine 1259 */ 1260 case OP_EndCoroutine: { /* in1 */ 1261 VdbeOp *pCaller; 1262 pIn1 = &aMem[pOp->p1]; 1263 assert( pIn1->flags==MEM_Int ); 1264 assert( pIn1->u.i>=0 && pIn1->u.inOp ); 1265 pCaller = &aOp[pIn1->u.i]; 1266 assert( pCaller->opcode==OP_Yield ); 1267 assert( pCaller->p2>=0 && pCaller->p2nOp ); 1268 pIn1->u.i = (int)(pOp - p->aOp) - 1; 1269 pOp = &aOp[pCaller->p2 - 1]; 1270 break; 1271 } 1272 1273 /* Opcode: Yield P1 P2 * * * 1274 ** 1275 ** Swap the program counter with the value in register P1. This 1276 ** has the effect of yielding to a coroutine. 1277 ** 1278 ** If the coroutine that is launched by this instruction ends with 1279 ** Yield or Return then continue to the next instruction. But if 1280 ** the coroutine launched by this instruction ends with 1281 ** EndCoroutine, then jump to P2 rather than continuing with the 1282 ** next instruction. 1283 ** 1284 ** See also: InitCoroutine 1285 */ 1286 case OP_Yield: { /* in1, jump0 */ 1287 int pcDest; 1288 pIn1 = &aMem[pOp->p1]; 1289 assert( VdbeMemDynamic(pIn1)==0 ); 1290 pIn1->flags = MEM_Int; 1291 pcDest = (int)pIn1->u.i; 1292 pIn1->u.i = (int)(pOp - aOp); 1293 REGISTER_TRACE(pOp->p1, pIn1); 1294 pOp = &aOp[pcDest]; 1295 break; 1296 } 1297 1298 /* Opcode: HaltIfNull P1 P2 P3 P4 P5 1299 ** Synopsis: if r[P3]=null halt 1300 ** 1301 ** Check the value in register P3. If it is NULL then Halt using 1302 ** parameter P1, P2, and P4 as if this were a Halt instruction. If the 1303 ** value in register P3 is not NULL, then this routine is a no-op. 1304 ** The P5 parameter should be 1. 1305 */ 1306 case OP_HaltIfNull: { /* in3 */ 1307 pIn3 = &aMem[pOp->p3]; 1308 #ifdef SQLITE_DEBUG 1309 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } 1310 #endif 1311 if( (pIn3->flags & MEM_Null)==0 ) break; 1312 /* Fall through into OP_Halt */ 1313 /* no break */ deliberate_fall_through 1314 } 1315 1316 /* Opcode: Halt P1 P2 P3 P4 P5 1317 ** 1318 ** Exit immediately. All open cursors, etc are closed 1319 ** automatically. 1320 ** 1321 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), 1322 ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). 1323 ** For errors, it can be some other value. If P1!=0 then P2 will determine 1324 ** whether or not to rollback the current transaction. Do not rollback 1325 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, 1326 ** then back out all changes that have occurred during this execution of the 1327 ** VDBE, but do not rollback the transaction. 1328 ** 1329 ** If P3 is not zero and P4 is NULL, then P3 is a register that holds the 1330 ** text of an error message. 1331 ** 1332 ** If P3 is zero and P4 is not null then the error message string is held 1333 ** in P4. 1334 ** 1335 ** P5 is a value between 1 and 4, inclusive, then the P4 error message 1336 ** string is modified as follows: 1337 ** 1338 ** 1: NOT NULL constraint failed: P4 1339 ** 2: UNIQUE constraint failed: P4 1340 ** 3: CHECK constraint failed: P4 1341 ** 4: FOREIGN KEY constraint failed: P4 1342 ** 1343 ** If P3 is zero and P5 is not zero and P4 is NULL, then everything after 1344 ** the ":" is omitted. 1345 ** 1346 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of 1347 ** every program. So a jump past the last instruction of the program 1348 ** is the same as executing Halt. 1349 */ 1350 case OP_Halt: { 1351 VdbeFrame *pFrame; 1352 int pcx; 1353 1354 #ifdef SQLITE_DEBUG 1355 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } 1356 #endif 1357 assert( pOp->p4type==P4_NOTUSED 1358 || pOp->p4type==P4_STATIC 1359 || pOp->p4type==P4_DYNAMIC ); 1360 1361 /* A deliberately coded "OP_Halt SQLITE_INTERNAL * * * *" opcode indicates 1362 ** something is wrong with the code generator. Raise an assertion in order 1363 ** to bring this to the attention of fuzzers and other testing tools. */ 1364 assert( pOp->p1!=SQLITE_INTERNAL ); 1365 1366 if( p->pFrame && pOp->p1==SQLITE_OK ){ 1367 /* Halt the sub-program. Return control to the parent frame. */ 1368 pFrame = p->pFrame; 1369 p->pFrame = pFrame->pParent; 1370 p->nFrame--; 1371 sqlite3VdbeSetChanges(db, p->nChange); 1372 pcx = sqlite3VdbeFrameRestore(pFrame); 1373 if( pOp->p2==OE_Ignore ){ 1374 /* Instruction pcx is the OP_Program that invoked the sub-program 1375 ** currently being halted. If the p2 instruction of this OP_Halt 1376 ** instruction is set to OE_Ignore, then the sub-program is throwing 1377 ** an IGNORE exception. In this case jump to the address specified 1378 ** as the p2 of the calling OP_Program. */ 1379 pcx = p->aOp[pcx].p2-1; 1380 } 1381 aOp = p->aOp; 1382 aMem = p->aMem; 1383 pOp = &aOp[pcx]; 1384 break; 1385 } 1386 p->rc = pOp->p1; 1387 p->errorAction = (u8)pOp->p2; 1388 assert( pOp->p5<=4 ); 1389 if( p->rc ){ 1390 if( pOp->p3>0 && pOp->p4type==P4_NOTUSED ){ 1391 const char *zErr; 1392 assert( pOp->p3<=(p->nMem + 1 - p->nCursor) ); 1393 zErr = sqlite3ValueText(&aMem[pOp->p3], SQLITE_UTF8); 1394 sqlite3VdbeError(p, "%s", zErr); 1395 }else if( pOp->p5 ){ 1396 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", 1397 "FOREIGN KEY" }; 1398 testcase( pOp->p5==1 ); 1399 testcase( pOp->p5==2 ); 1400 testcase( pOp->p5==3 ); 1401 testcase( pOp->p5==4 ); 1402 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]); 1403 if( pOp->p4.z ){ 1404 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z); 1405 } 1406 }else{ 1407 sqlite3VdbeError(p, "%s", pOp->p4.z); 1408 } 1409 sqlite3VdbeLogAbort(p, pOp->p1, pOp, aOp); 1410 } 1411 rc = sqlite3VdbeHalt(p); 1412 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); 1413 if( rc==SQLITE_BUSY ){ 1414 p->rc = SQLITE_BUSY; 1415 }else{ 1416 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); 1417 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); 1418 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; 1419 } 1420 goto vdbe_return; 1421 } 1422 1423 /* Opcode: Integer P1 P2 * * * 1424 ** Synopsis: r[P2]=P1 1425 ** 1426 ** The 32-bit integer value P1 is written into register P2. 1427 */ 1428 case OP_Integer: { /* out2 */ 1429 pOut = out2Prerelease(p, pOp); 1430 pOut->u.i = pOp->p1; 1431 break; 1432 } 1433 1434 /* Opcode: Int64 P1 P2 P3 * * 1435 ** Synopsis: r[P2]=PINT13 1436 ** 1437 ** Combine P1 and P3 into a signed 64-bit integer. P1 is the least 1438 ** significant 32 bits and P3 is the most significant. Store the 1439 ** result in register P2. 1440 */ 1441 case OP_Int64: { /* out2 */ 1442 pOut = out2Prerelease(p, pOp); 1443 pOut->u.i = INT32_TO_64(pOp->p1,pOp->p3); 1444 break; 1445 } 1446 1447 #ifndef SQLITE_OMIT_FLOATING_POINT 1448 /* Opcode: Real P1 P2 P3 * * 1449 ** Synopsis: r[P2]=PDBL13 1450 ** 1451 ** P1 and P3 are combined to 64 bits with P1 being the lower the P3 1452 ** the upper. The result is interpreted as a 64-bit floating-point 1453 ** and stored in register P2. 1454 */ 1455 case OP_Real: { /* same as TK_FLOAT, out2 */ 1456 u64 ii; 1457 pOut = out2Prerelease(p, pOp); 1458 pOut->flags = MEM_Real; 1459 ii = (u64)INT32_TO_64(pOp->p1,pOp->p3); 1460 swapMixedEndianFloat(ii); 1461 memcpy(&pOut->u.r, &ii, 8); 1462 assert( !sqlite3IsNaN(pOut->u.r) ); 1463 break; 1464 } 1465 #endif 1466 1467 /* Opcode: String8 * P2 * P4 * 1468 ** Synopsis: r[P2]='P4' 1469 ** 1470 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 1471 ** into a String opcode before it is executed for the first time. During 1472 ** this transformation, the length of string P4 is computed and stored 1473 ** as the P1 parameter. 1474 */ 1475 case OP_String8: { /* same as TK_STRING, out2 */ 1476 assert( pOp->p4.z!=0 ); 1477 pOut = out2Prerelease(p, pOp); 1478 pOp->p1 = sqlite3Strlen30(pOp->p4.z); 1479 1480 #ifndef SQLITE_OMIT_UTF16 1481 if( encoding!=SQLITE_UTF8 ){ 1482 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); 1483 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG ); 1484 if( rc ) goto too_big; 1485 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; 1486 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); 1487 assert( VdbeMemDynamic(pOut)==0 ); 1488 pOut->szMalloc = 0; 1489 pOut->flags |= MEM_Static; 1490 if( pOp->p4type==P4_DYNAMIC ){ 1491 sqlite3DbFree(db, pOp->p4.z); 1492 } 1493 pOp->p4type = P4_DYNAMIC; 1494 pOp->p4.z = pOut->z; 1495 pOp->p1 = pOut->n; 1496 } 1497 #endif 1498 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 1499 goto too_big; 1500 } 1501 pOp->opcode = OP_String; 1502 assert( rc==SQLITE_OK ); 1503 /* Fall through to the next case, OP_String */ 1504 /* no break */ deliberate_fall_through 1505 } 1506 1507 /* Opcode: String P1 P2 P3 P4 P5 1508 ** Synopsis: r[P2]='P4' (len=P1) 1509 ** 1510 ** The string value P4 of length P1 (bytes) is stored in register P2. 1511 ** 1512 ** If P3 is not zero and the content of register P3 is equal to P5, then 1513 ** the datatype of the register P2 is converted to BLOB. The content is 1514 ** the same sequence of bytes, it is merely interpreted as a BLOB instead 1515 ** of a string, as if it had been CAST. In other words: 1516 ** 1517 ** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB) 1518 */ 1519 case OP_String: { /* out2 */ 1520 assert( pOp->p4.z!=0 ); 1521 pOut = out2Prerelease(p, pOp); 1522 pOut->flags = MEM_Str|MEM_Static|MEM_Term; 1523 pOut->z = pOp->p4.z; 1524 pOut->n = pOp->p1; 1525 pOut->enc = encoding; 1526 UPDATE_MAX_BLOBSIZE(pOut); 1527 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS 1528 if( pOp->p3>0 ){ 1529 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 1530 pIn3 = &aMem[pOp->p3]; 1531 assert( pIn3->flags & MEM_Int ); 1532 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term; 1533 } 1534 #endif 1535 break; 1536 } 1537 1538 /* Opcode: BeginSubrtn * P2 * * * 1539 ** Synopsis: r[P2]=NULL 1540 ** 1541 ** Mark the beginning of a subroutine that can be entered in-line 1542 ** or that can be called using OP_Gosub. The subroutine should 1543 ** be terminated by an OP_Return instruction that has a P1 operand that 1544 ** is the same as the P2 operand to this opcode and that has P3 set to 1. 1545 ** If the subroutine is entered in-line, then the OP_Return will simply 1546 ** fall through. But if the subroutine is entered using OP_Gosub, then 1547 ** the OP_Return will jump back to the first instruction after the OP_Gosub. 1548 ** 1549 ** This routine works by loading a NULL into the P2 register. When the 1550 ** return address register contains a NULL, the OP_Return instruction is 1551 ** a no-op that simply falls through to the next instruction (assuming that 1552 ** the OP_Return opcode has a P3 value of 1). Thus if the subroutine is 1553 ** entered in-line, then the OP_Return will cause in-line execution to 1554 ** continue. But if the subroutine is entered via OP_Gosub, then the 1555 ** OP_Return will cause a return to the address following the OP_Gosub. 1556 ** 1557 ** This opcode is identical to OP_Null. It has a different name 1558 ** only to make the byte code easier to read and verify. 1559 */ 1560 /* Opcode: Null P1 P2 P3 * * 1561 ** Synopsis: r[P2..P3]=NULL 1562 ** 1563 ** Write a NULL into registers P2. If P3 greater than P2, then also write 1564 ** NULL into register P3 and every register in between P2 and P3. If P3 1565 ** is less than P2 (typically P3 is zero) then only register P2 is 1566 ** set to NULL. 1567 ** 1568 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that 1569 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on 1570 ** OP_Ne or OP_Eq. 1571 */ 1572 case OP_BeginSubrtn: 1573 case OP_Null: { /* out2 */ 1574 int cnt; 1575 u16 nullFlag; 1576 pOut = out2Prerelease(p, pOp); 1577 cnt = pOp->p3-pOp->p2; 1578 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 1579 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; 1580 pOut->n = 0; 1581 #ifdef SQLITE_DEBUG 1582 pOut->uTemp = 0; 1583 #endif 1584 while( cnt>0 ){ 1585 pOut++; 1586 memAboutToChange(p, pOut); 1587 sqlite3VdbeMemSetNull(pOut); 1588 pOut->flags = nullFlag; 1589 pOut->n = 0; 1590 cnt--; 1591 } 1592 break; 1593 } 1594 1595 /* Opcode: SoftNull P1 * * * * 1596 ** Synopsis: r[P1]=NULL 1597 ** 1598 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord 1599 ** instruction, but do not free any string or blob memory associated with 1600 ** the register, so that if the value was a string or blob that was 1601 ** previously copied using OP_SCopy, the copies will continue to be valid. 1602 */ 1603 case OP_SoftNull: { 1604 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 1605 pOut = &aMem[pOp->p1]; 1606 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null; 1607 break; 1608 } 1609 1610 /* Opcode: Blob P1 P2 * P4 * 1611 ** Synopsis: r[P2]=P4 (len=P1) 1612 ** 1613 ** P4 points to a blob of data P1 bytes long. Store this 1614 ** blob in register P2. If P4 is a NULL pointer, then construct 1615 ** a zero-filled blob that is P1 bytes long in P2. 1616 */ 1617 case OP_Blob: { /* out2 */ 1618 assert( pOp->p1 <= SQLITE_MAX_LENGTH ); 1619 pOut = out2Prerelease(p, pOp); 1620 if( pOp->p4.z==0 ){ 1621 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1); 1622 if( sqlite3VdbeMemExpandBlob(pOut) ) goto no_mem; 1623 }else{ 1624 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); 1625 } 1626 pOut->enc = encoding; 1627 UPDATE_MAX_BLOBSIZE(pOut); 1628 break; 1629 } 1630 1631 /* Opcode: Variable P1 P2 * * * 1632 ** Synopsis: r[P2]=parameter(P1) 1633 ** 1634 ** Transfer the values of bound parameter P1 into register P2 1635 */ 1636 case OP_Variable: { /* out2 */ 1637 Mem *pVar; /* Value being transferred */ 1638 1639 assert( pOp->p1>0 && pOp->p1<=p->nVar ); 1640 pVar = &p->aVar[pOp->p1 - 1]; 1641 if( sqlite3VdbeMemTooBig(pVar) ){ 1642 goto too_big; 1643 } 1644 pOut = &aMem[pOp->p2]; 1645 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut); 1646 memcpy(pOut, pVar, MEMCELLSIZE); 1647 pOut->flags &= ~(MEM_Dyn|MEM_Ephem); 1648 pOut->flags |= MEM_Static|MEM_FromBind; 1649 UPDATE_MAX_BLOBSIZE(pOut); 1650 break; 1651 } 1652 1653 /* Opcode: Move P1 P2 P3 * * 1654 ** Synopsis: r[P2@P3]=r[P1@P3] 1655 ** 1656 ** Move the P3 values in register P1..P1+P3-1 over into 1657 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are 1658 ** left holding a NULL. It is an error for register ranges 1659 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error 1660 ** for P3 to be less than 1. 1661 */ 1662 case OP_Move: { 1663 int n; /* Number of registers left to copy */ 1664 int p1; /* Register to copy from */ 1665 int p2; /* Register to copy to */ 1666 1667 n = pOp->p3; 1668 p1 = pOp->p1; 1669 p2 = pOp->p2; 1670 assert( n>0 && p1>0 && p2>0 ); 1671 assert( p1+n<=p2 || p2+n<=p1 ); 1672 1673 pIn1 = &aMem[p1]; 1674 pOut = &aMem[p2]; 1675 do{ 1676 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] ); 1677 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] ); 1678 assert( memIsValid(pIn1) ); 1679 memAboutToChange(p, pOut); 1680 sqlite3VdbeMemMove(pOut, pIn1); 1681 #ifdef SQLITE_DEBUG 1682 pIn1->pScopyFrom = 0; 1683 { int i; 1684 for(i=1; inMem; i++){ 1685 if( aMem[i].pScopyFrom==pIn1 ){ 1686 assert( aMem[i].bScopy ); 1687 aMem[i].pScopyFrom = pOut; 1688 } 1689 } 1690 } 1691 #endif 1692 Deephemeralize(pOut); 1693 REGISTER_TRACE(p2++, pOut); 1694 pIn1++; 1695 pOut++; 1696 }while( --n ); 1697 break; 1698 } 1699 1700 /* Opcode: Copy P1 P2 P3 * P5 1701 ** Synopsis: r[P2@P3+1]=r[P1@P3+1] 1702 ** 1703 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. 1704 ** 1705 ** If the 0x0002 bit of P5 is set then also clear the MEM_Subtype flag in the 1706 ** destination. The 0x0001 bit of P5 indicates that this Copy opcode cannot 1707 ** be merged. The 0x0001 bit is used by the query planner and does not 1708 ** come into play during query execution. 1709 ** 1710 ** This instruction makes a deep copy of the value. A duplicate 1711 ** is made of any string or blob constant. See also OP_SCopy. 1712 */ 1713 case OP_Copy: { 1714 int n; 1715 1716 n = pOp->p3; 1717 pIn1 = &aMem[pOp->p1]; 1718 pOut = &aMem[pOp->p2]; 1719 assert( pOut!=pIn1 ); 1720 while( 1 ){ 1721 memAboutToChange(p, pOut); 1722 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); 1723 Deephemeralize(pOut); 1724 if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){ 1725 pOut->flags &= ~MEM_Subtype; 1726 } 1727 #ifdef SQLITE_DEBUG 1728 pOut->pScopyFrom = 0; 1729 #endif 1730 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut); 1731 if( (n--)==0 ) break; 1732 pOut++; 1733 pIn1++; 1734 } 1735 break; 1736 } 1737 1738 /* Opcode: SCopy P1 P2 * * * 1739 ** Synopsis: r[P2]=r[P1] 1740 ** 1741 ** Make a shallow copy of register P1 into register P2. 1742 ** 1743 ** This instruction makes a shallow copy of the value. If the value 1744 ** is a string or blob, then the copy is only a pointer to the 1745 ** original and hence if the original changes so will the copy. 1746 ** Worse, if the original is deallocated, the copy becomes invalid. 1747 ** Thus the program must guarantee that the original will not change 1748 ** during the lifetime of the copy. Use OP_Copy to make a complete 1749 ** copy. 1750 */ 1751 case OP_SCopy: { /* out2 */ 1752 pIn1 = &aMem[pOp->p1]; 1753 pOut = &aMem[pOp->p2]; 1754 assert( pOut!=pIn1 ); 1755 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); 1756 #ifdef SQLITE_DEBUG 1757 pOut->pScopyFrom = pIn1; 1758 pOut->mScopyFlags = pIn1->flags; 1759 pIn1->bScopy = 1; 1760 #endif 1761 break; 1762 } 1763 1764 /* Opcode: IntCopy P1 P2 * * * 1765 ** Synopsis: r[P2]=r[P1] 1766 ** 1767 ** Transfer the integer value held in register P1 into register P2. 1768 ** 1769 ** This is an optimized version of SCopy that works only for integer 1770 ** values. 1771 */ 1772 case OP_IntCopy: { /* out2 */ 1773 pIn1 = &aMem[pOp->p1]; 1774 assert( (pIn1->flags & MEM_Int)!=0 ); 1775 pOut = &aMem[pOp->p2]; 1776 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i); 1777 break; 1778 } 1779 1780 /* Opcode: FkCheck * * * * * 1781 ** 1782 ** Halt with an SQLITE_CONSTRAINT error if there are any unresolved 1783 ** foreign key constraint violations. If there are no foreign key 1784 ** constraint violations, this is a no-op. 1785 ** 1786 ** FK constraint violations are also checked when the prepared statement 1787 ** exits. This opcode is used to raise foreign key constraint errors prior 1788 ** to returning results such as a row change count or the result of a 1789 ** RETURNING clause. 1790 */ 1791 case OP_FkCheck: { 1792 if( (rc = sqlite3VdbeCheckFkImmediate(p))!=SQLITE_OK ){ 1793 goto abort_due_to_error; 1794 } 1795 break; 1796 } 1797 1798 /* Opcode: ResultRow P1 P2 * * * 1799 ** Synopsis: output=r[P1@P2] 1800 ** 1801 ** The registers P1 through P1+P2-1 contain a single row of 1802 ** results. This opcode causes the sqlite3_step() call to terminate 1803 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt 1804 ** structure to provide access to the r(P1)..r(P1+P2-1) values as 1805 ** the result row. 1806 */ 1807 case OP_ResultRow: { 1808 assert( p->nResColumn==pOp->p2 ); 1809 assert( pOp->p1>0 || CORRUPT_DB ); 1810 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); 1811 1812 p->cacheCtr = (p->cacheCtr + 2)|1; 1813 p->pResultRow = &aMem[pOp->p1]; 1814 #ifdef SQLITE_DEBUG 1815 { 1816 Mem *pMem = p->pResultRow; 1817 int i; 1818 for(i=0; ip2; i++){ 1819 assert( memIsValid(&pMem[i]) ); 1820 REGISTER_TRACE(pOp->p1+i, &pMem[i]); 1821 /* The registers in the result will not be used again when the 1822 ** prepared statement restarts. This is because sqlite3_column() 1823 ** APIs might have caused type conversions of made other changes to 1824 ** the register values. Therefore, we can go ahead and break any 1825 ** OP_SCopy dependencies. */ 1826 pMem[i].pScopyFrom = 0; 1827 } 1828 } 1829 #endif 1830 if( db->mallocFailed ) goto no_mem; 1831 if( db->mTrace & SQLITE_TRACE_ROW ){ 1832 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0); 1833 } 1834 p->pc = (int)(pOp - aOp) + 1; 1835 rc = SQLITE_ROW; 1836 goto vdbe_return; 1837 } 1838 1839 /* Opcode: Concat P1 P2 P3 * * 1840 ** Synopsis: r[P3]=r[P2]+r[P1] 1841 ** 1842 ** Add the text in register P1 onto the end of the text in 1843 ** register P2 and store the result in register P3. 1844 ** If either the P1 or P2 text are NULL then store NULL in P3. 1845 ** 1846 ** P3 = P2 || P1 1847 ** 1848 ** It is illegal for P1 and P3 to be the same register. Sometimes, 1849 ** if P3 is the same register as P2, the implementation is able 1850 ** to avoid a memcpy(). 1851 */ 1852 case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ 1853 i64 nByte; /* Total size of the output string or blob */ 1854 u16 flags1; /* Initial flags for P1 */ 1855 u16 flags2; /* Initial flags for P2 */ 1856 1857 pIn1 = &aMem[pOp->p1]; 1858 pIn2 = &aMem[pOp->p2]; 1859 pOut = &aMem[pOp->p3]; 1860 testcase( pOut==pIn2 ); 1861 assert( pIn1!=pOut ); 1862 flags1 = pIn1->flags; 1863 testcase( flags1 & MEM_Null ); 1864 testcase( pIn2->flags & MEM_Null ); 1865 if( (flags1 | pIn2->flags) & MEM_Null ){ 1866 sqlite3VdbeMemSetNull(pOut); 1867 break; 1868 } 1869 if( (flags1 & (MEM_Str|MEM_Blob))==0 ){ 1870 if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem; 1871 flags1 = pIn1->flags & ~MEM_Str; 1872 }else if( (flags1 & MEM_Zero)!=0 ){ 1873 if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem; 1874 flags1 = pIn1->flags & ~MEM_Str; 1875 } 1876 flags2 = pIn2->flags; 1877 if( (flags2 & (MEM_Str|MEM_Blob))==0 ){ 1878 if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem; 1879 flags2 = pIn2->flags & ~MEM_Str; 1880 }else if( (flags2 & MEM_Zero)!=0 ){ 1881 if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem; 1882 flags2 = pIn2->flags & ~MEM_Str; 1883 } 1884 nByte = pIn1->n; 1885 nByte += pIn2->n; 1886 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 1887 goto too_big; 1888 } 1889 #if SQLITE_MAX_LENGTH>`2147483645` 1890 if( nByte>`2147483645` ){ goto too_big; } 1891 #endif 1892 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ 1893 goto no_mem; 1894 } 1895 MemSetTypeFlag(pOut, MEM_Str); 1896 if( pOut!=pIn2 ){ 1897 memcpy(pOut->z, pIn2->z, pIn2->n); 1898 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) ); 1899 pIn2->flags = flags2; 1900 } 1901 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); 1902 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); 1903 pIn1->flags = flags1; 1904 if( encoding>SQLITE_UTF8 ) nByte &= ~1; 1905 pOut->z[nByte]=0; 1906 pOut->z[nByte+1] = 0; 1907 pOut->flags |= MEM_Term; 1908 pOut->n = (int)nByte; 1909 pOut->enc = encoding; 1910 UPDATE_MAX_BLOBSIZE(pOut); 1911 break; 1912 } 1913 1914 /* Opcode: Add P1 P2 P3 * * 1915 ** Synopsis: r[P3]=r[P1]+r[P2] 1916 ** 1917 ** Add the value in register P1 to the value in register P2 1918 ** and store the result in register P3. 1919 ** If either input is NULL, the result is NULL. 1920 */ 1921 /* Opcode: Multiply P1 P2 P3 * * 1922 ** Synopsis: r[P3]=r[P1]*r[P2] 1923 ** 1924 ** 1925 ** Multiply the value in register P1 by the value in register P2 1926 ** and store the result in register P3. 1927 ** If either input is NULL, the result is NULL. 1928 */ 1929 /* Opcode: Subtract P1 P2 P3 * * 1930 ** Synopsis: r[P3]=r[P2]-r[P1] 1931 ** 1932 ** Subtract the value in register P1 from the value in register P2 1933 ** and store the result in register P3. 1934 ** If either input is NULL, the result is NULL. 1935 */ 1936 /* Opcode: Divide P1 P2 P3 * * 1937 ** Synopsis: r[P3]=r[P2]/r[P1] 1938 ** 1939 ** Divide the value in register P1 by the value in register P2 1940 ** and store the result in register P3 (P3=P2/P1). If the value in 1941 ** register P1 is zero, then the result is NULL. If either input is 1942 ** NULL, the result is NULL. 1943 */ 1944 /* Opcode: Remainder P1 P2 P3 * * 1945 ** Synopsis: r[P3]=r[P2]%r[P1] 1946 ** 1947 ** Compute the remainder after integer register P2 is divided by 1948 ** register P1 and store the result in register P3. 1949 ** If the value in register P1 is zero the result is NULL. 1950 ** If either operand is NULL, the result is NULL. 1951 */ 1952 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ 1953 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ 1954 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ 1955 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ 1956 case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ 1957 u16 type1; /* Numeric type of left operand */ 1958 u16 type2; /* Numeric type of right operand */ 1959 i64 iA; /* Integer value of left operand */ 1960 i64 iB; /* Integer value of right operand */ 1961 double rA; /* Real value of left operand */ 1962 double rB; /* Real value of right operand */ 1963 1964 pIn1 = &aMem[pOp->p1]; 1965 type1 = pIn1->flags; 1966 pIn2 = &aMem[pOp->p2]; 1967 type2 = pIn2->flags; 1968 pOut = &aMem[pOp->p3]; 1969 if( (type1 & type2 & MEM_Int)!=0 ){ 1970 int_math: 1971 iA = pIn1->u.i; 1972 iB = pIn2->u.i; 1973 switch( pOp->opcode ){ 1974 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; 1975 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; 1976 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break; 1977 case OP_Divide: { 1978 if( iA==0 ) goto arithmetic_result_is_null; 1979 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math; 1980 iB /= iA; 1981 break; 1982 } 1983 default: { 1984 if( iA==0 ) goto arithmetic_result_is_null; 1985 if( iA==-1 ) iA = 1; 1986 iB %= iA; 1987 break; 1988 } 1989 } 1990 pOut->u.i = iB; 1991 MemSetTypeFlag(pOut, MEM_Int); 1992 }else if( ((type1 | type2) & MEM_Null)!=0 ){ 1993 goto arithmetic_result_is_null; 1994 }else{ 1995 type1 = numericType(pIn1); 1996 type2 = numericType(pIn2); 1997 if( (type1 & type2 & MEM_Int)!=0 ) goto int_math; 1998 fp_math: 1999 rA = sqlite3VdbeRealValue(pIn1); 2000 rB = sqlite3VdbeRealValue(pIn2); 2001 switch( pOp->opcode ){ 2002 case OP_Add: rB += rA; break; 2003 case OP_Subtract: rB -= rA; break; 2004 case OP_Multiply: rB *= rA; break; 2005 case OP_Divide: { 2006 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ 2007 if( rA==(double)0 ) goto arithmetic_result_is_null; 2008 rB /= rA; 2009 break; 2010 } 2011 default: { 2012 iA = sqlite3VdbeIntValue(pIn1); 2013 iB = sqlite3VdbeIntValue(pIn2); 2014 if( iA==0 ) goto arithmetic_result_is_null; 2015 if( iA==-1 ) iA = 1; 2016 rB = (double)(iB % iA); 2017 break; 2018 } 2019 } 2020 #ifdef SQLITE_OMIT_FLOATING_POINT 2021 pOut->u.i = rB; 2022 MemSetTypeFlag(pOut, MEM_Int); 2023 #else 2024 if( sqlite3IsNaN(rB) ){ 2025 goto arithmetic_result_is_null; 2026 } 2027 pOut->u.r = rB; 2028 MemSetTypeFlag(pOut, MEM_Real); 2029 #endif 2030 } 2031 break; 2032 2033 arithmetic_result_is_null: 2034 sqlite3VdbeMemSetNull(pOut); 2035 break; 2036 } 2037 2038 /* Opcode: CollSeq P1 * * P4 2039 ** 2040 ** P4 is a pointer to a CollSeq object. If the next call to a user function 2041 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will 2042 ** be returned. This is used by the built-in min(), max() and nullif() 2043 ** functions. 2044 ** 2045 ** If P1 is not zero, then it is a register that a subsequent min() or 2046 ** max() aggregate will set to 1 if the current row is not the minimum or 2047 ** maximum. The P1 register is initialized to 0 by this instruction. 2048 ** 2049 ** The interface used by the implementation of the aforementioned functions 2050 ** to retrieve the collation sequence set by this opcode is not available 2051 ** publicly. Only built-in functions have access to this feature. 2052 */ 2053 case OP_CollSeq: { 2054 assert( pOp->p4type==P4_COLLSEQ ); 2055 if( pOp->p1 ){ 2056 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); 2057 } 2058 break; 2059 } 2060 2061 /* Opcode: BitAnd P1 P2 P3 * * 2062 ** Synopsis: r[P3]=r[P1]&r[P2] 2063 ** 2064 ** Take the bit-wise AND of the values in register P1 and P2 and 2065 ** store the result in register P3. 2066 ** If either input is NULL, the result is NULL. 2067 */ 2068 /* Opcode: BitOr P1 P2 P3 * * 2069 ** Synopsis: r[P3]=r[P1]|r[P2] 2070 ** 2071 ** Take the bit-wise OR of the values in register P1 and P2 and 2072 ** store the result in register P3. 2073 ** If either input is NULL, the result is NULL. 2074 */ 2075 /* Opcode: ShiftLeft P1 P2 P3 * * 2076 ** Synopsis: r[P3]=r[P2]<>r[P1] 2085 ** 2086 ** Shift the integer value in register P2 to the right by the 2087 ** number of bits specified by the integer in register P1. 2088 ** Store the result in register P3. 2089 ** If either input is NULL, the result is NULL. 2090 */ 2091 case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ 2092 case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ 2093 case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ 2094 case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ 2095 i64 iA; 2096 u64 uA; 2097 i64 iB; 2098 u8 op; 2099 2100 pIn1 = &aMem[pOp->p1]; 2101 pIn2 = &aMem[pOp->p2]; 2102 pOut = &aMem[pOp->p3]; 2103 if( (pIn1->flags | pIn2->flags) & MEM_Null ){ 2104 sqlite3VdbeMemSetNull(pOut); 2105 break; 2106 } 2107 iA = sqlite3VdbeIntValue(pIn2); 2108 iB = sqlite3VdbeIntValue(pIn1); 2109 op = pOp->opcode; 2110 if( op==OP_BitAnd ){ 2111 iA &= iB; 2112 }else if( op==OP_BitOr ){ 2113 iA |= iB; 2114 }else if( iB!=0 ){ 2115 assert( op==OP_ShiftRight || op==OP_ShiftLeft ); 2116 2117 /* If shifting by a negative amount, shift in the other direction */ 2118 if( iB<0 ){ 2119 assert( OP_ShiftRight==OP_ShiftLeft+1 ); 2120 op = 2*OP_ShiftLeft + 1 - op; 2121 iB = iB>(-64) ? -iB : 64; 2122 } 2123 2124 if( iB>=64 ){ 2125 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1; 2126 }else{ 2127 memcpy(&uA, &iA, sizeof(uA)); 2128 if( op==OP_ShiftLeft ){ 2129 uA <<= iB; 2130 }else{ 2131 uA >>= iB; 2132 /* Sign-extend on a right shift of a negative number */ 2133 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB); 2134 } 2135 memcpy(&iA, &uA, sizeof(iA)); 2136 } 2137 } 2138 pOut->u.i = iA; 2139 MemSetTypeFlag(pOut, MEM_Int); 2140 break; 2141 } 2142 2143 /* Opcode: AddImm P1 P2 * * * 2144 ** Synopsis: r[P1]=r[P1]+P2 2145 ** 2146 ** Add the constant P2 to the value in register P1. 2147 ** The result is always an integer. 2148 ** 2149 ** To force any register to be an integer, just add 0. 2150 */ 2151 case OP_AddImm: { /* in1 */ 2152 pIn1 = &aMem[pOp->p1]; 2153 memAboutToChange(p, pIn1); 2154 sqlite3VdbeMemIntegerify(pIn1); 2155 *(u64*)&pIn1->u.i += (u64)pOp->p2; 2156 break; 2157 } 2158 2159 /* Opcode: MustBeInt P1 P2 * * * 2160 ** 2161 ** Force the value in register P1 to be an integer. If the value 2162 ** in P1 is not an integer and cannot be converted into an integer 2163 ** without data loss, then jump immediately to P2, or if P2==0 2164 ** raise an SQLITE_MISMATCH exception. 2165 */ 2166 case OP_MustBeInt: { /* jump0, in1 */ 2167 pIn1 = &aMem[pOp->p1]; 2168 if( (pIn1->flags & MEM_Int)==0 ){ 2169 if( (pIn1->flags & MEM_Real)!=0 ){ 2170 sqlite3_int64 ix; 2171 if( pIn1->u.r>=-`9223372036854775808`.0 2172 && pIn1->u.r<+`9223372036854775808`.0 2173 && (double)(ix = sqlite3RealToI64(pIn1->u.r))==pIn1->u.r 2174 ){ 2175 pIn1->u.i = ix; 2176 MemSetTypeFlag(pIn1, MEM_Int); 2177 } 2178 }else{ 2179 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); 2180 } 2181 if( (pIn1->flags & MEM_Int)==0 ){ 2182 VdbeBranchTaken(1, 2); 2183 if( pOp->p2==0 ){ 2184 rc = SQLITE_MISMATCH; 2185 goto abort_due_to_error; 2186 }else{ 2187 goto jump_to_p2; 2188 } 2189 } 2190 } 2191 VdbeBranchTaken(0, 2); 2192 MemSetTypeFlag(pIn1, MEM_Int); 2193 break; 2194 } 2195 2196 #ifndef SQLITE_OMIT_FLOATING_POINT 2197 /* Opcode: RealAffinity P1 * * * * 2198 ** 2199 ** If register P1 holds an integer convert it to a real value. 2200 ** 2201 ** This opcode is used when extracting information from a column that 2202 ** has REAL affinity. Such column values may still be stored as 2203 ** integers, for space efficiency, but after extraction we want them 2204 ** to have only a real value. 2205 */ 2206 case OP_RealAffinity: { /* in1 */ 2207 pIn1 = &aMem[pOp->p1]; 2208 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){ 2209 testcase( pIn1->flags & MEM_Int ); 2210 testcase( pIn1->flags & MEM_IntReal ); 2211 sqlite3VdbeMemRealify(pIn1); 2212 REGISTER_TRACE(pOp->p1, pIn1); 2213 } 2214 break; 2215 } 2216 #endif 2217 2218 #if !defined(SQLITE_OMIT_CAST) || !defined(SQLITE_OMIT_ANALYZE) 2219 /* Opcode: Cast P1 P2 * * * 2220 ** Synopsis: affinity(r[P1]) 2221 ** 2222 ** Force the value in register P1 to be the type defined by P2. 2223 ** 2224 **
    2225 **
  • P2=='A' → BLOB 2226 **
  • P2=='B' → TEXT 2227 **
  • P2=='C' → NUMERIC 2228 **
  • P2=='D' → INTEGER 2229 **
  • P2=='E' → REAL 2230 **
2231 ** 2232 ** A NULL value is not changed by this routine. It remains NULL. 2233 */ 2234 case OP_Cast: { /* in1 */ 2235 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL ); 2236 testcase( pOp->p2==SQLITE_AFF_TEXT ); 2237 testcase( pOp->p2==SQLITE_AFF_BLOB ); 2238 testcase( pOp->p2==SQLITE_AFF_NUMERIC ); 2239 testcase( pOp->p2==SQLITE_AFF_INTEGER ); 2240 testcase( pOp->p2==SQLITE_AFF_REAL ); 2241 pIn1 = &aMem[pOp->p1]; 2242 memAboutToChange(p, pIn1); 2243 rc = ExpandBlob(pIn1); 2244 if( rc ) goto abort_due_to_error; 2245 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); 2246 if( rc ) goto abort_due_to_error; 2247 UPDATE_MAX_BLOBSIZE(pIn1); 2248 REGISTER_TRACE(pOp->p1, pIn1); 2249 break; 2250 } 2251 #endif /* SQLITE_OMIT_CAST */ 2252 2253 /* Opcode: Eq P1 P2 P3 P4 P5 2254 ** Synopsis: IF r[P3]==r[P1] 2255 ** 2256 ** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then 2257 ** jump to address P2. 2258 ** 2259 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - 2260 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 2261 ** to coerce both inputs according to this affinity before the 2262 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric 2263 ** affinity is used. Note that the affinity conversions are stored 2264 ** back into the input registers P1 and P3. So this opcode can cause 2265 ** persistent changes to registers P1 and P3. 2266 ** 2267 ** Once any conversions have taken place, and neither value is NULL, 2268 ** the values are compared. If both values are blobs then memcmp() is 2269 ** used to determine the results of the comparison. If both values 2270 ** are text, then the appropriate collating function specified in 2271 ** P4 is used to do the comparison. If P4 is not specified then 2272 ** memcmp() is used to compare text string. If both values are 2273 ** numeric, then a numeric comparison is used. If the two values 2274 ** are of different types, then numbers are considered less than 2275 ** strings and strings are considered less than blobs. 2276 ** 2277 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either 2278 ** true or false and is never NULL. If both operands are NULL then the result 2279 ** of comparison is true. If either operand is NULL then the result is false. 2280 ** If neither operand is NULL the result is the same as it would be if 2281 ** the SQLITE_NULLEQ flag were omitted from P5. 2282 ** 2283 ** This opcode saves the result of comparison for use by the new 2284 ** OP_Jump opcode. 2285 */ 2286 /* Opcode: Ne P1 P2 P3 P4 P5 2287 ** Synopsis: IF r[P3]!=r[P1] 2288 ** 2289 ** This works just like the Eq opcode except that the jump is taken if 2290 ** the operands in registers P1 and P3 are not equal. See the Eq opcode for 2291 ** additional information. 2292 */ 2293 /* Opcode: Lt P1 P2 P3 P4 P5 2294 ** Synopsis: IF r[P3]r[P1] 2333 ** 2334 ** This works just like the Lt opcode except that the jump is taken if 2335 ** the content of register P3 is greater than the content of 2336 ** register P1. See the Lt opcode for additional information. 2337 */ 2338 /* Opcode: Ge P1 P2 P3 P4 P5 2339 ** Synopsis: IF r[P3]>=r[P1] 2340 ** 2341 ** This works just like the Lt opcode except that the jump is taken if 2342 ** the content of register P3 is greater than or equal to the content of 2343 ** register P1. See the Lt opcode for additional information. 2344 */ 2345 case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ 2346 case OP_Ne: /* same as TK_NE, jump, in1, in3 */ 2347 case OP_Lt: /* same as TK_LT, jump, in1, in3 */ 2348 case OP_Le: /* same as TK_LE, jump, in1, in3 */ 2349 case OP_Gt: /* same as TK_GT, jump, in1, in3 */ 2350 case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ 2351 int res, res2; /* Result of the comparison of pIn1 against pIn3 */ 2352 char affinity; /* Affinity to use for comparison */ 2353 u16 flags1; /* Copy of initial value of pIn1->flags */ 2354 u16 flags3; /* Copy of initial value of pIn3->flags */ 2355 2356 pIn1 = &aMem[pOp->p1]; 2357 pIn3 = &aMem[pOp->p3]; 2358 flags1 = pIn1->flags; 2359 flags3 = pIn3->flags; 2360 if( (flags1 & flags3 & MEM_Int)!=0 ){ 2361 /* Common case of comparison of two integers */ 2362 if( pIn3->u.i > pIn1->u.i ){ 2363 if( sqlite3aGTb[pOp->opcode] ){ 2364 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); 2365 goto jump_to_p2; 2366 } 2367 iCompare = +1; 2368 VVA_ONLY( iCompareIsInit = 1; ) 2369 }else if( pIn3->u.i < pIn1->u.i ){ 2370 if( sqlite3aLTb[pOp->opcode] ){ 2371 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); 2372 goto jump_to_p2; 2373 } 2374 iCompare = -1; 2375 VVA_ONLY( iCompareIsInit = 1; ) 2376 }else{ 2377 if( sqlite3aEQb[pOp->opcode] ){ 2378 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); 2379 goto jump_to_p2; 2380 } 2381 iCompare = 0; 2382 VVA_ONLY( iCompareIsInit = 1; ) 2383 } 2384 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3); 2385 break; 2386 } 2387 if( (flags1 | flags3)&MEM_Null ){ 2388 /* One or both operands are NULL */ 2389 if( pOp->p5 & SQLITE_NULLEQ ){ 2390 /* If SQLITE_NULLEQ is set (which will only happen if the operator is 2391 ** OP_Eq or OP_Ne) then take the jump or not depending on whether 2392 ** or not both operands are null. 2393 */ 2394 assert( (flags1 & MEM_Cleared)==0 ); 2395 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB ); 2396 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 ); 2397 if( (flags1&flags3&MEM_Null)!=0 2398 && (flags3&MEM_Cleared)==0 2399 ){ 2400 res = 0; /* Operands are equal */ 2401 }else{ 2402 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */ 2403 } 2404 }else{ 2405 /* SQLITE_NULLEQ is clear and at least one operand is NULL, 2406 ** then the result is always NULL. 2407 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. 2408 */ 2409 VdbeBranchTaken(2,3); 2410 if( pOp->p5 & SQLITE_JUMPIFNULL ){ 2411 goto jump_to_p2; 2412 } 2413 iCompare = 1; /* Operands are not equal */ 2414 VVA_ONLY( iCompareIsInit = 1; ) 2415 break; 2416 } 2417 }else{ 2418 /* Neither operand is NULL and we couldn't do the special high-speed 2419 ** integer comparison case. So do a general-case comparison. */ 2420 affinity = pOp->p5 & SQLITE_AFF_MASK; 2421 if( affinity>=SQLITE_AFF_NUMERIC ){ 2422 if( (flags1 | flags3)&MEM_Str ){ 2423 if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ 2424 applyNumericAffinity(pIn1,0); 2425 assert( flags3==pIn3->flags || CORRUPT_DB ); 2426 flags3 = pIn3->flags; 2427 } 2428 if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ 2429 applyNumericAffinity(pIn3,0); 2430 } 2431 } 2432 }else if( affinity==SQLITE_AFF_TEXT && ((flags1 | flags3) & MEM_Str)!=0 ){ 2433 if( (flags1 & MEM_Str)!=0 ){ 2434 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal); 2435 }else if( (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ 2436 testcase( pIn1->flags & MEM_Int ); 2437 testcase( pIn1->flags & MEM_Real ); 2438 testcase( pIn1->flags & MEM_IntReal ); 2439 sqlite3VdbeMemStringify(pIn1, encoding, 1); 2440 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); 2441 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); 2442 if( NEVER(pIn1==pIn3) ) flags3 = flags1 | MEM_Str; 2443 } 2444 if( (flags3 & MEM_Str)!=0 ){ 2445 pIn3->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal); 2446 }else if( (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ 2447 testcase( pIn3->flags & MEM_Int ); 2448 testcase( pIn3->flags & MEM_Real ); 2449 testcase( pIn3->flags & MEM_IntReal ); 2450 sqlite3VdbeMemStringify(pIn3, encoding, 1); 2451 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); 2452 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); 2453 } 2454 } 2455 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); 2456 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); 2457 } 2458 2459 /* At this point, res is negative, zero, or positive if reg[P1] is 2460 ** less than, equal to, or greater than reg[P3], respectively. Compute 2461 ** the answer to this operator in res2, depending on what the comparison 2462 ** operator actually is. The next block of code depends on the fact 2463 ** that the 6 comparison operators are consecutive integers in this 2464 ** order: NE, EQ, GT, LE, LT, GE */ 2465 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 ); 2466 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 ); 2467 if( res<0 ){ 2468 res2 = sqlite3aLTb[pOp->opcode]; 2469 }else if( res==0 ){ 2470 res2 = sqlite3aEQb[pOp->opcode]; 2471 }else{ 2472 res2 = sqlite3aGTb[pOp->opcode]; 2473 } 2474 iCompare = res; 2475 VVA_ONLY( iCompareIsInit = 1; ) 2476 2477 /* Undo any changes made by applyAffinity() to the input registers. */ 2478 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); 2479 pIn3->flags = flags3; 2480 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); 2481 pIn1->flags = flags1; 2482 2483 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); 2484 if( res2 ){ 2485 goto jump_to_p2; 2486 } 2487 break; 2488 } 2489 2490 /* Opcode: ElseEq * P2 * * * 2491 ** 2492 ** This opcode must follow an OP_Lt or OP_Gt comparison operator. There 2493 ** can be zero or more OP_ReleaseReg opcodes intervening, but no other 2494 ** opcodes are allowed to occur between this instruction and the previous 2495 ** OP_Lt or OP_Gt. 2496 ** 2497 ** If the result of an OP_Eq comparison on the same two operands as 2498 ** the prior OP_Lt or OP_Gt would have been true, then jump to P2. If 2499 ** the result of an OP_Eq comparison on the two previous operands 2500 ** would have been false or NULL, then fall through. 2501 */ 2502 case OP_ElseEq: { /* same as TK_ESCAPE, jump */ 2503 2504 #ifdef SQLITE_DEBUG 2505 /* Verify the preconditions of this opcode - that it follows an OP_Lt or 2506 ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */ 2507 int iAddr; 2508 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){ 2509 if( aOp[iAddr].opcode==OP_ReleaseReg ) continue; 2510 assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt ); 2511 break; 2512 } 2513 #endif /* SQLITE_DEBUG */ 2514 assert( iCompareIsInit ); 2515 VdbeBranchTaken(iCompare==0, 2); 2516 if( iCompare==0 ) goto jump_to_p2; 2517 break; 2518 } 2519 2520 2521 /* Opcode: Permutation * * * P4 * 2522 ** 2523 ** Set the permutation used by the OP_Compare operator in the next 2524 ** instruction. The permutation is stored in the P4 operand. 2525 ** 2526 ** The permutation is only valid for the next opcode which must be 2527 ** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5. 2528 ** 2529 ** The first integer in the P4 integer array is the length of the array 2530 ** and does not become part of the permutation. 2531 */ 2532 case OP_Permutation: { 2533 assert( pOp->p4type==P4_INTARRAY ); 2534 assert( pOp->p4.ai ); 2535 assert( pOp[1].opcode==OP_Compare ); 2536 assert( pOp[1].p5 & OPFLAG_PERMUTE ); 2537 break; 2538 } 2539 2540 /* Opcode: Compare P1 P2 P3 P4 P5 2541 ** Synopsis: r[P1@P3] <-> r[P2@P3] 2542 ** 2543 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this 2544 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of 2545 ** the comparison for use by the next OP_Jump instruct. 2546 ** 2547 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is 2548 ** determined by the most recent OP_Permutation operator. If the 2549 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential 2550 ** order. 2551 ** 2552 ** P4 is a KeyInfo structure that defines collating sequences and sort 2553 ** orders for the comparison. The permutation applies to registers 2554 ** only. The KeyInfo elements are used sequentially. 2555 ** 2556 ** The comparison is a sort comparison, so NULLs compare equal, 2557 ** NULLs are less than numbers, numbers are less than strings, 2558 ** and strings are less than blobs. 2559 ** 2560 ** This opcode must be immediately followed by an OP_Jump opcode. 2561 */ 2562 case OP_Compare: { 2563 int n; 2564 int i; 2565 int p1; 2566 int p2; 2567 const KeyInfo *pKeyInfo; 2568 u32 idx; 2569 CollSeq *pColl; /* Collating sequence to use on this term */ 2570 int bRev; /* True for DESCENDING sort order */ 2571 u32 *aPermute; /* The permutation */ 2572 2573 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){ 2574 aPermute = 0; 2575 }else{ 2576 assert( pOp>aOp ); 2577 assert( pOp[-1].opcode==OP_Permutation ); 2578 assert( pOp[-1].p4type==P4_INTARRAY ); 2579 aPermute = pOp[-1].p4.ai + 1; 2580 assert( aPermute!=0 ); 2581 } 2582 n = pOp->p3; 2583 pKeyInfo = pOp->p4.pKeyInfo; 2584 assert( n>0 ); 2585 assert( pKeyInfo!=0 ); 2586 assert( pKeyInfo->aSortFlags!=0 ); 2587 p1 = pOp->p1; 2588 p2 = pOp->p2; 2589 #ifdef SQLITE_DEBUG 2590 if( aPermute ){ 2591 int k, mx = 0; 2592 for(k=0; k(u32)mx ) mx = aPermute[k]; 2593 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 ); 2594 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 ); 2595 }else{ 2596 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 ); 2597 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 ); 2598 } 2599 #endif /* SQLITE_DEBUG */ 2600 for(i=0; inKeyField ); 2607 pColl = pKeyInfo->aColl[i]; 2608 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC); 2609 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); 2610 VVA_ONLY( iCompareIsInit = 1; ) 2611 if( iCompare ){ 2612 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) 2613 && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null)) 2614 ){ 2615 iCompare = -iCompare; 2616 } 2617 if( bRev ) iCompare = -iCompare; 2618 break; 2619 } 2620 } 2621 assert( pOp[1].opcode==OP_Jump ); 2622 break; 2623 } 2624 2625 /* Opcode: Jump P1 P2 P3 * * 2626 ** 2627 ** Jump to the instruction at address P1, P2, or P3 depending on whether 2628 ** in the most recent OP_Compare instruction the P1 vector was less than, 2629 ** equal to, or greater than the P2 vector, respectively. 2630 ** 2631 ** This opcode must immediately follow an OP_Compare opcode. 2632 */ 2633 case OP_Jump: { /* jump */ 2634 assert( pOp>aOp && pOp[-1].opcode==OP_Compare ); 2635 assert( iCompareIsInit ); 2636 if( iCompare<0 ){ 2637 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1]; 2638 }else if( iCompare==0 ){ 2639 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1]; 2640 }else{ 2641 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1]; 2642 } 2643 break; 2644 } 2645 2646 /* Opcode: And P1 P2 P3 * * 2647 ** Synopsis: r[P3]=(r[P1] && r[P2]) 2648 ** 2649 ** Take the logical AND of the values in registers P1 and P2 and 2650 ** write the result into register P3. 2651 ** 2652 ** If either P1 or P2 is 0 (false) then the result is 0 even if 2653 ** the other input is NULL. A NULL and true or two NULLs give 2654 ** a NULL output. 2655 */ 2656 /* Opcode: Or P1 P2 P3 * * 2657 ** Synopsis: r[P3]=(r[P1] || r[P2]) 2658 ** 2659 ** Take the logical OR of the values in register P1 and P2 and 2660 ** store the answer in register P3. 2661 ** 2662 ** If either P1 or P2 is nonzero (true) then the result is 1 (true) 2663 ** even if the other input is NULL. A NULL and false or two NULLs 2664 ** give a NULL output. 2665 */ 2666 case OP_And: /* same as TK_AND, in1, in2, out3 */ 2667 case OP_Or: { /* same as TK_OR, in1, in2, out3 */ 2668 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ 2669 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ 2670 2671 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2); 2672 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2); 2673 if( pOp->opcode==OP_And ){ 2674 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; 2675 v1 = and_logic[v1*3+v2]; 2676 }else{ 2677 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 }; 2678 v1 = or_logic[v1*3+v2]; 2679 } 2680 pOut = &aMem[pOp->p3]; 2681 if( v1==2 ){ 2682 MemSetTypeFlag(pOut, MEM_Null); 2683 }else{ 2684 pOut->u.i = v1; 2685 MemSetTypeFlag(pOut, MEM_Int); 2686 } 2687 break; 2688 } 2689 2690 /* Opcode: IsTrue P1 P2 P3 P4 * 2691 ** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 2692 ** 2693 ** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and 2694 ** IS NOT FALSE operators. 2695 ** 2696 ** Interpret the value in register P1 as a boolean value. Store that 2697 ** boolean (a 0 or 1) in register P2. Or if the value in register P1 is 2698 ** NULL, then the P3 is stored in register P2. Invert the answer if P4 2699 ** is 1. 2700 ** 2701 ** The logic is summarized like this: 2702 ** 2703 **
    2704 **
  • If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE 2705 **
  • If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE 2706 **
  • If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE 2707 **
  • If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE 2708 **
2709 */ 2710 case OP_IsTrue: { /* in1, out2 */ 2711 assert( pOp->p4type==P4_INT32 ); 2712 assert( pOp->p4.i==0 || pOp->p4.i==1 ); 2713 assert( pOp->p3==0 || pOp->p3==1 ); 2714 sqlite3VdbeMemSetInt64(&aMem[pOp->p2], 2715 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i); 2716 break; 2717 } 2718 2719 /* Opcode: Not P1 P2 * * * 2720 ** Synopsis: r[P2]= !r[P1] 2721 ** 2722 ** Interpret the value in register P1 as a boolean value. Store the 2723 ** boolean complement in register P2. If the value in register P1 is 2724 ** NULL, then a NULL is stored in P2. 2725 */ 2726 case OP_Not: { /* same as TK_NOT, in1, out2 */ 2727 pIn1 = &aMem[pOp->p1]; 2728 pOut = &aMem[pOp->p2]; 2729 if( (pIn1->flags & MEM_Null)==0 ){ 2730 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0)); 2731 }else{ 2732 sqlite3VdbeMemSetNull(pOut); 2733 } 2734 break; 2735 } 2736 2737 /* Opcode: BitNot P1 P2 * * * 2738 ** Synopsis: r[P2]= ~r[P1] 2739 ** 2740 ** Interpret the content of register P1 as an integer. Store the 2741 ** ones-complement of the P1 value into register P2. If P1 holds 2742 ** a NULL then store a NULL in P2. 2743 */ 2744 case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ 2745 pIn1 = &aMem[pOp->p1]; 2746 pOut = &aMem[pOp->p2]; 2747 sqlite3VdbeMemSetNull(pOut); 2748 if( (pIn1->flags & MEM_Null)==0 ){ 2749 pOut->flags = MEM_Int; 2750 pOut->u.i = ~sqlite3VdbeIntValue(pIn1); 2751 } 2752 break; 2753 } 2754 2755 /* Opcode: Once P1 P2 P3 * * 2756 ** 2757 ** Fall through to the next instruction the first time this opcode is 2758 ** encountered on each invocation of the byte-code program. Jump to P2 2759 ** on the second and all subsequent encounters during the same invocation. 2760 ** 2761 ** Top-level programs determine first invocation by comparing the P1 2762 ** operand against the P1 operand on the OP_Init opcode at the beginning 2763 ** of the program. If the P1 values differ, then fall through and make 2764 ** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are 2765 ** the same then take the jump. 2766 ** 2767 ** For subprograms, there is a bitmask in the VdbeFrame that determines 2768 ** whether or not the jump should be taken. The bitmask is necessary 2769 ** because the self-altering code trick does not work for recursive 2770 ** triggers. 2771 ** 2772 ** The P3 operand is not used directly by this opcode. However P3 is 2773 ** used by the code generator as follows: If this opcode is the start 2774 ** of a subroutine and that subroutine uses a Bloom filter, then P3 will 2775 ** be the register that holds that Bloom filter. See tag-`202407032019` 2776 ** in the source code for implementation details. 2777 */ 2778 case OP_Once: { /* jump */ 2779 u32 iAddr; /* Address of this instruction */ 2780 assert( p->aOp[0].opcode==OP_Init ); 2781 if( p->pFrame ){ 2782 iAddr = (int)(pOp - p->aOp); 2783 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){ 2784 VdbeBranchTaken(1, 2); 2785 goto jump_to_p2; 2786 } 2787 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7); 2788 }else{ 2789 if( p->aOp[0].p1==pOp->p1 ){ 2790 VdbeBranchTaken(1, 2); 2791 goto jump_to_p2; 2792 } 2793 } 2794 VdbeBranchTaken(0, 2); 2795 pOp->p1 = p->aOp[0].p1; 2796 break; 2797 } 2798 2799 /* Opcode: If P1 P2 P3 * * 2800 ** 2801 ** Jump to P2 if the value in register P1 is true. The value 2802 ** is considered true if it is numeric and non-zero. If the value 2803 ** in P1 is NULL then take the jump if and only if P3 is non-zero. 2804 */ 2805 case OP_If: { /* jump, in1 */ 2806 int c; 2807 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3); 2808 VdbeBranchTaken(c!=0, 2); 2809 if( c ) goto jump_to_p2; 2810 break; 2811 } 2812 2813 /* Opcode: IfNot P1 P2 P3 * * 2814 ** 2815 ** Jump to P2 if the value in register P1 is False. The value 2816 ** is considered false if it has a numeric value of zero. If the value 2817 ** in P1 is NULL then take the jump if and only if P3 is non-zero. 2818 */ 2819 case OP_IfNot: { /* jump, in1 */ 2820 int c; 2821 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3); 2822 VdbeBranchTaken(c!=0, 2); 2823 if( c ) goto jump_to_p2; 2824 break; 2825 } 2826 2827 /* Opcode: IsNull P1 P2 * * * 2828 ** Synopsis: if r[P1]==NULL goto P2 2829 ** 2830 ** Jump to P2 if the value in register P1 is NULL. 2831 */ 2832 case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ 2833 pIn1 = &aMem[pOp->p1]; 2834 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); 2835 if( (pIn1->flags & MEM_Null)!=0 ){ 2836 goto jump_to_p2; 2837 } 2838 break; 2839 } 2840 2841 /* Opcode: IsType P1 P2 P3 P4 P5 2842 ** Synopsis: if typeof(P1.P3) in P5 goto P2 2843 ** 2844 ** Jump to P2 if the type of a column in a btree is one of the types specified 2845 ** by the P5 bitmask. 2846 ** 2847 ** P1 is normally a cursor on a btree for which the row decode cache is 2848 ** valid through at least column P3. In other words, there should have been 2849 ** a prior OP_Column for column P3 or greater. If the cursor is not valid, 2850 ** then this opcode might give spurious results. 2851 ** The the btree row has fewer than P3 columns, then use P4 as the 2852 ** datatype. 2853 ** 2854 ** If P1 is -1, then P3 is a register number and the datatype is taken 2855 ** from the value in that register. 2856 ** 2857 ** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant 2858 ** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04. 2859 ** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10. 2860 ** 2861 ** WARNING: This opcode does not reliably distinguish between NULL and REAL 2862 ** when P1>=0. If the database contains a NaN value, this opcode will think 2863 ** that the datatype is REAL when it should be NULL. When P1<0 and the value 2864 ** is already stored in register P3, then this opcode does reliably 2865 ** distinguish between NULL and REAL. The problem only arises then P1>=0. 2866 ** 2867 ** Take the jump to address P2 if and only if the datatype of the 2868 ** value determined by P1 and P3 corresponds to one of the bits in the 2869 ** P5 bitmask. 2870 ** 2871 */ 2872 case OP_IsType: { /* jump */ 2873 VdbeCursor *pC; 2874 u16 typeMask; 2875 u32 serialType; 2876 2877 assert( pOp->p1>=(-1) && pOp->p1nCursor ); 2878 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) ); 2879 if( pOp->p1>=0 ){ 2880 pC = p->apCsr[pOp->p1]; 2881 assert( pC!=0 ); 2882 assert( pOp->p3>=0 ); 2883 if( pOp->p3nHdrParsed ){ 2884 serialType = pC->aType[pOp->p3]; 2885 if( serialType>=12 ){ 2886 if( serialType&1 ){ 2887 typeMask = 0x04; /* SQLITE_TEXT */ 2888 }else{ 2889 typeMask = 0x08; /* SQLITE_BLOB */ 2890 } 2891 }else{ 2892 static const unsigned char aMask[] = { 2893 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2, 2894 0x01, 0x01, 0x10, 0x10 2895 }; 2896 testcase( serialType==0 ); 2897 testcase( serialType==1 ); 2898 testcase( serialType==2 ); 2899 testcase( serialType==3 ); 2900 testcase( serialType==4 ); 2901 testcase( serialType==5 ); 2902 testcase( serialType==6 ); 2903 testcase( serialType==7 ); 2904 testcase( serialType==8 ); 2905 testcase( serialType==9 ); 2906 testcase( serialType==10 ); 2907 testcase( serialType==11 ); 2908 typeMask = aMask[serialType]; 2909 } 2910 }else{ 2911 typeMask = 1 << (pOp->p4.i - 1); 2912 testcase( typeMask==0x01 ); 2913 testcase( typeMask==0x02 ); 2914 testcase( typeMask==0x04 ); 2915 testcase( typeMask==0x08 ); 2916 testcase( typeMask==0x10 ); 2917 } 2918 }else{ 2919 assert( memIsValid(&aMem[pOp->p3]) ); 2920 typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1); 2921 testcase( typeMask==0x01 ); 2922 testcase( typeMask==0x02 ); 2923 testcase( typeMask==0x04 ); 2924 testcase( typeMask==0x08 ); 2925 testcase( typeMask==0x10 ); 2926 } 2927 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2); 2928 if( typeMask & pOp->p5 ){ 2929 goto jump_to_p2; 2930 } 2931 break; 2932 } 2933 2934 /* Opcode: ZeroOrNull P1 P2 P3 * * 2935 ** Synopsis: r[P2] = 0 OR NULL 2936 ** 2937 ** If both registers P1 and P3 are NOT NULL, then store a zero in 2938 ** register P2. If either registers P1 or P3 are NULL then put 2939 ** a NULL in register P2. 2940 */ 2941 case OP_ZeroOrNull: { /* in1, in2, out2, in3 */ 2942 if( (aMem[pOp->p1].flags & MEM_Null)!=0 2943 || (aMem[pOp->p3].flags & MEM_Null)!=0 2944 ){ 2945 sqlite3VdbeMemSetNull(aMem + pOp->p2); 2946 }else{ 2947 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0); 2948 } 2949 break; 2950 } 2951 2952 /* Opcode: NotNull P1 P2 * * * 2953 ** Synopsis: if r[P1]!=NULL goto P2 2954 ** 2955 ** Jump to P2 if the value in register P1 is not NULL. 2956 */ 2957 case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ 2958 pIn1 = &aMem[pOp->p1]; 2959 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); 2960 if( (pIn1->flags & MEM_Null)==0 ){ 2961 goto jump_to_p2; 2962 } 2963 break; 2964 } 2965 2966 /* Opcode: IfNullRow P1 P2 P3 * * 2967 ** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2 2968 ** 2969 ** Check the cursor P1 to see if it is currently pointing at a NULL row. 2970 ** If it is, then set register P3 to NULL and jump immediately to P2. 2971 ** If P1 is not on a NULL row, then fall through without making any 2972 ** changes. 2973 ** 2974 ** If P1 is not an open cursor, then this opcode is a no-op. 2975 */ 2976 case OP_IfNullRow: { /* jump */ 2977 VdbeCursor *pC; 2978 assert( pOp->p1>=0 && pOp->p1nCursor ); 2979 pC = p->apCsr[pOp->p1]; 2980 if( pC && pC->nullRow ){ 2981 if( pOp->p3>0 ) sqlite3VdbeMemSetNull(aMem + pOp->p3); 2982 goto jump_to_p2; 2983 } 2984 break; 2985 } 2986 2987 #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC 2988 /* Opcode: Offset P1 P2 P3 * * 2989 ** Synopsis: r[P3] = sqlite_offset(P1) 2990 ** 2991 ** Store in register r[P3] the byte offset into the database file that is the 2992 ** start of the payload for the record at which that cursor P1 is currently 2993 ** pointing. 2994 ** 2995 ** P2 is the column number for the argument to the sqlite_offset() function. 2996 ** This opcode does not use P2 itself, but the P2 value is used by the 2997 ** code generator. The P1, P2, and P3 operands to this opcode are the 2998 ** same as for OP_Column. 2999 ** 3000 ** This opcode is only available if SQLite is compiled with the 3001 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option. 3002 */ 3003 case OP_Offset: { /* out3 */ 3004 VdbeCursor *pC; /* The VDBE cursor */ 3005 assert( pOp->p1>=0 && pOp->p1nCursor ); 3006 pC = p->apCsr[pOp->p1]; 3007 pOut = &p->aMem[pOp->p3]; 3008 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){ 3009 sqlite3VdbeMemSetNull(pOut); 3010 }else{ 3011 if( pC->deferredMoveto ){ 3012 rc = sqlite3VdbeFinishMoveto(pC); 3013 if( rc ) goto abort_due_to_error; 3014 } 3015 if( sqlite3BtreeEof(pC->uc.pCursor) ){ 3016 sqlite3VdbeMemSetNull(pOut); 3017 }else{ 3018 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor)); 3019 } 3020 } 3021 break; 3022 } 3023 #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */ 3024 3025 /* Opcode: Column P1 P2 P3 P4 P5 3026 ** Synopsis: r[P3]=PX cursor P1 column P2 3027 ** 3028 ** Interpret the data that cursor P1 points to as a structure built using 3029 ** the MakeRecord instruction. (See the MakeRecord opcode for additional 3030 ** information about the format of the data.) Extract the P2-th column 3031 ** from this record. If there are less than (P2+1) 3032 ** values in the record, extract a NULL. 3033 ** 3034 ** The value extracted is stored in register P3. 3035 ** 3036 ** If the record contains fewer than P2 fields, then extract a NULL. Or, 3037 ** if the P4 argument is a P4_MEM use the value of the P4 argument as 3038 ** the result. 3039 ** 3040 ** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed 3041 ** to only be used by the length() function or the equivalent. The content 3042 ** of large blobs is not loaded, thus saving CPU cycles. If the 3043 ** OPFLAG_TYPEOFARG bit is set then the result will only be used by the 3044 ** typeof() function or the IS NULL or IS NOT NULL operators or the 3045 ** equivalent. In this case, all content loading can be omitted. 3046 */ 3047 case OP_Column: { /* ncycle */ 3048 u32 p2; /* column number to retrieve */ 3049 VdbeCursor *pC; /* The VDBE cursor */ 3050 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */ 3051 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ 3052 int len; /* The length of the serialized data for the column */ 3053 int i; /* Loop counter */ 3054 Mem *pDest; /* Where to write the extracted value */ 3055 Mem sMem; /* For storing the record being decoded */ 3056 const u8 *zData; /* Part of the record being decoded */ 3057 const u8 *zHdr; /* Next unparsed byte of the header */ 3058 const u8 *zEndHdr; /* Pointer to first byte after the header */ 3059 u64 offset64; /* 64-bit offset */ 3060 u32 t; /* A type code from the record header */ 3061 Mem *pReg; /* PseudoTable input register */ 3062 3063 assert( pOp->p1>=0 && pOp->p1nCursor ); 3064 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 3065 pC = p->apCsr[pOp->p1]; 3066 p2 = (u32)pOp->p2; 3067 3068 op_column_restart: 3069 assert( pC!=0 ); 3070 assert( p2<(u32)pC->nField 3071 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) ); 3072 aOffset = pC->aOffset; 3073 assert( aOffset==pC->aType+pC->nField ); 3074 assert( pC->eCurType!=CURTYPE_VTAB ); 3075 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); 3076 assert( pC->eCurType!=CURTYPE_SORTER ); 3077 3078 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/ 3079 if( pC->nullRow ){ 3080 if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){ 3081 /* For the special case of as pseudo-cursor, the seekResult field 3082 ** identifies the register that holds the record */ 3083 pReg = &aMem[pC->seekResult]; 3084 assert( pReg->flags & MEM_Blob ); 3085 assert( memIsValid(pReg) ); 3086 pC->payloadSize = pC->szRow = pReg->n; 3087 pC->aRow = (u8*)pReg->z; 3088 }else{ 3089 pDest = &aMem[pOp->p3]; 3090 memAboutToChange(p, pDest); 3091 sqlite3VdbeMemSetNull(pDest); 3092 goto op_column_out; 3093 } 3094 }else{ 3095 pCrsr = pC->uc.pCursor; 3096 if( pC->deferredMoveto ){ 3097 u32 iMap; 3098 assert( !pC->isEphemeral ); 3099 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){ 3100 pC = pC->pAltCursor; 3101 p2 = iMap - 1; 3102 goto op_column_restart; 3103 } 3104 rc = sqlite3VdbeFinishMoveto(pC); 3105 if( rc ) goto abort_due_to_error; 3106 }else if( sqlite3BtreeCursorHasMoved(pCrsr) ){ 3107 rc = sqlite3VdbeHandleMovedCursor(pC); 3108 if( rc ) goto abort_due_to_error; 3109 goto op_column_restart; 3110 } 3111 assert( pC->eCurType==CURTYPE_BTREE ); 3112 assert( pCrsr ); 3113 assert( sqlite3BtreeCursorIsValid(pCrsr) ); 3114 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr); 3115 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow); 3116 assert( pC->szRow<=pC->payloadSize ); 3117 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */ 3118 } 3119 pC->cacheStatus = p->cacheCtr; 3120 if( (aOffset[0] = pC->aRow[0])<0x80 ){ 3121 pC->iHdrOffset = 1; 3122 }else{ 3123 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset); 3124 } 3125 pC->nHdrParsed = 0; 3126 3127 if( pC->szRowaRow does not have to hold the entire row, but it does at least 3129 ** need to cover the header of the record. If pC->aRow does not contain 3130 ** the complete header, then set it to zero, forcing the header to be 3131 ** dynamically allocated. */ 3132 pC->aRow = 0; 3133 pC->szRow = 0; 3134 3135 /* Make sure a corrupt database has not given us an oversize header. 3136 ** Do this now to avoid an oversize memory allocation. 3137 ** 3138 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte 3139 ** types use so much data space that there can only be 4096 and 32 of 3140 ** them, respectively. So the maximum header length results from a 3141 ** 3-byte type for each of the maximum of 32768 columns plus three 3142 ** extra bytes for the header length itself. 32768*3 + 3 = 98307. 3143 */ 3144 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){ 3145 goto op_column_corrupt; 3146 } 3147 }else{ 3148 /* This is an optimization. By skipping over the first few tests 3149 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a 3150 ** measurable performance gain. 3151 ** 3152 ** This branch is taken even if aOffset[0]==0. Such a record is never 3153 ** generated by SQLite, and could be considered corruption, but we 3154 ** accept it for historical reasons. When aOffset[0]==0, the code this 3155 ** branch jumps to reads past the end of the record, but never more 3156 ** than a few bytes. Even if the record occurs at the end of the page 3157 ** content area, the "page header" comes after the page content and so 3158 ** this overread is harmless. Similar overreads can occur for a corrupt 3159 ** database file. 3160 */ 3161 zData = pC->aRow; 3162 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ 3163 testcase( aOffset[0]==0 ); 3164 goto op_column_read_header; 3165 } 3166 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){ 3167 rc = sqlite3VdbeHandleMovedCursor(pC); 3168 if( rc ) goto abort_due_to_error; 3169 goto op_column_restart; 3170 } 3171 3172 /* Make sure at least the first p2+1 entries of the header have been 3173 ** parsed and valid information is in aOffset[] and pC->aType[]. 3174 */ 3175 if( pC->nHdrParsed<=p2 ){ 3176 /* If there is more header available for parsing in the record, try 3177 ** to extract additional fields up through the p2+1-th field 3178 */ 3179 if( pC->iHdrOffsetaRow==0 ){ 3182 memset(&sMem, 0, sizeof(sMem)); 3183 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem); 3184 if( rc!=SQLITE_OK ) goto abort_due_to_error; 3185 zData = (u8*)sMem.z; 3186 }else{ 3187 zData = pC->aRow; 3188 } 3189 3190 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */ 3191 op_column_read_header: 3192 i = pC->nHdrParsed; 3193 offset64 = aOffset[i]; 3194 zHdr = zData + pC->iHdrOffset; 3195 zEndHdr = zData + aOffset[0]; 3196 testcase( zHdr>=zEndHdr ); 3197 do{ 3198 if( (pC->aType[i] = t = zHdr[0])<0x80 ){ 3199 zHdr++; 3200 offset64 += sqlite3VdbeOneByteSerialTypeLen(t); 3201 }else{ 3202 zHdr += sqlite3GetVarint32(zHdr, &t); 3203 pC->aType[i] = t; 3204 offset64 += sqlite3VdbeSerialTypeLen(t); 3205 } 3206 aOffset[++i] = (u32)(offset64 & 0xffffffff); 3207 }while( (u32)i<=p2 && zHdr=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize)) 3215 || (offset64 > pC->payloadSize) 3216 ){ 3217 if( aOffset[0]==0 ){ 3218 i = 0; 3219 zHdr = zEndHdr; 3220 }else{ 3221 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); 3222 goto op_column_corrupt; 3223 } 3224 } 3225 3226 pC->nHdrParsed = i; 3227 pC->iHdrOffset = (u32)(zHdr - zData); 3228 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); 3229 }else{ 3230 t = 0; 3231 } 3232 3233 /* If after trying to extract new entries from the header, nHdrParsed is 3234 ** still not up to p2, that means that the record has fewer than p2 3235 ** columns. So the result will be either the default value or a NULL. 3236 */ 3237 if( pC->nHdrParsed<=p2 ){ 3238 pDest = &aMem[pOp->p3]; 3239 memAboutToChange(p, pDest); 3240 if( pOp->p4type==P4_MEM ){ 3241 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); 3242 }else{ 3243 sqlite3VdbeMemSetNull(pDest); 3244 } 3245 goto op_column_out; 3246 } 3247 }else{ 3248 t = pC->aType[p2]; 3249 } 3250 3251 /* Extract the content for the p2+1-th column. Control can only 3252 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are 3253 ** all valid. 3254 */ 3255 assert( p2nHdrParsed ); 3256 assert( rc==SQLITE_OK ); 3257 pDest = &aMem[pOp->p3]; 3258 memAboutToChange(p, pDest); 3259 assert( sqlite3VdbeCheckMemInvariants(pDest) ); 3260 if( VdbeMemDynamic(pDest) ){ 3261 sqlite3VdbeMemSetNull(pDest); 3262 } 3263 assert( t==pC->aType[p2] ); 3264 if( pC->szRow>=aOffset[p2+1] ){ 3265 /* This is the common case where the desired content fits on the original 3266 ** page - where the content is not on an overflow page. 3267 ** 3268 ** The big switch() is an in-line variant of sqlite3VdbeSerialGet() that 3269 ** has been optimized for the OP_Column opcode. 3270 */ 3271 zData = pC->aRow + aOffset[p2]; 3272 switch( t ){ 3273 case 0: 3274 case 11: 3275 pDest->flags = MEM_Null; 3276 break; 3277 case 1: 3278 pDest->u.i = ONE_BYTE_INT(zData); 3279 pDest->flags = MEM_Int; 3280 testcase( pDest->u.i<0 ); 3281 break; 3282 case 2: 3283 pDest->u.i = TWO_BYTE_INT(zData); 3284 pDest->flags = MEM_Int; 3285 testcase( pDest->u.i<0 ); 3286 break; 3287 case 3: 3288 pDest->u.i = THREE_BYTE_INT(zData); 3289 pDest->flags = MEM_Int; 3290 testcase( pDest->u.i<0 ); 3291 break; 3292 case 4: 3293 pDest->u.i = FOUR_BYTE_INT(zData); 3294 pDest->flags = MEM_Int; 3295 testcase( pDest->u.i<0 ); 3296 break; 3297 case 5: 3298 pDest->u.i = SIX_BYTE_INT(zData); 3299 pDest->flags = MEM_Int; 3300 testcase( pDest->u.i<0 ); 3301 break; 3302 case 6: { 3303 pDest->u.i = (i64)sqlite3Get8byte(zData); 3304 pDest->flags = MEM_Int; 3305 testcase( pDest->u.i<0 ); 3306 break; 3307 } 3308 case 7: { 3309 u64 x = sqlite3Get8byte(zData); 3310 swapMixedEndianFloat(x); 3311 pDest->flags = IsNaN(x) ? MEM_Null : MEM_Real; 3312 memcpy(&pDest->u.r, &x, sizeof(x)); 3313 testcase( pDest->u.r<0 ); 3314 break; 3315 } 3316 case 8: 3317 case 9: { 3318 pDest->u.i = t-8; 3319 pDest->flags = MEM_Int; 3320 break; 3321 } 3322 case 10: 3323 /* Internal use only: NULL with virtual table 3324 ** UPDATE no-change flag set */ 3325 pDest->flags = MEM_Null|MEM_Zero; 3326 pDest->u.nZero = 0; 3327 pDest->n = 0; 3328 break; 3329 default: { 3330 /* If the column value is a string or blob, we need a persistent 3331 ** value, not a MEM_Ephem value. This case is a fast short-cut 3332 ** that is equivalent to calling sqlite3VdbeSerialGet() and 3333 ** sqlite3VdbeDeephemeralize(). 3334 */ 3335 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term }; 3336 pDest->n = len = (t-12)/2; 3337 pDest->enc = encoding; 3338 if( pDest->szMalloc < len+2 ){ 3339 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big; 3340 pDest->flags = MEM_Null; 3341 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem; 3342 }else{ 3343 pDest->z = pDest->zMalloc; 3344 } 3345 memcpy(pDest->z, zData, len); 3346 pDest->z[len] = 0; 3347 pDest->z[len+1] = 0; 3348 pDest->flags = aFlag[t&1]; 3349 } 3350 } /* End of switch */ 3351 }else{ 3352 u8 p5; 3353 pDest->enc = encoding; 3354 assert( pDest->db==db ); 3355 /* This branch happens only when content is on overflow pages */ 3356 if( ((p5 = (pOp->p5 & OPFLAG_BYTELENARG))!=0 3357 && (p5==OPFLAG_TYPEOFARG 3358 || (t>=12 && ((t&1)==0 || p5==OPFLAG_BYTELENARG)) 3359 ) 3360 ) 3361 || sqlite3VdbeSerialTypeLen(t)==0 3362 ){ 3363 /* Content is irrelevant for 3364 ** 1. the typeof() function, 3365 ** 2. the length(X) function if X is a blob, and 3366 ** 3. if the content length is zero. 3367 ** So we might as well use bogus content rather than reading 3368 ** content from disk. 3369 ** 3370 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the 3371 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may 3372 ** read more. Use the global constant sqlite3CtypeMap[] as the array, 3373 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint()) 3374 ** and it begins with a bunch of zeros. 3375 */ 3376 sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest); 3377 }else{ 3378 rc = vdbeColumnFromOverflow(pC, p2, t, aOffset[p2], 3379 p->cacheCtr, colCacheCtr, pDest); 3380 if( rc ){ 3381 if( rc==SQLITE_NOMEM ) goto no_mem; 3382 if( rc==SQLITE_TOOBIG ) goto too_big; 3383 goto abort_due_to_error; 3384 } 3385 } 3386 } 3387 3388 op_column_out: 3389 UPDATE_MAX_BLOBSIZE(pDest); 3390 REGISTER_TRACE(pOp->p3, pDest); 3391 break; 3392 3393 op_column_corrupt: 3394 if( aOp[0].p3>0 ){ 3395 pOp = &aOp[aOp[0].p3-1]; 3396 break; 3397 }else{ 3398 rc = SQLITE_CORRUPT_BKPT; 3399 goto abort_due_to_error; 3400 } 3401 } 3402 3403 /* Opcode: TypeCheck P1 P2 P3 P4 * 3404 ** Synopsis: typecheck(r[P1@P2]) 3405 ** 3406 ** Apply affinities to the range of P2 registers beginning with P1. 3407 ** Take the affinities from the Table object in P4. If any value 3408 ** cannot be coerced into the correct type, then raise an error. 3409 ** 3410 ** If P3==0, then omit checking of VIRTUAL columns. 3411 ** 3412 ** If P3==1, then omit checking of all generated column, both VIRTUAL 3413 ** and STORED. 3414 ** 3415 ** If P3>=2, then only check column number P3-2 in the table (which will 3416 ** be a VIRTUAL column) against the value in reg[P1]. In this case, 3417 ** P2 will be 1. 3418 ** 3419 ** This opcode is similar to OP_Affinity except that this opcode 3420 ** forces the register type to the Table column type. This is used 3421 ** to implement "strict affinity". 3422 ** 3423 ** GENERATED ALWAYS AS ... STATIC columns are only checked if P3 3424 ** is zero. When P3 is non-zero, no type checking occurs for 3425 ** static generated columns. Virtual columns are computed at query time 3426 ** and so they are never checked. 3427 ** 3428 ** Preconditions: 3429 ** 3430 **
    3431 **
  • P2 should be the number of non-virtual columns in the 3432 ** table of P4 unless P3>1, in which case P2 will be 1. 3433 **
  • Table P4 is a STRICT table. 3434 **
3435 ** 3436 ** If any precondition is false, an assertion fault occurs. 3437 */ 3438 case OP_TypeCheck: { 3439 Table *pTab; 3440 Column *aCol; 3441 int i; 3442 int nCol; 3443 3444 assert( pOp->p4type==P4_TABLE ); 3445 if( p->expired==1 ) goto abort_due_to_expired; 3446 pTab = pOp->p4.pTab; 3447 assert( pTab->tabFlags & TF_Strict ); 3448 assert( pOp->p3>=0 && pOp->p3nCol+2 ); 3449 aCol = pTab->aCol; 3450 pIn1 = &aMem[pOp->p1]; 3451 if( pOp->p3<2 ){ 3452 assert( pTab->nNVCol==pOp->p2 ); 3453 i = 0; 3454 nCol = pTab->nCol; 3455 }else{ 3456 i = pOp->p3-2; 3457 nCol = i+1; 3458 assert( inCol ); 3459 assert( aCol[i].colFlags & COLFLAG_VIRTUAL ); 3460 assert( pOp->p2==1 ); 3461 } 3462 for(; ip3<2 ){ 3464 if( (aCol[i].colFlags & COLFLAG_VIRTUAL)!=0 ) continue; 3465 if( pOp->p3 ){ pIn1++; continue; } 3466 } 3467 assert( pIn1 < &aMem[pOp->p1+pOp->p2] ); 3468 applyAffinity(pIn1, aCol[i].affinity, encoding); 3469 if( (pIn1->flags & MEM_Null)==0 ){ 3470 switch( aCol[i].eCType ){ 3471 case COLTYPE_BLOB: { 3472 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error; 3473 break; 3474 } 3475 case COLTYPE_INTEGER: 3476 case COLTYPE_INT: { 3477 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error; 3478 break; 3479 } 3480 case COLTYPE_TEXT: { 3481 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error; 3482 break; 3483 } 3484 case COLTYPE_REAL: { 3485 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real ); 3486 assert( (pIn1->flags & MEM_IntReal)==0 ); 3487 if( pIn1->flags & MEM_Int ){ 3488 /* When applying REAL affinity, if the result is still an MEM_Int 3489 ** that will fit in 6 bytes, then change the type to MEM_IntReal 3490 ** so that we keep the high-resolution integer value but know that 3491 ** the type really wants to be REAL. */ 3492 testcase( pIn1->u.i==140737488355328LL ); 3493 testcase( pIn1->u.i==140737488355327LL ); 3494 testcase( pIn1->u.i==-140737488355328LL ); 3495 testcase( pIn1->u.i==-140737488355329LL ); 3496 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){ 3497 pIn1->flags |= MEM_IntReal; 3498 pIn1->flags &= ~(MEM_Int|MEM_Str); 3499 }else{ 3500 pIn1->u.r = (double)pIn1->u.i; 3501 pIn1->flags |= MEM_Real; 3502 pIn1->flags &= ~(MEM_Int|MEM_Str); 3503 } 3504 }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){ 3505 goto vdbe_type_error; 3506 } 3507 break; 3508 } 3509 default: { 3510 /* COLTYPE_ANY. Accept anything. */ 3511 break; 3512 } 3513 } 3514 } 3515 REGISTER_TRACE((int)(pIn1-aMem), pIn1); 3516 pIn1++; 3517 } 3518 assert( pIn1 == &aMem[pOp->p1+pOp->p2] ); 3519 break; 3520 3521 vdbe_type_error: 3522 sqlite3VdbeError(p, "cannot store %s value in %s column %s.%s", 3523 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1], 3524 pTab->zName, aCol[i].zCnName); 3525 rc = SQLITE_CONSTRAINT_DATATYPE; 3526 goto abort_due_to_error; 3527 } 3528 3529 /* Opcode: Affinity P1 P2 * P4 * 3530 ** Synopsis: affinity(r[P1@P2]) 3531 ** 3532 ** Apply affinities to a range of P2 registers starting with P1. 3533 ** 3534 ** P4 is a string that is P2 characters long. The N-th character of the 3535 ** string indicates the column affinity that should be used for the N-th 3536 ** memory cell in the range. 3537 */ 3538 case OP_Affinity: { 3539 const char *zAffinity; /* The affinity to be applied */ 3540 3541 zAffinity = pOp->p4.z; 3542 assert( zAffinity!=0 ); 3543 assert( pOp->p2>0 ); 3544 assert( zAffinity[pOp->p2]==0 ); 3545 pIn1 = &aMem[pOp->p1]; 3546 while( 1 /*exit-by-break*/ ){ 3547 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); 3548 assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) ); 3549 applyAffinity(pIn1, zAffinity[0], encoding); 3550 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){ 3551 pIn1->u.r = (double)pIn1->u.i; 3552 pIn1->flags |= MEM_Real; 3553 pIn1->flags &= ~(MEM_Int|MEM_Str); 3554 } 3555 REGISTER_TRACE((int)(pIn1-aMem), pIn1); 3556 zAffinity++; 3557 if( zAffinity[0]==0 ) break; 3558 pIn1++; 3559 } 3560 break; 3561 } 3562 3563 /* Opcode: MakeRecord P1 P2 P3 P4 * 3564 ** Synopsis: r[P3]=mkrec(r[P1@P2]) 3565 ** 3566 ** Convert P2 registers beginning with P1 into the [record format] 3567 ** use as a data record in a database table or as a key 3568 ** in an index. The OP_Column opcode can decode the record later. 3569 ** 3570 ** P4 may be a string that is P2 characters long. The N-th character of the 3571 ** string indicates the column affinity that should be used for the N-th 3572 ** field of the index key. 3573 ** 3574 ** The mapping from character to affinity is given by the SQLITE_AFF_ 3575 ** macros defined in sqliteInt.h. 3576 ** 3577 ** If P4 is NULL then all index fields have the affinity BLOB. 3578 ** 3579 ** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM 3580 ** compile-time option is enabled: 3581 ** 3582 ** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index 3583 ** of the right-most table that can be null-trimmed. 3584 ** 3585 ** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value 3586 ** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to 3587 ** accept no-change records with serial_type 10. This value is 3588 ** only used inside an assert() and does not affect the end result. 3589 */ 3590 case OP_MakeRecord: { 3591 Mem *pRec; /* The new record */ 3592 u64 nData; /* Number of bytes of data space */ 3593 int nHdr; /* Number of bytes of header space */ 3594 i64 nByte; /* Data space required for this record */ 3595 i64 nZero; /* Number of zero bytes at the end of the record */ 3596 int nVarint; /* Number of bytes in a varint */ 3597 u32 serial_type; /* Type field */ 3598 Mem *pData0; /* First field to be combined into the record */ 3599 Mem *pLast; /* Last field of the record */ 3600 int nField; /* Number of fields in the record */ 3601 char *zAffinity; /* The affinity string for the record */ 3602 u32 len; /* Length of a field */ 3603 u8 *zHdr; /* Where to write next byte of the header */ 3604 u8 *zPayload; /* Where to write next byte of the payload */ 3605 3606 /* Assuming the record contains N fields, the record format looks 3607 ** like this: 3608 ** 3609 ** ------------------------------------------------------------------------ 3610 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | 3611 ** ------------------------------------------------------------------------ 3612 ** 3613 ** Data(0) is taken from register P1. Data(1) comes from register P1+1 3614 ** and so forth. 3615 ** 3616 ** Each type field is a varint representing the serial type of the 3617 ** corresponding data element (see sqlite3VdbeSerialType()). The 3618 ** hdr-size field is also a varint which is the offset from the beginning 3619 ** of the record to data0. 3620 */ 3621 nData = 0; /* Number of bytes of data space */ 3622 nHdr = 0; /* Number of bytes of header space */ 3623 nZero = 0; /* Number of zero bytes at the end of the record */ 3624 nField = pOp->p1; 3625 zAffinity = pOp->p4.z; 3626 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 ); 3627 pData0 = &aMem[nField]; 3628 nField = pOp->p2; 3629 pLast = &pData0[nField-1]; 3630 3631 /* Identify the output register */ 3632 assert( pOp->p3p1 || pOp->p3>=pOp->p1+pOp->p2 ); 3633 pOut = &aMem[pOp->p3]; 3634 memAboutToChange(p, pOut); 3635 3636 /* Apply the requested affinity to all inputs 3637 */ 3638 assert( pData0<=pLast ); 3639 if( zAffinity ){ 3640 pRec = pData0; 3641 do{ 3642 applyAffinity(pRec, zAffinity[0], encoding); 3643 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){ 3644 pRec->flags |= MEM_IntReal; 3645 pRec->flags &= ~(MEM_Int|MEM_Str); 3646 } 3647 REGISTER_TRACE((int)(pRec-aMem), pRec); 3648 zAffinity++; 3649 pRec++; 3650 assert( zAffinity[0]==0 || pRec<=pLast ); 3651 }while( zAffinity[0] ); 3652 } 3653 3654 #ifdef SQLITE_ENABLE_NULL_TRIM 3655 /* NULLs can be safely trimmed from the end of the record, as long as 3656 ** as the schema format is 2 or more and none of the omitted columns 3657 ** have a non-NULL default value. Also, the record must be left with 3658 ** at least one field. If P5>0 then it will be one more than the 3659 ** index of the right-most column with a non-NULL default value */ 3660 if( pOp->p5 ){ 3661 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){ 3662 pLast--; 3663 nField--; 3664 } 3665 } 3666 #endif 3667 3668 /* Loop through the elements that will make up the record to figure 3669 ** out how much space is required for the new record. After this loop, 3670 ** the Mem.uTemp field of each term should hold the serial-type that will 3671 ** be used for that term in the generated record: 3672 ** 3673 ** Mem.uTemp value type 3674 ** --------------- --------------- 3675 ** 0 NULL 3676 ** 1 1-byte signed integer 3677 ** 2 2-byte signed integer 3678 ** 3 3-byte signed integer 3679 ** 4 4-byte signed integer 3680 ** 5 6-byte signed integer 3681 ** 6 8-byte signed integer 3682 ** 7 IEEE float 3683 ** 8 Integer constant 0 3684 ** 9 Integer constant 1 3685 ** 10,11 reserved for expansion 3686 ** N>=12 and even BLOB 3687 ** N>=13 and odd text 3688 ** 3689 ** The following additional values are computed: 3690 ** nHdr Number of bytes needed for the record header 3691 ** nData Number of bytes of data space needed for the record 3692 ** nZero Zero bytes at the end of the record 3693 */ 3694 pRec = pLast; 3695 do{ 3696 assert( memIsValid(pRec) ); 3697 if( pRec->flags & MEM_Null ){ 3698 if( pRec->flags & MEM_Zero ){ 3699 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual 3700 ** table methods that never invoke sqlite3_result_xxxxx() while 3701 ** computing an unchanging column value in an UPDATE statement. 3702 ** Give such values a special internal-use-only serial-type of 10 3703 ** so that they can be passed through to xUpdate and have 3704 ** a true sqlite3_value_nochange(). */ 3705 #ifndef SQLITE_ENABLE_NULL_TRIM 3706 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB ); 3707 #endif 3708 pRec->uTemp = 10; 3709 }else{ 3710 pRec->uTemp = 0; 3711 } 3712 nHdr++; 3713 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){ 3714 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ 3715 i64 i = pRec->u.i; 3716 u64 uu; 3717 testcase( pRec->flags & MEM_Int ); 3718 testcase( pRec->flags & MEM_IntReal ); 3719 if( i<0 ){ 3720 uu = ~i; 3721 }else{ 3722 uu = i; 3723 } 3724 nHdr++; 3725 testcase( uu==127 ); testcase( uu==128 ); 3726 testcase( uu==32767 ); testcase( uu==32768 ); 3727 testcase( uu==`8388607` ); testcase( uu==`8388608` ); 3728 testcase( uu==`2147483647` ); testcase( uu==2147483648LL ); 3729 testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL ); 3730 if( uu<=127 ){ 3731 if( (i&1)==i && p->minWriteFileFormat>=4 ){ 3732 pRec->uTemp = 8+(u32)uu; 3733 }else{ 3734 nData++; 3735 pRec->uTemp = 1; 3736 } 3737 }else if( uu<=32767 ){ 3738 nData += 2; 3739 pRec->uTemp = 2; 3740 }else if( uu<=`8388607` ){ 3741 nData += 3; 3742 pRec->uTemp = 3; 3743 }else if( uu<=`2147483647` ){ 3744 nData += 4; 3745 pRec->uTemp = 4; 3746 }else if( uu<=140737488355327LL ){ 3747 nData += 6; 3748 pRec->uTemp = 5; 3749 }else{ 3750 nData += 8; 3751 if( pRec->flags & MEM_IntReal ){ 3752 /* If the value is IntReal and is going to take up 8 bytes to store 3753 ** as an integer, then we might as well make it an 8-byte floating 3754 ** point value */ 3755 pRec->u.r = (double)pRec->u.i; 3756 pRec->flags &= ~MEM_IntReal; 3757 pRec->flags |= MEM_Real; 3758 pRec->uTemp = 7; 3759 }else{ 3760 pRec->uTemp = 6; 3761 } 3762 } 3763 }else if( pRec->flags & MEM_Real ){ 3764 nHdr++; 3765 nData += 8; 3766 pRec->uTemp = 7; 3767 }else{ 3768 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) ); 3769 assert( pRec->n>=0 ); 3770 len = (u32)pRec->n; 3771 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0); 3772 if( pRec->flags & MEM_Zero ){ 3773 if( nData ){ 3774 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; 3775 len += pRec->u.nZero; 3776 }else{ 3777 nZero += pRec->u.nZero; 3778 } 3779 serial_type += 2*(u32)pRec->u.nZero; 3780 } 3781 nData += len; 3782 nHdr += sqlite3VarintLen(serial_type); 3783 pRec->uTemp = serial_type; 3784 } 3785 if( pRec==pData0 ) break; 3786 pRec--; 3787 }while(1); 3788 3789 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint 3790 ** which determines the total number of bytes in the header. The varint 3791 ** value is the size of the header in bytes including the size varint 3792 ** itself. */ 3793 testcase( nHdr==126 ); 3794 testcase( nHdr==127 ); 3795 if( nHdr<=126 ){ 3796 /* The common case */ 3797 nHdr += 1; 3798 }else{ 3799 /* Rare case of a really large header */ 3800 nVarint = sqlite3VarintLen(nHdr); 3801 nHdr += nVarint; 3802 if( nVarint0 3813 # define OVERRUN 7 /* We are able to allocate an overrun of 7 bytes */ 3814 #else 3815 # define OVERRUN 0 /* No overrun will be available */ 3816 #endif 3817 3818 /* Make sure the output register has a buffer large enough to store 3819 ** the new record. The output register (pOp->p3) is not allowed to 3820 ** be one of the input registers (because the following call to 3821 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). 3822 */ 3823 if( nByte+nZero<=pOut->szMalloc-OVERRUN ){ 3824 /* The output register is already large enough to hold the record. 3825 ** No error checks or buffer enlargement is required */ 3826 pOut->z = pOut->zMalloc; 3827 }else{ 3828 /* Need to make sure that the output is not too big and then enlarge 3829 ** the output register to hold the full result */ 3830 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 3831 goto too_big; 3832 } 3833 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte+OVERRUN) ){ 3834 goto no_mem; 3835 } 3836 } 3837 pOut->n = (int)nByte; 3838 pOut->flags = MEM_Blob; 3839 if( nZero ){ 3840 pOut->u.nZero = nZero; 3841 pOut->flags |= MEM_Zero; 3842 } 3843 UPDATE_MAX_BLOBSIZE(pOut); 3844 zHdr = (u8 *)pOut->z; 3845 zPayload = zHdr + nHdr; 3846 3847 /* Write the record */ 3848 if( nHdr<0x80 ){ 3849 *(zHdr++) = nHdr; 3850 }else{ 3851 zHdr += sqlite3PutVarint(zHdr,nHdr); 3852 } 3853 assert( pData0<=pLast ); 3854 pRec = pData0; 3855 while( 1 /*exit-by-break*/ ){ 3856 serial_type = pRec->uTemp; 3857 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more 3858 ** additional varints, one per column. 3859 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record 3860 ** immediately follow the header. */ 3861 if( serial_type<=7 ){ 3862 *(zHdr++) = serial_type; 3863 if( serial_type==0 ){ 3864 /* NULL value. No change in zPayload */ 3865 }else{ 3866 u64 v; 3867 if( serial_type==7 ){ 3868 assert( sizeof(v)==sizeof(pRec->u.r) ); 3869 memcpy(&v, &pRec->u.r, sizeof(v)); 3870 swapMixedEndianFloat(v); 3871 }else{ 3872 v = pRec->u.i; 3873 } 3874 len = sqlite3SmallTypeSizes[serial_type]; 3875 assert( len>=1 && len<=8 && len!=5 && len!=7 ); 3876 #if SQLITE_BYTEORDER==1234 3877 v = sqlite3BSwap64(v); 3878 if( OVERRUN ){ 3879 static const u8 aShift[] = { 0, 56, 48, 40, 32, 16, 0, 0 }; 3880 v >>= aShift[serial_type]; 3881 memcpy(zPayload, &v, 8); 3882 }else{ 3883 /* Test this limb by compiling with -DSQLITE_MAX_LENGTH=`2147483647` */ 3884 memcpy(zPayload, (u8*)&v + 8 - len, len); 3885 } 3886 #elif SQLITE_BYTEORDER==4321 3887 if( OVERRUN ){ 3888 static const u8 aShift[] = { 0, 56, 48, 40, 32, 16, 0, 0 }; 3889 v <<= aShift[serial_type]; 3890 memcpy(zPayload, &v, 8); 3891 }else{ 3892 /* Test this limb by compiling with -DSQLITE_MAX_LENGTH=`2147483647` */ 3893 memcpy(zPayload, (u8*)&v + 8 - len, len); 3894 } 3895 #else 3896 /* Test this limb by compiling with -DSQLITE_BYTEORDER=0 */ 3897 switch( len ){ 3898 default: zPayload[7] = (u8)(v&0xff); v >>= 8; 3899 zPayload[6] = (u8)(v&0xff); v >>= 8; 3900 /* no break */ deliberate_fall_through 3901 case 6: zPayload[5] = (u8)(v&0xff); v >>= 8; 3902 zPayload[4] = (u8)(v&0xff); v >>= 8; 3903 /* no break */ deliberate_fall_through 3904 case 4: zPayload[3] = (u8)(v&0xff); v >>= 8; 3905 /* no break */ deliberate_fall_through 3906 case 3: zPayload[2] = (u8)(v&0xff); v >>= 8; 3907 /* no break */ deliberate_fall_through 3908 case 2: zPayload[1] = (u8)(v&0xff); v >>= 8; 3909 /* no break */ deliberate_fall_through 3910 case 1: zPayload[0] = (u8)(v&0xff); 3911 } 3912 #endif 3913 #undef OVERRUN /* We are done with the OVERRUN macro now */ 3914 zPayload += len; 3915 } 3916 }else if( serial_type<0x80 ){ 3917 *(zHdr++) = serial_type; 3918 if( serial_type>=14 && pRec->n>0 ){ 3919 assert( pRec->z!=0 ); 3920 memcpy(zPayload, pRec->z, pRec->n); 3921 zPayload += pRec->n; 3922 } 3923 }else{ 3924 zHdr += sqlite3PutVarint(zHdr, serial_type); 3925 if( pRec->n ){ 3926 assert( pRec->z!=0 ); 3927 assert( pRec->z!=(const char*)sqlite3CtypeMap ); 3928 memcpy(zPayload, pRec->z, pRec->n); 3929 zPayload += pRec->n; 3930 } 3931 } 3932 if( pRec==pLast ) break; 3933 pRec++; 3934 } 3935 assert( nHdr==(int)(zHdr - (u8*)pOut->z) ); 3936 assert( nByte==(int)(zPayload - (u8*)pOut->z) ); 3937 3938 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 3939 REGISTER_TRACE(pOp->p3, pOut); 3940 break; 3941 } 3942 3943 /* Opcode: Count P1 P2 P3 * * 3944 ** Synopsis: r[P2]=count() 3945 ** 3946 ** Store the number of entries (an integer value) in the table or index 3947 ** opened by cursor P1 in register P2. 3948 ** 3949 ** If P3==0, then an exact count is obtained, which involves visiting 3950 ** every btree page of the table. But if P3 is non-zero, an estimate 3951 ** is returned based on the current cursor position. 3952 */ 3953 case OP_Count: { /* out2 */ 3954 i64 nEntry; 3955 BtCursor *pCrsr; 3956 3957 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); 3958 pCrsr = p->apCsr[pOp->p1]->uc.pCursor; 3959 assert( pCrsr ); 3960 if( pOp->p3 ){ 3961 nEntry = sqlite3BtreeRowCountEst(pCrsr); 3962 }else{ 3963 nEntry = 0; /* Not needed. Only used to silence a warning. */ 3964 rc = sqlite3BtreeCount(db, pCrsr, &nEntry); 3965 if( rc ) goto abort_due_to_error; 3966 } 3967 pOut = out2Prerelease(p, pOp); 3968 pOut->u.i = nEntry; 3969 goto check_for_interrupt; 3970 } 3971 3972 /* Opcode: Savepoint P1 * * P4 * 3973 ** 3974 ** Open, release or rollback the savepoint named by parameter P4, depending 3975 ** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN). 3976 ** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE). 3977 ** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK). 3978 */ 3979 case OP_Savepoint: { 3980 int p1; /* Value of P1 operand */ 3981 char *zName; /* Name of savepoint */ 3982 int nName; 3983 Savepoint *pNew; 3984 Savepoint *pSavepoint; 3985 Savepoint *pTmp; 3986 int iSavepoint; 3987 int ii; 3988 3989 p1 = pOp->p1; 3990 zName = pOp->p4.z; 3991 3992 /* Assert that the p1 parameter is valid. Also that if there is no open 3993 ** transaction, then there cannot be any savepoints. 3994 */ 3995 assert( db->pSavepoint==0 || db->autoCommit==0 ); 3996 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); 3997 assert( db->pSavepoint || db->isTransactionSavepoint==0 ); 3998 assert( checkSavepointCount(db) ); 3999 assert( p->bIsReader ); 4000 4001 if( p1==SAVEPOINT_BEGIN ){ 4002 if( db->nVdbeWrite>0 ){ 4003 /* A new savepoint cannot be created if there are active write 4004 ** statements (i.e. open read/write incremental blob handles). 4005 */ 4006 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress"); 4007 rc = SQLITE_BUSY; 4008 }else{ 4009 nName = sqlite3Strlen30(zName); 4010 4011 #ifndef SQLITE_OMIT_VIRTUALTABLE 4012 /* This call is Ok even if this savepoint is actually a transaction 4013 ** savepoint (and therefore should not prompt xSavepoint()) callbacks. 4014 ** If this is a transaction savepoint being opened, it is guaranteed 4015 ** that the db->aVTrans[] array is empty. */ 4016 assert( db->autoCommit==0 || db->nVTrans==0 ); 4017 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, 4018 db->nStatement+db->nSavepoint); 4019 if( rc!=SQLITE_OK ) goto abort_due_to_error; 4020 #endif 4021 4022 /* Create a new savepoint structure. */ 4023 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1); 4024 if( pNew ){ 4025 pNew->zName = (char *)&pNew[1]; 4026 memcpy(pNew->zName, zName, nName+1); 4027 4028 /* If there is no open transaction, then mark this as a special 4029 ** "transaction savepoint". */ 4030 if( db->autoCommit ){ 4031 db->autoCommit = 0; 4032 db->isTransactionSavepoint = 1; 4033 }else{ 4034 db->nSavepoint++; 4035 } 4036 4037 /* Link the new savepoint into the database handle's list. */ 4038 pNew->pNext = db->pSavepoint; 4039 db->pSavepoint = pNew; 4040 pNew->nDeferredCons = db->nDeferredCons; 4041 pNew->nDeferredImmCons = db->nDeferredImmCons; 4042 } 4043 } 4044 }else{ 4045 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK ); 4046 iSavepoint = 0; 4047 4048 /* Find the named savepoint. If there is no such savepoint, then an 4049 ** an error is returned to the user. */ 4050 for( 4051 pSavepoint = db->pSavepoint; 4052 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); 4053 pSavepoint = pSavepoint->pNext 4054 ){ 4055 iSavepoint++; 4056 } 4057 if( !pSavepoint ){ 4058 sqlite3VdbeError(p, "no such savepoint: %s", zName); 4059 rc = SQLITE_ERROR; 4060 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ 4061 /* It is not possible to release (commit) a savepoint if there are 4062 ** active write statements. 4063 */ 4064 sqlite3VdbeError(p, "cannot release savepoint - " 4065 "SQL statements in progress"); 4066 rc = SQLITE_BUSY; 4067 }else{ 4068 4069 /* Determine whether or not this is a transaction savepoint. If so, 4070 ** and this is a RELEASE command, then the current transaction 4071 ** is committed. 4072 */ 4073 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; 4074 if( isTransaction && p1==SAVEPOINT_RELEASE ){ 4075 if( (rc = sqlite3VdbeCheckFkDeferred(p))!=SQLITE_OK ){ 4076 goto vdbe_return; 4077 } 4078 db->autoCommit = 1; 4079 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ 4080 p->pc = (int)(pOp - aOp); 4081 db->autoCommit = 0; 4082 p->rc = rc = SQLITE_BUSY; 4083 goto vdbe_return; 4084 } 4085 rc = p->rc; 4086 if( rc ){ 4087 db->autoCommit = 0; 4088 }else{ 4089 db->isTransactionSavepoint = 0; 4090 } 4091 }else{ 4092 int isSchemaChange; 4093 iSavepoint = db->nSavepoint - iSavepoint - 1; 4094 if( p1==SAVEPOINT_ROLLBACK ){ 4095 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0; 4096 for(ii=0; iinDb; ii++){ 4097 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, 4098 SQLITE_ABORT_ROLLBACK, 4099 isSchemaChange==0); 4100 if( rc!=SQLITE_OK ) goto abort_due_to_error; 4101 } 4102 }else{ 4103 assert( p1==SAVEPOINT_RELEASE ); 4104 isSchemaChange = 0; 4105 } 4106 for(ii=0; iinDb; ii++){ 4107 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); 4108 if( rc!=SQLITE_OK ){ 4109 goto abort_due_to_error; 4110 } 4111 } 4112 if( isSchemaChange ){ 4113 sqlite3ExpirePreparedStatements(db, 0); 4114 sqlite3ResetAllSchemasOfConnection(db); 4115 db->mDbFlags |= DBFLAG_SchemaChange; 4116 } 4117 } 4118 if( rc ) goto abort_due_to_error; 4119 4120 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all 4121 ** savepoints nested inside of the savepoint being operated on. */ 4122 while( db->pSavepoint!=pSavepoint ){ 4123 pTmp = db->pSavepoint; 4124 db->pSavepoint = pTmp->pNext; 4125 sqlite3DbFree(db, pTmp); 4126 db->nSavepoint--; 4127 } 4128 4129 /* If it is a RELEASE, then destroy the savepoint being operated on 4130 ** too. If it is a ROLLBACK TO, then set the number of deferred 4131 ** constraint violations present in the database to the value stored 4132 ** when the savepoint was created. */ 4133 if( p1==SAVEPOINT_RELEASE ){ 4134 assert( pSavepoint==db->pSavepoint ); 4135 db->pSavepoint = pSavepoint->pNext; 4136 sqlite3DbFree(db, pSavepoint); 4137 if( !isTransaction ){ 4138 db->nSavepoint--; 4139 } 4140 }else{ 4141 assert( p1==SAVEPOINT_ROLLBACK ); 4142 db->nDeferredCons = pSavepoint->nDeferredCons; 4143 db->nDeferredImmCons = pSavepoint->nDeferredImmCons; 4144 } 4145 4146 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ 4147 rc = sqlite3VtabSavepoint(db, p1, iSavepoint); 4148 if( rc!=SQLITE_OK ) goto abort_due_to_error; 4149 } 4150 } 4151 } 4152 if( rc ) goto abort_due_to_error; 4153 if( p->eVdbeState==VDBE_HALT_STATE ){ 4154 rc = SQLITE_DONE; 4155 goto vdbe_return; 4156 } 4157 break; 4158 } 4159 4160 /* Opcode: AutoCommit P1 P2 * * * 4161 ** 4162 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll 4163 ** back any currently active btree transactions. If there are any active 4164 ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if 4165 ** there are active writing VMs or active VMs that use shared cache. 4166 ** 4167 ** This instruction causes the VM to halt. 4168 */ 4169 case OP_AutoCommit: { 4170 int desiredAutoCommit; 4171 int iRollback; 4172 4173 desiredAutoCommit = pOp->p1; 4174 iRollback = pOp->p2; 4175 assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); 4176 assert( desiredAutoCommit==1 || iRollback==0 ); 4177 assert( db->nVdbeActive>0 ); /* At least this one VM is active */ 4178 assert( p->bIsReader ); 4179 4180 if( desiredAutoCommit!=db->autoCommit ){ 4181 if( iRollback ){ 4182 assert( desiredAutoCommit==1 ); 4183 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); 4184 db->autoCommit = 1; 4185 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){ 4186 /* If this instruction implements a COMMIT and other VMs are writing 4187 ** return an error indicating that the other VMs must complete first. 4188 */ 4189 sqlite3VdbeError(p, "cannot commit transaction - " 4190 "SQL statements in progress"); 4191 rc = SQLITE_BUSY; 4192 goto abort_due_to_error; 4193 }else if( (rc = sqlite3VdbeCheckFkDeferred(p))!=SQLITE_OK ){ 4194 goto vdbe_return; 4195 }else{ 4196 db->autoCommit = (u8)desiredAutoCommit; 4197 } 4198 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ 4199 p->pc = (int)(pOp - aOp); 4200 db->autoCommit = (u8)(1-desiredAutoCommit); 4201 p->rc = rc = SQLITE_BUSY; 4202 goto vdbe_return; 4203 } 4204 sqlite3CloseSavepoints(db); 4205 if( p->rc==SQLITE_OK ){ 4206 rc = SQLITE_DONE; 4207 }else{ 4208 rc = SQLITE_ERROR; 4209 } 4210 goto vdbe_return; 4211 }else{ 4212 sqlite3VdbeError(p, 4213 (!desiredAutoCommit)?"cannot start a transaction within a transaction":( 4214 (iRollback)?"cannot rollback - no transaction is active": 4215 "cannot commit - no transaction is active")); 4216 4217 rc = SQLITE_ERROR; 4218 goto abort_due_to_error; 4219 } 4220 /*NOTREACHED*/ assert(0); 4221 } 4222 4223 /* Opcode: Transaction P1 P2 P3 P4 P5 4224 ** 4225 ** Begin a transaction on database P1 if a transaction is not already 4226 ** active. 4227 ** If P2 is non-zero, then a write-transaction is started, or if a 4228 ** read-transaction is already active, it is upgraded to a write-transaction. 4229 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more 4230 ** then an exclusive transaction is started. 4231 ** 4232 ** P1 is the index of the database file on which the transaction is 4233 ** started. Index 0 is the main database file and index 1 is the 4234 ** file used for temporary tables. Indices of 2 or more are used for 4235 ** attached databases. 4236 ** 4237 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is 4238 ** true (this flag is set if the Vdbe may modify more than one row and may 4239 ** throw an ABORT exception), a statement transaction may also be opened. 4240 ** More specifically, a statement transaction is opened iff the database 4241 ** connection is currently not in autocommit mode, or if there are other 4242 ** active statements. A statement transaction allows the changes made by this 4243 ** VDBE to be rolled back after an error without having to roll back the 4244 ** entire transaction. If no error is encountered, the statement transaction 4245 ** will automatically commit when the VDBE halts. 4246 ** 4247 ** If P5!=0 then this opcode also checks the schema cookie against P3 4248 ** and the schema generation counter against P4. 4249 ** The cookie changes its value whenever the database schema changes. 4250 ** This operation is used to detect when that the cookie has changed 4251 ** and that the current process needs to reread the schema. If the schema 4252 ** cookie in P3 differs from the schema cookie in the database header or 4253 ** if the schema generation counter in P4 differs from the current 4254 ** generation counter, then an SQLITE_SCHEMA error is raised and execution 4255 ** halts. The sqlite3_step() wrapper function might then reprepare the 4256 ** statement and rerun it from the beginning. 4257 */ 4258 case OP_Transaction: { 4259 Btree *pBt; 4260 Db *pDb; 4261 int iMeta = 0; 4262 4263 assert( p->bIsReader ); 4264 assert( p->readOnly==0 || pOp->p2==0 ); 4265 assert( pOp->p2>=0 && pOp->p2<=2 ); 4266 assert( pOp->p1>=0 && pOp->p1nDb ); 4267 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 4268 assert( rc==SQLITE_OK ); 4269 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){ 4270 if( db->flags & SQLITE_QueryOnly ){ 4271 /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */ 4272 rc = SQLITE_READONLY; 4273 }else{ 4274 /* Writes prohibited due to a prior SQLITE_CORRUPT in the current 4275 ** transaction */ 4276 rc = SQLITE_CORRUPT; 4277 } 4278 goto abort_due_to_error; 4279 } 4280 pDb = &db->aDb[pOp->p1]; 4281 pBt = pDb->pBt; 4282 4283 if( pBt ){ 4284 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta); 4285 testcase( rc==SQLITE_BUSY_SNAPSHOT ); 4286 testcase( rc==SQLITE_BUSY_RECOVERY ); 4287 if( rc!=SQLITE_OK ){ 4288 if( (rc&0xff)==SQLITE_BUSY ){ 4289 p->pc = (int)(pOp - aOp); 4290 p->rc = rc; 4291 goto vdbe_return; 4292 } 4293 goto abort_due_to_error; 4294 } 4295 4296 if( p->usesStmtJournal 4297 && pOp->p2 4298 && (db->autoCommit==0 || db->nVdbeRead>1) 4299 ){ 4300 assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE ); 4301 if( p->iStatement==0 ){ 4302 assert( db->nStatement>=0 && db->nSavepoint>=0 ); 4303 db->nStatement++; 4304 p->iStatement = db->nSavepoint + db->nStatement; 4305 } 4306 4307 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); 4308 if( rc==SQLITE_OK ){ 4309 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); 4310 } 4311 4312 /* Store the current value of the database handles deferred constraint 4313 ** counter. If the statement transaction needs to be rolled back, 4314 ** the value of this counter needs to be restored too. */ 4315 p->nStmtDefCons = db->nDeferredCons; 4316 p->nStmtDefImmCons = db->nDeferredImmCons; 4317 } 4318 } 4319 assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); 4320 if( rc==SQLITE_OK 4321 && pOp->p5 4322 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i) 4323 ){ 4324 /* 4325 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema 4326 ** version is checked to ensure that the schema has not changed since the 4327 ** SQL statement was prepared. 4328 */ 4329 sqlite3DbFree(db, p->zErrMsg); 4330 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); 4331 /* If the schema-cookie from the database file matches the cookie 4332 ** stored with the in-memory representation of the schema, do 4333 ** not reload the schema from the database file. 4334 ** 4335 ** If virtual-tables are in use, this is not just an optimization. 4336 ** Often, v-tables store their data in other SQLite tables, which 4337 ** are queried from within xNext() and other v-table methods using 4338 ** prepared queries. If such a query is out-of-date, we do not want to 4339 ** discard the database schema, as the user code implementing the 4340 ** v-table would have to be ready for the sqlite3_vtab structure itself 4341 ** to be invalidated whenever sqlite3_step() is called from within 4342 ** a v-table method. 4343 */ 4344 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ 4345 sqlite3ResetOneSchema(db, pOp->p1, p); 4346 } 4347 p->expired = 1; 4348 rc = SQLITE_SCHEMA; 4349 4350 /* Set changeCntOn to 0 to prevent the value returned by sqlite3_changes() 4351 ** from being modified in sqlite3VdbeHalt(). If this statement is 4352 ** reprepared, changeCntOn will be set again. */ 4353 p->changeCntOn = 0; 4354 } 4355 if( rc ) goto abort_due_to_error; 4356 break; 4357 } 4358 4359 /* Opcode: ReadCookie P1 P2 P3 * * 4360 ** 4361 ** Read cookie number P3 from database P1 and write it into register P2. 4362 ** P3==1 is the schema version. P3==2 is the database format. 4363 ** P3==3 is the recommended pager cache size, and so forth. P1==0 is 4364 ** the main database file and P1==1 is the database file used to store 4365 ** temporary tables. 4366 ** 4367 ** There must be a read-lock on the database (either a transaction 4368 ** must be started or there must be an open cursor) before 4369 ** executing this instruction. 4370 */ 4371 case OP_ReadCookie: { /* out2 */ 4372 int iMeta; 4373 int iDb; 4374 int iCookie; 4375 4376 assert( p->bIsReader ); 4377 iDb = pOp->p1; 4378 iCookie = pOp->p3; 4379 assert( pOp->p3=0 && iDbnDb ); 4381 assert( db->aDb[iDb].pBt!=0 ); 4382 assert( DbMaskTest(p->btreeMask, iDb) ); 4383 4384 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); 4385 pOut = out2Prerelease(p, pOp); 4386 pOut->u.i = iMeta; 4387 break; 4388 } 4389 4390 /* Opcode: SetCookie P1 P2 P3 * P5 4391 ** 4392 ** Write the integer value P3 into cookie number P2 of database P1. 4393 ** P2==1 is the schema version. P2==2 is the database format. 4394 ** P2==3 is the recommended pager cache 4395 ** size, and so forth. P1==0 is the main database file and P1==1 is the 4396 ** database file used to store temporary tables. 4397 ** 4398 ** A transaction must be started before executing this opcode. 4399 ** 4400 ** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal 4401 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement 4402 ** has P5 set to 1, so that the internal schema version will be different 4403 ** from the database schema version, resulting in a schema reset. 4404 */ 4405 case OP_SetCookie: { 4406 Db *pDb; 4407 4408 sqlite3VdbeIncrWriteCounter(p, 0); 4409 assert( pOp->p2p1>=0 && pOp->p1nDb ); 4411 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 4412 assert( p->readOnly==0 ); 4413 pDb = &db->aDb[pOp->p1]; 4414 assert( pDb->pBt!=0 ); 4415 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); 4416 /* See note about index shifting on OP_ReadCookie */ 4417 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3); 4418 if( pOp->p2==BTREE_SCHEMA_VERSION ){ 4419 /* When the schema cookie changes, record the new cookie internally */ 4420 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5; 4421 db->mDbFlags |= DBFLAG_SchemaChange; 4422 sqlite3FkClearTriggerCache(db, pOp->p1); 4423 }else if( pOp->p2==BTREE_FILE_FORMAT ){ 4424 /* Record changes in the file format */ 4425 pDb->pSchema->file_format = pOp->p3; 4426 } 4427 if( pOp->p1==1 ){ 4428 /* Invalidate all prepared statements whenever the TEMP database 4429 ** schema is changed. Ticket #1644 */ 4430 sqlite3ExpirePreparedStatements(db, 0); 4431 p->expired = 0; 4432 } 4433 if( rc ) goto abort_due_to_error; 4434 break; 4435 } 4436 4437 /* Opcode: OpenRead P1 P2 P3 P4 P5 4438 ** Synopsis: root=P2 iDb=P3 4439 ** 4440 ** Open a read-only cursor for the database table whose root page is 4441 ** P2 in a database file. The database file is determined by P3. 4442 ** P3==0 means the main database, P3==1 means the database used for 4443 ** temporary tables, and P3>1 means used the corresponding attached 4444 ** database. Give the new cursor an identifier of P1. The P1 4445 ** values need not be contiguous but all P1 values should be small integers. 4446 ** It is an error for P1 to be negative. 4447 ** 4448 ** Allowed P5 bits: 4449 **
    4450 **
  • 0x02 OPFLAG_SEEKEQ: This cursor will only be used for 4451 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT 4452 ** of OP_SeekLE/OP_IdxLT) 4453 **
4454 ** 4455 ** The P4 value may be either an integer (P4_INT32) or a pointer to 4456 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 4457 ** object, then table being opened must be an [index b-tree] where the 4458 ** KeyInfo object defines the content and collating 4459 ** sequence of that index b-tree. Otherwise, if P4 is an integer 4460 ** value, then the table being opened must be a [table b-tree] with a 4461 ** number of columns no less than the value of P4. 4462 ** 4463 ** See also: OpenWrite, ReopenIdx 4464 */ 4465 /* Opcode: ReopenIdx P1 P2 P3 P4 P5 4466 ** Synopsis: root=P2 iDb=P3 4467 ** 4468 ** The ReopenIdx opcode works like OP_OpenRead except that it first 4469 ** checks to see if the cursor on P1 is already open on the same 4470 ** b-tree and if it is this opcode becomes a no-op. In other words, 4471 ** if the cursor is already open, do not reopen it. 4472 ** 4473 ** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ 4474 ** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must 4475 ** be the same as every other ReopenIdx or OpenRead for the same cursor 4476 ** number. 4477 ** 4478 ** Allowed P5 bits: 4479 **
    4480 **
  • 0x02 OPFLAG_SEEKEQ: This cursor will only be used for 4481 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT 4482 ** of OP_SeekLE/OP_IdxLT) 4483 **
4484 ** 4485 ** See also: OP_OpenRead, OP_OpenWrite 4486 */ 4487 /* Opcode: OpenWrite P1 P2 P3 P4 P5 4488 ** Synopsis: root=P2 iDb=P3 4489 ** 4490 ** Open a read/write cursor named P1 on the table or index whose root 4491 ** page is P2 (or whose root page is held in register P2 if the 4492 ** OPFLAG_P2ISREG bit is set in P5 - see below). The database file is 4493 ** determined by P3. P3==0 means the main database, P3==1 means the 4494 ** database used for temporary tables, and P3>1 means used the 4495 ** corresponding attached database. 4496 ** 4497 ** The P4 value may be either an integer (P4_INT32) or a pointer to 4498 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 4499 ** object, then table being opened must be an [index b-tree] where the 4500 ** KeyInfo object defines the content and collating 4501 ** sequence of that index b-tree. Otherwise, if P4 is an integer 4502 ** value, then the table being opened must be a [table b-tree] with a 4503 ** number of columns no less than the value of P4. 4504 ** 4505 ** Allowed P5 bits: 4506 **
    4507 **
  • 0x02 OPFLAG_SEEKEQ: This cursor will only be used for 4508 ** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT 4509 ** of OP_SeekLE/OP_IdxLT) 4510 **
  • 0x08 OPFLAG_FORDELETE: This cursor is used only to seek 4511 ** and subsequently delete entries in an index btree. This is a 4512 ** hint to the storage engine that the storage engine is allowed to 4513 ** ignore. The hint is not used by the official SQLite b*tree storage 4514 ** engine, but is used by COMDB2. 4515 **
  • 0x10 OPFLAG_P2ISREG: Use the content of register P2 4516 ** as the root page, not the value of P2 itself. 4517 **
4518 ** 4519 ** This instruction works like OpenRead except that it opens the cursor 4520 ** in read/write mode. 4521 ** 4522 ** See also: OP_OpenRead, OP_ReopenIdx 4523 */ 4524 case OP_ReopenIdx: { /* ncycle */ 4525 int nField; 4526 KeyInfo *pKeyInfo; 4527 u32 p2; 4528 int iDb; 4529 int wrFlag; 4530 Btree *pX; 4531 VdbeCursor *pCur; 4532 Db *pDb; 4533 4534 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); 4535 assert( pOp->p4type==P4_KEYINFO ); 4536 pCur = p->apCsr[pOp->p1]; 4537 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ 4538 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ 4539 assert( pCur->eCurType==CURTYPE_BTREE ); 4540 sqlite3BtreeClearCursor(pCur->uc.pCursor); 4541 goto open_cursor_set_hints; 4542 } 4543 /* If the cursor is not currently open or is open on a different 4544 ** index, then fall through into OP_OpenRead to force a reopen */ 4545 case OP_OpenRead: /* ncycle */ 4546 case OP_OpenWrite: 4547 4548 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); 4549 assert( p->bIsReader ); 4550 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx 4551 || p->readOnly==0 ); 4552 4553 if( p->expired==1 ){ 4554 rc = SQLITE_ABORT_ROLLBACK; 4555 goto abort_due_to_error; 4556 } 4557 4558 nField = 0; 4559 pKeyInfo = 0; 4560 p2 = (u32)pOp->p2; 4561 iDb = pOp->p3; 4562 assert( iDb>=0 && iDbnDb ); 4563 assert( DbMaskTest(p->btreeMask, iDb) ); 4564 pDb = &db->aDb[iDb]; 4565 pX = pDb->pBt; 4566 assert( pX!=0 ); 4567 if( pOp->opcode==OP_OpenWrite ){ 4568 assert( OPFLAG_FORDELETE==BTREE_FORDELETE ); 4569 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE); 4570 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 4571 if( pDb->pSchema->file_format < p->minWriteFileFormat ){ 4572 p->minWriteFileFormat = pDb->pSchema->file_format; 4573 } 4574 if( pOp->p5 & OPFLAG_P2ISREG ){ 4575 assert( p2>0 ); 4576 assert( p2<=(u32)(p->nMem+1 - p->nCursor) ); 4577 pIn2 = &aMem[p2]; 4578 assert( memIsValid(pIn2) ); 4579 assert( (pIn2->flags & MEM_Int)!=0 ); 4580 sqlite3VdbeMemIntegerify(pIn2); 4581 p2 = (int)pIn2->u.i; 4582 /* The p2 value always comes from a prior OP_CreateBtree opcode and 4583 ** that opcode will always set the p2 value to 2 or more or else fail. 4584 ** If there were a failure, the prepared statement would have halted 4585 ** before reaching this instruction. */ 4586 assert( p2>=2 ); 4587 } 4588 }else{ 4589 wrFlag = 0; 4590 assert( (pOp->p5 & OPFLAG_P2ISREG)==0 ); 4591 } 4592 if( pOp->p4type==P4_KEYINFO ){ 4593 pKeyInfo = pOp->p4.pKeyInfo; 4594 assert( pKeyInfo->enc==ENC(db) ); 4595 assert( pKeyInfo->db==db ); 4596 nField = pKeyInfo->nAllField; 4597 }else if( pOp->p4type==P4_INT32 ){ 4598 nField = pOp->p4.i; 4599 } 4600 assert( pOp->p1>=0 ); 4601 assert( nField>=0 ); 4602 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ 4603 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE); 4604 if( pCur==0 ) goto no_mem; 4605 pCur->iDb = iDb; 4606 pCur->nullRow = 1; 4607 pCur->isOrdered = 1; 4608 pCur->pgnoRoot = p2; 4609 #ifdef SQLITE_DEBUG 4610 pCur->wrFlag = wrFlag; 4611 #endif 4612 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor); 4613 pCur->pKeyInfo = pKeyInfo; 4614 /* Set the VdbeCursor.isTable variable. Previous versions of 4615 ** SQLite used to check if the root-page flags were sane at this point 4616 ** and report database corruption if they were not, but this check has 4617 ** since moved into the btree layer. */ 4618 pCur->isTable = pOp->p4type!=P4_KEYINFO; 4619 4620 open_cursor_set_hints: 4621 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); 4622 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); 4623 testcase( pOp->p5 & OPFLAG_BULKCSR ); 4624 testcase( pOp->p2 & OPFLAG_SEEKEQ ); 4625 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor, 4626 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); 4627 if( rc ) goto abort_due_to_error; 4628 break; 4629 } 4630 4631 /* Opcode: OpenDup P1 P2 * * * 4632 ** 4633 ** Open a new cursor P1 that points to the same ephemeral table as 4634 ** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral 4635 ** opcode. Only ephemeral cursors may be duplicated. 4636 ** 4637 ** Duplicate ephemeral cursors are used for self-joins of materialized views. 4638 */ 4639 case OP_OpenDup: { /* ncycle */ 4640 VdbeCursor *pOrig; /* The original cursor to be duplicated */ 4641 VdbeCursor *pCx; /* The new cursor */ 4642 4643 pOrig = p->apCsr[pOp->p2]; 4644 assert( pOrig ); 4645 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */ 4646 4647 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE); 4648 if( pCx==0 ) goto no_mem; 4649 pCx->nullRow = 1; 4650 pCx->isEphemeral = 1; 4651 pCx->pKeyInfo = pOrig->pKeyInfo; 4652 pCx->isTable = pOrig->isTable; 4653 pCx->pgnoRoot = pOrig->pgnoRoot; 4654 pCx->isOrdered = pOrig->isOrdered; 4655 pCx->ub.pBtx = pOrig->ub.pBtx; 4656 pCx->noReuse = 1; 4657 pOrig->noReuse = 1; 4658 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR, 4659 pCx->pKeyInfo, pCx->uc.pCursor); 4660 /* The sqlite3BtreeCursor() routine can only fail for the first cursor 4661 ** opened for a database. Since there is already an open cursor when this 4662 ** opcode is run, the sqlite3BtreeCursor() cannot fail */ 4663 assert( rc==SQLITE_OK ); 4664 break; 4665 } 4666 4667 4668 /* Opcode: OpenEphemeral P1 P2 P3 P4 P5 4669 ** Synopsis: nColumn=P2 4670 ** 4671 ** Open a new cursor P1 to a transient table. 4672 ** The cursor is always opened read/write even if 4673 ** the main database is read-only. The ephemeral 4674 ** table is deleted automatically when the cursor is closed. 4675 ** 4676 ** If the cursor P1 is already opened on an ephemeral table, the table 4677 ** is cleared (all content is erased). 4678 ** 4679 ** P2 is the number of columns in the ephemeral table. 4680 ** The cursor points to a BTree table if P4==0 and to a BTree index 4681 ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure 4682 ** that defines the format of keys in the index. 4683 ** 4684 ** The P5 parameter can be a mask of the BTREE_* flags defined 4685 ** in btree.h. These flags control aspects of the operation of 4686 ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are 4687 ** added automatically. 4688 ** 4689 ** If P3 is positive, then reg[P3] is modified slightly so that it 4690 ** can be used as zero-length data for OP_Insert. This is an optimization 4691 ** that avoids an extra OP_Blob opcode to initialize that register. 4692 */ 4693 /* Opcode: OpenAutoindex P1 P2 * P4 * 4694 ** Synopsis: nColumn=P2 4695 ** 4696 ** This opcode works the same as OP_OpenEphemeral. It has a 4697 ** different name to distinguish its use. Tables created using 4698 ** by this opcode will be used for automatically created transient 4699 ** indices in joins. 4700 */ 4701 case OP_OpenAutoindex: /* ncycle */ 4702 case OP_OpenEphemeral: { /* ncycle */ 4703 VdbeCursor *pCx; 4704 KeyInfo *pKeyInfo; 4705 4706 static const int vfsFlags = 4707 SQLITE_OPEN_READWRITE | 4708 SQLITE_OPEN_CREATE | 4709 SQLITE_OPEN_EXCLUSIVE | 4710 SQLITE_OPEN_DELETEONCLOSE | 4711 SQLITE_OPEN_TRANSIENT_DB; 4712 assert( pOp->p1>=0 ); 4713 assert( pOp->p2>=0 ); 4714 if( pOp->p3>0 ){ 4715 /* Make register reg[P3] into a value that can be used as the data 4716 ** form sqlite3BtreeInsert() where the length of the data is zero. */ 4717 assert( pOp->p2==0 ); /* Only used when number of columns is zero */ 4718 assert( pOp->opcode==OP_OpenEphemeral ); 4719 assert( aMem[pOp->p3].flags & MEM_Null ); 4720 aMem[pOp->p3].n = 0; 4721 aMem[pOp->p3].z = ""; 4722 } 4723 pCx = p->apCsr[pOp->p1]; 4724 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){ 4725 /* If the ephemeral table is already open and has no duplicates from 4726 ** OP_OpenDup, then erase all existing content so that the table is 4727 ** empty again, rather than creating a new table. */ 4728 assert( pCx->isEphemeral ); 4729 pCx->seqCount = 0; 4730 pCx->cacheStatus = CACHE_STALE; 4731 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0); 4732 }else{ 4733 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE); 4734 if( pCx==0 ) goto no_mem; 4735 pCx->isEphemeral = 1; 4736 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx, 4737 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, 4738 vfsFlags); 4739 if( rc==SQLITE_OK ){ 4740 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0); 4741 if( rc==SQLITE_OK ){ 4742 /* If a transient index is required, create it by calling 4743 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before 4744 ** opening it. If a transient table is required, just use the 4745 ** automatically created table with root-page 1 (an BLOB_INTKEY table). 4746 */ 4747 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ 4748 assert( pOp->p4type==P4_KEYINFO ); 4749 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot, 4750 BTREE_BLOBKEY | pOp->p5); 4751 if( rc==SQLITE_OK ){ 4752 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 ); 4753 assert( pKeyInfo->db==db ); 4754 assert( pKeyInfo->enc==ENC(db) ); 4755 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR, 4756 pKeyInfo, pCx->uc.pCursor); 4757 } 4758 pCx->isTable = 0; 4759 }else{ 4760 pCx->pgnoRoot = SCHEMA_ROOT; 4761 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR, 4762 0, pCx->uc.pCursor); 4763 pCx->isTable = 1; 4764 } 4765 } 4766 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); 4767 assert( p->apCsr[pOp->p1]==pCx ); 4768 if( rc ){ 4769 assert( !sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) ); 4770 sqlite3BtreeClose(pCx->ub.pBtx); 4771 p->apCsr[pOp->p1] = 0; /* Not required; helps with static analysis */ 4772 }else{ 4773 assert( sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) ); 4774 if( pOp->opcode==OP_OpenAutoindex ){ 4775 sqlite3BtreeCursorHintFlags(pCx->uc.pCursor, BTREE_SEEK_EQ); 4776 } 4777 } 4778 } 4779 } 4780 if( rc ) goto abort_due_to_error; 4781 pCx->nullRow = 1; 4782 break; 4783 } 4784 4785 /* Opcode: SorterOpen P1 P2 P3 P4 * 4786 ** 4787 ** This opcode works like OP_OpenEphemeral except that it opens 4788 ** a transient index that is specifically designed to sort large 4789 ** tables using an external merge-sort algorithm. 4790 ** 4791 ** If argument P3 is non-zero, then it indicates that the sorter may 4792 ** assume that a stable sort considering the first P3 fields of each 4793 ** key is sufficient to produce the required results. 4794 */ 4795 case OP_SorterOpen: { 4796 VdbeCursor *pCx; 4797 4798 assert( pOp->p1>=0 ); 4799 assert( pOp->p2>=0 ); 4800 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER); 4801 if( pCx==0 ) goto no_mem; 4802 pCx->pKeyInfo = pOp->p4.pKeyInfo; 4803 assert( pCx->pKeyInfo->db==db ); 4804 assert( pCx->pKeyInfo->enc==ENC(db) ); 4805 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); 4806 if( rc ) goto abort_due_to_error; 4807 break; 4808 } 4809 4810 /* Opcode: SequenceTest P1 P2 * * * 4811 ** Synopsis: if( cursor[P1].ctr++ ) pc = P2 4812 ** 4813 ** P1 is a sorter cursor. If the sequence counter is currently zero, jump 4814 ** to P2. Regardless of whether or not the jump is taken, increment the 4815 ** the sequence value. 4816 */ 4817 case OP_SequenceTest: { 4818 VdbeCursor *pC; 4819 assert( pOp->p1>=0 && pOp->p1nCursor ); 4820 pC = p->apCsr[pOp->p1]; 4821 assert( isSorter(pC) ); 4822 if( (pC->seqCount++)==0 ){ 4823 goto jump_to_p2; 4824 } 4825 break; 4826 } 4827 4828 /* Opcode: OpenPseudo P1 P2 P3 * * 4829 ** Synopsis: P3 columns in r[P2] 4830 ** 4831 ** Open a new cursor that points to a fake table that contains a single 4832 ** row of data. The content of that one row is the content of memory 4833 ** register P2. In other words, cursor P1 becomes an alias for the 4834 ** MEM_Blob content contained in register P2. 4835 ** 4836 ** A pseudo-table created by this opcode is used to hold a single 4837 ** row output from the sorter so that the row can be decomposed into 4838 ** individual columns using the OP_Column opcode. The OP_Column opcode 4839 ** is the only cursor opcode that works with a pseudo-table. 4840 ** 4841 ** P3 is the number of fields in the records that will be stored by 4842 ** the pseudo-table. If P2 is 0 or negative then the pseudo-cursor 4843 ** will return NULL for every column. 4844 */ 4845 case OP_OpenPseudo: { 4846 VdbeCursor *pCx; 4847 4848 assert( pOp->p1>=0 ); 4849 assert( pOp->p3>=0 ); 4850 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO); 4851 if( pCx==0 ) goto no_mem; 4852 pCx->nullRow = 1; 4853 pCx->seekResult = pOp->p2; 4854 pCx->isTable = 1; 4855 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx 4856 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test 4857 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto() 4858 ** which is a performance optimization */ 4859 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor(); 4860 assert( pOp->p5==0 ); 4861 break; 4862 } 4863 4864 /* Opcode: Close P1 * * * * 4865 ** 4866 ** Close a cursor previously opened as P1. If P1 is not 4867 ** currently open, this instruction is a no-op. 4868 */ 4869 case OP_Close: { /* ncycle */ 4870 assert( pOp->p1>=0 && pOp->p1nCursor ); 4871 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); 4872 p->apCsr[pOp->p1] = 0; 4873 break; 4874 } 4875 4876 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK 4877 /* Opcode: ColumnsUsed P1 P2 P3 * * 4878 ** Synopsis: Cursor P1 uses columns PHEX23 4879 ** 4880 ** This opcode (which only exists if SQLite was compiled with 4881 ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the 4882 ** table or index for cursor P1 are used. P2 and P3 combine to give 4883 ** a 64-bit unsigned integer mask. P2 stores the lower 32 bits and 4884 ** P3 stores the upper 32 bits. If the high-order bit of P3 is set 4885 ** that means that and 64-th or some later column is used. 4886 */ 4887 case OP_ColumnsUsed: { 4888 VdbeCursor *pC; 4889 pC = p->apCsr[pOp->p1]; 4890 assert( pC->eCurType==CURTYPE_BTREE ); 4891 pC->maskUsed = INT32_TO_64(pOp->p2,pOp->p3); 4892 break; 4893 } 4894 #endif 4895 4896 /* Opcode: SeekGE P1 P2 P3 P4 * 4897 ** Synopsis: key=r[P3@P4] 4898 ** 4899 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 4900 ** use the value in register P3 as the key. If cursor P1 refers 4901 ** to an SQL index, then P3 is the first in an array of P4 registers 4902 ** that are used as an unpacked index key. 4903 ** 4904 ** Reposition cursor P1 so that it points to the smallest entry that 4905 ** is greater than or equal to the key value. If there are no records 4906 ** greater than or equal to the key and P2 is not zero, then jump to P2. 4907 ** 4908 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this 4909 ** opcode will either land on a record that exactly matches the key, or 4910 ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ, 4911 ** this opcode must be followed by an IdxLE opcode with the same arguments, 4912 ** except that the P2 argument (the jump destination) can be different. 4913 ** The IdxGT opcode will be skipped if this opcode succeeds, but the 4914 ** IdxGT opcode will be used on subsequent loop iterations. The 4915 ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this 4916 ** is an equality search. 4917 ** 4918 ** The P2 of the IdxLE might be less than the P2 of this opcode. The P2 4919 ** of the IdxLE will be the location to jump to if there are one or more 4920 ** matching rows and is the location to continue with the next row of the 4921 ** first containing loop. The P2 of this opcode will be the location to 4922 ** jump to if zero rows of cursor P1 match the key, and that will be the 4923 ** continuation address of the first outer loop that contributes to the 4924 ** key. The P2 of this opcode might be greater than the P2 of the 4925 ** subsequent IdxLE if the first outer loop that influences the key is 4926 ** outer to the immediately next outer loop. 4927 ** 4928 ** This opcode leaves the cursor configured to move in forward order, 4929 ** from the beginning toward the end. In other words, the cursor is 4930 ** configured to use Next, not Prev. 4931 ** 4932 ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe 4933 */ 4934 /* Opcode: SeekGT P1 P2 P3 P4 * 4935 ** Synopsis: key=r[P3@P4] 4936 ** 4937 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 4938 ** use the value in register P3 as a key. If cursor P1 refers 4939 ** to an SQL index, then P3 is the first in an array of P4 registers 4940 ** that are used as an unpacked index key. 4941 ** 4942 ** Reposition cursor P1 so that it points to the smallest entry that 4943 ** is greater than the key value. If there are no records greater than 4944 ** the key and P2 is not zero, then jump to P2. 4945 ** 4946 ** This opcode leaves the cursor configured to move in forward order, 4947 ** from the beginning toward the end. In other words, the cursor is 4948 ** configured to use Next, not Prev. 4949 ** 4950 ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe 4951 */ 4952 /* Opcode: SeekLT P1 P2 P3 P4 * 4953 ** Synopsis: key=r[P3@P4] 4954 ** 4955 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 4956 ** use the value in register P3 as a key. If cursor P1 refers 4957 ** to an SQL index, then P3 is the first in an array of P4 registers 4958 ** that are used as an unpacked index key. 4959 ** 4960 ** Reposition cursor P1 so that it points to the largest entry that 4961 ** is less than the key value. If there are no records less than 4962 ** the key and P2 is not zero, then jump to P2. 4963 ** 4964 ** This opcode leaves the cursor configured to move in reverse order, 4965 ** from the end toward the beginning. In other words, the cursor is 4966 ** configured to use Prev, not Next. 4967 ** 4968 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe 4969 */ 4970 /* Opcode: SeekLE P1 P2 P3 P4 * 4971 ** Synopsis: key=r[P3@P4] 4972 ** 4973 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 4974 ** use the value in register P3 as a key. If cursor P1 refers 4975 ** to an SQL index, then P3 is the first in an array of P4 registers 4976 ** that are used as an unpacked index key. 4977 ** 4978 ** Reposition cursor P1 so that it points to the largest entry that 4979 ** is less than or equal to the key value. If there are no records 4980 ** less than or equal to the key and P2 is not zero, then jump to P2. 4981 ** 4982 ** This opcode leaves the cursor configured to move in reverse order, 4983 ** from the end toward the beginning. In other words, the cursor is 4984 ** configured to use Prev, not Next. 4985 ** 4986 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this 4987 ** opcode will either land on a record that exactly matches the key, or 4988 ** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ, 4989 ** this opcode must be followed by an IdxGE opcode with the same arguments 4990 ** except the P2 of IdxGE might be less than the P2 of this opcode. 4991 ** The IdxGE opcode will be skipped if this opcode succeeds, but the 4992 ** IdxGE opcode will be used on subsequent loop iterations. The 4993 ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this 4994 ** is an equality search. 4995 ** 4996 ** The P2 of the IdxGE might be less than the P2 of this opcode. The P2 4997 ** of the IdxGE will be the location to jump to if there are one or more 4998 ** matching rows and is the location to continue with the next row of the 4999 ** first containing loop. The P2 of this opcode will be the location to 5000 ** jump to if no rows of cursor P1 match the key, and that will be the 5001 ** continuation address of the first outer loop that contributes to the 5002 ** key. The P2 of this opcode might be greater than the P2 of the 5003 ** subsequent IdxLE if the first outer loop that influences the key is 5004 ** outer to the immediately next outer loop. 5005 ** 5006 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt 5007 */ 5008 case OP_SeekLT: /* jump0, in3, group, ncycle */ 5009 case OP_SeekLE: /* jump0, in3, group, ncycle */ 5010 case OP_SeekGE: /* jump0, in3, group, ncycle */ 5011 case OP_SeekGT: { /* jump0, in3, group, ncycle */ 5012 int res; /* Comparison result */ 5013 int oc; /* Opcode */ 5014 VdbeCursor *pC; /* The cursor to seek */ 5015 UnpackedRecord r; /* The key to seek for */ 5016 int nField; /* Number of columns or fields in the key */ 5017 i64 iKey; /* The rowid we are to seek to */ 5018 int eqOnly; /* Only interested in == results */ 5019 5020 assert( pOp->p1>=0 && pOp->p1nCursor ); 5021 assert( pOp->p2!=0 ); 5022 pC = p->apCsr[pOp->p1]; 5023 assert( pC!=0 ); 5024 assert( pC->eCurType==CURTYPE_BTREE ); 5025 assert( OP_SeekLE == OP_SeekLT+1 ); 5026 assert( OP_SeekGE == OP_SeekLT+2 ); 5027 assert( OP_SeekGT == OP_SeekLT+3 ); 5028 assert( pC->isOrdered ); 5029 assert( pC->uc.pCursor!=0 ); 5030 oc = pOp->opcode; 5031 eqOnly = 0; 5032 pC->nullRow = 0; 5033 #ifdef SQLITE_DEBUG 5034 pC->seekOp = pOp->opcode; 5035 #endif 5036 5037 pC->deferredMoveto = 0; 5038 pC->cacheStatus = CACHE_STALE; 5039 if( pC->isTable ){ 5040 u16 flags3, newType; 5041 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */ 5042 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 5043 || CORRUPT_DB ); 5044 5045 /* The input value in P3 might be of any type: integer, real, string, 5046 ** blob, or NULL. But it needs to be an integer before we can do 5047 ** the seek, so convert it. */ 5048 pIn3 = &aMem[pOp->p3]; 5049 flags3 = pIn3->flags; 5050 if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){ 5051 applyNumericAffinity(pIn3, 0); 5052 } 5053 iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */ 5054 newType = pIn3->flags; /* Record the type after applying numeric affinity */ 5055 pIn3->flags = flags3; /* But convert the type back to its original */ 5056 5057 /* If the P3 value could not be converted into an integer without 5058 ** loss of information, then special processing is required... */ 5059 if( (newType & (MEM_Int|MEM_IntReal))==0 ){ 5060 int c; 5061 if( (newType & MEM_Real)==0 ){ 5062 if( (newType & MEM_Null) || oc>=OP_SeekGE ){ 5063 VdbeBranchTaken(1,2); 5064 goto jump_to_p2; 5065 }else{ 5066 rc = sqlite3BtreeLast(pC->uc.pCursor, &res); 5067 if( rc!=SQLITE_OK ) goto abort_due_to_error; 5068 goto seek_not_found; 5069 } 5070 } 5071 c = sqlite3IntFloatCompare(iKey, pIn3->u.r); 5072 5073 /* If the approximation iKey is larger than the actual real search 5074 ** term, substitute >= for > and < for <=. e.g. if the search term 5075 ** is 4.9 and the integer approximation 5: 5076 ** 5077 ** (x > 4.9) -> (x >= 5) 5078 ** (x <= 4.9) -> (x < 5) 5079 */ 5080 if( c>0 ){ 5081 assert( OP_SeekGE==(OP_SeekGT-1) ); 5082 assert( OP_SeekLT==(OP_SeekLE-1) ); 5083 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) ); 5084 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--; 5085 } 5086 5087 /* If the approximation iKey is smaller than the actual real search 5088 ** term, substitute <= for < and > for >=. */ 5089 else if( c<0 ){ 5090 assert( OP_SeekLE==(OP_SeekLT+1) ); 5091 assert( OP_SeekGT==(OP_SeekGE+1) ); 5092 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); 5093 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; 5094 } 5095 } 5096 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res); 5097 pC->movetoTarget = iKey; /* Used by OP_Delete */ 5098 if( rc!=SQLITE_OK ){ 5099 goto abort_due_to_error; 5100 } 5101 }else{ 5102 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the 5103 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be 5104 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively, 5105 ** with the same key. 5106 */ 5107 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){ 5108 eqOnly = 1; 5109 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE ); 5110 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); 5111 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT ); 5112 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT ); 5113 assert( pOp[1].p1==pOp[0].p1 ); 5114 assert( pOp[1].p2<=pOp[0].p2 ); 5115 testcase( pOp->opcode==OP_SeekGE && pOp[1].p2opcode==OP_SeekLE && pOp[1].p2p4.i; 5122 assert( pOp->p4type==P4_INT32 ); 5123 assert( nField>0 ); 5124 r.pKeyInfo = pC->pKeyInfo; 5125 r.nField = (u16)nField; 5126 5127 /* The next line of code computes as follows, only faster: 5128 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ 5129 ** r.default_rc = -1; 5130 ** }else{ 5131 ** r.default_rc = +1; 5132 ** } 5133 */ 5134 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); 5135 assert( oc!=OP_SeekGT || r.default_rc==-1 ); 5136 assert( oc!=OP_SeekLE || r.default_rc==-1 ); 5137 assert( oc!=OP_SeekGE || r.default_rc==+1 ); 5138 assert( oc!=OP_SeekLT || r.default_rc==+1 ); 5139 5140 r.aMem = &aMem[pOp->p3]; 5141 #ifdef SQLITE_DEBUG 5142 { 5143 int i; 5144 for(i=0; i0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]); 5147 } 5148 } 5149 #endif 5150 r.eqSeen = 0; 5151 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res); 5152 if( rc!=SQLITE_OK ){ 5153 goto abort_due_to_error; 5154 } 5155 if( eqOnly && r.eqSeen==0 ){ 5156 assert( res!=0 ); 5157 goto seek_not_found; 5158 } 5159 } 5160 #ifdef SQLITE_TEST 5161 sqlite3_search_count++; 5162 #endif 5163 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); 5164 if( res<0 || (res==0 && oc==OP_SeekGT) ){ 5165 res = 0; 5166 rc = sqlite3BtreeNext(pC->uc.pCursor, 0); 5167 if( rc!=SQLITE_OK ){ 5168 if( rc==SQLITE_DONE ){ 5169 rc = SQLITE_OK; 5170 res = 1; 5171 }else{ 5172 goto abort_due_to_error; 5173 } 5174 } 5175 }else{ 5176 res = 0; 5177 } 5178 }else{ 5179 assert( oc==OP_SeekLT || oc==OP_SeekLE ); 5180 if( res>0 || (res==0 && oc==OP_SeekLT) ){ 5181 res = 0; 5182 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0); 5183 if( rc!=SQLITE_OK ){ 5184 if( rc==SQLITE_DONE ){ 5185 rc = SQLITE_OK; 5186 res = 1; 5187 }else{ 5188 goto abort_due_to_error; 5189 } 5190 } 5191 }else{ 5192 /* res might be negative because the table is empty. Check to 5193 ** see if this is the case. 5194 */ 5195 res = sqlite3BtreeEof(pC->uc.pCursor); 5196 } 5197 } 5198 seek_not_found: 5199 assert( pOp->p2>0 ); 5200 VdbeBranchTaken(res!=0,2); 5201 if( res ){ 5202 goto jump_to_p2; 5203 }else if( eqOnly ){ 5204 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); 5205 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */ 5206 } 5207 break; 5208 } 5209 5210 5211 /* Opcode: SeekScan P1 P2 * * P5 5212 ** Synopsis: Scan-ahead up to P1 rows 5213 ** 5214 ** This opcode is a prefix opcode to OP_SeekGE. In other words, this 5215 ** opcode must be immediately followed by OP_SeekGE. This constraint is 5216 ** checked by assert() statements. 5217 ** 5218 ** This opcode uses the P1 through P4 operands of the subsequent 5219 ** OP_SeekGE. In the text that follows, the operands of the subsequent 5220 ** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only 5221 ** the P1, P2 and P5 operands of this opcode are also used, and are called 5222 ** This.P1, This.P2 and This.P5. 5223 ** 5224 ** This opcode helps to optimize IN operators on a multi-column index 5225 ** where the IN operator is on the later terms of the index by avoiding 5226 ** unnecessary seeks on the btree, substituting steps to the next row 5227 ** of the b-tree instead. A correct answer is obtained if this opcode 5228 ** is omitted or is a no-op. 5229 ** 5230 ** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which 5231 ** is the desired entry that we want the cursor SeekGE.P1 to be pointing 5232 ** to. Call this SeekGE.P3/P4 row the "target". 5233 ** 5234 ** If the SeekGE.P1 cursor is not currently pointing to a valid row, 5235 ** then this opcode is a no-op and control passes through into the OP_SeekGE. 5236 ** 5237 ** If the SeekGE.P1 cursor is pointing to a valid row, then that row 5238 ** might be the target row, or it might be near and slightly before the 5239 ** target row, or it might be after the target row. If the cursor is 5240 ** currently before the target row, then this opcode attempts to position 5241 ** the cursor on or after the target row by invoking sqlite3BtreeStep() 5242 ** on the cursor between 1 and This.P1 times. 5243 ** 5244 ** The This.P5 parameter is a flag that indicates what to do if the 5245 ** cursor ends up pointing at a valid row that is past the target 5246 ** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If 5247 ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0 5248 ** case occurs when there are no inequality constraints to the right of 5249 ** the IN constraint. The jump to SeekGE.P2 ends the loop. The P5!=0 case 5250 ** occurs when there are inequality constraints to the right of the IN 5251 ** operator. In that case, the This.P2 will point either directly to or 5252 ** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for 5253 ** loop terminate. 5254 ** 5255 ** Possible outcomes from this opcode:
    5256 ** 5257 **
  1. If the cursor is initially not pointed to any valid row, then 5258 ** fall through into the subsequent OP_SeekGE opcode. 5259 ** 5260 **
  2. If the cursor is left pointing to a row that is before the target 5261 ** row, even after making as many as This.P1 calls to 5262 ** sqlite3BtreeNext(), then also fall through into OP_SeekGE. 5263 ** 5264 **
  3. If the cursor is left pointing at the target row, either because it 5265 ** was at the target row to begin with or because one or more 5266 ** sqlite3BtreeNext() calls moved the cursor to the target row, 5267 ** then jump to This.P2.., 5268 ** 5269 **
  4. If the cursor started out before the target row and a call to 5270 ** to sqlite3BtreeNext() moved the cursor off the end of the index 5271 ** (indicating that the target row definitely does not exist in the 5272 ** btree) then jump to SeekGE.P2, ending the loop. 5273 ** 5274 **
  5. If the cursor ends up on a valid row that is past the target row 5275 ** (indicating that the target row does not exist in the btree) then 5276 ** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0. 5277 **
5278 */ 5279 case OP_SeekScan: { /* ncycle */ 5280 VdbeCursor *pC; 5281 int res; 5282 int nStep; 5283 UnpackedRecord r; 5284 5285 assert( pOp[1].opcode==OP_SeekGE ); 5286 5287 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the 5288 ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first 5289 ** opcode past the OP_SeekGE itself. */ 5290 assert( pOp->p2>=(int)(pOp-aOp)+2 ); 5291 #ifdef SQLITE_DEBUG 5292 if( pOp->p5==0 ){ 5293 /* There are no inequality constraints following the IN constraint. */ 5294 assert( pOp[1].p1==aOp[pOp->p2-1].p1 ); 5295 assert( pOp[1].p2==aOp[pOp->p2-1].p2 ); 5296 assert( pOp[1].p3==aOp[pOp->p2-1].p3 ); 5297 assert( aOp[pOp->p2-1].opcode==OP_IdxGT 5298 || aOp[pOp->p2-1].opcode==OP_IdxGE ); 5299 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE ); 5300 }else{ 5301 /* There are inequality constraints. */ 5302 assert( pOp->p2==(int)(pOp-aOp)+2 ); 5303 assert( aOp[pOp->p2-1].opcode==OP_SeekGE ); 5304 } 5305 #endif 5306 5307 assert( pOp->p1>0 ); 5308 pC = p->apCsr[pOp[1].p1]; 5309 assert( pC!=0 ); 5310 assert( pC->eCurType==CURTYPE_BTREE ); 5311 assert( !pC->isTable ); 5312 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){ 5313 #ifdef SQLITE_DEBUG 5314 if( db->flags&SQLITE_VdbeTrace ){ 5315 printf("... cursor not valid - fall through\n"); 5316 } 5317 #endif 5318 break; 5319 } 5320 nStep = pOp->p1; 5321 assert( nStep>=1 ); 5322 r.pKeyInfo = pC->pKeyInfo; 5323 r.nField = (u16)pOp[1].p4.i; 5324 r.default_rc = 0; 5325 r.aMem = &aMem[pOp[1].p3]; 5326 #ifdef SQLITE_DEBUG 5327 { 5328 int i; 5329 for(i=0; i0 && pOp->p5==0 ){ 5340 seekscan_search_fail: 5341 /* Jump to SeekGE.P2, ending the loop */ 5342 #ifdef SQLITE_DEBUG 5343 if( db->flags&SQLITE_VdbeTrace ){ 5344 printf("... %d steps and then skip\n", pOp->p1 - nStep); 5345 } 5346 #endif 5347 VdbeBranchTaken(1,3); 5348 pOp++; 5349 goto jump_to_p2; 5350 } 5351 if( res>=0 ){ 5352 /* Jump to This.P2, bypassing the OP_SeekGE opcode */ 5353 #ifdef SQLITE_DEBUG 5354 if( db->flags&SQLITE_VdbeTrace ){ 5355 printf("... %d steps and then success\n", pOp->p1 - nStep); 5356 } 5357 #endif 5358 VdbeBranchTaken(2,3); 5359 goto jump_to_p2; 5360 break; 5361 } 5362 if( nStep<=0 ){ 5363 #ifdef SQLITE_DEBUG 5364 if( db->flags&SQLITE_VdbeTrace ){ 5365 printf("... fall through after %d steps\n", pOp->p1); 5366 } 5367 #endif 5368 VdbeBranchTaken(0,3); 5369 break; 5370 } 5371 nStep--; 5372 pC->cacheStatus = CACHE_STALE; 5373 rc = sqlite3BtreeNext(pC->uc.pCursor, 0); 5374 if( rc ){ 5375 if( rc==SQLITE_DONE ){ 5376 rc = SQLITE_OK; 5377 goto seekscan_search_fail; 5378 }else{ 5379 goto abort_due_to_error; 5380 } 5381 } 5382 } 5383 5384 break; 5385 } 5386 5387 5388 /* Opcode: SeekHit P1 P2 P3 * * 5389 ** Synopsis: set P2<=seekHit<=P3 5390 ** 5391 ** Increase or decrease the seekHit value for cursor P1, if necessary, 5392 ** so that it is no less than P2 and no greater than P3. 5393 ** 5394 ** The seekHit integer represents the maximum of terms in an index for which 5395 ** there is known to be at least one match. If the seekHit value is smaller 5396 ** than the total number of equality terms in an index lookup, then the 5397 ** OP_IfNoHope opcode might run to see if the IN loop can be abandoned 5398 ** early, thus saving work. This is part of the IN-early-out optimization. 5399 ** 5400 ** P1 must be a valid b-tree cursor. 5401 */ 5402 case OP_SeekHit: { /* ncycle */ 5403 VdbeCursor *pC; 5404 assert( pOp->p1>=0 && pOp->p1nCursor ); 5405 pC = p->apCsr[pOp->p1]; 5406 assert( pC!=0 ); 5407 assert( pOp->p3>=pOp->p2 ); 5408 if( pC->seekHitp2 ){ 5409 #ifdef SQLITE_DEBUG 5410 if( db->flags&SQLITE_VdbeTrace ){ 5411 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2); 5412 } 5413 #endif 5414 pC->seekHit = pOp->p2; 5415 }else if( pC->seekHit>pOp->p3 ){ 5416 #ifdef SQLITE_DEBUG 5417 if( db->flags&SQLITE_VdbeTrace ){ 5418 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3); 5419 } 5420 #endif 5421 pC->seekHit = pOp->p3; 5422 } 5423 break; 5424 } 5425 5426 /* Opcode: Found P1 P2 P3 P4 * 5427 ** Synopsis: key=r[P3@P4] 5428 ** 5429 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If 5430 ** P4>0 then register P3 is the first of P4 registers that form an unpacked 5431 ** record. 5432 ** 5433 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 5434 ** is a prefix of any entry in P1 then a jump is made to P2 and 5435 ** P1 is left pointing at the matching entry. 5436 ** 5437 ** This operation leaves the cursor in a state where it can be 5438 ** advanced in the forward direction. The Next instruction will work, 5439 ** but not the Prev instruction. 5440 ** 5441 ** See also: NotFound, NoConflict, NotExists. SeekGe 5442 */ 5443 /* Opcode: NotFound P1 P2 P3 P4 * 5444 ** Synopsis: key=r[P3@P4] 5445 ** 5446 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If 5447 ** P4>0 then register P3 is the first of P4 registers that form an unpacked 5448 ** record. 5449 ** 5450 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 5451 ** is not the prefix of any entry in P1 then a jump is made to P2. If P1 5452 ** does contain an entry whose prefix matches the P3/P4 record then control 5453 ** falls through to the next instruction and P1 is left pointing at the 5454 ** matching entry. 5455 ** 5456 ** This operation leaves the cursor in a state where it cannot be 5457 ** advanced in either direction. In other words, the Next and Prev 5458 ** opcodes do not work after this operation. 5459 ** 5460 ** See also: Found, NotExists, NoConflict, IfNoHope 5461 */ 5462 /* Opcode: IfNoHope P1 P2 P3 P4 * 5463 ** Synopsis: key=r[P3@P4] 5464 ** 5465 ** Register P3 is the first of P4 registers that form an unpacked 5466 ** record. Cursor P1 is an index btree. P2 is a jump destination. 5467 ** In other words, the operands to this opcode are the same as the 5468 ** operands to OP_NotFound and OP_IdxGT. 5469 ** 5470 ** This opcode is an optimization attempt only. If this opcode always 5471 ** falls through, the correct answer is still obtained, but extra work 5472 ** is performed. 5473 ** 5474 ** A value of N in the seekHit flag of cursor P1 means that there exists 5475 ** a key P3:N that will match some record in the index. We want to know 5476 ** if it is possible for a record P3:P4 to match some record in the 5477 ** index. If it is not possible, we can skip some work. So if seekHit 5478 ** is less than P4, attempt to find out if a match is possible by running 5479 ** OP_NotFound. 5480 ** 5481 ** This opcode is used in IN clause processing for a multi-column key. 5482 ** If an IN clause is attached to an element of the key other than the 5483 ** left-most element, and if there are no matches on the most recent 5484 ** seek over the whole key, then it might be that one of the key element 5485 ** to the left is prohibiting a match, and hence there is "no hope" of 5486 ** any match regardless of how many IN clause elements are checked. 5487 ** In such a case, we abandon the IN clause search early, using this 5488 ** opcode. The opcode name comes from the fact that the 5489 ** jump is taken if there is "no hope" of achieving a match. 5490 ** 5491 ** See also: NotFound, SeekHit 5492 */ 5493 /* Opcode: NoConflict P1 P2 P3 P4 * 5494 ** Synopsis: key=r[P3@P4] 5495 ** 5496 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If 5497 ** P4>0 then register P3 is the first of P4 registers that form an unpacked 5498 ** record. 5499 ** 5500 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 5501 ** contains any NULL value, jump immediately to P2. If all terms of the 5502 ** record are not-NULL then a check is done to determine if any row in the 5503 ** P1 index btree has a matching key prefix. If there are no matches, jump 5504 ** immediately to P2. If there is a match, fall through and leave the P1 5505 ** cursor pointing to the matching row. 5506 ** 5507 ** This opcode is similar to OP_NotFound with the exceptions that the 5508 ** branch is always taken if any part of the search key input is NULL. 5509 ** 5510 ** This operation leaves the cursor in a state where it cannot be 5511 ** advanced in either direction. In other words, the Next and Prev 5512 ** opcodes do not work after this operation. 5513 ** 5514 ** See also: NotFound, Found, NotExists 5515 */ 5516 case OP_IfNoHope: { /* jump, in3, ncycle */ 5517 VdbeCursor *pC; 5518 assert( pOp->p1>=0 && pOp->p1nCursor ); 5519 pC = p->apCsr[pOp->p1]; 5520 assert( pC!=0 ); 5521 #ifdef SQLITE_DEBUG 5522 if( db->flags&SQLITE_VdbeTrace ){ 5523 printf("seekHit is %d\n", pC->seekHit); 5524 } 5525 #endif 5526 if( pC->seekHit>=pOp->p4.i ) break; 5527 /* Fall through into OP_NotFound */ 5528 /* no break */ deliberate_fall_through 5529 } 5530 case OP_NoConflict: /* jump, in3, ncycle */ 5531 case OP_NotFound: /* jump, in3, ncycle */ 5532 case OP_Found: { /* jump, in3, ncycle */ 5533 int alreadyExists; 5534 int ii; 5535 VdbeCursor *pC; 5536 UnpackedRecord *pIdxKey; 5537 UnpackedRecord r; 5538 5539 #ifdef SQLITE_TEST 5540 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; 5541 #endif 5542 5543 assert( pOp->p1>=0 && pOp->p1nCursor ); 5544 assert( pOp->p4type==P4_INT32 ); 5545 pC = p->apCsr[pOp->p1]; 5546 assert( pC!=0 ); 5547 #ifdef SQLITE_DEBUG 5548 pC->seekOp = pOp->opcode; 5549 #endif 5550 r.aMem = &aMem[pOp->p3]; 5551 assert( pC->eCurType==CURTYPE_BTREE ); 5552 assert( pC->uc.pCursor!=0 ); 5553 assert( pC->isTable==0 ); 5554 r.nField = (u16)pOp->p4.i; 5555 if( r.nField>0 ){ 5556 /* Key values in an array of registers */ 5557 r.pKeyInfo = pC->pKeyInfo; 5558 r.default_rc = 0; 5559 #ifdef SQLITE_DEBUG 5560 (void)sqlite3FaultSim(50); /* For use by --counter in TH3 */ 5561 for(ii=0; iip3+ii, &r.aMem[ii]); 5565 } 5566 #endif 5567 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult); 5568 }else{ 5569 /* Composite key generated by OP_MakeRecord */ 5570 assert( r.aMem->flags & MEM_Blob ); 5571 assert( pOp->opcode!=OP_NoConflict ); 5572 rc = ExpandBlob(r.aMem); 5573 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); 5574 if( rc ) goto no_mem; 5575 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo); 5576 if( pIdxKey==0 ) goto no_mem; 5577 sqlite3VdbeRecordUnpack(r.aMem->n, r.aMem->z, pIdxKey); 5578 pIdxKey->default_rc = 0; 5579 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult); 5580 sqlite3DbFreeNN(db, pIdxKey); 5581 } 5582 if( rc!=SQLITE_OK ){ 5583 goto abort_due_to_error; 5584 } 5585 alreadyExists = (pC->seekResult==0); 5586 pC->nullRow = 1-alreadyExists; 5587 pC->deferredMoveto = 0; 5588 pC->cacheStatus = CACHE_STALE; 5589 if( pOp->opcode==OP_Found ){ 5590 VdbeBranchTaken(alreadyExists!=0,2); 5591 if( alreadyExists ) goto jump_to_p2; 5592 }else{ 5593 if( !alreadyExists ){ 5594 VdbeBranchTaken(1,2); 5595 goto jump_to_p2; 5596 } 5597 if( pOp->opcode==OP_NoConflict ){ 5598 /* For the OP_NoConflict opcode, take the jump if any of the 5599 ** input fields are NULL, since any key with a NULL will not 5600 ** conflict */ 5601 for(ii=0; iiopcode==OP_IfNoHope ){ 5610 pC->seekHit = pOp->p4.i; 5611 } 5612 } 5613 break; 5614 } 5615 5616 /* Opcode: SeekRowid P1 P2 P3 * * 5617 ** Synopsis: intkey=r[P3] 5618 ** 5619 ** P1 is the index of a cursor open on an SQL table btree (with integer 5620 ** keys). If register P3 does not contain an integer or if P1 does not 5621 ** contain a record with rowid P3 then jump immediately to P2. 5622 ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain 5623 ** a record with rowid P3 then 5624 ** leave the cursor pointing at that record and fall through to the next 5625 ** instruction. 5626 ** 5627 ** The OP_NotExists opcode performs the same operation, but with OP_NotExists 5628 ** the P3 register must be guaranteed to contain an integer value. With this 5629 ** opcode, register P3 might not contain an integer. 5630 ** 5631 ** The OP_NotFound opcode performs the same operation on index btrees 5632 ** (with arbitrary multi-value keys). 5633 ** 5634 ** This opcode leaves the cursor in a state where it cannot be advanced 5635 ** in either direction. In other words, the Next and Prev opcodes will 5636 ** not work following this opcode. 5637 ** 5638 ** See also: Found, NotFound, NoConflict, SeekRowid 5639 */ 5640 /* Opcode: NotExists P1 P2 P3 * * 5641 ** Synopsis: intkey=r[P3] 5642 ** 5643 ** P1 is the index of a cursor open on an SQL table btree (with integer 5644 ** keys). P3 is an integer rowid. If P1 does not contain a record with 5645 ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an 5646 ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then 5647 ** leave the cursor pointing at that record and fall through to the next 5648 ** instruction. 5649 ** 5650 ** The OP_SeekRowid opcode performs the same operation but also allows the 5651 ** P3 register to contain a non-integer value, in which case the jump is 5652 ** always taken. This opcode requires that P3 always contain an integer. 5653 ** 5654 ** The OP_NotFound opcode performs the same operation on index btrees 5655 ** (with arbitrary multi-value keys). 5656 ** 5657 ** This opcode leaves the cursor in a state where it cannot be advanced 5658 ** in either direction. In other words, the Next and Prev opcodes will 5659 ** not work following this opcode. 5660 ** 5661 ** See also: Found, NotFound, NoConflict, SeekRowid 5662 */ 5663 case OP_SeekRowid: { /* jump0, in3, ncycle */ 5664 VdbeCursor *pC; 5665 BtCursor *pCrsr; 5666 int res; 5667 i64 iKey; 5668 5669 pIn3 = &aMem[pOp->p3]; 5670 testcase( pIn3->flags & MEM_Int ); 5671 testcase( pIn3->flags & MEM_IntReal ); 5672 testcase( pIn3->flags & MEM_Real ); 5673 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str ); 5674 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){ 5675 /* If pIn3->u.i does not contain an integer, compute iKey as the 5676 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted 5677 ** into an integer without loss of information. Take care to avoid 5678 ** changing the datatype of pIn3, however, as it is used by other 5679 ** parts of the prepared statement. */ 5680 Mem x = pIn3[0]; 5681 if( (x.flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str ){ 5682 applyNumericAffinity(&x, 1); 5683 } 5684 if( x.flags & MEM_Int ){ 5685 iKey = x.u.i; 5686 }else 5687 if( (x.flags & MEM_Real)==0 5688 || x.u.r < -`9223372036854775808`.0 5689 || x.u.r > `9223372036854774784`.0 5690 /* ^^^^^^^^^^^^^^^^^^^^^-- same value as every other double 5691 ** between `9223372036854774263`.0 and `923372036854775295`.0 */ 5692 || (double)(iKey = sqlite3RealToI64(x.u.r))!=x.u.r 5693 ){ 5694 goto jump_to_p2; 5695 } 5696 goto notExistsWithKey; 5697 } 5698 /* Fall through into OP_NotExists */ 5699 /* no break */ deliberate_fall_through 5700 case OP_NotExists: /* jump, in3, ncycle */ 5701 pIn3 = &aMem[pOp->p3]; 5702 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid ); 5703 assert( pOp->p1>=0 && pOp->p1nCursor ); 5704 iKey = pIn3->u.i; 5705 notExistsWithKey: 5706 pC = p->apCsr[pOp->p1]; 5707 assert( pC!=0 ); 5708 #ifdef SQLITE_DEBUG 5709 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid; 5710 #endif 5711 assert( pC->isTable ); 5712 assert( pC->eCurType==CURTYPE_BTREE ); 5713 pCrsr = pC->uc.pCursor; 5714 assert( pCrsr!=0 ); 5715 res = 0; 5716 rc = sqlite3BtreeTableMoveto(pCrsr, iKey, 0, &res); 5717 assert( rc==SQLITE_OK || res==0 ); 5718 pC->movetoTarget = iKey; /* Used by OP_Delete */ 5719 pC->nullRow = 0; 5720 pC->cacheStatus = CACHE_STALE; 5721 pC->deferredMoveto = 0; 5722 VdbeBranchTaken(res!=0,2); 5723 pC->seekResult = res; 5724 if( res!=0 ){ 5725 assert( rc==SQLITE_OK ); 5726 if( pOp->p2==0 ){ 5727 rc = SQLITE_CORRUPT_BKPT; 5728 }else{ 5729 goto jump_to_p2; 5730 } 5731 } 5732 if( rc ) goto abort_due_to_error; 5733 break; 5734 } 5735 5736 /* Opcode: Sequence P1 P2 * * * 5737 ** Synopsis: r[P2]=cursor[P1].ctr++ 5738 ** 5739 ** Find the next available sequence number for cursor P1. 5740 ** Write the sequence number into register P2. 5741 ** The sequence number on the cursor is incremented after this 5742 ** instruction. 5743 */ 5744 case OP_Sequence: { /* out2 */ 5745 assert( pOp->p1>=0 && pOp->p1nCursor ); 5746 assert( p->apCsr[pOp->p1]!=0 ); 5747 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB ); 5748 pOut = out2Prerelease(p, pOp); 5749 pOut->u.i = p->apCsr[pOp->p1]->seqCount++; 5750 break; 5751 } 5752 5753 5754 /* Opcode: NewRowid P1 P2 P3 * * 5755 ** Synopsis: r[P2]=rowid 5756 ** 5757 ** Get a new integer record number (a.k.a "rowid") used as the key to a table. 5758 ** The record number is not previously used as a key in the database 5759 ** table that cursor P1 points to. The new record number is written 5760 ** written to register P2. 5761 ** 5762 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds 5763 ** the largest previously generated record number. No new record numbers are 5764 ** allowed to be less than this value. When this value reaches its maximum, 5765 ** an SQLITE_FULL error is generated. The P3 register is updated with the ' 5766 ** generated record number. This P3 mechanism is used to help implement the 5767 ** AUTOINCREMENT feature. 5768 */ 5769 case OP_NewRowid: { /* out2 */ 5770 i64 v; /* The new rowid */ 5771 VdbeCursor *pC; /* Cursor of table to get the new rowid */ 5772 int res; /* Result of an sqlite3BtreeLast() */ 5773 int cnt; /* Counter to limit the number of searches */ 5774 #ifndef SQLITE_OMIT_AUTOINCREMENT 5775 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ 5776 VdbeFrame *pFrame; /* Root frame of VDBE */ 5777 #endif 5778 5779 v = 0; 5780 res = 0; 5781 pOut = out2Prerelease(p, pOp); 5782 assert( pOp->p1>=0 && pOp->p1nCursor ); 5783 pC = p->apCsr[pOp->p1]; 5784 assert( pC!=0 ); 5785 assert( pC->isTable ); 5786 assert( pC->eCurType==CURTYPE_BTREE ); 5787 assert( pC->uc.pCursor!=0 ); 5788 { 5789 /* The next rowid or record number (different terms for the same 5790 ** thing) is obtained in a two-step algorithm. 5791 ** 5792 ** First we attempt to find the largest existing rowid and add one 5793 ** to that. But if the largest existing rowid is already the maximum 5794 ** positive integer, we have to fall through to the second 5795 ** probabilistic algorithm 5796 ** 5797 ** The second algorithm is to select a rowid at random and see if 5798 ** it already exists in the table. If it does not exist, we have 5799 ** succeeded. If the random rowid does exist, we select a new one 5800 ** and try again, up to 100 times. 5801 */ 5802 assert( pC->isTable ); 5803 5804 #ifdef SQLITE_32BIT_ROWID 5805 # define MAX_ROWID 0x7fffffff 5806 #else 5807 /* Some compilers complain about constants of the form 0x7fffffffffffffff. 5808 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems 5809 ** to provide the constant while making all compilers happy. 5810 */ 5811 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) 5812 #endif 5813 5814 if( !pC->useRandomRowid ){ 5815 rc = sqlite3BtreeLast(pC->uc.pCursor, &res); 5816 if( rc!=SQLITE_OK ){ 5817 goto abort_due_to_error; 5818 } 5819 if( res ){ 5820 v = 1; /* IMP: R-61914-48074 */ 5821 }else{ 5822 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) ); 5823 v = sqlite3BtreeIntegerKey(pC->uc.pCursor); 5824 if( v>=MAX_ROWID ){ 5825 pC->useRandomRowid = 1; 5826 }else{ 5827 v++; /* IMP: R-29538-34987 */ 5828 } 5829 } 5830 } 5831 5832 #ifndef SQLITE_OMIT_AUTOINCREMENT 5833 if( pOp->p3 ){ 5834 /* Assert that P3 is a valid memory cell. */ 5835 assert( pOp->p3>0 ); 5836 if( p->pFrame ){ 5837 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); 5838 /* Assert that P3 is a valid memory cell. */ 5839 assert( pOp->p3<=pFrame->nMem ); 5840 pMem = &pFrame->aMem[pOp->p3]; 5841 }else{ 5842 /* Assert that P3 is a valid memory cell. */ 5843 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 5844 pMem = &aMem[pOp->p3]; 5845 memAboutToChange(p, pMem); 5846 } 5847 assert( memIsValid(pMem) ); 5848 5849 REGISTER_TRACE(pOp->p3, pMem); 5850 sqlite3VdbeMemIntegerify(pMem); 5851 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ 5852 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ 5853 rc = SQLITE_FULL; /* IMP: R-17817-00630 */ 5854 goto abort_due_to_error; 5855 } 5856 if( vu.i+1 ){ 5857 v = pMem->u.i + 1; 5858 } 5859 pMem->u.i = v; 5860 } 5861 #endif 5862 if( pC->useRandomRowid ){ 5863 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the 5864 ** largest possible integer (`9223372036854775807`) then the database 5865 ** engine starts picking positive candidate ROWIDs at random until 5866 ** it finds one that is not previously used. */ 5867 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is 5868 ** an AUTOINCREMENT table. */ 5869 cnt = 0; 5870 do{ 5871 sqlite3_randomness(sizeof(v), &v); 5872 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ 5873 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v, 5874 0, &res))==SQLITE_OK) 5875 && (res==0) 5876 && (++cnt<100)); 5877 if( rc ) goto abort_due_to_error; 5878 if( res==0 ){ 5879 rc = SQLITE_FULL; /* IMP: R-38219-53002 */ 5880 goto abort_due_to_error; 5881 } 5882 assert( v>0 ); /* EV: R-40812-03570 */ 5883 } 5884 pC->deferredMoveto = 0; 5885 pC->cacheStatus = CACHE_STALE; 5886 } 5887 pOut->u.i = v; 5888 break; 5889 } 5890 5891 /* Opcode: Insert P1 P2 P3 P4 P5 5892 ** Synopsis: intkey=r[P3] data=r[P2] 5893 ** 5894 ** Write an entry into the table of cursor P1. A new entry is 5895 ** created if it doesn't already exist or the data for an existing 5896 ** entry is overwritten. The data is the value MEM_Blob stored in register 5897 ** number P2. The key is stored in register P3. The key must 5898 ** be a MEM_Int. 5899 ** 5900 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is 5901 ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, 5902 ** then rowid is stored for subsequent return by the 5903 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). 5904 ** 5905 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might 5906 ** run faster by avoiding an unnecessary seek on cursor P1. However, 5907 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior 5908 ** seeks on the cursor or if the most recent seek used a key equal to P3. 5909 ** 5910 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an 5911 ** UPDATE operation. Otherwise (if the flag is clear) then this opcode 5912 ** is part of an INSERT operation. The difference is only important to 5913 ** the update hook. 5914 ** 5915 ** Parameter P4 may point to a Table structure, or may be NULL. If it is 5916 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked 5917 ** following a successful insert. 5918 ** 5919 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically 5920 ** allocated, then ownership of P2 is transferred to the pseudo-cursor 5921 ** and register P2 becomes ephemeral. If the cursor is changed, the 5922 ** value of register P2 will then change. Make sure this does not 5923 ** cause any problems.) 5924 ** 5925 ** This instruction only works on tables. The equivalent instruction 5926 ** for indices is OP_IdxInsert. 5927 */ 5928 case OP_Insert: { 5929 Mem *pData; /* MEM cell holding data for the record to be inserted */ 5930 Mem *pKey; /* MEM cell holding key for the record */ 5931 VdbeCursor *pC; /* Cursor to table into which insert is written */ 5932 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ 5933 const char *zDb; /* database name - used by the update hook */ 5934 Table *pTab; /* Table structure - used by update and pre-update hooks */ 5935 BtreePayload x; /* Payload to be inserted */ 5936 5937 pData = &aMem[pOp->p2]; 5938 assert( pOp->p1>=0 && pOp->p1nCursor ); 5939 assert( memIsValid(pData) ); 5940 pC = p->apCsr[pOp->p1]; 5941 assert( pC!=0 ); 5942 assert( pC->eCurType==CURTYPE_BTREE ); 5943 assert( pC->deferredMoveto==0 ); 5944 assert( pC->uc.pCursor!=0 ); 5945 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable ); 5946 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); 5947 REGISTER_TRACE(pOp->p2, pData); 5948 sqlite3VdbeIncrWriteCounter(p, pC); 5949 5950 pKey = &aMem[pOp->p3]; 5951 assert( pKey->flags & MEM_Int ); 5952 assert( memIsValid(pKey) ); 5953 REGISTER_TRACE(pOp->p3, pKey); 5954 x.nKey = pKey->u.i; 5955 5956 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ 5957 assert( pC->iDb>=0 ); 5958 if( p->expired==1 ) goto abort_due_to_expired; 5959 zDb = db->aDb[pC->iDb].zDbSName; 5960 pTab = pOp->p4.pTab; 5961 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) ); 5962 }else{ 5963 pTab = 0; 5964 zDb = 0; 5965 } 5966 5967 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 5968 /* Invoke the pre-update hook, if any */ 5969 if( pTab ){ 5970 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){ 5971 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1); 5972 } 5973 if( db->xUpdateCallback==0 || pTab->aCol==0 ){ 5974 /* Prevent post-update hook from running in cases when it should not */ 5975 pTab = 0; 5976 } 5977 } 5978 if( pOp->p5 & OPFLAG_ISNOOP ) break; 5979 #endif 5980 5981 assert( (pOp->p5 & OPFLAG_LASTROWID)==0 || (pOp->p5 & OPFLAG_NCHANGE)!=0 ); 5982 if( pOp->p5 & OPFLAG_NCHANGE ){ 5983 p->nChange++; 5984 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey; 5985 } 5986 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 ); 5987 x.pData = pData->z; 5988 x.nData = pData->n; 5989 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); 5990 if( pData->flags & MEM_Zero ){ 5991 x.nZero = pData->u.nZero; 5992 }else{ 5993 x.nZero = 0; 5994 } 5995 x.pKey = 0; 5996 assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT ); 5997 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, 5998 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)), 5999 seekResult 6000 ); 6001 pC->deferredMoveto = 0; 6002 pC->cacheStatus = CACHE_STALE; 6003 colCacheCtr++; 6004 6005 /* Invoke the update-hook if required. */ 6006 if( rc ) goto abort_due_to_error; 6007 if( pTab ){ 6008 assert( db->xUpdateCallback!=0 ); 6009 assert( pTab->aCol!=0 ); 6010 db->xUpdateCallback(db->pUpdateArg, 6011 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT, 6012 zDb, pTab->zName, x.nKey); 6013 } 6014 break; 6015 } 6016 6017 /* Opcode: RowCell P1 P2 P3 * * 6018 ** 6019 ** P1 and P2 are both open cursors. Both must be opened on the same type 6020 ** of table - intkey or index. This opcode is used as part of copying 6021 ** the current row from P2 into P1. If the cursors are opened on intkey 6022 ** tables, register P3 contains the rowid to use with the new record in 6023 ** P1. If they are opened on index tables, P3 is not used. 6024 ** 6025 ** This opcode must be followed by either an Insert or InsertIdx opcode 6026 ** with the OPFLAG_PREFORMAT flag set to complete the insert operation. 6027 */ 6028 case OP_RowCell: { 6029 VdbeCursor *pDest; /* Cursor to write to */ 6030 VdbeCursor *pSrc; /* Cursor to read from */ 6031 i64 iKey; /* Rowid value to insert with */ 6032 assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert ); 6033 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 ); 6034 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 ); 6035 assert( pOp[1].p5 & OPFLAG_PREFORMAT ); 6036 pDest = p->apCsr[pOp->p1]; 6037 pSrc = p->apCsr[pOp->p2]; 6038 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0; 6039 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey); 6040 if( rc!=SQLITE_OK ) goto abort_due_to_error; 6041 break; 6042 }; 6043 6044 /* Opcode: Delete P1 P2 P3 P4 P5 6045 ** 6046 ** Delete the record at which the P1 cursor is currently pointing. 6047 ** 6048 ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then 6049 ** the cursor will be left pointing at either the next or the previous 6050 ** record in the table. If it is left pointing at the next record, then 6051 ** the next Next instruction will be a no-op. As a result, in this case 6052 ** it is ok to delete a record from within a Next loop. If 6053 ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be 6054 ** left in an undefined state. 6055 ** 6056 ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this 6057 ** delete is one of several associated with deleting a table row and 6058 ** all its associated index entries. Exactly one of those deletes is 6059 ** the "primary" delete. The others are all on OPFLAG_FORDELETE 6060 ** cursors or else are marked with the AUXDELETE flag. 6061 ** 6062 ** If the OPFLAG_NCHANGE (0x01) flag of P2 (NB: P2 not P5) is set, then 6063 ** the row change count is incremented (otherwise not). 6064 ** 6065 ** If the OPFLAG_ISNOOP (0x40) flag of P2 (not P5!) is set, then the 6066 ** pre-update-hook for deletes is run, but the btree is otherwise unchanged. 6067 ** This happens when the OP_Delete is to be shortly followed by an OP_Insert 6068 ** with the same key, causing the btree entry to be overwritten. 6069 ** 6070 ** P1 must not be pseudo-table. It has to be a real table with 6071 ** multiple rows. 6072 ** 6073 ** If P4 is not NULL then it points to a Table object. In this case either 6074 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must 6075 ** have been positioned using OP_NotFound prior to invoking this opcode in 6076 ** this case. Specifically, if one is configured, the pre-update hook is 6077 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured, 6078 ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2. 6079 ** 6080 ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address 6081 ** of the memory cell that contains the value that the rowid of the row will 6082 ** be set to by the update. 6083 */ 6084 case OP_Delete: { 6085 VdbeCursor *pC; 6086 const char *zDb; 6087 Table *pTab; 6088 int opflags; 6089 6090 opflags = pOp->p2; 6091 assert( pOp->p1>=0 && pOp->p1nCursor ); 6092 pC = p->apCsr[pOp->p1]; 6093 assert( pC!=0 ); 6094 assert( pC->eCurType==CURTYPE_BTREE ); 6095 assert( pC->uc.pCursor!=0 ); 6096 assert( pC->deferredMoveto==0 ); 6097 sqlite3VdbeIncrWriteCounter(p, pC); 6098 6099 #ifdef SQLITE_DEBUG 6100 if( pOp->p4type==P4_TABLE 6101 && !p->expired 6102 && HasRowid(pOp->p4.pTab) 6103 && pOp->p5==0 6104 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) 6105 ){ 6106 /* If p5 is zero, the seek operation that positioned the cursor prior to 6107 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of 6108 ** the row that is being deleted */ 6109 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor); 6110 assert( CORRUPT_DB || pC->movetoTarget==iKey ); 6111 } 6112 #endif 6113 6114 /* If the update-hook or pre-update-hook will be invoked, set zDb to 6115 ** the name of the db to pass as to it. Also set local pTab to a copy 6116 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was 6117 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set 6118 ** VdbeCursor.movetoTarget to the current rowid. */ 6119 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ 6120 if( p->expired==1 ) goto abort_due_to_expired; 6121 assert( pC->iDb>=0 ); 6122 assert( pOp->p4.pTab!=0 ); 6123 zDb = db->aDb[pC->iDb].zDbSName; 6124 pTab = pOp->p4.pTab; 6125 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){ 6126 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor); 6127 } 6128 }else{ 6129 zDb = 0; 6130 pTab = 0; 6131 } 6132 6133 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 6134 /* Invoke the pre-update-hook if required. */ 6135 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab ); 6136 if( db->xPreUpdateCallback && pTab ){ 6137 assert( !(opflags & OPFLAG_ISUPDATE) 6138 || HasRowid(pTab)==0 6139 || (aMem[pOp->p3].flags & MEM_Int) 6140 ); 6141 sqlite3VdbePreUpdateHook(p, pC, 6142 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, 6143 zDb, pTab, pC->movetoTarget, 6144 pOp->p3, -1 6145 ); 6146 } 6147 if( opflags & OPFLAG_ISNOOP ) break; 6148 #endif 6149 6150 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ 6151 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 ); 6152 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION ); 6153 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE ); 6154 6155 #ifdef SQLITE_DEBUG 6156 if( p->pFrame==0 ){ 6157 if( pC->isEphemeral==0 6158 && (pOp->p5 & OPFLAG_AUXDELETE)==0 6159 && (pC->wrFlag & OPFLAG_FORDELETE)==0 6160 ){ 6161 nExtraDelete++; 6162 } 6163 if( pOp->p2 & OPFLAG_NCHANGE ){ 6164 nExtraDelete--; 6165 } 6166 } 6167 #endif 6168 6169 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); 6170 pC->cacheStatus = CACHE_STALE; 6171 colCacheCtr++; 6172 pC->seekResult = 0; 6173 if( rc ) goto abort_due_to_error; 6174 6175 /* Invoke the update-hook if required. */ 6176 if( opflags & OPFLAG_NCHANGE ){ 6177 p->nChange++; 6178 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){ 6179 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName, 6180 pC->movetoTarget); 6181 assert( pC->iDb>=0 ); 6182 } 6183 } 6184 6185 break; 6186 } 6187 /* Opcode: ResetCount * * * * * 6188 ** 6189 ** The value of the change counter is copied to the database handle 6190 ** change counter (returned by subsequent calls to sqlite3_changes()). 6191 ** Then the VMs internal change counter resets to 0. 6192 ** This is used by trigger programs. 6193 */ 6194 case OP_ResetCount: { 6195 sqlite3VdbeSetChanges(db, p->nChange); 6196 p->nChange = 0; 6197 break; 6198 } 6199 6200 /* Opcode: SorterCompare P1 P2 P3 P4 6201 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 6202 ** 6203 ** P1 is a sorter cursor. This instruction compares a prefix of the 6204 ** record blob in register P3 against a prefix of the entry that 6205 ** the sorter cursor currently points to. Only the first P4 fields 6206 ** of r[P3] and the sorter record are compared. 6207 ** 6208 ** If either P3 or the sorter contains a NULL in one of their significant 6209 ** fields (not counting the P4 fields at the end which are ignored) then 6210 ** the comparison is assumed to be equal. 6211 ** 6212 ** Fall through to next instruction if the two records compare equal to 6213 ** each other. Jump to P2 if they are different. 6214 */ 6215 case OP_SorterCompare: { 6216 VdbeCursor *pC; 6217 int res; 6218 int nKeyCol; 6219 6220 pC = p->apCsr[pOp->p1]; 6221 assert( isSorter(pC) ); 6222 assert( pOp->p4type==P4_INT32 ); 6223 pIn3 = &aMem[pOp->p3]; 6224 nKeyCol = pOp->p4.i; 6225 res = 0; 6226 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); 6227 VdbeBranchTaken(res!=0,2); 6228 if( rc ) goto abort_due_to_error; 6229 if( res ) goto jump_to_p2; 6230 break; 6231 }; 6232 6233 /* Opcode: SorterData P1 P2 P3 * * 6234 ** Synopsis: r[P2]=data 6235 ** 6236 ** Write into register P2 the current sorter data for sorter cursor P1. 6237 ** Then clear the column header cache on cursor P3. 6238 ** 6239 ** This opcode is normally used to move a record out of the sorter and into 6240 ** a register that is the source for a pseudo-table cursor created using 6241 ** OpenPseudo. That pseudo-table cursor is the one that is identified by 6242 ** parameter P3. Clearing the P3 column cache as part of this opcode saves 6243 ** us from having to issue a separate NullRow instruction to clear that cache. 6244 */ 6245 case OP_SorterData: { /* ncycle */ 6246 VdbeCursor *pC; 6247 6248 pOut = &aMem[pOp->p2]; 6249 pC = p->apCsr[pOp->p1]; 6250 assert( isSorter(pC) ); 6251 rc = sqlite3VdbeSorterRowkey(pC, pOut); 6252 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); 6253 assert( pOp->p1>=0 && pOp->p1nCursor ); 6254 if( rc ) goto abort_due_to_error; 6255 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; 6256 break; 6257 } 6258 6259 /* Opcode: RowData P1 P2 P3 * * 6260 ** Synopsis: r[P2]=data 6261 ** 6262 ** Write into register P2 the complete row content for the row at 6263 ** which cursor P1 is currently pointing. 6264 ** There is no interpretation of the data. 6265 ** It is just copied onto the P2 register exactly as 6266 ** it is found in the database file. 6267 ** 6268 ** If cursor P1 is an index, then the content is the key of the row. 6269 ** If cursor P2 is a table, then the content extracted is the data. 6270 ** 6271 ** If the P1 cursor must be pointing to a valid row (not a NULL row) 6272 ** of a real table, not a pseudo-table. 6273 ** 6274 ** If P3!=0 then this opcode is allowed to make an ephemeral pointer 6275 ** into the database page. That means that the content of the output 6276 ** register will be invalidated as soon as the cursor moves - including 6277 ** moves caused by other cursors that "save" the current cursors 6278 ** position in order that they can write to the same table. If P3==0 6279 ** then a copy of the data is made into memory. P3!=0 is faster, but 6280 ** P3==0 is safer. 6281 ** 6282 ** If P3!=0 then the content of the P2 register is unsuitable for use 6283 ** in OP_Result and any OP_Result will invalidate the P2 register content. 6284 ** The P2 register content is invalidated by opcodes like OP_Function or 6285 ** by any use of another cursor pointing to the same table. 6286 */ 6287 case OP_RowData: { 6288 VdbeCursor *pC; 6289 BtCursor *pCrsr; 6290 u32 n; 6291 6292 pOut = out2Prerelease(p, pOp); 6293 6294 assert( pOp->p1>=0 && pOp->p1nCursor ); 6295 pC = p->apCsr[pOp->p1]; 6296 assert( pC!=0 ); 6297 assert( pC->eCurType==CURTYPE_BTREE ); 6298 assert( isSorter(pC)==0 ); 6299 assert( pC->nullRow==0 ); 6300 assert( pC->uc.pCursor!=0 ); 6301 pCrsr = pC->uc.pCursor; 6302 6303 /* The OP_RowData opcodes always follow OP_NotExists or 6304 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions 6305 ** that might invalidate the cursor. 6306 ** If this were not the case, one of the following assert()s 6307 ** would fail. Should this ever change (because of changes in the code 6308 ** generator) then the fix would be to insert a call to 6309 ** sqlite3VdbeCursorMoveto(). 6310 */ 6311 assert( pC->deferredMoveto==0 ); 6312 assert( sqlite3BtreeCursorIsValid(pCrsr) ); 6313 6314 n = sqlite3BtreePayloadSize(pCrsr); 6315 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ 6316 goto too_big; 6317 } 6318 testcase( n==0 ); 6319 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut); 6320 if( rc ) goto abort_due_to_error; 6321 if( !pOp->p3 ) Deephemeralize(pOut); 6322 UPDATE_MAX_BLOBSIZE(pOut); 6323 REGISTER_TRACE(pOp->p2, pOut); 6324 break; 6325 } 6326 6327 /* Opcode: Rowid P1 P2 * * * 6328 ** Synopsis: r[P2]=PX rowid of P1 6329 ** 6330 ** Store in register P2 an integer which is the key of the table entry that 6331 ** P1 is currently point to. 6332 ** 6333 ** P1 can be either an ordinary table or a virtual table. There used to 6334 ** be a separate OP_VRowid opcode for use with virtual tables, but this 6335 ** one opcode now works for both table types. 6336 */ 6337 case OP_Rowid: { /* out2, ncycle */ 6338 VdbeCursor *pC; 6339 i64 v; 6340 sqlite3_vtab *pVtab; 6341 const sqlite3_module *pModule; 6342 6343 pOut = out2Prerelease(p, pOp); 6344 assert( pOp->p1>=0 && pOp->p1nCursor ); 6345 pC = p->apCsr[pOp->p1]; 6346 assert( pC!=0 ); 6347 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); 6348 if( pC->nullRow ){ 6349 pOut->flags = MEM_Null; 6350 break; 6351 }else if( pC->deferredMoveto ){ 6352 v = pC->movetoTarget; 6353 #ifndef SQLITE_OMIT_VIRTUALTABLE 6354 }else if( pC->eCurType==CURTYPE_VTAB ){ 6355 assert( pC->uc.pVCur!=0 ); 6356 pVtab = pC->uc.pVCur->pVtab; 6357 pModule = pVtab->pModule; 6358 assert( pModule->xRowid ); 6359 rc = pModule->xRowid(pC->uc.pVCur, &v); 6360 sqlite3VtabImportErrmsg(p, pVtab); 6361 if( rc ) goto abort_due_to_error; 6362 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6363 }else{ 6364 assert( pC->eCurType==CURTYPE_BTREE ); 6365 assert( pC->uc.pCursor!=0 ); 6366 rc = sqlite3VdbeCursorRestore(pC); 6367 if( rc ) goto abort_due_to_error; 6368 if( pC->nullRow ){ 6369 pOut->flags = MEM_Null; 6370 break; 6371 } 6372 v = sqlite3BtreeIntegerKey(pC->uc.pCursor); 6373 } 6374 pOut->u.i = v; 6375 break; 6376 } 6377 6378 /* Opcode: Stale P1 * * * * 6379 ** 6380 ** Mark the row cache for cursor P1 as stale. This opcode is used 6381 ** on pseudo-cursors right before the content of the register that 6382 ** holds their record is modified. 6383 */ 6384 case OP_Stale: { 6385 VdbeCursor *pC; 6386 6387 pC = p->apCsr[pOp->p1]; 6388 assert( pC!=0 ); 6389 assert( pC->eCurType==CURTYPE_PSEUDO ); 6390 pC->cacheStatus = CACHE_STALE; 6391 break; 6392 } 6393 6394 /* Opcode: NullRow P1 P2 * * * 6395 ** 6396 ** Move the cursor P1 to a null row if P2 is 0. Any OP_Column operations 6397 ** that occur while the cursor is on the null row will always 6398 ** write a NULL. 6399 ** 6400 ** If cursor P1 is not previously opened, open it now to a special 6401 ** pseudo-cursor that always returns NULL for every column. 6402 ** 6403 ** If P2 is not 0, then the nullRow flag on the pseudo-cursor is 6404 ** turned off. 6405 */ 6406 case OP_NullRow: { 6407 VdbeCursor *pC; 6408 Mem *pMem; 6409 6410 assert( pOp->p1>=0 && pOp->p1nCursor ); 6411 pC = p->apCsr[pOp->p1]; 6412 if( pC==0 ){ 6413 /* If the cursor is not already open, create a special kind of 6414 ** pseudo-cursor that always gives null rows. */ 6415 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO); 6416 if( pC==0 ) goto no_mem; 6417 pC->seekResult = 0; 6418 pC->isTable = 1; 6419 pC->noReuse = 1; 6420 pC->uc.pCursor = sqlite3BtreeFakeValidCursor(); 6421 } 6422 pC->nullRow = (pOp->p2==0); 6423 pC->cacheStatus = CACHE_STALE; 6424 if( pC->eCurType==CURTYPE_BTREE ){ 6425 assert( pC->uc.pCursor!=0 ); 6426 sqlite3BtreeClearCursor(pC->uc.pCursor); 6427 }else if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){ 6428 static char nullRecord[] = { 0x02, 0x00 }; 6429 pMem = &aMem[pC->seekResult]; 6430 assert( !VdbeMemDynamic(pMem) ); 6431 pC->nullRow = 1; 6432 pMem->z = nullRecord; 6433 pMem->n = 2; 6434 pMem->flags = MEM_Blob|MEM_Ephem; 6435 } 6436 #ifdef SQLITE_DEBUG 6437 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow; 6438 #endif 6439 break; 6440 } 6441 6442 /* Opcode: SeekEnd P1 * * * * 6443 ** 6444 ** Position cursor P1 at the end of the btree for the purpose of 6445 ** appending a new entry onto the btree. 6446 ** 6447 ** It is assumed that the cursor is used only for appending and so 6448 ** if the cursor is valid, then the cursor must already be pointing 6449 ** at the end of the btree and so no changes are made to 6450 ** the cursor. 6451 */ 6452 /* Opcode: Last P1 P2 * * * 6453 ** 6454 ** The next use of the Rowid or Column or Prev instruction for P1 6455 ** will refer to the last entry in the database table or index. 6456 ** If the table or index is empty and P2>0, then jump immediately to P2. 6457 ** If P2 is 0 or if the table or index is not empty, fall through 6458 ** to the following instruction. 6459 ** 6460 ** This opcode leaves the cursor configured to move in reverse order, 6461 ** from the end toward the beginning. In other words, the cursor is 6462 ** configured to use Prev, not Next. 6463 */ 6464 case OP_SeekEnd: /* ncycle */ 6465 case OP_Last: { /* jump0, ncycle */ 6466 VdbeCursor *pC; 6467 BtCursor *pCrsr; 6468 int res; 6469 6470 assert( pOp->p1>=0 && pOp->p1nCursor ); 6471 pC = p->apCsr[pOp->p1]; 6472 assert( pC!=0 ); 6473 assert( pC->eCurType==CURTYPE_BTREE ); 6474 pCrsr = pC->uc.pCursor; 6475 res = 0; 6476 assert( pCrsr!=0 ); 6477 #ifdef SQLITE_DEBUG 6478 pC->seekOp = pOp->opcode; 6479 #endif 6480 if( pOp->opcode==OP_SeekEnd ){ 6481 assert( pOp->p2==0 ); 6482 pC->seekResult = -1; 6483 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){ 6484 break; 6485 } 6486 } 6487 rc = sqlite3BtreeLast(pCrsr, &res); 6488 pC->nullRow = (u8)res; 6489 pC->deferredMoveto = 0; 6490 pC->cacheStatus = CACHE_STALE; 6491 if( rc ) goto abort_due_to_error; 6492 if( pOp->p2>0 ){ 6493 VdbeBranchTaken(res!=0,2); 6494 if( res ) goto jump_to_p2; 6495 } 6496 break; 6497 } 6498 6499 /* Opcode: IfSizeBetween P1 P2 P3 P4 * 6500 ** 6501 ** Let N be the approximate number of rows in the table or index 6502 ** with cursor P1 and let X be 10*log2(N) if N is positive or -1 6503 ** if N is zero. 6504 ** 6505 ** Jump to P2 if X is in between P3 and P4, inclusive. 6506 */ 6507 case OP_IfSizeBetween: { /* jump */ 6508 VdbeCursor *pC; 6509 BtCursor *pCrsr; 6510 int res; 6511 i64 sz; 6512 6513 assert( pOp->p1>=0 && pOp->p1nCursor ); 6514 assert( pOp->p4type==P4_INT32 ); 6515 assert( pOp->p3>=-1 && pOp->p3<=640*2 ); 6516 assert( pOp->p4.i>=-1 && pOp->p4.i<=640*2 ); 6517 pC = p->apCsr[pOp->p1]; 6518 assert( pC!=0 ); 6519 pCrsr = pC->uc.pCursor; 6520 assert( pCrsr ); 6521 rc = sqlite3BtreeFirst(pCrsr, &res); 6522 if( rc ) goto abort_due_to_error; 6523 if( res!=0 ){ 6524 sz = -1; /* -Infinity encoding */ 6525 }else{ 6526 sz = sqlite3BtreeRowCountEst(pCrsr); 6527 assert( sz>0 ); 6528 sz = sqlite3LogEst((u64)sz); 6529 } 6530 res = sz>=pOp->p3 && sz<=pOp->p4.i; 6531 VdbeBranchTaken(res!=0,2); 6532 if( res ) goto jump_to_p2; 6533 break; 6534 } 6535 6536 6537 /* Opcode: SorterSort P1 P2 * * * 6538 ** 6539 ** After all records have been inserted into the Sorter object 6540 ** identified by P1, invoke this opcode to actually do the sorting. 6541 ** Jump to P2 if there are no records to be sorted. 6542 ** 6543 ** This opcode is an alias for OP_Sort and OP_Rewind that is used 6544 ** for Sorter objects. 6545 */ 6546 /* Opcode: Sort P1 P2 * * * 6547 ** 6548 ** This opcode does exactly the same thing as OP_Rewind except that 6549 ** it increments an undocumented global variable used for testing. 6550 ** 6551 ** Sorting is accomplished by writing records into a sorting index, 6552 ** then rewinding that index and playing it back from beginning to 6553 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the 6554 ** rewinding so that the global variable will be incremented and 6555 ** regression tests can determine whether or not the optimizer is 6556 ** correctly optimizing out sorts. 6557 */ 6558 case OP_SorterSort: /* jump ncycle */ 6559 case OP_Sort: { /* jump ncycle */ 6560 #ifdef SQLITE_TEST 6561 sqlite3_sort_count++; 6562 sqlite3_search_count--; 6563 #endif 6564 p->aCounter[SQLITE_STMTSTATUS_SORT]++; 6565 /* Fall through into OP_Rewind */ 6566 /* no break */ deliberate_fall_through 6567 } 6568 /* Opcode: Rewind P1 P2 * * * 6569 ** 6570 ** The next use of the Rowid or Column or Next instruction for P1 6571 ** will refer to the first entry in the database table or index. 6572 ** If the table or index is empty, jump immediately to P2. 6573 ** If the table or index is not empty, fall through to the following 6574 ** instruction. 6575 ** 6576 ** If P2 is zero, that is an assertion that the P1 table is never 6577 ** empty and hence the jump will never be taken. 6578 ** 6579 ** This opcode leaves the cursor configured to move in forward order, 6580 ** from the beginning toward the end. In other words, the cursor is 6581 ** configured to use Next, not Prev. 6582 */ 6583 case OP_Rewind: { /* jump0, ncycle */ 6584 VdbeCursor *pC; 6585 BtCursor *pCrsr; 6586 int res; 6587 6588 assert( pOp->p1>=0 && pOp->p1nCursor ); 6589 assert( pOp->p5==0 ); 6590 assert( pOp->p2>=0 && pOp->p2nOp ); 6591 6592 pC = p->apCsr[pOp->p1]; 6593 assert( pC!=0 ); 6594 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) ); 6595 res = 1; 6596 #ifdef SQLITE_DEBUG 6597 pC->seekOp = OP_Rewind; 6598 #endif 6599 if( isSorter(pC) ){ 6600 rc = sqlite3VdbeSorterRewind(pC, &res); 6601 }else{ 6602 assert( pC->eCurType==CURTYPE_BTREE ); 6603 pCrsr = pC->uc.pCursor; 6604 assert( pCrsr ); 6605 rc = sqlite3BtreeFirst(pCrsr, &res); 6606 pC->deferredMoveto = 0; 6607 pC->cacheStatus = CACHE_STALE; 6608 } 6609 if( rc ) goto abort_due_to_error; 6610 pC->nullRow = (u8)res; 6611 if( pOp->p2>0 ){ 6612 VdbeBranchTaken(res!=0,2); 6613 if( res ) goto jump_to_p2; 6614 } 6615 break; 6616 } 6617 6618 /* Opcode: IfEmpty P1 P2 * * * 6619 ** Synopsis: if( empty(P1) ) goto P2 6620 ** 6621 ** Check to see if the b-tree table that cursor P1 references is empty 6622 ** and jump to P2 if it is. 6623 */ 6624 case OP_IfEmpty: { /* jump */ 6625 VdbeCursor *pC; 6626 BtCursor *pCrsr; 6627 int res; 6628 6629 assert( pOp->p1>=0 && pOp->p1nCursor ); 6630 assert( pOp->p2>=0 && pOp->p2nOp ); 6631 6632 pC = p->apCsr[pOp->p1]; 6633 assert( pC!=0 ); 6634 assert( pC->eCurType==CURTYPE_BTREE ); 6635 pCrsr = pC->uc.pCursor; 6636 assert( pCrsr ); 6637 rc = sqlite3BtreeIsEmpty(pCrsr, &res); 6638 if( rc ) goto abort_due_to_error; 6639 VdbeBranchTaken(res!=0,2); 6640 if( res ) goto jump_to_p2; 6641 break; 6642 } 6643 6644 /* Opcode: Next P1 P2 P3 * P5 6645 ** 6646 ** Advance cursor P1 so that it points to the next key/data pair in its 6647 ** table or index. If there are no more key/value pairs then fall through 6648 ** to the following instruction. But if the cursor advance was successful, 6649 ** jump immediately to P2. 6650 ** 6651 ** The Next opcode is only valid following an SeekGT, SeekGE, or 6652 ** OP_Rewind opcode used to position the cursor. Next is not allowed 6653 ** to follow SeekLT, SeekLE, or OP_Last. 6654 ** 6655 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have 6656 ** been opened prior to this opcode or the program will segfault. 6657 ** 6658 ** The P3 value is a hint to the btree implementation. If P3==1, that 6659 ** means P1 is an SQL index and that this instruction could have been 6660 ** omitted if that index had been unique. P3 is usually 0. P3 is 6661 ** always either 0 or 1. 6662 ** 6663 ** If P5 is positive and the jump is taken, then event counter 6664 ** number P5-1 in the prepared statement is incremented. 6665 ** 6666 ** See also: Prev 6667 */ 6668 /* Opcode: Prev P1 P2 P3 * P5 6669 ** 6670 ** Back up cursor P1 so that it points to the previous key/data pair in its 6671 ** table or index. If there is no previous key/value pairs then fall through 6672 ** to the following instruction. But if the cursor backup was successful, 6673 ** jump immediately to P2. 6674 ** 6675 ** 6676 ** The Prev opcode is only valid following an SeekLT, SeekLE, or 6677 ** OP_Last opcode used to position the cursor. Prev is not allowed 6678 ** to follow SeekGT, SeekGE, or OP_Rewind. 6679 ** 6680 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is 6681 ** not open then the behavior is undefined. 6682 ** 6683 ** The P3 value is a hint to the btree implementation. If P3==1, that 6684 ** means P1 is an SQL index and that this instruction could have been 6685 ** omitted if that index had been unique. P3 is usually 0. P3 is 6686 ** always either 0 or 1. 6687 ** 6688 ** If P5 is positive and the jump is taken, then event counter 6689 ** number P5-1 in the prepared statement is incremented. 6690 */ 6691 /* Opcode: SorterNext P1 P2 * * P5 6692 ** 6693 ** This opcode works just like OP_Next except that P1 must be a 6694 ** sorter object for which the OP_SorterSort opcode has been 6695 ** invoked. This opcode advances the cursor to the next sorted 6696 ** record, or jumps to P2 if there are no more sorted records. 6697 */ 6698 case OP_SorterNext: { /* jump */ 6699 VdbeCursor *pC; 6700 6701 pC = p->apCsr[pOp->p1]; 6702 assert( isSorter(pC) ); 6703 rc = sqlite3VdbeSorterNext(db, pC); 6704 goto next_tail; 6705 6706 case OP_Prev: /* jump, ncycle */ 6707 assert( pOp->p1>=0 && pOp->p1nCursor ); 6708 assert( pOp->p5==0 6709 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP 6710 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX); 6711 pC = p->apCsr[pOp->p1]; 6712 assert( pC!=0 ); 6713 assert( pC->deferredMoveto==0 ); 6714 assert( pC->eCurType==CURTYPE_BTREE ); 6715 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE 6716 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope 6717 || pC->seekOp==OP_NullRow); 6718 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3); 6719 goto next_tail; 6720 6721 case OP_Next: /* jump, ncycle */ 6722 assert( pOp->p1>=0 && pOp->p1nCursor ); 6723 assert( pOp->p5==0 6724 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP 6725 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX); 6726 pC = p->apCsr[pOp->p1]; 6727 assert( pC!=0 ); 6728 assert( pC->deferredMoveto==0 ); 6729 assert( pC->eCurType==CURTYPE_BTREE ); 6730 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE 6731 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found 6732 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid 6733 || pC->seekOp==OP_IfNoHope); 6734 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3); 6735 6736 next_tail: 6737 pC->cacheStatus = CACHE_STALE; 6738 VdbeBranchTaken(rc==SQLITE_OK,2); 6739 if( rc==SQLITE_OK ){ 6740 pC->nullRow = 0; 6741 p->aCounter[pOp->p5]++; 6742 #ifdef SQLITE_TEST 6743 sqlite3_search_count++; 6744 #endif 6745 goto jump_to_p2_and_check_for_interrupt; 6746 } 6747 if( rc!=SQLITE_DONE ) goto abort_due_to_error; 6748 rc = SQLITE_OK; 6749 pC->nullRow = 1; 6750 goto check_for_interrupt; 6751 } 6752 6753 /* Opcode: IdxInsert P1 P2 P3 P4 P5 6754 ** Synopsis: key=r[P2] 6755 ** 6756 ** Register P2 holds an SQL index key made using the 6757 ** MakeRecord instructions. This opcode writes that key 6758 ** into the index P1. Data for the entry is nil. 6759 ** 6760 ** If P4 is not zero, then it is the number of values in the unpacked 6761 ** key of reg(P2). In that case, P3 is the index of the first register 6762 ** for the unpacked key. The availability of the unpacked key can sometimes 6763 ** be an optimization. 6764 ** 6765 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer 6766 ** that this insert is likely to be an append. 6767 ** 6768 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is 6769 ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, 6770 ** then the change counter is unchanged. 6771 ** 6772 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might 6773 ** run faster by avoiding an unnecessary seek on cursor P1. However, 6774 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior 6775 ** seeks on the cursor or if the most recent seek used a key equivalent 6776 ** to P2. 6777 ** 6778 ** This instruction only works for indices. The equivalent instruction 6779 ** for tables is OP_Insert. 6780 */ 6781 case OP_IdxInsert: { /* in2 */ 6782 VdbeCursor *pC; 6783 BtreePayload x; 6784 6785 assert( pOp->p1>=0 && pOp->p1nCursor ); 6786 pC = p->apCsr[pOp->p1]; 6787 sqlite3VdbeIncrWriteCounter(p, pC); 6788 assert( pC!=0 ); 6789 assert( !isSorter(pC) ); 6790 pIn2 = &aMem[pOp->p2]; 6791 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) ); 6792 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; 6793 assert( pC->eCurType==CURTYPE_BTREE ); 6794 assert( pC->isTable==0 ); 6795 rc = ExpandBlob(pIn2); 6796 if( rc ) goto abort_due_to_error; 6797 x.nKey = pIn2->n; 6798 x.pKey = pIn2->z; 6799 x.aMem = aMem + pOp->p3; 6800 x.nMem = (u16)pOp->p4.i; 6801 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, 6802 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)), 6803 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) 6804 ); 6805 assert( pC->deferredMoveto==0 ); 6806 pC->cacheStatus = CACHE_STALE; 6807 if( rc) goto abort_due_to_error; 6808 break; 6809 } 6810 6811 /* Opcode: SorterInsert P1 P2 * * * 6812 ** Synopsis: key=r[P2] 6813 ** 6814 ** Register P2 holds an SQL index key made using the 6815 ** MakeRecord instructions. This opcode writes that key 6816 ** into the sorter P1. Data for the entry is nil. 6817 */ 6818 case OP_SorterInsert: { /* in2 */ 6819 VdbeCursor *pC; 6820 6821 assert( pOp->p1>=0 && pOp->p1nCursor ); 6822 pC = p->apCsr[pOp->p1]; 6823 sqlite3VdbeIncrWriteCounter(p, pC); 6824 assert( pC!=0 ); 6825 assert( isSorter(pC) ); 6826 pIn2 = &aMem[pOp->p2]; 6827 assert( pIn2->flags & MEM_Blob ); 6828 assert( pC->isTable==0 ); 6829 rc = ExpandBlob(pIn2); 6830 if( rc ) goto abort_due_to_error; 6831 rc = sqlite3VdbeSorterWrite(pC, pIn2); 6832 if( rc) goto abort_due_to_error; 6833 break; 6834 } 6835 6836 /* Opcode: IdxDelete P1 P2 P3 P4 P5 6837 ** Synopsis: key=r[P2@P5] 6838 ** 6839 ** The content of P5 registers starting at register P2 form 6840 ** an unpacked index key. This opcode removes that entry from the 6841 ** index opened by cursor P1. 6842 ** 6843 ** P4 is a pointer to an Index structure. 6844 ** 6845 ** If P3 is non-zero, it is the register number of a register holding 6846 ** a record that will be inserted into this index. If that record is 6847 ** identical to the one that would be deleted by this instruction, 6848 ** skip the delete and set register P3 to NULL. 6849 ** 6850 ** Raise an SQLITE_CORRUPT_INDEX error if no matching index entry is found 6851 ** and not in writable_schema mode. 6852 */ 6853 case OP_IdxDelete: { 6854 VdbeCursor *pC; 6855 BtCursor *pCrsr; 6856 int res; 6857 UnpackedRecord r; 6858 6859 assert( pOp->p5>0 ); 6860 assert( pOp->p2>0 && pOp->p2+pOp->p5<=(p->nMem+1 - p->nCursor)+1 ); 6861 assert( pOp->p1>=0 && pOp->p1nCursor ); 6862 pC = p->apCsr[pOp->p1]; 6863 assert( pC!=0 ); 6864 assert( pC->eCurType==CURTYPE_BTREE ); 6865 sqlite3VdbeIncrWriteCounter(p, pC); 6866 pCrsr = pC->uc.pCursor; 6867 assert( pCrsr!=0 ); 6868 r.pKeyInfo = pC->pKeyInfo; 6869 r.nField = pOp->p5; 6870 r.default_rc = 0; 6871 r.aMem = &aMem[pOp->p2]; 6872 rc = sqlite3BtreeIndexMoveto(pCrsr, &r, &res); 6873 if( rc ) goto abort_due_to_error; 6874 if( res!=0 ){ 6875 if( p->expired==1 ) goto abort_due_to_expired; 6876 rc = sqlite3VdbeFindIndexKey(pCrsr, pOp->p4.pIdx, &r, &res, 0); 6877 if( rc!=SQLITE_OK ) goto abort_due_to_error; 6878 if( res!=0 ){ 6879 if( !sqlite3WritableSchema(db) ){ 6880 rc = sqlite3ReportError( 6881 SQLITE_CORRUPT_INDEX, __LINE__, "index corruption"); 6882 goto abort_due_to_error; 6883 } 6884 pC->cacheStatus = CACHE_STALE; 6885 pC->seekResult = 0; 6886 break; 6887 } 6888 } 6889 6890 if( pOp->p3 && vdbeIndexKeyCompare(pCrsr, &aMem[pOp->p3], &rc) ){ 6891 if( rc ) goto abort_due_to_error; 6892 sqlite3VdbeMemSetNull(&aMem[pOp->p3]); 6893 break; 6894 } 6895 6896 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); 6897 if( rc ) goto abort_due_to_error; 6898 assert( pC->deferredMoveto==0 ); 6899 pC->cacheStatus = CACHE_STALE; 6900 pC->seekResult = 0; 6901 break; 6902 } 6903 6904 /* Opcode: DeferredSeek P1 * P3 P4 * 6905 ** Synopsis: Move P3 to P1.rowid if needed 6906 ** 6907 ** P1 is an open index cursor and P3 is a cursor on the corresponding 6908 ** table. This opcode does a deferred seek of the P3 table cursor 6909 ** to the row that corresponds to the current row of P1. 6910 ** 6911 ** This is a deferred seek. Nothing actually happens until 6912 ** the cursor is used to read a record. That way, if no reads 6913 ** occur, no unnecessary I/O happens. 6914 ** 6915 ** P4 may be an array of integers (type P4_INTARRAY) containing 6916 ** one entry for each column in the P3 table. If array entry a(i) 6917 ** is non-zero, then reading column a(i)-1 from cursor P3 is 6918 ** equivalent to performing the deferred seek and then reading column i 6919 ** from P1. This information is stored in P3 and used to redirect 6920 ** reads against P3 over to P1, thus possibly avoiding the need to 6921 ** seek and read cursor P3. 6922 */ 6923 /* Opcode: IdxRowid P1 P2 * * * 6924 ** Synopsis: r[P2]=rowid 6925 ** 6926 ** Write into register P2 an integer which is the last entry in the record at 6927 ** the end of the index key pointed to by cursor P1. This integer should be 6928 ** the rowid of the table entry to which this index entry points. 6929 ** 6930 ** See also: Rowid, MakeRecord. 6931 */ 6932 case OP_DeferredSeek: /* ncycle */ 6933 case OP_IdxRowid: { /* out2, ncycle */ 6934 VdbeCursor *pC; /* The P1 index cursor */ 6935 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */ 6936 i64 rowid; /* Rowid that P1 current points to */ 6937 6938 assert( pOp->p1>=0 && pOp->p1nCursor ); 6939 pC = p->apCsr[pOp->p1]; 6940 assert( pC!=0 ); 6941 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) ); 6942 assert( pC->uc.pCursor!=0 ); 6943 assert( pC->isTable==0 || IsNullCursor(pC) ); 6944 assert( pC->deferredMoveto==0 ); 6945 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid ); 6946 6947 /* The IdxRowid and Seek opcodes are combined because of the commonality 6948 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */ 6949 rc = sqlite3VdbeCursorRestore(pC); 6950 6951 /* sqlite3VdbeCursorRestore() may fail if the cursor has been disturbed 6952 ** since it was last positioned and an error (e.g. OOM or an IO error) 6953 ** occurs while trying to reposition it. */ 6954 if( rc!=SQLITE_OK ) goto abort_due_to_error; 6955 6956 if( !pC->nullRow ){ 6957 rowid = 0; /* Not needed. Only used to silence a warning. */ 6958 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid); 6959 if( rc!=SQLITE_OK ){ 6960 goto abort_due_to_error; 6961 } 6962 if( pOp->opcode==OP_DeferredSeek ){ 6963 assert( pOp->p3>=0 && pOp->p3nCursor ); 6964 pTabCur = p->apCsr[pOp->p3]; 6965 assert( pTabCur!=0 ); 6966 assert( pTabCur->eCurType==CURTYPE_BTREE ); 6967 assert( pTabCur->uc.pCursor!=0 ); 6968 assert( pTabCur->isTable ); 6969 #if defined(SQLITE_ENABLE_CURSOR_HINTS) && defined(SQLITE_DEBUG) 6970 assert( 6971 sqlite3BtreeCursorHintTblCsr(pC->uc.pCursor)==pTabCur->uc.pCursor 6972 ); 6973 #endif 6974 pTabCur->nullRow = 0; 6975 pTabCur->movetoTarget = rowid; 6976 pTabCur->deferredMoveto = 1; 6977 pTabCur->cacheStatus = CACHE_STALE; 6978 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 ); 6979 assert( !pTabCur->isEphemeral ); 6980 pTabCur->ub.aAltMap = pOp->p4.ai; 6981 assert( !pC->isEphemeral ); 6982 pTabCur->pAltCursor = pC; 6983 }else{ 6984 pOut = out2Prerelease(p, pOp); 6985 pOut->u.i = rowid; 6986 } 6987 }else{ 6988 assert( pOp->opcode==OP_IdxRowid ); 6989 sqlite3VdbeMemSetNull(&aMem[pOp->p2]); 6990 } 6991 break; 6992 } 6993 6994 /* Opcode: FinishSeek P1 * * * * 6995 ** 6996 ** If cursor P1 was previously moved via OP_DeferredSeek, complete that 6997 ** seek operation now, without further delay. If the cursor seek has 6998 ** already occurred, this instruction is a no-op. 6999 */ 7000 case OP_FinishSeek: { /* ncycle */ 7001 VdbeCursor *pC; /* The P1 index cursor */ 7002 7003 assert( pOp->p1>=0 && pOp->p1nCursor ); 7004 pC = p->apCsr[pOp->p1]; 7005 if( pC->deferredMoveto ){ 7006 rc = sqlite3VdbeFinishMoveto(pC); 7007 if( rc ) goto abort_due_to_error; 7008 } 7009 break; 7010 } 7011 7012 /* Opcode: IdxGE P1 P2 P3 P4 * 7013 ** Synopsis: key=r[P3@P4] 7014 ** 7015 ** The P4 register values beginning with P3 form an unpacked index 7016 ** key that omits the PRIMARY KEY. Compare this key value against the index 7017 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID 7018 ** fields at the end. 7019 ** 7020 ** If the P1 index entry is greater than or equal to the key value 7021 ** then jump to P2. Otherwise fall through to the next instruction. 7022 */ 7023 /* Opcode: IdxGT P1 P2 P3 P4 * 7024 ** Synopsis: key=r[P3@P4] 7025 ** 7026 ** The P4 register values beginning with P3 form an unpacked index 7027 ** key that omits the PRIMARY KEY. Compare this key value against the index 7028 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID 7029 ** fields at the end. 7030 ** 7031 ** If the P1 index entry is greater than the key value 7032 ** then jump to P2. Otherwise fall through to the next instruction. 7033 */ 7034 /* Opcode: IdxLT P1 P2 P3 P4 * 7035 ** Synopsis: key=r[P3@P4] 7036 ** 7037 ** The P4 register values beginning with P3 form an unpacked index 7038 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against 7039 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or 7040 ** ROWID on the P1 index. 7041 ** 7042 ** If the P1 index entry is less than the key value then jump to P2. 7043 ** Otherwise fall through to the next instruction. 7044 */ 7045 /* Opcode: IdxLE P1 P2 P3 P4 * 7046 ** Synopsis: key=r[P3@P4] 7047 ** 7048 ** The P4 register values beginning with P3 form an unpacked index 7049 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against 7050 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or 7051 ** ROWID on the P1 index. 7052 ** 7053 ** If the P1 index entry is less than or equal to the key value then jump 7054 ** to P2. Otherwise fall through to the next instruction. 7055 */ 7056 case OP_IdxLE: /* jump, ncycle */ 7057 case OP_IdxGT: /* jump, ncycle */ 7058 case OP_IdxLT: /* jump, ncycle */ 7059 case OP_IdxGE: { /* jump, ncycle */ 7060 VdbeCursor *pC; 7061 int res; 7062 UnpackedRecord r; 7063 7064 assert( pOp->p1>=0 && pOp->p1nCursor ); 7065 pC = p->apCsr[pOp->p1]; 7066 assert( pC!=0 ); 7067 assert( pC->isOrdered ); 7068 assert( pC->eCurType==CURTYPE_BTREE ); 7069 assert( pC->uc.pCursor!=0); 7070 assert( pC->deferredMoveto==0 ); 7071 assert( pOp->p4type==P4_INT32 ); 7072 r.pKeyInfo = pC->pKeyInfo; 7073 r.nField = (u16)pOp->p4.i; 7074 if( pOp->opcodeopcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); 7076 r.default_rc = -1; 7077 }else{ 7078 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); 7079 r.default_rc = 0; 7080 } 7081 r.aMem = &aMem[pOp->p3]; 7082 #ifdef SQLITE_DEBUG 7083 { 7084 int i; 7085 for(i=0; ip3+i, &aMem[pOp->p3+i]); 7088 } 7089 } 7090 #endif 7091 7092 /* Inlined version of sqlite3VdbeIdxKeyCompare() */ 7093 { 7094 i64 nCellKey = 0; 7095 BtCursor *pCur; 7096 Mem m; 7097 7098 assert( pC->eCurType==CURTYPE_BTREE ); 7099 pCur = pC->uc.pCursor; 7100 assert( sqlite3BtreeCursorIsValid(pCur) ); 7101 nCellKey = sqlite3BtreePayloadSize(pCur); 7102 /* nCellKey will always be between 0 and 0xffffffff because of the way 7103 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ 7104 if( nCellKey<=0 || nCellKey>0x7fffffff ){ 7105 rc = SQLITE_CORRUPT_BKPT; 7106 goto abort_due_to_error; 7107 } 7108 sqlite3VdbeMemInit(&m, db, 0); 7109 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m); 7110 if( rc ) goto abort_due_to_error; 7111 res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, &r, 0); 7112 sqlite3VdbeMemReleaseMalloc(&m); 7113 } 7114 /* End of inlined sqlite3VdbeIdxKeyCompare() */ 7115 7116 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); 7117 if( (pOp->opcode&1)==(OP_IdxLT&1) ){ 7118 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); 7119 res = -res; 7120 }else{ 7121 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); 7122 res++; 7123 } 7124 VdbeBranchTaken(res>0,2); 7125 assert( rc==SQLITE_OK ); 7126 if( res>0 ) goto jump_to_p2; 7127 break; 7128 } 7129 7130 /* Opcode: Destroy P1 P2 P3 * * 7131 ** 7132 ** Delete an entire database table or index whose root page in the database 7133 ** file is given by P1. 7134 ** 7135 ** The table being destroyed is in the main database file if P3==0. If 7136 ** P3==1 then the table to be destroyed is in the auxiliary database file 7137 ** that is used to store tables create using CREATE TEMPORARY TABLE. 7138 ** 7139 ** If AUTOVACUUM is enabled then it is possible that another root page 7140 ** might be moved into the newly deleted root page in order to keep all 7141 ** root pages contiguous at the beginning of the database. The former 7142 ** value of the root page that moved - its value before the move occurred - 7143 ** is stored in register P2. If no page movement was required (because the 7144 ** table being dropped was already the last one in the database) then a 7145 ** zero is stored in register P2. If AUTOVACUUM is disabled then a zero 7146 ** is stored in register P2. 7147 ** 7148 ** This opcode throws an error if there are any active reader VMs when 7149 ** it is invoked. This is done to avoid the difficulty associated with 7150 ** updating existing cursors when a root page is moved in an AUTOVACUUM 7151 ** database. This error is thrown even if the database is not an AUTOVACUUM 7152 ** db in order to avoid introducing an incompatibility between autovacuum 7153 ** and non-autovacuum modes. 7154 ** 7155 ** See also: Clear 7156 */ 7157 case OP_Destroy: { /* out2 */ 7158 int iMoved; 7159 int iDb; 7160 7161 sqlite3VdbeIncrWriteCounter(p, 0); 7162 assert( p->readOnly==0 ); 7163 assert( pOp->p1>1 ); 7164 pOut = out2Prerelease(p, pOp); 7165 pOut->flags = MEM_Null; 7166 if( db->nVdbeRead > db->nVDestroy+1 ){ 7167 rc = SQLITE_LOCKED; 7168 p->errorAction = OE_Abort; 7169 goto abort_due_to_error; 7170 }else{ 7171 iDb = pOp->p3; 7172 assert( DbMaskTest(p->btreeMask, iDb) ); 7173 iMoved = 0; /* Not needed. Only to silence a warning. */ 7174 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); 7175 pOut->flags = MEM_Int; 7176 pOut->u.i = iMoved; 7177 if( rc ) goto abort_due_to_error; 7178 #ifndef SQLITE_OMIT_AUTOVACUUM 7179 if( iMoved!=0 ){ 7180 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); 7181 /* All OP_Destroy operations occur on the same btree */ 7182 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); 7183 resetSchemaOnFault = iDb+1; 7184 } 7185 #endif 7186 } 7187 break; 7188 } 7189 7190 /* 7191 ** Opcode: DbLock 7192 ** 7193 ** Raise an SQLITE_LOCKED error if there are any other reader statements 7194 ** currently active. 7195 */ 7196 case OP_DbLock: { 7197 if( db->nVdbeRead>1 ){ 7198 rc = SQLITE_LOCKED; 7199 p->errorAction = OE_Abort; 7200 goto abort_due_to_error; 7201 } 7202 break; 7203 }; 7204 7205 /* Opcode: Clear P1 P2 P3 7206 ** 7207 ** Delete all contents of the database table or index whose root page 7208 ** in the database file is given by P1. But, unlike Destroy, do not 7209 ** remove the table or index from the database file. 7210 ** 7211 ** The table being cleared is in the main database file if P2==0. If 7212 ** P2==1 then the table to be cleared is in the auxiliary database file 7213 ** that is used to store tables create using CREATE TEMPORARY TABLE. 7214 ** 7215 ** If the P3 value is non-zero, then the row change count is incremented 7216 ** by the number of rows in the table being cleared. If P3 is greater 7217 ** than zero, then the value stored in register P3 is also incremented 7218 ** by the number of rows in the table being cleared. 7219 ** 7220 ** See also: Destroy 7221 */ 7222 case OP_Clear: { 7223 i64 nChange; 7224 7225 sqlite3VdbeIncrWriteCounter(p, 0); 7226 nChange = 0; 7227 assert( p->readOnly==0 ); 7228 assert( DbMaskTest(p->btreeMask, pOp->p2) ); 7229 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange); 7230 if( pOp->p3 ){ 7231 p->nChange += nChange; 7232 if( pOp->p3>0 ){ 7233 assert( memIsValid(&aMem[pOp->p3]) ); 7234 memAboutToChange(p, &aMem[pOp->p3]); 7235 aMem[pOp->p3].u.i += nChange; 7236 } 7237 } 7238 if( rc ) goto abort_due_to_error; 7239 break; 7240 } 7241 7242 /* Opcode: ResetSorter P1 * * * * 7243 ** 7244 ** Delete all contents from the ephemeral table or sorter 7245 ** that is open on cursor P1. 7246 ** 7247 ** This opcode only works for cursors used for sorting and 7248 ** opened with OP_OpenEphemeral or OP_SorterOpen. 7249 */ 7250 case OP_ResetSorter: { 7251 VdbeCursor *pC; 7252 7253 assert( pOp->p1>=0 && pOp->p1nCursor ); 7254 pC = p->apCsr[pOp->p1]; 7255 assert( pC!=0 ); 7256 if( isSorter(pC) ){ 7257 sqlite3VdbeSorterReset(db, pC->uc.pSorter); 7258 }else{ 7259 assert( pC->eCurType==CURTYPE_BTREE ); 7260 assert( pC->isEphemeral ); 7261 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor); 7262 if( rc ) goto abort_due_to_error; 7263 } 7264 break; 7265 } 7266 7267 /* Opcode: CreateBtree P1 P2 P3 * * 7268 ** Synopsis: r[P2]=root iDb=P1 flags=P3 7269 ** 7270 ** Allocate a new b-tree in the main database file if P1==0 or in the 7271 ** TEMP database file if P1==1 or in an attached database if 7272 ** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table 7273 ** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table. 7274 ** The root page number of the new b-tree is stored in register P2. 7275 */ 7276 case OP_CreateBtree: { /* out2 */ 7277 Pgno pgno; 7278 Db *pDb; 7279 7280 sqlite3VdbeIncrWriteCounter(p, 0); 7281 pOut = out2Prerelease(p, pOp); 7282 pgno = 0; 7283 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY ); 7284 assert( pOp->p1>=0 && pOp->p1nDb ); 7285 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 7286 assert( p->readOnly==0 ); 7287 pDb = &db->aDb[pOp->p1]; 7288 assert( pDb->pBt!=0 ); 7289 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3); 7290 if( rc ) goto abort_due_to_error; 7291 pOut->u.i = pgno; 7292 break; 7293 } 7294 7295 /* Opcode: SqlExec P1 P2 * P4 * 7296 ** 7297 ** Run the SQL statement or statements specified in the P4 string. 7298 ** 7299 ** The P1 parameter is a bitmask of options: 7300 ** 7301 ** 0x0001 Disable Auth and Trace callbacks while the statements 7302 ** in P4 are running. 7303 ** 7304 ** 0x0002 Set db->nAnalysisLimit to P2 while the statements in 7305 ** P4 are running. 7306 ** 7307 */ 7308 case OP_SqlExec: { 7309 char *zErr; 7310 #ifndef SQLITE_OMIT_AUTHORIZATION 7311 sqlite3_xauth xAuth; 7312 #endif 7313 u8 mTrace; 7314 int savedAnalysisLimit; 7315 7316 sqlite3VdbeIncrWriteCounter(p, 0); 7317 db->nSqlExec++; 7318 zErr = 0; 7319 #ifndef SQLITE_OMIT_AUTHORIZATION 7320 xAuth = db->xAuth; 7321 #endif 7322 mTrace = db->mTrace; 7323 savedAnalysisLimit = db->nAnalysisLimit; 7324 if( pOp->p1 & 0x0001 ){ 7325 #ifndef SQLITE_OMIT_AUTHORIZATION 7326 db->xAuth = 0; 7327 #endif 7328 db->mTrace = 0; 7329 } 7330 if( pOp->p1 & 0x0002 ){ 7331 db->nAnalysisLimit = pOp->p2; 7332 } 7333 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, &zErr); 7334 db->nSqlExec--; 7335 #ifndef SQLITE_OMIT_AUTHORIZATION 7336 db->xAuth = xAuth; 7337 #endif 7338 db->mTrace = mTrace; 7339 db->nAnalysisLimit = savedAnalysisLimit; 7340 if( zErr || rc ){ 7341 sqlite3VdbeError(p, "%s", zErr); 7342 sqlite3_free(zErr); 7343 if( rc==SQLITE_NOMEM ) goto no_mem; 7344 goto abort_due_to_error; 7345 } 7346 break; 7347 } 7348 7349 /* Opcode: ParseSchema P1 * * P4 P5 7350 ** 7351 ** Read and parse all entries from the schema table of database P1 7352 ** that match the WHERE clause P4. If P4 is a NULL pointer, then the 7353 ** entire schema for P1 is reparsed. 7354 ** 7355 ** When P4 is NULL, the P5 value is used as the mFlags argument 7356 ** to sqlite3InitOne(). In other words, P5 should be a mask composed 7357 ** of INITFLAG_* values. 7358 ** 7359 ** The P4==0 case is only used by ALTER TABLE and P5!=0 for all such 7360 ** cases. For uses other than ALTER TABLE, P4<>0 and P5==0. 7361 ** 7362 ** This opcode invokes the parser to create a new virtual machine, 7363 ** then runs the new virtual machine. It is thus a re-entrant opcode. 7364 */ 7365 case OP_ParseSchema: { 7366 int iDb; 7367 const char *zSchema; 7368 char *zSql; 7369 InitData initData; 7370 7371 /* Any prepared statement that invokes this opcode will hold mutexes 7372 ** on every btree. This is a prerequisite for invoking 7373 ** sqlite3InitCallback(). 7374 */ 7375 #ifdef SQLITE_DEBUG 7376 for(iDb=0; iDbnDb; iDb++){ 7377 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); 7378 } 7379 #endif 7380 7381 iDb = pOp->p1; 7382 assert( iDb>=0 && iDbnDb ); 7383 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) 7384 || db->mallocFailed 7385 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) ); 7386 7387 /* ParseSchema is never invoked on a read-only database */ 7388 assert( db->aDb[iDb].pBt==0 || sqlite3BtreeIsReadonly(db->aDb[iDb].pBt)==0 ); 7389 7390 #ifndef SQLITE_OMIT_ALTERTABLE 7391 if( pOp->p4.z==0 ){ 7392 assert( pOp->p5!=0 ); 7393 sqlite3ExpireStmtsUsingSchema(db, iDb, p); 7394 sqlite3SchemaClear(db->aDb[iDb].pSchema); 7395 db->mDbFlags &= ~DBFLAG_SchemaKnownOk; 7396 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5); 7397 db->mDbFlags |= DBFLAG_SchemaChange; 7398 p->expired = 0; 7399 }else 7400 #endif 7401 { 7402 assert( pOp->p5==0 ); 7403 zSchema = LEGACY_SCHEMA_TABLE; 7404 initData.db = db; 7405 initData.iDb = iDb; 7406 initData.pzErrMsg = &p->zErrMsg; 7407 initData.mInitFlags = 0; 7408 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt); 7409 zSql = sqlite3MPrintf(db, 7410 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid", 7411 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z); 7412 if( zSql==0 ){ 7413 rc = SQLITE_NOMEM_BKPT; 7414 }else{ 7415 assert( db->init.busy==0 ); 7416 db->init.busy = 1; 7417 initData.rc = SQLITE_OK; 7418 initData.nInitRow = 0; 7419 assert( !db->mallocFailed ); 7420 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); 7421 if( rc==SQLITE_OK ) rc = initData.rc; 7422 if( rc==SQLITE_OK && initData.nInitRow==0 ){ 7423 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse 7424 ** at least one SQL statement. Any less than that indicates that 7425 ** the sqlite_schema table is corrupt. */ 7426 rc = SQLITE_CORRUPT_BKPT; 7427 } 7428 sqlite3DbFreeNN(db, zSql); 7429 db->init.busy = 0; 7430 } 7431 } 7432 if( rc ){ 7433 sqlite3ResetAllSchemasOfConnection(db); 7434 sqlite3ExpirePreparedStatements(db, 0); 7435 if( rc==SQLITE_NOMEM ){ 7436 goto no_mem; 7437 } 7438 goto abort_due_to_error; 7439 } 7440 break; 7441 } 7442 7443 #if !defined(SQLITE_OMIT_ANALYZE) 7444 /* Opcode: LoadAnalysis P1 * * * * 7445 ** 7446 ** Read the sqlite_stat1 table for database P1 and load the content 7447 ** of that table into the internal index hash table. This will cause 7448 ** the analysis to be used when preparing all subsequent queries. 7449 */ 7450 case OP_LoadAnalysis: { 7451 assert( pOp->p1>=0 && pOp->p1nDb ); 7452 rc = sqlite3AnalysisLoad(db, pOp->p1); 7453 if( rc ) goto abort_due_to_error; 7454 break; 7455 } 7456 #endif /* !defined(SQLITE_OMIT_ANALYZE) */ 7457 7458 /* Opcode: DropTable P1 * * P4 * 7459 ** 7460 ** Remove the internal (in-memory) data structures that describe 7461 ** the table named P4 in database P1. This is called after a table 7462 ** is dropped from disk (using the Destroy opcode) in order to keep 7463 ** the internal representation of the 7464 ** schema consistent with what is on disk. 7465 */ 7466 case OP_DropTable: { 7467 sqlite3VdbeIncrWriteCounter(p, 0); 7468 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); 7469 break; 7470 } 7471 7472 /* Opcode: DropIndex P1 * * P4 * 7473 ** 7474 ** Remove the internal (in-memory) data structures that describe 7475 ** the index named P4 in database P1. This is called after an index 7476 ** is dropped from disk (using the Destroy opcode) 7477 ** in order to keep the internal representation of the 7478 ** schema consistent with what is on disk. 7479 */ 7480 case OP_DropIndex: { 7481 sqlite3VdbeIncrWriteCounter(p, 0); 7482 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); 7483 break; 7484 } 7485 7486 /* Opcode: DropTrigger P1 * * P4 * 7487 ** 7488 ** Remove the internal (in-memory) data structures that describe 7489 ** the trigger named P4 in database P1. This is called after a trigger 7490 ** is dropped from disk (using the Destroy opcode) in order to keep 7491 ** the internal representation of the 7492 ** schema consistent with what is on disk. 7493 */ 7494 case OP_DropTrigger: { 7495 sqlite3VdbeIncrWriteCounter(p, 0); 7496 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); 7497 break; 7498 } 7499 7500 7501 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 7502 /* Opcode: IntegrityCk P1 P2 P3 P4 P5 7503 ** 7504 ** Do an analysis of the currently open database. Store in 7505 ** register (P1+1) the text of an error message describing any problems. 7506 ** If no problems are found, store a NULL in register (P1+1). 7507 ** 7508 ** The register (P1) contains one less than the maximum number of allowed 7509 ** errors. At most reg(P1) errors will be reported. 7510 ** In other words, the analysis stops as soon as reg(P1) errors are 7511 ** seen. Reg(P1) is updated with the number of errors remaining. 7512 ** 7513 ** The root page numbers of all tables in the database are integers 7514 ** stored in P4_INTARRAY argument. 7515 ** 7516 ** If P5 is not zero, the check is done on the auxiliary database 7517 ** file, not the main database file. 7518 ** 7519 ** This opcode is used to implement the integrity_check pragma. 7520 */ 7521 case OP_IntegrityCk: { 7522 int nRoot; /* Number of tables to check. (Number of root pages.) */ 7523 Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */ 7524 int nErr; /* Number of errors reported */ 7525 char *z; /* Text of the error report */ 7526 Mem *pnErr; /* Register keeping track of errors remaining */ 7527 7528 assert( p->bIsReader ); 7529 assert( pOp->p4type==P4_INTARRAY ); 7530 nRoot = pOp->p2; 7531 aRoot = pOp->p4.ai; 7532 assert( nRoot>0 ); 7533 assert( aRoot!=0 ); 7534 assert( aRoot[0]==(Pgno)nRoot ); 7535 assert( pOp->p1>0 && (pOp->p1+1)<=(p->nMem+1 - p->nCursor) ); 7536 pnErr = &aMem[pOp->p1]; 7537 assert( (pnErr->flags & MEM_Int)!=0 ); 7538 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); 7539 pIn1 = &aMem[pOp->p1+1]; 7540 assert( pOp->p5nDb ); 7541 assert( DbMaskTest(p->btreeMask, pOp->p5) ); 7542 rc = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], 7543 &aMem[pOp->p3], nRoot, (int)pnErr->u.i+1, &nErr, &z); 7544 sqlite3VdbeMemSetNull(pIn1); 7545 if( nErr==0 ){ 7546 assert( z==0 ); 7547 }else if( rc ){ 7548 sqlite3_free(z); 7549 goto abort_due_to_error; 7550 }else{ 7551 pnErr->u.i -= nErr-1; 7552 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); 7553 } 7554 UPDATE_MAX_BLOBSIZE(pIn1); 7555 sqlite3VdbeChangeEncoding(pIn1, encoding); 7556 goto check_for_interrupt; 7557 } 7558 7559 /* Opcode: IFindKey P1 P2 P3 P4 * 7560 ** 7561 ** This instruction always follows an OP_Found with the same P1, P2 and P3 7562 ** values as this instruction and a non-zero P4 value. The P4 value to 7563 ** this opcode is of type P4_INDEX and contains a pointer to the Index 7564 ** object of for the index being searched. 7565 ** 7566 ** This opcode uses sqlite3VdbeFindIndexKey() to search around the current 7567 ** cursor location for an index key that exactly matches all fields that 7568 ** are not indexed expressions or references to VIRTUAL generated columns, 7569 ** and either exactly match or are real numbers that are within 2 ULPs of 7570 ** each other if the don't match. 7571 ** 7572 ** To put it another way, this opcode looks for nearby index entries that 7573 ** are very close to the search key, but which might have small differences 7574 ** in floating-point values that come via an expression. 7575 ** 7576 ** If no nearby alternative entry is found in cursor P1, then jump to P2. 7577 ** But if a close match is found, fall through. 7578 ** 7579 ** This opcode is used by PRAGMA integrity_check to help distinguish 7580 ** between truely corrupt indexes and expression indexes that are holding 7581 ** floating-point values that are off by one or two ULPs. 7582 */ 7583 case OP_IFindKey: { /* jump, in3 */ 7584 VdbeCursor *pC; 7585 int res; 7586 UnpackedRecord r; 7587 7588 assert( pOp[-1].opcode==OP_Found ); 7589 assert( pOp[-1].p1==pOp->p1 ); 7590 assert( pOp[-1].p3==pOp->p3 ); 7591 pC = p->apCsr[pOp->p1]; 7592 assert( pOp->p4type==P4_INDEX ); 7593 if( p->expired==1 ) goto abort_due_to_expired; 7594 assert( pC->eCurType==CURTYPE_BTREE ); 7595 assert( pC->uc.pCursor!=0 ); 7596 assert( pC->isTable==0 ); 7597 7598 memset(&r, 0, sizeof(r)); 7599 r.aMem = &aMem[pOp->p3]; 7600 r.nField = pOp->p4.pIdx->nColumn; 7601 r.pKeyInfo = pC->pKeyInfo; 7602 7603 rc = sqlite3VdbeFindIndexKey(pC->uc.pCursor, pOp->p4.pIdx, &r, &res, 1); 7604 if( rc || res!=0 ){ 7605 rc = SQLITE_OK; 7606 goto jump_to_p2; 7607 } 7608 pC->nullRow = 0; 7609 break; 7610 }; 7611 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 7612 7613 /* Opcode: RowSetAdd P1 P2 * * * 7614 ** Synopsis: rowset(P1)=r[P2] 7615 ** 7616 ** Insert the integer value held by register P2 into a RowSet object 7617 ** held in register P1. 7618 ** 7619 ** An assertion fails if P2 is not an integer. 7620 */ 7621 case OP_RowSetAdd: { /* in1, in2 */ 7622 pIn1 = &aMem[pOp->p1]; 7623 pIn2 = &aMem[pOp->p2]; 7624 assert( (pIn2->flags & MEM_Int)!=0 ); 7625 if( (pIn1->flags & MEM_Blob)==0 ){ 7626 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem; 7627 } 7628 assert( sqlite3VdbeMemIsRowSet(pIn1) ); 7629 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i); 7630 break; 7631 } 7632 7633 /* Opcode: RowSetRead P1 P2 P3 * * 7634 ** Synopsis: r[P3]=rowset(P1) 7635 ** 7636 ** Extract the smallest value from the RowSet object in P1 7637 ** and put that value into register P3. 7638 ** Or, if RowSet object P1 is initially empty, leave P3 7639 ** unchanged and jump to instruction P2. 7640 */ 7641 case OP_RowSetRead: { /* jump, in1, out3 */ 7642 i64 val; 7643 7644 pIn1 = &aMem[pOp->p1]; 7645 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) ); 7646 if( (pIn1->flags & MEM_Blob)==0 7647 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0 7648 ){ 7649 /* The boolean index is empty */ 7650 sqlite3VdbeMemSetNull(pIn1); 7651 VdbeBranchTaken(1,2); 7652 goto jump_to_p2_and_check_for_interrupt; 7653 }else{ 7654 /* A value was pulled from the index */ 7655 VdbeBranchTaken(0,2); 7656 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); 7657 } 7658 goto check_for_interrupt; 7659 } 7660 7661 /* Opcode: RowSetTest P1 P2 P3 P4 7662 ** Synopsis: if r[P3] in rowset(P1) goto P2 7663 ** 7664 ** Register P3 is assumed to hold a 64-bit integer value. If register P1 7665 ** contains a RowSet object and that RowSet object contains 7666 ** the value held in P3, jump to register P2. Otherwise, insert the 7667 ** integer in P3 into the RowSet and continue on to the 7668 ** next opcode. 7669 ** 7670 ** The RowSet object is optimized for the case where sets of integers 7671 ** are inserted in distinct phases, which each set contains no duplicates. 7672 ** Each set is identified by a unique P4 value. The first set 7673 ** must have P4==0, the final set must have P4==-1, and for all other sets 7674 ** must have P4>0. 7675 ** 7676 ** This allows optimizations: (a) when P4==0 there is no need to test 7677 ** the RowSet object for P3, as it is guaranteed not to contain it, 7678 ** (b) when P4==-1 there is no need to insert the value, as it will 7679 ** never be tested for, and (c) when a value that is part of set X is 7680 ** inserted, there is no need to search to see if the same value was 7681 ** previously inserted as part of set X (only if it was previously 7682 ** inserted as part of some other set). 7683 */ 7684 case OP_RowSetTest: { /* jump, in1, in3 */ 7685 int iSet; 7686 int exists; 7687 7688 pIn1 = &aMem[pOp->p1]; 7689 pIn3 = &aMem[pOp->p3]; 7690 iSet = pOp->p4.i; 7691 assert( pIn3->flags&MEM_Int ); 7692 7693 /* If there is anything other than a rowset object in memory cell P1, 7694 ** delete it now and initialize P1 with an empty rowset 7695 */ 7696 if( (pIn1->flags & MEM_Blob)==0 ){ 7697 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem; 7698 } 7699 assert( sqlite3VdbeMemIsRowSet(pIn1) ); 7700 assert( pOp->p4type==P4_INT32 ); 7701 assert( iSet==-1 || iSet>=0 ); 7702 if( iSet ){ 7703 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i); 7704 VdbeBranchTaken(exists!=0,2); 7705 if( exists ) goto jump_to_p2; 7706 } 7707 if( iSet>=0 ){ 7708 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i); 7709 } 7710 break; 7711 } 7712 7713 7714 #ifndef SQLITE_OMIT_TRIGGER 7715 7716 /* Opcode: Program P1 P2 P3 P4 P5 7717 ** 7718 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). 7719 ** 7720 ** P1 contains the address of the memory cell that contains the first memory 7721 ** cell in an array of values used as arguments to the sub-program. P2 7722 ** contains the address to jump to if the sub-program throws an IGNORE 7723 ** exception using the RAISE() function. P2 might be zero, if there is 7724 ** no possibility that an IGNORE exception will be raised. 7725 ** Register P3 contains the address 7726 ** of a memory cell in this (the parent) VM that is used to allocate the 7727 ** memory required by the sub-vdbe at runtime. 7728 ** 7729 ** P4 is a pointer to the VM containing the trigger program. 7730 ** 7731 ** If P5 is non-zero, then recursive program invocation is enabled. 7732 */ 7733 case OP_Program: { /* jump0 */ 7734 int nMem; /* Number of memory registers for sub-program */ 7735 i64 nByte; /* Bytes of runtime space required for sub-program */ 7736 Mem *pRt; /* Register to allocate runtime space */ 7737 Mem *pMem; /* Used to iterate through memory cells */ 7738 Mem *pEnd; /* Last memory cell in new array */ 7739 VdbeFrame *pFrame; /* New vdbe frame to execute in */ 7740 SubProgram *pProgram; /* Sub-program to execute */ 7741 void *t; /* Token identifying trigger */ 7742 7743 pProgram = pOp->p4.pProgram; 7744 pRt = &aMem[pOp->p3]; 7745 assert( pProgram->nOp>0 ); 7746 7747 /* If the p5 flag is clear, then recursive invocation of triggers is 7748 ** disabled for backwards compatibility (p5 is set if this sub-program 7749 ** is really a trigger, not a foreign key action, and the flag set 7750 ** and cleared by the "PRAGMA recursive_triggers" command is clear). 7751 ** 7752 ** It is recursive invocation of triggers, at the SQL level, that is 7753 ** disabled. In some cases a single trigger may generate more than one 7754 ** SubProgram (if the trigger may be executed with more than one different 7755 ** ON CONFLICT algorithm). SubProgram structures associated with a 7756 ** single trigger all have the same value for the SubProgram.token 7757 ** variable. */ 7758 if( pOp->p5 ){ 7759 t = pProgram->token; 7760 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); 7761 if( pFrame ) break; 7762 } 7763 7764 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ 7765 rc = SQLITE_ERROR; 7766 sqlite3VdbeError(p, "triggers nested too deep"); 7767 goto abort_due_to_error; 7768 } 7769 7770 /* Register pRt is used to store the memory required to save the state 7771 ** of the current program, and the memory required at runtime to execute 7772 ** the trigger program. If this trigger has been fired before, then pRt 7773 ** is already allocated. Otherwise, it must be initialized. */ 7774 if( (pRt->flags&MEM_Blob)==0 ){ 7775 /* SubProgram.nMem is set to the number of memory cells used by the 7776 ** program stored in SubProgram.aOp. As well as these, one memory 7777 ** cell is required for each cursor used by the program. Set local 7778 ** variable nMem (and later, VdbeFrame.nChildMem) to this value. 7779 */ 7780 nMem = pProgram->nMem + pProgram->nCsr; 7781 assert( nMem>0 ); 7782 if( pProgram->nCsr==0 ) nMem++; 7783 nByte = ROUND8(sizeof(VdbeFrame)) 7784 + nMem * sizeof(Mem) 7785 + pProgram->nCsr * sizeof(VdbeCursor*) 7786 + (7 + (i64)pProgram->nOp)/8; 7787 pFrame = sqlite3DbMallocZero(db, nByte); 7788 if( !pFrame ){ 7789 goto no_mem; 7790 } 7791 sqlite3VdbeMemRelease(pRt); 7792 pRt->flags = MEM_Blob|MEM_Dyn; 7793 pRt->z = (char*)pFrame; 7794 pRt->n = (int)nByte; 7795 pRt->xDel = sqlite3VdbeFrameMemDel; 7796 7797 pFrame->v = p; 7798 pFrame->nChildMem = nMem; 7799 pFrame->nChildCsr = pProgram->nCsr; 7800 pFrame->pc = (int)(pOp - aOp); 7801 pFrame->aMem = p->aMem; 7802 pFrame->nMem = p->nMem; 7803 pFrame->apCsr = p->apCsr; 7804 pFrame->nCursor = p->nCursor; 7805 pFrame->aOp = p->aOp; 7806 pFrame->nOp = p->nOp; 7807 pFrame->token = pProgram->token; 7808 #ifdef SQLITE_DEBUG 7809 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC; 7810 #endif 7811 7812 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; 7813 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ 7814 pMem->flags = MEM_Undefined; 7815 pMem->db = db; 7816 } 7817 }else{ 7818 pFrame = (VdbeFrame*)pRt->z; 7819 assert( pRt->xDel==sqlite3VdbeFrameMemDel ); 7820 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem 7821 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) ); 7822 assert( pProgram->nCsr==pFrame->nChildCsr ); 7823 assert( (int)(pOp - aOp)==pFrame->pc ); 7824 } 7825 7826 p->nFrame++; 7827 pFrame->pParent = p->pFrame; 7828 pFrame->lastRowid = db->lastRowid; 7829 pFrame->nChange = p->nChange; 7830 pFrame->nDbChange = p->db->nChange; 7831 assert( pFrame->pAuxData==0 ); 7832 pFrame->pAuxData = p->pAuxData; 7833 p->pAuxData = 0; 7834 p->nChange = 0; 7835 p->pFrame = pFrame; 7836 p->aMem = aMem = VdbeFrameMem(pFrame); 7837 p->nMem = pFrame->nChildMem; 7838 p->nCursor = (u16)pFrame->nChildCsr; 7839 p->apCsr = (VdbeCursor **)&aMem[p->nMem]; 7840 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr]; 7841 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8); 7842 p->aOp = aOp = pProgram->aOp; 7843 p->nOp = pProgram->nOp; 7844 #ifdef SQLITE_DEBUG 7845 /* Verify that second and subsequent executions of the same trigger do not 7846 ** try to reuse register values from the first use. */ 7847 { 7848 int i; 7849 for(i=0; inMem; i++){ 7850 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */ 7851 MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */ 7852 } 7853 } 7854 #endif 7855 pOp = &aOp[-1]; 7856 goto check_for_interrupt; 7857 } 7858 7859 /* Opcode: Param P1 P2 * * * 7860 ** 7861 ** This opcode is only ever present in sub-programs called via the 7862 ** OP_Program instruction. Copy a value currently stored in a memory 7863 ** cell of the calling (parent) frame to cell P2 in the current frames 7864 ** address space. This is used by trigger programs to access the new.* 7865 ** and old.* values. 7866 ** 7867 ** The address of the cell in the parent frame is determined by adding 7868 ** the value of the P1 argument to the value of the P1 argument to the 7869 ** calling OP_Program instruction. 7870 */ 7871 case OP_Param: { /* out2 */ 7872 VdbeFrame *pFrame; 7873 Mem *pIn; 7874 pOut = out2Prerelease(p, pOp); 7875 pFrame = p->pFrame; 7876 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; 7877 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); 7878 break; 7879 } 7880 7881 #endif /* #ifndef SQLITE_OMIT_TRIGGER */ 7882 7883 #ifndef SQLITE_OMIT_FOREIGN_KEY 7884 /* Opcode: FkCounter P1 P2 * * * 7885 ** Synopsis: fkctr[P1]+=P2 7886 ** 7887 ** Increment a "constraint counter" by P2 (P2 may be negative or positive). 7888 ** If P1 is non-zero, the database constraint counter is incremented 7889 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the 7890 ** statement counter is incremented (immediate foreign key constraints). 7891 */ 7892 case OP_FkCounter: { 7893 if( pOp->p1 ){ 7894 db->nDeferredCons += pOp->p2; 7895 }else{ 7896 if( db->flags & SQLITE_DeferFKs ){ 7897 db->nDeferredImmCons += pOp->p2; 7898 }else{ 7899 p->nFkConstraint += pOp->p2; 7900 } 7901 } 7902 break; 7903 } 7904 7905 /* Opcode: FkIfZero P1 P2 * * * 7906 ** Synopsis: if fkctr[P1]==0 goto P2 7907 ** 7908 ** This opcode tests if a foreign key constraint-counter is currently zero. 7909 ** If so, jump to instruction P2. Otherwise, fall through to the next 7910 ** instruction. 7911 ** 7912 ** If P1 is non-zero, then the jump is taken if the database constraint-counter 7913 ** is zero (the one that counts deferred constraint violations). If P1 is 7914 ** zero, the jump is taken if the statement constraint-counter is zero 7915 ** (immediate foreign key constraint violations). 7916 */ 7917 case OP_FkIfZero: { /* jump */ 7918 if( pOp->p1 ){ 7919 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2); 7920 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; 7921 }else{ 7922 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2); 7923 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; 7924 } 7925 break; 7926 } 7927 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ 7928 7929 #ifndef SQLITE_OMIT_AUTOINCREMENT 7930 /* Opcode: MemMax P1 P2 * * * 7931 ** Synopsis: r[P1]=max(r[P1],r[P2]) 7932 ** 7933 ** P1 is a register in the root frame of this VM (the root frame is 7934 ** different from the current frame if this instruction is being executed 7935 ** within a sub-program). Set the value of register P1 to the maximum of 7936 ** its current value and the value in register P2. 7937 ** 7938 ** This instruction throws an error if the memory cell is not initially 7939 ** an integer. 7940 */ 7941 case OP_MemMax: { /* in2 */ 7942 VdbeFrame *pFrame; 7943 if( p->pFrame ){ 7944 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); 7945 pIn1 = &pFrame->aMem[pOp->p1]; 7946 }else{ 7947 pIn1 = &aMem[pOp->p1]; 7948 } 7949 assert( memIsValid(pIn1) ); 7950 sqlite3VdbeMemIntegerify(pIn1); 7951 pIn2 = &aMem[pOp->p2]; 7952 sqlite3VdbeMemIntegerify(pIn2); 7953 if( pIn1->u.iu.i){ 7954 pIn1->u.i = pIn2->u.i; 7955 } 7956 break; 7957 } 7958 #endif /* SQLITE_OMIT_AUTOINCREMENT */ 7959 7960 /* Opcode: IfPos P1 P2 P3 * * 7961 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 7962 ** 7963 ** Register P1 must contain an integer. 7964 ** If the value of register P1 is 1 or greater, subtract P3 from the 7965 ** value in P1 and jump to P2. 7966 ** 7967 ** If the initial value of register P1 is less than 1, then the 7968 ** value is unchanged and control passes through to the next instruction. 7969 */ 7970 case OP_IfPos: { /* jump, in1 */ 7971 pIn1 = &aMem[pOp->p1]; 7972 assert( pIn1->flags&MEM_Int ); 7973 VdbeBranchTaken( pIn1->u.i>0, 2); 7974 if( pIn1->u.i>0 ){ 7975 pIn1->u.i -= pOp->p3; 7976 goto jump_to_p2; 7977 } 7978 break; 7979 } 7980 7981 /* Opcode: OffsetLimit P1 P2 P3 * * 7982 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) 7983 ** 7984 ** This opcode performs a commonly used computation associated with 7985 ** LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3] 7986 ** holds the offset counter. The opcode computes the combined value 7987 ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] 7988 ** value computed is the total number of rows that will need to be 7989 ** visited in order to complete the query. 7990 ** 7991 ** If r[P3] is zero or negative, that means there is no OFFSET 7992 ** and r[P2] is set to be the value of the LIMIT, r[P1]. 7993 ** 7994 ** if r[P1] is zero or negative, that means there is no LIMIT 7995 ** and r[P2] is set to -1. 7996 ** 7997 ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3]. 7998 */ 7999 case OP_OffsetLimit: { /* in1, out2, in3 */ 8000 i64 x; 8001 pIn1 = &aMem[pOp->p1]; 8002 pIn3 = &aMem[pOp->p3]; 8003 pOut = out2Prerelease(p, pOp); 8004 assert( pIn1->flags & MEM_Int ); 8005 assert( pIn3->flags & MEM_Int ); 8006 x = pIn1->u.i; 8007 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){ 8008 /* If the LIMIT is less than or equal to zero, loop forever. This 8009 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then 8010 ** also loop forever. This is undocumented. In fact, one could argue 8011 ** that the loop should terminate. But assuming 1 billion iterations 8012 ** per second (far exceeding the capabilities of any current hardware) 8013 ** it would take nearly 300 years to actually reach the limit. So 8014 ** looping forever is a reasonable approximation. */ 8015 pOut->u.i = -1; 8016 }else{ 8017 pOut->u.i = x; 8018 } 8019 break; 8020 } 8021 8022 /* Opcode: IfNotZero P1 P2 * * * 8023 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2 8024 ** 8025 ** Register P1 must contain an integer. If the content of register P1 is 8026 ** initially greater than zero, then decrement the value in register P1. 8027 ** If it is non-zero (negative or positive) and then also jump to P2. 8028 ** If register P1 is initially zero, leave it unchanged and fall through. 8029 */ 8030 case OP_IfNotZero: { /* jump, in1 */ 8031 pIn1 = &aMem[pOp->p1]; 8032 assert( pIn1->flags&MEM_Int ); 8033 VdbeBranchTaken(pIn1->u.i<0, 2); 8034 if( pIn1->u.i ){ 8035 if( pIn1->u.i>0 ) pIn1->u.i--; 8036 goto jump_to_p2; 8037 } 8038 break; 8039 } 8040 8041 /* Opcode: DecrJumpZero P1 P2 * * * 8042 ** Synopsis: if (--r[P1])==0 goto P2 8043 ** 8044 ** Register P1 must hold an integer. Decrement the value in P1 8045 ** and jump to P2 if the new value is exactly zero. 8046 */ 8047 case OP_DecrJumpZero: { /* jump, in1 */ 8048 pIn1 = &aMem[pOp->p1]; 8049 assert( pIn1->flags&MEM_Int ); 8050 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--; 8051 VdbeBranchTaken(pIn1->u.i==0, 2); 8052 if( pIn1->u.i==0 ) goto jump_to_p2; 8053 break; 8054 } 8055 8056 8057 /* Opcode: AggStep * P2 P3 P4 P5 8058 ** Synopsis: accum=r[P3] step(r[P2@P5]) 8059 ** 8060 ** Execute the xStep function for an aggregate. 8061 ** The function has P5 arguments. P4 is a pointer to the 8062 ** FuncDef structure that specifies the function. Register P3 is the 8063 ** accumulator. 8064 ** 8065 ** The P5 arguments are taken from register P2 and its 8066 ** successors. 8067 */ 8068 /* Opcode: AggInverse * P2 P3 P4 P5 8069 ** Synopsis: accum=r[P3] inverse(r[P2@P5]) 8070 ** 8071 ** Execute the xInverse function for an aggregate. 8072 ** The function has P5 arguments. P4 is a pointer to the 8073 ** FuncDef structure that specifies the function. Register P3 is the 8074 ** accumulator. 8075 ** 8076 ** The P5 arguments are taken from register P2 and its 8077 ** successors. 8078 */ 8079 /* Opcode: AggStep1 P1 P2 P3 P4 P5 8080 ** Synopsis: accum=r[P3] step(r[P2@P5]) 8081 ** 8082 ** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an 8083 ** aggregate. The function has P5 arguments. P4 is a pointer to the 8084 ** FuncDef structure that specifies the function. Register P3 is the 8085 ** accumulator. 8086 ** 8087 ** The P5 arguments are taken from register P2 and its 8088 ** successors. 8089 ** 8090 ** This opcode is initially coded as OP_AggStep0. On first evaluation, 8091 ** the FuncDef stored in P4 is converted into an sqlite3_context and 8092 ** the opcode is changed. In this way, the initialization of the 8093 ** sqlite3_context only happens once, instead of on each call to the 8094 ** step function. 8095 */ 8096 case OP_AggInverse: 8097 case OP_AggStep: { 8098 int n; 8099 sqlite3_context *pCtx; 8100 u64 nAlloc; 8101 8102 assert( pOp->p4type==P4_FUNCDEF ); 8103 n = pOp->p5; 8104 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 8105 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) ); 8106 assert( pOp->p3p2 || pOp->p3>=pOp->p2+n ); 8107 8108 /* Allocate space for (a) the context object and (n-1) extra pointers 8109 ** to append to the sqlite3_context.argv[1] array, and (b) a memory 8110 ** cell in which to store the accumulation. Be careful that the memory 8111 ** cell is 8-byte aligned, even on platforms where a pointer is 32-bits. 8112 ** 8113 ** Note: We could avoid this by using a regular memory cell from aMem[] for 8114 ** the accumulator, instead of allocating one here. */ 8115 nAlloc = ROUND8P( SZ_CONTEXT(n) ); 8116 pCtx = sqlite3DbMallocRawNN(db, nAlloc + sizeof(Mem)); 8117 if( pCtx==0 ) goto no_mem; 8118 pCtx->pOut = (Mem*)((u8*)pCtx + nAlloc); 8119 assert( EIGHT_BYTE_ALIGNMENT(pCtx->pOut) ); 8120 8121 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null); 8122 pCtx->pMem = 0; 8123 pCtx->pFunc = pOp->p4.pFunc; 8124 pCtx->iOp = (int)(pOp - aOp); 8125 pCtx->pVdbe = p; 8126 pCtx->skipFlag = 0; 8127 pCtx->isError = 0; 8128 pCtx->enc = encoding; 8129 pCtx->argc = n; 8130 pOp->p4type = P4_FUNCCTX; 8131 pOp->p4.pCtx = pCtx; 8132 8133 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */ 8134 assert( pOp->p1==(pOp->opcode==OP_AggInverse) ); 8135 8136 pOp->opcode = OP_AggStep1; 8137 /* Fall through into OP_AggStep */ 8138 /* no break */ deliberate_fall_through 8139 } 8140 case OP_AggStep1: { 8141 int i; 8142 sqlite3_context *pCtx; 8143 Mem *pMem; 8144 8145 assert( pOp->p4type==P4_FUNCCTX ); 8146 pCtx = pOp->p4.pCtx; 8147 pMem = &aMem[pOp->p3]; 8148 8149 #ifdef SQLITE_DEBUG 8150 if( pOp->p1 ){ 8151 /* This is an OP_AggInverse call. Verify that xStep has always 8152 ** been called at least once prior to any xInverse call. */ 8153 assert( pMem->uTemp==0x1122e0e3 ); 8154 }else{ 8155 /* This is an OP_AggStep call. Mark it as such. */ 8156 pMem->uTemp = 0x1122e0e3; 8157 } 8158 #endif 8159 8160 /* If this function is inside of a trigger, the register array in aMem[] 8161 ** might change from one evaluation to the next. The next block of code 8162 ** checks to see if the register array has changed, and if so it 8163 ** reinitializes the relevant parts of the sqlite3_context object */ 8164 if( pCtx->pMem != pMem ){ 8165 pCtx->pMem = pMem; 8166 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; 8167 } 8168 8169 #ifdef SQLITE_DEBUG 8170 for(i=0; iargc; i++){ 8171 assert( memIsValid(pCtx->argv[i]) ); 8172 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); 8173 } 8174 #endif 8175 8176 pMem->n++; 8177 assert( pCtx->pOut->flags==MEM_Null ); 8178 assert( pCtx->isError==0 ); 8179 assert( pCtx->skipFlag==0 ); 8180 #ifndef SQLITE_OMIT_WINDOWFUNC 8181 if( pOp->p1 ){ 8182 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv); 8183 }else 8184 #endif 8185 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ 8186 8187 if( pCtx->isError ){ 8188 if( pCtx->isError>0 ){ 8189 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); 8190 rc = pCtx->isError; 8191 } 8192 if( pCtx->skipFlag ){ 8193 assert( pOp[-1].opcode==OP_CollSeq ); 8194 i = pOp[-1].p1; 8195 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); 8196 pCtx->skipFlag = 0; 8197 } 8198 sqlite3VdbeMemRelease(pCtx->pOut); 8199 pCtx->pOut->flags = MEM_Null; 8200 pCtx->isError = 0; 8201 if( rc ) goto abort_due_to_error; 8202 } 8203 assert( pCtx->pOut->flags==MEM_Null ); 8204 assert( pCtx->skipFlag==0 ); 8205 break; 8206 } 8207 8208 /* Opcode: AggFinal P1 P2 * P4 * 8209 ** Synopsis: accum=r[P1] N=P2 8210 ** 8211 ** P1 is the memory location that is the accumulator for an aggregate 8212 ** or window function. Execute the finalizer function 8213 ** for an aggregate and store the result in P1. 8214 ** 8215 ** P2 is the number of arguments that the step function takes and 8216 ** P4 is a pointer to the FuncDef for this function. The P2 8217 ** argument is not used by this opcode. It is only there to disambiguate 8218 ** functions that can take varying numbers of arguments. The 8219 ** P4 argument is only needed for the case where 8220 ** the step function was not previously called. 8221 */ 8222 /* Opcode: AggValue * P2 P3 P4 * 8223 ** Synopsis: r[P3]=value N=P2 8224 ** 8225 ** Invoke the xValue() function and store the result in register P3. 8226 ** 8227 ** P2 is the number of arguments that the step function takes and 8228 ** P4 is a pointer to the FuncDef for this function. The P2 8229 ** argument is not used by this opcode. It is only there to disambiguate 8230 ** functions that can take varying numbers of arguments. The 8231 ** P4 argument is only needed for the case where 8232 ** the step function was not previously called. 8233 */ 8234 case OP_AggValue: 8235 case OP_AggFinal: { 8236 Mem *pMem; 8237 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 8238 assert( pOp->p3==0 || pOp->opcode==OP_AggValue ); 8239 pMem = &aMem[pOp->p1]; 8240 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); 8241 #ifndef SQLITE_OMIT_WINDOWFUNC 8242 if( pOp->p3 ){ 8243 memAboutToChange(p, &aMem[pOp->p3]); 8244 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc); 8245 pMem = &aMem[pOp->p3]; 8246 }else 8247 #endif 8248 { 8249 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); 8250 } 8251 8252 if( rc ){ 8253 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); 8254 goto abort_due_to_error; 8255 } 8256 sqlite3VdbeChangeEncoding(pMem, encoding); 8257 UPDATE_MAX_BLOBSIZE(pMem); 8258 REGISTER_TRACE((int)(pMem-aMem), pMem); 8259 break; 8260 } 8261 8262 #ifndef SQLITE_OMIT_WAL 8263 /* Opcode: Checkpoint P1 P2 P3 * * 8264 ** 8265 ** Checkpoint database P1. This is a no-op if P1 is not currently in 8266 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL, 8267 ** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns 8268 ** SQLITE_BUSY or not, respectively. Write the number of pages in the 8269 ** WAL after the checkpoint into mem[P3+1] and the number of pages 8270 ** in the WAL that have been checkpointed after the checkpoint 8271 ** completes into mem[P3+2]. However on an error, mem[P3+1] and 8272 ** mem[P3+2] are initialized to -1. 8273 */ 8274 case OP_Checkpoint: { 8275 int i; /* Loop counter */ 8276 int aRes[3]; /* Results */ 8277 Mem *pMem; /* Write results here */ 8278 8279 assert( p->readOnly==0 ); 8280 aRes[0] = 0; 8281 aRes[1] = aRes[2] = -1; 8282 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE 8283 || pOp->p2==SQLITE_CHECKPOINT_FULL 8284 || pOp->p2==SQLITE_CHECKPOINT_RESTART 8285 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE 8286 || pOp->p2==SQLITE_CHECKPOINT_NOOP 8287 ); 8288 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); 8289 if( rc ){ 8290 if( rc!=SQLITE_BUSY ) goto abort_due_to_error; 8291 rc = SQLITE_OK; 8292 aRes[0] = 1; 8293 } 8294 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ 8295 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); 8296 } 8297 break; 8298 }; 8299 #endif 8300 8301 #ifndef SQLITE_OMIT_PRAGMA 8302 /* Opcode: JournalMode P1 P2 P3 * * 8303 ** 8304 ** Change the journal mode of database P1 to P3. P3 must be one of the 8305 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback 8306 ** modes (delete, truncate, persist, off and memory), this is a simple 8307 ** operation. No IO is required. 8308 ** 8309 ** If changing into or out of WAL mode the procedure is more complicated. 8310 ** 8311 ** Write a string containing the final journal-mode to register P2. 8312 */ 8313 case OP_JournalMode: { /* out2 */ 8314 Btree *pBt; /* Btree to change journal mode of */ 8315 Pager *pPager; /* Pager associated with pBt */ 8316 int eNew; /* New journal mode */ 8317 int eOld; /* The old journal mode */ 8318 #ifndef SQLITE_OMIT_WAL 8319 const char *zFilename; /* Name of database file for pPager */ 8320 #endif 8321 8322 pOut = out2Prerelease(p, pOp); 8323 eNew = pOp->p3; 8324 assert( eNew==PAGER_JOURNALMODE_DELETE 8325 || eNew==PAGER_JOURNALMODE_TRUNCATE 8326 || eNew==PAGER_JOURNALMODE_PERSIST 8327 || eNew==PAGER_JOURNALMODE_OFF 8328 || eNew==PAGER_JOURNALMODE_MEMORY 8329 || eNew==PAGER_JOURNALMODE_WAL 8330 || eNew==PAGER_JOURNALMODE_QUERY 8331 ); 8332 assert( pOp->p1>=0 && pOp->p1nDb ); 8333 assert( p->readOnly==0 ); 8334 8335 pBt = db->aDb[pOp->p1].pBt; 8336 pPager = sqlite3BtreePager(pBt); 8337 eOld = sqlite3PagerGetJournalMode(pPager); 8338 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; 8339 assert( sqlite3BtreeHoldsMutex(pBt) ); 8340 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; 8341 8342 #ifndef SQLITE_OMIT_WAL 8343 zFilename = sqlite3PagerFilename(pPager, 1); 8344 8345 /* Do not allow a transition to journal_mode=WAL for a database 8346 ** in temporary storage or if the VFS does not support shared memory 8347 */ 8348 if( eNew==PAGER_JOURNALMODE_WAL 8349 && (sqlite3Strlen30(zFilename)==0 /* Temp file */ 8350 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ 8351 ){ 8352 eNew = eOld; 8353 } 8354 8355 if( (eNew!=eOld) 8356 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) 8357 ){ 8358 if( !db->autoCommit || db->nVdbeRead>1 ){ 8359 rc = SQLITE_ERROR; 8360 sqlite3VdbeError(p, 8361 "cannot change %s wal mode from within a transaction", 8362 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") 8363 ); 8364 goto abort_due_to_error; 8365 }else{ 8366 8367 if( eOld==PAGER_JOURNALMODE_WAL ){ 8368 /* If leaving WAL mode, close the log file. If successful, the call 8369 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log 8370 ** file. An EXCLUSIVE lock may still be held on the database file 8371 ** after a successful return. 8372 */ 8373 rc = sqlite3PagerCloseWal(pPager, db); 8374 if( rc==SQLITE_OK ){ 8375 sqlite3PagerSetJournalMode(pPager, eNew); 8376 } 8377 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ 8378 /* Cannot transition directly from MEMORY to WAL. Use mode OFF 8379 ** as an intermediate */ 8380 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); 8381 } 8382 8383 /* Open a transaction on the database file. Regardless of the journal 8384 ** mode, this transaction always uses a rollback journal. 8385 */ 8386 assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE ); 8387 if( rc==SQLITE_OK ){ 8388 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); 8389 } 8390 } 8391 } 8392 #endif /* ifndef SQLITE_OMIT_WAL */ 8393 8394 if( rc ) eNew = eOld; 8395 eNew = sqlite3PagerSetJournalMode(pPager, eNew); 8396 8397 pOut->flags = MEM_Str|MEM_Static|MEM_Term; 8398 pOut->z = (char *)sqlite3JournalModename(eNew); 8399 pOut->n = sqlite3Strlen30(pOut->z); 8400 pOut->enc = SQLITE_UTF8; 8401 sqlite3VdbeChangeEncoding(pOut, encoding); 8402 if( rc ) goto abort_due_to_error; 8403 break; 8404 }; 8405 #endif /* SQLITE_OMIT_PRAGMA */ 8406 8407 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) 8408 /* Opcode: Vacuum P1 P2 * * * 8409 ** 8410 ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more 8411 ** for an attached database. The "temp" database may not be vacuumed. 8412 ** 8413 ** If P2 is not zero, then it is a register holding a string which is 8414 ** the file into which the result of vacuum should be written. When 8415 ** P2 is zero, the vacuum overwrites the original database. 8416 */ 8417 case OP_Vacuum: { 8418 assert( p->readOnly==0 ); 8419 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1, 8420 pOp->p2 ? &aMem[pOp->p2] : 0); 8421 if( rc ) goto abort_due_to_error; 8422 break; 8423 } 8424 #endif 8425 8426 #if !defined(SQLITE_OMIT_AUTOVACUUM) 8427 /* Opcode: IncrVacuum P1 P2 * * * 8428 ** 8429 ** Perform a single step of the incremental vacuum procedure on 8430 ** the P1 database. If the vacuum has finished, jump to instruction 8431 ** P2. Otherwise, fall through to the next instruction. 8432 */ 8433 case OP_IncrVacuum: { /* jump */ 8434 Btree *pBt; 8435 8436 assert( pOp->p1>=0 && pOp->p1nDb ); 8437 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 8438 assert( p->readOnly==0 ); 8439 pBt = db->aDb[pOp->p1].pBt; 8440 rc = sqlite3BtreeIncrVacuum(pBt); 8441 VdbeBranchTaken(rc==SQLITE_DONE,2); 8442 if( rc ){ 8443 if( rc!=SQLITE_DONE ) goto abort_due_to_error; 8444 rc = SQLITE_OK; 8445 goto jump_to_p2; 8446 } 8447 break; 8448 } 8449 #endif 8450 8451 /* Opcode: Expire P1 P2 * * * 8452 ** 8453 ** Cause precompiled statements to expire. When an expired statement 8454 ** is executed using sqlite3_step() it will either automatically 8455 ** reprepare itself (if it was originally created using sqlite3_prepare_v2()) 8456 ** or it will fail with SQLITE_SCHEMA. 8457 ** 8458 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, 8459 ** then only the currently executing statement is expired. 8460 ** 8461 ** If P2 is 0, then SQL statements are expired immediately. If P2 is 1, 8462 ** then running SQL statements are allowed to continue to run to completion. 8463 ** The P2==1 case occurs when a CREATE INDEX or similar schema change happens 8464 ** that might help the statement run faster but which does not affect the 8465 ** correctness of operation. 8466 */ 8467 case OP_Expire: { 8468 assert( pOp->p2==0 || pOp->p2==1 ); 8469 if( !pOp->p1 ){ 8470 sqlite3ExpirePreparedStatements(db, pOp->p2); 8471 }else{ 8472 p->expired = pOp->p2+1; 8473 } 8474 break; 8475 } 8476 8477 /* Opcode: CursorLock P1 * * * * 8478 ** 8479 ** Lock the btree to which cursor P1 is pointing so that the btree cannot be 8480 ** written by an other cursor. 8481 */ 8482 case OP_CursorLock: { 8483 VdbeCursor *pC; 8484 assert( pOp->p1>=0 && pOp->p1nCursor ); 8485 pC = p->apCsr[pOp->p1]; 8486 assert( pC!=0 ); 8487 assert( pC->eCurType==CURTYPE_BTREE ); 8488 sqlite3BtreeCursorPin(pC->uc.pCursor); 8489 break; 8490 } 8491 8492 /* Opcode: CursorUnlock P1 * * * * 8493 ** 8494 ** Unlock the btree to which cursor P1 is pointing so that it can be 8495 ** written by other cursors. 8496 */ 8497 case OP_CursorUnlock: { 8498 VdbeCursor *pC; 8499 assert( pOp->p1>=0 && pOp->p1nCursor ); 8500 pC = p->apCsr[pOp->p1]; 8501 assert( pC!=0 ); 8502 assert( pC->eCurType==CURTYPE_BTREE ); 8503 sqlite3BtreeCursorUnpin(pC->uc.pCursor); 8504 break; 8505 } 8506 8507 #ifndef SQLITE_OMIT_SHARED_CACHE 8508 /* Opcode: TableLock P1 P2 P3 P4 * 8509 ** Synopsis: iDb=P1 root=P2 write=P3 8510 ** 8511 ** Obtain a lock on a particular table. This instruction is only used when 8512 ** the shared-cache feature is enabled. 8513 ** 8514 ** P1 is the index of the database in sqlite3.aDb[] of the database 8515 ** on which the lock is acquired. A readlock is obtained if P3==0 or 8516 ** a write lock if P3==1. 8517 ** 8518 ** P2 contains the root-page of the table to lock. 8519 ** 8520 ** P4 contains a pointer to the name of the table being locked. This is only 8521 ** used to generate an error message if the lock cannot be obtained. 8522 */ 8523 case OP_TableLock: { 8524 u8 isWriteLock = (u8)pOp->p3; 8525 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){ 8526 int p1 = pOp->p1; 8527 assert( p1>=0 && p1nDb ); 8528 assert( DbMaskTest(p->btreeMask, p1) ); 8529 assert( isWriteLock==0 || isWriteLock==1 ); 8530 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); 8531 if( rc ){ 8532 if( (rc&0xFF)==SQLITE_LOCKED ){ 8533 const char *z = pOp->p4.z; 8534 sqlite3VdbeError(p, "database table is locked: %s", z); 8535 } 8536 goto abort_due_to_error; 8537 } 8538 } 8539 break; 8540 } 8541 #endif /* SQLITE_OMIT_SHARED_CACHE */ 8542 8543 #ifndef SQLITE_OMIT_VIRTUALTABLE 8544 /* Opcode: VBegin * * * P4 * 8545 ** 8546 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the 8547 ** xBegin method for that table. 8548 ** 8549 ** Also, whether or not P4 is set, check that this is not being called from 8550 ** within a callback to a virtual table xSync() method. If it is, the error 8551 ** code will be set to SQLITE_LOCKED. 8552 */ 8553 case OP_VBegin: { 8554 VTable *pVTab; 8555 pVTab = pOp->p4.pVtab; 8556 rc = sqlite3VtabBegin(db, pVTab); 8557 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); 8558 if( rc ) goto abort_due_to_error; 8559 break; 8560 } 8561 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 8562 8563 #ifndef SQLITE_OMIT_VIRTUALTABLE 8564 /* Opcode: VCreate P1 P2 * * * 8565 ** 8566 ** P2 is a register that holds the name of a virtual table in database 8567 ** P1. Call the xCreate method for that table. 8568 */ 8569 case OP_VCreate: { 8570 Mem sMem; /* For storing the record being decoded */ 8571 const char *zTab; /* Name of the virtual table */ 8572 8573 memset(&sMem, 0, sizeof(sMem)); 8574 sMem.db = db; 8575 /* Because P2 is always a static string, it is impossible for the 8576 ** sqlite3VdbeMemCopy() to fail */ 8577 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 ); 8578 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 ); 8579 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]); 8580 assert( rc==SQLITE_OK ); 8581 zTab = (const char*)sqlite3_value_text(&sMem); 8582 assert( zTab || db->mallocFailed ); 8583 if( zTab ){ 8584 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); 8585 } 8586 sqlite3VdbeMemRelease(&sMem); 8587 if( rc ) goto abort_due_to_error; 8588 break; 8589 } 8590 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 8591 8592 #ifndef SQLITE_OMIT_VIRTUALTABLE 8593 /* Opcode: VDestroy P1 * * P4 * 8594 ** 8595 ** P4 is the name of a virtual table in database P1. Call the xDestroy method 8596 ** of that table. 8597 */ 8598 case OP_VDestroy: { 8599 db->nVDestroy++; 8600 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); 8601 db->nVDestroy--; 8602 assert( p->errorAction==OE_Abort && p->usesStmtJournal ); 8603 if( rc ) goto abort_due_to_error; 8604 break; 8605 } 8606 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 8607 8608 #ifndef SQLITE_OMIT_VIRTUALTABLE 8609 /* Opcode: VOpen P1 * * P4 * 8610 ** 8611 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. 8612 ** P1 is a cursor number. This opcode opens a cursor to the virtual 8613 ** table and stores that cursor in P1. 8614 */ 8615 case OP_VOpen: { /* ncycle */ 8616 VdbeCursor *pCur; 8617 sqlite3_vtab_cursor *pVCur; 8618 sqlite3_vtab *pVtab; 8619 const sqlite3_module *pModule; 8620 8621 assert( p->bIsReader ); 8622 pCur = p->apCsr[pOp->p1]; 8623 if( pCur!=0 8624 && ALWAYS( pCur->eCurType==CURTYPE_VTAB ) 8625 && ALWAYS( pCur->uc.pVCur->pVtab==pOp->p4.pVtab->pVtab ) 8626 ){ 8627 /* This opcode is a no-op if the cursor is already open */ 8628 break; 8629 } 8630 pVCur = 0; 8631 pVtab = pOp->p4.pVtab->pVtab; 8632 if( pVtab==0 || NEVER(pVtab->pModule==0) ){ 8633 rc = SQLITE_LOCKED; 8634 goto abort_due_to_error; 8635 } 8636 pModule = pVtab->pModule; 8637 rc = pModule->xOpen(pVtab, &pVCur); 8638 sqlite3VtabImportErrmsg(p, pVtab); 8639 if( rc ) goto abort_due_to_error; 8640 8641 /* Initialize sqlite3_vtab_cursor base class */ 8642 pVCur->pVtab = pVtab; 8643 8644 /* Initialize vdbe cursor object */ 8645 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB); 8646 if( pCur ){ 8647 pCur->uc.pVCur = pVCur; 8648 pVtab->nRef++; 8649 }else{ 8650 assert( db->mallocFailed ); 8651 pModule->xClose(pVCur); 8652 goto no_mem; 8653 } 8654 break; 8655 } 8656 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 8657 8658 #ifndef SQLITE_OMIT_VIRTUALTABLE 8659 /* Opcode: VCheck P1 P2 P3 P4 * 8660 ** 8661 ** P4 is a pointer to a Table object that is a virtual table in schema P1 8662 ** that supports the xIntegrity() method. This opcode runs the xIntegrity() 8663 ** method for that virtual table, using P3 as the integer argument. If 8664 ** an error is reported back, the table name is prepended to the error 8665 ** message and that message is stored in P2. If no errors are seen, 8666 ** register P2 is set to NULL. 8667 */ 8668 case OP_VCheck: { /* out2 */ 8669 Table *pTab; 8670 sqlite3_vtab *pVtab; 8671 const sqlite3_module *pModule; 8672 char *zErr = 0; 8673 8674 pOut = &aMem[pOp->p2]; 8675 sqlite3VdbeMemSetNull(pOut); /* Innocent until proven guilty */ 8676 assert( pOp->p4type==P4_TABLEREF ); 8677 pTab = pOp->p4.pTab; 8678 assert( pTab!=0 ); 8679 assert( pTab->nTabRef>0 ); 8680 assert( IsVirtual(pTab) ); 8681 if( pTab->u.vtab.p==0 ) break; 8682 pVtab = pTab->u.vtab.p->pVtab; 8683 assert( pVtab!=0 ); 8684 pModule = pVtab->pModule; 8685 assert( pModule!=0 ); 8686 assert( pModule->iVersion>=4 ); 8687 assert( pModule->xIntegrity!=0 ); 8688 sqlite3VtabLock(pTab->u.vtab.p); 8689 assert( pOp->p1>=0 && pOp->p1nDb ); 8690 rc = pModule->xIntegrity(pVtab, db->aDb[pOp->p1].zDbSName, pTab->zName, 8691 pOp->p3, &zErr); 8692 sqlite3VtabUnlock(pTab->u.vtab.p); 8693 if( rc ){ 8694 sqlite3_free(zErr); 8695 goto abort_due_to_error; 8696 } 8697 if( zErr ){ 8698 sqlite3VdbeMemSetStr(pOut, zErr, -1, SQLITE_UTF8, sqlite3_free); 8699 } 8700 break; 8701 } 8702 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 8703 8704 #ifndef SQLITE_OMIT_VIRTUALTABLE 8705 /* Opcode: VInitIn P1 P2 P3 * * 8706 ** Synopsis: r[P2]=ValueList(P1,P3) 8707 ** 8708 ** Set register P2 to be a pointer to a ValueList object for cursor P1 8709 ** with cache register P3 and output register P3+1. This ValueList object 8710 ** can be used as the first argument to sqlite3_vtab_in_first() and 8711 ** sqlite3_vtab_in_next() to extract all of the values stored in the P1 8712 ** cursor. Register P3 is used to hold the values returned by 8713 ** sqlite3_vtab_in_first() and sqlite3_vtab_in_next(). 8714 */ 8715 case OP_VInitIn: { /* out2, ncycle */ 8716 VdbeCursor *pC; /* The cursor containing the RHS values */ 8717 ValueList *pRhs; /* New ValueList object to put in reg[P2] */ 8718 8719 pC = p->apCsr[pOp->p1]; 8720 pRhs = sqlite3_malloc64( sizeof(*pRhs) ); 8721 if( pRhs==0 ) goto no_mem; 8722 pRhs->pCsr = pC->uc.pCursor; 8723 pRhs->pOut = &aMem[pOp->p3]; 8724 pOut = out2Prerelease(p, pOp); 8725 pOut->flags = MEM_Null; 8726 sqlite3VdbeMemSetPointer(pOut, pRhs, "ValueList", sqlite3VdbeValueListFree); 8727 break; 8728 } 8729 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 8730 8731 8732 #ifndef SQLITE_OMIT_VIRTUALTABLE 8733 /* Opcode: VFilter P1 P2 P3 P4 * 8734 ** Synopsis: iplan=r[P3] zplan='P4' 8735 ** 8736 ** P1 is a cursor opened using VOpen. P2 is an address to jump to if 8737 ** the filtered result set is empty. 8738 ** 8739 ** P4 is either NULL or a string that was generated by the xBestIndex 8740 ** method of the module. The interpretation of the P4 string is left 8741 ** to the module implementation. 8742 ** 8743 ** This opcode invokes the xFilter method on the virtual table specified 8744 ** by P1. The integer query plan parameter to xFilter is stored in register 8745 ** P3. Register P3+1 stores the argc parameter to be passed to the 8746 ** xFilter method. Registers P3+2..P3+1+argc are the argc 8747 ** additional parameters which are passed to 8748 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. 8749 ** 8750 ** A jump is made to P2 if the result set after filtering would be empty. 8751 */ 8752 case OP_VFilter: { /* jump, ncycle */ 8753 int nArg; 8754 int iQuery; 8755 const sqlite3_module *pModule; 8756 Mem *pQuery; 8757 Mem *pArgc; 8758 sqlite3_vtab_cursor *pVCur; 8759 sqlite3_vtab *pVtab; 8760 VdbeCursor *pCur; 8761 int res; 8762 int i; 8763 Mem **apArg; 8764 8765 pQuery = &aMem[pOp->p3]; 8766 pArgc = &pQuery[1]; 8767 pCur = p->apCsr[pOp->p1]; 8768 assert( memIsValid(pQuery) ); 8769 REGISTER_TRACE(pOp->p3, pQuery); 8770 assert( pCur!=0 ); 8771 assert( pCur->eCurType==CURTYPE_VTAB ); 8772 pVCur = pCur->uc.pVCur; 8773 pVtab = pVCur->pVtab; 8774 pModule = pVtab->pModule; 8775 8776 /* Grab the index number and argc parameters */ 8777 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); 8778 nArg = (int)pArgc->u.i; 8779 iQuery = (int)pQuery->u.i; 8780 8781 /* Invoke the xFilter method */ 8782 apArg = p->apArg; 8783 assert( nArg<=p->napArg ); 8784 for(i = 0; ixFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg); 8788 sqlite3VtabImportErrmsg(p, pVtab); 8789 if( rc ) goto abort_due_to_error; 8790 res = pModule->xEof(pVCur); 8791 pCur->nullRow = 0; 8792 VdbeBranchTaken(res!=0,2); 8793 if( res ) goto jump_to_p2; 8794 break; 8795 } 8796 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 8797 8798 #ifndef SQLITE_OMIT_VIRTUALTABLE 8799 /* Opcode: VColumn P1 P2 P3 * P5 8800 ** Synopsis: r[P3]=vcolumn(P2) 8801 ** 8802 ** Store in register P3 the value of the P2-th column of 8803 ** the current row of the virtual-table of cursor P1. 8804 ** 8805 ** If the VColumn opcode is being used to fetch the value of 8806 ** an unchanging column during an UPDATE operation, then the P5 8807 ** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange() 8808 ** function to return true inside the xColumn method of the virtual 8809 ** table implementation. The P5 column might also contain other 8810 ** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are 8811 ** unused by OP_VColumn. 8812 */ 8813 case OP_VColumn: { /* ncycle */ 8814 sqlite3_vtab *pVtab; 8815 const sqlite3_module *pModule; 8816 Mem *pDest; 8817 sqlite3_context sContext; 8818 FuncDef nullFunc; 8819 8820 VdbeCursor *pCur = p->apCsr[pOp->p1]; 8821 assert( pCur!=0 ); 8822 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 8823 pDest = &aMem[pOp->p3]; 8824 memAboutToChange(p, pDest); 8825 if( pCur->nullRow ){ 8826 sqlite3VdbeMemSetNull(pDest); 8827 break; 8828 } 8829 assert( pCur->eCurType==CURTYPE_VTAB ); 8830 pVtab = pCur->uc.pVCur->pVtab; 8831 pModule = pVtab->pModule; 8832 assert( pModule->xColumn ); 8833 memset(&sContext, 0, sizeof(sContext)); 8834 sContext.pOut = pDest; 8835 sContext.enc = encoding; 8836 nullFunc.pUserData = 0; 8837 nullFunc.funcFlags = SQLITE_RESULT_SUBTYPE; 8838 sContext.pFunc = &nullFunc; 8839 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 ); 8840 if( pOp->p5 & OPFLAG_NOCHNG ){ 8841 sqlite3VdbeMemSetNull(pDest); 8842 pDest->flags = MEM_Null|MEM_Zero; 8843 pDest->u.nZero = 0; 8844 }else{ 8845 MemSetTypeFlag(pDest, MEM_Null); 8846 } 8847 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2); 8848 sqlite3VtabImportErrmsg(p, pVtab); 8849 if( sContext.isError>0 ){ 8850 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest)); 8851 rc = sContext.isError; 8852 } 8853 sqlite3VdbeChangeEncoding(pDest, encoding); 8854 REGISTER_TRACE(pOp->p3, pDest); 8855 UPDATE_MAX_BLOBSIZE(pDest); 8856 8857 if( rc ) goto abort_due_to_error; 8858 break; 8859 } 8860 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 8861 8862 #ifndef SQLITE_OMIT_VIRTUALTABLE 8863 /* Opcode: VNext P1 P2 * * * 8864 ** 8865 ** Advance virtual table P1 to the next row in its result set and 8866 ** jump to instruction P2. Or, if the virtual table has reached 8867 ** the end of its result set, then fall through to the next instruction. 8868 */ 8869 case OP_VNext: { /* jump, ncycle */ 8870 sqlite3_vtab *pVtab; 8871 const sqlite3_module *pModule; 8872 int res; 8873 VdbeCursor *pCur; 8874 8875 pCur = p->apCsr[pOp->p1]; 8876 assert( pCur!=0 ); 8877 assert( pCur->eCurType==CURTYPE_VTAB ); 8878 if( pCur->nullRow ){ 8879 break; 8880 } 8881 pVtab = pCur->uc.pVCur->pVtab; 8882 pModule = pVtab->pModule; 8883 assert( pModule->xNext ); 8884 8885 /* Invoke the xNext() method of the module. There is no way for the 8886 ** underlying implementation to return an error if one occurs during 8887 ** xNext(). Instead, if an error occurs, true is returned (indicating that 8888 ** data is available) and the error code returned when xColumn or 8889 ** some other method is next invoked on the save virtual table cursor. 8890 */ 8891 rc = pModule->xNext(pCur->uc.pVCur); 8892 sqlite3VtabImportErrmsg(p, pVtab); 8893 if( rc ) goto abort_due_to_error; 8894 res = pModule->xEof(pCur->uc.pVCur); 8895 VdbeBranchTaken(!res,2); 8896 if( !res ){ 8897 /* If there is data, jump to P2 */ 8898 goto jump_to_p2_and_check_for_interrupt; 8899 } 8900 goto check_for_interrupt; 8901 } 8902 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 8903 8904 #ifndef SQLITE_OMIT_VIRTUALTABLE 8905 /* Opcode: VRename P1 * * P4 * 8906 ** 8907 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. 8908 ** This opcode invokes the corresponding xRename method. The value 8909 ** in register P1 is passed as the zName argument to the xRename method. 8910 */ 8911 case OP_VRename: { 8912 sqlite3_vtab *pVtab; 8913 Mem *pName; 8914 int isLegacy; 8915 8916 isLegacy = (db->flags & SQLITE_LegacyAlter); 8917 db->flags |= SQLITE_LegacyAlter; 8918 pVtab = pOp->p4.pVtab->pVtab; 8919 pName = &aMem[pOp->p1]; 8920 assert( pVtab->pModule->xRename ); 8921 assert( memIsValid(pName) ); 8922 assert( p->readOnly==0 ); 8923 REGISTER_TRACE(pOp->p1, pName); 8924 assert( pName->flags & MEM_Str ); 8925 testcase( pName->enc==SQLITE_UTF8 ); 8926 testcase( pName->enc==SQLITE_UTF16BE ); 8927 testcase( pName->enc==SQLITE_UTF16LE ); 8928 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); 8929 if( rc ) goto abort_due_to_error; 8930 rc = pVtab->pModule->xRename(pVtab, pName->z); 8931 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter; 8932 sqlite3VtabImportErrmsg(p, pVtab); 8933 p->expired = 0; 8934 if( rc ) goto abort_due_to_error; 8935 break; 8936 } 8937 #endif 8938 8939 #ifndef SQLITE_OMIT_VIRTUALTABLE 8940 /* Opcode: VUpdate P1 P2 P3 P4 P5 8941 ** Synopsis: data=r[P3@P2] 8942 ** 8943 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. 8944 ** This opcode invokes the corresponding xUpdate method. P2 values 8945 ** are contiguous memory cells starting at P3 to pass to the xUpdate 8946 ** invocation. The value in register (P3+P2-1) corresponds to the 8947 ** p2th element of the argv array passed to xUpdate. 8948 ** 8949 ** The xUpdate method will do a DELETE or an INSERT or both. 8950 ** The argv[0] element (which corresponds to memory cell P3) 8951 ** is the rowid of a row to delete. If argv[0] is NULL then no 8952 ** deletion occurs. The argv[1] element is the rowid of the new 8953 ** row. This can be NULL to have the virtual table select the new 8954 ** rowid for itself. The subsequent elements in the array are 8955 ** the values of columns in the new row. 8956 ** 8957 ** If P2==1 then no insert is performed. argv[0] is the rowid of 8958 ** a row to delete. 8959 ** 8960 ** P1 is a boolean flag. If it is set to true and the xUpdate call 8961 ** is successful, then the value returned by sqlite3_last_insert_rowid() 8962 ** is set to the value of the rowid for the row just inserted. 8963 ** 8964 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to 8965 ** apply in the case of a constraint failure on an insert or update. 8966 */ 8967 case OP_VUpdate: { 8968 sqlite3_vtab *pVtab; 8969 const sqlite3_module *pModule; 8970 int nArg; 8971 int i; 8972 sqlite_int64 rowid = 0; 8973 Mem **apArg; 8974 Mem *pX; 8975 8976 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback 8977 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace 8978 ); 8979 assert( p->readOnly==0 ); 8980 if( db->mallocFailed ) goto no_mem; 8981 sqlite3VdbeIncrWriteCounter(p, 0); 8982 pVtab = pOp->p4.pVtab->pVtab; 8983 if( pVtab==0 || NEVER(pVtab->pModule==0) ){ 8984 rc = SQLITE_LOCKED; 8985 goto abort_due_to_error; 8986 } 8987 pModule = pVtab->pModule; 8988 nArg = pOp->p2; 8989 assert( pOp->p4type==P4_VTAB ); 8990 if( ALWAYS(pModule->xUpdate) ){ 8991 u8 vtabOnConflict = db->vtabOnConflict; 8992 apArg = p->apArg; 8993 pX = &aMem[pOp->p3]; 8994 assert( nArg<=p->napArg ); 8995 for(i=0; ivtabOnConflict = pOp->p5; 9002 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); 9003 db->vtabOnConflict = vtabOnConflict; 9004 sqlite3VtabImportErrmsg(p, pVtab); 9005 if( rc==SQLITE_OK && pOp->p1 ){ 9006 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); 9007 db->lastRowid = rowid; 9008 } 9009 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ 9010 if( pOp->p5==OE_Ignore ){ 9011 rc = SQLITE_OK; 9012 }else{ 9013 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); 9014 } 9015 }else{ 9016 p->nChange++; 9017 } 9018 if( rc ) goto abort_due_to_error; 9019 } 9020 break; 9021 } 9022 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 9023 9024 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 9025 /* Opcode: Pagecount P1 P2 * * * 9026 ** 9027 ** Write the current number of pages in database P1 to memory cell P2. 9028 */ 9029 case OP_Pagecount: { /* out2 */ 9030 pOut = out2Prerelease(p, pOp); 9031 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); 9032 break; 9033 } 9034 #endif 9035 9036 9037 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 9038 /* Opcode: MaxPgcnt P1 P2 P3 * * 9039 ** 9040 ** Try to set the maximum page count for database P1 to the value in P3. 9041 ** Do not let the maximum page count fall below the current page count and 9042 ** do not change the maximum page count value if P3==0. 9043 ** 9044 ** Store the maximum page count after the change in register P2. 9045 */ 9046 case OP_MaxPgcnt: { /* out2 */ 9047 unsigned int newMax; 9048 Btree *pBt; 9049 9050 pOut = out2Prerelease(p, pOp); 9051 pBt = db->aDb[pOp->p1].pBt; 9052 newMax = 0; 9053 if( pOp->p3 ){ 9054 newMax = sqlite3BtreeLastPage(pBt); 9055 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; 9056 } 9057 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); 9058 break; 9059 } 9060 #endif 9061 9062 /* Opcode: Function P1 P2 P3 P4 * 9063 ** Synopsis: r[P3]=func(r[P2@NP]) 9064 ** 9065 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that 9066 ** contains a pointer to the function to be run) with arguments taken 9067 ** from register P2 and successors. The number of arguments is in 9068 ** the sqlite3_context object that P4 points to. 9069 ** The result of the function is stored 9070 ** in register P3. Register P3 must not be one of the function inputs. 9071 ** 9072 ** P1 is a 32-bit bitmask indicating whether or not each argument to the 9073 ** function was determined to be constant at compile time. If the first 9074 ** argument was constant then bit 0 of P1 is set. This is used to determine 9075 ** whether meta data associated with a user function argument using the 9076 ** sqlite3_set_auxdata() API may be safely retained until the next 9077 ** invocation of this opcode. 9078 ** 9079 ** See also: AggStep, AggFinal, PureFunc 9080 */ 9081 /* Opcode: PureFunc P1 P2 P3 P4 * 9082 ** Synopsis: r[P3]=func(r[P2@NP]) 9083 ** 9084 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that 9085 ** contains a pointer to the function to be run) with arguments taken 9086 ** from register P2 and successors. The number of arguments is in 9087 ** the sqlite3_context object that P4 points to. 9088 ** The result of the function is stored 9089 ** in register P3. Register P3 must not be one of the function inputs. 9090 ** 9091 ** P1 is a 32-bit bitmask indicating whether or not each argument to the 9092 ** function was determined to be constant at compile time. If the first 9093 ** argument was constant then bit 0 of P1 is set. This is used to determine 9094 ** whether meta data associated with a user function argument using the 9095 ** sqlite3_set_auxdata() API may be safely retained until the next 9096 ** invocation of this opcode. 9097 ** 9098 ** This opcode works exactly like OP_Function. The only difference is in 9099 ** its name. This opcode is used in places where the function must be 9100 ** purely non-deterministic. Some built-in date/time functions can be 9101 ** either deterministic of non-deterministic, depending on their arguments. 9102 ** When those function are used in a non-deterministic way, they will check 9103 ** to see if they were called using OP_PureFunc instead of OP_Function, and 9104 ** if they were, they throw an error. 9105 ** 9106 ** See also: AggStep, AggFinal, Function 9107 */ 9108 case OP_PureFunc: /* group */ 9109 case OP_Function: { /* group */ 9110 int i; 9111 sqlite3_context *pCtx; 9112 9113 assert( pOp->p4type==P4_FUNCCTX ); 9114 pCtx = pOp->p4.pCtx; 9115 9116 /* If this function is inside of a trigger, the register array in aMem[] 9117 ** might change from one evaluation to the next. The next block of code 9118 ** checks to see if the register array has changed, and if so it 9119 ** reinitializes the relevant parts of the sqlite3_context object */ 9120 pOut = &aMem[pOp->p3]; 9121 if( pCtx->pOut != pOut ){ 9122 pCtx->pVdbe = p; 9123 pCtx->pOut = pOut; 9124 pCtx->enc = encoding; 9125 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; 9126 } 9127 assert( pCtx->pVdbe==p ); 9128 9129 memAboutToChange(p, pOut); 9130 #ifdef SQLITE_DEBUG 9131 for(i=0; iargc; i++){ 9132 assert( memIsValid(pCtx->argv[i]) ); 9133 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); 9134 } 9135 #endif 9136 MemSetTypeFlag(pOut, MEM_Null); 9137 assert( pCtx->isError==0 ); 9138 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ 9139 9140 /* If the function returned an error, throw an exception */ 9141 if( pCtx->isError ){ 9142 if( pCtx->isError>0 ){ 9143 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut)); 9144 rc = pCtx->isError; 9145 } 9146 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1); 9147 pCtx->isError = 0; 9148 if( rc ) goto abort_due_to_error; 9149 } 9150 9151 assert( (pOut->flags&MEM_Str)==0 9152 || pOut->enc==encoding 9153 || db->mallocFailed ); 9154 assert( !sqlite3VdbeMemTooBig(pOut) ); 9155 9156 REGISTER_TRACE(pOp->p3, pOut); 9157 UPDATE_MAX_BLOBSIZE(pOut); 9158 break; 9159 } 9160 9161 /* Opcode: ClrSubtype P1 * * * * 9162 ** Synopsis: r[P1].subtype = 0 9163 ** 9164 ** Clear the subtype from register P1. 9165 */ 9166 case OP_ClrSubtype: { /* in1 */ 9167 pIn1 = &aMem[pOp->p1]; 9168 pIn1->flags &= ~MEM_Subtype; 9169 break; 9170 } 9171 9172 /* Opcode: GetSubtype P1 P2 * * * 9173 ** Synopsis: r[P2] = r[P1].subtype 9174 ** 9175 ** Extract the subtype value from register P1 and write that subtype 9176 ** into register P2. If P1 has no subtype, then P1 gets a NULL. 9177 */ 9178 case OP_GetSubtype: { /* in1 out2 */ 9179 pIn1 = &aMem[pOp->p1]; 9180 pOut = &aMem[pOp->p2]; 9181 if( pIn1->flags & MEM_Subtype ){ 9182 sqlite3VdbeMemSetInt64(pOut, pIn1->eSubtype); 9183 }else{ 9184 sqlite3VdbeMemSetNull(pOut); 9185 } 9186 break; 9187 } 9188 9189 /* Opcode: SetSubtype P1 P2 * * * 9190 ** Synopsis: r[P2].subtype = r[P1] 9191 ** 9192 ** Set the subtype value of register P2 to the integer from register P1. 9193 ** If P1 is NULL, clear the subtype from p2. 9194 */ 9195 case OP_SetSubtype: { /* in1 out2 */ 9196 pIn1 = &aMem[pOp->p1]; 9197 pOut = &aMem[pOp->p2]; 9198 if( pIn1->flags & MEM_Null ){ 9199 pOut->flags &= ~MEM_Subtype; 9200 }else{ 9201 assert( pIn1->flags & MEM_Int ); 9202 pOut->flags |= MEM_Subtype; 9203 pOut->eSubtype = (u8)(pIn1->u.i & 0xff); 9204 } 9205 break; 9206 } 9207 9208 /* Opcode: FilterAdd P1 * P3 P4 * 9209 ** Synopsis: filter(P1) += key(P3@P4) 9210 ** 9211 ** Compute a hash on the P4 registers starting with r[P3] and 9212 ** add that hash to the bloom filter contained in r[P1]. 9213 */ 9214 case OP_FilterAdd: { 9215 u64 h; 9216 9217 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 9218 pIn1 = &aMem[pOp->p1]; 9219 assert( pIn1->flags & MEM_Blob ); 9220 assert( pIn1->n>0 ); 9221 h = filterHash(aMem, pOp); 9222 #ifdef SQLITE_DEBUG 9223 if( db->flags&SQLITE_VdbeTrace ){ 9224 int ii; 9225 for(ii=pOp->p3; iip3+pOp->p4.i; ii++){ 9226 registerTrace(ii, &aMem[ii]); 9227 } 9228 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n)); 9229 } 9230 #endif 9231 h %= (pIn1->n*8); 9232 pIn1->z[h/8] |= 1<<(h&7); 9233 break; 9234 } 9235 9236 /* Opcode: Filter P1 P2 P3 P4 * 9237 ** Synopsis: if key(P3@P4) not in filter(P1) goto P2 9238 ** 9239 ** Compute a hash on the key contained in the P4 registers starting 9240 ** with r[P3]. Check to see if that hash is found in the 9241 ** bloom filter hosted by register P1. If it is not present then 9242 ** maybe jump to P2. Otherwise fall through. 9243 ** 9244 ** False negatives are harmless. It is always safe to fall through, 9245 ** even if the value is in the bloom filter. A false negative causes 9246 ** more CPU cycles to be used, but it should still yield the correct 9247 ** answer. However, an incorrect answer may well arise from a 9248 ** false positive - if the jump is taken when it should fall through. 9249 */ 9250 case OP_Filter: { /* jump */ 9251 u64 h; 9252 9253 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 9254 pIn1 = &aMem[pOp->p1]; 9255 assert( (pIn1->flags & MEM_Blob)!=0 ); 9256 assert( pIn1->n >= 1 ); 9257 h = filterHash(aMem, pOp); 9258 #ifdef SQLITE_DEBUG 9259 if( db->flags&SQLITE_VdbeTrace ){ 9260 int ii; 9261 for(ii=pOp->p3; iip3+pOp->p4.i; ii++){ 9262 registerTrace(ii, &aMem[ii]); 9263 } 9264 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n)); 9265 } 9266 #endif 9267 h %= (pIn1->n*8); 9268 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){ 9269 VdbeBranchTaken(1, 2); 9270 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++; 9271 goto jump_to_p2; 9272 }else{ 9273 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++; 9274 VdbeBranchTaken(0, 2); 9275 } 9276 break; 9277 } 9278 9279 /* Opcode: Trace P1 P2 * P4 * 9280 ** 9281 ** Write P4 on the statement trace output if statement tracing is 9282 ** enabled. 9283 ** 9284 ** Operand P1 must be 0x7fffffff and P2 must positive. 9285 */ 9286 /* Opcode: Init P1 P2 P3 P4 * 9287 ** Synopsis: Start at P2 9288 ** 9289 ** Programs contain a single instance of this opcode as the very first 9290 ** opcode. 9291 ** 9292 ** If tracing is enabled (by the sqlite3_trace()) interface, then 9293 ** the UTF-8 string contained in P4 is emitted on the trace callback. 9294 ** Or if P4 is blank, use the string returned by sqlite3_sql(). 9295 ** 9296 ** If P2 is not zero, jump to instruction P2. 9297 ** 9298 ** Increment the value of P1 so that OP_Once opcodes will jump the 9299 ** first time they are evaluated for this run. 9300 ** 9301 ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT 9302 ** error is encountered. 9303 */ 9304 case OP_Trace: 9305 case OP_Init: { /* jump0 */ 9306 int i; 9307 #ifndef SQLITE_OMIT_TRACE 9308 char *zTrace; 9309 #endif 9310 9311 /* If the P4 argument is not NULL, then it must be an SQL comment string. 9312 ** The "--" string is broken up to prevent false-positives with srcck1.c. 9313 ** 9314 ** This assert() provides evidence for: 9315 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that 9316 ** would have been returned by the legacy sqlite3_trace() interface by 9317 ** using the X argument when X begins with "--" and invoking 9318 ** sqlite3_expanded_sql(P) otherwise. 9319 */ 9320 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 ); 9321 9322 /* OP_Init is always instruction 0 */ 9323 assert( pOp==p->aOp || pOp->opcode==OP_Trace ); 9324 9325 #ifndef SQLITE_OMIT_TRACE 9326 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0 9327 && p->minWriteFileFormat!=254 /* tag-`20220401a` */ 9328 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 9329 ){ 9330 #ifndef SQLITE_OMIT_DEPRECATED 9331 if( db->mTrace & SQLITE_TRACE_LEGACY ){ 9332 char *z = sqlite3VdbeExpandSql(p, zTrace); 9333 db->trace.xLegacy(db->pTraceArg, z); 9334 sqlite3_free(z); 9335 }else 9336 #endif 9337 if( db->nVdbeExec>1 ){ 9338 char *z = sqlite3MPrintf(db, "-- %s", zTrace); 9339 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z); 9340 sqlite3DbFree(db, z); 9341 }else{ 9342 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace); 9343 } 9344 } 9345 #ifdef SQLITE_USE_FCNTL_TRACE 9346 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); 9347 if( zTrace ){ 9348 int j; 9349 for(j=0; jnDb; j++){ 9350 if( DbMaskTest(p->btreeMask, j)==0 ) continue; 9351 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace); 9352 } 9353 } 9354 #endif /* SQLITE_USE_FCNTL_TRACE */ 9355 #ifdef SQLITE_DEBUG 9356 if( (db->flags & SQLITE_SqlTrace)!=0 9357 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 9358 ){ 9359 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); 9360 } 9361 #endif /* SQLITE_DEBUG */ 9362 #endif /* SQLITE_OMIT_TRACE */ 9363 assert( pOp->p2>0 ); 9364 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){ 9365 if( pOp->opcode==OP_Trace ) break; 9366 for(i=1; inOp; i++){ 9367 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0; 9368 } 9369 pOp->p1 = 0; 9370 } 9371 pOp->p1++; 9372 p->aCounter[SQLITE_STMTSTATUS_RUN]++; 9373 goto jump_to_p2; 9374 } 9375 9376 #ifdef SQLITE_ENABLE_CURSOR_HINTS 9377 /* Opcode: CursorHint P1 * P3 P4 * 9378 ** 9379 ** Provide a hint to cursor P1. 9380 ** 9381 ** If P4 is of type P4_EXPR, then the hint is that the cursor need only return 9382 ** rows that satisfy the Expr in P4. TK_REGISTER terms in the P4 expression 9383 ** refer to values currently held in registers. TK_COLUMN terms in the P4 9384 ** expression refer to columns in the b-tree to which cursor P1 is pointing. 9385 ** P3 is ignore in this case. 9386 ** 9387 ** Or, if P4 is P4_NOTUSED, then the hint is that cursor P1 is an index cursor 9388 ** used to drive table cursor P3. In other words, that this VM may execute 9389 ** OP_DeferredSeek instructions to lazily position P3 based on current 9390 ** position of P1. 9391 */ 9392 case OP_CursorHint: { 9393 VdbeCursor *pC; 9394 pC = p->apCsr[pOp->p1]; 9395 9396 assert( pOp->p1>=0 && pOp->p1nCursor ); 9397 9398 if( pC ){ 9399 assert( pC->eCurType==CURTYPE_BTREE ); 9400 if( pOp->p4type==P4_EXPR ){ 9401 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE, 9402 pOp->p4.pExpr, aMem); 9403 }else if( p->apCsr[pOp->p3] ){ 9404 sqlite3BtreeCursorHint( 9405 pC->uc.pCursor, BTREE_HINT_TABLECURSOR, p->apCsr[pOp->p3]->uc.pCursor 9406 ); 9407 } 9408 } 9409 break; 9410 } 9411 #endif /* SQLITE_ENABLE_CURSOR_HINTS */ 9412 9413 #ifdef SQLITE_DEBUG 9414 /* Opcode: Abortable * * * * * 9415 ** 9416 ** Verify that an Abort can happen. Assert if an Abort at this point 9417 ** might cause database corruption. This opcode only appears in debugging 9418 ** builds. 9419 ** 9420 ** An Abort is safe if either there have been no writes, or if there is 9421 ** an active statement journal. 9422 */ 9423 case OP_Abortable: { 9424 sqlite3VdbeAssertAbortable(p); 9425 break; 9426 } 9427 #endif 9428 9429 #ifdef SQLITE_DEBUG 9430 /* Opcode: ReleaseReg P1 P2 P3 * P5 9431 ** Synopsis: release r[P1@P2] mask P3 9432 ** 9433 ** Release registers from service. Any content that was in the 9434 ** the registers is unreliable after this opcode completes. 9435 ** 9436 ** The registers released will be the P2 registers starting at P1, 9437 ** except if bit ii of P3 set, then do not release register P1+ii. 9438 ** In other words, P3 is a mask of registers to preserve. 9439 ** 9440 ** Releasing a register clears the Mem.pScopyFrom pointer. That means 9441 ** that if the content of the released register was set using OP_SCopy, 9442 ** a change to the value of the source register for the OP_SCopy will no longer 9443 ** generate an assertion fault in sqlite3VdbeMemAboutToChange(). 9444 ** 9445 ** If P5 is set, then all released registers have their type set 9446 ** to MEM_Undefined so that any subsequent attempt to read the released 9447 ** register (before it is reinitialized) will generate an assertion fault. 9448 ** 9449 ** P5 ought to be set on every call to this opcode. 9450 ** However, there are places in the code generator will release registers 9451 ** before their are used, under the (valid) assumption that the registers 9452 ** will not be reallocated for some other purpose before they are used and 9453 ** hence are safe to release. 9454 ** 9455 ** This opcode is only available in testing and debugging builds. It is 9456 ** not generated for release builds. The purpose of this opcode is to help 9457 ** validate the generated bytecode. This opcode does not actually contribute 9458 ** to computing an answer. 9459 */ 9460 case OP_ReleaseReg: { 9461 Mem *pMem; 9462 int i; 9463 u32 constMask; 9464 assert( pOp->p1>0 ); 9465 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); 9466 pMem = &aMem[pOp->p1]; 9467 constMask = pOp->p3; 9468 for(i=0; ip2; i++, pMem++){ 9469 if( i>=32 || (constMask & MASKBIT32(i))==0 ){ 9470 pMem->pScopyFrom = 0; 9471 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined); 9472 } 9473 } 9474 break; 9475 } 9476 #endif 9477 9478 /* Opcode: Noop * * * * * 9479 ** 9480 ** Do nothing. Continue downward to the next opcode. 9481 */ 9482 /* Opcode: Explain P1 P2 P3 P4 * 9483 ** 9484 ** This is the same as OP_Noop during normal query execution. The 9485 ** purpose of this opcode is to hold information about the query 9486 ** plan for the purpose of EXPLAIN QUERY PLAN output. 9487 ** 9488 ** The P4 value is human-readable text that describes the query plan 9489 ** element. Something like "SCAN t1" or "SEARCH t2 USING INDEX t2x1". 9490 ** 9491 ** The P1 value is the ID of the current element and P2 is the parent 9492 ** element for the case of nested query plan elements. If P2 is zero 9493 ** then this element is a top-level element. 9494 ** 9495 ** For loop elements, P3 is the estimated code of each invocation of this 9496 ** element. 9497 ** 9498 ** As with all opcodes, the meanings of the parameters for OP_Explain 9499 ** are subject to change from one release to the next. Applications 9500 ** should not attempt to interpret or use any of the information 9501 ** contained in the OP_Explain opcode. The information provided by this 9502 ** opcode is intended for testing and debugging use only. 9503 */ 9504 default: { /* This is really OP_Noop, OP_Explain */ 9505 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); 9506 9507 break; 9508 } 9509 9510 /***************************************************************************** 9511 ** The cases of the switch statement above this line should all be indented 9512 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the 9513 ** readability. From this point on down, the normal indentation rules are 9514 ** restored. 9515 *****************************************************************************/ 9516 } 9517 9518 #if defined(VDBE_PROFILE) 9519 *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); 9520 pnCycle = 0; 9521 #elif defined(SQLITE_ENABLE_STMT_SCANSTATUS) 9522 if( pnCycle ){ 9523 *pnCycle += sqlite3Hwtime(); 9524 pnCycle = 0; 9525 } 9526 #endif 9527 9528 /* The following code adds nothing to the actual functionality 9529 ** of the program. It is only here for testing and debugging. 9530 ** On the other hand, it does burn CPU cycles every time through 9531 ** the evaluator loop. So we can leave it out when NDEBUG is defined. 9532 */ 9533 #ifndef NDEBUG 9534 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] ); 9535 9536 #ifdef SQLITE_DEBUG 9537 if( db->flags & SQLITE_VdbeTrace ){ 9538 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode]; 9539 if( rc!=0 ) printf("rc=%d\n",rc); 9540 if( opProperty & (OPFLG_OUT2) ){ 9541 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); 9542 } 9543 if( opProperty & OPFLG_OUT3 ){ 9544 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); 9545 } 9546 if( opProperty==0xff ){ 9547 /* Never happens. This code exists to avoid a harmless linkage 9548 ** warning about sqlite3VdbeRegisterDump() being defined but not 9549 ** used. */ 9550 sqlite3VdbeRegisterDump(p); 9551 } 9552 } 9553 #endif /* SQLITE_DEBUG */ 9554 #endif /* NDEBUG */ 9555 } /* The end of the for(;;) loop the loops through opcodes */ 9556 9557 /* If we reach this point, it means that execution is finished with 9558 ** an error of some kind. 9559 */ 9560 abort_due_to_error: 9561 if( db->mallocFailed ){ 9562 rc = SQLITE_NOMEM_BKPT; 9563 }else if( rc==SQLITE_IOERR_CORRUPTFS ){ 9564 rc = SQLITE_CORRUPT_BKPT; 9565 } 9566 assert( rc ); 9567 #ifdef SQLITE_DEBUG 9568 if( db->flags & SQLITE_VdbeTrace ){ 9569 const char *zTrace = p->zSql; 9570 if( zTrace==0 ){ 9571 if( aOp[0].opcode==OP_Trace ){ 9572 zTrace = aOp[0].p4.z; 9573 } 9574 if( zTrace==0 ) zTrace = "???"; 9575 } 9576 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace); 9577 } 9578 #endif 9579 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){ 9580 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); 9581 } 9582 p->rc = rc; 9583 sqlite3SystemError(db, rc); 9584 testcase( sqlite3GlobalConfig.xLog!=0 ); 9585 sqlite3VdbeLogAbort(p, rc, pOp, aOp); 9586 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p); 9587 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db); 9588 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){ 9589 db->flags |= SQLITE_CorruptRdOnly; 9590 } 9591 rc = SQLITE_ERROR; 9592 if( resetSchemaOnFault>0 ){ 9593 sqlite3ResetOneSchema(db, resetSchemaOnFault-1, p); 9594 } 9595 9596 /* This is the only way out of this procedure. We have to 9597 ** release the mutexes on btrees that were acquired at the 9598 ** top. */ 9599 vdbe_return: 9600 #if defined(VDBE_PROFILE) 9601 if( pnCycle ){ 9602 *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); 9603 pnCycle = 0; 9604 } 9605 #elif defined(SQLITE_ENABLE_STMT_SCANSTATUS) 9606 if( pnCycle ){ 9607 *pnCycle += sqlite3Hwtime(); 9608 pnCycle = 0; 9609 } 9610 #endif 9611 9612 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 9613 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){ 9614 nProgressLimit += db->nProgressOps; 9615 if( db->xProgress(db->pProgressArg) ){ 9616 nProgressLimit = LARGEST_UINT64; 9617 rc = SQLITE_INTERRUPT; 9618 goto abort_due_to_error; 9619 } 9620 } 9621 #endif 9622 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; 9623 if( DbMaskNonZero(p->lockMask) ){ 9624 sqlite3VdbeLeave(p); 9625 } 9626 assert( rc!=SQLITE_OK || nExtraDelete==0 9627 || sqlite3_strlike("DELETE%",p->zSql,0)!=0 9628 ); 9629 return rc; 9630 9631 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH 9632 ** is encountered. 9633 */ 9634 too_big: 9635 sqlite3VdbeError(p, "string or blob too big"); 9636 rc = SQLITE_TOOBIG; 9637 goto abort_due_to_error; 9638 9639 /* Jump to here if a malloc() fails. 9640 */ 9641 no_mem: 9642 sqlite3OomFault(db); 9643 sqlite3VdbeError(p, "out of memory"); 9644 rc = SQLITE_NOMEM_BKPT; 9645 goto abort_due_to_error; 9646 9647 /* Jump to here if the sqlite3_interrupt() API sets the interrupt 9648 ** flag. 9649 */ 9650 abort_due_to_interrupt: 9651 assert( AtomicLoad(&db->u1.isInterrupted) ); 9652 rc = SQLITE_INTERRUPT; 9653 goto abort_due_to_error; 9654 9655 abort_due_to_expired: 9656 rc = SQLITE_SCHEMA; 9657 goto abort_due_to_error; 9658 }