Skip to content

Commit

Permalink
Update the source code creating queries to use the new format of the
Browse files Browse the repository at this point in the history
conditions argument.  Not yet covered: tests, documentation examples.
  • Loading branch information
RKrahl committed Aug 26, 2024
1 parent c6c0c52 commit 214defa
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
14 changes: 7 additions & 7 deletions src/icat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,9 @@ def searchChunked(self, query, skip=0, count=None, chunksize=100):
# Mark all datasets as complete
# This will *not* work as expected!
query = Query(client, "Dataset", conditions={
"complete": "= False"
}, includes="1", order=["id"])
query = Query(client, "Dataset", conditions=[
("complete", "= False")
], includes="1", order=["id"])
for ds in client.searchChunked(query):
ds.complete = True
ds.update()
Expand Down Expand Up @@ -643,11 +643,11 @@ def searchUniqueKey(self, key, objindex=None):
attr = f.name
if f.relType == "ATTRIBUTE":
cond = "= '%s'" % simpleqp_unquote(av[attr])
query.addConditions({attr:cond})
query.addConditions([(attr, cond)])
elif f.relType == "ONE":
rk = str("%s_%s" % (f.type, av[attr]))
ro = self.searchUniqueKey(rk, objindex)
query.addConditions({"%s.id" % attr:"= %d" % ro.id})
query.addConditions([("%s.id" % attr, "= %d" % ro.id)])
else:
raise ValueError("malformed '%s': invalid attribute '%s'"
% (key, attr))
Expand Down Expand Up @@ -689,11 +689,11 @@ def searchMatching(self, obj, includes=None):
if v is None:
raise ValueError("%s is not set" % a)
if a in obj.InstAttr:
query.addConditions({a: "= '%s'" % v})
query.addConditions([(a, "= '%s'" % v)])
elif a in obj.InstRel:
if v.id is None:
raise ValueError("%s.id is not set" % a)
query.addConditions({"%s.id" % a: "= %d" % v.id})
query.addConditions([("%s.id" % a, "= %d" % v.id)])
else:
raise InternalError("Invalid constraint '%s' in %s."
% (a, obj.BeanName))
Expand Down
12 changes: 6 additions & 6 deletions src/icat/dump_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,16 @@ def getInvestigationQueries(client, invid):

return [
Query(client, "Investigation",
conditions={"id": "= %d" % invid}, includes=inv_includes),
conditions=[("id", "= %d" % invid)], includes=inv_includes),
Query(client, "Sample", order=["name"],
conditions={"investigation.id": "= %d" % invid},
conditions=[("investigation.id", "= %d" % invid)],
includes={"investigation", "type.facility",
"parameters", "parameters.type.facility"}),
Query(client, "Dataset", order=["name"],
conditions={"investigation.id": "= %d" % invid},
conditions=[("investigation.id", "= %d" % invid)],
includes=ds_includes),
Query(client, "Datafile", order=["dataset.name", "name"],
conditions={"dataset.investigation.id": "= %d" % invid},
conditions=[("dataset.investigation.id", "= %d" % invid)],
includes={"dataset", "datafileFormat.facility",
"parameters.type.facility"})
]
Expand Down Expand Up @@ -199,11 +199,11 @@ def getDataPublicationQueries(client, pubid):
# ICAT >= 5.0.0
return [
Query(client, "DataPublication", order=True,
conditions={"id": "= %d" % pubid},
conditions=[("id", "= %d" % pubid)],
includes={"facility", "content", "type.facility", "dates",
"fundingReferences.funding", "relatedItems"}),
Query(client, "DataPublicationUser", order=True,
conditions={"publication.id": "= %d" % pubid},
conditions=[("publication.id", "= %d" % pubid)],
includes={"publication", "user", "affiliations"}),
]
else:
Expand Down
6 changes: 3 additions & 3 deletions src/icat/dumpfile_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ def _searchByReference(self, element, objtype, objindex):
else:
# object is referenced by attributes.
attrs = set(element.keys()) - {'id'}
conditions = dict()
conditions = []
for attr in attrs:
if attr.endswith(".ref"):
ref = element.get(attr)
robj = self.client.searchUniqueKey(ref, objindex)
attr = "%s.id" % attr[:-4]
conditions[attr] = "= %d" % robj.id
conditions.append( (attr, "= %d" % robj.id) )
else:
conditions[attr] = "= '%s'" % element.get(attr)
conditions.append( (attr, "= '%s'" % element.get(attr)) )
query = Query(self.client, objtype, conditions=conditions)
return self.client.assertedSearch(query)[0]

Expand Down
19 changes: 9 additions & 10 deletions src/icat/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from .entity import Entity
from .exception import InternalError
from .query import Query


class GroupingMixin:
Expand All @@ -44,12 +45,11 @@ def getUsers(self, attribute=None):
corresponding attribute for all users in the group, otherwise
return the users.
"""
query = Query(client, "User", conditions=[
("userGroups.grouping.id", "= %d" % self.id)
])
if attribute is not None:
query = ("User.%s <-> UserGroup <-> %s [id=%d]"
% (attribute, self.BeanName, self.id))
else:
query = ("User <-> UserGroup <-> %s [id=%d]"
% (self.BeanName, self.id))
query.setAttributes(attribute)
return self.client.search(query)


Expand All @@ -72,12 +72,11 @@ def getInstrumentScientists(self, attribute=None):
given, return the corresponding attribute for all users
related to the instrument, otherwise return the users.
"""
query = Query(client, "User", conditions=[
("instrumentScientists.instrument.id", "= %d" % self.id)
])
if attribute is not None:
query = ("User.%s <-> InstrumentScientist <-> Instrument [id=%d]"
% (attribute, self.id))
else:
query = ("User <-> InstrumentScientist <-> Instrument [id=%d]"
% (self.id))
query.setAttributes(attribute)
return self.client.search(query)


Expand Down
4 changes: 2 additions & 2 deletions src/scripts/wipeicat.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def deleteobjs(query):
# Delete all datafiles having location not set directly from ICAT
# first, because they would cause trouble when we try to delete the
# remaining datafiles from IDS, see Issue icatproject/ids.server#63.
deleteobjs(Query(client, "Datafile", conditions={"location": "IS NULL"}))
deleteobjs(Query(client, "Datafile", conditions=[("location", "IS NULL")]))

# To delete datafiles from IDS, we must restore the datasets first,
# because IDS can only delete datafiles that are online. But
Expand All @@ -79,7 +79,7 @@ def deleteobjs(query):
# without considering IDS.
if client.ids:
dfquery = Query(client, "Datafile",
conditions={"location": "IS NOT NULL"}, limit=(0, 1))
conditions=[("location", "IS NOT NULL")], limit=(0, 1))
retriedDatasets = set()
while True:
deleteDatasets = []
Expand Down

0 comments on commit 214defa

Please sign in to comment.