From 51d948fc29f3b9c3a984ae97fac1b06e27388bca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 19 Jul 2016 17:29:26 +0200 Subject: [PATCH 1/4] TODO.md --- TODO.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.md b/TODO.md index 235f8a2a3..31cc9bd94 100644 --- a/TODO.md +++ b/TODO.md @@ -43,3 +43,4 @@ Reading list: - https://www.zetetic.net/sqlcipher/ - https://sqlite.org/sharedcache.html - Amazing tip from Xcode labs: add a EXCLUDED_SOURCE_FILE_NAMES build setting to conditionally exclude sources for different configuration: https://twitter.com/zats/status/74386298602026496 +- SQLITE_ENABLE_SQLLOG: http://mjtsai.com/blog/2016/07/19/sqlite_enable_sqllog/ \ No newline at end of file From 315426a4b6caf80cd58781e8a2fa1a3327cd5d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 19 Jul 2016 23:40:17 +0200 Subject: [PATCH 2/4] TODO.md --- TODO.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.md b/TODO.md index 31cc9bd94..92337f829 100644 --- a/TODO.md +++ b/TODO.md @@ -1,3 +1,4 @@ +- [ ] Improve error message for invalid primary keys (see https://github.com/groue/GRDB.swift/issues/81) - [ ] GRDBCipher: remove limitations on iOS or OS X versions - [ ] FetchedRecordsController: take inspiration from https://github.com/jflinter/Dwifft - [ ] File protection: Read https://github.com/ccgus/fmdb/issues/262 and understand https://lists.apple.com/archives/cocoa-dev/2012/Aug/msg00527.html From 8b09b2cdba52536cf634873cf456d3878dd72957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 20 Jul 2016 13:49:16 +0200 Subject: [PATCH 3/4] Improve error messages for MutablePersistable.update(), delete() & exist() (see #81) --- GRDB/Record/Persistable.swift | 6 +++--- TODO.md | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/GRDB/Record/Persistable.swift b/GRDB/Record/Persistable.swift index 129703738..3a4a0e164 100644 --- a/GRDB/Record/Persistable.swift +++ b/GRDB/Record/Persistable.swift @@ -503,7 +503,7 @@ final class DataMapper { // Fail early if primary key does not resolve to a database row. let primaryKeyColumns = primaryKey?.columns ?? [] let primaryKeyValues = databaseValues(forColumns: primaryKeyColumns, inDictionary: persistentDictionary) - GRDBPrecondition(primaryKeyValues.contains { !$0.isNull }, "invalid primary key in \(persistable)") + GRDBPrecondition(primaryKeyValues.contains { !$0.isNull }, "record can not be identified. persistentDictionary must contain non-nil value(s) for the key(s) \(primaryKeyColumns.joinWithSeparator((", ")))") // Update everything but primary key var updatedColumns = persistentDictionary.keys.removingElementsOf(primaryKeyColumns) @@ -533,7 +533,7 @@ final class DataMapper { // Fail early if primary key does not resolve to a database row. let primaryKeyColumns = primaryKey?.columns ?? [] let primaryKeyValues = databaseValues(forColumns: primaryKeyColumns, inDictionary: persistentDictionary) - GRDBPrecondition(primaryKeyValues.contains { !$0.isNull }, "invalid primary key in \(persistable)") + GRDBPrecondition(primaryKeyValues.contains { !$0.isNull }, "record can not be identified. persistentDictionary must contain non-nil value(s) for the key(s) \(primaryKeyColumns.joinWithSeparator((", ")))") let query = DeleteQuery( tableName: databaseTableName, @@ -547,7 +547,7 @@ final class DataMapper { // Fail early if primary key does not resolve to a database row. let primaryKeyColumns = primaryKey?.columns ?? [] let primaryKeyValues = databaseValues(forColumns: primaryKeyColumns, inDictionary: persistentDictionary) - GRDBPrecondition(primaryKeyValues.contains { !$0.isNull }, "invalid primary key in \(persistable)") + GRDBPrecondition(primaryKeyValues.contains { !$0.isNull }, "record can not be identified. persistentDictionary must contain non-nil value(s) for the key(s) \(primaryKeyColumns.joinWithSeparator((", ")))") let query = ExistsQuery( tableName: databaseTableName, diff --git a/TODO.md b/TODO.md index 92337f829..31cc9bd94 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,3 @@ -- [ ] Improve error message for invalid primary keys (see https://github.com/groue/GRDB.swift/issues/81) - [ ] GRDBCipher: remove limitations on iOS or OS X versions - [ ] FetchedRecordsController: take inspiration from https://github.com/jflinter/Dwifft - [ ] File protection: Read https://github.com/ccgus/fmdb/issues/262 and understand https://lists.apple.com/archives/cocoa-dev/2012/Aug/msg00527.html From c3105fa66f78079d351b5b7220ea2a322031352d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 20 Jul 2016 14:00:25 +0200 Subject: [PATCH 4/4] Documentation --- README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 67cca36f2..7df602d5e 100644 --- a/README.md +++ b/README.md @@ -1584,6 +1584,8 @@ Add the [TableMapping](#tablemapping-protocol) protocol and you can stop writing ```swift let persons = Person.filter(emailColumn != nil).order(nameColumn).fetchAll(db) let person = Person.fetchOne(db, key: 1) +let person = Person.fetchOne(db, key: ["email": "arthur@example.com"]) +let countries = Country.fetchAll(db, keys: ["FR", "US"]) ``` To learn more about querying records, check the [query interface](#the-query-interface). @@ -1625,7 +1627,15 @@ let person = Person.fetchOne(db, key: 1)! try person.delete(db) ``` -For batch deletions, you have to execute an [SQL query](#executing-updates): +The [TableMapping](#tablemapping-protocol) protocol gives you methods that delete according to primary key or any unique index: + +```swift +try Person.deleteOne(db, key: 1) +try Person.deleteOne(db, key: ["email": "arthur@example.com"]) +try Country.deleteAll(db, keys: ["FR", "US"]) +``` + +Other deletions require an [SQL query](#executing-updates): ```swift try db.execute("DELETE FROM persons") @@ -1637,7 +1647,7 @@ try db.execute("DELETE FROM persons") [Record](#record-class) subclasses and types that adopt the [TableMapping](#tablemapping-protocol) protocol can be counted: ```swift -let personWithEmailCount = Person.filter(email != nil).fetchCount(db) // Int +let personWithEmailCount = Person.filter(emailColumn != nil).fetchCount(db) // Int ```