From c44fb66551bf2f628648440ebc2010ad1135d9ac Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Mon, 20 Aug 2007 00:04:20 +0000 Subject: [PATCH] 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 --- django/db/backends/__init__.py | 6 ++++++ django/db/backends/ado_mssql/base.py | 6 +++--- django/db/backends/dummy/base.py | 1 - django/db/backends/mysql/base.py | 6 +++--- django/db/backends/mysql_old/base.py | 6 +++--- django/db/backends/oracle/base.py | 8 ++++---- django/db/backends/postgresql/base.py | 3 --- django/db/backends/postgresql_psycopg2/base.py | 3 --- django/db/backends/sqlite3/base.py | 3 --- django/db/models/query.py | 4 ++-- 10 files changed, 21 insertions(+), 25 deletions(-) diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index fb38c125b7..63114db67d 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -133,3 +133,9 @@ class BaseDatabaseOperations(object): the field should use its default value. """ return 'DEFAULT' + + def random_function_sql(self): + """ + Returns a SQL expression that returns a random value. + """ + return 'RANDOM()' diff --git a/django/db/backends/ado_mssql/base.py b/django/db/backends/ado_mssql/base.py index 3b4ffeb44f..51ea566218 100644 --- a/django/db/backends/ado_mssql/base.py +++ b/django/db/backends/ado_mssql/base.py @@ -67,6 +67,9 @@ class DatabaseOperations(BaseDatabaseOperations): cursor.execute("SELECT %s FROM %s WHERE %s = @@IDENTITY" % (pk_name, table_name, pk_name)) return cursor.fetchone()[0] + def random_function_sql(self): + return 'RAND()' + class DatabaseWrapper(BaseDatabaseWrapper): ops = DatabaseOperations() @@ -100,9 +103,6 @@ dictfetchone = util.dictfetchone dictfetchmany = util.dictfetchmany dictfetchall = util.dictfetchall -def get_random_function_sql(): - return "RAND()" - def get_start_transaction_sql(): return "BEGIN;" diff --git a/django/db/backends/dummy/base.py b/django/db/backends/dummy/base.py index 114042a701..f367ebe1aa 100644 --- a/django/db/backends/dummy/base.py +++ b/django/db/backends/dummy/base.py @@ -43,7 +43,6 @@ quote_name = complain dictfetchone = complain dictfetchmany = complain dictfetchall = complain -get_random_function_sql = complain get_start_transaction_sql = complain get_sql_flush = complain get_sql_sequence_reset = complain diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index 9daf0e1c0b..4b4d4eb44a 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -84,6 +84,9 @@ class DatabaseOperations(BaseDatabaseOperations): sql += "%s," % offset return sql + str(limit) + def random_function_sql(self): + return 'RAND()' + class DatabaseWrapper(BaseDatabaseWrapper): ops = DatabaseOperations() @@ -162,9 +165,6 @@ dictfetchone = util.dictfetchone dictfetchmany = util.dictfetchmany dictfetchall = util.dictfetchall -def get_random_function_sql(): - return "RAND()" - def get_start_transaction_sql(): return "BEGIN;" diff --git a/django/db/backends/mysql_old/base.py b/django/db/backends/mysql_old/base.py index dee70db41c..143572a598 100644 --- a/django/db/backends/mysql_old/base.py +++ b/django/db/backends/mysql_old/base.py @@ -94,6 +94,9 @@ class DatabaseOperations(BaseDatabaseOperations): sql += "%s," % offset return sql + str(limit) + def random_function_sql(self): + return 'RAND()' + class DatabaseWrapper(BaseDatabaseWrapper): ops = DatabaseOperations() @@ -181,9 +184,6 @@ dictfetchone = util.dictfetchone dictfetchmany = util.dictfetchmany dictfetchall = util.dictfetchall -def get_random_function_sql(): - return "RAND()" - def get_start_transaction_sql(): return "BEGIN;" diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index 133f587cce..a633e8e28f 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -70,6 +70,9 @@ class DatabaseOperations(BaseDatabaseOperations): def max_name_length(self): return 30 + def random_function_sql(self): + return "DBMS_RANDOM.RANDOM" + class DatabaseWrapper(BaseDatabaseWrapper): ops = DatabaseOperations() @@ -186,9 +189,6 @@ def get_field_cast_sql(db_type): else: return "%s%s" -def get_random_function_sql(): - return "DBMS_RANDOM.RANDOM" - def get_start_transaction_sql(): return None @@ -380,7 +380,7 @@ def get_query_set_class(DefaultQuerySet): ordering_to_use = opts.ordering for f in handle_legacy_orderlist(ordering_to_use): if f == '?': # Special case. - order_by.append(backend.get_random_function_sql()) + order_by.append(DatabaseOperations().random_function_sql()) else: if f.startswith('-'): col_name = f[1:] diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index fee127ec90..4c5a278daf 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -131,9 +131,6 @@ def dictfetchall(cursor): "Returns all rows from a cursor as a dict" return cursor.dictfetchall() -def get_random_function_sql(): - return "RANDOM()" - def get_start_transaction_sql(): return "BEGIN;" diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index 964d2688ec..44b80ac820 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -85,9 +85,6 @@ dictfetchone = util.dictfetchone dictfetchmany = util.dictfetchmany dictfetchall = util.dictfetchall -def get_random_function_sql(): - return "RANDOM()" - def get_start_transaction_sql(): return "BEGIN;" diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index f17eb9686f..0ab928dfc4 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -118,9 +118,6 @@ def _sqlite_extract(lookup_type, dt): return None return str(getattr(dt, lookup_type)) -def get_random_function_sql(): - return "RANDOM()" - def get_start_transaction_sql(): return "BEGIN;" diff --git a/django/db/models/query.py b/django/db/models/query.py index df3be74632..151915dd39 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -71,7 +71,7 @@ def orderlist2sql(order_list, opts, prefix=''): if f.startswith('-'): output.append('%s%s DESC' % (prefix, backend.quote_name(orderfield2column(f[1:], opts)))) elif f == '?': - output.append(backend.get_random_function_sql()) + output.append(connection.ops.random_function_sql()) else: output.append('%s%s ASC' % (prefix, backend.quote_name(orderfield2column(f, opts)))) return ', '.join(output) @@ -531,7 +531,7 @@ class _QuerySet(object): ordering_to_use = opts.ordering for f in handle_legacy_orderlist(ordering_to_use): if f == '?': # Special case. - order_by.append(backend.get_random_function_sql()) + order_by.append(connection.ops.random_function_sql()) else: if f.startswith('-'): col_name = f[1:]