File tree Expand file tree Collapse file tree 4 files changed +69
-29
lines changed
common/src/nativeMain/kotlin/com/powersync/db
core/src/nativeMain/kotlin/com/powersync
src/appleMain/kotlin/com/powersync Expand file tree Collapse file tree 4 files changed +69
-29
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11package 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
3510internal actual val inMemoryDriver: InMemoryConnectionFactory = DatabaseDriverFactory ()
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff line change 22
33package com.powersync
44
5+ import androidx.sqlite.SQLiteConnection
6+ import androidx.sqlite.execSQL
7+ import com.powersync.db.NativeConnectionFactory
58import com.powersync.db.crud.CrudTransaction
69import com.powersync.sync.SyncClientConfiguration
710import com.powersync.sync.SyncOptions
@@ -12,6 +15,30 @@ import kotlinx.coroutines.flow.catch
1215import kotlinx.coroutines.flow.map
1316import 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
You can’t perform that action at this time.
0 commit comments