Commit addd3df3 authored by Karen Tracey's avatar Karen Tracey
Browse files

Fixed #7672 -- Added a 'week_day' lookup type. Many thanks to Ross Poulton for...

Fixed #7672 -- Added a 'week_day' lookup type. Many thanks to Ross Poulton for the proposal and implementation on all built-in database backends..


git-svn-id: http://code.djangoproject.com/svn/django/trunk@9818 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 0326574d
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -318,9 +318,10 @@ answer newbie questions, and generally made Django that much better:
    Michael Placentra II <someone@michaelplacentra2.net>
    Luke Plant <http://lukeplant.me.uk/>
    plisk
    Mihai Preda <mihai_preda@yahoo.com>
    Daniel Poelzleithner <http://poelzi.org/>
    polpak@yahoo.com
    Ross Poulton <ross@rossp.org>    
    Mihai Preda <mihai_preda@yahoo.com>
    Matthias Pronk <django@masida.nl>
    Jyrki Pulliainen <jyrki.pulliainen@gmail.com>
    Thejaswi Puthraya <thejaswi.puthraya@gmail.com>
+6 −1
Original line number Diff line number Diff line
@@ -116,6 +116,11 @@ class DatabaseFeatures(BaseDatabaseFeatures):
class DatabaseOperations(BaseDatabaseOperations):
    def date_extract_sql(self, lookup_type, field_name):
        # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
        if lookup_type == 'week_day':
            # DAYOFWEEK() returns an integer, 1-7, Sunday=1.
            # Note: WEEKDAY() returns 0-6, Monday=0.
            return "DAYOFWEEK(%s)" % field_name
        else:
            return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name)

    def date_trunc_sql(self, lookup_type, field_name):
+9 −3
Original line number Diff line number Diff line
@@ -72,6 +72,10 @@ WHEN (new.%(col_name)s IS NULL)

    def date_extract_sql(self, lookup_type, field_name):
        # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions42a.htm#1017163
        if lookup_type == 'week_day':
            # TO_CHAR(field, 'D') returns an integer from 1-7, where 1=Sunday.
            return "TO_CHAR(%s, 'D')" % field_name
        else:
            return "EXTRACT(%s FROM %s)" % (lookup_type, field_name)

    def date_trunc_sql(self, lookup_type, field_name):
@@ -268,9 +272,11 @@ class DatabaseWrapper(BaseDatabaseWrapper):
            self.connection = Database.connect(conn_string, **self.options)
            cursor = FormatStylePlaceholderCursor(self.connection)
            # Set oracle date to ansi date format.  This only needs to execute
            # once when we create a new connection.
            # once when we create a new connection. We also set the Territory
            # to 'AMERICA' which forces Sunday to evaluate to a '1' in TO_CHAR().
            cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS' "
                           "NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'")
                           "NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF' "
                           "NLS_TERRITORY = 'AMERICA'")
            try:
                self.oracle_version = int(self.connection.version.split('.')[0])
                # There's no way for the DatabaseOperations class to know the
+6 −1
Original line number Diff line number Diff line
@@ -26,6 +26,11 @@ class DatabaseOperations(BaseDatabaseOperations):

    def date_extract_sql(self, lookup_type, field_name):
        # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
        if lookup_type == 'week_day':
            # Using EXTRACT(), PostgreSQL days are indexed as Sunday=0, Saturday=6.
            # If we instead us TO_CHAR, they're indexed with Sunday=1, Saturday=7
            return "TO_CHAR(%s, 'D')" % field_name
        else:
            return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)

    def date_trunc_sql(self, lookup_type, field_name):
+4 −1
Original line number Diff line number Diff line
@@ -207,6 +207,9 @@ def _sqlite_extract(lookup_type, dt):
        dt = util.typecast_timestamp(dt)
    except (ValueError, TypeError):
        return None
    if lookup_type == 'week_day':
        return unicode((dt.isoweekday() % 7) + 1)
    else:
        return unicode(getattr(dt, lookup_type))

def _sqlite_date_trunc(lookup_type, dt):
Loading