1
0
mirror of https://github.com/django/django.git synced 2025-07-05 10:19:20 +00:00

[boulder-oracle-sprint] Avoided string conversion for DateTimeField in oracle

git-svn-id: http://code.djangoproject.com/svn/django/branches/boulder-oracle-sprint@4008 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Boulder Sprinters 2006-11-05 00:13:09 +00:00
parent 8b1f56d06b
commit a12bdeb445

View File

@ -159,9 +159,9 @@ class Field(object):
def get_db_prep_save(self, value): def get_db_prep_save(self, value):
"Returns field's value prepared for saving into a database." "Returns field's value prepared for saving into a database."
# Oracle treats empty strings ('') the same as NULLs. # Oracle treats empty strings ('') the same as NULLs.
# To get around this wart, we need to change it to something else... # To get around this wart, we need to change it to something else...
if settings.DATABASE_ENGINE == 'oracle' and value == '': if settings.DATABASE_ENGINE == 'oracle' and value == '':
value = ' ' value = ' '
return value return value
@ -502,14 +502,17 @@ class DateTimeField(DateField):
# doesn't support microseconds. # doesn't support microseconds.
if (settings.DATABASE_ENGINE == 'mysql' or settings.DATABASE_ENGINE=='oracle') and hasattr(value, 'microsecond'): if (settings.DATABASE_ENGINE == 'mysql' or settings.DATABASE_ENGINE=='oracle') and hasattr(value, 'microsecond'):
value = value.replace(microsecond=0) value = value.replace(microsecond=0)
value = str(value) # cx_Oracle wants the raw datetime instead of a string
if settings.DATABASE_ENGINE != 'oracle':
value = str(value)
elif isinstance(value, datetime.date): elif isinstance(value, datetime.date):
# MySQL/Oracle will throw a warning if microseconds are given, because it # MySQL/Oracle will throw a warning if microseconds are given, because it
# doesn't support microseconds. # doesn't support microseconds.
if (settings.DATABASE_ENGINE == 'mysql' or settings.DATABASE_ENGINE=='oracle') and hasattr(value, 'microsecond'): if (settings.DATABASE_ENGINE == 'mysql' or settings.DATABASE_ENGINE=='oracle') and hasattr(value, 'microsecond'):
value = datetime.datetime(value.year, value.month, value.day, microsecond=0) value = datetime.datetime(value.year, value.month, value.day, microsecond=0)
value = str(value) # cx_Oracle wants the raw datetime instead of a string
if settings.DATABASE_ENGINE != 'oracle':
value = str(value)
return Field.get_db_prep_save(self, value) return Field.get_db_prep_save(self, value)
def get_db_prep_lookup(self, lookup_type, value): def get_db_prep_lookup(self, lookup_type, value):
@ -544,12 +547,12 @@ class DateTimeField(DateField):
def flatten_data(self,follow, obj = None): def flatten_data(self,follow, obj = None):
val = self._get_val_from_obj(obj) val = self._get_val_from_obj(obj)
date_field, time_field = self.get_manipulator_field_names('') date_field, time_field = self.get_manipulator_field_names('')
#cx_Oracle does not support strftime #cx_Oracle does not support strftime
if (settings.DATABASE_ENGINE=='oracle'): if (settings.DATABASE_ENGINE=='oracle'):
return {date_field: (val is not None or ''), return {date_field: (val is not None or ''),
time_field: (val is not None or '')} time_field: (val is not None or '')}
else: else:
return {date_field: (val is not None and val.strftime("%Y-%m-%d") or ''), return {date_field: (val is not None and val.strftime("%Y-%m-%d") or ''),
time_field: (val is not None and val.strftime("%H:%M:%S") or '')} time_field: (val is not None and val.strftime("%H:%M:%S") or '')}
class EmailField(CharField): class EmailField(CharField):
@ -782,12 +785,12 @@ class TimeField(Field):
# doesn't support microseconds. # doesn't support microseconds.
if settings.DATABASE_ENGINE == 'mysql': if settings.DATABASE_ENGINE == 'mysql':
value = value.replace(microsecond=0) value = value.replace(microsecond=0)
value = str(value) value = str(value)
elif settings.DATABASE_ENGINE == 'oracle': elif settings.DATABASE_ENGINE == 'oracle':
value = value.replace(microsecond=0) value = value.replace(microsecond=0)
# cx_Oracle expects a datetime.datetime to persist into TIMESTAMP field. # cx_Oracle expects a datetime.datetime to persist into TIMESTAMP field.
value = datetime.datetime(1900, 1, 1, value.hour, value.minute, value.second) value = datetime.datetime(1900, 1, 1, value.hour, value.minute, value.second)
else: else:
value = str(value) value = str(value)
return Field.get_db_prep_save(self, value) return Field.get_db_prep_save(self, value)