def explain(self, S):
"""
Explain a MySQL query.
The output of the EXPLAIN command is formatted as a table, and then
logged to the logger as an INFO.
Parameters
----------
S : string
The MySQL string to be explained.
"""
command = S.partition(" ")[0].upper()
if command in ["SHOW", "DESCRIBE", "SET", "RESET"]:
return
try:
explain_table = self.connection.execute("EXPLAIN %s" % S)
except pymysql.ProgrammingError as e:
raise SQLProgrammingError(S + "\n"+ "%s" % e)
else:
explain_table_rows = [[x[0] for x
in explain_table.description]]
for x in explain_table:
explain_table_rows.append([str(y) for y in x])
explain_column_width = [len(x[0]) for x
in explain_table.description]
for current_row in explain_table_rows:
for i, x in enumerate(current_row):
explain_column_width[i] = max(explain_column_width[i],
len(x))
format_string = " | ".join(["%%-%is" % x for x
in explain_column_width])
line_string = "-" * (sum(explain_column_width) - 3
+ 3 * len(explain_column_width))
log_rows = ["EXPLAIN %s" % S]
log_rows.append(line_string)
log_rows.append(format_string % tuple(explain_table_rows[0]))
log_rows.append(line_string)
for x in explain_table_rows[1:]:
log_rows.append(format_string % tuple(x))
log_rows.append(line_string)
logger.debug("\n".join(log_rows))
评论列表
文章目录