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