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