mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Make sure select statements are executed on the database queue
This commit is contained in:
1 parent
790177719a
commit
0914ab2c74
3 files changed
+65
-74
No files matched your search
+6
-14
@@ -158,45 +158,37 @@ func failOnError<R>(@noescape block: (Void) throws -> R) -> R {
|
||||
|
||||
extension Database {
|
||||
|
||||
public func fetchRowGenerator(sql: String, bindings: Bindings? = nil) -> AnyGenerator<Row> {
|
||||
public func fetchRows(sql: String, bindings: Bindings? = nil) -> AnySequence<Row> {
|
||||
return failOnError {
|
||||
let statement = try selectStatement(sql, bindings: bindings)
|
||||
return statement.fetchRowGenerator()
|
||||
return statement.fetchRows()
|
||||
}
|
||||
}
|
||||
|
||||
public func fetchRows(sql: String, bindings: Bindings? = nil) -> AnySequence<Row> {
|
||||
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<T: DatabaseValue>(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> AnyGenerator<T?> {
|
||||
public func fetch<T: DatabaseValue>(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> AnySequence<T?> {
|
||||
return failOnError {
|
||||
let statement = try selectStatement(sql, bindings: bindings)
|
||||
return statement.fetchGenerator(type)
|
||||
return statement.fetch(type)
|
||||
}
|
||||
}
|
||||
|
||||
public func fetch<T: DatabaseValue>(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> AnySequence<T?> {
|
||||
return AnySequence { self.fetchGenerator(type, sql, bindings: bindings) }
|
||||
}
|
||||
|
||||
public func fetchAll<T: DatabaseValue>(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> [T?] {
|
||||
return Array(fetch(type, sql, bindings: bindings))
|
||||
}
|
||||
|
||||
public func fetchOne<T: DatabaseValue>(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 {
|
||||
|
||||
+47
-48
@@ -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<Row> {
|
||||
public func fetchRows() -> AnySequence<Row> {
|
||||
// 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<Row> 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<Row> {
|
||||
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<T: DatabaseValue>(type: T.Type) -> AnyGenerator<T?> {
|
||||
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<T: DatabaseValue>(type: T.Type) -> AnySequence<T?> {
|
||||
let rowSequence = fetchRows()
|
||||
return AnySequence { () -> AnyGenerator<T?> 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<T: DatabaseValue>(type: T.Type) -> AnySequence<T?> {
|
||||
return AnySequence { self.fetchGenerator(type) }
|
||||
}
|
||||
|
||||
public func fetchAll<T: DatabaseValue>(type: T.Type) -> [T?] {
|
||||
return Array(fetch(type))
|
||||
}
|
||||
|
||||
public func fetchOne<T: DatabaseValue>(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 {
|
||||
|
||||
+12
-12
@@ -195,19 +195,19 @@ public class RowModel {
|
||||
}
|
||||
|
||||
extension Database {
|
||||
public func fetchGenerator<T: RowModel>(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> AnyGenerator<T> {
|
||||
let rowGenerator = fetchRowGenerator(sql, bindings: bindings)
|
||||
return anyGenerator {
|
||||
if let row = rowGenerator.next() {
|
||||
return T.init(row: row)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func fetch<T: RowModel>(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> AnySequence<T> {
|
||||
return AnySequence { self.fetchGenerator(type, sql, bindings: bindings) }
|
||||
let rowSequence = fetchRows(sql, bindings: bindings)
|
||||
return AnySequence { () -> AnyGenerator<T> 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<T: RowModel>(type: T.Type, _ sql: String, bindings: Bindings? = nil) -> [T] {
|
||||
@@ -215,7 +215,7 @@ extension Database {
|
||||
}
|
||||
|
||||
public func fetchOne<T: RowModel>(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 {
|
||||
|
||||
Reference in new issue
Block a user