000001 /* 000002 ** 2001 September 15 000003 ** 000004 ** The author disclaims copyright to this source code. In place of 000005 ** a legal notice, here is a blessing: 000006 ** 000007 ** May you do good and not evil. 000008 ** May you find forgiveness for yourself and forgive others. 000009 ** May you share freely, never taking more than you give. 000010 ** 000011 ************************************************************************* 000012 ** The code in this file implements the function that runs the 000013 ** bytecode of a prepared statement. 000014 ** 000015 ** Various scripts scan this source file in order to generate HTML 000016 ** documentation, headers files, or other derived files. The formatting 000017 ** of the code in this file is, therefore, important. See other comments 000018 ** in this file for details. If in doubt, do not deviate from existing 000019 ** commenting and indentation practices when changing or adding code. 000020 */ 000021 #include "sqliteInt.h" 000022 #include "vdbeInt.h" 000023 000024 /* 000025 ** Invoke this macro on memory cells just prior to changing the 000026 ** value of the cell. This macro verifies that shallow copies are 000027 ** not misused. A shallow copy of a string or blob just copies a 000028 ** pointer to the string or blob, not the content. If the original 000029 ** is changed while the copy is still in use, the string or blob might 000030 ** be changed out from under the copy. This macro verifies that nothing 000031 ** like that ever happens. 000032 */ 000033 #ifdef SQLITE_DEBUG 000034 # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M) 000035 #else 000036 # define memAboutToChange(P,M) 000037 #endif 000038 000039 /* 000040 ** The following global variable is incremented every time a cursor 000041 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test 000042 ** procedures use this information to make sure that indices are 000043 ** working correctly. This variable has no function other than to 000044 ** help verify the correct operation of the library. 000045 */ 000046 #ifdef SQLITE_TEST 000047 int sqlite3_search_count = 0; 000048 #endif 000049 000050 /* 000051 ** When this global variable is positive, it gets decremented once before 000052 ** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted 000053 ** field of the sqlite3 structure is set in order to simulate an interrupt. 000054 ** 000055 ** This facility is used for testing purposes only. It does not function 000056 ** in an ordinary build. 000057 */ 000058 #ifdef SQLITE_TEST 000059 int sqlite3_interrupt_count = 0; 000060 #endif 000061 000062 /* 000063 ** The next global variable is incremented each type the OP_Sort opcode 000064 ** is executed. The test procedures use this information to make sure that 000065 ** sorting is occurring or not occurring at appropriate times. This variable 000066 ** has no function other than to help verify the correct operation of the 000067 ** library. 000068 */ 000069 #ifdef SQLITE_TEST 000070 int sqlite3_sort_count = 0; 000071 #endif 000072 000073 /* 000074 ** The next global variable records the size of the largest MEM_Blob 000075 ** or MEM_Str that has been used by a VDBE opcode. The test procedures 000076 ** use this information to make sure that the zero-blob functionality 000077 ** is working correctly. This variable has no function other than to 000078 ** help verify the correct operation of the library. 000079 */ 000080 #ifdef SQLITE_TEST 000081 int sqlite3_max_blobsize = 0; 000082 static void updateMaxBlobsize(Mem *p){ 000083 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ 000084 sqlite3_max_blobsize = p->n; 000085 } 000086 } 000087 #endif 000088 000089 /* 000090 ** This macro evaluates to true if either the update hook or the preupdate 000091 ** hook are enabled for database connect DB. 000092 */ 000093 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 000094 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) 000095 #else 000096 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback) 000097 #endif 000098 000099 /* 000100 ** The next global variable is incremented each time the OP_Found opcode 000101 ** is executed. This is used to test whether or not the foreign key 000102 ** operation implemented using OP_FkIsZero is working. This variable 000103 ** has no function other than to help verify the correct operation of the 000104 ** library. 000105 */ 000106 #ifdef SQLITE_TEST 000107 int sqlite3_found_count = 0; 000108 #endif 000109 000110 /* 000111 ** Test a register to see if it exceeds the current maximum blob size. 000112 ** If it does, record the new maximum blob size. 000113 */ 000114 #if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE) 000115 # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) 000116 #else 000117 # define UPDATE_MAX_BLOBSIZE(P) 000118 #endif 000119 000120 /* 000121 ** Invoke the VDBE coverage callback, if that callback is defined. This 000122 ** feature is used for test suite validation only and does not appear an 000123 ** production builds. 000124 ** 000125 ** M is an integer, 2 or 3, that indices how many different ways the 000126 ** branch can go. It is usually 2. "I" is the direction the branch 000127 ** goes. 0 means falls through. 1 means branch is taken. 2 means the 000128 ** second alternative branch is taken. 000129 ** 000130 ** iSrcLine is the source code line (from the __LINE__ macro) that 000131 ** generated the VDBE instruction. This instrumentation assumes that all 000132 ** source code is in a single file (the amalgamation). Special values 1 000133 ** and 2 for the iSrcLine parameter mean that this particular branch is 000134 ** always taken or never taken, respectively. 000135 */ 000136 #if !defined(SQLITE_VDBE_COVERAGE) 000137 # define VdbeBranchTaken(I,M) 000138 #else 000139 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M) 000140 static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){ 000141 if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){ 000142 M = iSrcLine; 000143 /* Assert the truth of VdbeCoverageAlwaysTaken() and 000144 ** VdbeCoverageNeverTaken() */ 000145 assert( (M & I)==I ); 000146 }else{ 000147 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/ 000148 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, 000149 iSrcLine,I,M); 000150 } 000151 } 000152 #endif 000153 000154 /* 000155 ** Convert the given register into a string if it isn't one 000156 ** already. Return non-zero if a malloc() fails. 000157 */ 000158 #define Stringify(P, enc) \ 000159 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \ 000160 { goto no_mem; } 000161 000162 /* 000163 ** An ephemeral string value (signified by the MEM_Ephem flag) contains 000164 ** a pointer to a dynamically allocated string where some other entity 000165 ** is responsible for deallocating that string. Because the register 000166 ** does not control the string, it might be deleted without the register 000167 ** knowing it. 000168 ** 000169 ** This routine converts an ephemeral string into a dynamically allocated 000170 ** string that the register itself controls. In other words, it 000171 ** converts an MEM_Ephem string into a string with P.z==P.zMalloc. 000172 */ 000173 #define Deephemeralize(P) \ 000174 if( ((P)->flags&MEM_Ephem)!=0 \ 000175 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} 000176 000177 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */ 000178 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER) 000179 000180 /* 000181 ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL 000182 ** if we run out of memory. 000183 */ 000184 static VdbeCursor *allocateCursor( 000185 Vdbe *p, /* The virtual machine */ 000186 int iCur, /* Index of the new VdbeCursor */ 000187 int nField, /* Number of fields in the table or index */ 000188 int iDb, /* Database the cursor belongs to, or -1 */ 000189 u8 eCurType /* Type of the new cursor */ 000190 ){ 000191 /* Find the memory cell that will be used to store the blob of memory 000192 ** required for this VdbeCursor structure. It is convenient to use a 000193 ** vdbe memory cell to manage the memory allocation required for a 000194 ** VdbeCursor structure for the following reasons: 000195 ** 000196 ** * Sometimes cursor numbers are used for a couple of different 000197 ** purposes in a vdbe program. The different uses might require 000198 ** different sized allocations. Memory cells provide growable 000199 ** allocations. 000200 ** 000201 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can 000202 ** be freed lazily via the sqlite3_release_memory() API. This 000203 ** minimizes the number of malloc calls made by the system. 000204 ** 000205 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from 000206 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1]. 000207 ** Cursor 2 is at Mem[p->nMem-2]. And so forth. 000208 */ 000209 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem; 000210 000211 int nByte; 000212 VdbeCursor *pCx = 0; 000213 nByte = 000214 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + 000215 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0); 000216 000217 assert( iCur>=0 && iCur<p->nCursor ); 000218 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/ 000219 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); 000220 p->apCsr[iCur] = 0; 000221 } 000222 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){ 000223 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; 000224 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor)); 000225 pCx->eCurType = eCurType; 000226 pCx->iDb = iDb; 000227 pCx->nField = nField; 000228 pCx->aOffset = &pCx->aType[nField]; 000229 if( eCurType==CURTYPE_BTREE ){ 000230 pCx->uc.pCursor = (BtCursor*) 000231 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField]; 000232 sqlite3BtreeCursorZero(pCx->uc.pCursor); 000233 } 000234 } 000235 return pCx; 000236 } 000237 000238 /* 000239 ** Try to convert a value into a numeric representation if we can 000240 ** do so without loss of information. In other words, if the string 000241 ** looks like a number, convert it into a number. If it does not 000242 ** look like a number, leave it alone. 000243 ** 000244 ** If the bTryForInt flag is true, then extra effort is made to give 000245 ** an integer representation. Strings that look like floating point 000246 ** values but which have no fractional component (example: '48.00') 000247 ** will have a MEM_Int representation when bTryForInt is true. 000248 ** 000249 ** If bTryForInt is false, then if the input string contains a decimal 000250 ** point or exponential notation, the result is only MEM_Real, even 000251 ** if there is an exact integer representation of the quantity. 000252 */ 000253 static void applyNumericAffinity(Mem *pRec, int bTryForInt){ 000254 double rValue; 000255 i64 iValue; 000256 u8 enc = pRec->enc; 000257 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str ); 000258 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return; 000259 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){ 000260 pRec->u.i = iValue; 000261 pRec->flags |= MEM_Int; 000262 }else{ 000263 pRec->u.r = rValue; 000264 pRec->flags |= MEM_Real; 000265 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec); 000266 } 000267 } 000268 000269 /* 000270 ** Processing is determine by the affinity parameter: 000271 ** 000272 ** SQLITE_AFF_INTEGER: 000273 ** SQLITE_AFF_REAL: 000274 ** SQLITE_AFF_NUMERIC: 000275 ** Try to convert pRec to an integer representation or a 000276 ** floating-point representation if an integer representation 000277 ** is not possible. Note that the integer representation is 000278 ** always preferred, even if the affinity is REAL, because 000279 ** an integer representation is more space efficient on disk. 000280 ** 000281 ** SQLITE_AFF_TEXT: 000282 ** Convert pRec to a text representation. 000283 ** 000284 ** SQLITE_AFF_BLOB: 000285 ** No-op. pRec is unchanged. 000286 */ 000287 static void applyAffinity( 000288 Mem *pRec, /* The value to apply affinity to */ 000289 char affinity, /* The affinity to be applied */ 000290 u8 enc /* Use this text encoding */ 000291 ){ 000292 if( affinity>=SQLITE_AFF_NUMERIC ){ 000293 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL 000294 || affinity==SQLITE_AFF_NUMERIC ); 000295 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/ 000296 if( (pRec->flags & MEM_Real)==0 ){ 000297 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); 000298 }else{ 000299 sqlite3VdbeIntegerAffinity(pRec); 000300 } 000301 } 000302 }else if( affinity==SQLITE_AFF_TEXT ){ 000303 /* Only attempt the conversion to TEXT if there is an integer or real 000304 ** representation (blob and NULL do not get converted) but no string 000305 ** representation. It would be harmless to repeat the conversion if 000306 ** there is already a string rep, but it is pointless to waste those 000307 ** CPU cycles. */ 000308 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/ 000309 if( (pRec->flags&(MEM_Real|MEM_Int)) ){ 000310 sqlite3VdbeMemStringify(pRec, enc, 1); 000311 } 000312 } 000313 pRec->flags &= ~(MEM_Real|MEM_Int); 000314 } 000315 } 000316 000317 /* 000318 ** Try to convert the type of a function argument or a result column 000319 ** into a numeric representation. Use either INTEGER or REAL whichever 000320 ** is appropriate. But only do the conversion if it is possible without 000321 ** loss of information and return the revised type of the argument. 000322 */ 000323 int sqlite3_value_numeric_type(sqlite3_value *pVal){ 000324 int eType = sqlite3_value_type(pVal); 000325 if( eType==SQLITE_TEXT ){ 000326 Mem *pMem = (Mem*)pVal; 000327 applyNumericAffinity(pMem, 0); 000328 eType = sqlite3_value_type(pVal); 000329 } 000330 return eType; 000331 } 000332 000333 /* 000334 ** Exported version of applyAffinity(). This one works on sqlite3_value*, 000335 ** not the internal Mem* type. 000336 */ 000337 void sqlite3ValueApplyAffinity( 000338 sqlite3_value *pVal, 000339 u8 affinity, 000340 u8 enc 000341 ){ 000342 applyAffinity((Mem *)pVal, affinity, enc); 000343 } 000344 000345 /* 000346 ** pMem currently only holds a string type (or maybe a BLOB that we can 000347 ** interpret as a string if we want to). Compute its corresponding 000348 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields 000349 ** accordingly. 000350 */ 000351 static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ 000352 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 ); 000353 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); 000354 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){ 000355 return 0; 000356 } 000357 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){ 000358 return MEM_Int; 000359 } 000360 return MEM_Real; 000361 } 000362 000363 /* 000364 ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or 000365 ** none. 000366 ** 000367 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. 000368 ** But it does set pMem->u.r and pMem->u.i appropriately. 000369 */ 000370 static u16 numericType(Mem *pMem){ 000371 if( pMem->flags & (MEM_Int|MEM_Real) ){ 000372 return pMem->flags & (MEM_Int|MEM_Real); 000373 } 000374 if( pMem->flags & (MEM_Str|MEM_Blob) ){ 000375 return computeNumericType(pMem); 000376 } 000377 return 0; 000378 } 000379 000380 #ifdef SQLITE_DEBUG 000381 /* 000382 ** Write a nice string representation of the contents of cell pMem 000383 ** into buffer zBuf, length nBuf. 000384 */ 000385 void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ 000386 char *zCsr = zBuf; 000387 int f = pMem->flags; 000388 000389 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; 000390 000391 if( f&MEM_Blob ){ 000392 int i; 000393 char c; 000394 if( f & MEM_Dyn ){ 000395 c = 'z'; 000396 assert( (f & (MEM_Static|MEM_Ephem))==0 ); 000397 }else if( f & MEM_Static ){ 000398 c = 't'; 000399 assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); 000400 }else if( f & MEM_Ephem ){ 000401 c = 'e'; 000402 assert( (f & (MEM_Static|MEM_Dyn))==0 ); 000403 }else{ 000404 c = 's'; 000405 } 000406 000407 sqlite3_snprintf(100, zCsr, "%c", c); 000408 zCsr += sqlite3Strlen30(zCsr); 000409 sqlite3_snprintf(100, zCsr, "%d[", pMem->n); 000410 zCsr += sqlite3Strlen30(zCsr); 000411 for(i=0; i<16 && i<pMem->n; i++){ 000412 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); 000413 zCsr += sqlite3Strlen30(zCsr); 000414 } 000415 for(i=0; i<16 && i<pMem->n; i++){ 000416 char z = pMem->z[i]; 000417 if( z<32 || z>126 ) *zCsr++ = '.'; 000418 else *zCsr++ = z; 000419 } 000420 000421 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]); 000422 zCsr += sqlite3Strlen30(zCsr); 000423 if( f & MEM_Zero ){ 000424 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero); 000425 zCsr += sqlite3Strlen30(zCsr); 000426 } 000427 *zCsr = '\0'; 000428 }else if( f & MEM_Str ){ 000429 int j, k; 000430 zBuf[0] = ' '; 000431 if( f & MEM_Dyn ){ 000432 zBuf[1] = 'z'; 000433 assert( (f & (MEM_Static|MEM_Ephem))==0 ); 000434 }else if( f & MEM_Static ){ 000435 zBuf[1] = 't'; 000436 assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); 000437 }else if( f & MEM_Ephem ){ 000438 zBuf[1] = 'e'; 000439 assert( (f & (MEM_Static|MEM_Dyn))==0 ); 000440 }else{ 000441 zBuf[1] = 's'; 000442 } 000443 k = 2; 000444 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); 000445 k += sqlite3Strlen30(&zBuf[k]); 000446 zBuf[k++] = '['; 000447 for(j=0; j<15 && j<pMem->n; j++){ 000448 u8 c = pMem->z[j]; 000449 if( c>=0x20 && c<0x7f ){ 000450 zBuf[k++] = c; 000451 }else{ 000452 zBuf[k++] = '.'; 000453 } 000454 } 000455 zBuf[k++] = ']'; 000456 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); 000457 k += sqlite3Strlen30(&zBuf[k]); 000458 zBuf[k++] = 0; 000459 } 000460 } 000461 #endif 000462 000463 #ifdef SQLITE_DEBUG 000464 /* 000465 ** Print the value of a register for tracing purposes: 000466 */ 000467 static void memTracePrint(Mem *p){ 000468 if( p->flags & MEM_Undefined ){ 000469 printf(" undefined"); 000470 }else if( p->flags & MEM_Null ){ 000471 printf(" NULL"); 000472 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ 000473 printf(" si:%lld", p->u.i); 000474 }else if( p->flags & MEM_Int ){ 000475 printf(" i:%lld", p->u.i); 000476 #ifndef SQLITE_OMIT_FLOATING_POINT 000477 }else if( p->flags & MEM_Real ){ 000478 printf(" r:%g", p->u.r); 000479 #endif 000480 }else if( p->flags & MEM_RowSet ){ 000481 printf(" (rowset)"); 000482 }else{ 000483 char zBuf[200]; 000484 sqlite3VdbeMemPrettyPrint(p, zBuf); 000485 printf(" %s", zBuf); 000486 } 000487 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype); 000488 } 000489 static void registerTrace(int iReg, Mem *p){ 000490 printf("REG[%d] = ", iReg); 000491 memTracePrint(p); 000492 printf("\n"); 000493 } 000494 #endif 000495 000496 #ifdef SQLITE_DEBUG 000497 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) 000498 #else 000499 # define REGISTER_TRACE(R,M) 000500 #endif 000501 000502 000503 #ifdef VDBE_PROFILE 000504 000505 /* 000506 ** hwtime.h contains inline assembler code for implementing 000507 ** high-performance timing routines. 000508 */ 000509 #include "hwtime.h" 000510 000511 #endif 000512 000513 #ifndef NDEBUG 000514 /* 000515 ** This function is only called from within an assert() expression. It 000516 ** checks that the sqlite3.nTransaction variable is correctly set to 000517 ** the number of non-transaction savepoints currently in the 000518 ** linked list starting at sqlite3.pSavepoint. 000519 ** 000520 ** Usage: 000521 ** 000522 ** assert( checkSavepointCount(db) ); 000523 */ 000524 static int checkSavepointCount(sqlite3 *db){ 000525 int n = 0; 000526 Savepoint *p; 000527 for(p=db->pSavepoint; p; p=p->pNext) n++; 000528 assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); 000529 return 1; 000530 } 000531 #endif 000532 000533 /* 000534 ** Return the register of pOp->p2 after first preparing it to be 000535 ** overwritten with an integer value. 000536 */ 000537 static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){ 000538 sqlite3VdbeMemSetNull(pOut); 000539 pOut->flags = MEM_Int; 000540 return pOut; 000541 } 000542 static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ 000543 Mem *pOut; 000544 assert( pOp->p2>0 ); 000545 assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); 000546 pOut = &p->aMem[pOp->p2]; 000547 memAboutToChange(p, pOut); 000548 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/ 000549 return out2PrereleaseWithClear(pOut); 000550 }else{ 000551 pOut->flags = MEM_Int; 000552 return pOut; 000553 } 000554 } 000555 000556 000557 /* 000558 ** Execute as much of a VDBE program as we can. 000559 ** This is the core of sqlite3_step(). 000560 */ 000561 int sqlite3VdbeExec( 000562 Vdbe *p /* The VDBE */ 000563 ){ 000564 Op *aOp = p->aOp; /* Copy of p->aOp */ 000565 Op *pOp = aOp; /* Current operation */ 000566 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) 000567 Op *pOrigOp; /* Value of pOp at the top of the loop */ 000568 #endif 000569 #ifdef SQLITE_DEBUG 000570 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */ 000571 #endif 000572 int rc = SQLITE_OK; /* Value to return */ 000573 sqlite3 *db = p->db; /* The database */ 000574 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ 000575 u8 encoding = ENC(db); /* The database encoding */ 000576 int iCompare = 0; /* Result of last comparison */ 000577 unsigned nVmStep = 0; /* Number of virtual machine steps */ 000578 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 000579 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */ 000580 #endif 000581 Mem *aMem = p->aMem; /* Copy of p->aMem */ 000582 Mem *pIn1 = 0; /* 1st input operand */ 000583 Mem *pIn2 = 0; /* 2nd input operand */ 000584 Mem *pIn3 = 0; /* 3rd input operand */ 000585 Mem *pOut = 0; /* Output operand */ 000586 int *aPermute = 0; /* Permutation of columns for OP_Compare */ 000587 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */ 000588 #ifdef VDBE_PROFILE 000589 u64 start; /* CPU clock count at start of opcode */ 000590 #endif 000591 /*** INSERT STACK UNION HERE ***/ 000592 000593 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ 000594 sqlite3VdbeEnter(p); 000595 if( p->rc==SQLITE_NOMEM ){ 000596 /* This happens if a malloc() inside a call to sqlite3_column_text() or 000597 ** sqlite3_column_text16() failed. */ 000598 goto no_mem; 000599 } 000600 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); 000601 assert( p->bIsReader || p->readOnly!=0 ); 000602 p->rc = SQLITE_OK; 000603 p->iCurrentTime = 0; 000604 assert( p->explain==0 ); 000605 p->pResultSet = 0; 000606 db->busyHandler.nBusy = 0; 000607 if( db->u1.isInterrupted ) goto abort_due_to_interrupt; 000608 sqlite3VdbeIOTraceSql(p); 000609 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 000610 if( db->xProgress ){ 000611 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; 000612 assert( 0 < db->nProgressOps ); 000613 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); 000614 } 000615 #endif 000616 #ifdef SQLITE_DEBUG 000617 sqlite3BeginBenignMalloc(); 000618 if( p->pc==0 000619 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0 000620 ){ 000621 int i; 000622 int once = 1; 000623 sqlite3VdbePrintSql(p); 000624 if( p->db->flags & SQLITE_VdbeListing ){ 000625 printf("VDBE Program Listing:\n"); 000626 for(i=0; i<p->nOp; i++){ 000627 sqlite3VdbePrintOp(stdout, i, &aOp[i]); 000628 } 000629 } 000630 if( p->db->flags & SQLITE_VdbeEQP ){ 000631 for(i=0; i<p->nOp; i++){ 000632 if( aOp[i].opcode==OP_Explain ){ 000633 if( once ) printf("VDBE Query Plan:\n"); 000634 printf("%s\n", aOp[i].p4.z); 000635 once = 0; 000636 } 000637 } 000638 } 000639 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); 000640 } 000641 sqlite3EndBenignMalloc(); 000642 #endif 000643 for(pOp=&aOp[p->pc]; 1; pOp++){ 000644 /* Errors are detected by individual opcodes, with an immediate 000645 ** jumps to abort_due_to_error. */ 000646 assert( rc==SQLITE_OK ); 000647 000648 assert( pOp>=aOp && pOp<&aOp[p->nOp]); 000649 #ifdef VDBE_PROFILE 000650 start = sqlite3Hwtime(); 000651 #endif 000652 nVmStep++; 000653 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 000654 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++; 000655 #endif 000656 000657 /* Only allow tracing if SQLITE_DEBUG is defined. 000658 */ 000659 #ifdef SQLITE_DEBUG 000660 if( db->flags & SQLITE_VdbeTrace ){ 000661 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp); 000662 } 000663 #endif 000664 000665 000666 /* Check to see if we need to simulate an interrupt. This only happens 000667 ** if we have a special test build. 000668 */ 000669 #ifdef SQLITE_TEST 000670 if( sqlite3_interrupt_count>0 ){ 000671 sqlite3_interrupt_count--; 000672 if( sqlite3_interrupt_count==0 ){ 000673 sqlite3_interrupt(db); 000674 } 000675 } 000676 #endif 000677 000678 /* Sanity checking on other operands */ 000679 #ifdef SQLITE_DEBUG 000680 { 000681 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode]; 000682 if( (opProperty & OPFLG_IN1)!=0 ){ 000683 assert( pOp->p1>0 ); 000684 assert( pOp->p1<=(p->nMem+1 - p->nCursor) ); 000685 assert( memIsValid(&aMem[pOp->p1]) ); 000686 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); 000687 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); 000688 } 000689 if( (opProperty & OPFLG_IN2)!=0 ){ 000690 assert( pOp->p2>0 ); 000691 assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); 000692 assert( memIsValid(&aMem[pOp->p2]) ); 000693 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); 000694 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); 000695 } 000696 if( (opProperty & OPFLG_IN3)!=0 ){ 000697 assert( pOp->p3>0 ); 000698 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 000699 assert( memIsValid(&aMem[pOp->p3]) ); 000700 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); 000701 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); 000702 } 000703 if( (opProperty & OPFLG_OUT2)!=0 ){ 000704 assert( pOp->p2>0 ); 000705 assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); 000706 memAboutToChange(p, &aMem[pOp->p2]); 000707 } 000708 if( (opProperty & OPFLG_OUT3)!=0 ){ 000709 assert( pOp->p3>0 ); 000710 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 000711 memAboutToChange(p, &aMem[pOp->p3]); 000712 } 000713 } 000714 #endif 000715 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) 000716 pOrigOp = pOp; 000717 #endif 000718 000719 switch( pOp->opcode ){ 000720 000721 /***************************************************************************** 000722 ** What follows is a massive switch statement where each case implements a 000723 ** separate instruction in the virtual machine. If we follow the usual 000724 ** indentation conventions, each case should be indented by 6 spaces. But 000725 ** that is a lot of wasted space on the left margin. So the code within 000726 ** the switch statement will break with convention and be flush-left. Another 000727 ** big comment (similar to this one) will mark the point in the code where 000728 ** we transition back to normal indentation. 000729 ** 000730 ** The formatting of each case is important. The makefile for SQLite 000731 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this 000732 ** file looking for lines that begin with "case OP_". The opcodes.h files 000733 ** will be filled with #defines that give unique integer values to each 000734 ** opcode and the opcodes.c file is filled with an array of strings where 000735 ** each string is the symbolic name for the corresponding opcode. If the 000736 ** case statement is followed by a comment of the form "/# same as ... #/" 000737 ** that comment is used to determine the particular value of the opcode. 000738 ** 000739 ** Other keywords in the comment that follows each case are used to 000740 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. 000741 ** Keywords include: in1, in2, in3, out2, out3. See 000742 ** the mkopcodeh.awk script for additional information. 000743 ** 000744 ** Documentation about VDBE opcodes is generated by scanning this file 000745 ** for lines of that contain "Opcode:". That line and all subsequent 000746 ** comment lines are used in the generation of the opcode.html documentation 000747 ** file. 000748 ** 000749 ** SUMMARY: 000750 ** 000751 ** Formatting is important to scripts that scan this file. 000752 ** Do not deviate from the formatting style currently in use. 000753 ** 000754 *****************************************************************************/ 000755 000756 /* Opcode: Goto * P2 * * * 000757 ** 000758 ** An unconditional jump to address P2. 000759 ** The next instruction executed will be 000760 ** the one at index P2 from the beginning of 000761 ** the program. 000762 ** 000763 ** The P1 parameter is not actually used by this opcode. However, it 000764 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell 000765 ** that this Goto is the bottom of a loop and that the lines from P2 down 000766 ** to the current line should be indented for EXPLAIN output. 000767 */ 000768 case OP_Goto: { /* jump */ 000769 jump_to_p2_and_check_for_interrupt: 000770 pOp = &aOp[pOp->p2 - 1]; 000771 000772 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev, 000773 ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon 000774 ** completion. Check to see if sqlite3_interrupt() has been called 000775 ** or if the progress callback needs to be invoked. 000776 ** 000777 ** This code uses unstructured "goto" statements and does not look clean. 000778 ** But that is not due to sloppy coding habits. The code is written this 000779 ** way for performance, to avoid having to run the interrupt and progress 000780 ** checks on every opcode. This helps sqlite3_step() to run about 1.5% 000781 ** faster according to "valgrind --tool=cachegrind" */ 000782 check_for_interrupt: 000783 if( db->u1.isInterrupted ) goto abort_due_to_interrupt; 000784 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 000785 /* Call the progress callback if it is configured and the required number 000786 ** of VDBE ops have been executed (either since this invocation of 000787 ** sqlite3VdbeExec() or since last time the progress callback was called). 000788 ** If the progress callback returns non-zero, exit the virtual machine with 000789 ** a return code SQLITE_ABORT. 000790 */ 000791 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){ 000792 assert( db->nProgressOps!=0 ); 000793 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps); 000794 if( db->xProgress(db->pProgressArg) ){ 000795 rc = SQLITE_INTERRUPT; 000796 goto abort_due_to_error; 000797 } 000798 } 000799 #endif 000800 000801 break; 000802 } 000803 000804 /* Opcode: Gosub P1 P2 * * * 000805 ** 000806 ** Write the current address onto register P1 000807 ** and then jump to address P2. 000808 */ 000809 case OP_Gosub: { /* jump */ 000810 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 000811 pIn1 = &aMem[pOp->p1]; 000812 assert( VdbeMemDynamic(pIn1)==0 ); 000813 memAboutToChange(p, pIn1); 000814 pIn1->flags = MEM_Int; 000815 pIn1->u.i = (int)(pOp-aOp); 000816 REGISTER_TRACE(pOp->p1, pIn1); 000817 000818 /* Most jump operations do a goto to this spot in order to update 000819 ** the pOp pointer. */ 000820 jump_to_p2: 000821 pOp = &aOp[pOp->p2 - 1]; 000822 break; 000823 } 000824 000825 /* Opcode: Return P1 * * * * 000826 ** 000827 ** Jump to the next instruction after the address in register P1. After 000828 ** the jump, register P1 becomes undefined. 000829 */ 000830 case OP_Return: { /* in1 */ 000831 pIn1 = &aMem[pOp->p1]; 000832 assert( pIn1->flags==MEM_Int ); 000833 pOp = &aOp[pIn1->u.i]; 000834 pIn1->flags = MEM_Undefined; 000835 break; 000836 } 000837 000838 /* Opcode: InitCoroutine P1 P2 P3 * * 000839 ** 000840 ** Set up register P1 so that it will Yield to the coroutine 000841 ** located at address P3. 000842 ** 000843 ** If P2!=0 then the coroutine implementation immediately follows 000844 ** this opcode. So jump over the coroutine implementation to 000845 ** address P2. 000846 ** 000847 ** See also: EndCoroutine 000848 */ 000849 case OP_InitCoroutine: { /* jump */ 000850 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 000851 assert( pOp->p2>=0 && pOp->p2<p->nOp ); 000852 assert( pOp->p3>=0 && pOp->p3<p->nOp ); 000853 pOut = &aMem[pOp->p1]; 000854 assert( !VdbeMemDynamic(pOut) ); 000855 pOut->u.i = pOp->p3 - 1; 000856 pOut->flags = MEM_Int; 000857 if( pOp->p2 ) goto jump_to_p2; 000858 break; 000859 } 000860 000861 /* Opcode: EndCoroutine P1 * * * * 000862 ** 000863 ** The instruction at the address in register P1 is a Yield. 000864 ** Jump to the P2 parameter of that Yield. 000865 ** After the jump, register P1 becomes undefined. 000866 ** 000867 ** See also: InitCoroutine 000868 */ 000869 case OP_EndCoroutine: { /* in1 */ 000870 VdbeOp *pCaller; 000871 pIn1 = &aMem[pOp->p1]; 000872 assert( pIn1->flags==MEM_Int ); 000873 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp ); 000874 pCaller = &aOp[pIn1->u.i]; 000875 assert( pCaller->opcode==OP_Yield ); 000876 assert( pCaller->p2>=0 && pCaller->p2<p->nOp ); 000877 pOp = &aOp[pCaller->p2 - 1]; 000878 pIn1->flags = MEM_Undefined; 000879 break; 000880 } 000881 000882 /* Opcode: Yield P1 P2 * * * 000883 ** 000884 ** Swap the program counter with the value in register P1. This 000885 ** has the effect of yielding to a coroutine. 000886 ** 000887 ** If the coroutine that is launched by this instruction ends with 000888 ** Yield or Return then continue to the next instruction. But if 000889 ** the coroutine launched by this instruction ends with 000890 ** EndCoroutine, then jump to P2 rather than continuing with the 000891 ** next instruction. 000892 ** 000893 ** See also: InitCoroutine 000894 */ 000895 case OP_Yield: { /* in1, jump */ 000896 int pcDest; 000897 pIn1 = &aMem[pOp->p1]; 000898 assert( VdbeMemDynamic(pIn1)==0 ); 000899 pIn1->flags = MEM_Int; 000900 pcDest = (int)pIn1->u.i; 000901 pIn1->u.i = (int)(pOp - aOp); 000902 REGISTER_TRACE(pOp->p1, pIn1); 000903 pOp = &aOp[pcDest]; 000904 break; 000905 } 000906 000907 /* Opcode: HaltIfNull P1 P2 P3 P4 P5 000908 ** Synopsis: if r[P3]=null halt 000909 ** 000910 ** Check the value in register P3. If it is NULL then Halt using 000911 ** parameter P1, P2, and P4 as if this were a Halt instruction. If the 000912 ** value in register P3 is not NULL, then this routine is a no-op. 000913 ** The P5 parameter should be 1. 000914 */ 000915 case OP_HaltIfNull: { /* in3 */ 000916 pIn3 = &aMem[pOp->p3]; 000917 if( (pIn3->flags & MEM_Null)==0 ) break; 000918 /* Fall through into OP_Halt */ 000919 } 000920 000921 /* Opcode: Halt P1 P2 * P4 P5 000922 ** 000923 ** Exit immediately. All open cursors, etc are closed 000924 ** automatically. 000925 ** 000926 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), 000927 ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). 000928 ** For errors, it can be some other value. If P1!=0 then P2 will determine 000929 ** whether or not to rollback the current transaction. Do not rollback 000930 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, 000931 ** then back out all changes that have occurred during this execution of the 000932 ** VDBE, but do not rollback the transaction. 000933 ** 000934 ** If P4 is not null then it is an error message string. 000935 ** 000936 ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string. 000937 ** 000938 ** 0: (no change) 000939 ** 1: NOT NULL contraint failed: P4 000940 ** 2: UNIQUE constraint failed: P4 000941 ** 3: CHECK constraint failed: P4 000942 ** 4: FOREIGN KEY constraint failed: P4 000943 ** 000944 ** If P5 is not zero and P4 is NULL, then everything after the ":" is 000945 ** omitted. 000946 ** 000947 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of 000948 ** every program. So a jump past the last instruction of the program 000949 ** is the same as executing Halt. 000950 */ 000951 case OP_Halt: { 000952 VdbeFrame *pFrame; 000953 int pcx; 000954 000955 pcx = (int)(pOp - aOp); 000956 if( pOp->p1==SQLITE_OK && p->pFrame ){ 000957 /* Halt the sub-program. Return control to the parent frame. */ 000958 pFrame = p->pFrame; 000959 p->pFrame = pFrame->pParent; 000960 p->nFrame--; 000961 sqlite3VdbeSetChanges(db, p->nChange); 000962 pcx = sqlite3VdbeFrameRestore(pFrame); 000963 lastRowid = db->lastRowid; 000964 if( pOp->p2==OE_Ignore ){ 000965 /* Instruction pcx is the OP_Program that invoked the sub-program 000966 ** currently being halted. If the p2 instruction of this OP_Halt 000967 ** instruction is set to OE_Ignore, then the sub-program is throwing 000968 ** an IGNORE exception. In this case jump to the address specified 000969 ** as the p2 of the calling OP_Program. */ 000970 pcx = p->aOp[pcx].p2-1; 000971 } 000972 aOp = p->aOp; 000973 aMem = p->aMem; 000974 pOp = &aOp[pcx]; 000975 break; 000976 } 000977 p->rc = pOp->p1; 000978 p->errorAction = (u8)pOp->p2; 000979 p->pc = pcx; 000980 assert( pOp->p5<=4 ); 000981 if( p->rc ){ 000982 if( pOp->p5 ){ 000983 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", 000984 "FOREIGN KEY" }; 000985 testcase( pOp->p5==1 ); 000986 testcase( pOp->p5==2 ); 000987 testcase( pOp->p5==3 ); 000988 testcase( pOp->p5==4 ); 000989 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]); 000990 if( pOp->p4.z ){ 000991 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z); 000992 } 000993 }else{ 000994 sqlite3VdbeError(p, "%s", pOp->p4.z); 000995 } 000996 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg); 000997 } 000998 rc = sqlite3VdbeHalt(p); 000999 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); 001000 if( rc==SQLITE_BUSY ){ 001001 p->rc = SQLITE_BUSY; 001002 }else{ 001003 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); 001004 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); 001005 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; 001006 } 001007 goto vdbe_return; 001008 } 001009 001010 /* Opcode: Integer P1 P2 * * * 001011 ** Synopsis: r[P2]=P1 001012 ** 001013 ** The 32-bit integer value P1 is written into register P2. 001014 */ 001015 case OP_Integer: { /* out2 */ 001016 pOut = out2Prerelease(p, pOp); 001017 pOut->u.i = pOp->p1; 001018 break; 001019 } 001020 001021 /* Opcode: Int64 * P2 * P4 * 001022 ** Synopsis: r[P2]=P4 001023 ** 001024 ** P4 is a pointer to a 64-bit integer value. 001025 ** Write that value into register P2. 001026 */ 001027 case OP_Int64: { /* out2 */ 001028 pOut = out2Prerelease(p, pOp); 001029 assert( pOp->p4.pI64!=0 ); 001030 pOut->u.i = *pOp->p4.pI64; 001031 break; 001032 } 001033 001034 #ifndef SQLITE_OMIT_FLOATING_POINT 001035 /* Opcode: Real * P2 * P4 * 001036 ** Synopsis: r[P2]=P4 001037 ** 001038 ** P4 is a pointer to a 64-bit floating point value. 001039 ** Write that value into register P2. 001040 */ 001041 case OP_Real: { /* same as TK_FLOAT, out2 */ 001042 pOut = out2Prerelease(p, pOp); 001043 pOut->flags = MEM_Real; 001044 assert( !sqlite3IsNaN(*pOp->p4.pReal) ); 001045 pOut->u.r = *pOp->p4.pReal; 001046 break; 001047 } 001048 #endif 001049 001050 /* Opcode: String8 * P2 * P4 * 001051 ** Synopsis: r[P2]='P4' 001052 ** 001053 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 001054 ** into a String opcode before it is executed for the first time. During 001055 ** this transformation, the length of string P4 is computed and stored 001056 ** as the P1 parameter. 001057 */ 001058 case OP_String8: { /* same as TK_STRING, out2 */ 001059 assert( pOp->p4.z!=0 ); 001060 pOut = out2Prerelease(p, pOp); 001061 pOp->opcode = OP_String; 001062 pOp->p1 = sqlite3Strlen30(pOp->p4.z); 001063 001064 #ifndef SQLITE_OMIT_UTF16 001065 if( encoding!=SQLITE_UTF8 ){ 001066 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); 001067 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG ); 001068 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; 001069 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); 001070 assert( VdbeMemDynamic(pOut)==0 ); 001071 pOut->szMalloc = 0; 001072 pOut->flags |= MEM_Static; 001073 if( pOp->p4type==P4_DYNAMIC ){ 001074 sqlite3DbFree(db, pOp->p4.z); 001075 } 001076 pOp->p4type = P4_DYNAMIC; 001077 pOp->p4.z = pOut->z; 001078 pOp->p1 = pOut->n; 001079 } 001080 testcase( rc==SQLITE_TOOBIG ); 001081 #endif 001082 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 001083 goto too_big; 001084 } 001085 assert( rc==SQLITE_OK ); 001086 /* Fall through to the next case, OP_String */ 001087 } 001088 001089 /* Opcode: String P1 P2 P3 P4 P5 001090 ** Synopsis: r[P2]='P4' (len=P1) 001091 ** 001092 ** The string value P4 of length P1 (bytes) is stored in register P2. 001093 ** 001094 ** If P3 is not zero and the content of register P3 is equal to P5, then 001095 ** the datatype of the register P2 is converted to BLOB. The content is 001096 ** the same sequence of bytes, it is merely interpreted as a BLOB instead 001097 ** of a string, as if it had been CAST. In other words: 001098 ** 001099 ** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB) 001100 */ 001101 case OP_String: { /* out2 */ 001102 assert( pOp->p4.z!=0 ); 001103 pOut = out2Prerelease(p, pOp); 001104 pOut->flags = MEM_Str|MEM_Static|MEM_Term; 001105 pOut->z = pOp->p4.z; 001106 pOut->n = pOp->p1; 001107 pOut->enc = encoding; 001108 UPDATE_MAX_BLOBSIZE(pOut); 001109 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS 001110 if( pOp->p3>0 ){ 001111 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 001112 pIn3 = &aMem[pOp->p3]; 001113 assert( pIn3->flags & MEM_Int ); 001114 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term; 001115 } 001116 #endif 001117 break; 001118 } 001119 001120 /* Opcode: Null P1 P2 P3 * * 001121 ** Synopsis: r[P2..P3]=NULL 001122 ** 001123 ** Write a NULL into registers P2. If P3 greater than P2, then also write 001124 ** NULL into register P3 and every register in between P2 and P3. If P3 001125 ** is less than P2 (typically P3 is zero) then only register P2 is 001126 ** set to NULL. 001127 ** 001128 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that 001129 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on 001130 ** OP_Ne or OP_Eq. 001131 */ 001132 case OP_Null: { /* out2 */ 001133 int cnt; 001134 u16 nullFlag; 001135 pOut = out2Prerelease(p, pOp); 001136 cnt = pOp->p3-pOp->p2; 001137 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 001138 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; 001139 pOut->n = 0; 001140 while( cnt>0 ){ 001141 pOut++; 001142 memAboutToChange(p, pOut); 001143 sqlite3VdbeMemSetNull(pOut); 001144 pOut->flags = nullFlag; 001145 pOut->n = 0; 001146 cnt--; 001147 } 001148 break; 001149 } 001150 001151 /* Opcode: SoftNull P1 * * * * 001152 ** Synopsis: r[P1]=NULL 001153 ** 001154 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord 001155 ** instruction, but do not free any string or blob memory associated with 001156 ** the register, so that if the value was a string or blob that was 001157 ** previously copied using OP_SCopy, the copies will continue to be valid. 001158 */ 001159 case OP_SoftNull: { 001160 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 001161 pOut = &aMem[pOp->p1]; 001162 pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined; 001163 break; 001164 } 001165 001166 /* Opcode: Blob P1 P2 * P4 * 001167 ** Synopsis: r[P2]=P4 (len=P1) 001168 ** 001169 ** P4 points to a blob of data P1 bytes long. Store this 001170 ** blob in register P2. 001171 */ 001172 case OP_Blob: { /* out2 */ 001173 assert( pOp->p1 <= SQLITE_MAX_LENGTH ); 001174 pOut = out2Prerelease(p, pOp); 001175 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); 001176 pOut->enc = encoding; 001177 UPDATE_MAX_BLOBSIZE(pOut); 001178 break; 001179 } 001180 001181 /* Opcode: Variable P1 P2 * P4 * 001182 ** Synopsis: r[P2]=parameter(P1,P4) 001183 ** 001184 ** Transfer the values of bound parameter P1 into register P2 001185 ** 001186 ** If the parameter is named, then its name appears in P4. 001187 ** The P4 value is used by sqlite3_bind_parameter_name(). 001188 */ 001189 case OP_Variable: { /* out2 */ 001190 Mem *pVar; /* Value being transferred */ 001191 001192 assert( pOp->p1>0 && pOp->p1<=p->nVar ); 001193 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) ); 001194 pVar = &p->aVar[pOp->p1 - 1]; 001195 if( sqlite3VdbeMemTooBig(pVar) ){ 001196 goto too_big; 001197 } 001198 pOut = out2Prerelease(p, pOp); 001199 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); 001200 UPDATE_MAX_BLOBSIZE(pOut); 001201 break; 001202 } 001203 001204 /* Opcode: Move P1 P2 P3 * * 001205 ** Synopsis: r[P2@P3]=r[P1@P3] 001206 ** 001207 ** Move the P3 values in register P1..P1+P3-1 over into 001208 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are 001209 ** left holding a NULL. It is an error for register ranges 001210 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error 001211 ** for P3 to be less than 1. 001212 */ 001213 case OP_Move: { 001214 int n; /* Number of registers left to copy */ 001215 int p1; /* Register to copy from */ 001216 int p2; /* Register to copy to */ 001217 001218 n = pOp->p3; 001219 p1 = pOp->p1; 001220 p2 = pOp->p2; 001221 assert( n>0 && p1>0 && p2>0 ); 001222 assert( p1+n<=p2 || p2+n<=p1 ); 001223 001224 pIn1 = &aMem[p1]; 001225 pOut = &aMem[p2]; 001226 do{ 001227 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] ); 001228 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] ); 001229 assert( memIsValid(pIn1) ); 001230 memAboutToChange(p, pOut); 001231 sqlite3VdbeMemMove(pOut, pIn1); 001232 #ifdef SQLITE_DEBUG 001233 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){ 001234 pOut->pScopyFrom += pOp->p2 - p1; 001235 } 001236 #endif 001237 Deephemeralize(pOut); 001238 REGISTER_TRACE(p2++, pOut); 001239 pIn1++; 001240 pOut++; 001241 }while( --n ); 001242 break; 001243 } 001244 001245 /* Opcode: Copy P1 P2 P3 * * 001246 ** Synopsis: r[P2@P3+1]=r[P1@P3+1] 001247 ** 001248 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. 001249 ** 001250 ** This instruction makes a deep copy of the value. A duplicate 001251 ** is made of any string or blob constant. See also OP_SCopy. 001252 */ 001253 case OP_Copy: { 001254 int n; 001255 001256 n = pOp->p3; 001257 pIn1 = &aMem[pOp->p1]; 001258 pOut = &aMem[pOp->p2]; 001259 assert( pOut!=pIn1 ); 001260 while( 1 ){ 001261 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); 001262 Deephemeralize(pOut); 001263 #ifdef SQLITE_DEBUG 001264 pOut->pScopyFrom = 0; 001265 #endif 001266 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut); 001267 if( (n--)==0 ) break; 001268 pOut++; 001269 pIn1++; 001270 } 001271 break; 001272 } 001273 001274 /* Opcode: SCopy P1 P2 * * * 001275 ** Synopsis: r[P2]=r[P1] 001276 ** 001277 ** Make a shallow copy of register P1 into register P2. 001278 ** 001279 ** This instruction makes a shallow copy of the value. If the value 001280 ** is a string or blob, then the copy is only a pointer to the 001281 ** original and hence if the original changes so will the copy. 001282 ** Worse, if the original is deallocated, the copy becomes invalid. 001283 ** Thus the program must guarantee that the original will not change 001284 ** during the lifetime of the copy. Use OP_Copy to make a complete 001285 ** copy. 001286 */ 001287 case OP_SCopy: { /* out2 */ 001288 pIn1 = &aMem[pOp->p1]; 001289 pOut = &aMem[pOp->p2]; 001290 assert( pOut!=pIn1 ); 001291 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); 001292 #ifdef SQLITE_DEBUG 001293 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1; 001294 #endif 001295 break; 001296 } 001297 001298 /* Opcode: IntCopy P1 P2 * * * 001299 ** Synopsis: r[P2]=r[P1] 001300 ** 001301 ** Transfer the integer value held in register P1 into register P2. 001302 ** 001303 ** This is an optimized version of SCopy that works only for integer 001304 ** values. 001305 */ 001306 case OP_IntCopy: { /* out2 */ 001307 pIn1 = &aMem[pOp->p1]; 001308 assert( (pIn1->flags & MEM_Int)!=0 ); 001309 pOut = &aMem[pOp->p2]; 001310 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i); 001311 break; 001312 } 001313 001314 /* Opcode: ResultRow P1 P2 * * * 001315 ** Synopsis: output=r[P1@P2] 001316 ** 001317 ** The registers P1 through P1+P2-1 contain a single row of 001318 ** results. This opcode causes the sqlite3_step() call to terminate 001319 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt 001320 ** structure to provide access to the r(P1)..r(P1+P2-1) values as 001321 ** the result row. 001322 */ 001323 case OP_ResultRow: { 001324 Mem *pMem; 001325 int i; 001326 assert( p->nResColumn==pOp->p2 ); 001327 assert( pOp->p1>0 ); 001328 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); 001329 001330 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 001331 /* Run the progress counter just before returning. 001332 */ 001333 if( db->xProgress!=0 001334 && nVmStep>=nProgressLimit 001335 && db->xProgress(db->pProgressArg)!=0 001336 ){ 001337 rc = SQLITE_INTERRUPT; 001338 goto abort_due_to_error; 001339 } 001340 #endif 001341 001342 /* If this statement has violated immediate foreign key constraints, do 001343 ** not return the number of rows modified. And do not RELEASE the statement 001344 ** transaction. It needs to be rolled back. */ 001345 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){ 001346 assert( db->flags&SQLITE_CountRows ); 001347 assert( p->usesStmtJournal ); 001348 goto abort_due_to_error; 001349 } 001350 001351 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then 001352 ** DML statements invoke this opcode to return the number of rows 001353 ** modified to the user. This is the only way that a VM that 001354 ** opens a statement transaction may invoke this opcode. 001355 ** 001356 ** In case this is such a statement, close any statement transaction 001357 ** opened by this VM before returning control to the user. This is to 001358 ** ensure that statement-transactions are always nested, not overlapping. 001359 ** If the open statement-transaction is not closed here, then the user 001360 ** may step another VM that opens its own statement transaction. This 001361 ** may lead to overlapping statement transactions. 001362 ** 001363 ** The statement transaction is never a top-level transaction. Hence 001364 ** the RELEASE call below can never fail. 001365 */ 001366 assert( p->iStatement==0 || db->flags&SQLITE_CountRows ); 001367 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE); 001368 assert( rc==SQLITE_OK ); 001369 001370 /* Invalidate all ephemeral cursor row caches */ 001371 p->cacheCtr = (p->cacheCtr + 2)|1; 001372 001373 /* Make sure the results of the current row are \000 terminated 001374 ** and have an assigned type. The results are de-ephemeralized as 001375 ** a side effect. 001376 */ 001377 pMem = p->pResultSet = &aMem[pOp->p1]; 001378 for(i=0; i<pOp->p2; i++){ 001379 assert( memIsValid(&pMem[i]) ); 001380 Deephemeralize(&pMem[i]); 001381 assert( (pMem[i].flags & MEM_Ephem)==0 001382 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 ); 001383 sqlite3VdbeMemNulTerminate(&pMem[i]); 001384 REGISTER_TRACE(pOp->p1+i, &pMem[i]); 001385 } 001386 if( db->mallocFailed ) goto no_mem; 001387 001388 if( db->mTrace & SQLITE_TRACE_ROW ){ 001389 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0); 001390 } 001391 001392 /* Return SQLITE_ROW 001393 */ 001394 p->pc = (int)(pOp - aOp) + 1; 001395 rc = SQLITE_ROW; 001396 goto vdbe_return; 001397 } 001398 001399 /* Opcode: Concat P1 P2 P3 * * 001400 ** Synopsis: r[P3]=r[P2]+r[P1] 001401 ** 001402 ** Add the text in register P1 onto the end of the text in 001403 ** register P2 and store the result in register P3. 001404 ** If either the P1 or P2 text are NULL then store NULL in P3. 001405 ** 001406 ** P3 = P2 || P1 001407 ** 001408 ** It is illegal for P1 and P3 to be the same register. Sometimes, 001409 ** if P3 is the same register as P2, the implementation is able 001410 ** to avoid a memcpy(). 001411 */ 001412 case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ 001413 i64 nByte; 001414 001415 pIn1 = &aMem[pOp->p1]; 001416 pIn2 = &aMem[pOp->p2]; 001417 pOut = &aMem[pOp->p3]; 001418 assert( pIn1!=pOut ); 001419 if( (pIn1->flags | pIn2->flags) & MEM_Null ){ 001420 sqlite3VdbeMemSetNull(pOut); 001421 break; 001422 } 001423 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem; 001424 Stringify(pIn1, encoding); 001425 Stringify(pIn2, encoding); 001426 nByte = pIn1->n + pIn2->n; 001427 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 001428 goto too_big; 001429 } 001430 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ 001431 goto no_mem; 001432 } 001433 MemSetTypeFlag(pOut, MEM_Str); 001434 if( pOut!=pIn2 ){ 001435 memcpy(pOut->z, pIn2->z, pIn2->n); 001436 } 001437 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); 001438 pOut->z[nByte]=0; 001439 pOut->z[nByte+1] = 0; 001440 pOut->flags |= MEM_Term; 001441 pOut->n = (int)nByte; 001442 pOut->enc = encoding; 001443 UPDATE_MAX_BLOBSIZE(pOut); 001444 break; 001445 } 001446 001447 /* Opcode: Add P1 P2 P3 * * 001448 ** Synopsis: r[P3]=r[P1]+r[P2] 001449 ** 001450 ** Add the value in register P1 to the value in register P2 001451 ** and store the result in register P3. 001452 ** If either input is NULL, the result is NULL. 001453 */ 001454 /* Opcode: Multiply P1 P2 P3 * * 001455 ** Synopsis: r[P3]=r[P1]*r[P2] 001456 ** 001457 ** 001458 ** Multiply the value in register P1 by the value in register P2 001459 ** and store the result in register P3. 001460 ** If either input is NULL, the result is NULL. 001461 */ 001462 /* Opcode: Subtract P1 P2 P3 * * 001463 ** Synopsis: r[P3]=r[P2]-r[P1] 001464 ** 001465 ** Subtract the value in register P1 from the value in register P2 001466 ** and store the result in register P3. 001467 ** If either input is NULL, the result is NULL. 001468 */ 001469 /* Opcode: Divide P1 P2 P3 * * 001470 ** Synopsis: r[P3]=r[P2]/r[P1] 001471 ** 001472 ** Divide the value in register P1 by the value in register P2 001473 ** and store the result in register P3 (P3=P2/P1). If the value in 001474 ** register P1 is zero, then the result is NULL. If either input is 001475 ** NULL, the result is NULL. 001476 */ 001477 /* Opcode: Remainder P1 P2 P3 * * 001478 ** Synopsis: r[P3]=r[P2]%r[P1] 001479 ** 001480 ** Compute the remainder after integer register P2 is divided by 001481 ** register P1 and store the result in register P3. 001482 ** If the value in register P1 is zero the result is NULL. 001483 ** If either operand is NULL, the result is NULL. 001484 */ 001485 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ 001486 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ 001487 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ 001488 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ 001489 case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ 001490 char bIntint; /* Started out as two integer operands */ 001491 u16 flags; /* Combined MEM_* flags from both inputs */ 001492 u16 type1; /* Numeric type of left operand */ 001493 u16 type2; /* Numeric type of right operand */ 001494 i64 iA; /* Integer value of left operand */ 001495 i64 iB; /* Integer value of right operand */ 001496 double rA; /* Real value of left operand */ 001497 double rB; /* Real value of right operand */ 001498 001499 pIn1 = &aMem[pOp->p1]; 001500 type1 = numericType(pIn1); 001501 pIn2 = &aMem[pOp->p2]; 001502 type2 = numericType(pIn2); 001503 pOut = &aMem[pOp->p3]; 001504 flags = pIn1->flags | pIn2->flags; 001505 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null; 001506 if( (type1 & type2 & MEM_Int)!=0 ){ 001507 iA = pIn1->u.i; 001508 iB = pIn2->u.i; 001509 bIntint = 1; 001510 switch( pOp->opcode ){ 001511 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; 001512 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; 001513 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break; 001514 case OP_Divide: { 001515 if( iA==0 ) goto arithmetic_result_is_null; 001516 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math; 001517 iB /= iA; 001518 break; 001519 } 001520 default: { 001521 if( iA==0 ) goto arithmetic_result_is_null; 001522 if( iA==-1 ) iA = 1; 001523 iB %= iA; 001524 break; 001525 } 001526 } 001527 pOut->u.i = iB; 001528 MemSetTypeFlag(pOut, MEM_Int); 001529 }else{ 001530 bIntint = 0; 001531 fp_math: 001532 rA = sqlite3VdbeRealValue(pIn1); 001533 rB = sqlite3VdbeRealValue(pIn2); 001534 switch( pOp->opcode ){ 001535 case OP_Add: rB += rA; break; 001536 case OP_Subtract: rB -= rA; break; 001537 case OP_Multiply: rB *= rA; break; 001538 case OP_Divide: { 001539 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ 001540 if( rA==(double)0 ) goto arithmetic_result_is_null; 001541 rB /= rA; 001542 break; 001543 } 001544 default: { 001545 iA = (i64)rA; 001546 iB = (i64)rB; 001547 if( iA==0 ) goto arithmetic_result_is_null; 001548 if( iA==-1 ) iA = 1; 001549 rB = (double)(iB % iA); 001550 break; 001551 } 001552 } 001553 #ifdef SQLITE_OMIT_FLOATING_POINT 001554 pOut->u.i = rB; 001555 MemSetTypeFlag(pOut, MEM_Int); 001556 #else 001557 if( sqlite3IsNaN(rB) ){ 001558 goto arithmetic_result_is_null; 001559 } 001560 pOut->u.r = rB; 001561 MemSetTypeFlag(pOut, MEM_Real); 001562 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){ 001563 sqlite3VdbeIntegerAffinity(pOut); 001564 } 001565 #endif 001566 } 001567 break; 001568 001569 arithmetic_result_is_null: 001570 sqlite3VdbeMemSetNull(pOut); 001571 break; 001572 } 001573 001574 /* Opcode: CollSeq P1 * * P4 001575 ** 001576 ** P4 is a pointer to a CollSeq struct. If the next call to a user function 001577 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will 001578 ** be returned. This is used by the built-in min(), max() and nullif() 001579 ** functions. 001580 ** 001581 ** If P1 is not zero, then it is a register that a subsequent min() or 001582 ** max() aggregate will set to 1 if the current row is not the minimum or 001583 ** maximum. The P1 register is initialized to 0 by this instruction. 001584 ** 001585 ** The interface used by the implementation of the aforementioned functions 001586 ** to retrieve the collation sequence set by this opcode is not available 001587 ** publicly. Only built-in functions have access to this feature. 001588 */ 001589 case OP_CollSeq: { 001590 assert( pOp->p4type==P4_COLLSEQ ); 001591 if( pOp->p1 ){ 001592 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); 001593 } 001594 break; 001595 } 001596 001597 /* Opcode: Function0 P1 P2 P3 P4 P5 001598 ** Synopsis: r[P3]=func(r[P2@P5]) 001599 ** 001600 ** Invoke a user function (P4 is a pointer to a FuncDef object that 001601 ** defines the function) with P5 arguments taken from register P2 and 001602 ** successors. The result of the function is stored in register P3. 001603 ** Register P3 must not be one of the function inputs. 001604 ** 001605 ** P1 is a 32-bit bitmask indicating whether or not each argument to the 001606 ** function was determined to be constant at compile time. If the first 001607 ** argument was constant then bit 0 of P1 is set. This is used to determine 001608 ** whether meta data associated with a user function argument using the 001609 ** sqlite3_set_auxdata() API may be safely retained until the next 001610 ** invocation of this opcode. 001611 ** 001612 ** See also: Function, AggStep, AggFinal 001613 */ 001614 /* Opcode: Function P1 P2 P3 P4 P5 001615 ** Synopsis: r[P3]=func(r[P2@P5]) 001616 ** 001617 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that 001618 ** contains a pointer to the function to be run) with P5 arguments taken 001619 ** from register P2 and successors. The result of the function is stored 001620 ** in register P3. Register P3 must not be one of the function inputs. 001621 ** 001622 ** P1 is a 32-bit bitmask indicating whether or not each argument to the 001623 ** function was determined to be constant at compile time. If the first 001624 ** argument was constant then bit 0 of P1 is set. This is used to determine 001625 ** whether meta data associated with a user function argument using the 001626 ** sqlite3_set_auxdata() API may be safely retained until the next 001627 ** invocation of this opcode. 001628 ** 001629 ** SQL functions are initially coded as OP_Function0 with P4 pointing 001630 ** to a FuncDef object. But on first evaluation, the P4 operand is 001631 ** automatically converted into an sqlite3_context object and the operation 001632 ** changed to this OP_Function opcode. In this way, the initialization of 001633 ** the sqlite3_context object occurs only once, rather than once for each 001634 ** evaluation of the function. 001635 ** 001636 ** See also: Function0, AggStep, AggFinal 001637 */ 001638 case OP_Function0: { 001639 int n; 001640 sqlite3_context *pCtx; 001641 001642 assert( pOp->p4type==P4_FUNCDEF ); 001643 n = pOp->p5; 001644 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 001645 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) ); 001646 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); 001647 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); 001648 if( pCtx==0 ) goto no_mem; 001649 pCtx->pOut = 0; 001650 pCtx->pFunc = pOp->p4.pFunc; 001651 pCtx->iOp = (int)(pOp - aOp); 001652 pCtx->pVdbe = p; 001653 pCtx->argc = n; 001654 pOp->p4type = P4_FUNCCTX; 001655 pOp->p4.pCtx = pCtx; 001656 pOp->opcode = OP_Function; 001657 /* Fall through into OP_Function */ 001658 } 001659 case OP_Function: { 001660 int i; 001661 sqlite3_context *pCtx; 001662 001663 assert( pOp->p4type==P4_FUNCCTX ); 001664 pCtx = pOp->p4.pCtx; 001665 001666 /* If this function is inside of a trigger, the register array in aMem[] 001667 ** might change from one evaluation to the next. The next block of code 001668 ** checks to see if the register array has changed, and if so it 001669 ** reinitializes the relavant parts of the sqlite3_context object */ 001670 pOut = &aMem[pOp->p3]; 001671 if( pCtx->pOut != pOut ){ 001672 pCtx->pOut = pOut; 001673 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; 001674 } 001675 001676 memAboutToChange(p, pCtx->pOut); 001677 #ifdef SQLITE_DEBUG 001678 for(i=0; i<pCtx->argc; i++){ 001679 assert( memIsValid(pCtx->argv[i]) ); 001680 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); 001681 } 001682 #endif 001683 MemSetTypeFlag(pCtx->pOut, MEM_Null); 001684 pCtx->fErrorOrAux = 0; 001685 db->lastRowid = lastRowid; 001686 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ 001687 lastRowid = db->lastRowid; /* Remember rowid changes made by xSFunc */ 001688 001689 /* If the function returned an error, throw an exception */ 001690 if( pCtx->fErrorOrAux ){ 001691 if( pCtx->isError ){ 001692 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); 001693 rc = pCtx->isError; 001694 } 001695 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1); 001696 if( rc ) goto abort_due_to_error; 001697 } 001698 001699 /* Copy the result of the function into register P3 */ 001700 if( pOut->flags & (MEM_Str|MEM_Blob) ){ 001701 sqlite3VdbeChangeEncoding(pCtx->pOut, encoding); 001702 if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big; 001703 } 001704 001705 REGISTER_TRACE(pOp->p3, pCtx->pOut); 001706 UPDATE_MAX_BLOBSIZE(pCtx->pOut); 001707 break; 001708 } 001709 001710 /* Opcode: BitAnd P1 P2 P3 * * 001711 ** Synopsis: r[P3]=r[P1]&r[P2] 001712 ** 001713 ** Take the bit-wise AND of the values in register P1 and P2 and 001714 ** store the result in register P3. 001715 ** If either input is NULL, the result is NULL. 001716 */ 001717 /* Opcode: BitOr P1 P2 P3 * * 001718 ** Synopsis: r[P3]=r[P1]|r[P2] 001719 ** 001720 ** Take the bit-wise OR of the values in register P1 and P2 and 001721 ** store the result in register P3. 001722 ** If either input is NULL, the result is NULL. 001723 */ 001724 /* Opcode: ShiftLeft P1 P2 P3 * * 001725 ** Synopsis: r[P3]=r[P2]<<r[P1] 001726 ** 001727 ** Shift the integer value in register P2 to the left by the 001728 ** number of bits specified by the integer in register P1. 001729 ** Store the result in register P3. 001730 ** If either input is NULL, the result is NULL. 001731 */ 001732 /* Opcode: ShiftRight P1 P2 P3 * * 001733 ** Synopsis: r[P3]=r[P2]>>r[P1] 001734 ** 001735 ** Shift the integer value in register P2 to the right by the 001736 ** number of bits specified by the integer in register P1. 001737 ** Store the result in register P3. 001738 ** If either input is NULL, the result is NULL. 001739 */ 001740 case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ 001741 case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ 001742 case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ 001743 case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ 001744 i64 iA; 001745 u64 uA; 001746 i64 iB; 001747 u8 op; 001748 001749 pIn1 = &aMem[pOp->p1]; 001750 pIn2 = &aMem[pOp->p2]; 001751 pOut = &aMem[pOp->p3]; 001752 if( (pIn1->flags | pIn2->flags) & MEM_Null ){ 001753 sqlite3VdbeMemSetNull(pOut); 001754 break; 001755 } 001756 iA = sqlite3VdbeIntValue(pIn2); 001757 iB = sqlite3VdbeIntValue(pIn1); 001758 op = pOp->opcode; 001759 if( op==OP_BitAnd ){ 001760 iA &= iB; 001761 }else if( op==OP_BitOr ){ 001762 iA |= iB; 001763 }else if( iB!=0 ){ 001764 assert( op==OP_ShiftRight || op==OP_ShiftLeft ); 001765 001766 /* If shifting by a negative amount, shift in the other direction */ 001767 if( iB<0 ){ 001768 assert( OP_ShiftRight==OP_ShiftLeft+1 ); 001769 op = 2*OP_ShiftLeft + 1 - op; 001770 iB = iB>(-64) ? -iB : 64; 001771 } 001772 001773 if( iB>=64 ){ 001774 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1; 001775 }else{ 001776 memcpy(&uA, &iA, sizeof(uA)); 001777 if( op==OP_ShiftLeft ){ 001778 uA <<= iB; 001779 }else{ 001780 uA >>= iB; 001781 /* Sign-extend on a right shift of a negative number */ 001782 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB); 001783 } 001784 memcpy(&iA, &uA, sizeof(iA)); 001785 } 001786 } 001787 pOut->u.i = iA; 001788 MemSetTypeFlag(pOut, MEM_Int); 001789 break; 001790 } 001791 001792 /* Opcode: AddImm P1 P2 * * * 001793 ** Synopsis: r[P1]=r[P1]+P2 001794 ** 001795 ** Add the constant P2 to the value in register P1. 001796 ** The result is always an integer. 001797 ** 001798 ** To force any register to be an integer, just add 0. 001799 */ 001800 case OP_AddImm: { /* in1 */ 001801 pIn1 = &aMem[pOp->p1]; 001802 memAboutToChange(p, pIn1); 001803 sqlite3VdbeMemIntegerify(pIn1); 001804 pIn1->u.i += pOp->p2; 001805 break; 001806 } 001807 001808 /* Opcode: MustBeInt P1 P2 * * * 001809 ** 001810 ** Force the value in register P1 to be an integer. If the value 001811 ** in P1 is not an integer and cannot be converted into an integer 001812 ** without data loss, then jump immediately to P2, or if P2==0 001813 ** raise an SQLITE_MISMATCH exception. 001814 */ 001815 case OP_MustBeInt: { /* jump, in1 */ 001816 pIn1 = &aMem[pOp->p1]; 001817 if( (pIn1->flags & MEM_Int)==0 ){ 001818 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); 001819 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2); 001820 if( (pIn1->flags & MEM_Int)==0 ){ 001821 if( pOp->p2==0 ){ 001822 rc = SQLITE_MISMATCH; 001823 goto abort_due_to_error; 001824 }else{ 001825 goto jump_to_p2; 001826 } 001827 } 001828 } 001829 MemSetTypeFlag(pIn1, MEM_Int); 001830 break; 001831 } 001832 001833 #ifndef SQLITE_OMIT_FLOATING_POINT 001834 /* Opcode: RealAffinity P1 * * * * 001835 ** 001836 ** If register P1 holds an integer convert it to a real value. 001837 ** 001838 ** This opcode is used when extracting information from a column that 001839 ** has REAL affinity. Such column values may still be stored as 001840 ** integers, for space efficiency, but after extraction we want them 001841 ** to have only a real value. 001842 */ 001843 case OP_RealAffinity: { /* in1 */ 001844 pIn1 = &aMem[pOp->p1]; 001845 if( pIn1->flags & MEM_Int ){ 001846 sqlite3VdbeMemRealify(pIn1); 001847 } 001848 break; 001849 } 001850 #endif 001851 001852 #ifndef SQLITE_OMIT_CAST 001853 /* Opcode: Cast P1 P2 * * * 001854 ** Synopsis: affinity(r[P1]) 001855 ** 001856 ** Force the value in register P1 to be the type defined by P2. 001857 ** 001858 ** <ul> 001859 ** <li value="97"> TEXT 001860 ** <li value="98"> BLOB 001861 ** <li value="99"> NUMERIC 001862 ** <li value="100"> INTEGER 001863 ** <li value="101"> REAL 001864 ** </ul> 001865 ** 001866 ** A NULL value is not changed by this routine. It remains NULL. 001867 */ 001868 case OP_Cast: { /* in1 */ 001869 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL ); 001870 testcase( pOp->p2==SQLITE_AFF_TEXT ); 001871 testcase( pOp->p2==SQLITE_AFF_BLOB ); 001872 testcase( pOp->p2==SQLITE_AFF_NUMERIC ); 001873 testcase( pOp->p2==SQLITE_AFF_INTEGER ); 001874 testcase( pOp->p2==SQLITE_AFF_REAL ); 001875 pIn1 = &aMem[pOp->p1]; 001876 memAboutToChange(p, pIn1); 001877 rc = ExpandBlob(pIn1); 001878 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); 001879 UPDATE_MAX_BLOBSIZE(pIn1); 001880 if( rc ) goto abort_due_to_error; 001881 break; 001882 } 001883 #endif /* SQLITE_OMIT_CAST */ 001884 001885 /* Opcode: Eq P1 P2 P3 P4 P5 001886 ** Synopsis: IF r[P3]==r[P1] 001887 ** 001888 ** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then 001889 ** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then 001890 ** store the result of comparison in register P2. 001891 ** 001892 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - 001893 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 001894 ** to coerce both inputs according to this affinity before the 001895 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric 001896 ** affinity is used. Note that the affinity conversions are stored 001897 ** back into the input registers P1 and P3. So this opcode can cause 001898 ** persistent changes to registers P1 and P3. 001899 ** 001900 ** Once any conversions have taken place, and neither value is NULL, 001901 ** the values are compared. If both values are blobs then memcmp() is 001902 ** used to determine the results of the comparison. If both values 001903 ** are text, then the appropriate collating function specified in 001904 ** P4 is used to do the comparison. If P4 is not specified then 001905 ** memcmp() is used to compare text string. If both values are 001906 ** numeric, then a numeric comparison is used. If the two values 001907 ** are of different types, then numbers are considered less than 001908 ** strings and strings are considered less than blobs. 001909 ** 001910 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either 001911 ** true or false and is never NULL. If both operands are NULL then the result 001912 ** of comparison is true. If either operand is NULL then the result is false. 001913 ** If neither operand is NULL the result is the same as it would be if 001914 ** the SQLITE_NULLEQ flag were omitted from P5. 001915 ** 001916 ** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the 001917 ** content of r[P2] is only changed if the new value is NULL or 0 (false). 001918 ** In other words, a prior r[P2] value will not be overwritten by 1 (true). 001919 */ 001920 /* Opcode: Ne P1 P2 P3 P4 P5 001921 ** Synopsis: IF r[P3]!=r[P1] 001922 ** 001923 ** This works just like the Eq opcode except that the jump is taken if 001924 ** the operands in registers P1 and P3 are not equal. See the Eq opcode for 001925 ** additional information. 001926 ** 001927 ** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the 001928 ** content of r[P2] is only changed if the new value is NULL or 1 (true). 001929 ** In other words, a prior r[P2] value will not be overwritten by 0 (false). 001930 */ 001931 /* Opcode: Lt P1 P2 P3 P4 P5 001932 ** Synopsis: IF r[P3]<r[P1] 001933 ** 001934 ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then 001935 ** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store 001936 ** the result of comparison (0 or 1 or NULL) into register P2. 001937 ** 001938 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or 001939 ** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL 001940 ** bit is clear then fall through if either operand is NULL. 001941 ** 001942 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - 001943 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 001944 ** to coerce both inputs according to this affinity before the 001945 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric 001946 ** affinity is used. Note that the affinity conversions are stored 001947 ** back into the input registers P1 and P3. So this opcode can cause 001948 ** persistent changes to registers P1 and P3. 001949 ** 001950 ** Once any conversions have taken place, and neither value is NULL, 001951 ** the values are compared. If both values are blobs then memcmp() is 001952 ** used to determine the results of the comparison. If both values 001953 ** are text, then the appropriate collating function specified in 001954 ** P4 is used to do the comparison. If P4 is not specified then 001955 ** memcmp() is used to compare text string. If both values are 001956 ** numeric, then a numeric comparison is used. If the two values 001957 ** are of different types, then numbers are considered less than 001958 ** strings and strings are considered less than blobs. 001959 */ 001960 /* Opcode: Le P1 P2 P3 P4 P5 001961 ** Synopsis: IF r[P3]<=r[P1] 001962 ** 001963 ** This works just like the Lt opcode except that the jump is taken if 001964 ** the content of register P3 is less than or equal to the content of 001965 ** register P1. See the Lt opcode for additional information. 001966 */ 001967 /* Opcode: Gt P1 P2 P3 P4 P5 001968 ** Synopsis: IF r[P3]>r[P1] 001969 ** 001970 ** This works just like the Lt opcode except that the jump is taken if 001971 ** the content of register P3 is greater than the content of 001972 ** register P1. See the Lt opcode for additional information. 001973 */ 001974 /* Opcode: Ge P1 P2 P3 P4 P5 001975 ** Synopsis: IF r[P3]>=r[P1] 001976 ** 001977 ** This works just like the Lt opcode except that the jump is taken if 001978 ** the content of register P3 is greater than or equal to the content of 001979 ** register P1. See the Lt opcode for additional information. 001980 */ 001981 case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ 001982 case OP_Ne: /* same as TK_NE, jump, in1, in3 */ 001983 case OP_Lt: /* same as TK_LT, jump, in1, in3 */ 001984 case OP_Le: /* same as TK_LE, jump, in1, in3 */ 001985 case OP_Gt: /* same as TK_GT, jump, in1, in3 */ 001986 case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ 001987 int res, res2; /* Result of the comparison of pIn1 against pIn3 */ 001988 char affinity; /* Affinity to use for comparison */ 001989 u16 flags1; /* Copy of initial value of pIn1->flags */ 001990 u16 flags3; /* Copy of initial value of pIn3->flags */ 001991 001992 pIn1 = &aMem[pOp->p1]; 001993 pIn3 = &aMem[pOp->p3]; 001994 flags1 = pIn1->flags; 001995 flags3 = pIn3->flags; 001996 if( (flags1 | flags3)&MEM_Null ){ 001997 /* One or both operands are NULL */ 001998 if( pOp->p5 & SQLITE_NULLEQ ){ 001999 /* If SQLITE_NULLEQ is set (which will only happen if the operator is 002000 ** OP_Eq or OP_Ne) then take the jump or not depending on whether 002001 ** or not both operands are null. 002002 */ 002003 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne ); 002004 assert( (flags1 & MEM_Cleared)==0 ); 002005 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 ); 002006 if( (flags1&flags3&MEM_Null)!=0 002007 && (flags3&MEM_Cleared)==0 002008 ){ 002009 res = 0; /* Operands are equal */ 002010 }else{ 002011 res = 1; /* Operands are not equal */ 002012 } 002013 }else{ 002014 /* SQLITE_NULLEQ is clear and at least one operand is NULL, 002015 ** then the result is always NULL. 002016 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. 002017 */ 002018 if( pOp->p5 & SQLITE_STOREP2 ){ 002019 pOut = &aMem[pOp->p2]; 002020 iCompare = 1; /* Operands are not equal */ 002021 memAboutToChange(p, pOut); 002022 MemSetTypeFlag(pOut, MEM_Null); 002023 REGISTER_TRACE(pOp->p2, pOut); 002024 }else{ 002025 VdbeBranchTaken(2,3); 002026 if( pOp->p5 & SQLITE_JUMPIFNULL ){ 002027 goto jump_to_p2; 002028 } 002029 } 002030 break; 002031 } 002032 }else{ 002033 /* Neither operand is NULL. Do a comparison. */ 002034 affinity = pOp->p5 & SQLITE_AFF_MASK; 002035 if( affinity>=SQLITE_AFF_NUMERIC ){ 002036 if( (flags1 | flags3)&MEM_Str ){ 002037 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ 002038 applyNumericAffinity(pIn1,0); 002039 testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */ 002040 flags3 = pIn3->flags; 002041 } 002042 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ 002043 applyNumericAffinity(pIn3,0); 002044 } 002045 } 002046 /* Handle the common case of integer comparison here, as an 002047 ** optimization, to avoid a call to sqlite3MemCompare() */ 002048 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){ 002049 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; } 002050 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; } 002051 res = 0; 002052 goto compare_op; 002053 } 002054 }else if( affinity==SQLITE_AFF_TEXT ){ 002055 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){ 002056 testcase( pIn1->flags & MEM_Int ); 002057 testcase( pIn1->flags & MEM_Real ); 002058 sqlite3VdbeMemStringify(pIn1, encoding, 1); 002059 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); 002060 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); 002061 assert( pIn1!=pIn3 ); 002062 } 002063 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){ 002064 testcase( pIn3->flags & MEM_Int ); 002065 testcase( pIn3->flags & MEM_Real ); 002066 sqlite3VdbeMemStringify(pIn3, encoding, 1); 002067 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); 002068 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); 002069 } 002070 } 002071 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); 002072 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); 002073 } 002074 compare_op: 002075 switch( pOp->opcode ){ 002076 case OP_Eq: res2 = res==0; break; 002077 case OP_Ne: res2 = res; break; 002078 case OP_Lt: res2 = res<0; break; 002079 case OP_Le: res2 = res<=0; break; 002080 case OP_Gt: res2 = res>0; break; 002081 default: res2 = res>=0; break; 002082 } 002083 002084 /* Undo any changes made by applyAffinity() to the input registers. */ 002085 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); 002086 pIn1->flags = flags1; 002087 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); 002088 pIn3->flags = flags3; 002089 002090 if( pOp->p5 & SQLITE_STOREP2 ){ 002091 pOut = &aMem[pOp->p2]; 002092 iCompare = res; 002093 res2 = res2!=0; /* For this path res2 must be exactly 0 or 1 */ 002094 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){ 002095 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1 002096 ** and prevents OP_Ne from overwriting NULL with 0. This flag 002097 ** is only used in contexts where either: 002098 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0) 002099 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1) 002100 ** Therefore it is not necessary to check the content of r[P2] for 002101 ** NULL. */ 002102 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq ); 002103 assert( res2==0 || res2==1 ); 002104 testcase( res2==0 && pOp->opcode==OP_Eq ); 002105 testcase( res2==1 && pOp->opcode==OP_Eq ); 002106 testcase( res2==0 && pOp->opcode==OP_Ne ); 002107 testcase( res2==1 && pOp->opcode==OP_Ne ); 002108 if( (pOp->opcode==OP_Eq)==res2 ) break; 002109 } 002110 memAboutToChange(p, pOut); 002111 MemSetTypeFlag(pOut, MEM_Int); 002112 pOut->u.i = res2; 002113 REGISTER_TRACE(pOp->p2, pOut); 002114 }else{ 002115 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); 002116 if( res2 ){ 002117 goto jump_to_p2; 002118 } 002119 } 002120 break; 002121 } 002122 002123 /* Opcode: ElseNotEq * P2 * * * 002124 ** 002125 ** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator. 002126 ** If result of an OP_Eq comparison on the same two operands 002127 ** would have be NULL or false (0), then then jump to P2. 002128 ** If the result of an OP_Eq comparison on the two previous operands 002129 ** would have been true (1), then fall through. 002130 */ 002131 case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */ 002132 assert( pOp>aOp ); 002133 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt ); 002134 assert( pOp[-1].p5 & SQLITE_STOREP2 ); 002135 VdbeBranchTaken(iCompare!=0, 2); 002136 if( iCompare!=0 ) goto jump_to_p2; 002137 break; 002138 } 002139 002140 002141 /* Opcode: Permutation * * * P4 * 002142 ** 002143 ** Set the permutation used by the OP_Compare operator to be the array 002144 ** of integers in P4. 002145 ** 002146 ** The permutation is only valid until the next OP_Compare that has 002147 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should 002148 ** occur immediately prior to the OP_Compare. 002149 ** 002150 ** The first integer in the P4 integer array is the length of the array 002151 ** and does not become part of the permutation. 002152 */ 002153 case OP_Permutation: { 002154 assert( pOp->p4type==P4_INTARRAY ); 002155 assert( pOp->p4.ai ); 002156 aPermute = pOp->p4.ai + 1; 002157 break; 002158 } 002159 002160 /* Opcode: Compare P1 P2 P3 P4 P5 002161 ** Synopsis: r[P1@P3] <-> r[P2@P3] 002162 ** 002163 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this 002164 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of 002165 ** the comparison for use by the next OP_Jump instruct. 002166 ** 002167 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is 002168 ** determined by the most recent OP_Permutation operator. If the 002169 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential 002170 ** order. 002171 ** 002172 ** P4 is a KeyInfo structure that defines collating sequences and sort 002173 ** orders for the comparison. The permutation applies to registers 002174 ** only. The KeyInfo elements are used sequentially. 002175 ** 002176 ** The comparison is a sort comparison, so NULLs compare equal, 002177 ** NULLs are less than numbers, numbers are less than strings, 002178 ** and strings are less than blobs. 002179 */ 002180 case OP_Compare: { 002181 int n; 002182 int i; 002183 int p1; 002184 int p2; 002185 const KeyInfo *pKeyInfo; 002186 int idx; 002187 CollSeq *pColl; /* Collating sequence to use on this term */ 002188 int bRev; /* True for DESCENDING sort order */ 002189 002190 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0; 002191 n = pOp->p3; 002192 pKeyInfo = pOp->p4.pKeyInfo; 002193 assert( n>0 ); 002194 assert( pKeyInfo!=0 ); 002195 p1 = pOp->p1; 002196 p2 = pOp->p2; 002197 #if SQLITE_DEBUG 002198 if( aPermute ){ 002199 int k, mx = 0; 002200 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k]; 002201 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 ); 002202 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 ); 002203 }else{ 002204 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 ); 002205 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 ); 002206 } 002207 #endif /* SQLITE_DEBUG */ 002208 for(i=0; i<n; i++){ 002209 idx = aPermute ? aPermute[i] : i; 002210 assert( memIsValid(&aMem[p1+idx]) ); 002211 assert( memIsValid(&aMem[p2+idx]) ); 002212 REGISTER_TRACE(p1+idx, &aMem[p1+idx]); 002213 REGISTER_TRACE(p2+idx, &aMem[p2+idx]); 002214 assert( i<pKeyInfo->nField ); 002215 pColl = pKeyInfo->aColl[i]; 002216 bRev = pKeyInfo->aSortOrder[i]; 002217 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); 002218 if( iCompare ){ 002219 if( bRev ) iCompare = -iCompare; 002220 break; 002221 } 002222 } 002223 aPermute = 0; 002224 break; 002225 } 002226 002227 /* Opcode: Jump P1 P2 P3 * * 002228 ** 002229 ** Jump to the instruction at address P1, P2, or P3 depending on whether 002230 ** in the most recent OP_Compare instruction the P1 vector was less than 002231 ** equal to, or greater than the P2 vector, respectively. 002232 */ 002233 case OP_Jump: { /* jump */ 002234 if( iCompare<0 ){ 002235 VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1]; 002236 }else if( iCompare==0 ){ 002237 VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1]; 002238 }else{ 002239 VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1]; 002240 } 002241 break; 002242 } 002243 002244 /* Opcode: And P1 P2 P3 * * 002245 ** Synopsis: r[P3]=(r[P1] && r[P2]) 002246 ** 002247 ** Take the logical AND of the values in registers P1 and P2 and 002248 ** write the result into register P3. 002249 ** 002250 ** If either P1 or P2 is 0 (false) then the result is 0 even if 002251 ** the other input is NULL. A NULL and true or two NULLs give 002252 ** a NULL output. 002253 */ 002254 /* Opcode: Or P1 P2 P3 * * 002255 ** Synopsis: r[P3]=(r[P1] || r[P2]) 002256 ** 002257 ** Take the logical OR of the values in register P1 and P2 and 002258 ** store the answer in register P3. 002259 ** 002260 ** If either P1 or P2 is nonzero (true) then the result is 1 (true) 002261 ** even if the other input is NULL. A NULL and false or two NULLs 002262 ** give a NULL output. 002263 */ 002264 case OP_And: /* same as TK_AND, in1, in2, out3 */ 002265 case OP_Or: { /* same as TK_OR, in1, in2, out3 */ 002266 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ 002267 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ 002268 002269 pIn1 = &aMem[pOp->p1]; 002270 if( pIn1->flags & MEM_Null ){ 002271 v1 = 2; 002272 }else{ 002273 v1 = sqlite3VdbeIntValue(pIn1)!=0; 002274 } 002275 pIn2 = &aMem[pOp->p2]; 002276 if( pIn2->flags & MEM_Null ){ 002277 v2 = 2; 002278 }else{ 002279 v2 = sqlite3VdbeIntValue(pIn2)!=0; 002280 } 002281 if( pOp->opcode==OP_And ){ 002282 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; 002283 v1 = and_logic[v1*3+v2]; 002284 }else{ 002285 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 }; 002286 v1 = or_logic[v1*3+v2]; 002287 } 002288 pOut = &aMem[pOp->p3]; 002289 if( v1==2 ){ 002290 MemSetTypeFlag(pOut, MEM_Null); 002291 }else{ 002292 pOut->u.i = v1; 002293 MemSetTypeFlag(pOut, MEM_Int); 002294 } 002295 break; 002296 } 002297 002298 /* Opcode: Not P1 P2 * * * 002299 ** Synopsis: r[P2]= !r[P1] 002300 ** 002301 ** Interpret the value in register P1 as a boolean value. Store the 002302 ** boolean complement in register P2. If the value in register P1 is 002303 ** NULL, then a NULL is stored in P2. 002304 */ 002305 case OP_Not: { /* same as TK_NOT, in1, out2 */ 002306 pIn1 = &aMem[pOp->p1]; 002307 pOut = &aMem[pOp->p2]; 002308 sqlite3VdbeMemSetNull(pOut); 002309 if( (pIn1->flags & MEM_Null)==0 ){ 002310 pOut->flags = MEM_Int; 002311 pOut->u.i = !sqlite3VdbeIntValue(pIn1); 002312 } 002313 break; 002314 } 002315 002316 /* Opcode: BitNot P1 P2 * * * 002317 ** Synopsis: r[P1]= ~r[P1] 002318 ** 002319 ** Interpret the content of register P1 as an integer. Store the 002320 ** ones-complement of the P1 value into register P2. If P1 holds 002321 ** a NULL then store a NULL in P2. 002322 */ 002323 case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ 002324 pIn1 = &aMem[pOp->p1]; 002325 pOut = &aMem[pOp->p2]; 002326 sqlite3VdbeMemSetNull(pOut); 002327 if( (pIn1->flags & MEM_Null)==0 ){ 002328 pOut->flags = MEM_Int; 002329 pOut->u.i = ~sqlite3VdbeIntValue(pIn1); 002330 } 002331 break; 002332 } 002333 002334 /* Opcode: Once P1 P2 * * * 002335 ** 002336 ** If the P1 value is equal to the P1 value on the OP_Init opcode at 002337 ** instruction 0, then jump to P2. If the two P1 values differ, then 002338 ** set the P1 value on this opcode to equal the P1 value on the OP_Init 002339 ** and fall through. 002340 */ 002341 case OP_Once: { /* jump */ 002342 assert( p->aOp[0].opcode==OP_Init ); 002343 VdbeBranchTaken(p->aOp[0].p1==pOp->p1, 2); 002344 if( p->aOp[0].p1==pOp->p1 ){ 002345 goto jump_to_p2; 002346 }else{ 002347 pOp->p1 = p->aOp[0].p1; 002348 } 002349 break; 002350 } 002351 002352 /* Opcode: If P1 P2 P3 * * 002353 ** 002354 ** Jump to P2 if the value in register P1 is true. The value 002355 ** is considered true if it is numeric and non-zero. If the value 002356 ** in P1 is NULL then take the jump if and only if P3 is non-zero. 002357 */ 002358 /* Opcode: IfNot P1 P2 P3 * * 002359 ** 002360 ** Jump to P2 if the value in register P1 is False. The value 002361 ** is considered false if it has a numeric value of zero. If the value 002362 ** in P1 is NULL then take the jump if and only if P3 is non-zero. 002363 */ 002364 case OP_If: /* jump, in1 */ 002365 case OP_IfNot: { /* jump, in1 */ 002366 int c; 002367 pIn1 = &aMem[pOp->p1]; 002368 if( pIn1->flags & MEM_Null ){ 002369 c = pOp->p3; 002370 }else{ 002371 #ifdef SQLITE_OMIT_FLOATING_POINT 002372 c = sqlite3VdbeIntValue(pIn1)!=0; 002373 #else 002374 c = sqlite3VdbeRealValue(pIn1)!=0.0; 002375 #endif 002376 if( pOp->opcode==OP_IfNot ) c = !c; 002377 } 002378 VdbeBranchTaken(c!=0, 2); 002379 if( c ){ 002380 goto jump_to_p2; 002381 } 002382 break; 002383 } 002384 002385 /* Opcode: IsNull P1 P2 * * * 002386 ** Synopsis: if r[P1]==NULL goto P2 002387 ** 002388 ** Jump to P2 if the value in register P1 is NULL. 002389 */ 002390 case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ 002391 pIn1 = &aMem[pOp->p1]; 002392 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); 002393 if( (pIn1->flags & MEM_Null)!=0 ){ 002394 goto jump_to_p2; 002395 } 002396 break; 002397 } 002398 002399 /* Opcode: NotNull P1 P2 * * * 002400 ** Synopsis: if r[P1]!=NULL goto P2 002401 ** 002402 ** Jump to P2 if the value in register P1 is not NULL. 002403 */ 002404 case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ 002405 pIn1 = &aMem[pOp->p1]; 002406 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); 002407 if( (pIn1->flags & MEM_Null)==0 ){ 002408 goto jump_to_p2; 002409 } 002410 break; 002411 } 002412 002413 /* Opcode: Column P1 P2 P3 P4 P5 002414 ** Synopsis: r[P3]=PX 002415 ** 002416 ** Interpret the data that cursor P1 points to as a structure built using 002417 ** the MakeRecord instruction. (See the MakeRecord opcode for additional 002418 ** information about the format of the data.) Extract the P2-th column 002419 ** from this record. If there are less that (P2+1) 002420 ** values in the record, extract a NULL. 002421 ** 002422 ** The value extracted is stored in register P3. 002423 ** 002424 ** If the column contains fewer than P2 fields, then extract a NULL. Or, 002425 ** if the P4 argument is a P4_MEM use the value of the P4 argument as 002426 ** the result. 002427 ** 002428 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor, 002429 ** then the cache of the cursor is reset prior to extracting the column. 002430 ** The first OP_Column against a pseudo-table after the value of the content 002431 ** register has changed should have this bit set. 002432 ** 002433 ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when 002434 ** the result is guaranteed to only be used as the argument of a length() 002435 ** or typeof() function, respectively. The loading of large blobs can be 002436 ** skipped for length() and all content loading can be skipped for typeof(). 002437 */ 002438 case OP_Column: { 002439 int p2; /* column number to retrieve */ 002440 VdbeCursor *pC; /* The VDBE cursor */ 002441 BtCursor *pCrsr; /* The BTree cursor */ 002442 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ 002443 int len; /* The length of the serialized data for the column */ 002444 int i; /* Loop counter */ 002445 Mem *pDest; /* Where to write the extracted value */ 002446 Mem sMem; /* For storing the record being decoded */ 002447 const u8 *zData; /* Part of the record being decoded */ 002448 const u8 *zHdr; /* Next unparsed byte of the header */ 002449 const u8 *zEndHdr; /* Pointer to first byte after the header */ 002450 u32 offset; /* Offset into the data */ 002451 u64 offset64; /* 64-bit offset */ 002452 u32 avail; /* Number of bytes of available data */ 002453 u32 t; /* A type code from the record header */ 002454 Mem *pReg; /* PseudoTable input register */ 002455 002456 pC = p->apCsr[pOp->p1]; 002457 p2 = pOp->p2; 002458 002459 /* If the cursor cache is stale, bring it up-to-date */ 002460 rc = sqlite3VdbeCursorMoveto(&pC, &p2); 002461 if( rc ) goto abort_due_to_error; 002462 002463 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 002464 pDest = &aMem[pOp->p3]; 002465 memAboutToChange(p, pDest); 002466 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 002467 assert( pC!=0 ); 002468 assert( p2<pC->nField ); 002469 aOffset = pC->aOffset; 002470 assert( pC->eCurType!=CURTYPE_VTAB ); 002471 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); 002472 assert( pC->eCurType!=CURTYPE_SORTER ); 002473 002474 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/ 002475 if( pC->nullRow ){ 002476 if( pC->eCurType==CURTYPE_PSEUDO ){ 002477 assert( pC->uc.pseudoTableReg>0 ); 002478 pReg = &aMem[pC->uc.pseudoTableReg]; 002479 assert( pReg->flags & MEM_Blob ); 002480 assert( memIsValid(pReg) ); 002481 pC->payloadSize = pC->szRow = avail = pReg->n; 002482 pC->aRow = (u8*)pReg->z; 002483 }else{ 002484 sqlite3VdbeMemSetNull(pDest); 002485 goto op_column_out; 002486 } 002487 }else{ 002488 pCrsr = pC->uc.pCursor; 002489 assert( pC->eCurType==CURTYPE_BTREE ); 002490 assert( pCrsr ); 002491 assert( sqlite3BtreeCursorIsValid(pCrsr) ); 002492 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr); 002493 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &avail); 002494 assert( avail<=65536 ); /* Maximum page size is 64KiB */ 002495 if( pC->payloadSize <= (u32)avail ){ 002496 pC->szRow = pC->payloadSize; 002497 }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ 002498 goto too_big; 002499 }else{ 002500 pC->szRow = avail; 002501 } 002502 } 002503 pC->cacheStatus = p->cacheCtr; 002504 pC->iHdrOffset = getVarint32(pC->aRow, offset); 002505 pC->nHdrParsed = 0; 002506 aOffset[0] = offset; 002507 002508 002509 if( avail<offset ){ /*OPTIMIZATION-IF-FALSE*/ 002510 /* pC->aRow does not have to hold the entire row, but it does at least 002511 ** need to cover the header of the record. If pC->aRow does not contain 002512 ** the complete header, then set it to zero, forcing the header to be 002513 ** dynamically allocated. */ 002514 pC->aRow = 0; 002515 pC->szRow = 0; 002516 002517 /* Make sure a corrupt database has not given us an oversize header. 002518 ** Do this now to avoid an oversize memory allocation. 002519 ** 002520 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte 002521 ** types use so much data space that there can only be 4096 and 32 of 002522 ** them, respectively. So the maximum header length results from a 002523 ** 3-byte type for each of the maximum of 32768 columns plus three 002524 ** extra bytes for the header length itself. 32768*3 + 3 = 98307. 002525 */ 002526 if( offset > 98307 || offset > pC->payloadSize ){ 002527 rc = SQLITE_CORRUPT_BKPT; 002528 goto abort_due_to_error; 002529 } 002530 }else if( offset>0 ){ /*OPTIMIZATION-IF-TRUE*/ 002531 /* The following goto is an optimization. It can be omitted and 002532 ** everything will still work. But OP_Column is measurably faster 002533 ** by skipping the subsequent conditional, which is always true. 002534 */ 002535 zData = pC->aRow; 002536 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ 002537 goto op_column_read_header; 002538 } 002539 } 002540 002541 /* Make sure at least the first p2+1 entries of the header have been 002542 ** parsed and valid information is in aOffset[] and pC->aType[]. 002543 */ 002544 if( pC->nHdrParsed<=p2 ){ 002545 /* If there is more header available for parsing in the record, try 002546 ** to extract additional fields up through the p2+1-th field 002547 */ 002548 if( pC->iHdrOffset<aOffset[0] ){ 002549 /* Make sure zData points to enough of the record to cover the header. */ 002550 if( pC->aRow==0 ){ 002551 memset(&sMem, 0, sizeof(sMem)); 002552 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem); 002553 if( rc!=SQLITE_OK ) goto abort_due_to_error; 002554 zData = (u8*)sMem.z; 002555 }else{ 002556 zData = pC->aRow; 002557 } 002558 002559 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */ 002560 op_column_read_header: 002561 i = pC->nHdrParsed; 002562 offset64 = aOffset[i]; 002563 zHdr = zData + pC->iHdrOffset; 002564 zEndHdr = zData + aOffset[0]; 002565 do{ 002566 if( (t = zHdr[0])<0x80 ){ 002567 zHdr++; 002568 offset64 += sqlite3VdbeOneByteSerialTypeLen(t); 002569 }else{ 002570 zHdr += sqlite3GetVarint32(zHdr, &t); 002571 offset64 += sqlite3VdbeSerialTypeLen(t); 002572 } 002573 pC->aType[i++] = t; 002574 aOffset[i] = (u32)(offset64 & 0xffffffff); 002575 }while( i<=p2 && zHdr<zEndHdr ); 002576 002577 /* The record is corrupt if any of the following are true: 002578 ** (1) the bytes of the header extend past the declared header size 002579 ** (2) the entire header was used but not all data was used 002580 ** (3) the end of the data extends beyond the end of the record. 002581 */ 002582 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize)) 002583 || (offset64 > pC->payloadSize) 002584 ){ 002585 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); 002586 rc = SQLITE_CORRUPT_BKPT; 002587 goto abort_due_to_error; 002588 } 002589 002590 pC->nHdrParsed = i; 002591 pC->iHdrOffset = (u32)(zHdr - zData); 002592 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); 002593 }else{ 002594 t = 0; 002595 } 002596 002597 /* If after trying to extract new entries from the header, nHdrParsed is 002598 ** still not up to p2, that means that the record has fewer than p2 002599 ** columns. So the result will be either the default value or a NULL. 002600 */ 002601 if( pC->nHdrParsed<=p2 ){ 002602 if( pOp->p4type==P4_MEM ){ 002603 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); 002604 }else{ 002605 sqlite3VdbeMemSetNull(pDest); 002606 } 002607 goto op_column_out; 002608 } 002609 }else{ 002610 t = pC->aType[p2]; 002611 } 002612 002613 /* Extract the content for the p2+1-th column. Control can only 002614 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are 002615 ** all valid. 002616 */ 002617 assert( p2<pC->nHdrParsed ); 002618 assert( rc==SQLITE_OK ); 002619 assert( sqlite3VdbeCheckMemInvariants(pDest) ); 002620 if( VdbeMemDynamic(pDest) ){ 002621 sqlite3VdbeMemSetNull(pDest); 002622 } 002623 assert( t==pC->aType[p2] ); 002624 if( pC->szRow>=aOffset[p2+1] ){ 002625 /* This is the common case where the desired content fits on the original 002626 ** page - where the content is not on an overflow page */ 002627 zData = pC->aRow + aOffset[p2]; 002628 if( t<12 ){ 002629 sqlite3VdbeSerialGet(zData, t, pDest); 002630 }else{ 002631 /* If the column value is a string, we need a persistent value, not 002632 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent 002633 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize(). 002634 */ 002635 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term }; 002636 pDest->n = len = (t-12)/2; 002637 pDest->enc = encoding; 002638 if( pDest->szMalloc < len+2 ){ 002639 pDest->flags = MEM_Null; 002640 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem; 002641 }else{ 002642 pDest->z = pDest->zMalloc; 002643 } 002644 memcpy(pDest->z, zData, len); 002645 pDest->z[len] = 0; 002646 pDest->z[len+1] = 0; 002647 pDest->flags = aFlag[t&1]; 002648 } 002649 }else{ 002650 pDest->enc = encoding; 002651 /* This branch happens only when content is on overflow pages */ 002652 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0 002653 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)) 002654 || (len = sqlite3VdbeSerialTypeLen(t))==0 002655 ){ 002656 /* Content is irrelevant for 002657 ** 1. the typeof() function, 002658 ** 2. the length(X) function if X is a blob, and 002659 ** 3. if the content length is zero. 002660 ** So we might as well use bogus content rather than reading 002661 ** content from disk. */ 002662 static u8 aZero[8]; /* This is the bogus content */ 002663 sqlite3VdbeSerialGet(aZero, t, pDest); 002664 }else{ 002665 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest); 002666 if( rc!=SQLITE_OK ) goto abort_due_to_error; 002667 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); 002668 pDest->flags &= ~MEM_Ephem; 002669 } 002670 } 002671 002672 op_column_out: 002673 UPDATE_MAX_BLOBSIZE(pDest); 002674 REGISTER_TRACE(pOp->p3, pDest); 002675 break; 002676 } 002677 002678 /* Opcode: Affinity P1 P2 * P4 * 002679 ** Synopsis: affinity(r[P1@P2]) 002680 ** 002681 ** Apply affinities to a range of P2 registers starting with P1. 002682 ** 002683 ** P4 is a string that is P2 characters long. The nth character of the 002684 ** string indicates the column affinity that should be used for the nth 002685 ** memory cell in the range. 002686 */ 002687 case OP_Affinity: { 002688 const char *zAffinity; /* The affinity to be applied */ 002689 char cAff; /* A single character of affinity */ 002690 002691 zAffinity = pOp->p4.z; 002692 assert( zAffinity!=0 ); 002693 assert( zAffinity[pOp->p2]==0 ); 002694 pIn1 = &aMem[pOp->p1]; 002695 while( (cAff = *(zAffinity++))!=0 ){ 002696 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); 002697 assert( memIsValid(pIn1) ); 002698 applyAffinity(pIn1, cAff, encoding); 002699 pIn1++; 002700 } 002701 break; 002702 } 002703 002704 /* Opcode: MakeRecord P1 P2 P3 P4 * 002705 ** Synopsis: r[P3]=mkrec(r[P1@P2]) 002706 ** 002707 ** Convert P2 registers beginning with P1 into the [record format] 002708 ** use as a data record in a database table or as a key 002709 ** in an index. The OP_Column opcode can decode the record later. 002710 ** 002711 ** P4 may be a string that is P2 characters long. The nth character of the 002712 ** string indicates the column affinity that should be used for the nth 002713 ** field of the index key. 002714 ** 002715 ** The mapping from character to affinity is given by the SQLITE_AFF_ 002716 ** macros defined in sqliteInt.h. 002717 ** 002718 ** If P4 is NULL then all index fields have the affinity BLOB. 002719 */ 002720 case OP_MakeRecord: { 002721 u8 *zNewRecord; /* A buffer to hold the data for the new record */ 002722 Mem *pRec; /* The new record */ 002723 u64 nData; /* Number of bytes of data space */ 002724 int nHdr; /* Number of bytes of header space */ 002725 i64 nByte; /* Data space required for this record */ 002726 i64 nZero; /* Number of zero bytes at the end of the record */ 002727 int nVarint; /* Number of bytes in a varint */ 002728 u32 serial_type; /* Type field */ 002729 Mem *pData0; /* First field to be combined into the record */ 002730 Mem *pLast; /* Last field of the record */ 002731 int nField; /* Number of fields in the record */ 002732 char *zAffinity; /* The affinity string for the record */ 002733 int file_format; /* File format to use for encoding */ 002734 int i; /* Space used in zNewRecord[] header */ 002735 int j; /* Space used in zNewRecord[] content */ 002736 u32 len; /* Length of a field */ 002737 002738 /* Assuming the record contains N fields, the record format looks 002739 ** like this: 002740 ** 002741 ** ------------------------------------------------------------------------ 002742 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | 002743 ** ------------------------------------------------------------------------ 002744 ** 002745 ** Data(0) is taken from register P1. Data(1) comes from register P1+1 002746 ** and so forth. 002747 ** 002748 ** Each type field is a varint representing the serial type of the 002749 ** corresponding data element (see sqlite3VdbeSerialType()). The 002750 ** hdr-size field is also a varint which is the offset from the beginning 002751 ** of the record to data0. 002752 */ 002753 nData = 0; /* Number of bytes of data space */ 002754 nHdr = 0; /* Number of bytes of header space */ 002755 nZero = 0; /* Number of zero bytes at the end of the record */ 002756 nField = pOp->p1; 002757 zAffinity = pOp->p4.z; 002758 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 ); 002759 pData0 = &aMem[nField]; 002760 nField = pOp->p2; 002761 pLast = &pData0[nField-1]; 002762 file_format = p->minWriteFileFormat; 002763 002764 /* Identify the output register */ 002765 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); 002766 pOut = &aMem[pOp->p3]; 002767 memAboutToChange(p, pOut); 002768 002769 /* Apply the requested affinity to all inputs 002770 */ 002771 assert( pData0<=pLast ); 002772 if( zAffinity ){ 002773 pRec = pData0; 002774 do{ 002775 applyAffinity(pRec++, *(zAffinity++), encoding); 002776 assert( zAffinity[0]==0 || pRec<=pLast ); 002777 }while( zAffinity[0] ); 002778 } 002779 002780 /* Loop through the elements that will make up the record to figure 002781 ** out how much space is required for the new record. 002782 */ 002783 pRec = pLast; 002784 do{ 002785 assert( memIsValid(pRec) ); 002786 pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len); 002787 if( pRec->flags & MEM_Zero ){ 002788 if( nData ){ 002789 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; 002790 }else{ 002791 nZero += pRec->u.nZero; 002792 len -= pRec->u.nZero; 002793 } 002794 } 002795 nData += len; 002796 testcase( serial_type==127 ); 002797 testcase( serial_type==128 ); 002798 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type); 002799 if( pRec==pData0 ) break; 002800 pRec--; 002801 }while(1); 002802 002803 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint 002804 ** which determines the total number of bytes in the header. The varint 002805 ** value is the size of the header in bytes including the size varint 002806 ** itself. */ 002807 testcase( nHdr==126 ); 002808 testcase( nHdr==127 ); 002809 if( nHdr<=126 ){ 002810 /* The common case */ 002811 nHdr += 1; 002812 }else{ 002813 /* Rare case of a really large header */ 002814 nVarint = sqlite3VarintLen(nHdr); 002815 nHdr += nVarint; 002816 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++; 002817 } 002818 nByte = nHdr+nData; 002819 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ 002820 goto too_big; 002821 } 002822 002823 /* Make sure the output register has a buffer large enough to store 002824 ** the new record. The output register (pOp->p3) is not allowed to 002825 ** be one of the input registers (because the following call to 002826 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). 002827 */ 002828 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ 002829 goto no_mem; 002830 } 002831 zNewRecord = (u8 *)pOut->z; 002832 002833 /* Write the record */ 002834 i = putVarint32(zNewRecord, nHdr); 002835 j = nHdr; 002836 assert( pData0<=pLast ); 002837 pRec = pData0; 002838 do{ 002839 serial_type = pRec->uTemp; 002840 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more 002841 ** additional varints, one per column. */ 002842 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ 002843 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record 002844 ** immediately follow the header. */ 002845 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ 002846 }while( (++pRec)<=pLast ); 002847 assert( i==nHdr ); 002848 assert( j==nByte ); 002849 002850 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 002851 pOut->n = (int)nByte; 002852 pOut->flags = MEM_Blob; 002853 if( nZero ){ 002854 pOut->u.nZero = nZero; 002855 pOut->flags |= MEM_Zero; 002856 } 002857 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ 002858 REGISTER_TRACE(pOp->p3, pOut); 002859 UPDATE_MAX_BLOBSIZE(pOut); 002860 break; 002861 } 002862 002863 /* Opcode: Count P1 P2 * * * 002864 ** Synopsis: r[P2]=count() 002865 ** 002866 ** Store the number of entries (an integer value) in the table or index 002867 ** opened by cursor P1 in register P2 002868 */ 002869 #ifndef SQLITE_OMIT_BTREECOUNT 002870 case OP_Count: { /* out2 */ 002871 i64 nEntry; 002872 BtCursor *pCrsr; 002873 002874 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); 002875 pCrsr = p->apCsr[pOp->p1]->uc.pCursor; 002876 assert( pCrsr ); 002877 nEntry = 0; /* Not needed. Only used to silence a warning. */ 002878 rc = sqlite3BtreeCount(pCrsr, &nEntry); 002879 if( rc ) goto abort_due_to_error; 002880 pOut = out2Prerelease(p, pOp); 002881 pOut->u.i = nEntry; 002882 break; 002883 } 002884 #endif 002885 002886 /* Opcode: Savepoint P1 * * P4 * 002887 ** 002888 ** Open, release or rollback the savepoint named by parameter P4, depending 002889 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an 002890 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2. 002891 */ 002892 case OP_Savepoint: { 002893 int p1; /* Value of P1 operand */ 002894 char *zName; /* Name of savepoint */ 002895 int nName; 002896 Savepoint *pNew; 002897 Savepoint *pSavepoint; 002898 Savepoint *pTmp; 002899 int iSavepoint; 002900 int ii; 002901 002902 p1 = pOp->p1; 002903 zName = pOp->p4.z; 002904 002905 /* Assert that the p1 parameter is valid. Also that if there is no open 002906 ** transaction, then there cannot be any savepoints. 002907 */ 002908 assert( db->pSavepoint==0 || db->autoCommit==0 ); 002909 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); 002910 assert( db->pSavepoint || db->isTransactionSavepoint==0 ); 002911 assert( checkSavepointCount(db) ); 002912 assert( p->bIsReader ); 002913 002914 if( p1==SAVEPOINT_BEGIN ){ 002915 if( db->nVdbeWrite>0 ){ 002916 /* A new savepoint cannot be created if there are active write 002917 ** statements (i.e. open read/write incremental blob handles). 002918 */ 002919 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress"); 002920 rc = SQLITE_BUSY; 002921 }else{ 002922 nName = sqlite3Strlen30(zName); 002923 002924 #ifndef SQLITE_OMIT_VIRTUALTABLE 002925 /* This call is Ok even if this savepoint is actually a transaction 002926 ** savepoint (and therefore should not prompt xSavepoint()) callbacks. 002927 ** If this is a transaction savepoint being opened, it is guaranteed 002928 ** that the db->aVTrans[] array is empty. */ 002929 assert( db->autoCommit==0 || db->nVTrans==0 ); 002930 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, 002931 db->nStatement+db->nSavepoint); 002932 if( rc!=SQLITE_OK ) goto abort_due_to_error; 002933 #endif 002934 002935 /* Create a new savepoint structure. */ 002936 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1); 002937 if( pNew ){ 002938 pNew->zName = (char *)&pNew[1]; 002939 memcpy(pNew->zName, zName, nName+1); 002940 002941 /* If there is no open transaction, then mark this as a special 002942 ** "transaction savepoint". */ 002943 if( db->autoCommit ){ 002944 db->autoCommit = 0; 002945 db->isTransactionSavepoint = 1; 002946 }else{ 002947 db->nSavepoint++; 002948 } 002949 002950 /* Link the new savepoint into the database handle's list. */ 002951 pNew->pNext = db->pSavepoint; 002952 db->pSavepoint = pNew; 002953 pNew->nDeferredCons = db->nDeferredCons; 002954 pNew->nDeferredImmCons = db->nDeferredImmCons; 002955 } 002956 } 002957 }else{ 002958 iSavepoint = 0; 002959 002960 /* Find the named savepoint. If there is no such savepoint, then an 002961 ** an error is returned to the user. */ 002962 for( 002963 pSavepoint = db->pSavepoint; 002964 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); 002965 pSavepoint = pSavepoint->pNext 002966 ){ 002967 iSavepoint++; 002968 } 002969 if( !pSavepoint ){ 002970 sqlite3VdbeError(p, "no such savepoint: %s", zName); 002971 rc = SQLITE_ERROR; 002972 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ 002973 /* It is not possible to release (commit) a savepoint if there are 002974 ** active write statements. 002975 */ 002976 sqlite3VdbeError(p, "cannot release savepoint - " 002977 "SQL statements in progress"); 002978 rc = SQLITE_BUSY; 002979 }else{ 002980 002981 /* Determine whether or not this is a transaction savepoint. If so, 002982 ** and this is a RELEASE command, then the current transaction 002983 ** is committed. 002984 */ 002985 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; 002986 if( isTransaction && p1==SAVEPOINT_RELEASE ){ 002987 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ 002988 goto vdbe_return; 002989 } 002990 db->autoCommit = 1; 002991 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ 002992 p->pc = (int)(pOp - aOp); 002993 db->autoCommit = 0; 002994 p->rc = rc = SQLITE_BUSY; 002995 goto vdbe_return; 002996 } 002997 db->isTransactionSavepoint = 0; 002998 rc = p->rc; 002999 }else{ 003000 int isSchemaChange; 003001 iSavepoint = db->nSavepoint - iSavepoint - 1; 003002 if( p1==SAVEPOINT_ROLLBACK ){ 003003 isSchemaChange = (db->flags & SQLITE_InternChanges)!=0; 003004 for(ii=0; ii<db->nDb; ii++){ 003005 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, 003006 SQLITE_ABORT_ROLLBACK, 003007 isSchemaChange==0); 003008 if( rc!=SQLITE_OK ) goto abort_due_to_error; 003009 } 003010 }else{ 003011 isSchemaChange = 0; 003012 } 003013 for(ii=0; ii<db->nDb; ii++){ 003014 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); 003015 if( rc!=SQLITE_OK ){ 003016 goto abort_due_to_error; 003017 } 003018 } 003019 if( isSchemaChange ){ 003020 sqlite3ExpirePreparedStatements(db); 003021 sqlite3ResetAllSchemasOfConnection(db); 003022 db->flags = (db->flags | SQLITE_InternChanges); 003023 } 003024 } 003025 003026 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all 003027 ** savepoints nested inside of the savepoint being operated on. */ 003028 while( db->pSavepoint!=pSavepoint ){ 003029 pTmp = db->pSavepoint; 003030 db->pSavepoint = pTmp->pNext; 003031 sqlite3DbFree(db, pTmp); 003032 db->nSavepoint--; 003033 } 003034 003035 /* If it is a RELEASE, then destroy the savepoint being operated on 003036 ** too. If it is a ROLLBACK TO, then set the number of deferred 003037 ** constraint violations present in the database to the value stored 003038 ** when the savepoint was created. */ 003039 if( p1==SAVEPOINT_RELEASE ){ 003040 assert( pSavepoint==db->pSavepoint ); 003041 db->pSavepoint = pSavepoint->pNext; 003042 sqlite3DbFree(db, pSavepoint); 003043 if( !isTransaction ){ 003044 db->nSavepoint--; 003045 } 003046 }else{ 003047 db->nDeferredCons = pSavepoint->nDeferredCons; 003048 db->nDeferredImmCons = pSavepoint->nDeferredImmCons; 003049 } 003050 003051 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ 003052 rc = sqlite3VtabSavepoint(db, p1, iSavepoint); 003053 if( rc!=SQLITE_OK ) goto abort_due_to_error; 003054 } 003055 } 003056 } 003057 if( rc ) goto abort_due_to_error; 003058 003059 break; 003060 } 003061 003062 /* Opcode: AutoCommit P1 P2 * * * 003063 ** 003064 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll 003065 ** back any currently active btree transactions. If there are any active 003066 ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if 003067 ** there are active writing VMs or active VMs that use shared cache. 003068 ** 003069 ** This instruction causes the VM to halt. 003070 */ 003071 case OP_AutoCommit: { 003072 int desiredAutoCommit; 003073 int iRollback; 003074 003075 desiredAutoCommit = pOp->p1; 003076 iRollback = pOp->p2; 003077 assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); 003078 assert( desiredAutoCommit==1 || iRollback==0 ); 003079 assert( db->nVdbeActive>0 ); /* At least this one VM is active */ 003080 assert( p->bIsReader ); 003081 003082 if( desiredAutoCommit!=db->autoCommit ){ 003083 if( iRollback ){ 003084 assert( desiredAutoCommit==1 ); 003085 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); 003086 db->autoCommit = 1; 003087 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){ 003088 /* If this instruction implements a COMMIT and other VMs are writing 003089 ** return an error indicating that the other VMs must complete first. 003090 */ 003091 sqlite3VdbeError(p, "cannot commit transaction - " 003092 "SQL statements in progress"); 003093 rc = SQLITE_BUSY; 003094 goto abort_due_to_error; 003095 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ 003096 goto vdbe_return; 003097 }else{ 003098 db->autoCommit = (u8)desiredAutoCommit; 003099 } 003100 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ 003101 p->pc = (int)(pOp - aOp); 003102 db->autoCommit = (u8)(1-desiredAutoCommit); 003103 p->rc = rc = SQLITE_BUSY; 003104 goto vdbe_return; 003105 } 003106 assert( db->nStatement==0 ); 003107 sqlite3CloseSavepoints(db); 003108 if( p->rc==SQLITE_OK ){ 003109 rc = SQLITE_DONE; 003110 }else{ 003111 rc = SQLITE_ERROR; 003112 } 003113 goto vdbe_return; 003114 }else{ 003115 sqlite3VdbeError(p, 003116 (!desiredAutoCommit)?"cannot start a transaction within a transaction":( 003117 (iRollback)?"cannot rollback - no transaction is active": 003118 "cannot commit - no transaction is active")); 003119 003120 rc = SQLITE_ERROR; 003121 goto abort_due_to_error; 003122 } 003123 break; 003124 } 003125 003126 /* Opcode: Transaction P1 P2 P3 P4 P5 003127 ** 003128 ** Begin a transaction on database P1 if a transaction is not already 003129 ** active. 003130 ** If P2 is non-zero, then a write-transaction is started, or if a 003131 ** read-transaction is already active, it is upgraded to a write-transaction. 003132 ** If P2 is zero, then a read-transaction is started. 003133 ** 003134 ** P1 is the index of the database file on which the transaction is 003135 ** started. Index 0 is the main database file and index 1 is the 003136 ** file used for temporary tables. Indices of 2 or more are used for 003137 ** attached databases. 003138 ** 003139 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is 003140 ** true (this flag is set if the Vdbe may modify more than one row and may 003141 ** throw an ABORT exception), a statement transaction may also be opened. 003142 ** More specifically, a statement transaction is opened iff the database 003143 ** connection is currently not in autocommit mode, or if there are other 003144 ** active statements. A statement transaction allows the changes made by this 003145 ** VDBE to be rolled back after an error without having to roll back the 003146 ** entire transaction. If no error is encountered, the statement transaction 003147 ** will automatically commit when the VDBE halts. 003148 ** 003149 ** If P5!=0 then this opcode also checks the schema cookie against P3 003150 ** and the schema generation counter against P4. 003151 ** The cookie changes its value whenever the database schema changes. 003152 ** This operation is used to detect when that the cookie has changed 003153 ** and that the current process needs to reread the schema. If the schema 003154 ** cookie in P3 differs from the schema cookie in the database header or 003155 ** if the schema generation counter in P4 differs from the current 003156 ** generation counter, then an SQLITE_SCHEMA error is raised and execution 003157 ** halts. The sqlite3_step() wrapper function might then reprepare the 003158 ** statement and rerun it from the beginning. 003159 */ 003160 case OP_Transaction: { 003161 Btree *pBt; 003162 int iMeta; 003163 int iGen; 003164 003165 assert( p->bIsReader ); 003166 assert( p->readOnly==0 || pOp->p2==0 ); 003167 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 003168 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 003169 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){ 003170 rc = SQLITE_READONLY; 003171 goto abort_due_to_error; 003172 } 003173 pBt = db->aDb[pOp->p1].pBt; 003174 003175 if( pBt ){ 003176 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); 003177 testcase( rc==SQLITE_BUSY_SNAPSHOT ); 003178 testcase( rc==SQLITE_BUSY_RECOVERY ); 003179 if( rc!=SQLITE_OK ){ 003180 if( (rc&0xff)==SQLITE_BUSY ){ 003181 p->pc = (int)(pOp - aOp); 003182 p->rc = rc; 003183 goto vdbe_return; 003184 } 003185 goto abort_due_to_error; 003186 } 003187 003188 if( pOp->p2 && p->usesStmtJournal 003189 && (db->autoCommit==0 || db->nVdbeRead>1) 003190 ){ 003191 assert( sqlite3BtreeIsInTrans(pBt) ); 003192 if( p->iStatement==0 ){ 003193 assert( db->nStatement>=0 && db->nSavepoint>=0 ); 003194 db->nStatement++; 003195 p->iStatement = db->nSavepoint + db->nStatement; 003196 } 003197 003198 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); 003199 if( rc==SQLITE_OK ){ 003200 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); 003201 } 003202 003203 /* Store the current value of the database handles deferred constraint 003204 ** counter. If the statement transaction needs to be rolled back, 003205 ** the value of this counter needs to be restored too. */ 003206 p->nStmtDefCons = db->nDeferredCons; 003207 p->nStmtDefImmCons = db->nDeferredImmCons; 003208 } 003209 003210 /* Gather the schema version number for checking: 003211 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema 003212 ** version is checked to ensure that the schema has not changed since the 003213 ** SQL statement was prepared. 003214 */ 003215 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta); 003216 iGen = db->aDb[pOp->p1].pSchema->iGeneration; 003217 }else{ 003218 iGen = iMeta = 0; 003219 } 003220 assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); 003221 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){ 003222 sqlite3DbFree(db, p->zErrMsg); 003223 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); 003224 /* If the schema-cookie from the database file matches the cookie 003225 ** stored with the in-memory representation of the schema, do 003226 ** not reload the schema from the database file. 003227 ** 003228 ** If virtual-tables are in use, this is not just an optimization. 003229 ** Often, v-tables store their data in other SQLite tables, which 003230 ** are queried from within xNext() and other v-table methods using 003231 ** prepared queries. If such a query is out-of-date, we do not want to 003232 ** discard the database schema, as the user code implementing the 003233 ** v-table would have to be ready for the sqlite3_vtab structure itself 003234 ** to be invalidated whenever sqlite3_step() is called from within 003235 ** a v-table method. 003236 */ 003237 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ 003238 sqlite3ResetOneSchema(db, pOp->p1); 003239 } 003240 p->expired = 1; 003241 rc = SQLITE_SCHEMA; 003242 } 003243 if( rc ) goto abort_due_to_error; 003244 break; 003245 } 003246 003247 /* Opcode: ReadCookie P1 P2 P3 * * 003248 ** 003249 ** Read cookie number P3 from database P1 and write it into register P2. 003250 ** P3==1 is the schema version. P3==2 is the database format. 003251 ** P3==3 is the recommended pager cache size, and so forth. P1==0 is 003252 ** the main database file and P1==1 is the database file used to store 003253 ** temporary tables. 003254 ** 003255 ** There must be a read-lock on the database (either a transaction 003256 ** must be started or there must be an open cursor) before 003257 ** executing this instruction. 003258 */ 003259 case OP_ReadCookie: { /* out2 */ 003260 int iMeta; 003261 int iDb; 003262 int iCookie; 003263 003264 assert( p->bIsReader ); 003265 iDb = pOp->p1; 003266 iCookie = pOp->p3; 003267 assert( pOp->p3<SQLITE_N_BTREE_META ); 003268 assert( iDb>=0 && iDb<db->nDb ); 003269 assert( db->aDb[iDb].pBt!=0 ); 003270 assert( DbMaskTest(p->btreeMask, iDb) ); 003271 003272 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); 003273 pOut = out2Prerelease(p, pOp); 003274 pOut->u.i = iMeta; 003275 break; 003276 } 003277 003278 /* Opcode: SetCookie P1 P2 P3 * * 003279 ** 003280 ** Write the integer value P3 into cookie number P2 of database P1. 003281 ** P2==1 is the schema version. P2==2 is the database format. 003282 ** P2==3 is the recommended pager cache 003283 ** size, and so forth. P1==0 is the main database file and P1==1 is the 003284 ** database file used to store temporary tables. 003285 ** 003286 ** A transaction must be started before executing this opcode. 003287 */ 003288 case OP_SetCookie: { 003289 Db *pDb; 003290 assert( pOp->p2<SQLITE_N_BTREE_META ); 003291 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 003292 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 003293 assert( p->readOnly==0 ); 003294 pDb = &db->aDb[pOp->p1]; 003295 assert( pDb->pBt!=0 ); 003296 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); 003297 /* See note about index shifting on OP_ReadCookie */ 003298 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3); 003299 if( pOp->p2==BTREE_SCHEMA_VERSION ){ 003300 /* When the schema cookie changes, record the new cookie internally */ 003301 pDb->pSchema->schema_cookie = pOp->p3; 003302 db->flags |= SQLITE_InternChanges; 003303 }else if( pOp->p2==BTREE_FILE_FORMAT ){ 003304 /* Record changes in the file format */ 003305 pDb->pSchema->file_format = pOp->p3; 003306 } 003307 if( pOp->p1==1 ){ 003308 /* Invalidate all prepared statements whenever the TEMP database 003309 ** schema is changed. Ticket #1644 */ 003310 sqlite3ExpirePreparedStatements(db); 003311 p->expired = 0; 003312 } 003313 if( rc ) goto abort_due_to_error; 003314 break; 003315 } 003316 003317 /* Opcode: OpenRead P1 P2 P3 P4 P5 003318 ** Synopsis: root=P2 iDb=P3 003319 ** 003320 ** Open a read-only cursor for the database table whose root page is 003321 ** P2 in a database file. The database file is determined by P3. 003322 ** P3==0 means the main database, P3==1 means the database used for 003323 ** temporary tables, and P3>1 means used the corresponding attached 003324 ** database. Give the new cursor an identifier of P1. The P1 003325 ** values need not be contiguous but all P1 values should be small integers. 003326 ** It is an error for P1 to be negative. 003327 ** 003328 ** If P5!=0 then use the content of register P2 as the root page, not 003329 ** the value of P2 itself. 003330 ** 003331 ** There will be a read lock on the database whenever there is an 003332 ** open cursor. If the database was unlocked prior to this instruction 003333 ** then a read lock is acquired as part of this instruction. A read 003334 ** lock allows other processes to read the database but prohibits 003335 ** any other process from modifying the database. The read lock is 003336 ** released when all cursors are closed. If this instruction attempts 003337 ** to get a read lock but fails, the script terminates with an 003338 ** SQLITE_BUSY error code. 003339 ** 003340 ** The P4 value may be either an integer (P4_INT32) or a pointer to 003341 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 003342 ** structure, then said structure defines the content and collating 003343 ** sequence of the index being opened. Otherwise, if P4 is an integer 003344 ** value, it is set to the number of columns in the table. 003345 ** 003346 ** See also: OpenWrite, ReopenIdx 003347 */ 003348 /* Opcode: ReopenIdx P1 P2 P3 P4 P5 003349 ** Synopsis: root=P2 iDb=P3 003350 ** 003351 ** The ReopenIdx opcode works exactly like ReadOpen except that it first 003352 ** checks to see if the cursor on P1 is already open with a root page 003353 ** number of P2 and if it is this opcode becomes a no-op. In other words, 003354 ** if the cursor is already open, do not reopen it. 003355 ** 003356 ** The ReopenIdx opcode may only be used with P5==0 and with P4 being 003357 ** a P4_KEYINFO object. Furthermore, the P3 value must be the same as 003358 ** every other ReopenIdx or OpenRead for the same cursor number. 003359 ** 003360 ** See the OpenRead opcode documentation for additional information. 003361 */ 003362 /* Opcode: OpenWrite P1 P2 P3 P4 P5 003363 ** Synopsis: root=P2 iDb=P3 003364 ** 003365 ** Open a read/write cursor named P1 on the table or index whose root 003366 ** page is P2. Or if P5!=0 use the content of register P2 to find the 003367 ** root page. 003368 ** 003369 ** The P4 value may be either an integer (P4_INT32) or a pointer to 003370 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 003371 ** structure, then said structure defines the content and collating 003372 ** sequence of the index being opened. Otherwise, if P4 is an integer 003373 ** value, it is set to the number of columns in the table, or to the 003374 ** largest index of any column of the table that is actually used. 003375 ** 003376 ** This instruction works just like OpenRead except that it opens the cursor 003377 ** in read/write mode. For a given table, there can be one or more read-only 003378 ** cursors or a single read/write cursor but not both. 003379 ** 003380 ** See also OpenRead. 003381 */ 003382 case OP_ReopenIdx: { 003383 int nField; 003384 KeyInfo *pKeyInfo; 003385 int p2; 003386 int iDb; 003387 int wrFlag; 003388 Btree *pX; 003389 VdbeCursor *pCur; 003390 Db *pDb; 003391 003392 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); 003393 assert( pOp->p4type==P4_KEYINFO ); 003394 pCur = p->apCsr[pOp->p1]; 003395 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ 003396 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ 003397 goto open_cursor_set_hints; 003398 } 003399 /* If the cursor is not currently open or is open on a different 003400 ** index, then fall through into OP_OpenRead to force a reopen */ 003401 case OP_OpenRead: 003402 case OP_OpenWrite: 003403 003404 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); 003405 assert( p->bIsReader ); 003406 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx 003407 || p->readOnly==0 ); 003408 003409 if( p->expired ){ 003410 rc = SQLITE_ABORT_ROLLBACK; 003411 goto abort_due_to_error; 003412 } 003413 003414 nField = 0; 003415 pKeyInfo = 0; 003416 p2 = pOp->p2; 003417 iDb = pOp->p3; 003418 assert( iDb>=0 && iDb<db->nDb ); 003419 assert( DbMaskTest(p->btreeMask, iDb) ); 003420 pDb = &db->aDb[iDb]; 003421 pX = pDb->pBt; 003422 assert( pX!=0 ); 003423 if( pOp->opcode==OP_OpenWrite ){ 003424 assert( OPFLAG_FORDELETE==BTREE_FORDELETE ); 003425 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE); 003426 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 003427 if( pDb->pSchema->file_format < p->minWriteFileFormat ){ 003428 p->minWriteFileFormat = pDb->pSchema->file_format; 003429 } 003430 }else{ 003431 wrFlag = 0; 003432 } 003433 if( pOp->p5 & OPFLAG_P2ISREG ){ 003434 assert( p2>0 ); 003435 assert( p2<=(p->nMem+1 - p->nCursor) ); 003436 pIn2 = &aMem[p2]; 003437 assert( memIsValid(pIn2) ); 003438 assert( (pIn2->flags & MEM_Int)!=0 ); 003439 sqlite3VdbeMemIntegerify(pIn2); 003440 p2 = (int)pIn2->u.i; 003441 /* The p2 value always comes from a prior OP_CreateTable opcode and 003442 ** that opcode will always set the p2 value to 2 or more or else fail. 003443 ** If there were a failure, the prepared statement would have halted 003444 ** before reaching this instruction. */ 003445 assert( p2>=2 ); 003446 } 003447 if( pOp->p4type==P4_KEYINFO ){ 003448 pKeyInfo = pOp->p4.pKeyInfo; 003449 assert( pKeyInfo->enc==ENC(db) ); 003450 assert( pKeyInfo->db==db ); 003451 nField = pKeyInfo->nField+pKeyInfo->nXField; 003452 }else if( pOp->p4type==P4_INT32 ){ 003453 nField = pOp->p4.i; 003454 } 003455 assert( pOp->p1>=0 ); 003456 assert( nField>=0 ); 003457 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ 003458 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE); 003459 if( pCur==0 ) goto no_mem; 003460 pCur->nullRow = 1; 003461 pCur->isOrdered = 1; 003462 pCur->pgnoRoot = p2; 003463 #ifdef SQLITE_DEBUG 003464 pCur->wrFlag = wrFlag; 003465 #endif 003466 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor); 003467 pCur->pKeyInfo = pKeyInfo; 003468 /* Set the VdbeCursor.isTable variable. Previous versions of 003469 ** SQLite used to check if the root-page flags were sane at this point 003470 ** and report database corruption if they were not, but this check has 003471 ** since moved into the btree layer. */ 003472 pCur->isTable = pOp->p4type!=P4_KEYINFO; 003473 003474 open_cursor_set_hints: 003475 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); 003476 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); 003477 testcase( pOp->p5 & OPFLAG_BULKCSR ); 003478 #ifdef SQLITE_ENABLE_CURSOR_HINTS 003479 testcase( pOp->p2 & OPFLAG_SEEKEQ ); 003480 #endif 003481 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor, 003482 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); 003483 if( rc ) goto abort_due_to_error; 003484 break; 003485 } 003486 003487 /* Opcode: OpenEphemeral P1 P2 * P4 P5 003488 ** Synopsis: nColumn=P2 003489 ** 003490 ** Open a new cursor P1 to a transient table. 003491 ** The cursor is always opened read/write even if 003492 ** the main database is read-only. The ephemeral 003493 ** table is deleted automatically when the cursor is closed. 003494 ** 003495 ** P2 is the number of columns in the ephemeral table. 003496 ** The cursor points to a BTree table if P4==0 and to a BTree index 003497 ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure 003498 ** that defines the format of keys in the index. 003499 ** 003500 ** The P5 parameter can be a mask of the BTREE_* flags defined 003501 ** in btree.h. These flags control aspects of the operation of 003502 ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are 003503 ** added automatically. 003504 */ 003505 /* Opcode: OpenAutoindex P1 P2 * P4 * 003506 ** Synopsis: nColumn=P2 003507 ** 003508 ** This opcode works the same as OP_OpenEphemeral. It has a 003509 ** different name to distinguish its use. Tables created using 003510 ** by this opcode will be used for automatically created transient 003511 ** indices in joins. 003512 */ 003513 case OP_OpenAutoindex: 003514 case OP_OpenEphemeral: { 003515 VdbeCursor *pCx; 003516 KeyInfo *pKeyInfo; 003517 003518 static const int vfsFlags = 003519 SQLITE_OPEN_READWRITE | 003520 SQLITE_OPEN_CREATE | 003521 SQLITE_OPEN_EXCLUSIVE | 003522 SQLITE_OPEN_DELETEONCLOSE | 003523 SQLITE_OPEN_TRANSIENT_DB; 003524 assert( pOp->p1>=0 ); 003525 assert( pOp->p2>=0 ); 003526 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE); 003527 if( pCx==0 ) goto no_mem; 003528 pCx->nullRow = 1; 003529 pCx->isEphemeral = 1; 003530 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx, 003531 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags); 003532 if( rc==SQLITE_OK ){ 003533 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1); 003534 } 003535 if( rc==SQLITE_OK ){ 003536 /* If a transient index is required, create it by calling 003537 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before 003538 ** opening it. If a transient table is required, just use the 003539 ** automatically created table with root-page 1 (an BLOB_INTKEY table). 003540 */ 003541 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ 003542 int pgno; 003543 assert( pOp->p4type==P4_KEYINFO ); 003544 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5); 003545 if( rc==SQLITE_OK ){ 003546 assert( pgno==MASTER_ROOT+1 ); 003547 assert( pKeyInfo->db==db ); 003548 assert( pKeyInfo->enc==ENC(db) ); 003549 rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR, 003550 pKeyInfo, pCx->uc.pCursor); 003551 } 003552 pCx->isTable = 0; 003553 }else{ 003554 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR, 003555 0, pCx->uc.pCursor); 003556 pCx->isTable = 1; 003557 } 003558 } 003559 if( rc ) goto abort_due_to_error; 003560 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); 003561 break; 003562 } 003563 003564 /* Opcode: SorterOpen P1 P2 P3 P4 * 003565 ** 003566 ** This opcode works like OP_OpenEphemeral except that it opens 003567 ** a transient index that is specifically designed to sort large 003568 ** tables using an external merge-sort algorithm. 003569 ** 003570 ** If argument P3 is non-zero, then it indicates that the sorter may 003571 ** assume that a stable sort considering the first P3 fields of each 003572 ** key is sufficient to produce the required results. 003573 */ 003574 case OP_SorterOpen: { 003575 VdbeCursor *pCx; 003576 003577 assert( pOp->p1>=0 ); 003578 assert( pOp->p2>=0 ); 003579 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER); 003580 if( pCx==0 ) goto no_mem; 003581 pCx->pKeyInfo = pOp->p4.pKeyInfo; 003582 assert( pCx->pKeyInfo->db==db ); 003583 assert( pCx->pKeyInfo->enc==ENC(db) ); 003584 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); 003585 if( rc ) goto abort_due_to_error; 003586 break; 003587 } 003588 003589 /* Opcode: SequenceTest P1 P2 * * * 003590 ** Synopsis: if( cursor[P1].ctr++ ) pc = P2 003591 ** 003592 ** P1 is a sorter cursor. If the sequence counter is currently zero, jump 003593 ** to P2. Regardless of whether or not the jump is taken, increment the 003594 ** the sequence value. 003595 */ 003596 case OP_SequenceTest: { 003597 VdbeCursor *pC; 003598 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 003599 pC = p->apCsr[pOp->p1]; 003600 assert( isSorter(pC) ); 003601 if( (pC->seqCount++)==0 ){ 003602 goto jump_to_p2; 003603 } 003604 break; 003605 } 003606 003607 /* Opcode: OpenPseudo P1 P2 P3 * * 003608 ** Synopsis: P3 columns in r[P2] 003609 ** 003610 ** Open a new cursor that points to a fake table that contains a single 003611 ** row of data. The content of that one row is the content of memory 003612 ** register P2. In other words, cursor P1 becomes an alias for the 003613 ** MEM_Blob content contained in register P2. 003614 ** 003615 ** A pseudo-table created by this opcode is used to hold a single 003616 ** row output from the sorter so that the row can be decomposed into 003617 ** individual columns using the OP_Column opcode. The OP_Column opcode 003618 ** is the only cursor opcode that works with a pseudo-table. 003619 ** 003620 ** P3 is the number of fields in the records that will be stored by 003621 ** the pseudo-table. 003622 */ 003623 case OP_OpenPseudo: { 003624 VdbeCursor *pCx; 003625 003626 assert( pOp->p1>=0 ); 003627 assert( pOp->p3>=0 ); 003628 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO); 003629 if( pCx==0 ) goto no_mem; 003630 pCx->nullRow = 1; 003631 pCx->uc.pseudoTableReg = pOp->p2; 003632 pCx->isTable = 1; 003633 assert( pOp->p5==0 ); 003634 break; 003635 } 003636 003637 /* Opcode: Close P1 * * * * 003638 ** 003639 ** Close a cursor previously opened as P1. If P1 is not 003640 ** currently open, this instruction is a no-op. 003641 */ 003642 case OP_Close: { 003643 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 003644 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); 003645 p->apCsr[pOp->p1] = 0; 003646 break; 003647 } 003648 003649 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK 003650 /* Opcode: ColumnsUsed P1 * * P4 * 003651 ** 003652 ** This opcode (which only exists if SQLite was compiled with 003653 ** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the 003654 ** table or index for cursor P1 are used. P4 is a 64-bit integer 003655 ** (P4_INT64) in which the first 63 bits are one for each of the 003656 ** first 63 columns of the table or index that are actually used 003657 ** by the cursor. The high-order bit is set if any column after 003658 ** the 64th is used. 003659 */ 003660 case OP_ColumnsUsed: { 003661 VdbeCursor *pC; 003662 pC = p->apCsr[pOp->p1]; 003663 assert( pC->eCurType==CURTYPE_BTREE ); 003664 pC->maskUsed = *(u64*)pOp->p4.pI64; 003665 break; 003666 } 003667 #endif 003668 003669 /* Opcode: SeekGE P1 P2 P3 P4 * 003670 ** Synopsis: key=r[P3@P4] 003671 ** 003672 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 003673 ** use the value in register P3 as the key. If cursor P1 refers 003674 ** to an SQL index, then P3 is the first in an array of P4 registers 003675 ** that are used as an unpacked index key. 003676 ** 003677 ** Reposition cursor P1 so that it points to the smallest entry that 003678 ** is greater than or equal to the key value. If there are no records 003679 ** greater than or equal to the key and P2 is not zero, then jump to P2. 003680 ** 003681 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this 003682 ** opcode will always land on a record that equally equals the key, or 003683 ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this 003684 ** opcode must be followed by an IdxLE opcode with the same arguments. 003685 ** The IdxLE opcode will be skipped if this opcode succeeds, but the 003686 ** IdxLE opcode will be used on subsequent loop iterations. 003687 ** 003688 ** This opcode leaves the cursor configured to move in forward order, 003689 ** from the beginning toward the end. In other words, the cursor is 003690 ** configured to use Next, not Prev. 003691 ** 003692 ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe 003693 */ 003694 /* Opcode: SeekGT P1 P2 P3 P4 * 003695 ** Synopsis: key=r[P3@P4] 003696 ** 003697 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 003698 ** use the value in register P3 as a key. If cursor P1 refers 003699 ** to an SQL index, then P3 is the first in an array of P4 registers 003700 ** that are used as an unpacked index key. 003701 ** 003702 ** Reposition cursor P1 so that it points to the smallest entry that 003703 ** is greater than the key value. If there are no records greater than 003704 ** the key and P2 is not zero, then jump to P2. 003705 ** 003706 ** This opcode leaves the cursor configured to move in forward order, 003707 ** from the beginning toward the end. In other words, the cursor is 003708 ** configured to use Next, not Prev. 003709 ** 003710 ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe 003711 */ 003712 /* Opcode: SeekLT P1 P2 P3 P4 * 003713 ** Synopsis: key=r[P3@P4] 003714 ** 003715 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 003716 ** use the value in register P3 as a key. If cursor P1 refers 003717 ** to an SQL index, then P3 is the first in an array of P4 registers 003718 ** that are used as an unpacked index key. 003719 ** 003720 ** Reposition cursor P1 so that it points to the largest entry that 003721 ** is less than the key value. If there are no records less than 003722 ** the key and P2 is not zero, then jump to P2. 003723 ** 003724 ** This opcode leaves the cursor configured to move in reverse order, 003725 ** from the end toward the beginning. In other words, the cursor is 003726 ** configured to use Prev, not Next. 003727 ** 003728 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe 003729 */ 003730 /* Opcode: SeekLE P1 P2 P3 P4 * 003731 ** Synopsis: key=r[P3@P4] 003732 ** 003733 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 003734 ** use the value in register P3 as a key. If cursor P1 refers 003735 ** to an SQL index, then P3 is the first in an array of P4 registers 003736 ** that are used as an unpacked index key. 003737 ** 003738 ** Reposition cursor P1 so that it points to the largest entry that 003739 ** is less than or equal to the key value. If there are no records 003740 ** less than or equal to the key and P2 is not zero, then jump to P2. 003741 ** 003742 ** This opcode leaves the cursor configured to move in reverse order, 003743 ** from the end toward the beginning. In other words, the cursor is 003744 ** configured to use Prev, not Next. 003745 ** 003746 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this 003747 ** opcode will always land on a record that equally equals the key, or 003748 ** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this 003749 ** opcode must be followed by an IdxGE opcode with the same arguments. 003750 ** The IdxGE opcode will be skipped if this opcode succeeds, but the 003751 ** IdxGE opcode will be used on subsequent loop iterations. 003752 ** 003753 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt 003754 */ 003755 case OP_SeekLT: /* jump, in3 */ 003756 case OP_SeekLE: /* jump, in3 */ 003757 case OP_SeekGE: /* jump, in3 */ 003758 case OP_SeekGT: { /* jump, in3 */ 003759 int res; /* Comparison result */ 003760 int oc; /* Opcode */ 003761 VdbeCursor *pC; /* The cursor to seek */ 003762 UnpackedRecord r; /* The key to seek for */ 003763 int nField; /* Number of columns or fields in the key */ 003764 i64 iKey; /* The rowid we are to seek to */ 003765 int eqOnly; /* Only interested in == results */ 003766 003767 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 003768 assert( pOp->p2!=0 ); 003769 pC = p->apCsr[pOp->p1]; 003770 assert( pC!=0 ); 003771 assert( pC->eCurType==CURTYPE_BTREE ); 003772 assert( OP_SeekLE == OP_SeekLT+1 ); 003773 assert( OP_SeekGE == OP_SeekLT+2 ); 003774 assert( OP_SeekGT == OP_SeekLT+3 ); 003775 assert( pC->isOrdered ); 003776 assert( pC->uc.pCursor!=0 ); 003777 oc = pOp->opcode; 003778 eqOnly = 0; 003779 pC->nullRow = 0; 003780 #ifdef SQLITE_DEBUG 003781 pC->seekOp = pOp->opcode; 003782 #endif 003783 003784 if( pC->isTable ){ 003785 /* The BTREE_SEEK_EQ flag is only set on index cursors */ 003786 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 003787 || CORRUPT_DB ); 003788 003789 /* The input value in P3 might be of any type: integer, real, string, 003790 ** blob, or NULL. But it needs to be an integer before we can do 003791 ** the seek, so convert it. */ 003792 pIn3 = &aMem[pOp->p3]; 003793 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ 003794 applyNumericAffinity(pIn3, 0); 003795 } 003796 iKey = sqlite3VdbeIntValue(pIn3); 003797 003798 /* If the P3 value could not be converted into an integer without 003799 ** loss of information, then special processing is required... */ 003800 if( (pIn3->flags & MEM_Int)==0 ){ 003801 if( (pIn3->flags & MEM_Real)==0 ){ 003802 /* If the P3 value cannot be converted into any kind of a number, 003803 ** then the seek is not possible, so jump to P2 */ 003804 VdbeBranchTaken(1,2); goto jump_to_p2; 003805 break; 003806 } 003807 003808 /* If the approximation iKey is larger than the actual real search 003809 ** term, substitute >= for > and < for <=. e.g. if the search term 003810 ** is 4.9 and the integer approximation 5: 003811 ** 003812 ** (x > 4.9) -> (x >= 5) 003813 ** (x <= 4.9) -> (x < 5) 003814 */ 003815 if( pIn3->u.r<(double)iKey ){ 003816 assert( OP_SeekGE==(OP_SeekGT-1) ); 003817 assert( OP_SeekLT==(OP_SeekLE-1) ); 003818 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) ); 003819 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--; 003820 } 003821 003822 /* If the approximation iKey is smaller than the actual real search 003823 ** term, substitute <= for < and > for >=. */ 003824 else if( pIn3->u.r>(double)iKey ){ 003825 assert( OP_SeekLE==(OP_SeekLT+1) ); 003826 assert( OP_SeekGT==(OP_SeekGE+1) ); 003827 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); 003828 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; 003829 } 003830 } 003831 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res); 003832 pC->movetoTarget = iKey; /* Used by OP_Delete */ 003833 if( rc!=SQLITE_OK ){ 003834 goto abort_due_to_error; 003835 } 003836 }else{ 003837 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and 003838 ** OP_SeekLE opcodes are allowed, and these must be immediately followed 003839 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key. 003840 */ 003841 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){ 003842 eqOnly = 1; 003843 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE ); 003844 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); 003845 assert( pOp[1].p1==pOp[0].p1 ); 003846 assert( pOp[1].p2==pOp[0].p2 ); 003847 assert( pOp[1].p3==pOp[0].p3 ); 003848 assert( pOp[1].p4.i==pOp[0].p4.i ); 003849 } 003850 003851 nField = pOp->p4.i; 003852 assert( pOp->p4type==P4_INT32 ); 003853 assert( nField>0 ); 003854 r.pKeyInfo = pC->pKeyInfo; 003855 r.nField = (u16)nField; 003856 003857 /* The next line of code computes as follows, only faster: 003858 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ 003859 ** r.default_rc = -1; 003860 ** }else{ 003861 ** r.default_rc = +1; 003862 ** } 003863 */ 003864 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); 003865 assert( oc!=OP_SeekGT || r.default_rc==-1 ); 003866 assert( oc!=OP_SeekLE || r.default_rc==-1 ); 003867 assert( oc!=OP_SeekGE || r.default_rc==+1 ); 003868 assert( oc!=OP_SeekLT || r.default_rc==+1 ); 003869 003870 r.aMem = &aMem[pOp->p3]; 003871 #ifdef SQLITE_DEBUG 003872 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } 003873 #endif 003874 r.eqSeen = 0; 003875 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res); 003876 if( rc!=SQLITE_OK ){ 003877 goto abort_due_to_error; 003878 } 003879 if( eqOnly && r.eqSeen==0 ){ 003880 assert( res!=0 ); 003881 goto seek_not_found; 003882 } 003883 } 003884 pC->deferredMoveto = 0; 003885 pC->cacheStatus = CACHE_STALE; 003886 #ifdef SQLITE_TEST 003887 sqlite3_search_count++; 003888 #endif 003889 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); 003890 if( res<0 || (res==0 && oc==OP_SeekGT) ){ 003891 res = 0; 003892 rc = sqlite3BtreeNext(pC->uc.pCursor, &res); 003893 if( rc!=SQLITE_OK ) goto abort_due_to_error; 003894 }else{ 003895 res = 0; 003896 } 003897 }else{ 003898 assert( oc==OP_SeekLT || oc==OP_SeekLE ); 003899 if( res>0 || (res==0 && oc==OP_SeekLT) ){ 003900 res = 0; 003901 rc = sqlite3BtreePrevious(pC->uc.pCursor, &res); 003902 if( rc!=SQLITE_OK ) goto abort_due_to_error; 003903 }else{ 003904 /* res might be negative because the table is empty. Check to 003905 ** see if this is the case. 003906 */ 003907 res = sqlite3BtreeEof(pC->uc.pCursor); 003908 } 003909 } 003910 seek_not_found: 003911 assert( pOp->p2>0 ); 003912 VdbeBranchTaken(res!=0,2); 003913 if( res ){ 003914 goto jump_to_p2; 003915 }else if( eqOnly ){ 003916 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); 003917 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */ 003918 } 003919 break; 003920 } 003921 003922 /* Opcode: Found P1 P2 P3 P4 * 003923 ** Synopsis: key=r[P3@P4] 003924 ** 003925 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If 003926 ** P4>0 then register P3 is the first of P4 registers that form an unpacked 003927 ** record. 003928 ** 003929 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 003930 ** is a prefix of any entry in P1 then a jump is made to P2 and 003931 ** P1 is left pointing at the matching entry. 003932 ** 003933 ** This operation leaves the cursor in a state where it can be 003934 ** advanced in the forward direction. The Next instruction will work, 003935 ** but not the Prev instruction. 003936 ** 003937 ** See also: NotFound, NoConflict, NotExists. SeekGe 003938 */ 003939 /* Opcode: NotFound P1 P2 P3 P4 * 003940 ** Synopsis: key=r[P3@P4] 003941 ** 003942 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If 003943 ** P4>0 then register P3 is the first of P4 registers that form an unpacked 003944 ** record. 003945 ** 003946 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 003947 ** is not the prefix of any entry in P1 then a jump is made to P2. If P1 003948 ** does contain an entry whose prefix matches the P3/P4 record then control 003949 ** falls through to the next instruction and P1 is left pointing at the 003950 ** matching entry. 003951 ** 003952 ** This operation leaves the cursor in a state where it cannot be 003953 ** advanced in either direction. In other words, the Next and Prev 003954 ** opcodes do not work after this operation. 003955 ** 003956 ** See also: Found, NotExists, NoConflict 003957 */ 003958 /* Opcode: NoConflict P1 P2 P3 P4 * 003959 ** Synopsis: key=r[P3@P4] 003960 ** 003961 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If 003962 ** P4>0 then register P3 is the first of P4 registers that form an unpacked 003963 ** record. 003964 ** 003965 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 003966 ** contains any NULL value, jump immediately to P2. If all terms of the 003967 ** record are not-NULL then a check is done to determine if any row in the 003968 ** P1 index btree has a matching key prefix. If there are no matches, jump 003969 ** immediately to P2. If there is a match, fall through and leave the P1 003970 ** cursor pointing to the matching row. 003971 ** 003972 ** This opcode is similar to OP_NotFound with the exceptions that the 003973 ** branch is always taken if any part of the search key input is NULL. 003974 ** 003975 ** This operation leaves the cursor in a state where it cannot be 003976 ** advanced in either direction. In other words, the Next and Prev 003977 ** opcodes do not work after this operation. 003978 ** 003979 ** See also: NotFound, Found, NotExists 003980 */ 003981 case OP_NoConflict: /* jump, in3 */ 003982 case OP_NotFound: /* jump, in3 */ 003983 case OP_Found: { /* jump, in3 */ 003984 int alreadyExists; 003985 int takeJump; 003986 int ii; 003987 VdbeCursor *pC; 003988 int res; 003989 UnpackedRecord *pFree; 003990 UnpackedRecord *pIdxKey; 003991 UnpackedRecord r; 003992 003993 #ifdef SQLITE_TEST 003994 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; 003995 #endif 003996 003997 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 003998 assert( pOp->p4type==P4_INT32 ); 003999 pC = p->apCsr[pOp->p1]; 004000 assert( pC!=0 ); 004001 #ifdef SQLITE_DEBUG 004002 pC->seekOp = pOp->opcode; 004003 #endif 004004 pIn3 = &aMem[pOp->p3]; 004005 assert( pC->eCurType==CURTYPE_BTREE ); 004006 assert( pC->uc.pCursor!=0 ); 004007 assert( pC->isTable==0 ); 004008 if( pOp->p4.i>0 ){ 004009 r.pKeyInfo = pC->pKeyInfo; 004010 r.nField = (u16)pOp->p4.i; 004011 r.aMem = pIn3; 004012 #ifdef SQLITE_DEBUG 004013 for(ii=0; ii<r.nField; ii++){ 004014 assert( memIsValid(&r.aMem[ii]) ); 004015 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 ); 004016 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); 004017 } 004018 #endif 004019 pIdxKey = &r; 004020 pFree = 0; 004021 }else{ 004022 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo); 004023 if( pIdxKey==0 ) goto no_mem; 004024 assert( pIn3->flags & MEM_Blob ); 004025 (void)ExpandBlob(pIn3); 004026 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); 004027 } 004028 pIdxKey->default_rc = 0; 004029 takeJump = 0; 004030 if( pOp->opcode==OP_NoConflict ){ 004031 /* For the OP_NoConflict opcode, take the jump if any of the 004032 ** input fields are NULL, since any key with a NULL will not 004033 ** conflict */ 004034 for(ii=0; ii<pIdxKey->nField; ii++){ 004035 if( pIdxKey->aMem[ii].flags & MEM_Null ){ 004036 takeJump = 1; 004037 break; 004038 } 004039 } 004040 } 004041 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res); 004042 if( pFree ) sqlite3DbFree(db, pFree); 004043 if( rc!=SQLITE_OK ){ 004044 goto abort_due_to_error; 004045 } 004046 pC->seekResult = res; 004047 alreadyExists = (res==0); 004048 pC->nullRow = 1-alreadyExists; 004049 pC->deferredMoveto = 0; 004050 pC->cacheStatus = CACHE_STALE; 004051 if( pOp->opcode==OP_Found ){ 004052 VdbeBranchTaken(alreadyExists!=0,2); 004053 if( alreadyExists ) goto jump_to_p2; 004054 }else{ 004055 VdbeBranchTaken(takeJump||alreadyExists==0,2); 004056 if( takeJump || !alreadyExists ) goto jump_to_p2; 004057 } 004058 break; 004059 } 004060 004061 /* Opcode: SeekRowid P1 P2 P3 * * 004062 ** Synopsis: intkey=r[P3] 004063 ** 004064 ** P1 is the index of a cursor open on an SQL table btree (with integer 004065 ** keys). If register P3 does not contain an integer or if P1 does not 004066 ** contain a record with rowid P3 then jump immediately to P2. 004067 ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain 004068 ** a record with rowid P3 then 004069 ** leave the cursor pointing at that record and fall through to the next 004070 ** instruction. 004071 ** 004072 ** The OP_NotExists opcode performs the same operation, but with OP_NotExists 004073 ** the P3 register must be guaranteed to contain an integer value. With this 004074 ** opcode, register P3 might not contain an integer. 004075 ** 004076 ** The OP_NotFound opcode performs the same operation on index btrees 004077 ** (with arbitrary multi-value keys). 004078 ** 004079 ** This opcode leaves the cursor in a state where it cannot be advanced 004080 ** in either direction. In other words, the Next and Prev opcodes will 004081 ** not work following this opcode. 004082 ** 004083 ** See also: Found, NotFound, NoConflict, SeekRowid 004084 */ 004085 /* Opcode: NotExists P1 P2 P3 * * 004086 ** Synopsis: intkey=r[P3] 004087 ** 004088 ** P1 is the index of a cursor open on an SQL table btree (with integer 004089 ** keys). P3 is an integer rowid. If P1 does not contain a record with 004090 ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an 004091 ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then 004092 ** leave the cursor pointing at that record and fall through to the next 004093 ** instruction. 004094 ** 004095 ** The OP_SeekRowid opcode performs the same operation but also allows the 004096 ** P3 register to contain a non-integer value, in which case the jump is 004097 ** always taken. This opcode requires that P3 always contain an integer. 004098 ** 004099 ** The OP_NotFound opcode performs the same operation on index btrees 004100 ** (with arbitrary multi-value keys). 004101 ** 004102 ** This opcode leaves the cursor in a state where it cannot be advanced 004103 ** in either direction. In other words, the Next and Prev opcodes will 004104 ** not work following this opcode. 004105 ** 004106 ** See also: Found, NotFound, NoConflict, SeekRowid 004107 */ 004108 case OP_SeekRowid: { /* jump, in3 */ 004109 VdbeCursor *pC; 004110 BtCursor *pCrsr; 004111 int res; 004112 u64 iKey; 004113 004114 pIn3 = &aMem[pOp->p3]; 004115 if( (pIn3->flags & MEM_Int)==0 ){ 004116 applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding); 004117 if( (pIn3->flags & MEM_Int)==0 ) goto jump_to_p2; 004118 } 004119 /* Fall through into OP_NotExists */ 004120 case OP_NotExists: /* jump, in3 */ 004121 pIn3 = &aMem[pOp->p3]; 004122 assert( pIn3->flags & MEM_Int ); 004123 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004124 pC = p->apCsr[pOp->p1]; 004125 assert( pC!=0 ); 004126 #ifdef SQLITE_DEBUG 004127 pC->seekOp = 0; 004128 #endif 004129 assert( pC->isTable ); 004130 assert( pC->eCurType==CURTYPE_BTREE ); 004131 pCrsr = pC->uc.pCursor; 004132 assert( pCrsr!=0 ); 004133 res = 0; 004134 iKey = pIn3->u.i; 004135 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); 004136 assert( rc==SQLITE_OK || res==0 ); 004137 pC->movetoTarget = iKey; /* Used by OP_Delete */ 004138 pC->nullRow = 0; 004139 pC->cacheStatus = CACHE_STALE; 004140 pC->deferredMoveto = 0; 004141 VdbeBranchTaken(res!=0,2); 004142 pC->seekResult = res; 004143 if( res!=0 ){ 004144 assert( rc==SQLITE_OK ); 004145 if( pOp->p2==0 ){ 004146 rc = SQLITE_CORRUPT_BKPT; 004147 }else{ 004148 goto jump_to_p2; 004149 } 004150 } 004151 if( rc ) goto abort_due_to_error; 004152 break; 004153 } 004154 004155 /* Opcode: Sequence P1 P2 * * * 004156 ** Synopsis: r[P2]=cursor[P1].ctr++ 004157 ** 004158 ** Find the next available sequence number for cursor P1. 004159 ** Write the sequence number into register P2. 004160 ** The sequence number on the cursor is incremented after this 004161 ** instruction. 004162 */ 004163 case OP_Sequence: { /* out2 */ 004164 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004165 assert( p->apCsr[pOp->p1]!=0 ); 004166 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB ); 004167 pOut = out2Prerelease(p, pOp); 004168 pOut->u.i = p->apCsr[pOp->p1]->seqCount++; 004169 break; 004170 } 004171 004172 004173 /* Opcode: NewRowid P1 P2 P3 * * 004174 ** Synopsis: r[P2]=rowid 004175 ** 004176 ** Get a new integer record number (a.k.a "rowid") used as the key to a table. 004177 ** The record number is not previously used as a key in the database 004178 ** table that cursor P1 points to. The new record number is written 004179 ** written to register P2. 004180 ** 004181 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds 004182 ** the largest previously generated record number. No new record numbers are 004183 ** allowed to be less than this value. When this value reaches its maximum, 004184 ** an SQLITE_FULL error is generated. The P3 register is updated with the ' 004185 ** generated record number. This P3 mechanism is used to help implement the 004186 ** AUTOINCREMENT feature. 004187 */ 004188 case OP_NewRowid: { /* out2 */ 004189 i64 v; /* The new rowid */ 004190 VdbeCursor *pC; /* Cursor of table to get the new rowid */ 004191 int res; /* Result of an sqlite3BtreeLast() */ 004192 int cnt; /* Counter to limit the number of searches */ 004193 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ 004194 VdbeFrame *pFrame; /* Root frame of VDBE */ 004195 004196 v = 0; 004197 res = 0; 004198 pOut = out2Prerelease(p, pOp); 004199 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004200 pC = p->apCsr[pOp->p1]; 004201 assert( pC!=0 ); 004202 assert( pC->eCurType==CURTYPE_BTREE ); 004203 assert( pC->uc.pCursor!=0 ); 004204 { 004205 /* The next rowid or record number (different terms for the same 004206 ** thing) is obtained in a two-step algorithm. 004207 ** 004208 ** First we attempt to find the largest existing rowid and add one 004209 ** to that. But if the largest existing rowid is already the maximum 004210 ** positive integer, we have to fall through to the second 004211 ** probabilistic algorithm 004212 ** 004213 ** The second algorithm is to select a rowid at random and see if 004214 ** it already exists in the table. If it does not exist, we have 004215 ** succeeded. If the random rowid does exist, we select a new one 004216 ** and try again, up to 100 times. 004217 */ 004218 assert( pC->isTable ); 004219 004220 #ifdef SQLITE_32BIT_ROWID 004221 # define MAX_ROWID 0x7fffffff 004222 #else 004223 /* Some compilers complain about constants of the form 0x7fffffffffffffff. 004224 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems 004225 ** to provide the constant while making all compilers happy. 004226 */ 004227 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) 004228 #endif 004229 004230 if( !pC->useRandomRowid ){ 004231 rc = sqlite3BtreeLast(pC->uc.pCursor, &res); 004232 if( rc!=SQLITE_OK ){ 004233 goto abort_due_to_error; 004234 } 004235 if( res ){ 004236 v = 1; /* IMP: R-61914-48074 */ 004237 }else{ 004238 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) ); 004239 v = sqlite3BtreeIntegerKey(pC->uc.pCursor); 004240 if( v>=MAX_ROWID ){ 004241 pC->useRandomRowid = 1; 004242 }else{ 004243 v++; /* IMP: R-29538-34987 */ 004244 } 004245 } 004246 } 004247 004248 #ifndef SQLITE_OMIT_AUTOINCREMENT 004249 if( pOp->p3 ){ 004250 /* Assert that P3 is a valid memory cell. */ 004251 assert( pOp->p3>0 ); 004252 if( p->pFrame ){ 004253 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); 004254 /* Assert that P3 is a valid memory cell. */ 004255 assert( pOp->p3<=pFrame->nMem ); 004256 pMem = &pFrame->aMem[pOp->p3]; 004257 }else{ 004258 /* Assert that P3 is a valid memory cell. */ 004259 assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); 004260 pMem = &aMem[pOp->p3]; 004261 memAboutToChange(p, pMem); 004262 } 004263 assert( memIsValid(pMem) ); 004264 004265 REGISTER_TRACE(pOp->p3, pMem); 004266 sqlite3VdbeMemIntegerify(pMem); 004267 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ 004268 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ 004269 rc = SQLITE_FULL; /* IMP: R-17817-00630 */ 004270 goto abort_due_to_error; 004271 } 004272 if( v<pMem->u.i+1 ){ 004273 v = pMem->u.i + 1; 004274 } 004275 pMem->u.i = v; 004276 } 004277 #endif 004278 if( pC->useRandomRowid ){ 004279 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the 004280 ** largest possible integer (9223372036854775807) then the database 004281 ** engine starts picking positive candidate ROWIDs at random until 004282 ** it finds one that is not previously used. */ 004283 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is 004284 ** an AUTOINCREMENT table. */ 004285 cnt = 0; 004286 do{ 004287 sqlite3_randomness(sizeof(v), &v); 004288 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ 004289 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v, 004290 0, &res))==SQLITE_OK) 004291 && (res==0) 004292 && (++cnt<100)); 004293 if( rc ) goto abort_due_to_error; 004294 if( res==0 ){ 004295 rc = SQLITE_FULL; /* IMP: R-38219-53002 */ 004296 goto abort_due_to_error; 004297 } 004298 assert( v>0 ); /* EV: R-40812-03570 */ 004299 } 004300 pC->deferredMoveto = 0; 004301 pC->cacheStatus = CACHE_STALE; 004302 } 004303 pOut->u.i = v; 004304 break; 004305 } 004306 004307 /* Opcode: Insert P1 P2 P3 P4 P5 004308 ** Synopsis: intkey=r[P3] data=r[P2] 004309 ** 004310 ** Write an entry into the table of cursor P1. A new entry is 004311 ** created if it doesn't already exist or the data for an existing 004312 ** entry is overwritten. The data is the value MEM_Blob stored in register 004313 ** number P2. The key is stored in register P3. The key must 004314 ** be a MEM_Int. 004315 ** 004316 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is 004317 ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, 004318 ** then rowid is stored for subsequent return by the 004319 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). 004320 ** 004321 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might 004322 ** run faster by avoiding an unnecessary seek on cursor P1. However, 004323 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior 004324 ** seeks on the cursor or if the most recent seek used a key equal to P3. 004325 ** 004326 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an 004327 ** UPDATE operation. Otherwise (if the flag is clear) then this opcode 004328 ** is part of an INSERT operation. The difference is only important to 004329 ** the update hook. 004330 ** 004331 ** Parameter P4 may point to a Table structure, or may be NULL. If it is 004332 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked 004333 ** following a successful insert. 004334 ** 004335 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically 004336 ** allocated, then ownership of P2 is transferred to the pseudo-cursor 004337 ** and register P2 becomes ephemeral. If the cursor is changed, the 004338 ** value of register P2 will then change. Make sure this does not 004339 ** cause any problems.) 004340 ** 004341 ** This instruction only works on tables. The equivalent instruction 004342 ** for indices is OP_IdxInsert. 004343 */ 004344 /* Opcode: InsertInt P1 P2 P3 P4 P5 004345 ** Synopsis: intkey=P3 data=r[P2] 004346 ** 004347 ** This works exactly like OP_Insert except that the key is the 004348 ** integer value P3, not the value of the integer stored in register P3. 004349 */ 004350 case OP_Insert: 004351 case OP_InsertInt: { 004352 Mem *pData; /* MEM cell holding data for the record to be inserted */ 004353 Mem *pKey; /* MEM cell holding key for the record */ 004354 VdbeCursor *pC; /* Cursor to table into which insert is written */ 004355 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ 004356 const char *zDb; /* database name - used by the update hook */ 004357 Table *pTab; /* Table structure - used by update and pre-update hooks */ 004358 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ 004359 BtreePayload x; /* Payload to be inserted */ 004360 004361 op = 0; 004362 pData = &aMem[pOp->p2]; 004363 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004364 assert( memIsValid(pData) ); 004365 pC = p->apCsr[pOp->p1]; 004366 assert( pC!=0 ); 004367 assert( pC->eCurType==CURTYPE_BTREE ); 004368 assert( pC->uc.pCursor!=0 ); 004369 assert( pC->isTable ); 004370 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); 004371 REGISTER_TRACE(pOp->p2, pData); 004372 004373 if( pOp->opcode==OP_Insert ){ 004374 pKey = &aMem[pOp->p3]; 004375 assert( pKey->flags & MEM_Int ); 004376 assert( memIsValid(pKey) ); 004377 REGISTER_TRACE(pOp->p3, pKey); 004378 x.nKey = pKey->u.i; 004379 }else{ 004380 assert( pOp->opcode==OP_InsertInt ); 004381 x.nKey = pOp->p3; 004382 } 004383 004384 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ 004385 assert( pC->isTable ); 004386 assert( pC->iDb>=0 ); 004387 zDb = db->aDb[pC->iDb].zDbSName; 004388 pTab = pOp->p4.pTab; 004389 assert( HasRowid(pTab) ); 004390 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); 004391 }else{ 004392 pTab = 0; /* Not needed. Silence a comiler warning. */ 004393 zDb = 0; /* Not needed. Silence a compiler warning. */ 004394 } 004395 004396 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 004397 /* Invoke the pre-update hook, if any */ 004398 if( db->xPreUpdateCallback 004399 && pOp->p4type==P4_TABLE 004400 && !(pOp->p5 & OPFLAG_ISUPDATE) 004401 ){ 004402 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2); 004403 } 004404 #endif 004405 004406 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; 004407 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = x.nKey; 004408 if( pData->flags & MEM_Null ){ 004409 x.pData = 0; 004410 x.nData = 0; 004411 }else{ 004412 assert( pData->flags & (MEM_Blob|MEM_Str) ); 004413 x.pData = pData->z; 004414 x.nData = pData->n; 004415 } 004416 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); 004417 if( pData->flags & MEM_Zero ){ 004418 x.nZero = pData->u.nZero; 004419 }else{ 004420 x.nZero = 0; 004421 } 004422 x.pKey = 0; 004423 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, 004424 (pOp->p5 & OPFLAG_APPEND)!=0, seekResult 004425 ); 004426 pC->deferredMoveto = 0; 004427 pC->cacheStatus = CACHE_STALE; 004428 004429 /* Invoke the update-hook if required. */ 004430 if( rc ) goto abort_due_to_error; 004431 if( db->xUpdateCallback && op ){ 004432 db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, x.nKey); 004433 } 004434 break; 004435 } 004436 004437 /* Opcode: Delete P1 P2 P3 P4 P5 004438 ** 004439 ** Delete the record at which the P1 cursor is currently pointing. 004440 ** 004441 ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then 004442 ** the cursor will be left pointing at either the next or the previous 004443 ** record in the table. If it is left pointing at the next record, then 004444 ** the next Next instruction will be a no-op. As a result, in this case 004445 ** it is ok to delete a record from within a Next loop. If 004446 ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be 004447 ** left in an undefined state. 004448 ** 004449 ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this 004450 ** delete one of several associated with deleting a table row and all its 004451 ** associated index entries. Exactly one of those deletes is the "primary" 004452 ** delete. The others are all on OPFLAG_FORDELETE cursors or else are 004453 ** marked with the AUXDELETE flag. 004454 ** 004455 ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row 004456 ** change count is incremented (otherwise not). 004457 ** 004458 ** P1 must not be pseudo-table. It has to be a real table with 004459 ** multiple rows. 004460 ** 004461 ** If P4 is not NULL then it points to a Table object. In this case either 004462 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must 004463 ** have been positioned using OP_NotFound prior to invoking this opcode in 004464 ** this case. Specifically, if one is configured, the pre-update hook is 004465 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured, 004466 ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2. 004467 ** 004468 ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address 004469 ** of the memory cell that contains the value that the rowid of the row will 004470 ** be set to by the update. 004471 */ 004472 case OP_Delete: { 004473 VdbeCursor *pC; 004474 const char *zDb; 004475 Table *pTab; 004476 int opflags; 004477 004478 opflags = pOp->p2; 004479 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004480 pC = p->apCsr[pOp->p1]; 004481 assert( pC!=0 ); 004482 assert( pC->eCurType==CURTYPE_BTREE ); 004483 assert( pC->uc.pCursor!=0 ); 004484 assert( pC->deferredMoveto==0 ); 004485 004486 #ifdef SQLITE_DEBUG 004487 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){ 004488 /* If p5 is zero, the seek operation that positioned the cursor prior to 004489 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of 004490 ** the row that is being deleted */ 004491 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor); 004492 assert( pC->movetoTarget==iKey ); 004493 } 004494 #endif 004495 004496 /* If the update-hook or pre-update-hook will be invoked, set zDb to 004497 ** the name of the db to pass as to it. Also set local pTab to a copy 004498 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was 004499 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set 004500 ** VdbeCursor.movetoTarget to the current rowid. */ 004501 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ 004502 assert( pC->iDb>=0 ); 004503 assert( pOp->p4.pTab!=0 ); 004504 zDb = db->aDb[pC->iDb].zDbSName; 004505 pTab = pOp->p4.pTab; 004506 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){ 004507 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor); 004508 } 004509 }else{ 004510 zDb = 0; /* Not needed. Silence a compiler warning. */ 004511 pTab = 0; /* Not needed. Silence a compiler warning. */ 004512 } 004513 004514 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 004515 /* Invoke the pre-update-hook if required. */ 004516 if( db->xPreUpdateCallback && pOp->p4.pTab && HasRowid(pTab) ){ 004517 assert( !(opflags & OPFLAG_ISUPDATE) || (aMem[pOp->p3].flags & MEM_Int) ); 004518 sqlite3VdbePreUpdateHook(p, pC, 004519 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, 004520 zDb, pTab, pC->movetoTarget, 004521 pOp->p3 004522 ); 004523 } 004524 if( opflags & OPFLAG_ISNOOP ) break; 004525 #endif 004526 004527 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ 004528 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 ); 004529 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION ); 004530 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE ); 004531 004532 #ifdef SQLITE_DEBUG 004533 if( p->pFrame==0 ){ 004534 if( pC->isEphemeral==0 004535 && (pOp->p5 & OPFLAG_AUXDELETE)==0 004536 && (pC->wrFlag & OPFLAG_FORDELETE)==0 004537 ){ 004538 nExtraDelete++; 004539 } 004540 if( pOp->p2 & OPFLAG_NCHANGE ){ 004541 nExtraDelete--; 004542 } 004543 } 004544 #endif 004545 004546 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); 004547 pC->cacheStatus = CACHE_STALE; 004548 pC->seekResult = 0; 004549 if( rc ) goto abort_due_to_error; 004550 004551 /* Invoke the update-hook if required. */ 004552 if( opflags & OPFLAG_NCHANGE ){ 004553 p->nChange++; 004554 if( db->xUpdateCallback && HasRowid(pTab) ){ 004555 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName, 004556 pC->movetoTarget); 004557 assert( pC->iDb>=0 ); 004558 } 004559 } 004560 004561 break; 004562 } 004563 /* Opcode: ResetCount * * * * * 004564 ** 004565 ** The value of the change counter is copied to the database handle 004566 ** change counter (returned by subsequent calls to sqlite3_changes()). 004567 ** Then the VMs internal change counter resets to 0. 004568 ** This is used by trigger programs. 004569 */ 004570 case OP_ResetCount: { 004571 sqlite3VdbeSetChanges(db, p->nChange); 004572 p->nChange = 0; 004573 break; 004574 } 004575 004576 /* Opcode: SorterCompare P1 P2 P3 P4 004577 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 004578 ** 004579 ** P1 is a sorter cursor. This instruction compares a prefix of the 004580 ** record blob in register P3 against a prefix of the entry that 004581 ** the sorter cursor currently points to. Only the first P4 fields 004582 ** of r[P3] and the sorter record are compared. 004583 ** 004584 ** If either P3 or the sorter contains a NULL in one of their significant 004585 ** fields (not counting the P4 fields at the end which are ignored) then 004586 ** the comparison is assumed to be equal. 004587 ** 004588 ** Fall through to next instruction if the two records compare equal to 004589 ** each other. Jump to P2 if they are different. 004590 */ 004591 case OP_SorterCompare: { 004592 VdbeCursor *pC; 004593 int res; 004594 int nKeyCol; 004595 004596 pC = p->apCsr[pOp->p1]; 004597 assert( isSorter(pC) ); 004598 assert( pOp->p4type==P4_INT32 ); 004599 pIn3 = &aMem[pOp->p3]; 004600 nKeyCol = pOp->p4.i; 004601 res = 0; 004602 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); 004603 VdbeBranchTaken(res!=0,2); 004604 if( rc ) goto abort_due_to_error; 004605 if( res ) goto jump_to_p2; 004606 break; 004607 }; 004608 004609 /* Opcode: SorterData P1 P2 P3 * * 004610 ** Synopsis: r[P2]=data 004611 ** 004612 ** Write into register P2 the current sorter data for sorter cursor P1. 004613 ** Then clear the column header cache on cursor P3. 004614 ** 004615 ** This opcode is normally use to move a record out of the sorter and into 004616 ** a register that is the source for a pseudo-table cursor created using 004617 ** OpenPseudo. That pseudo-table cursor is the one that is identified by 004618 ** parameter P3. Clearing the P3 column cache as part of this opcode saves 004619 ** us from having to issue a separate NullRow instruction to clear that cache. 004620 */ 004621 case OP_SorterData: { 004622 VdbeCursor *pC; 004623 004624 pOut = &aMem[pOp->p2]; 004625 pC = p->apCsr[pOp->p1]; 004626 assert( isSorter(pC) ); 004627 rc = sqlite3VdbeSorterRowkey(pC, pOut); 004628 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); 004629 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004630 if( rc ) goto abort_due_to_error; 004631 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; 004632 break; 004633 } 004634 004635 /* Opcode: RowData P1 P2 * * * 004636 ** Synopsis: r[P2]=data 004637 ** 004638 ** Write into register P2 the complete row content for the row at 004639 ** which cursor P1 is currently pointing. 004640 ** There is no interpretation of the data. 004641 ** It is just copied onto the P2 register exactly as 004642 ** it is found in the database file. 004643 ** 004644 ** If cursor P1 is an index, then the content is the key of the row. 004645 ** If cursor P2 is a table, then the content extracted is the data. 004646 ** 004647 ** If the P1 cursor must be pointing to a valid row (not a NULL row) 004648 ** of a real table, not a pseudo-table. 004649 */ 004650 case OP_RowData: { 004651 VdbeCursor *pC; 004652 BtCursor *pCrsr; 004653 u32 n; 004654 004655 pOut = &aMem[pOp->p2]; 004656 memAboutToChange(p, pOut); 004657 004658 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004659 pC = p->apCsr[pOp->p1]; 004660 assert( pC!=0 ); 004661 assert( pC->eCurType==CURTYPE_BTREE ); 004662 assert( isSorter(pC)==0 ); 004663 assert( pC->nullRow==0 ); 004664 assert( pC->uc.pCursor!=0 ); 004665 pCrsr = pC->uc.pCursor; 004666 004667 /* The OP_RowData opcodes always follow OP_NotExists or 004668 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions 004669 ** that might invalidate the cursor. 004670 ** If this where not the case, on of the following assert()s 004671 ** would fail. Should this ever change (because of changes in the code 004672 ** generator) then the fix would be to insert a call to 004673 ** sqlite3VdbeCursorMoveto(). 004674 */ 004675 assert( pC->deferredMoveto==0 ); 004676 assert( sqlite3BtreeCursorIsValid(pCrsr) ); 004677 #if 0 /* Not required due to the previous to assert() statements */ 004678 rc = sqlite3VdbeCursorMoveto(pC); 004679 if( rc!=SQLITE_OK ) goto abort_due_to_error; 004680 #endif 004681 004682 n = sqlite3BtreePayloadSize(pCrsr); 004683 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ 004684 goto too_big; 004685 } 004686 testcase( n==0 ); 004687 if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){ 004688 goto no_mem; 004689 } 004690 pOut->n = n; 004691 MemSetTypeFlag(pOut, MEM_Blob); 004692 rc = sqlite3BtreePayload(pCrsr, 0, n, pOut->z); 004693 if( rc ) goto abort_due_to_error; 004694 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ 004695 UPDATE_MAX_BLOBSIZE(pOut); 004696 REGISTER_TRACE(pOp->p2, pOut); 004697 break; 004698 } 004699 004700 /* Opcode: Rowid P1 P2 * * * 004701 ** Synopsis: r[P2]=rowid 004702 ** 004703 ** Store in register P2 an integer which is the key of the table entry that 004704 ** P1 is currently point to. 004705 ** 004706 ** P1 can be either an ordinary table or a virtual table. There used to 004707 ** be a separate OP_VRowid opcode for use with virtual tables, but this 004708 ** one opcode now works for both table types. 004709 */ 004710 case OP_Rowid: { /* out2 */ 004711 VdbeCursor *pC; 004712 i64 v; 004713 sqlite3_vtab *pVtab; 004714 const sqlite3_module *pModule; 004715 004716 pOut = out2Prerelease(p, pOp); 004717 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004718 pC = p->apCsr[pOp->p1]; 004719 assert( pC!=0 ); 004720 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); 004721 if( pC->nullRow ){ 004722 pOut->flags = MEM_Null; 004723 break; 004724 }else if( pC->deferredMoveto ){ 004725 v = pC->movetoTarget; 004726 #ifndef SQLITE_OMIT_VIRTUALTABLE 004727 }else if( pC->eCurType==CURTYPE_VTAB ){ 004728 assert( pC->uc.pVCur!=0 ); 004729 pVtab = pC->uc.pVCur->pVtab; 004730 pModule = pVtab->pModule; 004731 assert( pModule->xRowid ); 004732 rc = pModule->xRowid(pC->uc.pVCur, &v); 004733 sqlite3VtabImportErrmsg(p, pVtab); 004734 if( rc ) goto abort_due_to_error; 004735 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 004736 }else{ 004737 assert( pC->eCurType==CURTYPE_BTREE ); 004738 assert( pC->uc.pCursor!=0 ); 004739 rc = sqlite3VdbeCursorRestore(pC); 004740 if( rc ) goto abort_due_to_error; 004741 if( pC->nullRow ){ 004742 pOut->flags = MEM_Null; 004743 break; 004744 } 004745 v = sqlite3BtreeIntegerKey(pC->uc.pCursor); 004746 } 004747 pOut->u.i = v; 004748 break; 004749 } 004750 004751 /* Opcode: NullRow P1 * * * * 004752 ** 004753 ** Move the cursor P1 to a null row. Any OP_Column operations 004754 ** that occur while the cursor is on the null row will always 004755 ** write a NULL. 004756 */ 004757 case OP_NullRow: { 004758 VdbeCursor *pC; 004759 004760 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004761 pC = p->apCsr[pOp->p1]; 004762 assert( pC!=0 ); 004763 pC->nullRow = 1; 004764 pC->cacheStatus = CACHE_STALE; 004765 if( pC->eCurType==CURTYPE_BTREE ){ 004766 assert( pC->uc.pCursor!=0 ); 004767 sqlite3BtreeClearCursor(pC->uc.pCursor); 004768 } 004769 break; 004770 } 004771 004772 /* Opcode: Last P1 P2 P3 * * 004773 ** 004774 ** The next use of the Rowid or Column or Prev instruction for P1 004775 ** will refer to the last entry in the database table or index. 004776 ** If the table or index is empty and P2>0, then jump immediately to P2. 004777 ** If P2 is 0 or if the table or index is not empty, fall through 004778 ** to the following instruction. 004779 ** 004780 ** This opcode leaves the cursor configured to move in reverse order, 004781 ** from the end toward the beginning. In other words, the cursor is 004782 ** configured to use Prev, not Next. 004783 ** 004784 ** If P3 is -1, then the cursor is positioned at the end of the btree 004785 ** for the purpose of appending a new entry onto the btree. In that 004786 ** case P2 must be 0. It is assumed that the cursor is used only for 004787 ** appending and so if the cursor is valid, then the cursor must already 004788 ** be pointing at the end of the btree and so no changes are made to 004789 ** the cursor. 004790 */ 004791 case OP_Last: { /* jump */ 004792 VdbeCursor *pC; 004793 BtCursor *pCrsr; 004794 int res; 004795 004796 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004797 pC = p->apCsr[pOp->p1]; 004798 assert( pC!=0 ); 004799 assert( pC->eCurType==CURTYPE_BTREE ); 004800 pCrsr = pC->uc.pCursor; 004801 res = 0; 004802 assert( pCrsr!=0 ); 004803 pC->seekResult = pOp->p3; 004804 #ifdef SQLITE_DEBUG 004805 pC->seekOp = OP_Last; 004806 #endif 004807 if( pOp->p3==0 || !sqlite3BtreeCursorIsValidNN(pCrsr) ){ 004808 rc = sqlite3BtreeLast(pCrsr, &res); 004809 pC->nullRow = (u8)res; 004810 pC->deferredMoveto = 0; 004811 pC->cacheStatus = CACHE_STALE; 004812 if( rc ) goto abort_due_to_error; 004813 if( pOp->p2>0 ){ 004814 VdbeBranchTaken(res!=0,2); 004815 if( res ) goto jump_to_p2; 004816 } 004817 }else{ 004818 assert( pOp->p2==0 ); 004819 } 004820 break; 004821 } 004822 004823 004824 /* Opcode: SorterSort P1 P2 * * * 004825 ** 004826 ** After all records have been inserted into the Sorter object 004827 ** identified by P1, invoke this opcode to actually do the sorting. 004828 ** Jump to P2 if there are no records to be sorted. 004829 ** 004830 ** This opcode is an alias for OP_Sort and OP_Rewind that is used 004831 ** for Sorter objects. 004832 */ 004833 /* Opcode: Sort P1 P2 * * * 004834 ** 004835 ** This opcode does exactly the same thing as OP_Rewind except that 004836 ** it increments an undocumented global variable used for testing. 004837 ** 004838 ** Sorting is accomplished by writing records into a sorting index, 004839 ** then rewinding that index and playing it back from beginning to 004840 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the 004841 ** rewinding so that the global variable will be incremented and 004842 ** regression tests can determine whether or not the optimizer is 004843 ** correctly optimizing out sorts. 004844 */ 004845 case OP_SorterSort: /* jump */ 004846 case OP_Sort: { /* jump */ 004847 #ifdef SQLITE_TEST 004848 sqlite3_sort_count++; 004849 sqlite3_search_count--; 004850 #endif 004851 p->aCounter[SQLITE_STMTSTATUS_SORT]++; 004852 /* Fall through into OP_Rewind */ 004853 } 004854 /* Opcode: Rewind P1 P2 * * * 004855 ** 004856 ** The next use of the Rowid or Column or Next instruction for P1 004857 ** will refer to the first entry in the database table or index. 004858 ** If the table or index is empty, jump immediately to P2. 004859 ** If the table or index is not empty, fall through to the following 004860 ** instruction. 004861 ** 004862 ** This opcode leaves the cursor configured to move in forward order, 004863 ** from the beginning toward the end. In other words, the cursor is 004864 ** configured to use Next, not Prev. 004865 */ 004866 case OP_Rewind: { /* jump */ 004867 VdbeCursor *pC; 004868 BtCursor *pCrsr; 004869 int res; 004870 004871 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004872 pC = p->apCsr[pOp->p1]; 004873 assert( pC!=0 ); 004874 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) ); 004875 res = 1; 004876 #ifdef SQLITE_DEBUG 004877 pC->seekOp = OP_Rewind; 004878 #endif 004879 if( isSorter(pC) ){ 004880 rc = sqlite3VdbeSorterRewind(pC, &res); 004881 }else{ 004882 assert( pC->eCurType==CURTYPE_BTREE ); 004883 pCrsr = pC->uc.pCursor; 004884 assert( pCrsr ); 004885 rc = sqlite3BtreeFirst(pCrsr, &res); 004886 pC->deferredMoveto = 0; 004887 pC->cacheStatus = CACHE_STALE; 004888 } 004889 if( rc ) goto abort_due_to_error; 004890 pC->nullRow = (u8)res; 004891 assert( pOp->p2>0 && pOp->p2<p->nOp ); 004892 VdbeBranchTaken(res!=0,2); 004893 if( res ) goto jump_to_p2; 004894 break; 004895 } 004896 004897 /* Opcode: Next P1 P2 P3 P4 P5 004898 ** 004899 ** Advance cursor P1 so that it points to the next key/data pair in its 004900 ** table or index. If there are no more key/value pairs then fall through 004901 ** to the following instruction. But if the cursor advance was successful, 004902 ** jump immediately to P2. 004903 ** 004904 ** The Next opcode is only valid following an SeekGT, SeekGE, or 004905 ** OP_Rewind opcode used to position the cursor. Next is not allowed 004906 ** to follow SeekLT, SeekLE, or OP_Last. 004907 ** 004908 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have 004909 ** been opened prior to this opcode or the program will segfault. 004910 ** 004911 ** The P3 value is a hint to the btree implementation. If P3==1, that 004912 ** means P1 is an SQL index and that this instruction could have been 004913 ** omitted if that index had been unique. P3 is usually 0. P3 is 004914 ** always either 0 or 1. 004915 ** 004916 ** P4 is always of type P4_ADVANCE. The function pointer points to 004917 ** sqlite3BtreeNext(). 004918 ** 004919 ** If P5 is positive and the jump is taken, then event counter 004920 ** number P5-1 in the prepared statement is incremented. 004921 ** 004922 ** See also: Prev, NextIfOpen 004923 */ 004924 /* Opcode: NextIfOpen P1 P2 P3 P4 P5 004925 ** 004926 ** This opcode works just like Next except that if cursor P1 is not 004927 ** open it behaves a no-op. 004928 */ 004929 /* Opcode: Prev P1 P2 P3 P4 P5 004930 ** 004931 ** Back up cursor P1 so that it points to the previous key/data pair in its 004932 ** table or index. If there is no previous key/value pairs then fall through 004933 ** to the following instruction. But if the cursor backup was successful, 004934 ** jump immediately to P2. 004935 ** 004936 ** 004937 ** The Prev opcode is only valid following an SeekLT, SeekLE, or 004938 ** OP_Last opcode used to position the cursor. Prev is not allowed 004939 ** to follow SeekGT, SeekGE, or OP_Rewind. 004940 ** 004941 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is 004942 ** not open then the behavior is undefined. 004943 ** 004944 ** The P3 value is a hint to the btree implementation. If P3==1, that 004945 ** means P1 is an SQL index and that this instruction could have been 004946 ** omitted if that index had been unique. P3 is usually 0. P3 is 004947 ** always either 0 or 1. 004948 ** 004949 ** P4 is always of type P4_ADVANCE. The function pointer points to 004950 ** sqlite3BtreePrevious(). 004951 ** 004952 ** If P5 is positive and the jump is taken, then event counter 004953 ** number P5-1 in the prepared statement is incremented. 004954 */ 004955 /* Opcode: PrevIfOpen P1 P2 P3 P4 P5 004956 ** 004957 ** This opcode works just like Prev except that if cursor P1 is not 004958 ** open it behaves a no-op. 004959 */ 004960 /* Opcode: SorterNext P1 P2 * * P5 004961 ** 004962 ** This opcode works just like OP_Next except that P1 must be a 004963 ** sorter object for which the OP_SorterSort opcode has been 004964 ** invoked. This opcode advances the cursor to the next sorted 004965 ** record, or jumps to P2 if there are no more sorted records. 004966 */ 004967 case OP_SorterNext: { /* jump */ 004968 VdbeCursor *pC; 004969 int res; 004970 004971 pC = p->apCsr[pOp->p1]; 004972 assert( isSorter(pC) ); 004973 res = 0; 004974 rc = sqlite3VdbeSorterNext(db, pC, &res); 004975 goto next_tail; 004976 case OP_PrevIfOpen: /* jump */ 004977 case OP_NextIfOpen: /* jump */ 004978 if( p->apCsr[pOp->p1]==0 ) break; 004979 /* Fall through */ 004980 case OP_Prev: /* jump */ 004981 case OP_Next: /* jump */ 004982 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 004983 assert( pOp->p5<ArraySize(p->aCounter) ); 004984 pC = p->apCsr[pOp->p1]; 004985 res = pOp->p3; 004986 assert( pC!=0 ); 004987 assert( pC->deferredMoveto==0 ); 004988 assert( pC->eCurType==CURTYPE_BTREE ); 004989 assert( res==0 || (res==1 && pC->isTable==0) ); 004990 testcase( res==1 ); 004991 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext ); 004992 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious ); 004993 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext ); 004994 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious); 004995 004996 /* The Next opcode is only used after SeekGT, SeekGE, and Rewind. 004997 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */ 004998 assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen 004999 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE 005000 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found); 005001 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen 005002 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE 005003 || pC->seekOp==OP_Last ); 005004 005005 rc = pOp->p4.xAdvance(pC->uc.pCursor, &res); 005006 next_tail: 005007 pC->cacheStatus = CACHE_STALE; 005008 VdbeBranchTaken(res==0,2); 005009 if( rc ) goto abort_due_to_error; 005010 if( res==0 ){ 005011 pC->nullRow = 0; 005012 p->aCounter[pOp->p5]++; 005013 #ifdef SQLITE_TEST 005014 sqlite3_search_count++; 005015 #endif 005016 goto jump_to_p2_and_check_for_interrupt; 005017 }else{ 005018 pC->nullRow = 1; 005019 } 005020 goto check_for_interrupt; 005021 } 005022 005023 /* Opcode: IdxInsert P1 P2 P3 P4 P5 005024 ** Synopsis: key=r[P2] 005025 ** 005026 ** Register P2 holds an SQL index key made using the 005027 ** MakeRecord instructions. This opcode writes that key 005028 ** into the index P1. Data for the entry is nil. 005029 ** 005030 ** If P4 is not zero, then it is the number of values in the unpacked 005031 ** key of reg(P2). In that case, P3 is the index of the first register 005032 ** for the unpacked key. The availability of the unpacked key can sometimes 005033 ** be an optimization. 005034 ** 005035 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer 005036 ** that this insert is likely to be an append. 005037 ** 005038 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is 005039 ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, 005040 ** then the change counter is unchanged. 005041 ** 005042 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might 005043 ** run faster by avoiding an unnecessary seek on cursor P1. However, 005044 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior 005045 ** seeks on the cursor or if the most recent seek used a key equivalent 005046 ** to P2. 005047 ** 005048 ** This instruction only works for indices. The equivalent instruction 005049 ** for tables is OP_Insert. 005050 */ 005051 /* Opcode: SorterInsert P1 P2 * * * 005052 ** Synopsis: key=r[P2] 005053 ** 005054 ** Register P2 holds an SQL index key made using the 005055 ** MakeRecord instructions. This opcode writes that key 005056 ** into the sorter P1. Data for the entry is nil. 005057 */ 005058 case OP_SorterInsert: /* in2 */ 005059 case OP_IdxInsert: { /* in2 */ 005060 VdbeCursor *pC; 005061 BtreePayload x; 005062 005063 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005064 pC = p->apCsr[pOp->p1]; 005065 assert( pC!=0 ); 005066 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) ); 005067 pIn2 = &aMem[pOp->p2]; 005068 assert( pIn2->flags & MEM_Blob ); 005069 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; 005070 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert ); 005071 assert( pC->isTable==0 ); 005072 rc = ExpandBlob(pIn2); 005073 if( rc ) goto abort_due_to_error; 005074 if( pOp->opcode==OP_SorterInsert ){ 005075 rc = sqlite3VdbeSorterWrite(pC, pIn2); 005076 }else{ 005077 x.nKey = pIn2->n; 005078 x.pKey = pIn2->z; 005079 x.aMem = aMem + pOp->p3; 005080 x.nMem = (u16)pOp->p4.i; 005081 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, 005082 (pOp->p5 & OPFLAG_APPEND)!=0, 005083 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) 005084 ); 005085 assert( pC->deferredMoveto==0 ); 005086 pC->cacheStatus = CACHE_STALE; 005087 } 005088 if( rc) goto abort_due_to_error; 005089 break; 005090 } 005091 005092 /* Opcode: IdxDelete P1 P2 P3 * * 005093 ** Synopsis: key=r[P2@P3] 005094 ** 005095 ** The content of P3 registers starting at register P2 form 005096 ** an unpacked index key. This opcode removes that entry from the 005097 ** index opened by cursor P1. 005098 */ 005099 case OP_IdxDelete: { 005100 VdbeCursor *pC; 005101 BtCursor *pCrsr; 005102 int res; 005103 UnpackedRecord r; 005104 005105 assert( pOp->p3>0 ); 005106 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 ); 005107 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005108 pC = p->apCsr[pOp->p1]; 005109 assert( pC!=0 ); 005110 assert( pC->eCurType==CURTYPE_BTREE ); 005111 pCrsr = pC->uc.pCursor; 005112 assert( pCrsr!=0 ); 005113 assert( pOp->p5==0 ); 005114 r.pKeyInfo = pC->pKeyInfo; 005115 r.nField = (u16)pOp->p3; 005116 r.default_rc = 0; 005117 r.aMem = &aMem[pOp->p2]; 005118 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); 005119 if( rc ) goto abort_due_to_error; 005120 if( res==0 ){ 005121 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); 005122 if( rc ) goto abort_due_to_error; 005123 } 005124 assert( pC->deferredMoveto==0 ); 005125 pC->cacheStatus = CACHE_STALE; 005126 pC->seekResult = 0; 005127 break; 005128 } 005129 005130 /* Opcode: Seek P1 * P3 P4 * 005131 ** Synopsis: Move P3 to P1.rowid 005132 ** 005133 ** P1 is an open index cursor and P3 is a cursor on the corresponding 005134 ** table. This opcode does a deferred seek of the P3 table cursor 005135 ** to the row that corresponds to the current row of P1. 005136 ** 005137 ** This is a deferred seek. Nothing actually happens until 005138 ** the cursor is used to read a record. That way, if no reads 005139 ** occur, no unnecessary I/O happens. 005140 ** 005141 ** P4 may be an array of integers (type P4_INTARRAY) containing 005142 ** one entry for each column in the P3 table. If array entry a(i) 005143 ** is non-zero, then reading column a(i)-1 from cursor P3 is 005144 ** equivalent to performing the deferred seek and then reading column i 005145 ** from P1. This information is stored in P3 and used to redirect 005146 ** reads against P3 over to P1, thus possibly avoiding the need to 005147 ** seek and read cursor P3. 005148 */ 005149 /* Opcode: IdxRowid P1 P2 * * * 005150 ** Synopsis: r[P2]=rowid 005151 ** 005152 ** Write into register P2 an integer which is the last entry in the record at 005153 ** the end of the index key pointed to by cursor P1. This integer should be 005154 ** the rowid of the table entry to which this index entry points. 005155 ** 005156 ** See also: Rowid, MakeRecord. 005157 */ 005158 case OP_Seek: 005159 case OP_IdxRowid: { /* out2 */ 005160 VdbeCursor *pC; /* The P1 index cursor */ 005161 VdbeCursor *pTabCur; /* The P2 table cursor (OP_Seek only) */ 005162 i64 rowid; /* Rowid that P1 current points to */ 005163 005164 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005165 pC = p->apCsr[pOp->p1]; 005166 assert( pC!=0 ); 005167 assert( pC->eCurType==CURTYPE_BTREE ); 005168 assert( pC->uc.pCursor!=0 ); 005169 assert( pC->isTable==0 ); 005170 assert( pC->deferredMoveto==0 ); 005171 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid ); 005172 005173 /* The IdxRowid and Seek opcodes are combined because of the commonality 005174 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */ 005175 rc = sqlite3VdbeCursorRestore(pC); 005176 005177 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted 005178 ** out from under the cursor. That will never happens for an IdxRowid 005179 ** or Seek opcode */ 005180 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; 005181 005182 if( !pC->nullRow ){ 005183 rowid = 0; /* Not needed. Only used to silence a warning. */ 005184 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid); 005185 if( rc!=SQLITE_OK ){ 005186 goto abort_due_to_error; 005187 } 005188 if( pOp->opcode==OP_Seek ){ 005189 assert( pOp->p3>=0 && pOp->p3<p->nCursor ); 005190 pTabCur = p->apCsr[pOp->p3]; 005191 assert( pTabCur!=0 ); 005192 assert( pTabCur->eCurType==CURTYPE_BTREE ); 005193 assert( pTabCur->uc.pCursor!=0 ); 005194 assert( pTabCur->isTable ); 005195 pTabCur->nullRow = 0; 005196 pTabCur->movetoTarget = rowid; 005197 pTabCur->deferredMoveto = 1; 005198 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 ); 005199 pTabCur->aAltMap = pOp->p4.ai; 005200 pTabCur->pAltCursor = pC; 005201 }else{ 005202 pOut = out2Prerelease(p, pOp); 005203 pOut->u.i = rowid; 005204 pOut->flags = MEM_Int; 005205 } 005206 }else{ 005207 assert( pOp->opcode==OP_IdxRowid ); 005208 sqlite3VdbeMemSetNull(&aMem[pOp->p2]); 005209 } 005210 break; 005211 } 005212 005213 /* Opcode: IdxGE P1 P2 P3 P4 P5 005214 ** Synopsis: key=r[P3@P4] 005215 ** 005216 ** The P4 register values beginning with P3 form an unpacked index 005217 ** key that omits the PRIMARY KEY. Compare this key value against the index 005218 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID 005219 ** fields at the end. 005220 ** 005221 ** If the P1 index entry is greater than or equal to the key value 005222 ** then jump to P2. Otherwise fall through to the next instruction. 005223 */ 005224 /* Opcode: IdxGT P1 P2 P3 P4 P5 005225 ** Synopsis: key=r[P3@P4] 005226 ** 005227 ** The P4 register values beginning with P3 form an unpacked index 005228 ** key that omits the PRIMARY KEY. Compare this key value against the index 005229 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID 005230 ** fields at the end. 005231 ** 005232 ** If the P1 index entry is greater than the key value 005233 ** then jump to P2. Otherwise fall through to the next instruction. 005234 */ 005235 /* Opcode: IdxLT P1 P2 P3 P4 P5 005236 ** Synopsis: key=r[P3@P4] 005237 ** 005238 ** The P4 register values beginning with P3 form an unpacked index 005239 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against 005240 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or 005241 ** ROWID on the P1 index. 005242 ** 005243 ** If the P1 index entry is less than the key value then jump to P2. 005244 ** Otherwise fall through to the next instruction. 005245 */ 005246 /* Opcode: IdxLE P1 P2 P3 P4 P5 005247 ** Synopsis: key=r[P3@P4] 005248 ** 005249 ** The P4 register values beginning with P3 form an unpacked index 005250 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against 005251 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or 005252 ** ROWID on the P1 index. 005253 ** 005254 ** If the P1 index entry is less than or equal to the key value then jump 005255 ** to P2. Otherwise fall through to the next instruction. 005256 */ 005257 case OP_IdxLE: /* jump */ 005258 case OP_IdxGT: /* jump */ 005259 case OP_IdxLT: /* jump */ 005260 case OP_IdxGE: { /* jump */ 005261 VdbeCursor *pC; 005262 int res; 005263 UnpackedRecord r; 005264 005265 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005266 pC = p->apCsr[pOp->p1]; 005267 assert( pC!=0 ); 005268 assert( pC->isOrdered ); 005269 assert( pC->eCurType==CURTYPE_BTREE ); 005270 assert( pC->uc.pCursor!=0); 005271 assert( pC->deferredMoveto==0 ); 005272 assert( pOp->p5==0 || pOp->p5==1 ); 005273 assert( pOp->p4type==P4_INT32 ); 005274 r.pKeyInfo = pC->pKeyInfo; 005275 r.nField = (u16)pOp->p4.i; 005276 if( pOp->opcode<OP_IdxLT ){ 005277 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); 005278 r.default_rc = -1; 005279 }else{ 005280 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); 005281 r.default_rc = 0; 005282 } 005283 r.aMem = &aMem[pOp->p3]; 005284 #ifdef SQLITE_DEBUG 005285 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } 005286 #endif 005287 res = 0; /* Not needed. Only used to silence a warning. */ 005288 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); 005289 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); 005290 if( (pOp->opcode&1)==(OP_IdxLT&1) ){ 005291 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); 005292 res = -res; 005293 }else{ 005294 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); 005295 res++; 005296 } 005297 VdbeBranchTaken(res>0,2); 005298 if( rc ) goto abort_due_to_error; 005299 if( res>0 ) goto jump_to_p2; 005300 break; 005301 } 005302 005303 /* Opcode: Destroy P1 P2 P3 * * 005304 ** 005305 ** Delete an entire database table or index whose root page in the database 005306 ** file is given by P1. 005307 ** 005308 ** The table being destroyed is in the main database file if P3==0. If 005309 ** P3==1 then the table to be clear is in the auxiliary database file 005310 ** that is used to store tables create using CREATE TEMPORARY TABLE. 005311 ** 005312 ** If AUTOVACUUM is enabled then it is possible that another root page 005313 ** might be moved into the newly deleted root page in order to keep all 005314 ** root pages contiguous at the beginning of the database. The former 005315 ** value of the root page that moved - its value before the move occurred - 005316 ** is stored in register P2. If no page 005317 ** movement was required (because the table being dropped was already 005318 ** the last one in the database) then a zero is stored in register P2. 005319 ** If AUTOVACUUM is disabled then a zero is stored in register P2. 005320 ** 005321 ** See also: Clear 005322 */ 005323 case OP_Destroy: { /* out2 */ 005324 int iMoved; 005325 int iDb; 005326 005327 assert( p->readOnly==0 ); 005328 assert( pOp->p1>1 ); 005329 pOut = out2Prerelease(p, pOp); 005330 pOut->flags = MEM_Null; 005331 if( db->nVdbeRead > db->nVDestroy+1 ){ 005332 rc = SQLITE_LOCKED; 005333 p->errorAction = OE_Abort; 005334 goto abort_due_to_error; 005335 }else{ 005336 iDb = pOp->p3; 005337 assert( DbMaskTest(p->btreeMask, iDb) ); 005338 iMoved = 0; /* Not needed. Only to silence a warning. */ 005339 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); 005340 pOut->flags = MEM_Int; 005341 pOut->u.i = iMoved; 005342 if( rc ) goto abort_due_to_error; 005343 #ifndef SQLITE_OMIT_AUTOVACUUM 005344 if( iMoved!=0 ){ 005345 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); 005346 /* All OP_Destroy operations occur on the same btree */ 005347 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); 005348 resetSchemaOnFault = iDb+1; 005349 } 005350 #endif 005351 } 005352 break; 005353 } 005354 005355 /* Opcode: Clear P1 P2 P3 005356 ** 005357 ** Delete all contents of the database table or index whose root page 005358 ** in the database file is given by P1. But, unlike Destroy, do not 005359 ** remove the table or index from the database file. 005360 ** 005361 ** The table being clear is in the main database file if P2==0. If 005362 ** P2==1 then the table to be clear is in the auxiliary database file 005363 ** that is used to store tables create using CREATE TEMPORARY TABLE. 005364 ** 005365 ** If the P3 value is non-zero, then the table referred to must be an 005366 ** intkey table (an SQL table, not an index). In this case the row change 005367 ** count is incremented by the number of rows in the table being cleared. 005368 ** If P3 is greater than zero, then the value stored in register P3 is 005369 ** also incremented by the number of rows in the table being cleared. 005370 ** 005371 ** See also: Destroy 005372 */ 005373 case OP_Clear: { 005374 int nChange; 005375 005376 nChange = 0; 005377 assert( p->readOnly==0 ); 005378 assert( DbMaskTest(p->btreeMask, pOp->p2) ); 005379 rc = sqlite3BtreeClearTable( 005380 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) 005381 ); 005382 if( pOp->p3 ){ 005383 p->nChange += nChange; 005384 if( pOp->p3>0 ){ 005385 assert( memIsValid(&aMem[pOp->p3]) ); 005386 memAboutToChange(p, &aMem[pOp->p3]); 005387 aMem[pOp->p3].u.i += nChange; 005388 } 005389 } 005390 if( rc ) goto abort_due_to_error; 005391 break; 005392 } 005393 005394 /* Opcode: ResetSorter P1 * * * * 005395 ** 005396 ** Delete all contents from the ephemeral table or sorter 005397 ** that is open on cursor P1. 005398 ** 005399 ** This opcode only works for cursors used for sorting and 005400 ** opened with OP_OpenEphemeral or OP_SorterOpen. 005401 */ 005402 case OP_ResetSorter: { 005403 VdbeCursor *pC; 005404 005405 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 005406 pC = p->apCsr[pOp->p1]; 005407 assert( pC!=0 ); 005408 if( isSorter(pC) ){ 005409 sqlite3VdbeSorterReset(db, pC->uc.pSorter); 005410 }else{ 005411 assert( pC->eCurType==CURTYPE_BTREE ); 005412 assert( pC->isEphemeral ); 005413 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor); 005414 if( rc ) goto abort_due_to_error; 005415 } 005416 break; 005417 } 005418 005419 /* Opcode: CreateTable P1 P2 * * * 005420 ** Synopsis: r[P2]=root iDb=P1 005421 ** 005422 ** Allocate a new table in the main database file if P1==0 or in the 005423 ** auxiliary database file if P1==1 or in an attached database if 005424 ** P1>1. Write the root page number of the new table into 005425 ** register P2 005426 ** 005427 ** The difference between a table and an index is this: A table must 005428 ** have a 4-byte integer key and can have arbitrary data. An index 005429 ** has an arbitrary key but no data. 005430 ** 005431 ** See also: CreateIndex 005432 */ 005433 /* Opcode: CreateIndex P1 P2 * * * 005434 ** Synopsis: r[P2]=root iDb=P1 005435 ** 005436 ** Allocate a new index in the main database file if P1==0 or in the 005437 ** auxiliary database file if P1==1 or in an attached database if 005438 ** P1>1. Write the root page number of the new table into 005439 ** register P2. 005440 ** 005441 ** See documentation on OP_CreateTable for additional information. 005442 */ 005443 case OP_CreateIndex: /* out2 */ 005444 case OP_CreateTable: { /* out2 */ 005445 int pgno; 005446 int flags; 005447 Db *pDb; 005448 005449 pOut = out2Prerelease(p, pOp); 005450 pgno = 0; 005451 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 005452 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 005453 assert( p->readOnly==0 ); 005454 pDb = &db->aDb[pOp->p1]; 005455 assert( pDb->pBt!=0 ); 005456 if( pOp->opcode==OP_CreateTable ){ 005457 /* flags = BTREE_INTKEY; */ 005458 flags = BTREE_INTKEY; 005459 }else{ 005460 flags = BTREE_BLOBKEY; 005461 } 005462 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); 005463 if( rc ) goto abort_due_to_error; 005464 pOut->u.i = pgno; 005465 break; 005466 } 005467 005468 /* Opcode: ParseSchema P1 * * P4 * 005469 ** 005470 ** Read and parse all entries from the SQLITE_MASTER table of database P1 005471 ** that match the WHERE clause P4. 005472 ** 005473 ** This opcode invokes the parser to create a new virtual machine, 005474 ** then runs the new virtual machine. It is thus a re-entrant opcode. 005475 */ 005476 case OP_ParseSchema: { 005477 int iDb; 005478 const char *zMaster; 005479 char *zSql; 005480 InitData initData; 005481 005482 /* Any prepared statement that invokes this opcode will hold mutexes 005483 ** on every btree. This is a prerequisite for invoking 005484 ** sqlite3InitCallback(). 005485 */ 005486 #ifdef SQLITE_DEBUG 005487 for(iDb=0; iDb<db->nDb; iDb++){ 005488 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); 005489 } 005490 #endif 005491 005492 iDb = pOp->p1; 005493 assert( iDb>=0 && iDb<db->nDb ); 005494 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) ); 005495 /* Used to be a conditional */ { 005496 zMaster = MASTER_NAME; 005497 initData.db = db; 005498 initData.iDb = pOp->p1; 005499 initData.pzErrMsg = &p->zErrMsg; 005500 zSql = sqlite3MPrintf(db, 005501 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid", 005502 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z); 005503 if( zSql==0 ){ 005504 rc = SQLITE_NOMEM_BKPT; 005505 }else{ 005506 assert( db->init.busy==0 ); 005507 db->init.busy = 1; 005508 initData.rc = SQLITE_OK; 005509 assert( !db->mallocFailed ); 005510 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); 005511 if( rc==SQLITE_OK ) rc = initData.rc; 005512 sqlite3DbFree(db, zSql); 005513 db->init.busy = 0; 005514 } 005515 } 005516 if( rc ){ 005517 sqlite3ResetAllSchemasOfConnection(db); 005518 if( rc==SQLITE_NOMEM ){ 005519 goto no_mem; 005520 } 005521 goto abort_due_to_error; 005522 } 005523 break; 005524 } 005525 005526 #if !defined(SQLITE_OMIT_ANALYZE) 005527 /* Opcode: LoadAnalysis P1 * * * * 005528 ** 005529 ** Read the sqlite_stat1 table for database P1 and load the content 005530 ** of that table into the internal index hash table. This will cause 005531 ** the analysis to be used when preparing all subsequent queries. 005532 */ 005533 case OP_LoadAnalysis: { 005534 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 005535 rc = sqlite3AnalysisLoad(db, pOp->p1); 005536 if( rc ) goto abort_due_to_error; 005537 break; 005538 } 005539 #endif /* !defined(SQLITE_OMIT_ANALYZE) */ 005540 005541 /* Opcode: DropTable P1 * * P4 * 005542 ** 005543 ** Remove the internal (in-memory) data structures that describe 005544 ** the table named P4 in database P1. This is called after a table 005545 ** is dropped from disk (using the Destroy opcode) in order to keep 005546 ** the internal representation of the 005547 ** schema consistent with what is on disk. 005548 */ 005549 case OP_DropTable: { 005550 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); 005551 break; 005552 } 005553 005554 /* Opcode: DropIndex P1 * * P4 * 005555 ** 005556 ** Remove the internal (in-memory) data structures that describe 005557 ** the index named P4 in database P1. This is called after an index 005558 ** is dropped from disk (using the Destroy opcode) 005559 ** in order to keep the internal representation of the 005560 ** schema consistent with what is on disk. 005561 */ 005562 case OP_DropIndex: { 005563 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); 005564 break; 005565 } 005566 005567 /* Opcode: DropTrigger P1 * * P4 * 005568 ** 005569 ** Remove the internal (in-memory) data structures that describe 005570 ** the trigger named P4 in database P1. This is called after a trigger 005571 ** is dropped from disk (using the Destroy opcode) in order to keep 005572 ** the internal representation of the 005573 ** schema consistent with what is on disk. 005574 */ 005575 case OP_DropTrigger: { 005576 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); 005577 break; 005578 } 005579 005580 005581 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 005582 /* Opcode: IntegrityCk P1 P2 P3 P4 P5 005583 ** 005584 ** Do an analysis of the currently open database. Store in 005585 ** register P1 the text of an error message describing any problems. 005586 ** If no problems are found, store a NULL in register P1. 005587 ** 005588 ** The register P3 contains the maximum number of allowed errors. 005589 ** At most reg(P3) errors will be reported. 005590 ** In other words, the analysis stops as soon as reg(P1) errors are 005591 ** seen. Reg(P1) is updated with the number of errors remaining. 005592 ** 005593 ** The root page numbers of all tables in the database are integers 005594 ** stored in P4_INTARRAY argument. 005595 ** 005596 ** If P5 is not zero, the check is done on the auxiliary database 005597 ** file, not the main database file. 005598 ** 005599 ** This opcode is used to implement the integrity_check pragma. 005600 */ 005601 case OP_IntegrityCk: { 005602 int nRoot; /* Number of tables to check. (Number of root pages.) */ 005603 int *aRoot; /* Array of rootpage numbers for tables to be checked */ 005604 int nErr; /* Number of errors reported */ 005605 char *z; /* Text of the error report */ 005606 Mem *pnErr; /* Register keeping track of errors remaining */ 005607 005608 assert( p->bIsReader ); 005609 nRoot = pOp->p2; 005610 aRoot = pOp->p4.ai; 005611 assert( nRoot>0 ); 005612 assert( aRoot[nRoot]==0 ); 005613 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 005614 pnErr = &aMem[pOp->p3]; 005615 assert( (pnErr->flags & MEM_Int)!=0 ); 005616 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); 005617 pIn1 = &aMem[pOp->p1]; 005618 assert( pOp->p5<db->nDb ); 005619 assert( DbMaskTest(p->btreeMask, pOp->p5) ); 005620 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, 005621 (int)pnErr->u.i, &nErr); 005622 pnErr->u.i -= nErr; 005623 sqlite3VdbeMemSetNull(pIn1); 005624 if( nErr==0 ){ 005625 assert( z==0 ); 005626 }else if( z==0 ){ 005627 goto no_mem; 005628 }else{ 005629 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); 005630 } 005631 UPDATE_MAX_BLOBSIZE(pIn1); 005632 sqlite3VdbeChangeEncoding(pIn1, encoding); 005633 break; 005634 } 005635 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 005636 005637 /* Opcode: RowSetAdd P1 P2 * * * 005638 ** Synopsis: rowset(P1)=r[P2] 005639 ** 005640 ** Insert the integer value held by register P2 into a boolean index 005641 ** held in register P1. 005642 ** 005643 ** An assertion fails if P2 is not an integer. 005644 */ 005645 case OP_RowSetAdd: { /* in1, in2 */ 005646 pIn1 = &aMem[pOp->p1]; 005647 pIn2 = &aMem[pOp->p2]; 005648 assert( (pIn2->flags & MEM_Int)!=0 ); 005649 if( (pIn1->flags & MEM_RowSet)==0 ){ 005650 sqlite3VdbeMemSetRowSet(pIn1); 005651 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; 005652 } 005653 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i); 005654 break; 005655 } 005656 005657 /* Opcode: RowSetRead P1 P2 P3 * * 005658 ** Synopsis: r[P3]=rowset(P1) 005659 ** 005660 ** Extract the smallest value from boolean index P1 and put that value into 005661 ** register P3. Or, if boolean index P1 is initially empty, leave P3 005662 ** unchanged and jump to instruction P2. 005663 */ 005664 case OP_RowSetRead: { /* jump, in1, out3 */ 005665 i64 val; 005666 005667 pIn1 = &aMem[pOp->p1]; 005668 if( (pIn1->flags & MEM_RowSet)==0 005669 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0 005670 ){ 005671 /* The boolean index is empty */ 005672 sqlite3VdbeMemSetNull(pIn1); 005673 VdbeBranchTaken(1,2); 005674 goto jump_to_p2_and_check_for_interrupt; 005675 }else{ 005676 /* A value was pulled from the index */ 005677 VdbeBranchTaken(0,2); 005678 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); 005679 } 005680 goto check_for_interrupt; 005681 } 005682 005683 /* Opcode: RowSetTest P1 P2 P3 P4 005684 ** Synopsis: if r[P3] in rowset(P1) goto P2 005685 ** 005686 ** Register P3 is assumed to hold a 64-bit integer value. If register P1 005687 ** contains a RowSet object and that RowSet object contains 005688 ** the value held in P3, jump to register P2. Otherwise, insert the 005689 ** integer in P3 into the RowSet and continue on to the 005690 ** next opcode. 005691 ** 005692 ** The RowSet object is optimized for the case where successive sets 005693 ** of integers, where each set contains no duplicates. Each set 005694 ** of values is identified by a unique P4 value. The first set 005695 ** must have P4==0, the final set P4=-1. P4 must be either -1 or 005696 ** non-negative. For non-negative values of P4 only the lower 4 005697 ** bits are significant. 005698 ** 005699 ** This allows optimizations: (a) when P4==0 there is no need to test 005700 ** the rowset object for P3, as it is guaranteed not to contain it, 005701 ** (b) when P4==-1 there is no need to insert the value, as it will 005702 ** never be tested for, and (c) when a value that is part of set X is 005703 ** inserted, there is no need to search to see if the same value was 005704 ** previously inserted as part of set X (only if it was previously 005705 ** inserted as part of some other set). 005706 */ 005707 case OP_RowSetTest: { /* jump, in1, in3 */ 005708 int iSet; 005709 int exists; 005710 005711 pIn1 = &aMem[pOp->p1]; 005712 pIn3 = &aMem[pOp->p3]; 005713 iSet = pOp->p4.i; 005714 assert( pIn3->flags&MEM_Int ); 005715 005716 /* If there is anything other than a rowset object in memory cell P1, 005717 ** delete it now and initialize P1 with an empty rowset 005718 */ 005719 if( (pIn1->flags & MEM_RowSet)==0 ){ 005720 sqlite3VdbeMemSetRowSet(pIn1); 005721 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; 005722 } 005723 005724 assert( pOp->p4type==P4_INT32 ); 005725 assert( iSet==-1 || iSet>=0 ); 005726 if( iSet ){ 005727 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i); 005728 VdbeBranchTaken(exists!=0,2); 005729 if( exists ) goto jump_to_p2; 005730 } 005731 if( iSet>=0 ){ 005732 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i); 005733 } 005734 break; 005735 } 005736 005737 005738 #ifndef SQLITE_OMIT_TRIGGER 005739 005740 /* Opcode: Program P1 P2 P3 P4 P5 005741 ** 005742 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). 005743 ** 005744 ** P1 contains the address of the memory cell that contains the first memory 005745 ** cell in an array of values used as arguments to the sub-program. P2 005746 ** contains the address to jump to if the sub-program throws an IGNORE 005747 ** exception using the RAISE() function. Register P3 contains the address 005748 ** of a memory cell in this (the parent) VM that is used to allocate the 005749 ** memory required by the sub-vdbe at runtime. 005750 ** 005751 ** P4 is a pointer to the VM containing the trigger program. 005752 ** 005753 ** If P5 is non-zero, then recursive program invocation is enabled. 005754 */ 005755 case OP_Program: { /* jump */ 005756 int nMem; /* Number of memory registers for sub-program */ 005757 int nByte; /* Bytes of runtime space required for sub-program */ 005758 Mem *pRt; /* Register to allocate runtime space */ 005759 Mem *pMem; /* Used to iterate through memory cells */ 005760 Mem *pEnd; /* Last memory cell in new array */ 005761 VdbeFrame *pFrame; /* New vdbe frame to execute in */ 005762 SubProgram *pProgram; /* Sub-program to execute */ 005763 void *t; /* Token identifying trigger */ 005764 005765 pProgram = pOp->p4.pProgram; 005766 pRt = &aMem[pOp->p3]; 005767 assert( pProgram->nOp>0 ); 005768 005769 /* If the p5 flag is clear, then recursive invocation of triggers is 005770 ** disabled for backwards compatibility (p5 is set if this sub-program 005771 ** is really a trigger, not a foreign key action, and the flag set 005772 ** and cleared by the "PRAGMA recursive_triggers" command is clear). 005773 ** 005774 ** It is recursive invocation of triggers, at the SQL level, that is 005775 ** disabled. In some cases a single trigger may generate more than one 005776 ** SubProgram (if the trigger may be executed with more than one different 005777 ** ON CONFLICT algorithm). SubProgram structures associated with a 005778 ** single trigger all have the same value for the SubProgram.token 005779 ** variable. */ 005780 if( pOp->p5 ){ 005781 t = pProgram->token; 005782 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); 005783 if( pFrame ) break; 005784 } 005785 005786 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ 005787 rc = SQLITE_ERROR; 005788 sqlite3VdbeError(p, "too many levels of trigger recursion"); 005789 goto abort_due_to_error; 005790 } 005791 005792 /* Register pRt is used to store the memory required to save the state 005793 ** of the current program, and the memory required at runtime to execute 005794 ** the trigger program. If this trigger has been fired before, then pRt 005795 ** is already allocated. Otherwise, it must be initialized. */ 005796 if( (pRt->flags&MEM_Frame)==0 ){ 005797 /* SubProgram.nMem is set to the number of memory cells used by the 005798 ** program stored in SubProgram.aOp. As well as these, one memory 005799 ** cell is required for each cursor used by the program. Set local 005800 ** variable nMem (and later, VdbeFrame.nChildMem) to this value. 005801 */ 005802 nMem = pProgram->nMem + pProgram->nCsr; 005803 assert( nMem>0 ); 005804 if( pProgram->nCsr==0 ) nMem++; 005805 nByte = ROUND8(sizeof(VdbeFrame)) 005806 + nMem * sizeof(Mem) 005807 + pProgram->nCsr * sizeof(VdbeCursor *); 005808 pFrame = sqlite3DbMallocZero(db, nByte); 005809 if( !pFrame ){ 005810 goto no_mem; 005811 } 005812 sqlite3VdbeMemRelease(pRt); 005813 pRt->flags = MEM_Frame; 005814 pRt->u.pFrame = pFrame; 005815 005816 pFrame->v = p; 005817 pFrame->nChildMem = nMem; 005818 pFrame->nChildCsr = pProgram->nCsr; 005819 pFrame->pc = (int)(pOp - aOp); 005820 pFrame->aMem = p->aMem; 005821 pFrame->nMem = p->nMem; 005822 pFrame->apCsr = p->apCsr; 005823 pFrame->nCursor = p->nCursor; 005824 pFrame->aOp = p->aOp; 005825 pFrame->nOp = p->nOp; 005826 pFrame->token = pProgram->token; 005827 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 005828 pFrame->anExec = p->anExec; 005829 #endif 005830 005831 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; 005832 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ 005833 pMem->flags = MEM_Undefined; 005834 pMem->db = db; 005835 } 005836 }else{ 005837 pFrame = pRt->u.pFrame; 005838 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem 005839 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) ); 005840 assert( pProgram->nCsr==pFrame->nChildCsr ); 005841 assert( (int)(pOp - aOp)==pFrame->pc ); 005842 } 005843 005844 p->nFrame++; 005845 pFrame->pParent = p->pFrame; 005846 pFrame->lastRowid = lastRowid; 005847 pFrame->nChange = p->nChange; 005848 pFrame->nDbChange = p->db->nChange; 005849 assert( pFrame->pAuxData==0 ); 005850 pFrame->pAuxData = p->pAuxData; 005851 p->pAuxData = 0; 005852 p->nChange = 0; 005853 p->pFrame = pFrame; 005854 p->aMem = aMem = VdbeFrameMem(pFrame); 005855 p->nMem = pFrame->nChildMem; 005856 p->nCursor = (u16)pFrame->nChildCsr; 005857 p->apCsr = (VdbeCursor **)&aMem[p->nMem]; 005858 p->aOp = aOp = pProgram->aOp; 005859 p->nOp = pProgram->nOp; 005860 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 005861 p->anExec = 0; 005862 #endif 005863 pOp = &aOp[-1]; 005864 005865 break; 005866 } 005867 005868 /* Opcode: Param P1 P2 * * * 005869 ** 005870 ** This opcode is only ever present in sub-programs called via the 005871 ** OP_Program instruction. Copy a value currently stored in a memory 005872 ** cell of the calling (parent) frame to cell P2 in the current frames 005873 ** address space. This is used by trigger programs to access the new.* 005874 ** and old.* values. 005875 ** 005876 ** The address of the cell in the parent frame is determined by adding 005877 ** the value of the P1 argument to the value of the P1 argument to the 005878 ** calling OP_Program instruction. 005879 */ 005880 case OP_Param: { /* out2 */ 005881 VdbeFrame *pFrame; 005882 Mem *pIn; 005883 pOut = out2Prerelease(p, pOp); 005884 pFrame = p->pFrame; 005885 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; 005886 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); 005887 break; 005888 } 005889 005890 #endif /* #ifndef SQLITE_OMIT_TRIGGER */ 005891 005892 #ifndef SQLITE_OMIT_FOREIGN_KEY 005893 /* Opcode: FkCounter P1 P2 * * * 005894 ** Synopsis: fkctr[P1]+=P2 005895 ** 005896 ** Increment a "constraint counter" by P2 (P2 may be negative or positive). 005897 ** If P1 is non-zero, the database constraint counter is incremented 005898 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the 005899 ** statement counter is incremented (immediate foreign key constraints). 005900 */ 005901 case OP_FkCounter: { 005902 if( db->flags & SQLITE_DeferFKs ){ 005903 db->nDeferredImmCons += pOp->p2; 005904 }else if( pOp->p1 ){ 005905 db->nDeferredCons += pOp->p2; 005906 }else{ 005907 p->nFkConstraint += pOp->p2; 005908 } 005909 break; 005910 } 005911 005912 /* Opcode: FkIfZero P1 P2 * * * 005913 ** Synopsis: if fkctr[P1]==0 goto P2 005914 ** 005915 ** This opcode tests if a foreign key constraint-counter is currently zero. 005916 ** If so, jump to instruction P2. Otherwise, fall through to the next 005917 ** instruction. 005918 ** 005919 ** If P1 is non-zero, then the jump is taken if the database constraint-counter 005920 ** is zero (the one that counts deferred constraint violations). If P1 is 005921 ** zero, the jump is taken if the statement constraint-counter is zero 005922 ** (immediate foreign key constraint violations). 005923 */ 005924 case OP_FkIfZero: { /* jump */ 005925 if( pOp->p1 ){ 005926 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2); 005927 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; 005928 }else{ 005929 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2); 005930 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; 005931 } 005932 break; 005933 } 005934 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ 005935 005936 #ifndef SQLITE_OMIT_AUTOINCREMENT 005937 /* Opcode: MemMax P1 P2 * * * 005938 ** Synopsis: r[P1]=max(r[P1],r[P2]) 005939 ** 005940 ** P1 is a register in the root frame of this VM (the root frame is 005941 ** different from the current frame if this instruction is being executed 005942 ** within a sub-program). Set the value of register P1 to the maximum of 005943 ** its current value and the value in register P2. 005944 ** 005945 ** This instruction throws an error if the memory cell is not initially 005946 ** an integer. 005947 */ 005948 case OP_MemMax: { /* in2 */ 005949 VdbeFrame *pFrame; 005950 if( p->pFrame ){ 005951 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); 005952 pIn1 = &pFrame->aMem[pOp->p1]; 005953 }else{ 005954 pIn1 = &aMem[pOp->p1]; 005955 } 005956 assert( memIsValid(pIn1) ); 005957 sqlite3VdbeMemIntegerify(pIn1); 005958 pIn2 = &aMem[pOp->p2]; 005959 sqlite3VdbeMemIntegerify(pIn2); 005960 if( pIn1->u.i<pIn2->u.i){ 005961 pIn1->u.i = pIn2->u.i; 005962 } 005963 break; 005964 } 005965 #endif /* SQLITE_OMIT_AUTOINCREMENT */ 005966 005967 /* Opcode: IfPos P1 P2 P3 * * 005968 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 005969 ** 005970 ** Register P1 must contain an integer. 005971 ** If the value of register P1 is 1 or greater, subtract P3 from the 005972 ** value in P1 and jump to P2. 005973 ** 005974 ** If the initial value of register P1 is less than 1, then the 005975 ** value is unchanged and control passes through to the next instruction. 005976 */ 005977 case OP_IfPos: { /* jump, in1 */ 005978 pIn1 = &aMem[pOp->p1]; 005979 assert( pIn1->flags&MEM_Int ); 005980 VdbeBranchTaken( pIn1->u.i>0, 2); 005981 if( pIn1->u.i>0 ){ 005982 pIn1->u.i -= pOp->p3; 005983 goto jump_to_p2; 005984 } 005985 break; 005986 } 005987 005988 /* Opcode: OffsetLimit P1 P2 P3 * * 005989 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) 005990 ** 005991 ** This opcode performs a commonly used computation associated with 005992 ** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3] 005993 ** holds the offset counter. The opcode computes the combined value 005994 ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] 005995 ** value computed is the total number of rows that will need to be 005996 ** visited in order to complete the query. 005997 ** 005998 ** If r[P3] is zero or negative, that means there is no OFFSET 005999 ** and r[P2] is set to be the value of the LIMIT, r[P1]. 006000 ** 006001 ** if r[P1] is zero or negative, that means there is no LIMIT 006002 ** and r[P2] is set to -1. 006003 ** 006004 ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3]. 006005 */ 006006 case OP_OffsetLimit: { /* in1, out2, in3 */ 006007 i64 x; 006008 pIn1 = &aMem[pOp->p1]; 006009 pIn3 = &aMem[pOp->p3]; 006010 pOut = out2Prerelease(p, pOp); 006011 assert( pIn1->flags & MEM_Int ); 006012 assert( pIn3->flags & MEM_Int ); 006013 x = pIn1->u.i; 006014 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){ 006015 /* If the LIMIT is less than or equal to zero, loop forever. This 006016 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then 006017 ** also loop forever. This is undocumented. In fact, one could argue 006018 ** that the loop should terminate. But assuming 1 billion iterations 006019 ** per second (far exceeding the capabilities of any current hardware) 006020 ** it would take nearly 300 years to actually reach the limit. So 006021 ** looping forever is a reasonable approximation. */ 006022 pOut->u.i = -1; 006023 }else{ 006024 pOut->u.i = x; 006025 } 006026 break; 006027 } 006028 006029 /* Opcode: IfNotZero P1 P2 * * * 006030 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2 006031 ** 006032 ** Register P1 must contain an integer. If the content of register P1 is 006033 ** initially greater than zero, then decrement the value in register P1. 006034 ** If it is non-zero (negative or positive) and then also jump to P2. 006035 ** If register P1 is initially zero, leave it unchanged and fall through. 006036 */ 006037 case OP_IfNotZero: { /* jump, in1 */ 006038 pIn1 = &aMem[pOp->p1]; 006039 assert( pIn1->flags&MEM_Int ); 006040 VdbeBranchTaken(pIn1->u.i<0, 2); 006041 if( pIn1->u.i ){ 006042 if( pIn1->u.i>0 ) pIn1->u.i--; 006043 goto jump_to_p2; 006044 } 006045 break; 006046 } 006047 006048 /* Opcode: DecrJumpZero P1 P2 * * * 006049 ** Synopsis: if (--r[P1])==0 goto P2 006050 ** 006051 ** Register P1 must hold an integer. Decrement the value in P1 006052 ** and jump to P2 if the new value is exactly zero. 006053 */ 006054 case OP_DecrJumpZero: { /* jump, in1 */ 006055 pIn1 = &aMem[pOp->p1]; 006056 assert( pIn1->flags&MEM_Int ); 006057 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--; 006058 VdbeBranchTaken(pIn1->u.i==0, 2); 006059 if( pIn1->u.i==0 ) goto jump_to_p2; 006060 break; 006061 } 006062 006063 006064 /* Opcode: AggStep0 * P2 P3 P4 P5 006065 ** Synopsis: accum=r[P3] step(r[P2@P5]) 006066 ** 006067 ** Execute the step function for an aggregate. The 006068 ** function has P5 arguments. P4 is a pointer to the FuncDef 006069 ** structure that specifies the function. Register P3 is the 006070 ** accumulator. 006071 ** 006072 ** The P5 arguments are taken from register P2 and its 006073 ** successors. 006074 */ 006075 /* Opcode: AggStep * P2 P3 P4 P5 006076 ** Synopsis: accum=r[P3] step(r[P2@P5]) 006077 ** 006078 ** Execute the step function for an aggregate. The 006079 ** function has P5 arguments. P4 is a pointer to an sqlite3_context 006080 ** object that is used to run the function. Register P3 is 006081 ** as the accumulator. 006082 ** 006083 ** The P5 arguments are taken from register P2 and its 006084 ** successors. 006085 ** 006086 ** This opcode is initially coded as OP_AggStep0. On first evaluation, 006087 ** the FuncDef stored in P4 is converted into an sqlite3_context and 006088 ** the opcode is changed. In this way, the initialization of the 006089 ** sqlite3_context only happens once, instead of on each call to the 006090 ** step function. 006091 */ 006092 case OP_AggStep0: { 006093 int n; 006094 sqlite3_context *pCtx; 006095 006096 assert( pOp->p4type==P4_FUNCDEF ); 006097 n = pOp->p5; 006098 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 006099 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) ); 006100 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); 006101 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); 006102 if( pCtx==0 ) goto no_mem; 006103 pCtx->pMem = 0; 006104 pCtx->pFunc = pOp->p4.pFunc; 006105 pCtx->iOp = (int)(pOp - aOp); 006106 pCtx->pVdbe = p; 006107 pCtx->argc = n; 006108 pOp->p4type = P4_FUNCCTX; 006109 pOp->p4.pCtx = pCtx; 006110 pOp->opcode = OP_AggStep; 006111 /* Fall through into OP_AggStep */ 006112 } 006113 case OP_AggStep: { 006114 int i; 006115 sqlite3_context *pCtx; 006116 Mem *pMem; 006117 Mem t; 006118 006119 assert( pOp->p4type==P4_FUNCCTX ); 006120 pCtx = pOp->p4.pCtx; 006121 pMem = &aMem[pOp->p3]; 006122 006123 /* If this function is inside of a trigger, the register array in aMem[] 006124 ** might change from one evaluation to the next. The next block of code 006125 ** checks to see if the register array has changed, and if so it 006126 ** reinitializes the relavant parts of the sqlite3_context object */ 006127 if( pCtx->pMem != pMem ){ 006128 pCtx->pMem = pMem; 006129 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; 006130 } 006131 006132 #ifdef SQLITE_DEBUG 006133 for(i=0; i<pCtx->argc; i++){ 006134 assert( memIsValid(pCtx->argv[i]) ); 006135 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); 006136 } 006137 #endif 006138 006139 pMem->n++; 006140 sqlite3VdbeMemInit(&t, db, MEM_Null); 006141 pCtx->pOut = &t; 006142 pCtx->fErrorOrAux = 0; 006143 pCtx->skipFlag = 0; 006144 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ 006145 if( pCtx->fErrorOrAux ){ 006146 if( pCtx->isError ){ 006147 sqlite3VdbeError(p, "%s", sqlite3_value_text(&t)); 006148 rc = pCtx->isError; 006149 } 006150 sqlite3VdbeMemRelease(&t); 006151 if( rc ) goto abort_due_to_error; 006152 }else{ 006153 assert( t.flags==MEM_Null ); 006154 } 006155 if( pCtx->skipFlag ){ 006156 assert( pOp[-1].opcode==OP_CollSeq ); 006157 i = pOp[-1].p1; 006158 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); 006159 } 006160 break; 006161 } 006162 006163 /* Opcode: AggFinal P1 P2 * P4 * 006164 ** Synopsis: accum=r[P1] N=P2 006165 ** 006166 ** Execute the finalizer function for an aggregate. P1 is 006167 ** the memory location that is the accumulator for the aggregate. 006168 ** 006169 ** P2 is the number of arguments that the step function takes and 006170 ** P4 is a pointer to the FuncDef for this function. The P2 006171 ** argument is not used by this opcode. It is only there to disambiguate 006172 ** functions that can take varying numbers of arguments. The 006173 ** P4 argument is only needed for the degenerate case where 006174 ** the step function was not previously called. 006175 */ 006176 case OP_AggFinal: { 006177 Mem *pMem; 006178 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); 006179 pMem = &aMem[pOp->p1]; 006180 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); 006181 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); 006182 if( rc ){ 006183 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); 006184 goto abort_due_to_error; 006185 } 006186 sqlite3VdbeChangeEncoding(pMem, encoding); 006187 UPDATE_MAX_BLOBSIZE(pMem); 006188 if( sqlite3VdbeMemTooBig(pMem) ){ 006189 goto too_big; 006190 } 006191 break; 006192 } 006193 006194 #ifndef SQLITE_OMIT_WAL 006195 /* Opcode: Checkpoint P1 P2 P3 * * 006196 ** 006197 ** Checkpoint database P1. This is a no-op if P1 is not currently in 006198 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL, 006199 ** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns 006200 ** SQLITE_BUSY or not, respectively. Write the number of pages in the 006201 ** WAL after the checkpoint into mem[P3+1] and the number of pages 006202 ** in the WAL that have been checkpointed after the checkpoint 006203 ** completes into mem[P3+2]. However on an error, mem[P3+1] and 006204 ** mem[P3+2] are initialized to -1. 006205 */ 006206 case OP_Checkpoint: { 006207 int i; /* Loop counter */ 006208 int aRes[3]; /* Results */ 006209 Mem *pMem; /* Write results here */ 006210 006211 assert( p->readOnly==0 ); 006212 aRes[0] = 0; 006213 aRes[1] = aRes[2] = -1; 006214 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE 006215 || pOp->p2==SQLITE_CHECKPOINT_FULL 006216 || pOp->p2==SQLITE_CHECKPOINT_RESTART 006217 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE 006218 ); 006219 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); 006220 if( rc ){ 006221 if( rc!=SQLITE_BUSY ) goto abort_due_to_error; 006222 rc = SQLITE_OK; 006223 aRes[0] = 1; 006224 } 006225 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ 006226 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); 006227 } 006228 break; 006229 }; 006230 #endif 006231 006232 #ifndef SQLITE_OMIT_PRAGMA 006233 /* Opcode: JournalMode P1 P2 P3 * * 006234 ** 006235 ** Change the journal mode of database P1 to P3. P3 must be one of the 006236 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback 006237 ** modes (delete, truncate, persist, off and memory), this is a simple 006238 ** operation. No IO is required. 006239 ** 006240 ** If changing into or out of WAL mode the procedure is more complicated. 006241 ** 006242 ** Write a string containing the final journal-mode to register P2. 006243 */ 006244 case OP_JournalMode: { /* out2 */ 006245 Btree *pBt; /* Btree to change journal mode of */ 006246 Pager *pPager; /* Pager associated with pBt */ 006247 int eNew; /* New journal mode */ 006248 int eOld; /* The old journal mode */ 006249 #ifndef SQLITE_OMIT_WAL 006250 const char *zFilename; /* Name of database file for pPager */ 006251 #endif 006252 006253 pOut = out2Prerelease(p, pOp); 006254 eNew = pOp->p3; 006255 assert( eNew==PAGER_JOURNALMODE_DELETE 006256 || eNew==PAGER_JOURNALMODE_TRUNCATE 006257 || eNew==PAGER_JOURNALMODE_PERSIST 006258 || eNew==PAGER_JOURNALMODE_OFF 006259 || eNew==PAGER_JOURNALMODE_MEMORY 006260 || eNew==PAGER_JOURNALMODE_WAL 006261 || eNew==PAGER_JOURNALMODE_QUERY 006262 ); 006263 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 006264 assert( p->readOnly==0 ); 006265 006266 pBt = db->aDb[pOp->p1].pBt; 006267 pPager = sqlite3BtreePager(pBt); 006268 eOld = sqlite3PagerGetJournalMode(pPager); 006269 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; 006270 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; 006271 006272 #ifndef SQLITE_OMIT_WAL 006273 zFilename = sqlite3PagerFilename(pPager, 1); 006274 006275 /* Do not allow a transition to journal_mode=WAL for a database 006276 ** in temporary storage or if the VFS does not support shared memory 006277 */ 006278 if( eNew==PAGER_JOURNALMODE_WAL 006279 && (sqlite3Strlen30(zFilename)==0 /* Temp file */ 006280 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ 006281 ){ 006282 eNew = eOld; 006283 } 006284 006285 if( (eNew!=eOld) 006286 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) 006287 ){ 006288 if( !db->autoCommit || db->nVdbeRead>1 ){ 006289 rc = SQLITE_ERROR; 006290 sqlite3VdbeError(p, 006291 "cannot change %s wal mode from within a transaction", 006292 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") 006293 ); 006294 goto abort_due_to_error; 006295 }else{ 006296 006297 if( eOld==PAGER_JOURNALMODE_WAL ){ 006298 /* If leaving WAL mode, close the log file. If successful, the call 006299 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log 006300 ** file. An EXCLUSIVE lock may still be held on the database file 006301 ** after a successful return. 006302 */ 006303 rc = sqlite3PagerCloseWal(pPager, db); 006304 if( rc==SQLITE_OK ){ 006305 sqlite3PagerSetJournalMode(pPager, eNew); 006306 } 006307 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ 006308 /* Cannot transition directly from MEMORY to WAL. Use mode OFF 006309 ** as an intermediate */ 006310 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); 006311 } 006312 006313 /* Open a transaction on the database file. Regardless of the journal 006314 ** mode, this transaction always uses a rollback journal. 006315 */ 006316 assert( sqlite3BtreeIsInTrans(pBt)==0 ); 006317 if( rc==SQLITE_OK ){ 006318 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); 006319 } 006320 } 006321 } 006322 #endif /* ifndef SQLITE_OMIT_WAL */ 006323 006324 if( rc ) eNew = eOld; 006325 eNew = sqlite3PagerSetJournalMode(pPager, eNew); 006326 006327 pOut->flags = MEM_Str|MEM_Static|MEM_Term; 006328 pOut->z = (char *)sqlite3JournalModename(eNew); 006329 pOut->n = sqlite3Strlen30(pOut->z); 006330 pOut->enc = SQLITE_UTF8; 006331 sqlite3VdbeChangeEncoding(pOut, encoding); 006332 if( rc ) goto abort_due_to_error; 006333 break; 006334 }; 006335 #endif /* SQLITE_OMIT_PRAGMA */ 006336 006337 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) 006338 /* Opcode: Vacuum P1 * * * * 006339 ** 006340 ** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more 006341 ** for an attached database. The "temp" database may not be vacuumed. 006342 */ 006343 case OP_Vacuum: { 006344 assert( p->readOnly==0 ); 006345 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1); 006346 if( rc ) goto abort_due_to_error; 006347 break; 006348 } 006349 #endif 006350 006351 #if !defined(SQLITE_OMIT_AUTOVACUUM) 006352 /* Opcode: IncrVacuum P1 P2 * * * 006353 ** 006354 ** Perform a single step of the incremental vacuum procedure on 006355 ** the P1 database. If the vacuum has finished, jump to instruction 006356 ** P2. Otherwise, fall through to the next instruction. 006357 */ 006358 case OP_IncrVacuum: { /* jump */ 006359 Btree *pBt; 006360 006361 assert( pOp->p1>=0 && pOp->p1<db->nDb ); 006362 assert( DbMaskTest(p->btreeMask, pOp->p1) ); 006363 assert( p->readOnly==0 ); 006364 pBt = db->aDb[pOp->p1].pBt; 006365 rc = sqlite3BtreeIncrVacuum(pBt); 006366 VdbeBranchTaken(rc==SQLITE_DONE,2); 006367 if( rc ){ 006368 if( rc!=SQLITE_DONE ) goto abort_due_to_error; 006369 rc = SQLITE_OK; 006370 goto jump_to_p2; 006371 } 006372 break; 006373 } 006374 #endif 006375 006376 /* Opcode: Expire P1 * * * * 006377 ** 006378 ** Cause precompiled statements to expire. When an expired statement 006379 ** is executed using sqlite3_step() it will either automatically 006380 ** reprepare itself (if it was originally created using sqlite3_prepare_v2()) 006381 ** or it will fail with SQLITE_SCHEMA. 006382 ** 006383 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, 006384 ** then only the currently executing statement is expired. 006385 */ 006386 case OP_Expire: { 006387 if( !pOp->p1 ){ 006388 sqlite3ExpirePreparedStatements(db); 006389 }else{ 006390 p->expired = 1; 006391 } 006392 break; 006393 } 006394 006395 #ifndef SQLITE_OMIT_SHARED_CACHE 006396 /* Opcode: TableLock P1 P2 P3 P4 * 006397 ** Synopsis: iDb=P1 root=P2 write=P3 006398 ** 006399 ** Obtain a lock on a particular table. This instruction is only used when 006400 ** the shared-cache feature is enabled. 006401 ** 006402 ** P1 is the index of the database in sqlite3.aDb[] of the database 006403 ** on which the lock is acquired. A readlock is obtained if P3==0 or 006404 ** a write lock if P3==1. 006405 ** 006406 ** P2 contains the root-page of the table to lock. 006407 ** 006408 ** P4 contains a pointer to the name of the table being locked. This is only 006409 ** used to generate an error message if the lock cannot be obtained. 006410 */ 006411 case OP_TableLock: { 006412 u8 isWriteLock = (u8)pOp->p3; 006413 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){ 006414 int p1 = pOp->p1; 006415 assert( p1>=0 && p1<db->nDb ); 006416 assert( DbMaskTest(p->btreeMask, p1) ); 006417 assert( isWriteLock==0 || isWriteLock==1 ); 006418 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); 006419 if( rc ){ 006420 if( (rc&0xFF)==SQLITE_LOCKED ){ 006421 const char *z = pOp->p4.z; 006422 sqlite3VdbeError(p, "database table is locked: %s", z); 006423 } 006424 goto abort_due_to_error; 006425 } 006426 } 006427 break; 006428 } 006429 #endif /* SQLITE_OMIT_SHARED_CACHE */ 006430 006431 #ifndef SQLITE_OMIT_VIRTUALTABLE 006432 /* Opcode: VBegin * * * P4 * 006433 ** 006434 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the 006435 ** xBegin method for that table. 006436 ** 006437 ** Also, whether or not P4 is set, check that this is not being called from 006438 ** within a callback to a virtual table xSync() method. If it is, the error 006439 ** code will be set to SQLITE_LOCKED. 006440 */ 006441 case OP_VBegin: { 006442 VTable *pVTab; 006443 pVTab = pOp->p4.pVtab; 006444 rc = sqlite3VtabBegin(db, pVTab); 006445 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); 006446 if( rc ) goto abort_due_to_error; 006447 break; 006448 } 006449 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 006450 006451 #ifndef SQLITE_OMIT_VIRTUALTABLE 006452 /* Opcode: VCreate P1 P2 * * * 006453 ** 006454 ** P2 is a register that holds the name of a virtual table in database 006455 ** P1. Call the xCreate method for that table. 006456 */ 006457 case OP_VCreate: { 006458 Mem sMem; /* For storing the record being decoded */ 006459 const char *zTab; /* Name of the virtual table */ 006460 006461 memset(&sMem, 0, sizeof(sMem)); 006462 sMem.db = db; 006463 /* Because P2 is always a static string, it is impossible for the 006464 ** sqlite3VdbeMemCopy() to fail */ 006465 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 ); 006466 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 ); 006467 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]); 006468 assert( rc==SQLITE_OK ); 006469 zTab = (const char*)sqlite3_value_text(&sMem); 006470 assert( zTab || db->mallocFailed ); 006471 if( zTab ){ 006472 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); 006473 } 006474 sqlite3VdbeMemRelease(&sMem); 006475 if( rc ) goto abort_due_to_error; 006476 break; 006477 } 006478 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 006479 006480 #ifndef SQLITE_OMIT_VIRTUALTABLE 006481 /* Opcode: VDestroy P1 * * P4 * 006482 ** 006483 ** P4 is the name of a virtual table in database P1. Call the xDestroy method 006484 ** of that table. 006485 */ 006486 case OP_VDestroy: { 006487 db->nVDestroy++; 006488 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); 006489 db->nVDestroy--; 006490 if( rc ) goto abort_due_to_error; 006491 break; 006492 } 006493 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 006494 006495 #ifndef SQLITE_OMIT_VIRTUALTABLE 006496 /* Opcode: VOpen P1 * * P4 * 006497 ** 006498 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. 006499 ** P1 is a cursor number. This opcode opens a cursor to the virtual 006500 ** table and stores that cursor in P1. 006501 */ 006502 case OP_VOpen: { 006503 VdbeCursor *pCur; 006504 sqlite3_vtab_cursor *pVCur; 006505 sqlite3_vtab *pVtab; 006506 const sqlite3_module *pModule; 006507 006508 assert( p->bIsReader ); 006509 pCur = 0; 006510 pVCur = 0; 006511 pVtab = pOp->p4.pVtab->pVtab; 006512 if( pVtab==0 || NEVER(pVtab->pModule==0) ){ 006513 rc = SQLITE_LOCKED; 006514 goto abort_due_to_error; 006515 } 006516 pModule = pVtab->pModule; 006517 rc = pModule->xOpen(pVtab, &pVCur); 006518 sqlite3VtabImportErrmsg(p, pVtab); 006519 if( rc ) goto abort_due_to_error; 006520 006521 /* Initialize sqlite3_vtab_cursor base class */ 006522 pVCur->pVtab = pVtab; 006523 006524 /* Initialize vdbe cursor object */ 006525 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB); 006526 if( pCur ){ 006527 pCur->uc.pVCur = pVCur; 006528 pVtab->nRef++; 006529 }else{ 006530 assert( db->mallocFailed ); 006531 pModule->xClose(pVCur); 006532 goto no_mem; 006533 } 006534 break; 006535 } 006536 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 006537 006538 #ifndef SQLITE_OMIT_VIRTUALTABLE 006539 /* Opcode: VFilter P1 P2 P3 P4 * 006540 ** Synopsis: iplan=r[P3] zplan='P4' 006541 ** 006542 ** P1 is a cursor opened using VOpen. P2 is an address to jump to if 006543 ** the filtered result set is empty. 006544 ** 006545 ** P4 is either NULL or a string that was generated by the xBestIndex 006546 ** method of the module. The interpretation of the P4 string is left 006547 ** to the module implementation. 006548 ** 006549 ** This opcode invokes the xFilter method on the virtual table specified 006550 ** by P1. The integer query plan parameter to xFilter is stored in register 006551 ** P3. Register P3+1 stores the argc parameter to be passed to the 006552 ** xFilter method. Registers P3+2..P3+1+argc are the argc 006553 ** additional parameters which are passed to 006554 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. 006555 ** 006556 ** A jump is made to P2 if the result set after filtering would be empty. 006557 */ 006558 case OP_VFilter: { /* jump */ 006559 int nArg; 006560 int iQuery; 006561 const sqlite3_module *pModule; 006562 Mem *pQuery; 006563 Mem *pArgc; 006564 sqlite3_vtab_cursor *pVCur; 006565 sqlite3_vtab *pVtab; 006566 VdbeCursor *pCur; 006567 int res; 006568 int i; 006569 Mem **apArg; 006570 006571 pQuery = &aMem[pOp->p3]; 006572 pArgc = &pQuery[1]; 006573 pCur = p->apCsr[pOp->p1]; 006574 assert( memIsValid(pQuery) ); 006575 REGISTER_TRACE(pOp->p3, pQuery); 006576 assert( pCur->eCurType==CURTYPE_VTAB ); 006577 pVCur = pCur->uc.pVCur; 006578 pVtab = pVCur->pVtab; 006579 pModule = pVtab->pModule; 006580 006581 /* Grab the index number and argc parameters */ 006582 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); 006583 nArg = (int)pArgc->u.i; 006584 iQuery = (int)pQuery->u.i; 006585 006586 /* Invoke the xFilter method */ 006587 res = 0; 006588 apArg = p->apArg; 006589 for(i = 0; i<nArg; i++){ 006590 apArg[i] = &pArgc[i+1]; 006591 } 006592 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg); 006593 sqlite3VtabImportErrmsg(p, pVtab); 006594 if( rc ) goto abort_due_to_error; 006595 res = pModule->xEof(pVCur); 006596 pCur->nullRow = 0; 006597 VdbeBranchTaken(res!=0,2); 006598 if( res ) goto jump_to_p2; 006599 break; 006600 } 006601 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 006602 006603 #ifndef SQLITE_OMIT_VIRTUALTABLE 006604 /* Opcode: VColumn P1 P2 P3 * * 006605 ** Synopsis: r[P3]=vcolumn(P2) 006606 ** 006607 ** Store the value of the P2-th column of 006608 ** the row of the virtual-table that the 006609 ** P1 cursor is pointing to into register P3. 006610 */ 006611 case OP_VColumn: { 006612 sqlite3_vtab *pVtab; 006613 const sqlite3_module *pModule; 006614 Mem *pDest; 006615 sqlite3_context sContext; 006616 006617 VdbeCursor *pCur = p->apCsr[pOp->p1]; 006618 assert( pCur->eCurType==CURTYPE_VTAB ); 006619 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); 006620 pDest = &aMem[pOp->p3]; 006621 memAboutToChange(p, pDest); 006622 if( pCur->nullRow ){ 006623 sqlite3VdbeMemSetNull(pDest); 006624 break; 006625 } 006626 pVtab = pCur->uc.pVCur->pVtab; 006627 pModule = pVtab->pModule; 006628 assert( pModule->xColumn ); 006629 memset(&sContext, 0, sizeof(sContext)); 006630 sContext.pOut = pDest; 006631 MemSetTypeFlag(pDest, MEM_Null); 006632 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2); 006633 sqlite3VtabImportErrmsg(p, pVtab); 006634 if( sContext.isError ){ 006635 rc = sContext.isError; 006636 } 006637 sqlite3VdbeChangeEncoding(pDest, encoding); 006638 REGISTER_TRACE(pOp->p3, pDest); 006639 UPDATE_MAX_BLOBSIZE(pDest); 006640 006641 if( sqlite3VdbeMemTooBig(pDest) ){ 006642 goto too_big; 006643 } 006644 if( rc ) goto abort_due_to_error; 006645 break; 006646 } 006647 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 006648 006649 #ifndef SQLITE_OMIT_VIRTUALTABLE 006650 /* Opcode: VNext P1 P2 * * * 006651 ** 006652 ** Advance virtual table P1 to the next row in its result set and 006653 ** jump to instruction P2. Or, if the virtual table has reached 006654 ** the end of its result set, then fall through to the next instruction. 006655 */ 006656 case OP_VNext: { /* jump */ 006657 sqlite3_vtab *pVtab; 006658 const sqlite3_module *pModule; 006659 int res; 006660 VdbeCursor *pCur; 006661 006662 res = 0; 006663 pCur = p->apCsr[pOp->p1]; 006664 assert( pCur->eCurType==CURTYPE_VTAB ); 006665 if( pCur->nullRow ){ 006666 break; 006667 } 006668 pVtab = pCur->uc.pVCur->pVtab; 006669 pModule = pVtab->pModule; 006670 assert( pModule->xNext ); 006671 006672 /* Invoke the xNext() method of the module. There is no way for the 006673 ** underlying implementation to return an error if one occurs during 006674 ** xNext(). Instead, if an error occurs, true is returned (indicating that 006675 ** data is available) and the error code returned when xColumn or 006676 ** some other method is next invoked on the save virtual table cursor. 006677 */ 006678 rc = pModule->xNext(pCur->uc.pVCur); 006679 sqlite3VtabImportErrmsg(p, pVtab); 006680 if( rc ) goto abort_due_to_error; 006681 res = pModule->xEof(pCur->uc.pVCur); 006682 VdbeBranchTaken(!res,2); 006683 if( !res ){ 006684 /* If there is data, jump to P2 */ 006685 goto jump_to_p2_and_check_for_interrupt; 006686 } 006687 goto check_for_interrupt; 006688 } 006689 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 006690 006691 #ifndef SQLITE_OMIT_VIRTUALTABLE 006692 /* Opcode: VRename P1 * * P4 * 006693 ** 006694 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. 006695 ** This opcode invokes the corresponding xRename method. The value 006696 ** in register P1 is passed as the zName argument to the xRename method. 006697 */ 006698 case OP_VRename: { 006699 sqlite3_vtab *pVtab; 006700 Mem *pName; 006701 006702 pVtab = pOp->p4.pVtab->pVtab; 006703 pName = &aMem[pOp->p1]; 006704 assert( pVtab->pModule->xRename ); 006705 assert( memIsValid(pName) ); 006706 assert( p->readOnly==0 ); 006707 REGISTER_TRACE(pOp->p1, pName); 006708 assert( pName->flags & MEM_Str ); 006709 testcase( pName->enc==SQLITE_UTF8 ); 006710 testcase( pName->enc==SQLITE_UTF16BE ); 006711 testcase( pName->enc==SQLITE_UTF16LE ); 006712 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); 006713 if( rc ) goto abort_due_to_error; 006714 rc = pVtab->pModule->xRename(pVtab, pName->z); 006715 sqlite3VtabImportErrmsg(p, pVtab); 006716 p->expired = 0; 006717 if( rc ) goto abort_due_to_error; 006718 break; 006719 } 006720 #endif 006721 006722 #ifndef SQLITE_OMIT_VIRTUALTABLE 006723 /* Opcode: VUpdate P1 P2 P3 P4 P5 006724 ** Synopsis: data=r[P3@P2] 006725 ** 006726 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. 006727 ** This opcode invokes the corresponding xUpdate method. P2 values 006728 ** are contiguous memory cells starting at P3 to pass to the xUpdate 006729 ** invocation. The value in register (P3+P2-1) corresponds to the 006730 ** p2th element of the argv array passed to xUpdate. 006731 ** 006732 ** The xUpdate method will do a DELETE or an INSERT or both. 006733 ** The argv[0] element (which corresponds to memory cell P3) 006734 ** is the rowid of a row to delete. If argv[0] is NULL then no 006735 ** deletion occurs. The argv[1] element is the rowid of the new 006736 ** row. This can be NULL to have the virtual table select the new 006737 ** rowid for itself. The subsequent elements in the array are 006738 ** the values of columns in the new row. 006739 ** 006740 ** If P2==1 then no insert is performed. argv[0] is the rowid of 006741 ** a row to delete. 006742 ** 006743 ** P1 is a boolean flag. If it is set to true and the xUpdate call 006744 ** is successful, then the value returned by sqlite3_last_insert_rowid() 006745 ** is set to the value of the rowid for the row just inserted. 006746 ** 006747 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to 006748 ** apply in the case of a constraint failure on an insert or update. 006749 */ 006750 case OP_VUpdate: { 006751 sqlite3_vtab *pVtab; 006752 const sqlite3_module *pModule; 006753 int nArg; 006754 int i; 006755 sqlite_int64 rowid; 006756 Mem **apArg; 006757 Mem *pX; 006758 006759 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback 006760 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace 006761 ); 006762 assert( p->readOnly==0 ); 006763 pVtab = pOp->p4.pVtab->pVtab; 006764 if( pVtab==0 || NEVER(pVtab->pModule==0) ){ 006765 rc = SQLITE_LOCKED; 006766 goto abort_due_to_error; 006767 } 006768 pModule = pVtab->pModule; 006769 nArg = pOp->p2; 006770 assert( pOp->p4type==P4_VTAB ); 006771 if( ALWAYS(pModule->xUpdate) ){ 006772 u8 vtabOnConflict = db->vtabOnConflict; 006773 apArg = p->apArg; 006774 pX = &aMem[pOp->p3]; 006775 for(i=0; i<nArg; i++){ 006776 assert( memIsValid(pX) ); 006777 memAboutToChange(p, pX); 006778 apArg[i] = pX; 006779 pX++; 006780 } 006781 db->vtabOnConflict = pOp->p5; 006782 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); 006783 db->vtabOnConflict = vtabOnConflict; 006784 sqlite3VtabImportErrmsg(p, pVtab); 006785 if( rc==SQLITE_OK && pOp->p1 ){ 006786 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); 006787 db->lastRowid = lastRowid = rowid; 006788 } 006789 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ 006790 if( pOp->p5==OE_Ignore ){ 006791 rc = SQLITE_OK; 006792 }else{ 006793 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); 006794 } 006795 }else{ 006796 p->nChange++; 006797 } 006798 if( rc ) goto abort_due_to_error; 006799 } 006800 break; 006801 } 006802 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 006803 006804 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 006805 /* Opcode: Pagecount P1 P2 * * * 006806 ** 006807 ** Write the current number of pages in database P1 to memory cell P2. 006808 */ 006809 case OP_Pagecount: { /* out2 */ 006810 pOut = out2Prerelease(p, pOp); 006811 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); 006812 break; 006813 } 006814 #endif 006815 006816 006817 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 006818 /* Opcode: MaxPgcnt P1 P2 P3 * * 006819 ** 006820 ** Try to set the maximum page count for database P1 to the value in P3. 006821 ** Do not let the maximum page count fall below the current page count and 006822 ** do not change the maximum page count value if P3==0. 006823 ** 006824 ** Store the maximum page count after the change in register P2. 006825 */ 006826 case OP_MaxPgcnt: { /* out2 */ 006827 unsigned int newMax; 006828 Btree *pBt; 006829 006830 pOut = out2Prerelease(p, pOp); 006831 pBt = db->aDb[pOp->p1].pBt; 006832 newMax = 0; 006833 if( pOp->p3 ){ 006834 newMax = sqlite3BtreeLastPage(pBt); 006835 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; 006836 } 006837 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); 006838 break; 006839 } 006840 #endif 006841 006842 006843 /* Opcode: Init P1 P2 * P4 * 006844 ** Synopsis: Start at P2 006845 ** 006846 ** Programs contain a single instance of this opcode as the very first 006847 ** opcode. 006848 ** 006849 ** If tracing is enabled (by the sqlite3_trace()) interface, then 006850 ** the UTF-8 string contained in P4 is emitted on the trace callback. 006851 ** Or if P4 is blank, use the string returned by sqlite3_sql(). 006852 ** 006853 ** If P2 is not zero, jump to instruction P2. 006854 ** 006855 ** Increment the value of P1 so that OP_Once opcodes will jump the 006856 ** first time they are evaluated for this run. 006857 */ 006858 case OP_Init: { /* jump */ 006859 char *zTrace; 006860 int i; 006861 006862 /* If the P4 argument is not NULL, then it must be an SQL comment string. 006863 ** The "--" string is broken up to prevent false-positives with srcck1.c. 006864 ** 006865 ** This assert() provides evidence for: 006866 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that 006867 ** would have been returned by the legacy sqlite3_trace() interface by 006868 ** using the X argument when X begins with "--" and invoking 006869 ** sqlite3_expanded_sql(P) otherwise. 006870 */ 006871 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 ); 006872 assert( pOp==p->aOp ); /* Always instruction 0 */ 006873 006874 #ifndef SQLITE_OMIT_TRACE 006875 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0 006876 && !p->doingRerun 006877 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 006878 ){ 006879 #ifndef SQLITE_OMIT_DEPRECATED 006880 if( db->mTrace & SQLITE_TRACE_LEGACY ){ 006881 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace; 006882 char *z = sqlite3VdbeExpandSql(p, zTrace); 006883 x(db->pTraceArg, z); 006884 sqlite3_free(z); 006885 }else 006886 #endif 006887 { 006888 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace); 006889 } 006890 } 006891 #ifdef SQLITE_USE_FCNTL_TRACE 006892 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); 006893 if( zTrace ){ 006894 int j; 006895 for(j=0; j<db->nDb; j++){ 006896 if( DbMaskTest(p->btreeMask, j)==0 ) continue; 006897 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace); 006898 } 006899 } 006900 #endif /* SQLITE_USE_FCNTL_TRACE */ 006901 #ifdef SQLITE_DEBUG 006902 if( (db->flags & SQLITE_SqlTrace)!=0 006903 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 006904 ){ 006905 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); 006906 } 006907 #endif /* SQLITE_DEBUG */ 006908 #endif /* SQLITE_OMIT_TRACE */ 006909 assert( pOp->p2>0 ); 006910 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){ 006911 for(i=1; i<p->nOp; i++){ 006912 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0; 006913 } 006914 pOp->p1 = 0; 006915 } 006916 pOp->p1++; 006917 goto jump_to_p2; 006918 } 006919 006920 #ifdef SQLITE_ENABLE_CURSOR_HINTS 006921 /* Opcode: CursorHint P1 * * P4 * 006922 ** 006923 ** Provide a hint to cursor P1 that it only needs to return rows that 006924 ** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer 006925 ** to values currently held in registers. TK_COLUMN terms in the P4 006926 ** expression refer to columns in the b-tree to which cursor P1 is pointing. 006927 */ 006928 case OP_CursorHint: { 006929 VdbeCursor *pC; 006930 006931 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); 006932 assert( pOp->p4type==P4_EXPR ); 006933 pC = p->apCsr[pOp->p1]; 006934 if( pC ){ 006935 assert( pC->eCurType==CURTYPE_BTREE ); 006936 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE, 006937 pOp->p4.pExpr, aMem); 006938 } 006939 break; 006940 } 006941 #endif /* SQLITE_ENABLE_CURSOR_HINTS */ 006942 006943 /* Opcode: Noop * * * * * 006944 ** 006945 ** Do nothing. This instruction is often useful as a jump 006946 ** destination. 006947 */ 006948 /* 006949 ** The magic Explain opcode are only inserted when explain==2 (which 006950 ** is to say when the EXPLAIN QUERY PLAN syntax is used.) 006951 ** This opcode records information from the optimizer. It is the 006952 ** the same as a no-op. This opcodesnever appears in a real VM program. 006953 */ 006954 default: { /* This is really OP_Noop and OP_Explain */ 006955 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); 006956 break; 006957 } 006958 006959 /***************************************************************************** 006960 ** The cases of the switch statement above this line should all be indented 006961 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the 006962 ** readability. From this point on down, the normal indentation rules are 006963 ** restored. 006964 *****************************************************************************/ 006965 } 006966 006967 #ifdef VDBE_PROFILE 006968 { 006969 u64 endTime = sqlite3Hwtime(); 006970 if( endTime>start ) pOrigOp->cycles += endTime - start; 006971 pOrigOp->cnt++; 006972 } 006973 #endif 006974 006975 /* The following code adds nothing to the actual functionality 006976 ** of the program. It is only here for testing and debugging. 006977 ** On the other hand, it does burn CPU cycles every time through 006978 ** the evaluator loop. So we can leave it out when NDEBUG is defined. 006979 */ 006980 #ifndef NDEBUG 006981 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] ); 006982 006983 #ifdef SQLITE_DEBUG 006984 if( db->flags & SQLITE_VdbeTrace ){ 006985 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode]; 006986 if( rc!=0 ) printf("rc=%d\n",rc); 006987 if( opProperty & (OPFLG_OUT2) ){ 006988 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); 006989 } 006990 if( opProperty & OPFLG_OUT3 ){ 006991 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); 006992 } 006993 } 006994 #endif /* SQLITE_DEBUG */ 006995 #endif /* NDEBUG */ 006996 } /* The end of the for(;;) loop the loops through opcodes */ 006997 006998 /* If we reach this point, it means that execution is finished with 006999 ** an error of some kind. 007000 */ 007001 abort_due_to_error: 007002 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT; 007003 assert( rc ); 007004 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){ 007005 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); 007006 } 007007 p->rc = rc; 007008 sqlite3SystemError(db, rc); 007009 testcase( sqlite3GlobalConfig.xLog!=0 ); 007010 sqlite3_log(rc, "statement aborts at %d: [%s] %s", 007011 (int)(pOp - aOp), p->zSql, p->zErrMsg); 007012 sqlite3VdbeHalt(p); 007013 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db); 007014 rc = SQLITE_ERROR; 007015 if( resetSchemaOnFault>0 ){ 007016 sqlite3ResetOneSchema(db, resetSchemaOnFault-1); 007017 } 007018 007019 /* This is the only way out of this procedure. We have to 007020 ** release the mutexes on btrees that were acquired at the 007021 ** top. */ 007022 vdbe_return: 007023 db->lastRowid = lastRowid; 007024 testcase( nVmStep>0 ); 007025 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; 007026 sqlite3VdbeLeave(p); 007027 assert( rc!=SQLITE_OK || nExtraDelete==0 007028 || sqlite3_strlike("DELETE%",p->zSql,0)!=0 007029 ); 007030 return rc; 007031 007032 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH 007033 ** is encountered. 007034 */ 007035 too_big: 007036 sqlite3VdbeError(p, "string or blob too big"); 007037 rc = SQLITE_TOOBIG; 007038 goto abort_due_to_error; 007039 007040 /* Jump to here if a malloc() fails. 007041 */ 007042 no_mem: 007043 sqlite3OomFault(db); 007044 sqlite3VdbeError(p, "out of memory"); 007045 rc = SQLITE_NOMEM_BKPT; 007046 goto abort_due_to_error; 007047 007048 /* Jump to here if the sqlite3_interrupt() API sets the interrupt 007049 ** flag. 007050 */ 007051 abort_due_to_interrupt: 007052 assert( db->u1.isInterrupted ); 007053 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT; 007054 p->rc = rc; 007055 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); 007056 goto abort_due_to_error; 007057 }