mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Make Row a class, and introduce DatabaseSequence and DatabaseGenerator for better performance.
This commit is contained in:
1 parent
8b46d27377
commit
49c9636086
8 files changed
+122
-121
No files matched your search
@@ -68,17 +68,17 @@ Well, GRDB provides three fetching methods for each fetchable type:
|
||||
|
||||
```swift
|
||||
// Row
|
||||
Row.fetch(db, "SELECT ...", arguments: ...) // AnySequence<Row>
|
||||
Row.fetch(db, "SELECT ...", arguments: ...) // DatabaseSequence<Row>
|
||||
Row.fetchAll(db, "SELECT ...", arguments: ...) // [Row]
|
||||
Row.fetchOne(db, "SELECT ...", arguments: ...) // Row?
|
||||
|
||||
// DatabaseValueConvertible
|
||||
String.fetch(db, "SELECT ...", arguments: ...) // AnySequence<String?>
|
||||
String.fetch(db, "SELECT ...", arguments: ...) // DatabaseSequence<String?>
|
||||
String.fetchAll(db, "SELECT ...", arguments: ...) // [String?]
|
||||
String.fetchOne(db, "SELECT ...", arguments: ...) // String?
|
||||
|
||||
// Record (via RowConvertible)
|
||||
Person.fetch(db, "SELECT ...", arguments: ...) // AnySequence<Person>
|
||||
Person.fetch(db, "SELECT ...", arguments: ...) // DatabaseSequence<Person>
|
||||
Person.fetchAll(db, "SELECT ...", arguments: ...) // [Person]
|
||||
Person.fetchOne(db, "SELECT ...", arguments: ...) // Person?
|
||||
```
|
||||
|
||||
@@ -7,12 +7,12 @@ values.
|
||||
The protocol comes with built-in methods that allow to fetch lazy sequences,
|
||||
arrays, or single instances:
|
||||
|
||||
String.fetch(db, "SELECT name FROM ...", arguments:...) // AnySequence<String?>
|
||||
String.fetch(db, "SELECT name FROM ...", arguments:...) // DatabaseSequence<String?>
|
||||
String.fetchAll(db, "SELECT name FROM ...", arguments:...) // [String?]
|
||||
String.fetchOne(db, "SELECT name FROM ...", arguments:...) // String?
|
||||
|
||||
let statement = db.selectStatement("SELECT name FROM ...")
|
||||
String.fetch(statement, arguments:...) // AnySequence<String?>
|
||||
String.fetch(statement, arguments:...) // DatabaseSequence<String?>
|
||||
String.fetchAll(statement, arguments:...) // [String?]
|
||||
String.fetchOne(statement, arguments:...) // String?
|
||||
|
||||
@@ -41,12 +41,12 @@ values.
|
||||
The protocol comes with built-in methods that allow to fetch lazy sequences,
|
||||
arrays, or single instances:
|
||||
|
||||
String.fetch(db, "SELECT name FROM ...", arguments:...) // AnySequence<String?>
|
||||
String.fetch(db, "SELECT name FROM ...", arguments:...) // DatabaseSequence<String?>
|
||||
String.fetchAll(db, "SELECT name FROM ...", arguments:...) // [String?]
|
||||
String.fetchOne(db, "SELECT name FROM ...", arguments:...) // String?
|
||||
|
||||
let statement = db.selectStatement("SELECT name FROM ...")
|
||||
String.fetch(statement, arguments:...) // AnySequence<String?>
|
||||
String.fetch(statement, arguments:...) // DatabaseSequence<String?>
|
||||
String.fetchAll(statement, arguments:...) // [String?]
|
||||
String.fetchOne(statement, arguments:...) // String?
|
||||
|
||||
@@ -61,7 +61,7 @@ public extension DatabaseValueConvertible {
|
||||
Fetches a lazy sequence of DatabaseValueConvertible values.
|
||||
|
||||
let statement = db.selectStatement("SELECT name FROM ...")
|
||||
let names = String.fetch(statement) // AnySequence<String?>
|
||||
let names = String.fetch(statement) // DatabaseSequence<String?>
|
||||
|
||||
The returned sequence can be consumed several times, but it may yield
|
||||
different results, should database changes have occurred between two
|
||||
@@ -79,7 +79,7 @@ public extension DatabaseValueConvertible {
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A lazy sequence of values.
|
||||
*/
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> AnySequence<Self?> {
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self?> {
|
||||
return statement.fetch(arguments: arguments) {
|
||||
Self.fromDatabaseValue(statement.databaseValue(atIndex: 0))
|
||||
}
|
||||
@@ -89,7 +89,7 @@ public extension DatabaseValueConvertible {
|
||||
// Fetches a lazy sequence of DatabaseValueConvertible values.
|
||||
//
|
||||
// let statement = db.selectStatement("SELECT name FROM ...")
|
||||
// let names = String.fetch(statement) // AnySequence<String?>
|
||||
// let names = String.fetch(statement) // DatabaseSequence<String?>
|
||||
//
|
||||
// The returned sequence can be consumed several times, but it may yield
|
||||
// different results, should database changes have occurred between two
|
||||
@@ -107,7 +107,7 @@ public extension DatabaseValueConvertible {
|
||||
// - parameter arguments: Optional statement arguments.
|
||||
// - returns: A lazy sequence of values.
|
||||
// */
|
||||
// public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> AnySequence<Self?> {
|
||||
// public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self?> {
|
||||
// return AnySequence {
|
||||
// statement.generate(arguments: arguments) {
|
||||
// statement.databaseValue(atIndex: 0).value()
|
||||
@@ -152,7 +152,7 @@ public extension DatabaseValueConvertible {
|
||||
/**
|
||||
Fetches a lazy sequence of DatabaseValueConvertible values.
|
||||
|
||||
let names = String.fetch(db, "SELECT name FROM ...") // AnySequence<String?>
|
||||
let names = String.fetch(db, "SELECT name FROM ...") // DatabaseSequence<String?>
|
||||
|
||||
The returned sequence can be consumed several times, but it may yield
|
||||
different results, should database changes have occurred between two
|
||||
@@ -171,14 +171,14 @@ public extension DatabaseValueConvertible {
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A lazy sequence of values.
|
||||
*/
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> AnySequence<Self?> {
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Self?> {
|
||||
return fetch(db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
// /**
|
||||
// Fetches a lazy sequence of DatabaseValueConvertible values.
|
||||
//
|
||||
// let names = String.fetch(db, "SELECT name FROM ...") // AnySequence<String?>
|
||||
// let names = String.fetch(db, "SELECT name FROM ...") // DatabaseSequence<String?>
|
||||
//
|
||||
// The returned sequence can be consumed several times, but it may yield
|
||||
// different results, should database changes have occurred between two
|
||||
@@ -197,7 +197,7 @@ public extension DatabaseValueConvertible {
|
||||
// - parameter arguments: Optional statement arguments.
|
||||
// - returns: A lazy sequence of values.
|
||||
// */
|
||||
// public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> AnySequence<Self?> {
|
||||
// public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Self?> {
|
||||
// return self.fetch(db.selectStatement(sql), arguments: arguments)
|
||||
// }
|
||||
|
||||
@@ -236,7 +236,7 @@ public extension DatabaseValueConvertible where Self: MetalType {
|
||||
Fetches a lazy sequence of DatabaseValueConvertible values.
|
||||
|
||||
let statement = db.selectStatement("SELECT name FROM ...")
|
||||
let names = String.fetch(statement) // AnySequence<String?>
|
||||
let names = String.fetch(statement) // DatabaseSequence<String?>
|
||||
|
||||
The returned sequence can be consumed several times, but it may yield
|
||||
different results, should database changes have occurred between two
|
||||
@@ -254,7 +254,7 @@ public extension DatabaseValueConvertible where Self: MetalType {
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A lazy sequence of values.
|
||||
*/
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> AnySequence<Self?> {
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self?> {
|
||||
return statement.fetch(arguments: arguments) {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
if sqlite3_column_type(sqliteStatement, 0) == SQLITE_NULL {
|
||||
@@ -302,7 +302,7 @@ public extension DatabaseValueConvertible where Self: MetalType {
|
||||
/**
|
||||
Fetches a lazy sequence of DatabaseValueConvertible values.
|
||||
|
||||
let names = String.fetch(db, "SELECT name FROM ...") // AnySequence<String?>
|
||||
let names = String.fetch(db, "SELECT name FROM ...") // DatabaseSequence<String?>
|
||||
|
||||
The returned sequence can be consumed several times, but it may yield
|
||||
different results, should database changes have occurred between two
|
||||
@@ -321,7 +321,7 @@ public extension DatabaseValueConvertible where Self: MetalType {
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A lazy sequence of values.
|
||||
*/
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> AnySequence<Self?> {
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Self?> {
|
||||
return fetch(db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
|
||||
@@ -7,12 +7,12 @@ Types that adopt RowConvertible can be initialized from a database Row.
|
||||
The protocol comes with built-in methods that allow to fetch lazy sequences,
|
||||
arrays, or single instances:
|
||||
|
||||
Person.fetch(db, "SELECT ...", arguments:...) // AnySequence<Person>
|
||||
Person.fetch(db, "SELECT ...", arguments:...) // DatabaseSequence<Person>
|
||||
Person.fetchAll(db, "SELECT ...", arguments:...) // [Person]
|
||||
Person.fetchOne(db, "SELECT ...", arguments:...) // Person?
|
||||
|
||||
let statement = db.selectStatement("SELECT ...")
|
||||
Person.fetch(statement, arguments:...) // AnySequence<Person>
|
||||
Person.fetch(statement, arguments:...) // DatabaseSequence<Person>
|
||||
Person.fetchAll(statement, arguments:...) // [Person]
|
||||
Person.fetchOne(statement, arguments:...) // Person?
|
||||
|
||||
@@ -42,7 +42,7 @@ extension RowConvertible {
|
||||
Fetches a lazy sequence.
|
||||
|
||||
let statement = db.selectStatement("SELECT * FROM persons")
|
||||
let persons = Person.fetch(statement) // AnySequence<Person>
|
||||
let persons = Person.fetch(statement) // DatabaseSequence<Person>
|
||||
|
||||
The returned sequence can be consumed several times, but it may yield
|
||||
different results, should database changes have occurred between two
|
||||
@@ -60,9 +60,10 @@ extension RowConvertible {
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A lazy sequence.
|
||||
*/
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> AnySequence<Self> {
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
// Metal rows can be reused. And reusing them yields better performance.
|
||||
let row = Row(metalStatement: statement)
|
||||
return statement.fetch(arguments: arguments) {
|
||||
let row = Row(metalStatement: statement)
|
||||
let value = Self.init(row: row)
|
||||
value.awakeFromFetch(row)
|
||||
return value
|
||||
@@ -72,7 +73,7 @@ extension RowConvertible {
|
||||
// Fetches a lazy sequence.
|
||||
//
|
||||
// let statement = db.selectStatement("SELECT * FROM persons")
|
||||
// let persons = Person.fetch(statement) // AnySequence<Person>
|
||||
// let persons = Person.fetch(statement) // DatabaseSequence<Person>
|
||||
//
|
||||
// The returned sequence can be consumed several times, but it may yield
|
||||
// different results, should database changes have occurred between two
|
||||
@@ -90,7 +91,7 @@ extension RowConvertible {
|
||||
// - parameter arguments: Optional statement arguments.
|
||||
// - returns: A lazy sequence.
|
||||
// */
|
||||
// public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> AnySequence<Self> {
|
||||
// public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
// return AnySequence {
|
||||
// statement.generate(arguments: arguments) {
|
||||
// let row = Row(statement: statement)
|
||||
@@ -138,7 +139,7 @@ extension RowConvertible {
|
||||
/**
|
||||
Fetches a lazy sequence.
|
||||
|
||||
let persons = Person.fetch(db, "SELECT * FROM persons") // AnySequence<Person>
|
||||
let persons = Person.fetch(db, "SELECT * FROM persons") // DatabaseSequence<Person>
|
||||
|
||||
The returned sequence can be consumed several times, but it may yield
|
||||
different results, should database changes have occurred between two
|
||||
@@ -157,14 +158,14 @@ extension RowConvertible {
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A lazy sequence.
|
||||
*/
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> AnySequence<Self> {
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
return fetch(db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
// /**
|
||||
// Fetches a lazy sequence.
|
||||
//
|
||||
// let persons = Person.fetch(db, "SELECT * FROM persons") // AnySequence<Person>
|
||||
// let persons = Person.fetch(db, "SELECT * FROM persons") // DatabaseSequence<Person>
|
||||
//
|
||||
// The returned sequence can be consumed several times, but it may yield
|
||||
// different results, should database changes have occurred between two
|
||||
@@ -183,7 +184,7 @@ extension RowConvertible {
|
||||
// - parameter arguments: Optional statement arguments.
|
||||
// - returns: A lazy sequence.
|
||||
// */
|
||||
// public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> AnySequence<Self> {
|
||||
// public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
// return fetch(db.selectStatement(sql), arguments: arguments)
|
||||
// }
|
||||
|
||||
|
||||
+21
-15
@@ -1,8 +1,12 @@
|
||||
/**
|
||||
A row is the result of a database query.
|
||||
A database row.
|
||||
*/
|
||||
public struct Row: CollectionType {
|
||||
|
||||
public class Row: CollectionType {
|
||||
// IMPLEMENTATION NOTE:
|
||||
//
|
||||
// Row is a class so that we can reuse a single instance when iterating a
|
||||
// SelectStatement. This avoids a lot of Swift boilerplate, and improves
|
||||
// performances.
|
||||
|
||||
// MARK: - Building rows
|
||||
|
||||
@@ -16,6 +20,7 @@ public struct Row: CollectionType {
|
||||
for (key, value) in dictionary {
|
||||
databaseDictionary[key] = value?.databaseValue ?? .Null
|
||||
}
|
||||
self.sqliteStatement = nil
|
||||
self.impl = DictionaryRowImpl(databaseDictionary: databaseDictionary)
|
||||
}
|
||||
|
||||
@@ -100,7 +105,7 @@ public struct Row: CollectionType {
|
||||
}
|
||||
|
||||
public func value<Value: protocol<DatabaseValueConvertible, MetalType>>(atIndex index: Int) -> Value? {
|
||||
let sqliteStatement = impl.sqliteStatement
|
||||
let sqliteStatement = self.sqliteStatement // accessing it through impl takes 69% of the time spent here, and only 16% through self.
|
||||
guard sqliteStatement != nil else {
|
||||
return Value.fromDatabaseValue(impl.databaseValue(atIndex: index))
|
||||
}
|
||||
@@ -118,7 +123,7 @@ public struct Row: CollectionType {
|
||||
}
|
||||
|
||||
public func value<Value: protocol<DatabaseValueConvertible, MetalType>>(atIndex index: Int) -> Value {
|
||||
let sqliteStatement = impl.sqliteStatement
|
||||
let sqliteStatement = self.sqliteStatement
|
||||
guard sqliteStatement != nil else {
|
||||
return Value.fromDatabaseValue(impl.databaseValue(atIndex: index))!
|
||||
}
|
||||
@@ -310,10 +315,10 @@ public struct Row: CollectionType {
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A lazy sequence of rows.
|
||||
*/
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> AnySequence<Row> {
|
||||
return statement.fetch(arguments: arguments) {
|
||||
Row(metalStatement: statement)
|
||||
}
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Row> {
|
||||
// Metal rows can be reused. And reusing them yields better performance.
|
||||
let row = Row(metalStatement: statement)
|
||||
return statement.fetch(arguments: arguments) { row }
|
||||
}
|
||||
|
||||
// /**
|
||||
@@ -338,7 +343,7 @@ public struct Row: CollectionType {
|
||||
// - parameter arguments: Optional statement arguments.
|
||||
// - returns: A lazy sequence of rows.
|
||||
// */
|
||||
// public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> AnySequence<Row> {
|
||||
// public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Row> {
|
||||
// return AnySequence {
|
||||
// statement.generate(arguments: arguments) {
|
||||
// Row(statement: statement)
|
||||
@@ -406,7 +411,7 @@ public struct Row: CollectionType {
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A lazy sequence of rows.
|
||||
*/
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> AnySequence<Row> {
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Row> {
|
||||
return fetch(db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
@@ -432,7 +437,7 @@ public struct Row: CollectionType {
|
||||
// - parameter arguments: Optional statement arguments.
|
||||
// - returns: A lazy sequence of rows.
|
||||
// */
|
||||
// public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> AnySequence<Row> {
|
||||
// public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Row> {
|
||||
// return fetch(db.selectStatement(sql), arguments: arguments)
|
||||
// }
|
||||
|
||||
@@ -468,6 +473,7 @@ public struct Row: CollectionType {
|
||||
// MARK: - Not Public
|
||||
|
||||
let impl: RowImpl
|
||||
let sqliteStatement: SQLiteStatement // IMPLEMENTATION NOTE: Making sqliteStatement a row property instead of a RowImpl property makes the extraction of MetalType values faster.
|
||||
|
||||
/**
|
||||
Builds a row from the *current state* of the SQLite statement.
|
||||
@@ -477,14 +483,17 @@ public struct Row: CollectionType {
|
||||
corrupting the row.
|
||||
*/
|
||||
init(detachedStatement statement: SelectStatement) {
|
||||
self.sqliteStatement = nil
|
||||
self.impl = DetachedRowImpl(statement: statement)
|
||||
}
|
||||
|
||||
init(metalStatement statement: SelectStatement) {
|
||||
self.sqliteStatement = statement.sqliteStatement
|
||||
self.impl = MetalRowImpl(statement: statement)
|
||||
}
|
||||
|
||||
init(impl: RowImpl) {
|
||||
self.sqliteStatement = nil
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
@@ -494,7 +503,6 @@ public struct Row: CollectionType {
|
||||
/// See Row.init(databaseDictionary:)
|
||||
private struct DictionaryRowImpl : RowImpl {
|
||||
let databaseDictionary: [String: DatabaseValue]
|
||||
var sqliteStatement: SQLiteStatement { return nil }
|
||||
|
||||
init (databaseDictionary: [String: DatabaseValue]) {
|
||||
self.databaseDictionary = databaseDictionary
|
||||
@@ -534,7 +542,6 @@ public struct Row: CollectionType {
|
||||
private struct DetachedRowImpl : RowImpl {
|
||||
let databaseValues: [DatabaseValue]
|
||||
let columnNames: [String]
|
||||
var sqliteStatement: SQLiteStatement { return nil }
|
||||
|
||||
init(statement: SelectStatement) {
|
||||
self.databaseValues = (0..<statement.columnCount).map { statement.databaseValue(atIndex: $0) }
|
||||
@@ -628,7 +635,6 @@ extension Row: CustomStringConvertible {
|
||||
// The protocol for Row underlying implementation
|
||||
protocol RowImpl {
|
||||
var count: Int { get }
|
||||
var sqliteStatement: SQLiteStatement { get }
|
||||
func databaseValue(atIndex index: Int) -> DatabaseValue
|
||||
func columnName(atIndex index: Int) -> String
|
||||
func indexForColumn(named name: String) -> Int? // This method MUST be case-insensitive.
|
||||
|
||||
@@ -26,46 +26,16 @@ public final class SelectStatement : Statement {
|
||||
// MARK: - Not public
|
||||
|
||||
/**
|
||||
Returns the DatabaseValue 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.
|
||||
|
||||
// Rows is an Array: all rows are loaded, which means that statement has
|
||||
// been fully iterated and consumed, and any SQLite casting opportunity
|
||||
// has passed.
|
||||
let rows = Row.fetchAll(db, "SELECT ...")
|
||||
|
||||
for row in rows {
|
||||
let itemCount: Int? = row.value(atIndex:0) // GRDB conversion to Int
|
||||
let hasItems: Bool? = row.value(atIndex:0) // GRDB conversion to Bool
|
||||
}
|
||||
TODO
|
||||
*/
|
||||
func databaseValue(atIndex index: Int) -> DatabaseValue {
|
||||
return DatabaseValue(sqliteStatement: sqliteStatement, index: index)
|
||||
}
|
||||
|
||||
/**
|
||||
Builds a generator from a SelectStatement.
|
||||
|
||||
let statement = db.selectStatement("SELECT ...")
|
||||
|
||||
// AnyGenerator<Row>
|
||||
let rowGenerator = statement.generate() { Row(statement: statement) }
|
||||
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- parameter transform: A function that maps the statement to the desired
|
||||
sequence element. SQLite statements are stateful: at the moment the
|
||||
*read* function is called, the statement has just read a row.
|
||||
- returns: A lazy sequence.
|
||||
TODO
|
||||
*/
|
||||
func fetch<T>(arguments arguments: StatementArguments?, read: () -> T) -> AnySequence<T> {
|
||||
func fetch<T>(arguments arguments: StatementArguments?, map: () -> T) -> DatabaseSequence<T> {
|
||||
if let arguments = arguments {
|
||||
self.arguments = arguments
|
||||
}
|
||||
@@ -77,24 +47,48 @@ public final class SelectStatement : Statement {
|
||||
// Check that sequence is built on a valid database.
|
||||
// See DatabaseQueue.inSafeDatabase().
|
||||
database.assertValid()
|
||||
|
||||
return DatabaseSequence(statement: self, map: map)
|
||||
}
|
||||
}
|
||||
|
||||
return AnySequence { () -> AnyGenerator<T> in
|
||||
|
||||
// Restart
|
||||
self.reset()
|
||||
|
||||
return anyGenerator { () -> T? in
|
||||
|
||||
let code = sqlite3_step(self.sqliteStatement)
|
||||
switch code {
|
||||
case SQLITE_DONE:
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
return read()
|
||||
default:
|
||||
fatalError(DatabaseError(code: code, message: self.database.lastErrorMessage, sql: self.sql, arguments: self.arguments).description)
|
||||
}
|
||||
}
|
||||
/**
|
||||
TODO
|
||||
*/
|
||||
public struct DatabaseSequence<T>: SequenceType {
|
||||
let statement: SelectStatement
|
||||
let map: () -> T
|
||||
public func generate() -> DatabaseGenerator<T> {
|
||||
// DatabaseSequence can be restarted:
|
||||
statement.reset()
|
||||
|
||||
return DatabaseGenerator(statement: statement, map: map)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
TODO
|
||||
*/
|
||||
public struct DatabaseGenerator<T>: GeneratorType {
|
||||
let statement: SelectStatement
|
||||
let sqliteStatement: SQLiteStatement
|
||||
let map: () -> T
|
||||
|
||||
init(statement: SelectStatement, map: () -> T) {
|
||||
self.statement = statement
|
||||
self.sqliteStatement = statement.sqliteStatement
|
||||
self.map = map
|
||||
}
|
||||
|
||||
public func next() -> T? {
|
||||
let code = sqlite3_step(sqliteStatement)
|
||||
switch code {
|
||||
case SQLITE_DONE:
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
return map()
|
||||
default:
|
||||
fatalError(DatabaseError(code: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments).description)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ Usage:
|
||||
let date = row.value(named: "date") as NSDate?
|
||||
|
||||
// Direct fetch:
|
||||
NSDate.fetch(db, "SELECT ...", arguments: ...) // AnySequence<NSDate?>
|
||||
NSDate.fetch(db, "SELECT ...", arguments: ...) // DatabaseSequence<NSDate?>
|
||||
NSDate.fetchAll(db, "SELECT ...", arguments: ...) // [NSDate?]
|
||||
NSDate.fetchOne(db, "SELECT ...", arguments: ...) // NSDate?
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabaseStorable {
|
||||
// IMPLEMENTATION NOTE
|
||||
//
|
||||
// Swift requires a required initializer so that we can fetch Records
|
||||
// in SelectStatement.fetch<Record: GRDB.Record>(type: Record.Type, arguments: StatementArguments? = nil) -> AnySequence<Record>
|
||||
// in SelectStatement.fetch<Record: GRDB.Record>(type: Record.Type, arguments: StatementArguments? = nil) -> DatabaseSequence<Record>
|
||||
//
|
||||
// This required initializer *can not* be the simple init(), because it
|
||||
// would prevent subclasses to provide handy initializers made of
|
||||
|
||||
@@ -53,7 +53,7 @@ let redWinesCount = dbQueue.inDatabase { db in // Int
|
||||
|
||||
dbQueue.inDatabase { db in
|
||||
let wines = Wine.fetchAll(db, "SELECT ...") // [Wine]
|
||||
for wine in Wine.fetch(db, "SELECT ...") { // AnySequence<Wine>
|
||||
for wine in Wine.fetch(db, "SELECT ...") { // DatabaseSequence<Wine>
|
||||
...
|
||||
}
|
||||
}
|
||||
@@ -209,19 +209,19 @@ You can fetch **Rows**, **Values**, and **Records**:
|
||||
|
||||
```swift
|
||||
dbQueue.inDatabase { db in
|
||||
Row.fetch(db, "SELECT ...", arguments: ...) // AnySequence<Row>
|
||||
Row.fetchAll(db, "SELECT ...", arguments: ...) // [Row]
|
||||
Row.fetchOne(db, "SELECT ...", arguments: ...) // Row?
|
||||
|
||||
String.fetch(db, "SELECT ...", arguments: ...) // AnySequence<String?>
|
||||
String.fetchAll(db, "SELECT ...", arguments: ...) // [String?]
|
||||
String.fetchOne(db, "SELECT ...", arguments: ...) // String?
|
||||
|
||||
Person.fetch(db, "SELECT ...", arguments: ...) // AnySequence<Person>
|
||||
Person.fetchAll(db, "SELECT ...", arguments: ...) // [Person]
|
||||
Person.fetchOne(db, "SELECT ...", arguments: ...) // Person?
|
||||
Person.fetchOne(db, primaryKey: 12) // Person?
|
||||
Person.fetchOne(db, key: ["name": "Arthur"]) // Person?
|
||||
Row.fetch(db, "SELECT ...", arguments: ...) // DatabaseSequence<Row>
|
||||
Row.fetchAll(db, "SELECT ...", arguments: ...) // [Row]
|
||||
Row.fetchOne(db, "SELECT ...", arguments: ...) // Row?
|
||||
|
||||
String.fetch(db, "SELECT ...", arguments: ...) // DatabaseSequence<String?>
|
||||
String.fetchAll(db, "SELECT ...", arguments: ...) // [String?]
|
||||
String.fetchOne(db, "SELECT ...", arguments: ...) // String?
|
||||
|
||||
Person.fetch(db, "SELECT ...", arguments: ...) // DatabaseSequence<Person>
|
||||
Person.fetchAll(db, "SELECT ...", arguments: ...) // [Person]
|
||||
Person.fetchOne(db, "SELECT ...", arguments: ...) // Person?
|
||||
Person.fetchOne(db, primaryKey: 12) // Person?
|
||||
Person.fetchOne(db, key: ["name": "Arthur"]) // Person?
|
||||
}
|
||||
```
|
||||
|
||||
@@ -238,7 +238,7 @@ Fetch **lazy sequences** of rows, **arrays**, or a **single** row:
|
||||
|
||||
```swift
|
||||
dbQueue.inDatabase { db in
|
||||
Row.fetch(db, "SELECT ...", arguments: ...) // AnySequence<Row>
|
||||
Row.fetch(db, "SELECT ...", arguments: ...) // DatabaseSequence<Row>
|
||||
Row.fetchAll(db, "SELECT ...", arguments: ...) // [Row]
|
||||
Row.fetchOne(db, "SELECT ...", arguments: ...) // Row?
|
||||
}
|
||||
@@ -335,7 +335,7 @@ Like rows, values can be fetched as **lazy sequences**, **arrays**, or **single*
|
||||
|
||||
```swift
|
||||
dbQueue.inDatabase { db in
|
||||
Int.fetch(db, "SELECT ...", arguments: ...) // AnySequence<Int?>
|
||||
Int.fetch(db, "SELECT ...", arguments: ...) // DatabaseSequence<Int?>
|
||||
Int.fetchAll(db, "SELECT ...", arguments: ...) // [Int?]
|
||||
Int.fetchOne(db, "SELECT ...", arguments: ...) // Int?
|
||||
}
|
||||
@@ -429,7 +429,7 @@ Extract NSDate from the database:
|
||||
let row = Row.fetchOne(db, "SELECT birthDate, ...")!
|
||||
let date = row.value(named: "birthDate") as NSDate?
|
||||
|
||||
NSDate.fetch(db, "SELECT ...") // AnySequence<NSDate?>
|
||||
NSDate.fetch(db, "SELECT ...") // DatabaseSequence<NSDate?>
|
||||
NSDate.fetchAll(db, "SELECT ...") // [NSDate?]
|
||||
NSDate.fetchOne(db, "SELECT ...") // NSDate?
|
||||
```
|
||||
@@ -483,7 +483,7 @@ let dbComponents = row.value(named: "birthDate")! as DatabaseDateComponents
|
||||
dbComponents.format // .YMD (the actual format found in the database)
|
||||
dbComponents.dateComponents // NSDateComponents
|
||||
|
||||
DatabaseDateComponents.fetch(db, "SELECT ...") // AnySequence<DatabaseDateComponents?>
|
||||
DatabaseDateComponents.fetch(db, "SELECT ...") // DatabaseSequence<DatabaseDateComponents?>
|
||||
DatabaseDateComponents.fetchAll(db, "SELECT ...") // [DatabaseDateComponents?]
|
||||
DatabaseDateComponents.fetchOne(db, "SELECT ...") // DatabaseDateComponents?
|
||||
```
|
||||
@@ -554,7 +554,7 @@ for rows in Row.fetch(db, "SELECT * FROM wines") {
|
||||
}
|
||||
|
||||
// Direct fetch:
|
||||
Color.fetch(db, "SELECT ...", arguments: ...) // AnySequence<Color?>
|
||||
Color.fetch(db, "SELECT ...", arguments: ...) // DatabaseSequence<Color?>
|
||||
Color.fetchAll(db, "SELECT ...", arguments: ...) // [Color?]
|
||||
Color.fetchOne(db, "SELECT ...", arguments: ...) // Color?
|
||||
```
|
||||
@@ -639,7 +639,7 @@ for rows in Row.fetch(db, "SELECT ...") {
|
||||
}
|
||||
|
||||
// Direct fetch:
|
||||
DatabaseTimestamp.fetch(db, "SELECT ...") // AnySequence<DatabaseTimestamp?>
|
||||
DatabaseTimestamp.fetch(db, "SELECT ...") // DatabaseSequence<DatabaseTimestamp?>
|
||||
DatabaseTimestamp.fetchAll(db, "SELECT ...") // [DatabaseTimestamp?]
|
||||
DatabaseTimestamp.fetchOne(db, "SELECT ...") // DatabaseTimestamp?
|
||||
```
|
||||
@@ -719,15 +719,15 @@ dbQueue.inDatabase { db in
|
||||
|
||||
let statement = db.selectStatement("SELECT ...")
|
||||
|
||||
Row.fetch(statement, arguments: ...) // AnySequence<Row>
|
||||
Row.fetch(statement, arguments: ...) // DatabaseSequence<Row>
|
||||
Row.fetchAll(statement, arguments: ...) // [Row]
|
||||
Row.fetchOne(statement, arguments: ...) // Row?
|
||||
|
||||
Int.fetch(statement, arguments: ...) // AnySequence<Int?>
|
||||
Int.fetch(statement, arguments: ...) // DatabaseSequence<Int?>
|
||||
Int.fetchAll(statement, arguments: ...) // [Int?]
|
||||
Int.fetchOne(statement, arguments: ...) // Int?
|
||||
|
||||
Person.fetch(statement, arguments: ...) // AnySequence<Person>
|
||||
Person.fetch(statement, arguments: ...) // DatabaseSequence<Person>
|
||||
Person.fetchAll(statement, arguments: ...) // [Person]
|
||||
Person.fetchOne(statement, arguments: ...) // Person?
|
||||
}
|
||||
@@ -947,7 +947,7 @@ Now you can fetch **lazy sequences** of records, **arrays**, or **single** insta
|
||||
```swift
|
||||
|
||||
dbQueue.inDatabase { db in
|
||||
Person.fetch(db, "SELECT ...", arguments:...) // AnySequence<Person>
|
||||
Person.fetch(db, "SELECT ...", arguments:...) // DatabaseSequence<Person>
|
||||
Person.fetchAll(db, "SELECT ...", arguments:...) // [Person]
|
||||
Person.fetchOne(db, "SELECT ...", arguments:...) // Person?
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user