Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions datagateway_api/src/datagateway_api/icat/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ def create_filter(self):
where_filter = self.create_condition(self.field, "=", self.value)
elif self.operation in ["ne", "neq"]:
where_filter = self.create_condition(self.field, "!=", self.value)
elif self.operation == "isnull":
if self.value:
where_filter = self.create_condition(self.field, "IS NULL", None)
else:
where_filter = self.create_condition(self.field, "IS NOT NULL", None)
elif self.operation == "like":
where_filter = self.create_condition(self.field, "like", f"%{self.value}%")
elif self.operation == "ilike":
Expand Down Expand Up @@ -139,6 +144,14 @@ def create_condition(attribute_name, operator, value):
"""

conditions = {}

# Handle unary operators (IS NULL, IS NOT NULL)
normalized_op = operator.strip().upper()
if normalized_op in ("IS NULL", "IS NOT NULL"):
conditions[attribute_name] = normalized_op
log.debug("Unary condition in ICAT where filter, %s", conditions)
return conditions

# Removing quote marks when doing conditions with IN expressions or when a
# distinct filter is used in a request
jpql_value = (
Expand Down
6 changes: 6 additions & 0 deletions datagateway_api/src/swagger/initialise_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ def initialise_datagateway_api_spec(spec):
},
},
},
{
"type": "object",
"title": "IS NULL",
"properties": {"isnull": {"type": "boolean"}},
},
{
"type": "object",
"title": "Substring equality",
Expand Down Expand Up @@ -157,6 +162,7 @@ def initialise_datagateway_api_spec(spec):
"examples": {
"eq": {"value": [{"id": {"eq": 1}}]},
"ne": {"value": [{"id": {"ne": 1}}]},
"isnull": {"value": [{"publicationDate": {"isnull": True}}]},
"like": {"value": [{"name": {"like": "dog"}}]},
"lt": {"value": [{"id": {"lt": 10}}]},
"lte": {"value": [{"id": {"lte": 50}}]},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class TestICATWhereFilter:
[
pytest.param("eq", 5, ["%s = '5'"], id="equal"),
pytest.param("ne", 5, ["%s != '5'"], id="not equal (ne)"),
pytest.param("isnull", True, ["%s IS NULL"], id="isnull (true)"),
pytest.param("isnull", False, ["%s IS NOT NULL"], id="isnull (false)"),
pytest.param("neq", 5, ["%s != '5'"], id="not equal (neq)"),
pytest.param("like", 5, ["%s like '%%5%%'"], id="like"),
pytest.param("ilike", 5, ["UPPER(%s) like UPPER('%%5%%')"], id="ilike"),
Expand Down
Loading