@@ -5,8 +5,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
5
5
let logger : any LoggerProtocol
6
6
7
7
private let kotlinDatabase : PowerSyncKotlin . PowerSyncDatabase
8
-
9
- var currentStatus : SyncStatus { kotlinDatabase. currentStatus }
8
+ let currentStatus : SyncStatus
10
9
11
10
init (
12
11
schema: Schema ,
@@ -21,6 +20,9 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
21
20
logger: logger. kLogger
22
21
)
23
22
self . logger = logger
23
+ self . currentStatus = KotlinSyncStatus (
24
+ baseStatus: kotlinDatabase. currentStatus
25
+ )
24
26
}
25
27
26
28
func waitForFirstSync( ) async throws {
@@ -55,11 +57,17 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
55
57
}
56
58
57
59
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)
59
64
}
60
65
61
66
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)
63
71
}
64
72
65
73
func getPowerSyncVersion( ) async throws -> String {
@@ -71,117 +79,130 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
71
79
}
72
80
73
81
func disconnectAndClear( clearLocal: Bool = true ) async throws {
74
- try await kotlinDatabase. disconnectAndClear ( clearLocal: clearLocal)
82
+ try await kotlinDatabase. disconnectAndClear (
83
+ clearLocal: clearLocal
84
+ )
75
85
}
76
86
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
+ }
79
94
}
80
95
81
96
func get< RowType> (
82
97
sql: String ,
83
- parameters: [ Any ] ? ,
98
+ parameters: [ Any ? ] ? ,
84
99
mapper: @escaping ( SqlCursor ) -> RowType
85
100
) 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
+ }
91
108
}
92
109
93
110
func get< RowType> (
94
111
sql: String ,
95
- parameters: [ Any ] ? ,
112
+ parameters: [ Any ? ] ? ,
96
113
mapper: @escaping ( SqlCursor ) throws -> RowType
97
114
) 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
+ }
109
122
}
110
123
111
124
func getAll< RowType> (
112
125
sql: String ,
113
- parameters: [ Any ] ? ,
126
+ parameters: [ Any ? ] ? ,
114
127
mapper: @escaping ( SqlCursor ) -> RowType
115
128
) 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
+ }
121
136
}
122
137
123
138
func getAll< RowType> (
124
139
sql: String ,
125
- parameters: [ Any ] ? ,
140
+ parameters: [ Any ? ] ? ,
126
141
mapper: @escaping ( SqlCursor ) throws -> RowType
127
142
) 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
+ }
139
150
}
140
151
141
152
func getOptional< RowType> (
142
153
sql: String ,
143
- parameters: [ Any ] ? ,
154
+ parameters: [ Any ? ] ? ,
144
155
mapper: @escaping ( SqlCursor ) -> RowType
145
156
) 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
+ }
151
164
}
152
165
153
166
func getOptional< RowType> (
154
167
sql: String ,
155
- parameters: [ Any ] ? ,
168
+ parameters: [ Any ? ] ? ,
156
169
mapper: @escaping ( SqlCursor ) throws -> RowType
157
170
) 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
+ }
169
178
}
170
-
179
+
171
180
func watch< RowType> (
172
181
sql: String ,
173
- parameters: [ Any ] ? ,
182
+ parameters: [ Any ? ] ? ,
174
183
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
+ )
177
192
}
178
193
179
194
func watch< RowType> (
180
195
sql: String ,
181
- parameters: [ Any ] ? ,
196
+ parameters: [ Any ? ] ? ,
182
197
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
+ )
185
206
}
186
207
187
208
func watch< RowType> (
@@ -202,18 +223,18 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
202
223
// EXPLAIN statement to prevent crashes in SKIEE
203
224
_ = try await self . kotlinDatabase. getAll (
204
225
sql: " EXPLAIN \( options. sql) " ,
205
- parameters: options. parameters,
226
+ parameters: mapParameters ( options. parameters) ,
206
227
mapper: { _ in " " }
207
228
)
208
229
209
230
// Watching for changes in the database
210
231
for try await values in try self . kotlinDatabase. watch (
211
232
sql: options. sql,
212
- parameters: options. parameters,
233
+ parameters: mapParameters ( options. parameters) ,
213
234
throttleMs: KotlinLong ( value: options. throttleMs) ,
214
235
mapper: { cursor in
215
236
do {
216
- return try options. mapper ( cursor)
237
+ return try options. mapper ( KotlinSqlCursor ( base : cursor) )
217
238
} catch {
218
239
mapperError = error
219
240
return ( )
@@ -247,12 +268,26 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
247
268
}
248
269
}
249
270
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
+ )
252
280
}
253
281
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
+ )
256
291
}
257
292
258
293
func close( ) async throws {
0 commit comments