Support for \w and \W
Please support \W to show warnings after each statement and \w to hide them, just like regular mysql cli.
Currently, mycli does not even show that warnings were generated. e.g., it prints only 1 row in set while the official CLI client does indicate that there was a problem:
1 row in set, 1 warning (0.00 sec)
One of the biggest drawbacks with MySQL is that the server only warns the user when it truncates or corrupts data when silently converting from one data type to another.
This would also be useful as a configuration option to show warnings permanently, similar to how the default client has a --show-warnings option.
+1 for this feature!
More notes:
- I agree with anthony-geoghegan that there should be an option to show warnings by default.
- If there are many identical warnings, please group them (it happens with big INSERT SELECT)
- There should also be the ability to show warnings but not notes.
Looks like mycli uses https://github.com/PyMySQL/PyMySQL which doesn't seem to expose warnings in a good official API at the moment.
>>> import pymysql
>>> c = pymysql.connect(host='127.0.0.1',port=4000,user='root')
>>> cur = c.cursor()
>>> cur.execute('SELECT 1 + "2 foo"')
1
>>> cur._result.warning_count
1
Looks like https://github.com/PyMySQL/PyMySQL/pull/1056 might fix that.
This could be a start, once something like this is in the offical API:
diff --git a/mycli/sqlexecute.py b/mycli/sqlexecute.py
index 9461438..b7c60a1 100644
--- a/mycli/sqlexecute.py
+++ b/mycli/sqlexecute.py
@@ -248,6 +248,8 @@ class SQLExecute(object):
except special.CommandNotFound: # Regular SQL
_logger.debug('Regular sql statement. sql: %r', sql)
cur.execute(sql)
+ if cur._result.warning_count > 0:
+ print(f"UHOH, THERE ARE {cur._result.warning_count} WARNINGS")
while True:
yield self.get_result(cur)
tidb [(none)] > SELECT 1 + "0 foo";
UHOH, THERE ARE 1 WARNINGS
+-------------+
| 1 + "0 foo" |
+-------------+
| 1.0 |
+-------------+
1 row in set
Time: 0.005s
tidb [(none)] > SELECT 1 + "0";
+---------+
| 1 + "0" |
+---------+
| 1.0 |
+---------+
1 row in set
Time: 0.003s