From c30b49117bb71ad3dd6e5a2f285e8cf2bd41855a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 4 Mar 2022 12:02:23 +0100 Subject: [PATCH] If a scope is found, there is no point searching for prefetched rows --- GRDB/Record/FetchableRecord+Decodable.swift | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/GRDB/Record/FetchableRecord+Decodable.swift b/GRDB/Record/FetchableRecord+Decodable.swift index 27a8e8d3d..1932faf37 100644 --- a/GRDB/Record/FetchableRecord+Decodable.swift +++ b/GRDB/Record/FetchableRecord+Decodable.swift @@ -219,9 +219,15 @@ private struct _RowDecoder: Decoder { } } - // Scope? (beware left joins: check if scoped row contains non-null values) - if let scopedRow = row.scopesTree[key.stringValue], scopedRow.containsNonNullValue { - return try decode(type, fromRow: scopedRow, codingPath: codingPath + [key]) + // Scope? + if let scopedRow = row.scopesTree[key.stringValue] { + // Beware left joins: check if scoped row contains non-null + // values before decoding + if scopedRow.containsNonNullValue { + return try decode(type, fromRow: scopedRow, codingPath: codingPath + [key]) + } else { + return nil + } } // Prefetched Rows? @@ -230,7 +236,7 @@ private struct _RowDecoder: Decoder { return try T(from: decoder) } - // Key is not a column, and not a scope. + // Unknown key return nil } @@ -265,7 +271,7 @@ private struct _RowDecoder: Decoder { return try T(from: decoder) } - // Key is not a column, and not a scope. + // Unknown key // // Should be throw an error? Well... The use case is the following: // @@ -292,7 +298,8 @@ private struct _RowDecoder: Decoder { // // Our current strategy is to assume that a missing key (such as // "book", which is not the name of a column, and not the name of a - // scope) has to be decoded right from the base row. + // scope) has to be decoded right from the base row. But this can + // happen only once. if let decodedRootKey = decodedRootKey { let keys = [decodedRootKey.stringValue, key.stringValue].sorted() throw DecodingError.keyNotFound(key, DecodingError.Context(