Skip to content

Commit b91611d

Browse files
committed
Use common SDK for Swift build
1 parent 15e1039 commit b91611d

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.powersync.db
2+
3+
import androidx.sqlite.SQLiteConnection
4+
import com.powersync.PersistentConnectionFactory
5+
import com.powersync.PowerSyncException
6+
import com.powersync.sqlite.Database
7+
8+
/**
9+
* A [PersistentConnectionFactory] implementation delegating to static `sqlite3_` invocations through cinterop.
10+
*/
11+
public abstract class NativeConnectionFactory: PersistentConnectionFactory {
12+
override fun openConnection(path: String, openFlags: Int): SQLiteConnection {
13+
val extensionPath = powersyncLoadableExtensionPath()
14+
val db = Database.open(path, openFlags)
15+
16+
if (extensionPath != null) {
17+
try {
18+
db.loadExtension(extensionPath, "sqlite3_powersync_init")
19+
} catch (e: PowerSyncException) {
20+
db.close()
21+
throw e
22+
}
23+
}
24+
25+
return db
26+
}
27+
28+
override fun openInMemoryConnection(): SQLiteConnection {
29+
return openConnection(":memory:", 0x02)
30+
}
31+
32+
/**
33+
* If the core extension should be loaded as a dynamic library, returns its path.
34+
*
35+
* Otherwise, installs the core extension as a static extension and returns null.
36+
*/
37+
protected abstract fun powersyncLoadableExtensionPath(): String?
38+
}

internal/PowerSyncKotlin/build.gradle.kts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ kotlin {
2525
baseName = "PowerSyncKotlin"
2626
xcf.add(this)
2727

28-
export(project(":core"))
28+
export(projects.common)
2929
isStatic = true
3030

3131
binaryOption("bundleId", "PowerSyncKotlin")
@@ -45,8 +45,9 @@ kotlin {
4545

4646
sourceSets {
4747
commonMain.dependencies {
48-
api(project(":core"))
48+
api(projects.common)
4949
implementation(libs.ktor.client.logging)
50+
implementation(libs.ktor.client.darwin)
5051
}
5152
}
5253
}

internal/PowerSyncKotlin/src/appleMain/kotlin/com/powersync/SDK.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
package com.powersync
44

5+
import androidx.sqlite.SQLiteConnection
6+
import androidx.sqlite.execSQL
7+
import com.powersync.db.NativeConnectionFactory
58
import com.powersync.db.crud.CrudTransaction
69
import com.powersync.sync.SyncClientConfiguration
710
import com.powersync.sync.SyncOptions
@@ -12,6 +15,33 @@ import kotlinx.coroutines.flow.catch
1215
import kotlinx.coroutines.flow.map
1316
import io.ktor.client.plugins.logging.Logger as KtorLogger
1417

18+
public fun sqlite3DatabaseFactory(initialStatements: List<String>): PersistentConnectionFactory {
19+
@OptIn(ExperimentalPowerSyncAPI::class)
20+
return object : NativeConnectionFactory() {
21+
override fun powersyncLoadableExtensionPath(): String? {
22+
return resolvePowerSyncLoadableExtensionPath()
23+
}
24+
25+
override fun resolveDefaultDatabasePath(dbFilename: String): String {
26+
return appleDefaultDatabasePath(dbFilename)
27+
}
28+
29+
override fun openConnection(path: String, openFlags: Int): SQLiteConnection {
30+
val conn = super.openConnection(path, openFlags)
31+
try {
32+
for (statement in initialStatements) {
33+
conn.execSQL(statement)
34+
}
35+
} catch (e: PowerSyncException) {
36+
conn.close()
37+
throw e
38+
}
39+
40+
return super.openConnection(path, openFlags)
41+
}
42+
}
43+
}
44+
1545
/**
1646
* Helper class designed to bridge SKIEE methods and allow them to throw
1747
* `PowerSyncException`. This is necessary because these exceptions cannot

0 commit comments

Comments
 (0)