Skip to content

Commit f9b22fa

Browse files
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.
1 parent ab3edad commit f9b22fa

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
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
@@ -539,6 +539,8 @@ def _SET_to_python(self, value, dsc=None): # pylint: disable=C0103
539539
"""
540540
set_type = None
541541
val = value.decode(self.charset)
542+
if not val:
543+
return set()
542544
try:
543545
set_type = set(val.split(','))
544546
except ValueError:

src/mysql_capi.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
# MySQL Connector/Python - MySQL driver written in Python.
3-
# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3+
# Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
44
55
# MySQL Connector/Python is licensed under the terms of the GPLv2
66
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -2308,9 +2308,15 @@ MySQL_fetch_row(MySQL *self)
23082308
{
23092309
if (field_flags & SET_FLAG)
23102310
{
2311-
value= PySet_New(PyUnicode_Split(value,
2312-
PyStringFromString(","),
2313-
-1));
2311+
if (!strlen(row[i]))
2312+
{
2313+
value= PySet_New(NULL);
2314+
}
2315+
else
2316+
{
2317+
value= PySet_New(PyUnicode_Split(
2318+
value, PyStringFromString(","), -1));
2319+
}
23142320
if (!value)
23152321
{
23162322
goto error;

tests/test_bugs.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3336,3 +3336,48 @@ def test_binary_charset(self):
33363336

33373337
self.cur.execute("SELECT * FROM {0}".format(self.tbl))
33383338
self.assertEqual(exp, self.cur.fetchall())
3339+
3340+
3341+
class BugOra20301989(tests.MySQLConnectorTests):
3342+
"""BUG#20301989: SET DATA TYPE NOT TRANSLATED CORRECTLY WHEN EMPTY
3343+
"""
3344+
def setUp(self):
3345+
config = tests.get_mysql_config()
3346+
cnx = connection.MySQLConnection(**config)
3347+
cur = cnx.cursor()
3348+
3349+
self.tbl = 'Bug20301989'
3350+
cur.execute("DROP TABLE IF EXISTS {0}".format(self.tbl))
3351+
3352+
create = ("CREATE TABLE {0} (col1 SET('val1', 'val2')) "
3353+
"DEFAULT CHARSET latin1".format(self.tbl))
3354+
cur.execute(create)
3355+
cur.close()
3356+
cnx.close()
3357+
3358+
def tearDown(self):
3359+
config = tests.get_mysql_config()
3360+
cnx = connection.MySQLConnection(**config)
3361+
cur = cnx.cursor()
3362+
cur.execute("DROP TABLE IF EXISTS {0}".format(self.tbl))
3363+
cur.close()
3364+
cnx.close()
3365+
3366+
@foreach_cnx()
3367+
def test_set(self):
3368+
cur = self.cnx.cursor()
3369+
sql = "INSERT INTO {0} VALUES(%s)".format(self.tbl)
3370+
cur.execute(sql, ('val1,val2',))
3371+
cur.execute(sql, ('val1',))
3372+
cur.execute(sql, ('',))
3373+
cur.execute(sql, (None,))
3374+
3375+
exp = [
3376+
(set([u'val1', u'val2']),),
3377+
(set([u'val1']),),
3378+
(set([]),),
3379+
(None,)
3380+
]
3381+
3382+
cur.execute("SELECT * FROM {0}".format(self.tbl))
3383+
self.assertEqual(exp, cur.fetchall())

0 commit comments

Comments
 (0)