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 ** Invoke this macro on memory cells just prior to changing the 26 ** value of the cell. This macro verifies that shallow copies are 27 ** not misused. A shallow copy of a string or blob just copies a 28 ** pointer to the string or blob, not the content. If the original 29 ** is changed while the copy is still in use, the string or blob might 30 ** be changed out from under the copy. This macro verifies that nothing 31 ** like that ever happens. 32 */ 33 #ifdef SQLITE_DEBUG 34 # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M) 35 #else 36 # define memAboutToChange(P,M) 37 #endif 38 39 /* 40 ** The following global variable is incremented every time a cursor 41 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test 42 ** procedures use this information to make sure that indices are 43 ** working correctly. This variable has no function other than to 44 ** help verify the correct operation of the library. 45 */ 46 #ifdef SQLITE_TEST 47 int sqlite3_search_count = 0; 48 #endif 49 50 /* 51 ** When this global variable is positive, it gets decremented once before 52 ** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted 53 ** field of the sqlite3 structure is set in order to simulate an interrupt. 54 ** 55 ** This facility is used for testing purposes only. It does not function 56 ** in an ordinary build. 57 */ 58 #ifdef SQLITE_TEST 59 int sqlite3_interrupt_count = 0; 60 #endif 61 62 /* 63 ** The next global variable is incremented each type the OP_Sort opcode 64 ** is executed. The test procedures use this information to make sure that 65 ** sorting is occurring or not occurring at appropriate times. This variable 66 ** has no function other than to help verify the correct operation of the 67 ** library. 68 */ 69 #ifdef SQLITE_TEST 70 int sqlite3_sort_count = 0; 71 #endif 72 73 /* 74 ** The next global variable records the size of the largest MEM_Blob 75 ** or MEM_Str that has been used by a VDBE opcode. The test procedures 76 ** use this information to make sure that the zero-blob functionality 77 ** is working correctly. This variable has no function other than to 78 ** help verify the correct operation of the library. 79 */ 80 #ifdef SQLITE_TEST 81 int sqlite3_max_blobsize = 0; 82 static void updateMaxBlobsize(Mem *p){ 83 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ 84 sqlite3_max_blobsize = p->n; 85 } 86 } 87 #endif 88 89 /* 90 ** This macro evaluates to true if either the update hook or the preupdate 91 ** hook are enabled for database connect DB. 92 */ 93 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 94 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) 95 #else 96 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback) 97 #endif 98 99 /* 100 ** The next global variable is incremented each time the OP_Found opcode 101 ** is executed. This is used to test whether or not the foreign key 102 ** operation implemented using OP_FkIsZero is working. This variable 103 ** has no function other than to help verify the correct operation of the 104 ** library. 105 */ 106 #ifdef SQLITE_TEST 107 int sqlite3_found_count = 0; 108 #endif 109 110 /* 111 ** Test a register to see if it exceeds the current maximum blob size. 112 ** If it does, record the new maximum blob size. 113 */ 114 #if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE) 115 # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) 116 #else 117 # define UPDATE_MAX_BLOBSIZE(P) 118 #endif 119 120 /* 121 ** Invoke the VDBE coverage callback, if that callback is defined. This 122 ** feature is used for test suite validation only and does not appear an 123 ** production builds. 124 ** 125 ** M is an integer, 2 or 3, that indices how many different ways the 126 ** branch can go. It is usually 2. "I" is the direction the branch 127 ** goes. 0 means falls through. 1 means branch is taken. 2 means the 128 ** second alternative branch is taken. 129 ** 130 ** iSrcLine is the source code line (from the __LINE__ macro) that 131 ** generated the VDBE instruction. This instrumentation assumes that all 132 ** source code is in a single file (the amalgamation). Special values 1 133 ** and 2 for the iSrcLine parameter mean that this particular branch is 134 ** always taken or never taken, respectively. 135 */ 136 #if !defined(SQLITE_VDBE_COVERAGE) 137 # define VdbeBranchTaken(I,M) 138 #else 139 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M) 140 static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){ 141 if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){ 142 M = iSrcLine; 143 /* Assert the truth of VdbeCoverageAlwaysTaken() and 144 ** VdbeCoverageNeverTaken() */ 145 assert( (M & I)==I ); 146 }else{ 147 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/ 148 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, 149 iSrcLine,I,M); 150 } 151 } 152 #endif 153 154 /* 155 ** Convert the given register into a string if it isn't one 156 ** already. Return non-zero if a malloc() fails. 157 */ 158 #define Stringify(P, enc) \ 159 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \ 160 { goto no_mem; } 161 162 /* 163 ** An ephemeral string value (signified by the MEM_Ephem flag) contains 164 ** a pointer to a dynamically allocated string where some other entity 165 ** is responsible for deallocating that string. Because the register 166 ** does not control the string, it might be deleted without the register 167 ** knowing it. 168 ** 169 ** This routine converts an ephemeral string into a dynamically allocated 170 ** string that the register itself controls. In other words, it 171 ** converts an MEM_Ephem string into a string with P.z==P.zMalloc. 172 */ 173 #define Deephemeralize(P) \ 174 if( ((P)->flags&MEM_Ephem)!=0 \ 175 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} 176 177 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */ 178 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER) 179 180 /* 181 ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL 182 ** if we run out of memory. 183 */ 184 static VdbeCursor *allocateCursor( 185 Vdbe *p, /* The virtual machine */ 186 int iCur, /* Index of the new VdbeCursor */ 187 int nField, /* Number of fields in the table or index */ 188 int iDb, /* Database the cursor belongs to, or -1 */ 189 u8 eCurType /* Type of the new cursor */ 190 ){ 191 /* Find the memory cell that will be used to store the blob of memory 192 ** required for this VdbeCursor structure. It is convenient to use a 193 ** vdbe memory cell to manage the memory allocation required for a 194 ** VdbeCursor structure for the following reasons: 195 ** 196 ** * Sometimes cursor numbers are used for a couple of different 197 ** purposes in a vdbe program. The different uses might require 198 ** different sized allocations. Memory cells provide growable 199 ** allocations. 200 ** 201 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can 202 ** be freed lazily via the sqlite3_release_memory() API. This 203 ** minimizes the number of malloc calls made by the system. 204 ** 205 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from 206 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1]. 207 ** Cursor 2 is at Mem[p->nMem-2]. And so forth. 208 */ 209 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem; 210 211 int nByte; 212 VdbeCursor *pCx = 0; 213 nByte = 214 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + 215 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0); 216 217 assert( iCur>=0 && iCur<p->nCursor ); 218 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/ 219 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); 220 p->apCsr[iCur] = 0; 221 } 222 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){ 223 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; 224 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor)); 225 pCx->eCurType = eCurType; 226 pCx->iDb = iDb; 227 pCx->nField = nField; 228 pCx->aOffset = &pCx->aType[nField]; 229 if( eCurType==CURTYPE_BTREE ){ 230 pCx->uc.pCursor = (BtCursor*) 231 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField]; 232 sqlite3BtreeCursorZero(pCx->uc.pCursor); 233 } 234 } 235 return pCx; 236 } 237 238 /* 239 ** Try to convert a value into a numeric representation if we can 240 ** do so without loss of information. In other words, if the string 241 ** looks like a number, convert it into a number. If it does not 242 ** look like a number, leave it alone. 243 ** 244 ** If the bTryForInt flag is true, then extra effort is made to give 245 ** an integer representation. Strings that look like floating point 246 ** values but which have no fractional component (example: '48.00') 247 ** will have a MEM_Int representation when bTryForInt is true. 248 ** 249 ** If bTryForInt is false, then if the input string contains a decimal 250 ** point or exponential notation, the result is only MEM_Real, even 251 ** if there is an exact integer representation of the quantity. 252 */ 253 static void applyNumericAffinity(Mem *pRec, int bTryForInt){ 254 double rValue; 255 i64 iValue; 256 u8 enc = pRec->enc; 257 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str ); 258 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return; 259 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){ 260 pRec->u.i = iValue; 261 pRec->flags |= MEM_Int; 262 }else{ 263 pRec->u.r = rValue; 264 pRec->flags |= MEM_Real; 265 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec); 266 } 267 } 268 269 /* 270 ** Processing is determine by the affinity parameter: 271 ** 272 ** SQLITE_AFF_INTEGER: 273 ** SQLITE_AFF_REAL: 274 ** SQLITE_AFF_NUMERIC: 275 ** Try to convert pRec to an integer representation or a 276 ** floating-point representation if an integer representation 277 ** is not possible. Note that the integer representation is 278 ** always preferred, even if the affinity is REAL, because 279 ** an integer representation is more space efficient on disk. 280 ** 281 ** SQLITE_AFF_TEXT: 282 ** Convert pRec to a text representation. 283 ** 284 ** SQLITE_AFF_BLOB: 285 ** No-op. pRec is unchanged. 286 */ 287 static void applyAffinity( 288 Mem *pRec, /* The value to apply affinity to */ 289 char affinity, /* The affinity to be applied */ 290 u8 enc /* Use this text encoding */ 291 ){ 292 if( affinity>=SQLITE_AFF_NUMERIC ){ 293 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL 294 || affinity==SQLITE_AFF_NUMERIC ); 295 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/ 296 if( (pRec->flags & MEM_Real)==0 ){ 297 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); 298 }else{ 299 sqlite3VdbeIntegerAffinity(pRec); 300 } 301 } 302 }else if( affinity==SQLITE_AFF_TEXT ){ 303 /* Only attempt the conversion to TEXT if there is an integer or real 304 ** representation (blob and NULL do not get converted) but no string 305 ** representation. It would be harmless to repeat the conversion if 306 ** there is already a string rep, but it is pointless to waste those 307 ** CPU cycles. */ 308 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/ 309 if( (pRec->flags&(MEM_Real|MEM_Int)) ){ 310 sqlite3VdbeMemStringify(pRec, enc, 1); 311 } 312 } 313 pRec->flags &= ~(MEM_Real|MEM_Int); 314 } 315 } 316 317 /* 318 ** Try to convert the type of a function argument or a result column 319 ** into a numeric representation. Use either INTEGER or REAL whichever 320 ** is appropriate. But only do the conversion if it is possible without 321 ** loss of information and return the revised type of the argument. 322 */ 323 int sqlite3_value_numeric_type(sqlite3_value *pVal){ 324 int eType = sqlite3_value_type(pVal); 325 if( eType==SQLITE_TEXT ){ 326 Mem *pMem = (Mem*)pVal; 327 applyNumericAffinity(pMem, 0); 328 eType = sqlite3_value_type(pVal); 329 } 330 return eType; 331 } 332 333 /* 334 ** Exported version of applyAffinity(). This one works on sqlite3_value*, 335 ** not the internal Mem* type. 336 */ 337 void sqlite3ValueApplyAffinity( 338 sqlite3_value *pVal, 339 u8 affinity, 340 u8 enc 341 ){ 342 applyAffinity((Mem *)pVal, affinity, enc); 343 } 344 345 /* 346 ** pMem currently only holds a string type (or maybe a BLOB that we can 347 ** interpret as a string if we want to). Compute its corresponding 348 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields 349 ** accordingly. 350 */ 351 static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ 352 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 ); 353 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); 354 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){ 355 return 0; 356 } 357 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){ 358 return MEM_Int; 359 } 360 return MEM_Real; 361 } 362 363 /* 364 ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or 365 ** none. 366 ** 367 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. 368 ** But it does set pMem->u.r and pMem->u.i appropriately. 369 */ 370 static u16 numericType(Mem *pMem){ 371 if( pMem->flags & (MEM_Int|MEM_Real) ){ 372 return pMem->flags & (MEM_Int|MEM_Real); 373 } 374 if( pMem->flags & (MEM_Str|MEM_Blob) ){ 375 return computeNumericType(pMem); 376 } 377 return 0; 378 } 379 380 #ifdef SQLITE_DEBUG 381 /* 382 ** Write a nice string representation of the contents of cell pMem 383 ** into buffer zBuf, length nBuf. 384 */ 385 void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ 386 char *zCsr = zBuf; 387 int f = pMem->flags; 388 389 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; 390 391 if( f&MEM_Blob ){ 392 int i; 393 char c; 394 if( f & MEM_Dyn ){ 395 c = 'z'; 396 assert( (f & (MEM_Static|MEM_Ephem))==0 ); 397 }else if( f & MEM_Static ){ 398 c = 't'; 399 assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); 400 }else if( f & MEM_Ephem ){ 401 c = 'e'; 402 assert( (f & (MEM_Static|MEM_Dyn))==0 ); 403 }else{ 404 c = 's'; 405 } 406 407 sqlite3_snprintf(100, zCsr, "%c", c); 408 zCsr += sqlite3Strlen30(zCsr); 409 sqlite3_snprintf(100, zCsr, "%d[", pMem->n); 410 zCsr += sqlite3Strlen30(zCsr); 411 for(i=0; i<16 && i<pMem->n; i++){ 412 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); 413 zCsr += sqlite3Strlen30(zCsr); 414 } 415 for(i=0; i<16 && i<pMem->n; i++){ 416 char z = pMem->z[i]; 417 if( z<32 || z>126 ) *zCsr++ = '.'; 418 else *zCsr++ = z; 419 } 420 421 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]); 422 zCsr += sqlite3Strlen30(zCsr); 423 if( f & MEM_Zero ){ 424 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero); 425 zCsr += sqlite3Strlen30(zCsr); 426 } 427 *zCsr = '\0'; 428 }else if( f & MEM_Str ){ 429 int j, k; 430 zBuf[0] = ' '; 431 if( f & MEM_Dyn ){ 432 zBuf[1] = 'z'; 433 assert( (f & (MEM_Static|MEM_Ephem))==0 ); 434 }else if( f & MEM_Static ){ 435 zBuf[1] = 't'; 436 assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); 437 }else if( f & MEM_Ephem ){ 438 zBuf[1] = 'e'; 439 assert( (f & (MEM_Static|MEM_Dyn))==0 ); 440 }else{ 441 zBuf[1] = 's'; 442 } 443 k = 2; 444 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); 445 k += sqlite3Strlen30(&zBuf[k]); 446 zBuf[k++] = '['; 447 for(j=0; j<15 && j<pMem->n; j++){ 448 u8 c = pMem->z[j]; 449 if( c>=0x20 && c<0x7f ){ 450 zBuf[k++] = c; 451 }else{ 452 zBuf[k++] = '.'; 453 } 454 } 455 zBuf[k++] = ']'; 456 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); 457 k += sqlite3Strlen30(&zBuf[k]); 458 zBuf[k++] = 0; 459 } 460 } 461 #endif 462 463 #ifdef SQLITE_DEBUG 464 /* 465 ** Print the value of a register for tracing purposes: 466 */ 467 static void memTracePrint(Mem *p){ 468 if( p->flags & MEM_Undefined ){ 469 printf(" undefined"); 470 }else if( p->flags & MEM_Null ){ 471 printf(" NULL"); 472 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ 473 printf(" si:%lld", p->u.i); 474 }else if( p->flags & MEM_Int ){ 475 printf(" i:%lld", p->u.i); 476 #ifndef SQLITE_OMIT_FLOATING_POINT 477 }else if( p->flags & MEM_Real ){ 478 printf(" r:%g", p->u.r); 479 #endif 480 }else if( p->flags & MEM_RowSet ){ 481 printf(" (rowset)"); 482 }else{ 483 char zBuf[200]; 484 sqlite3VdbeMemPrettyPrint(p, zBuf); 485 printf(" %s", zBuf); 486 } 487 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype); 488 } 489 static void registerTrace(int iReg, Mem *p){ 490 printf("REG[%d] = ", iReg); 491 memTracePrint(p); 492 printf("\n"); 493 } 494 #endif 495 496 #ifdef SQLITE_DEBUG 497 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) 498 #else 499 # define REGISTER_TRACE(R,M) 500 #endif 501 502 503 #ifdef VDBE_PROFILE 504 505 /* 506 ** hwtime.h contains inline assembler code for implementing 507 ** high-performance timing routines. 508 */ 509 #include "hwtime.h" 510 511 #endif 512 513 #ifndef NDEBUG 514 /* 515 ** This function is only called from within an assert() expression. It 516 ** checks that the sqlite3.nTransaction variable is correctly set to 517 ** the number of non-transaction savepoints currently in the 518 ** linked list starting at sqlite3.pSavepoint. 519 ** 520 ** Usage: 521 ** 522 ** assert( checkSavepointCount(db) ); 523 */ 524 static int checkSavepointCount(sqlite3 *db){ 525 int n = 0; 526 Savepoint *p; 527 for(p=db->pSavepoint; p; p=p->pNext) n++; 528 assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); 529 return 1; 530 } 531 #endif 532 533 /* 534 ** Return the register of pOp->p2 after first preparing it to be 535 ** overwritten with an integer value. 536 */ 537 static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){ 538 sqlite3VdbeMemSetNull(pOut); 539 pOut->flags = MEM_Int; 540 return pOut; 541 } 542 static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ 543 Mem *pOut; 544 assert( pOp->p2>0 ); 545 assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); 546 pOut = &p->aMem[pOp->p2]; 547 memAboutToChange(p, pOut); 548 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/ 549 return out2PrereleaseWithClear(pOut); 550 }else{ 551 pOut->flags = MEM_Int; 552 return pOut; 553 } 554 } 555 556 557 /* 558 ** Execute as much of a VDBE program as we can. 559 ** This is the core of sqlite3_step(). 560 */ 561 int sqlite3VdbeExec( 562 Vdbe *p /* The VDBE */ 563 ){ 564 Op *aOp = p->aOp; /* Copy of p->aOp */ 565 Op *pOp = aOp; /* Current operation */ 566 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) 567 Op *pOrigOp; /* Value of pOp at the top of the loop */ 568 #endif 569 #ifdef SQLITE_DEBUG 570 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */ 571 #endif 572 int rc = SQLITE_OK; /* Value to return */ 573 sqlite3 *db = p->db; /* The database */ 574 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ 575 u8 encoding = ENC(db); /* The database encoding */ 576 int iCompare = 0; /* Result of last comparison */ 577 unsigned nVmStep = 0; /* Number of virtual machine steps */ 578 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 579 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */ 580 #endif 581 Mem *aMem = p->aMem; /* Copy of p->aMem */ 582 Mem *pIn1 = 0; /* 1st input operand */ 583 Mem *pIn2 = 0; /* 2nd input operand */ 584 Mem *pIn3 = 0; /* 3rd input operand */ 585 Mem *pOut = 0; /* Output operand */ 586 #ifdef VDBE_PROFILE 587 u64 start; /* CPU clock count at start of opcode */ 588 #endif 589 /*** INSERT STACK UNION HERE ***/ 590 591 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ 592 sqlite3VdbeEnter(p); 593 if( p->rc==SQLITE_NOMEM ){ 594 /* This happens if a malloc() inside a call to sqlite3_column_text() or 595 ** sqlite3_column_text16() failed. */ 596 goto no_mem; 597 } 598 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); 599 assert( p->bIsReader || p->readOnly!=0 ); 600 p->rc = SQLITE_OK; 601 p->iCurrentTime = 0; 602 assert( p->explain==0 ); 603 p->pResultSet = 0; 604 db->busyHandler.nBusy = 0; 605 if( db->u1.isInterrupted ) goto abort_due_to_interrupt; 606 sqlite3VdbeIOTraceSql(p); 607 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 608 if( db->xProgress ){ 609 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; 610 assert( 0 < db->nProgressOps ); 611 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); 612 } 613 #endif 614 #ifdef SQLITE_DEBUG 615 sqlite3BeginBenignMalloc(); 616 if( p->pc==0 617 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0 618 ){ 619 int i; 620 int once = 1; 621 sqlite3VdbePrintSql(p); 622 if( p->db->flags & SQLITE_VdbeListing ){ 623 printf("VDBE Program Listing:\n"); 624 for(i=0; i<p->nOp; i++){ 625 sqlite3VdbePrintOp(stdout, i, &aOp[i]); 626 } 627 } 628 if( p->db->flags & SQLITE_VdbeEQP ){ 629 for(i=0; i<p->nOp; i++){ 630 if( aOp[i].opcode==OP_Explain ){ 631 if( once ) printf("VDBE Query Plan:\n"); 632 printf("%s\n", aOp[i].p4.z); 633 once = 0; 634 } 635 } 636 } 637 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); 638 } 639 sqlite3EndBenignMalloc(); 640 #endif 641 for(pOp=&aOp[p->pc]; 1; pOp++){ 642 /* Errors are detected by individual opcodes, with an immediate 643 ** jumps to abort_due_to_error. */ 644 assert( rc==SQLITE_OK ); 645 646 assert( pOp>=aOp && pOp<&aOp[p->nOp]); 647 #ifdef VDBE_PROFILE 648 start = sqlite3Hwtime(); 649 #endif 650 nVmStep++; 651 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 652 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++; 653 #endif 654 655 /* Only allow tracing if SQLITE_DEBUG is defined. 656 */ 657 #ifdef SQLITE_DEBUG 658 if( db->flags & SQLITE_VdbeTrace ){ 659 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp); 660 } 661 #endif 662 663 664 /* Check to see if we need to simulate an interrupt. This only happens 665 ** if we have a special test build. 666 */ 667 #ifdef SQLITE_TEST 668 if( sqlite3_interrupt_count>0 ){ 669 sqlite3_interrupt_count--; 670 if( sqlite3_interrupt_count==0 ){ 671 sqlite3_interrupt(db); 672 } 673 } 674 #endif 675 676 /* Sanity checking on other operands */ 677 #ifdef SQLITE_DEBUG 678 { 679 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode]; 680 if( (opProperty & OPFLG_IN1)!=0 ){ 681 assert( pOp->p1>0 ); 682 assert( pOp->p1<=(p->nMem+1 - p->nCursor) ); 683 assert( memIsValid(&aMem[pOp->p1]) ); 684 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); 685 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); 686 } 687 if( (opProperty & OPFLG_IN2)!=0 ){ 688 assert( pOp->p2>0 ); 689 assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); 690 assert( memIsValid(&aMem[pOp->p2]) ); 691 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); 692 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); 693 } 694 if( (opProperty & OPFLG_IN3)!=0 ){ 695 assert( pOp->p3>0 ); 696 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 697 assert( memIsValid(&aMem[pOp->p3]) ); 698 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); 699 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); 700 } 701 if( (opProperty & OPFLG_OUT2)!=0 ){ 702 assert( pOp->p2>0 ); 703 assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); 704 memAboutToChange(p, &aMem[pOp->p2]); 705 } 706 if( (opProperty & OPFLG_OUT3)!=0 ){ 707 assert( pOp->p3>0 ); 708 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 709 memAboutToChange(p, &aMem[pOp->p3]); 710 } 711 } 712 #endif 713 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) 714 pOrigOp = pOp; 715 #endif 716 717 switch( pOp->opcode ){ 718 719 /***************************************************************************** 720 ** What follows is a massive switch statement where each case implements a 721 ** separate instruction in the virtual machine. If we follow the usual 722 ** indentation conventions, each case should be indented by 6 spaces. But 723 ** that is a lot of wasted space on the left margin. So the code within 724 ** the switch statement will break with convention and be flush-left. Another 725 ** big comment (similar to this one) will mark the point in the code where 726 ** we transition back to normal indentation. 727 ** 728 ** The formatting of each case is important. The makefile for SQLite 729 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this 730 ** file looking for lines that begin with "case OP_". The opcodes.h files 731 ** will be filled with #defines that give unique integer values to each 732 ** opcode and the opcodes.c file is filled with an array of strings where 733 ** each string is the symbolic name for the corresponding opcode. If the 734 ** case statement is followed by a comment of the form "/# same as ... #/" 735 ** that comment is used to determine the particular value of the opcode. 736 ** 737 ** Other keywords in the comment that follows each case are used to 738 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. 739 ** Keywords include: in1, in2, in3, out2, out3. See 740 ** the mkopcodeh.awk script for additional information. 741 ** 742 ** Documentation about VDBE opcodes is generated by scanning this file 743 ** for lines of that contain "Opcode:". That line and all subsequent 744 ** comment lines are used in the generation of the opcode.html documentation 745 ** file. 746 ** 747 ** SUMMARY: 748 ** 749 ** Formatting is important to scripts that scan this file. 750 ** Do not deviate from the formatting style currently in use. 751 ** 752 *****************************************************************************/ 753 754 /* Opcode: Goto * P2 * * * 755 ** 756 ** An unconditional jump to address P2. 757 ** The next instruction executed will be 758 ** the one at index P2 from the beginning of 759 ** the program. 760 ** 761 ** The P1 parameter is not actually used by this opcode. However, it 762 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell 763 ** that this Goto is the bottom of a loop and that the lines from P2 down 764 ** to the current line should be indented for EXPLAIN output. 765 */ 766 case OP_Goto: { /* jump */ 767 jump_to_p2_and_check_for_interrupt: 768 pOp = &aOp[pOp->p2 - 1]; 769 770 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev, 771 ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon 772 ** completion. Check to see if sqlite3_interrupt() has been called 773 ** or if the progress callback needs to be invoked. 774 ** 775 ** This code uses unstructured "goto" statements and does not look clean. 776 ** But that is not due to sloppy coding habits. The code is written this 777 ** way for performance, to avoid having to run the interrupt and progress 778 ** checks on every opcode. This helps sqlite3_step() to run about 1.5% 779 ** faster according to "valgrind --tool=cachegrind" */ 780 check_for_interrupt: 781 if( db->u1.isInterrupted ) goto abort_due_to_interrupt; 782 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 783 /* Call the progress callback if it is configured and the required number 784 ** of VDBE ops have been executed (either since this invocation of 785 ** sqlite3VdbeExec() or since last time the progress callback was called). 786 ** If the progress callback returns non-zero, exit the virtual machine with 787 ** a return code SQLITE_ABORT. 788 */ 789 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){ 790 assert( db->nProgressOps!=0 ); 791 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps); 792 if( db->xProgress(db->pProgressArg) ){ 793 rc = SQLITE_INTERRUPT; 794 goto abort_due_to_error; 795 } 796 } 797 #endif 798 799 break; 800 } 801 802 /* Opcode: Gosub P1 P2 * * * 803 ** 804 ** Write the current address onto register P1 805 ** and then jump to address P2. 806 */ 807 case OP_Gosub: { /* jump */ 808 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 809 pIn1 = &aMem[pOp->p1]; 810 assert( VdbeMemDynamic(pIn1)==0 ); 811 memAboutToChange(p, pIn1); 812 pIn1->flags = MEM_Int; 813 pIn1->u.i = (int)(pOp-aOp); 814 REGISTER_TRACE(pOp->p1, pIn1); 815 816 /* Most jump operations do a goto to this spot in order to update 817 ** the pOp pointer. */ 818 jump_to_p2: 819 pOp = &aOp[pOp->p2 - 1]; 820 break; 821 } 822 823 /* Opcode: Return P1 * * * * 824 ** 825 ** Jump to the next instruction after the address in register P1. After 826 ** the jump, register P1 becomes undefined. 827 */ 828 case OP_Return: { /* in1 */ 829 pIn1 = &aMem[pOp->p1]; 830 assert( pIn1->flags==MEM_Int ); 831 pOp = &aOp[pIn1->u.i]; 832 pIn1->flags = MEM_Undefined; 833 break; 834 } 835 836 /* Opcode: InitCoroutine P1 P2 P3 * * 837 ** 838 ** Set up register P1 so that it will Yield to the coroutine 839 ** located at address P3. 840 ** 841 ** If P2!=0 then the coroutine implementation immediately follows 842 ** this opcode. So jump over the coroutine implementation to 843 ** address P2. 844 ** 845 ** See also: EndCoroutine 846 */ 847 case OP_InitCoroutine: { /* jump */ 848 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 849 assert( pOp->p2>=0 && pOp->p2<p->nOp ); 850 assert( pOp->p3>=0 && pOp->p3<p->nOp ); 851 pOut = &aMem[pOp->p1]; 852 assert( !VdbeMemDynamic(pOut) ); 853 pOut->u.i = pOp->p3 - 1; 854 pOut->flags = MEM_Int; 855 if( pOp->p2 ) goto jump_to_p2; 856 break; 857 } 858 859 /* Opcode: EndCoroutine P1 * * * * 860 ** 861 ** The instruction at the address in register P1 is a Yield. 862 ** Jump to the P2 parameter of that Yield. 863 ** After the jump, register P1 becomes undefined. 864 ** 865 ** See also: InitCoroutine 866 */ 867 case OP_EndCoroutine: { /* in1 */ 868 VdbeOp *pCaller; 869 pIn1 = &aMem[pOp->p1]; 870 assert( pIn1->flags==MEM_Int ); 871 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp ); 872 pCaller = &aOp[pIn1->u.i]; 873 assert( pCaller->opcode==OP_Yield ); 874 assert( pCaller->p2>=0 && pCaller->p2<p->nOp ); 875 pOp = &aOp[pCaller->p2 - 1]; 876 pIn1->flags = MEM_Undefined; 877 break; 878 } 879 880 /* Opcode: Yield P1 P2 * * * 881 ** 882 ** Swap the program counter with the value in register P1. This 883 ** has the effect of yielding to a coroutine. 884 ** 885 ** If the coroutine that is launched by this instruction ends with 886 ** Yield or Return then continue to the next instruction. But if 887 ** the coroutine launched by this instruction ends with 888 ** EndCoroutine, then jump to P2 rather than continuing with the 889 ** next instruction. 890 ** 891 ** See also: InitCoroutine 892 */ 893 case OP_Yield: { /* in1, jump */ 894 int pcDest; 895 pIn1 = &aMem[pOp->p1]; 896 assert( VdbeMemDynamic(pIn1)==0 ); 897 pIn1->flags = MEM_Int; 898 pcDest = (int)pIn1->u.i; 899 pIn1->u.i = (int)(pOp - aOp); 900 REGISTER_TRACE(pOp->p1, pIn1); 901 pOp = &aOp[pcDest]; 902 break; 903 } 904 905 /* Opcode: HaltIfNull P1 P2 P3 P4 P5 906 ** Synopsis: if r[P3]=null halt 907 ** 908 ** Check the value in register P3. If it is NULL then Halt using 909 ** parameter P1, P2, and P4 as if this were a Halt instruction. If the 910 ** value in register P3 is not NULL, then this routine is a no-op. 911 ** The P5 parameter should be 1. 912 */ 913 case OP_HaltIfNull: { /* in3 */ 914 pIn3 = &aMem[pOp->p3]; 915 if( (pIn3->flags & MEM_Null)==0 ) break; 916 /* Fall through into OP_Halt */ 917 } 918 919 /* Opcode: Halt P1 P2 * P4 P5 920 ** 921 ** Exit immediately. All open cursors, etc are closed 922 ** automatically. 923 ** 924 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), 925 ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). 926 ** For errors, it can be some other value. If P1!=0 then P2 will determine 927 ** whether or not to rollback the current transaction. Do not rollback 928 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, 929 ** then back out all changes that have occurred during this execution of the 930 ** VDBE, but do not rollback the transaction. 931 ** 932 ** If P4 is not null then it is an error message string. 933 ** 934 ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string. 935 ** 936 ** 0: (no change) 937 ** 1: NOT NULL contraint failed: P4 938 ** 2: UNIQUE constraint failed: P4 939 ** 3: CHECK constraint failed: P4 940 ** 4: FOREIGN KEY constraint failed: P4 941 ** 942 ** If P5 is not zero and P4 is NULL, then everything after the ":" is 943 ** omitted. 944 ** 945 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of 946 ** every program. So a jump past the last instruction of the program 947 ** is the same as executing Halt. 948 */ 949 case OP_Halt: { 950 VdbeFrame *pFrame; 951 int pcx; 952 953 pcx = (int)(pOp - aOp); 954 if( pOp->p1==SQLITE_OK && p->pFrame ){ 955 /* Halt the sub-program. Return control to the parent frame. */ 956 pFrame = p->pFrame; 957 p->pFrame = pFrame->pParent; 958 p->nFrame--; 959 sqlite3VdbeSetChanges(db, p->nChange); 960 pcx = sqlite3VdbeFrameRestore(pFrame); 961 if( pOp->p2==OE_Ignore ){ 962 /* Instruction pcx is the OP_Program that invoked the sub-program 963 ** currently being halted. If the p2 instruction of this OP_Halt 964 ** instruction is set to OE_Ignore, then the sub-program is throwing 965 ** an IGNORE exception. In this case jump to the address specified 966 ** as the p2 of the calling OP_Program. */ 967 pcx = p->aOp[pcx].p2-1; 968 } 969 aOp = p->aOp; 970 aMem = p->aMem; 971 pOp = &aOp[pcx]; 972 break; 973 } 974 p->rc = pOp->p1; 975 p->errorAction = (u8)pOp->p2; 976 p->pc = pcx; 977 assert( pOp->p5<=4 ); 978 if( p->rc ){ 979 if( pOp->p5 ){ 980 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", 981 "FOREIGN KEY" }; 982 testcase( pOp->p5==1 ); 983 testcase( pOp->p5==2 ); 984 testcase( pOp->p5==3 ); 985 testcase( pOp->p5==4 ); 986 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]); 987 if( pOp->p4.z ){ 988 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z); 989 } 990 }else{ 991 sqlite3VdbeError(p, "%s", pOp->p4.z); 992 } 993 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg); 994 } 995 rc = sqlite3VdbeHalt(p); 996 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); 997 if( rc==SQLITE_BUSY ){ 998 p->rc = SQLITE_BUSY; 999 }else{ 1000 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); 1001 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); 1002 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; 1003 } 1004 goto vdbe_return; 1005 } 1006 1007 /* Opcode: Integer P1 P2 * * * 1008 ** Synopsis: r[P2]=P1 1009 ** 1010 ** The 32-bit integer value P1 is written into register P2. 1011 */ 1012 case OP_Integer: { /* out2 */ 1013 pOut = out2Prerelease(p, pOp); 1014 pOut->u.i = pOp->p1; 1015 break; 1016 } 1017 1018 /* Opcode: Int64 * P2 * P4 * 1019 ** Synopsis: r[P2]=P4 1020 ** 1021 ** P4 is a pointer to a 64-bit integer value. 1022 ** Write that value into register P2. 1023 */ 1024 case OP_Int64: { /* out2 */ 1025 pOut = out2Prerelease(p, pOp); 1026 assert( pOp->p4.pI64!=0 ); 1027 pOut->u.i = *pOp->p4.pI64; 1028 break; 1029 } 1030 1031 #ifndef SQLITE_OMIT_FLOATING_POINT 1032 /* Opcode: Real * P2 * P4 * 1033 ** Synopsis: r[P2]=P4 1034 ** 1035 ** P4 is a pointer to a 64-bit floating point value. 1036 ** Write that value into register P2. 1037 */ 1038 case OP_Real: { /* same as TK_FLOAT, out2 */ 1039 pOut = out2Prerelease(p, pOp); 1040 pOut->flags = MEM_Real; 1041 assert( !sqlite3IsNaN(*pOp->p4.pReal) ); 1042 pOut->u.r = *pOp->p4.pReal; 1043 break; 1044 } 1045 #endif 1046 1047 /* Opcode: String8 * P2 * P4 * 1048 ** Synopsis: r[P2]='P4' 1049 ** 1050 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 1051 ** into a String opcode before it is executed for the first time. During 1052 ** this transformation, the length of string P4 is computed and stored 1053 ** as the P1 parameter. 1054 */ 1055 case OP_String8: { /* same as TK_STRING, out2 */ 1056 assert( pOp->p4.z!=0 ); 1057 pOut = out2Prerelease(p, pOp); 1058 pOp->opcode = OP_String; 1059 pOp->p1 = sqlite3Strlen30(pOp->p4.z); 1060 1061 #ifndef SQLITE_OMIT_UTF16 1062 if( encoding!=SQLITE_UTF8 ){ 1063 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); 1064 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG ); 1065 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; 1066 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); 1067 assert( VdbeMemDynamic(pOut)==0 ); 1068 pOut->szMalloc = 0; 1069 pOut->flags |= MEM_Static; 1070 if( pOp->p4type==P4_DYNAMIC ){ 1071 sqlite3DbFree(db, pOp->p4.z); 1072 } 1073 pOp->p4type = P4_DYNAMIC; 1074 pOp->p4.z = pOut->z; 1075 pOp->p1 = pOut->n; 1076 } 1077 testcase( rc==SQLITE_TOOBIG ); 1078 #endif 1079 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 1080 goto too_big; 1081 } 1082 assert( rc==SQLITE_OK ); 1083 /* Fall through to the next case, OP_String */ 1084 } 1085 1086 /* Opcode: String P1 P2 P3 P4 P5 1087 ** Synopsis: r[P2]='P4' (len=P1) 1088 ** 1089 ** The string value P4 of length P1 (bytes) is stored in register P2. 1090 ** 1091 ** If P3 is not zero and the content of register P3 is equal to P5, then 1092 ** the datatype of the register P2 is converted to BLOB. The content is 1093 ** the same sequence of bytes, it is merely interpreted as a BLOB instead 1094 ** of a string, as if it had been CAST. In other words: 1095 ** 1096 ** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB) 1097 */ 1098 case OP_String: { /* out2 */ 1099 assert( pOp->p4.z!=0 ); 1100 pOut = out2Prerelease(p, pOp); 1101 pOut->flags = MEM_Str|MEM_Static|MEM_Term; 1102 pOut->z = pOp->p4.z; 1103 pOut->n = pOp->p1; 1104 pOut->enc = encoding; 1105 UPDATE_MAX_BLOBSIZE(pOut); 1106 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS 1107 if( pOp->p3>0 ){ 1108 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 1109 pIn3 = &aMem[pOp->p3]; 1110 assert( pIn3->flags & MEM_Int ); 1111 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term; 1112 } 1113 #endif 1114 break; 1115 } 1116 1117 /* Opcode: Null P1 P2 P3 * * 1118 ** Synopsis: r[P2..P3]=NULL 1119 ** 1120 ** Write a NULL into registers P2. If P3 greater than P2, then also write 1121 ** NULL into register P3 and every register in between P2 and P3. If P3 1122 ** is less than P2 (typically P3 is zero) then only register P2 is 1123 ** set to NULL. 1124 ** 1125 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that 1126 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on 1127 ** OP_Ne or OP_Eq. 1128 */ 1129 case OP_Null: { /* out2 */ 1130 int cnt; 1131 u16 nullFlag; 1132 pOut = out2Prerelease(p, pOp); 1133 cnt = pOp->p3-pOp->p2; 1134 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 1135 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; 1136 pOut->n = 0; 1137 while( cnt>0 ){ 1138 pOut++; 1139 memAboutToChange(p, pOut); 1140 sqlite3VdbeMemSetNull(pOut); 1141 pOut->flags = nullFlag; 1142 pOut->n = 0; 1143 cnt--; 1144 } 1145 break; 1146 } 1147 1148 /* Opcode: SoftNull P1 * * * * 1149 ** Synopsis: r[P1]=NULL 1150 ** 1151 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord 1152 ** instruction, but do not free any string or blob memory associated with 1153 ** the register, so that if the value was a string or blob that was 1154 ** previously copied using OP_SCopy, the copies will continue to be valid. 1155 */ 1156 case OP_SoftNull: { 1157 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 1158 pOut = &aMem[pOp->p1]; 1159 pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined; 1160 break; 1161 } 1162 1163 /* Opcode: Blob P1 P2 * P4 * 1164 ** Synopsis: r[P2]=P4 (len=P1) 1165 ** 1166 ** P4 points to a blob of data P1 bytes long. Store this 1167 ** blob in register P2. 1168 */ 1169 case OP_Blob: { /* out2 */ 1170 assert( pOp->p1 <= SQLITE_MAX_LENGTH ); 1171 pOut = out2Prerelease(p, pOp); 1172 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); 1173 pOut->enc = encoding; 1174 UPDATE_MAX_BLOBSIZE(pOut); 1175 break; 1176 } 1177 1178 /* Opcode: Variable P1 P2 * P4 * 1179 ** Synopsis: r[P2]=parameter(P1,P4) 1180 ** 1181 ** Transfer the values of bound parameter P1 into register P2 1182 ** 1183 ** If the parameter is named, then its name appears in P4. 1184 ** The P4 value is used by sqlite3_bind_parameter_name(). 1185 */ 1186 case OP_Variable: { /* out2 */ 1187 Mem *pVar; /* Value being transferred */ 1188 1189 assert( pOp->p1>0 && pOp->p1<=p->nVar ); 1190 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) ); 1191 pVar = &p->aVar[pOp->p1 - 1]; 1192 if( sqlite3VdbeMemTooBig(pVar) ){ 1193 goto too_big; 1194 } 1195 pOut = &aMem[pOp->p2]; 1196 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); 1197 UPDATE_MAX_BLOBSIZE(pOut); 1198 break; 1199 } 1200 1201 /* Opcode: Move P1 P2 P3 * * 1202 ** Synopsis: r[P2@P3]=r[P1@P3] 1203 ** 1204 ** Move the P3 values in register P1..P1+P3-1 over into 1205 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are 1206 ** left holding a NULL. It is an error for register ranges 1207 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error 1208 ** for P3 to be less than 1. 1209 */ 1210 case OP_Move: { 1211 int n; /* Number of registers left to copy */ 1212 int p1; /* Register to copy from */ 1213 int p2; /* Register to copy to */ 1214 1215 n = pOp->p3; 1216 p1 = pOp->p1; 1217 p2 = pOp->p2; 1218 assert( n>0 && p1>0 && p2>0 ); 1219 assert( p1+n<=p2 || p2+n<=p1 ); 1220 1221 pIn1 = &aMem[p1]; 1222 pOut = &aMem[p2]; 1223 do{ 1224 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] ); 1225 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] ); 1226 assert( memIsValid(pIn1) ); 1227 memAboutToChange(p, pOut); 1228 sqlite3VdbeMemMove(pOut, pIn1); 1229 #ifdef SQLITE_DEBUG 1230 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){ 1231 pOut->pScopyFrom += pOp->p2 - p1; 1232 } 1233 #endif 1234 Deephemeralize(pOut); 1235 REGISTER_TRACE(p2++, pOut); 1236 pIn1++; 1237 pOut++; 1238 }while( --n ); 1239 break; 1240 } 1241 1242 /* Opcode: Copy P1 P2 P3 * * 1243 ** Synopsis: r[P2@P3+1]=r[P1@P3+1] 1244 ** 1245 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. 1246 ** 1247 ** This instruction makes a deep copy of the value. A duplicate 1248 ** is made of any string or blob constant. See also OP_SCopy. 1249 */ 1250 case OP_Copy: { 1251 int n; 1252 1253 n = pOp->p3; 1254 pIn1 = &aMem[pOp->p1]; 1255 pOut = &aMem[pOp->p2]; 1256 assert( pOut!=pIn1 ); 1257 while( 1 ){ 1258 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); 1259 Deephemeralize(pOut); 1260 #ifdef SQLITE_DEBUG 1261 pOut->pScopyFrom = 0; 1262 #endif 1263 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut); 1264 if( (n--)==0 ) break; 1265 pOut++; 1266 pIn1++; 1267 } 1268 break; 1269 } 1270 1271 /* Opcode: SCopy P1 P2 * * * 1272 ** Synopsis: r[P2]=r[P1] 1273 ** 1274 ** Make a shallow copy of register P1 into register P2. 1275 ** 1276 ** This instruction makes a shallow copy of the value. If the value 1277 ** is a string or blob, then the copy is only a pointer to the 1278 ** original and hence if the original changes so will the copy. 1279 ** Worse, if the original is deallocated, the copy becomes invalid. 1280 ** Thus the program must guarantee that the original will not change 1281 ** during the lifetime of the copy. Use OP_Copy to make a complete 1282 ** copy. 1283 */ 1284 case OP_SCopy: { /* out2 */ 1285 pIn1 = &aMem[pOp->p1]; 1286 pOut = &aMem[pOp->p2]; 1287 assert( pOut!=pIn1 ); 1288 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); 1289 #ifdef SQLITE_DEBUG 1290 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1; 1291 #endif 1292 break; 1293 } 1294 1295 /* Opcode: IntCopy P1 P2 * * * 1296 ** Synopsis: r[P2]=r[P1] 1297 ** 1298 ** Transfer the integer value held in register P1 into register P2. 1299 ** 1300 ** This is an optimized version of SCopy that works only for integer 1301 ** values. 1302 */ 1303 case OP_IntCopy: { /* out2 */ 1304 pIn1 = &aMem[pOp->p1]; 1305 assert( (pIn1->flags & MEM_Int)!=0 ); 1306 pOut = &aMem[pOp->p2]; 1307 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i); 1308 break; 1309 } 1310 1311 /* Opcode: ResultRow P1 P2 * * * 1312 ** Synopsis: output=r[P1@P2] 1313 ** 1314 ** The registers P1 through P1+P2-1 contain a single row of 1315 ** results. This opcode causes the sqlite3_step() call to terminate 1316 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt 1317 ** structure to provide access to the r(P1)..r(P1+P2-1) values as 1318 ** the result row. 1319 */ 1320 case OP_ResultRow: { 1321 Mem *pMem; 1322 int i; 1323 assert( p->nResColumn==pOp->p2 ); 1324 assert( pOp->p1>0 ); 1325 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); 1326 1327 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 1328 /* Run the progress counter just before returning. 1329 */ 1330 if( db->xProgress!=0 1331 && nVmStep>=nProgressLimit 1332 && db->xProgress(db->pProgressArg)!=0 1333 ){ 1334 rc = SQLITE_INTERRUPT; 1335 goto abort_due_to_error; 1336 } 1337 #endif 1338 1339 /* If this statement has violated immediate foreign key constraints, do 1340 ** not return the number of rows modified. And do not RELEASE the statement 1341 ** transaction. It needs to be rolled back. */ 1342 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){ 1343 assert( db->flags&SQLITE_CountRows ); 1344 assert( p->usesStmtJournal ); 1345 goto abort_due_to_error; 1346 } 1347 1348 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then 1349 ** DML statements invoke this opcode to return the number of rows 1350 ** modified to the user. This is the only way that a VM that 1351 ** opens a statement transaction may invoke this opcode. 1352 ** 1353 ** In case this is such a statement, close any statement transaction 1354 ** opened by this VM before returning control to the user. This is to 1355 ** ensure that statement-transactions are always nested, not overlapping. 1356 ** If the open statement-transaction is not closed here, then the user 1357 ** may step another VM that opens its own statement transaction. This 1358 ** may lead to overlapping statement transactions. 1359 ** 1360 ** The statement transaction is never a top-level transaction. Hence 1361 ** the RELEASE call below can never fail. 1362 */ 1363 assert( p->iStatement==0 || db->flags&SQLITE_CountRows ); 1364 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE); 1365 assert( rc==SQLITE_OK ); 1366 1367 /* Invalidate all ephemeral cursor row caches */ 1368 p->cacheCtr = (p->cacheCtr + 2)|1; 1369 1370 /* Make sure the results of the current row are \000 terminated 1371 ** and have an assigned type. The results are de-ephemeralized as 1372 ** a side effect. 1373 */ 1374 pMem = p->pResultSet = &aMem[pOp->p1]; 1375 for(i=0; i<pOp->p2; i++){ 1376 assert( memIsValid(&pMem[i]) ); 1377 Deephemeralize(&pMem[i]); 1378 assert( (pMem[i].flags & MEM_Ephem)==0 1379 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 ); 1380 sqlite3VdbeMemNulTerminate(&pMem[i]); 1381 REGISTER_TRACE(pOp->p1+i, &pMem[i]); 1382 } 1383 if( db->mallocFailed ) goto no_mem; 1384 1385 if( db->mTrace & SQLITE_TRACE_ROW ){ 1386 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0); 1387 } 1388 1389 /* Return SQLITE_ROW 1390 */ 1391 p->pc = (int)(pOp - aOp) + 1; 1392 rc = SQLITE_ROW; 1393 goto vdbe_return; 1394 } 1395 1396 /* Opcode: Concat P1 P2 P3 * * 1397 ** Synopsis: r[P3]=r[P2]+r[P1] 1398 ** 1399 ** Add the text in register P1 onto the end of the text in 1400 ** register P2 and store the result in register P3. 1401 ** If either the P1 or P2 text are NULL then store NULL in P3. 1402 ** 1403 ** P3 = P2 || P1 1404 ** 1405 ** It is illegal for P1 and P3 to be the same register. Sometimes, 1406 ** if P3 is the same register as P2, the implementation is able 1407 ** to avoid a memcpy(). 1408 */ 1409 case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ 1410 i64 nByte; 1411 1412 pIn1 = &aMem[pOp->p1]; 1413 pIn2 = &aMem[pOp->p2]; 1414 pOut = &aMem[pOp->p3]; 1415 assert( pIn1!=pOut ); 1416 if( (pIn1->flags | pIn2->flags) & MEM_Null ){ 1417 sqlite3VdbeMemSetNull(pOut); 1418 break; 1419 } 1420 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem; 1421 Stringify(pIn1, encoding); 1422 Stringify(pIn2, encoding); 1423 nByte = pIn1->n + pIn2->n; 1424 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 1425 goto too_big; 1426 } 1427 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ 1428 goto no_mem; 1429 } 1430 MemSetTypeFlag(pOut, MEM_Str); 1431 if( pOut!=pIn2 ){ 1432 memcpy(pOut->z, pIn2->z, pIn2->n); 1433 } 1434 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); 1435 pOut->z[nByte]=0; 1436 pOut->z[nByte+1] = 0; 1437 pOut->flags |= MEM_Term; 1438 pOut->n = (int)nByte; 1439 pOut->enc = encoding; 1440 UPDATE_MAX_BLOBSIZE(pOut); 1441 break; 1442 } 1443 1444 /* Opcode: Add P1 P2 P3 * * 1445 ** Synopsis: r[P3]=r[P1]+r[P2] 1446 ** 1447 ** Add the value in register P1 to the value in register P2 1448 ** and store the result in register P3. 1449 ** If either input is NULL, the result is NULL. 1450 */ 1451 /* Opcode: Multiply P1 P2 P3 * * 1452 ** Synopsis: r[P3]=r[P1]*r[P2] 1453 ** 1454 ** 1455 ** Multiply the value in register P1 by the value in register P2 1456 ** and store the result in register P3. 1457 ** If either input is NULL, the result is NULL. 1458 */ 1459 /* Opcode: Subtract P1 P2 P3 * * 1460 ** Synopsis: r[P3]=r[P2]-r[P1] 1461 ** 1462 ** Subtract the value in register P1 from the value in register P2 1463 ** and store the result in register P3. 1464 ** If either input is NULL, the result is NULL. 1465 */ 1466 /* Opcode: Divide P1 P2 P3 * * 1467 ** Synopsis: r[P3]=r[P2]/r[P1] 1468 ** 1469 ** Divide the value in register P1 by the value in register P2 1470 ** and store the result in register P3 (P3=P2/P1). If the value in 1471 ** register P1 is zero, then the result is NULL. If either input is 1472 ** NULL, the result is NULL. 1473 */ 1474 /* Opcode: Remainder P1 P2 P3 * * 1475 ** Synopsis: r[P3]=r[P2]%r[P1] 1476 ** 1477 ** Compute the remainder after integer register P2 is divided by 1478 ** register P1 and store the result in register P3. 1479 ** If the value in register P1 is zero the result is NULL. 1480 ** If either operand is NULL, the result is NULL. 1481 */ 1482 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ 1483 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ 1484 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ 1485 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ 1486 case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ 1487 char bIntint; /* Started out as two integer operands */ 1488 u16 flags; /* Combined MEM_* flags from both inputs */ 1489 u16 type1; /* Numeric type of left operand */ 1490 u16 type2; /* Numeric type of right operand */ 1491 i64 iA; /* Integer value of left operand */ 1492 i64 iB; /* Integer value of right operand */ 1493 double rA; /* Real value of left operand */ 1494 double rB; /* Real value of right operand */ 1495 1496 pIn1 = &aMem[pOp->p1]; 1497 type1 = numericType(pIn1); 1498 pIn2 = &aMem[pOp->p2]; 1499 type2 = numericType(pIn2); 1500 pOut = &aMem[pOp->p3]; 1501 flags = pIn1->flags | pIn2->flags; 1502 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null; 1503 if( (type1 & type2 & MEM_Int)!=0 ){ 1504 iA = pIn1->u.i; 1505 iB = pIn2->u.i; 1506 bIntint = 1; 1507 switch( pOp->opcode ){ 1508 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; 1509 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; 1510 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break; 1511 case OP_Divide: { 1512 if( iA==0 ) goto arithmetic_result_is_null; 1513 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math; 1514 iB /= iA; 1515 break; 1516 } 1517 default: { 1518 if( iA==0 ) goto arithmetic_result_is_null; 1519 if( iA==-1 ) iA = 1; 1520 iB %= iA; 1521 break; 1522 } 1523 } 1524 pOut->u.i = iB; 1525 MemSetTypeFlag(pOut, MEM_Int); 1526 }else{ 1527 bIntint = 0; 1528 fp_math: 1529 rA = sqlite3VdbeRealValue(pIn1); 1530 rB = sqlite3VdbeRealValue(pIn2); 1531 switch( pOp->opcode ){ 1532 case OP_Add: rB += rA; break; 1533 case OP_Subtract: rB -= rA; break; 1534 case OP_Multiply: rB *= rA; break; 1535 case OP_Divide: { 1536 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ 1537 if( rA==(double)0 ) goto arithmetic_result_is_null; 1538 rB /= rA; 1539 break; 1540 } 1541 default: { 1542 iA = (i64)rA; 1543 iB = (i64)rB; 1544 if( iA==0 ) goto arithmetic_result_is_null; 1545 if( iA==-1 ) iA = 1; 1546 rB = (double)(iB % iA); 1547 break; 1548 } 1549 } 1550 #ifdef SQLITE_OMIT_FLOATING_POINT 1551 pOut->u.i = rB; 1552 MemSetTypeFlag(pOut, MEM_Int); 1553 #else 1554 if( sqlite3IsNaN(rB) ){ 1555 goto arithmetic_result_is_null; 1556 } 1557 pOut->u.r = rB; 1558 MemSetTypeFlag(pOut, MEM_Real); 1559 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){ 1560 sqlite3VdbeIntegerAffinity(pOut); 1561 } 1562 #endif 1563 } 1564 break; 1565 1566 arithmetic_result_is_null: 1567 sqlite3VdbeMemSetNull(pOut); 1568 break; 1569 } 1570 1571 /* Opcode: CollSeq P1 * * P4 1572 ** 1573 ** P4 is a pointer to a CollSeq struct. If the next call to a user function 1574 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will 1575 ** be returned. This is used by the built-in min(), max() and nullif() 1576 ** functions. 1577 ** 1578 ** If P1 is not zero, then it is a register that a subsequent min() or 1579 ** max() aggregate will set to 1 if the current row is not the minimum or 1580 ** maximum. The P1 register is initialized to 0 by this instruction. 1581 ** 1582 ** The interface used by the implementation of the aforementioned functions 1583 ** to retrieve the collation sequence set by this opcode is not available 1584 ** publicly. Only built-in functions have access to this feature. 1585 */ 1586 case OP_CollSeq: { 1587 assert( pOp->p4type==P4_COLLSEQ ); 1588 if( pOp->p1 ){ 1589 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); 1590 } 1591 break; 1592 } 1593 1594 /* Opcode: Function0 P1 P2 P3 P4 P5 1595 ** Synopsis: r[P3]=func(r[P2@P5]) 1596 ** 1597 ** Invoke a user function (P4 is a pointer to a FuncDef object that 1598 ** defines the function) with P5 arguments taken from register P2 and 1599 ** successors. The result of the function is stored in register P3. 1600 ** Register P3 must not be one of the function inputs. 1601 ** 1602 ** P1 is a 32-bit bitmask indicating whether or not each argument to the 1603 ** function was determined to be constant at compile time. If the first 1604 ** argument was constant then bit 0 of P1 is set. This is used to determine 1605 ** whether meta data associated with a user function argument using the 1606 ** sqlite3_set_auxdata() API may be safely retained until the next 1607 ** invocation of this opcode. 1608 ** 1609 ** See also: Function, AggStep, AggFinal 1610 */ 1611 /* Opcode: Function P1 P2 P3 P4 P5 1612 ** Synopsis: r[P3]=func(r[P2@P5]) 1613 ** 1614 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that 1615 ** contains a pointer to the function to be run) with P5 arguments taken 1616 ** from register P2 and successors. The result of the function is stored 1617 ** in register P3. Register P3 must not be one of the function inputs. 1618 ** 1619 ** P1 is a 32-bit bitmask indicating whether or not each argument to the 1620 ** function was determined to be constant at compile time. If the first 1621 ** argument was constant then bit 0 of P1 is set. This is used to determine 1622 ** whether meta data associated with a user function argument using the 1623 ** sqlite3_set_auxdata() API may be safely retained until the next 1624 ** invocation of this opcode. 1625 ** 1626 ** SQL functions are initially coded as OP_Function0 with P4 pointing 1627 ** to a FuncDef object. But on first evaluation, the P4 operand is 1628 ** automatically converted into an sqlite3_context object and the operation 1629 ** changed to this OP_Function opcode. In this way, the initialization of 1630 ** the sqlite3_context object occurs only once, rather than once for each 1631 ** evaluation of the function. 1632 ** 1633 ** See also: Function0, AggStep, AggFinal 1634 */ 1635 case OP_Function0: { 1636 int n; 1637 sqlite3_context *pCtx; 1638 1639 assert( pOp->p4type==P4_FUNCDEF ); 1640 n = pOp->p5; 1641 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 1642 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) ); 1643 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); 1644 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); 1645 if( pCtx==0 ) goto no_mem; 1646 pCtx->pOut = 0; 1647 pCtx->pFunc = pOp->p4.pFunc; 1648 pCtx->iOp = (int)(pOp - aOp); 1649 pCtx->pVdbe = p; 1650 pCtx->argc = n; 1651 pOp->p4type = P4_FUNCCTX; 1652 pOp->p4.pCtx = pCtx; 1653 pOp->opcode = OP_Function; 1654 /* Fall through into OP_Function */ 1655 } 1656 case OP_Function: { 1657 int i; 1658 sqlite3_context *pCtx; 1659 1660 assert( pOp->p4type==P4_FUNCCTX ); 1661 pCtx = pOp->p4.pCtx; 1662 1663 /* If this function is inside of a trigger, the register array in aMem[] 1664 ** might change from one evaluation to the next. The next block of code 1665 ** checks to see if the register array has changed, and if so it 1666 ** reinitializes the relavant parts of the sqlite3_context object */ 1667 pOut = &aMem[pOp->p3]; 1668 if( pCtx->pOut != pOut ){ 1669 pCtx->pOut = pOut; 1670 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; 1671 } 1672 1673 memAboutToChange(p, pCtx->pOut); 1674 #ifdef SQLITE_DEBUG 1675 for(i=0; i<pCtx->argc; i++){ 1676 assert( memIsValid(pCtx->argv[i]) ); 1677 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); 1678 } 1679 #endif 1680 MemSetTypeFlag(pCtx->pOut, MEM_Null); 1681 pCtx->fErrorOrAux = 0; 1682 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ 1683 1684 /* If the function returned an error, throw an exception */ 1685 if( pCtx->fErrorOrAux ){ 1686 if( pCtx->isError ){ 1687 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); 1688 rc = pCtx->isError; 1689 } 1690 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1); 1691 if( rc ) goto abort_due_to_error; 1692 } 1693 1694 /* Copy the result of the function into register P3 */ 1695 if( pOut->flags & (MEM_Str|MEM_Blob) ){ 1696 sqlite3VdbeChangeEncoding(pCtx->pOut, encoding); 1697 if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big; 1698 } 1699 1700 REGISTER_TRACE(pOp->p3, pCtx->pOut); 1701 UPDATE_MAX_BLOBSIZE(pCtx->pOut); 1702 break; 1703 } 1704 1705 /* Opcode: BitAnd P1 P2 P3 * * 1706 ** Synopsis: r[P3]=r[P1]&r[P2] 1707 ** 1708 ** Take the bit-wise AND of the values in register P1 and P2 and 1709 ** store the result in register P3. 1710 ** If either input is NULL, the result is NULL. 1711 */ 1712 /* Opcode: BitOr P1 P2 P3 * * 1713 ** Synopsis: r[P3]=r[P1]|r[P2] 1714 ** 1715 ** Take the bit-wise OR of the values in register P1 and P2 and 1716 ** store the result in register P3. 1717 ** If either input is NULL, the result is NULL. 1718 */ 1719 /* Opcode: ShiftLeft P1 P2 P3 * * 1720 ** Synopsis: r[P3]=r[P2]<<r[P1] 1721 ** 1722 ** Shift the integer value in register P2 to the left by the 1723 ** number of bits specified by the integer in register P1. 1724 ** Store the result in register P3. 1725 ** If either input is NULL, the result is NULL. 1726 */ 1727 /* Opcode: ShiftRight P1 P2 P3 * * 1728 ** Synopsis: r[P3]=r[P2]>>r[P1] 1729 ** 1730 ** Shift the integer value in register P2 to the right by the 1731 ** number of bits specified by the integer in register P1. 1732 ** Store the result in register P3. 1733 ** If either input is NULL, the result is NULL. 1734 */ 1735 case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ 1736 case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ 1737 case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ 1738 case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ 1739 i64 iA; 1740 u64 uA; 1741 i64 iB; 1742 u8 op; 1743 1744 pIn1 = &aMem[pOp->p1]; 1745 pIn2 = &aMem[pOp->p2]; 1746 pOut = &aMem[pOp->p3]; 1747 if( (pIn1->flags | pIn2->flags) & MEM_Null ){ 1748 sqlite3VdbeMemSetNull(pOut); 1749 break; 1750 } 1751 iA = sqlite3VdbeIntValue(pIn2); 1752 iB = sqlite3VdbeIntValue(pIn1); 1753 op = pOp->opcode; 1754 if( op==OP_BitAnd ){ 1755 iA &= iB; 1756 }else if( op==OP_BitOr ){ 1757 iA |= iB; 1758 }else if( iB!=0 ){ 1759 assert( op==OP_ShiftRight || op==OP_ShiftLeft ); 1760 1761 /* If shifting by a negative amount, shift in the other direction */ 1762 if( iB<0 ){ 1763 assert( OP_ShiftRight==OP_ShiftLeft+1 ); 1764 op = 2*OP_ShiftLeft + 1 - op; 1765 iB = iB>(-64) ? -iB : 64; 1766 } 1767 1768 if( iB>=64 ){ 1769 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1; 1770 }else{ 1771 memcpy(&uA, &iA, sizeof(uA)); 1772 if( op==OP_ShiftLeft ){ 1773 uA <<= iB; 1774 }else{ 1775 uA >>= iB; 1776 /* Sign-extend on a right shift of a negative number */ 1777 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB); 1778 } 1779 memcpy(&iA, &uA, sizeof(iA)); 1780 } 1781 } 1782 pOut->u.i = iA; 1783 MemSetTypeFlag(pOut, MEM_Int); 1784 break; 1785 } 1786 1787 /* Opcode: AddImm P1 P2 * * * 1788 ** Synopsis: r[P1]=r[P1]+P2 1789 ** 1790 ** Add the constant P2 to the value in register P1. 1791 ** The result is always an integer. 1792 ** 1793 ** To force any register to be an integer, just add 0. 1794 */ 1795 case OP_AddImm: { /* in1 */ 1796 pIn1 = &aMem[pOp->p1]; 1797 memAboutToChange(p, pIn1); 1798 sqlite3VdbeMemIntegerify(pIn1); 1799 pIn1->u.i += pOp->p2; 1800 break; 1801 } 1802 1803 /* Opcode: MustBeInt P1 P2 * * * 1804 ** 1805 ** Force the value in register P1 to be an integer. If the value 1806 ** in P1 is not an integer and cannot be converted into an integer 1807 ** without data loss, then jump immediately to P2, or if P2==0 1808 ** raise an SQLITE_MISMATCH exception. 1809 */ 1810 case OP_MustBeInt: { /* jump, in1 */ 1811 pIn1 = &aMem[pOp->p1]; 1812 if( (pIn1->flags & MEM_Int)==0 ){ 1813 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); 1814 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2); 1815 if( (pIn1->flags & MEM_Int)==0 ){ 1816 if( pOp->p2==0 ){ 1817 rc = SQLITE_MISMATCH; 1818 goto abort_due_to_error; 1819 }else{ 1820 goto jump_to_p2; 1821 } 1822 } 1823 } 1824 MemSetTypeFlag(pIn1, MEM_Int); 1825 break; 1826 } 1827 1828 #ifndef SQLITE_OMIT_FLOATING_POINT 1829 /* Opcode: RealAffinity P1 * * * * 1830 ** 1831 ** If register P1 holds an integer convert it to a real value. 1832 ** 1833 ** This opcode is used when extracting information from a column that 1834 ** has REAL affinity. Such column values may still be stored as 1835 ** integers, for space efficiency, but after extraction we want them 1836 ** to have only a real value. 1837 */ 1838 case OP_RealAffinity: { /* in1 */ 1839 pIn1 = &aMem[pOp->p1]; 1840 if( pIn1->flags & MEM_Int ){ 1841 sqlite3VdbeMemRealify(pIn1); 1842 } 1843 break; 1844 } 1845 #endif 1846 1847 #ifndef SQLITE_OMIT_CAST 1848 /* Opcode: Cast P1 P2 * * * 1849 ** Synopsis: affinity(r[P1]) 1850 ** 1851 ** Force the value in register P1 to be the type defined by P2. 1852 ** 1853 ** <ul> 1854 ** <li value="97"> TEXT 1855 ** <li value="98"> BLOB 1856 ** <li value="99"> NUMERIC 1857 ** <li value="100"> INTEGER 1858 ** <li value="101"> REAL 1859 ** </ul> 1860 ** 1861 ** A NULL value is not changed by this routine. It remains NULL. 1862 */ 1863 case OP_Cast: { /* in1 */ 1864 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL ); 1865 testcase( pOp->p2==SQLITE_AFF_TEXT ); 1866 testcase( pOp->p2==SQLITE_AFF_BLOB ); 1867 testcase( pOp->p2==SQLITE_AFF_NUMERIC ); 1868 testcase( pOp->p2==SQLITE_AFF_INTEGER ); 1869 testcase( pOp->p2==SQLITE_AFF_REAL ); 1870 pIn1 = &aMem[pOp->p1]; 1871 memAboutToChange(p, pIn1); 1872 rc = ExpandBlob(pIn1); 1873 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); 1874 UPDATE_MAX_BLOBSIZE(pIn1); 1875 if( rc ) goto abort_due_to_error; 1876 break; 1877 } 1878 #endif /* SQLITE_OMIT_CAST */ 1879 1880 /* Opcode: Eq P1 P2 P3 P4 P5 1881 ** Synopsis: IF r[P3]==r[P1] 1882 ** 1883 ** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then 1884 ** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then 1885 ** store the result of comparison in register P2. 1886 ** 1887 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - 1888 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 1889 ** to coerce both inputs according to this affinity before the 1890 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric 1891 ** affinity is used. Note that the affinity conversions are stored 1892 ** back into the input registers P1 and P3. So this opcode can cause 1893 ** persistent changes to registers P1 and P3. 1894 ** 1895 ** Once any conversions have taken place, and neither value is NULL, 1896 ** the values are compared. If both values are blobs then memcmp() is 1897 ** used to determine the results of the comparison. If both values 1898 ** are text, then the appropriate collating function specified in 1899 ** P4 is used to do the comparison. If P4 is not specified then 1900 ** memcmp() is used to compare text string. If both values are 1901 ** numeric, then a numeric comparison is used. If the two values 1902 ** are of different types, then numbers are considered less than 1903 ** strings and strings are considered less than blobs. 1904 ** 1905 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either 1906 ** true or false and is never NULL. If both operands are NULL then the result 1907 ** of comparison is true. If either operand is NULL then the result is false. 1908 ** If neither operand is NULL the result is the same as it would be if 1909 ** the SQLITE_NULLEQ flag were omitted from P5. 1910 ** 1911 ** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the 1912 ** content of r[P2] is only changed if the new value is NULL or 0 (false). 1913 ** In other words, a prior r[P2] value will not be overwritten by 1 (true). 1914 */ 1915 /* Opcode: Ne P1 P2 P3 P4 P5 1916 ** Synopsis: IF r[P3]!=r[P1] 1917 ** 1918 ** This works just like the Eq opcode except that the jump is taken if 1919 ** the operands in registers P1 and P3 are not equal. See the Eq opcode for 1920 ** additional information. 1921 ** 1922 ** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the 1923 ** content of r[P2] is only changed if the new value is NULL or 1 (true). 1924 ** In other words, a prior r[P2] value will not be overwritten by 0 (false). 1925 */ 1926 /* Opcode: Lt P1 P2 P3 P4 P5 1927 ** Synopsis: IF r[P3]<r[P1] 1928 ** 1929 ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then 1930 ** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store 1931 ** the result of comparison (0 or 1 or NULL) into register P2. 1932 ** 1933 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or 1934 ** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL 1935 ** bit is clear then fall through if either operand is NULL. 1936 ** 1937 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - 1938 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 1939 ** to coerce both inputs according to this affinity before the 1940 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric 1941 ** affinity is used. Note that the affinity conversions are stored 1942 ** back into the input registers P1 and P3. So this opcode can cause 1943 ** persistent changes to registers P1 and P3. 1944 ** 1945 ** Once any conversions have taken place, and neither value is NULL, 1946 ** the values are compared. If both values are blobs then memcmp() is 1947 ** used to determine the results of the comparison. If both values 1948 ** are text, then the appropriate collating function specified in 1949 ** P4 is used to do the comparison. If P4 is not specified then 1950 ** memcmp() is used to compare text string. If both values are 1951 ** numeric, then a numeric comparison is used. If the two values 1952 ** are of different types, then numbers are considered less than 1953 ** strings and strings are considered less than blobs. 1954 */ 1955 /* Opcode: Le P1 P2 P3 P4 P5 1956 ** Synopsis: IF r[P3]<=r[P1] 1957 ** 1958 ** This works just like the Lt opcode except that the jump is taken if 1959 ** the content of register P3 is less than or equal to the content of 1960 ** register P1. See the Lt opcode for additional information. 1961 */ 1962 /* Opcode: Gt P1 P2 P3 P4 P5 1963 ** Synopsis: IF r[P3]>r[P1] 1964 ** 1965 ** This works just like the Lt opcode except that the jump is taken if 1966 ** the content of register P3 is greater than the content of 1967 ** register P1. See the Lt opcode for additional information. 1968 */ 1969 /* Opcode: Ge P1 P2 P3 P4 P5 1970 ** Synopsis: IF r[P3]>=r[P1] 1971 ** 1972 ** This works just like the Lt opcode except that the jump is taken if 1973 ** the content of register P3 is greater than or equal to the content of 1974 ** register P1. See the Lt opcode for additional information. 1975 */ 1976 case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ 1977 case OP_Ne: /* same as TK_NE, jump, in1, in3 */ 1978 case OP_Lt: /* same as TK_LT, jump, in1, in3 */ 1979 case OP_Le: /* same as TK_LE, jump, in1, in3 */ 1980 case OP_Gt: /* same as TK_GT, jump, in1, in3 */ 1981 case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ 1982 int res, res2; /* Result of the comparison of pIn1 against pIn3 */ 1983 char affinity; /* Affinity to use for comparison */ 1984 u16 flags1; /* Copy of initial value of pIn1->flags */ 1985 u16 flags3; /* Copy of initial value of pIn3->flags */ 1986 1987 pIn1 = &aMem[pOp->p1]; 1988 pIn3 = &aMem[pOp->p3]; 1989 flags1 = pIn1->flags; 1990 flags3 = pIn3->flags; 1991 if( (flags1 | flags3)&MEM_Null ){ 1992 /* One or both operands are NULL */ 1993 if( pOp->p5 & SQLITE_NULLEQ ){ 1994 /* If SQLITE_NULLEQ is set (which will only happen if the operator is 1995 ** OP_Eq or OP_Ne) then take the jump or not depending on whether 1996 ** or not both operands are null. 1997 */ 1998 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne ); 1999 assert( (flags1 & MEM_Cleared)==0 ); 2000 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 ); 2001 if( (flags1&flags3&MEM_Null)!=0 2002 && (flags3&MEM_Cleared)==0 2003 ){ 2004 res = 0; /* Operands are equal */ 2005 }else{ 2006 res = 1; /* Operands are not equal */ 2007 } 2008 }else{ 2009 /* SQLITE_NULLEQ is clear and at least one operand is NULL, 2010 ** then the result is always NULL. 2011 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. 2012 */ 2013 if( pOp->p5 & SQLITE_STOREP2 ){ 2014 pOut = &aMem[pOp->p2]; 2015 iCompare = 1; /* Operands are not equal */ 2016 memAboutToChange(p, pOut); 2017 MemSetTypeFlag(pOut, MEM_Null); 2018 REGISTER_TRACE(pOp->p2, pOut); 2019 }else{ 2020 VdbeBranchTaken(2,3); 2021 if( pOp->p5 & SQLITE_JUMPIFNULL ){ 2022 goto jump_to_p2; 2023 } 2024 } 2025 break; 2026 } 2027 }else{ 2028 /* Neither operand is NULL. Do a comparison. */ 2029 affinity = pOp->p5 & SQLITE_AFF_MASK; 2030 if( affinity>=SQLITE_AFF_NUMERIC ){ 2031 if( (flags1 | flags3)&MEM_Str ){ 2032 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ 2033 applyNumericAffinity(pIn1,0); 2034 testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */ 2035 flags3 = pIn3->flags; 2036 } 2037 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ 2038 applyNumericAffinity(pIn3,0); 2039 } 2040 } 2041 /* Handle the common case of integer comparison here, as an 2042 ** optimization, to avoid a call to sqlite3MemCompare() */ 2043 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){ 2044 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; } 2045 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; } 2046 res = 0; 2047 goto compare_op; 2048 } 2049 }else if( affinity==SQLITE_AFF_TEXT ){ 2050 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){ 2051 testcase( pIn1->flags & MEM_Int ); 2052 testcase( pIn1->flags & MEM_Real ); 2053 sqlite3VdbeMemStringify(pIn1, encoding, 1); 2054 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); 2055 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); 2056 assert( pIn1!=pIn3 ); 2057 } 2058 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){ 2059 testcase( pIn3->flags & MEM_Int ); 2060 testcase( pIn3->flags & MEM_Real ); 2061 sqlite3VdbeMemStringify(pIn3, encoding, 1); 2062 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); 2063 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); 2064 } 2065 } 2066 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); 2067 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); 2068 } 2069 compare_op: 2070 switch( pOp->opcode ){ 2071 case OP_Eq: res2 = res==0; break; 2072 case OP_Ne: res2 = res; break; 2073 case OP_Lt: res2 = res<0; break; 2074 case OP_Le: res2 = res<=0; break; 2075 case OP_Gt: res2 = res>0; break; 2076 default: res2 = res>=0; break; 2077 } 2078 2079 /* Undo any changes made by applyAffinity() to the input registers. */ 2080 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); 2081 pIn1->flags = flags1; 2082 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); 2083 pIn3->flags = flags3; 2084 2085 if( pOp->p5 & SQLITE_STOREP2 ){ 2086 pOut = &aMem[pOp->p2]; 2087 iCompare = res; 2088 res2 = res2!=0; /* For this path res2 must be exactly 0 or 1 */ 2089 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){ 2090 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1 2091 ** and prevents OP_Ne from overwriting NULL with 0. This flag 2092 ** is only used in contexts where either: 2093 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0) 2094 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1) 2095 ** Therefore it is not necessary to check the content of r[P2] for 2096 ** NULL. */ 2097 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq ); 2098 assert( res2==0 || res2==1 ); 2099 testcase( res2==0 && pOp->opcode==OP_Eq ); 2100 testcase( res2==1 && pOp->opcode==OP_Eq ); 2101 testcase( res2==0 && pOp->opcode==OP_Ne ); 2102 testcase( res2==1 && pOp->opcode==OP_Ne ); 2103 if( (pOp->opcode==OP_Eq)==res2 ) break; 2104 } 2105 memAboutToChange(p, pOut); 2106 MemSetTypeFlag(pOut, MEM_Int); 2107 pOut->u.i = res2; 2108 REGISTER_TRACE(pOp->p2, pOut); 2109 }else{ 2110 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); 2111 if( res2 ){ 2112 goto jump_to_p2; 2113 } 2114 } 2115 break; 2116 } 2117 2118 /* Opcode: ElseNotEq * P2 * * * 2119 ** 2120 ** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator. 2121 ** If result of an OP_Eq comparison on the same two operands 2122 ** would have be NULL or false (0), then then jump to P2. 2123 ** If the result of an OP_Eq comparison on the two previous operands 2124 ** would have been true (1), then fall through. 2125 */ 2126 case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */ 2127 assert( pOp>aOp ); 2128 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt ); 2129 assert( pOp[-1].p5 & SQLITE_STOREP2 ); 2130 VdbeBranchTaken(iCompare!=0, 2); 2131 if( iCompare!=0 ) goto jump_to_p2; 2132 break; 2133 } 2134 2135 2136 /* Opcode: Permutation * * * P4 * 2137 ** 2138 ** Set the permutation used by the OP_Compare operator in the next 2139 ** instruction. The permutation is stored in the P4 operand. 2140 ** 2141 ** The permutation is only valid until the next OP_Compare that has 2142 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should 2143 ** occur immediately prior to the OP_Compare. 2144 ** 2145 ** The first integer in the P4 integer array is the length of the array 2146 ** and does not become part of the permutation. 2147 */ 2148 case OP_Permutation: { 2149 assert( pOp->p4type==P4_INTARRAY ); 2150 assert( pOp->p4.ai ); 2151 assert( pOp[1].opcode==OP_Compare ); 2152 assert( pOp[1].p5 & OPFLAG_PERMUTE ); 2153 break; 2154 } 2155 2156 /* Opcode: Compare P1 P2 P3 P4 P5 2157 ** Synopsis: r[P1@P3] <-> r[P2@P3] 2158 ** 2159 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this 2160 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of 2161 ** the comparison for use by the next OP_Jump instruct. 2162 ** 2163 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is 2164 ** determined by the most recent OP_Permutation operator. If the 2165 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential 2166 ** order. 2167 ** 2168 ** P4 is a KeyInfo structure that defines collating sequences and sort 2169 ** orders for the comparison. The permutation applies to registers 2170 ** only. The KeyInfo elements are used sequentially. 2171 ** 2172 ** The comparison is a sort comparison, so NULLs compare equal, 2173 ** NULLs are less than numbers, numbers are less than strings, 2174 ** and strings are less than blobs. 2175 */ 2176 case OP_Compare: { 2177 int n; 2178 int i; 2179 int p1; 2180 int p2; 2181 const KeyInfo *pKeyInfo; 2182 int idx; 2183 CollSeq *pColl; /* Collating sequence to use on this term */ 2184 int bRev; /* True for DESCENDING sort order */ 2185 int *aPermute; /* The permutation */ 2186 2187 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){ 2188 aPermute = 0; 2189 }else{ 2190 assert( pOp>aOp ); 2191 assert( pOp[-1].opcode==OP_Permutation ); 2192 assert( pOp[-1].p4type==P4_INTARRAY ); 2193 aPermute = pOp[-1].p4.ai + 1; 2194 assert( aPermute!=0 ); 2195 } 2196 n = pOp->p3; 2197 pKeyInfo = pOp->p4.pKeyInfo; 2198 assert( n>0 ); 2199 assert( pKeyInfo!=0 ); 2200 p1 = pOp->p1; 2201 p2 = pOp->p2; 2202 #if SQLITE_DEBUG 2203 if( aPermute ){ 2204 int k, mx = 0; 2205 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k]; 2206 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 ); 2207 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 ); 2208 }else{ 2209 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 ); 2210 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 ); 2211 } 2212 #endif /* SQLITE_DEBUG */ 2213 for(i=0; i<n; i++){ 2214 idx = aPermute ? aPermute[i] : i; 2215 assert( memIsValid(&aMem[p1+idx]) ); 2216 assert( memIsValid(&aMem[p2+idx]) ); 2217 REGISTER_TRACE(p1+idx, &aMem[p1+idx]); 2218 REGISTER_TRACE(p2+idx, &aMem[p2+idx]); 2219 assert( i<pKeyInfo->nField ); 2220 pColl = pKeyInfo->aColl[i]; 2221 bRev = pKeyInfo->aSortOrder[i]; 2222 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); 2223 if( iCompare ){ 2224 if( bRev ) iCompare = -iCompare; 2225 break; 2226 } 2227 } 2228 break; 2229 } 2230 2231 /* Opcode: Jump P1 P2 P3 * * 2232 ** 2233 ** Jump to the instruction at address P1, P2, or P3 depending on whether 2234 ** in the most recent OP_Compare instruction the P1 vector was less than 2235 ** equal to, or greater than the P2 vector, respectively. 2236 */ 2237 case OP_Jump: { /* jump */ 2238 if( iCompare<0 ){ 2239 VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1]; 2240 }else if( iCompare==0 ){ 2241 VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1]; 2242 }else{ 2243 VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1]; 2244 } 2245 break; 2246 } 2247 2248 /* Opcode: And P1 P2 P3 * * 2249 ** Synopsis: r[P3]=(r[P1] && r[P2]) 2250 ** 2251 ** Take the logical AND of the values in registers P1 and P2 and 2252 ** write the result into register P3. 2253 ** 2254 ** If either P1 or P2 is 0 (false) then the result is 0 even if 2255 ** the other input is NULL. A NULL and true or two NULLs give 2256 ** a NULL output. 2257 */ 2258 /* Opcode: Or P1 P2 P3 * * 2259 ** Synopsis: r[P3]=(r[P1] || r[P2]) 2260 ** 2261 ** Take the logical OR of the values in register P1 and P2 and 2262 ** store the answer in register P3. 2263 ** 2264 ** If either P1 or P2 is nonzero (true) then the result is 1 (true) 2265 ** even if the other input is NULL. A NULL and false or two NULLs 2266 ** give a NULL output. 2267 */ 2268 case OP_And: /* same as TK_AND, in1, in2, out3 */ 2269 case OP_Or: { /* same as TK_OR, in1, in2, out3 */ 2270 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ 2271 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ 2272 2273 pIn1 = &aMem[pOp->p1]; 2274 if( pIn1->flags & MEM_Null ){ 2275 v1 = 2; 2276 }else{ 2277 v1 = sqlite3VdbeIntValue(pIn1)!=0; 2278 } 2279 pIn2 = &aMem[pOp->p2]; 2280 if( pIn2->flags & MEM_Null ){ 2281 v2 = 2; 2282 }else{ 2283 v2 = sqlite3VdbeIntValue(pIn2)!=0; 2284 } 2285 if( pOp->opcode==OP_And ){ 2286 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; 2287 v1 = and_logic[v1*3+v2]; 2288 }else{ 2289 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 }; 2290 v1 = or_logic[v1*3+v2]; 2291 } 2292 pOut = &aMem[pOp->p3]; 2293 if( v1==2 ){ 2294 MemSetTypeFlag(pOut, MEM_Null); 2295 }else{ 2296 pOut->u.i = v1; 2297 MemSetTypeFlag(pOut, MEM_Int); 2298 } 2299 break; 2300 } 2301 2302 /* Opcode: Not P1 P2 * * * 2303 ** Synopsis: r[P2]= !r[P1] 2304 ** 2305 ** Interpret the value in register P1 as a boolean value. Store the 2306 ** boolean complement in register P2. If the value in register P1 is 2307 ** NULL, then a NULL is stored in P2. 2308 */ 2309 case OP_Not: { /* same as TK_NOT, in1, out2 */ 2310 pIn1 = &aMem[pOp->p1]; 2311 pOut = &aMem[pOp->p2]; 2312 sqlite3VdbeMemSetNull(pOut); 2313 if( (pIn1->flags & MEM_Null)==0 ){ 2314 pOut->flags = MEM_Int; 2315 pOut->u.i = !sqlite3VdbeIntValue(pIn1); 2316 } 2317 break; 2318 } 2319 2320 /* Opcode: BitNot P1 P2 * * * 2321 ** Synopsis: r[P1]= ~r[P1] 2322 ** 2323 ** Interpret the content of register P1 as an integer. Store the 2324 ** ones-complement of the P1 value into register P2. If P1 holds 2325 ** a NULL then store a NULL in P2. 2326 */ 2327 case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ 2328 pIn1 = &aMem[pOp->p1]; 2329 pOut = &aMem[pOp->p2]; 2330 sqlite3VdbeMemSetNull(pOut); 2331 if( (pIn1->flags & MEM_Null)==0 ){ 2332 pOut->flags = MEM_Int; 2333 pOut->u.i = ~sqlite3VdbeIntValue(pIn1); 2334 } 2335 break; 2336 } 2337 2338 /* Opcode: Once P1 P2 * * * 2339 ** 2340 ** If the P1 value is equal to the P1 value on the OP_Init opcode at 2341 ** instruction 0, then jump to P2. If the two P1 values differ, then 2342 ** set the P1 value on this opcode to equal the P1 value on the OP_Init 2343 ** and fall through. 2344 */ 2345 case OP_Once: { /* jump */ 2346 assert( p->aOp[0].opcode==OP_Init ); 2347 VdbeBranchTaken(p->aOp[0].p1==pOp->p1, 2); 2348 if( p->aOp[0].p1==pOp->p1 ){ 2349 goto jump_to_p2; 2350 }else{ 2351 pOp->p1 = p->aOp[0].p1; 2352 } 2353 break; 2354 } 2355 2356 /* Opcode: If P1 P2 P3 * * 2357 ** 2358 ** Jump to P2 if the value in register P1 is true. The value 2359 ** is considered true if it is numeric and non-zero. If the value 2360 ** in P1 is NULL then take the jump if and only if P3 is non-zero. 2361 */ 2362 /* Opcode: IfNot P1 P2 P3 * * 2363 ** 2364 ** Jump to P2 if the value in register P1 is False. The value 2365 ** is considered false if it has a numeric value of zero. If the value 2366 ** in P1 is NULL then take the jump if and only if P3 is non-zero. 2367 */ 2368 case OP_If: /* jump, in1 */ 2369 case OP_IfNot: { /* jump, in1 */ 2370 int c; 2371 pIn1 = &aMem[pOp->p1]; 2372 if( pIn1->flags & MEM_Null ){ 2373 c = pOp->p3; 2374 }else{ 2375 #ifdef SQLITE_OMIT_FLOATING_POINT 2376 c = sqlite3VdbeIntValue(pIn1)!=0; 2377 #else 2378 c = sqlite3VdbeRealValue(pIn1)!=0.0; 2379 #endif 2380 if( pOp->opcode==OP_IfNot ) c = !c; 2381 } 2382 VdbeBranchTaken(c!=0, 2); 2383 if( c ){ 2384 goto jump_to_p2; 2385 } 2386 break; 2387 } 2388 2389 /* Opcode: IsNull P1 P2 * * * 2390 ** Synopsis: if r[P1]==NULL goto P2 2391 ** 2392 ** Jump to P2 if the value in register P1 is NULL. 2393 */ 2394 case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ 2395 pIn1 = &aMem[pOp->p1]; 2396 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); 2397 if( (pIn1->flags & MEM_Null)!=0 ){ 2398 goto jump_to_p2; 2399 } 2400 break; 2401 } 2402 2403 /* Opcode: NotNull P1 P2 * * * 2404 ** Synopsis: if r[P1]!=NULL goto P2 2405 ** 2406 ** Jump to P2 if the value in register P1 is not NULL. 2407 */ 2408 case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ 2409 pIn1 = &aMem[pOp->p1]; 2410 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); 2411 if( (pIn1->flags & MEM_Null)==0 ){ 2412 goto jump_to_p2; 2413 } 2414 break; 2415 } 2416 2417 /* Opcode: Column P1 P2 P3 P4 P5 2418 ** Synopsis: r[P3]=PX 2419 ** 2420 ** Interpret the data that cursor P1 points to as a structure built using 2421 ** the MakeRecord instruction. (See the MakeRecord opcode for additional 2422 ** information about the format of the data.) Extract the P2-th column 2423 ** from this record. If there are less that (P2+1) 2424 ** values in the record, extract a NULL. 2425 ** 2426 ** The value extracted is stored in register P3. 2427 ** 2428 ** If the column contains fewer than P2 fields, then extract a NULL. Or, 2429 ** if the P4 argument is a P4_MEM use the value of the P4 argument as 2430 ** the result. 2431 ** 2432 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor, 2433 ** then the cache of the cursor is reset prior to extracting the column. 2434 ** The first OP_Column against a pseudo-table after the value of the content 2435 ** register has changed should have this bit set. 2436 ** 2437 ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when 2438 ** the result is guaranteed to only be used as the argument of a length() 2439 ** or typeof() function, respectively. The loading of large blobs can be 2440 ** skipped for length() and all content loading can be skipped for typeof(). 2441 */ 2442 case OP_Column: { 2443 int p2; /* column number to retrieve */ 2444 VdbeCursor *pC; /* The VDBE cursor */ 2445 BtCursor *pCrsr; /* The BTree cursor */ 2446 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ 2447 int len; /* The length of the serialized data for the column */ 2448 int i; /* Loop counter */ 2449 Mem *pDest; /* Where to write the extracted value */ 2450 Mem sMem; /* For storing the record being decoded */ 2451 const u8 *zData; /* Part of the record being decoded */ 2452 const u8 *zHdr; /* Next unparsed byte of the header */ 2453 const u8 *zEndHdr; /* Pointer to first byte after the header */ 2454 u32 offset; /* Offset into the data */ 2455 u64 offset64; /* 64-bit offset */ 2456 u32 avail; /* Number of bytes of available data */ 2457 u32 t; /* A type code from the record header */ 2458 Mem *pReg; /* PseudoTable input register */ 2459 2460 pC = p->apCsr[pOp->p1]; 2461 p2 = pOp->p2; 2462 2463 /* If the cursor cache is stale, bring it up-to-date */ 2464 rc = sqlite3VdbeCursorMoveto(&pC, &p2); 2465 if( rc ) goto abort_due_to_error; 2466 2467 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 2468 pDest = &aMem[pOp->p3]; 2469 memAboutToChange(p, pDest); 2470 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 2471 assert( pC!=0 ); 2472 assert( p2<pC->nField ); 2473 aOffset = pC->aOffset; 2474 assert( pC->eCurType!=CURTYPE_VTAB ); 2475 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); 2476 assert( pC->eCurType!=CURTYPE_SORTER ); 2477 2478 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/ 2479 if( pC->nullRow ){ 2480 if( pC->eCurType==CURTYPE_PSEUDO ){ 2481 assert( pC->uc.pseudoTableReg>0 ); 2482 pReg = &aMem[pC->uc.pseudoTableReg]; 2483 assert( pReg->flags & MEM_Blob ); 2484 assert( memIsValid(pReg) ); 2485 pC->payloadSize = pC->szRow = avail = pReg->n; 2486 pC->aRow = (u8*)pReg->z; 2487 }else{ 2488 sqlite3VdbeMemSetNull(pDest); 2489 goto op_column_out; 2490 } 2491 }else{ 2492 pCrsr = pC->uc.pCursor; 2493 assert( pC->eCurType==CURTYPE_BTREE ); 2494 assert( pCrsr ); 2495 assert( sqlite3BtreeCursorIsValid(pCrsr) ); 2496 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr); 2497 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &avail); 2498 assert( avail<=65536 ); /* Maximum page size is 64KiB */ 2499 if( pC->payloadSize <= (u32)avail ){ 2500 pC->szRow = pC->payloadSize; 2501 }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ 2502 goto too_big; 2503 }else{ 2504 pC->szRow = avail; 2505 } 2506 } 2507 pC->cacheStatus = p->cacheCtr; 2508 pC->iHdrOffset = getVarint32(pC->aRow, offset); 2509 pC->nHdrParsed = 0; 2510 aOffset[0] = offset; 2511 2512 2513 if( avail<offset ){ /*OPTIMIZATION-IF-FALSE*/ 2514 /* pC->aRow does not have to hold the entire row, but it does at least 2515 ** need to cover the header of the record. If pC->aRow does not contain 2516 ** the complete header, then set it to zero, forcing the header to be 2517 ** dynamically allocated. */ 2518 pC->aRow = 0; 2519 pC->szRow = 0; 2520 2521 /* Make sure a corrupt database has not given us an oversize header. 2522 ** Do this now to avoid an oversize memory allocation. 2523 ** 2524 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte 2525 ** types use so much data space that there can only be 4096 and 32 of 2526 ** them, respectively. So the maximum header length results from a 2527 ** 3-byte type for each of the maximum of 32768 columns plus three 2528 ** extra bytes for the header length itself. 32768*3 + 3 = 98307. 2529 */ 2530 if( offset > 98307 || offset > pC->payloadSize ){ 2531 rc = SQLITE_CORRUPT_BKPT; 2532 goto abort_due_to_error; 2533 } 2534 }else if( offset>0 ){ /*OPTIMIZATION-IF-TRUE*/ 2535 /* The following goto is an optimization. It can be omitted and 2536 ** everything will still work. But OP_Column is measurably faster 2537 ** by skipping the subsequent conditional, which is always true. 2538 */ 2539 zData = pC->aRow; 2540 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ 2541 goto op_column_read_header; 2542 } 2543 } 2544 2545 /* Make sure at least the first p2+1 entries of the header have been 2546 ** parsed and valid information is in aOffset[] and pC->aType[]. 2547 */ 2548 if( pC->nHdrParsed<=p2 ){ 2549 /* If there is more header available for parsing in the record, try 2550 ** to extract additional fields up through the p2+1-th field 2551 */ 2552 if( pC->iHdrOffset<aOffset[0] ){ 2553 /* Make sure zData points to enough of the record to cover the header. */ 2554 if( pC->aRow==0 ){ 2555 memset(&sMem, 0, sizeof(sMem)); 2556 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem); 2557 if( rc!=SQLITE_OK ) goto abort_due_to_error; 2558 zData = (u8*)sMem.z; 2559 }else{ 2560 zData = pC->aRow; 2561 } 2562 2563 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */ 2564 op_column_read_header: 2565 i = pC->nHdrParsed; 2566 offset64 = aOffset[i]; 2567 zHdr = zData + pC->iHdrOffset; 2568 zEndHdr = zData + aOffset[0]; 2569 do{ 2570 if( (t = zHdr[0])<0x80 ){ 2571 zHdr++; 2572 offset64 += sqlite3VdbeOneByteSerialTypeLen(t); 2573 }else{ 2574 zHdr += sqlite3GetVarint32(zHdr, &t); 2575 offset64 += sqlite3VdbeSerialTypeLen(t); 2576 } 2577 pC->aType[i++] = t; 2578 aOffset[i] = (u32)(offset64 & 0xffffffff); 2579 }while( i<=p2 && zHdr<zEndHdr ); 2580 2581 /* The record is corrupt if any of the following are true: 2582 ** (1) the bytes of the header extend past the declared header size 2583 ** (2) the entire header was used but not all data was used 2584 ** (3) the end of the data extends beyond the end of the record. 2585 */ 2586 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize)) 2587 || (offset64 > pC->payloadSize) 2588 ){ 2589 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); 2590 rc = SQLITE_CORRUPT_BKPT; 2591 goto abort_due_to_error; 2592 } 2593 2594 pC->nHdrParsed = i; 2595 pC->iHdrOffset = (u32)(zHdr - zData); 2596 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); 2597 }else{ 2598 t = 0; 2599 } 2600 2601 /* If after trying to extract new entries from the header, nHdrParsed is 2602 ** still not up to p2, that means that the record has fewer than p2 2603 ** columns. So the result will be either the default value or a NULL. 2604 */ 2605 if( pC->nHdrParsed<=p2 ){ 2606 if( pOp->p4type==P4_MEM ){ 2607 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); 2608 }else{ 2609 sqlite3VdbeMemSetNull(pDest); 2610 } 2611 goto op_column_out; 2612 } 2613 }else{ 2614 t = pC->aType[p2]; 2615 } 2616 2617 /* Extract the content for the p2+1-th column. Control can only 2618 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are 2619 ** all valid. 2620 */ 2621 assert( p2<pC->nHdrParsed ); 2622 assert( rc==SQLITE_OK ); 2623 assert( sqlite3VdbeCheckMemInvariants(pDest) ); 2624 if( VdbeMemDynamic(pDest) ){ 2625 sqlite3VdbeMemSetNull(pDest); 2626 } 2627 assert( t==pC->aType[p2] ); 2628 if( pC->szRow>=aOffset[p2+1] ){ 2629 /* This is the common case where the desired content fits on the original 2630 ** page - where the content is not on an overflow page */ 2631 zData = pC->aRow + aOffset[p2]; 2632 if( t<12 ){ 2633 sqlite3VdbeSerialGet(zData, t, pDest); 2634 }else{ 2635 /* If the column value is a string, we need a persistent value, not 2636 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent 2637 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize(). 2638 */ 2639 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term }; 2640 pDest->n = len = (t-12)/2; 2641 pDest->enc = encoding; 2642 if( pDest->szMalloc < len+2 ){ 2643 pDest->flags = MEM_Null; 2644 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem; 2645 }else{ 2646 pDest->z = pDest->zMalloc; 2647 } 2648 memcpy(pDest->z, zData, len); 2649 pDest->z[len] = 0; 2650 pDest->z[len+1] = 0; 2651 pDest->flags = aFlag[t&1]; 2652 } 2653 }else{ 2654 pDest->enc = encoding; 2655 /* This branch happens only when content is on overflow pages */ 2656 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0 2657 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)) 2658 || (len = sqlite3VdbeSerialTypeLen(t))==0 2659 ){ 2660 /* Content is irrelevant for 2661 ** 1. the typeof() function, 2662 ** 2. the length(X) function if X is a blob, and 2663 ** 3. if the content length is zero. 2664 ** So we might as well use bogus content rather than reading 2665 ** content from disk. */ 2666 static u8 aZero[8]; /* This is the bogus content */ 2667 sqlite3VdbeSerialGet(aZero, t, pDest); 2668 }else{ 2669 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest); 2670 if( rc!=SQLITE_OK ) goto abort_due_to_error; 2671 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); 2672 pDest->flags &= ~MEM_Ephem; 2673 } 2674 } 2675 2676 op_column_out: 2677 UPDATE_MAX_BLOBSIZE(pDest); 2678 REGISTER_TRACE(pOp->p3, pDest); 2679 break; 2680 } 2681 2682 /* Opcode: Affinity P1 P2 * P4 * 2683 ** Synopsis: affinity(r[P1@P2]) 2684 ** 2685 ** Apply affinities to a range of P2 registers starting with P1. 2686 ** 2687 ** P4 is a string that is P2 characters long. The nth character of the 2688 ** string indicates the column affinity that should be used for the nth 2689 ** memory cell in the range. 2690 */ 2691 case OP_Affinity: { 2692 const char *zAffinity; /* The affinity to be applied */ 2693 char cAff; /* A single character of affinity */ 2694 2695 zAffinity = pOp->p4.z; 2696 assert( zAffinity!=0 ); 2697 assert( zAffinity[pOp->p2]==0 ); 2698 pIn1 = &aMem[pOp->p1]; 2699 while( (cAff = *(zAffinity++))!=0 ){ 2700 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); 2701 assert( memIsValid(pIn1) ); 2702 applyAffinity(pIn1, cAff, encoding); 2703 pIn1++; 2704 } 2705 break; 2706 } 2707 2708 /* Opcode: MakeRecord P1 P2 P3 P4 * 2709 ** Synopsis: r[P3]=mkrec(r[P1@P2]) 2710 ** 2711 ** Convert P2 registers beginning with P1 into the [record format] 2712 ** use as a data record in a database table or as a key 2713 ** in an index. The OP_Column opcode can decode the record later. 2714 ** 2715 ** P4 may be a string that is P2 characters long. The nth character of the 2716 ** string indicates the column affinity that should be used for the nth 2717 ** field of the index key. 2718 ** 2719 ** The mapping from character to affinity is given by the SQLITE_AFF_ 2720 ** macros defined in sqliteInt.h. 2721 ** 2722 ** If P4 is NULL then all index fields have the affinity BLOB. 2723 */ 2724 case OP_MakeRecord: { 2725 u8 *zNewRecord; /* A buffer to hold the data for the new record */ 2726 Mem *pRec; /* The new record */ 2727 u64 nData; /* Number of bytes of data space */ 2728 int nHdr; /* Number of bytes of header space */ 2729 i64 nByte; /* Data space required for this record */ 2730 i64 nZero; /* Number of zero bytes at the end of the record */ 2731 int nVarint; /* Number of bytes in a varint */ 2732 u32 serial_type; /* Type field */ 2733 Mem *pData0; /* First field to be combined into the record */ 2734 Mem *pLast; /* Last field of the record */ 2735 int nField; /* Number of fields in the record */ 2736 char *zAffinity; /* The affinity string for the record */ 2737 int file_format; /* File format to use for encoding */ 2738 int i; /* Space used in zNewRecord[] header */ 2739 int j; /* Space used in zNewRecord[] content */ 2740 u32 len; /* Length of a field */ 2741 2742 /* Assuming the record contains N fields, the record format looks 2743 ** like this: 2744 ** 2745 ** ------------------------------------------------------------------------ 2746 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | 2747 ** ------------------------------------------------------------------------ 2748 ** 2749 ** Data(0) is taken from register P1. Data(1) comes from register P1+1 2750 ** and so forth. 2751 ** 2752 ** Each type field is a varint representing the serial type of the 2753 ** corresponding data element (see sqlite3VdbeSerialType()). The 2754 ** hdr-size field is also a varint which is the offset from the beginning 2755 ** of the record to data0. 2756 */ 2757 nData = 0; /* Number of bytes of data space */ 2758 nHdr = 0; /* Number of bytes of header space */ 2759 nZero = 0; /* Number of zero bytes at the end of the record */ 2760 nField = pOp->p1; 2761 zAffinity = pOp->p4.z; 2762 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 ); 2763 pData0 = &aMem[nField]; 2764 nField = pOp->p2; 2765 pLast = &pData0[nField-1]; 2766 file_format = p->minWriteFileFormat; 2767 2768 /* Identify the output register */ 2769 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); 2770 pOut = &aMem[pOp->p3]; 2771 memAboutToChange(p, pOut); 2772 2773 /* Apply the requested affinity to all inputs 2774 */ 2775 assert( pData0<=pLast ); 2776 if( zAffinity ){ 2777 pRec = pData0; 2778 do{ 2779 applyAffinity(pRec++, *(zAffinity++), encoding); 2780 assert( zAffinity[0]==0 || pRec<=pLast ); 2781 }while( zAffinity[0] ); 2782 } 2783 2784 #ifdef SQLITE_ENABLE_NULL_TRIM 2785 /* NULLs can be safely trimmed from the end of the record, as long as 2786 ** as the schema format is 2 or more and none of the omitted columns 2787 ** have a non-NULL default value. Also, the record must be left with 2788 ** at least one field. If P5>0 then it will be one more than the 2789 ** index of the right-most column with a non-NULL default value */ 2790 if( pOp->p5 ){ 2791 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){ 2792 pLast--; 2793 nField--; 2794 } 2795 } 2796 #endif 2797 2798 /* Loop through the elements that will make up the record to figure 2799 ** out how much space is required for the new record. 2800 */ 2801 pRec = pLast; 2802 do{ 2803 assert( memIsValid(pRec) ); 2804 pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len); 2805 if( pRec->flags & MEM_Zero ){ 2806 if( nData ){ 2807 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; 2808 }else{ 2809 nZero += pRec->u.nZero; 2810 len -= pRec->u.nZero; 2811 } 2812 } 2813 nData += len; 2814 testcase( serial_type==127 ); 2815 testcase( serial_type==128 ); 2816 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type); 2817 if( pRec==pData0 ) break; 2818 pRec--; 2819 }while(1); 2820 2821 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint 2822 ** which determines the total number of bytes in the header. The varint 2823 ** value is the size of the header in bytes including the size varint 2824 ** itself. */ 2825 testcase( nHdr==126 ); 2826 testcase( nHdr==127 ); 2827 if( nHdr<=126 ){ 2828 /* The common case */ 2829 nHdr += 1; 2830 }else{ 2831 /* Rare case of a really large header */ 2832 nVarint = sqlite3VarintLen(nHdr); 2833 nHdr += nVarint; 2834 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++; 2835 } 2836 nByte = nHdr+nData; 2837 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 2838 goto too_big; 2839 } 2840 2841 /* Make sure the output register has a buffer large enough to store 2842 ** the new record. The output register (pOp->p3) is not allowed to 2843 ** be one of the input registers (because the following call to 2844 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). 2845 */ 2846 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ 2847 goto no_mem; 2848 } 2849 zNewRecord = (u8 *)pOut->z; 2850 2851 /* Write the record */ 2852 i = putVarint32(zNewRecord, nHdr); 2853 j = nHdr; 2854 assert( pData0<=pLast ); 2855 pRec = pData0; 2856 do{ 2857 serial_type = pRec->uTemp; 2858 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more 2859 ** additional varints, one per column. */ 2860 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ 2861 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record 2862 ** immediately follow the header. */ 2863 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ 2864 }while( (++pRec)<=pLast ); 2865 assert( i==nHdr ); 2866 assert( j==nByte ); 2867 2868 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 2869 pOut->n = (int)nByte; 2870 pOut->flags = MEM_Blob; 2871 if( nZero ){ 2872 pOut->u.nZero = nZero; 2873 pOut->flags |= MEM_Zero; 2874 } 2875 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ 2876 REGISTER_TRACE(pOp->p3, pOut); 2877 UPDATE_MAX_BLOBSIZE(pOut); 2878 break; 2879 } 2880 2881 /* Opcode: Count P1 P2 * * * 2882 ** Synopsis: r[P2]=count() 2883 ** 2884 ** Store the number of entries (an integer value) in the table or index 2885 ** opened by cursor P1 in register P2 2886 */ 2887 #ifndef SQLITE_OMIT_BTREECOUNT 2888 case OP_Count: { /* out2 */ 2889 i64 nEntry; 2890 BtCursor *pCrsr; 2891 2892 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); 2893 pCrsr = p->apCsr[pOp->p1]->uc.pCursor; 2894 assert( pCrsr ); 2895 nEntry = 0; /* Not needed. Only used to silence a warning. */ 2896 rc = sqlite3BtreeCount(pCrsr, &nEntry); 2897 if( rc ) goto abort_due_to_error; 2898 pOut = out2Prerelease(p, pOp); 2899 pOut->u.i = nEntry; 2900 break; 2901 } 2902 #endif 2903 2904 /* Opcode: Savepoint P1 * * P4 * 2905 ** 2906 ** Open, release or rollback the savepoint named by parameter P4, depending 2907 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an 2908 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2. 2909 */ 2910 case OP_Savepoint: { 2911 int p1; /* Value of P1 operand */ 2912 char *zName; /* Name of savepoint */ 2913 int nName; 2914 Savepoint *pNew; 2915 Savepoint *pSavepoint; 2916 Savepoint *pTmp; 2917 int iSavepoint; 2918 int ii; 2919 2920 p1 = pOp->p1; 2921 zName = pOp->p4.z; 2922 2923 /* Assert that the p1 parameter is valid. Also that if there is no open 2924 ** transaction, then there cannot be any savepoints. 2925 */ 2926 assert( db->pSavepoint==0 || db->autoCommit==0 ); 2927 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); 2928 assert( db->pSavepoint || db->isTransactionSavepoint==0 ); 2929 assert( checkSavepointCount(db) ); 2930 assert( p->bIsReader ); 2931 2932 if( p1==SAVEPOINT_BEGIN ){ 2933 if( db->nVdbeWrite>0 ){ 2934 /* A new savepoint cannot be created if there are active write 2935 ** statements (i.e. open read/write incremental blob handles). 2936 */ 2937 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress"); 2938 rc = SQLITE_BUSY; 2939 }else{ 2940 nName = sqlite3Strlen30(zName); 2941 2942 #ifndef SQLITE_OMIT_VIRTUALTABLE 2943 /* This call is Ok even if this savepoint is actually a transaction 2944 ** savepoint (and therefore should not prompt xSavepoint()) callbacks. 2945 ** If this is a transaction savepoint being opened, it is guaranteed 2946 ** that the db->aVTrans[] array is empty. */ 2947 assert( db->autoCommit==0 || db->nVTrans==0 ); 2948 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, 2949 db->nStatement+db->nSavepoint); 2950 if( rc!=SQLITE_OK ) goto abort_due_to_error; 2951 #endif 2952 2953 /* Create a new savepoint structure. */ 2954 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1); 2955 if( pNew ){ 2956 pNew->zName = (char *)&pNew[1]; 2957 memcpy(pNew->zName, zName, nName+1); 2958 2959 /* If there is no open transaction, then mark this as a special 2960 ** "transaction savepoint". */ 2961 if( db->autoCommit ){ 2962 db->autoCommit = 0; 2963 db->isTransactionSavepoint = 1; 2964 }else{ 2965 db->nSavepoint++; 2966 } 2967 2968 /* Link the new savepoint into the database handle's list. */ 2969 pNew->pNext = db->pSavepoint; 2970 db->pSavepoint = pNew; 2971 pNew->nDeferredCons = db->nDeferredCons; 2972 pNew->nDeferredImmCons = db->nDeferredImmCons; 2973 } 2974 } 2975 }else{ 2976 iSavepoint = 0; 2977 2978 /* Find the named savepoint. If there is no such savepoint, then an 2979 ** an error is returned to the user. */ 2980 for( 2981 pSavepoint = db->pSavepoint; 2982 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); 2983 pSavepoint = pSavepoint->pNext 2984 ){ 2985 iSavepoint++; 2986 } 2987 if( !pSavepoint ){ 2988 sqlite3VdbeError(p, "no such savepoint: %s", zName); 2989 rc = SQLITE_ERROR; 2990 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ 2991 /* It is not possible to release (commit) a savepoint if there are 2992 ** active write statements. 2993 */ 2994 sqlite3VdbeError(p, "cannot release savepoint - " 2995 "SQL statements in progress"); 2996 rc = SQLITE_BUSY; 2997 }else{ 2998 2999 /* Determine whether or not this is a transaction savepoint. If so, 3000 ** and this is a RELEASE command, then the current transaction 3001 ** is committed. 3002 */ 3003 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; 3004 if( isTransaction && p1==SAVEPOINT_RELEASE ){ 3005 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ 3006 goto vdbe_return; 3007 } 3008 db->autoCommit = 1; 3009 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ 3010 p->pc = (int)(pOp - aOp); 3011 db->autoCommit = 0; 3012 p->rc = rc = SQLITE_BUSY; 3013 goto vdbe_return; 3014 } 3015 db->isTransactionSavepoint = 0; 3016 rc = p->rc; 3017 }else{ 3018 int isSchemaChange; 3019 iSavepoint = db->nSavepoint - iSavepoint - 1; 3020 if( p1==SAVEPOINT_ROLLBACK ){ 3021 isSchemaChange = (db->flags & SQLITE_InternChanges)!=0; 3022 for(ii=0; ii<db->nDb; ii++){ 3023 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, 3024 SQLITE_ABORT_ROLLBACK, 3025 isSchemaChange==0); 3026 if( rc!=SQLITE_OK ) goto abort_due_to_error; 3027 } 3028 }else{ 3029 isSchemaChange = 0; 3030 } 3031 for(ii=0; ii<db->nDb; ii++){ 3032 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); 3033 if( rc!=SQLITE_OK ){ 3034 goto abort_due_to_error; 3035 } 3036 } 3037 if( isSchemaChange ){ 3038 sqlite3ExpirePreparedStatements(db); 3039 sqlite3ResetAllSchemasOfConnection(db); 3040 db->flags = (db->flags | SQLITE_InternChanges); 3041 } 3042 } 3043 3044 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all 3045 ** savepoints nested inside of the savepoint being operated on. */ 3046 while( db->pSavepoint!=pSavepoint ){ 3047 pTmp = db->pSavepoint; 3048 db->pSavepoint = pTmp->pNext; 3049 sqlite3DbFree(db, pTmp); 3050 db->nSavepoint--; 3051 } 3052 3053 /* If it is a RELEASE, then destroy the savepoint being operated on 3054 ** too. If it is a ROLLBACK TO, then set the number of deferred 3055 ** constraint violations present in the database to the value stored 3056 ** when the savepoint was created. */ 3057 if( p1==SAVEPOINT_RELEASE ){ 3058 assert( pSavepoint==db->pSavepoint ); 3059 db->pSavepoint = pSavepoint->pNext; 3060 sqlite3DbFree(db, pSavepoint); 3061 if( !isTransaction ){ 3062 db->nSavepoint--; 3063 } 3064 }else{ 3065 db->nDeferredCons = pSavepoint->nDeferredCons; 3066 db->nDeferredImmCons = pSavepoint->nDeferredImmCons; 3067 } 3068 3069 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ 3070 rc = sqlite3VtabSavepoint(db, p1, iSavepoint); 3071 if( rc!=SQLITE_OK ) goto abort_due_to_error; 3072 } 3073 } 3074 } 3075 if( rc ) goto abort_due_to_error; 3076 3077 break; 3078 } 3079 3080 /* Opcode: AutoCommit P1 P2 * * * 3081 ** 3082 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll 3083 ** back any currently active btree transactions. If there are any active 3084 ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if 3085 ** there are active writing VMs or active VMs that use shared cache. 3086 ** 3087 ** This instruction causes the VM to halt. 3088 */ 3089 case OP_AutoCommit: { 3090 int desiredAutoCommit; 3091 int iRollback; 3092 3093 desiredAutoCommit = pOp->p1; 3094 iRollback = pOp->p2; 3095 assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); 3096 assert( desiredAutoCommit==1 || iRollback==0 ); 3097 assert( db->nVdbeActive>0 ); /* At least this one VM is active */ 3098 assert( p->bIsReader ); 3099 3100 if( desiredAutoCommit!=db->autoCommit ){ 3101 if( iRollback ){ 3102 assert( desiredAutoCommit==1 ); 3103 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); 3104 db->autoCommit = 1; 3105 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){ 3106 /* If this instruction implements a COMMIT and other VMs are writing 3107 ** return an error indicating that the other VMs must complete first. 3108 */ 3109 sqlite3VdbeError(p, "cannot commit transaction - " 3110 "SQL statements in progress"); 3111 rc = SQLITE_BUSY; 3112 goto abort_due_to_error; 3113 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ 3114 goto vdbe_return; 3115 }else{ 3116 db->autoCommit = (u8)desiredAutoCommit; 3117 } 3118 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ 3119 p->pc = (int)(pOp - aOp); 3120 db->autoCommit = (u8)(1-desiredAutoCommit); 3121 p->rc = rc = SQLITE_BUSY; 3122 goto vdbe_return; 3123 } 3124 assert( db->nStatement==0 ); 3125 sqlite3CloseSavepoints(db); 3126 if( p->rc==SQLITE_OK ){ 3127 rc = SQLITE_DONE; 3128 }else{ 3129 rc = SQLITE_ERROR; 3130 } 3131 goto vdbe_return; 3132 }else{ 3133 sqlite3VdbeError(p, 3134 (!desiredAutoCommit)?"cannot start a transaction within a transaction":( 3135 (iRollback)?"cannot rollback - no transaction is active": 3136 "cannot commit - no transaction is active")); 3137 3138 rc = SQLITE_ERROR; 3139 goto abort_due_to_error; 3140 } 3141 break; 3142 } 3143 3144 /* Opcode: Transaction P1 P2 P3 P4 P5 3145 ** 3146 ** Begin a transaction on database P1 if a transaction is not already 3147 ** active. 3148 ** If P2 is non-zero, then a write-transaction is started, or if a 3149 ** read-transaction is already active, it is upgraded to a write-transaction. 3150 ** If P2 is zero, then a read-transaction is started. 3151 ** 3152 ** P1 is the index of the database file on which the transaction is 3153 ** started. Index 0 is the main database file and index 1 is the 3154 ** file used for temporary tables. Indices of 2 or more are used for 3155 ** attached databases. 3156 ** 3157 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is 3158 ** true (this flag is set if the Vdbe may modify more than one row and may 3159 ** throw an ABORT exception), a statement transaction may also be opened. 3160 ** More specifically, a statement transaction is opened iff the database 3161 ** connection is currently not in autocommit mode, or if there are other 3162 ** active statements. A statement transaction allows the changes made by this 3163 ** VDBE to be rolled back after an error without having to roll back the 3164 ** entire transaction. If no error is encountered, the statement transaction 3165 ** will automatically commit when the VDBE halts. 3166 ** 3167 ** If P5!=0 then this opcode also checks the schema cookie against P3 3168 ** and the schema generation counter against P4. 3169 ** The cookie changes its value whenever the database schema changes. 3170 ** This operation is used to detect when that the cookie has changed 3171 ** and that the current process needs to reread the schema. If the schema 3172 ** cookie in P3 differs from the schema cookie in the database header or 3173 ** if the schema generation counter in P4 differs from the current 3174 ** generation counter, then an SQLITE_SCHEMA error is raised and execution 3175 ** halts. The sqlite3_step() wrapper function might then reprepare the 3176 ** statement and rerun it from the beginning. 3177 */ 3178 case OP_Transaction: { 3179 Btree *pBt; 3180 int iMeta; 3181 int iGen; 3182 3183 assert( p->bIsReader ); 3184 assert( p->readOnly==0 || pOp->p2==0 ); 3185 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 3186 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 3187 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){ 3188 rc = SQLITE_READONLY; 3189 goto abort_due_to_error; 3190 } 3191 pBt = db->aDb[pOp->p1].pBt; 3192 3193 if( pBt ){ 3194 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); 3195 testcase( rc==SQLITE_BUSY_SNAPSHOT ); 3196 testcase( rc==SQLITE_BUSY_RECOVERY ); 3197 if( rc!=SQLITE_OK ){ 3198 if( (rc&0xff)==SQLITE_BUSY ){ 3199 p->pc = (int)(pOp - aOp); 3200 p->rc = rc; 3201 goto vdbe_return; 3202 } 3203 goto abort_due_to_error; 3204 } 3205 3206 if( pOp->p2 && p->usesStmtJournal 3207 && (db->autoCommit==0 || db->nVdbeRead>1) 3208 ){ 3209 assert( sqlite3BtreeIsInTrans(pBt) ); 3210 if( p->iStatement==0 ){ 3211 assert( db->nStatement>=0 && db->nSavepoint>=0 ); 3212 db->nStatement++; 3213 p->iStatement = db->nSavepoint + db->nStatement; 3214 } 3215 3216 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); 3217 if( rc==SQLITE_OK ){ 3218 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); 3219 } 3220 3221 /* Store the current value of the database handles deferred constraint 3222 ** counter. If the statement transaction needs to be rolled back, 3223 ** the value of this counter needs to be restored too. */ 3224 p->nStmtDefCons = db->nDeferredCons; 3225 p->nStmtDefImmCons = db->nDeferredImmCons; 3226 } 3227 3228 /* Gather the schema version number for checking: 3229 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema 3230 ** version is checked to ensure that the schema has not changed since the 3231 ** SQL statement was prepared. 3232 */ 3233 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta); 3234 iGen = db->aDb[pOp->p1].pSchema->iGeneration; 3235 }else{ 3236 iGen = iMeta = 0; 3237 } 3238 assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); 3239 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){ 3240 sqlite3DbFree(db, p->zErrMsg); 3241 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); 3242 /* If the schema-cookie from the database file matches the cookie 3243 ** stored with the in-memory representation of the schema, do 3244 ** not reload the schema from the database file. 3245 ** 3246 ** If virtual-tables are in use, this is not just an optimization. 3247 ** Often, v-tables store their data in other SQLite tables, which 3248 ** are queried from within xNext() and other v-table methods using 3249 ** prepared queries. If such a query is out-of-date, we do not want to 3250 ** discard the database schema, as the user code implementing the 3251 ** v-table would have to be ready for the sqlite3_vtab structure itself 3252 ** to be invalidated whenever sqlite3_step() is called from within 3253 ** a v-table method. 3254 */ 3255 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ 3256 sqlite3ResetOneSchema(db, pOp->p1); 3257 } 3258 p->expired = 1; 3259 rc = SQLITE_SCHEMA; 3260 } 3261 if( rc ) goto abort_due_to_error; 3262 break; 3263 } 3264 3265 /* Opcode: ReadCookie P1 P2 P3 * * 3266 ** 3267 ** Read cookie number P3 from database P1 and write it into register P2. 3268 ** P3==1 is the schema version. P3==2 is the database format. 3269 ** P3==3 is the recommended pager cache size, and so forth. P1==0 is 3270 ** the main database file and P1==1 is the database file used to store 3271 ** temporary tables. 3272 ** 3273 ** There must be a read-lock on the database (either a transaction 3274 ** must be started or there must be an open cursor) before 3275 ** executing this instruction. 3276 */ 3277 case OP_ReadCookie: { /* out2 */ 3278 int iMeta; 3279 int iDb; 3280 int iCookie; 3281 3282 assert( p->bIsReader ); 3283 iDb = pOp->p1; 3284 iCookie = pOp->p3; 3285 assert( pOp->p3<SQLITE_N_BTREE_META ); 3286 assert( iDb>=0 && iDb<db->nDb ); 3287 assert( db->aDb[iDb].pBt!=0 ); 3288 assert( DbMaskTest(p->btreeMask, iDb) ); 3289 3290 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); 3291 pOut = out2Prerelease(p, pOp); 3292 pOut->u.i = iMeta; 3293 break; 3294 } 3295 3296 /* Opcode: SetCookie P1 P2 P3 * * 3297 ** 3298 ** Write the integer value P3 into cookie number P2 of database P1. 3299 ** P2==1 is the schema version. P2==2 is the database format. 3300 ** P2==3 is the recommended pager cache 3301 ** size, and so forth. P1==0 is the main database file and P1==1 is the 3302 ** database file used to store temporary tables. 3303 ** 3304 ** A transaction must be started before executing this opcode. 3305 */ 3306 case OP_SetCookie: { 3307 Db *pDb; 3308 assert( pOp->p2<SQLITE_N_BTREE_META ); 3309 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 3310 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 3311 assert( p->readOnly==0 ); 3312 pDb = &db->aDb[pOp->p1]; 3313 assert( pDb->pBt!=0 ); 3314 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); 3315 /* See note about index shifting on OP_ReadCookie */ 3316 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3); 3317 if( pOp->p2==BTREE_SCHEMA_VERSION ){ 3318 /* When the schema cookie changes, record the new cookie internally */ 3319 pDb->pSchema->schema_cookie = pOp->p3; 3320 db->flags |= SQLITE_InternChanges; 3321 }else if( pOp->p2==BTREE_FILE_FORMAT ){ 3322 /* Record changes in the file format */ 3323 pDb->pSchema->file_format = pOp->p3; 3324 } 3325 if( pOp->p1==1 ){ 3326 /* Invalidate all prepared statements whenever the TEMP database 3327 ** schema is changed. Ticket #1644 */ 3328 sqlite3ExpirePreparedStatements(db); 3329 p->expired = 0; 3330 } 3331 if( rc ) goto abort_due_to_error; 3332 break; 3333 } 3334 3335 /* Opcode: OpenRead P1 P2 P3 P4 P5 3336 ** Synopsis: root=P2 iDb=P3 3337 ** 3338 ** Open a read-only cursor for the database table whose root page is 3339 ** P2 in a database file. The database file is determined by P3. 3340 ** P3==0 means the main database, P3==1 means the database used for 3341 ** temporary tables, and P3>1 means used the corresponding attached 3342 ** database. Give the new cursor an identifier of P1. The P1 3343 ** values need not be contiguous but all P1 values should be small integers. 3344 ** It is an error for P1 to be negative. 3345 ** 3346 ** If P5!=0 then use the content of register P2 as the root page, not 3347 ** the value of P2 itself. 3348 ** 3349 ** There will be a read lock on the database whenever there is an 3350 ** open cursor. If the database was unlocked prior to this instruction 3351 ** then a read lock is acquired as part of this instruction. A read 3352 ** lock allows other processes to read the database but prohibits 3353 ** any other process from modifying the database. The read lock is 3354 ** released when all cursors are closed. If this instruction attempts 3355 ** to get a read lock but fails, the script terminates with an 3356 ** SQLITE_BUSY error code. 3357 ** 3358 ** The P4 value may be either an integer (P4_INT32) or a pointer to 3359 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 3360 ** structure, then said structure defines the content and collating 3361 ** sequence of the index being opened. Otherwise, if P4 is an integer 3362 ** value, it is set to the number of columns in the table. 3363 ** 3364 ** See also: OpenWrite, ReopenIdx 3365 */ 3366 /* Opcode: ReopenIdx P1 P2 P3 P4 P5 3367 ** Synopsis: root=P2 iDb=P3 3368 ** 3369 ** The ReopenIdx opcode works exactly like ReadOpen except that it first 3370 ** checks to see if the cursor on P1 is already open with a root page 3371 ** number of P2 and if it is this opcode becomes a no-op. In other words, 3372 ** if the cursor is already open, do not reopen it. 3373 ** 3374 ** The ReopenIdx opcode may only be used with P5==0 and with P4 being 3375 ** a P4_KEYINFO object. Furthermore, the P3 value must be the same as 3376 ** every other ReopenIdx or OpenRead for the same cursor number. 3377 ** 3378 ** See the OpenRead opcode documentation for additional information. 3379 */ 3380 /* Opcode: OpenWrite P1 P2 P3 P4 P5 3381 ** Synopsis: root=P2 iDb=P3 3382 ** 3383 ** Open a read/write cursor named P1 on the table or index whose root 3384 ** page is P2. Or if P5!=0 use the content of register P2 to find the 3385 ** root page. 3386 ** 3387 ** The P4 value may be either an integer (P4_INT32) or a pointer to 3388 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 3389 ** structure, then said structure defines the content and collating 3390 ** sequence of the index being opened. Otherwise, if P4 is an integer 3391 ** value, it is set to the number of columns in the table, or to the 3392 ** largest index of any column of the table that is actually used. 3393 ** 3394 ** This instruction works just like OpenRead except that it opens the cursor 3395 ** in read/write mode. For a given table, there can be one or more read-only 3396 ** cursors or a single read/write cursor but not both. 3397 ** 3398 ** See also OpenRead. 3399 */ 3400 case OP_ReopenIdx: { 3401 int nField; 3402 KeyInfo *pKeyInfo; 3403 int p2; 3404 int iDb; 3405 int wrFlag; 3406 Btree *pX; 3407 VdbeCursor *pCur; 3408 Db *pDb; 3409 3410 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); 3411 assert( pOp->p4type==P4_KEYINFO ); 3412 pCur = p->apCsr[pOp->p1]; 3413 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ 3414 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ 3415 goto open_cursor_set_hints; 3416 } 3417 /* If the cursor is not currently open or is open on a different 3418 ** index, then fall through into OP_OpenRead to force a reopen */ 3419 case OP_OpenRead: 3420 case OP_OpenWrite: 3421 3422 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); 3423 assert( p->bIsReader ); 3424 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx 3425 || p->readOnly==0 ); 3426 3427 if( p->expired ){ 3428 rc = SQLITE_ABORT_ROLLBACK; 3429 goto abort_due_to_error; 3430 } 3431 3432 nField = 0; 3433 pKeyInfo = 0; 3434 p2 = pOp->p2; 3435 iDb = pOp->p3; 3436 assert( iDb>=0 && iDb<db->nDb ); 3437 assert( DbMaskTest(p->btreeMask, iDb) ); 3438 pDb = &db->aDb[iDb]; 3439 pX = pDb->pBt; 3440 assert( pX!=0 ); 3441 if( pOp->opcode==OP_OpenWrite ){ 3442 assert( OPFLAG_FORDELETE==BTREE_FORDELETE ); 3443 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE); 3444 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 3445 if( pDb->pSchema->file_format < p->minWriteFileFormat ){ 3446 p->minWriteFileFormat = pDb->pSchema->file_format; 3447 } 3448 }else{ 3449 wrFlag = 0; 3450 } 3451 if( pOp->p5 & OPFLAG_P2ISREG ){ 3452 assert( p2>0 ); 3453 assert( p2<=(p->nMem+1 - p->nCursor) ); 3454 pIn2 = &aMem[p2]; 3455 assert( memIsValid(pIn2) ); 3456 assert( (pIn2->flags & MEM_Int)!=0 ); 3457 sqlite3VdbeMemIntegerify(pIn2); 3458 p2 = (int)pIn2->u.i; 3459 /* The p2 value always comes from a prior OP_CreateTable opcode and 3460 ** that opcode will always set the p2 value to 2 or more or else fail. 3461 ** If there were a failure, the prepared statement would have halted 3462 ** before reaching this instruction. */ 3463 assert( p2>=2 ); 3464 } 3465 if( pOp->p4type==P4_KEYINFO ){ 3466 pKeyInfo = pOp->p4.pKeyInfo; 3467 assert( pKeyInfo->enc==ENC(db) ); 3468 assert( pKeyInfo->db==db ); 3469 nField = pKeyInfo->nField+pKeyInfo->nXField; 3470 }else if( pOp->p4type==P4_INT32 ){ 3471 nField = pOp->p4.i; 3472 } 3473 assert( pOp->p1>=0 ); 3474 assert( nField>=0 ); 3475 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ 3476 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE); 3477 if( pCur==0 ) goto no_mem; 3478 pCur->nullRow = 1; 3479 pCur->isOrdered = 1; 3480 pCur->pgnoRoot = p2; 3481 #ifdef SQLITE_DEBUG 3482 pCur->wrFlag = wrFlag; 3483 #endif 3484 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor); 3485 pCur->pKeyInfo = pKeyInfo; 3486 /* Set the VdbeCursor.isTable variable. Previous versions of 3487 ** SQLite used to check if the root-page flags were sane at this point 3488 ** and report database corruption if they were not, but this check has 3489 ** since moved into the btree layer. */ 3490 pCur->isTable = pOp->p4type!=P4_KEYINFO; 3491 3492 open_cursor_set_hints: 3493 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); 3494 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); 3495 testcase( pOp->p5 & OPFLAG_BULKCSR ); 3496 #ifdef SQLITE_ENABLE_CURSOR_HINTS 3497 testcase( pOp->p2 & OPFLAG_SEEKEQ ); 3498 #endif 3499 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor, 3500 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); 3501 if( rc ) goto abort_due_to_error; 3502 break; 3503 } 3504 3505 /* Opcode: OpenEphemeral P1 P2 * P4 P5 3506 ** Synopsis: nColumn=P2 3507 ** 3508 ** Open a new cursor P1 to a transient table. 3509 ** The cursor is always opened read/write even if 3510 ** the main database is read-only. The ephemeral 3511 ** table is deleted automatically when the cursor is closed. 3512 ** 3513 ** P2 is the number of columns in the ephemeral table. 3514 ** The cursor points to a BTree table if P4==0 and to a BTree index 3515 ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure 3516 ** that defines the format of keys in the index. 3517 ** 3518 ** The P5 parameter can be a mask of the BTREE_* flags defined 3519 ** in btree.h. These flags control aspects of the operation of 3520 ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are 3521 ** added automatically. 3522 */ 3523 /* Opcode: OpenAutoindex P1 P2 * P4 * 3524 ** Synopsis: nColumn=P2 3525 ** 3526 ** This opcode works the same as OP_OpenEphemeral. It has a 3527 ** different name to distinguish its use. Tables created using 3528 ** by this opcode will be used for automatically created transient 3529 ** indices in joins. 3530 */ 3531 case OP_OpenAutoindex: 3532 case OP_OpenEphemeral: { 3533 VdbeCursor *pCx; 3534 KeyInfo *pKeyInfo; 3535 3536 static const int vfsFlags = 3537 SQLITE_OPEN_READWRITE | 3538 SQLITE_OPEN_CREATE | 3539 SQLITE_OPEN_EXCLUSIVE | 3540 SQLITE_OPEN_DELETEONCLOSE | 3541 SQLITE_OPEN_TRANSIENT_DB; 3542 assert( pOp->p1>=0 ); 3543 assert( pOp->p2>=0 ); 3544 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE); 3545 if( pCx==0 ) goto no_mem; 3546 pCx->nullRow = 1; 3547 pCx->isEphemeral = 1; 3548 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx, 3549 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags); 3550 if( rc==SQLITE_OK ){ 3551 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1); 3552 } 3553 if( rc==SQLITE_OK ){ 3554 /* If a transient index is required, create it by calling 3555 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before 3556 ** opening it. If a transient table is required, just use the 3557 ** automatically created table with root-page 1 (an BLOB_INTKEY table). 3558 */ 3559 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ 3560 int pgno; 3561 assert( pOp->p4type==P4_KEYINFO ); 3562 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5); 3563 if( rc==SQLITE_OK ){ 3564 assert( pgno==MASTER_ROOT+1 ); 3565 assert( pKeyInfo->db==db ); 3566 assert( pKeyInfo->enc==ENC(db) ); 3567 rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR, 3568 pKeyInfo, pCx->uc.pCursor); 3569 } 3570 pCx->isTable = 0; 3571 }else{ 3572 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR, 3573 0, pCx->uc.pCursor); 3574 pCx->isTable = 1; 3575 } 3576 } 3577 if( rc ) goto abort_due_to_error; 3578 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); 3579 break; 3580 } 3581 3582 /* Opcode: SorterOpen P1 P2 P3 P4 * 3583 ** 3584 ** This opcode works like OP_OpenEphemeral except that it opens 3585 ** a transient index that is specifically designed to sort large 3586 ** tables using an external merge-sort algorithm. 3587 ** 3588 ** If argument P3 is non-zero, then it indicates that the sorter may 3589 ** assume that a stable sort considering the first P3 fields of each 3590 ** key is sufficient to produce the required results. 3591 */ 3592 case OP_SorterOpen: { 3593 VdbeCursor *pCx; 3594 3595 assert( pOp->p1>=0 ); 3596 assert( pOp->p2>=0 ); 3597 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER); 3598 if( pCx==0 ) goto no_mem; 3599 pCx->pKeyInfo = pOp->p4.pKeyInfo; 3600 assert( pCx->pKeyInfo->db==db ); 3601 assert( pCx->pKeyInfo->enc==ENC(db) ); 3602 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); 3603 if( rc ) goto abort_due_to_error; 3604 break; 3605 } 3606 3607 /* Opcode: SequenceTest P1 P2 * * * 3608 ** Synopsis: if( cursor[P1].ctr++ ) pc = P2 3609 ** 3610 ** P1 is a sorter cursor. If the sequence counter is currently zero, jump 3611 ** to P2. Regardless of whether or not the jump is taken, increment the 3612 ** the sequence value. 3613 */ 3614 case OP_SequenceTest: { 3615 VdbeCursor *pC; 3616 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 3617 pC = p->apCsr[pOp->p1]; 3618 assert( isSorter(pC) ); 3619 if( (pC->seqCount++)==0 ){ 3620 goto jump_to_p2; 3621 } 3622 break; 3623 } 3624 3625 /* Opcode: OpenPseudo P1 P2 P3 * * 3626 ** Synopsis: P3 columns in r[P2] 3627 ** 3628 ** Open a new cursor that points to a fake table that contains a single 3629 ** row of data. The content of that one row is the content of memory 3630 ** register P2. In other words, cursor P1 becomes an alias for the 3631 ** MEM_Blob content contained in register P2. 3632 ** 3633 ** A pseudo-table created by this opcode is used to hold a single 3634 ** row output from the sorter so that the row can be decomposed into 3635 ** individual columns using the OP_Column opcode. The OP_Column opcode 3636 ** is the only cursor opcode that works with a pseudo-table. 3637 ** 3638 ** P3 is the number of fields in the records that will be stored by 3639 ** the pseudo-table. 3640 */ 3641 case OP_OpenPseudo: { 3642 VdbeCursor *pCx; 3643 3644 assert( pOp->p1>=0 ); 3645 assert( pOp->p3>=0 ); 3646 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO); 3647 if( pCx==0 ) goto no_mem; 3648 pCx->nullRow = 1; 3649 pCx->uc.pseudoTableReg = pOp->p2; 3650 pCx->isTable = 1; 3651 assert( pOp->p5==0 ); 3652 break; 3653 } 3654 3655 /* Opcode: Close P1 * * * * 3656 ** 3657 ** Close a cursor previously opened as P1. If P1 is not 3658 ** currently open, this instruction is a no-op. 3659 */ 3660 case OP_Close: { 3661 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 3662 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); 3663 p->apCsr[pOp->p1] = 0; 3664 break; 3665 } 3666 3667 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK 3668 /* Opcode: ColumnsUsed P1 * * P4 * 3669 ** 3670 ** This opcode (which only exists if SQLite was compiled with 3671 ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the 3672 ** table or index for cursor P1 are used. P4 is a 64-bit integer 3673 ** (P4_INT64) in which the first 63 bits are one for each of the 3674 ** first 63 columns of the table or index that are actually used 3675 ** by the cursor. The high-order bit is set if any column after 3676 ** the 64th is used. 3677 */ 3678 case OP_ColumnsUsed: { 3679 VdbeCursor *pC; 3680 pC = p->apCsr[pOp->p1]; 3681 assert( pC->eCurType==CURTYPE_BTREE ); 3682 pC->maskUsed = *(u64*)pOp->p4.pI64; 3683 break; 3684 } 3685 #endif 3686 3687 /* Opcode: SeekGE P1 P2 P3 P4 * 3688 ** Synopsis: key=r[P3@P4] 3689 ** 3690 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 3691 ** use the value in register P3 as the key. If cursor P1 refers 3692 ** to an SQL index, then P3 is the first in an array of P4 registers 3693 ** that are used as an unpacked index key. 3694 ** 3695 ** Reposition cursor P1 so that it points to the smallest entry that 3696 ** is greater than or equal to the key value. If there are no records 3697 ** greater than or equal to the key and P2 is not zero, then jump to P2. 3698 ** 3699 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this 3700 ** opcode will always land on a record that equally equals the key, or 3701 ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this 3702 ** opcode must be followed by an IdxLE opcode with the same arguments. 3703 ** The IdxLE opcode will be skipped if this opcode succeeds, but the 3704 ** IdxLE opcode will be used on subsequent loop iterations. 3705 ** 3706 ** This opcode leaves the cursor configured to move in forward order, 3707 ** from the beginning toward the end. In other words, the cursor is 3708 ** configured to use Next, not Prev. 3709 ** 3710 ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe 3711 */ 3712 /* Opcode: SeekGT P1 P2 P3 P4 * 3713 ** Synopsis: key=r[P3@P4] 3714 ** 3715 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 3716 ** use the value in register P3 as a key. If cursor P1 refers 3717 ** to an SQL index, then P3 is the first in an array of P4 registers 3718 ** that are used as an unpacked index key. 3719 ** 3720 ** Reposition cursor P1 so that it points to the smallest entry that 3721 ** is greater than the key value. If there are no records greater than 3722 ** the key and P2 is not zero, then jump to P2. 3723 ** 3724 ** This opcode leaves the cursor configured to move in forward order, 3725 ** from the beginning toward the end. In other words, the cursor is 3726 ** configured to use Next, not Prev. 3727 ** 3728 ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe 3729 */ 3730 /* Opcode: SeekLT P1 P2 P3 P4 * 3731 ** Synopsis: key=r[P3@P4] 3732 ** 3733 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 3734 ** use the value in register P3 as a key. If cursor P1 refers 3735 ** to an SQL index, then P3 is the first in an array of P4 registers 3736 ** that are used as an unpacked index key. 3737 ** 3738 ** Reposition cursor P1 so that it points to the largest entry that 3739 ** is less than the key value. If there are no records less than 3740 ** the key and P2 is not zero, then jump to P2. 3741 ** 3742 ** This opcode leaves the cursor configured to move in reverse order, 3743 ** from the end toward the beginning. In other words, the cursor is 3744 ** configured to use Prev, not Next. 3745 ** 3746 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe 3747 */ 3748 /* Opcode: SeekLE P1 P2 P3 P4 * 3749 ** Synopsis: key=r[P3@P4] 3750 ** 3751 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 3752 ** use the value in register P3 as a key. If cursor P1 refers 3753 ** to an SQL index, then P3 is the first in an array of P4 registers 3754 ** that are used as an unpacked index key. 3755 ** 3756 ** Reposition cursor P1 so that it points to the largest entry that 3757 ** is less than or equal to the key value. If there are no records 3758 ** less than or equal to the key and P2 is not zero, then jump to P2. 3759 ** 3760 ** This opcode leaves the cursor configured to move in reverse order, 3761 ** from the end toward the beginning. In other words, the cursor is 3762 ** configured to use Prev, not Next. 3763 ** 3764 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this 3765 ** opcode will always land on a record that equally equals the key, or 3766 ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this 3767 ** opcode must be followed by an IdxGE opcode with the same arguments. 3768 ** The IdxGE opcode will be skipped if this opcode succeeds, but the 3769 ** IdxGE opcode will be used on subsequent loop iterations. 3770 ** 3771 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt 3772 */ 3773 case OP_SeekLT: /* jump, in3 */ 3774 case OP_SeekLE: /* jump, in3 */ 3775 case OP_SeekGE: /* jump, in3 */ 3776 case OP_SeekGT: { /* jump, in3 */ 3777 int res; /* Comparison result */ 3778 int oc; /* Opcode */ 3779 VdbeCursor *pC; /* The cursor to seek */ 3780 UnpackedRecord r; /* The key to seek for */ 3781 int nField; /* Number of columns or fields in the key */ 3782 i64 iKey; /* The rowid we are to seek to */ 3783 int eqOnly; /* Only interested in == results */ 3784 3785 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 3786 assert( pOp->p2!=0 ); 3787 pC = p->apCsr[pOp->p1]; 3788 assert( pC!=0 ); 3789 assert( pC->eCurType==CURTYPE_BTREE ); 3790 assert( OP_SeekLE == OP_SeekLT+1 ); 3791 assert( OP_SeekGE == OP_SeekLT+2 ); 3792 assert( OP_SeekGT == OP_SeekLT+3 ); 3793 assert( pC->isOrdered ); 3794 assert( pC->uc.pCursor!=0 ); 3795 oc = pOp->opcode; 3796 eqOnly = 0; 3797 pC->nullRow = 0; 3798 #ifdef SQLITE_DEBUG 3799 pC->seekOp = pOp->opcode; 3800 #endif 3801 3802 if( pC->isTable ){ 3803 /* The BTREE_SEEK_EQ flag is only set on index cursors */ 3804 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 3805 || CORRUPT_DB ); 3806 3807 /* The input value in P3 might be of any type: integer, real, string, 3808 ** blob, or NULL. But it needs to be an integer before we can do 3809 ** the seek, so convert it. */ 3810 pIn3 = &aMem[pOp->p3]; 3811 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ 3812 applyNumericAffinity(pIn3, 0); 3813 } 3814 iKey = sqlite3VdbeIntValue(pIn3); 3815 3816 /* If the P3 value could not be converted into an integer without 3817 ** loss of information, then special processing is required... */ 3818 if( (pIn3->flags & MEM_Int)==0 ){ 3819 if( (pIn3->flags & MEM_Real)==0 ){ 3820 /* If the P3 value cannot be converted into any kind of a number, 3821 ** then the seek is not possible, so jump to P2 */ 3822 VdbeBranchTaken(1,2); goto jump_to_p2; 3823 break; 3824 } 3825 3826 /* If the approximation iKey is larger than the actual real search 3827 ** term, substitute >= for > and < for <=. e.g. if the search term 3828 ** is 4.9 and the integer approximation 5: 3829 ** 3830 ** (x > 4.9) -> (x >= 5) 3831 ** (x <= 4.9) -> (x < 5) 3832 */ 3833 if( pIn3->u.r<(double)iKey ){ 3834 assert( OP_SeekGE==(OP_SeekGT-1) ); 3835 assert( OP_SeekLT==(OP_SeekLE-1) ); 3836 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) ); 3837 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--; 3838 } 3839 3840 /* If the approximation iKey is smaller than the actual real search 3841 ** term, substitute <= for < and > for >=. */ 3842 else if( pIn3->u.r>(double)iKey ){ 3843 assert( OP_SeekLE==(OP_SeekLT+1) ); 3844 assert( OP_SeekGT==(OP_SeekGE+1) ); 3845 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); 3846 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; 3847 } 3848 } 3849 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res); 3850 pC->movetoTarget = iKey; /* Used by OP_Delete */ 3851 if( rc!=SQLITE_OK ){ 3852 goto abort_due_to_error; 3853 } 3854 }else{ 3855 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and 3856 ** OP_SeekLE opcodes are allowed, and these must be immediately followed 3857 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key. 3858 */ 3859 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){ 3860 eqOnly = 1; 3861 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE ); 3862 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); 3863 assert( pOp[1].p1==pOp[0].p1 ); 3864 assert( pOp[1].p2==pOp[0].p2 ); 3865 assert( pOp[1].p3==pOp[0].p3 ); 3866 assert( pOp[1].p4.i==pOp[0].p4.i ); 3867 } 3868 3869 nField = pOp->p4.i; 3870 assert( pOp->p4type==P4_INT32 ); 3871 assert( nField>0 ); 3872 r.pKeyInfo = pC->pKeyInfo; 3873 r.nField = (u16)nField; 3874 3875 /* The next line of code computes as follows, only faster: 3876 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ 3877 ** r.default_rc = -1; 3878 ** }else{ 3879 ** r.default_rc = +1; 3880 ** } 3881 */ 3882 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); 3883 assert( oc!=OP_SeekGT || r.default_rc==-1 ); 3884 assert( oc!=OP_SeekLE || r.default_rc==-1 ); 3885 assert( oc!=OP_SeekGE || r.default_rc==+1 ); 3886 assert( oc!=OP_SeekLT || r.default_rc==+1 ); 3887 3888 r.aMem = &aMem[pOp->p3]; 3889 #ifdef SQLITE_DEBUG 3890 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } 3891 #endif 3892 r.eqSeen = 0; 3893 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res); 3894 if( rc!=SQLITE_OK ){ 3895 goto abort_due_to_error; 3896 } 3897 if( eqOnly && r.eqSeen==0 ){ 3898 assert( res!=0 ); 3899 goto seek_not_found; 3900 } 3901 } 3902 pC->deferredMoveto = 0; 3903 pC->cacheStatus = CACHE_STALE; 3904 #ifdef SQLITE_TEST 3905 sqlite3_search_count++; 3906 #endif 3907 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); 3908 if( res<0 || (res==0 && oc==OP_SeekGT) ){ 3909 res = 0; 3910 rc = sqlite3BtreeNext(pC->uc.pCursor, &res); 3911 if( rc!=SQLITE_OK ) goto abort_due_to_error; 3912 }else{ 3913 res = 0; 3914 } 3915 }else{ 3916 assert( oc==OP_SeekLT || oc==OP_SeekLE ); 3917 if( res>0 || (res==0 && oc==OP_SeekLT) ){ 3918 res = 0; 3919 rc = sqlite3BtreePrevious(pC->uc.pCursor, &res); 3920 if( rc!=SQLITE_OK ) goto abort_due_to_error; 3921 }else{ 3922 /* res might be negative because the table is empty. Check to 3923 ** see if this is the case. 3924 */ 3925 res = sqlite3BtreeEof(pC->uc.pCursor); 3926 } 3927 } 3928 seek_not_found: 3929 assert( pOp->p2>0 ); 3930 VdbeBranchTaken(res!=0,2); 3931 if( res ){ 3932 goto jump_to_p2; 3933 }else if( eqOnly ){ 3934 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); 3935 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */ 3936 } 3937 break; 3938 } 3939 3940 /* Opcode: Found P1 P2 P3 P4 * 3941 ** Synopsis: key=r[P3@P4] 3942 ** 3943 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If 3944 ** P4>0 then register P3 is the first of P4 registers that form an unpacked 3945 ** record. 3946 ** 3947 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 3948 ** is a prefix of any entry in P1 then a jump is made to P2 and 3949 ** P1 is left pointing at the matching entry. 3950 ** 3951 ** This operation leaves the cursor in a state where it can be 3952 ** advanced in the forward direction. The Next instruction will work, 3953 ** but not the Prev instruction. 3954 ** 3955 ** See also: NotFound, NoConflict, NotExists. SeekGe 3956 */ 3957 /* Opcode: NotFound P1 P2 P3 P4 * 3958 ** Synopsis: key=r[P3@P4] 3959 ** 3960 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If 3961 ** P4>0 then register P3 is the first of P4 registers that form an unpacked 3962 ** record. 3963 ** 3964 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 3965 ** is not the prefix of any entry in P1 then a jump is made to P2. If P1 3966 ** does contain an entry whose prefix matches the P3/P4 record then control 3967 ** falls through to the next instruction and P1 is left pointing at the 3968 ** matching entry. 3969 ** 3970 ** This operation leaves the cursor in a state where it cannot be 3971 ** advanced in either direction. In other words, the Next and Prev 3972 ** opcodes do not work after this operation. 3973 ** 3974 ** See also: Found, NotExists, NoConflict 3975 */ 3976 /* Opcode: NoConflict P1 P2 P3 P4 * 3977 ** Synopsis: key=r[P3@P4] 3978 ** 3979 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If 3980 ** P4>0 then register P3 is the first of P4 registers that form an unpacked 3981 ** record. 3982 ** 3983 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 3984 ** contains any NULL value, jump immediately to P2. If all terms of the 3985 ** record are not-NULL then a check is done to determine if any row in the 3986 ** P1 index btree has a matching key prefix. If there are no matches, jump 3987 ** immediately to P2. If there is a match, fall through and leave the P1 3988 ** cursor pointing to the matching row. 3989 ** 3990 ** This opcode is similar to OP_NotFound with the exceptions that the 3991 ** branch is always taken if any part of the search key input is NULL. 3992 ** 3993 ** This operation leaves the cursor in a state where it cannot be 3994 ** advanced in either direction. In other words, the Next and Prev 3995 ** opcodes do not work after this operation. 3996 ** 3997 ** See also: NotFound, Found, NotExists 3998 */ 3999 case OP_NoConflict: /* jump, in3 */ 4000 case OP_NotFound: /* jump, in3 */ 4001 case OP_Found: { /* jump, in3 */ 4002 int alreadyExists; 4003 int takeJump; 4004 int ii; 4005 VdbeCursor *pC; 4006 int res; 4007 UnpackedRecord *pFree; 4008 UnpackedRecord *pIdxKey; 4009 UnpackedRecord r; 4010 4011 #ifdef SQLITE_TEST 4012 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; 4013 #endif 4014 4015 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4016 assert( pOp->p4type==P4_INT32 ); 4017 pC = p->apCsr[pOp->p1]; 4018 assert( pC!=0 ); 4019 #ifdef SQLITE_DEBUG 4020 pC->seekOp = pOp->opcode; 4021 #endif 4022 pIn3 = &aMem[pOp->p3]; 4023 assert( pC->eCurType==CURTYPE_BTREE ); 4024 assert( pC->uc.pCursor!=0 ); 4025 assert( pC->isTable==0 ); 4026 if( pOp->p4.i>0 ){ 4027 r.pKeyInfo = pC->pKeyInfo; 4028 r.nField = (u16)pOp->p4.i; 4029 r.aMem = pIn3; 4030 #ifdef SQLITE_DEBUG 4031 for(ii=0; ii<r.nField; ii++){ 4032 assert( memIsValid(&r.aMem[ii]) ); 4033 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 ); 4034 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); 4035 } 4036 #endif 4037 pIdxKey = &r; 4038 pFree = 0; 4039 }else{ 4040 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo); 4041 if( pIdxKey==0 ) goto no_mem; 4042 assert( pIn3->flags & MEM_Blob ); 4043 (void)ExpandBlob(pIn3); 4044 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); 4045 } 4046 pIdxKey->default_rc = 0; 4047 takeJump = 0; 4048 if( pOp->opcode==OP_NoConflict ){ 4049 /* For the OP_NoConflict opcode, take the jump if any of the 4050 ** input fields are NULL, since any key with a NULL will not 4051 ** conflict */ 4052 for(ii=0; ii<pIdxKey->nField; ii++){ 4053 if( pIdxKey->aMem[ii].flags & MEM_Null ){ 4054 takeJump = 1; 4055 break; 4056 } 4057 } 4058 } 4059 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res); 4060 if( pFree ) sqlite3DbFree(db, pFree); 4061 if( rc!=SQLITE_OK ){ 4062 goto abort_due_to_error; 4063 } 4064 pC->seekResult = res; 4065 alreadyExists = (res==0); 4066 pC->nullRow = 1-alreadyExists; 4067 pC->deferredMoveto = 0; 4068 pC->cacheStatus = CACHE_STALE; 4069 if( pOp->opcode==OP_Found ){ 4070 VdbeBranchTaken(alreadyExists!=0,2); 4071 if( alreadyExists ) goto jump_to_p2; 4072 }else{ 4073 VdbeBranchTaken(takeJump||alreadyExists==0,2); 4074 if( takeJump || !alreadyExists ) goto jump_to_p2; 4075 } 4076 break; 4077 } 4078 4079 /* Opcode: SeekRowid P1 P2 P3 * * 4080 ** Synopsis: intkey=r[P3] 4081 ** 4082 ** P1 is the index of a cursor open on an SQL table btree (with integer 4083 ** keys). If register P3 does not contain an integer or if P1 does not 4084 ** contain a record with rowid P3 then jump immediately to P2. 4085 ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain 4086 ** a record with rowid P3 then 4087 ** leave the cursor pointing at that record and fall through to the next 4088 ** instruction. 4089 ** 4090 ** The OP_NotExists opcode performs the same operation, but with OP_NotExists 4091 ** the P3 register must be guaranteed to contain an integer value. With this 4092 ** opcode, register P3 might not contain an integer. 4093 ** 4094 ** The OP_NotFound opcode performs the same operation on index btrees 4095 ** (with arbitrary multi-value keys). 4096 ** 4097 ** This opcode leaves the cursor in a state where it cannot be advanced 4098 ** in either direction. In other words, the Next and Prev opcodes will 4099 ** not work following this opcode. 4100 ** 4101 ** See also: Found, NotFound, NoConflict, SeekRowid 4102 */ 4103 /* Opcode: NotExists P1 P2 P3 * * 4104 ** Synopsis: intkey=r[P3] 4105 ** 4106 ** P1 is the index of a cursor open on an SQL table btree (with integer 4107 ** keys). P3 is an integer rowid. If P1 does not contain a record with 4108 ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an 4109 ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then 4110 ** leave the cursor pointing at that record and fall through to the next 4111 ** instruction. 4112 ** 4113 ** The OP_SeekRowid opcode performs the same operation but also allows the 4114 ** P3 register to contain a non-integer value, in which case the jump is 4115 ** always taken. This opcode requires that P3 always contain an integer. 4116 ** 4117 ** The OP_NotFound opcode performs the same operation on index btrees 4118 ** (with arbitrary multi-value keys). 4119 ** 4120 ** This opcode leaves the cursor in a state where it cannot be advanced 4121 ** in either direction. In other words, the Next and Prev opcodes will 4122 ** not work following this opcode. 4123 ** 4124 ** See also: Found, NotFound, NoConflict, SeekRowid 4125 */ 4126 case OP_SeekRowid: { /* jump, in3 */ 4127 VdbeCursor *pC; 4128 BtCursor *pCrsr; 4129 int res; 4130 u64 iKey; 4131 4132 pIn3 = &aMem[pOp->p3]; 4133 if( (pIn3->flags & MEM_Int)==0 ){ 4134 applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding); 4135 if( (pIn3->flags & MEM_Int)==0 ) goto jump_to_p2; 4136 } 4137 /* Fall through into OP_NotExists */ 4138 case OP_NotExists: /* jump, in3 */ 4139 pIn3 = &aMem[pOp->p3]; 4140 assert( pIn3->flags & MEM_Int ); 4141 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4142 pC = p->apCsr[pOp->p1]; 4143 assert( pC!=0 ); 4144 #ifdef SQLITE_DEBUG 4145 pC->seekOp = 0; 4146 #endif 4147 assert( pC->isTable ); 4148 assert( pC->eCurType==CURTYPE_BTREE ); 4149 pCrsr = pC->uc.pCursor; 4150 assert( pCrsr!=0 ); 4151 res = 0; 4152 iKey = pIn3->u.i; 4153 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); 4154 assert( rc==SQLITE_OK || res==0 ); 4155 pC->movetoTarget = iKey; /* Used by OP_Delete */ 4156 pC->nullRow = 0; 4157 pC->cacheStatus = CACHE_STALE; 4158 pC->deferredMoveto = 0; 4159 VdbeBranchTaken(res!=0,2); 4160 pC->seekResult = res; 4161 if( res!=0 ){ 4162 assert( rc==SQLITE_OK ); 4163 if( pOp->p2==0 ){ 4164 rc = SQLITE_CORRUPT_BKPT; 4165 }else{ 4166 goto jump_to_p2; 4167 } 4168 } 4169 if( rc ) goto abort_due_to_error; 4170 break; 4171 } 4172 4173 /* Opcode: Sequence P1 P2 * * * 4174 ** Synopsis: r[P2]=cursor[P1].ctr++ 4175 ** 4176 ** Find the next available sequence number for cursor P1. 4177 ** Write the sequence number into register P2. 4178 ** The sequence number on the cursor is incremented after this 4179 ** instruction. 4180 */ 4181 case OP_Sequence: { /* out2 */ 4182 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4183 assert( p->apCsr[pOp->p1]!=0 ); 4184 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB ); 4185 pOut = out2Prerelease(p, pOp); 4186 pOut->u.i = p->apCsr[pOp->p1]->seqCount++; 4187 break; 4188 } 4189 4190 4191 /* Opcode: NewRowid P1 P2 P3 * * 4192 ** Synopsis: r[P2]=rowid 4193 ** 4194 ** Get a new integer record number (a.k.a "rowid") used as the key to a table. 4195 ** The record number is not previously used as a key in the database 4196 ** table that cursor P1 points to. The new record number is written 4197 ** written to register P2. 4198 ** 4199 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds 4200 ** the largest previously generated record number. No new record numbers are 4201 ** allowed to be less than this value. When this value reaches its maximum, 4202 ** an SQLITE_FULL error is generated. The P3 register is updated with the ' 4203 ** generated record number. This P3 mechanism is used to help implement the 4204 ** AUTOINCREMENT feature. 4205 */ 4206 case OP_NewRowid: { /* out2 */ 4207 i64 v; /* The new rowid */ 4208 VdbeCursor *pC; /* Cursor of table to get the new rowid */ 4209 int res; /* Result of an sqlite3BtreeLast() */ 4210 int cnt; /* Counter to limit the number of searches */ 4211 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ 4212 VdbeFrame *pFrame; /* Root frame of VDBE */ 4213 4214 v = 0; 4215 res = 0; 4216 pOut = out2Prerelease(p, pOp); 4217 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4218 pC = p->apCsr[pOp->p1]; 4219 assert( pC!=0 ); 4220 assert( pC->eCurType==CURTYPE_BTREE ); 4221 assert( pC->uc.pCursor!=0 ); 4222 { 4223 /* The next rowid or record number (different terms for the same 4224 ** thing) is obtained in a two-step algorithm. 4225 ** 4226 ** First we attempt to find the largest existing rowid and add one 4227 ** to that. But if the largest existing rowid is already the maximum 4228 ** positive integer, we have to fall through to the second 4229 ** probabilistic algorithm 4230 ** 4231 ** The second algorithm is to select a rowid at random and see if 4232 ** it already exists in the table. If it does not exist, we have 4233 ** succeeded. If the random rowid does exist, we select a new one 4234 ** and try again, up to 100 times. 4235 */ 4236 assert( pC->isTable ); 4237 4238 #ifdef SQLITE_32BIT_ROWID 4239 # define MAX_ROWID 0x7fffffff 4240 #else 4241 /* Some compilers complain about constants of the form 0x7fffffffffffffff. 4242 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems 4243 ** to provide the constant while making all compilers happy. 4244 */ 4245 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) 4246 #endif 4247 4248 if( !pC->useRandomRowid ){ 4249 rc = sqlite3BtreeLast(pC->uc.pCursor, &res); 4250 if( rc!=SQLITE_OK ){ 4251 goto abort_due_to_error; 4252 } 4253 if( res ){ 4254 v = 1; /* IMP: R-61914-48074 */ 4255 }else{ 4256 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) ); 4257 v = sqlite3BtreeIntegerKey(pC->uc.pCursor); 4258 if( v>=MAX_ROWID ){ 4259 pC->useRandomRowid = 1; 4260 }else{ 4261 v++; /* IMP: R-29538-34987 */ 4262 } 4263 } 4264 } 4265 4266 #ifndef SQLITE_OMIT_AUTOINCREMENT 4267 if( pOp->p3 ){ 4268 /* Assert that P3 is a valid memory cell. */ 4269 assert( pOp->p3>0 ); 4270 if( p->pFrame ){ 4271 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); 4272 /* Assert that P3 is a valid memory cell. */ 4273 assert( pOp->p3<=pFrame->nMem ); 4274 pMem = &pFrame->aMem[pOp->p3]; 4275 }else{ 4276 /* Assert that P3 is a valid memory cell. */ 4277 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 4278 pMem = &aMem[pOp->p3]; 4279 memAboutToChange(p, pMem); 4280 } 4281 assert( memIsValid(pMem) ); 4282 4283 REGISTER_TRACE(pOp->p3, pMem); 4284 sqlite3VdbeMemIntegerify(pMem); 4285 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ 4286 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ 4287 rc = SQLITE_FULL; /* IMP: R-17817-00630 */ 4288 goto abort_due_to_error; 4289 } 4290 if( v<pMem->u.i+1 ){ 4291 v = pMem->u.i + 1; 4292 } 4293 pMem->u.i = v; 4294 } 4295 #endif 4296 if( pC->useRandomRowid ){ 4297 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the 4298 ** largest possible integer (9223372036854775807) then the database 4299 ** engine starts picking positive candidate ROWIDs at random until 4300 ** it finds one that is not previously used. */ 4301 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is 4302 ** an AUTOINCREMENT table. */ 4303 cnt = 0; 4304 do{ 4305 sqlite3_randomness(sizeof(v), &v); 4306 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ 4307 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v, 4308 0, &res))==SQLITE_OK) 4309 && (res==0) 4310 && (++cnt<100)); 4311 if( rc ) goto abort_due_to_error; 4312 if( res==0 ){ 4313 rc = SQLITE_FULL; /* IMP: R-38219-53002 */ 4314 goto abort_due_to_error; 4315 } 4316 assert( v>0 ); /* EV: R-40812-03570 */ 4317 } 4318 pC->deferredMoveto = 0; 4319 pC->cacheStatus = CACHE_STALE; 4320 } 4321 pOut->u.i = v; 4322 break; 4323 } 4324 4325 /* Opcode: Insert P1 P2 P3 P4 P5 4326 ** Synopsis: intkey=r[P3] data=r[P2] 4327 ** 4328 ** Write an entry into the table of cursor P1. A new entry is 4329 ** created if it doesn't already exist or the data for an existing 4330 ** entry is overwritten. The data is the value MEM_Blob stored in register 4331 ** number P2. The key is stored in register P3. The key must 4332 ** be a MEM_Int. 4333 ** 4334 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is 4335 ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, 4336 ** then rowid is stored for subsequent return by the 4337 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). 4338 ** 4339 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might 4340 ** run faster by avoiding an unnecessary seek on cursor P1. However, 4341 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior 4342 ** seeks on the cursor or if the most recent seek used a key equal to P3. 4343 ** 4344 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an 4345 ** UPDATE operation. Otherwise (if the flag is clear) then this opcode 4346 ** is part of an INSERT operation. The difference is only important to 4347 ** the update hook. 4348 ** 4349 ** Parameter P4 may point to a Table structure, or may be NULL. If it is 4350 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked 4351 ** following a successful insert. 4352 ** 4353 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically 4354 ** allocated, then ownership of P2 is transferred to the pseudo-cursor 4355 ** and register P2 becomes ephemeral. If the cursor is changed, the 4356 ** value of register P2 will then change. Make sure this does not 4357 ** cause any problems.) 4358 ** 4359 ** This instruction only works on tables. The equivalent instruction 4360 ** for indices is OP_IdxInsert. 4361 */ 4362 /* Opcode: InsertInt P1 P2 P3 P4 P5 4363 ** Synopsis: intkey=P3 data=r[P2] 4364 ** 4365 ** This works exactly like OP_Insert except that the key is the 4366 ** integer value P3, not the value of the integer stored in register P3. 4367 */ 4368 case OP_Insert: 4369 case OP_InsertInt: { 4370 Mem *pData; /* MEM cell holding data for the record to be inserted */ 4371 Mem *pKey; /* MEM cell holding key for the record */ 4372 VdbeCursor *pC; /* Cursor to table into which insert is written */ 4373 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ 4374 const char *zDb; /* database name - used by the update hook */ 4375 Table *pTab; /* Table structure - used by update and pre-update hooks */ 4376 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ 4377 BtreePayload x; /* Payload to be inserted */ 4378 4379 op = 0; 4380 pData = &aMem[pOp->p2]; 4381 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4382 assert( memIsValid(pData) ); 4383 pC = p->apCsr[pOp->p1]; 4384 assert( pC!=0 ); 4385 assert( pC->eCurType==CURTYPE_BTREE ); 4386 assert( pC->uc.pCursor!=0 ); 4387 assert( pC->isTable ); 4388 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); 4389 REGISTER_TRACE(pOp->p2, pData); 4390 4391 if( pOp->opcode==OP_Insert ){ 4392 pKey = &aMem[pOp->p3]; 4393 assert( pKey->flags & MEM_Int ); 4394 assert( memIsValid(pKey) ); 4395 REGISTER_TRACE(pOp->p3, pKey); 4396 x.nKey = pKey->u.i; 4397 }else{ 4398 assert( pOp->opcode==OP_InsertInt ); 4399 x.nKey = pOp->p3; 4400 } 4401 4402 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ 4403 assert( pC->isTable ); 4404 assert( pC->iDb>=0 ); 4405 zDb = db->aDb[pC->iDb].zDbSName; 4406 pTab = pOp->p4.pTab; 4407 assert( HasRowid(pTab) ); 4408 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); 4409 }else{ 4410 pTab = 0; /* Not needed. Silence a comiler warning. */ 4411 zDb = 0; /* Not needed. Silence a compiler warning. */ 4412 } 4413 4414 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 4415 /* Invoke the pre-update hook, if any */ 4416 if( db->xPreUpdateCallback 4417 && pOp->p4type==P4_TABLE 4418 && !(pOp->p5 & OPFLAG_ISUPDATE) 4419 ){ 4420 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2); 4421 } 4422 #endif 4423 4424 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; 4425 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey; 4426 if( pData->flags & MEM_Null ){ 4427 x.pData = 0; 4428 x.nData = 0; 4429 }else{ 4430 assert( pData->flags & (MEM_Blob|MEM_Str) ); 4431 x.pData = pData->z; 4432 x.nData = pData->n; 4433 } 4434 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); 4435 if( pData->flags & MEM_Zero ){ 4436 x.nZero = pData->u.nZero; 4437 }else{ 4438 x.nZero = 0; 4439 } 4440 x.pKey = 0; 4441 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, 4442 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult 4443 ); 4444 pC->deferredMoveto = 0; 4445 pC->cacheStatus = CACHE_STALE; 4446 4447 /* Invoke the update-hook if required. */ 4448 if( rc ) goto abort_due_to_error; 4449 if( db->xUpdateCallback && op ){ 4450 db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, x.nKey); 4451 } 4452 break; 4453 } 4454 4455 /* Opcode: Delete P1 P2 P3 P4 P5 4456 ** 4457 ** Delete the record at which the P1 cursor is currently pointing. 4458 ** 4459 ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then 4460 ** the cursor will be left pointing at either the next or the previous 4461 ** record in the table. If it is left pointing at the next record, then 4462 ** the next Next instruction will be a no-op. As a result, in this case 4463 ** it is ok to delete a record from within a Next loop. If 4464 ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be 4465 ** left in an undefined state. 4466 ** 4467 ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this 4468 ** delete one of several associated with deleting a table row and all its 4469 ** associated index entries. Exactly one of those deletes is the "primary" 4470 ** delete. The others are all on OPFLAG_FORDELETE cursors or else are 4471 ** marked with the AUXDELETE flag. 4472 ** 4473 ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row 4474 ** change count is incremented (otherwise not). 4475 ** 4476 ** P1 must not be pseudo-table. It has to be a real table with 4477 ** multiple rows. 4478 ** 4479 ** If P4 is not NULL then it points to a Table object. In this case either 4480 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must 4481 ** have been positioned using OP_NotFound prior to invoking this opcode in 4482 ** this case. Specifically, if one is configured, the pre-update hook is 4483 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured, 4484 ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2. 4485 ** 4486 ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address 4487 ** of the memory cell that contains the value that the rowid of the row will 4488 ** be set to by the update. 4489 */ 4490 case OP_Delete: { 4491 VdbeCursor *pC; 4492 const char *zDb; 4493 Table *pTab; 4494 int opflags; 4495 4496 opflags = pOp->p2; 4497 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4498 pC = p->apCsr[pOp->p1]; 4499 assert( pC!=0 ); 4500 assert( pC->eCurType==CURTYPE_BTREE ); 4501 assert( pC->uc.pCursor!=0 ); 4502 assert( pC->deferredMoveto==0 ); 4503 4504 #ifdef SQLITE_DEBUG 4505 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){ 4506 /* If p5 is zero, the seek operation that positioned the cursor prior to 4507 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of 4508 ** the row that is being deleted */ 4509 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor); 4510 assert( pC->movetoTarget==iKey ); 4511 } 4512 #endif 4513 4514 /* If the update-hook or pre-update-hook will be invoked, set zDb to 4515 ** the name of the db to pass as to it. Also set local pTab to a copy 4516 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was 4517 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set 4518 ** VdbeCursor.movetoTarget to the current rowid. */ 4519 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ 4520 assert( pC->iDb>=0 ); 4521 assert( pOp->p4.pTab!=0 ); 4522 zDb = db->aDb[pC->iDb].zDbSName; 4523 pTab = pOp->p4.pTab; 4524 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){ 4525 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor); 4526 } 4527 }else{ 4528 zDb = 0; /* Not needed. Silence a compiler warning. */ 4529 pTab = 0; /* Not needed. Silence a compiler warning. */ 4530 } 4531 4532 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 4533 /* Invoke the pre-update-hook if required. */ 4534 if( db->xPreUpdateCallback && pOp->p4.pTab && HasRowid(pTab) ){ 4535 assert( !(opflags & OPFLAG_ISUPDATE) || (aMem[pOp->p3].flags & MEM_Int) ); 4536 sqlite3VdbePreUpdateHook(p, pC, 4537 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, 4538 zDb, pTab, pC->movetoTarget, 4539 pOp->p3 4540 ); 4541 } 4542 if( opflags & OPFLAG_ISNOOP ) break; 4543 #endif 4544 4545 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ 4546 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 ); 4547 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION ); 4548 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE ); 4549 4550 #ifdef SQLITE_DEBUG 4551 if( p->pFrame==0 ){ 4552 if( pC->isEphemeral==0 4553 && (pOp->p5 & OPFLAG_AUXDELETE)==0 4554 && (pC->wrFlag & OPFLAG_FORDELETE)==0 4555 ){ 4556 nExtraDelete++; 4557 } 4558 if( pOp->p2 & OPFLAG_NCHANGE ){ 4559 nExtraDelete--; 4560 } 4561 } 4562 #endif 4563 4564 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); 4565 pC->cacheStatus = CACHE_STALE; 4566 pC->seekResult = 0; 4567 if( rc ) goto abort_due_to_error; 4568 4569 /* Invoke the update-hook if required. */ 4570 if( opflags & OPFLAG_NCHANGE ){ 4571 p->nChange++; 4572 if( db->xUpdateCallback && HasRowid(pTab) ){ 4573 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName, 4574 pC->movetoTarget); 4575 assert( pC->iDb>=0 ); 4576 } 4577 } 4578 4579 break; 4580 } 4581 /* Opcode: ResetCount * * * * * 4582 ** 4583 ** The value of the change counter is copied to the database handle 4584 ** change counter (returned by subsequent calls to sqlite3_changes()). 4585 ** Then the VMs internal change counter resets to 0. 4586 ** This is used by trigger programs. 4587 */ 4588 case OP_ResetCount: { 4589 sqlite3VdbeSetChanges(db, p->nChange); 4590 p->nChange = 0; 4591 break; 4592 } 4593 4594 /* Opcode: SorterCompare P1 P2 P3 P4 4595 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 4596 ** 4597 ** P1 is a sorter cursor. This instruction compares a prefix of the 4598 ** record blob in register P3 against a prefix of the entry that 4599 ** the sorter cursor currently points to. Only the first P4 fields 4600 ** of r[P3] and the sorter record are compared. 4601 ** 4602 ** If either P3 or the sorter contains a NULL in one of their significant 4603 ** fields (not counting the P4 fields at the end which are ignored) then 4604 ** the comparison is assumed to be equal. 4605 ** 4606 ** Fall through to next instruction if the two records compare equal to 4607 ** each other. Jump to P2 if they are different. 4608 */ 4609 case OP_SorterCompare: { 4610 VdbeCursor *pC; 4611 int res; 4612 int nKeyCol; 4613 4614 pC = p->apCsr[pOp->p1]; 4615 assert( isSorter(pC) ); 4616 assert( pOp->p4type==P4_INT32 ); 4617 pIn3 = &aMem[pOp->p3]; 4618 nKeyCol = pOp->p4.i; 4619 res = 0; 4620 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); 4621 VdbeBranchTaken(res!=0,2); 4622 if( rc ) goto abort_due_to_error; 4623 if( res ) goto jump_to_p2; 4624 break; 4625 }; 4626 4627 /* Opcode: SorterData P1 P2 P3 * * 4628 ** Synopsis: r[P2]=data 4629 ** 4630 ** Write into register P2 the current sorter data for sorter cursor P1. 4631 ** Then clear the column header cache on cursor P3. 4632 ** 4633 ** This opcode is normally use to move a record out of the sorter and into 4634 ** a register that is the source for a pseudo-table cursor created using 4635 ** OpenPseudo. That pseudo-table cursor is the one that is identified by 4636 ** parameter P3. Clearing the P3 column cache as part of this opcode saves 4637 ** us from having to issue a separate NullRow instruction to clear that cache. 4638 */ 4639 case OP_SorterData: { 4640 VdbeCursor *pC; 4641 4642 pOut = &aMem[pOp->p2]; 4643 pC = p->apCsr[pOp->p1]; 4644 assert( isSorter(pC) ); 4645 rc = sqlite3VdbeSorterRowkey(pC, pOut); 4646 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); 4647 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4648 if( rc ) goto abort_due_to_error; 4649 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; 4650 break; 4651 } 4652 4653 /* Opcode: RowData P1 P2 P3 * * 4654 ** Synopsis: r[P2]=data 4655 ** 4656 ** Write into register P2 the complete row content for the row at 4657 ** which cursor P1 is currently pointing. 4658 ** There is no interpretation of the data. 4659 ** It is just copied onto the P2 register exactly as 4660 ** it is found in the database file. 4661 ** 4662 ** If cursor P1 is an index, then the content is the key of the row. 4663 ** If cursor P2 is a table, then the content extracted is the data. 4664 ** 4665 ** If the P1 cursor must be pointing to a valid row (not a NULL row) 4666 ** of a real table, not a pseudo-table. 4667 ** 4668 ** If P3!=0 then this opcode is allowed to make an ephermeral pointer 4669 ** into the database page. That means that the content of the output 4670 ** register will be invalidated as soon as the cursor moves - including 4671 ** moves caused by other cursors that "save" the the current cursors 4672 ** position in order that they can write to the same table. If P3==0 4673 ** then a copy of the data is made into memory. P3!=0 is faster, but 4674 ** P3==0 is safer. 4675 ** 4676 ** If P3!=0 then the content of the P2 register is unsuitable for use 4677 ** in OP_Result and any OP_Result will invalidate the P2 register content. 4678 ** The P2 register content is invalidated by opcodes like OP_Function or 4679 ** by any use of another cursor pointing to the same table. 4680 */ 4681 case OP_RowData: { 4682 VdbeCursor *pC; 4683 BtCursor *pCrsr; 4684 u32 n; 4685 4686 pOut = out2Prerelease(p, pOp); 4687 4688 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4689 pC = p->apCsr[pOp->p1]; 4690 assert( pC!=0 ); 4691 assert( pC->eCurType==CURTYPE_BTREE ); 4692 assert( isSorter(pC)==0 ); 4693 assert( pC->nullRow==0 ); 4694 assert( pC->uc.pCursor!=0 ); 4695 pCrsr = pC->uc.pCursor; 4696 4697 /* The OP_RowData opcodes always follow OP_NotExists or 4698 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions 4699 ** that might invalidate the cursor. 4700 ** If this where not the case, on of the following assert()s 4701 ** would fail. Should this ever change (because of changes in the code 4702 ** generator) then the fix would be to insert a call to 4703 ** sqlite3VdbeCursorMoveto(). 4704 */ 4705 assert( pC->deferredMoveto==0 ); 4706 assert( sqlite3BtreeCursorIsValid(pCrsr) ); 4707 #if 0 /* Not required due to the previous to assert() statements */ 4708 rc = sqlite3VdbeCursorMoveto(pC); 4709 if( rc!=SQLITE_OK ) goto abort_due_to_error; 4710 #endif 4711 4712 n = sqlite3BtreePayloadSize(pCrsr); 4713 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ 4714 goto too_big; 4715 } 4716 testcase( n==0 ); 4717 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut); 4718 if( rc ) goto abort_due_to_error; 4719 if( !pOp->p3 ) Deephemeralize(pOut); 4720 UPDATE_MAX_BLOBSIZE(pOut); 4721 REGISTER_TRACE(pOp->p2, pOut); 4722 break; 4723 } 4724 4725 /* Opcode: Rowid P1 P2 * * * 4726 ** Synopsis: r[P2]=rowid 4727 ** 4728 ** Store in register P2 an integer which is the key of the table entry that 4729 ** P1 is currently point to. 4730 ** 4731 ** P1 can be either an ordinary table or a virtual table. There used to 4732 ** be a separate OP_VRowid opcode for use with virtual tables, but this 4733 ** one opcode now works for both table types. 4734 */ 4735 case OP_Rowid: { /* out2 */ 4736 VdbeCursor *pC; 4737 i64 v; 4738 sqlite3_vtab *pVtab; 4739 const sqlite3_module *pModule; 4740 4741 pOut = out2Prerelease(p, pOp); 4742 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4743 pC = p->apCsr[pOp->p1]; 4744 assert( pC!=0 ); 4745 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); 4746 if( pC->nullRow ){ 4747 pOut->flags = MEM_Null; 4748 break; 4749 }else if( pC->deferredMoveto ){ 4750 v = pC->movetoTarget; 4751 #ifndef SQLITE_OMIT_VIRTUALTABLE 4752 }else if( pC->eCurType==CURTYPE_VTAB ){ 4753 assert( pC->uc.pVCur!=0 ); 4754 pVtab = pC->uc.pVCur->pVtab; 4755 pModule = pVtab->pModule; 4756 assert( pModule->xRowid ); 4757 rc = pModule->xRowid(pC->uc.pVCur, &v); 4758 sqlite3VtabImportErrmsg(p, pVtab); 4759 if( rc ) goto abort_due_to_error; 4760 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 4761 }else{ 4762 assert( pC->eCurType==CURTYPE_BTREE ); 4763 assert( pC->uc.pCursor!=0 ); 4764 rc = sqlite3VdbeCursorRestore(pC); 4765 if( rc ) goto abort_due_to_error; 4766 if( pC->nullRow ){ 4767 pOut->flags = MEM_Null; 4768 break; 4769 } 4770 v = sqlite3BtreeIntegerKey(pC->uc.pCursor); 4771 } 4772 pOut->u.i = v; 4773 break; 4774 } 4775 4776 /* Opcode: NullRow P1 * * * * 4777 ** 4778 ** Move the cursor P1 to a null row. Any OP_Column operations 4779 ** that occur while the cursor is on the null row will always 4780 ** write a NULL. 4781 */ 4782 case OP_NullRow: { 4783 VdbeCursor *pC; 4784 4785 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4786 pC = p->apCsr[pOp->p1]; 4787 assert( pC!=0 ); 4788 pC->nullRow = 1; 4789 pC->cacheStatus = CACHE_STALE; 4790 if( pC->eCurType==CURTYPE_BTREE ){ 4791 assert( pC->uc.pCursor!=0 ); 4792 sqlite3BtreeClearCursor(pC->uc.pCursor); 4793 } 4794 break; 4795 } 4796 4797 /* Opcode: Last P1 P2 P3 * * 4798 ** 4799 ** The next use of the Rowid or Column or Prev instruction for P1 4800 ** will refer to the last entry in the database table or index. 4801 ** If the table or index is empty and P2>0, then jump immediately to P2. 4802 ** If P2 is 0 or if the table or index is not empty, fall through 4803 ** to the following instruction. 4804 ** 4805 ** This opcode leaves the cursor configured to move in reverse order, 4806 ** from the end toward the beginning. In other words, the cursor is 4807 ** configured to use Prev, not Next. 4808 ** 4809 ** If P3 is -1, then the cursor is positioned at the end of the btree 4810 ** for the purpose of appending a new entry onto the btree. In that 4811 ** case P2 must be 0. It is assumed that the cursor is used only for 4812 ** appending and so if the cursor is valid, then the cursor must already 4813 ** be pointing at the end of the btree and so no changes are made to 4814 ** the cursor. 4815 */ 4816 case OP_Last: { /* jump */ 4817 VdbeCursor *pC; 4818 BtCursor *pCrsr; 4819 int res; 4820 4821 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4822 pC = p->apCsr[pOp->p1]; 4823 assert( pC!=0 ); 4824 assert( pC->eCurType==CURTYPE_BTREE ); 4825 pCrsr = pC->uc.pCursor; 4826 res = 0; 4827 assert( pCrsr!=0 ); 4828 pC->seekResult = pOp->p3; 4829 #ifdef SQLITE_DEBUG 4830 pC->seekOp = OP_Last; 4831 #endif 4832 if( pOp->p3==0 || !sqlite3BtreeCursorIsValidNN(pCrsr) ){ 4833 rc = sqlite3BtreeLast(pCrsr, &res); 4834 pC->nullRow = (u8)res; 4835 pC->deferredMoveto = 0; 4836 pC->cacheStatus = CACHE_STALE; 4837 if( rc ) goto abort_due_to_error; 4838 if( pOp->p2>0 ){ 4839 VdbeBranchTaken(res!=0,2); 4840 if( res ) goto jump_to_p2; 4841 } 4842 }else{ 4843 assert( pOp->p2==0 ); 4844 } 4845 break; 4846 } 4847 4848 4849 /* Opcode: SorterSort P1 P2 * * * 4850 ** 4851 ** After all records have been inserted into the Sorter object 4852 ** identified by P1, invoke this opcode to actually do the sorting. 4853 ** Jump to P2 if there are no records to be sorted. 4854 ** 4855 ** This opcode is an alias for OP_Sort and OP_Rewind that is used 4856 ** for Sorter objects. 4857 */ 4858 /* Opcode: Sort P1 P2 * * * 4859 ** 4860 ** This opcode does exactly the same thing as OP_Rewind except that 4861 ** it increments an undocumented global variable used for testing. 4862 ** 4863 ** Sorting is accomplished by writing records into a sorting index, 4864 ** then rewinding that index and playing it back from beginning to 4865 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the 4866 ** rewinding so that the global variable will be incremented and 4867 ** regression tests can determine whether or not the optimizer is 4868 ** correctly optimizing out sorts. 4869 */ 4870 case OP_SorterSort: /* jump */ 4871 case OP_Sort: { /* jump */ 4872 #ifdef SQLITE_TEST 4873 sqlite3_sort_count++; 4874 sqlite3_search_count--; 4875 #endif 4876 p->aCounter[SQLITE_STMTSTATUS_SORT]++; 4877 /* Fall through into OP_Rewind */ 4878 } 4879 /* Opcode: Rewind P1 P2 * * * 4880 ** 4881 ** The next use of the Rowid or Column or Next instruction for P1 4882 ** will refer to the first entry in the database table or index. 4883 ** If the table or index is empty, jump immediately to P2. 4884 ** If the table or index is not empty, fall through to the following 4885 ** instruction. 4886 ** 4887 ** This opcode leaves the cursor configured to move in forward order, 4888 ** from the beginning toward the end. In other words, the cursor is 4889 ** configured to use Next, not Prev. 4890 */ 4891 case OP_Rewind: { /* jump */ 4892 VdbeCursor *pC; 4893 BtCursor *pCrsr; 4894 int res; 4895 4896 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 4897 pC = p->apCsr[pOp->p1]; 4898 assert( pC!=0 ); 4899 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) ); 4900 res = 1; 4901 #ifdef SQLITE_DEBUG 4902 pC->seekOp = OP_Rewind; 4903 #endif 4904 if( isSorter(pC) ){ 4905 rc = sqlite3VdbeSorterRewind(pC, &res); 4906 }else{ 4907 assert( pC->eCurType==CURTYPE_BTREE ); 4908 pCrsr = pC->uc.pCursor; 4909 assert( pCrsr ); 4910 rc = sqlite3BtreeFirst(pCrsr, &res); 4911 pC->deferredMoveto = 0; 4912 pC->cacheStatus = CACHE_STALE; 4913 } 4914 if( rc ) goto abort_due_to_error; 4915 pC->nullRow = (u8)res; 4916 assert( pOp->p2>0 && pOp->p2<p->nOp ); 4917 VdbeBranchTaken(res!=0,2); 4918 if( res ) goto jump_to_p2; 4919 break; 4920 } 4921 4922 /* Opcode: Next P1 P2 P3 P4 P5 4923 ** 4924 ** Advance cursor P1 so that it points to the next key/data pair in its 4925 ** table or index. If there are no more key/value pairs then fall through 4926 ** to the following instruction. But if the cursor advance was successful, 4927 ** jump immediately to P2. 4928 ** 4929 ** The Next opcode is only valid following an SeekGT, SeekGE, or 4930 ** OP_Rewind opcode used to position the cursor. Next is not allowed 4931 ** to follow SeekLT, SeekLE, or OP_Last. 4932 ** 4933 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have 4934 ** been opened prior to this opcode or the program will segfault. 4935 ** 4936 ** The P3 value is a hint to the btree implementation. If P3==1, that 4937 ** means P1 is an SQL index and that this instruction could have been 4938 ** omitted if that index had been unique. P3 is usually 0. P3 is 4939 ** always either 0 or 1. 4940 ** 4941 ** P4 is always of type P4_ADVANCE. The function pointer points to 4942 ** sqlite3BtreeNext(). 4943 ** 4944 ** If P5 is positive and the jump is taken, then event counter 4945 ** number P5-1 in the prepared statement is incremented. 4946 ** 4947 ** See also: Prev, NextIfOpen 4948 */ 4949 /* Opcode: NextIfOpen P1 P2 P3 P4 P5 4950 ** 4951 ** This opcode works just like Next except that if cursor P1 is not 4952 ** open it behaves a no-op. 4953 */ 4954 /* Opcode: Prev P1 P2 P3 P4 P5 4955 ** 4956 ** Back up cursor P1 so that it points to the previous key/data pair in its 4957 ** table or index. If there is no previous key/value pairs then fall through 4958 ** to the following instruction. But if the cursor backup was successful, 4959 ** jump immediately to P2. 4960 ** 4961 ** 4962 ** The Prev opcode is only valid following an SeekLT, SeekLE, or 4963 ** OP_Last opcode used to position the cursor. Prev is not allowed 4964 ** to follow SeekGT, SeekGE, or OP_Rewind. 4965 ** 4966 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is 4967 ** not open then the behavior is undefined. 4968 ** 4969 ** The P3 value is a hint to the btree implementation. If P3==1, that 4970 ** means P1 is an SQL index and that this instruction could have been 4971 ** omitted if that index had been unique. P3 is usually 0. P3 is 4972 ** always either 0 or 1. 4973 ** 4974 ** P4 is always of type P4_ADVANCE. The function pointer points to 4975 ** sqlite3BtreePrevious(). 4976 ** 4977 ** If P5 is positive and the jump is taken, then event counter 4978 ** number P5-1 in the prepared statement is incremented. 4979 */ 4980 /* Opcode: PrevIfOpen P1 P2 P3 P4 P5 4981 ** 4982 ** This opcode works just like Prev except that if cursor P1 is not 4983 ** open it behaves a no-op. 4984 */ 4985 /* Opcode: SorterNext P1 P2 * * P5 4986 ** 4987 ** This opcode works just like OP_Next except that P1 must be a 4988 ** sorter object for which the OP_SorterSort opcode has been 4989 ** invoked. This opcode advances the cursor to the next sorted 4990 ** record, or jumps to P2 if there are no more sorted records. 4991 */ 4992 case OP_SorterNext: { /* jump */ 4993 VdbeCursor *pC; 4994 int res; 4995 4996 pC = p->apCsr[pOp->p1]; 4997 assert( isSorter(pC) ); 4998 res = 0; 4999 rc = sqlite3VdbeSorterNext(db, pC, &res); 5000 goto next_tail; 5001 case OP_PrevIfOpen: /* jump */ 5002 case OP_NextIfOpen: /* jump */ 5003 if( p->apCsr[pOp->p1]==0 ) break; 5004 /* Fall through */ 5005 case OP_Prev: /* jump */ 5006 case OP_Next: /* jump */ 5007 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 5008 assert( pOp->p5<ArraySize(p->aCounter) ); 5009 pC = p->apCsr[pOp->p1]; 5010 res = pOp->p3; 5011 assert( pC!=0 ); 5012 assert( pC->deferredMoveto==0 ); 5013 assert( pC->eCurType==CURTYPE_BTREE ); 5014 assert( res==0 || (res==1 && pC->isTable==0) ); 5015 testcase( res==1 ); 5016 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext ); 5017 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious ); 5018 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext ); 5019 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious); 5020 5021 /* The Next opcode is only used after SeekGT, SeekGE, and Rewind. 5022 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */ 5023 assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen 5024 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE 5025 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found); 5026 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen 5027 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE 5028 || pC->seekOp==OP_Last ); 5029 5030 rc = pOp->p4.xAdvance(pC->uc.pCursor, &res); 5031 next_tail: 5032 pC->cacheStatus = CACHE_STALE; 5033 VdbeBranchTaken(res==0,2); 5034 if( rc ) goto abort_due_to_error; 5035 if( res==0 ){ 5036 pC->nullRow = 0; 5037 p->aCounter[pOp->p5]++; 5038 #ifdef SQLITE_TEST 5039 sqlite3_search_count++; 5040 #endif 5041 goto jump_to_p2_and_check_for_interrupt; 5042 }else{ 5043 pC->nullRow = 1; 5044 } 5045 goto check_for_interrupt; 5046 } 5047 5048 /* Opcode: IdxInsert P1 P2 P3 P4 P5 5049 ** Synopsis: key=r[P2] 5050 ** 5051 ** Register P2 holds an SQL index key made using the 5052 ** MakeRecord instructions. This opcode writes that key 5053 ** into the index P1. Data for the entry is nil. 5054 ** 5055 ** If P4 is not zero, then it is the number of values in the unpacked 5056 ** key of reg(P2). In that case, P3 is the index of the first register 5057 ** for the unpacked key. The availability of the unpacked key can sometimes 5058 ** be an optimization. 5059 ** 5060 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer 5061 ** that this insert is likely to be an append. 5062 ** 5063 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is 5064 ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, 5065 ** then the change counter is unchanged. 5066 ** 5067 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might 5068 ** run faster by avoiding an unnecessary seek on cursor P1. However, 5069 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior 5070 ** seeks on the cursor or if the most recent seek used a key equivalent 5071 ** to P2. 5072 ** 5073 ** This instruction only works for indices. The equivalent instruction 5074 ** for tables is OP_Insert. 5075 */ 5076 /* Opcode: SorterInsert P1 P2 * * * 5077 ** Synopsis: key=r[P2] 5078 ** 5079 ** Register P2 holds an SQL index key made using the 5080 ** MakeRecord instructions. This opcode writes that key 5081 ** into the sorter P1. Data for the entry is nil. 5082 */ 5083 case OP_SorterInsert: /* in2 */ 5084 case OP_IdxInsert: { /* in2 */ 5085 VdbeCursor *pC; 5086 BtreePayload x; 5087 5088 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 5089 pC = p->apCsr[pOp->p1]; 5090 assert( pC!=0 ); 5091 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) ); 5092 pIn2 = &aMem[pOp->p2]; 5093 assert( pIn2->flags & MEM_Blob ); 5094 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; 5095 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert ); 5096 assert( pC->isTable==0 ); 5097 rc = ExpandBlob(pIn2); 5098 if( rc ) goto abort_due_to_error; 5099 if( pOp->opcode==OP_SorterInsert ){ 5100 rc = sqlite3VdbeSorterWrite(pC, pIn2); 5101 }else{ 5102 x.nKey = pIn2->n; 5103 x.pKey = pIn2->z; 5104 x.aMem = aMem + pOp->p3; 5105 x.nMem = (u16)pOp->p4.i; 5106 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, 5107 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), 5108 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) 5109 ); 5110 assert( pC->deferredMoveto==0 ); 5111 pC->cacheStatus = CACHE_STALE; 5112 } 5113 if( rc) goto abort_due_to_error; 5114 break; 5115 } 5116 5117 /* Opcode: IdxDelete P1 P2 P3 * * 5118 ** Synopsis: key=r[P2@P3] 5119 ** 5120 ** The content of P3 registers starting at register P2 form 5121 ** an unpacked index key. This opcode removes that entry from the 5122 ** index opened by cursor P1. 5123 */ 5124 case OP_IdxDelete: { 5125 VdbeCursor *pC; 5126 BtCursor *pCrsr; 5127 int res; 5128 UnpackedRecord r; 5129 5130 assert( pOp->p3>0 ); 5131 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 ); 5132 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 5133 pC = p->apCsr[pOp->p1]; 5134 assert( pC!=0 ); 5135 assert( pC->eCurType==CURTYPE_BTREE ); 5136 pCrsr = pC->uc.pCursor; 5137 assert( pCrsr!=0 ); 5138 assert( pOp->p5==0 ); 5139 r.pKeyInfo = pC->pKeyInfo; 5140 r.nField = (u16)pOp->p3; 5141 r.default_rc = 0; 5142 r.aMem = &aMem[pOp->p2]; 5143 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); 5144 if( rc ) goto abort_due_to_error; 5145 if( res==0 ){ 5146 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); 5147 if( rc ) goto abort_due_to_error; 5148 } 5149 assert( pC->deferredMoveto==0 ); 5150 pC->cacheStatus = CACHE_STALE; 5151 pC->seekResult = 0; 5152 break; 5153 } 5154 5155 /* Opcode: Seek P1 * P3 P4 * 5156 ** Synopsis: Move P3 to P1.rowid 5157 ** 5158 ** P1 is an open index cursor and P3 is a cursor on the corresponding 5159 ** table. This opcode does a deferred seek of the P3 table cursor 5160 ** to the row that corresponds to the current row of P1. 5161 ** 5162 ** This is a deferred seek. Nothing actually happens until 5163 ** the cursor is used to read a record. That way, if no reads 5164 ** occur, no unnecessary I/O happens. 5165 ** 5166 ** P4 may be an array of integers (type P4_INTARRAY) containing 5167 ** one entry for each column in the P3 table. If array entry a(i) 5168 ** is non-zero, then reading column a(i)-1 from cursor P3 is 5169 ** equivalent to performing the deferred seek and then reading column i 5170 ** from P1. This information is stored in P3 and used to redirect 5171 ** reads against P3 over to P1, thus possibly avoiding the need to 5172 ** seek and read cursor P3. 5173 */ 5174 /* Opcode: IdxRowid P1 P2 * * * 5175 ** Synopsis: r[P2]=rowid 5176 ** 5177 ** Write into register P2 an integer which is the last entry in the record at 5178 ** the end of the index key pointed to by cursor P1. This integer should be 5179 ** the rowid of the table entry to which this index entry points. 5180 ** 5181 ** See also: Rowid, MakeRecord. 5182 */ 5183 case OP_Seek: 5184 case OP_IdxRowid: { /* out2 */ 5185 VdbeCursor *pC; /* The P1 index cursor */ 5186 VdbeCursor *pTabCur; /* The P2 table cursor (OP_Seek only) */ 5187 i64 rowid; /* Rowid that P1 current points to */ 5188 5189 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 5190 pC = p->apCsr[pOp->p1]; 5191 assert( pC!=0 ); 5192 assert( pC->eCurType==CURTYPE_BTREE ); 5193 assert( pC->uc.pCursor!=0 ); 5194 assert( pC->isTable==0 ); 5195 assert( pC->deferredMoveto==0 ); 5196 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid ); 5197 5198 /* The IdxRowid and Seek opcodes are combined because of the commonality 5199 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */ 5200 rc = sqlite3VdbeCursorRestore(pC); 5201 5202 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted 5203 ** out from under the cursor. That will never happens for an IdxRowid 5204 ** or Seek opcode */ 5205 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; 5206 5207 if( !pC->nullRow ){ 5208 rowid = 0; /* Not needed. Only used to silence a warning. */ 5209 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid); 5210 if( rc!=SQLITE_OK ){ 5211 goto abort_due_to_error; 5212 } 5213 if( pOp->opcode==OP_Seek ){ 5214 assert( pOp->p3>=0 && pOp->p3<p->nCursor ); 5215 pTabCur = p->apCsr[pOp->p3]; 5216 assert( pTabCur!=0 ); 5217 assert( pTabCur->eCurType==CURTYPE_BTREE ); 5218 assert( pTabCur->uc.pCursor!=0 ); 5219 assert( pTabCur->isTable ); 5220 pTabCur->nullRow = 0; 5221 pTabCur->movetoTarget = rowid; 5222 pTabCur->deferredMoveto = 1; 5223 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 ); 5224 pTabCur->aAltMap = pOp->p4.ai; 5225 pTabCur->pAltCursor = pC; 5226 }else{ 5227 pOut = out2Prerelease(p, pOp); 5228 pOut->u.i = rowid; 5229 } 5230 }else{ 5231 assert( pOp->opcode==OP_IdxRowid ); 5232 sqlite3VdbeMemSetNull(&aMem[pOp->p2]); 5233 } 5234 break; 5235 } 5236 5237 /* Opcode: IdxGE P1 P2 P3 P4 P5 5238 ** Synopsis: key=r[P3@P4] 5239 ** 5240 ** The P4 register values beginning with P3 form an unpacked index 5241 ** key that omits the PRIMARY KEY. Compare this key value against the index 5242 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID 5243 ** fields at the end. 5244 ** 5245 ** If the P1 index entry is greater than or equal to the key value 5246 ** then jump to P2. Otherwise fall through to the next instruction. 5247 */ 5248 /* Opcode: IdxGT P1 P2 P3 P4 P5 5249 ** Synopsis: key=r[P3@P4] 5250 ** 5251 ** The P4 register values beginning with P3 form an unpacked index 5252 ** key that omits the PRIMARY KEY. Compare this key value against the index 5253 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID 5254 ** fields at the end. 5255 ** 5256 ** If the P1 index entry is greater than the key value 5257 ** then jump to P2. Otherwise fall through to the next instruction. 5258 */ 5259 /* Opcode: IdxLT P1 P2 P3 P4 P5 5260 ** Synopsis: key=r[P3@P4] 5261 ** 5262 ** The P4 register values beginning with P3 form an unpacked index 5263 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against 5264 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or 5265 ** ROWID on the P1 index. 5266 ** 5267 ** If the P1 index entry is less than the key value then jump to P2. 5268 ** Otherwise fall through to the next instruction. 5269 */ 5270 /* Opcode: IdxLE P1 P2 P3 P4 P5 5271 ** Synopsis: key=r[P3@P4] 5272 ** 5273 ** The P4 register values beginning with P3 form an unpacked index 5274 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against 5275 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or 5276 ** ROWID on the P1 index. 5277 ** 5278 ** If the P1 index entry is less than or equal to the key value then jump 5279 ** to P2. Otherwise fall through to the next instruction. 5280 */ 5281 case OP_IdxLE: /* jump */ 5282 case OP_IdxGT: /* jump */ 5283 case OP_IdxLT: /* jump */ 5284 case OP_IdxGE: { /* jump */ 5285 VdbeCursor *pC; 5286 int res; 5287 UnpackedRecord r; 5288 5289 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 5290 pC = p->apCsr[pOp->p1]; 5291 assert( pC!=0 ); 5292 assert( pC->isOrdered ); 5293 assert( pC->eCurType==CURTYPE_BTREE ); 5294 assert( pC->uc.pCursor!=0); 5295 assert( pC->deferredMoveto==0 ); 5296 assert( pOp->p5==0 || pOp->p5==1 ); 5297 assert( pOp->p4type==P4_INT32 ); 5298 r.pKeyInfo = pC->pKeyInfo; 5299 r.nField = (u16)pOp->p4.i; 5300 if( pOp->opcode<OP_IdxLT ){ 5301 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); 5302 r.default_rc = -1; 5303 }else{ 5304 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); 5305 r.default_rc = 0; 5306 } 5307 r.aMem = &aMem[pOp->p3]; 5308 #ifdef SQLITE_DEBUG 5309 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } 5310 #endif 5311 res = 0; /* Not needed. Only used to silence a warning. */ 5312 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); 5313 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); 5314 if( (pOp->opcode&1)==(OP_IdxLT&1) ){ 5315 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); 5316 res = -res; 5317 }else{ 5318 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); 5319 res++; 5320 } 5321 VdbeBranchTaken(res>0,2); 5322 if( rc ) goto abort_due_to_error; 5323 if( res>0 ) goto jump_to_p2; 5324 break; 5325 } 5326 5327 /* Opcode: Destroy P1 P2 P3 * * 5328 ** 5329 ** Delete an entire database table or index whose root page in the database 5330 ** file is given by P1. 5331 ** 5332 ** The table being destroyed is in the main database file if P3==0. If 5333 ** P3==1 then the table to be clear is in the auxiliary database file 5334 ** that is used to store tables create using CREATE TEMPORARY TABLE. 5335 ** 5336 ** If AUTOVACUUM is enabled then it is possible that another root page 5337 ** might be moved into the newly deleted root page in order to keep all 5338 ** root pages contiguous at the beginning of the database. The former 5339 ** value of the root page that moved - its value before the move occurred - 5340 ** is stored in register P2. If no page 5341 ** movement was required (because the table being dropped was already 5342 ** the last one in the database) then a zero is stored in register P2. 5343 ** If AUTOVACUUM is disabled then a zero is stored in register P2. 5344 ** 5345 ** See also: Clear 5346 */ 5347 case OP_Destroy: { /* out2 */ 5348 int iMoved; 5349 int iDb; 5350 5351 assert( p->readOnly==0 ); 5352 assert( pOp->p1>1 ); 5353 pOut = out2Prerelease(p, pOp); 5354 pOut->flags = MEM_Null; 5355 if( db->nVdbeRead > db->nVDestroy+1 ){ 5356 rc = SQLITE_LOCKED; 5357 p->errorAction = OE_Abort; 5358 goto abort_due_to_error; 5359 }else{ 5360 iDb = pOp->p3; 5361 assert( DbMaskTest(p->btreeMask, iDb) ); 5362 iMoved = 0; /* Not needed. Only to silence a warning. */ 5363 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); 5364 pOut->flags = MEM_Int; 5365 pOut->u.i = iMoved; 5366 if( rc ) goto abort_due_to_error; 5367 #ifndef SQLITE_OMIT_AUTOVACUUM 5368 if( iMoved!=0 ){ 5369 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); 5370 /* All OP_Destroy operations occur on the same btree */ 5371 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); 5372 resetSchemaOnFault = iDb+1; 5373 } 5374 #endif 5375 } 5376 break; 5377 } 5378 5379 /* Opcode: Clear P1 P2 P3 5380 ** 5381 ** Delete all contents of the database table or index whose root page 5382 ** in the database file is given by P1. But, unlike Destroy, do not 5383 ** remove the table or index from the database file. 5384 ** 5385 ** The table being clear is in the main database file if P2==0. If 5386 ** P2==1 then the table to be clear is in the auxiliary database file 5387 ** that is used to store tables create using CREATE TEMPORARY TABLE. 5388 ** 5389 ** If the P3 value is non-zero, then the table referred to must be an 5390 ** intkey table (an SQL table, not an index). In this case the row change 5391 ** count is incremented by the number of rows in the table being cleared. 5392 ** If P3 is greater than zero, then the value stored in register P3 is 5393 ** also incremented by the number of rows in the table being cleared. 5394 ** 5395 ** See also: Destroy 5396 */ 5397 case OP_Clear: { 5398 int nChange; 5399 5400 nChange = 0; 5401 assert( p->readOnly==0 ); 5402 assert( DbMaskTest(p->btreeMask, pOp->p2) ); 5403 rc = sqlite3BtreeClearTable( 5404 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) 5405 ); 5406 if( pOp->p3 ){ 5407 p->nChange += nChange; 5408 if( pOp->p3>0 ){ 5409 assert( memIsValid(&aMem[pOp->p3]) ); 5410 memAboutToChange(p, &aMem[pOp->p3]); 5411 aMem[pOp->p3].u.i += nChange; 5412 } 5413 } 5414 if( rc ) goto abort_due_to_error; 5415 break; 5416 } 5417 5418 /* Opcode: ResetSorter P1 * * * * 5419 ** 5420 ** Delete all contents from the ephemeral table or sorter 5421 ** that is open on cursor P1. 5422 ** 5423 ** This opcode only works for cursors used for sorting and 5424 ** opened with OP_OpenEphemeral or OP_SorterOpen. 5425 */ 5426 case OP_ResetSorter: { 5427 VdbeCursor *pC; 5428 5429 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 5430 pC = p->apCsr[pOp->p1]; 5431 assert( pC!=0 ); 5432 if( isSorter(pC) ){ 5433 sqlite3VdbeSorterReset(db, pC->uc.pSorter); 5434 }else{ 5435 assert( pC->eCurType==CURTYPE_BTREE ); 5436 assert( pC->isEphemeral ); 5437 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor); 5438 if( rc ) goto abort_due_to_error; 5439 } 5440 break; 5441 } 5442 5443 /* Opcode: CreateTable P1 P2 * * * 5444 ** Synopsis: r[P2]=root iDb=P1 5445 ** 5446 ** Allocate a new table in the main database file if P1==0 or in the 5447 ** auxiliary database file if P1==1 or in an attached database if 5448 ** P1>1. Write the root page number of the new table into 5449 ** register P2 5450 ** 5451 ** The difference between a table and an index is this: A table must 5452 ** have a 4-byte integer key and can have arbitrary data. An index 5453 ** has an arbitrary key but no data. 5454 ** 5455 ** See also: CreateIndex 5456 */ 5457 /* Opcode: CreateIndex P1 P2 * * * 5458 ** Synopsis: r[P2]=root iDb=P1 5459 ** 5460 ** Allocate a new index in the main database file if P1==0 or in the 5461 ** auxiliary database file if P1==1 or in an attached database if 5462 ** P1>1. Write the root page number of the new table into 5463 ** register P2. 5464 ** 5465 ** See documentation on OP_CreateTable for additional information. 5466 */ 5467 case OP_CreateIndex: /* out2 */ 5468 case OP_CreateTable: { /* out2 */ 5469 int pgno; 5470 int flags; 5471 Db *pDb; 5472 5473 pOut = out2Prerelease(p, pOp); 5474 pgno = 0; 5475 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 5476 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 5477 assert( p->readOnly==0 ); 5478 pDb = &db->aDb[pOp->p1]; 5479 assert( pDb->pBt!=0 ); 5480 if( pOp->opcode==OP_CreateTable ){ 5481 /* flags = BTREE_INTKEY; */ 5482 flags = BTREE_INTKEY; 5483 }else{ 5484 flags = BTREE_BLOBKEY; 5485 } 5486 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); 5487 if( rc ) goto abort_due_to_error; 5488 pOut->u.i = pgno; 5489 break; 5490 } 5491 5492 /* Opcode: ParseSchema P1 * * P4 * 5493 ** 5494 ** Read and parse all entries from the SQLITE_MASTER table of database P1 5495 ** that match the WHERE clause P4. 5496 ** 5497 ** This opcode invokes the parser to create a new virtual machine, 5498 ** then runs the new virtual machine. It is thus a re-entrant opcode. 5499 */ 5500 case OP_ParseSchema: { 5501 int iDb; 5502 const char *zMaster; 5503 char *zSql; 5504 InitData initData; 5505 5506 /* Any prepared statement that invokes this opcode will hold mutexes 5507 ** on every btree. This is a prerequisite for invoking 5508 ** sqlite3InitCallback(). 5509 */ 5510 #ifdef SQLITE_DEBUG 5511 for(iDb=0; iDb<db->nDb; iDb++){ 5512 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); 5513 } 5514 #endif 5515 5516 iDb = pOp->p1; 5517 assert( iDb>=0 && iDb<db->nDb ); 5518 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) ); 5519 /* Used to be a conditional */ { 5520 zMaster = MASTER_NAME; 5521 initData.db = db; 5522 initData.iDb = pOp->p1; 5523 initData.pzErrMsg = &p->zErrMsg; 5524 zSql = sqlite3MPrintf(db, 5525 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid", 5526 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z); 5527 if( zSql==0 ){ 5528 rc = SQLITE_NOMEM_BKPT; 5529 }else{ 5530 assert( db->init.busy==0 ); 5531 db->init.busy = 1; 5532 initData.rc = SQLITE_OK; 5533 assert( !db->mallocFailed ); 5534 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); 5535 if( rc==SQLITE_OK ) rc = initData.rc; 5536 sqlite3DbFree(db, zSql); 5537 db->init.busy = 0; 5538 } 5539 } 5540 if( rc ){ 5541 sqlite3ResetAllSchemasOfConnection(db); 5542 if( rc==SQLITE_NOMEM ){ 5543 goto no_mem; 5544 } 5545 goto abort_due_to_error; 5546 } 5547 break; 5548 } 5549 5550 #if !defined(SQLITE_OMIT_ANALYZE) 5551 /* Opcode: LoadAnalysis P1 * * * * 5552 ** 5553 ** Read the sqlite_stat1 table for database P1 and load the content 5554 ** of that table into the internal index hash table. This will cause 5555 ** the analysis to be used when preparing all subsequent queries. 5556 */ 5557 case OP_LoadAnalysis: { 5558 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 5559 rc = sqlite3AnalysisLoad(db, pOp->p1); 5560 if( rc ) goto abort_due_to_error; 5561 break; 5562 } 5563 #endif /* !defined(SQLITE_OMIT_ANALYZE) */ 5564 5565 /* Opcode: DropTable P1 * * P4 * 5566 ** 5567 ** Remove the internal (in-memory) data structures that describe 5568 ** the table named P4 in database P1. This is called after a table 5569 ** is dropped from disk (using the Destroy opcode) in order to keep 5570 ** the internal representation of the 5571 ** schema consistent with what is on disk. 5572 */ 5573 case OP_DropTable: { 5574 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); 5575 break; 5576 } 5577 5578 /* Opcode: DropIndex P1 * * P4 * 5579 ** 5580 ** Remove the internal (in-memory) data structures that describe 5581 ** the index named P4 in database P1. This is called after an index 5582 ** is dropped from disk (using the Destroy opcode) 5583 ** in order to keep the internal representation of the 5584 ** schema consistent with what is on disk. 5585 */ 5586 case OP_DropIndex: { 5587 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); 5588 break; 5589 } 5590 5591 /* Opcode: DropTrigger P1 * * P4 * 5592 ** 5593 ** Remove the internal (in-memory) data structures that describe 5594 ** the trigger named P4 in database P1. This is called after a trigger 5595 ** is dropped from disk (using the Destroy opcode) in order to keep 5596 ** the internal representation of the 5597 ** schema consistent with what is on disk. 5598 */ 5599 case OP_DropTrigger: { 5600 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); 5601 break; 5602 } 5603 5604 5605 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 5606 /* Opcode: IntegrityCk P1 P2 P3 P4 P5 5607 ** 5608 ** Do an analysis of the currently open database. Store in 5609 ** register P1 the text of an error message describing any problems. 5610 ** If no problems are found, store a NULL in register P1. 5611 ** 5612 ** The register P3 contains the maximum number of allowed errors. 5613 ** At most reg(P3) errors will be reported. 5614 ** In other words, the analysis stops as soon as reg(P1) errors are 5615 ** seen. Reg(P1) is updated with the number of errors remaining. 5616 ** 5617 ** The root page numbers of all tables in the database are integers 5618 ** stored in P4_INTARRAY argument. 5619 ** 5620 ** If P5 is not zero, the check is done on the auxiliary database 5621 ** file, not the main database file. 5622 ** 5623 ** This opcode is used to implement the integrity_check pragma. 5624 */ 5625 case OP_IntegrityCk: { 5626 int nRoot; /* Number of tables to check. (Number of root pages.) */ 5627 int *aRoot; /* Array of rootpage numbers for tables to be checked */ 5628 int nErr; /* Number of errors reported */ 5629 char *z; /* Text of the error report */ 5630 Mem *pnErr; /* Register keeping track of errors remaining */ 5631 5632 assert( p->bIsReader ); 5633 nRoot = pOp->p2; 5634 aRoot = pOp->p4.ai; 5635 assert( nRoot>0 ); 5636 assert( aRoot[nRoot]==0 ); 5637 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 5638 pnErr = &aMem[pOp->p3]; 5639 assert( (pnErr->flags & MEM_Int)!=0 ); 5640 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); 5641 pIn1 = &aMem[pOp->p1]; 5642 assert( pOp->p5<db->nDb ); 5643 assert( DbMaskTest(p->btreeMask, pOp->p5) ); 5644 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, 5645 (int)pnErr->u.i, &nErr); 5646 pnErr->u.i -= nErr; 5647 sqlite3VdbeMemSetNull(pIn1); 5648 if( nErr==0 ){ 5649 assert( z==0 ); 5650 }else if( z==0 ){ 5651 goto no_mem; 5652 }else{ 5653 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); 5654 } 5655 UPDATE_MAX_BLOBSIZE(pIn1); 5656 sqlite3VdbeChangeEncoding(pIn1, encoding); 5657 break; 5658 } 5659 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 5660 5661 /* Opcode: RowSetAdd P1 P2 * * * 5662 ** Synopsis: rowset(P1)=r[P2] 5663 ** 5664 ** Insert the integer value held by register P2 into a boolean index 5665 ** held in register P1. 5666 ** 5667 ** An assertion fails if P2 is not an integer. 5668 */ 5669 case OP_RowSetAdd: { /* in1, in2 */ 5670 pIn1 = &aMem[pOp->p1]; 5671 pIn2 = &aMem[pOp->p2]; 5672 assert( (pIn2->flags & MEM_Int)!=0 ); 5673 if( (pIn1->flags & MEM_RowSet)==0 ){ 5674 sqlite3VdbeMemSetRowSet(pIn1); 5675 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; 5676 } 5677 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i); 5678 break; 5679 } 5680 5681 /* Opcode: RowSetRead P1 P2 P3 * * 5682 ** Synopsis: r[P3]=rowset(P1) 5683 ** 5684 ** Extract the smallest value from boolean index P1 and put that value into 5685 ** register P3. Or, if boolean index P1 is initially empty, leave P3 5686 ** unchanged and jump to instruction P2. 5687 */ 5688 case OP_RowSetRead: { /* jump, in1, out3 */ 5689 i64 val; 5690 5691 pIn1 = &aMem[pOp->p1]; 5692 if( (pIn1->flags & MEM_RowSet)==0 5693 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0 5694 ){ 5695 /* The boolean index is empty */ 5696 sqlite3VdbeMemSetNull(pIn1); 5697 VdbeBranchTaken(1,2); 5698 goto jump_to_p2_and_check_for_interrupt; 5699 }else{ 5700 /* A value was pulled from the index */ 5701 VdbeBranchTaken(0,2); 5702 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); 5703 } 5704 goto check_for_interrupt; 5705 } 5706 5707 /* Opcode: RowSetTest P1 P2 P3 P4 5708 ** Synopsis: if r[P3] in rowset(P1) goto P2 5709 ** 5710 ** Register P3 is assumed to hold a 64-bit integer value. If register P1 5711 ** contains a RowSet object and that RowSet object contains 5712 ** the value held in P3, jump to register P2. Otherwise, insert the 5713 ** integer in P3 into the RowSet and continue on to the 5714 ** next opcode. 5715 ** 5716 ** The RowSet object is optimized for the case where successive sets 5717 ** of integers, where each set contains no duplicates. Each set 5718 ** of values is identified by a unique P4 value. The first set 5719 ** must have P4==0, the final set P4=-1. P4 must be either -1 or 5720 ** non-negative. For non-negative values of P4 only the lower 4 5721 ** bits are significant. 5722 ** 5723 ** This allows optimizations: (a) when P4==0 there is no need to test 5724 ** the rowset object for P3, as it is guaranteed not to contain it, 5725 ** (b) when P4==-1 there is no need to insert the value, as it will 5726 ** never be tested for, and (c) when a value that is part of set X is 5727 ** inserted, there is no need to search to see if the same value was 5728 ** previously inserted as part of set X (only if it was previously 5729 ** inserted as part of some other set). 5730 */ 5731 case OP_RowSetTest: { /* jump, in1, in3 */ 5732 int iSet; 5733 int exists; 5734 5735 pIn1 = &aMem[pOp->p1]; 5736 pIn3 = &aMem[pOp->p3]; 5737 iSet = pOp->p4.i; 5738 assert( pIn3->flags&MEM_Int ); 5739 5740 /* If there is anything other than a rowset object in memory cell P1, 5741 ** delete it now and initialize P1 with an empty rowset 5742 */ 5743 if( (pIn1->flags & MEM_RowSet)==0 ){ 5744 sqlite3VdbeMemSetRowSet(pIn1); 5745 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; 5746 } 5747 5748 assert( pOp->p4type==P4_INT32 ); 5749 assert( iSet==-1 || iSet>=0 ); 5750 if( iSet ){ 5751 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i); 5752 VdbeBranchTaken(exists!=0,2); 5753 if( exists ) goto jump_to_p2; 5754 } 5755 if( iSet>=0 ){ 5756 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i); 5757 } 5758 break; 5759 } 5760 5761 5762 #ifndef SQLITE_OMIT_TRIGGER 5763 5764 /* Opcode: Program P1 P2 P3 P4 P5 5765 ** 5766 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). 5767 ** 5768 ** P1 contains the address of the memory cell that contains the first memory 5769 ** cell in an array of values used as arguments to the sub-program. P2 5770 ** contains the address to jump to if the sub-program throws an IGNORE 5771 ** exception using the RAISE() function. Register P3 contains the address 5772 ** of a memory cell in this (the parent) VM that is used to allocate the 5773 ** memory required by the sub-vdbe at runtime. 5774 ** 5775 ** P4 is a pointer to the VM containing the trigger program. 5776 ** 5777 ** If P5 is non-zero, then recursive program invocation is enabled. 5778 */ 5779 case OP_Program: { /* jump */ 5780 int nMem; /* Number of memory registers for sub-program */ 5781 int nByte; /* Bytes of runtime space required for sub-program */ 5782 Mem *pRt; /* Register to allocate runtime space */ 5783 Mem *pMem; /* Used to iterate through memory cells */ 5784 Mem *pEnd; /* Last memory cell in new array */ 5785 VdbeFrame *pFrame; /* New vdbe frame to execute in */ 5786 SubProgram *pProgram; /* Sub-program to execute */ 5787 void *t; /* Token identifying trigger */ 5788 5789 pProgram = pOp->p4.pProgram; 5790 pRt = &aMem[pOp->p3]; 5791 assert( pProgram->nOp>0 ); 5792 5793 /* If the p5 flag is clear, then recursive invocation of triggers is 5794 ** disabled for backwards compatibility (p5 is set if this sub-program 5795 ** is really a trigger, not a foreign key action, and the flag set 5796 ** and cleared by the "PRAGMA recursive_triggers" command is clear). 5797 ** 5798 ** It is recursive invocation of triggers, at the SQL level, that is 5799 ** disabled. In some cases a single trigger may generate more than one 5800 ** SubProgram (if the trigger may be executed with more than one different 5801 ** ON CONFLICT algorithm). SubProgram structures associated with a 5802 ** single trigger all have the same value for the SubProgram.token 5803 ** variable. */ 5804 if( pOp->p5 ){ 5805 t = pProgram->token; 5806 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); 5807 if( pFrame ) break; 5808 } 5809 5810 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ 5811 rc = SQLITE_ERROR; 5812 sqlite3VdbeError(p, "too many levels of trigger recursion"); 5813 goto abort_due_to_error; 5814 } 5815 5816 /* Register pRt is used to store the memory required to save the state 5817 ** of the current program, and the memory required at runtime to execute 5818 ** the trigger program. If this trigger has been fired before, then pRt 5819 ** is already allocated. Otherwise, it must be initialized. */ 5820 if( (pRt->flags&MEM_Frame)==0 ){ 5821 /* SubProgram.nMem is set to the number of memory cells used by the 5822 ** program stored in SubProgram.aOp. As well as these, one memory 5823 ** cell is required for each cursor used by the program. Set local 5824 ** variable nMem (and later, VdbeFrame.nChildMem) to this value. 5825 */ 5826 nMem = pProgram->nMem + pProgram->nCsr; 5827 assert( nMem>0 ); 5828 if( pProgram->nCsr==0 ) nMem++; 5829 nByte = ROUND8(sizeof(VdbeFrame)) 5830 + nMem * sizeof(Mem) 5831 + pProgram->nCsr * sizeof(VdbeCursor *); 5832 pFrame = sqlite3DbMallocZero(db, nByte); 5833 if( !pFrame ){ 5834 goto no_mem; 5835 } 5836 sqlite3VdbeMemRelease(pRt); 5837 pRt->flags = MEM_Frame; 5838 pRt->u.pFrame = pFrame; 5839 5840 pFrame->v = p; 5841 pFrame->nChildMem = nMem; 5842 pFrame->nChildCsr = pProgram->nCsr; 5843 pFrame->pc = (int)(pOp - aOp); 5844 pFrame->aMem = p->aMem; 5845 pFrame->nMem = p->nMem; 5846 pFrame->apCsr = p->apCsr; 5847 pFrame->nCursor = p->nCursor; 5848 pFrame->aOp = p->aOp; 5849 pFrame->nOp = p->nOp; 5850 pFrame->token = pProgram->token; 5851 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 5852 pFrame->anExec = p->anExec; 5853 #endif 5854 5855 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; 5856 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ 5857 pMem->flags = MEM_Undefined; 5858 pMem->db = db; 5859 } 5860 }else{ 5861 pFrame = pRt->u.pFrame; 5862 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem 5863 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) ); 5864 assert( pProgram->nCsr==pFrame->nChildCsr ); 5865 assert( (int)(pOp - aOp)==pFrame->pc ); 5866 } 5867 5868 p->nFrame++; 5869 pFrame->pParent = p->pFrame; 5870 pFrame->lastRowid = db->lastRowid; 5871 pFrame->nChange = p->nChange; 5872 pFrame->nDbChange = p->db->nChange; 5873 assert( pFrame->pAuxData==0 ); 5874 pFrame->pAuxData = p->pAuxData; 5875 p->pAuxData = 0; 5876 p->nChange = 0; 5877 p->pFrame = pFrame; 5878 p->aMem = aMem = VdbeFrameMem(pFrame); 5879 p->nMem = pFrame->nChildMem; 5880 p->nCursor = (u16)pFrame->nChildCsr; 5881 p->apCsr = (VdbeCursor **)&aMem[p->nMem]; 5882 p->aOp = aOp = pProgram->aOp; 5883 p->nOp = pProgram->nOp; 5884 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 5885 p->anExec = 0; 5886 #endif 5887 pOp = &aOp[-1]; 5888 5889 break; 5890 } 5891 5892 /* Opcode: Param P1 P2 * * * 5893 ** 5894 ** This opcode is only ever present in sub-programs called via the 5895 ** OP_Program instruction. Copy a value currently stored in a memory 5896 ** cell of the calling (parent) frame to cell P2 in the current frames 5897 ** address space. This is used by trigger programs to access the new.* 5898 ** and old.* values. 5899 ** 5900 ** The address of the cell in the parent frame is determined by adding 5901 ** the value of the P1 argument to the value of the P1 argument to the 5902 ** calling OP_Program instruction. 5903 */ 5904 case OP_Param: { /* out2 */ 5905 VdbeFrame *pFrame; 5906 Mem *pIn; 5907 pOut = out2Prerelease(p, pOp); 5908 pFrame = p->pFrame; 5909 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; 5910 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); 5911 break; 5912 } 5913 5914 #endif /* #ifndef SQLITE_OMIT_TRIGGER */ 5915 5916 #ifndef SQLITE_OMIT_FOREIGN_KEY 5917 /* Opcode: FkCounter P1 P2 * * * 5918 ** Synopsis: fkctr[P1]+=P2 5919 ** 5920 ** Increment a "constraint counter" by P2 (P2 may be negative or positive). 5921 ** If P1 is non-zero, the database constraint counter is incremented 5922 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the 5923 ** statement counter is incremented (immediate foreign key constraints). 5924 */ 5925 case OP_FkCounter: { 5926 if( db->flags & SQLITE_DeferFKs ){ 5927 db->nDeferredImmCons += pOp->p2; 5928 }else if( pOp->p1 ){ 5929 db->nDeferredCons += pOp->p2; 5930 }else{ 5931 p->nFkConstraint += pOp->p2; 5932 } 5933 break; 5934 } 5935 5936 /* Opcode: FkIfZero P1 P2 * * * 5937 ** Synopsis: if fkctr[P1]==0 goto P2 5938 ** 5939 ** This opcode tests if a foreign key constraint-counter is currently zero. 5940 ** If so, jump to instruction P2. Otherwise, fall through to the next 5941 ** instruction. 5942 ** 5943 ** If P1 is non-zero, then the jump is taken if the database constraint-counter 5944 ** is zero (the one that counts deferred constraint violations). If P1 is 5945 ** zero, the jump is taken if the statement constraint-counter is zero 5946 ** (immediate foreign key constraint violations). 5947 */ 5948 case OP_FkIfZero: { /* jump */ 5949 if( pOp->p1 ){ 5950 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2); 5951 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; 5952 }else{ 5953 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2); 5954 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; 5955 } 5956 break; 5957 } 5958 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ 5959 5960 #ifndef SQLITE_OMIT_AUTOINCREMENT 5961 /* Opcode: MemMax P1 P2 * * * 5962 ** Synopsis: r[P1]=max(r[P1],r[P2]) 5963 ** 5964 ** P1 is a register in the root frame of this VM (the root frame is 5965 ** different from the current frame if this instruction is being executed 5966 ** within a sub-program). Set the value of register P1 to the maximum of 5967 ** its current value and the value in register P2. 5968 ** 5969 ** This instruction throws an error if the memory cell is not initially 5970 ** an integer. 5971 */ 5972 case OP_MemMax: { /* in2 */ 5973 VdbeFrame *pFrame; 5974 if( p->pFrame ){ 5975 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); 5976 pIn1 = &pFrame->aMem[pOp->p1]; 5977 }else{ 5978 pIn1 = &aMem[pOp->p1]; 5979 } 5980 assert( memIsValid(pIn1) ); 5981 sqlite3VdbeMemIntegerify(pIn1); 5982 pIn2 = &aMem[pOp->p2]; 5983 sqlite3VdbeMemIntegerify(pIn2); 5984 if( pIn1->u.i<pIn2->u.i){ 5985 pIn1->u.i = pIn2->u.i; 5986 } 5987 break; 5988 } 5989 #endif /* SQLITE_OMIT_AUTOINCREMENT */ 5990 5991 /* Opcode: IfPos P1 P2 P3 * * 5992 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 5993 ** 5994 ** Register P1 must contain an integer. 5995 ** If the value of register P1 is 1 or greater, subtract P3 from the 5996 ** value in P1 and jump to P2. 5997 ** 5998 ** If the initial value of register P1 is less than 1, then the 5999 ** value is unchanged and control passes through to the next instruction. 6000 */ 6001 case OP_IfPos: { /* jump, in1 */ 6002 pIn1 = &aMem[pOp->p1]; 6003 assert( pIn1->flags&MEM_Int ); 6004 VdbeBranchTaken( pIn1->u.i>0, 2); 6005 if( pIn1->u.i>0 ){ 6006 pIn1->u.i -= pOp->p3; 6007 goto jump_to_p2; 6008 } 6009 break; 6010 } 6011 6012 /* Opcode: OffsetLimit P1 P2 P3 * * 6013 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) 6014 ** 6015 ** This opcode performs a commonly used computation associated with 6016 ** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3] 6017 ** holds the offset counter. The opcode computes the combined value 6018 ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] 6019 ** value computed is the total number of rows that will need to be 6020 ** visited in order to complete the query. 6021 ** 6022 ** If r[P3] is zero or negative, that means there is no OFFSET 6023 ** and r[P2] is set to be the value of the LIMIT, r[P1]. 6024 ** 6025 ** if r[P1] is zero or negative, that means there is no LIMIT 6026 ** and r[P2] is set to -1. 6027 ** 6028 ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3]. 6029 */ 6030 case OP_OffsetLimit: { /* in1, out2, in3 */ 6031 i64 x; 6032 pIn1 = &aMem[pOp->p1]; 6033 pIn3 = &aMem[pOp->p3]; 6034 pOut = out2Prerelease(p, pOp); 6035 assert( pIn1->flags & MEM_Int ); 6036 assert( pIn3->flags & MEM_Int ); 6037 x = pIn1->u.i; 6038 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){ 6039 /* If the LIMIT is less than or equal to zero, loop forever. This 6040 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then 6041 ** also loop forever. This is undocumented. In fact, one could argue 6042 ** that the loop should terminate. But assuming 1 billion iterations 6043 ** per second (far exceeding the capabilities of any current hardware) 6044 ** it would take nearly 300 years to actually reach the limit. So 6045 ** looping forever is a reasonable approximation. */ 6046 pOut->u.i = -1; 6047 }else{ 6048 pOut->u.i = x; 6049 } 6050 break; 6051 } 6052 6053 /* Opcode: IfNotZero P1 P2 * * * 6054 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2 6055 ** 6056 ** Register P1 must contain an integer. If the content of register P1 is 6057 ** initially greater than zero, then decrement the value in register P1. 6058 ** If it is non-zero (negative or positive) and then also jump to P2. 6059 ** If register P1 is initially zero, leave it unchanged and fall through. 6060 */ 6061 case OP_IfNotZero: { /* jump, in1 */ 6062 pIn1 = &aMem[pOp->p1]; 6063 assert( pIn1->flags&MEM_Int ); 6064 VdbeBranchTaken(pIn1->u.i<0, 2); 6065 if( pIn1->u.i ){ 6066 if( pIn1->u.i>0 ) pIn1->u.i--; 6067 goto jump_to_p2; 6068 } 6069 break; 6070 } 6071 6072 /* Opcode: DecrJumpZero P1 P2 * * * 6073 ** Synopsis: if (--r[P1])==0 goto P2 6074 ** 6075 ** Register P1 must hold an integer. Decrement the value in P1 6076 ** and jump to P2 if the new value is exactly zero. 6077 */ 6078 case OP_DecrJumpZero: { /* jump, in1 */ 6079 pIn1 = &aMem[pOp->p1]; 6080 assert( pIn1->flags&MEM_Int ); 6081 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--; 6082 VdbeBranchTaken(pIn1->u.i==0, 2); 6083 if( pIn1->u.i==0 ) goto jump_to_p2; 6084 break; 6085 } 6086 6087 6088 /* Opcode: AggStep0 * P2 P3 P4 P5 6089 ** Synopsis: accum=r[P3] step(r[P2@P5]) 6090 ** 6091 ** Execute the step function for an aggregate. The 6092 ** function has P5 arguments. P4 is a pointer to the FuncDef 6093 ** structure that specifies the function. Register P3 is the 6094 ** accumulator. 6095 ** 6096 ** The P5 arguments are taken from register P2 and its 6097 ** successors. 6098 */ 6099 /* Opcode: AggStep * P2 P3 P4 P5 6100 ** Synopsis: accum=r[P3] step(r[P2@P5]) 6101 ** 6102 ** Execute the step function for an aggregate. The 6103 ** function has P5 arguments. P4 is a pointer to an sqlite3_context 6104 ** object that is used to run the function. Register P3 is 6105 ** as the accumulator. 6106 ** 6107 ** The P5 arguments are taken from register P2 and its 6108 ** successors. 6109 ** 6110 ** This opcode is initially coded as OP_AggStep0. On first evaluation, 6111 ** the FuncDef stored in P4 is converted into an sqlite3_context and 6112 ** the opcode is changed. In this way, the initialization of the 6113 ** sqlite3_context only happens once, instead of on each call to the 6114 ** step function. 6115 */ 6116 case OP_AggStep0: { 6117 int n; 6118 sqlite3_context *pCtx; 6119 6120 assert( pOp->p4type==P4_FUNCDEF ); 6121 n = pOp->p5; 6122 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 6123 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) ); 6124 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); 6125 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); 6126 if( pCtx==0 ) goto no_mem; 6127 pCtx->pMem = 0; 6128 pCtx->pFunc = pOp->p4.pFunc; 6129 pCtx->iOp = (int)(pOp - aOp); 6130 pCtx->pVdbe = p; 6131 pCtx->argc = n; 6132 pOp->p4type = P4_FUNCCTX; 6133 pOp->p4.pCtx = pCtx; 6134 pOp->opcode = OP_AggStep; 6135 /* Fall through into OP_AggStep */ 6136 } 6137 case OP_AggStep: { 6138 int i; 6139 sqlite3_context *pCtx; 6140 Mem *pMem; 6141 Mem t; 6142 6143 assert( pOp->p4type==P4_FUNCCTX ); 6144 pCtx = pOp->p4.pCtx; 6145 pMem = &aMem[pOp->p3]; 6146 6147 /* If this function is inside of a trigger, the register array in aMem[] 6148 ** might change from one evaluation to the next. The next block of code 6149 ** checks to see if the register array has changed, and if so it 6150 ** reinitializes the relavant parts of the sqlite3_context object */ 6151 if( pCtx->pMem != pMem ){ 6152 pCtx->pMem = pMem; 6153 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; 6154 } 6155 6156 #ifdef SQLITE_DEBUG 6157 for(i=0; i<pCtx->argc; i++){ 6158 assert( memIsValid(pCtx->argv[i]) ); 6159 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); 6160 } 6161 #endif 6162 6163 pMem->n++; 6164 sqlite3VdbeMemInit(&t, db, MEM_Null); 6165 pCtx->pOut = &t; 6166 pCtx->fErrorOrAux = 0; 6167 pCtx->skipFlag = 0; 6168 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ 6169 if( pCtx->fErrorOrAux ){ 6170 if( pCtx->isError ){ 6171 sqlite3VdbeError(p, "%s", sqlite3_value_text(&t)); 6172 rc = pCtx->isError; 6173 } 6174 sqlite3VdbeMemRelease(&t); 6175 if( rc ) goto abort_due_to_error; 6176 }else{ 6177 assert( t.flags==MEM_Null ); 6178 } 6179 if( pCtx->skipFlag ){ 6180 assert( pOp[-1].opcode==OP_CollSeq ); 6181 i = pOp[-1].p1; 6182 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); 6183 } 6184 break; 6185 } 6186 6187 /* Opcode: AggFinal P1 P2 * P4 * 6188 ** Synopsis: accum=r[P1] N=P2 6189 ** 6190 ** Execute the finalizer function for an aggregate. P1 is 6191 ** the memory location that is the accumulator for the aggregate. 6192 ** 6193 ** P2 is the number of arguments that the step function takes and 6194 ** P4 is a pointer to the FuncDef for this function. The P2 6195 ** argument is not used by this opcode. It is only there to disambiguate 6196 ** functions that can take varying numbers of arguments. The 6197 ** P4 argument is only needed for the degenerate case where 6198 ** the step function was not previously called. 6199 */ 6200 case OP_AggFinal: { 6201 Mem *pMem; 6202 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 6203 pMem = &aMem[pOp->p1]; 6204 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); 6205 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); 6206 if( rc ){ 6207 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); 6208 goto abort_due_to_error; 6209 } 6210 sqlite3VdbeChangeEncoding(pMem, encoding); 6211 UPDATE_MAX_BLOBSIZE(pMem); 6212 if( sqlite3VdbeMemTooBig(pMem) ){ 6213 goto too_big; 6214 } 6215 break; 6216 } 6217 6218 #ifndef SQLITE_OMIT_WAL 6219 /* Opcode: Checkpoint P1 P2 P3 * * 6220 ** 6221 ** Checkpoint database P1. This is a no-op if P1 is not currently in 6222 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL, 6223 ** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns 6224 ** SQLITE_BUSY or not, respectively. Write the number of pages in the 6225 ** WAL after the checkpoint into mem[P3+1] and the number of pages 6226 ** in the WAL that have been checkpointed after the checkpoint 6227 ** completes into mem[P3+2]. However on an error, mem[P3+1] and 6228 ** mem[P3+2] are initialized to -1. 6229 */ 6230 case OP_Checkpoint: { 6231 int i; /* Loop counter */ 6232 int aRes[3]; /* Results */ 6233 Mem *pMem; /* Write results here */ 6234 6235 assert( p->readOnly==0 ); 6236 aRes[0] = 0; 6237 aRes[1] = aRes[2] = -1; 6238 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE 6239 || pOp->p2==SQLITE_CHECKPOINT_FULL 6240 || pOp->p2==SQLITE_CHECKPOINT_RESTART 6241 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE 6242 ); 6243 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); 6244 if( rc ){ 6245 if( rc!=SQLITE_BUSY ) goto abort_due_to_error; 6246 rc = SQLITE_OK; 6247 aRes[0] = 1; 6248 } 6249 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ 6250 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); 6251 } 6252 break; 6253 }; 6254 #endif 6255 6256 #ifndef SQLITE_OMIT_PRAGMA 6257 /* Opcode: JournalMode P1 P2 P3 * * 6258 ** 6259 ** Change the journal mode of database P1 to P3. P3 must be one of the 6260 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback 6261 ** modes (delete, truncate, persist, off and memory), this is a simple 6262 ** operation. No IO is required. 6263 ** 6264 ** If changing into or out of WAL mode the procedure is more complicated. 6265 ** 6266 ** Write a string containing the final journal-mode to register P2. 6267 */ 6268 case OP_JournalMode: { /* out2 */ 6269 Btree *pBt; /* Btree to change journal mode of */ 6270 Pager *pPager; /* Pager associated with pBt */ 6271 int eNew; /* New journal mode */ 6272 int eOld; /* The old journal mode */ 6273 #ifndef SQLITE_OMIT_WAL 6274 const char *zFilename; /* Name of database file for pPager */ 6275 #endif 6276 6277 pOut = out2Prerelease(p, pOp); 6278 eNew = pOp->p3; 6279 assert( eNew==PAGER_JOURNALMODE_DELETE 6280 || eNew==PAGER_JOURNALMODE_TRUNCATE 6281 || eNew==PAGER_JOURNALMODE_PERSIST 6282 || eNew==PAGER_JOURNALMODE_OFF 6283 || eNew==PAGER_JOURNALMODE_MEMORY 6284 || eNew==PAGER_JOURNALMODE_WAL 6285 || eNew==PAGER_JOURNALMODE_QUERY 6286 ); 6287 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 6288 assert( p->readOnly==0 ); 6289 6290 pBt = db->aDb[pOp->p1].pBt; 6291 pPager = sqlite3BtreePager(pBt); 6292 eOld = sqlite3PagerGetJournalMode(pPager); 6293 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; 6294 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; 6295 6296 #ifndef SQLITE_OMIT_WAL 6297 zFilename = sqlite3PagerFilename(pPager, 1); 6298 6299 /* Do not allow a transition to journal_mode=WAL for a database 6300 ** in temporary storage or if the VFS does not support shared memory 6301 */ 6302 if( eNew==PAGER_JOURNALMODE_WAL 6303 && (sqlite3Strlen30(zFilename)==0 /* Temp file */ 6304 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ 6305 ){ 6306 eNew = eOld; 6307 } 6308 6309 if( (eNew!=eOld) 6310 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) 6311 ){ 6312 if( !db->autoCommit || db->nVdbeRead>1 ){ 6313 rc = SQLITE_ERROR; 6314 sqlite3VdbeError(p, 6315 "cannot change %s wal mode from within a transaction", 6316 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") 6317 ); 6318 goto abort_due_to_error; 6319 }else{ 6320 6321 if( eOld==PAGER_JOURNALMODE_WAL ){ 6322 /* If leaving WAL mode, close the log file. If successful, the call 6323 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log 6324 ** file. An EXCLUSIVE lock may still be held on the database file 6325 ** after a successful return. 6326 */ 6327 rc = sqlite3PagerCloseWal(pPager, db); 6328 if( rc==SQLITE_OK ){ 6329 sqlite3PagerSetJournalMode(pPager, eNew); 6330 } 6331 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ 6332 /* Cannot transition directly from MEMORY to WAL. Use mode OFF 6333 ** as an intermediate */ 6334 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); 6335 } 6336 6337 /* Open a transaction on the database file. Regardless of the journal 6338 ** mode, this transaction always uses a rollback journal. 6339 */ 6340 assert( sqlite3BtreeIsInTrans(pBt)==0 ); 6341 if( rc==SQLITE_OK ){ 6342 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); 6343 } 6344 } 6345 } 6346 #endif /* ifndef SQLITE_OMIT_WAL */ 6347 6348 if( rc ) eNew = eOld; 6349 eNew = sqlite3PagerSetJournalMode(pPager, eNew); 6350 6351 pOut->flags = MEM_Str|MEM_Static|MEM_Term; 6352 pOut->z = (char *)sqlite3JournalModename(eNew); 6353 pOut->n = sqlite3Strlen30(pOut->z); 6354 pOut->enc = SQLITE_UTF8; 6355 sqlite3VdbeChangeEncoding(pOut, encoding); 6356 if( rc ) goto abort_due_to_error; 6357 break; 6358 }; 6359 #endif /* SQLITE_OMIT_PRAGMA */ 6360 6361 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) 6362 /* Opcode: Vacuum P1 * * * * 6363 ** 6364 ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more 6365 ** for an attached database. The "temp" database may not be vacuumed. 6366 */ 6367 case OP_Vacuum: { 6368 assert( p->readOnly==0 ); 6369 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1); 6370 if( rc ) goto abort_due_to_error; 6371 break; 6372 } 6373 #endif 6374 6375 #if !defined(SQLITE_OMIT_AUTOVACUUM) 6376 /* Opcode: IncrVacuum P1 P2 * * * 6377 ** 6378 ** Perform a single step of the incremental vacuum procedure on 6379 ** the P1 database. If the vacuum has finished, jump to instruction 6380 ** P2. Otherwise, fall through to the next instruction. 6381 */ 6382 case OP_IncrVacuum: { /* jump */ 6383 Btree *pBt; 6384 6385 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 6386 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 6387 assert( p->readOnly==0 ); 6388 pBt = db->aDb[pOp->p1].pBt; 6389 rc = sqlite3BtreeIncrVacuum(pBt); 6390 VdbeBranchTaken(rc==SQLITE_DONE,2); 6391 if( rc ){ 6392 if( rc!=SQLITE_DONE ) goto abort_due_to_error; 6393 rc = SQLITE_OK; 6394 goto jump_to_p2; 6395 } 6396 break; 6397 } 6398 #endif 6399 6400 /* Opcode: Expire P1 * * * * 6401 ** 6402 ** Cause precompiled statements to expire. When an expired statement 6403 ** is executed using sqlite3_step() it will either automatically 6404 ** reprepare itself (if it was originally created using sqlite3_prepare_v2()) 6405 ** or it will fail with SQLITE_SCHEMA. 6406 ** 6407 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, 6408 ** then only the currently executing statement is expired. 6409 */ 6410 case OP_Expire: { 6411 if( !pOp->p1 ){ 6412 sqlite3ExpirePreparedStatements(db); 6413 }else{ 6414 p->expired = 1; 6415 } 6416 break; 6417 } 6418 6419 #ifndef SQLITE_OMIT_SHARED_CACHE 6420 /* Opcode: TableLock P1 P2 P3 P4 * 6421 ** Synopsis: iDb=P1 root=P2 write=P3 6422 ** 6423 ** Obtain a lock on a particular table. This instruction is only used when 6424 ** the shared-cache feature is enabled. 6425 ** 6426 ** P1 is the index of the database in sqlite3.aDb[] of the database 6427 ** on which the lock is acquired. A readlock is obtained if P3==0 or 6428 ** a write lock if P3==1. 6429 ** 6430 ** P2 contains the root-page of the table to lock. 6431 ** 6432 ** P4 contains a pointer to the name of the table being locked. This is only 6433 ** used to generate an error message if the lock cannot be obtained. 6434 */ 6435 case OP_TableLock: { 6436 u8 isWriteLock = (u8)pOp->p3; 6437 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){ 6438 int p1 = pOp->p1; 6439 assert( p1>=0 && p1<db->nDb ); 6440 assert( DbMaskTest(p->btreeMask, p1) ); 6441 assert( isWriteLock==0 || isWriteLock==1 ); 6442 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); 6443 if( rc ){ 6444 if( (rc&0xFF)==SQLITE_LOCKED ){ 6445 const char *z = pOp->p4.z; 6446 sqlite3VdbeError(p, "database table is locked: %s", z); 6447 } 6448 goto abort_due_to_error; 6449 } 6450 } 6451 break; 6452 } 6453 #endif /* SQLITE_OMIT_SHARED_CACHE */ 6454 6455 #ifndef SQLITE_OMIT_VIRTUALTABLE 6456 /* Opcode: VBegin * * * P4 * 6457 ** 6458 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the 6459 ** xBegin method for that table. 6460 ** 6461 ** Also, whether or not P4 is set, check that this is not being called from 6462 ** within a callback to a virtual table xSync() method. If it is, the error 6463 ** code will be set to SQLITE_LOCKED. 6464 */ 6465 case OP_VBegin: { 6466 VTable *pVTab; 6467 pVTab = pOp->p4.pVtab; 6468 rc = sqlite3VtabBegin(db, pVTab); 6469 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); 6470 if( rc ) goto abort_due_to_error; 6471 break; 6472 } 6473 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6474 6475 #ifndef SQLITE_OMIT_VIRTUALTABLE 6476 /* Opcode: VCreate P1 P2 * * * 6477 ** 6478 ** P2 is a register that holds the name of a virtual table in database 6479 ** P1. Call the xCreate method for that table. 6480 */ 6481 case OP_VCreate: { 6482 Mem sMem; /* For storing the record being decoded */ 6483 const char *zTab; /* Name of the virtual table */ 6484 6485 memset(&sMem, 0, sizeof(sMem)); 6486 sMem.db = db; 6487 /* Because P2 is always a static string, it is impossible for the 6488 ** sqlite3VdbeMemCopy() to fail */ 6489 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 ); 6490 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 ); 6491 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]); 6492 assert( rc==SQLITE_OK ); 6493 zTab = (const char*)sqlite3_value_text(&sMem); 6494 assert( zTab || db->mallocFailed ); 6495 if( zTab ){ 6496 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); 6497 } 6498 sqlite3VdbeMemRelease(&sMem); 6499 if( rc ) goto abort_due_to_error; 6500 break; 6501 } 6502 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6503 6504 #ifndef SQLITE_OMIT_VIRTUALTABLE 6505 /* Opcode: VDestroy P1 * * P4 * 6506 ** 6507 ** P4 is the name of a virtual table in database P1. Call the xDestroy method 6508 ** of that table. 6509 */ 6510 case OP_VDestroy: { 6511 db->nVDestroy++; 6512 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); 6513 db->nVDestroy--; 6514 if( rc ) goto abort_due_to_error; 6515 break; 6516 } 6517 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6518 6519 #ifndef SQLITE_OMIT_VIRTUALTABLE 6520 /* Opcode: VOpen P1 * * P4 * 6521 ** 6522 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. 6523 ** P1 is a cursor number. This opcode opens a cursor to the virtual 6524 ** table and stores that cursor in P1. 6525 */ 6526 case OP_VOpen: { 6527 VdbeCursor *pCur; 6528 sqlite3_vtab_cursor *pVCur; 6529 sqlite3_vtab *pVtab; 6530 const sqlite3_module *pModule; 6531 6532 assert( p->bIsReader ); 6533 pCur = 0; 6534 pVCur = 0; 6535 pVtab = pOp->p4.pVtab->pVtab; 6536 if( pVtab==0 || NEVER(pVtab->pModule==0) ){ 6537 rc = SQLITE_LOCKED; 6538 goto abort_due_to_error; 6539 } 6540 pModule = pVtab->pModule; 6541 rc = pModule->xOpen(pVtab, &pVCur); 6542 sqlite3VtabImportErrmsg(p, pVtab); 6543 if( rc ) goto abort_due_to_error; 6544 6545 /* Initialize sqlite3_vtab_cursor base class */ 6546 pVCur->pVtab = pVtab; 6547 6548 /* Initialize vdbe cursor object */ 6549 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB); 6550 if( pCur ){ 6551 pCur->uc.pVCur = pVCur; 6552 pVtab->nRef++; 6553 }else{ 6554 assert( db->mallocFailed ); 6555 pModule->xClose(pVCur); 6556 goto no_mem; 6557 } 6558 break; 6559 } 6560 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6561 6562 #ifndef SQLITE_OMIT_VIRTUALTABLE 6563 /* Opcode: VFilter P1 P2 P3 P4 * 6564 ** Synopsis: iplan=r[P3] zplan='P4' 6565 ** 6566 ** P1 is a cursor opened using VOpen. P2 is an address to jump to if 6567 ** the filtered result set is empty. 6568 ** 6569 ** P4 is either NULL or a string that was generated by the xBestIndex 6570 ** method of the module. The interpretation of the P4 string is left 6571 ** to the module implementation. 6572 ** 6573 ** This opcode invokes the xFilter method on the virtual table specified 6574 ** by P1. The integer query plan parameter to xFilter is stored in register 6575 ** P3. Register P3+1 stores the argc parameter to be passed to the 6576 ** xFilter method. Registers P3+2..P3+1+argc are the argc 6577 ** additional parameters which are passed to 6578 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. 6579 ** 6580 ** A jump is made to P2 if the result set after filtering would be empty. 6581 */ 6582 case OP_VFilter: { /* jump */ 6583 int nArg; 6584 int iQuery; 6585 const sqlite3_module *pModule; 6586 Mem *pQuery; 6587 Mem *pArgc; 6588 sqlite3_vtab_cursor *pVCur; 6589 sqlite3_vtab *pVtab; 6590 VdbeCursor *pCur; 6591 int res; 6592 int i; 6593 Mem **apArg; 6594 6595 pQuery = &aMem[pOp->p3]; 6596 pArgc = &pQuery[1]; 6597 pCur = p->apCsr[pOp->p1]; 6598 assert( memIsValid(pQuery) ); 6599 REGISTER_TRACE(pOp->p3, pQuery); 6600 assert( pCur->eCurType==CURTYPE_VTAB ); 6601 pVCur = pCur->uc.pVCur; 6602 pVtab = pVCur->pVtab; 6603 pModule = pVtab->pModule; 6604 6605 /* Grab the index number and argc parameters */ 6606 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); 6607 nArg = (int)pArgc->u.i; 6608 iQuery = (int)pQuery->u.i; 6609 6610 /* Invoke the xFilter method */ 6611 res = 0; 6612 apArg = p->apArg; 6613 for(i = 0; i<nArg; i++){ 6614 apArg[i] = &pArgc[i+1]; 6615 } 6616 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg); 6617 sqlite3VtabImportErrmsg(p, pVtab); 6618 if( rc ) goto abort_due_to_error; 6619 res = pModule->xEof(pVCur); 6620 pCur->nullRow = 0; 6621 VdbeBranchTaken(res!=0,2); 6622 if( res ) goto jump_to_p2; 6623 break; 6624 } 6625 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6626 6627 #ifndef SQLITE_OMIT_VIRTUALTABLE 6628 /* Opcode: VColumn P1 P2 P3 * * 6629 ** Synopsis: r[P3]=vcolumn(P2) 6630 ** 6631 ** Store the value of the P2-th column of 6632 ** the row of the virtual-table that the 6633 ** P1 cursor is pointing to into register P3. 6634 */ 6635 case OP_VColumn: { 6636 sqlite3_vtab *pVtab; 6637 const sqlite3_module *pModule; 6638 Mem *pDest; 6639 sqlite3_context sContext; 6640 6641 VdbeCursor *pCur = p->apCsr[pOp->p1]; 6642 assert( pCur->eCurType==CURTYPE_VTAB ); 6643 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 6644 pDest = &aMem[pOp->p3]; 6645 memAboutToChange(p, pDest); 6646 if( pCur->nullRow ){ 6647 sqlite3VdbeMemSetNull(pDest); 6648 break; 6649 } 6650 pVtab = pCur->uc.pVCur->pVtab; 6651 pModule = pVtab->pModule; 6652 assert( pModule->xColumn ); 6653 memset(&sContext, 0, sizeof(sContext)); 6654 sContext.pOut = pDest; 6655 MemSetTypeFlag(pDest, MEM_Null); 6656 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2); 6657 sqlite3VtabImportErrmsg(p, pVtab); 6658 if( sContext.isError ){ 6659 rc = sContext.isError; 6660 } 6661 sqlite3VdbeChangeEncoding(pDest, encoding); 6662 REGISTER_TRACE(pOp->p3, pDest); 6663 UPDATE_MAX_BLOBSIZE(pDest); 6664 6665 if( sqlite3VdbeMemTooBig(pDest) ){ 6666 goto too_big; 6667 } 6668 if( rc ) goto abort_due_to_error; 6669 break; 6670 } 6671 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6672 6673 #ifndef SQLITE_OMIT_VIRTUALTABLE 6674 /* Opcode: VNext P1 P2 * * * 6675 ** 6676 ** Advance virtual table P1 to the next row in its result set and 6677 ** jump to instruction P2. Or, if the virtual table has reached 6678 ** the end of its result set, then fall through to the next instruction. 6679 */ 6680 case OP_VNext: { /* jump */ 6681 sqlite3_vtab *pVtab; 6682 const sqlite3_module *pModule; 6683 int res; 6684 VdbeCursor *pCur; 6685 6686 res = 0; 6687 pCur = p->apCsr[pOp->p1]; 6688 assert( pCur->eCurType==CURTYPE_VTAB ); 6689 if( pCur->nullRow ){ 6690 break; 6691 } 6692 pVtab = pCur->uc.pVCur->pVtab; 6693 pModule = pVtab->pModule; 6694 assert( pModule->xNext ); 6695 6696 /* Invoke the xNext() method of the module. There is no way for the 6697 ** underlying implementation to return an error if one occurs during 6698 ** xNext(). Instead, if an error occurs, true is returned (indicating that 6699 ** data is available) and the error code returned when xColumn or 6700 ** some other method is next invoked on the save virtual table cursor. 6701 */ 6702 rc = pModule->xNext(pCur->uc.pVCur); 6703 sqlite3VtabImportErrmsg(p, pVtab); 6704 if( rc ) goto abort_due_to_error; 6705 res = pModule->xEof(pCur->uc.pVCur); 6706 VdbeBranchTaken(!res,2); 6707 if( !res ){ 6708 /* If there is data, jump to P2 */ 6709 goto jump_to_p2_and_check_for_interrupt; 6710 } 6711 goto check_for_interrupt; 6712 } 6713 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6714 6715 #ifndef SQLITE_OMIT_VIRTUALTABLE 6716 /* Opcode: VRename P1 * * P4 * 6717 ** 6718 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. 6719 ** This opcode invokes the corresponding xRename method. The value 6720 ** in register P1 is passed as the zName argument to the xRename method. 6721 */ 6722 case OP_VRename: { 6723 sqlite3_vtab *pVtab; 6724 Mem *pName; 6725 6726 pVtab = pOp->p4.pVtab->pVtab; 6727 pName = &aMem[pOp->p1]; 6728 assert( pVtab->pModule->xRename ); 6729 assert( memIsValid(pName) ); 6730 assert( p->readOnly==0 ); 6731 REGISTER_TRACE(pOp->p1, pName); 6732 assert( pName->flags & MEM_Str ); 6733 testcase( pName->enc==SQLITE_UTF8 ); 6734 testcase( pName->enc==SQLITE_UTF16BE ); 6735 testcase( pName->enc==SQLITE_UTF16LE ); 6736 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); 6737 if( rc ) goto abort_due_to_error; 6738 rc = pVtab->pModule->xRename(pVtab, pName->z); 6739 sqlite3VtabImportErrmsg(p, pVtab); 6740 p->expired = 0; 6741 if( rc ) goto abort_due_to_error; 6742 break; 6743 } 6744 #endif 6745 6746 #ifndef SQLITE_OMIT_VIRTUALTABLE 6747 /* Opcode: VUpdate P1 P2 P3 P4 P5 6748 ** Synopsis: data=r[P3@P2] 6749 ** 6750 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. 6751 ** This opcode invokes the corresponding xUpdate method. P2 values 6752 ** are contiguous memory cells starting at P3 to pass to the xUpdate 6753 ** invocation. The value in register (P3+P2-1) corresponds to the 6754 ** p2th element of the argv array passed to xUpdate. 6755 ** 6756 ** The xUpdate method will do a DELETE or an INSERT or both. 6757 ** The argv[0] element (which corresponds to memory cell P3) 6758 ** is the rowid of a row to delete. If argv[0] is NULL then no 6759 ** deletion occurs. The argv[1] element is the rowid of the new 6760 ** row. This can be NULL to have the virtual table select the new 6761 ** rowid for itself. The subsequent elements in the array are 6762 ** the values of columns in the new row. 6763 ** 6764 ** If P2==1 then no insert is performed. argv[0] is the rowid of 6765 ** a row to delete. 6766 ** 6767 ** P1 is a boolean flag. If it is set to true and the xUpdate call 6768 ** is successful, then the value returned by sqlite3_last_insert_rowid() 6769 ** is set to the value of the rowid for the row just inserted. 6770 ** 6771 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to 6772 ** apply in the case of a constraint failure on an insert or update. 6773 */ 6774 case OP_VUpdate: { 6775 sqlite3_vtab *pVtab; 6776 const sqlite3_module *pModule; 6777 int nArg; 6778 int i; 6779 sqlite_int64 rowid; 6780 Mem **apArg; 6781 Mem *pX; 6782 6783 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback 6784 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace 6785 ); 6786 assert( p->readOnly==0 ); 6787 pVtab = pOp->p4.pVtab->pVtab; 6788 if( pVtab==0 || NEVER(pVtab->pModule==0) ){ 6789 rc = SQLITE_LOCKED; 6790 goto abort_due_to_error; 6791 } 6792 pModule = pVtab->pModule; 6793 nArg = pOp->p2; 6794 assert( pOp->p4type==P4_VTAB ); 6795 if( ALWAYS(pModule->xUpdate) ){ 6796 u8 vtabOnConflict = db->vtabOnConflict; 6797 apArg = p->apArg; 6798 pX = &aMem[pOp->p3]; 6799 for(i=0; i<nArg; i++){ 6800 assert( memIsValid(pX) ); 6801 memAboutToChange(p, pX); 6802 apArg[i] = pX; 6803 pX++; 6804 } 6805 db->vtabOnConflict = pOp->p5; 6806 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); 6807 db->vtabOnConflict = vtabOnConflict; 6808 sqlite3VtabImportErrmsg(p, pVtab); 6809 if( rc==SQLITE_OK && pOp->p1 ){ 6810 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); 6811 db->lastRowid = rowid; 6812 } 6813 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ 6814 if( pOp->p5==OE_Ignore ){ 6815 rc = SQLITE_OK; 6816 }else{ 6817 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); 6818 } 6819 }else{ 6820 p->nChange++; 6821 } 6822 if( rc ) goto abort_due_to_error; 6823 } 6824 break; 6825 } 6826 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 6827 6828 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 6829 /* Opcode: Pagecount P1 P2 * * * 6830 ** 6831 ** Write the current number of pages in database P1 to memory cell P2. 6832 */ 6833 case OP_Pagecount: { /* out2 */ 6834 pOut = out2Prerelease(p, pOp); 6835 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); 6836 break; 6837 } 6838 #endif 6839 6840 6841 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 6842 /* Opcode: MaxPgcnt P1 P2 P3 * * 6843 ** 6844 ** Try to set the maximum page count for database P1 to the value in P3. 6845 ** Do not let the maximum page count fall below the current page count and 6846 ** do not change the maximum page count value if P3==0. 6847 ** 6848 ** Store the maximum page count after the change in register P2. 6849 */ 6850 case OP_MaxPgcnt: { /* out2 */ 6851 unsigned int newMax; 6852 Btree *pBt; 6853 6854 pOut = out2Prerelease(p, pOp); 6855 pBt = db->aDb[pOp->p1].pBt; 6856 newMax = 0; 6857 if( pOp->p3 ){ 6858 newMax = sqlite3BtreeLastPage(pBt); 6859 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; 6860 } 6861 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); 6862 break; 6863 } 6864 #endif 6865 6866 6867 /* Opcode: Init P1 P2 * P4 * 6868 ** Synopsis: Start at P2 6869 ** 6870 ** Programs contain a single instance of this opcode as the very first 6871 ** opcode. 6872 ** 6873 ** If tracing is enabled (by the sqlite3_trace()) interface, then 6874 ** the UTF-8 string contained in P4 is emitted on the trace callback. 6875 ** Or if P4 is blank, use the string returned by sqlite3_sql(). 6876 ** 6877 ** If P2 is not zero, jump to instruction P2. 6878 ** 6879 ** Increment the value of P1 so that OP_Once opcodes will jump the 6880 ** first time they are evaluated for this run. 6881 */ 6882 case OP_Init: { /* jump */ 6883 char *zTrace; 6884 int i; 6885 6886 /* If the P4 argument is not NULL, then it must be an SQL comment string. 6887 ** The "--" string is broken up to prevent false-positives with srcck1.c. 6888 ** 6889 ** This assert() provides evidence for: 6890 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that 6891 ** would have been returned by the legacy sqlite3_trace() interface by 6892 ** using the X argument when X begins with "--" and invoking 6893 ** sqlite3_expanded_sql(P) otherwise. 6894 */ 6895 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 ); 6896 assert( pOp==p->aOp ); /* Always instruction 0 */ 6897 6898 #ifndef SQLITE_OMIT_TRACE 6899 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0 6900 && !p->doingRerun 6901 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 6902 ){ 6903 #ifndef SQLITE_OMIT_DEPRECATED 6904 if( db->mTrace & SQLITE_TRACE_LEGACY ){ 6905 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace; 6906 char *z = sqlite3VdbeExpandSql(p, zTrace); 6907 x(db->pTraceArg, z); 6908 sqlite3_free(z); 6909 }else 6910 #endif 6911 { 6912 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace); 6913 } 6914 } 6915 #ifdef SQLITE_USE_FCNTL_TRACE 6916 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); 6917 if( zTrace ){ 6918 int j; 6919 for(j=0; j<db->nDb; j++){ 6920 if( DbMaskTest(p->btreeMask, j)==0 ) continue; 6921 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace); 6922 } 6923 } 6924 #endif /* SQLITE_USE_FCNTL_TRACE */ 6925 #ifdef SQLITE_DEBUG 6926 if( (db->flags & SQLITE_SqlTrace)!=0 6927 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 6928 ){ 6929 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); 6930 } 6931 #endif /* SQLITE_DEBUG */ 6932 #endif /* SQLITE_OMIT_TRACE */ 6933 assert( pOp->p2>0 ); 6934 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){ 6935 for(i=1; i<p->nOp; i++){ 6936 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0; 6937 } 6938 pOp->p1 = 0; 6939 } 6940 pOp->p1++; 6941 goto jump_to_p2; 6942 } 6943 6944 #ifdef SQLITE_ENABLE_CURSOR_HINTS 6945 /* Opcode: CursorHint P1 * * P4 * 6946 ** 6947 ** Provide a hint to cursor P1 that it only needs to return rows that 6948 ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer 6949 ** to values currently held in registers. TK_COLUMN terms in the P4 6950 ** expression refer to columns in the b-tree to which cursor P1 is pointing. 6951 */ 6952 case OP_CursorHint: { 6953 VdbeCursor *pC; 6954 6955 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 6956 assert( pOp->p4type==P4_EXPR ); 6957 pC = p->apCsr[pOp->p1]; 6958 if( pC ){ 6959 assert( pC->eCurType==CURTYPE_BTREE ); 6960 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE, 6961 pOp->p4.pExpr, aMem); 6962 } 6963 break; 6964 } 6965 #endif /* SQLITE_ENABLE_CURSOR_HINTS */ 6966 6967 /* Opcode: Noop * * * * * 6968 ** 6969 ** Do nothing. This instruction is often useful as a jump 6970 ** destination. 6971 */ 6972 /* 6973 ** The magic Explain opcode are only inserted when explain==2 (which 6974 ** is to say when the EXPLAIN QUERY PLAN syntax is used.) 6975 ** This opcode records information from the optimizer. It is the 6976 ** the same as a no-op. This opcodesnever appears in a real VM program. 6977 */ 6978 default: { /* This is really OP_Noop and OP_Explain */ 6979 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); 6980 break; 6981 } 6982 6983 /***************************************************************************** 6984 ** The cases of the switch statement above this line should all be indented 6985 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the 6986 ** readability. From this point on down, the normal indentation rules are 6987 ** restored. 6988 *****************************************************************************/ 6989 } 6990 6991 #ifdef VDBE_PROFILE 6992 { 6993 u64 endTime = sqlite3Hwtime(); 6994 if( endTime>start ) pOrigOp->cycles += endTime - start; 6995 pOrigOp->cnt++; 6996 } 6997 #endif 6998 6999 /* The following code adds nothing to the actual functionality 7000 ** of the program. It is only here for testing and debugging. 7001 ** On the other hand, it does burn CPU cycles every time through 7002 ** the evaluator loop. So we can leave it out when NDEBUG is defined. 7003 */ 7004 #ifndef NDEBUG 7005 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] ); 7006 7007 #ifdef SQLITE_DEBUG 7008 if( db->flags & SQLITE_VdbeTrace ){ 7009 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode]; 7010 if( rc!=0 ) printf("rc=%d\n",rc); 7011 if( opProperty & (OPFLG_OUT2) ){ 7012 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); 7013 } 7014 if( opProperty & OPFLG_OUT3 ){ 7015 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); 7016 } 7017 } 7018 #endif /* SQLITE_DEBUG */ 7019 #endif /* NDEBUG */ 7020 } /* The end of the for(;;) loop the loops through opcodes */ 7021 7022 /* If we reach this point, it means that execution is finished with 7023 ** an error of some kind. 7024 */ 7025 abort_due_to_error: 7026 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT; 7027 assert( rc ); 7028 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){ 7029 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); 7030 } 7031 p->rc = rc; 7032 sqlite3SystemError(db, rc); 7033 testcase( sqlite3GlobalConfig.xLog!=0 ); 7034 sqlite3_log(rc, "statement aborts at %d: [%s] %s", 7035 (int)(pOp - aOp), p->zSql, p->zErrMsg); 7036 sqlite3VdbeHalt(p); 7037 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db); 7038 rc = SQLITE_ERROR; 7039 if( resetSchemaOnFault>0 ){ 7040 sqlite3ResetOneSchema(db, resetSchemaOnFault-1); 7041 } 7042 7043 /* This is the only way out of this procedure. We have to 7044 ** release the mutexes on btrees that were acquired at the 7045 ** top. */ 7046 vdbe_return: 7047 testcase( nVmStep>0 ); 7048 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; 7049 sqlite3VdbeLeave(p); 7050 assert( rc!=SQLITE_OK || nExtraDelete==0 7051 || sqlite3_strlike("DELETE%",p->zSql,0)!=0 7052 ); 7053 return rc; 7054 7055 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH 7056 ** is encountered. 7057 */ 7058 too_big: 7059 sqlite3VdbeError(p, "string or blob too big"); 7060 rc = SQLITE_TOOBIG; 7061 goto abort_due_to_error; 7062 7063 /* Jump to here if a malloc() fails. 7064 */ 7065 no_mem: 7066 sqlite3OomFault(db); 7067 sqlite3VdbeError(p, "out of memory"); 7068 rc = SQLITE_NOMEM_BKPT; 7069 goto abort_due_to_error; 7070 7071 /* Jump to here if the sqlite3_interrupt() API sets the interrupt 7072 ** flag. 7073 */ 7074 abort_due_to_interrupt: 7075 assert( db->u1.isInterrupted ); 7076 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT; 7077 p->rc = rc; 7078 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); 7079 goto abort_due_to_error; 7080 } 7081