diff --git a/CHANGELOG.md b/CHANGELOG.md index c23af3cc..5a11ea00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] ======================================== @@ -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 diff --git a/Documentation/Index.md b/Documentation/Index.md index 050db1a4..3ab27a29 100644 --- a/Documentation/Index.md +++ b/Documentation/Index.md @@ -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 diff --git a/Sources/SQLite/Typed/Query.swift b/Sources/SQLite/Typed/Query.swift index 288768c1..c9d2ea9c 100644 --- a/Sources/SQLite/Typed/Query.swift +++ b/Sources/SQLite/Typed/Query.swift @@ -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) -> Self { + return `where`(Expression(predicate)) + } + + /// Adds a condition to the query’s `WHERE` clause. + /// This is an alias for `filter(predicate)` + public func `where`(_ predicate: Expression) -> Self { + return filter(predicate) + } + // MARK: GROUP BY /// Sets a `GROUP BY` clause on the query. diff --git a/Tests/SQLiteTests/QueryTests.swift b/Tests/SQLiteTests/QueryTests.swift index ed34921e..2cf164c6 100644 --- a/Tests/SQLiteTests/QueryTests.swift +++ b/Tests/SQLiteTests/QueryTests.swift @@ -8,6 +8,7 @@ class QueryTests : XCTestCase { let email = Expression("email") let age = Expression("age") let admin = Expression("admin") + let optionalAdmin = Expression("admin") let posts = Table("posts") let userId = Expression("user_id") @@ -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\")",