Merge 'bindings/rust: Return number of rows changed from Connection::execute()' from Rohith Suresh

Fixes #1904
This PR changes the existing behaviour of Connection.execute to not
return 0, but the number of rows that have been changed by the operation
within. The changes are:
1. Adds a getter for n_change and the execute function now returns the
n_change value
2. Integration test  to test the behaviour

Closes #1987
This commit is contained in:
Pekka Enberg
2025-07-20 09:49:51 +03:00
3 changed files with 87 additions and 1 deletions

View File

@@ -312,7 +312,9 @@ impl Statement {
return Ok(2);
}
Ok(turso_core::StepResult::Done) => {
return Ok(0);
let changes = stmt.n_change();
assert!(changes >= 0);
return Ok(changes as u64);
}
Ok(turso_core::StepResult::IO) => {
let _ = stmt.run_once();

View File

@@ -115,3 +115,83 @@ async fn test_cacheflush() {
fs::remove_file("test.db").await.unwrap();
fs::remove_file("test.db-wal").await.unwrap();
}
#[tokio::test]
async fn test_rows_returned() {
let db = Builder::new_local(":memory:").build().await.unwrap();
let conn = db.connect().unwrap();
//--- CRUD Operations ---//
conn.execute("CREATE TABLE t(id INTEGER PRIMARY KEY, val TEXT)", ())
.await
.unwrap();
let changed = conn
.execute("INSERT INTO t VALUES (1,'hello')", ())
.await
.unwrap();
let changed1 = conn
.execute("INSERT INTO t VALUES (2,'hi')", ())
.await
.unwrap();
let changed2 = conn
.execute("UPDATE t SET val='hi' WHERE id=1", ())
.await
.unwrap();
let changed3 = conn
.execute("DELETE FROM t WHERE val='hi'", ())
.await
.unwrap();
assert_eq!(changed, 1);
assert_eq!(changed1, 1);
assert_eq!(changed2, 1);
assert_eq!(changed3, 2);
//--- A more complicated example of insert with a select join subquery ---//
conn.execute(
"CREATE TABLE authors ( id INTEGER PRIMARY KEY, name TEXT NOT NULL);
",
(),
)
.await
.unwrap();
conn.execute(
"CREATE TABLE books ( id INTEGER PRIMARY KEY, author_id INTEGER NOT NULL REFERENCES authors(id), title TEXT NOT NULL); "
,()
).await.unwrap();
conn.execute(
"CREATE TABLE prize_winners ( book_id INTEGER PRIMARY KEY, author_name TEXT NOT NULL);",
(),
)
.await
.unwrap();
conn.execute(
"INSERT INTO authors (id, name) VALUES (1, 'Alice'), (2, 'Bob');",
(),
)
.await
.unwrap();
conn.execute(
"INSERT INTO books (id, author_id, title) VALUES (1, 1, 'Rust in Action'), (2, 1, 'Async Adventures'), (3, 1, 'Fearless Concurrency'), (4, 1, 'Unsafe Tales'), (5, 1, 'Zero-Cost Futures'), (6, 2, 'Learning SQL');",
()
).await.unwrap();
let rows_changed = conn
.execute(
"
INSERT INTO prize_winners (book_id, author_name)
SELECT b.id, a.name
FROM books b
JOIN authors a ON a.id = b.author_id
WHERE a.id = 1; -- Alices five books
",
(),
)
.await
.unwrap();
assert_eq!(rows_changed, 5);
}