Re: [SQLObject] Specifying defaultOrder
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
|
From: Luke O. <lu...@me...> - 2003-06-19 15:38:28
|
> > Sort order cannot currently be specified in SO.
>
> Should say something about the direction that development should be taking.
>
> Nick
Alright, here's what it'll take (from current CVS) to get order direction:
Questions:
Can you think of better names than "_defaultOrderDirection" and
"orderDirection"?
Should the value be the strings "DESC"/"ASC", be case-insensitive, be some
python representation of these strings? Accept strings but turn them into some
python rep?
1. Modify SQLObject to specify _defaultOrderDirection = None in class vars.
2. Modify SQLObject.select() to take keyword "orderDirection", and pass to
SelectResults.
def select(cls, clause=None, clauseTables=None,
orderBy=NoDefault, orderDirection=NoDefault,
groupBy=None, limit=None, lazyColumns=False):
return SelectResults(cls, clause, clauseTables=clauseTables,
orderBy=orderBy, orderDirection=orderDirection,
groupBy=groupBy, limit=limit, lazyColumns=lazyColumns)
3. Modify SelectResults (SQLObject.py) class to set ops['orderDirection'] =
sourceClass._defaultOrderDirection if not specified.
if self.ops.get('orderDirection', NoDefault) is NoDefault:
self.ops['orderDirection'] = sourceClass._defaultOrderDirection
4. Modify DBConnection.whereClauseForSelect() to actually insert the DESC/ASC
word if ops['orderDirection'] is set.
if order and ops.get('dbOrderBy'):
q = "%s ORDER BY %s" % (q, ", ".join(clauseList(ops['dbOrderBy'])))
+ if ops.get('orderDirection',None):
+ q = "%s %s" % (q, ops['orderDirection'])
(Also, DBMConnection implements it's own iterSelect/DBMSelectResults,
presumably because it doesn't support ORDER BY natively. This will need to be
modified to swap comparisons much like Joins below.)
4. Modify SOJoin.__init__() to accept an orderDirection keyword, modify
SOJoin._applyOrder() to use defaultSortClass._defaultOrderDirection if None
given, and to use a reverseSorter that swaps the comparison if
orderDirection.upper() == 'DESC'
def _applyOrderBy(self, results, defaultSortClass):
if self.orderBy is NoDefault:
self.orderBy = defaultSortClass._defaultOrder
+ if self.orderDirection is NoDefault:
+ self.orderDirection = defaultSortClass._defaultOrderDirection
if self.orderBy is not None:
def sorter(a, b, attr=self.orderBy):
return cmp(getattr(a, attr),
getattr(b, attr))
+ def revSorter(a, b, attr=self.orderBy):
+ return cmp(getattr(a, attr), getattr(b, attr))
+ if self.orderDirection.upper() == 'DESC'
results.sort(revSorter)
+ else:
+ results.sort(sorter)
return results
And I think that should be everything. I haven't tested all of this yet, and it
can't be committed till Ian returns, but comments are welcome.
- Luke
|