Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v8.0] fix (Transformation): use parameterised query in addTransformation #7894

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
6 changes: 4 additions & 2 deletions src/DIRAC/Core/Utilities/MySQL.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,11 @@ def _query(self, cmd, *, conn=None, debug=True):
return retDict

@captureOptimizerTraces
def _update(self, cmd, *, conn=None, debug=True):
def _update(self, cmd, *, args=None, conn=None, debug=True):
"""execute MySQL update command

:param args: parameters passed to cursor.execute(..., args=args) method.
:param conn: connection object.
:param debug: print or not the errors

return S_OK with number of updated registers upon success
Expand All @@ -772,7 +774,7 @@ def _update(self, cmd, *, conn=None, debug=True):

try:
cursor = connection.cursor()
res = cursor.execute(cmd)
res = cursor.execute(cmd, args=args)
retDict = S_OK(res)
if cursor.lastrowid:
retDict["lastRowId"] = cursor.lastrowid
Expand Down
65 changes: 33 additions & 32 deletions src/DIRAC/TransformationSystem/DB/TransformationDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,38 +156,39 @@ def addTransformation(
elif res["Message"] != "Transformation does not exist":
return res
self.lock.acquire()
res = self._escapeString(body)
if not res["OK"]:
return S_ERROR("Failed to parse the transformation body")
body = res["Value"]
req = (
"INSERT INTO Transformations (TransformationName,Description,LongDescription, \
CreationDate,LastUpdate,AuthorDN,AuthorGroup,Type,Plugin,AgentType,\
FileMask,Status,TransformationGroup,GroupSize,\
InheritedFrom,Body,MaxNumberOfTasks,EventsPerTask)\
VALUES ('%s','%s','%s',\
UTC_TIMESTAMP(),UTC_TIMESTAMP(),'%s','%s','%s','%s','%s',\
'%s','New','%s',%f,\
%d,%s,%d,%d);"
% (
transName,
description,
longDescription,
authorDN,
authorGroup,
transType,
plugin,
agentType,
fileMask,
transformationGroup,
groupSize,
inheritedFrom,
body,
maxTasks,
eventsPerTask,
)
)
res = self._update(req, conn=connection)

params = {
"TransformationName": transName,
"Description": description,
"LongDescription": longDescription,
"CreationDate": "UTC_TIMESTAMP()",
"LastUpdate": "UTC_TIMESTAMP()",
"AuthorDN": authorDN,
"AuthorGroup": authorGroup,
"Type": transType,
"Plugin": plugin,
"AgentType": agentType,
"FileMask": fileMask,
"Status": "New",
"TransformationGroup": transformationGroup,
"GroupSize": groupSize,
"InheritedFrom": inheritedFrom,
"Body": body,
ryuwd marked this conversation as resolved.
Show resolved Hide resolved
"MaxNumberOfTasks": maxTasks,
"EventsPerTask": eventsPerTask,
}

# A list of parameters that we do not want to substitute as parameters, but directly
# into the statement e.g. functions like "UTC_TIMESTAMP()"
unparameterised_columns = [
"CreationDate",
"LastUpdate",
]
subst = ", ".join(f"%({name})s" if name not in unparameterised_columns else params[name] for name in params)

req = f"INSERT INTO Transformations ({', '.join(params)}) VALUES ({subst});"

res = self._update(req, args=params, conn=connection)
if not res["OK"]:
self.lock.release()
return res
Expand Down
Loading