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