Skip to content

Commit 6cfffa4

Browse files
committed
set_character_set() sends "SET NAMES" always.
1 parent 3ee07a0 commit 6cfffa4

File tree

1 file changed

+11
-29
lines changed

1 file changed

+11
-29
lines changed

MySQLdb/connections.py

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,6 @@ class object, used to create cursors (keyword only)
200200
)
201201

202202
self.encoding = "ascii" # overridden in set_character_set()
203-
db = proxy(self)
204-
205-
def unicode_literal(u, dummy=None):
206-
return db.string_literal(u.encode(db.encoding))
207203

208204
if not charset:
209205
charset = self.character_set_name()
@@ -227,7 +223,11 @@ def unicode_literal(u, dummy=None):
227223
# MySQL may return JSON with charset==binary.
228224
self.converter[FIELD_TYPE.JSON] = str
229225

226+
db = proxy(self)
227+
def unicode_literal(u, dummy=None):
228+
return db.string_literal(u.encode(db.encoding))
230229
self.encoders[str] = unicode_literal
230+
231231
self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS
232232
if self._transactional:
233233
if autocommit is not None:
@@ -300,32 +300,14 @@ def begin(self):
300300
"""
301301
self.query(b"BEGIN")
302302

303-
if not hasattr(_mysql.connection, "warning_count"):
304-
305-
def warning_count(self):
306-
"""Return the number of warnings generated from the
307-
last query. This is derived from the info() method."""
308-
info = self.info()
309-
if info:
310-
return int(info.split()[-1])
311-
else:
312-
return 0
313-
314303
def set_character_set(self, charset):
315-
"""Set the connection character set to charset. The character
316-
set can only be changed in MySQL-4.1 and newer. If you try
317-
to change the character set from the current value in an
318-
older version, NotSupportedError will be raised."""
319-
py_charset = _charset_to_encoding.get(charset, charset)
320-
if self.character_set_name() != charset:
321-
try:
322-
super().set_character_set(charset)
323-
except AttributeError:
324-
if self._server_version < (4, 1):
325-
raise NotSupportedError("server is too old to set charset")
326-
self.query("SET NAMES %s" % charset)
327-
self.store_result()
328-
self.encoding = py_charset
304+
"""Set the connection character set to charset."""
305+
try:
306+
super().set_character_set(charset)
307+
except AttributeError:
308+
self.query("SET NAMES %s" % charset)
309+
self.store_result()
310+
self.encoding = _charset_to_encoding.get(charset, charset)
329311

330312
def set_sql_mode(self, sql_mode):
331313
"""Set the connection sql_mode. See MySQL documentation for

0 commit comments

Comments
 (0)