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 @@ -13,6 +13,7 @@ Features
* Accept `character_set` as a DSN query parameter.
* Don't attempt SSL for local socket connections when in "auto" SSL mode.
* Add prompt format string for SSL/TLS version of the connection.
* Add prompt format strings for displaying uptime.


Bug Fixes
Expand Down
15 changes: 14 additions & 1 deletion mycli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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 get_ssl_version
from mycli.packages.special.utils import format_uptime, get_ssl_version, get_uptime
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 @@ -1454,6 +1454,7 @@ def get_completions(self, text: str, cursor_position: int) -> Iterable[Completio
with self._completer_lock:
return self.completer.get_completions(Document(text=text, cursor_position=cursor_position), None)

# todo: time/uptime update on every character typed, instead of after every return
def get_prompt(self, string: str) -> str:
sqlexecute = self.sqlexecute
assert sqlexecute is not None
Expand Down Expand Up @@ -1483,6 +1484,18 @@ def get_prompt(self, string: str) -> str:
string = string.replace("\\k", os.path.basename(sqlexecute.socket or str(sqlexecute.port)))
string = string.replace("\\K", sqlexecute.socket or str(sqlexecute.port))
string = string.replace("\\A", self.dsn_alias or "(none)")
# jump through hoops for the test environment, and for efficiency
if hasattr(sqlexecute, 'conn') and sqlexecute.conn is not None:
if '\\y' in string:
with sqlexecute.conn.cursor() as cur:
string = string.replace('\\y', str(get_uptime(cur)) or '(none)')
if '\\Y' in string:
with sqlexecute.conn.cursor() as cur:
string = string.replace('\\Y', format_uptime(str(get_uptime(cur))) or '(none)')
else:
string = string.replace('\\y', '(none)')
string = string.replace('\\Y', '(none)')

string = string.replace("\\_", " ")
# jump through hoops for the test environment and for efficiency
if hasattr(sqlexecute, 'conn') and sqlexecute.conn is not None:
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
# * \y - uptime in seconds (requires frequent trips to the server)
# * \Y - uptime in words (requires frequent trips to the server)
# * \A - DSN alias
# * \n - a newline
# * \_ - a space
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 @@ -56,6 +56,22 @@ def format_uptime(uptime_in_seconds: str) -> str:
return uptime


def get_uptime(cur: Cursor) -> int:
query = 'SHOW STATUS LIKE "Uptime"'
logger.debug(query)

uptime = 0

try:
cur.execute(query)
if one := cur.fetchone():
uptime = int(one[1] or 0)
except pymysql.err.OperationalError:
pass

return uptime


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)
# * \y - uptime in seconds (requires frequent trips to the server)
# * \Y - uptime in words (requires frequent trips to the server)
# * \u - username
# * \A - DSN alias
# * \n - a newline
Expand Down