Skip to content

Instantly share code, notes, and snippets.

@husio
Created July 4, 2012 08:04
Show Gist options
  • Select an option

  • Save husio/3046012 to your computer and use it in GitHub Desktop.

Select an option

Save husio/3046012 to your computer and use it in GitHub Desktop.
Pretty print of any SQL
#!/usr/bin/env python
import os
import sys
import urllib
import urllib2
url = 'http://sqlformat.appspot.com/format/'
def main():
if len(sys.argv) == 1:
data = sys.stdin.read()
else:
if os.path.isfile(sys.argv[1]):
with open(sys.argv[1]) as fd:
data = fd.read()
else:
data = " ".join(sys.argv[1:])
payload = (
('data', data),
('format', 'text'),
('keyword_case', 'upper'),
('reindent', True),
('n_indents', 2),
)
response = urllib2.urlopen(url, urllib.urlencode(payload))
sql = response.read().strip()
try:
from pygments import highlight
from pygments.lexers import SqlLexer
from pygments.formatters import TerminalFormatter
sql = highlight(sql, SqlLexer(), TerminalFormatter())
except ImportError:
pass
print sql
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment