Refs #27795 -- Removed force_bytes() usage in MySQL backend.

The mysqlclient cursor attribute `_last_executed` is always stored as
bytes. Decode it.

TextField values are already type str. No need to decode.
This commit is contained in:
Jon Dufresne 2018-10-01 07:59:34 -05:00 committed by Carlton Gibson
parent 3212008ba6
commit efd8a82e26
1 changed files with 5 additions and 10 deletions

View File

@ -5,7 +5,6 @@ from django.conf import settings
from django.db.backends.base.operations import BaseDatabaseOperations from django.db.backends.base.operations import BaseDatabaseOperations
from django.utils import timezone from django.utils import timezone
from django.utils.duration import duration_microseconds from django.utils.duration import duration_microseconds
from django.utils.encoding import force_text
class DatabaseOperations(BaseDatabaseOperations): class DatabaseOperations(BaseDatabaseOperations):
@ -142,7 +141,10 @@ class DatabaseOperations(BaseDatabaseOperations):
# With MySQLdb, cursor objects have an (undocumented) "_last_executed" # With MySQLdb, cursor objects have an (undocumented) "_last_executed"
# attribute where the exact query sent to the database is saved. # attribute where the exact query sent to the database is saved.
# See MySQLdb/cursors.py in the source distribution. # See MySQLdb/cursors.py in the source distribution.
return force_text(getattr(cursor, '_last_executed', None), errors='replace') query = getattr(cursor, '_last_executed', None)
if query is not None:
query = query.decode(errors='replace')
return query
def no_limit_value(self): def no_limit_value(self):
# 2**64 - 1, as recommended by the MySQL documentation # 2**64 - 1, as recommended by the MySQL documentation
@ -233,9 +235,7 @@ class DatabaseOperations(BaseDatabaseOperations):
def get_db_converters(self, expression): def get_db_converters(self, expression):
converters = super().get_db_converters(expression) converters = super().get_db_converters(expression)
internal_type = expression.output_field.get_internal_type() internal_type = expression.output_field.get_internal_type()
if internal_type == 'TextField': if internal_type in ['BooleanField', 'NullBooleanField']:
converters.append(self.convert_textfield_value)
elif internal_type in ['BooleanField', 'NullBooleanField']:
converters.append(self.convert_booleanfield_value) converters.append(self.convert_booleanfield_value)
elif internal_type == 'DateTimeField': elif internal_type == 'DateTimeField':
if settings.USE_TZ: if settings.USE_TZ:
@ -244,11 +244,6 @@ class DatabaseOperations(BaseDatabaseOperations):
converters.append(self.convert_uuidfield_value) converters.append(self.convert_uuidfield_value)
return converters return converters
def convert_textfield_value(self, value, expression, connection):
if value is not None:
value = force_text(value)
return value
def convert_booleanfield_value(self, value, expression, connection): def convert_booleanfield_value(self, value, expression, connection):
if value in (0, 1): if value in (0, 1):
value = bool(value) value = bool(value)