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