Documentation

This commit is contained in:
Gwendal Roué committed 2021-04-18 16:31:44 +02:00
1 parent 6aa6c4d4b5
commit 8f7feda27c
1 file changed
+13 -2
+13 -2
View File
@@ -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].