Index: third_party/sqlite/amalgamation/sqlite3.04.c |
diff --git a/third_party/sqlite/amalgamation/sqlite3.04.c b/third_party/sqlite/amalgamation/sqlite3.04.c |
new file mode 100644 |
index 0000000000000000000000000000000000000000..25625116a71788757bbb7bcaa54dec09a9852271 |
--- /dev/null |
+++ b/third_party/sqlite/amalgamation/sqlite3.04.c |
@@ -0,0 +1,25704 @@ |
+/************** Begin file vdbe.c ********************************************/ |
+/* |
+** 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 |
+SQLITE_API 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 |
+SQLITE_API 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 |
+SQLITE_API 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 |
+SQLITE_API 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 |
+SQLITE_API 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 |
+ |
+/* |
+** 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 an integer, 2 or 3, that indices how many different ways the |
+** branch can go. It is usually 2. "I" is the direction the branch |
+** goes. 0 means falls through. 1 means branch is taken. 2 means the |
+** second alternative branch is taken. |
+** |
+** iSrcLine is the source code line (from the __LINE__ macro) that |
+** generated the VDBE instruction. This instrumentation assumes that all |
+** source code is in a single file (the amalgamation). Special values 1 |
+** and 2 for the iSrcLine parameter mean that this particular branch is |
+** always taken or never taken, respectively. |
+*/ |
+#if !defined(SQLITE_VDBE_COVERAGE) |
+# define VdbeBranchTaken(I,M) |
+#else |
+# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M) |
+ static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){ |
+ if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){ |
+ M = iSrcLine; |
+ /* Assert the truth of VdbeCoverageAlwaysTaken() and |
+ ** VdbeCoverageNeverTaken() */ |
+ assert( (M & I)==I ); |
+ }else{ |
+ if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/ |
+ sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, |
+ iSrcLine,I,M); |
+ } |
+ } |
+#endif |
+ |
+/* |
+** Convert the given register into a string if it isn't one |
+** already. Return non-zero if a malloc() fails. |
+*/ |
+#define Stringify(P, enc) \ |
+ if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \ |
+ { goto no_mem; } |
+ |
+/* |
+** 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*/ |
+ 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; |
+} |
+ |
+/* |
+** 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; |
+ i64 iValue; |
+ u8 enc = pRec->enc; |
+ assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str ); |
+ if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return; |
+ if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){ |
+ pRec->u.i = iValue; |
+ pRec->flags |= MEM_Int; |
+ }else{ |
+ pRec->u.r = rValue; |
+ pRec->flags |= MEM_Real; |
+ if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec); |
+ } |
+} |
+ |
+/* |
+** 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: |
+** 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)) ){ |
+ sqlite3VdbeMemStringify(pRec, enc, 1); |
+ } |
+ } |
+ pRec->flags &= ~(MEM_Real|MEM_Int); |
+ } |
+} |
+ |
+/* |
+** 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. |
+*/ |
+SQLITE_API 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. |
+*/ |
+SQLITE_PRIVATE 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){ |
+ assert( (pMem->flags & (MEM_Int|MEM_Real))==0 ); |
+ assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); |
+ if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){ |
+ return 0; |
+ } |
+ if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){ |
+ 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) ){ |
+ return pMem->flags & (MEM_Int|MEM_Real); |
+ } |
+ if( pMem->flags & (MEM_Str|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. |
+*/ |
+SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ |
+ char *zCsr = zBuf; |
+ 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_snprintf(100, zCsr, "%c", c); |
+ zCsr += sqlite3Strlen30(zCsr); |
+ sqlite3_snprintf(100, zCsr, "%d[", pMem->n); |
+ zCsr += sqlite3Strlen30(zCsr); |
+ for(i=0; i<16 && i<pMem->n; i++){ |
+ sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); |
+ zCsr += sqlite3Strlen30(zCsr); |
+ } |
+ for(i=0; i<16 && i<pMem->n; i++){ |
+ char z = pMem->z[i]; |
+ if( z<32 || z>126 ) *zCsr++ = '.'; |
+ else *zCsr++ = z; |
+ } |
+ |
+ sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]); |
+ zCsr += sqlite3Strlen30(zCsr); |
+ if( f & MEM_Zero ){ |
+ sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero); |
+ zCsr += sqlite3Strlen30(zCsr); |
+ } |
+ *zCsr = '\0'; |
+ }else if( f & MEM_Str ){ |
+ int j, k; |
+ zBuf[0] = ' '; |
+ if( f & MEM_Dyn ){ |
+ zBuf[1] = 'z'; |
+ assert( (f & (MEM_Static|MEM_Ephem))==0 ); |
+ }else if( f & MEM_Static ){ |
+ zBuf[1] = 't'; |
+ assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); |
+ }else if( f & MEM_Ephem ){ |
+ zBuf[1] = 'e'; |
+ assert( (f & (MEM_Static|MEM_Dyn))==0 ); |
+ }else{ |
+ zBuf[1] = 's'; |
+ } |
+ k = 2; |
+ sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); |
+ k += sqlite3Strlen30(&zBuf[k]); |
+ zBuf[k++] = '['; |
+ for(j=0; j<15 && j<pMem->n; j++){ |
+ u8 c = pMem->z[j]; |
+ if( c>=0x20 && c<0x7f ){ |
+ zBuf[k++] = c; |
+ }else{ |
+ zBuf[k++] = '.'; |
+ } |
+ } |
+ zBuf[k++] = ']'; |
+ sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); |
+ k += sqlite3Strlen30(&zBuf[k]); |
+ zBuf[k++] = 0; |
+ } |
+} |
+#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(" NULL"); |
+ }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
+ printf(" si:%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:%g", p->u.r); |
+#endif |
+ }else if( p->flags & MEM_RowSet ){ |
+ printf(" (rowset)"); |
+ }else{ |
+ char zBuf[200]; |
+ sqlite3VdbeMemPrettyPrint(p, zBuf); |
+ printf(" %s", zBuf); |
+ } |
+ if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype); |
+} |
+static void registerTrace(int iReg, Mem *p){ |
+ printf("REG[%d] = ", iReg); |
+ memTracePrint(p); |
+ printf("\n"); |
+} |
+#endif |
+ |
+#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 in the middle of vdbe.c *********************/ |
+/************** Begin file hwtime.h ******************************************/ |
+/* |
+** 2008 May 27 |
+** |
+** 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. |
+** |
+****************************************************************************** |
+** |
+** This file contains inline asm code for retrieving "high-performance" |
+** counters for x86 class CPUs. |
+*/ |
+#ifndef SQLITE_HWTIME_H |
+#define SQLITE_HWTIME_H |
+ |
+/* |
+** The following routine only works on pentium-class (or newer) processors. |
+** It uses the RDTSC opcode to read the cycle count value out of the |
+** processor and returns that value. This can be used for high-res |
+** profiling. |
+*/ |
+#if (defined(__GNUC__) || defined(_MSC_VER)) && \ |
+ (defined(i386) || defined(__i386__) || defined(_M_IX86)) |
+ |
+ #if defined(__GNUC__) |
+ |
+ __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
+ unsigned int lo, hi; |
+ __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); |
+ return (sqlite_uint64)hi << 32 | lo; |
+ } |
+ |
+ #elif defined(_MSC_VER) |
+ |
+ __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){ |
+ __asm { |
+ rdtsc |
+ ret ; return value at EDX:EAX |
+ } |
+ } |
+ |
+ #endif |
+ |
+#elif (defined(__GNUC__) && defined(__x86_64__)) |
+ |
+ __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
+ unsigned long val; |
+ __asm__ __volatile__ ("rdtsc" : "=A" (val)); |
+ return val; |
+ } |
+ |
+#elif (defined(__GNUC__) && defined(__ppc__)) |
+ |
+ __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
+ unsigned long long retval; |
+ unsigned long junk; |
+ __asm__ __volatile__ ("\n\ |
+ 1: mftbu %1\n\ |
+ mftb %L0\n\ |
+ mftbu %0\n\ |
+ cmpw %0,%1\n\ |
+ bne 1b" |
+ : "=r" (retval), "=r" (junk)); |
+ return retval; |
+ } |
+ |
+#else |
+ |
+ #error Need implementation of sqlite3Hwtime() for your platform. |
+ |
+ /* |
+ ** To compile without implementing sqlite3Hwtime() for your platform, |
+ ** you can remove the above #error and use the following |
+ ** stub function. You will lose timing support for many |
+ ** of the debugging and testing utilities, but it should at |
+ ** least compile and run. |
+ */ |
+SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); } |
+ |
+#endif |
+ |
+#endif /* !defined(SQLITE_HWTIME_H) */ |
+ |
+/************** End of hwtime.h **********************************************/ |
+/************** Continuing where we left off in vdbe.c ***********************/ |
+ |
+#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(). |
+*/ |
+SQLITE_PRIVATE 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 */ |
+ unsigned nVmStep = 0; /* Number of virtual machine steps */ |
+#ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
+ unsigned nProgressLimit = 0;/* 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); |
+ 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 ); |
+ assert( p->bIsReader || p->readOnly!=0 ); |
+ p->iCurrentTime = 0; |
+ assert( p->explain==0 ); |
+ p->pResultSet = 0; |
+ db->busyHandler.nBusy = 0; |
+ if( db->u1.isInterrupted ) goto abort_due_to_interrupt; |
+ sqlite3VdbeIOTraceSql(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); |
+ } |
+#endif |
+#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 = 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); |
+ } |
+#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 */ |
+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, OP_RowSetNext, 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( 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. |
+ */ |
+ if( db->xProgress!=0 && nVmStep>=nProgressLimit ){ |
+ assert( db->nProgressOps!=0 ); |
+ nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps); |
+ if( db->xProgress(db->pProgressArg) ){ |
+ 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]; |
+ if( (pIn3->flags & MEM_Null)==0 ) break; |
+ /* Fall through into OP_Halt */ |
+} |
+ |
+/* 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); |
+ 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->opcode = OP_String; |
+ 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( 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; |
+ } |
+ testcase( rc==SQLITE_TOOBIG ); |
+#endif |
+ if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
+ goto too_big; |
+ } |
+ assert( rc==SQLITE_OK ); |
+ /* Fall through to the next case, OP_String */ |
+} |
+ |
+/* 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; |
+ 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_Null)&~MEM_Undefined; |
+ 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]; |
+ sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); |
+ 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 |
+ if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){ |
+ pOut->pScopyFrom += pOp->p2 - p1; |
+ } |
+#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 ){ |
+ 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 |
+ if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1; |
+#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 ); |
+ |
+#ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
+ /* Run the progress counter just before returning. |
+ */ |
+ if( db->xProgress!=0 |
+ && nVmStep>=nProgressLimit |
+ && db->xProgress(db->pProgressArg)!=0 |
+ ){ |
+ rc = SQLITE_INTERRUPT; |
+ goto abort_due_to_error; |
+ } |
+#endif |
+ |
+ /* 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]); |
+ } |
+ if( db->mallocFailed ) goto no_mem; |
+ |
+ if( db->mTrace & SQLITE_TRACE_ROW ){ |
+ db->xTrace(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; |
+ |
+ pIn1 = &aMem[pOp->p1]; |
+ pIn2 = &aMem[pOp->p2]; |
+ pOut = &aMem[pOp->p3]; |
+ assert( pIn1!=pOut ); |
+ if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
+ sqlite3VdbeMemSetNull(pOut); |
+ break; |
+ } |
+ if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem; |
+ Stringify(pIn1, encoding); |
+ Stringify(pIn2, encoding); |
+ nByte = pIn1->n + pIn2->n; |
+ if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
+ goto too_big; |
+ } |
+ if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ |
+ goto no_mem; |
+ } |
+ MemSetTypeFlag(pOut, MEM_Str); |
+ if( pOut!=pIn2 ){ |
+ memcpy(pOut->z, pIn2->z, pIn2->n); |
+ } |
+ memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
+ pOut->z[nByte]=0; |
+ pOut->z[nByte+1] = 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 */ |
+ char bIntint; /* Started out as two integer operands */ |
+ 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( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null; |
+ if( (type1 & type2 & MEM_Int)!=0 ){ |
+ iA = pIn1->u.i; |
+ iB = pIn2->u.i; |
+ bIntint = 1; |
+ 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{ |
+ bIntint = 0; |
+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 = (i64)rA; |
+ iB = (i64)rB; |
+ 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); |
+ if( ((type1|type2)&MEM_Real)==0 && !bIntint ){ |
+ sqlite3VdbeIntegerAffinity(pOut); |
+ } |
+#endif |
+ } |
+ break; |
+ |
+arithmetic_result_is_null: |
+ sqlite3VdbeMemSetNull(pOut); |
+ break; |
+} |
+ |
+/* Opcode: CollSeq P1 * * P4 |
+** |
+** P4 is a pointer to a CollSeq struct. 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: Function0 P1 P2 P3 P4 P5 |
+** Synopsis: r[P3]=func(r[P2@P5]) |
+** |
+** Invoke a user function (P4 is a pointer to a FuncDef object that |
+** defines the function) with P5 arguments taken from register P2 and |
+** successors. The result of the function is stored in register P3. |
+** Register P3 must not be one of the function inputs. |
+** |
+** P1 is a 32-bit bitmask indicating whether or not each argument to the |
+** function was determined to be constant at compile time. If the first |
+** argument was constant then bit 0 of P1 is set. This is used to determine |
+** whether meta data associated with a user function argument using the |
+** sqlite3_set_auxdata() API may be safely retained until the next |
+** invocation of this opcode. |
+** |
+** See also: Function, AggStep, AggFinal |
+*/ |
+/* Opcode: Function P1 P2 P3 P4 P5 |
+** Synopsis: r[P3]=func(r[P2@P5]) |
+** |
+** Invoke a user function (P4 is a pointer to an sqlite3_context object that |
+** contains a pointer to the function to be run) with P5 arguments taken |
+** from register P2 and successors. The result of the function is stored |
+** in register P3. Register P3 must not be one of the function inputs. |
+** |
+** P1 is a 32-bit bitmask indicating whether or not each argument to the |
+** function was determined to be constant at compile time. If the first |
+** argument was constant then bit 0 of P1 is set. This is used to determine |
+** whether meta data associated with a user function argument using the |
+** sqlite3_set_auxdata() API may be safely retained until the next |
+** invocation of this opcode. |
+** |
+** SQL functions are initially coded as OP_Function0 with P4 pointing |
+** to a FuncDef object. But on first evaluation, the P4 operand is |
+** automatically converted into an sqlite3_context object and the operation |
+** changed to this OP_Function opcode. In this way, the initialization of |
+** the sqlite3_context object occurs only once, rather than once for each |
+** evaluation of the function. |
+** |
+** See also: Function0, AggStep, AggFinal |
+*/ |
+case OP_Function0: { |
+ int n; |
+ sqlite3_context *pCtx; |
+ |
+ assert( pOp->p4type==P4_FUNCDEF ); |
+ n = pOp->p5; |
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
+ assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) ); |
+ assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
+ pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); |
+ if( pCtx==0 ) goto no_mem; |
+ pCtx->pOut = 0; |
+ pCtx->pFunc = pOp->p4.pFunc; |
+ pCtx->iOp = (int)(pOp - aOp); |
+ pCtx->pVdbe = p; |
+ pCtx->argc = n; |
+ pOp->p4type = P4_FUNCCTX; |
+ pOp->p4.pCtx = pCtx; |
+ pOp->opcode = OP_Function; |
+ /* Fall through into OP_Function */ |
+} |
+case OP_Function: { |
+ int i; |
+ sqlite3_context *pCtx; |
+ |
+ assert( pOp->p4type==P4_FUNCCTX ); |
+ pCtx = pOp->p4.pCtx; |
+ |
+ /* If this function is inside of a trigger, the register array in aMem[] |
+ ** might change from one evaluation to the next. The next block of code |
+ ** checks to see if the register array has changed, and if so it |
+ ** reinitializes the relavant parts of the sqlite3_context object */ |
+ pOut = &aMem[pOp->p3]; |
+ if( pCtx->pOut != pOut ){ |
+ pCtx->pOut = pOut; |
+ for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
+ } |
+ |
+ memAboutToChange(p, pCtx->pOut); |
+#ifdef SQLITE_DEBUG |
+ for(i=0; i<pCtx->argc; i++){ |
+ assert( memIsValid(pCtx->argv[i]) ); |
+ REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
+ } |
+#endif |
+ MemSetTypeFlag(pCtx->pOut, MEM_Null); |
+ pCtx->fErrorOrAux = 0; |
+ (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ |
+ |
+ /* If the function returned an error, throw an exception */ |
+ if( pCtx->fErrorOrAux ){ |
+ if( pCtx->isError ){ |
+ sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); |
+ rc = pCtx->isError; |
+ } |
+ sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1); |
+ if( rc ) goto abort_due_to_error; |
+ } |
+ |
+ /* Copy the result of the function into register P3 */ |
+ if( pOut->flags & (MEM_Str|MEM_Blob) ){ |
+ sqlite3VdbeChangeEncoding(pCtx->pOut, encoding); |
+ if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big; |
+ } |
+ |
+ REGISTER_TRACE(pOp->p3, pCtx->pOut); |
+ UPDATE_MAX_BLOBSIZE(pCtx->pOut); |
+ 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); |
+ VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2); |
+ if( (pIn1->flags & MEM_Int)==0 ){ |
+ if( pOp->p2==0 ){ |
+ rc = SQLITE_MISMATCH; |
+ goto abort_due_to_error; |
+ }else{ |
+ goto jump_to_p2; |
+ } |
+ } |
+ } |
+ 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 ){ |
+ sqlite3VdbeMemRealify(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 value="97"> TEXT |
+** <li value="98"> BLOB |
+** <li value="99"> NUMERIC |
+** <li value="100"> INTEGER |
+** <li value="101"> 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); |
+ sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); |
+ UPDATE_MAX_BLOBSIZE(pIn1); |
+ if( rc ) goto abort_due_to_error; |
+ 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( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne ); |
+ assert( (flags1 & MEM_Cleared)==0 ); |
+ assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 ); |
+ if( (flags1&flags3&MEM_Null)!=0 |
+ && (flags3&MEM_Cleared)==0 |
+ ){ |
+ res = 0; /* Operands are equal */ |
+ }else{ |
+ res = 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_Real|MEM_Str))==MEM_Str ){ |
+ applyNumericAffinity(pIn1,0); |
+ testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */ |
+ flags3 = pIn3->flags; |
+ } |
+ if( (flags3 & (MEM_Int|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))!=0 ){ |
+ testcase( pIn1->flags & MEM_Int ); |
+ testcase( pIn1->flags & MEM_Real ); |
+ sqlite3VdbeMemStringify(pIn1, encoding, 1); |
+ testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); |
+ flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); |
+ assert( pIn1!=pIn3 ); |
+ } |
+ if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){ |
+ testcase( pIn3->flags & MEM_Int ); |
+ testcase( pIn3->flags & MEM_Real ); |
+ 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: |
+ switch( pOp->opcode ){ |
+ case OP_Eq: res2 = res==0; break; |
+ case OP_Ne: res2 = res; break; |
+ case OP_Lt: res2 = res<0; break; |
+ case OP_Le: res2 = res<=0; break; |
+ case OP_Gt: res2 = res>0; break; |
+ default: res2 = res>=0; break; |
+ } |
+ |
+ /* Undo any changes made by applyAffinity() to the input registers. */ |
+ assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); |
+ pIn1->flags = flags1; |
+ assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); |
+ pIn3->flags = flags3; |
+ |
+ if( pOp->p5 & SQLITE_STOREP2 ){ |
+ pOut = &aMem[pOp->p2]; |
+ iCompare = res; |
+ res2 = res2!=0; /* For this path res2 must be exactly 0 or 1 */ |
+ 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(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
+ if( res2 ){ |
+ goto jump_to_p2; |
+ } |
+ } |
+ break; |
+} |
+ |
+/* Opcode: ElseNotEq * P2 * * * |
+** |
+** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator. |
+** If result of an OP_Eq comparison on the same two operands |
+** would have be 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 */ |
+ assert( pOp>aOp ); |
+ assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt ); |
+ assert( pOp[-1].p5 & SQLITE_STOREP2 ); |
+ 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; |
+ int idx; |
+ CollSeq *pColl; /* Collating sequence to use on this term */ |
+ int bRev; /* True for DESCENDING sort order */ |
+ int *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; |
+#if SQLITE_DEBUG |
+ if( aPermute ){ |
+ int k, mx = 0; |
+ for(k=0; k<n; k++) if( aPermute[k]>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] : 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->nField ); |
+ pColl = pKeyInfo->aColl[i]; |
+ bRev = pKeyInfo->aSortOrder[i]; |
+ iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); |
+ if( 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,3); pOp = &aOp[pOp->p1 - 1]; |
+ }else if( iCompare==0 ){ |
+ VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1]; |
+ }else{ |
+ VdbeBranchTaken(2,3); 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 */ |
+ |
+ pIn1 = &aMem[pOp->p1]; |
+ if( pIn1->flags & MEM_Null ){ |
+ v1 = 2; |
+ }else{ |
+ v1 = sqlite3VdbeIntValue(pIn1)!=0; |
+ } |
+ pIn2 = &aMem[pOp->p2]; |
+ if( pIn2->flags & MEM_Null ){ |
+ v2 = 2; |
+ }else{ |
+ v2 = sqlite3VdbeIntValue(pIn2)!=0; |
+ } |
+ 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: 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]; |
+ sqlite3VdbeMemSetNull(pOut); |
+ if( (pIn1->flags & MEM_Null)==0 ){ |
+ pOut->flags = MEM_Int; |
+ pOut->u.i = !sqlite3VdbeIntValue(pIn1); |
+ } |
+ break; |
+} |
+ |
+/* Opcode: BitNot P1 P2 * * * |
+** Synopsis: r[P1]= ~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 * * * |
+** |
+** If the P1 value is equal to the P1 value on the OP_Init opcode at |
+** instruction 0, then jump to P2. If the two P1 values differ, then |
+** set the P1 value on this opcode to equal the P1 value on the OP_Init |
+** and fall through. |
+*/ |
+case OP_Once: { /* jump */ |
+ assert( p->aOp[0].opcode==OP_Init ); |
+ VdbeBranchTaken(p->aOp[0].p1==pOp->p1, 2); |
+ if( p->aOp[0].p1==pOp->p1 ){ |
+ goto jump_to_p2; |
+ }else{ |
+ 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. |
+*/ |
+/* 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_If: /* jump, in1 */ |
+case OP_IfNot: { /* jump, in1 */ |
+ int c; |
+ pIn1 = &aMem[pOp->p1]; |
+ if( pIn1->flags & MEM_Null ){ |
+ c = pOp->p3; |
+ }else{ |
+#ifdef SQLITE_OMIT_FLOATING_POINT |
+ c = sqlite3VdbeIntValue(pIn1)!=0; |
+#else |
+ c = sqlite3VdbeRealValue(pIn1)!=0.0; |
+#endif |
+ if( pOp->opcode==OP_IfNot ) c = !c; |
+ } |
+ 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: 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 column 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_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor, |
+** then the cache of the cursor is reset prior to extracting the column. |
+** The first OP_Column against a pseudo-table after the value of the content |
+** register has changed should have this bit set. |
+** |
+** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when |
+** 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: { |
+ int 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 */ |
+ u32 offset; /* Offset into the data */ |
+ u64 offset64; /* 64-bit offset */ |
+ u32 avail; /* Number of bytes of available data */ |
+ u32 t; /* A type code from the record header */ |
+ Mem *pReg; /* PseudoTable input register */ |
+ |
+ pC = p->apCsr[pOp->p1]; |
+ p2 = pOp->p2; |
+ |
+ /* If the cursor cache is stale, bring it up-to-date */ |
+ 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( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ assert( pC!=0 ); |
+ assert( p2<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 ){ |
+ assert( pC->uc.pseudoTableReg>0 ); |
+ pReg = &aMem[pC->uc.pseudoTableReg]; |
+ assert( pReg->flags & MEM_Blob ); |
+ assert( memIsValid(pReg) ); |
+ pC->payloadSize = pC->szRow = avail = 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, &avail); |
+ assert( avail<=65536 ); /* Maximum page size is 64KiB */ |
+ if( pC->payloadSize <= (u32)avail ){ |
+ pC->szRow = pC->payloadSize; |
+ }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
+ goto too_big; |
+ }else{ |
+ pC->szRow = avail; |
+ } |
+ } |
+ pC->cacheStatus = p->cacheCtr; |
+ pC->iHdrOffset = getVarint32(pC->aRow, offset); |
+ pC->nHdrParsed = 0; |
+ aOffset[0] = offset; |
+ |
+ |
+ if( avail<offset ){ /*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( offset > 98307 || offset > pC->payloadSize ){ |
+ rc = SQLITE_CORRUPT_BKPT; |
+ goto abort_due_to_error; |
+ } |
+ }else if( offset>0 ){ /*OPTIMIZATION-IF-TRUE*/ |
+ /* The following goto is an optimization. It can be omitted and |
+ ** everything will still work. But OP_Column is measurably faster |
+ ** by skipping the subsequent conditional, which is always true. |
+ */ |
+ zData = pC->aRow; |
+ assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ |
+ 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 = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, 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]; |
+ do{ |
+ if( (t = zHdr[0])<0x80 ){ |
+ zHdr++; |
+ offset64 += sqlite3VdbeOneByteSerialTypeLen(t); |
+ }else{ |
+ zHdr += sqlite3GetVarint32(zHdr, &t); |
+ offset64 += sqlite3VdbeSerialTypeLen(t); |
+ } |
+ pC->aType[i++] = t; |
+ aOffset[i] = (u32)(offset64 & 0xffffffff); |
+ }while( 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( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); |
+ rc = SQLITE_CORRUPT_BKPT; |
+ goto abort_due_to_error; |
+ } |
+ |
+ 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. */ |
+ static u8 aZero[8]; /* This is the bogus content */ |
+ sqlite3VdbeSerialGet(aZero, 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; |
+} |
+ |
+/* 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 nth character of the |
+** string indicates the column affinity that should be used for the nth |
+** memory cell in the range. |
+*/ |
+case OP_Affinity: { |
+ const char *zAffinity; /* The affinity to be applied */ |
+ char cAff; /* A single character of affinity */ |
+ |
+ zAffinity = pOp->p4.z; |
+ assert( zAffinity!=0 ); |
+ assert( zAffinity[pOp->p2]==0 ); |
+ pIn1 = &aMem[pOp->p1]; |
+ while( (cAff = *(zAffinity++))!=0 ){ |
+ assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); |
+ assert( memIsValid(pIn1) ); |
+ applyAffinity(pIn1, cAff, encoding); |
+ 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 nth character of the |
+** string indicates the column affinity that should be used for the nth |
+** 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. |
+*/ |
+case OP_MakeRecord: { |
+ u8 *zNewRecord; /* A buffer to hold the data for the new record */ |
+ 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 */ |
+ int i; /* Space used in zNewRecord[] header */ |
+ int j; /* Space used in zNewRecord[] content */ |
+ u32 len; /* Length of a field */ |
+ |
+ /* 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++), encoding); |
+ 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. |
+ */ |
+ pRec = pLast; |
+ do{ |
+ assert( memIsValid(pRec) ); |
+ pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len); |
+ if( pRec->flags & MEM_Zero ){ |
+ if( nData ){ |
+ if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; |
+ }else{ |
+ nZero += pRec->u.nZero; |
+ len -= pRec->u.nZero; |
+ } |
+ } |
+ nData += len; |
+ testcase( serial_type==127 ); |
+ testcase( serial_type==128 ); |
+ nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(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; |
+ if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
+ goto too_big; |
+ } |
+ |
+ /* 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( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ |
+ goto no_mem; |
+ } |
+ zNewRecord = (u8 *)pOut->z; |
+ |
+ /* Write the record */ |
+ i = putVarint32(zNewRecord, nHdr); |
+ j = 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. */ |
+ i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
+ /* EVIDENCE-OF: R-64536-51728 The values for each column in the record |
+ ** immediately follow the header. */ |
+ j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ |
+ }while( (++pRec)<=pLast ); |
+ assert( i==nHdr ); |
+ assert( j==nByte ); |
+ |
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
+ pOut->n = (int)nByte; |
+ pOut->flags = MEM_Blob; |
+ if( nZero ){ |
+ pOut->u.nZero = nZero; |
+ pOut->flags |= MEM_Zero; |
+ } |
+ pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ |
+ REGISTER_TRACE(pOp->p3, pOut); |
+ UPDATE_MAX_BLOBSIZE(pOut); |
+ break; |
+} |
+ |
+/* Opcode: Count P1 P2 * * * |
+** Synopsis: r[P2]=count() |
+** |
+** Store the number of entries (an integer value) in the table or index |
+** opened by cursor P1 in register P2 |
+*/ |
+#ifndef SQLITE_OMIT_BTREECOUNT |
+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 ); |
+ nEntry = 0; /* Not needed. Only used to silence a warning. */ |
+ rc = sqlite3BtreeCount(pCrsr, &nEntry); |
+ if( rc ) goto abort_due_to_error; |
+ pOut = out2Prerelease(p, pOp); |
+ pOut->u.i = nEntry; |
+ break; |
+} |
+#endif |
+ |
+/* 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, P1==0. To release (commit) an |
+** existing savepoint, P1==1, or to rollback an existing savepoint P1==2. |
+*/ |
+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{ |
+ 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; |
+ } |
+ db->isTransactionSavepoint = 0; |
+ rc = p->rc; |
+ }else{ |
+ int isSchemaChange; |
+ iSavepoint = db->nSavepoint - iSavepoint - 1; |
+ if( p1==SAVEPOINT_ROLLBACK ){ |
+ isSchemaChange = (db->flags & SQLITE_InternChanges)!=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{ |
+ 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); |
+ sqlite3ResetAllSchemasOfConnection(db); |
+ db->flags = (db->flags | SQLITE_InternChanges); |
+ } |
+ } |
+ |
+ /* 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{ |
+ 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; |
+ } |
+ assert( db->nStatement==0 ); |
+ 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; |
+ } |
+ break; |
+} |
+ |
+/* 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. |
+** |
+** 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; |
+ int iGen; |
+ |
+ assert( p->bIsReader ); |
+ assert( p->readOnly==0 || pOp->p2==0 ); |
+ 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); |
+ 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( pOp->p2 && p->usesStmtJournal |
+ && (db->autoCommit==0 || db->nVdbeRead>1) |
+ ){ |
+ assert( sqlite3BtreeIsInTrans(pBt) ); |
+ 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; |
+ } |
+ |
+ /* Gather the schema version number for checking: |
+ ** 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. |
+ */ |
+ sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta); |
+ iGen = db->aDb[pOp->p1].pSchema->iGeneration; |
+ }else{ |
+ iGen = iMeta = 0; |
+ } |
+ assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); |
+ if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){ |
+ 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 * * |
+** |
+** 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. |
+*/ |
+case OP_SetCookie: { |
+ Db *pDb; |
+ 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; |
+ db->flags |= SQLITE_InternChanges; |
+ }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); |
+ 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. |
+** |
+** If P5!=0 then use the content of register P2 as the root page, not |
+** the value of P2 itself. |
+** |
+** There will be a read lock on the database whenever there is an |
+** open cursor. If the database was unlocked prior to this instruction |
+** then a read lock is acquired as part of this instruction. A read |
+** lock allows other processes to read the database but prohibits |
+** any other process from modifying the database. The read lock is |
+** released when all cursors are closed. If this instruction attempts |
+** to get a read lock but fails, the script terminates with an |
+** SQLITE_BUSY error code. |
+** |
+** 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 |
+** structure, then said structure defines the content and collating |
+** sequence of the index being opened. Otherwise, if P4 is an integer |
+** value, it is set to the number of columns in the table. |
+** |
+** See also: OpenWrite, ReopenIdx |
+*/ |
+/* Opcode: ReopenIdx P1 P2 P3 P4 P5 |
+** Synopsis: root=P2 iDb=P3 |
+** |
+** The ReopenIdx opcode works exactly like ReadOpen except that it first |
+** checks to see if the cursor on P1 is already open with a root page |
+** number of P2 and if it is this opcode becomes a no-op. In other words, |
+** if the cursor is already open, do not reopen it. |
+** |
+** The ReopenIdx opcode may only be used with P5==0 and with P4 being |
+** a P4_KEYINFO object. Furthermore, the P3 value must be the same as |
+** every other ReopenIdx or OpenRead for the same cursor number. |
+** |
+** See the OpenRead opcode documentation for additional information. |
+*/ |
+/* Opcode: OpenWrite P1 P2 P3 P4 P5 |
+** Synopsis: root=P2 iDb=P3 |
+** |
+** Open a read/write cursor named P1 on the table or index whose root |
+** page is P2. Or if P5!=0 use the content of register P2 to find the |
+** root page. |
+** |
+** 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 |
+** structure, then said structure defines the content and collating |
+** sequence of the index being opened. Otherwise, if P4 is an integer |
+** value, it is set to the number of columns in the table, or to the |
+** largest index of any column of the table that is actually used. |
+** |
+** This instruction works just like OpenRead except that it opens the cursor |
+** in read/write mode. For a given table, there can be one or more read-only |
+** cursors or a single read/write cursor but not both. |
+** |
+** See also OpenRead. |
+*/ |
+case OP_ReopenIdx: { |
+ int nField; |
+ KeyInfo *pKeyInfo; |
+ int p2; |
+ int iDb; |
+ int wrFlag; |
+ Btree *pX; |
+ VdbeCursor *pCur; |
+ Db *pDb; |
+ |
+ assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
+ assert( pOp->p4type==P4_KEYINFO ); |
+ pCur = p->apCsr[pOp->p1]; |
+ if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ |
+ assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ |
+ goto open_cursor_set_hints; |
+ } |
+ /* If the cursor is not currently open or is open on a different |
+ ** index, then fall through into OP_OpenRead to force a reopen */ |
+case OP_OpenRead: |
+case OP_OpenWrite: |
+ |
+ assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
+ assert( p->bIsReader ); |
+ assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx |
+ || p->readOnly==0 ); |
+ |
+ if( p->expired ){ |
+ rc = SQLITE_ABORT_ROLLBACK; |
+ goto abort_due_to_error; |
+ } |
+ |
+ nField = 0; |
+ pKeyInfo = 0; |
+ p2 = pOp->p2; |
+ iDb = pOp->p3; |
+ assert( iDb>=0 && iDb<db->nDb ); |
+ assert( DbMaskTest(p->btreeMask, iDb) ); |
+ pDb = &db->aDb[iDb]; |
+ pX = pDb->pBt; |
+ assert( pX!=0 ); |
+ if( pOp->opcode==OP_OpenWrite ){ |
+ assert( OPFLAG_FORDELETE==BTREE_FORDELETE ); |
+ wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE); |
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
+ if( pDb->pSchema->file_format < p->minWriteFileFormat ){ |
+ p->minWriteFileFormat = pDb->pSchema->file_format; |
+ } |
+ }else{ |
+ wrFlag = 0; |
+ } |
+ if( pOp->p5 & OPFLAG_P2ISREG ){ |
+ assert( p2>0 ); |
+ assert( p2<=(p->nMem+1 - p->nCursor) ); |
+ pIn2 = &aMem[p2]; |
+ assert( memIsValid(pIn2) ); |
+ assert( (pIn2->flags & MEM_Int)!=0 ); |
+ sqlite3VdbeMemIntegerify(pIn2); |
+ p2 = (int)pIn2->u.i; |
+ /* The p2 value always comes from a prior OP_CreateTable opcode and |
+ ** that opcode will always set the p2 value to 2 or more or else fail. |
+ ** If there were a failure, the prepared statement would have halted |
+ ** before reaching this instruction. */ |
+ assert( p2>=2 ); |
+ } |
+ if( pOp->p4type==P4_KEYINFO ){ |
+ pKeyInfo = pOp->p4.pKeyInfo; |
+ assert( pKeyInfo->enc==ENC(db) ); |
+ assert( pKeyInfo->db==db ); |
+ nField = pKeyInfo->nField+pKeyInfo->nXField; |
+ }else if( pOp->p4type==P4_INT32 ){ |
+ nField = pOp->p4.i; |
+ } |
+ assert( pOp->p1>=0 ); |
+ assert( nField>=0 ); |
+ testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ |
+ pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE); |
+ if( pCur==0 ) goto no_mem; |
+ pCur->nullRow = 1; |
+ pCur->isOrdered = 1; |
+ pCur->pgnoRoot = p2; |
+#ifdef SQLITE_DEBUG |
+ pCur->wrFlag = wrFlag; |
+#endif |
+ rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor); |
+ pCur->pKeyInfo = pKeyInfo; |
+ /* Set the VdbeCursor.isTable variable. Previous versions of |
+ ** SQLite used to check if the root-page flags were sane at this point |
+ ** and report database corruption if they were not, but this check has |
+ ** since moved into the btree layer. */ |
+ pCur->isTable = pOp->p4type!=P4_KEYINFO; |
+ |
+open_cursor_set_hints: |
+ assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); |
+ assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); |
+ testcase( pOp->p5 & OPFLAG_BULKCSR ); |
+#ifdef SQLITE_ENABLE_CURSOR_HINTS |
+ testcase( pOp->p2 & OPFLAG_SEEKEQ ); |
+#endif |
+ sqlite3BtreeCursorHintFlags(pCur->uc.pCursor, |
+ (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); |
+ if( rc ) goto abort_due_to_error; |
+ break; |
+} |
+ |
+/* Opcode: OpenEphemeral P1 P2 * P4 P5 |
+** Synopsis: nColumn=P2 |
+** |
+** Open a new cursor P1 to a transient table. |
+** The cursor is always opened read/write even if |
+** the main database is read-only. The ephemeral |
+** table is deleted automatically when the cursor is closed. |
+** |
+** P2 is the number of columns in the ephemeral table. |
+** The cursor points to a BTree table if P4==0 and to a BTree index |
+** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure |
+** that defines the format of keys in the index. |
+** |
+** The P5 parameter can be a mask of the BTREE_* flags defined |
+** in btree.h. These flags control aspects of the operation of |
+** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are |
+** added automatically. |
+*/ |
+/* Opcode: OpenAutoindex P1 P2 * P4 * |
+** Synopsis: nColumn=P2 |
+** |
+** This opcode works the same as OP_OpenEphemeral. It has a |
+** different name to distinguish its use. Tables created using |
+** by this opcode will be used for automatically created transient |
+** indices in joins. |
+*/ |
+case OP_OpenAutoindex: |
+case OP_OpenEphemeral: { |
+ VdbeCursor *pCx; |
+ KeyInfo *pKeyInfo; |
+ |
+ static const int vfsFlags = |
+ SQLITE_OPEN_READWRITE | |
+ SQLITE_OPEN_CREATE | |
+ SQLITE_OPEN_EXCLUSIVE | |
+ SQLITE_OPEN_DELETEONCLOSE | |
+ SQLITE_OPEN_TRANSIENT_DB; |
+ assert( pOp->p1>=0 ); |
+ assert( pOp->p2>=0 ); |
+ pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE); |
+ if( pCx==0 ) goto no_mem; |
+ pCx->nullRow = 1; |
+ pCx->isEphemeral = 1; |
+ rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx, |
+ BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags); |
+ if( rc==SQLITE_OK ){ |
+ rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1); |
+ } |
+ if( rc==SQLITE_OK ){ |
+ /* If a transient index is required, create it by calling |
+ ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before |
+ ** opening it. If a transient table is required, just use the |
+ ** automatically created table with root-page 1 (an BLOB_INTKEY table). |
+ */ |
+ if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ |
+ int pgno; |
+ assert( pOp->p4type==P4_KEYINFO ); |
+ rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5); |
+ if( rc==SQLITE_OK ){ |
+ assert( pgno==MASTER_ROOT+1 ); |
+ assert( pKeyInfo->db==db ); |
+ assert( pKeyInfo->enc==ENC(db) ); |
+ rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR, |
+ pKeyInfo, pCx->uc.pCursor); |
+ } |
+ pCx->isTable = 0; |
+ }else{ |
+ rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR, |
+ 0, pCx->uc.pCursor); |
+ pCx->isTable = 1; |
+ } |
+ } |
+ if( rc ) goto abort_due_to_error; |
+ pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); |
+ break; |
+} |
+ |
+/* Opcode: SorterOpen P1 P2 P3 P4 * |
+** |
+** This opcode works like OP_OpenEphemeral except that it opens |
+** a transient index that is specifically designed to sort large |
+** tables using an external merge-sort algorithm. |
+** |
+** If argument P3 is non-zero, then it indicates that the sorter may |
+** assume that a stable sort considering the first P3 fields of each |
+** key is sufficient to produce the required results. |
+*/ |
+case OP_SorterOpen: { |
+ VdbeCursor *pCx; |
+ |
+ assert( pOp->p1>=0 ); |
+ assert( pOp->p2>=0 ); |
+ pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER); |
+ if( pCx==0 ) goto no_mem; |
+ pCx->pKeyInfo = pOp->p4.pKeyInfo; |
+ assert( pCx->pKeyInfo->db==db ); |
+ assert( pCx->pKeyInfo->enc==ENC(db) ); |
+ rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); |
+ if( rc ) goto abort_due_to_error; |
+ break; |
+} |
+ |
+/* Opcode: SequenceTest P1 P2 * * * |
+** Synopsis: if( cursor[P1].ctr++ ) pc = P2 |
+** |
+** P1 is a sorter cursor. If the sequence counter is currently zero, jump |
+** to P2. Regardless of whether or not the jump is taken, increment the |
+** the sequence value. |
+*/ |
+case OP_SequenceTest: { |
+ VdbeCursor *pC; |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( isSorter(pC) ); |
+ if( (pC->seqCount++)==0 ){ |
+ goto jump_to_p2; |
+ } |
+ break; |
+} |
+ |
+/* Opcode: OpenPseudo P1 P2 P3 * * |
+** Synopsis: P3 columns in r[P2] |
+** |
+** Open a new cursor that points to a fake table that contains a single |
+** row of data. The content of that one row is the content of memory |
+** register P2. In other words, cursor P1 becomes an alias for the |
+** MEM_Blob content contained in register P2. |
+** |
+** A pseudo-table created by this opcode is used to hold a single |
+** row output from the sorter so that the row can be decomposed into |
+** individual columns using the OP_Column opcode. The OP_Column opcode |
+** is the only cursor opcode that works with a pseudo-table. |
+** |
+** P3 is the number of fields in the records that will be stored by |
+** the pseudo-table. |
+*/ |
+case OP_OpenPseudo: { |
+ VdbeCursor *pCx; |
+ |
+ assert( pOp->p1>=0 ); |
+ assert( pOp->p3>=0 ); |
+ pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO); |
+ if( pCx==0 ) goto no_mem; |
+ pCx->nullRow = 1; |
+ pCx->uc.pseudoTableReg = pOp->p2; |
+ pCx->isTable = 1; |
+ assert( pOp->p5==0 ); |
+ break; |
+} |
+ |
+/* Opcode: Close P1 * * * * |
+** |
+** Close a cursor previously opened as P1. If P1 is not |
+** currently open, this instruction is a no-op. |
+*/ |
+case OP_Close: { |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); |
+ p->apCsr[pOp->p1] = 0; |
+ break; |
+} |
+ |
+#ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
+/* Opcode: ColumnsUsed P1 * * P4 * |
+** |
+** This opcode (which only exists if SQLite was compiled with |
+** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the |
+** table or index for cursor P1 are used. P4 is a 64-bit integer |
+** (P4_INT64) in which the first 63 bits are one for each of the |
+** first 63 columns of the table or index that are actually used |
+** by the cursor. The high-order bit is set if any column after |
+** the 64th is used. |
+*/ |
+case OP_ColumnsUsed: { |
+ VdbeCursor *pC; |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ pC->maskUsed = *(u64*)pOp->p4.pI64; |
+ break; |
+} |
+#endif |
+ |
+/* Opcode: SeekGE P1 P2 P3 P4 * |
+** Synopsis: key=r[P3@P4] |
+** |
+** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), |
+** use the value in register P3 as the key. If cursor P1 refers |
+** to an SQL index, then P3 is the first in an array of P4 registers |
+** that are used as an unpacked index key. |
+** |
+** Reposition cursor P1 so that it points to the smallest entry that |
+** is greater than or equal to the key value. If there are no records |
+** greater than or equal to the key and P2 is not zero, then jump to P2. |
+** |
+** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this |
+** opcode will always land on a record that equally equals the key, or |
+** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this |
+** opcode must be followed by an IdxLE opcode with the same arguments. |
+** The IdxLE opcode will be skipped if this opcode succeeds, but the |
+** IdxLE opcode will be used on subsequent loop iterations. |
+** |
+** This opcode leaves the cursor configured to move in forward order, |
+** from the beginning toward the end. In other words, the cursor is |
+** configured to use Next, not Prev. |
+** |
+** See also: Found, NotFound, SeekLt, SeekGt, SeekLe |
+*/ |
+/* Opcode: SeekGT P1 P2 P3 P4 * |
+** Synopsis: key=r[P3@P4] |
+** |
+** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), |
+** use the value in register P3 as a key. If cursor P1 refers |
+** to an SQL index, then P3 is the first in an array of P4 registers |
+** that are used as an unpacked index key. |
+** |
+** Reposition cursor P1 so that it points to the smallest entry that |
+** is greater than the key value. If there are no records greater than |
+** the key and P2 is not zero, then jump to P2. |
+** |
+** This opcode leaves the cursor configured to move in forward order, |
+** from the beginning toward the end. In other words, the cursor is |
+** configured to use Next, not Prev. |
+** |
+** See also: Found, NotFound, SeekLt, SeekGe, SeekLe |
+*/ |
+/* Opcode: SeekLT P1 P2 P3 P4 * |
+** Synopsis: key=r[P3@P4] |
+** |
+** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), |
+** use the value in register P3 as a key. If cursor P1 refers |
+** to an SQL index, then P3 is the first in an array of P4 registers |
+** that are used as an unpacked index key. |
+** |
+** Reposition cursor P1 so that it points to the largest entry that |
+** is less than the key value. If there are no records less than |
+** the key and P2 is not zero, then jump to P2. |
+** |
+** This opcode leaves the cursor configured to move in reverse order, |
+** from the end toward the beginning. In other words, the cursor is |
+** configured to use Prev, not Next. |
+** |
+** See also: Found, NotFound, SeekGt, SeekGe, SeekLe |
+*/ |
+/* Opcode: SeekLE P1 P2 P3 P4 * |
+** Synopsis: key=r[P3@P4] |
+** |
+** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), |
+** use the value in register P3 as a key. If cursor P1 refers |
+** to an SQL index, then P3 is the first in an array of P4 registers |
+** that are used as an unpacked index key. |
+** |
+** Reposition cursor P1 so that it points to the largest entry that |
+** is less than or equal to the key value. If there are no records |
+** less than or equal to the key and P2 is not zero, then jump to P2. |
+** |
+** This opcode leaves the cursor configured to move in reverse order, |
+** from the end toward the beginning. In other words, the cursor is |
+** configured to use Prev, not Next. |
+** |
+** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this |
+** opcode will always land on a record that equally equals the key, or |
+** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this |
+** opcode must be followed by an IdxGE opcode with the same arguments. |
+** The IdxGE opcode will be skipped if this opcode succeeds, but the |
+** IdxGE opcode will be used on subsequent loop iterations. |
+** |
+** See also: Found, NotFound, SeekGt, SeekGe, SeekLt |
+*/ |
+case OP_SeekLT: /* jump, in3 */ |
+case OP_SeekLE: /* jump, in3 */ |
+case OP_SeekGE: /* jump, in3 */ |
+case OP_SeekGT: { /* jump, in3 */ |
+ int res; /* Comparison result */ |
+ int oc; /* Opcode */ |
+ VdbeCursor *pC; /* The cursor to seek */ |
+ UnpackedRecord r; /* The key to seek for */ |
+ int nField; /* Number of columns or fields in the key */ |
+ i64 iKey; /* The rowid we are to seek to */ |
+ int eqOnly; /* Only interested in == results */ |
+ |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ assert( pOp->p2!=0 ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ assert( OP_SeekLE == OP_SeekLT+1 ); |
+ assert( OP_SeekGE == OP_SeekLT+2 ); |
+ assert( OP_SeekGT == OP_SeekLT+3 ); |
+ assert( pC->isOrdered ); |
+ assert( pC->uc.pCursor!=0 ); |
+ oc = pOp->opcode; |
+ eqOnly = 0; |
+ pC->nullRow = 0; |
+#ifdef SQLITE_DEBUG |
+ pC->seekOp = pOp->opcode; |
+#endif |
+ |
+ if( pC->isTable ){ |
+ /* The BTREE_SEEK_EQ flag is only set on index cursors */ |
+ assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 |
+ || CORRUPT_DB ); |
+ |
+ /* The input value in P3 might be of any type: integer, real, string, |
+ ** blob, or NULL. But it needs to be an integer before we can do |
+ ** the seek, so convert it. */ |
+ pIn3 = &aMem[pOp->p3]; |
+ if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
+ applyNumericAffinity(pIn3, 0); |
+ } |
+ iKey = sqlite3VdbeIntValue(pIn3); |
+ |
+ /* If the P3 value could not be converted into an integer without |
+ ** loss of information, then special processing is required... */ |
+ if( (pIn3->flags & MEM_Int)==0 ){ |
+ if( (pIn3->flags & MEM_Real)==0 ){ |
+ /* If the P3 value cannot be converted into any kind of a number, |
+ ** then the seek is not possible, so jump to P2 */ |
+ VdbeBranchTaken(1,2); goto jump_to_p2; |
+ break; |
+ } |
+ |
+ /* If the approximation iKey is larger than the actual real search |
+ ** term, substitute >= for > and < for <=. e.g. if the search term |
+ ** is 4.9 and the integer approximation 5: |
+ ** |
+ ** (x > 4.9) -> (x >= 5) |
+ ** (x <= 4.9) -> (x < 5) |
+ */ |
+ if( pIn3->u.r<(double)iKey ){ |
+ assert( OP_SeekGE==(OP_SeekGT-1) ); |
+ assert( OP_SeekLT==(OP_SeekLE-1) ); |
+ assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) ); |
+ if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--; |
+ } |
+ |
+ /* If the approximation iKey is smaller than the actual real search |
+ ** term, substitute <= for < and > for >=. */ |
+ else if( pIn3->u.r>(double)iKey ){ |
+ assert( OP_SeekLE==(OP_SeekLT+1) ); |
+ assert( OP_SeekGT==(OP_SeekGE+1) ); |
+ assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); |
+ if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; |
+ } |
+ } |
+ rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res); |
+ pC->movetoTarget = iKey; /* Used by OP_Delete */ |
+ if( rc!=SQLITE_OK ){ |
+ goto abort_due_to_error; |
+ } |
+ }else{ |
+ /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and |
+ ** OP_SeekLE opcodes are allowed, and these must be immediately followed |
+ ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key. |
+ */ |
+ if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){ |
+ eqOnly = 1; |
+ assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE ); |
+ assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
+ assert( pOp[1].p1==pOp[0].p1 ); |
+ assert( pOp[1].p2==pOp[0].p2 ); |
+ assert( pOp[1].p3==pOp[0].p3 ); |
+ assert( pOp[1].p4.i==pOp[0].p4.i ); |
+ } |
+ |
+ nField = pOp->p4.i; |
+ assert( pOp->p4type==P4_INT32 ); |
+ assert( nField>0 ); |
+ r.pKeyInfo = pC->pKeyInfo; |
+ r.nField = (u16)nField; |
+ |
+ /* The next line of code computes as follows, only faster: |
+ ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ |
+ ** r.default_rc = -1; |
+ ** }else{ |
+ ** r.default_rc = +1; |
+ ** } |
+ */ |
+ r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); |
+ assert( oc!=OP_SeekGT || r.default_rc==-1 ); |
+ assert( oc!=OP_SeekLE || r.default_rc==-1 ); |
+ assert( oc!=OP_SeekGE || r.default_rc==+1 ); |
+ assert( oc!=OP_SeekLT || r.default_rc==+1 ); |
+ |
+ r.aMem = &aMem[pOp->p3]; |
+#ifdef SQLITE_DEBUG |
+ { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
+#endif |
+ r.eqSeen = 0; |
+ rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res); |
+ if( rc!=SQLITE_OK ){ |
+ goto abort_due_to_error; |
+ } |
+ if( eqOnly && r.eqSeen==0 ){ |
+ assert( res!=0 ); |
+ goto seek_not_found; |
+ } |
+ } |
+ pC->deferredMoveto = 0; |
+ pC->cacheStatus = CACHE_STALE; |
+#ifdef SQLITE_TEST |
+ sqlite3_search_count++; |
+#endif |
+ if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); |
+ if( res<0 || (res==0 && oc==OP_SeekGT) ){ |
+ res = 0; |
+ rc = sqlite3BtreeNext(pC->uc.pCursor, &res); |
+ if( rc!=SQLITE_OK ) goto abort_due_to_error; |
+ }else{ |
+ res = 0; |
+ } |
+ }else{ |
+ assert( oc==OP_SeekLT || oc==OP_SeekLE ); |
+ if( res>0 || (res==0 && oc==OP_SeekLT) ){ |
+ res = 0; |
+ rc = sqlite3BtreePrevious(pC->uc.pCursor, &res); |
+ if( rc!=SQLITE_OK ) goto abort_due_to_error; |
+ }else{ |
+ /* res might be negative because the table is empty. Check to |
+ ** see if this is the case. |
+ */ |
+ res = sqlite3BtreeEof(pC->uc.pCursor); |
+ } |
+ } |
+seek_not_found: |
+ assert( pOp->p2>0 ); |
+ VdbeBranchTaken(res!=0,2); |
+ if( res ){ |
+ goto jump_to_p2; |
+ }else if( eqOnly ){ |
+ assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
+ pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */ |
+ } |
+ break; |
+} |
+ |
+/* Opcode: Found P1 P2 P3 P4 * |
+** Synopsis: key=r[P3@P4] |
+** |
+** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
+** P4>0 then register P3 is the first of P4 registers that form an unpacked |
+** record. |
+** |
+** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
+** is a prefix of any entry in P1 then a jump is made to P2 and |
+** P1 is left pointing at the matching entry. |
+** |
+** This operation leaves the cursor in a state where it can be |
+** advanced in the forward direction. The Next instruction will work, |
+** but not the Prev instruction. |
+** |
+** See also: NotFound, NoConflict, NotExists. SeekGe |
+*/ |
+/* Opcode: NotFound P1 P2 P3 P4 * |
+** Synopsis: key=r[P3@P4] |
+** |
+** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
+** P4>0 then register P3 is the first of P4 registers that form an unpacked |
+** record. |
+** |
+** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
+** is not the prefix of any entry in P1 then a jump is made to P2. If P1 |
+** does contain an entry whose prefix matches the P3/P4 record then control |
+** falls through to the next instruction and P1 is left pointing at the |
+** matching entry. |
+** |
+** This operation leaves the cursor in a state where it cannot be |
+** advanced in either direction. In other words, the Next and Prev |
+** opcodes do not work after this operation. |
+** |
+** See also: Found, NotExists, NoConflict |
+*/ |
+/* Opcode: NoConflict P1 P2 P3 P4 * |
+** Synopsis: key=r[P3@P4] |
+** |
+** If P4==0 then register P3 holds a blob constructed by MakeRecord. If |
+** P4>0 then register P3 is the first of P4 registers that form an unpacked |
+** record. |
+** |
+** Cursor P1 is on an index btree. If the record identified by P3 and P4 |
+** contains any NULL value, jump immediately to P2. If all terms of the |
+** record are not-NULL then a check is done to determine if any row in the |
+** P1 index btree has a matching key prefix. If there are no matches, jump |
+** immediately to P2. If there is a match, fall through and leave the P1 |
+** cursor pointing to the matching row. |
+** |
+** This opcode is similar to OP_NotFound with the exceptions that the |
+** branch is always taken if any part of the search key input is NULL. |
+** |
+** This operation leaves the cursor in a state where it cannot be |
+** advanced in either direction. In other words, the Next and Prev |
+** opcodes do not work after this operation. |
+** |
+** See also: NotFound, Found, NotExists |
+*/ |
+case OP_NoConflict: /* jump, in3 */ |
+case OP_NotFound: /* jump, in3 */ |
+case OP_Found: { /* jump, in3 */ |
+ int alreadyExists; |
+ int takeJump; |
+ int ii; |
+ VdbeCursor *pC; |
+ int res; |
+ UnpackedRecord *pFree; |
+ UnpackedRecord *pIdxKey; |
+ UnpackedRecord r; |
+ |
+#ifdef SQLITE_TEST |
+ if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; |
+#endif |
+ |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ assert( pOp->p4type==P4_INT32 ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+#ifdef SQLITE_DEBUG |
+ pC->seekOp = pOp->opcode; |
+#endif |
+ pIn3 = &aMem[pOp->p3]; |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ assert( pC->uc.pCursor!=0 ); |
+ assert( pC->isTable==0 ); |
+ if( pOp->p4.i>0 ){ |
+ r.pKeyInfo = pC->pKeyInfo; |
+ r.nField = (u16)pOp->p4.i; |
+ r.aMem = pIn3; |
+#ifdef SQLITE_DEBUG |
+ for(ii=0; ii<r.nField; ii++){ |
+ assert( memIsValid(&r.aMem[ii]) ); |
+ assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 ); |
+ if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); |
+ } |
+#endif |
+ pIdxKey = &r; |
+ pFree = 0; |
+ }else{ |
+ pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo); |
+ if( pIdxKey==0 ) goto no_mem; |
+ assert( pIn3->flags & MEM_Blob ); |
+ (void)ExpandBlob(pIn3); |
+ sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); |
+ } |
+ pIdxKey->default_rc = 0; |
+ takeJump = 0; |
+ if( pOp->opcode==OP_NoConflict ){ |
+ /* For the OP_NoConflict opcode, take the jump if any of the |
+ ** input fields are NULL, since any key with a NULL will not |
+ ** conflict */ |
+ for(ii=0; ii<pIdxKey->nField; ii++){ |
+ if( pIdxKey->aMem[ii].flags & MEM_Null ){ |
+ takeJump = 1; |
+ break; |
+ } |
+ } |
+ } |
+ rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res); |
+ if( pFree ) sqlite3DbFree(db, pFree); |
+ if( rc!=SQLITE_OK ){ |
+ goto abort_due_to_error; |
+ } |
+ pC->seekResult = res; |
+ alreadyExists = (res==0); |
+ pC->nullRow = 1-alreadyExists; |
+ pC->deferredMoveto = 0; |
+ pC->cacheStatus = CACHE_STALE; |
+ if( pOp->opcode==OP_Found ){ |
+ VdbeBranchTaken(alreadyExists!=0,2); |
+ if( alreadyExists ) goto jump_to_p2; |
+ }else{ |
+ VdbeBranchTaken(takeJump||alreadyExists==0,2); |
+ if( takeJump || !alreadyExists ) goto jump_to_p2; |
+ } |
+ break; |
+} |
+ |
+/* Opcode: SeekRowid P1 P2 P3 * * |
+** Synopsis: intkey=r[P3] |
+** |
+** P1 is the index of a cursor open on an SQL table btree (with integer |
+** keys). If register P3 does not contain an integer or if P1 does not |
+** contain a record with rowid P3 then jump immediately to P2. |
+** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain |
+** a record with rowid P3 then |
+** leave the cursor pointing at that record and fall through to the next |
+** instruction. |
+** |
+** The OP_NotExists opcode performs the same operation, but with OP_NotExists |
+** the P3 register must be guaranteed to contain an integer value. With this |
+** opcode, register P3 might not contain an integer. |
+** |
+** The OP_NotFound opcode performs the same operation on index btrees |
+** (with arbitrary multi-value keys). |
+** |
+** This opcode leaves the cursor in a state where it cannot be advanced |
+** in either direction. In other words, the Next and Prev opcodes will |
+** not work following this opcode. |
+** |
+** See also: Found, NotFound, NoConflict, SeekRowid |
+*/ |
+/* Opcode: NotExists P1 P2 P3 * * |
+** Synopsis: intkey=r[P3] |
+** |
+** P1 is the index of a cursor open on an SQL table btree (with integer |
+** keys). P3 is an integer rowid. If P1 does not contain a record with |
+** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an |
+** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then |
+** leave the cursor pointing at that record and fall through to the next |
+** instruction. |
+** |
+** The OP_SeekRowid opcode performs the same operation but also allows the |
+** P3 register to contain a non-integer value, in which case the jump is |
+** always taken. This opcode requires that P3 always contain an integer. |
+** |
+** The OP_NotFound opcode performs the same operation on index btrees |
+** (with arbitrary multi-value keys). |
+** |
+** This opcode leaves the cursor in a state where it cannot be advanced |
+** in either direction. In other words, the Next and Prev opcodes will |
+** not work following this opcode. |
+** |
+** See also: Found, NotFound, NoConflict, SeekRowid |
+*/ |
+case OP_SeekRowid: { /* jump, in3 */ |
+ VdbeCursor *pC; |
+ BtCursor *pCrsr; |
+ int res; |
+ u64 iKey; |
+ |
+ pIn3 = &aMem[pOp->p3]; |
+ if( (pIn3->flags & MEM_Int)==0 ){ |
+ applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding); |
+ if( (pIn3->flags & MEM_Int)==0 ) goto jump_to_p2; |
+ } |
+ /* Fall through into OP_NotExists */ |
+case OP_NotExists: /* jump, in3 */ |
+ pIn3 = &aMem[pOp->p3]; |
+ assert( pIn3->flags & MEM_Int ); |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+#ifdef SQLITE_DEBUG |
+ pC->seekOp = 0; |
+#endif |
+ assert( pC->isTable ); |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ pCrsr = pC->uc.pCursor; |
+ assert( pCrsr!=0 ); |
+ res = 0; |
+ iKey = pIn3->u.i; |
+ rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); |
+ assert( rc==SQLITE_OK || res==0 ); |
+ pC->movetoTarget = iKey; /* Used by OP_Delete */ |
+ pC->nullRow = 0; |
+ pC->cacheStatus = CACHE_STALE; |
+ pC->deferredMoveto = 0; |
+ VdbeBranchTaken(res!=0,2); |
+ pC->seekResult = res; |
+ if( res!=0 ){ |
+ assert( rc==SQLITE_OK ); |
+ if( pOp->p2==0 ){ |
+ rc = SQLITE_CORRUPT_BKPT; |
+ }else{ |
+ goto jump_to_p2; |
+ } |
+ } |
+ if( rc ) goto abort_due_to_error; |
+ break; |
+} |
+ |
+/* Opcode: Sequence P1 P2 * * * |
+** Synopsis: r[P2]=cursor[P1].ctr++ |
+** |
+** Find the next available sequence number for cursor P1. |
+** Write the sequence number into register P2. |
+** The sequence number on the cursor is incremented after this |
+** instruction. |
+*/ |
+case OP_Sequence: { /* out2 */ |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ assert( p->apCsr[pOp->p1]!=0 ); |
+ assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB ); |
+ pOut = out2Prerelease(p, pOp); |
+ pOut->u.i = p->apCsr[pOp->p1]->seqCount++; |
+ break; |
+} |
+ |
+ |
+/* Opcode: NewRowid P1 P2 P3 * * |
+** Synopsis: r[P2]=rowid |
+** |
+** Get a new integer record number (a.k.a "rowid") used as the key to a table. |
+** The record number is not previously used as a key in the database |
+** table that cursor P1 points to. The new record number is written |
+** written to register P2. |
+** |
+** If P3>0 then P3 is a register in the root frame of this VDBE that holds |
+** the largest previously generated record number. No new record numbers are |
+** allowed to be less than this value. When this value reaches its maximum, |
+** an SQLITE_FULL error is generated. The P3 register is updated with the ' |
+** generated record number. This P3 mechanism is used to help implement the |
+** AUTOINCREMENT feature. |
+*/ |
+case OP_NewRowid: { /* out2 */ |
+ i64 v; /* The new rowid */ |
+ VdbeCursor *pC; /* Cursor of table to get the new rowid */ |
+ int res; /* Result of an sqlite3BtreeLast() */ |
+ int cnt; /* Counter to limit the number of searches */ |
+ Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ |
+ VdbeFrame *pFrame; /* Root frame of VDBE */ |
+ |
+ v = 0; |
+ res = 0; |
+ pOut = out2Prerelease(p, pOp); |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ assert( pC->uc.pCursor!=0 ); |
+ { |
+ /* The next rowid or record number (different terms for the same |
+ ** thing) is obtained in a two-step algorithm. |
+ ** |
+ ** First we attempt to find the largest existing rowid and add one |
+ ** to that. But if the largest existing rowid is already the maximum |
+ ** positive integer, we have to fall through to the second |
+ ** probabilistic algorithm |
+ ** |
+ ** The second algorithm is to select a rowid at random and see if |
+ ** it already exists in the table. If it does not exist, we have |
+ ** succeeded. If the random rowid does exist, we select a new one |
+ ** and try again, up to 100 times. |
+ */ |
+ assert( pC->isTable ); |
+ |
+#ifdef SQLITE_32BIT_ROWID |
+# define MAX_ROWID 0x7fffffff |
+#else |
+ /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
+ ** Others complain about 0x7ffffffffffffffffLL. The following macro seems |
+ ** to provide the constant while making all compilers happy. |
+ */ |
+# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) |
+#endif |
+ |
+ if( !pC->useRandomRowid ){ |
+ rc = sqlite3BtreeLast(pC->uc.pCursor, &res); |
+ if( rc!=SQLITE_OK ){ |
+ goto abort_due_to_error; |
+ } |
+ if( res ){ |
+ v = 1; /* IMP: R-61914-48074 */ |
+ }else{ |
+ assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) ); |
+ v = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
+ if( v>=MAX_ROWID ){ |
+ pC->useRandomRowid = 1; |
+ }else{ |
+ v++; /* IMP: R-29538-34987 */ |
+ } |
+ } |
+ } |
+ |
+#ifndef SQLITE_OMIT_AUTOINCREMENT |
+ if( pOp->p3 ){ |
+ /* Assert that P3 is a valid memory cell. */ |
+ assert( pOp->p3>0 ); |
+ if( p->pFrame ){ |
+ for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
+ /* Assert that P3 is a valid memory cell. */ |
+ assert( pOp->p3<=pFrame->nMem ); |
+ pMem = &pFrame->aMem[pOp->p3]; |
+ }else{ |
+ /* Assert that P3 is a valid memory cell. */ |
+ assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); |
+ pMem = &aMem[pOp->p3]; |
+ memAboutToChange(p, pMem); |
+ } |
+ assert( memIsValid(pMem) ); |
+ |
+ REGISTER_TRACE(pOp->p3, pMem); |
+ sqlite3VdbeMemIntegerify(pMem); |
+ assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ |
+ if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ |
+ rc = SQLITE_FULL; /* IMP: R-17817-00630 */ |
+ goto abort_due_to_error; |
+ } |
+ if( v<pMem->u.i+1 ){ |
+ v = pMem->u.i + 1; |
+ } |
+ pMem->u.i = v; |
+ } |
+#endif |
+ if( pC->useRandomRowid ){ |
+ /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the |
+ ** largest possible integer (9223372036854775807) then the database |
+ ** engine starts picking positive candidate ROWIDs at random until |
+ ** it finds one that is not previously used. */ |
+ assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is |
+ ** an AUTOINCREMENT table. */ |
+ cnt = 0; |
+ do{ |
+ sqlite3_randomness(sizeof(v), &v); |
+ v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ |
+ }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v, |
+ 0, &res))==SQLITE_OK) |
+ && (res==0) |
+ && (++cnt<100)); |
+ if( rc ) goto abort_due_to_error; |
+ if( res==0 ){ |
+ rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
+ goto abort_due_to_error; |
+ } |
+ assert( v>0 ); /* EV: R-40812-03570 */ |
+ } |
+ pC->deferredMoveto = 0; |
+ pC->cacheStatus = CACHE_STALE; |
+ } |
+ pOut->u.i = v; |
+ break; |
+} |
+ |
+/* Opcode: Insert P1 P2 P3 P4 P5 |
+** Synopsis: intkey=r[P3] data=r[P2] |
+** |
+** Write an entry into the table of cursor P1. A new entry is |
+** created if it doesn't already exist or the data for an existing |
+** entry is overwritten. The data is the value MEM_Blob stored in register |
+** number P2. The key is stored in register P3. The key must |
+** be a MEM_Int. |
+** |
+** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is |
+** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, |
+** then rowid is stored for subsequent return by the |
+** sqlite3_last_insert_rowid() function (otherwise it is unmodified). |
+** |
+** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might |
+** run faster by avoiding an unnecessary seek on cursor P1. However, |
+** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior |
+** seeks on the cursor or if the most recent seek used a key equal to P3. |
+** |
+** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an |
+** UPDATE operation. Otherwise (if the flag is clear) then this opcode |
+** is part of an INSERT operation. The difference is only important to |
+** the update hook. |
+** |
+** Parameter P4 may point to a Table structure, or may be NULL. If it is |
+** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked |
+** following a successful insert. |
+** |
+** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically |
+** allocated, then ownership of P2 is transferred to the pseudo-cursor |
+** and register P2 becomes ephemeral. If the cursor is changed, the |
+** value of register P2 will then change. Make sure this does not |
+** cause any problems.) |
+** |
+** This instruction only works on tables. The equivalent instruction |
+** for indices is OP_IdxInsert. |
+*/ |
+/* Opcode: InsertInt P1 P2 P3 P4 P5 |
+** Synopsis: intkey=P3 data=r[P2] |
+** |
+** This works exactly like OP_Insert except that the key is the |
+** integer value P3, not the value of the integer stored in register P3. |
+*/ |
+case OP_Insert: |
+case OP_InsertInt: { |
+ Mem *pData; /* MEM cell holding data for the record to be inserted */ |
+ Mem *pKey; /* MEM cell holding key for the record */ |
+ VdbeCursor *pC; /* Cursor to table into which insert is written */ |
+ int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ |
+ const char *zDb; /* database name - used by the update hook */ |
+ Table *pTab; /* Table structure - used by update and pre-update hooks */ |
+ int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ |
+ BtreePayload x; /* Payload to be inserted */ |
+ |
+ op = 0; |
+ pData = &aMem[pOp->p2]; |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ assert( memIsValid(pData) ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ assert( pC->uc.pCursor!=0 ); |
+ assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable ); |
+ assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); |
+ REGISTER_TRACE(pOp->p2, pData); |
+ |
+ if( pOp->opcode==OP_Insert ){ |
+ pKey = &aMem[pOp->p3]; |
+ assert( pKey->flags & MEM_Int ); |
+ assert( memIsValid(pKey) ); |
+ REGISTER_TRACE(pOp->p3, pKey); |
+ x.nKey = pKey->u.i; |
+ }else{ |
+ assert( pOp->opcode==OP_InsertInt ); |
+ x.nKey = pOp->p3; |
+ } |
+ |
+ if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
+ assert( pC->iDb>=0 ); |
+ zDb = db->aDb[pC->iDb].zDbSName; |
+ pTab = pOp->p4.pTab; |
+ assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) ); |
+ op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); |
+ }else{ |
+ pTab = 0; /* Not needed. Silence a compiler warning. */ |
+ zDb = 0; /* Not needed. Silence a compiler warning. */ |
+ } |
+ |
+#ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
+ /* Invoke the pre-update hook, if any */ |
+ if( db->xPreUpdateCallback |
+ && pOp->p4type==P4_TABLE |
+ && !(pOp->p5 & OPFLAG_ISUPDATE) |
+ ){ |
+ sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2); |
+ } |
+ if( pOp->p5 & OPFLAG_ISNOOP ) break; |
+#endif |
+ |
+ if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
+ if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey; |
+ if( pData->flags & MEM_Null ){ |
+ x.pData = 0; |
+ x.nData = 0; |
+ }else{ |
+ assert( pData->flags & (MEM_Blob|MEM_Str) ); |
+ x.pData = pData->z; |
+ x.nData = pData->n; |
+ } |
+ seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); |
+ if( pData->flags & MEM_Zero ){ |
+ x.nZero = pData->u.nZero; |
+ }else{ |
+ x.nZero = 0; |
+ } |
+ x.pKey = 0; |
+ rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
+ (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult |
+ ); |
+ pC->deferredMoveto = 0; |
+ pC->cacheStatus = CACHE_STALE; |
+ |
+ /* Invoke the update-hook if required. */ |
+ if( rc ) goto abort_due_to_error; |
+ if( db->xUpdateCallback && op ){ |
+ db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, x.nKey); |
+ } |
+ break; |
+} |
+ |
+/* Opcode: Delete P1 P2 P3 P4 P5 |
+** |
+** Delete the record at which the P1 cursor is currently pointing. |
+** |
+** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then |
+** the cursor will be left pointing at either the next or the previous |
+** record in the table. If it is left pointing at the next record, then |
+** the next Next instruction will be a no-op. As a result, in this case |
+** it is ok to delete a record from within a Next loop. If |
+** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be |
+** left in an undefined state. |
+** |
+** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this |
+** delete one of several associated with deleting a table row and all its |
+** associated index entries. Exactly one of those deletes is the "primary" |
+** delete. The others are all on OPFLAG_FORDELETE cursors or else are |
+** marked with the AUXDELETE flag. |
+** |
+** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row |
+** change count is incremented (otherwise not). |
+** |
+** P1 must not be pseudo-table. It has to be a real table with |
+** multiple rows. |
+** |
+** If P4 is not NULL then it points to a Table object. In this case either |
+** the update or pre-update hook, or both, may be invoked. The P1 cursor must |
+** have been positioned using OP_NotFound prior to invoking this opcode in |
+** this case. Specifically, if one is configured, the pre-update hook is |
+** invoked if P4 is not NULL. The update-hook is invoked if one is configured, |
+** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2. |
+** |
+** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address |
+** of the memory cell that contains the value that the rowid of the row will |
+** be set to by the update. |
+*/ |
+case OP_Delete: { |
+ VdbeCursor *pC; |
+ const char *zDb; |
+ Table *pTab; |
+ int opflags; |
+ |
+ opflags = pOp->p2; |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ assert( pC->uc.pCursor!=0 ); |
+ assert( pC->deferredMoveto==0 ); |
+ |
+#ifdef SQLITE_DEBUG |
+ if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){ |
+ /* If p5 is zero, the seek operation that positioned the cursor prior to |
+ ** OP_Delete will have also set the pC->movetoTarget field to the rowid of |
+ ** the row that is being deleted */ |
+ i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
+ assert( pC->movetoTarget==iKey ); |
+ } |
+#endif |
+ |
+ /* If the update-hook or pre-update-hook will be invoked, set zDb to |
+ ** the name of the db to pass as to it. Also set local pTab to a copy |
+ ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was |
+ ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set |
+ ** VdbeCursor.movetoTarget to the current rowid. */ |
+ if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ |
+ assert( pC->iDb>=0 ); |
+ assert( pOp->p4.pTab!=0 ); |
+ zDb = db->aDb[pC->iDb].zDbSName; |
+ pTab = pOp->p4.pTab; |
+ if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){ |
+ pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
+ } |
+ }else{ |
+ zDb = 0; /* Not needed. Silence a compiler warning. */ |
+ pTab = 0; /* Not needed. Silence a compiler warning. */ |
+ } |
+ |
+#ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
+ /* Invoke the pre-update-hook if required. */ |
+ if( db->xPreUpdateCallback && pOp->p4.pTab ){ |
+ assert( !(opflags & OPFLAG_ISUPDATE) |
+ || HasRowid(pTab)==0 |
+ || (aMem[pOp->p3].flags & MEM_Int) |
+ ); |
+ sqlite3VdbePreUpdateHook(p, pC, |
+ (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, |
+ zDb, pTab, pC->movetoTarget, |
+ pOp->p3 |
+ ); |
+ } |
+ if( opflags & OPFLAG_ISNOOP ) break; |
+#endif |
+ |
+ /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ |
+ assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 ); |
+ assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION ); |
+ assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE ); |
+ |
+#ifdef SQLITE_DEBUG |
+ if( p->pFrame==0 ){ |
+ if( pC->isEphemeral==0 |
+ && (pOp->p5 & OPFLAG_AUXDELETE)==0 |
+ && (pC->wrFlag & OPFLAG_FORDELETE)==0 |
+ ){ |
+ nExtraDelete++; |
+ } |
+ if( pOp->p2 & OPFLAG_NCHANGE ){ |
+ nExtraDelete--; |
+ } |
+ } |
+#endif |
+ |
+ rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); |
+ pC->cacheStatus = CACHE_STALE; |
+ pC->seekResult = 0; |
+ if( rc ) goto abort_due_to_error; |
+ |
+ /* Invoke the update-hook if required. */ |
+ if( opflags & OPFLAG_NCHANGE ){ |
+ p->nChange++; |
+ if( db->xUpdateCallback && HasRowid(pTab) ){ |
+ db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName, |
+ pC->movetoTarget); |
+ assert( pC->iDb>=0 ); |
+ } |
+ } |
+ |
+ break; |
+} |
+/* Opcode: ResetCount * * * * * |
+** |
+** The value of the change counter is copied to the database handle |
+** change counter (returned by subsequent calls to sqlite3_changes()). |
+** Then the VMs internal change counter resets to 0. |
+** This is used by trigger programs. |
+*/ |
+case OP_ResetCount: { |
+ sqlite3VdbeSetChanges(db, p->nChange); |
+ p->nChange = 0; |
+ break; |
+} |
+ |
+/* Opcode: SorterCompare P1 P2 P3 P4 |
+** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 |
+** |
+** P1 is a sorter cursor. This instruction compares a prefix of the |
+** record blob in register P3 against a prefix of the entry that |
+** the sorter cursor currently points to. Only the first P4 fields |
+** of r[P3] and the sorter record are compared. |
+** |
+** If either P3 or the sorter contains a NULL in one of their significant |
+** fields (not counting the P4 fields at the end which are ignored) then |
+** the comparison is assumed to be equal. |
+** |
+** Fall through to next instruction if the two records compare equal to |
+** each other. Jump to P2 if they are different. |
+*/ |
+case OP_SorterCompare: { |
+ VdbeCursor *pC; |
+ int res; |
+ int nKeyCol; |
+ |
+ pC = p->apCsr[pOp->p1]; |
+ assert( isSorter(pC) ); |
+ assert( pOp->p4type==P4_INT32 ); |
+ pIn3 = &aMem[pOp->p3]; |
+ nKeyCol = pOp->p4.i; |
+ res = 0; |
+ rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); |
+ VdbeBranchTaken(res!=0,2); |
+ if( rc ) goto abort_due_to_error; |
+ if( res ) goto jump_to_p2; |
+ break; |
+}; |
+ |
+/* Opcode: SorterData P1 P2 P3 * * |
+** Synopsis: r[P2]=data |
+** |
+** Write into register P2 the current sorter data for sorter cursor P1. |
+** Then clear the column header cache on cursor P3. |
+** |
+** This opcode is normally use to move a record out of the sorter and into |
+** a register that is the source for a pseudo-table cursor created using |
+** OpenPseudo. That pseudo-table cursor is the one that is identified by |
+** parameter P3. Clearing the P3 column cache as part of this opcode saves |
+** us from having to issue a separate NullRow instruction to clear that cache. |
+*/ |
+case OP_SorterData: { |
+ VdbeCursor *pC; |
+ |
+ pOut = &aMem[pOp->p2]; |
+ pC = p->apCsr[pOp->p1]; |
+ assert( isSorter(pC) ); |
+ rc = sqlite3VdbeSorterRowkey(pC, pOut); |
+ assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ if( rc ) goto abort_due_to_error; |
+ p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; |
+ break; |
+} |
+ |
+/* Opcode: RowData P1 P2 P3 * * |
+** Synopsis: r[P2]=data |
+** |
+** Write into register P2 the complete row content for the row at |
+** which cursor P1 is currently pointing. |
+** There is no interpretation of the data. |
+** It is just copied onto the P2 register exactly as |
+** it is found in the database file. |
+** |
+** If cursor P1 is an index, then the content is the key of the row. |
+** If cursor P2 is a table, then the content extracted is the data. |
+** |
+** If the P1 cursor must be pointing to a valid row (not a NULL row) |
+** of a real table, not a pseudo-table. |
+** |
+** If P3!=0 then this opcode is allowed to make an ephermeral pointer |
+** into the database page. That means that the content of the output |
+** register will be invalidated as soon as the cursor moves - including |
+** moves caused by other cursors that "save" the the current cursors |
+** position in order that they can write to the same table. If P3==0 |
+** then a copy of the data is made into memory. P3!=0 is faster, but |
+** P3==0 is safer. |
+** |
+** If P3!=0 then the content of the P2 register is unsuitable for use |
+** in OP_Result and any OP_Result will invalidate the P2 register content. |
+** The P2 register content is invalidated by opcodes like OP_Function or |
+** by any use of another cursor pointing to the same table. |
+*/ |
+case OP_RowData: { |
+ VdbeCursor *pC; |
+ BtCursor *pCrsr; |
+ u32 n; |
+ |
+ pOut = out2Prerelease(p, pOp); |
+ |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ assert( isSorter(pC)==0 ); |
+ assert( pC->nullRow==0 ); |
+ assert( pC->uc.pCursor!=0 ); |
+ pCrsr = pC->uc.pCursor; |
+ |
+ /* The OP_RowData opcodes always follow OP_NotExists or |
+ ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions |
+ ** that might invalidate the cursor. |
+ ** If this where not the case, on of the following assert()s |
+ ** would fail. Should this ever change (because of changes in the code |
+ ** generator) then the fix would be to insert a call to |
+ ** sqlite3VdbeCursorMoveto(). |
+ */ |
+ assert( pC->deferredMoveto==0 ); |
+ assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
+#if 0 /* Not required due to the previous to assert() statements */ |
+ rc = sqlite3VdbeCursorMoveto(pC); |
+ if( rc!=SQLITE_OK ) goto abort_due_to_error; |
+#endif |
+ |
+ n = sqlite3BtreePayloadSize(pCrsr); |
+ if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
+ goto too_big; |
+ } |
+ testcase( n==0 ); |
+ rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut); |
+ if( rc ) goto abort_due_to_error; |
+ if( !pOp->p3 ) Deephemeralize(pOut); |
+ UPDATE_MAX_BLOBSIZE(pOut); |
+ REGISTER_TRACE(pOp->p2, pOut); |
+ break; |
+} |
+ |
+/* Opcode: Rowid P1 P2 * * * |
+** Synopsis: r[P2]=rowid |
+** |
+** Store in register P2 an integer which is the key of the table entry that |
+** P1 is currently point to. |
+** |
+** P1 can be either an ordinary table or a virtual table. There used to |
+** be a separate OP_VRowid opcode for use with virtual tables, but this |
+** one opcode now works for both table types. |
+*/ |
+case OP_Rowid: { /* out2 */ |
+ VdbeCursor *pC; |
+ i64 v; |
+ sqlite3_vtab *pVtab; |
+ const sqlite3_module *pModule; |
+ |
+ pOut = out2Prerelease(p, pOp); |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+ assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); |
+ if( pC->nullRow ){ |
+ pOut->flags = MEM_Null; |
+ break; |
+ }else if( pC->deferredMoveto ){ |
+ v = pC->movetoTarget; |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+ }else if( pC->eCurType==CURTYPE_VTAB ){ |
+ assert( pC->uc.pVCur!=0 ); |
+ pVtab = pC->uc.pVCur->pVtab; |
+ pModule = pVtab->pModule; |
+ assert( pModule->xRowid ); |
+ rc = pModule->xRowid(pC->uc.pVCur, &v); |
+ sqlite3VtabImportErrmsg(p, pVtab); |
+ if( rc ) goto abort_due_to_error; |
+#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
+ }else{ |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ assert( pC->uc.pCursor!=0 ); |
+ rc = sqlite3VdbeCursorRestore(pC); |
+ if( rc ) goto abort_due_to_error; |
+ if( pC->nullRow ){ |
+ pOut->flags = MEM_Null; |
+ break; |
+ } |
+ v = sqlite3BtreeIntegerKey(pC->uc.pCursor); |
+ } |
+ pOut->u.i = v; |
+ break; |
+} |
+ |
+/* Opcode: NullRow P1 * * * * |
+** |
+** Move the cursor P1 to a null row. Any OP_Column operations |
+** that occur while the cursor is on the null row will always |
+** write a NULL. |
+*/ |
+case OP_NullRow: { |
+ VdbeCursor *pC; |
+ |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+ pC->nullRow = 1; |
+ pC->cacheStatus = CACHE_STALE; |
+ if( pC->eCurType==CURTYPE_BTREE ){ |
+ assert( pC->uc.pCursor!=0 ); |
+ sqlite3BtreeClearCursor(pC->uc.pCursor); |
+ } |
+ break; |
+} |
+ |
+/* Opcode: Last P1 P2 P3 * * |
+** |
+** The next use of the Rowid or Column or Prev instruction for P1 |
+** will refer to the last entry in the database table or index. |
+** If the table or index is empty and P2>0, then jump immediately to P2. |
+** If P2 is 0 or if the table or index is not empty, fall through |
+** to the following instruction. |
+** |
+** This opcode leaves the cursor configured to move in reverse order, |
+** from the end toward the beginning. In other words, the cursor is |
+** configured to use Prev, not Next. |
+** |
+** If P3 is -1, then the cursor is positioned at the end of the btree |
+** for the purpose of appending a new entry onto the btree. In that |
+** case P2 must be 0. It is assumed that the cursor is used only for |
+** appending and so if the cursor is valid, then the cursor must already |
+** be pointing at the end of the btree and so no changes are made to |
+** the cursor. |
+*/ |
+case OP_Last: { /* jump */ |
+ VdbeCursor *pC; |
+ BtCursor *pCrsr; |
+ int res; |
+ |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ pCrsr = pC->uc.pCursor; |
+ res = 0; |
+ assert( pCrsr!=0 ); |
+ pC->seekResult = pOp->p3; |
+#ifdef SQLITE_DEBUG |
+ pC->seekOp = OP_Last; |
+#endif |
+ if( pOp->p3==0 || !sqlite3BtreeCursorIsValidNN(pCrsr) ){ |
+ rc = sqlite3BtreeLast(pCrsr, &res); |
+ pC->nullRow = (u8)res; |
+ pC->deferredMoveto = 0; |
+ pC->cacheStatus = CACHE_STALE; |
+ if( rc ) goto abort_due_to_error; |
+ if( pOp->p2>0 ){ |
+ VdbeBranchTaken(res!=0,2); |
+ if( res ) goto jump_to_p2; |
+ } |
+ }else{ |
+ assert( pOp->p2==0 ); |
+ } |
+ break; |
+} |
+ |
+ |
+/* Opcode: SorterSort P1 P2 * * * |
+** |
+** After all records have been inserted into the Sorter object |
+** identified by P1, invoke this opcode to actually do the sorting. |
+** Jump to P2 if there are no records to be sorted. |
+** |
+** This opcode is an alias for OP_Sort and OP_Rewind that is used |
+** for Sorter objects. |
+*/ |
+/* Opcode: Sort P1 P2 * * * |
+** |
+** This opcode does exactly the same thing as OP_Rewind except that |
+** it increments an undocumented global variable used for testing. |
+** |
+** Sorting is accomplished by writing records into a sorting index, |
+** then rewinding that index and playing it back from beginning to |
+** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
+** rewinding so that the global variable will be incremented and |
+** regression tests can determine whether or not the optimizer is |
+** correctly optimizing out sorts. |
+*/ |
+case OP_SorterSort: /* jump */ |
+case OP_Sort: { /* jump */ |
+#ifdef SQLITE_TEST |
+ sqlite3_sort_count++; |
+ sqlite3_search_count--; |
+#endif |
+ p->aCounter[SQLITE_STMTSTATUS_SORT]++; |
+ /* Fall through into OP_Rewind */ |
+} |
+/* Opcode: Rewind P1 P2 * * * |
+** |
+** The next use of the Rowid or Column or Next instruction for P1 |
+** will refer to the first entry in the database table or index. |
+** If the table or index is empty, jump immediately to P2. |
+** If the table or index is not empty, fall through to the following |
+** instruction. |
+** |
+** This opcode leaves the cursor configured to move in forward order, |
+** from the beginning toward the end. In other words, the cursor is |
+** configured to use Next, not Prev. |
+*/ |
+case OP_Rewind: { /* jump */ |
+ VdbeCursor *pC; |
+ BtCursor *pCrsr; |
+ int res; |
+ |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+ assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) ); |
+ res = 1; |
+#ifdef SQLITE_DEBUG |
+ pC->seekOp = OP_Rewind; |
+#endif |
+ if( isSorter(pC) ){ |
+ rc = sqlite3VdbeSorterRewind(pC, &res); |
+ }else{ |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ pCrsr = pC->uc.pCursor; |
+ assert( pCrsr ); |
+ rc = sqlite3BtreeFirst(pCrsr, &res); |
+ pC->deferredMoveto = 0; |
+ pC->cacheStatus = CACHE_STALE; |
+ } |
+ if( rc ) goto abort_due_to_error; |
+ pC->nullRow = (u8)res; |
+ assert( pOp->p2>0 && pOp->p2<p->nOp ); |
+ VdbeBranchTaken(res!=0,2); |
+ if( res ) goto jump_to_p2; |
+ break; |
+} |
+ |
+/* Opcode: Next P1 P2 P3 P4 P5 |
+** |
+** Advance cursor P1 so that it points to the next key/data pair in its |
+** table or index. If there are no more key/value pairs then fall through |
+** to the following instruction. But if the cursor advance was successful, |
+** jump immediately to P2. |
+** |
+** The Next opcode is only valid following an SeekGT, SeekGE, or |
+** OP_Rewind opcode used to position the cursor. Next is not allowed |
+** to follow SeekLT, SeekLE, or OP_Last. |
+** |
+** The P1 cursor must be for a real table, not a pseudo-table. P1 must have |
+** been opened prior to this opcode or the program will segfault. |
+** |
+** The P3 value is a hint to the btree implementation. If P3==1, that |
+** means P1 is an SQL index and that this instruction could have been |
+** omitted if that index had been unique. P3 is usually 0. P3 is |
+** always either 0 or 1. |
+** |
+** P4 is always of type P4_ADVANCE. The function pointer points to |
+** sqlite3BtreeNext(). |
+** |
+** If P5 is positive and the jump is taken, then event counter |
+** number P5-1 in the prepared statement is incremented. |
+** |
+** See also: Prev, NextIfOpen |
+*/ |
+/* Opcode: NextIfOpen P1 P2 P3 P4 P5 |
+** |
+** This opcode works just like Next except that if cursor P1 is not |
+** open it behaves a no-op. |
+*/ |
+/* Opcode: Prev P1 P2 P3 P4 P5 |
+** |
+** Back up cursor P1 so that it points to the previous key/data pair in its |
+** table or index. If there is no previous key/value pairs then fall through |
+** to the following instruction. But if the cursor backup was successful, |
+** jump immediately to P2. |
+** |
+** |
+** The Prev opcode is only valid following an SeekLT, SeekLE, or |
+** OP_Last opcode used to position the cursor. Prev is not allowed |
+** to follow SeekGT, SeekGE, or OP_Rewind. |
+** |
+** The P1 cursor must be for a real table, not a pseudo-table. If P1 is |
+** not open then the behavior is undefined. |
+** |
+** The P3 value is a hint to the btree implementation. If P3==1, that |
+** means P1 is an SQL index and that this instruction could have been |
+** omitted if that index had been unique. P3 is usually 0. P3 is |
+** always either 0 or 1. |
+** |
+** P4 is always of type P4_ADVANCE. The function pointer points to |
+** sqlite3BtreePrevious(). |
+** |
+** If P5 is positive and the jump is taken, then event counter |
+** number P5-1 in the prepared statement is incremented. |
+*/ |
+/* Opcode: PrevIfOpen P1 P2 P3 P4 P5 |
+** |
+** This opcode works just like Prev except that if cursor P1 is not |
+** open it behaves a no-op. |
+*/ |
+/* Opcode: SorterNext P1 P2 * * P5 |
+** |
+** This opcode works just like OP_Next except that P1 must be a |
+** sorter object for which the OP_SorterSort opcode has been |
+** invoked. This opcode advances the cursor to the next sorted |
+** record, or jumps to P2 if there are no more sorted records. |
+*/ |
+case OP_SorterNext: { /* jump */ |
+ VdbeCursor *pC; |
+ int res; |
+ |
+ pC = p->apCsr[pOp->p1]; |
+ assert( isSorter(pC) ); |
+ res = 0; |
+ rc = sqlite3VdbeSorterNext(db, pC, &res); |
+ goto next_tail; |
+case OP_PrevIfOpen: /* jump */ |
+case OP_NextIfOpen: /* jump */ |
+ if( p->apCsr[pOp->p1]==0 ) break; |
+ /* Fall through */ |
+case OP_Prev: /* jump */ |
+case OP_Next: /* jump */ |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ assert( pOp->p5<ArraySize(p->aCounter) ); |
+ pC = p->apCsr[pOp->p1]; |
+ res = pOp->p3; |
+ assert( pC!=0 ); |
+ assert( pC->deferredMoveto==0 ); |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ assert( res==0 || (res==1 && pC->isTable==0) ); |
+ testcase( res==1 ); |
+ assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext ); |
+ assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious ); |
+ assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext ); |
+ assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious); |
+ |
+ /* The Next opcode is only used after SeekGT, SeekGE, and Rewind. |
+ ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */ |
+ assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen |
+ || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE |
+ || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found); |
+ assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen |
+ || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE |
+ || pC->seekOp==OP_Last ); |
+ |
+ rc = pOp->p4.xAdvance(pC->uc.pCursor, &res); |
+next_tail: |
+ pC->cacheStatus = CACHE_STALE; |
+ VdbeBranchTaken(res==0,2); |
+ if( rc ) goto abort_due_to_error; |
+ if( res==0 ){ |
+ pC->nullRow = 0; |
+ p->aCounter[pOp->p5]++; |
+#ifdef SQLITE_TEST |
+ sqlite3_search_count++; |
+#endif |
+ goto jump_to_p2_and_check_for_interrupt; |
+ }else{ |
+ pC->nullRow = 1; |
+ } |
+ goto check_for_interrupt; |
+} |
+ |
+/* Opcode: IdxInsert P1 P2 P3 P4 P5 |
+** Synopsis: key=r[P2] |
+** |
+** Register P2 holds an SQL index key made using the |
+** MakeRecord instructions. This opcode writes that key |
+** into the index P1. Data for the entry is nil. |
+** |
+** If P4 is not zero, then it is the number of values in the unpacked |
+** key of reg(P2). In that case, P3 is the index of the first register |
+** for the unpacked key. The availability of the unpacked key can sometimes |
+** be an optimization. |
+** |
+** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer |
+** that this insert is likely to be an append. |
+** |
+** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is |
+** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, |
+** then the change counter is unchanged. |
+** |
+** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might |
+** run faster by avoiding an unnecessary seek on cursor P1. However, |
+** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior |
+** seeks on the cursor or if the most recent seek used a key equivalent |
+** to P2. |
+** |
+** This instruction only works for indices. The equivalent instruction |
+** for tables is OP_Insert. |
+*/ |
+/* Opcode: SorterInsert P1 P2 * * * |
+** Synopsis: key=r[P2] |
+** |
+** Register P2 holds an SQL index key made using the |
+** MakeRecord instructions. This opcode writes that key |
+** into the sorter P1. Data for the entry is nil. |
+*/ |
+case OP_SorterInsert: /* in2 */ |
+case OP_IdxInsert: { /* in2 */ |
+ VdbeCursor *pC; |
+ BtreePayload x; |
+ |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+ assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) ); |
+ pIn2 = &aMem[pOp->p2]; |
+ assert( pIn2->flags & MEM_Blob ); |
+ if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
+ assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert ); |
+ assert( pC->isTable==0 ); |
+ rc = ExpandBlob(pIn2); |
+ if( rc ) goto abort_due_to_error; |
+ if( pOp->opcode==OP_SorterInsert ){ |
+ rc = sqlite3VdbeSorterWrite(pC, pIn2); |
+ }else{ |
+ x.nKey = pIn2->n; |
+ x.pKey = pIn2->z; |
+ x.aMem = aMem + pOp->p3; |
+ x.nMem = (u16)pOp->p4.i; |
+ rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, |
+ (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), |
+ ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) |
+ ); |
+ assert( pC->deferredMoveto==0 ); |
+ pC->cacheStatus = CACHE_STALE; |
+ } |
+ if( rc) goto abort_due_to_error; |
+ break; |
+} |
+ |
+/* Opcode: IdxDelete P1 P2 P3 * * |
+** Synopsis: key=r[P2@P3] |
+** |
+** The content of P3 registers starting at register P2 form |
+** an unpacked index key. This opcode removes that entry from the |
+** index opened by cursor P1. |
+*/ |
+case OP_IdxDelete: { |
+ VdbeCursor *pC; |
+ BtCursor *pCrsr; |
+ int res; |
+ UnpackedRecord r; |
+ |
+ assert( pOp->p3>0 ); |
+ assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 ); |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ pCrsr = pC->uc.pCursor; |
+ assert( pCrsr!=0 ); |
+ assert( pOp->p5==0 ); |
+ r.pKeyInfo = pC->pKeyInfo; |
+ r.nField = (u16)pOp->p3; |
+ r.default_rc = 0; |
+ r.aMem = &aMem[pOp->p2]; |
+ rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); |
+ if( rc ) goto abort_due_to_error; |
+ if( res==0 ){ |
+ rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); |
+ if( rc ) goto abort_due_to_error; |
+ } |
+ assert( pC->deferredMoveto==0 ); |
+ pC->cacheStatus = CACHE_STALE; |
+ pC->seekResult = 0; |
+ break; |
+} |
+ |
+/* Opcode: Seek P1 * P3 P4 * |
+** Synopsis: Move P3 to P1.rowid |
+** |
+** P1 is an open index cursor and P3 is a cursor on the corresponding |
+** table. This opcode does a deferred seek of the P3 table cursor |
+** to the row that corresponds to the current row of P1. |
+** |
+** This is a deferred seek. Nothing actually happens until |
+** the cursor is used to read a record. That way, if no reads |
+** occur, no unnecessary I/O happens. |
+** |
+** P4 may be an array of integers (type P4_INTARRAY) containing |
+** one entry for each column in the P3 table. If array entry a(i) |
+** is non-zero, then reading column a(i)-1 from cursor P3 is |
+** equivalent to performing the deferred seek and then reading column i |
+** from P1. This information is stored in P3 and used to redirect |
+** reads against P3 over to P1, thus possibly avoiding the need to |
+** seek and read cursor P3. |
+*/ |
+/* Opcode: IdxRowid P1 P2 * * * |
+** Synopsis: r[P2]=rowid |
+** |
+** Write into register P2 an integer which is the last entry in the record at |
+** the end of the index key pointed to by cursor P1. This integer should be |
+** the rowid of the table entry to which this index entry points. |
+** |
+** See also: Rowid, MakeRecord. |
+*/ |
+case OP_Seek: |
+case OP_IdxRowid: { /* out2 */ |
+ VdbeCursor *pC; /* The P1 index cursor */ |
+ VdbeCursor *pTabCur; /* The P2 table cursor (OP_Seek only) */ |
+ i64 rowid; /* Rowid that P1 current points to */ |
+ |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ assert( pC->uc.pCursor!=0 ); |
+ assert( pC->isTable==0 ); |
+ assert( pC->deferredMoveto==0 ); |
+ assert( !pC->nullRow || pOp->opcode==OP_IdxRowid ); |
+ |
+ /* The IdxRowid and Seek opcodes are combined because of the commonality |
+ ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */ |
+ rc = sqlite3VdbeCursorRestore(pC); |
+ |
+ /* sqlite3VbeCursorRestore() can only fail if the record has been deleted |
+ ** out from under the cursor. That will never happens for an IdxRowid |
+ ** or Seek opcode */ |
+ if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
+ |
+ if( !pC->nullRow ){ |
+ rowid = 0; /* Not needed. Only used to silence a warning. */ |
+ rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid); |
+ if( rc!=SQLITE_OK ){ |
+ goto abort_due_to_error; |
+ } |
+ if( pOp->opcode==OP_Seek ){ |
+ assert( pOp->p3>=0 && pOp->p3<p->nCursor ); |
+ pTabCur = p->apCsr[pOp->p3]; |
+ assert( pTabCur!=0 ); |
+ assert( pTabCur->eCurType==CURTYPE_BTREE ); |
+ assert( pTabCur->uc.pCursor!=0 ); |
+ assert( pTabCur->isTable ); |
+ pTabCur->nullRow = 0; |
+ pTabCur->movetoTarget = rowid; |
+ pTabCur->deferredMoveto = 1; |
+ assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 ); |
+ pTabCur->aAltMap = pOp->p4.ai; |
+ pTabCur->pAltCursor = pC; |
+ }else{ |
+ pOut = out2Prerelease(p, pOp); |
+ pOut->u.i = rowid; |
+ } |
+ }else{ |
+ assert( pOp->opcode==OP_IdxRowid ); |
+ sqlite3VdbeMemSetNull(&aMem[pOp->p2]); |
+ } |
+ break; |
+} |
+ |
+/* Opcode: IdxGE P1 P2 P3 P4 P5 |
+** Synopsis: key=r[P3@P4] |
+** |
+** The P4 register values beginning with P3 form an unpacked index |
+** key that omits the PRIMARY KEY. Compare this key value against the index |
+** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID |
+** fields at the end. |
+** |
+** If the P1 index entry is greater than or equal to the key value |
+** then jump to P2. Otherwise fall through to the next instruction. |
+*/ |
+/* Opcode: IdxGT P1 P2 P3 P4 P5 |
+** Synopsis: key=r[P3@P4] |
+** |
+** The P4 register values beginning with P3 form an unpacked index |
+** key that omits the PRIMARY KEY. Compare this key value against the index |
+** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID |
+** fields at the end. |
+** |
+** If the P1 index entry is greater than the key value |
+** then jump to P2. Otherwise fall through to the next instruction. |
+*/ |
+/* Opcode: IdxLT P1 P2 P3 P4 P5 |
+** Synopsis: key=r[P3@P4] |
+** |
+** The P4 register values beginning with P3 form an unpacked index |
+** key that omits the PRIMARY KEY or ROWID. Compare this key value against |
+** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or |
+** ROWID on the P1 index. |
+** |
+** If the P1 index entry is less than the key value then jump to P2. |
+** Otherwise fall through to the next instruction. |
+*/ |
+/* Opcode: IdxLE P1 P2 P3 P4 P5 |
+** Synopsis: key=r[P3@P4] |
+** |
+** The P4 register values beginning with P3 form an unpacked index |
+** key that omits the PRIMARY KEY or ROWID. Compare this key value against |
+** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or |
+** ROWID on the P1 index. |
+** |
+** If the P1 index entry is less than or equal to the key value then jump |
+** to P2. Otherwise fall through to the next instruction. |
+*/ |
+case OP_IdxLE: /* jump */ |
+case OP_IdxGT: /* jump */ |
+case OP_IdxLT: /* jump */ |
+case OP_IdxGE: { /* jump */ |
+ VdbeCursor *pC; |
+ int res; |
+ UnpackedRecord r; |
+ |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+ assert( pC->isOrdered ); |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ assert( pC->uc.pCursor!=0); |
+ assert( pC->deferredMoveto==0 ); |
+ assert( pOp->p5==0 || pOp->p5==1 ); |
+ assert( pOp->p4type==P4_INT32 ); |
+ r.pKeyInfo = pC->pKeyInfo; |
+ r.nField = (u16)pOp->p4.i; |
+ if( pOp->opcode<OP_IdxLT ){ |
+ assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); |
+ r.default_rc = -1; |
+ }else{ |
+ assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); |
+ r.default_rc = 0; |
+ } |
+ r.aMem = &aMem[pOp->p3]; |
+#ifdef SQLITE_DEBUG |
+ { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
+#endif |
+ res = 0; /* Not needed. Only used to silence a warning. */ |
+ rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); |
+ assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); |
+ if( (pOp->opcode&1)==(OP_IdxLT&1) ){ |
+ assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); |
+ res = -res; |
+ }else{ |
+ assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); |
+ res++; |
+ } |
+ VdbeBranchTaken(res>0,2); |
+ if( rc ) goto abort_due_to_error; |
+ if( res>0 ) goto jump_to_p2; |
+ break; |
+} |
+ |
+/* Opcode: Destroy P1 P2 P3 * * |
+** |
+** Delete an entire database table or index whose root page in the database |
+** file is given by P1. |
+** |
+** The table being destroyed is in the main database file if P3==0. If |
+** P3==1 then the table to be clear is in the auxiliary database file |
+** that is used to store tables create using CREATE TEMPORARY TABLE. |
+** |
+** If AUTOVACUUM is enabled then it is possible that another root page |
+** might be moved into the newly deleted root page in order to keep all |
+** root pages contiguous at the beginning of the database. The former |
+** value of the root page that moved - its value before the move occurred - |
+** is stored in register P2. If no page |
+** movement was required (because the table being dropped was already |
+** the last one in the database) then a zero is stored in register P2. |
+** If AUTOVACUUM is disabled then a zero is stored in register P2. |
+** |
+** See also: Clear |
+*/ |
+case OP_Destroy: { /* out2 */ |
+ int iMoved; |
+ int iDb; |
+ |
+ assert( p->readOnly==0 ); |
+ assert( pOp->p1>1 ); |
+ pOut = out2Prerelease(p, pOp); |
+ pOut->flags = MEM_Null; |
+ if( db->nVdbeRead > db->nVDestroy+1 ){ |
+ rc = SQLITE_LOCKED; |
+ p->errorAction = OE_Abort; |
+ goto abort_due_to_error; |
+ }else{ |
+ iDb = pOp->p3; |
+ assert( DbMaskTest(p->btreeMask, iDb) ); |
+ iMoved = 0; /* Not needed. Only to silence a warning. */ |
+ rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); |
+ pOut->flags = MEM_Int; |
+ pOut->u.i = iMoved; |
+ if( rc ) goto abort_due_to_error; |
+#ifndef SQLITE_OMIT_AUTOVACUUM |
+ if( iMoved!=0 ){ |
+ sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); |
+ /* All OP_Destroy operations occur on the same btree */ |
+ assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); |
+ resetSchemaOnFault = iDb+1; |
+ } |
+#endif |
+ } |
+ break; |
+} |
+ |
+/* Opcode: Clear P1 P2 P3 |
+** |
+** Delete all contents of the database table or index whose root page |
+** in the database file is given by P1. But, unlike Destroy, do not |
+** remove the table or index from the database file. |
+** |
+** The table being clear is in the main database file if P2==0. If |
+** P2==1 then the table to be clear is in the auxiliary database file |
+** that is used to store tables create using CREATE TEMPORARY TABLE. |
+** |
+** If the P3 value is non-zero, then the table referred to must be an |
+** intkey table (an SQL table, not an index). In this case the row change |
+** count is incremented by the number of rows in the table being cleared. |
+** If P3 is greater than zero, then the value stored in register P3 is |
+** also incremented by the number of rows in the table being cleared. |
+** |
+** See also: Destroy |
+*/ |
+case OP_Clear: { |
+ int nChange; |
+ |
+ nChange = 0; |
+ assert( p->readOnly==0 ); |
+ assert( DbMaskTest(p->btreeMask, pOp->p2) ); |
+ rc = sqlite3BtreeClearTable( |
+ db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) |
+ ); |
+ if( pOp->p3 ){ |
+ p->nChange += nChange; |
+ if( pOp->p3>0 ){ |
+ assert( memIsValid(&aMem[pOp->p3]) ); |
+ memAboutToChange(p, &aMem[pOp->p3]); |
+ aMem[pOp->p3].u.i += nChange; |
+ } |
+ } |
+ if( rc ) goto abort_due_to_error; |
+ break; |
+} |
+ |
+/* Opcode: ResetSorter P1 * * * * |
+** |
+** Delete all contents from the ephemeral table or sorter |
+** that is open on cursor P1. |
+** |
+** This opcode only works for cursors used for sorting and |
+** opened with OP_OpenEphemeral or OP_SorterOpen. |
+*/ |
+case OP_ResetSorter: { |
+ VdbeCursor *pC; |
+ |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ pC = p->apCsr[pOp->p1]; |
+ assert( pC!=0 ); |
+ if( isSorter(pC) ){ |
+ sqlite3VdbeSorterReset(db, pC->uc.pSorter); |
+ }else{ |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ assert( pC->isEphemeral ); |
+ rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor); |
+ if( rc ) goto abort_due_to_error; |
+ } |
+ break; |
+} |
+ |
+/* Opcode: CreateTable P1 P2 * * * |
+** Synopsis: r[P2]=root iDb=P1 |
+** |
+** Allocate a new table in the main database file if P1==0 or in the |
+** auxiliary database file if P1==1 or in an attached database if |
+** P1>1. Write the root page number of the new table into |
+** register P2 |
+** |
+** The difference between a table and an index is this: A table must |
+** have a 4-byte integer key and can have arbitrary data. An index |
+** has an arbitrary key but no data. |
+** |
+** See also: CreateIndex |
+*/ |
+/* Opcode: CreateIndex P1 P2 * * * |
+** Synopsis: r[P2]=root iDb=P1 |
+** |
+** Allocate a new index in the main database file if P1==0 or in the |
+** auxiliary database file if P1==1 or in an attached database if |
+** P1>1. Write the root page number of the new table into |
+** register P2. |
+** |
+** See documentation on OP_CreateTable for additional information. |
+*/ |
+case OP_CreateIndex: /* out2 */ |
+case OP_CreateTable: { /* out2 */ |
+ int pgno; |
+ int flags; |
+ Db *pDb; |
+ |
+ pOut = out2Prerelease(p, pOp); |
+ pgno = 0; |
+ 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 ); |
+ if( pOp->opcode==OP_CreateTable ){ |
+ /* flags = BTREE_INTKEY; */ |
+ flags = BTREE_INTKEY; |
+ }else{ |
+ flags = BTREE_BLOBKEY; |
+ } |
+ rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); |
+ if( rc ) goto abort_due_to_error; |
+ pOut->u.i = pgno; |
+ break; |
+} |
+ |
+/* Opcode: ParseSchema P1 * * P4 * |
+** |
+** Read and parse all entries from the SQLITE_MASTER table of database P1 |
+** that match the WHERE clause P4. |
+** |
+** This opcode invokes the parser to create a new virtual machine, |
+** then runs the new virtual machine. It is thus a re-entrant opcode. |
+*/ |
+case OP_ParseSchema: { |
+ int iDb; |
+ const char *zMaster; |
+ char *zSql; |
+ InitData initData; |
+ |
+ /* Any prepared statement that invokes this opcode will hold mutexes |
+ ** on every btree. This is a prerequisite for invoking |
+ ** sqlite3InitCallback(). |
+ */ |
+#ifdef SQLITE_DEBUG |
+ for(iDb=0; iDb<db->nDb; iDb++){ |
+ assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
+ } |
+#endif |
+ |
+ iDb = pOp->p1; |
+ assert( iDb>=0 && iDb<db->nDb ); |
+ assert( DbHasProperty(db, iDb, DB_SchemaLoaded) ); |
+ /* Used to be a conditional */ { |
+ zMaster = MASTER_NAME; |
+ initData.db = db; |
+ initData.iDb = pOp->p1; |
+ initData.pzErrMsg = &p->zErrMsg; |
+ zSql = sqlite3MPrintf(db, |
+ "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid", |
+ db->aDb[iDb].zDbSName, zMaster, pOp->p4.z); |
+ if( zSql==0 ){ |
+ rc = SQLITE_NOMEM_BKPT; |
+ }else{ |
+ assert( db->init.busy==0 ); |
+ db->init.busy = 1; |
+ initData.rc = SQLITE_OK; |
+ assert( !db->mallocFailed ); |
+ rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
+ if( rc==SQLITE_OK ) rc = initData.rc; |
+ sqlite3DbFree(db, zSql); |
+ db->init.busy = 0; |
+ } |
+ } |
+ if( rc ){ |
+ sqlite3ResetAllSchemasOfConnection(db); |
+ if( rc==SQLITE_NOMEM ){ |
+ goto no_mem; |
+ } |
+ goto abort_due_to_error; |
+ } |
+ break; |
+} |
+ |
+#if !defined(SQLITE_OMIT_ANALYZE) |
+/* Opcode: LoadAnalysis P1 * * * * |
+** |
+** Read the sqlite_stat1 table for database P1 and load the content |
+** of that table into the internal index hash table. This will cause |
+** the analysis to be used when preparing all subsequent queries. |
+*/ |
+case OP_LoadAnalysis: { |
+ assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
+ rc = sqlite3AnalysisLoad(db, pOp->p1); |
+ if( rc ) goto abort_due_to_error; |
+ break; |
+} |
+#endif /* !defined(SQLITE_OMIT_ANALYZE) */ |
+ |
+/* Opcode: DropTable P1 * * P4 * |
+** |
+** Remove the internal (in-memory) data structures that describe |
+** the table named P4 in database P1. This is called after a table |
+** is dropped from disk (using the Destroy opcode) in order to keep |
+** the internal representation of the |
+** schema consistent with what is on disk. |
+*/ |
+case OP_DropTable: { |
+ sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); |
+ break; |
+} |
+ |
+/* Opcode: DropIndex P1 * * P4 * |
+** |
+** Remove the internal (in-memory) data structures that describe |
+** the index named P4 in database P1. This is called after an index |
+** is dropped from disk (using the Destroy opcode) |
+** in order to keep the internal representation of the |
+** schema consistent with what is on disk. |
+*/ |
+case OP_DropIndex: { |
+ sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); |
+ break; |
+} |
+ |
+/* Opcode: DropTrigger P1 * * P4 * |
+** |
+** Remove the internal (in-memory) data structures that describe |
+** the trigger named P4 in database P1. This is called after a trigger |
+** is dropped from disk (using the Destroy opcode) in order to keep |
+** the internal representation of the |
+** schema consistent with what is on disk. |
+*/ |
+case OP_DropTrigger: { |
+ sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); |
+ break; |
+} |
+ |
+ |
+#ifndef SQLITE_OMIT_INTEGRITY_CHECK |
+/* Opcode: IntegrityCk P1 P2 P3 P4 P5 |
+** |
+** Do an analysis of the currently open database. Store in |
+** register P1 the text of an error message describing any problems. |
+** If no problems are found, store a NULL in register P1. |
+** |
+** The register P3 contains the maximum number of allowed errors. |
+** At most reg(P3) errors will be reported. |
+** In other words, the analysis stops as soon as reg(P1) errors are |
+** seen. Reg(P1) is updated with the number of errors remaining. |
+** |
+** The root page numbers of all tables in the database are integers |
+** stored in P4_INTARRAY argument. |
+** |
+** If P5 is not zero, the check is done on the auxiliary database |
+** file, not the main database file. |
+** |
+** This opcode is used to implement the integrity_check pragma. |
+*/ |
+case OP_IntegrityCk: { |
+ int nRoot; /* Number of tables to check. (Number of root pages.) */ |
+ int *aRoot; /* Array of rootpage numbers for tables to be checked */ |
+ int nErr; /* Number of errors reported */ |
+ char *z; /* Text of the error report */ |
+ Mem *pnErr; /* Register keeping track of errors remaining */ |
+ |
+ assert( p->bIsReader ); |
+ nRoot = pOp->p2; |
+ aRoot = pOp->p4.ai; |
+ assert( nRoot>0 ); |
+ assert( aRoot[nRoot]==0 ); |
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
+ pnErr = &aMem[pOp->p3]; |
+ assert( (pnErr->flags & MEM_Int)!=0 ); |
+ assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); |
+ pIn1 = &aMem[pOp->p1]; |
+ assert( pOp->p5<db->nDb ); |
+ assert( DbMaskTest(p->btreeMask, pOp->p5) ); |
+ z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, |
+ (int)pnErr->u.i, &nErr); |
+ pnErr->u.i -= nErr; |
+ sqlite3VdbeMemSetNull(pIn1); |
+ if( nErr==0 ){ |
+ assert( z==0 ); |
+ }else if( z==0 ){ |
+ goto no_mem; |
+ }else{ |
+ sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); |
+ } |
+ UPDATE_MAX_BLOBSIZE(pIn1); |
+ sqlite3VdbeChangeEncoding(pIn1, encoding); |
+ break; |
+} |
+#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
+ |
+/* Opcode: RowSetAdd P1 P2 * * * |
+** Synopsis: rowset(P1)=r[P2] |
+** |
+** Insert the integer value held by register P2 into a boolean index |
+** held in register P1. |
+** |
+** An assertion fails if P2 is not an integer. |
+*/ |
+case OP_RowSetAdd: { /* in1, in2 */ |
+ pIn1 = &aMem[pOp->p1]; |
+ pIn2 = &aMem[pOp->p2]; |
+ assert( (pIn2->flags & MEM_Int)!=0 ); |
+ if( (pIn1->flags & MEM_RowSet)==0 ){ |
+ sqlite3VdbeMemSetRowSet(pIn1); |
+ if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; |
+ } |
+ sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i); |
+ break; |
+} |
+ |
+/* Opcode: RowSetRead P1 P2 P3 * * |
+** Synopsis: r[P3]=rowset(P1) |
+** |
+** Extract the smallest value from boolean index P1 and put that value into |
+** register P3. Or, if boolean index P1 is initially empty, leave P3 |
+** unchanged and jump to instruction P2. |
+*/ |
+case OP_RowSetRead: { /* jump, in1, out3 */ |
+ i64 val; |
+ |
+ pIn1 = &aMem[pOp->p1]; |
+ if( (pIn1->flags & MEM_RowSet)==0 |
+ || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0 |
+ ){ |
+ /* The boolean index is empty */ |
+ sqlite3VdbeMemSetNull(pIn1); |
+ VdbeBranchTaken(1,2); |
+ goto jump_to_p2_and_check_for_interrupt; |
+ }else{ |
+ /* A value was pulled from the index */ |
+ VdbeBranchTaken(0,2); |
+ sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); |
+ } |
+ goto check_for_interrupt; |
+} |
+ |
+/* Opcode: RowSetTest P1 P2 P3 P4 |
+** Synopsis: if r[P3] in rowset(P1) goto P2 |
+** |
+** Register P3 is assumed to hold a 64-bit integer value. If register P1 |
+** contains a RowSet object and that RowSet object contains |
+** the value held in P3, jump to register P2. Otherwise, insert the |
+** integer in P3 into the RowSet and continue on to the |
+** next opcode. |
+** |
+** The RowSet object is optimized for the case where successive sets |
+** of integers, where each set contains no duplicates. Each set |
+** of values is identified by a unique P4 value. The first set |
+** must have P4==0, the final set P4=-1. P4 must be either -1 or |
+** non-negative. For non-negative values of P4 only the lower 4 |
+** bits are significant. |
+** |
+** This allows optimizations: (a) when P4==0 there is no need to test |
+** the rowset object for P3, as it is guaranteed not to contain it, |
+** (b) when P4==-1 there is no need to insert the value, as it will |
+** never be tested for, and (c) when a value that is part of set X is |
+** inserted, there is no need to search to see if the same value was |
+** previously inserted as part of set X (only if it was previously |
+** inserted as part of some other set). |
+*/ |
+case OP_RowSetTest: { /* jump, in1, in3 */ |
+ int iSet; |
+ int exists; |
+ |
+ pIn1 = &aMem[pOp->p1]; |
+ pIn3 = &aMem[pOp->p3]; |
+ iSet = pOp->p4.i; |
+ assert( pIn3->flags&MEM_Int ); |
+ |
+ /* If there is anything other than a rowset object in memory cell P1, |
+ ** delete it now and initialize P1 with an empty rowset |
+ */ |
+ if( (pIn1->flags & MEM_RowSet)==0 ){ |
+ sqlite3VdbeMemSetRowSet(pIn1); |
+ if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; |
+ } |
+ |
+ assert( pOp->p4type==P4_INT32 ); |
+ assert( iSet==-1 || iSet>=0 ); |
+ if( iSet ){ |
+ exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i); |
+ VdbeBranchTaken(exists!=0,2); |
+ if( exists ) goto jump_to_p2; |
+ } |
+ if( iSet>=0 ){ |
+ sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i); |
+ } |
+ break; |
+} |
+ |
+ |
+#ifndef SQLITE_OMIT_TRIGGER |
+ |
+/* Opcode: Program P1 P2 P3 P4 P5 |
+** |
+** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). |
+** |
+** P1 contains the address of the memory cell that contains the first memory |
+** cell in an array of values used as arguments to the sub-program. P2 |
+** contains the address to jump to if the sub-program throws an IGNORE |
+** exception using the RAISE() function. Register P3 contains the address |
+** of a memory cell in this (the parent) VM that is used to allocate the |
+** memory required by the sub-vdbe at runtime. |
+** |
+** P4 is a pointer to the VM containing the trigger program. |
+** |
+** If P5 is non-zero, then recursive program invocation is enabled. |
+*/ |
+case OP_Program: { /* jump */ |
+ int nMem; /* Number of memory registers for sub-program */ |
+ int nByte; /* Bytes of runtime space required for sub-program */ |
+ Mem *pRt; /* Register to allocate runtime space */ |
+ Mem *pMem; /* Used to iterate through memory cells */ |
+ Mem *pEnd; /* Last memory cell in new array */ |
+ VdbeFrame *pFrame; /* New vdbe frame to execute in */ |
+ SubProgram *pProgram; /* Sub-program to execute */ |
+ void *t; /* Token identifying trigger */ |
+ |
+ pProgram = pOp->p4.pProgram; |
+ pRt = &aMem[pOp->p3]; |
+ assert( pProgram->nOp>0 ); |
+ |
+ /* If the p5 flag is clear, then recursive invocation of triggers is |
+ ** disabled for backwards compatibility (p5 is set if this sub-program |
+ ** is really a trigger, not a foreign key action, and the flag set |
+ ** and cleared by the "PRAGMA recursive_triggers" command is clear). |
+ ** |
+ ** It is recursive invocation of triggers, at the SQL level, that is |
+ ** disabled. In some cases a single trigger may generate more than one |
+ ** SubProgram (if the trigger may be executed with more than one different |
+ ** ON CONFLICT algorithm). SubProgram structures associated with a |
+ ** single trigger all have the same value for the SubProgram.token |
+ ** variable. */ |
+ if( pOp->p5 ){ |
+ t = pProgram->token; |
+ for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); |
+ if( pFrame ) break; |
+ } |
+ |
+ if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ |
+ rc = SQLITE_ERROR; |
+ sqlite3VdbeError(p, "too many levels of trigger recursion"); |
+ goto abort_due_to_error; |
+ } |
+ |
+ /* Register pRt is used to store the memory required to save the state |
+ ** of the current program, and the memory required at runtime to execute |
+ ** the trigger program. If this trigger has been fired before, then pRt |
+ ** is already allocated. Otherwise, it must be initialized. */ |
+ if( (pRt->flags&MEM_Frame)==0 ){ |
+ /* SubProgram.nMem is set to the number of memory cells used by the |
+ ** program stored in SubProgram.aOp. As well as these, one memory |
+ ** cell is required for each cursor used by the program. Set local |
+ ** variable nMem (and later, VdbeFrame.nChildMem) to this value. |
+ */ |
+ nMem = pProgram->nMem + pProgram->nCsr; |
+ assert( nMem>0 ); |
+ if( pProgram->nCsr==0 ) nMem++; |
+ nByte = ROUND8(sizeof(VdbeFrame)) |
+ + nMem * sizeof(Mem) |
+ + pProgram->nCsr * sizeof(VdbeCursor *); |
+ pFrame = sqlite3DbMallocZero(db, nByte); |
+ if( !pFrame ){ |
+ goto no_mem; |
+ } |
+ sqlite3VdbeMemRelease(pRt); |
+ pRt->flags = MEM_Frame; |
+ pRt->u.pFrame = pFrame; |
+ |
+ pFrame->v = p; |
+ pFrame->nChildMem = nMem; |
+ pFrame->nChildCsr = pProgram->nCsr; |
+ pFrame->pc = (int)(pOp - aOp); |
+ pFrame->aMem = p->aMem; |
+ pFrame->nMem = p->nMem; |
+ pFrame->apCsr = p->apCsr; |
+ pFrame->nCursor = p->nCursor; |
+ pFrame->aOp = p->aOp; |
+ pFrame->nOp = p->nOp; |
+ pFrame->token = pProgram->token; |
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
+ pFrame->anExec = p->anExec; |
+#endif |
+ |
+ pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; |
+ for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ |
+ pMem->flags = MEM_Undefined; |
+ pMem->db = db; |
+ } |
+ }else{ |
+ pFrame = pRt->u.pFrame; |
+ assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem |
+ || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) ); |
+ assert( pProgram->nCsr==pFrame->nChildCsr ); |
+ assert( (int)(pOp - aOp)==pFrame->pc ); |
+ } |
+ |
+ p->nFrame++; |
+ pFrame->pParent = p->pFrame; |
+ pFrame->lastRowid = db->lastRowid; |
+ pFrame->nChange = p->nChange; |
+ pFrame->nDbChange = p->db->nChange; |
+ assert( pFrame->pAuxData==0 ); |
+ pFrame->pAuxData = p->pAuxData; |
+ p->pAuxData = 0; |
+ p->nChange = 0; |
+ p->pFrame = pFrame; |
+ p->aMem = aMem = VdbeFrameMem(pFrame); |
+ p->nMem = pFrame->nChildMem; |
+ p->nCursor = (u16)pFrame->nChildCsr; |
+ p->apCsr = (VdbeCursor **)&aMem[p->nMem]; |
+ p->aOp = aOp = pProgram->aOp; |
+ p->nOp = pProgram->nOp; |
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
+ p->anExec = 0; |
+#endif |
+ pOp = &aOp[-1]; |
+ |
+ break; |
+} |
+ |
+/* Opcode: Param P1 P2 * * * |
+** |
+** This opcode is only ever present in sub-programs called via the |
+** OP_Program instruction. Copy a value currently stored in a memory |
+** cell of the calling (parent) frame to cell P2 in the current frames |
+** address space. This is used by trigger programs to access the new.* |
+** and old.* values. |
+** |
+** The address of the cell in the parent frame is determined by adding |
+** the value of the P1 argument to the value of the P1 argument to the |
+** calling OP_Program instruction. |
+*/ |
+case OP_Param: { /* out2 */ |
+ VdbeFrame *pFrame; |
+ Mem *pIn; |
+ pOut = out2Prerelease(p, pOp); |
+ pFrame = p->pFrame; |
+ pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; |
+ sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); |
+ break; |
+} |
+ |
+#endif /* #ifndef SQLITE_OMIT_TRIGGER */ |
+ |
+#ifndef SQLITE_OMIT_FOREIGN_KEY |
+/* Opcode: FkCounter P1 P2 * * * |
+** Synopsis: fkctr[P1]+=P2 |
+** |
+** Increment a "constraint counter" by P2 (P2 may be negative or positive). |
+** If P1 is non-zero, the database constraint counter is incremented |
+** (deferred foreign key constraints). Otherwise, if P1 is zero, the |
+** statement counter is incremented (immediate foreign key constraints). |
+*/ |
+case OP_FkCounter: { |
+ if( db->flags & SQLITE_DeferFKs ){ |
+ db->nDeferredImmCons += pOp->p2; |
+ }else if( pOp->p1 ){ |
+ db->nDeferredCons += pOp->p2; |
+ }else{ |
+ p->nFkConstraint += pOp->p2; |
+ } |
+ break; |
+} |
+ |
+/* Opcode: FkIfZero P1 P2 * * * |
+** Synopsis: if fkctr[P1]==0 goto P2 |
+** |
+** This opcode tests if a foreign key constraint-counter is currently zero. |
+** If so, jump to instruction P2. Otherwise, fall through to the next |
+** instruction. |
+** |
+** If P1 is non-zero, then the jump is taken if the database constraint-counter |
+** is zero (the one that counts deferred constraint violations). If P1 is |
+** zero, the jump is taken if the statement constraint-counter is zero |
+** (immediate foreign key constraint violations). |
+*/ |
+case OP_FkIfZero: { /* jump */ |
+ if( pOp->p1 ){ |
+ VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2); |
+ if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
+ }else{ |
+ VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2); |
+ if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
+ } |
+ break; |
+} |
+#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ |
+ |
+#ifndef SQLITE_OMIT_AUTOINCREMENT |
+/* Opcode: MemMax P1 P2 * * * |
+** Synopsis: r[P1]=max(r[P1],r[P2]) |
+** |
+** P1 is a register in the root frame of this VM (the root frame is |
+** different from the current frame if this instruction is being executed |
+** within a sub-program). Set the value of register P1 to the maximum of |
+** its current value and the value in register P2. |
+** |
+** This instruction throws an error if the memory cell is not initially |
+** an integer. |
+*/ |
+case OP_MemMax: { /* in2 */ |
+ VdbeFrame *pFrame; |
+ if( p->pFrame ){ |
+ for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
+ pIn1 = &pFrame->aMem[pOp->p1]; |
+ }else{ |
+ pIn1 = &aMem[pOp->p1]; |
+ } |
+ assert( memIsValid(pIn1) ); |
+ sqlite3VdbeMemIntegerify(pIn1); |
+ pIn2 = &aMem[pOp->p2]; |
+ sqlite3VdbeMemIntegerify(pIn2); |
+ if( pIn1->u.i<pIn2->u.i){ |
+ pIn1->u.i = pIn2->u.i; |
+ } |
+ break; |
+} |
+#endif /* SQLITE_OMIT_AUTOINCREMENT */ |
+ |
+/* Opcode: IfPos P1 P2 P3 * * |
+** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 |
+** |
+** Register P1 must contain an integer. |
+** If the value of register P1 is 1 or greater, subtract P3 from the |
+** value in P1 and jump to P2. |
+** |
+** If the initial value of register P1 is less than 1, then the |
+** value is unchanged and control passes through to the next instruction. |
+*/ |
+case OP_IfPos: { /* jump, in1 */ |
+ pIn1 = &aMem[pOp->p1]; |
+ assert( pIn1->flags&MEM_Int ); |
+ VdbeBranchTaken( pIn1->u.i>0, 2); |
+ if( pIn1->u.i>0 ){ |
+ pIn1->u.i -= pOp->p3; |
+ goto jump_to_p2; |
+ } |
+ break; |
+} |
+ |
+/* Opcode: OffsetLimit P1 P2 P3 * * |
+** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) |
+** |
+** This opcode performs a commonly used computation associated with |
+** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3] |
+** holds the offset counter. The opcode computes the combined value |
+** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] |
+** value computed is the total number of rows that will need to be |
+** visited in order to complete the query. |
+** |
+** If r[P3] is zero or negative, that means there is no OFFSET |
+** and r[P2] is set to be the value of the LIMIT, r[P1]. |
+** |
+** if r[P1] is zero or negative, that means there is no LIMIT |
+** and r[P2] is set to -1. |
+** |
+** Otherwise, r[P2] is set to the sum of r[P1] and r[P3]. |
+*/ |
+case OP_OffsetLimit: { /* in1, out2, in3 */ |
+ i64 x; |
+ pIn1 = &aMem[pOp->p1]; |
+ pIn3 = &aMem[pOp->p3]; |
+ pOut = out2Prerelease(p, pOp); |
+ assert( pIn1->flags & MEM_Int ); |
+ assert( pIn3->flags & MEM_Int ); |
+ x = pIn1->u.i; |
+ if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){ |
+ /* If the LIMIT is less than or equal to zero, loop forever. This |
+ ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then |
+ ** also loop forever. This is undocumented. In fact, one could argue |
+ ** that the loop should terminate. But assuming 1 billion iterations |
+ ** per second (far exceeding the capabilities of any current hardware) |
+ ** it would take nearly 300 years to actually reach the limit. So |
+ ** looping forever is a reasonable approximation. */ |
+ pOut->u.i = -1; |
+ }else{ |
+ pOut->u.i = x; |
+ } |
+ break; |
+} |
+ |
+/* Opcode: IfNotZero P1 P2 * * * |
+** Synopsis: if r[P1]!=0 then r[P1]--, goto P2 |
+** |
+** Register P1 must contain an integer. If the content of register P1 is |
+** initially greater than zero, then decrement the value in register P1. |
+** If it is non-zero (negative or positive) and then also jump to P2. |
+** If register P1 is initially zero, leave it unchanged and fall through. |
+*/ |
+case OP_IfNotZero: { /* jump, in1 */ |
+ pIn1 = &aMem[pOp->p1]; |
+ assert( pIn1->flags&MEM_Int ); |
+ VdbeBranchTaken(pIn1->u.i<0, 2); |
+ if( pIn1->u.i ){ |
+ if( pIn1->u.i>0 ) pIn1->u.i--; |
+ goto jump_to_p2; |
+ } |
+ break; |
+} |
+ |
+/* Opcode: DecrJumpZero P1 P2 * * * |
+** Synopsis: if (--r[P1])==0 goto P2 |
+** |
+** Register P1 must hold an integer. Decrement the value in P1 |
+** and jump to P2 if the new value is exactly zero. |
+*/ |
+case OP_DecrJumpZero: { /* jump, in1 */ |
+ pIn1 = &aMem[pOp->p1]; |
+ assert( pIn1->flags&MEM_Int ); |
+ if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--; |
+ VdbeBranchTaken(pIn1->u.i==0, 2); |
+ if( pIn1->u.i==0 ) goto jump_to_p2; |
+ break; |
+} |
+ |
+ |
+/* Opcode: AggStep0 * P2 P3 P4 P5 |
+** Synopsis: accum=r[P3] step(r[P2@P5]) |
+** |
+** Execute the step function for an aggregate. The |
+** function has P5 arguments. P4 is a pointer to the FuncDef |
+** structure that specifies the function. Register P3 is the |
+** accumulator. |
+** |
+** The P5 arguments are taken from register P2 and its |
+** successors. |
+*/ |
+/* Opcode: AggStep * P2 P3 P4 P5 |
+** Synopsis: accum=r[P3] step(r[P2@P5]) |
+** |
+** Execute the step function for an aggregate. The |
+** function has P5 arguments. P4 is a pointer to an sqlite3_context |
+** object that is used to run the function. Register P3 is |
+** as the accumulator. |
+** |
+** The P5 arguments are taken from register P2 and its |
+** successors. |
+** |
+** This opcode is initially coded as OP_AggStep0. On first evaluation, |
+** the FuncDef stored in P4 is converted into an sqlite3_context and |
+** the opcode is changed. In this way, the initialization of the |
+** sqlite3_context only happens once, instead of on each call to the |
+** step function. |
+*/ |
+case OP_AggStep0: { |
+ int n; |
+ sqlite3_context *pCtx; |
+ |
+ assert( pOp->p4type==P4_FUNCDEF ); |
+ n = pOp->p5; |
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
+ assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) ); |
+ assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
+ pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); |
+ if( pCtx==0 ) goto no_mem; |
+ pCtx->pMem = 0; |
+ pCtx->pFunc = pOp->p4.pFunc; |
+ pCtx->iOp = (int)(pOp - aOp); |
+ pCtx->pVdbe = p; |
+ pCtx->argc = n; |
+ pOp->p4type = P4_FUNCCTX; |
+ pOp->p4.pCtx = pCtx; |
+ pOp->opcode = OP_AggStep; |
+ /* Fall through into OP_AggStep */ |
+} |
+case OP_AggStep: { |
+ int i; |
+ sqlite3_context *pCtx; |
+ Mem *pMem; |
+ Mem t; |
+ |
+ assert( pOp->p4type==P4_FUNCCTX ); |
+ pCtx = pOp->p4.pCtx; |
+ pMem = &aMem[pOp->p3]; |
+ |
+ /* If this function is inside of a trigger, the register array in aMem[] |
+ ** might change from one evaluation to the next. The next block of code |
+ ** checks to see if the register array has changed, and if so it |
+ ** reinitializes the relavant parts of the sqlite3_context object */ |
+ if( pCtx->pMem != pMem ){ |
+ pCtx->pMem = pMem; |
+ for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
+ } |
+ |
+#ifdef SQLITE_DEBUG |
+ for(i=0; i<pCtx->argc; i++){ |
+ assert( memIsValid(pCtx->argv[i]) ); |
+ REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
+ } |
+#endif |
+ |
+ pMem->n++; |
+ sqlite3VdbeMemInit(&t, db, MEM_Null); |
+ pCtx->pOut = &t; |
+ pCtx->fErrorOrAux = 0; |
+ pCtx->skipFlag = 0; |
+ (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ |
+ if( pCtx->fErrorOrAux ){ |
+ if( pCtx->isError ){ |
+ sqlite3VdbeError(p, "%s", sqlite3_value_text(&t)); |
+ rc = pCtx->isError; |
+ } |
+ sqlite3VdbeMemRelease(&t); |
+ if( rc ) goto abort_due_to_error; |
+ }else{ |
+ assert( t.flags==MEM_Null ); |
+ } |
+ if( pCtx->skipFlag ){ |
+ assert( pOp[-1].opcode==OP_CollSeq ); |
+ i = pOp[-1].p1; |
+ if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); |
+ } |
+ break; |
+} |
+ |
+/* Opcode: AggFinal P1 P2 * P4 * |
+** Synopsis: accum=r[P1] N=P2 |
+** |
+** Execute the finalizer function for an aggregate. P1 is |
+** the memory location that is the accumulator for the aggregate. |
+** |
+** P2 is the number of arguments that the step function takes and |
+** P4 is a pointer to the FuncDef for this function. The P2 |
+** argument is not used by this opcode. It is only there to disambiguate |
+** functions that can take varying numbers of arguments. The |
+** P4 argument is only needed for the degenerate case where |
+** the step function was not previously called. |
+*/ |
+case OP_AggFinal: { |
+ Mem *pMem; |
+ assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); |
+ pMem = &aMem[pOp->p1]; |
+ assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); |
+ rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); |
+ if( rc ){ |
+ sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); |
+ goto abort_due_to_error; |
+ } |
+ sqlite3VdbeChangeEncoding(pMem, encoding); |
+ UPDATE_MAX_BLOBSIZE(pMem); |
+ if( sqlite3VdbeMemTooBig(pMem) ){ |
+ goto too_big; |
+ } |
+ break; |
+} |
+ |
+#ifndef SQLITE_OMIT_WAL |
+/* Opcode: Checkpoint P1 P2 P3 * * |
+** |
+** Checkpoint database P1. This is a no-op if P1 is not currently in |
+** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL, |
+** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns |
+** SQLITE_BUSY or not, respectively. Write the number of pages in the |
+** WAL after the checkpoint into mem[P3+1] and the number of pages |
+** in the WAL that have been checkpointed after the checkpoint |
+** completes into mem[P3+2]. However on an error, mem[P3+1] and |
+** mem[P3+2] are initialized to -1. |
+*/ |
+case OP_Checkpoint: { |
+ int i; /* Loop counter */ |
+ int aRes[3]; /* Results */ |
+ Mem *pMem; /* Write results here */ |
+ |
+ assert( p->readOnly==0 ); |
+ aRes[0] = 0; |
+ aRes[1] = aRes[2] = -1; |
+ assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE |
+ || pOp->p2==SQLITE_CHECKPOINT_FULL |
+ || pOp->p2==SQLITE_CHECKPOINT_RESTART |
+ || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE |
+ ); |
+ rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); |
+ if( rc ){ |
+ if( rc!=SQLITE_BUSY ) goto abort_due_to_error; |
+ rc = SQLITE_OK; |
+ aRes[0] = 1; |
+ } |
+ for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ |
+ sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); |
+ } |
+ break; |
+}; |
+#endif |
+ |
+#ifndef SQLITE_OMIT_PRAGMA |
+/* Opcode: JournalMode P1 P2 P3 * * |
+** |
+** Change the journal mode of database P1 to P3. P3 must be one of the |
+** PAGER_JOURNALMODE_XXX values. If changing between the various rollback |
+** modes (delete, truncate, persist, off and memory), this is a simple |
+** operation. No IO is required. |
+** |
+** If changing into or out of WAL mode the procedure is more complicated. |
+** |
+** Write a string containing the final journal-mode to register P2. |
+*/ |
+case OP_JournalMode: { /* out2 */ |
+ Btree *pBt; /* Btree to change journal mode of */ |
+ Pager *pPager; /* Pager associated with pBt */ |
+ int eNew; /* New journal mode */ |
+ int eOld; /* The old journal mode */ |
+#ifndef SQLITE_OMIT_WAL |
+ const char *zFilename; /* Name of database file for pPager */ |
+#endif |
+ |
+ pOut = out2Prerelease(p, pOp); |
+ eNew = pOp->p3; |
+ assert( eNew==PAGER_JOURNALMODE_DELETE |
+ || eNew==PAGER_JOURNALMODE_TRUNCATE |
+ || eNew==PAGER_JOURNALMODE_PERSIST |
+ || eNew==PAGER_JOURNALMODE_OFF |
+ || eNew==PAGER_JOURNALMODE_MEMORY |
+ || eNew==PAGER_JOURNALMODE_WAL |
+ || eNew==PAGER_JOURNALMODE_QUERY |
+ ); |
+ assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
+ assert( p->readOnly==0 ); |
+ |
+ pBt = db->aDb[pOp->p1].pBt; |
+ pPager = sqlite3BtreePager(pBt); |
+ eOld = sqlite3PagerGetJournalMode(pPager); |
+ if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; |
+ if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; |
+ |
+#ifndef SQLITE_OMIT_WAL |
+ zFilename = sqlite3PagerFilename(pPager, 1); |
+ |
+ /* Do not allow a transition to journal_mode=WAL for a database |
+ ** in temporary storage or if the VFS does not support shared memory |
+ */ |
+ if( eNew==PAGER_JOURNALMODE_WAL |
+ && (sqlite3Strlen30(zFilename)==0 /* Temp file */ |
+ || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ |
+ ){ |
+ eNew = eOld; |
+ } |
+ |
+ if( (eNew!=eOld) |
+ && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) |
+ ){ |
+ if( !db->autoCommit || db->nVdbeRead>1 ){ |
+ rc = SQLITE_ERROR; |
+ sqlite3VdbeError(p, |
+ "cannot change %s wal mode from within a transaction", |
+ (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") |
+ ); |
+ goto abort_due_to_error; |
+ }else{ |
+ |
+ if( eOld==PAGER_JOURNALMODE_WAL ){ |
+ /* If leaving WAL mode, close the log file. If successful, the call |
+ ** to PagerCloseWal() checkpoints and deletes the write-ahead-log |
+ ** file. An EXCLUSIVE lock may still be held on the database file |
+ ** after a successful return. |
+ */ |
+ rc = sqlite3PagerCloseWal(pPager, db); |
+ if( rc==SQLITE_OK ){ |
+ sqlite3PagerSetJournalMode(pPager, eNew); |
+ } |
+ }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ |
+ /* Cannot transition directly from MEMORY to WAL. Use mode OFF |
+ ** as an intermediate */ |
+ sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); |
+ } |
+ |
+ /* Open a transaction on the database file. Regardless of the journal |
+ ** mode, this transaction always uses a rollback journal. |
+ */ |
+ assert( sqlite3BtreeIsInTrans(pBt)==0 ); |
+ if( rc==SQLITE_OK ){ |
+ rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); |
+ } |
+ } |
+ } |
+#endif /* ifndef SQLITE_OMIT_WAL */ |
+ |
+ if( rc ) eNew = eOld; |
+ eNew = sqlite3PagerSetJournalMode(pPager, eNew); |
+ |
+ pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
+ pOut->z = (char *)sqlite3JournalModename(eNew); |
+ pOut->n = sqlite3Strlen30(pOut->z); |
+ pOut->enc = SQLITE_UTF8; |
+ sqlite3VdbeChangeEncoding(pOut, encoding); |
+ if( rc ) goto abort_due_to_error; |
+ break; |
+}; |
+#endif /* SQLITE_OMIT_PRAGMA */ |
+ |
+#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) |
+/* Opcode: Vacuum P1 * * * * |
+** |
+** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more |
+** for an attached database. The "temp" database may not be vacuumed. |
+*/ |
+case OP_Vacuum: { |
+ assert( p->readOnly==0 ); |
+ rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1); |
+ if( rc ) goto abort_due_to_error; |
+ break; |
+} |
+#endif |
+ |
+#if !defined(SQLITE_OMIT_AUTOVACUUM) |
+/* Opcode: IncrVacuum P1 P2 * * * |
+** |
+** Perform a single step of the incremental vacuum procedure on |
+** the P1 database. If the vacuum has finished, jump to instruction |
+** P2. Otherwise, fall through to the next instruction. |
+*/ |
+case OP_IncrVacuum: { /* jump */ |
+ Btree *pBt; |
+ |
+ assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
+ assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
+ assert( p->readOnly==0 ); |
+ pBt = db->aDb[pOp->p1].pBt; |
+ rc = sqlite3BtreeIncrVacuum(pBt); |
+ VdbeBranchTaken(rc==SQLITE_DONE,2); |
+ if( rc ){ |
+ if( rc!=SQLITE_DONE ) goto abort_due_to_error; |
+ rc = SQLITE_OK; |
+ goto jump_to_p2; |
+ } |
+ break; |
+} |
+#endif |
+ |
+/* Opcode: Expire P1 * * * * |
+** |
+** Cause precompiled statements to expire. When an expired statement |
+** is executed using sqlite3_step() it will either automatically |
+** reprepare itself (if it was originally created using sqlite3_prepare_v2()) |
+** or it will fail with SQLITE_SCHEMA. |
+** |
+** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, |
+** then only the currently executing statement is expired. |
+*/ |
+case OP_Expire: { |
+ if( !pOp->p1 ){ |
+ sqlite3ExpirePreparedStatements(db); |
+ }else{ |
+ p->expired = 1; |
+ } |
+ break; |
+} |
+ |
+#ifndef SQLITE_OMIT_SHARED_CACHE |
+/* Opcode: TableLock P1 P2 P3 P4 * |
+** Synopsis: iDb=P1 root=P2 write=P3 |
+** |
+** Obtain a lock on a particular table. This instruction is only used when |
+** the shared-cache feature is enabled. |
+** |
+** P1 is the index of the database in sqlite3.aDb[] of the database |
+** on which the lock is acquired. A readlock is obtained if P3==0 or |
+** a write lock if P3==1. |
+** |
+** P2 contains the root-page of the table to lock. |
+** |
+** P4 contains a pointer to the name of the table being locked. This is only |
+** used to generate an error message if the lock cannot be obtained. |
+*/ |
+case OP_TableLock: { |
+ u8 isWriteLock = (u8)pOp->p3; |
+ if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){ |
+ int p1 = pOp->p1; |
+ assert( p1>=0 && p1<db->nDb ); |
+ assert( DbMaskTest(p->btreeMask, p1) ); |
+ assert( isWriteLock==0 || isWriteLock==1 ); |
+ rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); |
+ if( rc ){ |
+ if( (rc&0xFF)==SQLITE_LOCKED ){ |
+ const char *z = pOp->p4.z; |
+ sqlite3VdbeError(p, "database table is locked: %s", z); |
+ } |
+ goto abort_due_to_error; |
+ } |
+ } |
+ break; |
+} |
+#endif /* SQLITE_OMIT_SHARED_CACHE */ |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+/* Opcode: VBegin * * * P4 * |
+** |
+** P4 may be a pointer to an sqlite3_vtab structure. If so, call the |
+** xBegin method for that table. |
+** |
+** Also, whether or not P4 is set, check that this is not being called from |
+** within a callback to a virtual table xSync() method. If it is, the error |
+** code will be set to SQLITE_LOCKED. |
+*/ |
+case OP_VBegin: { |
+ VTable *pVTab; |
+ pVTab = pOp->p4.pVtab; |
+ rc = sqlite3VtabBegin(db, pVTab); |
+ if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); |
+ if( rc ) goto abort_due_to_error; |
+ break; |
+} |
+#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+/* Opcode: VCreate P1 P2 * * * |
+** |
+** P2 is a register that holds the name of a virtual table in database |
+** P1. Call the xCreate method for that table. |
+*/ |
+case OP_VCreate: { |
+ Mem sMem; /* For storing the record being decoded */ |
+ const char *zTab; /* Name of the virtual table */ |
+ |
+ memset(&sMem, 0, sizeof(sMem)); |
+ sMem.db = db; |
+ /* Because P2 is always a static string, it is impossible for the |
+ ** sqlite3VdbeMemCopy() to fail */ |
+ assert( (aMem[pOp->p2].flags & MEM_Str)!=0 ); |
+ assert( (aMem[pOp->p2].flags & MEM_Static)!=0 ); |
+ rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]); |
+ assert( rc==SQLITE_OK ); |
+ zTab = (const char*)sqlite3_value_text(&sMem); |
+ assert( zTab || db->mallocFailed ); |
+ if( zTab ){ |
+ rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); |
+ } |
+ sqlite3VdbeMemRelease(&sMem); |
+ if( rc ) goto abort_due_to_error; |
+ break; |
+} |
+#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+/* Opcode: VDestroy P1 * * P4 * |
+** |
+** P4 is the name of a virtual table in database P1. Call the xDestroy method |
+** of that table. |
+*/ |
+case OP_VDestroy: { |
+ db->nVDestroy++; |
+ rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); |
+ db->nVDestroy--; |
+ if( rc ) goto abort_due_to_error; |
+ break; |
+} |
+#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+/* Opcode: VOpen P1 * * P4 * |
+** |
+** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
+** P1 is a cursor number. This opcode opens a cursor to the virtual |
+** table and stores that cursor in P1. |
+*/ |
+case OP_VOpen: { |
+ VdbeCursor *pCur; |
+ sqlite3_vtab_cursor *pVCur; |
+ sqlite3_vtab *pVtab; |
+ const sqlite3_module *pModule; |
+ |
+ assert( p->bIsReader ); |
+ pCur = 0; |
+ pVCur = 0; |
+ pVtab = pOp->p4.pVtab->pVtab; |
+ if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
+ rc = SQLITE_LOCKED; |
+ goto abort_due_to_error; |
+ } |
+ pModule = pVtab->pModule; |
+ rc = pModule->xOpen(pVtab, &pVCur); |
+ sqlite3VtabImportErrmsg(p, pVtab); |
+ if( rc ) goto abort_due_to_error; |
+ |
+ /* Initialize sqlite3_vtab_cursor base class */ |
+ pVCur->pVtab = pVtab; |
+ |
+ /* Initialize vdbe cursor object */ |
+ pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB); |
+ if( pCur ){ |
+ pCur->uc.pVCur = pVCur; |
+ pVtab->nRef++; |
+ }else{ |
+ assert( db->mallocFailed ); |
+ pModule->xClose(pVCur); |
+ goto no_mem; |
+ } |
+ break; |
+} |
+#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+/* Opcode: VFilter P1 P2 P3 P4 * |
+** Synopsis: iplan=r[P3] zplan='P4' |
+** |
+** P1 is a cursor opened using VOpen. P2 is an address to jump to if |
+** the filtered result set is empty. |
+** |
+** P4 is either NULL or a string that was generated by the xBestIndex |
+** method of the module. The interpretation of the P4 string is left |
+** to the module implementation. |
+** |
+** This opcode invokes the xFilter method on the virtual table specified |
+** by P1. The integer query plan parameter to xFilter is stored in register |
+** P3. Register P3+1 stores the argc parameter to be passed to the |
+** xFilter method. Registers P3+2..P3+1+argc are the argc |
+** additional parameters which are passed to |
+** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. |
+** |
+** A jump is made to P2 if the result set after filtering would be empty. |
+*/ |
+case OP_VFilter: { /* jump */ |
+ int nArg; |
+ int iQuery; |
+ const sqlite3_module *pModule; |
+ Mem *pQuery; |
+ Mem *pArgc; |
+ sqlite3_vtab_cursor *pVCur; |
+ sqlite3_vtab *pVtab; |
+ VdbeCursor *pCur; |
+ int res; |
+ int i; |
+ Mem **apArg; |
+ |
+ pQuery = &aMem[pOp->p3]; |
+ pArgc = &pQuery[1]; |
+ pCur = p->apCsr[pOp->p1]; |
+ assert( memIsValid(pQuery) ); |
+ REGISTER_TRACE(pOp->p3, pQuery); |
+ assert( pCur->eCurType==CURTYPE_VTAB ); |
+ pVCur = pCur->uc.pVCur; |
+ pVtab = pVCur->pVtab; |
+ pModule = pVtab->pModule; |
+ |
+ /* Grab the index number and argc parameters */ |
+ assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); |
+ nArg = (int)pArgc->u.i; |
+ iQuery = (int)pQuery->u.i; |
+ |
+ /* Invoke the xFilter method */ |
+ res = 0; |
+ apArg = p->apArg; |
+ for(i = 0; i<nArg; i++){ |
+ apArg[i] = &pArgc[i+1]; |
+ } |
+ rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg); |
+ sqlite3VtabImportErrmsg(p, pVtab); |
+ if( rc ) goto abort_due_to_error; |
+ res = pModule->xEof(pVCur); |
+ pCur->nullRow = 0; |
+ VdbeBranchTaken(res!=0,2); |
+ if( res ) goto jump_to_p2; |
+ break; |
+} |
+#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+/* Opcode: VColumn P1 P2 P3 * * |
+** Synopsis: r[P3]=vcolumn(P2) |
+** |
+** Store the value of the P2-th column of |
+** the row of the virtual-table that the |
+** P1 cursor is pointing to into register P3. |
+*/ |
+case OP_VColumn: { |
+ sqlite3_vtab *pVtab; |
+ const sqlite3_module *pModule; |
+ Mem *pDest; |
+ sqlite3_context sContext; |
+ |
+ VdbeCursor *pCur = p->apCsr[pOp->p1]; |
+ assert( pCur->eCurType==CURTYPE_VTAB ); |
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
+ pDest = &aMem[pOp->p3]; |
+ memAboutToChange(p, pDest); |
+ if( pCur->nullRow ){ |
+ sqlite3VdbeMemSetNull(pDest); |
+ break; |
+ } |
+ pVtab = pCur->uc.pVCur->pVtab; |
+ pModule = pVtab->pModule; |
+ assert( pModule->xColumn ); |
+ memset(&sContext, 0, sizeof(sContext)); |
+ sContext.pOut = pDest; |
+ MemSetTypeFlag(pDest, MEM_Null); |
+ rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2); |
+ sqlite3VtabImportErrmsg(p, pVtab); |
+ if( sContext.isError ){ |
+ rc = sContext.isError; |
+ } |
+ sqlite3VdbeChangeEncoding(pDest, encoding); |
+ REGISTER_TRACE(pOp->p3, pDest); |
+ UPDATE_MAX_BLOBSIZE(pDest); |
+ |
+ if( sqlite3VdbeMemTooBig(pDest) ){ |
+ goto too_big; |
+ } |
+ if( rc ) goto abort_due_to_error; |
+ break; |
+} |
+#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+/* Opcode: VNext P1 P2 * * * |
+** |
+** Advance virtual table P1 to the next row in its result set and |
+** jump to instruction P2. Or, if the virtual table has reached |
+** the end of its result set, then fall through to the next instruction. |
+*/ |
+case OP_VNext: { /* jump */ |
+ sqlite3_vtab *pVtab; |
+ const sqlite3_module *pModule; |
+ int res; |
+ VdbeCursor *pCur; |
+ |
+ res = 0; |
+ pCur = p->apCsr[pOp->p1]; |
+ assert( pCur->eCurType==CURTYPE_VTAB ); |
+ if( pCur->nullRow ){ |
+ break; |
+ } |
+ pVtab = pCur->uc.pVCur->pVtab; |
+ pModule = pVtab->pModule; |
+ assert( pModule->xNext ); |
+ |
+ /* Invoke the xNext() method of the module. There is no way for the |
+ ** underlying implementation to return an error if one occurs during |
+ ** xNext(). Instead, if an error occurs, true is returned (indicating that |
+ ** data is available) and the error code returned when xColumn or |
+ ** some other method is next invoked on the save virtual table cursor. |
+ */ |
+ rc = pModule->xNext(pCur->uc.pVCur); |
+ sqlite3VtabImportErrmsg(p, pVtab); |
+ if( rc ) goto abort_due_to_error; |
+ res = pModule->xEof(pCur->uc.pVCur); |
+ VdbeBranchTaken(!res,2); |
+ if( !res ){ |
+ /* If there is data, jump to P2 */ |
+ goto jump_to_p2_and_check_for_interrupt; |
+ } |
+ goto check_for_interrupt; |
+} |
+#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+/* Opcode: VRename P1 * * P4 * |
+** |
+** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
+** This opcode invokes the corresponding xRename method. The value |
+** in register P1 is passed as the zName argument to the xRename method. |
+*/ |
+case OP_VRename: { |
+ sqlite3_vtab *pVtab; |
+ Mem *pName; |
+ |
+ pVtab = pOp->p4.pVtab->pVtab; |
+ pName = &aMem[pOp->p1]; |
+ assert( pVtab->pModule->xRename ); |
+ assert( memIsValid(pName) ); |
+ assert( p->readOnly==0 ); |
+ REGISTER_TRACE(pOp->p1, pName); |
+ assert( pName->flags & MEM_Str ); |
+ testcase( pName->enc==SQLITE_UTF8 ); |
+ testcase( pName->enc==SQLITE_UTF16BE ); |
+ testcase( pName->enc==SQLITE_UTF16LE ); |
+ rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); |
+ if( rc ) goto abort_due_to_error; |
+ rc = pVtab->pModule->xRename(pVtab, pName->z); |
+ sqlite3VtabImportErrmsg(p, pVtab); |
+ p->expired = 0; |
+ if( rc ) goto abort_due_to_error; |
+ break; |
+} |
+#endif |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+/* Opcode: VUpdate P1 P2 P3 P4 P5 |
+** Synopsis: data=r[P3@P2] |
+** |
+** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. |
+** This opcode invokes the corresponding xUpdate method. P2 values |
+** are contiguous memory cells starting at P3 to pass to the xUpdate |
+** invocation. The value in register (P3+P2-1) corresponds to the |
+** p2th element of the argv array passed to xUpdate. |
+** |
+** The xUpdate method will do a DELETE or an INSERT or both. |
+** The argv[0] element (which corresponds to memory cell P3) |
+** is the rowid of a row to delete. If argv[0] is NULL then no |
+** deletion occurs. The argv[1] element is the rowid of the new |
+** row. This can be NULL to have the virtual table select the new |
+** rowid for itself. The subsequent elements in the array are |
+** the values of columns in the new row. |
+** |
+** If P2==1 then no insert is performed. argv[0] is the rowid of |
+** a row to delete. |
+** |
+** P1 is a boolean flag. If it is set to true and the xUpdate call |
+** is successful, then the value returned by sqlite3_last_insert_rowid() |
+** is set to the value of the rowid for the row just inserted. |
+** |
+** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to |
+** apply in the case of a constraint failure on an insert or update. |
+*/ |
+case OP_VUpdate: { |
+ sqlite3_vtab *pVtab; |
+ const sqlite3_module *pModule; |
+ int nArg; |
+ int i; |
+ sqlite_int64 rowid; |
+ Mem **apArg; |
+ Mem *pX; |
+ |
+ assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback |
+ || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace |
+ ); |
+ assert( p->readOnly==0 ); |
+ pVtab = pOp->p4.pVtab->pVtab; |
+ if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
+ rc = SQLITE_LOCKED; |
+ goto abort_due_to_error; |
+ } |
+ pModule = pVtab->pModule; |
+ nArg = pOp->p2; |
+ assert( pOp->p4type==P4_VTAB ); |
+ if( ALWAYS(pModule->xUpdate) ){ |
+ u8 vtabOnConflict = db->vtabOnConflict; |
+ apArg = p->apArg; |
+ pX = &aMem[pOp->p3]; |
+ for(i=0; i<nArg; i++){ |
+ assert( memIsValid(pX) ); |
+ memAboutToChange(p, pX); |
+ apArg[i] = pX; |
+ pX++; |
+ } |
+ db->vtabOnConflict = pOp->p5; |
+ rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); |
+ db->vtabOnConflict = vtabOnConflict; |
+ sqlite3VtabImportErrmsg(p, pVtab); |
+ if( rc==SQLITE_OK && pOp->p1 ){ |
+ assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); |
+ db->lastRowid = rowid; |
+ } |
+ if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ |
+ if( pOp->p5==OE_Ignore ){ |
+ rc = SQLITE_OK; |
+ }else{ |
+ p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); |
+ } |
+ }else{ |
+ p->nChange++; |
+ } |
+ if( rc ) goto abort_due_to_error; |
+ } |
+ break; |
+} |
+#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
+ |
+#ifndef SQLITE_OMIT_PAGER_PRAGMAS |
+/* Opcode: Pagecount P1 P2 * * * |
+** |
+** Write the current number of pages in database P1 to memory cell P2. |
+*/ |
+case OP_Pagecount: { /* out2 */ |
+ pOut = out2Prerelease(p, pOp); |
+ pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); |
+ break; |
+} |
+#endif |
+ |
+ |
+#ifndef SQLITE_OMIT_PAGER_PRAGMAS |
+/* Opcode: MaxPgcnt P1 P2 P3 * * |
+** |
+** Try to set the maximum page count for database P1 to the value in P3. |
+** Do not let the maximum page count fall below the current page count and |
+** do not change the maximum page count value if P3==0. |
+** |
+** Store the maximum page count after the change in register P2. |
+*/ |
+case OP_MaxPgcnt: { /* out2 */ |
+ unsigned int newMax; |
+ Btree *pBt; |
+ |
+ pOut = out2Prerelease(p, pOp); |
+ pBt = db->aDb[pOp->p1].pBt; |
+ newMax = 0; |
+ if( pOp->p3 ){ |
+ newMax = sqlite3BtreeLastPage(pBt); |
+ if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; |
+ } |
+ pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); |
+ break; |
+} |
+#endif |
+ |
+ |
+/* Opcode: Init P1 P2 * P4 * |
+** Synopsis: Start at P2 |
+** |
+** Programs contain a single instance of this opcode as the very first |
+** opcode. |
+** |
+** If tracing is enabled (by the sqlite3_trace()) interface, then |
+** the UTF-8 string contained in P4 is emitted on the trace callback. |
+** Or if P4 is blank, use the string returned by sqlite3_sql(). |
+** |
+** If P2 is not zero, jump to instruction P2. |
+** |
+** Increment the value of P1 so that OP_Once opcodes will jump the |
+** first time they are evaluated for this run. |
+*/ |
+case OP_Init: { /* jump */ |
+ char *zTrace; |
+ int i; |
+ |
+ /* If the P4 argument is not NULL, then it must be an SQL comment string. |
+ ** The "--" string is broken up to prevent false-positives with srcck1.c. |
+ ** |
+ ** This assert() provides evidence for: |
+ ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that |
+ ** would have been returned by the legacy sqlite3_trace() interface by |
+ ** using the X argument when X begins with "--" and invoking |
+ ** sqlite3_expanded_sql(P) otherwise. |
+ */ |
+ assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 ); |
+ assert( pOp==p->aOp ); /* Always instruction 0 */ |
+ |
+#ifndef SQLITE_OMIT_TRACE |
+ if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0 |
+ && !p->doingRerun |
+ && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
+ ){ |
+#ifndef SQLITE_OMIT_DEPRECATED |
+ if( db->mTrace & SQLITE_TRACE_LEGACY ){ |
+ void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace; |
+ char *z = sqlite3VdbeExpandSql(p, zTrace); |
+ x(db->pTraceArg, z); |
+ sqlite3_free(z); |
+ }else |
+#endif |
+ { |
+ (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace); |
+ } |
+ } |
+#ifdef SQLITE_USE_FCNTL_TRACE |
+ zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); |
+ if( zTrace ){ |
+ int j; |
+ for(j=0; j<db->nDb; j++){ |
+ if( DbMaskTest(p->btreeMask, j)==0 ) continue; |
+ sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace); |
+ } |
+ } |
+#endif /* SQLITE_USE_FCNTL_TRACE */ |
+#ifdef SQLITE_DEBUG |
+ if( (db->flags & SQLITE_SqlTrace)!=0 |
+ && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
+ ){ |
+ sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); |
+ } |
+#endif /* SQLITE_DEBUG */ |
+#endif /* SQLITE_OMIT_TRACE */ |
+ assert( pOp->p2>0 ); |
+ if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){ |
+ for(i=1; i<p->nOp; i++){ |
+ if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0; |
+ } |
+ pOp->p1 = 0; |
+ } |
+ pOp->p1++; |
+ goto jump_to_p2; |
+} |
+ |
+#ifdef SQLITE_ENABLE_CURSOR_HINTS |
+/* Opcode: CursorHint P1 * * P4 * |
+** |
+** Provide a hint to cursor P1 that it only needs to return rows that |
+** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer |
+** to values currently held in registers. TK_COLUMN terms in the P4 |
+** expression refer to columns in the b-tree to which cursor P1 is pointing. |
+*/ |
+case OP_CursorHint: { |
+ VdbeCursor *pC; |
+ |
+ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
+ assert( pOp->p4type==P4_EXPR ); |
+ pC = p->apCsr[pOp->p1]; |
+ if( pC ){ |
+ assert( pC->eCurType==CURTYPE_BTREE ); |
+ sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE, |
+ pOp->p4.pExpr, aMem); |
+ } |
+ break; |
+} |
+#endif /* SQLITE_ENABLE_CURSOR_HINTS */ |
+ |
+/* Opcode: Noop * * * * * |
+** |
+** Do nothing. This instruction is often useful as a jump |
+** destination. |
+*/ |
+/* |
+** The magic Explain opcode are only inserted when explain==2 (which |
+** is to say when the EXPLAIN QUERY PLAN syntax is used.) |
+** This opcode records information from the optimizer. It is the |
+** the same as a no-op. This opcodesnever appears in a real VM program. |
+*/ |
+default: { /* This is really OP_Noop and OP_Explain */ |
+ assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); |
+ break; |
+} |
+ |
+/***************************************************************************** |
+** The cases of the switch statement above this line should all be indented |
+** by 6 spaces. But the left-most 6 spaces have been removed to improve the |
+** readability. From this point on down, the normal indentation rules are |
+** restored. |
+*****************************************************************************/ |
+ } |
+ |
+#ifdef VDBE_PROFILE |
+ { |
+ u64 endTime = sqlite3Hwtime(); |
+ if( endTime>start ) pOrigOp->cycles += endTime - start; |
+ pOrigOp->cnt++; |
+ } |
+#endif |
+ |
+ /* The following code adds nothing to the actual functionality |
+ ** of the program. It is only here for testing and debugging. |
+ ** On the other hand, it does burn CPU cycles every time through |
+ ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
+ */ |
+#ifndef NDEBUG |
+ assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] ); |
+ |
+#ifdef SQLITE_DEBUG |
+ if( db->flags & SQLITE_VdbeTrace ){ |
+ u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode]; |
+ if( rc!=0 ) printf("rc=%d\n",rc); |
+ if( opProperty & (OPFLG_OUT2) ){ |
+ registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); |
+ } |
+ if( opProperty & OPFLG_OUT3 ){ |
+ registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); |
+ } |
+ } |
+#endif /* SQLITE_DEBUG */ |
+#endif /* NDEBUG */ |
+ } /* The end of the for(;;) loop the loops through opcodes */ |
+ |
+ /* If we reach this point, it means that execution is finished with |
+ ** an error of some kind. |
+ */ |
+abort_due_to_error: |
+ if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT; |
+ assert( rc ); |
+ if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){ |
+ sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); |
+ } |
+ p->rc = rc; |
+ sqlite3SystemError(db, rc); |
+ testcase( sqlite3GlobalConfig.xLog!=0 ); |
+ sqlite3_log(rc, "statement aborts at %d: [%s] %s", |
+ (int)(pOp - aOp), p->zSql, p->zErrMsg); |
+ sqlite3VdbeHalt(p); |
+ if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db); |
+ rc = SQLITE_ERROR; |
+ if( resetSchemaOnFault>0 ){ |
+ sqlite3ResetOneSchema(db, resetSchemaOnFault-1); |
+ } |
+ |
+ /* This is the only way out of this procedure. We have to |
+ ** release the mutexes on btrees that were acquired at the |
+ ** top. */ |
+vdbe_return: |
+ testcase( nVmStep>0 ); |
+ p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; |
+ sqlite3VdbeLeave(p); |
+ assert( rc!=SQLITE_OK || nExtraDelete==0 |
+ || sqlite3_strlike("DELETE%",p->zSql,0)!=0 |
+ ); |
+ return rc; |
+ |
+ /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH |
+ ** is encountered. |
+ */ |
+too_big: |
+ sqlite3VdbeError(p, "string or blob too big"); |
+ rc = SQLITE_TOOBIG; |
+ goto abort_due_to_error; |
+ |
+ /* Jump to here if a malloc() fails. |
+ */ |
+no_mem: |
+ sqlite3OomFault(db); |
+ sqlite3VdbeError(p, "out of memory"); |
+ rc = SQLITE_NOMEM_BKPT; |
+ goto abort_due_to_error; |
+ |
+ /* Jump to here if the sqlite3_interrupt() API sets the interrupt |
+ ** flag. |
+ */ |
+abort_due_to_interrupt: |
+ assert( db->u1.isInterrupted ); |
+ rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT; |
+ p->rc = rc; |
+ sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); |
+ goto abort_due_to_error; |
+} |
+ |
+ |
+/************** End of vdbe.c ************************************************/ |
+/************** Begin file vdbeblob.c ****************************************/ |
+/* |
+** 2007 May 1 |
+** |
+** 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. |
+** |
+************************************************************************* |
+** |
+** This file contains code used to implement incremental BLOB I/O. |
+*/ |
+ |
+/* #include "sqliteInt.h" */ |
+/* #include "vdbeInt.h" */ |
+ |
+#ifndef SQLITE_OMIT_INCRBLOB |
+ |
+/* |
+** Valid sqlite3_blob* handles point to Incrblob structures. |
+*/ |
+typedef struct Incrblob Incrblob; |
+struct Incrblob { |
+ int nByte; /* Size of open blob, in bytes */ |
+ int iOffset; /* Byte offset of blob in cursor data */ |
+ u16 iCol; /* Table column this handle is open on */ |
+ BtCursor *pCsr; /* Cursor pointing at blob row */ |
+ sqlite3_stmt *pStmt; /* Statement holding cursor open */ |
+ sqlite3 *db; /* The associated database */ |
+ char *zDb; /* Database name */ |
+ Table *pTab; /* Table object */ |
+}; |
+ |
+ |
+/* |
+** This function is used by both blob_open() and blob_reopen(). It seeks |
+** the b-tree cursor associated with blob handle p to point to row iRow. |
+** If successful, SQLITE_OK is returned and subsequent calls to |
+** sqlite3_blob_read() or sqlite3_blob_write() access the specified row. |
+** |
+** If an error occurs, or if the specified row does not exist or does not |
+** contain a value of type TEXT or BLOB in the column nominated when the |
+** blob handle was opened, then an error code is returned and *pzErr may |
+** be set to point to a buffer containing an error message. It is the |
+** responsibility of the caller to free the error message buffer using |
+** sqlite3DbFree(). |
+** |
+** If an error does occur, then the b-tree cursor is closed. All subsequent |
+** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will |
+** immediately return SQLITE_ABORT. |
+*/ |
+static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){ |
+ int rc; /* Error code */ |
+ char *zErr = 0; /* Error message */ |
+ Vdbe *v = (Vdbe *)p->pStmt; |
+ |
+ /* Set the value of register r[1] in the SQL statement to integer iRow. |
+ ** This is done directly as a performance optimization |
+ */ |
+ v->aMem[1].flags = MEM_Int; |
+ v->aMem[1].u.i = iRow; |
+ |
+ /* If the statement has been run before (and is paused at the OP_ResultRow) |
+ ** then back it up to the point where it does the OP_SeekRowid. This could |
+ ** have been down with an extra OP_Goto, but simply setting the program |
+ ** counter is faster. */ |
+ if( v->pc>3 ){ |
+ v->pc = 3; |
+ rc = sqlite3VdbeExec(v); |
+ }else{ |
+ rc = sqlite3_step(p->pStmt); |
+ } |
+ if( rc==SQLITE_ROW ){ |
+ VdbeCursor *pC = v->apCsr[0]; |
+ u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0; |
+ testcase( pC->nHdrParsed==p->iCol ); |
+ testcase( pC->nHdrParsed==p->iCol+1 ); |
+ if( type<12 ){ |
+ zErr = sqlite3MPrintf(p->db, "cannot open value of type %s", |
+ type==0?"null": type==7?"real": "integer" |
+ ); |
+ rc = SQLITE_ERROR; |
+ sqlite3_finalize(p->pStmt); |
+ p->pStmt = 0; |
+ }else{ |
+ p->iOffset = pC->aType[p->iCol + pC->nField]; |
+ p->nByte = sqlite3VdbeSerialTypeLen(type); |
+ p->pCsr = pC->uc.pCursor; |
+ sqlite3BtreeIncrblobCursor(p->pCsr); |
+ } |
+ } |
+ |
+ if( rc==SQLITE_ROW ){ |
+ rc = SQLITE_OK; |
+ }else if( p->pStmt ){ |
+ rc = sqlite3_finalize(p->pStmt); |
+ p->pStmt = 0; |
+ if( rc==SQLITE_OK ){ |
+ zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow); |
+ rc = SQLITE_ERROR; |
+ }else{ |
+ zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db)); |
+ } |
+ } |
+ |
+ assert( rc!=SQLITE_OK || zErr==0 ); |
+ assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE ); |
+ |
+ *pzErr = zErr; |
+ return rc; |
+} |
+ |
+/* |
+** Open a blob handle. |
+*/ |
+SQLITE_API int sqlite3_blob_open( |
+ sqlite3* db, /* The database connection */ |
+ const char *zDb, /* The attached database containing the blob */ |
+ const char *zTable, /* The table containing the blob */ |
+ const char *zColumn, /* The column containing the blob */ |
+ sqlite_int64 iRow, /* The row containing the glob */ |
+ int wrFlag, /* True -> read/write access, false -> read-only */ |
+ sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */ |
+){ |
+ int nAttempt = 0; |
+ int iCol; /* Index of zColumn in row-record */ |
+ int rc = SQLITE_OK; |
+ char *zErr = 0; |
+ Table *pTab; |
+ Parse *pParse = 0; |
+ Incrblob *pBlob = 0; |
+ |
+#ifdef SQLITE_ENABLE_API_ARMOR |
+ if( ppBlob==0 ){ |
+ return SQLITE_MISUSE_BKPT; |
+ } |
+#endif |
+ *ppBlob = 0; |
+#ifdef SQLITE_ENABLE_API_ARMOR |
+ if( !sqlite3SafetyCheckOk(db) || zTable==0 ){ |
+ return SQLITE_MISUSE_BKPT; |
+ } |
+#endif |
+ wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */ |
+ |
+ sqlite3_mutex_enter(db->mutex); |
+ |
+ pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob)); |
+ if( !pBlob ) goto blob_open_out; |
+ pParse = sqlite3StackAllocRaw(db, sizeof(*pParse)); |
+ if( !pParse ) goto blob_open_out; |
+ |
+ do { |
+ memset(pParse, 0, sizeof(Parse)); |
+ pParse->db = db; |
+ sqlite3DbFree(db, zErr); |
+ zErr = 0; |
+ |
+ sqlite3BtreeEnterAll(db); |
+ pTab = sqlite3LocateTable(pParse, 0, zTable, zDb); |
+ if( pTab && IsVirtual(pTab) ){ |
+ pTab = 0; |
+ sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable); |
+ } |
+ if( pTab && !HasRowid(pTab) ){ |
+ pTab = 0; |
+ sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable); |
+ } |
+#ifndef SQLITE_OMIT_VIEW |
+ if( pTab && pTab->pSelect ){ |
+ pTab = 0; |
+ sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable); |
+ } |
+#endif |
+ if( !pTab ){ |
+ if( pParse->zErrMsg ){ |
+ sqlite3DbFree(db, zErr); |
+ zErr = pParse->zErrMsg; |
+ pParse->zErrMsg = 0; |
+ } |
+ rc = SQLITE_ERROR; |
+ sqlite3BtreeLeaveAll(db); |
+ goto blob_open_out; |
+ } |
+ pBlob->pTab = pTab; |
+ pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName; |
+ |
+ /* Now search pTab for the exact column. */ |
+ for(iCol=0; iCol<pTab->nCol; iCol++) { |
+ if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){ |
+ break; |
+ } |
+ } |
+ if( iCol==pTab->nCol ){ |
+ sqlite3DbFree(db, zErr); |
+ zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn); |
+ rc = SQLITE_ERROR; |
+ sqlite3BtreeLeaveAll(db); |
+ goto blob_open_out; |
+ } |
+ |
+ /* If the value is being opened for writing, check that the |
+ ** column is not indexed, and that it is not part of a foreign key. |
+ */ |
+ if( wrFlag ){ |
+ const char *zFault = 0; |
+ Index *pIdx; |
+#ifndef SQLITE_OMIT_FOREIGN_KEY |
+ if( db->flags&SQLITE_ForeignKeys ){ |
+ /* Check that the column is not part of an FK child key definition. It |
+ ** is not necessary to check if it is part of a parent key, as parent |
+ ** key columns must be indexed. The check below will pick up this |
+ ** case. */ |
+ FKey *pFKey; |
+ for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
+ int j; |
+ for(j=0; j<pFKey->nCol; j++){ |
+ if( pFKey->aCol[j].iFrom==iCol ){ |
+ zFault = "foreign key"; |
+ } |
+ } |
+ } |
+ } |
+#endif |
+ for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
+ int j; |
+ for(j=0; j<pIdx->nKeyCol; j++){ |
+ /* FIXME: Be smarter about indexes that use expressions */ |
+ if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){ |
+ zFault = "indexed"; |
+ } |
+ } |
+ } |
+ if( zFault ){ |
+ sqlite3DbFree(db, zErr); |
+ zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault); |
+ rc = SQLITE_ERROR; |
+ sqlite3BtreeLeaveAll(db); |
+ goto blob_open_out; |
+ } |
+ } |
+ |
+ pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse); |
+ assert( pBlob->pStmt || db->mallocFailed ); |
+ if( pBlob->pStmt ){ |
+ |
+ /* This VDBE program seeks a btree cursor to the identified |
+ ** db/table/row entry. The reason for using a vdbe program instead |
+ ** of writing code to use the b-tree layer directly is that the |
+ ** vdbe program will take advantage of the various transaction, |
+ ** locking and error handling infrastructure built into the vdbe. |
+ ** |
+ ** After seeking the cursor, the vdbe executes an OP_ResultRow. |
+ ** Code external to the Vdbe then "borrows" the b-tree cursor and |
+ ** uses it to implement the blob_read(), blob_write() and |
+ ** blob_bytes() functions. |
+ ** |
+ ** The sqlite3_blob_close() function finalizes the vdbe program, |
+ ** which closes the b-tree cursor and (possibly) commits the |
+ ** transaction. |
+ */ |
+ static const int iLn = VDBE_OFFSET_LINENO(2); |
+ static const VdbeOpList openBlob[] = { |
+ {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */ |
+ {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */ |
+ /* blobSeekToRow() will initialize r[1] to the desired rowid */ |
+ {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */ |
+ {OP_Column, 0, 0, 1}, /* 3 */ |
+ {OP_ResultRow, 1, 0, 0}, /* 4 */ |
+ {OP_Halt, 0, 0, 0}, /* 5 */ |
+ }; |
+ Vdbe *v = (Vdbe *)pBlob->pStmt; |
+ int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
+ VdbeOp *aOp; |
+ |
+ sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag, |
+ pTab->pSchema->schema_cookie, |
+ pTab->pSchema->iGeneration); |
+ sqlite3VdbeChangeP5(v, 1); |
+ aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn); |
+ |
+ /* Make sure a mutex is held on the table to be accessed */ |
+ sqlite3VdbeUsesBtree(v, iDb); |
+ |
+ if( db->mallocFailed==0 ){ |
+ assert( aOp!=0 ); |
+ /* Configure the OP_TableLock instruction */ |
+#ifdef SQLITE_OMIT_SHARED_CACHE |
+ aOp[0].opcode = OP_Noop; |
+#else |
+ aOp[0].p1 = iDb; |
+ aOp[0].p2 = pTab->tnum; |
+ aOp[0].p3 = wrFlag; |
+ sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT); |
+ } |
+ if( db->mallocFailed==0 ){ |
+#endif |
+ |
+ /* Remove either the OP_OpenWrite or OpenRead. Set the P2 |
+ ** parameter of the other to pTab->tnum. */ |
+ if( wrFlag ) aOp[1].opcode = OP_OpenWrite; |
+ aOp[1].p2 = pTab->tnum; |
+ aOp[1].p3 = iDb; |
+ |
+ /* Configure the number of columns. Configure the cursor to |
+ ** think that the table has one more column than it really |
+ ** does. An OP_Column to retrieve this imaginary column will |
+ ** always return an SQL NULL. This is useful because it means |
+ ** we can invoke OP_Column to fill in the vdbe cursors type |
+ ** and offset cache without causing any IO. |
+ */ |
+ aOp[1].p4type = P4_INT32; |
+ aOp[1].p4.i = pTab->nCol+1; |
+ aOp[3].p2 = pTab->nCol; |
+ |
+ pParse->nVar = 0; |
+ pParse->nMem = 1; |
+ pParse->nTab = 1; |
+ sqlite3VdbeMakeReady(v, pParse); |
+ } |
+ } |
+ |
+ pBlob->iCol = iCol; |
+ pBlob->db = db; |
+ sqlite3BtreeLeaveAll(db); |
+ if( db->mallocFailed ){ |
+ goto blob_open_out; |
+ } |
+ rc = blobSeekToRow(pBlob, iRow, &zErr); |
+ } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA ); |
+ |
+blob_open_out: |
+ if( rc==SQLITE_OK && db->mallocFailed==0 ){ |
+ *ppBlob = (sqlite3_blob *)pBlob; |
+ }else{ |
+ if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt); |
+ sqlite3DbFree(db, pBlob); |
+ } |
+ sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr); |
+ sqlite3DbFree(db, zErr); |
+ sqlite3ParserReset(pParse); |
+ sqlite3StackFree(db, pParse); |
+ rc = sqlite3ApiExit(db, rc); |
+ sqlite3_mutex_leave(db->mutex); |
+ return rc; |
+} |
+ |
+/* |
+** Close a blob handle that was previously created using |
+** sqlite3_blob_open(). |
+*/ |
+SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){ |
+ Incrblob *p = (Incrblob *)pBlob; |
+ int rc; |
+ sqlite3 *db; |
+ |
+ if( p ){ |
+ db = p->db; |
+ sqlite3_mutex_enter(db->mutex); |
+ rc = sqlite3_finalize(p->pStmt); |
+ sqlite3DbFree(db, p); |
+ sqlite3_mutex_leave(db->mutex); |
+ }else{ |
+ rc = SQLITE_OK; |
+ } |
+ return rc; |
+} |
+ |
+/* |
+** Perform a read or write operation on a blob |
+*/ |
+static int blobReadWrite( |
+ sqlite3_blob *pBlob, |
+ void *z, |
+ int n, |
+ int iOffset, |
+ int (*xCall)(BtCursor*, u32, u32, void*) |
+){ |
+ int rc; |
+ Incrblob *p = (Incrblob *)pBlob; |
+ Vdbe *v; |
+ sqlite3 *db; |
+ |
+ if( p==0 ) return SQLITE_MISUSE_BKPT; |
+ db = p->db; |
+ sqlite3_mutex_enter(db->mutex); |
+ v = (Vdbe*)p->pStmt; |
+ |
+ if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){ |
+ /* Request is out of range. Return a transient error. */ |
+ rc = SQLITE_ERROR; |
+ }else if( v==0 ){ |
+ /* If there is no statement handle, then the blob-handle has |
+ ** already been invalidated. Return SQLITE_ABORT in this case. |
+ */ |
+ rc = SQLITE_ABORT; |
+ }else{ |
+ /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is |
+ ** returned, clean-up the statement handle. |
+ */ |
+ assert( db == v->db ); |
+ sqlite3BtreeEnterCursor(p->pCsr); |
+ |
+#ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
+ if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){ |
+ /* If a pre-update hook is registered and this is a write cursor, |
+ ** invoke it here. |
+ ** |
+ ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this |
+ ** operation should really be an SQLITE_UPDATE. This is probably |
+ ** incorrect, but is convenient because at this point the new.* values |
+ ** are not easily obtainable. And for the sessions module, an |
+ ** SQLITE_UPDATE where the PK columns do not change is handled in the |
+ ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually |
+ ** slightly more efficient). Since you cannot write to a PK column |
+ ** using the incremental-blob API, this works. For the sessions module |
+ ** anyhow. |
+ */ |
+ sqlite3_int64 iKey; |
+ iKey = sqlite3BtreeIntegerKey(p->pCsr); |
+ sqlite3VdbePreUpdateHook( |
+ v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1 |
+ ); |
+ } |
+#endif |
+ |
+ rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); |
+ sqlite3BtreeLeaveCursor(p->pCsr); |
+ if( rc==SQLITE_ABORT ){ |
+ sqlite3VdbeFinalize(v); |
+ p->pStmt = 0; |
+ }else{ |
+ v->rc = rc; |
+ } |
+ } |
+ sqlite3Error(db, rc); |
+ rc = sqlite3ApiExit(db, rc); |
+ sqlite3_mutex_leave(db->mutex); |
+ return rc; |
+} |
+ |
+/* |
+** Read data from a blob handle. |
+*/ |
+SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){ |
+ return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked); |
+} |
+ |
+/* |
+** Write data to a blob handle. |
+*/ |
+SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){ |
+ return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData); |
+} |
+ |
+/* |
+** Query a blob handle for the size of the data. |
+** |
+** The Incrblob.nByte field is fixed for the lifetime of the Incrblob |
+** so no mutex is required for access. |
+*/ |
+SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){ |
+ Incrblob *p = (Incrblob *)pBlob; |
+ return (p && p->pStmt) ? p->nByte : 0; |
+} |
+ |
+/* |
+** Move an existing blob handle to point to a different row of the same |
+** database table. |
+** |
+** If an error occurs, or if the specified row does not exist or does not |
+** contain a blob or text value, then an error code is returned and the |
+** database handle error code and message set. If this happens, then all |
+** subsequent calls to sqlite3_blob_xxx() functions (except blob_close()) |
+** immediately return SQLITE_ABORT. |
+*/ |
+SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){ |
+ int rc; |
+ Incrblob *p = (Incrblob *)pBlob; |
+ sqlite3 *db; |
+ |
+ if( p==0 ) return SQLITE_MISUSE_BKPT; |
+ db = p->db; |
+ sqlite3_mutex_enter(db->mutex); |
+ |
+ if( p->pStmt==0 ){ |
+ /* If there is no statement handle, then the blob-handle has |
+ ** already been invalidated. Return SQLITE_ABORT in this case. |
+ */ |
+ rc = SQLITE_ABORT; |
+ }else{ |
+ char *zErr; |
+ rc = blobSeekToRow(p, iRow, &zErr); |
+ if( rc!=SQLITE_OK ){ |
+ sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr); |
+ sqlite3DbFree(db, zErr); |
+ } |
+ assert( rc!=SQLITE_SCHEMA ); |
+ } |
+ |
+ rc = sqlite3ApiExit(db, rc); |
+ assert( rc==SQLITE_OK || p->pStmt==0 ); |
+ sqlite3_mutex_leave(db->mutex); |
+ return rc; |
+} |
+ |
+#endif /* #ifndef SQLITE_OMIT_INCRBLOB */ |
+ |
+/************** End of vdbeblob.c ********************************************/ |
+/************** Begin file vdbesort.c ****************************************/ |
+/* |
+** 2011-07-09 |
+** |
+** 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. |
+** |
+************************************************************************* |
+** This file contains code for the VdbeSorter object, used in concert with |
+** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements |
+** or by SELECT statements with ORDER BY clauses that cannot be satisfied |
+** using indexes and without LIMIT clauses. |
+** |
+** The VdbeSorter object implements a multi-threaded external merge sort |
+** algorithm that is efficient even if the number of elements being sorted |
+** exceeds the available memory. |
+** |
+** Here is the (internal, non-API) interface between this module and the |
+** rest of the SQLite system: |
+** |
+** sqlite3VdbeSorterInit() Create a new VdbeSorter object. |
+** |
+** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter |
+** object. The row is a binary blob in the |
+** OP_MakeRecord format that contains both |
+** the ORDER BY key columns and result columns |
+** in the case of a SELECT w/ ORDER BY, or |
+** the complete record for an index entry |
+** in the case of a CREATE INDEX. |
+** |
+** sqlite3VdbeSorterRewind() Sort all content previously added. |
+** Position the read cursor on the |
+** first sorted element. |
+** |
+** sqlite3VdbeSorterNext() Advance the read cursor to the next sorted |
+** element. |
+** |
+** sqlite3VdbeSorterRowkey() Return the complete binary blob for the |
+** row currently under the read cursor. |
+** |
+** sqlite3VdbeSorterCompare() Compare the binary blob for the row |
+** currently under the read cursor against |
+** another binary blob X and report if |
+** X is strictly less than the read cursor. |
+** Used to enforce uniqueness in a |
+** CREATE UNIQUE INDEX statement. |
+** |
+** sqlite3VdbeSorterClose() Close the VdbeSorter object and reclaim |
+** all resources. |
+** |
+** sqlite3VdbeSorterReset() Refurbish the VdbeSorter for reuse. This |
+** is like Close() followed by Init() only |
+** much faster. |
+** |
+** The interfaces above must be called in a particular order. Write() can |
+** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and |
+** Compare() can only occur in between Rewind() and Close()/Reset(). i.e. |
+** |
+** Init() |
+** for each record: Write() |
+** Rewind() |
+** Rowkey()/Compare() |
+** Next() |
+** Close() |
+** |
+** Algorithm: |
+** |
+** Records passed to the sorter via calls to Write() are initially held |
+** unsorted in main memory. Assuming the amount of memory used never exceeds |
+** a threshold, when Rewind() is called the set of records is sorted using |
+** an in-memory merge sort. In this case, no temporary files are required |
+** and subsequent calls to Rowkey(), Next() and Compare() read records |
+** directly from main memory. |
+** |
+** If the amount of space used to store records in main memory exceeds the |
+** threshold, then the set of records currently in memory are sorted and |
+** written to a temporary file in "Packed Memory Array" (PMA) format. |
+** A PMA created at this point is known as a "level-0 PMA". Higher levels |
+** of PMAs may be created by merging existing PMAs together - for example |
+** merging two or more level-0 PMAs together creates a level-1 PMA. |
+** |
+** The threshold for the amount of main memory to use before flushing |
+** records to a PMA is roughly the same as the limit configured for the |
+** page-cache of the main database. Specifically, the threshold is set to |
+** the value returned by "PRAGMA main.page_size" multipled by |
+** that returned by "PRAGMA main.cache_size", in bytes. |
+** |
+** If the sorter is running in single-threaded mode, then all PMAs generated |
+** are appended to a single temporary file. Or, if the sorter is running in |
+** multi-threaded mode then up to (N+1) temporary files may be opened, where |
+** N is the configured number of worker threads. In this case, instead of |
+** sorting the records and writing the PMA to a temporary file itself, the |
+** calling thread usually launches a worker thread to do so. Except, if |
+** there are already N worker threads running, the main thread does the work |
+** itself. |
+** |
+** The sorter is running in multi-threaded mode if (a) the library was built |
+** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater |
+** than zero, and (b) worker threads have been enabled at runtime by calling |
+** "PRAGMA threads=N" with some value of N greater than 0. |
+** |
+** When Rewind() is called, any data remaining in memory is flushed to a |
+** final PMA. So at this point the data is stored in some number of sorted |
+** PMAs within temporary files on disk. |
+** |
+** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the |
+** sorter is running in single-threaded mode, then these PMAs are merged |
+** incrementally as keys are retreived from the sorter by the VDBE. The |
+** MergeEngine object, described in further detail below, performs this |
+** merge. |
+** |
+** Or, if running in multi-threaded mode, then a background thread is |
+** launched to merge the existing PMAs. Once the background thread has |
+** merged T bytes of data into a single sorted PMA, the main thread |
+** begins reading keys from that PMA while the background thread proceeds |
+** with merging the next T bytes of data. And so on. |
+** |
+** Parameter T is set to half the value of the memory threshold used |
+** by Write() above to determine when to create a new PMA. |
+** |
+** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when |
+** Rewind() is called, then a hierarchy of incremental-merges is used. |
+** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on |
+** disk are merged together. Then T bytes of data from the second set, and |
+** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT |
+** PMAs at a time. This done is to improve locality. |
+** |
+** If running in multi-threaded mode and there are more than |
+** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more |
+** than one background thread may be created. Specifically, there may be |
+** one background thread for each temporary file on disk, and one background |
+** thread to merge the output of each of the others to a single PMA for |
+** the main thread to read from. |
+*/ |
+/* #include "sqliteInt.h" */ |
+/* #include "vdbeInt.h" */ |
+ |
+/* |
+** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various |
+** messages to stderr that may be helpful in understanding the performance |
+** characteristics of the sorter in multi-threaded mode. |
+*/ |
+#if 0 |
+# define SQLITE_DEBUG_SORTER_THREADS 1 |
+#endif |
+ |
+/* |
+** Hard-coded maximum amount of data to accumulate in memory before flushing |
+** to a level 0 PMA. The purpose of this limit is to prevent various integer |
+** overflows. 512MiB. |
+*/ |
+#define SQLITE_MAX_PMASZ (1<<29) |
+ |
+/* |
+** Private objects used by the sorter |
+*/ |
+typedef struct MergeEngine MergeEngine; /* Merge PMAs together */ |
+typedef struct PmaReader PmaReader; /* Incrementally read one PMA */ |
+typedef struct PmaWriter PmaWriter; /* Incrementally write one PMA */ |
+typedef struct SorterRecord SorterRecord; /* A record being sorted */ |
+typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */ |
+typedef struct SorterFile SorterFile; /* Temporary file object wrapper */ |
+typedef struct SorterList SorterList; /* In-memory list of records */ |
+typedef struct IncrMerger IncrMerger; /* Read & merge multiple PMAs */ |
+ |
+/* |
+** A container for a temp file handle and the current amount of data |
+** stored in the file. |
+*/ |
+struct SorterFile { |
+ sqlite3_file *pFd; /* File handle */ |
+ i64 iEof; /* Bytes of data stored in pFd */ |
+}; |
+ |
+/* |
+** An in-memory list of objects to be sorted. |
+** |
+** If aMemory==0 then each object is allocated separately and the objects |
+** are connected using SorterRecord.u.pNext. If aMemory!=0 then all objects |
+** are stored in the aMemory[] bulk memory, one right after the other, and |
+** are connected using SorterRecord.u.iNext. |
+*/ |
+struct SorterList { |
+ SorterRecord *pList; /* Linked list of records */ |
+ u8 *aMemory; /* If non-NULL, bulk memory to hold pList */ |
+ int szPMA; /* Size of pList as PMA in bytes */ |
+}; |
+ |
+/* |
+** The MergeEngine object is used to combine two or more smaller PMAs into |
+** one big PMA using a merge operation. Separate PMAs all need to be |
+** combined into one big PMA in order to be able to step through the sorted |
+** records in order. |
+** |
+** The aReadr[] array contains a PmaReader object for each of the PMAs being |
+** merged. An aReadr[] object either points to a valid key or else is at EOF. |
+** ("EOF" means "End Of File". When aReadr[] is at EOF there is no more data.) |
+** For the purposes of the paragraphs below, we assume that the array is |
+** actually N elements in size, where N is the smallest power of 2 greater |
+** to or equal to the number of PMAs being merged. The extra aReadr[] elements |
+** are treated as if they are empty (always at EOF). |
+** |
+** The aTree[] array is also N elements in size. The value of N is stored in |
+** the MergeEngine.nTree variable. |
+** |
+** The final (N/2) elements of aTree[] contain the results of comparing |
+** pairs of PMA keys together. Element i contains the result of |
+** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the |
+** aTree element is set to the index of it. |
+** |
+** For the purposes of this comparison, EOF is considered greater than any |
+** other key value. If the keys are equal (only possible with two EOF |
+** values), it doesn't matter which index is stored. |
+** |
+** The (N/4) elements of aTree[] that precede the final (N/2) described |
+** above contains the index of the smallest of each block of 4 PmaReaders |
+** And so on. So that aTree[1] contains the index of the PmaReader that |
+** currently points to the smallest key value. aTree[0] is unused. |
+** |
+** Example: |
+** |
+** aReadr[0] -> Banana |
+** aReadr[1] -> Feijoa |
+** aReadr[2] -> Elderberry |
+** aReadr[3] -> Currant |
+** aReadr[4] -> Grapefruit |
+** aReadr[5] -> Apple |
+** aReadr[6] -> Durian |
+** aReadr[7] -> EOF |
+** |
+** aTree[] = { X, 5 0, 5 0, 3, 5, 6 } |
+** |
+** The current element is "Apple" (the value of the key indicated by |
+** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will |
+** be advanced to the next key in its segment. Say the next key is |
+** "Eggplant": |
+** |
+** aReadr[5] -> Eggplant |
+** |
+** The contents of aTree[] are updated first by comparing the new PmaReader |
+** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader |
+** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree. |
+** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader |
+** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian), |
+** so the value written into element 1 of the array is 0. As follows: |
+** |
+** aTree[] = { X, 0 0, 6 0, 3, 5, 6 } |
+** |
+** In other words, each time we advance to the next sorter element, log2(N) |
+** key comparison operations are required, where N is the number of segments |
+** being merged (rounded up to the next power of 2). |
+*/ |
+struct MergeEngine { |
+ int nTree; /* Used size of aTree/aReadr (power of 2) */ |
+ SortSubtask *pTask; /* Used by this thread only */ |
+ int *aTree; /* Current state of incremental merge */ |
+ PmaReader *aReadr; /* Array of PmaReaders to merge data from */ |
+}; |
+ |
+/* |
+** This object represents a single thread of control in a sort operation. |
+** Exactly VdbeSorter.nTask instances of this object are allocated |
+** as part of each VdbeSorter object. Instances are never allocated any |
+** other way. VdbeSorter.nTask is set to the number of worker threads allowed |
+** (see SQLITE_CONFIG_WORKER_THREADS) plus one (the main thread). Thus for |
+** single-threaded operation, there is exactly one instance of this object |
+** and for multi-threaded operation there are two or more instances. |
+** |
+** Essentially, this structure contains all those fields of the VdbeSorter |
+** structure for which each thread requires a separate instance. For example, |
+** each thread requries its own UnpackedRecord object to unpack records in |
+** as part of comparison operations. |
+** |
+** Before a background thread is launched, variable bDone is set to 0. Then, |
+** right before it exits, the thread itself sets bDone to 1. This is used for |
+** two purposes: |
+** |
+** 1. When flushing the contents of memory to a level-0 PMA on disk, to |
+** attempt to select a SortSubtask for which there is not already an |
+** active background thread (since doing so causes the main thread |
+** to block until it finishes). |
+** |
+** 2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call |
+** to sqlite3ThreadJoin() is likely to block. Cases that are likely to |
+** block provoke debugging output. |
+** |
+** In both cases, the effects of the main thread seeing (bDone==0) even |
+** after the thread has finished are not dire. So we don't worry about |
+** memory barriers and such here. |
+*/ |
+typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int); |
+struct SortSubtask { |
+ SQLiteThread *pThread; /* Background thread, if any */ |
+ int bDone; /* Set if thread is finished but not joined */ |
+ VdbeSorter *pSorter; /* Sorter that owns this sub-task */ |
+ UnpackedRecord *pUnpacked; /* Space to unpack a record */ |
+ SorterList list; /* List for thread to write to a PMA */ |
+ int nPMA; /* Number of PMAs currently in file */ |
+ SorterCompare xCompare; /* Compare function to use */ |
+ SorterFile file; /* Temp file for level-0 PMAs */ |
+ SorterFile file2; /* Space for other PMAs */ |
+}; |
+ |
+ |
+/* |
+** Main sorter structure. A single instance of this is allocated for each |
+** sorter cursor created by the VDBE. |
+** |
+** mxKeysize: |
+** As records are added to the sorter by calls to sqlite3VdbeSorterWrite(), |
+** this variable is updated so as to be set to the size on disk of the |
+** largest record in the sorter. |
+*/ |
+struct VdbeSorter { |
+ int mnPmaSize; /* Minimum PMA size, in bytes */ |
+ int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */ |
+ int mxKeysize; /* Largest serialized key seen so far */ |
+ int pgsz; /* Main database page size */ |
+ PmaReader *pReader; /* Readr data from here after Rewind() */ |
+ MergeEngine *pMerger; /* Or here, if bUseThreads==0 */ |
+ sqlite3 *db; /* Database connection */ |
+ KeyInfo *pKeyInfo; /* How to compare records */ |
+ UnpackedRecord *pUnpacked; /* Used by VdbeSorterCompare() */ |
+ SorterList list; /* List of in-memory records */ |
+ int iMemory; /* Offset of free space in list.aMemory */ |
+ int nMemory; /* Size of list.aMemory allocation in bytes */ |
+ u8 bUsePMA; /* True if one or more PMAs created */ |
+ u8 bUseThreads; /* True to use background threads */ |
+ u8 iPrev; /* Previous thread used to flush PMA */ |
+ u8 nTask; /* Size of aTask[] array */ |
+ u8 typeMask; |
+ SortSubtask aTask[1]; /* One or more subtasks */ |
+}; |
+ |
+#define SORTER_TYPE_INTEGER 0x01 |
+#define SORTER_TYPE_TEXT 0x02 |
+ |
+/* |
+** An instance of the following object is used to read records out of a |
+** PMA, in sorted order. The next key to be read is cached in nKey/aKey. |
+** aKey might point into aMap or into aBuffer. If neither of those locations |
+** contain a contiguous representation of the key, then aAlloc is allocated |
+** and the key is copied into aAlloc and aKey is made to poitn to aAlloc. |
+** |
+** pFd==0 at EOF. |
+*/ |
+struct PmaReader { |
+ i64 iReadOff; /* Current read offset */ |
+ i64 iEof; /* 1 byte past EOF for this PmaReader */ |
+ int nAlloc; /* Bytes of space at aAlloc */ |
+ int nKey; /* Number of bytes in key */ |
+ sqlite3_file *pFd; /* File handle we are reading from */ |
+ u8 *aAlloc; /* Space for aKey if aBuffer and pMap wont work */ |
+ u8 *aKey; /* Pointer to current key */ |
+ u8 *aBuffer; /* Current read buffer */ |
+ int nBuffer; /* Size of read buffer in bytes */ |
+ u8 *aMap; /* Pointer to mapping of entire file */ |
+ IncrMerger *pIncr; /* Incremental merger */ |
+}; |
+ |
+/* |
+** Normally, a PmaReader object iterates through an existing PMA stored |
+** within a temp file. However, if the PmaReader.pIncr variable points to |
+** an object of the following type, it may be used to iterate/merge through |
+** multiple PMAs simultaneously. |
+** |
+** There are two types of IncrMerger object - single (bUseThread==0) and |
+** multi-threaded (bUseThread==1). |
+** |
+** A multi-threaded IncrMerger object uses two temporary files - aFile[0] |
+** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in |
+** size. When the IncrMerger is initialized, it reads enough data from |
+** pMerger to populate aFile[0]. It then sets variables within the |
+** corresponding PmaReader object to read from that file and kicks off |
+** a background thread to populate aFile[1] with the next mxSz bytes of |
+** sorted record data from pMerger. |
+** |
+** When the PmaReader reaches the end of aFile[0], it blocks until the |
+** background thread has finished populating aFile[1]. It then exchanges |
+** the contents of the aFile[0] and aFile[1] variables within this structure, |
+** sets the PmaReader fields to read from the new aFile[0] and kicks off |
+** another background thread to populate the new aFile[1]. And so on, until |
+** the contents of pMerger are exhausted. |
+** |
+** A single-threaded IncrMerger does not open any temporary files of its |
+** own. Instead, it has exclusive access to mxSz bytes of space beginning |
+** at offset iStartOff of file pTask->file2. And instead of using a |
+** background thread to prepare data for the PmaReader, with a single |
+** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with |
+** keys from pMerger by the calling thread whenever the PmaReader runs out |
+** of data. |
+*/ |
+struct IncrMerger { |
+ SortSubtask *pTask; /* Task that owns this merger */ |
+ MergeEngine *pMerger; /* Merge engine thread reads data from */ |
+ i64 iStartOff; /* Offset to start writing file at */ |
+ int mxSz; /* Maximum bytes of data to store */ |
+ int bEof; /* Set to true when merge is finished */ |
+ int bUseThread; /* True to use a bg thread for this object */ |
+ SorterFile aFile[2]; /* aFile[0] for reading, [1] for writing */ |
+}; |
+ |
+/* |
+** An instance of this object is used for writing a PMA. |
+** |
+** The PMA is written one record at a time. Each record is of an arbitrary |
+** size. But I/O is more efficient if it occurs in page-sized blocks where |
+** each block is aligned on a page boundary. This object caches writes to |
+** the PMA so that aligned, page-size blocks are written. |
+*/ |
+struct PmaWriter { |
+ int eFWErr; /* Non-zero if in an error state */ |
+ u8 *aBuffer; /* Pointer to write buffer */ |
+ int nBuffer; /* Size of write buffer in bytes */ |
+ int iBufStart; /* First byte of buffer to write */ |
+ int iBufEnd; /* Last byte of buffer to write */ |
+ i64 iWriteOff; /* Offset of start of buffer in file */ |
+ sqlite3_file *pFd; /* File handle to write to */ |
+}; |
+ |
+/* |
+** This object is the header on a single record while that record is being |
+** held in memory and prior to being written out as part of a PMA. |
+** |
+** How the linked list is connected depends on how memory is being managed |
+** by this module. If using a separate allocation for each in-memory record |
+** (VdbeSorter.list.aMemory==0), then the list is always connected using the |
+** SorterRecord.u.pNext pointers. |
+** |
+** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0), |
+** then while records are being accumulated the list is linked using the |
+** SorterRecord.u.iNext offset. This is because the aMemory[] array may |
+** be sqlite3Realloc()ed while records are being accumulated. Once the VM |
+** has finished passing records to the sorter, or when the in-memory buffer |
+** is full, the list is sorted. As part of the sorting process, it is |
+** converted to use the SorterRecord.u.pNext pointers. See function |
+** vdbeSorterSort() for details. |
+*/ |
+struct SorterRecord { |
+ int nVal; /* Size of the record in bytes */ |
+ union { |
+ SorterRecord *pNext; /* Pointer to next record in list */ |
+ int iNext; /* Offset within aMemory of next record */ |
+ } u; |
+ /* The data for the record immediately follows this header */ |
+}; |
+ |
+/* Return a pointer to the buffer containing the record data for SorterRecord |
+** object p. Should be used as if: |
+** |
+** void *SRVAL(SorterRecord *p) { return (void*)&p[1]; } |
+*/ |
+#define SRVAL(p) ((void*)((SorterRecord*)(p) + 1)) |
+ |
+ |
+/* Maximum number of PMAs that a single MergeEngine can merge */ |
+#define SORTER_MAX_MERGE_COUNT 16 |
+ |
+static int vdbeIncrSwap(IncrMerger*); |
+static void vdbeIncrFree(IncrMerger *); |
+ |
+/* |
+** Free all memory belonging to the PmaReader object passed as the |
+** argument. All structure fields are set to zero before returning. |
+*/ |
+static void vdbePmaReaderClear(PmaReader *pReadr){ |
+ sqlite3_free(pReadr->aAlloc); |
+ sqlite3_free(pReadr->aBuffer); |
+ if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap); |
+ vdbeIncrFree(pReadr->pIncr); |
+ memset(pReadr, 0, sizeof(PmaReader)); |
+} |
+ |
+/* |
+** Read the next nByte bytes of data from the PMA p. |
+** If successful, set *ppOut to point to a buffer containing the data |
+** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite |
+** error code. |
+** |
+** The buffer returned in *ppOut is only valid until the |
+** next call to this function. |
+*/ |
+static int vdbePmaReadBlob( |
+ PmaReader *p, /* PmaReader from which to take the blob */ |
+ int nByte, /* Bytes of data to read */ |
+ u8 **ppOut /* OUT: Pointer to buffer containing data */ |
+){ |
+ int iBuf; /* Offset within buffer to read from */ |
+ int nAvail; /* Bytes of data available in buffer */ |
+ |
+ if( p->aMap ){ |
+ *ppOut = &p->aMap[p->iReadOff]; |
+ p->iReadOff += nByte; |
+ return SQLITE_OK; |
+ } |
+ |
+ assert( p->aBuffer ); |
+ |
+ /* If there is no more data to be read from the buffer, read the next |
+ ** p->nBuffer bytes of data from the file into it. Or, if there are less |
+ ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */ |
+ iBuf = p->iReadOff % p->nBuffer; |
+ if( iBuf==0 ){ |
+ int nRead; /* Bytes to read from disk */ |
+ int rc; /* sqlite3OsRead() return code */ |
+ |
+ /* Determine how many bytes of data to read. */ |
+ if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){ |
+ nRead = p->nBuffer; |
+ }else{ |
+ nRead = (int)(p->iEof - p->iReadOff); |
+ } |
+ assert( nRead>0 ); |
+ |
+ /* Readr data from the file. Return early if an error occurs. */ |
+ rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff); |
+ assert( rc!=SQLITE_IOERR_SHORT_READ ); |
+ if( rc!=SQLITE_OK ) return rc; |
+ } |
+ nAvail = p->nBuffer - iBuf; |
+ |
+ if( nByte<=nAvail ){ |
+ /* The requested data is available in the in-memory buffer. In this |
+ ** case there is no need to make a copy of the data, just return a |
+ ** pointer into the buffer to the caller. */ |
+ *ppOut = &p->aBuffer[iBuf]; |
+ p->iReadOff += nByte; |
+ }else{ |
+ /* The requested data is not all available in the in-memory buffer. |
+ ** In this case, allocate space at p->aAlloc[] to copy the requested |
+ ** range into. Then return a copy of pointer p->aAlloc to the caller. */ |
+ int nRem; /* Bytes remaining to copy */ |
+ |
+ /* Extend the p->aAlloc[] allocation if required. */ |
+ if( p->nAlloc<nByte ){ |
+ u8 *aNew; |
+ int nNew = MAX(128, p->nAlloc*2); |
+ while( nByte>nNew ) nNew = nNew*2; |
+ aNew = sqlite3Realloc(p->aAlloc, nNew); |
+ if( !aNew ) return SQLITE_NOMEM_BKPT; |
+ p->nAlloc = nNew; |
+ p->aAlloc = aNew; |
+ } |
+ |
+ /* Copy as much data as is available in the buffer into the start of |
+ ** p->aAlloc[]. */ |
+ memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail); |
+ p->iReadOff += nAvail; |
+ nRem = nByte - nAvail; |
+ |
+ /* The following loop copies up to p->nBuffer bytes per iteration into |
+ ** the p->aAlloc[] buffer. */ |
+ while( nRem>0 ){ |
+ int rc; /* vdbePmaReadBlob() return code */ |
+ int nCopy; /* Number of bytes to copy */ |
+ u8 *aNext; /* Pointer to buffer to copy data from */ |
+ |
+ nCopy = nRem; |
+ if( nRem>p->nBuffer ) nCopy = p->nBuffer; |
+ rc = vdbePmaReadBlob(p, nCopy, &aNext); |
+ if( rc!=SQLITE_OK ) return rc; |
+ assert( aNext!=p->aAlloc ); |
+ memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy); |
+ nRem -= nCopy; |
+ } |
+ |
+ *ppOut = p->aAlloc; |
+ } |
+ |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Read a varint from the stream of data accessed by p. Set *pnOut to |
+** the value read. |
+*/ |
+static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){ |
+ int iBuf; |
+ |
+ if( p->aMap ){ |
+ p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut); |
+ }else{ |
+ iBuf = p->iReadOff % p->nBuffer; |
+ if( iBuf && (p->nBuffer-iBuf)>=9 ){ |
+ p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut); |
+ }else{ |
+ u8 aVarint[16], *a; |
+ int i = 0, rc; |
+ do{ |
+ rc = vdbePmaReadBlob(p, 1, &a); |
+ if( rc ) return rc; |
+ aVarint[(i++)&0xf] = a[0]; |
+ }while( (a[0]&0x80)!=0 ); |
+ sqlite3GetVarint(aVarint, pnOut); |
+ } |
+ } |
+ |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Attempt to memory map file pFile. If successful, set *pp to point to the |
+** new mapping and return SQLITE_OK. If the mapping is not attempted |
+** (because the file is too large or the VFS layer is configured not to use |
+** mmap), return SQLITE_OK and set *pp to NULL. |
+** |
+** Or, if an error occurs, return an SQLite error code. The final value of |
+** *pp is undefined in this case. |
+*/ |
+static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){ |
+ int rc = SQLITE_OK; |
+ if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){ |
+ sqlite3_file *pFd = pFile->pFd; |
+ if( pFd->pMethods->iVersion>=3 ){ |
+ rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp); |
+ testcase( rc!=SQLITE_OK ); |
+ } |
+ } |
+ return rc; |
+} |
+ |
+/* |
+** Attach PmaReader pReadr to file pFile (if it is not already attached to |
+** that file) and seek it to offset iOff within the file. Return SQLITE_OK |
+** if successful, or an SQLite error code if an error occurs. |
+*/ |
+static int vdbePmaReaderSeek( |
+ SortSubtask *pTask, /* Task context */ |
+ PmaReader *pReadr, /* Reader whose cursor is to be moved */ |
+ SorterFile *pFile, /* Sorter file to read from */ |
+ i64 iOff /* Offset in pFile */ |
+){ |
+ int rc = SQLITE_OK; |
+ |
+ assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 ); |
+ |
+ if( sqlite3FaultSim(201) ) return SQLITE_IOERR_READ; |
+ if( pReadr->aMap ){ |
+ sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap); |
+ pReadr->aMap = 0; |
+ } |
+ pReadr->iReadOff = iOff; |
+ pReadr->iEof = pFile->iEof; |
+ pReadr->pFd = pFile->pFd; |
+ |
+ rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap); |
+ if( rc==SQLITE_OK && pReadr->aMap==0 ){ |
+ int pgsz = pTask->pSorter->pgsz; |
+ int iBuf = pReadr->iReadOff % pgsz; |
+ if( pReadr->aBuffer==0 ){ |
+ pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz); |
+ if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT; |
+ pReadr->nBuffer = pgsz; |
+ } |
+ if( rc==SQLITE_OK && iBuf ){ |
+ int nRead = pgsz - iBuf; |
+ if( (pReadr->iReadOff + nRead) > pReadr->iEof ){ |
+ nRead = (int)(pReadr->iEof - pReadr->iReadOff); |
+ } |
+ rc = sqlite3OsRead( |
+ pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff |
+ ); |
+ testcase( rc!=SQLITE_OK ); |
+ } |
+ } |
+ |
+ return rc; |
+} |
+ |
+/* |
+** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if |
+** no error occurs, or an SQLite error code if one does. |
+*/ |
+static int vdbePmaReaderNext(PmaReader *pReadr){ |
+ int rc = SQLITE_OK; /* Return Code */ |
+ u64 nRec = 0; /* Size of record in bytes */ |
+ |
+ |
+ if( pReadr->iReadOff>=pReadr->iEof ){ |
+ IncrMerger *pIncr = pReadr->pIncr; |
+ int bEof = 1; |
+ if( pIncr ){ |
+ rc = vdbeIncrSwap(pIncr); |
+ if( rc==SQLITE_OK && pIncr->bEof==0 ){ |
+ rc = vdbePmaReaderSeek( |
+ pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff |
+ ); |
+ bEof = 0; |
+ } |
+ } |
+ |
+ if( bEof ){ |
+ /* This is an EOF condition */ |
+ vdbePmaReaderClear(pReadr); |
+ testcase( rc!=SQLITE_OK ); |
+ return rc; |
+ } |
+ } |
+ |
+ if( rc==SQLITE_OK ){ |
+ rc = vdbePmaReadVarint(pReadr, &nRec); |
+ } |
+ if( rc==SQLITE_OK ){ |
+ pReadr->nKey = (int)nRec; |
+ rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey); |
+ testcase( rc!=SQLITE_OK ); |
+ } |
+ |
+ return rc; |
+} |
+ |
+/* |
+** Initialize PmaReader pReadr to scan through the PMA stored in file pFile |
+** starting at offset iStart and ending at offset iEof-1. This function |
+** leaves the PmaReader pointing to the first key in the PMA (or EOF if the |
+** PMA is empty). |
+** |
+** If the pnByte parameter is NULL, then it is assumed that the file |
+** contains a single PMA, and that that PMA omits the initial length varint. |
+*/ |
+static int vdbePmaReaderInit( |
+ SortSubtask *pTask, /* Task context */ |
+ SorterFile *pFile, /* Sorter file to read from */ |
+ i64 iStart, /* Start offset in pFile */ |
+ PmaReader *pReadr, /* PmaReader to populate */ |
+ i64 *pnByte /* IN/OUT: Increment this value by PMA size */ |
+){ |
+ int rc; |
+ |
+ assert( pFile->iEof>iStart ); |
+ assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 ); |
+ assert( pReadr->aBuffer==0 ); |
+ assert( pReadr->aMap==0 ); |
+ |
+ rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iStart); |
+ if( rc==SQLITE_OK ){ |
+ u64 nByte = 0; /* Size of PMA in bytes */ |
+ rc = vdbePmaReadVarint(pReadr, &nByte); |
+ pReadr->iEof = pReadr->iReadOff + nByte; |
+ *pnByte += nByte; |
+ } |
+ |
+ if( rc==SQLITE_OK ){ |
+ rc = vdbePmaReaderNext(pReadr); |
+ } |
+ return rc; |
+} |
+ |
+/* |
+** A version of vdbeSorterCompare() that assumes that it has already been |
+** determined that the first field of key1 is equal to the first field of |
+** key2. |
+*/ |
+static int vdbeSorterCompareTail( |
+ SortSubtask *pTask, /* Subtask context (for pKeyInfo) */ |
+ int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */ |
+ const void *pKey1, int nKey1, /* Left side of comparison */ |
+ const void *pKey2, int nKey2 /* Right side of comparison */ |
+){ |
+ UnpackedRecord *r2 = pTask->pUnpacked; |
+ if( *pbKey2Cached==0 ){ |
+ sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2); |
+ *pbKey2Cached = 1; |
+ } |
+ return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1); |
+} |
+ |
+/* |
+** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, |
+** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences |
+** used by the comparison. Return the result of the comparison. |
+** |
+** If IN/OUT parameter *pbKey2Cached is true when this function is called, |
+** it is assumed that (pTask->pUnpacked) contains the unpacked version |
+** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked |
+** version of key2 and *pbKey2Cached set to true before returning. |
+** |
+** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set |
+** to SQLITE_NOMEM. |
+*/ |
+static int vdbeSorterCompare( |
+ SortSubtask *pTask, /* Subtask context (for pKeyInfo) */ |
+ int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */ |
+ const void *pKey1, int nKey1, /* Left side of comparison */ |
+ const void *pKey2, int nKey2 /* Right side of comparison */ |
+){ |
+ UnpackedRecord *r2 = pTask->pUnpacked; |
+ if( !*pbKey2Cached ){ |
+ sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2); |
+ *pbKey2Cached = 1; |
+ } |
+ return sqlite3VdbeRecordCompare(nKey1, pKey1, r2); |
+} |
+ |
+/* |
+** A specially optimized version of vdbeSorterCompare() that assumes that |
+** the first field of each key is a TEXT value and that the collation |
+** sequence to compare them with is BINARY. |
+*/ |
+static int vdbeSorterCompareText( |
+ SortSubtask *pTask, /* Subtask context (for pKeyInfo) */ |
+ int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */ |
+ const void *pKey1, int nKey1, /* Left side of comparison */ |
+ const void *pKey2, int nKey2 /* Right side of comparison */ |
+){ |
+ const u8 * const p1 = (const u8 * const)pKey1; |
+ const u8 * const p2 = (const u8 * const)pKey2; |
+ const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */ |
+ const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */ |
+ |
+ int n1; |
+ int n2; |
+ int res; |
+ |
+ getVarint32(&p1[1], n1); n1 = (n1 - 13) / 2; |
+ getVarint32(&p2[1], n2); n2 = (n2 - 13) / 2; |
+ res = memcmp(v1, v2, MIN(n1, n2)); |
+ if( res==0 ){ |
+ res = n1 - n2; |
+ } |
+ |
+ if( res==0 ){ |
+ if( pTask->pSorter->pKeyInfo->nField>1 ){ |
+ res = vdbeSorterCompareTail( |
+ pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2 |
+ ); |
+ } |
+ }else{ |
+ if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){ |
+ res = res * -1; |
+ } |
+ } |
+ |
+ return res; |
+} |
+ |
+/* |
+** A specially optimized version of vdbeSorterCompare() that assumes that |
+** the first field of each key is an INTEGER value. |
+*/ |
+static int vdbeSorterCompareInt( |
+ SortSubtask *pTask, /* Subtask context (for pKeyInfo) */ |
+ int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */ |
+ const void *pKey1, int nKey1, /* Left side of comparison */ |
+ const void *pKey2, int nKey2 /* Right side of comparison */ |
+){ |
+ const u8 * const p1 = (const u8 * const)pKey1; |
+ const u8 * const p2 = (const u8 * const)pKey2; |
+ const int s1 = p1[1]; /* Left hand serial type */ |
+ const int s2 = p2[1]; /* Right hand serial type */ |
+ const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */ |
+ const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */ |
+ int res; /* Return value */ |
+ |
+ assert( (s1>0 && s1<7) || s1==8 || s1==9 ); |
+ assert( (s2>0 && s2<7) || s2==8 || s2==9 ); |
+ |
+ if( s1>7 && s2>7 ){ |
+ res = s1 - s2; |
+ }else{ |
+ if( s1==s2 ){ |
+ if( (*v1 ^ *v2) & 0x80 ){ |
+ /* The two values have different signs */ |
+ res = (*v1 & 0x80) ? -1 : +1; |
+ }else{ |
+ /* The two values have the same sign. Compare using memcmp(). */ |
+ static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8 }; |
+ int i; |
+ res = 0; |
+ for(i=0; i<aLen[s1]; i++){ |
+ if( (res = v1[i] - v2[i]) ) break; |
+ } |
+ } |
+ }else{ |
+ if( s2>7 ){ |
+ res = +1; |
+ }else if( s1>7 ){ |
+ res = -1; |
+ }else{ |
+ res = s1 - s2; |
+ } |
+ assert( res!=0 ); |
+ |
+ if( res>0 ){ |
+ if( *v1 & 0x80 ) res = -1; |
+ }else{ |
+ if( *v2 & 0x80 ) res = +1; |
+ } |
+ } |
+ } |
+ |
+ if( res==0 ){ |
+ if( pTask->pSorter->pKeyInfo->nField>1 ){ |
+ res = vdbeSorterCompareTail( |
+ pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2 |
+ ); |
+ } |
+ }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){ |
+ res = res * -1; |
+ } |
+ |
+ return res; |
+} |
+ |
+/* |
+** Initialize the temporary index cursor just opened as a sorter cursor. |
+** |
+** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nField) |
+** to determine the number of fields that should be compared from the |
+** records being sorted. However, if the value passed as argument nField |
+** is non-zero and the sorter is able to guarantee a stable sort, nField |
+** is used instead. This is used when sorting records for a CREATE INDEX |
+** statement. In this case, keys are always delivered to the sorter in |
+** order of the primary key, which happens to be make up the final part |
+** of the records being sorted. So if the sort is stable, there is never |
+** any reason to compare PK fields and they can be ignored for a small |
+** performance boost. |
+** |
+** The sorter can guarantee a stable sort when running in single-threaded |
+** mode, but not in multi-threaded mode. |
+** |
+** SQLITE_OK is returned if successful, or an SQLite error code otherwise. |
+*/ |
+SQLITE_PRIVATE int sqlite3VdbeSorterInit( |
+ sqlite3 *db, /* Database connection (for malloc()) */ |
+ int nField, /* Number of key fields in each record */ |
+ VdbeCursor *pCsr /* Cursor that holds the new sorter */ |
+){ |
+ int pgsz; /* Page size of main database */ |
+ int i; /* Used to iterate through aTask[] */ |
+ VdbeSorter *pSorter; /* The new sorter */ |
+ KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */ |
+ int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */ |
+ int sz; /* Size of pSorter in bytes */ |
+ int rc = SQLITE_OK; |
+#if SQLITE_MAX_WORKER_THREADS==0 |
+# define nWorker 0 |
+#else |
+ int nWorker; |
+#endif |
+ |
+ /* Initialize the upper limit on the number of worker threads */ |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+ if( sqlite3TempInMemory(db) || sqlite3GlobalConfig.bCoreMutex==0 ){ |
+ nWorker = 0; |
+ }else{ |
+ nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS]; |
+ } |
+#endif |
+ |
+ /* Do not allow the total number of threads (main thread + all workers) |
+ ** to exceed the maximum merge count */ |
+#if SQLITE_MAX_WORKER_THREADS>=SORTER_MAX_MERGE_COUNT |
+ if( nWorker>=SORTER_MAX_MERGE_COUNT ){ |
+ nWorker = SORTER_MAX_MERGE_COUNT-1; |
+ } |
+#endif |
+ |
+ assert( pCsr->pKeyInfo && pCsr->pBtx==0 ); |
+ assert( pCsr->eCurType==CURTYPE_SORTER ); |
+ szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nField-1)*sizeof(CollSeq*); |
+ sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask); |
+ |
+ pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo); |
+ pCsr->uc.pSorter = pSorter; |
+ if( pSorter==0 ){ |
+ rc = SQLITE_NOMEM_BKPT; |
+ }else{ |
+ pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz); |
+ memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo); |
+ pKeyInfo->db = 0; |
+ if( nField && nWorker==0 ){ |
+ pKeyInfo->nXField += (pKeyInfo->nField - nField); |
+ pKeyInfo->nField = nField; |
+ } |
+ pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt); |
+ pSorter->nTask = nWorker + 1; |
+ pSorter->iPrev = (u8)(nWorker - 1); |
+ pSorter->bUseThreads = (pSorter->nTask>1); |
+ pSorter->db = db; |
+ for(i=0; i<pSorter->nTask; i++){ |
+ SortSubtask *pTask = &pSorter->aTask[i]; |
+ pTask->pSorter = pSorter; |
+ } |
+ |
+ if( !sqlite3TempInMemory(db) ){ |
+ i64 mxCache; /* Cache size in bytes*/ |
+ u32 szPma = sqlite3GlobalConfig.szPma; |
+ pSorter->mnPmaSize = szPma * pgsz; |
+ |
+ mxCache = db->aDb[0].pSchema->cache_size; |
+ if( mxCache<0 ){ |
+ /* A negative cache-size value C indicates that the cache is abs(C) |
+ ** KiB in size. */ |
+ mxCache = mxCache * -1024; |
+ }else{ |
+ mxCache = mxCache * pgsz; |
+ } |
+ mxCache = MIN(mxCache, SQLITE_MAX_PMASZ); |
+ pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache); |
+ |
+ /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of |
+ ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary |
+ ** large heap allocations. |
+ */ |
+ if( sqlite3GlobalConfig.pScratch==0 ){ |
+ assert( pSorter->iMemory==0 ); |
+ pSorter->nMemory = pgsz; |
+ pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz); |
+ if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT; |
+ } |
+ } |
+ |
+ if( (pKeyInfo->nField+pKeyInfo->nXField)<13 |
+ && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl) |
+ ){ |
+ pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT; |
+ } |
+ } |
+ |
+ return rc; |
+} |
+#undef nWorker /* Defined at the top of this function */ |
+ |
+/* |
+** Free the list of sorted records starting at pRecord. |
+*/ |
+static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){ |
+ SorterRecord *p; |
+ SorterRecord *pNext; |
+ for(p=pRecord; p; p=pNext){ |
+ pNext = p->u.pNext; |
+ sqlite3DbFree(db, p); |
+ } |
+} |
+ |
+/* |
+** Free all resources owned by the object indicated by argument pTask. All |
+** fields of *pTask are zeroed before returning. |
+*/ |
+static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){ |
+ sqlite3DbFree(db, pTask->pUnpacked); |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+ /* pTask->list.aMemory can only be non-zero if it was handed memory |
+ ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */ |
+ if( pTask->list.aMemory ){ |
+ sqlite3_free(pTask->list.aMemory); |
+ }else |
+#endif |
+ { |
+ assert( pTask->list.aMemory==0 ); |
+ vdbeSorterRecordFree(0, pTask->list.pList); |
+ } |
+ if( pTask->file.pFd ){ |
+ sqlite3OsCloseFree(pTask->file.pFd); |
+ } |
+ if( pTask->file2.pFd ){ |
+ sqlite3OsCloseFree(pTask->file2.pFd); |
+ } |
+ memset(pTask, 0, sizeof(SortSubtask)); |
+} |
+ |
+#ifdef SQLITE_DEBUG_SORTER_THREADS |
+static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){ |
+ i64 t; |
+ int iTask = (pTask - pTask->pSorter->aTask); |
+ sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t); |
+ fprintf(stderr, "%lld:%d %s\n", t, iTask, zEvent); |
+} |
+static void vdbeSorterRewindDebug(const char *zEvent){ |
+ i64 t; |
+ sqlite3OsCurrentTimeInt64(sqlite3_vfs_find(0), &t); |
+ fprintf(stderr, "%lld:X %s\n", t, zEvent); |
+} |
+static void vdbeSorterPopulateDebug( |
+ SortSubtask *pTask, |
+ const char *zEvent |
+){ |
+ i64 t; |
+ int iTask = (pTask - pTask->pSorter->aTask); |
+ sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t); |
+ fprintf(stderr, "%lld:bg%d %s\n", t, iTask, zEvent); |
+} |
+static void vdbeSorterBlockDebug( |
+ SortSubtask *pTask, |
+ int bBlocked, |
+ const char *zEvent |
+){ |
+ if( bBlocked ){ |
+ i64 t; |
+ sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t); |
+ fprintf(stderr, "%lld:main %s\n", t, zEvent); |
+ } |
+} |
+#else |
+# define vdbeSorterWorkDebug(x,y) |
+# define vdbeSorterRewindDebug(y) |
+# define vdbeSorterPopulateDebug(x,y) |
+# define vdbeSorterBlockDebug(x,y,z) |
+#endif |
+ |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+/* |
+** Join thread pTask->thread. |
+*/ |
+static int vdbeSorterJoinThread(SortSubtask *pTask){ |
+ int rc = SQLITE_OK; |
+ if( pTask->pThread ){ |
+#ifdef SQLITE_DEBUG_SORTER_THREADS |
+ int bDone = pTask->bDone; |
+#endif |
+ void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR); |
+ vdbeSorterBlockDebug(pTask, !bDone, "enter"); |
+ (void)sqlite3ThreadJoin(pTask->pThread, &pRet); |
+ vdbeSorterBlockDebug(pTask, !bDone, "exit"); |
+ rc = SQLITE_PTR_TO_INT(pRet); |
+ assert( pTask->bDone==1 ); |
+ pTask->bDone = 0; |
+ pTask->pThread = 0; |
+ } |
+ return rc; |
+} |
+ |
+/* |
+** Launch a background thread to run xTask(pIn). |
+*/ |
+static int vdbeSorterCreateThread( |
+ SortSubtask *pTask, /* Thread will use this task object */ |
+ void *(*xTask)(void*), /* Routine to run in a separate thread */ |
+ void *pIn /* Argument passed into xTask() */ |
+){ |
+ assert( pTask->pThread==0 && pTask->bDone==0 ); |
+ return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn); |
+} |
+ |
+/* |
+** Join all outstanding threads launched by SorterWrite() to create |
+** level-0 PMAs. |
+*/ |
+static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){ |
+ int rc = rcin; |
+ int i; |
+ |
+ /* This function is always called by the main user thread. |
+ ** |
+ ** If this function is being called after SorterRewind() has been called, |
+ ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread |
+ ** is currently attempt to join one of the other threads. To avoid a race |
+ ** condition where this thread also attempts to join the same object, join |
+ ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */ |
+ for(i=pSorter->nTask-1; i>=0; i--){ |
+ SortSubtask *pTask = &pSorter->aTask[i]; |
+ int rc2 = vdbeSorterJoinThread(pTask); |
+ if( rc==SQLITE_OK ) rc = rc2; |
+ } |
+ return rc; |
+} |
+#else |
+# define vdbeSorterJoinAll(x,rcin) (rcin) |
+# define vdbeSorterJoinThread(pTask) SQLITE_OK |
+#endif |
+ |
+/* |
+** Allocate a new MergeEngine object capable of handling up to |
+** nReader PmaReader inputs. |
+** |
+** nReader is automatically rounded up to the next power of two. |
+** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up. |
+*/ |
+static MergeEngine *vdbeMergeEngineNew(int nReader){ |
+ int N = 2; /* Smallest power of two >= nReader */ |
+ int nByte; /* Total bytes of space to allocate */ |
+ MergeEngine *pNew; /* Pointer to allocated object to return */ |
+ |
+ assert( nReader<=SORTER_MAX_MERGE_COUNT ); |
+ |
+ while( N<nReader ) N += N; |
+ nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader)); |
+ |
+ pNew = sqlite3FaultSim(100) ? 0 : (MergeEngine*)sqlite3MallocZero(nByte); |
+ if( pNew ){ |
+ pNew->nTree = N; |
+ pNew->pTask = 0; |
+ pNew->aReadr = (PmaReader*)&pNew[1]; |
+ pNew->aTree = (int*)&pNew->aReadr[N]; |
+ } |
+ return pNew; |
+} |
+ |
+/* |
+** Free the MergeEngine object passed as the only argument. |
+*/ |
+static void vdbeMergeEngineFree(MergeEngine *pMerger){ |
+ int i; |
+ if( pMerger ){ |
+ for(i=0; i<pMerger->nTree; i++){ |
+ vdbePmaReaderClear(&pMerger->aReadr[i]); |
+ } |
+ } |
+ sqlite3_free(pMerger); |
+} |
+ |
+/* |
+** Free all resources associated with the IncrMerger object indicated by |
+** the first argument. |
+*/ |
+static void vdbeIncrFree(IncrMerger *pIncr){ |
+ if( pIncr ){ |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+ if( pIncr->bUseThread ){ |
+ vdbeSorterJoinThread(pIncr->pTask); |
+ if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd); |
+ if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd); |
+ } |
+#endif |
+ vdbeMergeEngineFree(pIncr->pMerger); |
+ sqlite3_free(pIncr); |
+ } |
+} |
+ |
+/* |
+** Reset a sorting cursor back to its original empty state. |
+*/ |
+SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){ |
+ int i; |
+ (void)vdbeSorterJoinAll(pSorter, SQLITE_OK); |
+ assert( pSorter->bUseThreads || pSorter->pReader==0 ); |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+ if( pSorter->pReader ){ |
+ vdbePmaReaderClear(pSorter->pReader); |
+ sqlite3DbFree(db, pSorter->pReader); |
+ pSorter->pReader = 0; |
+ } |
+#endif |
+ vdbeMergeEngineFree(pSorter->pMerger); |
+ pSorter->pMerger = 0; |
+ for(i=0; i<pSorter->nTask; i++){ |
+ SortSubtask *pTask = &pSorter->aTask[i]; |
+ vdbeSortSubtaskCleanup(db, pTask); |
+ pTask->pSorter = pSorter; |
+ } |
+ if( pSorter->list.aMemory==0 ){ |
+ vdbeSorterRecordFree(0, pSorter->list.pList); |
+ } |
+ pSorter->list.pList = 0; |
+ pSorter->list.szPMA = 0; |
+ pSorter->bUsePMA = 0; |
+ pSorter->iMemory = 0; |
+ pSorter->mxKeysize = 0; |
+ sqlite3DbFree(db, pSorter->pUnpacked); |
+ pSorter->pUnpacked = 0; |
+} |
+ |
+/* |
+** Free any cursor components allocated by sqlite3VdbeSorterXXX routines. |
+*/ |
+SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){ |
+ VdbeSorter *pSorter; |
+ assert( pCsr->eCurType==CURTYPE_SORTER ); |
+ pSorter = pCsr->uc.pSorter; |
+ if( pSorter ){ |
+ sqlite3VdbeSorterReset(db, pSorter); |
+ sqlite3_free(pSorter->list.aMemory); |
+ sqlite3DbFree(db, pSorter); |
+ pCsr->uc.pSorter = 0; |
+ } |
+} |
+ |
+#if SQLITE_MAX_MMAP_SIZE>0 |
+/* |
+** The first argument is a file-handle open on a temporary file. The file |
+** is guaranteed to be nByte bytes or smaller in size. This function |
+** attempts to extend the file to nByte bytes in size and to ensure that |
+** the VFS has memory mapped it. |
+** |
+** Whether or not the file does end up memory mapped of course depends on |
+** the specific VFS implementation. |
+*/ |
+static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){ |
+ if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){ |
+ void *p = 0; |
+ int chunksize = 4*1024; |
+ sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE, &chunksize); |
+ sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT, &nByte); |
+ sqlite3OsFetch(pFd, 0, (int)nByte, &p); |
+ sqlite3OsUnfetch(pFd, 0, p); |
+ } |
+} |
+#else |
+# define vdbeSorterExtendFile(x,y,z) |
+#endif |
+ |
+/* |
+** Allocate space for a file-handle and open a temporary file. If successful, |
+** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK. |
+** Otherwise, set *ppFd to 0 and return an SQLite error code. |
+*/ |
+static int vdbeSorterOpenTempFile( |
+ sqlite3 *db, /* Database handle doing sort */ |
+ i64 nExtend, /* Attempt to extend file to this size */ |
+ sqlite3_file **ppFd |
+){ |
+ int rc; |
+ if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS; |
+ rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd, |
+ SQLITE_OPEN_TEMP_JOURNAL | |
+ SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | |
+ SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &rc |
+ ); |
+ if( rc==SQLITE_OK ){ |
+ i64 max = SQLITE_MAX_MMAP_SIZE; |
+ sqlite3OsFileControlHint(*ppFd, SQLITE_FCNTL_MMAP_SIZE, (void*)&max); |
+ if( nExtend>0 ){ |
+ vdbeSorterExtendFile(db, *ppFd, nExtend); |
+ } |
+ } |
+ return rc; |
+} |
+ |
+/* |
+** If it has not already been allocated, allocate the UnpackedRecord |
+** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or |
+** if no allocation was required), or SQLITE_NOMEM otherwise. |
+*/ |
+static int vdbeSortAllocUnpacked(SortSubtask *pTask){ |
+ if( pTask->pUnpacked==0 ){ |
+ pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo); |
+ if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT; |
+ pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nField; |
+ pTask->pUnpacked->errCode = 0; |
+ } |
+ return SQLITE_OK; |
+} |
+ |
+ |
+/* |
+** Merge the two sorted lists p1 and p2 into a single list. |
+*/ |
+static SorterRecord *vdbeSorterMerge( |
+ SortSubtask *pTask, /* Calling thread context */ |
+ SorterRecord *p1, /* First list to merge */ |
+ SorterRecord *p2 /* Second list to merge */ |
+){ |
+ SorterRecord *pFinal = 0; |
+ SorterRecord **pp = &pFinal; |
+ int bCached = 0; |
+ |
+ assert( p1!=0 && p2!=0 ); |
+ for(;;){ |
+ int res; |
+ res = pTask->xCompare( |
+ pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal |
+ ); |
+ |
+ if( res<=0 ){ |
+ *pp = p1; |
+ pp = &p1->u.pNext; |
+ p1 = p1->u.pNext; |
+ if( p1==0 ){ |
+ *pp = p2; |
+ break; |
+ } |
+ }else{ |
+ *pp = p2; |
+ pp = &p2->u.pNext; |
+ p2 = p2->u.pNext; |
+ bCached = 0; |
+ if( p2==0 ){ |
+ *pp = p1; |
+ break; |
+ } |
+ } |
+ } |
+ return pFinal; |
+} |
+ |
+/* |
+** Return the SorterCompare function to compare values collected by the |
+** sorter object passed as the only argument. |
+*/ |
+static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){ |
+ if( p->typeMask==SORTER_TYPE_INTEGER ){ |
+ return vdbeSorterCompareInt; |
+ }else if( p->typeMask==SORTER_TYPE_TEXT ){ |
+ return vdbeSorterCompareText; |
+ } |
+ return vdbeSorterCompare; |
+} |
+ |
+/* |
+** Sort the linked list of records headed at pTask->pList. Return |
+** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if |
+** an error occurs. |
+*/ |
+static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){ |
+ int i; |
+ SorterRecord **aSlot; |
+ SorterRecord *p; |
+ int rc; |
+ |
+ rc = vdbeSortAllocUnpacked(pTask); |
+ if( rc!=SQLITE_OK ) return rc; |
+ |
+ p = pList->pList; |
+ pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter); |
+ |
+ aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *)); |
+ if( !aSlot ){ |
+ return SQLITE_NOMEM_BKPT; |
+ } |
+ |
+ while( p ){ |
+ SorterRecord *pNext; |
+ if( pList->aMemory ){ |
+ if( (u8*)p==pList->aMemory ){ |
+ pNext = 0; |
+ }else{ |
+ assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) ); |
+ pNext = (SorterRecord*)&pList->aMemory[p->u.iNext]; |
+ } |
+ }else{ |
+ pNext = p->u.pNext; |
+ } |
+ |
+ p->u.pNext = 0; |
+ for(i=0; aSlot[i]; i++){ |
+ p = vdbeSorterMerge(pTask, p, aSlot[i]); |
+ aSlot[i] = 0; |
+ } |
+ aSlot[i] = p; |
+ p = pNext; |
+ } |
+ |
+ p = 0; |
+ for(i=0; i<64; i++){ |
+ if( aSlot[i]==0 ) continue; |
+ p = p ? vdbeSorterMerge(pTask, p, aSlot[i]) : aSlot[i]; |
+ } |
+ pList->pList = p; |
+ |
+ sqlite3_free(aSlot); |
+ assert( pTask->pUnpacked->errCode==SQLITE_OK |
+ || pTask->pUnpacked->errCode==SQLITE_NOMEM |
+ ); |
+ return pTask->pUnpacked->errCode; |
+} |
+ |
+/* |
+** Initialize a PMA-writer object. |
+*/ |
+static void vdbePmaWriterInit( |
+ sqlite3_file *pFd, /* File handle to write to */ |
+ PmaWriter *p, /* Object to populate */ |
+ int nBuf, /* Buffer size */ |
+ i64 iStart /* Offset of pFd to begin writing at */ |
+){ |
+ memset(p, 0, sizeof(PmaWriter)); |
+ p->aBuffer = (u8*)sqlite3Malloc(nBuf); |
+ if( !p->aBuffer ){ |
+ p->eFWErr = SQLITE_NOMEM_BKPT; |
+ }else{ |
+ p->iBufEnd = p->iBufStart = (iStart % nBuf); |
+ p->iWriteOff = iStart - p->iBufStart; |
+ p->nBuffer = nBuf; |
+ p->pFd = pFd; |
+ } |
+} |
+ |
+/* |
+** Write nData bytes of data to the PMA. Return SQLITE_OK |
+** if successful, or an SQLite error code if an error occurs. |
+*/ |
+static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){ |
+ int nRem = nData; |
+ while( nRem>0 && p->eFWErr==0 ){ |
+ int nCopy = nRem; |
+ if( nCopy>(p->nBuffer - p->iBufEnd) ){ |
+ nCopy = p->nBuffer - p->iBufEnd; |
+ } |
+ |
+ memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy); |
+ p->iBufEnd += nCopy; |
+ if( p->iBufEnd==p->nBuffer ){ |
+ p->eFWErr = sqlite3OsWrite(p->pFd, |
+ &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, |
+ p->iWriteOff + p->iBufStart |
+ ); |
+ p->iBufStart = p->iBufEnd = 0; |
+ p->iWriteOff += p->nBuffer; |
+ } |
+ assert( p->iBufEnd<p->nBuffer ); |
+ |
+ nRem -= nCopy; |
+ } |
+} |
+ |
+/* |
+** Flush any buffered data to disk and clean up the PMA-writer object. |
+** The results of using the PMA-writer after this call are undefined. |
+** Return SQLITE_OK if flushing the buffered data succeeds or is not |
+** required. Otherwise, return an SQLite error code. |
+** |
+** Before returning, set *piEof to the offset immediately following the |
+** last byte written to the file. |
+*/ |
+static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){ |
+ int rc; |
+ if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){ |
+ p->eFWErr = sqlite3OsWrite(p->pFd, |
+ &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, |
+ p->iWriteOff + p->iBufStart |
+ ); |
+ } |
+ *piEof = (p->iWriteOff + p->iBufEnd); |
+ sqlite3_free(p->aBuffer); |
+ rc = p->eFWErr; |
+ memset(p, 0, sizeof(PmaWriter)); |
+ return rc; |
+} |
+ |
+/* |
+** Write value iVal encoded as a varint to the PMA. Return |
+** SQLITE_OK if successful, or an SQLite error code if an error occurs. |
+*/ |
+static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){ |
+ int nByte; |
+ u8 aByte[10]; |
+ nByte = sqlite3PutVarint(aByte, iVal); |
+ vdbePmaWriteBlob(p, aByte, nByte); |
+} |
+ |
+/* |
+** Write the current contents of in-memory linked-list pList to a level-0 |
+** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if |
+** successful, or an SQLite error code otherwise. |
+** |
+** The format of a PMA is: |
+** |
+** * A varint. This varint contains the total number of bytes of content |
+** in the PMA (not including the varint itself). |
+** |
+** * One or more records packed end-to-end in order of ascending keys. |
+** Each record consists of a varint followed by a blob of data (the |
+** key). The varint is the number of bytes in the blob of data. |
+*/ |
+static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){ |
+ sqlite3 *db = pTask->pSorter->db; |
+ int rc = SQLITE_OK; /* Return code */ |
+ PmaWriter writer; /* Object used to write to the file */ |
+ |
+#ifdef SQLITE_DEBUG |
+ /* Set iSz to the expected size of file pTask->file after writing the PMA. |
+ ** This is used by an assert() statement at the end of this function. */ |
+ i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof; |
+#endif |
+ |
+ vdbeSorterWorkDebug(pTask, "enter"); |
+ memset(&writer, 0, sizeof(PmaWriter)); |
+ assert( pList->szPMA>0 ); |
+ |
+ /* If the first temporary PMA file has not been opened, open it now. */ |
+ if( pTask->file.pFd==0 ){ |
+ rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd); |
+ assert( rc!=SQLITE_OK || pTask->file.pFd ); |
+ assert( pTask->file.iEof==0 ); |
+ assert( pTask->nPMA==0 ); |
+ } |
+ |
+ /* Try to get the file to memory map */ |
+ if( rc==SQLITE_OK ){ |
+ vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9); |
+ } |
+ |
+ /* Sort the list */ |
+ if( rc==SQLITE_OK ){ |
+ rc = vdbeSorterSort(pTask, pList); |
+ } |
+ |
+ if( rc==SQLITE_OK ){ |
+ SorterRecord *p; |
+ SorterRecord *pNext = 0; |
+ |
+ vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz, |
+ pTask->file.iEof); |
+ pTask->nPMA++; |
+ vdbePmaWriteVarint(&writer, pList->szPMA); |
+ for(p=pList->pList; p; p=pNext){ |
+ pNext = p->u.pNext; |
+ vdbePmaWriteVarint(&writer, p->nVal); |
+ vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal); |
+ if( pList->aMemory==0 ) sqlite3_free(p); |
+ } |
+ pList->pList = p; |
+ rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof); |
+ } |
+ |
+ vdbeSorterWorkDebug(pTask, "exit"); |
+ assert( rc!=SQLITE_OK || pList->pList==0 ); |
+ assert( rc!=SQLITE_OK || pTask->file.iEof==iSz ); |
+ return rc; |
+} |
+ |
+/* |
+** Advance the MergeEngine to its next entry. |
+** Set *pbEof to true there is no next entry because |
+** the MergeEngine has reached the end of all its inputs. |
+** |
+** Return SQLITE_OK if successful or an error code if an error occurs. |
+*/ |
+static int vdbeMergeEngineStep( |
+ MergeEngine *pMerger, /* The merge engine to advance to the next row */ |
+ int *pbEof /* Set TRUE at EOF. Set false for more content */ |
+){ |
+ int rc; |
+ int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */ |
+ SortSubtask *pTask = pMerger->pTask; |
+ |
+ /* Advance the current PmaReader */ |
+ rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]); |
+ |
+ /* Update contents of aTree[] */ |
+ if( rc==SQLITE_OK ){ |
+ int i; /* Index of aTree[] to recalculate */ |
+ PmaReader *pReadr1; /* First PmaReader to compare */ |
+ PmaReader *pReadr2; /* Second PmaReader to compare */ |
+ int bCached = 0; |
+ |
+ /* Find the first two PmaReaders to compare. The one that was just |
+ ** advanced (iPrev) and the one next to it in the array. */ |
+ pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)]; |
+ pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)]; |
+ |
+ for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){ |
+ /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */ |
+ int iRes; |
+ if( pReadr1->pFd==0 ){ |
+ iRes = +1; |
+ }else if( pReadr2->pFd==0 ){ |
+ iRes = -1; |
+ }else{ |
+ iRes = pTask->xCompare(pTask, &bCached, |
+ pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey |
+ ); |
+ } |
+ |
+ /* If pReadr1 contained the smaller value, set aTree[i] to its index. |
+ ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this |
+ ** case there is no cache of pReadr2 in pTask->pUnpacked, so set |
+ ** pKey2 to point to the record belonging to pReadr2. |
+ ** |
+ ** Alternatively, if pReadr2 contains the smaller of the two values, |
+ ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare() |
+ ** was actually called above, then pTask->pUnpacked now contains |
+ ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent |
+ ** vdbeSorterCompare() from decoding pReadr2 again. |
+ ** |
+ ** If the two values were equal, then the value from the oldest |
+ ** PMA should be considered smaller. The VdbeSorter.aReadr[] array |
+ ** is sorted from oldest to newest, so pReadr1 contains older values |
+ ** than pReadr2 iff (pReadr1<pReadr2). */ |
+ if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){ |
+ pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr); |
+ pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ]; |
+ bCached = 0; |
+ }else{ |
+ if( pReadr1->pFd ) bCached = 0; |
+ pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr); |
+ pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ]; |
+ } |
+ } |
+ *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0); |
+ } |
+ |
+ return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc); |
+} |
+ |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+/* |
+** The main routine for background threads that write level-0 PMAs. |
+*/ |
+static void *vdbeSorterFlushThread(void *pCtx){ |
+ SortSubtask *pTask = (SortSubtask*)pCtx; |
+ int rc; /* Return code */ |
+ assert( pTask->bDone==0 ); |
+ rc = vdbeSorterListToPMA(pTask, &pTask->list); |
+ pTask->bDone = 1; |
+ return SQLITE_INT_TO_PTR(rc); |
+} |
+#endif /* SQLITE_MAX_WORKER_THREADS>0 */ |
+ |
+/* |
+** Flush the current contents of VdbeSorter.list to a new PMA, possibly |
+** using a background thread. |
+*/ |
+static int vdbeSorterFlushPMA(VdbeSorter *pSorter){ |
+#if SQLITE_MAX_WORKER_THREADS==0 |
+ pSorter->bUsePMA = 1; |
+ return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list); |
+#else |
+ int rc = SQLITE_OK; |
+ int i; |
+ SortSubtask *pTask = 0; /* Thread context used to create new PMA */ |
+ int nWorker = (pSorter->nTask-1); |
+ |
+ /* Set the flag to indicate that at least one PMA has been written. |
+ ** Or will be, anyhow. */ |
+ pSorter->bUsePMA = 1; |
+ |
+ /* Select a sub-task to sort and flush the current list of in-memory |
+ ** records to disk. If the sorter is running in multi-threaded mode, |
+ ** round-robin between the first (pSorter->nTask-1) tasks. Except, if |
+ ** the background thread from a sub-tasks previous turn is still running, |
+ ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy, |
+ ** fall back to using the final sub-task. The first (pSorter->nTask-1) |
+ ** sub-tasks are prefered as they use background threads - the final |
+ ** sub-task uses the main thread. */ |
+ for(i=0; i<nWorker; i++){ |
+ int iTest = (pSorter->iPrev + i + 1) % nWorker; |
+ pTask = &pSorter->aTask[iTest]; |
+ if( pTask->bDone ){ |
+ rc = vdbeSorterJoinThread(pTask); |
+ } |
+ if( rc!=SQLITE_OK || pTask->pThread==0 ) break; |
+ } |
+ |
+ if( rc==SQLITE_OK ){ |
+ if( i==nWorker ){ |
+ /* Use the foreground thread for this operation */ |
+ rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list); |
+ }else{ |
+ /* Launch a background thread for this operation */ |
+ u8 *aMem = pTask->list.aMemory; |
+ void *pCtx = (void*)pTask; |
+ |
+ assert( pTask->pThread==0 && pTask->bDone==0 ); |
+ assert( pTask->list.pList==0 ); |
+ assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 ); |
+ |
+ pSorter->iPrev = (u8)(pTask - pSorter->aTask); |
+ pTask->list = pSorter->list; |
+ pSorter->list.pList = 0; |
+ pSorter->list.szPMA = 0; |
+ if( aMem ){ |
+ pSorter->list.aMemory = aMem; |
+ pSorter->nMemory = sqlite3MallocSize(aMem); |
+ }else if( pSorter->list.aMemory ){ |
+ pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory); |
+ if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT; |
+ } |
+ |
+ rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx); |
+ } |
+ } |
+ |
+ return rc; |
+#endif /* SQLITE_MAX_WORKER_THREADS!=0 */ |
+} |
+ |
+/* |
+** Add a record to the sorter. |
+*/ |
+SQLITE_PRIVATE int sqlite3VdbeSorterWrite( |
+ const VdbeCursor *pCsr, /* Sorter cursor */ |
+ Mem *pVal /* Memory cell containing record */ |
+){ |
+ VdbeSorter *pSorter; |
+ int rc = SQLITE_OK; /* Return Code */ |
+ SorterRecord *pNew; /* New list element */ |
+ int bFlush; /* True to flush contents of memory to PMA */ |
+ int nReq; /* Bytes of memory required */ |
+ int nPMA; /* Bytes of PMA space required */ |
+ int t; /* serial type of first record field */ |
+ |
+ assert( pCsr->eCurType==CURTYPE_SORTER ); |
+ pSorter = pCsr->uc.pSorter; |
+ getVarint32((const u8*)&pVal->z[1], t); |
+ if( t>0 && t<10 && t!=7 ){ |
+ pSorter->typeMask &= SORTER_TYPE_INTEGER; |
+ }else if( t>10 && (t & 0x01) ){ |
+ pSorter->typeMask &= SORTER_TYPE_TEXT; |
+ }else{ |
+ pSorter->typeMask = 0; |
+ } |
+ |
+ assert( pSorter ); |
+ |
+ /* Figure out whether or not the current contents of memory should be |
+ ** flushed to a PMA before continuing. If so, do so. |
+ ** |
+ ** If using the single large allocation mode (pSorter->aMemory!=0), then |
+ ** flush the contents of memory to a new PMA if (a) at least one value is |
+ ** already in memory and (b) the new value will not fit in memory. |
+ ** |
+ ** Or, if using separate allocations for each record, flush the contents |
+ ** of memory to a PMA if either of the following are true: |
+ ** |
+ ** * The total memory allocated for the in-memory list is greater |
+ ** than (page-size * cache-size), or |
+ ** |
+ ** * The total memory allocated for the in-memory list is greater |
+ ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true. |
+ */ |
+ nReq = pVal->n + sizeof(SorterRecord); |
+ nPMA = pVal->n + sqlite3VarintLen(pVal->n); |
+ if( pSorter->mxPmaSize ){ |
+ if( pSorter->list.aMemory ){ |
+ bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize; |
+ }else{ |
+ bFlush = ( |
+ (pSorter->list.szPMA > pSorter->mxPmaSize) |
+ || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull()) |
+ ); |
+ } |
+ if( bFlush ){ |
+ rc = vdbeSorterFlushPMA(pSorter); |
+ pSorter->list.szPMA = 0; |
+ pSorter->iMemory = 0; |
+ assert( rc!=SQLITE_OK || pSorter->list.pList==0 ); |
+ } |
+ } |
+ |
+ pSorter->list.szPMA += nPMA; |
+ if( nPMA>pSorter->mxKeysize ){ |
+ pSorter->mxKeysize = nPMA; |
+ } |
+ |
+ if( pSorter->list.aMemory ){ |
+ int nMin = pSorter->iMemory + nReq; |
+ |
+ if( nMin>pSorter->nMemory ){ |
+ u8 *aNew; |
+ int iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory; |
+ int nNew = pSorter->nMemory * 2; |
+ while( nNew < nMin ) nNew = nNew*2; |
+ if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize; |
+ if( nNew < nMin ) nNew = nMin; |
+ |
+ aNew = sqlite3Realloc(pSorter->list.aMemory, nNew); |
+ if( !aNew ) return SQLITE_NOMEM_BKPT; |
+ pSorter->list.pList = (SorterRecord*)&aNew[iListOff]; |
+ pSorter->list.aMemory = aNew; |
+ pSorter->nMemory = nNew; |
+ } |
+ |
+ pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory]; |
+ pSorter->iMemory += ROUND8(nReq); |
+ if( pSorter->list.pList ){ |
+ pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory); |
+ } |
+ }else{ |
+ pNew = (SorterRecord *)sqlite3Malloc(nReq); |
+ if( pNew==0 ){ |
+ return SQLITE_NOMEM_BKPT; |
+ } |
+ pNew->u.pNext = pSorter->list.pList; |
+ } |
+ |
+ memcpy(SRVAL(pNew), pVal->z, pVal->n); |
+ pNew->nVal = pVal->n; |
+ pSorter->list.pList = pNew; |
+ |
+ return rc; |
+} |
+ |
+/* |
+** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format |
+** of the data stored in aFile[1] is the same as that used by regular PMAs, |
+** except that the number-of-bytes varint is omitted from the start. |
+*/ |
+static int vdbeIncrPopulate(IncrMerger *pIncr){ |
+ int rc = SQLITE_OK; |
+ int rc2; |
+ i64 iStart = pIncr->iStartOff; |
+ SorterFile *pOut = &pIncr->aFile[1]; |
+ SortSubtask *pTask = pIncr->pTask; |
+ MergeEngine *pMerger = pIncr->pMerger; |
+ PmaWriter writer; |
+ assert( pIncr->bEof==0 ); |
+ |
+ vdbeSorterPopulateDebug(pTask, "enter"); |
+ |
+ vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart); |
+ while( rc==SQLITE_OK ){ |
+ int dummy; |
+ PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ]; |
+ int nKey = pReader->nKey; |
+ i64 iEof = writer.iWriteOff + writer.iBufEnd; |
+ |
+ /* Check if the output file is full or if the input has been exhausted. |
+ ** In either case exit the loop. */ |
+ if( pReader->pFd==0 ) break; |
+ if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break; |
+ |
+ /* Write the next key to the output. */ |
+ vdbePmaWriteVarint(&writer, nKey); |
+ vdbePmaWriteBlob(&writer, pReader->aKey, nKey); |
+ assert( pIncr->pMerger->pTask==pTask ); |
+ rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy); |
+ } |
+ |
+ rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof); |
+ if( rc==SQLITE_OK ) rc = rc2; |
+ vdbeSorterPopulateDebug(pTask, "exit"); |
+ return rc; |
+} |
+ |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+/* |
+** The main routine for background threads that populate aFile[1] of |
+** multi-threaded IncrMerger objects. |
+*/ |
+static void *vdbeIncrPopulateThread(void *pCtx){ |
+ IncrMerger *pIncr = (IncrMerger*)pCtx; |
+ void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) ); |
+ pIncr->pTask->bDone = 1; |
+ return pRet; |
+} |
+ |
+/* |
+** Launch a background thread to populate aFile[1] of pIncr. |
+*/ |
+static int vdbeIncrBgPopulate(IncrMerger *pIncr){ |
+ void *p = (void*)pIncr; |
+ assert( pIncr->bUseThread ); |
+ return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p); |
+} |
+#endif |
+ |
+/* |
+** This function is called when the PmaReader corresponding to pIncr has |
+** finished reading the contents of aFile[0]. Its purpose is to "refill" |
+** aFile[0] such that the PmaReader should start rereading it from the |
+** beginning. |
+** |
+** For single-threaded objects, this is accomplished by literally reading |
+** keys from pIncr->pMerger and repopulating aFile[0]. |
+** |
+** For multi-threaded objects, all that is required is to wait until the |
+** background thread is finished (if it is not already) and then swap |
+** aFile[0] and aFile[1] in place. If the contents of pMerger have not |
+** been exhausted, this function also launches a new background thread |
+** to populate the new aFile[1]. |
+** |
+** SQLITE_OK is returned on success, or an SQLite error code otherwise. |
+*/ |
+static int vdbeIncrSwap(IncrMerger *pIncr){ |
+ int rc = SQLITE_OK; |
+ |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+ if( pIncr->bUseThread ){ |
+ rc = vdbeSorterJoinThread(pIncr->pTask); |
+ |
+ if( rc==SQLITE_OK ){ |
+ SorterFile f0 = pIncr->aFile[0]; |
+ pIncr->aFile[0] = pIncr->aFile[1]; |
+ pIncr->aFile[1] = f0; |
+ } |
+ |
+ if( rc==SQLITE_OK ){ |
+ if( pIncr->aFile[0].iEof==pIncr->iStartOff ){ |
+ pIncr->bEof = 1; |
+ }else{ |
+ rc = vdbeIncrBgPopulate(pIncr); |
+ } |
+ } |
+ }else |
+#endif |
+ { |
+ rc = vdbeIncrPopulate(pIncr); |
+ pIncr->aFile[0] = pIncr->aFile[1]; |
+ if( pIncr->aFile[0].iEof==pIncr->iStartOff ){ |
+ pIncr->bEof = 1; |
+ } |
+ } |
+ |
+ return rc; |
+} |
+ |
+/* |
+** Allocate and return a new IncrMerger object to read data from pMerger. |
+** |
+** If an OOM condition is encountered, return NULL. In this case free the |
+** pMerger argument before returning. |
+*/ |
+static int vdbeIncrMergerNew( |
+ SortSubtask *pTask, /* The thread that will be using the new IncrMerger */ |
+ MergeEngine *pMerger, /* The MergeEngine that the IncrMerger will control */ |
+ IncrMerger **ppOut /* Write the new IncrMerger here */ |
+){ |
+ int rc = SQLITE_OK; |
+ IncrMerger *pIncr = *ppOut = (IncrMerger*) |
+ (sqlite3FaultSim(100) ? 0 : sqlite3MallocZero(sizeof(*pIncr))); |
+ if( pIncr ){ |
+ pIncr->pMerger = pMerger; |
+ pIncr->pTask = pTask; |
+ pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2); |
+ pTask->file2.iEof += pIncr->mxSz; |
+ }else{ |
+ vdbeMergeEngineFree(pMerger); |
+ rc = SQLITE_NOMEM_BKPT; |
+ } |
+ return rc; |
+} |
+ |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+/* |
+** Set the "use-threads" flag on object pIncr. |
+*/ |
+static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){ |
+ pIncr->bUseThread = 1; |
+ pIncr->pTask->file2.iEof -= pIncr->mxSz; |
+} |
+#endif /* SQLITE_MAX_WORKER_THREADS>0 */ |
+ |
+ |
+ |
+/* |
+** Recompute pMerger->aTree[iOut] by comparing the next keys on the |
+** two PmaReaders that feed that entry. Neither of the PmaReaders |
+** are advanced. This routine merely does the comparison. |
+*/ |
+static void vdbeMergeEngineCompare( |
+ MergeEngine *pMerger, /* Merge engine containing PmaReaders to compare */ |
+ int iOut /* Store the result in pMerger->aTree[iOut] */ |
+){ |
+ int i1; |
+ int i2; |
+ int iRes; |
+ PmaReader *p1; |
+ PmaReader *p2; |
+ |
+ assert( iOut<pMerger->nTree && iOut>0 ); |
+ |
+ if( iOut>=(pMerger->nTree/2) ){ |
+ i1 = (iOut - pMerger->nTree/2) * 2; |
+ i2 = i1 + 1; |
+ }else{ |
+ i1 = pMerger->aTree[iOut*2]; |
+ i2 = pMerger->aTree[iOut*2+1]; |
+ } |
+ |
+ p1 = &pMerger->aReadr[i1]; |
+ p2 = &pMerger->aReadr[i2]; |
+ |
+ if( p1->pFd==0 ){ |
+ iRes = i2; |
+ }else if( p2->pFd==0 ){ |
+ iRes = i1; |
+ }else{ |
+ SortSubtask *pTask = pMerger->pTask; |
+ int bCached = 0; |
+ int res; |
+ assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */ |
+ res = pTask->xCompare( |
+ pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey |
+ ); |
+ if( res<=0 ){ |
+ iRes = i1; |
+ }else{ |
+ iRes = i2; |
+ } |
+ } |
+ |
+ pMerger->aTree[iOut] = iRes; |
+} |
+ |
+/* |
+** Allowed values for the eMode parameter to vdbeMergeEngineInit() |
+** and vdbePmaReaderIncrMergeInit(). |
+** |
+** Only INCRINIT_NORMAL is valid in single-threaded builds (when |
+** SQLITE_MAX_WORKER_THREADS==0). The other values are only used |
+** when there exists one or more separate worker threads. |
+*/ |
+#define INCRINIT_NORMAL 0 |
+#define INCRINIT_TASK 1 |
+#define INCRINIT_ROOT 2 |
+ |
+/* |
+** Forward reference required as the vdbeIncrMergeInit() and |
+** vdbePmaReaderIncrInit() routines are called mutually recursively when |
+** building a merge tree. |
+*/ |
+static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode); |
+ |
+/* |
+** Initialize the MergeEngine object passed as the second argument. Once this |
+** function returns, the first key of merged data may be read from the |
+** MergeEngine object in the usual fashion. |
+** |
+** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge |
+** objects attached to the PmaReader objects that the merger reads from have |
+** already been populated, but that they have not yet populated aFile[0] and |
+** set the PmaReader objects up to read from it. In this case all that is |
+** required is to call vdbePmaReaderNext() on each PmaReader to point it at |
+** its first key. |
+** |
+** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use |
+** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data |
+** to pMerger. |
+** |
+** SQLITE_OK is returned if successful, or an SQLite error code otherwise. |
+*/ |
+static int vdbeMergeEngineInit( |
+ SortSubtask *pTask, /* Thread that will run pMerger */ |
+ MergeEngine *pMerger, /* MergeEngine to initialize */ |
+ int eMode /* One of the INCRINIT_XXX constants */ |
+){ |
+ int rc = SQLITE_OK; /* Return code */ |
+ int i; /* For looping over PmaReader objects */ |
+ int nTree = pMerger->nTree; |
+ |
+ /* eMode is always INCRINIT_NORMAL in single-threaded mode */ |
+ assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL ); |
+ |
+ /* Verify that the MergeEngine is assigned to a single thread */ |
+ assert( pMerger->pTask==0 ); |
+ pMerger->pTask = pTask; |
+ |
+ for(i=0; i<nTree; i++){ |
+ if( SQLITE_MAX_WORKER_THREADS>0 && eMode==INCRINIT_ROOT ){ |
+ /* PmaReaders should be normally initialized in order, as if they are |
+ ** reading from the same temp file this makes for more linear file IO. |
+ ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is |
+ ** in use it will block the vdbePmaReaderNext() call while it uses |
+ ** the main thread to fill its buffer. So calling PmaReaderNext() |
+ ** on this PmaReader before any of the multi-threaded PmaReaders takes |
+ ** better advantage of multi-processor hardware. */ |
+ rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]); |
+ }else{ |
+ rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL); |
+ } |
+ if( rc!=SQLITE_OK ) return rc; |
+ } |
+ |
+ for(i=pMerger->nTree-1; i>0; i--){ |
+ vdbeMergeEngineCompare(pMerger, i); |
+ } |
+ return pTask->pUnpacked->errCode; |
+} |
+ |
+/* |
+** The PmaReader passed as the first argument is guaranteed to be an |
+** incremental-reader (pReadr->pIncr!=0). This function serves to open |
+** and/or initialize the temp file related fields of the IncrMerge |
+** object at (pReadr->pIncr). |
+** |
+** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders |
+** in the sub-tree headed by pReadr are also initialized. Data is then |
+** loaded into the buffers belonging to pReadr and it is set to point to |
+** the first key in its range. |
+** |
+** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed |
+** to be a multi-threaded PmaReader and this function is being called in a |
+** background thread. In this case all PmaReaders in the sub-tree are |
+** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to |
+** pReadr is populated. However, pReadr itself is not set up to point |
+** to its first key. A call to vdbePmaReaderNext() is still required to do |
+** that. |
+** |
+** The reason this function does not call vdbePmaReaderNext() immediately |
+** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has |
+** to block on thread (pTask->thread) before accessing aFile[1]. But, since |
+** this entire function is being run by thread (pTask->thread), that will |
+** lead to the current background thread attempting to join itself. |
+** |
+** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed |
+** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all |
+** child-trees have already been initialized using IncrInit(INCRINIT_TASK). |
+** In this case vdbePmaReaderNext() is called on all child PmaReaders and |
+** the current PmaReader set to point to the first key in its range. |
+** |
+** SQLITE_OK is returned if successful, or an SQLite error code otherwise. |
+*/ |
+static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){ |
+ int rc = SQLITE_OK; |
+ IncrMerger *pIncr = pReadr->pIncr; |
+ SortSubtask *pTask = pIncr->pTask; |
+ sqlite3 *db = pTask->pSorter->db; |
+ |
+ /* eMode is always INCRINIT_NORMAL in single-threaded mode */ |
+ assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL ); |
+ |
+ rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode); |
+ |
+ /* Set up the required files for pIncr. A multi-theaded IncrMerge object |
+ ** requires two temp files to itself, whereas a single-threaded object |
+ ** only requires a region of pTask->file2. */ |
+ if( rc==SQLITE_OK ){ |
+ int mxSz = pIncr->mxSz; |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+ if( pIncr->bUseThread ){ |
+ rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd); |
+ if( rc==SQLITE_OK ){ |
+ rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd); |
+ } |
+ }else |
+#endif |
+ /*if( !pIncr->bUseThread )*/{ |
+ if( pTask->file2.pFd==0 ){ |
+ assert( pTask->file2.iEof>0 ); |
+ rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd); |
+ pTask->file2.iEof = 0; |
+ } |
+ if( rc==SQLITE_OK ){ |
+ pIncr->aFile[1].pFd = pTask->file2.pFd; |
+ pIncr->iStartOff = pTask->file2.iEof; |
+ pTask->file2.iEof += mxSz; |
+ } |
+ } |
+ } |
+ |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+ if( rc==SQLITE_OK && pIncr->bUseThread ){ |
+ /* Use the current thread to populate aFile[1], even though this |
+ ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object, |
+ ** then this function is already running in background thread |
+ ** pIncr->pTask->thread. |
+ ** |
+ ** If this is the INCRINIT_ROOT object, then it is running in the |
+ ** main VDBE thread. But that is Ok, as that thread cannot return |
+ ** control to the VDBE or proceed with anything useful until the |
+ ** first results are ready from this merger object anyway. |
+ */ |
+ assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK ); |
+ rc = vdbeIncrPopulate(pIncr); |
+ } |
+#endif |
+ |
+ if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){ |
+ rc = vdbePmaReaderNext(pReadr); |
+ } |
+ |
+ return rc; |
+} |
+ |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+/* |
+** The main routine for vdbePmaReaderIncrMergeInit() operations run in |
+** background threads. |
+*/ |
+static void *vdbePmaReaderBgIncrInit(void *pCtx){ |
+ PmaReader *pReader = (PmaReader*)pCtx; |
+ void *pRet = SQLITE_INT_TO_PTR( |
+ vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK) |
+ ); |
+ pReader->pIncr->pTask->bDone = 1; |
+ return pRet; |
+} |
+#endif |
+ |
+/* |
+** If the PmaReader passed as the first argument is not an incremental-reader |
+** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes |
+** the vdbePmaReaderIncrMergeInit() function with the parameters passed to |
+** this routine to initialize the incremental merge. |
+** |
+** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1), |
+** then a background thread is launched to call vdbePmaReaderIncrMergeInit(). |
+** Or, if the IncrMerger is single threaded, the same function is called |
+** using the current thread. |
+*/ |
+static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){ |
+ IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */ |
+ int rc = SQLITE_OK; /* Return code */ |
+ if( pIncr ){ |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+ assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK ); |
+ if( pIncr->bUseThread ){ |
+ void *pCtx = (void*)pReadr; |
+ rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx); |
+ }else |
+#endif |
+ { |
+ rc = vdbePmaReaderIncrMergeInit(pReadr, eMode); |
+ } |
+ } |
+ return rc; |
+} |
+ |
+/* |
+** Allocate a new MergeEngine object to merge the contents of nPMA level-0 |
+** PMAs from pTask->file. If no error occurs, set *ppOut to point to |
+** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut |
+** to NULL and return an SQLite error code. |
+** |
+** When this function is called, *piOffset is set to the offset of the |
+** first PMA to read from pTask->file. Assuming no error occurs, it is |
+** set to the offset immediately following the last byte of the last |
+** PMA before returning. If an error does occur, then the final value of |
+** *piOffset is undefined. |
+*/ |
+static int vdbeMergeEngineLevel0( |
+ SortSubtask *pTask, /* Sorter task to read from */ |
+ int nPMA, /* Number of PMAs to read */ |
+ i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */ |
+ MergeEngine **ppOut /* OUT: New merge-engine */ |
+){ |
+ MergeEngine *pNew; /* Merge engine to return */ |
+ i64 iOff = *piOffset; |
+ int i; |
+ int rc = SQLITE_OK; |
+ |
+ *ppOut = pNew = vdbeMergeEngineNew(nPMA); |
+ if( pNew==0 ) rc = SQLITE_NOMEM_BKPT; |
+ |
+ for(i=0; i<nPMA && rc==SQLITE_OK; i++){ |
+ i64 nDummy = 0; |
+ PmaReader *pReadr = &pNew->aReadr[i]; |
+ rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy); |
+ iOff = pReadr->iEof; |
+ } |
+ |
+ if( rc!=SQLITE_OK ){ |
+ vdbeMergeEngineFree(pNew); |
+ *ppOut = 0; |
+ } |
+ *piOffset = iOff; |
+ return rc; |
+} |
+ |
+/* |
+** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of |
+** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes. |
+** |
+** i.e. |
+** |
+** nPMA<=16 -> TreeDepth() == 0 |
+** nPMA<=256 -> TreeDepth() == 1 |
+** nPMA<=65536 -> TreeDepth() == 2 |
+*/ |
+static int vdbeSorterTreeDepth(int nPMA){ |
+ int nDepth = 0; |
+ i64 nDiv = SORTER_MAX_MERGE_COUNT; |
+ while( nDiv < (i64)nPMA ){ |
+ nDiv = nDiv * SORTER_MAX_MERGE_COUNT; |
+ nDepth++; |
+ } |
+ return nDepth; |
+} |
+ |
+/* |
+** pRoot is the root of an incremental merge-tree with depth nDepth (according |
+** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the |
+** tree, counting from zero. This function adds pLeaf to the tree. |
+** |
+** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error |
+** code is returned and pLeaf is freed. |
+*/ |
+static int vdbeSorterAddToTree( |
+ SortSubtask *pTask, /* Task context */ |
+ int nDepth, /* Depth of tree according to TreeDepth() */ |
+ int iSeq, /* Sequence number of leaf within tree */ |
+ MergeEngine *pRoot, /* Root of tree */ |
+ MergeEngine *pLeaf /* Leaf to add to tree */ |
+){ |
+ int rc = SQLITE_OK; |
+ int nDiv = 1; |
+ int i; |
+ MergeEngine *p = pRoot; |
+ IncrMerger *pIncr; |
+ |
+ rc = vdbeIncrMergerNew(pTask, pLeaf, &pIncr); |
+ |
+ for(i=1; i<nDepth; i++){ |
+ nDiv = nDiv * SORTER_MAX_MERGE_COUNT; |
+ } |
+ |
+ for(i=1; i<nDepth && rc==SQLITE_OK; i++){ |
+ int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT; |
+ PmaReader *pReadr = &p->aReadr[iIter]; |
+ |
+ if( pReadr->pIncr==0 ){ |
+ MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT); |
+ if( pNew==0 ){ |
+ rc = SQLITE_NOMEM_BKPT; |
+ }else{ |
+ rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr); |
+ } |
+ } |
+ if( rc==SQLITE_OK ){ |
+ p = pReadr->pIncr->pMerger; |
+ nDiv = nDiv / SORTER_MAX_MERGE_COUNT; |
+ } |
+ } |
+ |
+ if( rc==SQLITE_OK ){ |
+ p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr; |
+ }else{ |
+ vdbeIncrFree(pIncr); |
+ } |
+ return rc; |
+} |
+ |
+/* |
+** This function is called as part of a SorterRewind() operation on a sorter |
+** that has already written two or more level-0 PMAs to one or more temp |
+** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that |
+** can be used to incrementally merge all PMAs on disk. |
+** |
+** If successful, SQLITE_OK is returned and *ppOut set to point to the |
+** MergeEngine object at the root of the tree before returning. Or, if an |
+** error occurs, an SQLite error code is returned and the final value |
+** of *ppOut is undefined. |
+*/ |
+static int vdbeSorterMergeTreeBuild( |
+ VdbeSorter *pSorter, /* The VDBE cursor that implements the sort */ |
+ MergeEngine **ppOut /* Write the MergeEngine here */ |
+){ |
+ MergeEngine *pMain = 0; |
+ int rc = SQLITE_OK; |
+ int iTask; |
+ |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+ /* If the sorter uses more than one task, then create the top-level |
+ ** MergeEngine here. This MergeEngine will read data from exactly |
+ ** one PmaReader per sub-task. */ |
+ assert( pSorter->bUseThreads || pSorter->nTask==1 ); |
+ if( pSorter->nTask>1 ){ |
+ pMain = vdbeMergeEngineNew(pSorter->nTask); |
+ if( pMain==0 ) rc = SQLITE_NOMEM_BKPT; |
+ } |
+#endif |
+ |
+ for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){ |
+ SortSubtask *pTask = &pSorter->aTask[iTask]; |
+ assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 ); |
+ if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){ |
+ MergeEngine *pRoot = 0; /* Root node of tree for this task */ |
+ int nDepth = vdbeSorterTreeDepth(pTask->nPMA); |
+ i64 iReadOff = 0; |
+ |
+ if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){ |
+ rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot); |
+ }else{ |
+ int i; |
+ int iSeq = 0; |
+ pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT); |
+ if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT; |
+ for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){ |
+ MergeEngine *pMerger = 0; /* New level-0 PMA merger */ |
+ int nReader; /* Number of level-0 PMAs to merge */ |
+ |
+ nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT); |
+ rc = vdbeMergeEngineLevel0(pTask, nReader, &iReadOff, &pMerger); |
+ if( rc==SQLITE_OK ){ |
+ rc = vdbeSorterAddToTree(pTask, nDepth, iSeq++, pRoot, pMerger); |
+ } |
+ } |
+ } |
+ |
+ if( rc==SQLITE_OK ){ |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+ if( pMain!=0 ){ |
+ rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr); |
+ }else |
+#endif |
+ { |
+ assert( pMain==0 ); |
+ pMain = pRoot; |
+ } |
+ }else{ |
+ vdbeMergeEngineFree(pRoot); |
+ } |
+ } |
+ } |
+ |
+ if( rc!=SQLITE_OK ){ |
+ vdbeMergeEngineFree(pMain); |
+ pMain = 0; |
+ } |
+ *ppOut = pMain; |
+ return rc; |
+} |
+ |
+/* |
+** This function is called as part of an sqlite3VdbeSorterRewind() operation |
+** on a sorter that has written two or more PMAs to temporary files. It sets |
+** up either VdbeSorter.pMerger (for single threaded sorters) or pReader |
+** (for multi-threaded sorters) so that it can be used to iterate through |
+** all records stored in the sorter. |
+** |
+** SQLITE_OK is returned if successful, or an SQLite error code otherwise. |
+*/ |
+static int vdbeSorterSetupMerge(VdbeSorter *pSorter){ |
+ int rc; /* Return code */ |
+ SortSubtask *pTask0 = &pSorter->aTask[0]; |
+ MergeEngine *pMain = 0; |
+#if SQLITE_MAX_WORKER_THREADS |
+ sqlite3 *db = pTask0->pSorter->db; |
+ int i; |
+ SorterCompare xCompare = vdbeSorterGetCompare(pSorter); |
+ for(i=0; i<pSorter->nTask; i++){ |
+ pSorter->aTask[i].xCompare = xCompare; |
+ } |
+#endif |
+ |
+ rc = vdbeSorterMergeTreeBuild(pSorter, &pMain); |
+ if( rc==SQLITE_OK ){ |
+#if SQLITE_MAX_WORKER_THREADS |
+ assert( pSorter->bUseThreads==0 || pSorter->nTask>1 ); |
+ if( pSorter->bUseThreads ){ |
+ int iTask; |
+ PmaReader *pReadr = 0; |
+ SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1]; |
+ rc = vdbeSortAllocUnpacked(pLast); |
+ if( rc==SQLITE_OK ){ |
+ pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader)); |
+ pSorter->pReader = pReadr; |
+ if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT; |
+ } |
+ if( rc==SQLITE_OK ){ |
+ rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr); |
+ if( rc==SQLITE_OK ){ |
+ vdbeIncrMergerSetThreads(pReadr->pIncr); |
+ for(iTask=0; iTask<(pSorter->nTask-1); iTask++){ |
+ IncrMerger *pIncr; |
+ if( (pIncr = pMain->aReadr[iTask].pIncr) ){ |
+ vdbeIncrMergerSetThreads(pIncr); |
+ assert( pIncr->pTask!=pLast ); |
+ } |
+ } |
+ for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){ |
+ /* Check that: |
+ ** |
+ ** a) The incremental merge object is configured to use the |
+ ** right task, and |
+ ** b) If it is using task (nTask-1), it is configured to run |
+ ** in single-threaded mode. This is important, as the |
+ ** root merge (INCRINIT_ROOT) will be using the same task |
+ ** object. |
+ */ |
+ PmaReader *p = &pMain->aReadr[iTask]; |
+ assert( p->pIncr==0 || ( |
+ (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */ |
+ && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */ |
+ )); |
+ rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK); |
+ } |
+ } |
+ pMain = 0; |
+ } |
+ if( rc==SQLITE_OK ){ |
+ rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT); |
+ } |
+ }else |
+#endif |
+ { |
+ rc = vdbeMergeEngineInit(pTask0, pMain, INCRINIT_NORMAL); |
+ pSorter->pMerger = pMain; |
+ pMain = 0; |
+ } |
+ } |
+ |
+ if( rc!=SQLITE_OK ){ |
+ vdbeMergeEngineFree(pMain); |
+ } |
+ return rc; |
+} |
+ |
+ |
+/* |
+** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite, |
+** this function is called to prepare for iterating through the records |
+** in sorted order. |
+*/ |
+SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){ |
+ VdbeSorter *pSorter; |
+ int rc = SQLITE_OK; /* Return code */ |
+ |
+ assert( pCsr->eCurType==CURTYPE_SORTER ); |
+ pSorter = pCsr->uc.pSorter; |
+ assert( pSorter ); |
+ |
+ /* If no data has been written to disk, then do not do so now. Instead, |
+ ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly |
+ ** from the in-memory list. */ |
+ if( pSorter->bUsePMA==0 ){ |
+ if( pSorter->list.pList ){ |
+ *pbEof = 0; |
+ rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list); |
+ }else{ |
+ *pbEof = 1; |
+ } |
+ return rc; |
+ } |
+ |
+ /* Write the current in-memory list to a PMA. When the VdbeSorterWrite() |
+ ** function flushes the contents of memory to disk, it immediately always |
+ ** creates a new list consisting of a single key immediately afterwards. |
+ ** So the list is never empty at this point. */ |
+ assert( pSorter->list.pList ); |
+ rc = vdbeSorterFlushPMA(pSorter); |
+ |
+ /* Join all threads */ |
+ rc = vdbeSorterJoinAll(pSorter, rc); |
+ |
+ vdbeSorterRewindDebug("rewind"); |
+ |
+ /* Assuming no errors have occurred, set up a merger structure to |
+ ** incrementally read and merge all remaining PMAs. */ |
+ assert( pSorter->pReader==0 ); |
+ if( rc==SQLITE_OK ){ |
+ rc = vdbeSorterSetupMerge(pSorter); |
+ *pbEof = 0; |
+ } |
+ |
+ vdbeSorterRewindDebug("rewinddone"); |
+ return rc; |
+} |
+ |
+/* |
+** Advance to the next element in the sorter. |
+*/ |
+SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){ |
+ VdbeSorter *pSorter; |
+ int rc; /* Return code */ |
+ |
+ assert( pCsr->eCurType==CURTYPE_SORTER ); |
+ pSorter = pCsr->uc.pSorter; |
+ assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) ); |
+ if( pSorter->bUsePMA ){ |
+ assert( pSorter->pReader==0 || pSorter->pMerger==0 ); |
+ assert( pSorter->bUseThreads==0 || pSorter->pReader ); |
+ assert( pSorter->bUseThreads==1 || pSorter->pMerger ); |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+ if( pSorter->bUseThreads ){ |
+ rc = vdbePmaReaderNext(pSorter->pReader); |
+ *pbEof = (pSorter->pReader->pFd==0); |
+ }else |
+#endif |
+ /*if( !pSorter->bUseThreads )*/ { |
+ assert( pSorter->pMerger!=0 ); |
+ assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) ); |
+ rc = vdbeMergeEngineStep(pSorter->pMerger, pbEof); |
+ } |
+ }else{ |
+ SorterRecord *pFree = pSorter->list.pList; |
+ pSorter->list.pList = pFree->u.pNext; |
+ pFree->u.pNext = 0; |
+ if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree); |
+ *pbEof = !pSorter->list.pList; |
+ rc = SQLITE_OK; |
+ } |
+ return rc; |
+} |
+ |
+/* |
+** Return a pointer to a buffer owned by the sorter that contains the |
+** current key. |
+*/ |
+static void *vdbeSorterRowkey( |
+ const VdbeSorter *pSorter, /* Sorter object */ |
+ int *pnKey /* OUT: Size of current key in bytes */ |
+){ |
+ void *pKey; |
+ if( pSorter->bUsePMA ){ |
+ PmaReader *pReader; |
+#if SQLITE_MAX_WORKER_THREADS>0 |
+ if( pSorter->bUseThreads ){ |
+ pReader = pSorter->pReader; |
+ }else |
+#endif |
+ /*if( !pSorter->bUseThreads )*/{ |
+ pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]]; |
+ } |
+ *pnKey = pReader->nKey; |
+ pKey = pReader->aKey; |
+ }else{ |
+ *pnKey = pSorter->list.pList->nVal; |
+ pKey = SRVAL(pSorter->list.pList); |
+ } |
+ return pKey; |
+} |
+ |
+/* |
+** Copy the current sorter key into the memory cell pOut. |
+*/ |
+SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){ |
+ VdbeSorter *pSorter; |
+ void *pKey; int nKey; /* Sorter key to copy into pOut */ |
+ |
+ assert( pCsr->eCurType==CURTYPE_SORTER ); |
+ pSorter = pCsr->uc.pSorter; |
+ pKey = vdbeSorterRowkey(pSorter, &nKey); |
+ if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){ |
+ return SQLITE_NOMEM_BKPT; |
+ } |
+ pOut->n = nKey; |
+ MemSetTypeFlag(pOut, MEM_Blob); |
+ memcpy(pOut->z, pKey, nKey); |
+ |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Compare the key in memory cell pVal with the key that the sorter cursor |
+** passed as the first argument currently points to. For the purposes of |
+** the comparison, ignore the rowid field at the end of each record. |
+** |
+** If the sorter cursor key contains any NULL values, consider it to be |
+** less than pVal. Even if pVal also contains NULL values. |
+** |
+** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM). |
+** Otherwise, set *pRes to a negative, zero or positive value if the |
+** key in pVal is smaller than, equal to or larger than the current sorter |
+** key. |
+** |
+** This routine forms the core of the OP_SorterCompare opcode, which in |
+** turn is used to verify uniqueness when constructing a UNIQUE INDEX. |
+*/ |
+SQLITE_PRIVATE int sqlite3VdbeSorterCompare( |
+ const VdbeCursor *pCsr, /* Sorter cursor */ |
+ Mem *pVal, /* Value to compare to current sorter key */ |
+ int nKeyCol, /* Compare this many columns */ |
+ int *pRes /* OUT: Result of comparison */ |
+){ |
+ VdbeSorter *pSorter; |
+ UnpackedRecord *r2; |
+ KeyInfo *pKeyInfo; |
+ int i; |
+ void *pKey; int nKey; /* Sorter key to compare pVal with */ |
+ |
+ assert( pCsr->eCurType==CURTYPE_SORTER ); |
+ pSorter = pCsr->uc.pSorter; |
+ r2 = pSorter->pUnpacked; |
+ pKeyInfo = pCsr->pKeyInfo; |
+ if( r2==0 ){ |
+ r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); |
+ if( r2==0 ) return SQLITE_NOMEM_BKPT; |
+ r2->nField = nKeyCol; |
+ } |
+ assert( r2->nField==nKeyCol ); |
+ |
+ pKey = vdbeSorterRowkey(pSorter, &nKey); |
+ sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, r2); |
+ for(i=0; i<nKeyCol; i++){ |
+ if( r2->aMem[i].flags & MEM_Null ){ |
+ *pRes = -1; |
+ return SQLITE_OK; |
+ } |
+ } |
+ |
+ *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2); |
+ return SQLITE_OK; |
+} |
+ |
+/************** End of vdbesort.c ********************************************/ |
+/************** Begin file memjournal.c **************************************/ |
+/* |
+** 2008 October 7 |
+** |
+** 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. |
+** |
+************************************************************************* |
+** |
+** This file contains code use to implement an in-memory rollback journal. |
+** The in-memory rollback journal is used to journal transactions for |
+** ":memory:" databases and when the journal_mode=MEMORY pragma is used. |
+** |
+** Update: The in-memory journal is also used to temporarily cache |
+** smaller journals that are not critical for power-loss recovery. |
+** For example, statement journals that are not too big will be held |
+** entirely in memory, thus reducing the number of file I/O calls, and |
+** more importantly, reducing temporary file creation events. If these |
+** journals become too large for memory, they are spilled to disk. But |
+** in the common case, they are usually small and no file I/O needs to |
+** occur. |
+*/ |
+/* #include "sqliteInt.h" */ |
+ |
+/* Forward references to internal structures */ |
+typedef struct MemJournal MemJournal; |
+typedef struct FilePoint FilePoint; |
+typedef struct FileChunk FileChunk; |
+ |
+/* |
+** The rollback journal is composed of a linked list of these structures. |
+** |
+** The zChunk array is always at least 8 bytes in size - usually much more. |
+** Its actual size is stored in the MemJournal.nChunkSize variable. |
+*/ |
+struct FileChunk { |
+ FileChunk *pNext; /* Next chunk in the journal */ |
+ u8 zChunk[8]; /* Content of this chunk */ |
+}; |
+ |
+/* |
+** By default, allocate this many bytes of memory for each FileChunk object. |
+*/ |
+#define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024 |
+ |
+/* |
+** For chunk size nChunkSize, return the number of bytes that should |
+** be allocated for each FileChunk structure. |
+*/ |
+#define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8)) |
+ |
+/* |
+** An instance of this object serves as a cursor into the rollback journal. |
+** The cursor can be either for reading or writing. |
+*/ |
+struct FilePoint { |
+ sqlite3_int64 iOffset; /* Offset from the beginning of the file */ |
+ FileChunk *pChunk; /* Specific chunk into which cursor points */ |
+}; |
+ |
+/* |
+** This structure is a subclass of sqlite3_file. Each open memory-journal |
+** is an instance of this class. |
+*/ |
+struct MemJournal { |
+ const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */ |
+ int nChunkSize; /* In-memory chunk-size */ |
+ |
+ int nSpill; /* Bytes of data before flushing */ |
+ int nSize; /* Bytes of data currently in memory */ |
+ FileChunk *pFirst; /* Head of in-memory chunk-list */ |
+ FilePoint endpoint; /* Pointer to the end of the file */ |
+ FilePoint readpoint; /* Pointer to the end of the last xRead() */ |
+ |
+ int flags; /* xOpen flags */ |
+ sqlite3_vfs *pVfs; /* The "real" underlying VFS */ |
+ const char *zJournal; /* Name of the journal file */ |
+}; |
+ |
+/* |
+** Read data from the in-memory journal file. This is the implementation |
+** of the sqlite3_vfs.xRead method. |
+*/ |
+static int memjrnlRead( |
+ sqlite3_file *pJfd, /* The journal file from which to read */ |
+ void *zBuf, /* Put the results here */ |
+ int iAmt, /* Number of bytes to read */ |
+ sqlite_int64 iOfst /* Begin reading at this offset */ |
+){ |
+ MemJournal *p = (MemJournal *)pJfd; |
+ u8 *zOut = zBuf; |
+ int nRead = iAmt; |
+ int iChunkOffset; |
+ FileChunk *pChunk; |
+ |
+#ifdef SQLITE_ENABLE_ATOMIC_WRITE |
+ if( (iAmt+iOfst)>p->endpoint.iOffset ){ |
+ return SQLITE_IOERR_SHORT_READ; |
+ } |
+#endif |
+ |
+ assert( (iAmt+iOfst)<=p->endpoint.iOffset ); |
+ assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 ); |
+ if( p->readpoint.iOffset!=iOfst || iOfst==0 ){ |
+ sqlite3_int64 iOff = 0; |
+ for(pChunk=p->pFirst; |
+ ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst; |
+ pChunk=pChunk->pNext |
+ ){ |
+ iOff += p->nChunkSize; |
+ } |
+ }else{ |
+ pChunk = p->readpoint.pChunk; |
+ assert( pChunk!=0 ); |
+ } |
+ |
+ iChunkOffset = (int)(iOfst%p->nChunkSize); |
+ do { |
+ int iSpace = p->nChunkSize - iChunkOffset; |
+ int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset)); |
+ memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy); |
+ zOut += nCopy; |
+ nRead -= iSpace; |
+ iChunkOffset = 0; |
+ } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 ); |
+ p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0; |
+ p->readpoint.pChunk = pChunk; |
+ |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Free the list of FileChunk structures headed at MemJournal.pFirst. |
+*/ |
+static void memjrnlFreeChunks(MemJournal *p){ |
+ FileChunk *pIter; |
+ FileChunk *pNext; |
+ for(pIter=p->pFirst; pIter; pIter=pNext){ |
+ pNext = pIter->pNext; |
+ sqlite3_free(pIter); |
+ } |
+ p->pFirst = 0; |
+} |
+ |
+/* |
+** Flush the contents of memory to a real file on disk. |
+*/ |
+static int memjrnlCreateFile(MemJournal *p){ |
+ int rc; |
+ sqlite3_file *pReal = (sqlite3_file*)p; |
+ MemJournal copy = *p; |
+ |
+ memset(p, 0, sizeof(MemJournal)); |
+ rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0); |
+ if( rc==SQLITE_OK ){ |
+ int nChunk = copy.nChunkSize; |
+ i64 iOff = 0; |
+ FileChunk *pIter; |
+ for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){ |
+ if( iOff + nChunk > copy.endpoint.iOffset ){ |
+ nChunk = copy.endpoint.iOffset - iOff; |
+ } |
+ rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff); |
+ if( rc ) break; |
+ iOff += nChunk; |
+ } |
+ if( rc==SQLITE_OK ){ |
+ /* No error has occurred. Free the in-memory buffers. */ |
+ memjrnlFreeChunks(©); |
+ } |
+ } |
+ if( rc!=SQLITE_OK ){ |
+ /* If an error occurred while creating or writing to the file, restore |
+ ** the original before returning. This way, SQLite uses the in-memory |
+ ** journal data to roll back changes made to the internal page-cache |
+ ** before this function was called. */ |
+ sqlite3OsClose(pReal); |
+ *p = copy; |
+ } |
+ return rc; |
+} |
+ |
+ |
+/* |
+** Write data to the file. |
+*/ |
+static int memjrnlWrite( |
+ sqlite3_file *pJfd, /* The journal file into which to write */ |
+ const void *zBuf, /* Take data to be written from here */ |
+ int iAmt, /* Number of bytes to write */ |
+ sqlite_int64 iOfst /* Begin writing at this offset into the file */ |
+){ |
+ MemJournal *p = (MemJournal *)pJfd; |
+ int nWrite = iAmt; |
+ u8 *zWrite = (u8 *)zBuf; |
+ |
+ /* If the file should be created now, create it and write the new data |
+ ** into the file on disk. */ |
+ if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){ |
+ int rc = memjrnlCreateFile(p); |
+ if( rc==SQLITE_OK ){ |
+ rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst); |
+ } |
+ return rc; |
+ } |
+ |
+ /* If the contents of this write should be stored in memory */ |
+ else{ |
+ /* An in-memory journal file should only ever be appended to. Random |
+ ** access writes are not required. The only exception to this is when |
+ ** the in-memory journal is being used by a connection using the |
+ ** atomic-write optimization. In this case the first 28 bytes of the |
+ ** journal file may be written as part of committing the transaction. */ |
+ assert( iOfst==p->endpoint.iOffset || iOfst==0 ); |
+#ifdef SQLITE_ENABLE_ATOMIC_WRITE |
+ if( iOfst==0 && p->pFirst ){ |
+ assert( p->nChunkSize>iAmt ); |
+ memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt); |
+ }else |
+#else |
+ assert( iOfst>0 || p->pFirst==0 ); |
+#endif |
+ { |
+ while( nWrite>0 ){ |
+ FileChunk *pChunk = p->endpoint.pChunk; |
+ int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize); |
+ int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset); |
+ |
+ if( iChunkOffset==0 ){ |
+ /* New chunk is required to extend the file. */ |
+ FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize)); |
+ if( !pNew ){ |
+ return SQLITE_IOERR_NOMEM_BKPT; |
+ } |
+ pNew->pNext = 0; |
+ if( pChunk ){ |
+ assert( p->pFirst ); |
+ pChunk->pNext = pNew; |
+ }else{ |
+ assert( !p->pFirst ); |
+ p->pFirst = pNew; |
+ } |
+ p->endpoint.pChunk = pNew; |
+ } |
+ |
+ memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace); |
+ zWrite += iSpace; |
+ nWrite -= iSpace; |
+ p->endpoint.iOffset += iSpace; |
+ } |
+ p->nSize = iAmt + iOfst; |
+ } |
+ } |
+ |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Truncate the file. |
+** |
+** If the journal file is already on disk, truncate it there. Or, if it |
+** is still in main memory but is being truncated to zero bytes in size, |
+** ignore |
+*/ |
+static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){ |
+ MemJournal *p = (MemJournal *)pJfd; |
+ if( ALWAYS(size==0) ){ |
+ memjrnlFreeChunks(p); |
+ p->nSize = 0; |
+ p->endpoint.pChunk = 0; |
+ p->endpoint.iOffset = 0; |
+ p->readpoint.pChunk = 0; |
+ p->readpoint.iOffset = 0; |
+ } |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Close the file. |
+*/ |
+static int memjrnlClose(sqlite3_file *pJfd){ |
+ MemJournal *p = (MemJournal *)pJfd; |
+ memjrnlFreeChunks(p); |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Sync the file. |
+** |
+** If the real file has been created, call its xSync method. Otherwise, |
+** syncing an in-memory journal is a no-op. |
+*/ |
+static int memjrnlSync(sqlite3_file *pJfd, int flags){ |
+ UNUSED_PARAMETER2(pJfd, flags); |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Query the size of the file in bytes. |
+*/ |
+static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){ |
+ MemJournal *p = (MemJournal *)pJfd; |
+ *pSize = (sqlite_int64) p->endpoint.iOffset; |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Table of methods for MemJournal sqlite3_file object. |
+*/ |
+static const struct sqlite3_io_methods MemJournalMethods = { |
+ 1, /* iVersion */ |
+ memjrnlClose, /* xClose */ |
+ memjrnlRead, /* xRead */ |
+ memjrnlWrite, /* xWrite */ |
+ memjrnlTruncate, /* xTruncate */ |
+ memjrnlSync, /* xSync */ |
+ memjrnlFileSize, /* xFileSize */ |
+ 0, /* xLock */ |
+ 0, /* xUnlock */ |
+ 0, /* xCheckReservedLock */ |
+ 0, /* xFileControl */ |
+ 0, /* xSectorSize */ |
+ 0, /* xDeviceCharacteristics */ |
+ 0, /* xShmMap */ |
+ 0, /* xShmLock */ |
+ 0, /* xShmBarrier */ |
+ 0, /* xShmUnmap */ |
+ 0, /* xFetch */ |
+ 0 /* xUnfetch */ |
+}; |
+ |
+/* |
+** Open a journal file. |
+** |
+** The behaviour of the journal file depends on the value of parameter |
+** nSpill. If nSpill is 0, then the journal file is always create and |
+** accessed using the underlying VFS. If nSpill is less than zero, then |
+** all content is always stored in main-memory. Finally, if nSpill is a |
+** positive value, then the journal file is initially created in-memory |
+** but may be flushed to disk later on. In this case the journal file is |
+** flushed to disk either when it grows larger than nSpill bytes in size, |
+** or when sqlite3JournalCreate() is called. |
+*/ |
+SQLITE_PRIVATE int sqlite3JournalOpen( |
+ sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */ |
+ const char *zName, /* Name of the journal file */ |
+ sqlite3_file *pJfd, /* Preallocated, blank file handle */ |
+ int flags, /* Opening flags */ |
+ int nSpill /* Bytes buffered before opening the file */ |
+){ |
+ MemJournal *p = (MemJournal*)pJfd; |
+ |
+ /* Zero the file-handle object. If nSpill was passed zero, initialize |
+ ** it using the sqlite3OsOpen() function of the underlying VFS. In this |
+ ** case none of the code in this module is executed as a result of calls |
+ ** made on the journal file-handle. */ |
+ memset(p, 0, sizeof(MemJournal)); |
+ if( nSpill==0 ){ |
+ return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0); |
+ } |
+ |
+ if( nSpill>0 ){ |
+ p->nChunkSize = nSpill; |
+ }else{ |
+ p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk); |
+ assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) ); |
+ } |
+ |
+ p->pMethod = (const sqlite3_io_methods*)&MemJournalMethods; |
+ p->nSpill = nSpill; |
+ p->flags = flags; |
+ p->zJournal = zName; |
+ p->pVfs = pVfs; |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Open an in-memory journal file. |
+*/ |
+SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){ |
+ sqlite3JournalOpen(0, 0, pJfd, 0, -1); |
+} |
+ |
+#ifdef SQLITE_ENABLE_ATOMIC_WRITE |
+/* |
+** If the argument p points to a MemJournal structure that is not an |
+** in-memory-only journal file (i.e. is one that was opened with a +ve |
+** nSpill parameter), and the underlying file has not yet been created, |
+** create it now. |
+*/ |
+SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){ |
+ int rc = SQLITE_OK; |
+ if( p->pMethods==&MemJournalMethods && ((MemJournal*)p)->nSpill>0 ){ |
+ rc = memjrnlCreateFile((MemJournal*)p); |
+ } |
+ return rc; |
+} |
+#endif |
+ |
+/* |
+** The file-handle passed as the only argument is open on a journal file. |
+** Return true if this "journal file" is currently stored in heap memory, |
+** or false otherwise. |
+*/ |
+SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p){ |
+ return p->pMethods==&MemJournalMethods; |
+} |
+ |
+/* |
+** Return the number of bytes required to store a JournalFile that uses vfs |
+** pVfs to create the underlying on-disk files. |
+*/ |
+SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){ |
+ return MAX(pVfs->szOsFile, (int)sizeof(MemJournal)); |
+} |
+ |
+/************** End of memjournal.c ******************************************/ |
+/************** Begin file walker.c ******************************************/ |
+/* |
+** 2008 August 16 |
+** |
+** 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. |
+** |
+************************************************************************* |
+** This file contains routines used for walking the parser tree for |
+** an SQL statement. |
+*/ |
+/* #include "sqliteInt.h" */ |
+/* #include <stdlib.h> */ |
+/* #include <string.h> */ |
+ |
+ |
+/* |
+** Walk an expression tree. Invoke the callback once for each node |
+** of the expression, while descending. (In other words, the callback |
+** is invoked before visiting children.) |
+** |
+** The return value from the callback should be one of the WRC_* |
+** constants to specify how to proceed with the walk. |
+** |
+** WRC_Continue Continue descending down the tree. |
+** |
+** WRC_Prune Do not descend into child nodes. But allow |
+** the walk to continue with sibling nodes. |
+** |
+** WRC_Abort Do no more callbacks. Unwind the stack and |
+** return the top-level walk call. |
+** |
+** The return value from this routine is WRC_Abort to abandon the tree walk |
+** and WRC_Continue to continue. |
+*/ |
+static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){ |
+ int rc; |
+ testcase( ExprHasProperty(pExpr, EP_TokenOnly) ); |
+ testcase( ExprHasProperty(pExpr, EP_Reduced) ); |
+ rc = pWalker->xExprCallback(pWalker, pExpr); |
+ if( rc || ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){ |
+ return rc & WRC_Abort; |
+ } |
+ if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort; |
+ if( pExpr->pRight && walkExpr(pWalker, pExpr->pRight) ) return WRC_Abort; |
+ if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
+ if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort; |
+ }else if( pExpr->x.pList ){ |
+ if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; |
+ } |
+ return WRC_Continue; |
+} |
+SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){ |
+ return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue; |
+} |
+ |
+/* |
+** Call sqlite3WalkExpr() for every expression in list p or until |
+** an abort request is seen. |
+*/ |
+SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){ |
+ int i; |
+ struct ExprList_item *pItem; |
+ if( p ){ |
+ for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ |
+ if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort; |
+ } |
+ } |
+ return WRC_Continue; |
+} |
+ |
+/* |
+** Walk all expressions associated with SELECT statement p. Do |
+** not invoke the SELECT callback on p, but do (of course) invoke |
+** any expr callbacks and SELECT callbacks that come from subqueries. |
+** Return WRC_Abort or WRC_Continue. |
+*/ |
+SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){ |
+ if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort; |
+ if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort; |
+ if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort; |
+ if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort; |
+ if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort; |
+ if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort; |
+ if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort; |
+ return WRC_Continue; |
+} |
+ |
+/* |
+** Walk the parse trees associated with all subqueries in the |
+** FROM clause of SELECT statement p. Do not invoke the select |
+** callback on p, but do invoke it on each FROM clause subquery |
+** and on any subqueries further down in the tree. Return |
+** WRC_Abort or WRC_Continue; |
+*/ |
+SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){ |
+ SrcList *pSrc; |
+ int i; |
+ struct SrcList_item *pItem; |
+ |
+ pSrc = p->pSrc; |
+ if( ALWAYS(pSrc) ){ |
+ for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){ |
+ if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){ |
+ return WRC_Abort; |
+ } |
+ if( pItem->fg.isTabFunc |
+ && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg) |
+ ){ |
+ return WRC_Abort; |
+ } |
+ } |
+ } |
+ return WRC_Continue; |
+} |
+ |
+/* |
+** Call sqlite3WalkExpr() for every expression in Select statement p. |
+** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and |
+** on the compound select chain, p->pPrior. |
+** |
+** If it is not NULL, the xSelectCallback() callback is invoked before |
+** the walk of the expressions and FROM clause. The xSelectCallback2() |
+** method, if it is not NULL, is invoked following the walk of the |
+** expressions and FROM clause. |
+** |
+** Return WRC_Continue under normal conditions. Return WRC_Abort if |
+** there is an abort request. |
+** |
+** If the Walker does not have an xSelectCallback() then this routine |
+** is a no-op returning WRC_Continue. |
+*/ |
+SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){ |
+ int rc; |
+ if( p==0 || (pWalker->xSelectCallback==0 && pWalker->xSelectCallback2==0) ){ |
+ return WRC_Continue; |
+ } |
+ rc = WRC_Continue; |
+ pWalker->walkerDepth++; |
+ while( p ){ |
+ if( pWalker->xSelectCallback ){ |
+ rc = pWalker->xSelectCallback(pWalker, p); |
+ if( rc ) break; |
+ } |
+ if( sqlite3WalkSelectExpr(pWalker, p) |
+ || sqlite3WalkSelectFrom(pWalker, p) |
+ ){ |
+ pWalker->walkerDepth--; |
+ return WRC_Abort; |
+ } |
+ if( pWalker->xSelectCallback2 ){ |
+ pWalker->xSelectCallback2(pWalker, p); |
+ } |
+ p = p->pPrior; |
+ } |
+ pWalker->walkerDepth--; |
+ return rc & WRC_Abort; |
+} |
+ |
+/************** End of walker.c **********************************************/ |
+/************** Begin file resolve.c *****************************************/ |
+/* |
+** 2008 August 18 |
+** |
+** 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. |
+** |
+************************************************************************* |
+** |
+** This file contains routines used for walking the parser tree and |
+** resolve all identifiers by associating them with a particular |
+** table and column. |
+*/ |
+/* #include "sqliteInt.h" */ |
+ |
+/* |
+** Walk the expression tree pExpr and increase the aggregate function |
+** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node. |
+** This needs to occur when copying a TK_AGG_FUNCTION node from an |
+** outer query into an inner subquery. |
+** |
+** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) |
+** is a helper function - a callback for the tree walker. |
+*/ |
+static int incrAggDepth(Walker *pWalker, Expr *pExpr){ |
+ if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n; |
+ return WRC_Continue; |
+} |
+static void incrAggFunctionDepth(Expr *pExpr, int N){ |
+ if( N>0 ){ |
+ Walker w; |
+ memset(&w, 0, sizeof(w)); |
+ w.xExprCallback = incrAggDepth; |
+ w.u.n = N; |
+ sqlite3WalkExpr(&w, pExpr); |
+ } |
+} |
+ |
+/* |
+** Turn the pExpr expression into an alias for the iCol-th column of the |
+** result set in pEList. |
+** |
+** If the reference is followed by a COLLATE operator, then make sure |
+** the COLLATE operator is preserved. For example: |
+** |
+** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase; |
+** |
+** Should be transformed into: |
+** |
+** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase; |
+** |
+** The nSubquery parameter specifies how many levels of subquery the |
+** alias is removed from the original expression. The usual value is |
+** zero but it might be more if the alias is contained within a subquery |
+** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION |
+** structures must be increased by the nSubquery amount. |
+*/ |
+static void resolveAlias( |
+ Parse *pParse, /* Parsing context */ |
+ ExprList *pEList, /* A result set */ |
+ int iCol, /* A column in the result set. 0..pEList->nExpr-1 */ |
+ Expr *pExpr, /* Transform this into an alias to the result set */ |
+ const char *zType, /* "GROUP" or "ORDER" or "" */ |
+ int nSubquery /* Number of subqueries that the label is moving */ |
+){ |
+ Expr *pOrig; /* The iCol-th column of the result set */ |
+ Expr *pDup; /* Copy of pOrig */ |
+ sqlite3 *db; /* The database connection */ |
+ |
+ assert( iCol>=0 && iCol<pEList->nExpr ); |
+ pOrig = pEList->a[iCol].pExpr; |
+ assert( pOrig!=0 ); |
+ db = pParse->db; |
+ pDup = sqlite3ExprDup(db, pOrig, 0); |
+ if( pDup==0 ) return; |
+ if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery); |
+ if( pExpr->op==TK_COLLATE ){ |
+ pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken); |
+ } |
+ ExprSetProperty(pDup, EP_Alias); |
+ |
+ /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This |
+ ** prevents ExprDelete() from deleting the Expr structure itself, |
+ ** allowing it to be repopulated by the memcpy() on the following line. |
+ ** The pExpr->u.zToken might point into memory that will be freed by the |
+ ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to |
+ ** make a copy of the token before doing the sqlite3DbFree(). |
+ */ |
+ ExprSetProperty(pExpr, EP_Static); |
+ sqlite3ExprDelete(db, pExpr); |
+ memcpy(pExpr, pDup, sizeof(*pExpr)); |
+ if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){ |
+ assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 ); |
+ pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken); |
+ pExpr->flags |= EP_MemToken; |
+ } |
+ sqlite3DbFree(db, pDup); |
+} |
+ |
+ |
+/* |
+** Return TRUE if the name zCol occurs anywhere in the USING clause. |
+** |
+** Return FALSE if the USING clause is NULL or if it does not contain |
+** zCol. |
+*/ |
+static int nameInUsingClause(IdList *pUsing, const char *zCol){ |
+ if( pUsing ){ |
+ int k; |
+ for(k=0; k<pUsing->nId; k++){ |
+ if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1; |
+ } |
+ } |
+ return 0; |
+} |
+ |
+/* |
+** Subqueries stores the original database, table and column names for their |
+** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN". |
+** Check to see if the zSpan given to this routine matches the zDb, zTab, |
+** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will |
+** match anything. |
+*/ |
+SQLITE_PRIVATE int sqlite3MatchSpanName( |
+ const char *zSpan, |
+ const char *zCol, |
+ const char *zTab, |
+ const char *zDb |
+){ |
+ int n; |
+ for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){} |
+ if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){ |
+ return 0; |
+ } |
+ zSpan += n+1; |
+ for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){} |
+ if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){ |
+ return 0; |
+ } |
+ zSpan += n+1; |
+ if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){ |
+ return 0; |
+ } |
+ return 1; |
+} |
+ |
+/* |
+** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up |
+** that name in the set of source tables in pSrcList and make the pExpr |
+** expression node refer back to that source column. The following changes |
+** are made to pExpr: |
+** |
+** pExpr->iDb Set the index in db->aDb[] of the database X |
+** (even if X is implied). |
+** pExpr->iTable Set to the cursor number for the table obtained |
+** from pSrcList. |
+** pExpr->pTab Points to the Table structure of X.Y (even if |
+** X and/or Y are implied.) |
+** pExpr->iColumn Set to the column number within the table. |
+** pExpr->op Set to TK_COLUMN. |
+** pExpr->pLeft Any expression this points to is deleted |
+** pExpr->pRight Any expression this points to is deleted. |
+** |
+** The zDb variable is the name of the database (the "X"). This value may be |
+** NULL meaning that name is of the form Y.Z or Z. Any available database |
+** can be used. The zTable variable is the name of the table (the "Y"). This |
+** value can be NULL if zDb is also NULL. If zTable is NULL it |
+** means that the form of the name is Z and that columns from any table |
+** can be used. |
+** |
+** If the name cannot be resolved unambiguously, leave an error message |
+** in pParse and return WRC_Abort. Return WRC_Prune on success. |
+*/ |
+static int lookupName( |
+ Parse *pParse, /* The parsing context */ |
+ const char *zDb, /* Name of the database containing table, or NULL */ |
+ const char *zTab, /* Name of table containing column, or NULL */ |
+ const char *zCol, /* Name of the column. */ |
+ NameContext *pNC, /* The name context used to resolve the name */ |
+ Expr *pExpr /* Make this EXPR node point to the selected column */ |
+){ |
+ int i, j; /* Loop counters */ |
+ int cnt = 0; /* Number of matching column names */ |
+ int cntTab = 0; /* Number of matching table names */ |
+ int nSubquery = 0; /* How many levels of subquery */ |
+ sqlite3 *db = pParse->db; /* The database connection */ |
+ struct SrcList_item *pItem; /* Use for looping over pSrcList items */ |
+ struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ |
+ NameContext *pTopNC = pNC; /* First namecontext in the list */ |
+ Schema *pSchema = 0; /* Schema of the expression */ |
+ int isTrigger = 0; /* True if resolved to a trigger column */ |
+ Table *pTab = 0; /* Table hold the row */ |
+ Column *pCol; /* A column of pTab */ |
+ |
+ assert( pNC ); /* the name context cannot be NULL. */ |
+ assert( zCol ); /* The Z in X.Y.Z cannot be NULL */ |
+ assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); |
+ |
+ /* Initialize the node to no-match */ |
+ pExpr->iTable = -1; |
+ pExpr->pTab = 0; |
+ ExprSetVVAProperty(pExpr, EP_NoReduce); |
+ |
+ /* Translate the schema name in zDb into a pointer to the corresponding |
+ ** schema. If not found, pSchema will remain NULL and nothing will match |
+ ** resulting in an appropriate error message toward the end of this routine |
+ */ |
+ if( zDb ){ |
+ testcase( pNC->ncFlags & NC_PartIdx ); |
+ testcase( pNC->ncFlags & NC_IsCheck ); |
+ if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){ |
+ /* Silently ignore database qualifiers inside CHECK constraints and |
+ ** partial indices. Do not raise errors because that might break |
+ ** legacy and because it does not hurt anything to just ignore the |
+ ** database name. */ |
+ zDb = 0; |
+ }else{ |
+ for(i=0; i<db->nDb; i++){ |
+ assert( db->aDb[i].zDbSName ); |
+ if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){ |
+ pSchema = db->aDb[i].pSchema; |
+ break; |
+ } |
+ } |
+ } |
+ } |
+ |
+ /* Start at the inner-most context and move outward until a match is found */ |
+ while( pNC && cnt==0 ){ |
+ ExprList *pEList; |
+ SrcList *pSrcList = pNC->pSrcList; |
+ |
+ if( pSrcList ){ |
+ for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ |
+ pTab = pItem->pTab; |
+ assert( pTab!=0 && pTab->zName!=0 ); |
+ assert( pTab->nCol>0 ); |
+ if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){ |
+ int hit = 0; |
+ pEList = pItem->pSelect->pEList; |
+ for(j=0; j<pEList->nExpr; j++){ |
+ if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){ |
+ cnt++; |
+ cntTab = 2; |
+ pMatch = pItem; |
+ pExpr->iColumn = j; |
+ hit = 1; |
+ } |
+ } |
+ if( hit || zTab==0 ) continue; |
+ } |
+ if( zDb && pTab->pSchema!=pSchema ){ |
+ continue; |
+ } |
+ if( zTab ){ |
+ const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName; |
+ assert( zTabName!=0 ); |
+ if( sqlite3StrICmp(zTabName, zTab)!=0 ){ |
+ continue; |
+ } |
+ } |
+ if( 0==(cntTab++) ){ |
+ pMatch = pItem; |
+ } |
+ for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ |
+ if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ |
+ /* If there has been exactly one prior match and this match |
+ ** is for the right-hand table of a NATURAL JOIN or is in a |
+ ** USING clause, then skip this match. |
+ */ |
+ if( cnt==1 ){ |
+ if( pItem->fg.jointype & JT_NATURAL ) continue; |
+ if( nameInUsingClause(pItem->pUsing, zCol) ) continue; |
+ } |
+ cnt++; |
+ pMatch = pItem; |
+ /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ |
+ pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j; |
+ break; |
+ } |
+ } |
+ } |
+ if( pMatch ){ |
+ pExpr->iTable = pMatch->iCursor; |
+ pExpr->pTab = pMatch->pTab; |
+ /* RIGHT JOIN not (yet) supported */ |
+ assert( (pMatch->fg.jointype & JT_RIGHT)==0 ); |
+ if( (pMatch->fg.jointype & JT_LEFT)!=0 ){ |
+ ExprSetProperty(pExpr, EP_CanBeNull); |
+ } |
+ pSchema = pExpr->pTab->pSchema; |
+ } |
+ } /* if( pSrcList ) */ |
+ |
+#ifndef SQLITE_OMIT_TRIGGER |
+ /* If we have not already resolved the name, then maybe |
+ ** it is a new.* or old.* trigger argument reference |
+ */ |
+ if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){ |
+ int op = pParse->eTriggerOp; |
+ assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT ); |
+ if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){ |
+ pExpr->iTable = 1; |
+ pTab = pParse->pTriggerTab; |
+ }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){ |
+ pExpr->iTable = 0; |
+ pTab = pParse->pTriggerTab; |
+ }else{ |
+ pTab = 0; |
+ } |
+ |
+ if( pTab ){ |
+ int iCol; |
+ pSchema = pTab->pSchema; |
+ cntTab++; |
+ for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){ |
+ if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ |
+ if( iCol==pTab->iPKey ){ |
+ iCol = -1; |
+ } |
+ break; |
+ } |
+ } |
+ if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){ |
+ /* IMP: R-51414-32910 */ |
+ iCol = -1; |
+ } |
+ if( iCol<pTab->nCol ){ |
+ cnt++; |
+ if( iCol<0 ){ |
+ pExpr->affinity = SQLITE_AFF_INTEGER; |
+ }else if( pExpr->iTable==0 ){ |
+ testcase( iCol==31 ); |
+ testcase( iCol==32 ); |
+ pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol)); |
+ }else{ |
+ testcase( iCol==31 ); |
+ testcase( iCol==32 ); |
+ pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol)); |
+ } |
+ pExpr->iColumn = (i16)iCol; |
+ pExpr->pTab = pTab; |
+ isTrigger = 1; |
+ } |
+ } |
+ } |
+#endif /* !defined(SQLITE_OMIT_TRIGGER) */ |
+ |
+ /* |
+ ** Perhaps the name is a reference to the ROWID |
+ */ |
+ if( cnt==0 |
+ && cntTab==1 |
+ && pMatch |
+ && (pNC->ncFlags & NC_IdxExpr)==0 |
+ && sqlite3IsRowid(zCol) |
+ && VisibleRowid(pMatch->pTab) |
+ ){ |
+ cnt = 1; |
+ pExpr->iColumn = -1; |
+ pExpr->affinity = SQLITE_AFF_INTEGER; |
+ } |
+ |
+ /* |
+ ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z |
+ ** might refer to an result-set alias. This happens, for example, when |
+ ** we are resolving names in the WHERE clause of the following command: |
+ ** |
+ ** SELECT a+b AS x FROM table WHERE x<10; |
+ ** |
+ ** In cases like this, replace pExpr with a copy of the expression that |
+ ** forms the result set entry ("a+b" in the example) and return immediately. |
+ ** Note that the expression in the result set should have already been |
+ ** resolved by the time the WHERE clause is resolved. |
+ ** |
+ ** The ability to use an output result-set column in the WHERE, GROUP BY, |
+ ** or HAVING clauses, or as part of a larger expression in the ORDER BY |
+ ** clause is not standard SQL. This is a (goofy) SQLite extension, that |
+ ** is supported for backwards compatibility only. Hence, we issue a warning |
+ ** on sqlite3_log() whenever the capability is used. |
+ */ |
+ if( (pEList = pNC->pEList)!=0 |
+ && zTab==0 |
+ && cnt==0 |
+ ){ |
+ for(j=0; j<pEList->nExpr; j++){ |
+ char *zAs = pEList->a[j].zName; |
+ if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ |
+ Expr *pOrig; |
+ assert( pExpr->pLeft==0 && pExpr->pRight==0 ); |
+ assert( pExpr->x.pList==0 ); |
+ assert( pExpr->x.pSelect==0 ); |
+ pOrig = pEList->a[j].pExpr; |
+ if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){ |
+ sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs); |
+ return WRC_Abort; |
+ } |
+ if( sqlite3ExprVectorSize(pOrig)!=1 ){ |
+ sqlite3ErrorMsg(pParse, "row value misused"); |
+ return WRC_Abort; |
+ } |
+ resolveAlias(pParse, pEList, j, pExpr, "", nSubquery); |
+ cnt = 1; |
+ pMatch = 0; |
+ assert( zTab==0 && zDb==0 ); |
+ goto lookupname_end; |
+ } |
+ } |
+ } |
+ |
+ /* Advance to the next name context. The loop will exit when either |
+ ** we have a match (cnt>0) or when we run out of name contexts. |
+ */ |
+ if( cnt==0 ){ |
+ pNC = pNC->pNext; |
+ nSubquery++; |
+ } |
+ } |
+ |
+ /* |
+ ** If X and Y are NULL (in other words if only the column name Z is |
+ ** supplied) and the value of Z is enclosed in double-quotes, then |
+ ** Z is a string literal if it doesn't match any column names. In that |
+ ** case, we need to return right away and not make any changes to |
+ ** pExpr. |
+ ** |
+ ** Because no reference was made to outer contexts, the pNC->nRef |
+ ** fields are not changed in any context. |
+ */ |
+ if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){ |
+ pExpr->op = TK_STRING; |
+ pExpr->pTab = 0; |
+ return WRC_Prune; |
+ } |
+ |
+ /* |
+ ** cnt==0 means there was not match. cnt>1 means there were two or |
+ ** more matches. Either way, we have an error. |
+ */ |
+ if( cnt!=1 ){ |
+ const char *zErr; |
+ zErr = cnt==0 ? "no such column" : "ambiguous column name"; |
+ if( zDb ){ |
+ sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol); |
+ }else if( zTab ){ |
+ sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol); |
+ }else{ |
+ sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol); |
+ } |
+ pParse->checkSchema = 1; |
+ pTopNC->nErr++; |
+ } |
+ |
+ /* If a column from a table in pSrcList is referenced, then record |
+ ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes |
+ ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the |
+ ** column number is greater than the number of bits in the bitmask |
+ ** then set the high-order bit of the bitmask. |
+ */ |
+ if( pExpr->iColumn>=0 && pMatch!=0 ){ |
+ int n = pExpr->iColumn; |
+ testcase( n==BMS-1 ); |
+ if( n>=BMS ){ |
+ n = BMS-1; |
+ } |
+ assert( pMatch->iCursor==pExpr->iTable ); |
+ pMatch->colUsed |= ((Bitmask)1)<<n; |
+ } |
+ |
+ /* Clean up and return |
+ */ |
+ sqlite3ExprDelete(db, pExpr->pLeft); |
+ pExpr->pLeft = 0; |
+ sqlite3ExprDelete(db, pExpr->pRight); |
+ pExpr->pRight = 0; |
+ pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN); |
+lookupname_end: |
+ if( cnt==1 ){ |
+ assert( pNC!=0 ); |
+ if( !ExprHasProperty(pExpr, EP_Alias) ){ |
+ sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); |
+ } |
+ /* Increment the nRef value on all name contexts from TopNC up to |
+ ** the point where the name matched. */ |
+ for(;;){ |
+ assert( pTopNC!=0 ); |
+ pTopNC->nRef++; |
+ if( pTopNC==pNC ) break; |
+ pTopNC = pTopNC->pNext; |
+ } |
+ return WRC_Prune; |
+ } else { |
+ return WRC_Abort; |
+ } |
+} |
+ |
+/* |
+** Allocate and return a pointer to an expression to load the column iCol |
+** from datasource iSrc in SrcList pSrc. |
+*/ |
+SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){ |
+ Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0); |
+ if( p ){ |
+ struct SrcList_item *pItem = &pSrc->a[iSrc]; |
+ p->pTab = pItem->pTab; |
+ p->iTable = pItem->iCursor; |
+ if( p->pTab->iPKey==iCol ){ |
+ p->iColumn = -1; |
+ }else{ |
+ p->iColumn = (ynVar)iCol; |
+ testcase( iCol==BMS ); |
+ testcase( iCol==BMS-1 ); |
+ pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol); |
+ } |
+ ExprSetProperty(p, EP_Resolved); |
+ } |
+ return p; |
+} |
+ |
+/* |
+** Report an error that an expression is not valid for some set of |
+** pNC->ncFlags values determined by validMask. |
+*/ |
+static void notValid( |
+ Parse *pParse, /* Leave error message here */ |
+ NameContext *pNC, /* The name context */ |
+ const char *zMsg, /* Type of error */ |
+ int validMask /* Set of contexts for which prohibited */ |
+){ |
+ assert( (validMask&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr))==0 ); |
+ if( (pNC->ncFlags & validMask)!=0 ){ |
+ const char *zIn = "partial index WHERE clauses"; |
+ if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions"; |
+#ifndef SQLITE_OMIT_CHECK |
+ else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints"; |
+#endif |
+ sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn); |
+ } |
+} |
+ |
+/* |
+** Expression p should encode a floating point value between 1.0 and 0.0. |
+** Return 1024 times this value. Or return -1 if p is not a floating point |
+** value between 1.0 and 0.0. |
+*/ |
+static int exprProbability(Expr *p){ |
+ double r = -1.0; |
+ if( p->op!=TK_FLOAT ) return -1; |
+ sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8); |
+ assert( r>=0.0 ); |
+ if( r>1.0 ) return -1; |
+ return (int)(r*134217728.0); |
+} |
+ |
+/* |
+** This routine is callback for sqlite3WalkExpr(). |
+** |
+** Resolve symbolic names into TK_COLUMN operators for the current |
+** node in the expression tree. Return 0 to continue the search down |
+** the tree or 2 to abort the tree walk. |
+** |
+** This routine also does error checking and name resolution for |
+** function names. The operator for aggregate functions is changed |
+** to TK_AGG_FUNCTION. |
+*/ |
+static int resolveExprStep(Walker *pWalker, Expr *pExpr){ |
+ NameContext *pNC; |
+ Parse *pParse; |
+ |
+ pNC = pWalker->u.pNC; |
+ assert( pNC!=0 ); |
+ pParse = pNC->pParse; |
+ assert( pParse==pWalker->pParse ); |
+ |
+ if( ExprHasProperty(pExpr, EP_Resolved) ) return WRC_Prune; |
+ ExprSetProperty(pExpr, EP_Resolved); |
+#ifndef NDEBUG |
+ if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){ |
+ SrcList *pSrcList = pNC->pSrcList; |
+ int i; |
+ for(i=0; i<pNC->pSrcList->nSrc; i++){ |
+ assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab); |
+ } |
+ } |
+#endif |
+ switch( pExpr->op ){ |
+ |
+#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) |
+ /* The special operator TK_ROW means use the rowid for the first |
+ ** column in the FROM clause. This is used by the LIMIT and ORDER BY |
+ ** clause processing on UPDATE and DELETE statements. |
+ */ |
+ case TK_ROW: { |
+ SrcList *pSrcList = pNC->pSrcList; |
+ struct SrcList_item *pItem; |
+ assert( pSrcList && pSrcList->nSrc==1 ); |
+ pItem = pSrcList->a; |
+ pExpr->op = TK_COLUMN; |
+ pExpr->pTab = pItem->pTab; |
+ pExpr->iTable = pItem->iCursor; |
+ pExpr->iColumn = -1; |
+ pExpr->affinity = SQLITE_AFF_INTEGER; |
+ break; |
+ } |
+#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) |
+ && !defined(SQLITE_OMIT_SUBQUERY) */ |
+ |
+ /* A lone identifier is the name of a column. |
+ */ |
+ case TK_ID: { |
+ return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr); |
+ } |
+ |
+ /* A table name and column name: ID.ID |
+ ** Or a database, table and column: ID.ID.ID |
+ */ |
+ case TK_DOT: { |
+ const char *zColumn; |
+ const char *zTable; |
+ const char *zDb; |
+ Expr *pRight; |
+ |
+ /* if( pSrcList==0 ) break; */ |
+ notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr); |
+ pRight = pExpr->pRight; |
+ if( pRight->op==TK_ID ){ |
+ zDb = 0; |
+ zTable = pExpr->pLeft->u.zToken; |
+ zColumn = pRight->u.zToken; |
+ }else{ |
+ assert( pRight->op==TK_DOT ); |
+ zDb = pExpr->pLeft->u.zToken; |
+ zTable = pRight->pLeft->u.zToken; |
+ zColumn = pRight->pRight->u.zToken; |
+ } |
+ return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr); |
+ } |
+ |
+ /* Resolve function names |
+ */ |
+ case TK_FUNCTION: { |
+ ExprList *pList = pExpr->x.pList; /* The argument list */ |
+ int n = pList ? pList->nExpr : 0; /* Number of arguments */ |
+ int no_such_func = 0; /* True if no such function exists */ |
+ int wrong_num_args = 0; /* True if wrong number of arguments */ |
+ int is_agg = 0; /* True if is an aggregate function */ |
+ int nId; /* Number of characters in function name */ |
+ const char *zId; /* The function name. */ |
+ FuncDef *pDef; /* Information about the function */ |
+ u8 enc = ENC(pParse->db); /* The database encoding */ |
+ |
+ assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
+ zId = pExpr->u.zToken; |
+ nId = sqlite3Strlen30(zId); |
+ pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0); |
+ if( pDef==0 ){ |
+ pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0); |
+ if( pDef==0 ){ |
+ no_such_func = 1; |
+ }else{ |
+ wrong_num_args = 1; |
+ } |
+ }else{ |
+ is_agg = pDef->xFinalize!=0; |
+ if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ |
+ ExprSetProperty(pExpr, EP_Unlikely|EP_Skip); |
+ if( n==2 ){ |
+ pExpr->iTable = exprProbability(pList->a[1].pExpr); |
+ if( pExpr->iTable<0 ){ |
+ sqlite3ErrorMsg(pParse, |
+ "second argument to likelihood() must be a " |
+ "constant between 0.0 and 1.0"); |
+ pNC->nErr++; |
+ } |
+ }else{ |
+ /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is |
+ ** equivalent to likelihood(X, 0.0625). |
+ ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is |
+ ** short-hand for likelihood(X,0.0625). |
+ ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand |
+ ** for likelihood(X,0.9375). |
+ ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent |
+ ** to likelihood(X,0.9375). */ |
+ /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */ |
+ pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120; |
+ } |
+ } |
+#ifndef SQLITE_OMIT_AUTHORIZATION |
+ { |
+ int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0); |
+ if( auth!=SQLITE_OK ){ |
+ if( auth==SQLITE_DENY ){ |
+ sqlite3ErrorMsg(pParse, "not authorized to use function: %s", |
+ pDef->zName); |
+ pNC->nErr++; |
+ } |
+ pExpr->op = TK_NULL; |
+ return WRC_Prune; |
+ } |
+ } |
+#endif |
+ if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){ |
+ /* For the purposes of the EP_ConstFunc flag, date and time |
+ ** functions and other functions that change slowly are considered |
+ ** constant because they are constant for the duration of one query */ |
+ ExprSetProperty(pExpr,EP_ConstFunc); |
+ } |
+ if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){ |
+ /* Date/time functions that use 'now', and other functions like |
+ ** sqlite_version() that might change over time cannot be used |
+ ** in an index. */ |
+ notValid(pParse, pNC, "non-deterministic functions", |
+ NC_IdxExpr|NC_PartIdx); |
+ } |
+ } |
+ if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){ |
+ sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); |
+ pNC->nErr++; |
+ is_agg = 0; |
+ }else if( no_such_func && pParse->db->init.busy==0 |
+#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION |
+ && pParse->explain==0 |
+#endif |
+ ){ |
+ sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); |
+ pNC->nErr++; |
+ }else if( wrong_num_args ){ |
+ sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", |
+ nId, zId); |
+ pNC->nErr++; |
+ } |
+ if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg; |
+ sqlite3WalkExprList(pWalker, pList); |
+ if( is_agg ){ |
+ NameContext *pNC2 = pNC; |
+ pExpr->op = TK_AGG_FUNCTION; |
+ pExpr->op2 = 0; |
+ while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){ |
+ pExpr->op2++; |
+ pNC2 = pNC2->pNext; |
+ } |
+ assert( pDef!=0 ); |
+ if( pNC2 ){ |
+ assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg ); |
+ testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 ); |
+ pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX); |
+ |
+ } |
+ pNC->ncFlags |= NC_AllowAgg; |
+ } |
+ /* FIX ME: Compute pExpr->affinity based on the expected return |
+ ** type of the function |
+ */ |
+ return WRC_Prune; |
+ } |
+#ifndef SQLITE_OMIT_SUBQUERY |
+ case TK_SELECT: |
+ case TK_EXISTS: testcase( pExpr->op==TK_EXISTS ); |
+#endif |
+ case TK_IN: { |
+ testcase( pExpr->op==TK_IN ); |
+ if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
+ int nRef = pNC->nRef; |
+ notValid(pParse, pNC, "subqueries", NC_IsCheck|NC_PartIdx|NC_IdxExpr); |
+ sqlite3WalkSelect(pWalker, pExpr->x.pSelect); |
+ assert( pNC->nRef>=nRef ); |
+ if( nRef!=pNC->nRef ){ |
+ ExprSetProperty(pExpr, EP_VarSelect); |
+ pNC->ncFlags |= NC_VarSelect; |
+ } |
+ } |
+ break; |
+ } |
+ case TK_VARIABLE: { |
+ notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr); |
+ break; |
+ } |
+ case TK_BETWEEN: |
+ case TK_EQ: |
+ case TK_NE: |
+ case TK_LT: |
+ case TK_LE: |
+ case TK_GT: |
+ case TK_GE: |
+ case TK_IS: |
+ case TK_ISNOT: { |
+ int nLeft, nRight; |
+ if( pParse->db->mallocFailed ) break; |
+ assert( pExpr->pLeft!=0 ); |
+ nLeft = sqlite3ExprVectorSize(pExpr->pLeft); |
+ if( pExpr->op==TK_BETWEEN ){ |
+ nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr); |
+ if( nRight==nLeft ){ |
+ nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr); |
+ } |
+ }else{ |
+ assert( pExpr->pRight!=0 ); |
+ nRight = sqlite3ExprVectorSize(pExpr->pRight); |
+ } |
+ if( nLeft!=nRight ){ |
+ testcase( pExpr->op==TK_EQ ); |
+ testcase( pExpr->op==TK_NE ); |
+ testcase( pExpr->op==TK_LT ); |
+ testcase( pExpr->op==TK_LE ); |
+ testcase( pExpr->op==TK_GT ); |
+ testcase( pExpr->op==TK_GE ); |
+ testcase( pExpr->op==TK_IS ); |
+ testcase( pExpr->op==TK_ISNOT ); |
+ testcase( pExpr->op==TK_BETWEEN ); |
+ sqlite3ErrorMsg(pParse, "row value misused"); |
+ } |
+ break; |
+ } |
+ } |
+ return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue; |
+} |
+ |
+/* |
+** pEList is a list of expressions which are really the result set of the |
+** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause. |
+** This routine checks to see if pE is a simple identifier which corresponds |
+** to the AS-name of one of the terms of the expression list. If it is, |
+** this routine return an integer between 1 and N where N is the number of |
+** elements in pEList, corresponding to the matching entry. If there is |
+** no match, or if pE is not a simple identifier, then this routine |
+** return 0. |
+** |
+** pEList has been resolved. pE has not. |
+*/ |
+static int resolveAsName( |
+ Parse *pParse, /* Parsing context for error messages */ |
+ ExprList *pEList, /* List of expressions to scan */ |
+ Expr *pE /* Expression we are trying to match */ |
+){ |
+ int i; /* Loop counter */ |
+ |
+ UNUSED_PARAMETER(pParse); |
+ |
+ if( pE->op==TK_ID ){ |
+ char *zCol = pE->u.zToken; |
+ for(i=0; i<pEList->nExpr; i++){ |
+ char *zAs = pEList->a[i].zName; |
+ if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ |
+ return i+1; |
+ } |
+ } |
+ } |
+ return 0; |
+} |
+ |
+/* |
+** pE is a pointer to an expression which is a single term in the |
+** ORDER BY of a compound SELECT. The expression has not been |
+** name resolved. |
+** |
+** At the point this routine is called, we already know that the |
+** ORDER BY term is not an integer index into the result set. That |
+** case is handled by the calling routine. |
+** |
+** Attempt to match pE against result set columns in the left-most |
+** SELECT statement. Return the index i of the matching column, |
+** as an indication to the caller that it should sort by the i-th column. |
+** The left-most column is 1. In other words, the value returned is the |
+** same integer value that would be used in the SQL statement to indicate |
+** the column. |
+** |
+** If there is no match, return 0. Return -1 if an error occurs. |
+*/ |
+static int resolveOrderByTermToExprList( |
+ Parse *pParse, /* Parsing context for error messages */ |
+ Select *pSelect, /* The SELECT statement with the ORDER BY clause */ |
+ Expr *pE /* The specific ORDER BY term */ |
+){ |
+ int i; /* Loop counter */ |
+ ExprList *pEList; /* The columns of the result set */ |
+ NameContext nc; /* Name context for resolving pE */ |
+ sqlite3 *db; /* Database connection */ |
+ int rc; /* Return code from subprocedures */ |
+ u8 savedSuppErr; /* Saved value of db->suppressErr */ |
+ |
+ assert( sqlite3ExprIsInteger(pE, &i)==0 ); |
+ pEList = pSelect->pEList; |
+ |
+ /* Resolve all names in the ORDER BY term expression |
+ */ |
+ memset(&nc, 0, sizeof(nc)); |
+ nc.pParse = pParse; |
+ nc.pSrcList = pSelect->pSrc; |
+ nc.pEList = pEList; |
+ nc.ncFlags = NC_AllowAgg; |
+ nc.nErr = 0; |
+ db = pParse->db; |
+ savedSuppErr = db->suppressErr; |
+ db->suppressErr = 1; |
+ rc = sqlite3ResolveExprNames(&nc, pE); |
+ db->suppressErr = savedSuppErr; |
+ if( rc ) return 0; |
+ |
+ /* Try to match the ORDER BY expression against an expression |
+ ** in the result set. Return an 1-based index of the matching |
+ ** result-set entry. |
+ */ |
+ for(i=0; i<pEList->nExpr; i++){ |
+ if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){ |
+ return i+1; |
+ } |
+ } |
+ |
+ /* If no match, return 0. */ |
+ return 0; |
+} |
+ |
+/* |
+** Generate an ORDER BY or GROUP BY term out-of-range error. |
+*/ |
+static void resolveOutOfRangeError( |
+ Parse *pParse, /* The error context into which to write the error */ |
+ const char *zType, /* "ORDER" or "GROUP" */ |
+ int i, /* The index (1-based) of the term out of range */ |
+ int mx /* Largest permissible value of i */ |
+){ |
+ sqlite3ErrorMsg(pParse, |
+ "%r %s BY term out of range - should be " |
+ "between 1 and %d", i, zType, mx); |
+} |
+ |
+/* |
+** Analyze the ORDER BY clause in a compound SELECT statement. Modify |
+** each term of the ORDER BY clause is a constant integer between 1 |
+** and N where N is the number of columns in the compound SELECT. |
+** |
+** ORDER BY terms that are already an integer between 1 and N are |
+** unmodified. ORDER BY terms that are integers outside the range of |
+** 1 through N generate an error. ORDER BY terms that are expressions |
+** are matched against result set expressions of compound SELECT |
+** beginning with the left-most SELECT and working toward the right. |
+** At the first match, the ORDER BY expression is transformed into |
+** the integer column number. |
+** |
+** Return the number of errors seen. |
+*/ |
+static int resolveCompoundOrderBy( |
+ Parse *pParse, /* Parsing context. Leave error messages here */ |
+ Select *pSelect /* The SELECT statement containing the ORDER BY */ |
+){ |
+ int i; |
+ ExprList *pOrderBy; |
+ ExprList *pEList; |
+ sqlite3 *db; |
+ int moreToDo = 1; |
+ |
+ pOrderBy = pSelect->pOrderBy; |
+ if( pOrderBy==0 ) return 0; |
+ db = pParse->db; |
+#if SQLITE_MAX_COLUMN |
+ if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
+ sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause"); |
+ return 1; |
+ } |
+#endif |
+ for(i=0; i<pOrderBy->nExpr; i++){ |
+ pOrderBy->a[i].done = 0; |
+ } |
+ pSelect->pNext = 0; |
+ while( pSelect->pPrior ){ |
+ pSelect->pPrior->pNext = pSelect; |
+ pSelect = pSelect->pPrior; |
+ } |
+ while( pSelect && moreToDo ){ |
+ struct ExprList_item *pItem; |
+ moreToDo = 0; |
+ pEList = pSelect->pEList; |
+ assert( pEList!=0 ); |
+ for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ |
+ int iCol = -1; |
+ Expr *pE, *pDup; |
+ if( pItem->done ) continue; |
+ pE = sqlite3ExprSkipCollate(pItem->pExpr); |
+ if( sqlite3ExprIsInteger(pE, &iCol) ){ |
+ if( iCol<=0 || iCol>pEList->nExpr ){ |
+ resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr); |
+ return 1; |
+ } |
+ }else{ |
+ iCol = resolveAsName(pParse, pEList, pE); |
+ if( iCol==0 ){ |
+ pDup = sqlite3ExprDup(db, pE, 0); |
+ if( !db->mallocFailed ){ |
+ assert(pDup); |
+ iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup); |
+ } |
+ sqlite3ExprDelete(db, pDup); |
+ } |
+ } |
+ if( iCol>0 ){ |
+ /* Convert the ORDER BY term into an integer column number iCol, |
+ ** taking care to preserve the COLLATE clause if it exists */ |
+ Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); |
+ if( pNew==0 ) return 1; |
+ pNew->flags |= EP_IntValue; |
+ pNew->u.iValue = iCol; |
+ if( pItem->pExpr==pE ){ |
+ pItem->pExpr = pNew; |
+ }else{ |
+ Expr *pParent = pItem->pExpr; |
+ assert( pParent->op==TK_COLLATE ); |
+ while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft; |
+ assert( pParent->pLeft==pE ); |
+ pParent->pLeft = pNew; |
+ } |
+ sqlite3ExprDelete(db, pE); |
+ pItem->u.x.iOrderByCol = (u16)iCol; |
+ pItem->done = 1; |
+ }else{ |
+ moreToDo = 1; |
+ } |
+ } |
+ pSelect = pSelect->pNext; |
+ } |
+ for(i=0; i<pOrderBy->nExpr; i++){ |
+ if( pOrderBy->a[i].done==0 ){ |
+ sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any " |
+ "column in the result set", i+1); |
+ return 1; |
+ } |
+ } |
+ return 0; |
+} |
+ |
+/* |
+** Check every term in the ORDER BY or GROUP BY clause pOrderBy of |
+** the SELECT statement pSelect. If any term is reference to a |
+** result set expression (as determined by the ExprList.a.u.x.iOrderByCol |
+** field) then convert that term into a copy of the corresponding result set |
+** column. |
+** |
+** If any errors are detected, add an error message to pParse and |
+** return non-zero. Return zero if no errors are seen. |
+*/ |
+SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy( |
+ Parse *pParse, /* Parsing context. Leave error messages here */ |
+ Select *pSelect, /* The SELECT statement containing the clause */ |
+ ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */ |
+ const char *zType /* "ORDER" or "GROUP" */ |
+){ |
+ int i; |
+ sqlite3 *db = pParse->db; |
+ ExprList *pEList; |
+ struct ExprList_item *pItem; |
+ |
+ if( pOrderBy==0 || pParse->db->mallocFailed ) return 0; |
+#if SQLITE_MAX_COLUMN |
+ if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
+ sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType); |
+ return 1; |
+ } |
+#endif |
+ pEList = pSelect->pEList; |
+ assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */ |
+ for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ |
+ if( pItem->u.x.iOrderByCol ){ |
+ if( pItem->u.x.iOrderByCol>pEList->nExpr ){ |
+ resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr); |
+ return 1; |
+ } |
+ resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, |
+ zType,0); |
+ } |
+ } |
+ return 0; |
+} |
+ |
+/* |
+** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect. |
+** The Name context of the SELECT statement is pNC. zType is either |
+** "ORDER" or "GROUP" depending on which type of clause pOrderBy is. |
+** |
+** This routine resolves each term of the clause into an expression. |
+** If the order-by term is an integer I between 1 and N (where N is the |
+** number of columns in the result set of the SELECT) then the expression |
+** in the resolution is a copy of the I-th result-set expression. If |
+** the order-by term is an identifier that corresponds to the AS-name of |
+** a result-set expression, then the term resolves to a copy of the |
+** result-set expression. Otherwise, the expression is resolved in |
+** the usual way - using sqlite3ResolveExprNames(). |
+** |
+** This routine returns the number of errors. If errors occur, then |
+** an appropriate error message might be left in pParse. (OOM errors |
+** excepted.) |
+*/ |
+static int resolveOrderGroupBy( |
+ NameContext *pNC, /* The name context of the SELECT statement */ |
+ Select *pSelect, /* The SELECT statement holding pOrderBy */ |
+ ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */ |
+ const char *zType /* Either "ORDER" or "GROUP", as appropriate */ |
+){ |
+ int i, j; /* Loop counters */ |
+ int iCol; /* Column number */ |
+ struct ExprList_item *pItem; /* A term of the ORDER BY clause */ |
+ Parse *pParse; /* Parsing context */ |
+ int nResult; /* Number of terms in the result set */ |
+ |
+ if( pOrderBy==0 ) return 0; |
+ nResult = pSelect->pEList->nExpr; |
+ pParse = pNC->pParse; |
+ for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ |
+ Expr *pE = pItem->pExpr; |
+ Expr *pE2 = sqlite3ExprSkipCollate(pE); |
+ if( zType[0]!='G' ){ |
+ iCol = resolveAsName(pParse, pSelect->pEList, pE2); |
+ if( iCol>0 ){ |
+ /* If an AS-name match is found, mark this ORDER BY column as being |
+ ** a copy of the iCol-th result-set column. The subsequent call to |
+ ** sqlite3ResolveOrderGroupBy() will convert the expression to a |
+ ** copy of the iCol-th result-set expression. */ |
+ pItem->u.x.iOrderByCol = (u16)iCol; |
+ continue; |
+ } |
+ } |
+ if( sqlite3ExprIsInteger(pE2, &iCol) ){ |
+ /* The ORDER BY term is an integer constant. Again, set the column |
+ ** number so that sqlite3ResolveOrderGroupBy() will convert the |
+ ** order-by term to a copy of the result-set expression */ |
+ if( iCol<1 || iCol>0xffff ){ |
+ resolveOutOfRangeError(pParse, zType, i+1, nResult); |
+ return 1; |
+ } |
+ pItem->u.x.iOrderByCol = (u16)iCol; |
+ continue; |
+ } |
+ |
+ /* Otherwise, treat the ORDER BY term as an ordinary expression */ |
+ pItem->u.x.iOrderByCol = 0; |
+ if( sqlite3ResolveExprNames(pNC, pE) ){ |
+ return 1; |
+ } |
+ for(j=0; j<pSelect->pEList->nExpr; j++){ |
+ if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){ |
+ pItem->u.x.iOrderByCol = j+1; |
+ } |
+ } |
+ } |
+ return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType); |
+} |
+ |
+/* |
+** Resolve names in the SELECT statement p and all of its descendants. |
+*/ |
+static int resolveSelectStep(Walker *pWalker, Select *p){ |
+ NameContext *pOuterNC; /* Context that contains this SELECT */ |
+ NameContext sNC; /* Name context of this SELECT */ |
+ int isCompound; /* True if p is a compound select */ |
+ int nCompound; /* Number of compound terms processed so far */ |
+ Parse *pParse; /* Parsing context */ |
+ int i; /* Loop counter */ |
+ ExprList *pGroupBy; /* The GROUP BY clause */ |
+ Select *pLeftmost; /* Left-most of SELECT of a compound */ |
+ sqlite3 *db; /* Database connection */ |
+ |
+ |
+ assert( p!=0 ); |
+ if( p->selFlags & SF_Resolved ){ |
+ return WRC_Prune; |
+ } |
+ pOuterNC = pWalker->u.pNC; |
+ pParse = pWalker->pParse; |
+ db = pParse->db; |
+ |
+ /* Normally sqlite3SelectExpand() will be called first and will have |
+ ** already expanded this SELECT. However, if this is a subquery within |
+ ** an expression, sqlite3ResolveExprNames() will be called without a |
+ ** prior call to sqlite3SelectExpand(). When that happens, let |
+ ** sqlite3SelectPrep() do all of the processing for this SELECT. |
+ ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and |
+ ** this routine in the correct order. |
+ */ |
+ if( (p->selFlags & SF_Expanded)==0 ){ |
+ sqlite3SelectPrep(pParse, p, pOuterNC); |
+ return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune; |
+ } |
+ |
+ isCompound = p->pPrior!=0; |
+ nCompound = 0; |
+ pLeftmost = p; |
+ while( p ){ |
+ assert( (p->selFlags & SF_Expanded)!=0 ); |
+ assert( (p->selFlags & SF_Resolved)==0 ); |
+ p->selFlags |= SF_Resolved; |
+ |
+ /* Resolve the expressions in the LIMIT and OFFSET clauses. These |
+ ** are not allowed to refer to any names, so pass an empty NameContext. |
+ */ |
+ memset(&sNC, 0, sizeof(sNC)); |
+ sNC.pParse = pParse; |
+ if( sqlite3ResolveExprNames(&sNC, p->pLimit) || |
+ sqlite3ResolveExprNames(&sNC, p->pOffset) ){ |
+ return WRC_Abort; |
+ } |
+ |
+ /* If the SF_Converted flags is set, then this Select object was |
+ ** was created by the convertCompoundSelectToSubquery() function. |
+ ** In this case the ORDER BY clause (p->pOrderBy) should be resolved |
+ ** as if it were part of the sub-query, not the parent. This block |
+ ** moves the pOrderBy down to the sub-query. It will be moved back |
+ ** after the names have been resolved. */ |
+ if( p->selFlags & SF_Converted ){ |
+ Select *pSub = p->pSrc->a[0].pSelect; |
+ assert( p->pSrc->nSrc==1 && p->pOrderBy ); |
+ assert( pSub->pPrior && pSub->pOrderBy==0 ); |
+ pSub->pOrderBy = p->pOrderBy; |
+ p->pOrderBy = 0; |
+ } |
+ |
+ /* Recursively resolve names in all subqueries |
+ */ |
+ for(i=0; i<p->pSrc->nSrc; i++){ |
+ struct SrcList_item *pItem = &p->pSrc->a[i]; |
+ if( pItem->pSelect ){ |
+ NameContext *pNC; /* Used to iterate name contexts */ |
+ int nRef = 0; /* Refcount for pOuterNC and outer contexts */ |
+ const char *zSavedContext = pParse->zAuthContext; |
+ |
+ /* Count the total number of references to pOuterNC and all of its |
+ ** parent contexts. After resolving references to expressions in |
+ ** pItem->pSelect, check if this value has changed. If so, then |
+ ** SELECT statement pItem->pSelect must be correlated. Set the |
+ ** pItem->fg.isCorrelated flag if this is the case. */ |
+ for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef; |
+ |
+ if( pItem->zName ) pParse->zAuthContext = pItem->zName; |
+ sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC); |
+ pParse->zAuthContext = zSavedContext; |
+ if( pParse->nErr || db->mallocFailed ) return WRC_Abort; |
+ |
+ for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef; |
+ assert( pItem->fg.isCorrelated==0 && nRef<=0 ); |
+ pItem->fg.isCorrelated = (nRef!=0); |
+ } |
+ } |
+ |
+ /* Set up the local name-context to pass to sqlite3ResolveExprNames() to |
+ ** resolve the result-set expression list. |
+ */ |
+ sNC.ncFlags = NC_AllowAgg; |
+ sNC.pSrcList = p->pSrc; |
+ sNC.pNext = pOuterNC; |
+ |
+ /* Resolve names in the result set. */ |
+ if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort; |
+ |
+ /* If there are no aggregate functions in the result-set, and no GROUP BY |
+ ** expression, do not allow aggregates in any of the other expressions. |
+ */ |
+ assert( (p->selFlags & SF_Aggregate)==0 ); |
+ pGroupBy = p->pGroupBy; |
+ if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){ |
+ assert( NC_MinMaxAgg==SF_MinMaxAgg ); |
+ p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg); |
+ }else{ |
+ sNC.ncFlags &= ~NC_AllowAgg; |
+ } |
+ |
+ /* If a HAVING clause is present, then there must be a GROUP BY clause. |
+ */ |
+ if( p->pHaving && !pGroupBy ){ |
+ sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); |
+ return WRC_Abort; |
+ } |
+ |
+ /* Add the output column list to the name-context before parsing the |
+ ** other expressions in the SELECT statement. This is so that |
+ ** expressions in the WHERE clause (etc.) can refer to expressions by |
+ ** aliases in the result set. |
+ ** |
+ ** Minor point: If this is the case, then the expression will be |
+ ** re-evaluated for each reference to it. |
+ */ |
+ sNC.pEList = p->pEList; |
+ if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort; |
+ if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort; |
+ |
+ /* Resolve names in table-valued-function arguments */ |
+ for(i=0; i<p->pSrc->nSrc; i++){ |
+ struct SrcList_item *pItem = &p->pSrc->a[i]; |
+ if( pItem->fg.isTabFunc |
+ && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg) |
+ ){ |
+ return WRC_Abort; |
+ } |
+ } |
+ |
+ /* The ORDER BY and GROUP BY clauses may not refer to terms in |
+ ** outer queries |
+ */ |
+ sNC.pNext = 0; |
+ sNC.ncFlags |= NC_AllowAgg; |
+ |
+ /* If this is a converted compound query, move the ORDER BY clause from |
+ ** the sub-query back to the parent query. At this point each term |
+ ** within the ORDER BY clause has been transformed to an integer value. |
+ ** These integers will be replaced by copies of the corresponding result |
+ ** set expressions by the call to resolveOrderGroupBy() below. */ |
+ if( p->selFlags & SF_Converted ){ |
+ Select *pSub = p->pSrc->a[0].pSelect; |
+ p->pOrderBy = pSub->pOrderBy; |
+ pSub->pOrderBy = 0; |
+ } |
+ |
+ /* Process the ORDER BY clause for singleton SELECT statements. |
+ ** The ORDER BY clause for compounds SELECT statements is handled |
+ ** below, after all of the result-sets for all of the elements of |
+ ** the compound have been resolved. |
+ ** |
+ ** If there is an ORDER BY clause on a term of a compound-select other |
+ ** than the right-most term, then that is a syntax error. But the error |
+ ** is not detected until much later, and so we need to go ahead and |
+ ** resolve those symbols on the incorrect ORDER BY for consistency. |
+ */ |
+ if( isCompound<=nCompound /* Defer right-most ORDER BY of a compound */ |
+ && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") |
+ ){ |
+ return WRC_Abort; |
+ } |
+ if( db->mallocFailed ){ |
+ return WRC_Abort; |
+ } |
+ |
+ /* Resolve the GROUP BY clause. At the same time, make sure |
+ ** the GROUP BY clause does not contain aggregate functions. |
+ */ |
+ if( pGroupBy ){ |
+ struct ExprList_item *pItem; |
+ |
+ if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){ |
+ return WRC_Abort; |
+ } |
+ for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){ |
+ if( ExprHasProperty(pItem->pExpr, EP_Agg) ){ |
+ sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in " |
+ "the GROUP BY clause"); |
+ return WRC_Abort; |
+ } |
+ } |
+ } |
+ |
+ /* If this is part of a compound SELECT, check that it has the right |
+ ** number of expressions in the select list. */ |
+ if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){ |
+ sqlite3SelectWrongNumTermsError(pParse, p->pNext); |
+ return WRC_Abort; |
+ } |
+ |
+ /* Advance to the next term of the compound |
+ */ |
+ p = p->pPrior; |
+ nCompound++; |
+ } |
+ |
+ /* Resolve the ORDER BY on a compound SELECT after all terms of |
+ ** the compound have been resolved. |
+ */ |
+ if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){ |
+ return WRC_Abort; |
+ } |
+ |
+ return WRC_Prune; |
+} |
+ |
+/* |
+** This routine walks an expression tree and resolves references to |
+** table columns and result-set columns. At the same time, do error |
+** checking on function usage and set a flag if any aggregate functions |
+** are seen. |
+** |
+** To resolve table columns references we look for nodes (or subtrees) of the |
+** form X.Y.Z or Y.Z or just Z where |
+** |
+** X: The name of a database. Ex: "main" or "temp" or |
+** the symbolic name assigned to an ATTACH-ed database. |
+** |
+** Y: The name of a table in a FROM clause. Or in a trigger |
+** one of the special names "old" or "new". |
+** |
+** Z: The name of a column in table Y. |
+** |
+** The node at the root of the subtree is modified as follows: |
+** |
+** Expr.op Changed to TK_COLUMN |
+** Expr.pTab Points to the Table object for X.Y |
+** Expr.iColumn The column index in X.Y. -1 for the rowid. |
+** Expr.iTable The VDBE cursor number for X.Y |
+** |
+** |
+** To resolve result-set references, look for expression nodes of the |
+** form Z (with no X and Y prefix) where the Z matches the right-hand |
+** size of an AS clause in the result-set of a SELECT. The Z expression |
+** is replaced by a copy of the left-hand side of the result-set expression. |
+** Table-name and function resolution occurs on the substituted expression |
+** tree. For example, in: |
+** |
+** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x; |
+** |
+** The "x" term of the order by is replaced by "a+b" to render: |
+** |
+** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b; |
+** |
+** Function calls are checked to make sure that the function is |
+** defined and that the correct number of arguments are specified. |
+** If the function is an aggregate function, then the NC_HasAgg flag is |
+** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION. |
+** If an expression contains aggregate functions then the EP_Agg |
+** property on the expression is set. |
+** |
+** An error message is left in pParse if anything is amiss. The number |
+** if errors is returned. |
+*/ |
+SQLITE_PRIVATE int sqlite3ResolveExprNames( |
+ NameContext *pNC, /* Namespace to resolve expressions in. */ |
+ Expr *pExpr /* The expression to be analyzed. */ |
+){ |
+ u16 savedHasAgg; |
+ Walker w; |
+ |
+ if( pExpr==0 ) return 0; |
+#if SQLITE_MAX_EXPR_DEPTH>0 |
+ { |
+ Parse *pParse = pNC->pParse; |
+ if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){ |
+ return 1; |
+ } |
+ pParse->nHeight += pExpr->nHeight; |
+ } |
+#endif |
+ savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg); |
+ pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg); |
+ w.pParse = pNC->pParse; |
+ w.xExprCallback = resolveExprStep; |
+ w.xSelectCallback = resolveSelectStep; |
+ w.xSelectCallback2 = 0; |
+ w.walkerDepth = 0; |
+ w.eCode = 0; |
+ w.u.pNC = pNC; |
+ sqlite3WalkExpr(&w, pExpr); |
+#if SQLITE_MAX_EXPR_DEPTH>0 |
+ pNC->pParse->nHeight -= pExpr->nHeight; |
+#endif |
+ if( pNC->nErr>0 || w.pParse->nErr>0 ){ |
+ ExprSetProperty(pExpr, EP_Error); |
+ } |
+ if( pNC->ncFlags & NC_HasAgg ){ |
+ ExprSetProperty(pExpr, EP_Agg); |
+ } |
+ pNC->ncFlags |= savedHasAgg; |
+ return ExprHasProperty(pExpr, EP_Error); |
+} |
+ |
+/* |
+** Resolve all names for all expression in an expression list. This is |
+** just like sqlite3ResolveExprNames() except that it works for an expression |
+** list rather than a single expression. |
+*/ |
+SQLITE_PRIVATE int sqlite3ResolveExprListNames( |
+ NameContext *pNC, /* Namespace to resolve expressions in. */ |
+ ExprList *pList /* The expression list to be analyzed. */ |
+){ |
+ int i; |
+ if( pList ){ |
+ for(i=0; i<pList->nExpr; i++){ |
+ if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort; |
+ } |
+ } |
+ return WRC_Continue; |
+} |
+ |
+/* |
+** Resolve all names in all expressions of a SELECT and in all |
+** decendents of the SELECT, including compounds off of p->pPrior, |
+** subqueries in expressions, and subqueries used as FROM clause |
+** terms. |
+** |
+** See sqlite3ResolveExprNames() for a description of the kinds of |
+** transformations that occur. |
+** |
+** All SELECT statements should have been expanded using |
+** sqlite3SelectExpand() prior to invoking this routine. |
+*/ |
+SQLITE_PRIVATE void sqlite3ResolveSelectNames( |
+ Parse *pParse, /* The parser context */ |
+ Select *p, /* The SELECT statement being coded. */ |
+ NameContext *pOuterNC /* Name context for parent SELECT statement */ |
+){ |
+ Walker w; |
+ |
+ assert( p!=0 ); |
+ memset(&w, 0, sizeof(w)); |
+ w.xExprCallback = resolveExprStep; |
+ w.xSelectCallback = resolveSelectStep; |
+ w.pParse = pParse; |
+ w.u.pNC = pOuterNC; |
+ sqlite3WalkSelect(&w, p); |
+} |
+ |
+/* |
+** Resolve names in expressions that can only reference a single table: |
+** |
+** * CHECK constraints |
+** * WHERE clauses on partial indices |
+** |
+** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression |
+** is set to -1 and the Expr.iColumn value is set to the column number. |
+** |
+** Any errors cause an error message to be set in pParse. |
+*/ |
+SQLITE_PRIVATE void sqlite3ResolveSelfReference( |
+ Parse *pParse, /* Parsing context */ |
+ Table *pTab, /* The table being referenced */ |
+ int type, /* NC_IsCheck or NC_PartIdx or NC_IdxExpr */ |
+ Expr *pExpr, /* Expression to resolve. May be NULL. */ |
+ ExprList *pList /* Expression list to resolve. May be NUL. */ |
+){ |
+ SrcList sSrc; /* Fake SrcList for pParse->pNewTable */ |
+ NameContext sNC; /* Name context for pParse->pNewTable */ |
+ |
+ assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr ); |
+ memset(&sNC, 0, sizeof(sNC)); |
+ memset(&sSrc, 0, sizeof(sSrc)); |
+ sSrc.nSrc = 1; |
+ sSrc.a[0].zName = pTab->zName; |
+ sSrc.a[0].pTab = pTab; |
+ sSrc.a[0].iCursor = -1; |
+ sNC.pParse = pParse; |
+ sNC.pSrcList = &sSrc; |
+ sNC.ncFlags = type; |
+ if( sqlite3ResolveExprNames(&sNC, pExpr) ) return; |
+ if( pList ) sqlite3ResolveExprListNames(&sNC, pList); |
+} |
+ |
+/************** End of resolve.c *********************************************/ |
+/************** Begin file expr.c ********************************************/ |
+/* |
+** 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. |
+** |
+************************************************************************* |
+** This file contains routines used for analyzing expressions and |
+** for generating VDBE code that evaluates expressions in SQLite. |
+*/ |
+/* #include "sqliteInt.h" */ |
+ |
+/* Forward declarations */ |
+static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int); |
+static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree); |
+ |
+/* |
+** Return the affinity character for a single column of a table. |
+*/ |
+SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table *pTab, int iCol){ |
+ assert( iCol<pTab->nCol ); |
+ return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER; |
+} |
+ |
+/* |
+** Return the 'affinity' of the expression pExpr if any. |
+** |
+** If pExpr is a column, a reference to a column via an 'AS' alias, |
+** or a sub-select with a column as the return value, then the |
+** affinity of that column is returned. Otherwise, 0x00 is returned, |
+** indicating no affinity for the expression. |
+** |
+** i.e. the WHERE clause expressions in the following statements all |
+** have an affinity: |
+** |
+** CREATE TABLE t1(a); |
+** SELECT * FROM t1 WHERE a; |
+** SELECT a AS b FROM t1 WHERE b; |
+** SELECT * FROM t1 WHERE (select a from t1); |
+*/ |
+SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){ |
+ int op; |
+ pExpr = sqlite3ExprSkipCollate(pExpr); |
+ if( pExpr->flags & EP_Generic ) return 0; |
+ op = pExpr->op; |
+ if( op==TK_SELECT ){ |
+ assert( pExpr->flags&EP_xIsSelect ); |
+ return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr); |
+ } |
+ if( op==TK_REGISTER ) op = pExpr->op2; |
+#ifndef SQLITE_OMIT_CAST |
+ if( op==TK_CAST ){ |
+ assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
+ return sqlite3AffinityType(pExpr->u.zToken, 0); |
+ } |
+#endif |
+ if( op==TK_AGG_COLUMN || op==TK_COLUMN ){ |
+ return sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn); |
+ } |
+ if( op==TK_SELECT_COLUMN ){ |
+ assert( pExpr->pLeft->flags&EP_xIsSelect ); |
+ return sqlite3ExprAffinity( |
+ pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr |
+ ); |
+ } |
+ return pExpr->affinity; |
+} |
+ |
+/* |
+** Set the collating sequence for expression pExpr to be the collating |
+** sequence named by pToken. Return a pointer to a new Expr node that |
+** implements the COLLATE operator. |
+** |
+** If a memory allocation error occurs, that fact is recorded in pParse->db |
+** and the pExpr parameter is returned unchanged. |
+*/ |
+SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken( |
+ Parse *pParse, /* Parsing context */ |
+ Expr *pExpr, /* Add the "COLLATE" clause to this expression */ |
+ const Token *pCollName, /* Name of collating sequence */ |
+ int dequote /* True to dequote pCollName */ |
+){ |
+ if( pCollName->n>0 ){ |
+ Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote); |
+ if( pNew ){ |
+ pNew->pLeft = pExpr; |
+ pNew->flags |= EP_Collate|EP_Skip; |
+ pExpr = pNew; |
+ } |
+ } |
+ return pExpr; |
+} |
+SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){ |
+ Token s; |
+ assert( zC!=0 ); |
+ sqlite3TokenInit(&s, (char*)zC); |
+ return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0); |
+} |
+ |
+/* |
+** Skip over any TK_COLLATE operators and any unlikely() |
+** or likelihood() function at the root of an expression. |
+*/ |
+SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){ |
+ while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){ |
+ if( ExprHasProperty(pExpr, EP_Unlikely) ){ |
+ assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
+ assert( pExpr->x.pList->nExpr>0 ); |
+ assert( pExpr->op==TK_FUNCTION ); |
+ pExpr = pExpr->x.pList->a[0].pExpr; |
+ }else{ |
+ assert( pExpr->op==TK_COLLATE ); |
+ pExpr = pExpr->pLeft; |
+ } |
+ } |
+ return pExpr; |
+} |
+ |
+/* |
+** Return the collation sequence for the expression pExpr. If |
+** there is no defined collating sequence, return NULL. |
+** |
+** The collating sequence might be determined by a COLLATE operator |
+** or by the presence of a column with a defined collating sequence. |
+** COLLATE operators take first precedence. Left operands take |
+** precedence over right operands. |
+*/ |
+SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ |
+ sqlite3 *db = pParse->db; |
+ CollSeq *pColl = 0; |
+ Expr *p = pExpr; |
+ while( p ){ |
+ int op = p->op; |
+ if( p->flags & EP_Generic ) break; |
+ if( op==TK_CAST || op==TK_UPLUS ){ |
+ p = p->pLeft; |
+ continue; |
+ } |
+ if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){ |
+ pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken); |
+ break; |
+ } |
+ if( (op==TK_AGG_COLUMN || op==TK_COLUMN |
+ || op==TK_REGISTER || op==TK_TRIGGER) |
+ && p->pTab!=0 |
+ ){ |
+ /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally |
+ ** a TK_COLUMN but was previously evaluated and cached in a register */ |
+ int j = p->iColumn; |
+ if( j>=0 ){ |
+ const char *zColl = p->pTab->aCol[j].zColl; |
+ pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); |
+ } |
+ break; |
+ } |
+ if( p->flags & EP_Collate ){ |
+ if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){ |
+ p = p->pLeft; |
+ }else{ |
+ Expr *pNext = p->pRight; |
+ /* The Expr.x union is never used at the same time as Expr.pRight */ |
+ assert( p->x.pList==0 || p->pRight==0 ); |
+ /* p->flags holds EP_Collate and p->pLeft->flags does not. And |
+ ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at |
+ ** least one EP_Collate. Thus the following two ALWAYS. */ |
+ if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){ |
+ int i; |
+ for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){ |
+ if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){ |
+ pNext = p->x.pList->a[i].pExpr; |
+ break; |
+ } |
+ } |
+ } |
+ p = pNext; |
+ } |
+ }else{ |
+ break; |
+ } |
+ } |
+ if( sqlite3CheckCollSeq(pParse, pColl) ){ |
+ pColl = 0; |
+ } |
+ return pColl; |
+} |
+ |
+/* |
+** pExpr is an operand of a comparison operator. aff2 is the |
+** type affinity of the other operand. This routine returns the |
+** type affinity that should be used for the comparison operator. |
+*/ |
+SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){ |
+ char aff1 = sqlite3ExprAffinity(pExpr); |
+ if( aff1 && aff2 ){ |
+ /* Both sides of the comparison are columns. If one has numeric |
+ ** affinity, use that. Otherwise use no affinity. |
+ */ |
+ if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ |
+ return SQLITE_AFF_NUMERIC; |
+ }else{ |
+ return SQLITE_AFF_BLOB; |
+ } |
+ }else if( !aff1 && !aff2 ){ |
+ /* Neither side of the comparison is a column. Compare the |
+ ** results directly. |
+ */ |
+ return SQLITE_AFF_BLOB; |
+ }else{ |
+ /* One side is a column, the other is not. Use the columns affinity. */ |
+ assert( aff1==0 || aff2==0 ); |
+ return (aff1 + aff2); |
+ } |
+} |
+ |
+/* |
+** pExpr is a comparison operator. Return the type affinity that should |
+** be applied to both operands prior to doing the comparison. |
+*/ |
+static char comparisonAffinity(Expr *pExpr){ |
+ char aff; |
+ assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || |
+ pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || |
+ pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT ); |
+ assert( pExpr->pLeft ); |
+ aff = sqlite3ExprAffinity(pExpr->pLeft); |
+ if( pExpr->pRight ){ |
+ aff = sqlite3CompareAffinity(pExpr->pRight, aff); |
+ }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
+ aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff); |
+ }else if( aff==0 ){ |
+ aff = SQLITE_AFF_BLOB; |
+ } |
+ return aff; |
+} |
+ |
+/* |
+** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. |
+** idx_affinity is the affinity of an indexed column. Return true |
+** if the index with affinity idx_affinity may be used to implement |
+** the comparison in pExpr. |
+*/ |
+SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ |
+ char aff = comparisonAffinity(pExpr); |
+ switch( aff ){ |
+ case SQLITE_AFF_BLOB: |
+ return 1; |
+ case SQLITE_AFF_TEXT: |
+ return idx_affinity==SQLITE_AFF_TEXT; |
+ default: |
+ return sqlite3IsNumericAffinity(idx_affinity); |
+ } |
+} |
+ |
+/* |
+** Return the P5 value that should be used for a binary comparison |
+** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. |
+*/ |
+static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ |
+ u8 aff = (char)sqlite3ExprAffinity(pExpr2); |
+ aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull; |
+ return aff; |
+} |
+ |
+/* |
+** Return a pointer to the collation sequence that should be used by |
+** a binary comparison operator comparing pLeft and pRight. |
+** |
+** If the left hand expression has a collating sequence type, then it is |
+** used. Otherwise the collation sequence for the right hand expression |
+** is used, or the default (BINARY) if neither expression has a collating |
+** type. |
+** |
+** Argument pRight (but not pLeft) may be a null pointer. In this case, |
+** it is not considered. |
+*/ |
+SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq( |
+ Parse *pParse, |
+ Expr *pLeft, |
+ Expr *pRight |
+){ |
+ CollSeq *pColl; |
+ assert( pLeft ); |
+ if( pLeft->flags & EP_Collate ){ |
+ pColl = sqlite3ExprCollSeq(pParse, pLeft); |
+ }else if( pRight && (pRight->flags & EP_Collate)!=0 ){ |
+ pColl = sqlite3ExprCollSeq(pParse, pRight); |
+ }else{ |
+ pColl = sqlite3ExprCollSeq(pParse, pLeft); |
+ if( !pColl ){ |
+ pColl = sqlite3ExprCollSeq(pParse, pRight); |
+ } |
+ } |
+ return pColl; |
+} |
+ |
+/* |
+** Generate code for a comparison operator. |
+*/ |
+static int codeCompare( |
+ Parse *pParse, /* The parsing (and code generating) context */ |
+ Expr *pLeft, /* The left operand */ |
+ Expr *pRight, /* The right operand */ |
+ int opcode, /* The comparison opcode */ |
+ int in1, int in2, /* Register holding operands */ |
+ int dest, /* Jump here if true. */ |
+ int jumpIfNull /* If true, jump if either operand is NULL */ |
+){ |
+ int p5; |
+ int addr; |
+ CollSeq *p4; |
+ |
+ p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); |
+ p5 = binaryCompareP5(pLeft, pRight, jumpIfNull); |
+ addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1, |
+ (void*)p4, P4_COLLSEQ); |
+ sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5); |
+ return addr; |
+} |
+ |
+/* |
+** Return true if expression pExpr is a vector, or false otherwise. |
+** |
+** A vector is defined as any expression that results in two or more |
+** columns of result. Every TK_VECTOR node is an vector because the |
+** parser will not generate a TK_VECTOR with fewer than two entries. |
+** But a TK_SELECT might be either a vector or a scalar. It is only |
+** considered a vector if it has two or more result columns. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr){ |
+ return sqlite3ExprVectorSize(pExpr)>1; |
+} |
+ |
+/* |
+** If the expression passed as the only argument is of type TK_VECTOR |
+** return the number of expressions in the vector. Or, if the expression |
+** is a sub-select, return the number of columns in the sub-select. For |
+** any other type of expression, return 1. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr){ |
+ u8 op = pExpr->op; |
+ if( op==TK_REGISTER ) op = pExpr->op2; |
+ if( op==TK_VECTOR ){ |
+ return pExpr->x.pList->nExpr; |
+ }else if( op==TK_SELECT ){ |
+ return pExpr->x.pSelect->pEList->nExpr; |
+ }else{ |
+ return 1; |
+ } |
+} |
+ |
+#ifndef SQLITE_OMIT_SUBQUERY |
+/* |
+** Return a pointer to a subexpression of pVector that is the i-th |
+** column of the vector (numbered starting with 0). The caller must |
+** ensure that i is within range. |
+** |
+** If pVector is really a scalar (and "scalar" here includes subqueries |
+** that return a single column!) then return pVector unmodified. |
+** |
+** pVector retains ownership of the returned subexpression. |
+** |
+** If the vector is a (SELECT ...) then the expression returned is |
+** just the expression for the i-th term of the result set, and may |
+** not be ready for evaluation because the table cursor has not yet |
+** been positioned. |
+*/ |
+SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){ |
+ assert( i<sqlite3ExprVectorSize(pVector) ); |
+ if( sqlite3ExprIsVector(pVector) ){ |
+ assert( pVector->op2==0 || pVector->op==TK_REGISTER ); |
+ if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){ |
+ return pVector->x.pSelect->pEList->a[i].pExpr; |
+ }else{ |
+ return pVector->x.pList->a[i].pExpr; |
+ } |
+ } |
+ return pVector; |
+} |
+#endif /* !defined(SQLITE_OMIT_SUBQUERY) */ |
+ |
+#ifndef SQLITE_OMIT_SUBQUERY |
+/* |
+** Compute and return a new Expr object which when passed to |
+** sqlite3ExprCode() will generate all necessary code to compute |
+** the iField-th column of the vector expression pVector. |
+** |
+** It is ok for pVector to be a scalar (as long as iField==0). |
+** In that case, this routine works like sqlite3ExprDup(). |
+** |
+** The caller owns the returned Expr object and is responsible for |
+** ensuring that the returned value eventually gets freed. |
+** |
+** The caller retains ownership of pVector. If pVector is a TK_SELECT, |
+** then the returned object will reference pVector and so pVector must remain |
+** valid for the life of the returned object. If pVector is a TK_VECTOR |
+** or a scalar expression, then it can be deleted as soon as this routine |
+** returns. |
+** |
+** A trick to cause a TK_SELECT pVector to be deleted together with |
+** the returned Expr object is to attach the pVector to the pRight field |
+** of the returned TK_SELECT_COLUMN Expr object. |
+*/ |
+SQLITE_PRIVATE Expr *sqlite3ExprForVectorField( |
+ Parse *pParse, /* Parsing context */ |
+ Expr *pVector, /* The vector. List of expressions or a sub-SELECT */ |
+ int iField /* Which column of the vector to return */ |
+){ |
+ Expr *pRet; |
+ if( pVector->op==TK_SELECT ){ |
+ assert( pVector->flags & EP_xIsSelect ); |
+ /* The TK_SELECT_COLUMN Expr node: |
+ ** |
+ ** pLeft: pVector containing TK_SELECT. Not deleted. |
+ ** pRight: not used. But recursively deleted. |
+ ** iColumn: Index of a column in pVector |
+ ** iTable: 0 or the number of columns on the LHS of an assignment |
+ ** pLeft->iTable: First in an array of register holding result, or 0 |
+ ** if the result is not yet computed. |
+ ** |
+ ** sqlite3ExprDelete() specifically skips the recursive delete of |
+ ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector |
+ ** can be attached to pRight to cause this node to take ownership of |
+ ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes |
+ ** with the same pLeft pointer to the pVector, but only one of them |
+ ** will own the pVector. |
+ */ |
+ pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0); |
+ if( pRet ){ |
+ pRet->iColumn = iField; |
+ pRet->pLeft = pVector; |
+ } |
+ assert( pRet==0 || pRet->iTable==0 ); |
+ }else{ |
+ if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr; |
+ pRet = sqlite3ExprDup(pParse->db, pVector, 0); |
+ } |
+ return pRet; |
+} |
+#endif /* !define(SQLITE_OMIT_SUBQUERY) */ |
+ |
+/* |
+** If expression pExpr is of type TK_SELECT, generate code to evaluate |
+** it. Return the register in which the result is stored (or, if the |
+** sub-select returns more than one column, the first in an array |
+** of registers in which the result is stored). |
+** |
+** If pExpr is not a TK_SELECT expression, return 0. |
+*/ |
+static int exprCodeSubselect(Parse *pParse, Expr *pExpr){ |
+ int reg = 0; |
+#ifndef SQLITE_OMIT_SUBQUERY |
+ if( pExpr->op==TK_SELECT ){ |
+ reg = sqlite3CodeSubselect(pParse, pExpr, 0, 0); |
+ } |
+#endif |
+ return reg; |
+} |
+ |
+/* |
+** Argument pVector points to a vector expression - either a TK_VECTOR |
+** or TK_SELECT that returns more than one column. This function returns |
+** the register number of a register that contains the value of |
+** element iField of the vector. |
+** |
+** If pVector is a TK_SELECT expression, then code for it must have |
+** already been generated using the exprCodeSubselect() routine. In this |
+** case parameter regSelect should be the first in an array of registers |
+** containing the results of the sub-select. |
+** |
+** If pVector is of type TK_VECTOR, then code for the requested field |
+** is generated. In this case (*pRegFree) may be set to the number of |
+** a temporary register to be freed by the caller before returning. |
+** |
+** Before returning, output parameter (*ppExpr) is set to point to the |
+** Expr object corresponding to element iElem of the vector. |
+*/ |
+static int exprVectorRegister( |
+ Parse *pParse, /* Parse context */ |
+ Expr *pVector, /* Vector to extract element from */ |
+ int iField, /* Field to extract from pVector */ |
+ int regSelect, /* First in array of registers */ |
+ Expr **ppExpr, /* OUT: Expression element */ |
+ int *pRegFree /* OUT: Temp register to free */ |
+){ |
+ u8 op = pVector->op; |
+ assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT ); |
+ if( op==TK_REGISTER ){ |
+ *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField); |
+ return pVector->iTable+iField; |
+ } |
+ if( op==TK_SELECT ){ |
+ *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr; |
+ return regSelect+iField; |
+ } |
+ *ppExpr = pVector->x.pList->a[iField].pExpr; |
+ return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree); |
+} |
+ |
+/* |
+** Expression pExpr is a comparison between two vector values. Compute |
+** the result of the comparison (1, 0, or NULL) and write that |
+** result into register dest. |
+** |
+** The caller must satisfy the following preconditions: |
+** |
+** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ |
+** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ |
+** otherwise: op==pExpr->op and p5==0 |
+*/ |
+static void codeVectorCompare( |
+ Parse *pParse, /* Code generator context */ |
+ Expr *pExpr, /* The comparison operation */ |
+ int dest, /* Write results into this register */ |
+ u8 op, /* Comparison operator */ |
+ u8 p5 /* SQLITE_NULLEQ or zero */ |
+){ |
+ Vdbe *v = pParse->pVdbe; |
+ Expr *pLeft = pExpr->pLeft; |
+ Expr *pRight = pExpr->pRight; |
+ int nLeft = sqlite3ExprVectorSize(pLeft); |
+ int i; |
+ int regLeft = 0; |
+ int regRight = 0; |
+ u8 opx = op; |
+ int addrDone = sqlite3VdbeMakeLabel(v); |
+ |
+ if( nLeft!=sqlite3ExprVectorSize(pRight) ){ |
+ sqlite3ErrorMsg(pParse, "row value misused"); |
+ return; |
+ } |
+ assert( pExpr->op==TK_EQ || pExpr->op==TK_NE |
+ || pExpr->op==TK_IS || pExpr->op==TK_ISNOT |
+ || pExpr->op==TK_LT || pExpr->op==TK_GT |
+ || pExpr->op==TK_LE || pExpr->op==TK_GE |
+ ); |
+ assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ) |
+ || (pExpr->op==TK_ISNOT && op==TK_NE) ); |
+ assert( p5==0 || pExpr->op!=op ); |
+ assert( p5==SQLITE_NULLEQ || pExpr->op==op ); |
+ |
+ p5 |= SQLITE_STOREP2; |
+ if( opx==TK_LE ) opx = TK_LT; |
+ if( opx==TK_GE ) opx = TK_GT; |
+ |
+ regLeft = exprCodeSubselect(pParse, pLeft); |
+ regRight = exprCodeSubselect(pParse, pRight); |
+ |
+ for(i=0; 1 /*Loop exits by "break"*/; i++){ |
+ int regFree1 = 0, regFree2 = 0; |
+ Expr *pL, *pR; |
+ int r1, r2; |
+ assert( i>=0 && i<nLeft ); |
+ if( i>0 ) sqlite3ExprCachePush(pParse); |
+ r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, ®Free1); |
+ r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, ®Free2); |
+ codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5); |
+ testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); |
+ testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); |
+ testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); |
+ testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); |
+ testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); |
+ testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); |
+ sqlite3ReleaseTempReg(pParse, regFree1); |
+ sqlite3ReleaseTempReg(pParse, regFree2); |
+ if( i>0 ) sqlite3ExprCachePop(pParse); |
+ if( i==nLeft-1 ){ |
+ break; |
+ } |
+ if( opx==TK_EQ ){ |
+ sqlite3VdbeAddOp2(v, OP_IfNot, dest, addrDone); VdbeCoverage(v); |
+ p5 |= SQLITE_KEEPNULL; |
+ }else if( opx==TK_NE ){ |
+ sqlite3VdbeAddOp2(v, OP_If, dest, addrDone); VdbeCoverage(v); |
+ p5 |= SQLITE_KEEPNULL; |
+ }else{ |
+ assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE ); |
+ sqlite3VdbeAddOp2(v, OP_ElseNotEq, 0, addrDone); |
+ VdbeCoverageIf(v, op==TK_LT); |
+ VdbeCoverageIf(v, op==TK_GT); |
+ VdbeCoverageIf(v, op==TK_LE); |
+ VdbeCoverageIf(v, op==TK_GE); |
+ if( i==nLeft-2 ) opx = op; |
+ } |
+ } |
+ sqlite3VdbeResolveLabel(v, addrDone); |
+} |
+ |
+#if SQLITE_MAX_EXPR_DEPTH>0 |
+/* |
+** Check that argument nHeight is less than or equal to the maximum |
+** expression depth allowed. If it is not, leave an error message in |
+** pParse. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){ |
+ int rc = SQLITE_OK; |
+ int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; |
+ if( nHeight>mxHeight ){ |
+ sqlite3ErrorMsg(pParse, |
+ "Expression tree is too large (maximum depth %d)", mxHeight |
+ ); |
+ rc = SQLITE_ERROR; |
+ } |
+ return rc; |
+} |
+ |
+/* The following three functions, heightOfExpr(), heightOfExprList() |
+** and heightOfSelect(), are used to determine the maximum height |
+** of any expression tree referenced by the structure passed as the |
+** first argument. |
+** |
+** If this maximum height is greater than the current value pointed |
+** to by pnHeight, the second parameter, then set *pnHeight to that |
+** value. |
+*/ |
+static void heightOfExpr(Expr *p, int *pnHeight){ |
+ if( p ){ |
+ if( p->nHeight>*pnHeight ){ |
+ *pnHeight = p->nHeight; |
+ } |
+ } |
+} |
+static void heightOfExprList(ExprList *p, int *pnHeight){ |
+ if( p ){ |
+ int i; |
+ for(i=0; i<p->nExpr; i++){ |
+ heightOfExpr(p->a[i].pExpr, pnHeight); |
+ } |
+ } |
+} |
+static void heightOfSelect(Select *p, int *pnHeight){ |
+ if( p ){ |
+ heightOfExpr(p->pWhere, pnHeight); |
+ heightOfExpr(p->pHaving, pnHeight); |
+ heightOfExpr(p->pLimit, pnHeight); |
+ heightOfExpr(p->pOffset, pnHeight); |
+ heightOfExprList(p->pEList, pnHeight); |
+ heightOfExprList(p->pGroupBy, pnHeight); |
+ heightOfExprList(p->pOrderBy, pnHeight); |
+ heightOfSelect(p->pPrior, pnHeight); |
+ } |
+} |
+ |
+/* |
+** Set the Expr.nHeight variable in the structure passed as an |
+** argument. An expression with no children, Expr.pList or |
+** Expr.pSelect member has a height of 1. Any other expression |
+** has a height equal to the maximum height of any other |
+** referenced Expr plus one. |
+** |
+** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags, |
+** if appropriate. |
+*/ |
+static void exprSetHeight(Expr *p){ |
+ int nHeight = 0; |
+ heightOfExpr(p->pLeft, &nHeight); |
+ heightOfExpr(p->pRight, &nHeight); |
+ if( ExprHasProperty(p, EP_xIsSelect) ){ |
+ heightOfSelect(p->x.pSelect, &nHeight); |
+ }else if( p->x.pList ){ |
+ heightOfExprList(p->x.pList, &nHeight); |
+ p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); |
+ } |
+ p->nHeight = nHeight + 1; |
+} |
+ |
+/* |
+** Set the Expr.nHeight variable using the exprSetHeight() function. If |
+** the height is greater than the maximum allowed expression depth, |
+** leave an error in pParse. |
+** |
+** Also propagate all EP_Propagate flags from the Expr.x.pList into |
+** Expr.flags. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ |
+ if( pParse->nErr ) return; |
+ exprSetHeight(p); |
+ sqlite3ExprCheckHeight(pParse, p->nHeight); |
+} |
+ |
+/* |
+** Return the maximum height of any expression tree referenced |
+** by the select statement passed as an argument. |
+*/ |
+SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){ |
+ int nHeight = 0; |
+ heightOfSelect(p, &nHeight); |
+ return nHeight; |
+} |
+#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */ |
+/* |
+** Propagate all EP_Propagate flags from the Expr.x.pList into |
+** Expr.flags. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ |
+ if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){ |
+ p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); |
+ } |
+} |
+#define exprSetHeight(y) |
+#endif /* SQLITE_MAX_EXPR_DEPTH>0 */ |
+ |
+/* |
+** This routine is the core allocator for Expr nodes. |
+** |
+** Construct a new expression node and return a pointer to it. Memory |
+** for this node and for the pToken argument is a single allocation |
+** obtained from sqlite3DbMalloc(). The calling function |
+** is responsible for making sure the node eventually gets freed. |
+** |
+** If dequote is true, then the token (if it exists) is dequoted. |
+** If dequote is false, no dequoting is performed. The deQuote |
+** parameter is ignored if pToken is NULL or if the token does not |
+** appear to be quoted. If the quotes were of the form "..." (double-quotes) |
+** then the EP_DblQuoted flag is set on the expression node. |
+** |
+** Special case: If op==TK_INTEGER and pToken points to a string that |
+** can be translated into a 32-bit integer, then the token is not |
+** stored in u.zToken. Instead, the integer values is written |
+** into u.iValue and the EP_IntValue flag is set. No extra storage |
+** is allocated to hold the integer text and the dequote flag is ignored. |
+*/ |
+SQLITE_PRIVATE Expr *sqlite3ExprAlloc( |
+ sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */ |
+ int op, /* Expression opcode */ |
+ const Token *pToken, /* Token argument. Might be NULL */ |
+ int dequote /* True to dequote */ |
+){ |
+ Expr *pNew; |
+ int nExtra = 0; |
+ int iValue = 0; |
+ |
+ assert( db!=0 ); |
+ if( pToken ){ |
+ if( op!=TK_INTEGER || pToken->z==0 |
+ || sqlite3GetInt32(pToken->z, &iValue)==0 ){ |
+ nExtra = pToken->n+1; |
+ assert( iValue>=0 ); |
+ } |
+ } |
+ pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra); |
+ if( pNew ){ |
+ memset(pNew, 0, sizeof(Expr)); |
+ pNew->op = (u8)op; |
+ pNew->iAgg = -1; |
+ if( pToken ){ |
+ if( nExtra==0 ){ |
+ pNew->flags |= EP_IntValue; |
+ pNew->u.iValue = iValue; |
+ }else{ |
+ pNew->u.zToken = (char*)&pNew[1]; |
+ assert( pToken->z!=0 || pToken->n==0 ); |
+ if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); |
+ pNew->u.zToken[pToken->n] = 0; |
+ if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){ |
+ if( pNew->u.zToken[0]=='"' ) pNew->flags |= EP_DblQuoted; |
+ sqlite3Dequote(pNew->u.zToken); |
+ } |
+ } |
+ } |
+#if SQLITE_MAX_EXPR_DEPTH>0 |
+ pNew->nHeight = 1; |
+#endif |
+ } |
+ return pNew; |
+} |
+ |
+/* |
+** Allocate a new expression node from a zero-terminated token that has |
+** already been dequoted. |
+*/ |
+SQLITE_PRIVATE Expr *sqlite3Expr( |
+ sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ |
+ int op, /* Expression opcode */ |
+ const char *zToken /* Token argument. Might be NULL */ |
+){ |
+ Token x; |
+ x.z = zToken; |
+ x.n = zToken ? sqlite3Strlen30(zToken) : 0; |
+ return sqlite3ExprAlloc(db, op, &x, 0); |
+} |
+ |
+/* |
+** Attach subtrees pLeft and pRight to the Expr node pRoot. |
+** |
+** If pRoot==NULL that means that a memory allocation error has occurred. |
+** In that case, delete the subtrees pLeft and pRight. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprAttachSubtrees( |
+ sqlite3 *db, |
+ Expr *pRoot, |
+ Expr *pLeft, |
+ Expr *pRight |
+){ |
+ if( pRoot==0 ){ |
+ assert( db->mallocFailed ); |
+ sqlite3ExprDelete(db, pLeft); |
+ sqlite3ExprDelete(db, pRight); |
+ }else{ |
+ if( pRight ){ |
+ pRoot->pRight = pRight; |
+ pRoot->flags |= EP_Propagate & pRight->flags; |
+ } |
+ if( pLeft ){ |
+ pRoot->pLeft = pLeft; |
+ pRoot->flags |= EP_Propagate & pLeft->flags; |
+ } |
+ exprSetHeight(pRoot); |
+ } |
+} |
+ |
+/* |
+** Allocate an Expr node which joins as many as two subtrees. |
+** |
+** One or both of the subtrees can be NULL. Return a pointer to the new |
+** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, |
+** free the subtrees and return NULL. |
+*/ |
+SQLITE_PRIVATE Expr *sqlite3PExpr( |
+ Parse *pParse, /* Parsing context */ |
+ int op, /* Expression opcode */ |
+ Expr *pLeft, /* Left operand */ |
+ Expr *pRight /* Right operand */ |
+){ |
+ Expr *p; |
+ if( op==TK_AND && pParse->nErr==0 ){ |
+ /* Take advantage of short-circuit false optimization for AND */ |
+ p = sqlite3ExprAnd(pParse->db, pLeft, pRight); |
+ }else{ |
+ p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)); |
+ if( p ){ |
+ memset(p, 0, sizeof(Expr)); |
+ p->op = op & TKFLG_MASK; |
+ p->iAgg = -1; |
+ } |
+ sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); |
+ } |
+ if( p ) { |
+ sqlite3ExprCheckHeight(pParse, p->nHeight); |
+ } |
+ return p; |
+} |
+ |
+/* |
+** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due |
+** do a memory allocation failure) then delete the pSelect object. |
+*/ |
+SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){ |
+ if( pExpr ){ |
+ pExpr->x.pSelect = pSelect; |
+ ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery); |
+ sqlite3ExprSetHeightAndFlags(pParse, pExpr); |
+ }else{ |
+ assert( pParse->db->mallocFailed ); |
+ sqlite3SelectDelete(pParse->db, pSelect); |
+ } |
+} |
+ |
+ |
+/* |
+** If the expression is always either TRUE or FALSE (respectively), |
+** then return 1. If one cannot determine the truth value of the |
+** expression at compile-time return 0. |
+** |
+** This is an optimization. If is OK to return 0 here even if |
+** the expression really is always false or false (a false negative). |
+** But it is a bug to return 1 if the expression might have different |
+** boolean values in different circumstances (a false positive.) |
+** |
+** Note that if the expression is part of conditional for a |
+** LEFT JOIN, then we cannot determine at compile-time whether or not |
+** is it true or false, so always return 0. |
+*/ |
+static int exprAlwaysTrue(Expr *p){ |
+ int v = 0; |
+ if( ExprHasProperty(p, EP_FromJoin) ) return 0; |
+ if( !sqlite3ExprIsInteger(p, &v) ) return 0; |
+ return v!=0; |
+} |
+static int exprAlwaysFalse(Expr *p){ |
+ int v = 0; |
+ if( ExprHasProperty(p, EP_FromJoin) ) return 0; |
+ if( !sqlite3ExprIsInteger(p, &v) ) return 0; |
+ return v==0; |
+} |
+ |
+/* |
+** Join two expressions using an AND operator. If either expression is |
+** NULL, then just return the other expression. |
+** |
+** If one side or the other of the AND is known to be false, then instead |
+** of returning an AND expression, just return a constant expression with |
+** a value of false. |
+*/ |
+SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ |
+ if( pLeft==0 ){ |
+ return pRight; |
+ }else if( pRight==0 ){ |
+ return pLeft; |
+ }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){ |
+ sqlite3ExprDelete(db, pLeft); |
+ sqlite3ExprDelete(db, pRight); |
+ return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0); |
+ }else{ |
+ Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0); |
+ sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight); |
+ return pNew; |
+ } |
+} |
+ |
+/* |
+** Construct a new expression node for a function with multiple |
+** arguments. |
+*/ |
+SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ |
+ Expr *pNew; |
+ sqlite3 *db = pParse->db; |
+ assert( pToken ); |
+ pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1); |
+ if( pNew==0 ){ |
+ sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */ |
+ return 0; |
+ } |
+ pNew->x.pList = pList; |
+ assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
+ sqlite3ExprSetHeightAndFlags(pParse, pNew); |
+ return pNew; |
+} |
+ |
+/* |
+** Assign a variable number to an expression that encodes a wildcard |
+** in the original SQL statement. |
+** |
+** Wildcards consisting of a single "?" are assigned the next sequential |
+** variable number. |
+** |
+** Wildcards of the form "?nnn" are assigned the number "nnn". We make |
+** sure "nnn" is not too big to avoid a denial of service attack when |
+** the SQL statement comes from an external source. |
+** |
+** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number |
+** as the previous instance of the same wildcard. Or if this is the first |
+** instance of the wildcard, the next sequential variable number is |
+** assigned. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){ |
+ sqlite3 *db = pParse->db; |
+ const char *z; |
+ ynVar x; |
+ |
+ if( pExpr==0 ) return; |
+ assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); |
+ z = pExpr->u.zToken; |
+ assert( z!=0 ); |
+ assert( z[0]!=0 ); |
+ assert( n==sqlite3Strlen30(z) ); |
+ if( z[1]==0 ){ |
+ /* Wildcard of the form "?". Assign the next variable number */ |
+ assert( z[0]=='?' ); |
+ x = (ynVar)(++pParse->nVar); |
+ }else{ |
+ int doAdd = 0; |
+ if( z[0]=='?' ){ |
+ /* Wildcard of the form "?nnn". Convert "nnn" to an integer and |
+ ** use it as the variable number */ |
+ i64 i; |
+ int bOk; |
+ if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/ |
+ i = z[1]-'0'; /* The common case of ?N for a single digit N */ |
+ bOk = 1; |
+ }else{ |
+ bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); |
+ } |
+ testcase( i==0 ); |
+ testcase( i==1 ); |
+ testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); |
+ testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); |
+ if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
+ sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", |
+ db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); |
+ return; |
+ } |
+ x = (ynVar)i; |
+ if( x>pParse->nVar ){ |
+ pParse->nVar = (int)x; |
+ doAdd = 1; |
+ }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){ |
+ doAdd = 1; |
+ } |
+ }else{ |
+ /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable |
+ ** number as the prior appearance of the same name, or if the name |
+ ** has never appeared before, reuse the same variable number |
+ */ |
+ x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n); |
+ if( x==0 ){ |
+ x = (ynVar)(++pParse->nVar); |
+ doAdd = 1; |
+ } |
+ } |
+ if( doAdd ){ |
+ pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x); |
+ } |
+ } |
+ pExpr->iColumn = x; |
+ if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
+ sqlite3ErrorMsg(pParse, "too many SQL variables"); |
+ } |
+} |
+ |
+/* |
+** Recursively delete an expression tree. |
+*/ |
+static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ |
+ assert( p!=0 ); |
+ /* Sanity check: Assert that the IntValue is non-negative if it exists */ |
+ assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 ); |
+#ifdef SQLITE_DEBUG |
+ if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){ |
+ assert( p->pLeft==0 ); |
+ assert( p->pRight==0 ); |
+ assert( p->x.pSelect==0 ); |
+ } |
+#endif |
+ if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){ |
+ /* The Expr.x union is never used at the same time as Expr.pRight */ |
+ assert( p->x.pList==0 || p->pRight==0 ); |
+ if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft); |
+ sqlite3ExprDelete(db, p->pRight); |
+ if( ExprHasProperty(p, EP_xIsSelect) ){ |
+ sqlite3SelectDelete(db, p->x.pSelect); |
+ }else{ |
+ sqlite3ExprListDelete(db, p->x.pList); |
+ } |
+ } |
+ if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken); |
+ if( !ExprHasProperty(p, EP_Static) ){ |
+ sqlite3DbFree(db, p); |
+ } |
+} |
+SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){ |
+ if( p ) sqlite3ExprDeleteNN(db, p); |
+} |
+ |
+/* |
+** Return the number of bytes allocated for the expression structure |
+** passed as the first argument. This is always one of EXPR_FULLSIZE, |
+** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE. |
+*/ |
+static int exprStructSize(Expr *p){ |
+ if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE; |
+ if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE; |
+ return EXPR_FULLSIZE; |
+} |
+ |
+/* |
+** The dupedExpr*Size() routines each return the number of bytes required |
+** to store a copy of an expression or expression tree. They differ in |
+** how much of the tree is measured. |
+** |
+** dupedExprStructSize() Size of only the Expr structure |
+** dupedExprNodeSize() Size of Expr + space for token |
+** dupedExprSize() Expr + token + subtree components |
+** |
+*************************************************************************** |
+** |
+** The dupedExprStructSize() function returns two values OR-ed together: |
+** (1) the space required for a copy of the Expr structure only and |
+** (2) the EP_xxx flags that indicate what the structure size should be. |
+** The return values is always one of: |
+** |
+** EXPR_FULLSIZE |
+** EXPR_REDUCEDSIZE | EP_Reduced |
+** EXPR_TOKENONLYSIZE | EP_TokenOnly |
+** |
+** The size of the structure can be found by masking the return value |
+** of this routine with 0xfff. The flags can be found by masking the |
+** return value with EP_Reduced|EP_TokenOnly. |
+** |
+** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size |
+** (unreduced) Expr objects as they or originally constructed by the parser. |
+** During expression analysis, extra information is computed and moved into |
+** later parts of teh Expr object and that extra information might get chopped |
+** off if the expression is reduced. Note also that it does not work to |
+** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal |
+** to reduce a pristine expression tree from the parser. The implementation |
+** of dupedExprStructSize() contain multiple assert() statements that attempt |
+** to enforce this constraint. |
+*/ |
+static int dupedExprStructSize(Expr *p, int flags){ |
+ int nSize; |
+ assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ |
+ assert( EXPR_FULLSIZE<=0xfff ); |
+ assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 ); |
+ if( 0==flags || p->op==TK_SELECT_COLUMN ){ |
+ nSize = EXPR_FULLSIZE; |
+ }else{ |
+ assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); |
+ assert( !ExprHasProperty(p, EP_FromJoin) ); |
+ assert( !ExprHasProperty(p, EP_MemToken) ); |
+ assert( !ExprHasProperty(p, EP_NoReduce) ); |
+ if( p->pLeft || p->x.pList ){ |
+ nSize = EXPR_REDUCEDSIZE | EP_Reduced; |
+ }else{ |
+ assert( p->pRight==0 ); |
+ nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly; |
+ } |
+ } |
+ return nSize; |
+} |
+ |
+/* |
+** This function returns the space in bytes required to store the copy |
+** of the Expr structure and a copy of the Expr.u.zToken string (if that |
+** string is defined.) |
+*/ |
+static int dupedExprNodeSize(Expr *p, int flags){ |
+ int nByte = dupedExprStructSize(p, flags) & 0xfff; |
+ if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ |
+ nByte += sqlite3Strlen30(p->u.zToken)+1; |
+ } |
+ return ROUND8(nByte); |
+} |
+ |
+/* |
+** Return the number of bytes required to create a duplicate of the |
+** expression passed as the first argument. The second argument is a |
+** mask containing EXPRDUP_XXX flags. |
+** |
+** The value returned includes space to create a copy of the Expr struct |
+** itself and the buffer referred to by Expr.u.zToken, if any. |
+** |
+** If the EXPRDUP_REDUCE flag is set, then the return value includes |
+** space to duplicate all Expr nodes in the tree formed by Expr.pLeft |
+** and Expr.pRight variables (but not for any structures pointed to or |
+** descended from the Expr.x.pList or Expr.x.pSelect variables). |
+*/ |
+static int dupedExprSize(Expr *p, int flags){ |
+ int nByte = 0; |
+ if( p ){ |
+ nByte = dupedExprNodeSize(p, flags); |
+ if( flags&EXPRDUP_REDUCE ){ |
+ nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags); |
+ } |
+ } |
+ return nByte; |
+} |
+ |
+/* |
+** This function is similar to sqlite3ExprDup(), except that if pzBuffer |
+** is not NULL then *pzBuffer is assumed to point to a buffer large enough |
+** to store the copy of expression p, the copies of p->u.zToken |
+** (if applicable), and the copies of the p->pLeft and p->pRight expressions, |
+** if any. Before returning, *pzBuffer is set to the first byte past the |
+** portion of the buffer copied into by this function. |
+*/ |
+static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){ |
+ Expr *pNew; /* Value to return */ |
+ u8 *zAlloc; /* Memory space from which to build Expr object */ |
+ u32 staticFlag; /* EP_Static if space not obtained from malloc */ |
+ |
+ assert( db!=0 ); |
+ assert( p ); |
+ assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE ); |
+ assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE ); |
+ |
+ /* Figure out where to write the new Expr structure. */ |
+ if( pzBuffer ){ |
+ zAlloc = *pzBuffer; |
+ staticFlag = EP_Static; |
+ }else{ |
+ zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags)); |
+ staticFlag = 0; |
+ } |
+ pNew = (Expr *)zAlloc; |
+ |
+ if( pNew ){ |
+ /* Set nNewSize to the size allocated for the structure pointed to |
+ ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or |
+ ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed |
+ ** by the copy of the p->u.zToken string (if any). |
+ */ |
+ const unsigned nStructSize = dupedExprStructSize(p, dupFlags); |
+ const int nNewSize = nStructSize & 0xfff; |
+ int nToken; |
+ if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ |
+ nToken = sqlite3Strlen30(p->u.zToken) + 1; |
+ }else{ |
+ nToken = 0; |
+ } |
+ if( dupFlags ){ |
+ assert( ExprHasProperty(p, EP_Reduced)==0 ); |
+ memcpy(zAlloc, p, nNewSize); |
+ }else{ |
+ u32 nSize = (u32)exprStructSize(p); |
+ memcpy(zAlloc, p, nSize); |
+ if( nSize<EXPR_FULLSIZE ){ |
+ memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); |
+ } |
+ } |
+ |
+ /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ |
+ pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); |
+ pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); |
+ pNew->flags |= staticFlag; |
+ |
+ /* Copy the p->u.zToken string, if any. */ |
+ if( nToken ){ |
+ char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize]; |
+ memcpy(zToken, p->u.zToken, nToken); |
+ } |
+ |
+ if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){ |
+ /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ |
+ if( ExprHasProperty(p, EP_xIsSelect) ){ |
+ pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags); |
+ }else{ |
+ pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags); |
+ } |
+ } |
+ |
+ /* Fill in pNew->pLeft and pNew->pRight. */ |
+ if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){ |
+ zAlloc += dupedExprNodeSize(p, dupFlags); |
+ if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){ |
+ pNew->pLeft = p->pLeft ? |
+ exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0; |
+ pNew->pRight = p->pRight ? |
+ exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0; |
+ } |
+ if( pzBuffer ){ |
+ *pzBuffer = zAlloc; |
+ } |
+ }else{ |
+ if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){ |
+ if( pNew->op==TK_SELECT_COLUMN ){ |
+ pNew->pLeft = p->pLeft; |
+ assert( p->iColumn==0 || p->pRight==0 ); |
+ assert( p->pRight==0 || p->pRight==p->pLeft ); |
+ }else{ |
+ pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); |
+ } |
+ pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); |
+ } |
+ } |
+ } |
+ return pNew; |
+} |
+ |
+/* |
+** Create and return a deep copy of the object passed as the second |
+** argument. If an OOM condition is encountered, NULL is returned |
+** and the db->mallocFailed flag set. |
+*/ |
+#ifndef SQLITE_OMIT_CTE |
+static With *withDup(sqlite3 *db, With *p){ |
+ With *pRet = 0; |
+ if( p ){ |
+ int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1); |
+ pRet = sqlite3DbMallocZero(db, nByte); |
+ if( pRet ){ |
+ int i; |
+ pRet->nCte = p->nCte; |
+ for(i=0; i<p->nCte; i++){ |
+ pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0); |
+ pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0); |
+ pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName); |
+ } |
+ } |
+ } |
+ return pRet; |
+} |
+#else |
+# define withDup(x,y) 0 |
+#endif |
+ |
+/* |
+** The following group of routines make deep copies of expressions, |
+** expression lists, ID lists, and select statements. The copies can |
+** be deleted (by being passed to their respective ...Delete() routines) |
+** without effecting the originals. |
+** |
+** The expression list, ID, and source lists return by sqlite3ExprListDup(), |
+** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded |
+** by subsequent calls to sqlite*ListAppend() routines. |
+** |
+** Any tables that the SrcList might point to are not duplicated. |
+** |
+** The flags parameter contains a combination of the EXPRDUP_XXX flags. |
+** If the EXPRDUP_REDUCE flag is set, then the structure returned is a |
+** truncated version of the usual Expr structure that will be stored as |
+** part of the in-memory representation of the database schema. |
+*/ |
+SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ |
+ assert( flags==0 || flags==EXPRDUP_REDUCE ); |
+ return p ? exprDup(db, p, flags, 0) : 0; |
+} |
+SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ |
+ ExprList *pNew; |
+ struct ExprList_item *pItem, *pOldItem; |
+ int i; |
+ Expr *pPriorSelectCol = 0; |
+ assert( db!=0 ); |
+ if( p==0 ) return 0; |
+ pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); |
+ if( pNew==0 ) return 0; |
+ pNew->nExpr = i = p->nExpr; |
+ if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){} |
+ pNew->a = pItem = sqlite3DbMallocRawNN(db, i*sizeof(p->a[0]) ); |
+ if( pItem==0 ){ |
+ sqlite3DbFree(db, pNew); |
+ return 0; |
+ } |
+ pOldItem = p->a; |
+ for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ |
+ Expr *pOldExpr = pOldItem->pExpr; |
+ Expr *pNewExpr; |
+ pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags); |
+ if( pOldExpr |
+ && pOldExpr->op==TK_SELECT_COLUMN |
+ && (pNewExpr = pItem->pExpr)!=0 |
+ ){ |
+ assert( pNewExpr->iColumn==0 || i>0 ); |
+ if( pNewExpr->iColumn==0 ){ |
+ assert( pOldExpr->pLeft==pOldExpr->pRight ); |
+ pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight; |
+ }else{ |
+ assert( i>0 ); |
+ assert( pItem[-1].pExpr!=0 ); |
+ assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 ); |
+ assert( pPriorSelectCol==pItem[-1].pExpr->pLeft ); |
+ pNewExpr->pLeft = pPriorSelectCol; |
+ } |
+ } |
+ pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
+ pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); |
+ pItem->sortOrder = pOldItem->sortOrder; |
+ pItem->done = 0; |
+ pItem->bSpanIsTab = pOldItem->bSpanIsTab; |
+ pItem->u = pOldItem->u; |
+ } |
+ return pNew; |
+} |
+ |
+/* |
+** If cursors, triggers, views and subqueries are all omitted from |
+** the build, then none of the following routines, except for |
+** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes |
+** called with a NULL argument. |
+*/ |
+#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ |
+ || !defined(SQLITE_OMIT_SUBQUERY) |
+SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){ |
+ SrcList *pNew; |
+ int i; |
+ int nByte; |
+ assert( db!=0 ); |
+ if( p==0 ) return 0; |
+ nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); |
+ pNew = sqlite3DbMallocRawNN(db, nByte ); |
+ if( pNew==0 ) return 0; |
+ pNew->nSrc = pNew->nAlloc = p->nSrc; |
+ for(i=0; i<p->nSrc; i++){ |
+ struct SrcList_item *pNewItem = &pNew->a[i]; |
+ struct SrcList_item *pOldItem = &p->a[i]; |
+ Table *pTab; |
+ pNewItem->pSchema = pOldItem->pSchema; |
+ pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); |
+ pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
+ pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); |
+ pNewItem->fg = pOldItem->fg; |
+ pNewItem->iCursor = pOldItem->iCursor; |
+ pNewItem->addrFillSub = pOldItem->addrFillSub; |
+ pNewItem->regReturn = pOldItem->regReturn; |
+ if( pNewItem->fg.isIndexedBy ){ |
+ pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy); |
+ } |
+ pNewItem->pIBIndex = pOldItem->pIBIndex; |
+ if( pNewItem->fg.isTabFunc ){ |
+ pNewItem->u1.pFuncArg = |
+ sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags); |
+ } |
+ pTab = pNewItem->pTab = pOldItem->pTab; |
+ if( pTab ){ |
+ pTab->nTabRef++; |
+ } |
+ pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); |
+ pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags); |
+ pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); |
+ pNewItem->colUsed = pOldItem->colUsed; |
+ } |
+ return pNew; |
+} |
+SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ |
+ IdList *pNew; |
+ int i; |
+ assert( db!=0 ); |
+ if( p==0 ) return 0; |
+ pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); |
+ if( pNew==0 ) return 0; |
+ pNew->nId = p->nId; |
+ pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) ); |
+ if( pNew->a==0 ){ |
+ sqlite3DbFree(db, pNew); |
+ return 0; |
+ } |
+ /* Note that because the size of the allocation for p->a[] is not |
+ ** necessarily a power of two, sqlite3IdListAppend() may not be called |
+ ** on the duplicate created by this function. */ |
+ for(i=0; i<p->nId; i++){ |
+ struct IdList_item *pNewItem = &pNew->a[i]; |
+ struct IdList_item *pOldItem = &p->a[i]; |
+ pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
+ pNewItem->idx = pOldItem->idx; |
+ } |
+ return pNew; |
+} |
+SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){ |
+ Select *pRet = 0; |
+ Select *pNext = 0; |
+ Select **pp = &pRet; |
+ Select *p; |
+ |
+ assert( db!=0 ); |
+ for(p=pDup; p; p=p->pPrior){ |
+ Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) ); |
+ if( pNew==0 ) break; |
+ pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); |
+ pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); |
+ pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); |
+ pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); |
+ pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); |
+ pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); |
+ pNew->op = p->op; |
+ pNew->pNext = pNext; |
+ pNew->pPrior = 0; |
+ pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); |
+ pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags); |
+ pNew->iLimit = 0; |
+ pNew->iOffset = 0; |
+ pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; |
+ pNew->addrOpenEphm[0] = -1; |
+ pNew->addrOpenEphm[1] = -1; |
+ pNew->nSelectRow = p->nSelectRow; |
+ pNew->pWith = withDup(db, p->pWith); |
+ sqlite3SelectSetName(pNew, p->zSelName); |
+ *pp = pNew; |
+ pp = &pNew->pPrior; |
+ pNext = pNew; |
+ } |
+ |
+ return pRet; |
+} |
+#else |
+SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ |
+ assert( p==0 ); |
+ return 0; |
+} |
+#endif |
+ |
+ |
+/* |
+** Add a new element to the end of an expression list. If pList is |
+** initially NULL, then create a new expression list. |
+** |
+** If a memory allocation error occurs, the entire list is freed and |
+** NULL is returned. If non-NULL is returned, then it is guaranteed |
+** that the new entry was successfully appended. |
+*/ |
+SQLITE_PRIVATE ExprList *sqlite3ExprListAppend( |
+ Parse *pParse, /* Parsing context */ |
+ ExprList *pList, /* List to which to append. Might be NULL */ |
+ Expr *pExpr /* Expression to be appended. Might be NULL */ |
+){ |
+ sqlite3 *db = pParse->db; |
+ assert( db!=0 ); |
+ if( pList==0 ){ |
+ pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) ); |
+ if( pList==0 ){ |
+ goto no_mem; |
+ } |
+ pList->nExpr = 0; |
+ pList->a = sqlite3DbMallocRawNN(db, sizeof(pList->a[0])); |
+ if( pList->a==0 ) goto no_mem; |
+ }else if( (pList->nExpr & (pList->nExpr-1))==0 ){ |
+ struct ExprList_item *a; |
+ assert( pList->nExpr>0 ); |
+ a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0])); |
+ if( a==0 ){ |
+ goto no_mem; |
+ } |
+ pList->a = a; |
+ } |
+ assert( pList->a!=0 ); |
+ if( 1 ){ |
+ struct ExprList_item *pItem = &pList->a[pList->nExpr++]; |
+ memset(pItem, 0, sizeof(*pItem)); |
+ pItem->pExpr = pExpr; |
+ } |
+ return pList; |
+ |
+no_mem: |
+ /* Avoid leaking memory if malloc has failed. */ |
+ sqlite3ExprDelete(db, pExpr); |
+ sqlite3ExprListDelete(db, pList); |
+ return 0; |
+} |
+ |
+/* |
+** pColumns and pExpr form a vector assignment which is part of the SET |
+** clause of an UPDATE statement. Like this: |
+** |
+** (a,b,c) = (expr1,expr2,expr3) |
+** Or: (a,b,c) = (SELECT x,y,z FROM ....) |
+** |
+** For each term of the vector assignment, append new entries to the |
+** expression list pList. In the case of a subquery on the RHS, append |
+** TK_SELECT_COLUMN expressions. |
+*/ |
+SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector( |
+ Parse *pParse, /* Parsing context */ |
+ ExprList *pList, /* List to which to append. Might be NULL */ |
+ IdList *pColumns, /* List of names of LHS of the assignment */ |
+ Expr *pExpr /* Vector expression to be appended. Might be NULL */ |
+){ |
+ sqlite3 *db = pParse->db; |
+ int n; |
+ int i; |
+ int iFirst = pList ? pList->nExpr : 0; |
+ /* pColumns can only be NULL due to an OOM but an OOM will cause an |
+ ** exit prior to this routine being invoked */ |
+ if( NEVER(pColumns==0) ) goto vector_append_error; |
+ if( pExpr==0 ) goto vector_append_error; |
+ |
+ /* If the RHS is a vector, then we can immediately check to see that |
+ ** the size of the RHS and LHS match. But if the RHS is a SELECT, |
+ ** wildcards ("*") in the result set of the SELECT must be expanded before |
+ ** we can do the size check, so defer the size check until code generation. |
+ */ |
+ if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){ |
+ sqlite3ErrorMsg(pParse, "%d columns assigned %d values", |
+ pColumns->nId, n); |
+ goto vector_append_error; |
+ } |
+ |
+ for(i=0; i<pColumns->nId; i++){ |
+ Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i); |
+ pList = sqlite3ExprListAppend(pParse, pList, pSubExpr); |
+ if( pList ){ |
+ assert( pList->nExpr==iFirst+i+1 ); |
+ pList->a[pList->nExpr-1].zName = pColumns->a[i].zName; |
+ pColumns->a[i].zName = 0; |
+ } |
+ } |
+ |
+ if( pExpr->op==TK_SELECT ){ |
+ if( pList && pList->a[iFirst].pExpr ){ |
+ Expr *pFirst = pList->a[iFirst].pExpr; |
+ assert( pFirst->op==TK_SELECT_COLUMN ); |
+ |
+ /* Store the SELECT statement in pRight so it will be deleted when |
+ ** sqlite3ExprListDelete() is called */ |
+ pFirst->pRight = pExpr; |
+ pExpr = 0; |
+ |
+ /* Remember the size of the LHS in iTable so that we can check that |
+ ** the RHS and LHS sizes match during code generation. */ |
+ pFirst->iTable = pColumns->nId; |
+ } |
+ } |
+ |
+vector_append_error: |
+ sqlite3ExprDelete(db, pExpr); |
+ sqlite3IdListDelete(db, pColumns); |
+ return pList; |
+} |
+ |
+/* |
+** Set the sort order for the last element on the given ExprList. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){ |
+ if( p==0 ) return; |
+ assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 ); |
+ assert( p->nExpr>0 ); |
+ if( iSortOrder<0 ){ |
+ assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC ); |
+ return; |
+ } |
+ p->a[p->nExpr-1].sortOrder = (u8)iSortOrder; |
+} |
+ |
+/* |
+** Set the ExprList.a[].zName element of the most recently added item |
+** on the expression list. |
+** |
+** pList might be NULL following an OOM error. But pName should never be |
+** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag |
+** is set. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprListSetName( |
+ Parse *pParse, /* Parsing context */ |
+ ExprList *pList, /* List to which to add the span. */ |
+ Token *pName, /* Name to be added */ |
+ int dequote /* True to cause the name to be dequoted */ |
+){ |
+ assert( pList!=0 || pParse->db->mallocFailed!=0 ); |
+ if( pList ){ |
+ struct ExprList_item *pItem; |
+ assert( pList->nExpr>0 ); |
+ pItem = &pList->a[pList->nExpr-1]; |
+ assert( pItem->zName==0 ); |
+ pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n); |
+ if( dequote ) sqlite3Dequote(pItem->zName); |
+ } |
+} |
+ |
+/* |
+** Set the ExprList.a[].zSpan element of the most recently added item |
+** on the expression list. |
+** |
+** pList might be NULL following an OOM error. But pSpan should never be |
+** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag |
+** is set. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprListSetSpan( |
+ Parse *pParse, /* Parsing context */ |
+ ExprList *pList, /* List to which to add the span. */ |
+ ExprSpan *pSpan /* The span to be added */ |
+){ |
+ sqlite3 *db = pParse->db; |
+ assert( pList!=0 || db->mallocFailed!=0 ); |
+ if( pList ){ |
+ struct ExprList_item *pItem = &pList->a[pList->nExpr-1]; |
+ assert( pList->nExpr>0 ); |
+ assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr ); |
+ sqlite3DbFree(db, pItem->zSpan); |
+ pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart, |
+ (int)(pSpan->zEnd - pSpan->zStart)); |
+ } |
+} |
+ |
+/* |
+** If the expression list pEList contains more than iLimit elements, |
+** leave an error message in pParse. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprListCheckLength( |
+ Parse *pParse, |
+ ExprList *pEList, |
+ const char *zObject |
+){ |
+ int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; |
+ testcase( pEList && pEList->nExpr==mx ); |
+ testcase( pEList && pEList->nExpr==mx+1 ); |
+ if( pEList && pEList->nExpr>mx ){ |
+ sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); |
+ } |
+} |
+ |
+/* |
+** Delete an entire expression list. |
+*/ |
+static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){ |
+ int i; |
+ struct ExprList_item *pItem; |
+ assert( pList->a!=0 || pList->nExpr==0 ); |
+ for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ |
+ sqlite3ExprDelete(db, pItem->pExpr); |
+ sqlite3DbFree(db, pItem->zName); |
+ sqlite3DbFree(db, pItem->zSpan); |
+ } |
+ sqlite3DbFree(db, pList->a); |
+ sqlite3DbFree(db, pList); |
+} |
+SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ |
+ if( pList ) exprListDeleteNN(db, pList); |
+} |
+ |
+/* |
+** Return the bitwise-OR of all Expr.flags fields in the given |
+** ExprList. |
+*/ |
+SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){ |
+ int i; |
+ u32 m = 0; |
+ if( pList ){ |
+ for(i=0; i<pList->nExpr; i++){ |
+ Expr *pExpr = pList->a[i].pExpr; |
+ assert( pExpr!=0 ); |
+ m |= pExpr->flags; |
+ } |
+ } |
+ return m; |
+} |
+ |
+/* |
+** These routines are Walker callbacks used to check expressions to |
+** see if they are "constant" for some definition of constant. The |
+** Walker.eCode value determines the type of "constant" we are looking |
+** for. |
+** |
+** These callback routines are used to implement the following: |
+** |
+** sqlite3ExprIsConstant() pWalker->eCode==1 |
+** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 |
+** sqlite3ExprIsTableConstant() pWalker->eCode==3 |
+** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 |
+** |
+** In all cases, the callbacks set Walker.eCode=0 and abort if the expression |
+** is found to not be a constant. |
+** |
+** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions |
+** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing |
+** an existing schema and 4 when processing a new statement. A bound |
+** parameter raises an error for new statements, but is silently converted |
+** to NULL for existing schemas. This allows sqlite_master tables that |
+** contain a bound parameter because they were generated by older versions |
+** of SQLite to be parsed by newer versions of SQLite without raising a |
+** malformed schema error. |
+*/ |
+static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ |
+ |
+ /* If pWalker->eCode is 2 then any term of the expression that comes from |
+ ** the ON or USING clauses of a left join disqualifies the expression |
+ ** from being considered constant. */ |
+ if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ |
+ pWalker->eCode = 0; |
+ return WRC_Abort; |
+ } |
+ |
+ switch( pExpr->op ){ |
+ /* Consider functions to be constant if all their arguments are constant |
+ ** and either pWalker->eCode==4 or 5 or the function has the |
+ ** SQLITE_FUNC_CONST flag. */ |
+ case TK_FUNCTION: |
+ if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){ |
+ return WRC_Continue; |
+ }else{ |
+ pWalker->eCode = 0; |
+ return WRC_Abort; |
+ } |
+ case TK_ID: |
+ case TK_COLUMN: |
+ case TK_AGG_FUNCTION: |
+ case TK_AGG_COLUMN: |
+ testcase( pExpr->op==TK_ID ); |
+ testcase( pExpr->op==TK_COLUMN ); |
+ testcase( pExpr->op==TK_AGG_FUNCTION ); |
+ testcase( pExpr->op==TK_AGG_COLUMN ); |
+ if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ |
+ return WRC_Continue; |
+ }else{ |
+ pWalker->eCode = 0; |
+ return WRC_Abort; |
+ } |
+ case TK_VARIABLE: |
+ if( pWalker->eCode==5 ){ |
+ /* Silently convert bound parameters that appear inside of CREATE |
+ ** statements into a NULL when parsing the CREATE statement text out |
+ ** of the sqlite_master table */ |
+ pExpr->op = TK_NULL; |
+ }else if( pWalker->eCode==4 ){ |
+ /* A bound parameter in a CREATE statement that originates from |
+ ** sqlite3_prepare() causes an error */ |
+ pWalker->eCode = 0; |
+ return WRC_Abort; |
+ } |
+ /* Fall through */ |
+ default: |
+ testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ |
+ testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */ |
+ return WRC_Continue; |
+ } |
+} |
+static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ |
+ UNUSED_PARAMETER(NotUsed); |
+ pWalker->eCode = 0; |
+ return WRC_Abort; |
+} |
+static int exprIsConst(Expr *p, int initFlag, int iCur){ |
+ Walker w; |
+ memset(&w, 0, sizeof(w)); |
+ w.eCode = initFlag; |
+ w.xExprCallback = exprNodeIsConstant; |
+ w.xSelectCallback = selectNodeIsConstant; |
+ w.u.iCur = iCur; |
+ sqlite3WalkExpr(&w, p); |
+ return w.eCode; |
+} |
+ |
+/* |
+** Walk an expression tree. Return non-zero if the expression is constant |
+** and 0 if it involves variables or function calls. |
+** |
+** For the purposes of this function, a double-quoted string (ex: "abc") |
+** is considered a variable but a single-quoted string (ex: 'abc') is |
+** a constant. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){ |
+ return exprIsConst(p, 1, 0); |
+} |
+ |
+/* |
+** Walk an expression tree. Return non-zero if the expression is constant |
+** that does no originate from the ON or USING clauses of a join. |
+** Return 0 if it involves variables or function calls or terms from |
+** an ON or USING clause. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){ |
+ return exprIsConst(p, 2, 0); |
+} |
+ |
+/* |
+** Walk an expression tree. Return non-zero if the expression is constant |
+** for any single row of the table with cursor iCur. In other words, the |
+** expression must not refer to any non-deterministic function nor any |
+** table other than iCur. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){ |
+ return exprIsConst(p, 3, iCur); |
+} |
+ |
+/* |
+** Walk an expression tree. Return non-zero if the expression is constant |
+** or a function call with constant arguments. Return and 0 if there |
+** are any variables. |
+** |
+** For the purposes of this function, a double-quoted string (ex: "abc") |
+** is considered a variable but a single-quoted string (ex: 'abc') is |
+** a constant. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ |
+ assert( isInit==0 || isInit==1 ); |
+ return exprIsConst(p, 4+isInit, 0); |
+} |
+ |
+#ifdef SQLITE_ENABLE_CURSOR_HINTS |
+/* |
+** Walk an expression tree. Return 1 if the expression contains a |
+** subquery of some kind. Return 0 if there are no subqueries. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr *p){ |
+ Walker w; |
+ memset(&w, 0, sizeof(w)); |
+ w.eCode = 1; |
+ w.xExprCallback = sqlite3ExprWalkNoop; |
+ w.xSelectCallback = selectNodeIsConstant; |
+ sqlite3WalkExpr(&w, p); |
+ return w.eCode==0; |
+} |
+#endif |
+ |
+/* |
+** If the expression p codes a constant integer that is small enough |
+** to fit in a 32-bit integer, return 1 and put the value of the integer |
+** in *pValue. If the expression is not an integer or if it is too big |
+** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){ |
+ int rc = 0; |
+ |
+ /* If an expression is an integer literal that fits in a signed 32-bit |
+ ** integer, then the EP_IntValue flag will have already been set */ |
+ assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0 |
+ || sqlite3GetInt32(p->u.zToken, &rc)==0 ); |
+ |
+ if( p->flags & EP_IntValue ){ |
+ *pValue = p->u.iValue; |
+ return 1; |
+ } |
+ switch( p->op ){ |
+ case TK_UPLUS: { |
+ rc = sqlite3ExprIsInteger(p->pLeft, pValue); |
+ break; |
+ } |
+ case TK_UMINUS: { |
+ int v; |
+ if( sqlite3ExprIsInteger(p->pLeft, &v) ){ |
+ assert( v!=(-2147483647-1) ); |
+ *pValue = -v; |
+ rc = 1; |
+ } |
+ break; |
+ } |
+ default: break; |
+ } |
+ return rc; |
+} |
+ |
+/* |
+** Return FALSE if there is no chance that the expression can be NULL. |
+** |
+** If the expression might be NULL or if the expression is too complex |
+** to tell return TRUE. |
+** |
+** This routine is used as an optimization, to skip OP_IsNull opcodes |
+** when we know that a value cannot be NULL. Hence, a false positive |
+** (returning TRUE when in fact the expression can never be NULL) might |
+** be a small performance hit but is otherwise harmless. On the other |
+** hand, a false negative (returning FALSE when the result could be NULL) |
+** will likely result in an incorrect answer. So when in doubt, return |
+** TRUE. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){ |
+ u8 op; |
+ while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } |
+ op = p->op; |
+ if( op==TK_REGISTER ) op = p->op2; |
+ switch( op ){ |
+ case TK_INTEGER: |
+ case TK_STRING: |
+ case TK_FLOAT: |
+ case TK_BLOB: |
+ return 0; |
+ case TK_COLUMN: |
+ assert( p->pTab!=0 ); |
+ return ExprHasProperty(p, EP_CanBeNull) || |
+ (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0); |
+ default: |
+ return 1; |
+ } |
+} |
+ |
+/* |
+** Return TRUE if the given expression is a constant which would be |
+** unchanged by OP_Affinity with the affinity given in the second |
+** argument. |
+** |
+** This routine is used to determine if the OP_Affinity operation |
+** can be omitted. When in doubt return FALSE. A false negative |
+** is harmless. A false positive, however, can result in the wrong |
+** answer. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ |
+ u8 op; |
+ if( aff==SQLITE_AFF_BLOB ) return 1; |
+ while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } |
+ op = p->op; |
+ if( op==TK_REGISTER ) op = p->op2; |
+ switch( op ){ |
+ case TK_INTEGER: { |
+ return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC; |
+ } |
+ case TK_FLOAT: { |
+ return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC; |
+ } |
+ case TK_STRING: { |
+ return aff==SQLITE_AFF_TEXT; |
+ } |
+ case TK_BLOB: { |
+ return 1; |
+ } |
+ case TK_COLUMN: { |
+ assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */ |
+ return p->iColumn<0 |
+ && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC); |
+ } |
+ default: { |
+ return 0; |
+ } |
+ } |
+} |
+ |
+/* |
+** Return TRUE if the given string is a row-id column name. |
+*/ |
+SQLITE_PRIVATE int sqlite3IsRowid(const char *z){ |
+ if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; |
+ if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; |
+ if( sqlite3StrICmp(z, "OID")==0 ) return 1; |
+ return 0; |
+} |
+ |
+/* |
+** pX is the RHS of an IN operator. If pX is a SELECT statement |
+** that can be simplified to a direct table access, then return |
+** a pointer to the SELECT statement. If pX is not a SELECT statement, |
+** or if the SELECT statement needs to be manifested into a transient |
+** table, then return NULL. |
+*/ |
+#ifndef SQLITE_OMIT_SUBQUERY |
+static Select *isCandidateForInOpt(Expr *pX){ |
+ Select *p; |
+ SrcList *pSrc; |
+ ExprList *pEList; |
+ Table *pTab; |
+ int i; |
+ if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */ |
+ if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */ |
+ p = pX->x.pSelect; |
+ if( p->pPrior ) return 0; /* Not a compound SELECT */ |
+ if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ |
+ testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); |
+ testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); |
+ return 0; /* No DISTINCT keyword and no aggregate functions */ |
+ } |
+ assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */ |
+ if( p->pLimit ) return 0; /* Has no LIMIT clause */ |
+ assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */ |
+ if( p->pWhere ) return 0; /* Has no WHERE clause */ |
+ pSrc = p->pSrc; |
+ assert( pSrc!=0 ); |
+ if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */ |
+ if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */ |
+ pTab = pSrc->a[0].pTab; |
+ assert( pTab!=0 ); |
+ assert( pTab->pSelect==0 ); /* FROM clause is not a view */ |
+ if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ |
+ pEList = p->pEList; |
+ assert( pEList!=0 ); |
+ /* All SELECT results must be columns. */ |
+ for(i=0; i<pEList->nExpr; i++){ |
+ Expr *pRes = pEList->a[i].pExpr; |
+ if( pRes->op!=TK_COLUMN ) return 0; |
+ assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */ |
+ } |
+ return p; |
+} |
+#endif /* SQLITE_OMIT_SUBQUERY */ |
+ |
+#ifndef SQLITE_OMIT_SUBQUERY |
+/* |
+** Generate code that checks the left-most column of index table iCur to see if |
+** it contains any NULL entries. Cause the register at regHasNull to be set |
+** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull |
+** to be set to NULL if iCur contains one or more NULL values. |
+*/ |
+static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){ |
+ int addr1; |
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull); |
+ addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); |
+ sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull); |
+ sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); |
+ VdbeComment((v, "first_entry_in(%d)", iCur)); |
+ sqlite3VdbeJumpHere(v, addr1); |
+} |
+#endif |
+ |
+ |
+#ifndef SQLITE_OMIT_SUBQUERY |
+/* |
+** The argument is an IN operator with a list (not a subquery) on the |
+** right-hand side. Return TRUE if that list is constant. |
+*/ |
+static int sqlite3InRhsIsConstant(Expr *pIn){ |
+ Expr *pLHS; |
+ int res; |
+ assert( !ExprHasProperty(pIn, EP_xIsSelect) ); |
+ pLHS = pIn->pLeft; |
+ pIn->pLeft = 0; |
+ res = sqlite3ExprIsConstant(pIn); |
+ pIn->pLeft = pLHS; |
+ return res; |
+} |
+#endif |
+ |
+/* |
+** This function is used by the implementation of the IN (...) operator. |
+** The pX parameter is the expression on the RHS of the IN operator, which |
+** might be either a list of expressions or a subquery. |
+** |
+** The job of this routine is to find or create a b-tree object that can |
+** be used either to test for membership in the RHS set or to iterate through |
+** all members of the RHS set, skipping duplicates. |
+** |
+** A cursor is opened on the b-tree object that is the RHS of the IN operator |
+** and pX->iTable is set to the index of that cursor. |
+** |
+** The returned value of this function indicates the b-tree type, as follows: |
+** |
+** IN_INDEX_ROWID - The cursor was opened on a database table. |
+** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index. |
+** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index. |
+** IN_INDEX_EPH - The cursor was opened on a specially created and |
+** populated epheremal table. |
+** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be |
+** implemented as a sequence of comparisons. |
+** |
+** An existing b-tree might be used if the RHS expression pX is a simple |
+** subquery such as: |
+** |
+** SELECT <column1>, <column2>... FROM <table> |
+** |
+** If the RHS of the IN operator is a list or a more complex subquery, then |
+** an ephemeral table might need to be generated from the RHS and then |
+** pX->iTable made to point to the ephemeral table instead of an |
+** existing table. |
+** |
+** The inFlags parameter must contain exactly one of the bits |
+** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains |
+** IN_INDEX_MEMBERSHIP, then the generated table will be used for a |
+** fast membership test. When the IN_INDEX_LOOP bit is set, the |
+** IN index will be used to loop over all values of the RHS of the |
+** IN operator. |
+** |
+** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate |
+** through the set members) then the b-tree must not contain duplicates. |
+** An epheremal table must be used unless the selected columns are guaranteed |
+** to be unique - either because it is an INTEGER PRIMARY KEY or due to |
+** a UNIQUE constraint or index. |
+** |
+** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used |
+** for fast set membership tests) then an epheremal table must |
+** be used unless <columns> is a single INTEGER PRIMARY KEY column or an |
+** index can be found with the specified <columns> as its left-most. |
+** |
+** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and |
+** if the RHS of the IN operator is a list (not a subquery) then this |
+** routine might decide that creating an ephemeral b-tree for membership |
+** testing is too expensive and return IN_INDEX_NOOP. In that case, the |
+** calling routine should implement the IN operator using a sequence |
+** of Eq or Ne comparison operations. |
+** |
+** When the b-tree is being used for membership tests, the calling function |
+** might need to know whether or not the RHS side of the IN operator |
+** contains a NULL. If prRhsHasNull is not a NULL pointer and |
+** if there is any chance that the (...) might contain a NULL value at |
+** runtime, then a register is allocated and the register number written |
+** to *prRhsHasNull. If there is no chance that the (...) contains a |
+** NULL value, then *prRhsHasNull is left unchanged. |
+** |
+** If a register is allocated and its location stored in *prRhsHasNull, then |
+** the value in that register will be NULL if the b-tree contains one or more |
+** NULL values, and it will be some non-NULL value if the b-tree contains no |
+** NULL values. |
+** |
+** If the aiMap parameter is not NULL, it must point to an array containing |
+** one element for each column returned by the SELECT statement on the RHS |
+** of the IN(...) operator. The i'th entry of the array is populated with the |
+** offset of the index column that matches the i'th column returned by the |
+** SELECT. For example, if the expression and selected index are: |
+** |
+** (?,?,?) IN (SELECT a, b, c FROM t1) |
+** CREATE INDEX i1 ON t1(b, c, a); |
+** |
+** then aiMap[] is populated with {2, 0, 1}. |
+*/ |
+#ifndef SQLITE_OMIT_SUBQUERY |
+SQLITE_PRIVATE int sqlite3FindInIndex( |
+ Parse *pParse, /* Parsing context */ |
+ Expr *pX, /* The right-hand side (RHS) of the IN operator */ |
+ u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */ |
+ int *prRhsHasNull, /* Register holding NULL status. See notes */ |
+ int *aiMap /* Mapping from Index fields to RHS fields */ |
+){ |
+ Select *p; /* SELECT to the right of IN operator */ |
+ int eType = 0; /* Type of RHS table. IN_INDEX_* */ |
+ int iTab = pParse->nTab++; /* Cursor of the RHS table */ |
+ int mustBeUnique; /* True if RHS must be unique */ |
+ Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ |
+ |
+ assert( pX->op==TK_IN ); |
+ mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0; |
+ |
+ /* If the RHS of this IN(...) operator is a SELECT, and if it matters |
+ ** whether or not the SELECT result contains NULL values, check whether |
+ ** or not NULL is actually possible (it may not be, for example, due |
+ ** to NOT NULL constraints in the schema). If no NULL values are possible, |
+ ** set prRhsHasNull to 0 before continuing. */ |
+ if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){ |
+ int i; |
+ ExprList *pEList = pX->x.pSelect->pEList; |
+ for(i=0; i<pEList->nExpr; i++){ |
+ if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break; |
+ } |
+ if( i==pEList->nExpr ){ |
+ prRhsHasNull = 0; |
+ } |
+ } |
+ |
+ /* Check to see if an existing table or index can be used to |
+ ** satisfy the query. This is preferable to generating a new |
+ ** ephemeral table. */ |
+ if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){ |
+ sqlite3 *db = pParse->db; /* Database connection */ |
+ Table *pTab; /* Table <table>. */ |
+ i16 iDb; /* Database idx for pTab */ |
+ ExprList *pEList = p->pEList; |
+ int nExpr = pEList->nExpr; |
+ |
+ assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ |
+ assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ |
+ assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ |
+ pTab = p->pSrc->a[0].pTab; |
+ |
+ /* Code an OP_Transaction and OP_TableLock for <table>. */ |
+ iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
+ sqlite3CodeVerifySchema(pParse, iDb); |
+ sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
+ |
+ assert(v); /* sqlite3GetVdbe() has always been previously called */ |
+ if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){ |
+ /* The "x IN (SELECT rowid FROM table)" case */ |
+ int iAddr = sqlite3VdbeAddOp0(v, OP_Once); |
+ VdbeCoverage(v); |
+ |
+ sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
+ eType = IN_INDEX_ROWID; |
+ |
+ sqlite3VdbeJumpHere(v, iAddr); |
+ }else{ |
+ Index *pIdx; /* Iterator variable */ |
+ int affinity_ok = 1; |
+ int i; |
+ |
+ /* Check that the affinity that will be used to perform each |
+ ** comparison is the same as the affinity of each column in table |
+ ** on the RHS of the IN operator. If it not, it is not possible to |
+ ** use any index of the RHS table. */ |
+ for(i=0; i<nExpr && affinity_ok; i++){ |
+ Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i); |
+ int iCol = pEList->a[i].pExpr->iColumn; |
+ char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */ |
+ char cmpaff = sqlite3CompareAffinity(pLhs, idxaff); |
+ testcase( cmpaff==SQLITE_AFF_BLOB ); |
+ testcase( cmpaff==SQLITE_AFF_TEXT ); |
+ switch( cmpaff ){ |
+ case SQLITE_AFF_BLOB: |
+ break; |
+ case SQLITE_AFF_TEXT: |
+ /* sqlite3CompareAffinity() only returns TEXT if one side or the |
+ ** other has no affinity and the other side is TEXT. Hence, |
+ ** the only way for cmpaff to be TEXT is for idxaff to be TEXT |
+ ** and for the term on the LHS of the IN to have no affinity. */ |
+ assert( idxaff==SQLITE_AFF_TEXT ); |
+ break; |
+ default: |
+ affinity_ok = sqlite3IsNumericAffinity(idxaff); |
+ } |
+ } |
+ |
+ if( affinity_ok ){ |
+ /* Search for an existing index that will work for this IN operator */ |
+ for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){ |
+ Bitmask colUsed; /* Columns of the index used */ |
+ Bitmask mCol; /* Mask for the current column */ |
+ if( pIdx->nColumn<nExpr ) continue; |
+ /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute |
+ ** BITMASK(nExpr) without overflowing */ |
+ testcase( pIdx->nColumn==BMS-2 ); |
+ testcase( pIdx->nColumn==BMS-1 ); |
+ if( pIdx->nColumn>=BMS-1 ) continue; |
+ if( mustBeUnique ){ |
+ if( pIdx->nKeyCol>nExpr |
+ ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx)) |
+ ){ |
+ continue; /* This index is not unique over the IN RHS columns */ |
+ } |
+ } |
+ |
+ colUsed = 0; /* Columns of index used so far */ |
+ for(i=0; i<nExpr; i++){ |
+ Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i); |
+ Expr *pRhs = pEList->a[i].pExpr; |
+ CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); |
+ int j; |
+ |
+ assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr ); |
+ for(j=0; j<nExpr; j++){ |
+ if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue; |
+ assert( pIdx->azColl[j] ); |
+ if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){ |
+ continue; |
+ } |
+ break; |
+ } |
+ if( j==nExpr ) break; |
+ mCol = MASKBIT(j); |
+ if( mCol & colUsed ) break; /* Each column used only once */ |
+ colUsed |= mCol; |
+ if( aiMap ) aiMap[i] = j; |
+ } |
+ |
+ assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) ); |
+ if( colUsed==(MASKBIT(nExpr)-1) ){ |
+ /* If we reach this point, that means the index pIdx is usable */ |
+ int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
+#ifndef SQLITE_OMIT_EXPLAIN |
+ sqlite3VdbeAddOp4(v, OP_Explain, 0, 0, 0, |
+ sqlite3MPrintf(db, "USING INDEX %s FOR IN-OPERATOR",pIdx->zName), |
+ P4_DYNAMIC); |
+#endif |
+ sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb); |
+ sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
+ VdbeComment((v, "%s", pIdx->zName)); |
+ assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 ); |
+ eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0]; |
+ |
+ if( prRhsHasNull ){ |
+#ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
+ i64 mask = (1<<nExpr)-1; |
+ sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, |
+ iTab, 0, 0, (u8*)&mask, P4_INT64); |
+#endif |
+ *prRhsHasNull = ++pParse->nMem; |
+ if( nExpr==1 ){ |
+ sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull); |
+ } |
+ } |
+ sqlite3VdbeJumpHere(v, iAddr); |
+ } |
+ } /* End loop over indexes */ |
+ } /* End if( affinity_ok ) */ |
+ } /* End if not an rowid index */ |
+ } /* End attempt to optimize using an index */ |
+ |
+ /* If no preexisting index is available for the IN clause |
+ ** and IN_INDEX_NOOP is an allowed reply |
+ ** and the RHS of the IN operator is a list, not a subquery |
+ ** and the RHS is not constant or has two or fewer terms, |
+ ** then it is not worth creating an ephemeral table to evaluate |
+ ** the IN operator so return IN_INDEX_NOOP. |
+ */ |
+ if( eType==0 |
+ && (inFlags & IN_INDEX_NOOP_OK) |
+ && !ExprHasProperty(pX, EP_xIsSelect) |
+ && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2) |
+ ){ |
+ eType = IN_INDEX_NOOP; |
+ } |
+ |
+ if( eType==0 ){ |
+ /* Could not find an existing table or index to use as the RHS b-tree. |
+ ** We will have to generate an ephemeral table to do the job. |
+ */ |
+ u32 savedNQueryLoop = pParse->nQueryLoop; |
+ int rMayHaveNull = 0; |
+ eType = IN_INDEX_EPH; |
+ if( inFlags & IN_INDEX_LOOP ){ |
+ pParse->nQueryLoop = 0; |
+ if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){ |
+ eType = IN_INDEX_ROWID; |
+ } |
+ }else if( prRhsHasNull ){ |
+ *prRhsHasNull = rMayHaveNull = ++pParse->nMem; |
+ } |
+ sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); |
+ pParse->nQueryLoop = savedNQueryLoop; |
+ }else{ |
+ pX->iTable = iTab; |
+ } |
+ |
+ if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){ |
+ int i, n; |
+ n = sqlite3ExprVectorSize(pX->pLeft); |
+ for(i=0; i<n; i++) aiMap[i] = i; |
+ } |
+ return eType; |
+} |
+#endif |
+ |
+#ifndef SQLITE_OMIT_SUBQUERY |
+/* |
+** Argument pExpr is an (?, ?...) IN(...) expression. This |
+** function allocates and returns a nul-terminated string containing |
+** the affinities to be used for each column of the comparison. |
+** |
+** It is the responsibility of the caller to ensure that the returned |
+** string is eventually freed using sqlite3DbFree(). |
+*/ |
+static char *exprINAffinity(Parse *pParse, Expr *pExpr){ |
+ Expr *pLeft = pExpr->pLeft; |
+ int nVal = sqlite3ExprVectorSize(pLeft); |
+ Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0; |
+ char *zRet; |
+ |
+ assert( pExpr->op==TK_IN ); |
+ zRet = sqlite3DbMallocZero(pParse->db, nVal+1); |
+ if( zRet ){ |
+ int i; |
+ for(i=0; i<nVal; i++){ |
+ Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i); |
+ char a = sqlite3ExprAffinity(pA); |
+ if( pSelect ){ |
+ zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a); |
+ }else{ |
+ zRet[i] = a; |
+ } |
+ } |
+ zRet[nVal] = '\0'; |
+ } |
+ return zRet; |
+} |
+#endif |
+ |
+#ifndef SQLITE_OMIT_SUBQUERY |
+/* |
+** Load the Parse object passed as the first argument with an error |
+** message of the form: |
+** |
+** "sub-select returns N columns - expected M" |
+*/ |
+SQLITE_PRIVATE void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){ |
+ const char *zFmt = "sub-select returns %d columns - expected %d"; |
+ sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect); |
+} |
+#endif |
+ |
+/* |
+** Expression pExpr is a vector that has been used in a context where |
+** it is not permitted. If pExpr is a sub-select vector, this routine |
+** loads the Parse object with a message of the form: |
+** |
+** "sub-select returns N columns - expected 1" |
+** |
+** Or, if it is a regular scalar vector: |
+** |
+** "row value misused" |
+*/ |
+SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){ |
+#ifndef SQLITE_OMIT_SUBQUERY |
+ if( pExpr->flags & EP_xIsSelect ){ |
+ sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1); |
+ }else |
+#endif |
+ { |
+ sqlite3ErrorMsg(pParse, "row value misused"); |
+ } |
+} |
+ |
+/* |
+** Generate code for scalar subqueries used as a subquery expression, EXISTS, |
+** or IN operators. Examples: |
+** |
+** (SELECT a FROM b) -- subquery |
+** EXISTS (SELECT a FROM b) -- EXISTS subquery |
+** x IN (4,5,11) -- IN operator with list on right-hand side |
+** x IN (SELECT a FROM b) -- IN operator with subquery on the right |
+** |
+** The pExpr parameter describes the expression that contains the IN |
+** operator or subquery. |
+** |
+** If parameter isRowid is non-zero, then expression pExpr is guaranteed |
+** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference |
+** to some integer key column of a table B-Tree. In this case, use an |
+** intkey B-Tree to store the set of IN(...) values instead of the usual |
+** (slower) variable length keys B-Tree. |
+** |
+** If rMayHaveNull is non-zero, that means that the operation is an IN |
+** (not a SELECT or EXISTS) and that the RHS might contains NULLs. |
+** All this routine does is initialize the register given by rMayHaveNull |
+** to NULL. Calling routines will take care of changing this register |
+** value to non-NULL if the RHS is NULL-free. |
+** |
+** For a SELECT or EXISTS operator, return the register that holds the |
+** result. For a multi-column SELECT, the result is stored in a contiguous |
+** array of registers and the return value is the register of the left-most |
+** result column. Return 0 for IN operators or if an error occurs. |
+*/ |
+#ifndef SQLITE_OMIT_SUBQUERY |
+SQLITE_PRIVATE int sqlite3CodeSubselect( |
+ Parse *pParse, /* Parsing context */ |
+ Expr *pExpr, /* The IN, SELECT, or EXISTS operator */ |
+ int rHasNullFlag, /* Register that records whether NULLs exist in RHS */ |
+ int isRowid /* If true, LHS of IN operator is a rowid */ |
+){ |
+ int jmpIfDynamic = -1; /* One-time test address */ |
+ int rReg = 0; /* Register storing resulting */ |
+ Vdbe *v = sqlite3GetVdbe(pParse); |
+ if( NEVER(v==0) ) return 0; |
+ sqlite3ExprCachePush(pParse); |
+ |
+ /* The evaluation of the IN/EXISTS/SELECT must be repeated every time it |
+ ** is encountered if any of the following is true: |
+ ** |
+ ** * The right-hand side is a correlated subquery |
+ ** * The right-hand side is an expression list containing variables |
+ ** * We are inside a trigger |
+ ** |
+ ** If all of the above are false, then we can run this code just once |
+ ** save the results, and reuse the same result on subsequent invocations. |
+ */ |
+ if( !ExprHasProperty(pExpr, EP_VarSelect) ){ |
+ jmpIfDynamic = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
+ } |
+ |
+#ifndef SQLITE_OMIT_EXPLAIN |
+ if( pParse->explain==2 ){ |
+ char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d", |
+ jmpIfDynamic>=0?"":"CORRELATED ", |
+ pExpr->op==TK_IN?"LIST":"SCALAR", |
+ pParse->iNextSelectId |
+ ); |
+ sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); |
+ } |
+#endif |
+ |
+ switch( pExpr->op ){ |
+ case TK_IN: { |
+ int addr; /* Address of OP_OpenEphemeral instruction */ |
+ Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ |
+ KeyInfo *pKeyInfo = 0; /* Key information */ |
+ int nVal; /* Size of vector pLeft */ |
+ |
+ nVal = sqlite3ExprVectorSize(pLeft); |
+ assert( !isRowid || nVal==1 ); |
+ |
+ /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' |
+ ** expression it is handled the same way. An ephemeral table is |
+ ** filled with index keys representing the results from the |
+ ** SELECT or the <exprlist>. |
+ ** |
+ ** If the 'x' expression is a column value, or the SELECT... |
+ ** statement returns a column value, then the affinity of that |
+ ** column is used to build the index keys. If both 'x' and the |
+ ** SELECT... statement are columns, then numeric affinity is used |
+ ** if either column has NUMERIC or INTEGER affinity. If neither |
+ ** 'x' nor the SELECT... statement are columns, then numeric affinity |
+ ** is used. |
+ */ |
+ pExpr->iTable = pParse->nTab++; |
+ addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, |
+ pExpr->iTable, (isRowid?0:nVal)); |
+ pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1); |
+ |
+ if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
+ /* Case 1: expr IN (SELECT ...) |
+ ** |
+ ** Generate code to write the results of the select into the temporary |
+ ** table allocated and opened above. |
+ */ |
+ Select *pSelect = pExpr->x.pSelect; |
+ ExprList *pEList = pSelect->pEList; |
+ |
+ assert( !isRowid ); |
+ /* If the LHS and RHS of the IN operator do not match, that |
+ ** error will have been caught long before we reach this point. */ |
+ if( ALWAYS(pEList->nExpr==nVal) ){ |
+ SelectDest dest; |
+ int i; |
+ sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); |
+ dest.zAffSdst = exprINAffinity(pParse, pExpr); |
+ assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); |
+ pSelect->iLimit = 0; |
+ testcase( pSelect->selFlags & SF_Distinct ); |
+ testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ |
+ if( sqlite3Select(pParse, pSelect, &dest) ){ |
+ sqlite3DbFree(pParse->db, dest.zAffSdst); |
+ sqlite3KeyInfoUnref(pKeyInfo); |
+ return 0; |
+ } |
+ sqlite3DbFree(pParse->db, dest.zAffSdst); |
+ assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */ |
+ assert( pEList!=0 ); |
+ assert( pEList->nExpr>0 ); |
+ assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); |
+ for(i=0; i<nVal; i++){ |
+ Expr *p = sqlite3VectorFieldSubexpr(pLeft, i); |
+ pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq( |
+ pParse, p, pEList->a[i].pExpr |
+ ); |
+ } |
+ } |
+ }else if( ALWAYS(pExpr->x.pList!=0) ){ |
+ /* Case 2: expr IN (exprlist) |
+ ** |
+ ** For each expression, build an index key from the evaluation and |
+ ** store it in the temporary table. If <expr> is a column, then use |
+ ** that columns affinity when building index keys. If <expr> is not |
+ ** a column, use numeric affinity. |
+ */ |
+ char affinity; /* Affinity of the LHS of the IN */ |
+ int i; |
+ ExprList *pList = pExpr->x.pList; |
+ struct ExprList_item *pItem; |
+ int r1, r2, r3; |
+ |
+ affinity = sqlite3ExprAffinity(pLeft); |
+ if( !affinity ){ |
+ affinity = SQLITE_AFF_BLOB; |
+ } |
+ if( pKeyInfo ){ |
+ assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); |
+ pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
+ } |
+ |
+ /* Loop through each expression in <exprlist>. */ |
+ r1 = sqlite3GetTempReg(pParse); |
+ r2 = sqlite3GetTempReg(pParse); |
+ if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2); |
+ for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ |
+ Expr *pE2 = pItem->pExpr; |
+ int iValToIns; |
+ |
+ /* If the expression is not constant then we will need to |
+ ** disable the test that was generated above that makes sure |
+ ** this code only executes once. Because for a non-constant |
+ ** expression we need to rerun this code each time. |
+ */ |
+ if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){ |
+ sqlite3VdbeChangeToNoop(v, jmpIfDynamic); |
+ jmpIfDynamic = -1; |
+ } |
+ |
+ /* Evaluate the expression and insert it into the temp table */ |
+ if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){ |
+ sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns); |
+ }else{ |
+ r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); |
+ if( isRowid ){ |
+ sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, |
+ sqlite3VdbeCurrentAddr(v)+2); |
+ VdbeCoverage(v); |
+ sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); |
+ }else{ |
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); |
+ sqlite3ExprCacheAffinityChange(pParse, r3, 1); |
+ sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pExpr->iTable, r2, r3, 1); |
+ } |
+ } |
+ } |
+ sqlite3ReleaseTempReg(pParse, r1); |
+ sqlite3ReleaseTempReg(pParse, r2); |
+ } |
+ if( pKeyInfo ){ |
+ sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO); |
+ } |
+ break; |
+ } |
+ |
+ case TK_EXISTS: |
+ case TK_SELECT: |
+ default: { |
+ /* Case 3: (SELECT ... FROM ...) |
+ ** or: EXISTS(SELECT ... FROM ...) |
+ ** |
+ ** For a SELECT, generate code to put the values for all columns of |
+ ** the first row into an array of registers and return the index of |
+ ** the first register. |
+ ** |
+ ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists) |
+ ** into a register and return that register number. |
+ ** |
+ ** In both cases, the query is augmented with "LIMIT 1". Any |
+ ** preexisting limit is discarded in place of the new LIMIT 1. |
+ */ |
+ Select *pSel; /* SELECT statement to encode */ |
+ SelectDest dest; /* How to deal with SELECT result */ |
+ int nReg; /* Registers to allocate */ |
+ |
+ testcase( pExpr->op==TK_EXISTS ); |
+ testcase( pExpr->op==TK_SELECT ); |
+ assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); |
+ assert( ExprHasProperty(pExpr, EP_xIsSelect) ); |
+ |
+ pSel = pExpr->x.pSelect; |
+ nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1; |
+ sqlite3SelectDestInit(&dest, 0, pParse->nMem+1); |
+ pParse->nMem += nReg; |
+ if( pExpr->op==TK_SELECT ){ |
+ dest.eDest = SRT_Mem; |
+ dest.iSdst = dest.iSDParm; |
+ dest.nSdst = nReg; |
+ sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1); |
+ VdbeComment((v, "Init subquery result")); |
+ }else{ |
+ dest.eDest = SRT_Exists; |
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm); |
+ VdbeComment((v, "Init EXISTS result")); |
+ } |
+ sqlite3ExprDelete(pParse->db, pSel->pLimit); |
+ pSel->pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER, |
+ &sqlite3IntTokens[1], 0); |
+ pSel->iLimit = 0; |
+ pSel->selFlags &= ~SF_MultiValue; |
+ if( sqlite3Select(pParse, pSel, &dest) ){ |
+ return 0; |
+ } |
+ rReg = dest.iSDParm; |
+ ExprSetVVAProperty(pExpr, EP_NoReduce); |
+ break; |
+ } |
+ } |
+ |
+ if( rHasNullFlag ){ |
+ sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag); |
+ } |
+ |
+ if( jmpIfDynamic>=0 ){ |
+ sqlite3VdbeJumpHere(v, jmpIfDynamic); |
+ } |
+ sqlite3ExprCachePop(pParse); |
+ |
+ return rReg; |
+} |
+#endif /* SQLITE_OMIT_SUBQUERY */ |
+ |
+#ifndef SQLITE_OMIT_SUBQUERY |
+/* |
+** Expr pIn is an IN(...) expression. This function checks that the |
+** sub-select on the RHS of the IN() operator has the same number of |
+** columns as the vector on the LHS. Or, if the RHS of the IN() is not |
+** a sub-query, that the LHS is a vector of size 1. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){ |
+ int nVector = sqlite3ExprVectorSize(pIn->pLeft); |
+ if( (pIn->flags & EP_xIsSelect) ){ |
+ if( nVector!=pIn->x.pSelect->pEList->nExpr ){ |
+ sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector); |
+ return 1; |
+ } |
+ }else if( nVector!=1 ){ |
+ sqlite3VectorErrorMsg(pParse, pIn->pLeft); |
+ return 1; |
+ } |
+ return 0; |
+} |
+#endif |
+ |
+#ifndef SQLITE_OMIT_SUBQUERY |
+/* |
+** Generate code for an IN expression. |
+** |
+** x IN (SELECT ...) |
+** x IN (value, value, ...) |
+** |
+** The left-hand side (LHS) is a scalar or vector expression. The |
+** right-hand side (RHS) is an array of zero or more scalar values, or a |
+** subquery. If the RHS is a subquery, the number of result columns must |
+** match the number of columns in the vector on the LHS. If the RHS is |
+** a list of values, the LHS must be a scalar. |
+** |
+** The IN operator is true if the LHS value is contained within the RHS. |
+** The result is false if the LHS is definitely not in the RHS. The |
+** result is NULL if the presence of the LHS in the RHS cannot be |
+** determined due to NULLs. |
+** |
+** This routine generates code that jumps to destIfFalse if the LHS is not |
+** contained within the RHS. If due to NULLs we cannot determine if the LHS |
+** is contained in the RHS then jump to destIfNull. If the LHS is contained |
+** within the RHS then fall through. |
+** |
+** See the separate in-operator.md documentation file in the canonical |
+** SQLite source tree for additional information. |
+*/ |
+static void sqlite3ExprCodeIN( |
+ Parse *pParse, /* Parsing and code generating context */ |
+ Expr *pExpr, /* The IN expression */ |
+ int destIfFalse, /* Jump here if LHS is not contained in the RHS */ |
+ int destIfNull /* Jump here if the results are unknown due to NULLs */ |
+){ |
+ int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ |
+ int eType; /* Type of the RHS */ |
+ int rLhs; /* Register(s) holding the LHS values */ |
+ int rLhsOrig; /* LHS values prior to reordering by aiMap[] */ |
+ Vdbe *v; /* Statement under construction */ |
+ int *aiMap = 0; /* Map from vector field to index column */ |
+ char *zAff = 0; /* Affinity string for comparisons */ |
+ int nVector; /* Size of vectors for this IN operator */ |
+ int iDummy; /* Dummy parameter to exprCodeVector() */ |
+ Expr *pLeft; /* The LHS of the IN operator */ |
+ int i; /* loop counter */ |
+ int destStep2; /* Where to jump when NULLs seen in step 2 */ |
+ int destStep6 = 0; /* Start of code for Step 6 */ |
+ int addrTruthOp; /* Address of opcode that determines the IN is true */ |
+ int destNotNull; /* Jump here if a comparison is not true in step 6 */ |
+ int addrTop; /* Top of the step-6 loop */ |
+ |
+ pLeft = pExpr->pLeft; |
+ if( sqlite3ExprCheckIN(pParse, pExpr) ) return; |
+ zAff = exprINAffinity(pParse, pExpr); |
+ nVector = sqlite3ExprVectorSize(pExpr->pLeft); |
+ aiMap = (int*)sqlite3DbMallocZero( |
+ pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1 |
+ ); |
+ if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error; |
+ |
+ /* Attempt to compute the RHS. After this step, if anything other than |
+ ** IN_INDEX_NOOP is returned, the table opened ith cursor pExpr->iTable |
+ ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned, |
+ ** the RHS has not yet been coded. */ |
+ v = pParse->pVdbe; |
+ assert( v!=0 ); /* OOM detected prior to this routine */ |
+ VdbeNoopComment((v, "begin IN expr")); |
+ eType = sqlite3FindInIndex(pParse, pExpr, |
+ IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK, |
+ destIfFalse==destIfNull ? 0 : &rRhsHasNull, aiMap); |
+ |
+ assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH |
+ || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC |
+ ); |
+#ifdef SQLITE_DEBUG |
+ /* Confirm that aiMap[] contains nVector integer values between 0 and |
+ ** nVector-1. */ |
+ for(i=0; i<nVector; i++){ |
+ int j, cnt; |
+ for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++; |
+ assert( cnt==1 ); |
+ } |
+#endif |
+ |
+ /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a |
+ ** vector, then it is stored in an array of nVector registers starting |
+ ** at r1. |
+ ** |
+ ** sqlite3FindInIndex() might have reordered the fields of the LHS vector |
+ ** so that the fields are in the same order as an existing index. The |
+ ** aiMap[] array contains a mapping from the original LHS field order to |
+ ** the field order that matches the RHS index. |
+ */ |
+ sqlite3ExprCachePush(pParse); |
+ rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy); |
+ for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */ |
+ if( i==nVector ){ |
+ /* LHS fields are not reordered */ |
+ rLhs = rLhsOrig; |
+ }else{ |
+ /* Need to reorder the LHS fields according to aiMap */ |
+ rLhs = sqlite3GetTempRange(pParse, nVector); |
+ for(i=0; i<nVector; i++){ |
+ sqlite3VdbeAddOp3(v, OP_Copy, rLhsOrig+i, rLhs+aiMap[i], 0); |
+ } |
+ } |
+ |
+ /* If sqlite3FindInIndex() did not find or create an index that is |
+ ** suitable for evaluating the IN operator, then evaluate using a |
+ ** sequence of comparisons. |
+ ** |
+ ** This is step (1) in the in-operator.md optimized algorithm. |
+ */ |
+ if( eType==IN_INDEX_NOOP ){ |
+ ExprList *pList = pExpr->x.pList; |
+ CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
+ int labelOk = sqlite3VdbeMakeLabel(v); |
+ int r2, regToFree; |
+ int regCkNull = 0; |
+ int ii; |
+ assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
+ if( destIfNull!=destIfFalse ){ |
+ regCkNull = sqlite3GetTempReg(pParse); |
+ sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull); |
+ } |
+ for(ii=0; ii<pList->nExpr; ii++){ |
+ r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree); |
+ if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){ |
+ sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull); |
+ } |
+ if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){ |
+ sqlite3VdbeAddOp4(v, OP_Eq, rLhs, labelOk, r2, |
+ (void*)pColl, P4_COLLSEQ); |
+ VdbeCoverageIf(v, ii<pList->nExpr-1); |
+ VdbeCoverageIf(v, ii==pList->nExpr-1); |
+ sqlite3VdbeChangeP5(v, zAff[0]); |
+ }else{ |
+ assert( destIfNull==destIfFalse ); |
+ sqlite3VdbeAddOp4(v, OP_Ne, rLhs, destIfFalse, r2, |
+ (void*)pColl, P4_COLLSEQ); VdbeCoverage(v); |
+ sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL); |
+ } |
+ sqlite3ReleaseTempReg(pParse, regToFree); |
+ } |
+ if( regCkNull ){ |
+ sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v); |
+ sqlite3VdbeGoto(v, destIfFalse); |
+ } |
+ sqlite3VdbeResolveLabel(v, labelOk); |
+ sqlite3ReleaseTempReg(pParse, regCkNull); |
+ goto sqlite3ExprCodeIN_finished; |
+ } |
+ |
+ /* Step 2: Check to see if the LHS contains any NULL columns. If the |
+ ** LHS does contain NULLs then the result must be either FALSE or NULL. |
+ ** We will then skip the binary search of the RHS. |
+ */ |
+ if( destIfNull==destIfFalse ){ |
+ destStep2 = destIfFalse; |
+ }else{ |
+ destStep2 = destStep6 = sqlite3VdbeMakeLabel(v); |
+ } |
+ for(i=0; i<nVector; i++){ |
+ Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i); |
+ if( sqlite3ExprCanBeNull(p) ){ |
+ sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2); |
+ VdbeCoverage(v); |
+ } |
+ } |
+ |
+ /* Step 3. The LHS is now known to be non-NULL. Do the binary search |
+ ** of the RHS using the LHS as a probe. If found, the result is |
+ ** true. |
+ */ |
+ if( eType==IN_INDEX_ROWID ){ |
+ /* In this case, the RHS is the ROWID of table b-tree and so we also |
+ ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4 |
+ ** into a single opcode. */ |
+ sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, rLhs); |
+ VdbeCoverage(v); |
+ addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto); /* Return True */ |
+ }else{ |
+ sqlite3VdbeAddOp4(v, OP_Affinity, rLhs, nVector, 0, zAff, nVector); |
+ if( destIfFalse==destIfNull ){ |
+ /* Combine Step 3 and Step 5 into a single opcode */ |
+ sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, |
+ rLhs, nVector); VdbeCoverage(v); |
+ goto sqlite3ExprCodeIN_finished; |
+ } |
+ /* Ordinary Step 3, for the case where FALSE and NULL are distinct */ |
+ addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, |
+ rLhs, nVector); VdbeCoverage(v); |
+ } |
+ |
+ /* Step 4. If the RHS is known to be non-NULL and we did not find |
+ ** an match on the search above, then the result must be FALSE. |
+ */ |
+ if( rRhsHasNull && nVector==1 ){ |
+ sqlite3VdbeAddOp2(v, OP_NotNull, rRhsHasNull, destIfFalse); |
+ VdbeCoverage(v); |
+ } |
+ |
+ /* Step 5. If we do not care about the difference between NULL and |
+ ** FALSE, then just return false. |
+ */ |
+ if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse); |
+ |
+ /* Step 6: Loop through rows of the RHS. Compare each row to the LHS. |
+ ** If any comparison is NULL, then the result is NULL. If all |
+ ** comparisons are FALSE then the final result is FALSE. |
+ ** |
+ ** For a scalar LHS, it is sufficient to check just the first row |
+ ** of the RHS. |
+ */ |
+ if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6); |
+ addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); |
+ VdbeCoverage(v); |
+ if( nVector>1 ){ |
+ destNotNull = sqlite3VdbeMakeLabel(v); |
+ }else{ |
+ /* For nVector==1, combine steps 6 and 7 by immediately returning |
+ ** FALSE if the first comparison is not NULL */ |
+ destNotNull = destIfFalse; |
+ } |
+ for(i=0; i<nVector; i++){ |
+ Expr *p; |
+ CollSeq *pColl; |
+ int r3 = sqlite3GetTempReg(pParse); |
+ p = sqlite3VectorFieldSubexpr(pLeft, i); |
+ pColl = sqlite3ExprCollSeq(pParse, p); |
+ sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, i, r3); |
+ sqlite3VdbeAddOp4(v, OP_Ne, rLhs+i, destNotNull, r3, |
+ (void*)pColl, P4_COLLSEQ); |
+ VdbeCoverage(v); |
+ sqlite3ReleaseTempReg(pParse, r3); |
+ } |
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull); |
+ if( nVector>1 ){ |
+ sqlite3VdbeResolveLabel(v, destNotNull); |
+ sqlite3VdbeAddOp2(v, OP_Next, pExpr->iTable, addrTop+1); |
+ VdbeCoverage(v); |
+ |
+ /* Step 7: If we reach this point, we know that the result must |
+ ** be false. */ |
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); |
+ } |
+ |
+ /* Jumps here in order to return true. */ |
+ sqlite3VdbeJumpHere(v, addrTruthOp); |
+ |
+sqlite3ExprCodeIN_finished: |
+ if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs); |
+ sqlite3ExprCachePop(pParse); |
+ VdbeComment((v, "end IN expr")); |
+sqlite3ExprCodeIN_oom_error: |
+ sqlite3DbFree(pParse->db, aiMap); |
+ sqlite3DbFree(pParse->db, zAff); |
+} |
+#endif /* SQLITE_OMIT_SUBQUERY */ |
+ |
+#ifndef SQLITE_OMIT_FLOATING_POINT |
+/* |
+** Generate an instruction that will put the floating point |
+** value described by z[0..n-1] into register iMem. |
+** |
+** The z[] string will probably not be zero-terminated. But the |
+** z[n] character is guaranteed to be something that does not look |
+** like the continuation of the number. |
+*/ |
+static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ |
+ if( ALWAYS(z!=0) ){ |
+ double value; |
+ sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); |
+ assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ |
+ if( negateFlag ) value = -value; |
+ sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL); |
+ } |
+} |
+#endif |
+ |
+ |
+/* |
+** Generate an instruction that will put the integer describe by |
+** text z[0..n-1] into register iMem. |
+** |
+** Expr.u.zToken is always UTF8 and zero-terminated. |
+*/ |
+static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){ |
+ Vdbe *v = pParse->pVdbe; |
+ if( pExpr->flags & EP_IntValue ){ |
+ int i = pExpr->u.iValue; |
+ assert( i>=0 ); |
+ if( negFlag ) i = -i; |
+ sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); |
+ }else{ |
+ int c; |
+ i64 value; |
+ const char *z = pExpr->u.zToken; |
+ assert( z!=0 ); |
+ c = sqlite3DecOrHexToI64(z, &value); |
+ if( c==1 || (c==2 && !negFlag) || (negFlag && value==SMALLEST_INT64)){ |
+#ifdef SQLITE_OMIT_FLOATING_POINT |
+ sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); |
+#else |
+#ifndef SQLITE_OMIT_HEX_INTEGER |
+ if( sqlite3_strnicmp(z,"0x",2)==0 ){ |
+ sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z); |
+ }else |
+#endif |
+ { |
+ codeReal(v, z, negFlag, iMem); |
+ } |
+#endif |
+ }else{ |
+ if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; } |
+ sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64); |
+ } |
+ } |
+} |
+ |
+/* |
+** Erase column-cache entry number i |
+*/ |
+static void cacheEntryClear(Parse *pParse, int i){ |
+ if( pParse->aColCache[i].tempReg ){ |
+ if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){ |
+ pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg; |
+ } |
+ } |
+ pParse->nColCache--; |
+ if( i<pParse->nColCache ){ |
+ pParse->aColCache[i] = pParse->aColCache[pParse->nColCache]; |
+ } |
+} |
+ |
+ |
+/* |
+** Record in the column cache that a particular column from a |
+** particular table is stored in a particular register. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){ |
+ int i; |
+ int minLru; |
+ int idxLru; |
+ struct yColCache *p; |
+ |
+ /* Unless an error has occurred, register numbers are always positive. */ |
+ assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed ); |
+ assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */ |
+ |
+ /* The SQLITE_ColumnCache flag disables the column cache. This is used |
+ ** for testing only - to verify that SQLite always gets the same answer |
+ ** with and without the column cache. |
+ */ |
+ if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return; |
+ |
+ /* First replace any existing entry. |
+ ** |
+ ** Actually, the way the column cache is currently used, we are guaranteed |
+ ** that the object will never already be in cache. Verify this guarantee. |
+ */ |
+#ifndef NDEBUG |
+ for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ |
+ assert( p->iTable!=iTab || p->iColumn!=iCol ); |
+ } |
+#endif |
+ |
+ /* If the cache is already full, delete the least recently used entry */ |
+ if( pParse->nColCache>=SQLITE_N_COLCACHE ){ |
+ minLru = 0x7fffffff; |
+ idxLru = -1; |
+ for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ |
+ if( p->lru<minLru ){ |
+ idxLru = i; |
+ minLru = p->lru; |
+ } |
+ } |
+ p = &pParse->aColCache[idxLru]; |
+ }else{ |
+ p = &pParse->aColCache[pParse->nColCache++]; |
+ } |
+ |
+ /* Add the new entry to the end of the cache */ |
+ p->iLevel = pParse->iCacheLevel; |
+ p->iTable = iTab; |
+ p->iColumn = iCol; |
+ p->iReg = iReg; |
+ p->tempReg = 0; |
+ p->lru = pParse->iCacheCnt++; |
+} |
+ |
+/* |
+** Indicate that registers between iReg..iReg+nReg-1 are being overwritten. |
+** Purge the range of registers from the column cache. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){ |
+ int i = 0; |
+ while( i<pParse->nColCache ){ |
+ struct yColCache *p = &pParse->aColCache[i]; |
+ if( p->iReg >= iReg && p->iReg < iReg+nReg ){ |
+ cacheEntryClear(pParse, i); |
+ }else{ |
+ i++; |
+ } |
+ } |
+} |
+ |
+/* |
+** Remember the current column cache context. Any new entries added |
+** added to the column cache after this call are removed when the |
+** corresponding pop occurs. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){ |
+ pParse->iCacheLevel++; |
+#ifdef SQLITE_DEBUG |
+ if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ |
+ printf("PUSH to %d\n", pParse->iCacheLevel); |
+ } |
+#endif |
+} |
+ |
+/* |
+** Remove from the column cache any entries that were added since the |
+** the previous sqlite3ExprCachePush operation. In other words, restore |
+** the cache to the state it was in prior the most recent Push. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse){ |
+ int i = 0; |
+ assert( pParse->iCacheLevel>=1 ); |
+ pParse->iCacheLevel--; |
+#ifdef SQLITE_DEBUG |
+ if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ |
+ printf("POP to %d\n", pParse->iCacheLevel); |
+ } |
+#endif |
+ while( i<pParse->nColCache ){ |
+ if( pParse->aColCache[i].iLevel>pParse->iCacheLevel ){ |
+ cacheEntryClear(pParse, i); |
+ }else{ |
+ i++; |
+ } |
+ } |
+} |
+ |
+/* |
+** When a cached column is reused, make sure that its register is |
+** no longer available as a temp register. ticket #3879: that same |
+** register might be in the cache in multiple places, so be sure to |
+** get them all. |
+*/ |
+static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){ |
+ int i; |
+ struct yColCache *p; |
+ for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ |
+ if( p->iReg==iReg ){ |
+ p->tempReg = 0; |
+ } |
+ } |
+} |
+ |
+/* Generate code that will load into register regOut a value that is |
+** appropriate for the iIdxCol-th column of index pIdx. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn( |
+ Parse *pParse, /* The parsing context */ |
+ Index *pIdx, /* The index whose column is to be loaded */ |
+ int iTabCur, /* Cursor pointing to a table row */ |
+ int iIdxCol, /* The column of the index to be loaded */ |
+ int regOut /* Store the index column value in this register */ |
+){ |
+ i16 iTabCol = pIdx->aiColumn[iIdxCol]; |
+ if( iTabCol==XN_EXPR ){ |
+ assert( pIdx->aColExpr ); |
+ assert( pIdx->aColExpr->nExpr>iIdxCol ); |
+ pParse->iSelfTab = iTabCur; |
+ sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut); |
+ }else{ |
+ sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur, |
+ iTabCol, regOut); |
+ } |
+} |
+ |
+/* |
+** Generate code to extract the value of the iCol-th column of a table. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable( |
+ Vdbe *v, /* The VDBE under construction */ |
+ Table *pTab, /* The table containing the value */ |
+ int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */ |
+ int iCol, /* Index of the column to extract */ |
+ int regOut /* Extract the value into this register */ |
+){ |
+ if( iCol<0 || iCol==pTab->iPKey ){ |
+ sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); |
+ }else{ |
+ int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; |
+ int x = iCol; |
+ if( !HasRowid(pTab) && !IsVirtual(pTab) ){ |
+ x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol); |
+ } |
+ sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); |
+ } |
+ if( iCol>=0 ){ |
+ sqlite3ColumnDefault(v, pTab, iCol, regOut); |
+ } |
+} |
+ |
+/* |
+** Generate code that will extract the iColumn-th column from |
+** table pTab and store the column value in a register. |
+** |
+** An effort is made to store the column value in register iReg. This |
+** is not garanteeed for GetColumn() - the result can be stored in |
+** any register. But the result is guaranteed to land in register iReg |
+** for GetColumnToReg(). |
+** |
+** There must be an open cursor to pTab in iTable when this routine |
+** is called. If iColumn<0 then code is generated that extracts the rowid. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprCodeGetColumn( |
+ Parse *pParse, /* Parsing and code generating context */ |
+ Table *pTab, /* Description of the table we are reading from */ |
+ int iColumn, /* Index of the table column */ |
+ int iTable, /* The cursor pointing to the table */ |
+ int iReg, /* Store results here */ |
+ u8 p5 /* P5 value for OP_Column + FLAGS */ |
+){ |
+ Vdbe *v = pParse->pVdbe; |
+ int i; |
+ struct yColCache *p; |
+ |
+ for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ |
+ if( p->iTable==iTable && p->iColumn==iColumn ){ |
+ p->lru = pParse->iCacheCnt++; |
+ sqlite3ExprCachePinRegister(pParse, p->iReg); |
+ return p->iReg; |
+ } |
+ } |
+ assert( v!=0 ); |
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg); |
+ if( p5 ){ |
+ sqlite3VdbeChangeP5(v, p5); |
+ }else{ |
+ sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg); |
+ } |
+ return iReg; |
+} |
+SQLITE_PRIVATE void sqlite3ExprCodeGetColumnToReg( |
+ Parse *pParse, /* Parsing and code generating context */ |
+ Table *pTab, /* Description of the table we are reading from */ |
+ int iColumn, /* Index of the table column */ |
+ int iTable, /* The cursor pointing to the table */ |
+ int iReg /* Store results here */ |
+){ |
+ int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0); |
+ if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg); |
+} |
+ |
+ |
+/* |
+** Clear all column cache entries. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){ |
+ int i; |
+ |
+#if SQLITE_DEBUG |
+ if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ |
+ printf("CLEAR\n"); |
+ } |
+#endif |
+ for(i=0; i<pParse->nColCache; i++){ |
+ if( pParse->aColCache[i].tempReg |
+ && pParse->nTempReg<ArraySize(pParse->aTempReg) |
+ ){ |
+ pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg; |
+ } |
+ } |
+ pParse->nColCache = 0; |
+} |
+ |
+/* |
+** Record the fact that an affinity change has occurred on iCount |
+** registers starting with iStart. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ |
+ sqlite3ExprCacheRemove(pParse, iStart, iCount); |
+} |
+ |
+/* |
+** Generate code to move content from registers iFrom...iFrom+nReg-1 |
+** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ |
+ assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo ); |
+ sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); |
+ sqlite3ExprCacheRemove(pParse, iFrom, nReg); |
+} |
+ |
+#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) |
+/* |
+** Return true if any register in the range iFrom..iTo (inclusive) |
+** is used as part of the column cache. |
+** |
+** This routine is used within assert() and testcase() macros only |
+** and does not appear in a normal build. |
+*/ |
+static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ |
+ int i; |
+ struct yColCache *p; |
+ for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ |
+ int r = p->iReg; |
+ if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/ |
+ } |
+ return 0; |
+} |
+#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ |
+ |
+ |
+/* |
+** Convert a scalar expression node to a TK_REGISTER referencing |
+** register iReg. The caller must ensure that iReg already contains |
+** the correct value for the expression. |
+*/ |
+static void exprToRegister(Expr *p, int iReg){ |
+ p->op2 = p->op; |
+ p->op = TK_REGISTER; |
+ p->iTable = iReg; |
+ ExprClearProperty(p, EP_Skip); |
+} |
+ |
+/* |
+** Evaluate an expression (either a vector or a scalar expression) and store |
+** the result in continguous temporary registers. Return the index of |
+** the first register used to store the result. |
+** |
+** If the returned result register is a temporary scalar, then also write |
+** that register number into *piFreeable. If the returned result register |
+** is not a temporary or if the expression is a vector set *piFreeable |
+** to 0. |
+*/ |
+static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){ |
+ int iResult; |
+ int nResult = sqlite3ExprVectorSize(p); |
+ if( nResult==1 ){ |
+ iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable); |
+ }else{ |
+ *piFreeable = 0; |
+ if( p->op==TK_SELECT ){ |
+ iResult = sqlite3CodeSubselect(pParse, p, 0, 0); |
+ }else{ |
+ int i; |
+ iResult = pParse->nMem+1; |
+ pParse->nMem += nResult; |
+ for(i=0; i<nResult; i++){ |
+ sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult); |
+ } |
+ } |
+ } |
+ return iResult; |
+} |
+ |
+ |
+/* |
+** Generate code into the current Vdbe to evaluate the given |
+** expression. Attempt to store the results in register "target". |
+** Return the register where results are stored. |
+** |
+** With this routine, there is no guarantee that results will |
+** be stored in target. The result might be stored in some other |
+** register if it is convenient to do so. The calling function |
+** must check the return code and move the results to the desired |
+** register. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ |
+ Vdbe *v = pParse->pVdbe; /* The VM under construction */ |
+ int op; /* The opcode being coded */ |
+ int inReg = target; /* Results stored in register inReg */ |
+ int regFree1 = 0; /* If non-zero free this temporary register */ |
+ int regFree2 = 0; /* If non-zero free this temporary register */ |
+ int r1, r2; /* Various register numbers */ |
+ Expr tempX; /* Temporary expression node */ |
+ int p5 = 0; |
+ |
+ assert( target>0 && target<=pParse->nMem ); |
+ if( v==0 ){ |
+ assert( pParse->db->mallocFailed ); |
+ return 0; |
+ } |
+ |
+ if( pExpr==0 ){ |
+ op = TK_NULL; |
+ }else{ |
+ op = pExpr->op; |
+ } |
+ switch( op ){ |
+ case TK_AGG_COLUMN: { |
+ AggInfo *pAggInfo = pExpr->pAggInfo; |
+ struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; |
+ if( !pAggInfo->directMode ){ |
+ assert( pCol->iMem>0 ); |
+ return pCol->iMem; |
+ }else if( pAggInfo->useSortingIdx ){ |
+ sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, |
+ pCol->iSorterColumn, target); |
+ return target; |
+ } |
+ /* Otherwise, fall thru into the TK_COLUMN case */ |
+ } |
+ case TK_COLUMN: { |
+ int iTab = pExpr->iTable; |
+ if( iTab<0 ){ |
+ if( pParse->ckBase>0 ){ |
+ /* Generating CHECK constraints or inserting into partial index */ |
+ return pExpr->iColumn + pParse->ckBase; |
+ }else{ |
+ /* Coding an expression that is part of an index where column names |
+ ** in the index refer to the table to which the index belongs */ |
+ iTab = pParse->iSelfTab; |
+ } |
+ } |
+ return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, |
+ pExpr->iColumn, iTab, target, |
+ pExpr->op2); |
+ } |
+ case TK_INTEGER: { |
+ codeInteger(pParse, pExpr, 0, target); |
+ return target; |
+ } |
+#ifndef SQLITE_OMIT_FLOATING_POINT |
+ case TK_FLOAT: { |
+ assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
+ codeReal(v, pExpr->u.zToken, 0, target); |
+ return target; |
+ } |
+#endif |
+ case TK_STRING: { |
+ assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
+ sqlite3VdbeLoadString(v, target, pExpr->u.zToken); |
+ return target; |
+ } |
+ case TK_NULL: { |
+ sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
+ return target; |
+ } |
+#ifndef SQLITE_OMIT_BLOB_LITERAL |
+ case TK_BLOB: { |
+ int n; |
+ const char *z; |
+ char *zBlob; |
+ assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
+ assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); |
+ assert( pExpr->u.zToken[1]=='\'' ); |
+ z = &pExpr->u.zToken[2]; |
+ n = sqlite3Strlen30(z) - 1; |
+ assert( z[n]=='\'' ); |
+ zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); |
+ sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); |
+ return target; |
+ } |
+#endif |
+ case TK_VARIABLE: { |
+ assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
+ assert( pExpr->u.zToken!=0 ); |
+ assert( pExpr->u.zToken[0]!=0 ); |
+ sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target); |
+ if( pExpr->u.zToken[1]!=0 ){ |
+ const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn); |
+ assert( pExpr->u.zToken[0]=='?' || strcmp(pExpr->u.zToken, z)==0 ); |
+ pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */ |
+ sqlite3VdbeAppendP4(v, (char*)z, P4_STATIC); |
+ } |
+ return target; |
+ } |
+ case TK_REGISTER: { |
+ return pExpr->iTable; |
+ } |
+#ifndef SQLITE_OMIT_CAST |
+ case TK_CAST: { |
+ /* Expressions of the form: CAST(pLeft AS token) */ |
+ inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
+ if( inReg!=target ){ |
+ sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); |
+ inReg = target; |
+ } |
+ sqlite3VdbeAddOp2(v, OP_Cast, target, |
+ sqlite3AffinityType(pExpr->u.zToken, 0)); |
+ testcase( usedAsColumnCache(pParse, inReg, inReg) ); |
+ sqlite3ExprCacheAffinityChange(pParse, inReg, 1); |
+ return inReg; |
+ } |
+#endif /* SQLITE_OMIT_CAST */ |
+ case TK_IS: |
+ case TK_ISNOT: |
+ op = (op==TK_IS) ? TK_EQ : TK_NE; |
+ p5 = SQLITE_NULLEQ; |
+ /* fall-through */ |
+ case TK_LT: |
+ case TK_LE: |
+ case TK_GT: |
+ case TK_GE: |
+ case TK_NE: |
+ case TK_EQ: { |
+ Expr *pLeft = pExpr->pLeft; |
+ if( sqlite3ExprIsVector(pLeft) ){ |
+ codeVectorCompare(pParse, pExpr, target, op, p5); |
+ }else{ |
+ r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); |
+ r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
+ codeCompare(pParse, pLeft, pExpr->pRight, op, |
+ r1, r2, inReg, SQLITE_STOREP2 | p5); |
+ assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); |
+ assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); |
+ assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); |
+ assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); |
+ assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); |
+ assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); |
+ testcase( regFree1==0 ); |
+ testcase( regFree2==0 ); |
+ } |
+ break; |
+ } |
+ case TK_AND: |
+ case TK_OR: |
+ case TK_PLUS: |
+ case TK_STAR: |
+ case TK_MINUS: |
+ case TK_REM: |
+ case TK_BITAND: |
+ case TK_BITOR: |
+ case TK_SLASH: |
+ case TK_LSHIFT: |
+ case TK_RSHIFT: |
+ case TK_CONCAT: { |
+ assert( TK_AND==OP_And ); testcase( op==TK_AND ); |
+ assert( TK_OR==OP_Or ); testcase( op==TK_OR ); |
+ assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS ); |
+ assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS ); |
+ assert( TK_REM==OP_Remainder ); testcase( op==TK_REM ); |
+ assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND ); |
+ assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR ); |
+ assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH ); |
+ assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT ); |
+ assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT ); |
+ assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT ); |
+ r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
+ r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
+ sqlite3VdbeAddOp3(v, op, r2, r1, target); |
+ testcase( regFree1==0 ); |
+ testcase( regFree2==0 ); |
+ break; |
+ } |
+ case TK_UMINUS: { |
+ Expr *pLeft = pExpr->pLeft; |
+ assert( pLeft ); |
+ if( pLeft->op==TK_INTEGER ){ |
+ codeInteger(pParse, pLeft, 1, target); |
+ return target; |
+#ifndef SQLITE_OMIT_FLOATING_POINT |
+ }else if( pLeft->op==TK_FLOAT ){ |
+ assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
+ codeReal(v, pLeft->u.zToken, 1, target); |
+ return target; |
+#endif |
+ }else{ |
+ tempX.op = TK_INTEGER; |
+ tempX.flags = EP_IntValue|EP_TokenOnly; |
+ tempX.u.iValue = 0; |
+ r1 = sqlite3ExprCodeTemp(pParse, &tempX, ®Free1); |
+ r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); |
+ sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); |
+ testcase( regFree2==0 ); |
+ } |
+ break; |
+ } |
+ case TK_BITNOT: |
+ case TK_NOT: { |
+ assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT ); |
+ assert( TK_NOT==OP_Not ); testcase( op==TK_NOT ); |
+ r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
+ testcase( regFree1==0 ); |
+ sqlite3VdbeAddOp2(v, op, r1, inReg); |
+ break; |
+ } |
+ case TK_ISNULL: |
+ case TK_NOTNULL: { |
+ int addr; |
+ assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); |
+ assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); |
+ sqlite3VdbeAddOp2(v, OP_Integer, 1, target); |
+ r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
+ testcase( regFree1==0 ); |
+ addr = sqlite3VdbeAddOp1(v, op, r1); |
+ VdbeCoverageIf(v, op==TK_ISNULL); |
+ VdbeCoverageIf(v, op==TK_NOTNULL); |
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, target); |
+ sqlite3VdbeJumpHere(v, addr); |
+ break; |
+ } |
+ case TK_AGG_FUNCTION: { |
+ AggInfo *pInfo = pExpr->pAggInfo; |
+ if( pInfo==0 ){ |
+ assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
+ sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); |
+ }else{ |
+ return pInfo->aFunc[pExpr->iAgg].iMem; |
+ } |
+ break; |
+ } |
+ case TK_FUNCTION: { |
+ ExprList *pFarg; /* List of function arguments */ |
+ int nFarg; /* Number of function arguments */ |
+ FuncDef *pDef; /* The function definition object */ |
+ const char *zId; /* The function name */ |
+ u32 constMask = 0; /* Mask of function arguments that are constant */ |
+ int i; /* Loop counter */ |
+ sqlite3 *db = pParse->db; /* The database connection */ |
+ u8 enc = ENC(db); /* The text encoding used by this database */ |
+ CollSeq *pColl = 0; /* A collating sequence */ |
+ |
+ if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){ |
+ /* SQL functions can be expensive. So try to move constant functions |
+ ** out of the inner loop, even if that means an extra OP_Copy. */ |
+ return sqlite3ExprCodeAtInit(pParse, pExpr, -1); |
+ } |
+ assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
+ if( ExprHasProperty(pExpr, EP_TokenOnly) ){ |
+ pFarg = 0; |
+ }else{ |
+ pFarg = pExpr->x.pList; |
+ } |
+ nFarg = pFarg ? pFarg->nExpr : 0; |
+ assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
+ zId = pExpr->u.zToken; |
+ pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0); |
+#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION |
+ if( pDef==0 && pParse->explain ){ |
+ pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0); |
+ } |
+#endif |
+ if( pDef==0 || pDef->xFinalize!=0 ){ |
+ sqlite3ErrorMsg(pParse, "unknown function: %s()", zId); |
+ break; |
+ } |
+ |
+ /* Attempt a direct implementation of the built-in COALESCE() and |
+ ** IFNULL() functions. This avoids unnecessary evaluation of |
+ ** arguments past the first non-NULL argument. |
+ */ |
+ if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){ |
+ int endCoalesce = sqlite3VdbeMakeLabel(v); |
+ assert( nFarg>=2 ); |
+ sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); |
+ for(i=1; i<nFarg; i++){ |
+ sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce); |
+ VdbeCoverage(v); |
+ sqlite3ExprCacheRemove(pParse, target, 1); |
+ sqlite3ExprCachePush(pParse); |
+ sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target); |
+ sqlite3ExprCachePop(pParse); |
+ } |
+ sqlite3VdbeResolveLabel(v, endCoalesce); |
+ break; |
+ } |
+ |
+ /* The UNLIKELY() function is a no-op. The result is the value |
+ ** of the first argument. |
+ */ |
+ if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ |
+ assert( nFarg>=1 ); |
+ return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target); |
+ } |
+ |
+#ifdef SQLITE_DEBUG |
+ /* The AFFINITY() function evaluates to a string that describes |
+ ** the type affinity of the argument. This is used for testing of |
+ ** the SQLite type logic. |
+ */ |
+ if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){ |
+ const char *azAff[] = { "blob", "text", "numeric", "integer", "real" }; |
+ char aff; |
+ assert( nFarg==1 ); |
+ aff = sqlite3ExprAffinity(pFarg->a[0].pExpr); |
+ sqlite3VdbeLoadString(v, target, |
+ aff ? azAff[aff-SQLITE_AFF_BLOB] : "none"); |
+ return target; |
+ } |
+#endif |
+ |
+ for(i=0; i<nFarg; i++){ |
+ if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){ |
+ testcase( i==31 ); |
+ constMask |= MASKBIT32(i); |
+ } |
+ if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ |
+ pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); |
+ } |
+ } |
+ if( pFarg ){ |
+ if( constMask ){ |
+ r1 = pParse->nMem+1; |
+ pParse->nMem += nFarg; |
+ }else{ |
+ r1 = sqlite3GetTempRange(pParse, nFarg); |
+ } |
+ |
+ /* For length() and typeof() functions with a column argument, |
+ ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG |
+ ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data |
+ ** loading. |
+ */ |
+ if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){ |
+ u8 exprOp; |
+ assert( nFarg==1 ); |
+ assert( pFarg->a[0].pExpr!=0 ); |
+ exprOp = pFarg->a[0].pExpr->op; |
+ if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){ |
+ assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG ); |
+ assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG ); |
+ testcase( pDef->funcFlags & OPFLAG_LENGTHARG ); |
+ pFarg->a[0].pExpr->op2 = |
+ pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG); |
+ } |
+ } |
+ |
+ sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */ |
+ sqlite3ExprCodeExprList(pParse, pFarg, r1, 0, |
+ SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR); |
+ sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */ |
+ }else{ |
+ r1 = 0; |
+ } |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+ /* Possibly overload the function if the first argument is |
+ ** a virtual table column. |
+ ** |
+ ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the |
+ ** second argument, not the first, as the argument to test to |
+ ** see if it is a column in a virtual table. This is done because |
+ ** the left operand of infix functions (the operand we want to |
+ ** control overloading) ends up as the second argument to the |
+ ** function. The expression "A glob B" is equivalent to |
+ ** "glob(B,A). We want to use the A in "A glob B" to test |
+ ** for function overloading. But we use the B term in "glob(B,A)". |
+ */ |
+ if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){ |
+ pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); |
+ }else if( nFarg>0 ){ |
+ pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); |
+ } |
+#endif |
+ if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){ |
+ if( !pColl ) pColl = db->pDfltColl; |
+ sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); |
+ } |
+ sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target, |
+ (char*)pDef, P4_FUNCDEF); |
+ sqlite3VdbeChangeP5(v, (u8)nFarg); |
+ if( nFarg && constMask==0 ){ |
+ sqlite3ReleaseTempRange(pParse, r1, nFarg); |
+ } |
+ return target; |
+ } |
+#ifndef SQLITE_OMIT_SUBQUERY |
+ case TK_EXISTS: |
+ case TK_SELECT: { |
+ int nCol; |
+ testcase( op==TK_EXISTS ); |
+ testcase( op==TK_SELECT ); |
+ if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){ |
+ sqlite3SubselectError(pParse, nCol, 1); |
+ }else{ |
+ return sqlite3CodeSubselect(pParse, pExpr, 0, 0); |
+ } |
+ break; |
+ } |
+ case TK_SELECT_COLUMN: { |
+ int n; |
+ if( pExpr->pLeft->iTable==0 ){ |
+ pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft, 0, 0); |
+ } |
+ assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT ); |
+ if( pExpr->iTable |
+ && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft)) |
+ ){ |
+ sqlite3ErrorMsg(pParse, "%d columns assigned %d values", |
+ pExpr->iTable, n); |
+ } |
+ return pExpr->pLeft->iTable + pExpr->iColumn; |
+ } |
+ case TK_IN: { |
+ int destIfFalse = sqlite3VdbeMakeLabel(v); |
+ int destIfNull = sqlite3VdbeMakeLabel(v); |
+ sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
+ sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); |
+ sqlite3VdbeAddOp2(v, OP_Integer, 1, target); |
+ sqlite3VdbeResolveLabel(v, destIfFalse); |
+ sqlite3VdbeAddOp2(v, OP_AddImm, target, 0); |
+ sqlite3VdbeResolveLabel(v, destIfNull); |
+ return target; |
+ } |
+#endif /* SQLITE_OMIT_SUBQUERY */ |
+ |
+ |
+ /* |
+ ** x BETWEEN y AND z |
+ ** |
+ ** This is equivalent to |
+ ** |
+ ** x>=y AND x<=z |
+ ** |
+ ** X is stored in pExpr->pLeft. |
+ ** Y is stored in pExpr->pList->a[0].pExpr. |
+ ** Z is stored in pExpr->pList->a[1].pExpr. |
+ */ |
+ case TK_BETWEEN: { |
+ exprCodeBetween(pParse, pExpr, target, 0, 0); |
+ return target; |
+ } |
+ case TK_SPAN: |
+ case TK_COLLATE: |
+ case TK_UPLUS: { |
+ return sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
+ } |
+ |
+ case TK_TRIGGER: { |
+ /* If the opcode is TK_TRIGGER, then the expression is a reference |
+ ** to a column in the new.* or old.* pseudo-tables available to |
+ ** trigger programs. In this case Expr.iTable is set to 1 for the |
+ ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn |
+ ** is set to the column of the pseudo-table to read, or to -1 to |
+ ** read the rowid field. |
+ ** |
+ ** The expression is implemented using an OP_Param opcode. The p1 |
+ ** parameter is set to 0 for an old.rowid reference, or to (i+1) |
+ ** to reference another column of the old.* pseudo-table, where |
+ ** i is the index of the column. For a new.rowid reference, p1 is |
+ ** set to (n+1), where n is the number of columns in each pseudo-table. |
+ ** For a reference to any other column in the new.* pseudo-table, p1 |
+ ** is set to (n+2+i), where n and i are as defined previously. For |
+ ** example, if the table on which triggers are being fired is |
+ ** declared as: |
+ ** |
+ ** CREATE TABLE t1(a, b); |
+ ** |
+ ** Then p1 is interpreted as follows: |
+ ** |
+ ** p1==0 -> old.rowid p1==3 -> new.rowid |
+ ** p1==1 -> old.a p1==4 -> new.a |
+ ** p1==2 -> old.b p1==5 -> new.b |
+ */ |
+ Table *pTab = pExpr->pTab; |
+ int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn; |
+ |
+ assert( pExpr->iTable==0 || pExpr->iTable==1 ); |
+ assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol ); |
+ assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey ); |
+ assert( p1>=0 && p1<(pTab->nCol*2+2) ); |
+ |
+ sqlite3VdbeAddOp2(v, OP_Param, p1, target); |
+ VdbeComment((v, "%s.%s -> $%d", |
+ (pExpr->iTable ? "new" : "old"), |
+ (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName), |
+ target |
+ )); |
+ |
+#ifndef SQLITE_OMIT_FLOATING_POINT |
+ /* If the column has REAL affinity, it may currently be stored as an |
+ ** integer. Use OP_RealAffinity to make sure it is really real. |
+ ** |
+ ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to |
+ ** floating point when extracting it from the record. */ |
+ if( pExpr->iColumn>=0 |
+ && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL |
+ ){ |
+ sqlite3VdbeAddOp1(v, OP_RealAffinity, target); |
+ } |
+#endif |
+ break; |
+ } |
+ |
+ case TK_VECTOR: { |
+ sqlite3ErrorMsg(pParse, "row value misused"); |
+ break; |
+ } |
+ |
+ /* |
+ ** Form A: |
+ ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END |
+ ** |
+ ** Form B: |
+ ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END |
+ ** |
+ ** Form A is can be transformed into the equivalent form B as follows: |
+ ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... |
+ ** WHEN x=eN THEN rN ELSE y END |
+ ** |
+ ** X (if it exists) is in pExpr->pLeft. |
+ ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is |
+ ** odd. The Y is also optional. If the number of elements in x.pList |
+ ** is even, then Y is omitted and the "otherwise" result is NULL. |
+ ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. |
+ ** |
+ ** The result of the expression is the Ri for the first matching Ei, |
+ ** or if there is no matching Ei, the ELSE term Y, or if there is |
+ ** no ELSE term, NULL. |
+ */ |
+ default: assert( op==TK_CASE ); { |
+ int endLabel; /* GOTO label for end of CASE stmt */ |
+ int nextCase; /* GOTO label for next WHEN clause */ |
+ int nExpr; /* 2x number of WHEN terms */ |
+ int i; /* Loop counter */ |
+ ExprList *pEList; /* List of WHEN terms */ |
+ struct ExprList_item *aListelem; /* Array of WHEN terms */ |
+ Expr opCompare; /* The X==Ei expression */ |
+ Expr *pX; /* The X expression */ |
+ Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ |
+ VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) |
+ |
+ assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); |
+ assert(pExpr->x.pList->nExpr > 0); |
+ pEList = pExpr->x.pList; |
+ aListelem = pEList->a; |
+ nExpr = pEList->nExpr; |
+ endLabel = sqlite3VdbeMakeLabel(v); |
+ if( (pX = pExpr->pLeft)!=0 ){ |
+ tempX = *pX; |
+ testcase( pX->op==TK_COLUMN ); |
+ exprToRegister(&tempX, exprCodeVector(pParse, &tempX, ®Free1)); |
+ testcase( regFree1==0 ); |
+ memset(&opCompare, 0, sizeof(opCompare)); |
+ opCompare.op = TK_EQ; |
+ opCompare.pLeft = &tempX; |
+ pTest = &opCompare; |
+ /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: |
+ ** The value in regFree1 might get SCopy-ed into the file result. |
+ ** So make sure that the regFree1 register is not reused for other |
+ ** purposes and possibly overwritten. */ |
+ regFree1 = 0; |
+ } |
+ for(i=0; i<nExpr-1; i=i+2){ |
+ sqlite3ExprCachePush(pParse); |
+ if( pX ){ |
+ assert( pTest!=0 ); |
+ opCompare.pRight = aListelem[i].pExpr; |
+ }else{ |
+ pTest = aListelem[i].pExpr; |
+ } |
+ nextCase = sqlite3VdbeMakeLabel(v); |
+ testcase( pTest->op==TK_COLUMN ); |
+ sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); |
+ testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); |
+ sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); |
+ sqlite3VdbeGoto(v, endLabel); |
+ sqlite3ExprCachePop(pParse); |
+ sqlite3VdbeResolveLabel(v, nextCase); |
+ } |
+ if( (nExpr&1)!=0 ){ |
+ sqlite3ExprCachePush(pParse); |
+ sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target); |
+ sqlite3ExprCachePop(pParse); |
+ }else{ |
+ sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
+ } |
+ assert( pParse->db->mallocFailed || pParse->nErr>0 |
+ || pParse->iCacheLevel==iCacheLevel ); |
+ sqlite3VdbeResolveLabel(v, endLabel); |
+ break; |
+ } |
+#ifndef SQLITE_OMIT_TRIGGER |
+ case TK_RAISE: { |
+ assert( pExpr->affinity==OE_Rollback |
+ || pExpr->affinity==OE_Abort |
+ || pExpr->affinity==OE_Fail |
+ || pExpr->affinity==OE_Ignore |
+ ); |
+ if( !pParse->pTriggerTab ){ |
+ sqlite3ErrorMsg(pParse, |
+ "RAISE() may only be used within a trigger-program"); |
+ return 0; |
+ } |
+ if( pExpr->affinity==OE_Abort ){ |
+ sqlite3MayAbort(pParse); |
+ } |
+ assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
+ if( pExpr->affinity==OE_Ignore ){ |
+ sqlite3VdbeAddOp4( |
+ v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); |
+ VdbeCoverage(v); |
+ }else{ |
+ sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER, |
+ pExpr->affinity, pExpr->u.zToken, 0, 0); |
+ } |
+ |
+ break; |
+ } |
+#endif |
+ } |
+ sqlite3ReleaseTempReg(pParse, regFree1); |
+ sqlite3ReleaseTempReg(pParse, regFree2); |
+ return inReg; |
+} |
+ |
+/* |
+** Factor out the code of the given expression to initialization time. |
+** |
+** If regDest>=0 then the result is always stored in that register and the |
+** result is not reusable. If regDest<0 then this routine is free to |
+** store the value whereever it wants. The register where the expression |
+** is stored is returned. When regDest<0, two identical expressions will |
+** code to the same register. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprCodeAtInit( |
+ Parse *pParse, /* Parsing context */ |
+ Expr *pExpr, /* The expression to code when the VDBE initializes */ |
+ int regDest /* Store the value in this register */ |
+){ |
+ ExprList *p; |
+ assert( ConstFactorOk(pParse) ); |
+ p = pParse->pConstExpr; |
+ if( regDest<0 && p ){ |
+ struct ExprList_item *pItem; |
+ int i; |
+ for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ |
+ if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){ |
+ return pItem->u.iConstExprReg; |
+ } |
+ } |
+ } |
+ pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); |
+ p = sqlite3ExprListAppend(pParse, p, pExpr); |
+ if( p ){ |
+ struct ExprList_item *pItem = &p->a[p->nExpr-1]; |
+ pItem->reusable = regDest<0; |
+ if( regDest<0 ) regDest = ++pParse->nMem; |
+ pItem->u.iConstExprReg = regDest; |
+ } |
+ pParse->pConstExpr = p; |
+ return regDest; |
+} |
+ |
+/* |
+** Generate code to evaluate an expression and store the results |
+** into a register. Return the register number where the results |
+** are stored. |
+** |
+** If the register is a temporary register that can be deallocated, |
+** then write its number into *pReg. If the result register is not |
+** a temporary, then set *pReg to zero. |
+** |
+** If pExpr is a constant, then this routine might generate this |
+** code to fill the register in the initialization section of the |
+** VDBE program, in order to factor it out of the evaluation loop. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ |
+ int r2; |
+ pExpr = sqlite3ExprSkipCollate(pExpr); |
+ if( ConstFactorOk(pParse) |
+ && pExpr->op!=TK_REGISTER |
+ && sqlite3ExprIsConstantNotJoin(pExpr) |
+ ){ |
+ *pReg = 0; |
+ r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1); |
+ }else{ |
+ int r1 = sqlite3GetTempReg(pParse); |
+ r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
+ if( r2==r1 ){ |
+ *pReg = r1; |
+ }else{ |
+ sqlite3ReleaseTempReg(pParse, r1); |
+ *pReg = 0; |
+ } |
+ } |
+ return r2; |
+} |
+ |
+/* |
+** Generate code that will evaluate expression pExpr and store the |
+** results in register target. The results are guaranteed to appear |
+** in register target. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ |
+ int inReg; |
+ |
+ assert( target>0 && target<=pParse->nMem ); |
+ if( pExpr && pExpr->op==TK_REGISTER ){ |
+ sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); |
+ }else{ |
+ inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); |
+ assert( pParse->pVdbe!=0 || pParse->db->mallocFailed ); |
+ if( inReg!=target && pParse->pVdbe ){ |
+ sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); |
+ } |
+ } |
+} |
+ |
+/* |
+** Make a transient copy of expression pExpr and then code it using |
+** sqlite3ExprCode(). This routine works just like sqlite3ExprCode() |
+** except that the input expression is guaranteed to be unchanged. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){ |
+ sqlite3 *db = pParse->db; |
+ pExpr = sqlite3ExprDup(db, pExpr, 0); |
+ if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target); |
+ sqlite3ExprDelete(db, pExpr); |
+} |
+ |
+/* |
+** Generate code that will evaluate expression pExpr and store the |
+** results in register target. The results are guaranteed to appear |
+** in register target. If the expression is constant, then this routine |
+** might choose to code the expression at initialization time. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){ |
+ if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){ |
+ sqlite3ExprCodeAtInit(pParse, pExpr, target); |
+ }else{ |
+ sqlite3ExprCode(pParse, pExpr, target); |
+ } |
+} |
+ |
+/* |
+** Generate code that evaluates the given expression and puts the result |
+** in register target. |
+** |
+** Also make a copy of the expression results into another "cache" register |
+** and modify the expression so that the next time it is evaluated, |
+** the result is a copy of the cache register. |
+** |
+** This routine is used for expressions that are used multiple |
+** times. They are evaluated once and the results of the expression |
+** are reused. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ |
+ Vdbe *v = pParse->pVdbe; |
+ int iMem; |
+ |
+ assert( target>0 ); |
+ assert( pExpr->op!=TK_REGISTER ); |
+ sqlite3ExprCode(pParse, pExpr, target); |
+ iMem = ++pParse->nMem; |
+ sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); |
+ exprToRegister(pExpr, iMem); |
+} |
+ |
+/* |
+** Generate code that pushes the value of every element of the given |
+** expression list into a sequence of registers beginning at target. |
+** |
+** Return the number of elements evaluated. |
+** |
+** The SQLITE_ECEL_DUP flag prevents the arguments from being |
+** filled using OP_SCopy. OP_Copy must be used instead. |
+** |
+** The SQLITE_ECEL_FACTOR argument allows constant arguments to be |
+** factored out into initialization code. |
+** |
+** The SQLITE_ECEL_REF flag means that expressions in the list with |
+** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored |
+** in registers at srcReg, and so the value can be copied from there. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprCodeExprList( |
+ Parse *pParse, /* Parsing context */ |
+ ExprList *pList, /* The expression list to be coded */ |
+ int target, /* Where to write results */ |
+ int srcReg, /* Source registers if SQLITE_ECEL_REF */ |
+ u8 flags /* SQLITE_ECEL_* flags */ |
+){ |
+ struct ExprList_item *pItem; |
+ int i, j, n; |
+ u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy; |
+ Vdbe *v = pParse->pVdbe; |
+ assert( pList!=0 ); |
+ assert( target>0 ); |
+ assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ |
+ n = pList->nExpr; |
+ if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; |
+ for(pItem=pList->a, i=0; i<n; i++, pItem++){ |
+ Expr *pExpr = pItem->pExpr; |
+ if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){ |
+ if( flags & SQLITE_ECEL_OMITREF ){ |
+ i--; |
+ n--; |
+ }else{ |
+ sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); |
+ } |
+ }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){ |
+ sqlite3ExprCodeAtInit(pParse, pExpr, target+i); |
+ }else{ |
+ int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); |
+ if( inReg!=target+i ){ |
+ VdbeOp *pOp; |
+ if( copyOp==OP_Copy |
+ && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy |
+ && pOp->p1+pOp->p3+1==inReg |
+ && pOp->p2+pOp->p3+1==target+i |
+ ){ |
+ pOp->p3++; |
+ }else{ |
+ sqlite3VdbeAddOp2(v, copyOp, inReg, target+i); |
+ } |
+ } |
+ } |
+ } |
+ return n; |
+} |
+ |
+/* |
+** Generate code for a BETWEEN operator. |
+** |
+** x BETWEEN y AND z |
+** |
+** The above is equivalent to |
+** |
+** x>=y AND x<=z |
+** |
+** Code it as such, taking care to do the common subexpression |
+** elimination of x. |
+** |
+** The xJumpIf parameter determines details: |
+** |
+** NULL: Store the boolean result in reg[dest] |
+** sqlite3ExprIfTrue: Jump to dest if true |
+** sqlite3ExprIfFalse: Jump to dest if false |
+** |
+** The jumpIfNull parameter is ignored if xJumpIf is NULL. |
+*/ |
+static void exprCodeBetween( |
+ Parse *pParse, /* Parsing and code generating context */ |
+ Expr *pExpr, /* The BETWEEN expression */ |
+ int dest, /* Jump destination or storage location */ |
+ void (*xJump)(Parse*,Expr*,int,int), /* Action to take */ |
+ int jumpIfNull /* Take the jump if the BETWEEN is NULL */ |
+){ |
+ Expr exprAnd; /* The AND operator in x>=y AND x<=z */ |
+ Expr compLeft; /* The x>=y term */ |
+ Expr compRight; /* The x<=z term */ |
+ Expr exprX; /* The x subexpression */ |
+ int regFree1 = 0; /* Temporary use register */ |
+ |
+ |
+ memset(&compLeft, 0, sizeof(Expr)); |
+ memset(&compRight, 0, sizeof(Expr)); |
+ memset(&exprAnd, 0, sizeof(Expr)); |
+ |
+ assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
+ exprX = *pExpr->pLeft; |
+ exprAnd.op = TK_AND; |
+ exprAnd.pLeft = &compLeft; |
+ exprAnd.pRight = &compRight; |
+ compLeft.op = TK_GE; |
+ compLeft.pLeft = &exprX; |
+ compLeft.pRight = pExpr->x.pList->a[0].pExpr; |
+ compRight.op = TK_LE; |
+ compRight.pLeft = &exprX; |
+ compRight.pRight = pExpr->x.pList->a[1].pExpr; |
+ exprToRegister(&exprX, exprCodeVector(pParse, &exprX, ®Free1)); |
+ if( xJump ){ |
+ xJump(pParse, &exprAnd, dest, jumpIfNull); |
+ }else{ |
+ /* Mark the expression is being from the ON or USING clause of a join |
+ ** so that the sqlite3ExprCodeTarget() routine will not attempt to move |
+ ** it into the Parse.pConstExpr list. We should use a new bit for this, |
+ ** for clarity, but we are out of bits in the Expr.flags field so we |
+ ** have to reuse the EP_FromJoin bit. Bummer. */ |
+ exprX.flags |= EP_FromJoin; |
+ sqlite3ExprCodeTarget(pParse, &exprAnd, dest); |
+ } |
+ sqlite3ReleaseTempReg(pParse, regFree1); |
+ |
+ /* Ensure adequate test coverage */ |
+ testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 ); |
+ testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 ); |
+ testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 ); |
+ testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 ); |
+ testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 ); |
+ testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 ); |
+ testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 ); |
+ testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 ); |
+ testcase( xJump==0 ); |
+} |
+ |
+/* |
+** Generate code for a boolean expression such that a jump is made |
+** to the label "dest" if the expression is true but execution |
+** continues straight thru if the expression is false. |
+** |
+** If the expression evaluates to NULL (neither true nor false), then |
+** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. |
+** |
+** This code depends on the fact that certain token values (ex: TK_EQ) |
+** are the same as opcode values (ex: OP_Eq) that implement the corresponding |
+** operation. Special comments in vdbe.c and the mkopcodeh.awk script in |
+** the make process cause these values to align. Assert()s in the code |
+** below verify that the numbers are aligned correctly. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
+ Vdbe *v = pParse->pVdbe; |
+ int op = 0; |
+ int regFree1 = 0; |
+ int regFree2 = 0; |
+ int r1, r2; |
+ |
+ assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); |
+ if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ |
+ if( NEVER(pExpr==0) ) return; /* No way this can happen */ |
+ op = pExpr->op; |
+ switch( op ){ |
+ case TK_AND: { |
+ int d2 = sqlite3VdbeMakeLabel(v); |
+ testcase( jumpIfNull==0 ); |
+ sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); |
+ sqlite3ExprCachePush(pParse); |
+ sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
+ sqlite3VdbeResolveLabel(v, d2); |
+ sqlite3ExprCachePop(pParse); |
+ break; |
+ } |
+ case TK_OR: { |
+ testcase( jumpIfNull==0 ); |
+ sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
+ sqlite3ExprCachePush(pParse); |
+ sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
+ sqlite3ExprCachePop(pParse); |
+ break; |
+ } |
+ case TK_NOT: { |
+ testcase( jumpIfNull==0 ); |
+ sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
+ break; |
+ } |
+ case TK_IS: |
+ case TK_ISNOT: |
+ testcase( op==TK_IS ); |
+ testcase( op==TK_ISNOT ); |
+ op = (op==TK_IS) ? TK_EQ : TK_NE; |
+ jumpIfNull = SQLITE_NULLEQ; |
+ /* Fall thru */ |
+ case TK_LT: |
+ case TK_LE: |
+ case TK_GT: |
+ case TK_GE: |
+ case TK_NE: |
+ case TK_EQ: { |
+ if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr; |
+ testcase( jumpIfNull==0 ); |
+ r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
+ r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
+ codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
+ r1, r2, dest, jumpIfNull); |
+ assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); |
+ assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); |
+ assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); |
+ assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); |
+ assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); |
+ VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); |
+ VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); |
+ assert(TK_NE==OP_Ne); testcase(op==OP_Ne); |
+ VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); |
+ VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); |
+ testcase( regFree1==0 ); |
+ testcase( regFree2==0 ); |
+ break; |
+ } |
+ case TK_ISNULL: |
+ case TK_NOTNULL: { |
+ assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); |
+ assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); |
+ r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
+ sqlite3VdbeAddOp2(v, op, r1, dest); |
+ VdbeCoverageIf(v, op==TK_ISNULL); |
+ VdbeCoverageIf(v, op==TK_NOTNULL); |
+ testcase( regFree1==0 ); |
+ break; |
+ } |
+ case TK_BETWEEN: { |
+ testcase( jumpIfNull==0 ); |
+ exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull); |
+ break; |
+ } |
+#ifndef SQLITE_OMIT_SUBQUERY |
+ case TK_IN: { |
+ int destIfFalse = sqlite3VdbeMakeLabel(v); |
+ int destIfNull = jumpIfNull ? dest : destIfFalse; |
+ sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); |
+ sqlite3VdbeGoto(v, dest); |
+ sqlite3VdbeResolveLabel(v, destIfFalse); |
+ break; |
+ } |
+#endif |
+ default: { |
+ default_expr: |
+ if( exprAlwaysTrue(pExpr) ){ |
+ sqlite3VdbeGoto(v, dest); |
+ }else if( exprAlwaysFalse(pExpr) ){ |
+ /* No-op */ |
+ }else{ |
+ r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); |
+ sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); |
+ VdbeCoverage(v); |
+ testcase( regFree1==0 ); |
+ testcase( jumpIfNull==0 ); |
+ } |
+ break; |
+ } |
+ } |
+ sqlite3ReleaseTempReg(pParse, regFree1); |
+ sqlite3ReleaseTempReg(pParse, regFree2); |
+} |
+ |
+/* |
+** Generate code for a boolean expression such that a jump is made |
+** to the label "dest" if the expression is false but execution |
+** continues straight thru if the expression is true. |
+** |
+** If the expression evaluates to NULL (neither true nor false) then |
+** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull |
+** is 0. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
+ Vdbe *v = pParse->pVdbe; |
+ int op = 0; |
+ int regFree1 = 0; |
+ int regFree2 = 0; |
+ int r1, r2; |
+ |
+ assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); |
+ if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ |
+ if( pExpr==0 ) return; |
+ |
+ /* The value of pExpr->op and op are related as follows: |
+ ** |
+ ** pExpr->op op |
+ ** --------- ---------- |
+ ** TK_ISNULL OP_NotNull |
+ ** TK_NOTNULL OP_IsNull |
+ ** TK_NE OP_Eq |
+ ** TK_EQ OP_Ne |
+ ** TK_GT OP_Le |
+ ** TK_LE OP_Gt |
+ ** TK_GE OP_Lt |
+ ** TK_LT OP_Ge |
+ ** |
+ ** For other values of pExpr->op, op is undefined and unused. |
+ ** The value of TK_ and OP_ constants are arranged such that we |
+ ** can compute the mapping above using the following expression. |
+ ** Assert()s verify that the computation is correct. |
+ */ |
+ op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); |
+ |
+ /* Verify correct alignment of TK_ and OP_ constants |
+ */ |
+ assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); |
+ assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); |
+ assert( pExpr->op!=TK_NE || op==OP_Eq ); |
+ assert( pExpr->op!=TK_EQ || op==OP_Ne ); |
+ assert( pExpr->op!=TK_LT || op==OP_Ge ); |
+ assert( pExpr->op!=TK_LE || op==OP_Gt ); |
+ assert( pExpr->op!=TK_GT || op==OP_Le ); |
+ assert( pExpr->op!=TK_GE || op==OP_Lt ); |
+ |
+ switch( pExpr->op ){ |
+ case TK_AND: { |
+ testcase( jumpIfNull==0 ); |
+ sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
+ sqlite3ExprCachePush(pParse); |
+ sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
+ sqlite3ExprCachePop(pParse); |
+ break; |
+ } |
+ case TK_OR: { |
+ int d2 = sqlite3VdbeMakeLabel(v); |
+ testcase( jumpIfNull==0 ); |
+ sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); |
+ sqlite3ExprCachePush(pParse); |
+ sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
+ sqlite3VdbeResolveLabel(v, d2); |
+ sqlite3ExprCachePop(pParse); |
+ break; |
+ } |
+ case TK_NOT: { |
+ testcase( jumpIfNull==0 ); |
+ sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
+ break; |
+ } |
+ case TK_IS: |
+ case TK_ISNOT: |
+ testcase( pExpr->op==TK_IS ); |
+ testcase( pExpr->op==TK_ISNOT ); |
+ op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; |
+ jumpIfNull = SQLITE_NULLEQ; |
+ /* Fall thru */ |
+ case TK_LT: |
+ case TK_LE: |
+ case TK_GT: |
+ case TK_GE: |
+ case TK_NE: |
+ case TK_EQ: { |
+ if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr; |
+ testcase( jumpIfNull==0 ); |
+ r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
+ r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
+ codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
+ r1, r2, dest, jumpIfNull); |
+ assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); |
+ assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); |
+ assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); |
+ assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); |
+ assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); |
+ VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); |
+ VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); |
+ assert(TK_NE==OP_Ne); testcase(op==OP_Ne); |
+ VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); |
+ VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); |
+ testcase( regFree1==0 ); |
+ testcase( regFree2==0 ); |
+ break; |
+ } |
+ case TK_ISNULL: |
+ case TK_NOTNULL: { |
+ r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
+ sqlite3VdbeAddOp2(v, op, r1, dest); |
+ testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL); |
+ testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL); |
+ testcase( regFree1==0 ); |
+ break; |
+ } |
+ case TK_BETWEEN: { |
+ testcase( jumpIfNull==0 ); |
+ exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull); |
+ break; |
+ } |
+#ifndef SQLITE_OMIT_SUBQUERY |
+ case TK_IN: { |
+ if( jumpIfNull ){ |
+ sqlite3ExprCodeIN(pParse, pExpr, dest, dest); |
+ }else{ |
+ int destIfNull = sqlite3VdbeMakeLabel(v); |
+ sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); |
+ sqlite3VdbeResolveLabel(v, destIfNull); |
+ } |
+ break; |
+ } |
+#endif |
+ default: { |
+ default_expr: |
+ if( exprAlwaysFalse(pExpr) ){ |
+ sqlite3VdbeGoto(v, dest); |
+ }else if( exprAlwaysTrue(pExpr) ){ |
+ /* no-op */ |
+ }else{ |
+ r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); |
+ sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); |
+ VdbeCoverage(v); |
+ testcase( regFree1==0 ); |
+ testcase( jumpIfNull==0 ); |
+ } |
+ break; |
+ } |
+ } |
+ sqlite3ReleaseTempReg(pParse, regFree1); |
+ sqlite3ReleaseTempReg(pParse, regFree2); |
+} |
+ |
+/* |
+** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before |
+** code generation, and that copy is deleted after code generation. This |
+** ensures that the original pExpr is unchanged. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){ |
+ sqlite3 *db = pParse->db; |
+ Expr *pCopy = sqlite3ExprDup(db, pExpr, 0); |
+ if( db->mallocFailed==0 ){ |
+ sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull); |
+ } |
+ sqlite3ExprDelete(db, pCopy); |
+} |
+ |
+ |
+/* |
+** Do a deep comparison of two expression trees. Return 0 if the two |
+** expressions are completely identical. Return 1 if they differ only |
+** by a COLLATE operator at the top level. Return 2 if there are differences |
+** other than the top-level COLLATE operator. |
+** |
+** If any subelement of pB has Expr.iTable==(-1) then it is allowed |
+** to compare equal to an equivalent element in pA with Expr.iTable==iTab. |
+** |
+** The pA side might be using TK_REGISTER. If that is the case and pB is |
+** not using TK_REGISTER but is otherwise equivalent, then still return 0. |
+** |
+** Sometimes this routine will return 2 even if the two expressions |
+** really are equivalent. If we cannot prove that the expressions are |
+** identical, we return 2 just to be safe. So if this routine |
+** returns 2, then you do not really know for certain if the two |
+** expressions are the same. But if you get a 0 or 1 return, then you |
+** can be sure the expressions are the same. In the places where |
+** this routine is used, it does not hurt to get an extra 2 - that |
+** just might result in some slightly slower code. But returning |
+** an incorrect 0 or 1 could lead to a malfunction. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){ |
+ u32 combinedFlags; |
+ if( pA==0 || pB==0 ){ |
+ return pB==pA ? 0 : 2; |
+ } |
+ combinedFlags = pA->flags | pB->flags; |
+ if( combinedFlags & EP_IntValue ){ |
+ if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){ |
+ return 0; |
+ } |
+ return 2; |
+ } |
+ if( pA->op!=pB->op ){ |
+ if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){ |
+ return 1; |
+ } |
+ if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){ |
+ return 1; |
+ } |
+ return 2; |
+ } |
+ if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){ |
+ if( pA->op==TK_FUNCTION ){ |
+ if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; |
+ }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ |
+ return pA->op==TK_COLLATE ? 1 : 2; |
+ } |
+ } |
+ if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; |
+ if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ |
+ if( combinedFlags & EP_xIsSelect ) return 2; |
+ if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2; |
+ if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2; |
+ if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; |
+ if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){ |
+ if( pA->iColumn!=pB->iColumn ) return 2; |
+ if( pA->iTable!=pB->iTable |
+ && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; |
+ } |
+ } |
+ return 0; |
+} |
+ |
+/* |
+** Compare two ExprList objects. Return 0 if they are identical and |
+** non-zero if they differ in any way. |
+** |
+** If any subelement of pB has Expr.iTable==(-1) then it is allowed |
+** to compare equal to an equivalent element in pA with Expr.iTable==iTab. |
+** |
+** This routine might return non-zero for equivalent ExprLists. The |
+** only consequence will be disabled optimizations. But this routine |
+** must never return 0 if the two ExprList objects are different, or |
+** a malfunction will result. |
+** |
+** Two NULL pointers are considered to be the same. But a NULL pointer |
+** always differs from a non-NULL pointer. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){ |
+ int i; |
+ if( pA==0 && pB==0 ) return 0; |
+ if( pA==0 || pB==0 ) return 1; |
+ if( pA->nExpr!=pB->nExpr ) return 1; |
+ for(i=0; i<pA->nExpr; i++){ |
+ Expr *pExprA = pA->a[i].pExpr; |
+ Expr *pExprB = pB->a[i].pExpr; |
+ if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1; |
+ if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1; |
+ } |
+ return 0; |
+} |
+ |
+/* |
+** Return true if we can prove the pE2 will always be true if pE1 is |
+** true. Return false if we cannot complete the proof or if pE2 might |
+** be false. Examples: |
+** |
+** pE1: x==5 pE2: x==5 Result: true |
+** pE1: x>0 pE2: x==5 Result: false |
+** pE1: x=21 pE2: x=21 OR y=43 Result: true |
+** pE1: x!=123 pE2: x IS NOT NULL Result: true |
+** pE1: x!=?1 pE2: x IS NOT NULL Result: true |
+** pE1: x IS NULL pE2: x IS NOT NULL Result: false |
+** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false |
+** |
+** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has |
+** Expr.iTable<0 then assume a table number given by iTab. |
+** |
+** When in doubt, return false. Returning true might give a performance |
+** improvement. Returning false might cause a performance reduction, but |
+** it will always give the correct answer and is hence always safe. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){ |
+ if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){ |
+ return 1; |
+ } |
+ if( pE2->op==TK_OR |
+ && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab) |
+ || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) ) |
+ ){ |
+ return 1; |
+ } |
+ if( pE2->op==TK_NOTNULL && pE1->op!=TK_ISNULL && pE1->op!=TK_IS ){ |
+ Expr *pX = sqlite3ExprSkipCollate(pE1->pLeft); |
+ testcase( pX!=pE1->pLeft ); |
+ if( sqlite3ExprCompare(pX, pE2->pLeft, iTab)==0 ) return 1; |
+ } |
+ return 0; |
+} |
+ |
+/* |
+** An instance of the following structure is used by the tree walker |
+** to determine if an expression can be evaluated by reference to the |
+** index only, without having to do a search for the corresponding |
+** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur |
+** is the cursor for the table. |
+*/ |
+struct IdxCover { |
+ Index *pIdx; /* The index to be tested for coverage */ |
+ int iCur; /* Cursor number for the table corresponding to the index */ |
+}; |
+ |
+/* |
+** Check to see if there are references to columns in table |
+** pWalker->u.pIdxCover->iCur can be satisfied using the index |
+** pWalker->u.pIdxCover->pIdx. |
+*/ |
+static int exprIdxCover(Walker *pWalker, Expr *pExpr){ |
+ if( pExpr->op==TK_COLUMN |
+ && pExpr->iTable==pWalker->u.pIdxCover->iCur |
+ && sqlite3ColumnOfIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0 |
+ ){ |
+ pWalker->eCode = 1; |
+ return WRC_Abort; |
+ } |
+ return WRC_Continue; |
+} |
+ |
+/* |
+** Determine if an index pIdx on table with cursor iCur contains will |
+** the expression pExpr. Return true if the index does cover the |
+** expression and false if the pExpr expression references table columns |
+** that are not found in the index pIdx. |
+** |
+** An index covering an expression means that the expression can be |
+** evaluated using only the index and without having to lookup the |
+** corresponding table entry. |
+*/ |
+SQLITE_PRIVATE int sqlite3ExprCoveredByIndex( |
+ Expr *pExpr, /* The index to be tested */ |
+ int iCur, /* The cursor number for the corresponding table */ |
+ Index *pIdx /* The index that might be used for coverage */ |
+){ |
+ Walker w; |
+ struct IdxCover xcov; |
+ memset(&w, 0, sizeof(w)); |
+ xcov.iCur = iCur; |
+ xcov.pIdx = pIdx; |
+ w.xExprCallback = exprIdxCover; |
+ w.u.pIdxCover = &xcov; |
+ sqlite3WalkExpr(&w, pExpr); |
+ return !w.eCode; |
+} |
+ |
+ |
+/* |
+** An instance of the following structure is used by the tree walker |
+** to count references to table columns in the arguments of an |
+** aggregate function, in order to implement the |
+** sqlite3FunctionThisSrc() routine. |
+*/ |
+struct SrcCount { |
+ SrcList *pSrc; /* One particular FROM clause in a nested query */ |
+ int nThis; /* Number of references to columns in pSrcList */ |
+ int nOther; /* Number of references to columns in other FROM clauses */ |
+}; |
+ |
+/* |
+** Count the number of references to columns. |
+*/ |
+static int exprSrcCount(Walker *pWalker, Expr *pExpr){ |
+ /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc() |
+ ** is always called before sqlite3ExprAnalyzeAggregates() and so the |
+ ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If |
+ ** sqlite3FunctionUsesThisSrc() is used differently in the future, the |
+ ** NEVER() will need to be removed. */ |
+ if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){ |
+ int i; |
+ struct SrcCount *p = pWalker->u.pSrcCount; |
+ SrcList *pSrc = p->pSrc; |
+ int nSrc = pSrc ? pSrc->nSrc : 0; |
+ for(i=0; i<nSrc; i++){ |
+ if( pExpr->iTable==pSrc->a[i].iCursor ) break; |
+ } |
+ if( i<nSrc ){ |
+ p->nThis++; |
+ }else{ |
+ p->nOther++; |
+ } |
+ } |
+ return WRC_Continue; |
+} |
+ |
+/* |
+** Determine if any of the arguments to the pExpr Function reference |
+** pSrcList. Return true if they do. Also return true if the function |
+** has no arguments or has only constant arguments. Return false if pExpr |
+** references columns but not columns of tables found in pSrcList. |
+*/ |
+SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){ |
+ Walker w; |
+ struct SrcCount cnt; |
+ assert( pExpr->op==TK_AGG_FUNCTION ); |
+ memset(&w, 0, sizeof(w)); |
+ w.xExprCallback = exprSrcCount; |
+ w.u.pSrcCount = &cnt; |
+ cnt.pSrc = pSrcList; |
+ cnt.nThis = 0; |
+ cnt.nOther = 0; |
+ sqlite3WalkExprList(&w, pExpr->x.pList); |
+ return cnt.nThis>0 || cnt.nOther==0; |
+} |
+ |
+/* |
+** Add a new element to the pAggInfo->aCol[] array. Return the index of |
+** the new element. Return a negative number if malloc fails. |
+*/ |
+static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ |
+ int i; |
+ pInfo->aCol = sqlite3ArrayAllocate( |
+ db, |
+ pInfo->aCol, |
+ sizeof(pInfo->aCol[0]), |
+ &pInfo->nColumn, |
+ &i |
+ ); |
+ return i; |
+} |
+ |
+/* |
+** Add a new element to the pAggInfo->aFunc[] array. Return the index of |
+** the new element. Return a negative number if malloc fails. |
+*/ |
+static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ |
+ int i; |
+ pInfo->aFunc = sqlite3ArrayAllocate( |
+ db, |
+ pInfo->aFunc, |
+ sizeof(pInfo->aFunc[0]), |
+ &pInfo->nFunc, |
+ &i |
+ ); |
+ return i; |
+} |
+ |
+/* |
+** This is the xExprCallback for a tree walker. It is used to |
+** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates |
+** for additional information. |
+*/ |
+static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ |
+ int i; |
+ NameContext *pNC = pWalker->u.pNC; |
+ Parse *pParse = pNC->pParse; |
+ SrcList *pSrcList = pNC->pSrcList; |
+ AggInfo *pAggInfo = pNC->pAggInfo; |
+ |
+ switch( pExpr->op ){ |
+ case TK_AGG_COLUMN: |
+ case TK_COLUMN: { |
+ testcase( pExpr->op==TK_AGG_COLUMN ); |
+ testcase( pExpr->op==TK_COLUMN ); |
+ /* Check to see if the column is in one of the tables in the FROM |
+ ** clause of the aggregate query */ |
+ if( ALWAYS(pSrcList!=0) ){ |
+ struct SrcList_item *pItem = pSrcList->a; |
+ for(i=0; i<pSrcList->nSrc; i++, pItem++){ |
+ struct AggInfo_col *pCol; |
+ assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); |
+ if( pExpr->iTable==pItem->iCursor ){ |
+ /* If we reach this point, it means that pExpr refers to a table |
+ ** that is in the FROM clause of the aggregate query. |
+ ** |
+ ** Make an entry for the column in pAggInfo->aCol[] if there |
+ ** is not an entry there already. |
+ */ |
+ int k; |
+ pCol = pAggInfo->aCol; |
+ for(k=0; k<pAggInfo->nColumn; k++, pCol++){ |
+ if( pCol->iTable==pExpr->iTable && |
+ pCol->iColumn==pExpr->iColumn ){ |
+ break; |
+ } |
+ } |
+ if( (k>=pAggInfo->nColumn) |
+ && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 |
+ ){ |
+ pCol = &pAggInfo->aCol[k]; |
+ pCol->pTab = pExpr->pTab; |
+ pCol->iTable = pExpr->iTable; |
+ pCol->iColumn = pExpr->iColumn; |
+ pCol->iMem = ++pParse->nMem; |
+ pCol->iSorterColumn = -1; |
+ pCol->pExpr = pExpr; |
+ if( pAggInfo->pGroupBy ){ |
+ int j, n; |
+ ExprList *pGB = pAggInfo->pGroupBy; |
+ struct ExprList_item *pTerm = pGB->a; |
+ n = pGB->nExpr; |
+ for(j=0; j<n; j++, pTerm++){ |
+ Expr *pE = pTerm->pExpr; |
+ if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && |
+ pE->iColumn==pExpr->iColumn ){ |
+ pCol->iSorterColumn = j; |
+ break; |
+ } |
+ } |
+ } |
+ if( pCol->iSorterColumn<0 ){ |
+ pCol->iSorterColumn = pAggInfo->nSortingColumn++; |
+ } |
+ } |
+ /* There is now an entry for pExpr in pAggInfo->aCol[] (either |
+ ** because it was there before or because we just created it). |
+ ** Convert the pExpr to be a TK_AGG_COLUMN referring to that |
+ ** pAggInfo->aCol[] entry. |
+ */ |
+ ExprSetVVAProperty(pExpr, EP_NoReduce); |
+ pExpr->pAggInfo = pAggInfo; |
+ pExpr->op = TK_AGG_COLUMN; |
+ pExpr->iAgg = (i16)k; |
+ break; |
+ } /* endif pExpr->iTable==pItem->iCursor */ |
+ } /* end loop over pSrcList */ |
+ } |
+ return WRC_Prune; |
+ } |
+ case TK_AGG_FUNCTION: { |
+ if( (pNC->ncFlags & NC_InAggFunc)==0 |
+ && pWalker->walkerDepth==pExpr->op2 |
+ ){ |
+ /* Check to see if pExpr is a duplicate of another aggregate |
+ ** function that is already in the pAggInfo structure |
+ */ |
+ struct AggInfo_func *pItem = pAggInfo->aFunc; |
+ for(i=0; i<pAggInfo->nFunc; i++, pItem++){ |
+ if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){ |
+ break; |
+ } |
+ } |
+ if( i>=pAggInfo->nFunc ){ |
+ /* pExpr is original. Make a new entry in pAggInfo->aFunc[] |
+ */ |
+ u8 enc = ENC(pParse->db); |
+ i = addAggInfoFunc(pParse->db, pAggInfo); |
+ if( i>=0 ){ |
+ assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
+ pItem = &pAggInfo->aFunc[i]; |
+ pItem->pExpr = pExpr; |
+ pItem->iMem = ++pParse->nMem; |
+ assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
+ pItem->pFunc = sqlite3FindFunction(pParse->db, |
+ pExpr->u.zToken, |
+ pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0); |
+ if( pExpr->flags & EP_Distinct ){ |
+ pItem->iDistinct = pParse->nTab++; |
+ }else{ |
+ pItem->iDistinct = -1; |
+ } |
+ } |
+ } |
+ /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry |
+ */ |
+ assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); |
+ ExprSetVVAProperty(pExpr, EP_NoReduce); |
+ pExpr->iAgg = (i16)i; |
+ pExpr->pAggInfo = pAggInfo; |
+ return WRC_Prune; |
+ }else{ |
+ return WRC_Continue; |
+ } |
+ } |
+ } |
+ return WRC_Continue; |
+} |
+static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ |
+ UNUSED_PARAMETER(pWalker); |
+ UNUSED_PARAMETER(pSelect); |
+ return WRC_Continue; |
+} |
+ |
+/* |
+** Analyze the pExpr expression looking for aggregate functions and |
+** for variables that need to be added to AggInfo object that pNC->pAggInfo |
+** points to. Additional entries are made on the AggInfo object as |
+** necessary. |
+** |
+** This routine should only be called after the expression has been |
+** analyzed by sqlite3ResolveExprNames(). |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ |
+ Walker w; |
+ memset(&w, 0, sizeof(w)); |
+ w.xExprCallback = analyzeAggregate; |
+ w.xSelectCallback = analyzeAggregatesInSelect; |
+ w.u.pNC = pNC; |
+ assert( pNC->pSrcList!=0 ); |
+ sqlite3WalkExpr(&w, pExpr); |
+} |
+ |
+/* |
+** Call sqlite3ExprAnalyzeAggregates() for every expression in an |
+** expression list. Return the number of errors. |
+** |
+** If an error is found, the analysis is cut short. |
+*/ |
+SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ |
+ struct ExprList_item *pItem; |
+ int i; |
+ if( pList ){ |
+ for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ |
+ sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); |
+ } |
+ } |
+} |
+ |
+/* |
+** Allocate a single new register for use to hold some intermediate result. |
+*/ |
+SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){ |
+ if( pParse->nTempReg==0 ){ |
+ return ++pParse->nMem; |
+ } |
+ return pParse->aTempReg[--pParse->nTempReg]; |
+} |
+ |
+/* |
+** Deallocate a register, making available for reuse for some other |
+** purpose. |
+** |
+** If a register is currently being used by the column cache, then |
+** the deallocation is deferred until the column cache line that uses |
+** the register becomes stale. |
+*/ |
+SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ |
+ if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ |
+ int i; |
+ struct yColCache *p; |
+ for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ |
+ if( p->iReg==iReg ){ |
+ p->tempReg = 1; |
+ return; |
+ } |
+ } |
+ pParse->aTempReg[pParse->nTempReg++] = iReg; |
+ } |
+} |
+ |
+/* |
+** Allocate or deallocate a block of nReg consecutive registers. |
+*/ |
+SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){ |
+ int i, n; |
+ if( nReg==1 ) return sqlite3GetTempReg(pParse); |
+ i = pParse->iRangeReg; |
+ n = pParse->nRangeReg; |
+ if( nReg<=n ){ |
+ assert( !usedAsColumnCache(pParse, i, i+n-1) ); |
+ pParse->iRangeReg += nReg; |
+ pParse->nRangeReg -= nReg; |
+ }else{ |
+ i = pParse->nMem+1; |
+ pParse->nMem += nReg; |
+ } |
+ return i; |
+} |
+SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ |
+ if( nReg==1 ){ |
+ sqlite3ReleaseTempReg(pParse, iReg); |
+ return; |
+ } |
+ sqlite3ExprCacheRemove(pParse, iReg, nReg); |
+ if( nReg>pParse->nRangeReg ){ |
+ pParse->nRangeReg = nReg; |
+ pParse->iRangeReg = iReg; |
+ } |
+} |
+ |
+/* |
+** Mark all temporary registers as being unavailable for reuse. |
+*/ |
+SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){ |
+ pParse->nTempReg = 0; |
+ pParse->nRangeReg = 0; |
+} |
+ |
+/* |
+** Validate that no temporary register falls within the range of |
+** iFirst..iLast, inclusive. This routine is only call from within assert() |
+** statements. |
+*/ |
+#ifdef SQLITE_DEBUG |
+SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){ |
+ int i; |
+ if( pParse->nRangeReg>0 |
+ && pParse->iRangeReg+pParse->nRangeReg<iLast |
+ && pParse->iRangeReg>=iFirst |
+ ){ |
+ return 0; |
+ } |
+ for(i=0; i<pParse->nTempReg; i++){ |
+ if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){ |
+ return 0; |
+ } |
+ } |
+ return 1; |
+} |
+#endif /* SQLITE_DEBUG */ |
+ |
+/************** End of expr.c ************************************************/ |
+/************** Begin file alter.c *******************************************/ |
+/* |
+** 2005 February 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. |
+** |
+************************************************************************* |
+** This file contains C code routines that used to generate VDBE code |
+** that implements the ALTER TABLE command. |
+*/ |
+/* #include "sqliteInt.h" */ |
+ |
+/* |
+** The code in this file only exists if we are not omitting the |
+** ALTER TABLE logic from the build. |
+*/ |
+#ifndef SQLITE_OMIT_ALTERTABLE |
+ |
+ |
+/* |
+** This function is used by SQL generated to implement the |
+** ALTER TABLE command. The first argument is the text of a CREATE TABLE or |
+** CREATE INDEX command. The second is a table name. The table name in |
+** the CREATE TABLE or CREATE INDEX statement is replaced with the third |
+** argument and the result returned. Examples: |
+** |
+** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def') |
+** -> 'CREATE TABLE def(a, b, c)' |
+** |
+** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def') |
+** -> 'CREATE INDEX i ON def(a, b, c)' |
+*/ |
+static void renameTableFunc( |
+ sqlite3_context *context, |
+ int NotUsed, |
+ sqlite3_value **argv |
+){ |
+ unsigned char const *zSql = sqlite3_value_text(argv[0]); |
+ unsigned char const *zTableName = sqlite3_value_text(argv[1]); |
+ |
+ int token; |
+ Token tname; |
+ unsigned char const *zCsr = zSql; |
+ int len = 0; |
+ char *zRet; |
+ |
+ sqlite3 *db = sqlite3_context_db_handle(context); |
+ |
+ UNUSED_PARAMETER(NotUsed); |
+ |
+ /* The principle used to locate the table name in the CREATE TABLE |
+ ** statement is that the table name is the first non-space token that |
+ ** is immediately followed by a TK_LP or TK_USING token. |
+ */ |
+ if( zSql ){ |
+ do { |
+ if( !*zCsr ){ |
+ /* Ran out of input before finding an opening bracket. Return NULL. */ |
+ return; |
+ } |
+ |
+ /* Store the token that zCsr points to in tname. */ |
+ tname.z = (char*)zCsr; |
+ tname.n = len; |
+ |
+ /* Advance zCsr to the next token. Store that token type in 'token', |
+ ** and its length in 'len' (to be used next iteration of this loop). |
+ */ |
+ do { |
+ zCsr += len; |
+ len = sqlite3GetToken(zCsr, &token); |
+ } while( token==TK_SPACE ); |
+ assert( len>0 ); |
+ } while( token!=TK_LP && token!=TK_USING ); |
+ |
+ zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql), |
+ zSql, zTableName, tname.z+tname.n); |
+ sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); |
+ } |
+} |
+ |
+/* |
+** This C function implements an SQL user function that is used by SQL code |
+** generated by the ALTER TABLE ... RENAME command to modify the definition |
+** of any foreign key constraints that use the table being renamed as the |
+** parent table. It is passed three arguments: |
+** |
+** 1) The complete text of the CREATE TABLE statement being modified, |
+** 2) The old name of the table being renamed, and |
+** 3) The new name of the table being renamed. |
+** |
+** It returns the new CREATE TABLE statement. For example: |
+** |
+** sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3') |
+** -> 'CREATE TABLE t1(a REFERENCES t3)' |
+*/ |
+#ifndef SQLITE_OMIT_FOREIGN_KEY |
+static void renameParentFunc( |
+ sqlite3_context *context, |
+ int NotUsed, |
+ sqlite3_value **argv |
+){ |
+ sqlite3 *db = sqlite3_context_db_handle(context); |
+ char *zOutput = 0; |
+ char *zResult; |
+ unsigned char const *zInput = sqlite3_value_text(argv[0]); |
+ unsigned char const *zOld = sqlite3_value_text(argv[1]); |
+ unsigned char const *zNew = sqlite3_value_text(argv[2]); |
+ |
+ unsigned const char *z; /* Pointer to token */ |
+ int n; /* Length of token z */ |
+ int token; /* Type of token */ |
+ |
+ UNUSED_PARAMETER(NotUsed); |
+ if( zInput==0 || zOld==0 ) return; |
+ for(z=zInput; *z; z=z+n){ |
+ n = sqlite3GetToken(z, &token); |
+ if( token==TK_REFERENCES ){ |
+ char *zParent; |
+ do { |
+ z += n; |
+ n = sqlite3GetToken(z, &token); |
+ }while( token==TK_SPACE ); |
+ |
+ if( token==TK_ILLEGAL ) break; |
+ zParent = sqlite3DbStrNDup(db, (const char *)z, n); |
+ if( zParent==0 ) break; |
+ sqlite3Dequote(zParent); |
+ if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){ |
+ char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", |
+ (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew |
+ ); |
+ sqlite3DbFree(db, zOutput); |
+ zOutput = zOut; |
+ zInput = &z[n]; |
+ } |
+ sqlite3DbFree(db, zParent); |
+ } |
+ } |
+ |
+ zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), |
+ sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC); |
+ sqlite3DbFree(db, zOutput); |
+} |
+#endif |
+ |
+#ifndef SQLITE_OMIT_TRIGGER |
+/* This function is used by SQL generated to implement the |
+** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER |
+** statement. The second is a table name. The table name in the CREATE |
+** TRIGGER statement is replaced with the third argument and the result |
+** returned. This is analagous to renameTableFunc() above, except for CREATE |
+** TRIGGER, not CREATE INDEX and CREATE TABLE. |
+*/ |
+static void renameTriggerFunc( |
+ sqlite3_context *context, |
+ int NotUsed, |
+ sqlite3_value **argv |
+){ |
+ unsigned char const *zSql = sqlite3_value_text(argv[0]); |
+ unsigned char const *zTableName = sqlite3_value_text(argv[1]); |
+ |
+ int token; |
+ Token tname; |
+ int dist = 3; |
+ unsigned char const *zCsr = zSql; |
+ int len = 0; |
+ char *zRet; |
+ sqlite3 *db = sqlite3_context_db_handle(context); |
+ |
+ UNUSED_PARAMETER(NotUsed); |
+ |
+ /* The principle used to locate the table name in the CREATE TRIGGER |
+ ** statement is that the table name is the first token that is immediately |
+ ** preceded by either TK_ON or TK_DOT and immediately followed by one |
+ ** of TK_WHEN, TK_BEGIN or TK_FOR. |
+ */ |
+ if( zSql ){ |
+ do { |
+ |
+ if( !*zCsr ){ |
+ /* Ran out of input before finding the table name. Return NULL. */ |
+ return; |
+ } |
+ |
+ /* Store the token that zCsr points to in tname. */ |
+ tname.z = (char*)zCsr; |
+ tname.n = len; |
+ |
+ /* Advance zCsr to the next token. Store that token type in 'token', |
+ ** and its length in 'len' (to be used next iteration of this loop). |
+ */ |
+ do { |
+ zCsr += len; |
+ len = sqlite3GetToken(zCsr, &token); |
+ }while( token==TK_SPACE ); |
+ assert( len>0 ); |
+ |
+ /* Variable 'dist' stores the number of tokens read since the most |
+ ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN |
+ ** token is read and 'dist' equals 2, the condition stated above |
+ ** to be met. |
+ ** |
+ ** Note that ON cannot be a database, table or column name, so |
+ ** there is no need to worry about syntax like |
+ ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc. |
+ */ |
+ dist++; |
+ if( token==TK_DOT || token==TK_ON ){ |
+ dist = 0; |
+ } |
+ } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) ); |
+ |
+ /* Variable tname now contains the token that is the old table-name |
+ ** in the CREATE TRIGGER statement. |
+ */ |
+ zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql), |
+ zSql, zTableName, tname.z+tname.n); |
+ sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); |
+ } |
+} |
+#endif /* !SQLITE_OMIT_TRIGGER */ |
+ |
+/* |
+** Register built-in functions used to help implement ALTER TABLE |
+*/ |
+SQLITE_PRIVATE void sqlite3AlterFunctions(void){ |
+ static FuncDef aAlterTableFuncs[] = { |
+ FUNCTION(sqlite_rename_table, 2, 0, 0, renameTableFunc), |
+#ifndef SQLITE_OMIT_TRIGGER |
+ FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc), |
+#endif |
+#ifndef SQLITE_OMIT_FOREIGN_KEY |
+ FUNCTION(sqlite_rename_parent, 3, 0, 0, renameParentFunc), |
+#endif |
+ }; |
+ sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs)); |
+} |
+ |
+/* |
+** This function is used to create the text of expressions of the form: |
+** |
+** name=<constant1> OR name=<constant2> OR ... |
+** |
+** If argument zWhere is NULL, then a pointer string containing the text |
+** "name=<constant>" is returned, where <constant> is the quoted version |
+** of the string passed as argument zConstant. The returned buffer is |
+** allocated using sqlite3DbMalloc(). It is the responsibility of the |
+** caller to ensure that it is eventually freed. |
+** |
+** If argument zWhere is not NULL, then the string returned is |
+** "<where> OR name=<constant>", where <where> is the contents of zWhere. |
+** In this case zWhere is passed to sqlite3DbFree() before returning. |
+** |
+*/ |
+static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){ |
+ char *zNew; |
+ if( !zWhere ){ |
+ zNew = sqlite3MPrintf(db, "name=%Q", zConstant); |
+ }else{ |
+ zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant); |
+ sqlite3DbFree(db, zWhere); |
+ } |
+ return zNew; |
+} |
+ |
+#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
+/* |
+** Generate the text of a WHERE expression which can be used to select all |
+** tables that have foreign key constraints that refer to table pTab (i.e. |
+** constraints for which pTab is the parent table) from the sqlite_master |
+** table. |
+*/ |
+static char *whereForeignKeys(Parse *pParse, Table *pTab){ |
+ FKey *p; |
+ char *zWhere = 0; |
+ for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ |
+ zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName); |
+ } |
+ return zWhere; |
+} |
+#endif |
+ |
+/* |
+** Generate the text of a WHERE expression which can be used to select all |
+** temporary triggers on table pTab from the sqlite_temp_master table. If |
+** table pTab has no temporary triggers, or is itself stored in the |
+** temporary database, NULL is returned. |
+*/ |
+static char *whereTempTriggers(Parse *pParse, Table *pTab){ |
+ Trigger *pTrig; |
+ char *zWhere = 0; |
+ const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */ |
+ |
+ /* If the table is not located in the temp-db (in which case NULL is |
+ ** returned, loop through the tables list of triggers. For each trigger |
+ ** that is not part of the temp-db schema, add a clause to the WHERE |
+ ** expression being built up in zWhere. |
+ */ |
+ if( pTab->pSchema!=pTempSchema ){ |
+ sqlite3 *db = pParse->db; |
+ for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ |
+ if( pTrig->pSchema==pTempSchema ){ |
+ zWhere = whereOrName(db, zWhere, pTrig->zName); |
+ } |
+ } |
+ } |
+ if( zWhere ){ |
+ char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere); |
+ sqlite3DbFree(pParse->db, zWhere); |
+ zWhere = zNew; |
+ } |
+ return zWhere; |
+} |
+ |
+/* |
+** Generate code to drop and reload the internal representation of table |
+** pTab from the database, including triggers and temporary triggers. |
+** Argument zName is the name of the table in the database schema at |
+** the time the generated code is executed. This can be different from |
+** pTab->zName if this function is being called to code part of an |
+** "ALTER TABLE RENAME TO" statement. |
+*/ |
+static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){ |
+ Vdbe *v; |
+ char *zWhere; |
+ int iDb; /* Index of database containing pTab */ |
+#ifndef SQLITE_OMIT_TRIGGER |
+ Trigger *pTrig; |
+#endif |
+ |
+ v = sqlite3GetVdbe(pParse); |
+ if( NEVER(v==0) ) return; |
+ assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
+ iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
+ assert( iDb>=0 ); |
+ |
+#ifndef SQLITE_OMIT_TRIGGER |
+ /* Drop any table triggers from the internal schema. */ |
+ for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ |
+ int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); |
+ assert( iTrigDb==iDb || iTrigDb==1 ); |
+ sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0); |
+ } |
+#endif |
+ |
+ /* Drop the table and index from the internal schema. */ |
+ sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
+ |
+ /* Reload the table, index and permanent trigger schemas. */ |
+ zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName); |
+ if( !zWhere ) return; |
+ sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere); |
+ |
+#ifndef SQLITE_OMIT_TRIGGER |
+ /* Now, if the table is not stored in the temp database, reload any temp |
+ ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. |
+ */ |
+ if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ |
+ sqlite3VdbeAddParseSchemaOp(v, 1, zWhere); |
+ } |
+#endif |
+} |
+ |
+/* |
+** Parameter zName is the name of a table that is about to be altered |
+** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN). |
+** If the table is a system table, this function leaves an error message |
+** in pParse->zErr (system tables may not be altered) and returns non-zero. |
+** |
+** Or, if zName is not a system table, zero is returned. |
+*/ |
+static int isSystemTable(Parse *pParse, const char *zName){ |
+ if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ |
+ sqlite3ErrorMsg(pParse, "table %s may not be altered", zName); |
+ return 1; |
+ } |
+ return 0; |
+} |
+ |
+/* |
+** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" |
+** command. |
+*/ |
+SQLITE_PRIVATE void sqlite3AlterRenameTable( |
+ Parse *pParse, /* Parser context. */ |
+ SrcList *pSrc, /* The table to rename. */ |
+ Token *pName /* The new table name. */ |
+){ |
+ int iDb; /* Database that contains the table */ |
+ char *zDb; /* Name of database iDb */ |
+ Table *pTab; /* Table being renamed */ |
+ char *zName = 0; /* NULL-terminated version of pName */ |
+ sqlite3 *db = pParse->db; /* Database connection */ |
+ int nTabName; /* Number of UTF-8 characters in zTabName */ |
+ const char *zTabName; /* Original name of the table */ |
+ Vdbe *v; |
+#ifndef SQLITE_OMIT_TRIGGER |
+ char *zWhere = 0; /* Where clause to locate temp triggers */ |
+#endif |
+ VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */ |
+ int savedDbFlags; /* Saved value of db->flags */ |
+ |
+ savedDbFlags = db->flags; |
+ if( NEVER(db->mallocFailed) ) goto exit_rename_table; |
+ assert( pSrc->nSrc==1 ); |
+ assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
+ |
+ pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); |
+ if( !pTab ) goto exit_rename_table; |
+ iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
+ zDb = db->aDb[iDb].zDbSName; |
+ db->flags |= SQLITE_PreferBuiltin; |
+ |
+ /* Get a NULL terminated version of the new table name. */ |
+ zName = sqlite3NameFromToken(db, pName); |
+ if( !zName ) goto exit_rename_table; |
+ |
+ /* Check that a table or index named 'zName' does not already exist |
+ ** in database iDb. If so, this is an error. |
+ */ |
+ if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){ |
+ sqlite3ErrorMsg(pParse, |
+ "there is already another table or index with this name: %s", zName); |
+ goto exit_rename_table; |
+ } |
+ |
+ /* Make sure it is not a system table being altered, or a reserved name |
+ ** that the table is being renamed to. |
+ */ |
+ if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){ |
+ goto exit_rename_table; |
+ } |
+ if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto |
+ exit_rename_table; |
+ } |
+ |
+#ifndef SQLITE_OMIT_VIEW |
+ if( pTab->pSelect ){ |
+ sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName); |
+ goto exit_rename_table; |
+ } |
+#endif |
+ |
+#ifndef SQLITE_OMIT_AUTHORIZATION |
+ /* Invoke the authorization callback. */ |
+ if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ |
+ goto exit_rename_table; |
+ } |
+#endif |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+ if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
+ goto exit_rename_table; |
+ } |
+ if( IsVirtual(pTab) ){ |
+ pVTab = sqlite3GetVTable(db, pTab); |
+ if( pVTab->pVtab->pModule->xRename==0 ){ |
+ pVTab = 0; |
+ } |
+ } |
+#endif |
+ |
+ /* Begin a transaction for database iDb. |
+ ** Then modify the schema cookie (since the ALTER TABLE modifies the |
+ ** schema). Open a statement transaction if the table is a virtual |
+ ** table. |
+ */ |
+ v = sqlite3GetVdbe(pParse); |
+ if( v==0 ){ |
+ goto exit_rename_table; |
+ } |
+ sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb); |
+ sqlite3ChangeCookie(pParse, iDb); |
+ |
+ /* If this is a virtual table, invoke the xRename() function if |
+ ** one is defined. The xRename() callback will modify the names |
+ ** of any resources used by the v-table implementation (including other |
+ ** SQLite tables) that are identified by the name of the virtual table. |
+ */ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+ if( pVTab ){ |
+ int i = ++pParse->nMem; |
+ sqlite3VdbeLoadString(v, i, zName); |
+ sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB); |
+ sqlite3MayAbort(pParse); |
+ } |
+#endif |
+ |
+ /* figure out how many UTF-8 characters are in zName */ |
+ zTabName = pTab->zName; |
+ nTabName = sqlite3Utf8CharLen(zTabName, -1); |
+ |
+#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
+ if( db->flags&SQLITE_ForeignKeys ){ |
+ /* If foreign-key support is enabled, rewrite the CREATE TABLE |
+ ** statements corresponding to all child tables of foreign key constraints |
+ ** for which the renamed table is the parent table. */ |
+ if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){ |
+ sqlite3NestedParse(pParse, |
+ "UPDATE \"%w\".%s SET " |
+ "sql = sqlite_rename_parent(sql, %Q, %Q) " |
+ "WHERE %s;", zDb, MASTER_NAME, zTabName, zName, zWhere); |
+ sqlite3DbFree(db, zWhere); |
+ } |
+ } |
+#endif |
+ |
+ /* Modify the sqlite_master table to use the new table name. */ |
+ sqlite3NestedParse(pParse, |
+ "UPDATE %Q.%s SET " |
+#ifdef SQLITE_OMIT_TRIGGER |
+ "sql = sqlite_rename_table(sql, %Q), " |
+#else |
+ "sql = CASE " |
+ "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)" |
+ "ELSE sqlite_rename_table(sql, %Q) END, " |
+#endif |
+ "tbl_name = %Q, " |
+ "name = CASE " |
+ "WHEN type='table' THEN %Q " |
+ "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN " |
+ "'sqlite_autoindex_' || %Q || substr(name,%d+18) " |
+ "ELSE name END " |
+ "WHERE tbl_name=%Q COLLATE nocase AND " |
+ "(type='table' OR type='index' OR type='trigger');", |
+ zDb, MASTER_NAME, zName, zName, zName, |
+#ifndef SQLITE_OMIT_TRIGGER |
+ zName, |
+#endif |
+ zName, nTabName, zTabName |
+ ); |
+ |
+#ifndef SQLITE_OMIT_AUTOINCREMENT |
+ /* If the sqlite_sequence table exists in this database, then update |
+ ** it with the new table name. |
+ */ |
+ if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){ |
+ sqlite3NestedParse(pParse, |
+ "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q", |
+ zDb, zName, pTab->zName); |
+ } |
+#endif |
+ |
+#ifndef SQLITE_OMIT_TRIGGER |
+ /* If there are TEMP triggers on this table, modify the sqlite_temp_master |
+ ** table. Don't do this if the table being ALTERed is itself located in |
+ ** the temp database. |
+ */ |
+ if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ |
+ sqlite3NestedParse(pParse, |
+ "UPDATE sqlite_temp_master SET " |
+ "sql = sqlite_rename_trigger(sql, %Q), " |
+ "tbl_name = %Q " |
+ "WHERE %s;", zName, zName, zWhere); |
+ sqlite3DbFree(db, zWhere); |
+ } |
+#endif |
+ |
+#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) |
+ if( db->flags&SQLITE_ForeignKeys ){ |
+ FKey *p; |
+ for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ |
+ Table *pFrom = p->pFrom; |
+ if( pFrom!=pTab ){ |
+ reloadTableSchema(pParse, p->pFrom, pFrom->zName); |
+ } |
+ } |
+ } |
+#endif |
+ |
+ /* Drop and reload the internal table schema. */ |
+ reloadTableSchema(pParse, pTab, zName); |
+ |
+exit_rename_table: |
+ sqlite3SrcListDelete(db, pSrc); |
+ sqlite3DbFree(db, zName); |
+ db->flags = savedDbFlags; |
+} |
+ |
+/* |
+** This function is called after an "ALTER TABLE ... ADD" statement |
+** has been parsed. Argument pColDef contains the text of the new |
+** column definition. |
+** |
+** The Table structure pParse->pNewTable was extended to include |
+** the new column during parsing. |
+*/ |
+SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ |
+ Table *pNew; /* Copy of pParse->pNewTable */ |
+ Table *pTab; /* Table being altered */ |
+ int iDb; /* Database number */ |
+ const char *zDb; /* Database name */ |
+ const char *zTab; /* Table name */ |
+ char *zCol; /* Null-terminated column definition */ |
+ Column *pCol; /* The new column */ |
+ Expr *pDflt; /* Default value for the new column */ |
+ sqlite3 *db; /* The database connection; */ |
+ Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */ |
+ int r1; /* Temporary registers */ |
+ |
+ db = pParse->db; |
+ if( pParse->nErr || db->mallocFailed ) return; |
+ assert( v!=0 ); |
+ pNew = pParse->pNewTable; |
+ assert( pNew ); |
+ |
+ assert( sqlite3BtreeHoldsAllMutexes(db) ); |
+ iDb = sqlite3SchemaToIndex(db, pNew->pSchema); |
+ zDb = db->aDb[iDb].zDbSName; |
+ zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */ |
+ pCol = &pNew->aCol[pNew->nCol-1]; |
+ pDflt = pCol->pDflt; |
+ pTab = sqlite3FindTable(db, zTab, zDb); |
+ assert( pTab ); |
+ |
+#ifndef SQLITE_OMIT_AUTHORIZATION |
+ /* Invoke the authorization callback. */ |
+ if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ |
+ return; |
+ } |
+#endif |
+ |
+ /* If the default value for the new column was specified with a |
+ ** literal NULL, then set pDflt to 0. This simplifies checking |
+ ** for an SQL NULL default below. |
+ */ |
+ assert( pDflt==0 || pDflt->op==TK_SPAN ); |
+ if( pDflt && pDflt->pLeft->op==TK_NULL ){ |
+ pDflt = 0; |
+ } |
+ |
+ /* Check that the new column is not specified as PRIMARY KEY or UNIQUE. |
+ ** If there is a NOT NULL constraint, then the default value for the |
+ ** column must not be NULL. |
+ */ |
+ if( pCol->colFlags & COLFLAG_PRIMKEY ){ |
+ sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column"); |
+ return; |
+ } |
+ if( pNew->pIndex ){ |
+ sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column"); |
+ return; |
+ } |
+ if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){ |
+ sqlite3ErrorMsg(pParse, |
+ "Cannot add a REFERENCES column with non-NULL default value"); |
+ return; |
+ } |
+ if( pCol->notNull && !pDflt ){ |
+ sqlite3ErrorMsg(pParse, |
+ "Cannot add a NOT NULL column with default value NULL"); |
+ return; |
+ } |
+ |
+ /* Ensure the default expression is something that sqlite3ValueFromExpr() |
+ ** can handle (i.e. not CURRENT_TIME etc.) |
+ */ |
+ if( pDflt ){ |
+ sqlite3_value *pVal = 0; |
+ int rc; |
+ rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal); |
+ assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
+ if( rc!=SQLITE_OK ){ |
+ assert( db->mallocFailed == 1 ); |
+ return; |
+ } |
+ if( !pVal ){ |
+ sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default"); |
+ return; |
+ } |
+ sqlite3ValueFree(pVal); |
+ } |
+ |
+ /* Modify the CREATE TABLE statement. */ |
+ zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); |
+ if( zCol ){ |
+ char *zEnd = &zCol[pColDef->n-1]; |
+ int savedDbFlags = db->flags; |
+ while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){ |
+ *zEnd-- = '\0'; |
+ } |
+ db->flags |= SQLITE_PreferBuiltin; |
+ sqlite3NestedParse(pParse, |
+ "UPDATE \"%w\".%s SET " |
+ "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) " |
+ "WHERE type = 'table' AND name = %Q", |
+ zDb, MASTER_NAME, pNew->addColOffset, zCol, pNew->addColOffset+1, |
+ zTab |
+ ); |
+ sqlite3DbFree(db, zCol); |
+ db->flags = savedDbFlags; |
+ } |
+ |
+ /* Make sure the schema version is at least 3. But do not upgrade |
+ ** from less than 3 to 4, as that will corrupt any preexisting DESC |
+ ** index. |
+ */ |
+ r1 = sqlite3GetTempReg(pParse); |
+ sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT); |
+ sqlite3VdbeUsesBtree(v, iDb); |
+ sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2); |
+ sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2); |
+ VdbeCoverage(v); |
+ sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3); |
+ sqlite3ReleaseTempReg(pParse, r1); |
+ |
+ /* Reload the schema of the modified table. */ |
+ reloadTableSchema(pParse, pTab, pTab->zName); |
+} |
+ |
+/* |
+** This function is called by the parser after the table-name in |
+** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument |
+** pSrc is the full-name of the table being altered. |
+** |
+** This routine makes a (partial) copy of the Table structure |
+** for the table being altered and sets Parse.pNewTable to point |
+** to it. Routines called by the parser as the column definition |
+** is parsed (i.e. sqlite3AddColumn()) add the new Column data to |
+** the copy. The copy of the Table structure is deleted by tokenize.c |
+** after parsing is finished. |
+** |
+** Routine sqlite3AlterFinishAddColumn() will be called to complete |
+** coding the "ALTER TABLE ... ADD" statement. |
+*/ |
+SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ |
+ Table *pNew; |
+ Table *pTab; |
+ Vdbe *v; |
+ int iDb; |
+ int i; |
+ int nAlloc; |
+ sqlite3 *db = pParse->db; |
+ |
+ /* Look up the table being altered. */ |
+ assert( pParse->pNewTable==0 ); |
+ assert( sqlite3BtreeHoldsAllMutexes(db) ); |
+ if( db->mallocFailed ) goto exit_begin_add_column; |
+ pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); |
+ if( !pTab ) goto exit_begin_add_column; |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+ if( IsVirtual(pTab) ){ |
+ sqlite3ErrorMsg(pParse, "virtual tables may not be altered"); |
+ goto exit_begin_add_column; |
+ } |
+#endif |
+ |
+ /* Make sure this is not an attempt to ALTER a view. */ |
+ if( pTab->pSelect ){ |
+ sqlite3ErrorMsg(pParse, "Cannot add a column to a view"); |
+ goto exit_begin_add_column; |
+ } |
+ if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){ |
+ goto exit_begin_add_column; |
+ } |
+ |
+ assert( pTab->addColOffset>0 ); |
+ iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
+ |
+ /* Put a copy of the Table struct in Parse.pNewTable for the |
+ ** sqlite3AddColumn() function and friends to modify. But modify |
+ ** the name by adding an "sqlite_altertab_" prefix. By adding this |
+ ** prefix, we insure that the name will not collide with an existing |
+ ** table because user table are not allowed to have the "sqlite_" |
+ ** prefix on their name. |
+ */ |
+ pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table)); |
+ if( !pNew ) goto exit_begin_add_column; |
+ pParse->pNewTable = pNew; |
+ pNew->nTabRef = 1; |
+ pNew->nCol = pTab->nCol; |
+ assert( pNew->nCol>0 ); |
+ nAlloc = (((pNew->nCol-1)/8)*8)+8; |
+ assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 ); |
+ pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc); |
+ pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName); |
+ if( !pNew->aCol || !pNew->zName ){ |
+ assert( db->mallocFailed ); |
+ goto exit_begin_add_column; |
+ } |
+ memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol); |
+ for(i=0; i<pNew->nCol; i++){ |
+ Column *pCol = &pNew->aCol[i]; |
+ pCol->zName = sqlite3DbStrDup(db, pCol->zName); |
+ pCol->zColl = 0; |
+ pCol->pDflt = 0; |
+ } |
+ pNew->pSchema = db->aDb[iDb].pSchema; |
+ pNew->addColOffset = pTab->addColOffset; |
+ pNew->nTabRef = 1; |
+ |
+ /* Begin a transaction and increment the schema cookie. */ |
+ sqlite3BeginWriteOperation(pParse, 0, iDb); |
+ v = sqlite3GetVdbe(pParse); |
+ if( !v ) goto exit_begin_add_column; |
+ sqlite3ChangeCookie(pParse, iDb); |
+ |
+exit_begin_add_column: |
+ sqlite3SrcListDelete(db, pSrc); |
+ return; |
+} |
+#endif /* SQLITE_ALTER_TABLE */ |
+ |
+/************** End of alter.c ***********************************************/ |
+/************** Begin file analyze.c *****************************************/ |
+/* |
+** 2005-07-08 |
+** |
+** 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. |
+** |
+************************************************************************* |
+** This file contains code associated with the ANALYZE command. |
+** |
+** The ANALYZE command gather statistics about the content of tables |
+** and indices. These statistics are made available to the query planner |
+** to help it make better decisions about how to perform queries. |
+** |
+** The following system tables are or have been supported: |
+** |
+** CREATE TABLE sqlite_stat1(tbl, idx, stat); |
+** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample); |
+** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample); |
+** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample); |
+** |
+** Additional tables might be added in future releases of SQLite. |
+** The sqlite_stat2 table is not created or used unless the SQLite version |
+** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled |
+** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated. |
+** The sqlite_stat2 table is superseded by sqlite_stat3, which is only |
+** created and used by SQLite versions 3.7.9 and later and with |
+** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3 |
+** is a superset of sqlite_stat2. The sqlite_stat4 is an enhanced |
+** version of sqlite_stat3 and is only available when compiled with |
+** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later. It is |
+** not possible to enable both STAT3 and STAT4 at the same time. If they |
+** are both enabled, then STAT4 takes precedence. |
+** |
+** For most applications, sqlite_stat1 provides all the statistics required |
+** for the query planner to make good choices. |
+** |
+** Format of sqlite_stat1: |
+** |
+** There is normally one row per index, with the index identified by the |
+** name in the idx column. The tbl column is the name of the table to |
+** which the index belongs. In each such row, the stat column will be |
+** a string consisting of a list of integers. The first integer in this |
+** list is the number of rows in the index. (This is the same as the |
+** number of rows in the table, except for partial indices.) The second |
+** integer is the average number of rows in the index that have the same |
+** value in the first column of the index. The third integer is the average |
+** number of rows in the index that have the same value for the first two |
+** columns. The N-th integer (for N>1) is the average number of rows in |
+** the index which have the same value for the first N-1 columns. For |
+** a K-column index, there will be K+1 integers in the stat column. If |
+** the index is unique, then the last integer will be 1. |
+** |
+** The list of integers in the stat column can optionally be followed |
+** by the keyword "unordered". The "unordered" keyword, if it is present, |
+** must be separated from the last integer by a single space. If the |
+** "unordered" keyword is present, then the query planner assumes that |
+** the index is unordered and will not use the index for a range query. |
+** |
+** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat |
+** column contains a single integer which is the (estimated) number of |
+** rows in the table identified by sqlite_stat1.tbl. |
+** |
+** Format of sqlite_stat2: |
+** |
+** The sqlite_stat2 is only created and is only used if SQLite is compiled |
+** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between |
+** 3.6.18 and 3.7.8. The "stat2" table contains additional information |
+** about the distribution of keys within an index. The index is identified by |
+** the "idx" column and the "tbl" column is the name of the table to which |
+** the index belongs. There are usually 10 rows in the sqlite_stat2 |
+** table for each index. |
+** |
+** The sqlite_stat2 entries for an index that have sampleno between 0 and 9 |
+** inclusive are samples of the left-most key value in the index taken at |
+** evenly spaced points along the index. Let the number of samples be S |
+** (10 in the standard build) and let C be the number of rows in the index. |
+** Then the sampled rows are given by: |
+** |
+** rownumber = (i*C*2 + C)/(S*2) |
+** |
+** For i between 0 and S-1. Conceptually, the index space is divided into |
+** S uniform buckets and the samples are the middle row from each bucket. |
+** |
+** The format for sqlite_stat2 is recorded here for legacy reference. This |
+** version of SQLite does not support sqlite_stat2. It neither reads nor |
+** writes the sqlite_stat2 table. This version of SQLite only supports |
+** sqlite_stat3. |
+** |
+** Format for sqlite_stat3: |
+** |
+** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the |
+** sqlite_stat4 format will be described first. Further information |
+** about sqlite_stat3 follows the sqlite_stat4 description. |
+** |
+** Format for sqlite_stat4: |
+** |
+** As with sqlite_stat2, the sqlite_stat4 table contains histogram data |
+** to aid the query planner in choosing good indices based on the values |
+** that indexed columns are compared against in the WHERE clauses of |
+** queries. |
+** |
+** The sqlite_stat4 table contains multiple entries for each index. |
+** The idx column names the index and the tbl column is the table of the |
+** index. If the idx and tbl columns are the same, then the sample is |
+** of the INTEGER PRIMARY KEY. The sample column is a blob which is the |
+** binary encoding of a key from the index. The nEq column is a |
+** list of integers. The first integer is the approximate number |
+** of entries in the index whose left-most column exactly matches |
+** the left-most column of the sample. The second integer in nEq |
+** is the approximate number of entries in the index where the |
+** first two columns match the first two columns of the sample. |
+** And so forth. nLt is another list of integers that show the approximate |
+** number of entries that are strictly less than the sample. The first |
+** integer in nLt contains the number of entries in the index where the |
+** left-most column is less than the left-most column of the sample. |
+** The K-th integer in the nLt entry is the number of index entries |
+** where the first K columns are less than the first K columns of the |
+** sample. The nDLt column is like nLt except that it contains the |
+** number of distinct entries in the index that are less than the |
+** sample. |
+** |
+** There can be an arbitrary number of sqlite_stat4 entries per index. |
+** The ANALYZE command will typically generate sqlite_stat4 tables |
+** that contain between 10 and 40 samples which are distributed across |
+** the key space, though not uniformly, and which include samples with |
+** large nEq values. |
+** |
+** Format for sqlite_stat3 redux: |
+** |
+** The sqlite_stat3 table is like sqlite_stat4 except that it only |
+** looks at the left-most column of the index. The sqlite_stat3.sample |
+** column contains the actual value of the left-most column instead |
+** of a blob encoding of the complete index key as is found in |
+** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3 |
+** all contain just a single integer which is the same as the first |
+** integer in the equivalent columns in sqlite_stat4. |
+*/ |
+#ifndef SQLITE_OMIT_ANALYZE |
+/* #include "sqliteInt.h" */ |
+ |
+#if defined(SQLITE_ENABLE_STAT4) |
+# define IsStat4 1 |
+# define IsStat3 0 |
+#elif defined(SQLITE_ENABLE_STAT3) |
+# define IsStat4 0 |
+# define IsStat3 1 |
+#else |
+# define IsStat4 0 |
+# define IsStat3 0 |
+# undef SQLITE_STAT4_SAMPLES |
+# define SQLITE_STAT4_SAMPLES 1 |
+#endif |
+#define IsStat34 (IsStat3+IsStat4) /* 1 for STAT3 or STAT4. 0 otherwise */ |
+ |
+/* |
+** This routine generates code that opens the sqlite_statN tables. |
+** The sqlite_stat1 table is always relevant. sqlite_stat2 is now |
+** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when |
+** appropriate compile-time options are provided. |
+** |
+** If the sqlite_statN tables do not previously exist, it is created. |
+** |
+** Argument zWhere may be a pointer to a buffer containing a table name, |
+** or it may be a NULL pointer. If it is not NULL, then all entries in |
+** the sqlite_statN tables associated with the named table are deleted. |
+** If zWhere==0, then code is generated to delete all stat table entries. |
+*/ |
+static void openStatTable( |
+ Parse *pParse, /* Parsing context */ |
+ int iDb, /* The database we are looking in */ |
+ int iStatCur, /* Open the sqlite_stat1 table on this cursor */ |
+ const char *zWhere, /* Delete entries for this table or index */ |
+ const char *zWhereType /* Either "tbl" or "idx" */ |
+){ |
+ static const struct { |
+ const char *zName; |
+ const char *zCols; |
+ } aTable[] = { |
+ { "sqlite_stat1", "tbl,idx,stat" }, |
+#if defined(SQLITE_ENABLE_STAT4) |
+ { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" }, |
+ { "sqlite_stat3", 0 }, |
+#elif defined(SQLITE_ENABLE_STAT3) |
+ { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" }, |
+ { "sqlite_stat4", 0 }, |
+#else |
+ { "sqlite_stat3", 0 }, |
+ { "sqlite_stat4", 0 }, |
+#endif |
+ }; |
+ int i; |
+ sqlite3 *db = pParse->db; |
+ Db *pDb; |
+ Vdbe *v = sqlite3GetVdbe(pParse); |
+ int aRoot[ArraySize(aTable)]; |
+ u8 aCreateTbl[ArraySize(aTable)]; |
+ |
+ if( v==0 ) return; |
+ assert( sqlite3BtreeHoldsAllMutexes(db) ); |
+ assert( sqlite3VdbeDb(v)==db ); |
+ pDb = &db->aDb[iDb]; |
+ |
+ /* Create new statistic tables if they do not exist, or clear them |
+ ** if they do already exist. |
+ */ |
+ for(i=0; i<ArraySize(aTable); i++){ |
+ const char *zTab = aTable[i].zName; |
+ Table *pStat; |
+ if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){ |
+ if( aTable[i].zCols ){ |
+ /* The sqlite_statN table does not exist. Create it. Note that a |
+ ** side-effect of the CREATE TABLE statement is to leave the rootpage |
+ ** of the new table in register pParse->regRoot. This is important |
+ ** because the OpenWrite opcode below will be needing it. */ |
+ sqlite3NestedParse(pParse, |
+ "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols |
+ ); |
+ aRoot[i] = pParse->regRoot; |
+ aCreateTbl[i] = OPFLAG_P2ISREG; |
+ } |
+ }else{ |
+ /* The table already exists. If zWhere is not NULL, delete all entries |
+ ** associated with the table zWhere. If zWhere is NULL, delete the |
+ ** entire contents of the table. */ |
+ aRoot[i] = pStat->tnum; |
+ aCreateTbl[i] = 0; |
+ sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab); |
+ if( zWhere ){ |
+ sqlite3NestedParse(pParse, |
+ "DELETE FROM %Q.%s WHERE %s=%Q", |
+ pDb->zDbSName, zTab, zWhereType, zWhere |
+ ); |
+ }else{ |
+ /* The sqlite_stat[134] table already exists. Delete all rows. */ |
+ sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb); |
+ } |
+ } |
+ } |
+ |
+ /* Open the sqlite_stat[134] tables for writing. */ |
+ for(i=0; aTable[i].zCols; i++){ |
+ assert( i<ArraySize(aTable) ); |
+ sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3); |
+ sqlite3VdbeChangeP5(v, aCreateTbl[i]); |
+ VdbeComment((v, aTable[i].zName)); |
+ } |
+} |
+ |
+/* |
+** Recommended number of samples for sqlite_stat4 |
+*/ |
+#ifndef SQLITE_STAT4_SAMPLES |
+# define SQLITE_STAT4_SAMPLES 24 |
+#endif |
+ |
+/* |
+** Three SQL functions - stat_init(), stat_push(), and stat_get() - |
+** share an instance of the following structure to hold their state |
+** information. |
+*/ |
+typedef struct Stat4Accum Stat4Accum; |
+typedef struct Stat4Sample Stat4Sample; |
+struct Stat4Sample { |
+ tRowcnt *anEq; /* sqlite_stat4.nEq */ |
+ tRowcnt *anDLt; /* sqlite_stat4.nDLt */ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ tRowcnt *anLt; /* sqlite_stat4.nLt */ |
+ union { |
+ i64 iRowid; /* Rowid in main table of the key */ |
+ u8 *aRowid; /* Key for WITHOUT ROWID tables */ |
+ } u; |
+ u32 nRowid; /* Sizeof aRowid[] */ |
+ u8 isPSample; /* True if a periodic sample */ |
+ int iCol; /* If !isPSample, the reason for inclusion */ |
+ u32 iHash; /* Tiebreaker hash */ |
+#endif |
+}; |
+struct Stat4Accum { |
+ tRowcnt nRow; /* Number of rows in the entire table */ |
+ tRowcnt nPSample; /* How often to do a periodic sample */ |
+ int nCol; /* Number of columns in index + pk/rowid */ |
+ int nKeyCol; /* Number of index columns w/o the pk/rowid */ |
+ int mxSample; /* Maximum number of samples to accumulate */ |
+ Stat4Sample current; /* Current row as a Stat4Sample */ |
+ u32 iPrn; /* Pseudo-random number used for sampling */ |
+ Stat4Sample *aBest; /* Array of nCol best samples */ |
+ int iMin; /* Index in a[] of entry with minimum score */ |
+ int nSample; /* Current number of samples */ |
+ int iGet; /* Index of current sample accessed by stat_get() */ |
+ Stat4Sample *a; /* Array of mxSample Stat4Sample objects */ |
+ sqlite3 *db; /* Database connection, for malloc() */ |
+}; |
+ |
+/* Reclaim memory used by a Stat4Sample |
+*/ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+static void sampleClear(sqlite3 *db, Stat4Sample *p){ |
+ assert( db!=0 ); |
+ if( p->nRowid ){ |
+ sqlite3DbFree(db, p->u.aRowid); |
+ p->nRowid = 0; |
+ } |
+} |
+#endif |
+ |
+/* Initialize the BLOB value of a ROWID |
+*/ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){ |
+ assert( db!=0 ); |
+ if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid); |
+ p->u.aRowid = sqlite3DbMallocRawNN(db, n); |
+ if( p->u.aRowid ){ |
+ p->nRowid = n; |
+ memcpy(p->u.aRowid, pData, n); |
+ }else{ |
+ p->nRowid = 0; |
+ } |
+} |
+#endif |
+ |
+/* Initialize the INTEGER value of a ROWID. |
+*/ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){ |
+ assert( db!=0 ); |
+ if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid); |
+ p->nRowid = 0; |
+ p->u.iRowid = iRowid; |
+} |
+#endif |
+ |
+ |
+/* |
+** Copy the contents of object (*pFrom) into (*pTo). |
+*/ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){ |
+ pTo->isPSample = pFrom->isPSample; |
+ pTo->iCol = pFrom->iCol; |
+ pTo->iHash = pFrom->iHash; |
+ memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol); |
+ memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol); |
+ memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol); |
+ if( pFrom->nRowid ){ |
+ sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid); |
+ }else{ |
+ sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid); |
+ } |
+} |
+#endif |
+ |
+/* |
+** Reclaim all memory of a Stat4Accum structure. |
+*/ |
+static void stat4Destructor(void *pOld){ |
+ Stat4Accum *p = (Stat4Accum*)pOld; |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ int i; |
+ for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i); |
+ for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i); |
+ sampleClear(p->db, &p->current); |
+#endif |
+ sqlite3DbFree(p->db, p); |
+} |
+ |
+/* |
+** Implementation of the stat_init(N,K,C) SQL function. The three parameters |
+** are: |
+** N: The number of columns in the index including the rowid/pk (note 1) |
+** K: The number of columns in the index excluding the rowid/pk. |
+** C: The number of rows in the index (note 2) |
+** |
+** Note 1: In the special case of the covering index that implements a |
+** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the |
+** total number of columns in the table. |
+** |
+** Note 2: C is only used for STAT3 and STAT4. |
+** |
+** For indexes on ordinary rowid tables, N==K+1. But for indexes on |
+** WITHOUT ROWID tables, N=K+P where P is the number of columns in the |
+** PRIMARY KEY of the table. The covering index that implements the |
+** original WITHOUT ROWID table as N==K as a special case. |
+** |
+** This routine allocates the Stat4Accum object in heap memory. The return |
+** value is a pointer to the Stat4Accum object. The datatype of the |
+** return value is BLOB, but it is really just a pointer to the Stat4Accum |
+** object. |
+*/ |
+static void statInit( |
+ sqlite3_context *context, |
+ int argc, |
+ sqlite3_value **argv |
+){ |
+ Stat4Accum *p; |
+ int nCol; /* Number of columns in index being sampled */ |
+ int nKeyCol; /* Number of key columns */ |
+ int nColUp; /* nCol rounded up for alignment */ |
+ int n; /* Bytes of space to allocate */ |
+ sqlite3 *db; /* Database connection */ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ int mxSample = SQLITE_STAT4_SAMPLES; |
+#endif |
+ |
+ /* Decode the three function arguments */ |
+ UNUSED_PARAMETER(argc); |
+ nCol = sqlite3_value_int(argv[0]); |
+ assert( nCol>0 ); |
+ nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol; |
+ nKeyCol = sqlite3_value_int(argv[1]); |
+ assert( nKeyCol<=nCol ); |
+ assert( nKeyCol>0 ); |
+ |
+ /* Allocate the space required for the Stat4Accum object */ |
+ n = sizeof(*p) |
+ + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */ |
+ + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */ |
+ + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */ |
+ + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample) |
+#endif |
+ ; |
+ db = sqlite3_context_db_handle(context); |
+ p = sqlite3DbMallocZero(db, n); |
+ if( p==0 ){ |
+ sqlite3_result_error_nomem(context); |
+ return; |
+ } |
+ |
+ p->db = db; |
+ p->nRow = 0; |
+ p->nCol = nCol; |
+ p->nKeyCol = nKeyCol; |
+ p->current.anDLt = (tRowcnt*)&p[1]; |
+ p->current.anEq = &p->current.anDLt[nColUp]; |
+ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ { |
+ u8 *pSpace; /* Allocated space not yet assigned */ |
+ int i; /* Used to iterate through p->aSample[] */ |
+ |
+ p->iGet = -1; |
+ p->mxSample = mxSample; |
+ p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1); |
+ p->current.anLt = &p->current.anEq[nColUp]; |
+ p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]); |
+ |
+ /* Set up the Stat4Accum.a[] and aBest[] arrays */ |
+ p->a = (struct Stat4Sample*)&p->current.anLt[nColUp]; |
+ p->aBest = &p->a[mxSample]; |
+ pSpace = (u8*)(&p->a[mxSample+nCol]); |
+ for(i=0; i<(mxSample+nCol); i++){ |
+ p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp); |
+ p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp); |
+ p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp); |
+ } |
+ assert( (pSpace - (u8*)p)==n ); |
+ |
+ for(i=0; i<nCol; i++){ |
+ p->aBest[i].iCol = i; |
+ } |
+ } |
+#endif |
+ |
+ /* Return a pointer to the allocated object to the caller. Note that |
+ ** only the pointer (the 2nd parameter) matters. The size of the object |
+ ** (given by the 3rd parameter) is never used and can be any positive |
+ ** value. */ |
+ sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor); |
+} |
+static const FuncDef statInitFuncdef = { |
+ 2+IsStat34, /* nArg */ |
+ SQLITE_UTF8, /* funcFlags */ |
+ 0, /* pUserData */ |
+ 0, /* pNext */ |
+ statInit, /* xSFunc */ |
+ 0, /* xFinalize */ |
+ "stat_init", /* zName */ |
+ {0} |
+}; |
+ |
+#ifdef SQLITE_ENABLE_STAT4 |
+/* |
+** pNew and pOld are both candidate non-periodic samples selected for |
+** the same column (pNew->iCol==pOld->iCol). Ignoring this column and |
+** considering only any trailing columns and the sample hash value, this |
+** function returns true if sample pNew is to be preferred over pOld. |
+** In other words, if we assume that the cardinalities of the selected |
+** column for pNew and pOld are equal, is pNew to be preferred over pOld. |
+** |
+** This function assumes that for each argument sample, the contents of |
+** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid. |
+*/ |
+static int sampleIsBetterPost( |
+ Stat4Accum *pAccum, |
+ Stat4Sample *pNew, |
+ Stat4Sample *pOld |
+){ |
+ int nCol = pAccum->nCol; |
+ int i; |
+ assert( pNew->iCol==pOld->iCol ); |
+ for(i=pNew->iCol+1; i<nCol; i++){ |
+ if( pNew->anEq[i]>pOld->anEq[i] ) return 1; |
+ if( pNew->anEq[i]<pOld->anEq[i] ) return 0; |
+ } |
+ if( pNew->iHash>pOld->iHash ) return 1; |
+ return 0; |
+} |
+#endif |
+ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+/* |
+** Return true if pNew is to be preferred over pOld. |
+** |
+** This function assumes that for each argument sample, the contents of |
+** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid. |
+*/ |
+static int sampleIsBetter( |
+ Stat4Accum *pAccum, |
+ Stat4Sample *pNew, |
+ Stat4Sample *pOld |
+){ |
+ tRowcnt nEqNew = pNew->anEq[pNew->iCol]; |
+ tRowcnt nEqOld = pOld->anEq[pOld->iCol]; |
+ |
+ assert( pOld->isPSample==0 && pNew->isPSample==0 ); |
+ assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) ); |
+ |
+ if( (nEqNew>nEqOld) ) return 1; |
+#ifdef SQLITE_ENABLE_STAT4 |
+ if( nEqNew==nEqOld ){ |
+ if( pNew->iCol<pOld->iCol ) return 1; |
+ return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld)); |
+ } |
+ return 0; |
+#else |
+ return (nEqNew==nEqOld && pNew->iHash>pOld->iHash); |
+#endif |
+} |
+ |
+/* |
+** Copy the contents of sample *pNew into the p->a[] array. If necessary, |
+** remove the least desirable sample from p->a[] to make room. |
+*/ |
+static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){ |
+ Stat4Sample *pSample = 0; |
+ int i; |
+ |
+ assert( IsStat4 || nEqZero==0 ); |
+ |
+#ifdef SQLITE_ENABLE_STAT4 |
+ if( pNew->isPSample==0 ){ |
+ Stat4Sample *pUpgrade = 0; |
+ assert( pNew->anEq[pNew->iCol]>0 ); |
+ |
+ /* This sample is being added because the prefix that ends in column |
+ ** iCol occurs many times in the table. However, if we have already |
+ ** added a sample that shares this prefix, there is no need to add |
+ ** this one. Instead, upgrade the priority of the highest priority |
+ ** existing sample that shares this prefix. */ |
+ for(i=p->nSample-1; i>=0; i--){ |
+ Stat4Sample *pOld = &p->a[i]; |
+ if( pOld->anEq[pNew->iCol]==0 ){ |
+ if( pOld->isPSample ) return; |
+ assert( pOld->iCol>pNew->iCol ); |
+ assert( sampleIsBetter(p, pNew, pOld) ); |
+ if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){ |
+ pUpgrade = pOld; |
+ } |
+ } |
+ } |
+ if( pUpgrade ){ |
+ pUpgrade->iCol = pNew->iCol; |
+ pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol]; |
+ goto find_new_min; |
+ } |
+ } |
+#endif |
+ |
+ /* If necessary, remove sample iMin to make room for the new sample. */ |
+ if( p->nSample>=p->mxSample ){ |
+ Stat4Sample *pMin = &p->a[p->iMin]; |
+ tRowcnt *anEq = pMin->anEq; |
+ tRowcnt *anLt = pMin->anLt; |
+ tRowcnt *anDLt = pMin->anDLt; |
+ sampleClear(p->db, pMin); |
+ memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1)); |
+ pSample = &p->a[p->nSample-1]; |
+ pSample->nRowid = 0; |
+ pSample->anEq = anEq; |
+ pSample->anDLt = anDLt; |
+ pSample->anLt = anLt; |
+ p->nSample = p->mxSample-1; |
+ } |
+ |
+ /* The "rows less-than" for the rowid column must be greater than that |
+ ** for the last sample in the p->a[] array. Otherwise, the samples would |
+ ** be out of order. */ |
+#ifdef SQLITE_ENABLE_STAT4 |
+ assert( p->nSample==0 |
+ || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] ); |
+#endif |
+ |
+ /* Insert the new sample */ |
+ pSample = &p->a[p->nSample]; |
+ sampleCopy(p, pSample, pNew); |
+ p->nSample++; |
+ |
+ /* Zero the first nEqZero entries in the anEq[] array. */ |
+ memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero); |
+ |
+#ifdef SQLITE_ENABLE_STAT4 |
+ find_new_min: |
+#endif |
+ if( p->nSample>=p->mxSample ){ |
+ int iMin = -1; |
+ for(i=0; i<p->mxSample; i++){ |
+ if( p->a[i].isPSample ) continue; |
+ if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){ |
+ iMin = i; |
+ } |
+ } |
+ assert( iMin>=0 ); |
+ p->iMin = iMin; |
+ } |
+} |
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
+ |
+/* |
+** Field iChng of the index being scanned has changed. So at this point |
+** p->current contains a sample that reflects the previous row of the |
+** index. The value of anEq[iChng] and subsequent anEq[] elements are |
+** correct at this point. |
+*/ |
+static void samplePushPrevious(Stat4Accum *p, int iChng){ |
+#ifdef SQLITE_ENABLE_STAT4 |
+ int i; |
+ |
+ /* Check if any samples from the aBest[] array should be pushed |
+ ** into IndexSample.a[] at this point. */ |
+ for(i=(p->nCol-2); i>=iChng; i--){ |
+ Stat4Sample *pBest = &p->aBest[i]; |
+ pBest->anEq[i] = p->current.anEq[i]; |
+ if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){ |
+ sampleInsert(p, pBest, i); |
+ } |
+ } |
+ |
+ /* Update the anEq[] fields of any samples already collected. */ |
+ for(i=p->nSample-1; i>=0; i--){ |
+ int j; |
+ for(j=iChng; j<p->nCol; j++){ |
+ if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j]; |
+ } |
+ } |
+#endif |
+ |
+#if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4) |
+ if( iChng==0 ){ |
+ tRowcnt nLt = p->current.anLt[0]; |
+ tRowcnt nEq = p->current.anEq[0]; |
+ |
+ /* Check if this is to be a periodic sample. If so, add it. */ |
+ if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){ |
+ p->current.isPSample = 1; |
+ sampleInsert(p, &p->current, 0); |
+ p->current.isPSample = 0; |
+ }else |
+ |
+ /* Or if it is a non-periodic sample. Add it in this case too. */ |
+ if( p->nSample<p->mxSample |
+ || sampleIsBetter(p, &p->current, &p->a[p->iMin]) |
+ ){ |
+ sampleInsert(p, &p->current, 0); |
+ } |
+ } |
+#endif |
+ |
+#ifndef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ UNUSED_PARAMETER( p ); |
+ UNUSED_PARAMETER( iChng ); |
+#endif |
+} |
+ |
+/* |
+** Implementation of the stat_push SQL function: stat_push(P,C,R) |
+** Arguments: |
+** |
+** P Pointer to the Stat4Accum object created by stat_init() |
+** C Index of left-most column to differ from previous row |
+** R Rowid for the current row. Might be a key record for |
+** WITHOUT ROWID tables. |
+** |
+** This SQL function always returns NULL. It's purpose it to accumulate |
+** statistical data and/or samples in the Stat4Accum object about the |
+** index being analyzed. The stat_get() SQL function will later be used to |
+** extract relevant information for constructing the sqlite_statN tables. |
+** |
+** The R parameter is only used for STAT3 and STAT4 |
+*/ |
+static void statPush( |
+ sqlite3_context *context, |
+ int argc, |
+ sqlite3_value **argv |
+){ |
+ int i; |
+ |
+ /* The three function arguments */ |
+ Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]); |
+ int iChng = sqlite3_value_int(argv[1]); |
+ |
+ UNUSED_PARAMETER( argc ); |
+ UNUSED_PARAMETER( context ); |
+ assert( p->nCol>0 ); |
+ assert( iChng<p->nCol ); |
+ |
+ if( p->nRow==0 ){ |
+ /* This is the first call to this function. Do initialization. */ |
+ for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1; |
+ }else{ |
+ /* Second and subsequent calls get processed here */ |
+ samplePushPrevious(p, iChng); |
+ |
+ /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply |
+ ** to the current row of the index. */ |
+ for(i=0; i<iChng; i++){ |
+ p->current.anEq[i]++; |
+ } |
+ for(i=iChng; i<p->nCol; i++){ |
+ p->current.anDLt[i]++; |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ p->current.anLt[i] += p->current.anEq[i]; |
+#endif |
+ p->current.anEq[i] = 1; |
+ } |
+ } |
+ p->nRow++; |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){ |
+ sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2])); |
+ }else{ |
+ sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]), |
+ sqlite3_value_blob(argv[2])); |
+ } |
+ p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345; |
+#endif |
+ |
+#ifdef SQLITE_ENABLE_STAT4 |
+ { |
+ tRowcnt nLt = p->current.anLt[p->nCol-1]; |
+ |
+ /* Check if this is to be a periodic sample. If so, add it. */ |
+ if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){ |
+ p->current.isPSample = 1; |
+ p->current.iCol = 0; |
+ sampleInsert(p, &p->current, p->nCol-1); |
+ p->current.isPSample = 0; |
+ } |
+ |
+ /* Update the aBest[] array. */ |
+ for(i=0; i<(p->nCol-1); i++){ |
+ p->current.iCol = i; |
+ if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){ |
+ sampleCopy(p, &p->aBest[i], &p->current); |
+ } |
+ } |
+ } |
+#endif |
+} |
+static const FuncDef statPushFuncdef = { |
+ 2+IsStat34, /* nArg */ |
+ SQLITE_UTF8, /* funcFlags */ |
+ 0, /* pUserData */ |
+ 0, /* pNext */ |
+ statPush, /* xSFunc */ |
+ 0, /* xFinalize */ |
+ "stat_push", /* zName */ |
+ {0} |
+}; |
+ |
+#define STAT_GET_STAT1 0 /* "stat" column of stat1 table */ |
+#define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */ |
+#define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */ |
+#define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */ |
+#define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */ |
+ |
+/* |
+** Implementation of the stat_get(P,J) SQL function. This routine is |
+** used to query statistical information that has been gathered into |
+** the Stat4Accum object by prior calls to stat_push(). The P parameter |
+** has type BLOB but it is really just a pointer to the Stat4Accum object. |
+** The content to returned is determined by the parameter J |
+** which is one of the STAT_GET_xxxx values defined above. |
+** |
+** The stat_get(P,J) function is not available to generic SQL. It is |
+** inserted as part of a manually constructed bytecode program. (See |
+** the callStatGet() routine below.) It is guaranteed that the P |
+** parameter will always be a poiner to a Stat4Accum object, never a |
+** NULL. |
+** |
+** If neither STAT3 nor STAT4 are enabled, then J is always |
+** STAT_GET_STAT1 and is hence omitted and this routine becomes |
+** a one-parameter function, stat_get(P), that always returns the |
+** stat1 table entry information. |
+*/ |
+static void statGet( |
+ sqlite3_context *context, |
+ int argc, |
+ sqlite3_value **argv |
+){ |
+ Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]); |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ /* STAT3 and STAT4 have a parameter on this routine. */ |
+ int eCall = sqlite3_value_int(argv[1]); |
+ assert( argc==2 ); |
+ assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ |
+ || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT |
+ || eCall==STAT_GET_NDLT |
+ ); |
+ if( eCall==STAT_GET_STAT1 ) |
+#else |
+ assert( argc==1 ); |
+#endif |
+ { |
+ /* Return the value to store in the "stat" column of the sqlite_stat1 |
+ ** table for this index. |
+ ** |
+ ** The value is a string composed of a list of integers describing |
+ ** the index. The first integer in the list is the total number of |
+ ** entries in the index. There is one additional integer in the list |
+ ** for each indexed column. This additional integer is an estimate of |
+ ** the number of rows matched by a stabbing query on the index using |
+ ** a key with the corresponding number of fields. In other words, |
+ ** if the index is on columns (a,b) and the sqlite_stat1 value is |
+ ** "100 10 2", then SQLite estimates that: |
+ ** |
+ ** * the index contains 100 rows, |
+ ** * "WHERE a=?" matches 10 rows, and |
+ ** * "WHERE a=? AND b=?" matches 2 rows. |
+ ** |
+ ** If D is the count of distinct values and K is the total number of |
+ ** rows, then each estimate is computed as: |
+ ** |
+ ** I = (K+D-1)/D |
+ */ |
+ char *z; |
+ int i; |
+ |
+ char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 ); |
+ if( zRet==0 ){ |
+ sqlite3_result_error_nomem(context); |
+ return; |
+ } |
+ |
+ sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow); |
+ z = zRet + sqlite3Strlen30(zRet); |
+ for(i=0; i<p->nKeyCol; i++){ |
+ u64 nDistinct = p->current.anDLt[i] + 1; |
+ u64 iVal = (p->nRow + nDistinct - 1) / nDistinct; |
+ sqlite3_snprintf(24, z, " %llu", iVal); |
+ z += sqlite3Strlen30(z); |
+ assert( p->current.anEq[i] ); |
+ } |
+ assert( z[0]=='\0' && z>zRet ); |
+ |
+ sqlite3_result_text(context, zRet, -1, sqlite3_free); |
+ } |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ else if( eCall==STAT_GET_ROWID ){ |
+ if( p->iGet<0 ){ |
+ samplePushPrevious(p, 0); |
+ p->iGet = 0; |
+ } |
+ if( p->iGet<p->nSample ){ |
+ Stat4Sample *pS = p->a + p->iGet; |
+ if( pS->nRowid==0 ){ |
+ sqlite3_result_int64(context, pS->u.iRowid); |
+ }else{ |
+ sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid, |
+ SQLITE_TRANSIENT); |
+ } |
+ } |
+ }else{ |
+ tRowcnt *aCnt = 0; |
+ |
+ assert( p->iGet<p->nSample ); |
+ switch( eCall ){ |
+ case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break; |
+ case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break; |
+ default: { |
+ aCnt = p->a[p->iGet].anDLt; |
+ p->iGet++; |
+ break; |
+ } |
+ } |
+ |
+ if( IsStat3 ){ |
+ sqlite3_result_int64(context, (i64)aCnt[0]); |
+ }else{ |
+ char *zRet = sqlite3MallocZero(p->nCol * 25); |
+ if( zRet==0 ){ |
+ sqlite3_result_error_nomem(context); |
+ }else{ |
+ int i; |
+ char *z = zRet; |
+ for(i=0; i<p->nCol; i++){ |
+ sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]); |
+ z += sqlite3Strlen30(z); |
+ } |
+ assert( z[0]=='\0' && z>zRet ); |
+ z[-1] = '\0'; |
+ sqlite3_result_text(context, zRet, -1, sqlite3_free); |
+ } |
+ } |
+ } |
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
+#ifndef SQLITE_DEBUG |
+ UNUSED_PARAMETER( argc ); |
+#endif |
+} |
+static const FuncDef statGetFuncdef = { |
+ 1+IsStat34, /* nArg */ |
+ SQLITE_UTF8, /* funcFlags */ |
+ 0, /* pUserData */ |
+ 0, /* pNext */ |
+ statGet, /* xSFunc */ |
+ 0, /* xFinalize */ |
+ "stat_get", /* zName */ |
+ {0} |
+}; |
+ |
+static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){ |
+ assert( regOut!=regStat4 && regOut!=regStat4+1 ); |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1); |
+#elif SQLITE_DEBUG |
+ assert( iParam==STAT_GET_STAT1 ); |
+#else |
+ UNUSED_PARAMETER( iParam ); |
+#endif |
+ sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4, regOut, |
+ (char*)&statGetFuncdef, P4_FUNCDEF); |
+ sqlite3VdbeChangeP5(v, 1 + IsStat34); |
+} |
+ |
+/* |
+** Generate code to do an analysis of all indices associated with |
+** a single table. |
+*/ |
+static void analyzeOneTable( |
+ Parse *pParse, /* Parser context */ |
+ Table *pTab, /* Table whose indices are to be analyzed */ |
+ Index *pOnlyIdx, /* If not NULL, only analyze this one index */ |
+ int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */ |
+ int iMem, /* Available memory locations begin here */ |
+ int iTab /* Next available cursor */ |
+){ |
+ sqlite3 *db = pParse->db; /* Database handle */ |
+ Index *pIdx; /* An index to being analyzed */ |
+ int iIdxCur; /* Cursor open on index being analyzed */ |
+ int iTabCur; /* Table cursor */ |
+ Vdbe *v; /* The virtual machine being built up */ |
+ int i; /* Loop counter */ |
+ int jZeroRows = -1; /* Jump from here if number of rows is zero */ |
+ int iDb; /* Index of database containing pTab */ |
+ u8 needTableCnt = 1; /* True to count the table */ |
+ int regNewRowid = iMem++; /* Rowid for the inserted record */ |
+ int regStat4 = iMem++; /* Register to hold Stat4Accum object */ |
+ int regChng = iMem++; /* Index of changed index field */ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ int regRowid = iMem++; /* Rowid argument passed to stat_push() */ |
+#endif |
+ int regTemp = iMem++; /* Temporary use register */ |
+ int regTabname = iMem++; /* Register containing table name */ |
+ int regIdxname = iMem++; /* Register containing index name */ |
+ int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */ |
+ int regPrev = iMem; /* MUST BE LAST (see below) */ |
+ |
+ pParse->nMem = MAX(pParse->nMem, iMem); |
+ v = sqlite3GetVdbe(pParse); |
+ if( v==0 || NEVER(pTab==0) ){ |
+ return; |
+ } |
+ if( pTab->tnum==0 ){ |
+ /* Do not gather statistics on views or virtual tables */ |
+ return; |
+ } |
+ if( sqlite3_strlike("sqlite_%", pTab->zName, 0)==0 ){ |
+ /* Do not gather statistics on system tables */ |
+ return; |
+ } |
+ assert( sqlite3BtreeHoldsAllMutexes(db) ); |
+ iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
+ assert( iDb>=0 ); |
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
+#ifndef SQLITE_OMIT_AUTHORIZATION |
+ if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0, |
+ db->aDb[iDb].zDbSName ) ){ |
+ return; |
+ } |
+#endif |
+ |
+ /* Establish a read-lock on the table at the shared-cache level. |
+ ** Open a read-only cursor on the table. Also allocate a cursor number |
+ ** to use for scanning indexes (iIdxCur). No index cursor is opened at |
+ ** this time though. */ |
+ sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
+ iTabCur = iTab++; |
+ iIdxCur = iTab++; |
+ pParse->nTab = MAX(pParse->nTab, iTab); |
+ sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead); |
+ sqlite3VdbeLoadString(v, regTabname, pTab->zName); |
+ |
+ for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
+ int nCol; /* Number of columns in pIdx. "N" */ |
+ int addrRewind; /* Address of "OP_Rewind iIdxCur" */ |
+ int addrNextRow; /* Address of "next_row:" */ |
+ const char *zIdxName; /* Name of the index */ |
+ int nColTest; /* Number of columns to test for changes */ |
+ |
+ if( pOnlyIdx && pOnlyIdx!=pIdx ) continue; |
+ if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0; |
+ if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){ |
+ nCol = pIdx->nKeyCol; |
+ zIdxName = pTab->zName; |
+ nColTest = nCol - 1; |
+ }else{ |
+ nCol = pIdx->nColumn; |
+ zIdxName = pIdx->zName; |
+ nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1; |
+ } |
+ |
+ /* Populate the register containing the index name. */ |
+ sqlite3VdbeLoadString(v, regIdxname, zIdxName); |
+ VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName)); |
+ |
+ /* |
+ ** Pseudo-code for loop that calls stat_push(): |
+ ** |
+ ** Rewind csr |
+ ** if eof(csr) goto end_of_scan; |
+ ** regChng = 0 |
+ ** goto chng_addr_0; |
+ ** |
+ ** next_row: |
+ ** regChng = 0 |
+ ** if( idx(0) != regPrev(0) ) goto chng_addr_0 |
+ ** regChng = 1 |
+ ** if( idx(1) != regPrev(1) ) goto chng_addr_1 |
+ ** ... |
+ ** regChng = N |
+ ** goto chng_addr_N |
+ ** |
+ ** chng_addr_0: |
+ ** regPrev(0) = idx(0) |
+ ** chng_addr_1: |
+ ** regPrev(1) = idx(1) |
+ ** ... |
+ ** |
+ ** endDistinctTest: |
+ ** regRowid = idx(rowid) |
+ ** stat_push(P, regChng, regRowid) |
+ ** Next csr |
+ ** if !eof(csr) goto next_row; |
+ ** |
+ ** end_of_scan: |
+ */ |
+ |
+ /* Make sure there are enough memory cells allocated to accommodate |
+ ** the regPrev array and a trailing rowid (the rowid slot is required |
+ ** when building a record to insert into the sample column of |
+ ** the sqlite_stat4 table. */ |
+ pParse->nMem = MAX(pParse->nMem, regPrev+nColTest); |
+ |
+ /* Open a read-only cursor on the index being analyzed. */ |
+ assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) ); |
+ sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb); |
+ sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
+ VdbeComment((v, "%s", pIdx->zName)); |
+ |
+ /* Invoke the stat_init() function. The arguments are: |
+ ** |
+ ** (1) the number of columns in the index including the rowid |
+ ** (or for a WITHOUT ROWID table, the number of PK columns), |
+ ** (2) the number of columns in the key without the rowid/pk |
+ ** (3) the number of rows in the index, |
+ ** |
+ ** |
+ ** The third argument is only used for STAT3 and STAT4 |
+ */ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3); |
+#endif |
+ sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1); |
+ sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2); |
+ sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4+1, regStat4, |
+ (char*)&statInitFuncdef, P4_FUNCDEF); |
+ sqlite3VdbeChangeP5(v, 2+IsStat34); |
+ |
+ /* Implementation of the following: |
+ ** |
+ ** Rewind csr |
+ ** if eof(csr) goto end_of_scan; |
+ ** regChng = 0 |
+ ** goto next_push_0; |
+ ** |
+ */ |
+ addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur); |
+ VdbeCoverage(v); |
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng); |
+ addrNextRow = sqlite3VdbeCurrentAddr(v); |
+ |
+ if( nColTest>0 ){ |
+ int endDistinctTest = sqlite3VdbeMakeLabel(v); |
+ int *aGotoChng; /* Array of jump instruction addresses */ |
+ aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest); |
+ if( aGotoChng==0 ) continue; |
+ |
+ /* |
+ ** next_row: |
+ ** regChng = 0 |
+ ** if( idx(0) != regPrev(0) ) goto chng_addr_0 |
+ ** regChng = 1 |
+ ** if( idx(1) != regPrev(1) ) goto chng_addr_1 |
+ ** ... |
+ ** regChng = N |
+ ** goto endDistinctTest |
+ */ |
+ sqlite3VdbeAddOp0(v, OP_Goto); |
+ addrNextRow = sqlite3VdbeCurrentAddr(v); |
+ if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){ |
+ /* For a single-column UNIQUE index, once we have found a non-NULL |
+ ** row, we know that all the rest will be distinct, so skip |
+ ** subsequent distinctness tests. */ |
+ sqlite3VdbeAddOp2(v, OP_NotNull, regPrev, endDistinctTest); |
+ VdbeCoverage(v); |
+ } |
+ for(i=0; i<nColTest; i++){ |
+ char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]); |
+ sqlite3VdbeAddOp2(v, OP_Integer, i, regChng); |
+ sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp); |
+ aGotoChng[i] = |
+ sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ); |
+ sqlite3VdbeChangeP5(v, SQLITE_NULLEQ); |
+ VdbeCoverage(v); |
+ } |
+ sqlite3VdbeAddOp2(v, OP_Integer, nColTest, regChng); |
+ sqlite3VdbeGoto(v, endDistinctTest); |
+ |
+ |
+ /* |
+ ** chng_addr_0: |
+ ** regPrev(0) = idx(0) |
+ ** chng_addr_1: |
+ ** regPrev(1) = idx(1) |
+ ** ... |
+ */ |
+ sqlite3VdbeJumpHere(v, addrNextRow-1); |
+ for(i=0; i<nColTest; i++){ |
+ sqlite3VdbeJumpHere(v, aGotoChng[i]); |
+ sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i); |
+ } |
+ sqlite3VdbeResolveLabel(v, endDistinctTest); |
+ sqlite3DbFree(db, aGotoChng); |
+ } |
+ |
+ /* |
+ ** chng_addr_N: |
+ ** regRowid = idx(rowid) // STAT34 only |
+ ** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only |
+ ** Next csr |
+ ** if !eof(csr) goto next_row; |
+ */ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ assert( regRowid==(regStat4+2) ); |
+ if( HasRowid(pTab) ){ |
+ sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid); |
+ }else{ |
+ Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); |
+ int j, k, regKey; |
+ regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol); |
+ for(j=0; j<pPk->nKeyCol; j++){ |
+ k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]); |
+ assert( k>=0 && k<pTab->nCol ); |
+ sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j); |
+ VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName)); |
+ } |
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid); |
+ sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol); |
+ } |
+#endif |
+ assert( regChng==(regStat4+1) ); |
+ sqlite3VdbeAddOp4(v, OP_Function0, 1, regStat4, regTemp, |
+ (char*)&statPushFuncdef, P4_FUNCDEF); |
+ sqlite3VdbeChangeP5(v, 2+IsStat34); |
+ sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v); |
+ |
+ /* Add the entry to the stat1 table. */ |
+ callStatGet(v, regStat4, STAT_GET_STAT1, regStat1); |
+ assert( "BBB"[0]==SQLITE_AFF_TEXT ); |
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0); |
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); |
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid); |
+ sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
+ |
+ /* Add the entries to the stat3 or stat4 table. */ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ { |
+ int regEq = regStat1; |
+ int regLt = regStat1+1; |
+ int regDLt = regStat1+2; |
+ int regSample = regStat1+3; |
+ int regCol = regStat1+4; |
+ int regSampleRowid = regCol + nCol; |
+ int addrNext; |
+ int addrIsNull; |
+ u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound; |
+ |
+ pParse->nMem = MAX(pParse->nMem, regCol+nCol); |
+ |
+ addrNext = sqlite3VdbeCurrentAddr(v); |
+ callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid); |
+ addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid); |
+ VdbeCoverage(v); |
+ callStatGet(v, regStat4, STAT_GET_NEQ, regEq); |
+ callStatGet(v, regStat4, STAT_GET_NLT, regLt); |
+ callStatGet(v, regStat4, STAT_GET_NDLT, regDLt); |
+ sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0); |
+ /* We know that the regSampleRowid row exists because it was read by |
+ ** the previous loop. Thus the not-found jump of seekOp will never |
+ ** be taken */ |
+ VdbeCoverageNeverTaken(v); |
+#ifdef SQLITE_ENABLE_STAT3 |
+ sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, 0, regSample); |
+#else |
+ for(i=0; i<nCol; i++){ |
+ sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i); |
+ } |
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample); |
+#endif |
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp); |
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid); |
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid); |
+ sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */ |
+ sqlite3VdbeJumpHere(v, addrIsNull); |
+ } |
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
+ |
+ /* End of analysis */ |
+ sqlite3VdbeJumpHere(v, addrRewind); |
+ } |
+ |
+ |
+ /* Create a single sqlite_stat1 entry containing NULL as the index |
+ ** name and the row count as the content. |
+ */ |
+ if( pOnlyIdx==0 && needTableCnt ){ |
+ VdbeComment((v, "%s", pTab->zName)); |
+ sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1); |
+ jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v); |
+ sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname); |
+ assert( "BBB"[0]==SQLITE_AFF_TEXT ); |
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0); |
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); |
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid); |
+ sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
+ sqlite3VdbeJumpHere(v, jZeroRows); |
+ } |
+} |
+ |
+ |
+/* |
+** Generate code that will cause the most recent index analysis to |
+** be loaded into internal hash tables where is can be used. |
+*/ |
+static void loadAnalysis(Parse *pParse, int iDb){ |
+ Vdbe *v = sqlite3GetVdbe(pParse); |
+ if( v ){ |
+ sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb); |
+ } |
+} |
+ |
+/* |
+** Generate code that will do an analysis of an entire database |
+*/ |
+static void analyzeDatabase(Parse *pParse, int iDb){ |
+ sqlite3 *db = pParse->db; |
+ Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */ |
+ HashElem *k; |
+ int iStatCur; |
+ int iMem; |
+ int iTab; |
+ |
+ sqlite3BeginWriteOperation(pParse, 0, iDb); |
+ iStatCur = pParse->nTab; |
+ pParse->nTab += 3; |
+ openStatTable(pParse, iDb, iStatCur, 0, 0); |
+ iMem = pParse->nMem+1; |
+ iTab = pParse->nTab; |
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
+ for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ |
+ Table *pTab = (Table*)sqliteHashData(k); |
+ analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab); |
+ } |
+ loadAnalysis(pParse, iDb); |
+} |
+ |
+/* |
+** Generate code that will do an analysis of a single table in |
+** a database. If pOnlyIdx is not NULL then it is a single index |
+** in pTab that should be analyzed. |
+*/ |
+static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){ |
+ int iDb; |
+ int iStatCur; |
+ |
+ assert( pTab!=0 ); |
+ assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
+ iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
+ sqlite3BeginWriteOperation(pParse, 0, iDb); |
+ iStatCur = pParse->nTab; |
+ pParse->nTab += 3; |
+ if( pOnlyIdx ){ |
+ openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx"); |
+ }else{ |
+ openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl"); |
+ } |
+ analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab); |
+ loadAnalysis(pParse, iDb); |
+} |
+ |
+/* |
+** Generate code for the ANALYZE command. The parser calls this routine |
+** when it recognizes an ANALYZE command. |
+** |
+** ANALYZE -- 1 |
+** ANALYZE <database> -- 2 |
+** ANALYZE ?<database>.?<tablename> -- 3 |
+** |
+** Form 1 causes all indices in all attached databases to be analyzed. |
+** Form 2 analyzes all indices the single database named. |
+** Form 3 analyzes all indices associated with the named table. |
+*/ |
+SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){ |
+ sqlite3 *db = pParse->db; |
+ int iDb; |
+ int i; |
+ char *z, *zDb; |
+ Table *pTab; |
+ Index *pIdx; |
+ Token *pTableName; |
+ Vdbe *v; |
+ |
+ /* Read the database schema. If an error occurs, leave an error message |
+ ** and code in pParse and return NULL. */ |
+ assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); |
+ if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
+ return; |
+ } |
+ |
+ assert( pName2!=0 || pName1==0 ); |
+ if( pName1==0 ){ |
+ /* Form 1: Analyze everything */ |
+ for(i=0; i<db->nDb; i++){ |
+ if( i==1 ) continue; /* Do not analyze the TEMP database */ |
+ analyzeDatabase(pParse, i); |
+ } |
+ }else if( pName2->n==0 ){ |
+ /* Form 2: Analyze the database or table named */ |
+ iDb = sqlite3FindDb(db, pName1); |
+ if( iDb>=0 ){ |
+ analyzeDatabase(pParse, iDb); |
+ }else{ |
+ z = sqlite3NameFromToken(db, pName1); |
+ if( z ){ |
+ if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){ |
+ analyzeTable(pParse, pIdx->pTable, pIdx); |
+ }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){ |
+ analyzeTable(pParse, pTab, 0); |
+ } |
+ sqlite3DbFree(db, z); |
+ } |
+ } |
+ }else{ |
+ /* Form 3: Analyze the fully qualified table name */ |
+ iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName); |
+ if( iDb>=0 ){ |
+ zDb = db->aDb[iDb].zDbSName; |
+ z = sqlite3NameFromToken(db, pTableName); |
+ if( z ){ |
+ if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){ |
+ analyzeTable(pParse, pIdx->pTable, pIdx); |
+ }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){ |
+ analyzeTable(pParse, pTab, 0); |
+ } |
+ sqlite3DbFree(db, z); |
+ } |
+ } |
+ } |
+ v = sqlite3GetVdbe(pParse); |
+ if( v ) sqlite3VdbeAddOp0(v, OP_Expire); |
+} |
+ |
+/* |
+** Used to pass information from the analyzer reader through to the |
+** callback routine. |
+*/ |
+typedef struct analysisInfo analysisInfo; |
+struct analysisInfo { |
+ sqlite3 *db; |
+ const char *zDatabase; |
+}; |
+ |
+/* |
+** The first argument points to a nul-terminated string containing a |
+** list of space separated integers. Read the first nOut of these into |
+** the array aOut[]. |
+*/ |
+static void decodeIntArray( |
+ char *zIntArray, /* String containing int array to decode */ |
+ int nOut, /* Number of slots in aOut[] */ |
+ tRowcnt *aOut, /* Store integers here */ |
+ LogEst *aLog, /* Or, if aOut==0, here */ |
+ Index *pIndex /* Handle extra flags for this index, if not NULL */ |
+){ |
+ char *z = zIntArray; |
+ int c; |
+ int i; |
+ tRowcnt v; |
+ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ if( z==0 ) z = ""; |
+#else |
+ assert( z!=0 ); |
+#endif |
+ for(i=0; *z && i<nOut; i++){ |
+ v = 0; |
+ while( (c=z[0])>='0' && c<='9' ){ |
+ v = v*10 + c - '0'; |
+ z++; |
+ } |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ if( aOut ) aOut[i] = v; |
+ if( aLog ) aLog[i] = sqlite3LogEst(v); |
+#else |
+ assert( aOut==0 ); |
+ UNUSED_PARAMETER(aOut); |
+ assert( aLog!=0 ); |
+ aLog[i] = sqlite3LogEst(v); |
+#endif |
+ if( *z==' ' ) z++; |
+ } |
+#ifndef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ assert( pIndex!=0 ); { |
+#else |
+ if( pIndex ){ |
+#endif |
+ pIndex->bUnordered = 0; |
+ pIndex->noSkipScan = 0; |
+ while( z[0] ){ |
+ if( sqlite3_strglob("unordered*", z)==0 ){ |
+ pIndex->bUnordered = 1; |
+ }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){ |
+ pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3)); |
+ }else if( sqlite3_strglob("noskipscan*", z)==0 ){ |
+ pIndex->noSkipScan = 1; |
+ } |
+#ifdef SQLITE_ENABLE_COSTMULT |
+ else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){ |
+ pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9)); |
+ } |
+#endif |
+ while( z[0]!=0 && z[0]!=' ' ) z++; |
+ while( z[0]==' ' ) z++; |
+ } |
+ } |
+} |
+ |
+/* |
+** This callback is invoked once for each index when reading the |
+** sqlite_stat1 table. |
+** |
+** argv[0] = name of the table |
+** argv[1] = name of the index (might be NULL) |
+** argv[2] = results of analysis - on integer for each column |
+** |
+** Entries for which argv[1]==NULL simply record the number of rows in |
+** the table. |
+*/ |
+static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ |
+ analysisInfo *pInfo = (analysisInfo*)pData; |
+ Index *pIndex; |
+ Table *pTable; |
+ const char *z; |
+ |
+ assert( argc==3 ); |
+ UNUSED_PARAMETER2(NotUsed, argc); |
+ |
+ if( argv==0 || argv[0]==0 || argv[2]==0 ){ |
+ return 0; |
+ } |
+ pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase); |
+ if( pTable==0 ){ |
+ return 0; |
+ } |
+ if( argv[1]==0 ){ |
+ pIndex = 0; |
+ }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){ |
+ pIndex = sqlite3PrimaryKeyIndex(pTable); |
+ }else{ |
+ pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase); |
+ } |
+ z = argv[2]; |
+ |
+ if( pIndex ){ |
+ tRowcnt *aiRowEst = 0; |
+ int nCol = pIndex->nKeyCol+1; |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ /* Index.aiRowEst may already be set here if there are duplicate |
+ ** sqlite_stat1 entries for this index. In that case just clobber |
+ ** the old data with the new instead of allocating a new array. */ |
+ if( pIndex->aiRowEst==0 ){ |
+ pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol); |
+ if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db); |
+ } |
+ aiRowEst = pIndex->aiRowEst; |
+#endif |
+ pIndex->bUnordered = 0; |
+ decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex); |
+ if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0]; |
+ }else{ |
+ Index fakeIdx; |
+ fakeIdx.szIdxRow = pTable->szTabRow; |
+#ifdef SQLITE_ENABLE_COSTMULT |
+ fakeIdx.pTable = pTable; |
+#endif |
+ decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx); |
+ pTable->szTabRow = fakeIdx.szIdxRow; |
+ } |
+ |
+ return 0; |
+} |
+ |
+/* |
+** If the Index.aSample variable is not NULL, delete the aSample[] array |
+** and its contents. |
+*/ |
+SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ if( pIdx->aSample ){ |
+ int j; |
+ for(j=0; j<pIdx->nSample; j++){ |
+ IndexSample *p = &pIdx->aSample[j]; |
+ sqlite3DbFree(db, p->p); |
+ } |
+ sqlite3DbFree(db, pIdx->aSample); |
+ } |
+ if( db && db->pnBytesFreed==0 ){ |
+ pIdx->nSample = 0; |
+ pIdx->aSample = 0; |
+ } |
+#else |
+ UNUSED_PARAMETER(db); |
+ UNUSED_PARAMETER(pIdx); |
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
+} |
+ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+/* |
+** Populate the pIdx->aAvgEq[] array based on the samples currently |
+** stored in pIdx->aSample[]. |
+*/ |
+static void initAvgEq(Index *pIdx){ |
+ if( pIdx ){ |
+ IndexSample *aSample = pIdx->aSample; |
+ IndexSample *pFinal = &aSample[pIdx->nSample-1]; |
+ int iCol; |
+ int nCol = 1; |
+ if( pIdx->nSampleCol>1 ){ |
+ /* If this is stat4 data, then calculate aAvgEq[] values for all |
+ ** sample columns except the last. The last is always set to 1, as |
+ ** once the trailing PK fields are considered all index keys are |
+ ** unique. */ |
+ nCol = pIdx->nSampleCol-1; |
+ pIdx->aAvgEq[nCol] = 1; |
+ } |
+ for(iCol=0; iCol<nCol; iCol++){ |
+ int nSample = pIdx->nSample; |
+ int i; /* Used to iterate through samples */ |
+ tRowcnt sumEq = 0; /* Sum of the nEq values */ |
+ tRowcnt avgEq = 0; |
+ tRowcnt nRow; /* Number of rows in index */ |
+ i64 nSum100 = 0; /* Number of terms contributing to sumEq */ |
+ i64 nDist100; /* Number of distinct values in index */ |
+ |
+ if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){ |
+ nRow = pFinal->anLt[iCol]; |
+ nDist100 = (i64)100 * pFinal->anDLt[iCol]; |
+ nSample--; |
+ }else{ |
+ nRow = pIdx->aiRowEst[0]; |
+ nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1]; |
+ } |
+ pIdx->nRowEst0 = nRow; |
+ |
+ /* Set nSum to the number of distinct (iCol+1) field prefixes that |
+ ** occur in the stat4 table for this index. Set sumEq to the sum of |
+ ** the nEq values for column iCol for the same set (adding the value |
+ ** only once where there exist duplicate prefixes). */ |
+ for(i=0; i<nSample; i++){ |
+ if( i==(pIdx->nSample-1) |
+ || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] |
+ ){ |
+ sumEq += aSample[i].anEq[iCol]; |
+ nSum100 += 100; |
+ } |
+ } |
+ |
+ if( nDist100>nSum100 && sumEq<nRow ){ |
+ avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100); |
+ } |
+ if( avgEq==0 ) avgEq = 1; |
+ pIdx->aAvgEq[iCol] = avgEq; |
+ } |
+ } |
+} |
+ |
+/* |
+** Look up an index by name. Or, if the name of a WITHOUT ROWID table |
+** is supplied instead, find the PRIMARY KEY index for that table. |
+*/ |
+static Index *findIndexOrPrimaryKey( |
+ sqlite3 *db, |
+ const char *zName, |
+ const char *zDb |
+){ |
+ Index *pIdx = sqlite3FindIndex(db, zName, zDb); |
+ if( pIdx==0 ){ |
+ Table *pTab = sqlite3FindTable(db, zName, zDb); |
+ if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab); |
+ } |
+ return pIdx; |
+} |
+ |
+/* |
+** Load the content from either the sqlite_stat4 or sqlite_stat3 table |
+** into the relevant Index.aSample[] arrays. |
+** |
+** Arguments zSql1 and zSql2 must point to SQL statements that return |
+** data equivalent to the following (statements are different for stat3, |
+** see the caller of this function for details): |
+** |
+** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx |
+** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4 |
+** |
+** where %Q is replaced with the database name before the SQL is executed. |
+*/ |
+static int loadStatTbl( |
+ sqlite3 *db, /* Database handle */ |
+ int bStat3, /* Assume single column records only */ |
+ const char *zSql1, /* SQL statement 1 (see above) */ |
+ const char *zSql2, /* SQL statement 2 (see above) */ |
+ const char *zDb /* Database name (e.g. "main") */ |
+){ |
+ int rc; /* Result codes from subroutines */ |
+ sqlite3_stmt *pStmt = 0; /* An SQL statement being run */ |
+ char *zSql; /* Text of the SQL statement */ |
+ Index *pPrevIdx = 0; /* Previous index in the loop */ |
+ IndexSample *pSample; /* A slot in pIdx->aSample[] */ |
+ |
+ assert( db->lookaside.bDisable ); |
+ zSql = sqlite3MPrintf(db, zSql1, zDb); |
+ if( !zSql ){ |
+ return SQLITE_NOMEM_BKPT; |
+ } |
+ rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
+ sqlite3DbFree(db, zSql); |
+ if( rc ) return rc; |
+ |
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
+ int nIdxCol = 1; /* Number of columns in stat4 records */ |
+ |
+ char *zIndex; /* Index name */ |
+ Index *pIdx; /* Pointer to the index object */ |
+ int nSample; /* Number of samples */ |
+ int nByte; /* Bytes of space required */ |
+ int i; /* Bytes of space required */ |
+ tRowcnt *pSpace; |
+ |
+ zIndex = (char *)sqlite3_column_text(pStmt, 0); |
+ if( zIndex==0 ) continue; |
+ nSample = sqlite3_column_int(pStmt, 1); |
+ pIdx = findIndexOrPrimaryKey(db, zIndex, zDb); |
+ assert( pIdx==0 || bStat3 || pIdx->nSample==0 ); |
+ /* Index.nSample is non-zero at this point if data has already been |
+ ** loaded from the stat4 table. In this case ignore stat3 data. */ |
+ if( pIdx==0 || pIdx->nSample ) continue; |
+ if( bStat3==0 ){ |
+ assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 ); |
+ if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){ |
+ nIdxCol = pIdx->nKeyCol; |
+ }else{ |
+ nIdxCol = pIdx->nColumn; |
+ } |
+ } |
+ pIdx->nSampleCol = nIdxCol; |
+ nByte = sizeof(IndexSample) * nSample; |
+ nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample; |
+ nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */ |
+ |
+ pIdx->aSample = sqlite3DbMallocZero(db, nByte); |
+ if( pIdx->aSample==0 ){ |
+ sqlite3_finalize(pStmt); |
+ return SQLITE_NOMEM_BKPT; |
+ } |
+ pSpace = (tRowcnt*)&pIdx->aSample[nSample]; |
+ pIdx->aAvgEq = pSpace; pSpace += nIdxCol; |
+ for(i=0; i<nSample; i++){ |
+ pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol; |
+ pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol; |
+ pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol; |
+ } |
+ assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) ); |
+ } |
+ rc = sqlite3_finalize(pStmt); |
+ if( rc ) return rc; |
+ |
+ zSql = sqlite3MPrintf(db, zSql2, zDb); |
+ if( !zSql ){ |
+ return SQLITE_NOMEM_BKPT; |
+ } |
+ rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
+ sqlite3DbFree(db, zSql); |
+ if( rc ) return rc; |
+ |
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
+ char *zIndex; /* Index name */ |
+ Index *pIdx; /* Pointer to the index object */ |
+ int nCol = 1; /* Number of columns in index */ |
+ |
+ zIndex = (char *)sqlite3_column_text(pStmt, 0); |
+ if( zIndex==0 ) continue; |
+ pIdx = findIndexOrPrimaryKey(db, zIndex, zDb); |
+ if( pIdx==0 ) continue; |
+ /* This next condition is true if data has already been loaded from |
+ ** the sqlite_stat4 table. In this case ignore stat3 data. */ |
+ nCol = pIdx->nSampleCol; |
+ if( bStat3 && nCol>1 ) continue; |
+ if( pIdx!=pPrevIdx ){ |
+ initAvgEq(pPrevIdx); |
+ pPrevIdx = pIdx; |
+ } |
+ pSample = &pIdx->aSample[pIdx->nSample]; |
+ decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0); |
+ decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0); |
+ decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0); |
+ |
+ /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer. |
+ ** This is in case the sample record is corrupted. In that case, the |
+ ** sqlite3VdbeRecordCompare() may read up to two varints past the |
+ ** end of the allocated buffer before it realizes it is dealing with |
+ ** a corrupt record. Adding the two 0x00 bytes prevents this from causing |
+ ** a buffer overread. */ |
+ pSample->n = sqlite3_column_bytes(pStmt, 4); |
+ pSample->p = sqlite3DbMallocZero(db, pSample->n + 2); |
+ if( pSample->p==0 ){ |
+ sqlite3_finalize(pStmt); |
+ return SQLITE_NOMEM_BKPT; |
+ } |
+ if( pSample->n ){ |
+ memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n); |
+ } |
+ pIdx->nSample++; |
+ } |
+ rc = sqlite3_finalize(pStmt); |
+ if( rc==SQLITE_OK ) initAvgEq(pPrevIdx); |
+ return rc; |
+} |
+ |
+/* |
+** Load content from the sqlite_stat4 and sqlite_stat3 tables into |
+** the Index.aSample[] arrays of all indices. |
+*/ |
+static int loadStat4(sqlite3 *db, const char *zDb){ |
+ int rc = SQLITE_OK; /* Result codes from subroutines */ |
+ |
+ assert( db->lookaside.bDisable ); |
+ if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){ |
+ rc = loadStatTbl(db, 0, |
+ "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx", |
+ "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4", |
+ zDb |
+ ); |
+ } |
+ |
+ if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){ |
+ rc = loadStatTbl(db, 1, |
+ "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx", |
+ "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3", |
+ zDb |
+ ); |
+ } |
+ |
+ return rc; |
+} |
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
+ |
+/* |
+** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The |
+** contents of sqlite_stat1 are used to populate the Index.aiRowEst[] |
+** arrays. The contents of sqlite_stat3/4 are used to populate the |
+** Index.aSample[] arrays. |
+** |
+** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR |
+** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined |
+** during compilation and the sqlite_stat3/4 table is present, no data is |
+** read from it. |
+** |
+** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the |
+** sqlite_stat4 table is not present in the database, SQLITE_ERROR is |
+** returned. However, in this case, data is read from the sqlite_stat1 |
+** table (if it is present) before returning. |
+** |
+** If an OOM error occurs, this function always sets db->mallocFailed. |
+** This means if the caller does not care about other errors, the return |
+** code may be ignored. |
+*/ |
+SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ |
+ analysisInfo sInfo; |
+ HashElem *i; |
+ char *zSql; |
+ int rc = SQLITE_OK; |
+ |
+ assert( iDb>=0 && iDb<db->nDb ); |
+ assert( db->aDb[iDb].pBt!=0 ); |
+ |
+ /* Clear any prior statistics */ |
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
+ for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ |
+ Index *pIdx = sqliteHashData(i); |
+ pIdx->aiRowLogEst[0] = 0; |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ sqlite3DeleteIndexSamples(db, pIdx); |
+ pIdx->aSample = 0; |
+#endif |
+ } |
+ |
+ /* Load new statistics out of the sqlite_stat1 table */ |
+ sInfo.db = db; |
+ sInfo.zDatabase = db->aDb[iDb].zDbSName; |
+ if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)!=0 ){ |
+ zSql = sqlite3MPrintf(db, |
+ "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase); |
+ if( zSql==0 ){ |
+ rc = SQLITE_NOMEM_BKPT; |
+ }else{ |
+ rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); |
+ sqlite3DbFree(db, zSql); |
+ } |
+ } |
+ |
+ /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */ |
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
+ for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ |
+ Index *pIdx = sqliteHashData(i); |
+ if( pIdx->aiRowLogEst[0]==0 ) sqlite3DefaultRowEst(pIdx); |
+ } |
+ |
+ /* Load the statistics from the sqlite_stat4 table. */ |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){ |
+ db->lookaside.bDisable++; |
+ rc = loadStat4(db, sInfo.zDatabase); |
+ db->lookaside.bDisable--; |
+ } |
+ for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ |
+ Index *pIdx = sqliteHashData(i); |
+ sqlite3_free(pIdx->aiRowEst); |
+ pIdx->aiRowEst = 0; |
+ } |
+#endif |
+ |
+ if( rc==SQLITE_NOMEM ){ |
+ sqlite3OomFault(db); |
+ } |
+ return rc; |
+} |
+ |
+ |
+#endif /* SQLITE_OMIT_ANALYZE */ |
+ |
+/************** End of analyze.c *********************************************/ |
+/************** Begin file attach.c ******************************************/ |
+/* |
+** 2003 April 6 |
+** |
+** 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. |
+** |
+************************************************************************* |
+** This file contains code used to implement the ATTACH and DETACH commands. |
+*/ |
+/* #include "sqliteInt.h" */ |
+ |
+#ifndef SQLITE_OMIT_ATTACH |
+/* |
+** Resolve an expression that was part of an ATTACH or DETACH statement. This |
+** is slightly different from resolving a normal SQL expression, because simple |
+** identifiers are treated as strings, not possible column names or aliases. |
+** |
+** i.e. if the parser sees: |
+** |
+** ATTACH DATABASE abc AS def |
+** |
+** it treats the two expressions as literal strings 'abc' and 'def' instead of |
+** looking for columns of the same name. |
+** |
+** This only applies to the root node of pExpr, so the statement: |
+** |
+** ATTACH DATABASE abc||def AS 'db2' |
+** |
+** will fail because neither abc or def can be resolved. |
+*/ |
+static int resolveAttachExpr(NameContext *pName, Expr *pExpr) |
+{ |
+ int rc = SQLITE_OK; |
+ if( pExpr ){ |
+ if( pExpr->op!=TK_ID ){ |
+ rc = sqlite3ResolveExprNames(pName, pExpr); |
+ }else{ |
+ pExpr->op = TK_STRING; |
+ } |
+ } |
+ return rc; |
+} |
+ |
+/* |
+** An SQL user-function registered to do the work of an ATTACH statement. The |
+** three arguments to the function come directly from an attach statement: |
+** |
+** ATTACH DATABASE x AS y KEY z |
+** |
+** SELECT sqlite_attach(x, y, z) |
+** |
+** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the |
+** third argument. |
+*/ |
+static void attachFunc( |
+ sqlite3_context *context, |
+ int NotUsed, |
+ sqlite3_value **argv |
+){ |
+ int i; |
+ int rc = 0; |
+ sqlite3 *db = sqlite3_context_db_handle(context); |
+ const char *zName; |
+ const char *zFile; |
+ char *zPath = 0; |
+ char *zErr = 0; |
+ unsigned int flags; |
+ Db *aNew; |
+ char *zErrDyn = 0; |
+ sqlite3_vfs *pVfs; |
+ |
+ UNUSED_PARAMETER(NotUsed); |
+ |
+ zFile = (const char *)sqlite3_value_text(argv[0]); |
+ zName = (const char *)sqlite3_value_text(argv[1]); |
+ if( zFile==0 ) zFile = ""; |
+ if( zName==0 ) zName = ""; |
+ |
+ /* Check for the following errors: |
+ ** |
+ ** * Too many attached databases, |
+ ** * Transaction currently open |
+ ** * Specified database name already being used. |
+ */ |
+ if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){ |
+ zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", |
+ db->aLimit[SQLITE_LIMIT_ATTACHED] |
+ ); |
+ goto attach_error; |
+ } |
+ if( !db->autoCommit ){ |
+ zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction"); |
+ goto attach_error; |
+ } |
+ for(i=0; i<db->nDb; i++){ |
+ char *z = db->aDb[i].zDbSName; |
+ assert( z && zName ); |
+ if( sqlite3StrICmp(z, zName)==0 ){ |
+ zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName); |
+ goto attach_error; |
+ } |
+ } |
+ |
+ /* Allocate the new entry in the db->aDb[] array and initialize the schema |
+ ** hash tables. |
+ */ |
+ if( db->aDb==db->aDbStatic ){ |
+ aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 ); |
+ if( aNew==0 ) return; |
+ memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2); |
+ }else{ |
+ aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) ); |
+ if( aNew==0 ) return; |
+ } |
+ db->aDb = aNew; |
+ aNew = &db->aDb[db->nDb]; |
+ memset(aNew, 0, sizeof(*aNew)); |
+ |
+ /* Open the database file. If the btree is successfully opened, use |
+ ** it to obtain the database schema. At this point the schema may |
+ ** or may not be initialized. |
+ */ |
+ flags = db->openFlags; |
+ rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr); |
+ if( rc!=SQLITE_OK ){ |
+ if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); |
+ sqlite3_result_error(context, zErr, -1); |
+ sqlite3_free(zErr); |
+ return; |
+ } |
+ assert( pVfs ); |
+ flags |= SQLITE_OPEN_MAIN_DB; |
+ rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags); |
+ sqlite3_free( zPath ); |
+ db->nDb++; |
+ db->skipBtreeMutex = 0; |
+ if( rc==SQLITE_CONSTRAINT ){ |
+ rc = SQLITE_ERROR; |
+ zErrDyn = sqlite3MPrintf(db, "database is already attached"); |
+ }else if( rc==SQLITE_OK ){ |
+ Pager *pPager; |
+ aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt); |
+ if( !aNew->pSchema ){ |
+ rc = SQLITE_NOMEM_BKPT; |
+ }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){ |
+ zErrDyn = sqlite3MPrintf(db, |
+ "attached databases must use the same text encoding as main database"); |
+ rc = SQLITE_ERROR; |
+ } |
+ sqlite3BtreeEnter(aNew->pBt); |
+ pPager = sqlite3BtreePager(aNew->pBt); |
+ sqlite3PagerLockingMode(pPager, db->dfltLockMode); |
+ sqlite3BtreeSecureDelete(aNew->pBt, |
+ sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) ); |
+#ifndef SQLITE_OMIT_PAGER_PRAGMAS |
+ sqlite3BtreeSetPagerFlags(aNew->pBt, |
+ PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK)); |
+#endif |
+ sqlite3BtreeLeave(aNew->pBt); |
+ } |
+ aNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1; |
+ aNew->zDbSName = sqlite3DbStrDup(db, zName); |
+ if( rc==SQLITE_OK && aNew->zDbSName==0 ){ |
+ rc = SQLITE_NOMEM_BKPT; |
+ } |
+ |
+ |
+#ifdef SQLITE_HAS_CODEC |
+ if( rc==SQLITE_OK ){ |
+ extern int sqlite3CodecAttach(sqlite3*, int, const void*, int); |
+ extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*); |
+ int nKey; |
+ char *zKey; |
+ int t = sqlite3_value_type(argv[2]); |
+ switch( t ){ |
+ case SQLITE_INTEGER: |
+ case SQLITE_FLOAT: |
+ zErrDyn = sqlite3DbStrDup(db, "Invalid key value"); |
+ rc = SQLITE_ERROR; |
+ break; |
+ |
+ case SQLITE_TEXT: |
+ case SQLITE_BLOB: |
+ nKey = sqlite3_value_bytes(argv[2]); |
+ zKey = (char *)sqlite3_value_blob(argv[2]); |
+ rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); |
+ break; |
+ |
+ case SQLITE_NULL: |
+ /* No key specified. Use the key from the main database */ |
+ sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey); |
+ if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){ |
+ rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); |
+ } |
+ break; |
+ } |
+ } |
+#endif |
+ |
+ /* If the file was opened successfully, read the schema for the new database. |
+ ** If this fails, or if opening the file failed, then close the file and |
+ ** remove the entry from the db->aDb[] array. i.e. put everything back the way |
+ ** we found it. |
+ */ |
+ if( rc==SQLITE_OK ){ |
+ sqlite3BtreeEnterAll(db); |
+ rc = sqlite3Init(db, &zErrDyn); |
+ sqlite3BtreeLeaveAll(db); |
+ } |
+#ifdef SQLITE_USER_AUTHENTICATION |
+ if( rc==SQLITE_OK ){ |
+ u8 newAuth = 0; |
+ rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth); |
+ if( newAuth<db->auth.authLevel ){ |
+ rc = SQLITE_AUTH_USER; |
+ } |
+ } |
+#endif |
+ if( rc ){ |
+ int iDb = db->nDb - 1; |
+ assert( iDb>=2 ); |
+ if( db->aDb[iDb].pBt ){ |
+ sqlite3BtreeClose(db->aDb[iDb].pBt); |
+ db->aDb[iDb].pBt = 0; |
+ db->aDb[iDb].pSchema = 0; |
+ } |
+ sqlite3ResetAllSchemasOfConnection(db); |
+ db->nDb = iDb; |
+ if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ |
+ sqlite3OomFault(db); |
+ sqlite3DbFree(db, zErrDyn); |
+ zErrDyn = sqlite3MPrintf(db, "out of memory"); |
+ }else if( zErrDyn==0 ){ |
+ zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile); |
+ } |
+ goto attach_error; |
+ } |
+ |
+ return; |
+ |
+attach_error: |
+ /* Return an error if we get here */ |
+ if( zErrDyn ){ |
+ sqlite3_result_error(context, zErrDyn, -1); |
+ sqlite3DbFree(db, zErrDyn); |
+ } |
+ if( rc ) sqlite3_result_error_code(context, rc); |
+} |
+ |
+/* |
+** An SQL user-function registered to do the work of an DETACH statement. The |
+** three arguments to the function come directly from a detach statement: |
+** |
+** DETACH DATABASE x |
+** |
+** SELECT sqlite_detach(x) |
+*/ |
+static void detachFunc( |
+ sqlite3_context *context, |
+ int NotUsed, |
+ sqlite3_value **argv |
+){ |
+ const char *zName = (const char *)sqlite3_value_text(argv[0]); |
+ sqlite3 *db = sqlite3_context_db_handle(context); |
+ int i; |
+ Db *pDb = 0; |
+ char zErr[128]; |
+ |
+ UNUSED_PARAMETER(NotUsed); |
+ |
+ if( zName==0 ) zName = ""; |
+ for(i=0; i<db->nDb; i++){ |
+ pDb = &db->aDb[i]; |
+ if( pDb->pBt==0 ) continue; |
+ if( sqlite3StrICmp(pDb->zDbSName, zName)==0 ) break; |
+ } |
+ |
+ if( i>=db->nDb ){ |
+ sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName); |
+ goto detach_error; |
+ } |
+ if( i<2 ){ |
+ sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName); |
+ goto detach_error; |
+ } |
+ if( !db->autoCommit ){ |
+ sqlite3_snprintf(sizeof(zErr), zErr, |
+ "cannot DETACH database within transaction"); |
+ goto detach_error; |
+ } |
+ if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){ |
+ sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName); |
+ goto detach_error; |
+ } |
+ |
+ sqlite3BtreeClose(pDb->pBt); |
+ pDb->pBt = 0; |
+ pDb->pSchema = 0; |
+ sqlite3CollapseDatabaseArray(db); |
+ return; |
+ |
+detach_error: |
+ sqlite3_result_error(context, zErr, -1); |
+} |
+ |
+/* |
+** This procedure generates VDBE code for a single invocation of either the |
+** sqlite_detach() or sqlite_attach() SQL user functions. |
+*/ |
+static void codeAttach( |
+ Parse *pParse, /* The parser context */ |
+ int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */ |
+ FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */ |
+ Expr *pAuthArg, /* Expression to pass to authorization callback */ |
+ Expr *pFilename, /* Name of database file */ |
+ Expr *pDbname, /* Name of the database to use internally */ |
+ Expr *pKey /* Database key for encryption extension */ |
+){ |
+ int rc; |
+ NameContext sName; |
+ Vdbe *v; |
+ sqlite3* db = pParse->db; |
+ int regArgs; |
+ |
+ if( pParse->nErr ) goto attach_end; |
+ memset(&sName, 0, sizeof(NameContext)); |
+ sName.pParse = pParse; |
+ |
+ if( |
+ SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) || |
+ SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) || |
+ SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey)) |
+ ){ |
+ goto attach_end; |
+ } |
+ |
+#ifndef SQLITE_OMIT_AUTHORIZATION |
+ if( pAuthArg ){ |
+ char *zAuthArg; |
+ if( pAuthArg->op==TK_STRING ){ |
+ zAuthArg = pAuthArg->u.zToken; |
+ }else{ |
+ zAuthArg = 0; |
+ } |
+ rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0); |
+ if(rc!=SQLITE_OK ){ |
+ goto attach_end; |
+ } |
+ } |
+#endif /* SQLITE_OMIT_AUTHORIZATION */ |
+ |
+ |
+ v = sqlite3GetVdbe(pParse); |
+ regArgs = sqlite3GetTempRange(pParse, 4); |
+ sqlite3ExprCode(pParse, pFilename, regArgs); |
+ sqlite3ExprCode(pParse, pDbname, regArgs+1); |
+ sqlite3ExprCode(pParse, pKey, regArgs+2); |
+ |
+ assert( v || db->mallocFailed ); |
+ if( v ){ |
+ sqlite3VdbeAddOp4(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3, |
+ (char *)pFunc, P4_FUNCDEF); |
+ assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg ); |
+ sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg)); |
+ |
+ /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this |
+ ** statement only). For DETACH, set it to false (expire all existing |
+ ** statements). |
+ */ |
+ sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH)); |
+ } |
+ |
+attach_end: |
+ sqlite3ExprDelete(db, pFilename); |
+ sqlite3ExprDelete(db, pDbname); |
+ sqlite3ExprDelete(db, pKey); |
+} |
+ |
+/* |
+** Called by the parser to compile a DETACH statement. |
+** |
+** DETACH pDbname |
+*/ |
+SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){ |
+ static const FuncDef detach_func = { |
+ 1, /* nArg */ |
+ SQLITE_UTF8, /* funcFlags */ |
+ 0, /* pUserData */ |
+ 0, /* pNext */ |
+ detachFunc, /* xSFunc */ |
+ 0, /* xFinalize */ |
+ "sqlite_detach", /* zName */ |
+ {0} |
+ }; |
+ codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname); |
+} |
+ |
+/* |
+** Called by the parser to compile an ATTACH statement. |
+** |
+** ATTACH p AS pDbname KEY pKey |
+*/ |
+SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){ |
+ static const FuncDef attach_func = { |
+ 3, /* nArg */ |
+ SQLITE_UTF8, /* funcFlags */ |
+ 0, /* pUserData */ |
+ 0, /* pNext */ |
+ attachFunc, /* xSFunc */ |
+ 0, /* xFinalize */ |
+ "sqlite_attach", /* zName */ |
+ {0} |
+ }; |
+ codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey); |
+} |
+#endif /* SQLITE_OMIT_ATTACH */ |
+ |
+/* |
+** Initialize a DbFixer structure. This routine must be called prior |
+** to passing the structure to one of the sqliteFixAAAA() routines below. |
+*/ |
+SQLITE_PRIVATE void sqlite3FixInit( |
+ DbFixer *pFix, /* The fixer to be initialized */ |
+ Parse *pParse, /* Error messages will be written here */ |
+ int iDb, /* This is the database that must be used */ |
+ const char *zType, /* "view", "trigger", or "index" */ |
+ const Token *pName /* Name of the view, trigger, or index */ |
+){ |
+ sqlite3 *db; |
+ |
+ db = pParse->db; |
+ assert( db->nDb>iDb ); |
+ pFix->pParse = pParse; |
+ pFix->zDb = db->aDb[iDb].zDbSName; |
+ pFix->pSchema = db->aDb[iDb].pSchema; |
+ pFix->zType = zType; |
+ pFix->pName = pName; |
+ pFix->bVarOnly = (iDb==1); |
+} |
+ |
+/* |
+** The following set of routines walk through the parse tree and assign |
+** a specific database to all table references where the database name |
+** was left unspecified in the original SQL statement. The pFix structure |
+** must have been initialized by a prior call to sqlite3FixInit(). |
+** |
+** These routines are used to make sure that an index, trigger, or |
+** view in one database does not refer to objects in a different database. |
+** (Exception: indices, triggers, and views in the TEMP database are |
+** allowed to refer to anything.) If a reference is explicitly made |
+** to an object in a different database, an error message is added to |
+** pParse->zErrMsg and these routines return non-zero. If everything |
+** checks out, these routines return 0. |
+*/ |
+SQLITE_PRIVATE int sqlite3FixSrcList( |
+ DbFixer *pFix, /* Context of the fixation */ |
+ SrcList *pList /* The Source list to check and modify */ |
+){ |
+ int i; |
+ const char *zDb; |
+ struct SrcList_item *pItem; |
+ |
+ if( NEVER(pList==0) ) return 0; |
+ zDb = pFix->zDb; |
+ for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ |
+ if( pFix->bVarOnly==0 ){ |
+ if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){ |
+ sqlite3ErrorMsg(pFix->pParse, |
+ "%s %T cannot reference objects in database %s", |
+ pFix->zType, pFix->pName, pItem->zDatabase); |
+ return 1; |
+ } |
+ sqlite3DbFree(pFix->pParse->db, pItem->zDatabase); |
+ pItem->zDatabase = 0; |
+ pItem->pSchema = pFix->pSchema; |
+ } |
+#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) |
+ if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1; |
+ if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1; |
+#endif |
+ } |
+ return 0; |
+} |
+#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) |
+SQLITE_PRIVATE int sqlite3FixSelect( |
+ DbFixer *pFix, /* Context of the fixation */ |
+ Select *pSelect /* The SELECT statement to be fixed to one database */ |
+){ |
+ while( pSelect ){ |
+ if( sqlite3FixExprList(pFix, pSelect->pEList) ){ |
+ return 1; |
+ } |
+ if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){ |
+ return 1; |
+ } |
+ if( sqlite3FixExpr(pFix, pSelect->pWhere) ){ |
+ return 1; |
+ } |
+ if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){ |
+ return 1; |
+ } |
+ if( sqlite3FixExpr(pFix, pSelect->pHaving) ){ |
+ return 1; |
+ } |
+ if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){ |
+ return 1; |
+ } |
+ if( sqlite3FixExpr(pFix, pSelect->pLimit) ){ |
+ return 1; |
+ } |
+ if( sqlite3FixExpr(pFix, pSelect->pOffset) ){ |
+ return 1; |
+ } |
+ pSelect = pSelect->pPrior; |
+ } |
+ return 0; |
+} |
+SQLITE_PRIVATE int sqlite3FixExpr( |
+ DbFixer *pFix, /* Context of the fixation */ |
+ Expr *pExpr /* The expression to be fixed to one database */ |
+){ |
+ while( pExpr ){ |
+ if( pExpr->op==TK_VARIABLE ){ |
+ if( pFix->pParse->db->init.busy ){ |
+ pExpr->op = TK_NULL; |
+ }else{ |
+ sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType); |
+ return 1; |
+ } |
+ } |
+ if( ExprHasProperty(pExpr, EP_TokenOnly|EP_Leaf) ) break; |
+ if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
+ if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1; |
+ }else{ |
+ if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1; |
+ } |
+ if( sqlite3FixExpr(pFix, pExpr->pRight) ){ |
+ return 1; |
+ } |
+ pExpr = pExpr->pLeft; |
+ } |
+ return 0; |
+} |
+SQLITE_PRIVATE int sqlite3FixExprList( |
+ DbFixer *pFix, /* Context of the fixation */ |
+ ExprList *pList /* The expression to be fixed to one database */ |
+){ |
+ int i; |
+ struct ExprList_item *pItem; |
+ if( pList==0 ) return 0; |
+ for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){ |
+ if( sqlite3FixExpr(pFix, pItem->pExpr) ){ |
+ return 1; |
+ } |
+ } |
+ return 0; |
+} |
+#endif |
+ |
+#ifndef SQLITE_OMIT_TRIGGER |
+SQLITE_PRIVATE int sqlite3FixTriggerStep( |
+ DbFixer *pFix, /* Context of the fixation */ |
+ TriggerStep *pStep /* The trigger step be fixed to one database */ |
+){ |
+ while( pStep ){ |
+ if( sqlite3FixSelect(pFix, pStep->pSelect) ){ |
+ return 1; |
+ } |
+ if( sqlite3FixExpr(pFix, pStep->pWhere) ){ |
+ return 1; |
+ } |
+ if( sqlite3FixExprList(pFix, pStep->pExprList) ){ |
+ return 1; |
+ } |
+ pStep = pStep->pNext; |
+ } |
+ return 0; |
+} |
+#endif |
+ |
+/************** End of attach.c **********************************************/ |
+/************** Begin file auth.c ********************************************/ |
+/* |
+** 2003 January 11 |
+** |
+** 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. |
+** |
+************************************************************************* |
+** This file contains code used to implement the sqlite3_set_authorizer() |
+** API. This facility is an optional feature of the library. Embedded |
+** systems that do not need this facility may omit it by recompiling |
+** the library with -DSQLITE_OMIT_AUTHORIZATION=1 |
+*/ |
+/* #include "sqliteInt.h" */ |
+ |
+/* |
+** All of the code in this file may be omitted by defining a single |
+** macro. |
+*/ |
+#ifndef SQLITE_OMIT_AUTHORIZATION |
+ |
+/* |
+** Set or clear the access authorization function. |
+** |
+** The access authorization function is be called during the compilation |
+** phase to verify that the user has read and/or write access permission on |
+** various fields of the database. The first argument to the auth function |
+** is a copy of the 3rd argument to this routine. The second argument |
+** to the auth function is one of these constants: |
+** |
+** SQLITE_CREATE_INDEX |
+** SQLITE_CREATE_TABLE |
+** SQLITE_CREATE_TEMP_INDEX |
+** SQLITE_CREATE_TEMP_TABLE |
+** SQLITE_CREATE_TEMP_TRIGGER |
+** SQLITE_CREATE_TEMP_VIEW |
+** SQLITE_CREATE_TRIGGER |
+** SQLITE_CREATE_VIEW |
+** SQLITE_DELETE |
+** SQLITE_DROP_INDEX |
+** SQLITE_DROP_TABLE |
+** SQLITE_DROP_TEMP_INDEX |
+** SQLITE_DROP_TEMP_TABLE |
+** SQLITE_DROP_TEMP_TRIGGER |
+** SQLITE_DROP_TEMP_VIEW |
+** SQLITE_DROP_TRIGGER |
+** SQLITE_DROP_VIEW |
+** SQLITE_INSERT |
+** SQLITE_PRAGMA |
+** SQLITE_READ |
+** SQLITE_SELECT |
+** SQLITE_TRANSACTION |
+** SQLITE_UPDATE |
+** |
+** The third and fourth arguments to the auth function are the name of |
+** the table and the column that are being accessed. The auth function |
+** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If |
+** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY |
+** means that the SQL statement will never-run - the sqlite3_exec() call |
+** will return with an error. SQLITE_IGNORE means that the SQL statement |
+** should run but attempts to read the specified column will return NULL |
+** and attempts to write the column will be ignored. |
+** |
+** Setting the auth function to NULL disables this hook. The default |
+** setting of the auth function is NULL. |
+*/ |
+SQLITE_API int sqlite3_set_authorizer( |
+ sqlite3 *db, |
+ int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), |
+ void *pArg |
+){ |
+#ifdef SQLITE_ENABLE_API_ARMOR |
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
+#endif |
+ sqlite3_mutex_enter(db->mutex); |
+ db->xAuth = (sqlite3_xauth)xAuth; |
+ db->pAuthArg = pArg; |
+ sqlite3ExpirePreparedStatements(db); |
+ sqlite3_mutex_leave(db->mutex); |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Write an error message into pParse->zErrMsg that explains that the |
+** user-supplied authorization function returned an illegal value. |
+*/ |
+static void sqliteAuthBadReturnCode(Parse *pParse){ |
+ sqlite3ErrorMsg(pParse, "authorizer malfunction"); |
+ pParse->rc = SQLITE_ERROR; |
+} |
+ |
+/* |
+** Invoke the authorization callback for permission to read column zCol from |
+** table zTab in database zDb. This function assumes that an authorization |
+** callback has been registered (i.e. that sqlite3.xAuth is not NULL). |
+** |
+** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed |
+** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE |
+** is treated as SQLITE_DENY. In this case an error is left in pParse. |
+*/ |
+SQLITE_PRIVATE int sqlite3AuthReadCol( |
+ Parse *pParse, /* The parser context */ |
+ const char *zTab, /* Table name */ |
+ const char *zCol, /* Column name */ |
+ int iDb /* Index of containing database. */ |
+){ |
+ sqlite3 *db = pParse->db; /* Database handle */ |
+ char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */ |
+ int rc; /* Auth callback return code */ |
+ |
+ if( db->init.busy ) return SQLITE_OK; |
+ rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext |
+#ifdef SQLITE_USER_AUTHENTICATION |
+ ,db->auth.zAuthUser |
+#endif |
+ ); |
+ if( rc==SQLITE_DENY ){ |
+ if( db->nDb>2 || iDb!=0 ){ |
+ sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol); |
+ }else{ |
+ sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol); |
+ } |
+ pParse->rc = SQLITE_AUTH; |
+ }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){ |
+ sqliteAuthBadReturnCode(pParse); |
+ } |
+ return rc; |
+} |
+ |
+/* |
+** The pExpr should be a TK_COLUMN expression. The table referred to |
+** is in pTabList or else it is the NEW or OLD table of a trigger. |
+** Check to see if it is OK to read this particular column. |
+** |
+** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN |
+** instruction into a TK_NULL. If the auth function returns SQLITE_DENY, |
+** then generate an error. |
+*/ |
+SQLITE_PRIVATE void sqlite3AuthRead( |
+ Parse *pParse, /* The parser context */ |
+ Expr *pExpr, /* The expression to check authorization on */ |
+ Schema *pSchema, /* The schema of the expression */ |
+ SrcList *pTabList /* All table that pExpr might refer to */ |
+){ |
+ sqlite3 *db = pParse->db; |
+ Table *pTab = 0; /* The table being read */ |
+ const char *zCol; /* Name of the column of the table */ |
+ int iSrc; /* Index in pTabList->a[] of table being read */ |
+ int iDb; /* The index of the database the expression refers to */ |
+ int iCol; /* Index of column in table */ |
+ |
+ if( db->xAuth==0 ) return; |
+ iDb = sqlite3SchemaToIndex(pParse->db, pSchema); |
+ if( iDb<0 ){ |
+ /* An attempt to read a column out of a subquery or other |
+ ** temporary table. */ |
+ return; |
+ } |
+ |
+ assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER ); |
+ if( pExpr->op==TK_TRIGGER ){ |
+ pTab = pParse->pTriggerTab; |
+ }else{ |
+ assert( pTabList ); |
+ for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){ |
+ if( pExpr->iTable==pTabList->a[iSrc].iCursor ){ |
+ pTab = pTabList->a[iSrc].pTab; |
+ break; |
+ } |
+ } |
+ } |
+ iCol = pExpr->iColumn; |
+ if( NEVER(pTab==0) ) return; |
+ |
+ if( iCol>=0 ){ |
+ assert( iCol<pTab->nCol ); |
+ zCol = pTab->aCol[iCol].zName; |
+ }else if( pTab->iPKey>=0 ){ |
+ assert( pTab->iPKey<pTab->nCol ); |
+ zCol = pTab->aCol[pTab->iPKey].zName; |
+ }else{ |
+ zCol = "ROWID"; |
+ } |
+ assert( iDb>=0 && iDb<db->nDb ); |
+ if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){ |
+ pExpr->op = TK_NULL; |
+ } |
+} |
+ |
+/* |
+** Do an authorization check using the code and arguments given. Return |
+** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY |
+** is returned, then the error count and error message in pParse are |
+** modified appropriately. |
+*/ |
+SQLITE_PRIVATE int sqlite3AuthCheck( |
+ Parse *pParse, |
+ int code, |
+ const char *zArg1, |
+ const char *zArg2, |
+ const char *zArg3 |
+){ |
+ sqlite3 *db = pParse->db; |
+ int rc; |
+ |
+ /* Don't do any authorization checks if the database is initialising |
+ ** or if the parser is being invoked from within sqlite3_declare_vtab. |
+ */ |
+ if( db->init.busy || IN_DECLARE_VTAB ){ |
+ return SQLITE_OK; |
+ } |
+ |
+ if( db->xAuth==0 ){ |
+ return SQLITE_OK; |
+ } |
+ rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext |
+#ifdef SQLITE_USER_AUTHENTICATION |
+ ,db->auth.zAuthUser |
+#endif |
+ ); |
+ if( rc==SQLITE_DENY ){ |
+ sqlite3ErrorMsg(pParse, "not authorized"); |
+ pParse->rc = SQLITE_AUTH; |
+ }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){ |
+ rc = SQLITE_DENY; |
+ sqliteAuthBadReturnCode(pParse); |
+ } |
+ return rc; |
+} |
+ |
+/* |
+** Push an authorization context. After this routine is called, the |
+** zArg3 argument to authorization callbacks will be zContext until |
+** popped. Or if pParse==0, this routine is a no-op. |
+*/ |
+SQLITE_PRIVATE void sqlite3AuthContextPush( |
+ Parse *pParse, |
+ AuthContext *pContext, |
+ const char *zContext |
+){ |
+ assert( pParse ); |
+ pContext->pParse = pParse; |
+ pContext->zAuthContext = pParse->zAuthContext; |
+ pParse->zAuthContext = zContext; |
+} |
+ |
+/* |
+** Pop an authorization context that was previously pushed |
+** by sqlite3AuthContextPush |
+*/ |
+SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){ |
+ if( pContext->pParse ){ |
+ pContext->pParse->zAuthContext = pContext->zAuthContext; |
+ pContext->pParse = 0; |
+ } |
+} |
+ |
+#endif /* SQLITE_OMIT_AUTHORIZATION */ |
+ |
+/************** End of auth.c ************************************************/ |
+/************** Begin file build.c *******************************************/ |
+/* |
+** 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. |
+** |
+************************************************************************* |
+** This file contains C code routines that are called by the SQLite parser |
+** when syntax rules are reduced. The routines in this file handle the |
+** following kinds of SQL syntax: |
+** |
+** CREATE TABLE |
+** DROP TABLE |
+** CREATE INDEX |
+** DROP INDEX |
+** creating ID lists |
+** BEGIN TRANSACTION |
+** COMMIT |
+** ROLLBACK |
+*/ |
+/* #include "sqliteInt.h" */ |
+ |
+#ifndef SQLITE_OMIT_SHARED_CACHE |
+/* |
+** The TableLock structure is only used by the sqlite3TableLock() and |
+** codeTableLocks() functions. |
+*/ |
+struct TableLock { |
+ int iDb; /* The database containing the table to be locked */ |
+ int iTab; /* The root page of the table to be locked */ |
+ u8 isWriteLock; /* True for write lock. False for a read lock */ |
+ const char *zLockName; /* Name of the table */ |
+}; |
+ |
+/* |
+** Record the fact that we want to lock a table at run-time. |
+** |
+** The table to be locked has root page iTab and is found in database iDb. |
+** A read or a write lock can be taken depending on isWritelock. |
+** |
+** This routine just records the fact that the lock is desired. The |
+** code to make the lock occur is generated by a later call to |
+** codeTableLocks() which occurs during sqlite3FinishCoding(). |
+*/ |
+SQLITE_PRIVATE void sqlite3TableLock( |
+ Parse *pParse, /* Parsing context */ |
+ int iDb, /* Index of the database containing the table to lock */ |
+ int iTab, /* Root page number of the table to be locked */ |
+ u8 isWriteLock, /* True for a write lock */ |
+ const char *zName /* Name of the table to be locked */ |
+){ |
+ Parse *pToplevel = sqlite3ParseToplevel(pParse); |
+ int i; |
+ int nBytes; |
+ TableLock *p; |
+ assert( iDb>=0 ); |
+ |
+ if( iDb==1 ) return; |
+ if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return; |
+ for(i=0; i<pToplevel->nTableLock; i++){ |
+ p = &pToplevel->aTableLock[i]; |
+ if( p->iDb==iDb && p->iTab==iTab ){ |
+ p->isWriteLock = (p->isWriteLock || isWriteLock); |
+ return; |
+ } |
+ } |
+ |
+ nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1); |
+ pToplevel->aTableLock = |
+ sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes); |
+ if( pToplevel->aTableLock ){ |
+ p = &pToplevel->aTableLock[pToplevel->nTableLock++]; |
+ p->iDb = iDb; |
+ p->iTab = iTab; |
+ p->isWriteLock = isWriteLock; |
+ p->zLockName = zName; |
+ }else{ |
+ pToplevel->nTableLock = 0; |
+ sqlite3OomFault(pToplevel->db); |
+ } |
+} |
+ |
+/* |
+** Code an OP_TableLock instruction for each table locked by the |
+** statement (configured by calls to sqlite3TableLock()). |
+*/ |
+static void codeTableLocks(Parse *pParse){ |
+ int i; |
+ Vdbe *pVdbe; |
+ |
+ pVdbe = sqlite3GetVdbe(pParse); |
+ assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */ |
+ |
+ for(i=0; i<pParse->nTableLock; i++){ |
+ TableLock *p = &pParse->aTableLock[i]; |
+ int p1 = p->iDb; |
+ sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock, |
+ p->zLockName, P4_STATIC); |
+ } |
+} |
+#else |
+ #define codeTableLocks(x) |
+#endif |
+ |
+/* |
+** Return TRUE if the given yDbMask object is empty - if it contains no |
+** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero() |
+** macros when SQLITE_MAX_ATTACHED is greater than 30. |
+*/ |
+#if SQLITE_MAX_ATTACHED>30 |
+SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask m){ |
+ int i; |
+ for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0; |
+ return 1; |
+} |
+#endif |
+ |
+/* |
+** This routine is called after a single SQL statement has been |
+** parsed and a VDBE program to execute that statement has been |
+** prepared. This routine puts the finishing touches on the |
+** VDBE program and resets the pParse structure for the next |
+** parse. |
+** |
+** Note that if an error occurred, it might be the case that |
+** no VDBE code was generated. |
+*/ |
+SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){ |
+ sqlite3 *db; |
+ Vdbe *v; |
+ |
+ assert( pParse->pToplevel==0 ); |
+ db = pParse->db; |
+ if( pParse->nested ) return; |
+ if( db->mallocFailed || pParse->nErr ){ |
+ if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR; |
+ return; |
+ } |
+ |
+ /* Begin by generating some termination code at the end of the |
+ ** vdbe program |
+ */ |
+ v = sqlite3GetVdbe(pParse); |
+ assert( !pParse->isMultiWrite |
+ || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort)); |
+ if( v ){ |
+ sqlite3VdbeAddOp0(v, OP_Halt); |
+ |
+#if SQLITE_USER_AUTHENTICATION |
+ if( pParse->nTableLock>0 && db->init.busy==0 ){ |
+ sqlite3UserAuthInit(db); |
+ if( db->auth.authLevel<UAUTH_User ){ |
+ sqlite3ErrorMsg(pParse, "user not authenticated"); |
+ pParse->rc = SQLITE_AUTH_USER; |
+ return; |
+ } |
+ } |
+#endif |
+ |
+ /* The cookie mask contains one bit for each database file open. |
+ ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are |
+ ** set for each database that is used. Generate code to start a |
+ ** transaction on each used database and to verify the schema cookie |
+ ** on each used database. |
+ */ |
+ if( db->mallocFailed==0 |
+ && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr) |
+ ){ |
+ int iDb, i; |
+ assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init ); |
+ sqlite3VdbeJumpHere(v, 0); |
+ for(iDb=0; iDb<db->nDb; iDb++){ |
+ Schema *pSchema; |
+ if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; |
+ sqlite3VdbeUsesBtree(v, iDb); |
+ pSchema = db->aDb[iDb].pSchema; |
+ sqlite3VdbeAddOp4Int(v, |
+ OP_Transaction, /* Opcode */ |
+ iDb, /* P1 */ |
+ DbMaskTest(pParse->writeMask,iDb), /* P2 */ |
+ pSchema->schema_cookie, /* P3 */ |
+ pSchema->iGeneration /* P4 */ |
+ ); |
+ if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); |
+ VdbeComment((v, |
+ "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); |
+ } |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+ for(i=0; i<pParse->nVtabLock; i++){ |
+ char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); |
+ sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); |
+ } |
+ pParse->nVtabLock = 0; |
+#endif |
+ |
+ /* Once all the cookies have been verified and transactions opened, |
+ ** obtain the required table-locks. This is a no-op unless the |
+ ** shared-cache feature is enabled. |
+ */ |
+ codeTableLocks(pParse); |
+ |
+ /* Initialize any AUTOINCREMENT data structures required. |
+ */ |
+ sqlite3AutoincrementBegin(pParse); |
+ |
+ /* Code constant expressions that where factored out of inner loops */ |
+ if( pParse->pConstExpr ){ |
+ ExprList *pEL = pParse->pConstExpr; |
+ pParse->okConstFactor = 0; |
+ for(i=0; i<pEL->nExpr; i++){ |
+ sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg); |
+ } |
+ } |
+ |
+ /* Finally, jump back to the beginning of the executable code. */ |
+ sqlite3VdbeGoto(v, 1); |
+ } |
+ } |
+ |
+ |
+ /* Get the VDBE program ready for execution |
+ */ |
+ if( v && pParse->nErr==0 && !db->mallocFailed ){ |
+ assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */ |
+ /* A minimum of one cursor is required if autoincrement is used |
+ * See ticket [a696379c1f08866] */ |
+ if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1; |
+ sqlite3VdbeMakeReady(v, pParse); |
+ pParse->rc = SQLITE_DONE; |
+ }else{ |
+ pParse->rc = SQLITE_ERROR; |
+ } |
+} |
+ |
+/* |
+** Run the parser and code generator recursively in order to generate |
+** code for the SQL statement given onto the end of the pParse context |
+** currently under construction. When the parser is run recursively |
+** this way, the final OP_Halt is not appended and other initialization |
+** and finalization steps are omitted because those are handling by the |
+** outermost parser. |
+** |
+** Not everything is nestable. This facility is designed to permit |
+** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use |
+** care if you decide to try to use this routine for some other purposes. |
+*/ |
+SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ |
+ va_list ap; |
+ char *zSql; |
+ char *zErrMsg = 0; |
+ sqlite3 *db = pParse->db; |
+ char saveBuf[PARSE_TAIL_SZ]; |
+ |
+ if( pParse->nErr ) return; |
+ assert( pParse->nested<10 ); /* Nesting should only be of limited depth */ |
+ va_start(ap, zFormat); |
+ zSql = sqlite3VMPrintf(db, zFormat, ap); |
+ va_end(ap); |
+ if( zSql==0 ){ |
+ return; /* A malloc must have failed */ |
+ } |
+ pParse->nested++; |
+ memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ); |
+ memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); |
+ sqlite3RunParser(pParse, zSql, &zErrMsg); |
+ sqlite3DbFree(db, zErrMsg); |
+ sqlite3DbFree(db, zSql); |
+ memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ); |
+ pParse->nested--; |
+} |
+ |
+#if SQLITE_USER_AUTHENTICATION |
+/* |
+** Return TRUE if zTable is the name of the system table that stores the |
+** list of users and their access credentials. |
+*/ |
+SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){ |
+ return sqlite3_stricmp(zTable, "sqlite_user")==0; |
+} |
+#endif |
+ |
+/* |
+** Locate the in-memory structure that describes a particular database |
+** table given the name of that table and (optionally) the name of the |
+** database containing the table. Return NULL if not found. |
+** |
+** If zDatabase is 0, all databases are searched for the table and the |
+** first matching table is returned. (No checking for duplicate table |
+** names is done.) The search order is TEMP first, then MAIN, then any |
+** auxiliary databases added using the ATTACH command. |
+** |
+** See also sqlite3LocateTable(). |
+*/ |
+SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ |
+ Table *p = 0; |
+ int i; |
+ |
+ /* All mutexes are required for schema access. Make sure we hold them. */ |
+ assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
+#if SQLITE_USER_AUTHENTICATION |
+ /* Only the admin user is allowed to know that the sqlite_user table |
+ ** exists */ |
+ if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){ |
+ return 0; |
+ } |
+#endif |
+ while(1){ |
+ for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
+ int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
+ if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){ |
+ assert( sqlite3SchemaMutexHeld(db, j, 0) ); |
+ p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName); |
+ if( p ) return p; |
+ } |
+ } |
+ /* Not found. If the name we were looking for was temp.sqlite_master |
+ ** then change the name to sqlite_temp_master and try again. */ |
+ if( sqlite3StrICmp(zName, MASTER_NAME)!=0 ) break; |
+ if( sqlite3_stricmp(zDatabase, db->aDb[1].zDbSName)!=0 ) break; |
+ zName = TEMP_MASTER_NAME; |
+ } |
+ return 0; |
+} |
+ |
+/* |
+** Locate the in-memory structure that describes a particular database |
+** table given the name of that table and (optionally) the name of the |
+** database containing the table. Return NULL if not found. Also leave an |
+** error message in pParse->zErrMsg. |
+** |
+** The difference between this routine and sqlite3FindTable() is that this |
+** routine leaves an error message in pParse->zErrMsg where |
+** sqlite3FindTable() does not. |
+*/ |
+SQLITE_PRIVATE Table *sqlite3LocateTable( |
+ Parse *pParse, /* context in which to report errors */ |
+ u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */ |
+ const char *zName, /* Name of the table we are looking for */ |
+ const char *zDbase /* Name of the database. Might be NULL */ |
+){ |
+ Table *p; |
+ |
+ /* Read the database schema. If an error occurs, leave an error message |
+ ** and code in pParse and return NULL. */ |
+ if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
+ return 0; |
+ } |
+ |
+ p = sqlite3FindTable(pParse->db, zName, zDbase); |
+ if( p==0 ){ |
+ const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table"; |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+ if( sqlite3FindDbName(pParse->db, zDbase)<1 ){ |
+ /* If zName is the not the name of a table in the schema created using |
+ ** CREATE, then check to see if it is the name of an virtual table that |
+ ** can be an eponymous virtual table. */ |
+ Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName); |
+ if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){ |
+ pMod = sqlite3PragmaVtabRegister(pParse->db, zName); |
+ } |
+ if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ |
+ return pMod->pEpoTab; |
+ } |
+ } |
+#endif |
+ if( (flags & LOCATE_NOERR)==0 ){ |
+ if( zDbase ){ |
+ sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); |
+ }else{ |
+ sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); |
+ } |
+ pParse->checkSchema = 1; |
+ } |
+ } |
+ |
+ return p; |
+} |
+ |
+/* |
+** Locate the table identified by *p. |
+** |
+** This is a wrapper around sqlite3LocateTable(). The difference between |
+** sqlite3LocateTable() and this function is that this function restricts |
+** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be |
+** non-NULL if it is part of a view or trigger program definition. See |
+** sqlite3FixSrcList() for details. |
+*/ |
+SQLITE_PRIVATE Table *sqlite3LocateTableItem( |
+ Parse *pParse, |
+ u32 flags, |
+ struct SrcList_item *p |
+){ |
+ const char *zDb; |
+ assert( p->pSchema==0 || p->zDatabase==0 ); |
+ if( p->pSchema ){ |
+ int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); |
+ zDb = pParse->db->aDb[iDb].zDbSName; |
+ }else{ |
+ zDb = p->zDatabase; |
+ } |
+ return sqlite3LocateTable(pParse, flags, p->zName, zDb); |
+} |
+ |
+/* |
+** Locate the in-memory structure that describes |
+** a particular index given the name of that index |
+** and the name of the database that contains the index. |
+** Return NULL if not found. |
+** |
+** If zDatabase is 0, all databases are searched for the |
+** table and the first matching index is returned. (No checking |
+** for duplicate index names is done.) The search order is |
+** TEMP first, then MAIN, then any auxiliary databases added |
+** using the ATTACH command. |
+*/ |
+SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ |
+ Index *p = 0; |
+ int i; |
+ /* All mutexes are required for schema access. Make sure we hold them. */ |
+ assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
+ for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
+ int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
+ Schema *pSchema = db->aDb[j].pSchema; |
+ assert( pSchema ); |
+ if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue; |
+ assert( sqlite3SchemaMutexHeld(db, j, 0) ); |
+ p = sqlite3HashFind(&pSchema->idxHash, zName); |
+ if( p ) break; |
+ } |
+ return p; |
+} |
+ |
+/* |
+** Reclaim the memory used by an index |
+*/ |
+static void freeIndex(sqlite3 *db, Index *p){ |
+#ifndef SQLITE_OMIT_ANALYZE |
+ sqlite3DeleteIndexSamples(db, p); |
+#endif |
+ sqlite3ExprDelete(db, p->pPartIdxWhere); |
+ sqlite3ExprListDelete(db, p->aColExpr); |
+ sqlite3DbFree(db, p->zColAff); |
+ if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl); |
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
+ sqlite3_free(p->aiRowEst); |
+#endif |
+ sqlite3DbFree(db, p); |
+} |
+ |
+/* |
+** For the index called zIdxName which is found in the database iDb, |
+** unlike that index from its Table then remove the index from |
+** the index hash table and free all memory structures associated |
+** with the index. |
+*/ |
+SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){ |
+ Index *pIndex; |
+ Hash *pHash; |
+ |
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
+ pHash = &db->aDb[iDb].pSchema->idxHash; |
+ pIndex = sqlite3HashInsert(pHash, zIdxName, 0); |
+ if( ALWAYS(pIndex) ){ |
+ if( pIndex->pTable->pIndex==pIndex ){ |
+ pIndex->pTable->pIndex = pIndex->pNext; |
+ }else{ |
+ Index *p; |
+ /* Justification of ALWAYS(); The index must be on the list of |
+ ** indices. */ |
+ p = pIndex->pTable->pIndex; |
+ while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; } |
+ if( ALWAYS(p && p->pNext==pIndex) ){ |
+ p->pNext = pIndex->pNext; |
+ } |
+ } |
+ freeIndex(db, pIndex); |
+ } |
+ db->flags |= SQLITE_InternChanges; |
+} |
+ |
+/* |
+** Look through the list of open database files in db->aDb[] and if |
+** any have been closed, remove them from the list. Reallocate the |
+** db->aDb[] structure to a smaller size, if possible. |
+** |
+** Entry 0 (the "main" database) and entry 1 (the "temp" database) |
+** are never candidates for being collapsed. |
+*/ |
+SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){ |
+ int i, j; |
+ for(i=j=2; i<db->nDb; i++){ |
+ struct Db *pDb = &db->aDb[i]; |
+ if( pDb->pBt==0 ){ |
+ sqlite3DbFree(db, pDb->zDbSName); |
+ pDb->zDbSName = 0; |
+ continue; |
+ } |
+ if( j<i ){ |
+ db->aDb[j] = db->aDb[i]; |
+ } |
+ j++; |
+ } |
+ db->nDb = j; |
+ if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ |
+ memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); |
+ sqlite3DbFree(db, db->aDb); |
+ db->aDb = db->aDbStatic; |
+ } |
+} |
+ |
+/* |
+** Reset the schema for the database at index iDb. Also reset the |
+** TEMP schema. |
+*/ |
+SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){ |
+ Db *pDb; |
+ assert( iDb<db->nDb ); |
+ |
+ /* Case 1: Reset the single schema identified by iDb */ |
+ pDb = &db->aDb[iDb]; |
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
+ assert( pDb->pSchema!=0 ); |
+ sqlite3SchemaClear(pDb->pSchema); |
+ |
+ /* If any database other than TEMP is reset, then also reset TEMP |
+ ** since TEMP might be holding triggers that reference tables in the |
+ ** other database. |
+ */ |
+ if( iDb!=1 ){ |
+ pDb = &db->aDb[1]; |
+ assert( pDb->pSchema!=0 ); |
+ sqlite3SchemaClear(pDb->pSchema); |
+ } |
+ return; |
+} |
+ |
+/* |
+** Erase all schema information from all attached databases (including |
+** "main" and "temp") for a single database connection. |
+*/ |
+SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){ |
+ int i; |
+ sqlite3BtreeEnterAll(db); |
+ for(i=0; i<db->nDb; i++){ |
+ Db *pDb = &db->aDb[i]; |
+ if( pDb->pSchema ){ |
+ sqlite3SchemaClear(pDb->pSchema); |
+ } |
+ } |
+ db->flags &= ~SQLITE_InternChanges; |
+ sqlite3VtabUnlockList(db); |
+ sqlite3BtreeLeaveAll(db); |
+ sqlite3CollapseDatabaseArray(db); |
+} |
+ |
+/* |
+** This routine is called when a commit occurs. |
+*/ |
+SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){ |
+ db->flags &= ~SQLITE_InternChanges; |
+} |
+ |
+/* |
+** Delete memory allocated for the column names of a table or view (the |
+** Table.aCol[] array). |
+*/ |
+SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){ |
+ int i; |
+ Column *pCol; |
+ assert( pTable!=0 ); |
+ if( (pCol = pTable->aCol)!=0 ){ |
+ for(i=0; i<pTable->nCol; i++, pCol++){ |
+ sqlite3DbFree(db, pCol->zName); |
+ sqlite3ExprDelete(db, pCol->pDflt); |
+ sqlite3DbFree(db, pCol->zColl); |
+ } |
+ sqlite3DbFree(db, pTable->aCol); |
+ } |
+} |
+ |
+/* |
+** Remove the memory data structures associated with the given |
+** Table. No changes are made to disk by this routine. |
+** |
+** This routine just deletes the data structure. It does not unlink |
+** the table data structure from the hash table. But it does destroy |
+** memory structures of the indices and foreign keys associated with |
+** the table. |
+** |
+** The db parameter is optional. It is needed if the Table object |
+** contains lookaside memory. (Table objects in the schema do not use |
+** lookaside memory, but some ephemeral Table objects do.) Or the |
+** db parameter can be used with db->pnBytesFreed to measure the memory |
+** used by the Table object. |
+*/ |
+static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){ |
+ Index *pIndex, *pNext; |
+ TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */ |
+ |
+ /* Record the number of outstanding lookaside allocations in schema Tables |
+ ** prior to doing any free() operations. Since schema Tables do not use |
+ ** lookaside, this number should not change. */ |
+ TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ? |
+ db->lookaside.nOut : 0 ); |
+ |
+ /* Delete all indices associated with this table. */ |
+ for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
+ pNext = pIndex->pNext; |
+ assert( pIndex->pSchema==pTable->pSchema |
+ || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) ); |
+ if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){ |
+ char *zName = pIndex->zName; |
+ TESTONLY ( Index *pOld = ) sqlite3HashInsert( |
+ &pIndex->pSchema->idxHash, zName, 0 |
+ ); |
+ assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
+ assert( pOld==pIndex || pOld==0 ); |
+ } |
+ freeIndex(db, pIndex); |
+ } |
+ |
+ /* Delete any foreign keys attached to this table. */ |
+ sqlite3FkDelete(db, pTable); |
+ |
+ /* Delete the Table structure itself. |
+ */ |
+ sqlite3DeleteColumnNames(db, pTable); |
+ sqlite3DbFree(db, pTable->zName); |
+ sqlite3DbFree(db, pTable->zColAff); |
+ sqlite3SelectDelete(db, pTable->pSelect); |
+ sqlite3ExprListDelete(db, pTable->pCheck); |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+ sqlite3VtabClear(db, pTable); |
+#endif |
+ sqlite3DbFree(db, pTable); |
+ |
+ /* Verify that no lookaside memory was used by schema tables */ |
+ assert( nLookaside==0 || nLookaside==db->lookaside.nOut ); |
+} |
+SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ |
+ /* Do not delete the table until the reference count reaches zero. */ |
+ if( !pTable ) return; |
+ if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return; |
+ deleteTable(db, pTable); |
+} |
+ |
+ |
+/* |
+** Unlink the given table from the hash tables and the delete the |
+** table structure with all its indices and foreign keys. |
+*/ |
+SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ |
+ Table *p; |
+ Db *pDb; |
+ |
+ assert( db!=0 ); |
+ assert( iDb>=0 && iDb<db->nDb ); |
+ assert( zTabName ); |
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
+ testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */ |
+ pDb = &db->aDb[iDb]; |
+ p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0); |
+ sqlite3DeleteTable(db, p); |
+ db->flags |= SQLITE_InternChanges; |
+} |
+ |
+/* |
+** Given a token, return a string that consists of the text of that |
+** token. Space to hold the returned string |
+** is obtained from sqliteMalloc() and must be freed by the calling |
+** function. |
+** |
+** Any quotation marks (ex: "name", 'name', [name], or `name`) that |
+** surround the body of the token are removed. |
+** |
+** Tokens are often just pointers into the original SQL text and so |
+** are not \000 terminated and are not persistent. The returned string |
+** is \000 terminated and is persistent. |
+*/ |
+SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){ |
+ char *zName; |
+ if( pName ){ |
+ zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n); |
+ sqlite3Dequote(zName); |
+ }else{ |
+ zName = 0; |
+ } |
+ return zName; |
+} |
+ |
+/* |
+** Open the sqlite_master table stored in database number iDb for |
+** writing. The table is opened using cursor 0. |
+*/ |
+SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){ |
+ Vdbe *v = sqlite3GetVdbe(p); |
+ sqlite3TableLock(p, iDb, MASTER_ROOT, 1, MASTER_NAME); |
+ sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5); |
+ if( p->nTab==0 ){ |
+ p->nTab = 1; |
+ } |
+} |
+ |
+/* |
+** Parameter zName points to a nul-terminated buffer containing the name |
+** of a database ("main", "temp" or the name of an attached db). This |
+** function returns the index of the named database in db->aDb[], or |
+** -1 if the named db cannot be found. |
+*/ |
+SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){ |
+ int i = -1; /* Database number */ |
+ if( zName ){ |
+ Db *pDb; |
+ for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ |
+ if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break; |
+ /* "main" is always an acceptable alias for the primary database |
+ ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */ |
+ if( i==0 && 0==sqlite3_stricmp("main", zName) ) break; |
+ } |
+ } |
+ return i; |
+} |
+ |
+/* |
+** The token *pName contains the name of a database (either "main" or |
+** "temp" or the name of an attached db). This routine returns the |
+** index of the named database in db->aDb[], or -1 if the named db |
+** does not exist. |
+*/ |
+SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){ |
+ int i; /* Database number */ |
+ char *zName; /* Name we are searching for */ |
+ zName = sqlite3NameFromToken(db, pName); |
+ i = sqlite3FindDbName(db, zName); |
+ sqlite3DbFree(db, zName); |
+ return i; |
+} |
+ |
+/* The table or view or trigger name is passed to this routine via tokens |
+** pName1 and pName2. If the table name was fully qualified, for example: |
+** |
+** CREATE TABLE xxx.yyy (...); |
+** |
+** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if |
+** the table name is not fully qualified, i.e.: |
+** |
+** CREATE TABLE yyy(...); |
+** |
+** Then pName1 is set to "yyy" and pName2 is "". |
+** |
+** This routine sets the *ppUnqual pointer to point at the token (pName1 or |
+** pName2) that stores the unqualified table name. The index of the |
+** database "xxx" is returned. |
+*/ |
+SQLITE_PRIVATE int sqlite3TwoPartName( |
+ Parse *pParse, /* Parsing and code generating context */ |
+ Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ |
+ Token *pName2, /* The "yyy" in the name "xxx.yyy" */ |
+ Token **pUnqual /* Write the unqualified object name here */ |
+){ |
+ int iDb; /* Database holding the object */ |
+ sqlite3 *db = pParse->db; |
+ |
+ assert( pName2!=0 ); |
+ if( pName2->n>0 ){ |
+ if( db->init.busy ) { |
+ sqlite3ErrorMsg(pParse, "corrupt database"); |
+ return -1; |
+ } |
+ *pUnqual = pName2; |
+ iDb = sqlite3FindDb(db, pName1); |
+ if( iDb<0 ){ |
+ sqlite3ErrorMsg(pParse, "unknown database %T", pName1); |
+ return -1; |
+ } |
+ }else{ |
+ assert( db->init.iDb==0 || db->init.busy || (db->flags & SQLITE_Vacuum)!=0); |
+ iDb = db->init.iDb; |
+ *pUnqual = pName1; |
+ } |
+ return iDb; |
+} |
+ |
+/* |
+** This routine is used to check if the UTF-8 string zName is a legal |
+** unqualified name for a new schema object (table, index, view or |
+** trigger). All names are legal except those that begin with the string |
+** "sqlite_" (in upper, lower or mixed case). This portion of the namespace |
+** is reserved for internal use. |
+*/ |
+SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){ |
+ if( !pParse->db->init.busy && pParse->nested==0 |
+ && (pParse->db->flags & SQLITE_WriteSchema)==0 |
+ && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ |
+ sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName); |
+ return SQLITE_ERROR; |
+ } |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Return the PRIMARY KEY index of a table |
+*/ |
+SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){ |
+ Index *p; |
+ for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){} |
+ return p; |
+} |
+ |
+/* |
+** Return the column of index pIdx that corresponds to table |
+** column iCol. Return -1 if not found. |
+*/ |
+SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){ |
+ int i; |
+ for(i=0; i<pIdx->nColumn; i++){ |
+ if( iCol==pIdx->aiColumn[i] ) return i; |
+ } |
+ return -1; |
+} |
+ |
+/* |
+** Begin constructing a new table representation in memory. This is |
+** the first of several action routines that get called in response |
+** to a CREATE TABLE statement. In particular, this routine is called |
+** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp |
+** flag is true if the table should be stored in the auxiliary database |
+** file instead of in the main database file. This is normally the case |
+** when the "TEMP" or "TEMPORARY" keyword occurs in between |
+** CREATE and TABLE. |
+** |
+** The new table record is initialized and put in pParse->pNewTable. |
+** As more of the CREATE TABLE statement is parsed, additional action |
+** routines will be called to add more information to this record. |
+** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine |
+** is called to complete the construction of the new table record. |
+*/ |
+SQLITE_PRIVATE void sqlite3StartTable( |
+ Parse *pParse, /* Parser context */ |
+ Token *pName1, /* First part of the name of the table or view */ |
+ Token *pName2, /* Second part of the name of the table or view */ |
+ int isTemp, /* True if this is a TEMP table */ |
+ int isView, /* True if this is a VIEW */ |
+ int isVirtual, /* True if this is a VIRTUAL table */ |
+ int noErr /* Do nothing if table already exists */ |
+){ |
+ Table *pTable; |
+ char *zName = 0; /* The name of the new table */ |
+ sqlite3 *db = pParse->db; |
+ Vdbe *v; |
+ int iDb; /* Database number to create the table in */ |
+ Token *pName; /* Unqualified name of the table to create */ |
+ |
+ if( db->init.busy && db->init.newTnum==1 ){ |
+ /* Special case: Parsing the sqlite_master or sqlite_temp_master schema */ |
+ iDb = db->init.iDb; |
+ zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb)); |
+ pName = pName1; |
+ }else{ |
+ /* The common case */ |
+ iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
+ if( iDb<0 ) return; |
+ if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ |
+ /* If creating a temp table, the name may not be qualified. Unless |
+ ** the database name is "temp" anyway. */ |
+ sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); |
+ return; |
+ } |
+ if( !OMIT_TEMPDB && isTemp ) iDb = 1; |
+ zName = sqlite3NameFromToken(db, pName); |
+ } |
+ pParse->sNameToken = *pName; |
+ if( zName==0 ) return; |
+ if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ |
+ goto begin_table_error; |
+ } |
+ if( db->init.iDb==1 ) isTemp = 1; |
+#ifndef SQLITE_OMIT_AUTHORIZATION |
+ assert( isTemp==0 || isTemp==1 ); |
+ assert( isView==0 || isView==1 ); |
+ { |
+ static const u8 aCode[] = { |
+ SQLITE_CREATE_TABLE, |
+ SQLITE_CREATE_TEMP_TABLE, |
+ SQLITE_CREATE_VIEW, |
+ SQLITE_CREATE_TEMP_VIEW |
+ }; |
+ char *zDb = db->aDb[iDb].zDbSName; |
+ if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ |
+ goto begin_table_error; |
+ } |
+ if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView], |
+ zName, 0, zDb) ){ |
+ goto begin_table_error; |
+ } |
+ } |
+#endif |
+ |
+ /* Make sure the new table name does not collide with an existing |
+ ** index or table name in the same database. Issue an error message if |
+ ** it does. The exception is if the statement being parsed was passed |
+ ** to an sqlite3_declare_vtab() call. In that case only the column names |
+ ** and types will be used, so there is no need to test for namespace |
+ ** collisions. |
+ */ |
+ if( !IN_DECLARE_VTAB ){ |
+ char *zDb = db->aDb[iDb].zDbSName; |
+ if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
+ goto begin_table_error; |
+ } |
+ pTable = sqlite3FindTable(db, zName, zDb); |
+ if( pTable ){ |
+ if( !noErr ){ |
+ sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
+ }else{ |
+ assert( !db->init.busy || CORRUPT_DB ); |
+ sqlite3CodeVerifySchema(pParse, iDb); |
+ } |
+ goto begin_table_error; |
+ } |
+ if( sqlite3FindIndex(db, zName, zDb)!=0 ){ |
+ sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
+ goto begin_table_error; |
+ } |
+ } |
+ |
+ pTable = sqlite3DbMallocZero(db, sizeof(Table)); |
+ if( pTable==0 ){ |
+ assert( db->mallocFailed ); |
+ pParse->rc = SQLITE_NOMEM_BKPT; |
+ pParse->nErr++; |
+ goto begin_table_error; |
+ } |
+ pTable->zName = zName; |
+ pTable->iPKey = -1; |
+ pTable->pSchema = db->aDb[iDb].pSchema; |
+ pTable->nTabRef = 1; |
+ pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
+ assert( pParse->pNewTable==0 ); |
+ pParse->pNewTable = pTable; |
+ |
+ /* If this is the magic sqlite_sequence table used by autoincrement, |
+ ** then record a pointer to this table in the main database structure |
+ ** so that INSERT can find the table easily. |
+ */ |
+#ifndef SQLITE_OMIT_AUTOINCREMENT |
+ if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){ |
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
+ pTable->pSchema->pSeqTab = pTable; |
+ } |
+#endif |
+ |
+ /* Begin generating the code that will insert the table record into |
+ ** the SQLITE_MASTER table. Note in particular that we must go ahead |
+ ** and allocate the record number for the table entry now. Before any |
+ ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause |
+ ** indices to be created and the table record must come before the |
+ ** indices. Hence, the record number for the table must be allocated |
+ ** now. |
+ */ |
+ if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ |
+ int addr1; |
+ int fileFormat; |
+ int reg1, reg2, reg3; |
+ /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */ |
+ static const char nullRow[] = { 6, 0, 0, 0, 0, 0 }; |
+ sqlite3BeginWriteOperation(pParse, 1, iDb); |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+ if( isVirtual ){ |
+ sqlite3VdbeAddOp0(v, OP_VBegin); |
+ } |
+#endif |
+ |
+ /* If the file format and encoding in the database have not been set, |
+ ** set them now. |
+ */ |
+ reg1 = pParse->regRowid = ++pParse->nMem; |
+ reg2 = pParse->regRoot = ++pParse->nMem; |
+ reg3 = ++pParse->nMem; |
+ sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT); |
+ sqlite3VdbeUsesBtree(v, iDb); |
+ addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v); |
+ fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? |
+ 1 : SQLITE_MAX_FILE_FORMAT; |
+ sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat); |
+ sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db)); |
+ sqlite3VdbeJumpHere(v, addr1); |
+ |
+ /* This just creates a place-holder record in the sqlite_master table. |
+ ** The record created does not contain anything yet. It will be replaced |
+ ** by the real entry in code generated at sqlite3EndTable(). |
+ ** |
+ ** The rowid for the new entry is left in register pParse->regRowid. |
+ ** The root page number of the new table is left in reg pParse->regRoot. |
+ ** The rowid and root page number values are needed by the code that |
+ ** sqlite3EndTable will generate. |
+ */ |
+#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
+ if( isView || isVirtual ){ |
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2); |
+ }else |
+#endif |
+ { |
+ pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2); |
+ } |
+ sqlite3OpenMasterTable(pParse, iDb); |
+ sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1); |
+ sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC); |
+ sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1); |
+ sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
+ sqlite3VdbeAddOp0(v, OP_Close); |
+ } |
+ |
+ /* Normal (non-error) return. */ |
+ return; |
+ |
+ /* If an error occurs, we jump here */ |
+begin_table_error: |
+ sqlite3DbFree(db, zName); |
+ return; |
+} |
+ |
+/* Set properties of a table column based on the (magical) |
+** name of the column. |
+*/ |
+#if SQLITE_ENABLE_HIDDEN_COLUMNS |
+SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){ |
+ if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){ |
+ pCol->colFlags |= COLFLAG_HIDDEN; |
+ }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){ |
+ pTab->tabFlags |= TF_OOOHidden; |
+ } |
+} |
+#endif |
+ |
+ |
+/* |
+** Add a new column to the table currently being constructed. |
+** |
+** The parser calls this routine once for each column declaration |
+** in a CREATE TABLE statement. sqlite3StartTable() gets called |
+** first to get things going. Then this routine is called for each |
+** column. |
+*/ |
+SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName, Token *pType){ |
+ Table *p; |
+ int i; |
+ char *z; |
+ char *zType; |
+ Column *pCol; |
+ sqlite3 *db = pParse->db; |
+ if( (p = pParse->pNewTable)==0 ) return; |
+#if SQLITE_MAX_COLUMN |
+ if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
+ sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); |
+ return; |
+ } |
+#endif |
+ z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2); |
+ if( z==0 ) return; |
+ memcpy(z, pName->z, pName->n); |
+ z[pName->n] = 0; |
+ sqlite3Dequote(z); |
+ for(i=0; i<p->nCol; i++){ |
+ if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){ |
+ sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); |
+ sqlite3DbFree(db, z); |
+ return; |
+ } |
+ } |
+ if( (p->nCol & 0x7)==0 ){ |
+ Column *aNew; |
+ aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0])); |
+ if( aNew==0 ){ |
+ sqlite3DbFree(db, z); |
+ return; |
+ } |
+ p->aCol = aNew; |
+ } |
+ pCol = &p->aCol[p->nCol]; |
+ memset(pCol, 0, sizeof(p->aCol[0])); |
+ pCol->zName = z; |
+ sqlite3ColumnPropertiesFromName(p, pCol); |
+ |
+ if( pType->n==0 ){ |
+ /* If there is no type specified, columns have the default affinity |
+ ** 'BLOB'. */ |
+ pCol->affinity = SQLITE_AFF_BLOB; |
+ pCol->szEst = 1; |
+ }else{ |
+ zType = z + sqlite3Strlen30(z) + 1; |
+ memcpy(zType, pType->z, pType->n); |
+ zType[pType->n] = 0; |
+ sqlite3Dequote(zType); |
+ pCol->affinity = sqlite3AffinityType(zType, &pCol->szEst); |
+ pCol->colFlags |= COLFLAG_HASTYPE; |
+ } |
+ p->nCol++; |
+ pParse->constraintName.n = 0; |
+} |
+ |
+/* |
+** This routine is called by the parser while in the middle of |
+** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
+** been seen on a column. This routine sets the notNull flag on |
+** the column currently under construction. |
+*/ |
+SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){ |
+ Table *p; |
+ p = pParse->pNewTable; |
+ if( p==0 || NEVER(p->nCol<1) ) return; |
+ p->aCol[p->nCol-1].notNull = (u8)onError; |
+} |
+ |
+/* |
+** Scan the column type name zType (length nType) and return the |
+** associated affinity type. |
+** |
+** This routine does a case-independent search of zType for the |
+** substrings in the following table. If one of the substrings is |
+** found, the corresponding affinity is returned. If zType contains |
+** more than one of the substrings, entries toward the top of |
+** the table take priority. For example, if zType is 'BLOBINT', |
+** SQLITE_AFF_INTEGER is returned. |
+** |
+** Substring | Affinity |
+** -------------------------------- |
+** 'INT' | SQLITE_AFF_INTEGER |
+** 'CHAR' | SQLITE_AFF_TEXT |
+** 'CLOB' | SQLITE_AFF_TEXT |
+** 'TEXT' | SQLITE_AFF_TEXT |
+** 'BLOB' | SQLITE_AFF_BLOB |
+** 'REAL' | SQLITE_AFF_REAL |
+** 'FLOA' | SQLITE_AFF_REAL |
+** 'DOUB' | SQLITE_AFF_REAL |
+** |
+** If none of the substrings in the above table are found, |
+** SQLITE_AFF_NUMERIC is returned. |
+*/ |
+SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){ |
+ u32 h = 0; |
+ char aff = SQLITE_AFF_NUMERIC; |
+ const char *zChar = 0; |
+ |
+ assert( zIn!=0 ); |
+ while( zIn[0] ){ |
+ h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; |
+ zIn++; |
+ if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ |
+ aff = SQLITE_AFF_TEXT; |
+ zChar = zIn; |
+ }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ |
+ aff = SQLITE_AFF_TEXT; |
+ }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ |
+ aff = SQLITE_AFF_TEXT; |
+ }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ |
+ && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ |
+ aff = SQLITE_AFF_BLOB; |
+ if( zIn[0]=='(' ) zChar = zIn; |
+#ifndef SQLITE_OMIT_FLOATING_POINT |
+ }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ |
+ && aff==SQLITE_AFF_NUMERIC ){ |
+ aff = SQLITE_AFF_REAL; |
+ }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */ |
+ && aff==SQLITE_AFF_NUMERIC ){ |
+ aff = SQLITE_AFF_REAL; |
+ }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */ |
+ && aff==SQLITE_AFF_NUMERIC ){ |
+ aff = SQLITE_AFF_REAL; |
+#endif |
+ }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */ |
+ aff = SQLITE_AFF_INTEGER; |
+ break; |
+ } |
+ } |
+ |
+ /* If pszEst is not NULL, store an estimate of the field size. The |
+ ** estimate is scaled so that the size of an integer is 1. */ |
+ if( pszEst ){ |
+ *pszEst = 1; /* default size is approx 4 bytes */ |
+ if( aff<SQLITE_AFF_NUMERIC ){ |
+ if( zChar ){ |
+ while( zChar[0] ){ |
+ if( sqlite3Isdigit(zChar[0]) ){ |
+ int v = 0; |
+ sqlite3GetInt32(zChar, &v); |
+ v = v/4 + 1; |
+ if( v>255 ) v = 255; |
+ *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */ |
+ break; |
+ } |
+ zChar++; |
+ } |
+ }else{ |
+ *pszEst = 5; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/ |
+ } |
+ } |
+ } |
+ return aff; |
+} |
+ |
+/* |
+** The expression is the default value for the most recently added column |
+** of the table currently under construction. |
+** |
+** Default value expressions must be constant. Raise an exception if this |
+** is not the case. |
+** |
+** This routine is called by the parser while in the middle of |
+** parsing a CREATE TABLE statement. |
+*/ |
+SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){ |
+ Table *p; |
+ Column *pCol; |
+ sqlite3 *db = pParse->db; |
+ p = pParse->pNewTable; |
+ if( p!=0 ){ |
+ pCol = &(p->aCol[p->nCol-1]); |
+ if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){ |
+ sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", |
+ pCol->zName); |
+ }else{ |
+ /* A copy of pExpr is used instead of the original, as pExpr contains |
+ ** tokens that point to volatile memory. The 'span' of the expression |
+ ** is required by pragma table_info. |
+ */ |
+ Expr x; |
+ sqlite3ExprDelete(db, pCol->pDflt); |
+ memset(&x, 0, sizeof(x)); |
+ x.op = TK_SPAN; |
+ x.u.zToken = sqlite3DbStrNDup(db, (char*)pSpan->zStart, |
+ (int)(pSpan->zEnd - pSpan->zStart)); |
+ x.pLeft = pSpan->pExpr; |
+ x.flags = EP_Skip; |
+ pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE); |
+ sqlite3DbFree(db, x.u.zToken); |
+ } |
+ } |
+ sqlite3ExprDelete(db, pSpan->pExpr); |
+} |
+ |
+/* |
+** Backwards Compatibility Hack: |
+** |
+** Historical versions of SQLite accepted strings as column names in |
+** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example: |
+** |
+** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim) |
+** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC); |
+** |
+** This is goofy. But to preserve backwards compatibility we continue to |
+** accept it. This routine does the necessary conversion. It converts |
+** the expression given in its argument from a TK_STRING into a TK_ID |
+** if the expression is just a TK_STRING with an optional COLLATE clause. |
+** If the epxression is anything other than TK_STRING, the expression is |
+** unchanged. |
+*/ |
+static void sqlite3StringToId(Expr *p){ |
+ if( p->op==TK_STRING ){ |
+ p->op = TK_ID; |
+ }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){ |
+ p->pLeft->op = TK_ID; |
+ } |
+} |
+ |
+/* |
+** Designate the PRIMARY KEY for the table. pList is a list of names |
+** of columns that form the primary key. If pList is NULL, then the |
+** most recently added column of the table is the primary key. |
+** |
+** A table can have at most one primary key. If the table already has |
+** a primary key (and this is the second primary key) then create an |
+** error. |
+** |
+** If the PRIMARY KEY is on a single column whose datatype is INTEGER, |
+** then we will try to use that column as the rowid. Set the Table.iPKey |
+** field of the table under construction to be the index of the |
+** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is |
+** no INTEGER PRIMARY KEY. |
+** |
+** If the key is not an INTEGER PRIMARY KEY, then create a unique |
+** index for the key. No index is created for INTEGER PRIMARY KEYs. |
+*/ |
+SQLITE_PRIVATE void sqlite3AddPrimaryKey( |
+ Parse *pParse, /* Parsing context */ |
+ ExprList *pList, /* List of field names to be indexed */ |
+ int onError, /* What to do with a uniqueness conflict */ |
+ int autoInc, /* True if the AUTOINCREMENT keyword is present */ |
+ int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ |
+){ |
+ Table *pTab = pParse->pNewTable; |
+ Column *pCol = 0; |
+ int iCol = -1, i; |
+ int nTerm; |
+ if( pTab==0 ) goto primary_key_exit; |
+ if( pTab->tabFlags & TF_HasPrimaryKey ){ |
+ sqlite3ErrorMsg(pParse, |
+ "table \"%s\" has more than one primary key", pTab->zName); |
+ goto primary_key_exit; |
+ } |
+ pTab->tabFlags |= TF_HasPrimaryKey; |
+ if( pList==0 ){ |
+ iCol = pTab->nCol - 1; |
+ pCol = &pTab->aCol[iCol]; |
+ pCol->colFlags |= COLFLAG_PRIMKEY; |
+ nTerm = 1; |
+ }else{ |
+ nTerm = pList->nExpr; |
+ for(i=0; i<nTerm; i++){ |
+ Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
+ assert( pCExpr!=0 ); |
+ sqlite3StringToId(pCExpr); |
+ if( pCExpr->op==TK_ID ){ |
+ const char *zCName = pCExpr->u.zToken; |
+ for(iCol=0; iCol<pTab->nCol; iCol++){ |
+ if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){ |
+ pCol = &pTab->aCol[iCol]; |
+ pCol->colFlags |= COLFLAG_PRIMKEY; |
+ break; |
+ } |
+ } |
+ } |
+ } |
+ } |
+ if( nTerm==1 |
+ && pCol |
+ && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0 |
+ && sortOrder!=SQLITE_SO_DESC |
+ ){ |
+ pTab->iPKey = iCol; |
+ pTab->keyConf = (u8)onError; |
+ assert( autoInc==0 || autoInc==1 ); |
+ pTab->tabFlags |= autoInc*TF_Autoincrement; |
+ if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder; |
+ }else if( autoInc ){ |
+#ifndef SQLITE_OMIT_AUTOINCREMENT |
+ sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " |
+ "INTEGER PRIMARY KEY"); |
+#endif |
+ }else{ |
+ sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, |
+ 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY); |
+ pList = 0; |
+ } |
+ |
+primary_key_exit: |
+ sqlite3ExprListDelete(pParse->db, pList); |
+ return; |
+} |
+ |
+/* |
+** Add a new CHECK constraint to the table currently under construction. |
+*/ |
+SQLITE_PRIVATE void sqlite3AddCheckConstraint( |
+ Parse *pParse, /* Parsing context */ |
+ Expr *pCheckExpr /* The check expression */ |
+){ |
+#ifndef SQLITE_OMIT_CHECK |
+ Table *pTab = pParse->pNewTable; |
+ sqlite3 *db = pParse->db; |
+ if( pTab && !IN_DECLARE_VTAB |
+ && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt) |
+ ){ |
+ pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr); |
+ if( pParse->constraintName.n ){ |
+ sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1); |
+ } |
+ }else |
+#endif |
+ { |
+ sqlite3ExprDelete(pParse->db, pCheckExpr); |
+ } |
+} |
+ |
+/* |
+** Set the collation function of the most recently parsed table column |
+** to the CollSeq given. |
+*/ |
+SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){ |
+ Table *p; |
+ int i; |
+ char *zColl; /* Dequoted name of collation sequence */ |
+ sqlite3 *db; |
+ |
+ if( (p = pParse->pNewTable)==0 ) return; |
+ i = p->nCol-1; |
+ db = pParse->db; |
+ zColl = sqlite3NameFromToken(db, pToken); |
+ if( !zColl ) return; |
+ |
+ if( sqlite3LocateCollSeq(pParse, zColl) ){ |
+ Index *pIdx; |
+ sqlite3DbFree(db, p->aCol[i].zColl); |
+ p->aCol[i].zColl = zColl; |
+ |
+ /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>", |
+ ** then an index may have been created on this column before the |
+ ** collation type was added. Correct this if it is the case. |
+ */ |
+ for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
+ assert( pIdx->nKeyCol==1 ); |
+ if( pIdx->aiColumn[0]==i ){ |
+ pIdx->azColl[0] = p->aCol[i].zColl; |
+ } |
+ } |
+ }else{ |
+ sqlite3DbFree(db, zColl); |
+ } |
+} |
+ |
+/* |
+** This function returns the collation sequence for database native text |
+** encoding identified by the string zName, length nName. |
+** |
+** If the requested collation sequence is not available, or not available |
+** in the database native encoding, the collation factory is invoked to |
+** request it. If the collation factory does not supply such a sequence, |
+** and the sequence is available in another text encoding, then that is |
+** returned instead. |
+** |
+** If no versions of the requested collations sequence are available, or |
+** another error occurs, NULL is returned and an error message written into |
+** pParse. |
+** |
+** This routine is a wrapper around sqlite3FindCollSeq(). This routine |
+** invokes the collation factory if the named collation cannot be found |
+** and generates an error message. |
+** |
+** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq() |
+*/ |
+SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){ |
+ sqlite3 *db = pParse->db; |
+ u8 enc = ENC(db); |
+ u8 initbusy = db->init.busy; |
+ CollSeq *pColl; |
+ |
+ pColl = sqlite3FindCollSeq(db, enc, zName, initbusy); |
+ if( !initbusy && (!pColl || !pColl->xCmp) ){ |
+ pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName); |
+ } |
+ |
+ return pColl; |
+} |
+ |
+ |
+/* |
+** Generate code that will increment the schema cookie. |
+** |
+** The schema cookie is used to determine when the schema for the |
+** database changes. After each schema change, the cookie value |
+** changes. When a process first reads the schema it records the |
+** cookie. Thereafter, whenever it goes to access the database, |
+** it checks the cookie to make sure the schema has not changed |
+** since it was last read. |
+** |
+** This plan is not completely bullet-proof. It is possible for |
+** the schema to change multiple times and for the cookie to be |
+** set back to prior value. But schema changes are infrequent |
+** and the probability of hitting the same cookie value is only |
+** 1 chance in 2^32. So we're safe enough. |
+** |
+** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments |
+** the schema-version whenever the schema changes. |
+*/ |
+SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){ |
+ sqlite3 *db = pParse->db; |
+ Vdbe *v = pParse->pVdbe; |
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
+ sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, |
+ db->aDb[iDb].pSchema->schema_cookie+1); |
+} |
+ |
+/* |
+** Measure the number of characters needed to output the given |
+** identifier. The number returned includes any quotes used |
+** but does not include the null terminator. |
+** |
+** The estimate is conservative. It might be larger that what is |
+** really needed. |
+*/ |
+static int identLength(const char *z){ |
+ int n; |
+ for(n=0; *z; n++, z++){ |
+ if( *z=='"' ){ n++; } |
+ } |
+ return n + 2; |
+} |
+ |
+/* |
+** The first parameter is a pointer to an output buffer. The second |
+** parameter is a pointer to an integer that contains the offset at |
+** which to write into the output buffer. This function copies the |
+** nul-terminated string pointed to by the third parameter, zSignedIdent, |
+** to the specified offset in the buffer and updates *pIdx to refer |
+** to the first byte after the last byte written before returning. |
+** |
+** If the string zSignedIdent consists entirely of alpha-numeric |
+** characters, does not begin with a digit and is not an SQL keyword, |
+** then it is copied to the output buffer exactly as it is. Otherwise, |
+** it is quoted using double-quotes. |
+*/ |
+static void identPut(char *z, int *pIdx, char *zSignedIdent){ |
+ unsigned char *zIdent = (unsigned char*)zSignedIdent; |
+ int i, j, needQuote; |
+ i = *pIdx; |
+ |
+ for(j=0; zIdent[j]; j++){ |
+ if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break; |
+ } |
+ needQuote = sqlite3Isdigit(zIdent[0]) |
+ || sqlite3KeywordCode(zIdent, j)!=TK_ID |
+ || zIdent[j]!=0 |
+ || j==0; |
+ |
+ if( needQuote ) z[i++] = '"'; |
+ for(j=0; zIdent[j]; j++){ |
+ z[i++] = zIdent[j]; |
+ if( zIdent[j]=='"' ) z[i++] = '"'; |
+ } |
+ if( needQuote ) z[i++] = '"'; |
+ z[i] = 0; |
+ *pIdx = i; |
+} |
+ |
+/* |
+** Generate a CREATE TABLE statement appropriate for the given |
+** table. Memory to hold the text of the statement is obtained |
+** from sqliteMalloc() and must be freed by the calling function. |
+*/ |
+static char *createTableStmt(sqlite3 *db, Table *p){ |
+ int i, k, n; |
+ char *zStmt; |
+ char *zSep, *zSep2, *zEnd; |
+ Column *pCol; |
+ n = 0; |
+ for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){ |
+ n += identLength(pCol->zName) + 5; |
+ } |
+ n += identLength(p->zName); |
+ if( n<50 ){ |
+ zSep = ""; |
+ zSep2 = ","; |
+ zEnd = ")"; |
+ }else{ |
+ zSep = "\n "; |
+ zSep2 = ",\n "; |
+ zEnd = "\n)"; |
+ } |
+ n += 35 + 6*p->nCol; |
+ zStmt = sqlite3DbMallocRaw(0, n); |
+ if( zStmt==0 ){ |
+ sqlite3OomFault(db); |
+ return 0; |
+ } |
+ sqlite3_snprintf(n, zStmt, "CREATE TABLE "); |
+ k = sqlite3Strlen30(zStmt); |
+ identPut(zStmt, &k, p->zName); |
+ zStmt[k++] = '('; |
+ for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ |
+ static const char * const azType[] = { |
+ /* SQLITE_AFF_BLOB */ "", |
+ /* SQLITE_AFF_TEXT */ " TEXT", |
+ /* SQLITE_AFF_NUMERIC */ " NUM", |
+ /* SQLITE_AFF_INTEGER */ " INT", |
+ /* SQLITE_AFF_REAL */ " REAL" |
+ }; |
+ int len; |
+ const char *zType; |
+ |
+ sqlite3_snprintf(n-k, &zStmt[k], zSep); |
+ k += sqlite3Strlen30(&zStmt[k]); |
+ zSep = zSep2; |
+ identPut(zStmt, &k, pCol->zName); |
+ assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 ); |
+ assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) ); |
+ testcase( pCol->affinity==SQLITE_AFF_BLOB ); |
+ testcase( pCol->affinity==SQLITE_AFF_TEXT ); |
+ testcase( pCol->affinity==SQLITE_AFF_NUMERIC ); |
+ testcase( pCol->affinity==SQLITE_AFF_INTEGER ); |
+ testcase( pCol->affinity==SQLITE_AFF_REAL ); |
+ |
+ zType = azType[pCol->affinity - SQLITE_AFF_BLOB]; |
+ len = sqlite3Strlen30(zType); |
+ assert( pCol->affinity==SQLITE_AFF_BLOB |
+ || pCol->affinity==sqlite3AffinityType(zType, 0) ); |
+ memcpy(&zStmt[k], zType, len); |
+ k += len; |
+ assert( k<=n ); |
+ } |
+ sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd); |
+ return zStmt; |
+} |
+ |
+/* |
+** Resize an Index object to hold N columns total. Return SQLITE_OK |
+** on success and SQLITE_NOMEM on an OOM error. |
+*/ |
+static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){ |
+ char *zExtra; |
+ int nByte; |
+ if( pIdx->nColumn>=N ) return SQLITE_OK; |
+ assert( pIdx->isResized==0 ); |
+ nByte = (sizeof(char*) + sizeof(i16) + 1)*N; |
+ zExtra = sqlite3DbMallocZero(db, nByte); |
+ if( zExtra==0 ) return SQLITE_NOMEM_BKPT; |
+ memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn); |
+ pIdx->azColl = (const char**)zExtra; |
+ zExtra += sizeof(char*)*N; |
+ memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn); |
+ pIdx->aiColumn = (i16*)zExtra; |
+ zExtra += sizeof(i16)*N; |
+ memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn); |
+ pIdx->aSortOrder = (u8*)zExtra; |
+ pIdx->nColumn = N; |
+ pIdx->isResized = 1; |
+ return SQLITE_OK; |
+} |
+ |
+/* |
+** Estimate the total row width for a table. |
+*/ |
+static void estimateTableWidth(Table *pTab){ |
+ unsigned wTable = 0; |
+ const Column *pTabCol; |
+ int i; |
+ for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){ |
+ wTable += pTabCol->szEst; |
+ } |
+ if( pTab->iPKey<0 ) wTable++; |
+ pTab->szTabRow = sqlite3LogEst(wTable*4); |
+} |
+ |
+/* |
+** Estimate the average size of a row for an index. |
+*/ |
+static void estimateIndexWidth(Index *pIdx){ |
+ unsigned wIndex = 0; |
+ int i; |
+ const Column *aCol = pIdx->pTable->aCol; |
+ for(i=0; i<pIdx->nColumn; i++){ |
+ i16 x = pIdx->aiColumn[i]; |
+ assert( x<pIdx->pTable->nCol ); |
+ wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst; |
+ } |
+ pIdx->szIdxRow = sqlite3LogEst(wIndex*4); |
+} |
+ |
+/* Return true if value x is found any of the first nCol entries of aiCol[] |
+*/ |
+static int hasColumn(const i16 *aiCol, int nCol, int x){ |
+ while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1; |
+ return 0; |
+} |
+ |
+/* |
+** This routine runs at the end of parsing a CREATE TABLE statement that |
+** has a WITHOUT ROWID clause. The job of this routine is to convert both |
+** internal schema data structures and the generated VDBE code so that they |
+** are appropriate for a WITHOUT ROWID table instead of a rowid table. |
+** Changes include: |
+** |
+** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL. |
+** (2) Convert the OP_CreateTable into an OP_CreateIndex. There is |
+** no rowid btree for a WITHOUT ROWID. Instead, the canonical |
+** data storage is a covering index btree. |
+** (3) Bypass the creation of the sqlite_master table entry |
+** for the PRIMARY KEY as the primary key index is now |
+** identified by the sqlite_master table entry of the table itself. |
+** (4) Set the Index.tnum of the PRIMARY KEY Index object in the |
+** schema to the rootpage from the main table. |
+** (5) Add all table columns to the PRIMARY KEY Index object |
+** so that the PRIMARY KEY is a covering index. The surplus |
+** columns are part of KeyInfo.nXField and are not used for |
+** sorting or lookup or uniqueness checks. |
+** (6) Replace the rowid tail on all automatically generated UNIQUE |
+** indices with the PRIMARY KEY columns. |
+** |
+** For virtual tables, only (1) is performed. |
+*/ |
+static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ |
+ Index *pIdx; |
+ Index *pPk; |
+ int nPk; |
+ int i, j; |
+ sqlite3 *db = pParse->db; |
+ Vdbe *v = pParse->pVdbe; |
+ |
+ /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables) |
+ */ |
+ if( !db->init.imposterTable ){ |
+ for(i=0; i<pTab->nCol; i++){ |
+ if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){ |
+ pTab->aCol[i].notNull = OE_Abort; |
+ } |
+ } |
+ } |
+ |
+ /* The remaining transformations only apply to b-tree tables, not to |
+ ** virtual tables */ |
+ if( IN_DECLARE_VTAB ) return; |
+ |
+ /* Convert the OP_CreateTable opcode that would normally create the |
+ ** root-page for the table into an OP_CreateIndex opcode. The index |
+ ** created will become the PRIMARY KEY index. |
+ */ |
+ if( pParse->addrCrTab ){ |
+ assert( v ); |
+ sqlite3VdbeChangeOpcode(v, pParse->addrCrTab, OP_CreateIndex); |
+ } |
+ |
+ /* Locate the PRIMARY KEY index. Or, if this table was originally |
+ ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. |
+ */ |
+ if( pTab->iPKey>=0 ){ |
+ ExprList *pList; |
+ Token ipkToken; |
+ sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName); |
+ pList = sqlite3ExprListAppend(pParse, 0, |
+ sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0)); |
+ if( pList==0 ) return; |
+ pList->a[0].sortOrder = pParse->iPkSortOrder; |
+ assert( pParse->pNewTable==pTab ); |
+ sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0, |
+ SQLITE_IDXTYPE_PRIMARYKEY); |
+ if( db->mallocFailed ) return; |
+ pPk = sqlite3PrimaryKeyIndex(pTab); |
+ pTab->iPKey = -1; |
+ }else{ |
+ pPk = sqlite3PrimaryKeyIndex(pTab); |
+ |
+ /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master |
+ ** table entry. This is only required if currently generating VDBE |
+ ** code for a CREATE TABLE (not when parsing one as part of reading |
+ ** a database schema). */ |
+ if( v ){ |
+ assert( db->init.busy==0 ); |
+ sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto); |
+ } |
+ |
+ /* |
+ ** Remove all redundant columns from the PRIMARY KEY. For example, change |
+ ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later |
+ ** code assumes the PRIMARY KEY contains no repeated columns. |
+ */ |
+ for(i=j=1; i<pPk->nKeyCol; i++){ |
+ if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){ |
+ pPk->nColumn--; |
+ }else{ |
+ pPk->aiColumn[j++] = pPk->aiColumn[i]; |
+ } |
+ } |
+ pPk->nKeyCol = j; |
+ } |
+ assert( pPk!=0 ); |
+ pPk->isCovering = 1; |
+ if( !db->init.imposterTable ) pPk->uniqNotNull = 1; |
+ nPk = pPk->nKeyCol; |
+ |
+ /* The root page of the PRIMARY KEY is the table root page */ |
+ pPk->tnum = pTab->tnum; |
+ |
+ /* Update the in-memory representation of all UNIQUE indices by converting |
+ ** the final rowid column into one or more columns of the PRIMARY KEY. |
+ */ |
+ for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
+ int n; |
+ if( IsPrimaryKeyIndex(pIdx) ) continue; |
+ for(i=n=0; i<nPk; i++){ |
+ if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++; |
+ } |
+ if( n==0 ){ |
+ /* This index is a superset of the primary key */ |
+ pIdx->nColumn = pIdx->nKeyCol; |
+ continue; |
+ } |
+ if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return; |
+ for(i=0, j=pIdx->nKeyCol; i<nPk; i++){ |
+ if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){ |
+ pIdx->aiColumn[j] = pPk->aiColumn[i]; |
+ pIdx->azColl[j] = pPk->azColl[i]; |
+ j++; |
+ } |
+ } |
+ assert( pIdx->nColumn>=pIdx->nKeyCol+n ); |
+ assert( pIdx->nColumn>=j ); |
+ } |
+ |
+ /* Add all table columns to the PRIMARY KEY index |
+ */ |
+ if( nPk<pTab->nCol ){ |
+ if( resizeIndexObject(db, pPk, pTab->nCol) ) return; |
+ for(i=0, j=nPk; i<pTab->nCol; i++){ |
+ if( !hasColumn(pPk->aiColumn, j, i) ){ |
+ assert( j<pPk->nColumn ); |
+ pPk->aiColumn[j] = i; |
+ pPk->azColl[j] = sqlite3StrBINARY; |
+ j++; |
+ } |
+ } |
+ assert( pPk->nColumn==j ); |
+ assert( pTab->nCol==j ); |
+ }else{ |
+ pPk->nColumn = pTab->nCol; |
+ } |
+} |
+ |
+/* |
+** This routine is called to report the final ")" that terminates |
+** a CREATE TABLE statement. |
+** |
+** The table structure that other action routines have been building |
+** is added to the internal hash tables, assuming no errors have |
+** occurred. |
+** |
+** An entry for the table is made in the master table on disk, unless |
+** this is a temporary table or db->init.busy==1. When db->init.busy==1 |
+** it means we are reading the sqlite_master table because we just |
+** connected to the database or because the sqlite_master table has |
+** recently changed, so the entry for this table already exists in |
+** the sqlite_master table. We do not want to create it again. |
+** |
+** If the pSelect argument is not NULL, it means that this routine |
+** was called to create a table generated from a |
+** "CREATE TABLE ... AS SELECT ..." statement. The column names of |
+** the new table will match the result set of the SELECT. |
+*/ |
+SQLITE_PRIVATE void sqlite3EndTable( |
+ Parse *pParse, /* Parse context */ |
+ Token *pCons, /* The ',' token after the last column defn. */ |
+ Token *pEnd, /* The ')' before options in the CREATE TABLE */ |
+ u8 tabOpts, /* Extra table options. Usually 0. */ |
+ Select *pSelect /* Select from a "CREATE ... AS SELECT" */ |
+){ |
+ Table *p; /* The new table */ |
+ sqlite3 *db = pParse->db; /* The database connection */ |
+ int iDb; /* Database in which the table lives */ |
+ Index *pIdx; /* An implied index of the table */ |
+ |
+ if( pEnd==0 && pSelect==0 ){ |
+ return; |
+ } |
+ assert( !db->mallocFailed ); |
+ p = pParse->pNewTable; |
+ if( p==0 ) return; |
+ |
+ assert( !db->init.busy || !pSelect ); |
+ |
+ /* If the db->init.busy is 1 it means we are reading the SQL off the |
+ ** "sqlite_master" or "sqlite_temp_master" table on the disk. |
+ ** So do not write to the disk again. Extract the root page number |
+ ** for the table from the db->init.newTnum field. (The page number |
+ ** should have been put there by the sqliteOpenCb routine.) |
+ ** |
+ ** If the root page number is 1, that means this is the sqlite_master |
+ ** table itself. So mark it read-only. |
+ */ |
+ if( db->init.busy ){ |
+ p->tnum = db->init.newTnum; |
+ if( p->tnum==1 ) p->tabFlags |= TF_Readonly; |
+ } |
+ |
+ /* Special processing for WITHOUT ROWID Tables */ |
+ if( tabOpts & TF_WithoutRowid ){ |
+ if( (p->tabFlags & TF_Autoincrement) ){ |
+ sqlite3ErrorMsg(pParse, |
+ "AUTOINCREMENT not allowed on WITHOUT ROWID tables"); |
+ return; |
+ } |
+ if( (p->tabFlags & TF_HasPrimaryKey)==0 ){ |
+ sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName); |
+ }else{ |
+ p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid; |
+ convertToWithoutRowidTable(pParse, p); |
+ } |
+ } |
+ |
+ iDb = sqlite3SchemaToIndex(db, p->pSchema); |
+ |
+#ifndef SQLITE_OMIT_CHECK |
+ /* Resolve names in all CHECK constraint expressions. |
+ */ |
+ if( p->pCheck ){ |
+ sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck); |
+ } |
+#endif /* !defined(SQLITE_OMIT_CHECK) */ |
+ |
+ /* Estimate the average row size for the table and for all implied indices */ |
+ estimateTableWidth(p); |
+ for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
+ estimateIndexWidth(pIdx); |
+ } |
+ |
+ /* If not initializing, then create a record for the new table |
+ ** in the SQLITE_MASTER table of the database. |
+ ** |
+ ** If this is a TEMPORARY table, write the entry into the auxiliary |
+ ** file instead of into the main database file. |
+ */ |
+ if( !db->init.busy ){ |
+ int n; |
+ Vdbe *v; |
+ char *zType; /* "view" or "table" */ |
+ char *zType2; /* "VIEW" or "TABLE" */ |
+ char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */ |
+ |
+ v = sqlite3GetVdbe(pParse); |
+ if( NEVER(v==0) ) return; |
+ |
+ sqlite3VdbeAddOp1(v, OP_Close, 0); |
+ |
+ /* |
+ ** Initialize zType for the new view or table. |
+ */ |
+ if( p->pSelect==0 ){ |
+ /* A regular table */ |
+ zType = "table"; |
+ zType2 = "TABLE"; |
+#ifndef SQLITE_OMIT_VIEW |
+ }else{ |
+ /* A view */ |
+ zType = "view"; |
+ zType2 = "VIEW"; |
+#endif |
+ } |
+ |
+ /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT |
+ ** statement to populate the new table. The root-page number for the |
+ ** new table is in register pParse->regRoot. |
+ ** |
+ ** Once the SELECT has been coded by sqlite3Select(), it is in a |
+ ** suitable state to query for the column names and types to be used |
+ ** by the new table. |
+ ** |
+ ** A shared-cache write-lock is not required to write to the new table, |
+ ** as a schema-lock must have already been obtained to create it. Since |
+ ** a schema-lock excludes all other database users, the write-lock would |
+ ** be redundant. |
+ */ |
+ if( pSelect ){ |
+ SelectDest dest; /* Where the SELECT should store results */ |
+ int regYield; /* Register holding co-routine entry-point */ |
+ int addrTop; /* Top of the co-routine */ |
+ int regRec; /* A record to be insert into the new table */ |
+ int regRowid; /* Rowid of the next row to insert */ |
+ int addrInsLoop; /* Top of the loop for inserting rows */ |
+ Table *pSelTab; /* A table that describes the SELECT results */ |
+ |
+ regYield = ++pParse->nMem; |
+ regRec = ++pParse->nMem; |
+ regRowid = ++pParse->nMem; |
+ assert(pParse->nTab==1); |
+ sqlite3MayAbort(pParse); |
+ sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); |
+ sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); |
+ pParse->nTab = 2; |
+ addrTop = sqlite3VdbeCurrentAddr(v) + 1; |
+ sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); |
+ sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); |
+ sqlite3Select(pParse, pSelect, &dest); |
+ sqlite3VdbeEndCoroutine(v, regYield); |
+ sqlite3VdbeJumpHere(v, addrTop - 1); |
+ if( pParse->nErr ) return; |
+ pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect); |
+ if( pSelTab==0 ) return; |
+ assert( p->aCol==0 ); |
+ p->nCol = pSelTab->nCol; |
+ p->aCol = pSelTab->aCol; |
+ pSelTab->nCol = 0; |
+ pSelTab->aCol = 0; |
+ sqlite3DeleteTable(db, pSelTab); |
+ addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); |
+ VdbeCoverage(v); |
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec); |
+ sqlite3TableAffinity(v, p, 0); |
+ sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid); |
+ sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid); |
+ sqlite3VdbeGoto(v, addrInsLoop); |
+ sqlite3VdbeJumpHere(v, addrInsLoop); |
+ sqlite3VdbeAddOp1(v, OP_Close, 1); |
+ } |
+ |
+ /* Compute the complete text of the CREATE statement */ |
+ if( pSelect ){ |
+ zStmt = createTableStmt(db, p); |
+ }else{ |
+ Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd; |
+ n = (int)(pEnd2->z - pParse->sNameToken.z); |
+ if( pEnd2->z[0]!=';' ) n += pEnd2->n; |
+ zStmt = sqlite3MPrintf(db, |
+ "CREATE %s %.*s", zType2, n, pParse->sNameToken.z |
+ ); |
+ } |
+ |
+ /* A slot for the record has already been allocated in the |
+ ** SQLITE_MASTER table. We just need to update that slot with all |
+ ** the information we've collected. |
+ */ |
+ sqlite3NestedParse(pParse, |
+ "UPDATE %Q.%s " |
+ "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q " |
+ "WHERE rowid=#%d", |
+ db->aDb[iDb].zDbSName, MASTER_NAME, |
+ zType, |
+ p->zName, |
+ p->zName, |
+ pParse->regRoot, |
+ zStmt, |
+ pParse->regRowid |
+ ); |
+ sqlite3DbFree(db, zStmt); |
+ sqlite3ChangeCookie(pParse, iDb); |
+ |
+#ifndef SQLITE_OMIT_AUTOINCREMENT |
+ /* Check to see if we need to create an sqlite_sequence table for |
+ ** keeping track of autoincrement keys. |
+ */ |
+ if( (p->tabFlags & TF_Autoincrement)!=0 ){ |
+ Db *pDb = &db->aDb[iDb]; |
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
+ if( pDb->pSchema->pSeqTab==0 ){ |
+ sqlite3NestedParse(pParse, |
+ "CREATE TABLE %Q.sqlite_sequence(name,seq)", |
+ pDb->zDbSName |
+ ); |
+ } |
+ } |
+#endif |
+ |
+ /* Reparse everything to update our internal data structures */ |
+ sqlite3VdbeAddParseSchemaOp(v, iDb, |
+ sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName)); |
+ } |
+ |
+ |
+ /* Add the table to the in-memory representation of the database. |
+ */ |
+ if( db->init.busy ){ |
+ Table *pOld; |
+ Schema *pSchema = p->pSchema; |
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
+ pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p); |
+ if( pOld ){ |
+ assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ |
+ sqlite3OomFault(db); |
+ return; |
+ } |
+ pParse->pNewTable = 0; |
+ db->flags |= SQLITE_InternChanges; |
+ |
+#ifndef SQLITE_OMIT_ALTERTABLE |
+ if( !p->pSelect ){ |
+ const char *zName = (const char *)pParse->sNameToken.z; |
+ int nName; |
+ assert( !pSelect && pCons && pEnd ); |
+ if( pCons->z==0 ){ |
+ pCons = pEnd; |
+ } |
+ nName = (int)((const char *)pCons->z - zName); |
+ p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName); |
+ } |
+#endif |
+ } |
+} |
+ |
+#ifndef SQLITE_OMIT_VIEW |
+/* |
+** The parser calls this routine in order to create a new VIEW |
+*/ |
+SQLITE_PRIVATE void sqlite3CreateView( |
+ Parse *pParse, /* The parsing context */ |
+ Token *pBegin, /* The CREATE token that begins the statement */ |
+ Token *pName1, /* The token that holds the name of the view */ |
+ Token *pName2, /* The token that holds the name of the view */ |
+ ExprList *pCNames, /* Optional list of view column names */ |
+ Select *pSelect, /* A SELECT statement that will become the new view */ |
+ int isTemp, /* TRUE for a TEMPORARY view */ |
+ int noErr /* Suppress error messages if VIEW already exists */ |
+){ |
+ Table *p; |
+ int n; |
+ const char *z; |
+ Token sEnd; |
+ DbFixer sFix; |
+ Token *pName = 0; |
+ int iDb; |
+ sqlite3 *db = pParse->db; |
+ |
+ if( pParse->nVar>0 ){ |
+ sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); |
+ goto create_view_fail; |
+ } |
+ sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); |
+ p = pParse->pNewTable; |
+ if( p==0 || pParse->nErr ) goto create_view_fail; |
+ sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
+ iDb = sqlite3SchemaToIndex(db, p->pSchema); |
+ sqlite3FixInit(&sFix, pParse, iDb, "view", pName); |
+ if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail; |
+ |
+ /* Make a copy of the entire SELECT statement that defines the view. |
+ ** This will force all the Expr.token.z values to be dynamically |
+ ** allocated rather than point to the input string - which means that |
+ ** they will persist after the current sqlite3_exec() call returns. |
+ */ |
+ p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); |
+ p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE); |
+ if( db->mallocFailed ) goto create_view_fail; |
+ |
+ /* Locate the end of the CREATE VIEW statement. Make sEnd point to |
+ ** the end. |
+ */ |
+ sEnd = pParse->sLastToken; |
+ assert( sEnd.z[0]!=0 ); |
+ if( sEnd.z[0]!=';' ){ |
+ sEnd.z += sEnd.n; |
+ } |
+ sEnd.n = 0; |
+ n = (int)(sEnd.z - pBegin->z); |
+ assert( n>0 ); |
+ z = pBegin->z; |
+ while( sqlite3Isspace(z[n-1]) ){ n--; } |
+ sEnd.z = &z[n-1]; |
+ sEnd.n = 1; |
+ |
+ /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */ |
+ sqlite3EndTable(pParse, 0, &sEnd, 0, 0); |
+ |
+create_view_fail: |
+ sqlite3SelectDelete(db, pSelect); |
+ sqlite3ExprListDelete(db, pCNames); |
+ return; |
+} |
+#endif /* SQLITE_OMIT_VIEW */ |
+ |
+#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
+/* |
+** The Table structure pTable is really a VIEW. Fill in the names of |
+** the columns of the view in the pTable structure. Return the number |
+** of errors. If an error is seen leave an error message in pParse->zErrMsg. |
+*/ |
+SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ |
+ Table *pSelTab; /* A fake table from which we get the result set */ |
+ Select *pSel; /* Copy of the SELECT that implements the view */ |
+ int nErr = 0; /* Number of errors encountered */ |
+ int n; /* Temporarily holds the number of cursors assigned */ |
+ sqlite3 *db = pParse->db; /* Database connection for malloc errors */ |
+#ifndef SQLITE_OMIT_AUTHORIZATION |
+ sqlite3_xauth xAuth; /* Saved xAuth pointer */ |
+#endif |
+ |
+ assert( pTable ); |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+ if( sqlite3VtabCallConnect(pParse, pTable) ){ |
+ return SQLITE_ERROR; |
+ } |
+ if( IsVirtual(pTable) ) return 0; |
+#endif |
+ |
+#ifndef SQLITE_OMIT_VIEW |
+ /* A positive nCol means the columns names for this view are |
+ ** already known. |
+ */ |
+ if( pTable->nCol>0 ) return 0; |
+ |
+ /* A negative nCol is a special marker meaning that we are currently |
+ ** trying to compute the column names. If we enter this routine with |
+ ** a negative nCol, it means two or more views form a loop, like this: |
+ ** |
+ ** CREATE VIEW one AS SELECT * FROM two; |
+ ** CREATE VIEW two AS SELECT * FROM one; |
+ ** |
+ ** Actually, the error above is now caught prior to reaching this point. |
+ ** But the following test is still important as it does come up |
+ ** in the following: |
+ ** |
+ ** CREATE TABLE main.ex1(a); |
+ ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1; |
+ ** SELECT * FROM temp.ex1; |
+ */ |
+ if( pTable->nCol<0 ){ |
+ sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName); |
+ return 1; |
+ } |
+ assert( pTable->nCol>=0 ); |
+ |
+ /* If we get this far, it means we need to compute the table names. |
+ ** Note that the call to sqlite3ResultSetOfSelect() will expand any |
+ ** "*" elements in the results set of the view and will assign cursors |
+ ** to the elements of the FROM clause. But we do not want these changes |
+ ** to be permanent. So the computation is done on a copy of the SELECT |
+ ** statement that defines the view. |
+ */ |
+ assert( pTable->pSelect ); |
+ pSel = sqlite3SelectDup(db, pTable->pSelect, 0); |
+ if( pSel ){ |
+ n = pParse->nTab; |
+ sqlite3SrcListAssignCursors(pParse, pSel->pSrc); |
+ pTable->nCol = -1; |
+ db->lookaside.bDisable++; |
+#ifndef SQLITE_OMIT_AUTHORIZATION |
+ xAuth = db->xAuth; |
+ db->xAuth = 0; |
+ pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); |
+ db->xAuth = xAuth; |
+#else |
+ pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); |
+#endif |
+ pParse->nTab = n; |
+ if( pTable->pCheck ){ |
+ /* CREATE VIEW name(arglist) AS ... |
+ ** The names of the columns in the table are taken from |
+ ** arglist which is stored in pTable->pCheck. The pCheck field |
+ ** normally holds CHECK constraints on an ordinary table, but for |
+ ** a VIEW it holds the list of column names. |
+ */ |
+ sqlite3ColumnsFromExprList(pParse, pTable->pCheck, |
+ &pTable->nCol, &pTable->aCol); |
+ if( db->mallocFailed==0 |
+ && pParse->nErr==0 |
+ && pTable->nCol==pSel->pEList->nExpr |
+ ){ |
+ sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel); |
+ } |
+ }else if( pSelTab ){ |
+ /* CREATE VIEW name AS... without an argument list. Construct |
+ ** the column names from the SELECT statement that defines the view. |
+ */ |
+ assert( pTable->aCol==0 ); |
+ pTable->nCol = pSelTab->nCol; |
+ pTable->aCol = pSelTab->aCol; |
+ pSelTab->nCol = 0; |
+ pSelTab->aCol = 0; |
+ assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) ); |
+ }else{ |
+ pTable->nCol = 0; |
+ nErr++; |
+ } |
+ sqlite3DeleteTable(db, pSelTab); |
+ sqlite3SelectDelete(db, pSel); |
+ db->lookaside.bDisable--; |
+ } else { |
+ nErr++; |
+ } |
+ pTable->pSchema->schemaFlags |= DB_UnresetViews; |
+#endif /* SQLITE_OMIT_VIEW */ |
+ return nErr; |
+} |
+#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
+ |
+#ifndef SQLITE_OMIT_VIEW |
+/* |
+** Clear the column names from every VIEW in database idx. |
+*/ |
+static void sqliteViewResetAll(sqlite3 *db, int idx){ |
+ HashElem *i; |
+ assert( sqlite3SchemaMutexHeld(db, idx, 0) ); |
+ if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; |
+ for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ |
+ Table *pTab = sqliteHashData(i); |
+ if( pTab->pSelect ){ |
+ sqlite3DeleteColumnNames(db, pTab); |
+ pTab->aCol = 0; |
+ pTab->nCol = 0; |
+ } |
+ } |
+ DbClearProperty(db, idx, DB_UnresetViews); |
+} |
+#else |
+# define sqliteViewResetAll(A,B) |
+#endif /* SQLITE_OMIT_VIEW */ |
+ |
+/* |
+** This function is called by the VDBE to adjust the internal schema |
+** used by SQLite when the btree layer moves a table root page. The |
+** root-page of a table or index in database iDb has changed from iFrom |
+** to iTo. |
+** |
+** Ticket #1728: The symbol table might still contain information |
+** on tables and/or indices that are the process of being deleted. |
+** If you are unlucky, one of those deleted indices or tables might |
+** have the same rootpage number as the real table or index that is |
+** being moved. So we cannot stop searching after the first match |
+** because the first match might be for one of the deleted indices |
+** or tables and not the table/index that is actually being moved. |
+** We must continue looping until all tables and indices with |
+** rootpage==iFrom have been converted to have a rootpage of iTo |
+** in order to be certain that we got the right one. |
+*/ |
+#ifndef SQLITE_OMIT_AUTOVACUUM |
+SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){ |
+ HashElem *pElem; |
+ Hash *pHash; |
+ Db *pDb; |
+ |
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
+ pDb = &db->aDb[iDb]; |
+ pHash = &pDb->pSchema->tblHash; |
+ for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ |
+ Table *pTab = sqliteHashData(pElem); |
+ if( pTab->tnum==iFrom ){ |
+ pTab->tnum = iTo; |
+ } |
+ } |
+ pHash = &pDb->pSchema->idxHash; |
+ for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ |
+ Index *pIdx = sqliteHashData(pElem); |
+ if( pIdx->tnum==iFrom ){ |
+ pIdx->tnum = iTo; |
+ } |
+ } |
+} |
+#endif |
+ |
+/* |
+** Write code to erase the table with root-page iTable from database iDb. |
+** Also write code to modify the sqlite_master table and internal schema |
+** if a root-page of another table is moved by the btree-layer whilst |
+** erasing iTable (this can happen with an auto-vacuum database). |
+*/ |
+static void destroyRootPage(Parse *pParse, int iTable, int iDb){ |
+ Vdbe *v = sqlite3GetVdbe(pParse); |
+ int r1 = sqlite3GetTempReg(pParse); |
+ assert( iTable>1 ); |
+ sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb); |
+ sqlite3MayAbort(pParse); |
+#ifndef SQLITE_OMIT_AUTOVACUUM |
+ /* OP_Destroy stores an in integer r1. If this integer |
+ ** is non-zero, then it is the root page number of a table moved to |
+ ** location iTable. The following code modifies the sqlite_master table to |
+ ** reflect this. |
+ ** |
+ ** The "#NNN" in the SQL is a special constant that means whatever value |
+ ** is in register NNN. See grammar rules associated with the TK_REGISTER |
+ ** token for additional information. |
+ */ |
+ sqlite3NestedParse(pParse, |
+ "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d", |
+ pParse->db->aDb[iDb].zDbSName, MASTER_NAME, iTable, r1, r1); |
+#endif |
+ sqlite3ReleaseTempReg(pParse, r1); |
+} |
+ |
+/* |
+** Write VDBE code to erase table pTab and all associated indices on disk. |
+** Code to update the sqlite_master tables and internal schema definitions |
+** in case a root-page belonging to another table is moved by the btree layer |
+** is also added (this can happen with an auto-vacuum database). |
+*/ |
+static void destroyTable(Parse *pParse, Table *pTab){ |
+#ifdef SQLITE_OMIT_AUTOVACUUM |
+ Index *pIdx; |
+ int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
+ destroyRootPage(pParse, pTab->tnum, iDb); |
+ for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
+ destroyRootPage(pParse, pIdx->tnum, iDb); |
+ } |
+#else |
+ /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM |
+ ** is not defined), then it is important to call OP_Destroy on the |
+ ** table and index root-pages in order, starting with the numerically |
+ ** largest root-page number. This guarantees that none of the root-pages |
+ ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the |
+ ** following were coded: |
+ ** |
+ ** OP_Destroy 4 0 |
+ ** ... |
+ ** OP_Destroy 5 0 |
+ ** |
+ ** and root page 5 happened to be the largest root-page number in the |
+ ** database, then root page 5 would be moved to page 4 by the |
+ ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit |
+ ** a free-list page. |
+ */ |
+ int iTab = pTab->tnum; |
+ int iDestroyed = 0; |
+ |
+ while( 1 ){ |
+ Index *pIdx; |
+ int iLargest = 0; |
+ |
+ if( iDestroyed==0 || iTab<iDestroyed ){ |
+ iLargest = iTab; |
+ } |
+ for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
+ int iIdx = pIdx->tnum; |
+ assert( pIdx->pSchema==pTab->pSchema ); |
+ if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){ |
+ iLargest = iIdx; |
+ } |
+ } |
+ if( iLargest==0 ){ |
+ return; |
+ }else{ |
+ int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
+ assert( iDb>=0 && iDb<pParse->db->nDb ); |
+ destroyRootPage(pParse, iLargest, iDb); |
+ iDestroyed = iLargest; |
+ } |
+ } |
+#endif |
+} |
+ |
+/* |
+** Remove entries from the sqlite_statN tables (for N in (1,2,3)) |
+** after a DROP INDEX or DROP TABLE command. |
+*/ |
+static void sqlite3ClearStatTables( |
+ Parse *pParse, /* The parsing context */ |
+ int iDb, /* The database number */ |
+ const char *zType, /* "idx" or "tbl" */ |
+ const char *zName /* Name of index or table */ |
+){ |
+ int i; |
+ const char *zDbName = pParse->db->aDb[iDb].zDbSName; |
+ for(i=1; i<=4; i++){ |
+ char zTab[24]; |
+ sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i); |
+ if( sqlite3FindTable(pParse->db, zTab, zDbName) ){ |
+ sqlite3NestedParse(pParse, |
+ "DELETE FROM %Q.%s WHERE %s=%Q", |
+ zDbName, zTab, zType, zName |
+ ); |
+ } |
+ } |
+} |
+ |
+/* |
+** Generate code to drop a table. |
+*/ |
+SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){ |
+ Vdbe *v; |
+ sqlite3 *db = pParse->db; |
+ Trigger *pTrigger; |
+ Db *pDb = &db->aDb[iDb]; |
+ |
+ v = sqlite3GetVdbe(pParse); |
+ assert( v!=0 ); |
+ sqlite3BeginWriteOperation(pParse, 1, iDb); |
+ |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+ if( IsVirtual(pTab) ){ |
+ sqlite3VdbeAddOp0(v, OP_VBegin); |
+ } |
+#endif |
+ |
+ /* Drop all triggers associated with the table being dropped. Code |
+ ** is generated to remove entries from sqlite_master and/or |
+ ** sqlite_temp_master if required. |
+ */ |
+ pTrigger = sqlite3TriggerList(pParse, pTab); |
+ while( pTrigger ){ |
+ assert( pTrigger->pSchema==pTab->pSchema || |
+ pTrigger->pSchema==db->aDb[1].pSchema ); |
+ sqlite3DropTriggerPtr(pParse, pTrigger); |
+ pTrigger = pTrigger->pNext; |
+ } |
+ |
+#ifndef SQLITE_OMIT_AUTOINCREMENT |
+ /* Remove any entries of the sqlite_sequence table associated with |
+ ** the table being dropped. This is done before the table is dropped |
+ ** at the btree level, in case the sqlite_sequence table needs to |
+ ** move as a result of the drop (can happen in auto-vacuum mode). |
+ */ |
+ if( pTab->tabFlags & TF_Autoincrement ){ |
+ sqlite3NestedParse(pParse, |
+ "DELETE FROM %Q.sqlite_sequence WHERE name=%Q", |
+ pDb->zDbSName, pTab->zName |
+ ); |
+ } |
+#endif |
+ |
+ /* Drop all SQLITE_MASTER table and index entries that refer to the |
+ ** table. The program name loops through the master table and deletes |
+ ** every row that refers to a table of the same name as the one being |
+ ** dropped. Triggers are handled separately because a trigger can be |
+ ** created in the temp database that refers to a table in another |
+ ** database. |
+ */ |
+ sqlite3NestedParse(pParse, |
+ "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", |
+ pDb->zDbSName, MASTER_NAME, pTab->zName); |
+ if( !isView && !IsVirtual(pTab) ){ |
+ destroyTable(pParse, pTab); |
+ } |
+ |
+ /* Remove the table entry from SQLite's internal schema and modify |
+ ** the schema cookie. |
+ */ |
+ if( IsVirtual(pTab) ){ |
+ sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); |
+ } |
+ sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
+ sqlite3ChangeCookie(pParse, iDb); |
+ sqliteViewResetAll(db, iDb); |
+} |
+ |
+/* |
+** This routine is called to do the work of a DROP TABLE statement. |
+** pName is the name of the table to be dropped. |
+*/ |
+SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ |
+ Table *pTab; |
+ Vdbe *v; |
+ sqlite3 *db = pParse->db; |
+ int iDb; |
+ |
+ if( db->mallocFailed ){ |
+ goto exit_drop_table; |
+ } |
+ assert( pParse->nErr==0 ); |
+ assert( pName->nSrc==1 ); |
+ if( sqlite3ReadSchema(pParse) ) goto exit_drop_table; |
+ if( noErr ) db->suppressErr++; |
+ assert( isView==0 || isView==LOCATE_VIEW ); |
+ pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); |
+ if( noErr ) db->suppressErr--; |
+ |
+ if( pTab==0 ){ |
+ if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); |
+ goto exit_drop_table; |
+ } |
+ iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
+ assert( iDb>=0 && iDb<db->nDb ); |
+ |
+ /* If pTab is a virtual table, call ViewGetColumnNames() to ensure |
+ ** it is initialized. |
+ */ |
+ if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){ |
+ goto exit_drop_table; |
+ } |
+#ifndef SQLITE_OMIT_AUTHORIZATION |
+ { |
+ int code; |
+ const char *zTab = SCHEMA_TABLE(iDb); |
+ const char *zDb = db->aDb[iDb].zDbSName; |
+ const char *zArg2 = 0; |
+ if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ |
+ goto exit_drop_table; |
+ } |
+ if( isView ){ |
+ if( !OMIT_TEMPDB && iDb==1 ){ |
+ code = SQLITE_DROP_TEMP_VIEW; |
+ }else{ |
+ code = SQLITE_DROP_VIEW; |
+ } |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+ }else if( IsVirtual(pTab) ){ |
+ code = SQLITE_DROP_VTABLE; |
+ zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName; |
+#endif |
+ }else{ |
+ if( !OMIT_TEMPDB && iDb==1 ){ |
+ code = SQLITE_DROP_TEMP_TABLE; |
+ }else{ |
+ code = SQLITE_DROP_TABLE; |
+ } |
+ } |
+ if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){ |
+ goto exit_drop_table; |
+ } |
+ if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
+ goto exit_drop_table; |
+ } |
+ } |
+#endif |
+ if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 |
+ && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){ |
+ sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); |
+ goto exit_drop_table; |
+ } |
+ |
+#ifndef SQLITE_OMIT_VIEW |
+ /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used |
+ ** on a table. |
+ */ |
+ if( isView && pTab->pSelect==0 ){ |
+ sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); |
+ goto exit_drop_table; |
+ } |
+ if( !isView && pTab->pSelect ){ |
+ sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName); |
+ goto exit_drop_table; |
+ } |
+#endif |
+ |
+ /* Generate code to remove the table from the master table |
+ ** on disk. |
+ */ |
+ v = sqlite3GetVdbe(pParse); |
+ if( v ){ |
+ sqlite3BeginWriteOperation(pParse, 1, iDb); |
+ sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); |
+ sqlite3FkDropTable(pParse, pName, pTab); |
+ sqlite3CodeDropTable(pParse, pTab, iDb, isView); |
+ } |
+ |
+exit_drop_table: |
+ sqlite3SrcListDelete(db, pName); |
+} |
+ |
+/* |
+** This routine is called to create a new foreign key on the table |
+** currently under construction. pFromCol determines which columns |
+** in the current table point to the foreign key. If pFromCol==0 then |
+** connect the key to the last column inserted. pTo is the name of |
+** the table referred to (a.k.a the "parent" table). pToCol is a list |
+** of tables in the parent pTo table. flags contains all |
+** information about the conflict resolution algorithms specified |
+** in the ON DELETE, ON UPDATE and ON INSERT clauses. |
+** |
+** An FKey structure is created and added to the table currently |
+** under construction in the pParse->pNewTable field. |
+** |
+** The foreign key is set for IMMEDIATE processing. A subsequent call |
+** to sqlite3DeferForeignKey() might change this to DEFERRED. |
+*/ |
+SQLITE_PRIVATE void sqlite3CreateForeignKey( |
+ Parse *pParse, /* Parsing context */ |
+ ExprList *pFromCol, /* Columns in this table that point to other table */ |
+ Token *pTo, /* Name of the other table */ |
+ ExprList *pToCol, /* Columns in the other table */ |
+ int flags /* Conflict resolution algorithms. */ |
+){ |
+ sqlite3 *db = pParse->db; |
+#ifndef SQLITE_OMIT_FOREIGN_KEY |
+ FKey *pFKey = 0; |
+ FKey *pNextTo; |
+ Table *p = pParse->pNewTable; |
+ int nByte; |
+ int i; |
+ int nCol; |
+ char *z; |
+ |
+ assert( pTo!=0 ); |
+ if( p==0 || IN_DECLARE_VTAB ) goto fk_end; |
+ if( pFromCol==0 ){ |
+ int iCol = p->nCol-1; |
+ if( NEVER(iCol<0) ) goto fk_end; |
+ if( pToCol && pToCol->nExpr!=1 ){ |
+ sqlite3ErrorMsg(pParse, "foreign key on %s" |
+ " should reference only one column of table %T", |
+ p->aCol[iCol].zName, pTo); |
+ goto fk_end; |
+ } |
+ nCol = 1; |
+ }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){ |
+ sqlite3ErrorMsg(pParse, |
+ "number of columns in foreign key does not match the number of " |
+ "columns in the referenced table"); |
+ goto fk_end; |
+ }else{ |
+ nCol = pFromCol->nExpr; |
+ } |
+ nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1; |
+ if( pToCol ){ |
+ for(i=0; i<pToCol->nExpr; i++){ |
+ nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1; |
+ } |
+ } |
+ pFKey = sqlite3DbMallocZero(db, nByte ); |
+ if( pFKey==0 ){ |
+ goto fk_end; |
+ } |
+ pFKey->pFrom = p; |
+ pFKey->pNextFrom = p->pFKey; |
+ z = (char*)&pFKey->aCol[nCol]; |
+ pFKey->zTo = z; |
+ memcpy(z, pTo->z, pTo->n); |
+ z[pTo->n] = 0; |
+ sqlite3Dequote(z); |
+ z += pTo->n+1; |
+ pFKey->nCol = nCol; |
+ if( pFromCol==0 ){ |
+ pFKey->aCol[0].iFrom = p->nCol-1; |
+ }else{ |
+ for(i=0; i<nCol; i++){ |
+ int j; |
+ for(j=0; j<p->nCol; j++){ |
+ if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){ |
+ pFKey->aCol[i].iFrom = j; |
+ break; |
+ } |
+ } |
+ if( j>=p->nCol ){ |
+ sqlite3ErrorMsg(pParse, |
+ "unknown column \"%s\" in foreign key definition", |
+ pFromCol->a[i].zName); |
+ goto fk_end; |
+ } |
+ } |
+ } |
+ if( pToCol ){ |
+ for(i=0; i<nCol; i++){ |
+ int n = sqlite3Strlen30(pToCol->a[i].zName); |
+ pFKey->aCol[i].zCol = z; |
+ memcpy(z, pToCol->a[i].zName, n); |
+ z[n] = 0; |
+ z += n+1; |
+ } |
+ } |
+ pFKey->isDeferred = 0; |
+ pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */ |
+ pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */ |
+ |
+ assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); |
+ pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, |
+ pFKey->zTo, (void *)pFKey |
+ ); |
+ if( pNextTo==pFKey ){ |
+ sqlite3OomFault(db); |
+ goto fk_end; |
+ } |
+ if( pNextTo ){ |
+ assert( pNextTo->pPrevTo==0 ); |
+ pFKey->pNextTo = pNextTo; |
+ pNextTo->pPrevTo = pFKey; |
+ } |
+ |
+ /* Link the foreign key to the table as the last step. |
+ */ |
+ p->pFKey = pFKey; |
+ pFKey = 0; |
+ |
+fk_end: |
+ sqlite3DbFree(db, pFKey); |
+#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
+ sqlite3ExprListDelete(db, pFromCol); |
+ sqlite3ExprListDelete(db, pToCol); |
+} |
+ |
+/* |
+** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED |
+** clause is seen as part of a foreign key definition. The isDeferred |
+** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. |
+** The behavior of the most recently created foreign key is adjusted |
+** accordingly. |
+*/ |
+SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ |
+#ifndef SQLITE_OMIT_FOREIGN_KEY |
+ Table *pTab; |
+ FKey *pFKey; |
+ if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; |
+ assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */ |
+ pFKey->isDeferred = (u8)isDeferred; |
+#endif |
+} |
+ |
+/* |
+** Generate code that will erase and refill index *pIdx. This is |
+** used to initialize a newly created index or to recompute the |
+** content of an index in response to a REINDEX command. |
+** |
+** if memRootPage is not negative, it means that the index is newly |
+** created. The register specified by memRootPage contains the |
+** root page number of the index. If memRootPage is negative, then |
+** the index already exists and must be cleared before being refilled and |
+** the root page number of the index is taken from pIndex->tnum. |
+*/ |
+static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ |
+ Table *pTab = pIndex->pTable; /* The table that is indexed */ |
+ int iTab = pParse->nTab++; /* Btree cursor used for pTab */ |
+ int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */ |
+ int iSorter; /* Cursor opened by OpenSorter (if in use) */ |
+ int addr1; /* Address of top of loop */ |
+ int addr2; /* Address to jump to for next iteration */ |
+ int tnum; /* Root page of index */ |
+ int iPartIdxLabel; /* Jump to this label to skip a row */ |
+ Vdbe *v; /* Generate code into this virtual machine */ |
+ KeyInfo *pKey; /* KeyInfo for index */ |
+ int regRecord; /* Register holding assembled index record */ |
+ sqlite3 *db = pParse->db; /* The database connection */ |
+ int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
+ |
+#ifndef SQLITE_OMIT_AUTHORIZATION |
+ if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, |
+ db->aDb[iDb].zDbSName ) ){ |
+ return; |
+ } |
+#endif |
+ |
+ /* Require a write-lock on the table to perform this operation */ |
+ sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); |
+ |
+ v = sqlite3GetVdbe(pParse); |
+ if( v==0 ) return; |
+ if( memRootPage>=0 ){ |
+ tnum = memRootPage; |
+ }else{ |
+ tnum = pIndex->tnum; |
+ } |
+ pKey = sqlite3KeyInfoOfIndex(pParse, pIndex); |
+ assert( pKey!=0 || db->mallocFailed || pParse->nErr ); |
+ |
+ /* Open the sorter cursor if we are to use one. */ |
+ iSorter = pParse->nTab++; |
+ sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*) |
+ sqlite3KeyInfoRef(pKey), P4_KEYINFO); |
+ |
+ /* Open the table. Loop through all rows of the table, inserting index |
+ ** records into the sorter. */ |
+ sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
+ addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v); |
+ regRecord = sqlite3GetTempReg(pParse); |
+ |
+ sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0); |
+ sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); |
+ sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); |
+ sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v); |
+ sqlite3VdbeJumpHere(v, addr1); |
+ if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); |
+ sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, |
+ (char *)pKey, P4_KEYINFO); |
+ sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); |
+ |
+ addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); |
+ if( IsUniqueIndex(pIndex) ){ |
+ int j2 = sqlite3VdbeCurrentAddr(v) + 3; |
+ sqlite3VdbeGoto(v, j2); |
+ addr2 = sqlite3VdbeCurrentAddr(v); |
+ sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, |
+ pIndex->nKeyCol); VdbeCoverage(v); |
+ sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); |
+ }else{ |
+ addr2 = sqlite3VdbeCurrentAddr(v); |
+ } |
+ sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); |
+ sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1); |
+ sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); |
+ sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
+ sqlite3ReleaseTempReg(pParse, regRecord); |
+ sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); |
+ sqlite3VdbeJumpHere(v, addr1); |
+ |
+ sqlite3VdbeAddOp1(v, OP_Close, iTab); |
+ sqlite3VdbeAddOp1(v, OP_Close, iIdx); |
+ sqlite3VdbeAddOp1(v, OP_Close, iSorter); |
+} |
+ |
+/* |
+** Allocate heap space to hold an Index object with nCol columns. |
+** |
+** Increase the allocation size to provide an extra nExtra bytes |
+** of 8-byte aligned space after the Index object and return a |
+** pointer to this extra space in *ppExtra. |
+*/ |
+SQLITE_PRIVATE Index *sqlite3AllocateIndexObject( |
+ sqlite3 *db, /* Database connection */ |
+ i16 nCol, /* Total number of columns in the index */ |
+ int nExtra, /* Number of bytes of extra space to alloc */ |
+ char **ppExtra /* Pointer to the "extra" space */ |
+){ |
+ Index *p; /* Allocated index object */ |
+ int nByte; /* Bytes of space for Index object + arrays */ |
+ |
+ nByte = ROUND8(sizeof(Index)) + /* Index structure */ |
+ ROUND8(sizeof(char*)*nCol) + /* Index.azColl */ |
+ ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */ |
+ sizeof(i16)*nCol + /* Index.aiColumn */ |
+ sizeof(u8)*nCol); /* Index.aSortOrder */ |
+ p = sqlite3DbMallocZero(db, nByte + nExtra); |
+ if( p ){ |
+ char *pExtra = ((char*)p)+ROUND8(sizeof(Index)); |
+ p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol); |
+ p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1); |
+ p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol; |
+ p->aSortOrder = (u8*)pExtra; |
+ p->nColumn = nCol; |
+ p->nKeyCol = nCol - 1; |
+ *ppExtra = ((char*)p) + nByte; |
+ } |
+ return p; |
+} |
+ |
+/* |
+** Create a new index for an SQL table. pName1.pName2 is the name of the index |
+** and pTblList is the name of the table that is to be indexed. Both will |
+** be NULL for a primary key or an index that is created to satisfy a |
+** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
+** as the table to be indexed. pParse->pNewTable is a table that is |
+** currently being constructed by a CREATE TABLE statement. |
+** |
+** pList is a list of columns to be indexed. pList will be NULL if this |
+** is a primary key or unique-constraint on the most recent column added |
+** to the table currently under construction. |
+*/ |
+SQLITE_PRIVATE void sqlite3CreateIndex( |
+ Parse *pParse, /* All information about this parse */ |
+ Token *pName1, /* First part of index name. May be NULL */ |
+ Token *pName2, /* Second part of index name. May be NULL */ |
+ SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ |
+ ExprList *pList, /* A list of columns to be indexed */ |
+ int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
+ Token *pStart, /* The CREATE token that begins this statement */ |
+ Expr *pPIWhere, /* WHERE clause for partial indices */ |
+ int sortOrder, /* Sort order of primary key when pList==NULL */ |
+ int ifNotExist, /* Omit error if index already exists */ |
+ u8 idxType /* The index type */ |
+){ |
+ Table *pTab = 0; /* Table to be indexed */ |
+ Index *pIndex = 0; /* The index to be created */ |
+ char *zName = 0; /* Name of the index */ |
+ int nName; /* Number of characters in zName */ |
+ int i, j; |
+ DbFixer sFix; /* For assigning database names to pTable */ |
+ int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ |
+ sqlite3 *db = pParse->db; |
+ Db *pDb; /* The specific table containing the indexed database */ |
+ int iDb; /* Index of the database that is being written */ |
+ Token *pName = 0; /* Unqualified name of the index to create */ |
+ struct ExprList_item *pListItem; /* For looping over pList */ |
+ int nExtra = 0; /* Space allocated for zExtra[] */ |
+ int nExtraCol; /* Number of extra columns needed */ |
+ char *zExtra = 0; /* Extra space after the Index object */ |
+ Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ |
+ |
+ if( db->mallocFailed || pParse->nErr>0 ){ |
+ goto exit_create_index; |
+ } |
+ if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){ |
+ goto exit_create_index; |
+ } |
+ if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
+ goto exit_create_index; |
+ } |
+ |
+ /* |
+ ** Find the table that is to be indexed. Return early if not found. |
+ */ |
+ if( pTblName!=0 ){ |
+ |
+ /* Use the two-part index name to determine the database |
+ ** to search for the table. 'Fix' the table name to this db |
+ ** before looking up the table. |
+ */ |
+ assert( pName1 && pName2 ); |
+ iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
+ if( iDb<0 ) goto exit_create_index; |
+ assert( pName && pName->z ); |
+ |
+#ifndef SQLITE_OMIT_TEMPDB |
+ /* If the index name was unqualified, check if the table |
+ ** is a temp table. If so, set the database to 1. Do not do this |
+ ** if initialising a database schema. |
+ */ |
+ if( !db->init.busy ){ |
+ pTab = sqlite3SrcListLookup(pParse, pTblName); |
+ if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ |
+ iDb = 1; |
+ } |
+ } |
+#endif |
+ |
+ sqlite3FixInit(&sFix, pParse, iDb, "index", pName); |
+ if( sqlite3FixSrcList(&sFix, pTblName) ){ |
+ /* Because the parser constructs pTblName from a single identifier, |
+ ** sqlite3FixSrcList can never fail. */ |
+ assert(0); |
+ } |
+ pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]); |
+ assert( db->mallocFailed==0 || pTab==0 ); |
+ if( pTab==0 ) goto exit_create_index; |
+ if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){ |
+ sqlite3ErrorMsg(pParse, |
+ "cannot create a TEMP index on non-TEMP table \"%s\"", |
+ pTab->zName); |
+ goto exit_create_index; |
+ } |
+ if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab); |
+ }else{ |
+ assert( pName==0 ); |
+ assert( pStart==0 ); |
+ pTab = pParse->pNewTable; |
+ if( !pTab ) goto exit_create_index; |
+ iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
+ } |
+ pDb = &db->aDb[iDb]; |
+ |
+ assert( pTab!=0 ); |
+ assert( pParse->nErr==0 ); |
+ if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 |
+ && db->init.busy==0 |
+#if SQLITE_USER_AUTHENTICATION |
+ && sqlite3UserAuthTable(pTab->zName)==0 |
+#endif |
+ && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){ |
+ sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); |
+ goto exit_create_index; |
+ } |
+#ifndef SQLITE_OMIT_VIEW |
+ if( pTab->pSelect ){ |
+ sqlite3ErrorMsg(pParse, "views may not be indexed"); |
+ goto exit_create_index; |
+ } |
+#endif |
+#ifndef SQLITE_OMIT_VIRTUALTABLE |
+ if( IsVirtual(pTab) ){ |
+ sqlite3ErrorMsg(pParse, "virtual tables may not be indexed"); |
+ goto exit_create_index; |
+ } |
+#endif |
+ |
+ /* |
+ ** Find the name of the index. Make sure there is not already another |
+ ** index or table with the same name. |
+ ** |
+ ** Exception: If we are reading the names of permanent indices from the |
+ ** sqlite_master table (because some other process changed the schema) and |
+ ** one of the index names collides with the name of a temporary table or |
+ ** index, then we will continue to process this index. |
+ ** |
+ ** If pName==0 it means that we are |
+ ** dealing with a primary key or UNIQUE constraint. We have to invent our |
+ ** own name. |
+ */ |
+ if( pName ){ |
+ zName = sqlite3NameFromToken(db, pName); |
+ if( zName==0 ) goto exit_create_index; |
+ assert( pName->z!=0 ); |
+ if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ |
+ goto exit_create_index; |
+ } |
+ if( !db->init.busy ){ |
+ if( sqlite3FindTable(db, zName, 0)!=0 ){ |
+ sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); |
+ goto exit_create_index; |
+ } |
+ } |
+ if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){ |
+ if( !ifNotExist ){ |
+ sqlite3ErrorMsg(pParse, "index %s already exists", zName); |
+ }else{ |
+ assert( !db->init.busy ); |
+ sqlite3CodeVerifySchema(pParse, iDb); |
+ } |
+ goto exit_create_index; |
+ } |
+ }else{ |
+ int n; |
+ Index *pLoop; |
+ for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} |
+ zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n); |
+ if( zName==0 ){ |
+ goto exit_create_index; |
+ } |
+ |
+ /* Automatic index names generated from within sqlite3_declare_vtab() |
+ ** must have names that are distinct from normal automatic index names. |
+ ** The following statement converts "sqlite3_autoindex..." into |
+ ** "sqlite3_butoindex..." in order to make the names distinct. |
+ ** The "vtab_err.test" test demonstrates the need of this statement. */ |
+ if( IN_DECLARE_VTAB ) zName[7]++; |
+ } |
+ |
+ /* Check for authorization to create an index. |
+ */ |
+#ifndef SQLITE_OMIT_AUTHORIZATION |
+ { |
+ const char *zDb = pDb->zDbSName; |
+ if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ |
+ goto exit_create_index; |
+ } |
+ i = SQLITE_CREATE_INDEX; |
+ if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; |
+ if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ |
+ goto exit_create_index; |
+ } |
+ } |
+#endif |
+ |
+ /* If pList==0, it means this routine was called to make a primary |
+ ** key out of the last column added to the table under construction. |
+ ** So create a fake list to simulate this. |
+ */ |
+ if( pList==0 ){ |
+ Token prevCol; |
+ sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName); |
+ pList = sqlite3ExprListAppend(pParse, 0, |
+ sqlite3ExprAlloc(db, TK_ID, &prevCol, 0)); |
+ if( pList==0 ) goto exit_create_index; |
+ assert( pList->nExpr==1 ); |
+ sqlite3ExprListSetSortOrder(pList, sortOrder); |
+ }else{ |
+ sqlite3ExprListCheckLength(pParse, pList, "index"); |
+ } |
+ |
+ /* Figure out how many bytes of space are required to store explicitly |
+ ** specified collation sequence names. |
+ */ |
+ for(i=0; i<pList->nExpr; i++){ |
+ Expr *pExpr = pList->a[i].pExpr; |
+ assert( pExpr!=0 ); |
+ if( pExpr->op==TK_COLLATE ){ |
+ nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken)); |
+ } |
+ } |
+ |
+ /* |
+ ** Allocate the index structure. |
+ */ |
+ nName = sqlite3Strlen30(zName); |
+ nExtraCol = pPk ? pPk->nKeyCol : 1; |
+ pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol, |
+ nName + nExtra + 1, &zExtra); |
+ if( db->mallocFailed ){ |
+ goto exit_create_index; |
+ } |
+ assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) ); |
+ assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) ); |
+ pIndex->zName = zExtra; |
+ zExtra += nName + 1; |
+ memcpy(pIndex->zName, zName, nName+1); |
+ pIndex->pTable = pTab; |
+ pIndex->onError = (u8)onError; |
+ pIndex->uniqNotNull = onError!=OE_None; |
+ pIndex->idxType = idxType; |
+ pIndex->pSchema = db->aDb[iDb].pSchema; |
+ pIndex->nKeyCol = pList->nExpr; |
+ if( pPIWhere ){ |
+ sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0); |
+ pIndex->pPartIdxWhere = pPIWhere; |
+ pPIWhere = 0; |
+ } |
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
+ |
+ /* Check to see if we should honor DESC requests on index columns |
+ */ |
+ if( pDb->pSchema->file_format>=4 ){ |
+ sortOrderMask = -1; /* Honor DESC */ |
+ }else{ |
+ sortOrderMask = 0; /* Ignore DESC */ |
+ } |
+ |
+ /* Analyze the list of expressions that form the terms of the index and |
+ ** report any errors. In the common case where the expression is exactly |
+ ** a table column, store that column in aiColumn[]. For general expressions, |
+ ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[]. |
+ ** |
+ ** TODO: Issue a warning if two or more columns of the index are identical. |
+ ** TODO: Issue a warning if the table primary key is used as part of the |
+ ** index key. |
+ */ |
+ for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){ |
+ Expr *pCExpr; /* The i-th index expression */ |
+ int requestedSortOrder; /* ASC or DESC on the i-th expression */ |
+ const char *zColl; /* Collation sequence name */ |
+ |
+ sqlite3StringToId(pListItem->pExpr); |
+ sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0); |
+ if( pParse->nErr ) goto exit_create_index; |
+ pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr); |
+ if( pCExpr->op!=TK_COLUMN ){ |
+ if( pTab==pParse->pNewTable ){ |
+ sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and " |
+ "UNIQUE constraints"); |
+ goto exit_create_index; |
+ } |
+ if( pIndex->aColExpr==0 ){ |
+ ExprList *pCopy = sqlite3ExprListDup(db, pList, 0); |
+ pIndex->aColExpr = pCopy; |
+ if( !db->mallocFailed ){ |
+ assert( pCopy!=0 ); |
+ pListItem = &pCopy->a[i]; |
+ } |
+ } |
+ j = XN_EXPR; |
+ pIndex->aiColumn[i] = XN_EXPR; |
+ pIndex->uniqNotNull = 0; |
+ }else{ |
+ j = pCExpr->iColumn; |
+ assert( j<=0x7fff ); |
+ if( j<0 ){ |
+ j = pTab->iPKey; |
+ }else if( pTab->aCol[j].notNull==0 ){ |
+ pIndex->uniqNotNull = 0; |
+ } |
+ pIndex->aiColumn[i] = (i16)j; |
+ } |
+ zColl = 0; |
+ if( pListItem->pExpr->op==TK_COLLATE ){ |
+ int nColl; |
+ zColl = pListItem->pExpr->u.zToken; |
+ nColl = sqlite3Strlen30(zColl) + 1; |
+ assert( nExtra>=nColl ); |
+ memcpy(zExtra, zColl, nColl); |
+ zColl = zExtra; |
+ zExtra += nColl; |
+ nExtra -= nColl; |
+ }else if( j>=0 ){ |
+ zColl = pTab->aCol[j].zColl; |
+ } |
+ if( !zColl ) zColl = sqlite3StrBINARY; |
+ if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){ |
+ goto exit_create_index; |
+ } |
+ pIndex->azColl[i] = zColl; |
+ requestedSortOrder = pListItem->sortOrder & sortOrderMask; |
+ pIndex->aSortOrder[i] = (u8)requestedSortOrder; |
+ } |
+ |
+ /* Append the table key to the end of the index. For WITHOUT ROWID |
+ ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For |
+ ** normal tables (when pPk==0) this will be the rowid. |
+ */ |
+ if( pPk ){ |
+ for(j=0; j<pPk->nKeyCol; j++){ |
+ int x = pPk->aiColumn[j]; |
+ assert( x>=0 ); |
+ if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){ |
+ pIndex->nColumn--; |
+ }else{ |
+ pIndex->aiColumn[i] = x; |
+ pIndex->azColl[i] = pPk->azColl[j]; |
+ pIndex->aSortOrder[i] = pPk->aSortOrder[j]; |
+ i++; |
+ } |
+ } |
+ assert( i==pIndex->nColumn ); |
+ }else{ |
+ pIndex->aiColumn[i] = XN_ROWID; |
+ pIndex->azColl[i] = sqlite3StrBINARY; |
+ } |
+ sqlite3DefaultRowEst(pIndex); |
+ if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex); |
+ |
+ /* If this index contains every column of its table, then mark |
+ ** it as a covering index */ |
+ assert( HasRowid(pTab) |
+ || pTab->iPKey<0 || sqlite3ColumnOfIndex(pIndex, pTab->iPKey)>=0 ); |
+ if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){ |
+ pIndex->isCovering = 1; |
+ for(j=0; j<pTab->nCol; j++){ |
+ if( j==pTab->iPKey ) continue; |
+ if( sqlite3ColumnOfIndex(pIndex,j)>=0 ) continue; |
+ pIndex->isCovering = 0; |
+ break; |
+ } |
+ } |
+ |
+ if( pTab==pParse->pNewTable ){ |
+ /* This routine has been called to create an automatic index as a |
+ ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or |
+ ** a PRIMARY KEY or UNIQUE clause following the column definitions. |
+ ** i.e. one of: |
+ ** |
+ ** CREATE TABLE t(x PRIMARY KEY, y); |
+ ** CREATE TABLE t(x, y, UNIQUE(x, y)); |
+ ** |
+ ** Either way, check to see if the table already has such an index. If |
+ ** so, don't bother creating this one. This only applies to |
+ ** automatically created indices. Users can do as they wish with |
+ ** explicit indices. |
+ ** |
+ ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent |
+ ** (and thus suppressing the second one) even if they have different |
+ ** sort orders. |
+ ** |
+ ** If there are different collating sequences or if the columns of |
+ ** the constraint occur in different orders, then the constraints are |
+ ** considered distinct and both result in separate indices. |
+ */ |
+ Index *pIdx; |
+ for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
+ int k; |
+ assert( IsUniqueIndex(pIdx) ); |
+ assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF ); |
+ assert( IsUniqueIndex(pIndex) ); |
+ |
+ if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue; |
+ for(k=0; k<pIdx->nKeyCol; k++){ |
+ const char *z1; |
+ const char *z2; |
+ assert( pIdx->aiColumn[k]>=0 ); |
+ if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; |
+ z1 = pIdx->azColl[k]; |
+ z2 = pIndex->azColl[k]; |
+ if( sqlite3StrICmp(z1, z2) ) break; |
+ } |
+ if( k==pIdx->nKeyCol ){ |
+ if( pIdx->onError!=pIndex->onError ){ |
+ /* This constraint creates the same index as a previous |
+ ** constraint specified somewhere in the CREATE TABLE statement. |
+ ** However the ON CONFLICT clauses are different. If both this |
+ ** constraint and the previous equivalent constraint have explicit |
+ ** ON CONFLICT clauses this is an error. Otherwise, use the |
+ ** explicitly specified behavior for the index. |
+ */ |
+ if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ |
+ sqlite3ErrorMsg(pParse, |
+ "conflicting ON CONFLICT clauses specified", 0); |
+ } |
+ if( pIdx->onError==OE_Default ){ |
+ pIdx->onError = pIndex->onError; |
+ } |
+ } |
+ if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType; |
+ goto exit_create_index; |
+ } |
+ } |
+ } |
+ |
+ /* Link the new Index structure to its table and to the other |
+ ** in-memory database structures. |
+ */ |
+ assert( pParse->nErr==0 ); |
+ if( db->init.busy ){ |
+ Index *p; |
+ assert( !IN_DECLARE_VTAB ); |
+ assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
+ p = sqlite3HashInsert(&pIndex->pSchema->idxHash, |
+ pIndex->zName, pIndex); |
+ if( p ){ |
+ assert( p==pIndex ); /* Malloc must have failed */ |
+ sqlite3OomFault(db); |
+ goto exit_create_index; |
+ } |
+ db->flags |= SQLITE_InternChanges; |
+ if( pTblName!=0 ){ |
+ pIndex->tnum = db->init.newTnum; |
+ } |
+ } |
+ |
+ /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the |
+ ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then |
+ ** emit code to allocate the index rootpage on disk and make an entry for |
+ ** the index in the sqlite_master table and populate the index with |
+ ** content. But, do not do this if we are simply reading the sqlite_master |
+ ** table to parse the schema, or if this index is the PRIMARY KEY index |
+ ** of a WITHOUT ROWID table. |
+ ** |
+ ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY |
+ ** or UNIQUE index in a CREATE TABLE statement. Since the table |
+ ** has just been created, it contains no data and the index initialization |
+ ** step can be skipped. |
+ */ |
+ else if( HasRowid(pTab) || pTblName!=0 ){ |
+ Vdbe *v; |
+ char *zStmt; |
+ int iMem = ++pParse->nMem; |
+ |
+ v = sqlite3GetVdbe(pParse); |
+ if( v==0 ) goto exit_create_index; |
+ |
+ sqlite3BeginWriteOperation(pParse, 1, iDb); |
+ |
+ /* Create the rootpage for the index using CreateIndex. But before |
+ ** doing so, code a Noop instruction and store its address in |
+ ** Index.tnum. This is required in case this index is actually a |
+ ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In |
+ ** that case the convertToWithoutRowidTable() routine will replace |
+ ** the Noop with a Goto to jump over the VDBE code generated below. */ |
+ pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop); |
+ sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem); |
+ |
+ /* Gather the complete text of the CREATE INDEX statement into |
+ ** the zStmt variable |
+ */ |
+ if( pStart ){ |
+ int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n; |
+ if( pName->z[n-1]==';' ) n--; |
+ /* A named index with an explicit CREATE INDEX statement */ |
+ zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s", |
+ onError==OE_None ? "" : " UNIQUE", n, pName->z); |
+ }else{ |
+ /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ |
+ /* zStmt = sqlite3MPrintf(""); */ |
+ zStmt = 0; |
+ } |
+ |
+ /* Add an entry in sqlite_master for this index |
+ */ |
+ sqlite3NestedParse(pParse, |
+ "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);", |
+ db->aDb[iDb].zDbSName, MASTER_NAME, |
+ pIndex->zName, |
+ pTab->zName, |
+ iMem, |
+ zStmt |
+ ); |
+ sqlite3DbFree(db, zStmt); |
+ |
+ /* Fill the index with data and reparse the schema. Code an OP_Expire |
+ ** to invalidate all pre-compiled statements. |
+ */ |
+ if( pTblName ){ |
+ sqlite3RefillIndex(pParse, pIndex, iMem); |
+ sqlite3ChangeCookie(pParse, iDb); |
+ sqlite3VdbeAddParseSchemaOp(v, iDb, |
+ sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName)); |
+ sqlite3VdbeAddOp0(v, OP_Expire); |
+ } |
+ |
+ sqlite3VdbeJumpHere(v, pIndex->tnum); |
+ } |
+ |
+ /* When adding an index to the list of indices for a table, make |
+ ** sure all indices labeled OE_Replace come after all those labeled |
+ ** OE_Ignore. This is necessary for the correct constraint check |
+ ** processing (in sqlite3GenerateConstraintChecks()) as part of |
+ ** UPDATE and INSERT statements. |
+ */ |
+ if( db->init.busy || pTblName==0 ){ |
+ if( onError!=OE_Replace || pTab->pIndex==0 |
+ || pTab->pIndex->onError==OE_Replace){ |
+ pIndex->pNext = pTab->pIndex; |
+ pTab->pIndex = pIndex; |
+ }else{ |
+ Index *pOther = pTab->pIndex; |
+ while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ |
+ pOther = pOther->pNext; |
+ } |
+ pIndex->pNext = pOther->pNext; |
+ pOther->pNext = pIndex; |
+ } |
+ pIndex = 0; |
+ } |
+ |
+ /* Clean up before exiting */ |
+exit_create_index: |
+ if( pIndex ) freeIndex(db, pIndex); |
+ sqlite3ExprDelete(db, pPIWhere); |
+ sqlite3ExprListDelete(db, pList); |
+ sqlite3SrcListDelete(db, pTblName); |
+ sqlite3DbFree(db, zName); |
+} |
+ |
+/* |
+** Fill the Index.aiRowEst[] array with default information - information |
+** to be used when we have not run the ANALYZE command. |
+** |
+** aiRowEst[0] is supposed to contain the number of elements in the index. |
+** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the |
+** number of rows in the table that match any particular value of the |
+** first column of the index. aiRowEst[2] is an estimate of the number |
+** of rows that match any particular combination of the first 2 columns |
+** of the index. And so forth. It must always be the case that |
+* |
+** aiRowEst[N]<=aiRowEst[N-1] |
+** aiRowEst[N]>=1 |
+** |
+** Apart from that, we have little to go on besides intuition as to |
+** how aiRowEst[] should be initialized. The numbers generated here |
+** are based on typical values found in actual indices. |
+*/ |
+SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){ |
+ /* 10, 9, 8, 7, 6 */ |
+ LogEst aVal[] = { 33, 32, 30, 28, 26 }; |
+ LogEst *a = pIdx->aiRowLogEst; |
+ int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol); |
+ int i; |
+ |
+ /* Set the first entry (number of rows in the index) to the estimated |
+ ** number of rows in the table, or half the number of rows in the table |
+ ** for a partial index. But do not let the estimate drop below 10. */ |
+ a[0] = pIdx->pTable->nRowLogEst; |
+ if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10; assert( 10==sqlite3LogEst(2) ); |
+ if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) ); |
+ |
+ /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is |
+ ** 6 and each subsequent value (if any) is 5. */ |
+ memcpy(&a[1], aVal, nCopy*sizeof(LogEst)); |
+ for(i=nCopy+1; i<=pIdx->nKeyCol; i++){ |
+ a[i] = 23; assert( 23==sqlite3LogEst(5) ); |
+ } |
+ |
+ assert( 0==sqlite3LogEst(1) ); |
+ if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0; |
+} |
+ |
+/* |
+** This routine will drop an existing named index. This routine |
+** implements the DROP INDEX statement. |
+*/ |
+SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ |
+ Index *pIndex; |
+ Vdbe *v; |
+ sqlite3 *db = pParse->db; |
+ int iDb; |
+ |
+ assert( pParse->nErr==0 ); /* Never called with prior errors */ |
+ if( db->mallocFailed ){ |
+ goto exit_drop_index; |
+ } |
+ assert( pName->nSrc==1 ); |
+ if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
+ goto exit_drop_index; |
+ } |
+ pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); |
+ if( pIndex==0 ){ |
+ if( !ifExists ){ |
+ sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0); |
+ }else{ |
+ sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); |
+ } |
+ pParse->checkSchema = 1; |
+ goto exit_drop_index; |
+ } |
+ if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){ |
+ sqlite3ErrorMsg(pParse, "index associated with UNIQUE " |
+ "or PRIMARY KEY constraint cannot be dropped", 0); |
+ goto exit_drop_index; |
+ } |
+ iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
+#ifndef SQLITE_OMIT_AUTHORIZATION |
+ { |
+ int code = SQLITE_DROP_INDEX; |
+ Table *pTab = pIndex->pTable; |
+ const char *zDb = db->aDb[iDb].zDbSName; |
+ const char *zTab = SCHEMA_TABLE(iDb); |
+ if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ |
+ goto exit_drop_index; |
+ } |
+ if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX; |
+ if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ |
+ goto exit_drop_index; |
+ } |
+ } |
+#endif |
+ |
+ /* Generate code to remove the index and from the master table */ |
+ v = sqlite3GetVdbe(pParse); |
+ if( v ){ |
+ sqlite3BeginWriteOperation(pParse, 1, iDb); |
+ sqlite3NestedParse(pParse, |
+ "DELETE FROM %Q.%s WHERE name=%Q AND type='index'", |
+ db->aDb[iDb].zDbSName, MASTER_NAME, pIndex->zName |
+ ); |
+ sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName); |
+ sqlite3ChangeCookie(pParse, iDb); |
+ destroyRootPage(pParse, pIndex->tnum, iDb); |
+ sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0); |
+ } |
+ |
+exit_drop_index: |
+ sqlite3SrcListDelete(db, pName); |
+} |
+ |
+/* |
+** pArray is a pointer to an array of objects. Each object in the |
+** array is szEntry bytes in size. This routine uses sqlite3DbRealloc() |
+** to extend the array so that there is space for a new object at the end. |
+** |
+** When this function is called, *pnEntry contains the current size of |
+** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes |
+** in total). |
+** |
+** If the realloc() is successful (i.e. if no OOM condition occurs), the |
+** space allocated for the new object is zeroed, *pnEntry updated to |
+** reflect the new size of the array and a pointer to the new allocation |
+** returned. *pIdx is set to the index of the new array entry in this case. |
+** |
+** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains |
+** unchanged and a copy of pArray returned. |
+*/ |
+SQLITE_PRIVATE void *sqlite3ArrayAllocate( |
+ sqlite3 *db, /* Connection to notify of malloc failures */ |
+ void *pArray, /* Array of objects. Might be reallocated */ |
+ int szEntry, /* Size of each object in the array */ |
+ int *pnEntry, /* Number of objects currently in use */ |
+ int *pIdx /* Write the index of a new slot here */ |
+){ |
+ char *z; |
+ int n = *pnEntry; |
+ if( (n & (n-1))==0 ){ |
+ int sz = (n==0) ? 1 : 2*n; |
+ void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry); |
+ if( pNew==0 ){ |
+ *pIdx = -1; |
+ return pArray; |
+ } |
+ pArray = pNew; |
+ } |
+ z = (char*)pArray; |
+ memset(&z[n * szEntry], 0, szEntry); |
+ *pIdx = n; |
+ ++*pnEntry; |
+ return pArray; |
+} |
+ |
+/* |
+** Append a new element to the given IdList. Create a new IdList if |
+** need be. |
+** |
+** A new IdList is returned, or NULL if malloc() fails. |
+*/ |
+SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){ |
+ int i; |
+ if( pList==0 ){ |
+ pList = sqlite3DbMallocZero(db, sizeof(IdList) ); |
+ if( pList==0 ) return 0; |
+ } |
+ pList->a = sqlite3ArrayAllocate( |
+ db, |
+ pList->a, |
+ sizeof(pList->a[0]), |
+ &pList->nId, |
+ &i |
+ ); |
+ if( i<0 ){ |
+ sqlite3IdListDelete(db, pList); |
+ return 0; |
+ } |
+ pList->a[i].zName = sqlite3NameFromToken(db, pToken); |
+ return pList; |
+} |
+ |
+/* |
+** Delete an IdList. |
+*/ |
+SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){ |
+ int i; |
+ if( pList==0 ) return; |
+ for(i=0; i<pList->nId; i++){ |
+ sqlite3DbFree(db, pList->a[i].zName); |
+ } |
+ sqlite3DbFree(db, pList->a); |
+ sqlite3DbFree(db, pList); |
+} |
+ |
+/* |
+** Return the index in pList of the identifier named zId. Return -1 |
+** if not found. |
+*/ |
+SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){ |
+ int i; |
+ if( pList==0 ) return -1; |
+ for(i=0; i<pList->nId; i++){ |
+ if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; |
+ } |
+ return -1; |
+} |
+ |
+/* |
+** Expand the space allocated for the given SrcList object by |
+** creating nExtra new slots beginning at iStart. iStart is zero based. |
+** New slots are zeroed. |
+** |
+** For example, suppose a SrcList initially contains two entries: A,B. |
+** To append 3 new entries onto the end, do this: |
+** |
+** sqlite3SrcListEnlarge(db, pSrclist, 3, 2); |
+** |
+** After the call above it would contain: A, B, nil, nil, nil. |
+** If the iStart argument had been 1 instead of 2, then the result |
+** would have been: A, nil, nil, nil, B. To prepend the new slots, |
+** the iStart value would be 0. The result then would |
+** be: nil, nil, nil, A, B. |
+** |
+** If a memory allocation fails the SrcList is unchanged. The |
+** db->mallocFailed flag will be set to true. |
+*/ |
+SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge( |
+ sqlite3 *db, /* Database connection to notify of OOM errors */ |
+ SrcList *pSrc, /* The SrcList to be enlarged */ |
+ int nExtra, /* Number of new slots to add to pSrc->a[] */ |
+ int iStart /* Index in pSrc->a[] of first new slot */ |
+){ |
+ int i; |
+ |
+ /* Sanity checking on calling parameters */ |
+ assert( iStart>=0 ); |
+ assert( nExtra>=1 ); |
+ assert( pSrc!=0 ); |
+ assert( iStart<=pSrc->nSrc ); |
+ |
+ /* Allocate additional space if needed */ |
+ if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){ |
+ SrcList *pNew; |
+ int nAlloc = pSrc->nSrc*2+nExtra; |
+ int nGot; |
+ pNew = sqlite3DbRealloc(db, pSrc, |
+ sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) ); |
+ if( pNew==0 ){ |
+ assert( db->mallocFailed ); |
+ return pSrc; |
+ } |
+ pSrc = pNew; |
+ nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1; |
+ pSrc->nAlloc = nGot; |
+ } |
+ |
+ /* Move existing slots that come after the newly inserted slots |
+ ** out of the way */ |
+ for(i=pSrc->nSrc-1; i>=iStart; i--){ |
+ pSrc->a[i+nExtra] = pSrc->a[i]; |
+ } |
+ pSrc->nSrc += nExtra; |
+ |
+ /* Zero the newly allocated slots */ |
+ memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra); |
+ for(i=iStart; i<iStart+nExtra; i++){ |
+ pSrc->a[i].iCursor = -1; |
+ } |
+ |
+ /* Return a pointer to the enlarged SrcList */ |
+ return pSrc; |
+} |
+ |
+ |
+/* |
+** Append a new table name to the given SrcList. Create a new SrcList if |
+** need be. A new entry is created in the SrcList even if pTable is NULL. |
+** |
+** A SrcList is returned, or NULL if there is an OOM error. The returned |
+** SrcList might be the same as the SrcList that was input or it might be |
+** a new one. If an OOM error does occurs, then the prior value of pList |
+** that is input to this routine is automatically freed. |
+** |
+** If pDatabase is not null, it means that the table has an optional |
+** database name prefix. Like this: "database.table". The pDatabase |
+** points to the table name and the pTable points to the database name. |
+** The SrcList.a[].zName field is filled with the table name which might |
+** come from pTable (if pDatabase is NULL) or from pDatabase. |
+** SrcList.a[].zDatabase is filled with the database name from pTable, |
+** or with NULL if no database is specified. |
+** |
+** In other words, if call like this: |
+** |
+** sqlite3SrcListAppend(D,A,B,0); |
+** |
+** Then B is a table name and the database name is unspecified. If called |
+** like this: |
+** |
+** sqlite3SrcListAppend(D,A,B,C); |
+** |
+** Then C is the table name and B is the database name. If C is defined |
+** then so is B. In other words, we never have a case where: |
+** |
+** sqlite3SrcListAppend(D,A,0,C); |
+** |
+** Both pTable and pDatabase are assumed to be quoted. They are dequoted |
+** before being added to the SrcList. |
+*/ |
+SQLITE_PRIVATE SrcList *sqlite3SrcListAppend( |
+ sqlite3 *db, /* Connection to notify of malloc failures */ |
+ SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */ |
+ Token *pTable, /* Table to append */ |
+ Token *pDatabase /* Database of the table */ |
+){ |
+ struct SrcList_item *pItem; |
+ assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */ |
+ assert( db!=0 ); |
+ if( pList==0 ){ |
+ pList = sqlite3DbMallocRawNN(db, sizeof(SrcList) ); |
+ if( pList==0 ) return 0; |
+ pList->nAlloc = 1; |
+ pList->nSrc = 1; |
+ memset(&pList->a[0], 0, sizeof(pList->a[0])); |
+ pList->a[0].iCursor = -1; |
+ }else{ |
+ pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc); |
+ } |
+ if( db->mallocFailed ){ |
+ sqlite3SrcListDelete(db, pList); |
+ return 0; |
+ } |
+ pItem = &pList->a[pList->nSrc-1]; |
+ if( pDatabase && pDatabase->z==0 ){ |
+ pDatabase = 0; |
+ } |
+ if( pDatabase ){ |
+ Token *pTemp = pDatabase; |
+ pDatabase = pTable; |
+ pTable = pTemp; |
+ } |
+ pItem->zName = sqlite3NameFromToken(db, pTable); |
+ pItem->zDatabase = sqlite3NameFromToken(db, pDatabase); |
+ return pList; |
+} |
+ |
+/* |
+** Assign VdbeCursor index numbers to all tables in a SrcList |
+*/ |
+SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ |
+ int i; |
+ struct SrcList_item *pItem; |
+ assert(pList || pParse->db->mallocFailed ); |
+ if( pList ){ |
+ for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ |
+ if( pItem->iCursor>=0 ) break; |
+ pItem->iCursor = pParse->nTab++; |
+ if( pItem->pSelect ){ |
+ sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc); |
+ } |
+ } |
+ } |
+} |
+ |
+/* |
+** Delete an entire SrcList including all its substructure. |
+*/ |
+SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ |
+ int i; |
+ struct SrcList_item *pItem; |
+ if( pList==0 ) return; |
+ for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ |
+ sqlite3DbFree(db, pItem->zDatabase); |
+ sqlite3DbFree(db, pItem->zName); |
+ sqlite3DbFree(db, pItem->zAlias); |
+ if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy); |
+ if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg); |
+ sqlite3DeleteTable(db, pItem->pTab); |
+ sqlite3SelectDelete(db, pItem->pSelect); |
+ sqlite3ExprDelete(db, pItem->pOn); |
+ sqlite3IdListDelete(db, pItem->pUsing); |
+ } |
+ sqlite3DbFree(db, pList); |
+} |
+ |
+/* |
+** This routine is called by the parser to add a new term to the |
+** end of a growing FROM clause. The "p" parameter is the part of |
+** the FROM clause that has already been constructed. "p" is NULL |
+** if this is the first term of the FROM clause. pTable and pDatabase |
+** are the name of the table and database named in the FROM clause term. |
+** pDatabase is NULL if the database name qualifier is missing - the |
+** usual case. If the term has an alias, then pAlias points to the |
+** alias token. If the term is a subquery, then pSubquery is the |
+** SELECT statement that the subquery encodes. The pTable and |
+** pDatabase parameters are NULL for subqueries. The pOn and pUsing |
+** parameters are the content of the ON and USING clauses. |
+** |
+** Return a new SrcList which encodes is the FROM with the new |
+** term added. |
+*/ |
+SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm( |
+ Parse *pParse, /* Parsing context */ |
+ SrcList *p, /* The left part of the FROM clause already seen */ |
+ Token *pTable, /* Name of the table to add to the FROM clause */ |
+ Token *pDatabase, /* Name of the database containing pTable */ |
+ Token *pAlias, /* The right-hand side of the AS subexpression */ |
+ Select *pSubquery, /* A subquery used in place of a table name */ |
+ Expr *pOn, /* The ON clause of a join */ |
+ IdList *pUsing /* The USING clause of a join */ |
+){ |
+ struct SrcList_item *pItem; |
+ sqlite3 *db = pParse->db; |
+ if( !p && (pOn || pUsing) ){ |
+ sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", |
+ (pOn ? "ON" : "USING") |
+ ); |
+ goto append_from_error; |
+ } |
+ p = sqlite3SrcListAppend(db, p, pTable, pDatabase); |
+ if( p==0 || NEVER(p->nSrc==0) ){ |
+ goto append_from_error; |
+ } |
+ pItem = &p->a[p->nSrc-1]; |
+ assert( pAlias!=0 ); |
+ if( pAlias->n ){ |
+ pItem->zAlias = sqlite3NameFromToken(db, pAlias); |
+ } |
+ pItem->pSelect = pSubquery; |
+ pItem->pOn = pOn; |
+ pItem->pUsing = pUsing; |
+ return p; |
+ |
+ append_from_error: |
+ assert( p==0 ); |
+ sqlite3ExprDelete(db, pOn); |
+ sqlite3IdListDelete(db, pUsing); |
+ sqlite3SelectDelete(db, pSubquery); |
+ return 0; |
+} |
+ |
+/* |
+** Add an INDEXED BY or NOT INDEXED clause to the most recently added |
+** element of the source-list passed as the second argument. |
+*/ |
+SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ |
+ assert( pIndexedBy!=0 ); |
+ if( p && ALWAYS(p->nSrc>0) ){ |
+ struct SrcList_item *pItem = &p->a[p->nSrc-1]; |
+ assert( pItem->fg.notIndexed==0 ); |
+ assert( pItem->fg.isIndexedBy==0 ); |
+ assert( pItem->fg.isTabFunc==0 ); |
+ if( pIndexedBy->n==1 && !pIndexedBy->z ){ |
+ /* A "NOT INDEXED" clause was supplied. See parse.y |
+ ** construct "indexed_opt" for details. */ |
+ pItem->fg.notIndexed = 1; |
+ }else{ |
+ pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy); |
+ pItem->fg.isIndexedBy = (pItem->u1.zIndexedBy!=0); |
+ } |
+ } |
+} |
+ |
+/* |
+** Add the list of function arguments to the SrcList entry for a |
+** table-valued-function. |
+*/ |
+SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){ |
+ if( p ){ |
+ struct SrcList_item *pItem = &p->a[p->nSrc-1]; |
+ assert( pItem->fg.notIndexed==0 ); |
+ assert( pItem->fg.isIndexedBy==0 ); |
+ assert( pItem->fg.isTabFunc==0 ); |
+ pItem->u1.pFuncArg = pList; |
+ pItem->fg.isTabFunc = 1; |
+ }else{ |
+ sqlite3ExprListDelete(pParse->db, pList); |
+ } |
+} |
+ |
+/* |
+** When building up a FROM clause in the parser, the join operator |
+** is initially attached to the left operand. But the code generator |
+** expects the join operator to be on the right operand. This routine |
+** Shifts all join operators from left to right for an entire FROM |
+** clause. |
+** |
+** Example: Suppose the join is like this: |
+** |
+** A natural cross join B |
+** |
+** The operator is "natural cross join". The A and B operands are stored |
+** in p->a[0] and p->a[1], respectively. The parser initially stores the |
+** operator with A. This routine shifts that operator over to B. |
+*/ |
+SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){ |
+ if( p ){ |
+ int i; |
+ for(i=p->nSrc-1; i>0; i--){ |
+ p->a[i].fg.jointype = p->a[i-1].fg.jointype; |
+ } |
+ p->a[0].fg.jointype = 0; |
+ } |
+} |
+ |
+/* |
+** Generate VDBE code for a BEGIN statement. |
+*/ |
+SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){ |
+ sqlite3 *db; |
+ Vdbe *v; |
+ int i; |
+ |
+ assert( pParse!=0 ); |
+ db = pParse->db; |
+ assert( db!=0 ); |
+ if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){ |
+ return; |
+ } |
+ v = sqlite3GetVdbe(pParse); |
+ if( !v ) return; |
+ if( type!=TK_DEFERRED ){ |
+ for(i=0; i<db->nDb; i++){ |
+ sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1); |
+ sqlite3VdbeUsesBtree(v, i); |
+ } |
+ } |
+ sqlite3VdbeAddOp0(v, OP_AutoCommit); |
+} |
+ |
+/* |
+** Generate VDBE code for a COMMIT statement. |
+*/ |
+SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){ |
+ Vdbe *v; |
+ |
+ assert( pParse!=0 ); |
+ assert( pParse->db!=0 ); |
+ if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){ |
+ return; |
+ } |
+ v = sqlite3GetVdbe(pParse); |
+ if( v ){ |
+ sqlite3VdbeAddOp1(v, OP_AutoCommit, 1); |
+ } |
+} |
+ |
+/* |
+** Generate VDBE code for a ROLLBACK statement. |
+*/ |
+SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){ |
+ Vdbe *v; |
+ |
+ assert( pParse!=0 ); |
+ assert( pParse->db!=0 ); |
+ if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){ |
+ return; |
+ } |
+ v = sqlite3GetVdbe(pParse); |
+ if( v ){ |
+ sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1); |
+ } |
+} |
+ |
+/* |
+** This function is called by the parser when it parses a command to create, |
+** release or rollback an SQL savepoint. |
+*/ |
+SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){ |
+ char *zName = sqlite3NameFromToken(pParse->db, pName); |
+ if( zName ){ |
+ Vdbe *v = sqlite3GetVdbe(pParse); |
+#ifndef SQLITE_OMIT_AUTHORIZATION |
+ static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" }; |
+ assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 ); |
+#endif |
+ if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){ |
+ sqlite3DbFree(pParse->db, zName); |
+ return; |
+ } |
+ sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC); |
+ } |
+} |
+ |
+/* |
+** Make sure the TEMP database is open and available for use. Return |
+** the number of errors. Leave any error messages in the pParse structure. |
+*/ |
+SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){ |
+ sqlite3 *db = pParse->db; |
+ if( db->aDb[1].pBt==0 && !pParse->explain ){ |
+ int rc; |
+ Btree *pBt; |
+ static const int flags = |
+ SQLITE_OPEN_READWRITE | |
+ SQLITE_OPEN_CREATE | |
+ SQLITE_OPEN_EXCLUSIVE | |
+ SQLITE_OPEN_DELETEONCLOSE | |
+ SQLITE_OPEN_TEMP_DB; |
+ |
+ rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags); |
+ if( rc!=SQLITE_OK ){ |
+ sqlite3ErrorMsg(pParse, "unable to open a temporary database " |
+ "file for storing temporary tables"); |
+ pParse->rc = rc; |
+ return 1; |
+ } |
+ db->aDb[1].pBt = pBt; |
+ assert( db->aDb[1].pSchema ); |
+ if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ |
+ sqlite3OomFault(db); |
+ return 1; |
+ } |
+ } |
+ return 0; |
+} |
+ |
+/* |
+** Record the fact that the schema cookie will need to be verified |
+** for database iDb. The code to actually verify the schema cookie |
+** will occur at the end of the top-level VDBE and will be generated |
+** later, by sqlite3FinishCoding(). |
+*/ |
+SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ |
+ Parse *pToplevel = sqlite3ParseToplevel(pParse); |
+ |
+ assert( iDb>=0 && iDb<pParse->db->nDb ); |
+ assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 ); |
+ assert( iDb<SQLITE_MAX_ATTACHED+2 ); |
+ assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) ); |
+ if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){ |
+ DbMaskSet(pToplevel->cookieMask, iDb); |
+ if( !OMIT_TEMPDB && iDb==1 ){ |
+ sqlite3OpenTempDatabase(pToplevel); |
+ } |
+ } |
+} |
+ |
+/* |
+** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each |
+** attached database. Otherwise, invoke it for the database named zDb only. |
+*/ |
+SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){ |
+ sqlite3 *db = pParse->db; |
+ int i; |
+ for(i=0; i<db->nDb; i++){ |
+ Db *pDb = &db->aDb[i]; |
+ if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){ |
+ sqlite3CodeVerifySchema(pParse, i); |
+ } |
+ } |
+} |
+ |
+/* |
+** Generate VDBE code that prepares for doing an operation that |
+** might change the database. |
+** |
+** This routine starts a new transaction if we are not already within |
+** a transaction. If we are already within a transaction, then a checkpoint |
+** is set if the setStatement parameter is true. A checkpoint should |
+** be set for operations that might fail (due to a constraint) part of |
+** the way through and which will need to undo some writes without having to |
+** rollback the whole transaction. For operations where all constraints |
+** can be checked before any changes are made to the database, it is never |
+** necessary to undo a write and the checkpoint should not be set. |
+*/ |
+SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ |
+ Parse *pToplevel = sqlite3ParseToplevel(pParse); |
+ sqlite3CodeVerifySchema(pParse, iDb); |
+ DbMaskSet(pToplevel->writeMask, iDb); |
+ pToplevel->isMultiWrite |= setStatement; |
+} |
+ |
+/* |
+** Indicate that the statement currently under construction might write |
+** more than one entry (example: deleting one row then inserting another, |
+** inserting multiple rows in a table, or inserting a row and index entries.) |
+** If an abort occurs after some of these writes have completed, then it will |
+** be necessary to undo the completed writes. |
+*/ |
+SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){ |
+ Parse *pToplevel = sqlite3ParseToplevel(pParse); |
+ pToplevel->isMultiWrite = 1; |
+} |
+ |
+/* |
+** The code generator calls this routine if is discovers that it is |
+** possible to abort a statement prior to completion. In order to |
+** perform this abort without corrupting the database, we need to make |
+** sure that the statement is protected by a statement transaction. |
+** |
+** Technically, we only need to set the mayAbort flag if the |
+** isMultiWrite flag was previously set. There is a time dependency |
+** such that the abort must occur after the multiwrite. This makes |
+** some statements involving the REPLACE conflict resolution algorithm |
+** go a little faster. But taking advantage of this time dependency |
+** makes it more difficult to prove that the code is correct (in |
+** particular, it prevents us from writing an effective |
+** implementation of sqlite3AssertMayAbort()) and so we have chosen |
+** to take the safe route and skip the optimization. |
+*/ |
+SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){ |
+ Parse *pToplevel = sqlite3ParseToplevel(pParse); |
+ pToplevel->mayAbort = 1; |
+} |
+ |
+/* |
+** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT |
+** error. The onError parameter determines which (if any) of the statement |
+** and/or current transaction is rolled back. |
+*/ |
+SQLITE_PRIVATE void sqlite3HaltConstraint( |
+ Parse *pParse, /* Parsing context */ |
+ int errCode, /* extended error code */ |
+ int onError, /* Constraint type */ |
+ char *p4, /* Error message */ |
+ i8 p4type, /* P4_STATIC or P4_TRANSIENT */ |
+ u8 p5Errmsg /* P5_ErrMsg type */ |
+){ |
+ Vdbe *v = sqlite3GetVdbe(pParse); |
+ assert( (errCode&0xff)==SQLITE_CONSTRAINT ); |
+ if( onError==OE_Abort ){ |
+ sqlite3MayAbort(pParse); |
+ } |
+ sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type); |
+ sqlite3VdbeChangeP5(v, p5Errmsg); |
+} |
+ |
+/* |
+** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation. |
+*/ |
+SQLITE_PRIVATE void sqlite3UniqueConstraint( |
+ Parse *pParse, /* Parsing context */ |
+ int onError, /* Constraint type */ |
+ Index *pIdx /* The index that triggers the constraint */ |
+){ |
+ char *zErr; |
+ int j; |
+ StrAccum errMsg; |
+ Table *pTab = pIdx->pTable; |
+ |
+ sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200); |
+ if( pIdx->aColExpr ){ |
+ sqlite3XPrintf(&errMsg, "index '%q'", pIdx->zName); |
+ }else{ |
+ for(j=0; j<pIdx->nKeyCol; j++){ |
+ char *zCol; |
+ assert( pIdx->aiColumn[j]>=0 ); |
+ zCol = pTab->aCol[pIdx->aiColumn[j]].zName; |
+ if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2); |
+ sqlite3XPrintf(&errMsg, "%s.%s", pTab->zName, zCol); |
+ } |
+ } |
+ zErr = sqlite3StrAccumFinish(&errMsg); |
+ sqlite3HaltConstraint(pParse, |
+ IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY |
+ : SQLITE_CONSTRAINT_UNIQUE, |
+ onError, zErr, P4_DYNAMIC, P5_ConstraintUnique); |
+} |
+ |
+ |
+/* |
+** Code an OP_Halt due to non-unique rowid. |
+*/ |
+SQLITE_PRIVATE void sqlite3RowidConstraint( |
+ Parse *pParse, /* Parsing context */ |
+ int onError, /* Conflict resolution algorithm */ |
+ Table *pTab /* The table with the non-unique rowid */ |
+){ |
+ char *zMsg; |
+ int rc; |
+ if( pTab->iPKey>=0 ){ |
+ zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName, |
+ pTab->aCol[pTab->iPKey].zName); |
+ rc = SQLITE_CONSTRAINT_PRIMARYKEY; |
+ }else{ |
+ zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName); |
+ rc = SQLITE_CONSTRAINT_ROWID; |
+ } |
+ sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC, |
+ P5_ConstraintUnique); |
+} |
+ |
+/* |
+** Check to see if pIndex uses the collating sequence pColl. Return |
+** true if it does and false if it does not. |
+*/ |
+#ifndef SQLITE_OMIT_REINDEX |
+static int collationMatch(const char *zColl, Index *pIndex){ |
+ int i; |
+ assert( zColl!=0 ); |
+ for(i=0; i<pIndex->nColumn; i++){ |
+ const char *z = pIndex->azColl[i]; |
+ assert( z!=0 || pIndex->aiColumn[i]<0 ); |
+ if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){ |
+ return 1; |
+ } |
+ } |
+ return 0; |
+} |
+#endif |
+ |
+/* |
+** Recompute all indices of pTab that use the collating sequence pColl. |
+** If pColl==0 then recompute all indices of pTab. |
+*/ |
+#ifndef SQLITE_OMIT_REINDEX |
+static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){ |
+ Index *pIndex; /* An index associated with pTab */ |
+ |
+ for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ |
+ if( zColl==0 || collationMatch(zColl, pIndex) ){ |
+ int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
+ sqlite3BeginWriteOperation(pParse, 0, iDb); |
+ sqlite3RefillIndex(pParse, pIndex, -1); |
+ } |
+ } |
+} |
+#endif |
+ |
+/* |
+** Recompute all indices of all tables in all databases where the |
+** indices use the collating sequence pColl. If pColl==0 then recompute |
+** all indices everywhere. |
+*/ |
+#ifndef SQLITE_OMIT_REINDEX |
+static void reindexDatabases(Parse *pParse, char const *zColl){ |
+ Db *pDb; /* A single database */ |
+ int iDb; /* The database index number */ |
+ sqlite3 *db = pParse->db; /* The database connection */ |
+ HashElem *k; /* For looping over tables in pDb */ |
+ Table *pTab; /* A table in the database */ |
+ |
+ assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */ |
+ for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){ |
+ assert( pDb!=0 ); |
+ for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){ |
+ pTab = (Table*)sqliteHashData(k); |
+ reindexTable(pParse, pTab, zColl); |
+ } |
+ } |
+} |
+#endif |
+ |
+/* |
+** Generate code for the REINDEX command. |
+** |
+** REINDEX -- 1 |
+** REINDEX <collation> -- 2 |
+** REINDEX ?<database>.?<tablename> -- 3 |
+** REINDEX ?<database>.?<indexname> -- 4 |
+** |
+** Form 1 causes all indices in all attached databases to be rebuilt. |
+** Form 2 rebuilds all indices in all databases that use the named |
+** collating function. Forms 3 and 4 rebuild the named index or all |
+** indices associated with the named table. |
+*/ |
+#ifndef SQLITE_OMIT_REINDEX |
+SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ |
+ CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */ |
+ char *z; /* Name of a table or index */ |
+ const char *zDb; /* Name of the database */ |
+ Table *pTab; /* A table in the database */ |
+ Index *pIndex; /* An index associated with pTab */ |
+ int iDb; /* The database index number */ |
+ sqlite3 *db = pParse->db; /* The database connection */ |
+ Token *pObjName; /* Name of the table or index to be reindexed */ |
+ |
+ /* Read the database schema. If an error occurs, leave an error message |
+ ** and code in pParse and return NULL. */ |
+ if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
+ return; |
+ } |
+ |
+ if( pName1==0 ){ |
+ reindexDatabases(pParse, 0); |
+ return; |
+ }else if( NEVER(pName2==0) || pName2->z==0 ){ |
+ char *zColl; |
+ assert( pName1->z ); |
+ zColl = sqlite3NameFromToken(pParse->db, pName1); |
+ if( !zColl ) return; |
+ pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); |
+ if( pColl ){ |
+ reindexDatabases(pParse, zColl); |
+ sqlite3DbFree(db, zColl); |
+ return; |
+ } |
+ sqlite3DbFree(db, zColl); |
+ } |
+ iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); |
+ if( iDb<0 ) return; |
+ z = sqlite3NameFromToken(db, pObjName); |
+ if( z==0 ) return; |
+ zDb = db->aDb[iDb].zDbSName; |
+ pTab = sqlite3FindTable(db, z, zDb); |
+ if( pTab ){ |
+ reindexTable(pParse, pTab, 0); |
+ sqlite3DbFree(db, z); |
+ return; |
+ } |
+ pIndex = sqlite3FindIndex(db, z, zDb); |
+ sqlite3DbFree(db, z); |
+ if( pIndex ){ |
+ sqlite3BeginWriteOperation(pParse, 0, iDb); |
+ sqlite3RefillIndex(pParse, pIndex, -1); |
+ return; |
+ } |
+ sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); |
+} |
+#endif |
+ |
+/* |
+** Return a KeyInfo structure that is appropriate for the given Index. |
+** |
+** The caller should invoke sqlite3KeyInfoUnref() on the returned object |
+** when it has finished using it. |
+*/ |
+SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ |
+ int i; |
+ int nCol = pIdx->nColumn; |
+ int nKey = pIdx->nKeyCol; |
+ KeyInfo *pKey; |
+ if( pParse->nErr ) return 0; |
+ if( pIdx->uniqNotNull ){ |
+ pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey); |
+ }else{ |
+ pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0); |
+ } |
+ if( pKey ){ |
+ assert( sqlite3KeyInfoIsWriteable(pKey) ); |
+ for(i=0; i<nCol; i++){ |
+ const char *zColl = pIdx->azColl[i]; |
+ pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 : |
+ sqlite3LocateCollSeq(pParse, zColl); |
+ pKey->aSortOrder[i] = pIdx->aSortOrder[i]; |
+ } |
+ if( pParse->nErr ){ |
+ sqlite3KeyInfoUnref(pKey); |
+ pKey = 0; |
+ } |
+ } |
+ return pKey; |
+} |
+ |
+#ifndef SQLITE_OMIT_CTE |
+/* |
+** This routine is invoked once per CTE by the parser while parsing a |
+** WITH clause. |
+*/ |
+SQLITE_PRIVATE With *sqlite3WithAdd( |
+ Parse *pParse, /* Parsing context */ |
+ With *pWith, /* Existing WITH clause, or NULL */ |
+ Token *pName, /* Name of the common-table */ |
+ ExprList *pArglist, /* Optional column name list for the table */ |
+ Select *pQuery /* Query used to initialize the table */ |
+){ |
+ sqlite3 *db = pParse->db; |
+ With *pNew; |
+ char *zName; |
+ |
+ /* Check that the CTE name is unique within this WITH clause. If |
+ ** not, store an error in the Parse structure. */ |
+ zName = sqlite3NameFromToken(pParse->db, pName); |
+ if( zName && pWith ){ |
+ int i; |
+ for(i=0; i<pWith->nCte; i++){ |
+ if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){ |
+ sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName); |
+ } |
+ } |
+ } |
+ |
+ if( pWith ){ |
+ int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte); |
+ pNew = sqlite3DbRealloc(db, pWith, nByte); |
+ }else{ |
+ pNew = sqlite3DbMallocZero(db, sizeof(*pWith)); |
+ } |
+ assert( (pNew!=0 && zName!=0) || db->mallocFailed ); |
+ |
+ if( db->mallocFailed ){ |
+ sqlite3ExprListDelete(db, pArglist); |
+ sqlite3SelectDelete(db, pQuery); |
+ sqlite3DbFree(db, zName); |
+ pNew = pWith; |
+ }else{ |
+ pNew->a[pNew->nCte].pSelect = pQuery; |
+ pNew->a[pNew->nCte].pCols = pArglist; |
+ pNew->a[pNew->nCte].zName = zName; |
+ pNew->a[pNew->nCte].zCteErr = 0; |
+ pNew->nCte++; |
+ } |
+ |
+ return pNew; |
+} |
+ |
+/* |
+** Free the contents of the With object passed as the second argument. |
+*/ |
+SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){ |
+ if( pWith ){ |
+ int i; |
+ for(i=0; i<pWith->nCte; i++){ |
+ struct Cte *pCte = &pWith->a[i]; |
+ sqlite3ExprListDelete(db, pCte->pCols); |
+ sqlite3SelectDelete(db, pCte->pSelect); |
+ sqlite3DbFree(db, pCte->zName); |
+ } |
+ sqlite3DbFree(db, pWith); |
+ } |
+} |
+#endif /* !defined(SQLITE_OMIT_CTE) */ |
+ |
+/************** End of build.c ***********************************************/ |
+ |
+/* Chain include. */ |
+#include "sqlite3.05.c" |