From 54026f1e8d0de7dbd0e718fe326ba12666a8d2e9 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 2 May 2015 21:27:44 +0200 Subject: [PATCH] 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. --- django/core/cache/backends/db.py | 6 ++-- django/db/backends/base/operations.py | 28 +++++++++---------- django/db/backends/mysql/operations.py | 4 +-- django/db/backends/oracle/operations.py | 6 ++-- .../postgresql_psycopg2/operations.py | 8 +++--- django/db/backends/sqlite3/operations.py | 4 +-- django/db/models/fields/__init__.py | 10 +++---- django/db/models/sql/query.py | 2 +- docs/releases/1.9.txt | 4 +++ tests/custom_methods/models.py | 2 +- 10 files changed, 39 insertions(+), 35 deletions(-) diff --git a/django/core/cache/backends/db.py b/django/core/cache/backends/db.py index 6b9716b430..5c8ef834f1 100644 --- a/django/core/cache/backends/db.py +++ b/django/core/cache/backends/db.py @@ -136,7 +136,7 @@ class DatabaseCache(BaseDatabaseCache): current_expires = typecast_timestamp(str(current_expires)) if settings.USE_TZ: 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)): cursor.execute("UPDATE %s SET value = %%s, expires = %%s " "WHERE cache_key = %%s" % table, @@ -177,7 +177,7 @@ class DatabaseCache(BaseDatabaseCache): with connections[db].cursor() as cursor: cursor.execute("SELECT cache_key FROM %s " "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 def _cull(self, db, cursor, now): @@ -188,7 +188,7 @@ class DatabaseCache(BaseDatabaseCache): now = now.replace(tzinfo=None) table = connections[db].ops.quote_name(self._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) num = cursor.fetchone()[0] if num > self._max_entries: diff --git a/django/db/backends/base/operations.py b/django/db/backends/base/operations.py index 485abaa40d..198d62eb14 100644 --- a/django/db/backends/base/operations.py +++ b/django/db/backends/base/operations.py @@ -433,7 +433,7 @@ class BaseDatabaseOperations(object): """ 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. @@ -442,17 +442,17 @@ class BaseDatabaseOperations(object): As a consequence it may not work perfectly in all circumstances. """ 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): - return self.value_to_db_date(value) + return self.adapt_datefield_value(value) elif isinstance(value, datetime.time): - return self.value_to_db_time(value) + return self.adapt_timefield_value(value) elif isinstance(value, decimal.Decimal): - return self.value_to_db_decimal(value) + return self.adapt_decimalfield_value(value) else: 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 by the backend driver for date columns. @@ -461,7 +461,7 @@ class BaseDatabaseOperations(object): return None 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 by the backend driver for datetime columns. @@ -470,7 +470,7 @@ class BaseDatabaseOperations(object): return None 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 by the backend driver for time columns. @@ -481,14 +481,14 @@ class BaseDatabaseOperations(object): raise ValueError("Django does not support timezone-aware times.") 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 expected by the backend driver for decimal (numeric) columns. """ 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 type for the backend driver. @@ -505,8 +505,8 @@ class BaseDatabaseOperations(object): """ first = datetime.date(value, 1, 1) second = datetime.date(value, 12, 31) - first = self.value_to_db_date(first) - second = self.value_to_db_date(second) + first = self.adapt_datefield_value(first) + second = self.adapt_datefield_value(second) return [first, second] def year_lookup_bounds_for_datetime_field(self, value): @@ -523,8 +523,8 @@ class BaseDatabaseOperations(object): tz = timezone.get_current_timezone() first = timezone.make_aware(first, tz) second = timezone.make_aware(second, tz) - first = self.value_to_db_datetime(first) - second = self.value_to_db_datetime(second) + first = self.adapt_datetimefield_value(first) + second = self.adapt_datetimefield_value(second) return [first, second] def get_db_converters(self, expression): diff --git a/django/db/backends/mysql/operations.py b/django/db/backends/mysql/operations.py index 39fbf49c30..886e118fb0 100644 --- a/django/db/backends/mysql/operations.py +++ b/django/db/backends/mysql/operations.py @@ -138,7 +138,7 @@ class DatabaseOperations(BaseDatabaseOperations): 'value for AutoField.') return value - def value_to_db_datetime(self, value): + def adapt_datetimefield_value(self, value): if value is None: return None @@ -154,7 +154,7 @@ class DatabaseOperations(BaseDatabaseOperations): return six.text_type(value) - def value_to_db_time(self, value): + def adapt_timefield_value(self, value): if value is None: return None diff --git a/django/db/backends/oracle/operations.py b/django/db/backends/oracle/operations.py index 8b6dbf2fdc..fe95da1303 100644 --- a/django/db/backends/oracle/operations.py +++ b/django/db/backends/oracle/operations.py @@ -374,7 +374,7 @@ WHEN (new.%(col_name)s IS NULL) else: 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 by the backend driver for date columns. @@ -383,7 +383,7 @@ WHEN (new.%(col_name)s IS NULL) """ 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 by the backend driver for datetime columns. @@ -405,7 +405,7 @@ WHEN (new.%(col_name)s IS NULL) return Oracle_datetime.from_datetime(value) - def value_to_db_time(self, value): + def adapt_timefield_value(self, value): if value is None: return None diff --git a/django/db/backends/postgresql_psycopg2/operations.py b/django/db/backends/postgresql_psycopg2/operations.py index df96d16ad2..65d6c9154b 100644 --- a/django/db/backends/postgresql_psycopg2/operations.py +++ b/django/db/backends/postgresql_psycopg2/operations.py @@ -225,16 +225,16 @@ class DatabaseOperations(BaseDatabaseOperations): items_sql = "(%s)" % ", ".join(["%s"] * len(fields)) return "VALUES " + ", ".join([items_sql] * num_values) - def value_to_db_date(self, value): + def adapt_datefield_value(self, value): return value - def value_to_db_datetime(self, value): + def adapt_datetimefield_value(self, value): return value - def value_to_db_time(self, value): + def adapt_timefield_value(self, value): return value - def value_to_db_ipaddress(self, value): + def adapt_ipaddressfield_value(self, value): if value: return Inet(value) return None diff --git a/django/db/backends/sqlite3/operations.py b/django/db/backends/sqlite3/operations.py index da99b6b217..1e5b2759af 100644 --- a/django/db/backends/sqlite3/operations.py +++ b/django/db/backends/sqlite3/operations.py @@ -113,7 +113,7 @@ class DatabaseOperations(BaseDatabaseOperations): # sql_flush() implementations). Just return SQL at this point return sql - def value_to_db_datetime(self, value): + def adapt_datetimefield_value(self, value): if value is None: return None @@ -126,7 +126,7 @@ class DatabaseOperations(BaseDatabaseOperations): return six.text_type(value) - def value_to_db_time(self, value): + def adapt_timefield_value(self, value): if value is None: return None diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 78e74b7850..f9ae55f844 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -1295,7 +1295,7 @@ class DateField(DateTimeCheckMixin, Field): # Casts dates into the format expected by the backend if not prepared: 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): val = self._get_val_from_obj(obj) @@ -1455,7 +1455,7 @@ class DateTimeField(DateField): # Casts datetimes into the format expected by the backend if not prepared: 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): 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) 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) def get_prep_value(self, value): @@ -1976,7 +1976,7 @@ class GenericIPAddressField(Field): def get_db_prep_value(self, value, connection, prepared=False): if not prepared: 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): 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 if not prepared: 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): val = self._get_val_from_obj(obj) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 52553ddfa1..776f06a7a8 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -99,7 +99,7 @@ class RawQuery(object): # Adapt parameters to the database, as much as possible considering # that the target type isn't known. See #17755. params_type = self.params_type - adapter = connection.ops.value_to_db_unknown + adapter = connection.ops.adapt_unknown_value if params_type is tuple: params = tuple(adapter(val) for val in self.params) elif params_type is dict: diff --git a/docs/releases/1.9.txt b/docs/releases/1.9.txt index f0c8a345bc..41dadd295c 100644 --- a/docs/releases/1.9.txt +++ b/docs/releases/1.9.txt @@ -323,6 +323,10 @@ Database backend API in ``DatabaseOperations.get_db_converters()``. +* The ``DatabaseOperations.value_to_db_()`` methods were renamed to + ``adapt_field_value()`` to mirror the ``convert_field_value()`` + methods. + Default settings that were tuples are now lists ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/custom_methods/models.py b/tests/custom_methods/models.py index e21f0a6bd4..7def1d6017 100644 --- a/tests/custom_methods/models.py +++ b/tests/custom_methods/models.py @@ -35,6 +35,6 @@ class Article(models.Model): SELECT id, headline, pub_date FROM custom_methods_article 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]) return [self.__class__(*row) for row in cursor.fetchall()]