-
-
Notifications
You must be signed in to change notification settings - Fork 727
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #365 from groue/GRDB3-DelayedOrdering
Delayed Request Ordering
- Loading branch information
Showing
20 changed files
with
300 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/// QueryOrdering provides the order clause to QueryInterfaceQuery | ||
/// and AssociationQuery. | ||
struct QueryOrdering { | ||
private var elements: [Element] = [] | ||
var isReversed: Bool | ||
|
||
private enum Element { | ||
case orderingTerms((Database) throws -> [SQLOrderingTerm]) | ||
case queryOrdering(QueryOrdering) | ||
|
||
var reversed: Element { | ||
switch self { | ||
case .orderingTerms(let orderings): | ||
return .orderingTerms { db in try orderings(db).map { $0.reversed } } | ||
case .queryOrdering(let queryOrdering): | ||
return .queryOrdering(queryOrdering.reversed) | ||
} | ||
} | ||
|
||
func qualified(with alias: TableAlias) -> Element { | ||
switch self { | ||
case .orderingTerms(let orderings): | ||
return .orderingTerms { db in try orderings(db).map { $0.qualifiedOrdering(with: alias) } } | ||
case .queryOrdering(let queryOrdering): | ||
return .queryOrdering(queryOrdering.qualified(with: alias)) | ||
} | ||
} | ||
|
||
func resolve(_ db: Database) throws -> [SQLOrderingTerm] { | ||
switch self { | ||
case .orderingTerms(let orderings): | ||
return try orderings(db) | ||
case .queryOrdering(let queryOrdering): | ||
return try queryOrdering.resolve(db) | ||
} | ||
} | ||
} | ||
|
||
private init(elements: [Element], isReversed: Bool) { | ||
self.elements = elements | ||
self.isReversed = isReversed | ||
} | ||
|
||
init() { | ||
self.init( | ||
elements: [], | ||
isReversed: false) | ||
} | ||
|
||
init(orderings: @escaping (Database) throws -> [SQLOrderingTerm]) { | ||
self.init( | ||
elements: [.orderingTerms(orderings)], | ||
isReversed: false) | ||
} | ||
|
||
var reversed: QueryOrdering { | ||
return QueryOrdering( | ||
elements: elements, | ||
isReversed: !isReversed) | ||
} | ||
|
||
func qualified(with alias: TableAlias) -> QueryOrdering { | ||
return QueryOrdering( | ||
elements: elements.map { $0.qualified(with: alias) }, | ||
isReversed: isReversed) | ||
} | ||
|
||
func appending(_ ordering: QueryOrdering) -> QueryOrdering { | ||
return QueryOrdering( | ||
elements: elements + [.queryOrdering(ordering)], | ||
isReversed: isReversed) | ||
} | ||
|
||
func resolve(_ db: Database) throws -> [SQLOrderingTerm] { | ||
if isReversed { | ||
return try elements.flatMap { try $0.reversed.resolve(db) } | ||
} else { | ||
return try elements.flatMap { try $0.resolve(db) } | ||
} | ||
} | ||
} |
Oops, something went wrong.