ds@1952: /* ds@1952: ** 2003 September 6 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** This file contains code used for creating, destroying, and populating ds@1952: ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior ds@1952: ** to version 2.8.7, all this code was combined into the vdbe.c source file. ds@1952: ** But that file was getting too big so this subroutines were split out. ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: #include ds@1952: #include "vdbeInt.h" ds@1952: ds@1952: ds@1952: ds@1952: /* ds@1952: ** When debugging the code generator in a symbolic debugger, one can ds@1952: ** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed ds@1952: ** as they are added to the instruction stream. ds@1952: */ ds@1952: #ifdef SQLITE_DEBUG ds@1952: int sqlite3_vdbe_addop_trace = 0; ds@1952: #endif ds@1952: ds@1952: ds@1952: /* ds@1952: ** Create a new virtual database engine. ds@1952: */ ds@1952: Vdbe *sqlite3VdbeCreate(sqlite3 *db){ ds@1952: Vdbe *p; ds@1952: p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); ds@1952: if( p==0 ) return 0; ds@1952: p->db = db; ds@1952: if( db->pVdbe ){ ds@1952: db->pVdbe->pPrev = p; ds@1952: } ds@1952: p->pNext = db->pVdbe; ds@1952: p->pPrev = 0; ds@1952: db->pVdbe = p; ds@1952: p->magic = VDBE_MAGIC_INIT; ds@1952: return p; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Remember the SQL string for a prepared statement. ds@1952: */ ds@1952: void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){ ds@1952: if( p==0 ) return; ds@1952: assert( p->zSql==0 ); ds@1952: p->zSql = sqlite3DbStrNDup(p->db, z, n); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the SQL associated with a prepared statement ds@1952: */ ds@1952: const char *sqlite3VdbeGetSql(Vdbe *p){ ds@1952: return p->zSql; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Swap all content between two VDBE structures. ds@1952: */ ds@1952: void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ ds@1952: Vdbe tmp, *pTmp; ds@1952: char *zTmp; ds@1952: int nTmp; ds@1952: tmp = *pA; ds@1952: *pA = *pB; ds@1952: *pB = tmp; ds@1952: pTmp = pA->pNext; ds@1952: pA->pNext = pB->pNext; ds@1952: pB->pNext = pTmp; ds@1952: pTmp = pA->pPrev; ds@1952: pA->pPrev = pB->pPrev; ds@1952: pB->pPrev = pTmp; ds@1952: zTmp = pA->zSql; ds@1952: pA->zSql = pB->zSql; ds@1952: pB->zSql = zTmp; ds@1952: nTmp = pA->nSql; ds@1952: pA->nSql = pB->nSql; ds@1952: pB->nSql = nTmp; ds@1952: } ds@1952: ds@1952: #ifdef SQLITE_DEBUG ds@1952: /* ds@1952: ** Turn tracing on or off ds@1952: */ ds@1952: void sqlite3VdbeTrace(Vdbe *p, FILE *trace){ ds@1952: p->trace = trace; ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Resize the Vdbe.aOp array so that it contains at least N ds@1952: ** elements. If the Vdbe is in VDBE_MAGIC_RUN state, then ds@1952: ** the Vdbe.aOp array will be sized to contain exactly N ds@1952: ** elements. Vdbe.nOpAlloc is set to reflect the new size of ds@1952: ** the array. ds@1952: ** ds@1952: ** If an out-of-memory error occurs while resizing the array, ds@1952: ** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that ds@1952: ** any opcodes already allocated can be correctly deallocated ds@1952: ** along with the rest of the Vdbe). ds@1952: */ ds@1952: static void resizeOpArray(Vdbe *p, int N){ ds@1952: int runMode = p->magic==VDBE_MAGIC_RUN; ds@1952: if( runMode || p->nOpAllocnOpAlloc; ds@1952: pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op)); ds@1952: if( pNew ){ ds@1952: p->nOpAlloc = nNew; ds@1952: p->aOp = pNew; ds@1952: if( nNew>oldSize ){ ds@1952: memset(&p->aOp[oldSize], 0, (nNew-oldSize)*sizeof(Op)); ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Add a new instruction to the list of instructions current in the ds@1952: ** VDBE. Return the address of the new instruction. ds@1952: ** ds@1952: ** Parameters: ds@1952: ** ds@1952: ** p Pointer to the VDBE ds@1952: ** ds@1952: ** op The opcode for this instruction ds@1952: ** ds@1952: ** p1, p2 First two of the three possible operands. ds@1952: ** ds@1952: ** Use the sqlite3VdbeResolveLabel() function to fix an address and ds@1952: ** the sqlite3VdbeChangeP3() function to change the value of the P3 ds@1952: ** operand. ds@1952: */ ds@1952: int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){ ds@1952: int i; ds@1952: VdbeOp *pOp; ds@1952: ds@1952: i = p->nOp; ds@1952: assert( p->magic==VDBE_MAGIC_INIT ); ds@1952: if( p->nOpAlloc<=i ){ ds@1952: resizeOpArray(p, i+1); ds@1952: if( p->db->mallocFailed ){ ds@1952: return 0; ds@1952: } ds@1952: } ds@1952: p->nOp++; ds@1952: pOp = &p->aOp[i]; ds@1952: pOp->opcode = op; ds@1952: pOp->p1 = p1; ds@1952: pOp->p2 = p2; ds@1952: pOp->p3 = 0; ds@1952: pOp->p3type = P3_NOTUSED; ds@1952: p->expired = 0; ds@1952: #ifdef SQLITE_DEBUG ds@1952: if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]); ds@1952: #endif ds@1952: return i; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Add an opcode that includes the p3 value. ds@1952: */ ds@1952: int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){ ds@1952: int addr = sqlite3VdbeAddOp(p, op, p1, p2); ds@1952: sqlite3VdbeChangeP3(p, addr, zP3, p3type); ds@1952: return addr; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Create a new symbolic label for an instruction that has yet to be ds@1952: ** coded. The symbolic label is really just a negative number. The ds@1952: ** label can be used as the P2 value of an operation. Later, when ds@1952: ** the label is resolved to a specific address, the VDBE will scan ds@1952: ** through its operation list and change all values of P2 which match ds@1952: ** the label into the resolved address. ds@1952: ** ds@1952: ** The VDBE knows that a P2 value is a label because labels are ds@1952: ** always negative and P2 values are suppose to be non-negative. ds@1952: ** Hence, a negative P2 value is a label that has yet to be resolved. ds@1952: ** ds@1952: ** Zero is returned if a malloc() fails. ds@1952: */ ds@1952: int sqlite3VdbeMakeLabel(Vdbe *p){ ds@1952: int i; ds@1952: i = p->nLabel++; ds@1952: assert( p->magic==VDBE_MAGIC_INIT ); ds@1952: if( i>=p->nLabelAlloc ){ ds@1952: p->nLabelAlloc = p->nLabelAlloc*2 + 10; ds@1952: p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, ds@1952: p->nLabelAlloc*sizeof(p->aLabel[0])); ds@1952: } ds@1952: if( p->aLabel ){ ds@1952: p->aLabel[i] = -1; ds@1952: } ds@1952: return -1-i; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Resolve label "x" to be the address of the next instruction to ds@1952: ** be inserted. The parameter "x" must have been obtained from ds@1952: ** a prior call to sqlite3VdbeMakeLabel(). ds@1952: */ ds@1952: void sqlite3VdbeResolveLabel(Vdbe *p, int x){ ds@1952: int j = -1-x; ds@1952: assert( p->magic==VDBE_MAGIC_INIT ); ds@1952: assert( j>=0 && jnLabel ); ds@1952: if( p->aLabel ){ ds@1952: p->aLabel[j] = p->nOp; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return non-zero if opcode 'op' is guarenteed not to push more values ds@1952: ** onto the VDBE stack than it pops off. ds@1952: */ ds@1952: static int opcodeNoPush(u8 op){ ds@1952: /* The 10 NOPUSH_MASK_n constants are defined in the automatically ds@1952: ** generated header file opcodes.h. Each is a 16-bit bitmask, one ds@1952: ** bit corresponding to each opcode implemented by the virtual ds@1952: ** machine in vdbe.c. The bit is true if the word "no-push" appears ds@1952: ** in a comment on the same line as the "case OP_XXX:" in ds@1952: ** sqlite3VdbeExec() in vdbe.c. ds@1952: ** ds@1952: ** If the bit is true, then the corresponding opcode is guarenteed not ds@1952: ** to grow the stack when it is executed. Otherwise, it may grow the ds@1952: ** stack by at most one entry. ds@1952: ** ds@1952: ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains ds@1952: ** one bit for opcodes 16 to 31, and so on. ds@1952: ** ds@1952: ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h ds@1952: ** because the file is generated by an awk program. Awk manipulates ds@1952: ** all numbers as floating-point and we don't want to risk a rounding ds@1952: ** error if someone builds with an awk that uses (for example) 32-bit ds@1952: ** IEEE floats. ds@1952: */ ds@1952: static const u32 masks[5] = { ds@1952: NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16), ds@1952: NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16), ds@1952: NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16), ds@1952: NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16), ds@1952: NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16) ds@1952: }; ds@1952: assert( op<32*5 ); ds@1952: return (masks[op>>5] & (1<<(op&0x1F))); ds@1952: } ds@1952: ds@1952: #ifndef NDEBUG ds@1952: int sqlite3VdbeOpcodeNoPush(u8 op){ ds@1952: return opcodeNoPush(op); ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Loop through the program looking for P2 values that are negative. ds@1952: ** Each such value is a label. Resolve the label by setting the P2 ds@1952: ** value to its correct non-zero value. ds@1952: ** ds@1952: ** This routine is called once after all opcodes have been inserted. ds@1952: ** ds@1952: ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument ds@1952: ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by ds@1952: ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array. ds@1952: ** ds@1952: ** The integer *pMaxStack is set to the maximum number of vdbe stack ds@1952: ** entries that static analysis reveals this program might need. ds@1952: ** ds@1952: ** This routine also does the following optimization: It scans for ds@1952: ** Halt instructions where P1==SQLITE_CONSTRAINT or P2==OE_Abort or for ds@1952: ** IdxInsert instructions where P2!=0. If no such instruction is ds@1952: ** found, then every Statement instruction is changed to a Noop. In ds@1952: ** this way, we avoid creating the statement journal file unnecessarily. ds@1952: */ ds@1952: static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){ ds@1952: int i; ds@1952: int nMaxArgs = 0; ds@1952: int nMaxStack = p->nOp; ds@1952: Op *pOp; ds@1952: int *aLabel = p->aLabel; ds@1952: int doesStatementRollback = 0; ds@1952: int hasStatementBegin = 0; ds@1952: for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ ds@1952: u8 opcode = pOp->opcode; ds@1952: ds@1952: if( opcode==OP_Function || opcode==OP_AggStep ds@1952: #ifndef SQLITE_OMIT_VIRTUALTABLE ds@1952: || opcode==OP_VUpdate ds@1952: #endif ds@1952: ){ ds@1952: if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; ds@1952: } ds@1952: if( opcode==OP_Halt ){ ds@1952: if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){ ds@1952: doesStatementRollback = 1; ds@1952: } ds@1952: }else if( opcode==OP_Statement ){ ds@1952: hasStatementBegin = 1; ds@1952: #ifndef SQLITE_OMIT_VIRTUALTABLE ds@1952: }else if( opcode==OP_VUpdate || opcode==OP_VRename ){ ds@1952: doesStatementRollback = 1; ds@1952: }else if( opcode==OP_VFilter ){ ds@1952: int n; ds@1952: assert( p->nOp - i >= 3 ); ds@1952: assert( pOp[-2].opcode==OP_Integer ); ds@1952: n = pOp[-2].p1; ds@1952: if( n>nMaxArgs ) nMaxArgs = n; ds@1952: #endif ds@1952: } ds@1952: if( opcodeNoPush(opcode) ){ ds@1952: nMaxStack--; ds@1952: } ds@1952: ds@1952: if( pOp->p2>=0 ) continue; ds@1952: assert( -1-pOp->p2nLabel ); ds@1952: pOp->p2 = aLabel[-1-pOp->p2]; ds@1952: } ds@1952: sqlite3_free(p->aLabel); ds@1952: p->aLabel = 0; ds@1952: ds@1952: *pMaxFuncArgs = nMaxArgs; ds@1952: *pMaxStack = nMaxStack; ds@1952: ds@1952: /* If we never rollback a statement transaction, then statement ds@1952: ** transactions are not needed. So change every OP_Statement ds@1952: ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive() ds@1952: ** which can be expensive on some platforms. ds@1952: */ ds@1952: if( hasStatementBegin && !doesStatementRollback ){ ds@1952: for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ ds@1952: if( pOp->opcode==OP_Statement ){ ds@1952: pOp->opcode = OP_Noop; ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the address of the next instruction to be inserted. ds@1952: */ ds@1952: int sqlite3VdbeCurrentAddr(Vdbe *p){ ds@1952: assert( p->magic==VDBE_MAGIC_INIT ); ds@1952: return p->nOp; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Add a whole list of operations to the operation stack. Return the ds@1952: ** address of the first operation added. ds@1952: */ ds@1952: int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ ds@1952: int addr; ds@1952: assert( p->magic==VDBE_MAGIC_INIT ); ds@1952: resizeOpArray(p, p->nOp + nOp); ds@1952: if( p->db->mallocFailed ){ ds@1952: return 0; ds@1952: } ds@1952: addr = p->nOp; ds@1952: if( nOp>0 ){ ds@1952: int i; ds@1952: VdbeOpList const *pIn = aOp; ds@1952: for(i=0; ip2; ds@1952: VdbeOp *pOut = &p->aOp[i+addr]; ds@1952: pOut->opcode = pIn->opcode; ds@1952: pOut->p1 = pIn->p1; ds@1952: pOut->p2 = p2<0 ? addr + ADDR(p2) : p2; ds@1952: pOut->p3 = pIn->p3; ds@1952: pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED; ds@1952: #ifdef SQLITE_DEBUG ds@1952: if( sqlite3_vdbe_addop_trace ){ ds@1952: sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); ds@1952: } ds@1952: #endif ds@1952: } ds@1952: p->nOp += nOp; ds@1952: } ds@1952: return addr; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Change the value of the P1 operand for a specific instruction. ds@1952: ** This routine is useful when a large program is loaded from a ds@1952: ** static array using sqlite3VdbeAddOpList but we want to make a ds@1952: ** few minor changes to the program. ds@1952: */ ds@1952: void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ ds@1952: assert( p==0 || p->magic==VDBE_MAGIC_INIT ); ds@1952: if( p && addr>=0 && p->nOp>addr && p->aOp ){ ds@1952: p->aOp[addr].p1 = val; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Change the value of the P2 operand for a specific instruction. ds@1952: ** This routine is useful for setting a jump destination. ds@1952: */ ds@1952: void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ ds@1952: assert( val>=0 ); ds@1952: assert( p==0 || p->magic==VDBE_MAGIC_INIT ); ds@1952: if( p && addr>=0 && p->nOp>addr && p->aOp ){ ds@1952: p->aOp[addr].p2 = val; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Change the P2 operand of instruction addr so that it points to ds@1952: ** the address of the next instruction to be coded. ds@1952: */ ds@1952: void sqlite3VdbeJumpHere(Vdbe *p, int addr){ ds@1952: sqlite3VdbeChangeP2(p, addr, p->nOp); ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** If the input FuncDef structure is ephemeral, then free it. If ds@1952: ** the FuncDef is not ephermal, then do nothing. ds@1952: */ ds@1952: static void freeEphemeralFunction(FuncDef *pDef){ ds@1952: if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){ ds@1952: sqlite3_free(pDef); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Delete a P3 value if necessary. ds@1952: */ ds@1952: static void freeP3(int p3type, void *p3){ ds@1952: if( p3 ){ ds@1952: switch( p3type ){ ds@1952: case P3_DYNAMIC: ds@1952: case P3_KEYINFO: ds@1952: case P3_KEYINFO_HANDOFF: { ds@1952: sqlite3_free(p3); ds@1952: break; ds@1952: } ds@1952: case P3_MPRINTF: { ds@1952: sqlite3_free(p3); ds@1952: break; ds@1952: } ds@1952: case P3_VDBEFUNC: { ds@1952: VdbeFunc *pVdbeFunc = (VdbeFunc *)p3; ds@1952: freeEphemeralFunction(pVdbeFunc->pFunc); ds@1952: sqlite3VdbeDeleteAuxData(pVdbeFunc, 0); ds@1952: sqlite3_free(pVdbeFunc); ds@1952: break; ds@1952: } ds@1952: case P3_FUNCDEF: { ds@1952: freeEphemeralFunction((FuncDef*)p3); ds@1952: break; ds@1952: } ds@1952: case P3_MEM: { ds@1952: sqlite3ValueFree((sqlite3_value*)p3); ds@1952: break; ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Change N opcodes starting at addr to No-ops. ds@1952: */ ds@1952: void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ ds@1952: if( p && p->aOp ){ ds@1952: VdbeOp *pOp = &p->aOp[addr]; ds@1952: while( N-- ){ ds@1952: freeP3(pOp->p3type, pOp->p3); ds@1952: memset(pOp, 0, sizeof(pOp[0])); ds@1952: pOp->opcode = OP_Noop; ds@1952: pOp++; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Change the value of the P3 operand for a specific instruction. ds@1952: ** This routine is useful when a large program is loaded from a ds@1952: ** static array using sqlite3VdbeAddOpList but we want to make a ds@1952: ** few minor changes to the program. ds@1952: ** ds@1952: ** If n>=0 then the P3 operand is dynamic, meaning that a copy of ds@1952: ** the string is made into memory obtained from sqlite3_malloc(). ds@1952: ** A value of n==0 means copy bytes of zP3 up to and including the ds@1952: ** first null byte. If n>0 then copy n+1 bytes of zP3. ds@1952: ** ds@1952: ** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure. ds@1952: ** A copy is made of the KeyInfo structure into memory obtained from ds@1952: ** sqlite3_malloc, to be freed when the Vdbe is finalized. ds@1952: ** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure ds@1952: ** stored in memory that the caller has obtained from sqlite3_malloc. The ds@1952: ** caller should not free the allocation, it will be freed when the Vdbe is ds@1952: ** finalized. ds@1952: ** ds@1952: ** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points ds@1952: ** to a string or structure that is guaranteed to exist for the lifetime of ds@1952: ** the Vdbe. In these cases we can just copy the pointer. ds@1952: ** ds@1952: ** If addr<0 then change P3 on the most recently inserted instruction. ds@1952: */ ds@1952: void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){ ds@1952: Op *pOp; ds@1952: assert( p==0 || p->magic==VDBE_MAGIC_INIT ); ds@1952: if( p==0 || p->aOp==0 || p->db->mallocFailed ){ ds@1952: if (n != P3_KEYINFO) { ds@1952: freeP3(n, (void*)*(char**)&zP3); ds@1952: } ds@1952: return; ds@1952: } ds@1952: if( addr<0 || addr>=p->nOp ){ ds@1952: addr = p->nOp - 1; ds@1952: if( addr<0 ) return; ds@1952: } ds@1952: pOp = &p->aOp[addr]; ds@1952: freeP3(pOp->p3type, pOp->p3); ds@1952: pOp->p3 = 0; ds@1952: if( zP3==0 ){ ds@1952: pOp->p3 = 0; ds@1952: pOp->p3type = P3_NOTUSED; ds@1952: }else if( n==P3_KEYINFO ){ ds@1952: KeyInfo *pKeyInfo; ds@1952: int nField, nByte; ds@1952: ds@1952: nField = ((KeyInfo*)zP3)->nField; ds@1952: nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; ds@1952: pKeyInfo = sqlite3_malloc( nByte ); ds@1952: pOp->p3 = (char*)pKeyInfo; ds@1952: if( pKeyInfo ){ ds@1952: unsigned char *aSortOrder; ds@1952: memcpy(pKeyInfo, zP3, nByte); ds@1952: aSortOrder = pKeyInfo->aSortOrder; ds@1952: if( aSortOrder ){ ds@1952: pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField]; ds@1952: memcpy(pKeyInfo->aSortOrder, aSortOrder, nField); ds@1952: } ds@1952: pOp->p3type = P3_KEYINFO; ds@1952: }else{ ds@1952: p->db->mallocFailed = 1; ds@1952: pOp->p3type = P3_NOTUSED; ds@1952: } ds@1952: }else if( n==P3_KEYINFO_HANDOFF ){ ds@1952: pOp->p3 = (char*)zP3; ds@1952: pOp->p3type = P3_KEYINFO; ds@1952: }else if( n<0 ){ ds@1952: pOp->p3 = (char*)zP3; ds@1952: pOp->p3type = n; ds@1952: }else{ ds@1952: if( n==0 ) n = strlen(zP3); ds@1952: pOp->p3 = sqlite3DbStrNDup(p->db, zP3, n); ds@1952: pOp->p3type = P3_DYNAMIC; ds@1952: } ds@1952: } ds@1952: ds@1952: #ifndef NDEBUG ds@1952: /* ds@1952: ** Replace the P3 field of the most recently coded instruction with ds@1952: ** comment text. ds@1952: */ ds@1952: void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ ds@1952: va_list ap; ds@1952: assert( p->nOp>0 || p->aOp==0 ); ds@1952: assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || p->db->mallocFailed ); ds@1952: va_start(ap, zFormat); ds@1952: sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(p->db, zFormat, ap), P3_DYNAMIC); ds@1952: va_end(ap); ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Return the opcode for a given address. ds@1952: */ ds@1952: VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ ds@1952: assert( p->magic==VDBE_MAGIC_INIT ); ds@1952: assert( (addr>=0 && addrnOp) || p->db->mallocFailed ); ds@1952: return ((addr>=0 && addrnOp)?(&p->aOp[addr]):0); ds@1952: } ds@1952: ds@1952: #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ ds@1952: || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) ds@1952: /* ds@1952: ** Compute a string that describes the P3 parameter for an opcode. ds@1952: ** Use zTemp for any required temporary buffer space. ds@1952: */ ds@1952: static char *displayP3(Op *pOp, char *zTemp, int nTemp){ ds@1952: char *zP3; ds@1952: assert( nTemp>=20 ); ds@1952: switch( pOp->p3type ){ ds@1952: case P3_KEYINFO: { ds@1952: int i, j; ds@1952: KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3; ds@1952: sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField); ds@1952: i = strlen(zTemp); ds@1952: for(j=0; jnField; j++){ ds@1952: CollSeq *pColl = pKeyInfo->aColl[j]; ds@1952: if( pColl ){ ds@1952: int n = strlen(pColl->zName); ds@1952: if( i+n>nTemp-6 ){ ds@1952: memcpy(&zTemp[i],",...",4); ds@1952: break; ds@1952: } ds@1952: zTemp[i++] = ','; ds@1952: if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){ ds@1952: zTemp[i++] = '-'; ds@1952: } ds@1952: memcpy(&zTemp[i], pColl->zName,n+1); ds@1952: i += n; ds@1952: }else if( i+4p3; ds@1952: sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName); ds@1952: zP3 = zTemp; ds@1952: break; ds@1952: } ds@1952: case P3_FUNCDEF: { ds@1952: FuncDef *pDef = (FuncDef*)pOp->p3; ds@1952: sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); ds@1952: zP3 = zTemp; ds@1952: break; ds@1952: } ds@1952: #ifndef SQLITE_OMIT_VIRTUALTABLE ds@1952: case P3_VTAB: { ds@1952: sqlite3_vtab *pVtab = (sqlite3_vtab*)pOp->p3; ds@1952: sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); ds@1952: zP3 = zTemp; ds@1952: break; ds@1952: } ds@1952: #endif ds@1952: default: { ds@1952: zP3 = pOp->p3; ds@1952: if( zP3==0 || pOp->opcode==OP_Noop ){ ds@1952: zP3 = ""; ds@1952: } ds@1952: } ds@1952: } ds@1952: assert( zP3!=0 ); ds@1952: return zP3; ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. ds@1952: ** ds@1952: */ ds@1952: void sqlite3VdbeUsesBtree(Vdbe *p, int i){ ds@1952: int mask; ds@1952: assert( i>=0 && idb->nDb ); ds@1952: assert( ibtreeMask)*8 ); ds@1952: mask = 1<btreeMask & mask)==0 ){ ds@1952: p->btreeMask |= mask; ds@1952: sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt); ds@1952: } ds@1952: } ds@1952: ds@1952: ds@1952: #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) ds@1952: /* ds@1952: ** Print a single opcode. This routine is used for debugging only. ds@1952: */ ds@1952: void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ ds@1952: char *zP3; ds@1952: char zPtr[50]; ds@1952: static const char *zFormat1 = "%4d %-13s %4d %4d %s\n"; ds@1952: if( pOut==0 ) pOut = stdout; ds@1952: zP3 = displayP3(pOp, zPtr, sizeof(zPtr)); ds@1952: fprintf(pOut, zFormat1, ds@1952: pc, sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, zP3); ds@1952: fflush(pOut); ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Release an array of N Mem elements ds@1952: */ ds@1952: static void releaseMemArray(Mem *p, int N){ ds@1952: if( p ){ ds@1952: while( N-->0 ){ ds@1952: assert( N<2 || p[0].db==p[1].db ); ds@1952: sqlite3VdbeMemRelease(p++); ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_EXPLAIN ds@1952: /* ds@1952: ** Give a listing of the program in the virtual machine. ds@1952: ** ds@1952: ** The interface is the same as sqlite3VdbeExec(). But instead of ds@1952: ** running the code, it invokes the callback once for each instruction. ds@1952: ** This feature is used to implement "EXPLAIN". ds@1952: */ ds@1952: int sqlite3VdbeList( ds@1952: Vdbe *p /* The VDBE */ ds@1952: ){ ds@1952: sqlite3 *db = p->db; ds@1952: int i; ds@1952: int rc = SQLITE_OK; ds@1952: ds@1952: assert( p->explain ); ds@1952: if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; ds@1952: assert( db->magic==SQLITE_MAGIC_BUSY ); ds@1952: assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); ds@1952: ds@1952: /* Even though this opcode does not put dynamic strings onto the ds@1952: ** the stack, they may become dynamic if the user calls ds@1952: ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. ds@1952: */ ds@1952: if( p->pTos==&p->aStack[4] ){ ds@1952: releaseMemArray(p->aStack, 5); ds@1952: } ds@1952: p->resOnStack = 0; ds@1952: ds@1952: do{ ds@1952: i = p->pc++; ds@1952: }while( inOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain ); ds@1952: if( i>=p->nOp ){ ds@1952: p->rc = SQLITE_OK; ds@1952: rc = SQLITE_DONE; ds@1952: }else if( db->u1.isInterrupted ){ ds@1952: p->rc = SQLITE_INTERRUPT; ds@1952: rc = SQLITE_ERROR; ds@1952: sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0); ds@1952: }else{ ds@1952: Op *pOp = &p->aOp[i]; ds@1952: Mem *pMem = p->aStack; ds@1952: pMem->flags = MEM_Int; ds@1952: pMem->type = SQLITE_INTEGER; ds@1952: pMem->u.i = i; /* Program counter */ ds@1952: pMem++; ds@1952: ds@1952: pMem->flags = MEM_Static|MEM_Str|MEM_Term; ds@1952: pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ ds@1952: assert( pMem->z!=0 ); ds@1952: pMem->n = strlen(pMem->z); ds@1952: pMem->type = SQLITE_TEXT; ds@1952: pMem->enc = SQLITE_UTF8; ds@1952: pMem++; ds@1952: ds@1952: pMem->flags = MEM_Int; ds@1952: pMem->u.i = pOp->p1; /* P1 */ ds@1952: pMem->type = SQLITE_INTEGER; ds@1952: pMem++; ds@1952: ds@1952: pMem->flags = MEM_Int; ds@1952: pMem->u.i = pOp->p2; /* P2 */ ds@1952: pMem->type = SQLITE_INTEGER; ds@1952: pMem++; ds@1952: ds@1952: pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P3 */ ds@1952: pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort)); ds@1952: assert( pMem->z!=0 ); ds@1952: pMem->n = strlen(pMem->z); ds@1952: pMem->type = SQLITE_TEXT; ds@1952: pMem->enc = SQLITE_UTF8; ds@1952: ds@1952: p->nResColumn = 5 - 2*(p->explain-1); ds@1952: p->pTos = pMem; ds@1952: p->rc = SQLITE_OK; ds@1952: p->resOnStack = 1; ds@1952: rc = SQLITE_ROW; ds@1952: } ds@1952: return rc; ds@1952: } ds@1952: #endif /* SQLITE_OMIT_EXPLAIN */ ds@1952: ds@1952: #ifdef SQLITE_DEBUG ds@1952: /* ds@1952: ** Print the SQL that was used to generate a VDBE program. ds@1952: */ ds@1952: void sqlite3VdbePrintSql(Vdbe *p){ ds@1952: int nOp = p->nOp; ds@1952: VdbeOp *pOp; ds@1952: if( nOp<1 ) return; ds@1952: pOp = &p->aOp[nOp-1]; ds@1952: if( pOp->opcode==OP_Noop && pOp->p3!=0 ){ ds@1952: const char *z = pOp->p3; ds@1952: while( isspace(*(u8*)z) ) z++; ds@1952: printf("SQL: [%s]\n", z); ds@1952: } ds@1952: } ds@1952: #endif ds@1952: ds@1952: #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) ds@1952: /* ds@1952: ** Print an IOTRACE message showing SQL content. ds@1952: */ ds@1952: void sqlite3VdbeIOTraceSql(Vdbe *p){ ds@1952: int nOp = p->nOp; ds@1952: VdbeOp *pOp; ds@1952: if( sqlite3_io_trace==0 ) return; ds@1952: if( nOp<1 ) return; ds@1952: pOp = &p->aOp[nOp-1]; ds@1952: if( pOp->opcode==OP_Noop && pOp->p3!=0 ){ ds@1952: int i, j; ds@1952: char z[1000]; ds@1952: sqlite3_snprintf(sizeof(z), z, "%s", pOp->p3); ds@1952: for(i=0; isspace((unsigned char)z[i]); i++){} ds@1952: for(j=0; z[i]; i++){ ds@1952: if( isspace((unsigned char)z[i]) ){ ds@1952: if( z[i-1]!=' ' ){ ds@1952: z[j++] = ' '; ds@1952: } ds@1952: }else{ ds@1952: z[j++] = z[i]; ds@1952: } ds@1952: } ds@1952: z[j] = 0; ds@1952: sqlite3_io_trace("SQL %s\n", z); ds@1952: } ds@1952: } ds@1952: #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */ ds@1952: ds@1952: ds@1952: /* ds@1952: ** Prepare a virtual machine for execution. This involves things such ds@1952: ** as allocating stack space and initializing the program counter. ds@1952: ** After the VDBE has be prepped, it can be executed by one or more ds@1952: ** calls to sqlite3VdbeExec(). ds@1952: ** ds@1952: ** This is the only way to move a VDBE from VDBE_MAGIC_INIT to ds@1952: ** VDBE_MAGIC_RUN. ds@1952: */ ds@1952: void sqlite3VdbeMakeReady( ds@1952: Vdbe *p, /* The VDBE */ ds@1952: int nVar, /* Number of '?' see in the SQL statement */ ds@1952: int nMem, /* Number of memory cells to allocate */ ds@1952: int nCursor, /* Number of cursors to allocate */ ds@1952: int isExplain /* True if the EXPLAIN keywords is present */ ds@1952: ){ ds@1952: int n; ds@1952: sqlite3 *db = p->db; ds@1952: ds@1952: assert( p!=0 ); ds@1952: assert( p->magic==VDBE_MAGIC_INIT ); ds@1952: ds@1952: /* There should be at least one opcode. ds@1952: */ ds@1952: assert( p->nOp>0 ); ds@1952: ds@1952: /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This ds@1952: * is because the call to resizeOpArray() below may shrink the ds@1952: * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN ds@1952: * state. ds@1952: */ ds@1952: p->magic = VDBE_MAGIC_RUN; ds@1952: ds@1952: /* No instruction ever pushes more than a single element onto the ds@1952: ** stack. And the stack never grows on successive executions of the ds@1952: ** same loop. So the total number of instructions is an upper bound ds@1952: ** on the maximum stack depth required. (Added later:) The ds@1952: ** resolveP2Values() call computes a tighter upper bound on the ds@1952: ** stack size. ds@1952: ** ds@1952: ** Allocation all the stack space we will ever need. ds@1952: */ ds@1952: if( p->aStack==0 ){ ds@1952: int nArg; /* Maximum number of args passed to a user function. */ ds@1952: int nStack; /* Maximum number of stack entries required */ ds@1952: resolveP2Values(p, &nArg, &nStack); ds@1952: resizeOpArray(p, p->nOp); ds@1952: assert( nVar>=0 ); ds@1952: assert( nStacknOp ); ds@1952: if( isExplain ){ ds@1952: nStack = 10; ds@1952: } ds@1952: p->aStack = sqlite3DbMallocZero(db, ds@1952: nStack*sizeof(p->aStack[0]) /* aStack */ ds@1952: + nArg*sizeof(Mem*) /* apArg */ ds@1952: + nVar*sizeof(Mem) /* aVar */ ds@1952: + nVar*sizeof(char*) /* azVar */ ds@1952: + nMem*sizeof(Mem) /* aMem */ ds@1952: + nCursor*sizeof(Cursor*) /* apCsr */ ds@1952: ); ds@1952: if( !db->mallocFailed ){ ds@1952: p->aMem = &p->aStack[nStack]; ds@1952: p->nMem = nMem; ds@1952: p->aVar = &p->aMem[nMem]; ds@1952: p->nVar = nVar; ds@1952: p->okVar = 0; ds@1952: p->apArg = (Mem**)&p->aVar[nVar]; ds@1952: p->azVar = (char**)&p->apArg[nArg]; ds@1952: p->apCsr = (Cursor**)&p->azVar[nVar]; ds@1952: p->nCursor = nCursor; ds@1952: for(n=0; naVar[n].flags = MEM_Null; ds@1952: p->aVar[n].db = db; ds@1952: } ds@1952: for(n=0; naStack[n].db = db; ds@1952: } ds@1952: } ds@1952: } ds@1952: for(n=0; nnMem; n++){ ds@1952: p->aMem[n].flags = MEM_Null; ds@1952: p->aMem[n].db = db; ds@1952: } ds@1952: ds@1952: p->pTos = &p->aStack[-1]; ds@1952: p->pc = -1; ds@1952: p->rc = SQLITE_OK; ds@1952: p->uniqueCnt = 0; ds@1952: p->returnDepth = 0; ds@1952: p->errorAction = OE_Abort; ds@1952: p->popStack = 0; ds@1952: p->explain |= isExplain; ds@1952: p->magic = VDBE_MAGIC_RUN; ds@1952: p->nChange = 0; ds@1952: p->cacheCtr = 1; ds@1952: p->minWriteFileFormat = 255; ds@1952: p->openedStatement = 0; ds@1952: #ifdef VDBE_PROFILE ds@1952: { ds@1952: int i; ds@1952: for(i=0; inOp; i++){ ds@1952: p->aOp[i].cnt = 0; ds@1952: p->aOp[i].cycles = 0; ds@1952: } ds@1952: } ds@1952: #endif ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Close a VDBE cursor and release all the resources that cursor happens ds@1952: ** to hold. ds@1952: */ ds@1952: void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){ ds@1952: if( pCx==0 ){ ds@1952: return; ds@1952: } ds@1952: if( pCx->pCursor ){ ds@1952: sqlite3BtreeCloseCursor(pCx->pCursor); ds@1952: } ds@1952: if( pCx->pBt ){ ds@1952: sqlite3BtreeClose(pCx->pBt); ds@1952: } ds@1952: #ifndef SQLITE_OMIT_VIRTUALTABLE ds@1952: if( pCx->pVtabCursor ){ ds@1952: sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; ds@1952: const sqlite3_module *pModule = pCx->pModule; ds@1952: p->inVtabMethod = 1; ds@1952: sqlite3SafetyOff(p->db); ds@1952: pModule->xClose(pVtabCursor); ds@1952: sqlite3SafetyOn(p->db); ds@1952: p->inVtabMethod = 0; ds@1952: } ds@1952: #endif ds@1952: sqlite3_free(pCx->pData); ds@1952: sqlite3_free(pCx->aType); ds@1952: sqlite3_free(pCx); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Close all cursors except for VTab cursors that are currently ds@1952: ** in use. ds@1952: */ ds@1952: static void closeAllCursorsExceptActiveVtabs(Vdbe *p){ ds@1952: int i; ds@1952: if( p->apCsr==0 ) return; ds@1952: for(i=0; inCursor; i++){ ds@1952: Cursor *pC = p->apCsr[i]; ds@1952: if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){ ds@1952: sqlite3VdbeFreeCursor(p, pC); ds@1952: p->apCsr[i] = 0; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Clean up the VM after execution. ds@1952: ** ds@1952: ** This routine will automatically close any cursors, lists, and/or ds@1952: ** sorters that were left open. It also deletes the values of ds@1952: ** variables in the aVar[] array. ds@1952: */ ds@1952: static void Cleanup(Vdbe *p){ ds@1952: int i; ds@1952: if( p->aStack ){ ds@1952: releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack)); ds@1952: p->pTos = &p->aStack[-1]; ds@1952: } ds@1952: closeAllCursorsExceptActiveVtabs(p); ds@1952: releaseMemArray(p->aMem, p->nMem); ds@1952: sqlite3VdbeFifoClear(&p->sFifo); ds@1952: if( p->contextStack ){ ds@1952: for(i=0; icontextStackTop; i++){ ds@1952: sqlite3VdbeFifoClear(&p->contextStack[i].sFifo); ds@1952: } ds@1952: sqlite3_free(p->contextStack); ds@1952: } ds@1952: p->contextStack = 0; ds@1952: p->contextStackDepth = 0; ds@1952: p->contextStackTop = 0; ds@1952: sqlite3_free(p->zErrMsg); ds@1952: p->zErrMsg = 0; ds@1952: p->resOnStack = 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Set the number of result columns that will be returned by this SQL ds@1952: ** statement. This is now set at compile time, rather than during ds@1952: ** execution of the vdbe program so that sqlite3_column_count() can ds@1952: ** be called on an SQL statement before sqlite3_step(). ds@1952: */ ds@1952: void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ ds@1952: Mem *pColName; ds@1952: int n; ds@1952: ds@1952: releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); ds@1952: sqlite3_free(p->aColName); ds@1952: n = nResColumn*COLNAME_N; ds@1952: p->nResColumn = nResColumn; ds@1952: p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n ); ds@1952: if( p->aColName==0 ) return; ds@1952: while( n-- > 0 ){ ds@1952: pColName->flags = MEM_Null; ds@1952: pColName->db = p->db; ds@1952: pColName++; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Set the name of the idx'th column to be returned by the SQL statement. ds@1952: ** zName must be a pointer to a nul terminated string. ds@1952: ** ds@1952: ** This call must be made after a call to sqlite3VdbeSetNumCols(). ds@1952: ** ds@1952: ** If N==P3_STATIC it means that zName is a pointer to a constant static ds@1952: ** string and we can just copy the pointer. If it is P3_DYNAMIC, then ds@1952: ** the string is freed using sqlite3_free() when the vdbe is finished with ds@1952: ** it. Otherwise, N bytes of zName are copied. ds@1952: */ ds@1952: int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){ ds@1952: int rc; ds@1952: Mem *pColName; ds@1952: assert( idxnResColumn ); ds@1952: assert( vardb->mallocFailed ) return SQLITE_NOMEM; ds@1952: assert( p->aColName!=0 ); ds@1952: pColName = &(p->aColName[idx+var*p->nResColumn]); ds@1952: if( N==P3_DYNAMIC || N==P3_STATIC ){ ds@1952: rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC); ds@1952: }else{ ds@1952: rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT); ds@1952: } ds@1952: if( rc==SQLITE_OK && N==P3_DYNAMIC ){ ds@1952: pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn; ds@1952: pColName->xDel = 0; ds@1952: } ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** A read or write transaction may or may not be active on database handle ds@1952: ** db. If a transaction is active, commit it. If there is a ds@1952: ** write-transaction spanning more than one database file, this routine ds@1952: ** takes care of the master journal trickery. ds@1952: */ ds@1952: static int vdbeCommit(sqlite3 *db){ ds@1952: int i; ds@1952: int nTrans = 0; /* Number of databases with an active write-transaction */ ds@1952: int rc = SQLITE_OK; ds@1952: int needXcommit = 0; ds@1952: ds@1952: /* Before doing anything else, call the xSync() callback for any ds@1952: ** virtual module tables written in this transaction. This has to ds@1952: ** be done before determining whether a master journal file is ds@1952: ** required, as an xSync() callback may add an attached database ds@1952: ** to the transaction. ds@1952: */ ds@1952: rc = sqlite3VtabSync(db, rc); ds@1952: if( rc!=SQLITE_OK ){ ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* This loop determines (a) if the commit hook should be invoked and ds@1952: ** (b) how many database files have open write transactions, not ds@1952: ** including the temp database. (b) is important because if more than ds@1952: ** one database file has an open write transaction, a master journal ds@1952: ** file is required for an atomic commit. ds@1952: */ ds@1952: for(i=0; inDb; i++){ ds@1952: Btree *pBt = db->aDb[i].pBt; ds@1952: if( sqlite3BtreeIsInTrans(pBt) ){ ds@1952: needXcommit = 1; ds@1952: if( i!=1 ) nTrans++; ds@1952: } ds@1952: } ds@1952: ds@1952: /* If there are any write-transactions at all, invoke the commit hook */ ds@1952: if( needXcommit && db->xCommitCallback ){ ds@1952: sqlite3SafetyOff(db); ds@1952: rc = db->xCommitCallback(db->pCommitArg); ds@1952: sqlite3SafetyOn(db); ds@1952: if( rc ){ ds@1952: return SQLITE_CONSTRAINT; ds@1952: } ds@1952: } ds@1952: ds@1952: /* The simple case - no more than one database file (not counting the ds@1952: ** TEMP database) has a transaction active. There is no need for the ds@1952: ** master-journal. ds@1952: ** ds@1952: ** If the return value of sqlite3BtreeGetFilename() is a zero length ds@1952: ** string, it means the main database is :memory:. In that case we do ds@1952: ** not support atomic multi-file commits, so use the simple case then ds@1952: ** too. ds@1952: */ ds@1952: if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){ ds@1952: for(i=0; rc==SQLITE_OK && inDb; i++){ ds@1952: Btree *pBt = db->aDb[i].pBt; ds@1952: if( pBt ){ ds@1952: rc = sqlite3BtreeCommitPhaseOne(pBt, 0); ds@1952: } ds@1952: } ds@1952: ds@1952: /* Do the commit only if all databases successfully complete phase 1. ds@1952: ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an ds@1952: ** IO error while deleting or truncating a journal file. It is unlikely, ds@1952: ** but could happen. In this case abandon processing and return the error. ds@1952: */ ds@1952: for(i=0; rc==SQLITE_OK && inDb; i++){ ds@1952: Btree *pBt = db->aDb[i].pBt; ds@1952: if( pBt ){ ds@1952: rc = sqlite3BtreeCommitPhaseTwo(pBt); ds@1952: } ds@1952: } ds@1952: if( rc==SQLITE_OK ){ ds@1952: sqlite3VtabCommit(db); ds@1952: } ds@1952: } ds@1952: ds@1952: /* The complex case - There is a multi-file write-transaction active. ds@1952: ** This requires a master journal file to ensure the transaction is ds@1952: ** committed atomicly. ds@1952: */ ds@1952: #ifndef SQLITE_OMIT_DISKIO ds@1952: else{ ds@1952: sqlite3_vfs *pVfs = db->pVfs; ds@1952: int needSync = 0; ds@1952: char *zMaster = 0; /* File-name for the master journal */ ds@1952: char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); ds@1952: sqlite3_file *pMaster = 0; ds@1952: i64 offset = 0; ds@1952: ds@1952: /* Select a master journal file name */ ds@1952: do { ds@1952: u32 random; ds@1952: sqlite3_free(zMaster); ds@1952: sqlite3Randomness(sizeof(random), &random); ds@1952: zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff); ds@1952: if( !zMaster ){ ds@1952: return SQLITE_NOMEM; ds@1952: } ds@1952: }while( sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS) ); ds@1952: ds@1952: /* Open the master journal. */ ds@1952: rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, ds@1952: SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| ds@1952: SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 ds@1952: ); ds@1952: if( rc!=SQLITE_OK ){ ds@1952: sqlite3_free(zMaster); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* Write the name of each database file in the transaction into the new ds@1952: ** master journal file. If an error occurs at this point close ds@1952: ** and delete the master journal file. All the individual journal files ds@1952: ** still have 'null' as the master journal pointer, so they will roll ds@1952: ** back independently if a failure occurs. ds@1952: */ ds@1952: for(i=0; inDb; i++){ ds@1952: Btree *pBt = db->aDb[i].pBt; ds@1952: if( i==1 ) continue; /* Ignore the TEMP database */ ds@1952: if( sqlite3BtreeIsInTrans(pBt) ){ ds@1952: char const *zFile = sqlite3BtreeGetJournalname(pBt); ds@1952: if( zFile[0]==0 ) continue; /* Ignore :memory: databases */ ds@1952: if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){ ds@1952: needSync = 1; ds@1952: } ds@1952: rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset); ds@1952: offset += strlen(zFile)+1; ds@1952: if( rc!=SQLITE_OK ){ ds@1952: sqlite3OsCloseFree(pMaster); ds@1952: sqlite3OsDelete(pVfs, zMaster, 0); ds@1952: sqlite3_free(zMaster); ds@1952: return rc; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* Sync the master journal file. If the IOCAP_SEQUENTIAL device ds@1952: ** flag is set this is not required. ds@1952: */ ds@1952: zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt); ds@1952: if( (needSync ds@1952: && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)) ds@1952: && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){ ds@1952: sqlite3OsCloseFree(pMaster); ds@1952: sqlite3OsDelete(pVfs, zMaster, 0); ds@1952: sqlite3_free(zMaster); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* Sync all the db files involved in the transaction. The same call ds@1952: ** sets the master journal pointer in each individual journal. If ds@1952: ** an error occurs here, do not delete the master journal file. ds@1952: ** ds@1952: ** If the error occurs during the first call to ds@1952: ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the ds@1952: ** master journal file will be orphaned. But we cannot delete it, ds@1952: ** in case the master journal file name was written into the journal ds@1952: ** file before the failure occured. ds@1952: */ ds@1952: for(i=0; rc==SQLITE_OK && inDb; i++){ ds@1952: Btree *pBt = db->aDb[i].pBt; ds@1952: if( pBt ){ ds@1952: rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster); ds@1952: } ds@1952: } ds@1952: sqlite3OsCloseFree(pMaster); ds@1952: if( rc!=SQLITE_OK ){ ds@1952: sqlite3_free(zMaster); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* Delete the master journal file. This commits the transaction. After ds@1952: ** doing this the directory is synced again before any individual ds@1952: ** transaction files are deleted. ds@1952: */ ds@1952: rc = sqlite3OsDelete(pVfs, zMaster, 1); ds@1952: sqlite3_free(zMaster); ds@1952: zMaster = 0; ds@1952: if( rc ){ ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* All files and directories have already been synced, so the following ds@1952: ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and ds@1952: ** deleting or truncating journals. If something goes wrong while ds@1952: ** this is happening we don't really care. The integrity of the ds@1952: ** transaction is already guaranteed, but some stray 'cold' journals ds@1952: ** may be lying around. Returning an error code won't help matters. ds@1952: */ ds@1952: disable_simulated_io_errors(); ds@1952: for(i=0; inDb; i++){ ds@1952: Btree *pBt = db->aDb[i].pBt; ds@1952: if( pBt ){ ds@1952: sqlite3BtreeCommitPhaseTwo(pBt); ds@1952: } ds@1952: } ds@1952: enable_simulated_io_errors(); ds@1952: ds@1952: sqlite3VtabCommit(db); ds@1952: } ds@1952: #endif ds@1952: ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This routine checks that the sqlite3.activeVdbeCnt count variable ds@1952: ** matches the number of vdbe's in the list sqlite3.pVdbe that are ds@1952: ** currently active. An assertion fails if the two counts do not match. ds@1952: ** This is an internal self-check only - it is not an essential processing ds@1952: ** step. ds@1952: ** ds@1952: ** This is a no-op if NDEBUG is defined. ds@1952: */ ds@1952: #ifndef NDEBUG ds@1952: static void checkActiveVdbeCnt(sqlite3 *db){ ds@1952: Vdbe *p; ds@1952: int cnt = 0; ds@1952: p = db->pVdbe; ds@1952: while( p ){ ds@1952: if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){ ds@1952: cnt++; ds@1952: } ds@1952: p = p->pNext; ds@1952: } ds@1952: assert( cnt==db->activeVdbeCnt ); ds@1952: } ds@1952: #else ds@1952: #define checkActiveVdbeCnt(x) ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** For every Btree that in database connection db which ds@1952: ** has been modified, "trip" or invalidate each cursor in ds@1952: ** that Btree might have been modified so that the cursor ds@1952: ** can never be used again. This happens when a rollback ds@1952: *** occurs. We have to trip all the other cursors, even ds@1952: ** cursor from other VMs in different database connections, ds@1952: ** so that none of them try to use the data at which they ds@1952: ** were pointing and which now may have been changed due ds@1952: ** to the rollback. ds@1952: ** ds@1952: ** Remember that a rollback can delete tables complete and ds@1952: ** reorder rootpages. So it is not sufficient just to save ds@1952: ** the state of the cursor. We have to invalidate the cursor ds@1952: ** so that it is never used again. ds@1952: */ ds@1952: void invalidateCursorsOnModifiedBtrees(sqlite3 *db){ ds@1952: int i; ds@1952: for(i=0; inDb; i++){ ds@1952: Btree *p = db->aDb[i].pBt; ds@1952: if( p && sqlite3BtreeIsInTrans(p) ){ ds@1952: sqlite3BtreeTripAllCursors(p, SQLITE_ABORT); ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This routine is called the when a VDBE tries to halt. If the VDBE ds@1952: ** has made changes and is in autocommit mode, then commit those ds@1952: ** changes. If a rollback is needed, then do the rollback. ds@1952: ** ds@1952: ** This routine is the only way to move the state of a VM from ds@1952: ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to ds@1952: ** call this on a VM that is in the SQLITE_MAGIC_HALT state. ds@1952: ** ds@1952: ** Return an error code. If the commit could not complete because of ds@1952: ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it ds@1952: ** means the close did not happen and needs to be repeated. ds@1952: */ ds@1952: int sqlite3VdbeHalt(Vdbe *p){ ds@1952: sqlite3 *db = p->db; ds@1952: int i; ds@1952: int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */ ds@1952: int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */ ds@1952: ds@1952: /* This function contains the logic that determines if a statement or ds@1952: ** transaction will be committed or rolled back as a result of the ds@1952: ** execution of this virtual machine. ds@1952: ** ds@1952: ** If any of the following errors occur: ds@1952: ** ds@1952: ** SQLITE_NOMEM ds@1952: ** SQLITE_IOERR ds@1952: ** SQLITE_FULL ds@1952: ** SQLITE_INTERRUPT ds@1952: ** ds@1952: ** Then the internal cache might have been left in an inconsistent ds@1952: ** state. We need to rollback the statement transaction, if there is ds@1952: ** one, or the complete transaction if there is no statement transaction. ds@1952: */ ds@1952: ds@1952: if( p->db->mallocFailed ){ ds@1952: p->rc = SQLITE_NOMEM; ds@1952: } ds@1952: closeAllCursorsExceptActiveVtabs(p); ds@1952: if( p->magic!=VDBE_MAGIC_RUN ){ ds@1952: return SQLITE_OK; ds@1952: } ds@1952: checkActiveVdbeCnt(db); ds@1952: ds@1952: /* No commit or rollback needed if the program never started */ ds@1952: if( p->pc>=0 ){ ds@1952: int mrc; /* Primary error code from p->rc */ ds@1952: ds@1952: /* Lock all btrees used by the statement */ ds@1952: sqlite3BtreeMutexArrayEnter(&p->aMutex); ds@1952: ds@1952: /* Check for one of the special errors */ ds@1952: mrc = p->rc & 0xff; ds@1952: isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR ds@1952: || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL ; ds@1952: if( isSpecialError ){ ds@1952: /* This loop does static analysis of the query to see which of the ds@1952: ** following three categories it falls into: ds@1952: ** ds@1952: ** Read-only ds@1952: ** Query with statement journal ds@1952: ** Query without statement journal ds@1952: ** ds@1952: ** We could do something more elegant than this static analysis (i.e. ds@1952: ** store the type of query as part of the compliation phase), but ds@1952: ** handling malloc() or IO failure is a fairly obscure edge case so ds@1952: ** this is probably easier. Todo: Might be an opportunity to reduce ds@1952: ** code size a very small amount though... ds@1952: */ ds@1952: int notReadOnly = 0; ds@1952: int isStatement = 0; ds@1952: assert(p->aOp || p->nOp==0); ds@1952: for(i=0; inOp; i++){ ds@1952: switch( p->aOp[i].opcode ){ ds@1952: case OP_Transaction: ds@1952: notReadOnly |= p->aOp[i].p2; ds@1952: break; ds@1952: case OP_Statement: ds@1952: isStatement = 1; ds@1952: break; ds@1952: } ds@1952: } ds@1952: ds@1952: ds@1952: /* If the query was read-only, we need do no rollback at all. Otherwise, ds@1952: ** proceed with the special handling. ds@1952: */ ds@1952: if( notReadOnly || mrc!=SQLITE_INTERRUPT ){ ds@1952: if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){ ds@1952: xFunc = sqlite3BtreeRollbackStmt; ds@1952: p->rc = SQLITE_BUSY; ds@1952: } else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && isStatement ){ ds@1952: xFunc = sqlite3BtreeRollbackStmt; ds@1952: }else{ ds@1952: /* We are forced to roll back the active transaction. Before doing ds@1952: ** so, abort any other statements this handle currently has active. ds@1952: */ ds@1952: invalidateCursorsOnModifiedBtrees(db); ds@1952: sqlite3RollbackAll(db); ds@1952: db->autoCommit = 1; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* If the auto-commit flag is set and this is the only active vdbe, then ds@1952: ** we do either a commit or rollback of the current transaction. ds@1952: ** ds@1952: ** Note: This block also runs if one of the special errors handled ds@1952: ** above has occured. ds@1952: */ ds@1952: if( db->autoCommit && db->activeVdbeCnt==1 ){ ds@1952: if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ ds@1952: /* The auto-commit flag is true, and the vdbe program was ds@1952: ** successful or hit an 'OR FAIL' constraint. This means a commit ds@1952: ** is required. ds@1952: */ ds@1952: int rc = vdbeCommit(db); ds@1952: if( rc==SQLITE_BUSY ){ ds@1952: sqlite3BtreeMutexArrayLeave(&p->aMutex); ds@1952: return SQLITE_BUSY; ds@1952: }else if( rc!=SQLITE_OK ){ ds@1952: p->rc = rc; ds@1952: sqlite3RollbackAll(db); ds@1952: }else{ ds@1952: sqlite3CommitInternalChanges(db); ds@1952: } ds@1952: }else{ ds@1952: sqlite3RollbackAll(db); ds@1952: } ds@1952: }else if( !xFunc ){ ds@1952: if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ ds@1952: if( p->openedStatement ){ ds@1952: xFunc = sqlite3BtreeCommitStmt; ds@1952: } ds@1952: }else if( p->errorAction==OE_Abort ){ ds@1952: xFunc = sqlite3BtreeRollbackStmt; ds@1952: }else{ ds@1952: invalidateCursorsOnModifiedBtrees(db); ds@1952: sqlite3RollbackAll(db); ds@1952: db->autoCommit = 1; ds@1952: } ds@1952: } ds@1952: ds@1952: /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or ds@1952: ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs ds@1952: ** and the return code is still SQLITE_OK, set the return code to the new ds@1952: ** error value. ds@1952: */ ds@1952: assert(!xFunc || ds@1952: xFunc==sqlite3BtreeCommitStmt || ds@1952: xFunc==sqlite3BtreeRollbackStmt ds@1952: ); ds@1952: for(i=0; xFunc && inDb; i++){ ds@1952: int rc; ds@1952: Btree *pBt = db->aDb[i].pBt; ds@1952: if( pBt ){ ds@1952: rc = xFunc(pBt); ds@1952: if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){ ds@1952: p->rc = rc; ds@1952: sqlite3SetString(&p->zErrMsg, 0); ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* If this was an INSERT, UPDATE or DELETE and the statement was committed, ds@1952: ** set the change counter. ds@1952: */ ds@1952: if( p->changeCntOn && p->pc>=0 ){ ds@1952: if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){ ds@1952: sqlite3VdbeSetChanges(db, p->nChange); ds@1952: }else{ ds@1952: sqlite3VdbeSetChanges(db, 0); ds@1952: } ds@1952: p->nChange = 0; ds@1952: } ds@1952: ds@1952: /* Rollback or commit any schema changes that occurred. */ ds@1952: if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){ ds@1952: sqlite3ResetInternalSchema(db, 0); ds@1952: db->flags = (db->flags | SQLITE_InternChanges); ds@1952: } ds@1952: ds@1952: /* Release the locks */ ds@1952: sqlite3BtreeMutexArrayLeave(&p->aMutex); ds@1952: } ds@1952: ds@1952: /* We have successfully halted and closed the VM. Record this fact. */ ds@1952: if( p->pc>=0 ){ ds@1952: db->activeVdbeCnt--; ds@1952: } ds@1952: p->magic = VDBE_MAGIC_HALT; ds@1952: checkActiveVdbeCnt(db); ds@1952: if( p->db->mallocFailed ){ ds@1952: p->rc = SQLITE_NOMEM; ds@1952: } ds@1952: checkActiveVdbeCnt(db); ds@1952: ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Each VDBE holds the result of the most recent sqlite3_step() call ds@1952: ** in p->rc. This routine sets that result back to SQLITE_OK. ds@1952: */ ds@1952: void sqlite3VdbeResetStepResult(Vdbe *p){ ds@1952: p->rc = SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Clean up a VDBE after execution but do not delete the VDBE just yet. ds@1952: ** Write any error messages into *pzErrMsg. Return the result code. ds@1952: ** ds@1952: ** After this routine is run, the VDBE should be ready to be executed ds@1952: ** again. ds@1952: ** ds@1952: ** To look at it another way, this routine resets the state of the ds@1952: ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to ds@1952: ** VDBE_MAGIC_INIT. ds@1952: */ ds@1952: int sqlite3VdbeReset(Vdbe *p){ ds@1952: sqlite3 *db; ds@1952: db = p->db; ds@1952: ds@1952: /* If the VM did not run to completion or if it encountered an ds@1952: ** error, then it might not have been halted properly. So halt ds@1952: ** it now. ds@1952: */ ds@1952: sqlite3SafetyOn(db); ds@1952: sqlite3VdbeHalt(p); ds@1952: sqlite3SafetyOff(db); ds@1952: ds@1952: /* If the VDBE has be run even partially, then transfer the error code ds@1952: ** and error message from the VDBE into the main database structure. But ds@1952: ** if the VDBE has just been set to run but has not actually executed any ds@1952: ** instructions yet, leave the main database error information unchanged. ds@1952: */ ds@1952: if( p->pc>=0 ){ ds@1952: if( p->zErrMsg ){ ds@1952: sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,sqlite3_free); ds@1952: db->errCode = p->rc; ds@1952: p->zErrMsg = 0; ds@1952: }else if( p->rc ){ ds@1952: sqlite3Error(db, p->rc, 0); ds@1952: }else{ ds@1952: sqlite3Error(db, SQLITE_OK, 0); ds@1952: } ds@1952: }else if( p->rc && p->expired ){ ds@1952: /* The expired flag was set on the VDBE before the first call ds@1952: ** to sqlite3_step(). For consistency (since sqlite3_step() was ds@1952: ** called), set the database error in this case as well. ds@1952: */ ds@1952: sqlite3Error(db, p->rc, 0); ds@1952: } ds@1952: ds@1952: /* Reclaim all memory used by the VDBE ds@1952: */ ds@1952: Cleanup(p); ds@1952: ds@1952: /* Save profiling information from this VDBE run. ds@1952: */ ds@1952: assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack ); ds@1952: #ifdef VDBE_PROFILE ds@1952: { ds@1952: FILE *out = fopen("vdbe_profile.out", "a"); ds@1952: if( out ){ ds@1952: int i; ds@1952: fprintf(out, "---- "); ds@1952: for(i=0; inOp; i++){ ds@1952: fprintf(out, "%02x", p->aOp[i].opcode); ds@1952: } ds@1952: fprintf(out, "\n"); ds@1952: for(i=0; inOp; i++){ ds@1952: fprintf(out, "%6d %10lld %8lld ", ds@1952: p->aOp[i].cnt, ds@1952: p->aOp[i].cycles, ds@1952: p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 ds@1952: ); ds@1952: sqlite3VdbePrintOp(out, i, &p->aOp[i]); ds@1952: } ds@1952: fclose(out); ds@1952: } ds@1952: } ds@1952: #endif ds@1952: p->magic = VDBE_MAGIC_INIT; ds@1952: p->aborted = 0; ds@1952: return p->rc & db->errMask; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Clean up and delete a VDBE after execution. Return an integer which is ds@1952: ** the result code. Write any error message text into *pzErrMsg. ds@1952: */ ds@1952: int sqlite3VdbeFinalize(Vdbe *p){ ds@1952: int rc = SQLITE_OK; ds@1952: if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ ds@1952: rc = sqlite3VdbeReset(p); ds@1952: assert( (rc & p->db->errMask)==rc ); ds@1952: }else if( p->magic!=VDBE_MAGIC_INIT ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: sqlite3VdbeDelete(p); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Call the destructor for each auxdata entry in pVdbeFunc for which ds@1952: ** the corresponding bit in mask is clear. Auxdata entries beyond 31 ds@1952: ** are always destroyed. To destroy all auxdata entries, call this ds@1952: ** routine with mask==0. ds@1952: */ ds@1952: void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){ ds@1952: int i; ds@1952: for(i=0; inAux; i++){ ds@1952: struct AuxData *pAux = &pVdbeFunc->apAux[i]; ds@1952: if( (i>31 || !(mask&(1<pAux ){ ds@1952: if( pAux->xDelete ){ ds@1952: pAux->xDelete(pAux->pAux); ds@1952: } ds@1952: pAux->pAux = 0; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Delete an entire VDBE. ds@1952: */ ds@1952: void sqlite3VdbeDelete(Vdbe *p){ ds@1952: int i; ds@1952: if( p==0 ) return; ds@1952: Cleanup(p); ds@1952: if( p->pPrev ){ ds@1952: p->pPrev->pNext = p->pNext; ds@1952: }else{ ds@1952: assert( p->db->pVdbe==p ); ds@1952: p->db->pVdbe = p->pNext; ds@1952: } ds@1952: if( p->pNext ){ ds@1952: p->pNext->pPrev = p->pPrev; ds@1952: } ds@1952: if( p->aOp ){ ds@1952: for(i=0; inOp; i++){ ds@1952: Op *pOp = &p->aOp[i]; ds@1952: freeP3(pOp->p3type, pOp->p3); ds@1952: } ds@1952: sqlite3_free(p->aOp); ds@1952: } ds@1952: releaseMemArray(p->aVar, p->nVar); ds@1952: sqlite3_free(p->aLabel); ds@1952: sqlite3_free(p->aStack); ds@1952: releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); ds@1952: sqlite3_free(p->aColName); ds@1952: sqlite3_free(p->zSql); ds@1952: p->magic = VDBE_MAGIC_DEAD; ds@1952: sqlite3_free(p); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** If a MoveTo operation is pending on the given cursor, then do that ds@1952: ** MoveTo now. Return an error code. If no MoveTo is pending, this ds@1952: ** routine does nothing and returns SQLITE_OK. ds@1952: */ ds@1952: int sqlite3VdbeCursorMoveto(Cursor *p){ ds@1952: if( p->deferredMoveto ){ ds@1952: int res, rc; ds@1952: #ifdef SQLITE_TEST ds@1952: extern int sqlite3_search_count; ds@1952: #endif ds@1952: assert( p->isTable ); ds@1952: rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, 0, &res); ds@1952: if( rc ) return rc; ds@1952: *p->pIncrKey = 0; ds@1952: p->lastRowid = keyToInt(p->movetoTarget); ds@1952: p->rowidIsValid = res==0; ds@1952: if( res<0 ){ ds@1952: rc = sqlite3BtreeNext(p->pCursor, &res); ds@1952: if( rc ) return rc; ds@1952: } ds@1952: #ifdef SQLITE_TEST ds@1952: sqlite3_search_count++; ds@1952: #endif ds@1952: p->deferredMoveto = 0; ds@1952: p->cacheStatus = CACHE_STALE; ds@1952: } ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The following functions: ds@1952: ** ds@1952: ** sqlite3VdbeSerialType() ds@1952: ** sqlite3VdbeSerialTypeLen() ds@1952: ** sqlite3VdbeSerialRead() ds@1952: ** sqlite3VdbeSerialLen() ds@1952: ** sqlite3VdbeSerialWrite() ds@1952: ** ds@1952: ** encapsulate the code that serializes values for storage in SQLite ds@1952: ** data and index records. Each serialized value consists of a ds@1952: ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned ds@1952: ** integer, stored as a varint. ds@1952: ** ds@1952: ** In an SQLite index record, the serial type is stored directly before ds@1952: ** the blob of data that it corresponds to. In a table record, all serial ds@1952: ** types are stored at the start of the record, and the blobs of data at ds@1952: ** the end. Hence these functions allow the caller to handle the ds@1952: ** serial-type and data blob seperately. ds@1952: ** ds@1952: ** The following table describes the various storage classes for data: ds@1952: ** ds@1952: ** serial type bytes of data type ds@1952: ** -------------- --------------- --------------- ds@1952: ** 0 0 NULL ds@1952: ** 1 1 signed integer ds@1952: ** 2 2 signed integer ds@1952: ** 3 3 signed integer ds@1952: ** 4 4 signed integer ds@1952: ** 5 6 signed integer ds@1952: ** 6 8 signed integer ds@1952: ** 7 8 IEEE float ds@1952: ** 8 0 Integer constant 0 ds@1952: ** 9 0 Integer constant 1 ds@1952: ** 10,11 reserved for expansion ds@1952: ** N>=12 and even (N-12)/2 BLOB ds@1952: ** N>=13 and odd (N-13)/2 text ds@1952: ** ds@1952: ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions ds@1952: ** of SQLite will not understand those serial types. ds@1952: */ ds@1952: ds@1952: /* ds@1952: ** Return the serial-type for the value stored in pMem. ds@1952: */ ds@1952: u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ ds@1952: int flags = pMem->flags; ds@1952: int n; ds@1952: ds@1952: if( flags&MEM_Null ){ ds@1952: return 0; ds@1952: } ds@1952: if( flags&MEM_Int ){ ds@1952: /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ ds@1952: # define MAX_6BYTE ((((i64)0x00001000)<<32)-1) ds@1952: i64 i = pMem->u.i; ds@1952: u64 u; ds@1952: if( file_format>=4 && (i&1)==i ){ ds@1952: return 8+i; ds@1952: } ds@1952: u = i<0 ? -i : i; ds@1952: if( u<=127 ) return 1; ds@1952: if( u<=32767 ) return 2; ds@1952: if( u<=8388607 ) return 3; ds@1952: if( u<=2147483647 ) return 4; ds@1952: if( u<=MAX_6BYTE ) return 5; ds@1952: return 6; ds@1952: } ds@1952: if( flags&MEM_Real ){ ds@1952: return 7; ds@1952: } ds@1952: assert( flags&(MEM_Str|MEM_Blob) ); ds@1952: n = pMem->n; ds@1952: if( flags & MEM_Zero ){ ds@1952: n += pMem->u.i; ds@1952: } ds@1952: assert( n>=0 ); ds@1952: return ((n*2) + 12 + ((flags&MEM_Str)!=0)); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the length of the data corresponding to the supplied serial-type. ds@1952: */ ds@1952: int sqlite3VdbeSerialTypeLen(u32 serial_type){ ds@1952: if( serial_type>=12 ){ ds@1952: return (serial_type-12)/2; ds@1952: }else{ ds@1952: static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 }; ds@1952: return aSize[serial_type]; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** If we are on an architecture with mixed-endian floating ds@1952: ** points (ex: ARM7) then swap the lower 4 bytes with the ds@1952: ** upper 4 bytes. Return the result. ds@1952: ** ds@1952: ** For most architectures, this is a no-op. ds@1952: ** ds@1952: ** (later): It is reported to me that the mixed-endian problem ds@1952: ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems ds@1952: ** that early versions of GCC stored the two words of a 64-bit ds@1952: ** float in the wrong order. And that error has been propagated ds@1952: ** ever since. The blame is not necessarily with GCC, though. ds@1952: ** GCC might have just copying the problem from a prior compiler. ds@1952: ** I am also told that newer versions of GCC that follow a different ds@1952: ** ABI get the byte order right. ds@1952: ** ds@1952: ** Developers using SQLite on an ARM7 should compile and run their ds@1952: ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG ds@1952: ** enabled, some asserts below will ensure that the byte order of ds@1952: ** floating point values is correct. ds@1952: ** ds@1952: ** (2007-08-30) Frank van Vugt has studied this problem closely ds@1952: ** and has send his findings to the SQLite developers. Frank ds@1952: ** writes that some Linux kernels offer floating point hardware ds@1952: ** emulation that uses only 32-bit mantissas instead of a full ds@1952: ** 48-bits as required by the IEEE standard. (This is the ds@1952: ** CONFIG_FPE_FASTFPE option.) On such systems, floating point ds@1952: ** byte swapping becomes very complicated. To avoid problems, ds@1952: ** the necessary byte swapping is carried out using a 64-bit integer ds@1952: ** rather than a 64-bit float. Frank assures us that the code here ds@1952: ** works for him. We, the developers, have no way to independently ds@1952: ** verify this, but Frank seems to know what he is talking about ds@1952: ** so we trust him. ds@1952: */ ds@1952: #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT ds@1952: static u64 floatSwap(u64 in){ ds@1952: union { ds@1952: u64 r; ds@1952: u32 i[2]; ds@1952: } u; ds@1952: u32 t; ds@1952: ds@1952: u.r = in; ds@1952: t = u.i[0]; ds@1952: u.i[0] = u.i[1]; ds@1952: u.i[1] = t; ds@1952: return u.r; ds@1952: } ds@1952: # define swapMixedEndianFloat(X) X = floatSwap(X) ds@1952: #else ds@1952: # define swapMixedEndianFloat(X) ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Write the serialized data blob for the value stored in pMem into ds@1952: ** buf. It is assumed that the caller has allocated sufficient space. ds@1952: ** Return the number of bytes written. ds@1952: ** ds@1952: ** nBuf is the amount of space left in buf[]. nBuf must always be ds@1952: ** large enough to hold the entire field. Except, if the field is ds@1952: ** a blob with a zero-filled tail, then buf[] might be just the right ds@1952: ** size to hold everything except for the zero-filled tail. If buf[] ds@1952: ** is only big enough to hold the non-zero prefix, then only write that ds@1952: ** prefix into buf[]. But if buf[] is large enough to hold both the ds@1952: ** prefix and the tail then write the prefix and set the tail to all ds@1952: ** zeros. ds@1952: ** ds@1952: ** Return the number of bytes actually written into buf[]. The number ds@1952: ** of bytes in the zero-filled tail is included in the return value only ds@1952: ** if those bytes were zeroed in buf[]. ds@1952: */ ds@1952: int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){ ds@1952: u32 serial_type = sqlite3VdbeSerialType(pMem, file_format); ds@1952: int len; ds@1952: ds@1952: /* Integer and Real */ ds@1952: if( serial_type<=7 && serial_type>0 ){ ds@1952: u64 v; ds@1952: int i; ds@1952: if( serial_type==7 ){ ds@1952: assert( sizeof(v)==sizeof(pMem->r) ); ds@1952: memcpy(&v, &pMem->r, sizeof(v)); ds@1952: swapMixedEndianFloat(v); ds@1952: }else{ ds@1952: v = pMem->u.i; ds@1952: } ds@1952: len = i = sqlite3VdbeSerialTypeLen(serial_type); ds@1952: assert( len<=nBuf ); ds@1952: while( i-- ){ ds@1952: buf[i] = (v&0xFF); ds@1952: v >>= 8; ds@1952: } ds@1952: return len; ds@1952: } ds@1952: ds@1952: /* String or blob */ ds@1952: if( serial_type>=12 ){ ds@1952: assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0) ds@1952: == sqlite3VdbeSerialTypeLen(serial_type) ); ds@1952: assert( pMem->n<=nBuf ); ds@1952: len = pMem->n; ds@1952: memcpy(buf, pMem->z, len); ds@1952: if( pMem->flags & MEM_Zero ){ ds@1952: len += pMem->u.i; ds@1952: if( len>nBuf ){ ds@1952: len = nBuf; ds@1952: } ds@1952: memset(&buf[pMem->n], 0, len-pMem->n); ds@1952: } ds@1952: return len; ds@1952: } ds@1952: ds@1952: /* NULL or constants 0 or 1 */ ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Deserialize the data blob pointed to by buf as serial type serial_type ds@1952: ** and store the result in pMem. Return the number of bytes read. ds@1952: */ ds@1952: int sqlite3VdbeSerialGet( ds@1952: const unsigned char *buf, /* Buffer to deserialize from */ ds@1952: u32 serial_type, /* Serial type to deserialize */ ds@1952: Mem *pMem /* Memory cell to write value into */ ds@1952: ){ ds@1952: switch( serial_type ){ ds@1952: case 10: /* Reserved for future use */ ds@1952: case 11: /* Reserved for future use */ ds@1952: case 0: { /* NULL */ ds@1952: pMem->flags = MEM_Null; ds@1952: break; ds@1952: } ds@1952: case 1: { /* 1-byte signed integer */ ds@1952: pMem->u.i = (signed char)buf[0]; ds@1952: pMem->flags = MEM_Int; ds@1952: return 1; ds@1952: } ds@1952: case 2: { /* 2-byte signed integer */ ds@1952: pMem->u.i = (((signed char)buf[0])<<8) | buf[1]; ds@1952: pMem->flags = MEM_Int; ds@1952: return 2; ds@1952: } ds@1952: case 3: { /* 3-byte signed integer */ ds@1952: pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2]; ds@1952: pMem->flags = MEM_Int; ds@1952: return 3; ds@1952: } ds@1952: case 4: { /* 4-byte signed integer */ ds@1952: pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; ds@1952: pMem->flags = MEM_Int; ds@1952: return 4; ds@1952: } ds@1952: case 5: { /* 6-byte signed integer */ ds@1952: u64 x = (((signed char)buf[0])<<8) | buf[1]; ds@1952: u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; ds@1952: x = (x<<32) | y; ds@1952: pMem->u.i = *(i64*)&x; ds@1952: pMem->flags = MEM_Int; ds@1952: return 6; ds@1952: } ds@1952: case 6: /* 8-byte signed integer */ ds@1952: case 7: { /* IEEE floating point */ ds@1952: u64 x; ds@1952: u32 y; ds@1952: #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) ds@1952: /* Verify that integers and floating point values use the same ds@1952: ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is ds@1952: ** defined that 64-bit floating point values really are mixed ds@1952: ** endian. ds@1952: */ ds@1952: static const u64 t1 = ((u64)0x3ff00000)<<32; ds@1952: static const double r1 = 1.0; ds@1952: u64 t2 = t1; ds@1952: swapMixedEndianFloat(t2); ds@1952: assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); ds@1952: #endif ds@1952: ds@1952: x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; ds@1952: y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; ds@1952: x = (x<<32) | y; ds@1952: if( serial_type==6 ){ ds@1952: pMem->u.i = *(i64*)&x; ds@1952: pMem->flags = MEM_Int; ds@1952: }else{ ds@1952: assert( sizeof(x)==8 && sizeof(pMem->r)==8 ); ds@1952: swapMixedEndianFloat(x); ds@1952: memcpy(&pMem->r, &x, sizeof(x)); ds@1952: pMem->flags = MEM_Real; ds@1952: } ds@1952: return 8; ds@1952: } ds@1952: case 8: /* Integer 0 */ ds@1952: case 9: { /* Integer 1 */ ds@1952: pMem->u.i = serial_type-8; ds@1952: pMem->flags = MEM_Int; ds@1952: return 0; ds@1952: } ds@1952: default: { ds@1952: int len = (serial_type-12)/2; ds@1952: pMem->z = (char *)buf; ds@1952: pMem->n = len; ds@1952: pMem->xDel = 0; ds@1952: if( serial_type&0x01 ){ ds@1952: pMem->flags = MEM_Str | MEM_Ephem; ds@1952: }else{ ds@1952: pMem->flags = MEM_Blob | MEM_Ephem; ds@1952: } ds@1952: return len; ds@1952: } ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The header of a record consists of a sequence variable-length integers. ds@1952: ** These integers are almost always small and are encoded as a single byte. ds@1952: ** The following macro takes advantage this fact to provide a fast decode ds@1952: ** of the integers in a record header. It is faster for the common case ds@1952: ** where the integer is a single byte. It is a little slower when the ds@1952: ** integer is two or more bytes. But overall it is faster. ds@1952: ** ds@1952: ** The following expressions are equivalent: ds@1952: ** ds@1952: ** x = sqlite3GetVarint32( A, &B ); ds@1952: ** ds@1952: ** x = GetVarint( A, B ); ds@1952: ** ds@1952: */ ds@1952: #define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B)) ds@1952: ds@1952: /* ds@1952: ** This function compares the two table rows or index records specified by ds@1952: ** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero ds@1952: ** or positive integer if {nKey1, pKey1} is less than, equal to or ds@1952: ** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings ds@1952: ** composed by the OP_MakeRecord opcode of the VDBE. ds@1952: */ ds@1952: int sqlite3VdbeRecordCompare( ds@1952: void *userData, ds@1952: int nKey1, const void *pKey1, ds@1952: int nKey2, const void *pKey2 ds@1952: ){ ds@1952: KeyInfo *pKeyInfo = (KeyInfo*)userData; ds@1952: u32 d1, d2; /* Offset into aKey[] of next data element */ ds@1952: u32 idx1, idx2; /* Offset into aKey[] of next header element */ ds@1952: u32 szHdr1, szHdr2; /* Number of bytes in header */ ds@1952: int i = 0; ds@1952: int nField; ds@1952: int rc = 0; ds@1952: const unsigned char *aKey1 = (const unsigned char *)pKey1; ds@1952: const unsigned char *aKey2 = (const unsigned char *)pKey2; ds@1952: ds@1952: Mem mem1; ds@1952: Mem mem2; ds@1952: mem1.enc = pKeyInfo->enc; ds@1952: mem1.db = pKeyInfo->db; ds@1952: mem2.enc = pKeyInfo->enc; ds@1952: mem2.db = pKeyInfo->db; ds@1952: ds@1952: idx1 = GetVarint(aKey1, szHdr1); ds@1952: d1 = szHdr1; ds@1952: idx2 = GetVarint(aKey2, szHdr2); ds@1952: d2 = szHdr2; ds@1952: nField = pKeyInfo->nField; ds@1952: while( idx1=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break; ds@1952: idx2 += GetVarint( aKey2+idx2, serial_type2 ); ds@1952: if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break; ds@1952: ds@1952: /* Extract the values to be compared. ds@1952: */ ds@1952: d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); ds@1952: d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2); ds@1952: ds@1952: /* Do the comparison ds@1952: */ ds@1952: rc = sqlite3MemCompare(&mem1, &mem2, iaColl[i] : 0); ds@1952: if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1); ds@1952: if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2); ds@1952: if( rc!=0 ){ ds@1952: break; ds@1952: } ds@1952: i++; ds@1952: } ds@1952: ds@1952: /* One of the keys ran out of fields, but all the fields up to that point ds@1952: ** were equal. If the incrKey flag is true, then the second key is ds@1952: ** treated as larger. ds@1952: */ ds@1952: if( rc==0 ){ ds@1952: if( pKeyInfo->incrKey ){ ds@1952: rc = -1; ds@1952: }else if( d1aSortOrder && inField ds@1952: && pKeyInfo->aSortOrder[i] ){ ds@1952: rc = -rc; ds@1952: } ds@1952: ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The argument is an index entry composed using the OP_MakeRecord opcode. ds@1952: ** The last entry in this record should be an integer (specifically ds@1952: ** an integer rowid). This routine returns the number of bytes in ds@1952: ** that integer. ds@1952: */ ds@1952: int sqlite3VdbeIdxRowidLen(const u8 *aKey){ ds@1952: u32 szHdr; /* Size of the header */ ds@1952: u32 typeRowid; /* Serial type of the rowid */ ds@1952: ds@1952: sqlite3GetVarint32(aKey, &szHdr); ds@1952: sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid); ds@1952: return sqlite3VdbeSerialTypeLen(typeRowid); ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** pCur points at an index entry created using the OP_MakeRecord opcode. ds@1952: ** Read the rowid (the last field in the record) and store it in *rowid. ds@1952: ** Return SQLITE_OK if everything works, or an error code otherwise. ds@1952: */ ds@1952: int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){ ds@1952: i64 nCellKey = 0; ds@1952: int rc; ds@1952: u32 szHdr; /* Size of the header */ ds@1952: u32 typeRowid; /* Serial type of the rowid */ ds@1952: u32 lenRowid; /* Size of the rowid */ ds@1952: Mem m, v; ds@1952: ds@1952: sqlite3BtreeKeySize(pCur, &nCellKey); ds@1952: if( nCellKey<=0 ){ ds@1952: return SQLITE_CORRUPT_BKPT; ds@1952: } ds@1952: rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m); ds@1952: if( rc ){ ds@1952: return rc; ds@1952: } ds@1952: sqlite3GetVarint32((u8*)m.z, &szHdr); ds@1952: sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid); ds@1952: lenRowid = sqlite3VdbeSerialTypeLen(typeRowid); ds@1952: sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); ds@1952: *rowid = v.u.i; ds@1952: sqlite3VdbeMemRelease(&m); ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Compare the key of the index entry that cursor pC is point to against ds@1952: ** the key string in pKey (of length nKey). Write into *pRes a number ds@1952: ** that is negative, zero, or positive if pC is less than, equal to, ds@1952: ** or greater than pKey. Return SQLITE_OK on success. ds@1952: ** ds@1952: ** pKey is either created without a rowid or is truncated so that it ds@1952: ** omits the rowid at the end. The rowid at the end of the index entry ds@1952: ** is ignored as well. ds@1952: */ ds@1952: int sqlite3VdbeIdxKeyCompare( ds@1952: Cursor *pC, /* The cursor to compare against */ ds@1952: int nKey, const u8 *pKey, /* The key to compare */ ds@1952: int *res /* Write the comparison result here */ ds@1952: ){ ds@1952: i64 nCellKey = 0; ds@1952: int rc; ds@1952: BtCursor *pCur = pC->pCursor; ds@1952: int lenRowid; ds@1952: Mem m; ds@1952: ds@1952: sqlite3BtreeKeySize(pCur, &nCellKey); ds@1952: if( nCellKey<=0 ){ ds@1952: *res = 0; ds@1952: return SQLITE_OK; ds@1952: } ds@1952: rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m); ds@1952: if( rc ){ ds@1952: return rc; ds@1952: } ds@1952: lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z); ds@1952: *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey); ds@1952: sqlite3VdbeMemRelease(&m); ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This routine sets the value to be returned by subsequent calls to ds@1952: ** sqlite3_changes() on the database handle 'db'. ds@1952: */ ds@1952: void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){ ds@1952: assert( sqlite3_mutex_held(db->mutex) ); ds@1952: db->nChange = nChange; ds@1952: db->nTotalChange += nChange; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Set a flag in the vdbe to update the change counter when it is finalised ds@1952: ** or reset. ds@1952: */ ds@1952: void sqlite3VdbeCountChanges(Vdbe *v){ ds@1952: v->changeCntOn = 1; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Mark every prepared statement associated with a database connection ds@1952: ** as expired. ds@1952: ** ds@1952: ** An expired statement means that recompilation of the statement is ds@1952: ** recommend. Statements expire when things happen that make their ds@1952: ** programs obsolete. Removing user-defined functions or collating ds@1952: ** sequences, or changing an authorization function are the types of ds@1952: ** things that make prepared statements obsolete. ds@1952: */ ds@1952: void sqlite3ExpirePreparedStatements(sqlite3 *db){ ds@1952: Vdbe *p; ds@1952: for(p = db->pVdbe; p; p=p->pNext){ ds@1952: p->expired = 1; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the database associated with the Vdbe. ds@1952: */ ds@1952: sqlite3 *sqlite3VdbeDb(Vdbe *v){ ds@1952: return v->db; ds@1952: }