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