diff --git a/CHANGELOG.md b/CHANGELOG.md index 82c337c18..c23bf0cef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,14 +41,15 @@ It comes with new features, but also a few breaking changes, and a set of update ### Documentation Diff - [Associations](Documentation/AssociationsBasics.md): Discover the major GRDB 3 feature -- [Database Queues](https://github.com/groue/GRDB.swift/blob/GRDB3/README.md#database-queues): focus on the `read` and `write` methods. -- [Database Pools](https://github.com/groue/GRDB.swift/blob/GRDB3/README.md#database-pools): focus on the `read` and `write` methods. -- [Transactions and Savepoints](https://github.com/groue/GRDB.swift/blob/GRDB3/README.md#transactions-and-savepoints): the chapter has been rewritten in order to introduce transactions as a power-user feature. -- [ScopeAdapter](https://github.com/groue/GRDB.swift/blob/GRDB3/README.md#scopeadapter): do you use row adapters? If so, have a look. -- [Examples of Record Definitions](https://github.com/groue/GRDB.swift/blob/GRDB3/README.md#examples-of-record-definitions): this new chapter provides a handy reference of the three main ways to define record types (Codable, plain struct, Record subclass). -- [SQL Operators](https://github.com/groue/GRDB.swift/blob/GRDB3/README.md#sql-operators): the chapter introduces the new `joined(operator:)` method that lets you join a chain of expressions with `AND` or `OR` without nesting: `[cond1, cond2, ...].joined(operator: .and)`. -- [Custom Requests](https://github.com/groue/GRDB.swift/blob/GRDB3/README.md#custom-requests): the old `Request` and `TypedRequest` protocols have been replaced with `FetchRequest`. If you want to know more about custom requests, check this chapter. -- [Migrations](https://github.com/groue/GRDB.swift/blob/GRDB3/README.md#migrations): learn how to check if a migration has been applied (very useful for migration tests). +- [Database Queues](README.md#database-queues): focus on the `read` and `write` methods. +- [Database Pools](README.md#database-pools): focus on the `read` and `write` methods. +- [Transactions and Savepoints](README.md#transactions-and-savepoints): the chapter has been rewritten in order to introduce transactions as a power-user feature. +- [ScopeAdapter](README.md#scopeadapter): do you use row adapters? If so, have a look. +- [Examples of Record Definitions](README.md#examples-of-record-definitions): this new chapter provides a handy reference of the three main ways to define record types (Codable, plain struct, Record subclass). +- [SQL Operators](README.md#sql-operators): the chapter introduces the new `joined(operator:)` method that lets you join a chain of expressions with `AND` or `OR` without nesting: `[cond1, cond2, ...].joined(operator: .and)`. +- [Custom Requests](README.md#custom-requests): the old `Request` and `TypedRequest` protocols have been replaced with `FetchRequest`. If you want to know more about custom requests, check this chapter. +- [Customized Decoding of Database Rows](README.md#customized-decoding-of-database-rows): learn how to escape the ready-made `FetchableRecord` protocol when it does not fit your needs. +- [Migrations](README.md#migrations): learn how to check if a migration has been applied (very useful for migration tests). diff --git a/Playgrounds/CustomizedDecodingOfDatabaseRows.playground/Contents.swift b/Playgrounds/CustomizedDecodingOfDatabaseRows.playground/Contents.swift index d1f6c7d76..9d02e4313 100644 --- a/Playgrounds/CustomizedDecodingOfDatabaseRows.playground/Contents.swift +++ b/Playgrounds/CustomizedDecodingOfDatabaseRows.playground/Contents.swift @@ -19,12 +19,12 @@ import GRDB //: //: For example: //: -//: - Your application needs polymorphic row decoding (which means decoding some -//: class or another depending on the values contained in a database row). +//: - Your application needs polymorphic row decoding: it decodes some class or +//: another, depending on the values contained in a database row. //: -//: - Your application needs to decode rows with a context - which means that -//: each decoded value should be initialized with some extra value that does not -//: come from the database. +//: - Your application needs to decode rows with a context: each decoded value +//: should be initialized with some extra value that does not come from +//: the database. //: //: - Your application needs a record type that supports untrusted databases, //: and may fail at decoding database rows (throw an error when a row contains @@ -391,9 +391,9 @@ try dbQueue.read { db in //: To end this tour, let's quickly look at two other possible customized //: row decoding strategies: //: -//: - Your application needs to decode rows with a context - which means that -//: each decoded value should be initialized with some extra value that does not -//: come from the database. +//: - Your application needs to decode rows with a context: each decoded value +//: should be initialized with some extra value that does not come from +//: the database. //: //: In this case, you may define a `ContextFetchableRecord` protocol, and //: derive all other fetching methods from the most fundamental one, which diff --git a/README.md b/README.md index 3d6f7c1a3..5a2ed9396 100644 --- a/README.md +++ b/README.md @@ -2033,6 +2033,7 @@ Your custom structs and classes can adopt each protocol individually, and opt in - [Record Comparison] - [Conflict Resolution](#conflict-resolution) - [The Implicit RowID Primary Key](#the-implicit-rowid-primary-key) +- [Customized Decoding of Database Rows](#customized-decoding-of-database-rows) **Records in a Glance** @@ -2143,6 +2144,7 @@ Details follow: - [Record Comparison] - [Conflict Resolution](#conflict-resolution) - [The Implicit RowID Primary Key](#the-implicit-rowid-primary-key) +- [Customized Decoding of Database Rows](#customized-decoding-of-database-rows) - [Examples of Record Definitions](#examples-of-record-definitions) - [List of Record Methods](#list-of-record-methods) @@ -2278,6 +2280,8 @@ try Place.fetchOne(db, "SELECT ...", arguments:...) // Place? See [fetching methods](#fetching-methods) for information about the `fetchCursor`, `fetchAll` and `fetchOne` methods. See [StatementArguments](http://groue.github.io/GRDB.swift/docs/3.0-beta.1/Structs/StatementArguments.html) for more information about the query arguments. +> :point_up: **Note**: The `FetchableRecord.init(row:)` initializer fits the needs of most applications. But some application are more demanding than others. When FetchableRecord does not exactly provide the support you need, have a look at the [Customized Decoding of Database Rows](#customized-decoding-of-database-rows) chapter. + ## TableRecord Protocol @@ -2928,6 +2932,21 @@ When SQLite won't let you provide an explicit primary key (as in [full-text](#fu ``` +## Customized Decoding of Database Rows + +**Some GRDB users eventually discover that the [FetchableRecord] protocol does not fit all situations.** Use cases that are not well handled by FetchableRecord include: + +- Your application needs polymorphic row decoding: it decodes some class or another, depending on the values contained in a database row. + +- Your application needs to decode rows with a context: each decoded value should be initialized with some extra value that does not come from the database. + +- Your application needs a record type that supports untrusted databases, and may fail at decoding database rows (throw an error when a row contains invalid values). + +Since those use cases are not well handled by FetchableRecord, don't try to implement them on top of this protocol: you'll just fight the framework. + +Instead, please have a look at the [CustomizedDecodingOfDatabaseRows](Playgrounds/CustomizedDecodingOfDatabaseRows.playground/Contents.swift) playground. You'll run some sample code, and learn how to escape FetchableRecord when you need. + + ## Examples of Record Definitions We will show below how to declare a record type for the following database table: