mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	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
This commit is contained in:
		| @@ -53,3 +53,10 @@ class BaseDatabaseOperations(object): | |||||||
|         This SQL is executed when a table is created. |         This SQL is executed when a table is created. | ||||||
|         """ |         """ | ||||||
|         return None |         return None | ||||||
|  |  | ||||||
|  |     def date_extract_sql(self, lookup_type, field_name): | ||||||
|  |         """ | ||||||
|  |         Given a lookup_type of 'year', 'month' or 'day', returns the SQL that | ||||||
|  |         extracts a value from the given date field field_name. | ||||||
|  |         """ | ||||||
|  |         raise NotImplementedError() | ||||||
|   | |||||||
| @@ -49,7 +49,8 @@ def variantToPython(variant, adType): | |||||||
| Database.convertVariantToPython = variantToPython | Database.convertVariantToPython = variantToPython | ||||||
|  |  | ||||||
| class DatabaseOperations(BaseDatabaseOperations): | class DatabaseOperations(BaseDatabaseOperations): | ||||||
|     pass |     def date_extract_sql(self, lookup_type, field_name): | ||||||
|  |         return "DATEPART(%s, %s)" % (lookup_type, field_name) | ||||||
|  |  | ||||||
| class DatabaseWrapper(BaseDatabaseWrapper): | class DatabaseWrapper(BaseDatabaseWrapper): | ||||||
|     ops = DatabaseOperations() |     ops = DatabaseOperations() | ||||||
| @@ -88,10 +89,6 @@ def get_last_insert_id(cursor, table_name, pk_name): | |||||||
|     cursor.execute("SELECT %s FROM %s WHERE %s = @@IDENTITY" % (pk_name, table_name, pk_name)) |     cursor.execute("SELECT %s FROM %s WHERE %s = @@IDENTITY" % (pk_name, table_name, pk_name)) | ||||||
|     return cursor.fetchone()[0] |     return cursor.fetchone()[0] | ||||||
|  |  | ||||||
| def get_date_extract_sql(lookup_type, table_name): |  | ||||||
|     # lookup_type is 'year', 'month', 'day' |  | ||||||
|     return "DATEPART(%s, %s)" % (lookup_type, table_name) |  | ||||||
|  |  | ||||||
| def get_date_trunc_sql(lookup_type, field_name): | def get_date_trunc_sql(lookup_type, field_name): | ||||||
|     # lookup_type is 'year', 'month', 'day' |     # lookup_type is 'year', 'month', 'day' | ||||||
|     if lookup_type=='year': |     if lookup_type=='year': | ||||||
|   | |||||||
| @@ -44,7 +44,6 @@ dictfetchone = complain | |||||||
| dictfetchmany = complain | dictfetchmany = complain | ||||||
| dictfetchall = complain | dictfetchall = complain | ||||||
| get_last_insert_id = complain | get_last_insert_id = complain | ||||||
| get_date_extract_sql = complain |  | ||||||
| get_date_trunc_sql = complain | get_date_trunc_sql = complain | ||||||
| get_datetime_cast_sql = complain | get_datetime_cast_sql = complain | ||||||
| get_limit_offset_sql = complain | get_limit_offset_sql = complain | ||||||
|   | |||||||
| @@ -54,7 +54,9 @@ server_version_re = re.compile(r'(\d{1,2})\.(\d{1,2})\.(\d{1,2})') | |||||||
| # TRADITIONAL will automatically cause most warnings to be treated as errors. | # TRADITIONAL will automatically cause most warnings to be treated as errors. | ||||||
|  |  | ||||||
| class DatabaseOperations(BaseDatabaseOperations): | class DatabaseOperations(BaseDatabaseOperations): | ||||||
|     pass |     def date_extract_sql(self, lookup_type, field_name): | ||||||
|  |         # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html | ||||||
|  |         return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name) | ||||||
|  |  | ||||||
| class DatabaseWrapper(BaseDatabaseWrapper): | class DatabaseWrapper(BaseDatabaseWrapper): | ||||||
|     ops = DatabaseOperations() |     ops = DatabaseOperations() | ||||||
| @@ -137,11 +139,6 @@ dictfetchall  = util.dictfetchall | |||||||
| def get_last_insert_id(cursor, table_name, pk_name): | def get_last_insert_id(cursor, table_name, pk_name): | ||||||
|     return cursor.lastrowid |     return cursor.lastrowid | ||||||
|  |  | ||||||
| def get_date_extract_sql(lookup_type, table_name): |  | ||||||
|     # lookup_type is 'year', 'month', 'day' |  | ||||||
|     # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html |  | ||||||
|     return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), table_name) |  | ||||||
|  |  | ||||||
| def get_date_trunc_sql(lookup_type, field_name): | def get_date_trunc_sql(lookup_type, field_name): | ||||||
|     # lookup_type is 'year', 'month', 'day' |     # lookup_type is 'year', 'month', 'day' | ||||||
|     fields = ['year', 'month', 'day', 'hour', 'minute', 'second'] |     fields = ['year', 'month', 'day', 'hour', 'minute', 'second'] | ||||||
|   | |||||||
| @@ -64,7 +64,9 @@ class MysqlDebugWrapper: | |||||||
|             return getattr(self.cursor, attr) |             return getattr(self.cursor, attr) | ||||||
|  |  | ||||||
| class DatabaseOperations(BaseDatabaseOperations): | class DatabaseOperations(BaseDatabaseOperations): | ||||||
|     pass |     def date_extract_sql(self, lookup_type, field_name): | ||||||
|  |         # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html | ||||||
|  |         return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name) | ||||||
|  |  | ||||||
| class DatabaseWrapper(BaseDatabaseWrapper): | class DatabaseWrapper(BaseDatabaseWrapper): | ||||||
|     ops = DatabaseOperations() |     ops = DatabaseOperations() | ||||||
| @@ -156,11 +158,6 @@ dictfetchall  = util.dictfetchall | |||||||
| def get_last_insert_id(cursor, table_name, pk_name): | def get_last_insert_id(cursor, table_name, pk_name): | ||||||
|     return cursor.lastrowid |     return cursor.lastrowid | ||||||
|  |  | ||||||
| def get_date_extract_sql(lookup_type, table_name): |  | ||||||
|     # lookup_type is 'year', 'month', 'day' |  | ||||||
|     # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html |  | ||||||
|     return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), table_name) |  | ||||||
|  |  | ||||||
| def get_date_trunc_sql(lookup_type, field_name): | def get_date_trunc_sql(lookup_type, field_name): | ||||||
|     # lookup_type is 'year', 'month', 'day' |     # lookup_type is 'year', 'month', 'day' | ||||||
|     fields = ['year', 'month', 'day', 'hour', 'minute', 'second'] |     fields = ['year', 'month', 'day', 'hour', 'minute', 'second'] | ||||||
|   | |||||||
| @@ -38,6 +38,10 @@ class DatabaseOperations(BaseDatabaseOperations): | |||||||
|                 END;/""" % (tr_name, quote_name(table), sq_name) |                 END;/""" % (tr_name, quote_name(table), sq_name) | ||||||
|         return sequence_sql, trigger_sql |         return sequence_sql, trigger_sql | ||||||
|  |  | ||||||
|  |     def date_extract_sql(self, lookup_type, field_name): | ||||||
|  |         # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions42a.htm#1017163 | ||||||
|  |         return "EXTRACT(%s FROM %s)" % (lookup_type, field_name) | ||||||
|  |  | ||||||
| class DatabaseWrapper(BaseDatabaseWrapper): | class DatabaseWrapper(BaseDatabaseWrapper): | ||||||
|     ops = DatabaseOperations() |     ops = DatabaseOperations() | ||||||
|  |  | ||||||
| @@ -153,11 +157,6 @@ def get_last_insert_id(cursor, table_name, pk_name): | |||||||
|     cursor.execute('SELECT %s_sq.currval FROM dual' % sq_name) |     cursor.execute('SELECT %s_sq.currval FROM dual' % sq_name) | ||||||
|     return cursor.fetchone()[0] |     return cursor.fetchone()[0] | ||||||
|  |  | ||||||
| def get_date_extract_sql(lookup_type, table_name): |  | ||||||
|     # lookup_type is 'year', 'month', 'day' |  | ||||||
|     # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions42a.htm#1017163 |  | ||||||
|     return "EXTRACT(%s FROM %s)" % (lookup_type, table_name) |  | ||||||
|  |  | ||||||
| def get_date_trunc_sql(lookup_type, field_name): | def get_date_trunc_sql(lookup_type, field_name): | ||||||
|     # lookup_type is 'year', 'month', 'day' |     # lookup_type is 'year', 'month', 'day' | ||||||
|     # Oracle uses TRUNC() for both dates and numbers. |     # Oracle uses TRUNC() for both dates and numbers. | ||||||
|   | |||||||
| @@ -58,7 +58,9 @@ class UnicodeCursorWrapper(object): | |||||||
| postgres_version = None | postgres_version = None | ||||||
|  |  | ||||||
| class DatabaseOperations(BaseDatabaseOperations): | class DatabaseOperations(BaseDatabaseOperations): | ||||||
|     pass |     def date_extract_sql(self, lookup_type, field_name): | ||||||
|  |         # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT | ||||||
|  |         return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name) | ||||||
|  |  | ||||||
| class DatabaseWrapper(BaseDatabaseWrapper): | class DatabaseWrapper(BaseDatabaseWrapper): | ||||||
|     ops = DatabaseOperations() |     ops = DatabaseOperations() | ||||||
| @@ -122,11 +124,6 @@ def get_last_insert_id(cursor, table_name, pk_name): | |||||||
|     cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) |     cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) | ||||||
|     return cursor.fetchone()[0] |     return cursor.fetchone()[0] | ||||||
|  |  | ||||||
| def get_date_extract_sql(lookup_type, table_name): |  | ||||||
|     # lookup_type is 'year', 'month', 'day' |  | ||||||
|     # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT |  | ||||||
|     return "EXTRACT('%s' FROM %s)" % (lookup_type, table_name) |  | ||||||
|  |  | ||||||
| def get_date_trunc_sql(lookup_type, field_name): | def get_date_trunc_sql(lookup_type, field_name): | ||||||
|     # lookup_type is 'year', 'month', 'day' |     # lookup_type is 'year', 'month', 'day' | ||||||
|     # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC |     # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC | ||||||
|   | |||||||
| @@ -20,7 +20,9 @@ psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) | |||||||
| postgres_version = None | postgres_version = None | ||||||
|  |  | ||||||
| class DatabaseOperations(BaseDatabaseOperations): | class DatabaseOperations(BaseDatabaseOperations): | ||||||
|     pass |     def date_extract_sql(self, lookup_type, field_name): | ||||||
|  |         # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT | ||||||
|  |         return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name) | ||||||
|  |  | ||||||
| class DatabaseWrapper(BaseDatabaseWrapper): | class DatabaseWrapper(BaseDatabaseWrapper): | ||||||
|     ops = DatabaseOperations() |     ops = DatabaseOperations() | ||||||
| @@ -76,11 +78,6 @@ def get_last_insert_id(cursor, table_name, pk_name): | |||||||
|     cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) |     cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) | ||||||
|     return cursor.fetchone()[0] |     return cursor.fetchone()[0] | ||||||
|  |  | ||||||
| def get_date_extract_sql(lookup_type, table_name): |  | ||||||
|     # lookup_type is 'year', 'month', 'day' |  | ||||||
|     # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT |  | ||||||
|     return "EXTRACT('%s' FROM %s)" % (lookup_type, table_name) |  | ||||||
|  |  | ||||||
| def get_date_trunc_sql(lookup_type, field_name): | def get_date_trunc_sql(lookup_type, field_name): | ||||||
|     # lookup_type is 'year', 'month', 'day' |     # lookup_type is 'year', 'month', 'day' | ||||||
|     # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC |     # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC | ||||||
|   | |||||||
| @@ -35,7 +35,10 @@ Database.register_converter("decimal", util.typecast_decimal) | |||||||
| Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal) | Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal) | ||||||
|  |  | ||||||
| class DatabaseOperations(BaseDatabaseOperations): | class DatabaseOperations(BaseDatabaseOperations): | ||||||
|     pass |     def date_extract_sql(self, lookup_type, field_name): | ||||||
|  |         # sqlite doesn't support extract, so we fake it with the user-defined | ||||||
|  |         # function _sqlite_extract that's registered in connect(). | ||||||
|  |         return 'django_extract("%s", %s)' % (lookup_type.lower(), field_name) | ||||||
|  |  | ||||||
| class DatabaseWrapper(BaseDatabaseWrapper): | class DatabaseWrapper(BaseDatabaseWrapper): | ||||||
|     ops = DatabaseOperations() |     ops = DatabaseOperations() | ||||||
| @@ -100,12 +103,6 @@ dictfetchall  = util.dictfetchall | |||||||
| def get_last_insert_id(cursor, table_name, pk_name): | def get_last_insert_id(cursor, table_name, pk_name): | ||||||
|     return cursor.lastrowid |     return cursor.lastrowid | ||||||
|  |  | ||||||
| def get_date_extract_sql(lookup_type, table_name): |  | ||||||
|     # lookup_type is 'year', 'month', 'day' |  | ||||||
|     # sqlite doesn't support extract, so we fake it with the user-defined |  | ||||||
|     # function _sqlite_extract that's registered in connect(), above. |  | ||||||
|     return 'django_extract("%s", %s)' % (lookup_type.lower(), table_name) |  | ||||||
|  |  | ||||||
| def _sqlite_extract(lookup_type, dt): | def _sqlite_extract(lookup_type, dt): | ||||||
|     try: |     try: | ||||||
|         dt = util.typecast_timestamp(dt) |         dt = util.typecast_timestamp(dt) | ||||||
|   | |||||||
| @@ -808,7 +808,7 @@ def get_where_clause(lookup_type, table_prefix, field_name, value, db_type): | |||||||
|     elif lookup_type in ('range', 'year'): |     elif lookup_type in ('range', 'year'): | ||||||
|         return '%s BETWEEN %%s AND %%s' % field_sql |         return '%s BETWEEN %%s AND %%s' % field_sql | ||||||
|     elif lookup_type in ('month', 'day'): |     elif lookup_type in ('month', 'day'): | ||||||
|         return "%s = %%s" % backend.get_date_extract_sql(lookup_type, field_sql) |         return "%s = %%s" % connection.ops.date_extract_sql(lookup_type, field_sql) | ||||||
|     elif lookup_type == 'isnull': |     elif lookup_type == 'isnull': | ||||||
|         return "%s IS %sNULL" % (field_sql, (not value and 'NOT ' or '')) |         return "%s IS %sNULL" % (field_sql, (not value and 'NOT ' or '')) | ||||||
|     elif lookup_type == 'search': |     elif lookup_type == 'search': | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user