From 82d953472e86873cdb5c486db2d7c10db8bf419d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 5 Jul 2015 12:31:50 +0200 Subject: [PATCH] Documentation WIP --- GRDB/Database.swift | 8 +-- GRDB/SelectStatement.swift | 99 ++++++++++++++++++++++++++++++++------ GRDB/Statement.swift | 6 +-- 3 files changed, 92 insertions(+), 21 deletions(-) diff --git a/GRDB/Database.swift b/GRDB/Database.swift index f8cbd8221..5de9b5f47 100644 --- a/GRDB/Database.swift +++ b/GRDB/Database.swift @@ -230,11 +230,11 @@ public final class Database { /** Convenience function that calls fatalError in case of error - let x = failOnError { + let x = verboseFailOnError { ... } */ -func failOnError(@noescape block: (Void) throws -> Result) -> Result { +func verboseFailOnError(@noescape block: (Void) throws -> Result) -> Result { do { return try block() } catch let error as SQLiteError { @@ -263,7 +263,7 @@ extension Database { - returns: A lazy sequence of rows. */ public func fetchRows(sql: String, bindings: Bindings? = nil) -> AnySequence { - return failOnError { + return verboseFailOnError { let statement = try selectStatement(sql, bindings: bindings) return statement.fetchRows() } @@ -318,7 +318,7 @@ extension Database { - returns: A lazy sequence of values. */ public func fetch(type: Value.Type, _ sql: String, bindings: Bindings? = nil) -> AnySequence { - return failOnError { + return verboseFailOnError { let statement = try selectStatement(sql, bindings: bindings) return statement.fetch(type) } diff --git a/GRDB/SelectStatement.swift b/GRDB/SelectStatement.swift index 7803470d0..818e3646f 100644 --- a/GRDB/SelectStatement.swift +++ b/GRDB/SelectStatement.swift @@ -24,19 +24,57 @@ import Foundation +/// A subclass of Statement that implements SELECT statements. public final class SelectStatement : Statement { - let unsafe: Bool + + /// The number of columns in the results. public lazy var columnCount: Int = Int(sqlite3_column_count(self.sqliteStatement)) + + /// The names of columns, ordered from left to right. public lazy var columnNames: [String] = (0.. SQLiteValue { switch sqlite3_column_type(sqliteStatement, Int32(index)) { case SQLITE_NULL: @@ -59,35 +97,50 @@ public final class SelectStatement : Statement { } } +/** +The SelectStatement methods that fetch rows. +*/ extension SelectStatement { - // let rows = statement.fetchRows() + /** + Fetches a lazy sequence of rows. + + let statement = try db.selectStatement("SELECT ...") + let rows = statement.fetchRows() + + - parameter bindings: Optional bindings for query parameters. + - returns: A lazy sequence of rows. + */ public func fetchRows(bindings bindings: Bindings? = nil) -> AnySequence { if let bindings = bindings { self.bindings = bindings } + + if let trace = self.database.configuration.trace { + trace(sql: self.sql, bindings: self.bindings) + } + return AnySequence { () -> AnyGenerator in - var trace = self.database.configuration.trace return anyGenerator { () -> Row? in - // Make sure values are not consumed in a different queue. + // Make sure values are consumed in the correct queue. // // Here we avoid this pattern: // // let rows = dbQueue.inDatabase { db in // try db.fetchRows("...") // } - // for row in rows { // fatal error + // for row in rows { // fatal error! // ... // } + // + // Check that the statement was created in a database queue, + // and then that the current database queue is the same as the + // one where the statement was created: 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 let appliedTrace = trace { - appliedTrace(sql: self.sql, bindings: self.bindings) - trace = nil - } let code = sqlite3_step(self.sqliteStatement) switch code { case SQLITE_DONE: @@ -95,7 +148,7 @@ extension SelectStatement { case SQLITE_ROW: return Row(statement: self, unsafe: self.unsafe) default: - failOnError { () -> Void in + verboseFailOnError { () -> Void in throw SQLiteError(code: code, sqliteConnection: self.database.sqliteConnection, sql: self.sql) } return nil @@ -104,12 +157,30 @@ extension SelectStatement { } } - // let rows = statement.fetchAllRows() + /** + Fetches an array of rows. + + let statement = try db.selectStatement("SELECT ...") + let rows = statement.fetchAllRows() + + - parameter bindings: Optional bindings for query parameters. + - returns: An array of rows. + */ public func fetchAllRows(bindings bindings: Bindings? = nil) -> [Row] { return Array(fetchRows(bindings: bindings)) } - // let row = statement.fetchOneRow() + /** + Fetches a single row. + + let statement = try db.selectStatement("SELECT ...") + let row = statement.fetchOneRow() + + - parameter sql: An SQL query. + - parameter bindings: Optional bindings for query parameters. + + - returns: An optional row. + */ public func fetchOneRow(bindings bindings: Bindings? = nil) -> Row? { return fetchRows(bindings: bindings).generate().next() } diff --git a/GRDB/Statement.swift b/GRDB/Statement.swift index 23b451500..6bbc49c08 100644 --- a/GRDB/Statement.swift +++ b/GRDB/Statement.swift @@ -80,7 +80,7 @@ public class Statement { } if code != SQLITE_OK { - failOnError { () -> Void in + verboseFailOnError { () -> Void in throw SQLiteError(code: code, sqliteConnection: self.database.sqliteConnection, sql: self.sql) } } @@ -101,7 +101,7 @@ public class Statement { final func reset() { let code = sqlite3_reset(sqliteStatement) if code != SQLITE_OK { - failOnError { () -> Void in + verboseFailOnError { () -> Void in throw SQLiteError(code: code, sqliteConnection: self.database.sqliteConnection, sql: self.sql) } } @@ -111,7 +111,7 @@ public class Statement { private func clearBindings() { let code = sqlite3_clear_bindings(sqliteStatement) if code != SQLITE_OK { - failOnError { () -> Void in + verboseFailOnError { () -> Void in throw SQLiteError(code: code, sqliteConnection: self.database.sqliteConnection, sql: self.sql) } }