Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactored get_random_function_sql() to DatabaseOperations.random_fun…

…ction_sql(). Refs #5106

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5962 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c44fb66551bf2f628648440ebc2010ad1135d9ac 1 parent aaf8760
Adrian Holovaty authored August 20, 2007
6  django/db/backends/__init__.py
@@ -133,3 +133,9 @@ def pk_default_value(self):
133 133
         the field should use its default value.
134 134
         """
135 135
         return 'DEFAULT'
  136
+
  137
+    def random_function_sql(self):
  138
+        """
  139
+        Returns a SQL expression that returns a random value.
  140
+        """
  141
+        return 'RANDOM()'
6  django/db/backends/ado_mssql/base.py
@@ -67,6 +67,9 @@ def last_insert_id(self, cursor, table_name, pk_name):
67 67
         cursor.execute("SELECT %s FROM %s WHERE %s = @@IDENTITY" % (pk_name, table_name, pk_name))
68 68
         return cursor.fetchone()[0]
69 69
 
  70
+    def random_function_sql(self):
  71
+        return 'RAND()'
  72
+
70 73
 class DatabaseWrapper(BaseDatabaseWrapper):
71 74
     ops = DatabaseOperations()
72 75
 
@@ -100,9 +103,6 @@ def quote_name(name):
100 103
 dictfetchmany = util.dictfetchmany
101 104
 dictfetchall  = util.dictfetchall
102 105
 
103  
-def get_random_function_sql():
104  
-    return "RAND()"
105  
-
106 106
 def get_start_transaction_sql():
107 107
     return "BEGIN;"
108 108
 
1  django/db/backends/dummy/base.py
@@ -43,7 +43,6 @@ def close(self):
43 43
 dictfetchone = complain
44 44
 dictfetchmany = complain
45 45
 dictfetchall = complain
46  
-get_random_function_sql = complain
47 46
 get_start_transaction_sql = complain
48 47
 get_sql_flush = complain
49 48
 get_sql_sequence_reset = complain
6  django/db/backends/mysql/base.py
@@ -84,6 +84,9 @@ def limit_offset_sql(self, limit, offset=None):
84 84
             sql += "%s," % offset
85 85
         return sql + str(limit)
86 86
 
  87
+    def random_function_sql(self):
  88
+        return 'RAND()'
  89
+
87 90
 class DatabaseWrapper(BaseDatabaseWrapper):
88 91
     ops = DatabaseOperations()
89 92
 
@@ -162,9 +165,6 @@ def quote_name(name):
162 165
 dictfetchmany = util.dictfetchmany
163 166
 dictfetchall  = util.dictfetchall
164 167
 
165  
-def get_random_function_sql():
166  
-    return "RAND()"
167  
-
168 168
 def get_start_transaction_sql():
169 169
     return "BEGIN;"
170 170
 
6  django/db/backends/mysql_old/base.py
@@ -94,6 +94,9 @@ def limit_offset_sql(self, limit, offset=None):
94 94
             sql += "%s," % offset
95 95
         return sql + str(limit)
96 96
 
  97
+    def random_function_sql(self):
  98
+        return 'RAND()'
  99
+
97 100
 class DatabaseWrapper(BaseDatabaseWrapper):
98 101
     ops = DatabaseOperations()
99 102
 
@@ -181,9 +184,6 @@ def quote_name(name):
181 184
 dictfetchmany = util.dictfetchmany
182 185
 dictfetchall  = util.dictfetchall
183 186
 
184  
-def get_random_function_sql():
185  
-    return "RAND()"
186  
-
187 187
 def get_start_transaction_sql():
188 188
     return "BEGIN;"
189 189
 
8  django/db/backends/oracle/base.py
@@ -70,6 +70,9 @@ def limit_offset_sql(self, limit, offset=None):
70 70
     def max_name_length(self):
71 71
         return 30
72 72
 
  73
+    def random_function_sql(self):
  74
+        return "DBMS_RANDOM.RANDOM"
  75
+
73 76
 class DatabaseWrapper(BaseDatabaseWrapper):
74 77
     ops = DatabaseOperations()
75 78
 
@@ -186,9 +189,6 @@ def get_field_cast_sql(db_type):
186 189
     else:
187 190
         return "%s%s"
188 191
 
189  
-def get_random_function_sql():
190  
-    return "DBMS_RANDOM.RANDOM"
191  
-
192 192
 def get_start_transaction_sql():
193 193
     return None
194 194
 
@@ -380,7 +380,7 @@ def _get_sql_clause(self, get_full_query=False):
380 380
                 ordering_to_use = opts.ordering
381 381
             for f in handle_legacy_orderlist(ordering_to_use):
382 382
                 if f == '?': # Special case.
383  
-                    order_by.append(backend.get_random_function_sql())
  383
+                    order_by.append(DatabaseOperations().random_function_sql())
384 384
                 else:
385 385
                     if f.startswith('-'):
386 386
                         col_name = f[1:]
3  django/db/backends/postgresql/base.py
@@ -131,9 +131,6 @@ def dictfetchall(cursor):
131 131
     "Returns all rows from a cursor as a dict"
132 132
     return cursor.dictfetchall()
133 133
 
134  
-def get_random_function_sql():
135  
-    return "RANDOM()"
136  
-
137 134
 def get_start_transaction_sql():
138 135
     return "BEGIN;"
139 136
 
3  django/db/backends/postgresql_psycopg2/base.py
@@ -85,9 +85,6 @@ def quote_name(name):
85 85
 dictfetchmany = util.dictfetchmany
86 86
 dictfetchall = util.dictfetchall
87 87
 
88  
-def get_random_function_sql():
89  
-    return "RANDOM()"
90  
-
91 88
 def get_start_transaction_sql():
92 89
     return "BEGIN;"
93 90
 
3  django/db/backends/sqlite3/base.py
@@ -118,9 +118,6 @@ def _sqlite_extract(lookup_type, dt):
118 118
         return None
119 119
     return str(getattr(dt, lookup_type))
120 120
 
121  
-def get_random_function_sql():
122  
-    return "RANDOM()"
123  
-
124 121
 def get_start_transaction_sql():
125 122
     return "BEGIN;"
126 123
 
4  django/db/models/query.py
@@ -71,7 +71,7 @@ def orderlist2sql(order_list, opts, prefix=''):
71 71
         if f.startswith('-'):
72 72
             output.append('%s%s DESC' % (prefix, backend.quote_name(orderfield2column(f[1:], opts))))
73 73
         elif f == '?':
74  
-            output.append(backend.get_random_function_sql())
  74
+            output.append(connection.ops.random_function_sql())
75 75
         else:
76 76
             output.append('%s%s ASC' % (prefix, backend.quote_name(orderfield2column(f, opts))))
77 77
     return ', '.join(output)
@@ -531,7 +531,7 @@ def _get_sql_clause(self):
531 531
             ordering_to_use = opts.ordering
532 532
         for f in handle_legacy_orderlist(ordering_to_use):
533 533
             if f == '?': # Special case.
534  
-                order_by.append(backend.get_random_function_sql())
  534
+                order_by.append(connection.ops.random_function_sql())
535 535
             else:
536 536
                 if f.startswith('-'):
537 537
                     col_name = f[1:]

0 notes on commit c44fb66

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