Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion mycli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions mycli/myclirc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions mycli/packages/special/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
2 changes: 2 additions & 0 deletions test/myclirc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down