-
Notifications
You must be signed in to change notification settings - Fork 7
GRDB Support #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stevensJourney
wants to merge
19
commits into
main
Choose a base branch
from
grdb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
GRDB Support #78
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
245e3d2
wip: grdb connection pool
stevensJourney 8428135
wip: grdb
stevensJourney dbd9c09
Use latest GRDB package. Update tests and queries.
stevensJourney 6f32934
wip: table update hooks
stevensJourney e2681f6
Add test for GRDB updates triggered by GRDB
stevensJourney a79ec5c
add join test
stevensJourney 8a278a1
WIP: Add GRDB demo app
stevensJourney 4002d85
demo improvements
stevensJourney 6f0e630
Table updates from PowerSync side
stevensJourney 41174b1
Use SQLite Session API for PowerSync updates.
stevensJourney 7aae6cf
Update GRDB dependency
stevensJourney e970fd4
Merge remote-tracking branch 'origin/main' into grdb
stevensJourney 76aeb1c
demo update
stevensJourney 281558a
Update README. Cleanup public APIs. WIP WatchOS.
stevensJourney 7beff16
Merge remote-tracking branch 'origin/main' into grdb
stevensJourney a81986e
Update READMEs
stevensJourney 1c1f2bb
Register extension on WatchOS
stevensJourney 6afa299
Swift Strict Concurrency
stevensJourney b4d9cbf
Demo and Docs cleanup
stevensJourney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import PowerSyncKotlin | ||
|
|
||
| final class SwiftSQLiteConnectionPoolAdapter: PowerSyncKotlin.SwiftPoolAdapter { | ||
| let pool: SQLiteConnectionPoolProtocol | ||
|
|
||
| init( | ||
| pool: SQLiteConnectionPoolProtocol | ||
| ) { | ||
| self.pool = pool | ||
| } | ||
|
|
||
| func __closePool() async throws { | ||
| do { | ||
| try pool.close() | ||
| } catch { | ||
| try? PowerSyncKotlin.throwPowerSyncException( | ||
| exception: PowerSyncException( | ||
| message: error.localizedDescription, | ||
| cause: nil | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func __leaseRead(callback: @escaping (Any) -> Void) async throws { | ||
| do { | ||
| try await pool.read { pointer in | ||
| callback(UInt(bitPattern: pointer)) | ||
| } | ||
| } catch { | ||
| try? PowerSyncKotlin.throwPowerSyncException( | ||
| exception: PowerSyncException( | ||
| message: error.localizedDescription, | ||
| cause: nil | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func __leaseWrite(callback: @escaping (Any) -> Void) async throws { | ||
| do { | ||
| try await pool.write { pointer in | ||
| callback(UInt(bitPattern: pointer)) | ||
| } | ||
| } catch { | ||
| try? PowerSyncKotlin.throwPowerSyncException( | ||
| exception: PowerSyncException( | ||
| message: error.localizedDescription, | ||
| cause: nil | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func __leaseAll(callback: @escaping (Any, [Any]) -> Void) async throws { | ||
| // TODO, actually use all connections | ||
| do { | ||
| try await pool.write { pointer in | ||
| callback(UInt(bitPattern: pointer), []) | ||
| } | ||
| } catch { | ||
| try? PowerSyncKotlin.throwPowerSyncException( | ||
| exception: PowerSyncException( | ||
| message: error.localizedDescription, | ||
| cause: nil | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension SQLiteConnectionPoolProtocol { | ||
| func toKotlin() -> PowerSyncKotlin.SwiftSQLiteConnectionPool { | ||
| return PowerSyncKotlin.SwiftSQLiteConnectionPool( | ||
| adapter: SwiftSQLiteConnectionPoolAdapter(pool: self) | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import Foundation | ||
|
|
||
| /// An implementation of a connection pool providing asynchronous access to a single writer and multiple readers. | ||
| /// This is the underlying pool implementation on which the higher-level PowerSync Swift SDK is built on. | ||
| public protocol SQLiteConnectionPoolProtocol { | ||
| /// Calls the callback with a read-only connection temporarily leased from the pool. | ||
| func read( | ||
| onConnection: @Sendable @escaping (OpaquePointer) -> Void, | ||
| ) async throws | ||
|
|
||
| /// Calls the callback with a read-write connection temporarily leased from the pool. | ||
| func write( | ||
| onConnection: @Sendable @escaping (OpaquePointer) -> Void, | ||
| ) async throws | ||
|
|
||
| /// Invokes the callback with all connections leased from the pool. | ||
| func withAllConnections( | ||
| onConnection: @Sendable @escaping ( | ||
| _ writer: OpaquePointer, | ||
| _ readers: [OpaquePointer] | ||
| ) -> Void, | ||
| ) async throws | ||
|
|
||
| /// Closes the connection pool and associated resources. | ||
| func close() throws | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import Foundation | ||
| import GRDB | ||
| import PowerSync | ||
| import SQLite3 | ||
|
|
||
| // The system SQLite does not expose this, | ||
| // linking PowerSync provides them | ||
| // Declare the missing function manually | ||
| @_silgen_name("sqlite3_enable_load_extension") | ||
| func sqlite3_enable_load_extension(_ db: OpaquePointer?, _ onoff: Int32) -> Int32 | ||
|
|
||
| // Similarly for sqlite3_load_extension if needed: | ||
| @_silgen_name("sqlite3_load_extension") | ||
| func sqlite3_load_extension(_ db: OpaquePointer?, _ fileName: UnsafePointer<Int8>?, _ procName: UnsafePointer<Int8>?, _ errMsg: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>?) -> Int32 | ||
|
|
||
| enum PowerSyncGRDBConfigError: Error { | ||
| case bundleNotFound | ||
| case extensionLoadFailed(String) | ||
| case unknownExtensionLoadError | ||
| } | ||
|
|
||
| func configurePowerSync(_ config: inout Configuration) { | ||
| config.prepareDatabase { database in | ||
| guard let bundle = Bundle(identifier: "co.powersync.sqlitecore") else { | ||
stevensJourney marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| throw PowerSyncGRDBConfigError.bundleNotFound | ||
| } | ||
|
|
||
| // Construct the full path to the shared library inside the bundle | ||
| let fullPath = bundle.bundlePath + "/powersync-sqlite-core" | ||
|
|
||
| let rc = sqlite3_enable_load_extension(database.sqliteConnection, 1) | ||
| if rc != SQLITE_OK { | ||
| throw PowerSyncGRDBConfigError.extensionLoadFailed("Could not enable extension loading") | ||
| } | ||
| var errorMsg: UnsafeMutablePointer<Int8>? | ||
| let loadResult = sqlite3_load_extension(database.sqliteConnection, fullPath, "sqlite3_powersync_init", &errorMsg) | ||
| if loadResult != SQLITE_OK { | ||
| if let errorMsg = errorMsg { | ||
| let message = String(cString: errorMsg) | ||
| sqlite3_free(errorMsg) | ||
| throw PowerSyncGRDBConfigError.extensionLoadFailed(message) | ||
| } else { | ||
| throw PowerSyncGRDBConfigError.unknownExtensionLoadError | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class GRDBConnectionPool: SQLiteConnectionPoolProtocol { | ||
| let pool: DatabasePool | ||
|
|
||
| init( | ||
| pool: DatabasePool | ||
| ) { | ||
| self.pool = pool | ||
| } | ||
|
|
||
| func read( | ||
| onConnection: @Sendable @escaping (OpaquePointer) -> Void | ||
| ) async throws { | ||
| try await pool.read { database in | ||
| guard let connection = database.sqliteConnection else { | ||
| return | ||
| } | ||
| onConnection(connection) | ||
| } | ||
| } | ||
|
|
||
| func write( | ||
| onConnection: @Sendable @escaping (OpaquePointer) -> Void | ||
| ) async throws { | ||
| // Don't start an explicit transaction | ||
| try await pool.writeWithoutTransaction { database in | ||
| guard let connection = database.sqliteConnection else { | ||
| return | ||
| } | ||
| onConnection(connection) | ||
| } | ||
| } | ||
|
|
||
| func withAllConnections( | ||
| onConnection _: @escaping (OpaquePointer, [OpaquePointer]) -> Void | ||
| ) async throws { | ||
| // TODO: | ||
| } | ||
|
|
||
| func close() throws { | ||
| try pool.close() | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.