Skip to content

Commit 0873474

Browse files
committed
DocC: SQL
1 parent bfe2450 commit 0873474

File tree

4 files changed

+78
-16
lines changed

4 files changed

+78
-16
lines changed

GRDB/Core/FetchRequest.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22

33
/// A type that fetches and decodes database rows.
44
///
5+
/// The main kinds of fetch requests are ``SQLRequest``
6+
/// and ``QueryInterfaceRequest``:
7+
///
8+
/// ```swift
9+
/// let lastName = "O'Reilly"
10+
///
11+
/// // SQLRequest
12+
/// let request: SQLRequest<Player> = """
13+
/// SELECT * FROM player WHERE lastName = \(lastName)
14+
/// """
15+
///
16+
/// // QueryInterfaceRequest
17+
/// let request = Player.filter(Column("lastName") == lastName)
18+
///
19+
/// // Use the request
20+
/// try dbQueue.read { db in
21+
/// let players = try request.fetchAll(db) // [Player]
22+
/// }
23+
/// ```
24+
///
525
/// ## Topics
626
///
727
/// ### Counting the Results

GRDB/Core/SQLRequest.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
/// without any risk of syntax errors or SQL injection:
55
///
66
/// ```swift
7-
/// extension Player {
7+
/// extension Player: FetchableRecord {
88
/// static func filter(name: String) -> SQLRequest<Player> {
99
/// "SELECT * FROM player WHERE name = \(name)"
1010
/// }
11+
///
12+
/// static func maximumScore() -> SQLRequest<Int> {
13+
/// "SELECT MAX(score) FROM player"
14+
/// }
1115
/// }
1216
///
1317
/// try dbQueue.read { db in
14-
/// let players = try Player.filter(name: "O'Brien").fetchAll(db)
18+
/// let players = try Player.filter(name: "O'Brien").fetchAll(db) // [Player]
19+
/// let maxScore = Player.maximumScore().fetchOne(db) // Int?
1520
/// }
1621
/// ```
1722
///

GRDB/Core/Statement.swift

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,47 @@ extension Statement {
505505

506506
// MARK: - Cursors
507507

508-
/// A cursor that iterates the result of a prepared ``Statement``.
508+
/// A cursor that lazily iterates the results of a prepared ``Statement``.
509+
///
510+
/// To get a `DatabaseCursor` instance, use a fetching method of
511+
/// ``Row``, ``DatabaseValueConvertible``, ``FetchableRecord``,
512+
/// or ``FetchRequest``. For example:
513+
///
514+
/// ```swift
515+
/// try dbQueue.read { db in
516+
/// // A cursor of database rows,
517+
/// // built from a prepared statement
518+
/// let statement = db.makeStatement(sql: "SELECT * FROM player")
519+
/// let rows = try Row.fetchCursor(statement)
520+
/// while let row = try rows.next() {
521+
/// let id: Int64 = row["id"]
522+
/// let name: String = row["name"]
523+
/// }
524+
///
525+
/// // A cursor of values,
526+
/// // built from an SQL string
527+
/// let scores = try Int.fetchCursor(db, sql: "SELECT score FROM player")
528+
/// while let score = try scores.next() {
529+
/// print(score)
530+
/// }
531+
///
532+
/// // A cursor of players,
533+
/// // built from a QueryInterfaceRequest:
534+
/// let request = Player.all()
535+
/// let players = try request.fetchCursor(db)
536+
/// while let player = try players.next() {
537+
/// print(player.name, player.score)
538+
/// }
539+
/// }
540+
/// ```
541+
///
542+
/// A database cursor is valid only during the current database access (read or
543+
/// write). Do not store or escape a cursor for later use.
544+
///
545+
/// A database cursor resets its underlying prepared statement with
546+
/// [`sqlite3_reset`](https://www.sqlite.org/c3ref/reset.html) when the cursor
547+
/// is created, and when it is deallocated. Don't share the same prepared
548+
/// statement between two cursors!
509549
public protocol DatabaseCursor: Cursor {
510550
/// Must be initialized to false.
511551
var _isDone: Bool { get set }

GRDB/Documentation.docc/SQLSupport.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,26 @@ SQL is the fundamental language for accessing SQLite databases.
44

55
## Topics
66

7-
### Prepared Statements
7+
### Fundamental Database Types
88

99
- ``Statement``
10-
11-
### Database Rows
12-
1310
- ``Row``
14-
15-
### Database Values
16-
17-
- ``DatabaseDateComponents``
1811
- ``DatabaseValue``
19-
- ``DatabaseValueConvertible``
20-
- ``StatementColumnConvertible``
12+
- ``DatabaseCursor``
2113

2214
### SQL Literals and Requests
2315

2416
- ``databaseQuestionMarks(count:)``
2517
- ``SQL``
2618
- ``SQLRequest``
27-
- ``FetchRequest``
2819

29-
### Cursors
20+
### Database Values
21+
22+
- ``DatabaseDateComponents``
23+
- ``DatabaseValueConvertible``
24+
- ``StatementColumnConvertible``
25+
26+
### Supporting Types
3027

3128
- ``Cursor``
32-
- ``DatabaseCursor``
29+
- ``FetchRequest``

0 commit comments

Comments
 (0)