Skip to content

Use androidx sqlite bindings internally #230

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Changelog

## 1.3.1 (unreleased)
## 1.4.0 (unreleased)

* Update SQLite to 3.50.3.
* Remove internal SQLDelight and SQLiter dependencies.
* Android: Ensure JNI libraries are 16KB-aligned.
* Add `rawConnection` getter to `ConnectionContext`, which is a `SQLiteConnection` instance from
`androidx.sqlite` that can be used to step through statements in a custom way.

## 1.3.0

Expand Down
2 changes: 0 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ plugins {
alias(libs.plugins.skie) apply false
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.sqldelight) apply false
alias(libs.plugins.grammarKitComposer) apply false
alias(libs.plugins.mavenPublishPlugin) apply false
alias(libs.plugins.downloadPlugin) apply false
alias(libs.plugins.kotlinter) apply false
Expand Down
1 change: 0 additions & 1 deletion compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ kotlin {
sourceSets {
commonMain.dependencies {
api(project(":core"))
implementation(project(":persistence"))
implementation(compose.runtime)
}
androidMain.dependencies {
Expand Down
8 changes: 6 additions & 2 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ kotlin {
all {
languageSettings {
optIn("kotlinx.cinterop.ExperimentalForeignApi")
optIn("kotlin.experimental.ExperimentalObjCRefinement")
}
}

Expand All @@ -201,6 +202,9 @@ kotlin {
}

dependencies {
api(libs.kermit)
api(libs.androidx.sqlite)

implementation(libs.uuid)
implementation(libs.kotlin.stdlib)
implementation(libs.ktor.client.core)
Expand All @@ -213,8 +217,7 @@ kotlin {
implementation(libs.kotlinx.datetime)
implementation(libs.stately.concurrency)
implementation(libs.configuration.annotations)
api(projects.persistence)
api(libs.kermit)
implementation(projects.drivers.common)
}
}

Expand All @@ -233,6 +236,7 @@ kotlin {

appleMain.dependencies {
implementation(libs.ktor.client.darwin)
implementation(projects.staticSqliteDriver)
}

commonTest.dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,79 +1,35 @@
package com.powersync

import android.content.Context
import com.powersync.db.JdbcSqliteDriver
import com.powersync.db.buildDefaultWalProperties
import com.powersync.db.internal.InternalSchema
import com.powersync.db.migrateDriver
import kotlinx.coroutines.CoroutineScope
import org.sqlite.SQLiteCommitListener
import java.util.concurrent.atomic.AtomicBoolean
import androidx.sqlite.SQLiteConnection
import com.powersync.db.loadExtensions
import com.powersync.internal.driver.AndroidDriver
import com.powersync.internal.driver.ConnectionListener
import com.powersync.internal.driver.JdbcConnection

@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
public actual class DatabaseDriverFactory(
private val context: Context,
) {
internal actual fun createDriver(
scope: CoroutineScope,
internal actual fun openDatabase(
dbFilename: String,
dbDirectory: String?,
readOnly: Boolean,
): PsSqlDriver {
val schema = InternalSchema

listener: ConnectionListener?,
): SQLiteConnection {
val dbPath =
if (dbDirectory != null) {
"$dbDirectory/$dbFilename"
} else {
context.getDatabasePath(dbFilename)
"${context.getDatabasePath(dbFilename)}"
}

val properties = buildDefaultWalProperties(readOnly = readOnly)
val isFirst = IS_FIRST_CONNECTION.getAndSet(false)
if (isFirst) {
// Make sure the temp_store_directory points towards a temporary directory we actually
// have access to. Due to sandboxing, the default /tmp/ is inaccessible.
// The temp_store_directory pragma is deprecated and not thread-safe, so we only set it
// on the first connection (it sets a global field and will affect every connection
// opened).
val escapedPath = context.cacheDir.absolutePath.replace("\"", "\"\"")
properties.setProperty("temp_store_directory", "\"$escapedPath\"")
}

val driver =
JdbcSqliteDriver(
url = "jdbc:sqlite:$dbPath",
properties = properties,
)

migrateDriver(driver, schema)

driver.loadExtensions(
val driver = AndroidDriver(context)
val connection = driver.openDatabase(dbPath, readOnly, listener) as JdbcConnection
connection.loadExtensions(
"libpowersync.so" to "sqlite3_powersync_init",
)

val mappedDriver = PsSqlDriver(driver = driver)

driver.connection.database.addUpdateListener { _, _, table, _ ->
mappedDriver.updateTable(table)
}

driver.connection.database.addCommitListener(
object : SQLiteCommitListener {
override fun onCommit() {
// We track transactions manually
}

override fun onRollback() {
mappedDriver.clearTableUpdates()
}
},
)

return mappedDriver
}

private companion object {
val IS_FIRST_CONNECTION = AtomicBoolean(true)
return connection
}
}
Loading
Loading