mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
More allStatements() documentation
This commit is contained in:
1 parent
6bbec1495d
commit
096eeffecf
2 files changed
+18
-10
No files matched your search
@@ -817,10 +817,10 @@ This `allStatements` method allows you to iterate all rows from multiple stateme
|
||||
// A Cursor of all rows from all statements
|
||||
let rows = try db
|
||||
.allStatements(sql: "...")
|
||||
.map { statement in try Row.fetchCursor(statement) }
|
||||
.joined()
|
||||
.flatMap { statement in try Row.fetchCursor(statement) }
|
||||
```
|
||||
|
||||
See [prepared statements](#prepared-statements) for more information about `allStatements()`.
|
||||
|
||||
### Cursors
|
||||
|
||||
@@ -1872,15 +1872,25 @@ When you want to build multiple statements joined with a semicolon, use the `all
|
||||
|
||||
```swift
|
||||
let statements = try db.allStatements(sql: """
|
||||
INSERT ...;
|
||||
SELECT ...;
|
||||
SELECT ...;
|
||||
""")
|
||||
INSERT INTO player (name, score) VALUES (?, ?);
|
||||
INSERT INTO player (name, score) VALUES (?, ?);
|
||||
""", arguments: ["Arthur", 100, "O'Brien", 1000])
|
||||
while let statement = try statements.next() {
|
||||
// Use statement
|
||||
try statement.execute()
|
||||
}
|
||||
```
|
||||
|
||||
`allStatements` also supports [SQL Interpolation]:
|
||||
|
||||
```swift
|
||||
let statements = try db.allStatements(literal: """
|
||||
INSERT INTO player (name, score) VALUES (\("Arthur"), \(100));
|
||||
INSERT INTO player (name, score) VALUES (\("O'Brien"), \(1000));
|
||||
""")
|
||||
while let statement = try statements.next() {
|
||||
try statement.execute()
|
||||
}
|
||||
```
|
||||
|
||||
> :point_up: **Note**: it is a programmer error to reuse a prepared statement that has failed: GRDB may crash if you do so.
|
||||
|
||||
|
||||
@@ -172,9 +172,7 @@ class SelectStatementTests : GRDBTestCase {
|
||||
SELECT age FROM persons ORDER BY age;
|
||||
SELECT age FROM persons ORDER BY age DESC;
|
||||
""")
|
||||
let ages = try Array(statements
|
||||
.map { try Int.fetchCursor($0) }
|
||||
.joined())
|
||||
let ages = try Array(statements.flatMap { try Int.fetchCursor($0) })
|
||||
XCTAssertEqual(ages, [13, 26, 41, 41, 26, 13])
|
||||
}
|
||||
do {
|
||||
|
||||
Reference in new issue
Block a user