Fossil SCM
Update the built-in SQLite to the latest trunk version (3.10.0 alpha).
Commit
22681034c19431435b67f216f93b1ed1386fff5d
Parent
2532f1bccc960f1…
2 files changed
+489
-141
+109
-15
+489
-141
| --- src/sqlite3.c | ||
| +++ src/sqlite3.c | ||
| @@ -325,11 +325,11 @@ | ||
| 325 | 325 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 326 | 326 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 327 | 327 | */ |
| 328 | 328 | #define SQLITE_VERSION "3.10.0" |
| 329 | 329 | #define SQLITE_VERSION_NUMBER 3010000 |
| 330 | -#define SQLITE_SOURCE_ID "2015-12-07 18:18:33 e7ae120d04cffafd9bc2b4ecd68571c17e05ed72" | |
| 330 | +#define SQLITE_SOURCE_ID "2015-12-11 13:51:02 e998513e442ce1206b12dc28bdc996d7b5f9f94d" | |
| 331 | 331 | |
| 332 | 332 | /* |
| 333 | 333 | ** CAPI3REF: Run-Time Library Version Numbers |
| 334 | 334 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 335 | 335 | ** |
| @@ -4617,12 +4617,12 @@ | ||
| 4617 | 4617 | ** |
| 4618 | 4618 | ** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object |
| 4619 | 4619 | ** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer |
| 4620 | 4620 | ** then sqlite3_value_free(V) is a harmless no-op. |
| 4621 | 4621 | */ |
| 4622 | -SQLITE_API SQLITE_EXPERIMENTAL sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value*); | |
| 4623 | -SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_value_free(sqlite3_value*); | |
| 4622 | +SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value*); | |
| 4623 | +SQLITE_API void SQLITE_STDCALL sqlite3_value_free(sqlite3_value*); | |
| 4624 | 4624 | |
| 4625 | 4625 | /* |
| 4626 | 4626 | ** CAPI3REF: Obtain Aggregate Function Context |
| 4627 | 4627 | ** METHOD: sqlite3_context |
| 4628 | 4628 | ** |
| @@ -8063,37 +8063,131 @@ | ||
| 8063 | 8063 | SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); |
| 8064 | 8064 | |
| 8065 | 8065 | /* |
| 8066 | 8066 | ** CAPI3REF: Flush caches to disk mid-transaction |
| 8067 | 8067 | ** |
| 8068 | -** If a write-transaction is open when this function is called, any dirty | |
| 8068 | +** ^If a write-transaction is open on [database connection] D when the | |
| 8069 | +** [sqlite3_db_cacheflush(D)] interface invoked, any dirty | |
| 8069 | 8070 | ** pages in the pager-cache that are not currently in use are written out |
| 8070 | 8071 | ** to disk. A dirty page may be in use if a database cursor created by an |
| 8071 | 8072 | ** active SQL statement is reading from it, or if it is page 1 of a database |
| 8072 | -** file (page 1 is always "in use"). Dirty pages are flushed for all | |
| 8073 | -** databases - "main", "temp" and any attached databases. | |
| 8073 | +** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)] | |
| 8074 | +** interface flushes caches for all schemas - "main", "temp", and | |
| 8075 | +** any [attached] databases. | |
| 8074 | 8076 | ** |
| 8075 | -** If this function needs to obtain extra database locks before dirty pages | |
| 8076 | -** can be flushed to disk, it does so. If said locks cannot be obtained | |
| 8077 | +** ^If this function needs to obtain extra database locks before dirty pages | |
| 8078 | +** can be flushed to disk, it does so. ^If those locks cannot be obtained | |
| 8077 | 8079 | ** immediately and there is a busy-handler callback configured, it is invoked |
| 8078 | -** in the usual manner. If the required lock still cannot be obtained, then | |
| 8080 | +** in the usual manner. ^If the required lock still cannot be obtained, then | |
| 8079 | 8081 | ** the database is skipped and an attempt made to flush any dirty pages |
| 8080 | -** belonging to the next (if any) database. If any databases are skipped | |
| 8082 | +** belonging to the next (if any) database. ^If any databases are skipped | |
| 8081 | 8083 | ** because locks cannot be obtained, but no other error occurs, this |
| 8082 | 8084 | ** function returns SQLITE_BUSY. |
| 8083 | 8085 | ** |
| 8084 | -** If any other error occurs while flushing dirty pages to disk (for | |
| 8086 | +** ^If any other error occurs while flushing dirty pages to disk (for | |
| 8085 | 8087 | ** example an IO error or out-of-memory condition), then processing is |
| 8086 | -** abandoned and an SQLite error code returned to the caller immediately. | |
| 8088 | +** abandoned and an SQLite [error code] is returned to the caller immediately. | |
| 8087 | 8089 | ** |
| 8088 | -** Otherwise, if no error occurs, SQLITE_OK is returned. | |
| 8090 | +** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK. | |
| 8089 | 8091 | ** |
| 8090 | -** This function does not set the database handle error code or message | |
| 8091 | -** returned by the sqlite3_errcode() and sqlite3_errmsg() functions. | |
| 8092 | +** ^This function does not set the database handle error code or message | |
| 8093 | +** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions. | |
| 8092 | 8094 | */ |
| 8093 | 8095 | SQLITE_API int SQLITE_STDCALL sqlite3_db_cacheflush(sqlite3*); |
| 8094 | 8096 | |
| 8097 | +/* | |
| 8098 | +** CAPI3REF: Database Snapshot | |
| 8099 | +** KEYWORDS: {snapshot} | |
| 8100 | +** EXPERIMENTAL | |
| 8101 | +** | |
| 8102 | +** An instance of the snapshot object records the state of a [WAL mode] | |
| 8103 | +** database for some specific point in history. | |
| 8104 | +** | |
| 8105 | +** In [WAL mode], multiple [database connections] that are open on the | |
| 8106 | +** same database file can each be reading a different historical version | |
| 8107 | +** of the database file. When a [database connection] begins a read | |
| 8108 | +** transaction, that connection sees an unchanging copy of the database | |
| 8109 | +** as it existed for the point in time when the transaction first started. | |
| 8110 | +** Subsequent changes to the database from other connections are not seen | |
| 8111 | +** by the reader until a new read transaction is started. | |
| 8112 | +** | |
| 8113 | +** The sqlite3_snapshot object records state information about an historical | |
| 8114 | +** version of the database file so that it is possible to later open a new read | |
| 8115 | +** transaction that sees that historical version of the database rather than | |
| 8116 | +** the most recent version. | |
| 8117 | +** | |
| 8118 | +** The constructor for this object is [sqlite3_snapshot_get()]. The | |
| 8119 | +** [sqlite3_snapshot_open()] method causes a fresh read transaction to refer | |
| 8120 | +** to an historical snapshot (if possible). The destructor for | |
| 8121 | +** sqlite3_snapshot objects is [sqlite3_snapshot_free()]. | |
| 8122 | +*/ | |
| 8123 | +typedef struct sqlite3_snapshot sqlite3_snapshot; | |
| 8124 | + | |
| 8125 | +/* | |
| 8126 | +** CAPI3REF: Record A Database Snapshot | |
| 8127 | +** EXPERIMENTAL | |
| 8128 | +** | |
| 8129 | +** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a | |
| 8130 | +** new [sqlite3_snapshot] object that records the current state of | |
| 8131 | +** schema S in database connection D. ^On success, the | |
| 8132 | +** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly | |
| 8133 | +** created [sqlite3_snapshot] object into *P and returns SQLITE_OK. | |
| 8134 | +** ^If schema S of [database connection] D is not a [WAL mode] database | |
| 8135 | +** that is in a read transaction, then [sqlite3_snapshot_get(D,S,P)] | |
| 8136 | +** leaves the *P value unchanged and returns an appropriate [error code]. | |
| 8137 | +** | |
| 8138 | +** The [sqlite3_snapshot] object returned from a successful call to | |
| 8139 | +** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()] | |
| 8140 | +** to avoid a memory leak. | |
| 8141 | +** | |
| 8142 | +** The [sqlite3_snapshot_get()] interface is only available when the | |
| 8143 | +** SQLITE_ENABLE_SNAPSHOT compile-time option is used. | |
| 8144 | +*/ | |
| 8145 | +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_snapshot_get( | |
| 8146 | + sqlite3 *db, | |
| 8147 | + const char *zSchema, | |
| 8148 | + sqlite3_snapshot **ppSnapshot | |
| 8149 | +); | |
| 8150 | + | |
| 8151 | +/* | |
| 8152 | +** CAPI3REF: Start a read transaction on an historical snapshot | |
| 8153 | +** EXPERIMENTAL | |
| 8154 | +** | |
| 8155 | +** ^The [sqlite3_snapshot_open(D,S,P)] interface attempts to move the | |
| 8156 | +** read transaction that is currently open on schema S of | |
| 8157 | +** [database connection] D so that it refers to historical [snapshot] P. | |
| 8158 | +** ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK on success | |
| 8159 | +** or an appropriate [error code] if it fails. | |
| 8160 | +** | |
| 8161 | +** ^In order to succeed, a call to [sqlite3_snapshot_open(D,S,P)] must be | |
| 8162 | +** the first operation, apart from other sqlite3_snapshot_open() calls, | |
| 8163 | +** following the [BEGIN] that starts a new read transaction. | |
| 8164 | +** ^A [snapshot] will fail to open if it has been overwritten by a | |
| 8165 | +** [checkpoint]. | |
| 8166 | +** | |
| 8167 | +** The [sqlite3_snapshot_open()] interface is only available when the | |
| 8168 | +** SQLITE_ENABLE_SNAPSHOT compile-time option is used. | |
| 8169 | +*/ | |
| 8170 | +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_snapshot_open( | |
| 8171 | + sqlite3 *db, | |
| 8172 | + const char *zSchema, | |
| 8173 | + sqlite3_snapshot *pSnapshot | |
| 8174 | +); | |
| 8175 | + | |
| 8176 | +/* | |
| 8177 | +** CAPI3REF: Destroy a snapshot | |
| 8178 | +** EXPERIMENTAL | |
| 8179 | +** | |
| 8180 | +** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P. | |
| 8181 | +** The application must eventually free every [sqlite3_snapshot] object | |
| 8182 | +** using this routine to avoid a memory leak. | |
| 8183 | +** | |
| 8184 | +** The [sqlite3_snapshot_free()] interface is only available when the | |
| 8185 | +** SQLITE_ENABLE_SNAPSHOT compile-time option is used. | |
| 8186 | +*/ | |
| 8187 | +SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_snapshot_free(sqlite3_snapshot*); | |
| 8188 | + | |
| 8095 | 8189 | /* |
| 8096 | 8190 | ** Undo the hack that converts floating point types to integer for |
| 8097 | 8191 | ** builds on processors without floating point support. |
| 8098 | 8192 | */ |
| 8099 | 8193 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| @@ -9017,10 +9111,25 @@ | ||
| 9017 | 9111 | #else /* Generates a warning - but it always works */ |
| 9018 | 9112 | # define SQLITE_INT_TO_PTR(X) ((void*)(X)) |
| 9019 | 9113 | # define SQLITE_PTR_TO_INT(X) ((int)(X)) |
| 9020 | 9114 | #endif |
| 9021 | 9115 | |
| 9116 | +/* | |
| 9117 | +** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to | |
| 9118 | +** something between S (inclusive) and E (exclusive). | |
| 9119 | +** | |
| 9120 | +** In other words, S is a buffer and E is a pointer to the first byte after | |
| 9121 | +** the end of buffer S. This macro returns true if P points to something | |
| 9122 | +** contained within the buffer S. | |
| 9123 | +*/ | |
| 9124 | +#if defined(HAVE_STDINT_H) | |
| 9125 | +# define SQLITE_WITHIN(P,S,E) \ | |
| 9126 | + ((uintptr_t)(P)>=(uintptr_t)(S) && (uintptr_t)(P)<(uintptr_t)(E)) | |
| 9127 | +#else | |
| 9128 | +# define SQLITE_WITHIN(P,S,E) ((P)>=(S) && (P)<(E)) | |
| 9129 | +#endif | |
| 9130 | + | |
| 9022 | 9131 | /* |
| 9023 | 9132 | ** A macro to hint to the compiler that a function should not be |
| 9024 | 9133 | ** inlined. |
| 9025 | 9134 | */ |
| 9026 | 9135 | #if defined(__GNUC__) |
| @@ -11086,10 +11195,14 @@ | ||
| 11086 | 11195 | SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*); |
| 11087 | 11196 | SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager); |
| 11088 | 11197 | SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager); |
| 11089 | 11198 | SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen); |
| 11090 | 11199 | SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager); |
| 11200 | +# ifdef SQLITE_ENABLE_SNAPSHOT | |
| 11201 | +SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot); | |
| 11202 | +SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot); | |
| 11203 | +# endif | |
| 11091 | 11204 | #endif |
| 11092 | 11205 | |
| 11093 | 11206 | #ifdef SQLITE_ENABLE_ZIPVFS |
| 11094 | 11207 | SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager); |
| 11095 | 11208 | #endif |
| @@ -13649,10 +13762,11 @@ | ||
| 13649 | 13762 | char *zText; /* The string collected so far */ |
| 13650 | 13763 | int nChar; /* Length of the string so far */ |
| 13651 | 13764 | int nAlloc; /* Amount of space allocated in zText */ |
| 13652 | 13765 | int mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */ |
| 13653 | 13766 | u8 accError; /* STRACCUM_NOMEM or STRACCUM_TOOBIG */ |
| 13767 | + u8 bMalloced; /* zText points to allocated space */ | |
| 13654 | 13768 | }; |
| 13655 | 13769 | #define STRACCUM_NOMEM 1 |
| 13656 | 13770 | #define STRACCUM_TOOBIG 2 |
| 13657 | 13771 | |
| 13658 | 13772 | /* |
| @@ -21930,11 +22044,11 @@ | ||
| 21930 | 22044 | ** would be much more complicated.) */ |
| 21931 | 22045 | assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); |
| 21932 | 22046 | scratchAllocOut--; |
| 21933 | 22047 | #endif |
| 21934 | 22048 | |
| 21935 | - if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){ | |
| 22049 | + if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){ | |
| 21936 | 22050 | /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */ |
| 21937 | 22051 | ScratchFreeslot *pSlot; |
| 21938 | 22052 | pSlot = (ScratchFreeslot*)p; |
| 21939 | 22053 | sqlite3_mutex_enter(mem0.mutex); |
| 21940 | 22054 | pSlot->pNext = mem0.pScratchFree; |
| @@ -21966,11 +22080,11 @@ | ||
| 21966 | 22080 | /* |
| 21967 | 22081 | ** TRUE if p is a lookaside memory allocation from db |
| 21968 | 22082 | */ |
| 21969 | 22083 | #ifndef SQLITE_OMIT_LOOKASIDE |
| 21970 | 22084 | static int isLookaside(sqlite3 *db, void *p){ |
| 21971 | - return p>=db->lookaside.pStart && p<db->lookaside.pEnd; | |
| 22085 | + return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd); | |
| 21972 | 22086 | } |
| 21973 | 22087 | #else |
| 21974 | 22088 | #define isLookaside(A,B) 0 |
| 21975 | 22089 | #endif |
| 21976 | 22090 | |
| @@ -23108,12 +23222,13 @@ | ||
| 23108 | 23222 | if( p->mxAlloc==0 ){ |
| 23109 | 23223 | N = p->nAlloc - p->nChar - 1; |
| 23110 | 23224 | setStrAccumError(p, STRACCUM_TOOBIG); |
| 23111 | 23225 | return N; |
| 23112 | 23226 | }else{ |
| 23113 | - char *zOld = (p->zText==p->zBase ? 0 : p->zText); | |
| 23227 | + char *zOld = p->bMalloced ? p->zText : 0; | |
| 23114 | 23228 | i64 szNew = p->nChar; |
| 23229 | + assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) ); | |
| 23115 | 23230 | szNew += N + 1; |
| 23116 | 23231 | if( szNew+p->nChar<=p->mxAlloc ){ |
| 23117 | 23232 | /* Force exponential buffer size growth as long as it does not overflow, |
| 23118 | 23233 | ** to avoid having to call this routine too often */ |
| 23119 | 23234 | szNew += p->nChar; |
| @@ -23130,13 +23245,14 @@ | ||
| 23130 | 23245 | }else{ |
| 23131 | 23246 | zNew = sqlite3_realloc64(zOld, p->nAlloc); |
| 23132 | 23247 | } |
| 23133 | 23248 | if( zNew ){ |
| 23134 | 23249 | assert( p->zText!=0 || p->nChar==0 ); |
| 23135 | - if( p->zText==p->zBase && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); | |
| 23250 | + if( !p->bMalloced && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); | |
| 23136 | 23251 | p->zText = zNew; |
| 23137 | 23252 | p->nAlloc = sqlite3DbMallocSize(p->db, zNew); |
| 23253 | + p->bMalloced = 1; | |
| 23138 | 23254 | }else{ |
| 23139 | 23255 | sqlite3StrAccumReset(p); |
| 23140 | 23256 | setStrAccumError(p, STRACCUM_NOMEM); |
| 23141 | 23257 | return 0; |
| 23142 | 23258 | } |
| @@ -23150,10 +23266,11 @@ | ||
| 23150 | 23266 | SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){ |
| 23151 | 23267 | testcase( p->nChar + (i64)N > 0x7fffffff ); |
| 23152 | 23268 | if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ |
| 23153 | 23269 | return; |
| 23154 | 23270 | } |
| 23271 | + assert( (p->zText==p->zBase)==(p->bMalloced==0) ); | |
| 23155 | 23272 | while( (N--)>0 ) p->zText[p->nChar++] = c; |
| 23156 | 23273 | } |
| 23157 | 23274 | |
| 23158 | 23275 | /* |
| 23159 | 23276 | ** The StrAccum "p" is not large enough to accept N new bytes of z[]. |
| @@ -23167,10 +23284,11 @@ | ||
| 23167 | 23284 | N = sqlite3StrAccumEnlarge(p, N); |
| 23168 | 23285 | if( N>0 ){ |
| 23169 | 23286 | memcpy(&p->zText[p->nChar], z, N); |
| 23170 | 23287 | p->nChar += N; |
| 23171 | 23288 | } |
| 23289 | + assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) ); | |
| 23172 | 23290 | } |
| 23173 | 23291 | |
| 23174 | 23292 | /* |
| 23175 | 23293 | ** Append N bytes of text from z to the StrAccum object. Increase the |
| 23176 | 23294 | ** size of the memory allocation for StrAccum if necessary. |
| @@ -23202,15 +23320,17 @@ | ||
| 23202 | 23320 | ** Return a pointer to the resulting string. Return a NULL |
| 23203 | 23321 | ** pointer if any kind of error was encountered. |
| 23204 | 23322 | */ |
| 23205 | 23323 | SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){ |
| 23206 | 23324 | if( p->zText ){ |
| 23325 | + assert( (p->zText==p->zBase)==(p->bMalloced==0) ); | |
| 23207 | 23326 | p->zText[p->nChar] = 0; |
| 23208 | - if( p->mxAlloc>0 && p->zText==p->zBase ){ | |
| 23327 | + if( p->mxAlloc>0 && p->bMalloced==0 ){ | |
| 23209 | 23328 | p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); |
| 23210 | 23329 | if( p->zText ){ |
| 23211 | 23330 | memcpy(p->zText, p->zBase, p->nChar+1); |
| 23331 | + p->bMalloced = 1; | |
| 23212 | 23332 | }else{ |
| 23213 | 23333 | setStrAccumError(p, STRACCUM_NOMEM); |
| 23214 | 23334 | } |
| 23215 | 23335 | } |
| 23216 | 23336 | } |
| @@ -23219,12 +23339,14 @@ | ||
| 23219 | 23339 | |
| 23220 | 23340 | /* |
| 23221 | 23341 | ** Reset an StrAccum string. Reclaim all malloced memory. |
| 23222 | 23342 | */ |
| 23223 | 23343 | SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){ |
| 23224 | - if( p->zText!=p->zBase ){ | |
| 23344 | + assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) ); | |
| 23345 | + if( p->bMalloced ){ | |
| 23225 | 23346 | sqlite3DbFree(p->db, p->zText); |
| 23347 | + p->bMalloced = 0; | |
| 23226 | 23348 | } |
| 23227 | 23349 | p->zText = 0; |
| 23228 | 23350 | } |
| 23229 | 23351 | |
| 23230 | 23352 | /* |
| @@ -23246,10 +23368,11 @@ | ||
| 23246 | 23368 | p->db = db; |
| 23247 | 23369 | p->nChar = 0; |
| 23248 | 23370 | p->nAlloc = n; |
| 23249 | 23371 | p->mxAlloc = mx; |
| 23250 | 23372 | p->accError = 0; |
| 23373 | + p->bMalloced = 0; | |
| 23251 | 23374 | } |
| 23252 | 23375 | |
| 23253 | 23376 | /* |
| 23254 | 23377 | ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 23255 | 23378 | ** %-conversion extensions. |
| @@ -43273,10 +43396,15 @@ | ||
| 43273 | 43396 | /* Return true if the argument is non-NULL and the WAL module is using |
| 43274 | 43397 | ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the |
| 43275 | 43398 | ** WAL module is using shared-memory, return false. |
| 43276 | 43399 | */ |
| 43277 | 43400 | SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal); |
| 43401 | + | |
| 43402 | +#ifdef SQLITE_ENABLE_SNAPSHOT | |
| 43403 | +SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot); | |
| 43404 | +SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot); | |
| 43405 | +#endif | |
| 43278 | 43406 | |
| 43279 | 43407 | #ifdef SQLITE_ENABLE_ZIPVFS |
| 43280 | 43408 | /* If the WAL file is not empty, return the number of bytes of content |
| 43281 | 43409 | ** stored in each frame (i.e. the db page-size when the WAL was created). |
| 43282 | 43410 | */ |
| @@ -50566,10 +50694,38 @@ | ||
| 50566 | 50694 | } |
| 50567 | 50695 | } |
| 50568 | 50696 | return rc; |
| 50569 | 50697 | } |
| 50570 | 50698 | |
| 50699 | +#ifdef SQLITE_ENABLE_SNAPSHOT | |
| 50700 | +/* | |
| 50701 | +** If this is a WAL database, obtain a snapshot handle for the snapshot | |
| 50702 | +** currently open. Otherwise, return an error. | |
| 50703 | +*/ | |
| 50704 | +SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){ | |
| 50705 | + int rc = SQLITE_ERROR; | |
| 50706 | + if( pPager->pWal ){ | |
| 50707 | + rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot); | |
| 50708 | + } | |
| 50709 | + return rc; | |
| 50710 | +} | |
| 50711 | + | |
| 50712 | +/* | |
| 50713 | +** If this is a WAL database, store a pointer to pSnapshot. Next time a | |
| 50714 | +** read transaction is opened, attempt to read from the snapshot it | |
| 50715 | +** identifies. If this is not a WAL database, return an error. | |
| 50716 | +*/ | |
| 50717 | +SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){ | |
| 50718 | + int rc = SQLITE_OK; | |
| 50719 | + if( pPager->pWal ){ | |
| 50720 | + sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot); | |
| 50721 | + }else{ | |
| 50722 | + rc = SQLITE_ERROR; | |
| 50723 | + } | |
| 50724 | + return rc; | |
| 50725 | +} | |
| 50726 | +#endif /* SQLITE_ENABLE_SNAPSHOT */ | |
| 50571 | 50727 | #endif /* !SQLITE_OMIT_WAL */ |
| 50572 | 50728 | |
| 50573 | 50729 | #ifdef SQLITE_ENABLE_ZIPVFS |
| 50574 | 50730 | /* |
| 50575 | 50731 | ** A read-lock must be held on the pager when this function is called. If |
| @@ -50861,11 +51017,12 @@ | ||
| 50861 | 51017 | #define WAL_MAX_VERSION 3007000 |
| 50862 | 51018 | #define WALINDEX_MAX_VERSION 3007000 |
| 50863 | 51019 | |
| 50864 | 51020 | /* |
| 50865 | 51021 | ** Indices of various locking bytes. WAL_NREADER is the number |
| 50866 | -** of available reader locks and should be at least 3. | |
| 51022 | +** of available reader locks and should be at least 3. The default | |
| 51023 | +** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5. | |
| 50867 | 51024 | */ |
| 50868 | 51025 | #define WAL_WRITE_LOCK 0 |
| 50869 | 51026 | #define WAL_ALL_BUT_WRITE 1 |
| 50870 | 51027 | #define WAL_CKPT_LOCK 1 |
| 50871 | 51028 | #define WAL_RECOVER_LOCK 2 |
| @@ -50881,11 +51038,14 @@ | ||
| 50881 | 51038 | |
| 50882 | 51039 | /* |
| 50883 | 51040 | ** The following object holds a copy of the wal-index header content. |
| 50884 | 51041 | ** |
| 50885 | 51042 | ** The actual header in the wal-index consists of two copies of this |
| 50886 | -** object. | |
| 51043 | +** object followed by one instance of the WalCkptInfo object. | |
| 51044 | +** For all versions of SQLite through 3.10.0 and probably beyond, | |
| 51045 | +** the locking bytes (WalCkptInfo.aLock) start at offset 120 and | |
| 51046 | +** the total header size is 136 bytes. | |
| 50887 | 51047 | ** |
| 50888 | 51048 | ** The szPage value can be any power of 2 between 512 and 32768, inclusive. |
| 50889 | 51049 | ** Or it can be 1 to represent a 65536-byte page. The latter case was |
| 50890 | 51050 | ** added in 3.7.1 when support for 64K pages was added. |
| 50891 | 51051 | */ |
| @@ -50913,10 +51073,20 @@ | ||
| 50913 | 51073 | ** database "backfilling".) The nBackfill number is never greater than |
| 50914 | 51074 | ** WalIndexHdr.mxFrame. nBackfill can only be increased by threads |
| 50915 | 51075 | ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread). |
| 50916 | 51076 | ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from |
| 50917 | 51077 | ** mxFrame back to zero when the WAL is reset. |
| 51078 | +** | |
| 51079 | +** nBackfillAttempted is the largest value of nBackfill that a checkpoint | |
| 51080 | +** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however | |
| 51081 | +** the nBackfillAttempted is set before any backfilling is done and the | |
| 51082 | +** nBackfill is only set after all backfilling completes. So if a checkpoint | |
| 51083 | +** crashes, nBackfillAttempted might be larger than nBackfill. The | |
| 51084 | +** WalIndexHdr.mxFrame must never be less than nBackfillAttempted. | |
| 51085 | +** | |
| 51086 | +** The aLock[] field is a set of bytes used for locking. These bytes should | |
| 51087 | +** never be read or written. | |
| 50918 | 51088 | ** |
| 50919 | 51089 | ** There is one entry in aReadMark[] for each reader lock. If a reader |
| 50920 | 51090 | ** holds read-lock K, then the value in aReadMark[K] is no greater than |
| 50921 | 51091 | ** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff) |
| 50922 | 51092 | ** for any aReadMark[] means that entry is unused. aReadMark[0] is |
| @@ -50953,22 +51123,24 @@ | ||
| 50953 | 51123 | ** order to read from any aReadMark[] entries. |
| 50954 | 51124 | */ |
| 50955 | 51125 | struct WalCkptInfo { |
| 50956 | 51126 | u32 nBackfill; /* Number of WAL frames backfilled into DB */ |
| 50957 | 51127 | u32 aReadMark[WAL_NREADER]; /* Reader marks */ |
| 51128 | + u8 aLock[SQLITE_SHM_NLOCK]; /* Reserved space for locks */ | |
| 51129 | + u32 nBackfillAttempted; /* WAL frames perhaps written, or maybe not */ | |
| 51130 | + u32 notUsed0; /* Available for future enhancements */ | |
| 50958 | 51131 | }; |
| 50959 | 51132 | #define READMARK_NOT_USED 0xffffffff |
| 50960 | 51133 | |
| 50961 | 51134 | |
| 50962 | 51135 | /* A block of WALINDEX_LOCK_RESERVED bytes beginning at |
| 50963 | 51136 | ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems |
| 50964 | 51137 | ** only support mandatory file-locks, we do not read or write data |
| 50965 | 51138 | ** from the region of the file on which locks are applied. |
| 50966 | 51139 | */ |
| 50967 | -#define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo)) | |
| 50968 | -#define WALINDEX_LOCK_RESERVED 16 | |
| 50969 | -#define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED) | |
| 51140 | +#define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock)) | |
| 51141 | +#define WALINDEX_HDR_SIZE (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo)) | |
| 50970 | 51142 | |
| 50971 | 51143 | /* Size of header before each frame in wal */ |
| 50972 | 51144 | #define WAL_FRAME_HDRSIZE 24 |
| 50973 | 51145 | |
| 50974 | 51146 | /* Size of write ahead log header, including checksum. */ |
| @@ -51023,10 +51195,13 @@ | ||
| 51023 | 51195 | const char *zWalName; /* Name of WAL file */ |
| 51024 | 51196 | u32 nCkpt; /* Checkpoint sequence counter in the wal-header */ |
| 51025 | 51197 | #ifdef SQLITE_DEBUG |
| 51026 | 51198 | u8 lockError; /* True if a locking error has occurred */ |
| 51027 | 51199 | #endif |
| 51200 | +#ifdef SQLITE_ENABLE_SNAPSHOT | |
| 51201 | + WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */ | |
| 51202 | +#endif | |
| 51028 | 51203 | }; |
| 51029 | 51204 | |
| 51030 | 51205 | /* |
| 51031 | 51206 | ** Candidate values for Wal.exclusiveMode. |
| 51032 | 51207 | */ |
| @@ -51787,10 +51962,11 @@ | ||
| 51787 | 51962 | ** currently holding locks that exclude all other readers, writers and |
| 51788 | 51963 | ** checkpointers. |
| 51789 | 51964 | */ |
| 51790 | 51965 | pInfo = walCkptInfo(pWal); |
| 51791 | 51966 | pInfo->nBackfill = 0; |
| 51967 | + pInfo->nBackfillAttempted = pWal->hdr.mxFrame; | |
| 51792 | 51968 | pInfo->aReadMark[0] = 0; |
| 51793 | 51969 | for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; |
| 51794 | 51970 | if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame; |
| 51795 | 51971 | |
| 51796 | 51972 | /* If more than one frame was recovered from the log file, report an |
| @@ -51858,11 +52034,15 @@ | ||
| 51858 | 52034 | assert( pDbFd ); |
| 51859 | 52035 | |
| 51860 | 52036 | /* In the amalgamation, the os_unix.c and os_win.c source files come before |
| 51861 | 52037 | ** this source file. Verify that the #defines of the locking byte offsets |
| 51862 | 52038 | ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value. |
| 52039 | + ** For that matter, if the lock offset ever changes from its initial design | |
| 52040 | + ** value of 120, we need to know that so there is an assert() to check it. | |
| 51863 | 52041 | */ |
| 52042 | + assert( 120==WALINDEX_LOCK_OFFSET ); | |
| 52043 | + assert( 136==WALINDEX_HDR_SIZE ); | |
| 51864 | 52044 | #ifdef WIN_SHM_BASE |
| 51865 | 52045 | assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET ); |
| 51866 | 52046 | #endif |
| 51867 | 52047 | #ifdef UNIX_SHM_BASE |
| 51868 | 52048 | assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET ); |
| @@ -52244,10 +52424,11 @@ | ||
| 52244 | 52424 | pWal->hdr.mxFrame = 0; |
| 52245 | 52425 | sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0])); |
| 52246 | 52426 | memcpy(&pWal->hdr.aSalt[1], &salt1, 4); |
| 52247 | 52427 | walIndexWriteHdr(pWal); |
| 52248 | 52428 | pInfo->nBackfill = 0; |
| 52429 | + pInfo->nBackfillAttempted = 0; | |
| 52249 | 52430 | pInfo->aReadMark[1] = 0; |
| 52250 | 52431 | for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; |
| 52251 | 52432 | assert( pInfo->aReadMark[0]==0 ); |
| 52252 | 52433 | } |
| 52253 | 52434 | |
| @@ -52352,10 +52533,12 @@ | ||
| 52352 | 52533 | if( pInfo->nBackfill<mxSafeFrame |
| 52353 | 52534 | && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK |
| 52354 | 52535 | ){ |
| 52355 | 52536 | i64 nSize; /* Current size of database file */ |
| 52356 | 52537 | u32 nBackfill = pInfo->nBackfill; |
| 52538 | + | |
| 52539 | + pInfo->nBackfillAttempted = mxSafeFrame; | |
| 52357 | 52540 | |
| 52358 | 52541 | /* Sync the WAL to disk */ |
| 52359 | 52542 | if( sync_flags ){ |
| 52360 | 52543 | rc = sqlite3OsSync(pWal->pWalFd, sync_flags); |
| 52361 | 52544 | } |
| @@ -52736,10 +52919,11 @@ | ||
| 52736 | 52919 | volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */ |
| 52737 | 52920 | u32 mxReadMark; /* Largest aReadMark[] value */ |
| 52738 | 52921 | int mxI; /* Index of largest aReadMark[] value */ |
| 52739 | 52922 | int i; /* Loop counter */ |
| 52740 | 52923 | int rc = SQLITE_OK; /* Return code */ |
| 52924 | + u32 mxFrame; /* Wal frame to lock to */ | |
| 52741 | 52925 | |
| 52742 | 52926 | assert( pWal->readLock<0 ); /* Not currently locked */ |
| 52743 | 52927 | |
| 52744 | 52928 | /* Take steps to avoid spinning forever if there is a protocol error. |
| 52745 | 52929 | ** |
| @@ -52799,11 +52983,16 @@ | ||
| 52799 | 52983 | return rc; |
| 52800 | 52984 | } |
| 52801 | 52985 | } |
| 52802 | 52986 | |
| 52803 | 52987 | pInfo = walCkptInfo(pWal); |
| 52804 | - if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){ | |
| 52988 | + if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame | |
| 52989 | +#ifdef SQLITE_ENABLE_SNAPSHOT | |
| 52990 | + && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0 | |
| 52991 | + || 0==memcmp(&pWal->hdr, pWal->pSnapshot, sizeof(WalIndexHdr))) | |
| 52992 | +#endif | |
| 52993 | + ){ | |
| 52805 | 52994 | /* The WAL has been completely backfilled (or it is empty). |
| 52806 | 52995 | ** and can be safely ignored. |
| 52807 | 52996 | */ |
| 52808 | 52997 | rc = walLockShared(pWal, WAL_READ_LOCK(0)); |
| 52809 | 52998 | walShmBarrier(pWal); |
| @@ -52837,89 +53026,92 @@ | ||
| 52837 | 53026 | ** to select one of the aReadMark[] entries that is closest to |
| 52838 | 53027 | ** but not exceeding pWal->hdr.mxFrame and lock that entry. |
| 52839 | 53028 | */ |
| 52840 | 53029 | mxReadMark = 0; |
| 52841 | 53030 | mxI = 0; |
| 53031 | + mxFrame = pWal->hdr.mxFrame; | |
| 53032 | +#ifdef SQLITE_ENABLE_SNAPSHOT | |
| 53033 | + if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){ | |
| 53034 | + mxFrame = pWal->pSnapshot->mxFrame; | |
| 53035 | + } | |
| 53036 | +#endif | |
| 52842 | 53037 | for(i=1; i<WAL_NREADER; i++){ |
| 52843 | 53038 | u32 thisMark = pInfo->aReadMark[i]; |
| 52844 | - if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){ | |
| 53039 | + if( mxReadMark<=thisMark && thisMark<=mxFrame ){ | |
| 52845 | 53040 | assert( thisMark!=READMARK_NOT_USED ); |
| 52846 | 53041 | mxReadMark = thisMark; |
| 52847 | 53042 | mxI = i; |
| 52848 | 53043 | } |
| 52849 | 53044 | } |
| 52850 | - /* There was once an "if" here. The extra "{" is to preserve indentation. */ | |
| 52851 | - { | |
| 52852 | - if( (pWal->readOnly & WAL_SHM_RDONLY)==0 | |
| 52853 | - && (mxReadMark<pWal->hdr.mxFrame || mxI==0) | |
| 52854 | - ){ | |
| 52855 | - for(i=1; i<WAL_NREADER; i++){ | |
| 52856 | - rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); | |
| 52857 | - if( rc==SQLITE_OK ){ | |
| 52858 | - mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame; | |
| 52859 | - mxI = i; | |
| 52860 | - walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); | |
| 52861 | - break; | |
| 52862 | - }else if( rc!=SQLITE_BUSY ){ | |
| 52863 | - return rc; | |
| 52864 | - } | |
| 52865 | - } | |
| 52866 | - } | |
| 52867 | - if( mxI==0 ){ | |
| 52868 | - assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); | |
| 52869 | - return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK; | |
| 52870 | - } | |
| 52871 | - | |
| 52872 | - rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); | |
| 52873 | - if( rc ){ | |
| 52874 | - return rc==SQLITE_BUSY ? WAL_RETRY : rc; | |
| 52875 | - } | |
| 52876 | - /* Now that the read-lock has been obtained, check that neither the | |
| 52877 | - ** value in the aReadMark[] array or the contents of the wal-index | |
| 52878 | - ** header have changed. | |
| 52879 | - ** | |
| 52880 | - ** It is necessary to check that the wal-index header did not change | |
| 52881 | - ** between the time it was read and when the shared-lock was obtained | |
| 52882 | - ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility | |
| 52883 | - ** that the log file may have been wrapped by a writer, or that frames | |
| 52884 | - ** that occur later in the log than pWal->hdr.mxFrame may have been | |
| 52885 | - ** copied into the database by a checkpointer. If either of these things | |
| 52886 | - ** happened, then reading the database with the current value of | |
| 52887 | - ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry | |
| 52888 | - ** instead. | |
| 52889 | - ** | |
| 52890 | - ** Before checking that the live wal-index header has not changed | |
| 52891 | - ** since it was read, set Wal.minFrame to the first frame in the wal | |
| 52892 | - ** file that has not yet been checkpointed. This client will not need | |
| 52893 | - ** to read any frames earlier than minFrame from the wal file - they | |
| 52894 | - ** can be safely read directly from the database file. | |
| 52895 | - ** | |
| 52896 | - ** Because a ShmBarrier() call is made between taking the copy of | |
| 52897 | - ** nBackfill and checking that the wal-header in shared-memory still | |
| 52898 | - ** matches the one cached in pWal->hdr, it is guaranteed that the | |
| 52899 | - ** checkpointer that set nBackfill was not working with a wal-index | |
| 52900 | - ** header newer than that cached in pWal->hdr. If it were, that could | |
| 52901 | - ** cause a problem. The checkpointer could omit to checkpoint | |
| 52902 | - ** a version of page X that lies before pWal->minFrame (call that version | |
| 52903 | - ** A) on the basis that there is a newer version (version B) of the same | |
| 52904 | - ** page later in the wal file. But if version B happens to like past | |
| 52905 | - ** frame pWal->hdr.mxFrame - then the client would incorrectly assume | |
| 52906 | - ** that it can read version A from the database file. However, since | |
| 52907 | - ** we can guarantee that the checkpointer that set nBackfill could not | |
| 52908 | - ** see any pages past pWal->hdr.mxFrame, this problem does not come up. | |
| 52909 | - */ | |
| 52910 | - pWal->minFrame = pInfo->nBackfill+1; | |
| 52911 | - walShmBarrier(pWal); | |
| 52912 | - if( pInfo->aReadMark[mxI]!=mxReadMark | |
| 52913 | - || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) | |
| 52914 | - ){ | |
| 52915 | - walUnlockShared(pWal, WAL_READ_LOCK(mxI)); | |
| 52916 | - return WAL_RETRY; | |
| 52917 | - }else{ | |
| 52918 | - assert( mxReadMark<=pWal->hdr.mxFrame ); | |
| 52919 | - pWal->readLock = (i16)mxI; | |
| 52920 | - } | |
| 53045 | + if( (pWal->readOnly & WAL_SHM_RDONLY)==0 | |
| 53046 | + && (mxReadMark<mxFrame || mxI==0) | |
| 53047 | + ){ | |
| 53048 | + for(i=1; i<WAL_NREADER; i++){ | |
| 53049 | + rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); | |
| 53050 | + if( rc==SQLITE_OK ){ | |
| 53051 | + mxReadMark = pInfo->aReadMark[i] = mxFrame; | |
| 53052 | + mxI = i; | |
| 53053 | + walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); | |
| 53054 | + break; | |
| 53055 | + }else if( rc!=SQLITE_BUSY ){ | |
| 53056 | + return rc; | |
| 53057 | + } | |
| 53058 | + } | |
| 53059 | + } | |
| 53060 | + if( mxI==0 ){ | |
| 53061 | + assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); | |
| 53062 | + return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK; | |
| 53063 | + } | |
| 53064 | + | |
| 53065 | + rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); | |
| 53066 | + if( rc ){ | |
| 53067 | + return rc==SQLITE_BUSY ? WAL_RETRY : rc; | |
| 53068 | + } | |
| 53069 | + /* Now that the read-lock has been obtained, check that neither the | |
| 53070 | + ** value in the aReadMark[] array or the contents of the wal-index | |
| 53071 | + ** header have changed. | |
| 53072 | + ** | |
| 53073 | + ** It is necessary to check that the wal-index header did not change | |
| 53074 | + ** between the time it was read and when the shared-lock was obtained | |
| 53075 | + ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility | |
| 53076 | + ** that the log file may have been wrapped by a writer, or that frames | |
| 53077 | + ** that occur later in the log than pWal->hdr.mxFrame may have been | |
| 53078 | + ** copied into the database by a checkpointer. If either of these things | |
| 53079 | + ** happened, then reading the database with the current value of | |
| 53080 | + ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry | |
| 53081 | + ** instead. | |
| 53082 | + ** | |
| 53083 | + ** Before checking that the live wal-index header has not changed | |
| 53084 | + ** since it was read, set Wal.minFrame to the first frame in the wal | |
| 53085 | + ** file that has not yet been checkpointed. This client will not need | |
| 53086 | + ** to read any frames earlier than minFrame from the wal file - they | |
| 53087 | + ** can be safely read directly from the database file. | |
| 53088 | + ** | |
| 53089 | + ** Because a ShmBarrier() call is made between taking the copy of | |
| 53090 | + ** nBackfill and checking that the wal-header in shared-memory still | |
| 53091 | + ** matches the one cached in pWal->hdr, it is guaranteed that the | |
| 53092 | + ** checkpointer that set nBackfill was not working with a wal-index | |
| 53093 | + ** header newer than that cached in pWal->hdr. If it were, that could | |
| 53094 | + ** cause a problem. The checkpointer could omit to checkpoint | |
| 53095 | + ** a version of page X that lies before pWal->minFrame (call that version | |
| 53096 | + ** A) on the basis that there is a newer version (version B) of the same | |
| 53097 | + ** page later in the wal file. But if version B happens to like past | |
| 53098 | + ** frame pWal->hdr.mxFrame - then the client would incorrectly assume | |
| 53099 | + ** that it can read version A from the database file. However, since | |
| 53100 | + ** we can guarantee that the checkpointer that set nBackfill could not | |
| 53101 | + ** see any pages past pWal->hdr.mxFrame, this problem does not come up. | |
| 53102 | + */ | |
| 53103 | + pWal->minFrame = pInfo->nBackfill+1; | |
| 53104 | + walShmBarrier(pWal); | |
| 53105 | + if( pInfo->aReadMark[mxI]!=mxReadMark | |
| 53106 | + || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) | |
| 53107 | + ){ | |
| 53108 | + walUnlockShared(pWal, WAL_READ_LOCK(mxI)); | |
| 53109 | + return WAL_RETRY; | |
| 53110 | + }else{ | |
| 53111 | + assert( mxReadMark<=pWal->hdr.mxFrame ); | |
| 53112 | + pWal->readLock = (i16)mxI; | |
| 52921 | 53113 | } |
| 52922 | 53114 | return rc; |
| 52923 | 53115 | } |
| 52924 | 53116 | |
| 52925 | 53117 | /* |
| @@ -52938,17 +53130,84 @@ | ||
| 52938 | 53130 | */ |
| 52939 | 53131 | SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ |
| 52940 | 53132 | int rc; /* Return code */ |
| 52941 | 53133 | int cnt = 0; /* Number of TryBeginRead attempts */ |
| 52942 | 53134 | |
| 53135 | +#ifdef SQLITE_ENABLE_SNAPSHOT | |
| 53136 | + int bChanged = 0; | |
| 53137 | + WalIndexHdr *pSnapshot = pWal->pSnapshot; | |
| 53138 | + if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ | |
| 53139 | + bChanged = 1; | |
| 53140 | + } | |
| 53141 | +#endif | |
| 53142 | + | |
| 52943 | 53143 | do{ |
| 52944 | 53144 | rc = walTryBeginRead(pWal, pChanged, 0, ++cnt); |
| 52945 | 53145 | }while( rc==WAL_RETRY ); |
| 52946 | 53146 | testcase( (rc&0xff)==SQLITE_BUSY ); |
| 52947 | 53147 | testcase( (rc&0xff)==SQLITE_IOERR ); |
| 52948 | 53148 | testcase( rc==SQLITE_PROTOCOL ); |
| 52949 | 53149 | testcase( rc==SQLITE_OK ); |
| 53150 | + | |
| 53151 | +#ifdef SQLITE_ENABLE_SNAPSHOT | |
| 53152 | + if( rc==SQLITE_OK ){ | |
| 53153 | + if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ | |
| 53154 | + /* At this point the client has a lock on an aReadMark[] slot holding | |
| 53155 | + ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr | |
| 53156 | + ** is populated with the wal-index header corresponding to the head | |
| 53157 | + ** of the wal file. Verify that pSnapshot is still valid before | |
| 53158 | + ** continuing. Reasons why pSnapshot might no longer be valid: | |
| 53159 | + ** | |
| 53160 | + ** (1) The WAL file has been reset since the snapshot was taken. | |
| 53161 | + ** In this case, the salt will have changed. | |
| 53162 | + ** | |
| 53163 | + ** (2) A checkpoint as been attempted that wrote frames past | |
| 53164 | + ** pSnapshot->mxFrame into the database file. Note that the | |
| 53165 | + ** checkpoint need not have completed for this to cause problems. | |
| 53166 | + */ | |
| 53167 | + volatile WalCkptInfo *pInfo = walCkptInfo(pWal); | |
| 53168 | + | |
| 53169 | + assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 ); | |
| 53170 | + assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame ); | |
| 53171 | + | |
| 53172 | + /* It is possible that there is a checkpointer thread running | |
| 53173 | + ** concurrent with this code. If this is the case, it may be that the | |
| 53174 | + ** checkpointer has already determined that it will checkpoint | |
| 53175 | + ** snapshot X, where X is later in the wal file than pSnapshot, but | |
| 53176 | + ** has not yet set the pInfo->nBackfillAttempted variable to indicate | |
| 53177 | + ** its intent. To avoid the race condition this leads to, ensure that | |
| 53178 | + ** there is no checkpointer process by taking a shared CKPT lock | |
| 53179 | + ** before checking pInfo->nBackfillAttempted. */ | |
| 53180 | + rc = walLockShared(pWal, WAL_CKPT_LOCK); | |
| 53181 | + | |
| 53182 | + if( rc==SQLITE_OK ){ | |
| 53183 | + /* Check that the wal file has not been wrapped. Assuming that it has | |
| 53184 | + ** not, also check that no checkpointer has attempted to checkpoint any | |
| 53185 | + ** frames beyond pSnapshot->mxFrame. If either of these conditions are | |
| 53186 | + ** true, return SQLITE_BUSY_SNAPSHOT. Otherwise, overwrite pWal->hdr | |
| 53187 | + ** with *pSnapshot and set *pChanged as appropriate for opening the | |
| 53188 | + ** snapshot. */ | |
| 53189 | + if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt)) | |
| 53190 | + && pSnapshot->mxFrame>=pInfo->nBackfillAttempted | |
| 53191 | + ){ | |
| 53192 | + memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr)); | |
| 53193 | + *pChanged = bChanged; | |
| 53194 | + }else{ | |
| 53195 | + rc = SQLITE_BUSY_SNAPSHOT; | |
| 53196 | + } | |
| 53197 | + | |
| 53198 | + /* Release the shared CKPT lock obtained above. */ | |
| 53199 | + walUnlockShared(pWal, WAL_CKPT_LOCK); | |
| 53200 | + } | |
| 53201 | + | |
| 53202 | + | |
| 53203 | + if( rc!=SQLITE_OK ){ | |
| 53204 | + sqlite3WalEndReadTransaction(pWal); | |
| 53205 | + } | |
| 53206 | + } | |
| 53207 | + } | |
| 53208 | +#endif | |
| 52950 | 53209 | return rc; |
| 52951 | 53210 | } |
| 52952 | 53211 | |
| 52953 | 53212 | /* |
| 52954 | 53213 | ** Finish with a read transaction. All this does is release the |
| @@ -53754,10 +54013,39 @@ | ||
| 53754 | 54013 | */ |
| 53755 | 54014 | SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){ |
| 53756 | 54015 | return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ); |
| 53757 | 54016 | } |
| 53758 | 54017 | |
| 54018 | +#ifdef SQLITE_ENABLE_SNAPSHOT | |
| 54019 | +/* Create a snapshot object. The content of a snapshot is opaque to | |
| 54020 | +** every other subsystem, so the WAL module can put whatever it needs | |
| 54021 | +** in the object. | |
| 54022 | +*/ | |
| 54023 | +SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){ | |
| 54024 | + int rc = SQLITE_OK; | |
| 54025 | + WalIndexHdr *pRet; | |
| 54026 | + | |
| 54027 | + assert( pWal->readLock>=0 && pWal->writeLock==0 ); | |
| 54028 | + | |
| 54029 | + pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr)); | |
| 54030 | + if( pRet==0 ){ | |
| 54031 | + rc = SQLITE_NOMEM; | |
| 54032 | + }else{ | |
| 54033 | + memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr)); | |
| 54034 | + *ppSnapshot = (sqlite3_snapshot*)pRet; | |
| 54035 | + } | |
| 54036 | + | |
| 54037 | + return rc; | |
| 54038 | +} | |
| 54039 | + | |
| 54040 | +/* Try to open on pSnapshot when the next read-transaction starts | |
| 54041 | +*/ | |
| 54042 | +SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){ | |
| 54043 | + pWal->pSnapshot = (WalIndexHdr*)pSnapshot; | |
| 54044 | +} | |
| 54045 | +#endif /* SQLITE_ENABLE_SNAPSHOT */ | |
| 54046 | + | |
| 53759 | 54047 | #ifdef SQLITE_ENABLE_ZIPVFS |
| 53760 | 54048 | /* |
| 53761 | 54049 | ** If the argument is not NULL, it points to a Wal object that holds a |
| 53762 | 54050 | ** read-lock. This function returns the database page-size if it is known, |
| 53763 | 54051 | ** or zero if it is not (or if pWal is NULL). |
| @@ -62312,12 +62600,11 @@ | ||
| 62312 | 62600 | ** if sibling page iOld had the same page number as pNew, and if |
| 62313 | 62601 | ** pCell really was a part of sibling page iOld (not a divider or |
| 62314 | 62602 | ** overflow cell), we can skip updating the pointer map entries. */ |
| 62315 | 62603 | if( iOld>=nNew |
| 62316 | 62604 | || pNew->pgno!=aPgno[iOld] |
| 62317 | - || pCell<aOld | |
| 62318 | - || pCell>=&aOld[usableSize] | |
| 62605 | + || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize]) | |
| 62319 | 62606 | ){ |
| 62320 | 62607 | if( !leafCorrection ){ |
| 62321 | 62608 | ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); |
| 62322 | 62609 | } |
| 62323 | 62610 | if( cachedCellSize(&b,i)>pNew->minLocal ){ |
| @@ -67343,21 +67630,21 @@ | ||
| 67343 | 67630 | (i*2+1)*sizeof(p->aLabel[0])); |
| 67344 | 67631 | } |
| 67345 | 67632 | if( p->aLabel ){ |
| 67346 | 67633 | p->aLabel[i] = -1; |
| 67347 | 67634 | } |
| 67348 | - return -1-i; | |
| 67635 | + return ADDR(i); | |
| 67349 | 67636 | } |
| 67350 | 67637 | |
| 67351 | 67638 | /* |
| 67352 | 67639 | ** Resolve label "x" to be the address of the next instruction to |
| 67353 | 67640 | ** be inserted. The parameter "x" must have been obtained from |
| 67354 | 67641 | ** a prior call to sqlite3VdbeMakeLabel(). |
| 67355 | 67642 | */ |
| 67356 | 67643 | SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){ |
| 67357 | 67644 | Parse *p = v->pParse; |
| 67358 | - int j = -1-x; | |
| 67645 | + int j = ADDR(x); | |
| 67359 | 67646 | assert( v->magic==VDBE_MAGIC_INIT ); |
| 67360 | 67647 | assert( j<p->nLabel ); |
| 67361 | 67648 | assert( j>=0 ); |
| 67362 | 67649 | if( p->aLabel ){ |
| 67363 | 67650 | p->aLabel[j] = v->nOp; |
| @@ -67580,12 +67867,12 @@ | ||
| 67580 | 67867 | } |
| 67581 | 67868 | } |
| 67582 | 67869 | |
| 67583 | 67870 | pOp->opflags = sqlite3OpcodeProperty[opcode]; |
| 67584 | 67871 | if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){ |
| 67585 | - assert( -1-pOp->p2<pParse->nLabel ); | |
| 67586 | - pOp->p2 = aLabel[-1-pOp->p2]; | |
| 67872 | + assert( ADDR(pOp->p2)<pParse->nLabel ); | |
| 67873 | + pOp->p2 = aLabel[ADDR(pOp->p2)]; | |
| 67587 | 67874 | } |
| 67588 | 67875 | } |
| 67589 | 67876 | sqlite3DbFree(p->db, pParse->aLabel); |
| 67590 | 67877 | pParse->aLabel = 0; |
| 67591 | 67878 | pParse->nLabel = 0; |
| @@ -67638,19 +67925,14 @@ | ||
| 67638 | 67925 | return 0; |
| 67639 | 67926 | } |
| 67640 | 67927 | addr = p->nOp; |
| 67641 | 67928 | pOut = &p->aOp[addr]; |
| 67642 | 67929 | for(i=0; i<nOp; i++, aOp++, pOut++){ |
| 67643 | - int p2 = aOp->p2; | |
| 67644 | 67930 | pOut->opcode = aOp->opcode; |
| 67645 | 67931 | pOut->p1 = aOp->p1; |
| 67646 | - if( p2<0 ){ | |
| 67647 | - assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP ); | |
| 67648 | - pOut->p2 = addr + ADDR(p2); | |
| 67649 | - }else{ | |
| 67650 | - pOut->p2 = p2; | |
| 67651 | - } | |
| 67932 | + pOut->p2 = aOp->p2; | |
| 67933 | + assert( aOp->p2>=0 ); | |
| 67652 | 67934 | pOut->p3 = aOp->p3; |
| 67653 | 67935 | pOut->p4type = P4_NOTUSED; |
| 67654 | 67936 | pOut->p4.p = 0; |
| 67655 | 67937 | pOut->p5 = 0; |
| 67656 | 67938 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| @@ -68858,13 +69140,11 @@ | ||
| 68858 | 69140 | p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort); |
| 68859 | 69141 | if( pParse->explain && nMem<10 ){ |
| 68860 | 69142 | nMem = 10; |
| 68861 | 69143 | } |
| 68862 | 69144 | memset(zCsr, 0, nFree); |
| 68863 | - nFree -= (zCsr - (u8*)0)&7; | |
| 68864 | - zCsr += (zCsr - (u8*)0)&7; | |
| 68865 | - assert( EIGHT_BYTE_ALIGNMENT(zCsr) ); | |
| 69145 | + assert( EIGHT_BYTE_ALIGNMENT(&zCsr[nFree]) ); | |
| 68866 | 69146 | p->expired = 0; |
| 68867 | 69147 | |
| 68868 | 69148 | /* Memory for registers, parameters, cursor, etc, is allocated in two |
| 68869 | 69149 | ** passes. On the first pass, we try to reuse unused space at the |
| 68870 | 69150 | ** end of the opcode array. If we are unable to satisfy all memory |
| @@ -70232,11 +70512,11 @@ | ||
| 70232 | 70512 | /* String or blob */ |
| 70233 | 70513 | if( serial_type>=12 ){ |
| 70234 | 70514 | assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0) |
| 70235 | 70515 | == (int)sqlite3VdbeSerialTypeLen(serial_type) ); |
| 70236 | 70516 | len = pMem->n; |
| 70237 | - memcpy(buf, pMem->z, len); | |
| 70517 | + if( len>0 ) memcpy(buf, pMem->z, len); | |
| 70238 | 70518 | return len; |
| 70239 | 70519 | } |
| 70240 | 70520 | |
| 70241 | 70521 | /* NULL or constants 0 or 1 */ |
| 70242 | 70522 | return 0; |
| @@ -86421,10 +86701,11 @@ | ||
| 86421 | 86701 | ** if any. Before returning, *pzBuffer is set to the first byte past the |
| 86422 | 86702 | ** portion of the buffer copied into by this function. |
| 86423 | 86703 | */ |
| 86424 | 86704 | static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){ |
| 86425 | 86705 | Expr *pNew = 0; /* Value to return */ |
| 86706 | + assert( flags==0 || flags==EXPRDUP_REDUCE ); | |
| 86426 | 86707 | if( p ){ |
| 86427 | 86708 | const int isReduced = (flags&EXPRDUP_REDUCE); |
| 86428 | 86709 | u8 *zAlloc; |
| 86429 | 86710 | u32 staticFlag = 0; |
| 86430 | 86711 | |
| @@ -86457,11 +86738,13 @@ | ||
| 86457 | 86738 | assert( ExprHasProperty(p, EP_Reduced)==0 ); |
| 86458 | 86739 | memcpy(zAlloc, p, nNewSize); |
| 86459 | 86740 | }else{ |
| 86460 | 86741 | int nSize = exprStructSize(p); |
| 86461 | 86742 | memcpy(zAlloc, p, nSize); |
| 86462 | - memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); | |
| 86743 | + if( nSize<EXPR_FULLSIZE ){ | |
| 86744 | + memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); | |
| 86745 | + } | |
| 86463 | 86746 | } |
| 86464 | 86747 | |
| 86465 | 86748 | /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ |
| 86466 | 86749 | pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); |
| 86467 | 86750 | pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); |
| @@ -86547,10 +86830,11 @@ | ||
| 86547 | 86830 | ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a |
| 86548 | 86831 | ** truncated version of the usual Expr structure that will be stored as |
| 86549 | 86832 | ** part of the in-memory representation of the database schema. |
| 86550 | 86833 | */ |
| 86551 | 86834 | SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ |
| 86835 | + assert( flags==0 || flags==EXPRDUP_REDUCE ); | |
| 86552 | 86836 | return exprDup(db, p, flags, 0); |
| 86553 | 86837 | } |
| 86554 | 86838 | SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ |
| 86555 | 86839 | ExprList *pNew; |
| 86556 | 86840 | struct ExprList_item *pItem, *pOldItem; |
| @@ -115734,35 +116018,16 @@ | ||
| 115734 | 116018 | |
| 115735 | 116019 | /* Generate code to destroy the database record of the trigger. |
| 115736 | 116020 | */ |
| 115737 | 116021 | assert( pTable!=0 ); |
| 115738 | 116022 | if( (v = sqlite3GetVdbe(pParse))!=0 ){ |
| 115739 | - int base; | |
| 115740 | - static const int iLn = VDBE_OFFSET_LINENO(2); | |
| 115741 | - static const VdbeOpList dropTrigger[] = { | |
| 115742 | - { OP_Rewind, 0, ADDR(9), 0}, | |
| 115743 | - { OP_String8, 0, 1, 0}, /* 1 */ | |
| 115744 | - { OP_Column, 0, 1, 2}, | |
| 115745 | - { OP_Ne, 2, ADDR(8), 1}, | |
| 115746 | - { OP_String8, 0, 1, 0}, /* 4: "trigger" */ | |
| 115747 | - { OP_Column, 0, 0, 2}, | |
| 115748 | - { OP_Ne, 2, ADDR(8), 1}, | |
| 115749 | - { OP_Delete, 0, 0, 0}, | |
| 115750 | - { OP_Next, 0, ADDR(1), 0}, /* 8 */ | |
| 115751 | - }; | |
| 115752 | - | |
| 115753 | - sqlite3BeginWriteOperation(pParse, 0, iDb); | |
| 115754 | - sqlite3OpenMasterTable(pParse, iDb); | |
| 115755 | - base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger, iLn); | |
| 115756 | - sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT); | |
| 115757 | - sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC); | |
| 116023 | + sqlite3NestedParse(pParse, | |
| 116024 | + "DELETE FROM %Q.%s WHERE name=%Q AND type='trigger'", | |
| 116025 | + db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pTrigger->zName | |
| 116026 | + ); | |
| 115758 | 116027 | sqlite3ChangeCookie(pParse, iDb); |
| 115759 | - sqlite3VdbeAddOp2(v, OP_Close, 0, 0); | |
| 115760 | 116028 | sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0); |
| 115761 | - if( pParse->nMem<3 ){ | |
| 115762 | - pParse->nMem = 3; | |
| 115763 | - } | |
| 115764 | 116029 | } |
| 115765 | 116030 | } |
| 115766 | 116031 | |
| 115767 | 116032 | /* |
| 115768 | 116033 | ** Remove a trigger from the hash tables of the sqlite* pointer. |
| @@ -135613,10 +135878,93 @@ | ||
| 135613 | 135878 | } |
| 135614 | 135879 | #endif |
| 135615 | 135880 | pBt = sqlite3DbNameToBtree(db, zDbName); |
| 135616 | 135881 | return pBt ? sqlite3BtreeIsReadonly(pBt) : -1; |
| 135617 | 135882 | } |
| 135883 | + | |
| 135884 | +#ifdef SQLITE_ENABLE_SNAPSHOT | |
| 135885 | +/* | |
| 135886 | +** Obtain a snapshot handle for the snapshot of database zDb currently | |
| 135887 | +** being read by handle db. | |
| 135888 | +*/ | |
| 135889 | +SQLITE_API int SQLITE_STDCALL sqlite3_snapshot_get( | |
| 135890 | + sqlite3 *db, | |
| 135891 | + const char *zDb, | |
| 135892 | + sqlite3_snapshot **ppSnapshot | |
| 135893 | +){ | |
| 135894 | + int rc = SQLITE_ERROR; | |
| 135895 | +#ifndef SQLITE_OMIT_WAL | |
| 135896 | + int iDb; | |
| 135897 | + | |
| 135898 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 135899 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 135900 | + return SQLITE_MISUSE_BKPT; | |
| 135901 | + } | |
| 135902 | +#endif | |
| 135903 | + sqlite3_mutex_enter(db->mutex); | |
| 135904 | + | |
| 135905 | + iDb = sqlite3FindDbName(db, zDb); | |
| 135906 | + if( iDb==0 || iDb>1 ){ | |
| 135907 | + Btree *pBt = db->aDb[iDb].pBt; | |
| 135908 | + if( 0==sqlite3BtreeIsInTrans(pBt) ){ | |
| 135909 | + rc = sqlite3BtreeBeginTrans(pBt, 0); | |
| 135910 | + if( rc==SQLITE_OK ){ | |
| 135911 | + rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot); | |
| 135912 | + } | |
| 135913 | + } | |
| 135914 | + } | |
| 135915 | + | |
| 135916 | + sqlite3_mutex_leave(db->mutex); | |
| 135917 | +#endif /* SQLITE_OMIT_WAL */ | |
| 135918 | + return rc; | |
| 135919 | +} | |
| 135920 | + | |
| 135921 | +/* | |
| 135922 | +** Open a read-transaction on the snapshot idendified by pSnapshot. | |
| 135923 | +*/ | |
| 135924 | +SQLITE_API int SQLITE_STDCALL sqlite3_snapshot_open( | |
| 135925 | + sqlite3 *db, | |
| 135926 | + const char *zDb, | |
| 135927 | + sqlite3_snapshot *pSnapshot | |
| 135928 | +){ | |
| 135929 | + int rc = SQLITE_ERROR; | |
| 135930 | +#ifndef SQLITE_OMIT_WAL | |
| 135931 | + | |
| 135932 | +#ifdef SQLITE_ENABLE_API_ARMOR | |
| 135933 | + if( !sqlite3SafetyCheckOk(db) ){ | |
| 135934 | + return SQLITE_MISUSE_BKPT; | |
| 135935 | + } | |
| 135936 | +#endif | |
| 135937 | + sqlite3_mutex_enter(db->mutex); | |
| 135938 | + if( db->autoCommit==0 ){ | |
| 135939 | + int iDb; | |
| 135940 | + iDb = sqlite3FindDbName(db, zDb); | |
| 135941 | + if( iDb==0 || iDb>1 ){ | |
| 135942 | + Btree *pBt = db->aDb[iDb].pBt; | |
| 135943 | + if( 0==sqlite3BtreeIsInReadTrans(pBt) ){ | |
| 135944 | + rc = sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), pSnapshot); | |
| 135945 | + if( rc==SQLITE_OK ){ | |
| 135946 | + rc = sqlite3BtreeBeginTrans(pBt, 0); | |
| 135947 | + sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), 0); | |
| 135948 | + } | |
| 135949 | + } | |
| 135950 | + } | |
| 135951 | + } | |
| 135952 | + | |
| 135953 | + sqlite3_mutex_leave(db->mutex); | |
| 135954 | +#endif /* SQLITE_OMIT_WAL */ | |
| 135955 | + return rc; | |
| 135956 | +} | |
| 135957 | + | |
| 135958 | +/* | |
| 135959 | +** Free a snapshot handle obtained from sqlite3_snapshot_get(). | |
| 135960 | +*/ | |
| 135961 | +SQLITE_API void SQLITE_STDCALL sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){ | |
| 135962 | + sqlite3_free(pSnapshot); | |
| 135963 | +} | |
| 135964 | +#endif /* SQLITE_ENABLE_SNAPSHOT */ | |
| 135965 | + | |
| 135618 | 135966 | |
| 135619 | 135967 | /************** End of main.c ************************************************/ |
| 135620 | 135968 | /************** Begin file notify.c ******************************************/ |
| 135621 | 135969 | /* |
| 135622 | 135970 | ** 2009 March 3 |
| @@ -181775,11 +182123,11 @@ | ||
| 181775 | 182123 | sqlite3_context *pCtx, /* Function call context */ |
| 181776 | 182124 | int nArg, /* Number of args */ |
| 181777 | 182125 | sqlite3_value **apVal /* Function arguments */ |
| 181778 | 182126 | ){ |
| 181779 | 182127 | assert( nArg==0 ); |
| 181780 | - sqlite3_result_text(pCtx, "fts5: 2015-12-03 12:01:54 d96de532cc4a192cfebae900701dcee0a7d29273", -1, SQLITE_TRANSIENT); | |
| 182128 | + sqlite3_result_text(pCtx, "fts5: 2015-12-10 17:59:50 05bc4f920ce23da48d1da6cd36a956fd6fd7c862", -1, SQLITE_TRANSIENT); | |
| 181781 | 182129 | } |
| 181782 | 182130 | |
| 181783 | 182131 | static int fts5Init(sqlite3 *db){ |
| 181784 | 182132 | static const sqlite3_module fts5Mod = { |
| 181785 | 182133 | /* iVersion */ 2, |
| 181786 | 182134 |
| --- src/sqlite3.c | |
| +++ src/sqlite3.c | |
| @@ -325,11 +325,11 @@ | |
| 325 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 326 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 327 | */ |
| 328 | #define SQLITE_VERSION "3.10.0" |
| 329 | #define SQLITE_VERSION_NUMBER 3010000 |
| 330 | #define SQLITE_SOURCE_ID "2015-12-07 18:18:33 e7ae120d04cffafd9bc2b4ecd68571c17e05ed72" |
| 331 | |
| 332 | /* |
| 333 | ** CAPI3REF: Run-Time Library Version Numbers |
| 334 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 335 | ** |
| @@ -4617,12 +4617,12 @@ | |
| 4617 | ** |
| 4618 | ** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object |
| 4619 | ** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer |
| 4620 | ** then sqlite3_value_free(V) is a harmless no-op. |
| 4621 | */ |
| 4622 | SQLITE_API SQLITE_EXPERIMENTAL sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value*); |
| 4623 | SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_value_free(sqlite3_value*); |
| 4624 | |
| 4625 | /* |
| 4626 | ** CAPI3REF: Obtain Aggregate Function Context |
| 4627 | ** METHOD: sqlite3_context |
| 4628 | ** |
| @@ -8063,37 +8063,131 @@ | |
| 8063 | SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); |
| 8064 | |
| 8065 | /* |
| 8066 | ** CAPI3REF: Flush caches to disk mid-transaction |
| 8067 | ** |
| 8068 | ** If a write-transaction is open when this function is called, any dirty |
| 8069 | ** pages in the pager-cache that are not currently in use are written out |
| 8070 | ** to disk. A dirty page may be in use if a database cursor created by an |
| 8071 | ** active SQL statement is reading from it, or if it is page 1 of a database |
| 8072 | ** file (page 1 is always "in use"). Dirty pages are flushed for all |
| 8073 | ** databases - "main", "temp" and any attached databases. |
| 8074 | ** |
| 8075 | ** If this function needs to obtain extra database locks before dirty pages |
| 8076 | ** can be flushed to disk, it does so. If said locks cannot be obtained |
| 8077 | ** immediately and there is a busy-handler callback configured, it is invoked |
| 8078 | ** in the usual manner. If the required lock still cannot be obtained, then |
| 8079 | ** the database is skipped and an attempt made to flush any dirty pages |
| 8080 | ** belonging to the next (if any) database. If any databases are skipped |
| 8081 | ** because locks cannot be obtained, but no other error occurs, this |
| 8082 | ** function returns SQLITE_BUSY. |
| 8083 | ** |
| 8084 | ** If any other error occurs while flushing dirty pages to disk (for |
| 8085 | ** example an IO error or out-of-memory condition), then processing is |
| 8086 | ** abandoned and an SQLite error code returned to the caller immediately. |
| 8087 | ** |
| 8088 | ** Otherwise, if no error occurs, SQLITE_OK is returned. |
| 8089 | ** |
| 8090 | ** This function does not set the database handle error code or message |
| 8091 | ** returned by the sqlite3_errcode() and sqlite3_errmsg() functions. |
| 8092 | */ |
| 8093 | SQLITE_API int SQLITE_STDCALL sqlite3_db_cacheflush(sqlite3*); |
| 8094 | |
| 8095 | /* |
| 8096 | ** Undo the hack that converts floating point types to integer for |
| 8097 | ** builds on processors without floating point support. |
| 8098 | */ |
| 8099 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| @@ -9017,10 +9111,25 @@ | |
| 9017 | #else /* Generates a warning - but it always works */ |
| 9018 | # define SQLITE_INT_TO_PTR(X) ((void*)(X)) |
| 9019 | # define SQLITE_PTR_TO_INT(X) ((int)(X)) |
| 9020 | #endif |
| 9021 | |
| 9022 | /* |
| 9023 | ** A macro to hint to the compiler that a function should not be |
| 9024 | ** inlined. |
| 9025 | */ |
| 9026 | #if defined(__GNUC__) |
| @@ -11086,10 +11195,14 @@ | |
| 11086 | SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*); |
| 11087 | SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager); |
| 11088 | SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager); |
| 11089 | SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen); |
| 11090 | SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager); |
| 11091 | #endif |
| 11092 | |
| 11093 | #ifdef SQLITE_ENABLE_ZIPVFS |
| 11094 | SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager); |
| 11095 | #endif |
| @@ -13649,10 +13762,11 @@ | |
| 13649 | char *zText; /* The string collected so far */ |
| 13650 | int nChar; /* Length of the string so far */ |
| 13651 | int nAlloc; /* Amount of space allocated in zText */ |
| 13652 | int mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */ |
| 13653 | u8 accError; /* STRACCUM_NOMEM or STRACCUM_TOOBIG */ |
| 13654 | }; |
| 13655 | #define STRACCUM_NOMEM 1 |
| 13656 | #define STRACCUM_TOOBIG 2 |
| 13657 | |
| 13658 | /* |
| @@ -21930,11 +22044,11 @@ | |
| 21930 | ** would be much more complicated.) */ |
| 21931 | assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); |
| 21932 | scratchAllocOut--; |
| 21933 | #endif |
| 21934 | |
| 21935 | if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){ |
| 21936 | /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */ |
| 21937 | ScratchFreeslot *pSlot; |
| 21938 | pSlot = (ScratchFreeslot*)p; |
| 21939 | sqlite3_mutex_enter(mem0.mutex); |
| 21940 | pSlot->pNext = mem0.pScratchFree; |
| @@ -21966,11 +22080,11 @@ | |
| 21966 | /* |
| 21967 | ** TRUE if p is a lookaside memory allocation from db |
| 21968 | */ |
| 21969 | #ifndef SQLITE_OMIT_LOOKASIDE |
| 21970 | static int isLookaside(sqlite3 *db, void *p){ |
| 21971 | return p>=db->lookaside.pStart && p<db->lookaside.pEnd; |
| 21972 | } |
| 21973 | #else |
| 21974 | #define isLookaside(A,B) 0 |
| 21975 | #endif |
| 21976 | |
| @@ -23108,12 +23222,13 @@ | |
| 23108 | if( p->mxAlloc==0 ){ |
| 23109 | N = p->nAlloc - p->nChar - 1; |
| 23110 | setStrAccumError(p, STRACCUM_TOOBIG); |
| 23111 | return N; |
| 23112 | }else{ |
| 23113 | char *zOld = (p->zText==p->zBase ? 0 : p->zText); |
| 23114 | i64 szNew = p->nChar; |
| 23115 | szNew += N + 1; |
| 23116 | if( szNew+p->nChar<=p->mxAlloc ){ |
| 23117 | /* Force exponential buffer size growth as long as it does not overflow, |
| 23118 | ** to avoid having to call this routine too often */ |
| 23119 | szNew += p->nChar; |
| @@ -23130,13 +23245,14 @@ | |
| 23130 | }else{ |
| 23131 | zNew = sqlite3_realloc64(zOld, p->nAlloc); |
| 23132 | } |
| 23133 | if( zNew ){ |
| 23134 | assert( p->zText!=0 || p->nChar==0 ); |
| 23135 | if( p->zText==p->zBase && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
| 23136 | p->zText = zNew; |
| 23137 | p->nAlloc = sqlite3DbMallocSize(p->db, zNew); |
| 23138 | }else{ |
| 23139 | sqlite3StrAccumReset(p); |
| 23140 | setStrAccumError(p, STRACCUM_NOMEM); |
| 23141 | return 0; |
| 23142 | } |
| @@ -23150,10 +23266,11 @@ | |
| 23150 | SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){ |
| 23151 | testcase( p->nChar + (i64)N > 0x7fffffff ); |
| 23152 | if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ |
| 23153 | return; |
| 23154 | } |
| 23155 | while( (N--)>0 ) p->zText[p->nChar++] = c; |
| 23156 | } |
| 23157 | |
| 23158 | /* |
| 23159 | ** The StrAccum "p" is not large enough to accept N new bytes of z[]. |
| @@ -23167,10 +23284,11 @@ | |
| 23167 | N = sqlite3StrAccumEnlarge(p, N); |
| 23168 | if( N>0 ){ |
| 23169 | memcpy(&p->zText[p->nChar], z, N); |
| 23170 | p->nChar += N; |
| 23171 | } |
| 23172 | } |
| 23173 | |
| 23174 | /* |
| 23175 | ** Append N bytes of text from z to the StrAccum object. Increase the |
| 23176 | ** size of the memory allocation for StrAccum if necessary. |
| @@ -23202,15 +23320,17 @@ | |
| 23202 | ** Return a pointer to the resulting string. Return a NULL |
| 23203 | ** pointer if any kind of error was encountered. |
| 23204 | */ |
| 23205 | SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){ |
| 23206 | if( p->zText ){ |
| 23207 | p->zText[p->nChar] = 0; |
| 23208 | if( p->mxAlloc>0 && p->zText==p->zBase ){ |
| 23209 | p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); |
| 23210 | if( p->zText ){ |
| 23211 | memcpy(p->zText, p->zBase, p->nChar+1); |
| 23212 | }else{ |
| 23213 | setStrAccumError(p, STRACCUM_NOMEM); |
| 23214 | } |
| 23215 | } |
| 23216 | } |
| @@ -23219,12 +23339,14 @@ | |
| 23219 | |
| 23220 | /* |
| 23221 | ** Reset an StrAccum string. Reclaim all malloced memory. |
| 23222 | */ |
| 23223 | SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){ |
| 23224 | if( p->zText!=p->zBase ){ |
| 23225 | sqlite3DbFree(p->db, p->zText); |
| 23226 | } |
| 23227 | p->zText = 0; |
| 23228 | } |
| 23229 | |
| 23230 | /* |
| @@ -23246,10 +23368,11 @@ | |
| 23246 | p->db = db; |
| 23247 | p->nChar = 0; |
| 23248 | p->nAlloc = n; |
| 23249 | p->mxAlloc = mx; |
| 23250 | p->accError = 0; |
| 23251 | } |
| 23252 | |
| 23253 | /* |
| 23254 | ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 23255 | ** %-conversion extensions. |
| @@ -43273,10 +43396,15 @@ | |
| 43273 | /* Return true if the argument is non-NULL and the WAL module is using |
| 43274 | ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the |
| 43275 | ** WAL module is using shared-memory, return false. |
| 43276 | */ |
| 43277 | SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal); |
| 43278 | |
| 43279 | #ifdef SQLITE_ENABLE_ZIPVFS |
| 43280 | /* If the WAL file is not empty, return the number of bytes of content |
| 43281 | ** stored in each frame (i.e. the db page-size when the WAL was created). |
| 43282 | */ |
| @@ -50566,10 +50694,38 @@ | |
| 50566 | } |
| 50567 | } |
| 50568 | return rc; |
| 50569 | } |
| 50570 | |
| 50571 | #endif /* !SQLITE_OMIT_WAL */ |
| 50572 | |
| 50573 | #ifdef SQLITE_ENABLE_ZIPVFS |
| 50574 | /* |
| 50575 | ** A read-lock must be held on the pager when this function is called. If |
| @@ -50861,11 +51017,12 @@ | |
| 50861 | #define WAL_MAX_VERSION 3007000 |
| 50862 | #define WALINDEX_MAX_VERSION 3007000 |
| 50863 | |
| 50864 | /* |
| 50865 | ** Indices of various locking bytes. WAL_NREADER is the number |
| 50866 | ** of available reader locks and should be at least 3. |
| 50867 | */ |
| 50868 | #define WAL_WRITE_LOCK 0 |
| 50869 | #define WAL_ALL_BUT_WRITE 1 |
| 50870 | #define WAL_CKPT_LOCK 1 |
| 50871 | #define WAL_RECOVER_LOCK 2 |
| @@ -50881,11 +51038,14 @@ | |
| 50881 | |
| 50882 | /* |
| 50883 | ** The following object holds a copy of the wal-index header content. |
| 50884 | ** |
| 50885 | ** The actual header in the wal-index consists of two copies of this |
| 50886 | ** object. |
| 50887 | ** |
| 50888 | ** The szPage value can be any power of 2 between 512 and 32768, inclusive. |
| 50889 | ** Or it can be 1 to represent a 65536-byte page. The latter case was |
| 50890 | ** added in 3.7.1 when support for 64K pages was added. |
| 50891 | */ |
| @@ -50913,10 +51073,20 @@ | |
| 50913 | ** database "backfilling".) The nBackfill number is never greater than |
| 50914 | ** WalIndexHdr.mxFrame. nBackfill can only be increased by threads |
| 50915 | ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread). |
| 50916 | ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from |
| 50917 | ** mxFrame back to zero when the WAL is reset. |
| 50918 | ** |
| 50919 | ** There is one entry in aReadMark[] for each reader lock. If a reader |
| 50920 | ** holds read-lock K, then the value in aReadMark[K] is no greater than |
| 50921 | ** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff) |
| 50922 | ** for any aReadMark[] means that entry is unused. aReadMark[0] is |
| @@ -50953,22 +51123,24 @@ | |
| 50953 | ** order to read from any aReadMark[] entries. |
| 50954 | */ |
| 50955 | struct WalCkptInfo { |
| 50956 | u32 nBackfill; /* Number of WAL frames backfilled into DB */ |
| 50957 | u32 aReadMark[WAL_NREADER]; /* Reader marks */ |
| 50958 | }; |
| 50959 | #define READMARK_NOT_USED 0xffffffff |
| 50960 | |
| 50961 | |
| 50962 | /* A block of WALINDEX_LOCK_RESERVED bytes beginning at |
| 50963 | ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems |
| 50964 | ** only support mandatory file-locks, we do not read or write data |
| 50965 | ** from the region of the file on which locks are applied. |
| 50966 | */ |
| 50967 | #define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo)) |
| 50968 | #define WALINDEX_LOCK_RESERVED 16 |
| 50969 | #define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED) |
| 50970 | |
| 50971 | /* Size of header before each frame in wal */ |
| 50972 | #define WAL_FRAME_HDRSIZE 24 |
| 50973 | |
| 50974 | /* Size of write ahead log header, including checksum. */ |
| @@ -51023,10 +51195,13 @@ | |
| 51023 | const char *zWalName; /* Name of WAL file */ |
| 51024 | u32 nCkpt; /* Checkpoint sequence counter in the wal-header */ |
| 51025 | #ifdef SQLITE_DEBUG |
| 51026 | u8 lockError; /* True if a locking error has occurred */ |
| 51027 | #endif |
| 51028 | }; |
| 51029 | |
| 51030 | /* |
| 51031 | ** Candidate values for Wal.exclusiveMode. |
| 51032 | */ |
| @@ -51787,10 +51962,11 @@ | |
| 51787 | ** currently holding locks that exclude all other readers, writers and |
| 51788 | ** checkpointers. |
| 51789 | */ |
| 51790 | pInfo = walCkptInfo(pWal); |
| 51791 | pInfo->nBackfill = 0; |
| 51792 | pInfo->aReadMark[0] = 0; |
| 51793 | for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; |
| 51794 | if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame; |
| 51795 | |
| 51796 | /* If more than one frame was recovered from the log file, report an |
| @@ -51858,11 +52034,15 @@ | |
| 51858 | assert( pDbFd ); |
| 51859 | |
| 51860 | /* In the amalgamation, the os_unix.c and os_win.c source files come before |
| 51861 | ** this source file. Verify that the #defines of the locking byte offsets |
| 51862 | ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value. |
| 51863 | */ |
| 51864 | #ifdef WIN_SHM_BASE |
| 51865 | assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET ); |
| 51866 | #endif |
| 51867 | #ifdef UNIX_SHM_BASE |
| 51868 | assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET ); |
| @@ -52244,10 +52424,11 @@ | |
| 52244 | pWal->hdr.mxFrame = 0; |
| 52245 | sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0])); |
| 52246 | memcpy(&pWal->hdr.aSalt[1], &salt1, 4); |
| 52247 | walIndexWriteHdr(pWal); |
| 52248 | pInfo->nBackfill = 0; |
| 52249 | pInfo->aReadMark[1] = 0; |
| 52250 | for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; |
| 52251 | assert( pInfo->aReadMark[0]==0 ); |
| 52252 | } |
| 52253 | |
| @@ -52352,10 +52533,12 @@ | |
| 52352 | if( pInfo->nBackfill<mxSafeFrame |
| 52353 | && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK |
| 52354 | ){ |
| 52355 | i64 nSize; /* Current size of database file */ |
| 52356 | u32 nBackfill = pInfo->nBackfill; |
| 52357 | |
| 52358 | /* Sync the WAL to disk */ |
| 52359 | if( sync_flags ){ |
| 52360 | rc = sqlite3OsSync(pWal->pWalFd, sync_flags); |
| 52361 | } |
| @@ -52736,10 +52919,11 @@ | |
| 52736 | volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */ |
| 52737 | u32 mxReadMark; /* Largest aReadMark[] value */ |
| 52738 | int mxI; /* Index of largest aReadMark[] value */ |
| 52739 | int i; /* Loop counter */ |
| 52740 | int rc = SQLITE_OK; /* Return code */ |
| 52741 | |
| 52742 | assert( pWal->readLock<0 ); /* Not currently locked */ |
| 52743 | |
| 52744 | /* Take steps to avoid spinning forever if there is a protocol error. |
| 52745 | ** |
| @@ -52799,11 +52983,16 @@ | |
| 52799 | return rc; |
| 52800 | } |
| 52801 | } |
| 52802 | |
| 52803 | pInfo = walCkptInfo(pWal); |
| 52804 | if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){ |
| 52805 | /* The WAL has been completely backfilled (or it is empty). |
| 52806 | ** and can be safely ignored. |
| 52807 | */ |
| 52808 | rc = walLockShared(pWal, WAL_READ_LOCK(0)); |
| 52809 | walShmBarrier(pWal); |
| @@ -52837,89 +53026,92 @@ | |
| 52837 | ** to select one of the aReadMark[] entries that is closest to |
| 52838 | ** but not exceeding pWal->hdr.mxFrame and lock that entry. |
| 52839 | */ |
| 52840 | mxReadMark = 0; |
| 52841 | mxI = 0; |
| 52842 | for(i=1; i<WAL_NREADER; i++){ |
| 52843 | u32 thisMark = pInfo->aReadMark[i]; |
| 52844 | if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){ |
| 52845 | assert( thisMark!=READMARK_NOT_USED ); |
| 52846 | mxReadMark = thisMark; |
| 52847 | mxI = i; |
| 52848 | } |
| 52849 | } |
| 52850 | /* There was once an "if" here. The extra "{" is to preserve indentation. */ |
| 52851 | { |
| 52852 | if( (pWal->readOnly & WAL_SHM_RDONLY)==0 |
| 52853 | && (mxReadMark<pWal->hdr.mxFrame || mxI==0) |
| 52854 | ){ |
| 52855 | for(i=1; i<WAL_NREADER; i++){ |
| 52856 | rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 52857 | if( rc==SQLITE_OK ){ |
| 52858 | mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame; |
| 52859 | mxI = i; |
| 52860 | walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 52861 | break; |
| 52862 | }else if( rc!=SQLITE_BUSY ){ |
| 52863 | return rc; |
| 52864 | } |
| 52865 | } |
| 52866 | } |
| 52867 | if( mxI==0 ){ |
| 52868 | assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); |
| 52869 | return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK; |
| 52870 | } |
| 52871 | |
| 52872 | rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); |
| 52873 | if( rc ){ |
| 52874 | return rc==SQLITE_BUSY ? WAL_RETRY : rc; |
| 52875 | } |
| 52876 | /* Now that the read-lock has been obtained, check that neither the |
| 52877 | ** value in the aReadMark[] array or the contents of the wal-index |
| 52878 | ** header have changed. |
| 52879 | ** |
| 52880 | ** It is necessary to check that the wal-index header did not change |
| 52881 | ** between the time it was read and when the shared-lock was obtained |
| 52882 | ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility |
| 52883 | ** that the log file may have been wrapped by a writer, or that frames |
| 52884 | ** that occur later in the log than pWal->hdr.mxFrame may have been |
| 52885 | ** copied into the database by a checkpointer. If either of these things |
| 52886 | ** happened, then reading the database with the current value of |
| 52887 | ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry |
| 52888 | ** instead. |
| 52889 | ** |
| 52890 | ** Before checking that the live wal-index header has not changed |
| 52891 | ** since it was read, set Wal.minFrame to the first frame in the wal |
| 52892 | ** file that has not yet been checkpointed. This client will not need |
| 52893 | ** to read any frames earlier than minFrame from the wal file - they |
| 52894 | ** can be safely read directly from the database file. |
| 52895 | ** |
| 52896 | ** Because a ShmBarrier() call is made between taking the copy of |
| 52897 | ** nBackfill and checking that the wal-header in shared-memory still |
| 52898 | ** matches the one cached in pWal->hdr, it is guaranteed that the |
| 52899 | ** checkpointer that set nBackfill was not working with a wal-index |
| 52900 | ** header newer than that cached in pWal->hdr. If it were, that could |
| 52901 | ** cause a problem. The checkpointer could omit to checkpoint |
| 52902 | ** a version of page X that lies before pWal->minFrame (call that version |
| 52903 | ** A) on the basis that there is a newer version (version B) of the same |
| 52904 | ** page later in the wal file. But if version B happens to like past |
| 52905 | ** frame pWal->hdr.mxFrame - then the client would incorrectly assume |
| 52906 | ** that it can read version A from the database file. However, since |
| 52907 | ** we can guarantee that the checkpointer that set nBackfill could not |
| 52908 | ** see any pages past pWal->hdr.mxFrame, this problem does not come up. |
| 52909 | */ |
| 52910 | pWal->minFrame = pInfo->nBackfill+1; |
| 52911 | walShmBarrier(pWal); |
| 52912 | if( pInfo->aReadMark[mxI]!=mxReadMark |
| 52913 | || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) |
| 52914 | ){ |
| 52915 | walUnlockShared(pWal, WAL_READ_LOCK(mxI)); |
| 52916 | return WAL_RETRY; |
| 52917 | }else{ |
| 52918 | assert( mxReadMark<=pWal->hdr.mxFrame ); |
| 52919 | pWal->readLock = (i16)mxI; |
| 52920 | } |
| 52921 | } |
| 52922 | return rc; |
| 52923 | } |
| 52924 | |
| 52925 | /* |
| @@ -52938,17 +53130,84 @@ | |
| 52938 | */ |
| 52939 | SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ |
| 52940 | int rc; /* Return code */ |
| 52941 | int cnt = 0; /* Number of TryBeginRead attempts */ |
| 52942 | |
| 52943 | do{ |
| 52944 | rc = walTryBeginRead(pWal, pChanged, 0, ++cnt); |
| 52945 | }while( rc==WAL_RETRY ); |
| 52946 | testcase( (rc&0xff)==SQLITE_BUSY ); |
| 52947 | testcase( (rc&0xff)==SQLITE_IOERR ); |
| 52948 | testcase( rc==SQLITE_PROTOCOL ); |
| 52949 | testcase( rc==SQLITE_OK ); |
| 52950 | return rc; |
| 52951 | } |
| 52952 | |
| 52953 | /* |
| 52954 | ** Finish with a read transaction. All this does is release the |
| @@ -53754,10 +54013,39 @@ | |
| 53754 | */ |
| 53755 | SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){ |
| 53756 | return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ); |
| 53757 | } |
| 53758 | |
| 53759 | #ifdef SQLITE_ENABLE_ZIPVFS |
| 53760 | /* |
| 53761 | ** If the argument is not NULL, it points to a Wal object that holds a |
| 53762 | ** read-lock. This function returns the database page-size if it is known, |
| 53763 | ** or zero if it is not (or if pWal is NULL). |
| @@ -62312,12 +62600,11 @@ | |
| 62312 | ** if sibling page iOld had the same page number as pNew, and if |
| 62313 | ** pCell really was a part of sibling page iOld (not a divider or |
| 62314 | ** overflow cell), we can skip updating the pointer map entries. */ |
| 62315 | if( iOld>=nNew |
| 62316 | || pNew->pgno!=aPgno[iOld] |
| 62317 | || pCell<aOld |
| 62318 | || pCell>=&aOld[usableSize] |
| 62319 | ){ |
| 62320 | if( !leafCorrection ){ |
| 62321 | ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); |
| 62322 | } |
| 62323 | if( cachedCellSize(&b,i)>pNew->minLocal ){ |
| @@ -67343,21 +67630,21 @@ | |
| 67343 | (i*2+1)*sizeof(p->aLabel[0])); |
| 67344 | } |
| 67345 | if( p->aLabel ){ |
| 67346 | p->aLabel[i] = -1; |
| 67347 | } |
| 67348 | return -1-i; |
| 67349 | } |
| 67350 | |
| 67351 | /* |
| 67352 | ** Resolve label "x" to be the address of the next instruction to |
| 67353 | ** be inserted. The parameter "x" must have been obtained from |
| 67354 | ** a prior call to sqlite3VdbeMakeLabel(). |
| 67355 | */ |
| 67356 | SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){ |
| 67357 | Parse *p = v->pParse; |
| 67358 | int j = -1-x; |
| 67359 | assert( v->magic==VDBE_MAGIC_INIT ); |
| 67360 | assert( j<p->nLabel ); |
| 67361 | assert( j>=0 ); |
| 67362 | if( p->aLabel ){ |
| 67363 | p->aLabel[j] = v->nOp; |
| @@ -67580,12 +67867,12 @@ | |
| 67580 | } |
| 67581 | } |
| 67582 | |
| 67583 | pOp->opflags = sqlite3OpcodeProperty[opcode]; |
| 67584 | if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){ |
| 67585 | assert( -1-pOp->p2<pParse->nLabel ); |
| 67586 | pOp->p2 = aLabel[-1-pOp->p2]; |
| 67587 | } |
| 67588 | } |
| 67589 | sqlite3DbFree(p->db, pParse->aLabel); |
| 67590 | pParse->aLabel = 0; |
| 67591 | pParse->nLabel = 0; |
| @@ -67638,19 +67925,14 @@ | |
| 67638 | return 0; |
| 67639 | } |
| 67640 | addr = p->nOp; |
| 67641 | pOut = &p->aOp[addr]; |
| 67642 | for(i=0; i<nOp; i++, aOp++, pOut++){ |
| 67643 | int p2 = aOp->p2; |
| 67644 | pOut->opcode = aOp->opcode; |
| 67645 | pOut->p1 = aOp->p1; |
| 67646 | if( p2<0 ){ |
| 67647 | assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP ); |
| 67648 | pOut->p2 = addr + ADDR(p2); |
| 67649 | }else{ |
| 67650 | pOut->p2 = p2; |
| 67651 | } |
| 67652 | pOut->p3 = aOp->p3; |
| 67653 | pOut->p4type = P4_NOTUSED; |
| 67654 | pOut->p4.p = 0; |
| 67655 | pOut->p5 = 0; |
| 67656 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| @@ -68858,13 +69140,11 @@ | |
| 68858 | p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort); |
| 68859 | if( pParse->explain && nMem<10 ){ |
| 68860 | nMem = 10; |
| 68861 | } |
| 68862 | memset(zCsr, 0, nFree); |
| 68863 | nFree -= (zCsr - (u8*)0)&7; |
| 68864 | zCsr += (zCsr - (u8*)0)&7; |
| 68865 | assert( EIGHT_BYTE_ALIGNMENT(zCsr) ); |
| 68866 | p->expired = 0; |
| 68867 | |
| 68868 | /* Memory for registers, parameters, cursor, etc, is allocated in two |
| 68869 | ** passes. On the first pass, we try to reuse unused space at the |
| 68870 | ** end of the opcode array. If we are unable to satisfy all memory |
| @@ -70232,11 +70512,11 @@ | |
| 70232 | /* String or blob */ |
| 70233 | if( serial_type>=12 ){ |
| 70234 | assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0) |
| 70235 | == (int)sqlite3VdbeSerialTypeLen(serial_type) ); |
| 70236 | len = pMem->n; |
| 70237 | memcpy(buf, pMem->z, len); |
| 70238 | return len; |
| 70239 | } |
| 70240 | |
| 70241 | /* NULL or constants 0 or 1 */ |
| 70242 | return 0; |
| @@ -86421,10 +86701,11 @@ | |
| 86421 | ** if any. Before returning, *pzBuffer is set to the first byte past the |
| 86422 | ** portion of the buffer copied into by this function. |
| 86423 | */ |
| 86424 | static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){ |
| 86425 | Expr *pNew = 0; /* Value to return */ |
| 86426 | if( p ){ |
| 86427 | const int isReduced = (flags&EXPRDUP_REDUCE); |
| 86428 | u8 *zAlloc; |
| 86429 | u32 staticFlag = 0; |
| 86430 | |
| @@ -86457,11 +86738,13 @@ | |
| 86457 | assert( ExprHasProperty(p, EP_Reduced)==0 ); |
| 86458 | memcpy(zAlloc, p, nNewSize); |
| 86459 | }else{ |
| 86460 | int nSize = exprStructSize(p); |
| 86461 | memcpy(zAlloc, p, nSize); |
| 86462 | memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); |
| 86463 | } |
| 86464 | |
| 86465 | /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ |
| 86466 | pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); |
| 86467 | pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); |
| @@ -86547,10 +86830,11 @@ | |
| 86547 | ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a |
| 86548 | ** truncated version of the usual Expr structure that will be stored as |
| 86549 | ** part of the in-memory representation of the database schema. |
| 86550 | */ |
| 86551 | SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ |
| 86552 | return exprDup(db, p, flags, 0); |
| 86553 | } |
| 86554 | SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ |
| 86555 | ExprList *pNew; |
| 86556 | struct ExprList_item *pItem, *pOldItem; |
| @@ -115734,35 +116018,16 @@ | |
| 115734 | |
| 115735 | /* Generate code to destroy the database record of the trigger. |
| 115736 | */ |
| 115737 | assert( pTable!=0 ); |
| 115738 | if( (v = sqlite3GetVdbe(pParse))!=0 ){ |
| 115739 | int base; |
| 115740 | static const int iLn = VDBE_OFFSET_LINENO(2); |
| 115741 | static const VdbeOpList dropTrigger[] = { |
| 115742 | { OP_Rewind, 0, ADDR(9), 0}, |
| 115743 | { OP_String8, 0, 1, 0}, /* 1 */ |
| 115744 | { OP_Column, 0, 1, 2}, |
| 115745 | { OP_Ne, 2, ADDR(8), 1}, |
| 115746 | { OP_String8, 0, 1, 0}, /* 4: "trigger" */ |
| 115747 | { OP_Column, 0, 0, 2}, |
| 115748 | { OP_Ne, 2, ADDR(8), 1}, |
| 115749 | { OP_Delete, 0, 0, 0}, |
| 115750 | { OP_Next, 0, ADDR(1), 0}, /* 8 */ |
| 115751 | }; |
| 115752 | |
| 115753 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 115754 | sqlite3OpenMasterTable(pParse, iDb); |
| 115755 | base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger, iLn); |
| 115756 | sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT); |
| 115757 | sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC); |
| 115758 | sqlite3ChangeCookie(pParse, iDb); |
| 115759 | sqlite3VdbeAddOp2(v, OP_Close, 0, 0); |
| 115760 | sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0); |
| 115761 | if( pParse->nMem<3 ){ |
| 115762 | pParse->nMem = 3; |
| 115763 | } |
| 115764 | } |
| 115765 | } |
| 115766 | |
| 115767 | /* |
| 115768 | ** Remove a trigger from the hash tables of the sqlite* pointer. |
| @@ -135613,10 +135878,93 @@ | |
| 135613 | } |
| 135614 | #endif |
| 135615 | pBt = sqlite3DbNameToBtree(db, zDbName); |
| 135616 | return pBt ? sqlite3BtreeIsReadonly(pBt) : -1; |
| 135617 | } |
| 135618 | |
| 135619 | /************** End of main.c ************************************************/ |
| 135620 | /************** Begin file notify.c ******************************************/ |
| 135621 | /* |
| 135622 | ** 2009 March 3 |
| @@ -181775,11 +182123,11 @@ | |
| 181775 | sqlite3_context *pCtx, /* Function call context */ |
| 181776 | int nArg, /* Number of args */ |
| 181777 | sqlite3_value **apVal /* Function arguments */ |
| 181778 | ){ |
| 181779 | assert( nArg==0 ); |
| 181780 | sqlite3_result_text(pCtx, "fts5: 2015-12-03 12:01:54 d96de532cc4a192cfebae900701dcee0a7d29273", -1, SQLITE_TRANSIENT); |
| 181781 | } |
| 181782 | |
| 181783 | static int fts5Init(sqlite3 *db){ |
| 181784 | static const sqlite3_module fts5Mod = { |
| 181785 | /* iVersion */ 2, |
| 181786 |
| --- src/sqlite3.c | |
| +++ src/sqlite3.c | |
| @@ -325,11 +325,11 @@ | |
| 325 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 326 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 327 | */ |
| 328 | #define SQLITE_VERSION "3.10.0" |
| 329 | #define SQLITE_VERSION_NUMBER 3010000 |
| 330 | #define SQLITE_SOURCE_ID "2015-12-11 13:51:02 e998513e442ce1206b12dc28bdc996d7b5f9f94d" |
| 331 | |
| 332 | /* |
| 333 | ** CAPI3REF: Run-Time Library Version Numbers |
| 334 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 335 | ** |
| @@ -4617,12 +4617,12 @@ | |
| 4617 | ** |
| 4618 | ** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object |
| 4619 | ** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer |
| 4620 | ** then sqlite3_value_free(V) is a harmless no-op. |
| 4621 | */ |
| 4622 | SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value*); |
| 4623 | SQLITE_API void SQLITE_STDCALL sqlite3_value_free(sqlite3_value*); |
| 4624 | |
| 4625 | /* |
| 4626 | ** CAPI3REF: Obtain Aggregate Function Context |
| 4627 | ** METHOD: sqlite3_context |
| 4628 | ** |
| @@ -8063,37 +8063,131 @@ | |
| 8063 | SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); |
| 8064 | |
| 8065 | /* |
| 8066 | ** CAPI3REF: Flush caches to disk mid-transaction |
| 8067 | ** |
| 8068 | ** ^If a write-transaction is open on [database connection] D when the |
| 8069 | ** [sqlite3_db_cacheflush(D)] interface invoked, any dirty |
| 8070 | ** pages in the pager-cache that are not currently in use are written out |
| 8071 | ** to disk. A dirty page may be in use if a database cursor created by an |
| 8072 | ** active SQL statement is reading from it, or if it is page 1 of a database |
| 8073 | ** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)] |
| 8074 | ** interface flushes caches for all schemas - "main", "temp", and |
| 8075 | ** any [attached] databases. |
| 8076 | ** |
| 8077 | ** ^If this function needs to obtain extra database locks before dirty pages |
| 8078 | ** can be flushed to disk, it does so. ^If those locks cannot be obtained |
| 8079 | ** immediately and there is a busy-handler callback configured, it is invoked |
| 8080 | ** in the usual manner. ^If the required lock still cannot be obtained, then |
| 8081 | ** the database is skipped and an attempt made to flush any dirty pages |
| 8082 | ** belonging to the next (if any) database. ^If any databases are skipped |
| 8083 | ** because locks cannot be obtained, but no other error occurs, this |
| 8084 | ** function returns SQLITE_BUSY. |
| 8085 | ** |
| 8086 | ** ^If any other error occurs while flushing dirty pages to disk (for |
| 8087 | ** example an IO error or out-of-memory condition), then processing is |
| 8088 | ** abandoned and an SQLite [error code] is returned to the caller immediately. |
| 8089 | ** |
| 8090 | ** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK. |
| 8091 | ** |
| 8092 | ** ^This function does not set the database handle error code or message |
| 8093 | ** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions. |
| 8094 | */ |
| 8095 | SQLITE_API int SQLITE_STDCALL sqlite3_db_cacheflush(sqlite3*); |
| 8096 | |
| 8097 | /* |
| 8098 | ** CAPI3REF: Database Snapshot |
| 8099 | ** KEYWORDS: {snapshot} |
| 8100 | ** EXPERIMENTAL |
| 8101 | ** |
| 8102 | ** An instance of the snapshot object records the state of a [WAL mode] |
| 8103 | ** database for some specific point in history. |
| 8104 | ** |
| 8105 | ** In [WAL mode], multiple [database connections] that are open on the |
| 8106 | ** same database file can each be reading a different historical version |
| 8107 | ** of the database file. When a [database connection] begins a read |
| 8108 | ** transaction, that connection sees an unchanging copy of the database |
| 8109 | ** as it existed for the point in time when the transaction first started. |
| 8110 | ** Subsequent changes to the database from other connections are not seen |
| 8111 | ** by the reader until a new read transaction is started. |
| 8112 | ** |
| 8113 | ** The sqlite3_snapshot object records state information about an historical |
| 8114 | ** version of the database file so that it is possible to later open a new read |
| 8115 | ** transaction that sees that historical version of the database rather than |
| 8116 | ** the most recent version. |
| 8117 | ** |
| 8118 | ** The constructor for this object is [sqlite3_snapshot_get()]. The |
| 8119 | ** [sqlite3_snapshot_open()] method causes a fresh read transaction to refer |
| 8120 | ** to an historical snapshot (if possible). The destructor for |
| 8121 | ** sqlite3_snapshot objects is [sqlite3_snapshot_free()]. |
| 8122 | */ |
| 8123 | typedef struct sqlite3_snapshot sqlite3_snapshot; |
| 8124 | |
| 8125 | /* |
| 8126 | ** CAPI3REF: Record A Database Snapshot |
| 8127 | ** EXPERIMENTAL |
| 8128 | ** |
| 8129 | ** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a |
| 8130 | ** new [sqlite3_snapshot] object that records the current state of |
| 8131 | ** schema S in database connection D. ^On success, the |
| 8132 | ** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly |
| 8133 | ** created [sqlite3_snapshot] object into *P and returns SQLITE_OK. |
| 8134 | ** ^If schema S of [database connection] D is not a [WAL mode] database |
| 8135 | ** that is in a read transaction, then [sqlite3_snapshot_get(D,S,P)] |
| 8136 | ** leaves the *P value unchanged and returns an appropriate [error code]. |
| 8137 | ** |
| 8138 | ** The [sqlite3_snapshot] object returned from a successful call to |
| 8139 | ** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()] |
| 8140 | ** to avoid a memory leak. |
| 8141 | ** |
| 8142 | ** The [sqlite3_snapshot_get()] interface is only available when the |
| 8143 | ** SQLITE_ENABLE_SNAPSHOT compile-time option is used. |
| 8144 | */ |
| 8145 | SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_snapshot_get( |
| 8146 | sqlite3 *db, |
| 8147 | const char *zSchema, |
| 8148 | sqlite3_snapshot **ppSnapshot |
| 8149 | ); |
| 8150 | |
| 8151 | /* |
| 8152 | ** CAPI3REF: Start a read transaction on an historical snapshot |
| 8153 | ** EXPERIMENTAL |
| 8154 | ** |
| 8155 | ** ^The [sqlite3_snapshot_open(D,S,P)] interface attempts to move the |
| 8156 | ** read transaction that is currently open on schema S of |
| 8157 | ** [database connection] D so that it refers to historical [snapshot] P. |
| 8158 | ** ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK on success |
| 8159 | ** or an appropriate [error code] if it fails. |
| 8160 | ** |
| 8161 | ** ^In order to succeed, a call to [sqlite3_snapshot_open(D,S,P)] must be |
| 8162 | ** the first operation, apart from other sqlite3_snapshot_open() calls, |
| 8163 | ** following the [BEGIN] that starts a new read transaction. |
| 8164 | ** ^A [snapshot] will fail to open if it has been overwritten by a |
| 8165 | ** [checkpoint]. |
| 8166 | ** |
| 8167 | ** The [sqlite3_snapshot_open()] interface is only available when the |
| 8168 | ** SQLITE_ENABLE_SNAPSHOT compile-time option is used. |
| 8169 | */ |
| 8170 | SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_snapshot_open( |
| 8171 | sqlite3 *db, |
| 8172 | const char *zSchema, |
| 8173 | sqlite3_snapshot *pSnapshot |
| 8174 | ); |
| 8175 | |
| 8176 | /* |
| 8177 | ** CAPI3REF: Destroy a snapshot |
| 8178 | ** EXPERIMENTAL |
| 8179 | ** |
| 8180 | ** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P. |
| 8181 | ** The application must eventually free every [sqlite3_snapshot] object |
| 8182 | ** using this routine to avoid a memory leak. |
| 8183 | ** |
| 8184 | ** The [sqlite3_snapshot_free()] interface is only available when the |
| 8185 | ** SQLITE_ENABLE_SNAPSHOT compile-time option is used. |
| 8186 | */ |
| 8187 | SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_snapshot_free(sqlite3_snapshot*); |
| 8188 | |
| 8189 | /* |
| 8190 | ** Undo the hack that converts floating point types to integer for |
| 8191 | ** builds on processors without floating point support. |
| 8192 | */ |
| 8193 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| @@ -9017,10 +9111,25 @@ | |
| 9111 | #else /* Generates a warning - but it always works */ |
| 9112 | # define SQLITE_INT_TO_PTR(X) ((void*)(X)) |
| 9113 | # define SQLITE_PTR_TO_INT(X) ((int)(X)) |
| 9114 | #endif |
| 9115 | |
| 9116 | /* |
| 9117 | ** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to |
| 9118 | ** something between S (inclusive) and E (exclusive). |
| 9119 | ** |
| 9120 | ** In other words, S is a buffer and E is a pointer to the first byte after |
| 9121 | ** the end of buffer S. This macro returns true if P points to something |
| 9122 | ** contained within the buffer S. |
| 9123 | */ |
| 9124 | #if defined(HAVE_STDINT_H) |
| 9125 | # define SQLITE_WITHIN(P,S,E) \ |
| 9126 | ((uintptr_t)(P)>=(uintptr_t)(S) && (uintptr_t)(P)<(uintptr_t)(E)) |
| 9127 | #else |
| 9128 | # define SQLITE_WITHIN(P,S,E) ((P)>=(S) && (P)<(E)) |
| 9129 | #endif |
| 9130 | |
| 9131 | /* |
| 9132 | ** A macro to hint to the compiler that a function should not be |
| 9133 | ** inlined. |
| 9134 | */ |
| 9135 | #if defined(__GNUC__) |
| @@ -11086,10 +11195,14 @@ | |
| 11195 | SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*); |
| 11196 | SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager); |
| 11197 | SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager); |
| 11198 | SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen); |
| 11199 | SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager); |
| 11200 | # ifdef SQLITE_ENABLE_SNAPSHOT |
| 11201 | SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot); |
| 11202 | SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot); |
| 11203 | # endif |
| 11204 | #endif |
| 11205 | |
| 11206 | #ifdef SQLITE_ENABLE_ZIPVFS |
| 11207 | SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager); |
| 11208 | #endif |
| @@ -13649,10 +13762,11 @@ | |
| 13762 | char *zText; /* The string collected so far */ |
| 13763 | int nChar; /* Length of the string so far */ |
| 13764 | int nAlloc; /* Amount of space allocated in zText */ |
| 13765 | int mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */ |
| 13766 | u8 accError; /* STRACCUM_NOMEM or STRACCUM_TOOBIG */ |
| 13767 | u8 bMalloced; /* zText points to allocated space */ |
| 13768 | }; |
| 13769 | #define STRACCUM_NOMEM 1 |
| 13770 | #define STRACCUM_TOOBIG 2 |
| 13771 | |
| 13772 | /* |
| @@ -21930,11 +22044,11 @@ | |
| 22044 | ** would be much more complicated.) */ |
| 22045 | assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); |
| 22046 | scratchAllocOut--; |
| 22047 | #endif |
| 22048 | |
| 22049 | if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){ |
| 22050 | /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */ |
| 22051 | ScratchFreeslot *pSlot; |
| 22052 | pSlot = (ScratchFreeslot*)p; |
| 22053 | sqlite3_mutex_enter(mem0.mutex); |
| 22054 | pSlot->pNext = mem0.pScratchFree; |
| @@ -21966,11 +22080,11 @@ | |
| 22080 | /* |
| 22081 | ** TRUE if p is a lookaside memory allocation from db |
| 22082 | */ |
| 22083 | #ifndef SQLITE_OMIT_LOOKASIDE |
| 22084 | static int isLookaside(sqlite3 *db, void *p){ |
| 22085 | return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd); |
| 22086 | } |
| 22087 | #else |
| 22088 | #define isLookaside(A,B) 0 |
| 22089 | #endif |
| 22090 | |
| @@ -23108,12 +23222,13 @@ | |
| 23222 | if( p->mxAlloc==0 ){ |
| 23223 | N = p->nAlloc - p->nChar - 1; |
| 23224 | setStrAccumError(p, STRACCUM_TOOBIG); |
| 23225 | return N; |
| 23226 | }else{ |
| 23227 | char *zOld = p->bMalloced ? p->zText : 0; |
| 23228 | i64 szNew = p->nChar; |
| 23229 | assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) ); |
| 23230 | szNew += N + 1; |
| 23231 | if( szNew+p->nChar<=p->mxAlloc ){ |
| 23232 | /* Force exponential buffer size growth as long as it does not overflow, |
| 23233 | ** to avoid having to call this routine too often */ |
| 23234 | szNew += p->nChar; |
| @@ -23130,13 +23245,14 @@ | |
| 23245 | }else{ |
| 23246 | zNew = sqlite3_realloc64(zOld, p->nAlloc); |
| 23247 | } |
| 23248 | if( zNew ){ |
| 23249 | assert( p->zText!=0 || p->nChar==0 ); |
| 23250 | if( !p->bMalloced && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
| 23251 | p->zText = zNew; |
| 23252 | p->nAlloc = sqlite3DbMallocSize(p->db, zNew); |
| 23253 | p->bMalloced = 1; |
| 23254 | }else{ |
| 23255 | sqlite3StrAccumReset(p); |
| 23256 | setStrAccumError(p, STRACCUM_NOMEM); |
| 23257 | return 0; |
| 23258 | } |
| @@ -23150,10 +23266,11 @@ | |
| 23266 | SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){ |
| 23267 | testcase( p->nChar + (i64)N > 0x7fffffff ); |
| 23268 | if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ |
| 23269 | return; |
| 23270 | } |
| 23271 | assert( (p->zText==p->zBase)==(p->bMalloced==0) ); |
| 23272 | while( (N--)>0 ) p->zText[p->nChar++] = c; |
| 23273 | } |
| 23274 | |
| 23275 | /* |
| 23276 | ** The StrAccum "p" is not large enough to accept N new bytes of z[]. |
| @@ -23167,10 +23284,11 @@ | |
| 23284 | N = sqlite3StrAccumEnlarge(p, N); |
| 23285 | if( N>0 ){ |
| 23286 | memcpy(&p->zText[p->nChar], z, N); |
| 23287 | p->nChar += N; |
| 23288 | } |
| 23289 | assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) ); |
| 23290 | } |
| 23291 | |
| 23292 | /* |
| 23293 | ** Append N bytes of text from z to the StrAccum object. Increase the |
| 23294 | ** size of the memory allocation for StrAccum if necessary. |
| @@ -23202,15 +23320,17 @@ | |
| 23320 | ** Return a pointer to the resulting string. Return a NULL |
| 23321 | ** pointer if any kind of error was encountered. |
| 23322 | */ |
| 23323 | SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){ |
| 23324 | if( p->zText ){ |
| 23325 | assert( (p->zText==p->zBase)==(p->bMalloced==0) ); |
| 23326 | p->zText[p->nChar] = 0; |
| 23327 | if( p->mxAlloc>0 && p->bMalloced==0 ){ |
| 23328 | p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); |
| 23329 | if( p->zText ){ |
| 23330 | memcpy(p->zText, p->zBase, p->nChar+1); |
| 23331 | p->bMalloced = 1; |
| 23332 | }else{ |
| 23333 | setStrAccumError(p, STRACCUM_NOMEM); |
| 23334 | } |
| 23335 | } |
| 23336 | } |
| @@ -23219,12 +23339,14 @@ | |
| 23339 | |
| 23340 | /* |
| 23341 | ** Reset an StrAccum string. Reclaim all malloced memory. |
| 23342 | */ |
| 23343 | SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){ |
| 23344 | assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) ); |
| 23345 | if( p->bMalloced ){ |
| 23346 | sqlite3DbFree(p->db, p->zText); |
| 23347 | p->bMalloced = 0; |
| 23348 | } |
| 23349 | p->zText = 0; |
| 23350 | } |
| 23351 | |
| 23352 | /* |
| @@ -23246,10 +23368,11 @@ | |
| 23368 | p->db = db; |
| 23369 | p->nChar = 0; |
| 23370 | p->nAlloc = n; |
| 23371 | p->mxAlloc = mx; |
| 23372 | p->accError = 0; |
| 23373 | p->bMalloced = 0; |
| 23374 | } |
| 23375 | |
| 23376 | /* |
| 23377 | ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 23378 | ** %-conversion extensions. |
| @@ -43273,10 +43396,15 @@ | |
| 43396 | /* Return true if the argument is non-NULL and the WAL module is using |
| 43397 | ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the |
| 43398 | ** WAL module is using shared-memory, return false. |
| 43399 | */ |
| 43400 | SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal); |
| 43401 | |
| 43402 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 43403 | SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot); |
| 43404 | SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot); |
| 43405 | #endif |
| 43406 | |
| 43407 | #ifdef SQLITE_ENABLE_ZIPVFS |
| 43408 | /* If the WAL file is not empty, return the number of bytes of content |
| 43409 | ** stored in each frame (i.e. the db page-size when the WAL was created). |
| 43410 | */ |
| @@ -50566,10 +50694,38 @@ | |
| 50694 | } |
| 50695 | } |
| 50696 | return rc; |
| 50697 | } |
| 50698 | |
| 50699 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 50700 | /* |
| 50701 | ** If this is a WAL database, obtain a snapshot handle for the snapshot |
| 50702 | ** currently open. Otherwise, return an error. |
| 50703 | */ |
| 50704 | SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){ |
| 50705 | int rc = SQLITE_ERROR; |
| 50706 | if( pPager->pWal ){ |
| 50707 | rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot); |
| 50708 | } |
| 50709 | return rc; |
| 50710 | } |
| 50711 | |
| 50712 | /* |
| 50713 | ** If this is a WAL database, store a pointer to pSnapshot. Next time a |
| 50714 | ** read transaction is opened, attempt to read from the snapshot it |
| 50715 | ** identifies. If this is not a WAL database, return an error. |
| 50716 | */ |
| 50717 | SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){ |
| 50718 | int rc = SQLITE_OK; |
| 50719 | if( pPager->pWal ){ |
| 50720 | sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot); |
| 50721 | }else{ |
| 50722 | rc = SQLITE_ERROR; |
| 50723 | } |
| 50724 | return rc; |
| 50725 | } |
| 50726 | #endif /* SQLITE_ENABLE_SNAPSHOT */ |
| 50727 | #endif /* !SQLITE_OMIT_WAL */ |
| 50728 | |
| 50729 | #ifdef SQLITE_ENABLE_ZIPVFS |
| 50730 | /* |
| 50731 | ** A read-lock must be held on the pager when this function is called. If |
| @@ -50861,11 +51017,12 @@ | |
| 51017 | #define WAL_MAX_VERSION 3007000 |
| 51018 | #define WALINDEX_MAX_VERSION 3007000 |
| 51019 | |
| 51020 | /* |
| 51021 | ** Indices of various locking bytes. WAL_NREADER is the number |
| 51022 | ** of available reader locks and should be at least 3. The default |
| 51023 | ** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5. |
| 51024 | */ |
| 51025 | #define WAL_WRITE_LOCK 0 |
| 51026 | #define WAL_ALL_BUT_WRITE 1 |
| 51027 | #define WAL_CKPT_LOCK 1 |
| 51028 | #define WAL_RECOVER_LOCK 2 |
| @@ -50881,11 +51038,14 @@ | |
| 51038 | |
| 51039 | /* |
| 51040 | ** The following object holds a copy of the wal-index header content. |
| 51041 | ** |
| 51042 | ** The actual header in the wal-index consists of two copies of this |
| 51043 | ** object followed by one instance of the WalCkptInfo object. |
| 51044 | ** For all versions of SQLite through 3.10.0 and probably beyond, |
| 51045 | ** the locking bytes (WalCkptInfo.aLock) start at offset 120 and |
| 51046 | ** the total header size is 136 bytes. |
| 51047 | ** |
| 51048 | ** The szPage value can be any power of 2 between 512 and 32768, inclusive. |
| 51049 | ** Or it can be 1 to represent a 65536-byte page. The latter case was |
| 51050 | ** added in 3.7.1 when support for 64K pages was added. |
| 51051 | */ |
| @@ -50913,10 +51073,20 @@ | |
| 51073 | ** database "backfilling".) The nBackfill number is never greater than |
| 51074 | ** WalIndexHdr.mxFrame. nBackfill can only be increased by threads |
| 51075 | ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread). |
| 51076 | ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from |
| 51077 | ** mxFrame back to zero when the WAL is reset. |
| 51078 | ** |
| 51079 | ** nBackfillAttempted is the largest value of nBackfill that a checkpoint |
| 51080 | ** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however |
| 51081 | ** the nBackfillAttempted is set before any backfilling is done and the |
| 51082 | ** nBackfill is only set after all backfilling completes. So if a checkpoint |
| 51083 | ** crashes, nBackfillAttempted might be larger than nBackfill. The |
| 51084 | ** WalIndexHdr.mxFrame must never be less than nBackfillAttempted. |
| 51085 | ** |
| 51086 | ** The aLock[] field is a set of bytes used for locking. These bytes should |
| 51087 | ** never be read or written. |
| 51088 | ** |
| 51089 | ** There is one entry in aReadMark[] for each reader lock. If a reader |
| 51090 | ** holds read-lock K, then the value in aReadMark[K] is no greater than |
| 51091 | ** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff) |
| 51092 | ** for any aReadMark[] means that entry is unused. aReadMark[0] is |
| @@ -50953,22 +51123,24 @@ | |
| 51123 | ** order to read from any aReadMark[] entries. |
| 51124 | */ |
| 51125 | struct WalCkptInfo { |
| 51126 | u32 nBackfill; /* Number of WAL frames backfilled into DB */ |
| 51127 | u32 aReadMark[WAL_NREADER]; /* Reader marks */ |
| 51128 | u8 aLock[SQLITE_SHM_NLOCK]; /* Reserved space for locks */ |
| 51129 | u32 nBackfillAttempted; /* WAL frames perhaps written, or maybe not */ |
| 51130 | u32 notUsed0; /* Available for future enhancements */ |
| 51131 | }; |
| 51132 | #define READMARK_NOT_USED 0xffffffff |
| 51133 | |
| 51134 | |
| 51135 | /* A block of WALINDEX_LOCK_RESERVED bytes beginning at |
| 51136 | ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems |
| 51137 | ** only support mandatory file-locks, we do not read or write data |
| 51138 | ** from the region of the file on which locks are applied. |
| 51139 | */ |
| 51140 | #define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock)) |
| 51141 | #define WALINDEX_HDR_SIZE (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo)) |
| 51142 | |
| 51143 | /* Size of header before each frame in wal */ |
| 51144 | #define WAL_FRAME_HDRSIZE 24 |
| 51145 | |
| 51146 | /* Size of write ahead log header, including checksum. */ |
| @@ -51023,10 +51195,13 @@ | |
| 51195 | const char *zWalName; /* Name of WAL file */ |
| 51196 | u32 nCkpt; /* Checkpoint sequence counter in the wal-header */ |
| 51197 | #ifdef SQLITE_DEBUG |
| 51198 | u8 lockError; /* True if a locking error has occurred */ |
| 51199 | #endif |
| 51200 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 51201 | WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */ |
| 51202 | #endif |
| 51203 | }; |
| 51204 | |
| 51205 | /* |
| 51206 | ** Candidate values for Wal.exclusiveMode. |
| 51207 | */ |
| @@ -51787,10 +51962,11 @@ | |
| 51962 | ** currently holding locks that exclude all other readers, writers and |
| 51963 | ** checkpointers. |
| 51964 | */ |
| 51965 | pInfo = walCkptInfo(pWal); |
| 51966 | pInfo->nBackfill = 0; |
| 51967 | pInfo->nBackfillAttempted = pWal->hdr.mxFrame; |
| 51968 | pInfo->aReadMark[0] = 0; |
| 51969 | for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; |
| 51970 | if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame; |
| 51971 | |
| 51972 | /* If more than one frame was recovered from the log file, report an |
| @@ -51858,11 +52034,15 @@ | |
| 52034 | assert( pDbFd ); |
| 52035 | |
| 52036 | /* In the amalgamation, the os_unix.c and os_win.c source files come before |
| 52037 | ** this source file. Verify that the #defines of the locking byte offsets |
| 52038 | ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value. |
| 52039 | ** For that matter, if the lock offset ever changes from its initial design |
| 52040 | ** value of 120, we need to know that so there is an assert() to check it. |
| 52041 | */ |
| 52042 | assert( 120==WALINDEX_LOCK_OFFSET ); |
| 52043 | assert( 136==WALINDEX_HDR_SIZE ); |
| 52044 | #ifdef WIN_SHM_BASE |
| 52045 | assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET ); |
| 52046 | #endif |
| 52047 | #ifdef UNIX_SHM_BASE |
| 52048 | assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET ); |
| @@ -52244,10 +52424,11 @@ | |
| 52424 | pWal->hdr.mxFrame = 0; |
| 52425 | sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0])); |
| 52426 | memcpy(&pWal->hdr.aSalt[1], &salt1, 4); |
| 52427 | walIndexWriteHdr(pWal); |
| 52428 | pInfo->nBackfill = 0; |
| 52429 | pInfo->nBackfillAttempted = 0; |
| 52430 | pInfo->aReadMark[1] = 0; |
| 52431 | for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; |
| 52432 | assert( pInfo->aReadMark[0]==0 ); |
| 52433 | } |
| 52434 | |
| @@ -52352,10 +52533,12 @@ | |
| 52533 | if( pInfo->nBackfill<mxSafeFrame |
| 52534 | && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK |
| 52535 | ){ |
| 52536 | i64 nSize; /* Current size of database file */ |
| 52537 | u32 nBackfill = pInfo->nBackfill; |
| 52538 | |
| 52539 | pInfo->nBackfillAttempted = mxSafeFrame; |
| 52540 | |
| 52541 | /* Sync the WAL to disk */ |
| 52542 | if( sync_flags ){ |
| 52543 | rc = sqlite3OsSync(pWal->pWalFd, sync_flags); |
| 52544 | } |
| @@ -52736,10 +52919,11 @@ | |
| 52919 | volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */ |
| 52920 | u32 mxReadMark; /* Largest aReadMark[] value */ |
| 52921 | int mxI; /* Index of largest aReadMark[] value */ |
| 52922 | int i; /* Loop counter */ |
| 52923 | int rc = SQLITE_OK; /* Return code */ |
| 52924 | u32 mxFrame; /* Wal frame to lock to */ |
| 52925 | |
| 52926 | assert( pWal->readLock<0 ); /* Not currently locked */ |
| 52927 | |
| 52928 | /* Take steps to avoid spinning forever if there is a protocol error. |
| 52929 | ** |
| @@ -52799,11 +52983,16 @@ | |
| 52983 | return rc; |
| 52984 | } |
| 52985 | } |
| 52986 | |
| 52987 | pInfo = walCkptInfo(pWal); |
| 52988 | if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame |
| 52989 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 52990 | && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0 |
| 52991 | || 0==memcmp(&pWal->hdr, pWal->pSnapshot, sizeof(WalIndexHdr))) |
| 52992 | #endif |
| 52993 | ){ |
| 52994 | /* The WAL has been completely backfilled (or it is empty). |
| 52995 | ** and can be safely ignored. |
| 52996 | */ |
| 52997 | rc = walLockShared(pWal, WAL_READ_LOCK(0)); |
| 52998 | walShmBarrier(pWal); |
| @@ -52837,89 +53026,92 @@ | |
| 53026 | ** to select one of the aReadMark[] entries that is closest to |
| 53027 | ** but not exceeding pWal->hdr.mxFrame and lock that entry. |
| 53028 | */ |
| 53029 | mxReadMark = 0; |
| 53030 | mxI = 0; |
| 53031 | mxFrame = pWal->hdr.mxFrame; |
| 53032 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 53033 | if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){ |
| 53034 | mxFrame = pWal->pSnapshot->mxFrame; |
| 53035 | } |
| 53036 | #endif |
| 53037 | for(i=1; i<WAL_NREADER; i++){ |
| 53038 | u32 thisMark = pInfo->aReadMark[i]; |
| 53039 | if( mxReadMark<=thisMark && thisMark<=mxFrame ){ |
| 53040 | assert( thisMark!=READMARK_NOT_USED ); |
| 53041 | mxReadMark = thisMark; |
| 53042 | mxI = i; |
| 53043 | } |
| 53044 | } |
| 53045 | if( (pWal->readOnly & WAL_SHM_RDONLY)==0 |
| 53046 | && (mxReadMark<mxFrame || mxI==0) |
| 53047 | ){ |
| 53048 | for(i=1; i<WAL_NREADER; i++){ |
| 53049 | rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 53050 | if( rc==SQLITE_OK ){ |
| 53051 | mxReadMark = pInfo->aReadMark[i] = mxFrame; |
| 53052 | mxI = i; |
| 53053 | walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 53054 | break; |
| 53055 | }else if( rc!=SQLITE_BUSY ){ |
| 53056 | return rc; |
| 53057 | } |
| 53058 | } |
| 53059 | } |
| 53060 | if( mxI==0 ){ |
| 53061 | assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); |
| 53062 | return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK; |
| 53063 | } |
| 53064 | |
| 53065 | rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); |
| 53066 | if( rc ){ |
| 53067 | return rc==SQLITE_BUSY ? WAL_RETRY : rc; |
| 53068 | } |
| 53069 | /* Now that the read-lock has been obtained, check that neither the |
| 53070 | ** value in the aReadMark[] array or the contents of the wal-index |
| 53071 | ** header have changed. |
| 53072 | ** |
| 53073 | ** It is necessary to check that the wal-index header did not change |
| 53074 | ** between the time it was read and when the shared-lock was obtained |
| 53075 | ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility |
| 53076 | ** that the log file may have been wrapped by a writer, or that frames |
| 53077 | ** that occur later in the log than pWal->hdr.mxFrame may have been |
| 53078 | ** copied into the database by a checkpointer. If either of these things |
| 53079 | ** happened, then reading the database with the current value of |
| 53080 | ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry |
| 53081 | ** instead. |
| 53082 | ** |
| 53083 | ** Before checking that the live wal-index header has not changed |
| 53084 | ** since it was read, set Wal.minFrame to the first frame in the wal |
| 53085 | ** file that has not yet been checkpointed. This client will not need |
| 53086 | ** to read any frames earlier than minFrame from the wal file - they |
| 53087 | ** can be safely read directly from the database file. |
| 53088 | ** |
| 53089 | ** Because a ShmBarrier() call is made between taking the copy of |
| 53090 | ** nBackfill and checking that the wal-header in shared-memory still |
| 53091 | ** matches the one cached in pWal->hdr, it is guaranteed that the |
| 53092 | ** checkpointer that set nBackfill was not working with a wal-index |
| 53093 | ** header newer than that cached in pWal->hdr. If it were, that could |
| 53094 | ** cause a problem. The checkpointer could omit to checkpoint |
| 53095 | ** a version of page X that lies before pWal->minFrame (call that version |
| 53096 | ** A) on the basis that there is a newer version (version B) of the same |
| 53097 | ** page later in the wal file. But if version B happens to like past |
| 53098 | ** frame pWal->hdr.mxFrame - then the client would incorrectly assume |
| 53099 | ** that it can read version A from the database file. However, since |
| 53100 | ** we can guarantee that the checkpointer that set nBackfill could not |
| 53101 | ** see any pages past pWal->hdr.mxFrame, this problem does not come up. |
| 53102 | */ |
| 53103 | pWal->minFrame = pInfo->nBackfill+1; |
| 53104 | walShmBarrier(pWal); |
| 53105 | if( pInfo->aReadMark[mxI]!=mxReadMark |
| 53106 | || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) |
| 53107 | ){ |
| 53108 | walUnlockShared(pWal, WAL_READ_LOCK(mxI)); |
| 53109 | return WAL_RETRY; |
| 53110 | }else{ |
| 53111 | assert( mxReadMark<=pWal->hdr.mxFrame ); |
| 53112 | pWal->readLock = (i16)mxI; |
| 53113 | } |
| 53114 | return rc; |
| 53115 | } |
| 53116 | |
| 53117 | /* |
| @@ -52938,17 +53130,84 @@ | |
| 53130 | */ |
| 53131 | SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ |
| 53132 | int rc; /* Return code */ |
| 53133 | int cnt = 0; /* Number of TryBeginRead attempts */ |
| 53134 | |
| 53135 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 53136 | int bChanged = 0; |
| 53137 | WalIndexHdr *pSnapshot = pWal->pSnapshot; |
| 53138 | if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ |
| 53139 | bChanged = 1; |
| 53140 | } |
| 53141 | #endif |
| 53142 | |
| 53143 | do{ |
| 53144 | rc = walTryBeginRead(pWal, pChanged, 0, ++cnt); |
| 53145 | }while( rc==WAL_RETRY ); |
| 53146 | testcase( (rc&0xff)==SQLITE_BUSY ); |
| 53147 | testcase( (rc&0xff)==SQLITE_IOERR ); |
| 53148 | testcase( rc==SQLITE_PROTOCOL ); |
| 53149 | testcase( rc==SQLITE_OK ); |
| 53150 | |
| 53151 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 53152 | if( rc==SQLITE_OK ){ |
| 53153 | if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ |
| 53154 | /* At this point the client has a lock on an aReadMark[] slot holding |
| 53155 | ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr |
| 53156 | ** is populated with the wal-index header corresponding to the head |
| 53157 | ** of the wal file. Verify that pSnapshot is still valid before |
| 53158 | ** continuing. Reasons why pSnapshot might no longer be valid: |
| 53159 | ** |
| 53160 | ** (1) The WAL file has been reset since the snapshot was taken. |
| 53161 | ** In this case, the salt will have changed. |
| 53162 | ** |
| 53163 | ** (2) A checkpoint as been attempted that wrote frames past |
| 53164 | ** pSnapshot->mxFrame into the database file. Note that the |
| 53165 | ** checkpoint need not have completed for this to cause problems. |
| 53166 | */ |
| 53167 | volatile WalCkptInfo *pInfo = walCkptInfo(pWal); |
| 53168 | |
| 53169 | assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 ); |
| 53170 | assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame ); |
| 53171 | |
| 53172 | /* It is possible that there is a checkpointer thread running |
| 53173 | ** concurrent with this code. If this is the case, it may be that the |
| 53174 | ** checkpointer has already determined that it will checkpoint |
| 53175 | ** snapshot X, where X is later in the wal file than pSnapshot, but |
| 53176 | ** has not yet set the pInfo->nBackfillAttempted variable to indicate |
| 53177 | ** its intent. To avoid the race condition this leads to, ensure that |
| 53178 | ** there is no checkpointer process by taking a shared CKPT lock |
| 53179 | ** before checking pInfo->nBackfillAttempted. */ |
| 53180 | rc = walLockShared(pWal, WAL_CKPT_LOCK); |
| 53181 | |
| 53182 | if( rc==SQLITE_OK ){ |
| 53183 | /* Check that the wal file has not been wrapped. Assuming that it has |
| 53184 | ** not, also check that no checkpointer has attempted to checkpoint any |
| 53185 | ** frames beyond pSnapshot->mxFrame. If either of these conditions are |
| 53186 | ** true, return SQLITE_BUSY_SNAPSHOT. Otherwise, overwrite pWal->hdr |
| 53187 | ** with *pSnapshot and set *pChanged as appropriate for opening the |
| 53188 | ** snapshot. */ |
| 53189 | if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt)) |
| 53190 | && pSnapshot->mxFrame>=pInfo->nBackfillAttempted |
| 53191 | ){ |
| 53192 | memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr)); |
| 53193 | *pChanged = bChanged; |
| 53194 | }else{ |
| 53195 | rc = SQLITE_BUSY_SNAPSHOT; |
| 53196 | } |
| 53197 | |
| 53198 | /* Release the shared CKPT lock obtained above. */ |
| 53199 | walUnlockShared(pWal, WAL_CKPT_LOCK); |
| 53200 | } |
| 53201 | |
| 53202 | |
| 53203 | if( rc!=SQLITE_OK ){ |
| 53204 | sqlite3WalEndReadTransaction(pWal); |
| 53205 | } |
| 53206 | } |
| 53207 | } |
| 53208 | #endif |
| 53209 | return rc; |
| 53210 | } |
| 53211 | |
| 53212 | /* |
| 53213 | ** Finish with a read transaction. All this does is release the |
| @@ -53754,10 +54013,39 @@ | |
| 54013 | */ |
| 54014 | SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){ |
| 54015 | return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ); |
| 54016 | } |
| 54017 | |
| 54018 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 54019 | /* Create a snapshot object. The content of a snapshot is opaque to |
| 54020 | ** every other subsystem, so the WAL module can put whatever it needs |
| 54021 | ** in the object. |
| 54022 | */ |
| 54023 | SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){ |
| 54024 | int rc = SQLITE_OK; |
| 54025 | WalIndexHdr *pRet; |
| 54026 | |
| 54027 | assert( pWal->readLock>=0 && pWal->writeLock==0 ); |
| 54028 | |
| 54029 | pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr)); |
| 54030 | if( pRet==0 ){ |
| 54031 | rc = SQLITE_NOMEM; |
| 54032 | }else{ |
| 54033 | memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr)); |
| 54034 | *ppSnapshot = (sqlite3_snapshot*)pRet; |
| 54035 | } |
| 54036 | |
| 54037 | return rc; |
| 54038 | } |
| 54039 | |
| 54040 | /* Try to open on pSnapshot when the next read-transaction starts |
| 54041 | */ |
| 54042 | SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){ |
| 54043 | pWal->pSnapshot = (WalIndexHdr*)pSnapshot; |
| 54044 | } |
| 54045 | #endif /* SQLITE_ENABLE_SNAPSHOT */ |
| 54046 | |
| 54047 | #ifdef SQLITE_ENABLE_ZIPVFS |
| 54048 | /* |
| 54049 | ** If the argument is not NULL, it points to a Wal object that holds a |
| 54050 | ** read-lock. This function returns the database page-size if it is known, |
| 54051 | ** or zero if it is not (or if pWal is NULL). |
| @@ -62312,12 +62600,11 @@ | |
| 62600 | ** if sibling page iOld had the same page number as pNew, and if |
| 62601 | ** pCell really was a part of sibling page iOld (not a divider or |
| 62602 | ** overflow cell), we can skip updating the pointer map entries. */ |
| 62603 | if( iOld>=nNew |
| 62604 | || pNew->pgno!=aPgno[iOld] |
| 62605 | || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize]) |
| 62606 | ){ |
| 62607 | if( !leafCorrection ){ |
| 62608 | ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); |
| 62609 | } |
| 62610 | if( cachedCellSize(&b,i)>pNew->minLocal ){ |
| @@ -67343,21 +67630,21 @@ | |
| 67630 | (i*2+1)*sizeof(p->aLabel[0])); |
| 67631 | } |
| 67632 | if( p->aLabel ){ |
| 67633 | p->aLabel[i] = -1; |
| 67634 | } |
| 67635 | return ADDR(i); |
| 67636 | } |
| 67637 | |
| 67638 | /* |
| 67639 | ** Resolve label "x" to be the address of the next instruction to |
| 67640 | ** be inserted. The parameter "x" must have been obtained from |
| 67641 | ** a prior call to sqlite3VdbeMakeLabel(). |
| 67642 | */ |
| 67643 | SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){ |
| 67644 | Parse *p = v->pParse; |
| 67645 | int j = ADDR(x); |
| 67646 | assert( v->magic==VDBE_MAGIC_INIT ); |
| 67647 | assert( j<p->nLabel ); |
| 67648 | assert( j>=0 ); |
| 67649 | if( p->aLabel ){ |
| 67650 | p->aLabel[j] = v->nOp; |
| @@ -67580,12 +67867,12 @@ | |
| 67867 | } |
| 67868 | } |
| 67869 | |
| 67870 | pOp->opflags = sqlite3OpcodeProperty[opcode]; |
| 67871 | if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){ |
| 67872 | assert( ADDR(pOp->p2)<pParse->nLabel ); |
| 67873 | pOp->p2 = aLabel[ADDR(pOp->p2)]; |
| 67874 | } |
| 67875 | } |
| 67876 | sqlite3DbFree(p->db, pParse->aLabel); |
| 67877 | pParse->aLabel = 0; |
| 67878 | pParse->nLabel = 0; |
| @@ -67638,19 +67925,14 @@ | |
| 67925 | return 0; |
| 67926 | } |
| 67927 | addr = p->nOp; |
| 67928 | pOut = &p->aOp[addr]; |
| 67929 | for(i=0; i<nOp; i++, aOp++, pOut++){ |
| 67930 | pOut->opcode = aOp->opcode; |
| 67931 | pOut->p1 = aOp->p1; |
| 67932 | pOut->p2 = aOp->p2; |
| 67933 | assert( aOp->p2>=0 ); |
| 67934 | pOut->p3 = aOp->p3; |
| 67935 | pOut->p4type = P4_NOTUSED; |
| 67936 | pOut->p4.p = 0; |
| 67937 | pOut->p5 = 0; |
| 67938 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| @@ -68858,13 +69140,11 @@ | |
| 69140 | p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort); |
| 69141 | if( pParse->explain && nMem<10 ){ |
| 69142 | nMem = 10; |
| 69143 | } |
| 69144 | memset(zCsr, 0, nFree); |
| 69145 | assert( EIGHT_BYTE_ALIGNMENT(&zCsr[nFree]) ); |
| 69146 | p->expired = 0; |
| 69147 | |
| 69148 | /* Memory for registers, parameters, cursor, etc, is allocated in two |
| 69149 | ** passes. On the first pass, we try to reuse unused space at the |
| 69150 | ** end of the opcode array. If we are unable to satisfy all memory |
| @@ -70232,11 +70512,11 @@ | |
| 70512 | /* String or blob */ |
| 70513 | if( serial_type>=12 ){ |
| 70514 | assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0) |
| 70515 | == (int)sqlite3VdbeSerialTypeLen(serial_type) ); |
| 70516 | len = pMem->n; |
| 70517 | if( len>0 ) memcpy(buf, pMem->z, len); |
| 70518 | return len; |
| 70519 | } |
| 70520 | |
| 70521 | /* NULL or constants 0 or 1 */ |
| 70522 | return 0; |
| @@ -86421,10 +86701,11 @@ | |
| 86701 | ** if any. Before returning, *pzBuffer is set to the first byte past the |
| 86702 | ** portion of the buffer copied into by this function. |
| 86703 | */ |
| 86704 | static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){ |
| 86705 | Expr *pNew = 0; /* Value to return */ |
| 86706 | assert( flags==0 || flags==EXPRDUP_REDUCE ); |
| 86707 | if( p ){ |
| 86708 | const int isReduced = (flags&EXPRDUP_REDUCE); |
| 86709 | u8 *zAlloc; |
| 86710 | u32 staticFlag = 0; |
| 86711 | |
| @@ -86457,11 +86738,13 @@ | |
| 86738 | assert( ExprHasProperty(p, EP_Reduced)==0 ); |
| 86739 | memcpy(zAlloc, p, nNewSize); |
| 86740 | }else{ |
| 86741 | int nSize = exprStructSize(p); |
| 86742 | memcpy(zAlloc, p, nSize); |
| 86743 | if( nSize<EXPR_FULLSIZE ){ |
| 86744 | memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); |
| 86745 | } |
| 86746 | } |
| 86747 | |
| 86748 | /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ |
| 86749 | pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); |
| 86750 | pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); |
| @@ -86547,10 +86830,11 @@ | |
| 86830 | ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a |
| 86831 | ** truncated version of the usual Expr structure that will be stored as |
| 86832 | ** part of the in-memory representation of the database schema. |
| 86833 | */ |
| 86834 | SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ |
| 86835 | assert( flags==0 || flags==EXPRDUP_REDUCE ); |
| 86836 | return exprDup(db, p, flags, 0); |
| 86837 | } |
| 86838 | SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ |
| 86839 | ExprList *pNew; |
| 86840 | struct ExprList_item *pItem, *pOldItem; |
| @@ -115734,35 +116018,16 @@ | |
| 116018 | |
| 116019 | /* Generate code to destroy the database record of the trigger. |
| 116020 | */ |
| 116021 | assert( pTable!=0 ); |
| 116022 | if( (v = sqlite3GetVdbe(pParse))!=0 ){ |
| 116023 | sqlite3NestedParse(pParse, |
| 116024 | "DELETE FROM %Q.%s WHERE name=%Q AND type='trigger'", |
| 116025 | db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pTrigger->zName |
| 116026 | ); |
| 116027 | sqlite3ChangeCookie(pParse, iDb); |
| 116028 | sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0); |
| 116029 | } |
| 116030 | } |
| 116031 | |
| 116032 | /* |
| 116033 | ** Remove a trigger from the hash tables of the sqlite* pointer. |
| @@ -135613,10 +135878,93 @@ | |
| 135878 | } |
| 135879 | #endif |
| 135880 | pBt = sqlite3DbNameToBtree(db, zDbName); |
| 135881 | return pBt ? sqlite3BtreeIsReadonly(pBt) : -1; |
| 135882 | } |
| 135883 | |
| 135884 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 135885 | /* |
| 135886 | ** Obtain a snapshot handle for the snapshot of database zDb currently |
| 135887 | ** being read by handle db. |
| 135888 | */ |
| 135889 | SQLITE_API int SQLITE_STDCALL sqlite3_snapshot_get( |
| 135890 | sqlite3 *db, |
| 135891 | const char *zDb, |
| 135892 | sqlite3_snapshot **ppSnapshot |
| 135893 | ){ |
| 135894 | int rc = SQLITE_ERROR; |
| 135895 | #ifndef SQLITE_OMIT_WAL |
| 135896 | int iDb; |
| 135897 | |
| 135898 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 135899 | if( !sqlite3SafetyCheckOk(db) ){ |
| 135900 | return SQLITE_MISUSE_BKPT; |
| 135901 | } |
| 135902 | #endif |
| 135903 | sqlite3_mutex_enter(db->mutex); |
| 135904 | |
| 135905 | iDb = sqlite3FindDbName(db, zDb); |
| 135906 | if( iDb==0 || iDb>1 ){ |
| 135907 | Btree *pBt = db->aDb[iDb].pBt; |
| 135908 | if( 0==sqlite3BtreeIsInTrans(pBt) ){ |
| 135909 | rc = sqlite3BtreeBeginTrans(pBt, 0); |
| 135910 | if( rc==SQLITE_OK ){ |
| 135911 | rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot); |
| 135912 | } |
| 135913 | } |
| 135914 | } |
| 135915 | |
| 135916 | sqlite3_mutex_leave(db->mutex); |
| 135917 | #endif /* SQLITE_OMIT_WAL */ |
| 135918 | return rc; |
| 135919 | } |
| 135920 | |
| 135921 | /* |
| 135922 | ** Open a read-transaction on the snapshot idendified by pSnapshot. |
| 135923 | */ |
| 135924 | SQLITE_API int SQLITE_STDCALL sqlite3_snapshot_open( |
| 135925 | sqlite3 *db, |
| 135926 | const char *zDb, |
| 135927 | sqlite3_snapshot *pSnapshot |
| 135928 | ){ |
| 135929 | int rc = SQLITE_ERROR; |
| 135930 | #ifndef SQLITE_OMIT_WAL |
| 135931 | |
| 135932 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 135933 | if( !sqlite3SafetyCheckOk(db) ){ |
| 135934 | return SQLITE_MISUSE_BKPT; |
| 135935 | } |
| 135936 | #endif |
| 135937 | sqlite3_mutex_enter(db->mutex); |
| 135938 | if( db->autoCommit==0 ){ |
| 135939 | int iDb; |
| 135940 | iDb = sqlite3FindDbName(db, zDb); |
| 135941 | if( iDb==0 || iDb>1 ){ |
| 135942 | Btree *pBt = db->aDb[iDb].pBt; |
| 135943 | if( 0==sqlite3BtreeIsInReadTrans(pBt) ){ |
| 135944 | rc = sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), pSnapshot); |
| 135945 | if( rc==SQLITE_OK ){ |
| 135946 | rc = sqlite3BtreeBeginTrans(pBt, 0); |
| 135947 | sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), 0); |
| 135948 | } |
| 135949 | } |
| 135950 | } |
| 135951 | } |
| 135952 | |
| 135953 | sqlite3_mutex_leave(db->mutex); |
| 135954 | #endif /* SQLITE_OMIT_WAL */ |
| 135955 | return rc; |
| 135956 | } |
| 135957 | |
| 135958 | /* |
| 135959 | ** Free a snapshot handle obtained from sqlite3_snapshot_get(). |
| 135960 | */ |
| 135961 | SQLITE_API void SQLITE_STDCALL sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){ |
| 135962 | sqlite3_free(pSnapshot); |
| 135963 | } |
| 135964 | #endif /* SQLITE_ENABLE_SNAPSHOT */ |
| 135965 | |
| 135966 | |
| 135967 | /************** End of main.c ************************************************/ |
| 135968 | /************** Begin file notify.c ******************************************/ |
| 135969 | /* |
| 135970 | ** 2009 March 3 |
| @@ -181775,11 +182123,11 @@ | |
| 182123 | sqlite3_context *pCtx, /* Function call context */ |
| 182124 | int nArg, /* Number of args */ |
| 182125 | sqlite3_value **apVal /* Function arguments */ |
| 182126 | ){ |
| 182127 | assert( nArg==0 ); |
| 182128 | sqlite3_result_text(pCtx, "fts5: 2015-12-10 17:59:50 05bc4f920ce23da48d1da6cd36a956fd6fd7c862", -1, SQLITE_TRANSIENT); |
| 182129 | } |
| 182130 | |
| 182131 | static int fts5Init(sqlite3 *db){ |
| 182132 | static const sqlite3_module fts5Mod = { |
| 182133 | /* iVersion */ 2, |
| 182134 |
+109
-15
| --- src/sqlite3.h | ||
| +++ src/sqlite3.h | ||
| @@ -111,11 +111,11 @@ | ||
| 111 | 111 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 112 | 112 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 113 | 113 | */ |
| 114 | 114 | #define SQLITE_VERSION "3.10.0" |
| 115 | 115 | #define SQLITE_VERSION_NUMBER 3010000 |
| 116 | -#define SQLITE_SOURCE_ID "2015-12-07 18:18:33 e7ae120d04cffafd9bc2b4ecd68571c17e05ed72" | |
| 116 | +#define SQLITE_SOURCE_ID "2015-12-11 13:51:02 e998513e442ce1206b12dc28bdc996d7b5f9f94d" | |
| 117 | 117 | |
| 118 | 118 | /* |
| 119 | 119 | ** CAPI3REF: Run-Time Library Version Numbers |
| 120 | 120 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 121 | 121 | ** |
| @@ -4403,12 +4403,12 @@ | ||
| 4403 | 4403 | ** |
| 4404 | 4404 | ** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object |
| 4405 | 4405 | ** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer |
| 4406 | 4406 | ** then sqlite3_value_free(V) is a harmless no-op. |
| 4407 | 4407 | */ |
| 4408 | -SQLITE_API SQLITE_EXPERIMENTAL sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value*); | |
| 4409 | -SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_value_free(sqlite3_value*); | |
| 4408 | +SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value*); | |
| 4409 | +SQLITE_API void SQLITE_STDCALL sqlite3_value_free(sqlite3_value*); | |
| 4410 | 4410 | |
| 4411 | 4411 | /* |
| 4412 | 4412 | ** CAPI3REF: Obtain Aggregate Function Context |
| 4413 | 4413 | ** METHOD: sqlite3_context |
| 4414 | 4414 | ** |
| @@ -7849,37 +7849,131 @@ | ||
| 7849 | 7849 | SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); |
| 7850 | 7850 | |
| 7851 | 7851 | /* |
| 7852 | 7852 | ** CAPI3REF: Flush caches to disk mid-transaction |
| 7853 | 7853 | ** |
| 7854 | -** If a write-transaction is open when this function is called, any dirty | |
| 7854 | +** ^If a write-transaction is open on [database connection] D when the | |
| 7855 | +** [sqlite3_db_cacheflush(D)] interface invoked, any dirty | |
| 7855 | 7856 | ** pages in the pager-cache that are not currently in use are written out |
| 7856 | 7857 | ** to disk. A dirty page may be in use if a database cursor created by an |
| 7857 | 7858 | ** active SQL statement is reading from it, or if it is page 1 of a database |
| 7858 | -** file (page 1 is always "in use"). Dirty pages are flushed for all | |
| 7859 | -** databases - "main", "temp" and any attached databases. | |
| 7859 | +** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)] | |
| 7860 | +** interface flushes caches for all schemas - "main", "temp", and | |
| 7861 | +** any [attached] databases. | |
| 7860 | 7862 | ** |
| 7861 | -** If this function needs to obtain extra database locks before dirty pages | |
| 7862 | -** can be flushed to disk, it does so. If said locks cannot be obtained | |
| 7863 | +** ^If this function needs to obtain extra database locks before dirty pages | |
| 7864 | +** can be flushed to disk, it does so. ^If those locks cannot be obtained | |
| 7863 | 7865 | ** immediately and there is a busy-handler callback configured, it is invoked |
| 7864 | -** in the usual manner. If the required lock still cannot be obtained, then | |
| 7866 | +** in the usual manner. ^If the required lock still cannot be obtained, then | |
| 7865 | 7867 | ** the database is skipped and an attempt made to flush any dirty pages |
| 7866 | -** belonging to the next (if any) database. If any databases are skipped | |
| 7868 | +** belonging to the next (if any) database. ^If any databases are skipped | |
| 7867 | 7869 | ** because locks cannot be obtained, but no other error occurs, this |
| 7868 | 7870 | ** function returns SQLITE_BUSY. |
| 7869 | 7871 | ** |
| 7870 | -** If any other error occurs while flushing dirty pages to disk (for | |
| 7872 | +** ^If any other error occurs while flushing dirty pages to disk (for | |
| 7871 | 7873 | ** example an IO error or out-of-memory condition), then processing is |
| 7872 | -** abandoned and an SQLite error code returned to the caller immediately. | |
| 7874 | +** abandoned and an SQLite [error code] is returned to the caller immediately. | |
| 7873 | 7875 | ** |
| 7874 | -** Otherwise, if no error occurs, SQLITE_OK is returned. | |
| 7876 | +** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK. | |
| 7875 | 7877 | ** |
| 7876 | -** This function does not set the database handle error code or message | |
| 7877 | -** returned by the sqlite3_errcode() and sqlite3_errmsg() functions. | |
| 7878 | +** ^This function does not set the database handle error code or message | |
| 7879 | +** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions. | |
| 7878 | 7880 | */ |
| 7879 | 7881 | SQLITE_API int SQLITE_STDCALL sqlite3_db_cacheflush(sqlite3*); |
| 7880 | 7882 | |
| 7883 | +/* | |
| 7884 | +** CAPI3REF: Database Snapshot | |
| 7885 | +** KEYWORDS: {snapshot} | |
| 7886 | +** EXPERIMENTAL | |
| 7887 | +** | |
| 7888 | +** An instance of the snapshot object records the state of a [WAL mode] | |
| 7889 | +** database for some specific point in history. | |
| 7890 | +** | |
| 7891 | +** In [WAL mode], multiple [database connections] that are open on the | |
| 7892 | +** same database file can each be reading a different historical version | |
| 7893 | +** of the database file. When a [database connection] begins a read | |
| 7894 | +** transaction, that connection sees an unchanging copy of the database | |
| 7895 | +** as it existed for the point in time when the transaction first started. | |
| 7896 | +** Subsequent changes to the database from other connections are not seen | |
| 7897 | +** by the reader until a new read transaction is started. | |
| 7898 | +** | |
| 7899 | +** The sqlite3_snapshot object records state information about an historical | |
| 7900 | +** version of the database file so that it is possible to later open a new read | |
| 7901 | +** transaction that sees that historical version of the database rather than | |
| 7902 | +** the most recent version. | |
| 7903 | +** | |
| 7904 | +** The constructor for this object is [sqlite3_snapshot_get()]. The | |
| 7905 | +** [sqlite3_snapshot_open()] method causes a fresh read transaction to refer | |
| 7906 | +** to an historical snapshot (if possible). The destructor for | |
| 7907 | +** sqlite3_snapshot objects is [sqlite3_snapshot_free()]. | |
| 7908 | +*/ | |
| 7909 | +typedef struct sqlite3_snapshot sqlite3_snapshot; | |
| 7910 | + | |
| 7911 | +/* | |
| 7912 | +** CAPI3REF: Record A Database Snapshot | |
| 7913 | +** EXPERIMENTAL | |
| 7914 | +** | |
| 7915 | +** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a | |
| 7916 | +** new [sqlite3_snapshot] object that records the current state of | |
| 7917 | +** schema S in database connection D. ^On success, the | |
| 7918 | +** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly | |
| 7919 | +** created [sqlite3_snapshot] object into *P and returns SQLITE_OK. | |
| 7920 | +** ^If schema S of [database connection] D is not a [WAL mode] database | |
| 7921 | +** that is in a read transaction, then [sqlite3_snapshot_get(D,S,P)] | |
| 7922 | +** leaves the *P value unchanged and returns an appropriate [error code]. | |
| 7923 | +** | |
| 7924 | +** The [sqlite3_snapshot] object returned from a successful call to | |
| 7925 | +** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()] | |
| 7926 | +** to avoid a memory leak. | |
| 7927 | +** | |
| 7928 | +** The [sqlite3_snapshot_get()] interface is only available when the | |
| 7929 | +** SQLITE_ENABLE_SNAPSHOT compile-time option is used. | |
| 7930 | +*/ | |
| 7931 | +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_snapshot_get( | |
| 7932 | + sqlite3 *db, | |
| 7933 | + const char *zSchema, | |
| 7934 | + sqlite3_snapshot **ppSnapshot | |
| 7935 | +); | |
| 7936 | + | |
| 7937 | +/* | |
| 7938 | +** CAPI3REF: Start a read transaction on an historical snapshot | |
| 7939 | +** EXPERIMENTAL | |
| 7940 | +** | |
| 7941 | +** ^The [sqlite3_snapshot_open(D,S,P)] interface attempts to move the | |
| 7942 | +** read transaction that is currently open on schema S of | |
| 7943 | +** [database connection] D so that it refers to historical [snapshot] P. | |
| 7944 | +** ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK on success | |
| 7945 | +** or an appropriate [error code] if it fails. | |
| 7946 | +** | |
| 7947 | +** ^In order to succeed, a call to [sqlite3_snapshot_open(D,S,P)] must be | |
| 7948 | +** the first operation, apart from other sqlite3_snapshot_open() calls, | |
| 7949 | +** following the [BEGIN] that starts a new read transaction. | |
| 7950 | +** ^A [snapshot] will fail to open if it has been overwritten by a | |
| 7951 | +** [checkpoint]. | |
| 7952 | +** | |
| 7953 | +** The [sqlite3_snapshot_open()] interface is only available when the | |
| 7954 | +** SQLITE_ENABLE_SNAPSHOT compile-time option is used. | |
| 7955 | +*/ | |
| 7956 | +SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_snapshot_open( | |
| 7957 | + sqlite3 *db, | |
| 7958 | + const char *zSchema, | |
| 7959 | + sqlite3_snapshot *pSnapshot | |
| 7960 | +); | |
| 7961 | + | |
| 7962 | +/* | |
| 7963 | +** CAPI3REF: Destroy a snapshot | |
| 7964 | +** EXPERIMENTAL | |
| 7965 | +** | |
| 7966 | +** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P. | |
| 7967 | +** The application must eventually free every [sqlite3_snapshot] object | |
| 7968 | +** using this routine to avoid a memory leak. | |
| 7969 | +** | |
| 7970 | +** The [sqlite3_snapshot_free()] interface is only available when the | |
| 7971 | +** SQLITE_ENABLE_SNAPSHOT compile-time option is used. | |
| 7972 | +*/ | |
| 7973 | +SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_snapshot_free(sqlite3_snapshot*); | |
| 7974 | + | |
| 7881 | 7975 | /* |
| 7882 | 7976 | ** Undo the hack that converts floating point types to integer for |
| 7883 | 7977 | ** builds on processors without floating point support. |
| 7884 | 7978 | */ |
| 7885 | 7979 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 7886 | 7980 |
| --- src/sqlite3.h | |
| +++ src/sqlite3.h | |
| @@ -111,11 +111,11 @@ | |
| 111 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 112 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 113 | */ |
| 114 | #define SQLITE_VERSION "3.10.0" |
| 115 | #define SQLITE_VERSION_NUMBER 3010000 |
| 116 | #define SQLITE_SOURCE_ID "2015-12-07 18:18:33 e7ae120d04cffafd9bc2b4ecd68571c17e05ed72" |
| 117 | |
| 118 | /* |
| 119 | ** CAPI3REF: Run-Time Library Version Numbers |
| 120 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 121 | ** |
| @@ -4403,12 +4403,12 @@ | |
| 4403 | ** |
| 4404 | ** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object |
| 4405 | ** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer |
| 4406 | ** then sqlite3_value_free(V) is a harmless no-op. |
| 4407 | */ |
| 4408 | SQLITE_API SQLITE_EXPERIMENTAL sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value*); |
| 4409 | SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_value_free(sqlite3_value*); |
| 4410 | |
| 4411 | /* |
| 4412 | ** CAPI3REF: Obtain Aggregate Function Context |
| 4413 | ** METHOD: sqlite3_context |
| 4414 | ** |
| @@ -7849,37 +7849,131 @@ | |
| 7849 | SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); |
| 7850 | |
| 7851 | /* |
| 7852 | ** CAPI3REF: Flush caches to disk mid-transaction |
| 7853 | ** |
| 7854 | ** If a write-transaction is open when this function is called, any dirty |
| 7855 | ** pages in the pager-cache that are not currently in use are written out |
| 7856 | ** to disk. A dirty page may be in use if a database cursor created by an |
| 7857 | ** active SQL statement is reading from it, or if it is page 1 of a database |
| 7858 | ** file (page 1 is always "in use"). Dirty pages are flushed for all |
| 7859 | ** databases - "main", "temp" and any attached databases. |
| 7860 | ** |
| 7861 | ** If this function needs to obtain extra database locks before dirty pages |
| 7862 | ** can be flushed to disk, it does so. If said locks cannot be obtained |
| 7863 | ** immediately and there is a busy-handler callback configured, it is invoked |
| 7864 | ** in the usual manner. If the required lock still cannot be obtained, then |
| 7865 | ** the database is skipped and an attempt made to flush any dirty pages |
| 7866 | ** belonging to the next (if any) database. If any databases are skipped |
| 7867 | ** because locks cannot be obtained, but no other error occurs, this |
| 7868 | ** function returns SQLITE_BUSY. |
| 7869 | ** |
| 7870 | ** If any other error occurs while flushing dirty pages to disk (for |
| 7871 | ** example an IO error or out-of-memory condition), then processing is |
| 7872 | ** abandoned and an SQLite error code returned to the caller immediately. |
| 7873 | ** |
| 7874 | ** Otherwise, if no error occurs, SQLITE_OK is returned. |
| 7875 | ** |
| 7876 | ** This function does not set the database handle error code or message |
| 7877 | ** returned by the sqlite3_errcode() and sqlite3_errmsg() functions. |
| 7878 | */ |
| 7879 | SQLITE_API int SQLITE_STDCALL sqlite3_db_cacheflush(sqlite3*); |
| 7880 | |
| 7881 | /* |
| 7882 | ** Undo the hack that converts floating point types to integer for |
| 7883 | ** builds on processors without floating point support. |
| 7884 | */ |
| 7885 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 7886 |
| --- src/sqlite3.h | |
| +++ src/sqlite3.h | |
| @@ -111,11 +111,11 @@ | |
| 111 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 112 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 113 | */ |
| 114 | #define SQLITE_VERSION "3.10.0" |
| 115 | #define SQLITE_VERSION_NUMBER 3010000 |
| 116 | #define SQLITE_SOURCE_ID "2015-12-11 13:51:02 e998513e442ce1206b12dc28bdc996d7b5f9f94d" |
| 117 | |
| 118 | /* |
| 119 | ** CAPI3REF: Run-Time Library Version Numbers |
| 120 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 121 | ** |
| @@ -4403,12 +4403,12 @@ | |
| 4403 | ** |
| 4404 | ** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object |
| 4405 | ** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer |
| 4406 | ** then sqlite3_value_free(V) is a harmless no-op. |
| 4407 | */ |
| 4408 | SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_value_dup(const sqlite3_value*); |
| 4409 | SQLITE_API void SQLITE_STDCALL sqlite3_value_free(sqlite3_value*); |
| 4410 | |
| 4411 | /* |
| 4412 | ** CAPI3REF: Obtain Aggregate Function Context |
| 4413 | ** METHOD: sqlite3_context |
| 4414 | ** |
| @@ -7849,37 +7849,131 @@ | |
| 7849 | SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); |
| 7850 | |
| 7851 | /* |
| 7852 | ** CAPI3REF: Flush caches to disk mid-transaction |
| 7853 | ** |
| 7854 | ** ^If a write-transaction is open on [database connection] D when the |
| 7855 | ** [sqlite3_db_cacheflush(D)] interface invoked, any dirty |
| 7856 | ** pages in the pager-cache that are not currently in use are written out |
| 7857 | ** to disk. A dirty page may be in use if a database cursor created by an |
| 7858 | ** active SQL statement is reading from it, or if it is page 1 of a database |
| 7859 | ** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)] |
| 7860 | ** interface flushes caches for all schemas - "main", "temp", and |
| 7861 | ** any [attached] databases. |
| 7862 | ** |
| 7863 | ** ^If this function needs to obtain extra database locks before dirty pages |
| 7864 | ** can be flushed to disk, it does so. ^If those locks cannot be obtained |
| 7865 | ** immediately and there is a busy-handler callback configured, it is invoked |
| 7866 | ** in the usual manner. ^If the required lock still cannot be obtained, then |
| 7867 | ** the database is skipped and an attempt made to flush any dirty pages |
| 7868 | ** belonging to the next (if any) database. ^If any databases are skipped |
| 7869 | ** because locks cannot be obtained, but no other error occurs, this |
| 7870 | ** function returns SQLITE_BUSY. |
| 7871 | ** |
| 7872 | ** ^If any other error occurs while flushing dirty pages to disk (for |
| 7873 | ** example an IO error or out-of-memory condition), then processing is |
| 7874 | ** abandoned and an SQLite [error code] is returned to the caller immediately. |
| 7875 | ** |
| 7876 | ** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK. |
| 7877 | ** |
| 7878 | ** ^This function does not set the database handle error code or message |
| 7879 | ** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions. |
| 7880 | */ |
| 7881 | SQLITE_API int SQLITE_STDCALL sqlite3_db_cacheflush(sqlite3*); |
| 7882 | |
| 7883 | /* |
| 7884 | ** CAPI3REF: Database Snapshot |
| 7885 | ** KEYWORDS: {snapshot} |
| 7886 | ** EXPERIMENTAL |
| 7887 | ** |
| 7888 | ** An instance of the snapshot object records the state of a [WAL mode] |
| 7889 | ** database for some specific point in history. |
| 7890 | ** |
| 7891 | ** In [WAL mode], multiple [database connections] that are open on the |
| 7892 | ** same database file can each be reading a different historical version |
| 7893 | ** of the database file. When a [database connection] begins a read |
| 7894 | ** transaction, that connection sees an unchanging copy of the database |
| 7895 | ** as it existed for the point in time when the transaction first started. |
| 7896 | ** Subsequent changes to the database from other connections are not seen |
| 7897 | ** by the reader until a new read transaction is started. |
| 7898 | ** |
| 7899 | ** The sqlite3_snapshot object records state information about an historical |
| 7900 | ** version of the database file so that it is possible to later open a new read |
| 7901 | ** transaction that sees that historical version of the database rather than |
| 7902 | ** the most recent version. |
| 7903 | ** |
| 7904 | ** The constructor for this object is [sqlite3_snapshot_get()]. The |
| 7905 | ** [sqlite3_snapshot_open()] method causes a fresh read transaction to refer |
| 7906 | ** to an historical snapshot (if possible). The destructor for |
| 7907 | ** sqlite3_snapshot objects is [sqlite3_snapshot_free()]. |
| 7908 | */ |
| 7909 | typedef struct sqlite3_snapshot sqlite3_snapshot; |
| 7910 | |
| 7911 | /* |
| 7912 | ** CAPI3REF: Record A Database Snapshot |
| 7913 | ** EXPERIMENTAL |
| 7914 | ** |
| 7915 | ** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a |
| 7916 | ** new [sqlite3_snapshot] object that records the current state of |
| 7917 | ** schema S in database connection D. ^On success, the |
| 7918 | ** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly |
| 7919 | ** created [sqlite3_snapshot] object into *P and returns SQLITE_OK. |
| 7920 | ** ^If schema S of [database connection] D is not a [WAL mode] database |
| 7921 | ** that is in a read transaction, then [sqlite3_snapshot_get(D,S,P)] |
| 7922 | ** leaves the *P value unchanged and returns an appropriate [error code]. |
| 7923 | ** |
| 7924 | ** The [sqlite3_snapshot] object returned from a successful call to |
| 7925 | ** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()] |
| 7926 | ** to avoid a memory leak. |
| 7927 | ** |
| 7928 | ** The [sqlite3_snapshot_get()] interface is only available when the |
| 7929 | ** SQLITE_ENABLE_SNAPSHOT compile-time option is used. |
| 7930 | */ |
| 7931 | SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_snapshot_get( |
| 7932 | sqlite3 *db, |
| 7933 | const char *zSchema, |
| 7934 | sqlite3_snapshot **ppSnapshot |
| 7935 | ); |
| 7936 | |
| 7937 | /* |
| 7938 | ** CAPI3REF: Start a read transaction on an historical snapshot |
| 7939 | ** EXPERIMENTAL |
| 7940 | ** |
| 7941 | ** ^The [sqlite3_snapshot_open(D,S,P)] interface attempts to move the |
| 7942 | ** read transaction that is currently open on schema S of |
| 7943 | ** [database connection] D so that it refers to historical [snapshot] P. |
| 7944 | ** ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK on success |
| 7945 | ** or an appropriate [error code] if it fails. |
| 7946 | ** |
| 7947 | ** ^In order to succeed, a call to [sqlite3_snapshot_open(D,S,P)] must be |
| 7948 | ** the first operation, apart from other sqlite3_snapshot_open() calls, |
| 7949 | ** following the [BEGIN] that starts a new read transaction. |
| 7950 | ** ^A [snapshot] will fail to open if it has been overwritten by a |
| 7951 | ** [checkpoint]. |
| 7952 | ** |
| 7953 | ** The [sqlite3_snapshot_open()] interface is only available when the |
| 7954 | ** SQLITE_ENABLE_SNAPSHOT compile-time option is used. |
| 7955 | */ |
| 7956 | SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_snapshot_open( |
| 7957 | sqlite3 *db, |
| 7958 | const char *zSchema, |
| 7959 | sqlite3_snapshot *pSnapshot |
| 7960 | ); |
| 7961 | |
| 7962 | /* |
| 7963 | ** CAPI3REF: Destroy a snapshot |
| 7964 | ** EXPERIMENTAL |
| 7965 | ** |
| 7966 | ** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P. |
| 7967 | ** The application must eventually free every [sqlite3_snapshot] object |
| 7968 | ** using this routine to avoid a memory leak. |
| 7969 | ** |
| 7970 | ** The [sqlite3_snapshot_free()] interface is only available when the |
| 7971 | ** SQLITE_ENABLE_SNAPSHOT compile-time option is used. |
| 7972 | */ |
| 7973 | SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_snapshot_free(sqlite3_snapshot*); |
| 7974 | |
| 7975 | /* |
| 7976 | ** Undo the hack that converts floating point types to integer for |
| 7977 | ** builds on processors without floating point support. |
| 7978 | */ |
| 7979 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 7980 |