From 6f4b80a31a5cd157c4d743650f951decdcfb6c41 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 22 Nov 2024 11:22:28 -0600 Subject: 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. --- gn_libs/mysqldb.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'gn_libs') 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 -- cgit v1.2.3