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