Fix client_flag fubar and add support for more detailed warning

information available from MySQL-4.1 and newer server.
This commit is contained in:
adustman
2006-02-27 18:48:57 +00:00
parent 36158116b1
commit ae901f0a4f
2 changed files with 30 additions and 8 deletions

View File

@ -132,16 +132,17 @@ class Connection(_mysql.connection):
del kwargs2['use_unicode']
client_flag = kwargs.get('client_flag', 0)
client_version = [ int(n) for n in _mysql.get_client_info().split('.')[:2] ]
client_version = tuple([ int(n) for n in _mysql.get_client_info().split('.')[:2] ])
if client_version >= (4, 1):
client_version |= CLIENT.MULTI_STATEMENTS
client_flag |= CLIENT.MULTI_STATEMENTS
if client_version >= (5, 0):
client_version |= CLIENT.MULTI_RESULTS
client_flag |= CLIENT.MULTI_RESULTS
kwargs2['client_flag'] = client_flag
super(Connection, self).__init__(*args, **kwargs2)
self._server_version = tuple([ int(n) for n in self.get_server_info().split('.')[:2] ])
self.charset = self.character_set_name().split('_')[0]
if use_unicode:
@ -210,6 +211,18 @@ class Connection(_mysql.connection):
else:
return 0
def show_warnings(self):
"""Return detailed information about warnings as a
sequence of tuples of (Level, Code, Message). This
is only supported in MySQL-4.1 and up. If your server
is an earlier version, an empty sequence is returned."""
if self._server_version < (4,1): return ()
self.query("SHOW WARNINGS")
r = self.store_result()
warnings = r.fetch_row(0)
return [ (level.tostring(), int(code), message.tostring())
for level, code, message in warnings ]
Warning = Warning
Error = Error
InterfaceError = InterfaceError

View File

@ -56,7 +56,16 @@ class BaseCursor(object):
def _warning_check(self):
from warnings import warn
if self._warnings and self._info:
if self._warnings:
warnings = self.connection.show_warnings()
if warnings:
# This is done in two loops in case
# Warnings are set to raise exceptions.
for w in warnings:
self.messages.append((self.Warning, w))
for w in warnings:
warn(w[-1], self.Warning, 3)
elif self._info:
self.messages.append((self.Warning, self._info))
warn(self._info, self.Warning, 3)