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