def __or__(self, other): if hash(self.prop) != hash(other.prop): raise excs.QueryError( '{!r} cannot be combined with {!r}'.format(self.prop, other.prop)) elif self.operator != other.operator: raise excs.QueryError( 'Operator mismatch with {!r} filter.' ' Use two separate filter calls.'.format(self.prop)) new_value = '|'.join((self.value, other.value)) return FilterNode(self.prop, self.operator, new_value)
def order(self, prop): """Order the query results by the given property. Args: prop: Property or OrderNode (i.e. a negated Property), The property to be sorted on can be given as "Model.prop" to get an ascending sort or "-Model.prop" to get a descending one. Returns: A reference to the query. Raises: ValueError: An improper argument was provided. QueryError: The provided property is not associated with the Model being queried. """ if isinstance(prop, query_nodes.OrderNode): order = prop prop = order.prop else: order = query_nodes.OrderNode(prop) logging.info('Adding query sort: %s', order) if not self._model_cls.is_valid_property(order.prop): raise excs.QueryError( 'Invalid Property argument "{}" cannot be used with {}'.format( order.prop, self._model_cls.__name__)) self._sort = order return self
def expand(self, *props): """Expand foreign key(s) in the query results.""" logging.info( 'Adding query expands: %s', ', '.join('{} -> {}'.format(prop, prop.expands_to) for prop in props)) for prop in props: if not self._model_cls.is_valid_property(prop): raise excs.QueryError('{}.{} cannot be used with {}'.format( prop.model_cls_name, prop.name, self._model_cls.__name__)) elif not prop.expandable: raise excs.QueryError( 'Property {} not marked as expandable'.format(prop)) self._expands |= set(props) return self
def limit(self, limit): """Add a result limit to the query.""" logging.info('Adding query limit: %s', limit) if limit < 0: raise excs.QueryError('Limit must be non-negative') self._limit = limit return self
def filter(self, *filters): """Add property filter(s) to the query.""" logging.info('Adding query filter(s): %s', ', '.join('"{}"'.format(filter_) for filter_ in filters)) for filter_ in filters: prop = filter_.prop if not self._model_cls.is_valid_property(prop): raise excs.QueryError('{!r} cannot be used to query {}'.format( prop, self._model_cls.__name__)) self._filters.extend(filters) return self
def __lt__(self, unused_other): raise excs.QueryError('Unsupported operation for boolean properties')