Renamed value_to_db_xxx to adapt_xxxfield_value.

This mirrors convert_xxxfield_value nicely, taking advantage of the
adapter/converter terminology which is commonly used by DB-API modules.
This commit is contained in:
Aymeric Augustin 2015-05-02 21:27:44 +02:00
parent d9521f66b1
commit 54026f1e8d
10 changed files with 39 additions and 35 deletions

View File

@ -136,7 +136,7 @@ class DatabaseCache(BaseDatabaseCache):
current_expires = typecast_timestamp(str(current_expires)) current_expires = typecast_timestamp(str(current_expires))
if settings.USE_TZ: if settings.USE_TZ:
current_expires = current_expires.replace(tzinfo=timezone.utc) current_expires = current_expires.replace(tzinfo=timezone.utc)
exp = connections[db].ops.value_to_db_datetime(exp) exp = connections[db].ops.adapt_datetimefield_value(exp)
if result and (mode == 'set' or (mode == 'add' and current_expires < now)): if result and (mode == 'set' or (mode == 'add' and current_expires < now)):
cursor.execute("UPDATE %s SET value = %%s, expires = %%s " cursor.execute("UPDATE %s SET value = %%s, expires = %%s "
"WHERE cache_key = %%s" % table, "WHERE cache_key = %%s" % table,
@ -177,7 +177,7 @@ class DatabaseCache(BaseDatabaseCache):
with connections[db].cursor() as cursor: with connections[db].cursor() as cursor:
cursor.execute("SELECT cache_key FROM %s " cursor.execute("SELECT cache_key FROM %s "
"WHERE cache_key = %%s and expires > %%s" % table, "WHERE cache_key = %%s and expires > %%s" % table,
[key, connections[db].ops.value_to_db_datetime(now)]) [key, connections[db].ops.adapt_datetimefield_value(now)])
return cursor.fetchone() is not None return cursor.fetchone() is not None
def _cull(self, db, cursor, now): def _cull(self, db, cursor, now):
@ -188,7 +188,7 @@ class DatabaseCache(BaseDatabaseCache):
now = now.replace(tzinfo=None) now = now.replace(tzinfo=None)
table = connections[db].ops.quote_name(self._table) table = connections[db].ops.quote_name(self._table)
cursor.execute("DELETE FROM %s WHERE expires < %%s" % table, cursor.execute("DELETE FROM %s WHERE expires < %%s" % table,
[connections[db].ops.value_to_db_datetime(now)]) [connections[db].ops.adapt_datetimefield_value(now)])
cursor.execute("SELECT COUNT(*) FROM %s" % table) cursor.execute("SELECT COUNT(*) FROM %s" % table)
num = cursor.fetchone()[0] num = cursor.fetchone()[0]
if num > self._max_entries: if num > self._max_entries:

View File

@ -433,7 +433,7 @@ class BaseDatabaseOperations(object):
""" """
return value return value
def value_to_db_unknown(self, value): def adapt_unknown_value(self, value):
""" """
Transforms a value to something compatible with the backend driver. Transforms a value to something compatible with the backend driver.
@ -442,17 +442,17 @@ class BaseDatabaseOperations(object):
As a consequence it may not work perfectly in all circumstances. As a consequence it may not work perfectly in all circumstances.
""" """
if isinstance(value, datetime.datetime): # must be before date if isinstance(value, datetime.datetime): # must be before date
return self.value_to_db_datetime(value) return self.adapt_datetimefield_value(value)
elif isinstance(value, datetime.date): elif isinstance(value, datetime.date):
return self.value_to_db_date(value) return self.adapt_datefield_value(value)
elif isinstance(value, datetime.time): elif isinstance(value, datetime.time):
return self.value_to_db_time(value) return self.adapt_timefield_value(value)
elif isinstance(value, decimal.Decimal): elif isinstance(value, decimal.Decimal):
return self.value_to_db_decimal(value) return self.adapt_decimalfield_value(value)
else: else:
return value return value
def value_to_db_date(self, value): def adapt_datefield_value(self, value):
""" """
Transforms a date value to an object compatible with what is expected Transforms a date value to an object compatible with what is expected
by the backend driver for date columns. by the backend driver for date columns.
@ -461,7 +461,7 @@ class BaseDatabaseOperations(object):
return None return None
return six.text_type(value) return six.text_type(value)
def value_to_db_datetime(self, value): def adapt_datetimefield_value(self, value):
""" """
Transforms a datetime value to an object compatible with what is expected Transforms a datetime value to an object compatible with what is expected
by the backend driver for datetime columns. by the backend driver for datetime columns.
@ -470,7 +470,7 @@ class BaseDatabaseOperations(object):
return None return None
return six.text_type(value) return six.text_type(value)
def value_to_db_time(self, value): def adapt_timefield_value(self, value):
""" """
Transforms a time value to an object compatible with what is expected Transforms a time value to an object compatible with what is expected
by the backend driver for time columns. by the backend driver for time columns.
@ -481,14 +481,14 @@ class BaseDatabaseOperations(object):
raise ValueError("Django does not support timezone-aware times.") raise ValueError("Django does not support timezone-aware times.")
return six.text_type(value) return six.text_type(value)
def value_to_db_decimal(self, value, max_digits, decimal_places): def adapt_decimalfield_value(self, value, max_digits, decimal_places):
""" """
Transforms a decimal.Decimal value to an object compatible with what is Transforms a decimal.Decimal value to an object compatible with what is
expected by the backend driver for decimal (numeric) columns. expected by the backend driver for decimal (numeric) columns.
""" """
return utils.format_number(value, max_digits, decimal_places) return utils.format_number(value, max_digits, decimal_places)
def value_to_db_ipaddress(self, value): def adapt_ipaddressfield_value(self, value):
""" """
Transforms a string representation of an IP address into the expected Transforms a string representation of an IP address into the expected
type for the backend driver. type for the backend driver.
@ -505,8 +505,8 @@ class BaseDatabaseOperations(object):
""" """
first = datetime.date(value, 1, 1) first = datetime.date(value, 1, 1)
second = datetime.date(value, 12, 31) second = datetime.date(value, 12, 31)
first = self.value_to_db_date(first) first = self.adapt_datefield_value(first)
second = self.value_to_db_date(second) second = self.adapt_datefield_value(second)
return [first, second] return [first, second]
def year_lookup_bounds_for_datetime_field(self, value): def year_lookup_bounds_for_datetime_field(self, value):
@ -523,8 +523,8 @@ class BaseDatabaseOperations(object):
tz = timezone.get_current_timezone() tz = timezone.get_current_timezone()
first = timezone.make_aware(first, tz) first = timezone.make_aware(first, tz)
second = timezone.make_aware(second, tz) second = timezone.make_aware(second, tz)
first = self.value_to_db_datetime(first) first = self.adapt_datetimefield_value(first)
second = self.value_to_db_datetime(second) second = self.adapt_datetimefield_value(second)
return [first, second] return [first, second]
def get_db_converters(self, expression): def get_db_converters(self, expression):

View File

@ -138,7 +138,7 @@ class DatabaseOperations(BaseDatabaseOperations):
'value for AutoField.') 'value for AutoField.')
return value return value
def value_to_db_datetime(self, value): def adapt_datetimefield_value(self, value):
if value is None: if value is None:
return None return None
@ -154,7 +154,7 @@ class DatabaseOperations(BaseDatabaseOperations):
return six.text_type(value) return six.text_type(value)
def value_to_db_time(self, value): def adapt_timefield_value(self, value):
if value is None: if value is None:
return None return None

View File

@ -374,7 +374,7 @@ WHEN (new.%(col_name)s IS NULL)
else: else:
return "TABLESPACE %s" % self.quote_name(tablespace) return "TABLESPACE %s" % self.quote_name(tablespace)
def value_to_db_date(self, value): def adapt_datefield_value(self, value):
""" """
Transform a date value to an object compatible with what is expected Transform a date value to an object compatible with what is expected
by the backend driver for date columns. by the backend driver for date columns.
@ -383,7 +383,7 @@ WHEN (new.%(col_name)s IS NULL)
""" """
return value return value
def value_to_db_datetime(self, value): def adapt_datetimefield_value(self, value):
""" """
Transform a datetime value to an object compatible with what is expected Transform a datetime value to an object compatible with what is expected
by the backend driver for datetime columns. by the backend driver for datetime columns.
@ -405,7 +405,7 @@ WHEN (new.%(col_name)s IS NULL)
return Oracle_datetime.from_datetime(value) return Oracle_datetime.from_datetime(value)
def value_to_db_time(self, value): def adapt_timefield_value(self, value):
if value is None: if value is None:
return None return None

View File

@ -225,16 +225,16 @@ class DatabaseOperations(BaseDatabaseOperations):
items_sql = "(%s)" % ", ".join(["%s"] * len(fields)) items_sql = "(%s)" % ", ".join(["%s"] * len(fields))
return "VALUES " + ", ".join([items_sql] * num_values) return "VALUES " + ", ".join([items_sql] * num_values)
def value_to_db_date(self, value): def adapt_datefield_value(self, value):
return value return value
def value_to_db_datetime(self, value): def adapt_datetimefield_value(self, value):
return value return value
def value_to_db_time(self, value): def adapt_timefield_value(self, value):
return value return value
def value_to_db_ipaddress(self, value): def adapt_ipaddressfield_value(self, value):
if value: if value:
return Inet(value) return Inet(value)
return None return None

View File

@ -113,7 +113,7 @@ class DatabaseOperations(BaseDatabaseOperations):
# sql_flush() implementations). Just return SQL at this point # sql_flush() implementations). Just return SQL at this point
return sql return sql
def value_to_db_datetime(self, value): def adapt_datetimefield_value(self, value):
if value is None: if value is None:
return None return None
@ -126,7 +126,7 @@ class DatabaseOperations(BaseDatabaseOperations):
return six.text_type(value) return six.text_type(value)
def value_to_db_time(self, value): def adapt_timefield_value(self, value):
if value is None: if value is None:
return None return None

View File

@ -1295,7 +1295,7 @@ class DateField(DateTimeCheckMixin, Field):
# Casts dates into the format expected by the backend # Casts dates into the format expected by the backend
if not prepared: if not prepared:
value = self.get_prep_value(value) value = self.get_prep_value(value)
return connection.ops.value_to_db_date(value) return connection.ops.adapt_datefield_value(value)
def value_to_string(self, obj): def value_to_string(self, obj):
val = self._get_val_from_obj(obj) val = self._get_val_from_obj(obj)
@ -1455,7 +1455,7 @@ class DateTimeField(DateField):
# Casts datetimes into the format expected by the backend # Casts datetimes into the format expected by the backend
if not prepared: if not prepared:
value = self.get_prep_value(value) value = self.get_prep_value(value)
return connection.ops.value_to_db_datetime(value) return connection.ops.adapt_datetimefield_value(value)
def value_to_string(self, obj): def value_to_string(self, obj):
val = self._get_val_from_obj(obj) val = self._get_val_from_obj(obj)
@ -1598,7 +1598,7 @@ class DecimalField(Field):
return utils.format_number(value, self.max_digits, self.decimal_places) return utils.format_number(value, self.max_digits, self.decimal_places)
def get_db_prep_save(self, value, connection): def get_db_prep_save(self, value, connection):
return connection.ops.value_to_db_decimal(self.to_python(value), return connection.ops.adapt_decimalfield_value(self.to_python(value),
self.max_digits, self.decimal_places) self.max_digits, self.decimal_places)
def get_prep_value(self, value): def get_prep_value(self, value):
@ -1976,7 +1976,7 @@ class GenericIPAddressField(Field):
def get_db_prep_value(self, value, connection, prepared=False): def get_db_prep_value(self, value, connection, prepared=False):
if not prepared: if not prepared:
value = self.get_prep_value(value) value = self.get_prep_value(value)
return connection.ops.value_to_db_ipaddress(value) return connection.ops.adapt_ipaddressfield_value(value)
def get_prep_value(self, value): def get_prep_value(self, value):
value = super(GenericIPAddressField, self).get_prep_value(value) value = super(GenericIPAddressField, self).get_prep_value(value)
@ -2266,7 +2266,7 @@ class TimeField(DateTimeCheckMixin, Field):
# Casts times into the format expected by the backend # Casts times into the format expected by the backend
if not prepared: if not prepared:
value = self.get_prep_value(value) value = self.get_prep_value(value)
return connection.ops.value_to_db_time(value) return connection.ops.adapt_timefield_value(value)
def value_to_string(self, obj): def value_to_string(self, obj):
val = self._get_val_from_obj(obj) val = self._get_val_from_obj(obj)

View File

@ -99,7 +99,7 @@ class RawQuery(object):
# Adapt parameters to the database, as much as possible considering # Adapt parameters to the database, as much as possible considering
# that the target type isn't known. See #17755. # that the target type isn't known. See #17755.
params_type = self.params_type params_type = self.params_type
adapter = connection.ops.value_to_db_unknown adapter = connection.ops.adapt_unknown_value
if params_type is tuple: if params_type is tuple:
params = tuple(adapter(val) for val in self.params) params = tuple(adapter(val) for val in self.params)
elif params_type is dict: elif params_type is dict:

View File

@ -323,6 +323,10 @@ Database backend API
in ``DatabaseOperations.get_db_converters()``. in ``DatabaseOperations.get_db_converters()``.
* The ``DatabaseOperations.value_to_db_<type>()`` methods were renamed to
``adapt_<type>field_value()`` to mirror the ``convert_<type>field_value()``
methods.
Default settings that were tuples are now lists Default settings that were tuples are now lists
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -35,6 +35,6 @@ class Article(models.Model):
SELECT id, headline, pub_date SELECT id, headline, pub_date
FROM custom_methods_article FROM custom_methods_article
WHERE pub_date = %s WHERE pub_date = %s
AND id != %s""", [connection.ops.value_to_db_date(self.pub_date), AND id != %s""", [connection.ops.adapt_datefield_value(self.pub_date),
self.id]) self.id])
return [self.__class__(*row) for row in cursor.fetchall()] return [self.__class__(*row) for row in cursor.fetchall()]