Skip to content
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

chore: allow nullable args in reads and writes #14

Merged
merged 1 commit into from
Apr 16, 2024
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
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
Loading