Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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, <doc:DefiningYourSchema#Customizing-a-table>, for more information on how to customize your
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions Sources/StructuredQueriesSQLiteMacros/Nonisolated.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import SwiftSyntax

#if compiler(>=6.1)
let nonisolated: TokenSyntax? = .keyword(.nonisolated, trailingTrivia: .space)
#else
let nonisolated: TokenSyntax? = nil
#endif
84 changes: 42 additions & 42 deletions Tests/StructuredQueriesMacrosTests/DatabaseFunctionMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down