X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/b320f1c7e3fdf51b0cd15927efea52ab01d93cbc..bef5cea48e3ead13185e01958f7a3a80d185b11e:/nominatim/api/logging.py diff --git a/nominatim/api/logging.py b/nominatim/api/logging.py index e9c88470..05598660 100644 --- a/nominatim/api/logging.py +++ b/nominatim/api/logging.py @@ -60,6 +60,19 @@ class BaseLogger: """ Print the SQL for the given statement. """ + def format_sql(self, conn: AsyncConnection, statement: 'sa.Executable') -> str: + """ Return the comiled version of the statement. + """ + try: + return str(cast('sa.ClauseElement', statement) + .compile(conn.sync_engine, compile_kwargs={"literal_binds": True})) + except sa.exc.CompileError: + pass + except NotImplementedError: + pass + + return str(cast('sa.ClauseElement', statement).compile(conn.sync_engine)) + class HTMLLogger(BaseLogger): """ Logger that formats messages in HTML. @@ -92,11 +105,10 @@ class HTMLLogger(BaseLogger): def sql(self, conn: AsyncConnection, statement: 'sa.Executable') -> None: - sqlstr = str(cast('sa.ClauseElement', statement) - .compile(conn.sync_engine, compile_kwargs={"literal_binds": True})) + sqlstr = self.format_sql(conn, statement) if CODE_HIGHLIGHT: sqlstr = highlight(sqlstr, PostgresLexer(), - HtmlFormatter(nowrap=True, lineseparator='
')) + HtmlFormatter(nowrap=True, lineseparator='
')) self._write(f'
{sqlstr}
') else: self._write(f'{sqlstr}') @@ -147,9 +159,7 @@ class TextLogger(BaseLogger): def sql(self, conn: AsyncConnection, statement: 'sa.Executable') -> None: - sqlstr = str(cast('sa.ClauseElement', statement) - .compile(conn.sync_engine, compile_kwargs={"literal_binds": True})) - sqlstr = '\n| '.join(textwrap.wrap(sqlstr, width=78)) + sqlstr = '\n| '.join(textwrap.wrap(self.format_sql(conn, statement), width=78)) self._write(f"| {sqlstr}\n\n")