summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2005-09-02 20:46:00 +0000
committerAdrian Holovaty <adrian@holovaty.com>2005-09-02 20:46:00 +0000
commitccbea84834a9f530c2019671787188599d2e148b (patch)
tree106e0ff7418cdd1dbd8314bd8d0e2e86775dc09a /django
parent95e8688d7f8c9393d0e1112b52e22e713f5fa25f (diff)
Fixed #404 -- Fixed random ordering in MySQL by abstracting random function into db backend modules. Thanks, mattycakes@gmail.com
git-svn-id: http://code.djangoproject.com/svn/django/trunk@615 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/core/db/__init__.py1
-rw-r--r--django/core/db/backends/mysql.py3
-rw-r--r--django/core/db/backends/postgresql.py3
-rw-r--r--django/core/db/backends/sqlite3.py3
-rw-r--r--django/core/meta/__init__.py4
5 files changed, 12 insertions, 2 deletions
diff --git a/django/core/db/__init__.py b/django/core/db/__init__.py
index d59f44bce3..c4f06e4c6e 100644
--- a/django/core/db/__init__.py
+++ b/django/core/db/__init__.py
@@ -38,6 +38,7 @@ get_last_insert_id = dbmod.get_last_insert_id
get_date_extract_sql = dbmod.get_date_extract_sql
get_date_trunc_sql = dbmod.get_date_trunc_sql
get_limit_offset_sql = dbmod.get_limit_offset_sql
+get_random_function_sql = dbmod.get_random_function_sql
get_table_list = dbmod.get_table_list
get_relations = dbmod.get_relations
OPERATOR_MAPPING = dbmod.OPERATOR_MAPPING
diff --git a/django/core/db/backends/mysql.py b/django/core/db/backends/mysql.py
index f65398b3b6..2193c6fbaa 100644
--- a/django/core/db/backends/mysql.py
+++ b/django/core/db/backends/mysql.py
@@ -77,6 +77,9 @@ def get_limit_offset_sql(limit, offset=None):
sql += "%s," % offset
return sql + str(limit)
+def get_random_function_sql():
+ return "RAND()"
+
def get_table_list(cursor):
"Returns a list of table names in the current database."
cursor.execute("SHOW TABLES")
diff --git a/django/core/db/backends/postgresql.py b/django/core/db/backends/postgresql.py
index 0afeec6f30..150bc3f888 100644
--- a/django/core/db/backends/postgresql.py
+++ b/django/core/db/backends/postgresql.py
@@ -77,6 +77,9 @@ def get_limit_offset_sql(limit, offset=None):
sql += " OFFSET %s" % offset
return sql
+def get_random_function_sql():
+ return "RANDOM()"
+
def get_table_list(cursor):
"Returns a list of table names in the current database."
cursor.execute("""
diff --git a/django/core/db/backends/sqlite3.py b/django/core/db/backends/sqlite3.py
index eef596eff0..fd44341400 100644
--- a/django/core/db/backends/sqlite3.py
+++ b/django/core/db/backends/sqlite3.py
@@ -103,6 +103,9 @@ def get_limit_offset_sql(limit, offset=None):
sql += " OFFSET %s" % offset
return sql
+def get_random_function_sql():
+ return "RANDOM()"
+
def _sqlite_date_trunc(lookup_type, dt):
try:
dt = typecasts.typecast_timestamp(dt)
diff --git a/django/core/meta/__init__.py b/django/core/meta/__init__.py
index a35498d87a..bc225f584f 100644
--- a/django/core/meta/__init__.py
+++ b/django/core/meta/__init__.py
@@ -62,7 +62,7 @@ def orderlist2sql(order_list, opts, prefix=''):
if f.startswith('-'):
output.append('%s%s DESC' % (prefix, orderfield2column(f[1:], opts)))
elif f == '?':
- output.append('RANDOM()')
+ output.append(db.get_random_function_sql())
else:
output.append('%s%s ASC' % (prefix, orderfield2column(f, opts)))
return ', '.join(output)
@@ -1319,7 +1319,7 @@ def function_get_sql_clause(opts, **kwargs):
order_by = []
for f in handle_legacy_orderlist(kwargs.get('order_by', opts.ordering)):
if f == '?': # Special case.
- order_by.append('RANDOM()')
+ order_by.append(db.get_random_function_sql())
else:
# Use the database table as a column prefix if it wasn't given,
# and if the requested column isn't a custom SELECT.