|
| 1 | +import Foundation |
| 2 | +import GRDB |
| 3 | +import PowerSync |
| 4 | +import SQLite3 |
| 5 | + |
| 6 | +// The system SQLite does not expose this, |
| 7 | +// linking PowerSync provides them |
| 8 | +// Declare the missing function manually |
| 9 | +@_silgen_name("sqlite3_enable_load_extension") |
| 10 | +func sqlite3_enable_load_extension(_ db: OpaquePointer?, _ onoff: Int32) -> Int32 |
| 11 | + |
| 12 | +// Similarly for sqlite3_load_extension if needed: |
| 13 | +@_silgen_name("sqlite3_load_extension") |
| 14 | +func sqlite3_load_extension(_ db: OpaquePointer?, _ fileName: UnsafePointer<Int8>?, _ procName: UnsafePointer<Int8>?, _ errMsg: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>?) -> Int32 |
| 15 | + |
| 16 | +enum PowerSyncGRDBConfigError: Error { |
| 17 | + case bundleNotFound |
| 18 | + case extensionLoadFailed(String) |
| 19 | + case unknownExtensionLoadError |
| 20 | +} |
| 21 | + |
| 22 | +func configurePowerSync(_ config: inout Configuration) { |
| 23 | + config.prepareDatabase { database in |
| 24 | + guard let bundle = Bundle(identifier: "co.powersync.sqlitecore") else { |
| 25 | + throw PowerSyncGRDBConfigError.bundleNotFound |
| 26 | + } |
| 27 | + |
| 28 | + // Construct the full path to the shared library inside the bundle |
| 29 | + let fullPath = bundle.bundlePath + "/powersync-sqlite-core" |
| 30 | + |
| 31 | + let rc = sqlite3_enable_load_extension(database.sqliteConnection, 1) |
| 32 | + if rc != SQLITE_OK { |
| 33 | + throw PowerSyncGRDBConfigError.extensionLoadFailed("Could not enable extension loading") |
| 34 | + } |
| 35 | + var errorMsg: UnsafeMutablePointer<Int8>? |
| 36 | + let loadResult = sqlite3_load_extension(database.sqliteConnection, fullPath, "sqlite3_powersync_init", &errorMsg) |
| 37 | + if loadResult != SQLITE_OK { |
| 38 | + if let errorMsg = errorMsg { |
| 39 | + let message = String(cString: errorMsg) |
| 40 | + sqlite3_free(errorMsg) |
| 41 | + throw PowerSyncGRDBConfigError.extensionLoadFailed(message) |
| 42 | + } else { |
| 43 | + throw PowerSyncGRDBConfigError.unknownExtensionLoadError |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +class GRDBConnectionPool: SQLiteConnectionPoolProtocol { |
| 50 | + let pool: DatabasePool |
| 51 | + |
| 52 | + init( |
| 53 | + pool: DatabasePool |
| 54 | + ) { |
| 55 | + self.pool = pool |
| 56 | + } |
| 57 | + |
| 58 | + func read( |
| 59 | + onConnection: @Sendable @escaping (OpaquePointer) -> Void |
| 60 | + ) async throws { |
| 61 | + try await pool.read { database in |
| 62 | + guard let connection = database.sqliteConnection else { |
| 63 | + return |
| 64 | + } |
| 65 | + onConnection(connection) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + func write( |
| 70 | + onConnection: @Sendable @escaping (OpaquePointer) -> Void |
| 71 | + ) async throws { |
| 72 | + try await pool.write { database in |
| 73 | + guard let connection = database.sqliteConnection else { |
| 74 | + return |
| 75 | + } |
| 76 | + onConnection(connection) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + func withAllConnections( |
| 81 | + onConnection _: @escaping (OpaquePointer, [OpaquePointer]) -> Void |
| 82 | + ) async throws { |
| 83 | + // TODO: |
| 84 | + } |
| 85 | + |
| 86 | + func close() throws { |
| 87 | + try pool.close() |
| 88 | + } |
| 89 | +} |
0 commit comments