From 214defa9ff7d783d518b8a9f7c1217094e38b0f0 Mon Sep 17 00:00:00 2001 From: Rolf Krahl Date: Mon, 26 Aug 2024 17:33:33 +0200 Subject: [PATCH] Update the source code creating queries to use the new format of the conditions argument. Not yet covered: tests, documentation examples. --- src/icat/client.py | 14 +++++++------- src/icat/dump_queries.py | 12 ++++++------ src/icat/dumpfile_xml.py | 6 +++--- src/icat/entities.py | 19 +++++++++---------- src/scripts/wipeicat.py | 4 ++-- 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/icat/client.py b/src/icat/client.py index 8c0cf2cf..48f84d19 100644 --- a/src/icat/client.py +++ b/src/icat/client.py @@ -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() @@ -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)) @@ -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)) diff --git a/src/icat/dump_queries.py b/src/icat/dump_queries.py index 7c8aa53b..1b6a4fa1 100644 --- a/src/icat/dump_queries.py +++ b/src/icat/dump_queries.py @@ -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"}) ] @@ -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: diff --git a/src/icat/dumpfile_xml.py b/src/icat/dumpfile_xml.py index 2373c4ea..7148d800 100644 --- a/src/icat/dumpfile_xml.py +++ b/src/icat/dumpfile_xml.py @@ -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] diff --git a/src/icat/entities.py b/src/icat/entities.py index 7761aeef..88a2506a 100644 --- a/src/icat/entities.py +++ b/src/icat/entities.py @@ -20,6 +20,7 @@ from .entity import Entity from .exception import InternalError +from .query import Query class GroupingMixin: @@ -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) @@ -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) diff --git a/src/scripts/wipeicat.py b/src/scripts/wipeicat.py index 4f6679ee..ffe8f1dc 100644 --- a/src/scripts/wipeicat.py +++ b/src/scripts/wipeicat.py @@ -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 @@ -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 = []