@@ -7,43 +7,79 @@ import SQLite3
77// linking PowerSync provides them
88// Declare the missing function manually
99@_silgen_name ( " sqlite3_enable_load_extension " )
10- func sqlite3_enable_load_extension( _ db: OpaquePointer ? , _ onoff: Int32 ) -> Int32
10+ func sqlite3_enable_load_extension(
11+ _ db: OpaquePointer ? ,
12+ _ onoff: Int32
13+ ) -> Int32
1114
1215// Similarly for sqlite3_load_extension if needed:
1316@_silgen_name ( " sqlite3_load_extension " )
14- func sqlite3_load_extension( _ db: OpaquePointer ? , _ fileName: UnsafePointer < Int8 > ? , _ procName: UnsafePointer < Int8 > ? , _ errMsg: UnsafeMutablePointer < UnsafeMutablePointer < Int8 > ? > ? ) -> Int32
17+ func sqlite3_load_extension(
18+ _ db: OpaquePointer ? ,
19+ _ fileName: UnsafePointer < Int8 > ? ,
20+ _ procName: UnsafePointer < Int8 > ? ,
21+ _ errMsg: UnsafeMutablePointer < UnsafeMutablePointer < Int8 > ? > ?
22+ ) -> Int32
1523
16- enum PowerSyncGRDBConfigError : Error {
17- case bundleNotFound
24+ enum PowerSyncGRDBError : Error {
25+ case coreBundleNotFound
1826 case extensionLoadFailed ( String)
1927 case unknownExtensionLoadError
28+ case connectionUnavailable
2029}
2130
22- func configurePowerSync( _ config: inout Configuration ) {
31+ struct PowerSyncSchemaSource : DatabaseSchemaSource {
32+ let schema : Schema
33+
34+ func columnsForPrimaryKey( _: Database , inView view: DatabaseObjectID ) throws -> [ String ] ? {
35+ if schema. tables. first ( where: { table in
36+ table. viewName == view. name
37+ } ) != nil {
38+ return [ " id " ]
39+ }
40+ return nil
41+ }
42+ }
43+
44+ func configurePowerSync(
45+ config: inout Configuration ,
46+ schema: Schema
47+ ) {
48+ // Register the PowerSync core extension
2349 config. prepareDatabase { database in
2450 guard let bundle = Bundle ( identifier: " co.powersync.sqlitecore " ) else {
25- throw PowerSyncGRDBConfigError . bundleNotFound
51+ throw PowerSyncGRDBError . coreBundleNotFound
2652 }
2753
2854 // Construct the full path to the shared library inside the bundle
2955 let fullPath = bundle. bundlePath + " /powersync-sqlite-core "
3056
31- let rc = sqlite3_enable_load_extension ( database. sqliteConnection, 1 )
32- if rc != SQLITE_OK {
33- throw PowerSyncGRDBConfigError . extensionLoadFailed ( " Could not enable extension loading " )
57+ let extensionLoadResult = sqlite3_enable_load_extension ( database. sqliteConnection, 1 )
58+ if extensionLoadResult != SQLITE_OK {
59+ throw PowerSyncGRDBError . extensionLoadFailed ( " Could not enable extension loading " )
3460 }
3561 var errorMsg : UnsafeMutablePointer < Int8 > ?
3662 let loadResult = sqlite3_load_extension ( database. sqliteConnection, fullPath, " sqlite3_powersync_init " , & errorMsg)
3763 if loadResult != SQLITE_OK {
3864 if let errorMsg = errorMsg {
3965 let message = String ( cString: errorMsg)
4066 sqlite3_free ( errorMsg)
41- throw PowerSyncGRDBConfigError . extensionLoadFailed ( message)
67+ throw PowerSyncGRDBError . extensionLoadFailed ( message)
4268 } else {
43- throw PowerSyncGRDBConfigError . unknownExtensionLoadError
69+ throw PowerSyncGRDBError . unknownExtensionLoadError
4470 }
4571 }
4672 }
73+
74+ // Supply the PowerSync views as a SchemaSource
75+ let powerSyncSchemaSource = PowerSyncSchemaSource (
76+ schema: schema
77+ )
78+ if let schemaSource = config. schemaSource {
79+ config. schemaSource = schemaSource. then ( powerSyncSchemaSource)
80+ } else {
81+ config. schemaSource = powerSyncSchemaSource
82+ }
4783}
4884
4985class GRDBConnectionPool : SQLiteConnectionPoolProtocol {
@@ -60,7 +96,7 @@ class GRDBConnectionPool: SQLiteConnectionPoolProtocol {
6096 ) async throws {
6197 try await pool. read { database in
6298 guard let connection = database. sqliteConnection else {
63- return
99+ throw PowerSyncGRDBError . connectionUnavailable
64100 }
65101 onConnection ( connection)
66102 }
@@ -72,7 +108,7 @@ class GRDBConnectionPool: SQLiteConnectionPoolProtocol {
72108 // Don't start an explicit transaction
73109 try await pool. writeWithoutTransaction { database in
74110 guard let connection = database. sqliteConnection else {
75- return
111+ throw PowerSyncGRDBError . connectionUnavailable
76112 }
77113 onConnection ( connection)
78114 }
0 commit comments