Persistable DAO assumes "rowid" as a default primary key, so that a record that exposes "rowid" in its persistentDictionary can be updated, and deleted.

This commit is contained in:
Gwendal Roué committed 2016-10-07 17:44:38 +02:00
1 parent 0936793b23
commit 22ef3c8b2a
3 files changed
+13 -11

No files matched your search

+11 -10
View File
@@ -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<String>, 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 }
+1 -1
View File
@@ -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
+1
View File
@@ -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)