Revision fbc263a9 lib/query.py
b/lib/query.py | ||
---|---|---|
629 | 629 |
""" |
630 | 630 |
return GetAllFields(self._fields) |
631 | 631 |
|
632 |
def Query(self, ctx): |
|
632 |
def Query(self, ctx, sort_by_name=True):
|
|
633 | 633 |
"""Execute a query. |
634 | 634 |
|
635 | 635 |
@param ctx: Data container passed to field retrieval functions, must |
636 | 636 |
support iteration using C{__iter__} |
637 |
@type sort_by_name: boolean |
|
638 |
@param sort_by_name: Whether to sort by name or keep the input data's |
|
639 |
ordering |
|
637 | 640 |
|
638 | 641 |
""" |
642 |
sort = (self._name_fn and sort_by_name) |
|
643 |
|
|
639 | 644 |
result = [] |
640 | 645 |
|
641 | 646 |
for idx, item in enumerate(ctx): |
... | ... | |
648 | 653 |
if __debug__: |
649 | 654 |
_VerifyResultRow(self._fields, row) |
650 | 655 |
|
651 |
if self._name_fn:
|
|
656 |
if sort:
|
|
652 | 657 |
(status, name) = _ProcessResult(self._name_fn(ctx, item)) |
653 | 658 |
assert status == constants.RS_NORMAL |
654 | 659 |
# TODO: Are there cases where we wouldn't want to use NiceSort? |
655 |
sortname = utils.NiceSortKey(name)
|
|
660 |
result.append((utils.NiceSortKey(name), idx, row))
|
|
656 | 661 |
else: |
657 |
sortname = None
|
|
662 |
result.append(row)
|
|
658 | 663 |
|
659 |
result.append((sortname, idx, row)) |
|
664 |
if not sort: |
|
665 |
return result |
|
660 | 666 |
|
661 | 667 |
# TODO: Would "heapq" be more efficient than sorting? |
662 | 668 |
|
... | ... | |
667 | 673 |
|
668 | 674 |
return map(operator.itemgetter(2), result) |
669 | 675 |
|
670 |
def OldStyleQuery(self, ctx): |
|
676 |
def OldStyleQuery(self, ctx, sort_by_name=True):
|
|
671 | 677 |
"""Query with "old" query result format. |
672 | 678 |
|
673 | 679 |
See L{Query.Query} for arguments. |
... | ... | |
681 | 687 |
errors.ECODE_INVAL) |
682 | 688 |
|
683 | 689 |
return [[value for (_, value) in row] |
684 |
for row in self.Query(ctx)] |
|
690 |
for row in self.Query(ctx, sort_by_name=sort_by_name)]
|
|
685 | 691 |
|
686 | 692 |
|
687 | 693 |
def _ProcessResult(value): |
... | ... | |
776 | 782 |
return result |
777 | 783 |
|
778 | 784 |
|
779 |
def GetQueryResponse(query, ctx): |
|
785 |
def GetQueryResponse(query, ctx, sort_by_name=True):
|
|
780 | 786 |
"""Prepares the response for a query. |
781 | 787 |
|
782 | 788 |
@type query: L{Query} |
783 | 789 |
@param ctx: Data container, see L{Query.Query} |
790 |
@type sort_by_name: boolean |
|
791 |
@param sort_by_name: Whether to sort by name or keep the input data's |
|
792 |
ordering |
|
784 | 793 |
|
785 | 794 |
""" |
786 |
return objects.QueryResponse(data=query.Query(ctx), |
|
795 |
return objects.QueryResponse(data=query.Query(ctx, sort_by_name=sort_by_name),
|
|
787 | 796 |
fields=query.GetFields()).ToDict() |
788 | 797 |
|
789 | 798 |
|
Also available in: Unified diff