From be0a2c01e1c35ef19eb830f89e174d1afbae5843 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 30 Jul 2021 13:55:59 +0200 Subject: [PATCH] More allStatements() documentation --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 795a5b36c..29c2a4ccc 100644 --- a/README.md +++ b/README.md @@ -1868,7 +1868,7 @@ 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: +**When you want to build multiple statements joined with a semicolon**, use the `allStatements` method: ```swift let statements = try db.allStatements(sql: """ @@ -1892,6 +1892,19 @@ while let statement = try statements.next() { } ``` +You can turn the [cursor](#cursors) returned from `allStatements` into a regular Swift array, but make sure all individual statements can compile even if the previous ones were not run: + +```swift +// OK +let statements = try Array(db.allStatements(sql: "SELECT ...; SELECT ...;")) +let statements = try Array(db.allStatements(sql: "INSERT ...; UPDATE ...;")) + +// Will fail since the insert statement won't compile until the table is created +let statements = try Array(db.allStatements(sql: "CREATE TABLE player ...; INSERT INTO player ...;")) +``` + +See also `Database.execute(sql:)` in the [Executing Updates](#executing-updates) chapter. + > :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).