Ticket #7420: db-be-features-r7583.diff

File db-be-features-r7583.diff, 9.2 KB (added by ramiro, 7 years ago)
  • django/contrib/sessions/backends/db.py

    diff -r d7d4f0dccb5e django/contrib/sessions/backends/db.py
    a b from django.contrib.sessions.models impo 
    22from django.contrib.sessions.models import Session
    33from django.contrib.sessions.backends.base import SessionBase
    44from django.core.exceptions import SuspiciousOperation
     5from django.db import connection
    56import datetime
    67
    78class SessionStore(SessionBase):
    class SessionStore(SessionBase): 
    1314
    1415    def load(self):
    1516        try:
     17            datetime_now = datetime.datetime.now()
     18            if connection.features.supports_usecs and hasattr(datetime_now, 'microsecond'):
     19                datetime_now = datetime_now.replace(microsecond=0)
    1620            s = Session.objects.get(
    1721                session_key = self.session_key,
    18                 expire_date__gt=datetime.datetime.now()
     22                expire_date__gt=datetime_now
    1923            )
    2024            return self.decode(s.session_data)
    2125        except (Session.DoesNotExist, SuspiciousOperation):
  • django/db/backends/__init__.py

    diff -r d7d4f0dccb5e django/db/backends/__init__.py
    a b class BaseDatabaseFeatures(object): 
    5252    uses_custom_query_class = False
    5353    empty_fetchmany_value = []
    5454    update_can_self_select = True
     55    supports_usecs = True
     56    time_field_needs_date = False
     57    interprets_empty_strings_as_nulls = False
     58    date_field_supports_time_value = True
    5559
    5660class BaseDatabaseOperations(object):
    5761    """
    class BaseDatabaseOperations(object): 
    266270        tablespaces.
    267271        """
    268272        return None
     273
     274    def prep_for_like_query(self, x):
     275        """Prepares a value for use in a LIKE query."""
     276        from django.utils.encoding import smart_unicode
     277
     278        return smart_unicode(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_")
  • django/db/backends/mysql/base.py

    diff -r d7d4f0dccb5e django/db/backends/mysql/base.py
    a b class DatabaseFeatures(BaseDatabaseFeatu 
    6464    inline_fk_references = False
    6565    empty_fetchmany_value = ()
    6666    update_can_self_select = False
     67    supports_usecs = False
    6768
    6869class DatabaseOperations(BaseDatabaseOperations):
    6970    def date_extract_sql(self, lookup_type, field_name):
  • django/db/backends/mysql_old/base.py

    diff -r d7d4f0dccb5e django/db/backends/mysql_old/base.py
    a b class DatabaseFeatures(BaseDatabaseFeatu 
    6868    inline_fk_references = False
    6969    empty_fetchmany_value = ()
    7070    update_can_self_select = False
     71    supports_usecs = False
    7172
    7273class DatabaseOperations(BaseDatabaseOperations):
    7374    def date_extract_sql(self, lookup_type, field_name):
  • django/db/backends/oracle/base.py

    diff -r d7d4f0dccb5e django/db/backends/oracle/base.py
    a b class DatabaseFeatures(BaseDatabaseFeatu 
    3131    supports_tablespaces = True
    3232    uses_case_insensitive_names = True
    3333    uses_custom_query_class = True
     34    time_field_needs_date = True
     35    interprets_empty_strings_as_nulls = True
     36    date_field_supports_time_value = False
    3437
    3538class DatabaseOperations(BaseDatabaseOperations):
    3639    def autoinc_sql(self, table, column):
  • django/db/models/fields/__init__.py

    diff -r d7d4f0dccb5e django/db/models/fields/__init__.py
    a b except ImportError: 
    77except ImportError:
    88    from django.utils import _decimal as decimal    # for Python 2.3
    99
    10 from django.db import get_creation_module
     10from django.db import connection, get_creation_module
    1111from django.db.models import signals
    1212from django.db.models.query_utils import QueryWrapper
    1313from django.dispatch import dispatcher
    HORIZONTAL, VERTICAL = 1, 2 
    3232# The values to use for "blank" in SelectFields. Will be appended to the start of most "choices" lists.
    3333BLANK_CHOICE_DASH = [("", "---------")]
    3434BLANK_CHOICE_NONE = [("", "None")]
    35 
    36 # prepares a value for use in a LIKE query
    37 prep_for_like_query = lambda x: smart_unicode(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_")
    3835
    3936# returns the <ul> class for a given radio_admin value
    4037get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '')
    class Field(object): 
    9794        self.blank, self.null = blank, null
    9895        # Oracle treats the empty string ('') as null, so coerce the null
    9996        # option whenever '' is a possible value.
    100         if self.empty_strings_allowed and settings.DATABASE_ENGINE == 'oracle':
     97        if self.empty_strings_allowed and connection.features.interprets_empty_strings_as_nulls:
    10198            self.null = True
    10299        self.core, self.rel, self.default = core, rel, default
    103100        self.editable = editable
    class Field(object): 
    235232        elif lookup_type in ('range', 'in'):
    236233            return value
    237234        elif lookup_type in ('contains', 'icontains'):
    238             return ["%%%s%%" % prep_for_like_query(value)]
     235            return ["%%%s%%" % connection.ops.prep_for_like_query(value)]
    239236        elif lookup_type == 'iexact':
    240             return [prep_for_like_query(value)]
     237            return [connection.ops.prep_for_like_query(value)]
    241238        elif lookup_type in ('startswith', 'istartswith'):
    242             return ["%s%%" % prep_for_like_query(value)]
     239            return ["%s%%" % connection.ops.prep_for_like_query(value)]
    243240        elif lookup_type in ('endswith', 'iendswith'):
    244             return ["%%%s" % prep_for_like_query(value)]
     241            return ["%%%s" % connection.ops.prep_for_like_query(value)]
    245242        elif lookup_type == 'isnull':
    246243            return []
    247244        elif lookup_type == 'year':
    class Field(object): 
    252249            if settings.DATABASE_ENGINE == 'sqlite3':
    253250                first = '%s-01-01'
    254251                second = '%s-12-31 23:59:59.999999'
    255             elif settings.DATABASE_ENGINE == 'oracle' and self.get_internal_type() == 'DateField':
     252            elif not connection.features.date_field_supports_time_value and self.get_internal_type() == 'DateField':
    256253                first = '%s-01-01'
    257254                second = '%s-12-31'
     255            elif not connection.features.supports_usecs:
     256                first = '%s-01-01 00:00:00'
     257                second = '%s-12-31 23:59:59.99'
    258258            else:
    259259                first = '%s-01-01 00:00:00'
    260260                second = '%s-12-31 23:59:59.999999'
    class Field(object): 
    271271            if callable(self.default):
    272272                return self.default()
    273273            return force_unicode(self.default, strings_only=True)
    274         if not self.empty_strings_allowed or (self.null and settings.DATABASE_ENGINE != 'oracle'):
     274        if not self.empty_strings_allowed or (self.null and not connection.features.interprets_empty_strings_as_nulls):
    275275            return None
    276276        return ""
    277277
    class DateTimeField(DateField): 
    629629        if value is not None:
    630630            # MySQL will throw a warning if microseconds are given, because it
    631631            # doesn't support microseconds.
    632             if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'):
     632            if not connection.features.supports_usecs and hasattr(value, 'microsecond'):
    633633                value = value.replace(microsecond=0)
    634634            value = smart_unicode(value)
    635635        return Field.get_db_prep_save(self, value)
    class FilePathField(Field): 
    863863        self.path, self.match, self.recursive = path, match, recursive
    864864        kwargs['max_length'] = kwargs.get('max_length', 100)
    865865        Field.__init__(self, verbose_name, name, **kwargs)
    866    
     866
    867867    def formfield(self, **kwargs):
    868868        defaults = {
    869869            'path': self.path,
    class TimeField(Field): 
    10711071        return "TimeField"
    10721072
    10731073    def get_db_prep_lookup(self, lookup_type, value):
    1074         if settings.DATABASE_ENGINE == 'oracle':
     1074        if connection.features.time_field_needs_date:
    10751075            # Oracle requires a date in order to parse.
    10761076            def prep(value):
    10771077                if isinstance(value, datetime.time):
    class TimeField(Field): 
    10981098        if value is not None:
    10991099            # MySQL will throw a warning if microseconds are given, because it
    11001100            # doesn't support microseconds.
    1101             if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'):
     1101            if not connection.features.supports_usecs and hasattr(value, 'microsecond'):
    11021102                value = value.replace(microsecond=0)
    1103             if settings.DATABASE_ENGINE == 'oracle':
     1103            if connection.features.time_field_needs_date:
    11041104                # cx_Oracle expects a datetime.datetime to persist into TIMESTAMP field.
    11051105                if isinstance(value, datetime.time):
    11061106                    value = datetime.datetime(1900, 1, 1, value.hour, value.minute,
Back to Top