| OLD | NEW |
| (Empty) |
| 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 ** $Id: vdbeaux.c,v 1.480 2009/08/08 18:01:08 drh Exp $ | |
| 18 */ | |
| 19 #include "sqliteInt.h" | |
| 20 #include "vdbeInt.h" | |
| 21 | |
| 22 | |
| 23 | |
| 24 /* | |
| 25 ** When debugging the code generator in a symbolic debugger, one can | |
| 26 ** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed | |
| 27 ** as they are added to the instruction stream. | |
| 28 */ | |
| 29 #ifdef SQLITE_DEBUG | |
| 30 int sqlite3VdbeAddopTrace = 0; | |
| 31 #endif | |
| 32 | |
| 33 | |
| 34 /* | |
| 35 ** Create a new virtual database engine. | |
| 36 */ | |
| 37 Vdbe *sqlite3VdbeCreate(sqlite3 *db){ | |
| 38 Vdbe *p; | |
| 39 p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); | |
| 40 if( p==0 ) return 0; | |
| 41 p->db = db; | |
| 42 if( db->pVdbe ){ | |
| 43 db->pVdbe->pPrev = p; | |
| 44 } | |
| 45 p->pNext = db->pVdbe; | |
| 46 p->pPrev = 0; | |
| 47 db->pVdbe = p; | |
| 48 p->magic = VDBE_MAGIC_INIT; | |
| 49 return p; | |
| 50 } | |
| 51 | |
| 52 /* | |
| 53 ** Remember the SQL string for a prepared statement. | |
| 54 */ | |
| 55 void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){ | |
| 56 if( p==0 ) return; | |
| 57 #ifdef SQLITE_OMIT_TRACE | |
| 58 if( !isPrepareV2 ) return; | |
| 59 #endif | |
| 60 assert( p->zSql==0 ); | |
| 61 p->zSql = sqlite3DbStrNDup(p->db, z, n); | |
| 62 p->isPrepareV2 = isPrepareV2 ? 1 : 0; | |
| 63 } | |
| 64 | |
| 65 /* | |
| 66 ** Return the SQL associated with a prepared statement | |
| 67 */ | |
| 68 const char *sqlite3_sql(sqlite3_stmt *pStmt){ | |
| 69 Vdbe *p = (Vdbe *)pStmt; | |
| 70 return (p->isPrepareV2 ? p->zSql : 0); | |
| 71 } | |
| 72 | |
| 73 /* | |
| 74 ** Swap all content between two VDBE structures. | |
| 75 */ | |
| 76 void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ | |
| 77 Vdbe tmp, *pTmp; | |
| 78 char *zTmp; | |
| 79 tmp = *pA; | |
| 80 *pA = *pB; | |
| 81 *pB = tmp; | |
| 82 pTmp = pA->pNext; | |
| 83 pA->pNext = pB->pNext; | |
| 84 pB->pNext = pTmp; | |
| 85 pTmp = pA->pPrev; | |
| 86 pA->pPrev = pB->pPrev; | |
| 87 pB->pPrev = pTmp; | |
| 88 zTmp = pA->zSql; | |
| 89 pA->zSql = pB->zSql; | |
| 90 pB->zSql = zTmp; | |
| 91 pB->isPrepareV2 = pA->isPrepareV2; | |
| 92 } | |
| 93 | |
| 94 #ifdef SQLITE_DEBUG | |
| 95 /* | |
| 96 ** Turn tracing on or off | |
| 97 */ | |
| 98 void sqlite3VdbeTrace(Vdbe *p, FILE *trace){ | |
| 99 p->trace = trace; | |
| 100 } | |
| 101 #endif | |
| 102 | |
| 103 /* | |
| 104 ** Resize the Vdbe.aOp array so that it is at least one op larger than | |
| 105 ** it was. | |
| 106 ** | |
| 107 ** If an out-of-memory error occurs while resizing the array, return | |
| 108 ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain | |
| 109 ** unchanged (this is so that any opcodes already allocated can be | |
| 110 ** correctly deallocated along with the rest of the Vdbe). | |
| 111 */ | |
| 112 static int growOpArray(Vdbe *p){ | |
| 113 VdbeOp *pNew; | |
| 114 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op))); | |
| 115 pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op)); | |
| 116 if( pNew ){ | |
| 117 p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op); | |
| 118 p->aOp = pNew; | |
| 119 } | |
| 120 return (pNew ? SQLITE_OK : SQLITE_NOMEM); | |
| 121 } | |
| 122 | |
| 123 /* | |
| 124 ** Add a new instruction to the list of instructions current in the | |
| 125 ** VDBE. Return the address of the new instruction. | |
| 126 ** | |
| 127 ** Parameters: | |
| 128 ** | |
| 129 ** p Pointer to the VDBE | |
| 130 ** | |
| 131 ** op The opcode for this instruction | |
| 132 ** | |
| 133 ** p1, p2, p3 Operands | |
| 134 ** | |
| 135 ** Use the sqlite3VdbeResolveLabel() function to fix an address and | |
| 136 ** the sqlite3VdbeChangeP4() function to change the value of the P4 | |
| 137 ** operand. | |
| 138 */ | |
| 139 int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ | |
| 140 int i; | |
| 141 VdbeOp *pOp; | |
| 142 | |
| 143 i = p->nOp; | |
| 144 assert( p->magic==VDBE_MAGIC_INIT ); | |
| 145 assert( op>0 && op<0xff ); | |
| 146 if( p->nOpAlloc<=i ){ | |
| 147 if( growOpArray(p) ){ | |
| 148 return 1; | |
| 149 } | |
| 150 } | |
| 151 p->nOp++; | |
| 152 pOp = &p->aOp[i]; | |
| 153 pOp->opcode = (u8)op; | |
| 154 pOp->p5 = 0; | |
| 155 pOp->p1 = p1; | |
| 156 pOp->p2 = p2; | |
| 157 pOp->p3 = p3; | |
| 158 pOp->p4.p = 0; | |
| 159 pOp->p4type = P4_NOTUSED; | |
| 160 p->expired = 0; | |
| 161 #ifdef SQLITE_DEBUG | |
| 162 pOp->zComment = 0; | |
| 163 if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]); | |
| 164 #endif | |
| 165 #ifdef VDBE_PROFILE | |
| 166 pOp->cycles = 0; | |
| 167 pOp->cnt = 0; | |
| 168 #endif | |
| 169 return i; | |
| 170 } | |
| 171 int sqlite3VdbeAddOp0(Vdbe *p, int op){ | |
| 172 return sqlite3VdbeAddOp3(p, op, 0, 0, 0); | |
| 173 } | |
| 174 int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ | |
| 175 return sqlite3VdbeAddOp3(p, op, p1, 0, 0); | |
| 176 } | |
| 177 int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ | |
| 178 return sqlite3VdbeAddOp3(p, op, p1, p2, 0); | |
| 179 } | |
| 180 | |
| 181 | |
| 182 /* | |
| 183 ** Add an opcode that includes the p4 value as a pointer. | |
| 184 */ | |
| 185 int sqlite3VdbeAddOp4( | |
| 186 Vdbe *p, /* Add the opcode to this VM */ | |
| 187 int op, /* The new opcode */ | |
| 188 int p1, /* The P1 operand */ | |
| 189 int p2, /* The P2 operand */ | |
| 190 int p3, /* The P3 operand */ | |
| 191 const char *zP4, /* The P4 operand */ | |
| 192 int p4type /* P4 operand type */ | |
| 193 ){ | |
| 194 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); | |
| 195 sqlite3VdbeChangeP4(p, addr, zP4, p4type); | |
| 196 return addr; | |
| 197 } | |
| 198 | |
| 199 /* | |
| 200 ** Create a new symbolic label for an instruction that has yet to be | |
| 201 ** coded. The symbolic label is really just a negative number. The | |
| 202 ** label can be used as the P2 value of an operation. Later, when | |
| 203 ** the label is resolved to a specific address, the VDBE will scan | |
| 204 ** through its operation list and change all values of P2 which match | |
| 205 ** the label into the resolved address. | |
| 206 ** | |
| 207 ** The VDBE knows that a P2 value is a label because labels are | |
| 208 ** always negative and P2 values are suppose to be non-negative. | |
| 209 ** Hence, a negative P2 value is a label that has yet to be resolved. | |
| 210 ** | |
| 211 ** Zero is returned if a malloc() fails. | |
| 212 */ | |
| 213 int sqlite3VdbeMakeLabel(Vdbe *p){ | |
| 214 int i; | |
| 215 i = p->nLabel++; | |
| 216 assert( p->magic==VDBE_MAGIC_INIT ); | |
| 217 if( i>=p->nLabelAlloc ){ | |
| 218 int n = p->nLabelAlloc*2 + 5; | |
| 219 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, | |
| 220 n*sizeof(p->aLabel[0])); | |
| 221 p->nLabelAlloc = sqlite3DbMallocSize(p->db, p->aLabel)/sizeof(p->aLabel[0]); | |
| 222 } | |
| 223 if( p->aLabel ){ | |
| 224 p->aLabel[i] = -1; | |
| 225 } | |
| 226 return -1-i; | |
| 227 } | |
| 228 | |
| 229 /* | |
| 230 ** Resolve label "x" to be the address of the next instruction to | |
| 231 ** be inserted. The parameter "x" must have been obtained from | |
| 232 ** a prior call to sqlite3VdbeMakeLabel(). | |
| 233 */ | |
| 234 void sqlite3VdbeResolveLabel(Vdbe *p, int x){ | |
| 235 int j = -1-x; | |
| 236 assert( p->magic==VDBE_MAGIC_INIT ); | |
| 237 assert( j>=0 && j<p->nLabel ); | |
| 238 if( p->aLabel ){ | |
| 239 p->aLabel[j] = p->nOp; | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 #ifdef SQLITE_DEBUG | |
| 244 | |
| 245 /* | |
| 246 ** The following type and function are used to iterate through all opcodes | |
| 247 ** in a Vdbe main program and each of the sub-programs (triggers) it may | |
| 248 ** invoke directly or indirectly. It should be used as follows: | |
| 249 ** | |
| 250 ** Op *pOp; | |
| 251 ** VdbeOpIter sIter; | |
| 252 ** | |
| 253 ** memset(&sIter, 0, sizeof(sIter)); | |
| 254 ** sIter.v = v; // v is of type Vdbe* | |
| 255 ** while( (pOp = opIterNext(&sIter)) ){ | |
| 256 ** // Do something with pOp | |
| 257 ** } | |
| 258 ** sqlite3DbFree(v->db, sIter.apSub); | |
| 259 ** | |
| 260 */ | |
| 261 typedef struct VdbeOpIter VdbeOpIter; | |
| 262 struct VdbeOpIter { | |
| 263 Vdbe *v; /* Vdbe to iterate through the opcodes of */ | |
| 264 SubProgram **apSub; /* Array of subprograms */ | |
| 265 int nSub; /* Number of entries in apSub */ | |
| 266 int iAddr; /* Address of next instruction to return */ | |
| 267 int iSub; /* 0 = main program, 1 = first sub-program etc. */ | |
| 268 }; | |
| 269 static Op *opIterNext(VdbeOpIter *p){ | |
| 270 Vdbe *v = p->v; | |
| 271 Op *pRet = 0; | |
| 272 Op *aOp; | |
| 273 int nOp; | |
| 274 | |
| 275 if( p->iSub<=p->nSub ){ | |
| 276 | |
| 277 if( p->iSub==0 ){ | |
| 278 aOp = v->aOp; | |
| 279 nOp = v->nOp; | |
| 280 }else{ | |
| 281 aOp = p->apSub[p->iSub-1]->aOp; | |
| 282 nOp = p->apSub[p->iSub-1]->nOp; | |
| 283 } | |
| 284 assert( p->iAddr<nOp ); | |
| 285 | |
| 286 pRet = &aOp[p->iAddr]; | |
| 287 p->iAddr++; | |
| 288 if( p->iAddr==nOp ){ | |
| 289 p->iSub++; | |
| 290 p->iAddr = 0; | |
| 291 } | |
| 292 | |
| 293 if( pRet->p4type==P4_SUBPROGRAM ){ | |
| 294 int nByte = (p->nSub+1)*sizeof(SubProgram*); | |
| 295 int j; | |
| 296 for(j=0; j<p->nSub; j++){ | |
| 297 if( p->apSub[j]==pRet->p4.pProgram ) break; | |
| 298 } | |
| 299 if( j==p->nSub ){ | |
| 300 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte); | |
| 301 if( !p->apSub ){ | |
| 302 pRet = 0; | |
| 303 }else{ | |
| 304 p->apSub[p->nSub++] = pRet->p4.pProgram; | |
| 305 } | |
| 306 } | |
| 307 } | |
| 308 } | |
| 309 | |
| 310 return pRet; | |
| 311 } | |
| 312 | |
| 313 /* | |
| 314 ** Check if the program stored in the VM associated with pParse may | |
| 315 ** throw an ABORT exception (causing the statement, but not transaction | |
| 316 ** to be rolled back). This condition is true if the main program or any | |
| 317 ** sub-programs contains any of the following: | |
| 318 ** | |
| 319 ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort. | |
| 320 ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort. | |
| 321 ** * OP_Destroy | |
| 322 ** * OP_VUpdate | |
| 323 ** * OP_VRename | |
| 324 ** | |
| 325 ** Then check that the value of Parse.mayAbort is true if an | |
| 326 ** ABORT may be thrown, or false otherwise. Return true if it does | |
| 327 ** match, or false otherwise. This function is intended to be used as | |
| 328 ** part of an assert statement in the compiler. Similar to: | |
| 329 ** | |
| 330 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) ); | |
| 331 */ | |
| 332 int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){ | |
| 333 int hasAbort = 0; | |
| 334 Op *pOp; | |
| 335 VdbeOpIter sIter; | |
| 336 memset(&sIter, 0, sizeof(sIter)); | |
| 337 sIter.v = v; | |
| 338 | |
| 339 while( (pOp = opIterNext(&sIter))!=0 ){ | |
| 340 int opcode = pOp->opcode; | |
| 341 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename | |
| 342 || ((opcode==OP_Halt || opcode==OP_HaltIfNull) | |
| 343 && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort)) | |
| 344 ){ | |
| 345 hasAbort = 1; | |
| 346 break; | |
| 347 } | |
| 348 } | |
| 349 sqlite3DbFree(v->db, sIter.apSub); | |
| 350 | |
| 351 /* Return true if hasAbort==mayAbort. Or if a malloc failure occured. | |
| 352 ** If malloc failed, then the while() loop above may not have iterated | |
| 353 ** through all opcodes and hasAbort may be set incorrectly. Return | |
| 354 ** true for this case to prevent the assert() in the callers frame | |
| 355 ** from failing. */ | |
| 356 return ( v->db->mallocFailed || hasAbort==mayAbort ); | |
| 357 } | |
| 358 #endif | |
| 359 | |
| 360 /* | |
| 361 ** Loop through the program looking for P2 values that are negative | |
| 362 ** on jump instructions. Each such value is a label. Resolve the | |
| 363 ** label by setting the P2 value to its correct non-zero value. | |
| 364 ** | |
| 365 ** This routine is called once after all opcodes have been inserted. | |
| 366 ** | |
| 367 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument | |
| 368 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by | |
| 369 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array. | |
| 370 */ | |
| 371 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ | |
| 372 int i; | |
| 373 int nMaxArgs = *pMaxFuncArgs; | |
| 374 Op *pOp; | |
| 375 int *aLabel = p->aLabel; | |
| 376 p->readOnly = 1; | |
| 377 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ | |
| 378 u8 opcode = pOp->opcode; | |
| 379 | |
| 380 if( opcode==OP_Function || opcode==OP_AggStep ){ | |
| 381 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5; | |
| 382 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 383 }else if( opcode==OP_VUpdate ){ | |
| 384 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; | |
| 385 #endif | |
| 386 }else if( opcode==OP_Transaction && pOp->p2!=0 ){ | |
| 387 p->readOnly = 0; | |
| 388 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 389 }else if( opcode==OP_VFilter ){ | |
| 390 int n; | |
| 391 assert( p->nOp - i >= 3 ); | |
| 392 assert( pOp[-1].opcode==OP_Integer ); | |
| 393 n = pOp[-1].p1; | |
| 394 if( n>nMaxArgs ) nMaxArgs = n; | |
| 395 #endif | |
| 396 } | |
| 397 | |
| 398 if( sqlite3VdbeOpcodeHasProperty(opcode, OPFLG_JUMP) && pOp->p2<0 ){ | |
| 399 assert( -1-pOp->p2<p->nLabel ); | |
| 400 pOp->p2 = aLabel[-1-pOp->p2]; | |
| 401 } | |
| 402 } | |
| 403 sqlite3DbFree(p->db, p->aLabel); | |
| 404 p->aLabel = 0; | |
| 405 | |
| 406 *pMaxFuncArgs = nMaxArgs; | |
| 407 } | |
| 408 | |
| 409 /* | |
| 410 ** Return the address of the next instruction to be inserted. | |
| 411 */ | |
| 412 int sqlite3VdbeCurrentAddr(Vdbe *p){ | |
| 413 assert( p->magic==VDBE_MAGIC_INIT ); | |
| 414 return p->nOp; | |
| 415 } | |
| 416 | |
| 417 /* | |
| 418 ** This function returns a pointer to the array of opcodes associated with | |
| 419 ** the Vdbe passed as the first argument. It is the callers responsibility | |
| 420 ** to arrange for the returned array to be eventually freed using the | |
| 421 ** vdbeFreeOpArray() function. | |
| 422 ** | |
| 423 ** Before returning, *pnOp is set to the number of entries in the returned | |
| 424 ** array. Also, *pnMaxArg is set to the larger of its current value and | |
| 425 ** the number of entries in the Vdbe.apArg[] array required to execute the | |
| 426 ** returned program. | |
| 427 */ | |
| 428 VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){ | |
| 429 VdbeOp *aOp = p->aOp; | |
| 430 assert( aOp && !p->db->mallocFailed ); | |
| 431 | |
| 432 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */ | |
| 433 assert( p->aMutex.nMutex==0 ); | |
| 434 | |
| 435 resolveP2Values(p, pnMaxArg); | |
| 436 *pnOp = p->nOp; | |
| 437 p->aOp = 0; | |
| 438 return aOp; | |
| 439 } | |
| 440 | |
| 441 /* | |
| 442 ** Add a whole list of operations to the operation stack. Return the | |
| 443 ** address of the first operation added. | |
| 444 */ | |
| 445 int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ | |
| 446 int addr; | |
| 447 assert( p->magic==VDBE_MAGIC_INIT ); | |
| 448 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){ | |
| 449 return 0; | |
| 450 } | |
| 451 addr = p->nOp; | |
| 452 if( ALWAYS(nOp>0) ){ | |
| 453 int i; | |
| 454 VdbeOpList const *pIn = aOp; | |
| 455 for(i=0; i<nOp; i++, pIn++){ | |
| 456 int p2 = pIn->p2; | |
| 457 VdbeOp *pOut = &p->aOp[i+addr]; | |
| 458 pOut->opcode = pIn->opcode; | |
| 459 pOut->p1 = pIn->p1; | |
| 460 if( p2<0 && sqlite3VdbeOpcodeHasProperty(pOut->opcode, OPFLG_JUMP) ){ | |
| 461 pOut->p2 = addr + ADDR(p2); | |
| 462 }else{ | |
| 463 pOut->p2 = p2; | |
| 464 } | |
| 465 pOut->p3 = pIn->p3; | |
| 466 pOut->p4type = P4_NOTUSED; | |
| 467 pOut->p4.p = 0; | |
| 468 pOut->p5 = 0; | |
| 469 #ifdef SQLITE_DEBUG | |
| 470 pOut->zComment = 0; | |
| 471 if( sqlite3VdbeAddopTrace ){ | |
| 472 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); | |
| 473 } | |
| 474 #endif | |
| 475 } | |
| 476 p->nOp += nOp; | |
| 477 } | |
| 478 return addr; | |
| 479 } | |
| 480 | |
| 481 /* | |
| 482 ** Change the value of the P1 operand for a specific instruction. | |
| 483 ** This routine is useful when a large program is loaded from a | |
| 484 ** static array using sqlite3VdbeAddOpList but we want to make a | |
| 485 ** few minor changes to the program. | |
| 486 */ | |
| 487 void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ | |
| 488 assert( p!=0 ); | |
| 489 assert( addr>=0 ); | |
| 490 if( p->nOp>addr ){ | |
| 491 p->aOp[addr].p1 = val; | |
| 492 } | |
| 493 } | |
| 494 | |
| 495 /* | |
| 496 ** Change the value of the P2 operand for a specific instruction. | |
| 497 ** This routine is useful for setting a jump destination. | |
| 498 */ | |
| 499 void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ | |
| 500 assert( p!=0 ); | |
| 501 assert( addr>=0 ); | |
| 502 if( p->nOp>addr ){ | |
| 503 p->aOp[addr].p2 = val; | |
| 504 } | |
| 505 } | |
| 506 | |
| 507 /* | |
| 508 ** Change the value of the P3 operand for a specific instruction. | |
| 509 */ | |
| 510 void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){ | |
| 511 assert( p!=0 ); | |
| 512 assert( addr>=0 ); | |
| 513 if( p->nOp>addr ){ | |
| 514 p->aOp[addr].p3 = val; | |
| 515 } | |
| 516 } | |
| 517 | |
| 518 /* | |
| 519 ** Change the value of the P5 operand for the most recently | |
| 520 ** added operation. | |
| 521 */ | |
| 522 void sqlite3VdbeChangeP5(Vdbe *p, u8 val){ | |
| 523 assert( p!=0 ); | |
| 524 if( p->aOp ){ | |
| 525 assert( p->nOp>0 ); | |
| 526 p->aOp[p->nOp-1].p5 = val; | |
| 527 } | |
| 528 } | |
| 529 | |
| 530 /* | |
| 531 ** Change the P2 operand of instruction addr so that it points to | |
| 532 ** the address of the next instruction to be coded. | |
| 533 */ | |
| 534 void sqlite3VdbeJumpHere(Vdbe *p, int addr){ | |
| 535 sqlite3VdbeChangeP2(p, addr, p->nOp); | |
| 536 } | |
| 537 | |
| 538 | |
| 539 /* | |
| 540 ** If the input FuncDef structure is ephemeral, then free it. If | |
| 541 ** the FuncDef is not ephermal, then do nothing. | |
| 542 */ | |
| 543 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ | |
| 544 if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){ | |
| 545 sqlite3DbFree(db, pDef); | |
| 546 } | |
| 547 } | |
| 548 | |
| 549 /* | |
| 550 ** Delete a P4 value if necessary. | |
| 551 */ | |
| 552 static void freeP4(sqlite3 *db, int p4type, void *p4){ | |
| 553 if( p4 ){ | |
| 554 switch( p4type ){ | |
| 555 case P4_REAL: | |
| 556 case P4_INT64: | |
| 557 case P4_MPRINTF: | |
| 558 case P4_DYNAMIC: | |
| 559 case P4_KEYINFO: | |
| 560 case P4_INTARRAY: | |
| 561 case P4_KEYINFO_HANDOFF: { | |
| 562 sqlite3DbFree(db, p4); | |
| 563 break; | |
| 564 } | |
| 565 case P4_VDBEFUNC: { | |
| 566 VdbeFunc *pVdbeFunc = (VdbeFunc *)p4; | |
| 567 freeEphemeralFunction(db, pVdbeFunc->pFunc); | |
| 568 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0); | |
| 569 sqlite3DbFree(db, pVdbeFunc); | |
| 570 break; | |
| 571 } | |
| 572 case P4_FUNCDEF: { | |
| 573 freeEphemeralFunction(db, (FuncDef*)p4); | |
| 574 break; | |
| 575 } | |
| 576 case P4_MEM: { | |
| 577 sqlite3ValueFree((sqlite3_value*)p4); | |
| 578 break; | |
| 579 } | |
| 580 case P4_VTAB : { | |
| 581 sqlite3VtabUnlock((VTable *)p4); | |
| 582 break; | |
| 583 } | |
| 584 case P4_SUBPROGRAM : { | |
| 585 sqlite3VdbeProgramDelete(db, (SubProgram *)p4, 1); | |
| 586 break; | |
| 587 } | |
| 588 } | |
| 589 } | |
| 590 } | |
| 591 | |
| 592 /* | |
| 593 ** Free the space allocated for aOp and any p4 values allocated for the | |
| 594 ** opcodes contained within. If aOp is not NULL it is assumed to contain | |
| 595 ** nOp entries. | |
| 596 */ | |
| 597 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){ | |
| 598 if( aOp ){ | |
| 599 Op *pOp; | |
| 600 for(pOp=aOp; pOp<&aOp[nOp]; pOp++){ | |
| 601 freeP4(db, pOp->p4type, pOp->p4.p); | |
| 602 #ifdef SQLITE_DEBUG | |
| 603 sqlite3DbFree(db, pOp->zComment); | |
| 604 #endif | |
| 605 } | |
| 606 } | |
| 607 sqlite3DbFree(db, aOp); | |
| 608 } | |
| 609 | |
| 610 /* | |
| 611 ** Decrement the ref-count on the SubProgram structure passed as the | |
| 612 ** second argument. If the ref-count reaches zero, free the structure. | |
| 613 ** | |
| 614 ** The array of VDBE opcodes stored as SubProgram.aOp is freed if | |
| 615 ** either the ref-count reaches zero or parameter freeop is non-zero. | |
| 616 ** | |
| 617 ** Since the array of opcodes pointed to by SubProgram.aOp may directly | |
| 618 ** or indirectly contain a reference to the SubProgram structure itself. | |
| 619 ** By passing a non-zero freeop parameter, the caller may ensure that all | |
| 620 ** SubProgram structures and their aOp arrays are freed, even when there | |
| 621 ** are such circular references. | |
| 622 */ | |
| 623 void sqlite3VdbeProgramDelete(sqlite3 *db, SubProgram *p, int freeop){ | |
| 624 if( p ){ | |
| 625 assert( p->nRef>0 ); | |
| 626 if( freeop || p->nRef==1 ){ | |
| 627 Op *aOp = p->aOp; | |
| 628 p->aOp = 0; | |
| 629 vdbeFreeOpArray(db, aOp, p->nOp); | |
| 630 p->nOp = 0; | |
| 631 } | |
| 632 p->nRef--; | |
| 633 if( p->nRef==0 ){ | |
| 634 sqlite3DbFree(db, p); | |
| 635 } | |
| 636 } | |
| 637 } | |
| 638 | |
| 639 | |
| 640 /* | |
| 641 ** Change N opcodes starting at addr to No-ops. | |
| 642 */ | |
| 643 void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ | |
| 644 if( p->aOp ){ | |
| 645 VdbeOp *pOp = &p->aOp[addr]; | |
| 646 sqlite3 *db = p->db; | |
| 647 while( N-- ){ | |
| 648 freeP4(db, pOp->p4type, pOp->p4.p); | |
| 649 memset(pOp, 0, sizeof(pOp[0])); | |
| 650 pOp->opcode = OP_Noop; | |
| 651 pOp++; | |
| 652 } | |
| 653 } | |
| 654 } | |
| 655 | |
| 656 /* | |
| 657 ** Change the value of the P4 operand for a specific instruction. | |
| 658 ** This routine is useful when a large program is loaded from a | |
| 659 ** static array using sqlite3VdbeAddOpList but we want to make a | |
| 660 ** few minor changes to the program. | |
| 661 ** | |
| 662 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of | |
| 663 ** the string is made into memory obtained from sqlite3_malloc(). | |
| 664 ** A value of n==0 means copy bytes of zP4 up to and including the | |
| 665 ** first null byte. If n>0 then copy n+1 bytes of zP4. | |
| 666 ** | |
| 667 ** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure. | |
| 668 ** A copy is made of the KeyInfo structure into memory obtained from | |
| 669 ** sqlite3_malloc, to be freed when the Vdbe is finalized. | |
| 670 ** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure | |
| 671 ** stored in memory that the caller has obtained from sqlite3_malloc. The | |
| 672 ** caller should not free the allocation, it will be freed when the Vdbe is | |
| 673 ** finalized. | |
| 674 ** | |
| 675 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points | |
| 676 ** to a string or structure that is guaranteed to exist for the lifetime of | |
| 677 ** the Vdbe. In these cases we can just copy the pointer. | |
| 678 ** | |
| 679 ** If addr<0 then change P4 on the most recently inserted instruction. | |
| 680 */ | |
| 681 void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ | |
| 682 Op *pOp; | |
| 683 sqlite3 *db; | |
| 684 assert( p!=0 ); | |
| 685 db = p->db; | |
| 686 assert( p->magic==VDBE_MAGIC_INIT ); | |
| 687 if( p->aOp==0 || db->mallocFailed ){ | |
| 688 if ( n!=P4_KEYINFO && n!=P4_VTAB ) { | |
| 689 freeP4(db, n, (void*)*(char**)&zP4); | |
| 690 } | |
| 691 return; | |
| 692 } | |
| 693 assert( p->nOp>0 ); | |
| 694 assert( addr<p->nOp ); | |
| 695 if( addr<0 ){ | |
| 696 addr = p->nOp - 1; | |
| 697 } | |
| 698 pOp = &p->aOp[addr]; | |
| 699 freeP4(db, pOp->p4type, pOp->p4.p); | |
| 700 pOp->p4.p = 0; | |
| 701 if( n==P4_INT32 ){ | |
| 702 /* Note: this cast is safe, because the origin data point was an int | |
| 703 ** that was cast to a (const char *). */ | |
| 704 pOp->p4.i = SQLITE_PTR_TO_INT(zP4); | |
| 705 pOp->p4type = P4_INT32; | |
| 706 }else if( zP4==0 ){ | |
| 707 pOp->p4.p = 0; | |
| 708 pOp->p4type = P4_NOTUSED; | |
| 709 }else if( n==P4_KEYINFO ){ | |
| 710 KeyInfo *pKeyInfo; | |
| 711 int nField, nByte; | |
| 712 | |
| 713 nField = ((KeyInfo*)zP4)->nField; | |
| 714 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; | |
| 715 pKeyInfo = sqlite3Malloc( nByte ); | |
| 716 pOp->p4.pKeyInfo = pKeyInfo; | |
| 717 if( pKeyInfo ){ | |
| 718 u8 *aSortOrder; | |
| 719 memcpy(pKeyInfo, zP4, nByte); | |
| 720 aSortOrder = pKeyInfo->aSortOrder; | |
| 721 if( aSortOrder ){ | |
| 722 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField]; | |
| 723 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField); | |
| 724 } | |
| 725 pOp->p4type = P4_KEYINFO; | |
| 726 }else{ | |
| 727 p->db->mallocFailed = 1; | |
| 728 pOp->p4type = P4_NOTUSED; | |
| 729 } | |
| 730 }else if( n==P4_KEYINFO_HANDOFF ){ | |
| 731 pOp->p4.p = (void*)zP4; | |
| 732 pOp->p4type = P4_KEYINFO; | |
| 733 }else if( n==P4_VTAB ){ | |
| 734 pOp->p4.p = (void*)zP4; | |
| 735 pOp->p4type = P4_VTAB; | |
| 736 sqlite3VtabLock((VTable *)zP4); | |
| 737 assert( ((VTable *)zP4)->db==p->db ); | |
| 738 }else if( n<0 ){ | |
| 739 pOp->p4.p = (void*)zP4; | |
| 740 pOp->p4type = (signed char)n; | |
| 741 }else{ | |
| 742 if( n==0 ) n = sqlite3Strlen30(zP4); | |
| 743 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n); | |
| 744 pOp->p4type = P4_DYNAMIC; | |
| 745 } | |
| 746 } | |
| 747 | |
| 748 #ifndef NDEBUG | |
| 749 /* | |
| 750 ** Change the comment on the the most recently coded instruction. Or | |
| 751 ** insert a No-op and add the comment to that new instruction. This | |
| 752 ** makes the code easier to read during debugging. None of this happens | |
| 753 ** in a production build. | |
| 754 */ | |
| 755 void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ | |
| 756 va_list ap; | |
| 757 if( !p ) return; | |
| 758 assert( p->nOp>0 || p->aOp==0 ); | |
| 759 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); | |
| 760 if( p->nOp ){ | |
| 761 char **pz = &p->aOp[p->nOp-1].zComment; | |
| 762 va_start(ap, zFormat); | |
| 763 sqlite3DbFree(p->db, *pz); | |
| 764 *pz = sqlite3VMPrintf(p->db, zFormat, ap); | |
| 765 va_end(ap); | |
| 766 } | |
| 767 } | |
| 768 void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ | |
| 769 va_list ap; | |
| 770 if( !p ) return; | |
| 771 sqlite3VdbeAddOp0(p, OP_Noop); | |
| 772 assert( p->nOp>0 || p->aOp==0 ); | |
| 773 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); | |
| 774 if( p->nOp ){ | |
| 775 char **pz = &p->aOp[p->nOp-1].zComment; | |
| 776 va_start(ap, zFormat); | |
| 777 sqlite3DbFree(p->db, *pz); | |
| 778 *pz = sqlite3VMPrintf(p->db, zFormat, ap); | |
| 779 va_end(ap); | |
| 780 } | |
| 781 } | |
| 782 #endif /* NDEBUG */ | |
| 783 | |
| 784 /* | |
| 785 ** Return the opcode for a given address. If the address is -1, then | |
| 786 ** return the most recently inserted opcode. | |
| 787 ** | |
| 788 ** If a memory allocation error has occurred prior to the calling of this | |
| 789 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode | |
| 790 ** is readable and writable, but it has no effect. The return of a dummy | |
| 791 ** opcode allows the call to continue functioning after a OOM fault without | |
| 792 ** having to check to see if the return from this routine is a valid pointer. | |
| 793 ** | |
| 794 ** About the #ifdef SQLITE_OMIT_TRACE: Normally, this routine is never called | |
| 795 ** unless p->nOp>0. This is because in the absense of SQLITE_OMIT_TRACE, | |
| 796 ** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as | |
| 797 ** a new VDBE is created. So we are free to set addr to p->nOp-1 without | |
| 798 ** having to double-check to make sure that the result is non-negative. But | |
| 799 ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to | |
| 800 ** check the value of p->nOp-1 before continuing. | |
| 801 */ | |
| 802 VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ | |
| 803 static VdbeOp dummy; | |
| 804 assert( p->magic==VDBE_MAGIC_INIT ); | |
| 805 if( addr<0 ){ | |
| 806 #ifdef SQLITE_OMIT_TRACE | |
| 807 if( p->nOp==0 ) return &dummy; | |
| 808 #endif | |
| 809 addr = p->nOp - 1; | |
| 810 } | |
| 811 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); | |
| 812 if( p->db->mallocFailed ){ | |
| 813 return &dummy; | |
| 814 }else{ | |
| 815 return &p->aOp[addr]; | |
| 816 } | |
| 817 } | |
| 818 | |
| 819 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ | |
| 820 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) | |
| 821 /* | |
| 822 ** Compute a string that describes the P4 parameter for an opcode. | |
| 823 ** Use zTemp for any required temporary buffer space. | |
| 824 */ | |
| 825 static char *displayP4(Op *pOp, char *zTemp, int nTemp){ | |
| 826 char *zP4 = zTemp; | |
| 827 assert( nTemp>=20 ); | |
| 828 switch( pOp->p4type ){ | |
| 829 case P4_KEYINFO_STATIC: | |
| 830 case P4_KEYINFO: { | |
| 831 int i, j; | |
| 832 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; | |
| 833 sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField); | |
| 834 i = sqlite3Strlen30(zTemp); | |
| 835 for(j=0; j<pKeyInfo->nField; j++){ | |
| 836 CollSeq *pColl = pKeyInfo->aColl[j]; | |
| 837 if( pColl ){ | |
| 838 int n = sqlite3Strlen30(pColl->zName); | |
| 839 if( i+n>nTemp-6 ){ | |
| 840 memcpy(&zTemp[i],",...",4); | |
| 841 break; | |
| 842 } | |
| 843 zTemp[i++] = ','; | |
| 844 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){ | |
| 845 zTemp[i++] = '-'; | |
| 846 } | |
| 847 memcpy(&zTemp[i], pColl->zName,n+1); | |
| 848 i += n; | |
| 849 }else if( i+4<nTemp-6 ){ | |
| 850 memcpy(&zTemp[i],",nil",4); | |
| 851 i += 4; | |
| 852 } | |
| 853 } | |
| 854 zTemp[i++] = ')'; | |
| 855 zTemp[i] = 0; | |
| 856 assert( i<nTemp ); | |
| 857 break; | |
| 858 } | |
| 859 case P4_COLLSEQ: { | |
| 860 CollSeq *pColl = pOp->p4.pColl; | |
| 861 sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName); | |
| 862 break; | |
| 863 } | |
| 864 case P4_FUNCDEF: { | |
| 865 FuncDef *pDef = pOp->p4.pFunc; | |
| 866 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); | |
| 867 break; | |
| 868 } | |
| 869 case P4_INT64: { | |
| 870 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64); | |
| 871 break; | |
| 872 } | |
| 873 case P4_INT32: { | |
| 874 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i); | |
| 875 break; | |
| 876 } | |
| 877 case P4_REAL: { | |
| 878 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal); | |
| 879 break; | |
| 880 } | |
| 881 case P4_MEM: { | |
| 882 Mem *pMem = pOp->p4.pMem; | |
| 883 assert( (pMem->flags & MEM_Null)==0 ); | |
| 884 if( pMem->flags & MEM_Str ){ | |
| 885 zP4 = pMem->z; | |
| 886 }else if( pMem->flags & MEM_Int ){ | |
| 887 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i); | |
| 888 }else if( pMem->flags & MEM_Real ){ | |
| 889 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r); | |
| 890 }else{ | |
| 891 assert( pMem->flags & MEM_Blob ); | |
| 892 zP4 = "(blob)"; | |
| 893 } | |
| 894 break; | |
| 895 } | |
| 896 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 897 case P4_VTAB: { | |
| 898 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab; | |
| 899 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); | |
| 900 break; | |
| 901 } | |
| 902 #endif | |
| 903 case P4_INTARRAY: { | |
| 904 sqlite3_snprintf(nTemp, zTemp, "intarray"); | |
| 905 break; | |
| 906 } | |
| 907 case P4_SUBPROGRAM: { | |
| 908 sqlite3_snprintf(nTemp, zTemp, "program"); | |
| 909 break; | |
| 910 } | |
| 911 default: { | |
| 912 zP4 = pOp->p4.z; | |
| 913 if( zP4==0 ){ | |
| 914 zP4 = zTemp; | |
| 915 zTemp[0] = 0; | |
| 916 } | |
| 917 } | |
| 918 } | |
| 919 assert( zP4!=0 ); | |
| 920 return zP4; | |
| 921 } | |
| 922 #endif | |
| 923 | |
| 924 /* | |
| 925 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. | |
| 926 */ | |
| 927 void sqlite3VdbeUsesBtree(Vdbe *p, int i){ | |
| 928 int mask; | |
| 929 assert( i>=0 && i<p->db->nDb && i<sizeof(u32)*8 ); | |
| 930 assert( i<(int)sizeof(p->btreeMask)*8 ); | |
| 931 mask = ((u32)1)<<i; | |
| 932 if( (p->btreeMask & mask)==0 ){ | |
| 933 p->btreeMask |= mask; | |
| 934 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt); | |
| 935 } | |
| 936 } | |
| 937 | |
| 938 | |
| 939 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) | |
| 940 /* | |
| 941 ** Print a single opcode. This routine is used for debugging only. | |
| 942 */ | |
| 943 void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ | |
| 944 char *zP4; | |
| 945 char zPtr[50]; | |
| 946 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n"; | |
| 947 if( pOut==0 ) pOut = stdout; | |
| 948 zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); | |
| 949 fprintf(pOut, zFormat1, pc, | |
| 950 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, | |
| 951 #ifdef SQLITE_DEBUG | |
| 952 pOp->zComment ? pOp->zComment : "" | |
| 953 #else | |
| 954 "" | |
| 955 #endif | |
| 956 ); | |
| 957 fflush(pOut); | |
| 958 } | |
| 959 #endif | |
| 960 | |
| 961 /* | |
| 962 ** Release an array of N Mem elements | |
| 963 */ | |
| 964 static void releaseMemArray(Mem *p, int N){ | |
| 965 if( p && N ){ | |
| 966 Mem *pEnd; | |
| 967 sqlite3 *db = p->db; | |
| 968 u8 malloc_failed = db->mallocFailed; | |
| 969 for(pEnd=&p[N]; p<pEnd; p++){ | |
| 970 assert( (&p[1])==pEnd || p[0].db==p[1].db ); | |
| 971 | |
| 972 /* This block is really an inlined version of sqlite3VdbeMemRelease() | |
| 973 ** that takes advantage of the fact that the memory cell value is | |
| 974 ** being set to NULL after releasing any dynamic resources. | |
| 975 ** | |
| 976 ** The justification for duplicating code is that according to | |
| 977 ** callgrind, this causes a certain test case to hit the CPU 4.7 | |
| 978 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if | |
| 979 ** sqlite3MemRelease() were called from here. With -O2, this jumps | |
| 980 ** to 6.6 percent. The test case is inserting 1000 rows into a table | |
| 981 ** with no indexes using a single prepared INSERT statement, bind() | |
| 982 ** and reset(). Inserts are grouped into a transaction. | |
| 983 */ | |
| 984 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){ | |
| 985 sqlite3VdbeMemRelease(p); | |
| 986 }else if( p->zMalloc ){ | |
| 987 sqlite3DbFree(db, p->zMalloc); | |
| 988 p->zMalloc = 0; | |
| 989 } | |
| 990 | |
| 991 p->flags = MEM_Null; | |
| 992 } | |
| 993 db->mallocFailed = malloc_failed; | |
| 994 } | |
| 995 } | |
| 996 | |
| 997 /* | |
| 998 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are | |
| 999 ** allocated by the OP_Program opcode in sqlite3VdbeExec(). | |
| 1000 */ | |
| 1001 void sqlite3VdbeFrameDelete(VdbeFrame *p){ | |
| 1002 int i; | |
| 1003 Mem *aMem = VdbeFrameMem(p); | |
| 1004 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem]; | |
| 1005 for(i=0; i<p->nChildCsr; i++){ | |
| 1006 sqlite3VdbeFreeCursor(p->v, apCsr[i]); | |
| 1007 } | |
| 1008 releaseMemArray(aMem, p->nChildMem); | |
| 1009 sqlite3DbFree(p->v->db, p); | |
| 1010 } | |
| 1011 | |
| 1012 | |
| 1013 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT | |
| 1014 int sqlite3VdbeReleaseBuffers(Vdbe *p){ | |
| 1015 int ii; | |
| 1016 int nFree = 0; | |
| 1017 assert( sqlite3_mutex_held(p->db->mutex) ); | |
| 1018 for(ii=1; ii<=p->nMem; ii++){ | |
| 1019 Mem *pMem = &p->aMem[ii]; | |
| 1020 if( pMem->flags & MEM_RowSet ){ | |
| 1021 sqlite3RowSetClear(pMem->u.pRowSet); | |
| 1022 } | |
| 1023 if( pMem->z && pMem->flags&MEM_Dyn ){ | |
| 1024 assert( !pMem->xDel ); | |
| 1025 nFree += sqlite3DbMallocSize(pMem->db, pMem->z); | |
| 1026 sqlite3VdbeMemRelease(pMem); | |
| 1027 } | |
| 1028 } | |
| 1029 return nFree; | |
| 1030 } | |
| 1031 #endif | |
| 1032 | |
| 1033 #ifndef SQLITE_OMIT_EXPLAIN | |
| 1034 /* | |
| 1035 ** Give a listing of the program in the virtual machine. | |
| 1036 ** | |
| 1037 ** The interface is the same as sqlite3VdbeExec(). But instead of | |
| 1038 ** running the code, it invokes the callback once for each instruction. | |
| 1039 ** This feature is used to implement "EXPLAIN". | |
| 1040 ** | |
| 1041 ** When p->explain==1, each instruction is listed. When | |
| 1042 ** p->explain==2, only OP_Explain instructions are listed and these | |
| 1043 ** are shown in a different format. p->explain==2 is used to implement | |
| 1044 ** EXPLAIN QUERY PLAN. | |
| 1045 */ | |
| 1046 int sqlite3VdbeList( | |
| 1047 Vdbe *p /* The VDBE */ | |
| 1048 ){ | |
| 1049 int nRow; /* Total number of rows to return */ | |
| 1050 int nSub = 0; /* Number of sub-vdbes seen so far */ | |
| 1051 SubProgram **apSub = 0; /* Array of sub-vdbes */ | |
| 1052 Mem *pSub = 0; | |
| 1053 sqlite3 *db = p->db; | |
| 1054 int i; | |
| 1055 int rc = SQLITE_OK; | |
| 1056 Mem *pMem = p->pResultSet = &p->aMem[1]; | |
| 1057 | |
| 1058 assert( p->explain ); | |
| 1059 assert( p->magic==VDBE_MAGIC_RUN ); | |
| 1060 assert( db->magic==SQLITE_MAGIC_BUSY ); | |
| 1061 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); | |
| 1062 | |
| 1063 /* Even though this opcode does not use dynamic strings for | |
| 1064 ** the result, result columns may become dynamic if the user calls | |
| 1065 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. | |
| 1066 */ | |
| 1067 releaseMemArray(pMem, 8); | |
| 1068 | |
| 1069 if( p->rc==SQLITE_NOMEM ){ | |
| 1070 /* This happens if a malloc() inside a call to sqlite3_column_text() or | |
| 1071 ** sqlite3_column_text16() failed. */ | |
| 1072 db->mallocFailed = 1; | |
| 1073 return SQLITE_ERROR; | |
| 1074 } | |
| 1075 | |
| 1076 /* Figure out total number of rows that will be returned by this | |
| 1077 ** EXPLAIN program. */ | |
| 1078 nRow = p->nOp; | |
| 1079 if( p->explain==1 ){ | |
| 1080 pSub = &p->aMem[9]; | |
| 1081 if( pSub->flags&MEM_Blob ){ | |
| 1082 nSub = pSub->n/sizeof(Vdbe*); | |
| 1083 apSub = (SubProgram **)pSub->z; | |
| 1084 } | |
| 1085 for(i=0; i<nSub; i++){ | |
| 1086 nRow += apSub[i]->nOp; | |
| 1087 } | |
| 1088 } | |
| 1089 | |
| 1090 do{ | |
| 1091 i = p->pc++; | |
| 1092 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain ); | |
| 1093 if( i>=nRow ){ | |
| 1094 p->rc = SQLITE_OK; | |
| 1095 rc = SQLITE_DONE; | |
| 1096 }else if( db->u1.isInterrupted ){ | |
| 1097 p->rc = SQLITE_INTERRUPT; | |
| 1098 rc = SQLITE_ERROR; | |
| 1099 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc)); | |
| 1100 }else{ | |
| 1101 char *z; | |
| 1102 Op *pOp; | |
| 1103 if( i<p->nOp ){ | |
| 1104 pOp = &p->aOp[i]; | |
| 1105 }else{ | |
| 1106 int j; | |
| 1107 i -= p->nOp; | |
| 1108 for(j=0; i>=apSub[j]->nOp; j++){ | |
| 1109 i -= apSub[j]->nOp; | |
| 1110 } | |
| 1111 pOp = &apSub[j]->aOp[i]; | |
| 1112 } | |
| 1113 if( p->explain==1 ){ | |
| 1114 pMem->flags = MEM_Int; | |
| 1115 pMem->type = SQLITE_INTEGER; | |
| 1116 pMem->u.i = i; /* Program counter */ | |
| 1117 pMem++; | |
| 1118 | |
| 1119 pMem->flags = MEM_Static|MEM_Str|MEM_Term; | |
| 1120 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ | |
| 1121 assert( pMem->z!=0 ); | |
| 1122 pMem->n = sqlite3Strlen30(pMem->z); | |
| 1123 pMem->type = SQLITE_TEXT; | |
| 1124 pMem->enc = SQLITE_UTF8; | |
| 1125 pMem++; | |
| 1126 | |
| 1127 if( pOp->p4type==P4_SUBPROGRAM ){ | |
| 1128 int nByte = (nSub+1)*sizeof(SubProgram*); | |
| 1129 int j; | |
| 1130 for(j=0; j<nSub; j++){ | |
| 1131 if( apSub[j]==pOp->p4.pProgram ) break; | |
| 1132 } | |
| 1133 if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, 1) ){ | |
| 1134 apSub = (SubProgram **)pSub->z; | |
| 1135 apSub[nSub++] = pOp->p4.pProgram; | |
| 1136 pSub->flags |= MEM_Blob; | |
| 1137 pSub->n = nSub*sizeof(SubProgram*); | |
| 1138 } | |
| 1139 } | |
| 1140 } | |
| 1141 | |
| 1142 pMem->flags = MEM_Int; | |
| 1143 pMem->u.i = pOp->p1; /* P1 */ | |
| 1144 pMem->type = SQLITE_INTEGER; | |
| 1145 pMem++; | |
| 1146 | |
| 1147 pMem->flags = MEM_Int; | |
| 1148 pMem->u.i = pOp->p2; /* P2 */ | |
| 1149 pMem->type = SQLITE_INTEGER; | |
| 1150 pMem++; | |
| 1151 | |
| 1152 if( p->explain==1 ){ | |
| 1153 pMem->flags = MEM_Int; | |
| 1154 pMem->u.i = pOp->p3; /* P3 */ | |
| 1155 pMem->type = SQLITE_INTEGER; | |
| 1156 pMem++; | |
| 1157 } | |
| 1158 | |
| 1159 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */ | |
| 1160 assert( p->db->mallocFailed ); | |
| 1161 return SQLITE_ERROR; | |
| 1162 } | |
| 1163 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; | |
| 1164 z = displayP4(pOp, pMem->z, 32); | |
| 1165 if( z!=pMem->z ){ | |
| 1166 sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0); | |
| 1167 }else{ | |
| 1168 assert( pMem->z!=0 ); | |
| 1169 pMem->n = sqlite3Strlen30(pMem->z); | |
| 1170 pMem->enc = SQLITE_UTF8; | |
| 1171 } | |
| 1172 pMem->type = SQLITE_TEXT; | |
| 1173 pMem++; | |
| 1174 | |
| 1175 if( p->explain==1 ){ | |
| 1176 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){ | |
| 1177 assert( p->db->mallocFailed ); | |
| 1178 return SQLITE_ERROR; | |
| 1179 } | |
| 1180 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; | |
| 1181 pMem->n = 2; | |
| 1182 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ | |
| 1183 pMem->type = SQLITE_TEXT; | |
| 1184 pMem->enc = SQLITE_UTF8; | |
| 1185 pMem++; | |
| 1186 | |
| 1187 #ifdef SQLITE_DEBUG | |
| 1188 if( pOp->zComment ){ | |
| 1189 pMem->flags = MEM_Str|MEM_Term; | |
| 1190 pMem->z = pOp->zComment; | |
| 1191 pMem->n = sqlite3Strlen30(pMem->z); | |
| 1192 pMem->enc = SQLITE_UTF8; | |
| 1193 pMem->type = SQLITE_TEXT; | |
| 1194 }else | |
| 1195 #endif | |
| 1196 { | |
| 1197 pMem->flags = MEM_Null; /* Comment */ | |
| 1198 pMem->type = SQLITE_NULL; | |
| 1199 } | |
| 1200 } | |
| 1201 | |
| 1202 p->nResColumn = 8 - 5*(p->explain-1); | |
| 1203 p->rc = SQLITE_OK; | |
| 1204 rc = SQLITE_ROW; | |
| 1205 } | |
| 1206 return rc; | |
| 1207 } | |
| 1208 #endif /* SQLITE_OMIT_EXPLAIN */ | |
| 1209 | |
| 1210 #ifdef SQLITE_DEBUG | |
| 1211 /* | |
| 1212 ** Print the SQL that was used to generate a VDBE program. | |
| 1213 */ | |
| 1214 void sqlite3VdbePrintSql(Vdbe *p){ | |
| 1215 int nOp = p->nOp; | |
| 1216 VdbeOp *pOp; | |
| 1217 if( nOp<1 ) return; | |
| 1218 pOp = &p->aOp[0]; | |
| 1219 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ | |
| 1220 const char *z = pOp->p4.z; | |
| 1221 while( sqlite3Isspace(*z) ) z++; | |
| 1222 printf("SQL: [%s]\n", z); | |
| 1223 } | |
| 1224 } | |
| 1225 #endif | |
| 1226 | |
| 1227 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) | |
| 1228 /* | |
| 1229 ** Print an IOTRACE message showing SQL content. | |
| 1230 */ | |
| 1231 void sqlite3VdbeIOTraceSql(Vdbe *p){ | |
| 1232 int nOp = p->nOp; | |
| 1233 VdbeOp *pOp; | |
| 1234 if( sqlite3IoTrace==0 ) return; | |
| 1235 if( nOp<1 ) return; | |
| 1236 pOp = &p->aOp[0]; | |
| 1237 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ | |
| 1238 int i, j; | |
| 1239 char z[1000]; | |
| 1240 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z); | |
| 1241 for(i=0; sqlite3Isspace(z[i]); i++){} | |
| 1242 for(j=0; z[i]; i++){ | |
| 1243 if( sqlite3Isspace(z[i]) ){ | |
| 1244 if( z[i-1]!=' ' ){ | |
| 1245 z[j++] = ' '; | |
| 1246 } | |
| 1247 }else{ | |
| 1248 z[j++] = z[i]; | |
| 1249 } | |
| 1250 } | |
| 1251 z[j] = 0; | |
| 1252 sqlite3IoTrace("SQL %s\n", z); | |
| 1253 } | |
| 1254 } | |
| 1255 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */ | |
| 1256 | |
| 1257 /* | |
| 1258 ** Allocate space from a fixed size buffer. Make *pp point to the | |
| 1259 ** allocated space. (Note: pp is a char* rather than a void** to | |
| 1260 ** work around the pointer aliasing rules of C.) *pp should initially | |
| 1261 ** be zero. If *pp is not zero, that means that the space has already | |
| 1262 ** been allocated and this routine is a noop. | |
| 1263 ** | |
| 1264 ** nByte is the number of bytes of space needed. | |
| 1265 ** | |
| 1266 ** *ppFrom point to available space and pEnd points to the end of the | |
| 1267 ** available space. | |
| 1268 ** | |
| 1269 ** *pnByte is a counter of the number of bytes of space that have failed | |
| 1270 ** to allocate. If there is insufficient space in *ppFrom to satisfy the | |
| 1271 ** request, then increment *pnByte by the amount of the request. | |
| 1272 */ | |
| 1273 static void allocSpace( | |
| 1274 char *pp, /* IN/OUT: Set *pp to point to allocated buffer */ | |
| 1275 int nByte, /* Number of bytes to allocate */ | |
| 1276 u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */ | |
| 1277 u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */ | |
| 1278 int *pnByte /* If allocation cannot be made, increment *pnByte */ | |
| 1279 ){ | |
| 1280 assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) ); | |
| 1281 if( (*(void**)pp)==0 ){ | |
| 1282 nByte = ROUND8(nByte); | |
| 1283 if( &(*ppFrom)[nByte] <= pEnd ){ | |
| 1284 *(void**)pp = (void *)*ppFrom; | |
| 1285 *ppFrom += nByte; | |
| 1286 }else{ | |
| 1287 *pnByte += nByte; | |
| 1288 } | |
| 1289 } | |
| 1290 } | |
| 1291 | |
| 1292 /* | |
| 1293 ** Prepare a virtual machine for execution. This involves things such | |
| 1294 ** as allocating stack space and initializing the program counter. | |
| 1295 ** After the VDBE has be prepped, it can be executed by one or more | |
| 1296 ** calls to sqlite3VdbeExec(). | |
| 1297 ** | |
| 1298 ** This is the only way to move a VDBE from VDBE_MAGIC_INIT to | |
| 1299 ** VDBE_MAGIC_RUN. | |
| 1300 ** | |
| 1301 ** This function may be called more than once on a single virtual machine. | |
| 1302 ** The first call is made while compiling the SQL statement. Subsequent | |
| 1303 ** calls are made as part of the process of resetting a statement to be | |
| 1304 ** re-executed (from a call to sqlite3_reset()). The nVar, nMem, nCursor | |
| 1305 ** and isExplain parameters are only passed correct values the first time | |
| 1306 ** the function is called. On subsequent calls, from sqlite3_reset(), nVar | |
| 1307 ** is passed -1 and nMem, nCursor and isExplain are all passed zero. | |
| 1308 */ | |
| 1309 void sqlite3VdbeMakeReady( | |
| 1310 Vdbe *p, /* The VDBE */ | |
| 1311 int nVar, /* Number of '?' see in the SQL statement */ | |
| 1312 int nMem, /* Number of memory cells to allocate */ | |
| 1313 int nCursor, /* Number of cursors to allocate */ | |
| 1314 int nArg, /* Maximum number of args in SubPrograms */ | |
| 1315 int isExplain, /* True if the EXPLAIN keywords is present */ | |
| 1316 int usesStmtJournal /* True to set Vdbe.usesStmtJournal */ | |
| 1317 ){ | |
| 1318 int n; | |
| 1319 sqlite3 *db = p->db; | |
| 1320 | |
| 1321 assert( p!=0 ); | |
| 1322 assert( p->magic==VDBE_MAGIC_INIT ); | |
| 1323 | |
| 1324 /* There should be at least one opcode. | |
| 1325 */ | |
| 1326 assert( p->nOp>0 ); | |
| 1327 | |
| 1328 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */ | |
| 1329 p->magic = VDBE_MAGIC_RUN; | |
| 1330 | |
| 1331 /* For each cursor required, also allocate a memory cell. Memory | |
| 1332 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by | |
| 1333 ** the vdbe program. Instead they are used to allocate space for | |
| 1334 ** VdbeCursor/BtCursor structures. The blob of memory associated with | |
| 1335 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1) | |
| 1336 ** stores the blob of memory associated with cursor 1, etc. | |
| 1337 ** | |
| 1338 ** See also: allocateCursor(). | |
| 1339 */ | |
| 1340 nMem += nCursor; | |
| 1341 | |
| 1342 /* Allocate space for memory registers, SQL variables, VDBE cursors and | |
| 1343 ** an array to marshal SQL function arguments in. This is only done the | |
| 1344 ** first time this function is called for a given VDBE, not when it is | |
| 1345 ** being called from sqlite3_reset() to reset the virtual machine. | |
| 1346 */ | |
| 1347 if( nVar>=0 && ALWAYS(db->mallocFailed==0) ){ | |
| 1348 u8 *zCsr = (u8 *)&p->aOp[p->nOp]; | |
| 1349 u8 *zEnd = (u8 *)&p->aOp[p->nOpAlloc]; | |
| 1350 int nByte; | |
| 1351 resolveP2Values(p, &nArg); | |
| 1352 p->usesStmtJournal = (u8)usesStmtJournal; | |
| 1353 if( isExplain && nMem<10 ){ | |
| 1354 nMem = 10; | |
| 1355 } | |
| 1356 memset(zCsr, 0, zEnd-zCsr); | |
| 1357 zCsr += (zCsr - (u8*)0)&7; | |
| 1358 assert( EIGHT_BYTE_ALIGNMENT(zCsr) ); | |
| 1359 | |
| 1360 do { | |
| 1361 nByte = 0; | |
| 1362 allocSpace((char*)&p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte); | |
| 1363 allocSpace((char*)&p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte); | |
| 1364 allocSpace((char*)&p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte); | |
| 1365 allocSpace((char*)&p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte); | |
| 1366 allocSpace((char*)&p->apCsr, | |
| 1367 nCursor*sizeof(VdbeCursor*), &zCsr, zEnd, &nByte | |
| 1368 ); | |
| 1369 if( nByte ){ | |
| 1370 p->pFree = sqlite3DbMallocZero(db, nByte); | |
| 1371 } | |
| 1372 zCsr = p->pFree; | |
| 1373 zEnd = &zCsr[nByte]; | |
| 1374 }while( nByte && !db->mallocFailed ); | |
| 1375 | |
| 1376 p->nCursor = (u16)nCursor; | |
| 1377 if( p->aVar ){ | |
| 1378 p->nVar = (u16)nVar; | |
| 1379 for(n=0; n<nVar; n++){ | |
| 1380 p->aVar[n].flags = MEM_Null; | |
| 1381 p->aVar[n].db = db; | |
| 1382 } | |
| 1383 } | |
| 1384 if( p->aMem ){ | |
| 1385 p->aMem--; /* aMem[] goes from 1..nMem */ | |
| 1386 p->nMem = nMem; /* not from 0..nMem-1 */ | |
| 1387 for(n=1; n<=nMem; n++){ | |
| 1388 p->aMem[n].flags = MEM_Null; | |
| 1389 p->aMem[n].db = db; | |
| 1390 } | |
| 1391 } | |
| 1392 } | |
| 1393 #ifdef SQLITE_DEBUG | |
| 1394 for(n=1; n<p->nMem; n++){ | |
| 1395 assert( p->aMem[n].db==db ); | |
| 1396 } | |
| 1397 #endif | |
| 1398 | |
| 1399 p->pc = -1; | |
| 1400 p->rc = SQLITE_OK; | |
| 1401 p->errorAction = OE_Abort; | |
| 1402 p->explain |= isExplain; | |
| 1403 p->magic = VDBE_MAGIC_RUN; | |
| 1404 p->nChange = 0; | |
| 1405 p->cacheCtr = 1; | |
| 1406 p->minWriteFileFormat = 255; | |
| 1407 p->iStatement = 0; | |
| 1408 #ifdef VDBE_PROFILE | |
| 1409 { | |
| 1410 int i; | |
| 1411 for(i=0; i<p->nOp; i++){ | |
| 1412 p->aOp[i].cnt = 0; | |
| 1413 p->aOp[i].cycles = 0; | |
| 1414 } | |
| 1415 } | |
| 1416 #endif | |
| 1417 } | |
| 1418 | |
| 1419 /* | |
| 1420 ** Close a VDBE cursor and release all the resources that cursor | |
| 1421 ** happens to hold. | |
| 1422 */ | |
| 1423 void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ | |
| 1424 if( pCx==0 ){ | |
| 1425 return; | |
| 1426 } | |
| 1427 if( pCx->pBt ){ | |
| 1428 sqlite3BtreeClose(pCx->pBt); | |
| 1429 /* The pCx->pCursor will be close automatically, if it exists, by | |
| 1430 ** the call above. */ | |
| 1431 }else if( pCx->pCursor ){ | |
| 1432 sqlite3BtreeCloseCursor(pCx->pCursor); | |
| 1433 } | |
| 1434 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 1435 if( pCx->pVtabCursor ){ | |
| 1436 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; | |
| 1437 const sqlite3_module *pModule = pCx->pModule; | |
| 1438 p->inVtabMethod = 1; | |
| 1439 (void)sqlite3SafetyOff(p->db); | |
| 1440 pModule->xClose(pVtabCursor); | |
| 1441 (void)sqlite3SafetyOn(p->db); | |
| 1442 p->inVtabMethod = 0; | |
| 1443 } | |
| 1444 #endif | |
| 1445 } | |
| 1446 | |
| 1447 /* | |
| 1448 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This | |
| 1449 ** is used, for example, when a trigger sub-program is halted to restore | |
| 1450 ** control to the main program. | |
| 1451 */ | |
| 1452 int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ | |
| 1453 Vdbe *v = pFrame->v; | |
| 1454 v->aOp = pFrame->aOp; | |
| 1455 v->nOp = pFrame->nOp; | |
| 1456 v->aMem = pFrame->aMem; | |
| 1457 v->nMem = pFrame->nMem; | |
| 1458 v->apCsr = pFrame->apCsr; | |
| 1459 v->nCursor = pFrame->nCursor; | |
| 1460 v->db->lastRowid = pFrame->lastRowid; | |
| 1461 v->nChange = pFrame->nChange; | |
| 1462 return pFrame->pc; | |
| 1463 } | |
| 1464 | |
| 1465 /* | |
| 1466 ** Close all cursors. | |
| 1467 ** | |
| 1468 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory | |
| 1469 ** cell array. This is necessary as the memory cell array may contain | |
| 1470 ** pointers to VdbeFrame objects, which may in turn contain pointers to | |
| 1471 ** open cursors. | |
| 1472 */ | |
| 1473 static void closeAllCursors(Vdbe *p){ | |
| 1474 if( p->pFrame ){ | |
| 1475 VdbeFrame *pFrame = p->pFrame; | |
| 1476 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); | |
| 1477 sqlite3VdbeFrameRestore(pFrame); | |
| 1478 } | |
| 1479 p->pFrame = 0; | |
| 1480 p->nFrame = 0; | |
| 1481 | |
| 1482 if( p->apCsr ){ | |
| 1483 int i; | |
| 1484 for(i=0; i<p->nCursor; i++){ | |
| 1485 VdbeCursor *pC = p->apCsr[i]; | |
| 1486 if( pC ){ | |
| 1487 sqlite3VdbeFreeCursor(p, pC); | |
| 1488 p->apCsr[i] = 0; | |
| 1489 } | |
| 1490 } | |
| 1491 } | |
| 1492 if( p->aMem ){ | |
| 1493 releaseMemArray(&p->aMem[1], p->nMem); | |
| 1494 } | |
| 1495 } | |
| 1496 | |
| 1497 /* | |
| 1498 ** Clean up the VM after execution. | |
| 1499 ** | |
| 1500 ** This routine will automatically close any cursors, lists, and/or | |
| 1501 ** sorters that were left open. It also deletes the values of | |
| 1502 ** variables in the aVar[] array. | |
| 1503 */ | |
| 1504 static void Cleanup(Vdbe *p){ | |
| 1505 sqlite3 *db = p->db; | |
| 1506 | |
| 1507 #ifdef SQLITE_DEBUG | |
| 1508 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and | |
| 1509 ** Vdbe.aMem[] arrays have already been cleaned up. */ | |
| 1510 int i; | |
| 1511 for(i=0; i<p->nCursor; i++) assert( p->apCsr==0 || p->apCsr[i]==0 ); | |
| 1512 for(i=1; i<=p->nMem; i++) assert( p->aMem==0 || p->aMem[i].flags==MEM_Null ); | |
| 1513 #endif | |
| 1514 | |
| 1515 sqlite3DbFree(db, p->zErrMsg); | |
| 1516 p->zErrMsg = 0; | |
| 1517 p->pResultSet = 0; | |
| 1518 } | |
| 1519 | |
| 1520 /* | |
| 1521 ** Set the number of result columns that will be returned by this SQL | |
| 1522 ** statement. This is now set at compile time, rather than during | |
| 1523 ** execution of the vdbe program so that sqlite3_column_count() can | |
| 1524 ** be called on an SQL statement before sqlite3_step(). | |
| 1525 */ | |
| 1526 void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ | |
| 1527 Mem *pColName; | |
| 1528 int n; | |
| 1529 sqlite3 *db = p->db; | |
| 1530 | |
| 1531 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); | |
| 1532 sqlite3DbFree(db, p->aColName); | |
| 1533 n = nResColumn*COLNAME_N; | |
| 1534 p->nResColumn = (u16)nResColumn; | |
| 1535 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n ); | |
| 1536 if( p->aColName==0 ) return; | |
| 1537 while( n-- > 0 ){ | |
| 1538 pColName->flags = MEM_Null; | |
| 1539 pColName->db = p->db; | |
| 1540 pColName++; | |
| 1541 } | |
| 1542 } | |
| 1543 | |
| 1544 /* | |
| 1545 ** Set the name of the idx'th column to be returned by the SQL statement. | |
| 1546 ** zName must be a pointer to a nul terminated string. | |
| 1547 ** | |
| 1548 ** This call must be made after a call to sqlite3VdbeSetNumCols(). | |
| 1549 ** | |
| 1550 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC | |
| 1551 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed | |
| 1552 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed. | |
| 1553 */ | |
| 1554 int sqlite3VdbeSetColName( | |
| 1555 Vdbe *p, /* Vdbe being configured */ | |
| 1556 int idx, /* Index of column zName applies to */ | |
| 1557 int var, /* One of the COLNAME_* constants */ | |
| 1558 const char *zName, /* Pointer to buffer containing name */ | |
| 1559 void (*xDel)(void*) /* Memory management strategy for zName */ | |
| 1560 ){ | |
| 1561 int rc; | |
| 1562 Mem *pColName; | |
| 1563 assert( idx<p->nResColumn ); | |
| 1564 assert( var<COLNAME_N ); | |
| 1565 if( p->db->mallocFailed ){ | |
| 1566 assert( !zName || xDel!=SQLITE_DYNAMIC ); | |
| 1567 return SQLITE_NOMEM; | |
| 1568 } | |
| 1569 assert( p->aColName!=0 ); | |
| 1570 pColName = &(p->aColName[idx+var*p->nResColumn]); | |
| 1571 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel); | |
| 1572 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 ); | |
| 1573 return rc; | |
| 1574 } | |
| 1575 | |
| 1576 /* | |
| 1577 ** A read or write transaction may or may not be active on database handle | |
| 1578 ** db. If a transaction is active, commit it. If there is a | |
| 1579 ** write-transaction spanning more than one database file, this routine | |
| 1580 ** takes care of the master journal trickery. | |
| 1581 */ | |
| 1582 static int vdbeCommit(sqlite3 *db, Vdbe *p){ | |
| 1583 int i; | |
| 1584 int nTrans = 0; /* Number of databases with an active write-transaction */ | |
| 1585 int rc = SQLITE_OK; | |
| 1586 int needXcommit = 0; | |
| 1587 | |
| 1588 #ifdef SQLITE_OMIT_VIRTUALTABLE | |
| 1589 /* With this option, sqlite3VtabSync() is defined to be simply | |
| 1590 ** SQLITE_OK so p is not used. | |
| 1591 */ | |
| 1592 UNUSED_PARAMETER(p); | |
| 1593 #endif | |
| 1594 | |
| 1595 /* Before doing anything else, call the xSync() callback for any | |
| 1596 ** virtual module tables written in this transaction. This has to | |
| 1597 ** be done before determining whether a master journal file is | |
| 1598 ** required, as an xSync() callback may add an attached database | |
| 1599 ** to the transaction. | |
| 1600 */ | |
| 1601 rc = sqlite3VtabSync(db, &p->zErrMsg); | |
| 1602 if( rc!=SQLITE_OK ){ | |
| 1603 return rc; | |
| 1604 } | |
| 1605 | |
| 1606 /* This loop determines (a) if the commit hook should be invoked and | |
| 1607 ** (b) how many database files have open write transactions, not | |
| 1608 ** including the temp database. (b) is important because if more than | |
| 1609 ** one database file has an open write transaction, a master journal | |
| 1610 ** file is required for an atomic commit. | |
| 1611 */ | |
| 1612 for(i=0; i<db->nDb; i++){ | |
| 1613 Btree *pBt = db->aDb[i].pBt; | |
| 1614 if( sqlite3BtreeIsInTrans(pBt) ){ | |
| 1615 needXcommit = 1; | |
| 1616 if( i!=1 ) nTrans++; | |
| 1617 } | |
| 1618 } | |
| 1619 | |
| 1620 /* If there are any write-transactions at all, invoke the commit hook */ | |
| 1621 if( needXcommit && db->xCommitCallback ){ | |
| 1622 (void)sqlite3SafetyOff(db); | |
| 1623 rc = db->xCommitCallback(db->pCommitArg); | |
| 1624 (void)sqlite3SafetyOn(db); | |
| 1625 if( rc ){ | |
| 1626 return SQLITE_CONSTRAINT; | |
| 1627 } | |
| 1628 } | |
| 1629 | |
| 1630 /* The simple case - no more than one database file (not counting the | |
| 1631 ** TEMP database) has a transaction active. There is no need for the | |
| 1632 ** master-journal. | |
| 1633 ** | |
| 1634 ** If the return value of sqlite3BtreeGetFilename() is a zero length | |
| 1635 ** string, it means the main database is :memory: or a temp file. In | |
| 1636 ** that case we do not support atomic multi-file commits, so use the | |
| 1637 ** simple case then too. | |
| 1638 */ | |
| 1639 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt)) | |
| 1640 || nTrans<=1 | |
| 1641 ){ | |
| 1642 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ | |
| 1643 Btree *pBt = db->aDb[i].pBt; | |
| 1644 if( pBt ){ | |
| 1645 rc = sqlite3BtreeCommitPhaseOne(pBt, 0); | |
| 1646 } | |
| 1647 } | |
| 1648 | |
| 1649 /* Do the commit only if all databases successfully complete phase 1. | |
| 1650 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an | |
| 1651 ** IO error while deleting or truncating a journal file. It is unlikely, | |
| 1652 ** but could happen. In this case abandon processing and return the error. | |
| 1653 */ | |
| 1654 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ | |
| 1655 Btree *pBt = db->aDb[i].pBt; | |
| 1656 if( pBt ){ | |
| 1657 rc = sqlite3BtreeCommitPhaseTwo(pBt); | |
| 1658 } | |
| 1659 } | |
| 1660 if( rc==SQLITE_OK ){ | |
| 1661 sqlite3VtabCommit(db); | |
| 1662 } | |
| 1663 } | |
| 1664 | |
| 1665 /* The complex case - There is a multi-file write-transaction active. | |
| 1666 ** This requires a master journal file to ensure the transaction is | |
| 1667 ** committed atomicly. | |
| 1668 */ | |
| 1669 #ifndef SQLITE_OMIT_DISKIO | |
| 1670 else{ | |
| 1671 sqlite3_vfs *pVfs = db->pVfs; | |
| 1672 int needSync = 0; | |
| 1673 char *zMaster = 0; /* File-name for the master journal */ | |
| 1674 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); | |
| 1675 sqlite3_file *pMaster = 0; | |
| 1676 i64 offset = 0; | |
| 1677 int res; | |
| 1678 | |
| 1679 /* Select a master journal file name */ | |
| 1680 do { | |
| 1681 u32 iRandom; | |
| 1682 sqlite3DbFree(db, zMaster); | |
| 1683 sqlite3_randomness(sizeof(iRandom), &iRandom); | |
| 1684 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, iRandom&0x7fffffff); | |
| 1685 if( !zMaster ){ | |
| 1686 return SQLITE_NOMEM; | |
| 1687 } | |
| 1688 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); | |
| 1689 }while( rc==SQLITE_OK && res ); | |
| 1690 if( rc==SQLITE_OK ){ | |
| 1691 /* Open the master journal. */ | |
| 1692 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, | |
| 1693 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| | |
| 1694 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 | |
| 1695 ); | |
| 1696 } | |
| 1697 if( rc!=SQLITE_OK ){ | |
| 1698 sqlite3DbFree(db, zMaster); | |
| 1699 return rc; | |
| 1700 } | |
| 1701 | |
| 1702 /* Write the name of each database file in the transaction into the new | |
| 1703 ** master journal file. If an error occurs at this point close | |
| 1704 ** and delete the master journal file. All the individual journal files | |
| 1705 ** still have 'null' as the master journal pointer, so they will roll | |
| 1706 ** back independently if a failure occurs. | |
| 1707 */ | |
| 1708 for(i=0; i<db->nDb; i++){ | |
| 1709 Btree *pBt = db->aDb[i].pBt; | |
| 1710 if( i==1 ) continue; /* Ignore the TEMP database */ | |
| 1711 if( sqlite3BtreeIsInTrans(pBt) ){ | |
| 1712 char const *zFile = sqlite3BtreeGetJournalname(pBt); | |
| 1713 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */ | |
| 1714 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){ | |
| 1715 needSync = 1; | |
| 1716 } | |
| 1717 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset); | |
| 1718 offset += sqlite3Strlen30(zFile)+1; | |
| 1719 if( rc!=SQLITE_OK ){ | |
| 1720 sqlite3OsCloseFree(pMaster); | |
| 1721 sqlite3OsDelete(pVfs, zMaster, 0); | |
| 1722 sqlite3DbFree(db, zMaster); | |
| 1723 return rc; | |
| 1724 } | |
| 1725 } | |
| 1726 } | |
| 1727 | |
| 1728 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device | |
| 1729 ** flag is set this is not required. | |
| 1730 */ | |
| 1731 if( needSync | |
| 1732 && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL) | |
| 1733 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL)) | |
| 1734 ){ | |
| 1735 sqlite3OsCloseFree(pMaster); | |
| 1736 sqlite3OsDelete(pVfs, zMaster, 0); | |
| 1737 sqlite3DbFree(db, zMaster); | |
| 1738 return rc; | |
| 1739 } | |
| 1740 | |
| 1741 /* Sync all the db files involved in the transaction. The same call | |
| 1742 ** sets the master journal pointer in each individual journal. If | |
| 1743 ** an error occurs here, do not delete the master journal file. | |
| 1744 ** | |
| 1745 ** If the error occurs during the first call to | |
| 1746 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the | |
| 1747 ** master journal file will be orphaned. But we cannot delete it, | |
| 1748 ** in case the master journal file name was written into the journal | |
| 1749 ** file before the failure occurred. | |
| 1750 */ | |
| 1751 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ | |
| 1752 Btree *pBt = db->aDb[i].pBt; | |
| 1753 if( pBt ){ | |
| 1754 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster); | |
| 1755 } | |
| 1756 } | |
| 1757 sqlite3OsCloseFree(pMaster); | |
| 1758 if( rc!=SQLITE_OK ){ | |
| 1759 sqlite3DbFree(db, zMaster); | |
| 1760 return rc; | |
| 1761 } | |
| 1762 | |
| 1763 /* Delete the master journal file. This commits the transaction. After | |
| 1764 ** doing this the directory is synced again before any individual | |
| 1765 ** transaction files are deleted. | |
| 1766 */ | |
| 1767 rc = sqlite3OsDelete(pVfs, zMaster, 1); | |
| 1768 sqlite3DbFree(db, zMaster); | |
| 1769 zMaster = 0; | |
| 1770 if( rc ){ | |
| 1771 return rc; | |
| 1772 } | |
| 1773 | |
| 1774 /* All files and directories have already been synced, so the following | |
| 1775 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and | |
| 1776 ** deleting or truncating journals. If something goes wrong while | |
| 1777 ** this is happening we don't really care. The integrity of the | |
| 1778 ** transaction is already guaranteed, but some stray 'cold' journals | |
| 1779 ** may be lying around. Returning an error code won't help matters. | |
| 1780 */ | |
| 1781 disable_simulated_io_errors(); | |
| 1782 sqlite3BeginBenignMalloc(); | |
| 1783 for(i=0; i<db->nDb; i++){ | |
| 1784 Btree *pBt = db->aDb[i].pBt; | |
| 1785 if( pBt ){ | |
| 1786 sqlite3BtreeCommitPhaseTwo(pBt); | |
| 1787 } | |
| 1788 } | |
| 1789 sqlite3EndBenignMalloc(); | |
| 1790 enable_simulated_io_errors(); | |
| 1791 | |
| 1792 sqlite3VtabCommit(db); | |
| 1793 } | |
| 1794 #endif | |
| 1795 | |
| 1796 return rc; | |
| 1797 } | |
| 1798 | |
| 1799 /* | |
| 1800 ** This routine checks that the sqlite3.activeVdbeCnt count variable | |
| 1801 ** matches the number of vdbe's in the list sqlite3.pVdbe that are | |
| 1802 ** currently active. An assertion fails if the two counts do not match. | |
| 1803 ** This is an internal self-check only - it is not an essential processing | |
| 1804 ** step. | |
| 1805 ** | |
| 1806 ** This is a no-op if NDEBUG is defined. | |
| 1807 */ | |
| 1808 #ifndef NDEBUG | |
| 1809 static void checkActiveVdbeCnt(sqlite3 *db){ | |
| 1810 Vdbe *p; | |
| 1811 int cnt = 0; | |
| 1812 int nWrite = 0; | |
| 1813 p = db->pVdbe; | |
| 1814 while( p ){ | |
| 1815 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){ | |
| 1816 cnt++; | |
| 1817 if( p->readOnly==0 ) nWrite++; | |
| 1818 } | |
| 1819 p = p->pNext; | |
| 1820 } | |
| 1821 assert( cnt==db->activeVdbeCnt ); | |
| 1822 assert( nWrite==db->writeVdbeCnt ); | |
| 1823 } | |
| 1824 #else | |
| 1825 #define checkActiveVdbeCnt(x) | |
| 1826 #endif | |
| 1827 | |
| 1828 /* | |
| 1829 ** For every Btree that in database connection db which | |
| 1830 ** has been modified, "trip" or invalidate each cursor in | |
| 1831 ** that Btree might have been modified so that the cursor | |
| 1832 ** can never be used again. This happens when a rollback | |
| 1833 *** occurs. We have to trip all the other cursors, even | |
| 1834 ** cursor from other VMs in different database connections, | |
| 1835 ** so that none of them try to use the data at which they | |
| 1836 ** were pointing and which now may have been changed due | |
| 1837 ** to the rollback. | |
| 1838 ** | |
| 1839 ** Remember that a rollback can delete tables complete and | |
| 1840 ** reorder rootpages. So it is not sufficient just to save | |
| 1841 ** the state of the cursor. We have to invalidate the cursor | |
| 1842 ** so that it is never used again. | |
| 1843 */ | |
| 1844 static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){ | |
| 1845 int i; | |
| 1846 for(i=0; i<db->nDb; i++){ | |
| 1847 Btree *p = db->aDb[i].pBt; | |
| 1848 if( p && sqlite3BtreeIsInTrans(p) ){ | |
| 1849 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT); | |
| 1850 } | |
| 1851 } | |
| 1852 } | |
| 1853 | |
| 1854 /* | |
| 1855 ** If the Vdbe passed as the first argument opened a statement-transaction, | |
| 1856 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or | |
| 1857 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement | |
| 1858 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the | |
| 1859 ** statement transaction is commtted. | |
| 1860 ** | |
| 1861 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. | |
| 1862 ** Otherwise SQLITE_OK. | |
| 1863 */ | |
| 1864 int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ | |
| 1865 sqlite3 *const db = p->db; | |
| 1866 int rc = SQLITE_OK; | |
| 1867 | |
| 1868 /* If p->iStatement is greater than zero, then this Vdbe opened a | |
| 1869 ** statement transaction that should be closed here. The only exception | |
| 1870 ** is that an IO error may have occured, causing an emergency rollback. | |
| 1871 ** In this case (db->nStatement==0), and there is nothing to do. | |
| 1872 */ | |
| 1873 if( db->nStatement && p->iStatement ){ | |
| 1874 int i; | |
| 1875 const int iSavepoint = p->iStatement-1; | |
| 1876 | |
| 1877 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE); | |
| 1878 assert( db->nStatement>0 ); | |
| 1879 assert( p->iStatement==(db->nStatement+db->nSavepoint) ); | |
| 1880 | |
| 1881 for(i=0; i<db->nDb; i++){ | |
| 1882 int rc2 = SQLITE_OK; | |
| 1883 Btree *pBt = db->aDb[i].pBt; | |
| 1884 if( pBt ){ | |
| 1885 if( eOp==SAVEPOINT_ROLLBACK ){ | |
| 1886 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint); | |
| 1887 } | |
| 1888 if( rc2==SQLITE_OK ){ | |
| 1889 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint); | |
| 1890 } | |
| 1891 if( rc==SQLITE_OK ){ | |
| 1892 rc = rc2; | |
| 1893 } | |
| 1894 } | |
| 1895 } | |
| 1896 db->nStatement--; | |
| 1897 p->iStatement = 0; | |
| 1898 } | |
| 1899 return rc; | |
| 1900 } | |
| 1901 | |
| 1902 /* | |
| 1903 ** If SQLite is compiled to support shared-cache mode and to be threadsafe, | |
| 1904 ** this routine obtains the mutex associated with each BtShared structure | |
| 1905 ** that may be accessed by the VM passed as an argument. In doing so it | |
| 1906 ** sets the BtShared.db member of each of the BtShared structures, ensuring | |
| 1907 ** that the correct busy-handler callback is invoked if required. | |
| 1908 ** | |
| 1909 ** If SQLite is not threadsafe but does support shared-cache mode, then | |
| 1910 ** sqlite3BtreeEnterAll() is invoked to set the BtShared.db variables | |
| 1911 ** of all of BtShared structures accessible via the database handle | |
| 1912 ** associated with the VM. Of course only a subset of these structures | |
| 1913 ** will be accessed by the VM, and we could use Vdbe.btreeMask to figure | |
| 1914 ** that subset out, but there is no advantage to doing so. | |
| 1915 ** | |
| 1916 ** If SQLite is not threadsafe and does not support shared-cache mode, this | |
| 1917 ** function is a no-op. | |
| 1918 */ | |
| 1919 #ifndef SQLITE_OMIT_SHARED_CACHE | |
| 1920 void sqlite3VdbeMutexArrayEnter(Vdbe *p){ | |
| 1921 #if SQLITE_THREADSAFE | |
| 1922 sqlite3BtreeMutexArrayEnter(&p->aMutex); | |
| 1923 #else | |
| 1924 sqlite3BtreeEnterAll(p->db); | |
| 1925 #endif | |
| 1926 } | |
| 1927 #endif | |
| 1928 | |
| 1929 /* | |
| 1930 ** This routine is called the when a VDBE tries to halt. If the VDBE | |
| 1931 ** has made changes and is in autocommit mode, then commit those | |
| 1932 ** changes. If a rollback is needed, then do the rollback. | |
| 1933 ** | |
| 1934 ** This routine is the only way to move the state of a VM from | |
| 1935 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to | |
| 1936 ** call this on a VM that is in the SQLITE_MAGIC_HALT state. | |
| 1937 ** | |
| 1938 ** Return an error code. If the commit could not complete because of | |
| 1939 ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it | |
| 1940 ** means the close did not happen and needs to be repeated. | |
| 1941 */ | |
| 1942 int sqlite3VdbeHalt(Vdbe *p){ | |
| 1943 int rc; /* Used to store transient return codes */ | |
| 1944 sqlite3 *db = p->db; | |
| 1945 | |
| 1946 /* This function contains the logic that determines if a statement or | |
| 1947 ** transaction will be committed or rolled back as a result of the | |
| 1948 ** execution of this virtual machine. | |
| 1949 ** | |
| 1950 ** If any of the following errors occur: | |
| 1951 ** | |
| 1952 ** SQLITE_NOMEM | |
| 1953 ** SQLITE_IOERR | |
| 1954 ** SQLITE_FULL | |
| 1955 ** SQLITE_INTERRUPT | |
| 1956 ** | |
| 1957 ** Then the internal cache might have been left in an inconsistent | |
| 1958 ** state. We need to rollback the statement transaction, if there is | |
| 1959 ** one, or the complete transaction if there is no statement transaction. | |
| 1960 */ | |
| 1961 | |
| 1962 if( p->db->mallocFailed ){ | |
| 1963 p->rc = SQLITE_NOMEM; | |
| 1964 } | |
| 1965 closeAllCursors(p); | |
| 1966 if( p->magic!=VDBE_MAGIC_RUN ){ | |
| 1967 return SQLITE_OK; | |
| 1968 } | |
| 1969 checkActiveVdbeCnt(db); | |
| 1970 | |
| 1971 /* No commit or rollback needed if the program never started */ | |
| 1972 if( p->pc>=0 ){ | |
| 1973 int mrc; /* Primary error code from p->rc */ | |
| 1974 int eStatementOp = 0; | |
| 1975 int isSpecialError; /* Set to true if a 'special' error */ | |
| 1976 | |
| 1977 /* Lock all btrees used by the statement */ | |
| 1978 sqlite3VdbeMutexArrayEnter(p); | |
| 1979 | |
| 1980 /* Check for one of the special errors */ | |
| 1981 mrc = p->rc & 0xff; | |
| 1982 assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */ | |
| 1983 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR | |
| 1984 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; | |
| 1985 if( isSpecialError ){ | |
| 1986 /* If the query was read-only, we need do no rollback at all. Otherwise, | |
| 1987 ** proceed with the special handling. | |
| 1988 */ | |
| 1989 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){ | |
| 1990 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){ | |
| 1991 eStatementOp = SAVEPOINT_ROLLBACK; | |
| 1992 }else{ | |
| 1993 /* We are forced to roll back the active transaction. Before doing | |
| 1994 ** so, abort any other statements this handle currently has active. | |
| 1995 */ | |
| 1996 invalidateCursorsOnModifiedBtrees(db); | |
| 1997 sqlite3RollbackAll(db); | |
| 1998 sqlite3CloseSavepoints(db); | |
| 1999 db->autoCommit = 1; | |
| 2000 } | |
| 2001 } | |
| 2002 } | |
| 2003 | |
| 2004 /* If the auto-commit flag is set and this is the only active writer | |
| 2005 ** VM, then we do either a commit or rollback of the current transaction. | |
| 2006 ** | |
| 2007 ** Note: This block also runs if one of the special errors handled | |
| 2008 ** above has occurred. | |
| 2009 */ | |
| 2010 if( !sqlite3VtabInSync(db) | |
| 2011 && db->autoCommit | |
| 2012 && db->writeVdbeCnt==(p->readOnly==0) | |
| 2013 ){ | |
| 2014 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ | |
| 2015 /* The auto-commit flag is true, and the vdbe program was | |
| 2016 ** successful or hit an 'OR FAIL' constraint. This means a commit | |
| 2017 ** is required. | |
| 2018 */ | |
| 2019 rc = vdbeCommit(db, p); | |
| 2020 if( rc==SQLITE_BUSY ){ | |
| 2021 sqlite3BtreeMutexArrayLeave(&p->aMutex); | |
| 2022 return SQLITE_BUSY; | |
| 2023 }else if( rc!=SQLITE_OK ){ | |
| 2024 p->rc = rc; | |
| 2025 sqlite3RollbackAll(db); | |
| 2026 }else{ | |
| 2027 sqlite3CommitInternalChanges(db); | |
| 2028 } | |
| 2029 }else{ | |
| 2030 sqlite3RollbackAll(db); | |
| 2031 } | |
| 2032 db->nStatement = 0; | |
| 2033 }else if( eStatementOp==0 ){ | |
| 2034 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ | |
| 2035 eStatementOp = SAVEPOINT_RELEASE; | |
| 2036 }else if( p->errorAction==OE_Abort ){ | |
| 2037 eStatementOp = SAVEPOINT_ROLLBACK; | |
| 2038 }else{ | |
| 2039 invalidateCursorsOnModifiedBtrees(db); | |
| 2040 sqlite3RollbackAll(db); | |
| 2041 sqlite3CloseSavepoints(db); | |
| 2042 db->autoCommit = 1; | |
| 2043 } | |
| 2044 } | |
| 2045 | |
| 2046 /* If eStatementOp is non-zero, then a statement transaction needs to | |
| 2047 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to | |
| 2048 ** do so. If this operation returns an error, and the current statement | |
| 2049 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then set the error | |
| 2050 ** code to the new value. | |
| 2051 */ | |
| 2052 if( eStatementOp ){ | |
| 2053 rc = sqlite3VdbeCloseStatement(p, eStatementOp); | |
| 2054 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){ | |
| 2055 p->rc = rc; | |
| 2056 sqlite3DbFree(db, p->zErrMsg); | |
| 2057 p->zErrMsg = 0; | |
| 2058 } | |
| 2059 } | |
| 2060 | |
| 2061 /* If this was an INSERT, UPDATE or DELETE and no statement transaction | |
| 2062 ** has been rolled back, update the database connection change-counter. | |
| 2063 */ | |
| 2064 if( p->changeCntOn ){ | |
| 2065 if( eStatementOp!=SAVEPOINT_ROLLBACK ){ | |
| 2066 sqlite3VdbeSetChanges(db, p->nChange); | |
| 2067 }else{ | |
| 2068 sqlite3VdbeSetChanges(db, 0); | |
| 2069 } | |
| 2070 p->nChange = 0; | |
| 2071 } | |
| 2072 | |
| 2073 /* Rollback or commit any schema changes that occurred. */ | |
| 2074 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){ | |
| 2075 sqlite3ResetInternalSchema(db, 0); | |
| 2076 db->flags = (db->flags | SQLITE_InternChanges); | |
| 2077 } | |
| 2078 | |
| 2079 /* Release the locks */ | |
| 2080 sqlite3BtreeMutexArrayLeave(&p->aMutex); | |
| 2081 } | |
| 2082 | |
| 2083 /* We have successfully halted and closed the VM. Record this fact. */ | |
| 2084 if( p->pc>=0 ){ | |
| 2085 db->activeVdbeCnt--; | |
| 2086 if( !p->readOnly ){ | |
| 2087 db->writeVdbeCnt--; | |
| 2088 } | |
| 2089 assert( db->activeVdbeCnt>=db->writeVdbeCnt ); | |
| 2090 } | |
| 2091 p->magic = VDBE_MAGIC_HALT; | |
| 2092 checkActiveVdbeCnt(db); | |
| 2093 if( p->db->mallocFailed ){ | |
| 2094 p->rc = SQLITE_NOMEM; | |
| 2095 } | |
| 2096 | |
| 2097 /* If the auto-commit flag is set to true, then any locks that were held | |
| 2098 ** by connection db have now been released. Call sqlite3ConnectionUnlocked() | |
| 2099 ** to invoke any required unlock-notify callbacks. | |
| 2100 */ | |
| 2101 if( db->autoCommit ){ | |
| 2102 sqlite3ConnectionUnlocked(db); | |
| 2103 } | |
| 2104 | |
| 2105 assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 ); | |
| 2106 return SQLITE_OK; | |
| 2107 } | |
| 2108 | |
| 2109 | |
| 2110 /* | |
| 2111 ** Each VDBE holds the result of the most recent sqlite3_step() call | |
| 2112 ** in p->rc. This routine sets that result back to SQLITE_OK. | |
| 2113 */ | |
| 2114 void sqlite3VdbeResetStepResult(Vdbe *p){ | |
| 2115 p->rc = SQLITE_OK; | |
| 2116 } | |
| 2117 | |
| 2118 /* | |
| 2119 ** Clean up a VDBE after execution but do not delete the VDBE just yet. | |
| 2120 ** Write any error messages into *pzErrMsg. Return the result code. | |
| 2121 ** | |
| 2122 ** After this routine is run, the VDBE should be ready to be executed | |
| 2123 ** again. | |
| 2124 ** | |
| 2125 ** To look at it another way, this routine resets the state of the | |
| 2126 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to | |
| 2127 ** VDBE_MAGIC_INIT. | |
| 2128 */ | |
| 2129 int sqlite3VdbeReset(Vdbe *p){ | |
| 2130 sqlite3 *db; | |
| 2131 db = p->db; | |
| 2132 | |
| 2133 /* If the VM did not run to completion or if it encountered an | |
| 2134 ** error, then it might not have been halted properly. So halt | |
| 2135 ** it now. | |
| 2136 */ | |
| 2137 (void)sqlite3SafetyOn(db); | |
| 2138 sqlite3VdbeHalt(p); | |
| 2139 (void)sqlite3SafetyOff(db); | |
| 2140 | |
| 2141 /* If the VDBE has be run even partially, then transfer the error code | |
| 2142 ** and error message from the VDBE into the main database structure. But | |
| 2143 ** if the VDBE has just been set to run but has not actually executed any | |
| 2144 ** instructions yet, leave the main database error information unchanged. | |
| 2145 */ | |
| 2146 if( p->pc>=0 ){ | |
| 2147 if( p->zErrMsg ){ | |
| 2148 sqlite3BeginBenignMalloc(); | |
| 2149 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT); | |
| 2150 sqlite3EndBenignMalloc(); | |
| 2151 db->errCode = p->rc; | |
| 2152 sqlite3DbFree(db, p->zErrMsg); | |
| 2153 p->zErrMsg = 0; | |
| 2154 }else if( p->rc ){ | |
| 2155 sqlite3Error(db, p->rc, 0); | |
| 2156 }else{ | |
| 2157 sqlite3Error(db, SQLITE_OK, 0); | |
| 2158 } | |
| 2159 }else if( p->rc && p->expired ){ | |
| 2160 /* The expired flag was set on the VDBE before the first call | |
| 2161 ** to sqlite3_step(). For consistency (since sqlite3_step() was | |
| 2162 ** called), set the database error in this case as well. | |
| 2163 */ | |
| 2164 sqlite3Error(db, p->rc, 0); | |
| 2165 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT); | |
| 2166 sqlite3DbFree(db, p->zErrMsg); | |
| 2167 p->zErrMsg = 0; | |
| 2168 } | |
| 2169 | |
| 2170 /* Reclaim all memory used by the VDBE | |
| 2171 */ | |
| 2172 Cleanup(p); | |
| 2173 | |
| 2174 /* Save profiling information from this VDBE run. | |
| 2175 */ | |
| 2176 #ifdef VDBE_PROFILE | |
| 2177 { | |
| 2178 FILE *out = fopen("vdbe_profile.out", "a"); | |
| 2179 if( out ){ | |
| 2180 int i; | |
| 2181 fprintf(out, "---- "); | |
| 2182 for(i=0; i<p->nOp; i++){ | |
| 2183 fprintf(out, "%02x", p->aOp[i].opcode); | |
| 2184 } | |
| 2185 fprintf(out, "\n"); | |
| 2186 for(i=0; i<p->nOp; i++){ | |
| 2187 fprintf(out, "%6d %10lld %8lld ", | |
| 2188 p->aOp[i].cnt, | |
| 2189 p->aOp[i].cycles, | |
| 2190 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 | |
| 2191 ); | |
| 2192 sqlite3VdbePrintOp(out, i, &p->aOp[i]); | |
| 2193 } | |
| 2194 fclose(out); | |
| 2195 } | |
| 2196 } | |
| 2197 #endif | |
| 2198 p->magic = VDBE_MAGIC_INIT; | |
| 2199 return p->rc & db->errMask; | |
| 2200 } | |
| 2201 | |
| 2202 /* | |
| 2203 ** Clean up and delete a VDBE after execution. Return an integer which is | |
| 2204 ** the result code. Write any error message text into *pzErrMsg. | |
| 2205 */ | |
| 2206 int sqlite3VdbeFinalize(Vdbe *p){ | |
| 2207 int rc = SQLITE_OK; | |
| 2208 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ | |
| 2209 rc = sqlite3VdbeReset(p); | |
| 2210 assert( (rc & p->db->errMask)==rc ); | |
| 2211 } | |
| 2212 sqlite3VdbeDelete(p); | |
| 2213 return rc; | |
| 2214 } | |
| 2215 | |
| 2216 /* | |
| 2217 ** Call the destructor for each auxdata entry in pVdbeFunc for which | |
| 2218 ** the corresponding bit in mask is clear. Auxdata entries beyond 31 | |
| 2219 ** are always destroyed. To destroy all auxdata entries, call this | |
| 2220 ** routine with mask==0. | |
| 2221 */ | |
| 2222 void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){ | |
| 2223 int i; | |
| 2224 for(i=0; i<pVdbeFunc->nAux; i++){ | |
| 2225 struct AuxData *pAux = &pVdbeFunc->apAux[i]; | |
| 2226 if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){ | |
| 2227 if( pAux->xDelete ){ | |
| 2228 pAux->xDelete(pAux->pAux); | |
| 2229 } | |
| 2230 pAux->pAux = 0; | |
| 2231 } | |
| 2232 } | |
| 2233 } | |
| 2234 | |
| 2235 /* | |
| 2236 ** Delete an entire VDBE. | |
| 2237 */ | |
| 2238 void sqlite3VdbeDelete(Vdbe *p){ | |
| 2239 sqlite3 *db; | |
| 2240 | |
| 2241 if( NEVER(p==0) ) return; | |
| 2242 db = p->db; | |
| 2243 if( p->pPrev ){ | |
| 2244 p->pPrev->pNext = p->pNext; | |
| 2245 }else{ | |
| 2246 assert( db->pVdbe==p ); | |
| 2247 db->pVdbe = p->pNext; | |
| 2248 } | |
| 2249 if( p->pNext ){ | |
| 2250 p->pNext->pPrev = p->pPrev; | |
| 2251 } | |
| 2252 releaseMemArray(p->aVar, p->nVar); | |
| 2253 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); | |
| 2254 vdbeFreeOpArray(db, p->aOp, p->nOp); | |
| 2255 sqlite3DbFree(db, p->aLabel); | |
| 2256 sqlite3DbFree(db, p->aColName); | |
| 2257 sqlite3DbFree(db, p->zSql); | |
| 2258 p->magic = VDBE_MAGIC_DEAD; | |
| 2259 sqlite3DbFree(db, p->pFree); | |
| 2260 sqlite3DbFree(db, p); | |
| 2261 } | |
| 2262 | |
| 2263 /* | |
| 2264 ** Make sure the cursor p is ready to read or write the row to which it | |
| 2265 ** was last positioned. Return an error code if an OOM fault or I/O error | |
| 2266 ** prevents us from positioning the cursor to its correct position. | |
| 2267 ** | |
| 2268 ** If a MoveTo operation is pending on the given cursor, then do that | |
| 2269 ** MoveTo now. If no move is pending, check to see if the row has been | |
| 2270 ** deleted out from under the cursor and if it has, mark the row as | |
| 2271 ** a NULL row. | |
| 2272 ** | |
| 2273 ** If the cursor is already pointing to the correct row and that row has | |
| 2274 ** not been deleted out from under the cursor, then this routine is a no-op. | |
| 2275 */ | |
| 2276 int sqlite3VdbeCursorMoveto(VdbeCursor *p){ | |
| 2277 if( p->deferredMoveto ){ | |
| 2278 int res, rc; | |
| 2279 #ifdef SQLITE_TEST | |
| 2280 extern int sqlite3_search_count; | |
| 2281 #endif | |
| 2282 assert( p->isTable ); | |
| 2283 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res); | |
| 2284 if( rc ) return rc; | |
| 2285 p->lastRowid = p->movetoTarget; | |
| 2286 p->rowidIsValid = ALWAYS(res==0) ?1:0; | |
| 2287 if( NEVER(res<0) ){ | |
| 2288 rc = sqlite3BtreeNext(p->pCursor, &res); | |
| 2289 if( rc ) return rc; | |
| 2290 } | |
| 2291 #ifdef SQLITE_TEST | |
| 2292 sqlite3_search_count++; | |
| 2293 #endif | |
| 2294 p->deferredMoveto = 0; | |
| 2295 p->cacheStatus = CACHE_STALE; | |
| 2296 }else if( ALWAYS(p->pCursor) ){ | |
| 2297 int hasMoved; | |
| 2298 int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved); | |
| 2299 if( rc ) return rc; | |
| 2300 if( hasMoved ){ | |
| 2301 p->cacheStatus = CACHE_STALE; | |
| 2302 p->nullRow = 1; | |
| 2303 } | |
| 2304 } | |
| 2305 return SQLITE_OK; | |
| 2306 } | |
| 2307 | |
| 2308 /* | |
| 2309 ** The following functions: | |
| 2310 ** | |
| 2311 ** sqlite3VdbeSerialType() | |
| 2312 ** sqlite3VdbeSerialTypeLen() | |
| 2313 ** sqlite3VdbeSerialLen() | |
| 2314 ** sqlite3VdbeSerialPut() | |
| 2315 ** sqlite3VdbeSerialGet() | |
| 2316 ** | |
| 2317 ** encapsulate the code that serializes values for storage in SQLite | |
| 2318 ** data and index records. Each serialized value consists of a | |
| 2319 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned | |
| 2320 ** integer, stored as a varint. | |
| 2321 ** | |
| 2322 ** In an SQLite index record, the serial type is stored directly before | |
| 2323 ** the blob of data that it corresponds to. In a table record, all serial | |
| 2324 ** types are stored at the start of the record, and the blobs of data at | |
| 2325 ** the end. Hence these functions allow the caller to handle the | |
| 2326 ** serial-type and data blob seperately. | |
| 2327 ** | |
| 2328 ** The following table describes the various storage classes for data: | |
| 2329 ** | |
| 2330 ** serial type bytes of data type | |
| 2331 ** -------------- --------------- --------------- | |
| 2332 ** 0 0 NULL | |
| 2333 ** 1 1 signed integer | |
| 2334 ** 2 2 signed integer | |
| 2335 ** 3 3 signed integer | |
| 2336 ** 4 4 signed integer | |
| 2337 ** 5 6 signed integer | |
| 2338 ** 6 8 signed integer | |
| 2339 ** 7 8 IEEE float | |
| 2340 ** 8 0 Integer constant 0 | |
| 2341 ** 9 0 Integer constant 1 | |
| 2342 ** 10,11 reserved for expansion | |
| 2343 ** N>=12 and even (N-12)/2 BLOB | |
| 2344 ** N>=13 and odd (N-13)/2 text | |
| 2345 ** | |
| 2346 ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions | |
| 2347 ** of SQLite will not understand those serial types. | |
| 2348 */ | |
| 2349 | |
| 2350 /* | |
| 2351 ** Return the serial-type for the value stored in pMem. | |
| 2352 */ | |
| 2353 u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ | |
| 2354 int flags = pMem->flags; | |
| 2355 int n; | |
| 2356 | |
| 2357 if( flags&MEM_Null ){ | |
| 2358 return 0; | |
| 2359 } | |
| 2360 if( flags&MEM_Int ){ | |
| 2361 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ | |
| 2362 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) | |
| 2363 i64 i = pMem->u.i; | |
| 2364 u64 u; | |
| 2365 if( file_format>=4 && (i&1)==i ){ | |
| 2366 return 8+(u32)i; | |
| 2367 } | |
| 2368 u = i<0 ? -i : i; | |
| 2369 if( u<=127 ) return 1; | |
| 2370 if( u<=32767 ) return 2; | |
| 2371 if( u<=8388607 ) return 3; | |
| 2372 if( u<=2147483647 ) return 4; | |
| 2373 if( u<=MAX_6BYTE ) return 5; | |
| 2374 return 6; | |
| 2375 } | |
| 2376 if( flags&MEM_Real ){ | |
| 2377 return 7; | |
| 2378 } | |
| 2379 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) ); | |
| 2380 n = pMem->n; | |
| 2381 if( flags & MEM_Zero ){ | |
| 2382 n += pMem->u.nZero; | |
| 2383 } | |
| 2384 assert( n>=0 ); | |
| 2385 return ((n*2) + 12 + ((flags&MEM_Str)!=0)); | |
| 2386 } | |
| 2387 | |
| 2388 /* | |
| 2389 ** Return the length of the data corresponding to the supplied serial-type. | |
| 2390 */ | |
| 2391 u32 sqlite3VdbeSerialTypeLen(u32 serial_type){ | |
| 2392 if( serial_type>=12 ){ | |
| 2393 return (serial_type-12)/2; | |
| 2394 }else{ | |
| 2395 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 }; | |
| 2396 return aSize[serial_type]; | |
| 2397 } | |
| 2398 } | |
| 2399 | |
| 2400 /* | |
| 2401 ** If we are on an architecture with mixed-endian floating | |
| 2402 ** points (ex: ARM7) then swap the lower 4 bytes with the | |
| 2403 ** upper 4 bytes. Return the result. | |
| 2404 ** | |
| 2405 ** For most architectures, this is a no-op. | |
| 2406 ** | |
| 2407 ** (later): It is reported to me that the mixed-endian problem | |
| 2408 ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems | |
| 2409 ** that early versions of GCC stored the two words of a 64-bit | |
| 2410 ** float in the wrong order. And that error has been propagated | |
| 2411 ** ever since. The blame is not necessarily with GCC, though. | |
| 2412 ** GCC might have just copying the problem from a prior compiler. | |
| 2413 ** I am also told that newer versions of GCC that follow a different | |
| 2414 ** ABI get the byte order right. | |
| 2415 ** | |
| 2416 ** Developers using SQLite on an ARM7 should compile and run their | |
| 2417 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG | |
| 2418 ** enabled, some asserts below will ensure that the byte order of | |
| 2419 ** floating point values is correct. | |
| 2420 ** | |
| 2421 ** (2007-08-30) Frank van Vugt has studied this problem closely | |
| 2422 ** and has send his findings to the SQLite developers. Frank | |
| 2423 ** writes that some Linux kernels offer floating point hardware | |
| 2424 ** emulation that uses only 32-bit mantissas instead of a full | |
| 2425 ** 48-bits as required by the IEEE standard. (This is the | |
| 2426 ** CONFIG_FPE_FASTFPE option.) On such systems, floating point | |
| 2427 ** byte swapping becomes very complicated. To avoid problems, | |
| 2428 ** the necessary byte swapping is carried out using a 64-bit integer | |
| 2429 ** rather than a 64-bit float. Frank assures us that the code here | |
| 2430 ** works for him. We, the developers, have no way to independently | |
| 2431 ** verify this, but Frank seems to know what he is talking about | |
| 2432 ** so we trust him. | |
| 2433 */ | |
| 2434 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT | |
| 2435 static u64 floatSwap(u64 in){ | |
| 2436 union { | |
| 2437 u64 r; | |
| 2438 u32 i[2]; | |
| 2439 } u; | |
| 2440 u32 t; | |
| 2441 | |
| 2442 u.r = in; | |
| 2443 t = u.i[0]; | |
| 2444 u.i[0] = u.i[1]; | |
| 2445 u.i[1] = t; | |
| 2446 return u.r; | |
| 2447 } | |
| 2448 # define swapMixedEndianFloat(X) X = floatSwap(X) | |
| 2449 #else | |
| 2450 # define swapMixedEndianFloat(X) | |
| 2451 #endif | |
| 2452 | |
| 2453 /* | |
| 2454 ** Write the serialized data blob for the value stored in pMem into | |
| 2455 ** buf. It is assumed that the caller has allocated sufficient space. | |
| 2456 ** Return the number of bytes written. | |
| 2457 ** | |
| 2458 ** nBuf is the amount of space left in buf[]. nBuf must always be | |
| 2459 ** large enough to hold the entire field. Except, if the field is | |
| 2460 ** a blob with a zero-filled tail, then buf[] might be just the right | |
| 2461 ** size to hold everything except for the zero-filled tail. If buf[] | |
| 2462 ** is only big enough to hold the non-zero prefix, then only write that | |
| 2463 ** prefix into buf[]. But if buf[] is large enough to hold both the | |
| 2464 ** prefix and the tail then write the prefix and set the tail to all | |
| 2465 ** zeros. | |
| 2466 ** | |
| 2467 ** Return the number of bytes actually written into buf[]. The number | |
| 2468 ** of bytes in the zero-filled tail is included in the return value only | |
| 2469 ** if those bytes were zeroed in buf[]. | |
| 2470 */ | |
| 2471 u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){ | |
| 2472 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format); | |
| 2473 u32 len; | |
| 2474 | |
| 2475 /* Integer and Real */ | |
| 2476 if( serial_type<=7 && serial_type>0 ){ | |
| 2477 u64 v; | |
| 2478 u32 i; | |
| 2479 if( serial_type==7 ){ | |
| 2480 assert( sizeof(v)==sizeof(pMem->r) ); | |
| 2481 memcpy(&v, &pMem->r, sizeof(v)); | |
| 2482 swapMixedEndianFloat(v); | |
| 2483 }else{ | |
| 2484 v = pMem->u.i; | |
| 2485 } | |
| 2486 len = i = sqlite3VdbeSerialTypeLen(serial_type); | |
| 2487 assert( len<=(u32)nBuf ); | |
| 2488 while( i-- ){ | |
| 2489 buf[i] = (u8)(v&0xFF); | |
| 2490 v >>= 8; | |
| 2491 } | |
| 2492 return len; | |
| 2493 } | |
| 2494 | |
| 2495 /* String or blob */ | |
| 2496 if( serial_type>=12 ){ | |
| 2497 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0) | |
| 2498 == (int)sqlite3VdbeSerialTypeLen(serial_type) ); | |
| 2499 assert( pMem->n<=nBuf ); | |
| 2500 len = pMem->n; | |
| 2501 memcpy(buf, pMem->z, len); | |
| 2502 if( pMem->flags & MEM_Zero ){ | |
| 2503 len += pMem->u.nZero; | |
| 2504 assert( nBuf>=0 ); | |
| 2505 if( len > (u32)nBuf ){ | |
| 2506 len = (u32)nBuf; | |
| 2507 } | |
| 2508 memset(&buf[pMem->n], 0, len-pMem->n); | |
| 2509 } | |
| 2510 return len; | |
| 2511 } | |
| 2512 | |
| 2513 /* NULL or constants 0 or 1 */ | |
| 2514 return 0; | |
| 2515 } | |
| 2516 | |
| 2517 /* | |
| 2518 ** Deserialize the data blob pointed to by buf as serial type serial_type | |
| 2519 ** and store the result in pMem. Return the number of bytes read. | |
| 2520 */ | |
| 2521 u32 sqlite3VdbeSerialGet( | |
| 2522 const unsigned char *buf, /* Buffer to deserialize from */ | |
| 2523 u32 serial_type, /* Serial type to deserialize */ | |
| 2524 Mem *pMem /* Memory cell to write value into */ | |
| 2525 ){ | |
| 2526 switch( serial_type ){ | |
| 2527 case 10: /* Reserved for future use */ | |
| 2528 case 11: /* Reserved for future use */ | |
| 2529 case 0: { /* NULL */ | |
| 2530 pMem->flags = MEM_Null; | |
| 2531 break; | |
| 2532 } | |
| 2533 case 1: { /* 1-byte signed integer */ | |
| 2534 pMem->u.i = (signed char)buf[0]; | |
| 2535 pMem->flags = MEM_Int; | |
| 2536 return 1; | |
| 2537 } | |
| 2538 case 2: { /* 2-byte signed integer */ | |
| 2539 pMem->u.i = (((signed char)buf[0])<<8) | buf[1]; | |
| 2540 pMem->flags = MEM_Int; | |
| 2541 return 2; | |
| 2542 } | |
| 2543 case 3: { /* 3-byte signed integer */ | |
| 2544 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2]; | |
| 2545 pMem->flags = MEM_Int; | |
| 2546 return 3; | |
| 2547 } | |
| 2548 case 4: { /* 4-byte signed integer */ | |
| 2549 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; | |
| 2550 pMem->flags = MEM_Int; | |
| 2551 return 4; | |
| 2552 } | |
| 2553 case 5: { /* 6-byte signed integer */ | |
| 2554 u64 x = (((signed char)buf[0])<<8) | buf[1]; | |
| 2555 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; | |
| 2556 x = (x<<32) | y; | |
| 2557 pMem->u.i = *(i64*)&x; | |
| 2558 pMem->flags = MEM_Int; | |
| 2559 return 6; | |
| 2560 } | |
| 2561 case 6: /* 8-byte signed integer */ | |
| 2562 case 7: { /* IEEE floating point */ | |
| 2563 u64 x; | |
| 2564 u32 y; | |
| 2565 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) | |
| 2566 /* Verify that integers and floating point values use the same | |
| 2567 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is | |
| 2568 ** defined that 64-bit floating point values really are mixed | |
| 2569 ** endian. | |
| 2570 */ | |
| 2571 static const u64 t1 = ((u64)0x3ff00000)<<32; | |
| 2572 static const double r1 = 1.0; | |
| 2573 u64 t2 = t1; | |
| 2574 swapMixedEndianFloat(t2); | |
| 2575 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); | |
| 2576 #endif | |
| 2577 | |
| 2578 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; | |
| 2579 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; | |
| 2580 x = (x<<32) | y; | |
| 2581 if( serial_type==6 ){ | |
| 2582 pMem->u.i = *(i64*)&x; | |
| 2583 pMem->flags = MEM_Int; | |
| 2584 }else{ | |
| 2585 assert( sizeof(x)==8 && sizeof(pMem->r)==8 ); | |
| 2586 swapMixedEndianFloat(x); | |
| 2587 memcpy(&pMem->r, &x, sizeof(x)); | |
| 2588 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real; | |
| 2589 } | |
| 2590 return 8; | |
| 2591 } | |
| 2592 case 8: /* Integer 0 */ | |
| 2593 case 9: { /* Integer 1 */ | |
| 2594 pMem->u.i = serial_type-8; | |
| 2595 pMem->flags = MEM_Int; | |
| 2596 return 0; | |
| 2597 } | |
| 2598 default: { | |
| 2599 u32 len = (serial_type-12)/2; | |
| 2600 pMem->z = (char *)buf; | |
| 2601 pMem->n = len; | |
| 2602 pMem->xDel = 0; | |
| 2603 if( serial_type&0x01 ){ | |
| 2604 pMem->flags = MEM_Str | MEM_Ephem; | |
| 2605 }else{ | |
| 2606 pMem->flags = MEM_Blob | MEM_Ephem; | |
| 2607 } | |
| 2608 return len; | |
| 2609 } | |
| 2610 } | |
| 2611 return 0; | |
| 2612 } | |
| 2613 | |
| 2614 | |
| 2615 /* | |
| 2616 ** Given the nKey-byte encoding of a record in pKey[], parse the | |
| 2617 ** record into a UnpackedRecord structure. Return a pointer to | |
| 2618 ** that structure. | |
| 2619 ** | |
| 2620 ** The calling function might provide szSpace bytes of memory | |
| 2621 ** space at pSpace. This space can be used to hold the returned | |
| 2622 ** VDbeParsedRecord structure if it is large enough. If it is | |
| 2623 ** not big enough, space is obtained from sqlite3_malloc(). | |
| 2624 ** | |
| 2625 ** The returned structure should be closed by a call to | |
| 2626 ** sqlite3VdbeDeleteUnpackedRecord(). | |
| 2627 */ | |
| 2628 UnpackedRecord *sqlite3VdbeRecordUnpack( | |
| 2629 KeyInfo *pKeyInfo, /* Information about the record format */ | |
| 2630 int nKey, /* Size of the binary record */ | |
| 2631 const void *pKey, /* The binary record */ | |
| 2632 char *pSpace, /* Unaligned space available to hold the object */ | |
| 2633 int szSpace /* Size of pSpace[] in bytes */ | |
| 2634 ){ | |
| 2635 const unsigned char *aKey = (const unsigned char *)pKey; | |
| 2636 UnpackedRecord *p; /* The unpacked record that we will return */ | |
| 2637 int nByte; /* Memory space needed to hold p, in bytes */ | |
| 2638 int d; | |
| 2639 u32 idx; | |
| 2640 u16 u; /* Unsigned loop counter */ | |
| 2641 u32 szHdr; | |
| 2642 Mem *pMem; | |
| 2643 int nOff; /* Increase pSpace by this much to 8-byte align it */ | |
| 2644 | |
| 2645 /* | |
| 2646 ** We want to shift the pointer pSpace up such that it is 8-byte aligned. | |
| 2647 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift | |
| 2648 ** it by. If pSpace is already 8-byte aligned, nOff should be zero. | |
| 2649 */ | |
| 2650 nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7; | |
| 2651 pSpace += nOff; | |
| 2652 szSpace -= nOff; | |
| 2653 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1); | |
| 2654 if( nByte>szSpace ){ | |
| 2655 p = sqlite3DbMallocRaw(pKeyInfo->db, nByte); | |
| 2656 if( p==0 ) return 0; | |
| 2657 p->flags = UNPACKED_NEED_FREE | UNPACKED_NEED_DESTROY; | |
| 2658 }else{ | |
| 2659 p = (UnpackedRecord*)pSpace; | |
| 2660 p->flags = UNPACKED_NEED_DESTROY; | |
| 2661 } | |
| 2662 p->pKeyInfo = pKeyInfo; | |
| 2663 p->nField = pKeyInfo->nField + 1; | |
| 2664 p->aMem = pMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))]; | |
| 2665 assert( EIGHT_BYTE_ALIGNMENT(pMem) ); | |
| 2666 idx = getVarint32(aKey, szHdr); | |
| 2667 d = szHdr; | |
| 2668 u = 0; | |
| 2669 while( idx<szHdr && u<p->nField && d<=nKey ){ | |
| 2670 u32 serial_type; | |
| 2671 | |
| 2672 idx += getVarint32(&aKey[idx], serial_type); | |
| 2673 pMem->enc = pKeyInfo->enc; | |
| 2674 pMem->db = pKeyInfo->db; | |
| 2675 pMem->flags = 0; | |
| 2676 pMem->zMalloc = 0; | |
| 2677 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); | |
| 2678 pMem++; | |
| 2679 u++; | |
| 2680 } | |
| 2681 assert( u<=pKeyInfo->nField + 1 ); | |
| 2682 p->nField = u; | |
| 2683 return (void*)p; | |
| 2684 } | |
| 2685 | |
| 2686 /* | |
| 2687 ** This routine destroys a UnpackedRecord object. | |
| 2688 */ | |
| 2689 void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){ | |
| 2690 int i; | |
| 2691 Mem *pMem; | |
| 2692 | |
| 2693 assert( p!=0 ); | |
| 2694 assert( p->flags & UNPACKED_NEED_DESTROY ); | |
| 2695 for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){ | |
| 2696 /* The unpacked record is always constructed by the | |
| 2697 ** sqlite3VdbeUnpackRecord() function above, which makes all | |
| 2698 ** strings and blobs static. And none of the elements are | |
| 2699 ** ever transformed, so there is never anything to delete. | |
| 2700 */ | |
| 2701 if( NEVER(pMem->zMalloc) ) sqlite3VdbeMemRelease(pMem); | |
| 2702 } | |
| 2703 if( p->flags & UNPACKED_NEED_FREE ){ | |
| 2704 sqlite3DbFree(p->pKeyInfo->db, p); | |
| 2705 } | |
| 2706 } | |
| 2707 | |
| 2708 /* | |
| 2709 ** This function compares the two table rows or index records | |
| 2710 ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero | |
| 2711 ** or positive integer if key1 is less than, equal to or | |
| 2712 ** greater than key2. The {nKey1, pKey1} key must be a blob | |
| 2713 ** created by th OP_MakeRecord opcode of the VDBE. The pPKey2 | |
| 2714 ** key must be a parsed key such as obtained from | |
| 2715 ** sqlite3VdbeParseRecord. | |
| 2716 ** | |
| 2717 ** Key1 and Key2 do not have to contain the same number of fields. | |
| 2718 ** The key with fewer fields is usually compares less than the | |
| 2719 ** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set | |
| 2720 ** and the common prefixes are equal, then key1 is less than key2. | |
| 2721 ** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are | |
| 2722 ** equal, then the keys are considered to be equal and | |
| 2723 ** the parts beyond the common prefix are ignored. | |
| 2724 ** | |
| 2725 ** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of | |
| 2726 ** the header of pKey1 is ignored. It is assumed that pKey1 is | |
| 2727 ** an index key, and thus ends with a rowid value. The last byte | |
| 2728 ** of the header will therefore be the serial type of the rowid: | |
| 2729 ** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types. | |
| 2730 ** The serial type of the final rowid will always be a single byte. | |
| 2731 ** By ignoring this last byte of the header, we force the comparison | |
| 2732 ** to ignore the rowid at the end of key1. | |
| 2733 */ | |
| 2734 int sqlite3VdbeRecordCompare( | |
| 2735 int nKey1, const void *pKey1, /* Left key */ | |
| 2736 UnpackedRecord *pPKey2 /* Right key */ | |
| 2737 ){ | |
| 2738 int d1; /* Offset into aKey[] of next data element */ | |
| 2739 u32 idx1; /* Offset into aKey[] of next header element */ | |
| 2740 u32 szHdr1; /* Number of bytes in header */ | |
| 2741 int i = 0; | |
| 2742 int nField; | |
| 2743 int rc = 0; | |
| 2744 const unsigned char *aKey1 = (const unsigned char *)pKey1; | |
| 2745 KeyInfo *pKeyInfo; | |
| 2746 Mem mem1; | |
| 2747 | |
| 2748 pKeyInfo = pPKey2->pKeyInfo; | |
| 2749 mem1.enc = pKeyInfo->enc; | |
| 2750 mem1.db = pKeyInfo->db; | |
| 2751 mem1.flags = 0; | |
| 2752 mem1.u.i = 0; /* not needed, here to silence compiler warning */ | |
| 2753 mem1.zMalloc = 0; | |
| 2754 | |
| 2755 idx1 = getVarint32(aKey1, szHdr1); | |
| 2756 d1 = szHdr1; | |
| 2757 if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){ | |
| 2758 szHdr1--; | |
| 2759 } | |
| 2760 nField = pKeyInfo->nField; | |
| 2761 while( idx1<szHdr1 && i<pPKey2->nField ){ | |
| 2762 u32 serial_type1; | |
| 2763 | |
| 2764 /* Read the serial types for the next element in each key. */ | |
| 2765 idx1 += getVarint32( aKey1+idx1, serial_type1 ); | |
| 2766 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break; | |
| 2767 | |
| 2768 /* Extract the values to be compared. | |
| 2769 */ | |
| 2770 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); | |
| 2771 | |
| 2772 /* Do the comparison | |
| 2773 */ | |
| 2774 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], | |
| 2775 i<nField ? pKeyInfo->aColl[i] : 0); | |
| 2776 if( rc!=0 ){ | |
| 2777 break; | |
| 2778 } | |
| 2779 i++; | |
| 2780 } | |
| 2781 | |
| 2782 /* No memory allocation is ever used on mem1. */ | |
| 2783 if( NEVER(mem1.zMalloc) ) sqlite3VdbeMemRelease(&mem1); | |
| 2784 | |
| 2785 /* If the PREFIX_SEARCH flag is set and all fields except the final | |
| 2786 ** rowid field were equal, then clear the PREFIX_SEARCH flag and set | |
| 2787 ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1). | |
| 2788 ** This is used by the OP_IsUnique opcode. | |
| 2789 */ | |
| 2790 if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){ | |
| 2791 assert( idx1==szHdr1 && rc ); | |
| 2792 assert( mem1.flags & MEM_Int ); | |
| 2793 pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH; | |
| 2794 pPKey2->rowid = mem1.u.i; | |
| 2795 } | |
| 2796 | |
| 2797 if( rc==0 ){ | |
| 2798 /* rc==0 here means that one of the keys ran out of fields and | |
| 2799 ** all the fields up to that point were equal. If the UNPACKED_INCRKEY | |
| 2800 ** flag is set, then break the tie by treating key2 as larger. | |
| 2801 ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes | |
| 2802 ** are considered to be equal. Otherwise, the longer key is the | |
| 2803 ** larger. As it happens, the pPKey2 will always be the longer | |
| 2804 ** if there is a difference. | |
| 2805 */ | |
| 2806 if( pPKey2->flags & UNPACKED_INCRKEY ){ | |
| 2807 rc = -1; | |
| 2808 }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){ | |
| 2809 /* Leave rc==0 */ | |
| 2810 }else if( idx1<szHdr1 ){ | |
| 2811 rc = 1; | |
| 2812 } | |
| 2813 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField | |
| 2814 && pKeyInfo->aSortOrder[i] ){ | |
| 2815 rc = -rc; | |
| 2816 } | |
| 2817 | |
| 2818 return rc; | |
| 2819 } | |
| 2820 | |
| 2821 | |
| 2822 /* | |
| 2823 ** pCur points at an index entry created using the OP_MakeRecord opcode. | |
| 2824 ** Read the rowid (the last field in the record) and store it in *rowid. | |
| 2825 ** Return SQLITE_OK if everything works, or an error code otherwise. | |
| 2826 ** | |
| 2827 ** pCur might be pointing to text obtained from a corrupt database file. | |
| 2828 ** So the content cannot be trusted. Do appropriate checks on the content. | |
| 2829 */ | |
| 2830 int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){ | |
| 2831 i64 nCellKey = 0; | |
| 2832 int rc; | |
| 2833 u32 szHdr; /* Size of the header */ | |
| 2834 u32 typeRowid; /* Serial type of the rowid */ | |
| 2835 u32 lenRowid; /* Size of the rowid */ | |
| 2836 Mem m, v; | |
| 2837 | |
| 2838 UNUSED_PARAMETER(db); | |
| 2839 | |
| 2840 /* Get the size of the index entry. Only indices entries of less | |
| 2841 ** than 2GiB are support - anything large must be database corruption. | |
| 2842 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so | |
| 2843 ** this code can safely assume that nCellKey is 32-bits | |
| 2844 */ | |
| 2845 assert( sqlite3BtreeCursorIsValid(pCur) ); | |
| 2846 rc = sqlite3BtreeKeySize(pCur, &nCellKey); | |
| 2847 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ | |
| 2848 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey ); | |
| 2849 | |
| 2850 /* Read in the complete content of the index entry */ | |
| 2851 memset(&m, 0, sizeof(m)); | |
| 2852 rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m); | |
| 2853 if( rc ){ | |
| 2854 return rc; | |
| 2855 } | |
| 2856 | |
| 2857 /* The index entry must begin with a header size */ | |
| 2858 (void)getVarint32((u8*)m.z, szHdr); | |
| 2859 testcase( szHdr==3 ); | |
| 2860 testcase( szHdr==m.n ); | |
| 2861 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){ | |
| 2862 goto idx_rowid_corruption; | |
| 2863 } | |
| 2864 | |
| 2865 /* The last field of the index should be an integer - the ROWID. | |
| 2866 ** Verify that the last entry really is an integer. */ | |
| 2867 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid); | |
| 2868 testcase( typeRowid==1 ); | |
| 2869 testcase( typeRowid==2 ); | |
| 2870 testcase( typeRowid==3 ); | |
| 2871 testcase( typeRowid==4 ); | |
| 2872 testcase( typeRowid==5 ); | |
| 2873 testcase( typeRowid==6 ); | |
| 2874 testcase( typeRowid==8 ); | |
| 2875 testcase( typeRowid==9 ); | |
| 2876 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){ | |
| 2877 goto idx_rowid_corruption; | |
| 2878 } | |
| 2879 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid); | |
| 2880 testcase( (u32)m.n==szHdr+lenRowid ); | |
| 2881 if( unlikely((u32)m.n<szHdr+lenRowid) ){ | |
| 2882 goto idx_rowid_corruption; | |
| 2883 } | |
| 2884 | |
| 2885 /* Fetch the integer off the end of the index record */ | |
| 2886 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); | |
| 2887 *rowid = v.u.i; | |
| 2888 sqlite3VdbeMemRelease(&m); | |
| 2889 return SQLITE_OK; | |
| 2890 | |
| 2891 /* Jump here if database corruption is detected after m has been | |
| 2892 ** allocated. Free the m object and return SQLITE_CORRUPT. */ | |
| 2893 idx_rowid_corruption: | |
| 2894 testcase( m.zMalloc!=0 ); | |
| 2895 sqlite3VdbeMemRelease(&m); | |
| 2896 return SQLITE_CORRUPT_BKPT; | |
| 2897 } | |
| 2898 | |
| 2899 /* | |
| 2900 ** Compare the key of the index entry that cursor pC is pointing to against | |
| 2901 ** the key string in pUnpacked. Write into *pRes a number | |
| 2902 ** that is negative, zero, or positive if pC is less than, equal to, | |
| 2903 ** or greater than pUnpacked. Return SQLITE_OK on success. | |
| 2904 ** | |
| 2905 ** pUnpacked is either created without a rowid or is truncated so that it | |
| 2906 ** omits the rowid at the end. The rowid at the end of the index entry | |
| 2907 ** is ignored as well. Hence, this routine only compares the prefixes | |
| 2908 ** of the keys prior to the final rowid, not the entire key. | |
| 2909 */ | |
| 2910 int sqlite3VdbeIdxKeyCompare( | |
| 2911 VdbeCursor *pC, /* The cursor to compare against */ | |
| 2912 UnpackedRecord *pUnpacked, /* Unpacked version of key to compare against */ | |
| 2913 int *res /* Write the comparison result here */ | |
| 2914 ){ | |
| 2915 i64 nCellKey = 0; | |
| 2916 int rc; | |
| 2917 BtCursor *pCur = pC->pCursor; | |
| 2918 Mem m; | |
| 2919 | |
| 2920 assert( sqlite3BtreeCursorIsValid(pCur) ); | |
| 2921 rc = sqlite3BtreeKeySize(pCur, &nCellKey); | |
| 2922 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ | |
| 2923 /* nCellKey will always be between 0 and 0xffffffff because of the say | |
| 2924 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ | |
| 2925 if( nCellKey<=0 || nCellKey>0x7fffffff ){ | |
| 2926 *res = 0; | |
| 2927 return SQLITE_CORRUPT; | |
| 2928 } | |
| 2929 memset(&m, 0, sizeof(m)); | |
| 2930 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m); | |
| 2931 if( rc ){ | |
| 2932 return rc; | |
| 2933 } | |
| 2934 assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID ); | |
| 2935 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); | |
| 2936 sqlite3VdbeMemRelease(&m); | |
| 2937 return SQLITE_OK; | |
| 2938 } | |
| 2939 | |
| 2940 /* | |
| 2941 ** This routine sets the value to be returned by subsequent calls to | |
| 2942 ** sqlite3_changes() on the database handle 'db'. | |
| 2943 */ | |
| 2944 void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){ | |
| 2945 assert( sqlite3_mutex_held(db->mutex) ); | |
| 2946 db->nChange = nChange; | |
| 2947 db->nTotalChange += nChange; | |
| 2948 } | |
| 2949 | |
| 2950 /* | |
| 2951 ** Set a flag in the vdbe to update the change counter when it is finalised | |
| 2952 ** or reset. | |
| 2953 */ | |
| 2954 void sqlite3VdbeCountChanges(Vdbe *v){ | |
| 2955 v->changeCntOn = 1; | |
| 2956 } | |
| 2957 | |
| 2958 /* | |
| 2959 ** Mark every prepared statement associated with a database connection | |
| 2960 ** as expired. | |
| 2961 ** | |
| 2962 ** An expired statement means that recompilation of the statement is | |
| 2963 ** recommend. Statements expire when things happen that make their | |
| 2964 ** programs obsolete. Removing user-defined functions or collating | |
| 2965 ** sequences, or changing an authorization function are the types of | |
| 2966 ** things that make prepared statements obsolete. | |
| 2967 */ | |
| 2968 void sqlite3ExpirePreparedStatements(sqlite3 *db){ | |
| 2969 Vdbe *p; | |
| 2970 for(p = db->pVdbe; p; p=p->pNext){ | |
| 2971 p->expired = 1; | |
| 2972 } | |
| 2973 } | |
| 2974 | |
| 2975 /* | |
| 2976 ** Return the database associated with the Vdbe. | |
| 2977 */ | |
| 2978 sqlite3 *sqlite3VdbeDb(Vdbe *v){ | |
| 2979 return v->db; | |
| 2980 } | |
| OLD | NEW |