#include "sqliteInt.h"
#include "vdbeInt.h"
static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef);
static void vdbeFreeOpArray(sqlite3 *, Op *, int);
Vdbe *sqlite3VdbeCreate(Parse *pParse){
sqlite3 *db = pParse->db;
Vdbe *p;
p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
if( p==0 ) return 0;
memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
p->db = db;
if( db->pVdbe ){
db->pVdbe->ppVPrev = &p->pVNext;
}
p->pVNext = db->pVdbe;
p->ppVPrev = &db->pVdbe;
db->pVdbe = p;
assert( p->eVdbeState==VDBE_INIT_STATE );
p->pParse = pParse;
pParse->pVdbe = p;
assert( pParse->aLabel==0 );
assert( pParse->nLabel==0 );
assert( p->nOpAlloc==0 );
assert( pParse->szOpAlloc==0 );
sqlite3VdbeAddOp2(p, OP_Init, 0, 1);
return p;
}
Parse *sqlite3VdbeParser(Vdbe *p){
return p->pParse;
}
void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
va_list ap;
sqlite3DbFree(p->db, p->zErrMsg);
va_start(ap, zFormat);
p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
va_end(ap);
}
void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){
if( p==0 ) return;
p->prepFlags = prepFlags;
if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
p->expmask = 0;
}
assert( p->zSql==0 );
p->zSql = sqlite3DbStrNDup(p->db, z, n);
}
#ifdef SQLITE_ENABLE_NORMALIZE
void sqlite3VdbeAddDblquoteStr(sqlite3 *db, Vdbe *p, const char *z){
if( p ){
int n = sqlite3Strlen30(z);
DblquoteStr *pStr = sqlite3DbMallocRawNN(db,
sizeof(*pStr)+n+1-sizeof(pStr->z));
if( pStr ){
pStr->pNextStr = p->pDblStr;
p->pDblStr = pStr;
memcpy(pStr->z, z, n+1);
}
}
}
#endif
#ifdef SQLITE_ENABLE_NORMALIZE
int sqlite3VdbeUsesDoubleQuotedString(
Vdbe *pVdbe,
const char *zId
){
DblquoteStr *pStr;
assert( zId!=0 );
if( pVdbe->pDblStr==0 ) return 0;
for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
if( strcmp(zId, pStr->z)==0 ) return 1;
}
return 0;
}
#endif
void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
Vdbe tmp, *pTmp, **ppTmp;
char *zTmp;
assert( pA->db==pB->db );
tmp = *pA;
*pA = *pB;
*pB = tmp;
pTmp = pA->pVNext;
pA->pVNext = pB->pVNext;
pB->pVNext = pTmp;
ppTmp = pA->ppVPrev;
pA->ppVPrev = pB->ppVPrev;
pB->ppVPrev = ppTmp;
zTmp = pA->zSql;
pA->zSql = pB->zSql;
pB->zSql = zTmp;
#ifdef SQLITE_ENABLE_NORMALIZE
zTmp = pA->zNormSql;
pA->zNormSql = pB->zNormSql;
pB->zNormSql = zTmp;
#endif
pB->expmask = pA->expmask;
pB->prepFlags = pA->prepFlags;
memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
}
static int growOpArray(Vdbe *v, int nOp){
VdbeOp *pNew;
Parse *p = v->pParse;
#ifdef SQLITE_TEST_REALLOC_STRESS
sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
: (sqlite3_int64)v->nOpAlloc+nOp);
#else
sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
: (sqlite3_int64)(1024/sizeof(Op)));
UNUSED_PARAMETER(nOp);
#endif
if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
sqlite3OomFault(p->db);
return SQLITE_NOMEM;
}
assert( nOp<=(int)(1024/sizeof(Op)) );
assert( nNew>=(v->nOpAlloc+nOp) );
pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
if( pNew ){
p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
v->nOpAlloc = p->szOpAlloc/sizeof(Op);
v->aOp = pNew;
}
return (pNew ? SQLITE_OK : SQLITE_NOMEM_BKPT);
}
#ifdef SQLITE_DEBUG
static void test_addop_breakpoint(int pc, Op *pOp){
static int n = 0;
(void)pc;
(void)pOp;
n++;
}
#endif
static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
assert( p->nOpAlloc<=p->nOp );
if( growOpArray(p, 1) ) return 1;
assert( p->nOpAlloc>p->nOp );
return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
}
int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
int i;
VdbeOp *pOp;
i = p->nOp;
assert( p->eVdbeState==VDBE_INIT_STATE );
assert( op>=0 && op<0xff );
if( p->nOpAlloc<=i ){
return growOp3(p, op, p1, p2, p3);
}
assert( p->aOp!=0 );
p->nOp++;
pOp = &p->aOp[i];
assert( pOp!=0 );
pOp->opcode = (u8)op;
pOp->p5 = 0;
pOp->p1 = p1;
pOp->p2 = p2;
pOp->p3 = p3;
pOp->p4.p = 0;
pOp->p4type = P4_NOTUSED;
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
pOp->zComment = 0;
#endif
#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
pOp->nExec = 0;
pOp->nCycle = 0;
#endif
#ifdef SQLITE_DEBUG
if( p->db->flags & SQLITE_VdbeAddopTrace ){
sqlite3VdbePrintOp(0, i, &p->aOp[i]);
test_addop_breakpoint(i, &p->aOp[i]);
}
#endif
#ifdef SQLITE_VDBE_COVERAGE
pOp->iSrcLine = 0;
#endif
return i;
}
int sqlite3VdbeAddOp0(Vdbe *p, int op){
return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
}
int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
}
int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
}
int sqlite3VdbeGoto(Vdbe *p, int iDest){
return sqlite3VdbeAddOp3(p, OP_Goto, 0, iDest, 0);
}
int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zStr, 0);
}
void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
va_list ap;
int i;
char c;
va_start(ap, zTypes);
for(i=0; (c = zTypes[i])!=0; i++){
if( c=='s' ){
const char *z = va_arg(ap, const char*);
sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest+i, 0, z, 0);
}else if( c=='i' ){
sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest+i);
}else{
goto skip_op_resultrow;
}
}
sqlite3VdbeAddOp2(p, OP_ResultRow, iDest, i);
skip_op_resultrow:
va_end(ap);
}
int sqlite3VdbeAddOp4(
Vdbe *p,
int op,
int p1,
int p2,
int p3,
const char *zP4,
int p4type
){
int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
sqlite3VdbeChangeP4(p, addr, zP4, p4type);
return addr;
}
int sqlite3VdbeAddFunctionCall(
Parse *pParse,
int p1,
int p2,
int p3,
int nArg,
const FuncDef *pFunc,
int eCallCtx
){
Vdbe *v = pParse->pVdbe;
int nByte;
int addr;
sqlite3_context *pCtx;
assert( v );
nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
if( pCtx==0 ){
assert( pParse->db->mallocFailed );
freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
return 0;
}
pCtx->pOut = 0;
pCtx->pFunc = (FuncDef*)pFunc;
pCtx->pVdbe = 0;
pCtx->isError = 0;
pCtx->argc = nArg;
pCtx->iOp = sqlite3VdbeCurrentAddr(v);
addr = sqlite3VdbeAddOp4(v, eCallCtx ? OP_PureFunc : OP_Function,
p1, p2, p3, (char*)pCtx, P4_FUNCCTX);
sqlite3VdbeChangeP5(v, eCallCtx & NC_SelfRef);
sqlite3MayAbort(pParse);
return addr;
}
int sqlite3VdbeAddOp4Dup8(
Vdbe *p,
int op,
int p1,
int p2,
int p3,
const u8 *zP4,
int p4type
){
char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
if( p4copy ) memcpy(p4copy, zP4, 8);
return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
}
#ifndef SQLITE_OMIT_EXPLAIN
int sqlite3VdbeExplainParent(Parse *pParse){
VdbeOp *pOp;
if( pParse->addrExplain==0 ) return 0;
pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
return pOp->p2;
}
#if defined(SQLITE_DEBUG)
void sqlite3ExplainBreakpoint(const char *z1, const char *z2){
(void)z1;
(void)z2;
}
#endif
int sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
int addr = 0;
#if !defined(SQLITE_DEBUG)
if( pParse->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
#endif
{
char *zMsg;
Vdbe *v;
va_list ap;
int iThis;
va_start(ap, zFmt);
zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
va_end(ap);
v = pParse->pVdbe;
iThis = v->nOp;
addr = sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
zMsg, P4_DYNAMIC);
sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
if( bPush){
pParse->addrExplain = iThis;
}
sqlite3VdbeScanStatus(v, iThis, 0, 0, 0, 0);
}
return addr;
}
void sqlite3VdbeExplainPop(Parse *pParse){
sqlite3ExplainBreakpoint("POP", 0);
pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
}
#endif
void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere, u16 p5){
int j;
sqlite3VdbeAddOp4(p, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
sqlite3VdbeChangeP5(p, p5);
for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
sqlite3MayAbort(p->pParse);
}
int sqlite3VdbeAddOp4Int(
Vdbe *p,
int op,
int p1,
int p2,
int p3,
int p4
){
int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
if( p->db->mallocFailed==0 ){
VdbeOp *pOp = &p->aOp[addr];
pOp->p4type = P4_INT32;
pOp->p4.i = p4;
}
return addr;
}
void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
v->pParse->nTempReg = 0;
v->pParse->nRangeReg = 0;
}
int sqlite3VdbeMakeLabel(Parse *pParse){
return --pParse->nLabel;
}
static SQLITE_NOINLINE void resizeResolveLabel(Parse *p, Vdbe *v, int j){
int nNewSize = 10 - p->nLabel;
p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
nNewSize*sizeof(p->aLabel[0]));
if( p->aLabel==0 ){
p->nLabelAlloc = 0;
}else{
#ifdef SQLITE_DEBUG
int i;
for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
#endif
if( nNewSize>=100 && (nNewSize/100)>(p->nLabelAlloc/100) ){
sqlite3ProgressCheck(p);
}
p->nLabelAlloc = nNewSize;
p->aLabel[j] = v->nOp;
}
}
void sqlite3VdbeResolveLabel(Vdbe *v, int x){
Parse *p = v->pParse;
int j = ADDR(x);
assert( v->eVdbeState==VDBE_INIT_STATE );
assert( j<-p->nLabel );
assert( j>=0 );
#ifdef SQLITE_DEBUG
if( p->db->flags & SQLITE_VdbeAddopTrace ){
printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
}
#endif
if( p->nLabelAlloc + p->nLabel < 0 ){
resizeResolveLabel(p,v,j);
}else{
assert( p->aLabel[j]==(-1) );
p->aLabel[j] = v->nOp;
}
}
void sqlite3VdbeRunOnlyOnce(Vdbe *p){
sqlite3VdbeAddOp2(p, OP_Expire, 1, 1);
}
void sqlite3VdbeReusable(Vdbe *p){
int i;
for(i=1; ALWAYS(i<p->nOp); i++){
if( ALWAYS(p->aOp[i].opcode==OP_Expire) ){
p->aOp[1].opcode = OP_Noop;
break;
}
}
}
#ifdef SQLITE_DEBUG
typedef struct VdbeOpIter VdbeOpIter;
struct VdbeOpIter {
Vdbe *v;
SubProgram **apSub;
int nSub;
int iAddr;
int iSub;
};
static Op *opIterNext(VdbeOpIter *p){
Vdbe *v = p->v;
Op *pRet = 0;
Op *aOp;
int nOp;
if( p->iSub<=p->nSub ){
if( p->iSub==0 ){
aOp = v->aOp;
nOp = v->nOp;
}else{
aOp = p->apSub[p->iSub-1]->aOp;
nOp = p->apSub[p->iSub-1]->nOp;
}
assert( p->iAddr<nOp );
pRet = &aOp[p->iAddr];
p->iAddr++;
if( p->iAddr==nOp ){
p->iSub++;
p->iAddr = 0;
}
if( pRet->p4type==P4_SUBPROGRAM ){
int nByte = (p->nSub+1)*sizeof(SubProgram*);
int j;
for(j=0; j<p->nSub; j++){
if( p->apSub[j]==pRet->p4.pProgram ) break;
}
if( j==p->nSub ){
p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
if( !p->apSub ){
pRet = 0;
}else{
p->apSub[p->nSub++] = pRet->p4.pProgram;
}
}
}
}
return pRet;
}
int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
int hasAbort = 0;
int hasFkCounter = 0;
int hasCreateTable = 0;
int hasCreateIndex = 0;
int hasInitCoroutine = 0;
Op *pOp;
VdbeOpIter sIter;
if( v==0 ) return 0;
memset(&sIter, 0, sizeof(sIter));
sIter.v = v;
while( (pOp = opIterNext(&sIter))!=0 ){
int opcode = pOp->opcode;
if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
|| opcode==OP_VDestroy
|| opcode==OP_VCreate
|| opcode==OP_ParseSchema
|| opcode==OP_Function || opcode==OP_PureFunc
|| ((opcode==OP_Halt || opcode==OP_HaltIfNull)
&& ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
){
hasAbort = 1;
break;
}
if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
if( mayAbort ){
if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
if( opcode==OP_Clear ) hasCreateIndex = 1;
}
if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
#ifndef SQLITE_OMIT_FOREIGN_KEY
if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
hasFkCounter = 1;
}
#endif
}
sqlite3DbFree(v->db, sIter.apSub);
return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
|| (hasCreateTable && hasInitCoroutine) || hasCreateIndex
);
}
#endif
#ifdef SQLITE_DEBUG
void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
if( pC==0
|| (pC->eCurType!=CURTYPE_SORTER
&& pC->eCurType!=CURTYPE_PSEUDO
&& !pC->isEphemeral)
){
p->nWrite++;
}
}
#endif
#ifdef SQLITE_DEBUG
void sqlite3VdbeAssertAbortable(Vdbe *p){
assert( p->nWrite==0 || p->usesStmtJournal );
}
#endif
static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
int nMaxArgs = *pMaxFuncArgs;
Op *pOp;
Parse *pParse = p->pParse;
int *aLabel = pParse->aLabel;
assert( pParse->db->mallocFailed==0 );
p->readOnly = 1;
p->bIsReader = 0;
pOp = &p->aOp[p->nOp-1];
assert( p->aOp[0].opcode==OP_Init );
while( 1 ){
if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
switch( pOp->opcode ){
case OP_Transaction: {
if( pOp->p2!=0 ) p->readOnly = 0;
deliberate_fall_through
}
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;
}
case OP_Init: {
assert( pOp->p2>=0 );
goto resolve_p2_values_loop_exit;
}
#ifndef SQLITE_OMIT_VIRTUALTABLE
case OP_VUpdate: {
if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
break;
}
case OP_VFilter: {
int n;
assert( (pOp - p->aOp) >= 3 );
assert( pOp[-1].opcode==OP_Integer );
n = pOp[-1].p1;
if( n>nMaxArgs ) nMaxArgs = n;
deliberate_fall_through
}
#endif
default: {
if( pOp->p2<0 ){
assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
assert( ADDR(pOp->p2)<-pParse->nLabel );
assert( aLabel!=0 );
pOp->p2 = aLabel[ADDR(pOp->p2)];
}
break;
}
}
assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
}
assert( pOp>p->aOp );
pOp--;
}
resolve_p2_values_loop_exit:
if( aLabel ){
sqlite3DbNNFreeNN(p->db, pParse->aLabel);
pParse->aLabel = 0;
}
pParse->nLabel = 0;
*pMaxFuncArgs = nMaxArgs;
assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
}
#ifdef SQLITE_DEBUG
void sqlite3VdbeNoJumpsOutsideSubrtn(
Vdbe *v,
int iFirst,
int iLast,
int iRetReg
){
VdbeOp *pOp;
Parse *pParse;
int i;
sqlite3_str *pErr = 0;
assert( v!=0 );
pParse = v->pParse;
assert( pParse!=0 );
if( pParse->nErr ) return;
assert( iLast>=iFirst );
assert( iLast<v->nOp );
pOp = &v->aOp[iFirst];
for(i=iFirst; i<=iLast; i++, pOp++){
if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 ){
int iDest = pOp->p2;
if( iDest==0 ) continue;
if( pOp->opcode==OP_Gosub ) continue;
if( iDest<0 ){
int j = ADDR(iDest);
assert( j>=0 );
if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){
continue;
}
iDest = pParse->aLabel[j];
}
if( iDest<iFirst || iDest>iLast ){
int j = iDest;
for(; j<v->nOp; j++){
VdbeOp *pX = &v->aOp[j];
if( pX->opcode==OP_Return ){
if( pX->p1==iRetReg ) break;
continue;
}
if( pX->opcode==OP_Noop ) continue;
if( pX->opcode==OP_Explain ) continue;
if( pErr==0 ){
pErr = sqlite3_str_new(0);
}else{
sqlite3_str_appendchar(pErr, 1, '\n');
}
sqlite3_str_appendf(pErr,
"Opcode at %d jumps to %d which is outside the "
"subroutine at %d..%d",
i, iDest, iFirst, iLast);
break;
}
}
}
}
if( pErr ){
char *zErr = sqlite3_str_finish(pErr);
sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_INTERNAL, OE_Abort, 0, zErr, 0);
sqlite3_free(zErr);
sqlite3MayAbort(pParse);
}
}
#endif
int sqlite3VdbeCurrentAddr(Vdbe *p){
assert( p->eVdbeState==VDBE_INIT_STATE );
return p->nOp;
}
#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
assert( p->nOp + N <= p->nOpAlloc );
}
#endif
#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
void sqlite3VdbeVerifyNoResultRow(Vdbe *p){
int i;
for(i=0; i<p->nOp; i++){
assert( p->aOp[i].opcode!=OP_ResultRow );
}
}
#endif
#if defined(SQLITE_DEBUG)
void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable);
}
#endif
VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
VdbeOp *aOp = p->aOp;
assert( aOp && !p->db->mallocFailed );
assert( DbMaskAllZero(p->btreeMask) );
resolveP2Values(p, pnMaxArg);
*pnOp = p->nOp;
p->aOp = 0;
return aOp;
}
VdbeOp *sqlite3VdbeAddOpList(
Vdbe *p,
int nOp,
VdbeOpList const *aOp,
int iLineno
){
int i;
VdbeOp *pOut, *pFirst;
assert( nOp>0 );
assert( p->eVdbeState==VDBE_INIT_STATE );
if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
return 0;
}
pFirst = pOut = &p->aOp[p->nOp];
for(i=0; i<nOp; i++, aOp++, pOut++){
pOut->opcode = aOp->opcode;
pOut->p1 = aOp->p1;
pOut->p2 = aOp->p2;
assert( aOp->p2>=0 );
if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
pOut->p2 += p->nOp;
}
pOut->p3 = aOp->p3;
pOut->p4type = P4_NOTUSED;
pOut->p4.p = 0;
pOut->p5 = 0;
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
pOut->zComment = 0;
#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+p->nOp, &p->aOp[i+p->nOp]);
}
#endif
}
p->nOp += nOp;
return pFirst;
}
#if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
void sqlite3VdbeScanStatus(
Vdbe *p,
int addrExplain,
int addrLoop,
int addrVisit,
LogEst nEst,
const char *zName
){
if( IS_STMT_SCANSTATUS(p->db) ){
sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
ScanStatus *aNew;
aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
if( aNew ){
ScanStatus *pNew = &aNew[p->nScan++];
memset(pNew, 0, sizeof(ScanStatus));
pNew->addrExplain = addrExplain;
pNew->addrLoop = addrLoop;
pNew->addrVisit = addrVisit;
pNew->nEst = nEst;
pNew->zName = sqlite3DbStrDup(p->db, zName);
p->aScan = aNew;
}
}
}
void sqlite3VdbeScanStatusRange(
Vdbe *p,
int addrExplain,
int addrStart,
int addrEnd
){
if( IS_STMT_SCANSTATUS(p->db) ){
ScanStatus *pScan = 0;
int ii;
for(ii=p->nScan-1; ii>=0; ii--){
pScan = &p->aScan[ii];
if( pScan->addrExplain==addrExplain ) break;
pScan = 0;
}
if( pScan ){
if( addrEnd<0 ) addrEnd = sqlite3VdbeCurrentAddr(p)-1;
for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){
if( pScan->aAddrRange[ii]==0 ){
pScan->aAddrRange[ii] = addrStart;
pScan->aAddrRange[ii+1] = addrEnd;
break;
}
}
}
}
}
void sqlite3VdbeScanStatusCounters(
Vdbe *p,
int addrExplain,
int addrLoop,
int addrVisit
){
if( IS_STMT_SCANSTATUS(p->db) ){
ScanStatus *pScan = 0;
int ii;
for(ii=p->nScan-1; ii>=0; ii--){
pScan = &p->aScan[ii];
if( pScan->addrExplain==addrExplain ) break;
pScan = 0;
}
if( pScan ){
pScan->addrLoop = addrLoop;
pScan->addrVisit = addrVisit;
}
}
}
#endif
void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){
assert( addr>=0 );
sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
}
void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
assert( addr>=0 );
sqlite3VdbeGetOp(p,addr)->p1 = val;
}
void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
assert( addr>=0 || p->db->mallocFailed );
sqlite3VdbeGetOp(p,addr)->p2 = val;
}
void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
assert( addr>=0 );
sqlite3VdbeGetOp(p,addr)->p3 = val;
}
void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
assert( p->nOp>0 || p->db->mallocFailed );
if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
}
void sqlite3VdbeTypeofColumn(Vdbe *p, int iDest){
VdbeOp *pOp = sqlite3VdbeGetLastOp(p);
if( pOp->p3==iDest && pOp->opcode==OP_Column ){
pOp->p5 |= OPFLAG_TYPEOFARG;
}
}
void sqlite3VdbeJumpHere(Vdbe *p, int addr){
sqlite3VdbeChangeP2(p, addr, p->nOp);
}
void sqlite3VdbeJumpHereOrPopInst(Vdbe *p, int addr){
if( addr==p->nOp-1 ){
assert( p->aOp[addr].opcode==OP_Once
|| p->aOp[addr].opcode==OP_If
|| p->aOp[addr].opcode==OP_FkIfZero );
assert( p->aOp[addr].p4type==0 );
#ifdef SQLITE_VDBE_COVERAGE
sqlite3VdbeGetLastOp(p)->iSrcLine = 0;
#endif
p->nOp--;
}else{
sqlite3VdbeChangeP2(p, addr, p->nOp);
}
}
static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
assert( db!=0 );
if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
sqlite3DbNNFreeNN(db, pDef);
}
}
static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
sqlite3DbNNFreeNN(db, p);
}
static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
assert( db!=0 );
freeEphemeralFunction(db, p->pFunc);
sqlite3DbNNFreeNN(db, p);
}
static void freeP4(sqlite3 *db, int p4type, void *p4){
assert( db );
switch( p4type ){
case P4_FUNCCTX: {
freeP4FuncCtx(db, (sqlite3_context*)p4);
break;
}
case P4_REAL:
case P4_INT64:
case P4_DYNAMIC:
case P4_INTARRAY: {
if( p4 ) sqlite3DbNNFreeNN(db, p4);
break;
}
case P4_KEYINFO: {
if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
break;
}
#ifdef SQLITE_ENABLE_CURSOR_HINTS
case P4_EXPR: {
sqlite3ExprDelete(db, (Expr*)p4);
break;
}
#endif
case P4_FUNCDEF: {
freeEphemeralFunction(db, (FuncDef*)p4);
break;
}
case P4_MEM: {
if( db->pnBytesFreed==0 ){
sqlite3ValueFree((sqlite3_value*)p4);
}else{
freeP4Mem(db, (Mem*)p4);
}
break;
}
case P4_VTAB : {
if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
break;
}
}
}
static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
assert( nOp>=0 );
assert( db!=0 );
if( aOp ){
Op *pOp = &aOp[nOp-1];
while(1){
if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
sqlite3DbFree(db, pOp->zComment);
#endif
if( pOp==aOp ) break;
pOp--;
}
sqlite3DbNNFreeNN(db, aOp);
}
}
void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
p->pNext = pVdbe->pProgram;
pVdbe->pProgram = p;
}
int sqlite3VdbeHasSubProgram(Vdbe *pVdbe){
return pVdbe->pProgram!=0;
}
int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
VdbeOp *pOp;
if( p->db->mallocFailed ) return 0;
assert( addr>=0 && addr<p->nOp );
pOp = &p->aOp[addr];
freeP4(p->db, pOp->p4type, pOp->p4.p);
pOp->p4type = P4_NOTUSED;
pOp->p4.z = 0;
pOp->opcode = OP_Noop;
return 1;
}
int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
return sqlite3VdbeChangeToNoop(p, p->nOp-1);
}else{
return 0;
}
}
#ifdef SQLITE_DEBUG
void sqlite3VdbeReleaseRegisters(
Parse *pParse,
int iFirst,
int N,
u32 mask,
int bUndefine
){
if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg) ) return;
assert( pParse->pVdbe );
assert( iFirst>=1 );
assert( iFirst+N-1<=pParse->nMem );
if( N<=31 && mask!=0 ){
while( N>0 && (mask&1)!=0 ){
mask >>= 1;
iFirst++;
N--;
}
while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
mask &= ~MASKBIT32(N-1);
N--;
}
}
if( N>0 ){
sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
}
}
#endif
static void SQLITE_NOINLINE vdbeChangeP4Full(
Vdbe *p,
Op *pOp,
const char *zP4,
int n
){
if( pOp->p4type ){
freeP4(p->db, pOp->p4type, pOp->p4.p);
pOp->p4type = 0;
pOp->p4.p = 0;
}
if( n<0 ){
sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
}else{
if( n==0 ) n = sqlite3Strlen30(zP4);
pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
pOp->p4type = P4_DYNAMIC;
}
}
void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
Op *pOp;
sqlite3 *db;
assert( p!=0 );
db = p->db;
assert( p->eVdbeState==VDBE_INIT_STATE );
assert( p->aOp!=0 || db->mallocFailed );
if( db->mallocFailed ){
if( n!=P4_VTAB ) freeP4(db, n, (void*)*(char**)&zP4);
return;
}
assert( p->nOp>0 );
assert( addr<p->nOp );
if( addr<0 ){
addr = p->nOp - 1;
}
pOp = &p->aOp[addr];
if( n>=0 || pOp->p4type ){
vdbeChangeP4Full(p, pOp, zP4, n);
return;
}
if( n==P4_INT32 ){
pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
pOp->p4type = P4_INT32;
}else if( zP4!=0 ){
assert( n<0 );
pOp->p4.p = (void*)zP4;
pOp->p4type = (signed char)n;
if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4);
}
}
void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
VdbeOp *pOp;
assert( n!=P4_INT32 && n!=P4_VTAB );
assert( n<=0 );
if( p->db->mallocFailed ){
freeP4(p->db, n, pP4);
}else{
assert( pP4!=0 || n==P4_DYNAMIC );
assert( p->nOp>0 );
pOp = &p->aOp[p->nOp-1];
assert( pOp->p4type==P4_NOTUSED );
pOp->p4type = n;
pOp->p4.p = pP4;
}
}
void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
Vdbe *v = pParse->pVdbe;
KeyInfo *pKeyInfo;
assert( v!=0 );
assert( pIdx!=0 );
pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
}
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
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->pParse->nErr>0 );
if( p->nOp ){
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);
vdbeVComment(p, zFormat, ap);
va_end(ap);
}
}
void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
va_list ap;
if( p ){
sqlite3VdbeAddOp0(p, OP_Noop);
va_start(ap, zFormat);
vdbeVComment(p, zFormat, ap);
va_end(ap);
}
}
#endif
#ifdef SQLITE_VDBE_COVERAGE
void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
sqlite3VdbeGetLastOp(v)->iSrcLine = iLine;
}
#endif
VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
static VdbeOp dummy;
assert( p->eVdbeState==VDBE_INIT_STATE );
assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
if( p->db->mallocFailed ){
return (VdbeOp*)&dummy;
}else{
return &p->aOp[addr];
}
}
VdbeOp *sqlite3VdbeGetLastOp(Vdbe *p){
return sqlite3VdbeGetOp(p, p->nOp - 1);
}
#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
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;
}
char *sqlite3VdbeDisplayComment(
sqlite3 *db,
const Op *pOp,
const char *zP4
){
const char *zOpName;
const char *zSynopsis;
int nOpName;
int ii;
char zAlt[50];
StrAccum x;
sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH);
zOpName = sqlite3OpcodeName(pOp->opcode);
nOpName = sqlite3Strlen30(zOpName);
if( zOpName[nOpName+1] ){
int seenCom = 0;
char c;
zSynopsis = zOpName + nOpName + 1;
if( strncmp(zSynopsis,"IF ",3)==0 ){
sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
zSynopsis = zAlt;
}
for(ii=0; (c = zSynopsis[ii])!=0; ii++){
if( c=='P' ){
c = zSynopsis[++ii];
if( c=='4' ){
sqlite3_str_appendall(&x, zP4);
}else if( c=='X' ){
if( pOp->zComment && pOp->zComment[0] ){
sqlite3_str_appendall(&x, pOp->zComment);
seenCom = 1;
break;
}
}else{
int v1 = translateP(c, pOp);
int v2;
if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
ii += 3;
v2 = translateP(zSynopsis[ii], pOp);
if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
ii += 2;
v2++;
}
if( v2<2 ){
sqlite3_str_appendf(&x, "%d", v1);
}else{
sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
}
}else if( strncmp(zSynopsis+ii+1, "@NP", 3)==0 ){
sqlite3_context *pCtx = pOp->p4.pCtx;
if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
sqlite3_str_appendf(&x, "%d", v1);
}else if( pCtx->argc>1 ){
sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
}else if( x.accError==0 ){
assert( x.nChar>2 );
x.nChar -= 2;
ii++;
}
ii += 3;
}else{
sqlite3_str_appendf(&x, "%d", v1);
if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
ii += 4;
}
}
}
}else{
sqlite3_str_appendchar(&x, 1, c);
}
}
if( !seenCom && pOp->zComment ){
sqlite3_str_appendf(&x, "; %s", pOp->zComment);
}
}else if( pOp->zComment ){
sqlite3_str_appendall(&x, pOp->zComment);
}
if( (x.accError & SQLITE_NOMEM)!=0 && db!=0 ){
sqlite3OomFault(db);
}
return sqlite3StrAccumFinish(&x);
}
#endif
#if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
static void displayP4Expr(StrAccum *p, Expr *pExpr){
const char *zOp = 0;
switch( pExpr->op ){
case TK_STRING:
assert( !ExprHasProperty(pExpr, EP_IntValue) );
sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
break;
case TK_INTEGER:
sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
break;
case TK_NULL:
sqlite3_str_appendf(p, "NULL");
break;
case TK_REGISTER: {
sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
break;
}
case TK_COLUMN: {
if( pExpr->iColumn<0 ){
sqlite3_str_appendf(p, "rowid");
}else{
sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
}
break;
}
case TK_LT: zOp = "LT"; break;
case TK_LE: zOp = "LE"; break;
case TK_GT: zOp = "GT"; break;
case TK_GE: zOp = "GE"; break;
case TK_NE: zOp = "NE"; break;
case TK_EQ: zOp = "EQ"; break;
case TK_IS: zOp = "IS"; break;
case TK_ISNOT: zOp = "ISNOT"; break;
case TK_AND: zOp = "AND"; break;
case TK_OR: zOp = "OR"; break;
case TK_PLUS: zOp = "ADD"; break;
case TK_STAR: zOp = "MUL"; break;
case TK_MINUS: zOp = "SUB"; break;
case TK_REM: zOp = "REM"; break;
case TK_BITAND: zOp = "BITAND"; break;
case TK_BITOR: zOp = "BITOR"; break;
case TK_SLASH: zOp = "DIV"; break;
case TK_LSHIFT: zOp = "LSHIFT"; break;
case TK_RSHIFT: zOp = "RSHIFT"; break;
case TK_CONCAT: zOp = "CONCAT"; break;
case TK_UMINUS: zOp = "MINUS"; break;
case TK_UPLUS: zOp = "PLUS"; break;
case TK_BITNOT: zOp = "BITNOT"; break;
case TK_NOT: zOp = "NOT"; break;
case TK_ISNULL: zOp = "ISNULL"; break;
case TK_NOTNULL: zOp = "NOTNULL"; break;
default:
sqlite3_str_appendf(p, "%s", "expr");
break;
}
if( zOp ){
sqlite3_str_appendf(p, "%s(", zOp);
displayP4Expr(p, pExpr->pLeft);
if( pExpr->pRight ){
sqlite3_str_append(p, ",", 1);
displayP4Expr(p, pExpr->pRight);
}
sqlite3_str_append(p, ")", 1);
}
}
#endif
#if VDBE_DISPLAY_P4
char *sqlite3VdbeDisplayP4(sqlite3 *db, Op *pOp){
char *zP4 = 0;
StrAccum x;
sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH);
switch( pOp->p4type ){
case P4_KEYINFO: {
int j;
KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
assert( pKeyInfo->aSortFlags!=0 );
sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
for(j=0; j<pKeyInfo->nKeyField; j++){
CollSeq *pColl = pKeyInfo->aColl[j];
const char *zColl = pColl ? pColl->zName : "";
if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
sqlite3_str_appendf(&x, ",%s%s%s",
(pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
(pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
zColl);
}
sqlite3_str_append(&x, ")", 1);
break;
}
#ifdef SQLITE_ENABLE_CURSOR_HINTS
case P4_EXPR: {
displayP4Expr(&x, pOp->p4.pExpr);
break;
}
#endif
case P4_COLLSEQ: {
static const char *const encnames[] = {"?", "8", "16LE", "16BE"};
CollSeq *pColl = pOp->p4.pColl;
assert( pColl->enc<4 );
sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
encnames[pColl->enc]);
break;
}
case P4_FUNCDEF: {
FuncDef *pDef = pOp->p4.pFunc;
sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
break;
}
case P4_FUNCCTX: {
FuncDef *pDef = pOp->p4.pCtx->pFunc;
sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
break;
}
case P4_INT64: {
sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
break;
}
case P4_INT32: {
sqlite3_str_appendf(&x, "%d", pOp->p4.i);
break;
}
case P4_REAL: {
sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
break;
}
case P4_MEM: {
Mem *pMem = pOp->p4.pMem;
if( pMem->flags & MEM_Str ){
zP4 = pMem->z;
}else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
sqlite3_str_appendf(&x, "%lld", pMem->u.i);
}else if( pMem->flags & MEM_Real ){
sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
}else if( pMem->flags & MEM_Null ){
zP4 = "NULL";
}else{
assert( pMem->flags & MEM_Blob );
zP4 = "(blob)";
}
break;
}
#ifndef SQLITE_OMIT_VIRTUALTABLE
case P4_VTAB: {
sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
sqlite3_str_appendf(&x, "vtab:%p", pVtab);
break;
}
#endif
case P4_INTARRAY: {
u32 i;
u32 *ai = pOp->p4.ai;
u32 n = ai[0];
for(i=1; i<=n; i++){
sqlite3_str_appendf(&x, "%c%u", (i==1 ? '[' : ','), ai[i]);
}
sqlite3_str_append(&x, "]", 1);
break;
}
case P4_SUBPROGRAM: {
zP4 = "program";
break;
}
case P4_TABLE: {
zP4 = pOp->p4.pTab->zName;
break;
}
default: {
zP4 = pOp->p4.z;
}
}
if( zP4 ) sqlite3_str_appendall(&x, zP4);
if( (x.accError & SQLITE_NOMEM)!=0 ){
sqlite3OomFault(db);
}
return sqlite3StrAccumFinish(&x);
}
#endif
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 );
DbMaskSet(p->btreeMask, i);
if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
DbMaskSet(p->lockMask, i);
}
}
#if !defined(SQLITE_OMIT_SHARED_CACHE)
void sqlite3VdbeEnter(Vdbe *p){
int i;
sqlite3 *db;
Db *aDb;
int nDb;
if( DbMaskAllZero(p->lockMask) ) return;
db = p->db;
aDb = db->aDb;
nDb = db->nDb;
for(i=0; i<nDb; i++){
if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
sqlite3BtreeEnter(aDb[i].pBt);
}
}
}
#endif
#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
int i;
sqlite3 *db;
Db *aDb;
int nDb;
db = p->db;
aDb = db->aDb;
nDb = db->nDb;
for(i=0; i<nDb; i++){
if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
sqlite3BtreeLeave(aDb[i].pBt);
}
}
}
void sqlite3VdbeLeave(Vdbe *p){
if( DbMaskAllZero(p->lockMask) ) return;
vdbeLeave(p);
}
#endif
#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){
char *zP4;
char *zCom;
sqlite3 dummyDb;
static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
if( pOut==0 ) pOut = stdout;
sqlite3BeginBenignMalloc();
dummyDb.mallocFailed = 1;
zP4 = sqlite3VdbeDisplayP4(&dummyDb, pOp);
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
zCom = sqlite3VdbeDisplayComment(0, pOp, zP4);
#else
zCom = 0;
#endif
fprintf(pOut, zFormat1, pc,
sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
zP4 ? zP4 : "", pOp->p5,
zCom ? zCom : ""
);
fflush(pOut);
sqlite3_free(zP4);
sqlite3_free(zCom);
sqlite3EndBenignMalloc();
}
#endif
static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
if( N>0 ){
do{
p->flags = flags;
p->db = db;
p->szMalloc = 0;
#ifdef SQLITE_DEBUG
p->pScopyFrom = 0;
#endif
p++;
}while( (--N)>0 );
}
}
static void releaseMemArray(Mem *p, int N){
if( p && N ){
Mem *pEnd = &p[N];
sqlite3 *db = p->db;
if( db->pnBytesFreed ){
do{
if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
}while( (++p)<pEnd );
return;
}
do{
assert( (&p[1])==pEnd || p[0].db==p[1].db );
assert( sqlite3VdbeCheckMemInvariants(p) );
testcase( p->flags & MEM_Agg );
testcase( p->flags & MEM_Dyn );
if( p->flags&(MEM_Agg|MEM_Dyn) ){
testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
sqlite3VdbeMemRelease(p);
p->flags = MEM_Undefined;
}else if( p->szMalloc ){
sqlite3DbNNFreeNN(db, p->zMalloc);
p->szMalloc = 0;
p->flags = MEM_Undefined;
}
#ifdef SQLITE_DEBUG
else{
p->flags = MEM_Undefined;
}
#endif
}while( (++p)<pEnd );
}
}
#ifdef SQLITE_DEBUG
int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){
if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
return 1;
}
#endif
void sqlite3VdbeFrameMemDel(void *pArg){
VdbeFrame *pFrame = (VdbeFrame*)pArg;
assert( sqlite3VdbeFrameIsValid(pFrame) );
pFrame->pParent = pFrame->v->pDelFrame;
pFrame->v->pDelFrame = pFrame;
}
#if defined(SQLITE_ENABLE_BYTECODE_VTAB) || !defined(SQLITE_OMIT_EXPLAIN)
int sqlite3VdbeNextOpcode(
Vdbe *p,
Mem *pSub,
int eMode,
int *piPc,
int *piAddr,
Op **paOp
){
int nRow;
int nSub = 0;
SubProgram **apSub = 0;
int i;
int rc = SQLITE_OK;
Op *aOp = 0;
int iPc;
nRow = p->nOp;
if( pSub!=0 ){
if( pSub->flags&MEM_Blob ){
nSub = pSub->n/sizeof(Vdbe*);
apSub = (SubProgram **)pSub->z;
}
for(i=0; i<nSub; i++){
nRow += apSub[i]->nOp;
}
}
iPc = *piPc;
while(1){
i = iPc++;
if( i>=nRow ){
p->rc = SQLITE_OK;
rc = SQLITE_DONE;
break;
}
if( i<p->nOp ){
aOp = p->aOp;
}else{
int j;
i -= p->nOp;
assert( apSub!=0 );
assert( nSub>0 );
for(j=0; i>=apSub[j]->nOp; j++){
i -= apSub[j]->nOp;
assert( i<apSub[j]->nOp || j+1<nSub );
}
aOp = apSub[j]->aOp;
}
if( pSub!=0 && aOp[i].p4type==P4_SUBPROGRAM ){
int nByte = (nSub+1)*sizeof(SubProgram*);
int j;
for(j=0; j<nSub; j++){
if( apSub[j]==aOp[i].p4.pProgram ) break;
}
if( j==nSub ){
p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
if( p->rc!=SQLITE_OK ){
rc = SQLITE_ERROR;
break;
}
apSub = (SubProgram **)pSub->z;
apSub[nSub++] = aOp[i].p4.pProgram;
MemSetTypeFlag(pSub, MEM_Blob);
pSub->n = nSub*sizeof(SubProgram*);
nRow += aOp[i].p4.pProgram->nOp;
}
}
if( eMode==0 ) break;
#ifdef SQLITE_ENABLE_BYTECODE_VTAB
if( eMode==2 ){
Op *pOp = aOp + i;
if( pOp->opcode==OP_OpenRead ) break;
if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
if( pOp->opcode==OP_ReopenIdx ) break;
}else
#endif
{
assert( eMode==1 );
if( aOp[i].opcode==OP_Explain ) break;
if( aOp[i].opcode==OP_Init && iPc>1 ) break;
}
}
*piPc = iPc;
*piAddr = i;
*paOp = aOp;
return rc;
}
#endif
void sqlite3VdbeFrameDelete(VdbeFrame *p){
int i;
Mem *aMem = VdbeFrameMem(p);
VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
assert( sqlite3VdbeFrameIsValid(p) );
for(i=0; i<p->nChildCsr; i++){
if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]);
}
releaseMemArray(aMem, p->nChildMem);
sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
sqlite3DbFree(p->v->db, p);
}
#ifndef SQLITE_OMIT_EXPLAIN
int sqlite3VdbeList(
Vdbe *p
){
Mem *pSub = 0;
sqlite3 *db = p->db;
int i;
int rc = SQLITE_OK;
Mem *pMem = &p->aMem[1];
int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
Op *aOp;
Op *pOp;
assert( p->explain );
assert( p->eVdbeState==VDBE_RUN_STATE );
assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
releaseMemArray(pMem, 8);
if( p->rc==SQLITE_NOMEM ){
sqlite3OomFault(db);
return SQLITE_ERROR;
}
if( bListSubprogs ){
assert( p->nMem>9 );
pSub = &p->aMem[9];
}else{
pSub = 0;
}
rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
if( rc==SQLITE_OK ){
pOp = aOp + i;
if( AtomicLoad(&db->u1.isInterrupted) ){
p->rc = SQLITE_INTERRUPT;
rc = SQLITE_ERROR;
sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
}else{
char *zP4 = sqlite3VdbeDisplayP4(db, pOp);
if( p->explain==2 ){
sqlite3VdbeMemSetInt64(pMem, pOp->p1);
sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free);
p->nResColumn = 4;
}else{
sqlite3VdbeMemSetInt64(pMem+0, i);
sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
-1, SQLITE_UTF8, SQLITE_STATIC);
sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
{
char *zCom = sqlite3VdbeDisplayComment(db, pOp, zP4);
sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
}
#else
sqlite3VdbeMemSetNull(pMem+7);
#endif
sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free);
p->nResColumn = 8;
}
p->pResultRow = pMem;
if( db->mallocFailed ){
p->rc = SQLITE_NOMEM;
rc = SQLITE_ERROR;
}else{
p->rc = SQLITE_OK;
rc = SQLITE_ROW;
}
}
}
return rc;
}
#endif
#ifdef SQLITE_DEBUG
void sqlite3VdbePrintSql(Vdbe *p){
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
#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
void sqlite3VdbeIOTraceSql(Vdbe *p){
int nOp = p->nOp;
VdbeOp *pOp;
if( sqlite3IoTrace==0 ) return;
if( nOp<1 ) return;
pOp = &p->aOp[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);
for(i=0; sqlite3Isspace(z[i]); i++){}
for(j=0; z[i]; i++){
if( sqlite3Isspace(z[i]) ){
if( z[i-1]!=' ' ){
z[j++] = ' ';
}
}else{
z[j++] = z[i];
}
}
z[j] = 0;
sqlite3IoTrace("SQL %s\n", z);
}
}
#endif
struct ReusableSpace {
u8 *pSpace;
sqlite3_int64 nFree;
sqlite3_int64 nNeeded;
};
static void *allocSpace(
struct ReusableSpace *p,
void *pBuf,
sqlite3_int64 nByte
){
assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
if( pBuf==0 ){
nByte = ROUND8P(nByte);
if( nByte <= p->nFree ){
p->nFree -= nByte;
pBuf = &p->pSpace[p->nFree];
}else{
p->nNeeded += nByte;
}
}
assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
return pBuf;
}
void sqlite3VdbeRewind(Vdbe *p){
#if defined(SQLITE_DEBUG)
int i;
#endif
assert( p!=0 );
assert( p->eVdbeState==VDBE_INIT_STATE
|| p->eVdbeState==VDBE_READY_STATE
|| p->eVdbeState==VDBE_HALT_STATE );
assert( p->nOp>0 );
p->eVdbeState = VDBE_READY_STATE;
#ifdef SQLITE_DEBUG
for(i=0; i<p->nMem; i++){
assert( p->aMem[i].db==p->db );
}
#endif
p->pc = -1;
p->rc = SQLITE_OK;
p->errorAction = OE_Abort;
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].nExec = 0;
p->aOp[i].nCycle = 0;
}
#endif
}
void sqlite3VdbeMakeReady(
Vdbe *p,
Parse *pParse
){
sqlite3 *db;
int nVar;
int nMem;
int nCursor;
int nArg;
int n;
struct ReusableSpace x;
assert( p!=0 );
assert( p->nOp>0 );
assert( pParse!=0 );
assert( p->eVdbeState==VDBE_INIT_STATE );
assert( pParse==p->pParse );
p->pVList = pParse->pVList;
pParse->pVList = 0;
db = p->db;
assert( db->mallocFailed==0 );
nVar = pParse->nVar;
nMem = pParse->nMem;
nCursor = pParse->nTab;
nArg = pParse->nMaxArg;
nMem += nCursor;
if( nCursor==0 && nMem>0 ) nMem++;
n = ROUND8P(sizeof(Op)*p->nOp);
x.pSpace = &((u8*)p->aOp)[n];
assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) );
x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n);
assert( x.nFree>=0 );
assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
resolveP2Values(p, &nArg);
p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
if( pParse->explain ){
static const char * const azColName[] = {
"addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
"id", "parent", "notused", "detail"
};
int iFirst, mx, i;
if( nMem<10 ) nMem = 10;
p->explain = pParse->explain;
if( pParse->explain==2 ){
sqlite3VdbeSetNumCols(p, 4);
iFirst = 8;
mx = 12;
}else{
sqlite3VdbeSetNumCols(p, 8);
iFirst = 0;
mx = 8;
}
for(i=iFirst; i<mx; i++){
sqlite3VdbeSetColName(p, i-iFirst, COLNAME_NAME,
azColName[i], SQLITE_STATIC);
}
}
p->expired = 0;
x.nNeeded = 0;
p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
if( x.nNeeded ){
x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
x.nFree = x.nNeeded;
if( !db->mallocFailed ){
p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
}
}
if( db->mallocFailed ){
p->nVar = 0;
p->nCursor = 0;
p->nMem = 0;
}else{
p->nCursor = nCursor;
p->nVar = (ynVar)nVar;
initMemArray(p->aVar, nVar, db, MEM_Null);
p->nMem = nMem;
initMemArray(p->aMem, nMem, db, MEM_Undefined);
memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
}
sqlite3VdbeRewind(p);
}
void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
if( pCx ) sqlite3VdbeFreeCursorNN(p,pCx);
}
void sqlite3VdbeFreeCursorNN(Vdbe *p, VdbeCursor *pCx){
switch( pCx->eCurType ){
case CURTYPE_SORTER: {
sqlite3VdbeSorterClose(p->db, pCx);
break;
}
case CURTYPE_BTREE: {
assert( pCx->uc.pCursor!=0 );
sqlite3BtreeCloseCursor(pCx->uc.pCursor);
break;
}
#ifndef SQLITE_OMIT_VIRTUALTABLE
case CURTYPE_VTAB: {
sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
const sqlite3_module *pModule = pVCur->pVtab->pModule;
assert( pVCur->pVtab->nRef>0 );
pVCur->pVtab->nRef--;
pModule->xClose(pVCur);
break;
}
#endif
}
}
static void closeCursorsInFrame(Vdbe *p){
int i;
for(i=0; i<p->nCursor; i++){
VdbeCursor *pC = p->apCsr[i];
if( pC ){
sqlite3VdbeFreeCursorNN(p, pC);
p->apCsr[i] = 0;
}
}
}
int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
Vdbe *v = pFrame->v;
closeCursorsInFrame(v);
v->aOp = pFrame->aOp;
v->nOp = pFrame->nOp;
v->aMem = pFrame->aMem;
v->nMem = pFrame->nMem;
v->apCsr = pFrame->apCsr;
v->nCursor = pFrame->nCursor;
v->db->lastRowid = pFrame->lastRowid;
v->nChange = pFrame->nChange;
v->db->nChange = pFrame->nDbChange;
sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
v->pAuxData = pFrame->pAuxData;
pFrame->pAuxData = 0;
return pFrame->pc;
}
static void closeAllCursors(Vdbe *p){
if( p->pFrame ){
VdbeFrame *pFrame;
for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
sqlite3VdbeFrameRestore(pFrame);
p->pFrame = 0;
p->nFrame = 0;
}
assert( p->nFrame==0 );
closeCursorsInFrame(p);
releaseMemArray(p->aMem, p->nMem);
while( p->pDelFrame ){
VdbeFrame *pDel = p->pDelFrame;
p->pDelFrame = pDel->pParent;
sqlite3VdbeFrameDelete(pDel);
}
if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
assert( p->pAuxData==0 );
}
void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
int n;
sqlite3 *db = p->db;
if( p->nResColumn ){
releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
sqlite3DbFree(db, p->aColName);
}
n = nResColumn*COLNAME_N;
p->nResColumn = (u16)nResColumn;
p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
if( p->aColName==0 ) return;
initMemArray(p->aColName, n, db, MEM_Null);
}
int sqlite3VdbeSetColName(
Vdbe *p,
int idx,
int var,
const char *zName,
void (*xDel)(void*)
){
int rc;
Mem *pColName;
assert( idx<p->nResColumn );
assert( var<COLNAME_N );
if( p->db->mallocFailed ){
assert( !zName || xDel!=SQLITE_DYNAMIC );
return SQLITE_NOMEM_BKPT;
}
assert( p->aColName!=0 );
pColName = &(p->aColName[idx+var*p->nResColumn]);
rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
return rc;
}
static int vdbeCommit(sqlite3 *db, Vdbe *p){
int i;
int nTrans = 0;
int rc = SQLITE_OK;
int needXcommit = 0;
#ifdef SQLITE_OMIT_VIRTUALTABLE
UNUSED_PARAMETER(p);
#endif
rc = sqlite3VtabSync(db, p);
for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
Btree *pBt = db->aDb[i].pBt;
if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE ){
static const u8 aMJNeeded[] = {
1,
1,
0,
1,
0,
0
};
Pager *pPager;
needXcommit = 1;
sqlite3BtreeEnter(pBt);
pPager = sqlite3BtreePager(pBt);
if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
&& aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
&& sqlite3PagerIsMemdb(pPager)==0
){
assert( i!=1 );
nTrans++;
}
rc = sqlite3PagerExclusiveLock(pPager);
sqlite3BtreeLeave(pBt);
}
}
if( rc!=SQLITE_OK ){
return rc;
}
if( needXcommit && db->xCommitCallback ){
rc = db->xCommitCallback(db->pCommitArg);
if( rc ){
return SQLITE_CONSTRAINT_COMMITHOOK;
}
}
if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
|| nTrans<=1
){
for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
Btree *pBt = db->aDb[i].pBt;
if( pBt ){
rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
}
}
for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
Btree *pBt = db->aDb[i].pBt;
if( pBt ){
rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
}
}
if( rc==SQLITE_OK ){
sqlite3VtabCommit(db);
}
}
#ifndef SQLITE_OMIT_DISKIO
else{
sqlite3_vfs *pVfs = db->pVfs;
char *zSuper = 0;
char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
sqlite3_file *pSuperJrnl = 0;
i64 offset = 0;
int res;
int retryCount = 0;
int nMainFile;
nMainFile = sqlite3Strlen30(zMainFile);
zSuper = sqlite3MPrintf(db, "%.4c%s%.16c", 0,zMainFile,0);
if( zSuper==0 ) return SQLITE_NOMEM_BKPT;
zSuper += 4;
do {
u32 iRandom;
if( retryCount ){
if( retryCount>100 ){
sqlite3_log(SQLITE_FULL, "MJ delete: %s", zSuper);
sqlite3OsDelete(pVfs, zSuper, 0);
break;
}else if( retryCount==1 ){
sqlite3_log(SQLITE_FULL, "MJ collide: %s", zSuper);
}
}
retryCount++;
sqlite3_randomness(sizeof(iRandom), &iRandom);
sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
(iRandom>>8)&0xffffff, iRandom&0xff);
assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
sqlite3FileSuffix3(zMainFile, zSuper);
rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS, &res);
}while( rc==SQLITE_OK && res );
if( rc==SQLITE_OK ){
rc = sqlite3OsOpenMalloc(pVfs, zSuper, &pSuperJrnl,
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_SUPER_JOURNAL, 0
);
}
if( rc!=SQLITE_OK ){
sqlite3DbFree(db, zSuper-4);
return rc;
}
for(i=0; i<db->nDb; i++){
Btree *pBt = db->aDb[i].pBt;
if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE ){
char const *zFile = sqlite3BtreeGetJournalname(pBt);
if( zFile==0 ){
continue;
}
assert( zFile[0]!=0 );
rc = sqlite3OsWrite(pSuperJrnl, zFile, sqlite3Strlen30(zFile)+1,offset);
offset += sqlite3Strlen30(zFile)+1;
if( rc!=SQLITE_OK ){
sqlite3OsCloseFree(pSuperJrnl);
sqlite3OsDelete(pVfs, zSuper, 0);
sqlite3DbFree(db, zSuper-4);
return rc;
}
}
}
if( 0==(sqlite3OsDeviceCharacteristics(pSuperJrnl)&SQLITE_IOCAP_SEQUENTIAL)
&& SQLITE_OK!=(rc = sqlite3OsSync(pSuperJrnl, SQLITE_SYNC_NORMAL))
){
sqlite3OsCloseFree(pSuperJrnl);
sqlite3OsDelete(pVfs, zSuper, 0);
sqlite3DbFree(db, zSuper-4);
return rc;
}
for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
Btree *pBt = db->aDb[i].pBt;
if( pBt ){
rc = sqlite3BtreeCommitPhaseOne(pBt, zSuper);
}
}
sqlite3OsCloseFree(pSuperJrnl);
assert( rc!=SQLITE_BUSY );
if( rc!=SQLITE_OK ){
sqlite3DbFree(db, zSuper-4);
return rc;
}
rc = sqlite3OsDelete(pVfs, zSuper, 1);
sqlite3DbFree(db, zSuper-4);
zSuper = 0;
if( rc ){
return rc;
}
disable_simulated_io_errors();
sqlite3BeginBenignMalloc();
for(i=0; i<db->nDb; i++){
Btree *pBt = db->aDb[i].pBt;
if( pBt ){
sqlite3BtreeCommitPhaseTwo(pBt, 1);
}
}
sqlite3EndBenignMalloc();
enable_simulated_io_errors();
sqlite3VtabCommit(db);
}
#endif
return rc;
}
#ifndef NDEBUG
static void checkActiveVdbeCnt(sqlite3 *db){
Vdbe *p;
int cnt = 0;
int nWrite = 0;
int nRead = 0;
p = db->pVdbe;
while( p ){
if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
cnt++;
if( p->readOnly==0 ) nWrite++;
if( p->bIsReader ) nRead++;
}
p = p->pVNext;
}
assert( cnt==db->nVdbeActive );
assert( nWrite==db->nVdbeWrite );
assert( nRead==db->nVdbeRead );
}
#else
#define checkActiveVdbeCnt(x)
#endif
static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
sqlite3 *const db = p->db;
int rc = SQLITE_OK;
int i;
const int iSavepoint = p->iStatement-1;
assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
assert( db->nStatement>0 );
assert( p->iStatement==(db->nStatement+db->nSavepoint) );
for(i=0; i<db->nDb; i++){
int rc2 = SQLITE_OK;
Btree *pBt = db->aDb[i].pBt;
if( pBt ){
if( eOp==SAVEPOINT_ROLLBACK ){
rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
}
if( rc2==SQLITE_OK ){
rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
}
if( rc==SQLITE_OK ){
rc = rc2;
}
}
}
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( eOp==SAVEPOINT_ROLLBACK ){
db->nDeferredCons = p->nStmtDefCons;
db->nDeferredImmCons = p->nStmtDefImmCons;
}
return rc;
}
int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
if( p->db->nStatement && p->iStatement ){
return vdbeCloseStatement(p, eOp);
}
return SQLITE_OK;
}
#ifndef SQLITE_OMIT_FOREIGN_KEY
int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
sqlite3 *db = p->db;
if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
|| (!deferred && p->nFkConstraint>0)
){
p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
p->errorAction = OE_Abort;
sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ) return SQLITE_ERROR;
return SQLITE_CONSTRAINT_FOREIGNKEY;
}
return SQLITE_OK;
}
#endif
int sqlite3VdbeHalt(Vdbe *p){
int rc;
sqlite3 *db = p->db;
assert( p->eVdbeState==VDBE_RUN_STATE );
if( db->mallocFailed ){
p->rc = SQLITE_NOMEM_BKPT;
}
closeAllCursors(p);
checkActiveVdbeCnt(db);
if( p->bIsReader ){
int mrc;
int eStatementOp = 0;
int isSpecialError;
sqlite3VdbeEnter(p);
if( p->rc ){
mrc = p->rc & 0xff;
isSpecialError = mrc==SQLITE_NOMEM
|| mrc==SQLITE_IOERR
|| mrc==SQLITE_INTERRUPT
|| mrc==SQLITE_FULL;
}else{
mrc = isSpecialError = 0;
}
if( isSpecialError ){
if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
eStatementOp = SAVEPOINT_ROLLBACK;
}else{
sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
sqlite3CloseSavepoints(db);
db->autoCommit = 1;
p->nChange = 0;
}
}
}
if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
sqlite3VdbeCheckFk(p, 0);
}
if( !sqlite3VtabInSync(db)
&& db->autoCommit
&& db->nVdbeWrite==(p->readOnly==0)
){
if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
rc = sqlite3VdbeCheckFk(p, 1);
if( rc!=SQLITE_OK ){
if( NEVER(p->readOnly) ){
sqlite3VdbeLeave(p);
return SQLITE_ERROR;
}
rc = SQLITE_CONSTRAINT_FOREIGNKEY;
}else if( db->flags & SQLITE_CorruptRdOnly ){
rc = SQLITE_CORRUPT;
db->flags &= ~SQLITE_CorruptRdOnly;
}else{
rc = vdbeCommit(db, p);
}
if( rc==SQLITE_BUSY && p->readOnly ){
sqlite3VdbeLeave(p);
return SQLITE_BUSY;
}else if( rc!=SQLITE_OK ){
p->rc = rc;
sqlite3RollbackAll(db, SQLITE_OK);
p->nChange = 0;
}else{
db->nDeferredCons = 0;
db->nDeferredImmCons = 0;
db->flags &= ~(u64)SQLITE_DeferFKs;
sqlite3CommitInternalChanges(db);
}
}else if( p->rc==SQLITE_SCHEMA && db->nVdbeActive>1 ){
p->nChange = 0;
}else{
sqlite3RollbackAll(db, SQLITE_OK);
p->nChange = 0;
}
db->nStatement = 0;
}else if( eStatementOp==0 ){
if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
eStatementOp = SAVEPOINT_RELEASE;
}else if( p->errorAction==OE_Abort ){
eStatementOp = SAVEPOINT_ROLLBACK;
}else{
sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
sqlite3CloseSavepoints(db);
db->autoCommit = 1;
p->nChange = 0;
}
}
if( eStatementOp ){
rc = sqlite3VdbeCloseStatement(p, eStatementOp);
if( rc ){
if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
p->rc = rc;
sqlite3DbFree(db, p->zErrMsg);
p->zErrMsg = 0;
}
sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
sqlite3CloseSavepoints(db);
db->autoCommit = 1;
p->nChange = 0;
}
}
if( p->changeCntOn ){
if( eStatementOp!=SAVEPOINT_ROLLBACK ){
sqlite3VdbeSetChanges(db, p->nChange);
}else{
sqlite3VdbeSetChanges(db, 0);
}
p->nChange = 0;
}
sqlite3VdbeLeave(p);
}
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->eVdbeState = VDBE_HALT_STATE;
checkActiveVdbeCnt(db);
if( db->mallocFailed ){
p->rc = SQLITE_NOMEM_BKPT;
}
if( db->autoCommit ){
sqlite3ConnectionUnlocked(db);
}
assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
}
void sqlite3VdbeResetStepResult(Vdbe *p){
p->rc = SQLITE_OK;
}
int sqlite3VdbeTransferError(Vdbe *p){
sqlite3 *db = p->db;
int rc = p->rc;
if( p->zErrMsg ){
db->bBenignMalloc++;
sqlite3BeginBenignMalloc();
if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
sqlite3EndBenignMalloc();
db->bBenignMalloc--;
}else if( db->pErr ){
sqlite3ValueSetNull(db->pErr);
}
db->errCode = rc;
db->errByteOffset = -1;
return rc;
}
#ifdef SQLITE_ENABLE_SQLLOG
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
int sqlite3VdbeReset(Vdbe *p){
#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
int i;
#endif
sqlite3 *db;
db = p->db;
if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
if( p->pc>=0 ){
vdbeInvokeSqllog(p);
if( db->pErr || p->zErrMsg ){
sqlite3VdbeTransferError(p);
}else{
db->errCode = p->rc;
}
}
#ifdef SQLITE_DEBUG
if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
if( p->aMem ){
for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
}
#endif
if( p->zErrMsg ){
sqlite3DbFree(db, p->zErrMsg);
p->zErrMsg = 0;
}
p->pResultRow = 0;
#ifdef SQLITE_DEBUG
p->nWrite = 0;
#endif
#ifdef VDBE_PROFILE
{
FILE *out = fopen("vdbe_profile.out", "a");
if( out ){
fprintf(out, "---- ");
for(i=0; i<p->nOp; i++){
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++){
char zHdr[100];
i64 cnt = p->aOp[i].nExec;
i64 cycles = p->aOp[i].nCycle;
sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
cnt,
cycles,
cnt>0 ? cycles/cnt : 0
);
fprintf(out, "%s", zHdr);
sqlite3VdbePrintOp(out, i, &p->aOp[i]);
}
fclose(out);
}
}
#endif
return p->rc & db->errMask;
}
int sqlite3VdbeFinalize(Vdbe *p){
int rc = SQLITE_OK;
assert( VDBE_RUN_STATE>VDBE_READY_STATE );
assert( VDBE_HALT_STATE>VDBE_READY_STATE );
assert( VDBE_INIT_STATE<VDBE_READY_STATE );
if( p->eVdbeState>=VDBE_READY_STATE ){
rc = sqlite3VdbeReset(p);
assert( (rc & p->db->errMask)==rc );
}
sqlite3VdbeDelete(p);
return rc;
}
void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
while( *pp ){
AuxData *pAux = *pp;
if( (iOp<0)
|| (pAux->iAuxOp==iOp
&& pAux->iAuxArg>=0
&& (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
){
testcase( pAux->iAuxArg==31 );
if( pAux->xDeleteAux ){
pAux->xDeleteAux(pAux->pAux);
}
*pp = pAux->pNextAux;
sqlite3DbFree(db, pAux);
}else{
pp= &pAux->pNextAux;
}
}
}
static void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
SubProgram *pSub, *pNext;
assert( db!=0 );
assert( p->db==0 || p->db==db );
if( p->aColName ){
releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
sqlite3DbNNFreeNN(db, p->aColName);
}
for(pSub=p->pProgram; pSub; pSub=pNext){
pNext = pSub->pNext;
vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
sqlite3DbFree(db, pSub);
}
if( p->eVdbeState!=VDBE_INIT_STATE ){
releaseMemArray(p->aVar, p->nVar);
if( p->pVList ) sqlite3DbNNFreeNN(db, p->pVList);
if( p->pFree ) sqlite3DbNNFreeNN(db, p->pFree);
}
vdbeFreeOpArray(db, p->aOp, p->nOp);
if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql);
#ifdef SQLITE_ENABLE_NORMALIZE
sqlite3DbFree(db, p->zNormSql);
{
DblquoteStr *pThis, *pNxt;
for(pThis=p->pDblStr; pThis; pThis=pNxt){
pNxt = pThis->pNextStr;
sqlite3DbFree(db, pThis);
}
}
#endif
#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
{
int i;
for(i=0; i<p->nScan; i++){
sqlite3DbFree(db, p->aScan[i].zName);
}
sqlite3DbFree(db, p->aScan);
}
#endif
}
void sqlite3VdbeDelete(Vdbe *p){
sqlite3 *db;
assert( p!=0 );
db = p->db;
assert( db!=0 );
assert( sqlite3_mutex_held(db->mutex) );
sqlite3VdbeClearObject(db, p);
if( db->pnBytesFreed==0 ){
assert( p->ppVPrev!=0 );
*p->ppVPrev = p->pVNext;
if( p->pVNext ){
p->pVNext->ppVPrev = p->ppVPrev;
}
}
sqlite3DbNNFreeNN(db, p);
}
int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor *p){
int res, rc;
#ifdef SQLITE_TEST
extern int sqlite3_search_count;
#endif
assert( p->deferredMoveto );
assert( p->isTable );
assert( p->eCurType==CURTYPE_BTREE );
rc = sqlite3BtreeTableMoveto(p->uc.pCursor, 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;
}
int SQLITE_NOINLINE sqlite3VdbeHandleMovedCursor(VdbeCursor *p){
int isDifferentRow, rc;
assert( p->eCurType==CURTYPE_BTREE );
assert( p->uc.pCursor!=0 );
assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
p->cacheStatus = CACHE_STALE;
if( isDifferentRow ) p->nullRow = 1;
return rc;
}
int sqlite3VdbeCursorRestore(VdbeCursor *p){
assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) );
if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
return sqlite3VdbeHandleMovedCursor(p);
}
return SQLITE_OK;
}
#if 0#endif
const u8 sqlite3SmallTypeSizes[128] = {
0, 1, 2, 3, 4, 6, 8, 8, 0, 0,
0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
54, 54, 55, 55, 56, 56, 57, 57
};
u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
if( serial_type>=128 ){
return (serial_type-12)/2;
}else{
assert( serial_type<12
|| sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
return sqlite3SmallTypeSizes[serial_type];
}
}
u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
assert( serial_type<128 );
return sqlite3SmallTypeSizes[serial_type];
}
#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
u64 sqlite3FloatSwap(u64 in){
union {
u64 r;
u32 i[2];
} u;
u32 t;
u.r = in;
t = u.i[0];
u.i[0] = u.i[1];
u.i[1] = t;
return u.r;
}
#endif
#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])
static void serialGet(
const unsigned char *buf,
u32 serial_type,
Mem *pMem
){
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)
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 = IsNaN(x) ? MEM_Null : MEM_Real;
}
}
void sqlite3VdbeSerialGet(
const unsigned char *buf,
u32 serial_type,
Mem *pMem
){
switch( serial_type ){
case 10: {
pMem->flags = MEM_Null|MEM_Zero;
pMem->n = 0;
pMem->u.nZero = 0;
return;
}
case 11:
case 0: {
pMem->flags = MEM_Null;
return;
}
case 1: {
pMem->u.i = ONE_BYTE_INT(buf);
pMem->flags = MEM_Int;
testcase( pMem->u.i<0 );
return;
}
case 2: {
pMem->u.i = TWO_BYTE_INT(buf);
pMem->flags = MEM_Int;
testcase( pMem->u.i<0 );
return;
}
case 3: {
pMem->u.i = THREE_BYTE_INT(buf);
pMem->flags = MEM_Int;
testcase( pMem->u.i<0 );
return;
}
case 4: {
pMem->u.i = FOUR_BYTE_INT(buf);
#ifdef __HP_cc
if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
#endif
pMem->flags = MEM_Int;
testcase( pMem->u.i<0 );
return;
}
case 5: {
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;
}
case 6:
case 7: {
serialGet(buf,serial_type,pMem);
return;
}
case 8:
case 9: {
pMem->u.i = serial_type-8;
pMem->flags = MEM_Int;
return;
}
default: {
static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
pMem->z = (char *)buf;
pMem->n = (serial_type-12)/2;
pMem->flags = aFlag[serial_type&1];
return;
}
}
return;
}
UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
KeyInfo *pKeyInfo
){
UnpackedRecord *p;
int nByte;
nByte = ROUND8P(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
if( !p ) return 0;
p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))];
assert( pKeyInfo->aSortFlags!=0 );
p->pKeyInfo = pKeyInfo;
p->nField = pKeyInfo->nKeyField + 1;
return p;
}
void sqlite3VdbeRecordUnpack(
KeyInfo *pKeyInfo,
int nKey,
const void *pKey,
UnpackedRecord *p
){
const unsigned char *aKey = (const unsigned char *)pKey;
u32 d;
u32 idx;
u16 u;
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 && d<=(u32)nKey ){
u32 serial_type;
idx += getVarint32(&aKey[idx], serial_type);
pMem->enc = pKeyInfo->enc;
pMem->db = pKeyInfo->db;
pMem->szMalloc = 0;
pMem->z = 0;
sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
d += sqlite3VdbeSerialTypeLen(serial_type);
pMem++;
if( (++u)>=p->nField ) break;
}
if( d>(u32)nKey && u ){
assert( CORRUPT_DB );
sqlite3VdbeMemSetNull(pMem-1);
}
assert( u<=pKeyInfo->nKeyField + 1 );
p->nField = u;
}
#ifdef SQLITE_DEBUG
static int vdbeRecordCompareDebug(
int nKey1, const void *pKey1,
const UnpackedRecord *pPKey2,
int desiredResult
){
u32 d1;
u32 idx1;
u32 szHdr1;
int i = 0;
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;
VVA_ONLY( mem1.szMalloc = 0; )
idx1 = getVarint32(aKey1, szHdr1);
if( szHdr1>98307 ) return SQLITE_CORRUPT;
d1 = szHdr1;
assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
assert( pKeyInfo->aSortFlags!=0 );
assert( pKeyInfo->nKeyField>0 );
assert( idx1<=szHdr1 || CORRUPT_DB );
do{
u32 serial_type1;
idx1 += getVarint32( aKey1+idx1, serial_type1 );
if( d1+(u64)serial_type1+2>(u64)nKey1
&& d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)>(u64)nKey1
){
if( serial_type1>=1
&& serial_type1<=7
&& d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)<=(u64)nKey1+8
&& CORRUPT_DB
){
return 1;
}
break;
}
sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
d1 += sqlite3VdbeSerialTypeLen(serial_type1);
rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
if( rc!=0 ){
assert( mem1.szMalloc==0 );
if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
&& ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
){
rc = -rc;
}
if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
rc = -rc;
}
goto debugCompareEnd;
}
i++;
}while( idx1<szHdr1 && i<pPKey2->nField );
assert( mem1.szMalloc==0 );
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
#ifdef SQLITE_DEBUG
static void vdbeAssertFieldCountWithinLimits(
int nKey, const void *pKey,
const KeyInfo *pKeyInfo
){
int nField = 0;
u32 szHdr;
u32 idx;
u32 notUsed;
const unsigned char *aKey = (const unsigned char*)pKey;
if( CORRUPT_DB ) return;
idx = getVarint32(aKey, szHdr);
assert( nKey>=0 );
assert( szHdr<=(u32)nKey );
while( idx<szHdr ){
idx += getVarint32(aKey+idx, notUsed);
nField++;
}
assert( nField <= pKeyInfo->nAllField );
}
#else
# define vdbeAssertFieldCountWithinLimits(A,B,C)
#endif
static int vdbeCompareMemString(
const Mem *pMem1,
const Mem *pMem2,
const CollSeq *pColl,
u8 *prcErr
){
if( pMem1->enc==pColl->enc ){
return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
}else{
int rc;
const void *v1, *v2;
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);
v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
if( (v1==0 || v2==0) ){
if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT;
rc = 0;
}else{
rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
}
sqlite3VdbeMemReleaseMalloc(&c1);
sqlite3VdbeMemReleaseMalloc(&c2);
return rc;
}
}
static int isAllZero(const char *z, int n){
int i;
for(i=0; i<n; i++){
if( z[i] ) return 0;
}
return 1;
}
SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
int c;
int n1 = pB1->n;
int n2 = pB2->n;
assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
if( (pB1->flags|pB2->flags) & MEM_Zero ){
if( pB1->flags & pB2->flags & MEM_Zero ){
return pB1->u.nZero - pB2->u.nZero;
}else if( pB1->flags & MEM_Zero ){
if( !isAllZero(pB2->z, pB2->n) ) return -1;
return pB1->u.nZero - n2;
}else{
if( !isAllZero(pB1->z, pB1->n) ) return +1;
return n1 - pB2->u.nZero;
}
}
c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
if( c ) return c;
return n1 - n2;
}
int sqlite3IntFloatCompare(i64 i, double r){
if( sizeof(LONGDOUBLE_TYPE)>8 ){
LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i;
testcase( x<r );
testcase( x>r );
testcase( x==r );
if( x<r ) return -1;
if( x>r ) return +1;
return 0;
}else{
i64 y;
double s;
if( r<-9223372036854775808.0 ) return +1;
if( r>=9223372036854775808.0 ) return -1;
y = (i64)r;
if( i<y ) return -1;
if( i>y ) return +1;
s = (double)i;
if( s<r ) return -1;
if( s>r ) return +1;
return 0;
}
}
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( !sqlite3VdbeMemIsRowSet(pMem1) && !sqlite3VdbeMemIsRowSet(pMem2) );
if( combined_flags&MEM_Null ){
return (f2&MEM_Null) - (f1&MEM_Null);
}
if( combined_flags&(MEM_Int|MEM_Real|MEM_IntReal) ){
testcase( combined_flags & MEM_Int );
testcase( combined_flags & MEM_Real );
testcase( combined_flags & MEM_IntReal );
if( (f1 & f2 & (MEM_Int|MEM_IntReal))!=0 ){
testcase( f1 & f2 & MEM_Int );
testcase( f1 & f2 & MEM_IntReal );
if( pMem1->u.i < pMem2->u.i ) return -1;
if( pMem1->u.i > pMem2->u.i ) return +1;
return 0;
}
if( (f1 & f2 & MEM_Real)!=0 ){
if( pMem1->u.r < pMem2->u.r ) return -1;
if( pMem1->u.r > pMem2->u.r ) return +1;
return 0;
}
if( (f1&(MEM_Int|MEM_IntReal))!=0 ){
testcase( f1 & MEM_Int );
testcase( f1 & MEM_IntReal );
if( (f2&MEM_Real)!=0 ){
return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
}else if( (f2&(MEM_Int|MEM_IntReal))!=0 ){
if( pMem1->u.i < pMem2->u.i ) return -1;
if( pMem1->u.i > pMem2->u.i ) return +1;
return 0;
}else{
return -1;
}
}
if( (f1&MEM_Real)!=0 ){
if( (f2&(MEM_Int|MEM_IntReal))!=0 ){
testcase( f2 & MEM_Int );
testcase( f2 & MEM_IntReal );
return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
}else{
return -1;
}
}
return +1;
}
if( combined_flags&MEM_Str ){
if( (f1 & MEM_Str)==0 ){
return 1;
}
if( (f2 & MEM_Str)==0 ){
return -1;
}
assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
assert( pMem1->enc==SQLITE_UTF8 ||
pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
assert( !pColl || pColl->xCmp );
if( pColl ){
return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
}
}
return sqlite3BlobCompare(pMem1, pMem2);
}
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);
}
int sqlite3VdbeRecordCompareWithSkip(
int nKey1, const void *pKey1,
UnpackedRecord *pPKey2,
int bSkip
){
u32 d1;
int i;
u32 szHdr1;
u32 idx1;
int rc = 0;
Mem *pRhs = pPKey2->aMem;
KeyInfo *pKeyInfo;
const unsigned char *aKey1 = (const unsigned char *)pKey1;
Mem mem1;
if( bSkip ){
u32 s1 = aKey1[1];
if( s1<0x80 ){
idx1 = 2;
}else{
idx1 = 1 + sqlite3GetVarint32(&aKey1[1], &s1);
}
szHdr1 = aKey1[0];
d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
i = 1;
pRhs++;
}else{
if( (szHdr1 = aKey1[0])<0x80 ){
idx1 = 1;
}else{
idx1 = sqlite3GetVarint32(aKey1, &szHdr1);
}
d1 = szHdr1;
i = 0;
}
if( d1>(unsigned)nKey1 ){
pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
return 0;
}
VVA_ONLY( mem1.szMalloc = 0; )
assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
|| CORRUPT_DB );
assert( pPKey2->pKeyInfo->aSortFlags!=0 );
assert( pPKey2->pKeyInfo->nKeyField>0 );
assert( idx1<=szHdr1 || CORRUPT_DB );
while( 1 ){
u32 serial_type;
if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
testcase( pRhs->flags & MEM_Int );
testcase( pRhs->flags & MEM_IntReal );
serial_type = aKey1[idx1];
testcase( serial_type==12 );
if( serial_type>=10 ){
rc = serial_type==10 ? -1 : +1;
}else if( serial_type==0 ){
rc = -1;
}else if( serial_type==7 ){
sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
}else{
i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
i64 rhs = pRhs->u.i;
if( lhs<rhs ){
rc = -1;
}else if( lhs>rhs ){
rc = +1;
}
}
}
else if( pRhs->flags & MEM_Real ){
serial_type = aKey1[idx1];
if( serial_type>=10 ){
rc = serial_type==10 ? -1 : +1;
}else if( serial_type==0 ){
rc = -1;
}else{
sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
if( serial_type==7 ){
if( mem1.u.r<pRhs->u.r ){
rc = -1;
}else if( mem1.u.r>pRhs->u.r ){
rc = +1;
}
}else{
rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
}
}
}
else if( pRhs->flags & MEM_Str ){
getVarint32NR(&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
|| (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
){
pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
return 0;
}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;
}
}
}
else if( pRhs->flags & MEM_Blob ){
assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
getVarint32NR(&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;
}else if( pRhs->flags & MEM_Zero ){
if( !isAllZero((const char*)&aKey1[d1],nStr) ){
rc = 1;
}else{
rc = nStr - pRhs->u.nZero;
}
}else{
int nCmp = MIN(nStr, pRhs->n);
rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
if( rc==0 ) rc = nStr - pRhs->n;
}
}
}
else{
serial_type = aKey1[idx1];
rc = (serial_type!=0 && serial_type!=10);
}
if( rc!=0 ){
int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
if( sortFlags ){
if( (sortFlags & KEYINFO_ORDER_BIGNULL)==0
|| ((sortFlags & KEYINFO_ORDER_DESC)
!=(serial_type==0 || (pRhs->flags&MEM_Null)))
){
rc = -rc;
}
}
assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
assert( mem1.szMalloc==0 );
return rc;
}
i++;
if( i==pPKey2->nField ) break;
pRhs++;
d1 += sqlite3VdbeSerialTypeLen(serial_type);
if( d1>(unsigned)nKey1 ) break;
idx1 += sqlite3VarintLen(serial_type);
if( idx1>=(unsigned)szHdr1 ){
pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
return 0;
}
}
assert( mem1.szMalloc==0 );
assert( CORRUPT_DB
|| vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
|| pPKey2->pKeyInfo->db->mallocFailed
);
pPKey2->eqSeen = 1;
return pPKey2->default_rc;
}
int sqlite3VdbeRecordCompare(
int nKey1, const void *pKey1,
UnpackedRecord *pPKey2
){
return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
}
static int vdbeRecordCompareInt(
int nKey1, const void *pKey1,
UnpackedRecord *pPKey2
){
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;
i64 lhs;
vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
switch( serial_type ){
case 1: {
lhs = ONE_BYTE_INT(aKey);
testcase( lhs<0 );
break;
}
case 2: {
lhs = TWO_BYTE_INT(aKey);
testcase( lhs<0 );
break;
}
case 3: {
lhs = THREE_BYTE_INT(aKey);
testcase( lhs<0 );
break;
}
case 4: {
y = FOUR_BYTE_UINT(aKey);
lhs = (i64)*(int*)&y;
testcase( lhs<0 );
break;
}
case 5: {
lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
testcase( lhs<0 );
break;
}
case 6: {
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;
case 0: case 7:
return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
default:
return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
}
assert( pPKey2->u.i == pPKey2->aMem[0].u.i );
v = pPKey2->u.i;
if( v>lhs ){
res = pPKey2->r1;
}else if( v<lhs ){
res = pPKey2->r2;
}else if( pPKey2->nField>1 ){
res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
}else{
res = pPKey2->default_rc;
pPKey2->eqSeen = 1;
}
assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
return res;
}
static int vdbeRecordCompareString(
int nKey1, const void *pKey1,
UnpackedRecord *pPKey2
){
const u8 *aKey1 = (const u8*)pKey1;
int serial_type;
int res;
assert( pPKey2->aMem[0].flags & MEM_Str );
assert( pPKey2->aMem[0].n == pPKey2->n );
assert( pPKey2->aMem[0].z == pPKey2->u.z );
vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
serial_type = (signed char)(aKey1[1]);
vrcs_restart:
if( serial_type<12 ){
if( serial_type<0 ){
sqlite3GetVarint32(&aKey1[1], (u32*)&serial_type);
if( serial_type>=12 ) goto vrcs_restart;
assert( CORRUPT_DB );
}
res = pPKey2->r1;
}else if( !(serial_type & 0x01) ){
res = pPKey2->r2;
}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;
}
nCmp = MIN( pPKey2->n, nStr );
res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp);
if( res>0 ){
res = pPKey2->r2;
}else if( res<0 ){
res = pPKey2->r1;
}else{
res = nStr - pPKey2->n;
if( res==0 ){
if( pPKey2->nField>1 ){
res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
}else{
res = pPKey2->default_rc;
pPKey2->eqSeen = 1;
}
}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;
}
RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
if( p->pKeyInfo->nAllField<=13 ){
int flags = p->aMem[0].flags;
if( p->pKeyInfo->aSortFlags[0] ){
if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
return sqlite3VdbeRecordCompare;
}
p->r1 = 1;
p->r2 = -1;
}else{
p->r1 = -1;
p->r2 = 1;
}
if( (flags & MEM_Int) ){
p->u.i = p->aMem[0].u.i;
return vdbeRecordCompareInt;
}
testcase( flags & MEM_Real );
testcase( flags & MEM_Null );
testcase( flags & MEM_Blob );
if( (flags & (MEM_Real|MEM_IntReal|MEM_Null|MEM_Blob))==0
&& p->pKeyInfo->aColl[0]==0
){
assert( flags & MEM_Str );
p->u.z = p->aMem[0].z;
p->n = p->aMem[0].n;
return vdbeRecordCompareString;
}
}
return sqlite3VdbeRecordCompare;
}
int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
i64 nCellKey = 0;
int rc;
u32 szHdr;
u32 typeRowid;
u32 lenRowid;
Mem m, v;
assert( sqlite3BtreeCursorIsValid(pCur) );
nCellKey = sqlite3BtreePayloadSize(pCur);
assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
sqlite3VdbeMemInit(&m, db, 0);
rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
if( rc ){
return rc;
}
getVarint32NR((u8*)m.z, szHdr);
testcase( szHdr==3 );
testcase( szHdr==(u32)m.n );
testcase( szHdr>0x7fffffff );
assert( m.n>=0 );
if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){
goto idx_rowid_corruption;
}
getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
testcase( typeRowid==1 );
testcase( typeRowid==2 );
testcase( typeRowid==3 );
testcase( typeRowid==4 );
testcase( typeRowid==5 );
testcase( typeRowid==6 );
testcase( typeRowid==8 );
testcase( typeRowid==9 );
if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
goto idx_rowid_corruption;
}
lenRowid = sqlite3SmallTypeSizes[typeRowid];
testcase( (u32)m.n==szHdr+lenRowid );
if( unlikely((u32)m.n<szHdr+lenRowid) ){
goto idx_rowid_corruption;
}
sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
*rowid = v.u.i;
sqlite3VdbeMemReleaseMalloc(&m);
return SQLITE_OK;
idx_rowid_corruption:
testcase( m.szMalloc!=0 );
sqlite3VdbeMemReleaseMalloc(&m);
return SQLITE_CORRUPT_BKPT;
}
int sqlite3VdbeIdxKeyCompare(
sqlite3 *db,
VdbeCursor *pC,
UnpackedRecord *pUnpacked,
int *res
){
i64 nCellKey = 0;
int rc;
BtCursor *pCur;
Mem m;
assert( pC->eCurType==CURTYPE_BTREE );
pCur = pC->uc.pCursor;
assert( sqlite3BtreeCursorIsValid(pCur) );
nCellKey = sqlite3BtreePayloadSize(pCur);
if( nCellKey<=0 || nCellKey>0x7fffffff ){
*res = 0;
return SQLITE_CORRUPT_BKPT;
}
sqlite3VdbeMemInit(&m, db, 0);
rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
if( rc ){
return rc;
}
*res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, pUnpacked, 0);
sqlite3VdbeMemReleaseMalloc(&m);
return SQLITE_OK;
}
void sqlite3VdbeSetChanges(sqlite3 *db, i64 nChange){
assert( sqlite3_mutex_held(db->mutex) );
db->nChange = nChange;
db->nTotalChange += nChange;
}
void sqlite3VdbeCountChanges(Vdbe *v){
v->changeCntOn = 1;
}
void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
Vdbe *p;
for(p = db->pVdbe; p; p=p->pVNext){
p->expired = iCode+1;
}
}
sqlite3 *sqlite3VdbeDb(Vdbe *v){
return v->db;
}
u8 sqlite3VdbePrepareFlags(Vdbe *v){
return v->prepFlags;
}
sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
assert( iVar>0 );
if( v ){
Mem *pMem = &v->aVar[iVar-1];
assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
if( 0==(pMem->flags & MEM_Null) ){
sqlite3_value *pRet = sqlite3ValueNew(v->db);
if( pRet ){
sqlite3VdbeMemCopy((Mem *)pRet, pMem);
sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
}
return pRet;
}
}
return 0;
}
void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
assert( iVar>0 );
assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
if( iVar>=32 ){
v->expmask |= 0x80000000;
}else{
v->expmask |= ((u32)1 << (iVar-1));
}
}
int sqlite3NotPureFunc(sqlite3_context *pCtx){
const VdbeOp *pOp;
#ifdef SQLITE_ENABLE_STAT4
if( pCtx->pVdbe==0 ) return 1;
#endif
pOp = pCtx->pVdbe->aOp + pCtx->iOp;
if( pOp->opcode==OP_PureFunc ){
const char *zContext;
char *zMsg;
if( pOp->p5 & NC_IsCheck ){
zContext = "a CHECK constraint";
}else if( pOp->p5 & NC_GenCol ){
zContext = "a generated column";
}else{
zContext = "an index";
}
zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
pCtx->pFunc->zName, zContext);
sqlite3_result_error(pCtx, zMsg, -1);
sqlite3_free(zMsg);
return 0;
}
return 1;
}
#if defined(SQLITE_ENABLE_CURSOR_HINTS) && defined(SQLITE_DEBUG)
int sqlite3CursorRangeHintExprCheck(Walker *pWalker, Expr *pExpr){
if( pExpr->op==TK_REGISTER ){
assert( (pWalker->u.aMem[pExpr->iTable].flags & MEM_Undefined)==0 );
}
return WRC_Continue;
}
#endif
#ifndef SQLITE_OMIT_VIRTUALTABLE
void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
if( pVtab->zErrMsg ){
sqlite3 *db = p->db;
sqlite3DbFree(db, p->zErrMsg);
p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
sqlite3_free(pVtab->zErrMsg);
pVtab->zErrMsg = 0;
}
}
#endif
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
assert( db!=0 );
if( p ){
int i;
for(i=0; i<nField; i++){
Mem *pMem = &p->aMem[i];
if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
}
sqlite3DbNNFreeNN(db, p);
}
}
#endif
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
void sqlite3VdbePreUpdateHook(
Vdbe *v,
VdbeCursor *pCsr,
int op,
const char *zDb,
Table *pTab,
i64 iKey1,
int iReg,
int iBlobWrite
){
sqlite3 *db = v->db;
i64 iKey2;
PreUpdate preupdate;
const char *zTbl = pTab->zName;
static const u8 fakeSortOrder = 0;
#ifdef SQLITE_DEBUG
int nRealCol;
if( pTab->tabFlags & TF_WithoutRowid ){
nRealCol = sqlite3PrimaryKeyIndex(pTab)->nColumn;
}else if( pTab->tabFlags & TF_HasVirtual ){
nRealCol = pTab->nNVCol;
}else{
nRealCol = pTab->nCol;
}
#endif
assert( db->pPreUpdate==0 );
memset(&preupdate, 0, sizeof(PreUpdate));
if( HasRowid(pTab)==0 ){
iKey1 = iKey2 = 0;
preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
}else{
if( op==SQLITE_UPDATE ){
iKey2 = v->aMem[iReg].u.i;
}else{
iKey2 = iKey1;
}
}
assert( pCsr!=0 );
assert( pCsr->eCurType==CURTYPE_BTREE );
assert( pCsr->nField==nRealCol
|| (pCsr->nField==nRealCol+1 && op==SQLITE_DELETE && iReg==-1)
);
preupdate.v = v;
preupdate.pCsr = pCsr;
preupdate.op = op;
preupdate.iNewReg = iReg;
preupdate.keyinfo.db = db;
preupdate.keyinfo.enc = ENC(db);
preupdate.keyinfo.nKeyField = pTab->nCol;
preupdate.keyinfo.aSortFlags = (u8*)&fakeSortOrder;
preupdate.iKey1 = iKey1;
preupdate.iKey2 = iKey2;
preupdate.pTab = pTab;
preupdate.iBlobWrite = iBlobWrite;
db->pPreUpdate = &preupdate;
db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
db->pPreUpdate = 0;
sqlite3DbFree(db, preupdate.aRecord);
vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
if( preupdate.aNew ){
int i;
for(i=0; i<pCsr->nField; i++){
sqlite3VdbeMemRelease(&preupdate.aNew[i]);
}
sqlite3DbNNFreeNN(db, preupdate.aNew);
}
}
#endif