From 2963ea723919532456d8745d306606ad5a661f55 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Tue, 19 Aug 2025 17:48:07 +0300 Subject: [PATCH] bindings/rust: Add `Connection::last_insert_rowid()` method Fixes #2670 --- bindings/rust/src/lib.rs | 6 ++++++ bindings/rust/tests/integration_tests.rs | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index 8dc335d9c..0cdeab99e 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -318,6 +318,12 @@ impl Connection { Ok(()) } + /// Returns the rowid of the last row inserted. + pub fn last_insert_rowid(&self) -> i64 { + let conn = self.inner.lock().unwrap(); + conn.last_insert_rowid() + } + /// Flush dirty pages to disk. /// This will write the dirty pages to the WAL. pub fn cacheflush(&self) -> Result<()> { diff --git a/bindings/rust/tests/integration_tests.rs b/bindings/rust/tests/integration_tests.rs index da7cb039b..a7e7dba4d 100644 --- a/bindings/rust/tests/integration_tests.rs +++ b/bindings/rust/tests/integration_tests.rs @@ -12,27 +12,32 @@ async fn test_rows_next() { conn.execute("INSERT INTO test (x) VALUES (1)", ()) .await .unwrap(); + assert_eq!(conn.last_insert_rowid(), 1); conn.execute("INSERT INTO test (x) VALUES (2)", ()) .await .unwrap(); + assert_eq!(conn.last_insert_rowid(), 2); conn.execute( "INSERT INTO test (x) VALUES (:x)", vec![(":x".to_string(), Value::Integer(3))], ) .await .unwrap(); + assert_eq!(conn.last_insert_rowid(), 3); conn.execute( "INSERT INTO test (x) VALUES (@x)", vec![("@x".to_string(), Value::Integer(4))], ) .await .unwrap(); + assert_eq!(conn.last_insert_rowid(), 4); conn.execute( "INSERT INTO test (x) VALUES ($x)", vec![("$x".to_string(), Value::Integer(5))], ) .await .unwrap(); + assert_eq!(conn.last_insert_rowid(), 5); let mut res = conn.query("SELECT * FROM test", ()).await.unwrap(); assert_eq!( res.next().await.unwrap().unwrap().get_value(0).unwrap(),