Skip to content

Commit dbaba58

Browse files
Cleanup
1 parent 7524bc7 commit dbaba58

18 files changed

+395
-144
lines changed

Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
2020
logger: logger.kLogger
2121
)
2222
self.logger = logger
23-
self.currentStatus = KotlinSyncStatus(
23+
currentStatus = KotlinSyncStatus(
2424
baseStatus: kotlinDatabase.currentStatus
2525
)
2626
}
@@ -30,18 +30,22 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
3030
}
3131

3232
func updateSchema(schema: any SchemaProtocol) async throws {
33-
try await kotlinDatabase.updateSchema(schema: KotlinAdapter.Schema.toKotlin(schema))
33+
try await kotlinDatabase.updateSchema(
34+
schema: KotlinAdapter.Schema.toKotlin(schema)
35+
)
3436
}
3537

3638
func waitForFirstSync(priority: Int32) async throws {
37-
try await kotlinDatabase.waitForFirstSync(priority: priority)
39+
try await kotlinDatabase.waitForFirstSync(
40+
priority: priority
41+
)
3842
}
3943

4044
func connect(
4145
connector: PowerSyncBackendConnector,
4246
crudThrottleMs: Int64 = 1000,
4347
retryDelayMs: Int64 = 5000,
44-
params: [String: JsonParam?] = [:]
48+
params: JsonParam = [:]
4549
) async throws {
4650
let connectorAdapter = PowerSyncBackendConnectorAdapter(
4751
swiftBackendConnector: connector,
@@ -52,22 +56,27 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
5256
connector: connectorAdapter,
5357
crudThrottleMs: crudThrottleMs,
5458
retryDelayMs: retryDelayMs,
55-
params: params
59+
// We map to basic values and use NSNull to avoid SKIEE thinking the values must be of Any type
60+
params: params.mapValues { $0.toValue() ?? NSNull() }
5661
)
5762
}
5863

5964
func getCrudBatch(limit: Int32 = 100) async throws -> CrudBatch? {
6065
guard let base = try await kotlinDatabase.getCrudBatch(limit: limit) else {
6166
return nil
6267
}
63-
return try KotlinCrudBatch(base)
68+
return try KotlinCrudBatch(
69+
batch: base
70+
)
6471
}
6572

6673
func getNextCrudTransaction() async throws -> CrudTransaction? {
6774
guard let base = try await kotlinDatabase.getNextCrudTransaction() else {
6875
return nil
6976
}
70-
return try KotlinCrudTransaction(base)
77+
return try KotlinCrudTransaction(
78+
transaction: base
79+
)
7180
}
7281

7382
func getPowerSyncVersion() async throws -> String {
@@ -85,7 +94,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
8594
}
8695

8796
func execute(sql: String, parameters: [Any?]?) async throws -> Int64 {
88-
try await writeTransaction {ctx in
97+
try await writeTransaction { ctx in
8998
try ctx.execute(
9099
sql: sql,
91100
parameters: parameters
@@ -98,7 +107,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
98107
parameters: [Any?]?,
99108
mapper: @escaping (SqlCursor) -> RowType
100109
) async throws -> RowType {
101-
try await readTransaction { ctx in
110+
try await readLock { ctx in
102111
try ctx.get(
103112
sql: sql,
104113
parameters: parameters,
@@ -112,7 +121,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
112121
parameters: [Any?]?,
113122
mapper: @escaping (SqlCursor) throws -> RowType
114123
) async throws -> RowType {
115-
try await readTransaction { ctx in
124+
try await readLock { ctx in
116125
try ctx.get(
117126
sql: sql,
118127
parameters: parameters,
@@ -126,7 +135,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
126135
parameters: [Any?]?,
127136
mapper: @escaping (SqlCursor) -> RowType
128137
) async throws -> [RowType] {
129-
try await readTransaction { ctx in
138+
try await readLock { ctx in
130139
try ctx.getAll(
131140
sql: sql,
132141
parameters: parameters,
@@ -140,7 +149,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
140149
parameters: [Any?]?,
141150
mapper: @escaping (SqlCursor) throws -> RowType
142151
) async throws -> [RowType] {
143-
try await readTransaction { ctx in
152+
try await readLock { ctx in
144153
try ctx.getAll(
145154
sql: sql,
146155
parameters: parameters,
@@ -154,7 +163,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
154163
parameters: [Any?]?,
155164
mapper: @escaping (SqlCursor) -> RowType
156165
) async throws -> RowType? {
157-
try await readTransaction { ctx in
166+
try await readLock { ctx in
158167
try ctx.getOptional(
159168
sql: sql,
160169
parameters: parameters,
@@ -168,15 +177,15 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
168177
parameters: [Any?]?,
169178
mapper: @escaping (SqlCursor) throws -> RowType
170179
) async throws -> RowType? {
171-
try await readTransaction { ctx in
180+
try await readLock { ctx in
172181
try ctx.getOptional(
173182
sql: sql,
174183
parameters: parameters,
175184
mapper: mapper
176185
)
177186
}
178187
}
179-
188+
180189
func watch<RowType>(
181190
sql: String,
182191
parameters: [Any?]?,
@@ -268,7 +277,22 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
268277
}
269278
}
270279

271-
func writeTransaction<R>(callback: @escaping (any ConnectionContext) throws -> R) async throws -> R {
280+
func writeLock<R>(
281+
callback: @escaping (any ConnectionContext) throws -> R
282+
) async throws -> R {
283+
return try safeCast(
284+
await kotlinDatabase.writeLock(
285+
callback: LockCallback(
286+
callback: callback
287+
)
288+
),
289+
to: R.self
290+
)
291+
}
292+
293+
func writeTransaction<R>(
294+
callback: @escaping (any Transaction) throws -> R
295+
) async throws -> R {
272296
return try safeCast(
273297
await kotlinDatabase.writeTransaction(
274298
callback: TransactionCallback(
@@ -279,7 +303,24 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
279303
)
280304
}
281305

282-
func readTransaction<R>(callback: @escaping (any ConnectionContext) throws -> R) async throws -> R {
306+
func readLock<R>(
307+
callback: @escaping (any ConnectionContext) throws -> R
308+
)
309+
async throws -> R
310+
{
311+
return try safeCast(
312+
await kotlinDatabase.readLock(
313+
callback: LockCallback(
314+
callback: callback
315+
)
316+
),
317+
to: R.self
318+
)
319+
}
320+
321+
func readTransaction<R>(
322+
callback: @escaping (any Transaction) throws -> R
323+
) async throws -> R {
283324
return try safeCast(
284325
await kotlinDatabase.readTransaction(
285326
callback: TransactionCallback(

Sources/PowerSync/Kotlin/TransactionCallback.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import PowerSyncKotlin
22

3-
class TransactionCallback<R>: PowerSyncKotlin.ThrowableTransactionCallback {
3+
class LockCallback<R>: PowerSyncKotlin.ThrowableLockCallback {
44
let callback: (ConnectionContext) throws -> R
55

66
init(callback: @escaping (ConnectionContext) throws -> R) {
@@ -21,10 +21,33 @@ class TransactionCallback<R>: PowerSyncKotlin.ThrowableTransactionCallback {
2121
// from a "core" package in Kotlin that provides better control over exception handling
2222
// and other functionality—without modifying the public `PowerSyncDatabase` API to include
2323
// Swift-specific logic.
24-
func execute(transaction: PowerSyncKotlin.PowerSyncTransaction) throws -> Any {
24+
func execute(context: PowerSyncKotlin.ConnectionContext) throws -> Any {
2525
do {
2626
return try callback(
2727
KotlinConnectionContext(
28+
ctx: context
29+
)
30+
)
31+
} catch {
32+
return PowerSyncKotlin.PowerSyncException(
33+
message: error.localizedDescription,
34+
cause: PowerSyncKotlin.KotlinThrowable(message: error.localizedDescription)
35+
)
36+
}
37+
}
38+
}
39+
40+
class TransactionCallback<R>: PowerSyncKotlin.ThrowableTransactionCallback {
41+
let callback: (Transaction) throws -> R
42+
43+
init(callback: @escaping (Transaction) throws -> R) {
44+
self.callback = callback
45+
}
46+
47+
func execute(transaction: PowerSyncKotlin.PowerSyncTransaction) throws -> Any {
48+
do {
49+
return try callback(
50+
KotlinTransactionContext(
2851
ctx: transaction
2952
)
3053
)

Sources/PowerSync/Kotlin/db/KotlinConnectionContext.swift

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import Foundation
22
import PowerSyncKotlin
33

4-
class KotlinConnectionContext: ConnectionContext {
5-
private let ctx: PowerSyncKotlin.ConnectionContext
6-
7-
init(ctx: PowerSyncKotlin.ConnectionContext) {
8-
self.ctx = ctx
9-
}
10-
4+
protocol KotlinConnectionContextProtocol: ConnectionContext {
5+
var ctx: PowerSyncKotlin.ConnectionContext { get }
6+
}
7+
8+
extension KotlinConnectionContextProtocol {
119
func execute(sql: String, parameters: [Any?]?) throws -> Int64 {
1210
try ctx.execute(
1311
sql: sql,
1412
parameters: mapParameters(parameters)
1513
)
1614
}
1715

18-
func getOptional<RowType>(sql: String, parameters: [Any?]?, mapper: @escaping (any SqlCursor) throws -> RowType) throws -> RowType? {
19-
return try wrapQueryCursorTypedSync(
16+
func getOptional<RowType>(
17+
sql: String,
18+
parameters: [Any?]?,
19+
mapper: @escaping (any SqlCursor) throws -> RowType
20+
) throws -> RowType? {
21+
return try wrapQueryCursorTyped(
2022
mapper: mapper,
2123
executor: { wrappedMapper in
2224
try self.ctx.getOptional(
@@ -29,8 +31,12 @@ class KotlinConnectionContext: ConnectionContext {
2931
)
3032
}
3133

32-
func getAll<RowType>(sql: String, parameters: [Any?]?, mapper: @escaping (any SqlCursor) throws -> RowType) throws -> [RowType] {
33-
return try wrapQueryCursorTypedSync(
34+
func getAll<RowType>(
35+
sql: String,
36+
parameters: [Any?]?,
37+
mapper: @escaping (any SqlCursor) throws -> RowType
38+
) throws -> [RowType] {
39+
return try wrapQueryCursorTyped(
3440
mapper: mapper,
3541
executor: { wrappedMapper in
3642
try self.ctx.getAll(
@@ -43,8 +49,12 @@ class KotlinConnectionContext: ConnectionContext {
4349
)
4450
}
4551

46-
func get<RowType>(sql: String, parameters: [Any?]?, mapper: @escaping (any SqlCursor) throws -> RowType) throws -> RowType {
47-
return try wrapQueryCursorTypedSync(
52+
func get<RowType>(
53+
sql: String,
54+
parameters: [Any?]?,
55+
mapper: @escaping (any SqlCursor) throws -> RowType
56+
) throws -> RowType {
57+
return try wrapQueryCursorTyped(
4858
mapper: mapper,
4959
executor: { wrappedMapper in
5060
try self.ctx.get(
@@ -58,6 +68,22 @@ class KotlinConnectionContext: ConnectionContext {
5868
}
5969
}
6070

71+
class KotlinConnectionContext: KotlinConnectionContextProtocol {
72+
let ctx: PowerSyncKotlin.ConnectionContext
73+
74+
init(ctx: PowerSyncKotlin.ConnectionContext) {
75+
self.ctx = ctx
76+
}
77+
}
78+
79+
class KotlinTransactionContext: Transaction, KotlinConnectionContextProtocol {
80+
let ctx: PowerSyncKotlin.ConnectionContext
81+
82+
init(ctx: PowerSyncKotlin.PowerSyncTransaction) {
83+
self.ctx = ctx
84+
}
85+
}
86+
6187
// Allows nil values to be passed to the Kotlin [Any] params
6288
internal func mapParameters(_ parameters: [Any?]?) -> [Any] {
6389
parameters?.map { item in
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import PowerSyncKotlin
22

33
struct KotlinCrudBatch: CrudBatch {
4-
let base: PowerSyncKotlin.CrudBatch
4+
let batch: PowerSyncKotlin.CrudBatch
55
let crud: [CrudEntry]
66

7-
init (_ base: PowerSyncKotlin.CrudBatch) throws {
8-
self.base = base
9-
self.crud = try base.crud.map { try KotlinCrudEntry($0) }
7+
init (batch: PowerSyncKotlin.CrudBatch) throws {
8+
self.batch = batch
9+
self.crud = try batch.crud.map { try KotlinCrudEntry(
10+
entry: $0
11+
) }
1012
}
1113

1214
var hasMore: Bool {
13-
base.hasMore
15+
batch.hasMore
1416
}
1517

1618
func complete(writeCheckpoint: String?) async throws {
17-
_ = try await base.complete.invoke(p1: writeCheckpoint)
19+
_ = try await batch.complete.invoke(p1: writeCheckpoint)
1820
}
1921
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
import PowerSyncKotlin
22

33
struct KotlinCrudEntry : CrudEntry {
4-
let base: PowerSyncKotlin.CrudEntry
4+
let entry: PowerSyncKotlin.CrudEntry
55
let op: UpdateType
66

7-
init (_ base: PowerSyncKotlin.CrudEntry) throws {
8-
self.base = base
9-
self.op = try UpdateType.fromString(base.op.name)
7+
init (entry: PowerSyncKotlin.CrudEntry) throws {
8+
self.entry = entry
9+
self.op = try UpdateType.fromString(entry.op.name)
1010
}
1111

1212
var id: String {
13-
base.id
13+
entry.id
1414
}
1515

16-
var clientId: Int32 {
17-
base.clientId
16+
var clientId: Int64 {
17+
Int64(entry.clientId)
1818
}
1919

2020
var table: String {
21-
base.table
21+
entry.table
2222
}
2323

24-
var transactionId: Int32? {
25-
base.transactionId?.int32Value
24+
var transactionId: Int64? {
25+
entry.transactionId?.int64Value
2626
}
2727

2828
var opData: [String : String?]? {
2929
/// Kotlin represents this as Map<String, String?>, but this is
3030
/// converted to [String: Any] by SKIEE
31-
base.opData?.mapValues { $0 as? String }
31+
entry.opData?.mapValues { $0 as? String }
3232
}
3333
}

0 commit comments

Comments
 (0)