def render_sql(db, q, inline=False):
"""
Render the sql used by a query (only works for Postgres)
:param q (query): an SQLAlchemy query object
:param inline (bool): inline parameters?
:return: str
"""
compiled_statement = q.statement.compile(dialect=postgresql.dialect())
pretty_statement = sql_format(str(compiled_statement), reindent=True)
if inline:
with db.session.connection().connection.connection.cursor() as cur:
return cur.mogrify(pretty_statement, compiled_statement.params).decode('utf-8')
else:
return pretty_statement + ("\nparameters: {}".format(str(compiled_statement.params)) if compiled_statement.params else '')
评论列表
文章目录