From ba272061b763c925678dcaed193fdeabdcfc224c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 8 Aug 2016 13:54:02 +0200 Subject: [PATCH 1/4] Documentation --- README.md | 87 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 73766e64f..c4c287717 100644 --- a/README.md +++ b/README.md @@ -174,10 +174,13 @@ Documentation - [SQLite API](#sqlite-api): The low-level SQLite API • [executing updates](#executing-updates) • [fetch queries](#fetch-queries) -**Application Tools** +**Records and the Query Interface** - [Records](#records): Fetching and persistence methods for your custom structs and class hierarchies. - [Query Interface](#the-query-interface): A swift way to generate SQL • [table creation](#database-schema) • [fetch requests](#requests) + +**Application Tools** + - [Migrations](#migrations): Transform your database as your application evolves. - [Database Changes Observation](#database-changes-observation): Perform post-commit and post-rollback actions. - [FetchedRecordsController](#fetchedrecordscontroller): Automatic database changes tracking, plus UITableView animations. @@ -1562,21 +1565,8 @@ Before jumping in the low-level wagon, here is a reminder of most SQLite APIs us - [sqlite3_set_authorizer](https://www.sqlite.org/c3ref/set_authorizer.html) -Application Tools -================= - -On top of the SQLite API described above, GRDB provides a toolkit for applications. While none of those are mandatory, all of them help dealing with the database: - -- [Records](#records): Fetching and persistence methods for your custom structs and class hierarchies. -- [Query Interface](#the-query-interface): A swift way to generate SQL. -- [Migrations](#migrations): Transform your database as your application evolves. -- [Database Changes Observation](#database-changes-observation): Perform post-commit and post-rollback actions. -- [FetchedRecordsController](#fetchedrecordscontroller): Automatic database changes tracking, plus UITableView animations. -- [Encryption](#encryption): Encrypt your database with SQLCipher. -- [Backup](#backup): Dump the content of a database to another. - - -## Records +Records +======= **On top of the [SQLite API](#sqlite-api), GRDB provides protocols and a class** that help manipulating database rows as regular objects named "records": @@ -1592,7 +1582,7 @@ Your custom structs and classes can adopt each protocol individually, and opt in > :point_up: **Note**: if you are familiar with Core Data's NSManagedObject or Realm's Object, you may experience a cultural shock: GRDB records are not uniqued, and do not auto-update. This is both a purpose, and a consequence of protocol-oriented programming. You should read [How to build an iOS application with SQLite and GRDB.swift](https://medium.com/@gwendal.roue/how-to-build-an-ios-application-with-sqlite-and-grdb-swift-d023a06c29b3) for a general introduction. -#### Inserting Records +### Inserting Records To insert a record in the database, subclass the [Record](#record-class) class or adopt the [Persistable](#persistable-protocol) protocol, and call the `insert` method: @@ -1606,7 +1596,7 @@ try person.insert(db) Of course, you need to open a [database connection](#database-connections), and [create a database table](#database-schema) first. -#### Fetching Records +### Fetching Records [Record](#record-class) subclasses and types that adopt the [RowConvertible](#rowconvertible-protocol) protocol can be fetched from the database: @@ -1627,7 +1617,7 @@ let countries = Country.fetchAll(db, keys: ["FR", "US"]) To learn more about querying records, check the [query interface](#the-query-interface). -#### Updating Records +### Updating Records [Record](#record-class) subclasses and types that adopt the [Persistable](#persistable-protocol) protocol can be updated in the database: @@ -1654,7 +1644,7 @@ try db.execute("UPDATE persons SET synchronized = 1") ``` -#### Deleting Records +### Deleting Records [Record](#record-class) subclasses and types that adopt the [Persistable](#persistable-protocol) protocol can be deleted from the database: @@ -1678,7 +1668,7 @@ try db.execute("DELETE FROM persons") ``` -#### Counting Records +### Counting Records [Record](#record-class) subclasses and types that adopt the [TableMapping](#tablemapping-protocol) protocol can be counted: @@ -1696,7 +1686,7 @@ You can now jump to: - [The Query Interface](#the-query-interface) -### RowConvertible Protocol +## RowConvertible Protocol **The RowConvertible protocol grants fetching methods to any type** that can be built from a database row: @@ -1746,7 +1736,7 @@ PointOfInterest.fetchOne(db, "SELECT ...", arguments:...) // PointOfInterest? See [fetching methods](#fetching-methods) for information about the `fetch`, `fetchAll` and `fetchOne` methods. See [fetching rows](#fetching-rows) for more information about the query arguments. -#### RowConvertible and Row Adapters +### RowConvertible and Row Adapters RowConvertible types usually consume rows by column name: @@ -1765,7 +1755,7 @@ extension PointOfInterest : RowConvertible { Occasionnally, you'll want to write a complex SQL query that uses different column names. In this case, [row adapters](#row-adapters) are there to help you mapping raw column names to the names expected by your RowConvertible types. -### TableMapping Protocol +## TableMapping Protocol **Adopt the TableMapping protocol** on top of [RowConvertible](#rowconvertible-protocol), and you are granted with the full [query interface](#the-query-interface). @@ -1823,7 +1813,7 @@ Person.fetchOne(db, key: ["name": "Arthur"]) // fatal error: table persons has n ``` -### Persistable Protocol +## Persistable Protocol **GRDB provides two protocols that let adopting types store themselves in the database:** @@ -1890,7 +1880,7 @@ paris.id // some value ``` -#### Persistence Methods +### Persistence Methods [Record](#record-class) subclasses and types that adopt [Persistable](#persistable-protocol) are given default implementations for methods that insert, update, and delete: @@ -1920,7 +1910,7 @@ pointOfInterest.exists(db) // Bool **All primary keys are supported**, including primary keys that span several columns. -#### Customizing the Persistence Methods +### Customizing the Persistence Methods Your custom type may want to perform extra work when the persistence methods are invoked. @@ -1970,7 +1960,7 @@ struct Link : Persistable { > :point_up: **Note**: it is recommended that you do not implement your own version of the `save` method. Its default implementation forwards the job to `update` or `insert`: these are the methods that may need customization, not `save`. -### Record Class +## Record Class **Record** is a class that is designed to be subclassed, and provides the full GRDB Record toolkit in one go: @@ -2076,7 +2066,7 @@ try poi.delete(db) ``` -#### Changes Tracking +### Changes Tracking **The [Record](#record-class) class provides changes tracking.** @@ -2111,7 +2101,8 @@ person.persistentChangedValues // ["age": 35] For an efficient algorithm which synchronizes the content of a database table with a JSON payload, check [JSONSynchronization.playground](Playgrounds/JSONSynchronization.playground/Contents.swift). -## The Query Interface +The Query Interface +=================== **The query interface lets you write pure Swift instead of SQL:** @@ -2141,7 +2132,7 @@ So don't miss the [SQL API](#sqlite-api). - [Fetching Aggregated Values](#fetching-aggregated-values) -### Database Schema +## Database Schema Once granted with a [database connection](#database-connections), you can setup your database schema without writing SQL: @@ -2151,7 +2142,7 @@ Once granted with a [database connection](#database-connections), you can setup - [Create Indexes](#create-indexes) -#### Create Tables +### Create Tables ```swift // CREATE TABLE pointOfInterests ( @@ -2251,7 +2242,7 @@ Other **table constraints** can involve several columns: } ``` -#### Modify Tables +### Modify Tables SQLite lets you rename tables, and add columns to existing tables: @@ -2268,7 +2259,7 @@ try db.alter(table: "persons") { t in > :point_up: **Note**: SQLite restricts the possible table alterations, and may require you to recreate dependent triggers or views. See the documentation of the [ALTER TABLE](https://www.sqlite.org/lang_altertable.html) for details. See [Advanced Database Schema Changes](#advanced-database-schema-changes) for a way to lift restrictions. -#### Drop Tables +### Drop Tables Drop tables with the `drop(table:)` method: @@ -2276,7 +2267,7 @@ Drop tables with the `drop(table:)` method: try db.drop(table: "obsolete") ``` -#### Create Indexes +### Create Indexes Create indexes with the `create(index:)` method: @@ -2292,7 +2283,7 @@ Relevant SQLite documentation: - [Partial Indexes](https://www.sqlite.org/partialindex.html) -### Requests +## Requests **The query interface requests** let you fetch values from the database: @@ -2442,12 +2433,12 @@ Person ``` -### Expressions +## Expressions Feed [requests](#requests) with SQL expressions built from your Swift code: -#### SQL Operators +### SQL Operators - `=`, `<>`, `<`, `<=`, `>`, `>=`, `IS`, `IS NOT` @@ -2540,7 +2531,7 @@ Feed [requests](#requests) with SQL expressions built from your Swift code: ``` -#### SQL Functions +### SQL Functions - `ABS`, `AVG`, `COUNT`, `LENGTH`, `MAX`, `MIN`, `SUM`: @@ -2593,7 +2584,7 @@ Feed [requests](#requests) with SQL expressions built from your Swift code: ``` -### Fetching from Requests +## Fetching from Requests Once you have a request, you can fetch the records at the origin of the request: @@ -2632,7 +2623,7 @@ let maxHeight = row.value(atIndex: 1) as Double? ``` -### Fetching By Primary Key +## Fetching By Primary Key **Fetching records according to their primary key** is a very common task. It has a shortcut which accepts any single-column primary key: @@ -2664,7 +2655,7 @@ Person.fetchOne(db, key: ["email": "arthur@example.com"]) // Person? ``` -### Fetching Aggregated Values +## Fetching Aggregated Values **Requests can count.** The `fetchCount()` method returns the number of rows that would be returned by a fetch request: @@ -2696,6 +2687,18 @@ let maxHeight = row.value(atIndex: 1) as Double? ``` +Application Tools +================= + +On top of the APIs described above, GRDB provides a toolkit for applications. While none of those are mandatory, all of them help dealing with the database: + +- [Migrations](#migrations): Transform your database as your application evolves. +- [Database Changes Observation](#database-changes-observation): Perform post-commit and post-rollback actions. +- [FetchedRecordsController](#fetchedrecordscontroller): Automatic database changes tracking, plus UITableView animations. +- [Encryption](#encryption): Encrypt your database with SQLCipher. +- [Backup](#backup): Dump the content of a database to another. + + ## Migrations **Migrations** are a convenient way to alter your database schema over time in a consistent and easy way. From b522abffb59ed95d9da9a9929ba54cb8277b6bac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 8 Aug 2016 21:24:01 +0200 Subject: [PATCH 2/4] TODO.md --- TODO.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.md b/TODO.md index 2f88f77bf..20d68f5bf 100644 --- a/TODO.md +++ b/TODO.md @@ -1,3 +1,4 @@ +- [ ] Record.exists(db) should return false when primary key is nil - [ ] Fatal Error when MutablePersistable.update(_:columns:) is given columns that do not exist in the database. - [ ] What is the behavior inTransaction and inSavepoint behaviors in case of commit error? Code looks like we do not rollback, leaving the app in a weird state (out of Swift transaction block with a SQLite transaction that may still be opened). - [ ] GRDBCipher: remove limitations on iOS or OS X versions From 2a47e5f32c05344b1398d2ca7f416cb84ba879f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 8 Aug 2016 21:28:15 +0200 Subject: [PATCH 3/4] TODO.md --- TODO.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index 20d68f5bf..0e6964352 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,3 @@ -- [ ] Record.exists(db) should return false when primary key is nil - [ ] Fatal Error when MutablePersistable.update(_:columns:) is given columns that do not exist in the database. - [ ] What is the behavior inTransaction and inSavepoint behaviors in case of commit error? Code looks like we do not rollback, leaving the app in a weird state (out of Swift transaction block with a SQLite transaction that may still be opened). - [ ] GRDBCipher: remove limitations on iOS or OS X versions @@ -21,6 +20,7 @@ Not sure +- [ ] Record.exists(db) should return false when primary key is nil - [X] Have Row adopt LiteralDictionaryConvertible - [ ] ... allowing non unique column names - [ ] Remove DatabaseValue.value() From 36d7bf77e35f693c6612fbd6a39637c7a2531409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 9 Aug 2016 09:07:45 +0200 Subject: [PATCH 4/4] Documentation --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c4c287717..61782fe20 100644 --- a/README.md +++ b/README.md @@ -3395,7 +3395,7 @@ They uncover programmer errors, false assumptions, and prevent misuses. Here are Row.fetchAll(db, "SELECT * FROM boooks") ``` - Solution: fix the SQL query. + Solution: fix the SQL query: ```swift Row.fetchAll(db, "SELECT * FROM books") @@ -3403,7 +3403,7 @@ They uncover programmer errors, false assumptions, and prevent misuses. Here are If you do have to run untrusted SQL queries, jump to [untrusted databases](#how-to-deal-with-untrusted-inputs). -- The code asks for a non-optional values, when the database contains NULL: +- The code asks for a non-optional value, when the database contains NULL: ```swift // fatal error: could not convert NULL to String.