From ff7f9073fca7ebd7baafda1b1a91c9426c7af259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 12 Mar 2023 13:26:09 +0100 Subject: [PATCH 1/7] Add links to DocC reference --- README.md | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c989c124b..c41b2f938 100644 --- a/README.md +++ b/README.md @@ -322,7 +322,7 @@ Documentation #### Reference -- [GRDB Reference](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/) +- 📖 [GRDB Reference](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/) #### Getting Started @@ -477,7 +477,7 @@ Advanced topics: ## Executing Updates -Once granted with a [database connection], the `execute` method executes the SQL statements that do not return any database row, such as `CREATE TABLE`, `INSERT`, `DELETE`, `ALTER`, etc. +Once granted with a [database connection], the [`execute(sql:arguments:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/database/execute(sql:arguments:)) method executes the SQL statements that do not return any database row, such as `CREATE TABLE`, `INSERT`, `DELETE`, `ALTER`, etc. For example: @@ -503,12 +503,14 @@ try dbQueue.write { db in The `?` and colon-prefixed keys like `:score` in the SQL query are the **statements arguments**. You pass arguments with arrays or dictionaries, as in the example above. See [Values](#values) for more information on supported arguments types (Bool, Int, String, Date, Swift enums, etc.), and [`StatementArguments`] for a detailed documentation of SQLite arguments. -You can also embed query arguments right into your SQL queries, with the `literal` argument label, as in the example below. See [SQL Interpolation] for more details. +You can also embed query arguments right into your SQL queries, with [`execute(literal:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/database/execute(literal:)), as in the example below. See [SQL Interpolation] for more details. ```swift try dbQueue.write { db in + let name = "O'Brien" + let score = 550 try db.execute(literal: """ - INSERT INTO player (name, score) VALUES (\("O'Brien"), \(550)) + INSERT INTO player (name, score) VALUES (\(name), \(score)) """) } ``` @@ -553,7 +555,7 @@ try db.execute(literal: """ When you want to make sure that a single statement is executed, use a prepared [`Statement`]. -**After an INSERT statement**, you can get the row ID of the inserted row: +**After an INSERT statement**, you can get the row ID of the inserted row with [`lastInsertedRowID`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/database/lastinsertedrowid): ```swift try db.execute( @@ -653,6 +655,8 @@ try Row.fetchOne(...) // Row? ### Cursors +📖 [`Cursor`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/cursor) + **Whenever you consume several rows from the database, you can fetch an Array, a Set, or a Cursor**. The `fetchAll()` and `fetchSet()` methods return regular Swift array and sets, that you iterate like all other arrays and sets: @@ -778,6 +782,7 @@ If you don't see, or don't care about the difference, use arrays. If you care ab - [Column Values](#column-values) - [DatabaseValue](#databasevalue) - [Rows as Dictionaries](#rows-as-dictionaries) +- 📖 [`Row`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/row) #### Fetching Rows @@ -945,9 +950,11 @@ Generally speaking, you can extract the type you need, provided it can be conver #### DatabaseValue -**DatabaseValue is an intermediate type between SQLite and your values, which gives information about the raw value stored in the database.** +📖 [`DatabaseValue`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasevalue) -You get DatabaseValue just like other value types: +**`DatabaseValue` is an intermediate type between SQLite and your values, which gives information about the raw value stored in the database.** + +You get `DatabaseValue` just like other value types: ```swift let dbValue: DatabaseValue = row[0] @@ -969,7 +976,7 @@ case .blob(let data): print("Data: \(data)") } ``` -You can extract regular [values](#values) (Bool, Int, String, Date, Swift enums, etc.) from DatabaseValue with the [DatabaseValueConvertible.fromDatabaseValue()](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasevalueconvertible/fromdatabasevalue(_:)-21zzv) method: +You can extract regular [values](#values) (Bool, Int, String, Date, Swift enums, etc.) from `DatabaseValue` with the [fromDatabaseValue()](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasevalueconvertible/fromdatabasevalue(_:)-21zzv) method: ```swift let dbValue: DatabaseValue = row["bookCount"] @@ -1055,6 +1062,8 @@ See the documentation of [`Dictionary.init(_:uniquingKeysWith:)`](https://develo ### Value Queries +📖 [`DatabaseValueConvertible`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasevalueconvertible) + **Instead of rows, you can directly fetch values.** There are many supported [value types](#values) (Bool, Int, String, Date, Swift enums, etc.). Like rows, fetch values as **cursors**, **arrays**, **sets**, or **single** values (see [fetching methods](#fetching-methods)). Values are extracted from the leftmost column of the SQL queries: @@ -1450,6 +1459,8 @@ SELECT maxLength(name) FROM player; -- custom aggregate ### Custom SQL Functions +📖 [`DatabaseFunction`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasefunction) + A *function* argument takes an array of [DatabaseValue](#databasevalue), and returns any valid [value](#values) (Bool, Int, String, Date, Swift enums, etc.) The number of database values is guaranteed to be *argumentCount*. SQLite has the opportunity to perform additional optimizations when functions are "pure", which means that their result only depends on their arguments. So make sure to set the *pure* argument to true when possible. @@ -1529,6 +1540,8 @@ Player.select(reverseString(nameColumn)) ### Custom Aggregates +📖 [`DatabaseFunction`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasefunction), [`DatabaseAggregate`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databaseaggregate) + Before registering a custom aggregate, you need to define a type that adopts the `DatabaseAggregate` protocol: ```swift @@ -1637,6 +1650,8 @@ Database.isSQLiteInternalTable(...) Database.isGRDBInternalTable(...) ``` +For more information, see [`tableExists(_:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/database/tableexists(_:)) and related methods. + ## Row Adapters From 0ffd25bc7498d9cdb08aa475da5b2f2b6372c93a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 12 Mar 2023 16:33:10 +0100 Subject: [PATCH 2/7] Fix documentation typo --- GRDB/Core/StatementColumnConvertible.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GRDB/Core/StatementColumnConvertible.swift b/GRDB/Core/StatementColumnConvertible.swift index 27ee208b6..6c63c2ced 100644 --- a/GRDB/Core/StatementColumnConvertible.swift +++ b/GRDB/Core/StatementColumnConvertible.swift @@ -12,7 +12,7 @@ /// // Optimized /// let scores = Int.fetchAll(db, sql: "SELECT score FROM player") /// -/// let rows = Row.fetchCursor(db, sql: "SELECT * FROM player") +/// let rows = try Row.fetchCursor(db, sql: "SELECT * FROM player") /// while let row = try rows.next() { /// // Optimized /// let int: Int = row[0] From 8cd71ac832ba59821692dbd6d71552493581c1d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 12 Mar 2023 17:18:28 +0100 Subject: [PATCH 3/7] Fix TableRecord.databaseSelection sample code --- GRDB/Record/TableRecord.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/GRDB/Record/TableRecord.swift b/GRDB/Record/TableRecord.swift index 27d3e914e..58f33e0e0 100644 --- a/GRDB/Record/TableRecord.swift +++ b/GRDB/Record/TableRecord.swift @@ -133,12 +133,15 @@ public protocol TableRecord { /// /// ```swift /// struct Player: TableRecord { - /// static let databaseSelection = [AllColumns()] + /// static let databaseSelection: [any SQLSelectable] = [AllColumns()] /// } /// /// struct PartialPlayer: TableRecord { /// static let databaseTableName = "player" - /// static let databaseSelection = [Column("id"), Column("name")] + /// static let databaseSelection: [any SQLSelectable] = [ + /// Column("id"), + /// Column("name"), + /// ] /// } /// /// // SELECT * FROM player @@ -147,6 +150,11 @@ public protocol TableRecord { /// // SELECT id, name FROM player /// try PartialPlayer.fetchAll(db) /// ``` + /// + /// > Important: Make sure the `databaseSelection` property is + /// > explicitly declared as `[any SQLSelectable]`. If it is not, the + /// > Swift compiler may silently miss the protocol requirement, + /// > resulting in sticky `SELECT *` requests. static var databaseSelection: [any SQLSelectable] { get } } From f7559d4c760e7405d6ef905d3f089ee3b6147cc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 12 Mar 2023 17:20:13 +0100 Subject: [PATCH 4/7] Moved row adapters doc into DocC --- CHANGELOG.md | 6 + Documentation/AssociationsBasics.md | 4 +- GRDB/Core/FetchRequest.swift | 4 +- GRDB/Core/Row.swift | 6 +- GRDB/Core/RowAdapter.swift | 360 ++++++++++++++++---- README.md | 504 +--------------------------- 6 files changed, 317 insertions(+), 567 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 330aaca0f..55f6f76e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -107,6 +107,12 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: --- +## Next Version + +- **Documentation Updates**: Moved more README chapters into DocC: + - [`RowAdapter`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/rowadapter) + - [`splittingRowAdapters(columnCounts:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/splittingrowadapters(columncounts:)) + ## 6.9.0 Released March 12, 2023 • [diff](https://github.com/groue/GRDB.swift/compare/v6.8.0...v6.9.0) diff --git a/Documentation/AssociationsBasics.md b/Documentation/AssociationsBasics.md index 5e00b98b2..ccd1e2832 100644 --- a/Documentation/AssociationsBasics.md +++ b/Documentation/AssociationsBasics.md @@ -130,7 +130,7 @@ let request = Book.including(optional: Book.author) let bookInfos = BookInfo.fetchAll(db, request) ``` -Before we dive in, please remember that associations can not generate all possible SQL queries that involve several tables. You may also *prefer* writing SQL, and this is just OK, because your SQL skills are welcome: see the [Joined Queries Support](../README.md#joined-queries-support) chapter. +Before we dive in, please remember that associations can not generate all possible SQL queries that involve several tables. You may also *prefer* writing SQL, and this is just OK, because your SQL skills are welcome. The [`splittingRowAdapters(columnCounts:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/splittingrowadapters(columncounts:)) method can help you consume the rows fetched from joined queries, as in `SELECT book.*, author.* FROM ...`. ## Required Protocols @@ -2996,7 +2996,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. [Isolation of Multiple Aggregates]: #isolation-of-multiple-aggregates [DerivableRequest Protocol]: #derivablerequest-protocol [Known Issues]: #known-issues -[Row Adapters]: ../README.md#row-adapters +[Row Adapters]: https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/rowadapter [query interface requests]: ../README.md#requests [TableRecord]: ../README.md#tablerecord-protocol [Good Practices for Designing Record Types]: GoodPracticesForDesigningRecordTypes.md diff --git a/GRDB/Core/FetchRequest.swift b/GRDB/Core/FetchRequest.swift index a1848aca8..c916c2d58 100644 --- a/GRDB/Core/FetchRequest.swift +++ b/GRDB/Core/FetchRequest.swift @@ -141,7 +141,9 @@ extension FetchRequest { /// Returns an adapted request. /// /// The returned request performs an identical database query, but adapts - /// the fetched rows. See ``RowAdapter``. + /// the fetched rows. See ``RowAdapter``, and + /// ``splittingRowAdapters(columnCounts:)`` for a sample code that uses + /// `adapted(_:)`. /// /// - parameter adapter: A closure that accepts a database connection and /// returns a row adapter. diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 8c7c5cb29..a75a04352 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -1152,8 +1152,7 @@ extension Row { /// Nil is returned if the scope is not available, or contains only /// null values. /// - /// See - /// for more information. + /// See ``splittingRowAdapters(columnCounts:)`` for a sample code. func decodeIfPresent( _ type: Record.Type = Record.self, forKey scope: String) @@ -1194,8 +1193,7 @@ extension Row { /// A fatal error is raised if the scope is not available, or contains only /// null values. /// - /// See - /// for more information. + /// See ``splittingRowAdapters(columnCounts:)`` for a sample code. func decode( _ type: Record.Type = Record.self, forKey scope: String) diff --git a/GRDB/Core/RowAdapter.swift b/GRDB/Core/RowAdapter.swift index c3902faab..670cc3f99 100644 --- a/GRDB/Core/RowAdapter.swift +++ b/GRDB/Core/RowAdapter.swift @@ -1,27 +1,136 @@ import Foundation -/// Returns an array of row adapters that split a row according to the input -/// number of columns. +/// Returns an array of row adapters that split a row according to the +/// provided numbers of columns. /// -/// For example: +/// This method is useful for splitting a row into chunks. +/// +/// For example, let's consider the following SQL query: /// /// ```swift -/// let sql = "SELECT 1, 2,3,4, 5,6, 7,8" -/// // <.><. . .><. .><. .> -/// let adapters = splittingRowAdapters([1, 3, 2]) -/// let adapter = ScopeAdapter([ -/// "a": adapters[0], -/// "b": adapters[1], -/// "c": adapters[2], -/// "d": adapters[3]]) -/// let row = try Row.fetchOne(db, sql: sql, adapter: adapter) -/// row.scopes["a"] // [1] -/// row.scopes["b"] // [2, 3, 4] -/// row.scopes["c"] // [5, 6] -/// row.scopes["d"] // [7, 8] +/// let sql = """ +/// SELECT player.*, team.* +/// FROM player +/// LEFT JOIN team ON team.id = player.teamId +/// WHERE player.id = ? +/// """ /// ``` +/// +/// The resulting rows contains columns from both player and team tables: +/// +/// ```swift +/// // [id: 1, name: "Arthur", teamId: 42, id: 42, name: "Reds"] +/// // <---------------------------------><--------------------> +/// // player columns team columns +/// let row = try Row.fetchOne(db, sql: sql, arguments: [1]) +/// ``` +/// +/// Because some columns have the same name (`id` and `name`), it is +/// difficult to access the team columns. +/// +/// `splittingRowAdapters` and ``ScopeAdapter`` make it possible to +/// access player and team columns independently, with row ``Row/scopes``: +/// +/// ```swift +/// let adapters = try splittingRowAdapters([ +/// db.columns(in: "player").count, +/// db.columns(in: "team").count, +/// ]) +/// let adapter = ScopeAdapter([ +/// "player": adapters[0], +/// "team": adapters[1], +/// ]) +/// if let row = try Row.fetchOne(db, sql: sql, arguments: [1], adapter: adapter) { +/// // A Row that only contains player columns +/// // [id: 1, name: "Arthur", teamId: 42] +/// row.scopes["player"] +/// +/// // A Row that only contains team columns +/// // [id: 42, name: "Reds"] +/// row.scopes["team"] +/// } +/// ``` +/// +/// Decoding ``FetchableRecord`` types is easy: +/// +/// ```swift +/// if let row = try Row.fetchOne(db, sql: sql, arguments: [1], adapter: adapter) { +/// // Player(id: 1, name: "Arthur", teamId: 42) +/// let player: Player = row["player"] +/// +/// // Team(id: 42, name: "Reds") +/// // nil if the LEFT JOIN has fetched NULL team columns +/// if let team: Team? = row["team"] +/// } +/// ``` +/// +/// You can package this technique in a dedicated type, as in the next +/// example. It enhances the previous sample codes with: +/// +/// - Support for record types that customize their fetched columns +/// with ``TableRecord/databaseSelection-7iphs``. +/// - ``SQLRequest`` and its support for [SQL Interpolation](https://github.com/groue/GRDB.swift/blob/master/Documentation/SQLInterpolation.md). +/// - ``FetchRequest/adapted(_:)`` for building a request that embeds the +/// row adapters. +/// +/// ```swift +/// struct Player: TableRecord, FetchableRecord { ... } +/// struct Team: TableRecord, FetchableRecord { ... } +/// +/// struct PlayerInfo { +/// var player: Player +/// var team: Team? +/// } +/// +/// extension PlayerInfo: FetchableRecord { +/// init(row: Row) { +/// player = row["player"] +/// team = row["team"] +/// } +/// } +/// +/// extension PlayerInfo { +/// /// The request for the player info, given a player id +/// static func filter(playerId: Int64) -> some FetchRequest { +/// // Build SQL request with SQL interpolation +/// let request: SQLRequest = """ +/// SELECT +/// \(columnsOf: Player.self), -- Instead of player.* +/// \(columnsOf: Team.self), -- Instead of team.* +/// LEFT JOIN team ON team.id = player.teamId +/// WHERE player.id = \(playerId) +/// """ +/// +/// // Returns an adapted request that defines the player and team +/// // scopes in the fetched row +/// return request.adapted { db in +/// let adapters = try splittingRowAdapters(columnCounts: [ +/// Player.numberOfSelectedColumns(db), +/// Team.numberOfSelectedColumns(db), +/// ]) +/// return ScopeAdapter([ +/// "player": adapters[0], +/// "team": adapters[1], +/// ]) +/// } +/// } +/// } +/// +/// // Usage +/// try dbQueue.read { db in +/// if let playerInfo = try PlayerInfo.filter(playerId: 1).fetchOne(db) { +/// print(playerInfo.player) // Player(id: 1, name: "Arthur", teamId: 42) +/// print(playerInfo.team) // Team(id: 42, name: "Reds") +/// } +/// } +/// ``` +/// +/// - parameter columnCounts: An array of row chunk lengths. +/// - returns: An array of row adapters that split a row into as many chunks +/// as the number of elements in `columnCounts`, plus one (the row adapter +/// for all columns that remain on the right of the last chunk). public func splittingRowAdapters(columnCounts: [Int]) -> [any RowAdapter] { - guard !columnCounts.isEmpty else { + if columnCounts.isEmpty { // Identity adapter return [SuffixRowAdapter(fromIndex: 0)] } @@ -149,25 +258,57 @@ extension Statement: _RowLayout { /// A type that helps two incompatible row interfaces working together. /// -/// You provide row adapters to methods that fetch. For example: +/// Row adapters present database rows in the way expected by the +/// row consumers. +/// +/// For example, when a row consumer expects a column named "consumed", but +/// the raw row has a column named "produced", the ``ColumnMapping`` row +/// adapter comes in handy: /// /// ```swift -/// try dbQueue.read { -/// // An adapter that ignores the first two columns -/// let adapter = SuffixRowAdapter(fromIndex: 2) -/// let sql = "SELECT 1 AS foo, 2 AS bar, 3 AS baz" +/// // Feeds the "consumed" column from "produced": +/// let adapter = ColumnMapping(["consumed": "produced"]) +/// let sql = "SELECT 'Hello' AS produced" +/// let row = try Row.fetchOne(db, sql: sql, adapter: adapter)! /// -/// // [baz:3] -/// try Row.fetchOne(db, sql: sql, adapter: adapter) -/// } +/// // [consumed:"Hello"] +/// print(row) +/// +/// // "Hello" +/// print(row["consumed"]) /// ``` /// +/// The raw fetched columns are not lost (see ``Row/unadapted``): +/// +/// ```swift +/// // â–¿ [consumed:"Hello"] +/// // unadapted: [produced:"Hello"] +/// print(row.debugDescription) +/// +/// // [produced:"Hello"] +/// print(row.unadapted) +/// ``` +/// +/// There are several situations where row adapters are useful. Among them: +/// +/// - Adapters help disambiguate columns with identical names, which may +/// happen when you select columns from several tables. +/// See ``splittingRowAdapters(columnCounts:)`` for some sample code. +/// +/// - Adapters help when SQLite outputs unexpected column names, which may +/// happen with some subqueries. See ``RenameColumnAdapter`` for +/// an example. +/// /// ## Topics /// -/// ### Splitting a Row +/// ### Splitting a Row into Chunks /// /// - ``splittingRowAdapters(columnCounts:)`` /// +/// ### Adding Scopes to an Adapter +/// +/// - ``addingScopes(_:)`` +/// /// ### Built-in Adapters /// /// - ``ColumnMapping`` @@ -222,9 +363,22 @@ extension RowAdapter { } } -/// EmptyRowAdapter is a row adapter that hides all columns. +/// `EmptyRowAdapter` is a row adapter that hides all columns. +/// +/// For example: +/// +/// ```swift +/// let adapter = EmptyRowAdapter() +/// let sql = "SELECT 0 AS a, 1 AS b, 2 AS c" +/// +/// let row = try Row.fetchOne(db, sql: sql, adapter: adapter)! +/// row.isEmpty // true +/// ``` +/// +/// This limit adapter may turn out useful in some narrow use cases. You'll +/// be happy to find it when you need it. public struct EmptyRowAdapter: RowAdapter { - /// Creates an EmptyRowAdapter + /// Creates an `EmptyRowAdapter`. public init() { } public func _layoutedAdapter(from layout: some _RowLayout) throws -> any _LayoutedRowAdapter { @@ -232,19 +386,28 @@ public struct EmptyRowAdapter: RowAdapter { } } -/// ColumnMapping is a row adapter that maps column names. +/// `ColumnMapping` is a row adapter that maps column names. /// -/// let adapter = ColumnMapping(["foo": "bar"]) -/// let sql = "SELECT 'foo' AS foo, 'bar' AS bar, 'baz' AS baz" +/// Build a `ColumnMapping` with a dictionary whose keys +/// are adapted column names, and values the column names in the base row: /// -/// // [foo:"bar"] -/// try Row.fetchOne(db, sql: sql, adapter: adapter) +/// ```swift +/// // Feeds "newA" from "a", and "newB" from "b": +/// let adapter = ColumnMapping(["newA": "a", "newB": "b"]) +/// let sql = "SELECT 0 AS a, 1 AS b, 2 AS c" +/// +/// // [newA:0, newB:1] +/// let row = try Row.fetchOne(db, sql: sql, adapter: adapter)! +/// ``` +/// +/// Note that columns that are not present in the dictionary are not present +/// in the resulting adapted row. public struct ColumnMapping: RowAdapter { /// A dictionary from mapped column names to column names in a base row. let mapping: [String: String] - /// Creates a ColumnMapping with a dictionary from mapped column names to - /// column names in a base row. + /// Creates a `ColumnMapping` with a dictionary from mapped column names + /// to column names in a base row. public init(_ mapping: [String: String]) { self.mapping = mapping } @@ -269,13 +432,17 @@ public struct ColumnMapping: RowAdapter { } } -/// SuffixRowAdapter is a row adapter that hides the first columns in a row. +/// `SuffixRowAdapter` hides the leftmost columns in a row. /// -/// let adapter = SuffixRowAdapter(fromIndex: 2) -/// let sql = "SELECT 1 AS foo, 2 AS bar, 3 AS baz" +/// For example: /// -/// // [baz:3] -/// try Row.fetchOne(db, sql: sql, adapter: adapter) +/// ```swift +/// let adapter = SuffixRowAdapter(fromIndex: 2) +/// let sql = "SELECT 0 AS a, 1 AS b, 2 AS c, 3 AS d" +/// +/// // [c:2, d: 3] +/// try Row.fetchOne(db, sql: sql, adapter: adapter)! +/// ``` public struct SuffixRowAdapter: RowAdapter { /// The suffix index let index: Int @@ -294,13 +461,17 @@ public struct SuffixRowAdapter: RowAdapter { } } -/// RangeRowAdapter is a row adapter that only exposes a range of columns. +/// `RangeRowAdapter` is a row adapter that only exposes a range of columns. /// -/// let adapter = RangeRowAdapter(1..<3) -/// let sql = "SELECT 1 AS foo, 2 AS bar, 3 AS baz, 4 as qux" +/// For example: /// -/// // [bar:2 baz:3] -/// try Row.fetchOne(db, sql: sql, adapter: adapter) +/// ```swift +/// let adapter = RangeRowAdapter(1..<3) +/// let sql = "SELECT 0 AS a, 1 AS b, 2 AS c, 3 AS d" +/// +/// // [b:1 c:2] +/// try Row.fetchOne(db, sql: sql, adapter: adapter) +/// ``` public struct RangeRowAdapter: RowAdapter { /// The range let range: CountableRange @@ -322,28 +493,65 @@ public struct RangeRowAdapter: RowAdapter { } } -/// `ScopeAdapter` is a row adapter that lets you define scopes on rows. +/// `ScopeAdapter` is a row adapter that defines row scopes. /// -/// // Two adapters -/// let fooAdapter = ColumnMapping(["value": "foo"]) -/// let barAdapter = ColumnMapping(["value": "bar"]) +/// `ScopeAdapter` does not change the columns and values of the fetched +/// row. Instead, it defines *scopes* based on other adapter, which you +/// access through the ``Row/scopes`` property of the fetched rows. /// -/// // Define scopes -/// let adapter = ScopeAdapter([ -/// "foo": fooAdapter, -/// "bar": barAdapter]) +/// For example: /// -/// // Fetch -/// let sql = "SELECT 'foo' AS foo, 'bar' AS bar" -/// let row = try Row.fetchOne(db, sql: sql, adapter: adapter)! +/// ```swift +/// let adapter = ScopeAdapter([ +/// "left": RangeRowAdapter(0..<2), +/// "right": RangeRowAdapter(2..<4)]) +/// let sql = "SELECT 0 AS a, 1 AS b, 2 AS c, 3 AS d" /// -/// // Scoped rows: -/// if let fooRow = row.scopes["foo"] { -/// fooRow["value"] // "foo" -/// } -/// if let barRow = row.scopes["bar"] { -/// barRow["value"] // "bar" -/// } +/// let row = try Row.fetchOne(db, sql: sql, adapter: adapter)! +/// +/// row // [a:0 b:1 c:2 d:3] +/// row.scopes["left"] // [a:0 b:1] +/// row.scopes["right"] // [c:2 d:3] +/// row.scopes["missing"] // nil +/// ``` +/// +/// Scopes can be nested: +/// +/// ```swift +/// let adapter = ScopeAdapter([ +/// "left": ScopeAdapter([ +/// "left": RangeRowAdapter(0..<1), +/// "right": RangeRowAdapter(1..<2)]), +/// "right": ScopeAdapter([ +/// "left": RangeRowAdapter(2..<3), +/// "right": RangeRowAdapter(3..<4)]) +/// ]) +/// let sql = "SELECT 0 AS a, 1 AS b, 2 AS c, 3 AS d" +/// let row = try Row.fetchOne(db, sql: sql, adapter: adapter)! +/// +/// let leftRow = row.scopes["left"]! +/// leftRow.scopes["left"] // [a:0] +/// leftRow.scopes["right"] // [b:1] +/// +/// let rightRow = row.scopes["right"]! +/// rightRow.scopes["left"] // [c:2] +/// rightRow.scopes["right"] // [d:3] +/// ``` +/// +/// Any adapter can be extended with scopes, with +/// ``RowAdapter/addingScopes(_:)``: +/// +/// ```swift +/// let baseAdapter = RangeRowAdapter(0..<2) +/// let adapter = baseAdapter.addingScopes([ +/// "remainder": SuffixRowAdapter(fromIndex: 2) +/// ]) +/// let sql = "SELECT 0 AS a, 1 AS b, 2 AS c, 3 AS d" +/// let row = try Row.fetchOne(db, sql: sql, adapter: adapter)! +/// +/// row // [a:0 b:1] +/// row.scopes["remainder"] // [c:2 d:3] +/// ``` public struct ScopeAdapter: RowAdapter { /// The base adapter @@ -423,11 +631,29 @@ struct ChainedAdapter: RowAdapter { /// /// For example: /// -/// let adapter = RenameColumnAdapter { $0 + "rrr" } -/// let sql = "SELECT 'foo' AS foo, 'bar' AS bar, 'baz' AS baz" +/// ```swift +/// let adapter = RenameColumnAdapter { column in column + "rrr" } +/// let sql = "SELECT 0 AS a, 1 AS b, 2 AS c" /// -/// // [foorrr:"foo", barrrr:"bar", bazrrr:"baz"] -/// try Row.fetchOne(db, sql: sql, adapter: adapter) +/// // [arrr:0, brrr:1, crrr:2] +/// let row = try Row.fetchOne(db, sql: sql, adapter: adapter)! +/// ``` +/// +/// This adapter is useful when subqueries contain duplicated column names: +/// +/// ```swift +/// let sql = "SELECT * FROM (SELECT 1 AS id, 2 AS id)" +/// +/// // Prints ["id", "id:1"] +/// // Note the "id:1" column, generated by SQLite. +/// let row = try Row.fetchOne(db, sql: sql)! +/// print(Array(row.columnNames)) +/// +/// // Drop the `:...` suffix, and prints ["id", "id"] +/// let adapter = RenameColumnAdapter { String($0.prefix(while: { $0 != ":" })) } +/// let adaptedRow = try Row.fetchOne(db, sql: sql, adapter: adapter)! +/// print(Array(adaptedRow.columnNames)) +/// ``` public struct RenameColumnAdapter: RowAdapter { let transform: (String) -> String diff --git a/README.md b/README.md index c41b2f938..6ff0c8c60 100644 --- a/README.md +++ b/README.md @@ -342,7 +342,6 @@ Documentation - [Migrations]: Transform your database as your application evolves. - [Full-Text Search]: Perform efficient and customizable full-text searches. -- [Joined Queries Support](#joined-queries-support): Consume complex joined queries. - [Database Observation]: Observe database changes and transactions. - [Encryption](#encryption): Encrypt your database with SQLCipher. - [Backup](#backup): Dump the content of a database to another. @@ -471,7 +470,7 @@ Advanced topics: - [Prepared Statements] - [Custom SQL Functions and Aggregates](#custom-sql-functions-and-aggregates) - [Database Schema Introspection](#database-schema-introspection) -- [Row Adapters](#row-adapters) +- [Row Adapters](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/rowadapter) - [Raw SQLite Pointers](#raw-sqlite-pointers) @@ -1653,191 +1652,6 @@ Database.isGRDBInternalTable(...) For more information, see [`tableExists(_:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/database/tableexists(_:)) and related methods. -## Row Adapters - -**Row adapters let you present database rows in the way expected by the row consumers.** - -They basically help two incompatible row interfaces to work together. For example, a row consumer expects a column named "consumed", but the produced row has a column named "produced". - -In this case, the `ColumnMapping` row adapter comes in handy: - -```swift -// Turn the 'produced' column into 'consumed': -let adapter = ColumnMapping(["consumed": "produced"]) -let row = try Row.fetchOne(db, sql: "SELECT 'Hello' AS produced", adapter: adapter)! - -// [consumed:"Hello"] -print(row) - -// "Hello" -print(row["consumed"]) - -// â–¿ [consumed:"Hello"] -// unadapted: [produced:"Hello"] -print(row.debugDescription) - -// [produced:"Hello"] -print(row.unadapted) -``` - -[Record types](#records) are typical row consumers that expect database rows to have a specific layout so that they can decode them: - -```swift -struct MyRecord: Decodable, FetchableRecord { - var consumed: String -} -let record = try MyRecord.fetchOne(db, sql: "SELECT 'Hello' AS produced", adapter: adapter)! -print(record.consumed) // "Hello" -``` - -There are several situations where row adapters are useful: - -- They help disambiguate columns with identical names, which may happen when you select columns from several tables. See [Joined Queries Support](#joined-queries-support) for an example. - -- They help when SQLite outputs unexpected column names, which may happen with some subqueries. See [RenameColumnAdapter](#renamecolumnadapter) for an example. - -Available row adapters are described below. - -- [ColumnMapping](#columnmapping) -- [EmptyRowAdapter](#emptyrowadapter) -- [RangeRowAdapter](#rangerowadapter) -- [RenameColumnAdapter](#renamecolumnadapter) -- [ScopeAdapter](#scopeadapter) -- [SuffixRowAdapter](#suffixrowadapter) - - -### ColumnMapping - -`ColumnMapping` renames columns. Build one with a dictionary whose keys are adapted column names, and values the column names in the raw row: - -```swift -// [newA:0, newB:1] -let adapter = ColumnMapping(["newA": "a", "newB": "b"]) -let row = try Row.fetchOne(db, sql: "SELECT 0 AS a, 1 AS b, 2 AS c", adapter: adapter)! -``` - -Note that columns that are not present in the dictionary are not present in the resulting adapted row. - - -### EmptyRowAdapter - -`EmptyRowAdapter` hides all columns. - -```swift -let adapter = EmptyRowAdapter() -let row = try Row.fetchOne(db, sql: "SELECT 0 AS a, 1 AS b, 2 AS c", adapter: adapter)! -row.isEmpty // true -``` - -This limit adapter may turn out useful in some narrow use cases. You'll be happy to find it when you need it. - - -### RangeRowAdapter - -`RangeRowAdapter` only exposes a range of columns. - -```swift -// [b:1] -let adapter = RangeRowAdapter(1..<2) -let row = try Row.fetchOne(db, sql: "SELECT 0 AS a, 1 AS b, 2 AS c", adapter: adapter)! -``` - - -### RenameColumnAdapter - -`RenameColumnAdapter` lets you transform column names with a function: - -```swift -// [arrr:0, brrr:1, crrr:2] -let adapter = RenameColumnAdapter { column in column + "rrr" } -let row = try Row.fetchOne(db, sql: "SELECT 0 AS a, 1 AS b, 2 AS c", adapter: adapter)! -``` - -This adapter may turn out useful, for example, when subqueries contain duplicated column names: - -```swift -let sql = "SELECT * FROM (SELECT 1 AS id, 2 AS id)" - -// Prints ["id", "id:1"] -// Note the "id:1" column, generated by SQLite. -let row = try Row.fetchOne(db, sql: sql)! -print(Array(row.columnNames)) - -// Drop the `:...` suffix, and prints ["id", "id"] -let adapter = RenameColumnAdapter { String($0.prefix(while: { $0 != ":" })) } -let adaptedRow = try Row.fetchOne(db, sql: sql, adapter: adapter)! -print(Array(adaptedRow.columnNames)) -``` - - -### ScopeAdapter - -`ScopeAdapter` defines *row scopes*: - -```swift -let adapter = ScopeAdapter([ - "left": RangeRowAdapter(0..<2), - "right": RangeRowAdapter(2..<4)]) -let row = try Row.fetchOne(db, sql: "SELECT 0 AS a, 1 AS b, 2 AS c, 3 AS d", adapter: adapter)! -``` - -ScopeAdapter does not change the columns and values of the fetched row. Instead, it defines *scopes*, which you access through the `Row.scopes` property: - -```swift -row // [a:0 b:1 c:2 d:3] -row.scopes["left"] // [a:0 b:1] -row.scopes["right"] // [c:2 d:3] -row.scopes["missing"] // nil -``` - -Scopes can be nested: - -```swift -let adapter = ScopeAdapter([ - "left": ScopeAdapter([ - "left": RangeRowAdapter(0..<1), - "right": RangeRowAdapter(1..<2)]), - "right": ScopeAdapter([ - "left": RangeRowAdapter(2..<3), - "right": RangeRowAdapter(3..<4)]) - ]) -let row = try Row.fetchOne(db, sql: "SELECT 0 AS a, 1 AS b, 2 AS c, 3 AS d", adapter: adapter)! - -let leftRow = row.scopes["left"]! -leftRow.scopes["left"] // [a:0] -leftRow.scopes["right"] // [b:1] - -let rightRow = row.scopes["right"]! -rightRow.scopes["left"] // [c:2] -rightRow.scopes["right"] // [d:3] -``` - -Any adapter can be extended with scopes: - -```swift -let baseAdapter = RangeRowAdapter(0..<2) -let adapter = ScopeAdapter(base: baseAdapter, scopes: [ - "remainder": SuffixRowAdapter(fromIndex: 2)]) -let row = try Row.fetchOne(db, sql: "SELECT 0 AS a, 1 AS b, 2 AS c, 3 AS d", adapter: adapter)! - -row // [a:0 b:1] -row.scopes["remainder"] // [c:2 d:3] -``` - -To see how `ScopeAdapter` can be used, see [Joined Queries Support](#joined-queries-support). - - -### SuffixRowAdapter - -`SuffixRowAdapter` hides the first columns in a row: - -```swift -// [b:1 c:2] -let adapter = SuffixRowAdapter(fromIndex: 1) -let row = try Row.fetchOne(db, sql: "SELECT 0 AS a, 1 AS b, 2 AS c", adapter: adapter)! -``` - - ## Raw SQLite Pointers **If not all SQLite APIs are exposed in GRDB, you can still use the [SQLite C Interface](https://www.sqlite.org/c3ref/intro.html) and call [SQLite C functions](https://www.sqlite.org/c3ref/funclist.html).** @@ -5020,7 +4834,7 @@ try Player.customRequest().fetchAll(db) // [Player] } ``` -- The `asRequest(of:)` method changes the type fetched by the request. It is useful, for example, when you use [Associations]: +- The [`asRequest(of:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/queryinterfacerequest/asrequest(of:)) method changes the type fetched by the request. It is useful, for example, when you use [Associations]: ```swift struct BookInfo: FetchableRecord, Decodable { @@ -5036,311 +4850,7 @@ try Player.customRequest().fetchAll(db) // [Player] try request.fetchAll(db) ``` -- The `adapted(_:)` method eases the consumption of complex rows with [row adapters](#row-adapters). See [Joined Queries Support](#joined-queries-support) for some sample code that uses this method. - - -## Joined Queries Support - -**GRDB helps consuming joined queries with complex selection.** - -In this chapter, we will focus on the extraction of information from complex rows, such as the ones fetched by the query below: - -```sql --- How to consume the left, middle, and right parts of those rows? -SELECT player.*, team.*, MAX(round.score) AS maxScore -FROM player -LEFT JOIN team ON ... -LEFT JOIN round ON ... -GROUP BY ... -``` - -We will not talk about the *generation* of joined queries, which is covered in [Associations]. - -**So what are we talking about?** - -It is difficult to consume rows fetched from complex joined queries, because they often contain several columns with the same name: `id` from table `player`, `id` from table `team`, etc. - -When such ambiguity happens, GRDB row accessors always favor the leftmost matching column. This means that `row["id"]` would give a player id, without any obvious way to access the team id. - -A classical technique to avoid this ambiguity is to give each column a unique name. For example: - -```sql --- A classical technique -SELECT player.id AS player_id, player.name AS player_name, team.id AS team_id, team.name AS team_name, team.color AS team_color, MAX(round.score) AS maxScore -FROM player -LEFT JOIN team ON ... -LEFT JOIN round ON ... -GROUP BY ... -``` - -This technique works pretty well, but it has three drawbacks: - -1. The selection becomes hard to read and understand. -2. Such queries are difficult to write by hand. -3. The mangled names are a *very* bad fit for [FetchableRecord] types that expect specific column names. After all, if the `Team` record type can read `SELECT * FROM team ...`, it should be able to read `SELECT ..., team.*, ...` as well. - -We thus need another technique. **Below we'll see how to split rows into slices, and preserve column names.** - -`SELECT player.*, team.*, MAX(round.score) AS maxScore FROM ...` will be split into three slices: one that contains player's columns, one that contains team's columns, and a remaining slice that contains remaining column(s). The Player record type will be able to read the first slice, which contains the columns expected by the `Player.init(row:)` initializer. In the same way, the Team record type could read the second slice. - -Unlike the name-mangling technique, splitting rows keeps SQL legible, accepts your hand-crafted SQL queries, and plays as nicely as possible with your existing [record types](#records). - -- [Splitting Rows, an Introduction](#splitting-rows-an-introduction) -- [Splitting Rows, the Record Way](#splitting-rows-the-record-way) -- [Splitting Rows, the Codable Way](#splitting-rows-the-codable-way) - - -### Splitting Rows, an Introduction - -Let's first write some introductory code, hoping that this chapter will make you understand how pieces fall together. We'll see [later](#splitting-rows-the-record-way) how records will help us streamline the initial approach, how to track changes in joined requests, and how we can use the standard Decodable protocol. - -To split rows, we will use [row adapters](#row-adapters). Row adapters adapt rows so that row consumers see exactly the columns they want. Among other things, row adapters can define several *row scopes* that give access to as many *row slices*. Sounds like a perfect match. - -At the very beginning, there is an SQL query: - -```swift -try dbQueue.read { db in - let sql = """ - SELECT player.*, team.*, MAX(round.score) AS maxScore - FROM player - LEFT JOIN team ON ... - LEFT JOIN round ON ... - GROUP BY ... - """ -``` - -We need an adapter that extracts player columns, in a slice that has as many columns as there are columns in the player table. That's [RangeRowAdapter](#rangerowadapter): - -```swift - // SELECT player.*, team.*, ... - // <------> - let playerWidth = try db.columns(in: "player").count - let playerAdapter = RangeRowAdapter(0 ..< playerWidth) -``` - -We also need an adapter that extracts team columns: - -```swift - // SELECT player.*, team.*, ... - // <----> - let teamWidth = try db.columns(in: "team").count - let teamAdapter = RangeRowAdapter(playerWidth ..< (playerWidth + teamWidth)) -``` - -We merge those two adapters in a single [ScopeAdapter](#scopeadapter) that will allow us to access both sliced rows: - -```swift - let playerScope = "player" - let teamScope = "team" - let adapter = ScopeAdapter([ - playerScope: playerAdapter, - teamScope: teamAdapter]) -``` - -And now we can fetch, and start consuming our rows. You already know [row cursors](#fetching-rows): - -```swift - let rows = try Row.fetchCursor(db, sql: sql, adapter: adapter) - while let row = try rows.next() { -``` - -From a fetched row, we can build a player: - -```swift - let player: Player = row[playerScope] -``` - -In the SQL query, the team is joined with the `LEFT JOIN` operator. This means that the team may be missing: its slice may contain team values, or it may only contain NULLs. When this happens, we don't want to build a Team record, and we thus load an *optional* Team: - -```swift - let team: Team? = row[teamScope] -``` - -And finally, we can load the maximum score, assuming that the "maxScore" column is not ambiguous: - -```swift - let maxScore: Int = row["maxScore"] - - print("player: \(player)") - print("team: \(team)") - print("maxScore: \(maxScore)") - } -} -``` - -> :bulb: In this chapter, we have learned: -> -> - how to use `RangeRowAdapter` to extract a specific table's columns into a *row slice*. -> - how to use `ScopeAdapter` to gives access to several row slices through named scopes. -> - how to use Row subscripting to extract records from rows, or optional records in order to deal with left joins. - - -### Splitting Rows, the Record Way - -Our introduction above has introduced important techniques. It uses [row adapters](#row-adapters) in order to split rows. It uses Row subscripting in order to extract records from row slices. - -But we may want to make it more usable and robust: - -1. It's generally easier to consume records than raw rows. -2. Joined records not always need all columns from a table (see `TableRecord.databaseSelection` in [Columns Selected by a Request]). -3. Building row adapters is long and error prone. - -To address the first bullet, let's define a record that holds our player, optional team, and maximum score. Since it can decode database rows, it adopts the [FetchableRecord] protocol: - -```swift -struct PlayerInfo { - var player: Player - var team: Team? - var maxScore: Int -} - -/// PlayerInfo can decode rows: -extension PlayerInfo: FetchableRecord { - private enum Scopes { - static let player = "player" - static let team = "team" - } - - init(row: Row) { - player = row[Scopes.player] - team = row[Scopes.team] - maxScore = row["maxScore"] - } -} -``` - -Now we write a method that returns a [custom request](#custom-requests), and then build the fetching method on top of that request: - -```swift -extension PlayerInfo { - /// The request for all player infos - static func all() -> some FetchRequest { -``` - -To acknowledge that both Player and Team records may customize their selection of the "player" and "team" columns, we'll write our SQL in a slightly different way: - -```swift - // Let Player and Team customize their selection: - let request: SQLRequest = """ - SELECT - \(columnsOf: Player.self), -- instead of player.* - \(columnsOf: Team.self), -- instead of team.* - MAX(round.score) AS maxScore - FROM player - LEFT JOIN team ON ... - LEFT JOIN round ON ... - GROUP BY ... - """ -``` - -Our SQL is no longer a regular String, but an `SQLRequest` which profits from [SQL Interpolation]. Inside this request, `\(columnsOf: Player.self)` outputs `player.*`, unless Player defines a [customized selection](#columns-selected-by-a-request). - -Now we need to build adapters. - -We use the `splittingRowAdapters` global function, whose job is precisely to build row adapters of desired widths: - -And since counting table columns require a database connection, we use the `adapted(_:)` request method. It allows requests to adapt themselves right before execution, when a database connection is available. - -```swift - return request.adapted { db in - let adapters = try splittingRowAdapters(columnCounts: [ - Player.numberOfSelectedColumns(db), - Team.numberOfSelectedColumns(db)]) - return ScopeAdapter([ - Scopes.player: adapters[0], - Scopes.team: adapters[1]]) - } - } -``` - -> **Note**: `splittingRowAdapters` returns as many adapters as necessary to fully split a row. In the example above, it returns *three* adapters: one for player, one for team, and one for the remaining columns. - -And finally, we can define the fetching method: - -```swift - /// Fetches all player infos - static func fetchAll(_ db: Database) throws -> [PlayerInfo] { - try all().fetchAll(db) - } -} -``` - -And when your app needs to fetch player infos, it now reads: - -```swift -// Fetch player infos -let playerInfos = try dbQueue.read { db in - try PlayerInfo.fetchAll(db) -} -``` - - -> :bulb: In this chapter, we have learned: -> -> - how to define a `FetchableRecord` record that consumes rows fetched from a joined query. -> - how to use [SQL Interpolation] and `numberOfSelectedColumns` in order to deal with nested record types that define custom selection. -> - how to use `splittingRowAdapters` in order to streamline the definition of row slices. -> - how to gather all relevant methods and constants in a record type, fully responsible of its relationship with the database. - - -### Splitting Rows, the Codable Way - -[Codable Records] build on top of the standard Decodable protocol in order to decode database rows. - -You can consume complex joined queries with Codable records as well. As a demonstration, we'll rewrite the [above](#splitting-rows-the-record-way) sample code: - -```swift -struct Player: Decodable, FetchableRecord, TableRecord { - var id: Int64 - var name: String -} -struct Team: Decodable, FetchableRecord, TableRecord { - var id: Int64 - var name: String - var color: Color -} -struct PlayerInfo: Decodable, FetchableRecord { - var player: Player - var team: Team? - var maxScore: Int -} - -extension PlayerInfo { - /// The request for all player infos - static func all() -> some FetchRequest { - let request: SQLRequest = """ - SELECT - \(columnsOf: Player.self), - \(columnsOf: Team.self), - MAX(round.score) AS maxScore - FROM player - LEFT JOIN team ON ... - LEFT JOIN round ON ... - GROUP BY ... - """ - return request.adapted { db in - let adapters = try splittingRowAdapters(columnCounts: [ - Player.numberOfSelectedColumns(db), - Team.numberOfSelectedColumns(db)]) - return ScopeAdapter([ - CodingKeys.player.stringValue: adapters[0], - CodingKeys.team.stringValue: adapters[1]]) - } - } - - /// Fetches all player infos - static func fetchAll(_ db: Database) throws -> [PlayerInfo] { - try all().fetchAll(db) - } -} - -// Fetch player infos -let playerInfos = try dbQueue.read { db in - try PlayerInfo.fetchAll(db) -} -``` - -> :bulb: In this chapter, we have learned how to use the `Decodable` protocol and its associated `CodingKeys` enum in order to dry up our code. +- The [`adapted(_:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/fetchrequest/adapted(_:)) method eases the consumption of complex rows with row adapters. See [`RowAdapter`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/rowadapter) and [`splittingRowAdapters(columnCounts:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/splittingrowadapters(columncounts:)) for a sample code that uses `adapted(_:)`. Encryption @@ -6752,6 +6262,10 @@ This chapter has [moved](Documentation/FullTextSearch.md). This chapter has [moved](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/concurrency). +#### Joined Queries Support + +This chapter was replaced with the documentation of [splittingRowAdapters(columnCounts:)](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/splittingrowadapters(columncounts:)). + #### Migrations This chapter has [moved](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/migrations). @@ -6772,6 +6286,10 @@ This error was renamed to [RecordError]. This chapter has [moved](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/statement). +#### Row Adapters + +This chapter has [moved](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/rowadapter). + #### RowConvertible Protocol This protocol has been renamed [FetchableRecord] in GRDB 3.0. From 397a008237211d6e5cdbc03d8ce5b288734a8d25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 12 Mar 2023 18:01:24 +0100 Subject: [PATCH 5/7] More links to DocC reference --- README.md | 60 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 6ff0c8c60..5fbd9f277 100644 --- a/README.md +++ b/README.md @@ -1912,6 +1912,8 @@ Details follow: ## FetchableRecord Protocol +📖 [`FetchableRecord`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/fetchablerecord) + **The FetchableRecord protocol grants fetching methods to any type** that can be built from a database row: ```swift @@ -1990,6 +1992,8 @@ See [fetching methods](#fetching-methods) for information about the `fetchCursor ## TableRecord Protocol +📖 [`TableRecord`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/tablerecord) + **The TableRecord protocol** generates SQL for you. To use TableRecord, subclass the [Record](#record-class) class, or adopt it explicitly: ```swift @@ -2046,6 +2050,8 @@ TableRecord can also fetch deal with primary and unique keys: see [Fetching by K ## PersistableRecord Protocol +📖 [`EncodableRecord`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/encodablerecord), [`MutablePersistableRecord`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/mutablepersistablerecord), [`PersistableRecord`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/persistablerecord) + **GRDB record types can create, update, and delete rows in the database.** Those abilities are granted by three protocols: @@ -2526,7 +2532,7 @@ struct Player: Identifiable, FetchableRecord, PersistableRecord { } ``` -When `id` has a [database-compatible type](#values) (Int64, Int, String, UUID, ...), the `Identifiable` conformance unlocks type-safe record and request methods: +When `id` has a [database-compatible type](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasevalueconvertible) (Int64, Int, String, UUID, ...), the `Identifiable` conformance unlocks type-safe record and request methods: ```swift let player = try Player.find(db, id: 1) // Player @@ -3648,6 +3654,8 @@ So don't miss the [SQL API](#sqlite-api). ## Requests +📖 [`QueryInterfaceRequest`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/queryinterfacerequest), [`Table`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/table) + **The query interface requests** let you fetch values from the database: ```swift @@ -3702,7 +3710,7 @@ enum Columns: String, ColumnExpression { You can now build requests with the following methods: `all`, `none`, `select`, `distinct`, `filter`, `matching`, `group`, `having`, `order`, `reversed`, `limit`, `joining`, `including`, `with`. All those methods return another request, which you can further refine by applying another method: `Player.select(...).filter(...).order(...)`. -- `all()`, `none()`: the requests for all rows, or no row. +- [`all()`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/tablerecord/all()), [`none()`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/tablerecord/none()): the requests for all rows, or no row. ```swift // SELECT * FROM player @@ -3711,21 +3719,21 @@ You can now build requests with the following methods: `all`, `none`, `select`, By default, all columns are selected. See [Columns Selected by a Request]. -- `select(...)` and `select(..., as:)` define the selected columns. See [Columns Selected by a Request]. +- [`select(...)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/selectionrequest/select(_:)-30yzl) and [`select(..., as:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/queryinterfacerequest/select(_:as:)-282xc) define the selected columns. See [Columns Selected by a Request]. ```swift // SELECT name FROM player Player.select(nameColumn, as: String.self) ``` -- `annotated(with: expression...)` extends the selection. +- [`annotated(with: expression...)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/selectionrequest/annotated(with:)-6ehs4) extends the selection. ```swift // SELECT *, (score + bonus) AS total FROM player Player.annotated(with: (scoreColumn + bonusColumn).forKey("total")) ``` -- `annotated(with: aggregate)` extends the selection with [association aggregates](Documentation/AssociationsBasics.md#association-aggregates). +- [`annotated(with: aggregate)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/derivablerequest/annotated(with:)-74xfs) extends the selection with [association aggregates](Documentation/AssociationsBasics.md#association-aggregates). ```swift // SELECT team.*, COUNT(DISTINCT player.id) AS playerCount @@ -3735,7 +3743,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, Team.annotated(with: Team.players.count) ``` -- `annotated(withRequired: association)` and `annotated(withOptional: association)` extends the selection with [Associations]. +- [`annotated(withRequired: association)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/joinablerequest/annotated(withrequired:)) and [`annotated(withOptional: association)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/joinablerequest/annotated(withoptional:)) extends the selection with [Associations]. ```swift // SELECT player.*, team.color @@ -3744,14 +3752,14 @@ You can now build requests with the following methods: `all`, `none`, `select`, Player.annotated(withRequired: Player.team.select(colorColumn)) ``` -- `distinct()` performs uniquing. +- [`distinct()`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/derivablerequest/distinct()) performs uniquing. ```swift // SELECT DISTINCT name FROM player Player.select(nameColumn, as: String.self).distinct() ``` -- `filter(expression)` applies conditions. +- [`filter(expression)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/filteredrequest/filter(_:)) applies conditions. ```swift // SELECT * FROM player WHERE id IN (1, 2, 3) @@ -3761,7 +3769,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, Player.filter(nameColumn != nil && heightColumn > 1.75) ``` -- `filter(id:)` and `filter(ids:)` are type-safe methods available on [Identifiable Records]: +- [`filter(id:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/tablerequest/filter(id:)) and [`filter(ids:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/tablerequest/filter(ids:)) are type-safe methods available on [Identifiable Records]: ```swift // SELECT * FROM player WHERE id = 1 @@ -3771,7 +3779,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, Country.filter(ids: ["FR", "US"]) ``` -- `filter(key:)` and `filter(keys:)` apply conditions on primary and unique keys: +- [`filter(key:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/tablerequest/filter(key:)-1p9sq) and [`filter(keys:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/tablerequest/filter(keys:)-6ggt1) apply conditions on primary and unique keys: ```swift // SELECT * FROM player WHERE id = 1 @@ -3787,7 +3795,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, Player.filter(key: ["email": "arthur@example.com"]) ``` -- `matching(pattern)` performs [full-text search](Documentation/FullTextSearch.md). +- `matching(pattern)` ([FTS3](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/tablerequest/matching(_:)-3s3zr), [FTS5](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/tablerequest/matching(_:)-7c1e8)) performs [full-text search](Documentation/FullTextSearch.md). ```swift // SELECT * FROM document WHERE document MATCH 'sqlite database' @@ -3797,7 +3805,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, When the pattern is nil, no row will match. -- `group(expression, ...)` groups rows. +- [`group(expression, ...)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/aggregatingrequest/group(_:)-edak) groups rows. ```swift // SELECT name, MAX(score) FROM player GROUP BY name @@ -3806,7 +3814,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, .group(nameColumn) ``` -- `having(expression)` applies conditions on grouped rows. +- [`having(expression)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/aggregatingrequest/having(_:)) applies conditions on grouped rows. ```swift // SELECT team, MAX(score) FROM player GROUP BY team HAVING MIN(score) >= 1000 @@ -3816,7 +3824,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, .having(min(scoreColumn) >= 1000) ``` -- `having(aggregate)` applies conditions on grouped rows, according to an [association aggregate](Documentation/AssociationsBasics.md#association-aggregates). +- [`having(aggregate)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/derivablerequest/having(_:)) applies conditions on grouped rows, according to an [association aggregate](Documentation/AssociationsBasics.md#association-aggregates). ```swift // SELECT team.* @@ -3827,7 +3835,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, Team.having(Team.players.count >= 5) ``` -- `order(ordering, ...)` sorts. +- [`order(ordering, ...)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/orderedrequest/order(_:)-63rzl) sorts. ```swift // SELECT * FROM player ORDER BY name @@ -3851,7 +3859,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, Player.order(scoreColumn).order(nameColumn) ``` -- `reversed()` reverses the eventual orderings. +- [`reversed()`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/orderedrequest/reversed()) reverses the eventual orderings. ```swift // SELECT * FROM player ORDER BY score ASC, name DESC @@ -3865,7 +3873,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, Player.all().reversed() ``` -- `limit(limit, offset: offset)` limits and pages results. +- [`limit(limit, offset: offset)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/queryinterfacerequest/limit(_:offset:)) limits and pages results. ```swift // SELECT * FROM player LIMIT 5 @@ -3875,7 +3883,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, Player.limit(5, offset: 10) ``` -- `joining(...)` and `including(...)` fetch and join records through [Associations]. +- [`joining(required:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/joinablerequest/joining(required:)), [`joining(optional:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/joinablerequest/joining(optional:)), [`including(required:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/joinablerequest/including(required:)), [`including(optional:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/joinablerequest/including(optional:)), and [`including(all:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/joinablerequest/including(all:)) fetch and join records through [Associations]. ```swift // SELECT player.*, team.* @@ -3884,7 +3892,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, Player.including(required: Player.team) ``` -- `with(cte)` embeds a [common table expression]: +- [`with(cte)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/derivablerequest/with(_:)) embeds a [common table expression]: ```swift // WITH ... SELECT * FROM player @@ -3894,7 +3902,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, - Other requests that involve the primary key: - - `selectPrimaryKey(as:)` selects the primary key. + - [`selectPrimaryKey(as:)`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/queryinterfacerequest/selectprimarykey(as:)) selects the primary key. ```swift // SELECT id FROM player @@ -3907,7 +3915,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, Citizenship.selectPrimaryKey(as: Row.self) // QueryInterfaceRequest ``` - - `orderByPrimaryKey()` sorts by primary key. + - [`orderByPrimaryKey()`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/tablerequest/orderbyprimarykey()) sorts by primary key. ```swift // SELECT * FROM player ORDER BY id @@ -3920,7 +3928,7 @@ You can now build requests with the following methods: `all`, `none`, `select`, Citizenship.orderByPrimaryKey() ``` - - `groupByPrimaryKey()` groups rows by primary key. + - [`groupByPrimaryKey()`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/tablerequest/groupbyprimarykey()) groups rows by primary key. You can refine requests by chaining those methods: @@ -4011,6 +4019,8 @@ Feed [requests](#requests) with SQL expressions built from your Swift code: ### SQL Operators +📖 [`SQLSpecificExpressible`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/sqlspecificexpressible) + GRDB comes with a Swift version of many SQLite [built-in operators](https://sqlite.org/lang_expr.html#operators), listed below. But not all: see [Embedding SQL in Query Interface Requests] for a way to add support for missing SQL operators. - `=`, `<>`, `<`, `<=`, `>`, `>=`, `IS`, `IS NOT` @@ -4270,6 +4280,8 @@ GRDB comes with a Swift version of many SQLite [built-in operators](https://sqli ### SQL Functions +📖 [`SQLSpecificExpressible`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/sqlspecificexpressible) + GRDB comes with a Swift version of many SQLite [built-in functions](https://sqlite.org/lang_corefunc.html), listed below. But not all: see [Embedding SQL in Query Interface Requests] for a way to add support for missing SQL functions. - `ABS`, `AVG`, `COUNT`, `DATETIME`, `JULIANDAY`, `LENGTH`, `MAX`, `MIN`, `SUM`, `TOTAL`: @@ -5273,6 +5285,8 @@ Considering that a local database is not some JSON loaded from a remote server, ### DatabaseError +📖 [`DatabaseError`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databaseerror) + **DatabaseError** are thrown on SQLite errors: ```swift @@ -5345,6 +5359,8 @@ Each DatabaseError has two codes: an `extendedResultCode` (see [extended result ### RecordError +📖 [`RecordError`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/recorderror) + **RecordError** is thrown by the [PersistableRecord] protocol when the `update` method could not find any row to update: ```swift From 840fa3b5da8de437d756ed55c8fc575188f162c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 12 Mar 2023 18:14:38 +0100 Subject: [PATCH 6/7] RecordError DocC --- GRDB/Record/TableRecord.swift | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/GRDB/Record/TableRecord.swift b/GRDB/Record/TableRecord.swift index 58f33e0e0..4e8d8b1b4 100644 --- a/GRDB/Record/TableRecord.swift +++ b/GRDB/Record/TableRecord.swift @@ -669,6 +669,31 @@ extension TableRecord { // MARK: - RecordError /// A record error. +/// +/// `RecordError` is thrown by ``MutablePersistableRecord`` types when an +/// `update` method could not find any row to update: +/// +/// ```swift +/// do { +/// try player.update(db) +/// } catch let RecordError.recordNotFound(databaseTableName: table, key: key) { +/// print("Key \(key) was not found in table \(table).") +/// } +/// ``` +/// +/// `RecordError` is also thrown by ``FetchableRecord`` types when a +/// `find` method does not find any record: +/// +/// ```swift +/// do { +/// let player = try Player.find(db, id: 42) +/// } catch let RecordError.recordNotFound(databaseTableName: table, key: key) { +/// print("Key \(key) was not found in table \(table).") +/// } +/// ``` +/// +/// You can create `RecordError` instances with the +/// ``TableRecord/recordNotFound(_:id:)`` method and its variants. public enum RecordError: Error { /// A record does not exist in the database. /// From ad33dfcfbfd344ce24dd06296417700ed9720c82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 12 Mar 2023 18:17:48 +0100 Subject: [PATCH 7/7] v6.9.1 --- CHANGELOG.md | 6 ++++-- GRDB.swift.podspec | 2 +- README.md | 4 ++-- Support/Info.plist | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55f6f76e5..cdac1e1cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: #### 6.x Releases -- `6.8.x` Releases - [6.9.0](#690) +- `6.8.x` Releases - [6.9.0](#690) - [6.9.1](#691) - `6.8.x` Releases - [6.8.0](#680) - `6.7.x` Releases - [6.7.0](#670) - `6.6.x` Releases - [6.6.0](#660) - [6.6.1](#661) @@ -107,7 +107,9 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: --- -## Next Version +## 6.9.1 + +Released March 12, 2023 • [diff](https://github.com/groue/GRDB.swift/compare/v6.8.0...v6.9.1) - **Documentation Updates**: Moved more README chapters into DocC: - [`RowAdapter`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/rowadapter) diff --git a/GRDB.swift.podspec b/GRDB.swift.podspec index 1b3120f8c..a10d5233e 100644 --- a/GRDB.swift.podspec +++ b/GRDB.swift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'GRDB.swift' - s.version = '6.9.0' + s.version = '6.9.1' s.license = { :type => 'MIT', :file => 'LICENSE' } s.summary = 'A toolkit for SQLite databases, with a focus on application development.' diff --git a/README.md b/README.md index 5fbd9f277..8db474960 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,13 @@ --- -**Latest release**: March 12, 2023 • [version 6.9.0](https://github.com/groue/GRDB.swift/tree/v6.9.0) • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 5 to GRDB 6](Documentation/GRDB6MigrationGuide.md) +**Latest release**: March 12, 2023 • [version 6.9.1](https://github.com/groue/GRDB.swift/tree/v6.9.1) • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 5 to GRDB 6](Documentation/GRDB6MigrationGuide.md) **Requirements**: iOS 11.0+ / macOS 10.13+ / tvOS 11.0+ / watchOS 4.0+ • SQLite 3.19.3+ • Swift 5.7+ / Xcode 14+ | Swift version | GRDB version | | -------------- | ----------------------------------------------------------- | -| **Swift 5.7+** | **v6.9.0** | +| **Swift 5.7+** | **v6.9.1** | | Swift 5.3 | [v5.26.1](https://github.com/groue/GRDB.swift/tree/v5.26.1) | | Swift 5.2 | [v5.12.0](https://github.com/groue/GRDB.swift/tree/v5.12.0) | | Swift 5.1 | [v4.14.0](https://github.com/groue/GRDB.swift/tree/v4.14.0) | diff --git a/Support/Info.plist b/Support/Info.plist index 6bcdf1c06..7ea5ae46c 100644 --- a/Support/Info.plist +++ b/Support/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 6.9.0 + 6.9.1 CFBundleSignature ???? CFBundleVersion