Django

Code

Changeset 5952

Show
Ignore:
Timestamp:
08/19/07 17:47:43 (1 year ago)
Author:
adrian
Message:

Refactored get_date_trunc_sql() to DatabaseOperations?.date_trunc_sql(). Refs #5106

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/backends/ado_mssql/base.py

    r5951 r5952  
    5353        return "DATEPART(%s, %s)" % (lookup_type, field_name) 
    5454 
     55    def date_trunc_sql(self, lookup_type, field_name): 
     56        if lookup_type == 'year': 
     57            return "Convert(datetime, Convert(varchar, DATEPART(year, %s)) + '/01/01')" % field_name 
     58        if lookup_type == 'month': 
     59            return "Convert(datetime, Convert(varchar, DATEPART(year, %s)) + '/' + Convert(varchar, DATEPART(month, %s)) + '/01')" % (field_name, field_name) 
     60        if lookup_type == 'day': 
     61            return "Convert(datetime, Convert(varchar(12), %s))" % field_name 
     62 
    5563class DatabaseWrapper(BaseDatabaseWrapper): 
    5664    ops = DatabaseOperations() 
     
    8997    cursor.execute("SELECT %s FROM %s WHERE %s = @@IDENTITY" % (pk_name, table_name, pk_name)) 
    9098    return cursor.fetchone()[0] 
    91  
    92 def get_date_trunc_sql(lookup_type, field_name): 
    93     # lookup_type is 'year', 'month', 'day' 
    94     if lookup_type=='year': 
    95         return "Convert(datetime, Convert(varchar, DATEPART(year, %s)) + '/01/01')" % field_name 
    96     if lookup_type=='month': 
    97         return "Convert(datetime, Convert(varchar, DATEPART(year, %s)) + '/' + Convert(varchar, DATEPART(month, %s)) + '/01')" % (field_name, field_name) 
    98     if lookup_type=='day': 
    99         return "Convert(datetime, Convert(varchar(12), %s))" % field_name 
    10099 
    101100def get_datetime_cast_sql(): 
  • django/trunk/django/db/backends/dummy/base.py

    r5951 r5952  
    4545dictfetchall = complain 
    4646get_last_insert_id = complain 
    47 get_date_trunc_sql = complain 
    4847get_datetime_cast_sql = complain 
    4948get_limit_offset_sql = complain 
  • django/trunk/django/db/backends/__init__.py

    r5951 r5952  
    6161        """ 
    6262        raise NotImplementedError() 
     63 
     64    def date_trunc_sql(self, lookup_type, field_name): 
     65        """ 
     66        Given a lookup_type of 'year', 'month' or 'day', returns the SQL that 
     67        truncates the given date field field_name to a DATE object with only 
     68        the given specificity. 
     69        """ 
     70        raise NotImplementedError() 
  • django/trunk/django/db/backends/mysql/base.py

    r5951 r5952  
    5858        # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html 
    5959        return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name) 
     60 
     61    def date_trunc_sql(self, lookup_type, field_name): 
     62        fields = ['year', 'month', 'day', 'hour', 'minute', 'second'] 
     63        format = ('%%Y-', '%%m', '-%%d', ' %%H:', '%%i', ':%%s') # Use double percents to escape. 
     64        format_def = ('0000-', '01', '-01', ' 00:', '00', ':00') 
     65        try: 
     66            i = fields.index(lookup_type) + 1 
     67        except ValueError: 
     68            sql = field_name 
     69        else: 
     70            format_str = ''.join([f for f in format[:i]] + [f for f in format_def[i:]]) 
     71            sql = "CAST(DATE_FORMAT(%s, '%s') AS DATETIME)" % (field_name, format_str) 
     72        return sql 
    6073 
    6174class DatabaseWrapper(BaseDatabaseWrapper): 
     
    140153    return cursor.lastrowid 
    141154 
    142 def get_date_trunc_sql(lookup_type, field_name): 
    143     # lookup_type is 'year', 'month', 'day' 
    144     fields = ['year', 'month', 'day', 'hour', 'minute', 'second'] 
    145     format = ('%%Y-', '%%m', '-%%d', ' %%H:', '%%i', ':%%s') # Use double percents to escape. 
    146     format_def = ('0000-', '01', '-01', ' 00:', '00', ':00') 
    147     try: 
    148         i = fields.index(lookup_type) + 1 
    149     except ValueError: 
    150         sql = field_name 
    151     else: 
    152         format_str = ''.join([f for f in format[:i]] + [f for f in format_def[i:]]) 
    153         sql = "CAST(DATE_FORMAT(%s, '%s') AS DATETIME)" % (field_name, format_str) 
    154     return sql 
    155  
    156155def get_datetime_cast_sql(): 
    157156    return None 
  • django/trunk/django/db/backends/mysql_old/base.py

    r5951 r5952  
    6868        # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html 
    6969        return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name) 
     70 
     71    def date_trunc_sql(self, lookup_type, field_name): 
     72        fields = ['year', 'month', 'day', 'hour', 'minute', 'second'] 
     73        format = ('%%Y-', '%%m', '-%%d', ' %%H:', '%%i', ':%%s') # Use double percents to escape. 
     74        format_def = ('0000-', '01', '-01', ' 00:', '00', ':00') 
     75        try: 
     76            i = fields.index(lookup_type) + 1 
     77        except ValueError: 
     78            sql = field_name 
     79        else: 
     80            format_str = ''.join([f for f in format[:i]] + [f for f in format_def[i:]]) 
     81            sql = "CAST(DATE_FORMAT(%s, '%s') AS DATETIME)" % (field_name, format_str) 
     82        return sql 
    7083 
    7184class DatabaseWrapper(BaseDatabaseWrapper): 
     
    159172    return cursor.lastrowid 
    160173 
    161 def get_date_trunc_sql(lookup_type, field_name): 
    162     # lookup_type is 'year', 'month', 'day' 
    163     fields = ['year', 'month', 'day', 'hour', 'minute', 'second'] 
    164     format = ('%%Y-', '%%m', '-%%d', ' %%H:', '%%i', ':%%s') # Use double percents to escape. 
    165     format_def = ('0000-', '01', '-01', ' 00:', '00', ':00') 
    166     try: 
    167         i = fields.index(lookup_type) + 1 
    168     except ValueError: 
    169         sql = field_name 
    170     else: 
    171         format_str = ''.join([f for f in format[:i]] + [f for f in format_def[i:]]) 
    172         sql = "CAST(DATE_FORMAT(%s, '%s') AS DATETIME)" % (field_name, format_str) 
    173     return sql 
    174  
    175174def get_datetime_cast_sql(): 
    176175    return None 
  • django/trunk/django/db/backends/oracle/base.py

    r5951 r5952  
    4343        return "EXTRACT(%s FROM %s)" % (lookup_type, field_name) 
    4444 
     45    def date_trunc_sql(self, lookup_type, field_name): 
     46        # Oracle uses TRUNC() for both dates and numbers. 
     47        # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions155a.htm#SQLRF06151 
     48        if lookup_type == 'day': 
     49            sql = 'TRUNC(%s)' % field_name 
     50        else: 
     51            sql = "TRUNC(%s, '%s')" % (field_name, lookup_type) 
     52        return sql 
     53 
    4554class DatabaseWrapper(BaseDatabaseWrapper): 
    4655    ops = DatabaseOperations() 
     
    157166    cursor.execute('SELECT %s_sq.currval FROM dual' % sq_name) 
    158167    return cursor.fetchone()[0] 
    159  
    160 def get_date_trunc_sql(lookup_type, field_name): 
    161     # lookup_type is 'year', 'month', 'day' 
    162     # Oracle uses TRUNC() for both dates and numbers. 
    163     # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions155a.htm#SQLRF06151 
    164     if lookup_type == 'day': 
    165         sql = 'TRUNC(%s)' % (field_name,) 
    166     else: 
    167         sql = "TRUNC(%s, '%s')" % (field_name, lookup_type) 
    168     return sql 
    169168 
    170169def get_datetime_cast_sql(): 
  • django/trunk/django/db/backends/postgresql/base.py

    r5951 r5952  
    6262        # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT 
    6363        return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name) 
     64 
     65    def date_trunc_sql(self, lookup_type, field_name): 
     66        # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC 
     67        return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name) 
    6468 
    6569class DatabaseWrapper(BaseDatabaseWrapper): 
     
    124128    cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) 
    125129    return cursor.fetchone()[0] 
    126  
    127 def get_date_trunc_sql(lookup_type, field_name): 
    128     # lookup_type is 'year', 'month', 'day' 
    129     # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC 
    130     return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name) 
    131130 
    132131def get_datetime_cast_sql(): 
  • django/trunk/django/db/backends/postgresql_psycopg2/base.py

    r5951 r5952  
    2424        # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT 
    2525        return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name) 
     26 
     27    def date_trunc_sql(self, lookup_type, field_name): 
     28        # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC 
     29        return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name) 
    2630 
    2731class DatabaseWrapper(BaseDatabaseWrapper): 
     
    7882    cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) 
    7983    return cursor.fetchone()[0] 
    80  
    81 def get_date_trunc_sql(lookup_type, field_name): 
    82     # lookup_type is 'year', 'month', 'day' 
    83     # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC 
    84     return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name) 
    8584 
    8685def get_datetime_cast_sql(): 
  • django/trunk/django/db/backends/sqlite3/base.py

    r5951 r5952  
    3838    def date_extract_sql(self, lookup_type, field_name): 
    3939        # sqlite doesn't support extract, so we fake it with the user-defined 
    40         # function _sqlite_extract that's registered in connect(). 
     40        # function django_extract that's registered in connect(). 
    4141        return 'django_extract("%s", %s)' % (lookup_type.lower(), field_name) 
     42 
     43    def date_trunc_sql(self, lookup_type, field_name): 
     44        # sqlite doesn't support DATE_TRUNC, so we fake it with a user-defined 
     45        # function django_date_trunc that's registered in connect(). 
     46        return 'django_date_trunc("%s", %s)' % (lookup_type.lower(), field_name) 
    4247 
    4348class DatabaseWrapper(BaseDatabaseWrapper): 
     
    110115        return None 
    111116    return str(getattr(dt, lookup_type)) 
    112  
    113 def get_date_trunc_sql(lookup_type, field_name): 
    114     # lookup_type is 'year', 'month', 'day' 
    115     # sqlite doesn't support DATE_TRUNC, so we fake it as above. 
    116     return 'django_date_trunc("%s", %s)' % (lookup_type.lower(), field_name) 
    117117 
    118118def get_datetime_cast_sql(): 
  • django/trunk/django/db/models/query.py

    r5951 r5952  
    648648            group_by = '1' 
    649649        else: 
    650             group_by = backend.get_date_trunc_sql(self._kind, 
    651                                                   '%s.%s' % (table_name, field_name)) 
     650            group_by = connection.ops.date_trunc_sql(self._kind, '%s.%s' % (table_name, field_name)) 
    652651 
    653652        sql = 'SELECT %s %s GROUP BY %s ORDER BY 1 %s' % \ 
    654             (backend.get_date_trunc_sql(self._kind, '%s.%s' % (backend.quote_name(self.model._meta.db_table), 
     653            (connection.ops.date_trunc_sql(self._kind, '%s.%s' % (backend.quote_name(self.model._meta.db_table), 
    655654            backend.quote_name(self._field.column))), sql, group_by, self._order) 
    656655        cursor = connection.cursor()