Created
October 24, 2014 23:30
-
-
Save jvanasco/69daa58aeb0e921cdbbe to your computer and use it in GitHub Desktop.
printing sqlalchemy queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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