diff --git a/GRDB/QueryInterface/SQL/SQLSelection.swift b/GRDB/QueryInterface/SQL/SQLSelection.swift index e4bbfb781..a1faad292 100644 --- a/GRDB/QueryInterface/SQL/SQLSelection.swift +++ b/GRDB/QueryInterface/SQL/SQLSelection.swift @@ -81,12 +81,15 @@ public struct SQLSelection { extension SQLSelection { /// Returns the number of columns in the selection. - func columnCount(_ db: Database) throws -> Int { + /// + /// Returns nil when the number of columns is unknown. + func columnCount(_ db: Database) throws -> Int? { switch impl { case .allColumns: - // Likely a GRDB bug - fatalError("Can't compute number of columns without an alias") - + // Likely a GRDB bug: we can't count the number of columns in an + // unqualified table. + return nil + case let .qualifiedAllColumns(alias): return try db.columns(in: alias.tableName).count @@ -101,11 +104,9 @@ extension SQLSelection { return 1 case .literal: - fatalError(""" - Selection literals don't known how many columns they contain. \ - To resolve this error, select one or several literal expressions instead. \ - See SQLLiteral.sqlExpression. - """) + // We do not embed any SQL parser: we can't count the number of + // columns in a literal selection. + return nil } } @@ -298,6 +299,29 @@ extension SQLSelection { } } +extension Array where Element == SQLSelection { + /// Returns the number of columns in the selection. + /// + /// This method raises a fatal error if the selection contains a literal, + /// + /// See `SQLSelection.columnCount(_:)` for testability. + func columnCount(_ db: Database) throws -> Int { + try reduce(0) { acc, selection in + guard let count = try selection.columnCount(db) else { + // Found an SQL literal: + // - Player.select(sql: "id, name, score") + // - Player.select(literal: "id, name, score") + fatalError(""" + Selection literals don't known how many columns they contain. \ + To resolve this error, select one or several expressions instead. + """) + } + + return acc + count + } + } +} + enum SQLCount { /// Represents `COUNT(*)` case all diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 20c854d6b..088579bc7 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -102,9 +102,7 @@ struct SQLQueryGenerator: Refinable { try relation .selectionPromise .resolve(db) - .reduce(0) { - try $0 + $1.columnCount(db) - } + .columnCount(db) } /// Returns a select statement @@ -608,9 +606,7 @@ private struct SQLQualifiedRelation { // The number of columns in source selection. Columns selected by joined // relations are appended after. - let sourceSelectionWidth = try sourceSelectionPromise.resolve(db).reduce(0) { - try $0 + $1.columnCount(db) - } + let sourceSelectionWidth = try sourceSelectionPromise.resolve(db).columnCount(db) // Recursively build adapters for each joined relation with a selection. // Name them according to the join keys. diff --git a/GRDB/Record/TableRecord.swift b/GRDB/Record/TableRecord.swift index bef83be36..120e42d20 100644 --- a/GRDB/Record/TableRecord.swift +++ b/GRDB/Record/TableRecord.swift @@ -131,10 +131,11 @@ extension TableRecord { /// try Player.numberOfSelectedColumns(db) /// } public static func numberOfSelectedColumns(_ db: Database) throws -> Int { + // The alias makes it possible to count the columns in `SELECT *`: let alias = TableAlias(tableName: databaseTableName) return try databaseSelection - .map { try $0.sqlSelection.qualified(with: alias).columnCount(db) } - .reduce(0, +) + .map { $0.sqlSelection.qualified(with: alias) } + .columnCount(db) } }