Skip to content

Conversation

@stephencelis
Copy link
Member

@stephencelis stephencelis commented Aug 30, 2025

This PR introduces the ability to create custom functions for SQLite. You can apply a new @DatabaseFunction macro to a Swift function and a type-safe query helper is generated alongside it.

You could control a date dependency, for example:

@DatabaseFunction
func customDate() -> Date {
  @Dependency(\.date.now) var now
  return now
}

// In a query:
Reminder.where { $0.dueDate <= $customDate() }
// SELECT … FROM "reminders" WHERE "dueDate" <= "customDate"()

// Theoretical syntax for adapters like SharingGRDB:
db.install($customDate)

This functionality is implemented in a new StructuredQueriesSQLite module, which we can use to quarantine SQLite-specific functionality. We should also move the FTS stuff there soon.

@stephencelis stephencelis requested a review from mbrandonw August 30, 2025 00:03
context.diagnose(
Diagnostic(
node: ellipsis,
message: MacroExpansionErrorMessage("Variadic arguments are not supported")
Copy link
Member Author

@stephencelis stephencelis Aug 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we theoretically could support variadics (and maybe even parameter packs) with a bit more work, but maybe we wait for some good use cases.

stephencelis added a commit to pointfreeco/sqlite-data that referenced this pull request Sep 1, 2025
@stephencelis stephencelis marked this pull request as ready for review September 1, 2025 17:43
@stephencelis stephencelis merged commit c078249 into main Sep 1, 2025
3 checks passed
@stephencelis stephencelis deleted the custom-functions branch September 1, 2025 20:28
stephencelis added a commit to pointfreeco/sqlite-data that referenced this pull request Sep 1, 2025
* Support for StructuredQueries' `@DatabaseFunction` macro

See pointfreeco/swift-structured-queries#151 for
more details.

* wip

* wip

* test

* wip

* wip
coenttb pushed a commit to coenttb/swift-structured-queries-postgres that referenced this pull request Oct 14, 2025
* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip
coenttb pushed a commit to coenttb/swift-structured-queries-postgres that referenced this pull request Oct 15, 2025
* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants