Index: third_party/sqlite/src/src/vdbeaux.c |
diff --git a/third_party/sqlite/src/src/vdbeaux.c b/third_party/sqlite/src/src/vdbeaux.c |
index 4d4bb224f93d8ce16b7b653344164df9a026f624..c0018bb71cae5d486e5e94f90963d3ce509dc16e 100644 |
--- a/third_party/sqlite/src/src/vdbeaux.c |
+++ b/third_party/sqlite/src/src/vdbeaux.c |
@@ -10,29 +10,16 @@ |
** |
************************************************************************* |
** This file contains code used for creating, destroying, and populating |
-** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior |
-** to version 2.8.7, all this code was combined into the vdbe.c source file. |
-** But that file was getting too big so this subroutines were split out. |
+** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) |
*/ |
#include "sqliteInt.h" |
#include "vdbeInt.h" |
- |
- |
-/* |
-** When debugging the code generator in a symbolic debugger, one can |
-** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed |
-** as they are added to the instruction stream. |
-*/ |
-#ifdef SQLITE_DEBUG |
-int sqlite3VdbeAddopTrace = 0; |
-#endif |
- |
- |
/* |
** Create a new virtual database engine. |
*/ |
-Vdbe *sqlite3VdbeCreate(sqlite3 *db){ |
+Vdbe *sqlite3VdbeCreate(Parse *pParse){ |
+ sqlite3 *db = pParse->db; |
Vdbe *p; |
p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); |
if( p==0 ) return 0; |
@@ -44,6 +31,10 @@ Vdbe *sqlite3VdbeCreate(sqlite3 *db){ |
p->pPrev = 0; |
db->pVdbe = p; |
p->magic = VDBE_MAGIC_INIT; |
+ p->pParse = pParse; |
+ assert( pParse->aLabel==0 ); |
+ assert( pParse->nLabel==0 ); |
+ assert( pParse->nOpAlloc==0 ); |
return p; |
} |
@@ -53,7 +44,7 @@ Vdbe *sqlite3VdbeCreate(sqlite3 *db){ |
void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){ |
assert( isPrepareV2==1 || isPrepareV2==0 ); |
if( p==0 ) return; |
-#ifdef SQLITE_OMIT_TRACE |
+#if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG) |
if( !isPrepareV2 ) return; |
#endif |
assert( p->zSql==0 ); |
@@ -90,35 +81,55 @@ void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ |
pB->isPrepareV2 = pA->isPrepareV2; |
} |
-#ifdef SQLITE_DEBUG |
-/* |
-** Turn tracing on or off |
-*/ |
-void sqlite3VdbeTrace(Vdbe *p, FILE *trace){ |
- p->trace = trace; |
-} |
-#endif |
- |
/* |
-** Resize the Vdbe.aOp array so that it is at least one op larger than |
-** it was. |
+** Resize the Vdbe.aOp array so that it is at least nOp elements larger |
+** than its current size. nOp is guaranteed to be less than or equal |
+** to 1024/sizeof(Op). |
** |
** If an out-of-memory error occurs while resizing the array, return |
-** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain |
+** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain |
** unchanged (this is so that any opcodes already allocated can be |
** correctly deallocated along with the rest of the Vdbe). |
*/ |
-static int growOpArray(Vdbe *p){ |
+static int growOpArray(Vdbe *v, int nOp){ |
VdbeOp *pNew; |
+ Parse *p = v->pParse; |
+ |
+ /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force |
+ ** more frequent reallocs and hence provide more opportunities for |
+ ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used |
+ ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array |
+ ** by the minimum* amount required until the size reaches 512. Normal |
+ ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current |
+ ** size of the op array or add 1KB of space, whichever is smaller. */ |
+#ifdef SQLITE_TEST_REALLOC_STRESS |
+ int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp); |
+#else |
int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op))); |
- pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op)); |
+ UNUSED_PARAMETER(nOp); |
+#endif |
+ |
+ assert( nOp<=(1024/sizeof(Op)) ); |
+ assert( nNew>=(p->nOpAlloc+nOp) ); |
+ pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op)); |
if( pNew ){ |
p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op); |
- p->aOp = pNew; |
+ v->aOp = pNew; |
} |
return (pNew ? SQLITE_OK : SQLITE_NOMEM); |
} |
+#ifdef SQLITE_DEBUG |
+/* This routine is just a convenient place to set a breakpoint that will |
+** fire after each opcode is inserted and displayed using |
+** "PRAGMA vdbe_addoptrace=on". |
+*/ |
+static void test_addop_breakpoint(void){ |
+ static int n = 0; |
+ n++; |
+} |
+#endif |
+ |
/* |
** Add a new instruction to the list of instructions current in the |
** VDBE. Return the address of the new instruction. |
@@ -142,8 +153,8 @@ int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ |
i = p->nOp; |
assert( p->magic==VDBE_MAGIC_INIT ); |
assert( op>0 && op<0xff ); |
- if( p->nOpAlloc<=i ){ |
- if( growOpArray(p) ){ |
+ if( p->pParse->nOpAlloc<=i ){ |
+ if( growOpArray(p, 1) ){ |
return 1; |
} |
} |
@@ -156,21 +167,31 @@ int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ |
pOp->p3 = p3; |
pOp->p4.p = 0; |
pOp->p4type = P4_NOTUSED; |
- p->expired = 0; |
- if( op==OP_ParseSchema ){ |
- /* Any program that uses the OP_ParseSchema opcode needs to lock |
- ** all btrees. */ |
- int j; |
- for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j); |
- } |
-#ifdef SQLITE_DEBUG |
+#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
pOp->zComment = 0; |
- if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]); |
+#endif |
+#ifdef SQLITE_DEBUG |
+ if( p->db->flags & SQLITE_VdbeAddopTrace ){ |
+ int jj, kk; |
+ Parse *pParse = p->pParse; |
+ for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){ |
+ struct yColCache *x = pParse->aColCache + jj; |
+ if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue; |
+ printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn); |
+ kk++; |
+ } |
+ if( kk ) printf("\n"); |
+ sqlite3VdbePrintOp(0, i, &p->aOp[i]); |
+ test_addop_breakpoint(); |
+ } |
#endif |
#ifdef VDBE_PROFILE |
pOp->cycles = 0; |
pOp->cnt = 0; |
#endif |
+#ifdef SQLITE_VDBE_COVERAGE |
+ pOp->iSrcLine = 0; |
+#endif |
return i; |
} |
int sqlite3VdbeAddOp0(Vdbe *p, int op){ |
@@ -202,6 +223,21 @@ int sqlite3VdbeAddOp4( |
} |
/* |
+** Add an OP_ParseSchema opcode. This routine is broken out from |
+** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees |
+** as having been used. |
+** |
+** The zWhere string must have been obtained from sqlite3_malloc(). |
+** This routine will take ownership of the allocated memory. |
+*/ |
+void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){ |
+ int j; |
+ int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0); |
+ sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC); |
+ for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j); |
+} |
+ |
+/* |
** Add an opcode that includes the p4 value as an integer. |
*/ |
int sqlite3VdbeAddOp4Int( |
@@ -231,15 +267,13 @@ int sqlite3VdbeAddOp4Int( |
** |
** Zero is returned if a malloc() fails. |
*/ |
-int sqlite3VdbeMakeLabel(Vdbe *p){ |
- int i; |
- i = p->nLabel++; |
- assert( p->magic==VDBE_MAGIC_INIT ); |
- if( i>=p->nLabelAlloc ){ |
- int n = p->nLabelAlloc*2 + 5; |
- p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, |
- n*sizeof(p->aLabel[0])); |
- p->nLabelAlloc = sqlite3DbMallocSize(p->db, p->aLabel)/sizeof(p->aLabel[0]); |
+int sqlite3VdbeMakeLabel(Vdbe *v){ |
+ Parse *p = v->pParse; |
+ int i = p->nLabel++; |
+ assert( v->magic==VDBE_MAGIC_INIT ); |
+ if( (i & (i-1))==0 ){ |
+ p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, |
+ (i*2+1)*sizeof(p->aLabel[0])); |
} |
if( p->aLabel ){ |
p->aLabel[i] = -1; |
@@ -252,13 +286,15 @@ int sqlite3VdbeMakeLabel(Vdbe *p){ |
** be inserted. The parameter "x" must have been obtained from |
** a prior call to sqlite3VdbeMakeLabel(). |
*/ |
-void sqlite3VdbeResolveLabel(Vdbe *p, int x){ |
+void sqlite3VdbeResolveLabel(Vdbe *v, int x){ |
+ Parse *p = v->pParse; |
int j = -1-x; |
- assert( p->magic==VDBE_MAGIC_INIT ); |
- assert( j>=0 && j<p->nLabel ); |
- if( p->aLabel ){ |
- p->aLabel[j] = p->nOp; |
+ assert( v->magic==VDBE_MAGIC_INIT ); |
+ assert( j<p->nLabel ); |
+ if( ALWAYS(j>=0) && p->aLabel ){ |
+ p->aLabel[j] = v->nOp; |
} |
+ p->iFixedOp = v->nOp - 1; |
} |
/* |
@@ -372,7 +408,7 @@ int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){ |
|| (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1) |
#endif |
|| ((opcode==OP_Halt || opcode==OP_HaltIfNull) |
- && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort)) |
+ && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort)) |
){ |
hasAbort = 1; |
break; |
@@ -380,7 +416,7 @@ int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){ |
} |
sqlite3DbFree(v->db, sIter.apSub); |
- /* Return true if hasAbort==mayAbort. Or if a malloc failure occured. |
+ /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred. |
** If malloc failed, then the while() loop above may not have iterated |
** through all opcodes and hasAbort may be set incorrectly. Return |
** true for this case to prevent the assert() in the callers frame |
@@ -406,37 +442,79 @@ static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ |
int i; |
int nMaxArgs = *pMaxFuncArgs; |
Op *pOp; |
- int *aLabel = p->aLabel; |
+ Parse *pParse = p->pParse; |
+ int *aLabel = pParse->aLabel; |
p->readOnly = 1; |
+ p->bIsReader = 0; |
for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ |
u8 opcode = pOp->opcode; |
- pOp->opflags = sqlite3OpcodeProperty[opcode]; |
- if( opcode==OP_Function || opcode==OP_AggStep ){ |
- if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5; |
- }else if( (opcode==OP_Transaction && pOp->p2!=0) || opcode==OP_Vacuum ){ |
- p->readOnly = 0; |
+ /* NOTE: Be sure to update mkopcodeh.awk when adding or removing |
+ ** cases from this switch! */ |
+ switch( opcode ){ |
+ case OP_Function: |
+ case OP_AggStep: { |
+ if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5; |
+ break; |
+ } |
+ case OP_Transaction: { |
+ if( pOp->p2!=0 ) p->readOnly = 0; |
+ /* fall thru */ |
+ } |
+ case OP_AutoCommit: |
+ case OP_Savepoint: { |
+ p->bIsReader = 1; |
+ break; |
+ } |
+#ifndef SQLITE_OMIT_WAL |
+ case OP_Checkpoint: |
+#endif |
+ case OP_Vacuum: |
+ case OP_JournalMode: { |
+ p->readOnly = 0; |
+ p->bIsReader = 1; |
+ break; |
+ } |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
- }else if( opcode==OP_VUpdate ){ |
- if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; |
- }else if( opcode==OP_VFilter ){ |
- int n; |
- assert( p->nOp - i >= 3 ); |
- assert( pOp[-1].opcode==OP_Integer ); |
- n = pOp[-1].p1; |
- if( n>nMaxArgs ) nMaxArgs = n; |
+ case OP_VUpdate: { |
+ if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; |
+ break; |
+ } |
+ case OP_VFilter: { |
+ int n; |
+ assert( p->nOp - i >= 3 ); |
+ assert( pOp[-1].opcode==OP_Integer ); |
+ n = pOp[-1].p1; |
+ if( n>nMaxArgs ) nMaxArgs = n; |
+ break; |
+ } |
#endif |
+ case OP_Next: |
+ case OP_NextIfOpen: |
+ case OP_SorterNext: { |
+ pOp->p4.xAdvance = sqlite3BtreeNext; |
+ pOp->p4type = P4_ADVANCE; |
+ break; |
+ } |
+ case OP_Prev: |
+ case OP_PrevIfOpen: { |
+ pOp->p4.xAdvance = sqlite3BtreePrevious; |
+ pOp->p4type = P4_ADVANCE; |
+ break; |
+ } |
} |
+ pOp->opflags = sqlite3OpcodeProperty[opcode]; |
if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){ |
- assert( -1-pOp->p2<p->nLabel ); |
+ assert( -1-pOp->p2<pParse->nLabel ); |
pOp->p2 = aLabel[-1-pOp->p2]; |
} |
} |
- sqlite3DbFree(p->db, p->aLabel); |
- p->aLabel = 0; |
- |
+ sqlite3DbFree(p->db, pParse->aLabel); |
+ pParse->aLabel = 0; |
+ pParse->nLabel = 0; |
*pMaxFuncArgs = nMaxArgs; |
+ assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) ); |
} |
/* |
@@ -463,7 +541,7 @@ VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){ |
assert( aOp && !p->db->mallocFailed ); |
/* Check that sqlite3VdbeUsesBtree() was not called on this VM */ |
- assert( p->btreeMask==0 ); |
+ assert( DbMaskAllZero(p->btreeMask) ); |
resolveP2Values(p, pnMaxArg); |
*pnOp = p->nOp; |
@@ -475,10 +553,10 @@ VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){ |
** Add a whole list of operations to the operation stack. Return the |
** address of the first operation added. |
*/ |
-int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ |
+int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){ |
int addr; |
assert( p->magic==VDBE_MAGIC_INIT ); |
- if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){ |
+ if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){ |
return 0; |
} |
addr = p->nOp; |
@@ -490,7 +568,8 @@ int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ |
VdbeOp *pOut = &p->aOp[i+addr]; |
pOut->opcode = pIn->opcode; |
pOut->p1 = pIn->p1; |
- if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){ |
+ if( p2<0 ){ |
+ assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP ); |
pOut->p2 = addr + ADDR(p2); |
}else{ |
pOut->p2 = p2; |
@@ -499,9 +578,16 @@ int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ |
pOut->p4type = P4_NOTUSED; |
pOut->p4.p = 0; |
pOut->p5 = 0; |
-#ifdef SQLITE_DEBUG |
+#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
pOut->zComment = 0; |
- if( sqlite3VdbeAddopTrace ){ |
+#endif |
+#ifdef SQLITE_VDBE_COVERAGE |
+ pOut->iSrcLine = iLineno+i; |
+#else |
+ (void)iLineno; |
+#endif |
+#ifdef SQLITE_DEBUG |
+ if( p->db->flags & SQLITE_VdbeAddopTrace ){ |
sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); |
} |
#endif |
@@ -517,10 +603,9 @@ int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ |
** static array using sqlite3VdbeAddOpList but we want to make a |
** few minor changes to the program. |
*/ |
-void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ |
+void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){ |
assert( p!=0 ); |
- assert( addr>=0 ); |
- if( p->nOp>addr ){ |
+ if( ((u32)p->nOp)>addr ){ |
p->aOp[addr].p1 = val; |
} |
} |
@@ -529,10 +614,9 @@ void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ |
** Change the value of the P2 operand for a specific instruction. |
** This routine is useful for setting a jump destination. |
*/ |
-void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ |
+void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){ |
assert( p!=0 ); |
- assert( addr>=0 ); |
- if( p->nOp>addr ){ |
+ if( ((u32)p->nOp)>addr ){ |
p->aOp[addr].p2 = val; |
} |
} |
@@ -540,10 +624,9 @@ void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ |
/* |
** Change the value of the P3 operand for a specific instruction. |
*/ |
-void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){ |
+void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){ |
assert( p!=0 ); |
- assert( addr>=0 ); |
- if( p->nOp>addr ){ |
+ if( ((u32)p->nOp)>addr ){ |
p->aOp[addr].p3 = val; |
} |
} |
@@ -565,8 +648,8 @@ void sqlite3VdbeChangeP5(Vdbe *p, u8 val){ |
** the address of the next instruction to be coded. |
*/ |
void sqlite3VdbeJumpHere(Vdbe *p, int addr){ |
- assert( addr>=0 ); |
sqlite3VdbeChangeP2(p, addr, p->nOp); |
+ p->pParse->iFixedOp = p->nOp - 1; |
} |
@@ -575,7 +658,7 @@ void sqlite3VdbeJumpHere(Vdbe *p, int addr){ |
** the FuncDef is not ephermal, then do nothing. |
*/ |
static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ |
- if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){ |
+ if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){ |
sqlite3DbFree(db, pDef); |
} |
} |
@@ -592,21 +675,16 @@ static void freeP4(sqlite3 *db, int p4type, void *p4){ |
case P4_REAL: |
case P4_INT64: |
case P4_DYNAMIC: |
- case P4_KEYINFO: |
- case P4_INTARRAY: |
- case P4_KEYINFO_HANDOFF: { |
+ case P4_INTARRAY: { |
sqlite3DbFree(db, p4); |
break; |
} |
- case P4_MPRINTF: { |
- if( db->pnBytesFreed==0 ) sqlite3_free(p4); |
+ case P4_KEYINFO: { |
+ if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4); |
break; |
} |
- case P4_VDBEFUNC: { |
- VdbeFunc *pVdbeFunc = (VdbeFunc *)p4; |
- freeEphemeralFunction(db, pVdbeFunc->pFunc); |
- if( db->pnBytesFreed==0 ) sqlite3VdbeDeleteAuxData(pVdbeFunc, 0); |
- sqlite3DbFree(db, pVdbeFunc); |
+ case P4_MPRINTF: { |
+ if( db->pnBytesFreed==0 ) sqlite3_free(p4); |
break; |
} |
case P4_FUNCDEF: { |
@@ -618,7 +696,7 @@ static void freeP4(sqlite3 *db, int p4type, void *p4){ |
sqlite3ValueFree((sqlite3_value*)p4); |
}else{ |
Mem *p = (Mem*)p4; |
- sqlite3DbFree(db, p->zMalloc); |
+ if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); |
sqlite3DbFree(db, p); |
} |
break; |
@@ -641,7 +719,7 @@ static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){ |
Op *pOp; |
for(pOp=aOp; pOp<&aOp[nOp]; pOp++){ |
freeP4(db, pOp->p4type, pOp->p4.p); |
-#ifdef SQLITE_DEBUG |
+#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
sqlite3DbFree(db, pOp->zComment); |
#endif |
} |
@@ -660,18 +738,29 @@ void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){ |
} |
/* |
-** Change N opcodes starting at addr to No-ops. |
+** Change the opcode at addr into OP_Noop |
*/ |
-void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ |
- if( p->aOp ){ |
+void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){ |
+ if( addr<p->nOp ){ |
VdbeOp *pOp = &p->aOp[addr]; |
sqlite3 *db = p->db; |
- while( N-- ){ |
- freeP4(db, pOp->p4type, pOp->p4.p); |
- memset(pOp, 0, sizeof(pOp[0])); |
- pOp->opcode = OP_Noop; |
- pOp++; |
- } |
+ freeP4(db, pOp->p4type, pOp->p4.p); |
+ memset(pOp, 0, sizeof(pOp[0])); |
+ pOp->opcode = OP_Noop; |
+ if( addr==p->nOp-1 ) p->nOp--; |
+ } |
+} |
+ |
+/* |
+** If the last opcode is "op" and it is not a jump destination, |
+** then remove it. Return true if and only if an opcode was removed. |
+*/ |
+int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){ |
+ if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){ |
+ sqlite3VdbeChangeToNoop(p, p->nOp-1); |
+ return 1; |
+ }else{ |
+ return 0; |
} |
} |
@@ -685,14 +774,6 @@ void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ |
** the string is made into memory obtained from sqlite3_malloc(). |
** A value of n==0 means copy bytes of zP4 up to and including the |
** first null byte. If n>0 then copy n+1 bytes of zP4. |
-** |
-** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure. |
-** A copy is made of the KeyInfo structure into memory obtained from |
-** sqlite3_malloc, to be freed when the Vdbe is finalized. |
-** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure |
-** stored in memory that the caller has obtained from sqlite3_malloc. The |
-** caller should not free the allocation, it will be freed when the Vdbe is |
-** finalized. |
** |
** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points |
** to a string or structure that is guaranteed to exist for the lifetime of |
@@ -707,7 +788,7 @@ void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ |
db = p->db; |
assert( p->magic==VDBE_MAGIC_INIT ); |
if( p->aOp==0 || db->mallocFailed ){ |
- if ( n!=P4_KEYINFO && n!=P4_VTAB ) { |
+ if( n!=P4_VTAB ){ |
freeP4(db, n, (void*)*(char**)&zP4); |
} |
return; |
@@ -718,6 +799,9 @@ void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ |
addr = p->nOp - 1; |
} |
pOp = &p->aOp[addr]; |
+ assert( pOp->p4type==P4_NOTUSED |
+ || pOp->p4type==P4_INT32 |
+ || pOp->p4type==P4_KEYINFO ); |
freeP4(db, pOp->p4type, pOp->p4.p); |
pOp->p4.p = 0; |
if( n==P4_INT32 ){ |
@@ -729,27 +813,6 @@ void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ |
pOp->p4.p = 0; |
pOp->p4type = P4_NOTUSED; |
}else if( n==P4_KEYINFO ){ |
- KeyInfo *pKeyInfo; |
- int nField, nByte; |
- |
- nField = ((KeyInfo*)zP4)->nField; |
- nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; |
- pKeyInfo = sqlite3DbMallocRaw(0, nByte); |
- pOp->p4.pKeyInfo = pKeyInfo; |
- if( pKeyInfo ){ |
- u8 *aSortOrder; |
- memcpy((char*)pKeyInfo, zP4, nByte - nField); |
- aSortOrder = pKeyInfo->aSortOrder; |
- if( aSortOrder ){ |
- pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField]; |
- memcpy(pKeyInfo->aSortOrder, aSortOrder, nField); |
- } |
- pOp->p4type = P4_KEYINFO; |
- }else{ |
- p->db->mallocFailed = 1; |
- pOp->p4type = P4_NOTUSED; |
- } |
- }else if( n==P4_KEYINFO_HANDOFF ){ |
pOp->p4.p = (void*)zP4; |
pOp->p4type = P4_KEYINFO; |
}else if( n==P4_VTAB ){ |
@@ -767,42 +830,62 @@ void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ |
} |
} |
-#ifndef NDEBUG |
/* |
-** Change the comment on the the most recently coded instruction. Or |
+** Set the P4 on the most recently added opcode to the KeyInfo for the |
+** index given. |
+*/ |
+void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){ |
+ Vdbe *v = pParse->pVdbe; |
+ assert( v!=0 ); |
+ assert( pIdx!=0 ); |
+ sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx), |
+ P4_KEYINFO); |
+} |
+ |
+#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
+/* |
+** Change the comment on the most recently coded instruction. Or |
** insert a No-op and add the comment to that new instruction. This |
** makes the code easier to read during debugging. None of this happens |
** in a production build. |
*/ |
-void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ |
- va_list ap; |
- if( !p ) return; |
+static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){ |
assert( p->nOp>0 || p->aOp==0 ); |
assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); |
if( p->nOp ){ |
- char **pz = &p->aOp[p->nOp-1].zComment; |
+ assert( p->aOp ); |
+ sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment); |
+ p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap); |
+ } |
+} |
+void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ |
+ va_list ap; |
+ if( p ){ |
va_start(ap, zFormat); |
- sqlite3DbFree(p->db, *pz); |
- *pz = sqlite3VMPrintf(p->db, zFormat, ap); |
+ vdbeVComment(p, zFormat, ap); |
va_end(ap); |
} |
} |
void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ |
va_list ap; |
- if( !p ) return; |
- sqlite3VdbeAddOp0(p, OP_Noop); |
- assert( p->nOp>0 || p->aOp==0 ); |
- assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); |
- if( p->nOp ){ |
- char **pz = &p->aOp[p->nOp-1].zComment; |
+ if( p ){ |
+ sqlite3VdbeAddOp0(p, OP_Noop); |
va_start(ap, zFormat); |
- sqlite3DbFree(p->db, *pz); |
- *pz = sqlite3VMPrintf(p->db, zFormat, ap); |
+ vdbeVComment(p, zFormat, ap); |
va_end(ap); |
} |
} |
#endif /* NDEBUG */ |
+#ifdef SQLITE_VDBE_COVERAGE |
+/* |
+** Set the value if the iSrcLine field for the previously coded instruction. |
+*/ |
+void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){ |
+ sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine; |
+} |
+#endif /* SQLITE_VDBE_COVERAGE */ |
+ |
/* |
** Return the opcode for a given address. If the address is -1, then |
** return the most recently inserted opcode. |
@@ -811,28 +894,17 @@ void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ |
** routine, then a pointer to a dummy VdbeOp will be returned. That opcode |
** is readable but not writable, though it is cast to a writable value. |
** The return of a dummy opcode allows the call to continue functioning |
-** after a OOM fault without having to check to see if the return from |
+** after an OOM fault without having to check to see if the return from |
** this routine is a valid pointer. But because the dummy.opcode is 0, |
** dummy will never be written to. This is verified by code inspection and |
** by running with Valgrind. |
-** |
-** About the #ifdef SQLITE_OMIT_TRACE: Normally, this routine is never called |
-** unless p->nOp>0. This is because in the absense of SQLITE_OMIT_TRACE, |
-** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as |
-** a new VDBE is created. So we are free to set addr to p->nOp-1 without |
-** having to double-check to make sure that the result is non-negative. But |
-** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to |
-** check the value of p->nOp-1 before continuing. |
*/ |
VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ |
/* C89 specifies that the constant "dummy" will be initialized to all |
** zeros, which is correct. MSVC generates a warning, nevertheless. */ |
- static const VdbeOp dummy; /* Ignore the MSVC warning about no initializer */ |
+ static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */ |
assert( p->magic==VDBE_MAGIC_INIT ); |
if( addr<0 ){ |
-#ifdef SQLITE_OMIT_TRACE |
- if( p->nOp==0 ) return (VdbeOp*)&dummy; |
-#endif |
addr = p->nOp - 1; |
} |
assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); |
@@ -843,6 +915,97 @@ VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ |
} |
} |
+#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) |
+/* |
+** Return an integer value for one of the parameters to the opcode pOp |
+** determined by character c. |
+*/ |
+static int translateP(char c, const Op *pOp){ |
+ if( c=='1' ) return pOp->p1; |
+ if( c=='2' ) return pOp->p2; |
+ if( c=='3' ) return pOp->p3; |
+ if( c=='4' ) return pOp->p4.i; |
+ return pOp->p5; |
+} |
+ |
+/* |
+** Compute a string for the "comment" field of a VDBE opcode listing. |
+** |
+** The Synopsis: field in comments in the vdbe.c source file gets converted |
+** to an extra string that is appended to the sqlite3OpcodeName(). In the |
+** absence of other comments, this synopsis becomes the comment on the opcode. |
+** Some translation occurs: |
+** |
+** "PX" -> "r[X]" |
+** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1 |
+** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0 |
+** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x |
+*/ |
+static int displayComment( |
+ const Op *pOp, /* The opcode to be commented */ |
+ const char *zP4, /* Previously obtained value for P4 */ |
+ char *zTemp, /* Write result here */ |
+ int nTemp /* Space available in zTemp[] */ |
+){ |
+ const char *zOpName; |
+ const char *zSynopsis; |
+ int nOpName; |
+ int ii, jj; |
+ zOpName = sqlite3OpcodeName(pOp->opcode); |
+ nOpName = sqlite3Strlen30(zOpName); |
+ if( zOpName[nOpName+1] ){ |
+ int seenCom = 0; |
+ char c; |
+ zSynopsis = zOpName += nOpName + 1; |
+ for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){ |
+ if( c=='P' ){ |
+ c = zSynopsis[++ii]; |
+ if( c=='4' ){ |
+ sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4); |
+ }else if( c=='X' ){ |
+ sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment); |
+ seenCom = 1; |
+ }else{ |
+ int v1 = translateP(c, pOp); |
+ int v2; |
+ sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1); |
+ if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){ |
+ ii += 3; |
+ jj += sqlite3Strlen30(zTemp+jj); |
+ v2 = translateP(zSynopsis[ii], pOp); |
+ if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){ |
+ ii += 2; |
+ v2++; |
+ } |
+ if( v2>1 ){ |
+ sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1); |
+ } |
+ }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){ |
+ ii += 4; |
+ } |
+ } |
+ jj += sqlite3Strlen30(zTemp+jj); |
+ }else{ |
+ zTemp[jj++] = c; |
+ } |
+ } |
+ if( !seenCom && jj<nTemp-5 && pOp->zComment ){ |
+ sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment); |
+ jj += sqlite3Strlen30(zTemp+jj); |
+ } |
+ if( jj<nTemp ) zTemp[jj] = 0; |
+ }else if( pOp->zComment ){ |
+ sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment); |
+ jj = sqlite3Strlen30(zTemp); |
+ }else{ |
+ zTemp[0] = 0; |
+ jj = 0; |
+ } |
+ return jj; |
+} |
+#endif /* SQLITE_DEBUG */ |
+ |
+ |
#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ |
|| defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
/* |
@@ -853,30 +1016,30 @@ static char *displayP4(Op *pOp, char *zTemp, int nTemp){ |
char *zP4 = zTemp; |
assert( nTemp>=20 ); |
switch( pOp->p4type ){ |
- case P4_KEYINFO_STATIC: |
case P4_KEYINFO: { |
int i, j; |
KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; |
- sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField); |
+ assert( pKeyInfo->aSortOrder!=0 ); |
+ sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField); |
i = sqlite3Strlen30(zTemp); |
for(j=0; j<pKeyInfo->nField; j++){ |
CollSeq *pColl = pKeyInfo->aColl[j]; |
- if( pColl ){ |
- int n = sqlite3Strlen30(pColl->zName); |
- if( i+n>nTemp-6 ){ |
- memcpy(&zTemp[i],",...",4); |
- break; |
- } |
- zTemp[i++] = ','; |
- if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){ |
- zTemp[i++] = '-'; |
- } |
- memcpy(&zTemp[i], pColl->zName,n+1); |
- i += n; |
- }else if( i+4<nTemp-6 ){ |
- memcpy(&zTemp[i],",nil",4); |
- i += 4; |
+ const char *zColl = pColl ? pColl->zName : "nil"; |
+ int n = sqlite3Strlen30(zColl); |
+ if( n==6 && memcmp(zColl,"BINARY",6)==0 ){ |
+ zColl = "B"; |
+ n = 1; |
} |
+ if( i+n>nTemp-6 ){ |
+ memcpy(&zTemp[i],",...",4); |
+ break; |
+ } |
+ zTemp[i++] = ','; |
+ if( pKeyInfo->aSortOrder[j] ){ |
+ zTemp[i++] = '-'; |
+ } |
+ memcpy(&zTemp[i], zColl, n+1); |
+ i += n; |
} |
zTemp[i++] = ')'; |
zTemp[i] = 0; |
@@ -885,7 +1048,7 @@ static char *displayP4(Op *pOp, char *zTemp, int nTemp){ |
} |
case P4_COLLSEQ: { |
CollSeq *pColl = pOp->p4.pColl; |
- sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName); |
+ sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName); |
break; |
} |
case P4_FUNCDEF: { |
@@ -907,13 +1070,14 @@ static char *displayP4(Op *pOp, char *zTemp, int nTemp){ |
} |
case P4_MEM: { |
Mem *pMem = pOp->p4.pMem; |
- assert( (pMem->flags & MEM_Null)==0 ); |
if( pMem->flags & MEM_Str ){ |
zP4 = pMem->z; |
}else if( pMem->flags & MEM_Int ){ |
sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i); |
}else if( pMem->flags & MEM_Real ){ |
- sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r); |
+ sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->u.r); |
+ }else if( pMem->flags & MEM_Null ){ |
+ sqlite3_snprintf(nTemp, zTemp, "NULL"); |
}else{ |
assert( pMem->flags & MEM_Blob ); |
zP4 = "(blob)"; |
@@ -935,6 +1099,10 @@ static char *displayP4(Op *pOp, char *zTemp, int nTemp){ |
sqlite3_snprintf(nTemp, zTemp, "program"); |
break; |
} |
+ case P4_ADVANCE: { |
+ zTemp[0] = 0; |
+ break; |
+ } |
default: { |
zP4 = pOp->p4.z; |
if( zP4==0 ){ |
@@ -952,15 +1120,16 @@ static char *displayP4(Op *pOp, char *zTemp, int nTemp){ |
** Declare to the Vdbe that the BTree object at db->aDb[i] is used. |
** |
** The prepared statements need to know in advance the complete set of |
-** attached databases that they will be using. A mask of these databases |
-** is maintained in p->btreeMask and is used for locking and other purposes. |
+** attached databases that will be use. A mask of these databases |
+** is maintained in p->btreeMask. The p->lockMask value is the subset of |
+** p->btreeMask of databases that will require a lock. |
*/ |
void sqlite3VdbeUsesBtree(Vdbe *p, int i){ |
assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 ); |
assert( i<(int)sizeof(p->btreeMask)*8 ); |
- p->btreeMask |= ((yDbMask)1)<<i; |
+ DbMaskSet(p->btreeMask, i); |
if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){ |
- p->lockMask |= ((yDbMask)1)<<i; |
+ DbMaskSet(p->lockMask, i); |
} |
} |
@@ -988,16 +1157,15 @@ void sqlite3VdbeUsesBtree(Vdbe *p, int i){ |
*/ |
void sqlite3VdbeEnter(Vdbe *p){ |
int i; |
- yDbMask mask; |
sqlite3 *db; |
Db *aDb; |
int nDb; |
- if( p->lockMask==0 ) return; /* The common case */ |
+ if( DbMaskAllZero(p->lockMask) ) return; /* The common case */ |
db = p->db; |
aDb = db->aDb; |
nDb = db->nDb; |
- for(i=0, mask=1; i<nDb; i++, mask += mask){ |
- if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){ |
+ for(i=0; i<nDb; i++){ |
+ if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){ |
sqlite3BtreeEnter(aDb[i].pBt); |
} |
} |
@@ -1010,16 +1178,15 @@ void sqlite3VdbeEnter(Vdbe *p){ |
*/ |
void sqlite3VdbeLeave(Vdbe *p){ |
int i; |
- yDbMask mask; |
sqlite3 *db; |
Db *aDb; |
int nDb; |
- if( p->lockMask==0 ) return; /* The common case */ |
+ if( DbMaskAllZero(p->lockMask) ) return; /* The common case */ |
db = p->db; |
aDb = db->aDb; |
nDb = db->nDb; |
- for(i=0, mask=1; i<nDb; i++, mask += mask){ |
- if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){ |
+ for(i=0; i<nDb; i++){ |
+ if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){ |
sqlite3BtreeLeave(aDb[i].pBt); |
} |
} |
@@ -1033,16 +1200,21 @@ void sqlite3VdbeLeave(Vdbe *p){ |
void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ |
char *zP4; |
char zPtr[50]; |
- static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n"; |
+ char zCom[100]; |
+ static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n"; |
if( pOut==0 ) pOut = stdout; |
zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); |
- fprintf(pOut, zFormat1, pc, |
- sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, |
-#ifdef SQLITE_DEBUG |
- pOp->zComment ? pOp->zComment : "" |
+#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
+ displayComment(pOp, zP4, zCom, sizeof(zCom)); |
#else |
- "" |
+ zCom[0] = 0; |
#endif |
+ /* NB: The sqlite3OpcodeName() function is implemented by code created |
+ ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the |
+ ** information from the vdbe.c source text */ |
+ fprintf(pOut, zFormat1, pc, |
+ sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, |
+ zCom |
); |
fflush(pOut); |
} |
@@ -1053,17 +1225,18 @@ void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ |
*/ |
static void releaseMemArray(Mem *p, int N){ |
if( p && N ){ |
- Mem *pEnd; |
+ Mem *pEnd = &p[N]; |
sqlite3 *db = p->db; |
u8 malloc_failed = db->mallocFailed; |
if( db->pnBytesFreed ){ |
- for(pEnd=&p[N]; p<pEnd; p++){ |
- sqlite3DbFree(db, p->zMalloc); |
- } |
+ do{ |
+ if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); |
+ }while( (++p)<pEnd ); |
return; |
} |
- for(pEnd=&p[N]; p<pEnd; p++){ |
+ do{ |
assert( (&p[1])==pEnd || p[0].db==p[1].db ); |
+ assert( sqlite3VdbeCheckMemInvariants(p) ); |
/* This block is really an inlined version of sqlite3VdbeMemRelease() |
** that takes advantage of the fact that the memory cell value is |
@@ -1077,15 +1250,19 @@ static void releaseMemArray(Mem *p, int N){ |
** with no indexes using a single prepared INSERT statement, bind() |
** and reset(). Inserts are grouped into a transaction. |
*/ |
+ testcase( p->flags & MEM_Agg ); |
+ testcase( p->flags & MEM_Dyn ); |
+ testcase( p->flags & MEM_Frame ); |
+ testcase( p->flags & MEM_RowSet ); |
if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){ |
sqlite3VdbeMemRelease(p); |
- }else if( p->zMalloc ){ |
+ }else if( p->szMalloc ){ |
sqlite3DbFree(db, p->zMalloc); |
- p->zMalloc = 0; |
+ p->szMalloc = 0; |
} |
- p->flags = MEM_Null; |
- } |
+ p->flags = MEM_Undefined; |
+ }while( (++p)<pEnd ); |
db->mallocFailed = malloc_failed; |
} |
} |
@@ -1131,7 +1308,7 @@ int sqlite3VdbeList( |
sqlite3 *db = p->db; /* The database connection */ |
int i; /* Loop counter */ |
int rc = SQLITE_OK; /* Return code */ |
- Mem *pMem = p->pResultSet = &p->aMem[1]; /* First Mem of result set */ |
+ Mem *pMem = &p->aMem[1]; /* First Mem of result set */ |
assert( p->explain ); |
assert( p->magic==VDBE_MAGIC_RUN ); |
@@ -1142,6 +1319,7 @@ int sqlite3VdbeList( |
** sqlite3_column_text16(), causing a translation to UTF-16 encoding. |
*/ |
releaseMemArray(pMem, 8); |
+ p->pResultSet = 0; |
if( p->rc==SQLITE_NOMEM ){ |
/* This happens if a malloc() inside a call to sqlite3_column_text() or |
@@ -1187,7 +1365,7 @@ int sqlite3VdbeList( |
rc = SQLITE_ERROR; |
sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc)); |
}else{ |
- char *z; |
+ char *zP4; |
Op *pOp; |
if( i<p->nOp ){ |
/* The output line number is small enough that we are still in the |
@@ -1205,15 +1383,13 @@ int sqlite3VdbeList( |
} |
if( p->explain==1 ){ |
pMem->flags = MEM_Int; |
- pMem->type = SQLITE_INTEGER; |
pMem->u.i = i; /* Program counter */ |
pMem++; |
pMem->flags = MEM_Static|MEM_Str|MEM_Term; |
- pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ |
+ pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ |
assert( pMem->z!=0 ); |
pMem->n = sqlite3Strlen30(pMem->z); |
- pMem->type = SQLITE_TEXT; |
pMem->enc = SQLITE_UTF8; |
pMem++; |
@@ -1228,7 +1404,7 @@ int sqlite3VdbeList( |
for(j=0; j<nSub; j++){ |
if( apSub[j]==pOp->p4.pProgram ) break; |
} |
- if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, 1) ){ |
+ if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){ |
apSub = (SubProgram **)pSub->z; |
apSub[nSub++] = pOp->p4.pProgram; |
pSub->flags |= MEM_Blob; |
@@ -1239,63 +1415,57 @@ int sqlite3VdbeList( |
pMem->flags = MEM_Int; |
pMem->u.i = pOp->p1; /* P1 */ |
- pMem->type = SQLITE_INTEGER; |
pMem++; |
pMem->flags = MEM_Int; |
pMem->u.i = pOp->p2; /* P2 */ |
- pMem->type = SQLITE_INTEGER; |
pMem++; |
pMem->flags = MEM_Int; |
pMem->u.i = pOp->p3; /* P3 */ |
- pMem->type = SQLITE_INTEGER; |
pMem++; |
- if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */ |
+ if( sqlite3VdbeMemClearAndResize(pMem, 32) ){ /* P4 */ |
assert( p->db->mallocFailed ); |
return SQLITE_ERROR; |
} |
- pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; |
- z = displayP4(pOp, pMem->z, 32); |
- if( z!=pMem->z ){ |
- sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0); |
+ pMem->flags = MEM_Str|MEM_Term; |
+ zP4 = displayP4(pOp, pMem->z, 32); |
+ if( zP4!=pMem->z ){ |
+ sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0); |
}else{ |
assert( pMem->z!=0 ); |
pMem->n = sqlite3Strlen30(pMem->z); |
pMem->enc = SQLITE_UTF8; |
} |
- pMem->type = SQLITE_TEXT; |
pMem++; |
if( p->explain==1 ){ |
- if( sqlite3VdbeMemGrow(pMem, 4, 0) ){ |
+ if( sqlite3VdbeMemClearAndResize(pMem, 4) ){ |
assert( p->db->mallocFailed ); |
return SQLITE_ERROR; |
} |
- pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; |
+ pMem->flags = MEM_Str|MEM_Term; |
pMem->n = 2; |
sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ |
- pMem->type = SQLITE_TEXT; |
pMem->enc = SQLITE_UTF8; |
pMem++; |
-#ifdef SQLITE_DEBUG |
- if( pOp->zComment ){ |
- pMem->flags = MEM_Str|MEM_Term; |
- pMem->z = pOp->zComment; |
- pMem->n = sqlite3Strlen30(pMem->z); |
- pMem->enc = SQLITE_UTF8; |
- pMem->type = SQLITE_TEXT; |
- }else |
-#endif |
- { |
- pMem->flags = MEM_Null; /* Comment */ |
- pMem->type = SQLITE_NULL; |
+#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
+ if( sqlite3VdbeMemClearAndResize(pMem, 500) ){ |
+ assert( p->db->mallocFailed ); |
+ return SQLITE_ERROR; |
} |
+ pMem->flags = MEM_Str|MEM_Term; |
+ pMem->n = displayComment(pOp, zP4, pMem->z, 500); |
+ pMem->enc = SQLITE_UTF8; |
+#else |
+ pMem->flags = MEM_Null; /* Comment */ |
+#endif |
} |
p->nResColumn = 8 - 4*(p->explain-1); |
+ p->pResultSet = &p->aMem[1]; |
p->rc = SQLITE_OK; |
rc = SQLITE_ROW; |
} |
@@ -1308,15 +1478,17 @@ int sqlite3VdbeList( |
** Print the SQL that was used to generate a VDBE program. |
*/ |
void sqlite3VdbePrintSql(Vdbe *p){ |
- int nOp = p->nOp; |
- VdbeOp *pOp; |
- if( nOp<1 ) return; |
- pOp = &p->aOp[0]; |
- if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ |
- const char *z = pOp->p4.z; |
- while( sqlite3Isspace(*z) ) z++; |
- printf("SQL: [%s]\n", z); |
+ const char *z = 0; |
+ if( p->zSql ){ |
+ z = p->zSql; |
+ }else if( p->nOp>=1 ){ |
+ const VdbeOp *pOp = &p->aOp[0]; |
+ if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){ |
+ z = pOp->p4.z; |
+ while( sqlite3Isspace(*z) ) z++; |
+ } |
} |
+ if( z ) printf("SQL: [%s]\n", z); |
} |
#endif |
@@ -1330,7 +1502,7 @@ void sqlite3VdbeIOTraceSql(Vdbe *p){ |
if( sqlite3IoTrace==0 ) return; |
if( nOp<1 ) return; |
pOp = &p->aOp[0]; |
- if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ |
+ if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){ |
int i, j; |
char z[1000]; |
sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z); |
@@ -1391,34 +1563,13 @@ static void *allocSpace( |
} |
/* |
-** Prepare a virtual machine for execution. This involves things such |
-** as allocating stack space and initializing the program counter. |
-** After the VDBE has be prepped, it can be executed by one or more |
-** calls to sqlite3VdbeExec(). |
-** |
-** This is the only way to move a VDBE from VDBE_MAGIC_INIT to |
-** VDBE_MAGIC_RUN. |
-** |
-** This function may be called more than once on a single virtual machine. |
-** The first call is made while compiling the SQL statement. Subsequent |
-** calls are made as part of the process of resetting a statement to be |
-** re-executed (from a call to sqlite3_reset()). The nVar, nMem, nCursor |
-** and isExplain parameters are only passed correct values the first time |
-** the function is called. On subsequent calls, from sqlite3_reset(), nVar |
-** is passed -1 and nMem, nCursor and isExplain are all passed zero. |
+** Rewind the VDBE back to the beginning in preparation for |
+** running it. |
*/ |
-void sqlite3VdbeMakeReady( |
- Vdbe *p, /* The VDBE */ |
- int nVar, /* Number of '?' see in the SQL statement */ |
- int nMem, /* Number of memory cells to allocate */ |
- int nCursor, /* Number of cursors to allocate */ |
- int nArg, /* Maximum number of args in SubPrograms */ |
- int isExplain, /* True if the EXPLAIN keywords is present */ |
- int usesStmtJournal /* True to set Vdbe.usesStmtJournal */ |
-){ |
- int n; |
- sqlite3 *db = p->db; |
- |
+void sqlite3VdbeRewind(Vdbe *p){ |
+#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
+ int i; |
+#endif |
assert( p!=0 ); |
assert( p->magic==VDBE_MAGIC_INIT ); |
@@ -1429,6 +1580,75 @@ void sqlite3VdbeMakeReady( |
/* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */ |
p->magic = VDBE_MAGIC_RUN; |
+#ifdef SQLITE_DEBUG |
+ for(i=1; i<p->nMem; i++){ |
+ assert( p->aMem[i].db==p->db ); |
+ } |
+#endif |
+ p->pc = -1; |
+ p->rc = SQLITE_OK; |
+ p->errorAction = OE_Abort; |
+ p->magic = VDBE_MAGIC_RUN; |
+ p->nChange = 0; |
+ p->cacheCtr = 1; |
+ p->minWriteFileFormat = 255; |
+ p->iStatement = 0; |
+ p->nFkConstraint = 0; |
+#ifdef VDBE_PROFILE |
+ for(i=0; i<p->nOp; i++){ |
+ p->aOp[i].cnt = 0; |
+ p->aOp[i].cycles = 0; |
+ } |
+#endif |
+} |
+ |
+/* |
+** Prepare a virtual machine for execution for the first time after |
+** creating the virtual machine. This involves things such |
+** as allocating registers and initializing the program counter. |
+** After the VDBE has be prepped, it can be executed by one or more |
+** calls to sqlite3VdbeExec(). |
+** |
+** This function may be called exactly once on each virtual machine. |
+** After this routine is called the VM has been "packaged" and is ready |
+** to run. After this routine is called, further calls to |
+** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects |
+** the Vdbe from the Parse object that helped generate it so that the |
+** the Vdbe becomes an independent entity and the Parse object can be |
+** destroyed. |
+** |
+** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back |
+** to its initial state after it has been run. |
+*/ |
+void sqlite3VdbeMakeReady( |
+ Vdbe *p, /* The VDBE */ |
+ Parse *pParse /* Parsing context */ |
+){ |
+ sqlite3 *db; /* The database connection */ |
+ int nVar; /* Number of parameters */ |
+ int nMem; /* Number of VM memory registers */ |
+ int nCursor; /* Number of cursors required */ |
+ int nArg; /* Number of arguments in subprograms */ |
+ int nOnce; /* Number of OP_Once instructions */ |
+ int n; /* Loop counter */ |
+ u8 *zCsr; /* Memory available for allocation */ |
+ u8 *zEnd; /* First byte past allocated memory */ |
+ int nByte; /* How much extra memory is needed */ |
+ |
+ assert( p!=0 ); |
+ assert( p->nOp>0 ); |
+ assert( pParse!=0 ); |
+ assert( p->magic==VDBE_MAGIC_INIT ); |
+ assert( pParse==p->pParse ); |
+ db = p->db; |
+ assert( db->mallocFailed==0 ); |
+ nVar = pParse->nVar; |
+ nMem = pParse->nMem; |
+ nCursor = pParse->nTab; |
+ nArg = pParse->nMaxArg; |
+ nOnce = pParse->nOnce; |
+ if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */ |
+ |
/* For each cursor required, also allocate a memory cell. Memory |
** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by |
** the vdbe program. Instead they are used to allocate space for |
@@ -1441,91 +1661,71 @@ void sqlite3VdbeMakeReady( |
nMem += nCursor; |
/* Allocate space for memory registers, SQL variables, VDBE cursors and |
- ** an array to marshal SQL function arguments in. This is only done the |
- ** first time this function is called for a given VDBE, not when it is |
- ** being called from sqlite3_reset() to reset the virtual machine. |
+ ** an array to marshal SQL function arguments in. |
*/ |
- if( nVar>=0 && ALWAYS(db->mallocFailed==0) ){ |
- u8 *zCsr = (u8 *)&p->aOp[p->nOp]; /* Memory avaliable for alloation */ |
- u8 *zEnd = (u8 *)&p->aOp[p->nOpAlloc]; /* First byte past available mem */ |
- int nByte; /* How much extra memory needed */ |
- |
- resolveP2Values(p, &nArg); |
- p->usesStmtJournal = (u8)usesStmtJournal; |
- if( isExplain && nMem<10 ){ |
- nMem = 10; |
- } |
- memset(zCsr, 0, zEnd-zCsr); |
- zCsr += (zCsr - (u8*)0)&7; |
- assert( EIGHT_BYTE_ALIGNMENT(zCsr) ); |
- |
- /* Memory for registers, parameters, cursor, etc, is allocated in two |
- ** passes. On the first pass, we try to reuse unused space at the |
- ** end of the opcode array. If we are unable to satisfy all memory |
- ** requirements by reusing the opcode array tail, then the second |
- ** pass will fill in the rest using a fresh allocation. |
- ** |
- ** This two-pass approach that reuses as much memory as possible from |
- ** the leftover space at the end of the opcode array can significantly |
- ** reduce the amount of memory held by a prepared statement. |
- */ |
- do { |
- nByte = 0; |
- p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte); |
- p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte); |
- p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte); |
- p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte); |
- p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*), |
- &zCsr, zEnd, &nByte); |
- if( nByte ){ |
- p->pFree = sqlite3DbMallocZero(db, nByte); |
- } |
- zCsr = p->pFree; |
- zEnd = &zCsr[nByte]; |
- }while( nByte && !db->mallocFailed ); |
- |
- p->nCursor = (u16)nCursor; |
- if( p->aVar ){ |
- p->nVar = (ynVar)nVar; |
- for(n=0; n<nVar; n++){ |
- p->aVar[n].flags = MEM_Null; |
- p->aVar[n].db = db; |
- } |
- } |
- if( p->aMem ){ |
- p->aMem--; /* aMem[] goes from 1..nMem */ |
- p->nMem = nMem; /* not from 0..nMem-1 */ |
- for(n=1; n<=nMem; n++){ |
- p->aMem[n].flags = MEM_Null; |
- p->aMem[n].db = db; |
- } |
- } |
- } |
-#ifdef SQLITE_DEBUG |
- for(n=1; n<p->nMem; n++){ |
- assert( p->aMem[n].db==db ); |
+ zCsr = (u8*)&p->aOp[p->nOp]; /* Memory avaliable for allocation */ |
+ zEnd = (u8*)&p->aOp[pParse->nOpAlloc]; /* First byte past end of zCsr[] */ |
+ |
+ resolveP2Values(p, &nArg); |
+ p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort); |
+ if( pParse->explain && nMem<10 ){ |
+ nMem = 10; |
} |
-#endif |
+ memset(zCsr, 0, zEnd-zCsr); |
+ zCsr += (zCsr - (u8*)0)&7; |
+ assert( EIGHT_BYTE_ALIGNMENT(zCsr) ); |
+ p->expired = 0; |
- p->pc = -1; |
- p->rc = SQLITE_OK; |
- p->errorAction = OE_Abort; |
- p->explain |= isExplain; |
- p->magic = VDBE_MAGIC_RUN; |
- p->nChange = 0; |
- p->cacheCtr = 1; |
- p->minWriteFileFormat = 255; |
- p->iStatement = 0; |
- p->nFkConstraint = 0; |
-#ifdef VDBE_PROFILE |
- { |
- int i; |
- for(i=0; i<p->nOp; i++){ |
- p->aOp[i].cnt = 0; |
- p->aOp[i].cycles = 0; |
+ /* Memory for registers, parameters, cursor, etc, is allocated in two |
+ ** passes. On the first pass, we try to reuse unused space at the |
+ ** end of the opcode array. If we are unable to satisfy all memory |
+ ** requirements by reusing the opcode array tail, then the second |
+ ** pass will fill in the rest using a fresh allocation. |
+ ** |
+ ** This two-pass approach that reuses as much memory as possible from |
+ ** the leftover space at the end of the opcode array can significantly |
+ ** reduce the amount of memory held by a prepared statement. |
+ */ |
+ do { |
+ nByte = 0; |
+ p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte); |
+ p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte); |
+ p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte); |
+ p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte); |
+ p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*), |
+ &zCsr, zEnd, &nByte); |
+ p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte); |
+ if( nByte ){ |
+ p->pFree = sqlite3DbMallocZero(db, nByte); |
+ } |
+ zCsr = p->pFree; |
+ zEnd = &zCsr[nByte]; |
+ }while( nByte && !db->mallocFailed ); |
+ |
+ p->nCursor = nCursor; |
+ p->nOnceFlag = nOnce; |
+ if( p->aVar ){ |
+ p->nVar = (ynVar)nVar; |
+ for(n=0; n<nVar; n++){ |
+ p->aVar[n].flags = MEM_Null; |
+ p->aVar[n].db = db; |
+ } |
+ } |
+ if( p->azVar ){ |
+ p->nzVar = pParse->nzVar; |
+ memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0])); |
+ memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0])); |
+ } |
+ if( p->aMem ){ |
+ p->aMem--; /* aMem[] goes from 1..nMem */ |
+ p->nMem = nMem; /* not from 0..nMem-1 */ |
+ for(n=1; n<=nMem; n++){ |
+ p->aMem[n].flags = MEM_Undefined; |
+ p->aMem[n].db = db; |
} |
} |
-#endif |
+ p->explain = pParse->explain; |
+ sqlite3VdbeRewind(p); |
} |
/* |
@@ -1536,6 +1736,7 @@ void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ |
if( pCx==0 ){ |
return; |
} |
+ sqlite3VdbeSorterClose(p->db, pCx); |
if( pCx->pBt ){ |
sqlite3BtreeClose(pCx->pBt); |
/* The pCx->pCursor will be close automatically, if it exists, by |
@@ -1544,9 +1745,9 @@ void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ |
sqlite3BtreeCloseCursor(pCx->pCursor); |
} |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
- if( pCx->pVtabCursor ){ |
+ else if( pCx->pVtabCursor ){ |
sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; |
- const sqlite3_module *pModule = pCx->pModule; |
+ const sqlite3_module *pModule = pVtabCursor->pVtab->pModule; |
p->inVtabMethod = 1; |
pModule->xClose(pVtabCursor); |
p->inVtabMethod = 0; |
@@ -1561,6 +1762,8 @@ void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ |
*/ |
int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ |
Vdbe *v = pFrame->v; |
+ v->aOnceFlag = pFrame->aOnceFlag; |
+ v->nOnceFlag = pFrame->nOnceFlag; |
v->aOp = pFrame->aOp; |
v->nOp = pFrame->nOp; |
v->aMem = pFrame->aMem; |
@@ -1585,9 +1788,10 @@ static void closeAllCursors(Vdbe *p){ |
VdbeFrame *pFrame; |
for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
sqlite3VdbeFrameRestore(pFrame); |
+ p->pFrame = 0; |
+ p->nFrame = 0; |
} |
- p->pFrame = 0; |
- p->nFrame = 0; |
+ assert( p->nFrame==0 ); |
if( p->apCsr ){ |
int i; |
@@ -1607,14 +1811,14 @@ static void closeAllCursors(Vdbe *p){ |
p->pDelFrame = pDel->pParent; |
sqlite3VdbeFrameDelete(pDel); |
} |
+ |
+ /* Delete any auxdata allocations made by the VM */ |
+ if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p, -1, 0); |
+ assert( p->pAuxData==0 ); |
} |
/* |
-** Clean up the VM after execution. |
-** |
-** This routine will automatically close any cursors, lists, and/or |
-** sorters that were left open. It also deletes the values of |
-** variables in the aVar[] array. |
+** Clean up the VM after a single run. |
*/ |
static void Cleanup(Vdbe *p){ |
sqlite3 *db = p->db; |
@@ -1623,8 +1827,10 @@ static void Cleanup(Vdbe *p){ |
/* Execute assert() statements to ensure that the Vdbe.apCsr[] and |
** Vdbe.aMem[] arrays have already been cleaned up. */ |
int i; |
- for(i=0; i<p->nCursor; i++) assert( p->apCsr==0 || p->apCsr[i]==0 ); |
- for(i=1; i<=p->nMem; i++) assert( p->aMem==0 || p->aMem[i].flags==MEM_Null ); |
+ if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 ); |
+ if( p->aMem ){ |
+ for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined ); |
+ } |
#endif |
sqlite3DbFree(db, p->zErrMsg); |
@@ -1713,7 +1919,7 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){ |
** required, as an xSync() callback may add an attached database |
** to the transaction. |
*/ |
- rc = sqlite3VtabSync(db, &p->zErrMsg); |
+ rc = sqlite3VtabSync(db, p); |
/* This loop determines (a) if the commit hook should be invoked and |
** (b) how many database files have open write transactions, not |
@@ -1726,7 +1932,9 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){ |
if( sqlite3BtreeIsInTrans(pBt) ){ |
needXcommit = 1; |
if( i!=1 ) nTrans++; |
+ sqlite3BtreeEnter(pBt); |
rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt)); |
+ sqlite3BtreeLeave(pBt); |
} |
} |
if( rc!=SQLITE_OK ){ |
@@ -1737,7 +1945,7 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){ |
if( needXcommit && db->xCommitCallback ){ |
rc = db->xCommitCallback(db->pCommitArg); |
if( rc ){ |
- return SQLITE_CONSTRAINT; |
+ return SQLITE_CONSTRAINT_COMMITHOOK; |
} |
} |
@@ -1778,7 +1986,7 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){ |
/* The complex case - There is a multi-file write-transaction active. |
** This requires a master journal file to ensure the transaction is |
- ** committed atomicly. |
+ ** committed atomically. |
*/ |
#ifndef SQLITE_OMIT_DISKIO |
else{ |
@@ -1789,16 +1997,32 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){ |
sqlite3_file *pMaster = 0; |
i64 offset = 0; |
int res; |
+ int retryCount = 0; |
+ int nMainFile; |
/* Select a master journal file name */ |
+ nMainFile = sqlite3Strlen30(zMainFile); |
+ zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile); |
+ if( zMaster==0 ) return SQLITE_NOMEM; |
do { |
u32 iRandom; |
- sqlite3DbFree(db, zMaster); |
- sqlite3_randomness(sizeof(iRandom), &iRandom); |
- zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, iRandom&0x7fffffff); |
- if( !zMaster ){ |
- return SQLITE_NOMEM; |
+ if( retryCount ){ |
+ if( retryCount>100 ){ |
+ sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster); |
+ sqlite3OsDelete(pVfs, zMaster, 0); |
+ break; |
+ }else if( retryCount==1 ){ |
+ sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster); |
+ } |
} |
+ retryCount++; |
+ sqlite3_randomness(sizeof(iRandom), &iRandom); |
+ sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X", |
+ (iRandom>>8)&0xffffff, iRandom&0xff); |
+ /* The antipenultimate character of the master journal name must |
+ ** be "9" to avoid name collisions when using 8+3 filenames. */ |
+ assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' ); |
+ sqlite3FileSuffix3(zMainFile, zMaster); |
rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); |
}while( rc==SQLITE_OK && res ); |
if( rc==SQLITE_OK ){ |
@@ -1914,7 +2138,7 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){ |
} |
/* |
-** This routine checks that the sqlite3.activeVdbeCnt count variable |
+** This routine checks that the sqlite3.nVdbeActive count variable |
** matches the number of vdbe's in the list sqlite3.pVdbe that are |
** currently active. An assertion fails if the two counts do not match. |
** This is an internal self-check only - it is not an essential processing |
@@ -1927,53 +2151,30 @@ static void checkActiveVdbeCnt(sqlite3 *db){ |
Vdbe *p; |
int cnt = 0; |
int nWrite = 0; |
+ int nRead = 0; |
p = db->pVdbe; |
while( p ){ |
- if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){ |
+ if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){ |
cnt++; |
if( p->readOnly==0 ) nWrite++; |
+ if( p->bIsReader ) nRead++; |
} |
p = p->pNext; |
} |
- assert( cnt==db->activeVdbeCnt ); |
- assert( nWrite==db->writeVdbeCnt ); |
+ assert( cnt==db->nVdbeActive ); |
+ assert( nWrite==db->nVdbeWrite ); |
+ assert( nRead==db->nVdbeRead ); |
} |
#else |
#define checkActiveVdbeCnt(x) |
#endif |
/* |
-** For every Btree that in database connection db which |
-** has been modified, "trip" or invalidate each cursor in |
-** that Btree might have been modified so that the cursor |
-** can never be used again. This happens when a rollback |
-*** occurs. We have to trip all the other cursors, even |
-** cursor from other VMs in different database connections, |
-** so that none of them try to use the data at which they |
-** were pointing and which now may have been changed due |
-** to the rollback. |
-** |
-** Remember that a rollback can delete tables complete and |
-** reorder rootpages. So it is not sufficient just to save |
-** the state of the cursor. We have to invalidate the cursor |
-** so that it is never used again. |
-*/ |
-static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){ |
- int i; |
- for(i=0; i<db->nDb; i++){ |
- Btree *p = db->aDb[i].pBt; |
- if( p && sqlite3BtreeIsInTrans(p) ){ |
- sqlite3BtreeTripAllCursors(p, SQLITE_ABORT); |
- } |
- } |
-} |
- |
-/* |
** If the Vdbe passed as the first argument opened a statement-transaction, |
** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or |
** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement |
** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the |
-** statement transaction is commtted. |
+** statement transaction is committed. |
** |
** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. |
** Otherwise SQLITE_OK. |
@@ -1984,7 +2185,7 @@ int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ |
/* If p->iStatement is greater than zero, then this Vdbe opened a |
** statement transaction that should be closed here. The only exception |
- ** is that an IO error may have occured, causing an emergency rollback. |
+ ** is that an IO error may have occurred, causing an emergency rollback. |
** In this case (db->nStatement==0), and there is nothing to do. |
*/ |
if( db->nStatement && p->iStatement ){ |
@@ -2013,11 +2214,21 @@ int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ |
db->nStatement--; |
p->iStatement = 0; |
+ if( rc==SQLITE_OK ){ |
+ if( eOp==SAVEPOINT_ROLLBACK ){ |
+ rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint); |
+ } |
+ if( rc==SQLITE_OK ){ |
+ rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint); |
+ } |
+ } |
+ |
/* If the statement transaction is being rolled back, also restore the |
** database handles deferred constraint counter to the value it had when |
** the statement transaction was opened. */ |
if( eOp==SAVEPOINT_ROLLBACK ){ |
db->nDeferredCons = p->nStmtDefCons; |
+ db->nDeferredImmCons = p->nStmtDefImmCons; |
} |
} |
return rc; |
@@ -2030,16 +2241,18 @@ int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ |
** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK. |
** |
** If there are outstanding FK violations and this function returns |
-** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write |
-** an error message to it. Then return SQLITE_ERROR. |
+** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY |
+** and write an error message to it. Then return SQLITE_ERROR. |
*/ |
#ifndef SQLITE_OMIT_FOREIGN_KEY |
int sqlite3VdbeCheckFk(Vdbe *p, int deferred){ |
sqlite3 *db = p->db; |
- if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){ |
- p->rc = SQLITE_CONSTRAINT; |
+ if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0) |
+ || (!deferred && p->nFkConstraint>0) |
+ ){ |
+ p->rc = SQLITE_CONSTRAINT_FOREIGNKEY; |
p->errorAction = OE_Abort; |
- sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed"); |
+ sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed"); |
return SQLITE_ERROR; |
} |
return SQLITE_OK; |
@@ -2082,14 +2295,16 @@ int sqlite3VdbeHalt(Vdbe *p){ |
if( p->db->mallocFailed ){ |
p->rc = SQLITE_NOMEM; |
} |
+ if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag); |
closeAllCursors(p); |
if( p->magic!=VDBE_MAGIC_RUN ){ |
return SQLITE_OK; |
} |
checkActiveVdbeCnt(db); |
- /* No commit or rollback needed if the program never started */ |
- if( p->pc>=0 ){ |
+ /* No commit or rollback needed if the program never started or if the |
+ ** SQL statement does not read or write a database file. */ |
+ if( p->pc>=0 && p->bIsReader ){ |
int mrc; /* Primary error code from p->rc */ |
int eStatementOp = 0; |
int isSpecialError; /* Set to true if a 'special' error */ |
@@ -2099,7 +2314,6 @@ int sqlite3VdbeHalt(Vdbe *p){ |
/* Check for one of the special errors */ |
mrc = p->rc & 0xff; |
- assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */ |
isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR |
|| mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; |
if( isSpecialError ){ |
@@ -2110,7 +2324,7 @@ int sqlite3VdbeHalt(Vdbe *p){ |
** |
** Even if the statement is read-only, it is important to perform |
** a statement or transaction rollback operation. If the error |
- ** occured while writing to the journal, sub-journal or database |
+ ** occurred while writing to the journal, sub-journal or database |
** file as part of an effort to free up cache space (see function |
** pagerStress() in pager.c), the rollback is required to restore |
** the pager to a consistent state. |
@@ -2122,8 +2336,7 @@ int sqlite3VdbeHalt(Vdbe *p){ |
/* We are forced to roll back the active transaction. Before doing |
** so, abort any other statements this handle currently has active. |
*/ |
- invalidateCursorsOnModifiedBtrees(db); |
- sqlite3RollbackAll(db); |
+ sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
sqlite3CloseSavepoints(db); |
db->autoCommit = 1; |
} |
@@ -2143,7 +2356,7 @@ int sqlite3VdbeHalt(Vdbe *p){ |
*/ |
if( !sqlite3VtabInSync(db) |
&& db->autoCommit |
- && db->writeVdbeCnt==(p->readOnly==0) |
+ && db->nVdbeWrite==(p->readOnly==0) |
){ |
if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ |
rc = sqlite3VdbeCheckFk(p, 1); |
@@ -2152,7 +2365,7 @@ int sqlite3VdbeHalt(Vdbe *p){ |
sqlite3VdbeLeave(p); |
return SQLITE_ERROR; |
} |
- rc = SQLITE_CONSTRAINT; |
+ rc = SQLITE_CONSTRAINT_FOREIGNKEY; |
}else{ |
/* The auto-commit flag is true, the vdbe program was successful |
** or hit an 'OR FAIL' constraint and there are no deferred foreign |
@@ -2165,13 +2378,15 @@ int sqlite3VdbeHalt(Vdbe *p){ |
return SQLITE_BUSY; |
}else if( rc!=SQLITE_OK ){ |
p->rc = rc; |
- sqlite3RollbackAll(db); |
+ sqlite3RollbackAll(db, SQLITE_OK); |
}else{ |
db->nDeferredCons = 0; |
+ db->nDeferredImmCons = 0; |
+ db->flags &= ~SQLITE_DeferFKs; |
sqlite3CommitInternalChanges(db); |
} |
}else{ |
- sqlite3RollbackAll(db); |
+ sqlite3RollbackAll(db, SQLITE_OK); |
} |
db->nStatement = 0; |
}else if( eStatementOp==0 ){ |
@@ -2180,8 +2395,7 @@ int sqlite3VdbeHalt(Vdbe *p){ |
}else if( p->errorAction==OE_Abort ){ |
eStatementOp = SAVEPOINT_ROLLBACK; |
}else{ |
- invalidateCursorsOnModifiedBtrees(db); |
- sqlite3RollbackAll(db); |
+ sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
sqlite3CloseSavepoints(db); |
db->autoCommit = 1; |
} |
@@ -2192,23 +2406,16 @@ int sqlite3VdbeHalt(Vdbe *p){ |
** do so. If this operation returns an error, and the current statement |
** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the |
** current statement error code. |
- ** |
- ** Note that sqlite3VdbeCloseStatement() can only fail if eStatementOp |
- ** is SAVEPOINT_ROLLBACK. But if p->rc==SQLITE_OK then eStatementOp |
- ** must be SAVEPOINT_RELEASE. Hence the NEVER(p->rc==SQLITE_OK) in |
- ** the following code. |
*/ |
if( eStatementOp ){ |
rc = sqlite3VdbeCloseStatement(p, eStatementOp); |
if( rc ){ |
- assert( eStatementOp==SAVEPOINT_ROLLBACK ); |
- if( NEVER(p->rc==SQLITE_OK) || p->rc==SQLITE_CONSTRAINT ){ |
+ if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){ |
p->rc = rc; |
sqlite3DbFree(db, p->zErrMsg); |
p->zErrMsg = 0; |
} |
- invalidateCursorsOnModifiedBtrees(db); |
- sqlite3RollbackAll(db); |
+ sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
sqlite3CloseSavepoints(db); |
db->autoCommit = 1; |
} |
@@ -2225,12 +2432,6 @@ int sqlite3VdbeHalt(Vdbe *p){ |
} |
p->nChange = 0; |
} |
- |
- /* Rollback or commit any schema changes that occurred. */ |
- if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){ |
- sqlite3ResetInternalSchema(db, -1); |
- db->flags = (db->flags | SQLITE_InternChanges); |
- } |
/* Release the locks */ |
sqlite3VdbeLeave(p); |
@@ -2238,11 +2439,12 @@ int sqlite3VdbeHalt(Vdbe *p){ |
/* We have successfully halted and closed the VM. Record this fact. */ |
if( p->pc>=0 ){ |
- db->activeVdbeCnt--; |
- if( !p->readOnly ){ |
- db->writeVdbeCnt--; |
- } |
- assert( db->activeVdbeCnt>=db->writeVdbeCnt ); |
+ db->nVdbeActive--; |
+ if( !p->readOnly ) db->nVdbeWrite--; |
+ if( p->bIsReader ) db->nVdbeRead--; |
+ assert( db->nVdbeActive>=db->nVdbeRead ); |
+ assert( db->nVdbeRead>=db->nVdbeWrite ); |
+ assert( db->nVdbeWrite>=0 ); |
} |
p->magic = VDBE_MAGIC_HALT; |
checkActiveVdbeCnt(db); |
@@ -2258,7 +2460,7 @@ int sqlite3VdbeHalt(Vdbe *p){ |
sqlite3ConnectionUnlocked(db); |
} |
- assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 ); |
+ assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 ); |
return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK); |
} |
@@ -2272,11 +2474,57 @@ void sqlite3VdbeResetStepResult(Vdbe *p){ |
} |
/* |
-** Clean up a VDBE after execution but do not delete the VDBE just yet. |
-** Write any error messages into *pzErrMsg. Return the result code. |
-** |
-** After this routine is run, the VDBE should be ready to be executed |
-** again. |
+** Copy the error code and error message belonging to the VDBE passed |
+** as the first argument to its database handle (so that they will be |
+** returned by calls to sqlite3_errcode() and sqlite3_errmsg()). |
+** |
+** This function does not clear the VDBE error code or message, just |
+** copies them to the database handle. |
+*/ |
+int sqlite3VdbeTransferError(Vdbe *p){ |
+ sqlite3 *db = p->db; |
+ int rc = p->rc; |
+ if( p->zErrMsg ){ |
+ u8 mallocFailed = db->mallocFailed; |
+ sqlite3BeginBenignMalloc(); |
+ if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db); |
+ sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT); |
+ sqlite3EndBenignMalloc(); |
+ db->mallocFailed = mallocFailed; |
+ db->errCode = rc; |
+ }else{ |
+ sqlite3Error(db, rc); |
+ } |
+ return rc; |
+} |
+ |
+#ifdef SQLITE_ENABLE_SQLLOG |
+/* |
+** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, |
+** invoke it. |
+*/ |
+static void vdbeInvokeSqllog(Vdbe *v){ |
+ if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){ |
+ char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql); |
+ assert( v->db->init.busy==0 ); |
+ if( zExpanded ){ |
+ sqlite3GlobalConfig.xSqllog( |
+ sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1 |
+ ); |
+ sqlite3DbFree(v->db, zExpanded); |
+ } |
+ } |
+} |
+#else |
+# define vdbeInvokeSqllog(x) |
+#endif |
+ |
+/* |
+** Clean up a VDBE after execution but do not delete the VDBE just yet. |
+** Write any error messages into *pzErrMsg. Return the result code. |
+** |
+** After this routine is run, the VDBE should be ready to be executed |
+** again. |
** |
** To look at it another way, this routine resets the state of the |
** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to |
@@ -2298,26 +2546,17 @@ int sqlite3VdbeReset(Vdbe *p){ |
** instructions yet, leave the main database error information unchanged. |
*/ |
if( p->pc>=0 ){ |
- if( p->zErrMsg ){ |
- sqlite3BeginBenignMalloc(); |
- sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT); |
- sqlite3EndBenignMalloc(); |
- db->errCode = p->rc; |
- sqlite3DbFree(db, p->zErrMsg); |
- p->zErrMsg = 0; |
- }else if( p->rc ){ |
- sqlite3Error(db, p->rc, 0); |
- }else{ |
- sqlite3Error(db, SQLITE_OK, 0); |
- } |
+ vdbeInvokeSqllog(p); |
+ sqlite3VdbeTransferError(p); |
+ sqlite3DbFree(db, p->zErrMsg); |
+ p->zErrMsg = 0; |
if( p->runOnlyOnce ) p->expired = 1; |
}else if( p->rc && p->expired ){ |
/* The expired flag was set on the VDBE before the first call |
** to sqlite3_step(). For consistency (since sqlite3_step() was |
** called), set the database error in this case as well. |
*/ |
- sqlite3Error(db, p->rc, 0); |
- sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT); |
+ sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg); |
sqlite3DbFree(db, p->zErrMsg); |
p->zErrMsg = 0; |
} |
@@ -2338,18 +2577,31 @@ int sqlite3VdbeReset(Vdbe *p){ |
fprintf(out, "%02x", p->aOp[i].opcode); |
} |
fprintf(out, "\n"); |
+ if( p->zSql ){ |
+ char c, pc = 0; |
+ fprintf(out, "-- "); |
+ for(i=0; (c = p->zSql[i])!=0; i++){ |
+ if( pc=='\n' ) fprintf(out, "-- "); |
+ putc(c, out); |
+ pc = c; |
+ } |
+ if( pc!='\n' ) fprintf(out, "\n"); |
+ } |
for(i=0; i<p->nOp; i++){ |
- fprintf(out, "%6d %10lld %8lld ", |
+ char zHdr[100]; |
+ sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ", |
p->aOp[i].cnt, |
p->aOp[i].cycles, |
p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 |
); |
+ fprintf(out, "%s", zHdr); |
sqlite3VdbePrintOp(out, i, &p->aOp[i]); |
} |
fclose(out); |
} |
} |
#endif |
+ p->iCurrentTime = 0; |
p->magic = VDBE_MAGIC_INIT; |
return p->rc & db->errMask; |
} |
@@ -2369,32 +2621,51 @@ int sqlite3VdbeFinalize(Vdbe *p){ |
} |
/* |
-** Call the destructor for each auxdata entry in pVdbeFunc for which |
-** the corresponding bit in mask is clear. Auxdata entries beyond 31 |
-** are always destroyed. To destroy all auxdata entries, call this |
-** routine with mask==0. |
+** If parameter iOp is less than zero, then invoke the destructor for |
+** all auxiliary data pointers currently cached by the VM passed as |
+** the first argument. |
+** |
+** Or, if iOp is greater than or equal to zero, then the destructor is |
+** only invoked for those auxiliary data pointers created by the user |
+** function invoked by the OP_Function opcode at instruction iOp of |
+** VM pVdbe, and only then if: |
+** |
+** * the associated function parameter is the 32nd or later (counting |
+** from left to right), or |
+** |
+** * the corresponding bit in argument mask is clear (where the first |
+** function parameter corresponds to bit 0 etc.). |
*/ |
-void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){ |
- int i; |
- for(i=0; i<pVdbeFunc->nAux; i++){ |
- struct AuxData *pAux = &pVdbeFunc->apAux[i]; |
- if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){ |
+void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){ |
+ AuxData **pp = &pVdbe->pAuxData; |
+ while( *pp ){ |
+ AuxData *pAux = *pp; |
+ if( (iOp<0) |
+ || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg)))) |
+ ){ |
+ testcase( pAux->iArg==31 ); |
if( pAux->xDelete ){ |
pAux->xDelete(pAux->pAux); |
} |
- pAux->pAux = 0; |
+ *pp = pAux->pNext; |
+ sqlite3DbFree(pVdbe->db, pAux); |
+ }else{ |
+ pp= &pAux->pNext; |
} |
} |
} |
/* |
-** Free all memory associated with the Vdbe passed as the second argument. |
+** Free all memory associated with the Vdbe passed as the second argument, |
+** except for object itself, which is preserved. |
+** |
** The difference between this function and sqlite3VdbeDelete() is that |
** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with |
-** the database connection. |
+** the database connection and frees the object itself. |
*/ |
-void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){ |
+void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){ |
SubProgram *pSub, *pNext; |
+ int i; |
assert( p->db==0 || p->db==db ); |
releaseMemArray(p->aVar, p->nVar); |
releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
@@ -2403,12 +2674,11 @@ void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){ |
vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); |
sqlite3DbFree(db, pSub); |
} |
+ for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); |
vdbeFreeOpArray(db, p->aOp, p->nOp); |
- sqlite3DbFree(db, p->aLabel); |
sqlite3DbFree(db, p->aColName); |
sqlite3DbFree(db, p->zSql); |
sqlite3DbFree(db, p->pFree); |
- sqlite3DbFree(db, p); |
} |
/* |
@@ -2419,6 +2689,8 @@ void sqlite3VdbeDelete(Vdbe *p){ |
if( NEVER(p==0) ) return; |
db = p->db; |
+ assert( sqlite3_mutex_held(db->mutex) ); |
+ sqlite3VdbeClearObject(db, p); |
if( p->pPrev ){ |
p->pPrev->pNext = p->pNext; |
}else{ |
@@ -2430,7 +2702,58 @@ void sqlite3VdbeDelete(Vdbe *p){ |
} |
p->magic = VDBE_MAGIC_DEAD; |
p->db = 0; |
- sqlite3VdbeDeleteObject(db, p); |
+ sqlite3DbFree(db, p); |
+} |
+ |
+/* |
+** The cursor "p" has a pending seek operation that has not yet been |
+** carried out. Seek the cursor now. If an error occurs, return |
+** the appropriate error code. |
+*/ |
+static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){ |
+ int res, rc; |
+#ifdef SQLITE_TEST |
+ extern int sqlite3_search_count; |
+#endif |
+ assert( p->deferredMoveto ); |
+ assert( p->isTable ); |
+ rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res); |
+ if( rc ) return rc; |
+ if( res!=0 ) return SQLITE_CORRUPT_BKPT; |
+#ifdef SQLITE_TEST |
+ sqlite3_search_count++; |
+#endif |
+ p->deferredMoveto = 0; |
+ p->cacheStatus = CACHE_STALE; |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Something has moved cursor "p" out of place. Maybe the row it was |
+** pointed to was deleted out from under it. Or maybe the btree was |
+** rebalanced. Whatever the cause, try to restore "p" to the place it |
+** is supposed to be pointing. If the row was deleted out from under the |
+** cursor, set the cursor to point to a NULL row. |
+*/ |
+static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){ |
+ int isDifferentRow, rc; |
+ assert( p->pCursor!=0 ); |
+ assert( sqlite3BtreeCursorHasMoved(p->pCursor) ); |
+ rc = sqlite3BtreeCursorRestore(p->pCursor, &isDifferentRow); |
+ p->cacheStatus = CACHE_STALE; |
+ if( isDifferentRow ) p->nullRow = 1; |
+ return rc; |
+} |
+ |
+/* |
+** Check to ensure that the cursor is valid. Restore the cursor |
+** if need be. Return any I/O error from the restore operation. |
+*/ |
+int sqlite3VdbeCursorRestore(VdbeCursor *p){ |
+ if( sqlite3BtreeCursorHasMoved(p->pCursor) ){ |
+ return handleMovedCursor(p); |
+ } |
+ return SQLITE_OK; |
} |
/* |
@@ -2448,29 +2771,10 @@ void sqlite3VdbeDelete(Vdbe *p){ |
*/ |
int sqlite3VdbeCursorMoveto(VdbeCursor *p){ |
if( p->deferredMoveto ){ |
- int res, rc; |
-#ifdef SQLITE_TEST |
- extern int sqlite3_search_count; |
-#endif |
- assert( p->isTable ); |
- rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res); |
- if( rc ) return rc; |
- p->lastRowid = p->movetoTarget; |
- if( res!=0 ) return SQLITE_CORRUPT_BKPT; |
- p->rowidIsValid = 1; |
-#ifdef SQLITE_TEST |
- sqlite3_search_count++; |
-#endif |
- p->deferredMoveto = 0; |
- p->cacheStatus = CACHE_STALE; |
- }else if( ALWAYS(p->pCursor) ){ |
- int hasMoved; |
- int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved); |
- if( rc ) return rc; |
- if( hasMoved ){ |
- p->cacheStatus = CACHE_STALE; |
- p->nullRow = 1; |
- } |
+ return handleDeferredMoveto(p); |
+ } |
+ if( p->pCursor && sqlite3BtreeCursorHasMoved(p->pCursor) ){ |
+ return handleMovedCursor(p); |
} |
return SQLITE_OK; |
} |
@@ -2493,7 +2797,7 @@ int sqlite3VdbeCursorMoveto(VdbeCursor *p){ |
** the blob of data that it corresponds to. In a table record, all serial |
** types are stored at the start of the record, and the blobs of data at |
** the end. Hence these functions allow the caller to handle the |
-** serial-type and data blob seperately. |
+** serial-type and data blob separately. |
** |
** The following table describes the various storage classes for data: |
** |
@@ -2522,7 +2826,7 @@ int sqlite3VdbeCursorMoveto(VdbeCursor *p){ |
*/ |
u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ |
int flags = pMem->flags; |
- int n; |
+ u32 n; |
if( flags&MEM_Null ){ |
return 0; |
@@ -2532,9 +2836,6 @@ u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ |
# define MAX_6BYTE ((((i64)0x00008000)<<32)-1) |
i64 i = pMem->u.i; |
u64 u; |
- if( file_format>=4 && (i&1)==i ){ |
- return 8+(u32)i; |
- } |
if( i<0 ){ |
if( i<(-MAX_6BYTE) ) return 6; |
/* Previous test prevents: u = -(-9223372036854775808) */ |
@@ -2542,7 +2843,9 @@ u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ |
}else{ |
u = i; |
} |
- if( u<=127 ) return 1; |
+ if( u<=127 ){ |
+ return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1; |
+ } |
if( u<=32767 ) return 2; |
if( u<=8388607 ) return 3; |
if( u<=2147483647 ) return 4; |
@@ -2553,11 +2856,11 @@ u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ |
return 7; |
} |
assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) ); |
- n = pMem->n; |
+ assert( pMem->n>=0 ); |
+ n = (u32)pMem->n; |
if( flags & MEM_Zero ){ |
n += pMem->u.nZero; |
} |
- assert( n>=0 ); |
return ((n*2) + 12 + ((flags&MEM_Str)!=0)); |
} |
@@ -2631,21 +2934,15 @@ static u64 floatSwap(u64 in){ |
** buf. It is assumed that the caller has allocated sufficient space. |
** Return the number of bytes written. |
** |
-** nBuf is the amount of space left in buf[]. nBuf must always be |
-** large enough to hold the entire field. Except, if the field is |
-** a blob with a zero-filled tail, then buf[] might be just the right |
-** size to hold everything except for the zero-filled tail. If buf[] |
-** is only big enough to hold the non-zero prefix, then only write that |
-** prefix into buf[]. But if buf[] is large enough to hold both the |
-** prefix and the tail then write the prefix and set the tail to all |
-** zeros. |
+** nBuf is the amount of space left in buf[]. The caller is responsible |
+** for allocating enough space to buf[] to hold the entire field, exclusive |
+** of the pMem->u.nZero bytes for a MEM_Zero value. |
** |
** Return the number of bytes actually written into buf[]. The number |
** of bytes in the zero-filled tail is included in the return value only |
** if those bytes were zeroed in buf[]. |
*/ |
-u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){ |
- u32 serial_type = sqlite3VdbeSerialType(pMem, file_format); |
+u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){ |
u32 len; |
/* Integer and Real */ |
@@ -2653,18 +2950,18 @@ u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){ |
u64 v; |
u32 i; |
if( serial_type==7 ){ |
- assert( sizeof(v)==sizeof(pMem->r) ); |
- memcpy(&v, &pMem->r, sizeof(v)); |
+ assert( sizeof(v)==sizeof(pMem->u.r) ); |
+ memcpy(&v, &pMem->u.r, sizeof(v)); |
swapMixedEndianFloat(v); |
}else{ |
v = pMem->u.i; |
} |
len = i = sqlite3VdbeSerialTypeLen(serial_type); |
- assert( len<=(u32)nBuf ); |
- while( i-- ){ |
- buf[i] = (u8)(v&0xFF); |
+ assert( i>0 ); |
+ do{ |
+ buf[--i] = (u8)(v&0xFF); |
v >>= 8; |
- } |
+ }while( i ); |
return len; |
} |
@@ -2672,17 +2969,8 @@ u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){ |
if( serial_type>=12 ){ |
assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0) |
== (int)sqlite3VdbeSerialTypeLen(serial_type) ); |
- assert( pMem->n<=nBuf ); |
len = pMem->n; |
memcpy(buf, pMem->z, len); |
- if( pMem->flags & MEM_Zero ){ |
- len += pMem->u.nZero; |
- assert( nBuf>=0 ); |
- if( len > (u32)nBuf ){ |
- len = (u32)nBuf; |
- } |
- memset(&buf[pMem->n], 0, len-pMem->n); |
- } |
return len; |
} |
@@ -2690,10 +2978,56 @@ u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){ |
return 0; |
} |
+/* Input "x" is a sequence of unsigned characters that represent a |
+** big-endian integer. Return the equivalent native integer |
+*/ |
+#define ONE_BYTE_INT(x) ((i8)(x)[0]) |
+#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1]) |
+#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2]) |
+#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) |
+#define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) |
+ |
/* |
** Deserialize the data blob pointed to by buf as serial type serial_type |
** and store the result in pMem. Return the number of bytes read. |
+** |
+** This function is implemented as two separate routines for performance. |
+** The few cases that require local variables are broken out into a separate |
+** routine so that in most cases the overhead of moving the stack pointer |
+** is avoided. |
*/ |
+static u32 SQLITE_NOINLINE serialGet( |
+ const unsigned char *buf, /* Buffer to deserialize from */ |
+ u32 serial_type, /* Serial type to deserialize */ |
+ Mem *pMem /* Memory cell to write value into */ |
+){ |
+ u64 x = FOUR_BYTE_UINT(buf); |
+ u32 y = FOUR_BYTE_UINT(buf+4); |
+ x = (x<<32) + y; |
+ if( serial_type==6 ){ |
+ pMem->u.i = *(i64*)&x; |
+ pMem->flags = MEM_Int; |
+ testcase( pMem->u.i<0 ); |
+ }else{ |
+#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) |
+ /* Verify that integers and floating point values use the same |
+ ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is |
+ ** defined that 64-bit floating point values really are mixed |
+ ** endian. |
+ */ |
+ static const u64 t1 = ((u64)0x3ff00000)<<32; |
+ static const double r1 = 1.0; |
+ u64 t2 = t1; |
+ swapMixedEndianFloat(t2); |
+ assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); |
+#endif |
+ assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 ); |
+ swapMixedEndianFloat(x); |
+ memcpy(&pMem->u.r, &x, sizeof(x)); |
+ pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real; |
+ } |
+ return 8; |
+} |
u32 sqlite3VdbeSerialGet( |
const unsigned char *buf, /* Buffer to deserialize from */ |
u32 serial_type, /* Serial type to deserialize */ |
@@ -2707,63 +3041,40 @@ u32 sqlite3VdbeSerialGet( |
break; |
} |
case 1: { /* 1-byte signed integer */ |
- pMem->u.i = (signed char)buf[0]; |
+ pMem->u.i = ONE_BYTE_INT(buf); |
pMem->flags = MEM_Int; |
+ testcase( pMem->u.i<0 ); |
return 1; |
} |
case 2: { /* 2-byte signed integer */ |
- pMem->u.i = (((signed char)buf[0])<<8) | buf[1]; |
+ pMem->u.i = TWO_BYTE_INT(buf); |
pMem->flags = MEM_Int; |
+ testcase( pMem->u.i<0 ); |
return 2; |
} |
case 3: { /* 3-byte signed integer */ |
- pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2]; |
+ pMem->u.i = THREE_BYTE_INT(buf); |
pMem->flags = MEM_Int; |
+ testcase( pMem->u.i<0 ); |
return 3; |
} |
case 4: { /* 4-byte signed integer */ |
- pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; |
+ pMem->u.i = FOUR_BYTE_INT(buf); |
pMem->flags = MEM_Int; |
+ testcase( pMem->u.i<0 ); |
return 4; |
} |
case 5: { /* 6-byte signed integer */ |
- u64 x = (((signed char)buf[0])<<8) | buf[1]; |
- u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; |
- x = (x<<32) | y; |
- pMem->u.i = *(i64*)&x; |
+ pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); |
pMem->flags = MEM_Int; |
+ testcase( pMem->u.i<0 ); |
return 6; |
} |
case 6: /* 8-byte signed integer */ |
case 7: { /* IEEE floating point */ |
- u64 x; |
- u32 y; |
-#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) |
- /* Verify that integers and floating point values use the same |
- ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is |
- ** defined that 64-bit floating point values really are mixed |
- ** endian. |
- */ |
- static const u64 t1 = ((u64)0x3ff00000)<<32; |
- static const double r1 = 1.0; |
- u64 t2 = t1; |
- swapMixedEndianFloat(t2); |
- assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); |
-#endif |
- |
- x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; |
- y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; |
- x = (x<<32) | y; |
- if( serial_type==6 ){ |
- pMem->u.i = *(i64*)&x; |
- pMem->flags = MEM_Int; |
- }else{ |
- assert( sizeof(x)==8 && sizeof(pMem->r)==8 ); |
- swapMixedEndianFloat(x); |
- memcpy(&pMem->r, &x, sizeof(x)); |
- pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real; |
- } |
- return 8; |
+ /* These use local variables, so do them in a separate routine |
+ ** to avoid having to move the frame pointer in the common case */ |
+ return serialGet(buf,serial_type,pMem); |
} |
case 8: /* Integer 0 */ |
case 9: { /* Integer 1 */ |
@@ -2772,164 +3083,136 @@ u32 sqlite3VdbeSerialGet( |
return 0; |
} |
default: { |
- u32 len = (serial_type-12)/2; |
+ static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem }; |
pMem->z = (char *)buf; |
- pMem->n = len; |
- pMem->xDel = 0; |
- if( serial_type&0x01 ){ |
- pMem->flags = MEM_Str | MEM_Ephem; |
- }else{ |
- pMem->flags = MEM_Blob | MEM_Ephem; |
- } |
- return len; |
+ pMem->n = (serial_type-12)/2; |
+ pMem->flags = aFlag[serial_type&1]; |
+ return pMem->n; |
} |
} |
return 0; |
} |
- |
- |
/* |
-** Given the nKey-byte encoding of a record in pKey[], parse the |
-** record into a UnpackedRecord structure. Return a pointer to |
-** that structure. |
+** This routine is used to allocate sufficient space for an UnpackedRecord |
+** structure large enough to be used with sqlite3VdbeRecordUnpack() if |
+** the first argument is a pointer to KeyInfo structure pKeyInfo. |
** |
-** The calling function might provide szSpace bytes of memory |
-** space at pSpace. This space can be used to hold the returned |
-** VDbeParsedRecord structure if it is large enough. If it is |
-** not big enough, space is obtained from sqlite3_malloc(). |
+** The space is either allocated using sqlite3DbMallocRaw() or from within |
+** the unaligned buffer passed via the second and third arguments (presumably |
+** stack space). If the former, then *ppFree is set to a pointer that should |
+** be eventually freed by the caller using sqlite3DbFree(). Or, if the |
+** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL |
+** before returning. |
** |
-** The returned structure should be closed by a call to |
-** sqlite3VdbeDeleteUnpackedRecord(). |
-*/ |
-UnpackedRecord *sqlite3VdbeRecordUnpack( |
- KeyInfo *pKeyInfo, /* Information about the record format */ |
- int nKey, /* Size of the binary record */ |
- const void *pKey, /* The binary record */ |
- char *pSpace, /* Unaligned space available to hold the object */ |
- int szSpace /* Size of pSpace[] in bytes */ |
+** If an OOM error occurs, NULL is returned. |
+*/ |
+UnpackedRecord *sqlite3VdbeAllocUnpackedRecord( |
+ KeyInfo *pKeyInfo, /* Description of the record */ |
+ char *pSpace, /* Unaligned space available */ |
+ int szSpace, /* Size of pSpace[] in bytes */ |
+ char **ppFree /* OUT: Caller should free this pointer */ |
){ |
- const unsigned char *aKey = (const unsigned char *)pKey; |
- UnpackedRecord *p; /* The unpacked record that we will return */ |
- int nByte; /* Memory space needed to hold p, in bytes */ |
- int d; |
- u32 idx; |
- u16 u; /* Unsigned loop counter */ |
- u32 szHdr; |
- Mem *pMem; |
- int nOff; /* Increase pSpace by this much to 8-byte align it */ |
- |
- /* |
- ** We want to shift the pointer pSpace up such that it is 8-byte aligned. |
+ UnpackedRecord *p; /* Unpacked record to return */ |
+ int nOff; /* Increment pSpace by nOff to align it */ |
+ int nByte; /* Number of bytes required for *p */ |
+ |
+ /* We want to shift the pointer pSpace up such that it is 8-byte aligned. |
** Thus, we need to calculate a value, nOff, between 0 and 7, to shift |
** it by. If pSpace is already 8-byte aligned, nOff should be zero. |
*/ |
nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7; |
- pSpace += nOff; |
- szSpace -= nOff; |
nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1); |
- if( nByte>szSpace ){ |
- p = sqlite3DbMallocRaw(pKeyInfo->db, nByte); |
- if( p==0 ) return 0; |
- p->flags = UNPACKED_NEED_FREE | UNPACKED_NEED_DESTROY; |
+ if( nByte>szSpace+nOff ){ |
+ p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte); |
+ *ppFree = (char *)p; |
+ if( !p ) return 0; |
}else{ |
- p = (UnpackedRecord*)pSpace; |
- p->flags = UNPACKED_NEED_DESTROY; |
+ p = (UnpackedRecord*)&pSpace[nOff]; |
+ *ppFree = 0; |
} |
+ |
+ p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))]; |
+ assert( pKeyInfo->aSortOrder!=0 ); |
p->pKeyInfo = pKeyInfo; |
p->nField = pKeyInfo->nField + 1; |
- p->aMem = pMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))]; |
+ return p; |
+} |
+ |
+/* |
+** Given the nKey-byte encoding of a record in pKey[], populate the |
+** UnpackedRecord structure indicated by the fourth argument with the |
+** contents of the decoded record. |
+*/ |
+void sqlite3VdbeRecordUnpack( |
+ KeyInfo *pKeyInfo, /* Information about the record format */ |
+ int nKey, /* Size of the binary record */ |
+ const void *pKey, /* The binary record */ |
+ UnpackedRecord *p /* Populate this structure before returning. */ |
+){ |
+ const unsigned char *aKey = (const unsigned char *)pKey; |
+ int d; |
+ u32 idx; /* Offset in aKey[] to read from */ |
+ u16 u; /* Unsigned loop counter */ |
+ u32 szHdr; |
+ Mem *pMem = p->aMem; |
+ |
+ p->default_rc = 0; |
assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
idx = getVarint32(aKey, szHdr); |
d = szHdr; |
u = 0; |
- while( idx<szHdr && u<p->nField && d<=nKey ){ |
+ while( idx<szHdr && d<=nKey ){ |
u32 serial_type; |
idx += getVarint32(&aKey[idx], serial_type); |
pMem->enc = pKeyInfo->enc; |
pMem->db = pKeyInfo->db; |
- pMem->flags = 0; |
- pMem->zMalloc = 0; |
+ /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */ |
+ pMem->szMalloc = 0; |
d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); |
pMem++; |
- u++; |
+ if( (++u)>=p->nField ) break; |
} |
assert( u<=pKeyInfo->nField + 1 ); |
p->nField = u; |
- return (void*)p; |
-} |
- |
-/* |
-** This routine destroys a UnpackedRecord object. |
-*/ |
-void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){ |
- int i; |
- Mem *pMem; |
- |
- assert( p!=0 ); |
- assert( p->flags & UNPACKED_NEED_DESTROY ); |
- for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){ |
- /* The unpacked record is always constructed by the |
- ** sqlite3VdbeUnpackRecord() function above, which makes all |
- ** strings and blobs static. And none of the elements are |
- ** ever transformed, so there is never anything to delete. |
- */ |
- if( NEVER(pMem->zMalloc) ) sqlite3VdbeMemRelease(pMem); |
- } |
- if( p->flags & UNPACKED_NEED_FREE ){ |
- sqlite3DbFree(p->pKeyInfo->db, p); |
- } |
} |
+#if SQLITE_DEBUG |
/* |
-** This function compares the two table rows or index records |
-** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero |
-** or positive integer if key1 is less than, equal to or |
-** greater than key2. The {nKey1, pKey1} key must be a blob |
-** created by th OP_MakeRecord opcode of the VDBE. The pPKey2 |
-** key must be a parsed key such as obtained from |
-** sqlite3VdbeParseRecord. |
+** This function compares two index or table record keys in the same way |
+** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(), |
+** this function deserializes and compares values using the |
+** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used |
+** in assert() statements to ensure that the optimized code in |
+** sqlite3VdbeRecordCompare() returns results with these two primitives. |
** |
-** Key1 and Key2 do not have to contain the same number of fields. |
-** The key with fewer fields is usually compares less than the |
-** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set |
-** and the common prefixes are equal, then key1 is less than key2. |
-** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are |
-** equal, then the keys are considered to be equal and |
-** the parts beyond the common prefix are ignored. |
-** |
-** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of |
-** the header of pKey1 is ignored. It is assumed that pKey1 is |
-** an index key, and thus ends with a rowid value. The last byte |
-** of the header will therefore be the serial type of the rowid: |
-** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types. |
-** The serial type of the final rowid will always be a single byte. |
-** By ignoring this last byte of the header, we force the comparison |
-** to ignore the rowid at the end of key1. |
+** Return true if the result of comparison is equivalent to desiredResult. |
+** Return false if there is a disagreement. |
*/ |
-int sqlite3VdbeRecordCompare( |
+static int vdbeRecordCompareDebug( |
int nKey1, const void *pKey1, /* Left key */ |
- UnpackedRecord *pPKey2 /* Right key */ |
+ const UnpackedRecord *pPKey2, /* Right key */ |
+ int desiredResult /* Correct answer */ |
){ |
- int d1; /* Offset into aKey[] of next data element */ |
+ u32 d1; /* Offset into aKey[] of next data element */ |
u32 idx1; /* Offset into aKey[] of next header element */ |
u32 szHdr1; /* Number of bytes in header */ |
int i = 0; |
- int nField; |
int rc = 0; |
const unsigned char *aKey1 = (const unsigned char *)pKey1; |
KeyInfo *pKeyInfo; |
Mem mem1; |
pKeyInfo = pPKey2->pKeyInfo; |
+ if( pKeyInfo->db==0 ) return 1; |
mem1.enc = pKeyInfo->enc; |
mem1.db = pKeyInfo->db; |
/* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */ |
- VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */ |
+ VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */ |
/* Compilers may complain that mem1.u.i is potentially uninitialized. |
** We could initialize it, as shown here, to silence those complaints. |
- ** But in fact, mem1.u.i will never actually be used initialized, and doing |
+ ** But in fact, mem1.u.i will never actually be used uninitialized, and doing |
** the unnecessary initialization has a measurable negative performance |
** impact, since this routine is a very high runner. And so, we choose |
** to ignore the compiler warnings and leave this variable uninitialized. |
@@ -2938,16 +3221,27 @@ int sqlite3VdbeRecordCompare( |
idx1 = getVarint32(aKey1, szHdr1); |
d1 = szHdr1; |
- if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){ |
- szHdr1--; |
- } |
- nField = pKeyInfo->nField; |
- while( idx1<szHdr1 && i<pPKey2->nField ){ |
+ assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB ); |
+ assert( pKeyInfo->aSortOrder!=0 ); |
+ assert( pKeyInfo->nField>0 ); |
+ assert( idx1<=szHdr1 || CORRUPT_DB ); |
+ do{ |
u32 serial_type1; |
/* Read the serial types for the next element in each key. */ |
idx1 += getVarint32( aKey1+idx1, serial_type1 ); |
- if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break; |
+ |
+ /* Verify that there is enough key space remaining to avoid |
+ ** a buffer overread. The "d1+serial_type1+2" subexpression will |
+ ** always be greater than or equal to the amount of required key space. |
+ ** Use that approximation to avoid the more expensive call to |
+ ** sqlite3VdbeSerialTypeLen() in the common case. |
+ */ |
+ if( d1+serial_type1+2>(u32)nKey1 |
+ && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1 |
+ ){ |
+ break; |
+ } |
/* Extract the values to be compared. |
*/ |
@@ -2955,58 +3249,623 @@ int sqlite3VdbeRecordCompare( |
/* Do the comparison |
*/ |
- rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], |
- i<nField ? pKeyInfo->aColl[i] : 0); |
+ rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]); |
if( rc!=0 ){ |
- assert( mem1.zMalloc==0 ); /* See comment below */ |
- |
- /* Invert the result if we are using DESC sort order. */ |
- if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){ |
- rc = -rc; |
+ assert( mem1.szMalloc==0 ); /* See comment below */ |
+ if( pKeyInfo->aSortOrder[i] ){ |
+ rc = -rc; /* Invert the result for DESC sort order. */ |
} |
- |
- /* If the PREFIX_SEARCH flag is set and all fields except the final |
- ** rowid field were equal, then clear the PREFIX_SEARCH flag and set |
- ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1). |
- ** This is used by the OP_IsUnique opcode. |
- */ |
- if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){ |
- assert( idx1==szHdr1 && rc ); |
- assert( mem1.flags & MEM_Int ); |
- pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH; |
- pPKey2->rowid = mem1.u.i; |
- } |
- |
- return rc; |
+ goto debugCompareEnd; |
} |
i++; |
- } |
+ }while( idx1<szHdr1 && i<pPKey2->nField ); |
/* No memory allocation is ever used on mem1. Prove this using |
** the following assert(). If the assert() fails, it indicates a |
** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). |
*/ |
- assert( mem1.zMalloc==0 ); |
+ assert( mem1.szMalloc==0 ); |
/* rc==0 here means that one of the keys ran out of fields and |
- ** all the fields up to that point were equal. If the UNPACKED_INCRKEY |
- ** flag is set, then break the tie by treating key2 as larger. |
- ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes |
- ** are considered to be equal. Otherwise, the longer key is the |
- ** larger. As it happens, the pPKey2 will always be the longer |
- ** if there is a difference. |
- */ |
- assert( rc==0 ); |
- if( pPKey2->flags & UNPACKED_INCRKEY ){ |
- rc = -1; |
- }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){ |
- /* Leave rc==0 */ |
- }else if( idx1<szHdr1 ){ |
- rc = 1; |
+ ** all the fields up to that point were equal. Return the default_rc |
+ ** value. */ |
+ rc = pPKey2->default_rc; |
+ |
+debugCompareEnd: |
+ if( desiredResult==0 && rc==0 ) return 1; |
+ if( desiredResult<0 && rc<0 ) return 1; |
+ if( desiredResult>0 && rc>0 ) return 1; |
+ if( CORRUPT_DB ) return 1; |
+ if( pKeyInfo->db->mallocFailed ) return 1; |
+ return 0; |
+} |
+#endif |
+ |
+/* |
+** Both *pMem1 and *pMem2 contain string values. Compare the two values |
+** using the collation sequence pColl. As usual, return a negative , zero |
+** or positive value if *pMem1 is less than, equal to or greater than |
+** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);". |
+*/ |
+static int vdbeCompareMemString( |
+ const Mem *pMem1, |
+ const Mem *pMem2, |
+ const CollSeq *pColl, |
+ u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */ |
+){ |
+ if( pMem1->enc==pColl->enc ){ |
+ /* The strings are already in the correct encoding. Call the |
+ ** comparison function directly */ |
+ return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z); |
+ }else{ |
+ int rc; |
+ const void *v1, *v2; |
+ int n1, n2; |
+ Mem c1; |
+ Mem c2; |
+ sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null); |
+ sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null); |
+ sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); |
+ sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); |
+ v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); |
+ n1 = v1==0 ? 0 : c1.n; |
+ v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); |
+ n2 = v2==0 ? 0 : c2.n; |
+ rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); |
+ sqlite3VdbeMemRelease(&c1); |
+ sqlite3VdbeMemRelease(&c2); |
+ if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM; |
+ return rc; |
} |
- return rc; |
} |
+ |
+/* |
+** Compare two blobs. Return negative, zero, or positive if the first |
+** is less than, equal to, or greater than the second, respectively. |
+** If one blob is a prefix of the other, then the shorter is the lessor. |
+*/ |
+static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){ |
+ int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n); |
+ if( c ) return c; |
+ return pB1->n - pB2->n; |
+} |
+ |
+ |
+/* |
+** Compare the values contained by the two memory cells, returning |
+** negative, zero or positive if pMem1 is less than, equal to, or greater |
+** than pMem2. Sorting order is NULL's first, followed by numbers (integers |
+** and reals) sorted numerically, followed by text ordered by the collating |
+** sequence pColl and finally blob's ordered by memcmp(). |
+** |
+** Two NULL values are considered equal by this function. |
+*/ |
+int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ |
+ int f1, f2; |
+ int combined_flags; |
+ |
+ f1 = pMem1->flags; |
+ f2 = pMem2->flags; |
+ combined_flags = f1|f2; |
+ assert( (combined_flags & MEM_RowSet)==0 ); |
+ /* If one value is NULL, it is less than the other. If both values |
+ ** are NULL, return 0. |
+ */ |
+ if( combined_flags&MEM_Null ){ |
+ return (f2&MEM_Null) - (f1&MEM_Null); |
+ } |
+ |
+ /* If one value is a number and the other is not, the number is less. |
+ ** If both are numbers, compare as reals if one is a real, or as integers |
+ ** if both values are integers. |
+ */ |
+ if( combined_flags&(MEM_Int|MEM_Real) ){ |
+ double r1, r2; |
+ if( (f1 & f2 & MEM_Int)!=0 ){ |
+ if( pMem1->u.i < pMem2->u.i ) return -1; |
+ if( pMem1->u.i > pMem2->u.i ) return 1; |
+ return 0; |
+ } |
+ if( (f1&MEM_Real)!=0 ){ |
+ r1 = pMem1->u.r; |
+ }else if( (f1&MEM_Int)!=0 ){ |
+ r1 = (double)pMem1->u.i; |
+ }else{ |
+ return 1; |
+ } |
+ if( (f2&MEM_Real)!=0 ){ |
+ r2 = pMem2->u.r; |
+ }else if( (f2&MEM_Int)!=0 ){ |
+ r2 = (double)pMem2->u.i; |
+ }else{ |
+ return -1; |
+ } |
+ if( r1<r2 ) return -1; |
+ if( r1>r2 ) return 1; |
+ return 0; |
+ } |
+ |
+ /* If one value is a string and the other is a blob, the string is less. |
+ ** If both are strings, compare using the collating functions. |
+ */ |
+ if( combined_flags&MEM_Str ){ |
+ if( (f1 & MEM_Str)==0 ){ |
+ return 1; |
+ } |
+ if( (f2 & MEM_Str)==0 ){ |
+ return -1; |
+ } |
+ |
+ assert( pMem1->enc==pMem2->enc ); |
+ assert( pMem1->enc==SQLITE_UTF8 || |
+ pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); |
+ |
+ /* The collation sequence must be defined at this point, even if |
+ ** the user deletes the collation sequence after the vdbe program is |
+ ** compiled (this was not always the case). |
+ */ |
+ assert( !pColl || pColl->xCmp ); |
+ |
+ if( pColl ){ |
+ return vdbeCompareMemString(pMem1, pMem2, pColl, 0); |
+ } |
+ /* If a NULL pointer was passed as the collate function, fall through |
+ ** to the blob case and use memcmp(). */ |
+ } |
+ |
+ /* Both values must be blobs. Compare using memcmp(). */ |
+ return sqlite3BlobCompare(pMem1, pMem2); |
+} |
+ |
+ |
+/* |
+** The first argument passed to this function is a serial-type that |
+** corresponds to an integer - all values between 1 and 9 inclusive |
+** except 7. The second points to a buffer containing an integer value |
+** serialized according to serial_type. This function deserializes |
+** and returns the value. |
+*/ |
+static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){ |
+ u32 y; |
+ assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) ); |
+ switch( serial_type ){ |
+ case 0: |
+ case 1: |
+ testcase( aKey[0]&0x80 ); |
+ return ONE_BYTE_INT(aKey); |
+ case 2: |
+ testcase( aKey[0]&0x80 ); |
+ return TWO_BYTE_INT(aKey); |
+ case 3: |
+ testcase( aKey[0]&0x80 ); |
+ return THREE_BYTE_INT(aKey); |
+ case 4: { |
+ testcase( aKey[0]&0x80 ); |
+ y = FOUR_BYTE_UINT(aKey); |
+ return (i64)*(int*)&y; |
+ } |
+ case 5: { |
+ testcase( aKey[0]&0x80 ); |
+ return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); |
+ } |
+ case 6: { |
+ u64 x = FOUR_BYTE_UINT(aKey); |
+ testcase( aKey[0]&0x80 ); |
+ x = (x<<32) | FOUR_BYTE_UINT(aKey+4); |
+ return (i64)*(i64*)&x; |
+ } |
+ } |
+ |
+ return (serial_type - 8); |
+} |
+ |
+/* |
+** This function compares the two table rows or index records |
+** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero |
+** or positive integer if key1 is less than, equal to or |
+** greater than key2. The {nKey1, pKey1} key must be a blob |
+** created by the OP_MakeRecord opcode of the VDBE. The pPKey2 |
+** key must be a parsed key such as obtained from |
+** sqlite3VdbeParseRecord. |
+** |
+** If argument bSkip is non-zero, it is assumed that the caller has already |
+** determined that the first fields of the keys are equal. |
+** |
+** Key1 and Key2 do not have to contain the same number of fields. If all |
+** fields that appear in both keys are equal, then pPKey2->default_rc is |
+** returned. |
+** |
+** If database corruption is discovered, set pPKey2->errCode to |
+** SQLITE_CORRUPT and return 0. If an OOM error is encountered, |
+** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the |
+** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db). |
+*/ |
+static int vdbeRecordCompareWithSkip( |
+ int nKey1, const void *pKey1, /* Left key */ |
+ UnpackedRecord *pPKey2, /* Right key */ |
+ int bSkip /* If true, skip the first field */ |
+){ |
+ u32 d1; /* Offset into aKey[] of next data element */ |
+ int i; /* Index of next field to compare */ |
+ u32 szHdr1; /* Size of record header in bytes */ |
+ u32 idx1; /* Offset of first type in header */ |
+ int rc = 0; /* Return value */ |
+ Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */ |
+ KeyInfo *pKeyInfo = pPKey2->pKeyInfo; |
+ const unsigned char *aKey1 = (const unsigned char *)pKey1; |
+ Mem mem1; |
+ |
+ /* If bSkip is true, then the caller has already determined that the first |
+ ** two elements in the keys are equal. Fix the various stack variables so |
+ ** that this routine begins comparing at the second field. */ |
+ if( bSkip ){ |
+ u32 s1; |
+ idx1 = 1 + getVarint32(&aKey1[1], s1); |
+ szHdr1 = aKey1[0]; |
+ d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1); |
+ i = 1; |
+ pRhs++; |
+ }else{ |
+ idx1 = getVarint32(aKey1, szHdr1); |
+ d1 = szHdr1; |
+ if( d1>(unsigned)nKey1 ){ |
+ pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; |
+ return 0; /* Corruption */ |
+ } |
+ i = 0; |
+ } |
+ |
+ VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */ |
+ assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField |
+ || CORRUPT_DB ); |
+ assert( pPKey2->pKeyInfo->aSortOrder!=0 ); |
+ assert( pPKey2->pKeyInfo->nField>0 ); |
+ assert( idx1<=szHdr1 || CORRUPT_DB ); |
+ do{ |
+ u32 serial_type; |
+ |
+ /* RHS is an integer */ |
+ if( pRhs->flags & MEM_Int ){ |
+ serial_type = aKey1[idx1]; |
+ testcase( serial_type==12 ); |
+ if( serial_type>=12 ){ |
+ rc = +1; |
+ }else if( serial_type==0 ){ |
+ rc = -1; |
+ }else if( serial_type==7 ){ |
+ double rhs = (double)pRhs->u.i; |
+ sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); |
+ if( mem1.u.r<rhs ){ |
+ rc = -1; |
+ }else if( mem1.u.r>rhs ){ |
+ rc = +1; |
+ } |
+ }else{ |
+ i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]); |
+ i64 rhs = pRhs->u.i; |
+ if( lhs<rhs ){ |
+ rc = -1; |
+ }else if( lhs>rhs ){ |
+ rc = +1; |
+ } |
+ } |
+ } |
+ |
+ /* RHS is real */ |
+ else if( pRhs->flags & MEM_Real ){ |
+ serial_type = aKey1[idx1]; |
+ if( serial_type>=12 ){ |
+ rc = +1; |
+ }else if( serial_type==0 ){ |
+ rc = -1; |
+ }else{ |
+ double rhs = pRhs->u.r; |
+ double lhs; |
+ sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); |
+ if( serial_type==7 ){ |
+ lhs = mem1.u.r; |
+ }else{ |
+ lhs = (double)mem1.u.i; |
+ } |
+ if( lhs<rhs ){ |
+ rc = -1; |
+ }else if( lhs>rhs ){ |
+ rc = +1; |
+ } |
+ } |
+ } |
+ |
+ /* RHS is a string */ |
+ else if( pRhs->flags & MEM_Str ){ |
+ getVarint32(&aKey1[idx1], serial_type); |
+ testcase( serial_type==12 ); |
+ if( serial_type<12 ){ |
+ rc = -1; |
+ }else if( !(serial_type & 0x01) ){ |
+ rc = +1; |
+ }else{ |
+ mem1.n = (serial_type - 12) / 2; |
+ testcase( (d1+mem1.n)==(unsigned)nKey1 ); |
+ testcase( (d1+mem1.n+1)==(unsigned)nKey1 ); |
+ if( (d1+mem1.n) > (unsigned)nKey1 ){ |
+ pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; |
+ return 0; /* Corruption */ |
+ }else if( pKeyInfo->aColl[i] ){ |
+ mem1.enc = pKeyInfo->enc; |
+ mem1.db = pKeyInfo->db; |
+ mem1.flags = MEM_Str; |
+ mem1.z = (char*)&aKey1[d1]; |
+ rc = vdbeCompareMemString( |
+ &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode |
+ ); |
+ }else{ |
+ int nCmp = MIN(mem1.n, pRhs->n); |
+ rc = memcmp(&aKey1[d1], pRhs->z, nCmp); |
+ if( rc==0 ) rc = mem1.n - pRhs->n; |
+ } |
+ } |
+ } |
+ |
+ /* RHS is a blob */ |
+ else if( pRhs->flags & MEM_Blob ){ |
+ getVarint32(&aKey1[idx1], serial_type); |
+ testcase( serial_type==12 ); |
+ if( serial_type<12 || (serial_type & 0x01) ){ |
+ rc = -1; |
+ }else{ |
+ int nStr = (serial_type - 12) / 2; |
+ testcase( (d1+nStr)==(unsigned)nKey1 ); |
+ testcase( (d1+nStr+1)==(unsigned)nKey1 ); |
+ if( (d1+nStr) > (unsigned)nKey1 ){ |
+ pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; |
+ return 0; /* Corruption */ |
+ }else{ |
+ int nCmp = MIN(nStr, pRhs->n); |
+ rc = memcmp(&aKey1[d1], pRhs->z, nCmp); |
+ if( rc==0 ) rc = nStr - pRhs->n; |
+ } |
+ } |
+ } |
+ |
+ /* RHS is null */ |
+ else{ |
+ serial_type = aKey1[idx1]; |
+ rc = (serial_type!=0); |
+ } |
+ |
+ if( rc!=0 ){ |
+ if( pKeyInfo->aSortOrder[i] ){ |
+ rc = -rc; |
+ } |
+ assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) ); |
+ assert( mem1.szMalloc==0 ); /* See comment below */ |
+ return rc; |
+ } |
+ |
+ i++; |
+ pRhs++; |
+ d1 += sqlite3VdbeSerialTypeLen(serial_type); |
+ idx1 += sqlite3VarintLen(serial_type); |
+ }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 ); |
+ |
+ /* No memory allocation is ever used on mem1. Prove this using |
+ ** the following assert(). If the assert() fails, it indicates a |
+ ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */ |
+ assert( mem1.szMalloc==0 ); |
+ |
+ /* rc==0 here means that one or both of the keys ran out of fields and |
+ ** all the fields up to that point were equal. Return the default_rc |
+ ** value. */ |
+ assert( CORRUPT_DB |
+ || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc) |
+ || pKeyInfo->db->mallocFailed |
+ ); |
+ return pPKey2->default_rc; |
+} |
+int sqlite3VdbeRecordCompare( |
+ int nKey1, const void *pKey1, /* Left key */ |
+ UnpackedRecord *pPKey2 /* Right key */ |
+){ |
+ return vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0); |
+} |
+ |
+ |
+/* |
+** This function is an optimized version of sqlite3VdbeRecordCompare() |
+** that (a) the first field of pPKey2 is an integer, and (b) the |
+** size-of-header varint at the start of (pKey1/nKey1) fits in a single |
+** byte (i.e. is less than 128). |
+** |
+** To avoid concerns about buffer overreads, this routine is only used |
+** on schemas where the maximum valid header size is 63 bytes or less. |
+*/ |
+static int vdbeRecordCompareInt( |
+ int nKey1, const void *pKey1, /* Left key */ |
+ UnpackedRecord *pPKey2 /* Right key */ |
+){ |
+ const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F]; |
+ int serial_type = ((const u8*)pKey1)[1]; |
+ int res; |
+ u32 y; |
+ u64 x; |
+ i64 v = pPKey2->aMem[0].u.i; |
+ i64 lhs; |
+ |
+ assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB ); |
+ switch( serial_type ){ |
+ case 1: { /* 1-byte signed integer */ |
+ lhs = ONE_BYTE_INT(aKey); |
+ testcase( lhs<0 ); |
+ break; |
+ } |
+ case 2: { /* 2-byte signed integer */ |
+ lhs = TWO_BYTE_INT(aKey); |
+ testcase( lhs<0 ); |
+ break; |
+ } |
+ case 3: { /* 3-byte signed integer */ |
+ lhs = THREE_BYTE_INT(aKey); |
+ testcase( lhs<0 ); |
+ break; |
+ } |
+ case 4: { /* 4-byte signed integer */ |
+ y = FOUR_BYTE_UINT(aKey); |
+ lhs = (i64)*(int*)&y; |
+ testcase( lhs<0 ); |
+ break; |
+ } |
+ case 5: { /* 6-byte signed integer */ |
+ lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); |
+ testcase( lhs<0 ); |
+ break; |
+ } |
+ case 6: { /* 8-byte signed integer */ |
+ x = FOUR_BYTE_UINT(aKey); |
+ x = (x<<32) | FOUR_BYTE_UINT(aKey+4); |
+ lhs = *(i64*)&x; |
+ testcase( lhs<0 ); |
+ break; |
+ } |
+ case 8: |
+ lhs = 0; |
+ break; |
+ case 9: |
+ lhs = 1; |
+ break; |
+ |
+ /* This case could be removed without changing the results of running |
+ ** this code. Including it causes gcc to generate a faster switch |
+ ** statement (since the range of switch targets now starts at zero and |
+ ** is contiguous) but does not cause any duplicate code to be generated |
+ ** (as gcc is clever enough to combine the two like cases). Other |
+ ** compilers might be similar. */ |
+ case 0: case 7: |
+ return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); |
+ |
+ default: |
+ return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); |
+ } |
+ |
+ if( v>lhs ){ |
+ res = pPKey2->r1; |
+ }else if( v<lhs ){ |
+ res = pPKey2->r2; |
+ }else if( pPKey2->nField>1 ){ |
+ /* The first fields of the two keys are equal. Compare the trailing |
+ ** fields. */ |
+ res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); |
+ }else{ |
+ /* The first fields of the two keys are equal and there are no trailing |
+ ** fields. Return pPKey2->default_rc in this case. */ |
+ res = pPKey2->default_rc; |
+ } |
+ |
+ assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) ); |
+ return res; |
+} |
+ |
+/* |
+** This function is an optimized version of sqlite3VdbeRecordCompare() |
+** that (a) the first field of pPKey2 is a string, that (b) the first field |
+** uses the collation sequence BINARY and (c) that the size-of-header varint |
+** at the start of (pKey1/nKey1) fits in a single byte. |
+*/ |
+static int vdbeRecordCompareString( |
+ int nKey1, const void *pKey1, /* Left key */ |
+ UnpackedRecord *pPKey2 /* Right key */ |
+){ |
+ const u8 *aKey1 = (const u8*)pKey1; |
+ int serial_type; |
+ int res; |
+ |
+ getVarint32(&aKey1[1], serial_type); |
+ if( serial_type<12 ){ |
+ res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */ |
+ }else if( !(serial_type & 0x01) ){ |
+ res = pPKey2->r2; /* (pKey1/nKey1) is a blob */ |
+ }else{ |
+ int nCmp; |
+ int nStr; |
+ int szHdr = aKey1[0]; |
+ |
+ nStr = (serial_type-12) / 2; |
+ if( (szHdr + nStr) > nKey1 ){ |
+ pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; |
+ return 0; /* Corruption */ |
+ } |
+ nCmp = MIN( pPKey2->aMem[0].n, nStr ); |
+ res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp); |
+ |
+ if( res==0 ){ |
+ res = nStr - pPKey2->aMem[0].n; |
+ if( res==0 ){ |
+ if( pPKey2->nField>1 ){ |
+ res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); |
+ }else{ |
+ res = pPKey2->default_rc; |
+ } |
+ }else if( res>0 ){ |
+ res = pPKey2->r2; |
+ }else{ |
+ res = pPKey2->r1; |
+ } |
+ }else if( res>0 ){ |
+ res = pPKey2->r2; |
+ }else{ |
+ res = pPKey2->r1; |
+ } |
+ } |
+ |
+ assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) |
+ || CORRUPT_DB |
+ || pPKey2->pKeyInfo->db->mallocFailed |
+ ); |
+ return res; |
+} |
+ |
+/* |
+** Return a pointer to an sqlite3VdbeRecordCompare() compatible function |
+** suitable for comparing serialized records to the unpacked record passed |
+** as the only argument. |
+*/ |
+RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){ |
+ /* varintRecordCompareInt() and varintRecordCompareString() both assume |
+ ** that the size-of-header varint that occurs at the start of each record |
+ ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt() |
+ ** also assumes that it is safe to overread a buffer by at least the |
+ ** maximum possible legal header size plus 8 bytes. Because there is |
+ ** guaranteed to be at least 74 (but not 136) bytes of padding following each |
+ ** buffer passed to varintRecordCompareInt() this makes it convenient to |
+ ** limit the size of the header to 64 bytes in cases where the first field |
+ ** is an integer. |
+ ** |
+ ** The easiest way to enforce this limit is to consider only records with |
+ ** 13 fields or less. If the first field is an integer, the maximum legal |
+ ** header size is (12*5 + 1 + 1) bytes. */ |
+ if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){ |
+ int flags = p->aMem[0].flags; |
+ if( p->pKeyInfo->aSortOrder[0] ){ |
+ p->r1 = 1; |
+ p->r2 = -1; |
+ }else{ |
+ p->r1 = -1; |
+ p->r2 = 1; |
+ } |
+ if( (flags & MEM_Int) ){ |
+ return vdbeRecordCompareInt; |
+ } |
+ testcase( flags & MEM_Real ); |
+ testcase( flags & MEM_Null ); |
+ testcase( flags & MEM_Blob ); |
+ if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){ |
+ assert( flags & MEM_Str ); |
+ return vdbeRecordCompareString; |
+ } |
+ } |
+ |
+ return sqlite3VdbeRecordCompare; |
+} |
/* |
** pCur points at an index entry created using the OP_MakeRecord opcode. |
@@ -3024,21 +3883,19 @@ int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){ |
u32 lenRowid; /* Size of the rowid */ |
Mem m, v; |
- UNUSED_PARAMETER(db); |
- |
/* Get the size of the index entry. Only indices entries of less |
** than 2GiB are support - anything large must be database corruption. |
** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so |
** this code can safely assume that nCellKey is 32-bits |
*/ |
assert( sqlite3BtreeCursorIsValid(pCur) ); |
- rc = sqlite3BtreeKeySize(pCur, &nCellKey); |
+ VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); |
assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ |
assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey ); |
/* Read in the complete content of the index entry */ |
- memset(&m, 0, sizeof(m)); |
- rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m); |
+ sqlite3VdbeMemInit(&m, db, 0); |
+ rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m); |
if( rc ){ |
return rc; |
} |
@@ -3080,7 +3937,7 @@ int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){ |
/* Jump here if database corruption is detected after m has been |
** allocated. Free the m object and return SQLITE_CORRUPT. */ |
idx_rowid_corruption: |
- testcase( m.zMalloc!=0 ); |
+ testcase( m.szMalloc!=0 ); |
sqlite3VdbeMemRelease(&m); |
return SQLITE_CORRUPT_BKPT; |
} |
@@ -3097,9 +3954,10 @@ idx_rowid_corruption: |
** of the keys prior to the final rowid, not the entire key. |
*/ |
int sqlite3VdbeIdxKeyCompare( |
- VdbeCursor *pC, /* The cursor to compare against */ |
- UnpackedRecord *pUnpacked, /* Unpacked version of key to compare against */ |
- int *res /* Write the comparison result here */ |
+ sqlite3 *db, /* Database connection */ |
+ VdbeCursor *pC, /* The cursor to compare against */ |
+ UnpackedRecord *pUnpacked, /* Unpacked version of key */ |
+ int *res /* Write the comparison result here */ |
){ |
i64 nCellKey = 0; |
int rc; |
@@ -3107,20 +3965,19 @@ int sqlite3VdbeIdxKeyCompare( |
Mem m; |
assert( sqlite3BtreeCursorIsValid(pCur) ); |
- rc = sqlite3BtreeKeySize(pCur, &nCellKey); |
+ VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); |
assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ |
- /* nCellKey will always be between 0 and 0xffffffff because of the say |
+ /* nCellKey will always be between 0 and 0xffffffff because of the way |
** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ |
if( nCellKey<=0 || nCellKey>0x7fffffff ){ |
*res = 0; |
return SQLITE_CORRUPT_BKPT; |
} |
- memset(&m, 0, sizeof(m)); |
- rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m); |
+ sqlite3VdbeMemInit(&m, db, 0); |
+ rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m); |
if( rc ){ |
return rc; |
} |
- assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID ); |
*res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); |
sqlite3VdbeMemRelease(&m); |
return SQLITE_OK; |
@@ -3176,7 +4033,7 @@ sqlite3 *sqlite3VdbeDb(Vdbe *v){ |
** |
** The returned value must be freed by the caller using sqlite3ValueFree(). |
*/ |
-sqlite3_value *sqlite3VdbeGetValue(Vdbe *v, int iVar, u8 aff){ |
+sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){ |
assert( iVar>0 ); |
if( v ){ |
Mem *pMem = &v->aVar[iVar-1]; |
@@ -3185,7 +4042,6 @@ sqlite3_value *sqlite3VdbeGetValue(Vdbe *v, int iVar, u8 aff){ |
if( pRet ){ |
sqlite3VdbeMemCopy((Mem *)pRet, pMem); |
sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8); |
- sqlite3VdbeMemStoreType((Mem *)pRet); |
} |
return pRet; |
} |
@@ -3206,3 +4062,18 @@ void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){ |
v->expmask |= ((u32)1 << (iVar-1)); |
} |
} |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+/* |
+** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored |
+** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored |
+** in memory obtained from sqlite3DbMalloc). |
+*/ |
+void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){ |
+ sqlite3 *db = p->db; |
+ sqlite3DbFree(db, p->zErrMsg); |
+ p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg); |
+ sqlite3_free(pVtab->zErrMsg); |
+ pVtab->zErrMsg = 0; |
+} |
+#endif /* SQLITE_OMIT_VIRTUALTABLE */ |