Skip to content

Commit 77a50bd

Browse files
committed
BUG28239074: MySQLCursorDict does not return rows as dictionaries
Fetching the result using fetchall() using the pure Python cursor dictionary returns tuples instead of dictionaries. This patch uses the _row_to_python() to convert the results to dictionaries.
1 parent ad94644 commit 77a50bd

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

lib/mysql/connector/cursor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,9 @@ def fetchall(self):
13151315
(rows, eof) = self._connection.get_rows()
13161316
if self._nextrow[0]:
13171317
rows.insert(0, self._nextrow[0])
1318-
res = rows
1318+
res = []
1319+
for row in rows:
1320+
res.append(self._row_to_python(row, self.description))
13191321
self._handle_eof(eof)
13201322
rowcount = len(rows)
13211323
if rowcount >= 0 and self._rowcount == -1:

tests/test_bugs.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5070,6 +5070,86 @@ def test_cursor_buffered(self):
50705070
cur_pure_buffered.close()
50715071
cur_cext_buffered.close()
50725072

5073+
def test_cursor_dictionary(self):
5074+
"""Test results from cursor buffered are the same in pure or c-ext"""
5075+
cur_pure_dictionary = self.cnx_pure.cursor(dictionary=True)
5076+
cur_cext_dictionary = self.cnx_cext.cursor(dictionary=True)
5077+
self._test_fetchone(cur_pure_dictionary, cur_cext_dictionary)
5078+
self._test_fetchmany(cur_pure_dictionary, cur_cext_dictionary)
5079+
self._test_fetch_fetchall(cur_pure_dictionary, cur_cext_dictionary)
5080+
cur_pure_dictionary.close()
5081+
cur_cext_dictionary.close()
5082+
5083+
def test_cursor_dictionary_buf(self):
5084+
"""Test results from cursor buffered are the same in pure or c-ext"""
5085+
cur_pure = self.cnx_pure.cursor(dictionary=True,
5086+
buffered=True)
5087+
cur_cext = self.cnx_cext.cursor(dictionary=True,
5088+
buffered=True)
5089+
self._test_fetchone(cur_pure, cur_cext)
5090+
self._test_fetchmany(cur_pure, cur_cext)
5091+
self._test_fetch_fetchall(cur_pure, cur_cext)
5092+
cur_pure.close()
5093+
cur_cext.close()
5094+
5095+
5096+
class BugOra28239074(tests.MySQLConnectorTests):
5097+
"""BUG#28239074: CURSOR DICTIONARY DOES NOT RETURN DICTIONARY TYPE RESULTS
5098+
"""
5099+
table = "bug28239074"
5100+
5101+
def setUp(self):
5102+
config_pure = tests.get_mysql_config()
5103+
config_pure["use_pure"] = True
5104+
self.cnx = mysql.connector.connect(**config_pure)
5105+
cur = self.cnx.cursor(dictionary=True)
5106+
5107+
cur.execute("DROP TABLE IF EXISTS {0}".format(self.table))
5108+
cur.execute("CREATE TABLE {0}(a char(50) ,b int) "
5109+
"DEFAULT CHARSET utf8".format(self.table))
5110+
data = [(chr(1), 1),('s', 2),(chr(120), 3),(chr(121), 4),(chr(127), 5)]
5111+
cur.executemany("INSERT INTO {0} (a, b) VALUES "
5112+
"(%s, %s)".format(self.table), data)
5113+
5114+
def tearDown(self):
5115+
self.cnx.cmd_query("DROP TABLE IF EXISTS {}".format(self.table))
5116+
self.cnx.close()
5117+
5118+
def test_cursor_dict(self):
5119+
exp = [
5120+
{u'a': u'\x01', u'b': 1},
5121+
{u'a': u's', u'b': 2},
5122+
{u'a': u'\x78', u'b': 3},
5123+
{u'a': u'\x79', u'b': 4},
5124+
{u'a': u'\x7f', u'b': 5}
5125+
]
5126+
cur = self.cnx.cursor(dictionary=True)
5127+
5128+
# Test fetchone
5129+
cur.execute("SELECT * FROM {}".format(self.table))
5130+
i = 0
5131+
row = cur.fetchone()
5132+
while row is not None:
5133+
self.assertTrue(isinstance(row, dict))
5134+
self.assertEqual(exp[i], row, "row {} is not equal to expected row"
5135+
" {}".format(row, exp[i]))
5136+
row = cur.fetchone()
5137+
i += 1
5138+
5139+
# Test fetchall
5140+
cur.execute("SELECT * FROM {}".format(self.table))
5141+
rows = cur.fetchall()
5142+
self.assertEqual(exp, rows, "rows {} is not equal to expected row")
5143+
5144+
# Test for each in cursor
5145+
cur.execute("SELECT * FROM {}".format(self.table))
5146+
i = 0
5147+
for row in cur:
5148+
self.assertTrue(isinstance(row, dict))
5149+
self.assertEqual(exp[i], row, "row {} is not equal to expected row"
5150+
" {}".format(row, exp[i]))
5151+
i += 1
5152+
50735153

50745154
class BugOra27364914(tests.MySQLConnectorTests):
50755155
"""BUG#27364914: CURSOR PREPARED STATEMENTS DO NOT CONVERT STRINGS

0 commit comments

Comments
 (0)