Skip to content

Commit b691b58

Browse files
author
Richard Jones
committed
sort/group by multilink in RDBMS
1 parent 651bf11 commit b691b58

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Fixed:
1919
download links (sf bug 927745)
2020
- all uses of TRACKER_WEB now ensure it ends with a '/'
2121
- roundup-admin install checks for existing tracker in target home
22+
- grouping (and sorting) by multilink in RDBMS backends (sf bug 655702)
2223

2324

2425
2004-03-27 0.7.0b2

roundup/backends/rdbms_common.py

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: rdbms_common.py,v 1.88 2004-04-02 05:58:45 richard Exp $
1+
# $Id: rdbms_common.py,v 1.89 2004-04-05 07:13:10 richard Exp $
22
''' Relational database (SQL) backend common code.
33
44
Basics:
@@ -2024,49 +2024,38 @@ def filter(self, search_matches, filterspec, sort=(None,None),
20242024
args = args + v
20252025

20262026
# "grouping" is just the first-order sorting in the SQL fetch
2027-
# can modify it...)
20282027
orderby = []
20292028
ordercols = []
2030-
if group[0] is not None and group[1] is not None:
2031-
if group[0] != '-':
2032-
orderby.append('_'+group[1])
2033-
ordercols.append('_'+group[1])
2034-
else:
2035-
orderby.append('_'+group[1]+' desc')
2036-
ordercols.append('_'+group[1])
2037-
2038-
# now add in the sorting
2039-
group = ''
2040-
if sort[0] is not None and sort[1] is not None:
2041-
direction, colname = sort
2042-
if direction != '-':
2043-
if colname == 'id':
2044-
orderby.append(colname)
2045-
else:
2046-
orderby.append('_'+colname)
2047-
ordercols.append('_'+colname)
2048-
else:
2049-
if colname == 'id':
2050-
orderby.append(colname+' desc')
2051-
ordercols.append(colname)
2029+
mlsort = []
2030+
for sortby in group, sort:
2031+
sdir, prop = sortby
2032+
if sdir and prop:
2033+
if isinstance(props[prop], Multilink):
2034+
mlsort.append(sortby)
2035+
continue
2036+
elif prop == 'id':
2037+
o = 'id'
20522038
else:
2053-
orderby.append('_'+colname+' desc')
2054-
ordercols.append('_'+colname)
2039+
o = '_'+prop
2040+
ordercols.append(o)
2041+
if sdir == '-':
2042+
o += ' desc'
2043+
orderby.append(o)
20552044

20562045
# construct the SQL
20572046
frum = ','.join(frum)
20582047
if where:
20592048
where = ' where ' + (' and '.join(where))
20602049
else:
20612050
where = ''
2062-
cols = ['id']
2051+
cols = ['distinct(id)']
20632052
if orderby:
20642053
cols = cols + ordercols
20652054
order = ' order by %s'%(','.join(orderby))
20662055
else:
20672056
order = ''
20682057
cols = ','.join(cols)
2069-
sql = 'select %s from %s %s%s%s'%(cols, frum, where, group, order)
2058+
sql = 'select %s from %s %s%s'%(cols, frum, where, order)
20702059
args = tuple(args)
20712060
if __debug__:
20722061
print >>hyperdb.DEBUG, 'filter', (self, sql, args)
@@ -2079,7 +2068,28 @@ def filter(self, search_matches, filterspec, sort=(None,None),
20792068

20802069
# return the IDs (the first column)
20812070
# XXX numeric ids
2082-
return [str(row[0]) for row in l]
2071+
l = [str(row[0]) for row in l]
2072+
2073+
if not mlsort:
2074+
return l
2075+
2076+
# ergh. someone wants to sort by a multilink.
2077+
r = []
2078+
for id in l:
2079+
m = []
2080+
for ml in mlsort:
2081+
m.append(self.get(id, ml[1]))
2082+
r.append((id, m))
2083+
i = 0
2084+
for sortby in mlsort:
2085+
def sortfun(a, b, dir=sortby[i]):
2086+
if dir == '-':
2087+
return cmp(b[1][i], a[1][i])
2088+
else:
2089+
return cmp(a[1][i], b[1][i])
2090+
r.sort(sortfun)
2091+
i += 1
2092+
return [i[0] for i in r]
20832093

20842094
def count(self):
20852095
'''Get the number of nodes in this class.

test/db_test_base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: db_test_base.py,v 1.20 2004-03-24 04:57:25 richard Exp $
18+
# $Id: db_test_base.py,v 1.21 2004-04-05 07:13:10 richard Exp $
1919

2020
import unittest, os, shutil, errno, imp, sys, time, pprint
2121

@@ -892,6 +892,11 @@ def testFilteringIntervalSort(self):
892892
# descending should sort 1d, 1:10, None
893893
ae(filt(None, {}, ('-','foo'), (None,None)), ['2', '1', '4', '3'])
894894

895+
def testFilteringMultilinkSort(self):
896+
ae, filt = self.filteringSetup()
897+
ae(filt(None, {}, ('+','nosy'), (None,None)), ['1', '2', '4', '3'])
898+
ae(filt(None, {}, ('-','nosy'), (None,None)), ['3', '4', '1', '2'])
899+
895900
# XXX add sorting tests for other types
896901
# XXX test auditors and reactors
897902

0 commit comments

Comments
 (0)