From 343a239905c5df75f111bb2f24c71ad1ac303aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 2 Jul 2015 12:27:11 +0200 Subject: [PATCH] Functions that fetch no longer throw (they crash), but the low-level Database.selectStatement(_:bindings:unsafe:). --- DatabaseMigratorTests.swift | 4 ++-- GRDB Tests/DatabaseTests.swift | 2 +- GRDB/DatabaseQueue.swift | 10 ++++++++++ README.md | 3 +-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/DatabaseMigratorTests.swift b/DatabaseMigratorTests.swift index 888e5fdc9..eb5c78001 100644 --- a/DatabaseMigratorTests.swift +++ b/DatabaseMigratorTests.swift @@ -31,7 +31,7 @@ class DatabaseMigratorTests: GRDBTests { assertNoError { try migrator.migrate(dbQueue) - try dbQueue.inDatabase { db in + dbQueue.inDatabase { db in XCTAssertTrue(db.tableExists("persons")) XCTAssertTrue(db.tableExists("pets")) } @@ -43,7 +43,7 @@ class DatabaseMigratorTests: GRDBTests { assertNoError { try migrator.migrate(dbQueue) - try dbQueue.inDatabase { db in + dbQueue.inDatabase { db in XCTAssertTrue(db.tableExists("persons")) XCTAssertFalse(db.tableExists("pets")) } diff --git a/GRDB Tests/DatabaseTests.swift b/GRDB Tests/DatabaseTests.swift index a6d935413..cac9208c8 100644 --- a/GRDB Tests/DatabaseTests.swift +++ b/GRDB Tests/DatabaseTests.swift @@ -428,7 +428,7 @@ class DatabaseTests: GRDBTests { // Fetching rows and values: - try dbQueue.inDatabase { db in + dbQueue.inDatabase { db in for row in db.fetchRows("SELECT * FROM persons") { // Leverage Swift type inference let name: String? = row.value(atIndex: 1) diff --git a/GRDB/DatabaseQueue.swift b/GRDB/DatabaseQueue.swift index 75446c8b8..9a3377ff6 100644 --- a/GRDB/DatabaseQueue.swift +++ b/GRDB/DatabaseQueue.swift @@ -39,6 +39,16 @@ public class DatabaseQueue { } } + public func inDatabase(block: (db: Database) -> Void) { + guard databaseQueueID != dispatch_get_specific(DatabaseQueue.databaseQueueIDKey) else { + fatalError("inDatabase(_:) was called reentrantly on the same queue, which would lead to a deadlock") + } + + dispatch_sync(queue) { () -> Void in + block(db: self.database) + } + } + public func inDatabase(block: (db: Database) -> R) -> R { guard databaseQueueID != dispatch_get_specific(DatabaseQueue.databaseQueueIDKey) else { fatalError("inDatabase(_:) was called reentrantly on the same queue, which would lead to a deadlock") diff --git a/README.md b/README.md index 006fb24b0..3171979a2 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ try dbQueue.inTransaction { db in // Fetching rows and values: -try dbQueue.inDatabase { db in +dbQueue.inDatabase { db in for row in db.fetchRows("SELECT * FROM persons") { // Leverage Swift type inference let name: String? = row.value(atIndex: 1) @@ -94,5 +94,4 @@ let names = dbQueue.inDatabase { db in db.fetchValues("SELECT name FROM persons ORDER BY name", type: String.self).map { $0! } } // names is [String]: ["Arthur", "Barbara"] - ```