-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprettysql
More file actions
executable file
·37 lines (27 loc) · 961 Bytes
/
Copy pathprettysql
File metadata and controls
executable file
·37 lines (27 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
from __future__ import print_function
import re as _re
import sqlparse as _sqlparse
def pretty_sql(sql):
# get the indentation of the first line, so we can inject it into the sql
# statement later
indent = _re.match(r"\s*", sql).group()
# strip sql termination, to format() does not generate a second empty statement
# which produces silly newlines
(sql, term_subs) = _re.subn(r";\s*$", "", sql)
sql = indent + _sqlparse.format(
sql,
reindent = True,
keyword_case = 'upper',
indent_width = 4,
indent_tabs = False,
).replace("\n", "\n" + indent)
# remove empty lines
sql = _re.sub(r"^\s*\n", "", sql, _re.MULTILINE)
# append statement terminator on a seperate line, if one was specified
if term_subs:
sql += "\n" + indent + ";"
return sql
if __name__ == '__main__':
import sys as _sys
print(pretty_sql(_sys.stdin.read()))