Skip to content

Commit

Permalink
Fixed #404 -- Fixed random ordering in MySQL by abstracting random fu…
Browse files Browse the repository at this point in the history
…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
adrianholovaty committed Sep 2, 2005
1 parent 95e8688 commit ccbea84
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions django/core/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions django/core/db/backends/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions django/core/db/backends/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("""
Expand Down
3 changes: 3 additions & 0 deletions django/core/db/backends/sqlite3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions django/core/meta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit ccbea84

Please sign in to comment.