Skip to content

Commit edb84b3

Browse files
authored
Enforce database functions to be nonisolated (#205)
* Database functions are always non-isolated. * wip * wip * update tests * clean up * docs
1 parent 6426bec commit edb84b3

File tree

6 files changed

+67
-44
lines changed

6 files changed

+67
-44
lines changed

Sources/StructuredQueriesCore/Documentation.docc/Articles/DefiningYourSchema.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ To define a Swift data type that represents this table, one can use the `@Table`
5151
}
5252
```
5353

54+
> Note: If your project is using [default main actor isolation] then you further need to annotate
55+
> your struct as `nonisolated`.
56+
[default main actor isolation]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0466-control-default-actor-isolation.md
57+
5458
Note that the struct's field names match the column tables of the table exactly. In order to support
5559
property names that differ from the columns names, you can use the `@Column` macro. See the section
5660
below, <doc:DefiningYourSchema#Customizing-a-table>, for more information on how to customize your

Sources/StructuredQueriesCore/Documentation.docc/Articles/QueryCookbook.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ extension Reminder {
5050
}
5151
```
5252

53+
> Note: If your project is using [default main actor isolation] then you further need to annotate
54+
> your extension as `nonisolated`, or define the helpers directly in the declaration of the struct.
55+
[default main actor isolation]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0466-control-default-actor-isolation.md
56+
5357
Then these helpers can be used when composing together a larger, more complex query. For example,
5458
we can select all non-deleted lists with the count of all non-deleted reminders in each list like
5559
so:
@@ -131,6 +135,10 @@ extension Reminder.TableColumns {
131135
}
132136
```
133137
138+
> Note: If your project is using [default main actor isolation] then you further need to annotate
139+
> your extension as `nonisolated`.
140+
[default main actor isolation]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0466-control-default-actor-isolation.md
141+
134142
Then you can use these helpers when building a query. For example, you can use
135143
``PrimaryKeyedTableDefinition/count(distinct:filter:)`` to count the number of past due, current and
136144
scheduled reminders in one single query like so:

Sources/StructuredQueriesSQLiteCore/Documentation.docc/Articles/CustomFunctions.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ func exclaim(_ string: String) -> String {
1818
}
1919
```
2020

21+
> Note: If your project is using [default main actor isolation] then you further need to annotate
22+
> your function as `nonisolated`.
23+
[default main actor isolation]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0466-control-default-actor-isolation.md
24+
2125
And will be immediately callable in a query by prefixing the function with `$`:
2226

2327
```swift

Sources/StructuredQueriesSQLiteMacros/DatabaseFunctionMacro.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ extension DatabaseFunctionMacro: PeerMacro {
226226

227227
return [
228228
"""
229-
\(attributes)\(access)\(`static`)var $\(raw: declarationName): \(functionTypeName) {
229+
\(attributes)\(access)\(`static`)\(nonisolated)var $\(raw: declarationName): \(functionTypeName) {
230230
\(functionTypeName)(\(declaration.name.trimmed))
231231
}
232232
""",
233233
"""
234-
\(attributes)\(access)struct \(functionTypeName): \
234+
\(attributes)\(access)\(nonisolated)struct \(functionTypeName): \
235235
StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
236236
public typealias Input = \(raw: representableInputType)
237237
public typealias Output = \(representableOutputType)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import SwiftSyntax
2+
3+
#if compiler(>=6.1)
4+
let nonisolated: TokenSyntax? = .keyword(.nonisolated, trailingTrivia: .space)
5+
#else
6+
let nonisolated: TokenSyntax? = nil
7+
#endif

Tests/StructuredQueriesMacrosTests/DatabaseFunctionMacroTests.swift

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ extension SnapshotTests {
1919
Date()
2020
}
2121
22-
var $currentDate: __macro_local_11currentDatefMu_ {
22+
nonisolated var $currentDate: __macro_local_11currentDatefMu_ {
2323
__macro_local_11currentDatefMu_(currentDate)
2424
}
2525
26-
struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
26+
nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
2727
public typealias Input = ()
2828
public typealias Output = Date
2929
public let name = "currentDate"
@@ -71,11 +71,11 @@ extension SnapshotTests {
7171
Date()
7272
}
7373
74-
var $currentDate: __macro_local_11currentDatefMu_ {
74+
nonisolated var $currentDate: __macro_local_11currentDatefMu_ {
7575
__macro_local_11currentDatefMu_(currentDate)
7676
}
7777
78-
struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
78+
nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
7979
public typealias Input = ()
8080
public typealias Output = Date
8181
public let name = "current_date"
@@ -123,11 +123,11 @@ extension SnapshotTests {
123123
strings.map { $0.capitalized }
124124
}
125125
126-
var $jsonCapitalize: __macro_local_14jsonCapitalizefMu_ {
126+
nonisolated var $jsonCapitalize: __macro_local_14jsonCapitalizefMu_ {
127127
__macro_local_14jsonCapitalizefMu_(jsonCapitalize)
128128
}
129129
130-
struct __macro_local_14jsonCapitalizefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
130+
nonisolated struct __macro_local_14jsonCapitalizefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
131131
public typealias Input = [String].JSONRepresentation
132132
public typealias Output = [String].JSONRepresentation
133133
public let name = "jsonCapitalize"
@@ -181,11 +181,11 @@ extension SnapshotTests {
181181
42
182182
}
183183
184-
var $fortyTwo: __macro_local_8fortyTwofMu_ {
184+
nonisolated var $fortyTwo: __macro_local_8fortyTwofMu_ {
185185
__macro_local_8fortyTwofMu_(fortyTwo)
186186
}
187187
188-
struct __macro_local_8fortyTwofMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
188+
nonisolated struct __macro_local_8fortyTwofMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
189189
public typealias Input = ()
190190
public typealias Output = Int
191191
public let name = "fortyTwo"
@@ -233,11 +233,11 @@ extension SnapshotTests {
233233
dateFormatter.date(from: format)
234234
}
235235
236-
var $currentDate: __macro_local_11currentDatefMu_ {
236+
nonisolated var $currentDate: __macro_local_11currentDatefMu_ {
237237
__macro_local_11currentDatefMu_(currentDate)
238238
}
239239
240-
struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
240+
nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
241241
public typealias Input = String
242242
public typealias Output = Date?
243243
public let name = "currentDate"
@@ -291,11 +291,11 @@ extension SnapshotTests {
291291
dateFormatter.date(from: format)
292292
}
293293
294-
var $currentDate: __macro_local_11currentDatefMu_ {
294+
nonisolated var $currentDate: __macro_local_11currentDatefMu_ {
295295
__macro_local_11currentDatefMu_(currentDate)
296296
}
297297
298-
struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
298+
nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
299299
public typealias Input = String
300300
public typealias Output = Date?
301301
public let name = "currentDate"
@@ -349,11 +349,11 @@ extension SnapshotTests {
349349
dateFormatter.date(from: format)
350350
}
351351
352-
var $currentDate: __macro_local_11currentDatefMu_ {
352+
nonisolated var $currentDate: __macro_local_11currentDatefMu_ {
353353
__macro_local_11currentDatefMu_(currentDate)
354354
}
355355
356-
struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
356+
nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
357357
public typealias Input = String
358358
public typealias Output = Date?
359359
public let name = "currentDate"
@@ -407,11 +407,11 @@ extension SnapshotTests {
407407
dateFormatter.date(from: format)
408408
}
409409
410-
var $currentDate: __macro_local_11currentDatefMu_ {
410+
nonisolated var $currentDate: __macro_local_11currentDatefMu_ {
411411
__macro_local_11currentDatefMu_(currentDate)
412412
}
413413
414-
struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
414+
nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
415415
public typealias Input = String
416416
public typealias Output = Date?
417417
public let name = "currentDate"
@@ -465,11 +465,11 @@ extension SnapshotTests {
465465
first + second
466466
}
467467
468-
var $concat: __macro_local_6concatfMu_ {
468+
nonisolated var $concat: __macro_local_6concatfMu_ {
469469
__macro_local_6concatfMu_(concat)
470470
}
471471
472-
struct __macro_local_6concatfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
472+
nonisolated struct __macro_local_6concatfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
473473
public typealias Input = (String, String)
474474
public typealias Output = String
475475
public let name = "concat"
@@ -545,11 +545,11 @@ extension SnapshotTests {
545545
dateFormatter.date(from: format)
546546
}
547547
548-
var $currentDate: __macro_local_11currentDatefMu_ {
548+
nonisolated var $currentDate: __macro_local_11currentDatefMu_ {
549549
__macro_local_11currentDatefMu_(currentDate)
550550
}
551551
552-
struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
552+
nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
553553
public typealias Input = String?
554554
public typealias Output = Date?
555555
public let name = "currentDate"
@@ -603,11 +603,11 @@ extension SnapshotTests {
603603
Date()
604604
}
605605
606-
var $currentDate: __macro_local_11currentDatefMu_ {
606+
nonisolated var $currentDate: __macro_local_11currentDatefMu_ {
607607
__macro_local_11currentDatefMu_(currentDate)
608608
}
609609
610-
struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
610+
nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
611611
public typealias Input = ()
612612
public typealias Output = Date
613613
public let name = "currentDate"
@@ -659,11 +659,11 @@ extension SnapshotTests {
659659
Date()
660660
}
661661
662-
var $currentDate: __macro_local_11currentDatefMu_ {
662+
nonisolated var $currentDate: __macro_local_11currentDatefMu_ {
663663
__macro_local_11currentDatefMu_(currentDate)
664664
}
665665
666-
struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
666+
nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
667667
public typealias Input = ()
668668
public typealias Output = Date
669669
public let name = "currentDate"
@@ -715,11 +715,11 @@ extension SnapshotTests {
715715
Date()
716716
}
717717
718-
public var $currentDate: __macro_local_11currentDatefMu_ {
718+
public nonisolated var $currentDate: __macro_local_11currentDatefMu_ {
719719
__macro_local_11currentDatefMu_(currentDate)
720720
}
721721
722-
public struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
722+
public nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
723723
public typealias Input = ()
724724
public typealias Output = Date
725725
public let name = "currentDate"
@@ -767,11 +767,11 @@ extension SnapshotTests {
767767
Date()
768768
}
769769
770-
static var $currentDate: __macro_local_11currentDatefMu_ {
770+
static nonisolated var $currentDate: __macro_local_11currentDatefMu_ {
771771
__macro_local_11currentDatefMu_(currentDate)
772772
}
773773
774-
struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
774+
nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
775775
public typealias Input = ()
776776
public typealias Output = Date
777777
public let name = "currentDate"
@@ -842,11 +842,11 @@ extension SnapshotTests {
842842
Date()
843843
}
844844
845-
@available(*, unavailable) var $currentDate: __macro_local_11currentDatefMu_ {
845+
@available(*, unavailable) nonisolated var $currentDate: __macro_local_11currentDatefMu_ {
846846
__macro_local_11currentDatefMu_(currentDate)
847847
}
848848
849-
@available(*, unavailable) struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
849+
@available(*, unavailable) nonisolated struct __macro_local_11currentDatefMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
850850
public typealias Input = ()
851851
public typealias Output = Date
852852
public let name = "currentDate"
@@ -894,11 +894,11 @@ extension SnapshotTests {
894894
42
895895
}
896896
897-
public var $default: __macro_local_7defaultfMu_ {
897+
public nonisolated var $default: __macro_local_7defaultfMu_ {
898898
__macro_local_7defaultfMu_(`default`)
899899
}
900900
901-
public struct __macro_local_7defaultfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
901+
public nonisolated struct __macro_local_7defaultfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
902902
public typealias Input = ()
903903
public typealias Output = Int
904904
public let name = "default"
@@ -946,11 +946,11 @@ extension SnapshotTests {
946946
print("...")
947947
}
948948
949-
public var $void: __macro_local_4voidfMu_ {
949+
public nonisolated var $void: __macro_local_4voidfMu_ {
950950
__macro_local_4voidfMu_(void)
951951
}
952952
953-
public struct __macro_local_4voidfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
953+
public nonisolated struct __macro_local_4voidfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
954954
public typealias Input = ()
955955
public typealias Output = Swift.Void
956956
public let name = "void"
@@ -993,11 +993,11 @@ extension SnapshotTests {
993993
throw Failure()
994994
}
995995
996-
public var $void: __macro_local_4voidfMu_ {
996+
public nonisolated var $void: __macro_local_4voidfMu_ {
997997
__macro_local_4voidfMu_(void)
998998
}
999999
1000-
public struct __macro_local_4voidfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
1000+
public nonisolated struct __macro_local_4voidfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
10011001
public typealias Input = ()
10021002
public typealias Output = Swift.Void
10031003
public let name = "void"
@@ -1053,11 +1053,11 @@ extension SnapshotTests {
10531053
Swift.min(x, y)
10541054
}
10551055
1056-
var $min: __macro_local_3minfMu_ {
1056+
nonisolated var $min: __macro_local_3minfMu_ {
10571057
__macro_local_3minfMu_(min)
10581058
}
10591059
1060-
struct __macro_local_3minfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
1060+
nonisolated struct __macro_local_3minfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
10611061
public typealias Input = (Int, Int)
10621062
public typealias Output = Swift.Void
10631063
public let name = "min"
@@ -1120,11 +1120,11 @@ extension SnapshotTests {
11201120
Swift.min(x, y)
11211121
}
11221122
1123-
var $min: __macro_local_3minfMu_ {
1123+
nonisolated var $min: __macro_local_3minfMu_ {
11241124
__macro_local_3minfMu_(min)
11251125
}
11261126
1127-
struct __macro_local_3minfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
1127+
nonisolated struct __macro_local_3minfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
11281128
public typealias Input = (Int, Int)
11291129
public typealias Output = Swift.Void
11301130
public let name = "min"
@@ -1184,11 +1184,11 @@ extension SnapshotTests {
11841184
!reminder.title.isEmpty || override
11851185
}
11861186
1187-
var $isValid: __macro_local_7isValidfMu_ {
1187+
nonisolated var $isValid: __macro_local_7isValidfMu_ {
11881188
__macro_local_7isValidfMu_(isValid)
11891189
}
11901190
1191-
struct __macro_local_7isValidfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
1191+
nonisolated struct __macro_local_7isValidfMu_: StructuredQueriesSQLiteCore.ScalarDatabaseFunction {
11921192
public typealias Input = (Reminder, Bool)
11931193
public typealias Output = Bool
11941194
public let name = "isValid"

0 commit comments

Comments
 (0)