From 2481d73d704a9181abd2b3a48232bcff81aeb2a3 Mon Sep 17 00:00:00 2001 From: Diego Reis Date: Thu, 20 Mar 2025 17:02:56 -0300 Subject: [PATCH] ext/python: Partially implements `commit()` It was based on https://docs.python.org/3/library/sqlite3.html but some more work is needed specially in LEGACY_TRANSACTION_CONTROL and isolation levels. See: https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.autocommit --- bindings/python/src/lib.rs | 20 +++++++++++++++++--- core/lib.rs | 4 ++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index 6b38f503c..dde9cf0c0 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -215,6 +215,13 @@ fn stmt_is_ddl(sql: &str) -> bool { sql.starts_with("CREATE") || sql.starts_with("ALTER") || sql.starts_with("DROP") } +#[pyclass(unsendable)] +#[derive(Clone)] +pub struct Connection { + conn: Rc, + io: Arc, +} + #[pymethods] impl Connection { pub fn cursor(&self) -> Result { @@ -232,9 +239,16 @@ impl Connection { } pub fn commit(&self) -> PyResult<()> { - Err(PyErr::new::( - "Transactions are not supported in this version", - )) + if !self.conn.get_auto_commit() { + self.conn.execute("COMMIT").map_err(|e| { + PyErr::new::(format!("Failed to commit: {:?}", e)) + })?; + + self.conn.execute("BEGIN").map_err(|e| { + PyErr::new::(format!("Failed to commit: {:?}", e)) + })?; + } + Ok(()) } pub fn rollback(&self) -> PyResult<()> { diff --git a/core/lib.rs b/core/lib.rs index 4a5f0a206..71f5474de 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -525,6 +525,10 @@ impl Connection { } all_vfs } + + pub fn get_auto_commit(&self) -> bool { + *self.auto_commit.borrow() + } } pub struct Statement {