More allStatements() documentation

This commit is contained in:
Gwendal Roué committed 2021-07-30 13:55:59 +02:00
1 parent f232dfde27
commit be0a2c01e1
1 file changed
+14 -1
+14 -1
View File
@@ -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).