mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Support for NULLS FIRST and NULLS LAST
This commit is contained in:
1 parent
2c9bef6440
commit
7208f53176
6 files changed
+126
-15
No files matched your search
@@ -40,6 +40,32 @@ public struct SQLCollatedExpression {
|
||||
return SQLOrdering.desc(sqlExpression)
|
||||
}
|
||||
|
||||
#if GRDBCUSTOMSQLITE
|
||||
/// Returns an ordering suitable for QueryInterfaceRequest.order()
|
||||
///
|
||||
/// let email: SQLCollatedExpression = Column("email").collating(.nocase)
|
||||
///
|
||||
/// // SELECT * FROM player ORDER BY email COLLATE NOCASE ASC NULLS LAST
|
||||
/// Player.order(email.ascNullsLast)
|
||||
///
|
||||
/// See https://github.com/groue/GRDB.swift/#the-query-interface
|
||||
public var ascNullsLast: SQLOrderingTerm {
|
||||
return SQLOrdering.ascNullsLast(sqlExpression)
|
||||
}
|
||||
|
||||
/// Returns an ordering suitable for QueryInterfaceRequest.order()
|
||||
///
|
||||
/// let email: SQLCollatedExpression = Column("email").collating(.nocase)
|
||||
///
|
||||
/// // SELECT * FROM player ORDER BY email COLLATE NOCASE DESC NULLS FIRST
|
||||
/// Player.order(email.descNullsFirst)
|
||||
///
|
||||
/// See https://github.com/groue/GRDB.swift/#the-query-interface
|
||||
public var descNullsFirst: SQLOrderingTerm {
|
||||
return SQLOrdering.descNullsFirst(sqlExpression)
|
||||
}
|
||||
#endif
|
||||
|
||||
init(_ expression: SQLExpression, collationName: Database.CollationName) {
|
||||
self.expression = expression
|
||||
self.collationName = collationName
|
||||
|
||||
@@ -26,6 +26,10 @@ public protocol SQLOrderingTerm {
|
||||
enum SQLOrdering: SQLOrderingTerm {
|
||||
case asc(SQLExpression)
|
||||
case desc(SQLExpression)
|
||||
#if GRDBCUSTOMSQLITE
|
||||
case ascNullsLast(SQLExpression)
|
||||
case descNullsFirst(SQLExpression)
|
||||
#endif
|
||||
|
||||
var reversed: SQLOrderingTerm {
|
||||
switch self {
|
||||
@@ -33,6 +37,12 @@ enum SQLOrdering: SQLOrderingTerm {
|
||||
return SQLOrdering.desc(expression)
|
||||
case .desc(let expression):
|
||||
return SQLOrdering.asc(expression)
|
||||
#if GRDBCUSTOMSQLITE
|
||||
case .ascNullsLast(let expression):
|
||||
return SQLOrdering.descNullsFirst(expression)
|
||||
case .descNullsFirst(let expression):
|
||||
return SQLOrdering.ascNullsLast(expression)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +52,12 @@ enum SQLOrdering: SQLOrderingTerm {
|
||||
return expression.expressionSQL(&context, wrappedInParenthesis: false) + " ASC"
|
||||
case .desc(let expression):
|
||||
return expression.expressionSQL(&context, wrappedInParenthesis: false) + " DESC"
|
||||
#if GRDBCUSTOMSQLITE
|
||||
case .ascNullsLast(let expression):
|
||||
return expression.expressionSQL(&context, wrappedInParenthesis: false) + " ASC NULLS LAST"
|
||||
case .descNullsFirst(let expression):
|
||||
return expression.expressionSQL(&context, wrappedInParenthesis: false) + " DESC NULLS FIRST"
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +67,12 @@ enum SQLOrdering: SQLOrderingTerm {
|
||||
return SQLOrdering.asc(expression.qualifiedExpression(with: alias))
|
||||
case .desc(let expression):
|
||||
return SQLOrdering.desc(expression.qualifiedExpression(with: alias))
|
||||
#if GRDBCUSTOMSQLITE
|
||||
case .ascNullsLast(let expression):
|
||||
return SQLOrdering.ascNullsLast(expression.qualifiedExpression(with: alias))
|
||||
case .descNullsFirst(let expression):
|
||||
return SQLOrdering.descNullsFirst(expression.qualifiedExpression(with: alias))
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,22 @@ extension SQLSpecificExpressible {
|
||||
public var desc: SQLOrderingTerm {
|
||||
return SQLOrdering.desc(sqlExpression)
|
||||
}
|
||||
|
||||
#if GRDBCUSTOMSQLITE
|
||||
/// Returns a value that can be used as an argument to QueryInterfaceRequest.order()
|
||||
///
|
||||
/// See https://github.com/groue/GRDB.swift/#the-query-interface
|
||||
public var ascNullsLast: SQLOrderingTerm {
|
||||
return SQLOrdering.ascNullsLast(sqlExpression)
|
||||
}
|
||||
|
||||
/// Returns a value that can be used as an argument to QueryInterfaceRequest.order()
|
||||
///
|
||||
/// See https://github.com/groue/GRDB.swift/#the-query-interface
|
||||
public var descNullsFirst: SQLOrderingTerm {
|
||||
return SQLOrdering.descNullsFirst(sqlExpression)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4083,6 +4083,13 @@ You can now build requests with the following methods: `all`, `none`, `select`,
|
||||
Player.order(scoreColumn.desc, nameColumn)
|
||||
```
|
||||
|
||||
SQLite considers NULL values to be smaller than any other values for sorting purposes. Hence, NULLs naturally appear at the beginning of an ascending ordering and at the end of a descending ordering. With a [custom SQLite build], this can be changed using `.ascNullsLast` and `.descNullsFirst`:
|
||||
|
||||
```swift
|
||||
// SELECT * FROM player ORDER BY score ASC NULLS LAST
|
||||
Player.order(nameColumn.ascNullsLast)
|
||||
```
|
||||
|
||||
Each `order` call clears any previous ordering:
|
||||
|
||||
```swift
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import XCTest
|
||||
#if GRDBCUSTOMSQLITE
|
||||
import GRDBCustomSQLite
|
||||
import GRDBCustomSQLite
|
||||
#else
|
||||
import GRDB
|
||||
import GRDB
|
||||
#endif
|
||||
|
||||
private struct Col {
|
||||
@@ -18,7 +18,7 @@ private struct Reader : TableRecord {
|
||||
private let tableRequest = Reader.all()
|
||||
|
||||
class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
|
||||
|
||||
var collation: DatabaseCollation!
|
||||
|
||||
override func setup(_ dbWriter: DatabaseWriter) throws {
|
||||
@@ -88,10 +88,10 @@ class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// MARK: - Count
|
||||
|
||||
|
||||
func testFetchCount() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -135,10 +135,10 @@ class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
XCTAssertEqual(lastSQLQuery, "SELECT COUNT(*) FROM (SELECT MAX(\"age\") FROM \"readers\" GROUP BY \"name\")")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// MARK: - Select
|
||||
|
||||
|
||||
func testSelectLiteral() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -155,7 +155,7 @@ class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
XCTAssertEqual(rows[1][1] as Int64, 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testSelectLiteralWithPositionalArguments() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -172,7 +172,7 @@ class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
XCTAssertEqual(rows[1][1] as Int64, 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testSelectLiteralWithNamedArguments() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -228,7 +228,7 @@ class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
XCTAssertEqual(rows[1][1] as Int64, 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testSelectionCustomKey() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -526,14 +526,14 @@ class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
.filter(sql: "name = :name", arguments: ["name": "arthur"])),
|
||||
"SELECT * FROM \"readers\" WHERE (age > 20) AND (name = 'arthur')")
|
||||
}
|
||||
|
||||
|
||||
func testFilter() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, tableRequest.filter(true)),
|
||||
"SELECT * FROM \"readers\" WHERE 1")
|
||||
}
|
||||
|
||||
|
||||
func testMultipleFilter() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
XCTAssertEqual(
|
||||
@@ -617,7 +617,7 @@ class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, tableRequest.group(Col.name).having(min(Col.age) > 18).having(max(Col.age) < 50)),
|
||||
"SELECT * FROM \"readers\" GROUP BY \"name\" HAVING (MIN(\"age\") > 18) AND (MAX(\"age\") < 50)")
|
||||
"SELECT * FROM \"readers\" GROUP BY \"name\" HAVING (MIN(\"age\") > 18) AND (MAX(\"age\") < 50)")
|
||||
}
|
||||
|
||||
|
||||
@@ -661,6 +661,14 @@ class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, tableRequest.order(abs(Col.age))),
|
||||
"SELECT * FROM \"readers\" ORDER BY ABS(\"age\")")
|
||||
#if GRDBCUSTOMSQLITE
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, tableRequest.order(Col.age.ascNullsLast)),
|
||||
"SELECT * FROM \"readers\" ORDER BY \"age\" ASC NULLS LAST")
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, tableRequest.order(Col.age.descNullsFirst)),
|
||||
"SELECT * FROM \"readers\" ORDER BY \"age\" DESC NULLS FIRST")
|
||||
#endif
|
||||
}
|
||||
|
||||
func testSortWithCollation() throws {
|
||||
@@ -674,6 +682,14 @@ class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, tableRequest.order(Col.name.collating(collation))),
|
||||
"SELECT * FROM \"readers\" ORDER BY \"name\" COLLATE localized_case_insensitive")
|
||||
#if GRDBCUSTOMSQLITE
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, tableRequest.order(Col.name.collating(.nocase).ascNullsLast)),
|
||||
"SELECT * FROM \"readers\" ORDER BY \"name\" COLLATE NOCASE ASC NULLS LAST")
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, tableRequest.order(Col.name.collating(.nocase).descNullsFirst)),
|
||||
"SELECT * FROM \"readers\" ORDER BY \"name\" COLLATE NOCASE DESC NULLS FIRST")
|
||||
#endif
|
||||
}
|
||||
|
||||
func testMultipleSort() throws {
|
||||
@@ -706,6 +722,14 @@ class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, tableRequest.order(abs(Col.age)).reversed()),
|
||||
"SELECT * FROM \"readers\" ORDER BY ABS(\"age\") DESC")
|
||||
#if GRDBCUSTOMSQLITE
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, tableRequest.order(Col.age.descNullsFirst).reversed()),
|
||||
"SELECT * FROM \"readers\" ORDER BY \"age\" ASC NULLS LAST")
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, tableRequest.order(Col.age.ascNullsLast).reversed()),
|
||||
"SELECT * FROM \"readers\" ORDER BY \"age\" DESC NULLS FIRST")
|
||||
#endif
|
||||
}
|
||||
|
||||
func testReverseWithCollation() throws {
|
||||
@@ -719,6 +743,14 @@ class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, tableRequest.order(Col.name.collating(collation)).reversed()),
|
||||
"SELECT * FROM \"readers\" ORDER BY \"name\" COLLATE localized_case_insensitive DESC")
|
||||
#if GRDBCUSTOMSQLITE
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, tableRequest.order(Col.name.collating(.nocase).ascNullsLast).reversed()),
|
||||
"SELECT * FROM \"readers\" ORDER BY \"name\" COLLATE NOCASE DESC NULLS FIRST")
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, tableRequest.order(Col.name.collating(.nocase).descNullsFirst).reversed()),
|
||||
"SELECT * FROM \"readers\" ORDER BY \"name\" COLLATE NOCASE ASC NULLS LAST")
|
||||
#endif
|
||||
}
|
||||
|
||||
func testMultipleReverse() throws {
|
||||
|
||||
@@ -258,6 +258,14 @@ class TableRecordQueryInterfaceRequestTests: GRDBTestCase {
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, Reader.order(abs(Col.age))),
|
||||
"SELECT * FROM \"readers\" ORDER BY ABS(\"age\")")
|
||||
#if GRDBCUSTOMSQLITE
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, Reader.order(Col.age.ascNullsLast)),
|
||||
"SELECT * FROM \"readers\" ORDER BY \"age\" ASC NULLS LAST")
|
||||
XCTAssertEqual(
|
||||
sql(dbQueue, Reader.order(Col.age.descNullsFirst)),
|
||||
"SELECT * FROM \"readers\" ORDER BY \"age\" DESC NULLS FIRST")
|
||||
#endif
|
||||
}
|
||||
|
||||
func testMultipleSort() throws {
|
||||
|
||||
Reference in new issue
Block a user