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
This commit is contained in:
Adrian Holovaty 2005-09-02 20:46:00 +00:00
parent 95e8688d7f
commit ccbea84834
5 changed files with 12 additions and 2 deletions

View File

@ -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

View File

@ -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 File

@ -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 File

@ -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 File

@ -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.