@@ -12,6 +12,7 @@ import co.touchlab.sqliter.sqlite3.sqlite3_enable_load_extension
12
12
import co.touchlab.sqliter.sqlite3.sqlite3_load_extension
13
13
import co.touchlab.sqliter.sqlite3.sqlite3_rollback_hook
14
14
import co.touchlab.sqliter.sqlite3.sqlite3_update_hook
15
+ import com.powersync.DatabaseDriverFactory.Companion.powerSyncExtensionPath
15
16
import com.powersync.db.internal.InternalSchema
16
17
import com.powersync.persistence.driver.NativeSqliteDriver
17
18
import com.powersync.persistence.driver.wrapConnection
@@ -22,6 +23,7 @@ import kotlinx.cinterop.MemScope
22
23
import kotlinx.cinterop.StableRef
23
24
import kotlinx.cinterop.alloc
24
25
import kotlinx.cinterop.asStableRef
26
+ import kotlinx.cinterop.free
25
27
import kotlinx.cinterop.nativeHeap
26
28
import kotlinx.cinterop.ptr
27
29
import kotlinx.cinterop.staticCFunction
@@ -132,38 +134,9 @@ public actual class DatabaseDriverFactory {
132
134
connection : DatabaseConnection ,
133
135
driver : DeferredDriver ,
134
136
) {
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()
166
138
139
+ val ptr = connection.getDbPointer().getPointer(MemScope ())
167
140
val driverRef = StableRef .create(driver)
168
141
169
142
sqlite3_update_hook(
@@ -221,3 +194,41 @@ public actual class DatabaseDriverFactory {
221
194
}
222
195
}
223
196
}
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