def test_paginate_query_null(self): self.mox.StubOutWithMock(self.model.user_id, 'isnot') self.model.user_id.isnot(None).AndReturn('asc_null_1') sqlalchemy.desc('asc_null_1').AndReturn('asc_null_2') self.query.order_by('asc_null_2').AndReturn(self.query) sqlalchemy.asc(self.model.user_id).AndReturn('asc_1') self.query.order_by('asc_1').AndReturn(self.query) self.mox.StubOutWithMock(self.model.project_id, 'is_') self.model.project_id.is_(None).AndReturn('desc_null_1') sqlalchemy.desc('desc_null_1').AndReturn('desc_null_2') self.query.order_by('desc_null_2').AndReturn(self.query) sqlalchemy.desc(self.model.project_id).AndReturn('desc_1') self.query.order_by('desc_1').AndReturn(self.query) self.mox.StubOutWithMock(sqlalchemy.sql, 'and_') sqlalchemy.sql.and_(mock.ANY).AndReturn('some_crit') sqlalchemy.sql.and_(mock.ANY, mock.ANY).AndReturn('another_crit') self.mox.StubOutWithMock(sqlalchemy.sql, 'or_') sqlalchemy.sql.or_('some_crit', 'another_crit').AndReturn('some_f') self.query.filter('some_f').AndReturn(self.query) self.query.limit(5).AndReturn(self.query) self.mox.ReplayAll() utils.paginate_query(self.query, self.model, 5, ['user_id', 'project_id'], marker=self.marker, sort_dirs=['asc-nullslast', 'desc-nullsfirst'])
def test_paginate_query_no_pagination(self): sqlalchemy.asc(self.model.user_id).AndReturn('asc') self.query.order_by('asc').AndReturn(self.query) sqlalchemy.desc(self.model.project_id).AndReturn('desc') self.query.order_by('desc').AndReturn(self.query) self.query.limit(5).AndReturn(self.query) self.mox.ReplayAll() utils.paginate_query(self.query, self.model, 5, ['user_id', 'project_id'], sort_dirs=['asc', 'desc'])
def test_paginate_query_no_pagination_no_sort_dirs(self): sqlalchemy.asc(self.model.user_id).AndReturn('asc_3') self.query.order_by('asc_3').AndReturn(self.query) sqlalchemy.asc(self.model.project_id).AndReturn('asc_2') self.query.order_by('asc_2').AndReturn(self.query) sqlalchemy.asc(self.model.snapshot_id).AndReturn('asc_1') self.query.order_by('asc_1').AndReturn(self.query) self.query.limit(5).AndReturn(self.query) self.mox.ReplayAll() utils.paginate_query(self.query, self.model, 5, ['user_id', 'project_id', 'snapshot_id'])
def test_paginate_on_hybrid(self): sqlalchemy.asc(self.model.user_id).AndReturn('asc_1') self.query.order_by('asc_1').AndReturn(self.query) sqlalchemy.desc(self.model.some_hybrid).AndReturn('desc_1') self.query.order_by('desc_1').AndReturn(self.query) self.query.limit(5).AndReturn(self.query) self.mox.ReplayAll() utils.paginate_query(self.query, self.model, 5, ['user_id', 'some_hybrid'], sort_dirs=['asc', 'desc'])
def _network_find(context, limit, sorts, marker, page_reverse, fields, defaults=None, provider_query=False, **filters): query = context.session.query(models.Network) model_filters = _model_query(context, models.Network, filters, query) if defaults: invert_defaults = False if INVERT_DEFAULTS in defaults: invert_defaults = True defaults.pop(0) if filters and invert_defaults: query = query.filter(and_(not_(models.Network.id.in_(defaults)), and_(*model_filters))) elif not provider_query and filters and not invert_defaults: query = query.filter(or_(models.Network.id.in_(defaults), and_(*model_filters))) elif not invert_defaults: query = query.filter(models.Network.id.in_(defaults)) else: query = query.filter(*model_filters) if "join_subnets" in filters: query = query.options(orm.joinedload(models.Network.subnets)) return paginate_query(query, models.Network, limit, sorts, marker)
def get_all(context, namespace_name, session, filters=None, marker=None, limit=None, sort_key='created_at', sort_dir='desc'): """Get all tags that match zero or more filters. :param filters: dict of filter keys and values. :param marker: tag id after which to start page :param limit: maximum number of namespaces to return :param sort_key: namespace attribute by which results should be sorted :param sort_dir: direction in which results should be sorted (asc, desc) """ namespace = namespace_api.get(context, namespace_name, session) query = (session.query(models.MetadefTag).filter_by( namespace_id=namespace['id'])) marker_tag = None if marker is not None: marker_tag = _get(context, marker, session) sort_keys = ['created_at', 'id'] sort_keys.insert(0, sort_key) if sort_key not in sort_keys else sort_keys query = paginate_query(query=query, model=models.MetadefTag, limit=limit, sort_keys=sort_keys, marker=marker_tag, sort_dir=sort_dir) metadef_tag = query.all() metadef_tag_list = [] for tag in metadef_tag: metadef_tag_list.append(tag.to_dict()) return metadef_tag_list
def _events_paginate_query(context, query, model, limit=None, sort_keys=None, marker=None, sort_dir=None): default_sort_keys = ['created_at'] if not sort_keys: sort_keys = default_sort_keys if not sort_dir: sort_dir = 'desc' # This assures the order of the stacks will always be the same # even for sort_key values that are not unique in the database sort_keys = sort_keys + ['id'] model_marker = None if marker: # not to use model_query(context, model).get(marker), because # user can only see the ID(column 'uuid') and the ID as the marker model_marker = model_query( context, model).filter_by(uuid=marker).first() try: query = utils.paginate_query(query, model, limit, sort_keys, model_marker, sort_dir) except utils.InvalidSortKey as exc: raise exception.Invalid(reason=exc.message) return query
def port_find(context, limit=None, sorts=['id'], marker_obj=None, fields=None, **filters): query = context.session.query(models.Port).options( orm.joinedload(models.Port.ip_addresses)) model_filters = _model_query(context, models.Port, filters) if filters.get("ip_address_id"): model_filters.append(models.Port.ip_addresses.any( models.IPAddress.id.in_(filters["ip_address_id"]))) if filters.get("device_id"): model_filters.append(models.Port.device_id.in_(filters["device_id"])) if filters.get("service"): model_filters.append(models.Port.associations.any( models.PortIpAssociation.service == filters["service"])) if "join_security_groups" in filters: query = query.options(orm.joinedload(models.Port.security_groups)) if fields and "port_subnets" in fields: query = query.options(orm.joinedload("ip_addresses.subnet")) query = query.options( orm.joinedload("ip_addresses.subnet.dns_nameservers")) query = query.options( orm.joinedload("ip_addresses.subnet.routes")) return paginate_query(query.filter(*model_filters), models.Port, limit, sorts, marker_obj)
def _get_pagination_query(query, pagination, api_model, model): if not pagination.get('sort'): pagination['sort'] = api_model.DEFAULT_SORT marker = None if pagination.get('marker'): key_attr = getattr(model, api_model.PRIMARY_KEY) marker_query = copy.copy(query) marker_query = marker_query.filter( key_attr == pagination['marker']) try: marker = marker_query.limit(1).one() except exc.NoResultFound: raise storage.InvalidMarker( 'Marker %s not found.' % pagination['marker']) limit = pagination.get('limit') # we sort by "severity" by its semantic than its alphabetical # order when "severity" specified in sorts. for sort_key, sort_dir in pagination['sort'][::-1]: if sort_key == 'severity': sort_dir_func = {'asc': asc, 'desc': desc}[sort_dir] query = query.order_by(sort_dir_func( func.field(getattr(model, sort_key), 'low', 'moderate', 'critical'))) pagination['sort'].remove((sort_key, sort_dir)) sort_keys = [s[0] for s in pagination['sort']] sort_dirs = [s[1] for s in pagination['sort']] return oslo_sql_utils.paginate_query( query, model, limit, sort_keys, sort_dirs=sort_dirs, marker=marker)
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)
def _paginate_query(model, limit=None, marker=None, sort_keys=None, sort_dirs=None, query=None): if not query: query = _secure_query(model) query = db_utils.paginate_query( query, model, limit, sort_keys if sort_keys else {}, marker=marker, sort_dirs=sort_dirs ) return query.all()
def _paginate_query(model, limit=None, marker=None, sort_key=None, sort_dir=None, query=None): if not query: query = model_query(model) sort_keys = ['id'] if sort_key and sort_key not in sort_keys: sort_keys.insert(0, sort_key) query = db_utils.paginate_query(query, model, limit, sort_keys, marker=marker, sort_dir=sort_dir) return query.all()
def _event_filter_paginate_query(context, query, filters=None, limit=None, marker=None, sort=None): if filters: query = utils.exact_filter(query, models.Event, filters) keys, dirs = utils.get_sort_params(sort, consts.EVENT_TIMESTAMP) if marker: marker = model_query(context, models.Event).get(marker) return sa_utils.paginate_query(query, models.Event, limit, keys, marker=marker, sort_dirs=dirs).all()
def cluster_policy_get_all(context, cluster_id, filters=None, sort=None): query = model_query(context, models.ClusterPolicies) query = query.filter_by(cluster_id=cluster_id) if filters: query = utils.exact_filter(query, models.ClusterPolicies, filters) keys, dirs = utils.get_sort_params(sort) return sa_utils.paginate_query(query, models.ClusterPolicies, None, keys, sort_dirs=dirs).all()
def notifications_get_all_by_filters( context, filters=None, sort_keys=None, sort_dirs=None, limit=None, marker=None): # NOTE(Dinesh_Bhor): If the limit is 0 there is no point in even going # to the database since nothing is going to be returned anyway. if limit == 0: return [] sort_keys, sort_dirs = _process_sort_params(sort_keys, sort_dirs) filters = filters or {} query = model_query(context, models.Notification) if 'source_host_uuid' in filters: query = query.filter(models.Notification.source_host_uuid == filters[ 'source_host_uuid']) if 'type' in filters: query = query.filter(models.Notification.type == filters['type']) if 'status' in filters: status = filters['status'] if isinstance(status, (list, tuple, set, frozenset)): column_attr = getattr(models.Notification, 'status') query = query.filter(column_attr.in_(status)) else: query = query.filter(models.Notification.status == status) if 'generated-since' in filters: generated_since = timeutils.normalize_time(filters['generated-since']) query = query.filter( models.Notification.generated_time >= generated_since) marker_row = None if marker is not None: marker_row = model_query(context, models.Notification ).filter_by(id=marker).first() if not marker_row: raise exception.MarkerNotFound(marker=marker) try: query = sqlalchemyutils.paginate_query(query, models.Notification, limit, sort_keys, marker=marker_row, sort_dirs=sort_dirs) except db_exc.InvalidSortKey as err: raise exception.InvalidSortKey(err.message) return query.all()
def list(self, limit, marker): """List subscriptions.""" with self._reader() as session: query = session.query(models.Subscription).filter_by( project_id=self._context.project_id) if marker is not None: try: marker = self.get(marker) except exceptions.NotFound: return [] return utils.paginate_query(query, models.Subscription, limit, ['id'], marker)
def cluster_get_all(context, limit=None, marker=None, sort=None, filters=None, project_safe=True): query = _query_cluster_get_all(context, project_safe=project_safe) if filters: query = utils.exact_filter(query, models.Cluster, filters) keys, dirs = utils.get_sort_params(sort, consts.CLUSTER_INIT_AT) if marker: marker = model_query(context, models.Cluster).get(marker) return sa_utils.paginate_query(query, models.Cluster, limit, keys, marker=marker, sort_dirs=dirs).all()
def node_get_all(context, cluster_id=None, limit=None, marker=None, sort=None, filters=None, project_safe=True): query = _query_node_get_all(context, project_safe=project_safe, cluster_id=cluster_id) if filters: query = utils.exact_filter(query, models.Node, filters) keys, dirs = utils.get_sort_params(sort, consts.NODE_INIT_AT) if marker: marker = model_query(context, models.Node).get(marker) return sa_utils.paginate_query(query, models.Node, limit, keys, marker=marker, sort_dirs=dirs).all()
def categories_list(filters=None, limit=None, marker=None): if filters is None: filters = {} sort_keys = filters.get("sort_keys", ["name"]) sort_dir = filters.get("sort_dir", "asc") session = db_session.get_session() query = session.query(models.Category) if marker is not None: marker = category_get(marker, session) query = utils.paginate_query(query, models.Category, limit, sort_keys, marker, sort_dir) return query.all()
def _paginate_query(model, limit=None, marker=None, sort_key=None, sort_dir=None, query=None): if not query: query = model_query(model) sort_keys = ["id"] if sort_key and sort_key not in sort_keys: sort_keys.insert(0, sort_key) try: query = db_utils.paginate_query(query, model, limit, sort_keys, marker=marker, sort_dir=sort_dir) except db_exc.InvalidSortKey: raise exception.InvalidParameterValue( _('The sort_key value "%(key)s" is an invalid field for sorting') % {"key": sort_key} ) return query.all()
def receiver_get_all(context, limit=None, marker=None, filters=None, sort=None, project_safe=True): query = model_query(context, models.Receiver) if not context.is_admin and project_safe: query = query.filter_by(project=context.project) if filters: query = utils.exact_filter(query, models.Receiver, filters) keys, dirs = utils.get_sort_params(sort, consts.RECEIVER_NAME) if marker: marker = model_query(context, models.Receiver).get(marker) return sa_utils.paginate_query(query, models.Receiver, limit, keys, marker=marker, sort_dirs=dirs).all()
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
def _generate_paginate_query(context, session, marker, limit, sort_keys, sort_dirs, filters, offset=None, paginate_type=models.Plan, use_model=False, **kwargs): """Generate the query to include the filters and the paginate options. Returns a query with sorting / pagination criteria added or None if the given filters will not yield any results. :param context: context to query under :param session: the session to use :param marker: the last item of the previous page; we returns the next results after this value. :param limit: maximum number of items to return :param sort_keys: list of attributes by which results should be sorted, paired with corresponding item in sort_dirs :param sort_dirs: list of directions in which results should be sorted, paired with corresponding item in sort_keys :param filters: dictionary of filters; values that are in lists, tuples, or sets cause an 'IN' operation, while exact matching is used for other values, see _process_plan_filters function for more information :param offset: number of items to skip :param paginate_type: type of pagination to generate :returns: updated query or None """ get_query, process_filters, get = PAGINATION_HELPERS[paginate_type] sort_keys, sort_dirs = process_sort_params(sort_keys, sort_dirs, default_dir='desc') if use_model: query = get_query(context, session=session, **kwargs) else: query = get_query(context, session=session) if filters: query = process_filters(query, filters) if query is None: return None marker_object = None if marker is not None: marker_object = get(context, marker, session=session) return sqlalchemyutils.paginate_query(query, paginate_type, limit, sort_keys, marker=marker_object, sort_dirs=sort_dirs)
def profile_get_all(context, limit=None, marker=None, sort=None, filters=None, project_safe=True): query = model_query(context, models.Profile) if not context.is_admin and project_safe: query = query.filter_by(project=context.project) if filters: query = utils.exact_filter(query, models.Profile, filters) keys, dirs = utils.get_sort_params(sort, consts.PROFILE_CREATED_AT) if marker: marker = model_query(context, models.Profile).get(marker) return sa_utils.paginate_query(query, models.Profile, limit, keys, marker=marker, sort_dirs=dirs).all()
def action_get_all(context, filters=None, limit=None, marker=None, sort=None, project_safe=True): query = model_query(context, models.Action) # TODO(Qiming): Enable multi-tenancy for actions # if project_safe: # query = query.filter_by(project=context.project) if filters: query = utils.exact_filter(query, models.Action, filters) keys, dirs = utils.get_sort_params(sort, consts.ACTION_CREATED_AT) if marker: marker = model_query(context, models.Action).get(marker) return sa_utils.paginate_query(query, models.Action, limit, keys, marker=marker, sort_dirs=dirs).all()
def _paginate_query(context, query, model, limit=None, marker=None, sort_keys=None, sort_dir=None, default_sort_keys=None): if not sort_keys: sort_keys = default_sort_keys or [] if not sort_dir: sort_dir = 'asc' model_marker = None if marker: model_marker = model_query(context, model).get(marker) try: query = utils.paginate_query(query, model, limit, sort_keys, model_marker, sort_dir) except utils.InvalidSortKey: raise exception.InvalidParameter(name='sort_keys', value=sort_keys) return query
def host_get_all_by_filters( context, filters=None, sort_keys=None, sort_dirs=None, limit=None, marker=None): # NOTE(Dinesh_Bhor): If the limit is 0 there is no point in even going # to the database since nothing is going to be returned anyway. if limit == 0: return [] sort_keys, sort_dirs = _process_sort_params(sort_keys, sort_dirs) filters = filters or {} query = model_query(context, models.Host).options(joinedload('failover_segment')) if 'failover_segment_id' in filters: query = query.filter(models.Host.failover_segment_id == filters[ 'failover_segment_id']) if 'type' in filters: query = query.filter(models.Host.type == filters['type']) if 'on_maintenance' in filters: query = query.filter(models.Host.on_maintenance == filters[ 'on_maintenance']) if 'reserved' in filters: query = query.filter(models.Host.reserved == filters['reserved']) marker_row = None if marker is not None: marker_row = model_query(context, models.Host ).filter_by(id=marker).first() if not marker_row: raise exception.MarkerNotFound(marker=marker) try: query = sqlalchemyutils.paginate_query(query, models.Host, limit, sort_keys, marker=marker_row, sort_dirs=sort_dirs) except db_exc.InvalidSortKey as e: raise exception.InvalidSortKey(e.message) return query.all()
def _paginate(context, query, model, session, filters, pagination_params, project_only=False): # NOTE(sigmavirus24) Retrieve the instance of the model represented by the # marker. try: marker = _marker_from(context, session, model, pagination_params['marker'], project_only) except sa_exc.NoResultFound: raise exceptions.BadRequest( message='Marker "{}" does not exist'.format( pagination_params['marker'] ) ) except Exception as err: raise exceptions.UnknownException(message=err) filters.setdefault('sort_keys', ['created_at', 'id']) filters.setdefault('sort_dir', 'asc') # Retrieve the results based on the marker and the limit try: results = db_utils.paginate_query( query, model, limit=pagination_params['limit'], sort_keys=filters['sort_keys'], sort_dir=filters['sort_dir'], marker=marker, ).all() except sa_exc.NoResultFound: raise exceptions.NotFound() except db_exc.InvalidSortKey as invalid_key: raise exceptions.BadRequest( message='"{}" is an invalid sort key'.format(invalid_key.key) ) except Exception as err: raise exceptions.UnknownException(message=err) try: links = _link_params_for( query, model, filters, pagination_params, marker, results, ) except Exception as err: raise exceptions.UnknownException(message=err) return results, links
def _get_all(context, session, filters=None, marker=None, limit=None, sort_key='created_at', sort_dir='desc'): """Get all namespaces that match zero or more filters. :param filters: dict of filter keys and values. :param marker: namespace id after which to start page :param limit: maximum number of namespaces to return :param sort_key: namespace attribute by which results should be sorted :param sort_dir: direction in which results should be sorted (asc, desc) """ filters = filters or {} query = _select_namespaces_query(context, session) # if visibility filter, apply it to the context based query visibility = filters.pop('visibility', None) if visibility is not None: query = query.filter(models.MetadefNamespace.visibility == visibility) # if id_list filter, apply it to the context based query id_list = filters.pop('id_list', None) if id_list is not None: query = query.filter(models.MetadefNamespace.id.in_(id_list)) marker_namespace = None if marker is not None: marker_namespace = _get(context, marker, session) sort_keys = ['created_at', 'id'] sort_keys.insert(0, sort_key) if sort_key not in sort_keys else sort_keys query = paginate_query(query=query, model=models.MetadefNamespace, limit=limit, sort_keys=sort_keys, marker=marker_namespace, sort_dir=sort_dir) return query.all()
def test_paginate_on_hybrid_assert_stmt(self): s = Session() q = s.query(FakeTable) q = utils.paginate_query( q, FakeTable, 5, ['user_id', 'some_hybrid'], sort_dirs=['asc', 'desc']) expected_core_sql = ( select([FakeTable]). order_by(sqlalchemy.asc(FakeTable.user_id)). order_by(sqlalchemy.desc(FakeTable.some_hybrid)). limit(5) ) self.assertEqual( str(q.statement.compile()), str(expected_core_sql.compile()) )
def get_switchport_bindings(self, context, filters=None, fields=None, sorts=None, limit=None, marker=None, page_reverse=False): query = context.session.query( nuage_models.NuageSwitchportBinding, nuage_models.NuageSwitchportMapping.switch_id, nuage_models.NuageSwitchportMapping.port_id) query = query.outerjoin( nuage_models.NuageSwitchportMapping, nuage_models.NuageSwitchportMapping.port_uuid == nuage_models.NuageSwitchportBinding.switchport_uuid) query = query.distinct() if filters: for key, value in six.iteritems(filters): column = getattr( nuage_models.NuageSwitchportBinding, key, None) if column is None: column = getattr( nuage_models.NuageSwitchportMapping, key, None) if column is not None: if not value: query = query.filter(sql.false()) else: query = query.filter(column.in_(value)) if sorts: marker_obj = lib_db_utils.get_marker_obj(self, context, 'switchport_bindings', limit, marker) sort_dirs = ['asc' if s[1] else 'desc' for s in sorts] query = sa_utils.paginate_query( query, nuage_models.NuageSwitchportBinding, limit, marker=marker_obj, sort_keys=sorts, sort_dirs=sort_dirs) items = [self._make_switchport_binding_dict_from_tuple(c, fields) for c in query] if limit and page_reverse: items.reverse() return items
def _flavor_get_all_from_db(context, inactive, filters, sort_key, sort_dir, limit, marker): """Returns all flavors. """ filters = filters or {} query = Flavor._flavor_get_query_from_db(context) if 'min_memory_mb' in filters: query = query.filter( api_models.Flavors.memory_mb >= filters['min_memory_mb']) if 'min_root_gb' in filters: query = query.filter( api_models.Flavors.root_gb >= filters['min_root_gb']) if 'disabled' in filters: query = query.filter( api_models.Flavors.disabled == filters['disabled']) if 'is_public' in filters and filters['is_public'] is not None: the_filter = [api_models.Flavors.is_public == filters['is_public']] if filters['is_public'] and context.project_id is not None: the_filter.extend([api_models.Flavors.projects.any( project_id=context.project_id)]) if len(the_filter) > 1: query = query.filter(or_(*the_filter)) else: query = query.filter(the_filter[0]) marker_row = None if marker is not None: marker_row = Flavor._flavor_get_query_from_db(context).\ filter_by(flavorid=marker).\ first() if not marker_row: raise exception.MarkerNotFound(marker=marker) query = sqlalchemyutils.paginate_query(query, api_models.Flavors, limit, [sort_key, 'id'], marker=marker_row, sort_dir=sort_dir) return [_dict_with_extra_specs(i) for i in query.all()]
def _subnet_find(context, limit, sorts, marker, page_reverse, fields, defaults=None, provider_query=False, **filters): query = context.session.query(models.Subnet) model_filters = _model_query(context, models.Subnet, filters, query) if defaults: invert_defaults = False if INVERT_DEFAULTS in defaults: invert_defaults = True defaults.pop(0) # when 'invert_defaults' were the only entry in defaults, # defaults will be empty now. The next 4 lines optimize # performance by avoiding running the in_ filter on an empty set: # like so: models.Subnet.id.in_([]) if defaults: subnet_filter = models.Subnet.id.in_(defaults) else: # if defaults is an empty list, just create a False # BinaryExpression subnet_filter = models.Subnet.id != models.Subnet.id if filters and invert_defaults: query = query.filter(and_(not_(subnet_filter), and_(*model_filters))) elif not provider_query and filters and not invert_defaults: query = query.filter(or_(subnet_filter, and_(*model_filters))) elif not invert_defaults: query = query.filter(subnet_filter) else: query = query.filter(*model_filters) if "join_dns" in filters: query = query.options(orm.joinedload(models.Subnet.dns_nameservers)) if "join_routes" in filters: query = query.options(orm.joinedload(models.Subnet.routes)) if "join_pool" in filters: query = query.options(orm.undefer('_allocation_pool_cache')) return paginate_query(query, models.Subnet, limit, sorts, marker)
def failover_segment_get_all_by_filters(context, filters=None, sort_keys=None, sort_dirs=None, limit=None, marker=None): # NOTE(Dinesh_Bhor): If the limit is 0 there is no point in even going # to the database since nothing is going to be returned anyway. if limit == 0: return [] sort_keys, sort_dirs = _process_sort_params(sort_keys, sort_dirs) filters = filters or {} query = model_query(context, models.FailoverSegment) if 'recovery_method' in filters: query = query.filter(models.FailoverSegment.recovery_method == filters['recovery_method']) if 'service_type' in filters: query = query.filter( models.FailoverSegment.service_type == filters['service_type']) marker_row = None if marker is not None: marker_row = model_query( context, models.FailoverSegment).filter_by(id=marker).first() if not marker_row: raise exception.MarkerNotFound(marker=marker) try: query = sqlalchemyutils.paginate_query(query, models.FailoverSegment, limit, sort_keys, marker=marker_row, sort_dirs=sort_dirs) except db_exc.InvalidSortKey as e: raise exception.InvalidSortKey(e) return query.all()
def _get_previous(query, model, current_marker, page_size, filters): # NOTE(sigmavirus24): To get the previous items based on the existing # filters, we need only reverse the direction that the user requested. original_sort_dir = filters['sort_dir'] sort_dir = 'desc' if original_sort_dir == 'desc': sort_dir = 'asc' results = db_utils.paginate_query( query, model, limit=page_size, sort_keys=filters['sort_keys'], sort_dir=sort_dir, marker=current_marker, ).all() if not results: return None return results[-1].id
def _get_pagination_query(query, pagination, api_model, model): if not pagination.get('sort'): pagination['sort'] = api_model.DEFAULT_SORT marker = None if pagination.get('marker'): key_attr = getattr(model, api_model.PRIMARY_KEY) marker_query = copy.copy(query) marker_query = marker_query.filter( key_attr == pagination['marker']) try: marker = marker_query.limit(1).one() except exc.NoResultFound: raise storage.InvalidMarker('Marker %s not found.' % pagination['marker']) limit = pagination.get('limit') sort_keys = [s[0] for s in pagination['sort']] sort_dirs = [s[1] for s in pagination['sort']] return oslo_sql_utils.paginate_query(query, model, limit, sort_keys, sort_dirs=sort_dirs, marker=marker)
def _paginate_query(model, limit=None, marker=None, sort_key=None, sort_dir=None, query=None, default_sort_key='id'): if not query: query = model_query(model) sort_keys = [default_sort_key] if sort_key and sort_key not in sort_keys: sort_keys.insert(0, sort_key) try: query = db_utils.paginate_query(query, model, limit, sort_keys, marker=marker, sort_dir=sort_dir) except db_exc.InvalidSortKey: raise exception.InvalidParameterValue( _('The sort_key value "%(key)s" is an invalid field for sorting') % {'key': sort_key}) return query.all()