Skip to content

Commit 3fce430

Browse files
committed
Remove DatabaseQuery
1 parent 8622b1e commit 3fce430

20 files changed

Lines changed: 251 additions & 197 deletions

Sources/Compiler/Gen/SwiftLanguage.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public struct SwiftLanguage: Language {
3939
input: String,
4040
output: String
4141
) -> String {
42-
return "AnyDatabaseQuery<\(input), \(output)>"
42+
return "DatabaseQuery<\(input), \(output)>"
4343
}
4444

4545
public func interpolatedQuestionMarks(for param: String) -> String {
@@ -544,7 +544,7 @@ public struct SwiftLanguage: Language {
544544
/// to be referenced explicitly in their decl.
545545
private func dbTypeAlias(for query: GeneratedQuery, queryType: String = "Query") {
546546
let name = query.typealiasName.replacingOccurrences(of: "Query", with: "DatabaseQuery")
547-
writer.write(line: "typealias ", name, " = AnyDatabaseQuery<", query.inputName, ", ", query.outputName, ">")
547+
writer.write(line: "typealias ", name, " = DatabaseQuery<", query.inputName, ", ", query.outputName, ">")
548548
}
549549

550550
private func inputExtension(

Sources/Otter/Connection.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ public protocol Connection: Actor {
2121
execute: (borrowing Transaction) throws -> Output
2222
) async throws -> Output
2323
}
24+
25+
actor NoopConnection: Connection {
26+
nonisolated func observe(subscriber: any DatabaseSubscriber) {}
27+
nonisolated func cancel(subscriber: any DatabaseSubscriber) {}
28+
29+
func begin<Output>(
30+
_ kind: Transaction.Kind,
31+
execute: (borrowing Transaction) throws -> Output
32+
) async throws -> Output {
33+
try execute(Transaction(connection: NoopRawConnection(), kind: kind))
34+
}
35+
}

Sources/Otter/ConnectionPool.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ public actor ConnectionPool: Sendable {
2323
/// The maximum number of connections we can create
2424
private let limit: Int
2525
/// Any connections available for use
26-
private var availableConnections: [SQLiteConnection]
26+
private var availableConnections: [RawConnection]
2727
/// Any caller waiting for a connection
2828
private var waitingForConnection: [WaiterContinuation] = []
2929
/// A lock to synchronize writes.
3030
private var writeLock = Lock()
3131
/// Manages alerting any subscribers of any database changes.
3232
private nonisolated let observer = DatabaseObserver()
3333

34-
typealias WaiterContinuation = CheckedContinuation<SQLiteConnection, Never>
34+
typealias WaiterContinuation = CheckedContinuation<RawConnection, Never>
3535

3636
public init(
3737
path: String,
@@ -72,7 +72,7 @@ public actor ConnectionPool: Sendable {
7272
}
7373

7474
/// Gives the connection back to the pool.
75-
private func reclaim(connection: SQLiteConnection, kind: Transaction.Kind) async {
75+
private func reclaim(connection: RawConnection, kind: Transaction.Kind) async {
7676
availableConnections.append(connection)
7777
alertAnyWaitersOfAvailableConnection()
7878

@@ -82,7 +82,7 @@ public actor ConnectionPool: Sendable {
8282
}
8383

8484
/// Will get, wait or create a connection to the database
85-
private func getConnection() async throws(OtterError) -> SQLiteConnection {
85+
private func getConnection() async throws(OtterError) -> RawConnection {
8686
guard availableConnections.isEmpty else {
8787
// Have an available connection, just use it
8888
return availableConnections.removeLast()

Sources/Otter/DatabaseQuery.swift

Lines changed: 0 additions & 45 deletions
This file was deleted.

Sources/Otter/Queries/AnyQuery.swift

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,42 @@
1010
/// on the original base query type. Erasing to this can allow for
1111
/// use of the operators.
1212
public struct AnyQuery<Input: Sendable, Output: Sendable>: Query {
13-
let query: any Query<Input, Output>
14-
13+
public let transactionKind: Transaction.Kind
14+
public let connection: any Connection
15+
public let watchedTables: Set<String>
16+
private let _execute: @Sendable (Input, borrowing Transaction) throws -> Output
17+
private let _observe: @Sendable (Input) -> any QueryObservation<Output>
18+
19+
public init(
20+
transactionKind: Transaction.Kind,
21+
connection: any Connection,
22+
watchedTables: Set<String>,
23+
execute: @Sendable @escaping (Input, borrowing Transaction) throws -> Output,
24+
observe: @Sendable @escaping (Input) -> any QueryObservation<Output>
25+
) {
26+
self.transactionKind = transactionKind
27+
self.connection = connection
28+
self.watchedTables = watchedTables
29+
self._execute = execute
30+
self._observe = observe
31+
}
32+
1533
public init(_ query: any Query<Input, Output>) {
16-
self.query = query
34+
self = AnyQuery(
35+
transactionKind: query.transactionKind,
36+
connection: query.connection,
37+
watchedTables: query.watchedTables,
38+
execute: { try query.execute(with: $0, tx: $1) },
39+
observe: { query.observe(with: $0) }
40+
)
1741
}
18-
19-
public func execute(with input: Input) async throws -> Output {
20-
try await query.execute(with: input)
42+
43+
public func execute(with input: Input, tx: borrowing Transaction) throws -> Output {
44+
try _execute(input, tx)
2145
}
2246

2347
public func observe(with input: Input) -> any QueryObservation<Output> {
24-
query.observe(with: input)
48+
_observe(input)
2549
}
2650
}
2751

Sources/Otter/Queries/Bulk.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
extension Queries {
9-
public struct Bulk<Base: DatabaseQuery>: DatabaseQuery {
9+
public struct Bulk<Base: Query>: Query {
1010
public typealias Input = [Base.Input]
1111
public typealias Output = [Base.Output]
1212

@@ -39,7 +39,7 @@ extension Queries {
3939
}
4040
}
4141

42-
extension DatabaseQuery {
42+
extension Query {
4343
/// Returns a query that executes the same query in bulk
4444
/// for each input.
4545
///

Sources/Otter/Queries/AnyDatabaseQuery.swift renamed to Sources/Otter/Queries/DatabaseQuery.swift

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
//
2-
// AnyDatabaseQuery.swift
2+
// DatabaseQuery.swift
33
// Otter
44
//
55
// Created by Wes Wickwire on 5/5/25.
66
//
77

8-
/// A query that is executed against a database.
9-
public struct AnyDatabaseQuery<Input, Output>: DatabaseQuery
8+
/// A default implementation of a `Query`. Expects to be executed
9+
/// against a real database and not a Noop.
10+
///
11+
/// This is the structure that the codegen of the compiler expects to use.
12+
public struct DatabaseQuery<Input, Output>: Query
1013
where Input: Sendable, Output: Sendable
1114
{
1215
public let connection: any Connection
1316
public let transactionKind: Transaction.Kind
1417
public let watchedTables: Set<String>
1518
public let execute: @Sendable (Input, borrowing Transaction) throws -> Output
1619

17-
/// Initializes a `AnyDatabaseQuery`. This a query that will be handed
20+
/// Initializes a `DatabaseQuery`. This a query that will be handed
1821
/// the input and transaction when execute is called.
1922
///
2023
/// ```swift
21-
/// AnyDatabaseQuery<In, Out>(.read, in: connection) { input, tx in
24+
/// DatabaseQuery<In, Out>(.read, in: connection) { input, tx in
2225
/// ...
2326
/// }
2427
/// ```
@@ -50,16 +53,3 @@ public struct AnyDatabaseQuery<Input, Output>: DatabaseQuery
5053
return try execute(input, tx)
5154
}
5255
}
53-
54-
public extension DatabaseQuery {
55-
/// Erases the current query to a `AnyDatabaseQuery`. Useful if you are using
56-
/// operators like `map` or `mapInput` which can have quite the long signature
57-
/// for combined queries.
58-
///
59-
/// - Returns: `self` erased to a `AnyDatabaseQuery`
60-
func eraseToAnyDatabaseQuery() -> AnyDatabaseQuery<Input, Output> {
61-
AnyDatabaseQuery(transactionKind, in: connection, watchingTables: watchedTables) { input, tx in
62-
try self.execute(with: input, tx: tx)
63-
}
64-
}
65-
}

Sources/Otter/Queries/Fail.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,21 @@ public extension Queries {
2323
self.error = error
2424
}
2525

26+
public var transactionKind: Transaction.Kind { .read }
27+
public var watchedTables: Set<String> { [] }
28+
public var connection: any Connection { NoopConnection() }
29+
2630
public func execute(with input: Input) async throws -> Output {
2731
throw error
2832
}
2933

34+
public func execute(
35+
with input: Input,
36+
tx: borrowing Transaction
37+
) throws -> Output {
38+
throw error
39+
}
40+
3041
public func observe(with input: Input) -> any QueryObservation<Output> {
3142
return Observation(error: error)
3243
}

Sources/Otter/Queries/Just.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,23 @@ public extension Queries {
4343
self = Just(nil)
4444
}
4545

46+
public var transactionKind: Transaction.Kind { .read }
47+
48+
public var watchedTables: Set<String> { [] }
49+
50+
public var connection: any Connection { NoopConnection() }
51+
4652
public func execute(with input: Input) async throws -> Output {
4753
return output
4854
}
4955

56+
public func execute(
57+
with input: Input,
58+
tx: borrowing Transaction
59+
) throws -> Output {
60+
return output
61+
}
62+
5063
public func observe(with input: Input) -> any QueryObservation<Output> {
5164
return Observation(output: output)
5265
}

Sources/Otter/Queries/Map.swift

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,20 @@ public extension Queries {
1515
/// The transform to apply to the output
1616
let transform: @Sendable (Base.Input, Base.Output) throws -> Output
1717

18-
public func execute(with input: Base.Input) async throws -> Output {
19-
try await transform(input, base.execute(with: input))
18+
public var transactionKind: Transaction.Kind {
19+
base.transactionKind
20+
}
21+
22+
public var connection: any Connection {
23+
base.connection
24+
}
25+
26+
public var watchedTables: Set<String> {
27+
base.watchedTables
28+
}
29+
30+
public func execute(with input: Base.Input, tx: borrowing Transaction) throws -> Output {
31+
try transform(input, base.execute(with: input, tx: tx))
2032
}
2133

2234
public func observe(with input: Base.Input) -> any QueryObservation<Output> {
@@ -51,26 +63,6 @@ public extension Queries {
5163
}
5264
}
5365

54-
extension Queries.Map: DatabaseQuery where Base: DatabaseQuery {
55-
public var connection: any Connection {
56-
return base.connection
57-
}
58-
59-
public var transactionKind: Transaction.Kind {
60-
return base.transactionKind
61-
}
62-
63-
public var watchedTables: Set<String> {
64-
return base.watchedTables
65-
}
66-
67-
public func execute(
68-
with input: Base.Input,
69-
tx: borrowing Transaction
70-
) throws -> Output {
71-
return try transform(input, base.execute(with: input, tx: tx))
72-
}
73-
}
7466

7567
public extension Query {
7668
/// Transforms the output of the query.

0 commit comments

Comments
 (0)