Skip to content

chore: make execute throwable in swift #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

## 1.0.0-BETA23

* Make `execute` and `PowerSyncTransaction` functions throwable for Swift

## 1.0.0-BETA22

* Fix `updateHasSynced` internal null pointer exception

## 1.0.0-BETA21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.powersync.db.crud.CrudRow
import com.powersync.db.crud.CrudTransaction
import com.powersync.db.internal.InternalDatabaseImpl
import com.powersync.db.internal.InternalTable
import com.powersync.db.internal.PowerSyncTransaction
import com.powersync.db.internal.ThrowableTransactionCallback
import com.powersync.db.schema.Schema
import com.powersync.sync.SyncStatus
import com.powersync.sync.SyncStream
Expand Down Expand Up @@ -164,9 +164,9 @@ internal class PowerSyncDatabaseImpl(
}

override suspend fun getNextCrudTransaction(): CrudTransaction? {
return internalDb.readTransaction {
return internalDb.readTransaction { transaction ->
val entry =
bucketStorage.nextCrudItem(this)
bucketStorage.nextCrudItem(transaction)
?: return@readTransaction null

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

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

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

override suspend fun execute(
sql: String,
Expand All @@ -235,16 +235,16 @@ internal class PowerSyncDatabaseImpl(
lastTransactionId: Int,
writeCheckpoint: String?,
) {
internalDb.writeTransaction {
internalDb.writeTransaction { transaction ->
internalDb.queries.deleteEntriesWithIdLessThan(lastTransactionId.toLong())

if (writeCheckpoint != null && !bucketStorage.hasCrud(this)) {
execute(
if (writeCheckpoint != null && !bucketStorage.hasCrud(transaction)) {
transaction.execute(
"UPDATE ps_buckets SET target_op = CAST(? AS INTEGER) WHERE name='\$local'",
listOf(writeCheckpoint),
)
} else {
execute(
transaction.execute(
"UPDATE ps_buckets SET target_op = CAST(? AS INTEGER) WHERE name='\$local'",
listOf(bucketStorage.getMaxOpId()),
)
Expand Down
6 changes: 3 additions & 3 deletions core/src/commonMain/kotlin/com/powersync/db/Queries.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.powersync.db

import com.powersync.PowerSyncException
import com.powersync.db.internal.PowerSyncTransaction
import com.powersync.db.internal.ThrowableTransactionCallback
import kotlinx.coroutines.flow.Flow
import kotlin.coroutines.cancellation.CancellationException

Expand Down Expand Up @@ -58,8 +58,8 @@ public interface Queries {
): Flow<List<RowType>>

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

@Throws(PowerSyncException::class, CancellationException::class)
public suspend fun <R> readTransaction(callback: (PowerSyncTransaction) -> R): R
public suspend fun <R> readTransaction(callback: ThrowableTransactionCallback<R>): R
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import app.cash.sqldelight.coroutines.mapToList
import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlPreparedStatement
import com.persistence.PowersyncQueries
import com.powersync.PowerSyncException
import com.powersync.PsSqlDriver
import com.powersync.db.SqlCursor
import com.powersync.db.runWrapped
import com.powersync.persistence.PsDatabase
import com.powersync.utils.JsonUtil
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview
Expand Down Expand Up @@ -91,13 +93,15 @@ internal class InternalDatabaseImpl(
): Long {
val numParams = parameters?.size ?: 0

return driver
.execute(
identifier = null,
sql = sql,
parameters = numParams,
binders = getBindersFromParams(parameters),
).value
return runWrapped {
driver
.execute(
identifier = null,
sql = sql,
parameters = numParams,
binders = getBindersFromParams(parameters),
).value
}
}

override suspend fun <RowType : Any> get(
Expand Down Expand Up @@ -214,17 +218,29 @@ internal class InternalDatabaseImpl(
}
}

override suspend fun <R> readTransaction(callback: PowerSyncTransaction.() -> R): R =
override suspend fun <R> readTransaction(callback: ThrowableTransactionCallback<R>): R =
withContext(dbContext) {
transactor.transactionWithResult(noEnclosing = true) {
callback(transaction)
runWrapped {
val result = callback.execute(transaction)
if (result is PowerSyncException) {
throw result
}
result
}
}
}

override suspend fun <R> writeTransaction(callback: PowerSyncTransaction.() -> R): R =
override suspend fun <R> writeTransaction(callback: ThrowableTransactionCallback<R>): R =
withContext(dbContext) {
transactor.transactionWithResult(noEnclosing = true) {
callback(transaction)
runWrapped {
val result = callback.execute(transaction)
if (result is PowerSyncException) {
throw result
}
result
}
}
}

Expand Down Expand Up @@ -331,3 +347,11 @@ internal fun getBindersFromParams(parameters: List<Any?>?): (SqlPreparedStatemen
}
}
}

/**
* Kotlin allows SAM (Single Abstract Method) interfaces to be treated like lambda expressions.
*/
public fun interface ThrowableTransactionCallback<R> {
@Throws(PowerSyncException::class, CancellationException::class)
public fun execute(transaction: PowerSyncTransaction): R
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package com.powersync.db.internal

import com.powersync.PowerSyncException
import com.powersync.db.SqlCursor
import kotlin.coroutines.cancellation.CancellationException

public interface PowerSyncTransaction {
@Throws(PowerSyncException::class, CancellationException::class)
public fun execute(
sql: String,
parameters: List<Any?>? = listOf(),
): Long

@Throws(PowerSyncException::class, CancellationException::class)
public fun <RowType : Any> getOptional(
sql: String,
parameters: List<Any?>? = listOf(),
mapper: (SqlCursor) -> RowType,
): RowType?

@Throws(PowerSyncException::class, CancellationException::class)
public fun <RowType : Any> getAll(
sql: String,
parameters: List<Any?>? = listOf(),
mapper: (SqlCursor) -> RowType,
): List<RowType>

@Throws(PowerSyncException::class, CancellationException::class)
public fun <RowType : Any> get(
sql: String,
parameters: List<Any?>? = listOf(),
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ development=true
RELEASE_SIGNING_ENABLED=true
# Library config
GROUP=com.powersync
LIBRARY_VERSION=1.0.0-BETA22
LIBRARY_VERSION=1.0.0-BETA23
GITHUB_REPO=https://github.com/powersync-ja/powersync-kotlin.git
# POM
POM_URL=https://github.com/powersync-ja/powersync-kotlin/
Expand Down