From a60fe170de649e5a34732ad4e12931a8ea156d29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 30 Jul 2021 08:51:57 +0200 Subject: [PATCH] Document allStatements() --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index 00e349ac0..3e50a2508 100644 --- a/README.md +++ b/README.md @@ -798,6 +798,29 @@ try Row.fetchOne(...) // Row? let count = try Int.fetchOne(db, sql: "SELECT COUNT(*) ...") // Int? ``` +All those fetching methods require an SQL string that contains a single SQL statement. When you want to fetch from multiple statements joined with a semicolon, iterate the multiple [prepared statements](#prepared-statements) found in the SQL string: + +```swift +let statements = try db.allStatements(sql: """ + SELECT ...; + SELECT ...; + SELECT ...; + """) +while let statement = try statements.next() { + let players = try Player.fetchAll(statement) +} +``` + +This `allStatements` method allows you to iterate all rows from multiple statements, like the SQLite [`sqlite3_exec`](https://www.sqlite.org/c3ref/exec.html) function: + +```swift +// A Cursor of all rows from all statements +let rows = try db + .allStatements(sql: "...") + .map { statement in try Row.fetchCursor(statement) } + .joined() +``` + ### Cursors @@ -1845,6 +1868,20 @@ try insertStatement.execute(arguments: ["name": "Arthur", "score": 1000]) let player = try Player.fetchOne(selectStatement, arguments: ["Arthur"]) ``` +When you want to build multiple statements joined with a semicolon, use the `allStatements` method: + +```swift +let statements = try db.allStatements(sql: """ + INSERT ...; + SELECT ...; + SELECT ...; + """) +while let statement = try statements.next() { + // Use statement +} +``` + + > :point_up: **Note**: it is a programmer error to reuse a prepared statement that has failed: GRDB may crash if you do so. For more information about prepared statements, see the [Statement reference](http://groue.github.io/GRDB.swift/docs/5.8/Classes/Statement.html).