Skip to content

Commit

Permalink
chore: allow nullable args in reads and writes (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: DominicGBauer <dominic@nomanini.com>
  • Loading branch information
DominicGBauer and DominicGBauer authored Apr 16, 2024
1 parent 67dc62f commit 44a39dd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

/**
* A PowerSync managed database.
Expand Down Expand Up @@ -166,31 +165,31 @@ internal class PowerSyncDatabaseImpl(

override suspend fun <RowType : Any> get(
sql: String,
parameters: List<Any>?,
parameters: List<Any?>?,
mapper: (SqlCursor) -> RowType
): RowType {
return internalDb.get(sql, parameters, mapper)
}

override suspend fun <RowType : Any> getAll(
sql: String,
parameters: List<Any>?,
parameters: List<Any?>?,
mapper: (SqlCursor) -> RowType
): List<RowType> {
return internalDb.getAll(sql, parameters, mapper)
}

override suspend fun <RowType : Any> getOptional(
sql: String,
parameters: List<Any>?,
parameters: List<Any?>?,
mapper: (SqlCursor) -> RowType
): RowType? {
return internalDb.getOptional(sql, parameters, mapper)
}

override fun <RowType : Any> watch(
sql: String,
parameters: List<Any>?,
parameters: List<Any?>?,
mapper: (SqlCursor) -> RowType
): Flow<List<RowType>> {
return internalDb.watch(sql, parameters, mapper)
Expand All @@ -205,7 +204,7 @@ internal class PowerSyncDatabaseImpl(
return internalDb.writeTransaction(body)
}

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

Expand Down
8 changes: 4 additions & 4 deletions core/src/commonMain/kotlin/com/powersync/db/ReadQueries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface ReadQueries {
*/
suspend fun <RowType : Any> get(
sql: String,
parameters: List<Any>? = listOf(),
parameters: List<Any?>? = listOf(),
mapper: (SqlCursor) -> RowType
): RowType

Expand All @@ -20,7 +20,7 @@ interface ReadQueries {
*/
suspend fun <RowType : Any> getAll(
sql: String,
parameters: List<Any>? = listOf(),
parameters: List<Any?>? = listOf(),
mapper: (SqlCursor) -> RowType
): List<RowType>

Expand All @@ -29,7 +29,7 @@ interface ReadQueries {
*/
suspend fun <RowType : Any> getOptional(
sql: String,
parameters: List<Any>? = listOf(),
parameters: List<Any?>? = listOf(),
mapper: (SqlCursor) -> RowType
): RowType?

Expand All @@ -38,7 +38,7 @@ interface ReadQueries {
*/
fun <RowType : Any> watch(
sql: String,
parameters: List<Any>? = listOf(),
parameters: List<Any?>? = listOf(),
mapper: (SqlCursor) -> RowType
): Flow<List<RowType>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface WriteQueries {
/**
* Execute a write query (INSERT, UPDATE, DELETE) and return the number of rows updated for an INSERT/DELETE/UPDATE.
*/
suspend fun execute(sql: String, parameters: List<Any>? = listOf()): Long
suspend fun execute(sql: String, parameters: List<Any?>? = listOf()): Long

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class PsInternalDatabase(val driver: PsSqlDriver, private val scope: CoroutineSc

override suspend fun execute(
sql: String,
parameters: List<Any>?
parameters: List<Any?>?
): Long {
val numParams = parameters?.size ?: 0

Expand All @@ -68,7 +68,7 @@ class PsInternalDatabase(val driver: PsSqlDriver, private val scope: CoroutineSc

override suspend fun <RowType : Any> get(
sql: String,
parameters: List<Any>?,
parameters: List<Any?>?,
mapper: (SqlCursor) -> RowType
): RowType {
return this.createQuery(
Expand All @@ -81,7 +81,7 @@ class PsInternalDatabase(val driver: PsSqlDriver, private val scope: CoroutineSc

override suspend fun <RowType : Any> getAll(
sql: String,
parameters: List<Any>?,
parameters: List<Any?>?,
mapper: (SqlCursor) -> RowType
): List<RowType> {
return this.createQuery(
Expand All @@ -94,7 +94,7 @@ class PsInternalDatabase(val driver: PsSqlDriver, private val scope: CoroutineSc

override suspend fun <RowType : Any> getOptional(
sql: String,
parameters: List<Any>?,
parameters: List<Any?>?,
mapper: (SqlCursor) -> RowType
): RowType? {
return this.createQuery(
Expand All @@ -107,7 +107,7 @@ class PsInternalDatabase(val driver: PsSqlDriver, private val scope: CoroutineSc

override fun <RowType : Any> watch(
sql: String,
parameters: List<Any>?,
parameters: List<Any?>?,
mapper: (SqlCursor) -> RowType
): Flow<List<RowType>> {

Expand Down Expand Up @@ -204,7 +204,7 @@ class PsInternalDatabase(val driver: PsSqlDriver, private val scope: CoroutineSc

private fun getSourceTables(
sql: String,
parameters: List<Any>?,
parameters: List<Any?>?,
): Set<String> {
val rows = createQuery(
query = "EXPLAIN $sql",
Expand Down Expand Up @@ -248,7 +248,7 @@ class PsInternalDatabase(val driver: PsSqlDriver, private val scope: CoroutineSc
)
}

fun getBindersFromParams(parameters: List<Any>?): (SqlPreparedStatement.() -> Unit)? {
fun getBindersFromParams(parameters: List<Any?>?): (SqlPreparedStatement.() -> Unit)? {
if (parameters.isNullOrEmpty()) {
return null
}
Expand All @@ -260,7 +260,11 @@ fun getBindersFromParams(parameters: List<Any>?): (SqlPreparedStatement.() -> Un
is Long -> bindLong(index, parameter)
is Double -> bindDouble(index, parameter)
is ByteArray -> bindBytes(index, parameter)
else -> throw IllegalArgumentException("Unsupported parameter type: ${parameter::class}, at index $index")
else -> {
if(parameter != null) {
throw IllegalArgumentException("Unsupported parameter type: ${parameter::class}, at index $index")
}
}
}
}
}
Expand Down

0 comments on commit 44a39dd

Please sign in to comment.