mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Refactored get_random_function_sql() to DatabaseOperations.random_function_sql(). Refs #5106
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5962 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
		| @@ -133,3 +133,9 @@ class BaseDatabaseOperations(object): | |||||||
|         the field should use its default value. |         the field should use its default value. | ||||||
|         """ |         """ | ||||||
|         return 'DEFAULT' |         return 'DEFAULT' | ||||||
|  |  | ||||||
|  |     def random_function_sql(self): | ||||||
|  |         """ | ||||||
|  |         Returns a SQL expression that returns a random value. | ||||||
|  |         """ | ||||||
|  |         return 'RANDOM()' | ||||||
|   | |||||||
| @@ -67,6 +67,9 @@ class DatabaseOperations(BaseDatabaseOperations): | |||||||
|         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 random_function_sql(self): | ||||||
|  |         return 'RAND()' | ||||||
|  |  | ||||||
| class DatabaseWrapper(BaseDatabaseWrapper): | class DatabaseWrapper(BaseDatabaseWrapper): | ||||||
|     ops = DatabaseOperations() |     ops = DatabaseOperations() | ||||||
|  |  | ||||||
| @@ -100,9 +103,6 @@ dictfetchone = util.dictfetchone | |||||||
| dictfetchmany = util.dictfetchmany | dictfetchmany = util.dictfetchmany | ||||||
| dictfetchall  = util.dictfetchall | dictfetchall  = util.dictfetchall | ||||||
|  |  | ||||||
| def get_random_function_sql(): |  | ||||||
|     return "RAND()" |  | ||||||
|  |  | ||||||
| def get_start_transaction_sql(): | def get_start_transaction_sql(): | ||||||
|     return "BEGIN;" |     return "BEGIN;" | ||||||
|  |  | ||||||
|   | |||||||
| @@ -43,7 +43,6 @@ quote_name = complain | |||||||
| dictfetchone = complain | dictfetchone = complain | ||||||
| dictfetchmany = complain | dictfetchmany = complain | ||||||
| dictfetchall = complain | dictfetchall = complain | ||||||
| get_random_function_sql = complain |  | ||||||
| get_start_transaction_sql = complain | get_start_transaction_sql = complain | ||||||
| get_sql_flush = complain | get_sql_flush = complain | ||||||
| get_sql_sequence_reset = complain | get_sql_sequence_reset = complain | ||||||
|   | |||||||
| @@ -84,6 +84,9 @@ class DatabaseOperations(BaseDatabaseOperations): | |||||||
|             sql += "%s," % offset |             sql += "%s," % offset | ||||||
|         return sql + str(limit) |         return sql + str(limit) | ||||||
|  |  | ||||||
|  |     def random_function_sql(self): | ||||||
|  |         return 'RAND()' | ||||||
|  |  | ||||||
| class DatabaseWrapper(BaseDatabaseWrapper): | class DatabaseWrapper(BaseDatabaseWrapper): | ||||||
|     ops = DatabaseOperations() |     ops = DatabaseOperations() | ||||||
|  |  | ||||||
| @@ -162,9 +165,6 @@ dictfetchone = util.dictfetchone | |||||||
| dictfetchmany = util.dictfetchmany | dictfetchmany = util.dictfetchmany | ||||||
| dictfetchall  = util.dictfetchall | dictfetchall  = util.dictfetchall | ||||||
|  |  | ||||||
| def get_random_function_sql(): |  | ||||||
|     return "RAND()" |  | ||||||
|  |  | ||||||
| def get_start_transaction_sql(): | def get_start_transaction_sql(): | ||||||
|     return "BEGIN;" |     return "BEGIN;" | ||||||
|  |  | ||||||
|   | |||||||
| @@ -94,6 +94,9 @@ class DatabaseOperations(BaseDatabaseOperations): | |||||||
|             sql += "%s," % offset |             sql += "%s," % offset | ||||||
|         return sql + str(limit) |         return sql + str(limit) | ||||||
|  |  | ||||||
|  |     def random_function_sql(self): | ||||||
|  |         return 'RAND()' | ||||||
|  |  | ||||||
| class DatabaseWrapper(BaseDatabaseWrapper): | class DatabaseWrapper(BaseDatabaseWrapper): | ||||||
|     ops = DatabaseOperations() |     ops = DatabaseOperations() | ||||||
|  |  | ||||||
| @@ -181,9 +184,6 @@ dictfetchone = util.dictfetchone | |||||||
| dictfetchmany = util.dictfetchmany | dictfetchmany = util.dictfetchmany | ||||||
| dictfetchall  = util.dictfetchall | dictfetchall  = util.dictfetchall | ||||||
|  |  | ||||||
| def get_random_function_sql(): |  | ||||||
|     return "RAND()" |  | ||||||
|  |  | ||||||
| def get_start_transaction_sql(): | def get_start_transaction_sql(): | ||||||
|     return "BEGIN;" |     return "BEGIN;" | ||||||
|  |  | ||||||
|   | |||||||
| @@ -70,6 +70,9 @@ class DatabaseOperations(BaseDatabaseOperations): | |||||||
|     def max_name_length(self): |     def max_name_length(self): | ||||||
|         return 30 |         return 30 | ||||||
|  |  | ||||||
|  |     def random_function_sql(self): | ||||||
|  |         return "DBMS_RANDOM.RANDOM" | ||||||
|  |  | ||||||
| class DatabaseWrapper(BaseDatabaseWrapper): | class DatabaseWrapper(BaseDatabaseWrapper): | ||||||
|     ops = DatabaseOperations() |     ops = DatabaseOperations() | ||||||
|  |  | ||||||
| @@ -186,9 +189,6 @@ def get_field_cast_sql(db_type): | |||||||
|     else: |     else: | ||||||
|         return "%s%s" |         return "%s%s" | ||||||
|  |  | ||||||
| def get_random_function_sql(): |  | ||||||
|     return "DBMS_RANDOM.RANDOM" |  | ||||||
|  |  | ||||||
| def get_start_transaction_sql(): | def get_start_transaction_sql(): | ||||||
|     return None |     return None | ||||||
|  |  | ||||||
| @@ -380,7 +380,7 @@ def get_query_set_class(DefaultQuerySet): | |||||||
|                 ordering_to_use = opts.ordering |                 ordering_to_use = opts.ordering | ||||||
|             for f in handle_legacy_orderlist(ordering_to_use): |             for f in handle_legacy_orderlist(ordering_to_use): | ||||||
|                 if f == '?': # Special case. |                 if f == '?': # Special case. | ||||||
|                     order_by.append(backend.get_random_function_sql()) |                     order_by.append(DatabaseOperations().random_function_sql()) | ||||||
|                 else: |                 else: | ||||||
|                     if f.startswith('-'): |                     if f.startswith('-'): | ||||||
|                         col_name = f[1:] |                         col_name = f[1:] | ||||||
|   | |||||||
| @@ -131,9 +131,6 @@ def dictfetchall(cursor): | |||||||
|     "Returns all rows from a cursor as a dict" |     "Returns all rows from a cursor as a dict" | ||||||
|     return cursor.dictfetchall() |     return cursor.dictfetchall() | ||||||
|  |  | ||||||
| def get_random_function_sql(): |  | ||||||
|     return "RANDOM()" |  | ||||||
|  |  | ||||||
| def get_start_transaction_sql(): | def get_start_transaction_sql(): | ||||||
|     return "BEGIN;" |     return "BEGIN;" | ||||||
|  |  | ||||||
|   | |||||||
| @@ -85,9 +85,6 @@ dictfetchone = util.dictfetchone | |||||||
| dictfetchmany = util.dictfetchmany | dictfetchmany = util.dictfetchmany | ||||||
| dictfetchall = util.dictfetchall | dictfetchall = util.dictfetchall | ||||||
|  |  | ||||||
| def get_random_function_sql(): |  | ||||||
|     return "RANDOM()" |  | ||||||
|  |  | ||||||
| def get_start_transaction_sql(): | def get_start_transaction_sql(): | ||||||
|     return "BEGIN;" |     return "BEGIN;" | ||||||
|  |  | ||||||
|   | |||||||
| @@ -118,9 +118,6 @@ def _sqlite_extract(lookup_type, dt): | |||||||
|         return None |         return None | ||||||
|     return str(getattr(dt, lookup_type)) |     return str(getattr(dt, lookup_type)) | ||||||
|  |  | ||||||
| def get_random_function_sql(): |  | ||||||
|     return "RANDOM()" |  | ||||||
|  |  | ||||||
| def get_start_transaction_sql(): | def get_start_transaction_sql(): | ||||||
|     return "BEGIN;" |     return "BEGIN;" | ||||||
|  |  | ||||||
|   | |||||||
| @@ -71,7 +71,7 @@ def orderlist2sql(order_list, opts, prefix=''): | |||||||
|         if f.startswith('-'): |         if f.startswith('-'): | ||||||
|             output.append('%s%s DESC' % (prefix, backend.quote_name(orderfield2column(f[1:], opts)))) |             output.append('%s%s DESC' % (prefix, backend.quote_name(orderfield2column(f[1:], opts)))) | ||||||
|         elif f == '?': |         elif f == '?': | ||||||
|             output.append(backend.get_random_function_sql()) |             output.append(connection.ops.random_function_sql()) | ||||||
|         else: |         else: | ||||||
|             output.append('%s%s ASC' % (prefix, backend.quote_name(orderfield2column(f, opts)))) |             output.append('%s%s ASC' % (prefix, backend.quote_name(orderfield2column(f, opts)))) | ||||||
|     return ', '.join(output) |     return ', '.join(output) | ||||||
| @@ -531,7 +531,7 @@ class _QuerySet(object): | |||||||
|             ordering_to_use = opts.ordering |             ordering_to_use = opts.ordering | ||||||
|         for f in handle_legacy_orderlist(ordering_to_use): |         for f in handle_legacy_orderlist(ordering_to_use): | ||||||
|             if f == '?': # Special case. |             if f == '?': # Special case. | ||||||
|                 order_by.append(backend.get_random_function_sql()) |                 order_by.append(connection.ops.random_function_sql()) | ||||||
|             else: |             else: | ||||||
|                 if f.startswith('-'): |                 if f.startswith('-'): | ||||||
|                     col_name = f[1:] |                     col_name = f[1:] | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user