Skip to content

Commit 004edd0

Browse files
peeyushguptaGeert Vanderkelen
authored and
Geert Vanderkelen
committed
BUG20301989: Fix conversion of empty set
We fix conversion of SET data coming from MySQL server when the set is empty. We now return an empty set(set([])) which was previously returned as a set of empty string(set([''])). A test case is added for BUG#20301989. (cherry picked from commit f9b22fa)
1 parent 6c06989 commit 004edd0

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/mysql/connector/conversion.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# MySQL Connector/Python - MySQL driver written in Python.
2-
# Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
33

44
# MySQL Connector/Python is licensed under the terms of the GPLv2
55
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -526,6 +526,8 @@ def _SET_to_python(self, value, dsc=None): # pylint: disable=C0103
526526
"""
527527
set_type = None
528528
val = value.decode(self.charset)
529+
if not val:
530+
return set()
529531
try:
530532
set_type = set(val.split(','))
531533
except ValueError:

tests/test_bugs.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3154,3 +3154,49 @@ def test_binary_charset(self):
31543154

31553155
self.cur.execute("SELECT * FROM {0}".format(self.tbl))
31563156
self.assertEqual(exp, self.cur.fetchall())
3157+
3158+
3159+
class BugOra20301989(tests.MySQLConnectorTests):
3160+
"""BUG#20301989: SET DATA TYPE NOT TRANSLATED CORRECTLY WHEN EMPTY
3161+
"""
3162+
def setUp(self):
3163+
config = tests.get_mysql_config()
3164+
cnx = connection.MySQLConnection(**config)
3165+
cur = cnx.cursor()
3166+
3167+
self.tbl = 'Bug20301989'
3168+
cur.execute("DROP TABLE IF EXISTS {0}".format(self.tbl))
3169+
3170+
create = ("CREATE TABLE {0} (col1 SET('val1', 'val2')) "
3171+
"DEFAULT CHARSET latin1".format(self.tbl))
3172+
cur.execute(create)
3173+
cur.close()
3174+
cnx.close()
3175+
3176+
def tearDown(self):
3177+
config = tests.get_mysql_config()
3178+
cnx = connection.MySQLConnection(**config)
3179+
cur = cnx.cursor()
3180+
cur.execute("DROP TABLE IF EXISTS {0}".format(self.tbl))
3181+
cur.close()
3182+
cnx.close()
3183+
3184+
def test_set(self):
3185+
config = tests.get_mysql_config()
3186+
cnx = connection.MySQLConnection(**config)
3187+
cur = cnx.cursor()
3188+
sql = "INSERT INTO {0} VALUES(%s)".format(self.tbl)
3189+
cur.execute(sql, ('val1,val2',))
3190+
cur.execute(sql, ('val1',))
3191+
cur.execute(sql, ('',))
3192+
cur.execute(sql, (None,))
3193+
3194+
exp = [
3195+
(set([u'val1', u'val2']),),
3196+
(set([u'val1']),),
3197+
(set([]),),
3198+
(None,)
3199+
]
3200+
3201+
cur.execute("SELECT * FROM {0}".format(self.tbl))
3202+
self.assertEqual(exp, cur.fetchall())

0 commit comments

Comments
 (0)