diff --git a/Sources/StructuredQueriesCore/Documentation.docc/Articles/DefiningYourSchema.md b/Sources/StructuredQueriesCore/Documentation.docc/Articles/DefiningYourSchema.md index 8e6a9720..b84137b0 100644 --- a/Sources/StructuredQueriesCore/Documentation.docc/Articles/DefiningYourSchema.md +++ b/Sources/StructuredQueriesCore/Documentation.docc/Articles/DefiningYourSchema.md @@ -51,6 +51,10 @@ To define a Swift data type that represents this table, one can use the `@Table` } ``` +> Note: If your project is using [default main actor isolation] then you further need to annotate +> your struct as `nonisolated`. +[default main actor isolation]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0466-control-default-actor-isolation.md + Note that the struct's field names match the column tables of the table exactly. In order to support property names that differ from the columns names, you can use the `@Column` macro. See the section below, , for more information on how to customize your diff --git a/Sources/StructuredQueriesCore/Documentation.docc/Articles/QueryCookbook.md b/Sources/StructuredQueriesCore/Documentation.docc/Articles/QueryCookbook.md index 85ffa293..5da62973 100644 --- a/Sources/StructuredQueriesCore/Documentation.docc/Articles/QueryCookbook.md +++ b/Sources/StructuredQueriesCore/Documentation.docc/Articles/QueryCookbook.md @@ -50,6 +50,10 @@ extension Reminder { } ``` +> Note: If your project is using [default main actor isolation] then you further need to annotate +> your extension as `nonisolated`, or define the helpers directly in the declaration of the struct. +[default main actor isolation]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0466-control-default-actor-isolation.md + Then these helpers can be used when composing together a larger, more complex query. For example, we can select all non-deleted lists with the count of all non-deleted reminders in each list like so: @@ -131,6 +135,10 @@ extension Reminder.TableColumns { } ``` +> Note: If your project is using [default main actor isolation] then you further need to annotate +> your extension as `nonisolated`. +[default main actor isolation]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0466-control-default-actor-isolation.md + Then you can use these helpers when building a query. For example, you can use ``PrimaryKeyedTableDefinition/count(distinct:filter:)`` to count the number of past due, current and scheduled reminders in one single query like so: diff --git a/Sources/StructuredQueriesSQLiteCore/Documentation.docc/Articles/CustomFunctions.md b/Sources/StructuredQueriesSQLiteCore/Documentation.docc/Articles/CustomFunctions.md index cb72050a..7d4b76b8 100644 --- a/Sources/StructuredQueriesSQLiteCore/Documentation.docc/Articles/CustomFunctions.md +++ b/Sources/StructuredQueriesSQLiteCore/Documentation.docc/Articles/CustomFunctions.md @@ -18,6 +18,10 @@ func exclaim(_ string: String) -> String { } ``` +> Note: If your project is using [default main actor isolation] then you further need to annotate +> your function as `nonisolated`. +[default main actor isolation]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0466-control-default-actor-isolation.md + And will be immediately callable in a query by prefixing the function with `$`: ```swift diff --git a/Sources/StructuredQueriesSQLiteMacros/DatabaseFunctionMacro.swift b/Sources/StructuredQueriesSQLiteMacros/DatabaseFunctionMacro.swift index 5fc5190a..ff0f7bb7 100644 --- a/Sources/StructuredQueriesSQLiteMacros/DatabaseFunctionMacro.swift +++ b/Sources/StructuredQueriesSQLiteMacros/DatabaseFunctionMacro.swift @@ -226,12 +226,12 @@ extension DatabaseFunctionMacro: PeerMacro { return [ """ - \(attributes)\(access)\(`static`)var $\(raw: declarationName): \(functionTypeName) { + \(attributes)\(access)\(`static`)\(nonisolated)var $\(raw: declarationName): \(functionTypeName) { \(functionTypeName)(\(declaration.name.trimmed)) } """, """ - \(attributes)\(access)struct \(functionTypeName): \ + \(attributes)\(access)\(nonisolated)struct \(functionTypeName): \ StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = \(raw: representableInputType) public typealias Output = \(representableOutputType) diff --git a/Sources/StructuredQueriesSQLiteMacros/Nonisolated.swift b/Sources/StructuredQueriesSQLiteMacros/Nonisolated.swift new file mode 100644 index 00000000..34418215 --- /dev/null +++ b/Sources/StructuredQueriesSQLiteMacros/Nonisolated.swift @@ -0,0 +1,7 @@ +import SwiftSyntax + +#if compiler(>=6.1) + let nonisolated: TokenSyntax? = .keyword(.nonisolated, trailingTrivia: .space) +#else + let nonisolated: TokenSyntax? = nil +#endif diff --git a/Tests/StructuredQueriesMacrosTests/DatabaseFunctionMacroTests.swift b/Tests/StructuredQueriesMacrosTests/DatabaseFunctionMacroTests.swift index 4ffdce05..09f50bde 100644 --- a/Tests/StructuredQueriesMacrosTests/DatabaseFunctionMacroTests.swift +++ b/Tests/StructuredQueriesMacrosTests/DatabaseFunctionMacroTests.swift @@ -19,11 +19,11 @@ extension SnapshotTests { Date() } - var $currentDate: __macro_local_11currentDatefMu_ { + nonisolated var $currentDate: __macro_local_11currentDatefMu_ { __macro_local_11currentDatefMu_(currentDate) } - struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = () public typealias Output = Date public let name = "currentDate" @@ -71,11 +71,11 @@ extension SnapshotTests { Date() } - var $currentDate: __macro_local_11currentDatefMu_ { + nonisolated var $currentDate: __macro_local_11currentDatefMu_ { __macro_local_11currentDatefMu_(currentDate) } - struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = () public typealias Output = Date public let name = "current_date" @@ -123,11 +123,11 @@ extension SnapshotTests { strings.map { $0.capitalized } } - var $jsonCapitalize: __macro_local_14jsonCapitalizefMu_ { + nonisolated var $jsonCapitalize: __macro_local_14jsonCapitalizefMu_ { __macro_local_14jsonCapitalizefMu_(jsonCapitalize) } - struct __macro_local_14jsonCapitalizefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_14jsonCapitalizefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = [String].JSONRepresentation public typealias Output = [String].JSONRepresentation public let name = "jsonCapitalize" @@ -181,11 +181,11 @@ extension SnapshotTests { 42 } - var $fortyTwo: __macro_local_8fortyTwofMu_ { + nonisolated var $fortyTwo: __macro_local_8fortyTwofMu_ { __macro_local_8fortyTwofMu_(fortyTwo) } - struct __macro_local_8fortyTwofMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_8fortyTwofMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = () public typealias Output = Int public let name = "fortyTwo" @@ -233,11 +233,11 @@ extension SnapshotTests { dateFormatter.date(from: format) } - var $currentDate: __macro_local_11currentDatefMu_ { + nonisolated var $currentDate: __macro_local_11currentDatefMu_ { __macro_local_11currentDatefMu_(currentDate) } - struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = String public typealias Output = Date? public let name = "currentDate" @@ -291,11 +291,11 @@ extension SnapshotTests { dateFormatter.date(from: format) } - var $currentDate: __macro_local_11currentDatefMu_ { + nonisolated var $currentDate: __macro_local_11currentDatefMu_ { __macro_local_11currentDatefMu_(currentDate) } - struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = String public typealias Output = Date? public let name = "currentDate" @@ -349,11 +349,11 @@ extension SnapshotTests { dateFormatter.date(from: format) } - var $currentDate: __macro_local_11currentDatefMu_ { + nonisolated var $currentDate: __macro_local_11currentDatefMu_ { __macro_local_11currentDatefMu_(currentDate) } - struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = String public typealias Output = Date? public let name = "currentDate" @@ -407,11 +407,11 @@ extension SnapshotTests { dateFormatter.date(from: format) } - var $currentDate: __macro_local_11currentDatefMu_ { + nonisolated var $currentDate: __macro_local_11currentDatefMu_ { __macro_local_11currentDatefMu_(currentDate) } - struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = String public typealias Output = Date? public let name = "currentDate" @@ -465,11 +465,11 @@ extension SnapshotTests { first + second } - var $concat: __macro_local_6concatfMu_ { + nonisolated var $concat: __macro_local_6concatfMu_ { __macro_local_6concatfMu_(concat) } - struct __macro_local_6concatfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_6concatfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = (String, String) public typealias Output = String public let name = "concat" @@ -545,11 +545,11 @@ extension SnapshotTests { dateFormatter.date(from: format) } - var $currentDate: __macro_local_11currentDatefMu_ { + nonisolated var $currentDate: __macro_local_11currentDatefMu_ { __macro_local_11currentDatefMu_(currentDate) } - struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = String? public typealias Output = Date? public let name = "currentDate" @@ -603,11 +603,11 @@ extension SnapshotTests { Date() } - var $currentDate: __macro_local_11currentDatefMu_ { + nonisolated var $currentDate: __macro_local_11currentDatefMu_ { __macro_local_11currentDatefMu_(currentDate) } - struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = () public typealias Output = Date public let name = "currentDate" @@ -659,11 +659,11 @@ extension SnapshotTests { Date() } - var $currentDate: __macro_local_11currentDatefMu_ { + nonisolated var $currentDate: __macro_local_11currentDatefMu_ { __macro_local_11currentDatefMu_(currentDate) } - struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = () public typealias Output = Date public let name = "currentDate" @@ -715,11 +715,11 @@ extension SnapshotTests { Date() } - public var $currentDate: __macro_local_11currentDatefMu_ { + public nonisolated var $currentDate: __macro_local_11currentDatefMu_ { __macro_local_11currentDatefMu_(currentDate) } - public struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + public nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = () public typealias Output = Date public let name = "currentDate" @@ -767,11 +767,11 @@ extension SnapshotTests { Date() } - static var $currentDate: __macro_local_11currentDatefMu_ { + static nonisolated var $currentDate: __macro_local_11currentDatefMu_ { __macro_local_11currentDatefMu_(currentDate) } - struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = () public typealias Output = Date public let name = "currentDate" @@ -842,11 +842,11 @@ extension SnapshotTests { Date() } - @available(*, unavailable) var $currentDate: __macro_local_11currentDatefMu_ { + @available(*, unavailable) nonisolated var $currentDate: __macro_local_11currentDatefMu_ { __macro_local_11currentDatefMu_(currentDate) } - @available(*, unavailable) struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + @available(*, unavailable) nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = () public typealias Output = Date public let name = "currentDate" @@ -894,11 +894,11 @@ extension SnapshotTests { 42 } - public var $default: __macro_local_7defaultfMu_ { + public nonisolated var $default: __macro_local_7defaultfMu_ { __macro_local_7defaultfMu_(`default`) } - public struct __macro_local_7defaultfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + public nonisolated struct __macro_local_7defaultfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = () public typealias Output = Int public let name = "default" @@ -946,11 +946,11 @@ extension SnapshotTests { print("...") } - public var $void: __macro_local_4voidfMu_ { + public nonisolated var $void: __macro_local_4voidfMu_ { __macro_local_4voidfMu_(void) } - public struct __macro_local_4voidfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + public nonisolated struct __macro_local_4voidfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = () public typealias Output = Swift.Void public let name = "void" @@ -993,11 +993,11 @@ extension SnapshotTests { throw Failure() } - public var $void: __macro_local_4voidfMu_ { + public nonisolated var $void: __macro_local_4voidfMu_ { __macro_local_4voidfMu_(void) } - public struct __macro_local_4voidfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + public nonisolated struct __macro_local_4voidfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = () public typealias Output = Swift.Void public let name = "void" @@ -1053,11 +1053,11 @@ extension SnapshotTests { Swift.min(x, y) } - var $min: __macro_local_3minfMu_ { + nonisolated var $min: __macro_local_3minfMu_ { __macro_local_3minfMu_(min) } - struct __macro_local_3minfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_3minfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = (Int, Int) public typealias Output = Swift.Void public let name = "min" @@ -1120,11 +1120,11 @@ extension SnapshotTests { Swift.min(x, y) } - var $min: __macro_local_3minfMu_ { + nonisolated var $min: __macro_local_3minfMu_ { __macro_local_3minfMu_(min) } - struct __macro_local_3minfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_3minfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = (Int, Int) public typealias Output = Swift.Void public let name = "min" @@ -1184,11 +1184,11 @@ extension SnapshotTests { !reminder.title.isEmpty || override } - var $isValid: __macro_local_7isValidfMu_ { + nonisolated var $isValid: __macro_local_7isValidfMu_ { __macro_local_7isValidfMu_(isValid) } - struct __macro_local_7isValidfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { + nonisolated struct __macro_local_7isValidfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction { public typealias Input = (Reminder, Bool) public typealias Output = Bool public let name = "isValid"