1 /* 2 ** 2003 September 6 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 ** This file contains code used for creating, destroying, and populating 13 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior 14 ** to version 2.8.7, all this code was combined into the vdbe.c source file. 15 ** But that file was getting too big so this subroutines were split out. 16 */ 17 #include "sqliteInt.h" 18 #include <ctype.h> 19 #include "vdbeInt.h" 20 21 22 23 /* 24 ** When debugging the code generator in a symbolic debugger, one can 25 ** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed 26 ** as they are added to the instruction stream. 27 */ 28 #ifdef SQLITE_DEBUG 29 int sqlite3_vdbe_addop_trace = 0; 30 #endif 31 32 33 /* 34 ** Create a new virtual database engine. 35 */ 36 Vdbe *sqlite3VdbeCreate(sqlite3 *db){ 37 Vdbe *p; 38 p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); 39 if( p==0 ) return 0; 40 p->db = db; 41 if( db->pVdbe ){ 42 db->pVdbe->pPrev = p; 43 } 44 p->pNext = db->pVdbe; 45 p->pPrev = 0; 46 db->pVdbe = p; 47 p->magic = VDBE_MAGIC_INIT; 48 return p; 49 } 50 51 /* 52 ** Remember the SQL string for a prepared statement. 53 */ 54 void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){ 55 if( p==0 ) return; 56 assert( p->zSql==0 ); 57 p->zSql = sqlite3DbStrNDup(p->db, z, n); 58 } 59 60 /* 61 ** Return the SQL associated with a prepared statement 62 */ 63 const char *sqlite3_sql(sqlite3_stmt *pStmt){ 64 return ((Vdbe *)pStmt)->zSql; 65 } 66 67 /* 68 ** Swap all content between two VDBE structures. 69 */ 70 void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ 71 Vdbe tmp, *pTmp; 72 char *zTmp; 73 int nTmp; 74 tmp = *pA; 75 *pA = *pB; 76 *pB = tmp; 77 pTmp = pA->pNext; 78 pA->pNext = pB->pNext; 79 pB->pNext = pTmp; 80 pTmp = pA->pPrev; 81 pA->pPrev = pB->pPrev; 82 pB->pPrev = pTmp; 83 zTmp = pA->zSql; 84 pA->zSql = pB->zSql; 85 pB->zSql = zTmp; 86 nTmp = pA->nSql; 87 pA->nSql = pB->nSql; 88 pB->nSql = nTmp; 89 } 90 91 #ifdef SQLITE_DEBUG 92 /* 93 ** Turn tracing on or off 94 */ 95 void sqlite3VdbeTrace(Vdbe *p, FILE *trace){ 96 p->trace = trace; 97 } 98 #endif 99 100 /* 101 ** Resize the Vdbe.aOp array so that it contains at least N 102 ** elements. 103 ** 104 ** If an out-of-memory error occurs while resizing the array, 105 ** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that 106 ** any opcodes already allocated can be correctly deallocated 107 ** along with the rest of the Vdbe). 108 */ 109 static void resizeOpArray(Vdbe *p, int N){ 110 VdbeOp *pNew; 111 int oldSize = p->nOpAlloc; 112 pNew = sqlite3DbRealloc(p->db, p->aOp, N*sizeof(Op)); 113 if( pNew ){ 114 p->nOpAlloc = N; 115 p->aOp = pNew; 116 if( N>oldSize ){ 117 memset(&p->aOp[oldSize], 0, (N-oldSize)*sizeof(Op)); 118 } 119 } 120 } 121 122 /* 123 ** Add a new instruction to the list of instructions current in the 124 ** VDBE. Return the address of the new instruction. 125 ** 126 ** Parameters: 127 ** 128 ** p Pointer to the VDBE 129 ** 130 ** op The opcode for this instruction 131 ** 132 ** p1, p2 First two of the three possible operands. 133 ** 134 ** Use the sqlite3VdbeResolveLabel() function to fix an address and 135 ** the sqlite3VdbeChangeP3() function to change the value of the P3 136 ** operand. 137 */ 138 int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){ 139 int i; 140 VdbeOp *pOp; 141 142 i = p->nOp; 143 assert( p->magic==VDBE_MAGIC_INIT ); 144 if( p->nOpAlloc<=i ){ 145 resizeOpArray(p, p->nOpAlloc*2 + 100); 146 if( p->db->mallocFailed ){ 147 return 0; 148 } 149 } 150 p->nOp++; 151 pOp = &p->aOp[i]; 152 pOp->opcode = op; 153 pOp->p1 = p1; 154 pOp->p2 = p2; 155 pOp->p3 = 0; 156 pOp->p3type = P3_NOTUSED; 157 p->expired = 0; 158 #ifdef SQLITE_DEBUG 159 if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]); 160 #endif 161 return i; 162 } 163 164 /* 165 ** Add an opcode that includes the p3 value. 166 */ 167 int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){ 168 int addr = sqlite3VdbeAddOp(p, op, p1, p2); 169 sqlite3VdbeChangeP3(p, addr, zP3, p3type); 170 return addr; 171 } 172 173 /* 174 ** Create a new symbolic label for an instruction that has yet to be 175 ** coded. The symbolic label is really just a negative number. The 176 ** label can be used as the P2 value of an operation. Later, when 177 ** the label is resolved to a specific address, the VDBE will scan 178 ** through its operation list and change all values of P2 which match 179 ** the label into the resolved address. 180 ** 181 ** The VDBE knows that a P2 value is a label because labels are 182 ** always negative and P2 values are suppose to be non-negative. 183 ** Hence, a negative P2 value is a label that has yet to be resolved. 184 ** 185 ** Zero is returned if a malloc() fails. 186 */ 187 int sqlite3VdbeMakeLabel(Vdbe *p){ 188 int i; 189 i = p->nLabel++; 190 assert( p->magic==VDBE_MAGIC_INIT ); 191 if( i>=p->nLabelAlloc ){ 192 p->nLabelAlloc = p->nLabelAlloc*2 + 10; 193 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, 194 p->nLabelAlloc*sizeof(p->aLabel[0])); 195 } 196 if( p->aLabel ){ 197 p->aLabel[i] = -1; 198 } 199 return -1-i; 200 } 201 202 /* 203 ** Resolve label "x" to be the address of the next instruction to 204 ** be inserted. The parameter "x" must have been obtained from 205 ** a prior call to sqlite3VdbeMakeLabel(). 206 */ 207 void sqlite3VdbeResolveLabel(Vdbe *p, int x){ 208 int j = -1-x; 209 assert( p->magic==VDBE_MAGIC_INIT ); 210 assert( j>=0 && j<p->nLabel ); 211 if( p->aLabel ){ 212 p->aLabel[j] = p->nOp; 213 } 214 } 215 216 /* 217 ** Return non-zero if opcode 'op' is guarenteed not to push more values 218 ** onto the VDBE stack than it pops off. 219 */ 220 static int opcodeNoPush(u8 op){ 221 /* The 10 NOPUSH_MASK_n constants are defined in the automatically 222 ** generated header file opcodes.h. Each is a 16-bit bitmask, one 223 ** bit corresponding to each opcode implemented by the virtual 224 ** machine in vdbe.c. The bit is true if the word "no-push" appears 225 ** in a comment on the same line as the "case OP_XXX:" in 226 ** sqlite3VdbeExec() in vdbe.c. 227 ** 228 ** If the bit is true, then the corresponding opcode is guarenteed not 229 ** to grow the stack when it is executed. Otherwise, it may grow the 230 ** stack by at most one entry. 231 ** 232 ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains 233 ** one bit for opcodes 16 to 31, and so on. 234 ** 235 ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h 236 ** because the file is generated by an awk program. Awk manipulates 237 ** all numbers as floating-point and we don't want to risk a rounding 238 ** error if someone builds with an awk that uses (for example) 32-bit 239 ** IEEE floats. 240 */ 241 static const u32 masks[5] = { 242 NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16), 243 NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16), 244 NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16), 245 NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16), 246 NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16) 247 }; 248 assert( op<32*5 ); 249 return (masks[op>>5] & (1<<(op&0x1F))); 250 } 251 252 #ifndef NDEBUG 253 int sqlite3VdbeOpcodeNoPush(u8 op){ 254 return opcodeNoPush(op); 255 } 256 #endif 257 258 /* 259 ** Loop through the program looking for P2 values that are negative. 260 ** Each such value is a label. Resolve the label by setting the P2 261 ** value to its correct non-zero value. 262 ** 263 ** This routine is called once after all opcodes have been inserted. 264 ** 265 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument 266 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by 267 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array. 268 ** 269 ** The integer *pMaxStack is set to the maximum number of vdbe stack 270 ** entries that static analysis reveals this program might need. 271 ** 272 ** This routine also does the following optimization: It scans for 273 ** Halt instructions where P1==SQLITE_CONSTRAINT or P2==OE_Abort or for 274 ** IdxInsert instructions where P2!=0. If no such instruction is 275 ** found, then every Statement instruction is changed to a Noop. In 276 ** this way, we avoid creating the statement journal file unnecessarily. 277 */ 278 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){ 279 int i; 280 int nMaxArgs = 0; 281 int nMaxStack = p->nOp; 282 Op *pOp; 283 int *aLabel = p->aLabel; 284 int doesStatementRollback = 0; 285 int hasStatementBegin = 0; 286 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ 287 u8 opcode = pOp->opcode; 288 289 if( opcode==OP_Function || opcode==OP_AggStep 290 #ifndef SQLITE_OMIT_VIRTUALTABLE 291 || opcode==OP_VUpdate 292 #endif 293 ){ 294 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; 295 } 296 if( opcode==OP_Halt ){ 297 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){ 298 doesStatementRollback = 1; 299 } 300 }else if( opcode==OP_Statement ){ 301 hasStatementBegin = 1; 302 #ifndef SQLITE_OMIT_VIRTUALTABLE 303 }else if( opcode==OP_VUpdate || opcode==OP_VRename ){ 304 doesStatementRollback = 1; 305 }else if( opcode==OP_VFilter ){ 306 int n; 307 assert( p->nOp - i >= 3 ); 308 assert( pOp[-2].opcode==OP_Integer ); 309 n = pOp[-2].p1; 310 if( n>nMaxArgs ) nMaxArgs = n; 311 #endif 312 } 313 if( opcodeNoPush(opcode) ){ 314 nMaxStack--; 315 } 316 317 if( pOp->p2>=0 ) continue; 318 assert( -1-pOp->p2<p->nLabel ); 319 pOp->p2 = aLabel[-1-pOp->p2]; 320 } 321 sqlite3_free(p->aLabel); 322 p->aLabel = 0; 323 324 *pMaxFuncArgs = nMaxArgs; 325 *pMaxStack = nMaxStack; 326 327 /* If we never rollback a statement transaction, then statement 328 ** transactions are not needed. So change every OP_Statement 329 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive() 330 ** which can be expensive on some platforms. 331 */ 332 if( hasStatementBegin && !doesStatementRollback ){ 333 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ 334 if( pOp->opcode==OP_Statement ){ 335 pOp->opcode = OP_Noop; 336 } 337 } 338 } 339 } 340 341 /* 342 ** Return the address of the next instruction to be inserted. 343 */ 344 int sqlite3VdbeCurrentAddr(Vdbe *p){ 345 assert( p->magic==VDBE_MAGIC_INIT ); 346 return p->nOp; 347 } 348 349 /* 350 ** Add a whole list of operations to the operation stack. Return the 351 ** address of the first operation added. 352 */ 353 int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ 354 int addr; 355 assert( p->magic==VDBE_MAGIC_INIT ); 356 if( p->nOp + nOp > p->nOpAlloc ){ 357 resizeOpArray(p, p->nOp*2 + nOp); 358 } 359 if( p->db->mallocFailed ){ 360 return 0; 361 } 362 addr = p->nOp; 363 if( nOp>0 ){ 364 int i; 365 VdbeOpList const *pIn = aOp; 366 for(i=0; i<nOp; i++, pIn++){ 367 int p2 = pIn->p2; 368 VdbeOp *pOut = &p->aOp[i+addr]; 369 pOut->opcode = pIn->opcode; 370 pOut->p1 = pIn->p1; 371 pOut->p2 = p2<0 ? addr + ADDR(p2) : p2; 372 pOut->p3 = pIn->p3; 373 pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED; 374 #ifdef SQLITE_DEBUG 375 if( sqlite3_vdbe_addop_trace ){ 376 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); 377 } 378 #endif 379 } 380 p->nOp += nOp; 381 } 382 return addr; 383 } 384 385 /* 386 ** Change the value of the P1 operand for a specific instruction. 387 ** This routine is useful when a large program is loaded from a 388 ** static array using sqlite3VdbeAddOpList but we want to make a 389 ** few minor changes to the program. 390 */ 391 void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ 392 assert( p==0 || p->magic==VDBE_MAGIC_INIT ); 393 if( p && addr>=0 && p->nOp>addr && p->aOp ){ 394 p->aOp[addr].p1 = val; 395 } 396 } 397 398 /* 399 ** Change the value of the P2 operand for a specific instruction. 400 ** This routine is useful for setting a jump destination. 401 */ 402 void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ 403 assert( val>=0 ); 404 assert( p==0 || p->magic==VDBE_MAGIC_INIT ); 405 if( p && addr>=0 && p->nOp>addr && p->aOp ){ 406 p->aOp[addr].p2 = val; 407 } 408 } 409 410 /* 411 ** Change the P2 operand of instruction addr so that it points to 412 ** the address of the next instruction to be coded. 413 */ 414 void sqlite3VdbeJumpHere(Vdbe *p, int addr){ 415 sqlite3VdbeChangeP2(p, addr, p->nOp); 416 } 417 418 419 /* 420 ** If the input FuncDef structure is ephemeral, then free it. If 421 ** the FuncDef is not ephermal, then do nothing. 422 */ 423 static void freeEphemeralFunction(FuncDef *pDef){ 424 if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){ 425 sqlite3_free(pDef); 426 } 427 } 428 429 /* 430 ** Delete a P3 value if necessary. 431 */ 432 static void freeP3(int p3type, void *p3){ 433 if( p3 ){ 434 switch( p3type ){ 435 case P3_REAL: 436 case P3_INT64: 437 case P3_MPRINTF: 438 case P3_DYNAMIC: 439 case P3_KEYINFO: 440 case P3_KEYINFO_HANDOFF: { 441 sqlite3_free(p3); 442 break; 443 } 444 case P3_VDBEFUNC: { 445 VdbeFunc *pVdbeFunc = (VdbeFunc *)p3; 446 freeEphemeralFunction(pVdbeFunc->pFunc); 447 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0); 448 sqlite3_free(pVdbeFunc); 449 break; 450 } 451 case P3_FUNCDEF: { 452 freeEphemeralFunction((FuncDef*)p3); 453 break; 454 } 455 case P3_MEM: { 456 sqlite3ValueFree((sqlite3_value*)p3); 457 break; 458 } 459 } 460 } 461 } 462 463 464 /* 465 ** Change N opcodes starting at addr to No-ops. 466 */ 467 void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ 468 if( p && p->aOp ){ 469 VdbeOp *pOp = &p->aOp[addr]; 470 while( N-- ){ 471 freeP3(pOp->p3type, pOp->p3); 472 memset(pOp, 0, sizeof(pOp[0])); 473 pOp->opcode = OP_Noop; 474 pOp++; 475 } 476 } 477 } 478 479 /* 480 ** Change the value of the P3 operand for a specific instruction. 481 ** This routine is useful when a large program is loaded from a 482 ** static array using sqlite3VdbeAddOpList but we want to make a 483 ** few minor changes to the program. 484 ** 485 ** If n>=0 then the P3 operand is dynamic, meaning that a copy of 486 ** the string is made into memory obtained from sqlite3_malloc(). 487 ** A value of n==0 means copy bytes of zP3 up to and including the 488 ** first null byte. If n>0 then copy n+1 bytes of zP3. 489 ** 490 ** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure. 491 ** A copy is made of the KeyInfo structure into memory obtained from 492 ** sqlite3_malloc, to be freed when the Vdbe is finalized. 493 ** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure 494 ** stored in memory that the caller has obtained from sqlite3_malloc. The 495 ** caller should not free the allocation, it will be freed when the Vdbe is 496 ** finalized. 497 ** 498 ** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points 499 ** to a string or structure that is guaranteed to exist for the lifetime of 500 ** the Vdbe. In these cases we can just copy the pointer. 501 ** 502 ** If addr<0 then change P3 on the most recently inserted instruction. 503 */ 504 void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){ 505 Op *pOp; 506 assert( p==0 || p->magic==VDBE_MAGIC_INIT ); 507 if( p==0 || p->aOp==0 || p->db->mallocFailed ){ 508 if (n != P3_KEYINFO) { 509 freeP3(n, (void*)*(char**)&zP3); 510 } 511 return; 512 } 513 if( addr<0 || addr>=p->nOp ){ 514 addr = p->nOp - 1; 515 if( addr<0 ) return; 516 } 517 pOp = &p->aOp[addr]; 518 freeP3(pOp->p3type, pOp->p3); 519 pOp->p3 = 0; 520 if( zP3==0 ){ 521 pOp->p3 = 0; 522 pOp->p3type = P3_NOTUSED; 523 }else if( n==P3_KEYINFO ){ 524 KeyInfo *pKeyInfo; 525 int nField, nByte; 526 527 nField = ((KeyInfo*)zP3)->nField; 528 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; 529 pKeyInfo = sqlite3_malloc( nByte ); 530 pOp->p3 = (char*)pKeyInfo; 531 if( pKeyInfo ){ 532 unsigned char *aSortOrder; 533 memcpy(pKeyInfo, zP3, nByte); 534 aSortOrder = pKeyInfo->aSortOrder; 535 if( aSortOrder ){ 536 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField]; 537 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField); 538 } 539 pOp->p3type = P3_KEYINFO; 540 }else{ 541 p->db->mallocFailed = 1; 542 pOp->p3type = P3_NOTUSED; 543 } 544 }else if( n==P3_KEYINFO_HANDOFF ){ 545 pOp->p3 = (char*)zP3; 546 pOp->p3type = P3_KEYINFO; 547 }else if( n<0 ){ 548 pOp->p3 = (char*)zP3; 549 pOp->p3type = n; 550 }else{ 551 if( n==0 ) n = strlen(zP3); 552 pOp->p3 = sqlite3DbStrNDup(p->db, zP3, n); 553 pOp->p3type = P3_DYNAMIC; 554 } 555 } 556 557 #ifndef NDEBUG 558 /* 559 ** Replace the P3 field of the most recently coded instruction with 560 ** comment text. 561 */ 562 void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ 563 va_list ap; 564 assert( p->nOp>0 || p->aOp==0 ); 565 assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || p->db->mallocFailed ); 566 va_start(ap, zFormat); 567 sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(p->db, zFormat, ap), P3_DYNAMIC); 568 va_end(ap); 569 } 570 #endif 571 572 /* 573 ** Return the opcode for a given address. 574 */ 575 VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ 576 assert( p->magic==VDBE_MAGIC_INIT ); 577 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); 578 return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0); 579 } 580 581 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ 582 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) 583 /* 584 ** Compute a string that describes the P3 parameter for an opcode. 585 ** Use zTemp for any required temporary buffer space. 586 */ 587 static char *displayP3(Op *pOp, char *zTemp, int nTemp){ 588 char *zP3; 589 assert( nTemp>=20 ); 590 switch( pOp->p3type ){ 591 case P3_KEYINFO: { 592 int i, j; 593 KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3; 594 sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField); 595 i = strlen(zTemp); 596 for(j=0; j<pKeyInfo->nField; j++){ 597 CollSeq *pColl = pKeyInfo->aColl[j]; 598 if( pColl ){ 599 int n = strlen(pColl->zName); 600 if( i+n>nTemp-6 ){ 601 memcpy(&zTemp[i],",...",4); 602 break; 603 } 604 zTemp[i++] = ','; 605 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){ 606 zTemp[i++] = '-'; 607 } 608 memcpy(&zTemp[i], pColl->zName,n+1); 609 i += n; 610 }else if( i+4<nTemp-6 ){ 611 memcpy(&zTemp[i],",nil",4); 612 i += 4; 613 } 614 } 615 zTemp[i++] = ')'; 616 zTemp[i] = 0; 617 assert( i<nTemp ); 618 zP3 = zTemp; 619 break; 620 } 621 case P3_COLLSEQ: { 622 CollSeq *pColl = (CollSeq*)pOp->p3; 623 sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName); 624 zP3 = zTemp; 625 break; 626 } 627 case P3_FUNCDEF: { 628 FuncDef *pDef = (FuncDef*)pOp->p3; 629 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); 630 zP3 = zTemp; 631 break; 632 } 633 case P3_INT64: { 634 sqlite3_snprintf(nTemp, zTemp, "%lld", *(sqlite3_int64*)pOp->p3); 635 zP3 = zTemp; 636 break; 637 } 638 case P3_REAL: { 639 sqlite3_snprintf(nTemp, zTemp, "%.16g", *(double*)pOp->p3); 640 zP3 = zTemp; 641 break; 642 } 643 #ifndef SQLITE_OMIT_VIRTUALTABLE 644 case P3_VTAB: { 645 sqlite3_vtab *pVtab = (sqlite3_vtab*)pOp->p3; 646 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); 647 zP3 = zTemp; 648 break; 649 } 650 #endif 651 default: { 652 zP3 = pOp->p3; 653 if( zP3==0 || pOp->opcode==OP_Noop ){ 654 zP3 = ""; 655 } 656 } 657 } 658 assert( zP3!=0 ); 659 return zP3; 660 } 661 #endif 662 663 /* 664 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. 665 ** 666 */ 667 void sqlite3VdbeUsesBtree(Vdbe *p, int i){ 668 int mask; 669 assert( i>=0 && i<p->db->nDb ); 670 assert( i<sizeof(p->btreeMask)*8 ); 671 mask = 1<<i; 672 if( (p->btreeMask & mask)==0 ){ 673 p->btreeMask |= mask; 674 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt); 675 } 676 } 677 678 679 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) 680 /* 681 ** Print a single opcode. This routine is used for debugging only. 682 */ 683 void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ 684 char *zP3; 685 char zPtr[50]; 686 static const char *zFormat1 = "%4d %-13s %4d %4d %s\n"; 687 if( pOut==0 ) pOut = stdout; 688 zP3 = displayP3(pOp, zPtr, sizeof(zPtr)); 689 fprintf(pOut, zFormat1, 690 pc, sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, zP3); 691 fflush(pOut); 692 } 693 #endif 694 695 /* 696 ** Release an array of N Mem elements 697 */ 698 static void releaseMemArray(Mem *p, int N){ 699 if( p ){ 700 while( N-->0 ){ 701 assert( N<2 || p[0].db==p[1].db ); 702 sqlite3VdbeMemRelease(p++); 703 } 704 } 705 } 706 707 #ifndef SQLITE_OMIT_EXPLAIN 708 /* 709 ** Give a listing of the program in the virtual machine. 710 ** 711 ** The interface is the same as sqlite3VdbeExec(). But instead of 712 ** running the code, it invokes the callback once for each instruction. 713 ** This feature is used to implement "EXPLAIN". 714 */ 715 int sqlite3VdbeList( 716 Vdbe *p /* The VDBE */ 717 ){ 718 sqlite3 *db = p->db; 719 int i; 720 int rc = SQLITE_OK; 721 722 assert( p->explain ); 723 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; 724 assert( db->magic==SQLITE_MAGIC_BUSY ); 725 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); 726 727 /* Even though this opcode does not put dynamic strings onto the 728 ** the stack, they may become dynamic if the user calls 729 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. 730 */ 731 if( p->pTos==&p->aStack[4] ){ 732 releaseMemArray(p->aStack, 5); 733 } 734 p->resOnStack = 0; 735 736 do{ 737 i = p->pc++; 738 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain ); 739 if( i>=p->nOp ){ 740 p->rc = SQLITE_OK; 741 rc = SQLITE_DONE; 742 }else if( db->u1.isInterrupted ){ 743 p->rc = SQLITE_INTERRUPT; 744 rc = SQLITE_ERROR; 745 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0); 746 }else{ 747 Op *pOp = &p->aOp[i]; 748 Mem *pMem = p->aStack; 749 pMem->flags = MEM_Int; 750 pMem->type = SQLITE_INTEGER; 751 pMem->u.i = i; /* Program counter */ 752 pMem++; 753 754 pMem->flags = MEM_Static|MEM_Str|MEM_Term; 755 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ 756 assert( pMem->z!=0 ); 757 pMem->n = strlen(pMem->z); 758 pMem->type = SQLITE_TEXT; 759 pMem->enc = SQLITE_UTF8; 760 pMem++; 761 762 pMem->flags = MEM_Int; 763 pMem->u.i = pOp->p1; /* P1 */ 764 pMem->type = SQLITE_INTEGER; 765 pMem++; 766 767 pMem->flags = MEM_Int; 768 pMem->u.i = pOp->p2; /* P2 */ 769 pMem->type = SQLITE_INTEGER; 770 pMem++; 771 772 pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P3 */ 773 pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort)); 774 assert( pMem->z!=0 ); 775 pMem->n = strlen(pMem->z); 776 pMem->type = SQLITE_TEXT; 777 pMem->enc = SQLITE_UTF8; 778 779 p->nResColumn = 5 - 2*(p->explain-1); 780 p->pTos = pMem; 781 p->rc = SQLITE_OK; 782 p->resOnStack = 1; 783 rc = SQLITE_ROW; 784 } 785 return rc; 786 } 787 #endif /* SQLITE_OMIT_EXPLAIN */ 788 789 #ifdef SQLITE_DEBUG 790 /* 791 ** Print the SQL that was used to generate a VDBE program. 792 */ 793 void sqlite3VdbePrintSql(Vdbe *p){ 794 int nOp = p->nOp; 795 VdbeOp *pOp; 796 if( nOp<1 ) return; 797 pOp = &p->aOp[nOp-1]; 798 if( pOp->opcode==OP_Noop && pOp->p3!=0 ){ 799 const char *z = pOp->p3; 800 while( isspace(*(u8*)z) ) z++; 801 printf("SQL: [%s]\n", z); 802 } 803 } 804 #endif 805 806 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) 807 /* 808 ** Print an IOTRACE message showing SQL content. 809 */ 810 void sqlite3VdbeIOTraceSql(Vdbe *p){ 811 int nOp = p->nOp; 812 VdbeOp *pOp; 813 if( sqlite3_io_trace==0 ) return; 814 if( nOp<1 ) return; 815 pOp = &p->aOp[nOp-1]; 816 if( pOp->opcode==OP_Noop && pOp->p3!=0 ){ 817 int i, j; 818 char z[1000]; 819 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p3); 820 for(i=0; isspace((unsigned char)z[i]); i++){} 821 for(j=0; z[i]; i++){ 822 if( isspace((unsigned char)z[i]) ){ 823 if( z[i-1]!=' ' ){ 824 z[j++] = ' '; 825 } 826 }else{ 827 z[j++] = z[i]; 828 } 829 } 830 z[j] = 0; 831 sqlite3_io_trace("SQL %s\n", z); 832 } 833 } 834 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */ 835 836 837 /* 838 ** Prepare a virtual machine for execution. This involves things such 839 ** as allocating stack space and initializing the program counter. 840 ** After the VDBE has be prepped, it can be executed by one or more 841 ** calls to sqlite3VdbeExec(). 842 ** 843 ** This is the only way to move a VDBE from VDBE_MAGIC_INIT to 844 ** VDBE_MAGIC_RUN. 845 */ 846 void sqlite3VdbeMakeReady( 847 Vdbe *p, /* The VDBE */ 848 int nVar, /* Number of '?' see in the SQL statement */ 849 int nMem, /* Number of memory cells to allocate */ 850 int nCursor, /* Number of cursors to allocate */ 851 int isExplain /* True if the EXPLAIN keywords is present */ 852 ){ 853 int n; 854 sqlite3 *db = p->db; 855 856 assert( p!=0 ); 857 assert( p->magic==VDBE_MAGIC_INIT ); 858 859 /* There should be at least one opcode. 860 */ 861 assert( p->nOp>0 ); 862 863 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This 864 * is because the call to resizeOpArray() below may shrink the 865 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN 866 * state. 867 */ 868 p->magic = VDBE_MAGIC_RUN; 869 870 /* No instruction ever pushes more than a single element onto the 871 ** stack. And the stack never grows on successive executions of the 872 ** same loop. So the total number of instructions is an upper bound 873 ** on the maximum stack depth required. (Added later:) The 874 ** resolveP2Values() call computes a tighter upper bound on the 875 ** stack size. 876 ** 877 ** Allocation all the stack space we will ever need. 878 */ 879 if( p->aStack==0 ){ 880 int nArg; /* Maximum number of args passed to a user function. */ 881 int nStack; /* Maximum number of stack entries required */ 882 resolveP2Values(p, &nArg, &nStack); 883 resizeOpArray(p, p->nOp); 884 assert( nVar>=0 ); 885 assert( nStack<p->nOp ); 886 if( isExplain ){ 887 nStack = 10; 888 } 889 p->aStack = sqlite3DbMallocZero(db, 890 nStack*sizeof(p->aStack[0]) /* aStack */ 891 + nArg*sizeof(Mem*) /* apArg */ 892 + nVar*sizeof(Mem) /* aVar */ 893 + nVar*sizeof(char*) /* azVar */ 894 + nMem*sizeof(Mem) /* aMem */ 895 + nCursor*sizeof(Cursor*) /* apCsr */ 896 ); 897 if( !db->mallocFailed ){ 898 p->aMem = &p->aStack[nStack]; 899 p->nMem = nMem; 900 p->aVar = &p->aMem[nMem]; 901 p->nVar = nVar; 902 p->okVar = 0; 903 p->apArg = (Mem**)&p->aVar[nVar]; 904 p->azVar = (char**)&p->apArg[nArg]; 905 p->apCsr = (Cursor**)&p->azVar[nVar]; 906 p->nCursor = nCursor; 907 for(n=0; n<nVar; n++){ 908 p->aVar[n].flags = MEM_Null; 909 p->aVar[n].db = db; 910 } 911 for(n=0; n<nStack; n++){ 912 p->aStack[n].db = db; 913 } 914 } 915 } 916 for(n=0; n<p->nMem; n++){ 917 p->aMem[n].flags = MEM_Null; 918 p->aMem[n].db = db; 919 } 920 921 p->pTos = &p->aStack[-1]; 922 p->pc = -1; 923 p->rc = SQLITE_OK; 924 p->uniqueCnt = 0; 925 p->returnDepth = 0; 926 p->errorAction = OE_Abort; 927 p->popStack = 0; 928 p->explain |= isExplain; 929 p->magic = VDBE_MAGIC_RUN; 930 p->nChange = 0; 931 p->cacheCtr = 1; 932 p->minWriteFileFormat = 255; 933 p->openedStatement = 0; 934 #ifdef VDBE_PROFILE 935 { 936 int i; 937 for(i=0; i<p->nOp; i++){ 938 p->aOp[i].cnt = 0; 939 p->aOp[i].cycles = 0; 940 } 941 } 942 #endif 943 } 944 945 /* 946 ** Close a VDBE cursor and release all the resources that cursor happens 947 ** to hold. 948 */ 949 void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){ 950 if( pCx==0 ){ 951 return; 952 } 953 if( pCx->pCursor ){ 954 sqlite3BtreeCloseCursor(pCx->pCursor); 955 } 956 if( pCx->pBt ){ 957 sqlite3BtreeClose(pCx->pBt); 958 } 959 #ifndef SQLITE_OMIT_VIRTUALTABLE 960 if( pCx->pVtabCursor ){ 961 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; 962 const sqlite3_module *pModule = pCx->pModule; 963 p->inVtabMethod = 1; 964 sqlite3SafetyOff(p->db); 965 pModule->xClose(pVtabCursor); 966 sqlite3SafetyOn(p->db); 967 p->inVtabMethod = 0; 968 } 969 #endif 970 sqlite3_free(pCx->pData); 971 sqlite3_free(pCx->aType); 972 sqlite3_free(pCx); 973 } 974 975 /* 976 ** Close all cursors except for VTab cursors that are currently 977 ** in use. 978 */ 979 static void closeAllCursorsExceptActiveVtabs(Vdbe *p){ 980 int i; 981 if( p->apCsr==0 ) return; 982 for(i=0; i<p->nCursor; i++){ 983 Cursor *pC = p->apCsr[i]; 984 if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){ 985 sqlite3VdbeFreeCursor(p, pC); 986 p->apCsr[i] = 0; 987 } 988 } 989 } 990 991 /* 992 ** Clean up the VM after execution. 993 ** 994 ** This routine will automatically close any cursors, lists, and/or 995 ** sorters that were left open. It also deletes the values of 996 ** variables in the aVar[] array. 997 */ 998 static void Cleanup(Vdbe *p){ 999 int i; 1000 if( p->aStack ){ 1001 releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack)); 1002 p->pTos = &p->aStack[-1]; 1003 } 1004 closeAllCursorsExceptActiveVtabs(p); 1005 releaseMemArray(p->aMem, p->nMem); 1006 sqlite3VdbeFifoClear(&p->sFifo); 1007 if( p->contextStack ){ 1008 for(i=0; i<p->contextStackTop; i++){ 1009 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo); 1010 } 1011 sqlite3_free(p->contextStack); 1012 } 1013 p->contextStack = 0; 1014 p->contextStackDepth = 0; 1015 p->contextStackTop = 0; 1016 sqlite3_free(p->zErrMsg); 1017 p->zErrMsg = 0; 1018 p->resOnStack = 0; 1019 } 1020 1021 /* 1022 ** Set the number of result columns that will be returned by this SQL 1023 ** statement. This is now set at compile time, rather than during 1024 ** execution of the vdbe program so that sqlite3_column_count() can 1025 ** be called on an SQL statement before sqlite3_step(). 1026 */ 1027 void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ 1028 Mem *pColName; 1029 int n; 1030 1031 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); 1032 sqlite3_free(p->aColName); 1033 n = nResColumn*COLNAME_N; 1034 p->nResColumn = nResColumn; 1035 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n ); 1036 if( p->aColName==0 ) return; 1037 while( n-- > 0 ){ 1038 pColName->flags = MEM_Null; 1039 pColName->db = p->db; 1040 pColName++; 1041 } 1042 } 1043 1044 /* 1045 ** Set the name of the idx'th column to be returned by the SQL statement. 1046 ** zName must be a pointer to a nul terminated string. 1047 ** 1048 ** This call must be made after a call to sqlite3VdbeSetNumCols(). 1049 ** 1050 ** If N==P3_STATIC it means that zName is a pointer to a constant static 1051 ** string and we can just copy the pointer. If it is P3_DYNAMIC, then 1052 ** the string is freed using sqlite3_free() when the vdbe is finished with 1053 ** it. Otherwise, N bytes of zName are copied. 1054 */ 1055 int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){ 1056 int rc; 1057 Mem *pColName; 1058 assert( idx<p->nResColumn ); 1059 assert( var<COLNAME_N ); 1060 if( p->db->mallocFailed ) return SQLITE_NOMEM; 1061 assert( p->aColName!=0 ); 1062 pColName = &(p->aColName[idx+var*p->nResColumn]); 1063 if( N==P3_DYNAMIC || N==P3_STATIC ){ 1064 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC); 1065 }else{ 1066 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT); 1067 } 1068 if( rc==SQLITE_OK && N==P3_DYNAMIC ){ 1069 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn; 1070 pColName->xDel = 0; 1071 } 1072 return rc; 1073 } 1074 1075 /* 1076 ** A read or write transaction may or may not be active on database handle 1077 ** db. If a transaction is active, commit it. If there is a 1078 ** write-transaction spanning more than one database file, this routine 1079 ** takes care of the master journal trickery. 1080 */ 1081 static int vdbeCommit(sqlite3 *db){ 1082 int i; 1083 int nTrans = 0; /* Number of databases with an active write-transaction */ 1084 int rc = SQLITE_OK; 1085 int needXcommit = 0; 1086 1087 /* Before doing anything else, call the xSync() callback for any 1088 ** virtual module tables written in this transaction. This has to 1089 ** be done before determining whether a master journal file is 1090 ** required, as an xSync() callback may add an attached database 1091 ** to the transaction. 1092 */ 1093 rc = sqlite3VtabSync(db, rc); 1094 if( rc!=SQLITE_OK ){ 1095 return rc; 1096 } 1097 1098 /* This loop determines (a) if the commit hook should be invoked and 1099 ** (b) how many database files have open write transactions, not 1100 ** including the temp database. (b) is important because if more than 1101 ** one database file has an open write transaction, a master journal 1102 ** file is required for an atomic commit. 1103 */ 1104 for(i=0; i<db->nDb; i++){ 1105 Btree *pBt = db->aDb[i].pBt; 1106 if( sqlite3BtreeIsInTrans(pBt) ){ 1107 needXcommit = 1; 1108 if( i!=1 ) nTrans++; 1109 } 1110 } 1111 1112 /* If there are any write-transactions at all, invoke the commit hook */ 1113 if( needXcommit && db->xCommitCallback ){ 1114 sqlite3SafetyOff(db); 1115 rc = db->xCommitCallback(db->pCommitArg); 1116 sqlite3SafetyOn(db); 1117 if( rc ){ 1118 return SQLITE_CONSTRAINT; 1119 } 1120 } 1121 1122 /* The simple case - no more than one database file (not counting the 1123 ** TEMP database) has a transaction active. There is no need for the 1124 ** master-journal. 1125 ** 1126 ** If the return value of sqlite3BtreeGetFilename() is a zero length 1127 ** string, it means the main database is :memory:. In that case we do 1128 ** not support atomic multi-file commits, so use the simple case then 1129 ** too. 1130 */ 1131 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){ 1132 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 1133 Btree *pBt = db->aDb[i].pBt; 1134 if( pBt ){ 1135 rc = sqlite3BtreeCommitPhaseOne(pBt, 0); 1136 } 1137 } 1138 1139 /* Do the commit only if all databases successfully complete phase 1. 1140 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an 1141 ** IO error while deleting or truncating a journal file. It is unlikely, 1142 ** but could happen. In this case abandon processing and return the error. 1143 */ 1144 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 1145 Btree *pBt = db->aDb[i].pBt; 1146 if( pBt ){ 1147 rc = sqlite3BtreeCommitPhaseTwo(pBt); 1148 } 1149 } 1150 if( rc==SQLITE_OK ){ 1151 sqlite3VtabCommit(db); 1152 } 1153 } 1154 1155 /* The complex case - There is a multi-file write-transaction active. 1156 ** This requires a master journal file to ensure the transaction is 1157 ** committed atomicly. 1158 */ 1159 #ifndef SQLITE_OMIT_DISKIO 1160 else{ 1161 sqlite3_vfs *pVfs = db->pVfs; 1162 int needSync = 0; 1163 char *zMaster = 0; /* File-name for the master journal */ 1164 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); 1165 sqlite3_file *pMaster = 0; 1166 i64 offset = 0; 1167 1168 /* Select a master journal file name */ 1169 do { 1170 u32 random; 1171 sqlite3_free(zMaster); 1172 sqlite3Randomness(sizeof(random), &random); 1173 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff); 1174 if( !zMaster ){ 1175 return SQLITE_NOMEM; 1176 } 1177 }while( sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS) ); 1178 1179 /* Open the master journal. */ 1180 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, 1181 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| 1182 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 1183 ); 1184 if( rc!=SQLITE_OK ){ 1185 sqlite3_free(zMaster); 1186 return rc; 1187 } 1188 1189 /* Write the name of each database file in the transaction into the new 1190 ** master journal file. If an error occurs at this point close 1191 ** and delete the master journal file. All the individual journal files 1192 ** still have 'null' as the master journal pointer, so they will roll 1193 ** back independently if a failure occurs. 1194 */ 1195 for(i=0; i<db->nDb; i++){ 1196 Btree *pBt = db->aDb[i].pBt; 1197 if( i==1 ) continue; /* Ignore the TEMP database */ 1198 if( sqlite3BtreeIsInTrans(pBt) ){ 1199 char const *zFile = sqlite3BtreeGetJournalname(pBt); 1200 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */ 1201 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){ 1202 needSync = 1; 1203 } 1204 rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset); 1205 offset += strlen(zFile)+1; 1206 if( rc!=SQLITE_OK ){ 1207 sqlite3OsCloseFree(pMaster); 1208 sqlite3OsDelete(pVfs, zMaster, 0); 1209 sqlite3_free(zMaster); 1210 return rc; 1211 } 1212 } 1213 } 1214 1215 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device 1216 ** flag is set this is not required. 1217 */ 1218 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt); 1219 if( (needSync 1220 && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)) 1221 && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){ 1222 sqlite3OsCloseFree(pMaster); 1223 sqlite3OsDelete(pVfs, zMaster, 0); 1224 sqlite3_free(zMaster); 1225 return rc; 1226 } 1227 1228 /* Sync all the db files involved in the transaction. The same call 1229 ** sets the master journal pointer in each individual journal. If 1230 ** an error occurs here, do not delete the master journal file. 1231 ** 1232 ** If the error occurs during the first call to 1233 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the 1234 ** master journal file will be orphaned. But we cannot delete it, 1235 ** in case the master journal file name was written into the journal 1236 ** file before the failure occured. 1237 */ 1238 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 1239 Btree *pBt = db->aDb[i].pBt; 1240 if( pBt ){ 1241 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster); 1242 } 1243 } 1244 sqlite3OsCloseFree(pMaster); 1245 if( rc!=SQLITE_OK ){ 1246 sqlite3_free(zMaster); 1247 return rc; 1248 } 1249 1250 /* Delete the master journal file. This commits the transaction. After 1251 ** doing this the directory is synced again before any individual 1252 ** transaction files are deleted. 1253 */ 1254 rc = sqlite3OsDelete(pVfs, zMaster, 1); 1255 sqlite3_free(zMaster); 1256 zMaster = 0; 1257 if( rc ){ 1258 return rc; 1259 } 1260 1261 /* All files and directories have already been synced, so the following 1262 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and 1263 ** deleting or truncating journals. If something goes wrong while 1264 ** this is happening we don't really care. The integrity of the 1265 ** transaction is already guaranteed, but some stray 'cold' journals 1266 ** may be lying around. Returning an error code won't help matters. 1267 */ 1268 disable_simulated_io_errors(); 1269 for(i=0; i<db->nDb; i++){ 1270 Btree *pBt = db->aDb[i].pBt; 1271 if( pBt ){ 1272 sqlite3BtreeCommitPhaseTwo(pBt); 1273 } 1274 } 1275 enable_simulated_io_errors(); 1276 1277 sqlite3VtabCommit(db); 1278 } 1279 #endif 1280 1281 return rc; 1282 } 1283 1284 /* 1285 ** This routine checks that the sqlite3.activeVdbeCnt count variable 1286 ** matches the number of vdbe's in the list sqlite3.pVdbe that are 1287 ** currently active. An assertion fails if the two counts do not match. 1288 ** This is an internal self-check only - it is not an essential processing 1289 ** step. 1290 ** 1291 ** This is a no-op if NDEBUG is defined. 1292 */ 1293 #ifndef NDEBUG 1294 static void checkActiveVdbeCnt(sqlite3 *db){ 1295 Vdbe *p; 1296 int cnt = 0; 1297 p = db->pVdbe; 1298 while( p ){ 1299 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){ 1300 cnt++; 1301 } 1302 p = p->pNext; 1303 } 1304 assert( cnt==db->activeVdbeCnt ); 1305 } 1306 #else 1307 #define checkActiveVdbeCnt(x) 1308 #endif 1309 1310 /* 1311 ** For every Btree that in database connection db which 1312 ** has been modified, "trip" or invalidate each cursor in 1313 ** that Btree might have been modified so that the cursor 1314 ** can never be used again. This happens when a rollback 1315 *** occurs. We have to trip all the other cursors, even 1316 ** cursor from other VMs in different database connections, 1317 ** so that none of them try to use the data at which they 1318 ** were pointing and which now may have been changed due 1319 ** to the rollback. 1320 ** 1321 ** Remember that a rollback can delete tables complete and 1322 ** reorder rootpages. So it is not sufficient just to save 1323 ** the state of the cursor. We have to invalidate the cursor 1324 ** so that it is never used again. 1325 */ 1326 void invalidateCursorsOnModifiedBtrees(sqlite3 *db){ 1327 int i; 1328 for(i=0; i<db->nDb; i++){ 1329 Btree *p = db->aDb[i].pBt; 1330 if( p && sqlite3BtreeIsInTrans(p) ){ 1331 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT); 1332 } 1333 } 1334 } 1335 1336 /* 1337 ** This routine is called the when a VDBE tries to halt. If the VDBE 1338 ** has made changes and is in autocommit mode, then commit those 1339 ** changes. If a rollback is needed, then do the rollback. 1340 ** 1341 ** This routine is the only way to move the state of a VM from 1342 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to 1343 ** call this on a VM that is in the SQLITE_MAGIC_HALT state. 1344 ** 1345 ** Return an error code. If the commit could not complete because of 1346 ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it 1347 ** means the close did not happen and needs to be repeated. 1348 */ 1349 int sqlite3VdbeHalt(Vdbe *p){ 1350 sqlite3 *db = p->db; 1351 int i; 1352 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */ 1353 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */ 1354 1355 /* This function contains the logic that determines if a statement or 1356 ** transaction will be committed or rolled back as a result of the 1357 ** execution of this virtual machine. 1358 ** 1359 ** If any of the following errors occur: 1360 ** 1361 ** SQLITE_NOMEM 1362 ** SQLITE_IOERR 1363 ** SQLITE_FULL 1364 ** SQLITE_INTERRUPT 1365 ** 1366 ** Then the internal cache might have been left in an inconsistent 1367 ** state. We need to rollback the statement transaction, if there is 1368 ** one, or the complete transaction if there is no statement transaction. 1369 */ 1370 1371 if( p->db->mallocFailed ){ 1372 p->rc = SQLITE_NOMEM; 1373 } 1374 closeAllCursorsExceptActiveVtabs(p); 1375 if( p->magic!=VDBE_MAGIC_RUN ){ 1376 return SQLITE_OK; 1377 } 1378 checkActiveVdbeCnt(db); 1379 1380 /* No commit or rollback needed if the program never started */ 1381 if( p->pc>=0 ){ 1382 int mrc; /* Primary error code from p->rc */ 1383 1384 /* Lock all btrees used by the statement */ 1385 sqlite3BtreeMutexArrayEnter(&p->aMutex); 1386 1387 /* Check for one of the special errors */ 1388 mrc = p->rc & 0xff; 1389 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR 1390 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL ; 1391 if( isSpecialError ){ 1392 /* This loop does static analysis of the query to see which of the 1393 ** following three categories it falls into: 1394 ** 1395 ** Read-only 1396 ** Query with statement journal 1397 ** Query without statement journal 1398 ** 1399 ** We could do something more elegant than this static analysis (i.e. 1400 ** store the type of query as part of the compliation phase), but 1401 ** handling malloc() or IO failure is a fairly obscure edge case so 1402 ** this is probably easier. Todo: Might be an opportunity to reduce 1403 ** code size a very small amount though... 1404 */ 1405 int notReadOnly = 0; 1406 int isStatement = 0; 1407 assert(p->aOp || p->nOp==0); 1408 for(i=0; i<p->nOp; i++){ 1409 switch( p->aOp[i].opcode ){ 1410 case OP_Transaction: 1411 notReadOnly |= p->aOp[i].p2; 1412 break; 1413 case OP_Statement: 1414 isStatement = 1; 1415 break; 1416 } 1417 } 1418 1419 1420 /* If the query was read-only, we need do no rollback at all. Otherwise, 1421 ** proceed with the special handling. 1422 */ 1423 if( notReadOnly || mrc!=SQLITE_INTERRUPT ){ 1424 if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){ 1425 xFunc = sqlite3BtreeRollbackStmt; 1426 p->rc = SQLITE_BUSY; 1427 } else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && isStatement ){ 1428 xFunc = sqlite3BtreeRollbackStmt; 1429 }else{ 1430 /* We are forced to roll back the active transaction. Before doing 1431 ** so, abort any other statements this handle currently has active. 1432 */ 1433 invalidateCursorsOnModifiedBtrees(db); 1434 sqlite3RollbackAll(db); 1435 db->autoCommit = 1; 1436 } 1437 } 1438 } 1439 1440 /* If the auto-commit flag is set and this is the only active vdbe, then 1441 ** we do either a commit or rollback of the current transaction. 1442 ** 1443 ** Note: This block also runs if one of the special errors handled 1444 ** above has occured. 1445 */ 1446 if( db->autoCommit && db->activeVdbeCnt==1 ){ 1447 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ 1448 /* The auto-commit flag is true, and the vdbe program was 1449 ** successful or hit an 'OR FAIL' constraint. This means a commit 1450 ** is required. 1451 */ 1452 int rc = vdbeCommit(db); 1453 if( rc==SQLITE_BUSY ){ 1454 sqlite3BtreeMutexArrayLeave(&p->aMutex); 1455 return SQLITE_BUSY; 1456 }else if( rc!=SQLITE_OK ){ 1457 p->rc = rc; 1458 sqlite3RollbackAll(db); 1459 }else{ 1460 sqlite3CommitInternalChanges(db); 1461 } 1462 }else{ 1463 sqlite3RollbackAll(db); 1464 } 1465 }else if( !xFunc ){ 1466 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ 1467 if( p->openedStatement ){ 1468 xFunc = sqlite3BtreeCommitStmt; 1469 } 1470 }else if( p->errorAction==OE_Abort ){ 1471 xFunc = sqlite3BtreeRollbackStmt; 1472 }else{ 1473 invalidateCursorsOnModifiedBtrees(db); 1474 sqlite3RollbackAll(db); 1475 db->autoCommit = 1; 1476 } 1477 } 1478 1479 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or 1480 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs 1481 ** and the return code is still SQLITE_OK, set the return code to the new 1482 ** error value. 1483 */ 1484 assert(!xFunc || 1485 xFunc==sqlite3BtreeCommitStmt || 1486 xFunc==sqlite3BtreeRollbackStmt 1487 ); 1488 for(i=0; xFunc && i<db->nDb; i++){ 1489 int rc; 1490 Btree *pBt = db->aDb[i].pBt; 1491 if( pBt ){ 1492 rc = xFunc(pBt); 1493 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){ 1494 p->rc = rc; 1495 sqlite3SetString(&p->zErrMsg, 0); 1496 } 1497 } 1498 } 1499 1500 /* If this was an INSERT, UPDATE or DELETE and the statement was committed, 1501 ** set the change counter. 1502 */ 1503 if( p->changeCntOn && p->pc>=0 ){ 1504 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){ 1505 sqlite3VdbeSetChanges(db, p->nChange); 1506 }else{ 1507 sqlite3VdbeSetChanges(db, 0); 1508 } 1509 p->nChange = 0; 1510 } 1511 1512 /* Rollback or commit any schema changes that occurred. */ 1513 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){ 1514 sqlite3ResetInternalSchema(db, 0); 1515 db->flags = (db->flags | SQLITE_InternChanges); 1516 } 1517 1518 /* Release the locks */ 1519 sqlite3BtreeMutexArrayLeave(&p->aMutex); 1520 } 1521 1522 /* We have successfully halted and closed the VM. Record this fact. */ 1523 if( p->pc>=0 ){ 1524 db->activeVdbeCnt--; 1525 } 1526 p->magic = VDBE_MAGIC_HALT; 1527 checkActiveVdbeCnt(db); 1528 if( p->db->mallocFailed ){ 1529 p->rc = SQLITE_NOMEM; 1530 } 1531 checkActiveVdbeCnt(db); 1532 1533 return SQLITE_OK; 1534 } 1535 1536 1537 /* 1538 ** Each VDBE holds the result of the most recent sqlite3_step() call 1539 ** in p->rc. This routine sets that result back to SQLITE_OK. 1540 */ 1541 void sqlite3VdbeResetStepResult(Vdbe *p){ 1542 p->rc = SQLITE_OK; 1543 } 1544 1545 /* 1546 ** Clean up a VDBE after execution but do not delete the VDBE just yet. 1547 ** Write any error messages into *pzErrMsg. Return the result code. 1548 ** 1549 ** After this routine is run, the VDBE should be ready to be executed 1550 ** again. 1551 ** 1552 ** To look at it another way, this routine resets the state of the 1553 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to 1554 ** VDBE_MAGIC_INIT. 1555 */ 1556 int sqlite3VdbeReset(Vdbe *p){ 1557 sqlite3 *db; 1558 db = p->db; 1559 1560 /* If the VM did not run to completion or if it encountered an 1561 ** error, then it might not have been halted properly. So halt 1562 ** it now. 1563 */ 1564 sqlite3SafetyOn(db); 1565 sqlite3VdbeHalt(p); 1566 sqlite3SafetyOff(db); 1567 1568 /* If the VDBE has be run even partially, then transfer the error code 1569 ** and error message from the VDBE into the main database structure. But 1570 ** if the VDBE has just been set to run but has not actually executed any 1571 ** instructions yet, leave the main database error information unchanged. 1572 */ 1573 if( p->pc>=0 ){ 1574 if( p->zErrMsg ){ 1575 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,sqlite3_free); 1576 db->errCode = p->rc; 1577 p->zErrMsg = 0; 1578 }else if( p->rc ){ 1579 sqlite3Error(db, p->rc, 0); 1580 }else{ 1581 sqlite3Error(db, SQLITE_OK, 0); 1582 } 1583 }else if( p->rc && p->expired ){ 1584 /* The expired flag was set on the VDBE before the first call 1585 ** to sqlite3_step(). For consistency (since sqlite3_step() was 1586 ** called), set the database error in this case as well. 1587 */ 1588 sqlite3Error(db, p->rc, 0); 1589 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3_free); 1590 p->zErrMsg = 0; 1591 } 1592 1593 /* Reclaim all memory used by the VDBE 1594 */ 1595 Cleanup(p); 1596 1597 /* Save profiling information from this VDBE run. 1598 */ 1599 assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack ); 1600 #ifdef VDBE_PROFILE 1601 { 1602 FILE *out = fopen("vdbe_profile.out", "a"); 1603 if( out ){ 1604 int i; 1605 fprintf(out, "---- "); 1606 for(i=0; i<p->nOp; i++){ 1607 fprintf(out, "%02x", p->aOp[i].opcode); 1608 } 1609 fprintf(out, "\n"); 1610 for(i=0; i<p->nOp; i++){ 1611 fprintf(out, "%6d %10lld %8lld ", 1612 p->aOp[i].cnt, 1613 p->aOp[i].cycles, 1614 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 1615 ); 1616 sqlite3VdbePrintOp(out, i, &p->aOp[i]); 1617 } 1618 fclose(out); 1619 } 1620 } 1621 #endif 1622 p->magic = VDBE_MAGIC_INIT; 1623 p->aborted = 0; 1624 return p->rc & db->errMask; 1625 } 1626 1627 /* 1628 ** Clean up and delete a VDBE after execution. Return an integer which is 1629 ** the result code. Write any error message text into *pzErrMsg. 1630 */ 1631 int sqlite3VdbeFinalize(Vdbe *p){ 1632 int rc = SQLITE_OK; 1633 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ 1634 rc = sqlite3VdbeReset(p); 1635 assert( (rc & p->db->errMask)==rc ); 1636 }else if( p->magic!=VDBE_MAGIC_INIT ){ 1637 return SQLITE_MISUSE; 1638 } 1639 sqlite3VdbeDelete(p); 1640 return rc; 1641 } 1642 1643 /* 1644 ** Call the destructor for each auxdata entry in pVdbeFunc for which 1645 ** the corresponding bit in mask is clear. Auxdata entries beyond 31 1646 ** are always destroyed. To destroy all auxdata entries, call this 1647 ** routine with mask==0. 1648 */ 1649 void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){ 1650 int i; 1651 for(i=0; i<pVdbeFunc->nAux; i++){ 1652 struct AuxData *pAux = &pVdbeFunc->apAux[i]; 1653 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){ 1654 if( pAux->xDelete ){ 1655 pAux->xDelete(pAux->pAux); 1656 } 1657 pAux->pAux = 0; 1658 } 1659 } 1660 } 1661 1662 /* 1663 ** Delete an entire VDBE. 1664 */ 1665 void sqlite3VdbeDelete(Vdbe *p){ 1666 int i; 1667 if( p==0 ) return; 1668 Cleanup(p); 1669 if( p->pPrev ){ 1670 p->pPrev->pNext = p->pNext; 1671 }else{ 1672 assert( p->db->pVdbe==p ); 1673 p->db->pVdbe = p->pNext; 1674 } 1675 if( p->pNext ){ 1676 p->pNext->pPrev = p->pPrev; 1677 } 1678 if( p->aOp ){ 1679 for(i=0; i<p->nOp; i++){ 1680 Op *pOp = &p->aOp[i]; 1681 freeP3(pOp->p3type, pOp->p3); 1682 } 1683 sqlite3_free(p->aOp); 1684 } 1685 releaseMemArray(p->aVar, p->nVar); 1686 sqlite3_free(p->aLabel); 1687 sqlite3_free(p->aStack); 1688 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); 1689 sqlite3_free(p->aColName); 1690 sqlite3_free(p->zSql); 1691 p->magic = VDBE_MAGIC_DEAD; 1692 sqlite3_free(p); 1693 } 1694 1695 /* 1696 ** If a MoveTo operation is pending on the given cursor, then do that 1697 ** MoveTo now. Return an error code. If no MoveTo is pending, this 1698 ** routine does nothing and returns SQLITE_OK. 1699 */ 1700 int sqlite3VdbeCursorMoveto(Cursor *p){ 1701 if( p->deferredMoveto ){ 1702 int res, rc; 1703 #ifdef SQLITE_TEST 1704 extern int sqlite3_search_count; 1705 #endif 1706 assert( p->isTable ); 1707 rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, 0, &res); 1708 if( rc ) return rc; 1709 *p->pIncrKey = 0; 1710 p->lastRowid = keyToInt(p->movetoTarget); 1711 p->rowidIsValid = res==0; 1712 if( res<0 ){ 1713 rc = sqlite3BtreeNext(p->pCursor, &res); 1714 if( rc ) return rc; 1715 } 1716 #ifdef SQLITE_TEST 1717 sqlite3_search_count++; 1718 #endif 1719 p->deferredMoveto = 0; 1720 p->cacheStatus = CACHE_STALE; 1721 } 1722 return SQLITE_OK; 1723 } 1724 1725 /* 1726 ** The following functions: 1727 ** 1728 ** sqlite3VdbeSerialType() 1729 ** sqlite3VdbeSerialTypeLen() 1730 ** sqlite3VdbeSerialRead() 1731 ** sqlite3VdbeSerialLen() 1732 ** sqlite3VdbeSerialWrite() 1733 ** 1734 ** encapsulate the code that serializes values for storage in SQLite 1735 ** data and index records. Each serialized value consists of a 1736 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned 1737 ** integer, stored as a varint. 1738 ** 1739 ** In an SQLite index record, the serial type is stored directly before 1740 ** the blob of data that it corresponds to. In a table record, all serial 1741 ** types are stored at the start of the record, and the blobs of data at 1742 ** the end. Hence these functions allow the caller to handle the 1743 ** serial-type and data blob seperately. 1744 ** 1745 ** The following table describes the various storage classes for data: 1746 ** 1747 ** serial type bytes of data type 1748 ** -------------- --------------- --------------- 1749 ** 0 0 NULL 1750 ** 1 1 signed integer 1751 ** 2 2 signed integer 1752 ** 3 3 signed integer 1753 ** 4 4 signed integer 1754 ** 5 6 signed integer 1755 ** 6 8 signed integer 1756 ** 7 8 IEEE float 1757 ** 8 0 Integer constant 0 1758 ** 9 0 Integer constant 1 1759 ** 10,11 reserved for expansion 1760 ** N>=12 and even (N-12)/2 BLOB 1761 ** N>=13 and odd (N-13)/2 text 1762 ** 1763 ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions 1764 ** of SQLite will not understand those serial types. 1765 */ 1766 1767 /* 1768 ** Return the serial-type for the value stored in pMem. 1769 */ 1770 u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ 1771 int flags = pMem->flags; 1772 int n; 1773 1774 if( flags&MEM_Null ){ 1775 return 0; 1776 } 1777 if( flags&MEM_Int ){ 1778 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ 1779 # define MAX_6BYTE ((((i64)0x00001000)<<32)-1) 1780 i64 i = pMem->u.i; 1781 u64 u; 1782 if( file_format>=4 && (i&1)==i ){ 1783 return 8+i; 1784 } 1785 u = i<0 ? -i : i; 1786 if( u<=127 ) return 1; 1787 if( u<=32767 ) return 2; 1788 if( u<=8388607 ) return 3; 1789 if( u<=2147483647 ) return 4; 1790 if( u<=MAX_6BYTE ) return 5; 1791 return 6; 1792 } 1793 if( flags&MEM_Real ){ 1794 return 7; 1795 } 1796 assert( flags&(MEM_Str|MEM_Blob) ); 1797 n = pMem->n; 1798 if( flags & MEM_Zero ){ 1799 n += pMem->u.i; 1800 } 1801 assert( n>=0 ); 1802 return ((n*2) + 12 + ((flags&MEM_Str)!=0)); 1803 } 1804 1805 /* 1806 ** Return the length of the data corresponding to the supplied serial-type. 1807 */ 1808 int sqlite3VdbeSerialTypeLen(u32 serial_type){ 1809 if( serial_type>=12 ){ 1810 return (serial_type-12)/2; 1811 }else{ 1812 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 }; 1813 return aSize[serial_type]; 1814 } 1815 } 1816 1817 /* 1818 ** If we are on an architecture with mixed-endian floating 1819 ** points (ex: ARM7) then swap the lower 4 bytes with the 1820 ** upper 4 bytes. Return the result. 1821 ** 1822 ** For most architectures, this is a no-op. 1823 ** 1824 ** (later): It is reported to me that the mixed-endian problem 1825 ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems 1826 ** that early versions of GCC stored the two words of a 64-bit 1827 ** float in the wrong order. And that error has been propagated 1828 ** ever since. The blame is not necessarily with GCC, though. 1829 ** GCC might have just copying the problem from a prior compiler. 1830 ** I am also told that newer versions of GCC that follow a different 1831 ** ABI get the byte order right. 1832 ** 1833 ** Developers using SQLite on an ARM7 should compile and run their 1834 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG 1835 ** enabled, some asserts below will ensure that the byte order of 1836 ** floating point values is correct. 1837 ** 1838 ** (2007-08-30) Frank van Vugt has studied this problem closely 1839 ** and has send his findings to the SQLite developers. Frank 1840 ** writes that some Linux kernels offer floating point hardware 1841 ** emulation that uses only 32-bit mantissas instead of a full 1842 ** 48-bits as required by the IEEE standard. (This is the 1843 ** CONFIG_FPE_FASTFPE option.) On such systems, floating point 1844 ** byte swapping becomes very complicated. To avoid problems, 1845 ** the necessary byte swapping is carried out using a 64-bit integer 1846 ** rather than a 64-bit float. Frank assures us that the code here 1847 ** works for him. We, the developers, have no way to independently 1848 ** verify this, but Frank seems to know what he is talking about 1849 ** so we trust him. 1850 */ 1851 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT 1852 static u64 floatSwap(u64 in){ 1853 union { 1854 u64 r; 1855 u32 i[2]; 1856 } u; 1857 u32 t; 1858 1859 u.r = in; 1860 t = u.i[0]; 1861 u.i[0] = u.i[1]; 1862 u.i[1] = t; 1863 return u.r; 1864 } 1865 # define swapMixedEndianFloat(X) X = floatSwap(X) 1866 #else 1867 # define swapMixedEndianFloat(X) 1868 #endif 1869 1870 /* 1871 ** Write the serialized data blob for the value stored in pMem into 1872 ** buf. It is assumed that the caller has allocated sufficient space. 1873 ** Return the number of bytes written. 1874 ** 1875 ** nBuf is the amount of space left in buf[]. nBuf must always be 1876 ** large enough to hold the entire field. Except, if the field is 1877 ** a blob with a zero-filled tail, then buf[] might be just the right 1878 ** size to hold everything except for the zero-filled tail. If buf[] 1879 ** is only big enough to hold the non-zero prefix, then only write that 1880 ** prefix into buf[]. But if buf[] is large enough to hold both the 1881 ** prefix and the tail then write the prefix and set the tail to all 1882 ** zeros. 1883 ** 1884 ** Return the number of bytes actually written into buf[]. The number 1885 ** of bytes in the zero-filled tail is included in the return value only 1886 ** if those bytes were zeroed in buf[]. 1887 */ 1888 int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){ 1889 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format); 1890 int len; 1891 1892 /* Integer and Real */ 1893 if( serial_type<=7 && serial_type>0 ){ 1894 u64 v; 1895 int i; 1896 if( serial_type==7 ){ 1897 assert( sizeof(v)==sizeof(pMem->r) ); 1898 memcpy(&v, &pMem->r, sizeof(v)); 1899 swapMixedEndianFloat(v); 1900 }else{ 1901 v = pMem->u.i; 1902 } 1903 len = i = sqlite3VdbeSerialTypeLen(serial_type); 1904 assert( len<=nBuf ); 1905 while( i-- ){ 1906 buf[i] = (v&0xFF); 1907 v >>= 8; 1908 } 1909 return len; 1910 } 1911 1912 /* String or blob */ 1913 if( serial_type>=12 ){ 1914 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0) 1915 == sqlite3VdbeSerialTypeLen(serial_type) ); 1916 assert( pMem->n<=nBuf ); 1917 len = pMem->n; 1918 memcpy(buf, pMem->z, len); 1919 if( pMem->flags & MEM_Zero ){ 1920 len += pMem->u.i; 1921 if( len>nBuf ){ 1922 len = nBuf; 1923 } 1924 memset(&buf[pMem->n], 0, len-pMem->n); 1925 } 1926 return len; 1927 } 1928 1929 /* NULL or constants 0 or 1 */ 1930 return 0; 1931 } 1932 1933 /* 1934 ** Deserialize the data blob pointed to by buf as serial type serial_type 1935 ** and store the result in pMem. Return the number of bytes read. 1936 */ 1937 int sqlite3VdbeSerialGet( 1938 const unsigned char *buf, /* Buffer to deserialize from */ 1939 u32 serial_type, /* Serial type to deserialize */ 1940 Mem *pMem /* Memory cell to write value into */ 1941 ){ 1942 switch( serial_type ){ 1943 case 10: /* Reserved for future use */ 1944 case 11: /* Reserved for future use */ 1945 case 0: { /* NULL */ 1946 pMem->flags = MEM_Null; 1947 break; 1948 } 1949 case 1: { /* 1-byte signed integer */ 1950 pMem->u.i = (signed char)buf[0]; 1951 pMem->flags = MEM_Int; 1952 return 1; 1953 } 1954 case 2: { /* 2-byte signed integer */ 1955 pMem->u.i = (((signed char)buf[0])<<8) | buf[1]; 1956 pMem->flags = MEM_Int; 1957 return 2; 1958 } 1959 case 3: { /* 3-byte signed integer */ 1960 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2]; 1961 pMem->flags = MEM_Int; 1962 return 3; 1963 } 1964 case 4: { /* 4-byte signed integer */ 1965 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; 1966 pMem->flags = MEM_Int; 1967 return 4; 1968 } 1969 case 5: { /* 6-byte signed integer */ 1970 u64 x = (((signed char)buf[0])<<8) | buf[1]; 1971 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; 1972 x = (x<<32) | y; 1973 pMem->u.i = *(i64*)&x; 1974 pMem->flags = MEM_Int; 1975 return 6; 1976 } 1977 case 6: /* 8-byte signed integer */ 1978 case 7: { /* IEEE floating point */ 1979 u64 x; 1980 u32 y; 1981 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) 1982 /* Verify that integers and floating point values use the same 1983 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is 1984 ** defined that 64-bit floating point values really are mixed 1985 ** endian. 1986 */ 1987 static const u64 t1 = ((u64)0x3ff00000)<<32; 1988 static const double r1 = 1.0; 1989 u64 t2 = t1; 1990 swapMixedEndianFloat(t2); 1991 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); 1992 #endif 1993 1994 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; 1995 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; 1996 x = (x<<32) | y; 1997 if( serial_type==6 ){ 1998 pMem->u.i = *(i64*)&x; 1999 pMem->flags = MEM_Int; 2000 }else{ 2001 assert( sizeof(x)==8 && sizeof(pMem->r)==8 ); 2002 swapMixedEndianFloat(x); 2003 memcpy(&pMem->r, &x, sizeof(x)); 2004 pMem->flags = MEM_Real; 2005 } 2006 return 8; 2007 } 2008 case 8: /* Integer 0 */ 2009 case 9: { /* Integer 1 */ 2010 pMem->u.i = serial_type-8; 2011 pMem->flags = MEM_Int; 2012 return 0; 2013 } 2014 default: { 2015 int len = (serial_type-12)/2; 2016 pMem->z = (char *)buf; 2017 pMem->n = len; 2018 pMem->xDel = 0; 2019 if( serial_type&0x01 ){ 2020 pMem->flags = MEM_Str | MEM_Ephem; 2021 }else{ 2022 pMem->flags = MEM_Blob | MEM_Ephem; 2023 } 2024 return len; 2025 } 2026 } 2027 return 0; 2028 } 2029 2030 /* 2031 ** The header of a record consists of a sequence variable-length integers. 2032 ** These integers are almost always small and are encoded as a single byte. 2033 ** The following macro takes advantage this fact to provide a fast decode 2034 ** of the integers in a record header. It is faster for the common case 2035 ** where the integer is a single byte. It is a little slower when the 2036 ** integer is two or more bytes. But overall it is faster. 2037 ** 2038 ** The following expressions are equivalent: 2039 ** 2040 ** x = sqlite3GetVarint32( A, &B ); 2041 ** 2042 ** x = GetVarint( A, B ); 2043 ** 2044 */ 2045 #define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B)) 2046 2047 /* 2048 ** This function compares the two table rows or index records specified by 2049 ** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero 2050 ** or positive integer if {nKey1, pKey1} is less than, equal to or 2051 ** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings 2052 ** composed by the OP_MakeRecord opcode of the VDBE. 2053 */ 2054 int sqlite3VdbeRecordCompare( 2055 void *userData, 2056 int nKey1, const void *pKey1, 2057 int nKey2, const void *pKey2 2058 ){ 2059 KeyInfo *pKeyInfo = (KeyInfo*)userData; 2060 u32 d1, d2; /* Offset into aKey[] of next data element */ 2061 u32 idx1, idx2; /* Offset into aKey[] of next header element */ 2062 u32 szHdr1, szHdr2; /* Number of bytes in header */ 2063 int i = 0; 2064 int nField; 2065 int rc = 0; 2066 const unsigned char *aKey1 = (const unsigned char *)pKey1; 2067 const unsigned char *aKey2 = (const unsigned char *)pKey2; 2068 2069 Mem mem1; 2070 Mem mem2; 2071 mem1.enc = pKeyInfo->enc; 2072 mem1.db = pKeyInfo->db; 2073 mem2.enc = pKeyInfo->enc; 2074 mem2.db = pKeyInfo->db; 2075 2076 idx1 = GetVarint(aKey1, szHdr1); 2077 d1 = szHdr1; 2078 idx2 = GetVarint(aKey2, szHdr2); 2079 d2 = szHdr2; 2080 nField = pKeyInfo->nField; 2081 while( idx1<szHdr1 && idx2<szHdr2 ){ 2082 u32 serial_type1; 2083 u32 serial_type2; 2084 2085 /* Read the serial types for the next element in each key. */ 2086 idx1 += GetVarint( aKey1+idx1, serial_type1 ); 2087 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break; 2088 idx2 += GetVarint( aKey2+idx2, serial_type2 ); 2089 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break; 2090 2091 /* Extract the values to be compared. 2092 */ 2093 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); 2094 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2); 2095 2096 /* Do the comparison 2097 */ 2098 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0); 2099 if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1); 2100 if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2); 2101 if( rc!=0 ){ 2102 break; 2103 } 2104 i++; 2105 } 2106 2107 /* One of the keys ran out of fields, but all the fields up to that point 2108 ** were equal. If the incrKey flag is true, then the second key is 2109 ** treated as larger. 2110 */ 2111 if( rc==0 ){ 2112 if( pKeyInfo->incrKey ){ 2113 rc = -1; 2114 }else if( d1<nKey1 ){ 2115 rc = 1; 2116 }else if( d2<nKey2 ){ 2117 rc = -1; 2118 } 2119 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField 2120 && pKeyInfo->aSortOrder[i] ){ 2121 rc = -rc; 2122 } 2123 2124 return rc; 2125 } 2126 2127 /* 2128 ** The argument is an index entry composed using the OP_MakeRecord opcode. 2129 ** The last entry in this record should be an integer (specifically 2130 ** an integer rowid). This routine returns the number of bytes in 2131 ** that integer. 2132 */ 2133 int sqlite3VdbeIdxRowidLen(const u8 *aKey){ 2134 u32 szHdr; /* Size of the header */ 2135 u32 typeRowid; /* Serial type of the rowid */ 2136 2137 sqlite3GetVarint32(aKey, &szHdr); 2138 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid); 2139 return sqlite3VdbeSerialTypeLen(typeRowid); 2140 } 2141 2142 2143 /* 2144 ** pCur points at an index entry created using the OP_MakeRecord opcode. 2145 ** Read the rowid (the last field in the record) and store it in *rowid. 2146 ** Return SQLITE_OK if everything works, or an error code otherwise. 2147 */ 2148 int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){ 2149 i64 nCellKey = 0; 2150 int rc; 2151 u32 szHdr; /* Size of the header */ 2152 u32 typeRowid; /* Serial type of the rowid */ 2153 u32 lenRowid; /* Size of the rowid */ 2154 Mem m, v; 2155 2156 sqlite3BtreeKeySize(pCur, &nCellKey); 2157 if( nCellKey<=0 ){ 2158 return SQLITE_CORRUPT_BKPT; 2159 } 2160 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m); 2161 if( rc ){ 2162 return rc; 2163 } 2164 sqlite3GetVarint32((u8*)m.z, &szHdr); 2165 sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid); 2166 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid); 2167 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); 2168 *rowid = v.u.i; 2169 sqlite3VdbeMemRelease(&m); 2170 return SQLITE_OK; 2171 } 2172 2173 /* 2174 ** Compare the key of the index entry that cursor pC is point to against 2175 ** the key string in pKey (of length nKey). Write into *pRes a number 2176 ** that is negative, zero, or positive if pC is less than, equal to, 2177 ** or greater than pKey. Return SQLITE_OK on success. 2178 ** 2179 ** pKey is either created without a rowid or is truncated so that it 2180 ** omits the rowid at the end. The rowid at the end of the index entry 2181 ** is ignored as well. 2182 */ 2183 int sqlite3VdbeIdxKeyCompare( 2184 Cursor *pC, /* The cursor to compare against */ 2185 int nKey, const u8 *pKey, /* The key to compare */ 2186 int *res /* Write the comparison result here */ 2187 ){ 2188 i64 nCellKey = 0; 2189 int rc; 2190 BtCursor *pCur = pC->pCursor; 2191 int lenRowid; 2192 Mem m; 2193 2194 sqlite3BtreeKeySize(pCur, &nCellKey); 2195 if( nCellKey<=0 ){ 2196 *res = 0; 2197 return SQLITE_OK; 2198 } 2199 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m); 2200 if( rc ){ 2201 return rc; 2202 } 2203 lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z); 2204 *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey); 2205 sqlite3VdbeMemRelease(&m); 2206 return SQLITE_OK; 2207 } 2208 2209 /* 2210 ** This routine sets the value to be returned by subsequent calls to 2211 ** sqlite3_changes() on the database handle 'db'. 2212 */ 2213 void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){ 2214 assert( sqlite3_mutex_held(db->mutex) ); 2215 db->nChange = nChange; 2216 db->nTotalChange += nChange; 2217 } 2218 2219 /* 2220 ** Set a flag in the vdbe to update the change counter when it is finalised 2221 ** or reset. 2222 */ 2223 void sqlite3VdbeCountChanges(Vdbe *v){ 2224 v->changeCntOn = 1; 2225 } 2226 2227 /* 2228 ** Mark every prepared statement associated with a database connection 2229 ** as expired. 2230 ** 2231 ** An expired statement means that recompilation of the statement is 2232 ** recommend. Statements expire when things happen that make their 2233 ** programs obsolete. Removing user-defined functions or collating 2234 ** sequences, or changing an authorization function are the types of 2235 ** things that make prepared statements obsolete. 2236 */ 2237 void sqlite3ExpirePreparedStatements(sqlite3 *db){ 2238 Vdbe *p; 2239 for(p = db->pVdbe; p; p=p->pNext){ 2240 p->expired = 1; 2241 } 2242 } 2243 2244 /* 2245 ** Return the database associated with the Vdbe. 2246 */ 2247 sqlite3 *sqlite3VdbeDb(Vdbe *v){ 2248 return v->db; 2249 } 2250