Skip to content

Commit 6bbfc78

Browse files
authored
Encryption part 2: Use common SDK for Swift build (#277)
* Use common SDK for Swift build * Simplify
1 parent 2116946 commit 6bbfc78

File tree

4 files changed

+69
-29
lines changed

4 files changed

+69
-29
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.powersync.db
2+
3+
import androidx.sqlite.SQLiteConnection
4+
import com.powersync.ExperimentalPowerSyncAPI
5+
import com.powersync.PersistentConnectionFactory
6+
import com.powersync.PowerSyncException
7+
import com.powersync.resolvePowerSyncLoadableExtensionPath
8+
import com.powersync.sqlite.Database
9+
10+
/**
11+
* A [PersistentConnectionFactory] implementation delegating to static `sqlite3_` invocations through cinterop.
12+
*/
13+
public abstract class NativeConnectionFactory : PersistentConnectionFactory {
14+
@OptIn(ExperimentalPowerSyncAPI::class)
15+
override fun openConnection(
16+
path: String,
17+
openFlags: Int,
18+
): SQLiteConnection {
19+
// On some platforms, most notably watchOS, there's no dynamic extension loading and the core extension is
20+
// registered via sqlite3_auto_extension.
21+
val extensionPath = resolvePowerSyncLoadableExtensionPath()
22+
val db = Database.open(path, openFlags)
23+
24+
if (extensionPath != null) {
25+
try {
26+
db.loadExtension(extensionPath, "sqlite3_powersync_init")
27+
} catch (e: PowerSyncException) {
28+
db.close()
29+
throw e
30+
}
31+
}
32+
33+
return db
34+
}
35+
36+
override fun openInMemoryConnection(): SQLiteConnection = openConnection(":memory:", 0x02)
37+
}
Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,10 @@
11
package com.powersync
22

3-
import androidx.sqlite.SQLiteConnection
4-
import com.powersync.sqlite.Database
3+
import com.powersync.db.NativeConnectionFactory
54

65
@Suppress(names = ["EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING"])
7-
public actual class DatabaseDriverFactory : PersistentConnectionFactory {
6+
public actual class DatabaseDriverFactory : NativeConnectionFactory() {
87
actual override fun resolveDefaultDatabasePath(dbFilename: String): String = appleDefaultDatabasePath(dbFilename)
9-
10-
@OptIn(ExperimentalPowerSyncAPI::class)
11-
actual override fun openConnection(
12-
path: String,
13-
openFlags: Int,
14-
): SQLiteConnection {
15-
// On some platforms, most notably watchOS, there's no dynamic extension loading and the core extension is
16-
// registered via sqlite3_auto_extension.
17-
val extensionPath = resolvePowerSyncLoadableExtensionPath()
18-
19-
val db = Database.open(path, openFlags)
20-
extensionPath?.let { path ->
21-
try {
22-
db.loadExtension(path, "sqlite3_powersync_init")
23-
} catch (e: PowerSyncException) {
24-
db.close()
25-
throw e
26-
}
27-
}
28-
29-
return db
30-
}
31-
32-
actual override fun openInMemoryConnection(): SQLiteConnection = openConnection(":memory:", 0x02)
338
}
349

3510
internal actual val inMemoryDriver: InMemoryConnectionFactory = DatabaseDriverFactory()

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
all {

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

Lines changed: 27 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,30 @@ 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 resolveDefaultDatabasePath(dbFilename: String): String = appleDefaultDatabasePath(dbFilename)
22+
23+
override fun openConnection(
24+
path: String,
25+
openFlags: Int,
26+
): SQLiteConnection {
27+
val conn = super.openConnection(path, openFlags)
28+
try {
29+
for (statement in initialStatements) {
30+
conn.execSQL(statement)
31+
}
32+
} catch (e: PowerSyncException) {
33+
conn.close()
34+
throw e
35+
}
36+
37+
return super.openConnection(path, openFlags)
38+
}
39+
}
40+
}
41+
1542
/**
1643
* Helper class designed to bridge SKIEE methods and allow them to throw
1744
* `PowerSyncException`. This is necessary because these exceptions cannot

0 commit comments

Comments
 (0)