00001 /* 00002 ** 2003 September 6 00003 ** 00004 ** The author disclaims copyright to this source code. In place of 00005 ** a legal notice, here is a blessing: 00006 ** 00007 ** May you do good and not evil. 00008 ** May you find forgiveness for yourself and forgive others. 00009 ** May you share freely, never taking more than you give. 00010 ** 00011 ************************************************************************* 00012 ** This file contains code used for creating, destroying, and populating 00013 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior 00014 ** to version 2.8.7, all this code was combined into the vdbe.c source file. 00015 ** But that file was getting too big so this subroutines were split out. 00016 ** 00017 ** $Id: vdbeaux.c,v 1.418 2008/11/05 17:41:19 drh Exp $ 00018 */ 00019 #include "sqliteInt.h" 00020 #include <ctype.h> 00021 #include "vdbeInt.h" 00022 00023 00024 00025 /* 00026 ** When debugging the code generator in a symbolic debugger, one can 00027 ** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed 00028 ** as they are added to the instruction stream. 00029 */ 00030 #ifdef SQLITE_DEBUG 00031 int sqlite3VdbeAddopTrace = 0; 00032 #endif 00033 00034 00035 /* 00036 ** Create a new virtual database engine. 00037 */ 00038 Vdbe *sqlite3VdbeCreate(sqlite3 *db){ 00039 Vdbe *p; 00040 p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); 00041 if( p==0 ) return 0; 00042 p->db = db; 00043 if( db->pVdbe ){ 00044 db->pVdbe->pPrev = p; 00045 } 00046 p->pNext = db->pVdbe; 00047 p->pPrev = 0; 00048 db->pVdbe = p; 00049 p->magic = VDBE_MAGIC_INIT; 00050 return p; 00051 } 00052 00053 /* 00054 ** Remember the SQL string for a prepared statement. 00055 */ 00056 void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){ 00057 if( p==0 ) return; 00058 assert( p->zSql==0 ); 00059 p->zSql = sqlite3DbStrNDup(p->db, z, n); 00060 } 00061 00062 /* 00063 ** Return the SQL associated with a prepared statement 00064 */ 00065 const char *sqlite3_sql(sqlite3_stmt *pStmt){ 00066 return ((Vdbe *)pStmt)->zSql; 00067 } 00068 00069 /* 00070 ** Swap all content between two VDBE structures. 00071 */ 00072 void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ 00073 Vdbe tmp, *pTmp; 00074 char *zTmp; 00075 int nTmp; 00076 tmp = *pA; 00077 *pA = *pB; 00078 *pB = tmp; 00079 pTmp = pA->pNext; 00080 pA->pNext = pB->pNext; 00081 pB->pNext = pTmp; 00082 pTmp = pA->pPrev; 00083 pA->pPrev = pB->pPrev; 00084 pB->pPrev = pTmp; 00085 zTmp = pA->zSql; 00086 pA->zSql = pB->zSql; 00087 pB->zSql = zTmp; 00088 nTmp = pA->nSql; 00089 pA->nSql = pB->nSql; 00090 pB->nSql = nTmp; 00091 } 00092 00093 #ifdef SQLITE_DEBUG 00094 /* 00095 ** Turn tracing on or off 00096 */ 00097 void sqlite3VdbeTrace(Vdbe *p, FILE *trace){ 00098 p->trace = trace; 00099 } 00100 #endif 00101 00102 /* 00103 ** Resize the Vdbe.aOp array so that it contains at least N 00104 ** elements. 00105 ** 00106 ** If an out-of-memory error occurs while resizing the array, 00107 ** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that 00108 ** any opcodes already allocated can be correctly deallocated 00109 ** along with the rest of the Vdbe). 00110 */ 00111 static void resizeOpArray(Vdbe *p, int N){ 00112 VdbeOp *pNew; 00113 pNew = sqlite3DbRealloc(p->db, p->aOp, N*sizeof(Op)); 00114 if( pNew ){ 00115 p->nOpAlloc = N; 00116 p->aOp = pNew; 00117 } 00118 } 00119 00120 /* 00121 ** Add a new instruction to the list of instructions current in the 00122 ** VDBE. Return the address of the new instruction. 00123 ** 00124 ** Parameters: 00125 ** 00126 ** p Pointer to the VDBE 00127 ** 00128 ** op The opcode for this instruction 00129 ** 00130 ** p1, p2, p3 Operands 00131 ** 00132 ** Use the sqlite3VdbeResolveLabel() function to fix an address and 00133 ** the sqlite3VdbeChangeP4() function to change the value of the P4 00134 ** operand. 00135 */ 00136 int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ 00137 int i; 00138 VdbeOp *pOp; 00139 00140 i = p->nOp; 00141 assert( p->magic==VDBE_MAGIC_INIT ); 00142 if( p->nOpAlloc<=i ){ 00143 resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op)); 00144 if( p->db->mallocFailed ){ 00145 return 0; 00146 } 00147 } 00148 p->nOp++; 00149 pOp = &p->aOp[i]; 00150 pOp->opcode = op; 00151 pOp->p5 = 0; 00152 pOp->p1 = p1; 00153 pOp->p2 = p2; 00154 pOp->p3 = p3; 00155 pOp->p4.p = 0; 00156 pOp->p4type = P4_NOTUSED; 00157 p->expired = 0; 00158 #ifdef SQLITE_DEBUG 00159 pOp->zComment = 0; 00160 if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]); 00161 #endif 00162 #ifdef VDBE_PROFILE 00163 pOp->cycles = 0; 00164 pOp->cnt = 0; 00165 #endif 00166 return i; 00167 } 00168 int sqlite3VdbeAddOp0(Vdbe *p, int op){ 00169 return sqlite3VdbeAddOp3(p, op, 0, 0, 0); 00170 } 00171 int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ 00172 return sqlite3VdbeAddOp3(p, op, p1, 0, 0); 00173 } 00174 int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ 00175 return sqlite3VdbeAddOp3(p, op, p1, p2, 0); 00176 } 00177 00178 00179 /* 00180 ** Add an opcode that includes the p4 value as a pointer. 00181 */ 00182 int sqlite3VdbeAddOp4( 00183 Vdbe *p, /* Add the opcode to this VM */ 00184 int op, /* The new opcode */ 00185 int p1, /* The P1 operand */ 00186 int p2, /* The P2 operand */ 00187 int p3, /* The P3 operand */ 00188 const char *zP4, /* The P4 operand */ 00189 int p4type /* P4 operand type */ 00190 ){ 00191 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); 00192 sqlite3VdbeChangeP4(p, addr, zP4, p4type); 00193 return addr; 00194 } 00195 00196 /* 00197 ** Create a new symbolic label for an instruction that has yet to be 00198 ** coded. The symbolic label is really just a negative number. The 00199 ** label can be used as the P2 value of an operation. Later, when 00200 ** the label is resolved to a specific address, the VDBE will scan 00201 ** through its operation list and change all values of P2 which match 00202 ** the label into the resolved address. 00203 ** 00204 ** The VDBE knows that a P2 value is a label because labels are 00205 ** always negative and P2 values are suppose to be non-negative. 00206 ** Hence, a negative P2 value is a label that has yet to be resolved. 00207 ** 00208 ** Zero is returned if a malloc() fails. 00209 */ 00210 int sqlite3VdbeMakeLabel(Vdbe *p){ 00211 int i; 00212 i = p->nLabel++; 00213 assert( p->magic==VDBE_MAGIC_INIT ); 00214 if( i>=p->nLabelAlloc ){ 00215 p->nLabelAlloc = p->nLabelAlloc*2 + 10; 00216 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, 00217 p->nLabelAlloc*sizeof(p->aLabel[0])); 00218 } 00219 if( p->aLabel ){ 00220 p->aLabel[i] = -1; 00221 } 00222 return -1-i; 00223 } 00224 00225 /* 00226 ** Resolve label "x" to be the address of the next instruction to 00227 ** be inserted. The parameter "x" must have been obtained from 00228 ** a prior call to sqlite3VdbeMakeLabel(). 00229 */ 00230 void sqlite3VdbeResolveLabel(Vdbe *p, int x){ 00231 int j = -1-x; 00232 assert( p->magic==VDBE_MAGIC_INIT ); 00233 assert( j>=0 && j<p->nLabel ); 00234 if( p->aLabel ){ 00235 p->aLabel[j] = p->nOp; 00236 } 00237 } 00238 00239 /* 00240 ** Loop through the program looking for P2 values that are negative 00241 ** on jump instructions. Each such value is a label. Resolve the 00242 ** label by setting the P2 value to its correct non-zero value. 00243 ** 00244 ** This routine is called once after all opcodes have been inserted. 00245 ** 00246 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument 00247 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by 00248 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array. 00249 ** 00250 ** This routine also does the following optimization: It scans for 00251 ** instructions that might cause a statement rollback. Such instructions 00252 ** are: 00253 ** 00254 ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort. 00255 ** * OP_Destroy 00256 ** * OP_VUpdate 00257 ** * OP_VRename 00258 ** 00259 ** If no such instruction is found, then every Statement instruction 00260 ** is changed to a Noop. In this way, we avoid creating the statement 00261 ** journal file unnecessarily. 00262 */ 00263 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ 00264 int i; 00265 int nMaxArgs = 0; 00266 Op *pOp; 00267 int *aLabel = p->aLabel; 00268 int doesStatementRollback = 0; 00269 int hasStatementBegin = 0; 00270 p->readOnly = 1; 00271 p->usesStmtJournal = 0; 00272 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ 00273 u8 opcode = pOp->opcode; 00274 00275 if( opcode==OP_Function || opcode==OP_AggStep ){ 00276 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5; 00277 #ifndef SQLITE_OMIT_VIRTUALTABLE 00278 }else if( opcode==OP_VUpdate ){ 00279 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; 00280 #endif 00281 } 00282 if( opcode==OP_Halt ){ 00283 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){ 00284 doesStatementRollback = 1; 00285 } 00286 }else if( opcode==OP_Statement ){ 00287 hasStatementBegin = 1; 00288 p->usesStmtJournal = 1; 00289 }else if( opcode==OP_Destroy ){ 00290 doesStatementRollback = 1; 00291 }else if( opcode==OP_Transaction && pOp->p2!=0 ){ 00292 p->readOnly = 0; 00293 #ifndef SQLITE_OMIT_VIRTUALTABLE 00294 }else if( opcode==OP_VUpdate || opcode==OP_VRename ){ 00295 doesStatementRollback = 1; 00296 }else if( opcode==OP_VFilter ){ 00297 int n; 00298 assert( p->nOp - i >= 3 ); 00299 assert( pOp[-1].opcode==OP_Integer ); 00300 n = pOp[-1].p1; 00301 if( n>nMaxArgs ) nMaxArgs = n; 00302 #endif 00303 } 00304 00305 if( sqlite3VdbeOpcodeHasProperty(opcode, OPFLG_JUMP) && pOp->p2<0 ){ 00306 assert( -1-pOp->p2<p->nLabel ); 00307 pOp->p2 = aLabel[-1-pOp->p2]; 00308 } 00309 } 00310 sqlite3DbFree(p->db, p->aLabel); 00311 p->aLabel = 0; 00312 00313 *pMaxFuncArgs = nMaxArgs; 00314 00315 /* If we never rollback a statement transaction, then statement 00316 ** transactions are not needed. So change every OP_Statement 00317 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive() 00318 ** which can be expensive on some platforms. 00319 */ 00320 if( hasStatementBegin && !doesStatementRollback ){ 00321 p->usesStmtJournal = 0; 00322 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ 00323 if( pOp->opcode==OP_Statement ){ 00324 pOp->opcode = OP_Noop; 00325 } 00326 } 00327 } 00328 } 00329 00330 /* 00331 ** Return the address of the next instruction to be inserted. 00332 */ 00333 int sqlite3VdbeCurrentAddr(Vdbe *p){ 00334 assert( p->magic==VDBE_MAGIC_INIT ); 00335 return p->nOp; 00336 } 00337 00338 /* 00339 ** Add a whole list of operations to the operation stack. Return the 00340 ** address of the first operation added. 00341 */ 00342 int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ 00343 int addr; 00344 assert( p->magic==VDBE_MAGIC_INIT ); 00345 if( p->nOp + nOp > p->nOpAlloc ){ 00346 resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op)); 00347 assert( p->nOp+nOp<=p->nOpAlloc || p->db->mallocFailed ); 00348 } 00349 if( p->db->mallocFailed ){ 00350 return 0; 00351 } 00352 addr = p->nOp; 00353 if( nOp>0 ){ 00354 int i; 00355 VdbeOpList const *pIn = aOp; 00356 for(i=0; i<nOp; i++, pIn++){ 00357 int p2 = pIn->p2; 00358 VdbeOp *pOut = &p->aOp[i+addr]; 00359 pOut->opcode = pIn->opcode; 00360 pOut->p1 = pIn->p1; 00361 if( p2<0 && sqlite3VdbeOpcodeHasProperty(pOut->opcode, OPFLG_JUMP) ){ 00362 pOut->p2 = addr + ADDR(p2); 00363 }else{ 00364 pOut->p2 = p2; 00365 } 00366 pOut->p3 = pIn->p3; 00367 pOut->p4type = P4_NOTUSED; 00368 pOut->p4.p = 0; 00369 pOut->p5 = 0; 00370 #ifdef SQLITE_DEBUG 00371 pOut->zComment = 0; 00372 if( sqlite3VdbeAddopTrace ){ 00373 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); 00374 } 00375 #endif 00376 } 00377 p->nOp += nOp; 00378 } 00379 return addr; 00380 } 00381 00382 /* 00383 ** Change the value of the P1 operand for a specific instruction. 00384 ** This routine is useful when a large program is loaded from a 00385 ** static array using sqlite3VdbeAddOpList but we want to make a 00386 ** few minor changes to the program. 00387 */ 00388 void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ 00389 assert( p==0 || p->magic==VDBE_MAGIC_INIT ); 00390 if( p && addr>=0 && p->nOp>addr && p->aOp ){ 00391 p->aOp[addr].p1 = val; 00392 } 00393 } 00394 00395 /* 00396 ** Change the value of the P2 operand for a specific instruction. 00397 ** This routine is useful for setting a jump destination. 00398 */ 00399 void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ 00400 assert( p==0 || p->magic==VDBE_MAGIC_INIT ); 00401 if( p && addr>=0 && p->nOp>addr && p->aOp ){ 00402 p->aOp[addr].p2 = val; 00403 } 00404 } 00405 00406 /* 00407 ** Change the value of the P3 operand for a specific instruction. 00408 */ 00409 void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){ 00410 assert( p==0 || p->magic==VDBE_MAGIC_INIT ); 00411 if( p && addr>=0 && p->nOp>addr && p->aOp ){ 00412 p->aOp[addr].p3 = val; 00413 } 00414 } 00415 00416 /* 00417 ** Change the value of the P5 operand for the most recently 00418 ** added operation. 00419 */ 00420 void sqlite3VdbeChangeP5(Vdbe *p, u8 val){ 00421 assert( p==0 || p->magic==VDBE_MAGIC_INIT ); 00422 if( p && p->aOp ){ 00423 assert( p->nOp>0 ); 00424 p->aOp[p->nOp-1].p5 = val; 00425 } 00426 } 00427 00428 /* 00429 ** Change the P2 operand of instruction addr so that it points to 00430 ** the address of the next instruction to be coded. 00431 */ 00432 void sqlite3VdbeJumpHere(Vdbe *p, int addr){ 00433 sqlite3VdbeChangeP2(p, addr, p->nOp); 00434 } 00435 00436 00437 /* 00438 ** If the input FuncDef structure is ephemeral, then free it. If 00439 ** the FuncDef is not ephermal, then do nothing. 00440 */ 00441 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ 00442 if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){ 00443 sqlite3DbFree(db, pDef); 00444 } 00445 } 00446 00447 /* 00448 ** Delete a P4 value if necessary. 00449 */ 00450 static void freeP4(sqlite3 *db, int p4type, void *p4){ 00451 if( p4 ){ 00452 switch( p4type ){ 00453 case P4_REAL: 00454 case P4_INT64: 00455 case P4_MPRINTF: 00456 case P4_DYNAMIC: 00457 case P4_KEYINFO: 00458 case P4_INTARRAY: 00459 case P4_KEYINFO_HANDOFF: { 00460 sqlite3DbFree(db, p4); 00461 break; 00462 } 00463 case P4_VDBEFUNC: { 00464 VdbeFunc *pVdbeFunc = (VdbeFunc *)p4; 00465 freeEphemeralFunction(db, pVdbeFunc->pFunc); 00466 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0); 00467 sqlite3DbFree(db, pVdbeFunc); 00468 break; 00469 } 00470 case P4_FUNCDEF: { 00471 freeEphemeralFunction(db, (FuncDef*)p4); 00472 break; 00473 } 00474 case P4_MEM: { 00475 sqlite3ValueFree((sqlite3_value*)p4); 00476 break; 00477 } 00478 } 00479 } 00480 } 00481 00482 00483 /* 00484 ** Change N opcodes starting at addr to No-ops. 00485 */ 00486 void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ 00487 if( p && p->aOp ){ 00488 VdbeOp *pOp = &p->aOp[addr]; 00489 sqlite3 *db = p->db; 00490 while( N-- ){ 00491 freeP4(db, pOp->p4type, pOp->p4.p); 00492 memset(pOp, 0, sizeof(pOp[0])); 00493 pOp->opcode = OP_Noop; 00494 pOp++; 00495 } 00496 } 00497 } 00498 00499 /* 00500 ** Change the value of the P4 operand for a specific instruction. 00501 ** This routine is useful when a large program is loaded from a 00502 ** static array using sqlite3VdbeAddOpList but we want to make a 00503 ** few minor changes to the program. 00504 ** 00505 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of 00506 ** the string is made into memory obtained from sqlite3_malloc(). 00507 ** A value of n==0 means copy bytes of zP4 up to and including the 00508 ** first null byte. If n>0 then copy n+1 bytes of zP4. 00509 ** 00510 ** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure. 00511 ** A copy is made of the KeyInfo structure into memory obtained from 00512 ** sqlite3_malloc, to be freed when the Vdbe is finalized. 00513 ** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure 00514 ** stored in memory that the caller has obtained from sqlite3_malloc. The 00515 ** caller should not free the allocation, it will be freed when the Vdbe is 00516 ** finalized. 00517 ** 00518 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points 00519 ** to a string or structure that is guaranteed to exist for the lifetime of 00520 ** the Vdbe. In these cases we can just copy the pointer. 00521 ** 00522 ** If addr<0 then change P4 on the most recently inserted instruction. 00523 */ 00524 void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ 00525 Op *pOp; 00526 sqlite3 *db; 00527 assert( p!=0 ); 00528 db = p->db; 00529 assert( p->magic==VDBE_MAGIC_INIT ); 00530 if( p->aOp==0 || db->mallocFailed ){ 00531 if (n != P4_KEYINFO) { 00532 freeP4(db, n, (void*)*(char**)&zP4); 00533 } 00534 return; 00535 } 00536 assert( addr<p->nOp ); 00537 if( addr<0 ){ 00538 addr = p->nOp - 1; 00539 if( addr<0 ) return; 00540 } 00541 pOp = &p->aOp[addr]; 00542 freeP4(db, pOp->p4type, pOp->p4.p); 00543 pOp->p4.p = 0; 00544 if( n==P4_INT32 ){ 00545 /* Note: this cast is safe, because the origin data point was an int 00546 ** that was cast to a (const char *). */ 00547 pOp->p4.i = SQLITE_PTR_TO_INT(zP4); 00548 pOp->p4type = n; 00549 }else if( zP4==0 ){ 00550 pOp->p4.p = 0; 00551 pOp->p4type = P4_NOTUSED; 00552 }else if( n==P4_KEYINFO ){ 00553 KeyInfo *pKeyInfo; 00554 int nField, nByte; 00555 00556 nField = ((KeyInfo*)zP4)->nField; 00557 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; 00558 pKeyInfo = sqlite3Malloc( nByte ); 00559 pOp->p4.pKeyInfo = pKeyInfo; 00560 if( pKeyInfo ){ 00561 u8 *aSortOrder; 00562 memcpy(pKeyInfo, zP4, nByte); 00563 aSortOrder = pKeyInfo->aSortOrder; 00564 if( aSortOrder ){ 00565 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField]; 00566 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField); 00567 } 00568 pOp->p4type = P4_KEYINFO; 00569 }else{ 00570 p->db->mallocFailed = 1; 00571 pOp->p4type = P4_NOTUSED; 00572 } 00573 }else if( n==P4_KEYINFO_HANDOFF ){ 00574 pOp->p4.p = (void*)zP4; 00575 pOp->p4type = P4_KEYINFO; 00576 }else if( n<0 ){ 00577 pOp->p4.p = (void*)zP4; 00578 pOp->p4type = n; 00579 }else{ 00580 if( n==0 ) n = strlen(zP4); 00581 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n); 00582 pOp->p4type = P4_DYNAMIC; 00583 } 00584 } 00585 00586 #ifndef NDEBUG 00587 /* 00588 ** Change the comment on the the most recently coded instruction. Or 00589 ** insert a No-op and add the comment to that new instruction. This 00590 ** makes the code easier to read during debugging. None of this happens 00591 ** in a production build. 00592 */ 00593 void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ 00594 va_list ap; 00595 assert( p->nOp>0 || p->aOp==0 ); 00596 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); 00597 if( p->nOp ){ 00598 char **pz = &p->aOp[p->nOp-1].zComment; 00599 va_start(ap, zFormat); 00600 sqlite3DbFree(p->db, *pz); 00601 *pz = sqlite3VMPrintf(p->db, zFormat, ap); 00602 va_end(ap); 00603 } 00604 } 00605 void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ 00606 va_list ap; 00607 sqlite3VdbeAddOp0(p, OP_Noop); 00608 assert( p->nOp>0 || p->aOp==0 ); 00609 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); 00610 if( p->nOp ){ 00611 char **pz = &p->aOp[p->nOp-1].zComment; 00612 va_start(ap, zFormat); 00613 sqlite3DbFree(p->db, *pz); 00614 *pz = sqlite3VMPrintf(p->db, zFormat, ap); 00615 va_end(ap); 00616 } 00617 } 00618 #endif /* NDEBUG */ 00619 00620 /* 00621 ** Return the opcode for a given address. 00622 */ 00623 VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ 00624 assert( p->magic==VDBE_MAGIC_INIT ); 00625 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); 00626 return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0); 00627 } 00628 00629 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ 00630 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) 00631 /* 00632 ** Compute a string that describes the P4 parameter for an opcode. 00633 ** Use zTemp for any required temporary buffer space. 00634 */ 00635 static char *displayP4(Op *pOp, char *zTemp, int nTemp){ 00636 char *zP4 = zTemp; 00637 assert( nTemp>=20 ); 00638 switch( pOp->p4type ){ 00639 case P4_KEYINFO_STATIC: 00640 case P4_KEYINFO: { 00641 int i, j; 00642 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; 00643 sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField); 00644 i = strlen(zTemp); 00645 for(j=0; j<pKeyInfo->nField; j++){ 00646 CollSeq *pColl = pKeyInfo->aColl[j]; 00647 if( pColl ){ 00648 int n = strlen(pColl->zName); 00649 if( i+n>nTemp-6 ){ 00650 memcpy(&zTemp[i],",...",4); 00651 break; 00652 } 00653 zTemp[i++] = ','; 00654 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){ 00655 zTemp[i++] = '-'; 00656 } 00657 memcpy(&zTemp[i], pColl->zName,n+1); 00658 i += n; 00659 }else if( i+4<nTemp-6 ){ 00660 memcpy(&zTemp[i],",nil",4); 00661 i += 4; 00662 } 00663 } 00664 zTemp[i++] = ')'; 00665 zTemp[i] = 0; 00666 assert( i<nTemp ); 00667 break; 00668 } 00669 case P4_COLLSEQ: { 00670 CollSeq *pColl = pOp->p4.pColl; 00671 sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName); 00672 break; 00673 } 00674 case P4_FUNCDEF: { 00675 FuncDef *pDef = pOp->p4.pFunc; 00676 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); 00677 break; 00678 } 00679 case P4_INT64: { 00680 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64); 00681 break; 00682 } 00683 case P4_INT32: { 00684 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i); 00685 break; 00686 } 00687 case P4_REAL: { 00688 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal); 00689 break; 00690 } 00691 case P4_MEM: { 00692 Mem *pMem = pOp->p4.pMem; 00693 assert( (pMem->flags & MEM_Null)==0 ); 00694 if( pMem->flags & MEM_Str ){ 00695 zP4 = pMem->z; 00696 }else if( pMem->flags & MEM_Int ){ 00697 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i); 00698 }else if( pMem->flags & MEM_Real ){ 00699 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r); 00700 } 00701 break; 00702 } 00703 #ifndef SQLITE_OMIT_VIRTUALTABLE 00704 case P4_VTAB: { 00705 sqlite3_vtab *pVtab = pOp->p4.pVtab; 00706 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); 00707 break; 00708 } 00709 #endif 00710 case P4_INTARRAY: { 00711 sqlite3_snprintf(nTemp, zTemp, "intarray"); 00712 break; 00713 } 00714 default: { 00715 zP4 = pOp->p4.z; 00716 if( zP4==0 ){ 00717 zP4 = zTemp; 00718 zTemp[0] = 0; 00719 } 00720 } 00721 } 00722 assert( zP4!=0 ); 00723 return zP4; 00724 } 00725 #endif 00726 00727 /* 00728 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. 00729 ** 00730 */ 00731 void sqlite3VdbeUsesBtree(Vdbe *p, int i){ 00732 int mask; 00733 assert( i>=0 && i<p->db->nDb ); 00734 assert( i<sizeof(p->btreeMask)*8 ); 00735 mask = 1<<i; 00736 if( (p->btreeMask & mask)==0 ){ 00737 p->btreeMask |= mask; 00738 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt); 00739 } 00740 } 00741 00742 00743 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) 00744 /* 00745 ** Print a single opcode. This routine is used for debugging only. 00746 */ 00747 void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ 00748 char *zP4; 00749 char zPtr[50]; 00750 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n"; 00751 if( pOut==0 ) pOut = stdout; 00752 zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); 00753 fprintf(pOut, zFormat1, pc, 00754 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, 00755 #ifdef SQLITE_DEBUG 00756 pOp->zComment ? pOp->zComment : "" 00757 #else 00758 "" 00759 #endif 00760 ); 00761 fflush(pOut); 00762 } 00763 #endif 00764 00765 /* 00766 ** Release an array of N Mem elements 00767 */ 00768 static void releaseMemArray(Mem *p, int N){ 00769 if( p && N ){ 00770 Mem *pEnd; 00771 sqlite3 *db = p->db; 00772 int malloc_failed = db->mallocFailed; 00773 for(pEnd=&p[N]; p<pEnd; p++){ 00774 assert( (&p[1])==pEnd || p[0].db==p[1].db ); 00775 00776 /* This block is really an inlined version of sqlite3VdbeMemRelease() 00777 ** that takes advantage of the fact that the memory cell value is 00778 ** being set to NULL after releasing any dynamic resources. 00779 ** 00780 ** The justification for duplicating code is that according to 00781 ** callgrind, this causes a certain test case to hit the CPU 4.7 00782 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if 00783 ** sqlite3MemRelease() were called from here. With -O2, this jumps 00784 ** to 6.6 percent. The test case is inserting 1000 rows into a table 00785 ** with no indexes using a single prepared INSERT statement, bind() 00786 ** and reset(). Inserts are grouped into a transaction. 00787 */ 00788 if( p->flags&(MEM_Agg|MEM_Dyn) ){ 00789 sqlite3VdbeMemRelease(p); 00790 }else if( p->zMalloc ){ 00791 sqlite3DbFree(db, p->zMalloc); 00792 p->zMalloc = 0; 00793 } 00794 00795 p->flags = MEM_Null; 00796 } 00797 db->mallocFailed = malloc_failed; 00798 } 00799 } 00800 00801 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 00802 int sqlite3VdbeReleaseBuffers(Vdbe *p){ 00803 int ii; 00804 int nFree = 0; 00805 assert( sqlite3_mutex_held(p->db->mutex) ); 00806 for(ii=1; ii<=p->nMem; ii++){ 00807 Mem *pMem = &p->aMem[ii]; 00808 if( pMem->z && pMem->flags&MEM_Dyn ){ 00809 assert( !pMem->xDel ); 00810 nFree += sqlite3DbMallocSize(pMem->db, pMem->z); 00811 sqlite3VdbeMemRelease(pMem); 00812 } 00813 } 00814 return nFree; 00815 } 00816 #endif 00817 00818 #ifndef SQLITE_OMIT_EXPLAIN 00819 /* 00820 ** Give a listing of the program in the virtual machine. 00821 ** 00822 ** The interface is the same as sqlite3VdbeExec(). But instead of 00823 ** running the code, it invokes the callback once for each instruction. 00824 ** This feature is used to implement "EXPLAIN". 00825 ** 00826 ** When p->explain==1, each instruction is listed. When 00827 ** p->explain==2, only OP_Explain instructions are listed and these 00828 ** are shown in a different format. p->explain==2 is used to implement 00829 ** EXPLAIN QUERY PLAN. 00830 */ 00831 int sqlite3VdbeList( 00832 Vdbe *p /* The VDBE */ 00833 ){ 00834 sqlite3 *db = p->db; 00835 int i; 00836 int rc = SQLITE_OK; 00837 Mem *pMem = p->pResultSet = &p->aMem[1]; 00838 00839 assert( p->explain ); 00840 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; 00841 assert( db->magic==SQLITE_MAGIC_BUSY ); 00842 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); 00843 00844 /* Even though this opcode does not use dynamic strings for 00845 ** the result, result columns may become dynamic if the user calls 00846 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. 00847 */ 00848 releaseMemArray(pMem, p->nMem); 00849 00850 do{ 00851 i = p->pc++; 00852 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain ); 00853 if( i>=p->nOp ){ 00854 p->rc = SQLITE_OK; 00855 rc = SQLITE_DONE; 00856 }else if( db->u1.isInterrupted ){ 00857 p->rc = SQLITE_INTERRUPT; 00858 rc = SQLITE_ERROR; 00859 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc)); 00860 }else{ 00861 char *z; 00862 Op *pOp = &p->aOp[i]; 00863 if( p->explain==1 ){ 00864 pMem->flags = MEM_Int; 00865 pMem->type = SQLITE_INTEGER; 00866 pMem->u.i = i; /* Program counter */ 00867 pMem++; 00868 00869 pMem->flags = MEM_Static|MEM_Str|MEM_Term; 00870 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ 00871 assert( pMem->z!=0 ); 00872 pMem->n = strlen(pMem->z); 00873 pMem->type = SQLITE_TEXT; 00874 pMem->enc = SQLITE_UTF8; 00875 pMem++; 00876 } 00877 00878 pMem->flags = MEM_Int; 00879 pMem->u.i = pOp->p1; /* P1 */ 00880 pMem->type = SQLITE_INTEGER; 00881 pMem++; 00882 00883 pMem->flags = MEM_Int; 00884 pMem->u.i = pOp->p2; /* P2 */ 00885 pMem->type = SQLITE_INTEGER; 00886 pMem++; 00887 00888 if( p->explain==1 ){ 00889 pMem->flags = MEM_Int; 00890 pMem->u.i = pOp->p3; /* P3 */ 00891 pMem->type = SQLITE_INTEGER; 00892 pMem++; 00893 } 00894 00895 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */ 00896 p->db->mallocFailed = 1; 00897 return SQLITE_NOMEM; 00898 } 00899 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; 00900 z = displayP4(pOp, pMem->z, 32); 00901 if( z!=pMem->z ){ 00902 sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0); 00903 }else{ 00904 assert( pMem->z!=0 ); 00905 pMem->n = strlen(pMem->z); 00906 pMem->enc = SQLITE_UTF8; 00907 } 00908 pMem->type = SQLITE_TEXT; 00909 pMem++; 00910 00911 if( p->explain==1 ){ 00912 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){ 00913 p->db->mallocFailed = 1; 00914 return SQLITE_NOMEM; 00915 } 00916 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; 00917 pMem->n = 2; 00918 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ 00919 pMem->type = SQLITE_TEXT; 00920 pMem->enc = SQLITE_UTF8; 00921 pMem++; 00922 00923 #ifdef SQLITE_DEBUG 00924 if( pOp->zComment ){ 00925 pMem->flags = MEM_Str|MEM_Term; 00926 pMem->z = pOp->zComment; 00927 pMem->n = strlen(pMem->z); 00928 pMem->enc = SQLITE_UTF8; 00929 pMem->type = SQLITE_TEXT; 00930 }else 00931 #endif 00932 { 00933 pMem->flags = MEM_Null; /* Comment */ 00934 pMem->type = SQLITE_NULL; 00935 } 00936 } 00937 00938 p->nResColumn = 8 - 5*(p->explain-1); 00939 p->rc = SQLITE_OK; 00940 rc = SQLITE_ROW; 00941 } 00942 return rc; 00943 } 00944 #endif /* SQLITE_OMIT_EXPLAIN */ 00945 00946 #ifdef SQLITE_DEBUG 00947 /* 00948 ** Print the SQL that was used to generate a VDBE program. 00949 */ 00950 void sqlite3VdbePrintSql(Vdbe *p){ 00951 int nOp = p->nOp; 00952 VdbeOp *pOp; 00953 if( nOp<1 ) return; 00954 pOp = &p->aOp[0]; 00955 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ 00956 const char *z = pOp->p4.z; 00957 while( isspace(*(u8*)z) ) z++; 00958 printf("SQL: [%s]\n", z); 00959 } 00960 } 00961 #endif 00962 00963 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) 00964 /* 00965 ** Print an IOTRACE message showing SQL content. 00966 */ 00967 void sqlite3VdbeIOTraceSql(Vdbe *p){ 00968 int nOp = p->nOp; 00969 VdbeOp *pOp; 00970 if( sqlite3IoTrace==0 ) return; 00971 if( nOp<1 ) return; 00972 pOp = &p->aOp[0]; 00973 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ 00974 int i, j; 00975 char z[1000]; 00976 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z); 00977 for(i=0; isspace((unsigned char)z[i]); i++){} 00978 for(j=0; z[i]; i++){ 00979 if( isspace((unsigned char)z[i]) ){ 00980 if( z[i-1]!=' ' ){ 00981 z[j++] = ' '; 00982 } 00983 }else{ 00984 z[j++] = z[i]; 00985 } 00986 } 00987 z[j] = 0; 00988 sqlite3IoTrace("SQL %s\n", z); 00989 } 00990 } 00991 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */ 00992 00993 00994 /* 00995 ** Prepare a virtual machine for execution. This involves things such 00996 ** as allocating stack space and initializing the program counter. 00997 ** After the VDBE has be prepped, it can be executed by one or more 00998 ** calls to sqlite3VdbeExec(). 00999 ** 01000 ** This is the only way to move a VDBE from VDBE_MAGIC_INIT to 01001 ** VDBE_MAGIC_RUN. 01002 */ 01003 void sqlite3VdbeMakeReady( 01004 Vdbe *p, /* The VDBE */ 01005 int nVar, /* Number of '?' see in the SQL statement */ 01006 int nMem, /* Number of memory cells to allocate */ 01007 int nCursor, /* Number of cursors to allocate */ 01008 int isExplain /* True if the EXPLAIN keywords is present */ 01009 ){ 01010 int n; 01011 sqlite3 *db = p->db; 01012 01013 assert( p!=0 ); 01014 assert( p->magic==VDBE_MAGIC_INIT ); 01015 01016 /* There should be at least one opcode. 01017 */ 01018 assert( p->nOp>0 ); 01019 01020 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This 01021 * is because the call to resizeOpArray() below may shrink the 01022 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN 01023 * state. 01024 */ 01025 p->magic = VDBE_MAGIC_RUN; 01026 01027 /* For each cursor required, also allocate a memory cell. Memory 01028 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by 01029 ** the vdbe program. Instead they are used to allocate space for 01030 ** VdbeCursor/BtCursor structures. The blob of memory associated with 01031 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1) 01032 ** stores the blob of memory associated with cursor 1, etc. 01033 ** 01034 ** See also: allocateCursor(). 01035 */ 01036 nMem += nCursor; 01037 01038 /* 01039 ** Allocation space for registers. 01040 */ 01041 if( p->aMem==0 ){ 01042 int nArg; /* Maximum number of args passed to a user function. */ 01043 resolveP2Values(p, &nArg); 01044 /*resizeOpArray(p, p->nOp);*/ 01045 assert( nVar>=0 ); 01046 if( isExplain && nMem<10 ){ 01047 nMem = 10; 01048 } 01049 p->aMem = sqlite3DbMallocZero(db, 01050 nMem*sizeof(Mem) /* aMem */ 01051 + nVar*sizeof(Mem) /* aVar */ 01052 + nArg*sizeof(Mem*) /* apArg */ 01053 + nVar*sizeof(char*) /* azVar */ 01054 + nCursor*sizeof(VdbeCursor*)+1 /* apCsr */ 01055 ); 01056 if( !db->mallocFailed ){ 01057 p->aMem--; /* aMem[] goes from 1..nMem */ 01058 p->nMem = nMem; /* not from 0..nMem-1 */ 01059 p->aVar = &p->aMem[nMem+1]; 01060 p->nVar = nVar; 01061 p->okVar = 0; 01062 p->apArg = (Mem**)&p->aVar[nVar]; 01063 p->azVar = (char**)&p->apArg[nArg]; 01064 p->apCsr = (VdbeCursor**)&p->azVar[nVar]; 01065 p->nCursor = nCursor; 01066 for(n=0; n<nVar; n++){ 01067 p->aVar[n].flags = MEM_Null; 01068 p->aVar[n].db = db; 01069 } 01070 for(n=1; n<=nMem; n++){ 01071 p->aMem[n].flags = MEM_Null; 01072 p->aMem[n].db = db; 01073 } 01074 } 01075 } 01076 #ifdef SQLITE_DEBUG 01077 for(n=1; n<p->nMem; n++){ 01078 assert( p->aMem[n].db==db ); 01079 } 01080 #endif 01081 01082 p->pc = -1; 01083 p->rc = SQLITE_OK; 01084 p->uniqueCnt = 0; 01085 p->errorAction = OE_Abort; 01086 p->explain |= isExplain; 01087 p->magic = VDBE_MAGIC_RUN; 01088 p->nChange = 0; 01089 p->cacheCtr = 1; 01090 p->minWriteFileFormat = 255; 01091 p->openedStatement = 0; 01092 #ifdef VDBE_PROFILE 01093 { 01094 int i; 01095 for(i=0; i<p->nOp; i++){ 01096 p->aOp[i].cnt = 0; 01097 p->aOp[i].cycles = 0; 01098 } 01099 } 01100 #endif 01101 } 01102 01103 /* 01104 ** Close a VDBE cursor and release all the resources that cursor 01105 ** happens to hold. 01106 */ 01107 void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ 01108 if( pCx==0 ){ 01109 return; 01110 } 01111 if( pCx->pBt ){ 01112 sqlite3BtreeClose(pCx->pBt); 01113 /* The pCx->pCursor will be close automatically, if it exists, by 01114 ** the call above. */ 01115 }else if( pCx->pCursor ){ 01116 sqlite3BtreeCloseCursor(pCx->pCursor); 01117 } 01118 #ifndef SQLITE_OMIT_VIRTUALTABLE 01119 if( pCx->pVtabCursor ){ 01120 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; 01121 const sqlite3_module *pModule = pCx->pModule; 01122 p->inVtabMethod = 1; 01123 (void)sqlite3SafetyOff(p->db); 01124 pModule->xClose(pVtabCursor); 01125 (void)sqlite3SafetyOn(p->db); 01126 p->inVtabMethod = 0; 01127 } 01128 #endif 01129 if( !pCx->ephemPseudoTable ){ 01130 sqlite3DbFree(p->db, pCx->pData); 01131 } 01132 } 01133 01134 /* 01135 ** Close all cursors except for VTab cursors that are currently 01136 ** in use. 01137 */ 01138 static void closeAllCursorsExceptActiveVtabs(Vdbe *p){ 01139 int i; 01140 if( p->apCsr==0 ) return; 01141 for(i=0; i<p->nCursor; i++){ 01142 VdbeCursor *pC = p->apCsr[i]; 01143 if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){ 01144 sqlite3VdbeFreeCursor(p, pC); 01145 p->apCsr[i] = 0; 01146 } 01147 } 01148 } 01149 01150 /* 01151 ** Clean up the VM after execution. 01152 ** 01153 ** This routine will automatically close any cursors, lists, and/or 01154 ** sorters that were left open. It also deletes the values of 01155 ** variables in the aVar[] array. 01156 */ 01157 static void Cleanup(Vdbe *p){ 01158 int i; 01159 sqlite3 *db = p->db; 01160 closeAllCursorsExceptActiveVtabs(p); 01161 for(i=1; i<=p->nMem; i++){ 01162 MemSetTypeFlag(&p->aMem[i], MEM_Null); 01163 } 01164 releaseMemArray(&p->aMem[1], p->nMem); 01165 sqlite3VdbeFifoClear(&p->sFifo); 01166 if( p->contextStack ){ 01167 for(i=0; i<p->contextStackTop; i++){ 01168 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo); 01169 } 01170 sqlite3DbFree(db, p->contextStack); 01171 } 01172 p->contextStack = 0; 01173 p->contextStackDepth = 0; 01174 p->contextStackTop = 0; 01175 sqlite3DbFree(db, p->zErrMsg); 01176 p->zErrMsg = 0; 01177 p->pResultSet = 0; 01178 } 01179 01180 /* 01181 ** Set the number of result columns that will be returned by this SQL 01182 ** statement. This is now set at compile time, rather than during 01183 ** execution of the vdbe program so that sqlite3_column_count() can 01184 ** be called on an SQL statement before sqlite3_step(). 01185 */ 01186 void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ 01187 Mem *pColName; 01188 int n; 01189 sqlite3 *db = p->db; 01190 01191 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); 01192 sqlite3DbFree(db, p->aColName); 01193 n = nResColumn*COLNAME_N; 01194 p->nResColumn = nResColumn; 01195 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n ); 01196 if( p->aColName==0 ) return; 01197 while( n-- > 0 ){ 01198 pColName->flags = MEM_Null; 01199 pColName->db = p->db; 01200 pColName++; 01201 } 01202 } 01203 01204 /* 01205 ** Set the name of the idx'th column to be returned by the SQL statement. 01206 ** zName must be a pointer to a nul terminated string. 01207 ** 01208 ** This call must be made after a call to sqlite3VdbeSetNumCols(). 01209 ** 01210 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC 01211 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed 01212 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed. 01213 */ 01214 int sqlite3VdbeSetColName( 01215 Vdbe *p, /* Vdbe being configured */ 01216 int idx, /* Index of column zName applies to */ 01217 int var, /* One of the COLNAME_* constants */ 01218 const char *zName, /* Pointer to buffer containing name */ 01219 void (*xDel)(void*) /* Memory management strategy for zName */ 01220 ){ 01221 int rc; 01222 Mem *pColName; 01223 assert( idx<p->nResColumn ); 01224 assert( var<COLNAME_N ); 01225 if( p->db->mallocFailed ){ 01226 assert( !zName || xDel!=SQLITE_DYNAMIC ); 01227 return SQLITE_NOMEM; 01228 } 01229 assert( p->aColName!=0 ); 01230 pColName = &(p->aColName[idx+var*p->nResColumn]); 01231 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel); 01232 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 ); 01233 return rc; 01234 } 01235 01236 /* 01237 ** A read or write transaction may or may not be active on database handle 01238 ** db. If a transaction is active, commit it. If there is a 01239 ** write-transaction spanning more than one database file, this routine 01240 ** takes care of the master journal trickery. 01241 */ 01242 static int vdbeCommit(sqlite3 *db, Vdbe *p){ 01243 int i; 01244 int nTrans = 0; /* Number of databases with an active write-transaction */ 01245 int rc = SQLITE_OK; 01246 int needXcommit = 0; 01247 01248 /* Before doing anything else, call the xSync() callback for any 01249 ** virtual module tables written in this transaction. This has to 01250 ** be done before determining whether a master journal file is 01251 ** required, as an xSync() callback may add an attached database 01252 ** to the transaction. 01253 */ 01254 rc = sqlite3VtabSync(db, &p->zErrMsg); 01255 if( rc!=SQLITE_OK ){ 01256 return rc; 01257 } 01258 01259 /* This loop determines (a) if the commit hook should be invoked and 01260 ** (b) how many database files have open write transactions, not 01261 ** including the temp database. (b) is important because if more than 01262 ** one database file has an open write transaction, a master journal 01263 ** file is required for an atomic commit. 01264 */ 01265 for(i=0; i<db->nDb; i++){ 01266 Btree *pBt = db->aDb[i].pBt; 01267 if( sqlite3BtreeIsInTrans(pBt) ){ 01268 needXcommit = 1; 01269 if( i!=1 ) nTrans++; 01270 } 01271 } 01272 01273 /* If there are any write-transactions at all, invoke the commit hook */ 01274 if( needXcommit && db->xCommitCallback ){ 01275 (void)sqlite3SafetyOff(db); 01276 rc = db->xCommitCallback(db->pCommitArg); 01277 (void)sqlite3SafetyOn(db); 01278 if( rc ){ 01279 return SQLITE_CONSTRAINT; 01280 } 01281 } 01282 01283 /* The simple case - no more than one database file (not counting the 01284 ** TEMP database) has a transaction active. There is no need for the 01285 ** master-journal. 01286 ** 01287 ** If the return value of sqlite3BtreeGetFilename() is a zero length 01288 ** string, it means the main database is :memory: or a temp file. In 01289 ** that case we do not support atomic multi-file commits, so use the 01290 ** simple case then too. 01291 */ 01292 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){ 01293 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 01294 Btree *pBt = db->aDb[i].pBt; 01295 if( pBt ){ 01296 rc = sqlite3BtreeCommitPhaseOne(pBt, 0); 01297 } 01298 } 01299 01300 /* Do the commit only if all databases successfully complete phase 1. 01301 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an 01302 ** IO error while deleting or truncating a journal file. It is unlikely, 01303 ** but could happen. In this case abandon processing and return the error. 01304 */ 01305 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 01306 Btree *pBt = db->aDb[i].pBt; 01307 if( pBt ){ 01308 rc = sqlite3BtreeCommitPhaseTwo(pBt); 01309 } 01310 } 01311 if( rc==SQLITE_OK ){ 01312 sqlite3VtabCommit(db); 01313 } 01314 } 01315 01316 /* The complex case - There is a multi-file write-transaction active. 01317 ** This requires a master journal file to ensure the transaction is 01318 ** committed atomicly. 01319 */ 01320 #ifndef SQLITE_OMIT_DISKIO 01321 else{ 01322 sqlite3_vfs *pVfs = db->pVfs; 01323 int needSync = 0; 01324 char *zMaster = 0; /* File-name for the master journal */ 01325 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); 01326 sqlite3_file *pMaster = 0; 01327 i64 offset = 0; 01328 int res; 01329 01330 /* Select a master journal file name */ 01331 do { 01332 u32 random; 01333 sqlite3DbFree(db, zMaster); 01334 sqlite3_randomness(sizeof(random), &random); 01335 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff); 01336 if( !zMaster ){ 01337 return SQLITE_NOMEM; 01338 } 01339 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); 01340 }while( rc==SQLITE_OK && res ); 01341 if( rc==SQLITE_OK ){ 01342 /* Open the master journal. */ 01343 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, 01344 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| 01345 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 01346 ); 01347 } 01348 if( rc!=SQLITE_OK ){ 01349 sqlite3DbFree(db, zMaster); 01350 return rc; 01351 } 01352 01353 /* Write the name of each database file in the transaction into the new 01354 ** master journal file. If an error occurs at this point close 01355 ** and delete the master journal file. All the individual journal files 01356 ** still have 'null' as the master journal pointer, so they will roll 01357 ** back independently if a failure occurs. 01358 */ 01359 for(i=0; i<db->nDb; i++){ 01360 Btree *pBt = db->aDb[i].pBt; 01361 if( i==1 ) continue; /* Ignore the TEMP database */ 01362 if( sqlite3BtreeIsInTrans(pBt) ){ 01363 char const *zFile = sqlite3BtreeGetJournalname(pBt); 01364 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */ 01365 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){ 01366 needSync = 1; 01367 } 01368 rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset); 01369 offset += strlen(zFile)+1; 01370 if( rc!=SQLITE_OK ){ 01371 sqlite3OsCloseFree(pMaster); 01372 sqlite3OsDelete(pVfs, zMaster, 0); 01373 sqlite3DbFree(db, zMaster); 01374 return rc; 01375 } 01376 } 01377 } 01378 01379 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device 01380 ** flag is set this is not required. 01381 */ 01382 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt); 01383 if( (needSync 01384 && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)) 01385 && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){ 01386 sqlite3OsCloseFree(pMaster); 01387 sqlite3OsDelete(pVfs, zMaster, 0); 01388 sqlite3DbFree(db, zMaster); 01389 return rc; 01390 } 01391 01392 /* Sync all the db files involved in the transaction. The same call 01393 ** sets the master journal pointer in each individual journal. If 01394 ** an error occurs here, do not delete the master journal file. 01395 ** 01396 ** If the error occurs during the first call to 01397 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the 01398 ** master journal file will be orphaned. But we cannot delete it, 01399 ** in case the master journal file name was written into the journal 01400 ** file before the failure occured. 01401 */ 01402 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 01403 Btree *pBt = db->aDb[i].pBt; 01404 if( pBt ){ 01405 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster); 01406 } 01407 } 01408 sqlite3OsCloseFree(pMaster); 01409 if( rc!=SQLITE_OK ){ 01410 sqlite3DbFree(db, zMaster); 01411 return rc; 01412 } 01413 01414 /* Delete the master journal file. This commits the transaction. After 01415 ** doing this the directory is synced again before any individual 01416 ** transaction files are deleted. 01417 */ 01418 rc = sqlite3OsDelete(pVfs, zMaster, 1); 01419 sqlite3DbFree(db, zMaster); 01420 zMaster = 0; 01421 if( rc ){ 01422 return rc; 01423 } 01424 01425 /* All files and directories have already been synced, so the following 01426 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and 01427 ** deleting or truncating journals. If something goes wrong while 01428 ** this is happening we don't really care. The integrity of the 01429 ** transaction is already guaranteed, but some stray 'cold' journals 01430 ** may be lying around. Returning an error code won't help matters. 01431 */ 01432 disable_simulated_io_errors(); 01433 sqlite3BeginBenignMalloc(); 01434 for(i=0; i<db->nDb; i++){ 01435 Btree *pBt = db->aDb[i].pBt; 01436 if( pBt ){ 01437 sqlite3BtreeCommitPhaseTwo(pBt); 01438 } 01439 } 01440 sqlite3EndBenignMalloc(); 01441 enable_simulated_io_errors(); 01442 01443 sqlite3VtabCommit(db); 01444 } 01445 #endif 01446 01447 return rc; 01448 } 01449 01450 /* 01451 ** This routine checks that the sqlite3.activeVdbeCnt count variable 01452 ** matches the number of vdbe's in the list sqlite3.pVdbe that are 01453 ** currently active. An assertion fails if the two counts do not match. 01454 ** This is an internal self-check only - it is not an essential processing 01455 ** step. 01456 ** 01457 ** This is a no-op if NDEBUG is defined. 01458 */ 01459 #ifndef NDEBUG 01460 static void checkActiveVdbeCnt(sqlite3 *db){ 01461 Vdbe *p; 01462 int cnt = 0; 01463 int nWrite = 0; 01464 p = db->pVdbe; 01465 while( p ){ 01466 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){ 01467 cnt++; 01468 if( p->readOnly==0 ) nWrite++; 01469 } 01470 p = p->pNext; 01471 } 01472 assert( cnt==db->activeVdbeCnt ); 01473 assert( nWrite==db->writeVdbeCnt ); 01474 } 01475 #else 01476 #define checkActiveVdbeCnt(x) 01477 #endif 01478 01479 /* 01480 ** For every Btree that in database connection db which 01481 ** has been modified, "trip" or invalidate each cursor in 01482 ** that Btree might have been modified so that the cursor 01483 ** can never be used again. This happens when a rollback 01484 *** occurs. We have to trip all the other cursors, even 01485 ** cursor from other VMs in different database connections, 01486 ** so that none of them try to use the data at which they 01487 ** were pointing and which now may have been changed due 01488 ** to the rollback. 01489 ** 01490 ** Remember that a rollback can delete tables complete and 01491 ** reorder rootpages. So it is not sufficient just to save 01492 ** the state of the cursor. We have to invalidate the cursor 01493 ** so that it is never used again. 01494 */ 01495 static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){ 01496 int i; 01497 for(i=0; i<db->nDb; i++){ 01498 Btree *p = db->aDb[i].pBt; 01499 if( p && sqlite3BtreeIsInTrans(p) ){ 01500 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT); 01501 } 01502 } 01503 } 01504 01505 /* 01506 ** This routine is called the when a VDBE tries to halt. If the VDBE 01507 ** has made changes and is in autocommit mode, then commit those 01508 ** changes. If a rollback is needed, then do the rollback. 01509 ** 01510 ** This routine is the only way to move the state of a VM from 01511 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to 01512 ** call this on a VM that is in the SQLITE_MAGIC_HALT state. 01513 ** 01514 ** Return an error code. If the commit could not complete because of 01515 ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it 01516 ** means the close did not happen and needs to be repeated. 01517 */ 01518 int sqlite3VdbeHalt(Vdbe *p){ 01519 sqlite3 *db = p->db; 01520 int i; 01521 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */ 01522 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */ 01523 01524 /* This function contains the logic that determines if a statement or 01525 ** transaction will be committed or rolled back as a result of the 01526 ** execution of this virtual machine. 01527 ** 01528 ** If any of the following errors occur: 01529 ** 01530 ** SQLITE_NOMEM 01531 ** SQLITE_IOERR 01532 ** SQLITE_FULL 01533 ** SQLITE_INTERRUPT 01534 ** 01535 ** Then the internal cache might have been left in an inconsistent 01536 ** state. We need to rollback the statement transaction, if there is 01537 ** one, or the complete transaction if there is no statement transaction. 01538 */ 01539 01540 if( p->db->mallocFailed ){ 01541 p->rc = SQLITE_NOMEM; 01542 } 01543 closeAllCursorsExceptActiveVtabs(p); 01544 if( p->magic!=VDBE_MAGIC_RUN ){ 01545 return SQLITE_OK; 01546 } 01547 checkActiveVdbeCnt(db); 01548 01549 /* No commit or rollback needed if the program never started */ 01550 if( p->pc>=0 ){ 01551 int mrc; /* Primary error code from p->rc */ 01552 01553 /* Lock all btrees used by the statement */ 01554 sqlite3BtreeMutexArrayEnter(&p->aMutex); 01555 01556 /* Check for one of the special errors */ 01557 mrc = p->rc & 0xff; 01558 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR 01559 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; 01560 if( isSpecialError ){ 01561 /* If the query was read-only, we need do no rollback at all. Otherwise, 01562 ** proceed with the special handling. 01563 */ 01564 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){ 01565 if( p->rc==SQLITE_IOERR_BLOCKED && p->usesStmtJournal ){ 01566 xFunc = sqlite3BtreeRollbackStmt; 01567 p->rc = SQLITE_BUSY; 01568 }else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) 01569 && p->usesStmtJournal ){ 01570 xFunc = sqlite3BtreeRollbackStmt; 01571 }else{ 01572 /* We are forced to roll back the active transaction. Before doing 01573 ** so, abort any other statements this handle currently has active. 01574 */ 01575 invalidateCursorsOnModifiedBtrees(db); 01576 sqlite3RollbackAll(db); 01577 db->autoCommit = 1; 01578 } 01579 } 01580 } 01581 01582 /* If the auto-commit flag is set and this is the only active vdbe, then 01583 ** we do either a commit or rollback of the current transaction. 01584 ** 01585 ** Note: This block also runs if one of the special errors handled 01586 ** above has occurred. 01587 */ 01588 if( db->autoCommit && db->writeVdbeCnt==(p->readOnly==0) ){ 01589 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ 01590 /* The auto-commit flag is true, and the vdbe program was 01591 ** successful or hit an 'OR FAIL' constraint. This means a commit 01592 ** is required. 01593 */ 01594 int rc = vdbeCommit(db, p); 01595 if( rc==SQLITE_BUSY ){ 01596 sqlite3BtreeMutexArrayLeave(&p->aMutex); 01597 return SQLITE_BUSY; 01598 }else if( rc!=SQLITE_OK ){ 01599 p->rc = rc; 01600 sqlite3RollbackAll(db); 01601 }else{ 01602 sqlite3CommitInternalChanges(db); 01603 } 01604 }else{ 01605 sqlite3RollbackAll(db); 01606 } 01607 }else if( !xFunc ){ 01608 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ 01609 if( p->openedStatement ){ 01610 xFunc = sqlite3BtreeCommitStmt; 01611 } 01612 }else if( p->errorAction==OE_Abort ){ 01613 xFunc = sqlite3BtreeRollbackStmt; 01614 }else{ 01615 invalidateCursorsOnModifiedBtrees(db); 01616 sqlite3RollbackAll(db); 01617 db->autoCommit = 1; 01618 } 01619 } 01620 01621 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or 01622 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs 01623 ** and the return code is still SQLITE_OK, set the return code to the new 01624 ** error value. 01625 */ 01626 assert(!xFunc || 01627 xFunc==sqlite3BtreeCommitStmt || 01628 xFunc==sqlite3BtreeRollbackStmt 01629 ); 01630 for(i=0; xFunc && i<db->nDb; i++){ 01631 int rc; 01632 Btree *pBt = db->aDb[i].pBt; 01633 if( pBt ){ 01634 rc = xFunc(pBt); 01635 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){ 01636 p->rc = rc; 01637 sqlite3DbFree(db, p->zErrMsg); 01638 p->zErrMsg = 0; 01639 } 01640 } 01641 } 01642 01643 /* If this was an INSERT, UPDATE or DELETE and the statement was committed, 01644 ** set the change counter. 01645 */ 01646 if( p->changeCntOn && p->pc>=0 ){ 01647 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){ 01648 sqlite3VdbeSetChanges(db, p->nChange); 01649 }else{ 01650 sqlite3VdbeSetChanges(db, 0); 01651 } 01652 p->nChange = 0; 01653 } 01654 01655 /* Rollback or commit any schema changes that occurred. */ 01656 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){ 01657 sqlite3ResetInternalSchema(db, 0); 01658 db->flags = (db->flags | SQLITE_InternChanges); 01659 } 01660 01661 /* Release the locks */ 01662 sqlite3BtreeMutexArrayLeave(&p->aMutex); 01663 } 01664 01665 /* We have successfully halted and closed the VM. Record this fact. */ 01666 if( p->pc>=0 ){ 01667 db->activeVdbeCnt--; 01668 if( !p->readOnly ){ 01669 db->writeVdbeCnt--; 01670 } 01671 assert( db->activeVdbeCnt>=db->writeVdbeCnt ); 01672 } 01673 p->magic = VDBE_MAGIC_HALT; 01674 checkActiveVdbeCnt(db); 01675 if( p->db->mallocFailed ){ 01676 p->rc = SQLITE_NOMEM; 01677 } 01678 01679 return SQLITE_OK; 01680 } 01681 01682 01683 /* 01684 ** Each VDBE holds the result of the most recent sqlite3_step() call 01685 ** in p->rc. This routine sets that result back to SQLITE_OK. 01686 */ 01687 void sqlite3VdbeResetStepResult(Vdbe *p){ 01688 p->rc = SQLITE_OK; 01689 } 01690 01691 /* 01692 ** Clean up a VDBE after execution but do not delete the VDBE just yet. 01693 ** Write any error messages into *pzErrMsg. Return the result code. 01694 ** 01695 ** After this routine is run, the VDBE should be ready to be executed 01696 ** again. 01697 ** 01698 ** To look at it another way, this routine resets the state of the 01699 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to 01700 ** VDBE_MAGIC_INIT. 01701 */ 01702 int sqlite3VdbeReset(Vdbe *p){ 01703 sqlite3 *db; 01704 db = p->db; 01705 01706 /* If the VM did not run to completion or if it encountered an 01707 ** error, then it might not have been halted properly. So halt 01708 ** it now. 01709 */ 01710 (void)sqlite3SafetyOn(db); 01711 sqlite3VdbeHalt(p); 01712 (void)sqlite3SafetyOff(db); 01713 01714 /* If the VDBE has be run even partially, then transfer the error code 01715 ** and error message from the VDBE into the main database structure. But 01716 ** if the VDBE has just been set to run but has not actually executed any 01717 ** instructions yet, leave the main database error information unchanged. 01718 */ 01719 if( p->pc>=0 ){ 01720 if( p->zErrMsg ){ 01721 sqlite3BeginBenignMalloc(); 01722 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT); 01723 sqlite3EndBenignMalloc(); 01724 db->errCode = p->rc; 01725 sqlite3DbFree(db, p->zErrMsg); 01726 p->zErrMsg = 0; 01727 }else if( p->rc ){ 01728 sqlite3Error(db, p->rc, 0); 01729 }else{ 01730 sqlite3Error(db, SQLITE_OK, 0); 01731 } 01732 }else if( p->rc && p->expired ){ 01733 /* The expired flag was set on the VDBE before the first call 01734 ** to sqlite3_step(). For consistency (since sqlite3_step() was 01735 ** called), set the database error in this case as well. 01736 */ 01737 sqlite3Error(db, p->rc, 0); 01738 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT); 01739 sqlite3DbFree(db, p->zErrMsg); 01740 p->zErrMsg = 0; 01741 } 01742 01743 /* Reclaim all memory used by the VDBE 01744 */ 01745 Cleanup(p); 01746 01747 /* Save profiling information from this VDBE run. 01748 */ 01749 #ifdef VDBE_PROFILE 01750 { 01751 FILE *out = fopen("vdbe_profile.out", "a"); 01752 if( out ){ 01753 int i; 01754 fprintf(out, "---- "); 01755 for(i=0; i<p->nOp; i++){ 01756 fprintf(out, "%02x", p->aOp[i].opcode); 01757 } 01758 fprintf(out, "\n"); 01759 for(i=0; i<p->nOp; i++){ 01760 fprintf(out, "%6d %10lld %8lld ", 01761 p->aOp[i].cnt, 01762 p->aOp[i].cycles, 01763 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 01764 ); 01765 sqlite3VdbePrintOp(out, i, &p->aOp[i]); 01766 } 01767 fclose(out); 01768 } 01769 } 01770 #endif 01771 p->magic = VDBE_MAGIC_INIT; 01772 return p->rc & db->errMask; 01773 } 01774 01775 /* 01776 ** Clean up and delete a VDBE after execution. Return an integer which is 01777 ** the result code. Write any error message text into *pzErrMsg. 01778 */ 01779 int sqlite3VdbeFinalize(Vdbe *p){ 01780 int rc = SQLITE_OK; 01781 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ 01782 rc = sqlite3VdbeReset(p); 01783 assert( (rc & p->db->errMask)==rc ); 01784 }else if( p->magic!=VDBE_MAGIC_INIT ){ 01785 return SQLITE_MISUSE; 01786 } 01787 sqlite3VdbeDelete(p); 01788 return rc; 01789 } 01790 01791 /* 01792 ** Call the destructor for each auxdata entry in pVdbeFunc for which 01793 ** the corresponding bit in mask is clear. Auxdata entries beyond 31 01794 ** are always destroyed. To destroy all auxdata entries, call this 01795 ** routine with mask==0. 01796 */ 01797 void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){ 01798 int i; 01799 for(i=0; i<pVdbeFunc->nAux; i++){ 01800 struct AuxData *pAux = &pVdbeFunc->apAux[i]; 01801 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){ 01802 if( pAux->xDelete ){ 01803 pAux->xDelete(pAux->pAux); 01804 } 01805 pAux->pAux = 0; 01806 } 01807 } 01808 } 01809 01810 /* 01811 ** Delete an entire VDBE. 01812 */ 01813 void sqlite3VdbeDelete(Vdbe *p){ 01814 int i; 01815 sqlite3 *db; 01816 01817 if( p==0 ) return; 01818 db = p->db; 01819 if( p->pPrev ){ 01820 p->pPrev->pNext = p->pNext; 01821 }else{ 01822 assert( db->pVdbe==p ); 01823 db->pVdbe = p->pNext; 01824 } 01825 if( p->pNext ){ 01826 p->pNext->pPrev = p->pPrev; 01827 } 01828 if( p->aOp ){ 01829 Op *pOp = p->aOp; 01830 for(i=0; i<p->nOp; i++, pOp++){ 01831 freeP4(db, pOp->p4type, pOp->p4.p); 01832 #ifdef SQLITE_DEBUG 01833 sqlite3DbFree(db, pOp->zComment); 01834 #endif 01835 } 01836 sqlite3DbFree(db, p->aOp); 01837 } 01838 releaseMemArray(p->aVar, p->nVar); 01839 sqlite3DbFree(db, p->aLabel); 01840 if( p->aMem ){ 01841 sqlite3DbFree(db, &p->aMem[1]); 01842 } 01843 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); 01844 sqlite3DbFree(db, p->aColName); 01845 sqlite3DbFree(db, p->zSql); 01846 p->magic = VDBE_MAGIC_DEAD; 01847 sqlite3DbFree(db, p); 01848 } 01849 01850 /* 01851 ** If a MoveTo operation is pending on the given cursor, then do that 01852 ** MoveTo now. Return an error code. If no MoveTo is pending, this 01853 ** routine does nothing and returns SQLITE_OK. 01854 */ 01855 int sqlite3VdbeCursorMoveto(VdbeCursor *p){ 01856 if( p->deferredMoveto ){ 01857 int res, rc; 01858 #ifdef SQLITE_TEST 01859 extern int sqlite3_search_count; 01860 #endif 01861 assert( p->isTable ); 01862 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res); 01863 if( rc ) return rc; 01864 p->lastRowid = keyToInt(p->movetoTarget); 01865 p->rowidIsValid = res==0; 01866 if( res<0 ){ 01867 rc = sqlite3BtreeNext(p->pCursor, &res); 01868 if( rc ) return rc; 01869 } 01870 #ifdef SQLITE_TEST 01871 sqlite3_search_count++; 01872 #endif 01873 p->deferredMoveto = 0; 01874 p->cacheStatus = CACHE_STALE; 01875 }else if( p->pCursor ){ 01876 int hasMoved; 01877 int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved); 01878 if( rc ) return rc; 01879 if( hasMoved ){ 01880 p->cacheStatus = CACHE_STALE; 01881 p->nullRow = 1; 01882 } 01883 } 01884 return SQLITE_OK; 01885 } 01886 01887 /* 01888 ** The following functions: 01889 ** 01890 ** sqlite3VdbeSerialType() 01891 ** sqlite3VdbeSerialTypeLen() 01892 ** sqlite3VdbeSerialLen() 01893 ** sqlite3VdbeSerialPut() 01894 ** sqlite3VdbeSerialGet() 01895 ** 01896 ** encapsulate the code that serializes values for storage in SQLite 01897 ** data and index records. Each serialized value consists of a 01898 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned 01899 ** integer, stored as a varint. 01900 ** 01901 ** In an SQLite index record, the serial type is stored directly before 01902 ** the blob of data that it corresponds to. In a table record, all serial 01903 ** types are stored at the start of the record, and the blobs of data at 01904 ** the end. Hence these functions allow the caller to handle the 01905 ** serial-type and data blob seperately. 01906 ** 01907 ** The following table describes the various storage classes for data: 01908 ** 01909 ** serial type bytes of data type 01910 ** -------------- --------------- --------------- 01911 ** 0 0 NULL 01912 ** 1 1 signed integer 01913 ** 2 2 signed integer 01914 ** 3 3 signed integer 01915 ** 4 4 signed integer 01916 ** 5 6 signed integer 01917 ** 6 8 signed integer 01918 ** 7 8 IEEE float 01919 ** 8 0 Integer constant 0 01920 ** 9 0 Integer constant 1 01921 ** 10,11 reserved for expansion 01922 ** N>=12 and even (N-12)/2 BLOB 01923 ** N>=13 and odd (N-13)/2 text 01924 ** 01925 ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions 01926 ** of SQLite will not understand those serial types. 01927 */ 01928 01929 /* 01930 ** Return the serial-type for the value stored in pMem. 01931 */ 01932 u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ 01933 int flags = pMem->flags; 01934 int n; 01935 01936 if( flags&MEM_Null ){ 01937 return 0; 01938 } 01939 if( flags&MEM_Int ){ 01940 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ 01941 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) 01942 i64 i = pMem->u.i; 01943 u64 u; 01944 if( file_format>=4 && (i&1)==i ){ 01945 return 8+i; 01946 } 01947 u = i<0 ? -i : i; 01948 if( u<=127 ) return 1; 01949 if( u<=32767 ) return 2; 01950 if( u<=8388607 ) return 3; 01951 if( u<=2147483647 ) return 4; 01952 if( u<=MAX_6BYTE ) return 5; 01953 return 6; 01954 } 01955 if( flags&MEM_Real ){ 01956 return 7; 01957 } 01958 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) ); 01959 n = pMem->n; 01960 if( flags & MEM_Zero ){ 01961 n += pMem->u.i; 01962 } 01963 assert( n>=0 ); 01964 return ((n*2) + 12 + ((flags&MEM_Str)!=0)); 01965 } 01966 01967 /* 01968 ** Return the length of the data corresponding to the supplied serial-type. 01969 */ 01970 int sqlite3VdbeSerialTypeLen(u32 serial_type){ 01971 if( serial_type>=12 ){ 01972 return (serial_type-12)/2; 01973 }else{ 01974 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 }; 01975 return aSize[serial_type]; 01976 } 01977 } 01978 01979 /* 01980 ** If we are on an architecture with mixed-endian floating 01981 ** points (ex: ARM7) then swap the lower 4 bytes with the 01982 ** upper 4 bytes. Return the result. 01983 ** 01984 ** For most architectures, this is a no-op. 01985 ** 01986 ** (later): It is reported to me that the mixed-endian problem 01987 ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems 01988 ** that early versions of GCC stored the two words of a 64-bit 01989 ** float in the wrong order. And that error has been propagated 01990 ** ever since. The blame is not necessarily with GCC, though. 01991 ** GCC might have just copying the problem from a prior compiler. 01992 ** I am also told that newer versions of GCC that follow a different 01993 ** ABI get the byte order right. 01994 ** 01995 ** Developers using SQLite on an ARM7 should compile and run their 01996 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG 01997 ** enabled, some asserts below will ensure that the byte order of 01998 ** floating point values is correct. 01999 ** 02000 ** (2007-08-30) Frank van Vugt has studied this problem closely 02001 ** and has send his findings to the SQLite developers. Frank 02002 ** writes that some Linux kernels offer floating point hardware 02003 ** emulation that uses only 32-bit mantissas instead of a full 02004 ** 48-bits as required by the IEEE standard. (This is the 02005 ** CONFIG_FPE_FASTFPE option.) On such systems, floating point 02006 ** byte swapping becomes very complicated. To avoid problems, 02007 ** the necessary byte swapping is carried out using a 64-bit integer 02008 ** rather than a 64-bit float. Frank assures us that the code here 02009 ** works for him. We, the developers, have no way to independently 02010 ** verify this, but Frank seems to know what he is talking about 02011 ** so we trust him. 02012 */ 02013 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT 02014 static u64 floatSwap(u64 in){ 02015 union { 02016 u64 r; 02017 u32 i[2]; 02018 } u; 02019 u32 t; 02020 02021 u.r = in; 02022 t = u.i[0]; 02023 u.i[0] = u.i[1]; 02024 u.i[1] = t; 02025 return u.r; 02026 } 02027 # define swapMixedEndianFloat(X) X = floatSwap(X) 02028 #else 02029 # define swapMixedEndianFloat(X) 02030 #endif 02031 02032 /* 02033 ** Write the serialized data blob for the value stored in pMem into 02034 ** buf. It is assumed that the caller has allocated sufficient space. 02035 ** Return the number of bytes written. 02036 ** 02037 ** nBuf is the amount of space left in buf[]. nBuf must always be 02038 ** large enough to hold the entire field. Except, if the field is 02039 ** a blob with a zero-filled tail, then buf[] might be just the right 02040 ** size to hold everything except for the zero-filled tail. If buf[] 02041 ** is only big enough to hold the non-zero prefix, then only write that 02042 ** prefix into buf[]. But if buf[] is large enough to hold both the 02043 ** prefix and the tail then write the prefix and set the tail to all 02044 ** zeros. 02045 ** 02046 ** Return the number of bytes actually written into buf[]. The number 02047 ** of bytes in the zero-filled tail is included in the return value only 02048 ** if those bytes were zeroed in buf[]. 02049 */ 02050 int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){ 02051 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format); 02052 int len; 02053 02054 /* Integer and Real */ 02055 if( serial_type<=7 && serial_type>0 ){ 02056 u64 v; 02057 int i; 02058 if( serial_type==7 ){ 02059 assert( sizeof(v)==sizeof(pMem->r) ); 02060 memcpy(&v, &pMem->r, sizeof(v)); 02061 swapMixedEndianFloat(v); 02062 }else{ 02063 v = pMem->u.i; 02064 } 02065 len = i = sqlite3VdbeSerialTypeLen(serial_type); 02066 assert( len<=nBuf ); 02067 while( i-- ){ 02068 buf[i] = (v&0xFF); 02069 v >>= 8; 02070 } 02071 return len; 02072 } 02073 02074 /* String or blob */ 02075 if( serial_type>=12 ){ 02076 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0) 02077 == sqlite3VdbeSerialTypeLen(serial_type) ); 02078 assert( pMem->n<=nBuf ); 02079 len = pMem->n; 02080 memcpy(buf, pMem->z, len); 02081 if( pMem->flags & MEM_Zero ){ 02082 len += pMem->u.i; 02083 if( len>nBuf ){ 02084 len = nBuf; 02085 } 02086 memset(&buf[pMem->n], 0, len-pMem->n); 02087 } 02088 return len; 02089 } 02090 02091 /* NULL or constants 0 or 1 */ 02092 return 0; 02093 } 02094 02095 /* 02096 ** Deserialize the data blob pointed to by buf as serial type serial_type 02097 ** and store the result in pMem. Return the number of bytes read. 02098 */ 02099 int sqlite3VdbeSerialGet( 02100 const unsigned char *buf, /* Buffer to deserialize from */ 02101 u32 serial_type, /* Serial type to deserialize */ 02102 Mem *pMem /* Memory cell to write value into */ 02103 ){ 02104 switch( serial_type ){ 02105 case 10: /* Reserved for future use */ 02106 case 11: /* Reserved for future use */ 02107 case 0: { /* NULL */ 02108 pMem->flags = MEM_Null; 02109 break; 02110 } 02111 case 1: { /* 1-byte signed integer */ 02112 pMem->u.i = (signed char)buf[0]; 02113 pMem->flags = MEM_Int; 02114 return 1; 02115 } 02116 case 2: { /* 2-byte signed integer */ 02117 pMem->u.i = (((signed char)buf[0])<<8) | buf[1]; 02118 pMem->flags = MEM_Int; 02119 return 2; 02120 } 02121 case 3: { /* 3-byte signed integer */ 02122 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2]; 02123 pMem->flags = MEM_Int; 02124 return 3; 02125 } 02126 case 4: { /* 4-byte signed integer */ 02127 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; 02128 pMem->flags = MEM_Int; 02129 return 4; 02130 } 02131 case 5: { /* 6-byte signed integer */ 02132 u64 x = (((signed char)buf[0])<<8) | buf[1]; 02133 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; 02134 x = (x<<32) | y; 02135 pMem->u.i = *(i64*)&x; 02136 pMem->flags = MEM_Int; 02137 return 6; 02138 } 02139 case 6: /* 8-byte signed integer */ 02140 case 7: { /* IEEE floating point */ 02141 u64 x; 02142 u32 y; 02143 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) 02144 /* Verify that integers and floating point values use the same 02145 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is 02146 ** defined that 64-bit floating point values really are mixed 02147 ** endian. 02148 */ 02149 static const u64 t1 = ((u64)0x3ff00000)<<32; 02150 static const double r1 = 1.0; 02151 u64 t2 = t1; 02152 swapMixedEndianFloat(t2); 02153 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); 02154 #endif 02155 02156 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; 02157 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; 02158 x = (x<<32) | y; 02159 if( serial_type==6 ){ 02160 pMem->u.i = *(i64*)&x; 02161 pMem->flags = MEM_Int; 02162 }else{ 02163 assert( sizeof(x)==8 && sizeof(pMem->r)==8 ); 02164 swapMixedEndianFloat(x); 02165 memcpy(&pMem->r, &x, sizeof(x)); 02166 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real; 02167 } 02168 return 8; 02169 } 02170 case 8: /* Integer 0 */ 02171 case 9: { /* Integer 1 */ 02172 pMem->u.i = serial_type-8; 02173 pMem->flags = MEM_Int; 02174 return 0; 02175 } 02176 default: { 02177 int len = (serial_type-12)/2; 02178 pMem->z = (char *)buf; 02179 pMem->n = len; 02180 pMem->xDel = 0; 02181 if( serial_type&0x01 ){ 02182 pMem->flags = MEM_Str | MEM_Ephem; 02183 }else{ 02184 pMem->flags = MEM_Blob | MEM_Ephem; 02185 } 02186 return len; 02187 } 02188 } 02189 return 0; 02190 } 02191 02192 02193 /* 02194 ** Given the nKey-byte encoding of a record in pKey[], parse the 02195 ** record into a UnpackedRecord structure. Return a pointer to 02196 ** that structure. 02197 ** 02198 ** The calling function might provide szSpace bytes of memory 02199 ** space at pSpace. This space can be used to hold the returned 02200 ** VDbeParsedRecord structure if it is large enough. If it is 02201 ** not big enough, space is obtained from sqlite3_malloc(). 02202 ** 02203 ** The returned structure should be closed by a call to 02204 ** sqlite3VdbeDeleteUnpackedRecord(). 02205 */ 02206 UnpackedRecord *sqlite3VdbeRecordUnpack( 02207 KeyInfo *pKeyInfo, /* Information about the record format */ 02208 int nKey, /* Size of the binary record */ 02209 const void *pKey, /* The binary record */ 02210 UnpackedRecord *pSpace,/* Space available to hold resulting object */ 02211 int szSpace /* Size of pSpace[] in bytes */ 02212 ){ 02213 const unsigned char *aKey = (const unsigned char *)pKey; 02214 UnpackedRecord *p; 02215 int nByte; 02216 int idx, d; 02217 u16 u; /* Unsigned loop counter */ 02218 u32 szHdr; 02219 Mem *pMem; 02220 02221 assert( sizeof(Mem)>sizeof(*p) ); 02222 nByte = sizeof(Mem)*(pKeyInfo->nField+2); 02223 if( nByte>szSpace ){ 02224 p = sqlite3DbMallocRaw(pKeyInfo->db, nByte); 02225 if( p==0 ) return 0; 02226 p->flags = UNPACKED_NEED_FREE | UNPACKED_NEED_DESTROY; 02227 }else{ 02228 p = pSpace; 02229 p->flags = UNPACKED_NEED_DESTROY; 02230 } 02231 p->pKeyInfo = pKeyInfo; 02232 p->nField = pKeyInfo->nField + 1; 02233 p->aMem = pMem = &((Mem*)p)[1]; 02234 idx = getVarint32(aKey, szHdr); 02235 d = szHdr; 02236 u = 0; 02237 while( idx<szHdr && u<p->nField ){ 02238 u32 serial_type; 02239 02240 idx += getVarint32( aKey+idx, serial_type); 02241 if( d>=nKey && sqlite3VdbeSerialTypeLen(serial_type)>0 ) break; 02242 pMem->enc = pKeyInfo->enc; 02243 pMem->db = pKeyInfo->db; 02244 pMem->flags = 0; 02245 pMem->zMalloc = 0; 02246 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); 02247 pMem++; 02248 u++; 02249 } 02250 assert( u<=pKeyInfo->nField + 1 ); 02251 p->nField = u; 02252 return (void*)p; 02253 } 02254 02255 /* 02256 ** This routine destroys a UnpackedRecord object 02257 */ 02258 void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){ 02259 if( p ){ 02260 if( p->flags & UNPACKED_NEED_DESTROY ){ 02261 int i; 02262 Mem *pMem; 02263 for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){ 02264 if( pMem->zMalloc ){ 02265 sqlite3VdbeMemRelease(pMem); 02266 } 02267 } 02268 } 02269 if( p->flags & UNPACKED_NEED_FREE ){ 02270 sqlite3DbFree(p->pKeyInfo->db, p); 02271 } 02272 } 02273 } 02274 02275 /* 02276 ** This function compares the two table rows or index records 02277 ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero 02278 ** or positive integer if key1 is less than, equal to or 02279 ** greater than key2. The {nKey1, pKey1} key must be a blob 02280 ** created by th OP_MakeRecord opcode of the VDBE. The pPKey2 02281 ** key must be a parsed key such as obtained from 02282 ** sqlite3VdbeParseRecord. 02283 ** 02284 ** Key1 and Key2 do not have to contain the same number of fields. 02285 ** The key with fewer fields is usually compares less than the 02286 ** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set 02287 ** and the common prefixes are equal, then key1 is less than key2. 02288 ** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are 02289 ** equal, then the keys are considered to be equal and 02290 ** the parts beyond the common prefix are ignored. 02291 ** 02292 ** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of 02293 ** the header of pKey1 is ignored. It is assumed that pKey1 is 02294 ** an index key, and thus ends with a rowid value. The last byte 02295 ** of the header will therefore be the serial type of the rowid: 02296 ** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types. 02297 ** The serial type of the final rowid will always be a single byte. 02298 ** By ignoring this last byte of the header, we force the comparison 02299 ** to ignore the rowid at the end of key1. 02300 */ 02301 int sqlite3VdbeRecordCompare( 02302 int nKey1, const void *pKey1, /* Left key */ 02303 UnpackedRecord *pPKey2 /* Right key */ 02304 ){ 02305 u32 d1; /* Offset into aKey[] of next data element */ 02306 u32 idx1; /* Offset into aKey[] of next header element */ 02307 u32 szHdr1; /* Number of bytes in header */ 02308 int i = 0; 02309 int nField; 02310 int rc = 0; 02311 const unsigned char *aKey1 = (const unsigned char *)pKey1; 02312 KeyInfo *pKeyInfo; 02313 Mem mem1; 02314 02315 pKeyInfo = pPKey2->pKeyInfo; 02316 mem1.enc = pKeyInfo->enc; 02317 mem1.db = pKeyInfo->db; 02318 mem1.flags = 0; 02319 mem1.zMalloc = 0; 02320 02321 idx1 = getVarint32(aKey1, szHdr1); 02322 d1 = szHdr1; 02323 if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){ 02324 szHdr1--; 02325 } 02326 nField = pKeyInfo->nField; 02327 while( idx1<szHdr1 && i<pPKey2->nField ){ 02328 u32 serial_type1; 02329 02330 /* Read the serial types for the next element in each key. */ 02331 idx1 += getVarint32( aKey1+idx1, serial_type1 ); 02332 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break; 02333 02334 /* Extract the values to be compared. 02335 */ 02336 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); 02337 02338 /* Do the comparison 02339 */ 02340 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], 02341 i<nField ? pKeyInfo->aColl[i] : 0); 02342 if( rc!=0 ){ 02343 break; 02344 } 02345 i++; 02346 } 02347 if( mem1.zMalloc ) sqlite3VdbeMemRelease(&mem1); 02348 02349 if( rc==0 ){ 02350 /* rc==0 here means that one of the keys ran out of fields and 02351 ** all the fields up to that point were equal. If the UNPACKED_INCRKEY 02352 ** flag is set, then break the tie by treating key2 as larger. 02353 ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes 02354 ** are considered to be equal. Otherwise, the longer key is the 02355 ** larger. As it happens, the pPKey2 will always be the longer 02356 ** if there is a difference. 02357 */ 02358 if( pPKey2->flags & UNPACKED_INCRKEY ){ 02359 rc = -1; 02360 }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){ 02361 /* Leave rc==0 */ 02362 }else if( idx1<szHdr1 ){ 02363 rc = 1; 02364 } 02365 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField 02366 && pKeyInfo->aSortOrder[i] ){ 02367 rc = -rc; 02368 } 02369 02370 return rc; 02371 } 02372 02373 02374 /* 02375 ** pCur points at an index entry created using the OP_MakeRecord opcode. 02376 ** Read the rowid (the last field in the record) and store it in *rowid. 02377 ** Return SQLITE_OK if everything works, or an error code otherwise. 02378 */ 02379 int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){ 02380 i64 nCellKey = 0; 02381 int rc; 02382 u32 szHdr; /* Size of the header */ 02383 u32 typeRowid; /* Serial type of the rowid */ 02384 u32 lenRowid; /* Size of the rowid */ 02385 Mem m, v; 02386 02387 sqlite3BtreeKeySize(pCur, &nCellKey); 02388 if( nCellKey<=0 ){ 02389 return SQLITE_CORRUPT_BKPT; 02390 } 02391 m.flags = 0; 02392 m.db = 0; 02393 m.zMalloc = 0; 02394 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m); 02395 if( rc ){ 02396 return rc; 02397 } 02398 (void)getVarint32((u8*)m.z, szHdr); 02399 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid); 02400 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid); 02401 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); 02402 *rowid = v.u.i; 02403 sqlite3VdbeMemRelease(&m); 02404 return SQLITE_OK; 02405 } 02406 02407 /* 02408 ** Compare the key of the index entry that cursor pC is point to against 02409 ** the key string in pKey (of length nKey). Write into *pRes a number 02410 ** that is negative, zero, or positive if pC is less than, equal to, 02411 ** or greater than pKey. Return SQLITE_OK on success. 02412 ** 02413 ** pKey is either created without a rowid or is truncated so that it 02414 ** omits the rowid at the end. The rowid at the end of the index entry 02415 ** is ignored as well. Hence, this routine only compares the prefixes 02416 ** of the keys prior to the final rowid, not the entire key. 02417 ** 02418 ** pUnpacked may be an unpacked version of pKey,nKey. If pUnpacked is 02419 ** supplied it is used in place of pKey,nKey. 02420 */ 02421 int sqlite3VdbeIdxKeyCompare( 02422 VdbeCursor *pC, /* The cursor to compare against */ 02423 UnpackedRecord *pUnpacked, /* Unpacked version of pKey and nKey */ 02424 int *res /* Write the comparison result here */ 02425 ){ 02426 i64 nCellKey = 0; 02427 int rc; 02428 BtCursor *pCur = pC->pCursor; 02429 Mem m; 02430 02431 sqlite3BtreeKeySize(pCur, &nCellKey); 02432 if( nCellKey<=0 ){ 02433 *res = 0; 02434 return SQLITE_OK; 02435 } 02436 m.db = 0; 02437 m.flags = 0; 02438 m.zMalloc = 0; 02439 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m); 02440 if( rc ){ 02441 return rc; 02442 } 02443 assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID ); 02444 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); 02445 sqlite3VdbeMemRelease(&m); 02446 return SQLITE_OK; 02447 } 02448 02449 /* 02450 ** This routine sets the value to be returned by subsequent calls to 02451 ** sqlite3_changes() on the database handle 'db'. 02452 */ 02453 void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){ 02454 assert( sqlite3_mutex_held(db->mutex) ); 02455 db->nChange = nChange; 02456 db->nTotalChange += nChange; 02457 } 02458 02459 /* 02460 ** Set a flag in the vdbe to update the change counter when it is finalised 02461 ** or reset. 02462 */ 02463 void sqlite3VdbeCountChanges(Vdbe *v){ 02464 v->changeCntOn = 1; 02465 } 02466 02467 /* 02468 ** Mark every prepared statement associated with a database connection 02469 ** as expired. 02470 ** 02471 ** An expired statement means that recompilation of the statement is 02472 ** recommend. Statements expire when things happen that make their 02473 ** programs obsolete. Removing user-defined functions or collating 02474 ** sequences, or changing an authorization function are the types of 02475 ** things that make prepared statements obsolete. 02476 */ 02477 void sqlite3ExpirePreparedStatements(sqlite3 *db){ 02478 Vdbe *p; 02479 for(p = db->pVdbe; p; p=p->pNext){ 02480 p->expired = 1; 02481 } 02482 } 02483 02484 /* 02485 ** Return the database associated with the Vdbe. 02486 */ 02487 sqlite3 *sqlite3VdbeDb(Vdbe *v){ 02488 return v->db; 02489 }
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:57 2011 by Doxygen 1.6.1