From d153b5e91bc69729290b2fb1f2d302deb4fb2a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 23 Sep 2015 22:19:47 +0200 Subject: [PATCH] Documentation --- .../Protocols/DatabaseValueConvertible.swift | 24 ++++++------ GRDB/Core/Protocols/RowConvertible.swift | 38 +++---------------- GRDB/Core/Row.swift | 16 ++++---- GRDB/Core/Statement.swift | 3 +- TODO.md | 2 + 5 files changed, 29 insertions(+), 54 deletions(-) diff --git a/GRDB/Core/Protocols/DatabaseValueConvertible.swift b/GRDB/Core/Protocols/DatabaseValueConvertible.swift index 4e305129c..3c5de707d 100644 --- a/GRDB/Core/Protocols/DatabaseValueConvertible.swift +++ b/GRDB/Core/Protocols/DatabaseValueConvertible.swift @@ -4,8 +4,8 @@ Types that adopt DatabaseValueConvertible can be initialized from database values. -The protocol comes with built-in methods that allow to fetch lazy sequences, -arrays, or single instances: +The protocol comes with built-in methods that allow to fetch sequences, arrays, +or single instances: String.fetch(db, "SELECT name FROM ...", arguments:...) // DatabaseSequence String.fetchAll(db, "SELECT name FROM ...", arguments:...) // [String?] @@ -38,8 +38,8 @@ public protocol DatabaseValueConvertible { Types that adopt DatabaseValueConvertible can be initialized from database values. -The protocol comes with built-in methods that allow to fetch lazy sequences, -arrays, or single instances: +The protocol comes with built-in methods that allow to fetch sequences, arrays, +or single instances: String.fetch(db, "SELECT name FROM ...", arguments:...) // DatabaseSequence String.fetchAll(db, "SELECT name FROM ...", arguments:...) // [String?] @@ -58,7 +58,7 @@ public extension DatabaseValueConvertible { /** TODO - Fetches a lazy sequence of DatabaseValueConvertible values. + Fetches a sequence of DatabaseValueConvertible values. let statement = db.selectStatement("SELECT name FROM ...") let names = String.fetch(statement) // DatabaseSequence @@ -77,7 +77,7 @@ public extension DatabaseValueConvertible { - parameter statement: The statement to run. - parameter arguments: Optional statement arguments. - - returns: A lazy sequence of values. + - returns: A sequence of values. */ public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence { return statement.fetch(arguments: arguments) { @@ -120,7 +120,7 @@ public extension DatabaseValueConvertible { // MARK: - Fetching From Database /** - Fetches a lazy sequence of DatabaseValueConvertible values. + Fetches a sequence of DatabaseValueConvertible values. let names = String.fetch(db, "SELECT name FROM ...") // DatabaseSequence @@ -139,7 +139,7 @@ public extension DatabaseValueConvertible { - parameter db: A Database. - parameter sql: An SQL query. - parameter arguments: Optional statement arguments. - - returns: A lazy sequence of values. + - returns: A sequence of values. */ public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence { return fetch(db.selectStatement(sql), arguments: arguments) @@ -183,7 +183,7 @@ public extension DatabaseValueConvertible where Self: MetalType { /** TODO - Fetches a lazy sequence of DatabaseValueConvertible values. + Fetches a sequence of DatabaseValueConvertible values. let statement = db.selectStatement("SELECT name FROM ...") let names = String.fetch(statement) // DatabaseSequence @@ -202,7 +202,7 @@ public extension DatabaseValueConvertible where Self: MetalType { - parameter statement: The statement to run. - parameter arguments: Optional statement arguments. - - returns: A lazy sequence of values. + - returns: A sequence of values. */ public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence { return statement.fetch(arguments: arguments) { @@ -250,7 +250,7 @@ public extension DatabaseValueConvertible where Self: MetalType { // MARK: - Fetching From Database /** - Fetches a lazy sequence of DatabaseValueConvertible values. + Fetches a sequence of DatabaseValueConvertible values. let names = String.fetch(db, "SELECT name FROM ...") // DatabaseSequence @@ -269,7 +269,7 @@ public extension DatabaseValueConvertible where Self: MetalType { - parameter db: A Database. - parameter sql: An SQL query. - parameter arguments: Optional statement arguments. - - returns: A lazy sequence of values. + - returns: A sequence of values. */ public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence { return fetch(db.selectStatement(sql), arguments: arguments) diff --git a/GRDB/Core/Protocols/RowConvertible.swift b/GRDB/Core/Protocols/RowConvertible.swift index e32693e55..f831e68c3 100644 --- a/GRDB/Core/Protocols/RowConvertible.swift +++ b/GRDB/Core/Protocols/RowConvertible.swift @@ -4,8 +4,8 @@ Types that adopt RowConvertible can be initialized from a database Row. let row = Row.fetchOne(db, "SELECT ...")! let person = Person(row: row) -The protocol comes with built-in methods that allow to fetch lazy sequences, -arrays, or single instances: +The protocol comes with built-in methods that allow to fetch sequences, arrays, +or single instances: Person.fetch(db, "SELECT ...", arguments:...) // DatabaseSequence Person.fetchAll(db, "SELECT ...", arguments:...) // [Person] @@ -39,7 +39,7 @@ extension RowConvertible { /** TODO - Fetches a lazy sequence. + Fetches a sequence. let statement = db.selectStatement("SELECT * FROM persons") let persons = Person.fetch(statement) // DatabaseSequence @@ -58,7 +58,7 @@ extension RowConvertible { - parameter statement: The statement to run. - parameter arguments: Optional statement arguments. - - returns: A lazy sequence. + - returns: A sequence. */ public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence { // Metal rows can be reused. And reusing them yields better performance. @@ -105,7 +105,7 @@ extension RowConvertible { // MARK: - Fetching From Database /** - Fetches a lazy sequence. + Fetches a sequence. let persons = Person.fetch(db, "SELECT * FROM persons") // DatabaseSequence @@ -124,38 +124,12 @@ extension RowConvertible { - parameter db: A Database. - parameter sql: An SQL query. - parameter arguments: Optional statement arguments. - - returns: A lazy sequence. + - returns: A sequence. */ public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence { return fetch(db.selectStatement(sql), arguments: arguments) } -// /** -// Fetches a lazy sequence. -// -// let persons = Person.fetch(db, "SELECT * FROM persons") // DatabaseSequence -// -// The returned sequence can be consumed several times, but it may yield -// different results, should database changes have occurred between two -// generations: -// -// let persons = Person.fetch(db, "SELECT * FROM persons") -// Array(persons).count // 3 -// db.execute("DELETE ...") -// Array(persons).count // 2 -// -// If the database is modified while the sequence is iterating, the remaining -// elements are undefined. -// -// - parameter db: A Database. -// - parameter sql: An SQL query. -// - parameter arguments: Optional statement arguments. -// - returns: A lazy sequence. -// */ -// public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence { -// return fetch(db.selectStatement(sql), arguments: arguments) -// } - /** Fetches an array. diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 6cf74d74f..bef2f0753 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -330,7 +330,7 @@ public class Row: CollectionType { /** TODO - Returns a row generator that provides fast but unsafe access to database + Returns a row sequence that provides fast but unsafe access to database values: for row in MetalRow.fetch(db, "SELECT id, name FROM persons") { @@ -338,19 +338,19 @@ public class Row: CollectionType { let name = row.string(atIndex: 1) } - The returned generator is *unsafe* because it must be used with extra care, + The returned sequence is *unsafe* because it must be used with extra care, and GRDB.swift will not prevent invalid usage. - It MUST be iterated right away. Do not reserve it, do not wrap it in an Array. - Granted with those constraints, the unsafe generator grants extra speed for + Granted with those constraints, the unsafe sequence grants extra speed for the typed row accessors Row.int64(atIndex:), Row.string(atIndex:), etc. - parameter db: A Database. - parameter sql: An SQL query. - parameter arguments: Optional statement arguments. - - returns: A lazy sequence of rows. + - returns: A sequence of rows. */ public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence { // Metal rows can be reused. And reusing them yields better performance. @@ -398,7 +398,7 @@ public class Row: CollectionType { /** TODO - Returns a row generator that provides fast but unsafe access to database + Returns a row sequence that provides fast but unsafe access to database values: for row in Row.fetch(db, "SELECT id, name FROM persons") { @@ -406,19 +406,19 @@ public class Row: CollectionType { let name = row.string(atIndex: 1) } - The returned generator is *unsafe* because it must be used with extra care, + The returned sequence is *unsafe* because it must be used with extra care, and GRDB.swift will not prevent invalid usage. - It MUST be iterated right away. Do not reserve it, do not wrap it in an Array. - Granted with those constraints, the unsafe generator grants extra speed for + Granted with those constraints, the unsafe sequence grants extra speed for the typed row accessors Row.int64(atIndex:), Row.string(atIndex:), etc. - parameter db: A Database. - parameter sql: An SQL query. - parameter arguments: Optional statement arguments. - - returns: A lazy sequence of rows. + - returns: A sequence of rows. */ public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence { return fetch(db.selectStatement(sql), arguments: arguments) diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index 78769e593..fcd7d3e20 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -1,5 +1,4 @@ - -/// A nicer name than COpaquePointer for SQLite statement handle +/// A nicer name than COpaquePointer for SQLite statement handle (TODO: better documentation) public typealias SQLiteStatement = COpaquePointer private let SQLITE_TRANSIENT = unsafeBitCast(COpaquePointer(bitPattern: -1), sqlite3_destructor_type.self) diff --git a/TODO.md b/TODO.md index d15a4dffb..6cbc866b8 100644 --- a/TODO.md +++ b/TODO.md @@ -8,3 +8,5 @@ - [ ] Write test for how a fetched sequence should behave. It should contains the expected values. It can be restarted. Some of those are already written. - [ ] Update documentation when they metal rows are exposed, so that user is encouraged copying them. - [ ] Metal: How does the code look like when one iterates metal row and extracts NSData without copy? +- [ ] Metal: See what can be done about testRowSequenceCanBeIteratedIndependentlyFromSQLiteStatement() +- [ ] Turn DatabaseIntRepresentable and DatabaseStringRepresentable into MetalType