diff --git a/changelog.md b/changelog.md index 15140b1d..e49b6092 100644 --- a/changelog.md +++ b/changelog.md @@ -7,6 +7,7 @@ Features * Allow styling of status, timing, and warnings text. * Set up customization of prompt/continuation colors in `~/.myclirc`. * Allow customization of the toolbar with prompt format strings. +* Add warnings-count prompt format strings: `\w` and `\W`. Bug Fixes diff --git a/mycli/main.py b/mycli/main.py index 14000fa8..7a8bdedf 100755 --- a/mycli/main.py +++ b/mycli/main.py @@ -77,7 +77,7 @@ from mycli.packages.prompt_utils import confirm, confirm_destructive_query from mycli.packages.special.favoritequeries import FavoriteQueries from mycli.packages.special.main import ArgType -from mycli.packages.special.utils import format_uptime, get_ssl_version, get_uptime +from mycli.packages.special.utils import format_uptime, get_ssl_version, get_uptime, get_warning_count from mycli.packages.sqlresult import SQLResult from mycli.packages.tabular_output import sql_format from mycli.packages.toolkit.history import FileHistoryWithTimestamp @@ -1589,6 +1589,18 @@ def get_prompt(self, string: str) -> str: string = string.replace('\\T', get_ssl_version(cur) or '(none)') else: string = string.replace('\\T', '(none)') + if hasattr(sqlexecute, 'conn') and sqlexecute.conn is not None: + if '\\w' in string: + with sqlexecute.conn.cursor() as cur: + string = string.replace('\\w', str(get_warning_count(cur) or '(none)')) + else: + string = string.replace('\\w', '(none)') + if hasattr(sqlexecute, 'conn') and sqlexecute.conn is not None: + if '\\W' in string: + with sqlexecute.conn.cursor() as cur: + string = string.replace('\\W', str(get_warning_count(cur) or '')) + else: + string = string.replace('\\W', '') return string def run_query( diff --git a/mycli/myclirc b/mycli/myclirc index d2f3efb9..374e9370 100644 --- a/mycli/myclirc +++ b/mycli/myclirc @@ -116,6 +116,8 @@ wider_completion_menu = False # * \T - connection SSL/TLS version # * \t - database vendor (Percona, MySQL, MariaDB, TiDB) # * \u - username +# * \w - number of warnings, or "(none)" (requires frequent trips to the server) +# * \W - number of warnings, or the empty string (requires frequent trips to the server) # * \y - uptime in seconds (requires frequent trips to the server) # * \Y - uptime in words (requires frequent trips to the server) # * \A - DSN alias diff --git a/mycli/packages/special/utils.py b/mycli/packages/special/utils.py index 88002a89..c6e12ebe 100644 --- a/mycli/packages/special/utils.py +++ b/mycli/packages/special/utils.py @@ -83,6 +83,22 @@ def get_uptime(cur: Cursor) -> int: return uptime +def get_warning_count(cur: Cursor) -> int: + query = 'SHOW COUNT(*) WARNINGS' + logger.debug(query) + + warning_count = 0 + + try: + cur.execute(query) + if one := cur.fetchone(): + warning_count = int(one[0] or 0) + except pymysql.err.OperationalError: + pass + + return warning_count + + def get_ssl_version(cur: Cursor) -> str | None: cache_key = (id(cur.connection), cur.connection.thread_id()) diff --git a/test/myclirc b/test/myclirc index 82f8f870..9ff96d8a 100644 --- a/test/myclirc +++ b/test/myclirc @@ -113,6 +113,8 @@ wider_completion_menu = False # * \K - full connection socket path OR the port # * \T - connection SSL/TLS version # * \t - database vendor (Percona, MySQL, MariaDB, TiDB) +# * \w - number of warnings, or "(none)" (requires frequent trips to the server) +# * \W - number of warnings, or the empty string (requires frequent trips to the server) # * \y - uptime in seconds (requires frequent trips to the server) # * \Y - uptime in words (requires frequent trips to the server) # * \u - username