Skip to content
Open
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
48 changes: 23 additions & 25 deletions src/DIRAC/Core/Utilities/MySQL.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ def _safeCmd(self, command):
"""Just replaces password, if visible, with *********"""
return command.replace(self.__passwd, "**********")

def _logCmd(self, cmd, args=None):
def _logCmd(self, cmd, args, cursor):
"""Return a copy of the SQL command with %s placeholders
replaced by their actual values including basic string quoting.
The formatting may not be perfect SQL in every case, but it should be
Expand All @@ -712,20 +712,11 @@ def _logCmd(self, cmd, args=None):
if args is None or not isinstance(args, (list, tuple)) or not args:
return safe

subs = []
for v in args:
if v is None:
subs.append("NULL")
elif isinstance(v, (int, float)):
subs.append(str(v))
else:
escaped = str(v).replace('"', '""')
subs.append(f'"{escaped}"')

try:
return safe % tuple(subs)
except TypeError:
mogrified = cursor.mogrify(safe, args)
except Exception:
return safe
return mogrified.decode("UTF-8")

def _connect(self):
"""
Expand Down Expand Up @@ -764,10 +755,6 @@ def _query(self, cmd, *, args=None, logArgs=None, conn=None, debug=True):
return S_ERROR upon error
"""

if logArgs is None:
logArgs = args
self.log.debug(f"_query: {self._logCmd(cmd, logArgs)}")

if conn:
connection = conn
else:
Expand All @@ -778,6 +765,12 @@ def _query(self, cmd, *, args=None, logArgs=None, conn=None, debug=True):

try:
cursor = connection.cursor()

if logArgs is None:
logArgs = args
if self.log.shown("DEBUG"):
self.log.debug(f"_query: {self._logCmd(cmd, logArgs, cursor)}")

if cursor.execute(cmd, args=args):
res = cursor.fetchall()
else:
Expand Down Expand Up @@ -818,9 +811,6 @@ def _update(self, cmd, *, args=None, logArgs=None, conn=None, debug=True):
lastRowId: if set, added to the returned dictionary
"""

if logArgs is None:
logArgs = args
self.log.debug(f"_update: {self._logCmd(cmd, logArgs)}")
if conn:
connection = conn
else:
Expand All @@ -831,6 +821,12 @@ def _update(self, cmd, *, args=None, logArgs=None, conn=None, debug=True):

try:
cursor = connection.cursor()

if logArgs is None:
logArgs = args
if self.log.shown("DEBUG"):
self.log.debug(f"_update: {self._logCmd(cmd, logArgs, cursor)}")

res = cursor.execute(cmd, args=args)

retDict = S_OK(res)
Expand Down Expand Up @@ -865,11 +861,6 @@ def _updatemany(self, cmd, data, *, logData=None, conn=None, debug=True):
S_ERROR upon error.
"""

if logData is None:
logData = data
for idx, row in enumerate(logData, 1):
self.log.debug(f"_updatemany [{idx}]: {self._logCmd(cmd, row)}")

if conn:
connection = conn
else:
Expand All @@ -880,6 +871,13 @@ def _updatemany(self, cmd, data, *, logData=None, conn=None, debug=True):

try:
cursor = connection.cursor()

if logData is None:
logData = data
if self.log.shown("DEBUG"):
for idx, row in enumerate(logData, 1):
self.log.debug(f"_updatemany [{idx}]: {self._logCmd(cmd, row, cursor)}")

res = cursor.executemany(cmd, data)
retDict = S_OK(res)
if cursor.lastrowid:
Expand Down
Loading