Ticket #1468: sqlite.patch

File sqlite.patch, 3.0 KB (added by mildly@…, 19 years ago)
  • django/core/db/backends/sqlite3.py

     
    3333        from django.conf.settings import DATABASE_NAME, DEBUG
    3434        if self.connection is None:
    3535            self.connection = Database.connect(DATABASE_NAME, detect_types=Database.PARSE_DECLTYPES)
    36             # register extract and date_trun functions
    37             self.connection.create_function("django_extract", 2, _sqlite_extract)
    38             self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
    3936        cursor = self.connection.cursor(factory=SQLiteCursorWrapper)
    4037        cursor.row_factory = utf8rowFactory
    4138        if DEBUG:
     
    8885    # lookup_type is 'year', 'month', 'day'
    8986    # sqlite doesn't support extract, so we fake it with the user-defined
    9087    # function _sqlite_extract that's registered in connect(), above.
    91     return 'django_extract("%s", %s)' % (lookup_type.lower(), table_name)
     88    strfvals = {
     89        'year': '%%Y',
     90        'month': '%%m',
     91        'day': '%%d',
     92    }
     93    return 'strftime( %s, "%s" ) ' % ( table_name, strfvals[lookup_type.lower()] )
    9294
    93 def _sqlite_extract(lookup_type, dt):
    94     try:
    95         dt = typecasts.typecast_timestamp(dt)
    96     except (ValueError, TypeError):
    97         return None
    98     return str(getattr(dt, lookup_type))
    99 
    10095def get_date_trunc_sql(lookup_type, field_name):
    10196    # lookup_type is 'year', 'month', 'day'
    10297    # sqlite doesn't support DATE_TRUNC, so we fake it as above.
    103     return 'django_date_trunc("%s", %s)' % (lookup_type.lower(), field_name)
     98    return 'strftime( "%%%%Y-%%%%m-%%%%d 00:00:00", %s, "start of %s" )' % (field_name, lookup_type.lower())
    10499
    105100def get_limit_offset_sql(limit, offset=None):
    106101    sql = "LIMIT %s" % limit
     
    111106def get_random_function_sql():
    112107    return "RANDOM()"
    113108
    114 def _sqlite_date_trunc(lookup_type, dt):
    115     try:
    116         dt = typecasts.typecast_timestamp(dt)
    117     except (ValueError, TypeError):
    118         return None
    119     if lookup_type == 'year':
    120         return "%i-01-01 00:00:00" % dt.year
    121     elif lookup_type == 'month':
    122         return "%i-%02i-01 00:00:00" % (dt.year, dt.month)
    123     elif lookup_type == 'day':
    124         return "%i-%02i-%02i 00:00:00" % (dt.year, dt.month, dt.day)
    125 
    126109def get_table_list(cursor):
    127110    cursor.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
    128111    return [row[0] for row in cursor.fetchall()]
  • django/core/meta/fields.py

     
    389389
    390390    def get_db_prep_lookup(self, lookup_type, value):
    391391        if lookup_type == 'range':
    392             value = [str(v) for v in value]
     392            value = [v.strftime("%Y-%m-%d") for v in value]
    393393        else:
    394394            value = str(value)
    395395        return Field.get_db_prep_lookup(self, lookup_type, value)
Back to Top