Skip to content

Commit cdf6bfe

Browse files
committed
Simplify
1 parent 6fcd27b commit cdf6bfe

File tree

3 files changed

+19
-48
lines changed

3 files changed

+19
-48
lines changed
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package com.powersync.db
22

33
import androidx.sqlite.SQLiteConnection
4+
import com.powersync.ExperimentalPowerSyncAPI
45
import com.powersync.PersistentConnectionFactory
56
import com.powersync.PowerSyncException
7+
import com.powersync.resolvePowerSyncLoadableExtensionPath
68
import com.powersync.sqlite.Database
79

810
/**
911
* A [PersistentConnectionFactory] implementation delegating to static `sqlite3_` invocations through cinterop.
1012
*/
11-
public abstract class NativeConnectionFactory: PersistentConnectionFactory {
12-
override fun openConnection(path: String, openFlags: Int): SQLiteConnection {
13-
val extensionPath = powersyncLoadableExtensionPath()
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()
1422
val db = Database.open(path, openFlags)
1523

1624
if (extensionPath != null) {
@@ -25,14 +33,5 @@ public abstract class NativeConnectionFactory: PersistentConnectionFactory {
2533
return db
2634
}
2735

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?
36+
override fun openInMemoryConnection(): SQLiteConnection = openConnection(":memory:", 0x02)
3837
}
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/src/appleMain/kotlin/com/powersync/SDK.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@ import io.ktor.client.plugins.logging.Logger as KtorLogger
1818
public fun sqlite3DatabaseFactory(initialStatements: List<String>): PersistentConnectionFactory {
1919
@OptIn(ExperimentalPowerSyncAPI::class)
2020
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-
}
21+
override fun resolveDefaultDatabasePath(dbFilename: String): String = appleDefaultDatabasePath(dbFilename)
2822

29-
override fun openConnection(path: String, openFlags: Int): SQLiteConnection {
23+
override fun openConnection(
24+
path: String,
25+
openFlags: Int,
26+
): SQLiteConnection {
3027
val conn = super.openConnection(path, openFlags)
3128
try {
3229
for (statement in initialStatements) {

0 commit comments

Comments
 (0)