Skip to content

Commit fd984d7

Browse files
committed
feat: Enhance _getTransformationID to support multiple transformation IDs and names
1 parent 6a08dbf commit fd984d7

1 file changed

Lines changed: 43 additions & 15 deletions

File tree

src/DIRAC/TransformationSystem/DB/TransformationDB.py

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -406,21 +406,49 @@ def __updateTransformationParameter(self, transID, paramName, paramValue, connec
406406

407407
def _getTransformationID(self, transName, connection=False):
408408
"""Method returns ID of transformation with the name=<name>"""
409-
try:
410-
transName = int(transName)
411-
cmd = f"SELECT TransformationID from Transformations WHERE TransformationID={transName};"
412-
except ValueError:
413-
if not isinstance(transName, str):
414-
return S_ERROR("Transformation should be ID or name")
415-
cmd = f"SELECT TransformationID from Transformations WHERE TransformationName='{transName}';"
416-
res = self._query(cmd, conn=connection)
417-
if not res["OK"]:
418-
gLogger.error("Failed to obtain transformation ID for transformation", f"{transName}: {res['Message']}")
419-
return res
420-
elif not res["Value"]:
421-
gLogger.verbose(f"Transformation {transName} does not exist")
422-
return S_ERROR("Transformation does not exist")
423-
return S_OK(res["Value"][0][0])
409+
tids = []
410+
tnames = []
411+
for name in [transName] if isinstance(transName, (int, str)) else transName:
412+
if isinstance(name, int):
413+
tids.append(name)
414+
elif name.isdigit():
415+
tids.append(int(name))
416+
else:
417+
tnames.append(name)
418+
419+
result = []
420+
if tids:
421+
cmd = "SELECT TransformationID from Transformations"
422+
cmd += f" WHERE TransformationID IN ({','.join(map(connection._escapeString, tids))})"
423+
res = self._query(cmd, conn=connection)
424+
if not res["OK"]:
425+
gLogger.error(
426+
"Failed to obtain transformation IDs for transformations", f"{transName}: {res['Message']}"
427+
)
428+
return res
429+
if len(res["Value"]) != len(tids):
430+
missing = set(tids) - {row[0] for row in res["Value"]}
431+
gLogger.verbose(f"Transformations {missing} do not exist")
432+
return S_ERROR(f"Transformations {missing} do not exist")
433+
result.extend(row[0] for row in res["Value"])
434+
if tnames:
435+
cmd = "SELECT TransformationID, TransformationName from Transformations"
436+
cmd += f" WHERE TransformationName IN ({','.join(map(connection._escapeString, tnames))})"
437+
res = self._query(cmd, conn=connection)
438+
if not res["OK"]:
439+
gLogger.error(
440+
"Failed to obtain transformation IDs for transformations", f"{transName}: {res['Message']}"
441+
)
442+
return res
443+
if len(res["Value"]) != len(tnames):
444+
missing = set(tnames) - {row[1] for row in res["Value"]}
445+
gLogger.verbose(f"Transformations {missing} do not exist")
446+
return S_ERROR(f"Transformations {missing} do not exist")
447+
result.extend(row[0] for row in res["Value"])
448+
449+
if isinstance(transName, (int, str)):
450+
return S_OK(result[0])
451+
return S_OK(result)
424452

425453
def __deleteTransformation(self, transID, connection=False):
426454
return self._update(f"DELETE FROM Transformations WHERE TransformationID={transID};", conn=connection)

0 commit comments

Comments
 (0)