Skip to content

Commit 3626561

Browse files
committed
erased query
1 parent f85b077 commit 3626561

6 files changed

Lines changed: 50 additions & 84 deletions

File tree

Sources/Feather/Database.swift

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,10 @@
77

88
public protocol Database: Actor {
99
func observe(subscriber: DatabaseSubscriber) throws(FeatherError)
10+
1011
nonisolated func cancel(subscriber: DatabaseSubscriber)
12+
1113
func begin(
1214
_ transaction: TransactionKind
1315
) async throws(FeatherError) -> sending Transaction
1416
}
15-
16-
/// Only used when the database has been erased using the `with(database:)` operator.
17-
/// No methods in this are actually called. The `WithDatabase` query holds the database
18-
/// that will actually be used.
19-
public actor ErasedDatabase: Database {
20-
static let shared = ErasedDatabase()
21-
22-
private init() {}
23-
24-
public func observe(subscriber: DatabaseSubscriber) throws(FeatherError) {}
25-
26-
public nonisolated func cancel(subscriber: DatabaseSubscriber) {}
27-
28-
public func begin(
29-
_ transaction: TransactionKind
30-
) async throws(FeatherError) -> sending Transaction {
31-
fatalError("Cannot be used directly")
32-
}
33-
}

Sources/Feather/DatabaseQuery.swift

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@ public struct FetchManyQuery<Input, Output>: DatabaseQuery
1212
Output.Element: RowDecodable & Sendable
1313
{
1414
public let transactionKind: TransactionKind
15-
private let database: any Database
1615
private let statement: @Sendable (Input, borrowing Transaction) throws -> Statement
1716

1817
public init(
1918
_ transactionKind: TransactionKind,
20-
database: any Database,
2119
statement: @Sendable @escaping (Input, borrowing Transaction) throws -> Statement
2220
){
2321
self.transactionKind = transactionKind
24-
self.database = database
2522
self.statement = statement
2623
}
2724

@@ -46,18 +43,13 @@ public struct FetchSingleQuery<Input, Output>: DatabaseQuery
4643
where Input: Sendable, Output: RowDecodable & Sendable
4744
{
4845
public let transactionKind: TransactionKind
49-
private let database: any Database
5046
private let statement: @Sendable (Input, borrowing Transaction) throws -> Statement
51-
52-
public typealias DB = any Database
53-
47+
5448
public init(
5549
_ transactionKind: TransactionKind,
56-
database: any Database,
5750
statement: @Sendable @escaping (Input, borrowing Transaction) throws -> Statement
5851
) {
5952
self.transactionKind = transactionKind
60-
self.database = database
6153
self.statement = statement
6254
}
6355

@@ -76,18 +68,15 @@ public struct VoidQuery<Input>: DatabaseQuery where Input: Sendable {
7668
public typealias Output = ()
7769

7870
public let transactionKind: TransactionKind
79-
private let database: any Database
8071
private let statement: @Sendable (Input, borrowing Transaction) throws -> Statement
8172

8273
public typealias DB = any Database
8374

8475
public init(
8576
_ transactionKind: TransactionKind,
86-
database: any Database,
8777
statement: @Sendable @escaping (Input, borrowing Transaction) throws -> Statement
8878
) {
8979
self.transactionKind = transactionKind
90-
self.database = database
9180
self.statement = statement
9281
}
9382

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: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,25 @@ 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: DatabaseQuery>: DatabaseQuery, Query {
14+
public struct WithDatabase<Base: DatabaseQuery>: Query {
1515
/// The original query that requires a database
1616
let base: Base
1717
/// The database to execute the query in
1818
let database: any Database
1919

20-
public var transactionKind: TransactionKind {
21-
return base.transactionKind
22-
}
23-
2420
public func execute(
25-
with input: Base.Input,
26-
in _: ErasedDatabase
21+
with input: Base.Input
2722
) async throws -> Base.Output {
2823
return try await base.execute(with: input, in: database)
2924
}
3025

31-
public func execute(
32-
with input: Base.Input,
33-
tx: borrowing Transaction
34-
) throws -> Base.Output {
35-
return try base.execute(with: input, tx: tx)
36-
}
37-
3826
public func observe(
3927
with input: Input,
40-
in _: ErasedDatabase,
4128
handle: @Sendable @escaping (Output) -> Void,
4229
cancelled: @Sendable @escaping () -> Void
4330
) -> QueryObservation<Input, Output> {
4431
return base.observe(with: input, in: database, handle: handle, cancelled: cancelled)
4532
}
46-
47-
public func execute(
48-
with input: Base.Input
49-
) async throws -> Base.Output {
50-
return try await base.execute(with: input, in: database)
51-
}
5233
}
5334

5435
/// Applies a transform to the queries result

Sources/Feather/Query.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,23 @@ public protocol Query<Input, Output> {
1010
associatedtype Output
1111

1212
func execute(with input: Input) async throws -> Output
13+
14+
func observe(
15+
with input: Input,
16+
handle: @Sendable @escaping (Output) -> Void,
17+
cancelled: @Sendable @escaping () -> Void
18+
) -> QueryObservation<Input, Output>
1319
}
1420

15-
extension Query where Input == () {
21+
public extension Query where Input == () {
1622
func execute() async throws -> Output {
1723
return try await execute(with: ())
1824
}
25+
26+
func observe(
27+
handle: @Sendable @escaping (Output) -> Void,
28+
cancelled: @Sendable @escaping () -> Void
29+
) -> QueryObservation<Input, Output> {
30+
return observe(with: (), handle: handle, cancelled: cancelled)
31+
}
1932
}

Sources/Feather/Queryable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public extension DatabaseQuery {
3939
cancelled: @Sendable @escaping () -> Void
4040
) -> QueryObservation<Input, Output> {
4141
return QueryObservation(
42-
query: self.with(database: database),
42+
query: self,
4343
input: input,
4444
database: database,
4545
handle: handle,

0 commit comments

Comments
 (0)