Documentation

This commit is contained in:
Gwendal Roué committed 2016-12-12 08:38:03 +01:00
1 parent 95af80acde
commit a68ed01003
2 files changed
+31 -26

No files matched your search

+9 -9
View File
@@ -1,8 +1,8 @@
/// The protocol for all types that define a way to fetch values from
/// a database.
///
/// Requests can feed the fetching methods of any fetchable type (row, value,
/// or record):
/// Requests can feed the fetching methods of any fetchable type (Row,
/// value, record):
///
/// let request: Request = ...
/// try Row.fetchCursor(db, request) // DatabaseCursor<Row>
@@ -17,13 +17,13 @@ public protocol Request {
extension Request {
/// Returns a TypedRequest bound to type T.
///
/// let request = Person
/// .select(min(heightColumn))
/// .bound(to: Double.self)
/// let minHeight = try request.fetchOne(db)
///
/// The returned request can fetch if the type T is fetchable (Row,
/// RowConvertible, DatabaseConvertible).
/// value, record).
///
/// let minHeight = Person
/// .select(min(heightColumn))
/// .bound(to: Double.self) // <--
/// .fetchOne(db)
///
/// - parameter type: The fetched type T
/// - returns: A typed request bound to type T.
@@ -101,7 +101,7 @@ public struct SQLRequest : Request {
/// a database, with an attached type.
///
/// Typed requests can fetch if their associated type Fetched is fetchable
/// (Row, RowConvertible, DatabaseConvertible)
/// (Row, value, record)
///
/// let request: ... // Some TypedRequest that fetches Person
/// try request.fetchCursor(db) // DatabaseCursor<Person>
+22 -17
View File
@@ -3080,13 +3080,13 @@ let arthur = try Person.filter(nameColumn == "Arthur").fetchOne(db) // Person?
**When the selected columns don't fit the source type**, change your target: any other type that adopts the [RowConvertible](#rowconvertible-protocol) protocol, plain [database rows](#fetching-rows), and even [values](#values):
```swift
let request = Person.select(min(heightColumn))
let minHeight = try Double.fetchOne(db, request) // Double?
let request = Player.select(max(scoreColumn))
let maxScore = try Int.fetchOne(db, request) // Int?
let request = Person.select(min(heightColumn), max(heightColumn))
let request = Player.select(min(scoreColumn), max(scoreColumn))
let row = try Row.fetchOne(db, request)!
let minHeight: Double? = row.value(atIndex: 0)
let maxHeight: Double? = row.value(atIndex: 1)
let minScore = row.value(atIndex: 0) as Int
let maxScore = row.value(atIndex: 1) as Int
```
@@ -3148,13 +3148,13 @@ let count = try Person.select(nameColumn, ageColumn).distinct().fetchCount(db)
**Other aggregated values** can also be selected and fetched (see [SQL Functions](#sql-functions)):
```swift
let request = Person.select(min(heightColumn))
let minHeight = try Double.fetchOne(db, request) // Double?
let request = Player.select(max(scoreColumn))
let maxScore = try Int.fetchOne(db, request) // Int?
let request = Person.select(min(heightColumn), max(heightColumn))
let request = Player.select(min(scoreColumn), max(scoreColumn))
let row = try Row.fetchOne(db, request)!
let minHeight: Double? = row.value(atIndex: 0)
let maxHeight: Double? = row.value(atIndex: 1)
let minScore = row.value(atIndex: 0) as Int
let maxScore = row.value(atIndex: 1) as Int
```
@@ -3280,7 +3280,16 @@ try request.fetchOne(db) // Person?
- [AnyRequest](http://cocoadocs.org/docsets/GRDB.swift/0.96.0/Structs/AnyRequest.html): a type-erased Request
- [AnyTypedRequest](http://cocoadocs.org/docsets/GRDB.swift/0.96.0/Structs/AnyTypedRequest.html): a type-erased TypedRequest
For example, let's fetch persons with an `SQLRequest` bound to the Person type:
Rebind the fetched type of requests:
```swift
let maxScore = Player // Int?
.select(max(scoreColumn))
.bound(to: Int.self)
.fetchOne(db)
```
Build `SQLRequest` bound to your record types:
```swift
extension Person {
@@ -3291,15 +3300,11 @@ extension Person {
return sqlRequest.bound(to: Person.self)
}
}
```
The `someCustomRequest` method can be used just like built-in requests from the [query interface](#requests):
```swift
try Person.someCustomRequest().fetchAll(db) // [Person]
```
Another example which demonstrates a possible use case for [row adapters](#row-adapters):
Use [row adapters](#row-adapters) to ease the consumption of complex rows:
```swift
struct BookAuthorPair : RowConvertible {
@@ -3318,7 +3323,7 @@ struct BookAuthorPair : RowConvertible {
"JOIN authors ON authors.id = books.authorID")
.bound(to: BookAuthorPair.self)
.adapted { db in
// Row scopes ease row consumption in init(row:)
// The scopes are used in init(row:)
try ScopeAdapter([
"book": SuffixRowAdapter(fromIndex: 0),
"author": SuffixRowAdapter(fromIndex: db.columnCount(in: "books"))])