From c24bf5d1189c55786df560995df6f5053115bf31 Mon Sep 17 00:00:00 2001 From: Micah Moore Date: Wed, 14 Jan 2026 11:32:48 -0500 Subject: [PATCH 01/11] SQLCipher adjustments to support forks which want to enable SQLCipher using traits/swiftSettings - Adds Database+SQLCipher extension with SQLCipher operations, enabled by #if SQLITE_HAS_CODEC - Adjusts Import Statements to import SQLCipher when SQLCipher trait is enabled (or SQLCipher swiftSetting is set) --- GRDB/Core/Configuration.swift | 4 +- GRDB/Core/Database+SQLCipher.swift | 211 ++++++++++++++++++ GRDB/Core/Database+Schema.swift | 4 +- GRDB/Core/Database+Statements.swift | 4 +- GRDB/Core/Database.swift | 127 +---------- GRDB/Core/DatabaseCollation.swift | 4 +- GRDB/Core/DatabaseError.swift | 4 +- GRDB/Core/DatabaseFunction.swift | 4 +- GRDB/Core/DatabaseSnapshotPool.swift | 8 +- GRDB/Core/DatabaseValue.swift | 4 +- GRDB/Core/Row.swift | 4 +- GRDB/Core/RowDecodingError.swift | 4 +- GRDB/Core/Statement.swift | 4 +- GRDB/Core/StatementAuthorizer.swift | 4 +- GRDB/Core/StatementColumnConvertible.swift | 4 +- GRDB/Core/Support/Foundation/Data.swift | 4 +- .../Foundation/DatabaseDateComponents.swift | 4 +- GRDB/Core/Support/Foundation/Date.swift | 4 +- GRDB/Core/Support/Foundation/Decimal.swift | 4 +- GRDB/Core/Support/Foundation/UUID.swift | 4 +- .../Support/StandardLibrary/Optional.swift | 4 +- .../StandardLibrary/StandardLibrary.swift | 4 +- GRDB/Core/TransactionObserver.swift | 4 +- GRDB/Core/WALSnapshot.swift | 4 +- GRDB/Dump/DumpFormats/DebugDumpFormat.swift | 4 +- GRDB/Dump/DumpFormats/JSONDumpFormat.swift | 4 +- GRDB/Dump/DumpFormats/LineDumpFormat.swift | 4 +- GRDB/Dump/DumpFormats/ListDumpFormat.swift | 4 +- GRDB/Dump/DumpFormats/QuoteDumpFormat.swift | 4 +- GRDB/FTS/FTS5.swift | 4 +- GRDB/FTS/FTS5CustomTokenizer.swift | 4 +- GRDB/FTS/FTS5Tokenizer.swift | 4 +- GRDB/FTS/FTS5WrapperTokenizer.swift | 4 +- GRDB/Record/FetchableRecord+Decodable.swift | 4 +- .../AssociationPrefetchingRowTests.swift | 4 +- Tests/GRDBTests/DataMemoryTests.swift | 4 +- .../DatabaseConfigurationTests.swift | 20 +- .../DatabasePoolReleaseMemoryTests.swift | 4 +- Tests/GRDBTests/DatabasePoolTests.swift | 4 +- Tests/GRDBTests/EncryptionTests.swift | 50 ++++- Tests/GRDBTests/GRDBTestCase.swift | 4 +- ...StatementColumnConvertibleFetchTests.swift | 4 +- Tests/GRDBTests/UpdateStatementTests.swift | 4 +- Tests/GRDBTests/db.SQLCipher3 | Bin 0 -> 2048 bytes 44 files changed, 358 insertions(+), 210 deletions(-) create mode 100644 GRDB/Core/Database+SQLCipher.swift create mode 100644 Tests/GRDBTests/db.SQLCipher3 diff --git a/GRDB/Core/Configuration.swift b/GRDB/Core/Configuration.swift index 933fb9c2b..e2b1ea2c8 100644 --- a/GRDB/Core/Configuration.swift +++ b/GRDB/Core/Configuration.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/Database+SQLCipher.swift b/GRDB/Core/Database+SQLCipher.swift new file mode 100644 index 000000000..fa031d625 --- /dev/null +++ b/GRDB/Core/Database+SQLCipher.swift @@ -0,0 +1,211 @@ +#if SQLITE_HAS_CODEC +import SQLCipher + +extension Database { + + /// Granularitly of SQLCipher log outputs + /// Each log level is more verbose than the last + /// + /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_log_level + public enum CipherLogLevel: String { + case none + case error + case warn + case info + case debug + case trace + } + + /// - Returns: the SQLCipher version + /// + /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_version + public var cipherVersion: String { + get throws { try String.fetchOne(self, sql: "PRAGMA cipher_version")! } + } + + /// - Returns: the SQLCipher fips status: 1 for fips mode, 0 for non-fips mode + /// The FIPS status will not be initialized until the database connection has been keyed + /// + /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_fips_status + public var cipherFipsStatus: String? { + get throws { try String.fetchOne(self, sql: "PRAGMA cipher_fips_status") } + } + + /// - Returns: The compiled crypto provider. + /// The database must be keyed before requesting the name of the crypto provider. + /// + /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_provider + public var cipherProvider: String? { + get throws { try String.fetchOne(self, sql: "PRAGMA cipher_provider") } + } + + /// - Returns: the version number provided from the compiled crypto provider. + /// This value, if known, is available only after the database has been keyed. + /// + /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_provider_version + public var cipherProviderVersion: String? { + get throws { try String.fetchOne(self, sql: "PRAGMA cipher_provider_version") } + } + + /// Sets the passphrase used to crypt and decrypt an SQLCipher database. + /// + /// Call this method from `Configuration.prepareDatabase`, + /// as in the example below: + /// + /// var config = Configuration() + /// config.prepareDatabase { db in + /// try db.usePassphrase("secret") + /// } + public func usePassphrase(_ passphrase: String) throws { + guard var data = passphrase.data(using: .utf8) else { + throw DatabaseError(message: "invalid passphrase") + } + defer { + data.resetBytes(in: 0.. We (Zetetic) have been discouraging the use of sqlite3_rekey in + // > favor of attaching a new database with the desired encryption + // > options and using sqlcipher_export() to migrate the contents and + // > schema of the original db into the new one: + // > https://discuss.zetetic.net/t/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher-and-avoid-file-is-encrypted-or-is-not-a-database-errors/ + let code = passphrase.withUnsafeBytes { + sqlite3_rekey(sqliteConnection, $0.baseAddress, CInt($0.count)) + } + guard code == SQLITE_OK else { + throw DatabaseError(resultCode: code, message: lastErrorMessage) + } + } + + /// When using Commercial or Enterprise SQLCipher packages you must call + /// `PRAGMA cipher_license` with a valid license code prior to executing + /// cryptographic operations on an encrypted database. + /// Failure to provide a license code, or use of an expired trial code, + /// will result in an `SQLITE_AUTH (23)` error code reported from the SQLite API + /// License Codes will activate SQLCipher Commercial or Enterprise packages + /// from Zetetic: https://www.zetetic.net/sqlcipher/buy/ + /// 15-day free trials are available by request: https://www.zetetic.net/sqlcipher/trial/ + /// + /// Call this method from `Configuration.prepareDatabase`, + /// as in the example below: + /// + /// var config = Configuration() + /// config.prepareDatabase { db in + /// try db.applyLicense(license) + /// } + /// + /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_license + /// - Parameter license: base64 SQLCipher license code to activate SQLCipher commercial + public func applyLicense(_ license: String) throws { + try execute(sql: "PRAGMA cipher_license = '\(license)'") + } + + /// Instructs SQLCipher to log internal debugging and operational information + /// to the sepecified log target (device) using `os_log` + /// The supplied logLevel will determine the granularity of the logs output + /// Available logLevel options are: NONE, ERROR, WARN, INFO, DEBUG, TRACE + /// Note that each level is more verbose than the last, + /// and particularly with DEBUG and TRACE the logging system will generate + /// a significant log volume + /// + /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_log + /// - Parameter logLevel: CipherLogLevel The granularity to use for the logging system - defaults to `DEBUG` + public func enableCipherLogging(logLevel: CipherLogLevel = .debug) throws { + try execute(sql: "PRAGMA cipher_log = device") + try execute(sql: "PRAGMA cipher_log_level = \(logLevel.rawValue.uppercased())") + } + + /// Instructs SQLCipher to disable logging internal debugging and operational information + /// + /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_log + public func disableCipherLogging() throws { + try execute(sql: "PRAGMA cipher_log_level = \(CipherLogLevel.none.rawValue.uppercased())") + } + + internal func validateSQLCipher() throws { + // https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688 + // + // > In order to avoid situations where SQLite might be used + // > improperly at runtime, we strongly recommend that + // > applications institute a runtime test to ensure that the + // > application is actually using SQLCipher on the active + // > connection. + if try String.fetchOne(self, sql: "PRAGMA cipher_version") == nil { + throw DatabaseError(resultCode: .SQLITE_MISUSE, message: """ + GRDB is not linked against SQLCipher. \ + Check https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688 + """) + } + } + + internal func dropAllDatabaseObjects() throws { + // SQLCipher does not support the backup API: + // https://discuss.zetetic.net/t/using-the-sqlite-online-backup-api/2631 + // So we'll drop all database objects one after the other. + + // Prevent foreign keys from messing with drop table statements + let foreignKeysEnabled = try Bool.fetchOne(self, sql: "PRAGMA foreign_keys")! + if foreignKeysEnabled { + try execute(sql: "PRAGMA foreign_keys = OFF") + } + + try throwingFirstError( + execute: { + // Remove all database objects, one after the other + try inTransaction { + let sql = "SELECT type, name FROM sqlite_master WHERE name NOT LIKE 'sqlite_%'" + while let row = try Row.fetchOne(self, sql: sql) { + let type: String = row["type"] + let name: String = row["name"] + try execute(sql: "DROP \(type) \(name.quotedDatabaseIdentifier)") + } + return .commit + } + }, + finally: { + // Restore foreign keys if needed + if foreignKeysEnabled { + try execute(sql: "PRAGMA foreign_keys = ON") + } + }) + } +} + +#endif diff --git a/GRDB/Core/Database+Schema.swift b/GRDB/Core/Database+Schema.swift index 593a1fcd8..d1e17ca49 100644 --- a/GRDB/Core/Database+Schema.swift +++ b/GRDB/Core/Database+Schema.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/Database+Statements.swift b/GRDB/Core/Database+Statements.swift index 02229266c..a5ac5ab83 100644 --- a/GRDB/Core/Database+Statements.swift +++ b/GRDB/Core/Database+Statements.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 730825d66..bcce91c53 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -4,8 +4,9 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher +import GRDBSQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif @@ -641,24 +642,6 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib } } - #if SQLITE_HAS_CODEC - private func validateSQLCipher() throws { - // https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688 - // - // > In order to avoid situations where SQLite might be used - // > improperly at runtime, we strongly recommend that - // > applications institute a runtime test to ensure that the - // > application is actually using SQLCipher on the active - // > connection. - if try String.fetchOne(self, sql: "PRAGMA cipher_version") == nil { - throw DatabaseError(resultCode: .SQLITE_MISUSE, message: """ - GRDB is not linked against SQLCipher. \ - Check https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688 - """) - } - } - #endif - private func validateFormat() throws { // Users are surprised when they open a picture as a database and // see no error (https://github.com/groue/GRDB.swift/issues/54). @@ -1852,35 +1835,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib func erase() throws { #if SQLITE_HAS_CODEC - // SQLCipher does not support the backup API: - // https://discuss.zetetic.net/t/using-the-sqlite-online-backup-api/2631 - // So we'll drop all database objects one after the other. - - // Prevent foreign keys from messing with drop table statements - let foreignKeysEnabled = try Bool.fetchOne(self, sql: "PRAGMA foreign_keys")! - if foreignKeysEnabled { - try execute(sql: "PRAGMA foreign_keys = OFF") - } - - try throwingFirstError( - execute: { - // Remove all database objects, one after the other - try inTransaction { - let sql = "SELECT type, name FROM sqlite_master WHERE name NOT LIKE 'sqlite_%'" - while let row = try Row.fetchOne(self, sql: sql) { - let type: String = row["type"] - let name: String = row["name"] - try execute(sql: "DROP \(type) \(name.quotedDatabaseIdentifier)") - } - return .commit - } - }, - finally: { - // Restore foreign keys if needed - if foreignKeysEnabled { - try execute(sql: "PRAGMA foreign_keys = ON") - } - }) + try dropAllDatabaseObjects() #else try DatabaseQueue().backup(to: self) #endif @@ -2003,80 +1958,6 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib @available(*, unavailable) extension Database: Sendable { } -#if SQLITE_HAS_CODEC -extension Database { - - // MARK: - Encryption - - /// Sets the passphrase used to crypt and decrypt an SQLCipher database. - /// - /// Call this method from `Configuration.prepareDatabase`, - /// as in the example below: - /// - /// var config = Configuration() - /// config.prepareDatabase { db in - /// try db.usePassphrase("secret") - /// } - public func usePassphrase(_ passphrase: String) throws { - guard var data = passphrase.data(using: .utf8) else { - throw DatabaseError(message: "invalid passphrase") - } - defer { - data.resetBytes(in: 0.. We (Zetetic) have been discouraging the use of sqlite3_rekey in - // > favor of attaching a new database with the desired encryption - // > options and using sqlcipher_export() to migrate the contents and - // > schema of the original db into the new one: - // > https://discuss.zetetic.net/t/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher-and-avoid-file-is-encrypted-or-is-not-a-database-errors/ - let code = passphrase.withUnsafeBytes { - sqlite3_rekey(sqliteConnection, $0.baseAddress, CInt($0.count)) - } - guard code == SQLITE_OK else { - throw DatabaseError(resultCode: code, message: lastErrorMessage) - } - } -} -#endif - extension Database { /// Returns the count of changes executed by one statement execution. func countChanges(_ count: inout Int, forTable tableName: String, updates: () throws -> T) throws -> T { diff --git a/GRDB/Core/DatabaseCollation.swift b/GRDB/Core/DatabaseCollation.swift index af59112ba..82f583ab3 100644 --- a/GRDB/Core/DatabaseCollation.swift +++ b/GRDB/Core/DatabaseCollation.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/DatabaseError.swift b/GRDB/Core/DatabaseError.swift index 78151bfca..f7c6b2c5d 100644 --- a/GRDB/Core/DatabaseError.swift +++ b/GRDB/Core/DatabaseError.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/DatabaseFunction.swift b/GRDB/Core/DatabaseFunction.swift index 35bc52bd0..845433a0d 100644 --- a/GRDB/Core/DatabaseFunction.swift +++ b/GRDB/Core/DatabaseFunction.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/DatabaseSnapshotPool.swift b/GRDB/Core/DatabaseSnapshotPool.swift index 4604c9c2c..460c799f4 100644 --- a/GRDB/Core/DatabaseSnapshotPool.swift +++ b/GRDB/Core/DatabaseSnapshotPool.swift @@ -5,8 +5,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif @@ -140,7 +140,7 @@ public final class DatabaseSnapshotPool { let walSnapshot = try db.isolated(readOnly: true) { try WALSnapshot(db) } - var holderConfig = Configuration() + var holderConfig = configuration holderConfig.allowsUnsafeTransactions = true snapshotHolder = try DatabaseQueue(path: path, configuration: holderConfig) try snapshotHolder.inDatabase { db in @@ -199,7 +199,7 @@ public final class DatabaseSnapshotPool { var configuration = Self.configure(configuration) // Acquire and hold WAL snapshot - var holderConfig = Configuration() + var holderConfig = configuration holderConfig.allowsUnsafeTransactions = true snapshotHolder = try DatabaseQueue(path: path, configuration: holderConfig) let walSnapshot = try snapshotHolder.inDatabase { db in diff --git a/GRDB/Core/DatabaseValue.swift b/GRDB/Core/DatabaseValue.swift index 2459e3ba7..7a36b3477 100644 --- a/GRDB/Core/DatabaseValue.swift +++ b/GRDB/Core/DatabaseValue.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index c4b639ae7..8f34f2851 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/RowDecodingError.swift b/GRDB/Core/RowDecodingError.swift index 025bb8035..b1db07eaa 100644 --- a/GRDB/Core/RowDecodingError.swift +++ b/GRDB/Core/RowDecodingError.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index e47907d97..fe31743dc 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/StatementAuthorizer.swift b/GRDB/Core/StatementAuthorizer.swift index a8871726f..6dc9c4ae5 100644 --- a/GRDB/Core/StatementAuthorizer.swift +++ b/GRDB/Core/StatementAuthorizer.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/StatementColumnConvertible.swift b/GRDB/Core/StatementColumnConvertible.swift index 490dffb7f..4da021744 100644 --- a/GRDB/Core/StatementColumnConvertible.swift +++ b/GRDB/Core/StatementColumnConvertible.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/Support/Foundation/Data.swift b/GRDB/Core/Support/Foundation/Data.swift index fd7d714a0..ff895b6e7 100644 --- a/GRDB/Core/Support/Foundation/Data.swift +++ b/GRDB/Core/Support/Foundation/Data.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift index d857ecbc2..e7cec1624 100644 --- a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift +++ b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/Support/Foundation/Date.swift b/GRDB/Core/Support/Foundation/Date.swift index dadc00fd6..a1d179e8f 100644 --- a/GRDB/Core/Support/Foundation/Date.swift +++ b/GRDB/Core/Support/Foundation/Date.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/Support/Foundation/Decimal.swift b/GRDB/Core/Support/Foundation/Decimal.swift index 70d84f3ab..0be2a68a9 100644 --- a/GRDB/Core/Support/Foundation/Decimal.swift +++ b/GRDB/Core/Support/Foundation/Decimal.swift @@ -5,8 +5,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/Support/Foundation/UUID.swift b/GRDB/Core/Support/Foundation/UUID.swift index 54b18b2c8..f2780c3cc 100644 --- a/GRDB/Core/Support/Foundation/UUID.swift +++ b/GRDB/Core/Support/Foundation/UUID.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/Support/StandardLibrary/Optional.swift b/GRDB/Core/Support/StandardLibrary/Optional.swift index 253b06e53..b598b119a 100644 --- a/GRDB/Core/Support/StandardLibrary/Optional.swift +++ b/GRDB/Core/Support/StandardLibrary/Optional.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift b/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift index f967fb837..c431aecc5 100644 --- a/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift +++ b/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/TransactionObserver.swift b/GRDB/Core/TransactionObserver.swift index 67212aad6..80289983e 100644 --- a/GRDB/Core/TransactionObserver.swift +++ b/GRDB/Core/TransactionObserver.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Core/WALSnapshot.swift b/GRDB/Core/WALSnapshot.swift index 729a8ac80..7009b4b5e 100644 --- a/GRDB/Core/WALSnapshot.swift +++ b/GRDB/Core/WALSnapshot.swift @@ -5,8 +5,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Dump/DumpFormats/DebugDumpFormat.swift b/GRDB/Dump/DumpFormats/DebugDumpFormat.swift index f957b1936..0d0288ce0 100644 --- a/GRDB/Dump/DumpFormats/DebugDumpFormat.swift +++ b/GRDB/Dump/DumpFormats/DebugDumpFormat.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Dump/DumpFormats/JSONDumpFormat.swift b/GRDB/Dump/DumpFormats/JSONDumpFormat.swift index 42dd67dba..47cb75d36 100644 --- a/GRDB/Dump/DumpFormats/JSONDumpFormat.swift +++ b/GRDB/Dump/DumpFormats/JSONDumpFormat.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Dump/DumpFormats/LineDumpFormat.swift b/GRDB/Dump/DumpFormats/LineDumpFormat.swift index 5111090e5..7416b9676 100644 --- a/GRDB/Dump/DumpFormats/LineDumpFormat.swift +++ b/GRDB/Dump/DumpFormats/LineDumpFormat.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Dump/DumpFormats/ListDumpFormat.swift b/GRDB/Dump/DumpFormats/ListDumpFormat.swift index f5536641a..146eb9785 100644 --- a/GRDB/Dump/DumpFormats/ListDumpFormat.swift +++ b/GRDB/Dump/DumpFormats/ListDumpFormat.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Dump/DumpFormats/QuoteDumpFormat.swift b/GRDB/Dump/DumpFormats/QuoteDumpFormat.swift index c605ae171..fd67b7de1 100644 --- a/GRDB/Dump/DumpFormats/QuoteDumpFormat.swift +++ b/GRDB/Dump/DumpFormats/QuoteDumpFormat.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/FTS/FTS5.swift b/GRDB/FTS/FTS5.swift index 127a43fef..adb108cc7 100644 --- a/GRDB/FTS/FTS5.swift +++ b/GRDB/FTS/FTS5.swift @@ -5,8 +5,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/FTS/FTS5CustomTokenizer.swift b/GRDB/FTS/FTS5CustomTokenizer.swift index a209337a6..63e3912fb 100644 --- a/GRDB/FTS/FTS5CustomTokenizer.swift +++ b/GRDB/FTS/FTS5CustomTokenizer.swift @@ -5,8 +5,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/FTS/FTS5Tokenizer.swift b/GRDB/FTS/FTS5Tokenizer.swift index 35f3ae267..41bd62aa9 100644 --- a/GRDB/FTS/FTS5Tokenizer.swift +++ b/GRDB/FTS/FTS5Tokenizer.swift @@ -5,8 +5,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/FTS/FTS5WrapperTokenizer.swift b/GRDB/FTS/FTS5WrapperTokenizer.swift index bda4ed181..0b355f6b5 100644 --- a/GRDB/FTS/FTS5WrapperTokenizer.swift +++ b/GRDB/FTS/FTS5WrapperTokenizer.swift @@ -5,8 +5,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/GRDB/Record/FetchableRecord+Decodable.swift b/GRDB/Record/FetchableRecord+Decodable.swift index c54ab06bc..069a06d8f 100644 --- a/GRDB/Record/FetchableRecord+Decodable.swift +++ b/GRDB/Record/FetchableRecord+Decodable.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/Tests/GRDBTests/AssociationPrefetchingRowTests.swift b/Tests/GRDBTests/AssociationPrefetchingRowTests.swift index c6fd39bd8..76ec55d07 100644 --- a/Tests/GRDBTests/AssociationPrefetchingRowTests.swift +++ b/Tests/GRDBTests/AssociationPrefetchingRowTests.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/Tests/GRDBTests/DataMemoryTests.swift b/Tests/GRDBTests/DataMemoryTests.swift index 1187ed250..5d7f4f540 100644 --- a/Tests/GRDBTests/DataMemoryTests.swift +++ b/Tests/GRDBTests/DataMemoryTests.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/Tests/GRDBTests/DatabaseConfigurationTests.swift b/Tests/GRDBTests/DatabaseConfigurationTests.swift index f0739ba1c..e89018492 100644 --- a/Tests/GRDBTests/DatabaseConfigurationTests.swift +++ b/Tests/GRDBTests/DatabaseConfigurationTests.swift @@ -12,24 +12,32 @@ class DatabaseConfigurationTests: GRDBTestCase { connectionCountMutex.increment() } + connectionCountMutex.store(0) _ = try DatabaseQueue(configuration: configuration) XCTAssertEqual(connectionCountMutex.load(), 1) + connectionCountMutex.store(0) _ = try makeDatabaseQueue(configuration: configuration) - XCTAssertEqual(connectionCountMutex.load(), 2) + XCTAssertEqual(connectionCountMutex.load(), 1) + connectionCountMutex.store(0) let pool = try makeDatabasePool(configuration: configuration) - XCTAssertEqual(connectionCountMutex.load(), 3) + XCTAssertEqual(connectionCountMutex.load(), 1) // writer + connectionCountMutex.store(0) try pool.read { _ in } - XCTAssertEqual(connectionCountMutex.load(), 4) + XCTAssertEqual(connectionCountMutex.load(), 1) // 1st reader + connectionCountMutex.store(0) try pool.makeSnapshot().read { _ in } - XCTAssertEqual(connectionCountMutex.load(), 5) + XCTAssertEqual(connectionCountMutex.load(), 1) // snapshot #if SQLITE_ENABLE_SNAPSHOT && !SQLITE_DISABLE_SNAPSHOT - try pool.makeSnapshotPool().read { _ in } - XCTAssertEqual(connectionCountMutex.load(), 6) + connectionCountMutex.store(0) + let snapshotPool = try pool.makeSnapshotPool() + XCTAssertEqual(connectionCountMutex.load(), 1) // snapshot + try snapshotPool.read { _ in } + XCTAssertEqual(connectionCountMutex.load(), 2) // 1st reader #endif } diff --git a/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift b/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift index 7302e2aa4..67108860e 100644 --- a/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift +++ b/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/Tests/GRDBTests/DatabasePoolTests.swift b/Tests/GRDBTests/DatabasePoolTests.swift index f8192e58f..86dfa9a75 100644 --- a/Tests/GRDBTests/DatabasePoolTests.swift +++ b/Tests/GRDBTests/DatabasePoolTests.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/Tests/GRDBTests/EncryptionTests.swift b/Tests/GRDBTests/EncryptionTests.swift index a3a1c7453..b101d3ebd 100644 --- a/Tests/GRDBTests/EncryptionTests.swift +++ b/Tests/GRDBTests/EncryptionTests.swift @@ -4,6 +4,12 @@ import GRDB class EncryptionTests: GRDBTestCase { +#if SWIFT_PACKAGE +let testBundle = Bundle.module +#else +let testBundle = Bundle(for: GRDBTestCase.self) +#endif + func testDatabaseQueueWithPassphraseToDatabaseQueueWithPassphrase() throws { do { var config = Configuration() @@ -707,7 +713,6 @@ class EncryptionTests: GRDBTestCase { else { XCTFail("Unknown SQLCipher version"); return } if cipherMajorVersion >= 4 { - let testBundle = Bundle(for: type(of: self)) let path = testBundle.url(forResource: "db", withExtension: "SQLCipher3")!.path var configuration = Configuration() configuration.prepareDatabase { db in @@ -728,5 +733,48 @@ class EncryptionTests: GRDBTestCase { } } } + + func testCipherVersion() throws { + try DatabaseQueue().inDatabase { db in + _ = try db.cipherVersion + } + } + + func testCipherFipsStatus() throws { + var config = Configuration() + config.prepareDatabase { db in + try db.usePassphrase("secret") + } + let dbQueue = try makeDatabaseQueue(configuration: config) + try dbQueue.inDatabase { db in + XCTAssertEqual("0", try db.cipherFipsStatus) + } + } + + func testCipherProvider() throws { + var config = Configuration() + config.prepareDatabase { db in + try db.usePassphrase("secret") + } + let dbQueue = try makeDatabaseQueue(configuration: config) + try dbQueue.inDatabase { db in + XCTAssertEqual("commoncrypto", try db.cipherProvider) + } + } + + func testCipherProviderVersion() throws { + var config = Configuration() + config.prepareDatabase { db in + try db.usePassphrase("secret") + } + let dbQueue = try makeDatabaseQueue(configuration: config) + let cipherVersion = try dbQueue.read { try $0.cipherVersion } + if "4.10.0".compare(cipherVersion, options: .numeric) == .orderedDescending { + throw XCTSkip("cipher_provider_version isn't available until SQLCipher 4.10.0") + } + try dbQueue.inDatabase { db in + XCTAssertNotNil(try db.cipherProviderVersion) + } + } } #endif diff --git a/Tests/GRDBTests/GRDBTestCase.swift b/Tests/GRDBTests/GRDBTestCase.swift index af1df612b..79d8372ac 100644 --- a/Tests/GRDBTests/GRDBTestCase.swift +++ b/Tests/GRDBTests/GRDBTestCase.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/Tests/GRDBTests/StatementColumnConvertibleFetchTests.swift b/Tests/GRDBTests/StatementColumnConvertibleFetchTests.swift index 02b77ec50..af9692cc0 100644 --- a/Tests/GRDBTests/StatementColumnConvertibleFetchTests.swift +++ b/Tests/GRDBTests/StatementColumnConvertibleFetchTests.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/Tests/GRDBTests/UpdateStatementTests.swift b/Tests/GRDBTests/UpdateStatementTests.swift index f11e35117..2f6a90cdc 100644 --- a/Tests/GRDBTests/UpdateStatementTests.swift +++ b/Tests/GRDBTests/UpdateStatementTests.swift @@ -4,8 +4,8 @@ import SQLCipher #elseif GRDBFRAMEWORK // GRDB.xcodeproj or CocoaPods (standard subspec) import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework -// #elseif SomeTrait -// import ... +#elseif SQLCipher +import SQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/Tests/GRDBTests/db.SQLCipher3 b/Tests/GRDBTests/db.SQLCipher3 new file mode 100644 index 0000000000000000000000000000000000000000..6a6fed7db1acfb5e3f453bf23fb3c35faceb9950 GIT binary patch literal 2048 zcmV+b2>i(86Gn(RFGoV1|jfIn;q)MGsS^2P%!DcZS1J#T++vo@~=JFoea|%)g zz$H?3-o@Z}I@VYXA!}M*cqxsC+hXV;DI0>^<#d+fc27VF=XWR#1*m@vC-C#TMH*&~ zg?IZlm)g1p#jD0rq|9}b7k1KkFK%~LqTNWzvYX6E4BO^0k4id*z^x%z;W#sIXkcb8 zW4w&i`JEI_H84tg3{>)bht!P`?wAMP{-5R*<9KYFy~3b>g@U1*?f&EuM7C=vigOHo zjVemIKiG#~Kq92llUqni57VllG7n%)UI2mVv^ygoP!u%n=?@3>csz-%r& zuSHn`TOMT2gCHCx{_KiBkYsx=Ei_NNNm;+wO(&IJP0zMXkHvdu7q3+EN^85mlET7V^E?bNGN=6i&hf>^gg}Pp0t)J~tLszdtMnJCtJ@eM@ahJ!- z%P;Oo7OT3-=;z!cde@J|!w`P81!-}ViL%|u*6OJ%?~fMjH0&MOiVbq@d_ znO$la9Xcol_-07kfXV{-_kToSy0U&hkl8OF-}cb@JSF3#9elI)Vn+ezLg{V5uO6Z` zCVplk?h(Om^kvf@R%ZQ4cW&N~t19i(lFH;XJ5&F8ZId*yM+niG*)F9+rh8yobrB!_ z@}Lug!I|*tu%>cFdyYb&LNAHO^6M?VUJyMl57jIqd#Ttr@)%_{V6_h$0-znL`7Ex>1imNQsavV0}-K~vMaz9+Qz1xaG%;(^dU z1Lnxak@O(%y7S3V$BuSH?p>lVyOY!!HtEqL#nxp~d15F*>;*bjG@YX4)ie|}5y3!e($|M)<(LU>AVqOid|oI5`eh|YF7M>L5_}xh{fjuFu`n!zXPifW)s@B- zHwjA41j%UnXxJ@)=5E4Sgu?|mH>kvpmK6gf-~)no~>JxrLe0`G{5-Qse1VUBV$WRi-6* zT<2wk3Pfptuy5}&2v;rRg}#dN0X8tNsY#0~tUXC^N{XmwN*q$Gh_AJ^#G*xwwCwr( z`p`}t=prz}0`l-{2EKOJqW4Zcz9Jd*iZ;<73G{Cy9^TQNiVSq!qeqvrY@c6HVx+El zwORy^WR)tHI;Ey%GlJKdfZ+$ZsI`DszMA0SdNdkkRd)E1)6O|`%`^HGRg&5zx97XwFCKw z2JgSUDj^yxL_wWM!$_Wz8tcD5FTKt6*SX)0AHIh~{S5}Ba6RpmlHgN&_R8#9t8M9Do z)vmPWVwC@;MMeO0q?ogQ*tKGNB2sG_`~!ZUP&{3EA#Aq(LU&_vo-p z<0FEd{)I+#PQUCW`l>U;fk;b6`H3*G2+{_;)IQ_%g#6Ymli&IP*?Fk~O6QpVc~%Bo ze(do_!q4&Nhi}g_u9_M*sZT=ajQP)xkH4tFDb#jMB0G6brUFxGuVrLBnwf{0QrC`h7c_a^x5;fTjJ`00wi zAnxkup-1-~MG-bDPfqb~lqRz)x+ArVn5;DycZjCdWu47Dy%?(Eb2fxPnM@}PN6Uel z0JR$_KLXB`3$$0)v&I$s<8!-@b4yd=mfNUEFxF_Xbp5Hv_94Ju(@O{&{bj)B2 zq{VRT2xju8LM9H?Je!!cdEP}^!yZ{vc!>vLFMNSgt07EIYB8a^_d;JCTp!%tL8)Dx z?BMJsyIck=81DLSlJXRG_9i?)yP@Ntsk~Xtt`b%v#-%wP!e_0z1Mwk*X-%#M@C2v{ z#tjQ0*uWn}4LQvmU?9`E{RM^fiS>HkDh;_gJ{k?xYz5_ue%C|^kd2Eg>w>~mQA(K2 z@SkuoVJ5vqfEG6y_$qzci z5+Of<=9iiupT)Jmn~XHZ-&=4ieK4)2=N>P-nYs>kxu2YTUDYBp;)ggm$za6SyG;a8 e+*a39!ZRbkg`I|Pg@^rq$>-rPCym=f{@&un|L{Ek literal 0 HcmV?d00001 From 2f94eb307aeeac6dca4a09f24f6d4280502ceb74 Mon Sep 17 00:00:00 2001 From: Micah Moore Date: Fri, 16 Jan 2026 14:59:25 -0500 Subject: [PATCH 02/11] Adds Foundation import to Database+SQLCipher --- GRDB/Core/Database+SQLCipher.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/GRDB/Core/Database+SQLCipher.swift b/GRDB/Core/Database+SQLCipher.swift index fa031d625..46327dbd7 100644 --- a/GRDB/Core/Database+SQLCipher.swift +++ b/GRDB/Core/Database+SQLCipher.swift @@ -1,5 +1,6 @@ #if SQLITE_HAS_CODEC import SQLCipher +import Foundation extension Database { From bd954c67918bc69e348500a8893a49a60be5762c Mon Sep 17 00:00:00 2001 From: Micah Moore Date: Mon, 19 Jan 2026 13:37:41 -0500 Subject: [PATCH 03/11] Removes GRDBSQLCipher import from SQLCipher conditional import in Database.swift to allow SQLCipher forkers versatility for defining pass-throughs to GRDB required sqlite variadic functions. --- GRDB/Core/Database.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index bcce91c53..ae30f0f4f 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -6,7 +6,6 @@ import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework #elseif SQLCipher import SQLCipher -import GRDBSQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif From c0ef0e0f99fe6ae44cf740d8479d54a56c0321de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 1 Feb 2026 11:19:30 +0100 Subject: [PATCH 04/11] Use the same logic for importing SQLCipher as in other files --- GRDB/Core/Database+SQLCipher.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/GRDB/Core/Database+SQLCipher.swift b/GRDB/Core/Database+SQLCipher.swift index 46327dbd7..e7b335dc4 100644 --- a/GRDB/Core/Database+SQLCipher.swift +++ b/GRDB/Core/Database+SQLCipher.swift @@ -1,5 +1,11 @@ #if SQLITE_HAS_CODEC +#if GRDBCIPHER // CocoaPods (SQLCipher subspec) import SQLCipher +#elseif SQLCipher +import SQLCipher +#else +#error("No SQLCipher library configured") +#endif import Foundation extension Database { From 2c4492a098c20e24157ec672ab4ee724bf5fe94f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 1 Feb 2026 11:35:27 +0100 Subject: [PATCH 05/11] Do not define CipherLogLevel as a frozen enum --- GRDB/Core/Database+SQLCipher.swift | 34 +++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/GRDB/Core/Database+SQLCipher.swift b/GRDB/Core/Database+SQLCipher.swift index e7b335dc4..678d42815 100644 --- a/GRDB/Core/Database+SQLCipher.swift +++ b/GRDB/Core/Database+SQLCipher.swift @@ -11,16 +11,34 @@ import Foundation extension Database { /// Granularitly of SQLCipher log outputs - /// Each log level is more verbose than the last + /// + /// Each log level is more verbose than the last. With ``debug`` and + /// ``trace`` the logging system will generate a significant log volume. /// /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_log_level - public enum CipherLogLevel: String { - case none - case error - case warn - case info - case debug - case trace + /// + /// ## Topics + /// + /// ### Log Levels + /// + /// - ``none`` + /// - ``error`` + /// - ``warn`` + /// - ``info`` + /// - ``debug`` + /// - ``trace`` + public struct CipherLogLevel: RawRepresentable, Sendable { + public var rawValue: String + public init(rawValue: String) { + self.rawValue = rawValue + } + + public static let none = Self(rawValue: "NONE") + public static let error = Self(rawValue: "ERROR") + public static let warn = Self(rawValue: "WARN") + public static let info = Self(rawValue: "INFO") + public static let debug = Self(rawValue: "DEBUG") + public static let trace = Self(rawValue: "TRACE") } /// - Returns: the SQLCipher version From d664db9610ef7a6d797f36c8f4c38b5bbe6d08d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 1 Feb 2026 11:37:44 +0100 Subject: [PATCH 06/11] Enhance documentation formatting --- GRDB/Core/Database+SQLCipher.swift | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/GRDB/Core/Database+SQLCipher.swift b/GRDB/Core/Database+SQLCipher.swift index 678d42815..198b2a3e7 100644 --- a/GRDB/Core/Database+SQLCipher.swift +++ b/GRDB/Core/Database+SQLCipher.swift @@ -148,13 +148,15 @@ extension Database { /// from Zetetic: https://www.zetetic.net/sqlcipher/buy/ /// 15-day free trials are available by request: https://www.zetetic.net/sqlcipher/trial/ /// - /// Call this method from `Configuration.prepareDatabase`, + /// Call this method from ``Configuration/prepareDatabase(_:)``, /// as in the example below: /// - /// var config = Configuration() - /// config.prepareDatabase { db in - /// try db.applyLicense(license) - /// } + /// ```swift + /// var config = Configuration() + /// config.prepareDatabase { db in + /// try db.applyLicense(license) + /// } + /// ``` /// /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_license /// - Parameter license: base64 SQLCipher license code to activate SQLCipher commercial From a1a9c3aefe8d39c039b72aca0d231e4de6e47d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 1 Feb 2026 12:23:22 +0100 Subject: [PATCH 07/11] Make links appear as links in DocC --- GRDB/Core/Database+SQLCipher.swift | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/GRDB/Core/Database+SQLCipher.swift b/GRDB/Core/Database+SQLCipher.swift index 198b2a3e7..4225ae17a 100644 --- a/GRDB/Core/Database+SQLCipher.swift +++ b/GRDB/Core/Database+SQLCipher.swift @@ -15,7 +15,7 @@ extension Database { /// Each log level is more verbose than the last. With ``debug`` and /// ``trace`` the logging system will generate a significant log volume. /// - /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_log_level + /// See /// /// ## Topics /// @@ -43,7 +43,7 @@ extension Database { /// - Returns: the SQLCipher version /// - /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_version + /// See public var cipherVersion: String { get throws { try String.fetchOne(self, sql: "PRAGMA cipher_version")! } } @@ -51,7 +51,7 @@ extension Database { /// - Returns: the SQLCipher fips status: 1 for fips mode, 0 for non-fips mode /// The FIPS status will not be initialized until the database connection has been keyed /// - /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_fips_status + /// See public var cipherFipsStatus: String? { get throws { try String.fetchOne(self, sql: "PRAGMA cipher_fips_status") } } @@ -59,7 +59,7 @@ extension Database { /// - Returns: The compiled crypto provider. /// The database must be keyed before requesting the name of the crypto provider. /// - /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_provider + /// See public var cipherProvider: String? { get throws { try String.fetchOne(self, sql: "PRAGMA cipher_provider") } } @@ -67,7 +67,7 @@ extension Database { /// - Returns: the version number provided from the compiled crypto provider. /// This value, if known, is available only after the database has been keyed. /// - /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_provider_version + /// See public var cipherProviderVersion: String? { get throws { try String.fetchOne(self, sql: "PRAGMA cipher_provider_version") } } @@ -145,8 +145,8 @@ extension Database { /// Failure to provide a license code, or use of an expired trial code, /// will result in an `SQLITE_AUTH (23)` error code reported from the SQLite API /// License Codes will activate SQLCipher Commercial or Enterprise packages - /// from Zetetic: https://www.zetetic.net/sqlcipher/buy/ - /// 15-day free trials are available by request: https://www.zetetic.net/sqlcipher/trial/ + /// from Zetetic: + /// 15-day free trials are available by request: /// /// Call this method from ``Configuration/prepareDatabase(_:)``, /// as in the example below: @@ -158,7 +158,7 @@ extension Database { /// } /// ``` /// - /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_license + /// See /// - Parameter license: base64 SQLCipher license code to activate SQLCipher commercial public func applyLicense(_ license: String) throws { try execute(sql: "PRAGMA cipher_license = '\(license)'") @@ -181,7 +181,7 @@ extension Database { /// Instructs SQLCipher to disable logging internal debugging and operational information /// - /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_log + /// See public func disableCipherLogging() throws { try execute(sql: "PRAGMA cipher_log_level = \(CipherLogLevel.none.rawValue.uppercased())") } From 8b5017714f0b4cefdd87602f6b751b0b1ae97217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 1 Feb 2026 12:28:51 +0100 Subject: [PATCH 08/11] Support more SQLCipher log destinations --- GRDB/Core/Database+SQLCipher.swift | 49 +++++++++++++++++++-------- Tests/GRDBTests/EncryptionTests.swift | 20 +++++++++++ 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/GRDB/Core/Database+SQLCipher.swift b/GRDB/Core/Database+SQLCipher.swift index 4225ae17a..7419ac684 100644 --- a/GRDB/Core/Database+SQLCipher.swift +++ b/GRDB/Core/Database+SQLCipher.swift @@ -10,7 +10,22 @@ import Foundation extension Database { - /// Granularitly of SQLCipher log outputs + /// Destination of SQLCipher logs. + /// + /// See + public struct CipherLogTarget: Sendable { + public var rawValue: String + public init(rawValue: String) { + self.rawValue = rawValue + } + + public static let stdout = Self(rawValue: "stdout") + public static let stderr = Self(rawValue: "stderr") + public static let device = Self(rawValue: "device") + public static func file(_ path: String) -> Self { Self(rawValue: path) } + } + + /// Granularitly of SQLCipher log outputs. /// /// Each log level is more verbose than the last. With ``debug`` and /// ``trace`` the logging system will generate a significant log volume. @@ -164,26 +179,30 @@ extension Database { try execute(sql: "PRAGMA cipher_license = '\(license)'") } - /// Instructs SQLCipher to log internal debugging and operational information - /// to the sepecified log target (device) using `os_log` - /// The supplied logLevel will determine the granularity of the logs output - /// Available logLevel options are: NONE, ERROR, WARN, INFO, DEBUG, TRACE - /// Note that each level is more verbose than the last, - /// and particularly with DEBUG and TRACE the logging system will generate - /// a significant log volume + /// Instructs SQLCipher to log internal debugging and operational information. /// - /// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_log - /// - Parameter logLevel: CipherLogLevel The granularity to use for the logging system - defaults to `DEBUG` - public func enableCipherLogging(logLevel: CipherLogLevel = .debug) throws { - try execute(sql: "PRAGMA cipher_log = device") - try execute(sql: "PRAGMA cipher_log_level = \(logLevel.rawValue.uppercased())") + /// The supplied ``logLevel`` determines the granularity of the logs output. + /// + /// See + /// - Parameter logLevel: The granularity to use for the logging system - defaults to `DEBUG`. + /// - Parameter target: The destination of SQLCipher logs - defaults to `.device`. + public func enableCipherLogging( + logLevel: CipherLogLevel = .debug, + target: CipherLogTarget = .device + ) throws { + // Pragma do not support SQL arguments. We need to generate SQL that contains literal values: + // PRAGMA cipher_log = '/path/to/file' + // PRAGMA cipher_log_level = 'xxx' + let context = SQLGenerationContext(self, argumentsSink: .literalValues) + try execute(sql: SQL("PRAGMA cipher_log = \(target.rawValue)").sql(context)) + try execute(sql: SQL("PRAGMA cipher_log_level = \(logLevel.rawValue.uppercased())").sql(context)) } - /// Instructs SQLCipher to disable logging internal debugging and operational information + /// Instructs SQLCipher to disable logging internal debugging and operational information. /// /// See public func disableCipherLogging() throws { - try execute(sql: "PRAGMA cipher_log_level = \(CipherLogLevel.none.rawValue.uppercased())") + try execute(sql: "PRAGMA cipher_log_level = NONE") } internal func validateSQLCipher() throws { diff --git a/Tests/GRDBTests/EncryptionTests.swift b/Tests/GRDBTests/EncryptionTests.swift index b101d3ebd..a507827a8 100644 --- a/Tests/GRDBTests/EncryptionTests.swift +++ b/Tests/GRDBTests/EncryptionTests.swift @@ -776,5 +776,25 @@ let testBundle = Bundle(for: GRDBTestCase.self) XCTAssertNotNil(try db.cipherProviderVersion) } } + + func test_enableCipherLogging() throws { + let dbQueue = try makeDatabaseQueue() + let cipherVersion = try dbQueue.read { try $0.cipherVersion } + if "4.0.0".compare(cipherVersion, options: .numeric) == .orderedDescending { + throw XCTSkip("SQLCipher logging is not available") + } + try dbQueue.inDatabase { db in + try db.enableCipherLogging() + try db.enableCipherLogging(logLevel: .error) + try db.enableCipherLogging(target: .stdout) + + let logFilePath = (NSTemporaryDirectory() as NSString).appendingPathComponent(ProcessInfo.processInfo.globallyUniqueString) + defer { + try? FileManager.default.removeItem(atPath: logFilePath) + } + try db.enableCipherLogging(target: .file(logFilePath)) + XCTAssert(FileManager.default.fileExists(atPath: logFilePath)) + } + } } #endif From 14fdba1ef349a9e85c3bc1894d7b90457d6bd616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 1 Feb 2026 12:37:46 +0100 Subject: [PATCH 09/11] Rename CipherXxx APIs to SQLCipherXxx when possible --- GRDB/Core/Database+SQLCipher.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/GRDB/Core/Database+SQLCipher.swift b/GRDB/Core/Database+SQLCipher.swift index 7419ac684..78b60cc23 100644 --- a/GRDB/Core/Database+SQLCipher.swift +++ b/GRDB/Core/Database+SQLCipher.swift @@ -13,7 +13,7 @@ extension Database { /// Destination of SQLCipher logs. /// /// See - public struct CipherLogTarget: Sendable { + public struct SQLCipherLogTarget: Sendable { public var rawValue: String public init(rawValue: String) { self.rawValue = rawValue @@ -42,7 +42,7 @@ extension Database { /// - ``info`` /// - ``debug`` /// - ``trace`` - public struct CipherLogLevel: RawRepresentable, Sendable { + public struct SQLCipherLogLevel: RawRepresentable, Sendable { public var rawValue: String public init(rawValue: String) { self.rawValue = rawValue @@ -169,13 +169,13 @@ extension Database { /// ```swift /// var config = Configuration() /// config.prepareDatabase { db in - /// try db.applyLicense(license) + /// try db.applySQLCipherLicense(license) /// } /// ``` /// /// See /// - Parameter license: base64 SQLCipher license code to activate SQLCipher commercial - public func applyLicense(_ license: String) throws { + public func applySQLCipherLicense(_ license: String) throws { try execute(sql: "PRAGMA cipher_license = '\(license)'") } @@ -187,8 +187,8 @@ extension Database { /// - Parameter logLevel: The granularity to use for the logging system - defaults to `DEBUG`. /// - Parameter target: The destination of SQLCipher logs - defaults to `.device`. public func enableCipherLogging( - logLevel: CipherLogLevel = .debug, - target: CipherLogTarget = .device + logLevel: SQLCipherLogLevel = .debug, + target: SQLCipherLogTarget = .device ) throws { // Pragma do not support SQL arguments. We need to generate SQL that contains literal values: // PRAGMA cipher_log = '/path/to/file' From 79276d7f68e313e97f21111ef960b2a45c7f9027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 1 Feb 2026 13:16:40 +0100 Subject: [PATCH 10/11] Prepare support for SPM+SQLCipher --- GRDB/Core/Database.swift | 1 + Package.swift | 18 +++++++++++ Sources/GRDBSQLCipher/SQLCipher_config.c | 1 + .../GRDBSQLCipher/include/SQLCipher_config.h | 32 +++++++++++++++++++ .../GRDBSQLCipher/include/module.modulemap | 4 +++ 5 files changed, 56 insertions(+) create mode 100644 Sources/GRDBSQLCipher/SQLCipher_config.c create mode 100644 Sources/GRDBSQLCipher/include/SQLCipher_config.h create mode 100644 Sources/GRDBSQLCipher/include/module.modulemap diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index ae30f0f4f..bcce91c53 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -6,6 +6,7 @@ import SQLite3 #elseif GRDBCUSTOMSQLITE // GRDBCustom Framework #elseif SQLCipher import SQLCipher +import GRDBSQLCipher #else // Default SPM trait must be the default. It impossible to detect from Xcode. import GRDBSQLite #endif diff --git a/Package.swift b/Package.swift index 26c61734a..606b665ef 100644 --- a/Package.swift +++ b/Package.swift @@ -36,6 +36,12 @@ if ProcessInfo.processInfo.environment["SPI_BUILDER"] == "1" { dependencies.append(.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0")) } +// GRDB+SQLCipher: Uncomment those lines +//dependencies.append(.package(url: "https://github.com/sqlcipher/SQLCipher.swift.git", from: "4.11.0")) +//cSettings.append(.define("SQLITE_HAS_CODEC")) +//swiftSettings.append(.define("SQLITE_HAS_CODEC")) +//swiftSettings.append(.define("SQLCipher")) + let package = Package( name: "GRDB", defaultLocalization: "en", // for tests @@ -46,19 +52,30 @@ let package = Package( .watchOS(.v7), ], products: [ + // GRDB+SQLCipher: Delete the GRDBSQLite library .library(name: "GRDBSQLite", targets: ["GRDBSQLite"]), .library(name: "GRDB", targets: ["GRDB"]), .library(name: "GRDB-dynamic", type: .dynamic, targets: ["GRDB"]), ], dependencies: dependencies, targets: [ + // GRDB+SQLCipher: Delete the GRDBSQLite target .systemLibrary( name: "GRDBSQLite", providers: [.apt(["libsqlite3-dev"])]), + // GRDB+SQLCipher: Uncomment the GRDBSQLCipher target + //.target( + // name: "GRDBSQLCipher", + // dependencies: [.product(name: "SQLCipher", package: "SQLCipher.swift")] + //), .target( name: "GRDB", dependencies: [ + // GRDB+SQLCipher: Delete the GRDBSQLite dependency .target(name: "GRDBSQLite"), + // GRDB+SQLCipher: Uncomment the SQLCipher and GRDBSQLCipher dependencies + //.product(name: "SQLCipher", package: "SQLCipher.swift"), + //.target(name: "GRDBSQLCipher"), ], path: "GRDB", resources: [.copy("PrivacyInfo.xcprivacy")], @@ -85,6 +102,7 @@ let package = Package( .copy("GRDBTests/Betty.jpeg"), .copy("GRDBTests/InflectionsTests.json"), .copy("GRDBTests/Issue1383.sqlite"), + .copy("GRDBTests/db.SQLCipher3"), ], cSettings: cSettings, swiftSettings: swiftSettings + [ diff --git a/Sources/GRDBSQLCipher/SQLCipher_config.c b/Sources/GRDBSQLCipher/SQLCipher_config.c new file mode 100644 index 000000000..26b22bdb9 --- /dev/null +++ b/Sources/GRDBSQLCipher/SQLCipher_config.c @@ -0,0 +1 @@ +// empty c-file diff --git a/Sources/GRDBSQLCipher/include/SQLCipher_config.h b/Sources/GRDBSQLCipher/include/SQLCipher_config.h new file mode 100644 index 000000000..a405306f6 --- /dev/null +++ b/Sources/GRDBSQLCipher/include/SQLCipher_config.h @@ -0,0 +1,32 @@ +#ifndef grdb_config_h +#define grdb_config_h + +#include + +typedef void(*_errorLogCallback)(void *pArg, int iErrCode, const char *zMsg); + +/// Wrapper around sqlite3_config(SQLITE_CONFIG_LOG, ...) which is a variadic +/// function that can't be used from Swift. +static inline void _registerErrorLogCallback(_errorLogCallback callback) { + sqlite3_config(SQLITE_CONFIG_LOG, callback, 0); +} + +#if SQLITE_VERSION_NUMBER >= 3029000 +/// Wrapper around sqlite3_db_config() which is a variadic function that can't +/// be used from Swift. +static inline void _disableDoubleQuotedStringLiterals(sqlite3 *db) { + sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DDL, 0, (void *)0); + sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DML, 0, (void *)0); +} + +/// Wrapper around sqlite3_db_config() which is a variadic function that can't +/// be used from Swift. +static inline void _enableDoubleQuotedStringLiterals(sqlite3 *db) { + sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DDL, 1, (void *)0); + sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DML, 1, (void *)0); +} +#else +static inline void _disableDoubleQuotedStringLiterals(sqlite3 *db) { } +static inline void _enableDoubleQuotedStringLiterals(sqlite3 *db) { } +#endif +#endif /* grdb_config_h */ diff --git a/Sources/GRDBSQLCipher/include/module.modulemap b/Sources/GRDBSQLCipher/include/module.modulemap new file mode 100644 index 000000000..b5e64c362 --- /dev/null +++ b/Sources/GRDBSQLCipher/include/module.modulemap @@ -0,0 +1,4 @@ +module GRDBSQLCipher { + header "SQLCipher_config.h" + export * + } From 14d4c842e4df98500041497881a47219fa1c58ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 1 Feb 2026 14:48:16 +0100 Subject: [PATCH 11/11] Document how to enable SQLCipher with SPM --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 60c79359a..0bd7c8db8 100644 --- a/README.md +++ b/README.md @@ -4589,7 +4589,9 @@ Encryption **GRDB can encrypt your database with [SQLCipher](http://sqlcipher.net) v3.4+.** -Use [CocoaPods](http://cocoapods.org/), and specify in your `Podfile`: +To use SQLCipher with the [Swift Package Manager](https://swift.org/package-manager/), you must fork GRDB, and modify `Package.swift`. Instructions are in the file itself, in comments that contain "GRDB+SQLCipher". + +To use SQLCipher with [CocoaPods](http://cocoapods.org/), specify in your `Podfile`: ```ruby # GRDB with SQLCipher 4