mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
PRAGMA query_only was added in SQLite 3.8.0 (iOS 8.2+ and OS X 10.10+)
This commit is contained in:
1 parent
e64b766599
commit
5c7712bf6c
3 files changed
+37
-5
No files matched your search
@@ -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<T>(_ 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user