Skip to content

Commit 54829bc

Browse files
committed
Delete more driver stuff
1 parent eccb7f7 commit 54829bc

File tree

44 files changed

+490
-1317
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+490
-1317
lines changed

core/build.gradle.kts

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -74,60 +74,6 @@ val downloadPowersyncDesktopBinaries by tasks.registering(Download::class) {
7474
onlyIfModified(true)
7575
}
7676

77-
val sqliteJDBCFolder =
78-
project.layout.buildDirectory
79-
.dir("jdbc")
80-
.get()
81-
82-
val jniLibsFolder = layout.projectDirectory.dir("src/androidMain/jni")
83-
84-
val downloadJDBCJar by tasks.registering(Download::class) {
85-
val version =
86-
libs.versions.sqlite.jdbc
87-
.get()
88-
val jar =
89-
"https://github.com/xerial/sqlite-jdbc/releases/download/$version/sqlite-jdbc-$version.jar"
90-
91-
src(jar)
92-
dest(sqliteJDBCFolder.file("jdbc.jar"))
93-
onlyIfModified(true)
94-
}
95-
96-
val extractJDBCJNI by tasks.registering(Copy::class) {
97-
dependsOn(downloadJDBCJar)
98-
99-
from(
100-
zipTree(downloadJDBCJar.get().dest).matching {
101-
include("org/sqlite/native/Linux-Android/**")
102-
},
103-
)
104-
105-
into(sqliteJDBCFolder.dir("jni"))
106-
}
107-
108-
val moveJDBCJNIFiles by tasks.registering(Copy::class) {
109-
dependsOn(extractJDBCJNI)
110-
111-
val abiMapping =
112-
mapOf(
113-
"aarch64" to "arm64-v8a",
114-
"arm" to "armeabi-v7a",
115-
"x86_64" to "x86_64",
116-
"x86" to "x86",
117-
)
118-
119-
abiMapping.forEach { (sourceABI, androidABI) ->
120-
from(sqliteJDBCFolder.dir("jni/org/sqlite/native/Linux-Android/$sourceABI")) {
121-
include("*.so")
122-
eachFile {
123-
path = "$androidABI/$name"
124-
}
125-
}
126-
}
127-
128-
into(jniLibsFolder) // Move everything into the base jniLibs folder
129-
}
130-
13177
val generateVersionConstant by tasks.registering {
13278
val target = project.layout.buildDirectory.dir("generated/constants")
13379
val packageName = "com.powersync.build"
@@ -193,9 +139,6 @@ kotlin {
193139

194140
val commonJava by creating {
195141
dependsOn(commonMain.get())
196-
dependencies {
197-
implementation(libs.sqlite.jdbc)
198-
}
199142
}
200143

201144
commonMain.configure {
@@ -204,7 +147,7 @@ kotlin {
204147
}
205148

206149
dependencies {
207-
api(libs.androidx.sqlite)
150+
api(libs.androidx.sqlite.sqlite)
208151

209152
implementation(libs.uuid)
210153
implementation(libs.kotlin.stdlib)
@@ -215,7 +158,7 @@ kotlin {
215158
implementation(libs.kotlinx.datetime)
216159
implementation(libs.stately.concurrency)
217160
implementation(libs.configuration.annotations)
218-
api(projects.persistence)
161+
implementation(libs.androidx.sqlite.bundled)
219162
api(libs.ktor.client.core)
220163
api(libs.kermit)
221164
}
@@ -236,9 +179,17 @@ kotlin {
236179

237180
appleMain.dependencies {
238181
implementation(libs.ktor.client.darwin)
239-
implementation(projects.staticSqliteDriver)
240182
}
241183

184+
// Common apple targets where we link the core extension dynamically
185+
val appleNonWatchOsMain by creating {
186+
dependsOn(appleMain.get())
187+
}
188+
189+
macosMain.orNull?.dependsOn(appleNonWatchOsMain)
190+
iosMain.orNull?.dependsOn(appleNonWatchOsMain)
191+
tvosMain.orNull?.dependsOn(appleNonWatchOsMain)
192+
242193
commonTest.dependencies {
243194
implementation(libs.kotlin.test)
244195
implementation(libs.test.coroutines)
@@ -298,12 +249,6 @@ android {
298249
ndkVersion = "27.1.12297006"
299250
}
300251

301-
androidComponents.onVariants {
302-
tasks.named("preBuild") {
303-
dependsOn(moveJDBCJNIFiles)
304-
}
305-
}
306-
307252
tasks.named<ProcessResources>(kotlin.jvm().compilations["main"].processResourcesTaskName) {
308253
from(downloadPowersyncDesktopBinaries)
309254
}
Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
11
package com.powersync
22

33
import android.content.Context
4-
import androidx.sqlite.SQLiteConnection
5-
import com.powersync.db.loadExtensions
6-
import com.powersync.internal.driver.AndroidDriver
7-
import com.powersync.internal.driver.ConnectionListener
8-
import com.powersync.internal.driver.JdbcConnection
4+
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
95

106
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
117
public actual class DatabaseDriverFactory(
128
private val context: Context,
139
) {
14-
internal actual fun openDatabase(
15-
dbFilename: String,
16-
dbDirectory: String?,
17-
readOnly: Boolean,
18-
listener: ConnectionListener?,
19-
): SQLiteConnection {
20-
val dbPath =
21-
if (dbDirectory != null) {
22-
"$dbDirectory/$dbFilename"
23-
} else {
24-
"${context.getDatabasePath(dbFilename)}"
25-
}
26-
27-
val driver = AndroidDriver(context)
28-
val connection = driver.openDatabase(dbPath, readOnly, listener) as JdbcConnection
29-
connection.loadExtensions(
30-
"libpowersync.so" to "sqlite3_powersync_init",
31-
)
10+
internal actual fun addPowerSyncExtension(driver: BundledSQLiteDriver) {
11+
driver.addExtension("libpowersync.so", "sqlite3_powersync_init")
12+
}
3213

33-
return connection
14+
internal actual fun resolveDefaultDatabasePath(dbFilename: String): String {
15+
return context.getDatabasePath(dbFilename).path
3416
}
3517
}
Lines changed: 11 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,24 @@
11
package com.powersync
22

3-
import androidx.sqlite.SQLiteConnection
4-
import com.powersync.DatabaseDriverFactory.Companion.powerSyncExtensionPath
5-
import com.powersync.internal.driver.ConnectionListener
6-
import com.powersync.internal.driver.NativeConnection
7-
import com.powersync.internal.driver.NativeDriver
8-
import kotlinx.cinterop.ByteVar
9-
import kotlinx.cinterop.CPointerVar
10-
import kotlinx.cinterop.ExperimentalForeignApi
11-
import kotlinx.cinterop.MemScope
123
import kotlinx.cinterop.UnsafeNumber
13-
import kotlinx.cinterop.alloc
14-
import kotlinx.cinterop.free
15-
import kotlinx.cinterop.nativeHeap
16-
import kotlinx.cinterop.ptr
17-
import kotlinx.cinterop.toKString
18-
import kotlinx.cinterop.value
19-
import kotlinx.io.files.Path
204
import platform.Foundation.NSApplicationSupportDirectory
21-
import platform.Foundation.NSBundle
225
import platform.Foundation.NSFileManager
236
import platform.Foundation.NSSearchPathForDirectoriesInDomains
247
import platform.Foundation.NSUserDomainMask
25-
import sqlite3.SQLITE_OK
26-
import sqlite3.sqlite3_enable_load_extension
27-
import sqlite3.sqlite3_load_extension
288

29-
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
30-
@OptIn(ExperimentalForeignApi::class)
31-
public actual class DatabaseDriverFactory {
32-
internal actual fun openDatabase(
33-
dbFilename: String,
34-
dbDirectory: String?,
35-
readOnly: Boolean,
36-
listener: ConnectionListener?,
37-
): SQLiteConnection {
38-
val directory = dbDirectory ?: defaultDatabaseDirectory()
39-
val path = Path(directory, dbFilename).toString()
40-
val db = NativeDriver().openNativeDatabase(path, readOnly, listener)
9+
@OptIn(UnsafeNumber::class)
10+
internal fun appleDefaultDatabasePath(dbFilename: String): String {
11+
// This needs to be compatible with https://github.com/touchlab/SQLiter/blob/a37bbe7e9c65e6a5a94c5bfcaccdaae55ad2bac9/sqliter-driver/src/appleMain/kotlin/co/touchlab/sqliter/DatabaseFileContext.kt#L36-L51
12+
val paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true)
13+
val documentsDirectory = paths[0] as String
4114

42-
db.loadPowerSyncSqliteCoreExtension()
43-
return db
44-
}
15+
val databaseDirectory = "$documentsDirectory/databases"
4516

46-
internal companion object {
47-
internal val powerSyncExtensionPath by lazy {
48-
// Try and find the bundle path for the SQLite core extension.
49-
val bundlePath =
50-
NSBundle.bundleWithIdentifier("co.powersync.sqlitecore")?.bundlePath
51-
?: // The bundle is not installed in the project
52-
throw PowerSyncException(
53-
"Please install the PowerSync SQLite core extension",
54-
cause = Exception("The `co.powersync.sqlitecore` bundle could not be found in the project."),
55-
)
17+
val fileManager = NSFileManager.defaultManager()
5618

57-
// Construct full path to the shared library inside the bundle
58-
bundlePath.let { "$it/powersync-sqlite-core" }
59-
}
19+
if (!fileManager.fileExistsAtPath(databaseDirectory)) {
20+
fileManager.createDirectoryAtPath(databaseDirectory, true, null, null)
21+
}; // Create folder
6022

61-
@OptIn(UnsafeNumber::class)
62-
private fun defaultDatabaseDirectory(search: String = "databases"): String {
63-
// This needs to be compatible with https://github.com/touchlab/SQLiter/blob/a37bbe7e9c65e6a5a94c5bfcaccdaae55ad2bac9/sqliter-driver/src/appleMain/kotlin/co/touchlab/sqliter/DatabaseFileContext.kt#L36-L51
64-
val paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true)
65-
val documentsDirectory = paths[0] as String
66-
67-
val databaseDirectory = "$documentsDirectory/$search"
68-
69-
val fileManager = NSFileManager.defaultManager()
70-
71-
if (!fileManager.fileExistsAtPath(databaseDirectory)) {
72-
fileManager.createDirectoryAtPath(databaseDirectory, true, null, null)
73-
}; // Create folder
74-
75-
return databaseDirectory
76-
}
77-
}
23+
return databaseDirectory
7824
}
79-
80-
internal fun NativeConnection.loadPowerSyncSqliteCoreExtensionDynamically() {
81-
val ptr = sqlite.getPointer(MemScope())
82-
val extensionPath = powerSyncExtensionPath
83-
84-
// Enable extension loading
85-
// We don't disable this after the fact, this should allow users to load their own extensions
86-
// in future.
87-
val enableResult = sqlite3_enable_load_extension(ptr, 1)
88-
if (enableResult != SQLITE_OK) {
89-
throw PowerSyncException(
90-
"Could not dynamically load the PowerSync SQLite core extension",
91-
cause =
92-
Exception(
93-
"Call to sqlite3_enable_load_extension failed",
94-
),
95-
)
96-
}
97-
98-
// A place to store a potential error message response
99-
val errMsg = nativeHeap.alloc<CPointerVar<ByteVar>>()
100-
val result =
101-
sqlite3_load_extension(ptr, extensionPath, "sqlite3_powersync_init", errMsg.ptr)
102-
val resultingError = errMsg.value
103-
nativeHeap.free(errMsg)
104-
if (result != SQLITE_OK) {
105-
val errorMessage = resultingError?.toKString() ?: "Unknown error"
106-
throw PowerSyncException(
107-
"Could not load the PowerSync SQLite core extension",
108-
cause =
109-
Exception(
110-
"Calling sqlite3_load_extension failed with error: $errorMessage",
111-
),
112-
)
113-
}
114-
}
115-
116-
internal expect fun NativeConnection.loadPowerSyncSqliteCoreExtension()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.powersync
2+
3+
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
4+
import platform.Foundation.NSBundle
5+
import kotlin.getValue
6+
7+
@Suppress(names = ["EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING"])
8+
public actual class DatabaseDriverFactory {
9+
internal actual fun addPowerSyncExtension(driver: BundledSQLiteDriver) {
10+
driver.addExtension(powerSyncExtensionPath, "sqlite3_powersync_init")
11+
}
12+
13+
internal actual fun resolveDefaultDatabasePath(dbFilename: String): String {
14+
return appleDefaultDatabasePath(dbFilename)
15+
}
16+
17+
private companion object {
18+
val powerSyncExtensionPath by lazy {
19+
// Try and find the bundle path for the SQLite core extension.
20+
val bundlePath =
21+
NSBundle.bundleWithIdentifier("co.powersync.sqlitecore")?.bundlePath
22+
?: // The bundle is not installed in the project
23+
throw PowerSyncException(
24+
"Please install the PowerSync SQLite core extension",
25+
cause = Exception("The `co.powersync.sqlitecore` bundle could not be found in the project."),
26+
)
27+
28+
// Construct full path to the shared library inside the bundle
29+
bundlePath.let { "$it/powersync-sqlite-core" }
30+
}
31+
}
32+
}

core/src/commonIntegrationTest/kotlin/com/powersync/DatabaseTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,9 @@ class DatabaseTest {
510510
listOf("a", "a@example.org"),
511511
)
512512

513+
513514
val raw = database.leaseConnection(readOnly = true)
514-
raw.prepare("SELECT * FROM users").use { stmt ->
515+
raw.usePrepared("SELECT * FROM users") { stmt ->
515516
stmt.step() shouldBe true
516517
stmt.getText(1) shouldBe "a"
517518
stmt.getText(2) shouldBe "a@example.org"
@@ -524,7 +525,7 @@ class DatabaseTest {
524525
fun testLeaseWrite() =
525526
databaseTest {
526527
val raw = database.leaseConnection(readOnly = false)
527-
raw.prepare("INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)").use { stmt ->
528+
raw.usePrepared("INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)") { stmt ->
528529
stmt.bindText(1, "name")
529530
stmt.bindText(2, "email")
530531
stmt.step() shouldBe false

core/src/commonJava/kotlin/com/powersync/db/LoadExtension.kt

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)