about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2026-05-18 15:50:31 -0500
committerFrederick Muriuki Muriithi2026-05-18 15:50:31 -0500
commitf8ba6230f03ded7761c640a2465b3b566961fb58 (patch)
treef8c62f345a38b3b45c11e5873425552a4e9bbe0b
parent422d40142e4373f51c539fa846cc33b604e54c0f (diff)
downloadgn-libs-main.tar.gz
Call a function `func` with a database connection. HEAD main
-rw-r--r--gn_libs/sqlite3.py9
1 files changed, 9 insertions, 0 deletions
diff --git a/gn_libs/sqlite3.py b/gn_libs/sqlite3.py
index 78e1c41..565ed82 100644
--- a/gn_libs/sqlite3.py
+++ b/gn_libs/sqlite3.py
@@ -43,3 +43,12 @@ def cursor(conn: DbConnection) -> Iterator[DbCursor]:
         raise exc
     finally:
         cur.close()
+
+
+def with_db_connection(db_uri: str, func: Callable[[DbConnection], Any]) -> Any:
+    """
+    Call `func`, a function of one argument with the SQLite3 connection created
+    from the connection string `db_uri`.
+    """
+    with connection(db_uri) as conn:
+        return func(conn)