Ticket #1468: sqlite.patch
File sqlite.patch, 3.0 KB (added by , 19 years ago) |
---|
-
django/core/db/backends/sqlite3.py
33 33 from django.conf.settings import DATABASE_NAME, DEBUG 34 34 if self.connection is None: 35 35 self.connection = Database.connect(DATABASE_NAME, detect_types=Database.PARSE_DECLTYPES) 36 # register extract and date_trun functions37 self.connection.create_function("django_extract", 2, _sqlite_extract)38 self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)39 36 cursor = self.connection.cursor(factory=SQLiteCursorWrapper) 40 37 cursor.row_factory = utf8rowFactory 41 38 if DEBUG: … … 88 85 # lookup_type is 'year', 'month', 'day' 89 86 # sqlite doesn't support extract, so we fake it with the user-defined 90 87 # 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()] ) 92 94 93 def _sqlite_extract(lookup_type, dt):94 try:95 dt = typecasts.typecast_timestamp(dt)96 except (ValueError, TypeError):97 return None98 return str(getattr(dt, lookup_type))99 100 95 def get_date_trunc_sql(lookup_type, field_name): 101 96 # lookup_type is 'year', 'month', 'day' 102 97 # 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()) 104 99 105 100 def get_limit_offset_sql(limit, offset=None): 106 101 sql = "LIMIT %s" % limit … … 111 106 def get_random_function_sql(): 112 107 return "RANDOM()" 113 108 114 def _sqlite_date_trunc(lookup_type, dt):115 try:116 dt = typecasts.typecast_timestamp(dt)117 except (ValueError, TypeError):118 return None119 if lookup_type == 'year':120 return "%i-01-01 00:00:00" % dt.year121 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 126 109 def get_table_list(cursor): 127 110 cursor.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name") 128 111 return [row[0] for row in cursor.fetchall()] -
django/core/meta/fields.py
389 389 390 390 def get_db_prep_lookup(self, lookup_type, value): 391 391 if lookup_type == 'range': 392 value = [ str(v) for v in value]392 value = [v.strftime("%Y-%m-%d") for v in value] 393 393 else: 394 394 value = str(value) 395 395 return Field.get_db_prep_lookup(self, lookup_type, value)