Permalink
Cannot retrieve contributors at this time
/* | |
** 2001 September 15 | |
** | |
** The author disclaims copyright to this source code. In place of | |
** a legal notice, here is a blessing: | |
** | |
** May you do good and not evil. | |
** May you find forgiveness for yourself and forgive others. | |
** May you share freely, never taking more than you give. | |
** | |
************************************************************************* | |
** The code in this file implements the function that runs the | |
** bytecode of a prepared statement. | |
** | |
** Various scripts scan this source file in order to generate HTML | |
** documentation, headers files, or other derived files. The formatting | |
** of the code in this file is, therefore, important. See other comments | |
** in this file for details. If in doubt, do not deviate from existing | |
** commenting and indentation practices when changing or adding code. | |
*/ | |
#include "sqliteInt.h" | |
#include "vdbeInt.h" | |
/* | |
** Invoke this macro on memory cells just prior to changing the | |
** value of the cell. This macro verifies that shallow copies are | |
** not misused. A shallow copy of a string or blob just copies a | |
** pointer to the string or blob, not the content. If the original | |
** is changed while the copy is still in use, the string or blob might | |
** be changed out from under the copy. This macro verifies that nothing | |
** like that ever happens. | |
*/ | |
#ifdef SQLITE_DEBUG | |
# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M) | |
#else | |
# define memAboutToChange(P,M) | |
#endif | |
/* | |
** The following global variable is incremented every time a cursor | |
** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test | |
** procedures use this information to make sure that indices are | |
** working correctly. This variable has no function other than to | |
** help verify the correct operation of the library. | |
*/ | |
#ifdef SQLITE_TEST | |
int sqlite3_search_count = 0; | |
#endif | |
/* | |
** When this global variable is positive, it gets decremented once before | |
** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted | |
** field of the sqlite3 structure is set in order to simulate an interrupt. | |
** | |
** This facility is used for testing purposes only. It does not function | |
** in an ordinary build. | |
*/ | |
#ifdef SQLITE_TEST | |
int sqlite3_interrupt_count = 0; | |
#endif | |
/* | |
** The next global variable is incremented each type the OP_Sort opcode | |
** is executed. The test procedures use this information to make sure that | |
** sorting is occurring or not occurring at appropriate times. This variable | |
** has no function other than to help verify the correct operation of the | |
** library. | |
*/ | |
#ifdef SQLITE_TEST | |
int sqlite3_sort_count = 0; | |
#endif | |
/* | |
** The next global variable records the size of the largest MEM_Blob | |
** or MEM_Str that has been used by a VDBE opcode. The test procedures | |
** use this information to make sure that the zero-blob functionality | |
** is working correctly. This variable has no function other than to | |
** help verify the correct operation of the library. | |
*/ | |
#ifdef SQLITE_TEST | |
int sqlite3_max_blobsize = 0; | |
static void updateMaxBlobsize(Mem *p){ | |
if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ | |
sqlite3_max_blobsize = p->n; | |
} | |
} | |
#endif | |
/* | |
** This macro evaluates to true if either the update hook or the preupdate | |
** hook are enabled for database connect DB. | |
*/ | |
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK | |
# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) | |
#else | |
# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback) | |
#endif | |
/* | |
** The next global variable is incremented each time the OP_Found opcode | |
** is executed. This is used to test whether or not the foreign key | |
** operation implemented using OP_FkIsZero is working. This variable | |
** has no function other than to help verify the correct operation of the | |
** library. | |
*/ | |
#ifdef SQLITE_TEST | |
int sqlite3_found_count = 0; | |
#endif | |
/* | |
** Test a register to see if it exceeds the current maximum blob size. | |
** If it does, record the new maximum blob size. | |
*/ | |
#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE) | |
# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) | |
#else | |
# define UPDATE_MAX_BLOBSIZE(P) | |
#endif | |
#ifdef SQLITE_DEBUG | |
/* This routine provides a convenient place to set a breakpoint during | |
** tracing with PRAGMA vdbe_trace=on. The breakpoint fires right after | |
** each opcode is printed. Variables "pc" (program counter) and pOp are | |
** available to add conditionals to the breakpoint. GDB example: | |
** | |
** break test_trace_breakpoint if pc=22 | |
** | |
** Other useful labels for breakpoints include: | |
** test_addop_breakpoint(pc,pOp) | |
** sqlite3CorruptError(lineno) | |
** sqlite3MisuseError(lineno) | |
** sqlite3CantopenError(lineno) | |
*/ | |
static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){ | |
static int n = 0; | |
n++; | |
} | |
#endif | |
/* | |
** Invoke the VDBE coverage callback, if that callback is defined. This | |
** feature is used for test suite validation only and does not appear an | |
** production builds. | |
** | |
** M is the type of branch. I is the direction taken for this instance of | |
** the branch. | |
** | |
** M: 2 - two-way branch (I=0: fall-thru 1: jump ) | |
** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL ) | |
** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3) | |
** | |
** In other words, if M is 2, then I is either 0 (for fall-through) or | |
** 1 (for when the branch is taken). If M is 3, the I is 0 for an | |
** ordinary fall-through, I is 1 if the branch was taken, and I is 2 | |
** if the result of comparison is NULL. For M=3, I=2 the jump may or | |
** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5. | |
** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2 | |
** depending on if the operands are less than, equal, or greater than. | |
** | |
** iSrcLine is the source code line (from the __LINE__ macro) that | |
** generated the VDBE instruction combined with flag bits. The source | |
** code line number is in the lower 24 bits of iSrcLine and the upper | |
** 8 bytes are flags. The lower three bits of the flags indicate | |
** values for I that should never occur. For example, if the branch is | |
** always taken, the flags should be 0x05 since the fall-through and | |
** alternate branch are never taken. If a branch is never taken then | |
** flags should be 0x06 since only the fall-through approach is allowed. | |
** | |
** Bit 0x08 of the flags indicates an OP_Jump opcode that is only | |
** interested in equal or not-equal. In other words, I==0 and I==2 | |
** should be treated as equivalent | |
** | |
** Since only a line number is retained, not the filename, this macro | |
** only works for amalgamation builds. But that is ok, since these macros | |
** should be no-ops except for special builds used to measure test coverage. | |
*/ | |
#if !defined(SQLITE_VDBE_COVERAGE) | |
# define VdbeBranchTaken(I,M) | |
#else | |
# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M) | |
static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){ | |
u8 mNever; | |
assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */ | |
assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */ | |
assert( I<M ); /* I can only be 2 if M is 3 or 4 */ | |
/* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */ | |
I = 1<<I; | |
/* The upper 8 bits of iSrcLine are flags. The lower three bits of | |
** the flags indicate directions that the branch can never go. If | |
** a branch really does go in one of those directions, assert right | |
** away. */ | |
mNever = iSrcLine >> 24; | |
assert( (I & mNever)==0 ); | |
if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/ | |
/* Invoke the branch coverage callback with three arguments: | |
** iSrcLine - the line number of the VdbeCoverage() macro, with | |
** flags removed. | |
** I - Mask of bits 0x07 indicating which cases are are | |
** fulfilled by this instance of the jump. 0x01 means | |
** fall-thru, 0x02 means taken, 0x04 means NULL. Any | |
** impossible cases (ex: if the comparison is never NULL) | |
** are filled in automatically so that the coverage | |
** measurement logic does not flag those impossible cases | |
** as missed coverage. | |
** M - Type of jump. Same as M argument above | |
*/ | |
I |= mNever; | |
if( M==2 ) I |= 0x04; | |
if( M==4 ){ | |
I |= 0x08; | |
if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/ | |
} | |
sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, | |
iSrcLine&0xffffff, I, M); | |
} | |
#endif | |
/* | |
** An ephemeral string value (signified by the MEM_Ephem flag) contains | |
** a pointer to a dynamically allocated string where some other entity | |
** is responsible for deallocating that string. Because the register | |
** does not control the string, it might be deleted without the register | |
** knowing it. | |
** | |
** This routine converts an ephemeral string into a dynamically allocated | |
** string that the register itself controls. In other words, it | |
** converts an MEM_Ephem string into a string with P.z==P.zMalloc. | |
*/ | |
#define Deephemeralize(P) \ | |
if( ((P)->flags&MEM_Ephem)!=0 \ | |
&& sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} | |
/* Return true if the cursor was opened using the OP_OpenSorter opcode. */ | |
#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER) | |
/* | |
** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL | |
** if we run out of memory. | |
*/ | |
static VdbeCursor *allocateCursor( | |
Vdbe *p, /* The virtual machine */ | |
int iCur, /* Index of the new VdbeCursor */ | |
int nField, /* Number of fields in the table or index */ | |
int iDb, /* Database the cursor belongs to, or -1 */ | |
u8 eCurType /* Type of the new cursor */ | |
){ | |
/* Find the memory cell that will be used to store the blob of memory | |
** required for this VdbeCursor structure. It is convenient to use a | |
** vdbe memory cell to manage the memory allocation required for a | |
** VdbeCursor structure for the following reasons: | |
** | |
** * Sometimes cursor numbers are used for a couple of different | |
** purposes in a vdbe program. The different uses might require | |
** different sized allocations. Memory cells provide growable | |
** allocations. | |
** | |
** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can | |
** be freed lazily via the sqlite3_release_memory() API. This | |
** minimizes the number of malloc calls made by the system. | |
** | |
** The memory cell for cursor 0 is aMem[0]. The rest are allocated from | |
** the top of the register space. Cursor 1 is at Mem[p->nMem-1]. | |
** Cursor 2 is at Mem[p->nMem-2]. And so forth. | |
*/ | |
Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem; | |
int nByte; | |
VdbeCursor *pCx = 0; | |
nByte = | |
ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + | |
(eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0); | |
assert( iCur>=0 && iCur<p->nCursor ); | |
if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/ | |
/* Before calling sqlite3VdbeFreeCursor(), ensure the isEphemeral flag | |
** is clear. Otherwise, if this is an ephemeral cursor created by | |
** OP_OpenDup, the cursor will not be closed and will still be part | |
** of a BtShared.pCursor list. */ | |
if( p->apCsr[iCur]->pBtx==0 ) p->apCsr[iCur]->isEphemeral = 0; | |
sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); | |
p->apCsr[iCur] = 0; | |
} | |
if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){ | |
p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; | |
memset(pCx, 0, offsetof(VdbeCursor,pAltCursor)); | |
pCx->eCurType = eCurType; | |
pCx->iDb = iDb; | |
pCx->nField = nField; | |
pCx->aOffset = &pCx->aType[nField]; | |
if( eCurType==CURTYPE_BTREE ){ | |
pCx->uc.pCursor = (BtCursor*) | |
&pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField]; | |
sqlite3BtreeCursorZero(pCx->uc.pCursor); | |
} | |
} | |
return pCx; | |
} | |
/* | |
** The string in pRec is known to look like an integer and to have a | |
** floating point value of rValue. Return true and set *piValue to the | |
** integer value if the string is in range to be an integer. Otherwise, | |
** return false. | |
*/ | |
static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){ | |
i64 iValue = (double)rValue; | |
if( sqlite3RealSameAsInt(rValue,iValue) ){ | |
*piValue = iValue; | |
return 1; | |
} | |
return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc); | |
} | |
/* | |
** Try to convert a value into a numeric representation if we can | |
** do so without loss of information. In other words, if the string | |
** looks like a number, convert it into a number. If it does not | |
** look like a number, leave it alone. | |
** | |
** If the bTryForInt flag is true, then extra effort is made to give | |
** an integer representation. Strings that look like floating point | |
** values but which have no fractional component (example: '48.00') | |
** will have a MEM_Int representation when bTryForInt is true. | |
** | |
** If bTryForInt is false, then if the input string contains a decimal | |
** point or exponential notation, the result is only MEM_Real, even | |
** if there is an exact integer representation of the quantity. | |
*/ | |
static void applyNumericAffinity(Mem *pRec, int bTryForInt){ | |
double rValue; | |
u8 enc = pRec->enc; | |
int rc; | |
assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str ); | |
rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc); | |
if( rc<=0 ) return; | |
if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){ | |
pRec->flags |= MEM_Int; | |
}else{ | |
pRec->u.r = rValue; | |
pRec->flags |= MEM_Real; | |
if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec); | |
} | |
/* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the | |
** string representation after computing a numeric equivalent, because the | |
** string representation might not be the canonical representation for the | |
** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */ | |
pRec->flags &= ~MEM_Str; | |
} | |
/* | |
** Processing is determine by the affinity parameter: | |
** | |
** SQLITE_AFF_INTEGER: | |
** SQLITE_AFF_REAL: | |
** SQLITE_AFF_NUMERIC: | |
** Try to convert pRec to an integer representation or a | |
** floating-point representation if an integer representation | |
** is not possible. Note that the integer representation is | |
** always preferred, even if the affinity is REAL, because | |
** an integer representation is more space efficient on disk. | |
** | |
** SQLITE_AFF_TEXT: | |
** Convert pRec to a text representation. | |
** | |
** SQLITE_AFF_BLOB: | |
** SQLITE_AFF_NONE: | |
** No-op. pRec is unchanged. | |
*/ | |
static void applyAffinity( | |
Mem *pRec, /* The value to apply affinity to */ | |
char affinity, /* The affinity to be applied */ | |
u8 enc /* Use this text encoding */ | |
){ | |
if( affinity>=SQLITE_AFF_NUMERIC ){ | |
assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL | |
|| affinity==SQLITE_AFF_NUMERIC ); | |
if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/ | |
if( (pRec->flags & MEM_Real)==0 ){ | |
if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); | |
}else{ | |
sqlite3VdbeIntegerAffinity(pRec); | |
} | |
} | |
}else if( affinity==SQLITE_AFF_TEXT ){ | |
/* Only attempt the conversion to TEXT if there is an integer or real | |
** representation (blob and NULL do not get converted) but no string | |
** representation. It would be harmless to repeat the conversion if | |
** there is already a string rep, but it is pointless to waste those | |
** CPU cycles. */ | |
if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/ | |
if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){ | |
testcase( pRec->flags & MEM_Int ); | |
testcase( pRec->flags & MEM_Real ); | |
testcase( pRec->flags & MEM_IntReal ); | |
sqlite3VdbeMemStringify(pRec, enc, 1); | |
} | |
} | |
pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal); | |
} | |
} | |
/* | |
** Try to convert the type of a function argument or a result column | |
** into a numeric representation. Use either INTEGER or REAL whichever | |
** is appropriate. But only do the conversion if it is possible without | |
** loss of information and return the revised type of the argument. | |
*/ | |
int sqlite3_value_numeric_type(sqlite3_value *pVal){ | |
int eType = sqlite3_value_type(pVal); | |
if( eType==SQLITE_TEXT ){ | |
Mem *pMem = (Mem*)pVal; | |
applyNumericAffinity(pMem, 0); | |
eType = sqlite3_value_type(pVal); | |
} | |
return eType; | |
} | |
/* | |
** Exported version of applyAffinity(). This one works on sqlite3_value*, | |
** not the internal Mem* type. | |
*/ | |
void sqlite3ValueApplyAffinity( | |
sqlite3_value *pVal, | |
u8 affinity, | |
u8 enc | |
){ | |
applyAffinity((Mem *)pVal, affinity, enc); | |
} | |
/* | |
** pMem currently only holds a string type (or maybe a BLOB that we can | |
** interpret as a string if we want to). Compute its corresponding | |
** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields | |
** accordingly. | |
*/ | |
static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ | |
int rc; | |
sqlite3_int64 ix; | |
assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ); | |
assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); | |
ExpandBlob(pMem); | |
rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc); | |
if( rc<=0 ){ | |
if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){ | |
pMem->u.i = ix; | |
return MEM_Int; | |
}else{ | |
return MEM_Real; | |
} | |
}else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){ | |
pMem->u.i = ix; | |
return MEM_Int; | |
} | |
return MEM_Real; | |
} | |
/* | |
** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or | |
** none. | |
** | |
** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. | |
** But it does set pMem->u.r and pMem->u.i appropriately. | |
*/ | |
static u16 numericType(Mem *pMem){ | |
if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){ | |
testcase( pMem->flags & MEM_Int ); | |
testcase( pMem->flags & MEM_Real ); | |
testcase( pMem->flags & MEM_IntReal ); | |
return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal); | |
} | |
if( pMem->flags & (MEM_Str|MEM_Blob) ){ | |
testcase( pMem->flags & MEM_Str ); | |
testcase( pMem->flags & MEM_Blob ); | |
return computeNumericType(pMem); | |
} | |
return 0; | |
} | |
#ifdef SQLITE_DEBUG | |
/* | |
** Write a nice string representation of the contents of cell pMem | |
** into buffer zBuf, length nBuf. | |
*/ | |
void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){ | |
int f = pMem->flags; | |
static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; | |
if( f&MEM_Blob ){ | |
int i; | |
char c; | |
if( f & MEM_Dyn ){ | |
c = 'z'; | |
assert( (f & (MEM_Static|MEM_Ephem))==0 ); | |
}else if( f & MEM_Static ){ | |
c = 't'; | |
assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); | |
}else if( f & MEM_Ephem ){ | |
c = 'e'; | |
assert( (f & (MEM_Static|MEM_Dyn))==0 ); | |
}else{ | |
c = 's'; | |
} | |
sqlite3_str_appendf(pStr, "%cx[", c); | |
for(i=0; i<25 && i<pMem->n; i++){ | |
sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF)); | |
} | |
sqlite3_str_appendf(pStr, "|"); | |
for(i=0; i<25 && i<pMem->n; i++){ | |
char z = pMem->z[i]; | |
sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z); | |
} | |
sqlite3_str_appendf(pStr,"]"); | |
if( f & MEM_Zero ){ | |
sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero); | |
} | |
}else if( f & MEM_Str ){ | |
int j; | |
u8 c; | |
if( f & MEM_Dyn ){ | |
c = 'z'; | |
assert( (f & (MEM_Static|MEM_Ephem))==0 ); | |
}else if( f & MEM_Static ){ | |
c = 't'; | |
assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); | |
}else if( f & MEM_Ephem ){ | |
c = 'e'; | |
assert( (f & (MEM_Static|MEM_Dyn))==0 ); | |
}else{ | |
c = 's'; | |
} | |
sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n); | |
for(j=0; j<25 && j<pMem->n; j++){ | |
c = pMem->z[j]; | |
sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.'); | |
} | |
sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]); | |
} | |
} | |
#endif | |
#ifdef SQLITE_DEBUG | |
/* | |
** Print the value of a register for tracing purposes: | |
*/ | |
static void memTracePrint(Mem *p){ | |
if( p->flags & MEM_Undefined ){ | |
printf(" undefined"); | |
}else if( p->flags & MEM_Null ){ | |
printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL"); | |
}else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ | |
printf(" si:%lld", p->u.i); | |
}else if( (p->flags & (MEM_IntReal))!=0 ){ | |
printf(" ir:%lld", p->u.i); | |
}else if( p->flags & MEM_Int ){ | |
printf(" i:%lld", p->u.i); | |
#ifndef SQLITE_OMIT_FLOATING_POINT | |
}else if( p->flags & MEM_Real ){ | |
printf(" r:%.17g", p->u.r); | |
#endif | |
}else if( sqlite3VdbeMemIsRowSet(p) ){ | |
printf(" (rowset)"); | |
}else{ | |
StrAccum acc; | |
char zBuf[1000]; | |
sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); | |
sqlite3VdbeMemPrettyPrint(p, &acc); | |
printf(" %s", sqlite3StrAccumFinish(&acc)); | |
} | |
if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype); | |
} | |
static void registerTrace(int iReg, Mem *p){ | |
printf("R[%d] = ", iReg); | |
memTracePrint(p); | |
if( p->pScopyFrom ){ | |
printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg])); | |
} | |
printf("\n"); | |
sqlite3VdbeCheckMemInvariants(p); | |
} | |
#endif | |
#ifdef SQLITE_DEBUG | |
/* | |
** Show the values of all registers in the virtual machine. Used for | |
** interactive debugging. | |
*/ | |
void sqlite3VdbeRegisterDump(Vdbe *v){ | |
int i; | |
for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i); | |
} | |
#endif /* SQLITE_DEBUG */ | |
#ifdef SQLITE_DEBUG | |
# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) | |
#else | |
# define REGISTER_TRACE(R,M) | |
#endif | |
#ifdef VDBE_PROFILE | |
/* | |
** hwtime.h contains inline assembler code for implementing | |
** high-performance timing routines. | |
*/ | |
#include "hwtime.h" | |
#endif | |
#ifndef NDEBUG | |
/* | |
** This function is only called from within an assert() expression. It | |
** checks that the sqlite3.nTransaction variable is correctly set to | |
** the number of non-transaction savepoints currently in the | |
** linked list starting at sqlite3.pSavepoint. | |
** | |
** Usage: | |
** | |
** assert( checkSavepointCount(db) ); | |
*/ | |
static int checkSavepointCount(sqlite3 *db){ | |
int n = 0; | |
Savepoint *p; | |
for(p=db->pSavepoint; p; p=p->pNext) n++; | |
assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); | |
return 1; | |
} | |
#endif | |
/* | |
** Return the register of pOp->p2 after first preparing it to be | |
** overwritten with an integer value. | |
*/ | |
static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){ | |
sqlite3VdbeMemSetNull(pOut); | |
pOut->flags = MEM_Int; | |
return pOut; | |
} | |
static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ | |
Mem *pOut; | |
assert( pOp->p2>0 ); | |
assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); | |
pOut = &p->aMem[pOp->p2]; | |
memAboutToChange(p, pOut); | |
if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/ | |
return out2PrereleaseWithClear(pOut); | |
}else{ | |
pOut->flags = MEM_Int; | |
return pOut; | |
} | |
} | |
/* | |
** Execute as much of a VDBE program as we can. | |
** This is the core of sqlite3_step(). | |
*/ | |
int sqlite3VdbeExec( | |
Vdbe *p /* The VDBE */ | |
){ | |
Op *aOp = p->aOp; /* Copy of p->aOp */ | |
Op *pOp = aOp; /* Current operation */ | |
#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) | |
Op *pOrigOp; /* Value of pOp at the top of the loop */ | |
#endif | |
#ifdef SQLITE_DEBUG | |
int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */ | |
#endif | |
int rc = SQLITE_OK; /* Value to return */ | |
sqlite3 *db = p->db; /* The database */ | |
u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ | |
u8 encoding = ENC(db); /* The database encoding */ | |
int iCompare = 0; /* Result of last comparison */ | |
u64 nVmStep = 0; /* Number of virtual machine steps */ | |
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK | |
u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */ | |
#endif | |
Mem *aMem = p->aMem; /* Copy of p->aMem */ | |
Mem *pIn1 = 0; /* 1st input operand */ | |
Mem *pIn2 = 0; /* 2nd input operand */ | |
Mem *pIn3 = 0; /* 3rd input operand */ | |
Mem *pOut = 0; /* Output operand */ | |
#ifdef VDBE_PROFILE | |
u64 start; /* CPU clock count at start of opcode */ | |
#endif | |
/*** INSERT STACK UNION HERE ***/ | |
assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ | |
sqlite3VdbeEnter(p); | |
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK | |
if( db->xProgress ){ | |
u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; | |
assert( 0 < db->nProgressOps ); | |
nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); | |
}else{ | |
nProgressLimit = LARGEST_UINT64; | |
} | |
#endif | |
if( p->rc==SQLITE_NOMEM ){ | |
/* This happens if a malloc() inside a call to sqlite3_column_text() or | |
** sqlite3_column_text16() failed. */ | |
goto no_mem; | |
} | |
assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); | |
testcase( p->rc!=SQLITE_OK ); | |
p->rc = SQLITE_OK; | |
assert( p->bIsReader || p->readOnly!=0 ); | |
p->iCurrentTime = 0; | |
assert( p->explain==0 ); | |
p->pResultSet = 0; | |
db->busyHandler.nBusy = 0; | |
if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt; | |
sqlite3VdbeIOTraceSql(p); | |
#ifdef SQLITE_DEBUG | |
sqlite3BeginBenignMalloc(); | |
if( p->pc==0 | |
&& (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0 | |
){ | |
int i; | |
int once = 1; | |
sqlite3VdbePrintSql(p); | |
if( p->db->flags & SQLITE_VdbeListing ){ | |
printf("VDBE Program Listing:\n"); | |
for(i=0; i<p->nOp; i++){ | |
sqlite3VdbePrintOp(stdout, i, &aOp[i]); | |
} | |
} | |
if( p->db->flags & SQLITE_VdbeEQP ){ | |
for(i=0; i<p->nOp; i++){ | |
if( aOp[i].opcode==OP_Explain ){ | |
if( once ) printf("VDBE Query Plan:\n"); | |
printf("%s\n", aOp[i].p4.z); | |
once = 0; | |
} | |
} | |
} | |
if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); | |
} | |
sqlite3EndBenignMalloc(); | |
#endif | |
for(pOp=&aOp[p->pc]; 1; pOp++){ | |
/* Errors are detected by individual opcodes, with an immediate | |
** jumps to abort_due_to_error. */ | |
assert( rc==SQLITE_OK ); | |
assert( pOp>=aOp && pOp<&aOp[p->nOp]); | |
#ifdef VDBE_PROFILE | |
start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); | |
#endif | |
nVmStep++; | |
#ifdef SQLITE_ENABLE_STMT_SCANSTATUS | |
if( p->anExec ) p->anExec[(int)(pOp-aOp)]++; | |
#endif | |
/* Only allow tracing if SQLITE_DEBUG is defined. | |
*/ | |
#ifdef SQLITE_DEBUG | |
if( db->flags & SQLITE_VdbeTrace ){ | |
sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp); | |
test_trace_breakpoint((int)(pOp - aOp),pOp,p); | |
} | |
#endif | |
/* Check to see if we need to simulate an interrupt. This only happens | |
** if we have a special test build. | |
*/ | |
#ifdef SQLITE_TEST | |
if( sqlite3_interrupt_count>0 ){ | |
sqlite3_interrupt_count--; | |
if( sqlite3_interrupt_count==0 ){ | |
sqlite3_interrupt(db); | |
} | |
} | |
#endif | |
/* Sanity checking on other operands */ | |
#ifdef SQLITE_DEBUG | |
{ | |
u8 opProperty = sqlite3OpcodeProperty[pOp->opcode]; | |
if( (opProperty & OPFLG_IN1)!=0 ){ | |
assert( pOp->p1>0 ); | |
assert( pOp->p1<=(p->nMem+1 - p->nCursor) ); | |
assert( memIsValid(&aMem[pOp->p1]) ); | |
assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); | |
REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); | |
} | |
if( (opProperty & OPFLG_IN2)!=0 ){ | |
assert( pOp->p2>0 ); | |
assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); | |
assert( memIsValid(&aMem[pOp->p2]) ); | |
assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); | |
REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); | |
} | |
if( (opProperty & OPFLG_IN3)!=0 ){ | |
assert( pOp->p3>0 ); | |
assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); | |
assert( memIsValid(&aMem[pOp->p3]) ); | |
assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); | |
REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); | |
} | |
if( (opProperty & OPFLG_OUT2)!=0 ){ | |
assert( pOp->p2>0 ); | |
assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); | |
memAboutToChange(p, &aMem[pOp->p2]); | |
} | |
if( (opProperty & OPFLG_OUT3)!=0 ){ | |
assert( pOp->p3>0 ); | |
assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); | |
memAboutToChange(p, &aMem[pOp->p3]); | |
} | |
} | |
#endif | |
#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) | |
pOrigOp = pOp; | |
#endif | |
switch( pOp->opcode ){ | |
/***************************************************************************** | |
** What follows is a massive switch statement where each case implements a | |
** separate instruction in the virtual machine. If we follow the usual | |
** indentation conventions, each case should be indented by 6 spaces. But | |
** that is a lot of wasted space on the left margin. So the code within | |
** the switch statement will break with convention and be flush-left. Another | |
** big comment (similar to this one) will mark the point in the code where | |
** we transition back to normal indentation. | |
** | |
** The formatting of each case is important. The makefile for SQLite | |
** generates two C files "opcodes.h" and "opcodes.c" by scanning this | |
** file looking for lines that begin with "case OP_". The opcodes.h files | |
** will be filled with #defines that give unique integer values to each | |
** opcode and the opcodes.c file is filled with an array of strings where | |
** each string is the symbolic name for the corresponding opcode. If the | |
** case statement is followed by a comment of the form "/# same as ... #/" | |
** that comment is used to determine the particular value of the opcode. | |
** | |
** Other keywords in the comment that follows each case are used to | |
** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. | |
** Keywords include: in1, in2, in3, out2, out3. See | |
** the mkopcodeh.awk script for additional information. | |
** | |
** Documentation about VDBE opcodes is generated by scanning this file | |
** for lines of that contain "Opcode:". That line and all subsequent | |
** comment lines are used in the generation of the opcode.html documentation | |
** file. | |
** | |
** SUMMARY: | |
** | |
** Formatting is important to scripts that scan this file. | |
** Do not deviate from the formatting style currently in use. | |
** | |
*****************************************************************************/ | |
/* Opcode: Goto * P2 * * * | |
** | |
** An unconditional jump to address P2. | |
** The next instruction executed will be | |
** the one at index P2 from the beginning of | |
** the program. | |
** | |
** The P1 parameter is not actually used by this opcode. However, it | |
** is sometimes set to 1 instead of 0 as a hint to the command-line shell | |
** that this Goto is the bottom of a loop and that the lines from P2 down | |
** to the current line should be indented for EXPLAIN output. | |
*/ | |
case OP_Goto: { /* jump */ | |
#ifdef SQLITE_DEBUG | |
/* In debuggging mode, when the p5 flags is set on an OP_Goto, that | |
** means we should really jump back to the preceeding OP_ReleaseReg | |
** instruction. */ | |
if( pOp->p5 ){ | |
assert( pOp->p2 < (int)(pOp - aOp) ); | |
assert( pOp->p2 > 1 ); | |
pOp = &aOp[pOp->p2 - 2]; | |
assert( pOp[1].opcode==OP_ReleaseReg ); | |
goto check_for_interrupt; | |
} | |
#endif | |
jump_to_p2_and_check_for_interrupt: | |
pOp = &aOp[pOp->p2 - 1]; | |
/* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev, | |
** OP_VNext, or OP_SorterNext) all jump here upon | |
** completion. Check to see if sqlite3_interrupt() has been called | |
** or if the progress callback needs to be invoked. | |
** | |
** This code uses unstructured "goto" statements and does not look clean. | |
** But that is not due to sloppy coding habits. The code is written this | |
** way for performance, to avoid having to run the interrupt and progress | |
** checks on every opcode. This helps sqlite3_step() to run about 1.5% | |
** faster according to "valgrind --tool=cachegrind" */ | |
check_for_interrupt: | |
if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt; | |
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK | |
/* Call the progress callback if it is configured and the required number | |
** of VDBE ops have been executed (either since this invocation of | |
** sqlite3VdbeExec() or since last time the progress callback was called). | |
** If the progress callback returns non-zero, exit the virtual machine with | |
** a return code SQLITE_ABORT. | |
*/ | |
while( nVmStep>=nProgressLimit && db->xProgress!=0 ){ | |
assert( db->nProgressOps!=0 ); | |
nProgressLimit += db->nProgressOps; | |
if( db->xProgress(db->pProgressArg) ){ | |
nProgressLimit = LARGEST_UINT64; | |
rc = SQLITE_INTERRUPT; | |
goto abort_due_to_error; | |
} | |
} | |
#endif | |
break; | |
} | |
/* Opcode: Gosub P1 P2 * * * | |
** | |
** Write the current address onto register P1 | |
** and then jump to address P2. | |
*/ | |
case OP_Gosub: { /* jump */ | |
assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); | |
pIn1 = &aMem[pOp->p1]; | |
assert( VdbeMemDynamic(pIn1)==0 ); | |
memAboutToChange(p, pIn1); | |
pIn1->flags = MEM_Int; | |
pIn1->u.i = (int)(pOp-aOp); | |
REGISTER_TRACE(pOp->p1, pIn1); | |
/* Most jump operations do a goto to this spot in order to update | |
** the pOp pointer. */ | |
jump_to_p2: | |
pOp = &aOp[pOp->p2 - 1]; | |
break; | |
} | |
/* Opcode: Return P1 * * * * | |
** | |
** Jump to the next instruction after the address in register P1. After | |
** the jump, register P1 becomes undefined. | |
*/ | |
case OP_Return: { /* in1 */ | |
pIn1 = &aMem[pOp->p1]; | |
assert( pIn1->flags==MEM_Int ); | |
pOp = &aOp[pIn1->u.i]; | |
pIn1->flags = MEM_Undefined; | |
break; | |
} | |
/* Opcode: InitCoroutine P1 P2 P3 * * | |
** | |
** Set up register P1 so that it will Yield to the coroutine | |
** located at address P3. | |
** | |
** If P2!=0 then the coroutine implementation immediately follows | |
** this opcode. So jump over the coroutine implementation to | |
** address P2. | |
** | |
** See also: EndCoroutine | |
*/ | |
case OP_InitCoroutine: { /* jump */ | |
assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); | |
assert( pOp->p2>=0 && pOp->p2<p->nOp ); | |
assert( pOp->p3>=0 && pOp->p3<p->nOp ); | |
pOut = &aMem[pOp->p1]; | |
assert( !VdbeMemDynamic(pOut) ); | |
pOut->u.i = pOp->p3 - 1; | |
pOut->flags = MEM_Int; | |
if( pOp->p2 ) goto jump_to_p2; | |
break; | |
} | |
/* Opcode: EndCoroutine P1 * * * * | |
** | |
** The instruction at the address in register P1 is a Yield. | |
** Jump to the P2 parameter of that Yield. | |
** After the jump, register P1 becomes undefined. | |
** | |
** See also: InitCoroutine | |
*/ | |
case OP_EndCoroutine: { /* in1 */ | |
VdbeOp *pCaller; | |
pIn1 = &aMem[pOp->p1]; | |
assert( pIn1->flags==MEM_Int ); | |
assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp ); | |
pCaller = &aOp[pIn1->u.i]; | |
assert( pCaller->opcode==OP_Yield ); | |
assert( pCaller->p2>=0 && pCaller->p2<p->nOp ); | |
pOp = &aOp[pCaller->p2 - 1]; | |
pIn1->flags = MEM_Undefined; | |
break; | |
} | |
/* Opcode: Yield P1 P2 * * * | |
** | |
** Swap the program counter with the value in register P1. This | |
** has the effect of yielding to a coroutine. | |
** | |
** If the coroutine that is launched by this instruction ends with | |
** Yield or Return then continue to the next instruction. But if | |
** the coroutine launched by this instruction ends with | |
** EndCoroutine, then jump to P2 rather than continuing with the | |
** next instruction. | |
** | |
** See also: InitCoroutine | |
*/ | |
case OP_Yield: { /* in1, jump */ | |
int pcDest; | |
pIn1 = &aMem[pOp->p1]; | |
assert( VdbeMemDynamic(pIn1)==0 ); | |
pIn1->flags = MEM_Int; | |
pcDest = (int)pIn1->u.i; | |
pIn1->u.i = (int)(pOp - aOp); | |
REGISTER_TRACE(pOp->p1, pIn1); | |
pOp = &aOp[pcDest]; | |
break; | |
} | |
/* Opcode: HaltIfNull P1 P2 P3 P4 P5 | |
** Synopsis: if r[P3]=null halt | |
** | |
** Check the value in register P3. If it is NULL then Halt using | |
** parameter P1, P2, and P4 as if this were a Halt instruction. If the | |
** value in register P3 is not NULL, then this routine is a no-op. | |
** The P5 parameter should be 1. | |
*/ | |
case OP_HaltIfNull: { /* in3 */ | |
pIn3 = &aMem[pOp->p3]; | |
#ifdef SQLITE_DEBUG | |
if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } | |
#endif | |
if( (pIn3->flags & MEM_Null)==0 ) break; | |
/* Fall through into OP_Halt */ | |
/* no break */ deliberate_fall_through | |
} | |
/* Opcode: Halt P1 P2 * P4 P5 | |
** | |
** Exit immediately. All open cursors, etc are closed | |
** automatically. | |
** | |
** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), | |
** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). | |
** For errors, it can be some other value. If P1!=0 then P2 will determine | |
** whether or not to rollback the current transaction. Do not rollback | |
** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, | |
** then back out all changes that have occurred during this execution of the | |
** VDBE, but do not rollback the transaction. | |
** | |
** If P4 is not null then it is an error message string. | |
** | |
** P5 is a value between 0 and 4, inclusive, that modifies the P4 string. | |
** | |
** 0: (no change) | |
** 1: NOT NULL contraint failed: P4 | |
** 2: UNIQUE constraint failed: P4 | |
** 3: CHECK constraint failed: P4 | |
** 4: FOREIGN KEY constraint failed: P4 | |
** | |
** If P5 is not zero and P4 is NULL, then everything after the ":" is | |
** omitted. | |
** | |
** There is an implied "Halt 0 0 0" instruction inserted at the very end of | |
** every program. So a jump past the last instruction of the program | |
** is the same as executing Halt. | |
*/ | |
case OP_Halt: { | |
VdbeFrame *pFrame; | |
int pcx; | |
pcx = (int)(pOp - aOp); | |
#ifdef SQLITE_DEBUG | |
if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } | |
#endif | |
if( pOp->p1==SQLITE_OK && p->pFrame ){ | |
/* Halt the sub-program. Return control to the parent frame. */ | |
pFrame = p->pFrame; | |
p->pFrame = pFrame->pParent; | |
p->nFrame--; | |
sqlite3VdbeSetChanges(db, p->nChange); | |
pcx = sqlite3VdbeFrameRestore(pFrame); | |
if( pOp->p2==OE_Ignore ){ | |
/* Instruction pcx is the OP_Program that invoked the sub-program | |
** currently being halted. If the p2 instruction of this OP_Halt | |
** instruction is set to OE_Ignore, then the sub-program is throwing | |
** an IGNORE exception. In this case jump to the address specified | |
** as the p2 of the calling OP_Program. */ | |
pcx = p->aOp[pcx].p2-1; | |
} | |
aOp = p->aOp; | |
aMem = p->aMem; | |
pOp = &aOp[pcx]; | |
break; | |
} | |
p->rc = pOp->p1; | |
p->errorAction = (u8)pOp->p2; | |
p->pc = pcx; | |
assert( pOp->p5<=4 ); | |
if( p->rc ){ | |
if( pOp->p5 ){ | |
static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", | |
"FOREIGN KEY" }; | |
testcase( pOp->p5==1 ); | |
testcase( pOp->p5==2 ); | |
testcase( pOp->p5==3 ); | |
testcase( pOp->p5==4 ); | |
sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]); | |
if( pOp->p4.z ){ | |
p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z); | |
} | |
}else{ | |
sqlite3VdbeError(p, "%s", pOp->p4.z); | |
} | |
sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg); | |
} | |
rc = sqlite3VdbeHalt(p); | |
assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); | |
if( rc==SQLITE_BUSY ){ | |
p->rc = SQLITE_BUSY; | |
}else{ | |
assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); | |
assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); | |
rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; | |
} | |
goto vdbe_return; | |
} | |
/* Opcode: Integer P1 P2 * * * | |
** Synopsis: r[P2]=P1 | |
** | |
** The 32-bit integer value P1 is written into register P2. | |
*/ | |
case OP_Integer: { /* out2 */ | |
pOut = out2Prerelease(p, pOp); | |
pOut->u.i = pOp->p1; | |
break; | |
} | |
/* Opcode: Int64 * P2 * P4 * | |
** Synopsis: r[P2]=P4 | |
** | |
** P4 is a pointer to a 64-bit integer value. | |
** Write that value into register P2. | |
*/ | |
case OP_Int64: { /* out2 */ | |
pOut = out2Prerelease(p, pOp); | |
assert( pOp->p4.pI64!=0 ); | |
pOut->u.i = *pOp->p4.pI64; | |
break; | |
} | |
#ifndef SQLITE_OMIT_FLOATING_POINT | |
/* Opcode: Real * P2 * P4 * | |
** Synopsis: r[P2]=P4 | |
** | |
** P4 is a pointer to a 64-bit floating point value. | |
** Write that value into register P2. | |
*/ | |
case OP_Real: { /* same as TK_FLOAT, out2 */ | |
pOut = out2Prerelease(p, pOp); | |
pOut->flags = MEM_Real; | |
assert( !sqlite3IsNaN(*pOp->p4.pReal) ); | |
pOut->u.r = *pOp->p4.pReal; | |
break; | |
} | |
#endif | |
/* Opcode: String8 * P2 * P4 * | |
** Synopsis: r[P2]='P4' | |
** | |
** P4 points to a nul terminated UTF-8 string. This opcode is transformed | |
** into a String opcode before it is executed for the first time. During | |
** this transformation, the length of string P4 is computed and stored | |
** as the P1 parameter. | |
*/ | |
case OP_String8: { /* same as TK_STRING, out2 */ | |
assert( pOp->p4.z!=0 ); | |
pOut = out2Prerelease(p, pOp); | |
pOp->p1 = sqlite3Strlen30(pOp->p4.z); | |
#ifndef SQLITE_OMIT_UTF16 | |
if( encoding!=SQLITE_UTF8 ){ | |
rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); | |
assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG ); | |
if( rc ) goto too_big; | |
if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; | |
assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); | |
assert( VdbeMemDynamic(pOut)==0 ); | |
pOut->szMalloc = 0; | |
pOut->flags |= MEM_Static; | |
if( pOp->p4type==P4_DYNAMIC ){ | |
sqlite3DbFree(db, pOp->p4.z); | |
} | |
pOp->p4type = P4_DYNAMIC; | |
pOp->p4.z = pOut->z; | |
pOp->p1 = pOut->n; | |
} | |
#endif | |
if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ | |
goto too_big; | |
} | |
pOp->opcode = OP_String; | |
assert( rc==SQLITE_OK ); | |
/* Fall through to the next case, OP_String */ | |
/* no break */ deliberate_fall_through | |
} | |
/* Opcode: String P1 P2 P3 P4 P5 | |
** Synopsis: r[P2]='P4' (len=P1) | |
** | |
** The string value P4 of length P1 (bytes) is stored in register P2. | |
** | |
** If P3 is not zero and the content of register P3 is equal to P5, then | |
** the datatype of the register P2 is converted to BLOB. The content is | |
** the same sequence of bytes, it is merely interpreted as a BLOB instead | |
** of a string, as if it had been CAST. In other words: | |
** | |
** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB) | |
*/ | |
case OP_String: { /* out2 */ | |
assert( pOp->p4.z!=0 ); | |
pOut = out2Prerelease(p, pOp); | |
pOut->flags = MEM_Str|MEM_Static|MEM_Term; | |
pOut->z = pOp->p4.z; | |
pOut->n = pOp->p1; | |
pOut->enc = encoding; | |
UPDATE_MAX_BLOBSIZE(pOut); | |
#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS | |
if( pOp->p3>0 ){ | |
assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); | |
pIn3 = &aMem[pOp->p3]; | |
assert( pIn3->flags & MEM_Int ); | |
if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term; | |
} | |
#endif | |
break; | |
} | |
/* Opcode: Null P1 P2 P3 * * | |
** Synopsis: r[P2..P3]=NULL | |
** | |
** Write a NULL into registers P2. If P3 greater than P2, then also write | |
** NULL into register P3 and every register in between P2 and P3. If P3 | |
** is less than P2 (typically P3 is zero) then only register P2 is | |
** set to NULL. | |
** | |
** If the P1 value is non-zero, then also set the MEM_Cleared flag so that | |
** NULL values will not compare equal even if SQLITE_NULLEQ is set on | |
** OP_Ne or OP_Eq. | |
*/ | |
case OP_Null: { /* out2 */ | |
int cnt; | |
u16 nullFlag; | |
pOut = out2Prerelease(p, pOp); | |
cnt = pOp->p3-pOp->p2; | |
assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); | |
pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; | |
pOut->n = 0; | |
#ifdef SQLITE_DEBUG | |
pOut->uTemp = 0; | |
#endif | |
while( cnt>0 ){ | |
pOut++; | |
memAboutToChange(p, pOut); | |
sqlite3VdbeMemSetNull(pOut); | |
pOut->flags = nullFlag; | |
pOut->n = 0; | |
cnt--; | |
} | |
break; | |
} | |
/* Opcode: SoftNull P1 * * * * | |
** Synopsis: r[P1]=NULL | |
** | |
** Set register P1 to have the value NULL as seen by the OP_MakeRecord | |
** instruction, but do not free any string or blob memory associated with | |
** the register, so that if the value was a string or blob that was | |
** previously copied using OP_SCopy, the copies will continue to be valid. | |
*/ | |
case OP_SoftNull: { | |
assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); | |
pOut = &aMem[pOp->p1]; | |
pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null; | |
break; | |
} | |
/* Opcode: Blob P1 P2 * P4 * | |
** Synopsis: r[P2]=P4 (len=P1) | |
** | |
** P4 points to a blob of data P1 bytes long. Store this | |
** blob in register P2. | |
*/ | |
case OP_Blob: { /* out2 */ | |
assert( pOp->p1 <= SQLITE_MAX_LENGTH ); | |
pOut = out2Prerelease(p, pOp); | |
sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); | |
pOut->enc = encoding; | |
UPDATE_MAX_BLOBSIZE(pOut); | |
break; | |
} | |
/* Opcode: Variable P1 P2 * P4 * | |
** Synopsis: r[P2]=parameter(P1,P4) | |
** | |
** Transfer the values of bound parameter P1 into register P2 | |
** | |
** If the parameter is named, then its name appears in P4. | |
** The P4 value is used by sqlite3_bind_parameter_name(). | |
*/ | |
case OP_Variable: { /* out2 */ | |
Mem *pVar; /* Value being transferred */ | |
assert( pOp->p1>0 && pOp->p1<=p->nVar ); | |
assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) ); | |
pVar = &p->aVar[pOp->p1 - 1]; | |
if( sqlite3VdbeMemTooBig(pVar) ){ | |
goto too_big; | |
} | |
pOut = &aMem[pOp->p2]; | |
if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut); | |
memcpy(pOut, pVar, MEMCELLSIZE); | |
pOut->flags &= ~(MEM_Dyn|MEM_Ephem); | |
pOut->flags |= MEM_Static|MEM_FromBind; | |
UPDATE_MAX_BLOBSIZE(pOut); | |
break; | |
} | |
/* Opcode: Move P1 P2 P3 * * | |
** Synopsis: r[P2@P3]=r[P1@P3] | |
** | |
** Move the P3 values in register P1..P1+P3-1 over into | |
** registers P2..P2+P3-1. Registers P1..P1+P3-1 are | |
** left holding a NULL. It is an error for register ranges | |
** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error | |
** for P3 to be less than 1. | |
*/ | |
case OP_Move: { | |
int n; /* Number of registers left to copy */ | |
int p1; /* Register to copy from */ | |
int p2; /* Register to copy to */ | |
n = pOp->p3; | |
p1 = pOp->p1; | |
p2 = pOp->p2; | |
assert( n>0 && p1>0 && p2>0 ); | |
assert( p1+n<=p2 || p2+n<=p1 ); | |
pIn1 = &aMem[p1]; | |
pOut = &aMem[p2]; | |
do{ | |
assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] ); | |
assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] ); | |
assert( memIsValid(pIn1) ); | |
memAboutToChange(p, pOut); | |
sqlite3VdbeMemMove(pOut, pIn1); | |
#ifdef SQLITE_DEBUG | |
pIn1->pScopyFrom = 0; | |
{ int i; | |
for(i=1; i<p->nMem; i++){ | |
if( aMem[i].pScopyFrom==pIn1 ){ | |
aMem[i].pScopyFrom = pOut; | |
} | |
} | |
} | |
#endif | |
Deephemeralize(pOut); | |
REGISTER_TRACE(p2++, pOut); | |
pIn1++; | |
pOut++; | |
}while( --n ); | |
break; | |
} | |
/* Opcode: Copy P1 P2 P3 * * | |
** Synopsis: r[P2@P3+1]=r[P1@P3+1] | |
** | |
** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. | |
** | |
** This instruction makes a deep copy of the value. A duplicate | |
** is made of any string or blob constant. See also OP_SCopy. | |
*/ | |
case OP_Copy: { | |
int n; | |
n = pOp->p3; | |
pIn1 = &aMem[pOp->p1]; | |
pOut = &aMem[pOp->p2]; | |
assert( pOut!=pIn1 ); | |
while( 1 ){ | |
memAboutToChange(p, pOut); | |
sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); | |
Deephemeralize(pOut); | |
#ifdef SQLITE_DEBUG | |
pOut->pScopyFrom = 0; | |
#endif | |
REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut); | |
if( (n--)==0 ) break; | |
pOut++; | |
pIn1++; | |
} | |
break; | |
} | |
/* Opcode: SCopy P1 P2 * * * | |
** Synopsis: r[P2]=r[P1] | |
** | |
** Make a shallow copy of register P1 into register P2. | |
** | |
** This instruction makes a shallow copy of the value. If the value | |
** is a string or blob, then the copy is only a pointer to the | |
** original and hence if the original changes so will the copy. | |
** Worse, if the original is deallocated, the copy becomes invalid. | |
** Thus the program must guarantee that the original will not change | |
** during the lifetime of the copy. Use OP_Copy to make a complete | |
** copy. | |
*/ | |
case OP_SCopy: { /* out2 */ | |
pIn1 = &aMem[pOp->p1]; | |
pOut = &aMem[pOp->p2]; | |
assert( pOut!=pIn1 ); | |
sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); | |
#ifdef SQLITE_DEBUG | |
pOut->pScopyFrom = pIn1; | |
pOut->mScopyFlags = pIn1->flags; | |
#endif | |
break; | |
} | |
/* Opcode: IntCopy P1 P2 * * * | |
** Synopsis: r[P2]=r[P1] | |
** | |
** Transfer the integer value held in register P1 into register P2. | |
** | |
** This is an optimized version of SCopy that works only for integer | |
** values. | |
*/ | |
case OP_IntCopy: { /* out2 */ | |
pIn1 = &aMem[pOp->p1]; | |
assert( (pIn1->flags & MEM_Int)!=0 ); | |
pOut = &aMem[pOp->p2]; | |
sqlite3VdbeMemSetInt64(pOut, pIn1->u.i); | |
break; | |
} | |
/* Opcode: ResultRow P1 P2 * * * | |
** Synopsis: output=r[P1@P2] | |
** | |
** The registers P1 through P1+P2-1 contain a single row of | |
** results. This opcode causes the sqlite3_step() call to terminate | |
** with an SQLITE_ROW return code and it sets up the sqlite3_stmt | |
** structure to provide access to the r(P1)..r(P1+P2-1) values as | |
** the result row. | |
*/ | |
case OP_ResultRow: { | |
Mem *pMem; | |
int i; | |
assert( p->nResColumn==pOp->p2 ); | |
assert( pOp->p1>0 ); | |
assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); | |
/* If this statement has violated immediate foreign key constraints, do | |
** not return the number of rows modified. And do not RELEASE the statement | |
** transaction. It needs to be rolled back. */ | |
if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){ | |
assert( db->flags&SQLITE_CountRows ); | |
assert( p->usesStmtJournal ); | |
goto abort_due_to_error; | |
} | |
/* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then | |
** DML statements invoke this opcode to return the number of rows | |
** modified to the user. This is the only way that a VM that | |
** opens a statement transaction may invoke this opcode. | |
** | |
** In case this is such a statement, close any statement transaction | |
** opened by this VM before returning control to the user. This is to | |
** ensure that statement-transactions are always nested, not overlapping. | |
** If the open statement-transaction is not closed here, then the user | |
** may step another VM that opens its own statement transaction. This | |
** may lead to overlapping statement transactions. | |
** | |
** The statement transaction is never a top-level transaction. Hence | |
** the RELEASE call below can never fail. | |
*/ | |
assert( p->iStatement==0 || db->flags&SQLITE_CountRows ); | |
rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE); | |
assert( rc==SQLITE_OK ); | |
/* Invalidate all ephemeral cursor row caches */ | |
p->cacheCtr = (p->cacheCtr + 2)|1; | |
/* Make sure the results of the current row are \000 terminated | |
** and have an assigned type. The results are de-ephemeralized as | |
** a side effect. | |
*/ | |
pMem = p->pResultSet = &aMem[pOp->p1]; | |
for(i=0; i<pOp->p2; i++){ | |
assert( memIsValid(&pMem[i]) ); | |
Deephemeralize(&pMem[i]); | |
assert( (pMem[i].flags & MEM_Ephem)==0 | |
|| (pMem[i].flags & (MEM_Str|MEM_Blob))==0 ); | |
sqlite3VdbeMemNulTerminate(&pMem[i]); | |
REGISTER_TRACE(pOp->p1+i, &pMem[i]); | |
#ifdef SQLITE_DEBUG | |
/* The registers in the result will not be used again when the | |
** prepared statement restarts. This is because sqlite3_column() | |
** APIs might have caused type conversions of made other changes to | |
** the register values. Therefore, we can go ahead and break any | |
** OP_SCopy dependencies. */ | |
pMem[i].pScopyFrom = 0; | |
#endif | |
} | |
if( db->mallocFailed ) goto no_mem; | |
if( db->mTrace & SQLITE_TRACE_ROW ){ | |
db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0); | |
} | |
/* Return SQLITE_ROW | |
*/ | |
p->pc = (int)(pOp - aOp) + 1; | |
rc = SQLITE_ROW; | |
goto vdbe_return; | |
} | |
/* Opcode: Concat P1 P2 P3 * * | |
** Synopsis: r[P3]=r[P2]+r[P1] | |
** | |
** Add the text in register P1 onto the end of the text in | |
** register P2 and store the result in register P3. | |
** If either the P1 or P2 text are NULL then store NULL in P3. | |
** | |
** P3 = P2 || P1 | |
** | |
** It is illegal for P1 and P3 to be the same register. Sometimes, | |
** if P3 is the same register as P2, the implementation is able | |
** to avoid a memcpy(). | |
*/ | |
case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ | |
i64 nByte; /* Total size of the output string or blob */ | |
u16 flags1; /* Initial flags for P1 */ | |
u16 flags2; /* Initial flags for P2 */ | |
pIn1 = &aMem[pOp->p1]; | |
pIn2 = &aMem[pOp->p2]; | |
pOut = &aMem[pOp->p3]; | |
testcase( pOut==pIn2 ); | |
assert( pIn1!=pOut ); | |
flags1 = pIn1->flags; | |
testcase( flags1 & MEM_Null ); | |
testcase( pIn2->flags & MEM_Null ); | |
if( (flags1 | pIn2->flags) & MEM_Null ){ | |
sqlite3VdbeMemSetNull(pOut); | |
break; | |
} | |
if( (flags1 & (MEM_Str|MEM_Blob))==0 ){ | |
if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem; | |
flags1 = pIn1->flags & ~MEM_Str; | |
}else if( (flags1 & MEM_Zero)!=0 ){ | |
if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem; | |
flags1 = pIn1->flags & ~MEM_Str; | |
} | |
flags2 = pIn2->flags; | |
if( (flags2 & (MEM_Str|MEM_Blob))==0 ){ | |
if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem; | |
flags2 = pIn2->flags & ~MEM_Str; | |
}else if( (flags2 & MEM_Zero)!=0 ){ | |
if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem; | |
flags2 = pIn2->flags & ~MEM_Str; | |
} | |
nByte = pIn1->n + pIn2->n; | |
if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ | |
goto too_big; | |
} | |
if( sqlite3VdbeMemGrow(pOut, (int)nByte+3, pOut==pIn2) ){ | |
goto no_mem; | |
} | |
MemSetTypeFlag(pOut, MEM_Str); | |
if( pOut!=pIn2 ){ | |
memcpy(pOut->z, pIn2->z, pIn2->n); | |
assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) ); | |
pIn2->flags = flags2; | |
} | |
memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); | |
assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); | |
pIn1->flags = flags1; | |
pOut->z[nByte]=0; | |
pOut->z[nByte+1] = 0; | |
pOut->z[nByte+2] = 0; | |
pOut->flags |= MEM_Term; | |
pOut->n = (int)nByte; | |
pOut->enc = encoding; | |
UPDATE_MAX_BLOBSIZE(pOut); | |
break; | |
} | |
/* Opcode: Add P1 P2 P3 * * | |
** Synopsis: r[P3]=r[P1]+r[P2] | |
** | |
** Add the value in register P1 to the value in register P2 | |
** and store the result in register P3. | |
** If either input is NULL, the result is NULL. | |
*/ | |
/* Opcode: Multiply P1 P2 P3 * * | |
** Synopsis: r[P3]=r[P1]*r[P2] | |
** | |
** | |
** Multiply the value in register P1 by the value in register P2 | |
** and store the result in register P3. | |
** If either input is NULL, the result is NULL. | |
*/ | |
/* Opcode: Subtract P1 P2 P3 * * | |
** Synopsis: r[P3]=r[P2]-r[P1] | |
** | |
** Subtract the value in register P1 from the value in register P2 | |
** and store the result in register P3. | |
** If either input is NULL, the result is NULL. | |
*/ | |
/* Opcode: Divide P1 P2 P3 * * | |
** Synopsis: r[P3]=r[P2]/r[P1] | |
** | |
** Divide the value in register P1 by the value in register P2 | |
** and store the result in register P3 (P3=P2/P1). If the value in | |
** register P1 is zero, then the result is NULL. If either input is | |
** NULL, the result is NULL. | |
*/ | |
/* Opcode: Remainder P1 P2 P3 * * | |
** Synopsis: r[P3]=r[P2]%r[P1] | |
** | |
** Compute the remainder after integer register P2 is divided by | |
** register P1 and store the result in register P3. | |
** If the value in register P1 is zero the result is NULL. | |
** If either operand is NULL, the result is NULL. | |
*/ | |
case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ | |
case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ | |
case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ | |
case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ | |
case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ | |
u16 flags; /* Combined MEM_* flags from both inputs */ | |
u16 type1; /* Numeric type of left operand */ | |
u16 type2; /* Numeric type of right operand */ | |
i64 iA; /* Integer value of left operand */ | |
i64 iB; /* Integer value of right operand */ | |
double rA; /* Real value of left operand */ | |
double rB; /* Real value of right operand */ | |
pIn1 = &aMem[pOp->p1]; | |
type1 = numericType(pIn1); | |
pIn2 = &aMem[pOp->p2]; | |
type2 = numericType(pIn2); | |
pOut = &aMem[pOp->p3]; | |
flags = pIn1->flags | pIn2->flags; | |
if( (type1 & type2 & MEM_Int)!=0 ){ | |
iA = pIn1->u.i; | |
iB = pIn2->u.i; | |
switch( pOp->opcode ){ | |
case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; | |
case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; | |
case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break; | |
case OP_Divide: { | |
if( iA==0 ) goto arithmetic_result_is_null; | |
if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math; | |
iB /= iA; | |
break; | |
} | |
default: { | |
if( iA==0 ) goto arithmetic_result_is_null; | |
if( iA==-1 ) iA = 1; | |
iB %= iA; | |
break; | |
} | |
} | |
pOut->u.i = iB; | |
MemSetTypeFlag(pOut, MEM_Int); | |
}else if( (flags & MEM_Null)!=0 ){ | |
goto arithmetic_result_is_null; | |
}else{ | |
fp_math: | |
rA = sqlite3VdbeRealValue(pIn1); | |
rB = sqlite3VdbeRealValue(pIn2); | |
switch( pOp->opcode ){ | |
case OP_Add: rB += rA; break; | |
case OP_Subtract: rB -= rA; break; | |
case OP_Multiply: rB *= rA; break; | |
case OP_Divide: { | |
/* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ | |
if( rA==(double)0 ) goto arithmetic_result_is_null; | |
rB /= rA; | |
break; | |
} | |
default: { | |
iA = sqlite3VdbeIntValue(pIn1); | |
iB = sqlite3VdbeIntValue(pIn2); | |
if( iA==0 ) goto arithmetic_result_is_null; | |
if( iA==-1 ) iA = 1; | |
rB = (double)(iB % iA); | |
break; | |
} | |
} | |
#ifdef SQLITE_OMIT_FLOATING_POINT | |
pOut->u.i = rB; | |
MemSetTypeFlag(pOut, MEM_Int); | |
#else | |
if( sqlite3IsNaN(rB) ){ | |
goto arithmetic_result_is_null; | |
} | |
pOut->u.r = rB; | |
MemSetTypeFlag(pOut, MEM_Real); | |
#endif | |
} | |
break; | |
arithmetic_result_is_null: | |
sqlite3VdbeMemSetNull(pOut); | |
break; | |
} | |
/* Opcode: CollSeq P1 * * P4 | |
** | |
** P4 is a pointer to a CollSeq object. If the next call to a user function | |
** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will | |
** be returned. This is used by the built-in min(), max() and nullif() | |
** functions. | |
** | |
** If P1 is not zero, then it is a register that a subsequent min() or | |
** max() aggregate will set to 1 if the current row is not the minimum or | |
** maximum. The P1 register is initialized to 0 by this instruction. | |
** | |
** The interface used by the implementation of the aforementioned functions | |
** to retrieve the collation sequence set by this opcode is not available | |
** publicly. Only built-in functions have access to this feature. | |
*/ | |
case OP_CollSeq: { | |
assert( pOp->p4type==P4_COLLSEQ ); | |
if( pOp->p1 ){ | |
sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); | |
} | |
break; | |
} | |
/* Opcode: BitAnd P1 P2 P3 * * | |
** Synopsis: r[P3]=r[P1]&r[P2] | |
** | |
** Take the bit-wise AND of the values in register P1 and P2 and | |
** store the result in register P3. | |
** If either input is NULL, the result is NULL. | |
*/ | |
/* Opcode: BitOr P1 P2 P3 * * | |
** Synopsis: r[P3]=r[P1]|r[P2] | |
** | |
** Take the bit-wise OR of the values in register P1 and P2 and | |
** store the result in register P3. | |
** If either input is NULL, the result is NULL. | |
*/ | |
/* Opcode: ShiftLeft P1 P2 P3 * * | |
** Synopsis: r[P3]=r[P2]<<r[P1] | |
** | |
** Shift the integer value in register P2 to the left by the | |
** number of bits specified by the integer in register P1. | |
** Store the result in register P3. | |
** If either input is NULL, the result is NULL. | |
*/ | |
/* Opcode: ShiftRight P1 P2 P3 * * | |
** Synopsis: r[P3]=r[P2]>>r[P1] | |
** | |
** Shift the integer value in register P2 to the right by the | |
** number of bits specified by the integer in register P1. | |
** Store the result in register P3. | |
** If either input is NULL, the result is NULL. | |
*/ | |
case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ | |
case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ | |
case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ | |
case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ | |
i64 iA; | |
u64 uA; | |
i64 iB; | |
u8 op; | |
pIn1 = &aMem[pOp->p1]; | |
pIn2 = &aMem[pOp->p2]; | |
pOut = &aMem[pOp->p3]; | |
if( (pIn1->flags | pIn2->flags) & MEM_Null ){ | |
sqlite3VdbeMemSetNull(pOut); | |
break; | |
} | |
iA = sqlite3VdbeIntValue(pIn2); | |
iB = sqlite3VdbeIntValue(pIn1); | |
op = pOp->opcode; | |
if( op==OP_BitAnd ){ | |
iA &= iB; | |
}else if( op==OP_BitOr ){ | |
iA |= iB; | |
}else if( iB!=0 ){ | |
assert( op==OP_ShiftRight || op==OP_ShiftLeft ); | |
/* If shifting by a negative amount, shift in the other direction */ | |
if( iB<0 ){ | |
assert( OP_ShiftRight==OP_ShiftLeft+1 ); | |
op = 2*OP_ShiftLeft + 1 - op; | |
iB = iB>(-64) ? -iB : 64; | |
} | |
if( iB>=64 ){ | |
iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1; | |
}else{ | |
memcpy(&uA, &iA, sizeof(uA)); | |
if( op==OP_ShiftLeft ){ | |
uA <<= iB; | |
}else{ | |
uA >>= iB; | |
/* Sign-extend on a right shift of a negative number */ | |
if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB); | |
} | |
memcpy(&iA, &uA, sizeof(iA)); | |
} | |
} | |
pOut->u.i = iA; | |
MemSetTypeFlag(pOut, MEM_Int); | |
break; | |
} | |
/* Opcode: AddImm P1 P2 * * * | |
** Synopsis: r[P1]=r[P1]+P2 | |
** | |
** Add the constant P2 to the value in register P1. | |
** The result is always an integer. | |
** | |
** To force any register to be an integer, just add 0. | |
*/ | |
case OP_AddImm: { /* in1 */ | |
pIn1 = &aMem[pOp->p1]; | |
memAboutToChange(p, pIn1); | |
sqlite3VdbeMemIntegerify(pIn1); | |
pIn1->u.i += pOp->p2; | |
break; | |
} | |
/* Opcode: MustBeInt P1 P2 * * * | |
** | |
** Force the value in register P1 to be an integer. If the value | |
** in P1 is not an integer and cannot be converted into an integer | |
** without data loss, then jump immediately to P2, or if P2==0 | |
** raise an SQLITE_MISMATCH exception. | |
*/ | |
case OP_MustBeInt: { /* jump, in1 */ | |
pIn1 = &aMem[pOp->p1]; | |
if( (pIn1->flags & MEM_Int)==0 ){ | |
applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); | |
if( (pIn1->flags & MEM_Int)==0 ){ | |
VdbeBranchTaken(1, 2); | |
if( pOp->p2==0 ){ | |
rc = SQLITE_MISMATCH; | |
goto abort_due_to_error; | |
}else{ | |
goto jump_to_p2; | |
} | |
} | |
} | |
VdbeBranchTaken(0, 2); | |
MemSetTypeFlag(pIn1, MEM_Int); | |
break; | |
} | |
#ifndef SQLITE_OMIT_FLOATING_POINT | |
/* Opcode: RealAffinity P1 * * * * | |
** | |
** If register P1 holds an integer convert it to a real value. | |
** | |
** This opcode is used when extracting information from a column that | |
** has REAL affinity. Such column values may still be stored as | |
** integers, for space efficiency, but after extraction we want them | |
** to have only a real value. | |
*/ | |
case OP_RealAffinity: { /* in1 */ | |
pIn1 = &aMem[pOp->p1]; | |
if( pIn1->flags & (MEM_Int|MEM_IntReal) ){ | |
testcase( pIn1->flags & MEM_Int ); | |
testcase( pIn1->flags & MEM_IntReal ); | |
sqlite3VdbeMemRealify(pIn1); | |
REGISTER_TRACE(pOp->p1, pIn1); | |
} | |
break; | |
} | |
#endif | |
#ifndef SQLITE_OMIT_CAST | |
/* Opcode: Cast P1 P2 * * * | |
** Synopsis: affinity(r[P1]) | |
** | |
** Force the value in register P1 to be the type defined by P2. | |
** | |
** <ul> | |
** <li> P2=='A' → BLOB | |
** <li> P2=='B' → TEXT | |
** <li> P2=='C' → NUMERIC | |
** <li> P2=='D' → INTEGER | |
** <li> P2=='E' → REAL | |
** </ul> | |
** | |
** A NULL value is not changed by this routine. It remains NULL. | |
*/ | |
case OP_Cast: { /* in1 */ | |
assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL ); | |
testcase( pOp->p2==SQLITE_AFF_TEXT ); | |
testcase( pOp->p2==SQLITE_AFF_BLOB ); | |
testcase( pOp->p2==SQLITE_AFF_NUMERIC ); | |
testcase( pOp->p2==SQLITE_AFF_INTEGER ); | |
testcase( pOp->p2==SQLITE_AFF_REAL ); | |
pIn1 = &aMem[pOp->p1]; | |
memAboutToChange(p, pIn1); | |
rc = ExpandBlob(pIn1); | |
if( rc ) goto abort_due_to_error; | |
rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); | |
if( rc ) goto abort_due_to_error; | |
UPDATE_MAX_BLOBSIZE(pIn1); | |
REGISTER_TRACE(pOp->p1, pIn1); | |
break; | |
} | |
#endif /* SQLITE_OMIT_CAST */ | |
/* Opcode: Eq P1 P2 P3 P4 P5 | |
** Synopsis: IF r[P3]==r[P1] | |
** | |
** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then | |
** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then | |
** store the result of comparison in register P2. | |
** | |
** The SQLITE_AFF_MASK portion of P5 must be an affinity character - | |
** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made | |
** to coerce both inputs according to this affinity before the | |
** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric | |
** affinity is used. Note that the affinity conversions are stored | |
** back into the input registers P1 and P3. So this opcode can cause | |
** persistent changes to registers P1 and P3. | |
** | |
** Once any conversions have taken place, and neither value is NULL, | |
** the values are compared. If both values are blobs then memcmp() is | |
** used to determine the results of the comparison. If both values | |
** are text, then the appropriate collating function specified in | |
** P4 is used to do the comparison. If P4 is not specified then | |
** memcmp() is used to compare text string. If both values are | |
** numeric, then a numeric comparison is used. If the two values | |
** are of different types, then numbers are considered less than | |
** strings and strings are considered less than blobs. | |
** | |
** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either | |
** true or false and is never NULL. If both operands are NULL then the result | |
** of comparison is true. If either operand is NULL then the result is false. | |
** If neither operand is NULL the result is the same as it would be if | |
** the SQLITE_NULLEQ flag were omitted from P5. | |
** | |
** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the | |
** content of r[P2] is only changed if the new value is NULL or 0 (false). | |
** In other words, a prior r[P2] value will not be overwritten by 1 (true). | |
*/ | |
/* Opcode: Ne P1 P2 P3 P4 P5 | |
** Synopsis: IF r[P3]!=r[P1] | |
** | |
** This works just like the Eq opcode except that the jump is taken if | |
** the operands in registers P1 and P3 are not equal. See the Eq opcode for | |
** additional information. | |
** | |
** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the | |
** content of r[P2] is only changed if the new value is NULL or 1 (true). | |
** In other words, a prior r[P2] value will not be overwritten by 0 (false). | |
*/ | |
/* Opcode: Lt P1 P2 P3 P4 P5 | |
** Synopsis: IF r[P3]<r[P1] | |
** | |
** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then | |
** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store | |
** the result of comparison (0 or 1 or NULL) into register P2. | |
** | |
** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or | |
** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL | |
** bit is clear then fall through if either operand is NULL. | |
** | |
** The SQLITE_AFF_MASK portion of P5 must be an affinity character - | |
** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made | |
** to coerce both inputs according to this affinity before the | |
** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric | |
** affinity is used. Note that the affinity conversions are stored | |
** back into the input registers P1 and P3. So this opcode can cause | |
** persistent changes to registers P1 and P3. | |
** | |
** Once any conversions have taken place, and neither value is NULL, | |
** the values are compared. If both values are blobs then memcmp() is | |
** used to determine the results of the comparison. If both values | |
** are text, then the appropriate collating function specified in | |
** P4 is used to do the comparison. If P4 is not specified then | |
** memcmp() is used to compare text string. If both values are | |
** numeric, then a numeric comparison is used. If the two values | |
** are of different types, then numbers are considered less than | |
** strings and strings are considered less than blobs. | |
*/ | |
/* Opcode: Le P1 P2 P3 P4 P5 | |
** Synopsis: IF r[P3]<=r[P1] | |
** | |
** This works just like the Lt opcode except that the jump is taken if | |
** the content of register P3 is less than or equal to the content of | |
** register P1. See the Lt opcode for additional information. | |
*/ | |
/* Opcode: Gt P1 P2 P3 P4 P5 | |
** Synopsis: IF r[P3]>r[P1] | |
** | |
** This works just like the Lt opcode except that the jump is taken if | |
** the content of register P3 is greater than the content of | |
** register P1. See the Lt opcode for additional information. | |
*/ | |
/* Opcode: Ge P1 P2 P3 P4 P5 | |
** Synopsis: IF r[P3]>=r[P1] | |
** | |
** This works just like the Lt opcode except that the jump is taken if | |
** the content of register P3 is greater than or equal to the content of | |
** register P1. See the Lt opcode for additional information. | |
*/ | |
case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ | |
case OP_Ne: /* same as TK_NE, jump, in1, in3 */ | |
case OP_Lt: /* same as TK_LT, jump, in1, in3 */ | |
case OP_Le: /* same as TK_LE, jump, in1, in3 */ | |
case OP_Gt: /* same as TK_GT, jump, in1, in3 */ | |
case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ | |
int res, res2; /* Result of the comparison of pIn1 against pIn3 */ | |
char affinity; /* Affinity to use for comparison */ | |
u16 flags1; /* Copy of initial value of pIn1->flags */ | |
u16 flags3; /* Copy of initial value of pIn3->flags */ | |
pIn1 = &aMem[pOp->p1]; | |
pIn3 = &aMem[pOp->p3]; | |
flags1 = pIn1->flags; | |
flags3 = pIn3->flags; | |
if( (flags1 | flags3)&MEM_Null ){ | |
/* One or both operands are NULL */ | |
if( pOp->p5 & SQLITE_NULLEQ ){ | |
/* If SQLITE_NULLEQ is set (which will only happen if the operator is | |
** OP_Eq or OP_Ne) then take the jump or not depending on whether | |
** or not both operands are null. | |
*/ | |
assert( (flags1 & MEM_Cleared)==0 ); | |
assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB ); | |
testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 ); | |
if( (flags1&flags3&MEM_Null)!=0 | |
&& (flags3&MEM_Cleared)==0 | |
){ | |
res = 0; /* Operands are equal */ | |
}else{ | |
res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */ | |
} | |
}else{ | |
/* SQLITE_NULLEQ is clear and at least one operand is NULL, | |
** then the result is always NULL. | |
** The jump is taken if the SQLITE_JUMPIFNULL bit is set. | |
*/ | |
if( pOp->p5 & SQLITE_STOREP2 ){ | |
pOut = &aMem[pOp->p2]; | |
iCompare = 1; /* Operands are not equal */ | |
memAboutToChange(p, pOut); | |
MemSetTypeFlag(pOut, MEM_Null); | |
REGISTER_TRACE(pOp->p2, pOut); | |
}else{ | |
VdbeBranchTaken(2,3); | |
if( pOp->p5 & SQLITE_JUMPIFNULL ){ | |
goto jump_to_p2; | |
} | |
} | |
break; | |
} | |
}else{ | |
/* Neither operand is NULL. Do a comparison. */ | |
affinity = pOp->p5 & SQLITE_AFF_MASK; | |
if( affinity>=SQLITE_AFF_NUMERIC ){ | |
if( (flags1 | flags3)&MEM_Str ){ | |
if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ | |
applyNumericAffinity(pIn1,0); | |
testcase( flags3==pIn3->flags ); | |
flags3 = pIn3->flags; | |
} | |
if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ | |
applyNumericAffinity(pIn3,0); | |
} | |
} | |
/* Handle the common case of integer comparison here, as an | |
** optimization, to avoid a call to sqlite3MemCompare() */ | |
if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){ | |
if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; } | |
if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; } | |
res = 0; | |
goto compare_op; | |
} | |
}else if( affinity==SQLITE_AFF_TEXT ){ | |
if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ | |
testcase( pIn1->flags & MEM_Int ); | |
testcase( pIn1->flags & MEM_Real ); | |
testcase( pIn1->flags & MEM_IntReal ); | |
sqlite3VdbeMemStringify(pIn1, encoding, 1); | |
testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); | |
flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); | |
if( NEVER(pIn1==pIn3) ) flags3 = flags1 | MEM_Str; | |
} | |
if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ | |
testcase( pIn3->flags & MEM_Int ); | |
testcase( pIn3->flags & MEM_Real ); | |
testcase( pIn3->flags & MEM_IntReal ); | |
sqlite3VdbeMemStringify(pIn3, encoding, 1); | |
testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); | |
flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); | |
} | |
} | |
assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); | |
res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); | |
} | |
compare_op: | |
/* At this point, res is negative, zero, or positive if reg[P1] is | |
** less than, equal to, or greater than reg[P3], respectively. Compute | |
** the answer to this operator in res2, depending on what the comparison | |
** operator actually is. The next block of code depends on the fact | |
** that the 6 comparison operators are consecutive integers in this | |
** order: NE, EQ, GT, LE, LT, GE */ | |
assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 ); | |
assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 ); | |
if( res<0 ){ /* ne, eq, gt, le, lt, ge */ | |
static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 }; | |
res2 = aLTb[pOp->opcode - OP_Ne]; | |
}else if( res==0 ){ | |
static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 }; | |
res2 = aEQb[pOp->opcode - OP_Ne]; | |
}else{ | |
static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 }; | |
res2 = aGTb[pOp->opcode - OP_Ne]; | |
} | |
/* Undo any changes made by applyAffinity() to the input registers. */ | |
assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); | |
pIn3->flags = flags3; | |
assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); | |
pIn1->flags = flags1; | |
if( pOp->p5 & SQLITE_STOREP2 ){ | |
pOut = &aMem[pOp->p2]; | |
iCompare = res; | |
if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){ | |
/* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1 | |
** and prevents OP_Ne from overwriting NULL with 0. This flag | |
** is only used in contexts where either: | |
** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0) | |
** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1) | |
** Therefore it is not necessary to check the content of r[P2] for | |
** NULL. */ | |
assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq ); | |
assert( res2==0 || res2==1 ); | |
testcase( res2==0 && pOp->opcode==OP_Eq ); | |
testcase( res2==1 && pOp->opcode==OP_Eq ); | |
testcase( res2==0 && pOp->opcode==OP_Ne ); | |
testcase( res2==1 && pOp->opcode==OP_Ne ); | |
if( (pOp->opcode==OP_Eq)==res2 ) break; | |
} | |
memAboutToChange(p, pOut); | |
MemSetTypeFlag(pOut, MEM_Int); | |
pOut->u.i = res2; | |
REGISTER_TRACE(pOp->p2, pOut); | |
}else{ | |
VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); | |
if( res2 ){ | |
goto jump_to_p2; | |
} | |
} | |
break; | |
} | |
/* Opcode: ElseNotEq * P2 * * * | |
** | |
** This opcode must follow an OP_Lt or OP_Gt comparison operator. There | |
** can be zero or more OP_ReleaseReg opcodes intervening, but no other | |
** opcodes are allowed to occur between this instruction and the previous | |
** OP_Lt or OP_Gt. Furthermore, the prior OP_Lt or OP_Gt must have the | |
** SQLITE_STOREP2 bit set in the P5 field. | |
** | |
** If result of an OP_Eq comparison on the same two operands as the | |
** prior OP_Lt or OP_Gt would have been NULL or false (0), then then | |
** jump to P2. If the result of an OP_Eq comparison on the two previous | |
** operands would have been true (1), then fall through. | |
*/ | |
case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */ | |
#ifdef SQLITE_DEBUG | |
/* Verify the preconditions of this opcode - that it follows an OP_Lt or | |
** OP_Gt with the SQLITE_STOREP2 flag set, with zero or more intervening | |
** OP_ReleaseReg opcodes */ | |
int iAddr; | |
for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){ | |
if( aOp[iAddr].opcode==OP_ReleaseReg ) continue; | |
assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt ); | |
assert( aOp[iAddr].p5 & SQLITE_STOREP2 ); | |
break; | |
} | |
#endif /* SQLITE_DEBUG */ | |
VdbeBranchTaken(iCompare!=0, 2); | |
if( iCompare!=0 ) goto jump_to_p2; | |
break; | |
} | |
/* Opcode: Permutation * * * P4 * | |
** | |
** Set the permutation used by the OP_Compare operator in the next | |
** instruction. The permutation is stored in the P4 operand. | |
** | |
** The permutation is only valid until the next OP_Compare that has | |
** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should | |
** occur immediately prior to the OP_Compare. | |
** | |
** The first integer in the P4 integer array is the length of the array | |
** and does not become part of the permutation. | |
*/ | |
case OP_Permutation: { | |
assert( pOp->p4type==P4_INTARRAY ); | |
assert( pOp->p4.ai ); | |
assert( pOp[1].opcode==OP_Compare ); | |
assert( pOp[1].p5 & OPFLAG_PERMUTE ); | |
break; | |
} | |
/* Opcode: Compare P1 P2 P3 P4 P5 | |
** Synopsis: r[P1@P3] <-> r[P2@P3] | |
** | |
** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this | |
** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of | |
** the comparison for use by the next OP_Jump instruct. | |
** | |
** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is | |
** determined by the most recent OP_Permutation operator. If the | |
** OPFLAG_PERMUTE bit is clear, then register are compared in sequential | |
** order. | |
** | |
** P4 is a KeyInfo structure that defines collating sequences and sort | |
** orders for the comparison. The permutation applies to registers | |
** only. The KeyInfo elements are used sequentially. | |
** | |
** The comparison is a sort comparison, so NULLs compare equal, | |
** NULLs are less than numbers, numbers are less than strings, | |
** and strings are less than blobs. | |
*/ | |
case OP_Compare: { | |
int n; | |
int i; | |
int p1; | |
int p2; | |
const KeyInfo *pKeyInfo; | |
u32 idx; | |
CollSeq *pColl; /* Collating sequence to use on this term */ | |
int bRev; /* True for DESCENDING sort order */ | |
u32 *aPermute; /* The permutation */ | |
if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){ | |
aPermute = 0; | |
}else{ | |
assert( pOp>aOp ); | |
assert( pOp[-1].opcode==OP_Permutation ); | |
assert( pOp[-1].p4type==P4_INTARRAY ); | |
aPermute = pOp[-1].p4.ai + 1; | |
assert( aPermute!=0 ); | |
} | |
n = pOp->p3; | |
pKeyInfo = pOp->p4.pKeyInfo; | |
assert( n>0 ); | |
assert( pKeyInfo!=0 ); | |
p1 = pOp->p1; | |
p2 = pOp->p2; | |
#ifdef SQLITE_DEBUG | |
if( aPermute ){ | |
int k, mx = 0; | |
for(k=0; k<n; k++) if( aPermute[k]>(u32)mx ) mx = aPermute[k]; | |
assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 ); | |
assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 ); | |
}else{ | |
assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 ); | |
assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 ); | |
} | |
#endif /* SQLITE_DEBUG */ | |
for(i=0; i<n; i++){ | |
idx = aPermute ? aPermute[i] : (u32)i; | |
assert( memIsValid(&aMem[p1+idx]) ); | |
assert( memIsValid(&aMem[p2+idx]) ); | |
REGISTER_TRACE(p1+idx, &aMem[p1+idx]); | |
REGISTER_TRACE(p2+idx, &aMem[p2+idx]); | |
assert( i<pKeyInfo->nKeyField ); | |
pColl = pKeyInfo->aColl[i]; | |
bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC); | |
iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); | |
if( iCompare ){ | |
if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) | |
&& ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null)) | |
){ | |
iCompare = -iCompare; | |
} | |
if( bRev ) iCompare = -iCompare; | |
break; | |
} | |
} | |
break; | |
} | |
/* Opcode: Jump P1 P2 P3 * * | |
** | |
** Jump to the instruction at address P1, P2, or P3 depending on whether | |
** in the most recent OP_Compare instruction the P1 vector was less than | |
** equal to, or greater than the P2 vector, respectively. | |
*/ | |
case OP_Jump: { /* jump */ | |
if( iCompare<0 ){ | |
VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1]; | |
}else if( iCompare==0 ){ | |
VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1]; | |
}else{ | |
VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1]; | |
} | |
break; | |
} | |
/* Opcode: And P1 P2 P3 * * | |
** Synopsis: r[P3]=(r[P1] && r[P2]) | |
** | |
** Take the logical AND of the values in registers P1 and P2 and | |
** write the result into register P3. | |
** | |
** If either P1 or P2 is 0 (false) then the result is 0 even if | |
** the other input is NULL. A NULL and true or two NULLs give | |
** a NULL output. | |
*/ | |
/* Opcode: Or P1 P2 P3 * * | |
** Synopsis: r[P3]=(r[P1] || r[P2]) | |
** | |
** Take the logical OR of the values in register P1 and P2 and | |
** store the answer in register P3. | |
** | |
** If either P1 or P2 is nonzero (true) then the result is 1 (true) | |
** even if the other input is NULL. A NULL and false or two NULLs | |
** give a NULL output. | |
*/ | |
case OP_And: /* same as TK_AND, in1, in2, out3 */ | |
case OP_Or: { /* same as TK_OR, in1, in2, out3 */ | |
int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ | |
int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ | |
v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2); | |
v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2); | |
if( pOp->opcode==OP_And ){ | |
static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; | |
v1 = and_logic[v1*3+v2]; | |
}else{ | |
static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 }; | |
v1 = or_logic[v1*3+v2]; | |
} | |
pOut = &aMem[pOp->p3]; | |
if( v1==2 ){ | |
MemSetTypeFlag(pOut, MEM_Null); | |
}else{ | |
pOut->u.i = v1; | |
MemSetTypeFlag(pOut, MEM_Int); | |
} | |
break; | |
} | |
/* Opcode: IsTrue P1 P2 P3 P4 * | |
** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 | |
** | |
** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and | |
** IS NOT FALSE operators. | |
** | |
** Interpret the value in register P1 as a boolean value. Store that | |
** boolean (a 0 or 1) in register P2. Or if the value in register P1 is | |
** NULL, then the P3 is stored in register P2. Invert the answer if P4 | |
** is 1. | |
** | |
** The logic is summarized like this: | |
** | |
** <ul> | |
** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE | |
** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE | |
** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE | |
** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE | |
** </ul> | |
*/ | |
case OP_IsTrue: { /* in1, out2 */ | |
assert( pOp->p4type==P4_INT32 ); | |
assert( pOp->p4.i==0 || pOp->p4.i==1 ); | |
assert( pOp->p3==0 || pOp->p3==1 ); | |
sqlite3VdbeMemSetInt64(&aMem[pOp->p2], | |
sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i); | |
break; | |
} | |
/* Opcode: Not P1 P2 * * * | |
** Synopsis: r[P2]= !r[P1] | |
** | |
** Interpret the value in register P1 as a boolean value. Store the | |
** boolean complement in register P2. If the value in register P1 is | |
** NULL, then a NULL is stored in P2. | |
*/ | |
case OP_Not: { /* same as TK_NOT, in1, out2 */ | |
pIn1 = &aMem[pOp->p1]; | |
pOut = &aMem[pOp->p2]; | |
if( (pIn1->flags & MEM_Null)==0 ){ | |
sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0)); | |
}else{ | |
sqlite3VdbeMemSetNull(pOut); | |
} | |
break; | |
} | |
/* Opcode: BitNot P1 P2 * * * | |
** Synopsis: r[P2]= ~r[P1] | |
** | |
** Interpret the content of register P1 as an integer. Store the | |
** ones-complement of the P1 value into register P2. If P1 holds | |
** a NULL then store a NULL in P2. | |
*/ | |
case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ | |
pIn1 = &aMem[pOp->p1]; | |
pOut = &aMem[pOp->p2]; | |
sqlite3VdbeMemSetNull(pOut); | |
if( (pIn1->flags & MEM_Null)==0 ){ | |
pOut->flags = MEM_Int; | |
pOut->u.i = ~sqlite3VdbeIntValue(pIn1); | |
} | |
break; | |
} | |
/* Opcode: Once P1 P2 * * * | |
** | |
** Fall through to the next instruction the first time this opcode is | |
** encountered on each invocation of the byte-code program. Jump to P2 | |
** on the second and all subsequent encounters during the same invocation. | |
** | |
** Top-level programs determine first invocation by comparing the P1 | |
** operand against the P1 operand on the OP_Init opcode at the beginning | |
** of the program. If the P1 values differ, then fall through and make | |
** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are | |
** the same then take the jump. | |
** | |
** For subprograms, there is a bitmask in the VdbeFrame that determines | |
** whether or not the jump should be taken. The bitmask is necessary | |
** because the self-altering code trick does not work for recursive | |
** triggers. | |
*/ | |
case OP_Once: { /* jump */ | |
u32 iAddr; /* Address of this instruction */ | |
assert( p->aOp[0].opcode==OP_Init ); | |
if( p->pFrame ){ | |
iAddr = (int)(pOp - p->aOp); | |
if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){ | |
VdbeBranchTaken(1, 2); | |
goto jump_to_p2; | |
} | |
p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7); | |
}else{ | |
if( p->aOp[0].p1==pOp->p1 ){ | |
VdbeBranchTaken(1, 2); | |
goto jump_to_p2; | |
} | |
} | |
VdbeBranchTaken(0, 2); | |
pOp->p1 = p->aOp[0].p1; | |
break; | |
} | |
/* Opcode: If P1 P2 P3 * * | |
** | |
** Jump to P2 if the value in register P1 is true. The value | |
** is considered true if it is numeric and non-zero. If the value | |
** in P1 is NULL then take the jump if and only if P3 is non-zero. | |
*/ | |
case OP_If: { /* jump, in1 */ | |
int c; | |
c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3); | |
VdbeBranchTaken(c!=0, 2); | |
if( c ) goto jump_to_p2; | |
break; | |
} | |
/* Opcode: IfNot P1 P2 P3 * * | |
** | |
** Jump to P2 if the value in register P1 is False. The value | |
** is considered false if it has a numeric value of zero. If the value | |
** in P1 is NULL then take the jump if and only if P3 is non-zero. | |
*/ | |
case OP_IfNot: { /* jump, in1 */ | |
int c; | |
c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3); | |
VdbeBranchTaken(c!=0, 2); | |
if( c ) goto jump_to_p2; | |
break; | |
} | |
/* Opcode: IsNull P1 P2 * * * | |
** Synopsis: if r[P1]==NULL goto P2 | |
** | |
** Jump to P2 if the value in register P1 is NULL. | |
*/ | |
case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ | |
pIn1 = &aMem[pOp->p1]; | |
VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); | |
if( (pIn1->flags & MEM_Null)!=0 ){ | |
goto jump_to_p2; | |
} | |
break; | |
} | |
/* Opcode: NotNull P1 P2 * * * | |
** Synopsis: if r[P1]!=NULL goto P2 | |
** | |
** Jump to P2 if the value in register P1 is not NULL. | |
*/ | |
case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ | |
pIn1 = &aMem[pOp->p1]; | |
VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); | |
if( (pIn1->flags & MEM_Null)==0 ){ | |
goto jump_to_p2; | |
} | |
break; | |
} | |
/* Opcode: IfNullRow P1 P2 P3 * * | |
** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2 | |
** | |
** Check the cursor P1 to see if it is currently pointing at a NULL row. | |
** If it is, then set register P3 to NULL and jump immediately to P2. | |
** If P1 is not on a NULL row, then fall through without making any | |
** changes. | |
*/ | |
case OP_IfNullRow: { /* jump */ | |
assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
assert( p->apCsr[pOp->p1]!=0 ); | |
if( p->apCsr[pOp->p1]->nullRow ){ | |
sqlite3VdbeMemSetNull(aMem + pOp->p3); | |
goto jump_to_p2; | |
} | |
break; | |
} | |
#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC | |
/* Opcode: Offset P1 P2 P3 * * | |
** Synopsis: r[P3] = sqlite_offset(P1) | |
** | |
** Store in register r[P3] the byte offset into the database file that is the | |
** start of the payload for the record at which that cursor P1 is currently | |
** pointing. | |
** | |
** P2 is the column number for the argument to the sqlite_offset() function. | |
** This opcode does not use P2 itself, but the P2 value is used by the | |
** code generator. The P1, P2, and P3 operands to this opcode are the | |
** same as for OP_Column. | |
** | |
** This opcode is only available if SQLite is compiled with the | |
** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option. | |
*/ | |
case OP_Offset: { /* out3 */ | |
VdbeCursor *pC; /* The VDBE cursor */ | |
assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
pC = p->apCsr[pOp->p1]; | |
pOut = &p->aMem[pOp->p3]; | |
if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){ | |
sqlite3VdbeMemSetNull(pOut); | |
}else{ | |
sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor)); | |
} | |
break; | |
} | |
#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */ | |
/* Opcode: Column P1 P2 P3 P4 P5 | |
** Synopsis: r[P3]=PX | |
** | |
** Interpret the data that cursor P1 points to as a structure built using | |
** the MakeRecord instruction. (See the MakeRecord opcode for additional | |
** information about the format of the data.) Extract the P2-th column | |
** from this record. If there are less that (P2+1) | |
** values in the record, extract a NULL. | |
** | |
** The value extracted is stored in register P3. | |
** | |
** If the record contains fewer than P2 fields, then extract a NULL. Or, | |
** if the P4 argument is a P4_MEM use the value of the P4 argument as | |
** the result. | |
** | |
** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then | |
** the result is guaranteed to only be used as the argument of a length() | |
** or typeof() function, respectively. The loading of large blobs can be | |
** skipped for length() and all content loading can be skipped for typeof(). | |
*/ | |
case OP_Column: { | |
u32 p2; /* column number to retrieve */ | |
VdbeCursor *pC; /* The VDBE cursor */ | |
BtCursor *pCrsr; /* The BTree cursor */ | |
u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ | |
int len; /* The length of the serialized data for the column */ | |
int i; /* Loop counter */ | |
Mem *pDest; /* Where to write the extracted value */ | |
Mem sMem; /* For storing the record being decoded */ | |
const u8 *zData; /* Part of the record being decoded */ | |
const u8 *zHdr; /* Next unparsed byte of the header */ | |
const u8 *zEndHdr; /* Pointer to first byte after the header */ | |
u64 offset64; /* 64-bit offset */ | |
u32 t; /* A type code from the record header */ | |
Mem *pReg; /* PseudoTable input register */ | |
assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
pC = p->apCsr[pOp->p1]; | |
assert( pC!=0 ); | |
p2 = (u32)pOp->p2; | |
/* If the cursor cache is stale (meaning it is not currently point at | |
** the correct row) then bring it up-to-date by doing the necessary | |
** B-Tree seek. */ | |
rc = sqlite3VdbeCursorMoveto(&pC, &p2); | |
if( rc ) goto abort_due_to_error; | |
assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); | |
pDest = &aMem[pOp->p3]; | |
memAboutToChange(p, pDest); | |
assert( pC!=0 ); | |
assert( p2<(u32)pC->nField ); | |
aOffset = pC->aOffset; | |
assert( pC->eCurType!=CURTYPE_VTAB ); | |
assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); | |
assert( pC->eCurType!=CURTYPE_SORTER ); | |
if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/ | |
if( pC->nullRow ){ | |
if( pC->eCurType==CURTYPE_PSEUDO ){ | |
/* For the special case of as pseudo-cursor, the seekResult field | |
** identifies the register that holds the record */ | |
assert( pC->seekResult>0 ); | |
pReg = &aMem[pC->seekResult]; | |
assert( pReg->flags & MEM_Blob ); | |
assert( memIsValid(pReg) ); | |
pC->payloadSize = pC->szRow = pReg->n; | |
pC->aRow = (u8*)pReg->z; | |
}else{ | |
sqlite3VdbeMemSetNull(pDest); | |
goto op_column_out; | |
} | |
}else{ | |
pCrsr = pC->uc.pCursor; | |
assert( pC->eCurType==CURTYPE_BTREE ); | |
assert( pCrsr ); | |
assert( sqlite3BtreeCursorIsValid(pCrsr) ); | |
pC->payloadSize = sqlite3BtreePayloadSize(pCrsr); | |
pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow); | |
assert( pC->szRow<=pC->payloadSize ); | |
assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */ | |
if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ | |
goto too_big; | |
} | |
} | |
pC->cacheStatus = p->cacheCtr; | |
pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]); | |
pC->nHdrParsed = 0; | |
if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/ | |
/* pC->aRow does not have to hold the entire row, but it does at least | |
** need to cover the header of the record. If pC->aRow does not contain | |
** the complete header, then set it to zero, forcing the header to be | |
** dynamically allocated. */ | |
pC->aRow = 0; | |
pC->szRow = 0; | |
/* Make sure a corrupt database has not given us an oversize header. | |
** Do this now to avoid an oversize memory allocation. | |
** | |
** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte | |
** types use so much data space that there can only be 4096 and 32 of | |
** them, respectively. So the maximum header length results from a | |
** 3-byte type for each of the maximum of 32768 columns plus three | |
** extra bytes for the header length itself. 32768*3 + 3 = 98307. | |
*/ | |
if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){ | |
goto op_column_corrupt; | |
} | |
}else{ | |
/* This is an optimization. By skipping over the first few tests | |
** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a | |
** measurable performance gain. | |
** | |
** This branch is taken even if aOffset[0]==0. Such a record is never | |
** generated by SQLite, and could be considered corruption, but we | |
** accept it for historical reasons. When aOffset[0]==0, the code this | |
** branch jumps to reads past the end of the record, but never more | |
** than a few bytes. Even if the record occurs at the end of the page | |
** content area, the "page header" comes after the page content and so | |
** this overread is harmless. Similar overreads can occur for a corrupt | |
** database file. | |
*/ | |
zData = pC->aRow; | |
assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ | |
testcase( aOffset[0]==0 ); | |
goto op_column_read_header; | |
} | |
} | |
/* Make sure at least the first p2+1 entries of the header have been | |
** parsed and valid information is in aOffset[] and pC->aType[]. | |
*/ | |
if( pC->nHdrParsed<=p2 ){ | |
/* If there is more header available for parsing in the record, try | |
** to extract additional fields up through the p2+1-th field | |
*/ | |
if( pC->iHdrOffset<aOffset[0] ){ | |
/* Make sure zData points to enough of the record to cover the header. */ | |
if( pC->aRow==0 ){ | |
memset(&sMem, 0, sizeof(sMem)); | |
rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem); | |
if( rc!=SQLITE_OK ) goto abort_due_to_error; | |
zData = (u8*)sMem.z; | |
}else{ | |
zData = pC->aRow; | |
} | |
/* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */ | |
op_column_read_header: | |
i = pC->nHdrParsed; | |
offset64 = aOffset[i]; | |
zHdr = zData + pC->iHdrOffset; | |
zEndHdr = zData + aOffset[0]; | |
testcase( zHdr>=zEndHdr ); | |
do{ | |
if( (pC->aType[i] = t = zHdr[0])<0x80 ){ | |
zHdr++; | |
offset64 += sqlite3VdbeOneByteSerialTypeLen(t); | |
}else{ | |
zHdr += sqlite3GetVarint32(zHdr, &t); | |
pC->aType[i] = t; | |
offset64 += sqlite3VdbeSerialTypeLen(t); | |
} | |
aOffset[++i] = (u32)(offset64 & 0xffffffff); | |
}while( (u32)i<=p2 && zHdr<zEndHdr ); | |
/* The record is corrupt if any of the following are true: | |
** (1) the bytes of the header extend past the declared header size | |
** (2) the entire header was used but not all data was used | |
** (3) the end of the data extends beyond the end of the record. | |
*/ | |
if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize)) | |
|| (offset64 > pC->payloadSize) | |
){ | |
if( aOffset[0]==0 ){ | |
i = 0; | |
zHdr = zEndHdr; | |
}else{ | |
if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); | |
goto op_column_corrupt; | |
} | |
} | |
pC->nHdrParsed = i; | |
pC->iHdrOffset = (u32)(zHdr - zData); | |
if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); | |
}else{ | |
t = 0; | |
} | |
/* If after trying to extract new entries from the header, nHdrParsed is | |
** still not up to p2, that means that the record has fewer than p2 | |
** columns. So the result will be either the default value or a NULL. | |
*/ | |
if( pC->nHdrParsed<=p2 ){ | |
if( pOp->p4type==P4_MEM ){ | |
sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); | |
}else{ | |
sqlite3VdbeMemSetNull(pDest); | |
} | |
goto op_column_out; | |
} | |
}else{ | |
t = pC->aType[p2]; | |
} | |
/* Extract the content for the p2+1-th column. Control can only | |
** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are | |
** all valid. | |
*/ | |
assert( p2<pC->nHdrParsed ); | |
assert( rc==SQLITE_OK ); | |
assert( sqlite3VdbeCheckMemInvariants(pDest) ); | |
if( VdbeMemDynamic(pDest) ){ | |
sqlite3VdbeMemSetNull(pDest); | |
} | |
assert( t==pC->aType[p2] ); | |
if( pC->szRow>=aOffset[p2+1] ){ | |
/* This is the common case where the desired content fits on the original | |
** page - where the content is not on an overflow page */ | |
zData = pC->aRow + aOffset[p2]; | |
if( t<12 ){ | |
sqlite3VdbeSerialGet(zData, t, pDest); | |
}else{ | |
/* If the column value is a string, we need a persistent value, not | |
** a MEM_Ephem value. This branch is a fast short-cut that is equivalent | |
** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize(). | |
*/ | |
static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term }; | |
pDest->n = len = (t-12)/2; | |
pDest->enc = encoding; | |
if( pDest->szMalloc < len+2 ){ | |
pDest->flags = MEM_Null; | |
if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem; | |
}else{ | |
pDest->z = pDest->zMalloc; | |
} | |
memcpy(pDest->z, zData, len); | |
pDest->z[len] = 0; | |
pDest->z[len+1] = 0; | |
pDest->flags = aFlag[t&1]; | |
} | |
}else{ | |
pDest->enc = encoding; | |
/* This branch happens only when content is on overflow pages */ | |
if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0 | |
&& ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)) | |
|| (len = sqlite3VdbeSerialTypeLen(t))==0 | |
){ | |
/* Content is irrelevant for | |
** 1. the typeof() function, | |
** 2. the length(X) function if X is a blob, and | |
** 3. if the content length is zero. | |
** So we might as well use bogus content rather than reading | |
** content from disk. | |
** | |
** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the | |
** buffer passed to it, debugging function VdbeMemPrettyPrint() may | |
** read more. Use the global constant sqlite3CtypeMap[] as the array, | |
** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint()) | |
** and it begins with a bunch of zeros. | |
*/ | |
sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest); | |
}else{ | |
rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest); | |
if( rc!=SQLITE_OK ) goto abort_due_to_error; | |
sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); | |
pDest->flags &= ~MEM_Ephem; | |
} | |
} | |
op_column_out: | |
UPDATE_MAX_BLOBSIZE(pDest); | |
REGISTER_TRACE(pOp->p3, pDest); | |
break; | |
op_column_corrupt: | |
if( aOp[0].p3>0 ){ | |
pOp = &aOp[aOp[0].p3-1]; | |
break; | |
}else{ | |
rc = SQLITE_CORRUPT_BKPT; | |
goto abort_due_to_error; | |
} | |
} | |
/* Opcode: Affinity P1 P2 * P4 * | |
** Synopsis: affinity(r[P1@P2]) | |
** | |
** Apply affinities to a range of P2 registers starting with P1. | |
** | |
** P4 is a string that is P2 characters long. The N-th character of the | |
** string indicates the column affinity that should be used for the N-th | |
** memory cell in the range. | |
*/ | |
case OP_Affinity: { | |
const char *zAffinity; /* The affinity to be applied */ | |
zAffinity = pOp->p4.z; | |
assert( zAffinity!=0 ); | |
assert( pOp->p2>0 ); | |
assert( zAffinity[pOp->p2]==0 ); | |
pIn1 = &aMem[pOp->p1]; | |
while( 1 /*exit-by-break*/ ){ | |
assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); | |
assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) ); | |
applyAffinity(pIn1, zAffinity[0], encoding); | |
if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){ | |
/* When applying REAL affinity, if the result is still an MEM_Int | |
** that will fit in 6 bytes, then change the type to MEM_IntReal | |
** so that we keep the high-resolution integer value but know that | |
** the type really wants to be REAL. */ | |
testcase( pIn1->u.i==140737488355328LL ); | |
testcase( pIn1->u.i==140737488355327LL ); | |
testcase( pIn1->u.i==-140737488355328LL ); | |
testcase( pIn1->u.i==-140737488355329LL ); | |
if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){ | |
pIn1->flags |= MEM_IntReal; | |
pIn1->flags &= ~MEM_Int; | |
}else{ | |
pIn1->u.r = (double)pIn1->u.i; | |
pIn1->flags |= MEM_Real; | |
pIn1->flags &= ~MEM_Int; | |
} | |
} | |
REGISTER_TRACE((int)(pIn1-aMem), pIn1); | |
zAffinity++; | |
if( zAffinity[0]==0 ) break; | |
pIn1++; | |
} | |
break; | |
} | |
/* Opcode: MakeRecord P1 P2 P3 P4 * | |
** Synopsis: r[P3]=mkrec(r[P1@P2]) | |
** | |
** Convert P2 registers beginning with P1 into the [record format] | |
** use as a data record in a database table or as a key | |
** in an index. The OP_Column opcode can decode the record later. | |
** | |
** P4 may be a string that is P2 characters long. The N-th character of the | |
** string indicates the column affinity that should be used for the N-th | |
** field of the index key. | |
** | |
** The mapping from character to affinity is given by the SQLITE_AFF_ | |
** macros defined in sqliteInt.h. | |
** | |
** If P4 is NULL then all index fields have the affinity BLOB. | |
** | |
** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM | |
** compile-time option is enabled: | |
** | |
** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index | |
** of the right-most table that can be null-trimmed. | |
** | |
** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value | |
** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to | |
** accept no-change records with serial_type 10. This value is | |
** only used inside an assert() and does not affect the end result. | |
*/ | |
case OP_MakeRecord: { | |
Mem *pRec; /* The new record */ | |
u64 nData; /* Number of bytes of data space */ | |
int nHdr; /* Number of bytes of header space */ | |
i64 nByte; /* Data space required for this record */ | |
i64 nZero; /* Number of zero bytes at the end of the record */ | |
int nVarint; /* Number of bytes in a varint */ | |
u32 serial_type; /* Type field */ | |
Mem *pData0; /* First field to be combined into the record */ | |
Mem *pLast; /* Last field of the record */ | |
int nField; /* Number of fields in the record */ | |
char *zAffinity; /* The affinity string for the record */ | |
int file_format; /* File format to use for encoding */ | |
u32 len; /* Length of a field */ | |
u8 *zHdr; /* Where to write next byte of the header */ | |
u8 *zPayload; /* Where to write next byte of the payload */ | |
/* Assuming the record contains N fields, the record format looks | |
** like this: | |
** | |
** ------------------------------------------------------------------------ | |
** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | | |
** ------------------------------------------------------------------------ | |
** | |
** Data(0) is taken from register P1. Data(1) comes from register P1+1 | |
** and so forth. | |
** | |
** Each type field is a varint representing the serial type of the | |
** corresponding data element (see sqlite3VdbeSerialType()). The | |
** hdr-size field is also a varint which is the offset from the beginning | |
** of the record to data0. | |
*/ | |
nData = 0; /* Number of bytes of data space */ | |
nHdr = 0; /* Number of bytes of header space */ | |
nZero = 0; /* Number of zero bytes at the end of the record */ | |
nField = pOp->p1; | |
zAffinity = pOp->p4.z; | |
assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 ); | |
pData0 = &aMem[nField]; | |
nField = pOp->p2; | |
pLast = &pData0[nField-1]; | |
file_format = p->minWriteFileFormat; | |
/* Identify the output register */ | |
assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); | |
pOut = &aMem[pOp->p3]; | |
memAboutToChange(p, pOut); | |
/* Apply the requested affinity to all inputs | |
*/ | |
assert( pData0<=pLast ); | |
if( zAffinity ){ | |
pRec = pData0; | |
do{ | |
applyAffinity(pRec, zAffinity[0], encoding); | |
if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){ | |
pRec->flags |= MEM_IntReal; | |
pRec->flags &= ~(MEM_Int); | |
} | |
REGISTER_TRACE((int)(pRec-aMem), pRec); | |
zAffinity++; | |
pRec++; | |
assert( zAffinity[0]==0 || pRec<=pLast ); | |
}while( zAffinity[0] ); | |
} | |
#ifdef SQLITE_ENABLE_NULL_TRIM | |
/* NULLs can be safely trimmed from the end of the record, as long as | |
** as the schema format is 2 or more and none of the omitted columns | |
** have a non-NULL default value. Also, the record must be left with | |
** at least one field. If P5>0 then it will be one more than the | |
** index of the right-most column with a non-NULL default value */ | |
if( pOp->p5 ){ | |
while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){ | |
pLast--; | |
nField--; | |
} | |
} | |
#endif | |
/* Loop through the elements that will make up the record to figure | |
** out how much space is required for the new record. After this loop, | |
** the Mem.uTemp field of each term should hold the serial-type that will | |
** be used for that term in the generated record: | |
** | |
** Mem.uTemp value type | |
** --------------- --------------- | |
** 0 NULL | |
** 1 1-byte signed integer | |
** 2 2-byte signed integer | |
** 3 3-byte signed integer | |
** 4 4-byte signed integer | |
** 5 6-byte signed integer | |
** 6 8-byte signed integer | |
** 7 IEEE float | |
** 8 Integer constant 0 | |
** 9 Integer constant 1 | |
** 10,11 reserved for expansion | |
** N>=12 and even BLOB | |
** N>=13 and odd text | |
** | |
** The following additional values are computed: | |
** nHdr Number of bytes needed for the record header | |
** nData Number of bytes of data space needed for the record | |
** nZero Zero bytes at the end of the record | |
*/ | |
pRec = pLast; | |
do{ | |
assert( memIsValid(pRec) ); | |
if( pRec->flags & MEM_Null ){ | |
if( pRec->flags & MEM_Zero ){ | |
/* Values with MEM_Null and MEM_Zero are created by xColumn virtual | |
** table methods that never invoke sqlite3_result_xxxxx() while | |
** computing an unchanging column value in an UPDATE statement. | |
** Give such values a special internal-use-only serial-type of 10 | |
** so that they can be passed through to xUpdate and have | |
** a true sqlite3_value_nochange(). */ | |
#ifndef SQLITE_ENABLE_NULL_TRIM | |
assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB ); | |
#endif | |
pRec->uTemp = 10; | |
}else{ | |
pRec->uTemp = 0; | |
} | |
nHdr++; | |
}else if( pRec->flags & (MEM_Int|MEM_IntReal) ){ | |
/* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ | |
i64 i = pRec->u.i; | |
u64 uu; | |
testcase( pRec->flags & MEM_Int ); | |
testcase( pRec->flags & MEM_IntReal ); | |
if( i<0 ){ | |
uu = ~i; | |
}else{ | |
uu = i; | |
} | |
nHdr++; | |
testcase( uu==127 ); testcase( uu==128 ); | |
testcase( uu==32767 ); testcase( uu==32768 ); | |
testcase( uu==8388607 ); testcase( uu==8388608 ); | |
testcase( uu==2147483647 ); testcase( uu==2147483648 ); | |
testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL ); | |
if( uu<=127 ){ | |
if( (i&1)==i && file_format>=4 ){ | |
pRec->uTemp = 8+(u32)uu; | |
}else{ | |
nData++; | |
pRec->uTemp = 1; | |
} | |
}else if( uu<=32767 ){ | |
nData += 2; | |
pRec->uTemp = 2; | |
}else if( uu<=8388607 ){ | |
nData += 3; | |
pRec->uTemp = 3; | |
}else if( uu<=2147483647 ){ | |
nData += 4; | |
pRec->uTemp = 4; | |
}else if( uu<=140737488355327LL ){ | |
nData += 6; | |
pRec->uTemp = 5; | |
}else{ | |
nData += 8; | |
if( pRec->flags & MEM_IntReal ){ | |
/* If the value is IntReal and is going to take up 8 bytes to store | |
** as an integer, then we might as well make it an 8-byte floating | |
** point value */ | |
pRec->u.r = (double)pRec->u.i; | |
pRec->flags &= ~MEM_IntReal; | |
pRec->flags |= MEM_Real; | |
pRec->uTemp = 7; | |
}else{ | |
pRec->uTemp = 6; | |
} | |
} | |
}else if( pRec->flags & MEM_Real ){ | |
nHdr++; | |
nData += 8; | |
pRec->uTemp = 7; | |
}else{ | |
assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) ); | |
assert( pRec->n>=0 ); | |
len = (u32)pRec->n; | |
serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0); | |
if( pRec->flags & MEM_Zero ){ | |
serial_type += pRec->u.nZero*2; | |
if( nData ){ | |
if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; | |
len += pRec->u.nZero; | |
}else{ | |
nZero += pRec->u.nZero; | |
} | |
} | |
nData += len; | |
nHdr += sqlite3VarintLen(serial_type); | |
pRec->uTemp = serial_type; | |
} | |
if( pRec==pData0 ) break; | |
pRec--; | |
}while(1); | |
/* EVIDENCE-OF: R-22564-11647 The header begins with a single varint | |
** which determines the total number of bytes in the header. The varint | |
** value is the size of the header in bytes including the size varint | |
** itself. */ | |
testcase( nHdr==126 ); | |
testcase( nHdr==127 ); | |
if( nHdr<=126 ){ | |
/* The common case */ | |
nHdr += 1; | |
}else{ | |
/* Rare case of a really large header */ | |
nVarint = sqlite3VarintLen(nHdr); | |
nHdr += nVarint; | |
if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++; | |
} | |
nByte = nHdr+nData; | |
/* Make sure the output register has a buffer large enough to store | |
** the new record. The output register (pOp->p3) is not allowed to | |
** be one of the input registers (because the following call to | |
** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). | |
*/ | |
if( nByte+nZero<=pOut->szMalloc ){ | |
/* The output register is already large enough to hold the record. | |
** No error checks or buffer enlargement is required */ | |
pOut->z = pOut->zMalloc; | |
}else{ | |
/* Need to make sure that the output is not too big and then enlarge | |
** the output register to hold the full result */ | |
if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ | |
goto too_big; | |
} | |
if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ | |
goto no_mem; | |
} | |
} | |
pOut->n = (int)nByte; | |
pOut->flags = MEM_Blob; | |
if( nZero ){ | |
pOut->u.nZero = nZero; | |
pOut->flags |= MEM_Zero; | |
} | |
UPDATE_MAX_BLOBSIZE(pOut); | |
zHdr = (u8 *)pOut->z; | |
zPayload = zHdr + nHdr; | |
/* Write the record */ | |
zHdr += putVarint32(zHdr, nHdr); | |
assert( pData0<=pLast ); | |
pRec = pData0; | |
do{ | |
serial_type = pRec->uTemp; | |
/* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more | |
** additional varints, one per column. */ | |
zHdr += putVarint32(zHdr, serial_type); /* serial type */ | |
/* EVIDENCE-OF: R-64536-51728 The values for each column in the record | |
** immediately follow the header. */ | |
zPayload += sqlite3VdbeSerialPut(zPayload, pRec, serial_type); /* content */ | |
}while( (++pRec)<=pLast ); | |
assert( nHdr==(int)(zHdr - (u8*)pOut->z) ); | |
assert( nByte==(int)(zPayload - (u8*)pOut->z) ); | |
assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); | |
REGISTER_TRACE(pOp->p3, pOut); | |
break; | |
} | |
/* Opcode: Count P1 P2 p3 * * | |
** Synopsis: r[P2]=count() | |
** | |
** Store the number of entries (an integer value) in the table or index | |
** opened by cursor P1 in register P2. | |
** | |
** If P3==0, then an exact count is obtained, which involves visiting | |
** every btree page of the table. But if P3 is non-zero, an estimate | |
** is returned based on the current cursor position. | |
*/ | |
case OP_Count: { /* out2 */ | |
i64 nEntry; | |
BtCursor *pCrsr; | |
assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); | |
pCrsr = p->apCsr[pOp->p1]->uc.pCursor; | |
assert( pCrsr ); | |
if( pOp->p3 ){ | |
nEntry = sqlite3BtreeRowCountEst(pCrsr); | |
}else{ | |
nEntry = 0; /* Not needed. Only used to silence a warning. */ | |
rc = sqlite3BtreeCount(db, pCrsr, &nEntry); | |
if( rc ) goto abort_due_to_error; | |
} | |
pOut = out2Prerelease(p, pOp); | |
pOut->u.i = nEntry; | |
goto check_for_interrupt; | |
} | |
/* Opcode: Savepoint P1 * * P4 * | |
** | |
** Open, release or rollback the savepoint named by parameter P4, depending | |
** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN). | |
** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE). | |
** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK). | |
*/ | |
case OP_Savepoint: { | |
int p1; /* Value of P1 operand */ | |
char *zName; /* Name of savepoint */ | |
int nName; | |
Savepoint *pNew; | |
Savepoint *pSavepoint; | |
Savepoint *pTmp; | |
int iSavepoint; | |
int ii; | |
p1 = pOp->p1; | |
zName = pOp->p4.z; | |
/* Assert that the p1 parameter is valid. Also that if there is no open | |
** transaction, then there cannot be any savepoints. | |
*/ | |
assert( db->pSavepoint==0 || db->autoCommit==0 ); | |
assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); | |
assert( db->pSavepoint || db->isTransactionSavepoint==0 ); | |
assert( checkSavepointCount(db) ); | |
assert( p->bIsReader ); | |
if( p1==SAVEPOINT_BEGIN ){ | |
if( db->nVdbeWrite>0 ){ | |
/* A new savepoint cannot be created if there are active write | |
** statements (i.e. open read/write incremental blob handles). | |
*/ | |
sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress"); | |
rc = SQLITE_BUSY; | |
}else{ | |
nName = sqlite3Strlen30(zName); | |
#ifndef SQLITE_OMIT_VIRTUALTABLE | |
/* This call is Ok even if this savepoint is actually a transaction | |
** savepoint (and therefore should not prompt xSavepoint()) callbacks. | |
** If this is a transaction savepoint being opened, it is guaranteed | |
** that the db->aVTrans[] array is empty. */ | |
assert( db->autoCommit==0 || db->nVTrans==0 ); | |
rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, | |
db->nStatement+db->nSavepoint); | |
if( rc!=SQLITE_OK ) goto abort_due_to_error; | |
#endif | |
/* Create a new savepoint structure. */ | |
pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1); | |
if( pNew ){ | |
pNew->zName = (char *)&pNew[1]; | |
memcpy(pNew->zName, zName, nName+1); | |
/* If there is no open transaction, then mark this as a special | |
** "transaction savepoint". */ | |
if( db->autoCommit ){ | |
db->autoCommit = 0; | |
db->isTransactionSavepoint = 1; | |
}else{ | |
db->nSavepoint++; | |
} | |
/* Link the new savepoint into the database handle's list. */ | |
pNew->pNext = db->pSavepoint; | |
db->pSavepoint = pNew; | |
pNew->nDeferredCons = db->nDeferredCons; | |
pNew->nDeferredImmCons = db->nDeferredImmCons; | |
} | |
} | |
}else{ | |
assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK ); | |
iSavepoint = 0; | |
/* Find the named savepoint. If there is no such savepoint, then an | |
** an error is returned to the user. */ | |
for( | |
pSavepoint = db->pSavepoint; | |
pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); | |
pSavepoint = pSavepoint->pNext | |
){ | |
iSavepoint++; | |
} | |
if( !pSavepoint ){ | |
sqlite3VdbeError(p, "no such savepoint: %s", zName); | |
rc = SQLITE_ERROR; | |
}else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ | |
/* It is not possible to release (commit) a savepoint if there are | |
** active write statements. | |
*/ | |
sqlite3VdbeError(p, "cannot release savepoint - " | |
"SQL statements in progress"); | |
rc = SQLITE_BUSY; | |
}else{ | |
/* Determine whether or not this is a transaction savepoint. If so, | |
** and this is a RELEASE command, then the current transaction | |
** is committed. | |
*/ | |
int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; | |
if( isTransaction && p1==SAVEPOINT_RELEASE ){ | |
if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ | |
goto vdbe_return; | |
} | |
db->autoCommit = 1; | |
if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ | |
p->pc = (int)(pOp - aOp); | |
db->autoCommit = 0; | |
p->rc = rc = SQLITE_BUSY; | |
goto vdbe_return; | |
} | |
rc = p->rc; | |
if( rc ){ | |
db->autoCommit = 0; | |
}else{ | |
db->isTransactionSavepoint = 0; | |
} | |
}else{ | |
int isSchemaChange; | |
iSavepoint = db->nSavepoint - iSavepoint - 1; | |
if( p1==SAVEPOINT_ROLLBACK ){ | |
isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0; | |
for(ii=0; ii<db->nDb; ii++){ | |
rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, | |
SQLITE_ABORT_ROLLBACK, | |
isSchemaChange==0); | |
if( rc!=SQLITE_OK ) goto abort_due_to_error; | |
} | |
}else{ | |
assert( p1==SAVEPOINT_RELEASE ); | |
isSchemaChange = 0; | |
} | |
for(ii=0; ii<db->nDb; ii++){ | |
rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); | |
if( rc!=SQLITE_OK ){ | |
goto abort_due_to_error; | |
} | |
} | |
if( isSchemaChange ){ | |
sqlite3ExpirePreparedStatements(db, 0); | |
sqlite3ResetAllSchemasOfConnection(db); | |
db->mDbFlags |= DBFLAG_SchemaChange; | |
} | |
} | |
if( rc ) goto abort_due_to_error; | |
/* Regardless of whether this is a RELEASE or ROLLBACK, destroy all | |
** savepoints nested inside of the savepoint being operated on. */ | |
while( db->pSavepoint!=pSavepoint ){ | |
pTmp = db->pSavepoint; | |
db->pSavepoint = pTmp->pNext; | |
sqlite3DbFree(db, pTmp); | |
db->nSavepoint--; | |
} | |
/* If it is a RELEASE, then destroy the savepoint being operated on | |
** too. If it is a ROLLBACK TO, then set the number of deferred | |
** constraint violations present in the database to the value stored | |
** when the savepoint was created. */ | |
if( p1==SAVEPOINT_RELEASE ){ | |
assert( pSavepoint==db->pSavepoint ); | |
db->pSavepoint = pSavepoint->pNext; | |
sqlite3DbFree(db, pSavepoint); | |
if( !isTransaction ){ | |
db->nSavepoint--; | |
} | |
}else{ | |
assert( p1==SAVEPOINT_ROLLBACK ); | |
db->nDeferredCons = pSavepoint->nDeferredCons; | |
db->nDeferredImmCons = pSavepoint->nDeferredImmCons; | |
} | |
if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ | |
rc = sqlite3VtabSavepoint(db, p1, iSavepoint); | |
if( rc!=SQLITE_OK ) goto abort_due_to_error; | |
} | |
} | |
} | |
if( rc ) goto abort_due_to_error; | |
break; | |
} | |
/* Opcode: AutoCommit P1 P2 * * * | |
** | |
** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll | |
** back any currently active btree transactions. If there are any active | |
** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if | |
** there are active writing VMs or active VMs that use shared cache. | |
** | |
** This instruction causes the VM to halt. | |
*/ | |
case OP_AutoCommit: { | |
int desiredAutoCommit; | |
int iRollback; | |
desiredAutoCommit = pOp->p1; | |
iRollback = pOp->p2; | |
assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); | |
assert( desiredAutoCommit==1 || iRollback==0 ); | |
assert( db->nVdbeActive>0 ); /* At least this one VM is active */ | |
assert( p->bIsReader ); | |
if( desiredAutoCommit!=db->autoCommit ){ | |
if( iRollback ){ | |
assert( desiredAutoCommit==1 ); | |
sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); | |
db->autoCommit = 1; | |
}else if( desiredAutoCommit && db->nVdbeWrite>0 ){ | |
/* If this instruction implements a COMMIT and other VMs are writing | |
** return an error indicating that the other VMs must complete first. | |
*/ | |
sqlite3VdbeError(p, "cannot commit transaction - " | |
"SQL statements in progress"); | |
rc = SQLITE_BUSY; | |
goto abort_due_to_error; | |
}else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ | |
goto vdbe_return; | |
}else{ | |
db->autoCommit = (u8)desiredAutoCommit; | |
} | |
if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ | |
p->pc = (int)(pOp - aOp); | |
db->autoCommit = (u8)(1-desiredAutoCommit); | |
p->rc = rc = SQLITE_BUSY; | |
goto vdbe_return; | |
} | |
sqlite3CloseSavepoints(db); | |
if( p->rc==SQLITE_OK ){ | |
rc = SQLITE_DONE; | |
}else{ | |
rc = SQLITE_ERROR; | |
} | |
goto vdbe_return; | |
}else{ | |
sqlite3VdbeError(p, | |
(!desiredAutoCommit)?"cannot start a transaction within a transaction":( | |
(iRollback)?"cannot rollback - no transaction is active": | |
"cannot commit - no transaction is active")); | |
rc = SQLITE_ERROR; | |
goto abort_due_to_error; | |
} | |
/*NOTREACHED*/ assert(0); | |
} | |
/* Opcode: Transaction P1 P2 P3 P4 P5 | |
** | |
** Begin a transaction on database P1 if a transaction is not already | |
** active. | |
** If P2 is non-zero, then a write-transaction is started, or if a | |
** read-transaction is already active, it is upgraded to a write-transaction. | |
** If P2 is zero, then a read-transaction is started. If P2 is 2 or more | |
** then an exclusive transaction is started. | |
** | |
** P1 is the index of the database file on which the transaction is | |
** started. Index 0 is the main database file and index 1 is the | |
** file used for temporary tables. Indices of 2 or more are used for | |
** attached databases. | |
** | |
** If a write-transaction is started and the Vdbe.usesStmtJournal flag is | |
** true (this flag is set if the Vdbe may modify more than one row and may | |
** throw an ABORT exception), a statement transaction may also be opened. | |
** More specifically, a statement transaction is opened iff the database | |
** connection is currently not in autocommit mode, or if there are other | |
** active statements. A statement transaction allows the changes made by this | |
** VDBE to be rolled back after an error without having to roll back the | |
** entire transaction. If no error is encountered, the statement transaction | |
** will automatically commit when the VDBE halts. | |
** | |
** If P5!=0 then this opcode also checks the schema cookie against P3 | |
** and the schema generation counter against P4. | |
** The cookie changes its value whenever the database schema changes. | |
** This operation is used to detect when that the cookie has changed | |
** and that the current process needs to reread the schema. If the schema | |
** cookie in P3 differs from the schema cookie in the database header or | |
** if the schema generation counter in P4 differs from the current | |
** generation counter, then an SQLITE_SCHEMA error is raised and execution | |
** halts. The sqlite3_step() wrapper function might then reprepare the | |
** statement and rerun it from the beginning. | |
*/ | |
case OP_Transaction: { | |
Btree *pBt; | |
int iMeta = 0; | |
assert( p->bIsReader ); | |
assert( p->readOnly==0 || pOp->p2==0 ); | |
assert( pOp->p2>=0 && pOp->p2<=2 ); | |
assert( pOp->p1>=0 && pOp->p1<db->nDb ); | |
assert( DbMaskTest(p->btreeMask, pOp->p1) ); | |
if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){ | |
rc = SQLITE_READONLY; | |
goto abort_due_to_error; | |
} | |
pBt = db->aDb[pOp->p1].pBt; | |
if( pBt ){ | |
rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta); | |
testcase( rc==SQLITE_BUSY_SNAPSHOT ); | |
testcase( rc==SQLITE_BUSY_RECOVERY ); | |
if( rc!=SQLITE_OK ){ | |
if( (rc&0xff)==SQLITE_BUSY ){ | |
p->pc = (int)(pOp - aOp); | |
p->rc = rc; | |
goto vdbe_return; | |
} | |
goto abort_due_to_error; | |
} | |
if( p->usesStmtJournal | |
&& pOp->p2 | |
&& (db->autoCommit==0 || db->nVdbeRead>1) | |
){ | |
assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE ); | |
if( p->iStatement==0 ){ | |
assert( db->nStatement>=0 && db->nSavepoint>=0 ); | |
db->nStatement++; | |
p->iStatement = db->nSavepoint + db->nStatement; | |
} | |
rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); | |
if( rc==SQLITE_OK ){ | |
rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); | |
} | |
/* Store the current value of the database handles deferred constraint | |
** counter. If the statement transaction needs to be rolled back, | |
** the value of this counter needs to be restored too. */ | |
p->nStmtDefCons = db->nDeferredCons; | |
p->nStmtDefImmCons = db->nDeferredImmCons; | |
} | |
} | |
assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); | |
if( pOp->p5 | |
&& (iMeta!=pOp->p3 | |
|| db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i) | |
){ | |
/* | |
** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema | |
** version is checked to ensure that the schema has not changed since the | |
** SQL statement was prepared. | |
*/ | |
sqlite3DbFree(db, p->zErrMsg); | |
p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); | |
/* If the schema-cookie from the database file matches the cookie | |
** stored with the in-memory representation of the schema, do | |
** not reload the schema from the database file. | |
** | |
** If virtual-tables are in use, this is not just an optimization. | |
** Often, v-tables store their data in other SQLite tables, which | |
** are queried from within xNext() and other v-table methods using | |
** prepared queries. If such a query is out-of-date, we do not want to | |
** discard the database schema, as the user code implementing the | |
** v-table would have to be ready for the sqlite3_vtab structure itself | |
** to be invalidated whenever sqlite3_step() is called from within | |
** a v-table method. | |
*/ | |
if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ | |
sqlite3ResetOneSchema(db, pOp->p1); | |
} | |
p->expired = 1; | |
rc = SQLITE_SCHEMA; | |
} | |
if( rc ) goto abort_due_to_error; | |
break; | |
} | |
/* Opcode: ReadCookie P1 P2 P3 * * | |
** | |
** Read cookie number P3 from database P1 and write it into register P2. | |
** P3==1 is the schema version. P3==2 is the database format. | |
** P3==3 is the recommended pager cache size, and so forth. P1==0 is | |
** the main database file and P1==1 is the database file used to store | |
** temporary tables. | |
** | |
** There must be a read-lock on the database (either a transaction | |
** must be started or there must be an open cursor) before | |
** executing this instruction. | |
*/ | |
case OP_ReadCookie: { /* out2 */ | |
int iMeta; | |
int iDb; | |
int iCookie; | |
assert( p->bIsReader ); | |
iDb = pOp->p1; | |
iCookie = pOp->p3; | |
assert( pOp->p3<SQLITE_N_BTREE_META ); | |
assert( iDb>=0 && iDb<db->nDb ); | |
assert( db->aDb[iDb].pBt!=0 ); | |
assert( DbMaskTest(p->btreeMask, iDb) ); | |
sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); | |
pOut = out2Prerelease(p, pOp); | |
pOut->u.i = iMeta; | |
break; | |
} | |
/* Opcode: SetCookie P1 P2 P3 * P5 | |
** | |
** Write the integer value P3 into cookie number P2 of database P1. | |
** P2==1 is the schema version. P2==2 is the database format. | |
** P2==3 is the recommended pager cache | |
** size, and so forth. P1==0 is the main database file and P1==1 is the | |
** database file used to store temporary tables. | |
** | |
** A transaction must be started before executing this opcode. | |
** | |
** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal | |
** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement | |
** has P5 set to 1, so that the internal schema version will be different | |
** from the database schema version, resulting in a schema reset. | |
*/ | |
case OP_SetCookie: { | |
Db *pDb; | |
sqlite3VdbeIncrWriteCounter(p, 0); | |
assert( pOp->p2<SQLITE_N_BTREE_META ); | |
assert( pOp->p1>=0 && pOp->p1<db->nDb ); | |
assert( DbMaskTest(p->btreeMask, pOp->p1) ); | |
assert( p->readOnly==0 ); | |
pDb = &db->aDb[pOp->p1]; | |
assert( pDb->pBt!=0 ); | |
assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); | |
/* See note about index shifting on OP_ReadCookie */ | |
rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3); | |
if( pOp->p2==BTREE_SCHEMA_VERSION ){ | |
/* When the schema cookie changes, record the new cookie internally */ | |
pDb->pSchema->schema_cookie = pOp->p3 - pOp->p5; | |
db->mDbFlags |= DBFLAG_SchemaChange; | |
}else if( pOp->p2==BTREE_FILE_FORMAT ){ | |
/* Record changes in the file format */ | |
pDb->pSchema->file_format = pOp->p3; | |
} | |
if( pOp->p1==1 ){ | |
/* Invalidate all prepared statements whenever the TEMP database | |
** schema is changed. Ticket #1644 */ | |
sqlite3ExpirePreparedStatements(db, 0); | |
p->expired = 0; | |
} | |
if( rc ) goto abort_due_to_error; | |
break; | |
} | |
/* Opcode: OpenRead P1 P2 P3 P4 P5 | |
** Synopsis: root=P2 iDb=P3 | |
** | |
** Open a read-only cursor for the database table whose root page is | |
** P2 in a database file. The database file is determined by P3. | |
** P3==0 means the main database, P3==1 means the database used for | |
** temporary tables, and P3>1 means used the corresponding attached | |
** database. Give the new cursor an identifier of P1. The P1 | |
** values need not be contiguous but all P1 values should be small integers. | |
** It is an error for P1 to be negative. | |
** | |
** Allowed P5 bits: | |
** <ul> | |
** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for | |
** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT | |
** of OP_SeekLE/OP_IdxLT) | |
** </ul> | |
** | |
** The P4 value may be either an integer (P4_INT32) or a pointer to | |
** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo | |
** object, then table being opened must be an [index b-tree] where the | |
** KeyInfo object defines the content and collating | |
** sequence of that index b-tree. Otherwise, if P4 is an integer | |
** value, then the table being opened must be a [table b-tree] with a | |
** number of columns no less than the value of P4. | |
** | |
** See also: OpenWrite, ReopenIdx | |
*/ | |
/* Opcode: ReopenIdx P1 P2 P3 P4 P5 | |
** Synopsis: root=P2 iDb=P3 | |
** | |
** The ReopenIdx opcode works like OP_OpenRead except that it first |