Django

Code

Ticket #659: sqlite3.py.patch

File sqlite3.py.patch, 1.5 kB (added by pgross, 3 years ago)

This isn't fully tested, but this patch fixed my bug. Basically, instead of using the django_extract function I just use strftime to extract the year/month/day.

  • sqlite3.py

    old new  
    3434        if self.connection is None: 
    3535            self.connection = Database.connect(DATABASE_NAME, detect_types=Database.PARSE_DECLTYPES) 
    3636            # register extract and date_trun functions 
    37             self.connection.create_function("django_extract", 2, _sqlite_extract) 
    3837            self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) 
    3938        cursor = self.connection.cursor(factory=SQLiteCursorWrapper) 
    4039        cursor.row_factory = utf8rowFactory 
     
    8180 
    8281def get_date_extract_sql(lookup_type, table_name): 
    8382    # lookup_type is 'year', 'month', 'day' 
    84     # sqlite doesn't support extract, so we fake it with the user-defined 
    85     # function _sqlite_extract that's registered in connect(), above. 
    86     return 'django_extract("%s", %s)' % (lookup_type.lower(), table_name) 
     83    # sqlite doesn't support extract, so we fake it with the strftime function 
     84    date_codes = {'year': '"%%Y"', 'month': '"%%m"', 'day': '"%%d"'} 
     85    return 'strftime(%s, %s)' % (date_codes[lookup_type], table_name) 
    8786 
    88 def _sqlite_extract(lookup_type, dt): 
    89     try: 
    90         dt = typecasts.typecast_timestamp(dt) 
    91     except (ValueError, TypeError): 
    92         return None 
    93     return str(getattr(dt, lookup_type)) 
    94  
    9587def get_date_trunc_sql(lookup_type, field_name): 
    9688    # lookup_type is 'year', 'month', 'day' 
    9789    # sqlite doesn't support DATE_TRUNC, so we fake it as above.