Skip to content

Commit

Permalink
The query interface order method replaces any previously applied or…
Browse files Browse the repository at this point in the history
…dering (see #85)
  • Loading branch information
groue committed Jul 19, 2016
1 parent 09f57d3 commit 0677a8a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion GRDB/QueryInterface/QueryInterfaceRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ extension QueryInterfaceRequest {
@warn_unused_result
public func order(orderings: [_SQLOrdering]) -> QueryInterfaceRequest<T> {
var query = self.query
query.orderings.appendContentsOf(orderings)
query.orderings = orderings
return QueryInterfaceRequest(query: query)
}

Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\"")
}


Expand Down Expand Up @@ -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\"")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\"")
}


Expand Down

0 comments on commit 0677a8a

Please sign in to comment.