#1423 closed enhancement (fixed)
[patch] MySQL get_data_trunc_sql using DATE_FORMAT() instead
| Reported by: | Owned by: | Adrian Holovaty | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | magic-removal |
| Severity: | normal | Keywords: | mysql date_trunc get_date_trunc_sql |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Hi!
My first hour Django, but here is my first small contribution. Hope this helps!
This is get_date_trunc_sql using DATE_FORMAT() instead of the INTERVAL. Since we don't have DATE_TRUNC() (yet??) in MySQL, I think this might just do the trick.
Btw, I used the same lookup_type as for PostgreSQL's DATE_TRUNC() function.
def get_date_trunc_sql(lookup_type, field_name):
# Fieldnames used like PostgreSQL's date_trunc() function
fields = [ 'year','month','day','hour','minute','second']
# Default format: %Y-%m-%d %H-%i-%s, and the corresponding 'null' values
format = [ '%Y-','%m','-%d',' %H:','%i',':%s']
format_def = [ '0000-','01','-01',' 00:','00',':00']
sql = ''
try:
i = fields.index(lookup_type) + 1
except ValueError:
# Could not find the type, so lets just do nothing!
sql = field_name
else:
str = ''
# Loop over the format
for f in format[:i]:
str = str + f
# Get the part which we need to truncate
for f in format_def[i:]:
str = str + f
# Use DATE_FORMAT() with the field and the format
sql = "DATE_FORMAT(%s,'%s')" % (field_name, str)
return sql
sql = get_date_trunc_sql('day', "'2005-02-28 19:23:45'")
print "SELECT " + sql;
Thanks!
Geert
Attachments (1)
Change History (9)
comment:1 by , 20 years ago
| Summary: | MySQL get_data_trunc_sql using DATE_FORMAT() instead → [patch] MySQL get_data_trunc_sql using DATE_FORMAT() instead |
|---|
comment:2 by , 20 years ago
comment:3 by , 20 years ago
comment:4 by , 20 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
Thanks for this patch, but it won't do exactly what we need. It formats the date properly, but the date formatting returns a string rather than a date object. And Django needs this function to return a date/datetime object so that the typecasting will work correctly (i.e., so the result will be converted to a Python datetime object rather than a string).
comment:5 by , 20 years ago
| Resolution: | invalid |
|---|---|
| Status: | closed → reopened |
Understood.. and this is closed, but here is the solution then.
We just need a CAST( .. AS DATETIME) around DATE_FORMAT()
sql = "CAST(DATE_FORMAT(%s,'%s') AS DATETIME)" % (field_name, str)
This returns then a datetime object, like:
SELECT CAST(DATE_FORMAT('2005-02-28 19:23:45','%Y-%m-%d 00:00:00') AS DATETIME)
(datetime.datetime(2005, 2, 28, 0, 0),)
by , 20 years ago
| Attachment: | mysql_date_trunc.diff added |
|---|
svn diff against the magic-removal branch
comment:6 by , 19 years ago
| Triage Stage: | Unreviewed → Design decision needed |
|---|
Can someone please review the new patch.
comment:7 by , 19 years ago
Not sure what's still needed here, but my patch made it (though a bit altered) in the SVN or I'm using a bad branch..
(why are email addresses shown here??)
comment:8 by , 19 years ago
| Resolution: | → fixed |
|---|---|
| Status: | reopened → closed |
Ok, thanks. The last bit of code for this ticket happened in [2735].
(In [2449]) Added model unit tests for get_DATEFIELD_list(). Refs #1423