| Index: third_party/sqlite/src/src/vdbemem.c
|
| diff --git a/third_party/sqlite/src/src/vdbemem.c b/third_party/sqlite/src/src/vdbemem.c
|
| index 882c686334a1b17fc02988765a3d83018b2dd919..870fb5bd891c2c83ae8c8683afa5ceca93b99649 100644
|
| --- a/third_party/sqlite/src/src/vdbemem.c
|
| +++ b/third_party/sqlite/src/src/vdbemem.c
|
| @@ -18,11 +18,52 @@
|
| #include "sqliteInt.h"
|
| #include "vdbeInt.h"
|
|
|
| +#ifdef SQLITE_DEBUG
|
| /*
|
| -** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
|
| -** P if required.
|
| +** Check invariants on a Mem object.
|
| +**
|
| +** This routine is intended for use inside of assert() statements, like
|
| +** this: assert( sqlite3VdbeCheckMemInvariants(pMem) );
|
| */
|
| -#define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
|
| +int sqlite3VdbeCheckMemInvariants(Mem *p){
|
| + /* If MEM_Dyn is set then Mem.xDel!=0.
|
| + ** Mem.xDel is might not be initialized if MEM_Dyn is clear.
|
| + */
|
| + assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
|
| +
|
| + /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we
|
| + ** ensure that if Mem.szMalloc>0 then it is safe to do
|
| + ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
|
| + ** That saves a few cycles in inner loops. */
|
| + assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
|
| +
|
| + /* Cannot be both MEM_Int and MEM_Real at the same time */
|
| + assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
|
| +
|
| + /* The szMalloc field holds the correct memory allocation size */
|
| + assert( p->szMalloc==0
|
| + || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
|
| +
|
| + /* If p holds a string or blob, the Mem.z must point to exactly
|
| + ** one of the following:
|
| + **
|
| + ** (1) Memory in Mem.zMalloc and managed by the Mem object
|
| + ** (2) Memory to be freed using Mem.xDel
|
| + ** (3) An ephemeral string or blob
|
| + ** (4) A static string or blob
|
| + */
|
| + if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
|
| + assert(
|
| + ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
|
| + ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
|
| + ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
|
| + ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
|
| + );
|
| + }
|
| + return 1;
|
| +}
|
| +#endif
|
| +
|
|
|
| /*
|
| ** If pMem is an object with a valid string representation, this routine
|
| @@ -38,7 +79,9 @@
|
| ** between formats.
|
| */
|
| int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
|
| +#ifndef SQLITE_OMIT_UTF16
|
| int rc;
|
| +#endif
|
| assert( (pMem->flags&MEM_RowSet)==0 );
|
| assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
|
| || desiredEnc==SQLITE_UTF16BE );
|
| @@ -63,59 +106,84 @@ int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
|
|
|
| /*
|
| ** Make sure pMem->z points to a writable allocation of at least
|
| -** n bytes.
|
| -**
|
| -** If the memory cell currently contains string or blob data
|
| -** and the third argument passed to this function is true, the
|
| -** current content of the cell is preserved. Otherwise, it may
|
| -** be discarded.
|
| +** min(n,32) bytes.
|
| **
|
| -** This function sets the MEM_Dyn flag and clears any xDel callback.
|
| -** It also clears MEM_Ephem and MEM_Static. If the preserve flag is
|
| -** not set, Mem.n is zeroed.
|
| +** If the bPreserve argument is true, then copy of the content of
|
| +** pMem->z into the new allocation. pMem must be either a string or
|
| +** blob if bPreserve is true. If bPreserve is false, any prior content
|
| +** in pMem->z is discarded.
|
| */
|
| -int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
|
| - assert( 1 >=
|
| - ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
|
| - (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) +
|
| - ((pMem->flags&MEM_Ephem) ? 1 : 0) +
|
| - ((pMem->flags&MEM_Static) ? 1 : 0)
|
| - );
|
| +SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
|
| + assert( sqlite3VdbeCheckMemInvariants(pMem) );
|
| assert( (pMem->flags&MEM_RowSet)==0 );
|
|
|
| - if( n<32 ) n = 32;
|
| - if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
|
| - if( preserve && pMem->z==pMem->zMalloc ){
|
| + /* If the bPreserve flag is set to true, then the memory cell must already
|
| + ** contain a valid string or blob value. */
|
| + assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
|
| + testcase( bPreserve && pMem->z==0 );
|
| +
|
| + assert( pMem->szMalloc==0
|
| + || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
|
| + if( pMem->szMalloc<n ){
|
| + if( n<32 ) n = 32;
|
| + if( bPreserve && pMem->szMalloc>0 && pMem->z==pMem->zMalloc ){
|
| pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
|
| - preserve = 0;
|
| + bPreserve = 0;
|
| }else{
|
| - sqlite3DbFree(pMem->db, pMem->zMalloc);
|
| + if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
|
| pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
|
| }
|
| + if( pMem->zMalloc==0 ){
|
| + sqlite3VdbeMemSetNull(pMem);
|
| + pMem->z = 0;
|
| + pMem->szMalloc = 0;
|
| + return SQLITE_NOMEM;
|
| + }else{
|
| + pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
|
| + }
|
| }
|
|
|
| - if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
|
| + if( bPreserve && pMem->z && pMem->z!=pMem->zMalloc ){
|
| memcpy(pMem->zMalloc, pMem->z, pMem->n);
|
| }
|
| - if( pMem->flags&MEM_Dyn && pMem->xDel ){
|
| + if( (pMem->flags&MEM_Dyn)!=0 ){
|
| + assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
|
| pMem->xDel((void *)(pMem->z));
|
| }
|
|
|
| pMem->z = pMem->zMalloc;
|
| - if( pMem->z==0 ){
|
| - pMem->flags = MEM_Null;
|
| - }else{
|
| - pMem->flags &= ~(MEM_Ephem|MEM_Static);
|
| + pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
|
| + return SQLITE_OK;
|
| +}
|
| +
|
| +/*
|
| +** Change the pMem->zMalloc allocation to be at least szNew bytes.
|
| +** If pMem->zMalloc already meets or exceeds the requested size, this
|
| +** routine is a no-op.
|
| +**
|
| +** Any prior string or blob content in the pMem object may be discarded.
|
| +** The pMem->xDel destructor is called, if it exists. Though MEM_Str
|
| +** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null
|
| +** values are preserved.
|
| +**
|
| +** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
|
| +** if unable to complete the resizing.
|
| +*/
|
| +int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
|
| + assert( szNew>0 );
|
| + assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
|
| + if( pMem->szMalloc<szNew ){
|
| + return sqlite3VdbeMemGrow(pMem, szNew, 0);
|
| }
|
| - pMem->xDel = 0;
|
| - return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
|
| + assert( (pMem->flags & MEM_Dyn)==0 );
|
| + pMem->z = pMem->zMalloc;
|
| + pMem->flags &= (MEM_Null|MEM_Int|MEM_Real);
|
| + return SQLITE_OK;
|
| }
|
|
|
| /*
|
| -** Make the given Mem object MEM_Dyn. In other words, make it so
|
| -** that any TEXT or BLOB content is stored in memory obtained from
|
| -** malloc(). In this way, we know that the memory is safe to be
|
| -** overwritten or altered.
|
| +** Change pMem so that its MEM_Str or MEM_Blob value is stored in
|
| +** MEM.zMalloc, where it can be safely written.
|
| **
|
| ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
|
| */
|
| @@ -123,9 +191,9 @@ int sqlite3VdbeMemMakeWriteable(Mem *pMem){
|
| int f;
|
| assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
|
| assert( (pMem->flags&MEM_RowSet)==0 );
|
| - expandBlob(pMem);
|
| + ExpandBlob(pMem);
|
| f = pMem->flags;
|
| - if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
|
| + if( (f&(MEM_Str|MEM_Blob)) && (pMem->szMalloc==0 || pMem->z!=pMem->zMalloc) ){
|
| if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
|
| return SQLITE_NOMEM;
|
| }
|
| @@ -169,15 +237,11 @@ int sqlite3VdbeMemExpandBlob(Mem *pMem){
|
| }
|
| #endif
|
|
|
| -
|
| /*
|
| -** Make sure the given Mem is \u0000 terminated.
|
| +** It is already known that pMem contains an unterminated string.
|
| +** Add the zero terminator.
|
| */
|
| -int sqlite3VdbeMemNulTerminate(Mem *pMem){
|
| - assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
|
| - if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
|
| - return SQLITE_OK; /* Nothing to do */
|
| - }
|
| +static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
|
| if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
|
| return SQLITE_NOMEM;
|
| }
|
| @@ -188,20 +252,34 @@ int sqlite3VdbeMemNulTerminate(Mem *pMem){
|
| }
|
|
|
| /*
|
| +** Make sure the given Mem is \u0000 terminated.
|
| +*/
|
| +int sqlite3VdbeMemNulTerminate(Mem *pMem){
|
| + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
|
| + testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
|
| + testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
|
| + if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
|
| + return SQLITE_OK; /* Nothing to do */
|
| + }else{
|
| + return vdbeMemAddTerminator(pMem);
|
| + }
|
| +}
|
| +
|
| +/*
|
| ** Add MEM_Str to the set of representations for the given Mem. Numbers
|
| ** are converted using sqlite3_snprintf(). Converting a BLOB to a string
|
| ** is a no-op.
|
| **
|
| -** Existing representations MEM_Int and MEM_Real are *not* invalidated.
|
| +** Existing representations MEM_Int and MEM_Real are invalidated if
|
| +** bForce is true but are retained if bForce is false.
|
| **
|
| ** A MEM_Null value will never be passed to this function. This function is
|
| ** used for converting values to text for returning to the user (i.e. via
|
| ** sqlite3_value_text()), or for ensuring that values to be used as btree
|
| ** keys are strings. In the former case a NULL pointer is returned the
|
| -** user and the later is an internal programming error.
|
| +** user and the latter is an internal programming error.
|
| */
|
| -int sqlite3VdbeMemStringify(Mem *pMem, int enc){
|
| - int rc = SQLITE_OK;
|
| +int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
|
| int fg = pMem->flags;
|
| const int nByte = 32;
|
|
|
| @@ -213,11 +291,11 @@ int sqlite3VdbeMemStringify(Mem *pMem, int enc){
|
| assert( EIGHT_BYTE_ALIGNMENT(pMem) );
|
|
|
|
|
| - if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
|
| + if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
|
| return SQLITE_NOMEM;
|
| }
|
|
|
| - /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
|
| + /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
|
| ** string representation of the value. Then, if the required encoding
|
| ** is UTF-16le or UTF-16be do a translation.
|
| **
|
| @@ -227,13 +305,14 @@ int sqlite3VdbeMemStringify(Mem *pMem, int enc){
|
| sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
|
| }else{
|
| assert( fg & MEM_Real );
|
| - sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
|
| + sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
|
| }
|
| pMem->n = sqlite3Strlen30(pMem->z);
|
| pMem->enc = SQLITE_UTF8;
|
| pMem->flags |= MEM_Str|MEM_Term;
|
| + if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
|
| sqlite3VdbeChangeEncoding(pMem, enc);
|
| - return rc;
|
| + return SQLITE_OK;
|
| }
|
|
|
| /*
|
| @@ -248,74 +327,96 @@ int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
|
| int rc = SQLITE_OK;
|
| if( ALWAYS(pFunc && pFunc->xFinalize) ){
|
| sqlite3_context ctx;
|
| + Mem t;
|
| assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
|
| assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
|
| memset(&ctx, 0, sizeof(ctx));
|
| - ctx.s.flags = MEM_Null;
|
| - ctx.s.db = pMem->db;
|
| + memset(&t, 0, sizeof(t));
|
| + t.flags = MEM_Null;
|
| + t.db = pMem->db;
|
| + ctx.pOut = &t;
|
| ctx.pMem = pMem;
|
| ctx.pFunc = pFunc;
|
| pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
|
| - assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
|
| - sqlite3DbFree(pMem->db, pMem->zMalloc);
|
| - memcpy(pMem, &ctx.s, sizeof(ctx.s));
|
| + assert( (pMem->flags & MEM_Dyn)==0 );
|
| + if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
|
| + memcpy(pMem, &t, sizeof(t));
|
| rc = ctx.isError;
|
| }
|
| return rc;
|
| }
|
|
|
| /*
|
| -** If the memory cell contains a string value that must be freed by
|
| -** invoking an external callback, free it now. Calling this function
|
| -** does not free any Mem.zMalloc buffer.
|
| +** If the memory cell contains a value that must be freed by
|
| +** invoking the external callback in Mem.xDel, then this routine
|
| +** will free that value. It also sets Mem.flags to MEM_Null.
|
| +**
|
| +** This is a helper routine for sqlite3VdbeMemSetNull() and
|
| +** for sqlite3VdbeMemRelease(). Use those other routines as the
|
| +** entry point for releasing Mem resources.
|
| */
|
| -void sqlite3VdbeMemReleaseExternal(Mem *p){
|
| +static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){
|
| assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
|
| - testcase( p->flags & MEM_Agg );
|
| - testcase( p->flags & MEM_Dyn );
|
| - testcase( p->flags & MEM_RowSet );
|
| - testcase( p->flags & MEM_Frame );
|
| - if( p->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame) ){
|
| - if( p->flags&MEM_Agg ){
|
| - sqlite3VdbeMemFinalize(p, p->u.pDef);
|
| - assert( (p->flags & MEM_Agg)==0 );
|
| - sqlite3VdbeMemRelease(p);
|
| - }else if( p->flags&MEM_Dyn && p->xDel ){
|
| - assert( (p->flags&MEM_RowSet)==0 );
|
| - p->xDel((void *)p->z);
|
| - p->xDel = 0;
|
| - }else if( p->flags&MEM_RowSet ){
|
| - sqlite3RowSetClear(p->u.pRowSet);
|
| - }else if( p->flags&MEM_Frame ){
|
| - sqlite3VdbeMemSetNull(p);
|
| - }
|
| + assert( VdbeMemDynamic(p) );
|
| + if( p->flags&MEM_Agg ){
|
| + sqlite3VdbeMemFinalize(p, p->u.pDef);
|
| + assert( (p->flags & MEM_Agg)==0 );
|
| + testcase( p->flags & MEM_Dyn );
|
| }
|
| + if( p->flags&MEM_Dyn ){
|
| + assert( (p->flags&MEM_RowSet)==0 );
|
| + assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
|
| + p->xDel((void *)p->z);
|
| + }else if( p->flags&MEM_RowSet ){
|
| + sqlite3RowSetClear(p->u.pRowSet);
|
| + }else if( p->flags&MEM_Frame ){
|
| + VdbeFrame *pFrame = p->u.pFrame;
|
| + pFrame->pParent = pFrame->v->pDelFrame;
|
| + pFrame->v->pDelFrame = pFrame;
|
| + }
|
| + p->flags = MEM_Null;
|
| }
|
|
|
| /*
|
| -** Release any memory held by the Mem. This may leave the Mem in an
|
| -** inconsistent state, for example with (Mem.z==0) and
|
| -** (Mem.type==SQLITE_TEXT).
|
| +** Release memory held by the Mem p, both external memory cleared
|
| +** by p->xDel and memory in p->zMalloc.
|
| +**
|
| +** This is a helper routine invoked by sqlite3VdbeMemRelease() in
|
| +** the unusual case where there really is memory in p that needs
|
| +** to be freed.
|
| */
|
| -void sqlite3VdbeMemRelease(Mem *p){
|
| - sqlite3VdbeMemReleaseExternal(p);
|
| - sqlite3DbFree(p->db, p->zMalloc);
|
| +static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
|
| + if( VdbeMemDynamic(p) ){
|
| + vdbeMemClearExternAndSetNull(p);
|
| + }
|
| + if( p->szMalloc ){
|
| + sqlite3DbFree(p->db, p->zMalloc);
|
| + p->szMalloc = 0;
|
| + }
|
| p->z = 0;
|
| - p->zMalloc = 0;
|
| - p->xDel = 0;
|
| }
|
|
|
| /*
|
| -** Convert a 64-bit IEEE double into a 64-bit signed integer.
|
| -** If the double is too large, return 0x8000000000000000.
|
| +** Release any memory resources held by the Mem. Both the memory that is
|
| +** free by Mem.xDel and the Mem.zMalloc allocation are freed.
|
| **
|
| -** Most systems appear to do this simply by assigning
|
| -** variables and without the extra range tests. But
|
| -** there are reports that windows throws an expection
|
| -** if the floating point value is out of range. (See ticket #2880.)
|
| -** Because we do not completely understand the problem, we will
|
| -** take the conservative approach and always do range tests
|
| -** before attempting the conversion.
|
| +** Use this routine prior to clean up prior to abandoning a Mem, or to
|
| +** reset a Mem back to its minimum memory utilization.
|
| +**
|
| +** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
|
| +** prior to inserting new content into the Mem.
|
| +*/
|
| +void sqlite3VdbeMemRelease(Mem *p){
|
| + assert( sqlite3VdbeCheckMemInvariants(p) );
|
| + if( VdbeMemDynamic(p) || p->szMalloc ){
|
| + vdbeMemClear(p);
|
| + }
|
| +}
|
| +
|
| +/*
|
| +** Convert a 64-bit IEEE double into a 64-bit signed integer.
|
| +** If the double is out of range of a 64-bit signed integer then
|
| +** return the closest available 64-bit signed integer.
|
| */
|
| static i64 doubleToInt64(double r){
|
| #ifdef SQLITE_OMIT_FLOATING_POINT
|
| @@ -332,14 +433,10 @@ static i64 doubleToInt64(double r){
|
| static const i64 maxInt = LARGEST_INT64;
|
| static const i64 minInt = SMALLEST_INT64;
|
|
|
| - if( r<(double)minInt ){
|
| - return minInt;
|
| - }else if( r>(double)maxInt ){
|
| - /* minInt is correct here - not maxInt. It turns out that assigning
|
| - ** a very large positive number to an integer results in a very large
|
| - ** negative integer. This makes no sense, but it is what x86 hardware
|
| - ** does so for compatibility we will do the same in software. */
|
| + if( r<=(double)minInt ){
|
| return minInt;
|
| + }else if( r>=(double)maxInt ){
|
| + return maxInt;
|
| }else{
|
| return (i64)r;
|
| }
|
| @@ -352,7 +449,7 @@ static i64 doubleToInt64(double r){
|
| ** If pMem is an integer, then the value is exact. If pMem is
|
| ** a floating-point then the value returned is the integer part.
|
| ** If pMem is a string or blob, then we make an attempt to convert
|
| -** it into a integer and return that. If pMem represents an
|
| +** it into an integer and return that. If pMem represents an
|
| ** an SQL-NULL value, return 0.
|
| **
|
| ** If pMem represents a string value, its encoding might be changed.
|
| @@ -365,11 +462,10 @@ i64 sqlite3VdbeIntValue(Mem *pMem){
|
| if( flags & MEM_Int ){
|
| return pMem->u.i;
|
| }else if( flags & MEM_Real ){
|
| - return doubleToInt64(pMem->r);
|
| + return doubleToInt64(pMem->u.r);
|
| }else if( flags & (MEM_Str|MEM_Blob) ){
|
| i64 value = 0;
|
| assert( pMem->z || pMem->n==0 );
|
| - testcase( pMem->z==0 );
|
| sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
|
| return value;
|
| }else{
|
| @@ -387,7 +483,7 @@ double sqlite3VdbeRealValue(Mem *pMem){
|
| assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
|
| assert( EIGHT_BYTE_ALIGNMENT(pMem) );
|
| if( pMem->flags & MEM_Real ){
|
| - return pMem->r;
|
| + return pMem->u.r;
|
| }else if( pMem->flags & MEM_Int ){
|
| return (double)pMem->u.i;
|
| }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
|
| @@ -406,12 +502,13 @@ double sqlite3VdbeRealValue(Mem *pMem){
|
| ** MEM_Int if we can.
|
| */
|
| void sqlite3VdbeIntegerAffinity(Mem *pMem){
|
| + i64 ix;
|
| assert( pMem->flags & MEM_Real );
|
| assert( (pMem->flags & MEM_RowSet)==0 );
|
| assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
|
| assert( EIGHT_BYTE_ALIGNMENT(pMem) );
|
|
|
| - pMem->u.i = doubleToInt64(pMem->r);
|
| + ix = doubleToInt64(pMem->u.r);
|
|
|
| /* Only mark the value as an integer if
|
| **
|
| @@ -421,13 +518,11 @@ void sqlite3VdbeIntegerAffinity(Mem *pMem){
|
| **
|
| ** The second and third terms in the following conditional enforces
|
| ** the second condition under the assumption that addition overflow causes
|
| - ** values to wrap around. On x86 hardware, the third term is always
|
| - ** true and could be omitted. But we leave it in because other
|
| - ** architectures might behave differently.
|
| + ** values to wrap around.
|
| */
|
| - if( pMem->r==(double)pMem->u.i && pMem->u.i>SMALLEST_INT64
|
| - && ALWAYS(pMem->u.i<LARGEST_INT64) ){
|
| - pMem->flags |= MEM_Int;
|
| + if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
|
| + pMem->u.i = ix;
|
| + MemSetTypeFlag(pMem, MEM_Int);
|
| }
|
| }
|
|
|
| @@ -452,7 +547,7 @@ int sqlite3VdbeMemRealify(Mem *pMem){
|
| assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
|
| assert( EIGHT_BYTE_ALIGNMENT(pMem) );
|
|
|
| - pMem->r = sqlite3VdbeRealValue(pMem);
|
| + pMem->u.r = sqlite3VdbeRealValue(pMem);
|
| MemSetTypeFlag(pMem, MEM_Real);
|
| return SQLITE_OK;
|
| }
|
| @@ -472,7 +567,7 @@ int sqlite3VdbeMemNumerify(Mem *pMem){
|
| if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
|
| MemSetTypeFlag(pMem, MEM_Int);
|
| }else{
|
| - pMem->r = sqlite3VdbeRealValue(pMem);
|
| + pMem->u.r = sqlite3VdbeRealValue(pMem);
|
| MemSetTypeFlag(pMem, MEM_Real);
|
| sqlite3VdbeIntegerAffinity(pMem);
|
| }
|
| @@ -483,19 +578,83 @@ int sqlite3VdbeMemNumerify(Mem *pMem){
|
| }
|
|
|
| /*
|
| +** Cast the datatype of the value in pMem according to the affinity
|
| +** "aff". Casting is different from applying affinity in that a cast
|
| +** is forced. In other words, the value is converted into the desired
|
| +** affinity even if that results in loss of data. This routine is
|
| +** used (for example) to implement the SQL "cast()" operator.
|
| +*/
|
| +void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
|
| + if( pMem->flags & MEM_Null ) return;
|
| + switch( aff ){
|
| + case SQLITE_AFF_NONE: { /* Really a cast to BLOB */
|
| + if( (pMem->flags & MEM_Blob)==0 ){
|
| + sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
|
| + assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
|
| + MemSetTypeFlag(pMem, MEM_Blob);
|
| + }else{
|
| + pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
|
| + }
|
| + break;
|
| + }
|
| + case SQLITE_AFF_NUMERIC: {
|
| + sqlite3VdbeMemNumerify(pMem);
|
| + break;
|
| + }
|
| + case SQLITE_AFF_INTEGER: {
|
| + sqlite3VdbeMemIntegerify(pMem);
|
| + break;
|
| + }
|
| + case SQLITE_AFF_REAL: {
|
| + sqlite3VdbeMemRealify(pMem);
|
| + break;
|
| + }
|
| + default: {
|
| + assert( aff==SQLITE_AFF_TEXT );
|
| + assert( MEM_Str==(MEM_Blob>>3) );
|
| + pMem->flags |= (pMem->flags&MEM_Blob)>>3;
|
| + sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
|
| + assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
|
| + pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
|
| + break;
|
| + }
|
| + }
|
| +}
|
| +
|
| +/*
|
| +** Initialize bulk memory to be a consistent Mem object.
|
| +**
|
| +** The minimum amount of initialization feasible is performed.
|
| +*/
|
| +void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
|
| + assert( (flags & ~MEM_TypeMask)==0 );
|
| + pMem->flags = flags;
|
| + pMem->db = db;
|
| + pMem->szMalloc = 0;
|
| +}
|
| +
|
| +
|
| +/*
|
| ** Delete any previous value and set the value stored in *pMem to NULL.
|
| +**
|
| +** This routine calls the Mem.xDel destructor to dispose of values that
|
| +** require the destructor. But it preserves the Mem.zMalloc memory allocation.
|
| +** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
|
| +** routine to invoke the destructor and deallocates Mem.zMalloc.
|
| +**
|
| +** Use this routine to reset the Mem prior to insert a new value.
|
| +**
|
| +** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
|
| */
|
| void sqlite3VdbeMemSetNull(Mem *pMem){
|
| - if( pMem->flags & MEM_Frame ){
|
| - VdbeFrame *pFrame = pMem->u.pFrame;
|
| - pFrame->pParent = pFrame->v->pDelFrame;
|
| - pFrame->v->pDelFrame = pFrame;
|
| - }
|
| - if( pMem->flags & MEM_RowSet ){
|
| - sqlite3RowSetClear(pMem->u.pRowSet);
|
| + if( VdbeMemDynamic(pMem) ){
|
| + vdbeMemClearExternAndSetNull(pMem);
|
| + }else{
|
| + pMem->flags = MEM_Null;
|
| }
|
| - MemSetTypeFlag(pMem, MEM_Null);
|
| - pMem->type = SQLITE_NULL;
|
| +}
|
| +void sqlite3ValueSetNull(sqlite3_value *p){
|
| + sqlite3VdbeMemSetNull((Mem*)p);
|
| }
|
|
|
| /*
|
| @@ -505,19 +664,22 @@ void sqlite3VdbeMemSetNull(Mem *pMem){
|
| void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
|
| sqlite3VdbeMemRelease(pMem);
|
| pMem->flags = MEM_Blob|MEM_Zero;
|
| - pMem->type = SQLITE_BLOB;
|
| pMem->n = 0;
|
| if( n<0 ) n = 0;
|
| pMem->u.nZero = n;
|
| pMem->enc = SQLITE_UTF8;
|
| + pMem->z = 0;
|
| +}
|
|
|
| -#ifdef SQLITE_OMIT_INCRBLOB
|
| - sqlite3VdbeMemGrow(pMem, n, 0);
|
| - if( pMem->z ){
|
| - pMem->n = n;
|
| - memset(pMem->z, 0, n);
|
| - }
|
| -#endif
|
| +/*
|
| +** The pMem is known to contain content that needs to be destroyed prior
|
| +** to a value change. So invoke the destructor, then set the value to
|
| +** a 64-bit integer.
|
| +*/
|
| +static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
|
| + sqlite3VdbeMemSetNull(pMem);
|
| + pMem->u.i = val;
|
| + pMem->flags = MEM_Int;
|
| }
|
|
|
| /*
|
| @@ -525,10 +687,12 @@ void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
|
| ** manifest type INTEGER.
|
| */
|
| void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
|
| - sqlite3VdbeMemRelease(pMem);
|
| - pMem->u.i = val;
|
| - pMem->flags = MEM_Int;
|
| - pMem->type = SQLITE_INTEGER;
|
| + if( VdbeMemDynamic(pMem) ){
|
| + vdbeReleaseAndSetInt64(pMem, val);
|
| + }else{
|
| + pMem->u.i = val;
|
| + pMem->flags = MEM_Int;
|
| + }
|
| }
|
|
|
| #ifndef SQLITE_OMIT_FLOATING_POINT
|
| @@ -537,13 +701,10 @@ void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
|
| ** manifest type REAL.
|
| */
|
| void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
|
| - if( sqlite3IsNaN(val) ){
|
| - sqlite3VdbeMemSetNull(pMem);
|
| - }else{
|
| - sqlite3VdbeMemRelease(pMem);
|
| - pMem->r = val;
|
| + sqlite3VdbeMemSetNull(pMem);
|
| + if( !sqlite3IsNaN(val) ){
|
| + pMem->u.r = val;
|
| pMem->flags = MEM_Real;
|
| - pMem->type = SQLITE_FLOAT;
|
| }
|
| }
|
| #endif
|
| @@ -560,10 +721,11 @@ void sqlite3VdbeMemSetRowSet(Mem *pMem){
|
| pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
|
| if( db->mallocFailed ){
|
| pMem->flags = MEM_Null;
|
| + pMem->szMalloc = 0;
|
| }else{
|
| assert( pMem->zMalloc );
|
| - pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc,
|
| - sqlite3DbMallocSize(db, pMem->zMalloc));
|
| + pMem->szMalloc = sqlite3DbMallocSize(db, pMem->zMalloc);
|
| + pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, pMem->szMalloc);
|
| assert( pMem->u.pRowSet!=0 );
|
| pMem->flags = MEM_RowSet;
|
| }
|
| @@ -587,19 +749,19 @@ int sqlite3VdbeMemTooBig(Mem *p){
|
|
|
| #ifdef SQLITE_DEBUG
|
| /*
|
| -** This routine prepares a memory cell for modication by breaking
|
| +** This routine prepares a memory cell for modification by breaking
|
| ** its link to a shallow copy and by marking any current shallow
|
| ** copies of this cell as invalid.
|
| **
|
| ** This is used for testing and debugging only - to make sure shallow
|
| ** copies are not misused.
|
| */
|
| -void sqlite3VdbeMemPrepareToChange(Vdbe *pVdbe, Mem *pMem){
|
| +void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
|
| int i;
|
| Mem *pX;
|
| for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
|
| if( pX->pScopyFrom==pMem ){
|
| - pX->flags |= MEM_Invalid;
|
| + pX->flags |= MEM_Undefined;
|
| pX->pScopyFrom = 0;
|
| }
|
| }
|
| @@ -610,7 +772,7 @@ void sqlite3VdbeMemPrepareToChange(Vdbe *pVdbe, Mem *pMem){
|
| /*
|
| ** Size of struct Mem not including the Mem.zMalloc member.
|
| */
|
| -#define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
|
| +#define MEMCELLSIZE offsetof(Mem,zMalloc)
|
|
|
| /*
|
| ** Make an shallow copy of pFrom into pTo. Prior contents of
|
| @@ -620,9 +782,9 @@ void sqlite3VdbeMemPrepareToChange(Vdbe *pVdbe, Mem *pMem){
|
| */
|
| void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
|
| assert( (pFrom->flags & MEM_RowSet)==0 );
|
| - sqlite3VdbeMemReleaseExternal(pTo);
|
| + assert( pTo->db==pFrom->db );
|
| + if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
|
| memcpy(pTo, pFrom, MEMCELLSIZE);
|
| - pTo->xDel = 0;
|
| if( (pFrom->flags&MEM_Static)==0 ){
|
| pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
|
| assert( srcType==MEM_Ephem || srcType==MEM_Static );
|
| @@ -637,11 +799,11 @@ void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
|
| int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
|
| int rc = SQLITE_OK;
|
|
|
| + assert( pTo->db==pFrom->db );
|
| assert( (pFrom->flags & MEM_RowSet)==0 );
|
| - sqlite3VdbeMemReleaseExternal(pTo);
|
| + if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
|
| memcpy(pTo, pFrom, MEMCELLSIZE);
|
| pTo->flags &= ~MEM_Dyn;
|
| -
|
| if( pTo->flags&(MEM_Str|MEM_Blob) ){
|
| if( 0==(pFrom->flags&MEM_Static) ){
|
| pTo->flags |= MEM_Ephem;
|
| @@ -666,8 +828,7 @@ void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
|
| sqlite3VdbeMemRelease(pTo);
|
| memcpy(pTo, pFrom, sizeof(Mem));
|
| pFrom->flags = MEM_Null;
|
| - pFrom->xDel = 0;
|
| - pFrom->zMalloc = 0;
|
| + pFrom->szMalloc = 0;
|
| }
|
|
|
| /*
|
| @@ -714,7 +875,8 @@ int sqlite3VdbeMemSetStr(
|
| if( nByte<0 ){
|
| assert( enc!=0 );
|
| if( enc==SQLITE_UTF8 ){
|
| - for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
|
| + nByte = sqlite3Strlen30(z);
|
| + if( nByte>iLimit ) nByte = iLimit+1;
|
| }else{
|
| for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
|
| }
|
| @@ -733,14 +895,17 @@ int sqlite3VdbeMemSetStr(
|
| if( nByte>iLimit ){
|
| return SQLITE_TOOBIG;
|
| }
|
| - if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
|
| + testcase( nAlloc==0 );
|
| + testcase( nAlloc==31 );
|
| + testcase( nAlloc==32 );
|
| + if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){
|
| return SQLITE_NOMEM;
|
| }
|
| memcpy(pMem->z, z, nAlloc);
|
| }else if( xDel==SQLITE_DYNAMIC ){
|
| sqlite3VdbeMemRelease(pMem);
|
| pMem->zMalloc = pMem->z = (char *)z;
|
| - pMem->xDel = 0;
|
| + pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
|
| }else{
|
| sqlite3VdbeMemRelease(pMem);
|
| pMem->z = (char *)z;
|
| @@ -751,7 +916,6 @@ int sqlite3VdbeMemSetStr(
|
| pMem->n = nByte;
|
| pMem->flags = flags;
|
| pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
|
| - pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
|
|
|
| #ifndef SQLITE_OMIT_UTF16
|
| if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
|
| @@ -767,148 +931,34 @@ int sqlite3VdbeMemSetStr(
|
| }
|
|
|
| /*
|
| -** Compare the values contained by the two memory cells, returning
|
| -** negative, zero or positive if pMem1 is less than, equal to, or greater
|
| -** than pMem2. Sorting order is NULL's first, followed by numbers (integers
|
| -** and reals) sorted numerically, followed by text ordered by the collating
|
| -** sequence pColl and finally blob's ordered by memcmp().
|
| -**
|
| -** Two NULL values are considered equal by this function.
|
| -*/
|
| -int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
|
| - int rc;
|
| - int f1, f2;
|
| - int combined_flags;
|
| -
|
| - f1 = pMem1->flags;
|
| - f2 = pMem2->flags;
|
| - combined_flags = f1|f2;
|
| - assert( (combined_flags & MEM_RowSet)==0 );
|
| -
|
| - /* If one value is NULL, it is less than the other. If both values
|
| - ** are NULL, return 0.
|
| - */
|
| - if( combined_flags&MEM_Null ){
|
| - return (f2&MEM_Null) - (f1&MEM_Null);
|
| - }
|
| -
|
| - /* If one value is a number and the other is not, the number is less.
|
| - ** If both are numbers, compare as reals if one is a real, or as integers
|
| - ** if both values are integers.
|
| - */
|
| - if( combined_flags&(MEM_Int|MEM_Real) ){
|
| - if( !(f1&(MEM_Int|MEM_Real)) ){
|
| - return 1;
|
| - }
|
| - if( !(f2&(MEM_Int|MEM_Real)) ){
|
| - return -1;
|
| - }
|
| - if( (f1 & f2 & MEM_Int)==0 ){
|
| - double r1, r2;
|
| - if( (f1&MEM_Real)==0 ){
|
| - r1 = (double)pMem1->u.i;
|
| - }else{
|
| - r1 = pMem1->r;
|
| - }
|
| - if( (f2&MEM_Real)==0 ){
|
| - r2 = (double)pMem2->u.i;
|
| - }else{
|
| - r2 = pMem2->r;
|
| - }
|
| - if( r1<r2 ) return -1;
|
| - if( r1>r2 ) return 1;
|
| - return 0;
|
| - }else{
|
| - assert( f1&MEM_Int );
|
| - assert( f2&MEM_Int );
|
| - if( pMem1->u.i < pMem2->u.i ) return -1;
|
| - if( pMem1->u.i > pMem2->u.i ) return 1;
|
| - return 0;
|
| - }
|
| - }
|
| -
|
| - /* If one value is a string and the other is a blob, the string is less.
|
| - ** If both are strings, compare using the collating functions.
|
| - */
|
| - if( combined_flags&MEM_Str ){
|
| - if( (f1 & MEM_Str)==0 ){
|
| - return 1;
|
| - }
|
| - if( (f2 & MEM_Str)==0 ){
|
| - return -1;
|
| - }
|
| -
|
| - assert( pMem1->enc==pMem2->enc );
|
| - assert( pMem1->enc==SQLITE_UTF8 ||
|
| - pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
|
| -
|
| - /* The collation sequence must be defined at this point, even if
|
| - ** the user deletes the collation sequence after the vdbe program is
|
| - ** compiled (this was not always the case).
|
| - */
|
| - assert( !pColl || pColl->xCmp );
|
| -
|
| - if( pColl ){
|
| - if( pMem1->enc==pColl->enc ){
|
| - /* The strings are already in the correct encoding. Call the
|
| - ** comparison function directly */
|
| - return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
|
| - }else{
|
| - const void *v1, *v2;
|
| - int n1, n2;
|
| - Mem c1;
|
| - Mem c2;
|
| - memset(&c1, 0, sizeof(c1));
|
| - memset(&c2, 0, sizeof(c2));
|
| - sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
|
| - sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
|
| - v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
|
| - n1 = v1==0 ? 0 : c1.n;
|
| - v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
|
| - n2 = v2==0 ? 0 : c2.n;
|
| - rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
|
| - sqlite3VdbeMemRelease(&c1);
|
| - sqlite3VdbeMemRelease(&c2);
|
| - return rc;
|
| - }
|
| - }
|
| - /* If a NULL pointer was passed as the collate function, fall through
|
| - ** to the blob case and use memcmp(). */
|
| - }
|
| -
|
| - /* Both values must be blobs. Compare using memcmp(). */
|
| - rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
|
| - if( rc==0 ){
|
| - rc = pMem1->n - pMem2->n;
|
| - }
|
| - return rc;
|
| -}
|
| -
|
| -/*
|
| ** Move data out of a btree key or data field and into a Mem structure.
|
| ** The data or key is taken from the entry that pCur is currently pointing
|
| ** to. offset and amt determine what portion of the data or key to retrieve.
|
| ** key is true to get the key or false to get data. The result is written
|
| ** into the pMem element.
|
| **
|
| -** The pMem structure is assumed to be uninitialized. Any prior content
|
| -** is overwritten without being freed.
|
| +** The pMem object must have been initialized. This routine will use
|
| +** pMem->zMalloc to hold the content from the btree, if possible. New
|
| +** pMem->zMalloc space will be allocated if necessary. The calling routine
|
| +** is responsible for making sure that the pMem object is eventually
|
| +** destroyed.
|
| **
|
| ** If this routine fails for any reason (malloc returns NULL or unable
|
| ** to read from the disk) then the pMem is left in an inconsistent state.
|
| */
|
| int sqlite3VdbeMemFromBtree(
|
| BtCursor *pCur, /* Cursor pointing at record to retrieve. */
|
| - int offset, /* Offset from the start of data to return bytes from. */
|
| - int amt, /* Number of bytes to return. */
|
| + u32 offset, /* Offset from the start of data to return bytes from. */
|
| + u32 amt, /* Number of bytes to return. */
|
| int key, /* If true, retrieve from the btree key, not data. */
|
| Mem *pMem /* OUT: Return data in this Mem structure. */
|
| ){
|
| char *zData; /* Data from the btree layer */
|
| - int available = 0; /* Number of bytes available on the local btree page */
|
| + u32 available = 0; /* Number of bytes available on the local btree page */
|
| int rc = SQLITE_OK; /* Return code */
|
|
|
| assert( sqlite3BtreeCursorIsValid(pCur) );
|
| + assert( !VdbeMemDynamic(pMem) );
|
|
|
| /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
|
| ** that both the BtShared and database handle mutexes are held. */
|
| @@ -920,65 +970,60 @@ int sqlite3VdbeMemFromBtree(
|
| }
|
| assert( zData!=0 );
|
|
|
| - if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
|
| - sqlite3VdbeMemRelease(pMem);
|
| + if( offset+amt<=available ){
|
| pMem->z = &zData[offset];
|
| pMem->flags = MEM_Blob|MEM_Ephem;
|
| - }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
|
| - pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
|
| - pMem->enc = 0;
|
| - pMem->type = SQLITE_BLOB;
|
| - if( key ){
|
| - rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
|
| - }else{
|
| - rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
|
| - }
|
| - pMem->z[amt] = 0;
|
| - pMem->z[amt+1] = 0;
|
| - if( rc!=SQLITE_OK ){
|
| - sqlite3VdbeMemRelease(pMem);
|
| + pMem->n = (int)amt;
|
| + }else{
|
| + pMem->flags = MEM_Null;
|
| + if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
|
| + if( key ){
|
| + rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
|
| + }else{
|
| + rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
|
| + }
|
| + if( rc==SQLITE_OK ){
|
| + pMem->z[amt] = 0;
|
| + pMem->z[amt+1] = 0;
|
| + pMem->flags = MEM_Blob|MEM_Term;
|
| + pMem->n = (int)amt;
|
| + }else{
|
| + sqlite3VdbeMemRelease(pMem);
|
| + }
|
| }
|
| }
|
| - pMem->n = amt;
|
|
|
| return rc;
|
| }
|
|
|
| -/* This function is only available internally, it is not part of the
|
| -** external API. It works in a similar way to sqlite3_value_text(),
|
| -** except the data returned is in the encoding specified by the second
|
| -** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
|
| -** SQLITE_UTF8.
|
| -**
|
| -** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
|
| -** If that is the case, then the result must be aligned on an even byte
|
| -** boundary.
|
| +/*
|
| +** The pVal argument is known to be a value other than NULL.
|
| +** Convert it into a string with encoding enc and return a pointer
|
| +** to a zero-terminated version of that string.
|
| */
|
| -const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
|
| - if( !pVal ) return 0;
|
| -
|
| +static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
|
| + assert( pVal!=0 );
|
| assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
|
| assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
|
| assert( (pVal->flags & MEM_RowSet)==0 );
|
| -
|
| - if( pVal->flags&MEM_Null ){
|
| - return 0;
|
| - }
|
| - assert( (MEM_Blob>>3) == MEM_Str );
|
| - pVal->flags |= (pVal->flags & MEM_Blob)>>3;
|
| - expandBlob(pVal);
|
| - if( pVal->flags&MEM_Str ){
|
| - sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
|
| + assert( (pVal->flags & (MEM_Null))==0 );
|
| + if( pVal->flags & (MEM_Blob|MEM_Str) ){
|
| + pVal->flags |= MEM_Str;
|
| + if( pVal->flags & MEM_Zero ){
|
| + sqlite3VdbeMemExpandBlob(pVal);
|
| + }
|
| + if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
|
| + sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
|
| + }
|
| if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
|
| assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
|
| if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
|
| return 0;
|
| }
|
| }
|
| - sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-59893-45467 */
|
| + sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
|
| }else{
|
| - assert( (pVal->flags&MEM_Blob)==0 );
|
| - sqlite3VdbeMemStringify(pVal, enc);
|
| + sqlite3VdbeMemStringify(pVal, enc, 0);
|
| assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
|
| }
|
| assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
|
| @@ -990,6 +1035,30 @@ const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
|
| }
|
| }
|
|
|
| +/* This function is only available internally, it is not part of the
|
| +** external API. It works in a similar way to sqlite3_value_text(),
|
| +** except the data returned is in the encoding specified by the second
|
| +** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
|
| +** SQLITE_UTF8.
|
| +**
|
| +** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
|
| +** If that is the case, then the result must be aligned on an even byte
|
| +** boundary.
|
| +*/
|
| +const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
|
| + if( !pVal ) return 0;
|
| + assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
|
| + assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
|
| + assert( (pVal->flags & MEM_RowSet)==0 );
|
| + if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
|
| + return pVal->z;
|
| + }
|
| + if( pVal->flags&MEM_Null ){
|
| + return 0;
|
| + }
|
| + return valueToText(pVal, enc);
|
| +}
|
| +
|
| /*
|
| ** Create a new sqlite3_value object.
|
| */
|
| @@ -997,50 +1066,116 @@ sqlite3_value *sqlite3ValueNew(sqlite3 *db){
|
| Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
|
| if( p ){
|
| p->flags = MEM_Null;
|
| - p->type = SQLITE_NULL;
|
| p->db = db;
|
| }
|
| return p;
|
| }
|
|
|
| /*
|
| -** Create a new sqlite3_value object, containing the value of pExpr.
|
| +** Context object passed by sqlite3Stat4ProbeSetValue() through to
|
| +** valueNew(). See comments above valueNew() for details.
|
| +*/
|
| +struct ValueNewStat4Ctx {
|
| + Parse *pParse;
|
| + Index *pIdx;
|
| + UnpackedRecord **ppRec;
|
| + int iVal;
|
| +};
|
| +
|
| +/*
|
| +** Allocate and return a pointer to a new sqlite3_value object. If
|
| +** the second argument to this function is NULL, the object is allocated
|
| +** by calling sqlite3ValueNew().
|
| **
|
| -** This only works for very simple expressions that consist of one constant
|
| -** token (i.e. "5", "5.1", "'a string'"). If the expression can
|
| -** be converted directly into a value, then the value is allocated and
|
| -** a pointer written to *ppVal. The caller is responsible for deallocating
|
| -** the value by passing it to sqlite3ValueFree() later on. If the expression
|
| -** cannot be converted to a value, then *ppVal is set to NULL.
|
| +** Otherwise, if the second argument is non-zero, then this function is
|
| +** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
|
| +** already been allocated, allocate the UnpackedRecord structure that
|
| +** that function will return to its caller here. Then return a pointer
|
| +** an sqlite3_value within the UnpackedRecord.a[] array.
|
| */
|
| -int sqlite3ValueFromExpr(
|
| - sqlite3 *db, /* The database connection */
|
| - Expr *pExpr, /* The expression to evaluate */
|
| - u8 enc, /* Encoding to use */
|
| - u8 affinity, /* Affinity to use */
|
| - sqlite3_value **ppVal /* Write the new value here */
|
| +static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
|
| +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
|
| + if( p ){
|
| + UnpackedRecord *pRec = p->ppRec[0];
|
| +
|
| + if( pRec==0 ){
|
| + Index *pIdx = p->pIdx; /* Index being probed */
|
| + int nByte; /* Bytes of space to allocate */
|
| + int i; /* Counter variable */
|
| + int nCol = pIdx->nColumn; /* Number of index columns including rowid */
|
| +
|
| + nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
|
| + pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
|
| + if( pRec ){
|
| + pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
|
| + if( pRec->pKeyInfo ){
|
| + assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
|
| + assert( pRec->pKeyInfo->enc==ENC(db) );
|
| + pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
|
| + for(i=0; i<nCol; i++){
|
| + pRec->aMem[i].flags = MEM_Null;
|
| + pRec->aMem[i].db = db;
|
| + }
|
| + }else{
|
| + sqlite3DbFree(db, pRec);
|
| + pRec = 0;
|
| + }
|
| + }
|
| + if( pRec==0 ) return 0;
|
| + p->ppRec[0] = pRec;
|
| + }
|
| +
|
| + pRec->nField = p->iVal+1;
|
| + return &pRec->aMem[p->iVal];
|
| + }
|
| +#else
|
| + UNUSED_PARAMETER(p);
|
| +#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
|
| + return sqlite3ValueNew(db);
|
| +}
|
| +
|
| +/*
|
| +** Extract a value from the supplied expression in the manner described
|
| +** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
|
| +** using valueNew().
|
| +**
|
| +** If pCtx is NULL and an error occurs after the sqlite3_value object
|
| +** has been allocated, it is freed before returning. Or, if pCtx is not
|
| +** NULL, it is assumed that the caller will free any allocated object
|
| +** in all cases.
|
| +*/
|
| +static int valueFromExpr(
|
| + sqlite3 *db, /* The database connection */
|
| + Expr *pExpr, /* The expression to evaluate */
|
| + u8 enc, /* Encoding to use */
|
| + u8 affinity, /* Affinity to use */
|
| + sqlite3_value **ppVal, /* Write the new value here */
|
| + struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
|
| ){
|
| int op;
|
| char *zVal = 0;
|
| sqlite3_value *pVal = 0;
|
| int negInt = 1;
|
| const char *zNeg = "";
|
| + int rc = SQLITE_OK;
|
|
|
| if( !pExpr ){
|
| *ppVal = 0;
|
| return SQLITE_OK;
|
| }
|
| - op = pExpr->op;
|
| -
|
| - /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2.
|
| - ** The ifdef here is to enable us to achieve 100% branch test coverage even
|
| - ** when SQLITE_ENABLE_STAT2 is omitted.
|
| - */
|
| -#ifdef SQLITE_ENABLE_STAT2
|
| - if( op==TK_REGISTER ) op = pExpr->op2;
|
| -#else
|
| + while( (op = pExpr->op)==TK_UPLUS ) pExpr = pExpr->pLeft;
|
| if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
|
| -#endif
|
| +
|
| + if( op==TK_CAST ){
|
| + u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
|
| + rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
|
| + testcase( rc!=SQLITE_OK );
|
| + if( *ppVal ){
|
| + sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8);
|
| + sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8);
|
| + }
|
| + return rc;
|
| + }
|
|
|
| /* Handle negative integers in a single step. This is needed in the
|
| ** case when the value is -9223372036854775808.
|
| @@ -1054,7 +1189,7 @@ int sqlite3ValueFromExpr(
|
| }
|
|
|
| if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
|
| - pVal = sqlite3ValueNew(db);
|
| + pVal = valueNew(db, pCtx);
|
| if( pVal==0 ) goto no_mem;
|
| if( ExprHasProperty(pExpr, EP_IntValue) ){
|
| sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
|
| @@ -1062,7 +1197,6 @@ int sqlite3ValueFromExpr(
|
| zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
|
| if( zVal==0 ) goto no_mem;
|
| sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
|
| - if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
|
| }
|
| if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
|
| sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
|
| @@ -1071,24 +1205,26 @@ int sqlite3ValueFromExpr(
|
| }
|
| if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
|
| if( enc!=SQLITE_UTF8 ){
|
| - sqlite3VdbeChangeEncoding(pVal, enc);
|
| + rc = sqlite3VdbeChangeEncoding(pVal, enc);
|
| }
|
| }else if( op==TK_UMINUS ) {
|
| /* This branch happens for multiple negative signs. Ex: -(-5) */
|
| - if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
|
| + if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal)
|
| + && pVal!=0
|
| + ){
|
| sqlite3VdbeMemNumerify(pVal);
|
| - if( pVal->u.i==SMALLEST_INT64 ){
|
| - pVal->flags &= MEM_Int;
|
| - pVal->flags |= MEM_Real;
|
| - pVal->r = (double)LARGEST_INT64;
|
| + if( pVal->flags & MEM_Real ){
|
| + pVal->u.r = -pVal->u.r;
|
| + }else if( pVal->u.i==SMALLEST_INT64 ){
|
| + pVal->u.r = -(double)SMALLEST_INT64;
|
| + MemSetTypeFlag(pVal, MEM_Real);
|
| }else{
|
| pVal->u.i = -pVal->u.i;
|
| }
|
| - pVal->r = -pVal->r;
|
| sqlite3ValueApplyAffinity(pVal, affinity, enc);
|
| }
|
| }else if( op==TK_NULL ){
|
| - pVal = sqlite3ValueNew(db);
|
| + pVal = valueNew(db, pCtx);
|
| if( pVal==0 ) goto no_mem;
|
| }
|
| #ifndef SQLITE_OMIT_BLOB_LITERAL
|
| @@ -1096,7 +1232,7 @@ int sqlite3ValueFromExpr(
|
| int nVal;
|
| assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
|
| assert( pExpr->u.zToken[1]=='\'' );
|
| - pVal = sqlite3ValueNew(db);
|
| + pVal = valueNew(db, pCtx);
|
| if( !pVal ) goto no_mem;
|
| zVal = &pExpr->u.zToken[2];
|
| nVal = sqlite3Strlen30(zVal)-1;
|
| @@ -1106,21 +1242,301 @@ int sqlite3ValueFromExpr(
|
| }
|
| #endif
|
|
|
| - if( pVal ){
|
| - sqlite3VdbeMemStoreType(pVal);
|
| - }
|
| *ppVal = pVal;
|
| - return SQLITE_OK;
|
| + return rc;
|
|
|
| no_mem:
|
| db->mallocFailed = 1;
|
| sqlite3DbFree(db, zVal);
|
| - sqlite3ValueFree(pVal);
|
| - *ppVal = 0;
|
| + assert( *ppVal==0 );
|
| +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
|
| + if( pCtx==0 ) sqlite3ValueFree(pVal);
|
| +#else
|
| + assert( pCtx==0 ); sqlite3ValueFree(pVal);
|
| +#endif
|
| return SQLITE_NOMEM;
|
| }
|
|
|
| /*
|
| +** Create a new sqlite3_value object, containing the value of pExpr.
|
| +**
|
| +** This only works for very simple expressions that consist of one constant
|
| +** token (i.e. "5", "5.1", "'a string'"). If the expression can
|
| +** be converted directly into a value, then the value is allocated and
|
| +** a pointer written to *ppVal. The caller is responsible for deallocating
|
| +** the value by passing it to sqlite3ValueFree() later on. If the expression
|
| +** cannot be converted to a value, then *ppVal is set to NULL.
|
| +*/
|
| +int sqlite3ValueFromExpr(
|
| + sqlite3 *db, /* The database connection */
|
| + Expr *pExpr, /* The expression to evaluate */
|
| + u8 enc, /* Encoding to use */
|
| + u8 affinity, /* Affinity to use */
|
| + sqlite3_value **ppVal /* Write the new value here */
|
| +){
|
| + return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0);
|
| +}
|
| +
|
| +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
|
| +/*
|
| +** The implementation of the sqlite_record() function. This function accepts
|
| +** a single argument of any type. The return value is a formatted database
|
| +** record (a blob) containing the argument value.
|
| +**
|
| +** This is used to convert the value stored in the 'sample' column of the
|
| +** sqlite_stat3 table to the record format SQLite uses internally.
|
| +*/
|
| +static void recordFunc(
|
| + sqlite3_context *context,
|
| + int argc,
|
| + sqlite3_value **argv
|
| +){
|
| + const int file_format = 1;
|
| + int iSerial; /* Serial type */
|
| + int nSerial; /* Bytes of space for iSerial as varint */
|
| + int nVal; /* Bytes of space required for argv[0] */
|
| + int nRet;
|
| + sqlite3 *db;
|
| + u8 *aRet;
|
| +
|
| + UNUSED_PARAMETER( argc );
|
| + iSerial = sqlite3VdbeSerialType(argv[0], file_format);
|
| + nSerial = sqlite3VarintLen(iSerial);
|
| + nVal = sqlite3VdbeSerialTypeLen(iSerial);
|
| + db = sqlite3_context_db_handle(context);
|
| +
|
| + nRet = 1 + nSerial + nVal;
|
| + aRet = sqlite3DbMallocRaw(db, nRet);
|
| + if( aRet==0 ){
|
| + sqlite3_result_error_nomem(context);
|
| + }else{
|
| + aRet[0] = nSerial+1;
|
| + putVarint32(&aRet[1], iSerial);
|
| + sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
|
| + sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
|
| + sqlite3DbFree(db, aRet);
|
| + }
|
| +}
|
| +
|
| +/*
|
| +** Register built-in functions used to help read ANALYZE data.
|
| +*/
|
| +void sqlite3AnalyzeFunctions(void){
|
| + static SQLITE_WSD FuncDef aAnalyzeTableFuncs[] = {
|
| + FUNCTION(sqlite_record, 1, 0, 0, recordFunc),
|
| + };
|
| + int i;
|
| + FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
|
| + FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
|
| + for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
|
| + sqlite3FuncDefInsert(pHash, &aFunc[i]);
|
| + }
|
| +}
|
| +
|
| +/*
|
| +** Attempt to extract a value from pExpr and use it to construct *ppVal.
|
| +**
|
| +** If pAlloc is not NULL, then an UnpackedRecord object is created for
|
| +** pAlloc if one does not exist and the new value is added to the
|
| +** UnpackedRecord object.
|
| +**
|
| +** A value is extracted in the following cases:
|
| +**
|
| +** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
|
| +**
|
| +** * The expression is a bound variable, and this is a reprepare, or
|
| +**
|
| +** * The expression is a literal value.
|
| +**
|
| +** On success, *ppVal is made to point to the extracted value. The caller
|
| +** is responsible for ensuring that the value is eventually freed.
|
| +*/
|
| +static int stat4ValueFromExpr(
|
| + Parse *pParse, /* Parse context */
|
| + Expr *pExpr, /* The expression to extract a value from */
|
| + u8 affinity, /* Affinity to use */
|
| + struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */
|
| + sqlite3_value **ppVal /* OUT: New value object (or NULL) */
|
| +){
|
| + int rc = SQLITE_OK;
|
| + sqlite3_value *pVal = 0;
|
| + sqlite3 *db = pParse->db;
|
| +
|
| + /* Skip over any TK_COLLATE nodes */
|
| + pExpr = sqlite3ExprSkipCollate(pExpr);
|
| +
|
| + if( !pExpr ){
|
| + pVal = valueNew(db, pAlloc);
|
| + if( pVal ){
|
| + sqlite3VdbeMemSetNull((Mem*)pVal);
|
| + }
|
| + }else if( pExpr->op==TK_VARIABLE
|
| + || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
|
| + ){
|
| + Vdbe *v;
|
| + int iBindVar = pExpr->iColumn;
|
| + sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
|
| + if( (v = pParse->pReprepare)!=0 ){
|
| + pVal = valueNew(db, pAlloc);
|
| + if( pVal ){
|
| + rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
|
| + if( rc==SQLITE_OK ){
|
| + sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
|
| + }
|
| + pVal->db = pParse->db;
|
| + }
|
| + }
|
| + }else{
|
| + rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc);
|
| + }
|
| +
|
| + assert( pVal==0 || pVal->db==db );
|
| + *ppVal = pVal;
|
| + return rc;
|
| +}
|
| +
|
| +/*
|
| +** This function is used to allocate and populate UnpackedRecord
|
| +** structures intended to be compared against sample index keys stored
|
| +** in the sqlite_stat4 table.
|
| +**
|
| +** A single call to this function attempts to populates field iVal (leftmost
|
| +** is 0 etc.) of the unpacked record with a value extracted from expression
|
| +** pExpr. Extraction of values is possible if:
|
| +**
|
| +** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
|
| +**
|
| +** * The expression is a bound variable, and this is a reprepare, or
|
| +**
|
| +** * The sqlite3ValueFromExpr() function is able to extract a value
|
| +** from the expression (i.e. the expression is a literal value).
|
| +**
|
| +** If a value can be extracted, the affinity passed as the 5th argument
|
| +** is applied to it before it is copied into the UnpackedRecord. Output
|
| +** parameter *pbOk is set to true if a value is extracted, or false
|
| +** otherwise.
|
| +**
|
| +** When this function is called, *ppRec must either point to an object
|
| +** allocated by an earlier call to this function, or must be NULL. If it
|
| +** is NULL and a value can be successfully extracted, a new UnpackedRecord
|
| +** is allocated (and *ppRec set to point to it) before returning.
|
| +**
|
| +** Unless an error is encountered, SQLITE_OK is returned. It is not an
|
| +** error if a value cannot be extracted from pExpr. If an error does
|
| +** occur, an SQLite error code is returned.
|
| +*/
|
| +int sqlite3Stat4ProbeSetValue(
|
| + Parse *pParse, /* Parse context */
|
| + Index *pIdx, /* Index being probed */
|
| + UnpackedRecord **ppRec, /* IN/OUT: Probe record */
|
| + Expr *pExpr, /* The expression to extract a value from */
|
| + u8 affinity, /* Affinity to use */
|
| + int iVal, /* Array element to populate */
|
| + int *pbOk /* OUT: True if value was extracted */
|
| +){
|
| + int rc;
|
| + sqlite3_value *pVal = 0;
|
| + struct ValueNewStat4Ctx alloc;
|
| +
|
| + alloc.pParse = pParse;
|
| + alloc.pIdx = pIdx;
|
| + alloc.ppRec = ppRec;
|
| + alloc.iVal = iVal;
|
| +
|
| + rc = stat4ValueFromExpr(pParse, pExpr, affinity, &alloc, &pVal);
|
| + assert( pVal==0 || pVal->db==pParse->db );
|
| + *pbOk = (pVal!=0);
|
| + return rc;
|
| +}
|
| +
|
| +/*
|
| +** Attempt to extract a value from expression pExpr using the methods
|
| +** as described for sqlite3Stat4ProbeSetValue() above.
|
| +**
|
| +** If successful, set *ppVal to point to a new value object and return
|
| +** SQLITE_OK. If no value can be extracted, but no other error occurs
|
| +** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
|
| +** does occur, return an SQLite error code. The final value of *ppVal
|
| +** is undefined in this case.
|
| +*/
|
| +int sqlite3Stat4ValueFromExpr(
|
| + Parse *pParse, /* Parse context */
|
| + Expr *pExpr, /* The expression to extract a value from */
|
| + u8 affinity, /* Affinity to use */
|
| + sqlite3_value **ppVal /* OUT: New value object (or NULL) */
|
| +){
|
| + return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
|
| +}
|
| +
|
| +/*
|
| +** Extract the iCol-th column from the nRec-byte record in pRec. Write
|
| +** the column value into *ppVal. If *ppVal is initially NULL then a new
|
| +** sqlite3_value object is allocated.
|
| +**
|
| +** If *ppVal is initially NULL then the caller is responsible for
|
| +** ensuring that the value written into *ppVal is eventually freed.
|
| +*/
|
| +int sqlite3Stat4Column(
|
| + sqlite3 *db, /* Database handle */
|
| + const void *pRec, /* Pointer to buffer containing record */
|
| + int nRec, /* Size of buffer pRec in bytes */
|
| + int iCol, /* Column to extract */
|
| + sqlite3_value **ppVal /* OUT: Extracted value */
|
| +){
|
| + u32 t; /* a column type code */
|
| + int nHdr; /* Size of the header in the record */
|
| + int iHdr; /* Next unread header byte */
|
| + int iField; /* Next unread data byte */
|
| + int szField; /* Size of the current data field */
|
| + int i; /* Column index */
|
| + u8 *a = (u8*)pRec; /* Typecast byte array */
|
| + Mem *pMem = *ppVal; /* Write result into this Mem object */
|
| +
|
| + assert( iCol>0 );
|
| + iHdr = getVarint32(a, nHdr);
|
| + if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
|
| + iField = nHdr;
|
| + for(i=0; i<=iCol; i++){
|
| + iHdr += getVarint32(&a[iHdr], t);
|
| + testcase( iHdr==nHdr );
|
| + testcase( iHdr==nHdr+1 );
|
| + if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT;
|
| + szField = sqlite3VdbeSerialTypeLen(t);
|
| + iField += szField;
|
| + }
|
| + testcase( iField==nRec );
|
| + testcase( iField==nRec+1 );
|
| + if( iField>nRec ) return SQLITE_CORRUPT_BKPT;
|
| + if( pMem==0 ){
|
| + pMem = *ppVal = sqlite3ValueNew(db);
|
| + if( pMem==0 ) return SQLITE_NOMEM;
|
| + }
|
| + sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
|
| + pMem->enc = ENC(db);
|
| + return SQLITE_OK;
|
| +}
|
| +
|
| +/*
|
| +** Unless it is NULL, the argument must be an UnpackedRecord object returned
|
| +** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
|
| +** the object.
|
| +*/
|
| +void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
|
| + if( pRec ){
|
| + int i;
|
| + int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
|
| + Mem *aMem = pRec->aMem;
|
| + sqlite3 *db = aMem[0].db;
|
| + for(i=0; i<nCol; i++){
|
| + if( aMem[i].szMalloc ) sqlite3DbFree(db, aMem[i].zMalloc);
|
| + }
|
| + sqlite3KeyInfoUnref(pRec->pKeyInfo);
|
| + sqlite3DbFree(db, pRec);
|
| + }
|
| +}
|
| +#endif /* ifdef SQLITE_ENABLE_STAT4 */
|
| +
|
| +/*
|
| ** Change the string value of an sqlite3_value object
|
| */
|
| void sqlite3ValueSetStr(
|
|
|