Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Adrian Holovaty authored September 02, 2005
1  django/core/db/__init__.py
@@ -38,6 +38,7 @@
38 38
 get_date_extract_sql = dbmod.get_date_extract_sql
39 39
 get_date_trunc_sql = dbmod.get_date_trunc_sql
40 40
 get_limit_offset_sql = dbmod.get_limit_offset_sql
  41
+get_random_function_sql = dbmod.get_random_function_sql
41 42
 get_table_list = dbmod.get_table_list
42 43
 get_relations = dbmod.get_relations
43 44
 OPERATOR_MAPPING = dbmod.OPERATOR_MAPPING
3  django/core/db/backends/mysql.py
@@ -77,6 +77,9 @@ def get_limit_offset_sql(limit, offset=None):
77 77
         sql += "%s," % offset
78 78
     return sql + str(limit)
79 79
 
  80
+def get_random_function_sql():
  81
+    return "RAND()"
  82
+
80 83
 def get_table_list(cursor):
81 84
     "Returns a list of table names in the current database."
82 85
     cursor.execute("SHOW TABLES")
3  django/core/db/backends/postgresql.py
@@ -77,6 +77,9 @@ def get_limit_offset_sql(limit, offset=None):
77 77
         sql += " OFFSET %s" % offset
78 78
     return sql
79 79
 
  80
+def get_random_function_sql():
  81
+    return "RANDOM()"
  82
+
80 83
 def get_table_list(cursor):
81 84
     "Returns a list of table names in the current database."
82 85
     cursor.execute("""
3  django/core/db/backends/sqlite3.py
@@ -103,6 +103,9 @@ def get_limit_offset_sql(limit, offset=None):
103 103
         sql += " OFFSET %s" % offset
104 104
     return sql
105 105
 
  106
+def get_random_function_sql():
  107
+    return "RANDOM()"
  108
+
106 109
 def _sqlite_date_trunc(lookup_type, dt):
107 110
     try:
108 111
         dt = typecasts.typecast_timestamp(dt)
4  django/core/meta/__init__.py
@@ -62,7 +62,7 @@ def orderlist2sql(order_list, opts, prefix=''):
62 62
         if f.startswith('-'):
63 63
             output.append('%s%s DESC' % (prefix, orderfield2column(f[1:], opts)))
64 64
         elif f == '?':
65  
-            output.append('RANDOM()')
  65
+            output.append(db.get_random_function_sql())
66 66
         else:
67 67
             output.append('%s%s ASC' % (prefix, orderfield2column(f, opts)))
68 68
     return ', '.join(output)
@@ -1319,7 +1319,7 @@ def function_get_sql_clause(opts, **kwargs):
1319 1319
     order_by = []
1320 1320
     for f in handle_legacy_orderlist(kwargs.get('order_by', opts.ordering)):
1321 1321
         if f == '?': # Special case.
1322  
-            order_by.append('RANDOM()')
  1322
+            order_by.append(db.get_random_function_sql())
1323 1323
         else:
1324 1324
             # Use the database table as a column prefix if it wasn't given,
1325 1325
             # and if the requested column isn't a custom SELECT.

0 notes on commit ccbea84

Please sign in to comment.
Something went wrong with that request. Please try again.