Skip to content

Commit 7524bc7

Browse files
wip: cleanup API
1 parent df4c602 commit 7524bc7

27 files changed

+810
-193
lines changed

Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift

Lines changed: 110 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
55
let logger: any LoggerProtocol
66

77
private let kotlinDatabase: PowerSyncKotlin.PowerSyncDatabase
8-
9-
var currentStatus: SyncStatus { kotlinDatabase.currentStatus }
8+
let currentStatus: SyncStatus
109

1110
init(
1211
schema: Schema,
@@ -21,6 +20,9 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
2120
logger: logger.kLogger
2221
)
2322
self.logger = logger
23+
self.currentStatus = KotlinSyncStatus(
24+
baseStatus: kotlinDatabase.currentStatus
25+
)
2426
}
2527

2628
func waitForFirstSync() async throws {
@@ -55,11 +57,17 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
5557
}
5658

5759
func getCrudBatch(limit: Int32 = 100) async throws -> CrudBatch? {
58-
try await kotlinDatabase.getCrudBatch(limit: limit)
60+
guard let base = try await kotlinDatabase.getCrudBatch(limit: limit) else {
61+
return nil
62+
}
63+
return try KotlinCrudBatch(base)
5964
}
6065

6166
func getNextCrudTransaction() async throws -> CrudTransaction? {
62-
try await kotlinDatabase.getNextCrudTransaction()
67+
guard let base = try await kotlinDatabase.getNextCrudTransaction() else {
68+
return nil
69+
}
70+
return try KotlinCrudTransaction(base)
6371
}
6472

6573
func getPowerSyncVersion() async throws -> String {
@@ -71,117 +79,130 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
7179
}
7280

7381
func disconnectAndClear(clearLocal: Bool = true) async throws {
74-
try await kotlinDatabase.disconnectAndClear(clearLocal: clearLocal)
82+
try await kotlinDatabase.disconnectAndClear(
83+
clearLocal: clearLocal
84+
)
7585
}
7686

77-
func execute(sql: String, parameters: [Any]?) async throws -> Int64 {
78-
try Int64(truncating: await kotlinDatabase.execute(sql: sql, parameters: parameters))
87+
func execute(sql: String, parameters: [Any?]?) async throws -> Int64 {
88+
try await writeTransaction {ctx in
89+
try ctx.execute(
90+
sql: sql,
91+
parameters: parameters
92+
)
93+
}
7994
}
8095

8196
func get<RowType>(
8297
sql: String,
83-
parameters: [Any]?,
98+
parameters: [Any?]?,
8499
mapper: @escaping (SqlCursor) -> RowType
85100
) async throws -> RowType {
86-
try safeCast(await kotlinDatabase.get(
87-
sql: sql,
88-
parameters: parameters,
89-
mapper: mapper
90-
), to: RowType.self)
101+
try await readTransaction { ctx in
102+
try ctx.get(
103+
sql: sql,
104+
parameters: parameters,
105+
mapper: mapper
106+
)
107+
}
91108
}
92109

93110
func get<RowType>(
94111
sql: String,
95-
parameters: [Any]?,
112+
parameters: [Any?]?,
96113
mapper: @escaping (SqlCursor) throws -> RowType
97114
) async throws -> RowType {
98-
return try await wrapQueryCursorTyped(
99-
mapper: mapper,
100-
executor: { wrappedMapper in
101-
try await self.kotlinDatabase.get(
102-
sql: sql,
103-
parameters: parameters,
104-
mapper: wrappedMapper
105-
)
106-
},
107-
resultType: RowType.self
108-
)
115+
try await readTransaction { ctx in
116+
try ctx.get(
117+
sql: sql,
118+
parameters: parameters,
119+
mapper: mapper
120+
)
121+
}
109122
}
110123

111124
func getAll<RowType>(
112125
sql: String,
113-
parameters: [Any]?,
126+
parameters: [Any?]?,
114127
mapper: @escaping (SqlCursor) -> RowType
115128
) async throws -> [RowType] {
116-
try safeCast(await kotlinDatabase.getAll(
117-
sql: sql,
118-
parameters: parameters,
119-
mapper: mapper
120-
), to: [RowType].self)
129+
try await readTransaction { ctx in
130+
try ctx.getAll(
131+
sql: sql,
132+
parameters: parameters,
133+
mapper: mapper
134+
)
135+
}
121136
}
122137

123138
func getAll<RowType>(
124139
sql: String,
125-
parameters: [Any]?,
140+
parameters: [Any?]?,
126141
mapper: @escaping (SqlCursor) throws -> RowType
127142
) async throws -> [RowType] {
128-
try await wrapQueryCursorTyped(
129-
mapper: mapper,
130-
executor: { wrappedMapper in
131-
try await self.kotlinDatabase.getAll(
132-
sql: sql,
133-
parameters: parameters,
134-
mapper: wrappedMapper
135-
)
136-
},
137-
resultType: [RowType].self
138-
)
143+
try await readTransaction { ctx in
144+
try ctx.getAll(
145+
sql: sql,
146+
parameters: parameters,
147+
mapper: mapper
148+
)
149+
}
139150
}
140151

141152
func getOptional<RowType>(
142153
sql: String,
143-
parameters: [Any]?,
154+
parameters: [Any?]?,
144155
mapper: @escaping (SqlCursor) -> RowType
145156
) async throws -> RowType? {
146-
try safeCast(await kotlinDatabase.getOptional(
147-
sql: sql,
148-
parameters: parameters,
149-
mapper: mapper
150-
), to: RowType?.self)
157+
try await readTransaction { ctx in
158+
try ctx.getOptional(
159+
sql: sql,
160+
parameters: parameters,
161+
mapper: mapper
162+
)
163+
}
151164
}
152165

153166
func getOptional<RowType>(
154167
sql: String,
155-
parameters: [Any]?,
168+
parameters: [Any?]?,
156169
mapper: @escaping (SqlCursor) throws -> RowType
157170
) async throws -> RowType? {
158-
try await wrapQueryCursorTyped(
159-
mapper: mapper,
160-
executor: { wrappedMapper in
161-
try await self.kotlinDatabase.getOptional(
162-
sql: sql,
163-
parameters: parameters,
164-
mapper: wrappedMapper
165-
)
166-
},
167-
resultType: RowType?.self
168-
)
171+
try await readTransaction { ctx in
172+
try ctx.getOptional(
173+
sql: sql,
174+
parameters: parameters,
175+
mapper: mapper
176+
)
177+
}
169178
}
170-
179+
171180
func watch<RowType>(
172181
sql: String,
173-
parameters: [Any]?,
182+
parameters: [Any?]?,
174183
mapper: @escaping (SqlCursor) -> RowType
175-
) throws -> AsyncThrowingStream<[RowType], Error> {
176-
try watch(options: WatchOptions(sql: sql, parameters: parameters, mapper: mapper))
184+
) throws -> AsyncThrowingStream<[RowType], any Error> {
185+
try watch(
186+
options: WatchOptions(
187+
sql: sql,
188+
parameters: parameters,
189+
mapper: mapper
190+
)
191+
)
177192
}
178193

179194
func watch<RowType>(
180195
sql: String,
181-
parameters: [Any]?,
196+
parameters: [Any?]?,
182197
mapper: @escaping (SqlCursor) throws -> RowType
183-
) throws -> AsyncThrowingStream<[RowType], Error> {
184-
try watch(options: WatchOptions(sql: sql, parameters: parameters, mapper: mapper))
198+
) throws -> AsyncThrowingStream<[RowType], any Error> {
199+
try watch(
200+
options: WatchOptions(
201+
sql: sql,
202+
parameters: parameters,
203+
mapper: mapper
204+
)
205+
)
185206
}
186207

187208
func watch<RowType>(
@@ -202,18 +223,18 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
202223
// EXPLAIN statement to prevent crashes in SKIEE
203224
_ = try await self.kotlinDatabase.getAll(
204225
sql: "EXPLAIN \(options.sql)",
205-
parameters: options.parameters,
226+
parameters: mapParameters(options.parameters),
206227
mapper: { _ in "" }
207228
)
208229

209230
// Watching for changes in the database
210231
for try await values in try self.kotlinDatabase.watch(
211232
sql: options.sql,
212-
parameters: options.parameters,
233+
parameters: mapParameters(options.parameters),
213234
throttleMs: KotlinLong(value: options.throttleMs),
214235
mapper: { cursor in
215236
do {
216-
return try options.mapper(cursor)
237+
return try options.mapper(KotlinSqlCursor(base: cursor))
217238
} catch {
218239
mapperError = error
219240
return ()
@@ -247,12 +268,26 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
247268
}
248269
}
249270

250-
func writeTransaction<R>(callback: @escaping (any PowerSyncTransaction) throws -> R) async throws -> R {
251-
return try safeCast(await kotlinDatabase.writeTransaction(callback: TransactionCallback(callback: callback)), to: R.self)
271+
func writeTransaction<R>(callback: @escaping (any ConnectionContext) throws -> R) async throws -> R {
272+
return try safeCast(
273+
await kotlinDatabase.writeTransaction(
274+
callback: TransactionCallback(
275+
callback: callback
276+
)
277+
),
278+
to: R.self
279+
)
252280
}
253281

254-
func readTransaction<R>(callback: @escaping (any PowerSyncTransaction) throws -> R) async throws -> R {
255-
return try safeCast(await kotlinDatabase.readTransaction(callback: TransactionCallback(callback: callback)), to: R.self)
282+
func readTransaction<R>(callback: @escaping (any ConnectionContext) throws -> R) async throws -> R {
283+
return try safeCast(
284+
await kotlinDatabase.readTransaction(
285+
callback: TransactionCallback(
286+
callback: callback
287+
)
288+
),
289+
to: R.self
290+
)
256291
}
257292

258293
func close() async throws {
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import PowerSyncKotlin
22

33
typealias KotlinPowerSyncBackendConnector = PowerSyncKotlin.PowerSyncBackendConnector
4-
public typealias CrudEntry = PowerSyncKotlin.CrudEntry
5-
public typealias CrudBatch = PowerSyncKotlin.CrudBatch
6-
public typealias SyncStatus = PowerSyncKotlin.SyncStatus
7-
public typealias SqlCursor = PowerSyncKotlin.SqlCursor
8-
public typealias JsonParam = PowerSyncKotlin.JsonParam
9-
public typealias CrudTransaction = PowerSyncKotlin.CrudTransaction
104
typealias KotlinPowerSyncCredentials = PowerSyncKotlin.PowerSyncCredentials
115
typealias KotlinPowerSyncDatabase = PowerSyncKotlin.PowerSyncDatabase
12-
public typealias Transaction = PowerSyncKotlin.PowerSyncTransaction
13-
public typealias ConnectionContext = PowerSyncKotlin.ConnectionContext
6+

Sources/PowerSync/Kotlin/SafeCastError.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Foundation
2+
13
enum SafeCastError: Error, CustomStringConvertible {
24
case typeMismatch(expected: Any.Type, actual: Any?)
35

@@ -11,6 +13,15 @@ enum SafeCastError: Error, CustomStringConvertible {
1113
}
1214

1315
internal func safeCast<T>(_ value: Any?, to type: T.Type) throws -> T {
16+
// Special handling for nil when T is an optional type
17+
if value == nil || value is NSNull {
18+
// Check if T is an optional type that can accept nil
19+
let nilValue: Any? = nil
20+
if let nilAsT = nilValue as? T {
21+
return nilAsT
22+
}
23+
}
24+
1425
if let castedValue = value as? T {
1526
return castedValue
1627
} else {

Sources/PowerSync/Kotlin/SqlCursor.swift

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

0 commit comments

Comments
 (0)