Skip to content

Commit 0e2bdee

Browse files
committed
BUG24948205: Results from JSON_TYPE() are returned as bytearray
Result value from running a query with JSON_TYPE() is returned as a VAR_STRING value which is not converted and is returned as a bytearray. With this patch the returned value will be converted to a string type using the current charset, in case the conversion fail the value will still returned as a bytearray value. v3
1 parent 6320e07 commit 0e2bdee

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

lib/mysql/connector/conversion.py

Lines changed: 5 additions & 2 deletions
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, 2016, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2009, 2018, 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
@@ -559,7 +559,10 @@ def _STRING_to_python(self, value, dsc=None): # pylint: disable=C0103
559559
if dsc[7] & FieldFlag.SET:
560560
return self._SET_to_python(value, dsc)
561561
if dsc[7] & FieldFlag.BINARY:
562-
return value
562+
try:
563+
return value.decode(self.charset)
564+
except LookupError:
565+
return value
563566

564567
if self.charset == 'binary':
565568
return value

tests/test_bugs.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# MySQL Connector/Python - MySQL driver written in Python.
3-
# Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
3+
# Copyright (c) 2009, 2018, 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
@@ -52,6 +52,7 @@
5252
from mysql.connector import (connection, cursor, conversion, protocol,
5353
errors, constants, pooling)
5454
from mysql.connector.optionfiles import read_option_files
55+
from mysql.connector.catch23 import STRING_TYPES
5556
import mysql.connector
5657
import cpy_distutils
5758

@@ -4525,3 +4526,24 @@ def test_executemany_utf8mb4(self):
45254526
"INSERT INTO {0} VALUES (%s, %s)".format(self.tbl),
45264527
[(1, "Nuno"), (2, "Amitabh"), (3, "Rafael")]
45274528
)
4529+
4530+
4531+
class BugOra24948205(tests.MySQLConnectorTests):
4532+
"""BUG#24948205: RESULT OF JSON_TYPE IS BYTEARRAY INSTEAD OF STR
4533+
"""
4534+
def setUp(self):
4535+
config = tests.get_mysql_config()
4536+
self.tbl = "BugOra24948205"
4537+
self.cnx = connection.MySQLConnection(**config)
4538+
self.cur = self.cnx.cursor()
4539+
4540+
def test_execute_get_json_type_as_str(self):
4541+
self.cur.execute("SELECT j, JSON_TYPE(j), 'foo'"
4542+
"FROM (SELECT json_object('foo', 'bar') AS j) jdata")
4543+
data = [('{"foo": "bar"}', 'OBJECT', 'foo')]
4544+
rows = self.cur.fetchall()
4545+
self.assertEqual(data, rows)
4546+
for col in rows[0]:
4547+
self.assertTrue(isinstance(col, STRING_TYPES),
4548+
"{} is type {} and not the expected type "
4549+
"string".format(col, type(col)))

tests/test_cursor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def test_next(self):
321321
self.cur = cursor.MySQLCursor(self.cnx)
322322
self.assertRaises(StopIteration, self.cur.__next__)
323323
self.cur.execute("SELECT BINARY 'ham'")
324-
exp = (b'ham',)
324+
exp = ('ham',)
325325
self.assertEqual(exp, next(self.cur))
326326
self.cur.close()
327327

@@ -554,7 +554,7 @@ def test_execute(self):
554554
self.assertTrue(tests.cmp_result(exp, self.cur._warnings))
555555

556556
self.cur.execute("SELECT BINARY 'ham'")
557-
exp = [(b'ham',)]
557+
exp = [('ham',)]
558558
self.assertEqual(exp, self.cur.fetchall())
559559
self.cur.close()
560560

@@ -826,7 +826,7 @@ def test_fetchone(self):
826826
self.cnx = connection.MySQLConnection(**tests.get_mysql_config())
827827
self.cur = self.cnx.cursor()
828828
self.cur.execute("SELECT BINARY 'ham'")
829-
exp = (b'ham',)
829+
exp = ('ham',)
830830
self.assertEqual(exp, self.cur.fetchone())
831831
self.assertEqual(None, self.cur.fetchone())
832832
self.cur.close()

0 commit comments

Comments
 (0)