From 0914ab2c74b4455acf2bdbeccc0dd8c311c716ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 3 Jul 2015 08:20:22 +0200 Subject: [PATCH] Make sure select statements are executed on the database queue --- GRDB/Database.swift | 20 +++----- GRDB/SelectStatement.swift | 95 +++++++++++++++++++------------------- RowModel.swift | 24 +++++----- 3 files changed, 65 insertions(+), 74 deletions(-) diff --git a/GRDB/Database.swift b/GRDB/Database.swift index d4a5dab7f..a83fd7577 100644 --- a/GRDB/Database.swift +++ b/GRDB/Database.swift @@ -158,45 +158,37 @@ func failOnError(@noescape block: (Void) throws -> R) -> R { extension Database { - public func fetchRowGenerator(sql: String, bindings: Bindings? = nil) -> AnyGenerator { + public func fetchRows(sql: String, bindings: Bindings? = nil) -> AnySequence { return failOnError { let statement = try selectStatement(sql, bindings: bindings) - return statement.fetchRowGenerator() + return statement.fetchRows() } } - public func fetchRows(sql: String, bindings: Bindings? = nil) -> AnySequence { - return AnySequence { self.fetchRowGenerator(sql, bindings: bindings) } - } - public func fetchAllRows(sql: String, bindings: Bindings? = nil) -> [Row] { return Array(fetchRows(sql, bindings: bindings)) } public func fetchOneRow(sql: String, bindings: Bindings? = nil) -> Row? { - return fetchRowGenerator(sql, bindings: bindings).next() + return fetchRows(sql, bindings: bindings).generate().next() } } extension Database { - public func fetchGenerator(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> AnyGenerator { + public func fetch(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> AnySequence { return failOnError { let statement = try selectStatement(sql, bindings: bindings) - return statement.fetchGenerator(type) + return statement.fetch(type) } } - public func fetch(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> AnySequence { - return AnySequence { self.fetchGenerator(type, sql, bindings: bindings) } - } - public func fetchAll(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> [T?] { return Array(fetch(type, sql, bindings: bindings)) } public func fetchOne(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> T? { - if let first = fetchGenerator(type, sql, bindings: bindings).next() { + if let first = fetch(type, sql, bindings: bindings).generate().next() { // one row containing an optional value return first } else { diff --git a/GRDB/SelectStatement.swift b/GRDB/SelectStatement.swift index 99ec87335..7cf10e3ac 100644 --- a/GRDB/SelectStatement.swift +++ b/GRDB/SelectStatement.swift @@ -16,6 +16,7 @@ public final class SelectStatement : Statement { init(database: Database, sql: String, bindings: Bindings?, unsafe: Bool) throws { self.unsafe = unsafe try super.init(database: database, sql: sql, bindings: bindings) + assert(databaseQueueID != nil) } func sqliteValueAtIndex(index: Int) -> SQLiteValue { @@ -37,81 +38,79 @@ public final class SelectStatement : Statement { extension SelectStatement { - public func fetchRowGenerator() -> AnyGenerator { + public func fetchRows() -> AnySequence { // TODO: Document this reset performed on each generation try! reset() - var logSQL = database.configuration.verbose - return anyGenerator { () -> Row? in - // Make sure values are not consumed in a different queue. - // - // Here we avoid this pattern: - // - // let rows = dbQueue.inDatabase { db in - // try db.fetchRows("...") - // } - // for row in rows { // fatal error - // ... - // } - guard self.databaseQueueID == dispatch_get_specific(DatabaseQueue.databaseQueueIDKey) else { - fatalError("SelectStatement was not iterated on its database queue. Consider wrapping the results of the fetch in an Array before escaping the database queue.") - } - - if logSQL { - NSLog("%@", self.sql) - logSQL = false - } - let code = sqlite3_step(self.sqliteStatement) - switch code { - case SQLITE_DONE: - return nil - case SQLITE_ROW: - return Row(statement: self, unsafe: self.unsafe) - default: - failOnError { () -> Void in - throw SQLiteError(code: code, sqliteConnection: self.database.sqliteConnection, sql: self.sql) + return AnySequence { () -> AnyGenerator in + var logSQL = self.database.configuration.verbose + return anyGenerator { () -> Row? in + // Make sure values are not consumed in a different queue. + // + // Here we avoid this pattern: + // + // let rows = dbQueue.inDatabase { db in + // try db.fetchRows("...") + // } + // for row in rows { // fatal error + // ... + // } + assert(self.databaseQueueID != nil) + guard self.databaseQueueID == dispatch_get_specific(DatabaseQueue.databaseQueueIDKey) else { + fatalError("SelectStatement was not iterated on its database queue. Consider wrapping the results of the fetch in an Array before escaping the database queue.") + } + + if logSQL { + NSLog("%@", self.sql) + logSQL = false + } + let code = sqlite3_step(self.sqliteStatement) + switch code { + case SQLITE_DONE: + return nil + case SQLITE_ROW: + return Row(statement: self, unsafe: self.unsafe) + default: + failOnError { () -> Void in + throw SQLiteError(code: code, sqliteConnection: self.database.sqliteConnection, sql: self.sql) + } + return nil } - return nil } } } - public func fetchRows() -> AnySequence { - return AnySequence { self.fetchRowGenerator() } - } - public func fetchAllRows() -> [Row] { return Array(fetchRows()) } public func fetchOneRow() -> Row? { - return fetchRowGenerator().next() + return fetchRows().generate().next() } } extension SelectStatement { - public func fetchGenerator(type: T.Type) -> AnyGenerator { - let rowGenerator = fetchRowGenerator() - return anyGenerator { () -> T?? in - if let row = rowGenerator.next() { - return Optional.Some(row.value(atIndex: 0) as T?) - } else { - return nil + public func fetch(type: T.Type) -> AnySequence { + let rowSequence = fetchRows() + return AnySequence { () -> AnyGenerator in + let rowGenerator = rowSequence.generate() + return anyGenerator { () -> T?? in + if let row = rowGenerator.next() { + return Optional.Some(row.value(atIndex: 0) as T?) + } else { + return nil + } } } } - public func fetch(type: T.Type) -> AnySequence { - return AnySequence { self.fetchGenerator(type) } - } - public func fetchAll(type: T.Type) -> [T?] { return Array(fetch(type)) } public func fetchOne(type: T.Type) -> T? { - if let optionalValue = fetchGenerator(type).next() { + if let optionalValue = fetch(type).generate().next() { // one row containing an optional value return optionalValue } else { diff --git a/RowModel.swift b/RowModel.swift index 4025c40f1..53798f01f 100644 --- a/RowModel.swift +++ b/RowModel.swift @@ -195,19 +195,19 @@ public class RowModel { } extension Database { - public func fetchGenerator(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> AnyGenerator { - let rowGenerator = fetchRowGenerator(sql, bindings: bindings) - return anyGenerator { - if let row = rowGenerator.next() { - return T.init(row: row) - } else { - return nil - } - } - } public func fetch(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> AnySequence { - return AnySequence { self.fetchGenerator(type, sql, bindings: bindings) } + let rowSequence = fetchRows(sql, bindings: bindings) + return AnySequence { () -> AnyGenerator in + let rowGenerator = rowSequence.generate() + return anyGenerator { () -> T? in + if let row = rowGenerator.next() { + return T.init(row: row) + } else { + return nil + } + } + } } public func fetchAll(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> [T] { @@ -215,7 +215,7 @@ extension Database { } public func fetchOne(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> T? { - if let first = fetchGenerator(type, sql, bindings: bindings).next() { + if let first = fetch(type, sql, bindings: bindings).generate().next() { // one row containing an optional value return first } else {