Skip to content

Commit 8428135

Browse files
wip: grdb
1 parent 245e3d2 commit 8428135

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

Sources/PowerSync/Kotlin/KotlinSQLiteConnectionPool.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class SwiftSQLiteConnectionPoolAdapter: PowerSyncKotlin.SwiftPoolAdapter {
2525
func __leaseRead(callback: @escaping (Any) -> Void) async throws {
2626
do {
2727
try await pool.read { pointer in
28-
callback(pointer)
28+
callback(UInt(bitPattern: pointer))
2929
}
3030
} catch {
3131
try? PowerSyncKotlin.throwPowerSyncException(
@@ -40,7 +40,23 @@ final class SwiftSQLiteConnectionPoolAdapter: PowerSyncKotlin.SwiftPoolAdapter {
4040
func __leaseWrite(callback: @escaping (Any) -> Void) async throws {
4141
do {
4242
try await pool.write { pointer in
43-
callback(pointer)
43+
callback(UInt(bitPattern: pointer))
44+
}
45+
} catch {
46+
try? PowerSyncKotlin.throwPowerSyncException(
47+
exception: PowerSyncException(
48+
message: error.localizedDescription,
49+
cause: nil
50+
)
51+
)
52+
}
53+
}
54+
55+
func __leaseAll(callback: @escaping (Any, [Any]) -> Void) async throws {
56+
// TODO, actually use all connections
57+
do {
58+
try await pool.write { pointer in
59+
callback(UInt(bitPattern: pointer), [])
4460
}
4561
} catch {
4662
try? PowerSyncKotlin.throwPowerSyncException(

Sources/PowerSync/Protocol/SQLiteConnectionPool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public protocol SQLiteConnectionPoolProtocol {
1515

1616
/// Invokes the callback with all connections leased from the pool.
1717
func withAllConnections(
18-
onConnection: @escaping (
18+
onConnection: @Sendable @escaping (
1919
_ writer: OpaquePointer,
2020
_ readers: [OpaquePointer]
2121
) -> Void,

Sources/PowerSyncGRDB/GRDBPool.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ class GRDBConnectionPool: SQLiteConnectionPoolProtocol {
6969
func write(
7070
onConnection: @Sendable @escaping (OpaquePointer) -> Void
7171
) async throws {
72-
try await pool.write { database in
72+
// Don't start an explicit transaction
73+
try await pool.writeWithoutTransaction { database in
7374
guard let connection = database.sqliteConnection else {
7475
return
7576
}

Tests/PowerSyncGRDBTests/BasicTest.swift

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,22 @@ import XCTest
77
final class GRDBTests: XCTestCase {
88
private var database: PowerSyncDatabaseProtocol!
99
private var schema: Schema!
10+
private var pool: DatabasePool!
1011

1112
override func setUp() async throws {
1213
try await super.setUp()
1314
schema = Schema(tables: [
1415
Table(name: "users", columns: [
15-
.text("count"),
16-
.integer("is_active"),
17-
.real("weight"),
18-
.text("description")
16+
.text("name"),
17+
.text("count")
1918
])
2019
])
2120

2221
var config = Configuration()
2322
configurePowerSync(&config)
24-
2523
let documentsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
2624
let dbURL = documentsDir.appendingPathComponent("test.sqlite")
27-
let pool = try DatabasePool(
25+
pool = try DatabasePool(
2826
path: dbURL.path,
2927
configuration: config
3028
)
@@ -48,10 +46,39 @@ final class GRDBTests: XCTestCase {
4846

4947
func testValidValues() async throws {
5048
let result = try await database.get(
51-
"SELECT powersync_rs_version as r"
49+
"SELECT powersync_rs_version() as r"
5250
) { cursor in
5351
try cursor.getString(index: 0)
5452
}
5553
print(result)
54+
55+
try await database.execute(
56+
"INSERT INTO users(id, name, count) VALUES(uuid(), 'steven', 1)"
57+
)
58+
59+
let initialUsers = try await database.getAll(
60+
"SELECT * FROM users"
61+
) { cursor in
62+
try cursor.getString(name: "name")
63+
}
64+
print("initial users \(initialUsers)")
65+
66+
// Now use a GRDB query
67+
struct Users: Codable, Identifiable, FetchableRecord, PersistableRecord {
68+
var id: String
69+
var name: String
70+
var count: Int
71+
72+
enum Columns {
73+
static let name = Column(CodingKeys.name)
74+
static let count = Column(CodingKeys.count)
75+
}
76+
}
77+
78+
let grdbUsers = try await pool.write { db in
79+
try Users.fetchAll(db)
80+
}
81+
82+
print(grdbUsers)
5683
}
5784
}

0 commit comments

Comments
 (0)