| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.32.0. By combining all the individual C code files into this |
| 3 | +** version 3.32.1. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a single translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -1160,13 +1160,13 @@ |
| 1160 | 1160 | ** |
| 1161 | 1161 | ** See also: [sqlite3_libversion()], |
| 1162 | 1162 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 1163 | 1163 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 1164 | 1164 | */ |
| 1165 | | -#define SQLITE_VERSION "3.32.0" |
| 1166 | | -#define SQLITE_VERSION_NUMBER 3032000 |
| 1167 | | -#define SQLITE_SOURCE_ID "2020-05-04 19:52:00 8eee591d3cb9fadfd5cac5543bd66ef9cb371a72d3ad3241fb3bfd67fb216eda" |
| 1165 | +#define SQLITE_VERSION "3.32.1" |
| 1166 | +#define SQLITE_VERSION_NUMBER 3032001 |
| 1167 | +#define SQLITE_SOURCE_ID "2020-05-25 16:19:56 0c1fcf4711a2e66c813aed38cf41cd3e2123ee8eb6db98118086764c4ba83350" |
| 1168 | 1168 | |
| 1169 | 1169 | /* |
| 1170 | 1170 | ** CAPI3REF: Run-Time Library Version Numbers |
| 1171 | 1171 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 1172 | 1172 | ** |
| | @@ -1545,18 +1545,20 @@ |
| 1545 | 1545 | #define SQLITE_IOERR_DATA (SQLITE_IOERR | (32<<8)) |
| 1546 | 1546 | #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8)) |
| 1547 | 1547 | #define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8)) |
| 1548 | 1548 | #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8)) |
| 1549 | 1549 | #define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8)) |
| 1550 | +#define SQLITE_BUSY_TIMEOUT (SQLITE_BUSY | (3<<8)) |
| 1550 | 1551 | #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8)) |
| 1551 | 1552 | #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8)) |
| 1552 | 1553 | #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8)) |
| 1553 | 1554 | #define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8)) |
| 1554 | 1555 | #define SQLITE_CANTOPEN_DIRTYWAL (SQLITE_CANTOPEN | (5<<8)) /* Not Used */ |
| 1555 | 1556 | #define SQLITE_CANTOPEN_SYMLINK (SQLITE_CANTOPEN | (6<<8)) |
| 1556 | 1557 | #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8)) |
| 1557 | 1558 | #define SQLITE_CORRUPT_SEQUENCE (SQLITE_CORRUPT | (2<<8)) |
| 1559 | +#define SQLITE_CORRUPT_INDEX (SQLITE_CORRUPT | (3<<8)) |
| 1558 | 1560 | #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8)) |
| 1559 | 1561 | #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8)) |
| 1560 | 1562 | #define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8)) |
| 1561 | 1563 | #define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8)) |
| 1562 | 1564 | #define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8)) |
| | @@ -6530,11 +6532,11 @@ |
| 6530 | 6532 | ** when first called if N is less than or equal to zero or if a memory |
| 6531 | 6533 | ** allocate error occurs. |
| 6532 | 6534 | ** |
| 6533 | 6535 | ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is |
| 6534 | 6536 | ** determined by the N parameter on first successful call. Changing the |
| 6535 | | -** value of N in any subsequents call to sqlite3_aggregate_context() within |
| 6537 | +** value of N in any subsequent call to sqlite3_aggregate_context() within |
| 6536 | 6538 | ** the same aggregate function instance will not resize the memory |
| 6537 | 6539 | ** allocation.)^ Within the xFinal callback, it is customary to set |
| 6538 | 6540 | ** N=0 in calls to sqlite3_aggregate_context(C,N) so that no |
| 6539 | 6541 | ** pointless memory allocations occur. |
| 6540 | 6542 | ** |
| | @@ -14535,11 +14537,10 @@ |
| 14535 | 14537 | typedef struct BusyHandler BusyHandler; |
| 14536 | 14538 | struct BusyHandler { |
| 14537 | 14539 | int (*xBusyHandler)(void *,int); /* The busy callback */ |
| 14538 | 14540 | void *pBusyArg; /* First arg to busy callback */ |
| 14539 | 14541 | int nBusy; /* Incremented with each busy call */ |
| 14540 | | - u8 bExtraFileArg; /* Include sqlite3_file as callback arg */ |
| 14541 | 14542 | }; |
| 14542 | 14543 | |
| 14543 | 14544 | /* |
| 14544 | 14545 | ** Name of the master database table. The master database table |
| 14545 | 14546 | ** is a special table that holds the names and attributes of all |
| | @@ -15918,17 +15919,25 @@ |
| 15918 | 15919 | SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager); |
| 15919 | 15920 | SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager); |
| 15920 | 15921 | SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen); |
| 15921 | 15922 | SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*); |
| 15922 | 15923 | # ifdef SQLITE_ENABLE_SNAPSHOT |
| 15923 | | -SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot); |
| 15924 | | -SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot); |
| 15924 | +SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager*, sqlite3_snapshot **ppSnapshot); |
| 15925 | +SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager*, sqlite3_snapshot *pSnapshot); |
| 15925 | 15926 | SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager); |
| 15926 | 15927 | SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot); |
| 15927 | 15928 | SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager); |
| 15928 | 15929 | # endif |
| 15929 | 15930 | #endif |
| 15931 | + |
| 15932 | +#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_ENABLE_SETLK_TIMEOUT) |
| 15933 | +SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager*, int); |
| 15934 | +SQLITE_PRIVATE void sqlite3PagerWalDb(Pager*, sqlite3*); |
| 15935 | +#else |
| 15936 | +# define sqlite3PagerWalWriteLock(y,z) SQLITE_OK |
| 15937 | +# define sqlite3PagerWalDb(x,y) |
| 15938 | +#endif |
| 15930 | 15939 | |
| 15931 | 15940 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 15932 | 15941 | SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno); |
| 15933 | 15942 | #endif |
| 15934 | 15943 | |
| | @@ -15951,15 +15960,10 @@ |
| 15951 | 15960 | SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*); |
| 15952 | 15961 | SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*); |
| 15953 | 15962 | SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *); |
| 15954 | 15963 | SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*); |
| 15955 | 15964 | SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *); |
| 15956 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 15957 | | -SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager); |
| 15958 | | -#else |
| 15959 | | -# define sqlite3PagerResetLockTimeout(X) |
| 15960 | | -#endif |
| 15961 | 15965 | |
| 15962 | 15966 | /* Functions used to truncate the database file. */ |
| 15963 | 15967 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno); |
| 15964 | 15968 | |
| 15965 | 15969 | SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16); |
| | @@ -17115,11 +17119,11 @@ |
| 17115 | 17119 | #define SQLITE_FUNC_EPHEM 0x0010 /* Ephemeral. Delete with VDBE */ |
| 17116 | 17120 | #define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/ |
| 17117 | 17121 | #define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */ |
| 17118 | 17122 | #define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */ |
| 17119 | 17123 | #define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */ |
| 17120 | | -#define SQLITE_FUNC_COALESCE 0x0200 /* Built-in coalesce() or ifnull() */ |
| 17124 | +/* 0x0200 -- available for reuse */ |
| 17121 | 17125 | #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */ |
| 17122 | 17126 | #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */ |
| 17123 | 17127 | #define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */ |
| 17124 | 17128 | #define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a |
| 17125 | 17129 | ** single query - might change over time */ |
| | @@ -17136,10 +17140,11 @@ |
| 17136 | 17140 | #define INLINEFUNC_coalesce 0 |
| 17137 | 17141 | #define INLINEFUNC_implies_nonnull_row 1 |
| 17138 | 17142 | #define INLINEFUNC_expr_implies_expr 2 |
| 17139 | 17143 | #define INLINEFUNC_expr_compare 3 |
| 17140 | 17144 | #define INLINEFUNC_affinity 4 |
| 17145 | +#define INLINEFUNC_iif 5 |
| 17141 | 17146 | #define INLINEFUNC_unlikely 99 /* Default case */ |
| 17142 | 17147 | |
| 17143 | 17148 | /* |
| 17144 | 17149 | ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are |
| 17145 | 17150 | ** used to create the initializers for the FuncDef structures. |
| | @@ -17836,11 +17841,11 @@ |
| 17836 | 17841 | /* |
| 17837 | 17842 | ** An instance of this structure contains information needed to generate |
| 17838 | 17843 | ** code for a SELECT that contains aggregate functions. |
| 17839 | 17844 | ** |
| 17840 | 17845 | ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a |
| 17841 | | -** pointer to this structure. The Expr.iColumn field is the index in |
| 17846 | +** pointer to this structure. The Expr.iAgg field is the index in |
| 17842 | 17847 | ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate |
| 17843 | 17848 | ** code for that node. |
| 17844 | 17849 | ** |
| 17845 | 17850 | ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the |
| 17846 | 17851 | ** original Select structure that describes the SELECT statement. These |
| | @@ -19075,10 +19080,13 @@ |
| 19075 | 19080 | SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*); |
| 19076 | 19081 | SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*); |
| 19077 | 19082 | SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker*, Expr*); |
| 19078 | 19083 | SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker*, Select*); |
| 19079 | 19084 | SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker*, Select*); |
| 19085 | +SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker*,Select*); |
| 19086 | +SQLITE_PRIVATE void sqlite3WalkerDepthDecrease(Walker*,Select*); |
| 19087 | + |
| 19080 | 19088 | #ifdef SQLITE_DEBUG |
| 19081 | 19089 | SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker*, Select*); |
| 19082 | 19090 | #endif |
| 19083 | 19091 | |
| 19084 | 19092 | /* |
| | @@ -19935,11 +19943,11 @@ |
| 19935 | 19943 | SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse*, Expr*); |
| 19936 | 19944 | SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse*, ExprList*); |
| 19937 | 19945 | SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*); |
| 19938 | 19946 | SQLITE_PRIVATE char sqlite3AffinityType(const char*, Column*); |
| 19939 | 19947 | SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*); |
| 19940 | | -SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*); |
| 19948 | +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*); |
| 19941 | 19949 | SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*); |
| 19942 | 19950 | SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *); |
| 19943 | 19951 | SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB); |
| 19944 | 19952 | SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*); |
| 19945 | 19953 | SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*); |
| | @@ -20060,12 +20068,14 @@ |
| 20060 | 20068 | # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0) |
| 20061 | 20069 | #endif |
| 20062 | 20070 | SQLITE_PRIVATE int sqlite3ReadOnlyShadowTables(sqlite3 *db); |
| 20063 | 20071 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 20064 | 20072 | SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName); |
| 20073 | +SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*); |
| 20065 | 20074 | #else |
| 20066 | 20075 | # define sqlite3ShadowTableName(A,B) 0 |
| 20076 | +# define sqlite3IsShadowTableOf(A,B,C) 0 |
| 20067 | 20077 | #endif |
| 20068 | 20078 | SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse*,Module*); |
| 20069 | 20079 | SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3*,Module*); |
| 20070 | 20080 | SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*); |
| 20071 | 20081 | SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int); |
| | @@ -27730,14 +27740,16 @@ |
| 27730 | 27740 | if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= |
| 27731 | 27741 | mem0.alarmThreshold-nDiff ){ |
| 27732 | 27742 | sqlite3MallocAlarm(nDiff); |
| 27733 | 27743 | } |
| 27734 | 27744 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 27745 | +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 27735 | 27746 | if( pNew==0 && mem0.alarmThreshold>0 ){ |
| 27736 | 27747 | sqlite3MallocAlarm((int)nBytes); |
| 27737 | 27748 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 27738 | 27749 | } |
| 27750 | +#endif |
| 27739 | 27751 | if( pNew ){ |
| 27740 | 27752 | nNew = sqlite3MallocSize(pNew); |
| 27741 | 27753 | sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); |
| 27742 | 27754 | } |
| 27743 | 27755 | sqlite3_mutex_leave(mem0.mutex); |
| | @@ -27918,11 +27930,11 @@ |
| 27918 | 27930 | } |
| 27919 | 27931 | }else{ |
| 27920 | 27932 | assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 27921 | 27933 | assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 27922 | 27934 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 27923 | | - pNew = sqlite3_realloc64(p, n); |
| 27935 | + pNew = sqlite3Realloc(p, n); |
| 27924 | 27936 | if( !pNew ){ |
| 27925 | 27937 | sqlite3OomFault(db); |
| 27926 | 27938 | } |
| 27927 | 27939 | sqlite3MemdebugSetType(pNew, |
| 27928 | 27940 | (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); |
| | @@ -28265,10 +28277,17 @@ |
| 28265 | 28277 | #ifndef SQLITE_PRINT_BUF_SIZE |
| 28266 | 28278 | # define SQLITE_PRINT_BUF_SIZE 70 |
| 28267 | 28279 | #endif |
| 28268 | 28280 | #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ |
| 28269 | 28281 | |
| 28282 | +/* |
| 28283 | +** Hard limit on the precision of floating-point conversions. |
| 28284 | +*/ |
| 28285 | +#ifndef SQLITE_PRINTF_PRECISION_LIMIT |
| 28286 | +# define SQLITE_FP_PRECISION_LIMIT 100000000 |
| 28287 | +#endif |
| 28288 | + |
| 28270 | 28289 | /* |
| 28271 | 28290 | ** Render a string given by "fmt" into the StrAccum object. |
| 28272 | 28291 | */ |
| 28273 | 28292 | SQLITE_API void sqlite3_str_vappendf( |
| 28274 | 28293 | sqlite3_str *pAccum, /* Accumulate results here */ |
| | @@ -28465,10 +28484,12 @@ |
| 28465 | 28484 | ** precision The specified precision. The default |
| 28466 | 28485 | ** is -1. |
| 28467 | 28486 | ** xtype The class of the conversion. |
| 28468 | 28487 | ** infop Pointer to the appropriate info struct. |
| 28469 | 28488 | */ |
| 28489 | + assert( width>=0 ); |
| 28490 | + assert( precision>=(-1) ); |
| 28470 | 28491 | switch( xtype ){ |
| 28471 | 28492 | case etPOINTER: |
| 28472 | 28493 | flag_long = sizeof(char*)==sizeof(i64) ? 2 : |
| 28473 | 28494 | sizeof(char*)==sizeof(long int) ? 1 : 0; |
| 28474 | 28495 | /* Fall through into the next case */ |
| | @@ -28586,10 +28607,15 @@ |
| 28586 | 28607 | } |
| 28587 | 28608 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 28588 | 28609 | length = 0; |
| 28589 | 28610 | #else |
| 28590 | 28611 | if( precision<0 ) precision = 6; /* Set default precision */ |
| 28612 | +#ifdef SQLITE_FP_PRECISION_LIMIT |
| 28613 | + if( precision>SQLITE_FP_PRECISION_LIMIT ){ |
| 28614 | + precision = SQLITE_FP_PRECISION_LIMIT; |
| 28615 | + } |
| 28616 | +#endif |
| 28591 | 28617 | if( realvalue<0.0 ){ |
| 28592 | 28618 | realvalue = -realvalue; |
| 28593 | 28619 | prefix = '-'; |
| 28594 | 28620 | }else{ |
| 28595 | 28621 | prefix = flag_prefix; |
| | @@ -28868,11 +28894,11 @@ |
| 28868 | 28894 | }else{ |
| 28869 | 28895 | escarg = va_arg(ap,char*); |
| 28870 | 28896 | } |
| 28871 | 28897 | isnull = escarg==0; |
| 28872 | 28898 | if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); |
| 28873 | | - /* For %q, %Q, and %w, the precision is the number of byte (or |
| 28899 | + /* For %q, %Q, and %w, the precision is the number of bytes (or |
| 28874 | 28900 | ** characters if the ! flags is present) to use from the input. |
| 28875 | 28901 | ** Because of the extra quoting characters inserted, the number |
| 28876 | 28902 | ** of output characters may be larger than the precision. |
| 28877 | 28903 | */ |
| 28878 | 28904 | k = precision; |
| | @@ -28995,11 +29021,11 @@ |
| 28995 | 29021 | p->nAlloc = (int)szNew; |
| 28996 | 29022 | } |
| 28997 | 29023 | if( p->db ){ |
| 28998 | 29024 | zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); |
| 28999 | 29025 | }else{ |
| 29000 | | - zNew = sqlite3_realloc64(zOld, p->nAlloc); |
| 29026 | + zNew = sqlite3Realloc(zOld, p->nAlloc); |
| 29001 | 29027 | } |
| 29002 | 29028 | if( zNew ){ |
| 29003 | 29029 | assert( p->zText!=0 || p->nChar==0 ); |
| 29004 | 29030 | if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
| 29005 | 29031 | p->zText = zNew; |
| | @@ -29337,11 +29363,11 @@ |
| 29337 | 29363 | ** and segfaults if you give it a long long int. |
| 29338 | 29364 | */ |
| 29339 | 29365 | SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){ |
| 29340 | 29366 | va_list ap; |
| 29341 | 29367 | StrAccum acc; |
| 29342 | | - char zBuf[500]; |
| 29368 | + char zBuf[SQLITE_PRINT_BUF_SIZE*10]; |
| 29343 | 29369 | sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| 29344 | 29370 | va_start(ap,zFormat); |
| 29345 | 29371 | sqlite3_str_vappendf(&acc, zFormat, ap); |
| 29346 | 29372 | va_end(ap); |
| 29347 | 29373 | sqlite3StrAccumFinish(&acc); |
| | @@ -29953,12 +29979,13 @@ |
| 29953 | 29979 | #else |
| 29954 | 29980 | pWin = 0; |
| 29955 | 29981 | #endif |
| 29956 | 29982 | } |
| 29957 | 29983 | if( pExpr->op==TK_AGG_FUNCTION ){ |
| 29958 | | - sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s", |
| 29959 | | - pExpr->op2, pExpr->u.zToken, zFlgs); |
| 29984 | + sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s iAgg=%d agg=%p", |
| 29985 | + pExpr->op2, pExpr->u.zToken, zFlgs, |
| 29986 | + pExpr->iAgg, pExpr->pAggInfo); |
| 29960 | 29987 | }else if( pExpr->op2!=0 ){ |
| 29961 | 29988 | const char *zOp2; |
| 29962 | 29989 | char zBuf[8]; |
| 29963 | 29990 | sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2); |
| 29964 | 29991 | zOp2 = zBuf; |
| | @@ -30847,10 +30874,11 @@ |
| 30847 | 30874 | /* UTF-16 Little-endian -> UTF-8 */ |
| 30848 | 30875 | while( zIn<zTerm ){ |
| 30849 | 30876 | c = *(zIn++); |
| 30850 | 30877 | c += (*(zIn++))<<8; |
| 30851 | 30878 | if( c>=0xd800 && c<0xe000 ){ |
| 30879 | +#ifdef SQLITE_REPLACE_INVALID_UTF |
| 30852 | 30880 | if( c>=0xdc00 || zIn>=zTerm ){ |
| 30853 | 30881 | c = 0xfffd; |
| 30854 | 30882 | }else{ |
| 30855 | 30883 | int c2 = *(zIn++); |
| 30856 | 30884 | c2 += (*(zIn++))<<8; |
| | @@ -30859,19 +30887,27 @@ |
| 30859 | 30887 | c = 0xfffd; |
| 30860 | 30888 | }else{ |
| 30861 | 30889 | c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000; |
| 30862 | 30890 | } |
| 30863 | 30891 | } |
| 30892 | +#else |
| 30893 | + if( zIn<zTerm ){ |
| 30894 | + int c2 = (*zIn++); |
| 30895 | + c2 += ((*zIn++)<<8); |
| 30896 | + c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); |
| 30897 | + } |
| 30898 | +#endif |
| 30864 | 30899 | } |
| 30865 | 30900 | WRITE_UTF8(z, c); |
| 30866 | 30901 | } |
| 30867 | 30902 | }else{ |
| 30868 | 30903 | /* UTF-16 Big-endian -> UTF-8 */ |
| 30869 | 30904 | while( zIn<zTerm ){ |
| 30870 | 30905 | c = (*(zIn++))<<8; |
| 30871 | 30906 | c += *(zIn++); |
| 30872 | 30907 | if( c>=0xd800 && c<0xe000 ){ |
| 30908 | +#ifdef SQLITE_REPLACE_INVALID_UTF |
| 30873 | 30909 | if( c>=0xdc00 || zIn>=zTerm ){ |
| 30874 | 30910 | c = 0xfffd; |
| 30875 | 30911 | }else{ |
| 30876 | 30912 | int c2 = (*(zIn++))<<8; |
| 30877 | 30913 | c2 += *(zIn++); |
| | @@ -30880,10 +30916,17 @@ |
| 30880 | 30916 | c = 0xfffd; |
| 30881 | 30917 | }else{ |
| 30882 | 30918 | c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000; |
| 30883 | 30919 | } |
| 30884 | 30920 | } |
| 30921 | +#else |
| 30922 | + if( zIn<zTerm ){ |
| 30923 | + int c2 = ((*zIn++)<<8); |
| 30924 | + c2 += (*zIn++); |
| 30925 | + c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); |
| 30926 | + } |
| 30927 | +#endif |
| 30885 | 30928 | } |
| 30886 | 30929 | WRITE_UTF8(z, c); |
| 30887 | 30930 | } |
| 30888 | 30931 | } |
| 30889 | 30932 | pMem->n = (int)(z - zOut); |
| | @@ -34971,20 +35014,21 @@ |
| 34971 | 35014 | static int osSetPosixAdvisoryLock( |
| 34972 | 35015 | int h, /* The file descriptor on which to take the lock */ |
| 34973 | 35016 | struct flock *pLock, /* The description of the lock */ |
| 34974 | 35017 | unixFile *pFile /* Structure holding timeout value */ |
| 34975 | 35018 | ){ |
| 35019 | + int tm = pFile->iBusyTimeout; |
| 34976 | 35020 | int rc = osFcntl(h,F_SETLK,pLock); |
| 34977 | | - while( rc<0 && pFile->iBusyTimeout>0 ){ |
| 35021 | + while( rc<0 && tm>0 ){ |
| 34978 | 35022 | /* On systems that support some kind of blocking file lock with a timeout, |
| 34979 | 35023 | ** make appropriate changes here to invoke that blocking file lock. On |
| 34980 | 35024 | ** generic posix, however, there is no such API. So we simply try the |
| 34981 | 35025 | ** lock once every millisecond until either the timeout expires, or until |
| 34982 | 35026 | ** the lock is obtained. */ |
| 34983 | 35027 | usleep(1000); |
| 34984 | 35028 | rc = osFcntl(h,F_SETLK,pLock); |
| 34985 | | - pFile->iBusyTimeout--; |
| 35029 | + tm--; |
| 34986 | 35030 | } |
| 34987 | 35031 | return rc; |
| 34988 | 35032 | } |
| 34989 | 35033 | #endif /* SQLITE_ENABLE_SETLK_TIMEOUT */ |
| 34990 | 35034 | |
| | @@ -37722,17 +37766,24 @@ |
| 37722 | 37766 | |
| 37723 | 37767 | /* Locks are within range */ |
| 37724 | 37768 | assert( n>=1 && n<=SQLITE_SHM_NLOCK ); |
| 37725 | 37769 | |
| 37726 | 37770 | if( pShmNode->hShm>=0 ){ |
| 37771 | + int res; |
| 37727 | 37772 | /* Initialize the locking parameters */ |
| 37728 | 37773 | f.l_type = lockType; |
| 37729 | 37774 | f.l_whence = SEEK_SET; |
| 37730 | 37775 | f.l_start = ofst; |
| 37731 | 37776 | f.l_len = n; |
| 37732 | | - rc = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile); |
| 37733 | | - rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 37777 | + res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile); |
| 37778 | + if( res==-1 ){ |
| 37779 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 37780 | + rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY); |
| 37781 | +#else |
| 37782 | + rc = SQLITE_BUSY; |
| 37783 | +#endif |
| 37784 | + } |
| 37734 | 37785 | } |
| 37735 | 37786 | |
| 37736 | 37787 | /* Update the global lock state and do debug tracing */ |
| 37737 | 37788 | #ifdef SQLITE_DEBUG |
| 37738 | 37789 | { u16 mask; |
| | @@ -38225,26 +38276,27 @@ |
| 38225 | 38276 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 38226 | 38277 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
| 38227 | 38278 | assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 38228 | 38279 | assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); |
| 38229 | 38280 | |
| 38230 | | - /* Check that, if this to be a blocking lock, that locks have been |
| 38231 | | - ** obtained in the following order. |
| 38281 | + /* Check that, if this to be a blocking lock, no locks that occur later |
| 38282 | + ** in the following list than the lock being obtained are already held: |
| 38232 | 38283 | ** |
| 38233 | 38284 | ** 1. Checkpointer lock (ofst==1). |
| 38234 | | - ** 2. Recover lock (ofst==2). |
| 38285 | + ** 2. Write lock (ofst==0). |
| 38235 | 38286 | ** 3. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK). |
| 38236 | | - ** 4. Write lock (ofst==0). |
| 38237 | 38287 | ** |
| 38238 | 38288 | ** In other words, if this is a blocking lock, none of the locks that |
| 38239 | 38289 | ** occur later in the above list than the lock being obtained may be |
| 38240 | 38290 | ** held. */ |
| 38241 | 38291 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 38242 | | - assert( pDbFd->iBusyTimeout==0 |
| 38243 | | - || (flags & SQLITE_SHM_UNLOCK) || ofst==0 |
| 38244 | | - || ((p->exclMask|p->sharedMask)&~((1<<ofst)-2))==0 |
| 38245 | | - ); |
| 38292 | + assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || ( |
| 38293 | + (ofst!=2) /* not RECOVER */ |
| 38294 | + && (ofst!=1 || (p->exclMask|p->sharedMask)==0) |
| 38295 | + && (ofst!=0 || (p->exclMask|p->sharedMask)<3) |
| 38296 | + && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst)) |
| 38297 | + )); |
| 38246 | 38298 | #endif |
| 38247 | 38299 | |
| 38248 | 38300 | mask = (1<<(ofst+n)) - (1<<ofst); |
| 38249 | 38301 | assert( n>1 || mask==(1<<ofst) ); |
| 38250 | 38302 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
| | @@ -45033,10 +45085,11 @@ |
| 45033 | 45085 | } |
| 45034 | 45086 | |
| 45035 | 45087 | /* Forward references to VFS helper methods used for temporary files */ |
| 45036 | 45088 | static int winGetTempname(sqlite3_vfs *, char **); |
| 45037 | 45089 | static int winIsDir(const void *); |
| 45090 | +static BOOL winIsLongPathPrefix(const char *); |
| 45038 | 45091 | static BOOL winIsDriveLetterAndColon(const char *); |
| 45039 | 45092 | |
| 45040 | 45093 | /* |
| 45041 | 45094 | ** Control and query of the open file handle. |
| 45042 | 45095 | */ |
| | @@ -46802,11 +46855,13 @@ |
| 46802 | 46855 | pFile->pVfs = pVfs; |
| 46803 | 46856 | pFile->h = h; |
| 46804 | 46857 | if( isReadonly ){ |
| 46805 | 46858 | pFile->ctrlFlags |= WINFILE_RDONLY; |
| 46806 | 46859 | } |
| 46807 | | - if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
| 46860 | + if( (flags & SQLITE_OPEN_MAIN_DB) |
| 46861 | + && sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) |
| 46862 | + ){ |
| 46808 | 46863 | pFile->ctrlFlags |= WINFILE_PSOW; |
| 46809 | 46864 | } |
| 46810 | 46865 | pFile->lastErrno = NO_ERROR; |
| 46811 | 46866 | pFile->zPath = zName; |
| 46812 | 46867 | #if SQLITE_MAX_MMAP_SIZE>0 |
| | @@ -47011,10 +47066,21 @@ |
| 47011 | 47066 | *pResOut = rc; |
| 47012 | 47067 | OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n", |
| 47013 | 47068 | zFilename, pResOut, *pResOut)); |
| 47014 | 47069 | return SQLITE_OK; |
| 47015 | 47070 | } |
| 47071 | + |
| 47072 | +/* |
| 47073 | +** Returns non-zero if the specified path name starts with the "long path" |
| 47074 | +** prefix. |
| 47075 | +*/ |
| 47076 | +static BOOL winIsLongPathPrefix( |
| 47077 | + const char *zPathname |
| 47078 | +){ |
| 47079 | + return ( zPathname[0]=='\\' && zPathname[1]=='\\' |
| 47080 | + && zPathname[2]=='?' && zPathname[3]=='\\' ); |
| 47081 | +} |
| 47016 | 47082 | |
| 47017 | 47083 | /* |
| 47018 | 47084 | ** Returns non-zero if the specified path name starts with a drive letter |
| 47019 | 47085 | ** followed by a colon character. |
| 47020 | 47086 | */ |
| | @@ -47076,14 +47142,15 @@ |
| 47076 | 47142 | DWORD nByte; |
| 47077 | 47143 | void *zConverted; |
| 47078 | 47144 | char *zOut; |
| 47079 | 47145 | #endif |
| 47080 | 47146 | |
| 47081 | | - /* If this path name begins with "/X:", where "X" is any alphabetic |
| 47082 | | - ** character, discard the initial "/" from the pathname. |
| 47147 | + /* If this path name begins with "/X:" or "\\?\", where "X" is any |
| 47148 | + ** alphabetic character, discard the initial "/" from the pathname. |
| 47083 | 47149 | */ |
| 47084 | | - if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){ |
| 47150 | + if( zRelative[0]=='/' && (winIsDriveLetterAndColon(zRelative+1) |
| 47151 | + || winIsLongPathPrefix(zRelative+1)) ){ |
| 47085 | 47152 | zRelative++; |
| 47086 | 47153 | } |
| 47087 | 47154 | |
| 47088 | 47155 | #if defined(__CYGWIN__) |
| 47089 | 47156 | SimulateIOError( return SQLITE_ERROR ); |
| | @@ -47835,11 +47902,11 @@ |
| 47835 | 47902 | if( newSz>p->szMax ){ |
| 47836 | 47903 | return SQLITE_FULL; |
| 47837 | 47904 | } |
| 47838 | 47905 | newSz *= 2; |
| 47839 | 47906 | if( newSz>p->szMax ) newSz = p->szMax; |
| 47840 | | - pNew = sqlite3_realloc64(p->aData, newSz); |
| 47907 | + pNew = sqlite3Realloc(p->aData, newSz); |
| 47841 | 47908 | if( pNew==0 ) return SQLITE_NOMEM; |
| 47842 | 47909 | p->aData = pNew; |
| 47843 | 47910 | p->szAlloc = newSz; |
| 47844 | 47911 | return SQLITE_OK; |
| 47845 | 47912 | } |
| | @@ -48282,14 +48349,15 @@ |
| 48282 | 48349 | */ |
| 48283 | 48350 | SQLITE_PRIVATE int sqlite3MemdbInit(void){ |
| 48284 | 48351 | sqlite3_vfs *pLower = sqlite3_vfs_find(0); |
| 48285 | 48352 | int sz = pLower->szOsFile; |
| 48286 | 48353 | memdb_vfs.pAppData = pLower; |
| 48287 | | - /* In all known configurations of SQLite, the size of a default |
| 48288 | | - ** sqlite3_file is greater than the size of a memdb sqlite3_file. |
| 48289 | | - ** Should that ever change, remove the following NEVER() */ |
| 48290 | | - if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile); |
| 48354 | + /* The following conditional can only be true when compiled for |
| 48355 | + ** Windows x86 and SQLITE_MAX_MMAP_SIZE=0. We always leave |
| 48356 | + ** it in, to be safe, but it is marked as NO_TEST since there |
| 48357 | + ** is no way to reach it under most builds. */ |
| 48358 | + if( sz<sizeof(MemFile) ) sz = sizeof(MemFile); /*NO_TEST*/ |
| 48291 | 48359 | memdb_vfs.szOsFile = sz; |
| 48292 | 48360 | return sqlite3_vfs_register(&memdb_vfs, 0); |
| 48293 | 48361 | } |
| 48294 | 48362 | #endif /* SQLITE_ENABLE_DESERIALIZE */ |
| 48295 | 48363 | |
| | @@ -51548,10 +51616,15 @@ |
| 51548 | 51616 | SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal); |
| 51549 | 51617 | #endif |
| 51550 | 51618 | |
| 51551 | 51619 | /* Return the sqlite3_file object for the WAL file */ |
| 51552 | 51620 | SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal); |
| 51621 | + |
| 51622 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 51623 | +SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock); |
| 51624 | +SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db); |
| 51625 | +#endif |
| 51553 | 51626 | |
| 51554 | 51627 | #endif /* ifndef SQLITE_OMIT_WAL */ |
| 51555 | 51628 | #endif /* SQLITE_WAL_H */ |
| 51556 | 51629 | |
| 51557 | 51630 | /************** End of wal.h *************************************************/ |
| | @@ -57238,11 +57311,10 @@ |
| 57238 | 57311 | Pager *pPager; |
| 57239 | 57312 | assert( pPg!=0 ); |
| 57240 | 57313 | assert( pPg->pgno==1 ); |
| 57241 | 57314 | assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */ |
| 57242 | 57315 | pPager = pPg->pPager; |
| 57243 | | - sqlite3PagerResetLockTimeout(pPager); |
| 57244 | 57316 | sqlite3PcacheRelease(pPg); |
| 57245 | 57317 | pagerUnlockIfUnused(pPager); |
| 57246 | 57318 | } |
| 57247 | 57319 | |
| 57248 | 57320 | /* |
| | @@ -58531,20 +58603,10 @@ |
| 58531 | 58603 | */ |
| 58532 | 58604 | SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){ |
| 58533 | 58605 | return pPager->fd; |
| 58534 | 58606 | } |
| 58535 | 58607 | |
| 58536 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 58537 | | -/* |
| 58538 | | -** Reset the lock timeout for pager. |
| 58539 | | -*/ |
| 58540 | | -SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager){ |
| 58541 | | - int x = 0; |
| 58542 | | - sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_LOCK_TIMEOUT, &x); |
| 58543 | | -} |
| 58544 | | -#endif |
| 58545 | | - |
| 58546 | 58608 | /* |
| 58547 | 58609 | ** Return the file handle for the journal file (if it exists). |
| 58548 | 58610 | ** This will be either the rollback journal or the WAL file. |
| 58549 | 58611 | */ |
| 58550 | 58612 | SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){ |
| | @@ -58954,11 +59016,10 @@ |
| 58954 | 59016 | (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler), |
| 58955 | 59017 | pPager->pBusyHandlerArg, |
| 58956 | 59018 | pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace, |
| 58957 | 59019 | pnLog, pnCkpt |
| 58958 | 59020 | ); |
| 58959 | | - sqlite3PagerResetLockTimeout(pPager); |
| 58960 | 59021 | } |
| 58961 | 59022 | return rc; |
| 58962 | 59023 | } |
| 58963 | 59024 | |
| 58964 | 59025 | SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){ |
| | @@ -59119,11 +59180,35 @@ |
| 59119 | 59180 | } |
| 59120 | 59181 | } |
| 59121 | 59182 | return rc; |
| 59122 | 59183 | } |
| 59123 | 59184 | |
| 59185 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 59186 | +/* |
| 59187 | +** If pager pPager is a wal-mode database not in exclusive locking mode, |
| 59188 | +** invoke the sqlite3WalWriteLock() function on the associated Wal object |
| 59189 | +** with the same db and bLock parameters as were passed to this function. |
| 59190 | +** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise. |
| 59191 | +*/ |
| 59192 | +SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager *pPager, int bLock){ |
| 59193 | + int rc = SQLITE_OK; |
| 59194 | + if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){ |
| 59195 | + rc = sqlite3WalWriteLock(pPager->pWal, bLock); |
| 59196 | + } |
| 59197 | + return rc; |
| 59198 | +} |
| 59124 | 59199 | |
| 59200 | +/* |
| 59201 | +** Set the database handle used by the wal layer to determine if |
| 59202 | +** blocking locks are required. |
| 59203 | +*/ |
| 59204 | +SQLITE_PRIVATE void sqlite3PagerWalDb(Pager *pPager, sqlite3 *db){ |
| 59205 | + if( pagerUseWal(pPager) ){ |
| 59206 | + sqlite3WalDb(pPager->pWal, db); |
| 59207 | + } |
| 59208 | +} |
| 59209 | +#endif |
| 59125 | 59210 | |
| 59126 | 59211 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 59127 | 59212 | /* |
| 59128 | 59213 | ** If this is a WAL database, obtain a snapshot handle for the snapshot |
| 59129 | 59214 | ** currently open. Otherwise, return an error. |
| | @@ -59139,11 +59224,14 @@ |
| 59139 | 59224 | /* |
| 59140 | 59225 | ** If this is a WAL database, store a pointer to pSnapshot. Next time a |
| 59141 | 59226 | ** read transaction is opened, attempt to read from the snapshot it |
| 59142 | 59227 | ** identifies. If this is not a WAL database, return an error. |
| 59143 | 59228 | */ |
| 59144 | | -SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){ |
| 59229 | +SQLITE_PRIVATE int sqlite3PagerSnapshotOpen( |
| 59230 | + Pager *pPager, |
| 59231 | + sqlite3_snapshot *pSnapshot |
| 59232 | +){ |
| 59145 | 59233 | int rc = SQLITE_OK; |
| 59146 | 59234 | if( pPager->pWal ){ |
| 59147 | 59235 | sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot); |
| 59148 | 59236 | }else{ |
| 59149 | 59237 | rc = SQLITE_ERROR; |
| | @@ -59684,10 +59772,13 @@ |
| 59684 | 59772 | u8 lockError; /* True if a locking error has occurred */ |
| 59685 | 59773 | #endif |
| 59686 | 59774 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 59687 | 59775 | WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */ |
| 59688 | 59776 | #endif |
| 59777 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 59778 | + sqlite3 *db; |
| 59779 | +#endif |
| 59689 | 59780 | }; |
| 59690 | 59781 | |
| 59691 | 59782 | /* |
| 59692 | 59783 | ** Candidate values for Wal.exclusiveMode. |
| 59693 | 59784 | */ |
| | @@ -59782,11 +59873,11 @@ |
| 59782 | 59873 | |
| 59783 | 59874 | /* Enlarge the pWal->apWiData[] array if required */ |
| 59784 | 59875 | if( pWal->nWiData<=iPage ){ |
| 59785 | 59876 | sqlite3_int64 nByte = sizeof(u32*)*(iPage+1); |
| 59786 | 59877 | volatile u32 **apNew; |
| 59787 | | - apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte); |
| 59878 | + apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte); |
| 59788 | 59879 | if( !apNew ){ |
| 59789 | 59880 | *ppPage = 0; |
| 59790 | 59881 | return SQLITE_NOMEM_BKPT; |
| 59791 | 59882 | } |
| 59792 | 59883 | memset((void*)&apNew[pWal->nWiData], 0, |
| | @@ -59903,29 +59994,47 @@ |
| 59903 | 59994 | |
| 59904 | 59995 | aOut[0] = s1; |
| 59905 | 59996 | aOut[1] = s2; |
| 59906 | 59997 | } |
| 59907 | 59998 | |
| 59999 | +/* |
| 60000 | +** If there is the possibility of concurrent access to the SHM file |
| 60001 | +** from multiple threads and/or processes, then do a memory barrier. |
| 60002 | +*/ |
| 59908 | 60003 | static void walShmBarrier(Wal *pWal){ |
| 59909 | 60004 | if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){ |
| 59910 | 60005 | sqlite3OsShmBarrier(pWal->pDbFd); |
| 59911 | 60006 | } |
| 59912 | 60007 | } |
| 59913 | 60008 | |
| 60009 | +/* |
| 60010 | +** Add the SQLITE_NO_TSAN as part of the return-type of a function |
| 60011 | +** definition as a hint that the function contains constructs that |
| 60012 | +** might give false-positive TSAN warnings. |
| 60013 | +** |
| 60014 | +** See tag-20200519-1. |
| 60015 | +*/ |
| 60016 | +#if defined(__clang__) && !defined(SQLITE_NO_TSAN) |
| 60017 | +# define SQLITE_NO_TSAN __attribute__((no_sanitize_thread)) |
| 60018 | +#else |
| 60019 | +# define SQLITE_NO_TSAN |
| 60020 | +#endif |
| 60021 | + |
| 59914 | 60022 | /* |
| 59915 | 60023 | ** Write the header information in pWal->hdr into the wal-index. |
| 59916 | 60024 | ** |
| 59917 | 60025 | ** The checksum on pWal->hdr is updated before it is written. |
| 59918 | 60026 | */ |
| 59919 | | -static void walIndexWriteHdr(Wal *pWal){ |
| 60027 | +static SQLITE_NO_TSAN void walIndexWriteHdr(Wal *pWal){ |
| 59920 | 60028 | volatile WalIndexHdr *aHdr = walIndexHdr(pWal); |
| 59921 | 60029 | const int nCksum = offsetof(WalIndexHdr, aCksum); |
| 59922 | 60030 | |
| 59923 | 60031 | assert( pWal->writeLock ); |
| 59924 | 60032 | pWal->hdr.isInit = 1; |
| 59925 | 60033 | pWal->hdr.iVersion = WALINDEX_MAX_VERSION; |
| 59926 | 60034 | walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum); |
| 60035 | + /* Possible TSAN false-positive. See tag-20200519-1 */ |
| 59927 | 60036 | memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr)); |
| 59928 | 60037 | walShmBarrier(pWal); |
| 59929 | 60038 | memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr)); |
| 59930 | 60039 | } |
| 59931 | 60040 | |
| | @@ -60057,11 +60166,11 @@ |
| 60057 | 60166 | if( pWal->exclusiveMode ) return SQLITE_OK; |
| 60058 | 60167 | rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1, |
| 60059 | 60168 | SQLITE_SHM_LOCK | SQLITE_SHM_SHARED); |
| 60060 | 60169 | WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal, |
| 60061 | 60170 | walLockName(lockIdx), rc ? "failed" : "ok")); |
| 60062 | | - VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); ) |
| 60171 | + VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); ) |
| 60063 | 60172 | return rc; |
| 60064 | 60173 | } |
| 60065 | 60174 | static void walUnlockShared(Wal *pWal, int lockIdx){ |
| 60066 | 60175 | if( pWal->exclusiveMode ) return; |
| 60067 | 60176 | (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1, |
| | @@ -60073,11 +60182,11 @@ |
| 60073 | 60182 | if( pWal->exclusiveMode ) return SQLITE_OK; |
| 60074 | 60183 | rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n, |
| 60075 | 60184 | SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE); |
| 60076 | 60185 | WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal, |
| 60077 | 60186 | walLockName(lockIdx), n, rc ? "failed" : "ok")); |
| 60078 | | - VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); ) |
| 60187 | + VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); ) |
| 60079 | 60188 | return rc; |
| 60080 | 60189 | } |
| 60081 | 60190 | static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){ |
| 60082 | 60191 | if( pWal->exclusiveMode ) return; |
| 60083 | 60192 | (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n, |
| | @@ -60345,15 +60454,10 @@ |
| 60345 | 60454 | int rc; /* Return Code */ |
| 60346 | 60455 | i64 nSize; /* Size of log file */ |
| 60347 | 60456 | u32 aFrameCksum[2] = {0, 0}; |
| 60348 | 60457 | int iLock; /* Lock offset to lock for checkpoint */ |
| 60349 | 60458 | |
| 60350 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 60351 | | - int tmout = 0; |
| 60352 | | - sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout); |
| 60353 | | -#endif |
| 60354 | | - |
| 60355 | 60459 | /* Obtain an exclusive lock on all byte in the locking range not already |
| 60356 | 60460 | ** locked by the caller. The caller is guaranteed to have locked the |
| 60357 | 60461 | ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte. |
| 60358 | 60462 | ** If successful, the same bytes that are locked here are unlocked before |
| 60359 | 60463 | ** this function returns. |
| | @@ -60897,10 +61001,93 @@ |
| 60897 | 61001 | p = 0; |
| 60898 | 61002 | } |
| 60899 | 61003 | *pp = p; |
| 60900 | 61004 | return rc; |
| 60901 | 61005 | } |
| 61006 | + |
| 61007 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 61008 | +/* |
| 61009 | +** Attempt to enable blocking locks. Blocking locks are enabled only if (a) |
| 61010 | +** they are supported by the VFS, and (b) the database handle is configured |
| 61011 | +** with a busy-timeout. Return 1 if blocking locks are successfully enabled, |
| 61012 | +** or 0 otherwise. |
| 61013 | +*/ |
| 61014 | +static int walEnableBlocking(Wal *pWal){ |
| 61015 | + int res = 0; |
| 61016 | + if( pWal->db ){ |
| 61017 | + int tmout = pWal->db->busyTimeout; |
| 61018 | + if( tmout ){ |
| 61019 | + int rc; |
| 61020 | + rc = sqlite3OsFileControl( |
| 61021 | + pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout |
| 61022 | + ); |
| 61023 | + res = (rc==SQLITE_OK); |
| 61024 | + } |
| 61025 | + } |
| 61026 | + return res; |
| 61027 | +} |
| 61028 | + |
| 61029 | +/* |
| 61030 | +** Disable blocking locks. |
| 61031 | +*/ |
| 61032 | +static void walDisableBlocking(Wal *pWal){ |
| 61033 | + int tmout = 0; |
| 61034 | + sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout); |
| 61035 | +} |
| 61036 | + |
| 61037 | +/* |
| 61038 | +** If parameter bLock is true, attempt to enable blocking locks, take |
| 61039 | +** the WRITER lock, and then disable blocking locks. If blocking locks |
| 61040 | +** cannot be enabled, no attempt to obtain the WRITER lock is made. Return |
| 61041 | +** an SQLite error code if an error occurs, or SQLITE_OK otherwise. It is not |
| 61042 | +** an error if blocking locks can not be enabled. |
| 61043 | +** |
| 61044 | +** If the bLock parameter is false and the WRITER lock is held, release it. |
| 61045 | +*/ |
| 61046 | +SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock){ |
| 61047 | + int rc = SQLITE_OK; |
| 61048 | + assert( pWal->readLock<0 || bLock==0 ); |
| 61049 | + if( bLock ){ |
| 61050 | + assert( pWal->db ); |
| 61051 | + if( walEnableBlocking(pWal) ){ |
| 61052 | + rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 61053 | + if( rc==SQLITE_OK ){ |
| 61054 | + pWal->writeLock = 1; |
| 61055 | + } |
| 61056 | + walDisableBlocking(pWal); |
| 61057 | + } |
| 61058 | + }else if( pWal->writeLock ){ |
| 61059 | + walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 61060 | + pWal->writeLock = 0; |
| 61061 | + } |
| 61062 | + return rc; |
| 61063 | +} |
| 61064 | + |
| 61065 | +/* |
| 61066 | +** Set the database handle used to determine if blocking locks are required. |
| 61067 | +*/ |
| 61068 | +SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db){ |
| 61069 | + pWal->db = db; |
| 61070 | +} |
| 61071 | + |
| 61072 | +/* |
| 61073 | +** Take an exclusive WRITE lock. Blocking if so configured. |
| 61074 | +*/ |
| 61075 | +static int walLockWriter(Wal *pWal){ |
| 61076 | + int rc; |
| 61077 | + walEnableBlocking(pWal); |
| 61078 | + rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 61079 | + walDisableBlocking(pWal); |
| 61080 | + return rc; |
| 61081 | +} |
| 61082 | +#else |
| 61083 | +# define walEnableBlocking(x) 0 |
| 61084 | +# define walDisableBlocking(x) |
| 61085 | +# define walLockWriter(pWal) walLockExclusive((pWal), WAL_WRITE_LOCK, 1) |
| 61086 | +# define sqlite3WalDb(pWal, db) |
| 61087 | +#endif /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */ |
| 61088 | + |
| 60902 | 61089 | |
| 60903 | 61090 | /* |
| 60904 | 61091 | ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and |
| 60905 | 61092 | ** n. If the attempt fails and parameter xBusy is not NULL, then it is a |
| 60906 | 61093 | ** busy-handler function. Invoke it and retry the lock until either the |
| | @@ -60915,10 +61102,16 @@ |
| 60915 | 61102 | ){ |
| 60916 | 61103 | int rc; |
| 60917 | 61104 | do { |
| 60918 | 61105 | rc = walLockExclusive(pWal, lockIdx, n); |
| 60919 | 61106 | }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) ); |
| 61107 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 61108 | + if( rc==SQLITE_BUSY_TIMEOUT ){ |
| 61109 | + walDisableBlocking(pWal); |
| 61110 | + rc = SQLITE_BUSY; |
| 61111 | + } |
| 61112 | +#endif |
| 60920 | 61113 | return rc; |
| 60921 | 61114 | } |
| 60922 | 61115 | |
| 60923 | 61116 | /* |
| 60924 | 61117 | ** The cache of the wal-index header must be valid to call this function. |
| | @@ -60952,11 +61145,11 @@ |
| 60952 | 61145 | pWal->nCkpt++; |
| 60953 | 61146 | pWal->hdr.mxFrame = 0; |
| 60954 | 61147 | sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0])); |
| 60955 | 61148 | memcpy(&pWal->hdr.aSalt[1], &salt1, 4); |
| 60956 | 61149 | walIndexWriteHdr(pWal); |
| 60957 | | - pInfo->nBackfill = 0; |
| 61150 | + AtomicStore(&pInfo->nBackfill, 0); |
| 60958 | 61151 | pInfo->nBackfillAttempted = 0; |
| 60959 | 61152 | pInfo->aReadMark[1] = 0; |
| 60960 | 61153 | for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; |
| 60961 | 61154 | assert( pInfo->aReadMark[0]==0 ); |
| 60962 | 61155 | } |
| | @@ -61027,36 +61220,17 @@ |
| 61027 | 61220 | ** cannot be backfilled from the WAL. |
| 61028 | 61221 | */ |
| 61029 | 61222 | mxSafeFrame = pWal->hdr.mxFrame; |
| 61030 | 61223 | mxPage = pWal->hdr.nPage; |
| 61031 | 61224 | for(i=1; i<WAL_NREADER; i++){ |
| 61032 | | - /* Thread-sanitizer reports that the following is an unsafe read, |
| 61033 | | - ** as some other thread may be in the process of updating the value |
| 61034 | | - ** of the aReadMark[] slot. The assumption here is that if that is |
| 61035 | | - ** happening, the other client may only be increasing the value, |
| 61036 | | - ** not decreasing it. So assuming either that either the "old" or |
| 61037 | | - ** "new" version of the value is read, and not some arbitrary value |
| 61038 | | - ** that would never be written by a real client, things are still |
| 61039 | | - ** safe. |
| 61040 | | - ** |
| 61041 | | - ** Astute readers have pointed out that the assumption stated in the |
| 61042 | | - ** last sentence of the previous paragraph is not guaranteed to be |
| 61043 | | - ** true for all conforming systems. However, the assumption is true |
| 61044 | | - ** for all compilers and architectures in common use today (circa |
| 61045 | | - ** 2019-11-27) and the alternatives are both slow and complex, and |
| 61046 | | - ** so we will continue to go with the current design for now. If this |
| 61047 | | - ** bothers you, or if you really are running on a system where aligned |
| 61048 | | - ** 32-bit reads and writes are not atomic, then you can simply avoid |
| 61049 | | - ** the use of WAL mode, or only use WAL mode together with |
| 61050 | | - ** PRAGMA locking_mode=EXCLUSIVE and all will be well. |
| 61051 | | - */ |
| 61052 | | - u32 y = pInfo->aReadMark[i]; |
| 61225 | + u32 y = AtomicLoad(pInfo->aReadMark+i); |
| 61053 | 61226 | if( mxSafeFrame>y ){ |
| 61054 | 61227 | assert( y<=pWal->hdr.mxFrame ); |
| 61055 | 61228 | rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1); |
| 61056 | 61229 | if( rc==SQLITE_OK ){ |
| 61057 | | - pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED); |
| 61230 | + u32 iMark = (i==1 ? mxSafeFrame : READMARK_NOT_USED); |
| 61231 | + AtomicStore(pInfo->aReadMark+i, iMark); |
| 61058 | 61232 | walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 61059 | 61233 | }else if( rc==SQLITE_BUSY ){ |
| 61060 | 61234 | mxSafeFrame = y; |
| 61061 | 61235 | xBusy = 0; |
| 61062 | 61236 | }else{ |
| | @@ -61070,11 +61244,11 @@ |
| 61070 | 61244 | rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter); |
| 61071 | 61245 | assert( rc==SQLITE_OK || pIter==0 ); |
| 61072 | 61246 | } |
| 61073 | 61247 | |
| 61074 | 61248 | if( pIter |
| 61075 | | - && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK |
| 61249 | + && (rc = walBusyLock(pWal,xBusy,pBusyArg,WAL_READ_LOCK(0),1))==SQLITE_OK |
| 61076 | 61250 | ){ |
| 61077 | 61251 | u32 nBackfill = pInfo->nBackfill; |
| 61078 | 61252 | |
| 61079 | 61253 | pInfo->nBackfillAttempted = mxSafeFrame; |
| 61080 | 61254 | |
| | @@ -61126,11 +61300,11 @@ |
| 61126 | 61300 | if( rc==SQLITE_OK ){ |
| 61127 | 61301 | rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags)); |
| 61128 | 61302 | } |
| 61129 | 61303 | } |
| 61130 | 61304 | if( rc==SQLITE_OK ){ |
| 61131 | | - pInfo->nBackfill = mxSafeFrame; |
| 61305 | + AtomicStore(&pInfo->nBackfill, mxSafeFrame); |
| 61132 | 61306 | } |
| 61133 | 61307 | } |
| 61134 | 61308 | |
| 61135 | 61309 | /* Release the reader lock held while backfilling */ |
| 61136 | 61310 | walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1); |
| | @@ -61285,11 +61459,11 @@ |
| 61285 | 61459 | ** and *pChanged is set to 1. |
| 61286 | 61460 | ** |
| 61287 | 61461 | ** If the checksum cannot be verified return non-zero. If the header |
| 61288 | 61462 | ** is read successfully and the checksum verified, return zero. |
| 61289 | 61463 | */ |
| 61290 | | -static int walIndexTryHdr(Wal *pWal, int *pChanged){ |
| 61464 | +static SQLITE_NO_TSAN int walIndexTryHdr(Wal *pWal, int *pChanged){ |
| 61291 | 61465 | u32 aCksum[2]; /* Checksum on the header content */ |
| 61292 | 61466 | WalIndexHdr h1, h2; /* Two copies of the header content */ |
| 61293 | 61467 | WalIndexHdr volatile *aHdr; /* Header in shared memory */ |
| 61294 | 61468 | |
| 61295 | 61469 | /* The first page of the wal-index must be mapped at this point. */ |
| | @@ -61298,17 +61472,23 @@ |
| 61298 | 61472 | /* Read the header. This might happen concurrently with a write to the |
| 61299 | 61473 | ** same area of shared memory on a different CPU in a SMP, |
| 61300 | 61474 | ** meaning it is possible that an inconsistent snapshot is read |
| 61301 | 61475 | ** from the file. If this happens, return non-zero. |
| 61302 | 61476 | ** |
| 61477 | + ** tag-20200519-1: |
| 61303 | 61478 | ** There are two copies of the header at the beginning of the wal-index. |
| 61304 | 61479 | ** When reading, read [0] first then [1]. Writes are in the reverse order. |
| 61305 | 61480 | ** Memory barriers are used to prevent the compiler or the hardware from |
| 61306 | | - ** reordering the reads and writes. |
| 61481 | + ** reordering the reads and writes. TSAN and similar tools can sometimes |
| 61482 | + ** give false-positive warnings about these accesses because the tools do not |
| 61483 | + ** account for the double-read and the memory barrier. The use of mutexes |
| 61484 | + ** here would be problematic as the memory being accessed is potentially |
| 61485 | + ** shared among multiple processes and not all mutex implementions work |
| 61486 | + ** reliably in that environment. |
| 61307 | 61487 | */ |
| 61308 | 61488 | aHdr = walIndexHdr(pWal); |
| 61309 | | - memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); |
| 61489 | + memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */ |
| 61310 | 61490 | walShmBarrier(pWal); |
| 61311 | 61491 | memcpy(&h2, (void *)&aHdr[1], sizeof(h2)); |
| 61312 | 61492 | |
| 61313 | 61493 | if( memcmp(&h1, &h2, sizeof(h1))!=0 ){ |
| 61314 | 61494 | return 1; /* Dirty read */ |
| | @@ -61394,32 +61574,36 @@ |
| 61394 | 61574 | badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1); |
| 61395 | 61575 | |
| 61396 | 61576 | /* If the first attempt failed, it might have been due to a race |
| 61397 | 61577 | ** with a writer. So get a WRITE lock and try again. |
| 61398 | 61578 | */ |
| 61399 | | - assert( badHdr==0 || pWal->writeLock==0 ); |
| 61400 | 61579 | if( badHdr ){ |
| 61401 | 61580 | if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){ |
| 61402 | 61581 | if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){ |
| 61403 | 61582 | walUnlockShared(pWal, WAL_WRITE_LOCK); |
| 61404 | 61583 | rc = SQLITE_READONLY_RECOVERY; |
| 61405 | 61584 | } |
| 61406 | | - }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){ |
| 61407 | | - pWal->writeLock = 1; |
| 61408 | | - if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){ |
| 61409 | | - badHdr = walIndexTryHdr(pWal, pChanged); |
| 61410 | | - if( badHdr ){ |
| 61411 | | - /* If the wal-index header is still malformed even while holding |
| 61412 | | - ** a WRITE lock, it can only mean that the header is corrupted and |
| 61413 | | - ** needs to be reconstructed. So run recovery to do exactly that. |
| 61414 | | - */ |
| 61415 | | - rc = walIndexRecover(pWal); |
| 61416 | | - *pChanged = 1; |
| 61417 | | - } |
| 61418 | | - } |
| 61419 | | - pWal->writeLock = 0; |
| 61420 | | - walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 61585 | + }else{ |
| 61586 | + int bWriteLock = pWal->writeLock; |
| 61587 | + if( bWriteLock || SQLITE_OK==(rc = walLockWriter(pWal)) ){ |
| 61588 | + pWal->writeLock = 1; |
| 61589 | + if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){ |
| 61590 | + badHdr = walIndexTryHdr(pWal, pChanged); |
| 61591 | + if( badHdr ){ |
| 61592 | + /* If the wal-index header is still malformed even while holding |
| 61593 | + ** a WRITE lock, it can only mean that the header is corrupted and |
| 61594 | + ** needs to be reconstructed. So run recovery to do exactly that. |
| 61595 | + */ |
| 61596 | + rc = walIndexRecover(pWal); |
| 61597 | + *pChanged = 1; |
| 61598 | + } |
| 61599 | + } |
| 61600 | + if( bWriteLock==0 ){ |
| 61601 | + pWal->writeLock = 0; |
| 61602 | + walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 61603 | + } |
| 61604 | + } |
| 61421 | 61605 | } |
| 61422 | 61606 | } |
| 61423 | 61607 | |
| 61424 | 61608 | /* If the header is read successfully, check the version number to make |
| 61425 | 61609 | ** sure the wal-index was not constructed with some future format that |
| | @@ -61745,11 +61929,11 @@ |
| 61745 | 61929 | } |
| 61746 | 61930 | |
| 61747 | 61931 | assert( pWal->nWiData>0 ); |
| 61748 | 61932 | assert( pWal->apWiData[0]!=0 ); |
| 61749 | 61933 | pInfo = walCkptInfo(pWal); |
| 61750 | | - if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame |
| 61934 | + if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame |
| 61751 | 61935 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 61752 | 61936 | && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0) |
| 61753 | 61937 | #endif |
| 61754 | 61938 | ){ |
| 61755 | 61939 | /* The WAL has been completely backfilled (or it is empty). |
| | @@ -61912,11 +62096,11 @@ |
| 61912 | 62096 | void *pBuf2 = sqlite3_malloc(szPage); |
| 61913 | 62097 | if( pBuf1==0 || pBuf2==0 ){ |
| 61914 | 62098 | rc = SQLITE_NOMEM; |
| 61915 | 62099 | }else{ |
| 61916 | 62100 | u32 i = pInfo->nBackfillAttempted; |
| 61917 | | - for(i=pInfo->nBackfillAttempted; i>pInfo->nBackfill; i--){ |
| 62101 | + for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){ |
| 61918 | 62102 | WalHashLoc sLoc; /* Hash table location */ |
| 61919 | 62103 | u32 pgno; /* Page number in db file */ |
| 61920 | 62104 | i64 iDbOff; /* Offset of db file entry */ |
| 61921 | 62105 | i64 iWalOff; /* Offset of wal file entry */ |
| 61922 | 62106 | |
| | @@ -61967,26 +62151,40 @@ |
| 61967 | 62151 | ** needs to be flushed. |
| 61968 | 62152 | */ |
| 61969 | 62153 | SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ |
| 61970 | 62154 | int rc; /* Return code */ |
| 61971 | 62155 | int cnt = 0; /* Number of TryBeginRead attempts */ |
| 61972 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 61973 | | - int tmout = 0; |
| 61974 | | -#endif |
| 61975 | | - |
| 61976 | 62156 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 61977 | 62157 | int bChanged = 0; |
| 61978 | 62158 | WalIndexHdr *pSnapshot = pWal->pSnapshot; |
| 61979 | | - if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ |
| 61980 | | - bChanged = 1; |
| 61981 | | - } |
| 61982 | 62159 | #endif |
| 61983 | 62160 | |
| 61984 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 61985 | | - /* Disable blocking locks. They are not useful when trying to open a |
| 61986 | | - ** read-transaction, and blocking may cause deadlock anyway. */ |
| 61987 | | - sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout); |
| 62161 | + assert( pWal->ckptLock==0 ); |
| 62162 | + |
| 62163 | +#ifdef SQLITE_ENABLE_SNAPSHOT |
| 62164 | + if( pSnapshot ){ |
| 62165 | + if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ |
| 62166 | + bChanged = 1; |
| 62167 | + } |
| 62168 | + |
| 62169 | + /* It is possible that there is a checkpointer thread running |
| 62170 | + ** concurrent with this code. If this is the case, it may be that the |
| 62171 | + ** checkpointer has already determined that it will checkpoint |
| 62172 | + ** snapshot X, where X is later in the wal file than pSnapshot, but |
| 62173 | + ** has not yet set the pInfo->nBackfillAttempted variable to indicate |
| 62174 | + ** its intent. To avoid the race condition this leads to, ensure that |
| 62175 | + ** there is no checkpointer process by taking a shared CKPT lock |
| 62176 | + ** before checking pInfo->nBackfillAttempted. */ |
| 62177 | + (void)walEnableBlocking(pWal); |
| 62178 | + rc = walLockShared(pWal, WAL_CKPT_LOCK); |
| 62179 | + walDisableBlocking(pWal); |
| 62180 | + |
| 62181 | + if( rc!=SQLITE_OK ){ |
| 62182 | + return rc; |
| 62183 | + } |
| 62184 | + pWal->ckptLock = 1; |
| 62185 | + } |
| 61988 | 62186 | #endif |
| 61989 | 62187 | |
| 61990 | 62188 | do{ |
| 61991 | 62189 | rc = walTryBeginRead(pWal, pChanged, 0, ++cnt); |
| 61992 | 62190 | }while( rc==WAL_RETRY ); |
| | @@ -61993,20 +62191,10 @@ |
| 61993 | 62191 | testcase( (rc&0xff)==SQLITE_BUSY ); |
| 61994 | 62192 | testcase( (rc&0xff)==SQLITE_IOERR ); |
| 61995 | 62193 | testcase( rc==SQLITE_PROTOCOL ); |
| 61996 | 62194 | testcase( rc==SQLITE_OK ); |
| 61997 | 62195 | |
| 61998 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 61999 | | - /* If they were disabled earlier and the read-transaction has been |
| 62000 | | - ** successfully opened, re-enable blocking locks. This is because the |
| 62001 | | - ** connection may attempt to upgrade to a write-transaction, which does |
| 62002 | | - ** benefit from using blocking locks. */ |
| 62003 | | - if( rc==SQLITE_OK ){ |
| 62004 | | - sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout); |
| 62005 | | - } |
| 62006 | | -#endif |
| 62007 | | - |
| 62008 | 62196 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 62009 | 62197 | if( rc==SQLITE_OK ){ |
| 62010 | 62198 | if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ |
| 62011 | 62199 | /* At this point the client has a lock on an aReadMark[] slot holding |
| 62012 | 62200 | ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr |
| | @@ -62024,52 +62212,46 @@ |
| 62024 | 62212 | volatile WalCkptInfo *pInfo = walCkptInfo(pWal); |
| 62025 | 62213 | |
| 62026 | 62214 | assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 ); |
| 62027 | 62215 | assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame ); |
| 62028 | 62216 | |
| 62029 | | - /* It is possible that there is a checkpointer thread running |
| 62030 | | - ** concurrent with this code. If this is the case, it may be that the |
| 62031 | | - ** checkpointer has already determined that it will checkpoint |
| 62032 | | - ** snapshot X, where X is later in the wal file than pSnapshot, but |
| 62033 | | - ** has not yet set the pInfo->nBackfillAttempted variable to indicate |
| 62034 | | - ** its intent. To avoid the race condition this leads to, ensure that |
| 62035 | | - ** there is no checkpointer process by taking a shared CKPT lock |
| 62036 | | - ** before checking pInfo->nBackfillAttempted. |
| 62037 | | - ** |
| 62038 | | - ** TODO: Does the aReadMark[] lock prevent a checkpointer from doing |
| 62039 | | - ** this already? |
| 62040 | | - */ |
| 62041 | | - rc = walLockShared(pWal, WAL_CKPT_LOCK); |
| 62042 | | - |
| 62043 | | - if( rc==SQLITE_OK ){ |
| 62044 | | - /* Check that the wal file has not been wrapped. Assuming that it has |
| 62045 | | - ** not, also check that no checkpointer has attempted to checkpoint any |
| 62046 | | - ** frames beyond pSnapshot->mxFrame. If either of these conditions are |
| 62047 | | - ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr |
| 62048 | | - ** with *pSnapshot and set *pChanged as appropriate for opening the |
| 62049 | | - ** snapshot. */ |
| 62050 | | - if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt)) |
| 62051 | | - && pSnapshot->mxFrame>=pInfo->nBackfillAttempted |
| 62052 | | - ){ |
| 62053 | | - assert( pWal->readLock>0 ); |
| 62054 | | - memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr)); |
| 62055 | | - *pChanged = bChanged; |
| 62056 | | - }else{ |
| 62057 | | - rc = SQLITE_ERROR_SNAPSHOT; |
| 62058 | | - } |
| 62059 | | - |
| 62060 | | - /* Release the shared CKPT lock obtained above. */ |
| 62061 | | - walUnlockShared(pWal, WAL_CKPT_LOCK); |
| 62062 | | - pWal->minFrame = 1; |
| 62063 | | - } |
| 62064 | | - |
| 62217 | + /* Check that the wal file has not been wrapped. Assuming that it has |
| 62218 | + ** not, also check that no checkpointer has attempted to checkpoint any |
| 62219 | + ** frames beyond pSnapshot->mxFrame. If either of these conditions are |
| 62220 | + ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr |
| 62221 | + ** with *pSnapshot and set *pChanged as appropriate for opening the |
| 62222 | + ** snapshot. */ |
| 62223 | + if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt)) |
| 62224 | + && pSnapshot->mxFrame>=pInfo->nBackfillAttempted |
| 62225 | + ){ |
| 62226 | + assert( pWal->readLock>0 ); |
| 62227 | + memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr)); |
| 62228 | + *pChanged = bChanged; |
| 62229 | + }else{ |
| 62230 | + rc = SQLITE_ERROR_SNAPSHOT; |
| 62231 | + } |
| 62232 | + |
| 62233 | + /* A client using a non-current snapshot may not ignore any frames |
| 62234 | + ** from the start of the wal file. This is because, for a system |
| 62235 | + ** where (minFrame < iSnapshot < maxFrame), a checkpointer may |
| 62236 | + ** have omitted to checkpoint a frame earlier than minFrame in |
| 62237 | + ** the file because there exists a frame after iSnapshot that |
| 62238 | + ** is the same database page. */ |
| 62239 | + pWal->minFrame = 1; |
| 62065 | 62240 | |
| 62066 | 62241 | if( rc!=SQLITE_OK ){ |
| 62067 | 62242 | sqlite3WalEndReadTransaction(pWal); |
| 62068 | 62243 | } |
| 62069 | 62244 | } |
| 62070 | 62245 | } |
| 62246 | + |
| 62247 | + /* Release the shared CKPT lock obtained above. */ |
| 62248 | + if( pWal->ckptLock ){ |
| 62249 | + assert( pSnapshot ); |
| 62250 | + walUnlockShared(pWal, WAL_CKPT_LOCK); |
| 62251 | + pWal->ckptLock = 0; |
| 62252 | + } |
| 62071 | 62253 | #endif |
| 62072 | 62254 | return rc; |
| 62073 | 62255 | } |
| 62074 | 62256 | |
| 62075 | 62257 | /* |
| | @@ -62145,26 +62327,28 @@ |
| 62145 | 62327 | for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){ |
| 62146 | 62328 | WalHashLoc sLoc; /* Hash table location */ |
| 62147 | 62329 | int iKey; /* Hash slot index */ |
| 62148 | 62330 | int nCollide; /* Number of hash collisions remaining */ |
| 62149 | 62331 | int rc; /* Error code */ |
| 62332 | + u32 iH; |
| 62150 | 62333 | |
| 62151 | 62334 | rc = walHashGet(pWal, iHash, &sLoc); |
| 62152 | 62335 | if( rc!=SQLITE_OK ){ |
| 62153 | 62336 | return rc; |
| 62154 | 62337 | } |
| 62155 | 62338 | nCollide = HASHTABLE_NSLOT; |
| 62156 | | - for(iKey=walHash(pgno); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){ |
| 62157 | | - u32 iH = sLoc.aHash[iKey]; |
| 62339 | + iKey = walHash(pgno); |
| 62340 | + while( (iH = AtomicLoad(&sLoc.aHash[iKey]))!=0 ){ |
| 62158 | 62341 | u32 iFrame = iH + sLoc.iZero; |
| 62159 | 62342 | if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH]==pgno ){ |
| 62160 | 62343 | assert( iFrame>iRead || CORRUPT_DB ); |
| 62161 | 62344 | iRead = iFrame; |
| 62162 | 62345 | } |
| 62163 | 62346 | if( (nCollide--)==0 ){ |
| 62164 | 62347 | return SQLITE_CORRUPT_BKPT; |
| 62165 | 62348 | } |
| 62349 | + iKey = walNextHash(iKey); |
| 62166 | 62350 | } |
| 62167 | 62351 | if( iRead ) break; |
| 62168 | 62352 | } |
| 62169 | 62353 | |
| 62170 | 62354 | #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT |
| | @@ -62235,10 +62419,20 @@ |
| 62235 | 62419 | ** |
| 62236 | 62420 | ** There can only be a single writer active at a time. |
| 62237 | 62421 | */ |
| 62238 | 62422 | SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){ |
| 62239 | 62423 | int rc; |
| 62424 | + |
| 62425 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 62426 | + /* If the write-lock is already held, then it was obtained before the |
| 62427 | + ** read-transaction was even opened, making this call a no-op. |
| 62428 | + ** Return early. */ |
| 62429 | + if( pWal->writeLock ){ |
| 62430 | + assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) ); |
| 62431 | + return SQLITE_OK; |
| 62432 | + } |
| 62433 | +#endif |
| 62240 | 62434 | |
| 62241 | 62435 | /* Cannot start a write transaction without first holding a read |
| 62242 | 62436 | ** transaction. */ |
| 62243 | 62437 | assert( pWal->readLock>=0 ); |
| 62244 | 62438 | assert( pWal->writeLock==0 && pWal->iReCksum==0 ); |
| | @@ -62811,50 +63005,57 @@ |
| 62811 | 63005 | ** in the SQLITE_CHECKPOINT_PASSIVE mode. */ |
| 62812 | 63006 | assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 ); |
| 62813 | 63007 | |
| 62814 | 63008 | if( pWal->readOnly ) return SQLITE_READONLY; |
| 62815 | 63009 | WALTRACE(("WAL%p: checkpoint begins\n", pWal)); |
| 63010 | + |
| 63011 | + /* Enable blocking locks, if possible. If blocking locks are successfully |
| 63012 | + ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */ |
| 63013 | + sqlite3WalDb(pWal, db); |
| 63014 | + (void)walEnableBlocking(pWal); |
| 62816 | 63015 | |
| 62817 | 63016 | /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive |
| 62818 | | - ** "checkpoint" lock on the database file. */ |
| 63017 | + ** "checkpoint" lock on the database file. |
| 63018 | + ** EVIDENCE-OF: R-10421-19736 If any other process is running a |
| 63019 | + ** checkpoint operation at the same time, the lock cannot be obtained and |
| 63020 | + ** SQLITE_BUSY is returned. |
| 63021 | + ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured, |
| 63022 | + ** it will not be invoked in this case. |
| 63023 | + */ |
| 62819 | 63024 | rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1); |
| 62820 | | - if( rc ){ |
| 62821 | | - /* EVIDENCE-OF: R-10421-19736 If any other process is running a |
| 62822 | | - ** checkpoint operation at the same time, the lock cannot be obtained and |
| 62823 | | - ** SQLITE_BUSY is returned. |
| 62824 | | - ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured, |
| 62825 | | - ** it will not be invoked in this case. |
| 62826 | | - */ |
| 62827 | | - testcase( rc==SQLITE_BUSY ); |
| 62828 | | - testcase( xBusy!=0 ); |
| 62829 | | - return rc; |
| 62830 | | - } |
| 62831 | | - pWal->ckptLock = 1; |
| 62832 | | - |
| 62833 | | - /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and |
| 62834 | | - ** TRUNCATE modes also obtain the exclusive "writer" lock on the database |
| 62835 | | - ** file. |
| 62836 | | - ** |
| 62837 | | - ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained |
| 62838 | | - ** immediately, and a busy-handler is configured, it is invoked and the |
| 62839 | | - ** writer lock retried until either the busy-handler returns 0 or the |
| 62840 | | - ** lock is successfully obtained. |
| 62841 | | - */ |
| 62842 | | - if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){ |
| 62843 | | - rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1); |
| 62844 | | - if( rc==SQLITE_OK ){ |
| 62845 | | - pWal->writeLock = 1; |
| 62846 | | - }else if( rc==SQLITE_BUSY ){ |
| 62847 | | - eMode2 = SQLITE_CHECKPOINT_PASSIVE; |
| 62848 | | - xBusy2 = 0; |
| 62849 | | - rc = SQLITE_OK; |
| 62850 | | - } |
| 62851 | | - } |
| 63025 | + testcase( rc==SQLITE_BUSY ); |
| 63026 | + testcase( rc!=SQLITE_OK && xBusy2!=0 ); |
| 63027 | + if( rc==SQLITE_OK ){ |
| 63028 | + pWal->ckptLock = 1; |
| 63029 | + |
| 63030 | + /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and |
| 63031 | + ** TRUNCATE modes also obtain the exclusive "writer" lock on the database |
| 63032 | + ** file. |
| 63033 | + ** |
| 63034 | + ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained |
| 63035 | + ** immediately, and a busy-handler is configured, it is invoked and the |
| 63036 | + ** writer lock retried until either the busy-handler returns 0 or the |
| 63037 | + ** lock is successfully obtained. |
| 63038 | + */ |
| 63039 | + if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){ |
| 63040 | + rc = walBusyLock(pWal, xBusy2, pBusyArg, WAL_WRITE_LOCK, 1); |
| 63041 | + if( rc==SQLITE_OK ){ |
| 63042 | + pWal->writeLock = 1; |
| 63043 | + }else if( rc==SQLITE_BUSY ){ |
| 63044 | + eMode2 = SQLITE_CHECKPOINT_PASSIVE; |
| 63045 | + xBusy2 = 0; |
| 63046 | + rc = SQLITE_OK; |
| 63047 | + } |
| 63048 | + } |
| 63049 | + } |
| 63050 | + |
| 62852 | 63051 | |
| 62853 | 63052 | /* Read the wal-index header. */ |
| 62854 | 63053 | if( rc==SQLITE_OK ){ |
| 63054 | + walDisableBlocking(pWal); |
| 62855 | 63055 | rc = walIndexReadHdr(pWal, &isChanged); |
| 63056 | + (void)walEnableBlocking(pWal); |
| 62856 | 63057 | if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){ |
| 62857 | 63058 | sqlite3OsUnfetch(pWal->pDbFd, 0, 0); |
| 62858 | 63059 | } |
| 62859 | 63060 | } |
| 62860 | 63061 | |
| | @@ -62881,16 +63082,24 @@ |
| 62881 | 63082 | ** next time the pager opens a snapshot on this database it knows that |
| 62882 | 63083 | ** the cache needs to be reset. |
| 62883 | 63084 | */ |
| 62884 | 63085 | memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); |
| 62885 | 63086 | } |
| 63087 | + |
| 63088 | + walDisableBlocking(pWal); |
| 63089 | + sqlite3WalDb(pWal, 0); |
| 62886 | 63090 | |
| 62887 | 63091 | /* Release the locks. */ |
| 62888 | 63092 | sqlite3WalEndWriteTransaction(pWal); |
| 62889 | | - walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); |
| 62890 | | - pWal->ckptLock = 0; |
| 63093 | + if( pWal->ckptLock ){ |
| 63094 | + walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); |
| 63095 | + pWal->ckptLock = 0; |
| 63096 | + } |
| 62891 | 63097 | WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok")); |
| 63098 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 63099 | + if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY; |
| 63100 | +#endif |
| 62892 | 63101 | return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc); |
| 62893 | 63102 | } |
| 62894 | 63103 | |
| 62895 | 63104 | /* Return the value to pass to a sqlite3_wal_hook callback, the |
| 62896 | 63105 | ** number of frames in the WAL at the point of the last commit since |
| | @@ -63003,11 +63212,14 @@ |
| 63003 | 63212 | return rc; |
| 63004 | 63213 | } |
| 63005 | 63214 | |
| 63006 | 63215 | /* Try to open on pSnapshot when the next read-transaction starts |
| 63007 | 63216 | */ |
| 63008 | | -SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){ |
| 63217 | +SQLITE_PRIVATE void sqlite3WalSnapshotOpen( |
| 63218 | + Wal *pWal, |
| 63219 | + sqlite3_snapshot *pSnapshot |
| 63220 | +){ |
| 63009 | 63221 | pWal->pSnapshot = (WalIndexHdr*)pSnapshot; |
| 63010 | 63222 | } |
| 63011 | 63223 | |
| 63012 | 63224 | /* |
| 63013 | 63225 | ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if |
| | @@ -63522,11 +63734,11 @@ |
| 63522 | 63734 | u8 incrVacuum; /* True if incr-vacuum is enabled */ |
| 63523 | 63735 | u8 bDoTruncate; /* True to truncate db on commit */ |
| 63524 | 63736 | #endif |
| 63525 | 63737 | u8 inTransaction; /* Transaction state */ |
| 63526 | 63738 | u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */ |
| 63527 | | - u8 nReserveWanted; /* 1 more than desired number of extra bytes per page */ |
| 63739 | + u8 nReserveWanted; /* Desired number of extra bytes per page */ |
| 63528 | 63740 | u16 btsFlags; /* Boolean parameters. See BTS_* macros below */ |
| 63529 | 63741 | u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| 63530 | 63742 | u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 63531 | 63743 | u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 63532 | 63744 | u16 minLeaf; /* Minimum local payload in a LEAFDATA table */ |
| | @@ -66416,12 +66628,11 @@ |
| 66416 | 66628 | */ |
| 66417 | 66629 | static int btreeInvokeBusyHandler(void *pArg){ |
| 66418 | 66630 | BtShared *pBt = (BtShared*)pArg; |
| 66419 | 66631 | assert( pBt->db ); |
| 66420 | 66632 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 66421 | | - return sqlite3InvokeBusyHandler(&pBt->db->busyHandler, |
| 66422 | | - sqlite3PagerFile(pBt->pPager)); |
| 66633 | + return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 66423 | 66634 | } |
| 66424 | 66635 | |
| 66425 | 66636 | /* |
| 66426 | 66637 | ** Open a database file. |
| 66427 | 66638 | ** |
| | @@ -66968,23 +67179,21 @@ |
| 66968 | 67179 | ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size |
| 66969 | 67180 | ** and autovacuum mode can no longer be changed. |
| 66970 | 67181 | */ |
| 66971 | 67182 | SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){ |
| 66972 | 67183 | int rc = SQLITE_OK; |
| 67184 | + int x; |
| 66973 | 67185 | BtShared *pBt = p->pBt; |
| 66974 | | - assert( nReserve>=-1 && nReserve<=254 ); |
| 67186 | + assert( nReserve>=0 && nReserve<=255 ); |
| 66975 | 67187 | sqlite3BtreeEnter(p); |
| 66976 | | - if( nReserve>=0 ){ |
| 66977 | | - pBt->nReserveWanted = nReserve + 1; |
| 66978 | | - } |
| 67188 | + pBt->nReserveWanted = nReserve; |
| 67189 | + x = pBt->pageSize - pBt->usableSize; |
| 67190 | + if( nReserve<x ) nReserve = x; |
| 66979 | 67191 | if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){ |
| 66980 | 67192 | sqlite3BtreeLeave(p); |
| 66981 | 67193 | return SQLITE_READONLY; |
| 66982 | 67194 | } |
| 66983 | | - if( nReserve<0 ){ |
| 66984 | | - nReserve = pBt->pageSize - pBt->usableSize; |
| 66985 | | - } |
| 66986 | 67195 | assert( nReserve>=0 && nReserve<=255 ); |
| 66987 | 67196 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 66988 | 67197 | ((pageSize-1)&pageSize)==0 ){ |
| 66989 | 67198 | assert( (pageSize & 7)==0 ); |
| 66990 | 67199 | assert( !pBt->pCursor ); |
| | @@ -67031,16 +67240,16 @@ |
| 67031 | 67240 | ** The value returned is the larger of the current reserve size and |
| 67032 | 67241 | ** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES. |
| 67033 | 67242 | ** The amount of reserve can only grow - never shrink. |
| 67034 | 67243 | */ |
| 67035 | 67244 | SQLITE_PRIVATE int sqlite3BtreeGetRequestedReserve(Btree *p){ |
| 67036 | | - int n; |
| 67245 | + int n1, n2; |
| 67037 | 67246 | sqlite3BtreeEnter(p); |
| 67038 | | - n = ((int)p->pBt->nReserveWanted) - 1; |
| 67039 | | - if( n<0 ) n = sqlite3BtreeGetReserveNoMutex(p); |
| 67247 | + n1 = (int)p->pBt->nReserveWanted; |
| 67248 | + n2 = sqlite3BtreeGetReserveNoMutex(p); |
| 67040 | 67249 | sqlite3BtreeLeave(p); |
| 67041 | | - return n; |
| 67250 | + return n1>n2 ? n1 : n2; |
| 67042 | 67251 | } |
| 67043 | 67252 | |
| 67044 | 67253 | |
| 67045 | 67254 | /* |
| 67046 | 67255 | ** Set the maximum page count for a database if mxPage is positive. |
| | @@ -67486,10 +67695,11 @@ |
| 67486 | 67695 | ** when A already has a read lock, we encourage A to give up and let B |
| 67487 | 67696 | ** proceed. |
| 67488 | 67697 | */ |
| 67489 | 67698 | SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){ |
| 67490 | 67699 | BtShared *pBt = p->pBt; |
| 67700 | + Pager *pPager = pBt->pPager; |
| 67491 | 67701 | int rc = SQLITE_OK; |
| 67492 | 67702 | |
| 67493 | 67703 | sqlite3BtreeEnter(p); |
| 67494 | 67704 | btreeIntegrity(p); |
| 67495 | 67705 | |
| | @@ -67501,11 +67711,11 @@ |
| 67501 | 67711 | goto trans_begun; |
| 67502 | 67712 | } |
| 67503 | 67713 | assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 ); |
| 67504 | 67714 | |
| 67505 | 67715 | if( (p->db->flags & SQLITE_ResetDatabase) |
| 67506 | | - && sqlite3PagerIsreadonly(pBt->pPager)==0 |
| 67716 | + && sqlite3PagerIsreadonly(pPager)==0 |
| 67507 | 67717 | ){ |
| 67508 | 67718 | pBt->btsFlags &= ~BTS_READ_ONLY; |
| 67509 | 67719 | } |
| 67510 | 67720 | |
| 67511 | 67721 | /* Write transactions are not possible on a read-only database */ |
| | @@ -67549,10 +67759,22 @@ |
| 67549 | 67759 | if( SQLITE_OK!=rc ) goto trans_begun; |
| 67550 | 67760 | |
| 67551 | 67761 | pBt->btsFlags &= ~BTS_INITIALLY_EMPTY; |
| 67552 | 67762 | if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY; |
| 67553 | 67763 | do { |
| 67764 | + sqlite3PagerWalDb(pPager, p->db); |
| 67765 | + |
| 67766 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67767 | + /* If transitioning from no transaction directly to a write transaction, |
| 67768 | + ** block for the WRITER lock first if possible. */ |
| 67769 | + if( pBt->pPage1==0 && wrflag ){ |
| 67770 | + assert( pBt->inTransaction==TRANS_NONE ); |
| 67771 | + rc = sqlite3PagerWalWriteLock(pPager, 1); |
| 67772 | + if( rc!=SQLITE_BUSY && rc!=SQLITE_OK ) break; |
| 67773 | + } |
| 67774 | +#endif |
| 67775 | + |
| 67554 | 67776 | /* Call lockBtree() until either pBt->pPage1 is populated or |
| 67555 | 67777 | ** lockBtree() returns something other than SQLITE_OK. lockBtree() |
| 67556 | 67778 | ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after |
| 67557 | 67779 | ** reading page 1 it discovers that the page-size of the database |
| 67558 | 67780 | ** file is not pBt->pageSize. In this case lockBtree() will update |
| | @@ -67562,11 +67784,11 @@ |
| 67562 | 67784 | |
| 67563 | 67785 | if( rc==SQLITE_OK && wrflag ){ |
| 67564 | 67786 | if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){ |
| 67565 | 67787 | rc = SQLITE_READONLY; |
| 67566 | 67788 | }else{ |
| 67567 | | - rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db)); |
| 67789 | + rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db)); |
| 67568 | 67790 | if( rc==SQLITE_OK ){ |
| 67569 | 67791 | rc = newDatabase(pBt); |
| 67570 | 67792 | }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){ |
| 67571 | 67793 | /* if there was no transaction opened when this function was |
| 67572 | 67794 | ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error |
| | @@ -67575,15 +67797,19 @@ |
| 67575 | 67797 | } |
| 67576 | 67798 | } |
| 67577 | 67799 | } |
| 67578 | 67800 | |
| 67579 | 67801 | if( rc!=SQLITE_OK ){ |
| 67802 | + (void)sqlite3PagerWalWriteLock(pPager, 0); |
| 67580 | 67803 | unlockBtreeIfUnused(pBt); |
| 67581 | 67804 | } |
| 67582 | 67805 | }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
| 67583 | 67806 | btreeInvokeBusyHandler(pBt) ); |
| 67584 | | - sqlite3PagerResetLockTimeout(pBt->pPager); |
| 67807 | + sqlite3PagerWalDb(pPager, 0); |
| 67808 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67809 | + if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY; |
| 67810 | +#endif |
| 67585 | 67811 | |
| 67586 | 67812 | if( rc==SQLITE_OK ){ |
| 67587 | 67813 | if( p->inTrans==TRANS_NONE ){ |
| 67588 | 67814 | pBt->nTransaction++; |
| 67589 | 67815 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| | @@ -67631,11 +67857,11 @@ |
| 67631 | 67857 | if( wrflag ){ |
| 67632 | 67858 | /* This call makes sure that the pager has the correct number of |
| 67633 | 67859 | ** open savepoints. If the second parameter is greater than 0 and |
| 67634 | 67860 | ** the sub-journal is not already open, then it will be opened here. |
| 67635 | 67861 | */ |
| 67636 | | - rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); |
| 67862 | + rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint); |
| 67637 | 67863 | } |
| 67638 | 67864 | } |
| 67639 | 67865 | |
| 67640 | 67866 | btreeIntegrity(p); |
| 67641 | 67867 | sqlite3BtreeLeave(p); |
| | @@ -71267,11 +71493,11 @@ |
| 71267 | 71493 | |
| 71268 | 71494 | /* Remove cells from the start and end of the page */ |
| 71269 | 71495 | assert( nCell>=0 ); |
| 71270 | 71496 | if( iOld<iNew ){ |
| 71271 | 71497 | int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray); |
| 71272 | | - if( nShift>nCell ) return SQLITE_CORRUPT_BKPT; |
| 71498 | + if( NEVER(nShift>nCell) ) return SQLITE_CORRUPT_BKPT; |
| 71273 | 71499 | memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); |
| 71274 | 71500 | nCell -= nShift; |
| 71275 | 71501 | } |
| 71276 | 71502 | if( iNewEnd < iOldEnd ){ |
| 71277 | 71503 | int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray); |
| | @@ -74746,11 +74972,11 @@ |
| 74746 | 74972 | ** Attempt to set the page size of the destination to match the page size |
| 74747 | 74973 | ** of the source. |
| 74748 | 74974 | */ |
| 74749 | 74975 | static int setDestPgsz(sqlite3_backup *p){ |
| 74750 | 74976 | int rc; |
| 74751 | | - rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0); |
| 74977 | + rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0); |
| 74752 | 74978 | return rc; |
| 74753 | 74979 | } |
| 74754 | 74980 | |
| 74755 | 74981 | /* |
| 74756 | 74982 | ** Check that there is no open read-transaction on the b-tree passed as the |
| | @@ -79143,10 +79369,11 @@ |
| 79143 | 79369 | char *zP4; |
| 79144 | 79370 | char *zCom; |
| 79145 | 79371 | sqlite3 dummyDb; |
| 79146 | 79372 | static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n"; |
| 79147 | 79373 | if( pOut==0 ) pOut = stdout; |
| 79374 | + sqlite3BeginBenignMalloc(); |
| 79148 | 79375 | dummyDb.mallocFailed = 1; |
| 79149 | 79376 | zP4 = sqlite3VdbeDisplayP4(&dummyDb, pOp); |
| 79150 | 79377 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 79151 | 79378 | zCom = sqlite3VdbeDisplayComment(0, pOp, zP4); |
| 79152 | 79379 | #else |
| | @@ -79161,10 +79388,11 @@ |
| 79161 | 79388 | zCom ? zCom : "" |
| 79162 | 79389 | ); |
| 79163 | 79390 | fflush(pOut); |
| 79164 | 79391 | sqlite3_free(zP4); |
| 79165 | 79392 | sqlite3_free(zCom); |
| 79393 | + sqlite3EndBenignMalloc(); |
| 79166 | 79394 | } |
| 79167 | 79395 | #endif |
| 79168 | 79396 | |
| 79169 | 79397 | /* |
| 79170 | 79398 | ** Initialize an array of N Mem element. |
| | @@ -83902,11 +84130,11 @@ |
| 83902 | 84130 | p->db->errCode = SQLITE_OK; |
| 83903 | 84131 | |
| 83904 | 84132 | /* If the bit corresponding to this variable in Vdbe.expmask is set, then |
| 83905 | 84133 | ** binding a new value to this variable invalidates the current query plan. |
| 83906 | 84134 | ** |
| 83907 | | - ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host |
| 84135 | + ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host |
| 83908 | 84136 | ** parameter in the WHERE clause might influence the choice of query plan |
| 83909 | 84137 | ** for a statement, then the statement will be automatically recompiled, |
| 83910 | 84138 | ** as if there had been a schema change, on the first sqlite3_step() call |
| 83911 | 84139 | ** following any change to the bindings of that parameter. |
| 83912 | 84140 | */ |
| | @@ -90540,16 +90768,23 @@ |
| 90540 | 90768 | rc = sqlite3VdbeSorterWrite(pC, pIn2); |
| 90541 | 90769 | if( rc) goto abort_due_to_error; |
| 90542 | 90770 | break; |
| 90543 | 90771 | } |
| 90544 | 90772 | |
| 90545 | | -/* Opcode: IdxDelete P1 P2 P3 * * |
| 90773 | +/* Opcode: IdxDelete P1 P2 P3 * P5 |
| 90546 | 90774 | ** Synopsis: key=r[P2@P3] |
| 90547 | 90775 | ** |
| 90548 | 90776 | ** The content of P3 registers starting at register P2 form |
| 90549 | 90777 | ** an unpacked index key. This opcode removes that entry from the |
| 90550 | 90778 | ** index opened by cursor P1. |
| 90779 | +** |
| 90780 | +** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error |
| 90781 | +** if no matching index entry is found. This happens when running |
| 90782 | +** an UPDATE or DELETE statement and the index entry to be updated |
| 90783 | +** or deleted is not found. For some uses of IdxDelete |
| 90784 | +** (example: the EXCEPT operator) it does not matter that no matching |
| 90785 | +** entry is found. For those cases, P5 is zero. |
| 90551 | 90786 | */ |
| 90552 | 90787 | case OP_IdxDelete: { |
| 90553 | 90788 | VdbeCursor *pC; |
| 90554 | 90789 | BtCursor *pCrsr; |
| 90555 | 90790 | int res; |
| | @@ -90562,20 +90797,22 @@ |
| 90562 | 90797 | assert( pC!=0 ); |
| 90563 | 90798 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 90564 | 90799 | sqlite3VdbeIncrWriteCounter(p, pC); |
| 90565 | 90800 | pCrsr = pC->uc.pCursor; |
| 90566 | 90801 | assert( pCrsr!=0 ); |
| 90567 | | - assert( pOp->p5==0 ); |
| 90568 | 90802 | r.pKeyInfo = pC->pKeyInfo; |
| 90569 | 90803 | r.nField = (u16)pOp->p3; |
| 90570 | 90804 | r.default_rc = 0; |
| 90571 | 90805 | r.aMem = &aMem[pOp->p2]; |
| 90572 | 90806 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); |
| 90573 | 90807 | if( rc ) goto abort_due_to_error; |
| 90574 | 90808 | if( res==0 ){ |
| 90575 | 90809 | rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); |
| 90576 | 90810 | if( rc ) goto abort_due_to_error; |
| 90811 | + }else if( pOp->p5 ){ |
| 90812 | + rc = SQLITE_CORRUPT_INDEX; |
| 90813 | + goto abort_due_to_error; |
| 90577 | 90814 | } |
| 90578 | 90815 | assert( pC->deferredMoveto==0 ); |
| 90579 | 90816 | pC->cacheStatus = CACHE_STALE; |
| 90580 | 90817 | pC->seekResult = 0; |
| 90581 | 90818 | break; |
| | @@ -96192,12 +96429,12 @@ |
| 96192 | 96429 | ************************************************************************* |
| 96193 | 96430 | ** |
| 96194 | 96431 | ** This file implements virtual-tables for examining the bytecode content |
| 96195 | 96432 | ** of a prepared statement. |
| 96196 | 96433 | */ |
| 96197 | | -#ifdef SQLITE_ENABLE_BYTECODE_VTAB |
| 96198 | 96434 | /* #include "sqliteInt.h" */ |
| 96435 | +#if defined(SQLITE_ENABLE_BYTECODE_VTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 96199 | 96436 | /* #include "vdbeInt.h" */ |
| 96200 | 96437 | |
| 96201 | 96438 | /* An instance of the bytecode() table-valued function. |
| 96202 | 96439 | */ |
| 96203 | 96440 | typedef struct bytecodevtab bytecodevtab; |
| | @@ -96598,10 +96835,12 @@ |
| 96598 | 96835 | if( rc==SQLITE_OK ){ |
| 96599 | 96836 | rc = sqlite3_create_module(db, "tables_used", &bytecodevtabModule, &db); |
| 96600 | 96837 | } |
| 96601 | 96838 | return rc; |
| 96602 | 96839 | } |
| 96840 | +#elif defined(SQLITE_ENABLE_BYTECODE_VTAB) |
| 96841 | +SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ return SQLITE_OK; } |
| 96603 | 96842 | #endif /* SQLITE_ENABLE_BYTECODE_VTAB */ |
| 96604 | 96843 | |
| 96605 | 96844 | /************** End of vdbevtab.c ********************************************/ |
| 96606 | 96845 | /************** Begin file memjournal.c **************************************/ |
| 96607 | 96846 | /* |
| | @@ -97243,10 +97482,47 @@ |
| 97243 | 97482 | } |
| 97244 | 97483 | p = p->pPrior; |
| 97245 | 97484 | }while( p!=0 ); |
| 97246 | 97485 | return WRC_Continue; |
| 97247 | 97486 | } |
| 97487 | + |
| 97488 | +/* Increase the walkerDepth when entering a subquery, and |
| 97489 | +** descrease when leaving the subquery. |
| 97490 | +*/ |
| 97491 | +SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){ |
| 97492 | + UNUSED_PARAMETER(pSelect); |
| 97493 | + pWalker->walkerDepth++; |
| 97494 | + return WRC_Continue; |
| 97495 | +} |
| 97496 | +SQLITE_PRIVATE void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){ |
| 97497 | + UNUSED_PARAMETER(pSelect); |
| 97498 | + pWalker->walkerDepth--; |
| 97499 | +} |
| 97500 | + |
| 97501 | + |
| 97502 | +/* |
| 97503 | +** No-op routine for the parse-tree walker. |
| 97504 | +** |
| 97505 | +** When this routine is the Walker.xExprCallback then expression trees |
| 97506 | +** are walked without any actions being taken at each node. Presumably, |
| 97507 | +** when this routine is used for Walker.xExprCallback then |
| 97508 | +** Walker.xSelectCallback is set to do something useful for every |
| 97509 | +** subquery in the parser tree. |
| 97510 | +*/ |
| 97511 | +SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){ |
| 97512 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 97513 | + return WRC_Continue; |
| 97514 | +} |
| 97515 | + |
| 97516 | +/* |
| 97517 | +** No-op routine for the parse-tree walker for SELECT statements. |
| 97518 | +** subquery in the parser tree. |
| 97519 | +*/ |
| 97520 | +SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){ |
| 97521 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 97522 | + return WRC_Continue; |
| 97523 | +} |
| 97248 | 97524 | |
| 97249 | 97525 | /************** End of walker.c **********************************************/ |
| 97250 | 97526 | /************** Begin file resolve.c *****************************************/ |
| 97251 | 97527 | /* |
| 97252 | 97528 | ** 2008 August 18 |
| | @@ -97272,10 +97548,12 @@ |
| 97272 | 97548 | ** This needs to occur when copying a TK_AGG_FUNCTION node from an |
| 97273 | 97549 | ** outer query into an inner subquery. |
| 97274 | 97550 | ** |
| 97275 | 97551 | ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) |
| 97276 | 97552 | ** is a helper function - a callback for the tree walker. |
| 97553 | +** |
| 97554 | +** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c |
| 97277 | 97555 | */ |
| 97278 | 97556 | static int incrAggDepth(Walker *pWalker, Expr *pExpr){ |
| 97279 | 97557 | if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n; |
| 97280 | 97558 | return WRC_Continue; |
| 97281 | 97559 | } |
| | @@ -102902,10 +103180,17 @@ |
| 102902 | 103180 | } |
| 102903 | 103181 | setDoNotMergeFlagOnCopy(v); |
| 102904 | 103182 | sqlite3VdbeResolveLabel(v, endCoalesce); |
| 102905 | 103183 | break; |
| 102906 | 103184 | } |
| 103185 | + case INLINEFUNC_iif: { |
| 103186 | + Expr caseExpr; |
| 103187 | + memset(&caseExpr, 0, sizeof(caseExpr)); |
| 103188 | + caseExpr.op = TK_CASE; |
| 103189 | + caseExpr.x.pList = pFarg; |
| 103190 | + return sqlite3ExprCodeTarget(pParse, &caseExpr, target); |
| 103191 | + } |
| 102907 | 103192 | |
| 102908 | 103193 | default: { |
| 102909 | 103194 | /* The UNLIKELY() function is a no-op. The result is the value |
| 102910 | 103195 | ** of the first argument. |
| 102911 | 103196 | */ |
| | @@ -103006,11 +103291,14 @@ |
| 103006 | 103291 | op = pExpr->op; |
| 103007 | 103292 | } |
| 103008 | 103293 | switch( op ){ |
| 103009 | 103294 | case TK_AGG_COLUMN: { |
| 103010 | 103295 | AggInfo *pAggInfo = pExpr->pAggInfo; |
| 103011 | | - struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; |
| 103296 | + struct AggInfo_col *pCol; |
| 103297 | + assert( pAggInfo!=0 ); |
| 103298 | + assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn ); |
| 103299 | + pCol = &pAggInfo->aCol[pExpr->iAgg]; |
| 103012 | 103300 | if( !pAggInfo->directMode ){ |
| 103013 | 103301 | assert( pCol->iMem>0 ); |
| 103014 | 103302 | return pCol->iMem; |
| 103015 | 103303 | }else if( pAggInfo->useSortingIdx ){ |
| 103016 | 103304 | Table *pTab = pCol->pTab; |
| | @@ -103306,11 +103594,14 @@ |
| 103306 | 103594 | sqlite3VdbeJumpHere(v, addr); |
| 103307 | 103595 | break; |
| 103308 | 103596 | } |
| 103309 | 103597 | case TK_AGG_FUNCTION: { |
| 103310 | 103598 | AggInfo *pInfo = pExpr->pAggInfo; |
| 103311 | | - if( pInfo==0 ){ |
| 103599 | + if( pInfo==0 |
| 103600 | + || NEVER(pExpr->iAgg<0) |
| 103601 | + || NEVER(pExpr->iAgg>=pInfo->nFunc) |
| 103602 | + ){ |
| 103312 | 103603 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 103313 | 103604 | sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); |
| 103314 | 103605 | }else{ |
| 103315 | 103606 | return pInfo->aFunc[pExpr->iAgg].iMem; |
| 103316 | 103607 | } |
| | @@ -103684,11 +103975,11 @@ |
| 103684 | 103975 | assert( pExpr->affExpr==OE_Rollback |
| 103685 | 103976 | || pExpr->affExpr==OE_Abort |
| 103686 | 103977 | || pExpr->affExpr==OE_Fail |
| 103687 | 103978 | || pExpr->affExpr==OE_Ignore |
| 103688 | 103979 | ); |
| 103689 | | - if( !pParse->pTriggerTab ){ |
| 103980 | + if( !pParse->pTriggerTab && !pParse->nested ){ |
| 103690 | 103981 | sqlite3ErrorMsg(pParse, |
| 103691 | 103982 | "RAISE() may only be used within a trigger-program"); |
| 103692 | 103983 | return 0; |
| 103693 | 103984 | } |
| 103694 | 103985 | if( pExpr->affExpr==OE_Abort ){ |
| | @@ -103698,12 +103989,13 @@ |
| 103698 | 103989 | if( pExpr->affExpr==OE_Ignore ){ |
| 103699 | 103990 | sqlite3VdbeAddOp4( |
| 103700 | 103991 | v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); |
| 103701 | 103992 | VdbeCoverage(v); |
| 103702 | 103993 | }else{ |
| 103703 | | - sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER, |
| 103704 | | - pExpr->affExpr, pExpr->u.zToken, 0, 0); |
| 103994 | + sqlite3HaltConstraint(pParse, |
| 103995 | + pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR, |
| 103996 | + pExpr->affExpr, pExpr->u.zToken, 0, 0); |
| 103705 | 103997 | } |
| 103706 | 103998 | |
| 103707 | 103999 | break; |
| 103708 | 104000 | } |
| 103709 | 104001 | #endif |
| | @@ -105061,19 +105353,10 @@ |
| 105061 | 105353 | } |
| 105062 | 105354 | } |
| 105063 | 105355 | } |
| 105064 | 105356 | return WRC_Continue; |
| 105065 | 105357 | } |
| 105066 | | -static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ |
| 105067 | | - UNUSED_PARAMETER(pSelect); |
| 105068 | | - pWalker->walkerDepth++; |
| 105069 | | - return WRC_Continue; |
| 105070 | | -} |
| 105071 | | -static void analyzeAggregatesInSelectEnd(Walker *pWalker, Select *pSelect){ |
| 105072 | | - UNUSED_PARAMETER(pSelect); |
| 105073 | | - pWalker->walkerDepth--; |
| 105074 | | -} |
| 105075 | 105358 | |
| 105076 | 105359 | /* |
| 105077 | 105360 | ** Analyze the pExpr expression looking for aggregate functions and |
| 105078 | 105361 | ** for variables that need to be added to AggInfo object that pNC->pAggInfo |
| 105079 | 105362 | ** points to. Additional entries are made on the AggInfo object as |
| | @@ -105083,12 +105366,12 @@ |
| 105083 | 105366 | ** analyzed by sqlite3ResolveExprNames(). |
| 105084 | 105367 | */ |
| 105085 | 105368 | SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ |
| 105086 | 105369 | Walker w; |
| 105087 | 105370 | w.xExprCallback = analyzeAggregate; |
| 105088 | | - w.xSelectCallback = analyzeAggregatesInSelect; |
| 105089 | | - w.xSelectCallback2 = analyzeAggregatesInSelectEnd; |
| 105371 | + w.xSelectCallback = sqlite3WalkerDepthIncrease; |
| 105372 | + w.xSelectCallback2 = sqlite3WalkerDepthDecrease; |
| 105090 | 105373 | w.walkerDepth = 0; |
| 105091 | 105374 | w.u.pNC = pNC; |
| 105092 | 105375 | w.pParse = 0; |
| 105093 | 105376 | assert( pNC->pSrcList!=0 ); |
| 105094 | 105377 | sqlite3WalkExpr(&w, pExpr); |
| | @@ -105323,11 +105606,14 @@ |
| 105323 | 105606 | if( !zName ) goto exit_rename_table; |
| 105324 | 105607 | |
| 105325 | 105608 | /* Check that a table or index named 'zName' does not already exist |
| 105326 | 105609 | ** in database iDb. If so, this is an error. |
| 105327 | 105610 | */ |
| 105328 | | - if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){ |
| 105611 | + if( sqlite3FindTable(db, zName, zDb) |
| 105612 | + || sqlite3FindIndex(db, zName, zDb) |
| 105613 | + || sqlite3IsShadowTableOf(db, pTab, zName) |
| 105614 | + ){ |
| 105329 | 105615 | sqlite3ErrorMsg(pParse, |
| 105330 | 105616 | "there is already another table or index with this name: %s", zName); |
| 105331 | 105617 | goto exit_rename_table; |
| 105332 | 105618 | } |
| 105333 | 105619 | |
| | @@ -105454,10 +105740,26 @@ |
| 105454 | 105740 | exit_rename_table: |
| 105455 | 105741 | sqlite3SrcListDelete(db, pSrc); |
| 105456 | 105742 | sqlite3DbFree(db, zName); |
| 105457 | 105743 | db->mDbFlags = savedDbFlags; |
| 105458 | 105744 | } |
| 105745 | + |
| 105746 | +/* |
| 105747 | +** Write code that will raise an error if the table described by |
| 105748 | +** zDb and zTab is not empty. |
| 105749 | +*/ |
| 105750 | +static void sqlite3ErrorIfNotEmpty( |
| 105751 | + Parse *pParse, /* Parsing context */ |
| 105752 | + const char *zDb, /* Schema holding the table */ |
| 105753 | + const char *zTab, /* Table to check for empty */ |
| 105754 | + const char *zErr /* Error message text */ |
| 105755 | +){ |
| 105756 | + sqlite3NestedParse(pParse, |
| 105757 | + "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"", |
| 105758 | + zErr, zDb, zTab |
| 105759 | + ); |
| 105760 | +} |
| 105459 | 105761 | |
| 105460 | 105762 | /* |
| 105461 | 105763 | ** This function is called after an "ALTER TABLE ... ADD" statement |
| 105462 | 105764 | ** has been parsed. Argument pColDef contains the text of the new |
| 105463 | 105765 | ** column definition. |
| | @@ -105507,11 +105809,12 @@ |
| 105507 | 105809 | if( pCol->colFlags & COLFLAG_PRIMKEY ){ |
| 105508 | 105810 | sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column"); |
| 105509 | 105811 | return; |
| 105510 | 105812 | } |
| 105511 | 105813 | if( pNew->pIndex ){ |
| 105512 | | - sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column"); |
| 105814 | + sqlite3ErrorMsg(pParse, |
| 105815 | + "Cannot add a UNIQUE column"); |
| 105513 | 105816 | return; |
| 105514 | 105817 | } |
| 105515 | 105818 | if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){ |
| 105516 | 105819 | /* If the default value for the new column was specified with a |
| 105517 | 105820 | ** literal NULL, then set pDflt to 0. This simplifies checking |
| | @@ -105520,19 +105823,18 @@ |
| 105520 | 105823 | assert( pDflt==0 || pDflt->op==TK_SPAN ); |
| 105521 | 105824 | if( pDflt && pDflt->pLeft->op==TK_NULL ){ |
| 105522 | 105825 | pDflt = 0; |
| 105523 | 105826 | } |
| 105524 | 105827 | if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){ |
| 105525 | | - sqlite3ErrorMsg(pParse, |
| 105828 | + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, |
| 105526 | 105829 | "Cannot add a REFERENCES column with non-NULL default value"); |
| 105527 | | - return; |
| 105528 | 105830 | } |
| 105529 | 105831 | if( pCol->notNull && !pDflt ){ |
| 105530 | | - sqlite3ErrorMsg(pParse, |
| 105832 | + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, |
| 105531 | 105833 | "Cannot add a NOT NULL column with default value NULL"); |
| 105532 | | - return; |
| 105533 | 105834 | } |
| 105835 | + |
| 105534 | 105836 | |
| 105535 | 105837 | /* Ensure the default expression is something that sqlite3ValueFromExpr() |
| 105536 | 105838 | ** can handle (i.e. not CURRENT_TIME etc.) |
| 105537 | 105839 | */ |
| 105538 | 105840 | if( pDflt ){ |
| | @@ -105543,18 +105845,17 @@ |
| 105543 | 105845 | if( rc!=SQLITE_OK ){ |
| 105544 | 105846 | assert( db->mallocFailed == 1 ); |
| 105545 | 105847 | return; |
| 105546 | 105848 | } |
| 105547 | 105849 | if( !pVal ){ |
| 105548 | | - sqlite3ErrorMsg(pParse,"Cannot add a column with non-constant default"); |
| 105549 | | - return; |
| 105850 | + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, |
| 105851 | + "Cannot add a column with non-constant default"); |
| 105550 | 105852 | } |
| 105551 | 105853 | sqlite3ValueFree(pVal); |
| 105552 | 105854 | } |
| 105553 | 105855 | }else if( pCol->colFlags & COLFLAG_STORED ){ |
| 105554 | | - sqlite3ErrorMsg(pParse, "cannot add a STORED column"); |
| 105555 | | - return; |
| 105856 | + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, "cannot add a STORED column"); |
| 105556 | 105857 | } |
| 105557 | 105858 | |
| 105558 | 105859 | |
| 105559 | 105860 | /* Modify the CREATE TABLE statement. */ |
| 105560 | 105861 | zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); |
| | @@ -110055,26 +110356,43 @@ |
| 110055 | 110356 | ** exists */ |
| 110056 | 110357 | if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){ |
| 110057 | 110358 | return 0; |
| 110058 | 110359 | } |
| 110059 | 110360 | #endif |
| 110060 | | - while(1){ |
| 110061 | | - for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
| 110062 | | - int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
| 110063 | | - if( zDatabase==0 || sqlite3DbIsNamed(db, j, zDatabase) ){ |
| 110064 | | - assert( sqlite3SchemaMutexHeld(db, j, 0) ); |
| 110065 | | - p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName); |
| 110066 | | - if( p ) return p; |
| 110067 | | - } |
| 110068 | | - } |
| 110069 | | - /* Not found. If the name we were looking for was temp.sqlite_master |
| 110070 | | - ** then change the name to sqlite_temp_master and try again. */ |
| 110071 | | - if( sqlite3StrICmp(zName, MASTER_NAME)!=0 ) break; |
| 110072 | | - if( sqlite3_stricmp(zDatabase, db->aDb[1].zDbSName)!=0 ) break; |
| 110073 | | - zName = TEMP_MASTER_NAME; |
| 110074 | | - } |
| 110075 | | - return 0; |
| 110361 | + if( zDatabase ){ |
| 110362 | + for(i=0; i<db->nDb; i++){ |
| 110363 | + if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break; |
| 110364 | + } |
| 110365 | + if( i>=db->nDb ){ |
| 110366 | + /* No match against the official names. But always match "main" |
| 110367 | + ** to schema 0 as a legacy fallback. */ |
| 110368 | + if( sqlite3StrICmp(zDatabase,"main")==0 ){ |
| 110369 | + i = 0; |
| 110370 | + }else{ |
| 110371 | + return 0; |
| 110372 | + } |
| 110373 | + } |
| 110374 | + p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); |
| 110375 | + if( p==0 && i==1 && sqlite3StrICmp(zName, MASTER_NAME)==0 ){ |
| 110376 | + /* All temp.sqlite_master to be an alias for sqlite_temp_master */ |
| 110377 | + p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, TEMP_MASTER_NAME); |
| 110378 | + } |
| 110379 | + }else{ |
| 110380 | + /* Match against TEMP first */ |
| 110381 | + p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName); |
| 110382 | + if( p ) return p; |
| 110383 | + /* The main database is second */ |
| 110384 | + p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName); |
| 110385 | + if( p ) return p; |
| 110386 | + /* Attached databases are in order of attachment */ |
| 110387 | + for(i=2; i<db->nDb; i++){ |
| 110388 | + assert( sqlite3SchemaMutexHeld(db, i, 0) ); |
| 110389 | + p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); |
| 110390 | + if( p ) break; |
| 110391 | + } |
| 110392 | + } |
| 110393 | + return p; |
| 110076 | 110394 | } |
| 110077 | 110395 | |
| 110078 | 110396 | /* |
| 110079 | 110397 | ** Locate the in-memory structure that describes a particular database |
| 110080 | 110398 | ** table given the name of that table and (optionally) the name of the |
| | @@ -111874,10 +112192,32 @@ |
| 111874 | 112192 | assert( pPk->nColumn==j ); |
| 111875 | 112193 | assert( pTab->nNVCol<=j ); |
| 111876 | 112194 | recomputeColumnsNotIndexed(pPk); |
| 111877 | 112195 | } |
| 111878 | 112196 | |
| 112197 | + |
| 112198 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 112199 | +/* |
| 112200 | +** Return true if pTab is a virtual table and zName is a shadow table name |
| 112201 | +** for that virtual table. |
| 112202 | +*/ |
| 112203 | +SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){ |
| 112204 | + int nName; /* Length of zName */ |
| 112205 | + Module *pMod; /* Module for the virtual table */ |
| 112206 | + |
| 112207 | + if( !IsVirtual(pTab) ) return 0; |
| 112208 | + nName = sqlite3Strlen30(pTab->zName); |
| 112209 | + if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0; |
| 112210 | + if( zName[nName]!='_' ) return 0; |
| 112211 | + pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]); |
| 112212 | + if( pMod==0 ) return 0; |
| 112213 | + if( pMod->pModule->iVersion<3 ) return 0; |
| 112214 | + if( pMod->pModule->xShadowName==0 ) return 0; |
| 112215 | + return pMod->pModule->xShadowName(zName+nName+1); |
| 112216 | +} |
| 112217 | +#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 112218 | + |
| 111879 | 112219 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 111880 | 112220 | /* |
| 111881 | 112221 | ** Return true if zName is a shadow table name in the current database |
| 111882 | 112222 | ** connection. |
| 111883 | 112223 | ** |
| | @@ -111885,26 +112225,21 @@ |
| 111885 | 112225 | ** restored to its original value prior to this routine returning. |
| 111886 | 112226 | */ |
| 111887 | 112227 | SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName){ |
| 111888 | 112228 | char *zTail; /* Pointer to the last "_" in zName */ |
| 111889 | 112229 | Table *pTab; /* Table that zName is a shadow of */ |
| 111890 | | - Module *pMod; /* Module for the virtual table */ |
| 111891 | | - |
| 111892 | 112230 | zTail = strrchr(zName, '_'); |
| 111893 | 112231 | if( zTail==0 ) return 0; |
| 111894 | 112232 | *zTail = 0; |
| 111895 | 112233 | pTab = sqlite3FindTable(db, zName, 0); |
| 111896 | 112234 | *zTail = '_'; |
| 111897 | 112235 | if( pTab==0 ) return 0; |
| 111898 | 112236 | if( !IsVirtual(pTab) ) return 0; |
| 111899 | | - pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]); |
| 111900 | | - if( pMod==0 ) return 0; |
| 111901 | | - if( pMod->pModule->iVersion<3 ) return 0; |
| 111902 | | - if( pMod->pModule->xShadowName==0 ) return 0; |
| 111903 | | - return pMod->pModule->xShadowName(zTail+1); |
| 112237 | + return sqlite3IsShadowTableOf(db, pTab, zName); |
| 111904 | 112238 | } |
| 111905 | 112239 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 112240 | + |
| 111906 | 112241 | |
| 111907 | 112242 | #ifdef SQLITE_DEBUG |
| 111908 | 112243 | /* |
| 111909 | 112244 | ** Mark all nodes of an expression as EP_Immutable, indicating that |
| 111910 | 112245 | ** they should not be changed. Expressions attached to a table or |
| | @@ -114373,11 +114708,11 @@ |
| 114373 | 114708 | pParse->rc = rc; |
| 114374 | 114709 | return 1; |
| 114375 | 114710 | } |
| 114376 | 114711 | db->aDb[1].pBt = pBt; |
| 114377 | 114712 | assert( db->aDb[1].pSchema ); |
| 114378 | | - if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ |
| 114713 | + if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){ |
| 114379 | 114714 | sqlite3OomFault(db); |
| 114380 | 114715 | return 1; |
| 114381 | 114716 | } |
| 114382 | 114717 | } |
| 114383 | 114718 | return 0; |
| | @@ -114484,11 +114819,11 @@ |
| 114484 | 114819 | char *p4, /* Error message */ |
| 114485 | 114820 | i8 p4type, /* P4_STATIC or P4_TRANSIENT */ |
| 114486 | 114821 | u8 p5Errmsg /* P5_ErrMsg type */ |
| 114487 | 114822 | ){ |
| 114488 | 114823 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 114489 | | - assert( (errCode&0xff)==SQLITE_CONSTRAINT ); |
| 114824 | + assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested ); |
| 114490 | 114825 | if( onError==OE_Abort ){ |
| 114491 | 114826 | sqlite3MayAbort(pParse); |
| 114492 | 114827 | } |
| 114493 | 114828 | sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type); |
| 114494 | 114829 | sqlite3VdbeChangeP5(v, p5Errmsg); |
| | @@ -116197,10 +116532,11 @@ |
| 116197 | 116532 | VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName)); |
| 116198 | 116533 | r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1, |
| 116199 | 116534 | &iPartIdxLabel, pPrior, r1); |
| 116200 | 116535 | sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1, |
| 116201 | 116536 | pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn); |
| 116537 | + sqlite3VdbeChangeP5(v, 1); /* Cause IdxDelete to error if no entry found */ |
| 116202 | 116538 | sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); |
| 116203 | 116539 | pPrior = pIdx; |
| 116204 | 116540 | } |
| 116205 | 116541 | } |
| 116206 | 116542 | |
| | @@ -117590,11 +117926,11 @@ |
| 117590 | 117926 | if( (cntExpand&(cntExpand-1))==0 ){ |
| 117591 | 117927 | /* Grow the size of the output buffer only on substitutions |
| 117592 | 117928 | ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */ |
| 117593 | 117929 | u8 *zOld; |
| 117594 | 117930 | zOld = zOut; |
| 117595 | | - zOut = sqlite3_realloc64(zOut, (int)nOut + (nOut - nStr - 1)); |
| 117931 | + zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1)); |
| 117596 | 117932 | if( zOut==0 ){ |
| 117597 | 117933 | sqlite3_result_error_nomem(context); |
| 117598 | 117934 | sqlite3_free(zOld); |
| 117599 | 117935 | return; |
| 117600 | 117936 | } |
| | @@ -118287,11 +118623,11 @@ |
| 118287 | 118623 | FUNCTION(round, 2, 0, 0, roundFunc ), |
| 118288 | 118624 | #endif |
| 118289 | 118625 | FUNCTION(upper, 1, 0, 0, upperFunc ), |
| 118290 | 118626 | FUNCTION(lower, 1, 0, 0, lowerFunc ), |
| 118291 | 118627 | FUNCTION(hex, 1, 0, 0, hexFunc ), |
| 118292 | | - INLINE_FUNC(ifnull, 2, INLINEFUNC_coalesce, SQLITE_FUNC_COALESCE), |
| 118628 | + INLINE_FUNC(ifnull, 2, INLINEFUNC_coalesce, 0 ), |
| 118293 | 118629 | VFUNCTION(random, 0, 0, 0, randomFunc ), |
| 118294 | 118630 | VFUNCTION(randomblob, 1, 0, 0, randomBlob ), |
| 118295 | 118631 | FUNCTION(nullif, 2, 0, 1, nullifFunc ), |
| 118296 | 118632 | DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ), |
| 118297 | 118633 | DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), |
| | @@ -118327,11 +118663,12 @@ |
| 118327 | 118663 | #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION |
| 118328 | 118664 | FUNCTION(unknown, -1, 0, 0, unknownFunc ), |
| 118329 | 118665 | #endif |
| 118330 | 118666 | FUNCTION(coalesce, 1, 0, 0, 0 ), |
| 118331 | 118667 | FUNCTION(coalesce, 0, 0, 0, 0 ), |
| 118332 | | - INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, SQLITE_FUNC_COALESCE), |
| 118668 | + INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ), |
| 118669 | + INLINE_FUNC(iif, 3, INLINEFUNC_iif, 0 ), |
| 118333 | 118670 | }; |
| 118334 | 118671 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 118335 | 118672 | sqlite3AlterFunctions(); |
| 118336 | 118673 | #endif |
| 118337 | 118674 | sqlite3WindowFunctions(); |
| | @@ -121752,11 +122089,11 @@ |
| 121752 | 122089 | } |
| 121753 | 122090 | if( bAffinityDone==0 && (pUpIdx==0 || pUpIdx==pIdx) ){ |
| 121754 | 122091 | sqlite3TableAffinity(v, pTab, regNewData+1); |
| 121755 | 122092 | bAffinityDone = 1; |
| 121756 | 122093 | } |
| 121757 | | - VdbeNoopComment((v, "uniqueness check for %s", pIdx->zName)); |
| 122094 | + VdbeNoopComment((v, "prep index %s", pIdx->zName)); |
| 121758 | 122095 | iThisCur = iIdxCur+ix; |
| 121759 | 122096 | |
| 121760 | 122097 | |
| 121761 | 122098 | /* Skip partial indices for which the WHERE clause is not true */ |
| 121762 | 122099 | if( pIdx->pPartIdxWhere ){ |
| | @@ -125555,11 +125892,11 @@ |
| 125555 | 125892 | }else{ |
| 125556 | 125893 | /* Malloc may fail when setting the page-size, as there is an internal |
| 125557 | 125894 | ** buffer that the pager module resizes using sqlite3_realloc(). |
| 125558 | 125895 | */ |
| 125559 | 125896 | db->nextPagesize = sqlite3Atoi(zRight); |
| 125560 | | - if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){ |
| 125897 | + if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){ |
| 125561 | 125898 | sqlite3OomFault(db); |
| 125562 | 125899 | } |
| 125563 | 125900 | } |
| 125564 | 125901 | break; |
| 125565 | 125902 | } |
| | @@ -133724,33 +134061,10 @@ |
| 133724 | 134061 | } |
| 133725 | 134062 | } |
| 133726 | 134063 | return WRC_Continue; |
| 133727 | 134064 | } |
| 133728 | 134065 | |
| 133729 | | -/* |
| 133730 | | -** No-op routine for the parse-tree walker. |
| 133731 | | -** |
| 133732 | | -** When this routine is the Walker.xExprCallback then expression trees |
| 133733 | | -** are walked without any actions being taken at each node. Presumably, |
| 133734 | | -** when this routine is used for Walker.xExprCallback then |
| 133735 | | -** Walker.xSelectCallback is set to do something useful for every |
| 133736 | | -** subquery in the parser tree. |
| 133737 | | -*/ |
| 133738 | | -SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){ |
| 133739 | | - UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 133740 | | - return WRC_Continue; |
| 133741 | | -} |
| 133742 | | - |
| 133743 | | -/* |
| 133744 | | -** No-op routine for the parse-tree walker for SELECT statements. |
| 133745 | | -** subquery in the parser tree. |
| 133746 | | -*/ |
| 133747 | | -SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){ |
| 133748 | | - UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 133749 | | - return WRC_Continue; |
| 133750 | | -} |
| 133751 | | - |
| 133752 | 134066 | #if SQLITE_DEBUG |
| 133753 | 134067 | /* |
| 133754 | 134068 | ** Always assert. This xSelectCallback2 implementation proves that the |
| 133755 | 134069 | ** xSelectCallback2 is never invoked. |
| 133756 | 134070 | */ |
| | @@ -134917,11 +135231,11 @@ |
| 134917 | 135231 | sAggInfo.mxReg = pParse->nMem; |
| 134918 | 135232 | if( db->mallocFailed ) goto select_end; |
| 134919 | 135233 | #if SELECTTRACE_ENABLED |
| 134920 | 135234 | if( sqlite3SelectTrace & 0x400 ){ |
| 134921 | 135235 | int ii; |
| 134922 | | - SELECTTRACE(0x400,pParse,p,("After aggregate analysis:\n")); |
| 135236 | + SELECTTRACE(0x400,pParse,p,("After aggregate analysis %p:\n", &sAggInfo)); |
| 134923 | 135237 | sqlite3TreeViewSelect(0, p, 0); |
| 134924 | 135238 | for(ii=0; ii<sAggInfo.nColumn; ii++){ |
| 134925 | 135239 | sqlite3DebugPrintf("agg-column[%d] iMem=%d\n", |
| 134926 | 135240 | ii, sAggInfo.aCol[ii].iMem); |
| 134927 | 135241 | sqlite3TreeViewExpr(0, sAggInfo.aCol[ii].pExpr, 0); |
| | @@ -135193,17 +135507,19 @@ |
| 135193 | 135507 | ** |
| 135194 | 135508 | ** In practice the KeyInfo structure will not be used. It is only |
| 135195 | 135509 | ** passed to keep OP_OpenRead happy. |
| 135196 | 135510 | */ |
| 135197 | 135511 | if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab); |
| 135198 | | - for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 135199 | | - if( pIdx->bUnordered==0 |
| 135200 | | - && pIdx->szIdxRow<pTab->szTabRow |
| 135201 | | - && pIdx->pPartIdxWhere==0 |
| 135202 | | - && (!pBest || pIdx->szIdxRow<pBest->szIdxRow) |
| 135203 | | - ){ |
| 135204 | | - pBest = pIdx; |
| 135512 | + if( !p->pSrc->a[0].fg.notIndexed ){ |
| 135513 | + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 135514 | + if( pIdx->bUnordered==0 |
| 135515 | + && pIdx->szIdxRow<pTab->szTabRow |
| 135516 | + && pIdx->pPartIdxWhere==0 |
| 135517 | + && (!pBest || pIdx->szIdxRow<pBest->szIdxRow) |
| 135518 | + ){ |
| 135519 | + pBest = pIdx; |
| 135520 | + } |
| 135205 | 135521 | } |
| 135206 | 135522 | } |
| 135207 | 135523 | if( pBest ){ |
| 135208 | 135524 | iRoot = pBest->tnum; |
| 135209 | 135525 | pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest); |
| | @@ -135378,11 +135694,11 @@ |
| 135378 | 135694 | need = nCol; |
| 135379 | 135695 | } |
| 135380 | 135696 | if( p->nData + need > p->nAlloc ){ |
| 135381 | 135697 | char **azNew; |
| 135382 | 135698 | p->nAlloc = p->nAlloc*2 + need; |
| 135383 | | - azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc ); |
| 135699 | + azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc ); |
| 135384 | 135700 | if( azNew==0 ) goto malloc_failed; |
| 135385 | 135701 | p->azResult = azNew; |
| 135386 | 135702 | } |
| 135387 | 135703 | |
| 135388 | 135704 | /* If this is the first row, then generate an extra row containing |
| | @@ -135487,11 +135803,11 @@ |
| 135487 | 135803 | sqlite3_free_table(&res.azResult[1]); |
| 135488 | 135804 | return rc; |
| 135489 | 135805 | } |
| 135490 | 135806 | if( res.nAlloc>res.nData ){ |
| 135491 | 135807 | char **azNew; |
| 135492 | | - azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData ); |
| 135808 | + azNew = sqlite3Realloc( res.azResult, sizeof(char*)*res.nData ); |
| 135493 | 135809 | if( azNew==0 ){ |
| 135494 | 135810 | sqlite3_free_table(&res.azResult[1]); |
| 135495 | 135811 | db->errCode = SQLITE_NOMEM; |
| 135496 | 135812 | return SQLITE_NOMEM_BKPT; |
| 135497 | 135813 | } |
| | @@ -136777,14 +137093,14 @@ |
| 136777 | 137093 | ** Therefore, the P4 parameter is only required if the default value for |
| 136778 | 137094 | ** the column is a literal number, string or null. The sqlite3ValueFromExpr() |
| 136779 | 137095 | ** function is capable of transforming these types of expressions into |
| 136780 | 137096 | ** sqlite3_value objects. |
| 136781 | 137097 | ** |
| 136782 | | -** If parameter iReg is not negative, code an OP_RealAffinity instruction |
| 136783 | | -** on register iReg. This is used when an equivalent integer value is |
| 136784 | | -** stored in place of an 8-byte floating point value in order to save |
| 136785 | | -** space. |
| 137098 | +** If column as REAL affinity and the table is an ordinary b-tree table |
| 137099 | +** (not a virtual table) then the value might have been stored as an |
| 137100 | +** integer. In that case, add an OP_RealAffinity opcode to make sure |
| 137101 | +** it has been converted into REAL. |
| 136786 | 137102 | */ |
| 136787 | 137103 | SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){ |
| 136788 | 137104 | assert( pTab!=0 ); |
| 136789 | 137105 | if( !pTab->pSelect ){ |
| 136790 | 137106 | sqlite3_value *pValue = 0; |
| | @@ -136797,11 +137113,11 @@ |
| 136797 | 137113 | if( pValue ){ |
| 136798 | 137114 | sqlite3VdbeAppendP4(v, pValue, P4_MEM); |
| 136799 | 137115 | } |
| 136800 | 137116 | } |
| 136801 | 137117 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 136802 | | - if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){ |
| 137118 | + if( pTab->aCol[i].affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){ |
| 136803 | 137119 | sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); |
| 136804 | 137120 | } |
| 136805 | 137121 | #endif |
| 136806 | 137122 | } |
| 136807 | 137123 | |
| | @@ -138439,11 +138755,11 @@ |
| 138439 | 138755 | db->mDbFlags = saved_mDbFlags; |
| 138440 | 138756 | db->flags = saved_flags; |
| 138441 | 138757 | db->nChange = saved_nChange; |
| 138442 | 138758 | db->nTotalChange = saved_nTotalChange; |
| 138443 | 138759 | db->mTrace = saved_mTrace; |
| 138444 | | - sqlite3BtreeSetPageSize(pMain, -1, -1, 1); |
| 138760 | + sqlite3BtreeSetPageSize(pMain, -1, 0, 1); |
| 138445 | 138761 | |
| 138446 | 138762 | /* Currently there is an SQL level transaction open on the vacuum |
| 138447 | 138763 | ** database. No locks are held on any other files (since the main file |
| 138448 | 138764 | ** was committed at the btree level). So it safe to end the transaction |
| 138449 | 138765 | ** by manually setting the autoCommit flag to true and detaching the |
| | @@ -139646,11 +139962,11 @@ |
| 139646 | 139962 | assert( IsVirtual(pTab) ); |
| 139647 | 139963 | for(i=0; i<pToplevel->nVtabLock; i++){ |
| 139648 | 139964 | if( pTab==pToplevel->apVtabLock[i] ) return; |
| 139649 | 139965 | } |
| 139650 | 139966 | n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]); |
| 139651 | | - apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n); |
| 139967 | + apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n); |
| 139652 | 139968 | if( apVtabLock ){ |
| 139653 | 139969 | pToplevel->apVtabLock = apVtabLock; |
| 139654 | 139970 | pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab; |
| 139655 | 139971 | }else{ |
| 139656 | 139972 | sqlite3OomFault(pToplevel->db); |
| | @@ -150938,24 +151254,47 @@ |
| 150938 | 151254 | ){ |
| 150939 | 151255 | if( pAppend ){ |
| 150940 | 151256 | int i; |
| 150941 | 151257 | int nInit = pList ? pList->nExpr : 0; |
| 150942 | 151258 | for(i=0; i<pAppend->nExpr; i++){ |
| 150943 | | - int iDummy; |
| 150944 | 151259 | Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0); |
| 150945 | 151260 | assert( pDup==0 || !ExprHasProperty(pDup, EP_MemToken) ); |
| 150946 | | - if( bIntToNull && pDup && sqlite3ExprIsInteger(pDup, &iDummy) ){ |
| 150947 | | - pDup->op = TK_NULL; |
| 150948 | | - pDup->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse); |
| 150949 | | - pDup->u.zToken = 0; |
| 151261 | + if( bIntToNull && pDup ){ |
| 151262 | + int iDummy; |
| 151263 | + Expr *pSub; |
| 151264 | + for(pSub=pDup; ExprHasProperty(pSub, EP_Skip); pSub=pSub->pLeft){ |
| 151265 | + assert( pSub ); |
| 151266 | + } |
| 151267 | + if( sqlite3ExprIsInteger(pSub, &iDummy) ){ |
| 151268 | + pSub->op = TK_NULL; |
| 151269 | + pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse); |
| 151270 | + pSub->u.zToken = 0; |
| 151271 | + } |
| 150950 | 151272 | } |
| 150951 | 151273 | pList = sqlite3ExprListAppend(pParse, pList, pDup); |
| 150952 | 151274 | if( pList ) pList->a[nInit+i].sortFlags = pAppend->a[i].sortFlags; |
| 150953 | 151275 | } |
| 150954 | 151276 | } |
| 150955 | 151277 | return pList; |
| 150956 | 151278 | } |
| 151279 | + |
| 151280 | +/* |
| 151281 | +** When rewriting a query, if the new subquery in the FROM clause |
| 151282 | +** contains TK_AGG_FUNCTION nodes that refer to an outer query, |
| 151283 | +** then we have to increase the Expr->op2 values of those nodes |
| 151284 | +** due to the extra subquery layer that was added. |
| 151285 | +** |
| 151286 | +** See also the incrAggDepth() routine in resolve.c |
| 151287 | +*/ |
| 151288 | +static int sqlite3WindowExtraAggFuncDepth(Walker *pWalker, Expr *pExpr){ |
| 151289 | + if( pExpr->op==TK_AGG_FUNCTION |
| 151290 | + && pExpr->op2>=pWalker->walkerDepth |
| 151291 | + ){ |
| 151292 | + pExpr->op2++; |
| 151293 | + } |
| 151294 | + return WRC_Continue; |
| 151295 | +} |
| 150957 | 151296 | |
| 150958 | 151297 | /* |
| 150959 | 151298 | ** If the SELECT statement passed as the second argument does not invoke |
| 150960 | 151299 | ** any SQL window functions, this function is a no-op. Otherwise, it |
| 150961 | 151300 | ** rewrites the SELECT statement so that window function xStep functions |
| | @@ -151062,10 +151401,11 @@ |
| 151062 | 151401 | pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0 |
| 151063 | 151402 | ); |
| 151064 | 151403 | p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0); |
| 151065 | 151404 | if( p->pSrc ){ |
| 151066 | 151405 | Table *pTab2; |
| 151406 | + Walker w; |
| 151067 | 151407 | p->pSrc->a[0].pSelect = pSub; |
| 151068 | 151408 | sqlite3SrcListAssignCursors(pParse, p->pSrc); |
| 151069 | 151409 | pSub->selFlags |= SF_Expanded; |
| 151070 | 151410 | pTab2 = sqlite3ResultSetOfSelect(pParse, pSub, SQLITE_AFF_NONE); |
| 151071 | 151411 | pSub->selFlags |= (selFlags & SF_Aggregate); |
| | @@ -151077,10 +151417,15 @@ |
| 151077 | 151417 | }else{ |
| 151078 | 151418 | memcpy(pTab, pTab2, sizeof(Table)); |
| 151079 | 151419 | pTab->tabFlags |= TF_Ephemeral; |
| 151080 | 151420 | p->pSrc->a[0].pTab = pTab; |
| 151081 | 151421 | pTab = pTab2; |
| 151422 | + memset(&w, 0, sizeof(w)); |
| 151423 | + w.xExprCallback = sqlite3WindowExtraAggFuncDepth; |
| 151424 | + w.xSelectCallback = sqlite3WalkerDepthIncrease; |
| 151425 | + w.xSelectCallback2 = sqlite3WalkerDepthDecrease; |
| 151426 | + sqlite3WalkSelect(&w, pSub); |
| 151082 | 151427 | } |
| 151083 | 151428 | }else{ |
| 151084 | 151429 | sqlite3SelectDelete(db, pSub); |
| 151085 | 151430 | } |
| 151086 | 151431 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
| | @@ -160130,10 +160475,11 @@ |
| 160130 | 160475 | } |
| 160131 | 160476 | #endif |
| 160132 | 160477 | if( rc==SQLITE_OK ){ |
| 160133 | 160478 | sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, |
| 160134 | 160479 | sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage); |
| 160480 | + sqlite3MemoryBarrier(); |
| 160135 | 160481 | sqlite3GlobalConfig.isInit = 1; |
| 160136 | 160482 | #ifdef SQLITE_EXTRA_INIT |
| 160137 | 160483 | bRunExtraInit = 1; |
| 160138 | 160484 | #endif |
| 160139 | 160485 | } |
| | @@ -161431,12 +161777,11 @@ |
| 161431 | 161777 | ** Return non-zero to retry the lock. Return zero to stop trying |
| 161432 | 161778 | ** and cause SQLite to return SQLITE_BUSY. |
| 161433 | 161779 | */ |
| 161434 | 161780 | static int sqliteDefaultBusyCallback( |
| 161435 | 161781 | void *ptr, /* Database connection */ |
| 161436 | | - int count, /* Number of times table has been busy */ |
| 161437 | | - sqlite3_file *pFile /* The file on which the lock occurred */ |
| 161782 | + int count /* Number of times table has been busy */ |
| 161438 | 161783 | ){ |
| 161439 | 161784 | #if SQLITE_OS_WIN || HAVE_USLEEP |
| 161440 | 161785 | /* This case is for systems that have support for sleeping for fractions of |
| 161441 | 161786 | ** a second. Examples: All windows systems, unix systems with usleep() */ |
| 161442 | 161787 | static const u8 delays[] = |
| | @@ -161446,35 +161791,10 @@ |
| 161446 | 161791 | # define NDELAY ArraySize(delays) |
| 161447 | 161792 | sqlite3 *db = (sqlite3 *)ptr; |
| 161448 | 161793 | int tmout = db->busyTimeout; |
| 161449 | 161794 | int delay, prior; |
| 161450 | 161795 | |
| 161451 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 161452 | | - if( sqlite3OsFileControl(pFile,SQLITE_FCNTL_LOCK_TIMEOUT,&tmout)==SQLITE_OK ){ |
| 161453 | | - if( count ){ |
| 161454 | | - /* If this is the second or later invocation of the busy-handler, |
| 161455 | | - ** but tmout==0, then code in wal.c must have disabled the blocking |
| 161456 | | - ** lock before the SQLITE_BUSY error was hit. In this case, no delay |
| 161457 | | - ** occurred while waiting for the lock, so fall through to the xSleep() |
| 161458 | | - ** code below to delay a while before retrying the lock. |
| 161459 | | - ** |
| 161460 | | - ** Alternatively, if tmout!=0, then SQLite has already waited |
| 161461 | | - ** sqlite3.busyTimeout ms for a lock. In this case, return 0 to |
| 161462 | | - ** indicate that the lock should not be retried and the SQLITE_BUSY |
| 161463 | | - ** error returned to the application. */ |
| 161464 | | - if( tmout ){ |
| 161465 | | - tmout = 0; |
| 161466 | | - sqlite3OsFileControl(pFile, SQLITE_FCNTL_LOCK_TIMEOUT, &tmout); |
| 161467 | | - return 0; |
| 161468 | | - } |
| 161469 | | - }else{ |
| 161470 | | - return 1; |
| 161471 | | - } |
| 161472 | | - } |
| 161473 | | -#else |
| 161474 | | - UNUSED_PARAMETER(pFile); |
| 161475 | | -#endif |
| 161476 | 161796 | assert( count>=0 ); |
| 161477 | 161797 | if( count < NDELAY ){ |
| 161478 | 161798 | delay = delays[count]; |
| 161479 | 161799 | prior = totals[count]; |
| 161480 | 161800 | }else{ |
| | @@ -161490,11 +161810,10 @@ |
| 161490 | 161810 | #else |
| 161491 | 161811 | /* This case for unix systems that lack usleep() support. Sleeping |
| 161492 | 161812 | ** must be done in increments of whole seconds */ |
| 161493 | 161813 | sqlite3 *db = (sqlite3 *)ptr; |
| 161494 | 161814 | int tmout = ((sqlite3 *)ptr)->busyTimeout; |
| 161495 | | - UNUSED_PARAMETER(pFile); |
| 161496 | 161815 | if( (count+1)*1000 > tmout ){ |
| 161497 | 161816 | return 0; |
| 161498 | 161817 | } |
| 161499 | 161818 | sqlite3OsSleep(db->pVfs, 1000000); |
| 161500 | 161819 | return 1; |
| | @@ -161508,23 +161827,14 @@ |
| 161508 | 161827 | ** lock on VFS file pFile. |
| 161509 | 161828 | ** |
| 161510 | 161829 | ** If this routine returns non-zero, the lock is retried. If it |
| 161511 | 161830 | ** returns 0, the operation aborts with an SQLITE_BUSY error. |
| 161512 | 161831 | */ |
| 161513 | | -SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p, sqlite3_file *pFile){ |
| 161832 | +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){ |
| 161514 | 161833 | int rc; |
| 161515 | 161834 | if( p->xBusyHandler==0 || p->nBusy<0 ) return 0; |
| 161516 | | - if( p->bExtraFileArg ){ |
| 161517 | | - /* Add an extra parameter with the pFile pointer to the end of the |
| 161518 | | - ** callback argument list */ |
| 161519 | | - int (*xTra)(void*,int,sqlite3_file*); |
| 161520 | | - xTra = (int(*)(void*,int,sqlite3_file*))p->xBusyHandler; |
| 161521 | | - rc = xTra(p->pBusyArg, p->nBusy, pFile); |
| 161522 | | - }else{ |
| 161523 | | - /* Legacy style busy handler callback */ |
| 161524 | | - rc = p->xBusyHandler(p->pBusyArg, p->nBusy); |
| 161525 | | - } |
| 161835 | + rc = p->xBusyHandler(p->pBusyArg, p->nBusy); |
| 161526 | 161836 | if( rc==0 ){ |
| 161527 | 161837 | p->nBusy = -1; |
| 161528 | 161838 | }else{ |
| 161529 | 161839 | p->nBusy++; |
| 161530 | 161840 | } |
| | @@ -161545,11 +161855,10 @@ |
| 161545 | 161855 | #endif |
| 161546 | 161856 | sqlite3_mutex_enter(db->mutex); |
| 161547 | 161857 | db->busyHandler.xBusyHandler = xBusy; |
| 161548 | 161858 | db->busyHandler.pBusyArg = pArg; |
| 161549 | 161859 | db->busyHandler.nBusy = 0; |
| 161550 | | - db->busyHandler.bExtraFileArg = 0; |
| 161551 | 161860 | db->busyTimeout = 0; |
| 161552 | 161861 | sqlite3_mutex_leave(db->mutex); |
| 161553 | 161862 | return SQLITE_OK; |
| 161554 | 161863 | } |
| 161555 | 161864 | |
| | @@ -161596,11 +161905,10 @@ |
| 161596 | 161905 | #endif |
| 161597 | 161906 | if( ms>0 ){ |
| 161598 | 161907 | sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback, |
| 161599 | 161908 | (void*)db); |
| 161600 | 161909 | db->busyTimeout = ms; |
| 161601 | | - db->busyHandler.bExtraFileArg = 1; |
| 161602 | 161910 | }else{ |
| 161603 | 161911 | sqlite3_busy_handler(db, 0, 0); |
| 161604 | 161912 | } |
| 161605 | 161913 | return SQLITE_OK; |
| 161606 | 161914 | } |
| | @@ -163071,10 +163379,13 @@ |
| 163071 | 163379 | | SQLITE_EnableQPSG |
| 163072 | 163380 | #endif |
| 163073 | 163381 | #if defined(SQLITE_DEFAULT_DEFENSIVE) |
| 163074 | 163382 | | SQLITE_Defensive |
| 163075 | 163383 | #endif |
| 163384 | +#if defined(SQLITE_DEFAULT_LEGACY_ALTER_TABLE) |
| 163385 | + | SQLITE_LegacyAlter |
| 163386 | +#endif |
| 163076 | 163387 | ; |
| 163077 | 163388 | sqlite3HashInit(&db->aCollSeq); |
| 163078 | 163389 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 163079 | 163390 | sqlite3HashInit(&db->aModule); |
| 163080 | 163391 | #endif |
| | @@ -163113,11 +163424,11 @@ |
| 163113 | 163424 | assert( SQLITE_OPEN_CREATE == 0x04 ); |
| 163114 | 163425 | testcase( (1<<(flags&7))==0x02 ); /* READONLY */ |
| 163115 | 163426 | testcase( (1<<(flags&7))==0x04 ); /* READWRITE */ |
| 163116 | 163427 | testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */ |
| 163117 | 163428 | if( ((1<<(flags&7)) & 0x46)==0 ){ |
| 163118 | | - rc = SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */ |
| 163429 | + rc = SQLITE_MISUSE_BKPT; /* IMP: R-18321-05872 */ |
| 163119 | 163430 | }else{ |
| 163120 | 163431 | rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg); |
| 163121 | 163432 | } |
| 163122 | 163433 | if( rc!=SQLITE_OK ){ |
| 163123 | 163434 | if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); |
| | @@ -163668,11 +163979,11 @@ |
| 163668 | 163979 | *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager); |
| 163669 | 163980 | rc = SQLITE_OK; |
| 163670 | 163981 | }else if( op==SQLITE_FCNTL_RESERVE_BYTES ){ |
| 163671 | 163982 | int iNew = *(int*)pArg; |
| 163672 | 163983 | *(int*)pArg = sqlite3BtreeGetRequestedReserve(pBtree); |
| 163673 | | - if( iNew>=0 && iNew<=254 ){ |
| 163984 | + if( iNew>=0 && iNew<=255 ){ |
| 163674 | 163985 | sqlite3BtreeSetPageSize(pBtree, 0, iNew, 0); |
| 163675 | 163986 | } |
| 163676 | 163987 | rc = SQLITE_OK; |
| 163677 | 163988 | }else{ |
| 163678 | 163989 | rc = sqlite3OsFileControl(fd, op, pArg); |
| | @@ -167905,11 +168216,13 @@ |
| 167905 | 168216 | static void fts3ReadNextPos( |
| 167906 | 168217 | char **pp, /* IN/OUT: Pointer into position-list buffer */ |
| 167907 | 168218 | sqlite3_int64 *pi /* IN/OUT: Value read from position-list */ |
| 167908 | 168219 | ){ |
| 167909 | 168220 | if( (**pp)&0xFE ){ |
| 167910 | | - fts3GetDeltaVarint(pp, pi); |
| 168221 | + int iVal; |
| 168222 | + *pp += fts3GetVarint32((*pp), &iVal); |
| 168223 | + *pi += iVal; |
| 167911 | 168224 | *pi -= 2; |
| 167912 | 168225 | }else{ |
| 167913 | 168226 | *pi = POSITION_LIST_END; |
| 167914 | 168227 | } |
| 167915 | 168228 | } |
| | @@ -171035,10 +171348,11 @@ |
| 171035 | 171348 | while( *pRc==SQLITE_OK && pLeft->bEof==0 ){ |
| 171036 | 171349 | memset(pDl->pList, 0, pDl->nList); |
| 171037 | 171350 | fts3EvalNextRow(pCsr, pLeft, pRc); |
| 171038 | 171351 | } |
| 171039 | 171352 | } |
| 171353 | + pRight->bEof = pLeft->bEof = 1; |
| 171040 | 171354 | } |
| 171041 | 171355 | } |
| 171042 | 171356 | break; |
| 171043 | 171357 | } |
| 171044 | 171358 | |
| | @@ -182580,11 +182894,11 @@ |
| 182580 | 182894 | iStart = pExpr->iPhrase * p->nCol; |
| 182581 | 182895 | }else{ |
| 182582 | 182896 | iStart = pExpr->iPhrase * ((p->nCol + 31) / 32); |
| 182583 | 182897 | } |
| 182584 | 182898 | |
| 182585 | | - while( 1 ){ |
| 182899 | + if( pIter ) while( 1 ){ |
| 182586 | 182900 | int nHit = fts3ColumnlistCount(&pIter); |
| 182587 | 182901 | if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){ |
| 182588 | 182902 | if( p->flag==FTS3_MATCHINFO_LHITS ){ |
| 182589 | 182903 | p->aMatchinfo[iStart + iCol] = (u32)nHit; |
| 182590 | 182904 | }else if( nHit ){ |
| | @@ -184494,10 +184808,11 @@ |
| 184494 | 184808 | } |
| 184495 | 184809 | |
| 184496 | 184810 | /* Append N bytes from zIn onto the end of the JsonString string. |
| 184497 | 184811 | */ |
| 184498 | 184812 | static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){ |
| 184813 | + if( N==0 ) return; |
| 184499 | 184814 | if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return; |
| 184500 | 184815 | memcpy(p->zBuf+p->nUsed, zIn, N); |
| 184501 | 184816 | p->nUsed += N; |
| 184502 | 184817 | } |
| 184503 | 184818 | |
| | @@ -224505,11 +224820,11 @@ |
| 224505 | 224820 | int nArg, /* Number of args */ |
| 224506 | 224821 | sqlite3_value **apUnused /* Function arguments */ |
| 224507 | 224822 | ){ |
| 224508 | 224823 | assert( nArg==0 ); |
| 224509 | 224824 | UNUSED_PARAM2(nArg, apUnused); |
| 224510 | | - sqlite3_result_text(pCtx, "fts5: 2020-05-04 19:52:00 8eee591d3cb9fadfd5cac5543bd66ef9cb371a72d3ad3241fb3bfd67fb216eda", -1, SQLITE_TRANSIENT); |
| 224825 | + sqlite3_result_text(pCtx, "fts5: 2020-05-25 16:19:56 0c1fcf4711a2e66c813aed38cf41cd3e2123ee8eb6db98118086764c4ba83350", -1, SQLITE_TRANSIENT); |
| 224511 | 224826 | } |
| 224512 | 224827 | |
| 224513 | 224828 | /* |
| 224514 | 224829 | ** Return true if zName is the extension on one of the shadow tables used |
| 224515 | 224830 | ** by this module. |
| | @@ -229288,12 +229603,12 @@ |
| 229288 | 229603 | } |
| 229289 | 229604 | #endif /* SQLITE_CORE */ |
| 229290 | 229605 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */ |
| 229291 | 229606 | |
| 229292 | 229607 | /************** End of stmt.c ************************************************/ |
| 229293 | | -#if __LINE__!=229293 |
| 229608 | +#if __LINE__!=229608 |
| 229294 | 229609 | #undef SQLITE_SOURCE_ID |
| 229295 | | -#define SQLITE_SOURCE_ID "2020-05-04 19:52:00 8eee591d3cb9fadfd5cac5543bd66ef9cb371a72d3ad3241fb3bfd67fb21alt2" |
| 229610 | +#define SQLITE_SOURCE_ID "2020-05-25 16:19:56 0c1fcf4711a2e66c813aed38cf41cd3e2123ee8eb6db98118086764c4ba8alt2" |
| 229296 | 229611 | #endif |
| 229297 | 229612 | /* Return the source-id for this library */ |
| 229298 | 229613 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 229299 | 229614 | /************************** End of sqlite3.c ******************************/ |
| 229300 | 229615 | |