Skip to content

Commit 1a0bee7

Browse files
chore: make execute throwable in swift (#119)
* chore: make execute throwable in swift * chore: make transaction functions throwable * chore: add working solution so far * fix: type issue * chore: use throwablecallback
1 parent 0cc9bc9 commit 1a0bee7

File tree

6 files changed

+59
-24
lines changed

6 files changed

+59
-24
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22

3+
## 1.0.0-BETA23
4+
5+
* Make `execute` and `PowerSyncTransaction` functions throwable for Swift
6+
37
## 1.0.0-BETA22
8+
49
* Fix `updateHasSynced` internal null pointer exception
510

611
## 1.0.0-BETA21

core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import com.powersync.db.crud.CrudRow
1313
import com.powersync.db.crud.CrudTransaction
1414
import com.powersync.db.internal.InternalDatabaseImpl
1515
import com.powersync.db.internal.InternalTable
16-
import com.powersync.db.internal.PowerSyncTransaction
16+
import com.powersync.db.internal.ThrowableTransactionCallback
1717
import com.powersync.db.schema.Schema
1818
import com.powersync.sync.SyncStatus
1919
import com.powersync.sync.SyncStream
@@ -164,9 +164,9 @@ internal class PowerSyncDatabaseImpl(
164164
}
165165

166166
override suspend fun getNextCrudTransaction(): CrudTransaction? {
167-
return internalDb.readTransaction {
167+
return internalDb.readTransaction { transaction ->
168168
val entry =
169-
bucketStorage.nextCrudItem(this)
169+
bucketStorage.nextCrudItem(transaction)
170170
?: return@readTransaction null
171171

172172
val txId = entry.transactionId
@@ -222,9 +222,9 @@ internal class PowerSyncDatabaseImpl(
222222
mapper: (SqlCursor) -> RowType,
223223
): Flow<List<RowType>> = internalDb.watch(sql, parameters, mapper)
224224

225-
override suspend fun <R> readTransaction(callback: (tx: PowerSyncTransaction) -> R): R = internalDb.writeTransaction(callback)
225+
override suspend fun <R> readTransaction(callback: ThrowableTransactionCallback<R>): R = internalDb.writeTransaction(callback)
226226

227-
override suspend fun <R> writeTransaction(callback: (tx: PowerSyncTransaction) -> R): R = internalDb.writeTransaction(callback)
227+
override suspend fun <R> writeTransaction(callback: ThrowableTransactionCallback<R>): R = internalDb.writeTransaction(callback)
228228

229229
override suspend fun execute(
230230
sql: String,
@@ -235,16 +235,16 @@ internal class PowerSyncDatabaseImpl(
235235
lastTransactionId: Int,
236236
writeCheckpoint: String?,
237237
) {
238-
internalDb.writeTransaction {
238+
internalDb.writeTransaction { transaction ->
239239
internalDb.queries.deleteEntriesWithIdLessThan(lastTransactionId.toLong())
240240

241-
if (writeCheckpoint != null && !bucketStorage.hasCrud(this)) {
242-
execute(
241+
if (writeCheckpoint != null && !bucketStorage.hasCrud(transaction)) {
242+
transaction.execute(
243243
"UPDATE ps_buckets SET target_op = CAST(? AS INTEGER) WHERE name='\$local'",
244244
listOf(writeCheckpoint),
245245
)
246246
} else {
247-
execute(
247+
transaction.execute(
248248
"UPDATE ps_buckets SET target_op = CAST(? AS INTEGER) WHERE name='\$local'",
249249
listOf(bucketStorage.getMaxOpId()),
250250
)

core/src/commonMain/kotlin/com/powersync/db/Queries.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.powersync.db
22

33
import com.powersync.PowerSyncException
4-
import com.powersync.db.internal.PowerSyncTransaction
4+
import com.powersync.db.internal.ThrowableTransactionCallback
55
import kotlinx.coroutines.flow.Flow
66
import kotlin.coroutines.cancellation.CancellationException
77

@@ -58,8 +58,8 @@ public interface Queries {
5858
): Flow<List<RowType>>
5959

6060
@Throws(PowerSyncException::class, CancellationException::class)
61-
public suspend fun <R> writeTransaction(callback: (PowerSyncTransaction) -> R): R
61+
public suspend fun <R> writeTransaction(callback: ThrowableTransactionCallback<R>): R
6262

6363
@Throws(PowerSyncException::class, CancellationException::class)
64-
public suspend fun <R> readTransaction(callback: (PowerSyncTransaction) -> R): R
64+
public suspend fun <R> readTransaction(callback: ThrowableTransactionCallback<R>): R
6565
}

core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import app.cash.sqldelight.coroutines.mapToList
77
import app.cash.sqldelight.db.QueryResult
88
import app.cash.sqldelight.db.SqlPreparedStatement
99
import com.persistence.PowersyncQueries
10+
import com.powersync.PowerSyncException
1011
import com.powersync.PsSqlDriver
1112
import com.powersync.db.SqlCursor
1213
import com.powersync.db.runWrapped
1314
import com.powersync.persistence.PsDatabase
1415
import com.powersync.utils.JsonUtil
16+
import kotlinx.coroutines.CancellationException
1517
import kotlinx.coroutines.CoroutineScope
1618
import kotlinx.coroutines.Dispatchers
1719
import kotlinx.coroutines.FlowPreview
@@ -91,13 +93,15 @@ internal class InternalDatabaseImpl(
9193
): Long {
9294
val numParams = parameters?.size ?: 0
9395

94-
return driver
95-
.execute(
96-
identifier = null,
97-
sql = sql,
98-
parameters = numParams,
99-
binders = getBindersFromParams(parameters),
100-
).value
96+
return runWrapped {
97+
driver
98+
.execute(
99+
identifier = null,
100+
sql = sql,
101+
parameters = numParams,
102+
binders = getBindersFromParams(parameters),
103+
).value
104+
}
101105
}
102106

103107
override suspend fun <RowType : Any> get(
@@ -214,17 +218,29 @@ internal class InternalDatabaseImpl(
214218
}
215219
}
216220

217-
override suspend fun <R> readTransaction(callback: PowerSyncTransaction.() -> R): R =
221+
override suspend fun <R> readTransaction(callback: ThrowableTransactionCallback<R>): R =
218222
withContext(dbContext) {
219223
transactor.transactionWithResult(noEnclosing = true) {
220-
callback(transaction)
224+
runWrapped {
225+
val result = callback.execute(transaction)
226+
if (result is PowerSyncException) {
227+
throw result
228+
}
229+
result
230+
}
221231
}
222232
}
223233

224-
override suspend fun <R> writeTransaction(callback: PowerSyncTransaction.() -> R): R =
234+
override suspend fun <R> writeTransaction(callback: ThrowableTransactionCallback<R>): R =
225235
withContext(dbContext) {
226236
transactor.transactionWithResult(noEnclosing = true) {
227-
callback(transaction)
237+
runWrapped {
238+
val result = callback.execute(transaction)
239+
if (result is PowerSyncException) {
240+
throw result
241+
}
242+
result
243+
}
228244
}
229245
}
230246

@@ -331,3 +347,11 @@ internal fun getBindersFromParams(parameters: List<Any?>?): (SqlPreparedStatemen
331347
}
332348
}
333349
}
350+
351+
/**
352+
* Kotlin allows SAM (Single Abstract Method) interfaces to be treated like lambda expressions.
353+
*/
354+
public fun interface ThrowableTransactionCallback<R> {
355+
@Throws(PowerSyncException::class, CancellationException::class)
356+
public fun execute(transaction: PowerSyncTransaction): R
357+
}

core/src/commonMain/kotlin/com/powersync/db/internal/PowerSyncTransaction.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
package com.powersync.db.internal
22

3+
import com.powersync.PowerSyncException
34
import com.powersync.db.SqlCursor
5+
import kotlin.coroutines.cancellation.CancellationException
46

57
public interface PowerSyncTransaction {
8+
@Throws(PowerSyncException::class, CancellationException::class)
69
public fun execute(
710
sql: String,
811
parameters: List<Any?>? = listOf(),
912
): Long
1013

14+
@Throws(PowerSyncException::class, CancellationException::class)
1115
public fun <RowType : Any> getOptional(
1216
sql: String,
1317
parameters: List<Any?>? = listOf(),
1418
mapper: (SqlCursor) -> RowType,
1519
): RowType?
1620

21+
@Throws(PowerSyncException::class, CancellationException::class)
1722
public fun <RowType : Any> getAll(
1823
sql: String,
1924
parameters: List<Any?>? = listOf(),
2025
mapper: (SqlCursor) -> RowType,
2126
): List<RowType>
2227

28+
@Throws(PowerSyncException::class, CancellationException::class)
2329
public fun <RowType : Any> get(
2430
sql: String,
2531
parameters: List<Any?>? = listOf(),

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ development=true
1717
RELEASE_SIGNING_ENABLED=true
1818
# Library config
1919
GROUP=com.powersync
20-
LIBRARY_VERSION=1.0.0-BETA22
20+
LIBRARY_VERSION=1.0.0-BETA23
2121
GITHUB_REPO=https://github.com/powersync-ja/powersync-kotlin.git
2222
# POM
2323
POM_URL=https://github.com/powersync-ja/powersync-kotlin/

0 commit comments

Comments
 (0)