From fe6b5e62b1f2a18c049b8fb7ee497120639b7817 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sun, 12 Apr 2015 20:33:58 +0200 Subject: [PATCH] Normalized the implementation of get_db_converters. Put the types in the same order and checked for None consistently. --- django/db/backends/base/operations.py | 5 ++-- django/db/backends/mysql/operations.py | 22 +++++++++--------- django/db/backends/oracle/operations.py | 29 ++++++++++++------------ django/db/backends/sqlite3/operations.py | 21 +++++++++-------- 4 files changed, 41 insertions(+), 36 deletions(-) diff --git a/django/db/backends/base/operations.py b/django/db/backends/base/operations.py index 28b110f849..df6d844297 100644 --- a/django/db/backends/base/operations.py +++ b/django/db/backends/base/operations.py @@ -505,10 +505,11 @@ class BaseDatabaseOperations(object): return [first, second] def get_db_converters(self, expression): - """Get a list of functions needed to convert field data. + """ + Get a list of functions needed to convert field data. Some field types on some backends do not provide data in the correct - format, this is the hook for coverter functions. + format, this is the hook for converter functions. """ return [] diff --git a/django/db/backends/mysql/operations.py b/django/db/backends/mysql/operations.py index 3de35c7891..39fbf49c30 100644 --- a/django/db/backends/mysql/operations.py +++ b/django/db/backends/mysql/operations.py @@ -182,16 +182,21 @@ class DatabaseOperations(BaseDatabaseOperations): def get_db_converters(self, expression): converters = super(DatabaseOperations, self).get_db_converters(expression) internal_type = expression.output_field.get_internal_type() - if internal_type in ['BooleanField', 'NullBooleanField']: - converters.append(self.convert_booleanfield_value) - if internal_type == 'DateTimeField': - converters.append(self.convert_datetimefield_value) - if internal_type == 'UUIDField': - converters.append(self.convert_uuidfield_value) if internal_type == 'TextField': converters.append(self.convert_textfield_value) + elif internal_type in ['BooleanField', 'NullBooleanField']: + converters.append(self.convert_booleanfield_value) + elif internal_type == 'DateTimeField': + converters.append(self.convert_datetimefield_value) + elif internal_type == 'UUIDField': + converters.append(self.convert_uuidfield_value) return converters + def convert_textfield_value(self, value, expression, connection, context): + if value is not None: + value = force_text(value) + return value + def convert_booleanfield_value(self, value, expression, connection, context): if value in (0, 1): value = bool(value) @@ -207,8 +212,3 @@ class DatabaseOperations(BaseDatabaseOperations): if value is not None: value = uuid.UUID(value) return value - - def convert_textfield_value(self, value, expression, connection, context): - if value is not None: - value = force_text(value) - return value diff --git a/django/db/backends/oracle/operations.py b/django/db/backends/oracle/operations.py index 2ef8caf4e5..2b3ffb3a8c 100644 --- a/django/db/backends/oracle/operations.py +++ b/django/db/backends/oracle/operations.py @@ -174,18 +174,6 @@ WHEN (new.%(col_name)s IS NULL) converters.append(self.convert_empty_values) return converters - def convert_empty_values(self, value, expression, connection, context): - # Oracle stores empty strings as null. We need to undo this in - # order to adhere to the Django convention of using the empty - # string instead of null, but only if the field accepts the - # empty string. - field = expression.output_field - if value is None and field.empty_strings_allowed: - value = '' - if field.get_internal_type() == 'BinaryField': - value = b'' - return value - def convert_textfield_value(self, value, expression, connection, context): if isinstance(value, Database.LOB): value = force_text(value.read()) @@ -197,7 +185,7 @@ WHEN (new.%(col_name)s IS NULL) return value def convert_booleanfield_value(self, value, expression, connection, context): - if value in (1, 0): + if value in (0, 1): value = bool(value) return value @@ -213,7 +201,8 @@ WHEN (new.%(col_name)s IS NULL) def convert_datefield_value(self, value, expression, connection, context): if isinstance(value, Database.Timestamp): - return value.date() + value = value.date() + return value def convert_timefield_value(self, value, expression, connection, context): if isinstance(value, Database.Timestamp): @@ -225,6 +214,18 @@ WHEN (new.%(col_name)s IS NULL) value = uuid.UUID(value) return value + def convert_empty_values(self, value, expression, connection, context): + # Oracle stores empty strings as null. We need to undo this in + # order to adhere to the Django convention of using the empty + # string instead of null, but only if the field accepts the + # empty string. + field = expression.output_field + if value is None and field.empty_strings_allowed: + value = '' + if field.get_internal_type() == 'BinaryField': + value = b'' + return value + def deferrable_sql(self): return " DEFERRABLE INITIALLY DEFERRED" diff --git a/django/db/backends/sqlite3/operations.py b/django/db/backends/sqlite3/operations.py index 3505ebda03..da99b6b217 100644 --- a/django/db/backends/sqlite3/operations.py +++ b/django/db/backends/sqlite3/operations.py @@ -151,15 +151,6 @@ class DatabaseOperations(BaseDatabaseOperations): converters.append(self.convert_uuidfield_value) return converters - def convert_decimalfield_value(self, value, expression, connection, context): - return backend_utils.typecast_decimal(expression.output_field.format_number(value)) - - def convert_datefield_value(self, value, expression, connection, context): - if value is not None: - if not isinstance(value, datetime.date): - value = parse_date(value) - return value - def convert_datetimefield_value(self, value, expression, connection, context): if value is not None: if not isinstance(value, datetime.datetime): @@ -168,12 +159,24 @@ class DatabaseOperations(BaseDatabaseOperations): value = value.replace(tzinfo=timezone.utc) return value + def convert_datefield_value(self, value, expression, connection, context): + if value is not None: + if not isinstance(value, datetime.date): + value = parse_date(value) + return value + def convert_timefield_value(self, value, expression, connection, context): if value is not None: if not isinstance(value, datetime.time): value = parse_time(value) return value + def convert_decimalfield_value(self, value, expression, connection, context): + if value is not None: + value = expression.output_field.format_number(value) + value = backend_utils.typecast_decimal(value) + return value + def convert_uuidfield_value(self, value, expression, connection, context): if value is not None: value = uuid.UUID(value)