-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresDatabaseConnection.swift
More file actions
51 lines (45 loc) · 1.43 KB
/
PostgresDatabaseConnection.swift
File metadata and controls
51 lines (45 loc) · 1.43 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
//
// PostgresDatabaseConnection.swift
// feather-postgres-database
//
// Created by Tibor Bödecs on 2026. 01. 10.
//
import FeatherDatabase
import PostgresNIO
public struct PostgresDatabaseConnection: DatabaseConnection {
public typealias Query = PostgresDatabaseQuery
public typealias RowSequence = PostgresDatabaseRowSequence
var connection: PostgresConnection
public var logger: Logger
/// Execute a Postgres query on this connection.
///
/// This wraps `PostgresNIO` query execution and maps errors.
/// - Parameters:
/// - query: The Postgres query to execute.
/// - handler: A closure that receives the RowSequence result.
/// - Throws: A `DatabaseError` if the query fails.
/// - Returns: A query result containing the returned rows.
@discardableResult
public func run<T: Sendable>(
query: Query,
_ handler: (RowSequence) async throws -> T = { $0 }
) async throws(DatabaseError) -> T {
do {
let sequence = try await connection.query(
.init(
unsafeSQL: query.sql,
binds: query.bindings
),
logger: logger
)
return try await handler(
PostgresDatabaseRowSequence(
backingSequence: sequence
)
)
}
catch {
throw .query(error)
}
}
}