From 22ef3c8b2a6ac6f661c4e4bc9fefc1d5024dcc51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 7 Oct 2016 17:44:38 +0200 Subject: [PATCH] Persistable DAO assumes "rowid" as a default primary key, so that a record that exposes "rowid" in its persistentDictionary can be updated, and deleted. --- GRDB/Record/Persistable.swift | 21 +++++++++++---------- GRDB/Record/Record.swift | 2 +- TODO.md | 1 + 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/GRDB/Record/Persistable.swift b/GRDB/Record/Persistable.swift index ad65ec509..69249645d 100644 --- a/GRDB/Record/Persistable.swift +++ b/GRDB/Record/Persistable.swift @@ -300,10 +300,11 @@ public extension MutablePersistable { fileprivate func canUpdate(_ db: Database) -> Bool { // Fail early if database table does not exist. let databaseTableName = type(of: self).databaseTableName - guard let primaryKey = try! db.primaryKey(databaseTableName) else { - return false - } + // Update according to explicit primary key, or implicit rowid. + let primaryKey = try! db.primaryKey(databaseTableName) ?? PrimaryKeyInfo.rowID("rowid") + + // We need a primary key value in the persistentDictionary let persistentDictionary = self.persistentDictionary for column in primaryKey.columns where !databaseValue(for: column, inDictionary: persistentDictionary).isNull { return true @@ -324,7 +325,7 @@ public extension MutablePersistable { try dao.insertStatement(onConflict: conflictResolutionForInsert).execute() if !conflictResolutionForInsert.invalidatesLastInsertedRowID { - didInsert(with: db.lastInsertedRowID, for: dao.primaryKey?.rowIDColumn) + didInsert(with: db.lastInsertedRowID, for: dao.primaryKey.rowIDColumn) } } @@ -533,7 +534,7 @@ public extension Persistable { try dao.insertStatement(onConflict: conflictResolutionForInsert).execute() if !conflictResolutionForInsert.invalidatesLastInsertedRowID { - didInsert(with: db.lastInsertedRowID, for: dao.primaryKey?.rowIDColumn) + didInsert(with: db.lastInsertedRowID, for: dao.primaryKey.rowIDColumn) } } @@ -588,12 +589,12 @@ final class DAO { let databaseTableName: String /// The table primary key - let primaryKey: PrimaryKeyInfo? + let primaryKey: PrimaryKeyInfo init(_ db: Database, _ record: MutablePersistable) { // Fail early if database table does not exist. let databaseTableName = type(of: record).databaseTableName - let primaryKey = try! db.primaryKey(databaseTableName) + let primaryKey = try! db.primaryKey(databaseTableName) ?? PrimaryKeyInfo.rowID("rowid") // Fail early if persistentDictionary is empty let persistentDictionary = record.persistentDictionary @@ -619,7 +620,7 @@ final class DAO { /// Returns nil if and only if primary key is nil func updateStatement(columns: Set, onConflict: Database.ConflictResolution) -> UpdateStatement? { // Fail early if primary key does not resolve to a database row. - let primaryKeyColumns = primaryKey?.columns ?? [] + let primaryKeyColumns = primaryKey.columns let primaryKeyValues = databaseValues(for: primaryKeyColumns, inDictionary: persistentDictionary) guard primaryKeyValues.contains(where: { !$0.isNull }) else { return nil } @@ -661,7 +662,7 @@ final class DAO { /// Returns nil if and only if primary key is nil func deleteStatement() -> UpdateStatement? { // Fail early if primary key does not resolve to a database row. - let primaryKeyColumns = primaryKey?.columns ?? [] + let primaryKeyColumns = primaryKey.columns let primaryKeyValues = databaseValues(for: primaryKeyColumns, inDictionary: persistentDictionary) guard primaryKeyValues.contains(where: { !$0.isNull }) else { return nil } @@ -676,7 +677,7 @@ final class DAO { /// Returns nil if and only if primary key is nil func existsStatement() -> SelectStatement? { // Fail early if primary key does not resolve to a database row. - let primaryKeyColumns = primaryKey?.columns ?? [] + let primaryKeyColumns = primaryKey.columns let primaryKeyValues = databaseValues(for: primaryKeyColumns, inDictionary: persistentDictionary) guard primaryKeyValues.contains(where: { !$0.isNull }) else { return nil } diff --git a/GRDB/Record/Record.swift b/GRDB/Record/Record.swift index 0ba1141ce..a37400b16 100644 --- a/GRDB/Record/Record.swift +++ b/GRDB/Record/Record.swift @@ -222,7 +222,7 @@ open class Record : RowConvertible, TableMapping, Persistable { if !conflictResolutionForInsert.invalidatesLastInsertedRowID { let rowID = db.lastInsertedRowID - let rowIDColumn = dao.primaryKey?.rowIDColumn + let rowIDColumn = dao.primaryKey.rowIDColumn didInsert(with: rowID, for: rowIDColumn) // Update persistentDictionary with inserted id, so that we can diff --git a/TODO.md b/TODO.md index b3674b969..1398689d9 100644 --- a/TODO.md +++ b/TODO.md @@ -1,3 +1,4 @@ +- [ ] FTS: The didInsert(with:for:) column used to be nil when the rowid column was hidden. This has changed when the Persistable DAO started assuming a "rowid" primary key, in order to let records wrapping FTS3/4/5 tables "know" their primary key, and support updates and deletion. The problem is that the optional column argument of didInsert(with:for:) is never nil now, and the the "rowid" name may be already used by another column. Think about it. - [ ] FTS: deal with hidden docid. Select docid, * from..., didInsertWith... etc. - [ ] FTS: check https://www.sqlite.org/spellfix1.html - [ ] Store dates as timestamp (https://twitter.com/gloparco/status/780948021613912064, https://github.com/groue/GRDB.swift/issues/97)