Skip to content

Commit c7adbab

Browse files
committed
Make addPowerSyncExtension public
1 parent f532ba9 commit c7adbab

File tree

5 files changed

+53
-51
lines changed

5 files changed

+53
-51
lines changed

core/src/androidMain/kotlin/com/powersync/DatabaseDriverFactory.android.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import androidx.sqlite.driver.bundled.BundledSQLiteDriver
77
public actual class DatabaseDriverFactory(
88
private val context: Context,
99
) {
10-
internal actual fun addPowerSyncExtension(driver: BundledSQLiteDriver) {
11-
driver.addExtension("libpowersync.so", "sqlite3_powersync_init")
12-
}
13-
1410
internal actual fun resolveDefaultDatabasePath(dbFilename: String): String {
1511
return context.getDatabasePath(dbFilename).path
1612
}
1713
}
14+
15+
public actual fun BundledSQLiteDriver.addPowerSyncExtension() {
16+
addExtension("libpowersync.so", "sqlite3_powersync_init")
17+
}

core/src/appleNonWatchOsMain/kotlin/com/powersync/DatabaseDriverFactory.appleNonWatchOs.kt

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,25 @@ import kotlin.getValue
66

77
@Suppress(names = ["EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING"])
88
public actual class DatabaseDriverFactory {
9-
internal actual fun addPowerSyncExtension(driver: BundledSQLiteDriver) {
10-
driver.addExtension(powerSyncExtensionPath, "sqlite3_powersync_init")
11-
}
12-
139
internal actual fun resolveDefaultDatabasePath(dbFilename: String): String {
1410
return appleDefaultDatabasePath(dbFilename)
1511
}
12+
}
1613

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-
)
14+
public actual fun BundledSQLiteDriver.addPowerSyncExtension() {
15+
addExtension(powerSyncExtensionPath, "sqlite3_powersync_init")
16+
}
2717

28-
// Construct full path to the shared library inside the bundle
29-
bundlePath.let { "$it/powersync-sqlite-core" }
30-
}
31-
}
18+
private val powerSyncExtensionPath: String 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" }
3230
}

core/src/commonMain/kotlin/com/powersync/DatabaseDriverFactory.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,20 @@ import androidx.sqlite.driver.bundled.SQLITE_OPEN_READWRITE
88

99
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
1010
public expect class DatabaseDriverFactory {
11-
internal fun addPowerSyncExtension(driver: BundledSQLiteDriver)
12-
1311
internal fun resolveDefaultDatabasePath(dbFilename: String): String
1412
}
1513

14+
/**
15+
* Registers the PowerSync core extension on connections opened by this [BundledSQLiteDriver].
16+
*
17+
* This method will be invoked by the PowerSync SDK when creating new databases. When using
18+
* [PowerSyncDatabase.opened] with an existing connection pool, you should configure the driver
19+
* backing that pool to load the extension.
20+
*/
21+
@ExperimentalPowerSyncAPI()
22+
public expect fun BundledSQLiteDriver.addPowerSyncExtension()
23+
24+
@OptIn(ExperimentalPowerSyncAPI::class)
1625
internal fun openDatabase(
1726
factory: DatabaseDriverFactory,
1827
dbFilename: String,
@@ -27,8 +36,7 @@ internal fun openDatabase(
2736
factory.resolveDefaultDatabasePath(dbFilename)
2837
}
2938

30-
factory.addPowerSyncExtension(driver)
31-
39+
driver.addPowerSyncExtension()
3240
return driver.open(dbPath, if (readOnly) {
3341
SQLITE_OPEN_READONLY
3442
} else {

core/src/jvmMain/kotlin/com/powersync/DatabaseDriverFactory.jvm.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import androidx.sqlite.driver.bundled.BundledSQLiteDriver
44

55
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING", "SqlNoDataSourceInspection")
66
public actual class DatabaseDriverFactory {
7-
internal actual fun addPowerSyncExtension(driver: BundledSQLiteDriver) {
8-
driver.addExtension(powersyncExtension, "sqlite3_powersync_init")
9-
}
10-
117
internal actual fun resolveDefaultDatabasePath(dbFilename: String): String {
128
return dbFilename
139
}
10+
}
1411

15-
public companion object {
16-
private val powersyncExtension: String = extractLib("powersync")
17-
}
12+
public actual fun BundledSQLiteDriver.addPowerSyncExtension() {
13+
addExtension(powersyncExtension, "sqlite3_powersync_init")
1814
}
15+
16+
private val powersyncExtension: String by lazy { extractLib("powersync") }

core/src/watchosMain/kotlin/com/powersync/DatabaseDriverFactory.watchos.kt

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,26 @@ import com.powersync.static.powersync_init_static
55

66
@Suppress(names = ["EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING"])
77
public actual class DatabaseDriverFactory {
8-
internal actual fun addPowerSyncExtension(driver: BundledSQLiteDriver) {
9-
didLoadExtension
10-
}
11-
128
internal actual fun resolveDefaultDatabasePath(dbFilename: String): String {
139
return appleDefaultDatabasePath(dbFilename)
1410
}
11+
}
1512

16-
private companion object {
17-
val didLoadExtension by lazy {
18-
val rc = powersync_init_static()
19-
if (rc != 0) {
20-
throw PowerSyncException(
21-
"Could not load the PowerSync SQLite core extension",
22-
cause =
23-
Exception(
24-
"Calling powersync_init_static returned result code $rc",
25-
),
26-
)
27-
}
13+
public actual fun BundledSQLiteDriver.addPowerSyncExtension() {
14+
didLoadExtension
15+
}
2816

29-
true
30-
}
17+
private val didLoadExtension by lazy {
18+
val rc = powersync_init_static()
19+
if (rc != 0) {
20+
throw PowerSyncException(
21+
"Could not load the PowerSync SQLite core extension",
22+
cause =
23+
Exception(
24+
"Calling powersync_init_static returned result code $rc",
25+
),
26+
)
3127
}
28+
29+
true
3230
}

0 commit comments

Comments
 (0)