Skip to content

Commit

Permalink
Merge pull request #31 from libsql/isolation-level
Browse files Browse the repository at this point in the history
Implement Connection.isolation_level
  • Loading branch information
penberg authored Dec 12, 2023
2 parents 750ffe0 + e51c49d commit 85defe2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ Unimplemented.

### autocommit

Unimplemented.
The driver only supports the legacy `isolation_level`-based transaction mode control and does not implement the `autocommit` property yet (see issue [#30](https://github.com/libsql/libsql-experimental-python/issues/30) for details).

### in_transaction

Returns `True` if there's an active transaction with uncommitted changes; otherwise returns `False`.

### isolation_level

Unimplemented.
Transaction handling mode configuration. If `isolation_level` is set to `"DEFERRED"`, `"IMMEDIATE"`, or `"EXCLUSIVE"`, transactions begin implicitly, but need to be committed manually. If `isolation_level` is set to `None`, then database is in auto-commit mode, executing each statement in its own transaction.

### row_factory

Expand Down
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ fn is_remote_path(path: &str) -> bool {
}

#[pyfunction]
#[pyo3(signature = (database, isolation_level="DEFERRED", check_same_thread=true, uri=false, sync_url=None, auth_token=""))]
#[pyo3(signature = (database, isolation_level="DEFERRED".to_string(), check_same_thread=true, uri=false, sync_url=None, auth_token=""))]
fn connect(
database: String,
isolation_level: Option<&str>,
isolation_level: Option<String>,
check_same_thread: bool,
uri: bool,
sync_url: Option<String>,
Expand Down Expand Up @@ -56,6 +56,7 @@ fn connect(
db,
conn,
rt,
isolation_level,
autocommit,
})
}
Expand All @@ -65,6 +66,7 @@ pub struct Connection {
db: libsql_core::Database,
conn: Arc<libsql_core::Connection>,
rt: tokio::runtime::Runtime,
isolation_level: Option<String>,
autocommit: bool,
}

Expand Down Expand Up @@ -139,6 +141,11 @@ impl Connection {
Ok(cursor)
}

#[getter]
fn isolation_level(self_: PyRef<'_, Self>) -> Option<String> {
self_.isolation_level.clone()
}

#[getter]
fn in_transaction(self_: PyRef<'_, Self>) -> PyResult<bool> {
Ok(!self_.conn.is_autocommit())
Expand Down
1 change: 1 addition & 0 deletions tests/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def test_commit_and_rollback(provider):
@pytest.mark.parametrize("provider", ["libsql", "sqlite"])
def test_autocommit(provider):
conn = connect(provider, ":memory:", None)
assert conn.isolation_level == None
assert conn.in_transaction == False
cur = conn.cursor()
assert conn.in_transaction == False
Expand Down

0 comments on commit 85defe2

Please sign in to comment.