Merge pull request #1461 from francois-random/expose-total-changes

expose total_changes()
This commit is contained in:
gwenn 2024-02-28 21:18:18 +01:00 committed by GitHub
commit def8e9460d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 0 deletions

View File

@ -288,6 +288,18 @@ impl InnerConnection {
}
}
#[inline]
pub fn total_changes(&self) -> u64 {
#[cfg(not(feature = "modern_sqlite"))]
unsafe {
ffi::sqlite3_total_changes(self.db()) as u64
}
#[cfg(feature = "modern_sqlite")] // 3.37.0
unsafe {
ffi::sqlite3_total_changes64(self.db()) as u64
}
}
#[inline]
pub fn is_autocommit(&self) -> bool {
unsafe { ffi::sqlite3_get_autocommit(self.db()) != 0 }

View File

@ -1005,6 +1005,16 @@ impl Connection {
self.db.borrow().changes()
}
/// Return the total number of rows modified, inserted or deleted by all
/// completed INSERT, UPDATE or DELETE statements since the database
/// connection was opened, including those executed as part of trigger programs.
///
/// See <https://www.sqlite.org/c3ref/total_changes.html>
#[inline]
pub fn total_changes(&self) -> u64 {
self.db.borrow().total_changes()
}
/// Test for auto-commit mode.
/// Autocommit mode is on by default.
#[inline]
@ -1693,6 +1703,29 @@ mod test {
Ok(())
}
#[test]
fn test_total_changes() -> Result<()> {
let db = Connection::open_in_memory()?;
let sql = "CREATE TABLE foo(x INTEGER PRIMARY KEY, value TEXT default '' NOT NULL,
desc TEXT default '');
CREATE VIEW foo_bar AS SELECT x, desc FROM foo WHERE value = 'bar';
CREATE TRIGGER INSERT_FOOBAR
INSTEAD OF INSERT
ON foo_bar
BEGIN
INSERT INTO foo VALUES(new.x, 'bar', new.desc);
END;";
db.execute_batch(sql)?;
let total_changes_before = db.total_changes();
let changes = db
.prepare("INSERT INTO foo_bar VALUES(null, 'baz');")?
.execute([])?;
let total_changes_after = db.total_changes();
assert_eq!(changes, 0);
assert_eq!(total_changes_after - total_changes_before, 1);
Ok(())
}
#[test]
fn test_is_autocommit() -> Result<()> {
let db = Connection::open_in_memory()?;