Skip to content

Commit f985cfc

Browse files
committed
BUG19584051: Fix comparision of type_code of columns for PEP-249
The type_code in cursor.description was not comparing equal to any of the type objects defined in mysql.connector.dbapi. We fix the issue by removing __cmp__ and implementing the rich comparison operators for the PEP-249 class DBAPITypeObject. A unit test has been added for BUG#19584051.
1 parent d30307a commit f985cfc

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

lib/mysql/connector/dbapi.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,22 @@
3636

3737
from . import constants
3838

39-
class _DBAPITypeObject:
39+
class _DBAPITypeObject(object):
4040

4141
def __init__(self, *values):
4242
self.values = values
4343

44-
def __cmp__(self, other):
44+
def __eq__(self, other):
4545
if other in self.values:
46-
return 0
47-
if other < self.values:
48-
return 1
46+
return True
4947
else:
50-
return -1
48+
return False
49+
50+
def __ne__(self, other):
51+
if other in self.values:
52+
return False
53+
else:
54+
return True
5155

5256
Date = datetime.date
5357
Time = datetime.time
@@ -64,8 +68,8 @@ def TimestampFromTicks(ticks):
6468

6569
Binary = bytes
6670

67-
STRING = _DBAPITypeObject(constants.FieldType.get_string_types())
68-
BINARY = _DBAPITypeObject(constants.FieldType.get_binary_types())
69-
NUMBER = _DBAPITypeObject(constants.FieldType.get_number_types())
70-
DATETIME = _DBAPITypeObject(constants.FieldType.get_timestamp_types())
71+
STRING = _DBAPITypeObject(*constants.FieldType.get_string_types())
72+
BINARY = _DBAPITypeObject(*constants.FieldType.get_binary_types())
73+
NUMBER = _DBAPITypeObject(*constants.FieldType.get_number_types())
74+
DATETIME = _DBAPITypeObject(*constants.FieldType.get_timestamp_types())
7175
ROWID = _DBAPITypeObject()

tests/test_bugs.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2867,3 +2867,51 @@ def test_option_files_with_include(self):
28672867

28682868
os.remove(temp_cnf_file)
28692869
os.remove(temp_include_file)
2870+
2871+
2872+
class BugOra19584051(tests.MySQLConnectorTests):
2873+
"""BUG#19584051: TYPE_CODE DOES NOT COMPARE EQUAL
2874+
"""
2875+
def setUp(self):
2876+
config = tests.get_mysql_config()
2877+
self.cnx = connection.MySQLConnection(**config)
2878+
self.cursor = self.cnx.cursor()
2879+
2880+
self.tbl = 'Bug19584051'
2881+
self.cursor.execute("DROP TABLE IF EXISTS %s" % self.tbl)
2882+
2883+
create = ('CREATE TABLE {0}(col1 INT NOT NULL, col2 BLOB, '
2884+
'col3 VARCHAR(10), col4 DECIMAL(4,2), '
2885+
'col5 DATETIME , col6 YEAR, '
2886+
'PRIMARY KEY(col1))'.format(self.tbl))
2887+
2888+
self.cursor.execute(create)
2889+
2890+
def tearDown(self):
2891+
self.cursor.execute("DROP TABLE IF EXISTS %s" % self.tbl)
2892+
self.cursor.close()
2893+
self.cnx.close()
2894+
2895+
def test_dbapi(self):
2896+
cur = self.cnx.cursor()
2897+
sql = ("INSERT INTO {0}(col1, col2, col3, col4, col5, col6) "
2898+
"VALUES (%s, %s, %s, %s, %s, %s)".format(self.tbl))
2899+
params = (100, 'blob-data', 'foo', 1.2, datetime(2014, 8, 4, 9, 11, 14),
2900+
2014)
2901+
2902+
exp = [
2903+
mysql.connector.NUMBER,
2904+
mysql.connector.BINARY,
2905+
mysql.connector.STRING,
2906+
mysql.connector.NUMBER,
2907+
mysql.connector.DATETIME,
2908+
mysql.connector.NUMBER,
2909+
]
2910+
cur.execute(sql, params)
2911+
2912+
sql = "SELECT * FROM {0}".format(self.tbl)
2913+
cur.execute(sql)
2914+
temp = cur.fetchone()
2915+
type_codes = [row[1] for row in cur.description]
2916+
self.assertEqual(exp, type_codes)
2917+
cur.close()

0 commit comments

Comments
 (0)