diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index d171342fc..d65013bfd 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -1282,7 +1282,7 @@ extension Row { private let row: Row private let scopes: [String: LayoutedRowAdapter] private let prefetchedRows: Row.PrefetchedRowsView - + /// The scopes defined on this row. public var names: Dictionary.Keys { return scopes.keys @@ -1323,7 +1323,8 @@ extension Row { // // let request = A.including(required: A.b.including(all: B.c)) // let row = try Row.fetchOne(db, request)! - // row.scopes["b"]!.prefetchedRows["cs"] + // row.prefetchedRows["cs"] // Some array + // row.scopes["b"]!.prefetchedRows["cs"] // The same array adaptedRow.prefetchedRows = Row.PrefetchedRowsView(prefetches: prefetch.prefetches) } return (name: name, row: adaptedRow) @@ -1432,7 +1433,15 @@ extension Row { return prefetches.isEmpty } - /// The prefetch keys defined on this row + /// The keys for available prefetched rows + /// + /// For example: + /// + /// let request = Author.including(all: Author.books) + /// let row = try Row.fetchOne(db, request)! + /// + /// print(row.prefetchedRows.keys) + /// // Prints ["books"] public var keys: Set { var result: Set = [] var fifo = Array(prefetches) @@ -1448,6 +1457,18 @@ extension Row { /// Returns the rows associated with the given key, by performing a /// breadth-first search in this row's prefetch tree. + /// + /// For example: + /// + /// let request = Author.including(all: Author.books) + /// let row = try Row.fetchOne(db, request)! + /// + /// print(row) + /// // Prints [id:1 name:"Herman Melville"] + /// + /// let bookRows = row.prefetchedRows["books"] + /// print(bookRows[0]) + /// // Prints [id:42 title:"Moby-Dick"] public subscript(_ key: String) -> [Row]? { var fifo = Array(prefetches) while !fifo.isEmpty { diff --git a/GRDB/QueryInterface/Association/Association.swift b/GRDB/QueryInterface/Association/Association.swift index 0087ac459..1ef246423 100644 --- a/GRDB/QueryInterface/Association/Association.swift +++ b/GRDB/QueryInterface/Association/Association.swift @@ -259,37 +259,41 @@ extension Association { } extension SQLRelation { - /// Creates an relation that prefetches another one. + /// Creates a relation that prefetches another one. func including(all sqlAssociation: SQLAssociation) -> SQLRelation { return sqlAssociation.extendedRelation(self, kind: .allPrefetched) } - /// Creates an relation that includes another one. The columns of the + /// Creates a relation that includes another one. The columns of the /// associated record are selected. The returned relation does not /// require that the associated database table contains a matching row. func including(optional sqlAssociation: SQLAssociation) -> SQLRelation { return sqlAssociation.extendedRelation(self, kind: .oneOptional) } - /// Creates an relation that includes another one. The columns of the + /// Creates a relation that includes another one. The columns of the /// associated record are selected. The returned relation requires /// that the associated database table contains a matching row. func including(required sqlAssociation: SQLAssociation) -> SQLRelation { return sqlAssociation.extendedRelation(self, kind: .oneRequired) } - /// Creates an relation that joins another one. The columns of the + /// Creates a relation that joins another one. The columns of the /// associated record are not selected. The returned relation does not /// require that the associated database table contains a matching row. func joining(optional sqlAssociation: SQLAssociation) -> SQLRelation { - return sqlAssociation.mapDestinationRelation { $0.select([]) }.extendedRelation(self, kind: .oneOptional) + return sqlAssociation + .mapDestinationRelation { $0.select([]) } + .extendedRelation(self, kind: .oneOptional) } - /// Creates an relation that joins another one. The columns of the + /// Creates a relation that joins another one. The columns of the /// associated record are not selected. The returned relation requires /// that the associated database table contains a matching row. func joining(required sqlAssociation: SQLAssociation) -> SQLRelation { - return sqlAssociation.mapDestinationRelation { $0.select([]) }.extendedRelation(self, kind: .oneRequired) + return sqlAssociation + .mapDestinationRelation { $0.select([]) } + .extendedRelation(self, kind: .oneRequired) } } diff --git a/GRDB/QueryInterface/FetchableRecord+QueryInterface.swift b/GRDB/QueryInterface/FetchableRecord+QueryInterface.swift index 3663328cc..c600e87f1 100644 --- a/GRDB/QueryInterface/FetchableRecord+QueryInterface.swift +++ b/GRDB/QueryInterface/FetchableRecord+QueryInterface.swift @@ -15,7 +15,6 @@ extension QueryInterfaceRequest where RowDecoder: FetchableRecord { /// - parameter db: A database connection. /// - returns: A cursor over fetched records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - @inlinable // TODO: should not be inlinable public func fetchCursor(_ db: Database) throws -> RecordCursor { return try RowDecoder.fetchCursor(db, self) } @@ -28,7 +27,6 @@ extension QueryInterfaceRequest where RowDecoder: FetchableRecord { /// - parameter db: A database connection. /// - returns: An array of records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - @inlinable // TODO: should not be inlinable public func fetchAll(_ db: Database) throws -> [RowDecoder] { return try RowDecoder.fetchAll(db, self) } @@ -41,7 +39,6 @@ extension QueryInterfaceRequest where RowDecoder: FetchableRecord { /// - parameter db: A database connection. /// - returns: An optional record. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - @inlinable // TODO: should not be inlinable public func fetchOne(_ db: Database) throws -> RowDecoder? { return try RowDecoder.fetchOne(db, self) } @@ -69,9 +66,8 @@ extension FetchableRecord { /// - sql: a FetchRequest. /// - returns: A cursor over fetched records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - @inlinable // TODO: should not be inlinable public static func fetchCursor(_ db: Database, _ request: QueryInterfaceRequest) throws -> RecordCursor { - precondition(request.prefetchedAssociations.isEmpty, "Not implemented: fetching cursor with prefetched associations") + precondition(request.prefetchedAssociations.isEmpty, "Not implemented: fetchCursor with prefetched associations") let (statement, adapter) = try request.prepare(db, forSingleResult: false) return try fetchCursor(statement, adapter: adapter) } @@ -86,7 +82,6 @@ extension FetchableRecord { /// - sql: a FetchRequest. /// - returns: An array of records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - @inlinable // TODO: should not be inlinable public static func fetchAll(_ db: Database, _ request: QueryInterfaceRequest) throws -> [Self] { let (statement, adapter) = try request.prepare(db, forSingleResult: false) let associations = request.prefetchedAssociations @@ -109,7 +104,6 @@ extension FetchableRecord { /// - sql: a FetchRequest. /// - returns: An optional record. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - @inlinable // TODO: should not be inlinable public static func fetchOne(_ db: Database, _ request: QueryInterfaceRequest) throws -> Self? { let (statement, adapter) = try request.prepare(db, forSingleResult: true) let associations = request.prefetchedAssociations diff --git a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift index b7478ea94..bdd126f8f 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift @@ -21,7 +21,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { } } - /// Creates a request that joins an association. The columns of the + /// Creates a request that includes an association. The columns of the /// associated record are selected. The returned request requires /// that the associated database table contains a matching row. public func including(required association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { @@ -43,7 +43,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { } } - /// Creates a request that includes an association. The columns of the + /// Creates a request that joins an association. The columns of the /// associated record are not selected. The returned request requires /// that the associated database table contains a matching row. public func joining(required association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index 14c79ced0..00afbc9bd 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -39,7 +39,6 @@ public struct QueryInterfaceRequest { self.init(query: SQLSelectQuery(relation: relation)) } - @usableFromInline // TODO: should not be @usableFromInline var prefetchedAssociations: [SQLAssociation] { return query.relation.prefetchedAssociations } diff --git a/GRDB/QueryInterface/Row+QueryInterface.swift b/GRDB/QueryInterface/Row+QueryInterface.swift index 6c9c820ac..33097f021 100644 --- a/GRDB/QueryInterface/Row+QueryInterface.swift +++ b/GRDB/QueryInterface/Row+QueryInterface.swift @@ -27,7 +27,6 @@ extension QueryInterfaceRequest where RowDecoder == Row { /// - parameter db: A database connection. /// - returns: A cursor over fetched rows. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - @inlinable // TODO: should not be inlinable public func fetchCursor(_ db: Database) throws -> RowCursor { return try Row.fetchCursor(db, self) } @@ -40,7 +39,6 @@ extension QueryInterfaceRequest where RowDecoder == Row { /// - parameter db: A database connection. /// - returns: An array of fetched rows. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - @inlinable // TODO: should not be inlinable public func fetchAll(_ db: Database) throws -> [Row] { return try Row.fetchAll(db, self) } @@ -53,7 +51,6 @@ extension QueryInterfaceRequest where RowDecoder == Row { /// - parameter db: A database connection. /// - returns: An optional row. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - @inlinable // TODO: should not be inlinable public func fetchOne(_ db: Database) throws -> Row? { return try Row.fetchOne(db, self) } @@ -90,9 +87,8 @@ extension Row { /// - request: A FetchRequest. /// - returns: A cursor over fetched rows. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - @inlinable // TODO: should not be inlinable public static func fetchCursor(_ db: Database, _ request: QueryInterfaceRequest) throws -> RowCursor { - precondition(request.prefetchedAssociations.isEmpty, "Not implemented: fetching cursor with prefetched associations") + precondition(request.prefetchedAssociations.isEmpty, "Not implemented: fetchCursor with prefetched associations") let (statement, adapter) = try request.prepare(db, forSingleResult: false) return try fetchCursor(statement, adapter: adapter) } @@ -107,7 +103,6 @@ extension Row { /// - request: A FetchRequest. /// - returns: An array of rows. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - @inlinable // TODO: should not be inlinable public static func fetchAll(_ db: Database, _ request: QueryInterfaceRequest) throws -> [Row] { let (statement, adapter) = try request.prepare(db, forSingleResult: false) let rows = try fetchAll(statement, adapter: adapter) @@ -129,7 +124,6 @@ extension Row { /// - request: A FetchRequest. /// - returns: An optional row. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. - @inlinable // TODO: should not be inlinable public static func fetchOne(_ db: Database, _ request: QueryInterfaceRequest) throws -> Row? { let (statement, adapter) = try request.prepare(db, forSingleResult: true) guard let row = try fetchOne(statement, adapter: adapter) else { @@ -143,8 +137,8 @@ extension Row { return row } - @usableFromInline - /* private */ static func prefetch(_ db: Database, associations: [SQLAssociation], in rows: [Row]) throws { + /// Append rows from prefetched associations into the argument rows. + static func prefetch(_ db: Database, associations: [SQLAssociation], in rows: [Row]) throws { guard let firstRow = rows.first else { return } @@ -158,26 +152,24 @@ extension Row { // Annotate prefetched rows with pivot columns, so that we can // group them. // - // Those pivot columns are necessaary when we prefetch + // Those pivot columns are necessary when we prefetch // indirect associations: // // // SELECT country.*, passport.citizenId AS grdb_citizenId - // // -- ^ the necessaary pivot column + // // -- ^ the necessary pivot column // // FROM country // // JOIN passport ON passport.countryCode = country.code // // AND passport.citizenId IN (1, 2, 3) // Citizen.including(all: Citizen.countries) // - // Those pivot columns are redundant when we prefetch - // direct associations: + // Those pivot columns are redundant when we prefetch direct + // associations (maybe we'll remove this redundancy later): // // // SELECT *, authorId AS grdb_authorId // // -- ^ the redundant pivot column // // FROM book // // WHERE authorId IN (1, 2, 3) // Author.including(all: Author.books) - // - // Maybe we'll remove this redundancy later. let pivotColumns = pivotMapping.map { $0.right } let pivotAlias = TableAlias() let prefetchedRelation = association