mirror of
https://github.com/groue/GRDB.swift
synced 2026-12-01 04:38:44 +00:00
Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dffc4bbe5d | ||
|
|
aa28797f67 | ||
|
|
fe59d44499 | ||
|
|
064eb9dea2 | ||
|
|
7bb5c636e7 | ||
|
|
080f9275cc | ||
|
|
aadc73cc11 | ||
|
|
2024f857a0 |
No files matched your search
@@ -352,29 +352,35 @@ extension DatabasePool: DatabaseReader {
|
||||
}
|
||||
|
||||
public func read<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
_ value: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
guard let readerPool else {
|
||||
throw DatabaseError.connectionIsClosed()
|
||||
}
|
||||
|
||||
return try await readerPool.get { reader in
|
||||
try await reader.execute { db in
|
||||
defer {
|
||||
// Commit or rollback, but make sure we leave the read-only transaction
|
||||
// (commit may fail with a CancellationError).
|
||||
do {
|
||||
try db.commit()
|
||||
} catch {
|
||||
try? db.rollback()
|
||||
}
|
||||
assert(!db.isInsideTransaction)
|
||||
}
|
||||
// The block isolation comes from the DEFERRED transaction.
|
||||
try db.beginTransaction(.deferred)
|
||||
try db.clearSchemaCacheIfNeeded()
|
||||
return try value(db)
|
||||
}
|
||||
// Compiler does not see that value can be sent.
|
||||
return try await withoutActuallyEscaping(value) { value in
|
||||
typealias SendableClosure = @Sendable (Database) throws -> T
|
||||
let value = unsafeBitCast(value, to: SendableClosure.self)
|
||||
|
||||
return try await readerPool.get { reader in
|
||||
try await reader.execute { db in
|
||||
defer {
|
||||
// Commit or rollback, but make sure we leave the read-only transaction
|
||||
// (commit may fail with a CancellationError).
|
||||
do {
|
||||
try db.commit()
|
||||
} catch {
|
||||
try? db.rollback()
|
||||
}
|
||||
assert(!db.isInsideTransaction)
|
||||
}
|
||||
// The block isolation comes from the DEFERRED transaction.
|
||||
try db.beginTransaction(.deferred)
|
||||
try db.clearSchemaCacheIfNeeded()
|
||||
return try value(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -432,16 +438,22 @@ extension DatabasePool: DatabaseReader {
|
||||
}
|
||||
|
||||
public func unsafeRead<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
_ value: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
guard let readerPool else {
|
||||
throw DatabaseError.connectionIsClosed()
|
||||
}
|
||||
|
||||
return try await readerPool.get { reader in
|
||||
try await reader.execute { db in
|
||||
try db.clearSchemaCacheIfNeeded()
|
||||
return try value(db)
|
||||
// Compiler does not see that value can be sent.
|
||||
return try await withoutActuallyEscaping(value) { value in
|
||||
typealias SendableClosure = @Sendable (Database) throws -> T
|
||||
let value = unsafeBitCast(value, to: SendableClosure.self)
|
||||
|
||||
return try await readerPool.get { reader in
|
||||
try await reader.execute { db in
|
||||
try db.clearSchemaCacheIfNeeded()
|
||||
return try value(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -786,7 +798,7 @@ extension DatabasePool: DatabaseWriter {
|
||||
}
|
||||
|
||||
public func writeWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
_ updates: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await writer.execute(updates)
|
||||
}
|
||||
@@ -802,32 +814,38 @@ extension DatabasePool: DatabaseWriter {
|
||||
}
|
||||
|
||||
public func barrierWriteWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
_ updates: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
guard let readerPool else {
|
||||
throw DatabaseError.connectionIsClosed()
|
||||
}
|
||||
|
||||
// Pool.barrier does not support async calls (yet?).
|
||||
// So we perform cancellation checks just as in
|
||||
// the async version of SerializedDatabase.execute().
|
||||
let cancelMutex = Mutex<(@Sendable () -> Void)?>(nil)
|
||||
return try await withTaskCancellationHandler {
|
||||
try Task.checkCancellation()
|
||||
return try await readerPool.barrier {
|
||||
// Compiler does not see that updates can be sent.
|
||||
return try await withoutActuallyEscaping(updates) { updates in
|
||||
typealias SendableClosure = @Sendable (Database) throws -> T
|
||||
let updates = unsafeBitCast(updates, to: SendableClosure.self)
|
||||
|
||||
// Pool.barrier does not support async calls (yet?).
|
||||
// So we perform cancellation checks just as in
|
||||
// the async version of SerializedDatabase.execute().
|
||||
let cancelMutex = Mutex<(@Sendable () -> Void)?>(nil)
|
||||
return try await withTaskCancellationHandler {
|
||||
try Task.checkCancellation()
|
||||
return try writer.sync { db in
|
||||
defer {
|
||||
cancelMutex.store(nil)
|
||||
db.uncancel()
|
||||
}
|
||||
cancelMutex.store(db.cancel)
|
||||
return try await readerPool.barrier {
|
||||
try Task.checkCancellation()
|
||||
return try updates(db)
|
||||
return try writer.sync { db in
|
||||
defer {
|
||||
cancelMutex.store(nil)
|
||||
db.uncancel()
|
||||
}
|
||||
cancelMutex.store(db.cancel)
|
||||
try Task.checkCancellation()
|
||||
return try updates(db)
|
||||
}
|
||||
}
|
||||
} onCancel: {
|
||||
cancelMutex.withLock { $0?() }
|
||||
}
|
||||
} onCancel: {
|
||||
cancelMutex.withLock { $0?() }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -234,7 +234,7 @@ extension DatabaseQueue: DatabaseReader {
|
||||
}
|
||||
|
||||
public func read<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
_ value: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await writer.execute { db in
|
||||
try db.isolated(readOnly: true) {
|
||||
@@ -278,7 +278,7 @@ extension DatabaseQueue: DatabaseReader {
|
||||
}
|
||||
|
||||
public func unsafeRead<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
_ value: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await writer.execute(value)
|
||||
}
|
||||
@@ -397,7 +397,7 @@ extension DatabaseQueue: DatabaseWriter {
|
||||
}
|
||||
|
||||
public func writeWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
_ updates: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await writer.execute(updates)
|
||||
}
|
||||
@@ -408,7 +408,7 @@ extension DatabaseQueue: DatabaseWriter {
|
||||
}
|
||||
|
||||
public func barrierWriteWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
_ updates: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await writer.execute(updates)
|
||||
}
|
||||
|
||||
@@ -215,7 +215,7 @@ public protocol DatabaseReader: AnyObject, Sendable {
|
||||
/// database access, or the error thrown by `value`, or
|
||||
/// `CancellationError` if the task is cancelled.
|
||||
func read<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
_ value: sending (Database) throws -> T
|
||||
) async throws -> T
|
||||
|
||||
/// Schedules read-only database operations for execution, and
|
||||
@@ -319,7 +319,7 @@ public protocol DatabaseReader: AnyObject, Sendable {
|
||||
/// database access, or the error thrown by `value`, or
|
||||
/// `CancellationError` if the task is cancelled.
|
||||
func unsafeRead<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
_ value: sending (Database) throws -> T
|
||||
) async throws -> T
|
||||
|
||||
/// Schedules database operations for execution, and returns immediately.
|
||||
@@ -652,7 +652,7 @@ extension AnyDatabaseReader: DatabaseReader {
|
||||
}
|
||||
|
||||
public func read<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
_ value: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await base.read(value)
|
||||
}
|
||||
@@ -669,7 +669,7 @@ extension AnyDatabaseReader: DatabaseReader {
|
||||
}
|
||||
|
||||
public func unsafeRead<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
_ value: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await base.unsafeRead(value)
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ extension DatabaseSnapshot: DatabaseSnapshotReader {
|
||||
}
|
||||
|
||||
public func read<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
_ value: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await reader.execute(value)
|
||||
}
|
||||
@@ -180,7 +180,7 @@ extension DatabaseSnapshot: DatabaseSnapshotReader {
|
||||
// `DatabaseSnapshotReader`, because of
|
||||
// <https://github.com/apple/swift/issues/74469>.
|
||||
public func unsafeRead<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
_ value: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await read(value)
|
||||
}
|
||||
|
||||
@@ -297,7 +297,7 @@ extension DatabaseSnapshotPool: DatabaseSnapshotReader {
|
||||
}
|
||||
|
||||
public func read<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
_ value: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
guard let readerPool else {
|
||||
throw DatabaseError.connectionIsClosed()
|
||||
@@ -345,7 +345,7 @@ extension DatabaseSnapshotPool: DatabaseSnapshotReader {
|
||||
// `DatabaseSnapshotReader`, because of
|
||||
// <https://github.com/apple/swift/issues/74469>.
|
||||
public func unsafeRead<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
_ value: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await read(value)
|
||||
}
|
||||
|
||||
@@ -130,7 +130,7 @@ public protocol DatabaseWriter: DatabaseReader {
|
||||
/// database access, or the error thrown by `updates`, or
|
||||
/// `CancellationError` if the task is cancelled.
|
||||
func writeWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
_ updates: sending (Database) throws -> T
|
||||
) async throws -> T
|
||||
|
||||
/// Executes database operations, and returns their result after they have
|
||||
@@ -213,7 +213,7 @@ public protocol DatabaseWriter: DatabaseReader {
|
||||
/// database access, or the error thrown by `updates`, or
|
||||
/// `CancellationError` if the task is cancelled.
|
||||
func barrierWriteWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
_ updates: sending (Database) throws -> T
|
||||
) async throws -> T
|
||||
|
||||
/// Schedules database operations for execution, and returns immediately.
|
||||
@@ -640,7 +640,7 @@ extension DatabaseWriter {
|
||||
/// database access, or the error thrown by `updates`, or
|
||||
/// `CancellationError` if the task is cancelled.
|
||||
public func write<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
_ updates: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await writeWithoutTransaction { db in
|
||||
var result: T?
|
||||
@@ -885,7 +885,7 @@ extension AnyDatabaseWriter: DatabaseReader {
|
||||
}
|
||||
|
||||
public func read<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
_ value: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await base.read(value)
|
||||
}
|
||||
@@ -902,7 +902,7 @@ extension AnyDatabaseWriter: DatabaseReader {
|
||||
}
|
||||
|
||||
public func unsafeRead<T: Sendable>(
|
||||
_ value: @Sendable (Database) throws -> T
|
||||
_ value: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await base.unsafeRead(value)
|
||||
}
|
||||
@@ -936,7 +936,7 @@ extension AnyDatabaseWriter: DatabaseWriter {
|
||||
}
|
||||
|
||||
public func writeWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
_ updates: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await base.writeWithoutTransaction(updates)
|
||||
}
|
||||
@@ -947,7 +947,7 @@ extension AnyDatabaseWriter: DatabaseWriter {
|
||||
}
|
||||
|
||||
public func barrierWriteWithoutTransaction<T: Sendable>(
|
||||
_ updates: @Sendable (Database) throws -> T
|
||||
_ updates: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
try await base.barrierWriteWithoutTransaction(updates)
|
||||
}
|
||||
|
||||
@@ -250,22 +250,29 @@ final class SerializedDatabase {
|
||||
|
||||
/// Asynchrously executes the block.
|
||||
func execute<T: Sendable>(
|
||||
_ block: @Sendable (Database) throws -> T
|
||||
_ block: sending (Database) throws -> T
|
||||
) async throws -> T {
|
||||
let cancelMutex = Mutex<(@Sendable () -> Void)?>(nil)
|
||||
return try await withTaskCancellationHandler {
|
||||
try await actor.execute {
|
||||
defer {
|
||||
cancelMutex.store(nil)
|
||||
db.uncancel()
|
||||
preconditionNoUnsafeTransactionLeft(db)
|
||||
|
||||
// Compiler does not see that block can be sent.
|
||||
return try await withoutActuallyEscaping(block) { block in
|
||||
typealias SendableClosure = @Sendable (Database) throws -> T
|
||||
let block = unsafeBitCast(block, to: SendableClosure.self)
|
||||
|
||||
return try await withTaskCancellationHandler {
|
||||
try await actor.execute {
|
||||
defer {
|
||||
cancelMutex.store(nil)
|
||||
db.uncancel()
|
||||
preconditionNoUnsafeTransactionLeft(db)
|
||||
}
|
||||
cancelMutex.store(db.cancel)
|
||||
try Task.checkCancellation()
|
||||
return try block(db)
|
||||
}
|
||||
cancelMutex.store(db.cancel)
|
||||
try Task.checkCancellation()
|
||||
return try block(db)
|
||||
} onCancel: {
|
||||
cancelMutex.withLock { $0?() }
|
||||
}
|
||||
} onCancel: {
|
||||
cancelMutex.withLock { $0?() }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+26
-2
@@ -1,4 +1,4 @@
|
||||
// swift-tools-version:6.1
|
||||
// swift-tools-version:6.2
|
||||
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||
|
||||
import Foundation
|
||||
@@ -80,6 +80,9 @@ let package = Package(
|
||||
"Swift6Migration",
|
||||
"generatePerformanceReport.rb",
|
||||
"parsePerformanceTests.rb",
|
||||
"Swift6ApprochableConcurrencyGRDBTests",
|
||||
"Swift6DefaultGRDBTests",
|
||||
"Swift6MainActorIsolationGRDBTests",
|
||||
],
|
||||
resources: [
|
||||
.copy("GRDBTests/Betty.jpeg"),
|
||||
@@ -92,7 +95,28 @@ let package = Package(
|
||||
.swiftLanguageMode(.v5),
|
||||
.enableUpcomingFeature("InferSendableFromCaptures"),
|
||||
.enableUpcomingFeature("GlobalActorIsolatedTypesUsability"),
|
||||
])
|
||||
]),
|
||||
.testTarget(
|
||||
name: "Swift6ApprochableConcurrencyGRDBTests",
|
||||
dependencies: ["GRDB"],
|
||||
swiftSettings: [
|
||||
.enableUpcomingFeature("NonisolatedNonsendingByDefault"),
|
||||
.enableUpcomingFeature("InferIsolatedConformances")
|
||||
]
|
||||
),
|
||||
.testTarget(
|
||||
name: "Swift6DefaultGRDBTests",
|
||||
dependencies: ["GRDB"]
|
||||
),
|
||||
.testTarget(
|
||||
name: "Swift6MainActorIsolationGRDBTests",
|
||||
dependencies: ["GRDB"],
|
||||
swiftSettings: [
|
||||
.defaultIsolation(MainActor.self),
|
||||
.enableUpcomingFeature("NonisolatedNonsendingByDefault"),
|
||||
.enableUpcomingFeature("InferIsolatedConformances")
|
||||
]
|
||||
),
|
||||
],
|
||||
swiftLanguageModes: [.v6]
|
||||
)
|
||||
@@ -0,0 +1,105 @@
|
||||
// swift-tools-version:6.1
|
||||
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||
|
||||
import Foundation
|
||||
import PackageDescription
|
||||
|
||||
let darwinPlatforms: [Platform] = [
|
||||
.iOS,
|
||||
.macOS,
|
||||
.macCatalyst,
|
||||
.tvOS,
|
||||
.visionOS,
|
||||
.watchOS,
|
||||
]
|
||||
var swiftSettings: [SwiftSetting] = [
|
||||
.define("SQLITE_ENABLE_FTS5"),
|
||||
.define("SQLITE_ENABLE_SNAPSHOT"),
|
||||
]
|
||||
var cSettings: [CSetting] = []
|
||||
var dependencies: [PackageDescription.Package.Dependency] = []
|
||||
|
||||
// Don't rely on those environment variables. They are ONLY testing conveniences:
|
||||
// $ SQLITE_ENABLE_PREUPDATE_HOOK=1 make test_SPM
|
||||
if ProcessInfo.processInfo.environment["SQLITE_ENABLE_PREUPDATE_HOOK"] == "1" {
|
||||
swiftSettings.append(.define("SQLITE_ENABLE_PREUPDATE_HOOK"))
|
||||
cSettings.append(.define("GRDB_SQLITE_ENABLE_PREUPDATE_HOOK"))
|
||||
}
|
||||
|
||||
// The SPI_BUILDER environment variable enables documentation building
|
||||
// on <https://swiftpackageindex.com/groue/GRDB.swift>. See
|
||||
// <https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/2122>
|
||||
// for more information.
|
||||
//
|
||||
// SPI_BUILDER also enables the `make docs-localhost` command.
|
||||
if ProcessInfo.processInfo.environment["SPI_BUILDER"] == "1" {
|
||||
dependencies.append(.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"))
|
||||
}
|
||||
|
||||
let package = Package(
|
||||
name: "GRDB",
|
||||
defaultLocalization: "en", // for tests
|
||||
platforms: [
|
||||
.iOS(.v13),
|
||||
.macOS(.v10_15),
|
||||
.tvOS(.v13),
|
||||
.watchOS(.v7),
|
||||
],
|
||||
products: [
|
||||
.library(name: "GRDBSQLite", targets: ["GRDBSQLite"]),
|
||||
.library(name: "GRDB", targets: ["GRDB"]),
|
||||
.library(name: "GRDB-dynamic", type: .dynamic, targets: ["GRDB"]),
|
||||
],
|
||||
dependencies: dependencies,
|
||||
targets: [
|
||||
.systemLibrary(
|
||||
name: "GRDBSQLite",
|
||||
providers: [.apt(["libsqlite3-dev"])]),
|
||||
.target(
|
||||
name: "GRDB",
|
||||
dependencies: [
|
||||
.target(name: "GRDBSQLite"),
|
||||
],
|
||||
path: "GRDB",
|
||||
resources: [.copy("PrivacyInfo.xcprivacy")],
|
||||
cSettings: cSettings,
|
||||
swiftSettings: swiftSettings),
|
||||
.testTarget(
|
||||
name: "GRDBTests",
|
||||
dependencies: ["GRDB"],
|
||||
path: "Tests",
|
||||
exclude: [
|
||||
"CocoaPods",
|
||||
"Crash",
|
||||
"CustomSQLite",
|
||||
"GRDBManualInstall",
|
||||
"GRDBTests/getThreadsCount.c",
|
||||
"Info.plist",
|
||||
"Performance",
|
||||
"SPM",
|
||||
"Swift6Migration",
|
||||
"generatePerformanceReport.rb",
|
||||
"parsePerformanceTests.rb",
|
||||
"Swift6ApprochableConcurrencyGRDBTests",
|
||||
"Swift6DefaultGRDBTests",
|
||||
"Swift6MainActorIsolationGRDBTests",
|
||||
],
|
||||
resources: [
|
||||
.copy("GRDBTests/Betty.jpeg"),
|
||||
.copy("GRDBTests/InflectionsTests.json"),
|
||||
.copy("GRDBTests/Issue1383.sqlite"),
|
||||
],
|
||||
cSettings: cSettings,
|
||||
swiftSettings: swiftSettings + [
|
||||
// Tests still use the Swift 5 language mode.
|
||||
.swiftLanguageMode(.v5),
|
||||
.enableUpcomingFeature("InferSendableFromCaptures"),
|
||||
.enableUpcomingFeature("GlobalActorIsolatedTypesUsability"),
|
||||
]),
|
||||
.testTarget(
|
||||
name: "Swift6DefaultGRDBTests",
|
||||
dependencies: ["GRDB"]
|
||||
),
|
||||
],
|
||||
swiftLanguageModes: [.v6]
|
||||
)
|
||||
@@ -0,0 +1,69 @@
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
private struct Player {
|
||||
var id: Int64?
|
||||
var name: String
|
||||
}
|
||||
|
||||
extension Player: Codable, Identifiable { }
|
||||
extension Player: FetchableRecord, TableRecord {
|
||||
enum Columns {
|
||||
static let name = Column(CodingKeys.name)
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct FetchableRecordTests {
|
||||
@Test func fetch_record() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let player = try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO player(name) VALUES ('Arthur')
|
||||
""")
|
||||
|
||||
return try Player.fetchOne(db)
|
||||
}
|
||||
|
||||
#expect(player?.name == "Arthur")
|
||||
}
|
||||
|
||||
@Test func fetch_record_by_id() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let player = try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO player(id, name) VALUES (42, 'Arthur')
|
||||
""")
|
||||
|
||||
return try Player.find(db, id: 42)
|
||||
}
|
||||
|
||||
#expect(player.name == "Arthur")
|
||||
}
|
||||
|
||||
@Test func fetch_record_by_column() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let player = try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO player(id, name) VALUES (42, 'Arthur')
|
||||
""")
|
||||
|
||||
return try Player
|
||||
.filter { $0.name == "Arthur" }
|
||||
.fetchOne(db)
|
||||
}
|
||||
|
||||
#expect(player?.id == 42)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
private struct Player {
|
||||
var id: Int64?
|
||||
var name: String
|
||||
}
|
||||
|
||||
extension Player: Codable, Identifiable { }
|
||||
extension Player: MutablePersistableRecord {
|
||||
mutating func didInsert(_ inserted: InsertionSuccess) {
|
||||
id = inserted.rowID
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct MutablePersistableRecordTests {
|
||||
@Test func insert_record() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
|
||||
var player = Player(name: "Arthur")
|
||||
try player.insert(db)
|
||||
#expect(player.id == 1)
|
||||
|
||||
let playerCount = try Player.fetchCount(db)
|
||||
#expect(playerCount == 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
private class NonSendable {
|
||||
var value: Int
|
||||
|
||||
init(value: Int) {
|
||||
self.value = value
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct NonSendableTests {
|
||||
@Test func non_sendable_type_in_synchronous_database_access() throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let input = NonSendable(value: 0)
|
||||
let output = try dbQueue.read { _ in
|
||||
input
|
||||
}
|
||||
#expect(input === output)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseReader_read() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.read { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseReader_unsafeRead() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.unsafeRead { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseWriter_barrierWriteWithoutTransaction() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.barrierWriteWithoutTransaction { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseWriter_write() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.write { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseWriter_writeWithoutTransaction() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.writeWithoutTransaction { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
private struct Player {
|
||||
var id: Int64
|
||||
var name: String
|
||||
}
|
||||
|
||||
extension Player: Codable, Identifiable { }
|
||||
extension Player: PersistableRecord { }
|
||||
|
||||
@Suite struct PersistableRecordTests {
|
||||
@Test func insert_record() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
|
||||
try Player(id: 1, name: "Arthur").insert(db)
|
||||
let playerCount = try Player.fetchCount(db)
|
||||
#expect(playerCount == 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
private struct Player {
|
||||
var id: Int64?
|
||||
var name: String
|
||||
}
|
||||
|
||||
extension Player: Codable, Identifiable { }
|
||||
extension Player: FetchableRecord, TableRecord {
|
||||
enum Columns {
|
||||
static let name = Column(CodingKeys.name)
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct FetchableRecordTests {
|
||||
@Test func fetch_record() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let player = try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO player(name) VALUES ('Arthur')
|
||||
""")
|
||||
|
||||
return try Player.fetchOne(db)
|
||||
}
|
||||
|
||||
#expect(player?.name == "Arthur")
|
||||
}
|
||||
|
||||
@Test func fetch_record_by_id() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let player = try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO player(id, name) VALUES (42, 'Arthur')
|
||||
""")
|
||||
|
||||
return try Player.find(db, id: 42)
|
||||
}
|
||||
|
||||
#expect(player.name == "Arthur")
|
||||
}
|
||||
|
||||
@Test func fetch_record_by_column() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let player = try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO player(id, name) VALUES (42, 'Arthur')
|
||||
""")
|
||||
|
||||
return try Player
|
||||
.filter { $0.name == "Arthur" }
|
||||
.fetchOne(db)
|
||||
}
|
||||
|
||||
#expect(player?.id == 42)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
private struct Player {
|
||||
var id: Int64?
|
||||
var name: String
|
||||
}
|
||||
|
||||
extension Player: Codable, Identifiable { }
|
||||
extension Player: MutablePersistableRecord {
|
||||
mutating func didInsert(_ inserted: InsertionSuccess) {
|
||||
id = inserted.rowID
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct MutablePersistableRecordTests {
|
||||
@Test func insert_record() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
|
||||
var player = Player(name: "Arthur")
|
||||
try player.insert(db)
|
||||
#expect(player.id == 1)
|
||||
|
||||
let playerCount = try Player.fetchCount(db)
|
||||
#expect(playerCount == 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
private class NonSendable {
|
||||
var value: Int
|
||||
|
||||
init(value: Int) {
|
||||
self.value = value
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct NonSendableTests {
|
||||
@Test func non_sendable_type_in_synchronous_database_access() throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let input = NonSendable(value: 0)
|
||||
let output = try dbQueue.read { _ in
|
||||
input
|
||||
}
|
||||
#expect(input === output)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseReader_read() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.read { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseReader_unsafeRead() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.unsafeRead { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseWriter_barrierWriteWithoutTransaction() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.barrierWriteWithoutTransaction { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseWriter_write() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.write { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseWriter_writeWithoutTransaction() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.writeWithoutTransaction { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
private struct Player {
|
||||
var id: Int64
|
||||
var name: String
|
||||
}
|
||||
|
||||
extension Player: Codable, Identifiable { }
|
||||
extension Player: PersistableRecord { }
|
||||
|
||||
@Suite struct PersistableRecordTests {
|
||||
@Test func insert_record() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
|
||||
try Player(id: 1, name: "Arthur").insert(db)
|
||||
let playerCount = try Player.fetchCount(db)
|
||||
#expect(playerCount == 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
nonisolated private struct Player: Codable {
|
||||
var id: Int64?
|
||||
var name: String
|
||||
}
|
||||
|
||||
extension Player: nonisolated Identifiable { }
|
||||
extension Player: nonisolated FetchableRecord, TableRecord {
|
||||
nonisolated enum Columns {
|
||||
static let name = Column(CodingKeys.name)
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct FetchableRecordTests {
|
||||
@Test func fetch_record() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let player = try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO player(name) VALUES ('Arthur')
|
||||
""")
|
||||
|
||||
return try Player.fetchOne(db)
|
||||
}
|
||||
|
||||
#expect(player?.name == "Arthur")
|
||||
}
|
||||
|
||||
@Test func fetch_record_by_id() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let player = try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO player(id, name) VALUES (42, 'Arthur')
|
||||
""")
|
||||
|
||||
return try Player.find(db, id: 42)
|
||||
}
|
||||
|
||||
#expect(player.name == "Arthur")
|
||||
}
|
||||
|
||||
@Test func fetch_record_by_column() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let player = try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
try db.execute(sql: """
|
||||
INSERT INTO player(id, name) VALUES (42, 'Arthur')
|
||||
""")
|
||||
|
||||
return try Player
|
||||
.filter { $0.name == "Arthur" }
|
||||
.fetchOne(db)
|
||||
}
|
||||
|
||||
#expect(player?.id == 42)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
nonisolated private struct Player: Codable {
|
||||
var id: Int64?
|
||||
var name: String
|
||||
}
|
||||
|
||||
extension Player: nonisolated Identifiable { }
|
||||
extension Player: MutablePersistableRecord {
|
||||
nonisolated mutating func didInsert(_ inserted: InsertionSuccess) {
|
||||
id = inserted.rowID
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct MutablePersistableRecordTests {
|
||||
@Test func insert_record() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
|
||||
var player = Player(name: "Arthur")
|
||||
try player.insert(db)
|
||||
#expect(player.id == 1)
|
||||
|
||||
let playerCount = try Player.fetchCount(db)
|
||||
#expect(playerCount == 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
nonisolated private class NonSendable {
|
||||
var value: Int
|
||||
|
||||
init(value: Int) {
|
||||
self.value = value
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct NonSendableTests {
|
||||
@Test func non_sendable_type_in_synchronous_database_access() throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let input = NonSendable(value: 0)
|
||||
let output = try dbQueue.read { _ in
|
||||
input
|
||||
}
|
||||
#expect(input === output)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseReader_read() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.read { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseReader_unsafeRead() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.unsafeRead { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseWriter_barrierWriteWithoutTransaction() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.barrierWriteWithoutTransaction { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseWriter_write() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.write { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
|
||||
@Test func non_sendable_type_sent_to_databaseWriter_writeWithoutTransaction() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
let nonSendable = NonSendable(value: 0)
|
||||
let value = try await dbQueue.writeWithoutTransaction { _ in
|
||||
nonSendable.value
|
||||
}
|
||||
#expect(value == 0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import GRDB
|
||||
import Testing
|
||||
|
||||
nonisolated private struct Player: Codable {
|
||||
var id: Int64
|
||||
var name: String
|
||||
}
|
||||
|
||||
extension Player: nonisolated Identifiable { }
|
||||
extension Player: PersistableRecord { }
|
||||
|
||||
@Suite struct PersistableRecordTests {
|
||||
@Test func insert_record() async throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
try await dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
|
||||
try Player(id: 1, name: "Arthur").insert(db)
|
||||
let playerCount = try Player.fetchCount(db)
|
||||
#expect(playerCount == 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user