#warning("TODO: doc") public struct CommonTableExpression { var tableName: String var request: _FetchRequest } extension CommonTableExpression { var relationForAll: SQLRelation { SQLRelation( source: .table(tableName: tableName, alias: nil), selectionPromise: DatabasePromise(value: [_AllCTEColumns(request: request, alias: nil)])) } #warning("TODO: doc") public func all() -> QueryInterfaceRequest { QueryInterfaceRequest(relation: relationForAll) } } extension CommonTableExpression { #warning("TODO: Do we need to be able to provide a foreign key? A USING clause?") #warning("TODO: doc") public func association(from: T.Type = T.self, on condition: @escaping (TableAlias, TableAlias) -> SQLExpressible) -> JoinAssociation { JoinAssociation( key: .inflected(tableName), condition: .promise(condition), relation: relationForAll) } #warning("TODO: doc") public func association(from: T.Type = T.self, using columns: Column...) -> JoinAssociation { association(from: T.self, on: { (left, right) -> SQLExpressible in columns.map { left[$0] == right[$0] }.joined(operator: .and) }) } #warning("TODO: Do we need to be able to provide a foreign key? A USING clause?") #warning("TODO: doc") public func association( to cte: CommonTableExpression, on condition: @escaping (TableAlias, TableAlias) -> SQLExpressible) -> JoinAssociation { JoinAssociation( key: .inflected(cte.tableName), condition: .promise(condition), relation: cte.relationForAll) } #warning("TODO: Do we need to be able to provide a foreign key? A USING clause?") #warning("TODO: doc") public func association( to destination: Destination.Type, on condition: @escaping (TableAlias, TableAlias) -> SQLExpressible) -> JoinAssociation where Destination: TableRecord { JoinAssociation( key: .inflected(Destination.databaseTableName), condition: .promise(condition), relation: Destination.relationForAll) } } extension _FetchRequest { #warning("TODO: doc") public func commonTableExpression(tableName: String) -> CommonTableExpression { CommonTableExpression( tableName: tableName, request: self) } } // MARK: - QueryInterfaceRequest extension QueryInterfaceRequest { #warning("TODO: doc") public func with(_ ctes: CommonTableExpression...) -> Self { with(ctes) } #warning("TODO: doc") public func with(_ ctes: [CommonTableExpression]) -> Self { mapInto(\.query.ctes) { for cte in ctes { $0[cte.tableName] = cte.request } } } } // MARK: - _AllCTEColumns /// :nodoc: public struct _AllCTEColumns { var request: _FetchRequest var alias: TableAlias? } extension _AllCTEColumns: SQLSelectable, Refinable { /// :nodoc: public func _count(distinct: Bool) -> _SQLCount? { // SELECT DISTINCT * FROM tableName ... if distinct { // Can't count return nil } // SELECT * FROM tableName ... // -> // SELECT COUNT(*) FROM tableName ... return .all } /// :nodoc: public func _qualifiedSelectable(with alias: TableAlias) -> SQLSelectable { // Never requalify if self.alias != nil { return self } return with(\.alias, alias) } /// :nodoc: public func _columnCount(_ db: Database) throws -> Int { let context = SQLGenerationContext(db) let sql = try request.requestSQL(context, forSingleResult: false) let statement = try db.makeSelectStatement(sql: sql) return statement.columnCount } /// :nodoc: public func _accept(_ visitor: inout Visitor) throws { if let alias = alias { return try _SQLQualifiedAllColumns(alias: alias)._accept(&visitor) } else { return try AllColumns()._accept(&visitor) } } }