Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbca829b

Browse files
committed
feat: support IN/NOT_IN/NOT_EQUAL operators
1 parentdb044b1 commitbca829b

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

‎google/cloud/datastore/query.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class Query(object):
8686
"<":query_pb2.PropertyFilter.Operator.LESS_THAN,
8787
">":query_pb2.PropertyFilter.Operator.GREATER_THAN,
8888
"=":query_pb2.PropertyFilter.Operator.EQUAL,
89+
"!=":query_pb2.PropertyFilter.Operator.NOT_EQUAL,
90+
"IN":query_pb2.PropertyFilter.Operator.IN,
91+
"NOT_IN":query_pb2.PropertyFilter.Operator.NOT_IN,
8992
}
9093
"""Mapping of operator strings and their protobuf equivalents."""
9194

@@ -215,7 +218,7 @@ def add_filter(self, property_name, operator, value):
215218
216219
where property is a property stored on the entity in the datastore
217220
and operator is one of ``OPERATORS``
218-
(ie, ``=``, ``<``, ``<=``, ``>``, ``>=``):
221+
(ie, ``=``, ``<``, ``<=``, ``>``, ``>=``, ``!=``, ``IN``, ``NOT_IN``):
219222
220223
.. testsetup:: query-filter
221224
@@ -235,7 +238,7 @@ def add_filter(self, property_name, operator, value):
235238
:param property_name: A property name.
236239
237240
:type operator: str
238-
:param operator: One of ``=``, ``<``, ``<=``, ``>``, ``>=``.
241+
:param operator: One of ``=``, ``<``, ``<=``, ``>``, ``>=``, ``!=``, ``IN``, ``NOT_IN``.
239242
240243
:type value: :class:`int`, :class:`str`, :class:`bool`,
241244
:class:`float`, :class:`NoneType`,
@@ -252,7 +255,7 @@ def add_filter(self, property_name, operator, value):
252255
"""
253256
ifself.OPERATORS.get(operator)isNone:
254257
error_message='Invalid expression: "%s"'% (operator,)
255-
choices_message="Please use one of: =, <, <=, >, >=."
258+
choices_message="Please use one of: =, <, <=, >, >=, !=, IN, NOT_IN."
256259
raiseValueError(error_message,choices_message)
257260

258261
ifproperty_name=="__key__"andnotisinstance(value,Key):
@@ -293,7 +296,7 @@ def key_filter(self, key, operator="="):
293296
:param key: The key to filter on.
294297
295298
:type operator: str
296-
:param operator: (Optional) One of ``=``, ``<``, ``<=``, ``>``, ``>=``.
299+
:param operator: (Optional) One of ``=``, ``<``, ``<=``, ``>``, ``>=``, ``!=``, ``IN``, ``NOT_IN``.
297300
Defaults to ``=``.
298301
"""
299302
self.add_filter("__key__",operator,key)

‎tests/unit/test_query.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,18 @@ def test_query_add_filter_w_all_operators():
175175
query.add_filter("lt_prop","<","val3")
176176
query.add_filter("gt_prop",">","val4")
177177
query.add_filter("eq_prop","=","val5")
178-
assertlen(query.filters)==5
178+
query.add_filter("in_prop","IN", ["val6"])
179+
query.add_filter("neq_prop","!=","val9")
180+
query.add_filter("not_in_prop","NOT_IN", ["val13"])
181+
assertlen(query.filters)==8
179182
assertquery.filters[0]== ("leq_prop","<=","val1")
180183
assertquery.filters[1]== ("geq_prop",">=","val2")
181184
assertquery.filters[2]== ("lt_prop","<","val3")
182185
assertquery.filters[3]== ("gt_prop",">","val4")
183186
assertquery.filters[4]== ("eq_prop","=","val5")
187+
assertquery.filters[5]== ("in_prop","IN", ["val6"])
188+
assertquery.filters[6]== ("neq_prop","!=","val9")
189+
assertquery.filters[7]== ("not_in_prop","NOT_IN", ["val13"])
184190

185191

186192
deftest_query_add_filter_w_known_operator_and_entity():

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp