@@ -8,7 +8,7 @@ final class PowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
88 private let dbFilename : String ?
99 private let httpClient : HttpClient
1010 private let initializer = DatabaseInitializationAction ( )
11- fileprivate let queries : ConnectionPoolQueries
11+ let pool : any SQLiteConnectionPoolProtocol
1212 let schema : AsyncMutex < Schema >
1313
1414 init (
@@ -24,7 +24,7 @@ final class PowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
2424 self . logger = logger
2525 self . schema = AsyncMutex ( schema)
2626 self . httpClient = httpClient
27- self . queries = ConnectionPoolQueries ( pool: pool )
27+ self . pool = pool
2828 self . group = activeInstanceStore. referenceGroup ( identifier: identifier, logger: logger)
2929 }
3030
@@ -44,7 +44,7 @@ final class PowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
4444
4545 fileprivate func resolveOfflineSyncStatus( ) async throws {
4646 // We can't use get() here because it runs as part of the initialization step.
47- let offlineSyncStatus = try await queries . readLock { connection in
47+ let offlineSyncStatus = try await readLockInner { connection in
4848 try connection. get ( sql: " SELECT powersync_offline_sync_status() " , parameters: [ ] ) { cursor in
4949 let raw = try cursor. getString ( index: 0 )
5050 guard let data = raw. data ( using: . utf8) else {
@@ -70,16 +70,17 @@ final class PowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
7070 }
7171
7272 fileprivate func applySchema( schema: Schema ) async throws {
73- try await queries . withAll { writer, readers in
73+ try await pool . withAllConnections { writer, readers in
7474 let encoded = try StreamingSyncClient . jsonEncoder. encode ( schema)
7575 guard let asString = String ( data: encoded, encoding: . utf8) else {
7676 throw PowerSyncError . operationFailed ( message: " Could not serialize schema " )
7777 }
78- try writer. execute ( sql: " SELECT powersync_replace_schema(?) " , parameters: [ asString] )
78+
79+ let _ = try writer. execute ( sql: " SELECT powersync_replace_schema(?) " , parameters: [ . string( asString) ] )
7980
8081 for reader in readers {
8182 // Update the schema on all read connections
82- try reader. execute ( sql: " pragma table_info('sqlite_master') " , parameters: [ ] )
83+ let _ = try reader. execute ( sql: " pragma table_info('sqlite_master') " , parameters: [ ] )
8384 }
8485 }
8586 }
@@ -113,7 +114,7 @@ final class PowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
113114 try await initialize ( )
114115 try await initializer. close {
115116 await group. syncCoordinator. disconnect ( )
116- try await queries . pool. close ( )
117+ try await pool. close ( )
117118 }
118119 }
119120
@@ -127,6 +128,7 @@ final class PowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
127128 }
128129
129130 func connect( connector: any PowerSyncBackendConnectorProtocol , options: ConnectOptions ? ) async throws {
131+ try await initialize ( )
130132 await group. syncCoordinator. connect ( db: self , connector: connector, options: options ?? ConnectOptions ( ) , client: httpClient)
131133 }
132134
@@ -143,23 +145,35 @@ final class PowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
143145
144146 do {
145147 let flags = flags
146- let _ = try await queries . writeLock { ctx in try ctx. execute ( sql: " SELECT powersync_clear(?) " , parameters: [ flags] ) }
148+ let _ = try await writeLockInner { ctx in try ctx. execute ( sql: " SELECT powersync_clear(?) " , parameters: [ flags] ) }
147149 }
148150 }
149151 }
150152
151153 func writeLock< R: Sendable > ( callback: @escaping @Sendable ( any ConnectionContext ) throws -> R ) async throws -> R {
152154 try await initialize ( )
153- return try await queries. writeLock ( callback: callback)
155+ return try await writeLockInner ( callback: callback)
156+ }
157+
158+ fileprivate func writeLockInner< R: Sendable > ( callback: @escaping @Sendable ( any ConnectionContext ) throws -> R ) async throws -> R {
159+ return try await self . pool. write { connection in
160+ try callback ( ConnectionLeaseContext ( lease: connection) )
161+ }
154162 }
155163
156164 func readLock< R: Sendable > ( callback: @escaping @Sendable ( any ConnectionContext ) throws -> R ) async throws -> R {
157165 try await initialize ( )
158- return try await queries. readLock ( callback: callback)
166+ return try await readLockInner ( callback: callback)
167+ }
168+
169+ fileprivate func readLockInner< R: Sendable > ( callback: @escaping @Sendable ( any ConnectionContext ) throws -> R ) async throws -> R {
170+ return try await pool. read { connection in
171+ try callback ( ConnectionLeaseContext ( lease: connection) )
172+ }
159173 }
160174
161175 func watch< RowType: Sendable > ( options: WatchOptions < RowType > ) throws -> AsyncThrowingStream < [ RowType ] , any Error > {
162- return try queries . watch ( options: options)
176+ return watchImpl ( db : self , options: options)
163177 }
164178
165179 static let maxOpId = Int64 . max
@@ -178,7 +192,7 @@ private actor DatabaseInitializationAction {
178192 return
179193 }
180194
181- powerSyncVersion = try await db. queries . writeLock { conn in
195+ powerSyncVersion = try await db. writeLockInner { conn in
182196 let sqliteVersion = try conn. get ( sql: " SELECT sqlite_version() " , parameters: [ ] ) { try $0. getString ( index: 0 ) }
183197 let powerSyncVersion = try conn. get ( sql: " SELECT powersync_rs_version() " , parameters: [ ] ) { try $0. getString ( index: 0 ) }
184198
0 commit comments