-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseClientSQLite.swift
More file actions
94 lines (85 loc) · 2.69 KB
/
DatabaseClientSQLite.swift
File metadata and controls
94 lines (85 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// DatabaseClientSQLite.swift
// feather-database-sqlite
//
// Created by Tibor Bödecs on 2026. 01. 10..
//
import FeatherDatabase
import Logging
import SQLiteNIOExtras
/// A SQLite-backed database client.
///
/// Use this client to execute queries and manage transactions on SQLite.
public struct DatabaseClientSQLite: DatabaseClient {
public typealias Connection = DatabaseConnectionSQLite
let client: SQLiteClient
var logger: Logger
/// Create a SQLite database client backed by a connection pool.
///
/// - Parameters:
/// - client: The SQLite client to use.
/// - logger: The logger to use.
public init(
client: SQLiteClient,
logger: Logger
) {
self.client = client
self.logger = logger
}
// MARK: - database api
/// Execute work using a leased connection.
///
/// The closure is executed with a pooled connection.
/// - Parameters closure: A closure that receives the SQLite connection.
/// - Throws: A `DatabaseError` if the connection fails.
/// - Returns: The query result produced by the closure.
@discardableResult
public func withConnection<T>(
_ closure: (Connection) async throws -> T
) async throws(DatabaseError) -> T {
do {
return try await client.withConnection { connection in
try await closure(
DatabaseConnectionSQLite(
connection: connection,
logger: logger
)
)
}
}
catch {
throw .connection(error)
}
}
/// Execute work inside a SQLite transaction.
///
/// The closure runs between `BEGIN` and `COMMIT` with rollback on failure.
/// - Parameters closure: A closure that receives the SQLite connection.
/// - Throws: A `DatabaseError` if transaction handling fails.
/// - Returns: The query result produced by the closure.
@discardableResult
public func withTransaction<T>(
_ closure: (Connection) async throws -> T
) async throws(DatabaseError) -> T {
do {
return try await client.withTransaction { connection in
try await closure(
DatabaseConnectionSQLite(
connection: connection,
logger: logger
)
)
}
}
catch let error as SQLiteTransactionError {
throw .transaction(
DatabaseTransactionErrorSQLite(
underlyingError: error
)
)
}
catch {
throw .connection(error)
}
}
}