Skip to content

Commit 5572922

Browse files
committed
wip
1 parent 5337652 commit 5572922

5 files changed

Lines changed: 133 additions & 181 deletions

File tree

Sources/Feather/DatabaseQuery.swift

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,26 @@ public struct FetchManyQuery<Input, Output>: Queryable
1212
Output.Element: RowDecodable & Sendable
1313
{
1414
public let transactionKind: TransactionKind
15-
private let _statement: @Sendable (Input, borrowing Transaction) throws -> Statement
15+
private let database: any Database
16+
private let statement: @Sendable (Input, borrowing Transaction) throws -> Statement
17+
18+
public typealias DB = any Database
1619

1720
public init(
1821
_ transactionKind: TransactionKind,
22+
database: any Database,
1923
statement: @Sendable @escaping (Input, borrowing Transaction) throws -> Statement
2024
){
2125
self.transactionKind = transactionKind
22-
self._statement = statement
23-
}
24-
25-
public func statement(
26-
input: Input,
27-
transaction: borrowing Transaction
28-
) throws -> Statement {
29-
return try _statement(input, transaction)
26+
self.database = database
27+
self.statement = statement
3028
}
31-
29+
3230
public func execute(
3331
with input: Input,
3432
tx: borrowing Transaction
3533
) throws -> Output {
36-
let statement = try statement(input: input, transaction: tx)
34+
let statement = try statement(input, tx)
3735
var cursor = Cursor<Output.Element>(of: statement)
3836
var result: Output = []
3937

@@ -50,30 +48,26 @@ public struct FetchSingleQuery<Input, Output>: Queryable
5048
where Input: Sendable, Output: RowDecodable & Sendable
5149
{
5250
public let transactionKind: TransactionKind
53-
private let _statement: @Sendable (Input, borrowing Transaction) throws -> Statement
51+
private let database: any Database
52+
private let statement: @Sendable (Input, borrowing Transaction) throws -> Statement
5453

55-
public typealias DB = ConnectionPool
54+
public typealias DB = any Database
5655

5756
public init(
5857
_ transactionKind: TransactionKind,
58+
database: any Database,
5959
statement: @Sendable @escaping (Input, borrowing Transaction) throws -> Statement
6060
) {
6161
self.transactionKind = transactionKind
62-
self._statement = statement
63-
}
64-
65-
public func statement(
66-
input: Input,
67-
transaction: borrowing Transaction
68-
) throws -> Statement {
69-
return try _statement(input, transaction)
62+
self.database = database
63+
self.statement = statement
7064
}
7165

7266
public func execute(
7367
with input: Input,
7468
tx: borrowing Transaction
7569
) throws -> Output? {
76-
let statement = try statement(input: input, transaction: tx)
70+
let statement = try statement(input, tx)
7771
var cursor = Cursor<Output>(of: statement)
7872
return try cursor.next()
7973
}
@@ -84,30 +78,26 @@ public struct VoidQuery<Input>: Queryable where Input: Sendable {
8478
public typealias Output = ()
8579

8680
public let transactionKind: TransactionKind
87-
private let _statement: @Sendable (Input, borrowing Transaction) throws -> Statement
81+
private let database: any Database
82+
private let statement: @Sendable (Input, borrowing Transaction) throws -> Statement
8883

89-
public typealias DB = ConnectionPool
84+
public typealias DB = any Database
9085

9186
public init(
9287
_ transactionKind: TransactionKind,
88+
database: any Database,
9389
statement: @Sendable @escaping (Input, borrowing Transaction) throws -> Statement
9490
) {
9591
self.transactionKind = transactionKind
96-
self._statement = statement
97-
}
98-
99-
public func statement(
100-
input: Input,
101-
transaction: borrowing Transaction
102-
) throws -> Statement {
103-
return try _statement(input, transaction)
92+
self.database = database
93+
self.statement = statement
10494
}
10595

10696
public func execute(
10797
with input: Input,
10898
tx: borrowing Transaction
10999
) throws {
110-
let statement = try statement(input: input, transaction: tx)
100+
let statement = try statement(input, tx)
111101
_ = try statement.step()
112102
}
113103
}

Sources/Feather/Migration.swift

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ public struct MigrationRunner {
1717
public static func execute(migrations: [String], tx: borrowing Transaction) throws {
1818
try createTableIfNeeded(tx: tx)
1919

20-
let lastMigration = try lastMigration
21-
.replaceNil(with: 0)
22-
.execute(with: (), tx: tx)
23-
24-
let pendingMigrations = migrations.enumerated()
25-
.map { (number: $0.offset + 1, migration: $0.element) }
26-
.filter { $0.number > lastMigration }
27-
.sorted { $0.number < $1.number }
28-
29-
for (number, migration) in pendingMigrations {
30-
try execute(migration: migration, number: number, tx: tx)
31-
}
20+
// let lastMigration = try lastMigration
21+
// .replaceNil(with: 0)
22+
// .execute(with: (), tx: tx)
23+
//
24+
// let pendingMigrations = migrations.enumerated()
25+
// .map { (number: $0.offset + 1, migration: $0.element) }
26+
// .filter { $0.number > lastMigration }
27+
// .sorted { $0.number < $1.number }
28+
//
29+
// for (number, migration) in pendingMigrations {
30+
// try execute(migration: migration, number: number, tx: tx)
31+
// }
3232
}
3333

3434
static func createTableIfNeeded(tx: borrowing Transaction) throws(FeatherError) {
@@ -41,24 +41,24 @@ public struct MigrationRunner {
4141

4242
static func execute(migration: String, number: Int, tx: borrowing Transaction) throws {
4343
try tx.execute(sql: migration)
44-
try insertMigration.execute(with: number, tx: tx)
44+
// try insertMigration.execute(with: number, tx: tx)
4545
}
4646

47-
private static var lastMigration: FetchSingleQuery<(), Int> {
48-
return FetchSingleQuery(.read) { _, transaction in
49-
try Statement(in: transaction) {
50-
"SELECT MAX(number) FROM \(MigrationRunner.migrationTableName)"
51-
}
52-
}
53-
}
54-
55-
private static var insertMigration: VoidQuery<Int> {
56-
return VoidQuery(.write) { input, transaction in
57-
try Statement(in: transaction) {
58-
"INSERT INTO \(MigrationRunner.migrationTableName) (number) VALUES (?)"
59-
} bind: { statement in
60-
try statement.bind(value: input, to: 1)
61-
}
62-
}
63-
}
47+
// private static var lastMigration: FetchSingleQuery<(), Int> {
48+
// return FetchSingleQuery(.read) { _, transaction in
49+
// try Statement(in: transaction) {
50+
// "SELECT MAX(number) FROM \(MigrationRunner.migrationTableName)"
51+
// }
52+
// }
53+
// }
54+
//
55+
// private static var insertMigration: VoidQuery<Int> {
56+
// return VoidQuery(.write) { input, transaction in
57+
// try Statement(in: transaction) {
58+
// "INSERT INTO \(MigrationRunner.migrationTableName) (number) VALUES (?)"
59+
// } bind: { statement in
60+
// try statement.bind(value: input, to: 1)
61+
// }
62+
// }
63+
// }
6464
}

Sources/Feather/Queries.swift

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ public enum Queries {
1111
/// Allows for the erasing of the database so a query can be
1212
/// passed around and be able to be executed without
1313
/// having the caller worry about by what.
14-
public struct WithDatabase<Base: Queryable>: Query {
14+
public struct WithDatabase<Base: Queryable>: Queryable {
1515
/// The original query that requires a database
1616
let base: Base
1717
/// The database to execute the query in
18-
let database: any Database
18+
let database: Base.DB
1919

2020
public var transactionKind: TransactionKind {
2121
return base.transactionKind
2222
}
2323

2424
public func execute(
25-
with input: Base.Input
25+
with input: Base.Input,
26+
in _: ()
2627
) async throws -> Base.Output {
27-
let tx = try await database.begin(transactionKind)
28-
return try execute(with: input, tx: tx)
28+
return try await base.execute(with: input, in: database)
2929
}
3030

3131
public func execute(
@@ -34,23 +34,10 @@ public enum Queries {
3434
) throws -> Base.Output {
3535
return try base.execute(with: input, tx: tx)
3636
}
37-
38-
public func observe(
39-
with input: Input,
40-
handle: @Sendable @escaping (Output) -> Void,
41-
cancelled: @Sendable @escaping () -> Void
42-
) -> QueryObservation<Input, Output> {
43-
return base.observe(
44-
with: input,
45-
in: database,
46-
handle: handle,
47-
cancelled: cancelled
48-
)
49-
}
5037
}
5138

5239
/// Applies a transform to the queries result
53-
public struct Map<Base: Query, Output: Sendable>: Query {
40+
public struct Map<Base: Queryable, Output: Sendable>: Queryable {
5441
/// The upstream query to transform
5542
let base: Base
5643
/// The transform to apply to the output
@@ -60,23 +47,13 @@ public enum Queries {
6047
return base.transactionKind
6148
}
6249

63-
public func execute(with input: Base.Input) async throws -> Output {
64-
return try await transform(base.execute(with: input))
65-
}
66-
67-
public func observe(
68-
with input: Input,
69-
handle: @Sendable @escaping (Output) -> Void,
70-
cancelled: @Sendable @escaping () -> Void
71-
) -> QueryObservation<Input, Output> {
72-
fatalError()
73-
// return base.observe(
74-
// with: input,
75-
// handle: { handle(transform($0)) },
76-
// cancelled: cancelled
77-
// )
50+
public func execute(
51+
with input: Base.Input,
52+
in database: Base.DB
53+
) async throws -> Output {
54+
return try await transform(base.execute(with: input, in: database))
7855
}
79-
56+
8057
public func execute(
8158
with input: Base.Input,
8259
tx: borrowing Transaction
@@ -86,8 +63,8 @@ public enum Queries {
8663
}
8764

8865
/// Applies a transform to the queries result
89-
public struct Just<Input, Output, DB>: Query
90-
where Input: Sendable, Output: Sendable, DB: Database
66+
public struct Just<Input, Output>: Queryable
67+
where Input: Sendable, Output: Sendable
9168
{
9269
let output: Output
9370

@@ -100,7 +77,8 @@ public enum Queries {
10077
}
10178

10279
public func execute(
103-
with input: Input
80+
with input: Input,
81+
in _: ()
10482
) async throws -> Output {
10583
return output
10684
}
@@ -111,20 +89,6 @@ public enum Queries {
11189
) throws -> Output {
11290
return output
11391
}
114-
115-
public func observe(
116-
with input: Input,
117-
handle: @Sendable @escaping (Output) -> Void,
118-
cancelled: @Sendable @escaping () -> Void
119-
) -> QueryObservation<Input, Output> {
120-
QueryObservation<Input, Output>(
121-
query: self,
122-
input: input,
123-
database: ErasedDatabase.shared,
124-
handle: handle,
125-
cancelled: cancelled
126-
)
127-
}
12892
}
12993

13094
public struct Then<First, Second>: Queryable
@@ -134,6 +98,8 @@ public enum Queries {
13498
let first: First
13599
let second: Second
136100

101+
public typealias DB = any Database
102+
137103
public var transactionKind: TransactionKind {
138104
return max(first.transactionKind, second.transactionKind)
139105
}
@@ -150,7 +116,7 @@ public enum Queries {
150116
}
151117

152118
public extension Queryable {
153-
func with(database: any Database) -> Queries.WithDatabase<Self> {
119+
func with(database: DB) -> Queries.WithDatabase<Self> {
154120
return Queries.WithDatabase(base: self, database: database)
155121
}
156122

@@ -178,5 +144,3 @@ public extension Queryable {
178144
}
179145
}
180146
}
181-
182-

Sources/Feather/QueryObservation.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
// Created by Wes Wickwire on 3/10/25.
66
//
77

8-
public final class QueryObservation<Input, Output>: DatabaseSubscriber, Sendable
9-
where Input: Sendable, Output: Sendable
8+
public final class QueryObservation<Input, Output, DB>: DatabaseSubscriber, Sendable
9+
where Input: Sendable, Output: Sendable, DB: Sendable
1010
{
11-
private let query: any Queryable<Input, Output>
11+
private let query: any Queryable<Input, Output, DB>
1212
private let input: Input
13-
private let database: any Database
13+
private let database: DB
1414
private let handle: @Sendable (Output) -> Void
1515
private let cancelled: @Sendable () -> Void
1616

1717
init(
18-
query: any Queryable<Input, Output>,
18+
query: any Queryable<Input, Output, DB>,
1919
input: Input,
20-
database: any Database,
20+
database: DB,
2121
handle: @Sendable @escaping (Output) -> Void,
2222
cancelled: @Sendable @escaping () -> Void
2323
) {
@@ -39,11 +39,11 @@ public final class QueryObservation<Input, Output>: DatabaseSubscriber, Sendable
3939
}
4040

4141
public func cancel() {
42-
database.cancel(subscriber: self)
42+
// database.cancel(subscriber: self)
4343
}
4444

4545
public func start() async throws {
46-
try await database.observe(subscriber: self)
46+
// try await database.observe(subscriber: self)
4747
try await emitNext()
4848
}
4949

@@ -56,11 +56,11 @@ public final class QueryObservation<Input, Output>: DatabaseSubscriber, Sendable
5656
extension Queryable {
5757
func observe(
5858
with input: Input,
59-
in database: any Database,
59+
in database: DB,
6060
handle: @Sendable @escaping (Output) -> Void,
6161
cancelled: @Sendable @escaping () -> Void
62-
) -> QueryObservation<Input, Output> {
63-
QueryObservation<Input, Output>(
62+
) -> QueryObservation<Input, Output, DB> {
63+
QueryObservation<Input, Output, DB>(
6464
query: self,
6565
input: input,
6666
database: database,
@@ -71,7 +71,7 @@ extension Queryable {
7171

7272
func stream(
7373
with input: Input,
74-
in database: any Database
74+
in database: DB
7575
) -> AsyncThrowingStream<Output, Error> {
7676
return AsyncThrowingStream<Output, Error> { continuation in
7777
let observation = self.observe(

0 commit comments

Comments
 (0)