def _find_customr(self, model, context, criterion, one=False, marker=None, limit=None, sort_key=None, sort_dir=None): """ Base "finder" method Used to abstract these details from all the _find_*() methods. """ # First up, create a query and apply the various filters query = self.session.query(model) query = self._apply_criterion(model, query, criterion) query = self._apply_deleted_criteria(context, model, query) query = self._apply_fetch_record(context, model, query) if one: # If we're asked to return exactly one record, but multiple or # none match, raise a NotFound try: return query.one() except (exc.NoResultFound, exc.MultipleResultsFound): raise exceptions.NotFound() else: # If marker is not none and basestring we query it. # Otherwise, return all matching records if marker is not None: try: marker = self._find(model, context, {'id': marker}, one=True) except exceptions.NotFound: raise exceptions.MarkerNotFound( 'Marker %s could not be found' % marker) # Malformed UUIDs return StatementError except sqlalchemy_exc.StatementError as statement_error: raise exceptions.InvalidMarker(statement_error.message) sort_key = sort_key or 'created_at' sort_dir = sort_dir or 'asc' try: query = paginate_query(query, model, limit, [sort_key, 'id', 'created_at'], marker=marker, sort_dir=sort_dir) return query.all() except InvalidSortKey as sort_key_error: raise exceptions.InvalidSortKey(sort_key_error.message) # Any ValueErrors are propagated back to the user as is. # Limits, sort_dir and sort_key are checked at the API layer. # If however central or storage is called directly, invalid values # show up as ValueError except ValueError as value_error: raise exceptions.ValueError(value_error.message)
def check_marker(table, marker, session): marker_query = select([table]).where(table.c.id == marker) try: marker_resultproxy = session.execute(marker_query) marker = marker_resultproxy.fetchone() if marker is None: raise exceptions.MarkerNotFound('Marker %s could not be found' % marker) except oslo_db_exception.DBError as e: # Malformed UUIDs return StatementError wrapped in a # DBError if isinstance(e.inner_exception, sqlalchemy_exc.StatementError): raise exceptions.InvalidMarker() else: raise return marker
def _find(self, context, table, cls, list_cls, exc_notfound, criterion, one=False, marker=None, limit=None, sort_key=None, sort_dir=None, query=None, apply_tenant_criteria=True): sort_key = sort_key or 'created_at' sort_dir = sort_dir or 'asc' # Build the query if query is None: query = select([table]) query = self._apply_criterion(table, query, criterion) if apply_tenant_criteria: query = self._apply_tenant_criteria(context, table, query) query = self._apply_deleted_criteria(context, table, query) # Execute the Query if one: # NOTE(kiall): If we expect one value, and two rows match, we raise # a NotFound. Limiting to 2 allows us to determine # when we need to raise, while selecting the minimal # number of rows. resultproxy = self.session.execute(query.limit(2)) results = resultproxy.fetchall() if len(results) != 1: raise exc_notfound() else: return _set_object_from_model(cls(), results[0]) else: if marker is not None: # If marker is not none and basestring we query it. # Otherwise, return all matching records marker_query = select([table]).where(table.c.id == marker) try: marker_resultproxy = self.session.execute(marker_query) marker = marker_resultproxy.fetchone() if marker is None: raise exceptions.MarkerNotFound( 'Marker %s could not be found' % marker) except oslo_db_exception.DBError as e: # Malformed UUIDs return StatementError wrapped in a # DBError if isinstance(e.inner_exception, sqlalchemy_exc.StatementError): raise exceptions.InvalidMarker() else: raise try: query = utils.paginate_query( query, table, limit, [sort_key, 'id', 'created_at'], marker=marker, sort_dir=sort_dir) resultproxy = self.session.execute(query) results = resultproxy.fetchall() return _set_listobject_from_models(list_cls(), results) except oslodb_utils.InvalidSortKey as sort_key_error: raise exceptions.InvalidSortKey(sort_key_error.message) # Any ValueErrors are propagated back to the user as is. # Limits, sort_dir and sort_key are checked at the API layer. # If however central or storage is called directly, invalid values # show up as ValueError except ValueError as value_error: raise exceptions.ValueError(value_error.message)