diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a5ae10a5..c0d1c4f8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,8 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: #### 5.x Releases -- `5.0.x` Releases - [5.0.0](#500) | [5.0.1](#501) + +- `5.0.x` Releases - [5.0.0](#500) | [5.0.1](#501) | [5.0.2](#502) - `5.0.0` Betas - [5.0.0-beta](#500-beta) | [5.0.0-beta.2](#500-beta2) | [5.0.0-beta.3](#500-beta3) | [5.0.0-beta.4](#500-beta4) | [5.0.0-beta.5](#500-beta5) | [5.0.0-beta.6](#500-beta6) | [5.0.0-beta.7](#500-beta7) | [5.0.0-beta.8](#500-beta8) | [5.0.0-beta.9](#500-beta9) | [5.0.0-beta.10](#500-beta10) | [5.0.0-beta.11](#500-beta11) @@ -68,6 +69,14 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: - [0.110.0](#01100), ... +## 5.0.2 + +Released October 6, 2020 • [diff](https://github.com/groue/GRDB.swift/compare/v5.0.1...v5.0.2) + +- **Fixed**: [#844](https://github.com/groue/GRDB.swift/issues/844): Fix a crash in the demo app +- **Fixed**: [#847](https://github.com/groue/GRDB.swift/issues/847): Remove dependency on SQLITE_ENABLE_SNAPSHOT + + ## 5.0.1 Released September 27, 2020 • [diff](https://github.com/groue/GRDB.swift/compare/v5.0.0...v5.0.1) diff --git a/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS.xcodeproj/xcshareddata/xcschemes/GRDBDemoiOS.xcscheme b/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS.xcodeproj/xcshareddata/xcschemes/GRDBDemoiOS.xcscheme index d26693588..89a28d505 100644 --- a/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS.xcodeproj/xcshareddata/xcschemes/GRDBDemoiOS.xcscheme +++ b/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS.xcodeproj/xcshareddata/xcschemes/GRDBDemoiOS.xcscheme @@ -31,7 +31,7 @@ () snapshot.appendSections([0]) snapshot.appendItems(players, toSection: 0) diff --git a/GRDB.swift.podspec b/GRDB.swift.podspec index 8837a706e..69ca1a17c 100644 --- a/GRDB.swift.podspec +++ b/GRDB.swift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'GRDB.swift' - s.version = '5.0.1' + s.version = '5.0.2' s.license = { :type => 'MIT', :file => 'LICENSE' } s.summary = 'A toolkit for SQLite databases, with a focus on application development.' diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index ce6f5e67f..ef83e5e36 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -488,18 +488,17 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib // MARK: - Snapshots - /// Returns a snapshot that must be freed with `grdb_snapshot_free`. + #if SQLITE_ENABLE_SNAPSHOT + /// Returns a snapshot that must be freed with `sqlite3_snapshot_free`. /// /// See https://www.sqlite.org/c3ref/snapshot.html func takeVersionSnapshot() throws -> UnsafeMutablePointer { var snapshot: UnsafeMutablePointer? let code = withUnsafeMutablePointer(to: &snapshot) { - grdb_snapshot_get(sqliteConnection, "main", $0) + sqlite3_snapshot_get(sqliteConnection, "main", $0) } guard code == SQLITE_OK else { - // Don't grab `lastErrorMessage`, because grdb_snapshot_get may be a - // shim that does not call the missing sqlite3_snapshot_get. - throw DatabaseError(resultCode: code) + throw DatabaseError(resultCode: code, message: lastErrorMessage) } if let snapshot = snapshot { return snapshot @@ -511,12 +510,13 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib func wasChanged(since initialSnapshot: UnsafeMutablePointer) throws -> Bool { let secondSnapshot = try takeVersionSnapshot() defer { - grdb_snapshot_free(secondSnapshot) + sqlite3_snapshot_free(secondSnapshot) } - let cmp = grdb_snapshot_cmp(initialSnapshot, secondSnapshot) + let cmp = sqlite3_snapshot_cmp(initialSnapshot, secondSnapshot) assert(cmp <= 0, "Unexpected snapshot ordering") return cmp < 0 } + #endif // MARK: - Authorizer diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 181af079f..bdcd4e787 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -874,6 +874,7 @@ extension DatabasePool: DatabaseReader { // // 3. Install the transaction observer. + #if SQLITE_ENABLE_SNAPSHOT if scheduler.immediateInitialValue() { do { let initialSnapshot = try makeSnapshot() @@ -904,10 +905,36 @@ extension DatabasePool: DatabaseReader { } } } + #else + if scheduler.immediateInitialValue() { + do { + let initialValue = try read(observer.fetchInitialValue) + onChange(initialValue) + addObserver(observer: observer) + } catch { + observer.complete() + observation.events.didFail?(error) + } + } else { + _weakAsyncRead { [weak self] dbResult in + guard let self = self, let dbResult = dbResult else { return } + if observer.isCompleted { return } + + do { + let initialValue = try observer.fetchInitialValue(dbResult.get()) + observer.notifyChange(initialValue) + self.addObserver(observer: observer) + } catch { + observer.notifyErrorAndComplete(error) + } + } + } + #endif return observer } + #if SQLITE_ENABLE_SNAPSHOT // Support for _addConcurrent(observation:) private func add( observer: ValueObserver, @@ -924,20 +951,11 @@ extension DatabasePool: DatabaseReader { // database versions. It prevents database checkpointing, // and keeps versions (`sqlite3_snapshot`) valid // and comparable. - let fetchNeeded: Bool = withExtendedLifetime(initialSnapshot) { - // Version is nil if SQLite is not compiled with - // SQLITE_ENABLE_SNAPSHOT, or if the DatabaseSnaphot - // could not grab its version. In this case, we don't - // care, and just fetch a fresh value. + let fetchNeeded: Bool = try withExtendedLifetime(initialSnapshot) { guard let initialVersion = initialSnapshot.version else { return true } - do { - return try db.wasChanged(since: initialVersion) - } catch { - // ignore: we'll just re-fetch - return true - } + return try db.wasChanged(since: initialVersion) } if fetchNeeded { @@ -956,6 +974,27 @@ extension DatabasePool: DatabaseReader { } } } + #else + // Support for _addConcurrent(observation:) + private func addObserver(observer: ValueObserver) { + _weakAsyncWriteWithoutTransaction { db in + guard let db = db else { return } + if observer.isCompleted { return } + + do { + observer.events.databaseDidChange?() + if let value = try observer.fetchValue(db) { + observer.notifyChange(value) + } + + // Now we can start observation + db.add(transactionObserver: observer, extent: .observerLifetime) + } catch { + observer.notifyErrorAndComplete(error) + } + } + } + #endif } extension DatabasePool { diff --git a/GRDB/Core/DatabaseSnapshot.swift b/GRDB/Core/DatabaseSnapshot.swift index 68d7de378..33dff3b46 100644 --- a/GRDB/Core/DatabaseSnapshot.swift +++ b/GRDB/Core/DatabaseSnapshot.swift @@ -14,8 +14,10 @@ public class DatabaseSnapshot: DatabaseReader { serializedDatabase.configuration } - /// Not nil iff SQLite was compiled with `SQLITE_ENABLE_SNAPSHOT`. + #if SQLITE_ENABLE_SNAPSHOT + // Support for ValueObservation in DatabasePool private(set) var version: UnsafeMutablePointer? + #endif init(path: String, configuration: Configuration = Configuration(), defaultLabel: String, purpose: String) throws { var configuration = DatabasePool.readerConfiguration(configuration) @@ -41,17 +43,22 @@ public class DatabaseSnapshot: DatabaseReader { // Acquire snapshot isolation try db.internalCachedSelectStatement(sql: "SELECT rootpage FROM sqlite_master LIMIT 1").makeCursor().next() - // Support for ValueObservation in DatabasePool + #if SQLITE_ENABLE_SNAPSHOT + // We must expect an error: https://www.sqlite.org/c3ref/snapshot_get.html + // > At least one transaction must be written to it first. version = try? db.takeVersionSnapshot() + #endif } } deinit { // Leave snapshot isolation serializedDatabase.reentrantSync { db in + #if SQLITE_ENABLE_SNAPSHOT if let version = version { - grdb_snapshot_free(version) + sqlite3_snapshot_free(version) } + #endif try? db.commit() } } diff --git a/Makefile b/Makefile index 80ad79670..482062b4a 100644 --- a/Makefile +++ b/Makefile @@ -129,7 +129,7 @@ test_framework_GRDBOSX_maxSwift: -scheme GRDBOSX \ SWIFT_VERSION=$(MAX_SWIFT_VERSION) \ 'OTHER_SWIFT_FLAGS=$(inherited) -D SQLITE_ENABLE_FTS5 -D SQLITE_ENABLE_PREUPDATE_HOOK' \ - 'GCC_PREPROCESSOR_DEFINITIONS=$(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1 GRDB_SQLITE_ENABLE_SNAPSHOT=1' \ + 'GCC_PREPROCESSOR_DEFINITIONS=$(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1' \ $(TEST_ACTIONS) \ $(XCPRETTY) @@ -140,7 +140,7 @@ ifdef MIN_SWIFT_VERSION -scheme GRDBOSX \ SWIFT_VERSION=$(MIN_SWIFT_VERSION) \ 'OTHER_SWIFT_FLAGS=$(inherited) -D SQLITE_ENABLE_FTS5 -D SQLITE_ENABLE_PREUPDATE_HOOK' \ - 'GCC_PREPROCESSOR_DEFINITIONS=$(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1 GRDB_SQLITE_ENABLE_SNAPSHOT=1' \ + 'GCC_PREPROCESSOR_DEFINITIONS=$(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1' \ $(TEST_ACTIONS) \ $(XCPRETTY) endif @@ -164,7 +164,7 @@ test_framework_GRDBiOS_maxTarget_maxSwift: -destination $(MAX_IOS_DESTINATION) \ SWIFT_VERSION=$(MAX_SWIFT_VERSION) \ 'OTHER_SWIFT_FLAGS=$(inherited) -D SQLITE_ENABLE_FTS5 -D SQLITE_ENABLE_PREUPDATE_HOOK' \ - 'GCC_PREPROCESSOR_DEFINITIONS=$(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1 GRDB_SQLITE_ENABLE_SNAPSHOT=1' \ + 'GCC_PREPROCESSOR_DEFINITIONS=$(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1' \ $(TEST_ACTIONS) \ $(XCPRETTY) @@ -176,7 +176,7 @@ ifdef MIN_SWIFT_VERSION -destination $(MAX_IOS_DESTINATION) \ SWIFT_VERSION=$(MIN_SWIFT_VERSION) \ 'OTHER_SWIFT_FLAGS=$(inherited) -D SQLITE_ENABLE_FTS5 -D SQLITE_ENABLE_PREUPDATE_HOOK' \ - 'GCC_PREPROCESSOR_DEFINITIONS=$(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1 GRDB_SQLITE_ENABLE_SNAPSHOT=1' \ + 'GCC_PREPROCESSOR_DEFINITIONS=$(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1' \ $(TEST_ACTIONS) \ $(XCPRETTY) endif @@ -200,7 +200,7 @@ test_framework_GRDBtvOS_maxTarget_maxSwift: -destination $(MAX_TVOS_DESTINATION) \ SWIFT_VERSION=$(MAX_SWIFT_VERSION) \ 'OTHER_SWIFT_FLAGS=$(inherited) -D SQLITE_ENABLE_FTS5 -D SQLITE_ENABLE_PREUPDATE_HOOK' \ - 'GCC_PREPROCESSOR_DEFINITIONS=$(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1 GRDB_SQLITE_ENABLE_SNAPSHOT=1' \ + 'GCC_PREPROCESSOR_DEFINITIONS=$(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1' \ $(TEST_ACTIONS) \ $(XCPRETTY) @@ -212,7 +212,7 @@ ifdef MIN_SWIFT_VERSION -destination $(MAX_TVOS_DESTINATION) \ SWIFT_VERSION=$(MIN_SWIFT_VERSION) \ 'OTHER_SWIFT_FLAGS=$(inherited) -D SQLITE_ENABLE_FTS5 -D SQLITE_ENABLE_PREUPDATE_HOOK' \ - 'GCC_PREPROCESSOR_DEFINITIONS=$(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1 GRDB_SQLITE_ENABLE_SNAPSHOT=1' \ + 'GCC_PREPROCESSOR_DEFINITIONS=$(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1' \ $(TEST_ACTIONS) \ $(XCPRETTY) endif @@ -388,10 +388,11 @@ SQLiteCustom: SQLiteCustom/src/sqlite3.h echo '/* Makefile generated */' > SQLiteCustom/GRDBCustomSQLite-USER.h echo '#define SQLITE_ENABLE_PREUPDATE_HOOK' >> SQLiteCustom/GRDBCustomSQLite-USER.h echo '#define SQLITE_ENABLE_FTS5' >> SQLiteCustom/GRDBCustomSQLite-USER.h + echo '#define SQLITE_ENABLE_SNAPSHOT' >> SQLiteCustom/GRDBCustomSQLite-USER.h echo '// Makefile generated' > SQLiteCustom/GRDBCustomSQLite-USER.xcconfig - echo 'CUSTOM_OTHER_SWIFT_FLAGS = -D SQLITE_ENABLE_PREUPDATE_HOOK -D SQLITE_ENABLE_FTS5' >> SQLiteCustom/GRDBCustomSQLite-USER.xcconfig + echo 'CUSTOM_OTHER_SWIFT_FLAGS = -D SQLITE_ENABLE_PREUPDATE_HOOK -D SQLITE_ENABLE_FTS5 -D SQLITE_ENABLE_SNAPSHOT' >> SQLiteCustom/GRDBCustomSQLite-USER.xcconfig echo '// Makefile generated' > SQLiteCustom/src/SQLiteLib-USER.xcconfig - echo 'CUSTOM_SQLLIBRARY_CFLAGS = -DSQLITE_ENABLE_PREUPDATE_HOOK -DSQLITE_ENABLE_FTS5' >> SQLiteCustom/src/SQLiteLib-USER.xcconfig + echo 'CUSTOM_SQLLIBRARY_CFLAGS = -DSQLITE_ENABLE_PREUPDATE_HOOK -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_SNAPSHOT' >> SQLiteCustom/src/SQLiteLib-USER.xcconfig # Makes sure the SQLiteCustom/src submodule has been downloaded SQLiteCustom/src/sqlite3.h: @@ -408,8 +409,8 @@ ifdef JAZZY --author 'Gwendal Roué' \ --author_url https://github.com/groue \ --github_url https://github.com/groue/GRDB.swift \ - --github-file-prefix https://github.com/groue/GRDB.swift/tree/v5.0.1 \ - --module-version 5.0.1 \ + --github-file-prefix https://github.com/groue/GRDB.swift/tree/v5.0.2 \ + --module-version 5.0.2 \ --module GRDB \ --root-url http://groue.github.io/GRDB.swift/docs/5.0/ \ --output Documentation/Reference \ diff --git a/README.md b/README.md index 1687d0e4d..94c0d9216 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,13 @@ --- -**Latest release**: September 27, 2020 • version 5.0.1 • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 4 to GRDB 5](Documentation/GRDB5MigrationGuide.md) +**Latest release**: October 6, 2020 • version 5.0.2 • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 4 to GRDB 5](Documentation/GRDB5MigrationGuide.md) **Requirements**: iOS 10.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+ • Swift 5.2+ / Xcode 11.4+ | Swift version | GRDB version | | -------------- | ----------------------------------------------------------- | -| **Swift 5.2+** | **v5.0.1** | +| **Swift 5.2+** | **v5.0.2** | | Swift 5.1 | [v4.14.0](https://github.com/groue/GRDB.swift/tree/v4.14.0) | | Swift 5 | [v4.14.0](https://github.com/groue/GRDB.swift/tree/v4.14.0) | | Swift 4.2 | [v4.14.0](https://github.com/groue/GRDB.swift/tree/v4.14.0) | diff --git a/SQLiteCustom/grdb_config.h b/SQLiteCustom/grdb_config.h index fce1c888d..3e53403fe 100644 --- a/SQLiteCustom/grdb_config.h +++ b/SQLiteCustom/grdb_config.h @@ -27,65 +27,4 @@ static inline void enableDoubleQuotedStringLiterals(sqlite3 *db) { static inline void disableDoubleQuotedStringLiterals(sqlite3 *db) { } static inline void enableDoubleQuotedStringLiterals(sqlite3 *db) { } #endif - -/* - Snapshots - ========= - - We have a linker/C-interop difficulty here: - - - Not all SQLite versions ship with the sqlite3_snapshot_get function. - - Not all iOS/macOS versions ship a header that contains - the declaration for sqlite3_snapshot_get(), even when SQLite is - actually compiled with SQLITE_ENABLE_SNAPSHOT. - - This makes it really difficult to deal with system SQLite, custom - SQLite builds, SQLCipher, and SPM. - - To avoid those problems, we add grdb_snapshot_xxx shim functions in the - following header files: - - - SQLiteCustom/grdb_config.h - - Sources/CSQLite/shim.h - - Support/grdb_config.h - */ -#ifdef SQLITE_ENABLE_SNAPSHOT -static inline int grdb_snapshot_get( - sqlite3 *db, - const char *zSchema, - sqlite3_snapshot **ppSnapshot) -{ - return sqlite3_snapshot_get(db, zSchema, ppSnapshot); -} - -static inline void grdb_snapshot_free(sqlite3_snapshot* ppSnapshot) { - sqlite3_snapshot_free(ppSnapshot); -} - -static inline int grdb_snapshot_cmp( - sqlite3_snapshot *p1, - sqlite3_snapshot *p2) -{ - return sqlite3_snapshot_cmp(p1, p2); -} -#else -static inline int grdb_snapshot_get( - sqlite3 *db, - const char *zSchema, - sqlite3_snapshot **ppSnapshot) -{ - return SQLITE_MISUSE; -} - -static inline void grdb_snapshot_free(sqlite3_snapshot* ppSnapshot) { -} - -static inline int grdb_snapshot_cmp( - sqlite3_snapshot *p1, - sqlite3_snapshot *p2) -{ - return 0; -} -#endif /* SQLITE_ENABLE_SNAPSHOT */ - #endif /* grdb_config_h */ diff --git a/Sources/CSQLite/shim.h b/Sources/CSQLite/shim.h index 76fbfa496..be4cd9cf9 100644 --- a/Sources/CSQLite/shim.h +++ b/Sources/CSQLite/shim.h @@ -26,82 +26,3 @@ static inline void enableDoubleQuotedStringLiterals(sqlite3 *db) { static inline void disableDoubleQuotedStringLiterals(sqlite3 *db) { } static inline void enableDoubleQuotedStringLiterals(sqlite3 *db) { } #endif - -/* - Snapshots - ========= - - We have a linker/C-interop difficulty here: - - - Not all SQLite versions ship with the sqlite3_snapshot_get function. - - Not all iOS/macOS versions ship a header that contains - the declaration for sqlite3_snapshot_get(), even when SQLite is - actually compiled with SQLITE_ENABLE_SNAPSHOT. - - This makes it really difficult to deal with system SQLite, custom - SQLite builds, SQLCipher, and SPM. - - To avoid those problems, we add grdb_snapshot_xxx shim functions in the - following header files: - - - SQLiteCustom/grdb_config.h - - Sources/CSQLite/shim.h - - Support/grdb_config.h - */ -#ifdef SQLITE_ENABLE_SNAPSHOT -static inline int grdb_snapshot_get( - sqlite3 *db, - const char *zSchema, - sqlite3_snapshot **ppSnapshot) -{ - return sqlite3_snapshot_get(db, zSchema, ppSnapshot); -} - -static inline void grdb_snapshot_free(sqlite3_snapshot* ppSnapshot) { - sqlite3_snapshot_free(ppSnapshot); -} - -static inline int grdb_snapshot_cmp( - sqlite3_snapshot *p1, - sqlite3_snapshot *p2) -{ - return sqlite3_snapshot_cmp(p1, p2); -} -#else -// Assume snapshot apis *are* defined, but not exposed. -typedef struct sqlite3_snapshot { - unsigned char hidden[48]; -} sqlite3_snapshot; - -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get( - sqlite3 *db, - const char *zSchema, - sqlite3_snapshot **ppSnapshot -); - -SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*); - -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp( - sqlite3_snapshot *p1, - sqlite3_snapshot *p2 -); - -static inline int grdb_snapshot_get( - sqlite3 *db, - const char *zSchema, - sqlite3_snapshot **ppSnapshot) -{ - return sqlite3_snapshot_get(db, zSchema, ppSnapshot); -} - -static inline void grdb_snapshot_free(sqlite3_snapshot* ppSnapshot) { - sqlite3_snapshot_free(ppSnapshot); -} - -static inline int grdb_snapshot_cmp( - sqlite3_snapshot *p1, - sqlite3_snapshot *p2) -{ - return sqlite3_snapshot_cmp(p1, p2); -} -#endif /* SQLITE_ENABLE_SNAPSHOT */ diff --git a/Support/Info.plist b/Support/Info.plist index dd439e299..78ab3233f 100644 --- a/Support/Info.plist +++ b/Support/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 5.0.1 + 5.0.2 CFBundleSignature ???? CFBundleVersion diff --git a/Support/SQLCipher_config.h b/Support/SQLCipher_config.h index 9942c1bae..2a59df68c 100644 --- a/Support/SQLCipher_config.h +++ b/Support/SQLCipher_config.h @@ -29,65 +29,4 @@ static inline void enableDoubleQuotedStringLiterals(sqlite3 *db) { static inline void disableDoubleQuotedStringLiterals(sqlite3 *db) { } static inline void enableDoubleQuotedStringLiterals(sqlite3 *db) { } #endif - -/* - Snapshots - ========= - - We have a linker/C-interop difficulty here: - - - Not all SQLite versions ship with the sqlite3_snapshot_get function. - - Not all iOS/macOS versions ship a header that contains - the declaration for sqlite3_snapshot_get(), even when SQLite is - actually compiled with SQLITE_ENABLE_SNAPSHOT. - - This makes it really difficult to deal with system SQLite, custom - SQLite builds, SQLCipher, and SPM. - - To avoid those problems, we add grdb_snapshot_xxx shim functions in the - following header files: - - - SQLiteCustom/grdb_config.h - - Sources/CSQLite/shim.h - - Support/grdb_config.h - */ -#ifdef SQLITE_ENABLE_SNAPSHOT -static inline int grdb_snapshot_get( - sqlite3 *db, - const char *zSchema, - sqlite3_snapshot **ppSnapshot) -{ - return sqlite3_snapshot_get(db, zSchema, ppSnapshot); -} - -static inline void grdb_snapshot_free(sqlite3_snapshot* ppSnapshot) { - sqlite3_snapshot_free(ppSnapshot); -} - -static inline int grdb_snapshot_cmp( - sqlite3_snapshot *p1, - sqlite3_snapshot *p2) -{ - return sqlite3_snapshot_cmp(p1, p2); -} -#else -static inline int grdb_snapshot_get( - sqlite3 *db, - const char *zSchema, - sqlite3_snapshot **ppSnapshot) -{ - return SQLITE_MISUSE; -} - -static inline void grdb_snapshot_free(sqlite3_snapshot* ppSnapshot) { -} - -static inline int grdb_snapshot_cmp( - sqlite3_snapshot *p1, - sqlite3_snapshot *p2) -{ - return 0; -} -#endif /* SQLITE_ENABLE_SNAPSHOT */ - #endif /* grdb_config_h */ diff --git a/Support/grdb_config.h b/Support/grdb_config.h index ad42371d1..0987e0eeb 100644 --- a/Support/grdb_config.h +++ b/Support/grdb_config.h @@ -50,83 +50,4 @@ SQLITE_API int sqlite3_preupdate_count(sqlite3 *); SQLITE_API int sqlite3_preupdate_depth(sqlite3 *); SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **); #endif /* GRDB_SQLITE_ENABLE_PREUPDATE_HOOK */ - -/* - Snapshots - ========= - - We have a linker/C-interop difficulty here: - - - Not all SQLite versions ship with the sqlite3_snapshot_get function. - - Not all iOS/macOS versions ship a header that contains - the declaration for sqlite3_snapshot_get(), even when SQLite is - actually compiled with SQLITE_ENABLE_SNAPSHOT. - - This makes it really difficult to deal with system SQLite, custom - SQLite builds, SQLCipher, and SPM. - - To avoid those problems, we add grdb_snapshot_xxx shim functions in the - following header files: - - - SQLiteCustom/grdb_config.h - - Sources/CSQLite/shim.h - - Support/grdb_config.h - */ -#ifdef SQLITE_ENABLE_SNAPSHOT -static inline int grdb_snapshot_get( - sqlite3 *db, - const char *zSchema, - sqlite3_snapshot **ppSnapshot) -{ - return sqlite3_snapshot_get(db, zSchema, ppSnapshot); -} - -static inline void grdb_snapshot_free(sqlite3_snapshot* ppSnapshot) { - sqlite3_snapshot_free(ppSnapshot); -} - -static inline int grdb_snapshot_cmp( - sqlite3_snapshot *p1, - sqlite3_snapshot *p2) -{ - return sqlite3_snapshot_cmp(p1, p2); -} -#else -// Assume snapshot apis *are* defined, but not exposed. -typedef struct sqlite3_snapshot { - unsigned char hidden[48]; -} sqlite3_snapshot; - -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get( - sqlite3 *db, - const char *zSchema, - sqlite3_snapshot **ppSnapshot -); - -SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*); - -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp( - sqlite3_snapshot *p1, - sqlite3_snapshot *p2 -); - -static inline int grdb_snapshot_get( - sqlite3 *db, - const char *zSchema, - sqlite3_snapshot **ppSnapshot) -{ - return sqlite3_snapshot_get(db, zSchema, ppSnapshot); -} - -static inline void grdb_snapshot_free(sqlite3_snapshot* ppSnapshot) { - sqlite3_snapshot_free(ppSnapshot); -} - -static inline int grdb_snapshot_cmp( - sqlite3_snapshot *p1, - sqlite3_snapshot *p2) -{ - return sqlite3_snapshot_cmp(p1, p2); -} -#endif /* SQLITE_ENABLE_SNAPSHOT */ #endif /* grdb_config_h */ diff --git a/Tests/GRDBTests/DatabaseSnapshotTests.swift b/Tests/GRDBTests/DatabaseSnapshotTests.swift index ffbfe1699..3f2732212 100644 --- a/Tests/GRDBTests/DatabaseSnapshotTests.swift +++ b/Tests/GRDBTests/DatabaseSnapshotTests.swift @@ -72,7 +72,7 @@ class DatabaseSnapshotTests: GRDBTestCase { func testSnapshotCreatedFromTransactionObserver() throws { // Creating a snapshot from a didCommit callback is an important use // case. But we know SQLite snapshots created with - // sqlite3_snapshot_get() require a transaction. This means that + // sqlite3_snapshot_get() requires a transaction. This means that // creating a snapshot will open a transaction. We must make sure this // transaction does not create any deadlock of reentrancy issue with // transaction observers. diff --git a/Tests/GRDBTests/ValueObservationTests.swift b/Tests/GRDBTests/ValueObservationTests.swift index a531112b3..8c2a1957c 100644 --- a/Tests/GRDBTests/ValueObservationTests.swift +++ b/Tests/GRDBTests/ValueObservationTests.swift @@ -110,23 +110,6 @@ class ValueObservationTests: GRDBTestCase { // MARK: - Snapshot Optimization - private func isSnapshotEnabled(_ dbPool: DatabasePool) throws -> Bool { - // Testing Database.sqliteCompileOptions.contains("ENABLE_SNAPSHOT") - // is... not enough: on iOS 10.3.1, SQLITE_ENABLE_SNAPSHOT is not - // exposed, and yet, sqlite3_snaphost_get() works :shrug: - // - // So let's perform a runtime check: - try dbPool.read { db in - do { - let version = try db.takeVersionSnapshot() - grdb_snapshot_free(version) - return true - } catch { - return false - } - } - } - func testDisallowedSnapshotOptimizationWithAsyncScheduler() throws { let dbPool = try makeDatabasePool() try dbPool.write { db in @@ -231,39 +214,39 @@ class ValueObservationTests: GRDBTestCase { return try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM t")! } - if try isSnapshotEnabled(dbPool) { - let expectation = self.expectation(description: "") - expectation.expectedFulfillmentCount = 2 - var observedCounts: [Int] = [] - let cancellable = observation.start( - in: dbPool, - scheduling: .async(onQueue: .main), - onError: { error in XCTFail("Unexpected error: \(error)") }, - onChange: { count in - observedCounts.append(count) - expectation.fulfill() + #if SQLITE_ENABLE_SNAPSHOT + let expectation = self.expectation(description: "") + expectation.expectedFulfillmentCount = 2 + var observedCounts: [Int] = [] + let cancellable = observation.start( + in: dbPool, + scheduling: .async(onQueue: .main), + onError: { error in XCTFail("Unexpected error: \(error)") }, + onChange: { count in + observedCounts.append(count) + expectation.fulfill() }) - withExtendedLifetime(cancellable) { - waitForExpectations(timeout: 2, handler: nil) - XCTAssertEqual(observedCounts, [0, 1]) - } - } else { - let expectation = self.expectation(description: "") - expectation.expectedFulfillmentCount = 3 - var observedCounts: [Int] = [] - let cancellable = observation.start( - in: dbPool, - scheduling: .async(onQueue: .main), - onError: { error in XCTFail("Unexpected error: \(error)") }, - onChange: { count in - observedCounts.append(count) - expectation.fulfill() - }) - withExtendedLifetime(cancellable) { - waitForExpectations(timeout: 2, handler: nil) - XCTAssertEqual(observedCounts, [0, 0, 1]) - } + withExtendedLifetime(cancellable) { + waitForExpectations(timeout: 2, handler: nil) + XCTAssertEqual(observedCounts, [0, 1]) } + #else + let expectation = self.expectation(description: "") + expectation.expectedFulfillmentCount = 3 + var observedCounts: [Int] = [] + let cancellable = observation.start( + in: dbPool, + scheduling: .async(onQueue: .main), + onError: { error in XCTFail("Unexpected error: \(error)") }, + onChange: { count in + observedCounts.append(count) + expectation.fulfill() + }) + withExtendedLifetime(cancellable) { + waitForExpectations(timeout: 2, handler: nil) + XCTAssertEqual(observedCounts, [0, 0, 1]) + } + #endif } func testAllowedSnapshotOptimizationWithImmediateScheduler() throws { @@ -290,39 +273,39 @@ class ValueObservationTests: GRDBTestCase { return try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM t")! } - if try isSnapshotEnabled(dbPool) { - let expectation = self.expectation(description: "") - expectation.expectedFulfillmentCount = 2 - var observedCounts: [Int] = [] - let cancellable = observation.start( - in: dbPool, - scheduling: .immediate, - onError: { error in XCTFail("Unexpected error: \(error)") }, - onChange: { count in - observedCounts.append(count) - expectation.fulfill() + #if SQLITE_ENABLE_SNAPSHOT + let expectation = self.expectation(description: "") + expectation.expectedFulfillmentCount = 2 + var observedCounts: [Int] = [] + let cancellable = observation.start( + in: dbPool, + scheduling: .immediate, + onError: { error in XCTFail("Unexpected error: \(error)") }, + onChange: { count in + observedCounts.append(count) + expectation.fulfill() }) - withExtendedLifetime(cancellable) { - waitForExpectations(timeout: 2, handler: nil) - XCTAssertEqual(observedCounts, [0, 1]) - } - } else { - let expectation = self.expectation(description: "") - expectation.expectedFulfillmentCount = 3 - var observedCounts: [Int] = [] - let cancellable = observation.start( - in: dbPool, - scheduling: .immediate, - onError: { error in XCTFail("Unexpected error: \(error)") }, - onChange: { count in - observedCounts.append(count) - expectation.fulfill() - }) - withExtendedLifetime(cancellable) { - waitForExpectations(timeout: 2, handler: nil) - XCTAssertEqual(observedCounts, [0, 0, 1]) - } + withExtendedLifetime(cancellable) { + waitForExpectations(timeout: 2, handler: nil) + XCTAssertEqual(observedCounts, [0, 1]) } + #else + let expectation = self.expectation(description: "") + expectation.expectedFulfillmentCount = 3 + var observedCounts: [Int] = [] + let cancellable = observation.start( + in: dbPool, + scheduling: .immediate, + onError: { error in XCTFail("Unexpected error: \(error)") }, + onChange: { count in + observedCounts.append(count) + expectation.fulfill() + }) + withExtendedLifetime(cancellable) { + waitForExpectations(timeout: 2, handler: nil) + XCTAssertEqual(observedCounts, [0, 0, 1]) + } + #endif } // MARK: - Cancellation