Code

Ticket #5293: sql_function_in_order_by.diff

File sql_function_in_order_by.diff, 1.6 KB (added by rb.online@…, 7 years ago)

patch

Line 
1diff -ru django/db/models/query.py /Library/Frameworks/Python.framework/Versions/Current/lib/python2.5/site-packages/django/db/models/query.py
2--- django/db/models/query.py   2007-03-22 04:00:12.000000000 +0100
3+++ /Library/Frameworks/Python.framework/Versions/Current/lib/python2.5/site-packages/django/db/models/query.py 2007-08-29 15:49:42.000000000 +0200
4@@ -529,6 +529,12 @@
5                 else:
6                     col_name = f
7                     order = "ASC"
8+                if "(" in col_name:
9+                    b, e = col_name.find("("), col_name.find(")")
10+                    function = col_name[:b].upper()
11+                    col_name = col_name[b+1:e]
12+                else:
13+                    function = None
14                 if "." in col_name:
15                     table_prefix, col_name = col_name.split('.', 1)
16                     table_prefix = backend.quote_name(table_prefix) + '.'
17@@ -539,7 +545,10 @@
18                         table_prefix = backend.quote_name(opts.db_table) + '.'
19                     else:
20                         table_prefix = ''
21-                order_by.append('%s%s %s' % (table_prefix, backend.quote_name(orderfield2column(col_name, opts)), order))
22+                if function:
23+                    order_by.append('%s(%s%s) %s' % (function, table_prefix, backend.quote_name(orderfield2column(col_name, opts)), order))
24+                else:
25+                    order_by.append('%s%s %s' % (table_prefix, backend.quote_name(orderfield2column(col_name, opts)), order))
26         if order_by:
27             sql.append("ORDER BY " + ", ".join(order_by))