Skip to content

Instantly share code, notes, and snippets.

@jvanasco
Created October 24, 2014 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvanasco/69daa58aeb0e921cdbbe to your computer and use it in GitHub Desktop.
Save jvanasco/69daa58aeb0e921cdbbe to your computer and use it in GitHub Desktop.
printing sqlalchemy queries
import sqlparse
from sqlalchemy.dialects import postgresql as dialect_postgresql
# ------------------------------------------------------------------------------
def print_query(q):
"""prints a sqlalchemy query"""
print "-" * 30
if hasattr(q, 'statement'):
print "[q.statement.compile %s]" % type(q)
statement = str(q.statement.compile(dialect=dialect_postgresql.dialect(), compile_kwargs={"literal_binds": True}))
elif hasattr(q, 'compile'):
print "[q.compile %s]" % type(q)
statement = str(q.compile(dialect=dialect_postgresql.dialect(), compile_kwargs={"literal_binds": True}))
else:
print "[q %s]" % type(q)
statement = str(q)
print sqlparse.format(statement, reindent=True, keyword_case='upper')
print "-" * 30
# ------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment