Skip to content

Allow where as alias for filter #572

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

Merged
merged 1 commit into from
Dec 21, 2016
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Fixed SQLCipher integration with read-only databases ([#559][])
* Preliminary Swift Package Manager support ([#548][], [#560][])
* Fixed null pointer when fetching an empty BLOB ([#561][])
* Allow `where` as alias for `filter` ([#571][])

0.11.1 (06-12-2016), [diff][diff-0.11.1]
========================================
Expand All @@ -30,3 +31,4 @@
[#559]: https://github.com/stephencelis/SQLite.swift/pull/559
[#560]: https://github.com/stephencelis/SQLite.swift/pull/560
[#561]: https://github.com/stephencelis/SQLite.swift/issues/561
[#571]: https://github.com/stephencelis/SQLite.swift/issues/571
6 changes: 5 additions & 1 deletion Documentation/Index.md
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,12 @@ users.filter(verified || balance >= 10_000)

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

> _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).
Instead of `filter` we can also use the `where` function which is an alias:

``` swift
users.where(id == 1)
// SELECT * FROM "users" WHERE ("id" = 1)
```

##### Filter Operators and Functions

Expand Down
12 changes: 12 additions & 0 deletions Sources/SQLite/Typed/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,18 @@ extension QueryType {
return query
}

/// Adds a condition to the query’s `WHERE` clause.
/// This is an alias for `filter(predicate)`
public func `where`(_ predicate: Expression<Bool>) -> Self {
return `where`(Expression<Bool?>(predicate))
}

/// Adds a condition to the query’s `WHERE` clause.
/// This is an alias for `filter(predicate)`
public func `where`(_ predicate: Expression<Bool?>) -> Self {
return filter(predicate)
}

// MARK: GROUP BY

/// Sets a `GROUP BY` clause on the query.
Expand Down
29 changes: 29 additions & 0 deletions Tests/SQLiteTests/QueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class QueryTests : XCTestCase {
let email = Expression<String>("email")
let age = Expression<Int?>("age")
let admin = Expression<Bool>("admin")
let optionalAdmin = Expression<Bool?>("admin")

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

func test_filter_compilesWhereClause_false() {
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 0)", users.filter(admin == false))
}

func test_filter_compilesWhereClause_optional() {
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 1)", users.filter(optionalAdmin == true))
}

func test_filter_compilesWhereClause_optional_false() {
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 0)", users.filter(optionalAdmin == false))
}

func test_where_compilesWhereClause() {
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 1)", users.where(admin == true))
}

func test_where_compilesWhereClause_false() {
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 0)", users.where(admin == false))
}

func test_where_compilesWhereClause_optional() {
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 1)", users.where(optionalAdmin == true))
}

func test_where_compilesWhereClause_optional_false() {
AssertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 0)", users.where(optionalAdmin == false))
}

func test_filter_whenChained_compilesAggregateWhereClause() {
AssertSQL(
"SELECT * FROM \"users\" WHERE ((\"age\" >= 35) AND \"admin\")",
Expand Down