@@ -12,6 +12,7 @@ import co.touchlab.sqliter.sqlite3.sqlite3_enable_load_extension
1212import co.touchlab.sqliter.sqlite3.sqlite3_load_extension
1313import co.touchlab.sqliter.sqlite3.sqlite3_rollback_hook
1414import co.touchlab.sqliter.sqlite3.sqlite3_update_hook
15+ import com.powersync.DatabaseDriverFactory.Companion.powerSyncExtensionPath
1516import com.powersync.db.internal.InternalSchema
1617import com.powersync.persistence.driver.NativeSqliteDriver
1718import com.powersync.persistence.driver.wrapConnection
@@ -22,6 +23,7 @@ import kotlinx.cinterop.MemScope
2223import kotlinx.cinterop.StableRef
2324import kotlinx.cinterop.alloc
2425import kotlinx.cinterop.asStableRef
26+ import kotlinx.cinterop.free
2527import kotlinx.cinterop.nativeHeap
2628import kotlinx.cinterop.ptr
2729import kotlinx.cinterop.staticCFunction
@@ -132,38 +134,9 @@ public actual class DatabaseDriverFactory {
132134 connection : DatabaseConnection ,
133135 driver : DeferredDriver ,
134136 ) {
135- val ptr = connection.getDbPointer().getPointer(MemScope ())
136- val extensionPath = powerSyncExtensionPath
137-
138- // Enable extension loading
139- // We don't disable this after the fact, this should allow users to load their own extensions
140- // in future.
141- val enableResult = sqlite3_enable_load_extension(ptr, 1 )
142- if (enableResult != SqliteErrorType .SQLITE_OK .code) {
143- throw PowerSyncException (
144- " Could not dynamically load the PowerSync SQLite core extension" ,
145- cause =
146- Exception (
147- " Call to sqlite3_enable_load_extension failed" ,
148- ),
149- )
150- }
151-
152- // A place to store a potential error message response
153- val errMsg = nativeHeap.alloc<CPointerVar <ByteVar >>()
154- val result =
155- sqlite3_load_extension(ptr, extensionPath, " sqlite3_powersync_init" , errMsg.ptr)
156- if (result != SqliteErrorType .SQLITE_OK .code) {
157- val errorMessage = errMsg.value?.toKString() ? : " Unknown error"
158- throw PowerSyncException (
159- " Could not load the PowerSync SQLite core extension" ,
160- cause =
161- Exception (
162- " Calling sqlite3_load_extension failed with error: $errorMessage " ,
163- ),
164- )
165- }
137+ connection.loadPowerSyncSqliteCoreExtension()
166138
139+ val ptr = connection.getDbPointer().getPointer(MemScope ())
167140 val driverRef = StableRef .create(driver)
168141
169142 sqlite3_update_hook(
@@ -221,3 +194,41 @@ public actual class DatabaseDriverFactory {
221194 }
222195 }
223196}
197+
198+ internal fun DatabaseConnection.loadPowerSyncSqliteCoreExtensionDynamically () {
199+ val ptr = getDbPointer().getPointer(MemScope ())
200+ val extensionPath = powerSyncExtensionPath
201+
202+ // Enable extension loading
203+ // We don't disable this after the fact, this should allow users to load their own extensions
204+ // in future.
205+ val enableResult = sqlite3_enable_load_extension(ptr, 1 )
206+ if (enableResult != SqliteErrorType .SQLITE_OK .code) {
207+ throw PowerSyncException (
208+ " Could not dynamically load the PowerSync SQLite core extension" ,
209+ cause =
210+ Exception (
211+ " Call to sqlite3_enable_load_extension failed" ,
212+ ),
213+ )
214+ }
215+
216+ // A place to store a potential error message response
217+ val errMsg = nativeHeap.alloc<CPointerVar <ByteVar >>()
218+ val result =
219+ sqlite3_load_extension(ptr, extensionPath, " sqlite3_powersync_init" , errMsg.ptr)
220+ val resultingError = errMsg.value
221+ nativeHeap.free(errMsg)
222+ if (result != SqliteErrorType .SQLITE_OK .code) {
223+ val errorMessage = resultingError?.toKString() ? : " Unknown error"
224+ throw PowerSyncException (
225+ " Could not load the PowerSync SQLite core extension" ,
226+ cause =
227+ Exception (
228+ " Calling sqlite3_load_extension failed with error: $errorMessage " ,
229+ ),
230+ )
231+ }
232+ }
233+
234+ internal expect fun DatabaseConnection.loadPowerSyncSqliteCoreExtension ()
0 commit comments