Kaydet (Commit) fe6b5e62 authored tarafından Aymeric Augustin's avatar Aymeric Augustin

Normalized the implementation of get_db_converters.

Put the types in the same order and checked for None consistently.
üst ec186572
...@@ -505,10 +505,11 @@ class BaseDatabaseOperations(object): ...@@ -505,10 +505,11 @@ class BaseDatabaseOperations(object):
return [first, second] return [first, second]
def get_db_converters(self, expression): 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 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 [] return []
......
...@@ -182,16 +182,21 @@ class DatabaseOperations(BaseDatabaseOperations): ...@@ -182,16 +182,21 @@ class DatabaseOperations(BaseDatabaseOperations):
def get_db_converters(self, expression): def get_db_converters(self, expression):
converters = super(DatabaseOperations, self).get_db_converters(expression) converters = super(DatabaseOperations, self).get_db_converters(expression)
internal_type = expression.output_field.get_internal_type() internal_type = expression.output_field.get_internal_type()
if internal_type in ['BooleanField', 'NullBooleanField']: if internal_type == 'TextField':
converters.append(self.convert_textfield_value)
elif internal_type in ['BooleanField', 'NullBooleanField']:
converters.append(self.convert_booleanfield_value) converters.append(self.convert_booleanfield_value)
if internal_type == 'DateTimeField': elif internal_type == 'DateTimeField':
converters.append(self.convert_datetimefield_value) converters.append(self.convert_datetimefield_value)
if internal_type == 'UUIDField': elif internal_type == 'UUIDField':
converters.append(self.convert_uuidfield_value) converters.append(self.convert_uuidfield_value)
if internal_type == 'TextField':
converters.append(self.convert_textfield_value)
return converters 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): def convert_booleanfield_value(self, value, expression, connection, context):
if value in (0, 1): if value in (0, 1):
value = bool(value) value = bool(value)
...@@ -207,8 +212,3 @@ class DatabaseOperations(BaseDatabaseOperations): ...@@ -207,8 +212,3 @@ class DatabaseOperations(BaseDatabaseOperations):
if value is not None: if value is not None:
value = uuid.UUID(value) value = uuid.UUID(value)
return value return value
def convert_textfield_value(self, value, expression, connection, context):
if value is not None:
value = force_text(value)
return value
...@@ -174,18 +174,6 @@ WHEN (new.%(col_name)s IS NULL) ...@@ -174,18 +174,6 @@ WHEN (new.%(col_name)s IS NULL)
converters.append(self.convert_empty_values) converters.append(self.convert_empty_values)
return converters 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): def convert_textfield_value(self, value, expression, connection, context):
if isinstance(value, Database.LOB): if isinstance(value, Database.LOB):
value = force_text(value.read()) value = force_text(value.read())
...@@ -197,7 +185,7 @@ WHEN (new.%(col_name)s IS NULL) ...@@ -197,7 +185,7 @@ WHEN (new.%(col_name)s IS NULL)
return value return value
def convert_booleanfield_value(self, value, expression, connection, context): def convert_booleanfield_value(self, value, expression, connection, context):
if value in (1, 0): if value in (0, 1):
value = bool(value) value = bool(value)
return value return value
...@@ -213,7 +201,8 @@ WHEN (new.%(col_name)s IS NULL) ...@@ -213,7 +201,8 @@ WHEN (new.%(col_name)s IS NULL)
def convert_datefield_value(self, value, expression, connection, context): def convert_datefield_value(self, value, expression, connection, context):
if isinstance(value, Database.Timestamp): if isinstance(value, Database.Timestamp):
return value.date() value = value.date()
return value
def convert_timefield_value(self, value, expression, connection, context): def convert_timefield_value(self, value, expression, connection, context):
if isinstance(value, Database.Timestamp): if isinstance(value, Database.Timestamp):
...@@ -225,6 +214,18 @@ WHEN (new.%(col_name)s IS NULL) ...@@ -225,6 +214,18 @@ WHEN (new.%(col_name)s IS NULL)
value = uuid.UUID(value) value = uuid.UUID(value)
return 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): def deferrable_sql(self):
return " DEFERRABLE INITIALLY DEFERRED" return " DEFERRABLE INITIALLY DEFERRED"
......
...@@ -151,15 +151,6 @@ class DatabaseOperations(BaseDatabaseOperations): ...@@ -151,15 +151,6 @@ class DatabaseOperations(BaseDatabaseOperations):
converters.append(self.convert_uuidfield_value) converters.append(self.convert_uuidfield_value)
return converters 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): def convert_datetimefield_value(self, value, expression, connection, context):
if value is not None: if value is not None:
if not isinstance(value, datetime.datetime): if not isinstance(value, datetime.datetime):
...@@ -168,12 +159,24 @@ class DatabaseOperations(BaseDatabaseOperations): ...@@ -168,12 +159,24 @@ class DatabaseOperations(BaseDatabaseOperations):
value = value.replace(tzinfo=timezone.utc) value = value.replace(tzinfo=timezone.utc)
return value 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): def convert_timefield_value(self, value, expression, connection, context):
if value is not None: if value is not None:
if not isinstance(value, datetime.time): if not isinstance(value, datetime.time):
value = parse_time(value) value = parse_time(value)
return 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): def convert_uuidfield_value(self, value, expression, connection, context):
if value is not None: if value is not None:
value = uuid.UUID(value) value = uuid.UUID(value)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment