| | @@ -73,12 +73,45 @@ |
| 73 | 73 | # define _FILE_OFFSET_BITS 64 |
| 74 | 74 | # endif |
| 75 | 75 | # define _LARGEFILE_SOURCE 1 |
| 76 | 76 | #endif |
| 77 | 77 | |
| 78 | +/* |
| 79 | +** For MinGW, check to see if we can include the header file containing its |
| 80 | +** version information, among other things. Normally, this internal MinGW |
| 81 | +** header file would [only] be included automatically by other MinGW header |
| 82 | +** files; however, the contained version information is now required by this |
| 83 | +** header file to work around binary compatibility issues (see below) and |
| 84 | +** this is the only known way to reliably obtain it. This entire #if block |
| 85 | +** would be completely unnecessary if there was any other way of detecting |
| 86 | +** MinGW via their preprocessor (e.g. if they customized their GCC to define |
| 87 | +** some MinGW-specific macros). When compiling for MinGW, either the |
| 88 | +** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be |
| 89 | +** defined; otherwise, detection of conditions specific to MinGW will be |
| 90 | +** disabled. |
| 91 | +*/ |
| 92 | +#if defined(_HAVE_MINGW_H) |
| 93 | +# include "mingw.h" |
| 94 | +#elif defined(_HAVE__MINGW_H) |
| 95 | +# include "_mingw.h" |
| 96 | +#endif |
| 97 | + |
| 98 | +/* |
| 99 | +** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T |
| 100 | +** define is required to maintain binary compatibility with the MSVC runtime |
| 101 | +** library in use (e.g. for Windows XP). |
| 102 | +*/ |
| 103 | +#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \ |
| 104 | + defined(_WIN32) && !defined(_WIN64) && \ |
| 105 | + defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \ |
| 106 | + defined(__MSVCRT__) |
| 107 | +# define _USE_32BIT_TIME_T |
| 108 | +#endif |
| 109 | + |
| 78 | 110 | /* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear |
| 79 | | -** first in QNX. |
| 111 | +** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for |
| 112 | +** MinGW. |
| 80 | 113 | */ |
| 81 | 114 | /************** Include sqlite3.h in the middle of sqliteInt.h ***************/ |
| 82 | 115 | /************** Begin file sqlite3.h *****************************************/ |
| 83 | 116 | /* |
| 84 | 117 | ** 2001 September 15 |
| | @@ -189,11 +222,11 @@ |
| 189 | 222 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 190 | 223 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 191 | 224 | */ |
| 192 | 225 | #define SQLITE_VERSION "3.8.4" |
| 193 | 226 | #define SQLITE_VERSION_NUMBER 3008004 |
| 194 | | -#define SQLITE_SOURCE_ID "2014-02-27 15:04:13 a6690400235705ecc0d1a60dacff6ad5fb1f944a" |
| 227 | +#define SQLITE_SOURCE_ID "2014-03-05 19:04:46 0723effc9ccae7c660fb847b36ce9324e0cb5042" |
| 195 | 228 | |
| 196 | 229 | /* |
| 197 | 230 | ** CAPI3REF: Run-Time Library Version Numbers |
| 198 | 231 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 199 | 232 | ** |
| | @@ -9334,12 +9367,15 @@ |
| 9334 | 9367 | #ifndef SQLITE_OMIT_TRACE |
| 9335 | 9368 | SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*); |
| 9336 | 9369 | #endif |
| 9337 | 9370 | |
| 9338 | 9371 | SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*); |
| 9339 | | -SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*); |
| 9372 | +SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,const UnpackedRecord*,int); |
| 9340 | 9373 | SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **); |
| 9374 | + |
| 9375 | +typedef int (*RecordCompare)(int,const void*,const UnpackedRecord*,int); |
| 9376 | +SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*); |
| 9341 | 9377 | |
| 9342 | 9378 | #ifndef SQLITE_OMIT_TRIGGER |
| 9343 | 9379 | SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *); |
| 9344 | 9380 | #endif |
| 9345 | 9381 | |
| | @@ -10950,23 +10986,23 @@ |
| 10950 | 10986 | ** the OP_MakeRecord opcode of the VDBE and is disassembled by the |
| 10951 | 10987 | ** OP_Column opcode. |
| 10952 | 10988 | ** |
| 10953 | 10989 | ** This structure holds a record that has already been disassembled |
| 10954 | 10990 | ** into its constituent fields. |
| 10991 | +** |
| 10992 | +** The r1 and r2 member variables are only used by the optimized comparison |
| 10993 | +** functions vdbeRecordCompareInt() and vdbeRecordCompareString(). |
| 10955 | 10994 | */ |
| 10956 | 10995 | struct UnpackedRecord { |
| 10957 | 10996 | KeyInfo *pKeyInfo; /* Collation and sort-order information */ |
| 10958 | 10997 | u16 nField; /* Number of entries in apMem[] */ |
| 10959 | | - u8 flags; /* Boolean settings. UNPACKED_... below */ |
| 10998 | + i8 default_rc; /* Comparison result if keys are equal */ |
| 10960 | 10999 | Mem *aMem; /* Values */ |
| 11000 | + int r1; /* Value to return if (lhs > rhs) */ |
| 11001 | + int r2; /* Value to return if (rhs < lhs) */ |
| 10961 | 11002 | }; |
| 10962 | 11003 | |
| 10963 | | -/* |
| 10964 | | -** Allowed values of UnpackedRecord.flags |
| 10965 | | -*/ |
| 10966 | | -#define UNPACKED_INCRKEY 0x01 /* Make this key an epsilon larger */ |
| 10967 | | -#define UNPACKED_PREFIX_MATCH 0x02 /* A prefix match is considered OK */ |
| 10968 | 11004 | |
| 10969 | 11005 | /* |
| 10970 | 11006 | ** Each SQL index is represented in memory by an |
| 10971 | 11007 | ** instance of the following structure. |
| 10972 | 11008 | ** |
| | @@ -11382,12 +11418,12 @@ |
| 11382 | 11418 | ** |
| 11383 | 11419 | ** In the colUsed field, the high-order bit (bit 63) is set if the table |
| 11384 | 11420 | ** contains more than 63 columns and the 64-th or later column is used. |
| 11385 | 11421 | */ |
| 11386 | 11422 | struct SrcList { |
| 11387 | | - u8 nSrc; /* Number of tables or subqueries in the FROM clause */ |
| 11388 | | - u8 nAlloc; /* Number of entries allocated in a[] below */ |
| 11423 | + int nSrc; /* Number of tables or subqueries in the FROM clause */ |
| 11424 | + u32 nAlloc; /* Number of entries allocated in a[] below */ |
| 11389 | 11425 | struct SrcList_item { |
| 11390 | 11426 | Schema *pSchema; /* Schema to which this item is fixed */ |
| 11391 | 11427 | char *zDatabase; /* Name of database holding this table */ |
| 11392 | 11428 | char *zName; /* Name of the table */ |
| 11393 | 11429 | char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */ |
| | @@ -13415,10 +13451,13 @@ |
| 13415 | 13451 | #ifdef SQLITE_OMIT_COMPLETE |
| 13416 | 13452 | "OMIT_COMPLETE", |
| 13417 | 13453 | #endif |
| 13418 | 13454 | #ifdef SQLITE_OMIT_COMPOUND_SELECT |
| 13419 | 13455 | "OMIT_COMPOUND_SELECT", |
| 13456 | +#endif |
| 13457 | +#ifdef SQLITE_OMIT_CTE |
| 13458 | + "OMIT_CTE", |
| 13420 | 13459 | #endif |
| 13421 | 13460 | #ifdef SQLITE_OMIT_DATETIME_FUNCS |
| 13422 | 13461 | "OMIT_DATETIME_FUNCS", |
| 13423 | 13462 | #endif |
| 13424 | 13463 | #ifdef SQLITE_OMIT_DECLTYPE |
| | @@ -13800,11 +13839,10 @@ |
| 13800 | 13839 | RowSet *pRowSet; /* Used only when flags==MEM_RowSet */ |
| 13801 | 13840 | VdbeFrame *pFrame; /* Used when flags==MEM_Frame */ |
| 13802 | 13841 | } u; |
| 13803 | 13842 | int n; /* Number of characters in string value, excluding '\0' */ |
| 13804 | 13843 | u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */ |
| 13805 | | - u8 memType; /* Lower 5 bits of flags */ |
| 13806 | 13844 | u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */ |
| 13807 | 13845 | #ifdef SQLITE_DEBUG |
| 13808 | 13846 | Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */ |
| 13809 | 13847 | void *pFiller; /* So that sizeof(Mem) is a multiple of 8 */ |
| 13810 | 13848 | #endif |
| | @@ -13827,10 +13865,11 @@ |
| 13827 | 13865 | #define MEM_Null 0x0001 /* Value is NULL */ |
| 13828 | 13866 | #define MEM_Str 0x0002 /* Value is a string */ |
| 13829 | 13867 | #define MEM_Int 0x0004 /* Value is an integer */ |
| 13830 | 13868 | #define MEM_Real 0x0008 /* Value is a real number */ |
| 13831 | 13869 | #define MEM_Blob 0x0010 /* Value is a BLOB */ |
| 13870 | +#define MEM_AffMask 0x001f /* Mask of affinity bits */ |
| 13832 | 13871 | #define MEM_RowSet 0x0020 /* Value is a RowSet object */ |
| 13833 | 13872 | #define MEM_Frame 0x0040 /* Value is a VdbeFrame object */ |
| 13834 | 13873 | #define MEM_Undefined 0x0080 /* Value is undefined */ |
| 13835 | 13874 | #define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */ |
| 13836 | 13875 | #define MEM_TypeMask 0x01ff /* Mask of type bits */ |
| | @@ -13840,11 +13879,11 @@ |
| 13840 | 13879 | ** the following flags must be set to determine the memory management |
| 13841 | 13880 | ** policy for Mem.z. The MEM_Term flag tells us whether or not the |
| 13842 | 13881 | ** string is \000 or \u0000 terminated |
| 13843 | 13882 | */ |
| 13844 | 13883 | #define MEM_Term 0x0200 /* String rep is nul terminated */ |
| 13845 | | -#define MEM_Dyn 0x0400 /* Need to call sqliteFree() on Mem.z */ |
| 13884 | +#define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */ |
| 13846 | 13885 | #define MEM_Static 0x0800 /* Mem.z points to a static string */ |
| 13847 | 13886 | #define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */ |
| 13848 | 13887 | #define MEM_Agg 0x2000 /* Mem.z points to an agg function context */ |
| 13849 | 13888 | #define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */ |
| 13850 | 13889 | #ifdef SQLITE_OMIT_INCRBLOB |
| | @@ -14023,11 +14062,11 @@ |
| 14023 | 14062 | SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32); |
| 14024 | 14063 | SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*); |
| 14025 | 14064 | SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int); |
| 14026 | 14065 | |
| 14027 | 14066 | int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *); |
| 14028 | | -SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*); |
| 14067 | +SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,const UnpackedRecord*,int*); |
| 14029 | 14068 | SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *); |
| 14030 | 14069 | SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*); |
| 14031 | 14070 | SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*); |
| 14032 | 14071 | SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*); |
| 14033 | 14072 | SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*); |
| | @@ -14066,12 +14105,10 @@ |
| 14066 | 14105 | SQLITE_PRIVATE const char *sqlite3OpcodeName(int); |
| 14067 | 14106 | SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); |
| 14068 | 14107 | SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int); |
| 14069 | 14108 | SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); |
| 14070 | 14109 | SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *); |
| 14071 | | -#define sqlite3VdbeMemStoreType(X) (X)->memType = (u8)((X)->flags&0x1f) |
| 14072 | | -/* void sqlite3VdbeMemStoreType(Mem *pMem); */ |
| 14073 | 14110 | SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p); |
| 14074 | 14111 | |
| 14075 | 14112 | SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *); |
| 14076 | 14113 | SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *); |
| 14077 | 14114 | SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *); |
| | @@ -14088,10 +14125,11 @@ |
| 14088 | 14125 | # define sqlite3VdbeLeave(X) |
| 14089 | 14126 | #endif |
| 14090 | 14127 | |
| 14091 | 14128 | #ifdef SQLITE_DEBUG |
| 14092 | 14129 | SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*); |
| 14130 | +SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*); |
| 14093 | 14131 | #endif |
| 14094 | 14132 | |
| 14095 | 14133 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 14096 | 14134 | SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int); |
| 14097 | 14135 | #else |
| | @@ -21471,11 +21509,11 @@ |
| 21471 | 21509 | assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len ); |
| 21472 | 21510 | |
| 21473 | 21511 | sqlite3VdbeMemRelease(pMem); |
| 21474 | 21512 | pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem); |
| 21475 | 21513 | pMem->enc = desiredEnc; |
| 21476 | | - pMem->flags |= (MEM_Term|MEM_Dyn); |
| 21514 | + pMem->flags |= (MEM_Term); |
| 21477 | 21515 | pMem->z = (char*)zOut; |
| 21478 | 21516 | pMem->zMalloc = pMem->z; |
| 21479 | 21517 | |
| 21480 | 21518 | translate_out: |
| 21481 | 21519 | #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG) |
| | @@ -21599,11 +21637,10 @@ |
| 21599 | 21637 | sqlite3VdbeMemRelease(&m); |
| 21600 | 21638 | m.z = 0; |
| 21601 | 21639 | } |
| 21602 | 21640 | assert( (m.flags & MEM_Term)!=0 || db->mallocFailed ); |
| 21603 | 21641 | assert( (m.flags & MEM_Str)!=0 || db->mallocFailed ); |
| 21604 | | - assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed ); |
| 21605 | 21642 | assert( m.z || db->mallocFailed ); |
| 21606 | 21643 | return m.z; |
| 21607 | 21644 | } |
| 21608 | 21645 | |
| 21609 | 21646 | /* |
| | @@ -55305,10 +55342,11 @@ |
| 55305 | 55342 | i64 intKey, /* The table key */ |
| 55306 | 55343 | int biasRight, /* If true, bias the search to the high end */ |
| 55307 | 55344 | int *pRes /* Write search results here */ |
| 55308 | 55345 | ){ |
| 55309 | 55346 | int rc; |
| 55347 | + RecordCompare xRecordCompare; |
| 55310 | 55348 | |
| 55311 | 55349 | assert( cursorHoldsMutex(pCur) ); |
| 55312 | 55350 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 55313 | 55351 | assert( pRes ); |
| 55314 | 55352 | assert( (pIdxKey==0)==(pCur->pKeyInfo==0) ); |
| | @@ -55325,10 +55363,20 @@ |
| 55325 | 55363 | if( pCur->atLast && pCur->info.nKey<intKey ){ |
| 55326 | 55364 | *pRes = -1; |
| 55327 | 55365 | return SQLITE_OK; |
| 55328 | 55366 | } |
| 55329 | 55367 | } |
| 55368 | + |
| 55369 | + if( pIdxKey ){ |
| 55370 | + xRecordCompare = sqlite3VdbeFindCompare(pIdxKey); |
| 55371 | + assert( pIdxKey->default_rc==1 |
| 55372 | + || pIdxKey->default_rc==0 |
| 55373 | + || pIdxKey->default_rc==-1 |
| 55374 | + ); |
| 55375 | + }else{ |
| 55376 | + xRecordCompare = 0; /* Not actually used. Avoids a compiler warning. */ |
| 55377 | + } |
| 55330 | 55378 | |
| 55331 | 55379 | rc = moveToRoot(pCur); |
| 55332 | 55380 | if( rc ){ |
| 55333 | 55381 | return rc; |
| 55334 | 55382 | } |
| | @@ -55358,11 +55406,11 @@ |
| 55358 | 55406 | lwr = 0; |
| 55359 | 55407 | upr = pPage->nCell-1; |
| 55360 | 55408 | assert( biasRight==0 || biasRight==1 ); |
| 55361 | 55409 | idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */ |
| 55362 | 55410 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
| 55363 | | - if( pPage->intKey ){ |
| 55411 | + if( xRecordCompare==0 ){ |
| 55364 | 55412 | for(;;){ |
| 55365 | 55413 | i64 nCellKey; |
| 55366 | 55414 | pCell = findCell(pPage, idx) + pPage->childPtrSize; |
| 55367 | 55415 | if( pPage->hasData ){ |
| 55368 | 55416 | while( 0x80 <= *(pCell++) ){ |
| | @@ -55410,18 +55458,18 @@ |
| 55410 | 55458 | if( nCell<=pPage->max1bytePayload ){ |
| 55411 | 55459 | /* This branch runs if the record-size field of the cell is a |
| 55412 | 55460 | ** single byte varint and the record fits entirely on the main |
| 55413 | 55461 | ** b-tree page. */ |
| 55414 | 55462 | testcase( pCell+nCell+1==pPage->aDataEnd ); |
| 55415 | | - c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey); |
| 55463 | + c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey, 0); |
| 55416 | 55464 | }else if( !(pCell[1] & 0x80) |
| 55417 | 55465 | && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal |
| 55418 | 55466 | ){ |
| 55419 | 55467 | /* The record-size field is a 2 byte varint and the record |
| 55420 | 55468 | ** fits entirely on the main b-tree page. */ |
| 55421 | 55469 | testcase( pCell+nCell+2==pPage->aDataEnd ); |
| 55422 | | - c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey); |
| 55470 | + c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey, 0); |
| 55423 | 55471 | }else{ |
| 55424 | 55472 | /* The record flows over onto one or more overflow pages. In |
| 55425 | 55473 | ** this case the whole cell needs to be parsed, a buffer allocated |
| 55426 | 55474 | ** and accessPayload() used to retrieve the record into the |
| 55427 | 55475 | ** buffer before VdbeRecordCompare() can be called. */ |
| | @@ -55438,11 +55486,11 @@ |
| 55438 | 55486 | rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0); |
| 55439 | 55487 | if( rc ){ |
| 55440 | 55488 | sqlite3_free(pCellKey); |
| 55441 | 55489 | goto moveto_finish; |
| 55442 | 55490 | } |
| 55443 | | - c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey); |
| 55491 | + c = xRecordCompare(nCell, pCellKey, pIdxKey, 0); |
| 55444 | 55492 | sqlite3_free(pCellKey); |
| 55445 | 55493 | } |
| 55446 | 55494 | if( c<0 ){ |
| 55447 | 55495 | lwr = idx+1; |
| 55448 | 55496 | }else if( c>0 ){ |
| | @@ -60001,10 +60049,46 @@ |
| 60001 | 60049 | ** This file contains code use to manipulate "Mem" structure. A "Mem" |
| 60002 | 60050 | ** stores a single value in the VDBE. Mem is an opaque structure visible |
| 60003 | 60051 | ** only within the VDBE. Interface routines refer to a Mem using the |
| 60004 | 60052 | ** name sqlite_value |
| 60005 | 60053 | */ |
| 60054 | + |
| 60055 | +#ifdef SQLITE_DEBUG |
| 60056 | +/* |
| 60057 | +** Check invariants on a Mem object. |
| 60058 | +** |
| 60059 | +** This routine is intended for use inside of assert() statements, like |
| 60060 | +** this: assert( sqlite3VdbeCheckMemInvariants(pMem) ); |
| 60061 | +*/ |
| 60062 | +SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){ |
| 60063 | + /* The MEM_Dyn bit is set if and only if Mem.xDel is a non-NULL destructor |
| 60064 | + ** function for Mem.z |
| 60065 | + */ |
| 60066 | + assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 ); |
| 60067 | + assert( (p->flags & MEM_Dyn)!=0 || p->xDel==0 ); |
| 60068 | + |
| 60069 | + /* If p holds a string or blob, the Mem.z must point to exactly |
| 60070 | + ** one of the following: |
| 60071 | + ** |
| 60072 | + ** (1) Memory in Mem.zMalloc and managed by the Mem object |
| 60073 | + ** (2) Memory to be freed using Mem.xDel |
| 60074 | + ** (3) An ephermal string or blob |
| 60075 | + ** (4) A static string or blob |
| 60076 | + */ |
| 60077 | + if( (p->flags & (MEM_Str|MEM_Blob)) && p->z!=0 ){ |
| 60078 | + assert( |
| 60079 | + ((p->z==p->zMalloc)? 1 : 0) + |
| 60080 | + ((p->flags&MEM_Dyn)!=0 ? 1 : 0) + |
| 60081 | + ((p->flags&MEM_Ephem)!=0 ? 1 : 0) + |
| 60082 | + ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1 |
| 60083 | + ); |
| 60084 | + } |
| 60085 | + |
| 60086 | + return 1; |
| 60087 | +} |
| 60088 | +#endif |
| 60089 | + |
| 60006 | 60090 | |
| 60007 | 60091 | /* |
| 60008 | 60092 | ** If pMem is an object with a valid string representation, this routine |
| 60009 | 60093 | ** ensures the internal encoding for the string representation is |
| 60010 | 60094 | ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE. |
| | @@ -60051,16 +60135,11 @@ |
| 60051 | 60135 | ** pMem->z into the new allocation. pMem must be either a string or |
| 60052 | 60136 | ** blob if bPreserve is true. If bPreserve is false, any prior content |
| 60053 | 60137 | ** in pMem->z is discarded. |
| 60054 | 60138 | */ |
| 60055 | 60139 | SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){ |
| 60056 | | - assert( 1 >= |
| 60057 | | - ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) + |
| 60058 | | - (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + |
| 60059 | | - ((pMem->flags&MEM_Ephem) ? 1 : 0) + |
| 60060 | | - ((pMem->flags&MEM_Static) ? 1 : 0) |
| 60061 | | - ); |
| 60140 | + assert( sqlite3VdbeCheckMemInvariants(pMem) ); |
| 60062 | 60141 | assert( (pMem->flags&MEM_RowSet)==0 ); |
| 60063 | 60142 | |
| 60064 | 60143 | /* If the bPreserve flag is set to true, then the memory cell must already |
| 60065 | 60144 | ** contain a valid string or blob value. */ |
| 60066 | 60145 | assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) ); |
| | @@ -60074,26 +60153,27 @@ |
| 60074 | 60153 | }else{ |
| 60075 | 60154 | sqlite3DbFree(pMem->db, pMem->zMalloc); |
| 60076 | 60155 | pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n); |
| 60077 | 60156 | } |
| 60078 | 60157 | if( pMem->zMalloc==0 ){ |
| 60079 | | - sqlite3VdbeMemRelease(pMem); |
| 60158 | + VdbeMemRelease(pMem); |
| 60159 | + pMem->z = 0; |
| 60080 | 60160 | pMem->flags = MEM_Null; |
| 60081 | 60161 | return SQLITE_NOMEM; |
| 60082 | 60162 | } |
| 60083 | 60163 | } |
| 60084 | 60164 | |
| 60085 | 60165 | if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){ |
| 60086 | 60166 | memcpy(pMem->zMalloc, pMem->z, pMem->n); |
| 60087 | 60167 | } |
| 60088 | | - if( (pMem->flags&MEM_Dyn)!=0 && pMem->xDel ){ |
| 60089 | | - assert( pMem->xDel!=SQLITE_DYNAMIC ); |
| 60168 | + if( (pMem->flags&MEM_Dyn)!=0 ){ |
| 60169 | + assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC ); |
| 60090 | 60170 | pMem->xDel((void *)(pMem->z)); |
| 60091 | 60171 | } |
| 60092 | 60172 | |
| 60093 | 60173 | pMem->z = pMem->zMalloc; |
| 60094 | | - pMem->flags &= ~(MEM_Ephem|MEM_Static); |
| 60174 | + pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static); |
| 60095 | 60175 | pMem->xDel = 0; |
| 60096 | 60176 | return SQLITE_OK; |
| 60097 | 60177 | } |
| 60098 | 60178 | |
| 60099 | 60179 | /* |
| | @@ -60258,13 +60338,13 @@ |
| 60258 | 60338 | assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) ); |
| 60259 | 60339 | if( p->flags&MEM_Agg ){ |
| 60260 | 60340 | sqlite3VdbeMemFinalize(p, p->u.pDef); |
| 60261 | 60341 | assert( (p->flags & MEM_Agg)==0 ); |
| 60262 | 60342 | sqlite3VdbeMemRelease(p); |
| 60263 | | - }else if( p->flags&MEM_Dyn && p->xDel ){ |
| 60343 | + }else if( p->flags&MEM_Dyn ){ |
| 60264 | 60344 | assert( (p->flags&MEM_RowSet)==0 ); |
| 60265 | | - assert( p->xDel!=SQLITE_DYNAMIC ); |
| 60345 | + assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 ); |
| 60266 | 60346 | p->xDel((void *)p->z); |
| 60267 | 60347 | p->xDel = 0; |
| 60268 | 60348 | }else if( p->flags&MEM_RowSet ){ |
| 60269 | 60349 | sqlite3RowSetClear(p->u.pRowSet); |
| 60270 | 60350 | }else if( p->flags&MEM_Frame ){ |
| | @@ -60273,13 +60353,14 @@ |
| 60273 | 60353 | } |
| 60274 | 60354 | |
| 60275 | 60355 | /* |
| 60276 | 60356 | ** Release any memory held by the Mem. This may leave the Mem in an |
| 60277 | 60357 | ** inconsistent state, for example with (Mem.z==0) and |
| 60278 | | -** (Mem.memType==MEM_Str). |
| 60358 | +** (Mem.flags==MEM_Str). |
| 60279 | 60359 | */ |
| 60280 | 60360 | SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){ |
| 60361 | + assert( sqlite3VdbeCheckMemInvariants(p) ); |
| 60281 | 60362 | VdbeMemRelease(p); |
| 60282 | 60363 | if( p->zMalloc ){ |
| 60283 | 60364 | sqlite3DbFree(p->db, p->zMalloc); |
| 60284 | 60365 | p->zMalloc = 0; |
| 60285 | 60366 | } |
| | @@ -60464,11 +60545,10 @@ |
| 60464 | 60545 | } |
| 60465 | 60546 | if( pMem->flags & MEM_RowSet ){ |
| 60466 | 60547 | sqlite3RowSetClear(pMem->u.pRowSet); |
| 60467 | 60548 | } |
| 60468 | 60549 | MemSetTypeFlag(pMem, MEM_Null); |
| 60469 | | - pMem->memType = MEM_Null; |
| 60470 | 60550 | } |
| 60471 | 60551 | SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){ |
| 60472 | 60552 | sqlite3VdbeMemSetNull((Mem*)p); |
| 60473 | 60553 | } |
| 60474 | 60554 | |
| | @@ -60477,11 +60557,10 @@ |
| 60477 | 60557 | ** n containing all zeros. |
| 60478 | 60558 | */ |
| 60479 | 60559 | SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){ |
| 60480 | 60560 | sqlite3VdbeMemRelease(pMem); |
| 60481 | 60561 | pMem->flags = MEM_Blob|MEM_Zero; |
| 60482 | | - pMem->memType = MEM_Blob; |
| 60483 | 60562 | pMem->n = 0; |
| 60484 | 60563 | if( n<0 ) n = 0; |
| 60485 | 60564 | pMem->u.nZero = n; |
| 60486 | 60565 | pMem->enc = SQLITE_UTF8; |
| 60487 | 60566 | |
| | @@ -60500,11 +60579,10 @@ |
| 60500 | 60579 | */ |
| 60501 | 60580 | SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){ |
| 60502 | 60581 | sqlite3VdbeMemRelease(pMem); |
| 60503 | 60582 | pMem->u.i = val; |
| 60504 | 60583 | pMem->flags = MEM_Int; |
| 60505 | | - pMem->memType = MEM_Int; |
| 60506 | 60584 | } |
| 60507 | 60585 | |
| 60508 | 60586 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 60509 | 60587 | /* |
| 60510 | 60588 | ** Delete any previous value and set the value stored in *pMem to val, |
| | @@ -60515,11 +60593,10 @@ |
| 60515 | 60593 | sqlite3VdbeMemSetNull(pMem); |
| 60516 | 60594 | }else{ |
| 60517 | 60595 | sqlite3VdbeMemRelease(pMem); |
| 60518 | 60596 | pMem->r = val; |
| 60519 | 60597 | pMem->flags = MEM_Real; |
| 60520 | | - pMem->memType = MEM_Real; |
| 60521 | 60598 | } |
| 60522 | 60599 | } |
| 60523 | 60600 | #endif |
| 60524 | 60601 | |
| 60525 | 60602 | /* |
| | @@ -60613,10 +60690,11 @@ |
| 60613 | 60690 | |
| 60614 | 60691 | assert( (pFrom->flags & MEM_RowSet)==0 ); |
| 60615 | 60692 | VdbeMemRelease(pTo); |
| 60616 | 60693 | memcpy(pTo, pFrom, MEMCELLSIZE); |
| 60617 | 60694 | pTo->flags &= ~MEM_Dyn; |
| 60695 | + pTo->xDel = 0; |
| 60618 | 60696 | |
| 60619 | 60697 | if( pTo->flags&(MEM_Str|MEM_Blob) ){ |
| 60620 | 60698 | if( 0==(pFrom->flags&MEM_Static) ){ |
| 60621 | 60699 | pTo->flags |= MEM_Ephem; |
| 60622 | 60700 | rc = sqlite3VdbeMemMakeWriteable(pTo); |
| | @@ -60723,11 +60801,10 @@ |
| 60723 | 60801 | } |
| 60724 | 60802 | |
| 60725 | 60803 | pMem->n = nByte; |
| 60726 | 60804 | pMem->flags = flags; |
| 60727 | 60805 | pMem->enc = (enc==0 ? SQLITE_UTF8 : enc); |
| 60728 | | - pMem->memType = flags&0x1f; |
| 60729 | 60806 | |
| 60730 | 60807 | #ifndef SQLITE_OMIT_UTF16 |
| 60731 | 60808 | if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){ |
| 60732 | 60809 | return SQLITE_NOMEM; |
| 60733 | 60810 | } |
| | @@ -60738,123 +60815,10 @@ |
| 60738 | 60815 | } |
| 60739 | 60816 | |
| 60740 | 60817 | return SQLITE_OK; |
| 60741 | 60818 | } |
| 60742 | 60819 | |
| 60743 | | -/* |
| 60744 | | -** Compare the values contained by the two memory cells, returning |
| 60745 | | -** negative, zero or positive if pMem1 is less than, equal to, or greater |
| 60746 | | -** than pMem2. Sorting order is NULL's first, followed by numbers (integers |
| 60747 | | -** and reals) sorted numerically, followed by text ordered by the collating |
| 60748 | | -** sequence pColl and finally blob's ordered by memcmp(). |
| 60749 | | -** |
| 60750 | | -** Two NULL values are considered equal by this function. |
| 60751 | | -*/ |
| 60752 | | -SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ |
| 60753 | | - int rc; |
| 60754 | | - int f1, f2; |
| 60755 | | - int combined_flags; |
| 60756 | | - |
| 60757 | | - f1 = pMem1->flags; |
| 60758 | | - f2 = pMem2->flags; |
| 60759 | | - combined_flags = f1|f2; |
| 60760 | | - assert( (combined_flags & MEM_RowSet)==0 ); |
| 60761 | | - |
| 60762 | | - /* If one value is NULL, it is less than the other. If both values |
| 60763 | | - ** are NULL, return 0. |
| 60764 | | - */ |
| 60765 | | - if( combined_flags&MEM_Null ){ |
| 60766 | | - return (f2&MEM_Null) - (f1&MEM_Null); |
| 60767 | | - } |
| 60768 | | - |
| 60769 | | - /* If one value is a number and the other is not, the number is less. |
| 60770 | | - ** If both are numbers, compare as reals if one is a real, or as integers |
| 60771 | | - ** if both values are integers. |
| 60772 | | - */ |
| 60773 | | - if( combined_flags&(MEM_Int|MEM_Real) ){ |
| 60774 | | - double r1, r2; |
| 60775 | | - if( (f1 & f2 & MEM_Int)!=0 ){ |
| 60776 | | - if( pMem1->u.i < pMem2->u.i ) return -1; |
| 60777 | | - if( pMem1->u.i > pMem2->u.i ) return 1; |
| 60778 | | - return 0; |
| 60779 | | - } |
| 60780 | | - if( (f1&MEM_Real)!=0 ){ |
| 60781 | | - r1 = pMem1->r; |
| 60782 | | - }else if( (f1&MEM_Int)!=0 ){ |
| 60783 | | - r1 = (double)pMem1->u.i; |
| 60784 | | - }else{ |
| 60785 | | - return 1; |
| 60786 | | - } |
| 60787 | | - if( (f2&MEM_Real)!=0 ){ |
| 60788 | | - r2 = pMem2->r; |
| 60789 | | - }else if( (f2&MEM_Int)!=0 ){ |
| 60790 | | - r2 = (double)pMem2->u.i; |
| 60791 | | - }else{ |
| 60792 | | - return -1; |
| 60793 | | - } |
| 60794 | | - if( r1<r2 ) return -1; |
| 60795 | | - if( r1>r2 ) return 1; |
| 60796 | | - return 0; |
| 60797 | | - } |
| 60798 | | - |
| 60799 | | - /* If one value is a string and the other is a blob, the string is less. |
| 60800 | | - ** If both are strings, compare using the collating functions. |
| 60801 | | - */ |
| 60802 | | - if( combined_flags&MEM_Str ){ |
| 60803 | | - if( (f1 & MEM_Str)==0 ){ |
| 60804 | | - return 1; |
| 60805 | | - } |
| 60806 | | - if( (f2 & MEM_Str)==0 ){ |
| 60807 | | - return -1; |
| 60808 | | - } |
| 60809 | | - |
| 60810 | | - assert( pMem1->enc==pMem2->enc ); |
| 60811 | | - assert( pMem1->enc==SQLITE_UTF8 || |
| 60812 | | - pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); |
| 60813 | | - |
| 60814 | | - /* The collation sequence must be defined at this point, even if |
| 60815 | | - ** the user deletes the collation sequence after the vdbe program is |
| 60816 | | - ** compiled (this was not always the case). |
| 60817 | | - */ |
| 60818 | | - assert( !pColl || pColl->xCmp ); |
| 60819 | | - |
| 60820 | | - if( pColl ){ |
| 60821 | | - if( pMem1->enc==pColl->enc ){ |
| 60822 | | - /* The strings are already in the correct encoding. Call the |
| 60823 | | - ** comparison function directly */ |
| 60824 | | - return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z); |
| 60825 | | - }else{ |
| 60826 | | - const void *v1, *v2; |
| 60827 | | - int n1, n2; |
| 60828 | | - Mem c1; |
| 60829 | | - Mem c2; |
| 60830 | | - memset(&c1, 0, sizeof(c1)); |
| 60831 | | - memset(&c2, 0, sizeof(c2)); |
| 60832 | | - sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); |
| 60833 | | - sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); |
| 60834 | | - v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); |
| 60835 | | - n1 = v1==0 ? 0 : c1.n; |
| 60836 | | - v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); |
| 60837 | | - n2 = v2==0 ? 0 : c2.n; |
| 60838 | | - rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); |
| 60839 | | - sqlite3VdbeMemRelease(&c1); |
| 60840 | | - sqlite3VdbeMemRelease(&c2); |
| 60841 | | - return rc; |
| 60842 | | - } |
| 60843 | | - } |
| 60844 | | - /* If a NULL pointer was passed as the collate function, fall through |
| 60845 | | - ** to the blob case and use memcmp(). */ |
| 60846 | | - } |
| 60847 | | - |
| 60848 | | - /* Both values must be blobs. Compare using memcmp(). */ |
| 60849 | | - rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n); |
| 60850 | | - if( rc==0 ){ |
| 60851 | | - rc = pMem1->n - pMem2->n; |
| 60852 | | - } |
| 60853 | | - return rc; |
| 60854 | | -} |
| 60855 | | - |
| 60856 | 60820 | /* |
| 60857 | 60821 | ** Move data out of a btree key or data field and into a Mem structure. |
| 60858 | 60822 | ** The data or key is taken from the entry that pCur is currently pointing |
| 60859 | 60823 | ** to. offset and amt determine what portion of the data or key to retrieve. |
| 60860 | 60824 | ** key is true to get the key or false to get data. The result is written |
| | @@ -60891,26 +60855,26 @@ |
| 60891 | 60855 | |
| 60892 | 60856 | if( offset+amt<=available ){ |
| 60893 | 60857 | sqlite3VdbeMemRelease(pMem); |
| 60894 | 60858 | pMem->z = &zData[offset]; |
| 60895 | 60859 | pMem->flags = MEM_Blob|MEM_Ephem; |
| 60860 | + pMem->n = (int)amt; |
| 60896 | 60861 | }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){ |
| 60897 | | - pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term; |
| 60898 | | - pMem->enc = 0; |
| 60899 | | - pMem->memType = MEM_Blob; |
| 60900 | 60862 | if( key ){ |
| 60901 | 60863 | rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z); |
| 60902 | 60864 | }else{ |
| 60903 | 60865 | rc = sqlite3BtreeData(pCur, offset, amt, pMem->z); |
| 60904 | 60866 | } |
| 60905 | | - pMem->z[amt] = 0; |
| 60906 | | - pMem->z[amt+1] = 0; |
| 60907 | | - if( rc!=SQLITE_OK ){ |
| 60867 | + if( rc==SQLITE_OK ){ |
| 60868 | + pMem->z[amt] = 0; |
| 60869 | + pMem->z[amt+1] = 0; |
| 60870 | + pMem->flags = MEM_Blob|MEM_Term; |
| 60871 | + pMem->n = (int)amt; |
| 60872 | + }else{ |
| 60908 | 60873 | sqlite3VdbeMemRelease(pMem); |
| 60909 | 60874 | } |
| 60910 | 60875 | } |
| 60911 | | - pMem->n = (int)amt; |
| 60912 | 60876 | |
| 60913 | 60877 | return rc; |
| 60914 | 60878 | } |
| 60915 | 60879 | |
| 60916 | 60880 | /* This function is only available internally, it is not part of the |
| | @@ -60964,11 +60928,10 @@ |
| 60964 | 60928 | */ |
| 60965 | 60929 | SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){ |
| 60966 | 60930 | Mem *p = sqlite3DbMallocZero(db, sizeof(*p)); |
| 60967 | 60931 | if( p ){ |
| 60968 | 60932 | p->flags = MEM_Null; |
| 60969 | | - p->memType = MEM_Null; |
| 60970 | 60933 | p->db = db; |
| 60971 | 60934 | } |
| 60972 | 60935 | return p; |
| 60973 | 60936 | } |
| 60974 | 60937 | |
| | @@ -61010,15 +60973,13 @@ |
| 61010 | 60973 | if( pRec ){ |
| 61011 | 60974 | pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx); |
| 61012 | 60975 | if( pRec->pKeyInfo ){ |
| 61013 | 60976 | assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol ); |
| 61014 | 60977 | assert( pRec->pKeyInfo->enc==ENC(db) ); |
| 61015 | | - pRec->flags = UNPACKED_PREFIX_MATCH; |
| 61016 | 60978 | pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord))); |
| 61017 | 60979 | for(i=0; i<nCol; i++){ |
| 61018 | 60980 | pRec->aMem[i].flags = MEM_Null; |
| 61019 | | - pRec->aMem[i].memType = MEM_Null; |
| 61020 | 60981 | pRec->aMem[i].db = db; |
| 61021 | 60982 | } |
| 61022 | 60983 | }else{ |
| 61023 | 60984 | sqlite3DbFree(db, pRec); |
| 61024 | 60985 | pRec = 0; |
| | @@ -61087,11 +61048,10 @@ |
| 61087 | 61048 | sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt); |
| 61088 | 61049 | }else{ |
| 61089 | 61050 | zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken); |
| 61090 | 61051 | if( zVal==0 ) goto no_mem; |
| 61091 | 61052 | sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC); |
| 61092 | | - if( op==TK_FLOAT ) pVal->memType = MEM_Real; |
| 61093 | 61053 | } |
| 61094 | 61054 | if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){ |
| 61095 | 61055 | sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8); |
| 61096 | 61056 | }else{ |
| 61097 | 61057 | sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8); |
| | @@ -61105,13 +61065,13 @@ |
| 61105 | 61065 | if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) |
| 61106 | 61066 | && pVal!=0 |
| 61107 | 61067 | ){ |
| 61108 | 61068 | sqlite3VdbeMemNumerify(pVal); |
| 61109 | 61069 | if( pVal->u.i==SMALLEST_INT64 ){ |
| 61110 | | - pVal->flags &= MEM_Int; |
| 61070 | + pVal->flags &= ~MEM_Int; |
| 61111 | 61071 | pVal->flags |= MEM_Real; |
| 61112 | | - pVal->r = (double)LARGEST_INT64; |
| 61072 | + pVal->r = (double)SMALLEST_INT64; |
| 61113 | 61073 | }else{ |
| 61114 | 61074 | pVal->u.i = -pVal->u.i; |
| 61115 | 61075 | } |
| 61116 | 61076 | pVal->r = -pVal->r; |
| 61117 | 61077 | sqlite3ValueApplyAffinity(pVal, affinity, enc); |
| | @@ -61133,13 +61093,10 @@ |
| 61133 | 61093 | sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2, |
| 61134 | 61094 | 0, SQLITE_DYNAMIC); |
| 61135 | 61095 | } |
| 61136 | 61096 | #endif |
| 61137 | 61097 | |
| 61138 | | - if( pVal ){ |
| 61139 | | - sqlite3VdbeMemStoreType(pVal); |
| 61140 | | - } |
| 61141 | 61098 | *ppVal = pVal; |
| 61142 | 61099 | return rc; |
| 61143 | 61100 | |
| 61144 | 61101 | no_mem: |
| 61145 | 61102 | db->mallocFailed = 1; |
| | @@ -61299,11 +61256,10 @@ |
| 61299 | 61256 | rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]); |
| 61300 | 61257 | if( rc==SQLITE_OK ){ |
| 61301 | 61258 | sqlite3ValueApplyAffinity(pVal, affinity, ENC(db)); |
| 61302 | 61259 | } |
| 61303 | 61260 | pVal->db = pParse->db; |
| 61304 | | - sqlite3VdbeMemStoreType((Mem*)pVal); |
| 61305 | 61261 | } |
| 61306 | 61262 | } |
| 61307 | 61263 | }else{ |
| 61308 | 61264 | rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc); |
| 61309 | 61265 | } |
| | @@ -62591,10 +62547,11 @@ |
| 62591 | 62547 | } |
| 62592 | 62548 | return; |
| 62593 | 62549 | } |
| 62594 | 62550 | for(pEnd=&p[N]; p<pEnd; p++){ |
| 62595 | 62551 | assert( (&p[1])==pEnd || p[0].db==p[1].db ); |
| 62552 | + assert( sqlite3VdbeCheckMemInvariants(p) ); |
| 62596 | 62553 | |
| 62597 | 62554 | /* This block is really an inlined version of sqlite3VdbeMemRelease() |
| 62598 | 62555 | ** that takes advantage of the fact that the memory cell value is |
| 62599 | 62556 | ** being set to NULL after releasing any dynamic resources. |
| 62600 | 62557 | ** |
| | @@ -62733,19 +62690,17 @@ |
| 62733 | 62690 | } |
| 62734 | 62691 | pOp = &apSub[j]->aOp[i]; |
| 62735 | 62692 | } |
| 62736 | 62693 | if( p->explain==1 ){ |
| 62737 | 62694 | pMem->flags = MEM_Int; |
| 62738 | | - pMem->memType = MEM_Int; |
| 62739 | 62695 | pMem->u.i = i; /* Program counter */ |
| 62740 | 62696 | pMem++; |
| 62741 | 62697 | |
| 62742 | 62698 | pMem->flags = MEM_Static|MEM_Str|MEM_Term; |
| 62743 | 62699 | pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ |
| 62744 | 62700 | assert( pMem->z!=0 ); |
| 62745 | 62701 | pMem->n = sqlite3Strlen30(pMem->z); |
| 62746 | | - pMem->memType = MEM_Str; |
| 62747 | 62702 | pMem->enc = SQLITE_UTF8; |
| 62748 | 62703 | pMem++; |
| 62749 | 62704 | |
| 62750 | 62705 | /* When an OP_Program opcode is encounter (the only opcode that has |
| 62751 | 62706 | ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms |
| | @@ -62767,63 +62722,56 @@ |
| 62767 | 62722 | } |
| 62768 | 62723 | } |
| 62769 | 62724 | |
| 62770 | 62725 | pMem->flags = MEM_Int; |
| 62771 | 62726 | pMem->u.i = pOp->p1; /* P1 */ |
| 62772 | | - pMem->memType = MEM_Int; |
| 62773 | 62727 | pMem++; |
| 62774 | 62728 | |
| 62775 | 62729 | pMem->flags = MEM_Int; |
| 62776 | 62730 | pMem->u.i = pOp->p2; /* P2 */ |
| 62777 | | - pMem->memType = MEM_Int; |
| 62778 | 62731 | pMem++; |
| 62779 | 62732 | |
| 62780 | 62733 | pMem->flags = MEM_Int; |
| 62781 | 62734 | pMem->u.i = pOp->p3; /* P3 */ |
| 62782 | | - pMem->memType = MEM_Int; |
| 62783 | 62735 | pMem++; |
| 62784 | 62736 | |
| 62785 | 62737 | if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */ |
| 62786 | 62738 | assert( p->db->mallocFailed ); |
| 62787 | 62739 | return SQLITE_ERROR; |
| 62788 | 62740 | } |
| 62789 | | - pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; |
| 62741 | + pMem->flags = MEM_Str|MEM_Term; |
| 62790 | 62742 | zP4 = displayP4(pOp, pMem->z, 32); |
| 62791 | 62743 | if( zP4!=pMem->z ){ |
| 62792 | 62744 | sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0); |
| 62793 | 62745 | }else{ |
| 62794 | 62746 | assert( pMem->z!=0 ); |
| 62795 | 62747 | pMem->n = sqlite3Strlen30(pMem->z); |
| 62796 | 62748 | pMem->enc = SQLITE_UTF8; |
| 62797 | 62749 | } |
| 62798 | | - pMem->memType = MEM_Str; |
| 62799 | 62750 | pMem++; |
| 62800 | 62751 | |
| 62801 | 62752 | if( p->explain==1 ){ |
| 62802 | 62753 | if( sqlite3VdbeMemGrow(pMem, 4, 0) ){ |
| 62803 | 62754 | assert( p->db->mallocFailed ); |
| 62804 | 62755 | return SQLITE_ERROR; |
| 62805 | 62756 | } |
| 62806 | | - pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; |
| 62757 | + pMem->flags = MEM_Str|MEM_Term; |
| 62807 | 62758 | pMem->n = 2; |
| 62808 | 62759 | sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ |
| 62809 | | - pMem->memType = MEM_Str; |
| 62810 | 62760 | pMem->enc = SQLITE_UTF8; |
| 62811 | 62761 | pMem++; |
| 62812 | 62762 | |
| 62813 | 62763 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 62814 | 62764 | if( sqlite3VdbeMemGrow(pMem, 500, 0) ){ |
| 62815 | 62765 | assert( p->db->mallocFailed ); |
| 62816 | 62766 | return SQLITE_ERROR; |
| 62817 | 62767 | } |
| 62818 | | - pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; |
| 62768 | + pMem->flags = MEM_Str|MEM_Term; |
| 62819 | 62769 | pMem->n = displayComment(pOp, zP4, pMem->z, 500); |
| 62820 | | - pMem->memType = MEM_Str; |
| 62821 | 62770 | pMem->enc = SQLITE_UTF8; |
| 62822 | 62771 | #else |
| 62823 | 62772 | pMem->flags = MEM_Null; /* Comment */ |
| 62824 | | - pMem->memType = MEM_Null; |
| 62825 | 62773 | #endif |
| 62826 | 62774 | } |
| 62827 | 62775 | |
| 62828 | 62776 | p->nResColumn = 8 - 4*(p->explain-1); |
| 62829 | 62777 | p->pResultSet = &p->aMem[1]; |
| | @@ -64312,10 +64260,18 @@ |
| 64312 | 64260 | |
| 64313 | 64261 | /* NULL or constants 0 or 1 */ |
| 64314 | 64262 | return 0; |
| 64315 | 64263 | } |
| 64316 | 64264 | |
| 64265 | +/* Input "x" is a sequence of unsigned characters that represent a |
| 64266 | +** big-endian integer. Return the equivalent native integer |
| 64267 | +*/ |
| 64268 | +#define ONE_BYTE_INT(x) ((i8)(x)[0]) |
| 64269 | +#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1]) |
| 64270 | +#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2]) |
| 64271 | +#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) |
| 64272 | + |
| 64317 | 64273 | /* |
| 64318 | 64274 | ** Deserialize the data blob pointed to by buf as serial type serial_type |
| 64319 | 64275 | ** and store the result in pMem. Return the number of bytes read. |
| 64320 | 64276 | */ |
| 64321 | 64277 | SQLITE_PRIVATE u32 sqlite3VdbeSerialGet( |
| | @@ -64323,46 +64279,40 @@ |
| 64323 | 64279 | u32 serial_type, /* Serial type to deserialize */ |
| 64324 | 64280 | Mem *pMem /* Memory cell to write value into */ |
| 64325 | 64281 | ){ |
| 64326 | 64282 | u64 x; |
| 64327 | 64283 | u32 y; |
| 64328 | | - int i; |
| 64329 | 64284 | switch( serial_type ){ |
| 64330 | 64285 | case 10: /* Reserved for future use */ |
| 64331 | 64286 | case 11: /* Reserved for future use */ |
| 64332 | 64287 | case 0: { /* NULL */ |
| 64333 | 64288 | pMem->flags = MEM_Null; |
| 64334 | 64289 | break; |
| 64335 | 64290 | } |
| 64336 | 64291 | case 1: { /* 1-byte signed integer */ |
| 64337 | | - pMem->u.i = (signed char)buf[0]; |
| 64292 | + pMem->u.i = ONE_BYTE_INT(buf); |
| 64338 | 64293 | pMem->flags = MEM_Int; |
| 64339 | 64294 | return 1; |
| 64340 | 64295 | } |
| 64341 | 64296 | case 2: { /* 2-byte signed integer */ |
| 64342 | | - i = 256*(signed char)buf[0] | buf[1]; |
| 64343 | | - pMem->u.i = (i64)i; |
| 64297 | + pMem->u.i = TWO_BYTE_INT(buf); |
| 64344 | 64298 | pMem->flags = MEM_Int; |
| 64345 | 64299 | return 2; |
| 64346 | 64300 | } |
| 64347 | 64301 | case 3: { /* 3-byte signed integer */ |
| 64348 | | - i = 65536*(signed char)buf[0] | (buf[1]<<8) | buf[2]; |
| 64349 | | - pMem->u.i = (i64)i; |
| 64302 | + pMem->u.i = THREE_BYTE_INT(buf); |
| 64350 | 64303 | pMem->flags = MEM_Int; |
| 64351 | 64304 | return 3; |
| 64352 | 64305 | } |
| 64353 | 64306 | case 4: { /* 4-byte signed integer */ |
| 64354 | | - y = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; |
| 64307 | + y = FOUR_BYTE_UINT(buf); |
| 64355 | 64308 | pMem->u.i = (i64)*(int*)&y; |
| 64356 | 64309 | pMem->flags = MEM_Int; |
| 64357 | 64310 | return 4; |
| 64358 | 64311 | } |
| 64359 | 64312 | case 5: { /* 6-byte signed integer */ |
| 64360 | | - x = 256*(signed char)buf[0] + buf[1]; |
| 64361 | | - y = ((unsigned)buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; |
| 64362 | | - x = (x<<32) | y; |
| 64363 | | - pMem->u.i = *(i64*)&x; |
| 64313 | + pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); |
| 64364 | 64314 | pMem->flags = MEM_Int; |
| 64365 | 64315 | return 6; |
| 64366 | 64316 | } |
| 64367 | 64317 | case 6: /* 8-byte signed integer */ |
| 64368 | 64318 | case 7: { /* IEEE floating point */ |
| | @@ -64376,12 +64326,12 @@ |
| 64376 | 64326 | static const double r1 = 1.0; |
| 64377 | 64327 | u64 t2 = t1; |
| 64378 | 64328 | swapMixedEndianFloat(t2); |
| 64379 | 64329 | assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); |
| 64380 | 64330 | #endif |
| 64381 | | - x = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; |
| 64382 | | - y = ((unsigned)buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; |
| 64331 | + x = FOUR_BYTE_UINT(buf); |
| 64332 | + y = FOUR_BYTE_UINT(buf+4); |
| 64383 | 64333 | x = (x<<32) | y; |
| 64384 | 64334 | if( serial_type==6 ){ |
| 64385 | 64335 | pMem->u.i = *(i64*)&x; |
| 64386 | 64336 | pMem->flags = MEM_Int; |
| 64387 | 64337 | }else{ |
| | @@ -64473,11 +64423,11 @@ |
| 64473 | 64423 | u32 idx; /* Offset in aKey[] to read from */ |
| 64474 | 64424 | u16 u; /* Unsigned loop counter */ |
| 64475 | 64425 | u32 szHdr; |
| 64476 | 64426 | Mem *pMem = p->aMem; |
| 64477 | 64427 | |
| 64478 | | - p->flags = 0; |
| 64428 | + p->default_rc = 0; |
| 64479 | 64429 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
| 64480 | 64430 | idx = getVarint32(aKey, szHdr); |
| 64481 | 64431 | d = szHdr; |
| 64482 | 64432 | u = 0; |
| 64483 | 64433 | while( idx<szHdr && u<p->nField && d<=nKey ){ |
| | @@ -64494,30 +64444,22 @@ |
| 64494 | 64444 | } |
| 64495 | 64445 | assert( u<=pKeyInfo->nField + 1 ); |
| 64496 | 64446 | p->nField = u; |
| 64497 | 64447 | } |
| 64498 | 64448 | |
| 64449 | +#if SQLITE_DEBUG |
| 64499 | 64450 | /* |
| 64500 | | -** This function compares the two table rows or index records |
| 64501 | | -** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero |
| 64502 | | -** or positive integer if key1 is less than, equal to or |
| 64503 | | -** greater than key2. The {nKey1, pKey1} key must be a blob |
| 64504 | | -** created by th OP_MakeRecord opcode of the VDBE. The pPKey2 |
| 64505 | | -** key must be a parsed key such as obtained from |
| 64506 | | -** sqlite3VdbeParseRecord. |
| 64507 | | -** |
| 64508 | | -** Key1 and Key2 do not have to contain the same number of fields. |
| 64509 | | -** The key with fewer fields is usually compares less than the |
| 64510 | | -** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set |
| 64511 | | -** and the common prefixes are equal, then key1 is less than key2. |
| 64512 | | -** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are |
| 64513 | | -** equal, then the keys are considered to be equal and |
| 64514 | | -** the parts beyond the common prefix are ignored. |
| 64451 | +** This function compares two index or table record keys in the same way |
| 64452 | +** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(), |
| 64453 | +** this function deserializes and compares values using the |
| 64454 | +** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used |
| 64455 | +** in assert() statements to ensure that the optimized code in |
| 64456 | +** sqlite3VdbeRecordCompare() returns results with these two primitives. |
| 64515 | 64457 | */ |
| 64516 | | -SQLITE_PRIVATE int sqlite3VdbeRecordCompare( |
| 64458 | +static int vdbeRecordCompareDebug( |
| 64517 | 64459 | int nKey1, const void *pKey1, /* Left key */ |
| 64518 | | - UnpackedRecord *pPKey2 /* Right key */ |
| 64460 | + const UnpackedRecord *pPKey2 /* Right key */ |
| 64519 | 64461 | ){ |
| 64520 | 64462 | u32 d1; /* Offset into aKey[] of next data element */ |
| 64521 | 64463 | u32 idx1; /* Offset into aKey[] of next header element */ |
| 64522 | 64464 | u32 szHdr1; /* Number of bytes in header */ |
| 64523 | 64465 | int i = 0; |
| | @@ -64587,28 +64529,559 @@ |
| 64587 | 64529 | ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). |
| 64588 | 64530 | */ |
| 64589 | 64531 | assert( mem1.zMalloc==0 ); |
| 64590 | 64532 | |
| 64591 | 64533 | /* rc==0 here means that one of the keys ran out of fields and |
| 64592 | | - ** all the fields up to that point were equal. If the UNPACKED_INCRKEY |
| 64593 | | - ** flag is set, then break the tie by treating key2 as larger. |
| 64594 | | - ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes |
| 64595 | | - ** are considered to be equal. Otherwise, the longer key is the |
| 64596 | | - ** larger. As it happens, the pPKey2 will always be the longer |
| 64597 | | - ** if there is a difference. |
| 64598 | | - */ |
| 64599 | | - assert( rc==0 ); |
| 64600 | | - if( pPKey2->flags & UNPACKED_INCRKEY ){ |
| 64601 | | - rc = -1; |
| 64602 | | - }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){ |
| 64603 | | - /* Leave rc==0 */ |
| 64604 | | - }else if( idx1<szHdr1 ){ |
| 64605 | | - rc = 1; |
| 64606 | | - } |
| 64607 | | - return rc; |
| 64608 | | -} |
| 64609 | | - |
| 64534 | + ** all the fields up to that point were equal. Return the the default_rc |
| 64535 | + ** value. */ |
| 64536 | + return pPKey2->default_rc; |
| 64537 | +} |
| 64538 | +#endif |
| 64539 | + |
| 64540 | +/* |
| 64541 | +** Both *pMem1 and *pMem2 contain string values. Compare the two values |
| 64542 | +** using the collation sequence pColl. As usual, return a negative , zero |
| 64543 | +** or positive value if *pMem1 is less than, equal to or greater than |
| 64544 | +** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);". |
| 64545 | +*/ |
| 64546 | +static int vdbeCompareMemString( |
| 64547 | + const Mem *pMem1, |
| 64548 | + const Mem *pMem2, |
| 64549 | + const CollSeq *pColl |
| 64550 | +){ |
| 64551 | + if( pMem1->enc==pColl->enc ){ |
| 64552 | + /* The strings are already in the correct encoding. Call the |
| 64553 | + ** comparison function directly */ |
| 64554 | + return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z); |
| 64555 | + }else{ |
| 64556 | + int rc; |
| 64557 | + const void *v1, *v2; |
| 64558 | + int n1, n2; |
| 64559 | + Mem c1; |
| 64560 | + Mem c2; |
| 64561 | + memset(&c1, 0, sizeof(c1)); |
| 64562 | + memset(&c2, 0, sizeof(c2)); |
| 64563 | + sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); |
| 64564 | + sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); |
| 64565 | + v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); |
| 64566 | + n1 = v1==0 ? 0 : c1.n; |
| 64567 | + v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); |
| 64568 | + n2 = v2==0 ? 0 : c2.n; |
| 64569 | + rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); |
| 64570 | + sqlite3VdbeMemRelease(&c1); |
| 64571 | + sqlite3VdbeMemRelease(&c2); |
| 64572 | + return rc; |
| 64573 | + } |
| 64574 | +} |
| 64575 | + |
| 64576 | +/* |
| 64577 | +** Compare the values contained by the two memory cells, returning |
| 64578 | +** negative, zero or positive if pMem1 is less than, equal to, or greater |
| 64579 | +** than pMem2. Sorting order is NULL's first, followed by numbers (integers |
| 64580 | +** and reals) sorted numerically, followed by text ordered by the collating |
| 64581 | +** sequence pColl and finally blob's ordered by memcmp(). |
| 64582 | +** |
| 64583 | +** Two NULL values are considered equal by this function. |
| 64584 | +*/ |
| 64585 | +SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ |
| 64586 | + int rc; |
| 64587 | + int f1, f2; |
| 64588 | + int combined_flags; |
| 64589 | + |
| 64590 | + f1 = pMem1->flags; |
| 64591 | + f2 = pMem2->flags; |
| 64592 | + combined_flags = f1|f2; |
| 64593 | + assert( (combined_flags & MEM_RowSet)==0 ); |
| 64594 | + |
| 64595 | + /* If one value is NULL, it is less than the other. If both values |
| 64596 | + ** are NULL, return 0. |
| 64597 | + */ |
| 64598 | + if( combined_flags&MEM_Null ){ |
| 64599 | + return (f2&MEM_Null) - (f1&MEM_Null); |
| 64600 | + } |
| 64601 | + |
| 64602 | + /* If one value is a number and the other is not, the number is less. |
| 64603 | + ** If both are numbers, compare as reals if one is a real, or as integers |
| 64604 | + ** if both values are integers. |
| 64605 | + */ |
| 64606 | + if( combined_flags&(MEM_Int|MEM_Real) ){ |
| 64607 | + double r1, r2; |
| 64608 | + if( (f1 & f2 & MEM_Int)!=0 ){ |
| 64609 | + if( pMem1->u.i < pMem2->u.i ) return -1; |
| 64610 | + if( pMem1->u.i > pMem2->u.i ) return 1; |
| 64611 | + return 0; |
| 64612 | + } |
| 64613 | + if( (f1&MEM_Real)!=0 ){ |
| 64614 | + r1 = pMem1->r; |
| 64615 | + }else if( (f1&MEM_Int)!=0 ){ |
| 64616 | + r1 = (double)pMem1->u.i; |
| 64617 | + }else{ |
| 64618 | + return 1; |
| 64619 | + } |
| 64620 | + if( (f2&MEM_Real)!=0 ){ |
| 64621 | + r2 = pMem2->r; |
| 64622 | + }else if( (f2&MEM_Int)!=0 ){ |
| 64623 | + r2 = (double)pMem2->u.i; |
| 64624 | + }else{ |
| 64625 | + return -1; |
| 64626 | + } |
| 64627 | + if( r1<r2 ) return -1; |
| 64628 | + if( r1>r2 ) return 1; |
| 64629 | + return 0; |
| 64630 | + } |
| 64631 | + |
| 64632 | + /* If one value is a string and the other is a blob, the string is less. |
| 64633 | + ** If both are strings, compare using the collating functions. |
| 64634 | + */ |
| 64635 | + if( combined_flags&MEM_Str ){ |
| 64636 | + if( (f1 & MEM_Str)==0 ){ |
| 64637 | + return 1; |
| 64638 | + } |
| 64639 | + if( (f2 & MEM_Str)==0 ){ |
| 64640 | + return -1; |
| 64641 | + } |
| 64642 | + |
| 64643 | + assert( pMem1->enc==pMem2->enc ); |
| 64644 | + assert( pMem1->enc==SQLITE_UTF8 || |
| 64645 | + pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); |
| 64646 | + |
| 64647 | + /* The collation sequence must be defined at this point, even if |
| 64648 | + ** the user deletes the collation sequence after the vdbe program is |
| 64649 | + ** compiled (this was not always the case). |
| 64650 | + */ |
| 64651 | + assert( !pColl || pColl->xCmp ); |
| 64652 | + |
| 64653 | + if( pColl ){ |
| 64654 | + return vdbeCompareMemString(pMem1, pMem2, pColl); |
| 64655 | + } |
| 64656 | + /* If a NULL pointer was passed as the collate function, fall through |
| 64657 | + ** to the blob case and use memcmp(). */ |
| 64658 | + } |
| 64659 | + |
| 64660 | + /* Both values must be blobs. Compare using memcmp(). */ |
| 64661 | + rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n); |
| 64662 | + if( rc==0 ){ |
| 64663 | + rc = pMem1->n - pMem2->n; |
| 64664 | + } |
| 64665 | + return rc; |
| 64666 | +} |
| 64667 | + |
| 64668 | + |
| 64669 | +/* |
| 64670 | +** The first argument passed to this function is a serial-type that |
| 64671 | +** corresponds to an integer - all values between 1 and 9 inclusive |
| 64672 | +** except 7. The second points to a buffer containing an integer value |
| 64673 | +** serialized according to serial_type. This function deserializes |
| 64674 | +** and returns the value. |
| 64675 | +*/ |
| 64676 | +static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){ |
| 64677 | + u32 y; |
| 64678 | + assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) ); |
| 64679 | + switch( serial_type ){ |
| 64680 | + case 0: |
| 64681 | + case 1: |
| 64682 | + return ONE_BYTE_INT(aKey); |
| 64683 | + case 2: |
| 64684 | + return TWO_BYTE_INT(aKey); |
| 64685 | + case 3: |
| 64686 | + return THREE_BYTE_INT(aKey); |
| 64687 | + case 4: { |
| 64688 | + y = FOUR_BYTE_UINT(aKey); |
| 64689 | + return (i64)*(int*)&y; |
| 64690 | + } |
| 64691 | + case 5: { |
| 64692 | + return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); |
| 64693 | + } |
| 64694 | + case 6: { |
| 64695 | + u64 x = FOUR_BYTE_UINT(aKey); |
| 64696 | + x = (x<<32) | FOUR_BYTE_UINT(aKey+4); |
| 64697 | + return (i64)*(i64*)&x; |
| 64698 | + } |
| 64699 | + } |
| 64700 | + |
| 64701 | + return (serial_type - 8); |
| 64702 | +} |
| 64703 | + |
| 64704 | +/* |
| 64705 | +** This function compares the two table rows or index records |
| 64706 | +** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero |
| 64707 | +** or positive integer if key1 is less than, equal to or |
| 64708 | +** greater than key2. The {nKey1, pKey1} key must be a blob |
| 64709 | +** created by th OP_MakeRecord opcode of the VDBE. The pPKey2 |
| 64710 | +** key must be a parsed key such as obtained from |
| 64711 | +** sqlite3VdbeParseRecord. |
| 64712 | +** |
| 64713 | +** If argument bSkip is non-zero, it is assumed that the caller has already |
| 64714 | +** determined that the first fields of the keys are equal. |
| 64715 | +** |
| 64716 | +** Key1 and Key2 do not have to contain the same number of fields. If all |
| 64717 | +** fields that appear in both keys are equal, then pPKey2->default_rc is |
| 64718 | +** returned. |
| 64719 | +*/ |
| 64720 | +SQLITE_PRIVATE int sqlite3VdbeRecordCompare( |
| 64721 | + int nKey1, const void *pKey1, /* Left key */ |
| 64722 | + const UnpackedRecord *pPKey2, /* Right key */ |
| 64723 | + int bSkip /* If true, skip the first field */ |
| 64724 | +){ |
| 64725 | + u32 d1; /* Offset into aKey[] of next data element */ |
| 64726 | + int i; /* Index of next field to compare */ |
| 64727 | + u32 szHdr1; /* Size of record header in bytes */ |
| 64728 | + u32 idx1; /* Offset of first type in header */ |
| 64729 | + int rc = 0; /* Return value */ |
| 64730 | + Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */ |
| 64731 | + KeyInfo *pKeyInfo = pPKey2->pKeyInfo; |
| 64732 | + const unsigned char *aKey1 = (const unsigned char *)pKey1; |
| 64733 | + Mem mem1; |
| 64734 | + |
| 64735 | + /* If bSkip is true, then the caller has already determined that the first |
| 64736 | + ** two elements in the keys are equal. Fix the various stack variables so |
| 64737 | + ** that this routine begins comparing at the second field. */ |
| 64738 | + if( bSkip ){ |
| 64739 | + u32 s1; |
| 64740 | + idx1 = 1 + getVarint32(&aKey1[1], s1); |
| 64741 | + szHdr1 = aKey1[0]; |
| 64742 | + d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1); |
| 64743 | + i = 1; |
| 64744 | + pRhs++; |
| 64745 | + }else{ |
| 64746 | + idx1 = getVarint32(aKey1, szHdr1); |
| 64747 | + d1 = szHdr1; |
| 64748 | + i = 0; |
| 64749 | + } |
| 64750 | + |
| 64751 | + VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */ |
| 64752 | + assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField |
| 64753 | + || CORRUPT_DB ); |
| 64754 | + assert( pPKey2->pKeyInfo->aSortOrder!=0 ); |
| 64755 | + assert( pPKey2->pKeyInfo->nField>0 ); |
| 64756 | + assert( idx1<=szHdr1 || CORRUPT_DB ); |
| 64757 | + do{ |
| 64758 | + u32 serial_type; |
| 64759 | + |
| 64760 | + /* RHS is an integer */ |
| 64761 | + if( pRhs->flags & MEM_Int ){ |
| 64762 | + serial_type = aKey1[idx1]; |
| 64763 | + if( serial_type>=12 ){ |
| 64764 | + rc = +1; |
| 64765 | + }else if( serial_type==0 ){ |
| 64766 | + rc = -1; |
| 64767 | + }else if( serial_type==7 ){ |
| 64768 | + double rhs = (double)pRhs->u.i; |
| 64769 | + sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); |
| 64770 | + if( mem1.r<rhs ){ |
| 64771 | + rc = -1; |
| 64772 | + }else if( mem1.r>rhs ){ |
| 64773 | + rc = +1; |
| 64774 | + } |
| 64775 | + }else{ |
| 64776 | + i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]); |
| 64777 | + i64 rhs = pRhs->u.i; |
| 64778 | + if( lhs<rhs ){ |
| 64779 | + rc = -1; |
| 64780 | + }else if( lhs>rhs ){ |
| 64781 | + rc = +1; |
| 64782 | + } |
| 64783 | + } |
| 64784 | + } |
| 64785 | + |
| 64786 | + /* RHS is real */ |
| 64787 | + else if( pRhs->flags & MEM_Real ){ |
| 64788 | + serial_type = aKey1[idx1]; |
| 64789 | + if( serial_type>=12 ){ |
| 64790 | + rc = +1; |
| 64791 | + }else if( serial_type==0 ){ |
| 64792 | + rc = -1; |
| 64793 | + }else{ |
| 64794 | + double rhs = pRhs->r; |
| 64795 | + double lhs; |
| 64796 | + sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); |
| 64797 | + if( serial_type==7 ){ |
| 64798 | + lhs = mem1.r; |
| 64799 | + }else{ |
| 64800 | + lhs = (double)mem1.u.i; |
| 64801 | + } |
| 64802 | + if( lhs<rhs ){ |
| 64803 | + rc = -1; |
| 64804 | + }else if( lhs>rhs ){ |
| 64805 | + rc = +1; |
| 64806 | + } |
| 64807 | + } |
| 64808 | + } |
| 64809 | + |
| 64810 | + /* RHS is a string */ |
| 64811 | + else if( pRhs->flags & MEM_Str ){ |
| 64812 | + getVarint32(&aKey1[idx1], serial_type); |
| 64813 | + if( serial_type<12 ){ |
| 64814 | + rc = -1; |
| 64815 | + }else if( !(serial_type & 0x01) ){ |
| 64816 | + rc = +1; |
| 64817 | + }else{ |
| 64818 | + mem1.n = (serial_type - 12) / 2; |
| 64819 | + if( (d1+mem1.n) > (unsigned)nKey1 ){ |
| 64820 | + rc = 1; /* Corruption */ |
| 64821 | + }else if( pKeyInfo->aColl[i] ){ |
| 64822 | + mem1.enc = pKeyInfo->enc; |
| 64823 | + mem1.db = pKeyInfo->db; |
| 64824 | + mem1.flags = MEM_Str; |
| 64825 | + mem1.z = (char*)&aKey1[d1]; |
| 64826 | + rc = vdbeCompareMemString(&mem1, pRhs, pKeyInfo->aColl[i]); |
| 64827 | + }else{ |
| 64828 | + int nCmp = MIN(mem1.n, pRhs->n); |
| 64829 | + rc = memcmp(&aKey1[d1], pRhs->z, nCmp); |
| 64830 | + if( rc==0 ) rc = mem1.n - pRhs->n; |
| 64831 | + } |
| 64832 | + } |
| 64833 | + } |
| 64834 | + |
| 64835 | + /* RHS is a blob */ |
| 64836 | + else if( pRhs->flags & MEM_Blob ){ |
| 64837 | + getVarint32(&aKey1[idx1], serial_type); |
| 64838 | + if( serial_type<12 || (serial_type & 0x01) ){ |
| 64839 | + rc = -1; |
| 64840 | + }else{ |
| 64841 | + int nStr = (serial_type - 12) / 2; |
| 64842 | + if( (d1+nStr) > (unsigned)nKey1 ){ |
| 64843 | + rc = 1; /* Corruption */ |
| 64844 | + }else{ |
| 64845 | + int nCmp = MIN(nStr, pRhs->n); |
| 64846 | + rc = memcmp(&aKey1[d1], pRhs->z, nCmp); |
| 64847 | + if( rc==0 ) rc = nStr - pRhs->n; |
| 64848 | + } |
| 64849 | + } |
| 64850 | + } |
| 64851 | + |
| 64852 | + /* RHS is null */ |
| 64853 | + else{ |
| 64854 | + serial_type = aKey1[idx1]; |
| 64855 | + rc = (serial_type!=0); |
| 64856 | + } |
| 64857 | + |
| 64858 | + if( rc!=0 ){ |
| 64859 | + if( pKeyInfo->aSortOrder[i] ){ |
| 64860 | + rc = -rc; |
| 64861 | + } |
| 64862 | + assert( CORRUPT_DB |
| 64863 | + || (rc<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0) |
| 64864 | + || (rc>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0) |
| 64865 | + || pKeyInfo->db->mallocFailed |
| 64866 | + ); |
| 64867 | + assert( mem1.zMalloc==0 ); /* See comment below */ |
| 64868 | + return rc; |
| 64869 | + } |
| 64870 | + |
| 64871 | + i++; |
| 64872 | + pRhs++; |
| 64873 | + d1 += sqlite3VdbeSerialTypeLen(serial_type); |
| 64874 | + idx1 += sqlite3VarintLen(serial_type); |
| 64875 | + }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 ); |
| 64876 | + |
| 64877 | + /* No memory allocation is ever used on mem1. Prove this using |
| 64878 | + ** the following assert(). If the assert() fails, it indicates a |
| 64879 | + ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */ |
| 64880 | + assert( mem1.zMalloc==0 ); |
| 64881 | + |
| 64882 | + /* rc==0 here means that one or both of the keys ran out of fields and |
| 64883 | + ** all the fields up to that point were equal. Return the the default_rc |
| 64884 | + ** value. */ |
| 64885 | + assert( CORRUPT_DB |
| 64886 | + || pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2) |
| 64887 | + ); |
| 64888 | + return pPKey2->default_rc; |
| 64889 | +} |
| 64890 | + |
| 64891 | +/* |
| 64892 | +** This function is an optimized version of sqlite3VdbeRecordCompare() |
| 64893 | +** that (a) the first field of pPKey2 is an integer, and (b) the |
| 64894 | +** size-of-header varint at the start of (pKey1/nKey1) fits in a single |
| 64895 | +** byte (i.e. is less than 128). |
| 64896 | +*/ |
| 64897 | +static int vdbeRecordCompareInt( |
| 64898 | + int nKey1, const void *pKey1, /* Left key */ |
| 64899 | + const UnpackedRecord *pPKey2, /* Right key */ |
| 64900 | + int bSkip /* Ignored */ |
| 64901 | +){ |
| 64902 | + const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F]; |
| 64903 | + int serial_type = ((const u8*)pKey1)[1]; |
| 64904 | + int res; |
| 64905 | + u32 y; |
| 64906 | + u64 x; |
| 64907 | + i64 v = pPKey2->aMem[0].u.i; |
| 64908 | + i64 lhs; |
| 64909 | + UNUSED_PARAMETER(bSkip); |
| 64910 | + |
| 64911 | + assert( bSkip==0 ); |
| 64912 | + switch( serial_type ){ |
| 64913 | + case 1: { /* 1-byte signed integer */ |
| 64914 | + lhs = ONE_BYTE_INT(aKey); |
| 64915 | + break; |
| 64916 | + } |
| 64917 | + case 2: { /* 2-byte signed integer */ |
| 64918 | + lhs = TWO_BYTE_INT(aKey); |
| 64919 | + break; |
| 64920 | + } |
| 64921 | + case 3: { /* 3-byte signed integer */ |
| 64922 | + lhs = THREE_BYTE_INT(aKey); |
| 64923 | + break; |
| 64924 | + } |
| 64925 | + case 4: { /* 4-byte signed integer */ |
| 64926 | + y = FOUR_BYTE_UINT(aKey); |
| 64927 | + lhs = (i64)*(int*)&y; |
| 64928 | + break; |
| 64929 | + } |
| 64930 | + case 5: { /* 6-byte signed integer */ |
| 64931 | + lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); |
| 64932 | + break; |
| 64933 | + } |
| 64934 | + case 6: { /* 8-byte signed integer */ |
| 64935 | + x = FOUR_BYTE_UINT(aKey); |
| 64936 | + x = (x<<32) | FOUR_BYTE_UINT(aKey+4); |
| 64937 | + lhs = *(i64*)&x; |
| 64938 | + break; |
| 64939 | + } |
| 64940 | + case 8: |
| 64941 | + lhs = 0; |
| 64942 | + break; |
| 64943 | + case 9: |
| 64944 | + lhs = 1; |
| 64945 | + break; |
| 64946 | + |
| 64947 | + /* This case could be removed without changing the results of running |
| 64948 | + ** this code. Including it causes gcc to generate a faster switch |
| 64949 | + ** statement (since the range of switch targets now starts at zero and |
| 64950 | + ** is contiguous) but does not cause any duplicate code to be generated |
| 64951 | + ** (as gcc is clever enough to combine the two like cases). Other |
| 64952 | + ** compilers might be similar. */ |
| 64953 | + case 0: case 7: |
| 64954 | + return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0); |
| 64955 | + |
| 64956 | + default: |
| 64957 | + return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0); |
| 64958 | + } |
| 64959 | + |
| 64960 | + if( v>lhs ){ |
| 64961 | + res = pPKey2->r1; |
| 64962 | + }else if( v<lhs ){ |
| 64963 | + res = pPKey2->r2; |
| 64964 | + }else if( pPKey2->nField>1 ){ |
| 64965 | + /* The first fields of the two keys are equal. Compare the trailing |
| 64966 | + ** fields. */ |
| 64967 | + res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1); |
| 64968 | + }else{ |
| 64969 | + /* The first fields of the two keys are equal and there are no trailing |
| 64970 | + ** fields. Return pPKey2->default_rc in this case. */ |
| 64971 | + res = pPKey2->default_rc; |
| 64972 | + } |
| 64973 | + |
| 64974 | + assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0) |
| 64975 | + || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0) |
| 64976 | + || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0) |
| 64977 | + || CORRUPT_DB |
| 64978 | + ); |
| 64979 | + return res; |
| 64980 | +} |
| 64981 | + |
| 64982 | +/* |
| 64983 | +** This function is an optimized version of sqlite3VdbeRecordCompare() |
| 64984 | +** that (a) the first field of pPKey2 is a string, that (b) the first field |
| 64985 | +** uses the collation sequence BINARY and (c) that the size-of-header varint |
| 64986 | +** at the start of (pKey1/nKey1) fits in a single byte. |
| 64987 | +*/ |
| 64988 | +static int vdbeRecordCompareString( |
| 64989 | + int nKey1, const void *pKey1, /* Left key */ |
| 64990 | + const UnpackedRecord *pPKey2, /* Right key */ |
| 64991 | + int bSkip |
| 64992 | +){ |
| 64993 | + const u8 *aKey1 = (const u8*)pKey1; |
| 64994 | + int serial_type; |
| 64995 | + int res; |
| 64996 | + UNUSED_PARAMETER(bSkip); |
| 64997 | + |
| 64998 | + assert( bSkip==0 ); |
| 64999 | + getVarint32(&aKey1[1], serial_type); |
| 65000 | + |
| 65001 | + if( serial_type<12 ){ |
| 65002 | + res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */ |
| 65003 | + }else if( !(serial_type & 0x01) ){ |
| 65004 | + res = pPKey2->r2; /* (pKey1/nKey1) is a blob */ |
| 65005 | + }else{ |
| 65006 | + int nCmp; |
| 65007 | + int nStr; |
| 65008 | + int szHdr = aKey1[0]; |
| 65009 | + |
| 65010 | + nStr = (serial_type-12) / 2; |
| 65011 | + if( (szHdr + nStr) > nKey1 ) return 0; /* Corruption */ |
| 65012 | + nCmp = MIN( pPKey2->aMem[0].n, nStr ); |
| 65013 | + res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp); |
| 65014 | + |
| 65015 | + if( res==0 ){ |
| 65016 | + res = nStr - pPKey2->aMem[0].n; |
| 65017 | + if( res==0 ){ |
| 65018 | + if( pPKey2->nField>1 ){ |
| 65019 | + res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1); |
| 65020 | + }else{ |
| 65021 | + res = pPKey2->default_rc; |
| 65022 | + } |
| 65023 | + }else if( res>0 ){ |
| 65024 | + res = pPKey2->r2; |
| 65025 | + }else{ |
| 65026 | + res = pPKey2->r1; |
| 65027 | + } |
| 65028 | + }else if( res>0 ){ |
| 65029 | + res = pPKey2->r2; |
| 65030 | + }else{ |
| 65031 | + res = pPKey2->r1; |
| 65032 | + } |
| 65033 | + } |
| 65034 | + |
| 65035 | + assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0) |
| 65036 | + || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0) |
| 65037 | + || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0) |
| 65038 | + || CORRUPT_DB |
| 65039 | + ); |
| 65040 | + return res; |
| 65041 | +} |
| 65042 | + |
| 65043 | +/* |
| 65044 | +** Return a pointer to an sqlite3VdbeRecordCompare() compatible function |
| 65045 | +** suitable for comparing serialized records to the unpacked record passed |
| 65046 | +** as the only argument. |
| 65047 | +*/ |
| 65048 | +SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){ |
| 65049 | + /* varintRecordCompareInt() and varintRecordCompareString() both assume |
| 65050 | + ** that the size-of-header varint that occurs at the start of each record |
| 65051 | + ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt() |
| 65052 | + ** also assumes that it is safe to overread a buffer by at least the |
| 65053 | + ** maximum possible legal header size plus 8 bytes. Because there is |
| 65054 | + ** guaranteed to be at least 74 (but not 136) bytes of padding following each |
| 65055 | + ** buffer passed to varintRecordCompareInt() this makes it convenient to |
| 65056 | + ** limit the size of the header to 64 bytes in cases where the first field |
| 65057 | + ** is an integer. |
| 65058 | + ** |
| 65059 | + ** The easiest way to enforce this limit is to consider only records with |
| 65060 | + ** 13 fields or less. If the first field is an integer, the maximum legal |
| 65061 | + ** header size is (12*5 + 1 + 1) bytes. */ |
| 65062 | + if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){ |
| 65063 | + int flags = p->aMem[0].flags; |
| 65064 | + if( p->pKeyInfo->aSortOrder[0] ){ |
| 65065 | + p->r1 = 1; |
| 65066 | + p->r2 = -1; |
| 65067 | + }else{ |
| 65068 | + p->r1 = -1; |
| 65069 | + p->r2 = 1; |
| 65070 | + } |
| 65071 | + if( (flags & MEM_Int) ){ |
| 65072 | + return vdbeRecordCompareInt; |
| 65073 | + } |
| 65074 | + if( (flags & (MEM_Int|MEM_Real|MEM_Null|MEM_Blob))==0 |
| 65075 | + && p->pKeyInfo->aColl[0]==0 |
| 65076 | + ){ |
| 65077 | + return vdbeRecordCompareString; |
| 65078 | + } |
| 65079 | + } |
| 65080 | + |
| 65081 | + return sqlite3VdbeRecordCompare; |
| 65082 | +} |
| 64610 | 65083 | |
| 64611 | 65084 | /* |
| 64612 | 65085 | ** pCur points at an index entry created using the OP_MakeRecord opcode. |
| 64613 | 65086 | ** Read the rowid (the last field in the record) and store it in *rowid. |
| 64614 | 65087 | ** Return SQLITE_OK if everything works, or an error code otherwise. |
| | @@ -64695,23 +65168,23 @@ |
| 64695 | 65168 | ** omits the rowid at the end. The rowid at the end of the index entry |
| 64696 | 65169 | ** is ignored as well. Hence, this routine only compares the prefixes |
| 64697 | 65170 | ** of the keys prior to the final rowid, not the entire key. |
| 64698 | 65171 | */ |
| 64699 | 65172 | SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare( |
| 64700 | | - VdbeCursor *pC, /* The cursor to compare against */ |
| 64701 | | - UnpackedRecord *pUnpacked, /* Unpacked version of key to compare against */ |
| 64702 | | - int *res /* Write the comparison result here */ |
| 65173 | + VdbeCursor *pC, /* The cursor to compare against */ |
| 65174 | + const UnpackedRecord *pUnpacked, /* Unpacked version of key */ |
| 65175 | + int *res /* Write the comparison result here */ |
| 64703 | 65176 | ){ |
| 64704 | 65177 | i64 nCellKey = 0; |
| 64705 | 65178 | int rc; |
| 64706 | 65179 | BtCursor *pCur = pC->pCursor; |
| 64707 | 65180 | Mem m; |
| 64708 | 65181 | |
| 64709 | 65182 | assert( sqlite3BtreeCursorIsValid(pCur) ); |
| 64710 | 65183 | VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); |
| 64711 | 65184 | assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ |
| 64712 | | - /* nCellKey will always be between 0 and 0xffffffff because of the say |
| 65185 | + /* nCellKey will always be between 0 and 0xffffffff because of the way |
| 64713 | 65186 | ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ |
| 64714 | 65187 | if( nCellKey<=0 || nCellKey>0x7fffffff ){ |
| 64715 | 65188 | *res = 0; |
| 64716 | 65189 | return SQLITE_CORRUPT_BKPT; |
| 64717 | 65190 | } |
| | @@ -64718,12 +65191,11 @@ |
| 64718 | 65191 | memset(&m, 0, sizeof(m)); |
| 64719 | 65192 | rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m); |
| 64720 | 65193 | if( rc ){ |
| 64721 | 65194 | return rc; |
| 64722 | 65195 | } |
| 64723 | | - assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH ); |
| 64724 | | - *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); |
| 65196 | + *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked, 0); |
| 64725 | 65197 | sqlite3VdbeMemRelease(&m); |
| 64726 | 65198 | return SQLITE_OK; |
| 64727 | 65199 | } |
| 64728 | 65200 | |
| 64729 | 65201 | /* |
| | @@ -64783,11 +65255,10 @@ |
| 64783 | 65255 | if( 0==(pMem->flags & MEM_Null) ){ |
| 64784 | 65256 | sqlite3_value *pRet = sqlite3ValueNew(v->db); |
| 64785 | 65257 | if( pRet ){ |
| 64786 | 65258 | sqlite3VdbeMemCopy((Mem *)pRet, pMem); |
| 64787 | 65259 | sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8); |
| 64788 | | - sqlite3VdbeMemStoreType((Mem *)pRet); |
| 64789 | 65260 | } |
| 64790 | 65261 | return pRet; |
| 64791 | 65262 | } |
| 64792 | 65263 | } |
| 64793 | 65264 | return 0; |
| | @@ -64957,11 +65428,10 @@ |
| 64957 | 65428 | */ |
| 64958 | 65429 | SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){ |
| 64959 | 65430 | Mem *p = (Mem*)pVal; |
| 64960 | 65431 | if( p->flags & (MEM_Blob|MEM_Str) ){ |
| 64961 | 65432 | sqlite3VdbeMemExpandBlob(p); |
| 64962 | | - p->flags &= ~MEM_Str; |
| 64963 | 65433 | p->flags |= MEM_Blob; |
| 64964 | 65434 | return p->n ? p->z : 0; |
| 64965 | 65435 | }else{ |
| 64966 | 65436 | return sqlite3_value_text(pVal); |
| 64967 | 65437 | } |
| | @@ -65028,11 +65498,11 @@ |
| 65028 | 65498 | SQLITE_INTEGER, /* 0x1c */ |
| 65029 | 65499 | SQLITE_NULL, /* 0x1d */ |
| 65030 | 65500 | SQLITE_INTEGER, /* 0x1e */ |
| 65031 | 65501 | SQLITE_NULL, /* 0x1f */ |
| 65032 | 65502 | }; |
| 65033 | | - return aType[pVal->memType&0x1f]; |
| 65503 | + return aType[pVal->flags&MEM_AffMask]; |
| 65034 | 65504 | } |
| 65035 | 65505 | |
| 65036 | 65506 | /**************************** sqlite3_result_ ******************************* |
| 65037 | 65507 | ** The following routines are used by user-defined functions to specify |
| 65038 | 65508 | ** the function result. |
| | @@ -65549,10 +66019,34 @@ |
| 65549 | 66019 | Vdbe *pVm = (Vdbe *)pStmt; |
| 65550 | 66020 | if( pVm==0 || pVm->pResultSet==0 ) return 0; |
| 65551 | 66021 | return pVm->nResColumn; |
| 65552 | 66022 | } |
| 65553 | 66023 | |
| 66024 | +/* |
| 66025 | +** Return a pointer to static memory containing an SQL NULL value. |
| 66026 | +*/ |
| 66027 | +static const Mem *columnNullValue(void){ |
| 66028 | + /* Even though the Mem structure contains an element |
| 66029 | + ** of type i64, on certain architectures (x86) with certain compiler |
| 66030 | + ** switches (-Os), gcc may align this Mem object on a 4-byte boundary |
| 66031 | + ** instead of an 8-byte one. This all works fine, except that when |
| 66032 | + ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s |
| 66033 | + ** that a Mem structure is located on an 8-byte boundary. To prevent |
| 66034 | + ** these assert()s from failing, when building with SQLITE_DEBUG defined |
| 66035 | + ** using gcc, we force nullMem to be 8-byte aligned using the magical |
| 66036 | + ** __attribute__((aligned(8))) macro. */ |
| 66037 | + static const Mem nullMem |
| 66038 | +#if defined(SQLITE_DEBUG) && defined(__GNUC__) |
| 66039 | + __attribute__((aligned(8))) |
| 66040 | +#endif |
| 66041 | + = {0, "", (double)0, {0}, 0, MEM_Null, 0, |
| 66042 | +#ifdef SQLITE_DEBUG |
| 66043 | + 0, 0, /* pScopyFrom, pFiller */ |
| 66044 | +#endif |
| 66045 | + 0, 0 }; |
| 66046 | + return &nullMem; |
| 66047 | +} |
| 65554 | 66048 | |
| 65555 | 66049 | /* |
| 65556 | 66050 | ** Check to see if column iCol of the given statement is valid. If |
| 65557 | 66051 | ** it is, return a pointer to the Mem for the value of that column. |
| 65558 | 66052 | ** If iCol is not valid, return a pointer to a Mem which has a value |
| | @@ -65565,36 +66059,15 @@ |
| 65565 | 66059 | pVm = (Vdbe *)pStmt; |
| 65566 | 66060 | if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ |
| 65567 | 66061 | sqlite3_mutex_enter(pVm->db->mutex); |
| 65568 | 66062 | pOut = &pVm->pResultSet[i]; |
| 65569 | 66063 | }else{ |
| 65570 | | - /* If the value passed as the second argument is out of range, return |
| 65571 | | - ** a pointer to the following static Mem object which contains the |
| 65572 | | - ** value SQL NULL. Even though the Mem structure contains an element |
| 65573 | | - ** of type i64, on certain architectures (x86) with certain compiler |
| 65574 | | - ** switches (-Os), gcc may align this Mem object on a 4-byte boundary |
| 65575 | | - ** instead of an 8-byte one. This all works fine, except that when |
| 65576 | | - ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s |
| 65577 | | - ** that a Mem structure is located on an 8-byte boundary. To prevent |
| 65578 | | - ** these assert()s from failing, when building with SQLITE_DEBUG defined |
| 65579 | | - ** using gcc, we force nullMem to be 8-byte aligned using the magical |
| 65580 | | - ** __attribute__((aligned(8))) macro. */ |
| 65581 | | - static const Mem nullMem |
| 65582 | | -#if defined(SQLITE_DEBUG) && defined(__GNUC__) |
| 65583 | | - __attribute__((aligned(8))) |
| 65584 | | -#endif |
| 65585 | | - = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0, |
| 65586 | | -#ifdef SQLITE_DEBUG |
| 65587 | | - 0, 0, /* pScopyFrom, pFiller */ |
| 65588 | | -#endif |
| 65589 | | - 0, 0 }; |
| 65590 | | - |
| 65591 | 66064 | if( pVm && ALWAYS(pVm->db) ){ |
| 65592 | 66065 | sqlite3_mutex_enter(pVm->db->mutex); |
| 65593 | 66066 | sqlite3Error(pVm->db, SQLITE_RANGE, 0); |
| 65594 | 66067 | } |
| 65595 | | - pOut = (Mem*)&nullMem; |
| 66068 | + pOut = (Mem*)columnNullValue(); |
| 65596 | 66069 | } |
| 65597 | 66070 | return pOut; |
| 65598 | 66071 | } |
| 65599 | 66072 | |
| 65600 | 66073 | /* |
| | @@ -66629,11 +67102,11 @@ |
| 66629 | 67102 | ** does not control the string, it might be deleted without the register |
| 66630 | 67103 | ** knowing it. |
| 66631 | 67104 | ** |
| 66632 | 67105 | ** This routine converts an ephemeral string into a dynamically allocated |
| 66633 | 67106 | ** string that the register itself controls. In other words, it |
| 66634 | | -** converts an MEM_Ephem string into an MEM_Dyn string. |
| 67107 | +** converts an MEM_Ephem string into a string with P.z==P.zMalloc. |
| 66635 | 67108 | */ |
| 66636 | 67109 | #define Deephemeralize(P) \ |
| 66637 | 67110 | if( ((P)->flags&MEM_Ephem)!=0 \ |
| 66638 | 67111 | && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} |
| 66639 | 67112 | |
| | @@ -66770,11 +67243,10 @@ |
| 66770 | 67243 | SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){ |
| 66771 | 67244 | int eType = sqlite3_value_type(pVal); |
| 66772 | 67245 | if( eType==SQLITE_TEXT ){ |
| 66773 | 67246 | Mem *pMem = (Mem*)pVal; |
| 66774 | 67247 | applyNumericAffinity(pMem); |
| 66775 | | - sqlite3VdbeMemStoreType(pMem); |
| 66776 | 67248 | eType = sqlite3_value_type(pVal); |
| 66777 | 67249 | } |
| 66778 | 67250 | return eType; |
| 66779 | 67251 | } |
| 66780 | 67252 | |
| | @@ -67166,22 +67638,25 @@ |
| 67166 | 67638 | #ifdef SQLITE_DEBUG |
| 67167 | 67639 | if( (pOp->opflags & OPFLG_IN1)!=0 ){ |
| 67168 | 67640 | assert( pOp->p1>0 ); |
| 67169 | 67641 | assert( pOp->p1<=(p->nMem-p->nCursor) ); |
| 67170 | 67642 | assert( memIsValid(&aMem[pOp->p1]) ); |
| 67643 | + assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); |
| 67171 | 67644 | REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); |
| 67172 | 67645 | } |
| 67173 | 67646 | if( (pOp->opflags & OPFLG_IN2)!=0 ){ |
| 67174 | 67647 | assert( pOp->p2>0 ); |
| 67175 | 67648 | assert( pOp->p2<=(p->nMem-p->nCursor) ); |
| 67176 | 67649 | assert( memIsValid(&aMem[pOp->p2]) ); |
| 67650 | + assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); |
| 67177 | 67651 | REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); |
| 67178 | 67652 | } |
| 67179 | 67653 | if( (pOp->opflags & OPFLG_IN3)!=0 ){ |
| 67180 | 67654 | assert( pOp->p3>0 ); |
| 67181 | 67655 | assert( pOp->p3<=(p->nMem-p->nCursor) ); |
| 67182 | 67656 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 67657 | + assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); |
| 67183 | 67658 | REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); |
| 67184 | 67659 | } |
| 67185 | 67660 | if( (pOp->opflags & OPFLG_OUT2)!=0 ){ |
| 67186 | 67661 | assert( pOp->p2>0 ); |
| 67187 | 67662 | assert( pOp->p2<=(p->nMem-p->nCursor) ); |
| | @@ -67279,11 +67754,11 @@ |
| 67279 | 67754 | ** and then jump to address P2. |
| 67280 | 67755 | */ |
| 67281 | 67756 | case OP_Gosub: { /* jump */ |
| 67282 | 67757 | assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); |
| 67283 | 67758 | pIn1 = &aMem[pOp->p1]; |
| 67284 | | - assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 67759 | + assert( VdbeMemDynamic(pIn1)==0 ); |
| 67285 | 67760 | memAboutToChange(p, pIn1); |
| 67286 | 67761 | pIn1->flags = MEM_Int; |
| 67287 | 67762 | pIn1->u.i = pc; |
| 67288 | 67763 | REGISTER_TRACE(pOp->p1, pIn1); |
| 67289 | 67764 | pc = pOp->p2 - 1; |
| | @@ -67352,11 +67827,11 @@ |
| 67352 | 67827 | ** OP_EndCoroutine, jump immediately to P2. |
| 67353 | 67828 | */ |
| 67354 | 67829 | case OP_Yield: { /* in1, jump */ |
| 67355 | 67830 | int pcDest; |
| 67356 | 67831 | pIn1 = &aMem[pOp->p1]; |
| 67357 | | - assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 67832 | + assert( VdbeMemDynamic(pIn1)==0 ); |
| 67358 | 67833 | pIn1->flags = MEM_Int; |
| 67359 | 67834 | pcDest = (int)pIn1->u.i; |
| 67360 | 67835 | pIn1->u.i = pc; |
| 67361 | 67836 | REGISTER_TRACE(pOp->p1, pIn1); |
| 67362 | 67837 | pc = pcDest; |
| | @@ -67525,14 +68000,13 @@ |
| 67525 | 68000 | if( encoding!=SQLITE_UTF8 ){ |
| 67526 | 68001 | rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); |
| 67527 | 68002 | if( rc==SQLITE_TOOBIG ) goto too_big; |
| 67528 | 68003 | if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; |
| 67529 | 68004 | assert( pOut->zMalloc==pOut->z ); |
| 67530 | | - assert( pOut->flags & MEM_Dyn ); |
| 68005 | + assert( VdbeMemDynamic(pOut)==0 ); |
| 67531 | 68006 | pOut->zMalloc = 0; |
| 67532 | 68007 | pOut->flags |= MEM_Static; |
| 67533 | | - pOut->flags &= ~MEM_Dyn; |
| 67534 | 68008 | if( pOp->p4type==P4_DYNAMIC ){ |
| 67535 | 68009 | sqlite3DbFree(db, pOp->p4.z); |
| 67536 | 68010 | } |
| 67537 | 68011 | pOp->p4type = P4_DYNAMIC; |
| 67538 | 68012 | pOp->p4.z = pOut->z; |
| | @@ -67664,18 +68138,20 @@ |
| 67664 | 68138 | do{ |
| 67665 | 68139 | assert( pOut<=&aMem[(p->nMem-p->nCursor)] ); |
| 67666 | 68140 | assert( pIn1<=&aMem[(p->nMem-p->nCursor)] ); |
| 67667 | 68141 | assert( memIsValid(pIn1) ); |
| 67668 | 68142 | memAboutToChange(p, pOut); |
| 68143 | + VdbeMemRelease(pOut); |
| 67669 | 68144 | zMalloc = pOut->zMalloc; |
| 67670 | | - pOut->zMalloc = 0; |
| 67671 | | - sqlite3VdbeMemMove(pOut, pIn1); |
| 68145 | + memcpy(pOut, pIn1, sizeof(Mem)); |
| 67672 | 68146 | #ifdef SQLITE_DEBUG |
| 67673 | 68147 | if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){ |
| 67674 | 68148 | pOut->pScopyFrom += p1 - pOp->p2; |
| 67675 | 68149 | } |
| 67676 | 68150 | #endif |
| 68151 | + pIn1->flags = MEM_Undefined; |
| 68152 | + pIn1->xDel = 0; |
| 67677 | 68153 | pIn1->zMalloc = zMalloc; |
| 67678 | 68154 | REGISTER_TRACE(p2++, pOut); |
| 67679 | 68155 | pIn1++; |
| 67680 | 68156 | pOut++; |
| 67681 | 68157 | }while( n-- ); |
| | @@ -67805,11 +68281,10 @@ |
| 67805 | 68281 | assert( memIsValid(&pMem[i]) ); |
| 67806 | 68282 | Deephemeralize(&pMem[i]); |
| 67807 | 68283 | assert( (pMem[i].flags & MEM_Ephem)==0 |
| 67808 | 68284 | || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 ); |
| 67809 | 68285 | sqlite3VdbeMemNulTerminate(&pMem[i]); |
| 67810 | | - sqlite3VdbeMemStoreType(&pMem[i]); |
| 67811 | 68286 | REGISTER_TRACE(pOp->p1+i, &pMem[i]); |
| 67812 | 68287 | } |
| 67813 | 68288 | if( db->mallocFailed ) goto no_mem; |
| 67814 | 68289 | |
| 67815 | 68290 | /* Return SQLITE_ROW |
| | @@ -67848,14 +68323,14 @@ |
| 67848 | 68323 | Stringify(pIn2, encoding); |
| 67849 | 68324 | nByte = pIn1->n + pIn2->n; |
| 67850 | 68325 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 67851 | 68326 | goto too_big; |
| 67852 | 68327 | } |
| 67853 | | - MemSetTypeFlag(pOut, MEM_Str); |
| 67854 | 68328 | if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ |
| 67855 | 68329 | goto no_mem; |
| 67856 | 68330 | } |
| 68331 | + MemSetTypeFlag(pOut, MEM_Str); |
| 67857 | 68332 | if( pOut!=pIn2 ){ |
| 67858 | 68333 | memcpy(pOut->z, pIn2->z, pIn2->n); |
| 67859 | 68334 | } |
| 67860 | 68335 | memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
| 67861 | 68336 | pOut->z[nByte]=0; |
| | @@ -68051,11 +68526,10 @@ |
| 68051 | 68526 | pArg = &aMem[pOp->p2]; |
| 68052 | 68527 | for(i=0; i<n; i++, pArg++){ |
| 68053 | 68528 | assert( memIsValid(pArg) ); |
| 68054 | 68529 | apVal[i] = pArg; |
| 68055 | 68530 | Deephemeralize(pArg); |
| 68056 | | - sqlite3VdbeMemStoreType(pArg); |
| 68057 | 68531 | REGISTER_TRACE(pOp->p2+i, pArg); |
| 68058 | 68532 | } |
| 68059 | 68533 | |
| 68060 | 68534 | assert( pOp->p4type==P4_FUNCDEF ); |
| 68061 | 68535 | ctx.pFunc = pOp->p4.pFunc; |
| | @@ -69026,10 +69500,11 @@ |
| 69026 | 69500 | ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are |
| 69027 | 69501 | ** all valid. |
| 69028 | 69502 | */ |
| 69029 | 69503 | assert( p2<pC->nHdrParsed ); |
| 69030 | 69504 | assert( rc==SQLITE_OK ); |
| 69505 | + assert( sqlite3VdbeCheckMemInvariants(pDest) ); |
| 69031 | 69506 | if( pC->szRow>=aOffset[p2+1] ){ |
| 69032 | 69507 | /* This is the common case where the desired content fits on the original |
| 69033 | 69508 | ** page - where the content is not on an overflow page */ |
| 69034 | 69509 | VdbeMemRelease(pDest); |
| 69035 | 69510 | sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest); |
| | @@ -69063,12 +69538,12 @@ |
| 69063 | 69538 | ** sqlite3VdbeMemFromBtree() call above) then transfer control of that |
| 69064 | 69539 | ** dynamically allocated space over to the pDest structure. |
| 69065 | 69540 | ** This prevents a memory copy. */ |
| 69066 | 69541 | if( sMem.zMalloc ){ |
| 69067 | 69542 | assert( sMem.z==sMem.zMalloc ); |
| 69068 | | - assert( !(pDest->flags & MEM_Dyn) ); |
| 69069 | | - assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z ); |
| 69543 | + assert( VdbeMemDynamic(pDest)==0 ); |
| 69544 | + assert( (pDest->flags & (MEM_Blob|MEM_Str))==0 || pDest->z==sMem.z ); |
| 69070 | 69545 | pDest->flags &= ~(MEM_Ephem|MEM_Static); |
| 69071 | 69546 | pDest->flags |= MEM_Term; |
| 69072 | 69547 | pDest->z = sMem.z; |
| 69073 | 69548 | pDest->zMalloc = sMem.zMalloc; |
| 69074 | 69549 | } |
| | @@ -69247,11 +69722,11 @@ |
| 69247 | 69722 | assert( i==nHdr ); |
| 69248 | 69723 | assert( j==nByte ); |
| 69249 | 69724 | |
| 69250 | 69725 | assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); |
| 69251 | 69726 | pOut->n = (int)nByte; |
| 69252 | | - pOut->flags = MEM_Blob | MEM_Dyn; |
| 69727 | + pOut->flags = MEM_Blob; |
| 69253 | 69728 | pOut->xDel = 0; |
| 69254 | 69729 | if( nZero ){ |
| 69255 | 69730 | pOut->u.nZero = nZero; |
| 69256 | 69731 | pOut->flags |= MEM_Zero; |
| 69257 | 69732 | } |
| | @@ -70121,20 +70596,20 @@ |
| 70121 | 70596 | r.pKeyInfo = pC->pKeyInfo; |
| 70122 | 70597 | r.nField = (u16)nField; |
| 70123 | 70598 | |
| 70124 | 70599 | /* The next line of code computes as follows, only faster: |
| 70125 | 70600 | ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ |
| 70126 | | - ** r.flags = UNPACKED_INCRKEY; |
| 70601 | + ** r.default_rc = -1; |
| 70127 | 70602 | ** }else{ |
| 70128 | | - ** r.flags = 0; |
| 70603 | + ** r.default_rc = +1; |
| 70129 | 70604 | ** } |
| 70130 | 70605 | */ |
| 70131 | | - r.flags = (u8)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLT))); |
| 70132 | | - assert( oc!=OP_SeekGT || r.flags==UNPACKED_INCRKEY ); |
| 70133 | | - assert( oc!=OP_SeekLE || r.flags==UNPACKED_INCRKEY ); |
| 70134 | | - assert( oc!=OP_SeekGE || r.flags==0 ); |
| 70135 | | - assert( oc!=OP_SeekLT || r.flags==0 ); |
| 70606 | + r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); |
| 70607 | + assert( oc!=OP_SeekGT || r.default_rc==-1 ); |
| 70608 | + assert( oc!=OP_SeekLE || r.default_rc==-1 ); |
| 70609 | + assert( oc!=OP_SeekGE || r.default_rc==+1 ); |
| 70610 | + assert( oc!=OP_SeekLT || r.default_rc==+1 ); |
| 70136 | 70611 | |
| 70137 | 70612 | r.aMem = &aMem[pOp->p3]; |
| 70138 | 70613 | #ifdef SQLITE_DEBUG |
| 70139 | 70614 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 70140 | 70615 | #endif |
| | @@ -70288,22 +70763,21 @@ |
| 70288 | 70763 | ExpandBlob(&r.aMem[ii]); |
| 70289 | 70764 | #ifdef SQLITE_DEBUG |
| 70290 | 70765 | if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); |
| 70291 | 70766 | #endif |
| 70292 | 70767 | } |
| 70293 | | - r.flags = UNPACKED_PREFIX_MATCH; |
| 70294 | 70768 | pIdxKey = &r; |
| 70295 | 70769 | }else{ |
| 70296 | 70770 | pIdxKey = sqlite3VdbeAllocUnpackedRecord( |
| 70297 | 70771 | pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree |
| 70298 | 70772 | ); |
| 70299 | 70773 | if( pIdxKey==0 ) goto no_mem; |
| 70300 | 70774 | assert( pIn3->flags & MEM_Blob ); |
| 70301 | 70775 | assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */ |
| 70302 | 70776 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); |
| 70303 | | - pIdxKey->flags |= UNPACKED_PREFIX_MATCH; |
| 70304 | 70777 | } |
| 70778 | + pIdxKey->default_rc = 0; |
| 70305 | 70779 | if( pOp->opcode==OP_NoConflict ){ |
| 70306 | 70780 | /* For the OP_NoConflict opcode, take the jump if any of the |
| 70307 | 70781 | ** input fields are NULL, since any key with a NULL will not |
| 70308 | 70782 | ** conflict */ |
| 70309 | 70783 | for(ii=0; ii<r.nField; ii++){ |
| | @@ -71188,11 +71662,11 @@ |
| 71188 | 71662 | pCrsr = pC->pCursor; |
| 71189 | 71663 | assert( pCrsr!=0 ); |
| 71190 | 71664 | assert( pOp->p5==0 ); |
| 71191 | 71665 | r.pKeyInfo = pC->pKeyInfo; |
| 71192 | 71666 | r.nField = (u16)pOp->p3; |
| 71193 | | - r.flags = UNPACKED_PREFIX_MATCH; |
| 71667 | + r.default_rc = 0; |
| 71194 | 71668 | r.aMem = &aMem[pOp->p2]; |
| 71195 | 71669 | #ifdef SQLITE_DEBUG |
| 71196 | 71670 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 71197 | 71671 | #endif |
| 71198 | 71672 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); |
| | @@ -71302,14 +71776,14 @@ |
| 71302 | 71776 | assert( pOp->p4type==P4_INT32 ); |
| 71303 | 71777 | r.pKeyInfo = pC->pKeyInfo; |
| 71304 | 71778 | r.nField = (u16)pOp->p4.i; |
| 71305 | 71779 | if( pOp->opcode<OP_IdxLT ){ |
| 71306 | 71780 | assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); |
| 71307 | | - r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH; |
| 71781 | + r.default_rc = -1; |
| 71308 | 71782 | }else{ |
| 71309 | 71783 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); |
| 71310 | | - r.flags = UNPACKED_PREFIX_MATCH; |
| 71784 | + r.default_rc = 0; |
| 71311 | 71785 | } |
| 71312 | 71786 | r.aMem = &aMem[pOp->p3]; |
| 71313 | 71787 | #ifdef SQLITE_DEBUG |
| 71314 | 71788 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 71315 | 71789 | #endif |
| | @@ -72056,11 +72530,10 @@ |
| 72056 | 72530 | assert( apVal || n==0 ); |
| 72057 | 72531 | for(i=0; i<n; i++, pRec++){ |
| 72058 | 72532 | assert( memIsValid(pRec) ); |
| 72059 | 72533 | apVal[i] = pRec; |
| 72060 | 72534 | memAboutToChange(p, pRec); |
| 72061 | | - sqlite3VdbeMemStoreType(pRec); |
| 72062 | 72535 | } |
| 72063 | 72536 | ctx.pFunc = pOp->p4.pFunc; |
| 72064 | 72537 | assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); |
| 72065 | 72538 | ctx.pMem = pMem = &aMem[pOp->p3]; |
| 72066 | 72539 | pMem->n++; |
| | @@ -72490,11 +72963,10 @@ |
| 72490 | 72963 | { |
| 72491 | 72964 | res = 0; |
| 72492 | 72965 | apArg = p->apArg; |
| 72493 | 72966 | for(i = 0; i<nArg; i++){ |
| 72494 | 72967 | apArg[i] = &pArgc[i+1]; |
| 72495 | | - sqlite3VdbeMemStoreType(apArg[i]); |
| 72496 | 72968 | } |
| 72497 | 72969 | |
| 72498 | 72970 | p->inVtabMethod = 1; |
| 72499 | 72971 | rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg); |
| 72500 | 72972 | p->inVtabMethod = 0; |
| | @@ -72697,11 +73169,10 @@ |
| 72697 | 73169 | apArg = p->apArg; |
| 72698 | 73170 | pX = &aMem[pOp->p3]; |
| 72699 | 73171 | for(i=0; i<nArg; i++){ |
| 72700 | 73172 | assert( memIsValid(pX) ); |
| 72701 | 73173 | memAboutToChange(p, pX); |
| 72702 | | - sqlite3VdbeMemStoreType(pX); |
| 72703 | 73174 | apArg[i] = pX; |
| 72704 | 73175 | pX++; |
| 72705 | 73176 | } |
| 72706 | 73177 | db->vtabOnConflict = pOp->p5; |
| 72707 | 73178 | rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); |
| | @@ -73809,14 +74280,14 @@ |
| 73809 | 74280 | if( r2->aMem[i].flags & MEM_Null ){ |
| 73810 | 74281 | *pRes = -1; |
| 73811 | 74282 | return; |
| 73812 | 74283 | } |
| 73813 | 74284 | } |
| 73814 | | - r2->flags |= UNPACKED_PREFIX_MATCH; |
| 74285 | + assert( r2->default_rc==0 ); |
| 73815 | 74286 | } |
| 73816 | 74287 | |
| 73817 | | - *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2); |
| 74288 | + *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2, 0); |
| 73818 | 74289 | } |
| 73819 | 74290 | |
| 73820 | 74291 | /* |
| 73821 | 74292 | ** This function is called to compare two iterator keys when merging |
| 73822 | 74293 | ** multiple b-tree segments. Parameter iOut is the index of the aTree[] |
| | @@ -75449,10 +75920,12 @@ |
| 75449 | 75920 | pExpr->iTable = 1; |
| 75450 | 75921 | pTab = pParse->pTriggerTab; |
| 75451 | 75922 | }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){ |
| 75452 | 75923 | pExpr->iTable = 0; |
| 75453 | 75924 | pTab = pParse->pTriggerTab; |
| 75925 | + }else{ |
| 75926 | + pTab = 0; |
| 75454 | 75927 | } |
| 75455 | 75928 | |
| 75456 | 75929 | if( pTab ){ |
| 75457 | 75930 | int iCol; |
| 75458 | 75931 | pSchema = pTab->pSchema; |
| | @@ -87719,11 +88192,11 @@ |
| 87719 | 88192 | assert( nExtra>=1 ); |
| 87720 | 88193 | assert( pSrc!=0 ); |
| 87721 | 88194 | assert( iStart<=pSrc->nSrc ); |
| 87722 | 88195 | |
| 87723 | 88196 | /* Allocate additional space if needed */ |
| 87724 | | - if( pSrc->nSrc+nExtra>pSrc->nAlloc ){ |
| 88197 | + if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){ |
| 87725 | 88198 | SrcList *pNew; |
| 87726 | 88199 | int nAlloc = pSrc->nSrc+nExtra; |
| 87727 | 88200 | int nGot; |
| 87728 | 88201 | pNew = sqlite3DbRealloc(db, pSrc, |
| 87729 | 88202 | sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) ); |
| | @@ -87731,19 +88204,19 @@ |
| 87731 | 88204 | assert( db->mallocFailed ); |
| 87732 | 88205 | return pSrc; |
| 87733 | 88206 | } |
| 87734 | 88207 | pSrc = pNew; |
| 87735 | 88208 | nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1; |
| 87736 | | - pSrc->nAlloc = (u8)nGot; |
| 88209 | + pSrc->nAlloc = nGot; |
| 87737 | 88210 | } |
| 87738 | 88211 | |
| 87739 | 88212 | /* Move existing slots that come after the newly inserted slots |
| 87740 | 88213 | ** out of the way */ |
| 87741 | 88214 | for(i=pSrc->nSrc-1; i>=iStart; i--){ |
| 87742 | 88215 | pSrc->a[i+nExtra] = pSrc->a[i]; |
| 87743 | 88216 | } |
| 87744 | | - pSrc->nSrc += (i8)nExtra; |
| 88217 | + pSrc->nSrc += nExtra; |
| 87745 | 88218 | |
| 87746 | 88219 | /* Zero the newly allocated slots */ |
| 87747 | 88220 | memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra); |
| 87748 | 88221 | for(i=iStart; i<iStart+nExtra; i++){ |
| 87749 | 88222 | pSrc->a[i].iCursor = -1; |
| | @@ -100117,11 +100590,11 @@ |
| 100117 | 100590 | }else if( eDest!=SRT_Exists ){ |
| 100118 | 100591 | /* If the destination is an EXISTS(...) expression, the actual |
| 100119 | 100592 | ** values returned by the SELECT are not required. |
| 100120 | 100593 | */ |
| 100121 | 100594 | sqlite3ExprCodeExprList(pParse, pEList, regResult, |
| 100122 | | - (eDest==SRT_Output)?SQLITE_ECEL_DUP:0); |
| 100595 | + (eDest==SRT_Output||eDest==SRT_Coroutine)?SQLITE_ECEL_DUP:0); |
| 100123 | 100596 | } |
| 100124 | 100597 | |
| 100125 | 100598 | /* If the DISTINCT keyword was present on the SELECT statement |
| 100126 | 100599 | ** and this row has been seen before, then do not make this row |
| 100127 | 100600 | ** part of the result. |
| | @@ -101030,11 +101503,11 @@ |
| 101030 | 101503 | for(j=cnt=0; j<i; j++){ |
| 101031 | 101504 | if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){ |
| 101032 | 101505 | char *zNewName; |
| 101033 | 101506 | int k; |
| 101034 | 101507 | for(k=nName-1; k>1 && sqlite3Isdigit(zName[k]); k--){} |
| 101035 | | - if( zName[k]==':' ) nName = k; |
| 101508 | + if( k>=0 && zName[k]==':' ) nName = k; |
| 101036 | 101509 | zName[nName] = 0; |
| 101037 | 101510 | zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt); |
| 101038 | 101511 | sqlite3DbFree(db, zName); |
| 101039 | 101512 | zName = zNewName; |
| 101040 | 101513 | j = -1; |
| | @@ -110767,11 +111240,11 @@ |
| 110767 | 111240 | iCol = pRec->nField - 1; |
| 110768 | 111241 | assert( pIdx->nSample>0 ); |
| 110769 | 111242 | assert( pRec->nField>0 && iCol<pIdx->nSampleCol ); |
| 110770 | 111243 | do{ |
| 110771 | 111244 | iTest = (iMin+i)/2; |
| 110772 | | - res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec); |
| 111245 | + res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec, 0); |
| 110773 | 111246 | if( res<0 ){ |
| 110774 | 111247 | iMin = iTest+1; |
| 110775 | 111248 | }else{ |
| 110776 | 111249 | i = iTest; |
| 110777 | 111250 | } |
| | @@ -110782,20 +111255,20 @@ |
| 110782 | 111255 | ** above found the right answer. This block serves no purpose other |
| 110783 | 111256 | ** than to invoke the asserts. */ |
| 110784 | 111257 | if( res==0 ){ |
| 110785 | 111258 | /* If (res==0) is true, then sample $i must be equal to pRec */ |
| 110786 | 111259 | assert( i<pIdx->nSample ); |
| 110787 | | - assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) |
| 111260 | + assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0) |
| 110788 | 111261 | || pParse->db->mallocFailed ); |
| 110789 | 111262 | }else{ |
| 110790 | 111263 | /* Otherwise, pRec must be smaller than sample $i and larger than |
| 110791 | 111264 | ** sample ($i-1). */ |
| 110792 | 111265 | assert( i==pIdx->nSample |
| 110793 | | - || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 |
| 111266 | + || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)>0 |
| 110794 | 111267 | || pParse->db->mallocFailed ); |
| 110795 | 111268 | assert( i==0 |
| 110796 | | - || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 |
| 111269 | + || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec, 0)<0 |
| 110797 | 111270 | || pParse->db->mallocFailed ); |
| 110798 | 111271 | } |
| 110799 | 111272 | #endif /* ifdef SQLITE_DEBUG */ |
| 110800 | 111273 | |
| 110801 | 111274 | /* At this point, aSample[i] is the first sample that is greater than |
| | @@ -114724,11 +115197,11 @@ |
| 114724 | 115197 | |
| 114725 | 115198 | /* For a co-routine, change all OP_Column references to the table of |
| 114726 | 115199 | ** the co-routine into OP_SCopy of result contained in a register. |
| 114727 | 115200 | ** OP_Rowid becomes OP_Null. |
| 114728 | 115201 | */ |
| 114729 | | - if( pTabItem->viaCoroutine ){ |
| 115202 | + if( pTabItem->viaCoroutine && !db->mallocFailed ){ |
| 114730 | 115203 | last = sqlite3VdbeCurrentAddr(v); |
| 114731 | 115204 | k = pLevel->addrBody; |
| 114732 | 115205 | pOp = sqlite3VdbeGetOp(v, k); |
| 114733 | 115206 | for(; k<last; k++, pOp++){ |
| 114734 | 115207 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 114735 | 115208 | |