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