From 8d759898d3767832e203c8ef79a911dc0c283eb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 4 May 2019 23:07:45 +0200 Subject: [PATCH] Cleanup --- GRDB/QueryInterface/Row+QueryInterface.swift | 23 ++++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/GRDB/QueryInterface/Row+QueryInterface.swift b/GRDB/QueryInterface/Row+QueryInterface.swift index a838f92a4..2378a5d2d 100644 --- a/GRDB/QueryInterface/Row+QueryInterface.swift +++ b/GRDB/QueryInterface/Row+QueryInterface.swift @@ -81,14 +81,7 @@ extension Row { .fetchAll(db) .grouped(byDatabaseValuesOnColumns: rightColumns.map { "grdb_\($0)" }) - let groupingIndexes: [Int] = pivotMapping.map { columns -> Int in - let column = columns.left - guard let index = firstRow.index(ofColumn: column) else { - fatalError("Column \(column) is not selected") - } - return index - } - + let groupingIndexes = firstRow.indexes(ofColumns: pivotMapping.map { $0.left }) for row in rows { let groupingKey = groupingIndexes.map { row.impl.databaseValue(atUncheckedIndex: $0) } let prefetchedRows = prefetchedRows[groupingKey] ?? [] @@ -108,13 +101,25 @@ extension Array where Element == Row { guard let firstRow = first else { return [:] } - let indexes: [Int] = columns.map { firstRow.index(ofColumn: $0)! } + let indexes = firstRow.indexes(ofColumns: columns) return Dictionary(grouping: self, by: { row in indexes.map { row.impl.databaseValue(atUncheckedIndex: $0) } }) } } +extension Row { + /// - precondition: Columns all exist in tthe row. + fileprivate func indexes(ofColumns columns: [String]) -> [Int] { + return columns.map { column -> Int in + guard let index = index(ofColumn: column) else { + fatalError("Column \(column) is not selected") + } + return index + } + } +} + extension QueryInterfaceRequest where RowDecoder == Row { // MARK: Fetching Rows