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