def _comparator(self, doc1, doc2) -> Any: _orders = self._orders # Add implicit sorting by name, using the last specified direction. if len(_orders) == 0: lastDirection = BaseQuery.ASCENDING else: if _orders[-1].direction == 1: lastDirection = BaseQuery.ASCENDING else: lastDirection = BaseQuery.DESCENDING orderBys = list(_orders) order_pb = query.StructuredQuery.Order( field=query.StructuredQuery.FieldReference(field_path="id"), direction=_enum_from_direction(lastDirection), ) orderBys.append(order_pb) for orderBy in orderBys: if orderBy.field.field_path == "id": # If ordering by docuent id, compare resource paths. comp = Order()._compare_to(doc1.reference._path, doc2.reference._path) else: if ( orderBy.field.field_path not in doc1._data or orderBy.field.field_path not in doc2._data ): raise ValueError( "Can only compare fields that exist in the " "DocumentSnapshot. Please include the fields you are " "ordering on in your select() call." ) v1 = doc1._data[orderBy.field.field_path] v2 = doc2._data[orderBy.field.field_path] encoded_v1 = _helpers.encode_value(v1) encoded_v2 = _helpers.encode_value(v2) comp = Order().compare(encoded_v1, encoded_v2) if comp != 0: # 1 == Ascending, -1 == Descending return orderBy.direction * comp return 0
def _comparator(self, doc1, doc2): _orders = self._orders # Add implicit sorting by name, using the last specified direction. if len(_orders) == 0: lastDirection = Query.ASCENDING else: if _orders[-1].direction == 1: lastDirection = Query.ASCENDING else: lastDirection = Query.DESCENDING orderBys = list(_orders) order_pb = query_pb2.StructuredQuery.Order( field=query_pb2.StructuredQuery.FieldReference(field_path="id"), direction=_enum_from_direction(lastDirection), ) orderBys.append(order_pb) for orderBy in orderBys: if orderBy.field.field_path == "id": # If ordering by docuent id, compare resource paths. comp = Order()._compare_to(doc1.reference._path, doc2.reference._path) else: if ( orderBy.field.field_path not in doc1._data or orderBy.field.field_path not in doc2._data ): raise ValueError( "Can only compare fields that exist in the " "DocumentSnapshot. Please include the fields you are " "ordering on in your select() call." ) v1 = doc1._data[orderBy.field.field_path] v2 = doc2._data[orderBy.field.field_path] encoded_v1 = _helpers.encode_value(v1) encoded_v2 = _helpers.encode_value(v2) comp = Order().compare(encoded_v1, encoded_v2) if comp != 0: # 1 == Ascending, -1 == Descending return orderBy.direction * comp return 0
def _make_field_filter_pb(field_path, op_string, value): from google.cloud.firestore_v1.proto import query_pb2 from google.cloud.firestore_v1 import _helpers from google.cloud.firestore_v1.query import _enum_from_op_string return query_pb2.StructuredQuery.FieldFilter( field=query_pb2.StructuredQuery.FieldReference(field_path=field_path), op=_enum_from_op_string(op_string), value=_helpers.encode_value(value), )
def _cursor_pb(cursor_pair: Tuple[list, bool]) -> Optional[Cursor]: """Convert a cursor pair to a protobuf. If ``cursor_pair`` is :data:`None`, just returns :data:`None`. Args: cursor_pair (Optional[Tuple[list, bool]]): Two-tuple of * a list of field values. * a ``before`` flag Returns: Optional[google.cloud.firestore_v1.types.Cursor]: A protobuf cursor corresponding to the values. """ if cursor_pair is not None: data, before = cursor_pair value_pbs = [_helpers.encode_value(value) for value in data] return query.Cursor(values=value_pbs, before=before)
def _cursor_pb(cursor_pair): """Convert a cursor pair to a protobuf. If ``cursor_pair`` is :data:`None`, just returns :data:`None`. Args: cursor_pair (Optional[Tuple[list, bool]]): Two-tuple of * a list of field values. * a ``before`` flag Returns: Optional[google.cloud.firestore_v1.types.Cursor]: A protobuf cursor corresponding to the values. """ if cursor_pair is not None: data, before = cursor_pair value_pbs = [_helpers.encode_value(value) for value in data] return query_pb2.Cursor(values=value_pbs, before=before)
def where(self, field_path, op_string, value): """Filter the query on a field. See :meth:`~google.cloud.firestore_v1.client.Client.field_path` for more information on **field paths**. Returns a new :class:`~google.cloud.firestore_v1.query.Query` that filters on a specific field path, according to an operation (e.g. ``==`` or "equals") and a particular value to be paired with that operation. Args: field_path (str): A field path (``.``-delimited list of field names) for the field to filter on. op_string (str): A comparison operation in the form of a string. Acceptable values are ``<``, ``<=``, ``==``, ``>=`` and ``>``. value (Any): The value to compare the field against in the filter. If ``value`` is :data:`None` or a NaN, then ``==`` is the only allowed operation. Returns: :class:`~google.cloud.firestore_v1.query.Query`: A filtered query. Acts as a copy of the current query, modified with the newly added filter. Raises: ValueError: If ``field_path`` is invalid. ValueError: If ``value`` is a NaN or :data:`None` and ``op_string`` is not ``==``. """ field_path_module.split_field_path(field_path) # raises if value is None: if op_string != _EQ_OP: raise ValueError(_BAD_OP_NAN_NULL) filter_pb = query_pb2.StructuredQuery.UnaryFilter( field=query_pb2.StructuredQuery.FieldReference(field_path=field_path), op=enums.StructuredQuery.UnaryFilter.Operator.IS_NULL, ) elif _isnan(value): if op_string != _EQ_OP: raise ValueError(_BAD_OP_NAN_NULL) filter_pb = query_pb2.StructuredQuery.UnaryFilter( field=query_pb2.StructuredQuery.FieldReference(field_path=field_path), op=enums.StructuredQuery.UnaryFilter.Operator.IS_NAN, ) elif isinstance(value, (transforms.Sentinel, transforms._ValueList)): raise ValueError(_INVALID_WHERE_TRANSFORM) else: filter_pb = query_pb2.StructuredQuery.FieldFilter( field=query_pb2.StructuredQuery.FieldReference(field_path=field_path), op=_enum_from_op_string(op_string), value=_helpers.encode_value(value), ) new_filters = self._field_filters + (filter_pb,) return self.__class__( self._parent, projection=self._projection, field_filters=new_filters, orders=self._orders, limit=self._limit, offset=self._offset, start_at=self._start_at, end_at=self._end_at, all_descendants=self._all_descendants, )
def _boolean_value(b): return encode_value(b)
def _int_value(l): return encode_value(l)
def _object_value(keysAndValues): return encode_value(keysAndValues)
def _geoPoint_value(latitude, longitude): return encode_value(GeoPoint(latitude, longitude))
def _blob_value(b): return encode_value(b)
def _string_value(s): if not isinstance(s, six.text_type): s = six.u(s) return encode_value(s)
def _boolean_value(b): return encode_value(b)
def _double_value(d): from google.cloud.firestore_v1._helpers import encode_value return encode_value(d)
def _string_value(s): return encode_value(s)
def _object_value(keysAndValues): return encode_value(keysAndValues)
def _array_value(values=[]): return encode_value(values)
def _geoPoint_value(latitude, longitude): return encode_value(GeoPoint(latitude, longitude))
def nullValue(): return encode_value(None)
def _double_value(d): return encode_value(d)
def _int_value(value): return encode_value(value)
def _blob_value(b): return encode_value(b)
def _int_value(value): from google.cloud.firestore_v1._helpers import encode_value return encode_value(value)
def _string_value(s): if not isinstance(s, six.text_type): s = six.u(s) return encode_value(s)
def _double_value(d): return encode_value(d)
def _string_value(s): from google.cloud.firestore_v1._helpers import encode_value return encode_value(s)
def _string_value(s): if not isinstance(s, str): s = str(s) return encode_value(s)
def _blob_value(b): from google.cloud.firestore_v1._helpers import encode_value return encode_value(b)
def nullValue(): return encode_value(None)
def nullValue(): from google.cloud.firestore_v1._helpers import encode_value return encode_value(None)
def _array_value(values=[]): return encode_value(values)
def _geoPoint_value(latitude, longitude): from google.cloud.firestore_v1._helpers import encode_value from google.cloud.firestore_v1._helpers import GeoPoint return encode_value(GeoPoint(latitude, longitude))
def where(self, field_path: str, op_string: str, value) -> "BaseQuery": """Filter the query on a field. See :meth:`~google.cloud.firestore_v1.client.Client.field_path` for more information on **field paths**. Returns a new :class:`~google.cloud.firestore_v1.query.Query` that filters on a specific field path, according to an operation (e.g. ``==`` or "equals") and a particular value to be paired with that operation. Args: field_path (str): A field path (``.``-delimited list of field names) for the field to filter on. op_string (str): A comparison operation in the form of a string. Acceptable values are ``<``, ``<=``, ``==``, ``>=``, ``>``, ``in``, ``array_contains`` and ``array_contains_any``. value (Any): The value to compare the field against in the filter. If ``value`` is :data:`None` or a NaN, then ``==`` is the only allowed operation. Returns: :class:`~google.cloud.firestore_v1.query.Query`: A filtered query. Acts as a copy of the current query, modified with the newly added filter. Raises: ValueError: If ``field_path`` is invalid. ValueError: If ``value`` is a NaN or :data:`None` and ``op_string`` is not ``==``. """ field_path_module.split_field_path(field_path) # raises if value is None: if op_string != _EQ_OP: raise ValueError(_BAD_OP_NAN_NULL) filter_pb = query.StructuredQuery.UnaryFilter( field=query.StructuredQuery.FieldReference(field_path=field_path), op=StructuredQuery.UnaryFilter.Operator.IS_NULL, ) elif _isnan(value): if op_string != _EQ_OP: raise ValueError(_BAD_OP_NAN_NULL) filter_pb = query.StructuredQuery.UnaryFilter( field=query.StructuredQuery.FieldReference(field_path=field_path), op=StructuredQuery.UnaryFilter.Operator.IS_NAN, ) elif isinstance(value, (transforms.Sentinel, transforms._ValueList)): raise ValueError(_INVALID_WHERE_TRANSFORM) else: filter_pb = query.StructuredQuery.FieldFilter( field=query.StructuredQuery.FieldReference(field_path=field_path), op=_enum_from_op_string(op_string), value=_helpers.encode_value(value), ) new_filters = self._field_filters + (filter_pb,) return self.__class__( self._parent, projection=self._projection, field_filters=new_filters, orders=self._orders, limit=self._limit, offset=self._offset, limit_to_last=self._limit_to_last, start_at=self._start_at, end_at=self._end_at, all_descendants=self._all_descendants, )
def _array_value(values=[]): from google.cloud.firestore_v1._helpers import encode_value return encode_value(values)
def _object_value(keysAndValues): from google.cloud.firestore_v1._helpers import encode_value return encode_value(keysAndValues)
def _int_value(l): return encode_value(l)