diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index 85295bc7d..551e9e8c8 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -223,13 +223,24 @@ extension DatabaseQueue : DatabaseReader { /// /// This method is *not* reentrant. /// - /// Attempts to write in the database throw a DatabaseError whose + /// Starting iOS 8.2, OSX 10.10, and with custom SQLite builds and + /// SQLCipher, attempts to write in the database throw a DatabaseError whose /// resultCode is `SQLITE_READONLY`. /// /// - parameter block: A block that accesses the database. /// - throws: The error thrown by the block. public func read(_ block: (Database) throws -> T) rethrows -> T { - return try inDatabase { try readOnly($0, block) } + // query_only pragma was added in SQLite 3.8.0 http://www.sqlite.org/changes.html#version_3_8_0 + // It is available from iOS 8.2 and OS X 10.10 https://github.com/yapstudios/YapDatabase/wiki/SQLite-version-(bundled-with-OS) + #if USING_CUSTOMSQLITE || USING_SQLCIPHER + return try inDatabase { try readOnly($0, block) } + #else + if #available(iOS 8.2, OSX 10.10, *) { + return try inDatabase { try readOnly($0, block) } + } else { + return try inDatabase(block) + } + #endif } /// Alias for `inDatabase`. See `DatabaseReader.unsafeRead`. @@ -299,13 +310,24 @@ extension DatabaseQueue : DatabaseWriter { /// Synchronously executes *block*. /// - /// Attempts to write in the database throw a DatabaseError whose + /// Starting iOS 8.2, OSX 10.10, and with custom SQLite builds and + /// SQLCipher, attempts to write in the database throw a DatabaseError whose /// resultCode is `SQLITE_READONLY`. /// /// This method must be called from the protected database dispatch queue. /// See `DatabaseWriter.readFromCurrentState`. public func readFromCurrentState(_ block: @escaping (Database) -> Void) { - serializedDatabase.execute { readOnly($0, block) } + // query_only pragma was added in SQLite 3.8.0 http://www.sqlite.org/changes.html#version_3_8_0 + // It is available from iOS 8.2 and OS X 10.10 https://github.com/yapstudios/YapDatabase/wiki/SQLite-version-(bundled-with-OS) + #if USING_CUSTOMSQLITE || USING_SQLCIPHER + serializedDatabase.execute { readOnly($0, block) } + #else + if #available(iOS 8.2, OSX 10.10, *) { + serializedDatabase.execute { readOnly($0, block) } + } else { + serializedDatabase.execute(block) + } + #endif } } diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index 8f89777c4..cd6f3e5d9 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -53,7 +53,9 @@ public protocol DatabaseReader : class { /// let count = try Int.fetchOne(db, "SELECT COUNT(*) FROM wines")! /// } /// - /// Guarantee 2: the block argument can't modify the database. + /// Guarantee 2: Starting iOS 8.2, OSX 10.10, and with custom SQLite builds + /// and SQLCipher, attempts to write in the database throw a DatabaseError + /// whose resultCode is `SQLITE_READONLY`. /// /// - parameter block: A block that accesses the database. /// - throws: The error thrown by the block, or any DatabaseError that would diff --git a/Tests/Public/Core/DatabaseReader/DatabaseReaderTests.swift b/Tests/Public/Core/DatabaseReader/DatabaseReaderTests.swift index cf9b41f7f..6edd7dd0b 100644 --- a/Tests/Public/Core/DatabaseReader/DatabaseReaderTests.swift +++ b/Tests/Public/Core/DatabaseReader/DatabaseReaderTests.swift @@ -10,6 +10,14 @@ import XCTest class DatabaseReaderTests : GRDBTestCase { func testDatabaseQueueReadPreventsDatabaseModification() throws { + // query_only pragma was added in SQLite 3.8.0 http://www.sqlite.org/changes.html#version_3_8_0 + // It is available from iOS 8.2 and OS X 10.10 https://github.com/yapstudios/YapDatabase/wiki/SQLite-version-(bundled-with-OS) + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(table: "table1") { t in