Documentation WIP

This commit is contained in:
Gwendal Roué committed 2015-07-05 12:31:50 +02:00
1 parent f4ad456569
commit 82d953472e
3 files changed
+92 -21

No files matched your search

+4 -4
View File
@@ -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<Result>(@noescape block: (Void) throws -> Result) -> Result {
func verboseFailOnError<Result>(@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<Row> {
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<Value: SQLiteValueConvertible>(type: Value.Type, _ sql: String, bindings: Bindings? = nil) -> AnySequence<Value?> {
return failOnError {
return verboseFailOnError {
let statement = try selectStatement(sql, bindings: bindings)
return statement.fetch(type)
}
+85 -14
View File
@@ -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..<self.columnCount).map { index in
return String.fromCString(sqlite3_column_name(self.sqliteStatement, Int32(index)))!
}
// MARK: - Not public
/**
If true, the fetched rows are *unsafe*.
See Row(statement:unsafe:) for details.
*/
let unsafe: Bool
init(database: Database, sql: String, bindings: Bindings?, unsafe: Bool) throws {
self.unsafe = unsafe
try super.init(database: database, sql: sql, bindings: bindings)
// Make sure the statement is created in a database queue:
assert(databaseQueueID != nil)
}
/**
Returns the SQLiteValue at given index.
It is the *only* method which loads data straight from SQLite.
We preserve the *raw storage class* of database values, and do not use the
SQLite built-in casting between types.
This is *by design*, because a GRDB user generally consumes database values
long after the opportunity to use SQLite casting has passed, which is during
the statement consumption.
// All rows are loaded, which means that statement has been fully
// consumed, and any SQLite casting opportunity has passed.
let rows = db.fetchAllRows("SELECT ...")
for row in rows {
let age: Int = row.value(atIndex:0) // the conversion actually happens in GRDB.
}
The only known caveat (so far) of snubbing SQLite built-in casting is the
String to Bool conversion. See Row.value(atIndex:) for more information.
*/
func sqliteValue(atIndex index: Int) -> 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<Row> {
if let bindings = bindings {
self.bindings = bindings
}
if let trace = self.database.configuration.trace {
trace(sql: self.sql, bindings: self.bindings)
}
return AnySequence { () -> AnyGenerator<Row> 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()
}
+3 -3
View File
@@ -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)
}
}