diff --git a/GRDB/Record/FetchableRecord+Decodable.swift b/GRDB/Record/FetchableRecord+Decodable.swift index bdfd96c45..6437d4e1e 100644 --- a/GRDB/Record/FetchableRecord+Decodable.swift +++ b/GRDB/Record/FetchableRecord+Decodable.swift @@ -51,7 +51,8 @@ private struct _RowDecoder: Decoder { func singleValueContainer() throws -> SingleValueDecodingContainer { guard let key = codingPath.last else { - fatalError("single value decoding from database row is not supported") + // Decoding an array of scalars from rows: pick the first column + return ColumnDecoder(row: row, columnIndex: 0, codingPath: codingPath) } guard let index = row.index(forColumn: key.stringValue) else { // Don't use DecodingError.keyNotFound: diff --git a/Tests/GRDBTests/AssociationPrefetchingCodableRecordTests.swift b/Tests/GRDBTests/AssociationPrefetchingCodableRecordTests.swift index 64cfea4d9..3c800ccd0 100644 --- a/Tests/GRDBTests/AssociationPrefetchingCodableRecordTests.swift +++ b/Tests/GRDBTests/AssociationPrefetchingCodableRecordTests.swift @@ -230,6 +230,86 @@ class AssociationPrefetchingCodableRecordTests: GRDBTestCase { } } + func testIncludingAllHasManyScalar() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + // Plain request + do { + let request = A + .including(all: A + .hasMany(B.self) + .select(Column("colb2")) + .distinct() + .order(Column("colb2"))) + .orderByPrimaryKey() + + // Array + do { + struct Record: FetchableRecord, Decodable, Equatable { + var a: A + var bs: [Int64] + } + + // Record.fetchAll + do { + let records = try Record.fetchAll(db, request) + XCTAssertEqual(records, [ + Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: [1]), + Record( + a: A(row: ["cola1": 2, "cola2": "a2"]), + bs: [2]), + Record( + a: A(row: ["cola1": 3, "cola2": "a3"]), + bs: []), + ]) + } + + // Record.fetchOne + do { + let record = try Record.fetchOne(db, request)! + XCTAssertEqual(record, Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: [1])) + } + } + + // Set + do { + struct Record: FetchableRecord, Decodable, Equatable { + var a: A + var bs: Set + } + + // Record.fetchAll + do { + let records = try Record.fetchAll(db, request) + XCTAssertEqual(records, [ + Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: [1]), + Record( + a: A(row: ["cola1": 2, "cola2": "a2"]), + bs: [2]), + Record( + a: A(row: ["cola1": 3, "cola2": "a3"]), + bs: []), + ]) + } + + // Record.fetchOne + do { + let record = try Record.fetchOne(db, request)! + XCTAssertEqual(record, Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: [1])) + } + } + } + } + } + func testIncludingAllHasManyIncludingAllHasMany() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in diff --git a/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift b/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift index 52b17be15..6666ed177 100644 --- a/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift +++ b/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift @@ -157,6 +157,37 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { } } + func testIncludingAllHasManyScalar() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + // Plain request + do { + let request = A + .including(all: A + .hasMany(B.self) + .select(Column("colb2")) + .distinct() + .order(Column("colb2"))) + .orderByPrimaryKey() + + sqlQueries.removeAll() + _ = try Row.fetchAll(db, request) + + let selectQueries = sqlQueries.filter(isSelectQuery) + XCTAssertEqual(selectQueries, [ + """ + SELECT * FROM "a" ORDER BY "cola1" + """, + """ + SELECT DISTINCT "colb2", "colb2" AS "grdb_colb2" \ + FROM "b" \ + WHERE "colb2" IN (1, 2, 3) \ + ORDER BY "colb2" + """]) + } + } + } + func testIncludingAllHasManyWithCompoundForeignKey() throws { // We can use the CTE technique let dbQueue = try makeDatabaseQueue()