diff options
author | Frederick Muriuki Muriithi | 2024-11-22 11:22:28 -0600 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-11-22 11:22:28 -0600 |
commit | 6f4b80a31a5cd157c4d743650f951decdcfb6c41 (patch) | |
tree | 509a0dbd11aeed6c9483aae71a1e064df3ff9d5a | |
parent | 70f456f4f1b400f308bd38238baae047555b9278 (diff) | |
download | gn-libs-6f4b80a31a5cd157c4d743650f951decdcfb6c41.tar.gz |
Provide a means to debug queries that have been run.
It is sometimes necessary, when debugging, to see what query was
actually run, in order to make sense of, and fix failures. This commit
provides a means to see what the actual query that was run look(s/ed)
like.
-rw-r--r-- | gn_libs/mysqldb.py | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/gn_libs/mysqldb.py b/gn_libs/mysqldb.py index f0bc651..897b0c7 100644 --- a/gn_libs/mysqldb.py +++ b/gn_libs/mysqldb.py @@ -1,6 +1,7 @@ """This exte""" import logging import contextlib +from logging import Logger from urllib.parse import urlparse from typing import Any, Iterator, Protocol, Callable @@ -91,3 +92,11 @@ def database_connection(sql_uri: str, logger: logging.Logger = _logger) -> Itera finally: connection.commit() connection.close() + + +def debug_query(cursor: Cursor, logger: Logger) -> None: + """Debug the actual query run with MySQLdb""" + for attr in ("_executed", "statement", "_last_executed"): + if hasattr(cursor, attr): + logger.debug("MySQLdb QUERY: %s", getattr(cursor, attr)) + break |