Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #404 -- Fixed random ordering in MySQL by abstracting random fu…

…nction into db backend modules. Thanks, mattycakes@gmail.com

git-svn-id: http://code.djangoproject.com/svn/django/trunk@615 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ccbea84834a9f530c2019671787188599d2e148b 1 parent 95e8688
@adrianholovaty adrianholovaty authored
View
1  django/core/db/__init__.py
@@ -38,6 +38,7 @@
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
View
3  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")
View
3  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("""
View
3  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)
View
4 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.
Please sign in to comment.
Something went wrong with that request. Please try again.