示例#1
0
def paginate_query(query, model, limit, sorts, marker_obj=None):
    if not sorts:
        return query

    sort_keys = db_utils.get_and_validate_sort_keys(sorts, model)
    sort_dirs = ['asc' if s[1] else 'desc' for s in sorts]
    return sa_utils.paginate_query(query, model, limit, marker=marker_obj,
                                   sort_keys=sort_keys, sort_dirs=sort_dirs)
示例#2
0
 def _get_collection_query(self, context, model, filters=None,
                           sorts=None, limit=None, marker_obj=None,
                           page_reverse=False):
     collection = self._model_query(context, model)
     collection = self._apply_filters_to_query(collection, model, filters,
                                               context)
     if sorts:
         sort_keys = db_utils.get_and_validate_sort_keys(sorts, model)
         sort_dirs = db_utils.get_sort_dirs(sorts, page_reverse)
         collection = sa_utils.paginate_query(collection, model, limit,
                                              marker=marker_obj,
                                              sort_keys=sort_keys,
                                              sort_dirs=sort_dirs)
     return collection
示例#3
0
def get_collection_query(context, model, filters=None, sorts=None, limit=None,
                         marker_obj=None, page_reverse=False):
    collection = query_with_hooks(context, model)
    collection = apply_filters(collection, model, filters, context)
    if sorts:
        sort_keys = db_utils.get_and_validate_sort_keys(sorts, model)
        sort_dirs = db_utils.get_sort_dirs(sorts, page_reverse)
        # we always want deterministic results for sorted queries
        # so add unique keys to limit queries when present.
        # (http://docs.sqlalchemy.org/en/latest/orm/
        #  loading_relationships.html#subqueryload-ordering)
        # (http://docs.sqlalchemy.org/en/latest/faq/
        #  ormconfiguration.html#faq-subqueryload-limit-sort)
        for k in _unique_keys(model):
            if k not in sort_keys:
                sort_keys.append(k)
                sort_dirs.append('asc')
        collection = sa_utils.paginate_query(collection, model, limit,
                                             marker=marker_obj,
                                             sort_keys=sort_keys,
                                             sort_dirs=sort_dirs)
    return collection
示例#4
0
 def test_get_and_validate_sort_keys(self):
     sorts = [('name', False), ('status', True)]
     self.assertEqual(['name', 'status'],
                      utils.get_and_validate_sort_keys(sorts, FakePort))