| | @@ -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.37.0" |
| 456 | 456 | #define SQLITE_VERSION_NUMBER 3037000 |
| 457 | | -#define SQLITE_SOURCE_ID "2021-10-22 11:17:29 1a038242dc6c0cab97dd9375acfce62aa1c386debc36aaed388d366b87ddd931" |
| 457 | +#define SQLITE_SOURCE_ID "2021-11-15 19:10:13 bd66ab8a1bc3c43a57c7caff5f54545b0feb0177f1f51492f30d308c123c43ba" |
| 458 | 458 | |
| 459 | 459 | /* |
| 460 | 460 | ** CAPI3REF: Run-Time Library Version Numbers |
| 461 | 461 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 462 | 462 | ** |
| | @@ -913,10 +913,11 @@ |
| 913 | 913 | #define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */ |
| 914 | 914 | #define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */ |
| 915 | 915 | #define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */ |
| 916 | 916 | #define SQLITE_OPEN_WAL 0x00080000 /* VFS only */ |
| 917 | 917 | #define SQLITE_OPEN_NOFOLLOW 0x01000000 /* Ok for sqlite3_open_v2() */ |
| 918 | +#define SQLITE_OPEN_EXRESCODE 0x02000000 /* Extended result codes */ |
| 918 | 919 | |
| 919 | 920 | /* Reserved: 0x00F00000 */ |
| 920 | 921 | /* Legacy compatibility: */ |
| 921 | 922 | #define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */ |
| 922 | 923 | |
| | @@ -3732,10 +3733,18 @@ |
| 3732 | 3733 | ** |
| 3733 | 3734 | ** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt> |
| 3734 | 3735 | ** <dd>The database is opened [shared cache] disabled, overriding |
| 3735 | 3736 | ** the default shared cache setting provided by |
| 3736 | 3737 | ** [sqlite3_enable_shared_cache()].)^ |
| 3738 | +** |
| 3739 | +** [[OPEN_EXRESCODE]] ^(<dt>[SQLITE_OPEN_EXRESCODE]</dt> |
| 3740 | +** <dd>The database connection comes up in "extended result code mode". |
| 3741 | +** In other words, the database behaves has if |
| 3742 | +** [sqlite3_extended_result_codes(db,1)] where called on the database |
| 3743 | +** connection as soon as the connection is created. In addition to setting |
| 3744 | +** the extended result code mode, this flag also causes [sqlite3_open_v2()] |
| 3745 | +** to return an extended result code.</dd> |
| 3737 | 3746 | ** |
| 3738 | 3747 | ** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt> |
| 3739 | 3748 | ** <dd>The database filename is not allowed to be a symbolic link</dd> |
| 3740 | 3749 | ** </dl>)^ |
| 3741 | 3750 | ** |
| | @@ -6709,10 +6718,76 @@ |
| 6709 | 6718 | ** |
| 6710 | 6719 | ** See also the [sqlite3_update_hook()] interface. |
| 6711 | 6720 | */ |
| 6712 | 6721 | SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); |
| 6713 | 6722 | SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); |
| 6723 | + |
| 6724 | +/* |
| 6725 | +** CAPI3REF: Autovacuum Compaction Amount Callback |
| 6726 | +** METHOD: sqlite3 |
| 6727 | +** |
| 6728 | +** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback |
| 6729 | +** function C that is invoked prior to each autovacuum of the database |
| 6730 | +** file. ^The callback is passed a copy of the generic data pointer (P), |
| 6731 | +** the schema-name of the attached database that is being autovacuumed, |
| 6732 | +** the the size of the database file in pages, the number of free pages, |
| 6733 | +** and the number of bytes per page, respectively. The callback should |
| 6734 | +** return the number of free pages that should be removed by the |
| 6735 | +** autovacuum. ^If the callback returns zero, then no autovacuum happens. |
| 6736 | +** ^If the value returned is greater than or equal to the number of |
| 6737 | +** free pages, then a complete autovacuum happens. |
| 6738 | +** |
| 6739 | +** <p>^If there are multiple ATTACH-ed database files that are being |
| 6740 | +** modified as part of a transaction commit, then the autovacuum pages |
| 6741 | +** callback is invoked separately for each file. |
| 6742 | +** |
| 6743 | +** <p><b>The callback is not reentrant.</b> The callback function should |
| 6744 | +** not attempt to invoke any other SQLite interface. If it does, bad |
| 6745 | +** things may happen, including segmentation faults and corrupt database |
| 6746 | +** files. The callback function should be a simple function that |
| 6747 | +** does some arithmetic on its input parameters and returns a result. |
| 6748 | +** |
| 6749 | +** ^The X parameter to sqlite3_autovacuum_pages(D,C,P,X) is an optional |
| 6750 | +** destructor for the P parameter. ^If X is not NULL, then X(P) is |
| 6751 | +** invoked whenever the database connection closes or when the callback |
| 6752 | +** is overwritten by another invocation of sqlite3_autovacuum_pages(). |
| 6753 | +** |
| 6754 | +** <p>^There is only one autovacuum pages callback per database connection. |
| 6755 | +** ^Each call to the sqlite3_autovacuum_pages() interface overrides all |
| 6756 | +** previous invocations for that database connection. ^If the callback |
| 6757 | +** argument (C) to sqlite3_autovacuum_pages(D,C,P,X) is a NULL pointer, |
| 6758 | +** then the autovacuum steps callback is cancelled. The return value |
| 6759 | +** from sqlite3_autovacuum_pages() is normally SQLITE_OK, but might |
| 6760 | +** be some other error code if something goes wrong. The current |
| 6761 | +** implementation will only return SQLITE_OK or SQLITE_MISUSE, but other |
| 6762 | +** return codes might be added in future releases. |
| 6763 | +** |
| 6764 | +** <p>If no autovacuum pages callback is specified (the usual case) or |
| 6765 | +** a NULL pointer is provided for the callback, |
| 6766 | +** then the default behavior is to vacuum all free pages. So, in other |
| 6767 | +** words, the default behavior is the same as if the callback function |
| 6768 | +** were something like this: |
| 6769 | +** |
| 6770 | +** <blockquote><pre> |
| 6771 | +** unsigned int demonstration_autovac_pages_callback( |
| 6772 | +** void *pClientData, |
| 6773 | +** const char *zSchema, |
| 6774 | +** unsigned int nDbPage, |
| 6775 | +** unsigned int nFreePage, |
| 6776 | +** unsigned int nBytePerPage |
| 6777 | +** ){ |
| 6778 | +** return nFreePage; |
| 6779 | +** } |
| 6780 | +** </pre></blockquote> |
| 6781 | +*/ |
| 6782 | +SQLITE_API int sqlite3_autovacuum_pages( |
| 6783 | + sqlite3 *db, |
| 6784 | + unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int), |
| 6785 | + void*, |
| 6786 | + void(*)(void*) |
| 6787 | +); |
| 6788 | + |
| 6714 | 6789 | |
| 6715 | 6790 | /* |
| 6716 | 6791 | ** CAPI3REF: Data Change Notification Callbacks |
| 6717 | 6792 | ** METHOD: sqlite3 |
| 6718 | 6793 | ** |
| | @@ -14078,15 +14153,29 @@ |
| 14078 | 14153 | int nBusy; /* Incremented with each busy call */ |
| 14079 | 14154 | }; |
| 14080 | 14155 | |
| 14081 | 14156 | /* |
| 14082 | 14157 | ** Name of table that holds the database schema. |
| 14158 | +** |
| 14159 | +** The PREFERRED names are used whereever possible. But LEGACY is also |
| 14160 | +** used for backwards compatibility. |
| 14161 | +** |
| 14162 | +** 1. Queries can use either the PREFERRED or the LEGACY names |
| 14163 | +** 2. The sqlite3_set_authorizer() callback uses the LEGACY name |
| 14164 | +** 3. The PRAGMA table_list statement uses the PREFERRED name |
| 14165 | +** |
| 14166 | +** The LEGACY names are stored in the internal symbol hash table |
| 14167 | +** in support of (2). Names are translated using sqlite3PreferredTableName() |
| 14168 | +** for (3). The sqlite3FindTable() function takes care of translating |
| 14169 | +** names for (1). |
| 14170 | +** |
| 14171 | +** Note that "sqlite_temp_schema" can also be called "temp.sqlite_schema". |
| 14083 | 14172 | */ |
| 14084 | | -#define DFLT_SCHEMA_TABLE "sqlite_master" |
| 14085 | | -#define DFLT_TEMP_SCHEMA_TABLE "sqlite_temp_master" |
| 14086 | | -#define ALT_SCHEMA_TABLE "sqlite_schema" |
| 14087 | | -#define ALT_TEMP_SCHEMA_TABLE "sqlite_temp_schema" |
| 14173 | +#define LEGACY_SCHEMA_TABLE "sqlite_master" |
| 14174 | +#define LEGACY_TEMP_SCHEMA_TABLE "sqlite_temp_master" |
| 14175 | +#define PREFERRED_SCHEMA_TABLE "sqlite_schema" |
| 14176 | +#define PREFERRED_TEMP_SCHEMA_TABLE "sqlite_temp_schema" |
| 14088 | 14177 | |
| 14089 | 14178 | |
| 14090 | 14179 | /* |
| 14091 | 14180 | ** The root-page of the schema table. |
| 14092 | 14181 | */ |
| | @@ -14094,11 +14183,11 @@ |
| 14094 | 14183 | |
| 14095 | 14184 | /* |
| 14096 | 14185 | ** The name of the schema table. The name is different for TEMP. |
| 14097 | 14186 | */ |
| 14098 | 14187 | #define SCHEMA_TABLE(x) \ |
| 14099 | | - ((!OMIT_TEMPDB)&&(x==1)?DFLT_TEMP_SCHEMA_TABLE:DFLT_SCHEMA_TABLE) |
| 14188 | + ((!OMIT_TEMPDB)&&(x==1)?LEGACY_TEMP_SCHEMA_TABLE:LEGACY_SCHEMA_TABLE) |
| 14100 | 14189 | |
| 14101 | 14190 | /* |
| 14102 | 14191 | ** A convenience macro that returns the number of elements in |
| 14103 | 14192 | ** an array. |
| 14104 | 14193 | */ |
| | @@ -16437,10 +16526,13 @@ |
| 16437 | 16526 | int (*xCommitCallback)(void*); /* Invoked at every commit. */ |
| 16438 | 16527 | void *pRollbackArg; /* Argument to xRollbackCallback() */ |
| 16439 | 16528 | void (*xRollbackCallback)(void*); /* Invoked at every commit. */ |
| 16440 | 16529 | void *pUpdateArg; |
| 16441 | 16530 | void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64); |
| 16531 | + void *pAutovacPagesArg; /* Client argument to autovac_pages */ |
| 16532 | + void (*xAutovacDestr)(void*); /* Destructor for pAutovacPAgesArg */ |
| 16533 | + unsigned int (*xAutovacPages)(void*,const char*,u32,u32,u32); |
| 16442 | 16534 | Parse *pParse; /* Current parse */ |
| 16443 | 16535 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 16444 | 16536 | void *pPreUpdateArg; /* First argument to xPreUpdateCallback */ |
| 16445 | 16537 | void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */ |
| 16446 | 16538 | void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64 |
| | @@ -16566,10 +16658,11 @@ |
| 16566 | 16658 | #define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/ |
| 16567 | 16659 | #define SQLITE_EnableView 0x80000000 /* Enable the use of views */ |
| 16568 | 16660 | #define SQLITE_CountRows HI(0x00001) /* Count rows changed by INSERT, */ |
| 16569 | 16661 | /* DELETE, or UPDATE and return */ |
| 16570 | 16662 | /* the count using a callback. */ |
| 16663 | +#define SQLITE_CorruptRdOnly HI(0x00002) /* Prohibit writes due to error */ |
| 16571 | 16664 | |
| 16572 | 16665 | /* Flags used only if debugging */ |
| 16573 | 16666 | #ifdef SQLITE_DEBUG |
| 16574 | 16667 | #define SQLITE_SqlTrace HI(0x0100000) /* Debug print SQL as it executes */ |
| 16575 | 16668 | #define SQLITE_VdbeListing HI(0x0200000) /* Debug listings of VDBE progs */ |
| | @@ -18694,12 +18787,14 @@ |
| 18694 | 18787 | } InitData; |
| 18695 | 18788 | |
| 18696 | 18789 | /* |
| 18697 | 18790 | ** Allowed values for mInitFlags |
| 18698 | 18791 | */ |
| 18792 | +#define INITFLAG_AlterMask 0x0003 /* Types of ALTER */ |
| 18699 | 18793 | #define INITFLAG_AlterRename 0x0001 /* Reparse after a RENAME */ |
| 18700 | 18794 | #define INITFLAG_AlterDrop 0x0002 /* Reparse after a DROP COLUMN */ |
| 18795 | +#define INITFLAG_AlterAdd 0x0003 /* Reparse after an ADD COLUMN */ |
| 18701 | 18796 | |
| 18702 | 18797 | /* Tuning parameters are set using SQLITE_TESTCTRL_TUNE and are controlled |
| 18703 | 18798 | ** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning |
| 18704 | 18799 | ** parameters are for temporary use during development, to help find |
| 18705 | 18800 | ** optimial values for parameters in the query planner. The should not |
| | @@ -18816,12 +18911,12 @@ |
| 18816 | 18911 | union { /* Extra data for callback */ |
| 18817 | 18912 | NameContext *pNC; /* Naming context */ |
| 18818 | 18913 | int n; /* A counter */ |
| 18819 | 18914 | int iCur; /* A cursor number */ |
| 18820 | 18915 | SrcList *pSrcList; /* FROM clause */ |
| 18821 | | - struct SrcCount *pSrcCount; /* Counting column references */ |
| 18822 | 18916 | struct CCurHint *pCCurHint; /* Used by codeCursorHint() */ |
| 18917 | + struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */ |
| 18823 | 18918 | int *aiCol; /* array of column indexes */ |
| 18824 | 18919 | struct IdxCover *pIdxCover; /* Check for index coverage */ |
| 18825 | 18920 | struct IdxExprTrans *pIdxTrans; /* Convert idxed expr to column */ |
| 18826 | 18921 | ExprList *pGroupBy; /* GROUP BY clause */ |
| 18827 | 18922 | Select *pSelect; /* HAVING to WHERE clause ctx */ |
| | @@ -19458,10 +19553,11 @@ |
| 19458 | 19553 | SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int); |
| 19459 | 19554 | SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*); |
| 19460 | 19555 | #define LOCATE_VIEW 0x01 |
| 19461 | 19556 | #define LOCATE_NOERR 0x02 |
| 19462 | 19557 | SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*); |
| 19558 | +SQLITE_PRIVATE const char *sqlite3PreferredTableName(const char*); |
| 19463 | 19559 | SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,u32 flags,SrcItem *); |
| 19464 | 19560 | SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*); |
| 19465 | 19561 | SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*); |
| 19466 | 19562 | SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*); |
| 19467 | 19563 | SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*,Expr*); |
| | @@ -19474,11 +19570,11 @@ |
| 19474 | 19570 | SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int); |
| 19475 | 19571 | SQLITE_PRIVATE void sqlite3AggInfoPersistWalkerInit(Walker*,Parse*); |
| 19476 | 19572 | SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*); |
| 19477 | 19573 | SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*); |
| 19478 | 19574 | SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx); |
| 19479 | | -SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*); |
| 19575 | +SQLITE_PRIVATE int sqlite3ReferencesSrcList(Parse*, Expr*, SrcList*); |
| 19480 | 19576 | SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*); |
| 19481 | 19577 | #ifndef SQLITE_UNTESTABLE |
| 19482 | 19578 | SQLITE_PRIVATE void sqlite3PrngSaveState(void); |
| 19483 | 19579 | SQLITE_PRIVATE void sqlite3PrngRestoreState(void); |
| 19484 | 19580 | #endif |
| | @@ -19916,13 +20012,15 @@ |
| 19916 | 20012 | #endif |
| 19917 | 20013 | SQLITE_PRIVATE int sqlite3ReadOnlyShadowTables(sqlite3 *db); |
| 19918 | 20014 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 19919 | 20015 | SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName); |
| 19920 | 20016 | SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*); |
| 20017 | +SQLITE_PRIVATE void sqlite3MarkAllShadowTablesOf(sqlite3*, Table*); |
| 19921 | 20018 | #else |
| 19922 | 20019 | # define sqlite3ShadowTableName(A,B) 0 |
| 19923 | 20020 | # define sqlite3IsShadowTableOf(A,B,C) 0 |
| 20021 | +# define sqlite3MarkAllShadowTablesOf(A,B) |
| 19924 | 20022 | #endif |
| 19925 | 20023 | SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse*,Module*); |
| 19926 | 20024 | SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3*,Module*); |
| 19927 | 20025 | SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*); |
| 19928 | 20026 | SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int); |
| | @@ -22105,11 +22203,15 @@ |
| 22105 | 22203 | SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double); |
| 22106 | 22204 | #endif |
| 22107 | 22205 | SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*)); |
| 22108 | 22206 | SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem*,sqlite3*,u16); |
| 22109 | 22207 | SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*); |
| 22208 | +#ifndef SQLITE_OMIT_INCRBLOB |
| 22110 | 22209 | SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int); |
| 22210 | +#else |
| 22211 | +SQLITE_PRIVATE int sqlite3VdbeMemSetZeroBlob(Mem*,int); |
| 22212 | +#endif |
| 22111 | 22213 | #ifdef SQLITE_DEBUG |
| 22112 | 22214 | SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem*); |
| 22113 | 22215 | #endif |
| 22114 | 22216 | SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem*); |
| 22115 | 22217 | SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*); |
| | @@ -24126,16 +24228,19 @@ |
| 24126 | 24228 | pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile); |
| 24127 | 24229 | if( pFile ){ |
| 24128 | 24230 | rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags); |
| 24129 | 24231 | if( rc!=SQLITE_OK ){ |
| 24130 | 24232 | sqlite3_free(pFile); |
| 24233 | + *ppFile = 0; |
| 24131 | 24234 | }else{ |
| 24132 | 24235 | *ppFile = pFile; |
| 24133 | 24236 | } |
| 24134 | 24237 | }else{ |
| 24238 | + *ppFile = 0; |
| 24135 | 24239 | rc = SQLITE_NOMEM_BKPT; |
| 24136 | 24240 | } |
| 24241 | + assert( *ppFile!=0 || rc!=SQLITE_OK ); |
| 24137 | 24242 | return rc; |
| 24138 | 24243 | } |
| 24139 | 24244 | SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *pFile){ |
| 24140 | 24245 | assert( pFile ); |
| 24141 | 24246 | sqlite3OsClose(pFile); |
| | @@ -38079,11 +38184,13 @@ |
| 38079 | 38184 | } |
| 38080 | 38185 | } |
| 38081 | 38186 | |
| 38082 | 38187 | /* Forward declaration */ |
| 38083 | 38188 | static int unixGetTempname(int nBuf, char *zBuf); |
| 38084 | | -static int unixFcntlExternalReader(unixFile*, int*); |
| 38189 | +#ifndef SQLITE_OMIT_WAL |
| 38190 | + static int unixFcntlExternalReader(unixFile*, int*); |
| 38191 | +#endif |
| 38085 | 38192 | |
| 38086 | 38193 | /* |
| 38087 | 38194 | ** Information and control of an open file handle. |
| 38088 | 38195 | */ |
| 38089 | 38196 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
| | @@ -38198,11 +38305,16 @@ |
| 38198 | 38305 | return proxyFileControl(id,op,pArg); |
| 38199 | 38306 | } |
| 38200 | 38307 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
| 38201 | 38308 | |
| 38202 | 38309 | case SQLITE_FCNTL_EXTERNAL_READER: { |
| 38310 | +#ifndef SQLITE_OMIT_WAL |
| 38203 | 38311 | return unixFcntlExternalReader((unixFile*)id, (int*)pArg); |
| 38312 | +#else |
| 38313 | + *(int*)pArg = 0; |
| 38314 | + return SQLITE_OK; |
| 38315 | +#endif |
| 38204 | 38316 | } |
| 38205 | 38317 | } |
| 38206 | 38318 | return SQLITE_NOTFOUND; |
| 38207 | 38319 | } |
| 38208 | 38320 | |
| | @@ -40252,10 +40364,15 @@ |
| 40252 | 40364 | if( randomnessPid!=osGetpid(0) ){ |
| 40253 | 40365 | randomnessPid = osGetpid(0); |
| 40254 | 40366 | sqlite3_randomness(0,0); |
| 40255 | 40367 | } |
| 40256 | 40368 | memset(p, 0, sizeof(unixFile)); |
| 40369 | + |
| 40370 | +#ifdef SQLITE_ASSERT_NO_FILES |
| 40371 | + /* Applications that never read or write a persistent disk files */ |
| 40372 | + assert( zName==0 ); |
| 40373 | +#endif |
| 40257 | 40374 | |
| 40258 | 40375 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
| 40259 | 40376 | UnixUnusedFd *pUnused; |
| 40260 | 40377 | pUnused = findReusableFd(zName, flags); |
| 40261 | 40378 | if( pUnused ){ |
| | @@ -48664,11 +48781,11 @@ |
| 48664 | 48781 | /* |
| 48665 | 48782 | ** Try to enlarge the memory allocation to hold at least sz bytes |
| 48666 | 48783 | */ |
| 48667 | 48784 | static int memdbEnlarge(MemStore *p, sqlite3_int64 newSz){ |
| 48668 | 48785 | unsigned char *pNew; |
| 48669 | | - if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){ |
| 48786 | + if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || NEVER(p->nMmap>0) ){ |
| 48670 | 48787 | return SQLITE_FULL; |
| 48671 | 48788 | } |
| 48672 | 48789 | if( newSz>p->szMax ){ |
| 48673 | 48790 | return SQLITE_FULL; |
| 48674 | 48791 | } |
| | @@ -48723,12 +48840,13 @@ |
| 48723 | 48840 | */ |
| 48724 | 48841 | static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){ |
| 48725 | 48842 | MemStore *p = ((MemFile*)pFile)->pStore; |
| 48726 | 48843 | int rc = SQLITE_OK; |
| 48727 | 48844 | memdbEnter(p); |
| 48728 | | - if( NEVER(size>p->sz) ){ |
| 48729 | | - rc = SQLITE_FULL; |
| 48845 | + if( size>p->sz ){ |
| 48846 | + /* This can only happen with a corrupt wal mode db */ |
| 48847 | + rc = SQLITE_CORRUPT; |
| 48730 | 48848 | }else{ |
| 48731 | 48849 | p->sz = size; |
| 48732 | 48850 | } |
| 48733 | 48851 | memdbLeave(p); |
| 48734 | 48852 | return rc; |
| | @@ -48863,11 +48981,11 @@ |
| 48863 | 48981 | int iAmt, |
| 48864 | 48982 | void **pp |
| 48865 | 48983 | ){ |
| 48866 | 48984 | MemStore *p = ((MemFile*)pFile)->pStore; |
| 48867 | 48985 | memdbEnter(p); |
| 48868 | | - if( iOfst+iAmt>p->sz ){ |
| 48986 | + if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)!=0 ){ |
| 48869 | 48987 | *pp = 0; |
| 48870 | 48988 | }else{ |
| 48871 | 48989 | p->nMmap++; |
| 48872 | 48990 | *pp = (void*)(p->aData + iOfst); |
| 48873 | 48991 | } |
| | @@ -48897,13 +49015,12 @@ |
| 48897 | 49015 | int *pOutFlags |
| 48898 | 49016 | ){ |
| 48899 | 49017 | MemFile *pFile = (MemFile*)pFd; |
| 48900 | 49018 | MemStore *p = 0; |
| 48901 | 49019 | int szName; |
| 48902 | | - if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){ |
| 48903 | | - return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFd, flags, pOutFlags); |
| 48904 | | - } |
| 49020 | + UNUSED_PARAMETER(pVfs); |
| 49021 | + |
| 48905 | 49022 | memset(pFile, 0, sizeof(*pFile)); |
| 48906 | 49023 | szName = sqlite3Strlen30(zName); |
| 48907 | 49024 | if( szName>1 && zName[0]=='/' ){ |
| 48908 | 49025 | int i; |
| 48909 | 49026 | #ifndef SQLITE_MUTEX_OMIT |
| | @@ -48959,12 +49076,13 @@ |
| 48959 | 49076 | memset(p, 0, sizeof(*p)); |
| 48960 | 49077 | p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE; |
| 48961 | 49078 | p->szMax = sqlite3GlobalConfig.mxMemdbSize; |
| 48962 | 49079 | } |
| 48963 | 49080 | pFile->pStore = p; |
| 48964 | | - assert( pOutFlags!=0 ); /* True because flags==SQLITE_OPEN_MAIN_DB */ |
| 48965 | | - *pOutFlags = flags | SQLITE_OPEN_MEMORY; |
| 49081 | + if( pOutFlags!=0 ){ |
| 49082 | + *pOutFlags = flags | SQLITE_OPEN_MEMORY; |
| 49083 | + } |
| 48966 | 49084 | pFd->pMethods = &memdb_io_methods; |
| 48967 | 49085 | memdbLeave(p); |
| 48968 | 49086 | return SQLITE_OK; |
| 48969 | 49087 | } |
| 48970 | 49088 | |
| | @@ -53155,10 +53273,11 @@ |
| 53155 | 53273 | u8 walSyncFlags; /* See description above */ |
| 53156 | 53274 | u8 tempFile; /* zFilename is a temporary or immutable file */ |
| 53157 | 53275 | u8 noLock; /* Do not lock (except in WAL mode) */ |
| 53158 | 53276 | u8 readOnly; /* True for a read-only database */ |
| 53159 | 53277 | u8 memDb; /* True to inhibit all file I/O */ |
| 53278 | + u8 memVfs; /* VFS-implemented memory database */ |
| 53160 | 53279 | |
| 53161 | 53280 | /************************************************************************** |
| 53162 | 53281 | ** The following block contains those class members that change during |
| 53163 | 53282 | ** routine operation. Class members not in this block are either fixed |
| 53164 | 53283 | ** when the pager is first created or else only change when there is a |
| | @@ -57397,11 +57516,11 @@ |
| 57397 | 57516 | if( zFilename && zFilename[0] ){ |
| 57398 | 57517 | int fout = 0; /* VFS flags returned by xOpen() */ |
| 57399 | 57518 | rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout); |
| 57400 | 57519 | assert( !memDb ); |
| 57401 | 57520 | #ifndef SQLITE_OMIT_DESERIALIZE |
| 57402 | | - memJM = (fout&SQLITE_OPEN_MEMORY)!=0; |
| 57521 | + pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY)!=0; |
| 57403 | 57522 | #endif |
| 57404 | 57523 | readOnly = (fout&SQLITE_OPEN_READONLY)!=0; |
| 57405 | 57524 | |
| 57406 | 57525 | /* If the file was successfully opened for read/write access, |
| 57407 | 57526 | ** choose a default page size in case we have to create the |
| | @@ -59334,11 +59453,11 @@ |
| 59334 | 59453 | |
| 59335 | 59454 | /* |
| 59336 | 59455 | ** Return true if this is an in-memory or temp-file backed pager. |
| 59337 | 59456 | */ |
| 59338 | 59457 | SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){ |
| 59339 | | - return pPager->tempFile; |
| 59458 | + return pPager->tempFile || pPager->memVfs; |
| 59340 | 59459 | } |
| 59341 | 59460 | |
| 59342 | 59461 | /* |
| 59343 | 59462 | ** Check that there are at least nSavepoint savepoints open. If there are |
| 59344 | 59463 | ** currently less than nSavepoints open, then open one or more savepoints |
| | @@ -60859,13 +60978,17 @@ |
| 60859 | 60978 | ** If the wal-index is currently smaller the iPage pages then the size |
| 60860 | 60979 | ** of the wal-index might be increased, but only if it is safe to do |
| 60861 | 60980 | ** so. It is safe to enlarge the wal-index if pWal->writeLock is true |
| 60862 | 60981 | ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE. |
| 60863 | 60982 | ** |
| 60864 | | -** If this call is successful, *ppPage is set to point to the wal-index |
| 60865 | | -** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs, |
| 60866 | | -** then an SQLite error code is returned and *ppPage is set to 0. |
| 60983 | +** Three possible result scenarios: |
| 60984 | +** |
| 60985 | +** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page |
| 60986 | +** (2) rc>=SQLITE_ERROR and *ppPage==NULL |
| 60987 | +** (3) rc==SQLITE_OK and *ppPage==NULL // only if iPage==0 |
| 60988 | +** |
| 60989 | +** Scenario (3) can only occur when pWal->writeLock is false and iPage==0 |
| 60867 | 60990 | */ |
| 60868 | 60991 | static SQLITE_NOINLINE int walIndexPageRealloc( |
| 60869 | 60992 | Wal *pWal, /* The WAL context */ |
| 60870 | 60993 | int iPage, /* The page we seek */ |
| 60871 | 60994 | volatile u32 **ppPage /* Write the page pointer here */ |
| | @@ -60894,11 +61017,13 @@ |
| 60894 | 61017 | if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT; |
| 60895 | 61018 | }else{ |
| 60896 | 61019 | rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, |
| 60897 | 61020 | pWal->writeLock, (void volatile **)&pWal->apWiData[iPage] |
| 60898 | 61021 | ); |
| 60899 | | - assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 ); |
| 61022 | + assert( pWal->apWiData[iPage]!=0 |
| 61023 | + || rc!=SQLITE_OK |
| 61024 | + || (pWal->writeLock==0 && iPage==0) ); |
| 60900 | 61025 | testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK ); |
| 60901 | 61026 | if( rc==SQLITE_OK ){ |
| 60902 | 61027 | if( iPage>0 && sqlite3FaultSim(600) ) rc = SQLITE_NOMEM; |
| 60903 | 61028 | }else if( (rc&0xff)==SQLITE_READONLY ){ |
| 60904 | 61029 | pWal->readOnly |= WAL_SHM_RDONLY; |
| | @@ -61233,12 +61358,12 @@ |
| 61233 | 61358 | ** in the wal-index file. Set pLoc->iZero to one less than the frame |
| 61234 | 61359 | ** number of the first frame indexed by this hash table. If a |
| 61235 | 61360 | ** slot in the hash table is set to N, it refers to frame number |
| 61236 | 61361 | ** (pLoc->iZero+N) in the log. |
| 61237 | 61362 | ** |
| 61238 | | -** Finally, set pLoc->aPgno so that pLoc->aPgno[1] is the page number of the |
| 61239 | | -** first frame indexed by the hash table, frame (pLoc->iZero+1). |
| 61363 | +** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the |
| 61364 | +** first frame indexed by the hash table, frame (pLoc->iZero). |
| 61240 | 61365 | */ |
| 61241 | 61366 | static int walHashGet( |
| 61242 | 61367 | Wal *pWal, /* WAL handle */ |
| 61243 | 61368 | int iHash, /* Find the iHash'th table */ |
| 61244 | 61369 | WalHashLoc *pLoc /* OUT: Hash table location */ |
| | @@ -61246,19 +61371,20 @@ |
| 61246 | 61371 | int rc; /* Return code */ |
| 61247 | 61372 | |
| 61248 | 61373 | rc = walIndexPage(pWal, iHash, &pLoc->aPgno); |
| 61249 | 61374 | assert( rc==SQLITE_OK || iHash>0 ); |
| 61250 | 61375 | |
| 61251 | | - if( rc==SQLITE_OK ){ |
| 61376 | + if( pLoc->aPgno ){ |
| 61252 | 61377 | pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE]; |
| 61253 | 61378 | if( iHash==0 ){ |
| 61254 | 61379 | pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)]; |
| 61255 | 61380 | pLoc->iZero = 0; |
| 61256 | 61381 | }else{ |
| 61257 | 61382 | pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE; |
| 61258 | 61383 | } |
| 61259 | | - pLoc->aPgno = &pLoc->aPgno[-1]; |
| 61384 | + }else if( NEVER(rc==SQLITE_OK) ){ |
| 61385 | + rc = SQLITE_ERROR; |
| 61260 | 61386 | } |
| 61261 | 61387 | return rc; |
| 61262 | 61388 | } |
| 61263 | 61389 | |
| 61264 | 61390 | /* |
| | @@ -61336,25 +61462,26 @@ |
| 61336 | 61462 | } |
| 61337 | 61463 | |
| 61338 | 61464 | /* Zero the entries in the aPgno array that correspond to frames with |
| 61339 | 61465 | ** frame numbers greater than pWal->hdr.mxFrame. |
| 61340 | 61466 | */ |
| 61341 | | - nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit+1]); |
| 61342 | | - memset((void *)&sLoc.aPgno[iLimit+1], 0, nByte); |
| 61467 | + nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]); |
| 61468 | + assert( nByte>=0 ); |
| 61469 | + memset((void *)&sLoc.aPgno[iLimit], 0, nByte); |
| 61343 | 61470 | |
| 61344 | 61471 | #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT |
| 61345 | 61472 | /* Verify that the every entry in the mapping region is still reachable |
| 61346 | 61473 | ** via the hash table even after the cleanup. |
| 61347 | 61474 | */ |
| 61348 | 61475 | if( iLimit ){ |
| 61349 | 61476 | int j; /* Loop counter */ |
| 61350 | 61477 | int iKey; /* Hash key */ |
| 61351 | | - for(j=1; j<=iLimit; j++){ |
| 61478 | + for(j=0; j<iLimit; j++){ |
| 61352 | 61479 | for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){ |
| 61353 | | - if( sLoc.aHash[iKey]==j ) break; |
| 61480 | + if( sLoc.aHash[iKey]==j+1 ) break; |
| 61354 | 61481 | } |
| 61355 | | - assert( sLoc.aHash[iKey]==j ); |
| 61482 | + assert( sLoc.aHash[iKey]==j+1 ); |
| 61356 | 61483 | } |
| 61357 | 61484 | } |
| 61358 | 61485 | #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */ |
| 61359 | 61486 | } |
| 61360 | 61487 | |
| | @@ -61382,32 +61509,32 @@ |
| 61382 | 61509 | |
| 61383 | 61510 | /* If this is the first entry to be added to this hash-table, zero the |
| 61384 | 61511 | ** entire hash table and aPgno[] array before proceeding. |
| 61385 | 61512 | */ |
| 61386 | 61513 | if( idx==1 ){ |
| 61387 | | - int nByte = (int)((u8 *)&sLoc.aHash[HASHTABLE_NSLOT] |
| 61388 | | - - (u8 *)&sLoc.aPgno[1]); |
| 61389 | | - memset((void*)&sLoc.aPgno[1], 0, nByte); |
| 61514 | + int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno); |
| 61515 | + assert( nByte>=0 ); |
| 61516 | + memset((void*)sLoc.aPgno, 0, nByte); |
| 61390 | 61517 | } |
| 61391 | 61518 | |
| 61392 | 61519 | /* If the entry in aPgno[] is already set, then the previous writer |
| 61393 | 61520 | ** must have exited unexpectedly in the middle of a transaction (after |
| 61394 | 61521 | ** writing one or more dirty pages to the WAL to free up memory). |
| 61395 | 61522 | ** Remove the remnants of that writers uncommitted transaction from |
| 61396 | 61523 | ** the hash-table before writing any new entries. |
| 61397 | 61524 | */ |
| 61398 | | - if( sLoc.aPgno[idx] ){ |
| 61525 | + if( sLoc.aPgno[idx-1] ){ |
| 61399 | 61526 | walCleanupHash(pWal); |
| 61400 | | - assert( !sLoc.aPgno[idx] ); |
| 61527 | + assert( !sLoc.aPgno[idx-1] ); |
| 61401 | 61528 | } |
| 61402 | 61529 | |
| 61403 | 61530 | /* Write the aPgno[] array entry and the hash-table slot. */ |
| 61404 | 61531 | nCollide = idx; |
| 61405 | 61532 | for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){ |
| 61406 | 61533 | if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT; |
| 61407 | 61534 | } |
| 61408 | | - sLoc.aPgno[idx] = iPage; |
| 61535 | + sLoc.aPgno[idx-1] = iPage; |
| 61409 | 61536 | AtomicStore(&sLoc.aHash[iKey], (ht_slot)idx); |
| 61410 | 61537 | |
| 61411 | 61538 | #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT |
| 61412 | 61539 | /* Verify that the number of entries in the hash table exactly equals |
| 61413 | 61540 | ** the number of entries in the mapping region. |
| | @@ -61424,22 +61551,21 @@ |
| 61424 | 61551 | ** thing to check, so only do this occasionally - not on every |
| 61425 | 61552 | ** iteration. |
| 61426 | 61553 | */ |
| 61427 | 61554 | if( (idx&0x3ff)==0 ){ |
| 61428 | 61555 | int i; /* Loop counter */ |
| 61429 | | - for(i=1; i<=idx; i++){ |
| 61556 | + for(i=0; i<idx; i++){ |
| 61430 | 61557 | for(iKey=walHash(sLoc.aPgno[i]); |
| 61431 | 61558 | sLoc.aHash[iKey]; |
| 61432 | 61559 | iKey=walNextHash(iKey)){ |
| 61433 | | - if( sLoc.aHash[iKey]==i ) break; |
| 61560 | + if( sLoc.aHash[iKey]==i+1 ) break; |
| 61434 | 61561 | } |
| 61435 | | - assert( sLoc.aHash[iKey]==i ); |
| 61562 | + assert( sLoc.aHash[iKey]==i+1 ); |
| 61436 | 61563 | } |
| 61437 | 61564 | } |
| 61438 | 61565 | #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */ |
| 61439 | 61566 | } |
| 61440 | | - |
| 61441 | 61567 | |
| 61442 | 61568 | return rc; |
| 61443 | 61569 | } |
| 61444 | 61570 | |
| 61445 | 61571 | |
| | @@ -61557,11 +61683,12 @@ |
| 61557 | 61683 | u32 iFrame; /* Index of last frame read */ |
| 61558 | 61684 | u32 iLast = MIN(iLastFrame, HASHTABLE_NPAGE_ONE+iPg*HASHTABLE_NPAGE); |
| 61559 | 61685 | u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE); |
| 61560 | 61686 | u32 nHdr, nHdr32; |
| 61561 | 61687 | rc = walIndexPage(pWal, iPg, (volatile u32**)&aShare); |
| 61562 | | - if( rc ) break; |
| 61688 | + assert( aShare!=0 || rc!=SQLITE_OK ); |
| 61689 | + if( aShare==0 ) break; |
| 61563 | 61690 | pWal->apWiData[iPg] = aPrivate; |
| 61564 | 61691 | |
| 61565 | 61692 | for(iFrame=iFirst; iFrame<=iLast; iFrame++){ |
| 61566 | 61693 | i64 iOffset = walFrameOffset(iFrame, szPage); |
| 61567 | 61694 | u32 pgno; /* Database page number for frame */ |
| | @@ -62054,11 +62181,10 @@ |
| 62054 | 62181 | if( rc==SQLITE_OK ){ |
| 62055 | 62182 | int j; /* Counter variable */ |
| 62056 | 62183 | int nEntry; /* Number of entries in this segment */ |
| 62057 | 62184 | ht_slot *aIndex; /* Sorted index for this segment */ |
| 62058 | 62185 | |
| 62059 | | - sLoc.aPgno++; |
| 62060 | 62186 | if( (i+1)==nSegment ){ |
| 62061 | 62187 | nEntry = (int)(iLast - sLoc.iZero); |
| 62062 | 62188 | }else{ |
| 62063 | 62189 | nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno); |
| 62064 | 62190 | } |
| | @@ -63193,11 +63319,12 @@ |
| 63193 | 63319 | i64 iDbOff; /* Offset of db file entry */ |
| 63194 | 63320 | i64 iWalOff; /* Offset of wal file entry */ |
| 63195 | 63321 | |
| 63196 | 63322 | rc = walHashGet(pWal, walFramePage(i), &sLoc); |
| 63197 | 63323 | if( rc!=SQLITE_OK ) break; |
| 63198 | | - pgno = sLoc.aPgno[i-sLoc.iZero]; |
| 63324 | + assert( i - sLoc.iZero - 1 >=0 ); |
| 63325 | + pgno = sLoc.aPgno[i-sLoc.iZero-1]; |
| 63199 | 63326 | iDbOff = (i64)(pgno-1) * szPage; |
| 63200 | 63327 | |
| 63201 | 63328 | if( iDbOff+szPage<=szDb ){ |
| 63202 | 63329 | iWalOff = walFrameOffset(i, szPage) + WAL_FRAME_HDRSIZE; |
| 63203 | 63330 | rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff); |
| | @@ -63426,11 +63553,11 @@ |
| 63426 | 63553 | } |
| 63427 | 63554 | nCollide = HASHTABLE_NSLOT; |
| 63428 | 63555 | iKey = walHash(pgno); |
| 63429 | 63556 | while( (iH = AtomicLoad(&sLoc.aHash[iKey]))!=0 ){ |
| 63430 | 63557 | u32 iFrame = iH + sLoc.iZero; |
| 63431 | | - if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH]==pgno ){ |
| 63558 | + if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){ |
| 63432 | 63559 | assert( iFrame>iRead || CORRUPT_DB ); |
| 63433 | 63560 | iRead = iFrame; |
| 63434 | 63561 | } |
| 63435 | 63562 | if( (nCollide--)==0 ){ |
| 63436 | 63563 | return SQLITE_CORRUPT_BKPT; |
| | @@ -66919,11 +67046,11 @@ |
| 66919 | 67046 | if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage); |
| 66920 | 67047 | sz2 = get2byte(&data[iFree2+2]); |
| 66921 | 67048 | if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage); |
| 66922 | 67049 | memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz)); |
| 66923 | 67050 | sz += sz2; |
| 66924 | | - }else if( iFree+sz>usableSize ){ |
| 67051 | + }else if( NEVER(iFree+sz>usableSize) ){ |
| 66925 | 67052 | return SQLITE_CORRUPT_PAGE(pPage); |
| 66926 | 67053 | } |
| 66927 | 67054 | |
| 66928 | 67055 | cbrk = top+sz; |
| 66929 | 67056 | assert( cbrk+(iFree-top) <= usableSize ); |
| | @@ -69370,27 +69497,30 @@ |
| 69370 | 69497 | } |
| 69371 | 69498 | |
| 69372 | 69499 | /* |
| 69373 | 69500 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
| 69374 | 69501 | ** is committed for an auto-vacuum database. |
| 69375 | | -** |
| 69376 | | -** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 69377 | | -** the database file should be truncated to during the commit process. |
| 69378 | | -** i.e. the database has been reorganized so that only the first *pnTrunc |
| 69379 | | -** pages are in use. |
| 69380 | 69502 | */ |
| 69381 | | -static int autoVacuumCommit(BtShared *pBt){ |
| 69503 | +static int autoVacuumCommit(Btree *p){ |
| 69382 | 69504 | int rc = SQLITE_OK; |
| 69383 | | - Pager *pPager = pBt->pPager; |
| 69384 | | - VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); ) |
| 69505 | + Pager *pPager; |
| 69506 | + BtShared *pBt; |
| 69507 | + sqlite3 *db; |
| 69508 | + VVA_ONLY( int nRef ); |
| 69509 | + |
| 69510 | + assert( p!=0 ); |
| 69511 | + pBt = p->pBt; |
| 69512 | + pPager = pBt->pPager; |
| 69513 | + VVA_ONLY( nRef = sqlite3PagerRefcount(pPager); ) |
| 69385 | 69514 | |
| 69386 | 69515 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 69387 | 69516 | invalidateAllOverflowCache(pBt); |
| 69388 | 69517 | assert(pBt->autoVacuum); |
| 69389 | 69518 | if( !pBt->incrVacuum ){ |
| 69390 | 69519 | Pgno nFin; /* Number of pages in database after autovacuuming */ |
| 69391 | 69520 | Pgno nFree; /* Number of pages on the freelist initially */ |
| 69521 | + Pgno nVac; /* Number of pages to vacuum */ |
| 69392 | 69522 | Pgno iFree; /* The next page to be freed */ |
| 69393 | 69523 | Pgno nOrig; /* Database size before freeing */ |
| 69394 | 69524 | |
| 69395 | 69525 | nOrig = btreePagecount(pBt); |
| 69396 | 69526 | if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| | @@ -69400,22 +69530,46 @@ |
| 69400 | 69530 | */ |
| 69401 | 69531 | return SQLITE_CORRUPT_BKPT; |
| 69402 | 69532 | } |
| 69403 | 69533 | |
| 69404 | 69534 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 69405 | | - nFin = finalDbSize(pBt, nOrig, nFree); |
| 69535 | + db = p->db; |
| 69536 | + if( db->xAutovacPages ){ |
| 69537 | + int iDb; |
| 69538 | + for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){ |
| 69539 | + if( db->aDb[iDb].pBt==p ) break; |
| 69540 | + } |
| 69541 | + nVac = db->xAutovacPages( |
| 69542 | + db->pAutovacPagesArg, |
| 69543 | + db->aDb[iDb].zDbSName, |
| 69544 | + nOrig, |
| 69545 | + nFree, |
| 69546 | + pBt->pageSize |
| 69547 | + ); |
| 69548 | + if( nVac>nFree ){ |
| 69549 | + nVac = nFree; |
| 69550 | + } |
| 69551 | + if( nVac==0 ){ |
| 69552 | + return SQLITE_OK; |
| 69553 | + } |
| 69554 | + }else{ |
| 69555 | + nVac = nFree; |
| 69556 | + } |
| 69557 | + nFin = finalDbSize(pBt, nOrig, nVac); |
| 69406 | 69558 | if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT; |
| 69407 | 69559 | if( nFin<nOrig ){ |
| 69408 | 69560 | rc = saveAllCursors(pBt, 0, 0); |
| 69409 | 69561 | } |
| 69410 | 69562 | for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){ |
| 69411 | | - rc = incrVacuumStep(pBt, nFin, iFree, 1); |
| 69563 | + rc = incrVacuumStep(pBt, nFin, iFree, nVac==nFree); |
| 69412 | 69564 | } |
| 69413 | 69565 | if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ |
| 69414 | 69566 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 69415 | | - put4byte(&pBt->pPage1->aData[32], 0); |
| 69416 | | - put4byte(&pBt->pPage1->aData[36], 0); |
| 69567 | + if( nVac==nFree ){ |
| 69568 | + put4byte(&pBt->pPage1->aData[32], 0); |
| 69569 | + put4byte(&pBt->pPage1->aData[36], 0); |
| 69570 | + } |
| 69417 | 69571 | put4byte(&pBt->pPage1->aData[28], nFin); |
| 69418 | 69572 | pBt->bDoTruncate = 1; |
| 69419 | 69573 | pBt->nPage = nFin; |
| 69420 | 69574 | } |
| 69421 | 69575 | if( rc!=SQLITE_OK ){ |
| | @@ -69462,11 +69616,11 @@ |
| 69462 | 69616 | if( p->inTrans==TRANS_WRITE ){ |
| 69463 | 69617 | BtShared *pBt = p->pBt; |
| 69464 | 69618 | sqlite3BtreeEnter(p); |
| 69465 | 69619 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 69466 | 69620 | if( pBt->autoVacuum ){ |
| 69467 | | - rc = autoVacuumCommit(pBt); |
| 69621 | + rc = autoVacuumCommit(p); |
| 69468 | 69622 | if( rc!=SQLITE_OK ){ |
| 69469 | 69623 | sqlite3BtreeLeave(p); |
| 69470 | 69624 | return rc; |
| 69471 | 69625 | } |
| 69472 | 69626 | } |
| | @@ -77424,10 +77578,12 @@ |
| 77424 | 77578 | nByte = 1; |
| 77425 | 77579 | } |
| 77426 | 77580 | if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){ |
| 77427 | 77581 | return SQLITE_NOMEM_BKPT; |
| 77428 | 77582 | } |
| 77583 | + assert( pMem->z!=0 ); |
| 77584 | + assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte ); |
| 77429 | 77585 | |
| 77430 | 77586 | memset(&pMem->z[pMem->n], 0, pMem->u.nZero); |
| 77431 | 77587 | pMem->n += pMem->u.nZero; |
| 77432 | 77588 | pMem->flags &= ~(MEM_Zero|MEM_Term); |
| 77433 | 77589 | return SQLITE_OK; |
| | @@ -77900,19 +78056,35 @@ |
| 77900 | 78056 | |
| 77901 | 78057 | /* |
| 77902 | 78058 | ** Delete any previous value and set the value to be a BLOB of length |
| 77903 | 78059 | ** n containing all zeros. |
| 77904 | 78060 | */ |
| 78061 | +#ifndef SQLITE_OMIT_INCRBLOB |
| 77905 | 78062 | SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){ |
| 77906 | 78063 | sqlite3VdbeMemRelease(pMem); |
| 77907 | 78064 | pMem->flags = MEM_Blob|MEM_Zero; |
| 77908 | 78065 | pMem->n = 0; |
| 77909 | 78066 | if( n<0 ) n = 0; |
| 77910 | 78067 | pMem->u.nZero = n; |
| 77911 | 78068 | pMem->enc = SQLITE_UTF8; |
| 77912 | 78069 | pMem->z = 0; |
| 77913 | 78070 | } |
| 78071 | +#else |
| 78072 | +SQLITE_PRIVATE int sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){ |
| 78073 | + int nByte = n>0?n:1; |
| 78074 | + if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){ |
| 78075 | + return SQLITE_NOMEM_BKPT; |
| 78076 | + } |
| 78077 | + assert( pMem->z!=0 ); |
| 78078 | + assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte ); |
| 78079 | + memset(pMem->z, 0, nByte); |
| 78080 | + pMem->n = n>0?n:0; |
| 78081 | + pMem->flags = MEM_Blob; |
| 78082 | + pMem->enc = SQLITE_UTF8; |
| 78083 | + return SQLITE_OK; |
| 78084 | +} |
| 78085 | +#endif |
| 77914 | 78086 | |
| 77915 | 78087 | /* |
| 77916 | 78088 | ** The pMem is known to contain content that needs to be destroyed prior |
| 77917 | 78089 | ** to a value change. So invoke the destructor, then set the value to |
| 77918 | 78090 | ** a 64-bit integer. |
| | @@ -82037,13 +82209,19 @@ |
| 82037 | 82209 | |
| 82038 | 82210 | /* Lock all btrees used by the statement */ |
| 82039 | 82211 | sqlite3VdbeEnter(p); |
| 82040 | 82212 | |
| 82041 | 82213 | /* Check for one of the special errors */ |
| 82042 | | - mrc = p->rc & 0xff; |
| 82043 | | - isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR |
| 82044 | | - || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; |
| 82214 | + if( p->rc ){ |
| 82215 | + mrc = p->rc & 0xff; |
| 82216 | + isSpecialError = mrc==SQLITE_NOMEM |
| 82217 | + || mrc==SQLITE_IOERR |
| 82218 | + || mrc==SQLITE_INTERRUPT |
| 82219 | + || mrc==SQLITE_FULL; |
| 82220 | + }else{ |
| 82221 | + mrc = isSpecialError = 0; |
| 82222 | + } |
| 82045 | 82223 | if( isSpecialError ){ |
| 82046 | 82224 | /* If the query was read-only and the error code is SQLITE_INTERRUPT, |
| 82047 | 82225 | ** no rollback is necessary. Otherwise, at least a savepoint |
| 82048 | 82226 | ** transaction must be rolled back to restore the database to a |
| 82049 | 82227 | ** consistent state. |
| | @@ -82091,10 +82269,13 @@ |
| 82091 | 82269 | if( NEVER(p->readOnly) ){ |
| 82092 | 82270 | sqlite3VdbeLeave(p); |
| 82093 | 82271 | return SQLITE_ERROR; |
| 82094 | 82272 | } |
| 82095 | 82273 | rc = SQLITE_CONSTRAINT_FOREIGNKEY; |
| 82274 | + }else if( db->flags & SQLITE_CorruptRdOnly ){ |
| 82275 | + rc = SQLITE_CORRUPT; |
| 82276 | + db->flags &= ~SQLITE_CorruptRdOnly; |
| 82096 | 82277 | }else{ |
| 82097 | 82278 | /* The auto-commit flag is true, the vdbe program was successful |
| 82098 | 82279 | ** or hit an 'OR FAIL' constraint and there are no deferred foreign |
| 82099 | 82280 | ** key constraints to hold up the transaction. This means a commit |
| 82100 | 82281 | ** is required. */ |
| | @@ -84195,10 +84376,12 @@ |
| 84195 | 84376 | }else{ |
| 84196 | 84377 | iKey2 = iKey1; |
| 84197 | 84378 | } |
| 84198 | 84379 | } |
| 84199 | 84380 | |
| 84381 | + assert( pCsr!=0 ); |
| 84382 | + assert( pCsr->eCurType==CURTYPE_BTREE ); |
| 84200 | 84383 | assert( pCsr->nField==pTab->nCol |
| 84201 | 84384 | || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1) |
| 84202 | 84385 | ); |
| 84203 | 84386 | |
| 84204 | 84387 | preupdate.v = v; |
| | @@ -84773,12 +84956,16 @@ |
| 84773 | 84956 | Mem *pOut = pCtx->pOut; |
| 84774 | 84957 | assert( sqlite3_mutex_held(pOut->db->mutex) ); |
| 84775 | 84958 | if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 84776 | 84959 | return SQLITE_TOOBIG; |
| 84777 | 84960 | } |
| 84961 | +#ifndef SQLITE_OMIT_INCRBLOB |
| 84778 | 84962 | sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); |
| 84779 | 84963 | return SQLITE_OK; |
| 84964 | +#else |
| 84965 | + return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); |
| 84966 | +#endif |
| 84780 | 84967 | } |
| 84781 | 84968 | SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ |
| 84782 | 84969 | pCtx->isError = errCode ? errCode : -1; |
| 84783 | 84970 | #ifdef SQLITE_DEBUG |
| 84784 | 84971 | if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; |
| | @@ -85786,11 +85973,15 @@ |
| 85786 | 85973 | SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ |
| 85787 | 85974 | int rc; |
| 85788 | 85975 | Vdbe *p = (Vdbe *)pStmt; |
| 85789 | 85976 | rc = vdbeUnbind(p, i); |
| 85790 | 85977 | if( rc==SQLITE_OK ){ |
| 85978 | +#ifndef SQLITE_OMIT_INCRBLOB |
| 85791 | 85979 | sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); |
| 85980 | +#else |
| 85981 | + rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); |
| 85982 | +#endif |
| 85792 | 85983 | sqlite3_mutex_leave(p->db->mutex); |
| 85793 | 85984 | } |
| 85794 | 85985 | return rc; |
| 85795 | 85986 | } |
| 85796 | 85987 | SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){ |
| | @@ -86074,10 +86265,11 @@ |
| 86074 | 86265 | /* If the old.* record has not yet been loaded into memory, do so now. */ |
| 86075 | 86266 | if( p->pUnpacked==0 ){ |
| 86076 | 86267 | u32 nRec; |
| 86077 | 86268 | u8 *aRec; |
| 86078 | 86269 | |
| 86270 | + assert( p->pCsr->eCurType==CURTYPE_BTREE ); |
| 86079 | 86271 | nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor); |
| 86080 | 86272 | aRec = sqlite3DbMallocRaw(db, nRec); |
| 86081 | 86273 | if( !aRec ) goto preupdate_old_out; |
| 86082 | 86274 | rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec); |
| 86083 | 86275 | if( rc==SQLITE_OK ){ |
| | @@ -89391,20 +89583,25 @@ |
| 89391 | 89583 | rc = SQLITE_CORRUPT_BKPT; |
| 89392 | 89584 | goto abort_due_to_error; |
| 89393 | 89585 | } |
| 89394 | 89586 | } |
| 89395 | 89587 | |
| 89396 | | -/* Opcode: TypeCheck P1 P2 * P4 * |
| 89588 | +/* Opcode: TypeCheck P1 P2 P3 P4 * |
| 89397 | 89589 | ** Synopsis: typecheck(r[P1@P2]) |
| 89398 | 89590 | ** |
| 89399 | 89591 | ** Apply affinities to the range of P2 registers beginning with P1. |
| 89400 | 89592 | ** Take the affinities from the Table object in P4. If any value |
| 89401 | 89593 | ** cannot be coerced into the correct type, then raise an error. |
| 89402 | 89594 | ** |
| 89403 | 89595 | ** This opcode is similar to OP_Affinity except that this opcode |
| 89404 | 89596 | ** forces the register type to the Table column type. This is used |
| 89405 | 89597 | ** to implement "strict affinity". |
| 89598 | +** |
| 89599 | +** GENERATED ALWAYS AS ... STATIC columns are only checked if P3 |
| 89600 | +** is zero. When P3 is non-zero, no type checking occurs for |
| 89601 | +** static generated columns. Virtual columns are computed at query time |
| 89602 | +** and so they are never checked. |
| 89406 | 89603 | ** |
| 89407 | 89604 | ** Preconditions: |
| 89408 | 89605 | ** |
| 89409 | 89606 | ** <ul> |
| 89410 | 89607 | ** <li> P2 should be the number of non-virtual columns in the |
| | @@ -89424,11 +89621,14 @@ |
| 89424 | 89621 | assert( pTab->tabFlags & TF_Strict ); |
| 89425 | 89622 | assert( pTab->nNVCol==pOp->p2 ); |
| 89426 | 89623 | aCol = pTab->aCol; |
| 89427 | 89624 | pIn1 = &aMem[pOp->p1]; |
| 89428 | 89625 | for(i=0; i<pTab->nCol; i++){ |
| 89429 | | - if( aCol[i].colFlags & COLFLAG_VIRTUAL ) continue; |
| 89626 | + if( aCol[i].colFlags & COLFLAG_GENERATED ){ |
| 89627 | + if( aCol[i].colFlags & COLFLAG_VIRTUAL ) continue; |
| 89628 | + if( pOp->p3 ){ pIn1++; continue; } |
| 89629 | + } |
| 89430 | 89630 | assert( pIn1 < &aMem[pOp->p1+pOp->p2] ); |
| 89431 | 89631 | applyAffinity(pIn1, aCol[i].affinity, encoding); |
| 89432 | 89632 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 89433 | 89633 | switch( aCol[i].eCType ){ |
| 89434 | 89634 | case COLTYPE_BLOB: { |
| | @@ -90148,12 +90348,20 @@ |
| 90148 | 90348 | assert( p->bIsReader ); |
| 90149 | 90349 | assert( p->readOnly==0 || pOp->p2==0 ); |
| 90150 | 90350 | assert( pOp->p2>=0 && pOp->p2<=2 ); |
| 90151 | 90351 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 90152 | 90352 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
| 90153 | | - if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){ |
| 90154 | | - rc = SQLITE_READONLY; |
| 90353 | + assert( rc==SQLITE_OK ); |
| 90354 | + if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){ |
| 90355 | + if( db->flags & SQLITE_QueryOnly ){ |
| 90356 | + /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */ |
| 90357 | + rc = SQLITE_READONLY; |
| 90358 | + }else{ |
| 90359 | + /* Writes prohibited due to a prior SQLITE_CORRUPT in the current |
| 90360 | + ** transaction */ |
| 90361 | + rc = SQLITE_CORRUPT; |
| 90362 | + } |
| 90155 | 90363 | goto abort_due_to_error; |
| 90156 | 90364 | } |
| 90157 | 90365 | pBt = db->aDb[pOp->p1].pBt; |
| 90158 | 90366 | |
| 90159 | 90367 | if( pBt ){ |
| | @@ -90191,11 +90399,12 @@ |
| 90191 | 90399 | p->nStmtDefCons = db->nDeferredCons; |
| 90192 | 90400 | p->nStmtDefImmCons = db->nDeferredImmCons; |
| 90193 | 90401 | } |
| 90194 | 90402 | } |
| 90195 | 90403 | assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); |
| 90196 | | - if( pOp->p5 |
| 90404 | + if( rc==SQLITE_OK |
| 90405 | + && pOp->p5 |
| 90197 | 90406 | && (iMeta!=pOp->p3 |
| 90198 | 90407 | || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i) |
| 90199 | 90408 | ){ |
| 90200 | 90409 | /* |
| 90201 | 90410 | ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema |
| | @@ -92997,11 +93206,11 @@ |
| 92997 | 93206 | db->mDbFlags |= DBFLAG_SchemaChange; |
| 92998 | 93207 | p->expired = 0; |
| 92999 | 93208 | }else |
| 93000 | 93209 | #endif |
| 93001 | 93210 | { |
| 93002 | | - zSchema = DFLT_SCHEMA_TABLE; |
| 93211 | + zSchema = LEGACY_SCHEMA_TABLE; |
| 93003 | 93212 | initData.db = db; |
| 93004 | 93213 | initData.iDb = iDb; |
| 93005 | 93214 | initData.pzErrMsg = &p->zErrMsg; |
| 93006 | 93215 | initData.mInitFlags = 0; |
| 93007 | 93216 | initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt); |
| | @@ -94217,10 +94426,11 @@ |
| 94217 | 94426 | pQuery = &aMem[pOp->p3]; |
| 94218 | 94427 | pArgc = &pQuery[1]; |
| 94219 | 94428 | pCur = p->apCsr[pOp->p1]; |
| 94220 | 94429 | assert( memIsValid(pQuery) ); |
| 94221 | 94430 | REGISTER_TRACE(pOp->p3, pQuery); |
| 94431 | + assert( pCur!=0 ); |
| 94222 | 94432 | assert( pCur->eCurType==CURTYPE_VTAB ); |
| 94223 | 94433 | pVCur = pCur->uc.pVCur; |
| 94224 | 94434 | pVtab = pVCur->pVtab; |
| 94225 | 94435 | pModule = pVtab->pModule; |
| 94226 | 94436 | |
| | @@ -94265,10 +94475,11 @@ |
| 94265 | 94475 | const sqlite3_module *pModule; |
| 94266 | 94476 | Mem *pDest; |
| 94267 | 94477 | sqlite3_context sContext; |
| 94268 | 94478 | |
| 94269 | 94479 | VdbeCursor *pCur = p->apCsr[pOp->p1]; |
| 94480 | + assert( pCur!=0 ); |
| 94270 | 94481 | assert( pCur->eCurType==CURTYPE_VTAB ); |
| 94271 | 94482 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 94272 | 94483 | pDest = &aMem[pOp->p3]; |
| 94273 | 94484 | memAboutToChange(p, pDest); |
| 94274 | 94485 | if( pCur->nullRow ){ |
| | @@ -94318,10 +94529,11 @@ |
| 94318 | 94529 | const sqlite3_module *pModule; |
| 94319 | 94530 | int res; |
| 94320 | 94531 | VdbeCursor *pCur; |
| 94321 | 94532 | |
| 94322 | 94533 | pCur = p->apCsr[pOp->p1]; |
| 94534 | + assert( pCur!=0 ); |
| 94323 | 94535 | assert( pCur->eCurType==CURTYPE_VTAB ); |
| 94324 | 94536 | if( pCur->nullRow ){ |
| 94325 | 94537 | break; |
| 94326 | 94538 | } |
| 94327 | 94539 | pVtab = pCur->uc.pVCur->pVtab; |
| | @@ -94413,11 +94625,11 @@ |
| 94413 | 94625 | case OP_VUpdate: { |
| 94414 | 94626 | sqlite3_vtab *pVtab; |
| 94415 | 94627 | const sqlite3_module *pModule; |
| 94416 | 94628 | int nArg; |
| 94417 | 94629 | int i; |
| 94418 | | - sqlite_int64 rowid; |
| 94630 | + sqlite_int64 rowid = 0; |
| 94419 | 94631 | Mem **apArg; |
| 94420 | 94632 | Mem *pX; |
| 94421 | 94633 | |
| 94422 | 94634 | assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback |
| 94423 | 94635 | || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace |
| | @@ -94860,11 +95072,18 @@ |
| 94860 | 95072 | rc = SQLITE_CORRUPT_BKPT; |
| 94861 | 95073 | } |
| 94862 | 95074 | assert( rc ); |
| 94863 | 95075 | #ifdef SQLITE_DEBUG |
| 94864 | 95076 | if( db->flags & SQLITE_VdbeTrace ){ |
| 94865 | | - printf("ABORT-due-to-error. rc=%d\n", rc); |
| 95077 | + const char *zTrace = p->zSql; |
| 95078 | + if( zTrace==0 ){ |
| 95079 | + if( aOp[0].opcode==OP_Trace ){ |
| 95080 | + zTrace = aOp[0].p4.z; |
| 95081 | + } |
| 95082 | + if( zTrace==0 ) zTrace = "???"; |
| 95083 | + } |
| 95084 | + printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace); |
| 94866 | 95085 | } |
| 94867 | 95086 | #endif |
| 94868 | 95087 | if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){ |
| 94869 | 95088 | sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); |
| 94870 | 95089 | } |
| | @@ -94873,10 +95092,13 @@ |
| 94873 | 95092 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 94874 | 95093 | sqlite3_log(rc, "statement aborts at %d: [%s] %s", |
| 94875 | 95094 | (int)(pOp - aOp), p->zSql, p->zErrMsg); |
| 94876 | 95095 | sqlite3VdbeHalt(p); |
| 94877 | 95096 | if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db); |
| 95097 | + if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){ |
| 95098 | + db->flags |= SQLITE_CorruptRdOnly; |
| 95099 | + } |
| 94878 | 95100 | rc = SQLITE_ERROR; |
| 94879 | 95101 | if( resetSchemaOnFault>0 ){ |
| 94880 | 95102 | sqlite3ResetOneSchema(db, resetSchemaOnFault-1); |
| 94881 | 95103 | } |
| 94882 | 95104 | |
| | @@ -95004,11 +95226,14 @@ |
| 95004 | 95226 | }else{ |
| 95005 | 95227 | rc = sqlite3_step(p->pStmt); |
| 95006 | 95228 | } |
| 95007 | 95229 | if( rc==SQLITE_ROW ){ |
| 95008 | 95230 | VdbeCursor *pC = v->apCsr[0]; |
| 95009 | | - u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0; |
| 95231 | + u32 type; |
| 95232 | + assert( pC!=0 ); |
| 95233 | + assert( pC->eCurType==CURTYPE_BTREE ); |
| 95234 | + type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0; |
| 95010 | 95235 | testcase( pC->nHdrParsed==p->iCol ); |
| 95011 | 95236 | testcase( pC->nHdrParsed==p->iCol+1 ); |
| 95012 | 95237 | if( type<12 ){ |
| 95013 | 95238 | zErr = sqlite3MPrintf(p->db, "cannot open value of type %s", |
| 95014 | 95239 | type==0?"null": type==7?"real": "integer" |
| | @@ -95349,10 +95574,12 @@ |
| 95349 | 95574 | ** using the incremental-blob API, this works. For the sessions module |
| 95350 | 95575 | ** anyhow. |
| 95351 | 95576 | */ |
| 95352 | 95577 | sqlite3_int64 iKey; |
| 95353 | 95578 | iKey = sqlite3BtreeIntegerKey(p->pCsr); |
| 95579 | + assert( v->apCsr[0]!=0 ); |
| 95580 | + assert( v->apCsr[0]->eCurType==CURTYPE_BTREE ); |
| 95354 | 95581 | sqlite3VdbePreUpdateHook( |
| 95355 | 95582 | v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol |
| 95356 | 95583 | ); |
| 95357 | 95584 | } |
| 95358 | 95585 | #endif |
| | @@ -96731,11 +96958,11 @@ |
| 96731 | 96958 | void *p = 0; |
| 96732 | 96959 | int chunksize = 4*1024; |
| 96733 | 96960 | sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE, &chunksize); |
| 96734 | 96961 | sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT, &nByte); |
| 96735 | 96962 | sqlite3OsFetch(pFd, 0, (int)nByte, &p); |
| 96736 | | - sqlite3OsUnfetch(pFd, 0, p); |
| 96963 | + if( p ) sqlite3OsUnfetch(pFd, 0, p); |
| 96737 | 96964 | } |
| 96738 | 96965 | } |
| 96739 | 96966 | #else |
| 96740 | 96967 | # define vdbeSorterExtendFile(x,y,z) |
| 96741 | 96968 | #endif |
| | @@ -97449,10 +97676,11 @@ |
| 97449 | 97676 | pTask->file2.iEof += pIncr->mxSz; |
| 97450 | 97677 | }else{ |
| 97451 | 97678 | vdbeMergeEngineFree(pMerger); |
| 97452 | 97679 | rc = SQLITE_NOMEM_BKPT; |
| 97453 | 97680 | } |
| 97681 | + assert( *ppOut!=0 || rc!=SQLITE_OK ); |
| 97454 | 97682 | return rc; |
| 97455 | 97683 | } |
| 97456 | 97684 | |
| 97457 | 97685 | #if SQLITE_MAX_WORKER_THREADS>0 |
| 97458 | 97686 | /* |
| | @@ -100427,19 +100655,22 @@ |
| 100427 | 100655 | sqlite3WindowLink(pSel, pWin); |
| 100428 | 100656 | pNC->ncFlags |= NC_HasWin; |
| 100429 | 100657 | }else |
| 100430 | 100658 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 100431 | 100659 | { |
| 100432 | | - NameContext *pNC2 = pNC; |
| 100660 | + NameContext *pNC2; /* For looping up thru outer contexts */ |
| 100433 | 100661 | pExpr->op = TK_AGG_FUNCTION; |
| 100434 | 100662 | pExpr->op2 = 0; |
| 100435 | 100663 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 100436 | 100664 | if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 100437 | 100665 | sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter); |
| 100438 | 100666 | } |
| 100439 | 100667 | #endif |
| 100440 | | - while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){ |
| 100668 | + pNC2 = pNC; |
| 100669 | + while( pNC2 |
| 100670 | + && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0 |
| 100671 | + ){ |
| 100441 | 100672 | pExpr->op2++; |
| 100442 | 100673 | pNC2 = pNC2->pNext; |
| 100443 | 100674 | } |
| 100444 | 100675 | assert( pDef!=0 || IN_RENAME_OBJECT ); |
| 100445 | 100676 | if( pNC2 && pDef ){ |
| | @@ -101405,12 +101636,12 @@ |
| 101405 | 101636 | |
| 101406 | 101637 | /* |
| 101407 | 101638 | ** Return the affinity character for a single column of a table. |
| 101408 | 101639 | */ |
| 101409 | 101640 | SQLITE_PRIVATE char sqlite3TableColumnAffinity(const Table *pTab, int iCol){ |
| 101410 | | - assert( iCol<pTab->nCol ); |
| 101411 | | - return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER; |
| 101641 | + if( iCol<0 || NEVER(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER; |
| 101642 | + return pTab->aCol[iCol].affinity; |
| 101412 | 101643 | } |
| 101413 | 101644 | |
| 101414 | 101645 | /* |
| 101415 | 101646 | ** Return the 'affinity' of the expression pExpr if any. |
| 101416 | 101647 | ** |
| | @@ -103131,11 +103362,11 @@ |
| 103131 | 103362 | } |
| 103132 | 103363 | |
| 103133 | 103364 | return pRet; |
| 103134 | 103365 | } |
| 103135 | 103366 | #else |
| 103136 | | -SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ |
| 103367 | +SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, const Select *p, int flags){ |
| 103137 | 103368 | assert( p==0 ); |
| 103138 | 103369 | return 0; |
| 103139 | 103370 | } |
| 103140 | 103371 | #endif |
| 103141 | 103372 | |
| | @@ -104186,11 +104417,12 @@ |
| 104186 | 104417 | Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i); |
| 104187 | 104418 | Expr *pRhs = pEList->a[i].pExpr; |
| 104188 | 104419 | CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); |
| 104189 | 104420 | int j; |
| 104190 | 104421 | |
| 104191 | | - assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr ); |
| 104422 | + assert( pReq!=0 || pRhs->iColumn==XN_ROWID |
| 104423 | + || pParse->nErr || db->mallocFailed ); |
| 104192 | 104424 | for(j=0; j<nExpr; j++){ |
| 104193 | 104425 | if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue; |
| 104194 | 104426 | assert( pIdx->azColl[j] ); |
| 104195 | 104427 | if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){ |
| 104196 | 104428 | continue; |
| | @@ -107262,93 +107494,128 @@ |
| 107262 | 107494 | sqlite3WalkExpr(&w, pExpr); |
| 107263 | 107495 | return !w.eCode; |
| 107264 | 107496 | } |
| 107265 | 107497 | |
| 107266 | 107498 | |
| 107267 | | -/* |
| 107268 | | -** An instance of the following structure is used by the tree walker |
| 107269 | | -** to count references to table columns in the arguments of an |
| 107270 | | -** aggregate function, in order to implement the |
| 107271 | | -** sqlite3FunctionThisSrc() routine. |
| 107499 | +/* Structure used to pass information throught the Walker in order to |
| 107500 | +** implement sqlite3ReferencesSrcList(). |
| 107272 | 107501 | */ |
| 107273 | | -struct SrcCount { |
| 107274 | | - SrcList *pSrc; /* One particular FROM clause in a nested query */ |
| 107275 | | - int iSrcInner; /* Smallest cursor number in this context */ |
| 107276 | | - int nThis; /* Number of references to columns in pSrcList */ |
| 107277 | | - int nOther; /* Number of references to columns in other FROM clauses */ |
| 107502 | +struct RefSrcList { |
| 107503 | + sqlite3 *db; /* Database connection used for sqlite3DbRealloc() */ |
| 107504 | + SrcList *pRef; /* Looking for references to these tables */ |
| 107505 | + int nExclude; /* Number of tables to exclude from the search */ |
| 107506 | + int *aiExclude; /* Cursor IDs for tables to exclude from the search */ |
| 107278 | 107507 | }; |
| 107279 | 107508 | |
| 107280 | 107509 | /* |
| 107281 | | -** xSelect callback for sqlite3FunctionUsesThisSrc(). If this is the first |
| 107282 | | -** SELECT with a FROM clause encountered during this iteration, set |
| 107283 | | -** SrcCount.iSrcInner to the cursor number of the leftmost object in |
| 107284 | | -** the FROM cause. |
| 107510 | +** Walker SELECT callbacks for sqlite3ReferencesSrcList(). |
| 107511 | +** |
| 107512 | +** When entering a new subquery on the pExpr argument, add all FROM clause |
| 107513 | +** entries for that subquery to the exclude list. |
| 107514 | +** |
| 107515 | +** When leaving the subquery, remove those entries from the exclude list. |
| 107285 | 107516 | */ |
| 107286 | | -static int selectSrcCount(Walker *pWalker, Select *pSel){ |
| 107287 | | - struct SrcCount *p = pWalker->u.pSrcCount; |
| 107288 | | - if( p->iSrcInner==0x7FFFFFFF && ALWAYS(pSel->pSrc) && pSel->pSrc->nSrc ){ |
| 107289 | | - pWalker->u.pSrcCount->iSrcInner = pSel->pSrc->a[0].iCursor; |
| 107517 | +static int selectRefEnter(Walker *pWalker, Select *pSelect){ |
| 107518 | + struct RefSrcList *p = pWalker->u.pRefSrcList; |
| 107519 | + SrcList *pSrc = pSelect->pSrc; |
| 107520 | + int i, j, *piNew; |
| 107521 | + if( pSrc->nSrc==0 ) return WRC_Continue; |
| 107522 | + j = p->nExclude; |
| 107523 | + p->nExclude += pSrc->nSrc; |
| 107524 | + piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int)); |
| 107525 | + if( piNew==0 ){ |
| 107526 | + p->nExclude = 0; |
| 107527 | + return WRC_Abort; |
| 107528 | + }else{ |
| 107529 | + p->aiExclude = piNew; |
| 107530 | + } |
| 107531 | + for(i=0; i<pSrc->nSrc; i++, j++){ |
| 107532 | + p->aiExclude[j] = pSrc->a[i].iCursor; |
| 107290 | 107533 | } |
| 107291 | 107534 | return WRC_Continue; |
| 107292 | 107535 | } |
| 107293 | | - |
| 107294 | | -/* |
| 107295 | | -** Count the number of references to columns. |
| 107296 | | -*/ |
| 107297 | | -static int exprSrcCount(Walker *pWalker, Expr *pExpr){ |
| 107298 | | - /* There was once a NEVER() on the second term on the grounds that |
| 107299 | | - ** sqlite3FunctionUsesThisSrc() was always called before |
| 107300 | | - ** sqlite3ExprAnalyzeAggregates() and so the TK_COLUMNs have not yet |
| 107301 | | - ** been converted into TK_AGG_COLUMN. But this is no longer true due |
| 107302 | | - ** to window functions - sqlite3WindowRewrite() may now indirectly call |
| 107303 | | - ** FunctionUsesThisSrc() when creating a new sub-select. */ |
| 107304 | | - if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){ |
| 107305 | | - int i; |
| 107306 | | - struct SrcCount *p = pWalker->u.pSrcCount; |
| 107307 | | - SrcList *pSrc = p->pSrc; |
| 107536 | +static void selectRefLeave(Walker *pWalker, Select *pSelect){ |
| 107537 | + struct RefSrcList *p = pWalker->u.pRefSrcList; |
| 107538 | + SrcList *pSrc = pSelect->pSrc; |
| 107539 | + if( p->nExclude ){ |
| 107540 | + assert( p->nExclude>=pSrc->nSrc ); |
| 107541 | + p->nExclude -= pSrc->nSrc; |
| 107542 | + } |
| 107543 | +} |
| 107544 | + |
| 107545 | +/* This is the Walker EXPR callback for sqlite3ReferencesSrcList(). |
| 107546 | +** |
| 107547 | +** Set the 0x01 bit of pWalker->eCode if there is a reference to any |
| 107548 | +** of the tables shown in RefSrcList.pRef. |
| 107549 | +** |
| 107550 | +** Set the 0x02 bit of pWalker->eCode if there is a reference to a |
| 107551 | +** table is in neither RefSrcList.pRef nor RefSrcList.aiExclude. |
| 107552 | +*/ |
| 107553 | +static int exprRefToSrcList(Walker *pWalker, Expr *pExpr){ |
| 107554 | + if( pExpr->op==TK_COLUMN |
| 107555 | + || pExpr->op==TK_AGG_COLUMN |
| 107556 | + ){ |
| 107557 | + int i; |
| 107558 | + struct RefSrcList *p = pWalker->u.pRefSrcList; |
| 107559 | + SrcList *pSrc = p->pRef; |
| 107308 | 107560 | int nSrc = pSrc ? pSrc->nSrc : 0; |
| 107309 | 107561 | for(i=0; i<nSrc; i++){ |
| 107310 | | - if( pExpr->iTable==pSrc->a[i].iCursor ) break; |
| 107311 | | - } |
| 107312 | | - if( i<nSrc ){ |
| 107313 | | - p->nThis++; |
| 107314 | | - }else if( pExpr->iTable<p->iSrcInner ){ |
| 107315 | | - /* In a well-formed parse tree (no name resolution errors), |
| 107316 | | - ** TK_COLUMN nodes with smaller Expr.iTable values are in an |
| 107317 | | - ** outer context. Those are the only ones to count as "other" */ |
| 107318 | | - p->nOther++; |
| 107562 | + if( pExpr->iTable==pSrc->a[i].iCursor ){ |
| 107563 | + pWalker->eCode |= 1; |
| 107564 | + return WRC_Continue; |
| 107565 | + } |
| 107566 | + } |
| 107567 | + for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){} |
| 107568 | + if( i>=p->nExclude ){ |
| 107569 | + pWalker->eCode |= 2; |
| 107319 | 107570 | } |
| 107320 | 107571 | } |
| 107321 | 107572 | return WRC_Continue; |
| 107322 | 107573 | } |
| 107323 | 107574 | |
| 107324 | 107575 | /* |
| 107325 | | -** Determine if any of the arguments to the pExpr Function reference |
| 107326 | | -** pSrcList. Return true if they do. Also return true if the function |
| 107327 | | -** has no arguments or has only constant arguments. Return false if pExpr |
| 107328 | | -** references columns but not columns of tables found in pSrcList. |
| 107576 | +** Check to see if pExpr references any tables in pSrcList. |
| 107577 | +** Possible return values: |
| 107578 | +** |
| 107579 | +** 1 pExpr does references a table in pSrcList. |
| 107580 | +** |
| 107581 | +** 0 pExpr references some table that is not defined in either |
| 107582 | +** pSrcList or in subqueries of pExpr itself. |
| 107583 | +** |
| 107584 | +** -1 pExpr only references no tables at all, or it only |
| 107585 | +** references tables defined in subqueries of pExpr itself. |
| 107586 | +** |
| 107587 | +** As currently used, pExpr is always an aggregate function call. That |
| 107588 | +** fact is exploited for efficiency. |
| 107329 | 107589 | */ |
| 107330 | | -SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){ |
| 107590 | +SQLITE_PRIVATE int sqlite3ReferencesSrcList(Parse *pParse, Expr *pExpr, SrcList *pSrcList){ |
| 107331 | 107591 | Walker w; |
| 107332 | | - struct SrcCount cnt; |
| 107333 | | - assert( pExpr->op==TK_AGG_FUNCTION ); |
| 107592 | + struct RefSrcList x; |
| 107334 | 107593 | memset(&w, 0, sizeof(w)); |
| 107335 | | - w.xExprCallback = exprSrcCount; |
| 107336 | | - w.xSelectCallback = selectSrcCount; |
| 107337 | | - w.u.pSrcCount = &cnt; |
| 107338 | | - cnt.pSrc = pSrcList; |
| 107339 | | - cnt.iSrcInner = (pSrcList&&pSrcList->nSrc)?pSrcList->a[0].iCursor:0x7FFFFFFF; |
| 107340 | | - cnt.nThis = 0; |
| 107341 | | - cnt.nOther = 0; |
| 107594 | + memset(&x, 0, sizeof(x)); |
| 107595 | + w.xExprCallback = exprRefToSrcList; |
| 107596 | + w.xSelectCallback = selectRefEnter; |
| 107597 | + w.xSelectCallback2 = selectRefLeave; |
| 107598 | + w.u.pRefSrcList = &x; |
| 107599 | + x.db = pParse->db; |
| 107600 | + x.pRef = pSrcList; |
| 107601 | + assert( pExpr->op==TK_AGG_FUNCTION ); |
| 107342 | 107602 | assert( ExprUseXList(pExpr) ); |
| 107343 | 107603 | sqlite3WalkExprList(&w, pExpr->x.pList); |
| 107344 | 107604 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 107345 | 107605 | if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 107346 | 107606 | sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter); |
| 107347 | 107607 | } |
| 107348 | 107608 | #endif |
| 107349 | | - return cnt.nThis>0 || cnt.nOther==0; |
| 107609 | + sqlite3DbFree(pParse->db, x.aiExclude); |
| 107610 | + if( w.eCode & 0x01 ){ |
| 107611 | + return 1; |
| 107612 | + }else if( w.eCode ){ |
| 107613 | + return 0; |
| 107614 | + }else{ |
| 107615 | + return -1; |
| 107616 | + } |
| 107350 | 107617 | } |
| 107351 | 107618 | |
| 107352 | 107619 | /* |
| 107353 | 107620 | ** This is a Walker expression node callback. |
| 107354 | 107621 | ** |
| | @@ -107758,11 +108025,11 @@ |
| 107758 | 108025 | int bNoDQS /* Do not allow DQS in the schema */ |
| 107759 | 108026 | ){ |
| 107760 | 108027 | pParse->colNamesSet = 1; |
| 107761 | 108028 | sqlite3NestedParse(pParse, |
| 107762 | 108029 | "SELECT 1 " |
| 107763 | | - "FROM \"%w\"." DFLT_SCHEMA_TABLE " " |
| 108030 | + "FROM \"%w\"." LEGACY_SCHEMA_TABLE " " |
| 107764 | 108031 | "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" |
| 107765 | 108032 | " AND sql NOT LIKE 'create virtual%%'" |
| 107766 | 108033 | " AND sqlite_rename_test(%Q, sql, type, name, %d, %Q, %d)=NULL ", |
| 107767 | 108034 | zDb, |
| 107768 | 108035 | zDb, bTemp, zWhen, bNoDQS |
| | @@ -107769,11 +108036,11 @@ |
| 107769 | 108036 | ); |
| 107770 | 108037 | |
| 107771 | 108038 | if( bTemp==0 ){ |
| 107772 | 108039 | sqlite3NestedParse(pParse, |
| 107773 | 108040 | "SELECT 1 " |
| 107774 | | - "FROM temp." DFLT_SCHEMA_TABLE " " |
| 108041 | + "FROM temp." LEGACY_SCHEMA_TABLE " " |
| 107775 | 108042 | "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" |
| 107776 | 108043 | " AND sql NOT LIKE 'create virtual%%'" |
| 107777 | 108044 | " AND sqlite_rename_test(%Q, sql, type, name, 1, %Q, %d)=NULL ", |
| 107778 | 108045 | zDb, zWhen, bNoDQS |
| 107779 | 108046 | ); |
| | @@ -107787,18 +108054,18 @@ |
| 107787 | 108054 | ** not true, similarly update all SQL statements in the sqlite_schema table |
| 107788 | 108055 | ** of the temp db. |
| 107789 | 108056 | */ |
| 107790 | 108057 | static void renameFixQuotes(Parse *pParse, const char *zDb, int bTemp){ |
| 107791 | 108058 | sqlite3NestedParse(pParse, |
| 107792 | | - "UPDATE \"%w\"." DFLT_SCHEMA_TABLE |
| 108059 | + "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE |
| 107793 | 108060 | " SET sql = sqlite_rename_quotefix(%Q, sql)" |
| 107794 | 108061 | "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" |
| 107795 | 108062 | " AND sql NOT LIKE 'create virtual%%'" , zDb, zDb |
| 107796 | 108063 | ); |
| 107797 | 108064 | if( bTemp==0 ){ |
| 107798 | 108065 | sqlite3NestedParse(pParse, |
| 107799 | | - "UPDATE temp." DFLT_SCHEMA_TABLE |
| 108066 | + "UPDATE temp." LEGACY_SCHEMA_TABLE |
| 107800 | 108067 | " SET sql = sqlite_rename_quotefix('temp', sql)" |
| 107801 | 108068 | "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" |
| 107802 | 108069 | " AND sql NOT LIKE 'create virtual%%'" |
| 107803 | 108070 | ); |
| 107804 | 108071 | } |
| | @@ -107912,21 +108179,21 @@ |
| 107912 | 108179 | nTabName = sqlite3Utf8CharLen(zTabName, -1); |
| 107913 | 108180 | |
| 107914 | 108181 | /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in |
| 107915 | 108182 | ** the schema to use the new table name. */ |
| 107916 | 108183 | sqlite3NestedParse(pParse, |
| 107917 | | - "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET " |
| 108184 | + "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET " |
| 107918 | 108185 | "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) " |
| 107919 | 108186 | "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)" |
| 107920 | 108187 | "AND name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" |
| 107921 | 108188 | , zDb, zDb, zTabName, zName, (iDb==1), zTabName |
| 107922 | 108189 | ); |
| 107923 | 108190 | |
| 107924 | 108191 | /* Update the tbl_name and name columns of the sqlite_schema table |
| 107925 | 108192 | ** as required. */ |
| 107926 | 108193 | sqlite3NestedParse(pParse, |
| 107927 | | - "UPDATE %Q." DFLT_SCHEMA_TABLE " SET " |
| 108194 | + "UPDATE %Q." LEGACY_SCHEMA_TABLE " SET " |
| 107928 | 108195 | "tbl_name = %Q, " |
| 107929 | 108196 | "name = CASE " |
| 107930 | 108197 | "WHEN type='table' THEN %Q " |
| 107931 | 108198 | "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' " |
| 107932 | 108199 | " AND type='index' THEN " |
| | @@ -108111,11 +108378,11 @@ |
| 108111 | 108378 | /* substr() operations on characters, but addColOffset is in bytes. So we |
| 108112 | 108379 | ** have to use printf() to translate between these units: */ |
| 108113 | 108380 | assert( IsOrdinaryTable(pTab) ); |
| 108114 | 108381 | assert( IsOrdinaryTable(pNew) ); |
| 108115 | 108382 | sqlite3NestedParse(pParse, |
| 108116 | | - "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET " |
| 108383 | + "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET " |
| 108117 | 108384 | "sql = printf('%%.%ds, ',sql) || %Q" |
| 108118 | 108385 | " || substr(sql,1+length(printf('%%.%ds',sql))) " |
| 108119 | 108386 | "WHERE type = 'table' AND name = %Q", |
| 108120 | 108387 | zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset, |
| 108121 | 108388 | zTab |
| | @@ -108137,11 +108404,11 @@ |
| 108137 | 108404 | VdbeCoverage(v); |
| 108138 | 108405 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3); |
| 108139 | 108406 | sqlite3ReleaseTempReg(pParse, r1); |
| 108140 | 108407 | |
| 108141 | 108408 | /* Reload the table definition */ |
| 108142 | | - renameReloadSchema(pParse, iDb, INITFLAG_AlterRename); |
| 108409 | + renameReloadSchema(pParse, iDb, INITFLAG_AlterAdd); |
| 108143 | 108410 | |
| 108144 | 108411 | /* Verify that constraints are still satisfied */ |
| 108145 | 108412 | if( pNew->pCheck!=0 |
| 108146 | 108413 | || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0) |
| 108147 | 108414 | ){ |
| | @@ -108345,21 +108612,21 @@ |
| 108345 | 108612 | zNew = sqlite3NameFromToken(db, pNew); |
| 108346 | 108613 | if( !zNew ) goto exit_rename_column; |
| 108347 | 108614 | assert( pNew->n>0 ); |
| 108348 | 108615 | bQuote = sqlite3Isquote(pNew->z[0]); |
| 108349 | 108616 | sqlite3NestedParse(pParse, |
| 108350 | | - "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET " |
| 108617 | + "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET " |
| 108351 | 108618 | "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) " |
| 108352 | 108619 | "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' " |
| 108353 | 108620 | " AND (type != 'index' OR tbl_name = %Q)", |
| 108354 | 108621 | zDb, |
| 108355 | 108622 | zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1, |
| 108356 | 108623 | pTab->zName |
| 108357 | 108624 | ); |
| 108358 | 108625 | |
| 108359 | 108626 | sqlite3NestedParse(pParse, |
| 108360 | | - "UPDATE temp." DFLT_SCHEMA_TABLE " SET " |
| 108627 | + "UPDATE temp." LEGACY_SCHEMA_TABLE " SET " |
| 108361 | 108628 | "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) " |
| 108362 | 108629 | "WHERE type IN ('trigger', 'view')", |
| 108363 | 108630 | zDb, pTab->zName, iCol, zNew, bQuote |
| 108364 | 108631 | ); |
| 108365 | 108632 | |
| | @@ -109030,10 +109297,13 @@ |
| 109030 | 109297 | }else{ |
| 109031 | 109298 | p->pTab->nTabRef++; |
| 109032 | 109299 | rc = sqlite3ViewGetColumnNames(pParse, p->pTab); |
| 109033 | 109300 | } |
| 109034 | 109301 | } |
| 109302 | + } |
| 109303 | + if( rc==SQLITE_OK && db->mallocFailed ){ |
| 109304 | + rc = SQLITE_NOMEM; |
| 109035 | 109305 | } |
| 109036 | 109306 | sNC.pSrcList = pSrc; |
| 109037 | 109307 | if( rc==SQLITE_OK && pStep->pWhere ){ |
| 109038 | 109308 | rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere); |
| 109039 | 109309 | } |
| | @@ -109833,11 +110103,11 @@ |
| 109833 | 110103 | assert( iDb>=0 ); |
| 109834 | 110104 | zDb = db->aDb[iDb].zDbSName; |
| 109835 | 110105 | renameTestSchema(pParse, zDb, iDb==1, "", 0); |
| 109836 | 110106 | renameFixQuotes(pParse, zDb, iDb==1); |
| 109837 | 110107 | sqlite3NestedParse(pParse, |
| 109838 | | - "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET " |
| 110108 | + "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET " |
| 109839 | 110109 | "sql = sqlite_drop_column(%d, sql, %d) " |
| 109840 | 110110 | "WHERE (type=='table' AND tbl_name=%Q COLLATE nocase)" |
| 109841 | 110111 | , zDb, iDb, iCol, pTab->zName |
| 109842 | 110112 | ); |
| 109843 | 110113 | |
| | @@ -113122,21 +113392,21 @@ |
| 113122 | 113392 | } |
| 113123 | 113393 | } |
| 113124 | 113394 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); |
| 113125 | 113395 | if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ |
| 113126 | 113396 | if( i==1 ){ |
| 113127 | | - if( sqlite3StrICmp(zName+7, &ALT_TEMP_SCHEMA_TABLE[7])==0 |
| 113128 | | - || sqlite3StrICmp(zName+7, &ALT_SCHEMA_TABLE[7])==0 |
| 113129 | | - || sqlite3StrICmp(zName+7, &DFLT_SCHEMA_TABLE[7])==0 |
| 113397 | + if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 |
| 113398 | + || sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 |
| 113399 | + || sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE[7])==0 |
| 113130 | 113400 | ){ |
| 113131 | 113401 | p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, |
| 113132 | | - DFLT_TEMP_SCHEMA_TABLE); |
| 113402 | + LEGACY_TEMP_SCHEMA_TABLE); |
| 113133 | 113403 | } |
| 113134 | 113404 | }else{ |
| 113135 | | - if( sqlite3StrICmp(zName+7, &ALT_SCHEMA_TABLE[7])==0 ){ |
| 113405 | + if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 ){ |
| 113136 | 113406 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, |
| 113137 | | - DFLT_SCHEMA_TABLE); |
| 113407 | + LEGACY_SCHEMA_TABLE); |
| 113138 | 113408 | } |
| 113139 | 113409 | } |
| 113140 | 113410 | } |
| 113141 | 113411 | }else{ |
| 113142 | 113412 | /* Match against TEMP first */ |
| | @@ -113150,15 +113420,15 @@ |
| 113150 | 113420 | assert( sqlite3SchemaMutexHeld(db, i, 0) ); |
| 113151 | 113421 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); |
| 113152 | 113422 | if( p ) break; |
| 113153 | 113423 | } |
| 113154 | 113424 | if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ |
| 113155 | | - if( sqlite3StrICmp(zName+7, &ALT_SCHEMA_TABLE[7])==0 ){ |
| 113156 | | - p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, DFLT_SCHEMA_TABLE); |
| 113157 | | - }else if( sqlite3StrICmp(zName+7, &ALT_TEMP_SCHEMA_TABLE[7])==0 ){ |
| 113425 | + if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 ){ |
| 113426 | + p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE); |
| 113427 | + }else if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 ){ |
| 113158 | 113428 | p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, |
| 113159 | | - DFLT_TEMP_SCHEMA_TABLE); |
| 113429 | + LEGACY_TEMP_SCHEMA_TABLE); |
| 113160 | 113430 | } |
| 113161 | 113431 | } |
| 113162 | 113432 | } |
| 113163 | 113433 | return p; |
| 113164 | 113434 | } |
| | @@ -113249,10 +113519,26 @@ |
| 113249 | 113519 | }else{ |
| 113250 | 113520 | zDb = p->zDatabase; |
| 113251 | 113521 | } |
| 113252 | 113522 | return sqlite3LocateTable(pParse, flags, p->zName, zDb); |
| 113253 | 113523 | } |
| 113524 | + |
| 113525 | +/* |
| 113526 | +** Return the preferred table name for system tables. Translate legacy |
| 113527 | +** names into the new preferred names, as appropriate. |
| 113528 | +*/ |
| 113529 | +SQLITE_PRIVATE const char *sqlite3PreferredTableName(const char *zName){ |
| 113530 | + if( sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ |
| 113531 | + if( sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE[7])==0 ){ |
| 113532 | + return PREFERRED_SCHEMA_TABLE; |
| 113533 | + } |
| 113534 | + if( sqlite3StrICmp(zName+7, &LEGACY_TEMP_SCHEMA_TABLE[7])==0 ){ |
| 113535 | + return PREFERRED_TEMP_SCHEMA_TABLE; |
| 113536 | + } |
| 113537 | + } |
| 113538 | + return zName; |
| 113539 | +} |
| 113254 | 113540 | |
| 113255 | 113541 | /* |
| 113256 | 113542 | ** Locate the in-memory structure that describes |
| 113257 | 113543 | ** a particular index given the name of that index |
| 113258 | 113544 | ** and the name of the database that contains the index. |
| | @@ -113648,11 +113934,11 @@ |
| 113648 | 113934 | ** Open the sqlite_schema table stored in database number iDb for |
| 113649 | 113935 | ** writing. The table is opened using cursor 0. |
| 113650 | 113936 | */ |
| 113651 | 113937 | SQLITE_PRIVATE void sqlite3OpenSchemaTable(Parse *p, int iDb){ |
| 113652 | 113938 | Vdbe *v = sqlite3GetVdbe(p); |
| 113653 | | - sqlite3TableLock(p, iDb, SCHEMA_ROOT, 1, DFLT_SCHEMA_TABLE); |
| 113939 | + sqlite3TableLock(p, iDb, SCHEMA_ROOT, 1, LEGACY_SCHEMA_TABLE); |
| 113654 | 113940 | sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, SCHEMA_ROOT, iDb, 5); |
| 113655 | 113941 | if( p->nTab==0 ){ |
| 113656 | 113942 | p->nTab = 1; |
| 113657 | 113943 | } |
| 113658 | 113944 | } |
| | @@ -115227,10 +115513,45 @@ |
| 115227 | 115513 | if( pMod->pModule->xShadowName==0 ) return 0; |
| 115228 | 115514 | return pMod->pModule->xShadowName(zName+nName+1); |
| 115229 | 115515 | } |
| 115230 | 115516 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 115231 | 115517 | |
| 115518 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 115519 | +/* |
| 115520 | +** Table pTab is a virtual table. If it the virtual table implementation |
| 115521 | +** exists and has an xShadowName method, then loop over all other ordinary |
| 115522 | +** tables within the same schema looking for shadow tables of pTab, and mark |
| 115523 | +** any shadow tables seen using the TF_Shadow flag. |
| 115524 | +*/ |
| 115525 | +SQLITE_PRIVATE void sqlite3MarkAllShadowTablesOf(sqlite3 *db, Table *pTab){ |
| 115526 | + int nName; /* Length of pTab->zName */ |
| 115527 | + Module *pMod; /* Module for the virtual table */ |
| 115528 | + HashElem *k; /* For looping through the symbol table */ |
| 115529 | + |
| 115530 | + assert( IsVirtual(pTab) ); |
| 115531 | + pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]); |
| 115532 | + if( pMod==0 ) return; |
| 115533 | + if( NEVER(pMod->pModule==0) ) return; |
| 115534 | + if( pMod->pModule->iVersion<3 ) return; |
| 115535 | + if( pMod->pModule->xShadowName==0 ) return; |
| 115536 | + assert( pTab->zName!=0 ); |
| 115537 | + nName = sqlite3Strlen30(pTab->zName); |
| 115538 | + for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){ |
| 115539 | + Table *pOther = sqliteHashData(k); |
| 115540 | + assert( pOther->zName!=0 ); |
| 115541 | + if( !IsOrdinaryTable(pOther) ) continue; |
| 115542 | + if( pOther->tabFlags & TF_Shadow ) continue; |
| 115543 | + if( sqlite3StrNICmp(pOther->zName, pTab->zName, nName)==0 |
| 115544 | + && pOther->zName[nName]=='_' |
| 115545 | + && pMod->pModule->xShadowName(pOther->zName+nName+1) |
| 115546 | + ){ |
| 115547 | + pOther->tabFlags |= TF_Shadow; |
| 115548 | + } |
| 115549 | + } |
| 115550 | +} |
| 115551 | +#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 115552 | + |
| 115232 | 115553 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 115233 | 115554 | /* |
| 115234 | 115555 | ** Return true if zName is a shadow table name in the current database |
| 115235 | 115556 | ** connection. |
| 115236 | 115557 | ** |
| | @@ -115555,11 +115876,11 @@ |
| 115555 | 115876 | /* A slot for the record has already been allocated in the |
| 115556 | 115877 | ** schema table. We just need to update that slot with all |
| 115557 | 115878 | ** the information we've collected. |
| 115558 | 115879 | */ |
| 115559 | 115880 | sqlite3NestedParse(pParse, |
| 115560 | | - "UPDATE %Q." DFLT_SCHEMA_TABLE |
| 115881 | + "UPDATE %Q." LEGACY_SCHEMA_TABLE |
| 115561 | 115882 | " SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q" |
| 115562 | 115883 | " WHERE rowid=#%d", |
| 115563 | 115884 | db->aDb[iDb].zDbSName, |
| 115564 | 115885 | zType, |
| 115565 | 115886 | p->zName, |
| | @@ -115741,17 +116062,16 @@ |
| 115741 | 116062 | #endif |
| 115742 | 116063 | |
| 115743 | 116064 | assert( pTable ); |
| 115744 | 116065 | |
| 115745 | 116066 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 115746 | | - db->nSchemaLock++; |
| 115747 | | - rc = sqlite3VtabCallConnect(pParse, pTable); |
| 115748 | | - db->nSchemaLock--; |
| 115749 | | - if( rc ){ |
| 115750 | | - return 1; |
| 116067 | + if( IsVirtual(pTable) ){ |
| 116068 | + db->nSchemaLock++; |
| 116069 | + rc = sqlite3VtabCallConnect(pParse, pTable); |
| 116070 | + db->nSchemaLock--; |
| 116071 | + return rc; |
| 115751 | 116072 | } |
| 115752 | | - if( IsVirtual(pTable) ) return 0; |
| 115753 | 116073 | #endif |
| 115754 | 116074 | |
| 115755 | 116075 | #ifndef SQLITE_OMIT_VIEW |
| 115756 | 116076 | /* A positive nCol means the columns names for this view are |
| 115757 | 116077 | ** already known. |
| | @@ -115935,11 +116255,11 @@ |
| 115935 | 116255 | ** The "#NNN" in the SQL is a special constant that means whatever value |
| 115936 | 116256 | ** is in register NNN. See grammar rules associated with the TK_REGISTER |
| 115937 | 116257 | ** token for additional information. |
| 115938 | 116258 | */ |
| 115939 | 116259 | sqlite3NestedParse(pParse, |
| 115940 | | - "UPDATE %Q." DFLT_SCHEMA_TABLE |
| 116260 | + "UPDATE %Q." LEGACY_SCHEMA_TABLE |
| 115941 | 116261 | " SET rootpage=%d WHERE #%d AND rootpage=#%d", |
| 115942 | 116262 | pParse->db->aDb[iDb].zDbSName, iTable, r1, r1); |
| 115943 | 116263 | #endif |
| 115944 | 116264 | sqlite3ReleaseTempReg(pParse, r1); |
| 115945 | 116265 | } |
| | @@ -116070,11 +116390,11 @@ |
| 116070 | 116390 | ** dropped. Triggers are handled separately because a trigger can be |
| 116071 | 116391 | ** created in the temp database that refers to a table in another |
| 116072 | 116392 | ** database. |
| 116073 | 116393 | */ |
| 116074 | 116394 | sqlite3NestedParse(pParse, |
| 116075 | | - "DELETE FROM %Q." DFLT_SCHEMA_TABLE |
| 116395 | + "DELETE FROM %Q." LEGACY_SCHEMA_TABLE |
| 116076 | 116396 | " WHERE tbl_name=%Q and type!='trigger'", |
| 116077 | 116397 | pDb->zDbSName, pTab->zName); |
| 116078 | 116398 | if( !isView && !IsVirtual(pTab) ){ |
| 116079 | 116399 | destroyTable(pParse, pTab); |
| 116080 | 116400 | } |
| | @@ -116117,10 +116437,13 @@ |
| 116117 | 116437 | if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0; |
| 116118 | 116438 | return 1; |
| 116119 | 116439 | } |
| 116120 | 116440 | if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){ |
| 116121 | 116441 | return 1; |
| 116442 | + } |
| 116443 | + if( pTab->tabFlags & TF_Eponymous ){ |
| 116444 | + return 1; |
| 116122 | 116445 | } |
| 116123 | 116446 | return 0; |
| 116124 | 116447 | } |
| 116125 | 116448 | |
| 116126 | 116449 | /* |
| | @@ -117087,11 +117410,11 @@ |
| 117087 | 117410 | } |
| 117088 | 117411 | |
| 117089 | 117412 | /* Add an entry in sqlite_schema for this index |
| 117090 | 117413 | */ |
| 117091 | 117414 | sqlite3NestedParse(pParse, |
| 117092 | | - "INSERT INTO %Q." DFLT_SCHEMA_TABLE " VALUES('index',%Q,%Q,#%d,%Q);", |
| 117415 | + "INSERT INTO %Q." LEGACY_SCHEMA_TABLE " VALUES('index',%Q,%Q,#%d,%Q);", |
| 117093 | 117416 | db->aDb[iDb].zDbSName, |
| 117094 | 117417 | pIndex->zName, |
| 117095 | 117418 | pTab->zName, |
| 117096 | 117419 | iMem, |
| 117097 | 117420 | zStmt |
| | @@ -117273,11 +117596,11 @@ |
| 117273 | 117596 | /* Generate code to remove the index and from the schema table */ |
| 117274 | 117597 | v = sqlite3GetVdbe(pParse); |
| 117275 | 117598 | if( v ){ |
| 117276 | 117599 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 117277 | 117600 | sqlite3NestedParse(pParse, |
| 117278 | | - "DELETE FROM %Q." DFLT_SCHEMA_TABLE " WHERE name=%Q AND type='index'", |
| 117601 | + "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='index'", |
| 117279 | 117602 | db->aDb[iDb].zDbSName, pIndex->zName |
| 117280 | 117603 | ); |
| 117281 | 117604 | sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName); |
| 117282 | 117605 | sqlite3ChangeCookie(pParse, iDb); |
| 117283 | 117606 | destroyRootPage(pParse, pIndex->tnum, iDb); |
| | @@ -123933,28 +124256,34 @@ |
| 123933 | 124256 | |
| 123934 | 124257 | /* Before computing generated columns, first go through and make sure |
| 123935 | 124258 | ** that appropriate affinity has been applied to the regular columns |
| 123936 | 124259 | */ |
| 123937 | 124260 | sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore); |
| 123938 | | - if( (pTab->tabFlags & TF_HasStored)!=0 |
| 123939 | | - && (pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1))->opcode==OP_Affinity |
| 123940 | | - ){ |
| 123941 | | - /* Change the OP_Affinity argument to '@' (NONE) for all stored |
| 123942 | | - ** columns. '@' is the no-op affinity and those columns have not |
| 123943 | | - ** yet been computed. */ |
| 123944 | | - int ii, jj; |
| 123945 | | - char *zP4 = pOp->p4.z; |
| 123946 | | - assert( zP4!=0 ); |
| 123947 | | - assert( pOp->p4type==P4_DYNAMIC ); |
| 123948 | | - for(ii=jj=0; zP4[jj]; ii++){ |
| 123949 | | - if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){ |
| 123950 | | - continue; |
| 123951 | | - } |
| 123952 | | - if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){ |
| 123953 | | - zP4[jj] = SQLITE_AFF_NONE; |
| 123954 | | - } |
| 123955 | | - jj++; |
| 124261 | + if( (pTab->tabFlags & TF_HasStored)!=0 ){ |
| 124262 | + pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1); |
| 124263 | + if( pOp->opcode==OP_Affinity ){ |
| 124264 | + /* Change the OP_Affinity argument to '@' (NONE) for all stored |
| 124265 | + ** columns. '@' is the no-op affinity and those columns have not |
| 124266 | + ** yet been computed. */ |
| 124267 | + int ii, jj; |
| 124268 | + char *zP4 = pOp->p4.z; |
| 124269 | + assert( zP4!=0 ); |
| 124270 | + assert( pOp->p4type==P4_DYNAMIC ); |
| 124271 | + for(ii=jj=0; zP4[jj]; ii++){ |
| 124272 | + if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){ |
| 124273 | + continue; |
| 124274 | + } |
| 124275 | + if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){ |
| 124276 | + zP4[jj] = SQLITE_AFF_NONE; |
| 124277 | + } |
| 124278 | + jj++; |
| 124279 | + } |
| 124280 | + }else if( pOp->opcode==OP_TypeCheck ){ |
| 124281 | + /* If an OP_TypeCheck was generated because the table is STRICT, |
| 124282 | + ** then set the P3 operand to indicate that generated columns should |
| 124283 | + ** not be checked */ |
| 124284 | + pOp->p3 = 1; |
| 123956 | 124285 | } |
| 123957 | 124286 | } |
| 123958 | 124287 | |
| 123959 | 124288 | /* Because there can be multiple generated columns that refer to one another, |
| 123960 | 124289 | ** this is a two-pass algorithm. On the first pass, mark all generated |
| | @@ -125978,11 +126307,12 @@ |
| 125978 | 126307 | default: { |
| 125979 | 126308 | int nConflictCk; /* Number of opcodes in conflict check logic */ |
| 125980 | 126309 | |
| 125981 | 126310 | assert( onError==OE_Replace ); |
| 125982 | 126311 | nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk; |
| 125983 | | - assert( nConflictCk>0 ); |
| 126312 | + assert( nConflictCk>0 || db->mallocFailed ); |
| 126313 | + testcase( nConflictCk<=0 ); |
| 125984 | 126314 | testcase( nConflictCk>1 ); |
| 125985 | 126315 | if( regTrigCnt ){ |
| 125986 | 126316 | sqlite3MultiWrite(pParse); |
| 125987 | 126317 | nReplaceTrig++; |
| 125988 | 126318 | } |
| | @@ -126264,12 +126594,13 @@ |
| 126264 | 126594 | |
| 126265 | 126595 | assert( op==OP_OpenRead || op==OP_OpenWrite ); |
| 126266 | 126596 | assert( op==OP_OpenWrite || p5==0 ); |
| 126267 | 126597 | if( IsVirtual(pTab) ){ |
| 126268 | 126598 | /* This routine is a no-op for virtual tables. Leave the output |
| 126269 | | - ** variables *piDataCur and *piIdxCur uninitialized so that valgrind |
| 126270 | | - ** can detect if they are used by mistake in the caller. */ |
| 126599 | + ** variables *piDataCur and *piIdxCur set to illegal cursor numbers |
| 126600 | + ** for improved error detection. */ |
| 126601 | + *piDataCur = *piIdxCur = -999; |
| 126271 | 126602 | return 0; |
| 126272 | 126603 | } |
| 126273 | 126604 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 126274 | 126605 | v = pParse->pVdbe; |
| 126275 | 126606 | assert( v!=0 ); |
| | @@ -127276,10 +127607,14 @@ |
| 127276 | 127607 | /* Version 3.34.0 and later */ |
| 127277 | 127608 | int (*txn_state)(sqlite3*,const char*); |
| 127278 | 127609 | /* Version 3.36.1 and later */ |
| 127279 | 127610 | sqlite3_int64 (*changes64)(sqlite3*); |
| 127280 | 127611 | sqlite3_int64 (*total_changes64)(sqlite3*); |
| 127612 | + /* Version 3.37.0 and later */ |
| 127613 | + int (*autovacuum_pages)(sqlite3*, |
| 127614 | + unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int), |
| 127615 | + void*, void(*)(void*)); |
| 127281 | 127616 | }; |
| 127282 | 127617 | |
| 127283 | 127618 | /* |
| 127284 | 127619 | ** This is the function signature used for all extension entry points. It |
| 127285 | 127620 | ** is also defined in the file "loadext.c". |
| | @@ -127582,10 +127917,15 @@ |
| 127582 | 127917 | #define sqlite3_create_filename sqlite3_api->create_filename |
| 127583 | 127918 | #define sqlite3_free_filename sqlite3_api->free_filename |
| 127584 | 127919 | #define sqlite3_database_file_object sqlite3_api->database_file_object |
| 127585 | 127920 | /* Version 3.34.0 and later */ |
| 127586 | 127921 | #define sqlite3_txn_state sqlite3_api->txn_state |
| 127922 | +/* Version 3.36.1 and later */ |
| 127923 | +#define sqlite3_changes64 sqlite3_api->changes64 |
| 127924 | +#define sqlite3_total_changes64 sqlite3_api->total_changes64 |
| 127925 | +/* Version 3.37.0 and later */ |
| 127926 | +#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages |
| 127587 | 127927 | #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ |
| 127588 | 127928 | |
| 127589 | 127929 | #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 127590 | 127930 | /* This case when the file really is being compiled as a loadable |
| 127591 | 127931 | ** extension */ |
| | @@ -128069,10 +128409,12 @@ |
| 128069 | 128409 | /* Version 3.34.0 and later */ |
| 128070 | 128410 | sqlite3_txn_state, |
| 128071 | 128411 | /* Version 3.36.1 and later */ |
| 128072 | 128412 | sqlite3_changes64, |
| 128073 | 128413 | sqlite3_total_changes64, |
| 128414 | + /* Version 3.37.0 and later */ |
| 128415 | + sqlite3_autovacuum_pages, |
| 128074 | 128416 | }; |
| 128075 | 128417 | |
| 128076 | 128418 | /* True if x is the directory separator character |
| 128077 | 128419 | */ |
| 128078 | 128420 | #if SQLITE_OS_WIN |
| | @@ -130391,11 +130733,11 @@ |
| 130391 | 130733 | }else{ |
| 130392 | 130734 | zType = "table"; |
| 130393 | 130735 | } |
| 130394 | 130736 | sqlite3VdbeMultiLoad(v, 1, "sssiii", |
| 130395 | 130737 | db->aDb[ii].zDbSName, |
| 130396 | | - pTab->zName, |
| 130738 | + sqlite3PreferredTableName(pTab->zName), |
| 130397 | 130739 | zType, |
| 130398 | 130740 | pTab->nCol, |
| 130399 | 130741 | (pTab->tabFlags & TF_WithoutRowid)!=0, |
| 130400 | 130742 | (pTab->tabFlags & TF_Strict)!=0 |
| 130401 | 130743 | ); |
| | @@ -130411,11 +130753,11 @@ |
| 130411 | 130753 | pParse->nMem = 5; |
| 130412 | 130754 | sqlite3CodeVerifySchema(pParse, iDb); |
| 130413 | 130755 | for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){ |
| 130414 | 130756 | Table *pTab = sqliteHashData(i); |
| 130415 | 130757 | sqlite3VdbeMultiLoad(v, 1, "ssiii", |
| 130416 | | - pTab->zName, |
| 130758 | + sqlite3PreferredTableName(pTab->zName), |
| 130417 | 130759 | 0, |
| 130418 | 130760 | pTab->szTabRow, |
| 130419 | 130761 | pTab->nRowLogEst, |
| 130420 | 130762 | pTab->tabFlags); |
| 130421 | 130763 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| | @@ -130906,11 +131248,11 @@ |
| 130906 | 131248 | if( pCol->notNull ){ |
| 130907 | 131249 | jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v); |
| 130908 | 131250 | zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName, |
| 130909 | 131251 | pCol->zCnName); |
| 130910 | 131252 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); |
| 130911 | | - if( bStrict ){ |
| 131253 | + if( bStrict && pCol->eCType!=COLTYPE_ANY ){ |
| 130912 | 131254 | sqlite3VdbeGoto(v, doError); |
| 130913 | 131255 | }else{ |
| 130914 | 131256 | integrityCheckResultRow(v); |
| 130915 | 131257 | } |
| 130916 | 131258 | sqlite3VdbeJumpHere(v, jmp2); |
| | @@ -131867,14 +132209,19 @@ |
| 131867 | 132209 | sqlite3 *db = pData->db; |
| 131868 | 132210 | if( db->mallocFailed ){ |
| 131869 | 132211 | pData->rc = SQLITE_NOMEM_BKPT; |
| 131870 | 132212 | }else if( pData->pzErrMsg[0]!=0 ){ |
| 131871 | 132213 | /* A error message has already been generated. Do not overwrite it */ |
| 131872 | | - }else if( pData->mInitFlags & (INITFLAG_AlterRename|INITFLAG_AlterDrop) ){ |
| 132214 | + }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){ |
| 132215 | + static const char *azAlterType[] = { |
| 132216 | + "rename", |
| 132217 | + "drop column", |
| 132218 | + "add column" |
| 132219 | + }; |
| 131873 | 132220 | *pData->pzErrMsg = sqlite3MPrintf(db, |
| 131874 | 132221 | "error in %s %s after %s: %s", azObj[0], azObj[1], |
| 131875 | | - (pData->mInitFlags & INITFLAG_AlterRename) ? "rename" : "drop column", |
| 132222 | + azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1], |
| 131876 | 132223 | zExtra |
| 131877 | 132224 | ); |
| 131878 | 132225 | pData->rc = SQLITE_ERROR; |
| 131879 | 132226 | }else if( db->flags & SQLITE_WriteSchema ){ |
| 131880 | 132227 | pData->rc = SQLITE_CORRUPT_BKPT; |
| | @@ -135070,10 +135417,13 @@ |
| 135070 | 135417 | n = sqlite3Strlen30(pCol->zCnName); |
| 135071 | 135418 | pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+m+2); |
| 135072 | 135419 | if( pCol->zCnName ){ |
| 135073 | 135420 | memcpy(&pCol->zCnName[n+1], zType, m+1); |
| 135074 | 135421 | pCol->colFlags |= COLFLAG_HASTYPE; |
| 135422 | + }else{ |
| 135423 | + testcase( pCol->colFlags & COLFLAG_HASTYPE ); |
| 135424 | + pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL); |
| 135075 | 135425 | } |
| 135076 | 135426 | } |
| 135077 | 135427 | if( pCol->affinity<=SQLITE_AFF_NONE ) pCol->affinity = aff; |
| 135078 | 135428 | pColl = sqlite3ExprCollSeq(pParse, p); |
| 135079 | 135429 | if( pColl ){ |
| | @@ -137809,31 +138159,44 @@ |
| 137809 | 138159 | ** |
| 137810 | 138160 | ** SELECT count(*) FROM <tbl> |
| 137811 | 138161 | ** |
| 137812 | 138162 | ** where table is a database table, not a sub-select or view. If the query |
| 137813 | 138163 | ** does match this pattern, then a pointer to the Table object representing |
| 137814 | | -** <tbl> is returned. Otherwise, 0 is returned. |
| 138164 | +** <tbl> is returned. Otherwise, NULL is returned. |
| 138165 | +** |
| 138166 | +** This routine a condition for the count optimization. A correct answer |
| 138167 | +** is obtained (though perhaps more slowly) if this routine returns NULL when |
| 138168 | +** it could have returned a table pointer. But returning the pointer when |
| 138169 | +** NULL should have been returned can result in incorrect answers and/or |
| 138170 | +** crashes. So, when in doubt, return NULL. |
| 137815 | 138171 | */ |
| 137816 | 138172 | static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){ |
| 137817 | 138173 | Table *pTab; |
| 137818 | 138174 | Expr *pExpr; |
| 137819 | 138175 | |
| 137820 | 138176 | assert( !p->pGroupBy ); |
| 137821 | 138177 | |
| 137822 | | - if( p->pWhere || p->pEList->nExpr!=1 |
| 137823 | | - || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect |
| 138178 | + if( p->pWhere |
| 138179 | + || p->pEList->nExpr!=1 |
| 138180 | + || p->pSrc->nSrc!=1 |
| 138181 | + || p->pSrc->a[0].pSelect |
| 138182 | + || pAggInfo->nFunc!=1 |
| 137824 | 138183 | ){ |
| 137825 | 138184 | return 0; |
| 137826 | 138185 | } |
| 137827 | 138186 | pTab = p->pSrc->a[0].pTab; |
| 138187 | + assert( pTab!=0 ); |
| 138188 | + assert( !IsView(pTab) ); |
| 138189 | + if( !IsOrdinaryTable(pTab) ) return 0; |
| 137828 | 138190 | pExpr = p->pEList->a[0].pExpr; |
| 137829 | | - assert( pTab && !IsView(pTab) && pExpr ); |
| 137830 | | - |
| 137831 | | - if( IsVirtual(pTab) ) return 0; |
| 138191 | + assert( pExpr!=0 ); |
| 137832 | 138192 | if( pExpr->op!=TK_AGG_FUNCTION ) return 0; |
| 137833 | | - if( NEVER(pAggInfo->nFunc==0) ) return 0; |
| 138193 | + if( pExpr->pAggInfo!=pAggInfo ) return 0; |
| 137834 | 138194 | if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0; |
| 138195 | + assert( pAggInfo->aFunc[0].pFExpr==pExpr ); |
| 138196 | + testcase( ExprHasProperty(pExpr, EP_Distinct) ); |
| 138197 | + testcase( ExprHasProperty(pExpr, EP_WinFunc) ); |
| 137835 | 138198 | if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc) ) return 0; |
| 137836 | 138199 | |
| 137837 | 138200 | return pTab; |
| 137838 | 138201 | } |
| 137839 | 138202 | |
| | @@ -140934,11 +141297,11 @@ |
| 140934 | 141297 | if( v==0 ) goto triggerfinish_cleanup; |
| 140935 | 141298 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 140936 | 141299 | z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n); |
| 140937 | 141300 | testcase( z==0 ); |
| 140938 | 141301 | sqlite3NestedParse(pParse, |
| 140939 | | - "INSERT INTO %Q." DFLT_SCHEMA_TABLE |
| 141302 | + "INSERT INTO %Q." LEGACY_SCHEMA_TABLE |
| 140940 | 141303 | " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')", |
| 140941 | 141304 | db->aDb[iDb].zDbSName, zName, |
| 140942 | 141305 | pTrig->table, z); |
| 140943 | 141306 | sqlite3DbFree(db, z); |
| 140944 | 141307 | sqlite3ChangeCookie(pParse, iDb); |
| | @@ -141248,11 +141611,11 @@ |
| 141248 | 141611 | |
| 141249 | 141612 | /* Generate code to destroy the database record of the trigger. |
| 141250 | 141613 | */ |
| 141251 | 141614 | if( (v = sqlite3GetVdbe(pParse))!=0 ){ |
| 141252 | 141615 | sqlite3NestedParse(pParse, |
| 141253 | | - "DELETE FROM %Q." DFLT_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'", |
| 141616 | + "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'", |
| 141254 | 141617 | db->aDb[iDb].zDbSName, pTrigger->zName |
| 141255 | 141618 | ); |
| 141256 | 141619 | sqlite3ChangeCookie(pParse, iDb); |
| 141257 | 141620 | sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0); |
| 141258 | 141621 | } |
| | @@ -143901,11 +144264,13 @@ |
| 143901 | 144264 | rc = sqlite3BtreeBeginTrans(pMain, pOut==0 ? 2 : 0, 0); |
| 143902 | 144265 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
| 143903 | 144266 | |
| 143904 | 144267 | /* Do not attempt to change the page size for a WAL database */ |
| 143905 | 144268 | if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain)) |
| 143906 | | - ==PAGER_JOURNALMODE_WAL ){ |
| 144269 | + ==PAGER_JOURNALMODE_WAL |
| 144270 | + && pOut==0 |
| 144271 | + ){ |
| 143907 | 144272 | db->nextPagesize = 0; |
| 143908 | 144273 | } |
| 143909 | 144274 | |
| 143910 | 144275 | if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0) |
| 143911 | 144276 | || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0)) |
| | @@ -144543,11 +144908,11 @@ |
| 144543 | 144908 | ** entry in the sqlite_schema table tht was created for this vtab |
| 144544 | 144909 | ** by sqlite3StartTable(). |
| 144545 | 144910 | */ |
| 144546 | 144911 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 144547 | 144912 | sqlite3NestedParse(pParse, |
| 144548 | | - "UPDATE %Q." DFLT_SCHEMA_TABLE " " |
| 144913 | + "UPDATE %Q." LEGACY_SCHEMA_TABLE " " |
| 144549 | 144914 | "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q " |
| 144550 | 144915 | "WHERE rowid=#%d", |
| 144551 | 144916 | db->aDb[iDb].zDbSName, |
| 144552 | 144917 | pTab->zName, |
| 144553 | 144918 | pTab->zName, |
| | @@ -144563,22 +144928,18 @@ |
| 144563 | 144928 | sqlite3DbFree(db, zStmt); |
| 144564 | 144929 | |
| 144565 | 144930 | iReg = ++pParse->nMem; |
| 144566 | 144931 | sqlite3VdbeLoadString(v, iReg, pTab->zName); |
| 144567 | 144932 | sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg); |
| 144568 | | - } |
| 144569 | | - |
| 144570 | | - /* If we are rereading the sqlite_schema table create the in-memory |
| 144571 | | - ** record of the table. The xConnect() method is not called until |
| 144572 | | - ** the first time the virtual table is used in an SQL statement. This |
| 144573 | | - ** allows a schema that contains virtual tables to be loaded before |
| 144574 | | - ** the required virtual table implementations are registered. */ |
| 144575 | | - else { |
| 144933 | + }else{ |
| 144934 | + /* If we are rereading the sqlite_schema table create the in-memory |
| 144935 | + ** record of the table. */ |
| 144576 | 144936 | Table *pOld; |
| 144577 | 144937 | Schema *pSchema = pTab->pSchema; |
| 144578 | 144938 | const char *zName = pTab->zName; |
| 144579 | | - assert( sqlite3SchemaMutexHeld(db, 0, pSchema) ); |
| 144939 | + assert( zName!=0 ); |
| 144940 | + sqlite3MarkAllShadowTablesOf(db, pTab); |
| 144580 | 144941 | pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab); |
| 144581 | 144942 | if( pOld ){ |
| 144582 | 144943 | sqlite3OomFault(db); |
| 144583 | 144944 | assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */ |
| 144584 | 144945 | return; |
| | @@ -144758,11 +145119,12 @@ |
| 144758 | 145119 | const char *zMod; |
| 144759 | 145120 | Module *pMod; |
| 144760 | 145121 | int rc; |
| 144761 | 145122 | |
| 144762 | 145123 | assert( pTab ); |
| 144763 | | - if( !IsVirtual(pTab) || sqlite3GetVTable(db, pTab) ){ |
| 145124 | + assert( IsVirtual(pTab) ); |
| 145125 | + if( sqlite3GetVTable(db, pTab) ){ |
| 144764 | 145126 | return SQLITE_OK; |
| 144765 | 145127 | } |
| 144766 | 145128 | |
| 144767 | 145129 | /* Locate the required virtual table module */ |
| 144768 | 145130 | zMod = pTab->u.vtab.azArg[0]; |
| | @@ -144962,11 +145324,14 @@ |
| 144962 | 145324 | SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){ |
| 144963 | 145325 | int rc = SQLITE_OK; |
| 144964 | 145326 | Table *pTab; |
| 144965 | 145327 | |
| 144966 | 145328 | pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName); |
| 144967 | | - if( pTab!=0 && ALWAYS(IsVirtual(pTab)) && ALWAYS(pTab->u.vtab.p!=0) ){ |
| 145329 | + if( ALWAYS(pTab!=0) |
| 145330 | + && ALWAYS(IsVirtual(pTab)) |
| 145331 | + && ALWAYS(pTab->u.vtab.p!=0) |
| 145332 | + ){ |
| 144968 | 145333 | VTable *p; |
| 144969 | 145334 | int (*xDestroy)(sqlite3_vtab *); |
| 144970 | 145335 | for(p=pTab->u.vtab.p; p; p=p->pNext){ |
| 144971 | 145336 | assert( p->pVtab ); |
| 144972 | 145337 | if( p->pVtab->nRef>0 ){ |
| | @@ -147900,12 +148265,23 @@ |
| 147900 | 148265 | |
| 147901 | 148266 | /* Load the value for the inequality constraint at the end of the |
| 147902 | 148267 | ** range (if any). |
| 147903 | 148268 | */ |
| 147904 | 148269 | nConstraint = nEq; |
| 148270 | + assert( pLevel->p2==0 ); |
| 147905 | 148271 | if( pRangeEnd ){ |
| 147906 | 148272 | Expr *pRight = pRangeEnd->pExpr->pRight; |
| 148273 | + if( addrSeekScan ){ |
| 148274 | + /* For a seek-scan that has a range on the lowest term of the index, |
| 148275 | + ** we have to make the top of the loop be code that sets the end |
| 148276 | + ** condition of the range. Otherwise, the OP_SeekScan might jump |
| 148277 | + ** over that initialization, leaving the range-end value set to the |
| 148278 | + ** range-start value, resulting in a wrong answer. |
| 148279 | + ** See ticket 5981a8c041a3c2f3 (2021-11-02). |
| 148280 | + */ |
| 148281 | + pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 148282 | + } |
| 147907 | 148283 | codeExprOrVector(pParse, pRight, regBase+nEq, nTop); |
| 147908 | 148284 | whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd); |
| 147909 | 148285 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 |
| 147910 | 148286 | && sqlite3ExprCanBeNull(pRight) |
| 147911 | 148287 | ){ |
| | @@ -147935,11 +148311,11 @@ |
| 147935 | 148311 | } |
| 147936 | 148312 | sqlite3DbFree(db, zStartAff); |
| 147937 | 148313 | sqlite3DbFree(db, zEndAff); |
| 147938 | 148314 | |
| 147939 | 148315 | /* Top of the loop body */ |
| 147940 | | - pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 148316 | + if( pLevel->p2==0 ) pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 147941 | 148317 | |
| 147942 | 148318 | /* Check if the index cursor is past the end of the range. */ |
| 147943 | 148319 | if( nConstraint ){ |
| 147944 | 148320 | if( regBignull ){ |
| 147945 | 148321 | /* Except, skip the end-of-range check while doing the NULL-scan */ |
| | @@ -155575,10 +155951,11 @@ |
| 155575 | 155951 | ** program. |
| 155576 | 155952 | */ |
| 155577 | 155953 | for(ii=0; ii<nTabList; ii++){ |
| 155578 | 155954 | int addrExplain; |
| 155579 | 155955 | int wsFlags; |
| 155956 | + if( pParse->nErr ) goto whereBeginError; |
| 155580 | 155957 | pLevel = &pWInfo->a[ii]; |
| 155581 | 155958 | wsFlags = pLevel->pWLoop->wsFlags; |
| 155582 | 155959 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 155583 | 155960 | if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 155584 | 155961 | constructAutomaticIndex(pParse, &pWInfo->sWC, |
| | @@ -157028,11 +157405,15 @@ |
| 157028 | 157405 | } |
| 157029 | 157406 | }else{ |
| 157030 | 157407 | sqlite3SelectDelete(db, pSub); |
| 157031 | 157408 | } |
| 157032 | 157409 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
| 157033 | | - sqlite3DbFree(db, pTab); |
| 157410 | + |
| 157411 | + /* Defer deleting the temporary table pTab because if an error occurred, |
| 157412 | + ** there could still be references to that table embedded in the |
| 157413 | + ** result-set or ORDER BY clause of the SELECT statement p. */ |
| 157414 | + sqlite3ParserAddCleanup(pParse, sqlite3DbFree, pTab); |
| 157034 | 157415 | } |
| 157035 | 157416 | |
| 157036 | 157417 | if( rc ){ |
| 157037 | 157418 | if( pParse->nErr==0 ){ |
| 157038 | 157419 | assert( pParse->db->mallocFailed ); |
| | @@ -160475,13 +160856,13 @@ |
| 160475 | 160856 | yyStackEntry *yystackEnd; /* Last entry in the stack */ |
| 160476 | 160857 | #endif |
| 160477 | 160858 | }; |
| 160478 | 160859 | typedef struct yyParser yyParser; |
| 160479 | 160860 | |
| 160861 | +/* #include <assert.h> */ |
| 160480 | 160862 | #ifndef NDEBUG |
| 160481 | 160863 | /* #include <stdio.h> */ |
| 160482 | | -/* #include <assert.h> */ |
| 160483 | 160864 | static FILE *yyTraceFILE = 0; |
| 160484 | 160865 | static char *yyTracePrompt = 0; |
| 160485 | 160866 | #endif /* NDEBUG */ |
| 160486 | 160867 | |
| 160487 | 160868 | #ifndef NDEBUG |
| | @@ -164226,12 +164607,12 @@ |
| 164226 | 164607 | assert( yypParser->yytos>=yypParser->yystack ); |
| 164227 | 164608 | assert( yyact==yypParser->yytos->stateno ); |
| 164228 | 164609 | yyact = yy_find_shift_action((YYCODETYPE)yymajor,yyact); |
| 164229 | 164610 | if( yyact >= YY_MIN_REDUCE ){ |
| 164230 | 164611 | unsigned int yyruleno = yyact - YY_MIN_REDUCE; /* Reduce by this rule */ |
| 164231 | | - assert( yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ); |
| 164232 | 164612 | #ifndef NDEBUG |
| 164613 | + assert( yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ); |
| 164233 | 164614 | if( yyTraceFILE ){ |
| 164234 | 164615 | int yysize = yyRuleInfoNRhs[yyruleno]; |
| 164235 | 164616 | if( yysize ){ |
| 164236 | 164617 | fprintf(yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n", |
| 164237 | 164618 | yyTracePrompt, |
| | @@ -167559,10 +167940,13 @@ |
| 167559 | 167940 | ** So it needs to be freed here. Todo: Why not roll the temp schema into |
| 167560 | 167941 | ** the same sqliteMalloc() as the one that allocates the database |
| 167561 | 167942 | ** structure? |
| 167562 | 167943 | */ |
| 167563 | 167944 | sqlite3DbFree(db, db->aDb[1].pSchema); |
| 167945 | + if( db->xAutovacDestr ){ |
| 167946 | + db->xAutovacDestr(db->pAutovacPagesArg); |
| 167947 | + } |
| 167564 | 167948 | sqlite3_mutex_leave(db->mutex); |
| 167565 | 167949 | db->eOpenState = SQLITE_STATE_CLOSED; |
| 167566 | 167950 | sqlite3_mutex_free(db->mutex); |
| 167567 | 167951 | assert( sqlite3LookasideUsed(db,0)==0 ); |
| 167568 | 167952 | if( db->lookaside.bMalloced ){ |
| | @@ -167613,11 +167997,11 @@ |
| 167613 | 167997 | sqlite3BtreeLeaveAll(db); |
| 167614 | 167998 | |
| 167615 | 167999 | /* Any deferred constraint violations have now been resolved. */ |
| 167616 | 168000 | db->nDeferredCons = 0; |
| 167617 | 168001 | db->nDeferredImmCons = 0; |
| 167618 | | - db->flags &= ~(u64)SQLITE_DeferFKs; |
| 168002 | + db->flags &= ~(u64)(SQLITE_DeferFKs|SQLITE_CorruptRdOnly); |
| 167619 | 168003 | |
| 167620 | 168004 | /* If one has been configured, invoke the rollback-hook callback */ |
| 167621 | 168005 | if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ |
| 167622 | 168006 | db->xRollbackCallback(db->pRollbackArg); |
| 167623 | 168007 | } |
| | @@ -168459,10 +168843,38 @@ |
| 168459 | 168843 | db->pPreUpdateArg = pArg; |
| 168460 | 168844 | sqlite3_mutex_leave(db->mutex); |
| 168461 | 168845 | return pRet; |
| 168462 | 168846 | } |
| 168463 | 168847 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
| 168848 | + |
| 168849 | +/* |
| 168850 | +** Register a function to be invoked prior to each autovacuum that |
| 168851 | +** determines the number of pages to vacuum. |
| 168852 | +*/ |
| 168853 | +SQLITE_API int sqlite3_autovacuum_pages( |
| 168854 | + sqlite3 *db, /* Attach the hook to this database */ |
| 168855 | + unsigned int (*xCallback)(void*,const char*,u32,u32,u32), |
| 168856 | + void *pArg, /* Argument to the function */ |
| 168857 | + void (*xDestructor)(void*) /* Destructor for pArg */ |
| 168858 | +){ |
| 168859 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 168860 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 168861 | + if( xDestructor ) xDestructor(pArg); |
| 168862 | + return SQLITE_MISUSE_BKPT; |
| 168863 | + } |
| 168864 | +#endif |
| 168865 | + sqlite3_mutex_enter(db->mutex); |
| 168866 | + if( db->xAutovacDestr ){ |
| 168867 | + db->xAutovacDestr(db->pAutovacPagesArg); |
| 168868 | + } |
| 168869 | + db->xAutovacPages = xCallback; |
| 168870 | + db->pAutovacPagesArg = pArg; |
| 168871 | + db->xAutovacDestr = xDestructor; |
| 168872 | + sqlite3_mutex_leave(db->mutex); |
| 168873 | + return SQLITE_OK; |
| 168874 | +} |
| 168875 | + |
| 168464 | 168876 | |
| 168465 | 168877 | #ifndef SQLITE_OMIT_WAL |
| 168466 | 168878 | /* |
| 168467 | 168879 | ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint(). |
| 168468 | 168880 | ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file |
| | @@ -169313,12 +169725,12 @@ |
| 169313 | 169725 | ** |
| 169314 | 169726 | ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were |
| 169315 | 169727 | ** dealt with in the previous code block. Besides these, the only |
| 169316 | 169728 | ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY, |
| 169317 | 169729 | ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE, |
| 169318 | | - ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits. Silently mask |
| 169319 | | - ** off all other flags. |
| 169730 | + ** SQLITE_OPEN_PRIVATECACHE, SQLITE_OPEN_EXRESCODE, and some reserved |
| 169731 | + ** bits. Silently mask off all other flags. |
| 169320 | 169732 | */ |
| 169321 | 169733 | flags &= ~( SQLITE_OPEN_DELETEONCLOSE | |
| 169322 | 169734 | SQLITE_OPEN_EXCLUSIVE | |
| 169323 | 169735 | SQLITE_OPEN_MAIN_DB | |
| 169324 | 169736 | SQLITE_OPEN_TEMP_DB | |
| | @@ -169349,11 +169761,11 @@ |
| 169349 | 169761 | if( isThreadsafe==0 ){ |
| 169350 | 169762 | sqlite3MutexWarnOnContention(db->mutex); |
| 169351 | 169763 | } |
| 169352 | 169764 | } |
| 169353 | 169765 | sqlite3_mutex_enter(db->mutex); |
| 169354 | | - db->errMask = 0xff; |
| 169766 | + db->errMask = (flags & SQLITE_OPEN_EXRESCODE)!=0 ? 0xffffffff : 0xff; |
| 169355 | 169767 | db->nDb = 2; |
| 169356 | 169768 | db->eOpenState = SQLITE_STATE_BUSY; |
| 169357 | 169769 | db->aDb = db->aDbStatic; |
| 169358 | 169770 | db->lookaside.bDisable = 1; |
| 169359 | 169771 | db->lookaside.sz = 0; |
| | @@ -169581,12 +169993,12 @@ |
| 169581 | 169993 | assert( db->mutex!=0 || isThreadsafe==0 |
| 169582 | 169994 | || sqlite3GlobalConfig.bFullMutex==0 ); |
| 169583 | 169995 | sqlite3_mutex_leave(db->mutex); |
| 169584 | 169996 | } |
| 169585 | 169997 | rc = sqlite3_errcode(db); |
| 169586 | | - assert( db!=0 || rc==SQLITE_NOMEM ); |
| 169587 | | - if( rc==SQLITE_NOMEM ){ |
| 169998 | + assert( db!=0 || (rc&0xff)==SQLITE_NOMEM ); |
| 169999 | + if( (rc&0xff)==SQLITE_NOMEM ){ |
| 169588 | 170000 | sqlite3_close(db); |
| 169589 | 170001 | db = 0; |
| 169590 | 170002 | }else if( rc!=SQLITE_OK ){ |
| 169591 | 170003 | db->eOpenState = SQLITE_STATE_SICK; |
| 169592 | 170004 | } |
| | @@ -169597,11 +170009,11 @@ |
| 169597 | 170009 | void *pArg = sqlite3GlobalConfig.pSqllogArg; |
| 169598 | 170010 | sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0); |
| 169599 | 170011 | } |
| 169600 | 170012 | #endif |
| 169601 | 170013 | sqlite3_free_filename(zOpen); |
| 169602 | | - return rc & 0xff; |
| 170014 | + return rc; |
| 169603 | 170015 | } |
| 169604 | 170016 | |
| 169605 | 170017 | |
| 169606 | 170018 | /* |
| 169607 | 170019 | ** Open a new database handle. |
| | @@ -178147,10 +178559,13 @@ |
| 178147 | 178559 | while( rc==SQLITE_OK && !pNear->bEof ){ |
| 178148 | 178560 | fts3EvalNextRow(pCsr, pNear, &rc); |
| 178149 | 178561 | if( bEofSave==0 && pNear->iDocid==iDocid ) break; |
| 178150 | 178562 | } |
| 178151 | 178563 | assert( rc!=SQLITE_OK || pPhrase->bIncr==0 ); |
| 178564 | + if( rc==SQLITE_OK && pNear->bEof!=bEofSave ){ |
| 178565 | + rc = FTS_CORRUPT_VTAB; |
| 178566 | + } |
| 178152 | 178567 | } |
| 178153 | 178568 | if( bTreeEof ){ |
| 178154 | 178569 | while( rc==SQLITE_OK && !pNear->bEof ){ |
| 178155 | 178570 | fts3EvalNextRow(pCsr, pNear, &rc); |
| 178156 | 178571 | } |
| | @@ -189837,17 +190252,17 @@ |
| 189837 | 190252 | int iEnd = 0; |
| 189838 | 190253 | int iCurrent = 0; |
| 189839 | 190254 | const char *zDoc; |
| 189840 | 190255 | int nDoc; |
| 189841 | 190256 | |
| 189842 | | - /* Initialize the contents of sCtx.aTerm[] for column iCol. There is |
| 189843 | | - ** no way that this operation can fail, so the return code from |
| 189844 | | - ** fts3ExprIterate() can be discarded. |
| 190257 | + /* Initialize the contents of sCtx.aTerm[] for column iCol. This |
| 190258 | + ** operation may fail if the database contains corrupt records. |
| 189845 | 190259 | */ |
| 189846 | 190260 | sCtx.iCol = iCol; |
| 189847 | 190261 | sCtx.iTerm = 0; |
| 189848 | | - (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx); |
| 190262 | + rc = fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx); |
| 190263 | + if( rc!=SQLITE_OK ) goto offsets_out; |
| 189849 | 190264 | |
| 189850 | 190265 | /* Retreive the text stored in column iCol. If an SQL NULL is stored |
| 189851 | 190266 | ** in column iCol, jump immediately to the next iteration of the loop. |
| 189852 | 190267 | ** If an OOM occurs while retrieving the data (this can happen if SQLite |
| 189853 | 190268 | ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM |
| | @@ -190856,11 +191271,11 @@ |
| 190856 | 191271 | # define ALWAYS(X) (X) |
| 190857 | 191272 | # define NEVER(X) (X) |
| 190858 | 191273 | # endif |
| 190859 | 191274 | # define testcase(X) |
| 190860 | 191275 | #endif |
| 190861 | | -#if defined(NDEBUG) |
| 191276 | +#if !defined(SQLITE_DEBUG) && !defined(SQLITE_COVERAGE_TEST) |
| 190862 | 191277 | # define VVA(X) |
| 190863 | 191278 | #else |
| 190864 | 191279 | # define VVA(X) X |
| 190865 | 191280 | #endif |
| 190866 | 191281 | |
| | @@ -192407,12 +192822,15 @@ |
| 192407 | 192822 | }else{ |
| 192408 | 192823 | JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]); |
| 192409 | 192824 | if( pNew==0 ) return 0; |
| 192410 | 192825 | pTarget = &pParse->aNode[iTarget]; |
| 192411 | 192826 | if( pNew!=&pTarget[j+1] ){ |
| 192412 | | - assert( pTarget[j+1].eU==0 || pTarget[j+1].eU==1 ); |
| 192827 | + assert( pTarget[j+1].eU==0 |
| 192828 | + || pTarget[j+1].eU==1 |
| 192829 | + || pTarget[j+1].eU==2 ); |
| 192413 | 192830 | testcase( pTarget[j+1].eU==1 ); |
| 192831 | + testcase( pTarget[j+1].eU==2 ); |
| 192414 | 192832 | VVA( pTarget[j+1].eU = 5 ); |
| 192415 | 192833 | pTarget[j+1].u.pPatch = pNew; |
| 192416 | 192834 | pTarget[j+1].jnFlags |= JNODE_PATCH; |
| 192417 | 192835 | } |
| 192418 | 192836 | } |
| | @@ -201403,10 +201821,17 @@ |
| 201403 | 201821 | ** Swap two objects of type TYPE. |
| 201404 | 201822 | */ |
| 201405 | 201823 | #if !defined(SQLITE_AMALGAMATION) |
| 201406 | 201824 | # define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 201407 | 201825 | #endif |
| 201826 | + |
| 201827 | +/* |
| 201828 | +** Name of the URI option that causes RBU to take an exclusive lock as |
| 201829 | +** part of the incremental checkpoint operation. |
| 201830 | +*/ |
| 201831 | +#define RBU_EXCLUSIVE_CHECKPOINT "rbu_exclusive_checkpoint" |
| 201832 | + |
| 201408 | 201833 | |
| 201409 | 201834 | /* |
| 201410 | 201835 | ** The rbu_state table is used to save the state of a partially applied |
| 201411 | 201836 | ** update so that it can be resumed later. The table consists of integer |
| 201412 | 201837 | ** keys mapped to values as follows: |
| | @@ -204050,17 +204475,23 @@ |
| 204050 | 204475 | |
| 204051 | 204476 | |
| 204052 | 204477 | /* |
| 204053 | 204478 | ** Open the database handle and attach the RBU database as "rbu". If an |
| 204054 | 204479 | ** error occurs, leave an error code and message in the RBU handle. |
| 204480 | +** |
| 204481 | +** If argument dbMain is not NULL, then it is a database handle already |
| 204482 | +** open on the target database. Use this handle instead of opening a new |
| 204483 | +** one. |
| 204055 | 204484 | */ |
| 204056 | | -static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){ |
| 204485 | +static void rbuOpenDatabase(sqlite3rbu *p, sqlite3 *dbMain, int *pbRetry){ |
| 204057 | 204486 | assert( p->rc || (p->dbMain==0 && p->dbRbu==0) ); |
| 204058 | 204487 | assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 ); |
| 204488 | + assert( dbMain==0 || rbuIsVacuum(p)==0 ); |
| 204059 | 204489 | |
| 204060 | 204490 | /* Open the RBU database */ |
| 204061 | 204491 | p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1); |
| 204492 | + p->dbMain = dbMain; |
| 204062 | 204493 | |
| 204063 | 204494 | if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){ |
| 204064 | 204495 | sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p); |
| 204065 | 204496 | if( p->zState==0 ){ |
| 204066 | 204497 | const char *zFile = sqlite3_db_filename(p->dbRbu, "main"); |
| | @@ -204422,19 +204853,35 @@ |
| 204422 | 204853 | p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff); |
| 204423 | 204854 | } |
| 204424 | 204855 | |
| 204425 | 204856 | |
| 204426 | 204857 | /* |
| 204427 | | -** Take an EXCLUSIVE lock on the database file. |
| 204858 | +** Take an EXCLUSIVE lock on the database file. Return SQLITE_OK if |
| 204859 | +** successful, or an SQLite error code otherwise. |
| 204428 | 204860 | */ |
| 204429 | | -static void rbuLockDatabase(sqlite3rbu *p){ |
| 204430 | | - sqlite3_file *pReal = p->pTargetFd->pReal; |
| 204431 | | - assert( p->rc==SQLITE_OK ); |
| 204432 | | - p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED); |
| 204433 | | - if( p->rc==SQLITE_OK ){ |
| 204434 | | - p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE); |
| 204861 | +static int rbuLockDatabase(sqlite3 *db){ |
| 204862 | + int rc = SQLITE_OK; |
| 204863 | + sqlite3_file *fd = 0; |
| 204864 | + sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, &fd); |
| 204865 | + |
| 204866 | + if( fd->pMethods ){ |
| 204867 | + rc = fd->pMethods->xLock(fd, SQLITE_LOCK_SHARED); |
| 204868 | + if( rc==SQLITE_OK ){ |
| 204869 | + rc = fd->pMethods->xLock(fd, SQLITE_LOCK_EXCLUSIVE); |
| 204870 | + } |
| 204435 | 204871 | } |
| 204872 | + return rc; |
| 204873 | +} |
| 204874 | + |
| 204875 | +/* |
| 204876 | +** Return true if the database handle passed as the only argument |
| 204877 | +** was opened with the rbu_exclusive_checkpoint=1 URI parameter |
| 204878 | +** specified. Or false otherwise. |
| 204879 | +*/ |
| 204880 | +static int rbuExclusiveCheckpoint(sqlite3 *db){ |
| 204881 | + const char *zUri = sqlite3_db_filename(db, 0); |
| 204882 | + return sqlite3_uri_boolean(zUri, RBU_EXCLUSIVE_CHECKPOINT, 0); |
| 204436 | 204883 | } |
| 204437 | 204884 | |
| 204438 | 204885 | #if defined(_WIN32_WCE) |
| 204439 | 204886 | static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){ |
| 204440 | 204887 | int nChar; |
| | @@ -204488,22 +204935,28 @@ |
| 204488 | 204935 | ** in WAL mode). So no other connection may be writing the db. |
| 204489 | 204936 | ** |
| 204490 | 204937 | ** In order to ensure that there are no database readers, an EXCLUSIVE |
| 204491 | 204938 | ** lock is obtained here before the *-oal is moved to *-wal. |
| 204492 | 204939 | */ |
| 204493 | | - rbuLockDatabase(p); |
| 204940 | + sqlite3 *dbMain = 0; |
| 204941 | + rbuFileSuffix3(zBase, zWal); |
| 204942 | + rbuFileSuffix3(zBase, zOal); |
| 204943 | + |
| 204944 | + /* Re-open the databases. */ |
| 204945 | + rbuObjIterFinalize(&p->objiter); |
| 204946 | + sqlite3_close(p->dbRbu); |
| 204947 | + sqlite3_close(p->dbMain); |
| 204948 | + p->dbMain = 0; |
| 204949 | + p->dbRbu = 0; |
| 204950 | + |
| 204951 | + dbMain = rbuOpenDbhandle(p, p->zTarget, 1); |
| 204952 | + if( dbMain ){ |
| 204953 | + assert( p->rc==SQLITE_OK ); |
| 204954 | + p->rc = rbuLockDatabase(dbMain); |
| 204955 | + } |
| 204956 | + |
| 204494 | 204957 | if( p->rc==SQLITE_OK ){ |
| 204495 | | - rbuFileSuffix3(zBase, zWal); |
| 204496 | | - rbuFileSuffix3(zBase, zOal); |
| 204497 | | - |
| 204498 | | - /* Re-open the databases. */ |
| 204499 | | - rbuObjIterFinalize(&p->objiter); |
| 204500 | | - sqlite3_close(p->dbRbu); |
| 204501 | | - sqlite3_close(p->dbMain); |
| 204502 | | - p->dbMain = 0; |
| 204503 | | - p->dbRbu = 0; |
| 204504 | | - |
| 204505 | 204958 | #if defined(_WIN32_WCE) |
| 204506 | 204959 | { |
| 204507 | 204960 | LPWSTR zWideOal; |
| 204508 | 204961 | LPWSTR zWideWal; |
| 204509 | 204962 | |
| | @@ -204526,15 +204979,23 @@ |
| 204526 | 204979 | } |
| 204527 | 204980 | } |
| 204528 | 204981 | #else |
| 204529 | 204982 | p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK; |
| 204530 | 204983 | #endif |
| 204984 | + } |
| 204531 | 204985 | |
| 204532 | | - if( p->rc==SQLITE_OK ){ |
| 204533 | | - rbuOpenDatabase(p, 0); |
| 204534 | | - rbuSetupCheckpoint(p, 0); |
| 204535 | | - } |
| 204986 | + if( p->rc!=SQLITE_OK |
| 204987 | + || rbuIsVacuum(p) |
| 204988 | + || rbuExclusiveCheckpoint(dbMain)==0 |
| 204989 | + ){ |
| 204990 | + sqlite3_close(dbMain); |
| 204991 | + dbMain = 0; |
| 204992 | + } |
| 204993 | + |
| 204994 | + if( p->rc==SQLITE_OK ){ |
| 204995 | + rbuOpenDatabase(p, dbMain, 0); |
| 204996 | + rbuSetupCheckpoint(p, 0); |
| 204536 | 204997 | } |
| 204537 | 204998 | } |
| 204538 | 204999 | |
| 204539 | 205000 | sqlite3_free(zWal); |
| 204540 | 205001 | sqlite3_free(zOal); |
| | @@ -205281,13 +205742,13 @@ |
| 205281 | 205742 | ** to be a wal-mode db. But, this may have happened due to an earlier |
| 205282 | 205743 | ** RBU vacuum operation leaving an old wal file in the directory. |
| 205283 | 205744 | ** If this is the case, it will have been checkpointed and deleted |
| 205284 | 205745 | ** when the handle was closed and a second attempt to open the |
| 205285 | 205746 | ** database may succeed. */ |
| 205286 | | - rbuOpenDatabase(p, &bRetry); |
| 205747 | + rbuOpenDatabase(p, 0, &bRetry); |
| 205287 | 205748 | if( bRetry ){ |
| 205288 | | - rbuOpenDatabase(p, 0); |
| 205749 | + rbuOpenDatabase(p, 0, 0); |
| 205289 | 205750 | } |
| 205290 | 205751 | } |
| 205291 | 205752 | |
| 205292 | 205753 | if( p->rc==SQLITE_OK ){ |
| 205293 | 205754 | pState = rbuLoadState(p); |
| | @@ -205378,10 +205839,18 @@ |
| 205378 | 205839 | } |
| 205379 | 205840 | } |
| 205380 | 205841 | }else if( p->eStage==RBU_STAGE_MOVE ){ |
| 205381 | 205842 | /* no-op */ |
| 205382 | 205843 | }else if( p->eStage==RBU_STAGE_CKPT ){ |
| 205844 | + if( !rbuIsVacuum(p) && rbuExclusiveCheckpoint(p->dbMain) ){ |
| 205845 | + /* If the rbu_exclusive_checkpoint=1 URI parameter was specified |
| 205846 | + ** and an incremental checkpoint is being resumed, attempt an |
| 205847 | + ** exclusive lock on the db file. If this fails, so be it. */ |
| 205848 | + p->eStage = RBU_STAGE_DONE; |
| 205849 | + rbuLockDatabase(p->dbMain); |
| 205850 | + p->eStage = RBU_STAGE_CKPT; |
| 205851 | + } |
| 205383 | 205852 | rbuSetupCheckpoint(p, pState); |
| 205384 | 205853 | }else if( p->eStage==RBU_STAGE_DONE ){ |
| 205385 | 205854 | p->rc = SQLITE_DONE; |
| 205386 | 205855 | }else{ |
| 205387 | 205856 | p->rc = SQLITE_CORRUPT; |
| | @@ -205415,11 +205884,10 @@ |
| 205415 | 205884 | const char *zTarget, |
| 205416 | 205885 | const char *zRbu, |
| 205417 | 205886 | const char *zState |
| 205418 | 205887 | ){ |
| 205419 | 205888 | if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); } |
| 205420 | | - /* TODO: Check that zTarget and zRbu are non-NULL */ |
| 205421 | 205889 | return openRbuHandle(zTarget, zRbu, zState); |
| 205422 | 205890 | } |
| 205423 | 205891 | |
| 205424 | 205892 | /* |
| 205425 | 205893 | ** Open a handle to begin or resume an RBU VACUUM operation. |
| | @@ -215536,13 +216004,13 @@ |
| 215536 | 216004 | fts5yyStackEntry *fts5yystackEnd; /* Last entry in the stack */ |
| 215537 | 216005 | #endif |
| 215538 | 216006 | }; |
| 215539 | 216007 | typedef struct fts5yyParser fts5yyParser; |
| 215540 | 216008 | |
| 215541 | | -#ifndef NDEBUG |
| 215542 | | -/* #include <stdio.h> */ |
| 215543 | 216009 | /* #include <assert.h> */ |
| 216010 | +#ifndef NDEBUG |
| 216011 | +/* #include <stdio.h> */ |
| 215544 | 216012 | static FILE *fts5yyTraceFILE = 0; |
| 215545 | 216013 | static char *fts5yyTracePrompt = 0; |
| 215546 | 216014 | #endif /* NDEBUG */ |
| 215547 | 216015 | |
| 215548 | 216016 | #ifndef NDEBUG |
| | @@ -216475,12 +216943,12 @@ |
| 216475 | 216943 | assert( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystack ); |
| 216476 | 216944 | assert( fts5yyact==fts5yypParser->fts5yytos->stateno ); |
| 216477 | 216945 | fts5yyact = fts5yy_find_shift_action((fts5YYCODETYPE)fts5yymajor,fts5yyact); |
| 216478 | 216946 | if( fts5yyact >= fts5YY_MIN_REDUCE ){ |
| 216479 | 216947 | unsigned int fts5yyruleno = fts5yyact - fts5YY_MIN_REDUCE; /* Reduce by this rule */ |
| 216480 | | - assert( fts5yyruleno<(int)(sizeof(fts5yyRuleName)/sizeof(fts5yyRuleName[0])) ); |
| 216481 | 216948 | #ifndef NDEBUG |
| 216949 | + assert( fts5yyruleno<(int)(sizeof(fts5yyRuleName)/sizeof(fts5yyRuleName[0])) ); |
| 216482 | 216950 | if( fts5yyTraceFILE ){ |
| 216483 | 216951 | int fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno]; |
| 216484 | 216952 | if( fts5yysize ){ |
| 216485 | 216953 | fprintf(fts5yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n", |
| 216486 | 216954 | fts5yyTracePrompt, |
| | @@ -221608,20 +222076,29 @@ |
| 221608 | 222076 | Fts5PoslistWriter writer; |
| 221609 | 222077 | int bOk; /* True if ok to populate */ |
| 221610 | 222078 | int bMiss; |
| 221611 | 222079 | }; |
| 221612 | 222080 | |
| 222081 | +/* |
| 222082 | +** Clear the position lists associated with all phrases in the expression |
| 222083 | +** passed as the first argument. Argument bLive is true if the expression |
| 222084 | +** might be pointing to a real entry, otherwise it has just been reset. |
| 222085 | +** |
| 222086 | +** At present this function is only used for detail=col and detail=none |
| 222087 | +** fts5 tables. This implies that all phrases must be at most 1 token |
| 222088 | +** in size, as phrase matches are not supported without detail=full. |
| 222089 | +*/ |
| 221613 | 222090 | static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){ |
| 221614 | 222091 | Fts5PoslistPopulator *pRet; |
| 221615 | 222092 | pRet = sqlite3_malloc64(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase); |
| 221616 | 222093 | if( pRet ){ |
| 221617 | 222094 | int i; |
| 221618 | 222095 | memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase); |
| 221619 | 222096 | for(i=0; i<pExpr->nPhrase; i++){ |
| 221620 | 222097 | Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist; |
| 221621 | 222098 | Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode; |
| 221622 | | - assert( pExpr->apExprPhrase[i]->nTerm==1 ); |
| 222099 | + assert( pExpr->apExprPhrase[i]->nTerm<=1 ); |
| 221623 | 222100 | if( bLive && |
| 221624 | 222101 | (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof) |
| 221625 | 222102 | ){ |
| 221626 | 222103 | pRet[i].bMiss = 1; |
| 221627 | 222104 | }else{ |
| | @@ -231982,11 +232459,11 @@ |
| 231982 | 232459 | int nArg, /* Number of args */ |
| 231983 | 232460 | sqlite3_value **apUnused /* Function arguments */ |
| 231984 | 232461 | ){ |
| 231985 | 232462 | assert( nArg==0 ); |
| 231986 | 232463 | UNUSED_PARAM2(nArg, apUnused); |
| 231987 | | - sqlite3_result_text(pCtx, "fts5: 2021-10-22 11:17:29 1a038242dc6c0cab97dd9375acfce62aa1c386debc36aaed388d366b87ddd931", -1, SQLITE_TRANSIENT); |
| 232464 | + sqlite3_result_text(pCtx, "fts5: 2021-11-15 19:10:13 bd66ab8a1bc3c43a57c7caff5f54545b0feb0177f1f51492f30d308c123c43ba", -1, SQLITE_TRANSIENT); |
| 231988 | 232465 | } |
| 231989 | 232466 | |
| 231990 | 232467 | /* |
| 231991 | 232468 | ** Return true if zName is the extension on one of the shadow tables used |
| 231992 | 232469 | ** by this module. |
| 231993 | 232470 | |