|
9 | 9 | /// directly mapped to a default SQLite connection like `SQLiteConnection` |
10 | 10 | /// but is a much more high level of abstraction that allows for safe |
11 | 11 | /// communication to a database. |
12 | | -public protocol Connection: Actor { |
| 12 | +public protocol Connection: Sendable { |
13 | 13 | /// Starts observation for the given subscriber |
14 | | - nonisolated func observe(subscriber: DatabaseSubscriber) |
| 14 | + func observe(subscriber: DatabaseSubscriber) |
15 | 15 |
|
16 | 16 | /// Cancels the observation for the given subscriber |
17 | | - nonisolated func cancel(subscriber: DatabaseSubscriber) |
| 17 | + func cancel(subscriber: DatabaseSubscriber) |
18 | 18 |
|
19 | 19 | func begin<Output>( |
20 | 20 | _ kind: Transaction.Kind, |
21 | | - execute: (borrowing Transaction) throws -> Output |
| 21 | + execute: @Sendable (borrowing Transaction) throws -> Output |
22 | 22 | ) async throws -> Output |
23 | 23 | } |
24 | 24 |
|
25 | | -actor NoopConnection: Connection { |
26 | | - nonisolated func observe(subscriber: any DatabaseSubscriber) {} |
27 | | - nonisolated func cancel(subscriber: any DatabaseSubscriber) {} |
| 25 | +/// A no operation database connection that does nothing. |
| 26 | +struct NoopConnection: Connection { |
| 27 | + func observe(subscriber: any DatabaseSubscriber) {} |
| 28 | + func cancel(subscriber: any DatabaseSubscriber) {} |
28 | 29 |
|
29 | 30 | func begin<Output>( |
30 | 31 | _ kind: Transaction.Kind, |
31 | | - execute: (borrowing Transaction) throws -> Output |
| 32 | + execute: @Sendable (borrowing Transaction) throws -> Output |
32 | 33 | ) async throws -> Output { |
33 | 34 | try execute(Transaction(connection: NoopRawConnection(), kind: kind)) |
34 | 35 | } |
35 | 36 | } |
| 37 | + |
| 38 | +/// A type that has a database connection. |
| 39 | +/// Useful for the queries structures. |
| 40 | +/// |
| 41 | +/// Note: This should not be explicitly used and is |
| 42 | +/// intended only for the codegen. |
| 43 | +public protocol ConnectionWrapper: Connection { |
| 44 | + var connection: any Connection { get } |
| 45 | +} |
| 46 | + |
| 47 | +extension ConnectionWrapper { |
| 48 | + func observe(subscriber: DatabaseSubscriber) { |
| 49 | + connection.observe(subscriber: subscriber) |
| 50 | + } |
| 51 | + |
| 52 | + func cancel(subscriber: DatabaseSubscriber) { |
| 53 | + connection.cancel(subscriber: subscriber) |
| 54 | + } |
| 55 | + |
| 56 | + func begin<Output>( |
| 57 | + _ kind: Transaction.Kind, |
| 58 | + execute: @Sendable (borrowing Transaction) throws -> Output |
| 59 | + ) async throws -> Output { |
| 60 | + try await connection.begin(kind, execute: execute) |
| 61 | + } |
| 62 | +} |
0 commit comments