| | @@ -452,11 +452,11 @@ |
| 452 | 452 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 453 | 453 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 454 | 454 | */ |
| 455 | 455 | #define SQLITE_VERSION "3.38.0" |
| 456 | 456 | #define SQLITE_VERSION_NUMBER 3038000 |
| 457 | | -#define SQLITE_SOURCE_ID "2021-12-09 20:06:18 633bfeeea2bccdd44126acf3f61ecca163c9d933bdc787a2c18a697dc9406882" |
| 457 | +#define SQLITE_SOURCE_ID "2021-12-31 22:53:15 e654b57a9fc32021453eed48d1c1bba65c833fb1aac3946567968c877e4cbd10" |
| 458 | 458 | |
| 459 | 459 | /* |
| 460 | 460 | ** CAPI3REF: Run-Time Library Version Numbers |
| 461 | 461 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 462 | 462 | ** |
| | @@ -4128,17 +4128,18 @@ |
| 4128 | 4128 | ** |
| 4129 | 4129 | ** The values returned by sqlite3_errcode() and/or |
| 4130 | 4130 | ** sqlite3_extended_errcode() might change with each API call. |
| 4131 | 4131 | ** Except, there are some interfaces that are guaranteed to never |
| 4132 | 4132 | ** change the value of the error code. The error-code preserving |
| 4133 | | -** interfaces are: |
| 4133 | +** interfaces include the following: |
| 4134 | 4134 | ** |
| 4135 | 4135 | ** <ul> |
| 4136 | 4136 | ** <li> sqlite3_errcode() |
| 4137 | 4137 | ** <li> sqlite3_extended_errcode() |
| 4138 | 4138 | ** <li> sqlite3_errmsg() |
| 4139 | 4139 | ** <li> sqlite3_errmsg16() |
| 4140 | +** <li> sqlite3_error_offset() |
| 4140 | 4141 | ** </ul> |
| 4141 | 4142 | ** |
| 4142 | 4143 | ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language |
| 4143 | 4144 | ** text that describes the error, as either UTF-8 or UTF-16 respectively. |
| 4144 | 4145 | ** ^(Memory to hold the error message string is managed internally. |
| | @@ -4148,10 +4149,17 @@ |
| 4148 | 4149 | ** |
| 4149 | 4150 | ** ^The sqlite3_errstr() interface returns the English-language text |
| 4150 | 4151 | ** that describes the [result code], as UTF-8. |
| 4151 | 4152 | ** ^(Memory to hold the error message string is managed internally |
| 4152 | 4153 | ** and must not be freed by the application)^. |
| 4154 | +** |
| 4155 | +** ^If the most recent error references a specific token in the input |
| 4156 | +** SQL, the sqlite3_error_offset() interface returns the byte offset |
| 4157 | +** of the start of that token. ^The byte offset returned by |
| 4158 | +** sqlite3_error_offset() assumes that the input SQL is UTF8. |
| 4159 | +** ^If the most error does not reference a specific token in the input |
| 4160 | +** SQL, then the sqlite3_error_offset() function returns -1. |
| 4153 | 4161 | ** |
| 4154 | 4162 | ** When the serialized [threading mode] is in use, it might be the |
| 4155 | 4163 | ** case that a second error occurs on a separate thread in between |
| 4156 | 4164 | ** the time of the first error and the call to these interfaces. |
| 4157 | 4165 | ** When that happens, the second error will be reported since these |
| | @@ -4168,10 +4176,11 @@ |
| 4168 | 4176 | SQLITE_API int sqlite3_errcode(sqlite3 *db); |
| 4169 | 4177 | SQLITE_API int sqlite3_extended_errcode(sqlite3 *db); |
| 4170 | 4178 | SQLITE_API const char *sqlite3_errmsg(sqlite3*); |
| 4171 | 4179 | SQLITE_API const void *sqlite3_errmsg16(sqlite3*); |
| 4172 | 4180 | SQLITE_API const char *sqlite3_errstr(int); |
| 4181 | +SQLITE_API int sqlite3_error_offset(sqlite3 *db); |
| 4173 | 4182 | |
| 4174 | 4183 | /* |
| 4175 | 4184 | ** CAPI3REF: Prepared Statement Object |
| 4176 | 4185 | ** KEYWORDS: {prepared statement} {prepared statements} |
| 4177 | 4186 | ** |
| | @@ -9768,18 +9777,37 @@ |
| 9768 | 9777 | |
| 9769 | 9778 | /* |
| 9770 | 9779 | ** CAPI3REF: Determine The Collation For a Virtual Table Constraint |
| 9771 | 9780 | ** |
| 9772 | 9781 | ** This function may only be called from within a call to the [xBestIndex] |
| 9773 | | -** method of a [virtual table]. |
| 9782 | +** method of a [virtual table]. This function returns a pointer to a string |
| 9783 | +** that is the name of the appropriate collation sequence to use for text |
| 9784 | +** comparisons on the constraint identified by its arguments. |
| 9774 | 9785 | ** |
| 9775 | | -** The first argument must be the sqlite3_index_info object that is the |
| 9776 | | -** first parameter to the xBestIndex() method. The second argument must be |
| 9777 | | -** an index into the aConstraint[] array belonging to the sqlite3_index_info |
| 9778 | | -** structure passed to xBestIndex. This function returns a pointer to a buffer |
| 9779 | | -** containing the name of the collation sequence for the corresponding |
| 9780 | | -** constraint. |
| 9786 | +** The first argument must be the pointer to the sqlite3_index_info object |
| 9787 | +** that is the first parameter to the xBestIndex() method. The second argument |
| 9788 | +** must be an index into the aConstraint[] array belonging to the |
| 9789 | +** sqlite3_index_info structure passed to xBestIndex. |
| 9790 | +** |
| 9791 | +** Important: |
| 9792 | +** The first parameter must be the same pointer that is passed into the |
| 9793 | +** xBestMethod() method. The first parameter may not be a pointer to a |
| 9794 | +** different sqlite3_index_info object, even an exact copy. |
| 9795 | +** |
| 9796 | +** The return value is computed as follows: |
| 9797 | +** |
| 9798 | +** <ol> |
| 9799 | +** <li><p> If the constraint comes from a WHERE clause expression that contains |
| 9800 | +** a [COLLATE operator], then the name of the collation specified by |
| 9801 | +** that COLLATE operator is returned. |
| 9802 | +** <li><p> If there is no COLLATE operator, but the column that is the subject |
| 9803 | +** of the constraint specifies an alternative collating sequence via |
| 9804 | +** a [COLLATE clause] on the column definition within the CREATE TABLE |
| 9805 | +** statement that was passed into [sqlite3_declare_vtab()], then the |
| 9806 | +** name of that alternative collating sequence is returned. |
| 9807 | +** <li><p> Otherwise, "BINARY" is returned. |
| 9808 | +** </ol> |
| 9781 | 9809 | */ |
| 9782 | 9810 | SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int); |
| 9783 | 9811 | |
| 9784 | 9812 | /* |
| 9785 | 9813 | ** CAPI3REF: Conflict resolution modes |
| | @@ -16489,10 +16517,11 @@ |
| 16489 | 16517 | i64 lastRowid; /* ROWID of most recent insert (see above) */ |
| 16490 | 16518 | i64 szMmap; /* Default mmap_size setting */ |
| 16491 | 16519 | u32 nSchemaLock; /* Do not reset the schema when non-zero */ |
| 16492 | 16520 | unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */ |
| 16493 | 16521 | int errCode; /* Most recent error code (SQLITE_*) */ |
| 16522 | + int errByteOffset; /* Byte offset of error in SQL statement */ |
| 16494 | 16523 | int errMask; /* & result codes with this before returning */ |
| 16495 | 16524 | int iSysErrno; /* Errno value from last system error */ |
| 16496 | 16525 | u32 dbOptFlags; /* Flags to enable/disable optimizations */ |
| 16497 | 16526 | u8 enc; /* Text encoding */ |
| 16498 | 16527 | u8 autoCommit; /* The auto-commit flag. */ |
| | @@ -16725,10 +16754,11 @@ |
| 16725 | 16754 | #define SQLITE_SeekScan 0x00020000 /* The OP_SeekScan optimization */ |
| 16726 | 16755 | #define SQLITE_OmitOrderBy 0x00040000 /* Omit pointless ORDER BY */ |
| 16727 | 16756 | /* TH3 expects this value ^^^^^^^^^^ to be 0x40000. Coordinate any change */ |
| 16728 | 16757 | #define SQLITE_BloomFilter 0x00080000 /* Use a Bloom filter on searches */ |
| 16729 | 16758 | #define SQLITE_BloomPulldown 0x00100000 /* Run Bloom filters early */ |
| 16759 | +#define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */ |
| 16730 | 16760 | #define SQLITE_AllOpts 0xffffffff /* All optimizations */ |
| 16731 | 16761 | |
| 16732 | 16762 | /* |
| 16733 | 16763 | ** Macros for testing whether or not optimizations are enabled or disabled. |
| 16734 | 16764 | */ |
| | @@ -18519,10 +18549,12 @@ |
| 18519 | 18549 | TableLock *aTableLock; /* Required table locks for shared-cache mode */ |
| 18520 | 18550 | #endif |
| 18521 | 18551 | AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */ |
| 18522 | 18552 | Parse *pToplevel; /* Parse structure for main program (or NULL) */ |
| 18523 | 18553 | Table *pTriggerTab; /* Table triggers are being coded for */ |
| 18554 | + TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */ |
| 18555 | + ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */ |
| 18524 | 18556 | union { |
| 18525 | 18557 | int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */ |
| 18526 | 18558 | Returning *pReturning; /* The RETURNING clause */ |
| 18527 | 18559 | } u1; |
| 18528 | 18560 | u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */ |
| | @@ -18573,13 +18605,11 @@ |
| 18573 | 18605 | const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */ |
| 18574 | 18606 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 18575 | 18607 | Token sArg; /* Complete text of a module argument */ |
| 18576 | 18608 | Table **apVtabLock; /* Pointer to virtual tables needing locking */ |
| 18577 | 18609 | #endif |
| 18578 | | - TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */ |
| 18579 | 18610 | With *pWith; /* Current WITH clause, or NULL */ |
| 18580 | | - ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */ |
| 18581 | 18611 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 18582 | 18612 | RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */ |
| 18583 | 18613 | #endif |
| 18584 | 18614 | }; |
| 18585 | 18615 | |
| | @@ -19366,11 +19396,11 @@ |
| 19366 | 19396 | SQLITE_PRIVATE void sqlite3Dequote(char*); |
| 19367 | 19397 | SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*); |
| 19368 | 19398 | SQLITE_PRIVATE void sqlite3DequoteToken(Token*); |
| 19369 | 19399 | SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*); |
| 19370 | 19400 | SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int); |
| 19371 | | -SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **); |
| 19401 | +SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*); |
| 19372 | 19402 | SQLITE_PRIVATE void sqlite3FinishCoding(Parse*); |
| 19373 | 19403 | SQLITE_PRIVATE int sqlite3GetTempReg(Parse*); |
| 19374 | 19404 | SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int); |
| 19375 | 19405 | SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int); |
| 19376 | 19406 | SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int); |
| | @@ -19646,10 +19676,11 @@ |
| 19646 | 19676 | SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,const IdList*); |
| 19647 | 19677 | SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,const Select*,int); |
| 19648 | 19678 | SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*); |
| 19649 | 19679 | SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int); |
| 19650 | 19680 | SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8); |
| 19681 | +SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum*,sqlite3_value*); |
| 19651 | 19682 | SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void); |
| 19652 | 19683 | SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void); |
| 19653 | 19684 | SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*); |
| 19654 | 19685 | SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*); |
| 19655 | 19686 | SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*); |
| | @@ -19932,15 +19963,17 @@ |
| 19932 | 19963 | SQLITE_PRIVATE void sqlite3OomClear(sqlite3*); |
| 19933 | 19964 | SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int); |
| 19934 | 19965 | SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *); |
| 19935 | 19966 | |
| 19936 | 19967 | SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int); |
| 19968 | +SQLITE_PRIVATE int sqlite3StrAccumEnlarge(StrAccum*, int); |
| 19937 | 19969 | SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*); |
| 19938 | 19970 | SQLITE_PRIVATE void sqlite3StrAccumSetError(StrAccum*, u8); |
| 19939 | 19971 | SQLITE_PRIVATE void sqlite3ResultStrAccum(sqlite3_context*,StrAccum*); |
| 19940 | 19972 | SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int); |
| 19941 | 19973 | SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int); |
| 19974 | +SQLITE_PRIVATE void sqlite3RecordErrorByteOffset(sqlite3*,const char*); |
| 19942 | 19975 | |
| 19943 | 19976 | SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *); |
| 19944 | 19977 | SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *); |
| 19945 | 19978 | |
| 19946 | 19979 | #ifndef SQLITE_OMIT_SUBQUERY |
| | @@ -22181,11 +22214,11 @@ |
| 22181 | 22214 | SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, u32*); |
| 22182 | 22215 | SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*); |
| 22183 | 22216 | SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32); |
| 22184 | 22217 | SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8); |
| 22185 | 22218 | SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32); |
| 22186 | | -SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*); |
| 22219 | +SQLITE_PRIVATE void sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*); |
| 22187 | 22220 | SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int); |
| 22188 | 22221 | |
| 22189 | 22222 | int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *); |
| 22190 | 22223 | SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*); |
| 22191 | 22224 | SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*); |
| | @@ -23319,22 +23352,21 @@ |
| 23319 | 23352 | ** |
| 23320 | 23353 | ** Where NNN is an arbitrary floating-point number and "days" can be one |
| 23321 | 23354 | ** of several units of time. |
| 23322 | 23355 | */ |
| 23323 | 23356 | static const struct { |
| 23324 | | - u8 eType; /* Transformation type code */ |
| 23325 | | - u8 nName; /* Length of th name */ |
| 23326 | | - char *zName; /* Name of the transformation */ |
| 23327 | | - double rLimit; /* Maximum NNN value for this transform */ |
| 23328 | | - double rXform; /* Constant used for this transform */ |
| 23357 | + u8 nName; /* Length of the name */ |
| 23358 | + char zName[7]; /* Name of the transformation */ |
| 23359 | + float rLimit; /* Maximum NNN value for this transform */ |
| 23360 | + float rXform; /* Constant used for this transform */ |
| 23329 | 23361 | } aXformType[] = { |
| 23330 | | - { 0, 6, "second", 464269060800.0, 1000.0 }, |
| 23331 | | - { 0, 6, "minute", 7737817680.0, 60000.0 }, |
| 23332 | | - { 0, 4, "hour", 128963628.0, 3600000.0 }, |
| 23333 | | - { 0, 3, "day", 5373485.0, 86400000.0 }, |
| 23334 | | - { 1, 5, "month", 176546.0, 2592000000.0 }, |
| 23335 | | - { 2, 4, "year", 14713.0, 31536000000.0 }, |
| 23362 | + { 6, "second", 4.6427e+14, 1.0 }, |
| 23363 | + { 6, "minute", 7.7379e+12, 60.0 }, |
| 23364 | + { 4, "hour", 1.2897e+11, 3600.0 }, |
| 23365 | + { 3, "day", 5373485.0, 86400.0 }, |
| 23366 | + { 5, "month", 176546.0, 2592000.0 }, |
| 23367 | + { 4, "year", 14713.0, 31536000.0 }, |
| 23336 | 23368 | }; |
| 23337 | 23369 | |
| 23338 | 23370 | /* |
| 23339 | 23371 | ** Process a modifier to a date-time stamp. The modifiers are |
| 23340 | 23372 | ** as follows: |
| | @@ -23567,33 +23599,35 @@ |
| 23567 | 23599 | for(i=0; i<ArraySize(aXformType); i++){ |
| 23568 | 23600 | if( aXformType[i].nName==n |
| 23569 | 23601 | && sqlite3_strnicmp(aXformType[i].zName, z, n)==0 |
| 23570 | 23602 | && r>-aXformType[i].rLimit && r<aXformType[i].rLimit |
| 23571 | 23603 | ){ |
| 23572 | | - switch( aXformType[i].eType ){ |
| 23573 | | - case 1: { /* Special processing to add months */ |
| 23604 | + switch( i ){ |
| 23605 | + case 4: { /* Special processing to add months */ |
| 23574 | 23606 | int x; |
| 23607 | + assert( strcmp(aXformType[i].zName,"month")==0 ); |
| 23575 | 23608 | computeYMD_HMS(p); |
| 23576 | 23609 | p->M += (int)r; |
| 23577 | 23610 | x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; |
| 23578 | 23611 | p->Y += x; |
| 23579 | 23612 | p->M -= x*12; |
| 23580 | 23613 | p->validJD = 0; |
| 23581 | 23614 | r -= (int)r; |
| 23582 | 23615 | break; |
| 23583 | 23616 | } |
| 23584 | | - case 2: { /* Special processing to add years */ |
| 23617 | + case 5: { /* Special processing to add years */ |
| 23585 | 23618 | int y = (int)r; |
| 23619 | + assert( strcmp(aXformType[i].zName,"year")==0 ); |
| 23586 | 23620 | computeYMD_HMS(p); |
| 23587 | 23621 | p->Y += y; |
| 23588 | 23622 | p->validJD = 0; |
| 23589 | 23623 | r -= (int)r; |
| 23590 | 23624 | break; |
| 23591 | 23625 | } |
| 23592 | 23626 | } |
| 23593 | 23627 | computeJD(p); |
| 23594 | | - p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder); |
| 23628 | + p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder); |
| 23595 | 23629 | rc = 0; |
| 23596 | 23630 | break; |
| 23597 | 23631 | } |
| 23598 | 23632 | } |
| 23599 | 23633 | clearYMD_HMS_TZ(p); |
| | @@ -29835,10 +29869,11 @@ |
| 29835 | 29869 | if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; |
| 29836 | 29870 | pToken = va_arg(ap, Token*); |
| 29837 | 29871 | assert( bArgList==0 ); |
| 29838 | 29872 | if( pToken && pToken->n ){ |
| 29839 | 29873 | sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); |
| 29874 | + sqlite3RecordErrorByteOffset(pAccum->db, pToken->z); |
| 29840 | 29875 | } |
| 29841 | 29876 | length = width = 0; |
| 29842 | 29877 | break; |
| 29843 | 29878 | } |
| 29844 | 29879 | case etSRCITEM: { |
| | @@ -29889,18 +29924,42 @@ |
| 29889 | 29924 | zExtra = 0; |
| 29890 | 29925 | } |
| 29891 | 29926 | }/* End for loop over the format string */ |
| 29892 | 29927 | } /* End of function */ |
| 29893 | 29928 | |
| 29929 | + |
| 29930 | +/* |
| 29931 | +** The z string points to the first character of a token that is |
| 29932 | +** associated with an error. If db does not already have an error |
| 29933 | +** byte offset recorded, try to compute the error byte offset for |
| 29934 | +** z and set the error byte offset in db. |
| 29935 | +*/ |
| 29936 | +SQLITE_PRIVATE void sqlite3RecordErrorByteOffset(sqlite3 *db, const char *z){ |
| 29937 | + const Parse *pParse; |
| 29938 | + const char *zText; |
| 29939 | + const char *zEnd; |
| 29940 | + assert( z!=0 ); |
| 29941 | + if( NEVER(db==0) ) return; |
| 29942 | + if( db->errByteOffset!=(-2) ) return; |
| 29943 | + pParse = db->pParse; |
| 29944 | + if( NEVER(pParse==0) ) return; |
| 29945 | + zText =pParse->zTail; |
| 29946 | + if( NEVER(zText==0) ) return; |
| 29947 | + zEnd = &zText[strlen(zText)]; |
| 29948 | + if( SQLITE_WITHIN(z,zText,zEnd) ){ |
| 29949 | + db->errByteOffset = (int)(z-zText); |
| 29950 | + } |
| 29951 | +} |
| 29952 | + |
| 29894 | 29953 | /* |
| 29895 | 29954 | ** Enlarge the memory allocation on a StrAccum object so that it is |
| 29896 | 29955 | ** able to accept at least N more bytes of text. |
| 29897 | 29956 | ** |
| 29898 | 29957 | ** Return the number of bytes of text that StrAccum is able to accept |
| 29899 | 29958 | ** after the attempted enlargement. The value returned might be zero. |
| 29900 | 29959 | */ |
| 29901 | | -static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ |
| 29960 | +SQLITE_PRIVATE int sqlite3StrAccumEnlarge(StrAccum *p, int N){ |
| 29902 | 29961 | char *zNew; |
| 29903 | 29962 | assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ |
| 29904 | 29963 | if( p->accError ){ |
| 29905 | 29964 | testcase(p->accError==SQLITE_TOOBIG); |
| 29906 | 29965 | testcase(p->accError==SQLITE_NOMEM); |
| | @@ -32212,20 +32271,25 @@ |
| 32212 | 32271 | ** that would be appropriate. |
| 32213 | 32272 | */ |
| 32214 | 32273 | SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){ |
| 32215 | 32274 | assert( db!=0 ); |
| 32216 | 32275 | db->errCode = err_code; |
| 32217 | | - if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code); |
| 32276 | + if( err_code || db->pErr ){ |
| 32277 | + sqlite3ErrorFinish(db, err_code); |
| 32278 | + }else{ |
| 32279 | + db->errByteOffset = -1; |
| 32280 | + } |
| 32218 | 32281 | } |
| 32219 | 32282 | |
| 32220 | 32283 | /* |
| 32221 | 32284 | ** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state |
| 32222 | 32285 | ** and error message. |
| 32223 | 32286 | */ |
| 32224 | 32287 | SQLITE_PRIVATE void sqlite3ErrorClear(sqlite3 *db){ |
| 32225 | 32288 | assert( db!=0 ); |
| 32226 | 32289 | db->errCode = SQLITE_OK; |
| 32290 | + db->errByteOffset = -1; |
| 32227 | 32291 | if( db->pErr ) sqlite3ValueSetNull(db->pErr); |
| 32228 | 32292 | } |
| 32229 | 32293 | |
| 32230 | 32294 | /* |
| 32231 | 32295 | ** Load the sqlite3.iSysErrno field if that is an appropriate thing |
| | @@ -32242,21 +32306,12 @@ |
| 32242 | 32306 | /* |
| 32243 | 32307 | ** Set the most recent error code and error string for the sqlite |
| 32244 | 32308 | ** handle "db". The error code is set to "err_code". |
| 32245 | 32309 | ** |
| 32246 | 32310 | ** If it is not NULL, string zFormat specifies the format of the |
| 32247 | | -** error string in the style of the printf functions: The following |
| 32248 | | -** format characters are allowed: |
| 32249 | | -** |
| 32250 | | -** %s Insert a string |
| 32251 | | -** %z A string that should be freed after use |
| 32252 | | -** %d Insert an integer |
| 32253 | | -** %T Insert a token |
| 32254 | | -** %S Insert the first element of a SrcList |
| 32255 | | -** |
| 32256 | | -** zFormat and any string tokens that follow it are assumed to be |
| 32257 | | -** encoded in UTF-8. |
| 32311 | +** error string. zFormat and any string tokens that follow it are |
| 32312 | +** assumed to be encoded in UTF-8. |
| 32258 | 32313 | ** |
| 32259 | 32314 | ** To clear the most recent error for sqlite handle "db", sqlite3Error |
| 32260 | 32315 | ** should be called with err_code set to SQLITE_OK and zFormat set |
| 32261 | 32316 | ** to NULL. |
| 32262 | 32317 | */ |
| | @@ -32276,17 +32331,10 @@ |
| 32276 | 32331 | } |
| 32277 | 32332 | } |
| 32278 | 32333 | |
| 32279 | 32334 | /* |
| 32280 | 32335 | ** Add an error message to pParse->zErrMsg and increment pParse->nErr. |
| 32281 | | -** The following formatting characters are allowed: |
| 32282 | | -** |
| 32283 | | -** %s Insert a string |
| 32284 | | -** %z A string that should be freed after use |
| 32285 | | -** %d Insert an integer |
| 32286 | | -** %T Insert a token |
| 32287 | | -** %S Insert the first element of a SrcList |
| 32288 | 32336 | ** |
| 32289 | 32337 | ** This function should be used to report any error that occurs while |
| 32290 | 32338 | ** compiling an SQL statement (i.e. within sqlite3_prepare()). The |
| 32291 | 32339 | ** last thing the sqlite3_prepare() function does is copy the error |
| 32292 | 32340 | ** stored by this function into the database handle using sqlite3Error(). |
| | @@ -32295,13 +32343,15 @@ |
| 32295 | 32343 | */ |
| 32296 | 32344 | SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ |
| 32297 | 32345 | char *zMsg; |
| 32298 | 32346 | va_list ap; |
| 32299 | 32347 | sqlite3 *db = pParse->db; |
| 32348 | + db->errByteOffset = -2; |
| 32300 | 32349 | va_start(ap, zFormat); |
| 32301 | 32350 | zMsg = sqlite3VMPrintf(db, zFormat, ap); |
| 32302 | 32351 | va_end(ap); |
| 32352 | + if( db->errByteOffset<-1 ) db->errByteOffset = -1; |
| 32303 | 32353 | if( db->suppressErr ){ |
| 32304 | 32354 | sqlite3DbFree(db, zMsg); |
| 32305 | 32355 | }else{ |
| 32306 | 32356 | pParse->nErr++; |
| 32307 | 32357 | sqlite3DbFree(db, pParse->zErrMsg); |
| | @@ -66875,11 +66925,11 @@ |
| 66875 | 66925 | |
| 66876 | 66926 | pInfo->nKey = *(i64*)&iKey; |
| 66877 | 66927 | pInfo->nPayload = nPayload; |
| 66878 | 66928 | pInfo->pPayload = pIter; |
| 66879 | 66929 | testcase( nPayload==pPage->maxLocal ); |
| 66880 | | - testcase( nPayload==pPage->maxLocal+1 ); |
| 66930 | + testcase( nPayload==(u32)pPage->maxLocal+1 ); |
| 66881 | 66931 | if( nPayload<=pPage->maxLocal ){ |
| 66882 | 66932 | /* This is the (easy) common case where the entire payload fits |
| 66883 | 66933 | ** on the local page. No overflow is required. |
| 66884 | 66934 | */ |
| 66885 | 66935 | pInfo->nSize = nPayload + (u16)(pIter - pCell); |
| | @@ -66912,11 +66962,11 @@ |
| 66912 | 66962 | pIter++; |
| 66913 | 66963 | pInfo->nKey = nPayload; |
| 66914 | 66964 | pInfo->nPayload = nPayload; |
| 66915 | 66965 | pInfo->pPayload = pIter; |
| 66916 | 66966 | testcase( nPayload==pPage->maxLocal ); |
| 66917 | | - testcase( nPayload==pPage->maxLocal+1 ); |
| 66967 | + testcase( nPayload==(u32)pPage->maxLocal+1 ); |
| 66918 | 66968 | if( nPayload<=pPage->maxLocal ){ |
| 66919 | 66969 | /* This is the (easy) common case where the entire payload fits |
| 66920 | 66970 | ** on the local page. No overflow is required. |
| 66921 | 66971 | */ |
| 66922 | 66972 | pInfo->nSize = nPayload + (u16)(pIter - pCell); |
| | @@ -66975,19 +67025,19 @@ |
| 66975 | 67025 | ** past the end of the key value. */ |
| 66976 | 67026 | pEnd = &pIter[9]; |
| 66977 | 67027 | while( (*pIter++)&0x80 && pIter<pEnd ); |
| 66978 | 67028 | } |
| 66979 | 67029 | testcase( nSize==pPage->maxLocal ); |
| 66980 | | - testcase( nSize==pPage->maxLocal+1 ); |
| 67030 | + testcase( nSize==(u32)pPage->maxLocal+1 ); |
| 66981 | 67031 | if( nSize<=pPage->maxLocal ){ |
| 66982 | 67032 | nSize += (u32)(pIter - pCell); |
| 66983 | 67033 | if( nSize<4 ) nSize = 4; |
| 66984 | 67034 | }else{ |
| 66985 | 67035 | int minLocal = pPage->minLocal; |
| 66986 | 67036 | nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4); |
| 66987 | 67037 | testcase( nSize==pPage->maxLocal ); |
| 66988 | | - testcase( nSize==pPage->maxLocal+1 ); |
| 67038 | + testcase( nSize==(u32)pPage->maxLocal+1 ); |
| 66989 | 67039 | if( nSize>pPage->maxLocal ){ |
| 66990 | 67040 | nSize = minLocal; |
| 66991 | 67041 | } |
| 66992 | 67042 | nSize += 4 + (u16)(pIter - pCell); |
| 66993 | 67043 | } |
| | @@ -69882,11 +69932,11 @@ |
| 69882 | 69932 | */ |
| 69883 | 69933 | static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){ |
| 69884 | 69934 | int nPage = get4byte(&pPage1->aData[28]); |
| 69885 | 69935 | testcase( nPage==0 ); |
| 69886 | 69936 | if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 69887 | | - testcase( pBt->nPage!=nPage ); |
| 69937 | + testcase( pBt->nPage!=(u32)nPage ); |
| 69888 | 69938 | pBt->nPage = nPage; |
| 69889 | 69939 | } |
| 69890 | 69940 | |
| 69891 | 69941 | /* |
| 69892 | 69942 | ** Rollback the transaction in progress. |
| | @@ -70903,11 +70953,11 @@ |
| 70903 | 70953 | if( pCur->iPage ){ |
| 70904 | 70954 | releasePageNotNull(pCur->pPage); |
| 70905 | 70955 | while( --pCur->iPage ){ |
| 70906 | 70956 | releasePageNotNull(pCur->apPage[pCur->iPage]); |
| 70907 | 70957 | } |
| 70908 | | - pCur->pPage = pCur->apPage[0]; |
| 70958 | + pRoot = pCur->pPage = pCur->apPage[0]; |
| 70909 | 70959 | goto skip_init; |
| 70910 | 70960 | } |
| 70911 | 70961 | }else if( pCur->pgnoRoot==0 ){ |
| 70912 | 70962 | pCur->eState = CURSOR_INVALID; |
| 70913 | 70963 | return SQLITE_EMPTY; |
| | @@ -70950,11 +71000,10 @@ |
| 70950 | 71000 | skip_init: |
| 70951 | 71001 | pCur->ix = 0; |
| 70952 | 71002 | pCur->info.nSize = 0; |
| 70953 | 71003 | pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl); |
| 70954 | 71004 | |
| 70955 | | - pRoot = pCur->pPage; |
| 70956 | 71005 | if( pRoot->nCell>0 ){ |
| 70957 | 71006 | pCur->eState = CURSOR_VALID; |
| 70958 | 71007 | }else if( !pRoot->leaf ){ |
| 70959 | 71008 | Pgno subpage; |
| 70960 | 71009 | if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT; |
| | @@ -72458,11 +72507,11 @@ |
| 72458 | 72507 | assert( pPage->nFree>=0 ); |
| 72459 | 72508 | data = pPage->aData; |
| 72460 | 72509 | ptr = &pPage->aCellIdx[2*idx]; |
| 72461 | 72510 | pc = get2byte(ptr); |
| 72462 | 72511 | hdr = pPage->hdrOffset; |
| 72463 | | - testcase( pc==get2byte(&data[hdr+5]) ); |
| 72512 | + testcase( pc==(u32)get2byte(&data[hdr+5]) ); |
| 72464 | 72513 | testcase( pc+sz==pPage->pBt->usableSize ); |
| 72465 | 72514 | if( pc+sz > pPage->pBt->usableSize ){ |
| 72466 | 72515 | *pRC = SQLITE_CORRUPT_BKPT; |
| 72467 | 72516 | return; |
| 72468 | 72517 | } |
| | @@ -83085,18 +83134,18 @@ |
| 83085 | 83134 | #define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) |
| 83086 | 83135 | #define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) |
| 83087 | 83136 | |
| 83088 | 83137 | /* |
| 83089 | 83138 | ** Deserialize the data blob pointed to by buf as serial type serial_type |
| 83090 | | -** and store the result in pMem. Return the number of bytes read. |
| 83139 | +** and store the result in pMem. |
| 83091 | 83140 | ** |
| 83092 | 83141 | ** This function is implemented as two separate routines for performance. |
| 83093 | 83142 | ** The few cases that require local variables are broken out into a separate |
| 83094 | 83143 | ** routine so that in most cases the overhead of moving the stack pointer |
| 83095 | 83144 | ** is avoided. |
| 83096 | 83145 | */ |
| 83097 | | -static u32 serialGet( |
| 83146 | +static void serialGet( |
| 83098 | 83147 | const unsigned char *buf, /* Buffer to deserialize from */ |
| 83099 | 83148 | u32 serial_type, /* Serial type to deserialize */ |
| 83100 | 83149 | Mem *pMem /* Memory cell to write value into */ |
| 83101 | 83150 | ){ |
| 83102 | 83151 | u64 x = FOUR_BYTE_UINT(buf); |
| | @@ -83126,13 +83175,12 @@ |
| 83126 | 83175 | assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 ); |
| 83127 | 83176 | swapMixedEndianFloat(x); |
| 83128 | 83177 | memcpy(&pMem->u.r, &x, sizeof(x)); |
| 83129 | 83178 | pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real; |
| 83130 | 83179 | } |
| 83131 | | - return 8; |
| 83132 | 83180 | } |
| 83133 | | -SQLITE_PRIVATE u32 sqlite3VdbeSerialGet( |
| 83181 | +SQLITE_PRIVATE void sqlite3VdbeSerialGet( |
| 83134 | 83182 | const unsigned char *buf, /* Buffer to deserialize from */ |
| 83135 | 83183 | u32 serial_type, /* Serial type to deserialize */ |
| 83136 | 83184 | Mem *pMem /* Memory cell to write value into */ |
| 83137 | 83185 | ){ |
| 83138 | 83186 | switch( serial_type ){ |
| | @@ -83139,41 +83187,41 @@ |
| 83139 | 83187 | case 10: { /* Internal use only: NULL with virtual table |
| 83140 | 83188 | ** UPDATE no-change flag set */ |
| 83141 | 83189 | pMem->flags = MEM_Null|MEM_Zero; |
| 83142 | 83190 | pMem->n = 0; |
| 83143 | 83191 | pMem->u.nZero = 0; |
| 83144 | | - break; |
| 83192 | + return; |
| 83145 | 83193 | } |
| 83146 | 83194 | case 11: /* Reserved for future use */ |
| 83147 | 83195 | case 0: { /* Null */ |
| 83148 | 83196 | /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */ |
| 83149 | 83197 | pMem->flags = MEM_Null; |
| 83150 | | - break; |
| 83198 | + return; |
| 83151 | 83199 | } |
| 83152 | 83200 | case 1: { |
| 83153 | 83201 | /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement |
| 83154 | 83202 | ** integer. */ |
| 83155 | 83203 | pMem->u.i = ONE_BYTE_INT(buf); |
| 83156 | 83204 | pMem->flags = MEM_Int; |
| 83157 | 83205 | testcase( pMem->u.i<0 ); |
| 83158 | | - return 1; |
| 83206 | + return; |
| 83159 | 83207 | } |
| 83160 | 83208 | case 2: { /* 2-byte signed integer */ |
| 83161 | 83209 | /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit |
| 83162 | 83210 | ** twos-complement integer. */ |
| 83163 | 83211 | pMem->u.i = TWO_BYTE_INT(buf); |
| 83164 | 83212 | pMem->flags = MEM_Int; |
| 83165 | 83213 | testcase( pMem->u.i<0 ); |
| 83166 | | - return 2; |
| 83214 | + return; |
| 83167 | 83215 | } |
| 83168 | 83216 | case 3: { /* 3-byte signed integer */ |
| 83169 | 83217 | /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit |
| 83170 | 83218 | ** twos-complement integer. */ |
| 83171 | 83219 | pMem->u.i = THREE_BYTE_INT(buf); |
| 83172 | 83220 | pMem->flags = MEM_Int; |
| 83173 | 83221 | testcase( pMem->u.i<0 ); |
| 83174 | | - return 3; |
| 83222 | + return; |
| 83175 | 83223 | } |
| 83176 | 83224 | case 4: { /* 4-byte signed integer */ |
| 83177 | 83225 | /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit |
| 83178 | 83226 | ** twos-complement integer. */ |
| 83179 | 83227 | pMem->u.i = FOUR_BYTE_INT(buf); |
| | @@ -83181,33 +83229,34 @@ |
| 83181 | 83229 | /* Work around a sign-extension bug in the HP compiler for HP/UX */ |
| 83182 | 83230 | if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL; |
| 83183 | 83231 | #endif |
| 83184 | 83232 | pMem->flags = MEM_Int; |
| 83185 | 83233 | testcase( pMem->u.i<0 ); |
| 83186 | | - return 4; |
| 83234 | + return; |
| 83187 | 83235 | } |
| 83188 | 83236 | case 5: { /* 6-byte signed integer */ |
| 83189 | 83237 | /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit |
| 83190 | 83238 | ** twos-complement integer. */ |
| 83191 | 83239 | pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); |
| 83192 | 83240 | pMem->flags = MEM_Int; |
| 83193 | 83241 | testcase( pMem->u.i<0 ); |
| 83194 | | - return 6; |
| 83242 | + return; |
| 83195 | 83243 | } |
| 83196 | 83244 | case 6: /* 8-byte signed integer */ |
| 83197 | 83245 | case 7: { /* IEEE floating point */ |
| 83198 | 83246 | /* These use local variables, so do them in a separate routine |
| 83199 | 83247 | ** to avoid having to move the frame pointer in the common case */ |
| 83200 | | - return serialGet(buf,serial_type,pMem); |
| 83248 | + serialGet(buf,serial_type,pMem); |
| 83249 | + return; |
| 83201 | 83250 | } |
| 83202 | 83251 | case 8: /* Integer 0 */ |
| 83203 | 83252 | case 9: { /* Integer 1 */ |
| 83204 | 83253 | /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */ |
| 83205 | 83254 | /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */ |
| 83206 | 83255 | pMem->u.i = serial_type-8; |
| 83207 | 83256 | pMem->flags = MEM_Int; |
| 83208 | | - return 0; |
| 83257 | + return; |
| 83209 | 83258 | } |
| 83210 | 83259 | default: { |
| 83211 | 83260 | /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in |
| 83212 | 83261 | ** length. |
| 83213 | 83262 | ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and |
| | @@ -83214,14 +83263,14 @@ |
| 83214 | 83263 | ** (N-13)/2 bytes in length. */ |
| 83215 | 83264 | static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem }; |
| 83216 | 83265 | pMem->z = (char *)buf; |
| 83217 | 83266 | pMem->n = (serial_type-12)/2; |
| 83218 | 83267 | pMem->flags = aFlag[serial_type&1]; |
| 83219 | | - return pMem->n; |
| 83268 | + return; |
| 83220 | 83269 | } |
| 83221 | 83270 | } |
| 83222 | | - return 0; |
| 83271 | + return; |
| 83223 | 83272 | } |
| 83224 | 83273 | /* |
| 83225 | 83274 | ** This routine is used to allocate sufficient space for an UnpackedRecord |
| 83226 | 83275 | ** structure large enough to be used with sqlite3VdbeRecordUnpack() if |
| 83227 | 83276 | ** the first argument is a pointer to KeyInfo structure pKeyInfo. |
| | @@ -83280,11 +83329,12 @@ |
| 83280 | 83329 | pMem->enc = pKeyInfo->enc; |
| 83281 | 83330 | pMem->db = pKeyInfo->db; |
| 83282 | 83331 | /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */ |
| 83283 | 83332 | pMem->szMalloc = 0; |
| 83284 | 83333 | pMem->z = 0; |
| 83285 | | - d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); |
| 83334 | + sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); |
| 83335 | + d += sqlite3VdbeSerialTypeLen(serial_type); |
| 83286 | 83336 | pMem++; |
| 83287 | 83337 | if( (++u)>=p->nField ) break; |
| 83288 | 83338 | } |
| 83289 | 83339 | if( d>(u32)nKey && u ){ |
| 83290 | 83340 | assert( CORRUPT_DB ); |
| | @@ -83364,11 +83414,12 @@ |
| 83364 | 83414 | break; |
| 83365 | 83415 | } |
| 83366 | 83416 | |
| 83367 | 83417 | /* Extract the values to be compared. |
| 83368 | 83418 | */ |
| 83369 | | - d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); |
| 83419 | + sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); |
| 83420 | + d1 += sqlite3VdbeSerialTypeLen(serial_type1); |
| 83370 | 83421 | |
| 83371 | 83422 | /* Do the comparison |
| 83372 | 83423 | */ |
| 83373 | 83424 | rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], |
| 83374 | 83425 | pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0); |
| | @@ -84168,11 +84219,11 @@ |
| 84168 | 84219 | } |
| 84169 | 84220 | |
| 84170 | 84221 | /* The index entry must begin with a header size */ |
| 84171 | 84222 | getVarint32NR((u8*)m.z, szHdr); |
| 84172 | 84223 | testcase( szHdr==3 ); |
| 84173 | | - testcase( szHdr==m.n ); |
| 84224 | + testcase( szHdr==(u32)m.n ); |
| 84174 | 84225 | testcase( szHdr>0x7fffffff ); |
| 84175 | 84226 | assert( m.n>=0 ); |
| 84176 | 84227 | if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){ |
| 84177 | 84228 | goto idx_rowid_corruption; |
| 84178 | 84229 | } |
| | @@ -87446,11 +87497,10 @@ |
| 87446 | 87497 | int i, mx; |
| 87447 | 87498 | u64 h = 0; |
| 87448 | 87499 | |
| 87449 | 87500 | i = pOp->p3; |
| 87450 | 87501 | assert( pOp->p4type==P4_INT32 ); |
| 87451 | | - mx = i + pOp->p4.i; |
| 87452 | 87502 | for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){ |
| 87453 | 87503 | const Mem *p = &aMem[i]; |
| 87454 | 87504 | if( p->flags & (MEM_Int|MEM_IntReal) ){ |
| 87455 | 87505 | h += p->u.i; |
| 87456 | 87506 | }else if( p->flags & MEM_Real ){ |
| | @@ -100064,12 +100114,13 @@ |
| 100064 | 100114 | hit = 1; |
| 100065 | 100115 | } |
| 100066 | 100116 | } |
| 100067 | 100117 | if( hit || zTab==0 ) continue; |
| 100068 | 100118 | } |
| 100069 | | - if( zDb && pTab->pSchema!=pSchema ){ |
| 100070 | | - continue; |
| 100119 | + if( zDb ){ |
| 100120 | + if( pTab->pSchema!=pSchema ) continue; |
| 100121 | + if( pSchema==0 && strcmp(zDb,"*")!=0 ) continue; |
| 100071 | 100122 | } |
| 100072 | 100123 | if( zTab ){ |
| 100073 | 100124 | const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName; |
| 100074 | 100125 | assert( zTabName!=0 ); |
| 100075 | 100126 | if( sqlite3StrICmp(zTabName, zTab)!=0 ){ |
| | @@ -100196,10 +100247,11 @@ |
| 100196 | 100247 | { |
| 100197 | 100248 | assert( ExprUseYTab(pExpr) ); |
| 100198 | 100249 | pExpr->y.pTab = pTab; |
| 100199 | 100250 | if( pParse->bReturning ){ |
| 100200 | 100251 | eNewExprOp = TK_REGISTER; |
| 100252 | + pExpr->op2 = TK_COLUMN; |
| 100201 | 100253 | pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable + |
| 100202 | 100254 | sqlite3TableColumnToStorage(pTab, iCol) + 1; |
| 100203 | 100255 | }else{ |
| 100204 | 100256 | pExpr->iColumn = (i16)iCol; |
| 100205 | 100257 | eNewExprOp = TK_TRIGGER; |
| | @@ -109275,11 +109327,10 @@ |
| 109275 | 109327 | sqlite3 *db, /* Database handle */ |
| 109276 | 109328 | const char *zSql, /* SQL to parse */ |
| 109277 | 109329 | int bTemp /* True if SQL is from temp schema */ |
| 109278 | 109330 | ){ |
| 109279 | 109331 | int rc; |
| 109280 | | - char *zErr = 0; |
| 109281 | 109332 | |
| 109282 | 109333 | db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb); |
| 109283 | 109334 | |
| 109284 | 109335 | /* Parse the SQL statement passed as the first argument. If no error |
| 109285 | 109336 | ** occurs and the parse does not result in a new table, index or |
| | @@ -109286,14 +109337,11 @@ |
| 109286 | 109337 | ** trigger object, the database must be corrupt. */ |
| 109287 | 109338 | memset(p, 0, sizeof(Parse)); |
| 109288 | 109339 | p->eParseMode = PARSE_MODE_RENAME; |
| 109289 | 109340 | p->db = db; |
| 109290 | 109341 | p->nQueryLoop = 1; |
| 109291 | | - rc = zSql ? sqlite3RunParser(p, zSql, &zErr) : SQLITE_NOMEM; |
| 109292 | | - assert( p->zErrMsg==0 ); |
| 109293 | | - assert( rc!=SQLITE_OK || zErr==0 ); |
| 109294 | | - p->zErrMsg = zErr; |
| 109342 | + rc = zSql ? sqlite3RunParser(p, zSql) : SQLITE_NOMEM; |
| 109295 | 109343 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
| 109296 | 109344 | if( rc==SQLITE_OK |
| 109297 | 109345 | && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0 |
| 109298 | 109346 | ){ |
| 109299 | 109347 | rc = SQLITE_CORRUPT_BKPT; |
| | @@ -113496,11 +113544,10 @@ |
| 113496 | 113544 | ** built-in function. |
| 113497 | 113545 | */ |
| 113498 | 113546 | SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ |
| 113499 | 113547 | va_list ap; |
| 113500 | 113548 | char *zSql; |
| 113501 | | - char *zErrMsg = 0; |
| 113502 | 113549 | sqlite3 *db = pParse->db; |
| 113503 | 113550 | u32 savedDbFlags = db->mDbFlags; |
| 113504 | 113551 | char saveBuf[PARSE_TAIL_SZ]; |
| 113505 | 113552 | |
| 113506 | 113553 | if( pParse->nErr ) return; |
| | @@ -113518,13 +113565,12 @@ |
| 113518 | 113565 | } |
| 113519 | 113566 | pParse->nested++; |
| 113520 | 113567 | memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ); |
| 113521 | 113568 | memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); |
| 113522 | 113569 | db->mDbFlags |= DBFLAG_PreferBuiltin; |
| 113523 | | - sqlite3RunParser(pParse, zSql, &zErrMsg); |
| 113570 | + sqlite3RunParser(pParse, zSql); |
| 113524 | 113571 | db->mDbFlags = savedDbFlags; |
| 113525 | | - sqlite3DbFree(db, zErrMsg); |
| 113526 | 113572 | sqlite3DbFree(db, zSql); |
| 113527 | 113573 | memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ); |
| 113528 | 113574 | pParse->nested--; |
| 113529 | 113575 | } |
| 113530 | 113576 | |
| | @@ -121372,87 +121418,95 @@ |
| 121372 | 121418 | '0', '1', '2', '3', '4', '5', '6', '7', |
| 121373 | 121419 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 121374 | 121420 | }; |
| 121375 | 121421 | |
| 121376 | 121422 | /* |
| 121377 | | -** Implementation of the QUOTE() function. This function takes a single |
| 121378 | | -** argument. If the argument is numeric, the return value is the same as |
| 121379 | | -** the argument. If the argument is NULL, the return value is the string |
| 121380 | | -** "NULL". Otherwise, the argument is enclosed in single quotes with |
| 121381 | | -** single-quote escapes. |
| 121423 | +** Append to pStr text that is the SQL literal representation of the |
| 121424 | +** value contained in pValue. |
| 121382 | 121425 | */ |
| 121383 | | -static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 121384 | | - assert( argc==1 ); |
| 121385 | | - UNUSED_PARAMETER(argc); |
| 121386 | | - switch( sqlite3_value_type(argv[0]) ){ |
| 121426 | +SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue){ |
| 121427 | + /* As currently implemented, the string must be initially empty. |
| 121428 | + ** we might relax this requirement in the future, but that will |
| 121429 | + ** require enhancements to the implementation. */ |
| 121430 | + assert( pStr!=0 && pStr->nChar==0 ); |
| 121431 | + |
| 121432 | + switch( sqlite3_value_type(pValue) ){ |
| 121387 | 121433 | case SQLITE_FLOAT: { |
| 121388 | 121434 | double r1, r2; |
| 121389 | | - char zBuf[50]; |
| 121390 | | - r1 = sqlite3_value_double(argv[0]); |
| 121391 | | - sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1); |
| 121392 | | - sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8); |
| 121393 | | - if( r1!=r2 ){ |
| 121394 | | - sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1); |
| 121395 | | - } |
| 121396 | | - sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
| 121435 | + const char *zVal; |
| 121436 | + r1 = sqlite3_value_double(pValue); |
| 121437 | + sqlite3_str_appendf(pStr, "%!.15g", r1); |
| 121438 | + zVal = sqlite3_str_value(pStr); |
| 121439 | + if( zVal ){ |
| 121440 | + sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8); |
| 121441 | + if( r1!=r2 ){ |
| 121442 | + sqlite3_str_reset(pStr); |
| 121443 | + sqlite3_str_appendf(pStr, "%!.20e", r1); |
| 121444 | + } |
| 121445 | + } |
| 121397 | 121446 | break; |
| 121398 | 121447 | } |
| 121399 | 121448 | case SQLITE_INTEGER: { |
| 121400 | | - sqlite3_result_value(context, argv[0]); |
| 121449 | + sqlite3_str_appendf(pStr, "%lld", sqlite3_value_int64(pValue)); |
| 121401 | 121450 | break; |
| 121402 | 121451 | } |
| 121403 | 121452 | case SQLITE_BLOB: { |
| 121404 | | - char *zText = 0; |
| 121405 | | - char const *zBlob = sqlite3_value_blob(argv[0]); |
| 121406 | | - int nBlob = sqlite3_value_bytes(argv[0]); |
| 121407 | | - assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ |
| 121408 | | - zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); |
| 121409 | | - if( zText ){ |
| 121453 | + char const *zBlob = sqlite3_value_blob(pValue); |
| 121454 | + int nBlob = sqlite3_value_bytes(pValue); |
| 121455 | + assert( zBlob==sqlite3_value_blob(pValue) ); /* No encoding change */ |
| 121456 | + sqlite3StrAccumEnlarge(pStr, nBlob*2 + 4); |
| 121457 | + if( pStr->accError==0 ){ |
| 121458 | + char *zText = pStr->zText; |
| 121410 | 121459 | int i; |
| 121411 | 121460 | for(i=0; i<nBlob; i++){ |
| 121412 | 121461 | zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; |
| 121413 | 121462 | zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; |
| 121414 | 121463 | } |
| 121415 | 121464 | zText[(nBlob*2)+2] = '\''; |
| 121416 | 121465 | zText[(nBlob*2)+3] = '\0'; |
| 121417 | 121466 | zText[0] = 'X'; |
| 121418 | 121467 | zText[1] = '\''; |
| 121419 | | - sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); |
| 121420 | | - sqlite3_free(zText); |
| 121468 | + pStr->nChar = nBlob*2 + 3; |
| 121421 | 121469 | } |
| 121422 | 121470 | break; |
| 121423 | 121471 | } |
| 121424 | 121472 | case SQLITE_TEXT: { |
| 121425 | | - int i,j; |
| 121426 | | - u64 n; |
| 121427 | | - const unsigned char *zArg = sqlite3_value_text(argv[0]); |
| 121428 | | - char *z; |
| 121429 | | - |
| 121430 | | - if( zArg==0 ) return; |
| 121431 | | - for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } |
| 121432 | | - z = contextMalloc(context, ((i64)i)+((i64)n)+3); |
| 121433 | | - if( z ){ |
| 121434 | | - z[0] = '\''; |
| 121435 | | - for(i=0, j=1; zArg[i]; i++){ |
| 121436 | | - z[j++] = zArg[i]; |
| 121437 | | - if( zArg[i]=='\'' ){ |
| 121438 | | - z[j++] = '\''; |
| 121439 | | - } |
| 121440 | | - } |
| 121441 | | - z[j++] = '\''; |
| 121442 | | - z[j] = 0; |
| 121443 | | - sqlite3_result_text(context, z, j, sqlite3_free); |
| 121444 | | - } |
| 121473 | + const unsigned char *zArg = sqlite3_value_text(pValue); |
| 121474 | + sqlite3_str_appendf(pStr, "%Q", zArg); |
| 121445 | 121475 | break; |
| 121446 | 121476 | } |
| 121447 | 121477 | default: { |
| 121448 | | - assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); |
| 121449 | | - sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); |
| 121478 | + assert( sqlite3_value_type(pValue)==SQLITE_NULL ); |
| 121479 | + sqlite3_str_append(pStr, "NULL", 4); |
| 121450 | 121480 | break; |
| 121451 | 121481 | } |
| 121452 | 121482 | } |
| 121453 | 121483 | } |
| 121484 | + |
| 121485 | +/* |
| 121486 | +** Implementation of the QUOTE() function. |
| 121487 | +** |
| 121488 | +** The quote(X) function returns the text of an SQL literal which is the |
| 121489 | +** value of its argument suitable for inclusion into an SQL statement. |
| 121490 | +** Strings are surrounded by single-quotes with escapes on interior quotes |
| 121491 | +** as needed. BLOBs are encoded as hexadecimal literals. Strings with |
| 121492 | +** embedded NUL characters cannot be represented as string literals in SQL |
| 121493 | +** and hence the returned string literal is truncated prior to the first NUL. |
| 121494 | +*/ |
| 121495 | +static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 121496 | + sqlite3_str str; |
| 121497 | + sqlite3 *db = sqlite3_context_db_handle(context); |
| 121498 | + assert( argc==1 ); |
| 121499 | + UNUSED_PARAMETER(argc); |
| 121500 | + sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); |
| 121501 | + sqlite3QuoteValue(&str,argv[0]); |
| 121502 | + sqlite3_result_text(context, sqlite3StrAccumFinish(&str), str.nChar, |
| 121503 | + SQLITE_DYNAMIC); |
| 121504 | + if( str.accError==SQLITE_NOMEM ){ |
| 121505 | + sqlite3_result_error_nomem(context); |
| 121506 | + } |
| 121507 | +} |
| 121454 | 121508 | |
| 121455 | 121509 | /* |
| 121456 | 121510 | ** The unicode() function. Return the integer unicode code-point value |
| 121457 | 121511 | ** for the first character of the input string. |
| 121458 | 121512 | */ |
| | @@ -126174,10 +126228,11 @@ |
| 126174 | 126228 | ** the UNIQUE constraints have run. |
| 126175 | 126229 | */ |
| 126176 | 126230 | if( onError==OE_Replace /* IPK rule is REPLACE */ |
| 126177 | 126231 | && onError!=overrideError /* Rules for other constraints are different */ |
| 126178 | 126232 | && pTab->pIndex /* There exist other constraints */ |
| 126233 | + && !upsertIpkDelay /* IPK check already deferred by UPSERT */ |
| 126179 | 126234 | ){ |
| 126180 | 126235 | ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1; |
| 126181 | 126236 | VdbeComment((v, "defer IPK REPLACE until last")); |
| 126182 | 126237 | } |
| 126183 | 126238 | |
| | @@ -126582,10 +126637,11 @@ |
| 126582 | 126637 | |
| 126583 | 126638 | /* If the IPK constraint is a REPLACE, run it last */ |
| 126584 | 126639 | if( ipkTop ){ |
| 126585 | 126640 | sqlite3VdbeGoto(v, ipkTop); |
| 126586 | 126641 | VdbeComment((v, "Do IPK REPLACE")); |
| 126642 | + assert( ipkBottom>0 ); |
| 126587 | 126643 | sqlite3VdbeJumpHere(v, ipkBottom); |
| 126588 | 126644 | } |
| 126589 | 126645 | |
| 126590 | 126646 | /* Recheck all uniqueness constraints after replace triggers have run */ |
| 126591 | 126647 | testcase( regTrigCnt!=0 && nReplaceTrig==0 ); |
| | @@ -127802,10 +127858,12 @@ |
| 127802 | 127858 | sqlite3_int64 (*total_changes64)(sqlite3*); |
| 127803 | 127859 | /* Version 3.37.0 and later */ |
| 127804 | 127860 | int (*autovacuum_pages)(sqlite3*, |
| 127805 | 127861 | unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int), |
| 127806 | 127862 | void*, void(*)(void*)); |
| 127863 | + /* Version 3.38.0 and later */ |
| 127864 | + int (*error_offset)(sqlite3*); |
| 127807 | 127865 | }; |
| 127808 | 127866 | |
| 127809 | 127867 | /* |
| 127810 | 127868 | ** This is the function signature used for all extension entry points. It |
| 127811 | 127869 | ** is also defined in the file "loadext.c". |
| | @@ -128113,10 +128171,12 @@ |
| 128113 | 128171 | /* Version 3.36.1 and later */ |
| 128114 | 128172 | #define sqlite3_changes64 sqlite3_api->changes64 |
| 128115 | 128173 | #define sqlite3_total_changes64 sqlite3_api->total_changes64 |
| 128116 | 128174 | /* Version 3.37.0 and later */ |
| 128117 | 128175 | #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages |
| 128176 | +/* Version 3.38.0 and later */ |
| 128177 | +#define sqlite3_error_offset sqlite3_api->error_offset |
| 128118 | 128178 | #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ |
| 128119 | 128179 | |
| 128120 | 128180 | #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 128121 | 128181 | /* This case when the file really is being compiled as a loadable |
| 128122 | 128182 | ** extension */ |
| | @@ -128602,10 +128662,12 @@ |
| 128602 | 128662 | /* Version 3.36.1 and later */ |
| 128603 | 128663 | sqlite3_changes64, |
| 128604 | 128664 | sqlite3_total_changes64, |
| 128605 | 128665 | /* Version 3.37.0 and later */ |
| 128606 | 128666 | sqlite3_autovacuum_pages, |
| 128667 | + /* Version 3.38.0 and later */ |
| 128668 | + sqlite3_error_offset, |
| 128607 | 128669 | }; |
| 128608 | 128670 | |
| 128609 | 128671 | /* True if x is the directory separator character |
| 128610 | 128672 | */ |
| 128611 | 128673 | #if SQLITE_OS_WIN |
| | @@ -132941,10 +133003,14 @@ |
| 132941 | 133003 | /* |
| 132942 | 133004 | ** Free all memory allocations in the pParse object |
| 132943 | 133005 | */ |
| 132944 | 133006 | SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){ |
| 132945 | 133007 | sqlite3 *db = pParse->db; |
| 133008 | + assert( pParse->nested==0 ); |
| 133009 | +#ifndef SQLITE_OMIT_SHARED_CACHE |
| 133010 | + sqlite3DbFree(db, pParse->aTableLock); |
| 133011 | +#endif |
| 132946 | 133012 | while( pParse->pCleanup ){ |
| 132947 | 133013 | ParseCleanup *pCleanup = pParse->pCleanup; |
| 132948 | 133014 | pParse->pCleanup = pCleanup->pNext; |
| 132949 | 133015 | pCleanup->xCleanup(db, pCleanup->pPtr); |
| 132950 | 133016 | sqlite3DbFreeNN(db, pCleanup); |
| | @@ -133020,11 +133086,10 @@ |
| 133020 | 133086 | u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ |
| 133021 | 133087 | Vdbe *pReprepare, /* VM being reprepared */ |
| 133022 | 133088 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 133023 | 133089 | const char **pzTail /* OUT: End of parsed string */ |
| 133024 | 133090 | ){ |
| 133025 | | - char *zErrMsg = 0; /* Error message */ |
| 133026 | 133091 | int rc = SQLITE_OK; /* Result code */ |
| 133027 | 133092 | int i; /* Loop counter */ |
| 133028 | 133093 | Parse sParse; /* Parsing context */ |
| 133029 | 133094 | |
| 133030 | 133095 | memset(&sParse, 0, PARSE_HDR_SZ); |
| | @@ -133095,18 +133160,18 @@ |
| 133095 | 133160 | rc = sqlite3ApiExit(db, SQLITE_TOOBIG); |
| 133096 | 133161 | goto end_prepare; |
| 133097 | 133162 | } |
| 133098 | 133163 | zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); |
| 133099 | 133164 | if( zSqlCopy ){ |
| 133100 | | - sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg); |
| 133165 | + sqlite3RunParser(&sParse, zSqlCopy); |
| 133101 | 133166 | sParse.zTail = &zSql[sParse.zTail-zSqlCopy]; |
| 133102 | 133167 | sqlite3DbFree(db, zSqlCopy); |
| 133103 | 133168 | }else{ |
| 133104 | 133169 | sParse.zTail = &zSql[nBytes]; |
| 133105 | 133170 | } |
| 133106 | 133171 | }else{ |
| 133107 | | - sqlite3RunParser(&sParse, zSql, &zErrMsg); |
| 133172 | + sqlite3RunParser(&sParse, zSql); |
| 133108 | 133173 | } |
| 133109 | 133174 | assert( 0==sParse.nQueryLoop ); |
| 133110 | 133175 | |
| 133111 | 133176 | if( pzTail ){ |
| 133112 | 133177 | *pzTail = sParse.zTail; |
| | @@ -133126,18 +133191,18 @@ |
| 133126 | 133191 | if( sParse.pVdbe ){ |
| 133127 | 133192 | sqlite3VdbeFinalize(sParse.pVdbe); |
| 133128 | 133193 | } |
| 133129 | 133194 | assert( 0==(*ppStmt) ); |
| 133130 | 133195 | rc = sParse.rc; |
| 133131 | | - if( zErrMsg ){ |
| 133132 | | - sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg); |
| 133133 | | - sqlite3DbFree(db, zErrMsg); |
| 133196 | + if( sParse.zErrMsg ){ |
| 133197 | + sqlite3ErrorWithMsg(db, rc, "%s", sParse.zErrMsg); |
| 133198 | + sqlite3DbFree(db, sParse.zErrMsg); |
| 133134 | 133199 | }else{ |
| 133135 | 133200 | sqlite3Error(db, rc); |
| 133136 | 133201 | } |
| 133137 | 133202 | }else{ |
| 133138 | | - assert( zErrMsg==0 ); |
| 133203 | + assert( sParse.zErrMsg==0 ); |
| 133139 | 133204 | *ppStmt = (sqlite3_stmt*)sParse.pVdbe; |
| 133140 | 133205 | rc = SQLITE_OK; |
| 133141 | 133206 | sqlite3ErrorClear(db); |
| 133142 | 133207 | } |
| 133143 | 133208 | |
| | @@ -136696,10 +136761,12 @@ |
| 136696 | 136761 | Select *p, /* The right-most of SELECTs to be coded */ |
| 136697 | 136762 | SelectDest *pDest /* What to do with query results */ |
| 136698 | 136763 | ){ |
| 136699 | 136764 | int i, j; /* Loop counters */ |
| 136700 | 136765 | Select *pPrior; /* Another SELECT immediately to our left */ |
| 136766 | + Select *pSplit; /* Left-most SELECT in the right-hand group */ |
| 136767 | + int nSelect; /* Number of SELECT statements in the compound */ |
| 136701 | 136768 | Vdbe *v; /* Generate code to this VDBE */ |
| 136702 | 136769 | SelectDest destA; /* Destination for coroutine A */ |
| 136703 | 136770 | SelectDest destB; /* Destination for coroutine B */ |
| 136704 | 136771 | int regAddrA; /* Address register for select-A coroutine */ |
| 136705 | 136772 | int regAddrB; /* Address register for select-B coroutine */ |
| | @@ -136741,12 +136808,11 @@ |
| 136741 | 136808 | |
| 136742 | 136809 | |
| 136743 | 136810 | /* Patch up the ORDER BY clause |
| 136744 | 136811 | */ |
| 136745 | 136812 | op = p->op; |
| 136746 | | - pPrior = p->pPrior; |
| 136747 | | - assert( pPrior->pOrderBy==0 ); |
| 136813 | + assert( p->pPrior->pOrderBy==0 ); |
| 136748 | 136814 | pOrderBy = p->pOrderBy; |
| 136749 | 136815 | assert( pOrderBy ); |
| 136750 | 136816 | nOrderBy = pOrderBy->nExpr; |
| 136751 | 136817 | |
| 136752 | 136818 | /* For operators other than UNION ALL we have to make sure that |
| | @@ -136792,15 +136858,10 @@ |
| 136792 | 136858 | pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1); |
| 136793 | 136859 | }else{ |
| 136794 | 136860 | pKeyMerge = 0; |
| 136795 | 136861 | } |
| 136796 | 136862 | |
| 136797 | | - /* Reattach the ORDER BY clause to the query. |
| 136798 | | - */ |
| 136799 | | - p->pOrderBy = pOrderBy; |
| 136800 | | - pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0); |
| 136801 | | - |
| 136802 | 136863 | /* Allocate a range of temporary registers and the KeyInfo needed |
| 136803 | 136864 | ** for the logic that removes duplicate result rows when the |
| 136804 | 136865 | ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL). |
| 136805 | 136866 | */ |
| 136806 | 136867 | if( op==TK_ALL ){ |
| | @@ -136821,16 +136882,33 @@ |
| 136821 | 136882 | } |
| 136822 | 136883 | } |
| 136823 | 136884 | |
| 136824 | 136885 | /* Separate the left and the right query from one another |
| 136825 | 136886 | */ |
| 136826 | | - p->pPrior = 0; |
| 136887 | + nSelect = 1; |
| 136888 | + if( (op==TK_ALL || op==TK_UNION) |
| 136889 | + && OptimizationEnabled(db, SQLITE_BalancedMerge) |
| 136890 | + ){ |
| 136891 | + for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){ |
| 136892 | + nSelect++; |
| 136893 | + assert( pSplit->pPrior->pNext==pSplit ); |
| 136894 | + } |
| 136895 | + } |
| 136896 | + if( nSelect<=3 ){ |
| 136897 | + pSplit = p; |
| 136898 | + }else{ |
| 136899 | + pSplit = p; |
| 136900 | + for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; } |
| 136901 | + } |
| 136902 | + pPrior = pSplit->pPrior; |
| 136903 | + pSplit->pPrior = 0; |
| 136827 | 136904 | pPrior->pNext = 0; |
| 136905 | + assert( p->pOrderBy == pOrderBy ); |
| 136906 | + assert( pOrderBy!=0 || db->mallocFailed ); |
| 136907 | + pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0); |
| 136828 | 136908 | sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER"); |
| 136829 | | - if( pPrior->pPrior==0 ){ |
| 136830 | | - sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER"); |
| 136831 | | - } |
| 136909 | + sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER"); |
| 136832 | 136910 | |
| 136833 | 136911 | /* Compute the limit registers */ |
| 136834 | 136912 | computeLimitRegisters(pParse, p, labelEnd); |
| 136835 | 136913 | if( p->iLimit && op==TK_ALL ){ |
| 136836 | 136914 | regLimitA = ++pParse->nMem; |
| | @@ -136977,16 +137055,15 @@ |
| 136977 | 137055 | */ |
| 136978 | 137056 | sqlite3VdbeResolveLabel(v, labelEnd); |
| 136979 | 137057 | |
| 136980 | 137058 | /* Reassembly the compound query so that it will be freed correctly |
| 136981 | 137059 | ** by the calling function */ |
| 136982 | | - if( p->pPrior ){ |
| 136983 | | - sqlite3SelectDelete(db, p->pPrior); |
| 137060 | + if( pSplit->pPrior ){ |
| 137061 | + sqlite3SelectDelete(db, pSplit->pPrior); |
| 136984 | 137062 | } |
| 136985 | | - p->pPrior = pPrior; |
| 136986 | | - pPrior->pNext = p; |
| 136987 | | - |
| 137063 | + pSplit->pPrior = pPrior; |
| 137064 | + pPrior->pNext = pSplit; |
| 136988 | 137065 | sqlite3ExprListDelete(db, pPrior->pOrderBy); |
| 136989 | 137066 | pPrior->pOrderBy = 0; |
| 136990 | 137067 | |
| 136991 | 137068 | /*** TBD: Insert subroutine calls to close cursors on incomplete |
| 136992 | 137069 | **** subqueries ****/ |
| | @@ -142081,11 +142158,16 @@ |
| 142081 | 142158 | int reg = pParse->nMem+1; |
| 142082 | 142159 | pParse->nMem += nCol+2; |
| 142083 | 142160 | pReturning->iRetReg = reg; |
| 142084 | 142161 | for(i=0; i<nCol; i++){ |
| 142085 | 142162 | Expr *pCol = pNew->a[i].pExpr; |
| 142163 | + assert( pCol!=0 || pParse->db->mallocFailed ); |
| 142164 | + if( pCol==0 ) continue; |
| 142086 | 142165 | sqlite3ExprCodeFactorable(pParse, pCol, reg+i); |
| 142166 | + if( sqlite3ExprAffinity(pCol)==SQLITE_AFF_REAL ){ |
| 142167 | + sqlite3VdbeAddOp1(v, OP_RealAffinity, reg+i); |
| 142168 | + } |
| 142087 | 142169 | } |
| 142088 | 142170 | sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, i, reg+i); |
| 142089 | 142171 | sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1); |
| 142090 | 142172 | sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1); |
| 142091 | 142173 | } |
| | @@ -145420,11 +145502,10 @@ |
| 145420 | 145502 | */ |
| 145421 | 145503 | SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ |
| 145422 | 145504 | VtabCtx *pCtx; |
| 145423 | 145505 | int rc = SQLITE_OK; |
| 145424 | 145506 | Table *pTab; |
| 145425 | | - char *zErr = 0; |
| 145426 | 145507 | Parse sParse; |
| 145427 | 145508 | int initBusy; |
| 145428 | 145509 | |
| 145429 | 145510 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 145430 | 145511 | if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){ |
| | @@ -145449,15 +145530,16 @@ |
| 145449 | 145530 | ** in case a bug arises. */ |
| 145450 | 145531 | assert( db->init.busy==0 ); |
| 145451 | 145532 | initBusy = db->init.busy; |
| 145452 | 145533 | db->init.busy = 0; |
| 145453 | 145534 | sParse.nQueryLoop = 1; |
| 145454 | | - if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable, &zErr) |
| 145455 | | - && sParse.pNewTable |
| 145456 | | - && !db->mallocFailed |
| 145535 | + if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable) |
| 145536 | + && ALWAYS(sParse.pNewTable!=0) |
| 145537 | + && ALWAYS(!db->mallocFailed) |
| 145457 | 145538 | && IsOrdinaryTable(sParse.pNewTable) |
| 145458 | 145539 | ){ |
| 145540 | + assert( sParse.zErrMsg==0 ); |
| 145459 | 145541 | if( !pTab->aCol ){ |
| 145460 | 145542 | Table *pNew = sParse.pNewTable; |
| 145461 | 145543 | Index *pIdx; |
| 145462 | 145544 | pTab->aCol = pNew->aCol; |
| 145463 | 145545 | sqlite3ExprListDelete(db, pNew->u.tab.pDfltList); |
| | @@ -145483,12 +145565,13 @@ |
| 145483 | 145565 | pIdx->pTable = pTab; |
| 145484 | 145566 | } |
| 145485 | 145567 | } |
| 145486 | 145568 | pCtx->bDeclared = 1; |
| 145487 | 145569 | }else{ |
| 145488 | | - sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr); |
| 145489 | | - sqlite3DbFree(db, zErr); |
| 145570 | + sqlite3ErrorWithMsg(db, SQLITE_ERROR, |
| 145571 | + (sParse.zErrMsg ? "%s" : 0), sParse.zErrMsg); |
| 145572 | + sqlite3DbFree(db, sParse.zErrMsg); |
| 145490 | 145573 | rc = SQLITE_ERROR; |
| 145491 | 145574 | } |
| 145492 | 145575 | sParse.eParseMode = PARSE_MODE_NORMAL; |
| 145493 | 145576 | |
| 145494 | 145577 | if( sParse.pVdbe ){ |
| | @@ -146251,11 +146334,11 @@ |
| 146251 | 146334 | #define TERM_VIRTUAL 0x0002 /* Added by the optimizer. Do not code */ |
| 146252 | 146335 | #define TERM_CODED 0x0004 /* This term is already coded */ |
| 146253 | 146336 | #define TERM_COPIED 0x0008 /* Has a child */ |
| 146254 | 146337 | #define TERM_ORINFO 0x0010 /* Need to free the WhereTerm.u.pOrInfo object */ |
| 146255 | 146338 | #define TERM_ANDINFO 0x0020 /* Need to free the WhereTerm.u.pAndInfo obj */ |
| 146256 | | -#define TERM_OR_OK 0x0040 /* Used during OR-clause processing */ |
| 146339 | +#define TERM_OK 0x0040 /* Used during OR-clause processing */ |
| 146257 | 146340 | #define TERM_VNULL 0x0080 /* Manufactured x>NULL or x<=NULL term */ |
| 146258 | 146341 | #define TERM_LIKEOPT 0x0100 /* Virtual terms from the LIKE optimization */ |
| 146259 | 146342 | #define TERM_LIKECOND 0x0200 /* Conditionally this LIKE operator term */ |
| 146260 | 146343 | #define TERM_LIKE 0x0400 /* The original LIKE operator */ |
| 146261 | 146344 | #define TERM_IS 0x0800 /* Term.pExpr is an IS operator */ |
| | @@ -147961,11 +148044,11 @@ |
| 147961 | 148044 | ){ |
| 147962 | 148045 | while( ++iLevel < pWInfo->nLevel ){ |
| 147963 | 148046 | WhereLevel *pLevel = &pWInfo->a[iLevel]; |
| 147964 | 148047 | WhereLoop *pLoop = pLevel->pWLoop; |
| 147965 | 148048 | if( pLevel->regFilter==0 ) continue; |
| 147966 | | - /* ,--- Because constructBloomFilter() has will not have set |
| 148049 | + /* ,--- Because sqlite3ConstructBloomFilter() has will not have set |
| 147967 | 148050 | ** vvvvv--' pLevel->regFilter if this were true. */ |
| 147968 | 148051 | if( NEVER(pLoop->prereq & notReady) ) continue; |
| 147969 | 148052 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 147970 | 148053 | WhereTerm *pTerm = pLoop->aLTerm[0]; |
| 147971 | 148054 | int regRowid; |
| | @@ -147981,10 +148064,11 @@ |
| 147981 | 148064 | u16 nEq = pLoop->u.btree.nEq; |
| 147982 | 148065 | int r1; |
| 147983 | 148066 | char *zStartAff; |
| 147984 | 148067 | |
| 147985 | 148068 | assert( pLoop->wsFlags & WHERE_INDEXED ); |
| 148069 | + assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 ); |
| 147986 | 148070 | r1 = codeAllEqualityTerms(pParse,pLevel,0,0,&zStartAff); |
| 147987 | 148071 | codeApplyAffinity(pParse, r1, nEq, zStartAff); |
| 147988 | 148072 | sqlite3DbFree(pParse->db, zStartAff); |
| 147989 | 148073 | sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter, |
| 147990 | 148074 | addrNxt, r1, nEq); |
| | @@ -150043,11 +150127,11 @@ |
| 150043 | 150127 | for(j=0; j<2 && !okToChngToIN; j++){ |
| 150044 | 150128 | Expr *pLeft = 0; |
| 150045 | 150129 | pOrTerm = pOrWc->a; |
| 150046 | 150130 | for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ |
| 150047 | 150131 | assert( pOrTerm->eOperator & WO_EQ ); |
| 150048 | | - pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 150132 | + pOrTerm->wtFlags &= ~TERM_OK; |
| 150049 | 150133 | if( pOrTerm->leftCursor==iCursor ){ |
| 150050 | 150134 | /* This is the 2-bit case and we are on the second iteration and |
| 150051 | 150135 | ** current term is from the first iteration. So skip this term. */ |
| 150052 | 150136 | assert( j==1 ); |
| 150053 | 150137 | continue; |
| | @@ -150084,11 +150168,11 @@ |
| 150084 | 150168 | okToChngToIN = 1; |
| 150085 | 150169 | for(; i>=0 && okToChngToIN; i--, pOrTerm++){ |
| 150086 | 150170 | assert( pOrTerm->eOperator & WO_EQ ); |
| 150087 | 150171 | assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 ); |
| 150088 | 150172 | if( pOrTerm->leftCursor!=iCursor ){ |
| 150089 | | - pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 150173 | + pOrTerm->wtFlags &= ~TERM_OK; |
| 150090 | 150174 | }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR |
| 150091 | 150175 | && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1) |
| 150092 | 150176 | )){ |
| 150093 | 150177 | okToChngToIN = 0; |
| 150094 | 150178 | }else{ |
| | @@ -150100,11 +150184,11 @@ |
| 150100 | 150184 | affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); |
| 150101 | 150185 | affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); |
| 150102 | 150186 | if( affRight!=0 && affRight!=affLeft ){ |
| 150103 | 150187 | okToChngToIN = 0; |
| 150104 | 150188 | }else{ |
| 150105 | | - pOrTerm->wtFlags |= TERM_OR_OK; |
| 150189 | + pOrTerm->wtFlags |= TERM_OK; |
| 150106 | 150190 | } |
| 150107 | 150191 | } |
| 150108 | 150192 | } |
| 150109 | 150193 | } |
| 150110 | 150194 | |
| | @@ -150117,11 +150201,11 @@ |
| 150117 | 150201 | ExprList *pList = 0; /* The RHS of the IN operator */ |
| 150118 | 150202 | Expr *pLeft = 0; /* The LHS of the IN operator */ |
| 150119 | 150203 | Expr *pNew; /* The complete IN operator */ |
| 150120 | 150204 | |
| 150121 | 150205 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ |
| 150122 | | - if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
| 150206 | + if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue; |
| 150123 | 150207 | assert( pOrTerm->eOperator & WO_EQ ); |
| 150124 | 150208 | assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 ); |
| 150125 | 150209 | assert( pOrTerm->leftCursor==iCursor ); |
| 150126 | 150210 | assert( pOrTerm->u.x.leftColumn==iColumn ); |
| 150127 | 150211 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
| | @@ -151651,16 +151735,18 @@ |
| 151651 | 151735 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
| 151652 | 151736 | static void whereTraceIndexInfoInputs(sqlite3_index_info *p){ |
| 151653 | 151737 | int i; |
| 151654 | 151738 | if( !sqlite3WhereTrace ) return; |
| 151655 | 151739 | for(i=0; i<p->nConstraint; i++){ |
| 151656 | | - sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| 151740 | + sqlite3DebugPrintf( |
| 151741 | + " constraint[%d]: col=%d termid=%d op=%d usabled=%d collseq=%s\n", |
| 151657 | 151742 | i, |
| 151658 | 151743 | p->aConstraint[i].iColumn, |
| 151659 | 151744 | p->aConstraint[i].iTermOffset, |
| 151660 | 151745 | p->aConstraint[i].op, |
| 151661 | | - p->aConstraint[i].usable); |
| 151746 | + p->aConstraint[i].usable, |
| 151747 | + sqlite3_vtab_collation(p,i)); |
| 151662 | 151748 | } |
| 151663 | 151749 | for(i=0; i<p->nOrderBy; i++){ |
| 151664 | 151750 | sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", |
| 151665 | 151751 | i, |
| 151666 | 151752 | p->aOrderBy[i].iColumn, |
| | @@ -151960,11 +152046,11 @@ |
| 151960 | 152046 | ** |
| 151961 | 152047 | ** This routine may only be called if it has previously been determined that |
| 151962 | 152048 | ** the loop would benefit from a Bloom filter, and the WHERE_BLOOMFILTER bit |
| 151963 | 152049 | ** is set. |
| 151964 | 152050 | */ |
| 151965 | | -static SQLITE_NOINLINE void constructBloomFilter( |
| 152051 | +static SQLITE_NOINLINE void sqlite3ConstructBloomFilter( |
| 151966 | 152052 | WhereInfo *pWInfo, /* The WHERE clause */ |
| 151967 | 152053 | int iLevel, /* Index in pWInfo->a[] that is pLevel */ |
| 151968 | 152054 | WhereLevel *pLevel, /* Make a Bloom filter for this FROM term */ |
| 151969 | 152055 | Bitmask notReady /* Loops that are not ready */ |
| 151970 | 152056 | ){ |
| | @@ -152044,17 +152130,24 @@ |
| 152044 | 152130 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); |
| 152045 | 152131 | VdbeCoverage(v); |
| 152046 | 152132 | sqlite3VdbeJumpHere(v, addrTop); |
| 152047 | 152133 | pLoop->wsFlags &= ~WHERE_BLOOMFILTER; |
| 152048 | 152134 | if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break; |
| 152049 | | - while( iLevel < pWInfo->nLevel ){ |
| 152050 | | - iLevel++; |
| 152135 | + while( ++iLevel < pWInfo->nLevel ){ |
| 152051 | 152136 | pLevel = &pWInfo->a[iLevel]; |
| 152052 | 152137 | pLoop = pLevel->pWLoop; |
| 152053 | | - if( pLoop==0 ) continue; |
| 152138 | + if( NEVER(pLoop==0) ) continue; |
| 152054 | 152139 | if( pLoop->prereq & notReady ) continue; |
| 152055 | | - if( pLoop->wsFlags & WHERE_BLOOMFILTER ) break; |
| 152140 | + if( (pLoop->wsFlags & (WHERE_BLOOMFILTER|WHERE_COLUMN_IN)) |
| 152141 | + ==WHERE_BLOOMFILTER |
| 152142 | + ){ |
| 152143 | + /* This is a candidate for bloom-filter pull-down (early evaluation). |
| 152144 | + ** The test that WHERE_COLUMN_IN is omitted is important, as we are |
| 152145 | + ** not able to do early evaluation of bloom filters that make use of |
| 152146 | + ** the IN operator */ |
| 152147 | + break; |
| 152148 | + } |
| 152056 | 152149 | } |
| 152057 | 152150 | }while( iLevel < pWInfo->nLevel ); |
| 152058 | 152151 | sqlite3VdbeJumpHere(v, addrOnce); |
| 152059 | 152152 | } |
| 152060 | 152153 | |
| | @@ -152081,14 +152174,23 @@ |
| 152081 | 152174 | struct HiddenIndexInfo *pHidden; |
| 152082 | 152175 | WhereTerm *pTerm; |
| 152083 | 152176 | int nOrderBy; |
| 152084 | 152177 | sqlite3_index_info *pIdxInfo; |
| 152085 | 152178 | u16 mNoOmit = 0; |
| 152179 | + const Table *pTab; |
| 152086 | 152180 | |
| 152087 | | - /* Count the number of possible WHERE clause constraints referring |
| 152088 | | - ** to this virtual table */ |
| 152181 | + assert( pSrc!=0 ); |
| 152182 | + pTab = pSrc->pTab; |
| 152183 | + assert( pTab!=0 ); |
| 152184 | + assert( IsVirtual(pTab) ); |
| 152185 | + |
| 152186 | + /* Find all WHERE clause constraints referring to this virtual table. |
| 152187 | + ** Mark each term with the TERM_OK flag. Set nTerm to the number of |
| 152188 | + ** terms found. |
| 152189 | + */ |
| 152089 | 152190 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 152191 | + pTerm->wtFlags &= ~TERM_OK; |
| 152090 | 152192 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
| 152091 | 152193 | if( pTerm->prereqRight & mUnusable ) continue; |
| 152092 | 152194 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 152093 | 152195 | testcase( pTerm->eOperator & WO_IN ); |
| 152094 | 152196 | testcase( pTerm->eOperator & WO_ISNULL ); |
| | @@ -152095,12 +152197,23 @@ |
| 152095 | 152197 | testcase( pTerm->eOperator & WO_IS ); |
| 152096 | 152198 | testcase( pTerm->eOperator & WO_ALL ); |
| 152097 | 152199 | if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue; |
| 152098 | 152200 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
| 152099 | 152201 | assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); |
| 152100 | | - assert( pTerm->u.x.leftColumn>=(-1) ); |
| 152202 | + assert( pTerm->u.x.leftColumn>=XN_ROWID ); |
| 152203 | + assert( pTerm->u.x.leftColumn<pTab->nCol ); |
| 152204 | + |
| 152205 | + /* tag-20191211-002: WHERE-clause constraints are not useful to the |
| 152206 | + ** right-hand table of a LEFT JOIN. See tag-20191211-001 for the |
| 152207 | + ** equivalent restriction for ordinary tables. */ |
| 152208 | + if( (pSrc->fg.jointype & JT_LEFT)!=0 |
| 152209 | + && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) |
| 152210 | + ){ |
| 152211 | + continue; |
| 152212 | + } |
| 152101 | 152213 | nTerm++; |
| 152214 | + pTerm->wtFlags |= TERM_OK; |
| 152102 | 152215 | } |
| 152103 | 152216 | |
| 152104 | 152217 | /* If the ORDER BY clause contains only columns in the current |
| 152105 | 152218 | ** virtual table then allocate space for the aOrderBy part of |
| 152106 | 152219 | ** the sqlite3_index_info structure. |
| | @@ -152108,12 +152221,45 @@ |
| 152108 | 152221 | nOrderBy = 0; |
| 152109 | 152222 | if( pOrderBy ){ |
| 152110 | 152223 | int n = pOrderBy->nExpr; |
| 152111 | 152224 | for(i=0; i<n; i++){ |
| 152112 | 152225 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 152113 | | - if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; |
| 152226 | + Expr *pE2; |
| 152227 | + |
| 152228 | + /* Skip over constant terms in the ORDER BY clause */ |
| 152229 | + if( sqlite3ExprIsConstant(pExpr) ){ |
| 152230 | + continue; |
| 152231 | + } |
| 152232 | + |
| 152233 | + /* Virtual tables are unable to deal with NULLS FIRST */ |
| 152114 | 152234 | if( pOrderBy->a[i].sortFlags & KEYINFO_ORDER_BIGNULL ) break; |
| 152235 | + |
| 152236 | + /* First case - a direct column references without a COLLATE operator */ |
| 152237 | + if( pExpr->op==TK_COLUMN && pExpr->iTable==pSrc->iCursor ){ |
| 152238 | + assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol ); |
| 152239 | + continue; |
| 152240 | + } |
| 152241 | + |
| 152242 | + /* 2nd case - a column reference with a COLLATE operator. Only match |
| 152243 | + ** of the COLLATE operator matches the collation of the column. */ |
| 152244 | + if( pExpr->op==TK_COLLATE |
| 152245 | + && (pE2 = pExpr->pLeft)->op==TK_COLUMN |
| 152246 | + && pE2->iTable==pSrc->iCursor |
| 152247 | + ){ |
| 152248 | + const char *zColl; /* The collating sequence name */ |
| 152249 | + assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 152250 | + assert( pExpr->u.zToken!=0 ); |
| 152251 | + assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol ); |
| 152252 | + pExpr->iColumn = pE2->iColumn; |
| 152253 | + if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */ |
| 152254 | + zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]); |
| 152255 | + if( zColl==0 ) zColl = sqlite3StrBINARY; |
| 152256 | + if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue; |
| 152257 | + } |
| 152258 | + |
| 152259 | + /* No matches cause a break out of the loop */ |
| 152260 | + break; |
| 152115 | 152261 | } |
| 152116 | 152262 | if( i==n){ |
| 152117 | 152263 | nOrderBy = n; |
| 152118 | 152264 | } |
| 152119 | 152265 | } |
| | @@ -152129,38 +152275,18 @@ |
| 152129 | 152275 | } |
| 152130 | 152276 | pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1]; |
| 152131 | 152277 | pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1]; |
| 152132 | 152278 | pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; |
| 152133 | 152279 | pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; |
| 152134 | | - pIdxInfo->nOrderBy = nOrderBy; |
| 152135 | 152280 | pIdxInfo->aConstraint = pIdxCons; |
| 152136 | 152281 | pIdxInfo->aOrderBy = pIdxOrderBy; |
| 152137 | 152282 | pIdxInfo->aConstraintUsage = pUsage; |
| 152138 | 152283 | pHidden->pWC = pWC; |
| 152139 | 152284 | pHidden->pParse = pParse; |
| 152140 | 152285 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 152141 | 152286 | u16 op; |
| 152142 | | - if( pTerm->leftCursor != pSrc->iCursor ) continue; |
| 152143 | | - if( pTerm->prereqRight & mUnusable ) continue; |
| 152144 | | - assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 152145 | | - testcase( pTerm->eOperator & WO_IN ); |
| 152146 | | - testcase( pTerm->eOperator & WO_IS ); |
| 152147 | | - testcase( pTerm->eOperator & WO_ISNULL ); |
| 152148 | | - testcase( pTerm->eOperator & WO_ALL ); |
| 152149 | | - if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue; |
| 152150 | | - if( pTerm->wtFlags & TERM_VNULL ) continue; |
| 152151 | | - |
| 152152 | | - /* tag-20191211-002: WHERE-clause constraints are not useful to the |
| 152153 | | - ** right-hand table of a LEFT JOIN. See tag-20191211-001 for the |
| 152154 | | - ** equivalent restriction for ordinary tables. */ |
| 152155 | | - if( (pSrc->fg.jointype & JT_LEFT)!=0 |
| 152156 | | - && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) |
| 152157 | | - ){ |
| 152158 | | - continue; |
| 152159 | | - } |
| 152160 | | - assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); |
| 152161 | | - assert( pTerm->u.x.leftColumn>=(-1) ); |
| 152287 | + if( (pTerm->wtFlags & TERM_OK)==0 ) continue; |
| 152162 | 152288 | pIdxCons[j].iColumn = pTerm->u.x.leftColumn; |
| 152163 | 152289 | pIdxCons[j].iTermOffset = i; |
| 152164 | 152290 | op = pTerm->eOperator & WO_ALL; |
| 152165 | 152291 | if( op==WO_IN ) op = WO_EQ; |
| 152166 | 152292 | if( op==WO_AUX ){ |
| | @@ -152193,16 +152319,23 @@ |
| 152193 | 152319 | } |
| 152194 | 152320 | } |
| 152195 | 152321 | |
| 152196 | 152322 | j++; |
| 152197 | 152323 | } |
| 152324 | + assert( j==nTerm ); |
| 152198 | 152325 | pIdxInfo->nConstraint = j; |
| 152199 | | - for(i=0; i<nOrderBy; i++){ |
| 152326 | + for(i=j=0; i<nOrderBy; i++){ |
| 152200 | 152327 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 152201 | | - pIdxOrderBy[i].iColumn = pExpr->iColumn; |
| 152202 | | - pIdxOrderBy[i].desc = pOrderBy->a[i].sortFlags & KEYINFO_ORDER_DESC; |
| 152328 | + if( sqlite3ExprIsConstant(pExpr) ) continue; |
| 152329 | + assert( pExpr->op==TK_COLUMN |
| 152330 | + || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN |
| 152331 | + && pExpr->iColumn==pExpr->pLeft->iColumn) ); |
| 152332 | + pIdxOrderBy[j].iColumn = pExpr->iColumn; |
| 152333 | + pIdxOrderBy[j].desc = pOrderBy->a[i].sortFlags & KEYINFO_ORDER_DESC; |
| 152334 | + j++; |
| 152203 | 152335 | } |
| 152336 | + pIdxInfo->nOrderBy = j; |
| 152204 | 152337 | |
| 152205 | 152338 | *pmNoOmit = mNoOmit; |
| 152206 | 152339 | return pIdxInfo; |
| 152207 | 152340 | } |
| 152208 | 152341 | |
| | @@ -154530,15 +154663,23 @@ |
| 154530 | 154663 | |
| 154531 | 154664 | return rc; |
| 154532 | 154665 | } |
| 154533 | 154666 | |
| 154534 | 154667 | /* |
| 154535 | | -** If this function is invoked from within an xBestIndex() callback, it |
| 154536 | | -** returns a pointer to a buffer containing the name of the collation |
| 154537 | | -** sequence associated with element iCons of the sqlite3_index_info.aConstraint |
| 154538 | | -** array. Or, if iCons is out of range or there is no active xBestIndex |
| 154539 | | -** call, return NULL. |
| 154668 | +** Return the collating sequence for a constraint passed into xBestIndex. |
| 154669 | +** |
| 154670 | +** pIdxInfo must be an sqlite3_index_info structure passed into xBestIndex. |
| 154671 | +** This routine depends on there being a HiddenIndexInfo structure immediately |
| 154672 | +** following the sqlite3_index_info structure. |
| 154673 | +** |
| 154674 | +** Return a pointer to the collation name: |
| 154675 | +** |
| 154676 | +** 1. If there is an explicit COLLATE operator on the constaint, return it. |
| 154677 | +** |
| 154678 | +** 2. Else, if the column has an alternative collation, return that. |
| 154679 | +** |
| 154680 | +** 3. Otherwise, return "BINARY". |
| 154540 | 154681 | */ |
| 154541 | 154682 | SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){ |
| 154542 | 154683 | HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; |
| 154543 | 154684 | const char *zRet = 0; |
| 154544 | 154685 | if( iCons>=0 && iCons<pIdxInfo->nConstraint ){ |
| | @@ -155972,11 +156113,11 @@ |
| 155972 | 156113 | assert( pWInfo->nLevel>=2 ); |
| 155973 | 156114 | assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) ); |
| 155974 | 156115 | nSearch = pWInfo->a[0].pWLoop->nOut; |
| 155975 | 156116 | for(i=1; i<pWInfo->nLevel; i++){ |
| 155976 | 156117 | WhereLoop *pLoop = pWInfo->a[i].pWLoop; |
| 155977 | | - const int reqFlags = (WHERE_SELFCULL|WHERE_COLUMN_EQ); |
| 156118 | + const unsigned int reqFlags = (WHERE_SELFCULL|WHERE_COLUMN_EQ); |
| 155978 | 156119 | if( (pLoop->wsFlags & reqFlags)==reqFlags |
| 155979 | 156120 | /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */ |
| 155980 | 156121 | && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0) |
| 155981 | 156122 | ){ |
| 155982 | 156123 | SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab]; |
| | @@ -156590,11 +156731,11 @@ |
| 156590 | 156731 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 156591 | 156732 | constructAutomaticIndex(pParse, &pWInfo->sWC, |
| 156592 | 156733 | &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 156593 | 156734 | #endif |
| 156594 | 156735 | }else{ |
| 156595 | | - constructBloomFilter(pWInfo, ii, pLevel, notReady); |
| 156736 | + sqlite3ConstructBloomFilter(pWInfo, ii, pLevel, notReady); |
| 156596 | 156737 | } |
| 156597 | 156738 | if( db->mallocFailed ) goto whereBeginError; |
| 156598 | 156739 | } |
| 156599 | 156740 | addrExplain = sqlite3WhereExplainOneScan( |
| 156600 | 156741 | pParse, pTabList, pLevel, wctrlFlags |
| | @@ -166471,17 +166612,13 @@ |
| 166471 | 166612 | *tokenType = TK_ID; |
| 166472 | 166613 | return i; |
| 166473 | 166614 | } |
| 166474 | 166615 | |
| 166475 | 166616 | /* |
| 166476 | | -** Run the parser on the given SQL string. The parser structure is |
| 166477 | | -** passed in. An SQLITE_ status code is returned. If an error occurs |
| 166478 | | -** then an and attempt is made to write an error message into |
| 166479 | | -** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that |
| 166480 | | -** error message. |
| 166617 | +** Run the parser on the given SQL string. |
| 166481 | 166618 | */ |
| 166482 | | -SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ |
| 166619 | +SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql){ |
| 166483 | 166620 | int nErr = 0; /* Number of errors encountered */ |
| 166484 | 166621 | void *pEngine; /* The LEMON-generated LALR(1) parser */ |
| 166485 | 166622 | int n = 0; /* Length of the next token token */ |
| 166486 | 166623 | int tokenType; /* type of the next token */ |
| 166487 | 166624 | int lastTokenParsed = -1; /* type of the previous token */ |
| | @@ -166498,11 +166635,10 @@ |
| 166498 | 166635 | if( db->nVdbeActive==0 ){ |
| 166499 | 166636 | AtomicStore(&db->u1.isInterrupted, 0); |
| 166500 | 166637 | } |
| 166501 | 166638 | pParse->rc = SQLITE_OK; |
| 166502 | 166639 | pParse->zTail = zSql; |
| 166503 | | - assert( pzErrMsg!=0 ); |
| 166504 | 166640 | #ifdef SQLITE_DEBUG |
| 166505 | 166641 | if( db->flags & SQLITE_ParserTrace ){ |
| 166506 | 166642 | printf("parser: [[[%s]]]\n", zSql); |
| 166507 | 166643 | sqlite3ParserTrace(stdout, "parser: "); |
| 166508 | 166644 | }else{ |
| | @@ -166541,10 +166677,11 @@ |
| 166541 | 166677 | if( tokenType>=TK_SPACE ){ |
| 166542 | 166678 | assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL ); |
| 166543 | 166679 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 166544 | 166680 | if( AtomicLoad(&db->u1.isInterrupted) ){ |
| 166545 | 166681 | pParse->rc = SQLITE_INTERRUPT; |
| 166682 | + pParse->nErr++; |
| 166546 | 166683 | break; |
| 166547 | 166684 | } |
| 166548 | 166685 | if( tokenType==TK_SPACE ){ |
| 166549 | 166686 | zSql += n; |
| 166550 | 166687 | continue; |
| | @@ -166598,45 +166735,30 @@ |
| 166598 | 166735 | sqlite3ParserFree(pEngine, sqlite3_free); |
| 166599 | 166736 | #endif |
| 166600 | 166737 | if( db->mallocFailed ){ |
| 166601 | 166738 | pParse->rc = SQLITE_NOMEM_BKPT; |
| 166602 | 166739 | } |
| 166603 | | - if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ |
| 166604 | | - pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); |
| 166605 | | - } |
| 166606 | | - assert( pzErrMsg!=0 ); |
| 166607 | | - if( pParse->zErrMsg ){ |
| 166608 | | - *pzErrMsg = pParse->zErrMsg; |
| 166609 | | - sqlite3_log(pParse->rc, "%s in \"%s\"", |
| 166610 | | - *pzErrMsg, pParse->zTail); |
| 166611 | | - pParse->zErrMsg = 0; |
| 166740 | + if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){ |
| 166741 | + if( pParse->zErrMsg==0 ){ |
| 166742 | + pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); |
| 166743 | + } |
| 166744 | + sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail); |
| 166612 | 166745 | nErr++; |
| 166613 | 166746 | } |
| 166614 | 166747 | pParse->zTail = zSql; |
| 166615 | | - if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ |
| 166616 | | - sqlite3VdbeDelete(pParse->pVdbe); |
| 166617 | | - pParse->pVdbe = 0; |
| 166618 | | - } |
| 166619 | | -#ifndef SQLITE_OMIT_SHARED_CACHE |
| 166620 | | - if( pParse->nested==0 ){ |
| 166621 | | - sqlite3DbFree(db, pParse->aTableLock); |
| 166622 | | - pParse->aTableLock = 0; |
| 166623 | | - pParse->nTableLock = 0; |
| 166624 | | - } |
| 166625 | | -#endif |
| 166626 | 166748 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 166627 | 166749 | sqlite3_free(pParse->apVtabLock); |
| 166628 | 166750 | #endif |
| 166629 | 166751 | |
| 166630 | | - if( !IN_SPECIAL_PARSE ){ |
| 166752 | + if( pParse->pNewTable && !IN_SPECIAL_PARSE ){ |
| 166631 | 166753 | /* If the pParse->declareVtab flag is set, do not delete any table |
| 166632 | 166754 | ** structure built up in pParse->pNewTable. The calling code (see vtab.c) |
| 166633 | 166755 | ** will take responsibility for freeing the Table structure. |
| 166634 | 166756 | */ |
| 166635 | 166757 | sqlite3DeleteTable(db, pParse->pNewTable); |
| 166636 | 166758 | } |
| 166637 | | - if( !IN_RENAME_OBJECT ){ |
| 166759 | + if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){ |
| 166638 | 166760 | sqlite3DeleteTrigger(db, pParse->pNewTrigger); |
| 166639 | 166761 | } |
| 166640 | 166762 | sqlite3DbFree(db, pParse->pVList); |
| 166641 | 166763 | db->pParse = pParentParse; |
| 166642 | 166764 | assert( nErr==0 || pParse->rc!=SQLITE_OK ); |
| | @@ -169763,10 +169885,23 @@ |
| 169763 | 169885 | } |
| 169764 | 169886 | } |
| 169765 | 169887 | sqlite3_mutex_leave(db->mutex); |
| 169766 | 169888 | return z; |
| 169767 | 169889 | } |
| 169890 | + |
| 169891 | +/* |
| 169892 | +** Return the byte offset of the most recent error |
| 169893 | +*/ |
| 169894 | +SQLITE_API int sqlite3_error_offset(sqlite3 *db){ |
| 169895 | + int iOffset = -1; |
| 169896 | + if( db && sqlite3SafetyCheckSickOrOk(db) && db->errCode ){ |
| 169897 | + sqlite3_mutex_enter(db->mutex); |
| 169898 | + iOffset = db->errByteOffset; |
| 169899 | + sqlite3_mutex_leave(db->mutex); |
| 169900 | + } |
| 169901 | + return iOffset; |
| 169902 | +} |
| 169768 | 169903 | |
| 169769 | 169904 | #ifndef SQLITE_OMIT_UTF16 |
| 169770 | 169905 | /* |
| 169771 | 169906 | ** Return UTF-16 encoded English language explanation of the most recent |
| 169772 | 169907 | ** error. |
| | @@ -173514,11 +173649,11 @@ |
| 173514 | 173649 | SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **); |
| 173515 | 173650 | SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *); |
| 173516 | 173651 | SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr); |
| 173517 | 173652 | |
| 173518 | 173653 | /* fts3_tokenize_vtab.c */ |
| 173519 | | -SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *); |
| 173654 | +SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *, void(*xDestroy)(void*)); |
| 173520 | 173655 | |
| 173521 | 173656 | /* fts3_unicode2.c (functions generated by parsing unicode text files) */ |
| 173522 | 173657 | #ifndef SQLITE_DISABLE_FTS3_UNICODE |
| 173523 | 173658 | SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int); |
| 173524 | 173659 | SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int); |
| | @@ -173546,10 +173681,16 @@ |
| 173546 | 173681 | /* #include "fts3.h" */ |
| 173547 | 173682 | #ifndef SQLITE_CORE |
| 173548 | 173683 | /* # include "sqlite3ext.h" */ |
| 173549 | 173684 | SQLITE_EXTENSION_INIT1 |
| 173550 | 173685 | #endif |
| 173686 | + |
| 173687 | +typedef struct Fts3HashWrapper Fts3HashWrapper; |
| 173688 | +struct Fts3HashWrapper { |
| 173689 | + Fts3Hash hash; /* Hash table */ |
| 173690 | + int nRef; /* Number of pointers to this object */ |
| 173691 | +}; |
| 173551 | 173692 | |
| 173552 | 173693 | static int fts3EvalNext(Fts3Cursor *pCsr); |
| 173553 | 173694 | static int fts3EvalStart(Fts3Cursor *pCsr); |
| 173554 | 173695 | static int fts3TermSegReaderCursor( |
| 173555 | 173696 | Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **); |
| | @@ -174411,11 +174552,11 @@ |
| 174411 | 174552 | int argc, /* Number of elements in argv array */ |
| 174412 | 174553 | const char * const *argv, /* xCreate/xConnect argument array */ |
| 174413 | 174554 | sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */ |
| 174414 | 174555 | char **pzErr /* Write any error message here */ |
| 174415 | 174556 | ){ |
| 174416 | | - Fts3Hash *pHash = (Fts3Hash *)pAux; |
| 174557 | + Fts3Hash *pHash = &((Fts3HashWrapper*)pAux)->hash; |
| 174417 | 174558 | Fts3Table *p = 0; /* Pointer to allocated vtab */ |
| 174418 | 174559 | int rc = SQLITE_OK; /* Return code */ |
| 174419 | 174560 | int i; /* Iterator variable */ |
| 174420 | 174561 | sqlite3_int64 nByte; /* Size of allocation used for *p */ |
| 174421 | 174562 | int iCol; /* Column index */ |
| | @@ -177246,13 +177387,16 @@ |
| 177246 | 177387 | ** This function is registered as the module destructor (called when an |
| 177247 | 177388 | ** FTS3 enabled database connection is closed). It frees the memory |
| 177248 | 177389 | ** allocated for the tokenizer hash table. |
| 177249 | 177390 | */ |
| 177250 | 177391 | static void hashDestroy(void *p){ |
| 177251 | | - Fts3Hash *pHash = (Fts3Hash *)p; |
| 177252 | | - sqlite3Fts3HashClear(pHash); |
| 177253 | | - sqlite3_free(pHash); |
| 177392 | + Fts3HashWrapper *pHash = (Fts3HashWrapper *)p; |
| 177393 | + pHash->nRef--; |
| 177394 | + if( pHash->nRef<=0 ){ |
| 177395 | + sqlite3Fts3HashClear(&pHash->hash); |
| 177396 | + sqlite3_free(pHash); |
| 177397 | + } |
| 177254 | 177398 | } |
| 177255 | 177399 | |
| 177256 | 177400 | /* |
| 177257 | 177401 | ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are |
| 177258 | 177402 | ** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c |
| | @@ -177278,11 +177422,11 @@ |
| 177278 | 177422 | ** SQLite. If fts3 is built as a dynamically loadable extension, this |
| 177279 | 177423 | ** function is called by the sqlite3_extension_init() entry point. |
| 177280 | 177424 | */ |
| 177281 | 177425 | SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){ |
| 177282 | 177426 | int rc = SQLITE_OK; |
| 177283 | | - Fts3Hash *pHash = 0; |
| 177427 | + Fts3HashWrapper *pHash = 0; |
| 177284 | 177428 | const sqlite3_tokenizer_module *pSimple = 0; |
| 177285 | 177429 | const sqlite3_tokenizer_module *pPorter = 0; |
| 177286 | 177430 | #ifndef SQLITE_DISABLE_FTS3_UNICODE |
| 177287 | 177431 | const sqlite3_tokenizer_module *pUnicode = 0; |
| 177288 | 177432 | #endif |
| | @@ -177306,70 +177450,74 @@ |
| 177306 | 177450 | |
| 177307 | 177451 | sqlite3Fts3SimpleTokenizerModule(&pSimple); |
| 177308 | 177452 | sqlite3Fts3PorterTokenizerModule(&pPorter); |
| 177309 | 177453 | |
| 177310 | 177454 | /* Allocate and initialize the hash-table used to store tokenizers. */ |
| 177311 | | - pHash = sqlite3_malloc(sizeof(Fts3Hash)); |
| 177455 | + pHash = sqlite3_malloc(sizeof(Fts3HashWrapper)); |
| 177312 | 177456 | if( !pHash ){ |
| 177313 | 177457 | rc = SQLITE_NOMEM; |
| 177314 | 177458 | }else{ |
| 177315 | | - sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); |
| 177459 | + sqlite3Fts3HashInit(&pHash->hash, FTS3_HASH_STRING, 1); |
| 177460 | + pHash->nRef = 0; |
| 177316 | 177461 | } |
| 177317 | 177462 | |
| 177318 | 177463 | /* Load the built-in tokenizers into the hash table */ |
| 177319 | 177464 | if( rc==SQLITE_OK ){ |
| 177320 | | - if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple) |
| 177321 | | - || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) |
| 177465 | + if( sqlite3Fts3HashInsert(&pHash->hash, "simple", 7, (void *)pSimple) |
| 177466 | + || sqlite3Fts3HashInsert(&pHash->hash, "porter", 7, (void *)pPorter) |
| 177322 | 177467 | |
| 177323 | 177468 | #ifndef SQLITE_DISABLE_FTS3_UNICODE |
| 177324 | | - || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode) |
| 177469 | + || sqlite3Fts3HashInsert(&pHash->hash, "unicode61", 10, (void *)pUnicode) |
| 177325 | 177470 | #endif |
| 177326 | 177471 | #ifdef SQLITE_ENABLE_ICU |
| 177327 | | - || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu)) |
| 177472 | + || (pIcu && sqlite3Fts3HashInsert(&pHash->hash, "icu", 4, (void *)pIcu)) |
| 177328 | 177473 | #endif |
| 177329 | 177474 | ){ |
| 177330 | 177475 | rc = SQLITE_NOMEM; |
| 177331 | 177476 | } |
| 177332 | 177477 | } |
| 177333 | 177478 | |
| 177334 | 177479 | #ifdef SQLITE_TEST |
| 177335 | 177480 | if( rc==SQLITE_OK ){ |
| 177336 | | - rc = sqlite3Fts3ExprInitTestInterface(db, pHash); |
| 177481 | + rc = sqlite3Fts3ExprInitTestInterface(db, &pHash->hash); |
| 177337 | 177482 | } |
| 177338 | 177483 | #endif |
| 177339 | 177484 | |
| 177340 | 177485 | /* Create the virtual table wrapper around the hash-table and overload |
| 177341 | 177486 | ** the four scalar functions. If this is successful, register the |
| 177342 | 177487 | ** module with sqlite. |
| 177343 | 177488 | */ |
| 177344 | 177489 | if( SQLITE_OK==rc |
| 177345 | | - && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer")) |
| 177490 | + && SQLITE_OK==(rc=sqlite3Fts3InitHashTable(db,&pHash->hash,"fts3_tokenizer")) |
| 177346 | 177491 | && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1)) |
| 177347 | 177492 | && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1)) |
| 177348 | 177493 | && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1)) |
| 177349 | 177494 | && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2)) |
| 177350 | 177495 | && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1)) |
| 177351 | 177496 | ){ |
| 177497 | + pHash->nRef++; |
| 177352 | 177498 | rc = sqlite3_create_module_v2( |
| 177353 | 177499 | db, "fts3", &fts3Module, (void *)pHash, hashDestroy |
| 177354 | 177500 | ); |
| 177355 | 177501 | if( rc==SQLITE_OK ){ |
| 177502 | + pHash->nRef++; |
| 177356 | 177503 | rc = sqlite3_create_module_v2( |
| 177357 | | - db, "fts4", &fts3Module, (void *)pHash, 0 |
| 177504 | + db, "fts4", &fts3Module, (void *)pHash, hashDestroy |
| 177358 | 177505 | ); |
| 177359 | 177506 | } |
| 177360 | 177507 | if( rc==SQLITE_OK ){ |
| 177361 | | - rc = sqlite3Fts3InitTok(db, (void *)pHash); |
| 177508 | + pHash->nRef++; |
| 177509 | + rc = sqlite3Fts3InitTok(db, (void *)pHash, hashDestroy); |
| 177362 | 177510 | } |
| 177363 | 177511 | return rc; |
| 177364 | 177512 | } |
| 177365 | 177513 | |
| 177366 | 177514 | |
| 177367 | 177515 | /* An error has occurred. Delete the hash table and return the error code. */ |
| 177368 | 177516 | assert( rc!=SQLITE_OK ); |
| 177369 | 177517 | if( pHash ){ |
| 177370 | | - sqlite3Fts3HashClear(pHash); |
| 177518 | + sqlite3Fts3HashClear(&pHash->hash); |
| 177371 | 177519 | sqlite3_free(pHash); |
| 177372 | 177520 | } |
| 177373 | 177521 | return rc; |
| 177374 | 177522 | } |
| 177375 | 177523 | |
| | @@ -177712,11 +177860,11 @@ |
| 177712 | 177860 | ){ |
| 177713 | 177861 | char *p = *ppIter; |
| 177714 | 177862 | |
| 177715 | 177863 | assert( nDoclist>0 ); |
| 177716 | 177864 | assert( *pbEof==0 ); |
| 177717 | | - assert( p || *piDocid==0 ); |
| 177865 | + assert_fts3_nc( p || *piDocid==0 ); |
| 177718 | 177866 | assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) ); |
| 177719 | 177867 | |
| 177720 | 177868 | if( p==0 ){ |
| 177721 | 177869 | sqlite3_int64 iDocid = 0; |
| 177722 | 177870 | char *pNext = 0; |
| | @@ -183429,11 +183577,11 @@ |
| 183429 | 183577 | |
| 183430 | 183578 | /* |
| 183431 | 183579 | ** Register the fts3tok module with database connection db. Return SQLITE_OK |
| 183432 | 183580 | ** if successful or an error code if sqlite3_create_module() fails. |
| 183433 | 183581 | */ |
| 183434 | | -SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){ |
| 183582 | +SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash, void(*xDestroy)(void*)){ |
| 183435 | 183583 | static const sqlite3_module fts3tok_module = { |
| 183436 | 183584 | 0, /* iVersion */ |
| 183437 | 183585 | fts3tokConnectMethod, /* xCreate */ |
| 183438 | 183586 | fts3tokConnectMethod, /* xConnect */ |
| 183439 | 183587 | fts3tokBestIndexMethod, /* xBestIndex */ |
| | @@ -183458,11 +183606,13 @@ |
| 183458 | 183606 | 0, /* xRollbackTo */ |
| 183459 | 183607 | 0 /* xShadowName */ |
| 183460 | 183608 | }; |
| 183461 | 183609 | int rc; /* Return code */ |
| 183462 | 183610 | |
| 183463 | | - rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash); |
| 183611 | + rc = sqlite3_create_module_v2( |
| 183612 | + db, "fts3tokenize", &fts3tok_module, (void*)pHash, xDestroy |
| 183613 | + ); |
| 183464 | 183614 | return rc; |
| 183465 | 183615 | } |
| 183466 | 183616 | |
| 183467 | 183617 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ |
| 183468 | 183618 | |
| | @@ -191846,10 +191996,19 @@ |
| 191846 | 191996 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) |
| 191847 | 191997 | #if !defined(SQLITEINT_H) |
| 191848 | 191998 | /* #include "sqlite3ext.h" */ |
| 191849 | 191999 | #endif |
| 191850 | 192000 | SQLITE_EXTENSION_INIT1 |
| 192001 | + |
| 192002 | +/* If compiling this extension separately (why would anybody do that when |
| 192003 | +** it is built into the amalgamation?) we must set NDEBUG if SQLITE_DEBUG |
| 192004 | +** is not defined *before* including <assert.h>, in order to disable asserts(). |
| 192005 | +*/ |
| 192006 | +#if !defined(SQLITE_AMALGAMATION) && !defined(SQLITE_DEBUG) |
| 192007 | +# define NDEBUG 1 |
| 192008 | +#endif |
| 192009 | + |
| 191851 | 192010 | /* #include <assert.h> */ |
| 191852 | 192011 | /* #include <string.h> */ |
| 191853 | 192012 | /* #include <stdlib.h> */ |
| 191854 | 192013 | /* #include <stdarg.h> */ |
| 191855 | 192014 | |
| | @@ -233132,11 +233291,11 @@ |
| 233132 | 233291 | int nArg, /* Number of args */ |
| 233133 | 233292 | sqlite3_value **apUnused /* Function arguments */ |
| 233134 | 233293 | ){ |
| 233135 | 233294 | assert( nArg==0 ); |
| 233136 | 233295 | UNUSED_PARAM2(nArg, apUnused); |
| 233137 | | - sqlite3_result_text(pCtx, "fts5: 2021-12-09 20:06:18 633bfeeea2bccdd44126acf3f61ecca163c9d933bdc787a2c18a697dc9406882", -1, SQLITE_TRANSIENT); |
| 233296 | + sqlite3_result_text(pCtx, "fts5: 2021-12-31 22:53:15 e654b57a9fc32021453eed48d1c1bba65c833fb1aac3946567968c877e4cbd10", -1, SQLITE_TRANSIENT); |
| 233138 | 233297 | } |
| 233139 | 233298 | |
| 233140 | 233299 | /* |
| 233141 | 233300 | ** Return true if zName is the extension on one of the shadow tables used |
| 233142 | 233301 | ** by this module. |
| 233143 | 233302 | |