Skip to content

Commit 7570da9

Browse files
committed
Connection Wrapper
1 parent f087eb4 commit 7570da9

4 files changed

Lines changed: 43 additions & 13 deletions

File tree

Sources/Compiler/Gen/SwiftLanguage.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public struct SwiftLanguage: Language {
209209
}
210210

211211
private func queriesProtocol(name: String, queries: [GeneratedQuery]) {
212-
writer.write(line: "protocol ", name, " {")
212+
writer.write(line: "protocol ", name, ": ConnectionWrapper {")
213213

214214
writer.indent()
215215

@@ -228,6 +228,8 @@ public struct SwiftLanguage: Language {
228228
writer.write(line: "struct ", name, "Noop: ", name, " {")
229229
writer.indent()
230230

231+
writer.write(line: "let connection: any Connection = NoopConnection()")
232+
231233
for query in queries {
232234
writer.write(line: "let ")
233235
writer.write(query.variableName)

Sources/Otter/Connection.swift

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,54 @@
99
/// directly mapped to a default SQLite connection like `SQLiteConnection`
1010
/// but is a much more high level of abstraction that allows for safe
1111
/// communication to a database.
12-
public protocol Connection: Actor {
12+
public protocol Connection: Sendable {
1313
/// Starts observation for the given subscriber
14-
nonisolated func observe(subscriber: DatabaseSubscriber)
14+
func observe(subscriber: DatabaseSubscriber)
1515

1616
/// Cancels the observation for the given subscriber
17-
nonisolated func cancel(subscriber: DatabaseSubscriber)
17+
func cancel(subscriber: DatabaseSubscriber)
1818

1919
func begin<Output>(
2020
_ kind: Transaction.Kind,
21-
execute: (borrowing Transaction) throws -> Output
21+
execute: @Sendable (borrowing Transaction) throws -> Output
2222
) async throws -> Output
2323
}
2424

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) {}
2829

2930
func begin<Output>(
3031
_ kind: Transaction.Kind,
31-
execute: (borrowing Transaction) throws -> Output
32+
execute: @Sendable (borrowing Transaction) throws -> Output
3233
) async throws -> Output {
3334
try execute(Transaction(connection: NoopRawConnection(), kind: kind))
3435
}
3536
}
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+
}

Sources/Otter/ConnectionPool.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ extension ConnectionPool: Connection {
128128
}
129129

130130
/// Starts a transaction.
131-
public func begin<Output: Sendable>(
131+
public nonisolated func begin<Output: Sendable>(
132132
_ kind: Transaction.Kind,
133-
execute: (borrowing Transaction) throws -> Output
133+
execute: @Sendable (borrowing Transaction) throws -> Output
134134
) async throws -> Output {
135135
try await beginNoCommit(kind) { tx in
136136
// The `Result` wrapper seems weird, but allows us to keep
@@ -162,7 +162,7 @@ extension ConnectionPool: Connection {
162162
/// makes sure it reclaims the connection.
163163
public func beginNoCommit<Output: Sendable>(
164164
_ kind: Transaction.Kind,
165-
execute: (consuming Transaction) async throws -> Output
165+
execute: @Sendable (consuming Transaction) async throws -> Output
166166
) async throws -> Output {
167167
let tx = try await begin(kind)
168168
let conn = tx.connection

Tests/CompilerTests/Gen/Swift.output

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ struct SelectWithOptionalInterestOutput: Hashable, Sendable, RowDecodable {
182182
}
183183
}
184184

185-
protocol QueriesQueries {
185+
protocol QueriesQueries: ConnectionWrapper {
186186
associatedtype _InsertUser: InsertUserQuery
187187
var insertUser: _InsertUser { get }
188188
associatedtype _SelectUsers: SelectUsersQuery
@@ -202,6 +202,7 @@ protocol QueriesQueries {
202202
}
203203

204204
struct QueriesQueriesNoop: QueriesQueries {
205+
let connection: any Connection = NoopConnection()
205206
let insertUser: AnyQuery<InsertUserInput, ()>
206207
let selectUsers: AnyQuery<(), [User]>
207208
let selectUserById: AnyQuery<Int, User?>

0 commit comments

Comments
 (0)