diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index 3c0063b232..e0dfb1ffdf 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -189,7 +189,7 @@ extension QueryInterfaceRequest { @warn_unused_result public func order(orderings: [_SQLOrdering]) -> QueryInterfaceRequest { var query = self.query - query.orderings.appendContentsOf(orderings) + query.orderings = orderings return QueryInterfaceRequest(query: query) } diff --git a/README.md b/README.md index e92eecc823..50c6560a10 100644 --- a/README.md +++ b/README.md @@ -2182,6 +2182,13 @@ All the methods above return another request, which you can further refine by ap // SELECT * FROM "persons" ORDER BY "score" DESC, "name" Person.order(scoreColumn.desc, nameColumn) ``` + + Each `order` call clears any previous ordering: + + ```swift + // SELECT * FROM "persons" ORDER BY "name" + Person.order(scoreColumn).order(nameColumn) + ``` - `reverse()` reverses the eventual orderings. @@ -2214,13 +2221,14 @@ You can refine requests by chaining those methods: Person.order(nameColumn).filter(emailColumn != nil) ``` -The `select`, `group` and `limit` methods ignore and replace previously applied selection, grouping and limits. On the opposite, `filter`, `having`, and `order` methods extend the query: +The `select`, `order`, `group`, and `limit` methods ignore and replace previously applied selection, orderings, grouping, and limits. On the opposite, `filter`, and `having` methods extend the query: ```swift Person // SELECT * FROM "persons" .filter(nameColumn != nil) // WHERE (("name" IS NOT NULL) .filter(emailColumn != nil) // AND ("email IS NOT NULL")) - .order(nameColumn) // ORDER BY "name" + .order(nameColumn) // - ignored - + .order(ageColumn) // ORDER BY "age" .limit(20, offset: 40) // - ignored - .limit(10) // LIMIT 10 ``` diff --git a/Tests/Public/QueryInterfaceRequest/QueryInterfaceRequestTests.swift b/Tests/Public/QueryInterfaceRequest/QueryInterfaceRequestTests.swift index fee7aa5557..2b80aa505a 100644 --- a/Tests/Public/QueryInterfaceRequest/QueryInterfaceRequestTests.swift +++ b/Tests/Public/QueryInterfaceRequest/QueryInterfaceRequestTests.swift @@ -415,7 +415,7 @@ class QueryInterfaceRequestTests: GRDBTestCase { let dbQueue = try! makeDatabaseQueue() XCTAssertEqual( sql(dbQueue, tableRequest.order(Col.age).order(Col.name)), - "SELECT * FROM \"readers\" ORDER BY \"age\", \"name\"") + "SELECT * FROM \"readers\" ORDER BY \"name\"") } @@ -462,7 +462,7 @@ class QueryInterfaceRequestTests: GRDBTestCase { sql(dbQueue, tableRequest.reverse().reverse()), "SELECT * FROM \"readers\"") XCTAssertEqual( - sql(dbQueue, tableRequest.order(Col.age).order(Col.name).reverse().reverse()), + sql(dbQueue, tableRequest.order(Col.age, Col.name).reverse().reverse()), "SELECT * FROM \"readers\" ORDER BY \"age\", \"name\"") } diff --git a/Tests/Public/QueryInterfaceRequest/TableMapping+QueryInterfaceRequestTests.swift b/Tests/Public/QueryInterfaceRequest/TableMapping+QueryInterfaceRequestTests.swift index c86383e673..1adc13dfe6 100644 --- a/Tests/Public/QueryInterfaceRequest/TableMapping+QueryInterfaceRequestTests.swift +++ b/Tests/Public/QueryInterfaceRequest/TableMapping+QueryInterfaceRequestTests.swift @@ -254,7 +254,7 @@ class TableMappingQueryInterfaceRequestTests: GRDBTestCase { let dbQueue = try! makeDatabaseQueue() XCTAssertEqual( sql(dbQueue, Reader.order(Col.age).order(Col.name)), - "SELECT * FROM \"readers\" ORDER BY \"age\", \"name\"") + "SELECT * FROM \"readers\" ORDER BY \"name\"") }