Change the way we count changes in order to support INSTEAD OF triggers

Database.changesCount calls sqlite3_changes(), whose documentation says:

> https://sqlite.org/c3ref/changes.html
> Changes to a view that are intercepted by INSTEAD OF triggers are not counted.

Since we want to support INSTEAD OF triggers well, so that record types can be based on views, we switch to sqlite3_total_changes().
This commit is contained in:
Gwendal Roué authored and Gwendal Roué committed 2025-09-23 17:12:27 +02:00
1 parent 38f5226e95
commit c7bbebc80f
3 files changed
+9 -4

No files matched your search

@@ -635,8 +635,9 @@ extension QueryInterfaceRequest {
@discardableResult
public func deleteAll(_ db: Database) throws -> Int {
let statement = try SQLQueryGenerator(relation: relation).makeDeleteStatement(db)
let prevCount = db.totalChangesCount
try statement.execute()
return db.changesCount
return db.totalChangesCount - prevCount
}
}
@@ -1135,8 +1136,9 @@ extension QueryInterfaceRequest {
// database not hit
return 0
}
let prevCount = db.totalChangesCount
try updateStatement.execute()
return db.changesCount
return db.totalChangesCount - prevCount
}
/// Updates matching rows, and returns the number of updated rows.
@@ -53,7 +53,8 @@ extension MutablePersistableRecord {
// Nil primary key
return false
}
let prevCount = db.totalChangesCount
try statement.execute()
return db.changesCount > 0
return (db.totalChangesCount - prevCount) > 0
}
}
@@ -956,8 +956,10 @@ extension MutablePersistableRecord {
// Nil primary key
try dao.recordNotFound()
}
let prevCount = db.totalChangesCount
let returned = try fetch(statement)
if db.changesCount == 0 {
let changesCount = db.totalChangesCount - prevCount
if changesCount == 0 {
// No row was updated
try dao.recordNotFound()
}