From 8f7feda27ce123ad166f98bd3530c03c2acf1605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 18 Apr 2021 16:31:44 +0200 Subject: [PATCH] Documentation --- Documentation/QueryInterfaceOrganization.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Documentation/QueryInterfaceOrganization.md b/Documentation/QueryInterfaceOrganization.md index 5d538637c..aa2e0842c 100644 --- a/Documentation/QueryInterfaceOrganization.md +++ b/Documentation/QueryInterfaceOrganization.md @@ -335,17 +335,28 @@ let expression = length(Column("name")) > 0 // SQLExpression Player.filter(expression) ``` -When it looks like GRDB APIs are unable to build a particular expression, fallback to raw [SQL]: +When it looks like GRDB APIs are unable to build a particular expression, use [SQL]: ```swift func date(_ value: SQLSpecificExpressible) -> SQLExpression { SQL("DATE(\(value))").sqlExpression } -// SELECT * FROM "player" WHERE DATE("createdAt") = '2020-01-23' +// SELECT * FROM player WHERE DATE(createdAt) = '2020-01-23' let request = Player.filter(date(Column("createdAt")) == "2020-01-23") ``` +This technique, based on [SQL Interpolation], is composable and works well even when several tables are involved. See how the `createdAt` column below is correctly attributed to the `player` table: + +```swift +// SELECT player.*, team.* FROM player +// JOIN team ON team.id = player.teamId +// WHERE DATE(player.createdAt) = '2020-01-23' +let request = Player + .filter(date(Column("createdAt")) == "2020-01-23") + .including(required: Player.team) +``` + ### SQLExpressible `SQLExpressible` is the protocol for all [SQLite expressions](https://sqlite.org/syntax/expr.html). It is adopted by [Column], [SQL], [SQLExpression], and also [Int, String, Date], etc. It has an `sqlExpression` property which returns an [SQLExpression].