Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance SQLLiteral and SQL interpolation again #706

Merged
merged 6 commits into from
Feb 26, 2020
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
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ All notable changes to this project will be documented in this file.
GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: APIs flagged [**:fire: EXPERIMENTAL**](README.md#what-are-experimental-features). Those are unstable, and may break between any two minor releases of the library.


<!--
[Next Release](#next-release)
-->


#### 4.x Releases
Expand Down Expand Up @@ -63,9 +61,11 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception:
- [0.110.0](#01100), ...


<!--
## Next Release
-->

**New**

- [#706](https://github.com/groue/GRDB.swift/pull/706): Enhance SQLLiteral and SQL interpolation again


## 4.10.0
Expand Down
2 changes: 1 addition & 1 deletion Documentation/SQLInterpolation.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Such literal expressions can be returned by Swift functions:

```swift
func date(_ value: SQLExpressible) -> SQLExpression {
SQLLiteral("DATE(\(lhs.sqlExpression))").sqlExpression
SQLLiteral("DATE(\(value))").sqlExpression
}

// SELECT * FROM "player" WHERE DATE("createdAt") = '2020-01-23'
Expand Down
11 changes: 10 additions & 1 deletion GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ extension SQLInterpolation {
/// let request: SQLRequest<Int> = """
/// SELECT score + \(bonus) FROM player
/// """
public mutating func appendInterpolation<T: SQLExpressible>(_ expressible: T?) {
///
/// You can also derive literal expressions from other expressions:
///
/// func date(_ value: SQLExpressible) -> SQLExpression {
/// SQLLiteral("DATE(\(value))").sqlExpression
/// }
///
/// // SELECT * FROM player WHERE DATE(createdAt) = '2020-02-25'
/// let request = Player.filter(date(Column("createdAt")) == "2020-02-25")
public mutating func appendInterpolation(_ expressible: SQLExpressible?) {
if let expressible = expressible {
elements.append(.expression(expressible.sqlExpression))
} else {
Expand Down
27 changes: 27 additions & 0 deletions Tests/GRDBTests/SQLLiteralTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,33 @@ extension SQLLiteralTests {
SELECT * FROM "player" WHERE (DATE("createdAt")) = '2020-01-23'
""")
}

do {
// Here we test that users can define functions that return
// literal expressions.
func date(_ value: SQLExpressible) -> SQLExpression {
return SQLLiteral("DATE(\(value))").sqlExpression
}
let createdAt = Column("createdAt")
let request = Player.filter(date(createdAt) == "2020-01-23")
try assertEqualSQL(db, request, """
SELECT * FROM "player" WHERE (DATE("createdAt")) = '2020-01-23'
""")
}

do {
// Here we test that users can still define functions that
// return literal expressions with the previously
// supported technique.
func date(_ value: SQLExpressible) -> SQLExpression {
return SQLLiteral("DATE(\(value.sqlExpression))").sqlExpression
}
let createdAt = Column("createdAt")
let request = Player.filter(date(createdAt) == "2020-01-23")
try assertEqualSQL(db, request, """
SELECT * FROM "player" WHERE (DATE("createdAt")) = '2020-01-23'
""")
}
}
}
}
Expand Down