Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactored get_date_extract_sql() to DatabaseOperations.date_extract_…

…sql(). Refs #5106

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5951 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit aab04a4c2f880645783f05ef9a87eef14966503a 1 parent 38b5d7f
Adrian Holovaty authored August 19, 2007
7  django/db/backends/__init__.py
@@ -53,3 +53,10 @@ def autoinc_sql(self, table):
53 53
         This SQL is executed when a table is created.
54 54
         """
55 55
         return None
  56
+
  57
+    def date_extract_sql(self, lookup_type, field_name):
  58
+        """
  59
+        Given a lookup_type of 'year', 'month' or 'day', returns the SQL that
  60
+        extracts a value from the given date field field_name.
  61
+        """
  62
+        raise NotImplementedError()
7  django/db/backends/ado_mssql/base.py
@@ -49,7 +49,8 @@ def variantToPython(variant, adType):
49 49
 Database.convertVariantToPython = variantToPython
50 50
 
51 51
 class DatabaseOperations(BaseDatabaseOperations):
52  
-    pass
  52
+    def date_extract_sql(self, lookup_type, field_name):
  53
+        return "DATEPART(%s, %s)" % (lookup_type, field_name)
53 54
 
54 55
 class DatabaseWrapper(BaseDatabaseWrapper):
55 56
     ops = DatabaseOperations()
@@ -88,10 +89,6 @@ def get_last_insert_id(cursor, table_name, pk_name):
88 89
     cursor.execute("SELECT %s FROM %s WHERE %s = @@IDENTITY" % (pk_name, table_name, pk_name))
89 90
     return cursor.fetchone()[0]
90 91
 
91  
-def get_date_extract_sql(lookup_type, table_name):
92  
-    # lookup_type is 'year', 'month', 'day'
93  
-    return "DATEPART(%s, %s)" % (lookup_type, table_name)
94  
-
95 92
 def get_date_trunc_sql(lookup_type, field_name):
96 93
     # lookup_type is 'year', 'month', 'day'
97 94
     if lookup_type=='year':
1  django/db/backends/dummy/base.py
@@ -44,7 +44,6 @@ def close(self):
44 44
 dictfetchmany = complain
45 45
 dictfetchall = complain
46 46
 get_last_insert_id = complain
47  
-get_date_extract_sql = complain
48 47
 get_date_trunc_sql = complain
49 48
 get_datetime_cast_sql = complain
50 49
 get_limit_offset_sql = complain
9  django/db/backends/mysql/base.py
@@ -54,7 +54,9 @@
54 54
 # TRADITIONAL will automatically cause most warnings to be treated as errors.
55 55
 
56 56
 class DatabaseOperations(BaseDatabaseOperations):
57  
-    pass
  57
+    def date_extract_sql(self, lookup_type, field_name):
  58
+        # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
  59
+        return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name)
58 60
 
59 61
 class DatabaseWrapper(BaseDatabaseWrapper):
60 62
     ops = DatabaseOperations()
@@ -137,11 +139,6 @@ def quote_name(name):
137 139
 def get_last_insert_id(cursor, table_name, pk_name):
138 140
     return cursor.lastrowid
139 141
 
140  
-def get_date_extract_sql(lookup_type, table_name):
141  
-    # lookup_type is 'year', 'month', 'day'
142  
-    # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
143  
-    return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), table_name)
144  
-
145 142
 def get_date_trunc_sql(lookup_type, field_name):
146 143
     # lookup_type is 'year', 'month', 'day'
147 144
     fields = ['year', 'month', 'day', 'hour', 'minute', 'second']
9  django/db/backends/mysql_old/base.py
@@ -64,7 +64,9 @@ def __getattr__(self, attr):
64 64
             return getattr(self.cursor, attr)
65 65
 
66 66
 class DatabaseOperations(BaseDatabaseOperations):
67  
-    pass
  67
+    def date_extract_sql(self, lookup_type, field_name):
  68
+        # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
  69
+        return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name)
68 70
 
69 71
 class DatabaseWrapper(BaseDatabaseWrapper):
70 72
     ops = DatabaseOperations()
@@ -156,11 +158,6 @@ def quote_name(name):
156 158
 def get_last_insert_id(cursor, table_name, pk_name):
157 159
     return cursor.lastrowid
158 160
 
159  
-def get_date_extract_sql(lookup_type, table_name):
160  
-    # lookup_type is 'year', 'month', 'day'
161  
-    # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
162  
-    return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), table_name)
163  
-
164 161
 def get_date_trunc_sql(lookup_type, field_name):
165 162
     # lookup_type is 'year', 'month', 'day'
166 163
     fields = ['year', 'month', 'day', 'hour', 'minute', 'second']
9  django/db/backends/oracle/base.py
@@ -38,6 +38,10 @@ def autoinc_sql(self, table):
38 38
                 END;/""" % (tr_name, quote_name(table), sq_name)
39 39
         return sequence_sql, trigger_sql
40 40
 
  41
+    def date_extract_sql(self, lookup_type, field_name):
  42
+        # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions42a.htm#1017163
  43
+        return "EXTRACT(%s FROM %s)" % (lookup_type, field_name)
  44
+
41 45
 class DatabaseWrapper(BaseDatabaseWrapper):
42 46
     ops = DatabaseOperations()
43 47
 
@@ -153,11 +157,6 @@ def get_last_insert_id(cursor, table_name, pk_name):
153 157
     cursor.execute('SELECT %s_sq.currval FROM dual' % sq_name)
154 158
     return cursor.fetchone()[0]
155 159
 
156  
-def get_date_extract_sql(lookup_type, table_name):
157  
-    # lookup_type is 'year', 'month', 'day'
158  
-    # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions42a.htm#1017163
159  
-    return "EXTRACT(%s FROM %s)" % (lookup_type, table_name)
160  
-
161 160
 def get_date_trunc_sql(lookup_type, field_name):
162 161
     # lookup_type is 'year', 'month', 'day'
163 162
     # Oracle uses TRUNC() for both dates and numbers.
9  django/db/backends/postgresql/base.py
@@ -58,7 +58,9 @@ def __getattr__(self, attr):
58 58
 postgres_version = None
59 59
 
60 60
 class DatabaseOperations(BaseDatabaseOperations):
61  
-    pass
  61
+    def date_extract_sql(self, lookup_type, field_name):
  62
+        # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
  63
+        return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)
62 64
 
63 65
 class DatabaseWrapper(BaseDatabaseWrapper):
64 66
     ops = DatabaseOperations()
@@ -122,11 +124,6 @@ def get_last_insert_id(cursor, table_name, pk_name):
122 124
     cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name))
123 125
     return cursor.fetchone()[0]
124 126
 
125  
-def get_date_extract_sql(lookup_type, table_name):
126  
-    # lookup_type is 'year', 'month', 'day'
127  
-    # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
128  
-    return "EXTRACT('%s' FROM %s)" % (lookup_type, table_name)
129  
-
130 127
 def get_date_trunc_sql(lookup_type, field_name):
131 128
     # lookup_type is 'year', 'month', 'day'
132 129
     # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
9  django/db/backends/postgresql_psycopg2/base.py
@@ -20,7 +20,9 @@
20 20
 postgres_version = None
21 21
 
22 22
 class DatabaseOperations(BaseDatabaseOperations):
23  
-    pass
  23
+    def date_extract_sql(self, lookup_type, field_name):
  24
+        # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
  25
+        return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)
24 26
 
25 27
 class DatabaseWrapper(BaseDatabaseWrapper):
26 28
     ops = DatabaseOperations()
@@ -76,11 +78,6 @@ def get_last_insert_id(cursor, table_name, pk_name):
76 78
     cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name))
77 79
     return cursor.fetchone()[0]
78 80
 
79  
-def get_date_extract_sql(lookup_type, table_name):
80  
-    # lookup_type is 'year', 'month', 'day'
81  
-    # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
82  
-    return "EXTRACT('%s' FROM %s)" % (lookup_type, table_name)
83  
-
84 81
 def get_date_trunc_sql(lookup_type, field_name):
85 82
     # lookup_type is 'year', 'month', 'day'
86 83
     # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
11  django/db/backends/sqlite3/base.py
@@ -35,7 +35,10 @@
35 35
 Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal)
36 36
 
37 37
 class DatabaseOperations(BaseDatabaseOperations):
38  
-    pass
  38
+    def date_extract_sql(self, lookup_type, field_name):
  39
+        # sqlite doesn't support extract, so we fake it with the user-defined
  40
+        # function _sqlite_extract that's registered in connect().
  41
+        return 'django_extract("%s", %s)' % (lookup_type.lower(), field_name)
39 42
 
40 43
 class DatabaseWrapper(BaseDatabaseWrapper):
41 44
     ops = DatabaseOperations()
@@ -100,12 +103,6 @@ def quote_name(name):
100 103
 def get_last_insert_id(cursor, table_name, pk_name):
101 104
     return cursor.lastrowid
102 105
 
103  
-def get_date_extract_sql(lookup_type, table_name):
104  
-    # lookup_type is 'year', 'month', 'day'
105  
-    # sqlite doesn't support extract, so we fake it with the user-defined
106  
-    # function _sqlite_extract that's registered in connect(), above.
107  
-    return 'django_extract("%s", %s)' % (lookup_type.lower(), table_name)
108  
-
109 106
 def _sqlite_extract(lookup_type, dt):
110 107
     try:
111 108
         dt = util.typecast_timestamp(dt)
2  django/db/models/query.py
@@ -808,7 +808,7 @@ def get_where_clause(lookup_type, table_prefix, field_name, value, db_type):
808 808
     elif lookup_type in ('range', 'year'):
809 809
         return '%s BETWEEN %%s AND %%s' % field_sql
810 810
     elif lookup_type in ('month', 'day'):
811  
-        return "%s = %%s" % backend.get_date_extract_sql(lookup_type, field_sql)
  811
+        return "%s = %%s" % connection.ops.date_extract_sql(lookup_type, field_sql)
812 812
     elif lookup_type == 'isnull':
813 813
         return "%s IS %sNULL" % (field_sql, (not value and 'NOT ' or ''))
814 814
     elif lookup_type == 'search':

0 notes on commit aab04a4

Please sign in to comment.
Something went wrong with that request. Please try again.