diff --git a/README.md b/README.md index 0d2fecb26..afc278044 100644 --- a/README.md +++ b/README.md @@ -1923,14 +1923,12 @@ player.name = "Arthur" try player.update(db) ``` -Records can [track their changes](#changes-tracking), so that you can avoid useless updates: +It is possible to [avoid useless updates](#changes-tracking): ```swift let player = try Player.fetchOne(db, key: 1)! player.name = "Arthur" -if player.hasDatabaseChanges { - try player.update(db) -} +try player.updateChanges(db) ``` For batch updates, execute an [SQL query](#executing-updates): @@ -1957,7 +1955,7 @@ try Player.deleteOne(db, key: ["email": "arthur@example.com"]) try Country.deleteAll(db, keys: ["FR", "US"]) ``` -For batch deletes, see the [query interface](#the-query-interface): +For batch deletes, execute an [SQL query](#executing-updates), or see the [query interface](#the-query-interface): ```swift try Player.filter(emailColumn == nil).deleteAll(db) @@ -1993,25 +1991,14 @@ Details follow: - [RowConvertible](#rowconvertible-protocol) is able to **read**: it grants the ability to efficiently decode raw database row. - Imagine you want to load places from the `places` database table. - - One way to do it is to load raw database rows: + You can always decode rows yourself: ```swift - func fetchPlaceRows(_ db: Database) throws -> [Row] { - return try Row.fetchAll(db, "SELECT * FROM places") - } - ``` - - The problem is that [raw rows](#row-queries) are not easy to deal with, and you may prefer using a proper `Place` type: - - ```swift - // Dedicated model struct Place { ... } - func fetchPlaces(_ db: Database) throws -> [Place] { + try dbQueue.inDatabase { db in let rows = try Row.fetchAll(db, "SELECT * FROM places") - return rows.map { row in - Place( + let places: [Place] = rows.map { row in + return Place( id: row["id"], title: row["title"], coordinate: CLLocationCoordinate2D( @@ -2022,45 +2009,16 @@ Details follow: } ``` - This code is verbose, and so you define an `init(row:)` initializer: + But RowConvertible lets you write code that is much more readable, and much more efficient as well, both in terms of performance and memory usage: ```swift - // Row initializer - struct Place { - init(row: Row) { - id = row["id"] - ... - } - } - func fetchPlaces(_ db: Database) throws -> [Place] { - let rows = try Row.fetchAll(db, "SELECT * FROM places") - return rows.map { Place(row: $0) } + struct Place: FetchableRecord { ... } + try dbQueue.inDatabase { db in + let places = try Place.fetchAll(db, "SELECT * FROM places") } ``` - Now you notice that this code may use a lot of memory when you have many rows: a full array of database rows is created in order to build an array of places. Furthermore, rows that have been copied from the database have lost the ability to directly load values from SQLite: that's inefficient. You thus use a [database cursor](#cursors), which is both lazy and efficient: - - ```swift - // Cursor for efficiency - func fetchPlaces(_ db: Database) throws -> [Place] { - let rowCursor = try Row.fetchCursor(db, "SELECT * FROM places") - let placeCursor = rowCursor.map { Place(row: $0) } - return try Array(placeCursor) - } - ``` - - That's better. And that's what RowConvertible does, with a little performance bonus, and in a single line: - - ```swift - struct Place : RowConvertible { - init(row: Row) { ... } - } - func fetchPlaces(_ db: Database) throws -> [Place] { - return try Place.fetchAll(db, "SELECT * FROM places") - } - ``` - - RowConvertible is not able to build SQL requests, though. For that, you also need TableMapping: + RowConvertible is not able to build SQL requests for you, though. For that, you also need TableMapping: - [TableMapping](#tablemapping-protocol) is able to **build requests without SQL**: @@ -2089,6 +2047,8 @@ Details follow: try Place(...).insert(db) } ``` + + A persistable record can also [compare](#changes-tracking) itself against other records, and avoid useless database updates. ## RowConvertible Protocol