Extract joining methods to the JoinableRequest protocol

This commit is contained in:
Gwendal Roué committed 2019-05-29 08:36:55 +02:00
1 parent 1b5e94a050
commit e7a590b82b
7 files changed
+285 -219

No files matched your search

+59 -29
View File
@@ -29,13 +29,12 @@ struct SQLQuery {
}
}
extension SQLQuery: SelectionRequest, FilteredRequest, OrderedRequest {
func select(_ selection: [SQLSelectable]) -> SQLQuery {
return mapRelation { $0.select(selection) }
}
func annotated(with selection: [SQLSelectable]) -> SQLQuery {
return mapRelation { $0.annotated(with: selection) }
extension SQLQuery {
/// Returns a query whose relation is transformed by the given closure.
func mapRelation(_ transform: (SQLRelation) -> SQLRelation) -> SQLQuery {
var query = self
query.relation = transform(relation)
return query
}
func distinct() -> SQLQuery {
@@ -43,29 +42,41 @@ extension SQLQuery: SelectionRequest, FilteredRequest, OrderedRequest {
query.isDistinct = true
return query
}
func expectingSingleResult() -> SQLQuery {
var query = self
query.expectsSingleResult = true
return query
}
func limit(_ limit: Int, offset: Int? = nil) -> SQLQuery {
var query = self
query.limit = SQLLimit(limit: limit, offset: offset)
return query
}
func qualified(with alias: TableAlias) -> SQLQuery {
return mapRelation { $0.qualified(with: alias) }
}
}
extension SQLQuery: SelectionRequest {
func select(_ selection: [SQLSelectable]) -> SQLQuery {
return mapRelation { $0.select(selection) }
}
func annotated(with selection: [SQLSelectable]) -> SQLQuery {
return mapRelation { $0.annotated(with: selection) }
}
}
extension SQLQuery: FilteredRequest {
func filter(_ predicate: @escaping (Database) throws -> SQLExpressible) -> SQLQuery {
return mapRelation { $0.filter(predicate) }
}
func group(_ expressions: @escaping (Database) throws -> [SQLExpressible]) -> SQLQuery {
var query = self
query.groupPromise = DatabasePromise { db in try expressions(db).map { $0.sqlExpression } }
return query
}
func having(_ predicate: SQLExpressible) -> SQLQuery {
var query = self
query.havingExpressions.append(predicate.sqlExpression)
return query
}
}
extension SQLQuery: OrderedRequest {
func order(_ orderings: @escaping (Database) throws -> [SQLOrderingTerm]) -> SQLQuery {
return mapRelation { $0.order(orderings) }
}
@@ -77,22 +88,41 @@ extension SQLQuery: SelectionRequest, FilteredRequest, OrderedRequest {
func unordered() -> SQLQuery {
return mapRelation { $0.unordered() }
}
}
func limit(_ limit: Int, offset: Int? = nil) -> SQLQuery {
extension SQLQuery: AggregatingRequest {
func group(_ expressions: @escaping (Database) throws -> [SQLExpressible]) -> SQLQuery {
var query = self
query.limit = SQLLimit(limit: limit, offset: offset)
query.groupPromise = DatabasePromise { db in try expressions(db).map { $0.sqlExpression } }
return query
}
func qualified(with alias: TableAlias) -> SQLQuery {
return mapRelation { $0.qualified(with: alias) }
func having(_ predicate: SQLExpressible) -> SQLQuery {
var query = self
query.havingExpressions.append(predicate.sqlExpression)
return query
}
}
extension SQLQuery: _JoinableRequest {
func _including(all association: SQLAssociation) -> SQLQuery {
return mapRelation { $0._including(all: association) }
}
/// Returns a query whose relation is transformed by the given closure.
func mapRelation(_ transform: (SQLRelation) -> SQLRelation) -> SQLQuery {
var query = self
query.relation = transform(relation)
return query
func _including(optional association: SQLAssociation) -> SQLQuery {
return mapRelation { $0._including(optional: association) }
}
func _including(required association: SQLAssociation) -> SQLQuery {
return mapRelation { $0._including(required: association) }
}
func _joining(optional association: SQLAssociation) -> SQLQuery {
return mapRelation { $0._joining(optional: association) }
}
func _joining(required association: SQLAssociation) -> SQLQuery {
return mapRelation { $0._joining(required: association) }
}
}