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