Skip to content

Commit

Permalink
remove 'executeWrite' and instead make 'execute' always perform a wri…
Browse files Browse the repository at this point in the history
…te query
  • Loading branch information
Manrich121 committed Mar 13, 2024
1 parent e288a97 commit 6036cc3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,6 @@ internal class PowerSyncDatabaseImpl(
return internalDb.execute(sql, parameters)
}

override suspend fun executeWrite(sql: String, parameters: List<Any>?): Long {
return internalDb.executeWrite(sql, parameters)
}

private suspend fun handleWriteCheckpoint(lastTransactionId: Int, writeCheckpoint: String?) {
writeTransaction {
internalDb.queries.deleteEntriesWithIdLessThan(lastTransactionId.toLong())
Expand Down
2 changes: 0 additions & 2 deletions core/src/commonMain/kotlin/com/powersync/db/WriteQueries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ interface WriteQueries {
*/
suspend fun execute(sql: String, parameters: List<Any>? = listOf()): Long

suspend fun executeWrite(sql: String, parameters: List<Any>?): Long

suspend fun <R> writeTransaction(body: suspend SuspendingTransactionWithReturn<R>.() -> R): R

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ class PsInternalDatabase(val driver: PsSqlDriver, private val scope: CoroutineSc
): Long {
val numParams = parameters?.size ?: 0

return createQuery(
return createWriteQuery(
sql,
parameters = numParams,
binders = getBindersFromParams(parameters)
).awaitAsOneOrNull() ?: 0
).awaitAsOneOrNull() ?: 0L
}

override suspend fun <RowType : Any> get(
Expand Down Expand Up @@ -115,12 +115,21 @@ class PsInternalDatabase(val driver: PsSqlDriver, private val scope: CoroutineSc
}


private fun createQuery(
private fun createWriteQuery(
query: String,
parameters: Int = 0,
binders: (SqlPreparedStatement.() -> Unit)? = null,
): ExecutableQuery<Long> {
return createQuery(query, { cursor -> cursor.getLong(0)!! }, parameters, binders)
return object : ExecutableQuery<Long>(mapper = { cursor -> cursor.getLong(0)!! }) {
override fun <R> execute(mapper: (SqlCursor) -> QueryResult<R>): QueryResult<R> {
return driver.execute(
identifier = null,
sql = query,
parameters,
binders
) as QueryResult<R>
}
}
}

private fun <T : Any> createQuery(
Expand Down Expand Up @@ -167,12 +176,6 @@ class PsInternalDatabase(val driver: PsSqlDriver, private val scope: CoroutineSc
return transactor.transactionWithResult(noEnclosing = true, body)
}

override suspend fun executeWrite(sql: String, parameters: List<Any>?): Long {
return writeTransaction {
execute(sql, parameters)
}
}

// Register callback for table updates
private fun tableUpdates(): Flow<List<String>> {
return driver.tableUpdates()
Expand Down

0 comments on commit 6036cc3

Please sign in to comment.