mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +00:00
magic-removal: Fixed #1423 -- Improved implementation of get_date_trunc_sql() for MySQL backend. Thanks, Geert Vanderkelen
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2735 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
dee1af5d8b
commit
08c28d9897
1
AUTHORS
1
AUTHORS
@ -106,6 +106,7 @@ answer newbie questions, and generally made Django that much better:
|
|||||||
Joe Topjian <http://joe.terrarum.net/geek/code/python/django/>
|
Joe Topjian <http://joe.terrarum.net/geek/code/python/django/>
|
||||||
Malcolm Tredinnick
|
Malcolm Tredinnick
|
||||||
Amit Upadhyay
|
Amit Upadhyay
|
||||||
|
Geert Vanderkelen
|
||||||
Milton Waddams
|
Milton Waddams
|
||||||
Rachel Willmer <http://www.willmer.com/kb/>
|
Rachel Willmer <http://www.willmer.com/kb/>
|
||||||
wojtek
|
wojtek
|
||||||
|
@ -124,18 +124,17 @@ def get_date_extract_sql(lookup_type, table_name):
|
|||||||
|
|
||||||
def get_date_trunc_sql(lookup_type, field_name):
|
def get_date_trunc_sql(lookup_type, field_name):
|
||||||
# lookup_type is 'year', 'month', 'day'
|
# lookup_type is 'year', 'month', 'day'
|
||||||
# http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
|
fields = ['year', 'month', 'day', 'hour', 'minute', 'second']
|
||||||
# MySQL doesn't support DATE_TRUNC, so we fake it by subtracting intervals.
|
format = ('%%Y-', '%%m', '-%%d', ' %%H:', '%%i', ':%%s') # Use double percents to escape.
|
||||||
# If you know of a better way to do this, please file a Django ticket.
|
format_def = ('0000-', '01', '-01', ' 00:', '00', ':00')
|
||||||
# Note that we can't use DATE_FORMAT directly because that causes the output
|
try:
|
||||||
# to be a string rather than a datetime object, and we need MySQL to return
|
i = fields.index(lookup_type) + 1
|
||||||
# a date so that it's typecasted properly into a Python datetime object.
|
except ValueError:
|
||||||
subtractions = ["interval (DATE_FORMAT(%s, '%%%%s')) second - interval (DATE_FORMAT(%s, '%%%%i')) minute - interval (DATE_FORMAT(%s, '%%%%H')) hour" % (field_name, field_name, field_name)]
|
sql = field_name
|
||||||
if lookup_type in ('year', 'month'):
|
else:
|
||||||
subtractions.append(" - interval (DATE_FORMAT(%s, '%%%%e')-1) day" % field_name)
|
format_str = ''.join([f for f in format[:i]] + [f for f in format_def[i:]])
|
||||||
if lookup_type == 'year':
|
sql = "CAST(DATE_FORMAT(%s, '%s') AS DATETIME)" % (field_name, format_str)
|
||||||
subtractions.append(" - interval (DATE_FORMAT(%s, '%%%%m')-1) month" % field_name)
|
return sql
|
||||||
return "(%s - %s)" % (field_name, ''.join(subtractions))
|
|
||||||
|
|
||||||
def get_limit_offset_sql(limit, offset=None):
|
def get_limit_offset_sql(limit, offset=None):
|
||||||
sql = "LIMIT "
|
sql = "LIMIT "
|
||||||
|
Loading…
x
Reference in New Issue
Block a user