11/******************************************************************************
22** This file is an amalgamation of many separate C source files from SQLite
3- ** version 3.40.0 . By combining all the individual C code files into this
3+ ** version 3.40.1 . By combining all the individual C code files into this
44** single large file, the entire code can be compiled as a single translation
55** unit. This allows many compilers to do optimizations that would not be
66** possible if the files were compiled separately. Performance improvements
@@ -452,9 +452,9 @@ extern "C" {
452452** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453453** [sqlite_version()] and [sqlite_source_id()].
454454*/
455- #define SQLITE_VERSION "3.40.0 "
456- #define SQLITE_VERSION_NUMBER 3040000
457- #define SQLITE_SOURCE_ID "2022-11-16 12:10:08 89c459e766ea7e9165d0beeb124708b955a4950d0f4792f457465d71b158d318 "
455+ #define SQLITE_VERSION "3.40.1 "
456+ #define SQLITE_VERSION_NUMBER 3040001
457+ #define SQLITE_SOURCE_ID "2022-12-28 14:03:47 df5c253c0b3dd24916e4ec7cf77d3db5294cc9fd45ae7b9c5e82ad8197f38a24 "
458458
459459/*
460460** CAPI3REF: Run-Time Library Version Numbers
@@ -1498,6 +1498,12 @@ struct sqlite3_io_methods {
14981498**
14991499** <li>[[SQLITE_FCNTL_CKSM_FILE]]
15001500** Used by the cksmvfs VFS module only.
1501+ **
1502+ ** <li>[[SQLITE_FCNTL_RESET_CACHE]]
1503+ ** If there is currently no transaction open on the database, and the
1504+ ** database is not a temp db, then this file-control purges the contents
1505+ ** of the in-memory page cache. If there is an open transaction, or if
1506+ ** the db is a temp-db, it is a no-op, not an error.
15011507** </ul>
15021508*/
15031509#define SQLITE_FCNTL_LOCKSTATE 1
@@ -1540,6 +1546,7 @@ struct sqlite3_io_methods {
15401546#define SQLITE_FCNTL_CKPT_START 39
15411547#define SQLITE_FCNTL_EXTERNAL_READER 40
15421548#define SQLITE_FCNTL_CKSM_FILE 41
1549+ #define SQLITE_FCNTL_RESET_CACHE 42
15431550
15441551/* deprecated names */
15451552#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
@@ -15714,6 +15721,8 @@ SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
1571415721
1571515722SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor*, BtCursor*, i64);
1571615723
15724+ SQLITE_PRIVATE void sqlite3BtreeClearCache(Btree*);
15725+
1571715726/*
1571815727** If we are not using shared cache, then there is no need to
1571915728** use mutexes to access the BtShared structures. So make the
@@ -27290,9 +27299,13 @@ static int memsys5Roundup(int n){
2729027299 if( n<=mem5.szAtom ) return mem5.szAtom;
2729127300 return mem5.szAtom*2;
2729227301 }
27293- if( n>0x40000000 ) return 0;
27302+ if( n>0x10000000 ){
27303+ if( n>0x40000000 ) return 0;
27304+ if( n>0x20000000 ) return 0x40000000;
27305+ return 0x20000000;
27306+ }
2729427307 for(iFullSz=mem5.szAtom*8; iFullSz<n; iFullSz *= 4);
27295- if( (iFullSz/2)>=n ) return iFullSz/2;
27308+ if( (iFullSz/2)>=(i64) n ) return iFullSz/2;
2729627309 return iFullSz;
2729727310}
2729827311
@@ -37338,6 +37351,9 @@ static int robust_open(const char *z, int f, mode_t m){
3733837351 break;
3733937352 }
3734037353 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
37354+ if( (f & (O_EXCL|O_CREAT))==(O_EXCL|O_CREAT) ){
37355+ (void)osUnlink(z);
37356+ }
3734137357 osClose(fd);
3734237358 sqlite3_log(SQLITE_WARNING,
3734337359 "attempt to open \"%s\" as file descriptor %d", z, fd);
@@ -51071,6 +51087,7 @@ static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
5107151087static int memdbSync(sqlite3_file*, int flags);
5107251088static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
5107351089static int memdbLock(sqlite3_file*, int);
51090+ static int memdbUnlock(sqlite3_file*, int);
5107451091/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
5107551092static int memdbFileControl(sqlite3_file*, int op, void *pArg);
5107651093/* static int memdbSectorSize(sqlite3_file*); // not used */
@@ -51129,7 +51146,7 @@ static const sqlite3_io_methods memdb_io_methods = {
5112951146 memdbSync, /* xSync */
5113051147 memdbFileSize, /* xFileSize */
5113151148 memdbLock, /* xLock */
51132- memdbLock , /* xUnlock - same as xLock in this case */
51149+ memdbUnlock , /* xUnlock */
5113351150 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
5113451151 memdbFileControl, /* xFileControl */
5113551152 0, /* memdbSectorSize,*/ /* xSectorSize */
@@ -51330,39 +51347,81 @@ static int memdbLock(sqlite3_file *pFile, int eLock){
5133051347 MemFile *pThis = (MemFile*)pFile;
5133151348 MemStore *p = pThis->pStore;
5133251349 int rc = SQLITE_OK;
51333- if( eLock= =pThis->eLock ) return SQLITE_OK;
51350+ if( eLock< =pThis->eLock ) return SQLITE_OK;
5133451351 memdbEnter(p);
51335- if( eLock>SQLITE_LOCK_SHARED ){
51336- if( p->mFlags & SQLITE_DESERIALIZE_READONLY ){
51337- rc = SQLITE_READONLY;
51338- }else if( pThis->eLock<=SQLITE_LOCK_SHARED ){
51339- if( p->nWrLock ){
51340- rc = SQLITE_BUSY;
51341- }else{
51342- p->nWrLock = 1;
51352+
51353+ assert( p->nWrLock==0 || p->nWrLock==1 );
51354+ assert( pThis->eLock<=SQLITE_LOCK_SHARED || p->nWrLock==1 );
51355+ assert( pThis->eLock==SQLITE_LOCK_NONE || p->nRdLock>=1 );
51356+
51357+ if( eLock>SQLITE_LOCK_SHARED && (p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
51358+ rc = SQLITE_READONLY;
51359+ }else{
51360+ switch( eLock ){
51361+ case SQLITE_LOCK_SHARED: {
51362+ assert( pThis->eLock==SQLITE_LOCK_NONE );
51363+ if( p->nWrLock>0 ){
51364+ rc = SQLITE_BUSY;
51365+ }else{
51366+ p->nRdLock++;
51367+ }
51368+ break;
51369+ };
51370+
51371+ case SQLITE_LOCK_RESERVED:
51372+ case SQLITE_LOCK_PENDING: {
51373+ assert( pThis->eLock>=SQLITE_LOCK_SHARED );
51374+ if( ALWAYS(pThis->eLock==SQLITE_LOCK_SHARED) ){
51375+ if( p->nWrLock>0 ){
51376+ rc = SQLITE_BUSY;
51377+ }else{
51378+ p->nWrLock = 1;
51379+ }
51380+ }
51381+ break;
51382+ }
51383+
51384+ default: {
51385+ assert( eLock==SQLITE_LOCK_EXCLUSIVE );
51386+ assert( pThis->eLock>=SQLITE_LOCK_SHARED );
51387+ if( p->nRdLock>1 ){
51388+ rc = SQLITE_BUSY;
51389+ }else if( pThis->eLock==SQLITE_LOCK_SHARED ){
51390+ p->nWrLock = 1;
51391+ }
51392+ break;
5134351393 }
5134451394 }
51345- }else if( eLock==SQLITE_LOCK_SHARED ){
51346- if( pThis->eLock > SQLITE_LOCK_SHARED ){
51347- assert( p->nWrLock==1 );
51348- p->nWrLock = 0;
51349- }else if( p->nWrLock ){
51350- rc = SQLITE_BUSY;
51351- }else{
51352- p->nRdLock++;
51395+ }
51396+ if( rc==SQLITE_OK ) pThis->eLock = eLock;
51397+ memdbLeave(p);
51398+ return rc;
51399+ }
51400+
51401+ /*
51402+ ** Unlock an memdb-file.
51403+ */
51404+ static int memdbUnlock(sqlite3_file *pFile, int eLock){
51405+ MemFile *pThis = (MemFile*)pFile;
51406+ MemStore *p = pThis->pStore;
51407+ if( eLock>=pThis->eLock ) return SQLITE_OK;
51408+ memdbEnter(p);
51409+
51410+ assert( eLock==SQLITE_LOCK_SHARED || eLock==SQLITE_LOCK_NONE );
51411+ if( eLock==SQLITE_LOCK_SHARED ){
51412+ if( ALWAYS(pThis->eLock>SQLITE_LOCK_SHARED) ){
51413+ p->nWrLock--;
5135351414 }
5135451415 }else{
51355- assert( eLock==SQLITE_LOCK_NONE );
5135651416 if( pThis->eLock>SQLITE_LOCK_SHARED ){
51357- assert( p->nWrLock==1 );
51358- p->nWrLock = 0;
51417+ p->nWrLock--;
5135951418 }
51360- assert( p->nRdLock>0 );
5136151419 p->nRdLock--;
5136251420 }
51363- if( rc==SQLITE_OK ) pThis->eLock = eLock;
51421+
51422+ pThis->eLock = eLock;
5136451423 memdbLeave(p);
51365- return rc ;
51424+ return SQLITE_OK ;
5136651425}
5136751426
5136851427#if 0
@@ -51472,7 +51531,7 @@ static int memdbOpen(
5147251531
5147351532 memset(pFile, 0, sizeof(*pFile));
5147451533 szName = sqlite3Strlen30(zName);
51475- if( szName>1 && zName[0]=='/' ){
51534+ if( szName>1 && ( zName[0]=='/' || zName[0]=='\\') ){
5147651535 int i;
5147751536#ifndef SQLITE_MUTEX_OMIT
5147851537 sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
@@ -51819,6 +51878,13 @@ SQLITE_API int sqlite3_deserialize(
5181951878 return rc;
5182051879}
5182151880
51881+ /*
51882+ ** Return true if the VFS is the memvfs.
51883+ */
51884+ SQLITE_PRIVATE int sqlite3IsMemdb(const sqlite3_vfs *pVfs){
51885+ return pVfs==&memdb_vfs;
51886+ }
51887+
5182251888/*
5182351889** This routine is called when the extension is loaded.
5182451890** Register the new VFS.
@@ -79147,6 +79213,17 @@ SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
7914779213*/
7914879214SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
7914979215
79216+ /*
79217+ ** If no transaction is active and the database is not a temp-db, clear
79218+ ** the in-memory pager cache.
79219+ */
79220+ SQLITE_PRIVATE void sqlite3BtreeClearCache(Btree *p){
79221+ BtShared *pBt = p->pBt;
79222+ if( pBt->inTransaction==TRANS_NONE ){
79223+ sqlite3PagerClearCache(pBt->pPager);
79224+ }
79225+ }
79226+
7915079227#if !defined(SQLITE_OMIT_SHARED_CACHE)
7915179228/*
7915279229** Return true if the Btree passed as the only argument is sharable.
@@ -83386,7 +83463,7 @@ SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
8338683463 if( p->db->mallocFailed ){
8338783464 freeP4(p->db, n, pP4);
8338883465 }else{
83389- assert( pP4!=0 );
83466+ assert( pP4!=0 || n==P4_DYNAMIC );
8339083467 assert( p->nOp>0 );
8339183468 pOp = &p->aOp[p->nOp-1];
8339283469 assert( pOp->p4type==P4_NOTUSED );
@@ -132298,7 +132375,7 @@ static const sqlite3_api_routines sqlite3Apis = {
132298132375#endif
132299132376 sqlite3_db_name,
132300132377 /* Version 3.40.0 and later */
132301- sqlite3_value_type
132378+ sqlite3_value_encoding
132302132379};
132303132380
132304132381/* True if x is the directory separator character
@@ -145439,7 +145516,7 @@ SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
145439145516 if( pTrig->pTabSchema==pTab->pSchema
145440145517 && pTrig->table
145441145518 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
145442- && pTrig->pTabSchema!=pTmpSchema
145519+ && ( pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning)
145443145520 ){
145444145521 pTrig->pNext = pList;
145445145522 pList = pTrig;
@@ -155623,7 +155700,7 @@ SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
155623155700** block sorting is required.
155624155701*/
155625155702SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
155626- return pWInfo->nOBSat;
155703+ return pWInfo->nOBSat<0 ? 0 : pWInfo->nOBSat ;
155627155704}
155628155705
155629155706/*
@@ -174531,7 +174608,7 @@ SQLITE_API int sqlite3_overload_function(
174531174608 rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)!=0;
174532174609 sqlite3_mutex_leave(db->mutex);
174533174610 if( rc ) return SQLITE_OK;
174534- zCopy = sqlite3_mprintf(zName);
174611+ zCopy = sqlite3_mprintf("%s", zName);
174535174612 if( zCopy==0 ) return SQLITE_NOMEM;
174536174613 return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF8,
174537174614 zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free);
@@ -176363,6 +176440,9 @@ SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, vo
176363176440 sqlite3BtreeSetPageSize(pBtree, 0, iNew, 0);
176364176441 }
176365176442 rc = SQLITE_OK;
176443+ }else if( op==SQLITE_FCNTL_RESET_CACHE ){
176444+ sqlite3BtreeClearCache(pBtree);
176445+ rc = SQLITE_OK;
176366176446 }else{
176367176447 int nSave = db->busyHandler.nBusy;
176368176448 rc = sqlite3OsFileControl(fd, op, pArg);
@@ -217778,6 +217858,22 @@ static int sessionChangesetNextOne(
217778217858 if( p->op==SQLITE_INSERT ) p->op = SQLITE_DELETE;
217779217859 else if( p->op==SQLITE_DELETE ) p->op = SQLITE_INSERT;
217780217860 }
217861+
217862+ /* If this is an UPDATE that is part of a changeset, then check that
217863+ ** there are no fields in the old.* record that are not (a) PK fields,
217864+ ** or (b) also present in the new.* record.
217865+ **
217866+ ** Such records are technically corrupt, but the rebaser was at one
217867+ ** point generating them. Under most circumstances this is benign, but
217868+ ** can cause spurious SQLITE_RANGE errors when applying the changeset. */
217869+ if( p->bPatchset==0 && p->op==SQLITE_UPDATE){
217870+ for(i=0; i<p->nCol; i++){
217871+ if( p->abPK[i]==0 && p->apValue[i+p->nCol]==0 ){
217872+ sqlite3ValueFree(p->apValue[i]);
217873+ p->apValue[i] = 0;
217874+ }
217875+ }
217876+ }
217781217877 }
217782217878
217783217879 return SQLITE_ROW;
@@ -219974,7 +220070,7 @@ static void sessionAppendPartialUpdate(
219974220070 if( !pIter->abPK[i] && a1[0] ) bData = 1;
219975220071 memcpy(pOut, a1, n1);
219976220072 pOut += n1;
219977- }else if( a2[0]!=0xFF ){
220073+ }else if( a2[0]!=0xFF && a1[0] ){
219978220074 bData = 1;
219979220075 memcpy(pOut, a2, n2);
219980220076 pOut += n2;
@@ -236004,7 +236100,7 @@ static void fts5CheckTransactionState(Fts5FullTable *p, int op, int iSavepoint){
236004236100 break;
236005236101
236006236102 case FTS5_SYNC:
236007- assert( p->ts.eState==1 );
236103+ assert( p->ts.eState==1 || p->ts.eState==2 );
236008236104 p->ts.eState = 2;
236009236105 break;
236010236106
@@ -236019,21 +236115,21 @@ static void fts5CheckTransactionState(Fts5FullTable *p, int op, int iSavepoint){
236019236115 break;
236020236116
236021236117 case FTS5_SAVEPOINT:
236022- assert( p->ts.eState= =1 );
236118+ assert( p->ts.eState> =1 );
236023236119 assert( iSavepoint>=0 );
236024236120 assert( iSavepoint>=p->ts.iSavepoint );
236025236121 p->ts.iSavepoint = iSavepoint;
236026236122 break;
236027236123
236028236124 case FTS5_RELEASE:
236029- assert( p->ts.eState= =1 );
236125+ assert( p->ts.eState> =1 );
236030236126 assert( iSavepoint>=0 );
236031236127 assert( iSavepoint<=p->ts.iSavepoint );
236032236128 p->ts.iSavepoint = iSavepoint-1;
236033236129 break;
236034236130
236035236131 case FTS5_ROLLBACKTO:
236036- assert( p->ts.eState= =1 );
236132+ assert( p->ts.eState> =1 );
236037236133 assert( iSavepoint>=-1 );
236038236134 /* The following assert() can fail if another vtab strikes an error
236039236135 ** within an xSavepoint() call then SQLite calls xRollbackTo() - without
@@ -237369,7 +237465,7 @@ static int fts5UpdateMethod(
237369237465 int rc = SQLITE_OK; /* Return code */
237370237466
237371237467 /* A transaction must be open when this is called. */
237372- assert( pTab->ts.eState==1 );
237468+ assert( pTab->ts.eState==1 || pTab->ts.eState==2 );
237373237469
237374237470 assert( pVtab->zErrMsg==0 );
237375237471 assert( nArg==1 || nArg==(2+pConfig->nCol+2) );
@@ -238537,7 +238633,7 @@ static void fts5SourceIdFunc(
238537238633){
238538238634 assert( nArg==0 );
238539238635 UNUSED_PARAM2(nArg, apUnused);
238540- sqlite3_result_text(pCtx, "fts5: 2022-11-16 12:10:08 89c459e766ea7e9165d0beeb124708b955a4950d0f4792f457465d71b158d318 ", -1, SQLITE_TRANSIENT);
238636+ sqlite3_result_text(pCtx, "fts5: 2022-12-28 14:03:47 df5c253c0b3dd24916e4ec7cf77d3db5294cc9fd45ae7b9c5e82ad8197f38a24 ", -1, SQLITE_TRANSIENT);
238541238637}
238542238638
238543238639/*
0 commit comments