Django

Code

Changeset 7643

Show
Ignore:
Timestamp:
06/15/08 22:15:04 (5 months ago)
Author:
adrian
Message:

Fixed #7420 -- Abstracted some more database options into DatabaseFeatures? -- supports_usecs, time_field_needs_date, interprets_empty_strings_as_nulls and date_field_supports_time_value -- and changed various hard-coded 'if DATABASE_BACKEND == oracle' statements to use the new options. Thanks to ramiro for the patch

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/backends/__init__.py

    r7624 r7643  
    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): 
     
    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        return smart_unicode(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_") 
  • django/trunk/django/db/backends/mysql/base.py

    r7496 r7643  
    6565    empty_fetchmany_value = () 
    6666    update_can_self_select = False 
     67    supports_usecs = False 
    6768 
    6869class DatabaseOperations(BaseDatabaseOperations): 
  • django/trunk/django/db/backends/mysql_old/base.py

    r7496 r7643  
    6969    empty_fetchmany_value = () 
    7070    update_can_self_select = False 
     71    supports_usecs = False 
    7172 
    7273class DatabaseOperations(BaseDatabaseOperations): 
  • django/trunk/django/db/backends/oracle/base.py

    r7477 r7643  
    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): 
  • django/trunk/django/db/models/fields/__init__.py

    r7494 r7643  
    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 
     
    3434BLANK_CHOICE_NONE = [("", "None")] 
    3535 
    36 # prepares a value for use in a LIKE query 
    37 prep_for_like_query = lambda x: smart_unicode(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_") 
    38  
    3936# returns the <ul> class for a given radio_admin value 
    4037get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '') 
     
    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 
     
    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 [] 
     
    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' 
     
    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 "" 
     
    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) 
     
    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 = { 
     
    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): 
     
    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):