Ticket #20292: issue-20292.diff

File issue-20292.diff, 3.1 KB (added by Jani Tiainen, 11 years ago)
  • django/db/backends/oracle/base.py

    diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py
    index 4ffe708..875bbbc 100644
    a b class DatabaseFeatures(BaseDatabaseFeatures):  
    8787    supports_tablespaces = True
    8888    supports_sequence_reset = False
    8989
     90class OracleDateTimeAdapter(datetime.datetime):
     91    input_size = cx_Oracle.TIMESTAMP
     92
    9093class DatabaseOperations(BaseDatabaseOperations):
    9194    compiler_module = "django.db.backends.oracle.compiler"
    9295
    WHEN (new.%(col_name)s IS NULL)  
    358361        else:
    359362            return "TABLESPACE %s" % self.quote_name(tablespace)
    360363
     364    def value_to_db_date(self, value):
     365        """
     366        Transform a date value to an object compatible with what is expected
     367        by the backend driver for date columns.
     368        """
     369        if value is None:
     370            return None
     371        return value
     372       
    361373    def value_to_db_datetime(self, value):
     374        """
     375        Transform a datetime value to an object compatible with what is expected
     376        by the backend driver for datetime columns.
     377       
     378        If naive datetime is passed assumes that is in UTC. Normally Django
     379        models.DateTimeField makes sure that if USE_TZ is True passed datetime
     380        is timezone aware.
     381        """
     382       
    362383        if value is None:
    363384            return None
    364 
    365         # Oracle doesn't support tz-aware datetimes
    366         if timezone.is_aware(value):
    367             if settings.USE_TZ:
    368                 value = value.astimezone(timezone.utc).replace(tzinfo=None)
     385       
     386        if settings.USE_TZ:
     387            if timezone.is_aware(value):
     388                value = value.astimezone(timezone.utc)
    369389            else:
    370                 raise ValueError("Oracle backend does not support timezone-aware datetimes when USE_TZ is False.")
    371 
    372         return six.text_type(value)
    373 
     390                # Assume that naive datetime is actually in UTC,
     391                value = value.replace(tzinfo=timezone.utc)
     392        elif timezone.is_aware(value):
     393            raise ValueError("Oracle backend does not support timezone-aware datetimes when USE_TZ is False.")
     394                   
     395       
     396        value = OracleDateTimeAdapter(value.year, value.month, value.day,
     397                               value.hour, value.minute, value.second,
     398                               value.microsecond, value.tzinfo)
     399       
     400        return value
     401       
    374402    def value_to_db_time(self, value):
    375403        if value is None:
    376404            return None
    WHEN (new.%(col_name)s IS NULL)  
    386414                                 value.second, value.microsecond)
    387415
    388416    def year_lookup_bounds_for_date_field(self, value):
    389         first = '%s-01-01'
    390         second = '%s-12-31'
    391         return [first % value, second % value]
     417        # Create bounds as real date values
     418        first = datetime.date(value, 1, 1)
     419        last = datetime.date(value, 12, 31)
     420        return [first, last]
    392421
    393422    def combine_expression(self, connector, sub_expressions):
    394423        "Oracle requires special cases for %% and & operators in query expressions"
Back to Top