mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-22 04:00:39 +00:00
DatabaseFunction is Sendable
This commit is contained in:
1 parent
05f7d3c8d4
commit
6e691fe71d
5 files changed
+18
-20
No files matched your search
@@ -29,7 +29,7 @@ import SQLite3
|
||||
/// - ``localizedUppercase``
|
||||
/// - ``lowercase``
|
||||
/// - ``uppercase``
|
||||
public final class DatabaseFunction: Hashable {
|
||||
public final class DatabaseFunction: Hashable, Sendable {
|
||||
// SQLite identifies functions by (name + argument count)
|
||||
private struct Identity: Hashable {
|
||||
let name: String
|
||||
@@ -82,11 +82,11 @@ public final class DatabaseFunction: Hashable {
|
||||
_ name: String,
|
||||
argumentCount: Int? = nil,
|
||||
pure: Bool = false,
|
||||
function: @escaping ([DatabaseValue]) throws -> (any DatabaseValueConvertible)?)
|
||||
function: @escaping @Sendable ([DatabaseValue]) throws -> (any DatabaseValueConvertible)?)
|
||||
{
|
||||
self.identity = Identity(name: name, nArg: argumentCount.map(CInt.init) ?? -1)
|
||||
self.isPure = pure
|
||||
self.kind = .function{ (argc, argv) in
|
||||
self.kind = .function { (argc, argv) in
|
||||
let arguments = (0..<Int(argc)).map { index in
|
||||
DatabaseValue(sqliteValue: argv.unsafelyUnwrapped[index]!)
|
||||
}
|
||||
@@ -274,12 +274,12 @@ public final class DatabaseFunction: Hashable {
|
||||
|
||||
/// A function kind: an "SQL function" or an "aggregate".
|
||||
/// See <http://sqlite.org/capi3ref.html#sqlite3_create_function>
|
||||
private enum Kind {
|
||||
private enum Kind: Sendable {
|
||||
/// A regular function: SELECT f(1)
|
||||
case function((CInt, UnsafeMutablePointer<OpaquePointer?>?) throws -> (any DatabaseValueConvertible)?)
|
||||
case function(@Sendable (CInt, UnsafeMutablePointer<OpaquePointer?>?) throws -> (any DatabaseValueConvertible)?)
|
||||
|
||||
/// An aggregate: SELECT f(foo) FROM bar GROUP BY baz
|
||||
case aggregate(() -> any DatabaseAggregate)
|
||||
case aggregate(@Sendable () -> any DatabaseAggregate)
|
||||
|
||||
/// Feeds the `pApp` parameter of sqlite3_create_function_v2
|
||||
/// <http://sqlite.org/capi3ref.html#sqlite3_create_function>
|
||||
|
||||
@@ -113,7 +113,6 @@
|
||||
- [X] configuration
|
||||
- [X] customtransactionclock
|
||||
- [X] database/busymode
|
||||
- [ ] databaseaggregate
|
||||
- [ ] databasecancellable
|
||||
- [ ] databasecollation
|
||||
- [X] databasecolumndecodingstrategy
|
||||
@@ -122,7 +121,7 @@
|
||||
- [X] databasedataencodingstrategy
|
||||
- [X] databasedatedecodingstrategy
|
||||
- [X] databasedateencodingstrategy
|
||||
- [ ] databasefunction
|
||||
- [X] databasefunction
|
||||
- [ ] databasemigrator
|
||||
- [ ] databasepublishers/databaseregion
|
||||
- [ ] databasepublishers/migrate
|
||||
|
||||
@@ -347,13 +347,13 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
|
||||
func testFunctionsAreClosures() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
var x = 123
|
||||
let mutex = Mutex(123)
|
||||
let fn = DatabaseFunction("f", argumentCount: 0) { dbValues in
|
||||
return x
|
||||
return mutex.value
|
||||
}
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
x = 321
|
||||
mutex.value = 321
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT f()")!, 321)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,20 +139,20 @@ class SelectStatementTests : GRDBTestCase {
|
||||
func testCachedSelectStatementStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
var needsThrow = false
|
||||
let needsThrowMutex = Mutex(false)
|
||||
db.add(function: DatabaseFunction("bomb", argumentCount: 0, pure: false) { _ in
|
||||
if needsThrow {
|
||||
if needsThrowMutex.value {
|
||||
throw DatabaseError(message: "boom")
|
||||
}
|
||||
return "success"
|
||||
})
|
||||
let sql = "SELECT bomb()"
|
||||
|
||||
needsThrow = false
|
||||
needsThrowMutex.value = false
|
||||
XCTAssertEqual(try String.fetchAll(db.cachedStatement(sql: sql)), ["success"])
|
||||
|
||||
do {
|
||||
needsThrow = true
|
||||
needsThrowMutex.value = true
|
||||
_ = try String.fetchAll(db.cachedStatement(sql: sql))
|
||||
XCTFail()
|
||||
} catch let error as DatabaseError {
|
||||
@@ -162,7 +162,7 @@ class SelectStatementTests : GRDBTestCase {
|
||||
XCTAssertEqual(error.description, "SQLite error 1: boom - while executing `\(sql)`")
|
||||
}
|
||||
|
||||
needsThrow = false
|
||||
needsThrowMutex.value = false
|
||||
XCTAssertEqual(try String.fetchAll(db.cachedStatement(sql: sql)), ["success"])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,17 +178,16 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
|
||||
func testUpdateStatementAcceptsSelectQueriesAndConsumeAllRows() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
var index = 0
|
||||
let indexMutex = Mutex(0)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: DatabaseFunction("seq", argumentCount: 0, pure: false) { _ in
|
||||
defer { index += 1 }
|
||||
return index
|
||||
indexMutex.increment()
|
||||
})
|
||||
try db.execute(sql: "SELECT seq() UNION ALL SELECT seq() UNION ALL SELECT seq()")
|
||||
let statement = try db.makeStatement(sql: "SELECT seq() UNION ALL SELECT seq() UNION ALL SELECT seq()")
|
||||
try statement.execute()
|
||||
}
|
||||
XCTAssertEqual(index, 3 + 3)
|
||||
XCTAssertEqual(indexMutex.value, 3 + 3)
|
||||
}
|
||||
|
||||
func testExecuteNothing() throws {
|
||||
|
||||
Reference in new issue
Block a user