-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseRowSequencePostgres.swift
More file actions
75 lines (68 loc) · 2.33 KB
/
DatabaseRowSequencePostgres.swift
File metadata and controls
75 lines (68 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// DatabaseRowSequencePostgres.swift
// feather-database-postgres
//
// Created by Tibor Bödecs on 2026. 01. 10.
//
import FeatherDatabase
import PostgresNIO
/// A query result backed by a Postgres row sequence.
///
/// Use this type to iterate or collect Postgres query results.
public struct DatabaseRowSequencePostgres: DatabaseRowSequence {
var backingSequence: PostgresRowSequence
/// An async iterator over Postgres rows.
///
/// This iterator pulls rows from the backing sequence.
public struct AsyncIterator: AsyncIteratorProtocol {
var backingIterator: PostgresRowSequence.AsyncIterator
/// Return the next row in the sequence.
///
/// This stops when the sequence ends or the task is cancelled.
/// - Throws: An error if the underlying sequence fails.
/// - Returns: The next `PostgresRow`, or `nil` when finished.
#if compiler(>=6.2)
@concurrent
public mutating func next() async throws -> DatabaseRowPostgres? {
guard !Task.isCancelled else {
return nil
}
guard let postgresRow = try await backingIterator.next() else {
return nil
}
return .init(row: postgresRow)
}
#else
public mutating func next() async throws -> PostgresRow? {
guard !Task.isCancelled else {
return nil
}
guard let postgresRow = try await backingIterator.next() else {
return nil
}
return postgresRow
}
#endif
}
/// Create an async iterator over the result rows.
///
/// Use this to iterate the result as an `AsyncSequence`.
/// - Returns: An iterator over the result rows.
public func makeAsyncIterator() -> AsyncIterator {
.init(
backingIterator: backingSequence.makeAsyncIterator(),
)
}
/// Collect all rows into an array.
///
/// This consumes the sequence and returns all rows.
/// - Throws: An error if iteration fails.
/// - Returns: An array of `PostgresRow` values.
public func collect() async throws -> [DatabaseRowPostgres] {
var items: [DatabaseRowPostgres] = []
for try await item in self {
items.append(item)
}
return items
}
}