From 67bdc740bd3d07526dea4d2ed00db944830ca433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 6 Mar 2023 08:03:08 +0100 Subject: [PATCH] DatabaseValueConvertible and collections: move the rationale out of the user-facing documentation. --- GRDB/Core/DatabaseValueConvertible.swift | 35 ++++++++++++++++++ .../Extension/DatabaseValueConvertible.md | 37 +------------------ 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/GRDB/Core/DatabaseValueConvertible.swift b/GRDB/Core/DatabaseValueConvertible.swift index e88184af8..64bc7800d 100644 --- a/GRDB/Core/DatabaseValueConvertible.swift +++ b/GRDB/Core/DatabaseValueConvertible.swift @@ -1,3 +1,38 @@ +// Standard collections `Array`, `Set`, and `Dictionary` do not conform to +// `DatabaseValueConvertible`, on purpose. +// +// Adding `DatabaseValueConvertible` conformance to those collection types +// would litter JSON values in unexpected places, and foster misuse. For +// example, it is better when the code below *does not compile*: +// +// ```swift +// // MISUSE: if Array would conform to DatabaseValueConvertible, this +// // code would compile, and run the incorrect SQLite query +// // `SELECT ... WHERE id IN ('[1,2,3]')`, instead of the expected +// // `SELECT ... WHERE id IN (1, 2, 3)`. +// let ids = [1, 2, 3] +// let players = try Player.fetchAll(db, sql: """ +// SELECT * FROM player WHERE id IN (?) +// """, arguments: [ids]) +// ``` +// +// Correct and fostered versions of the code above are: +// +// ```swift +// // CORRECT (explicit SQLite arguments): +// let ids = [1, 2, 3] +// let questionMarks = databaseQuestionMarks(count: ids.count) // "?,?,?" +// let players = try Player.fetchAll(db, sql: """ +// SELECT * FROM player WHERE id IN (\(questionMarks)) +// """, arguments: StatementArguments(ids)) +// +// // CORRECT (SQL interpolation): +// let ids = [1, 2, 3] +// let request: SQLRequest = """ +// SELECT * FROM player WHERE id IN \(ids) +// """ +// let players = try request.fetchAll(db) +// ``` public protocol DatabaseValueConvertible: SQLExpressible, StatementBinding { /// A database value. var databaseValue: DatabaseValue { get } diff --git a/GRDB/Documentation.docc/Extension/DatabaseValueConvertible.md b/GRDB/Documentation.docc/Extension/DatabaseValueConvertible.md index da9247a2b..e6c692c7a 100644 --- a/GRDB/Documentation.docc/Extension/DatabaseValueConvertible.md +++ b/GRDB/Documentation.docc/Extension/DatabaseValueConvertible.md @@ -8,6 +8,8 @@ A `DatabaseValueConvertible` type supports conversion to and from database value > Note: Types that converts to and from multiple columns in a database row must not conform to the `DatabaseValueConvertible` protocol. Those types are called **record types**, and should conform to record protocols instead. See . +> Note: Standard collections `Array`, `Set`, and `Dictionary` do not conform to `DatabaseValueConvertible`. To store arrays, sets, or dictionaries in individual database values, wrap them as properties of `Codable` record types. They will automatically be stored as JSON objects and arrays. See . + ## Conforming to the DatabaseValueConvertible Protocol To conform to `DatabaseValueConvertible`, implement the two requirements ``fromDatabaseValue(_:)-21zzv`` and ``databaseValue-1ob9k``. Do not customize the ``fromMissingColumn()-7iamp`` requirement. Do not customize the conformance of `Optional`: it is built-in when the wrapped type is itself `DatabaseValueConvertible`. @@ -127,41 +129,6 @@ extension EvenInteger: StatementColumnConvertible { } ``` -> Note: Standard collections `Array`, `Set`, and `Dictionary` do not conform to `DatabaseValueConvertible`, on purpose. You won't be able to directly fetch or store arrays, sets, or dictionaries as JSON database values. -> -> Standard collections get free JSON support when they are embedded as properties of `Codable` record types, though: see . -> -> It is not advised to consider the absence of those conformances as a lack, and add `DatabaseValueConvertible` conformance to those collection types in your application. This would litter JSON values in unexpected places, and foster misuse. For example, it is better when the code below *does not compile*: -> -> ```swift -> // MISUSE: if Array would conform to DatabaseValueConvertible, this -> // code would compile, and run the incorrect SQLite query -> // `SELECT ... WHERE id IN ('[1,2,3]')`, instead of the expected -> // `SELECT ... WHERE id IN (1, 2, 3)`. -> let ids = [1, 2, 3] -> let players = try Player.fetchAll(db, sql: """ -> SELECT * FROM player WHERE id IN (?) -> """, arguments: [ids]) -> ``` -> -> Correct and fostered versions of the code above are: -> -> ```swift -> // CORRECT (explicit SQLite arguments): -> let ids = [1, 2, 3] -> let questionMarks = databaseQuestionMarks(count: ids.count) // "?,?,?" -> let players = try Player.fetchAll(db, sql: """ -> SELECT * FROM player WHERE id IN (\(questionMarks)) -> """, arguments: StatementArguments(ids)) -> -> // CORRECT (SQL interpolation): -> let ids = [1, 2, 3] -> let request: SQLRequest = """ -> SELECT * FROM player WHERE id IN \(ids) -> """ -> let players = try request.fetchAll(db) -> ``` - ## Topics ### Creating a Value