Skip to content
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 @@ -2,8 +2,8 @@ package com.powersync

import android.content.Context
import androidx.sqlite.db.SupportSQLiteDatabase
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
import com.powersync.db.internal.InternalSchema
import com.powersync.persistence.driver.AndroidSqliteDriver
import io.requery.android.database.sqlite.RequerySQLiteOpenHelperFactory
import io.requery.android.database.sqlite.SQLiteCustomExtension
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -32,7 +32,7 @@ public actual class DatabaseDriverFactory(
}
}

public actual fun createDriver(
internal actual fun createDriver(
scope: CoroutineScope,
dbFilename: String,
): PsSqlDriver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlinx.coroutines.CoroutineScope

@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
public expect class DatabaseDriverFactory {
public fun createDriver(
internal fun createDriver(
scope: CoroutineScope,
dbFilename: String,
): PsSqlDriver
Expand Down
12 changes: 6 additions & 6 deletions core/src/commonMain/kotlin/com/powersync/PsSqlDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

public class PsSqlDriver(
internal class PsSqlDriver(
private val driver: SqlDriver,
private val scope: CoroutineScope,
) : SqlDriver by driver {
Expand All @@ -19,21 +19,21 @@ public class PsSqlDriver(
// In-memory buffer to store table names before flushing
private val pendingUpdates = mutableSetOf<String>()

public fun updateTable(tableName: String) {
fun updateTable(tableName: String) {
pendingUpdates.add(tableName)
}

public fun clearTableUpdates() {
fun clearTableUpdates() {
pendingUpdates.clear()
}

// Flows on table updates
public fun tableUpdates(): Flow<List<String>> = tableUpdatesFlow.asSharedFlow()
fun tableUpdates(): Flow<List<String>> = tableUpdatesFlow.asSharedFlow()

// Flows on table updates containing a specific table
public fun updatesOnTable(tableName: String): Flow<Unit> = tableUpdates().filter { it.contains(tableName) }.map { }
fun updatesOnTable(tableName: String): Flow<Unit> = tableUpdates().filter { it.contains(tableName) }.map { }

public fun fireTableUpdates() {
fun fireTableUpdates() {
val updates = pendingUpdates.toList()
if (updates.isEmpty()) {
return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.powersync.bucket

import app.cash.sqldelight.db.SqlCursor
import co.touchlab.kermit.Logger
import co.touchlab.stately.concurrency.AtomicBoolean
import com.powersync.db.SqlCursor
import com.powersync.db.crud.CrudEntry
import com.powersync.db.crud.CrudRow
import com.powersync.db.internal.InternalDatabase
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.powersync.db

import app.cash.sqldelight.db.SqlCursor
import co.touchlab.kermit.Logger
import com.powersync.DatabaseDriverFactory
import com.powersync.PowerSyncDatabase
Expand Down
1 change: 0 additions & 1 deletion core/src/commonMain/kotlin/com/powersync/db/Queries.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.powersync.db

import app.cash.sqldelight.db.SqlCursor
import com.powersync.db.internal.PowerSyncTransaction
import kotlinx.coroutines.flow.Flow

Expand Down
25 changes: 25 additions & 0 deletions core/src/commonMain/kotlin/com/powersync/db/SqlCursor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.powersync.db

public interface SqlCursor {
public fun getBoolean(index: Int): Boolean?

public fun getBytes(index: Int): ByteArray?

public fun getDouble(index: Int): Double?

public fun getLong(index: Int): Long?

public fun getString(index: Int): String?

public fun columnName(index: Int): String?

public val columnCount: Int

public val columnNames: Map<String, Int>
}

public fun SqlCursor.getBoolean(name: String): Boolean? = columnNames[name]?.let { getBoolean(it) }
public fun SqlCursor.getBytes(name: String): ByteArray? = columnNames[name]?.let { getBytes(it) }
public fun SqlCursor.getDouble(name: String): Double? = columnNames[name]?.let { getDouble(it) }
public fun SqlCursor.getLong(name: String): Long? = columnNames[name]?.let { getLong(it) }
public fun SqlCursor.getString(name: String): String? = columnNames[name]?.let { getString(it) }
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import app.cash.sqldelight.Query
import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlCursor
import app.cash.sqldelight.db.SqlPreparedStatement
import com.persistence.PowersyncQueries
import com.powersync.PsSqlDriver
import com.powersync.db.SqlCursor
import com.powersync.persistence.PsDatabase
import com.powersync.utils.JsonUtil
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -184,8 +184,8 @@ internal class InternalDatabaseImpl(
parameters: Int = 0,
binders: (SqlPreparedStatement.() -> Unit)? = null,
): ExecutableQuery<T> =
object : ExecutableQuery<T>(mapper) {
override fun <R> execute(mapper: (SqlCursor) -> QueryResult<R>): QueryResult<R> =
object : ExecutableQuery<T>(wrapperMapper(mapper)) {
override fun <R> execute(mapper: (app.cash.sqldelight.db.SqlCursor) -> QueryResult<R>): QueryResult<R> =
driver.executeQuery(null, query, mapper, parameters, binders)
}

Expand All @@ -196,8 +196,8 @@ internal class InternalDatabaseImpl(
binders: (SqlPreparedStatement.() -> Unit)? = null,
tables: Set<String> = setOf(),
): Query<T> =
object : Query<T>(mapper) {
override fun <R> execute(mapper: (SqlCursor) -> QueryResult<R>): QueryResult<R> =
object : Query<T>(wrapperMapper(mapper)) {
override fun <R> execute(mapper: (app.cash.sqldelight.db.SqlCursor) -> QueryResult<R>): QueryResult<R> =
driver.executeQuery(null, query, mapper, parameters, binders)

override fun addListener(listener: Listener) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.powersync.db.internal

import app.cash.sqldelight.db.SqlCursor
import com.powersync.db.SqlCursor

public interface PowerSyncTransaction {
public fun execute(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.powersync.db.internal

import app.cash.sqldelight.db.SqlCursor
import com.powersync.persistence.driver.ColNamesSqlCursor

internal class SqlCursorWrapper(val realCursor: ColNamesSqlCursor) : com.powersync.db.SqlCursor {
override fun getBoolean(index: Int): Boolean? = realCursor.getBoolean(index)

override fun getBytes(index: Int): ByteArray? = realCursor.getBytes(index)

override fun getDouble(index: Int): Double? = realCursor.getDouble(index)

override fun getLong(index: Int): Long? = realCursor.getLong(index)

override fun getString(index: Int): String? = realCursor.getString(index)

override fun columnName(index: Int): String? = realCursor.columnName(index)

override val columnCount: Int
get() = realCursor.columnCount

override val columnNames: Map<String, Int> by lazy {
val map = HashMap<String, Int>(this.columnCount)
for (i in 0 until columnCount) {
val key = columnName(i)
if (key == null) {
continue
}
if (map.containsKey(key)) {
var index = 1
val basicKey = "$key&JOIN"
var finalKey = basicKey + index
while (map.containsKey(finalKey)) {
finalKey = basicKey + ++index
}
map[finalKey] = i
} else {
map[key] = i
}
}
map
}
}

internal fun <T> wrapperMapper(mapper: (com.powersync.db.SqlCursor) -> T): (SqlCursor) -> T {
return { realCursor -> mapper(SqlCursorWrapper(realCursor as ColNamesSqlCursor)) }
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.powersync

import app.cash.sqldelight.driver.native.NativeSqliteDriver
import app.cash.sqldelight.driver.native.wrapConnection
import co.touchlab.sqliter.DatabaseConfiguration
import co.touchlab.sqliter.DatabaseConnection
import com.powersync.db.internal.InternalSchema
import com.powersync.persistence.driver.NativeSqliteDriver
import com.powersync.persistence.driver.wrapConnection
import com.powersync.sqlite.core.init_powersync_sqlite_extension
import com.powersync.sqlite.core.sqlite3_commit_hook
import com.powersync.sqlite.core.sqlite3_rollback_hook
Expand Down Expand Up @@ -46,7 +46,7 @@ public actual class DatabaseDriverFactory {
}
}

public actual fun createDriver(
internal actual fun createDriver(
scope: CoroutineScope,
dbFilename: String,
): PsSqlDriver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public actual class DatabaseDriverFactory {
}
}

public actual fun createDriver(
internal actual fun createDriver(
scope: CoroutineScope,
dbFilename: String,
): PsSqlDriver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import app.cash.sqldelight.db.SqlCursor
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlPreparedStatement
import app.cash.sqldelight.db.SqlSchema
import app.cash.sqldelight.driver.jdbc.JdbcPreparedStatement
import com.powersync.persistence.driver.JdbcPreparedStatement
import org.sqlite.SQLiteConnection
import java.nio.file.Path
import java.sql.DriverManager
Expand Down
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ junit = "4.13.2"

compose = "1.6.11"
compose-preview = "1.7.2"
androidxSqlite = "2.4.0"

# plugins
android-gradle-plugin = "8.5.1"
Expand Down Expand Up @@ -88,6 +89,8 @@ stately-concurrency = { module = "co.touchlab:stately-concurrency", version.ref
supabase-client = { module = "io.github.jan-tennert.supabase:postgrest-kt", version.ref = "supabase" }
supabase-auth = { module = "io.github.jan-tennert.supabase:auth-kt", version.ref = "supabase" }

androidx-sqliteFramework = { module = "androidx.sqlite:sqlite-framework", version.ref = "androidxSqlite" }

# Sample - Android
androidx-core = { group = "androidx.core", name = "core-ktx", version.ref = "androidx-core" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidx-appcompat" }
Expand Down
1 change: 1 addition & 0 deletions persistence/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ kotlin {
api(libs.sqldelight.driver.android)
api(libs.powersync.sqlite.core.android)
api(libs.requery.sqlite.android)
implementation(libs.androidx.sqliteFramework)
}

jvmMain.dependencies {
Expand Down
Loading