mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Database.readOnly is reentrant
This commit is contained in:
1 parent
e3d2559b9d
commit
ac85ffff84
2 files changed
+69
-8
No files matched your search
@@ -162,7 +162,20 @@ public final class Database {
|
||||
|
||||
private var functions = Set<DatabaseFunction>()
|
||||
private var collations = Set<DatabaseCollation>()
|
||||
|
||||
private var readOnlyDepth = 0 {
|
||||
didSet {
|
||||
// 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)
|
||||
// Assume those pragmas never fail
|
||||
switch (oldValue, readOnlyDepth) {
|
||||
case (1, 0):
|
||||
try! internalCachedUpdateStatement(sql: "PRAGMA query_only = 0").execute()
|
||||
case (0, 1):
|
||||
try! internalCachedUpdateStatement(sql: "PRAGMA query_only = 1").execute()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
}
|
||||
private var isClosed: Bool = false
|
||||
|
||||
// MARK: - Initializer
|
||||
@@ -541,11 +554,8 @@ extension Database {
|
||||
return try 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)
|
||||
// Assume those pragmas never fail
|
||||
try! internalCachedUpdateStatement(sql: "PRAGMA query_only = 1").execute()
|
||||
defer { try! internalCachedUpdateStatement(sql: "PRAGMA query_only = 0").execute() }
|
||||
readOnlyDepth += 1
|
||||
defer { readOnlyDepth -= 1 }
|
||||
return try block()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import XCTest
|
||||
#if GRDBCUSTOMSQLITE
|
||||
import GRDBCustomSQLite
|
||||
@testable import GRDBCustomSQLite
|
||||
#else
|
||||
import GRDB
|
||||
#if SWIFT_PACKAGE
|
||||
import CSQLite
|
||||
#else
|
||||
import SQLite3
|
||||
#endif
|
||||
@testable import GRDB
|
||||
#endif
|
||||
|
||||
class DatabaseTests : GRDBTestCase {
|
||||
@@ -311,4 +316,50 @@ class DatabaseTests : GRDBTestCase {
|
||||
XCTAssertEqual(lastSQLQuery, "COMMIT TRANSACTION")
|
||||
}
|
||||
}
|
||||
|
||||
// Test an internal API
|
||||
func testReadOnly() throws {
|
||||
// query_only pragma was added in SQLite 3.8.0 http://www.sqlite.org/changes.html#version_3_8_0
|
||||
guard sqlite3_libversion_number() >= 3008000 else {
|
||||
return
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE t (id INTEGER PRIMARY KEY)")
|
||||
|
||||
// Write access OK
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
|
||||
try db.readOnly {
|
||||
do {
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
XCTFail()
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_READONLY)
|
||||
}
|
||||
|
||||
// Reentrancy
|
||||
try db.readOnly {
|
||||
do {
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
XCTFail()
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_READONLY)
|
||||
}
|
||||
}
|
||||
|
||||
// Still read-only
|
||||
do {
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
XCTFail()
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_READONLY)
|
||||
}
|
||||
}
|
||||
|
||||
// Write access OK
|
||||
try db.execute(sql: "INSERT INTO t DEFAULT VALUES")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user