diff options
Diffstat (limited to 'qc_app/db_utils.py')
-rw-r--r-- | qc_app/db_utils.py | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/qc_app/db_utils.py b/qc_app/db_utils.py index a04c5e1..75b6b73 100644 --- a/qc_app/db_utils.py +++ b/qc_app/db_utils.py @@ -2,7 +2,7 @@ import logging import traceback import contextlib -from typing import Tuple, Optional, Iterator +from typing import Any, Tuple, Optional, Iterator, Callable from urllib.parse import urlparse import MySQLdb as mdb @@ -32,3 +32,8 @@ def database_connection(db_url: Optional[str] = None) -> Iterator[mdb.Connection connection.rollback() finally: connection.close() + +def with_db_connection(func: Callable[[mdb.Connection], Any]) -> Any: + """Call `func` with a MySQDdb database connection.""" + with database_connection(app.config["SQL_URI"]) as conn: + return func(conn) |