Skip to content

Commit bd80bb2

Browse files
committed
Allow where as alias for filter
1 parent 871e803 commit bd80bb2

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Fixed SQLCipher integration with read-only databases ([#559][])
55
* Preliminary Swift Package Manager support ([#548][], [#560][])
66
* Fixed null pointer when fetching an empty BLOB ([#561][])
7+
* Allow `where` as alias for `filter` ([#571][])
78

89
0.11.1 (06-12-2016), [diff][diff-0.11.1]
910
========================================
@@ -30,3 +31,4 @@
3031
[#559]: https://github.com/stephencelis/SQLite.swift/pull/559
3132
[#560]: https://github.com/stephencelis/SQLite.swift/pull/560
3233
[#561]: https://github.com/stephencelis/SQLite.swift/issues/561
34+
[#571]: https://github.com/stephencelis/SQLite.swift/issues/571

Documentation/Index.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,12 @@ users.filter(verified || balance >= 10_000)
755755

756756
We can build our own boolean expressions by using one of the many [filter operators and functions](#filter-operators-and-functions).
757757

758-
> _Note:_ SQLite.swift defines `filter` instead of `where` because `where` is [a reserved keyword](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-ID413).
758+
Instead of `filter` we can also use the `where` function which is an alias:
759759

760+
``` swift
761+
users.where(id == 1)
762+
// SELECT * FROM "users" WHERE ("id" = 1)
763+
```
760764

761765
##### Filter Operators and Functions
762766

Sources/SQLite/Typed/Query.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,18 @@ extension QueryType {
306306
return query
307307
}
308308

309+
/// Adds a condition to the query’s `WHERE` clause.
310+
/// This is an alias for `filter(predicate)`
311+
public func `where`(_ predicate: Expression<Bool>) -> Self {
312+
return `where`(Expression<Bool?>(predicate))
313+
}
314+
315+
/// Adds a condition to the query’s `WHERE` clause.
316+
/// This is an alias for `filter(predicate)`
317+
public func `where`(_ predicate: Expression<Bool?>) -> Self {
318+
return filter(predicate)
319+
}
320+
309321
// MARK: GROUP BY
310322

311323
/// Sets a `GROUP BY` clause on the query.

Tests/SQLiteTests/QueryTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class QueryTests : XCTestCase {
88
let email = Expression<String>("email")
99
let age = Expression<Int?>("age")
1010
let admin = Expression<Bool>("admin")
11+
let optionalAdmin = Expression<Bool?>("admin")
1112

1213
let posts = Table("posts")
1314
let userId = Expression<Int64>("user_id")
@@ -88,6 +89,34 @@ class QueryTests : XCTestCase {
8889
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 1)", users.filter(admin == true))
8990
}
9091

92+
func test_filter_compilesWhereClause_false() {
93+
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 0)", users.filter(admin == false))
94+
}
95+
96+
func test_filter_compilesWhereClause_optional() {
97+
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 1)", users.filter(optionalAdmin == true))
98+
}
99+
100+
func test_filter_compilesWhereClause_optional_false() {
101+
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 0)", users.filter(optionalAdmin == false))
102+
}
103+
104+
func test_where_compilesWhereClause() {
105+
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 1)", users.where(admin == true))
106+
}
107+
108+
func test_where_compilesWhereClause_false() {
109+
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 0)", users.where(admin == false))
110+
}
111+
112+
func test_where_compilesWhereClause_optional() {
113+
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 1)", users.where(optionalAdmin == true))
114+
}
115+
116+
func test_where_compilesWhereClause_optional_false() {
117+
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 0)", users.where(optionalAdmin == false))
118+
}
119+
91120
func test_filter_whenChained_compilesAggregateWhereClause() {
92121
AssertSQL(
93122
"SELECT * FROM \"users\" WHERE ((\"age\" >= 35) AND \"admin\")",

0 commit comments

Comments
 (0)