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
44Basics:
@@ -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.
0 commit comments