Django

Code

Ticket #1423 (closed: fixed)

Opened 3 years ago

Last modified 1 year ago

[patch] MySQL get_data_trunc_sql using DATE_FORMAT() instead

Reported by: Geert Vanderkelen <geert@kemuri.org> Assigned to: adrian
Milestone: Component: Database layer (models, ORM)
Version: magic-removal Keywords: mysql date_trunc get_date_trunc_sql
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

mysql_date_trunc.diff (1.8 kB) - added by Geert Vanderkelen <geert@kemuri.org> on 03/08/06 18:45:13.
svn diff against the magic-removal branch

Change History

02/28/06 19:00:32 changed by anonymous

  • summary changed from MySQL get_data_trunc_sql using DATE_FORMAT() instead to [patch] MySQL get_data_trunc_sql using DATE_FORMAT() instead.

02/28/06 21:12:25 changed by adrian

(In [2449]) Added model unit tests for get_DATEFIELD_list(). Refs #1423

02/28/06 21:17:25 changed by adrian

(In [2450]) Added note to mysql backend about DATE_FORMAT not working. Refs #1423

02/28/06 21:18:48 changed by adrian

  • status changed from new to closed.
  • resolution set to invalid.

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).

03/01/06 05:44:56 changed by Geert Vanderkelen <geert@kemuri.org>

  • status changed from closed to reopened.
  • resolution deleted.

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),)

03/08/06 18:45:13 changed by Geert Vanderkelen <geert@kemuri.org>

  • attachment mysql_date_trunc.diff added.

svn diff against the magic-removal branch

01/24/07 21:33:26 changed by Gary Wilson <gary.wilson@gmail.com>

  • stage changed from Unreviewed to Design decision needed.

Can someone please review the new patch.

01/25/07 03:40:13 changed by geert@kemuri.org

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??)

01/25/07 07:40:15 changed by Gary Wilson <gary.wilson@gmail.com>

  • status changed from reopened to closed.
  • resolution set to fixed.

Ok, thanks. The last bit of code for this ticket happened in [2735].


Add/Change #1423 ([patch] MySQL get_data_trunc_sql using DATE_FORMAT() instead)




Change Properties
Action