mirror of
https://github.com/tursodatabase/limbo.git
synced 2025-08-03 09:42:22 +00:00
ext/python: Add support for Context Manager
This commit is contained in:
parent
6798341b05
commit
4ca5b11bed
2 changed files with 24 additions and 0 deletions
|
@ -266,6 +266,19 @@ impl Connection {
|
|||
"Transactions are not supported in this version",
|
||||
))
|
||||
}
|
||||
|
||||
fn __enter__(&self) -> PyResult<Self> {
|
||||
Ok(self.clone())
|
||||
}
|
||||
|
||||
fn __exit__(
|
||||
&self,
|
||||
_exc_type: Option<&Bound<'_, PyAny>>,
|
||||
_exc_val: Option<&Bound<'_, PyAny>>,
|
||||
_exc_tb: Option<&Bound<'_, PyAny>>,
|
||||
) -> PyResult<()> {
|
||||
self.close()
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::arc_with_non_send_sync)]
|
||||
|
|
|
@ -144,6 +144,17 @@ def test_commit(provider):
|
|||
conn.close()
|
||||
assert record
|
||||
|
||||
@pytest.mark.parametrize("provider", ["sqlite3", "limbo"])
|
||||
def test_with_statement(provider):
|
||||
with connect(provider, "tests/database.db") as conn:
|
||||
conn = connect(provider, "tests/database.db")
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT MAX(id) FROM users")
|
||||
|
||||
max_id = cursor.fetchone()
|
||||
|
||||
assert max_id
|
||||
assert max_id == (2,)
|
||||
|
||||
def connect(provider, database):
|
||||
if provider == "limbo":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue