-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat-sql.py
More file actions
40 lines (28 loc) · 782 Bytes
/
format-sql.py
File metadata and controls
40 lines (28 loc) · 782 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
38
39
40
import sys
import sqlparse
import re
from enum import Enum
# TODO: Decide what to do about $/?
# contents = contents.replace(f"${identifier}", f"__id_{identifier}")
contents = sys.stdin.read()
f_string = ""
if contents.startswith("f"):
f_string = "f"
contents = contents[1:]
# always return multiline
quote = contents[0]
quotes = 3 * quote
contents = contents.replace(quote, "")
contents = re.sub(r"{(\w+)}", r"__identifier__\1", contents)
result = sqlparse.format(
contents,
indent_columns=True,
keyword_case="upper",
identifier_case="lower",
output_format="sql",
indent_after_first=True,
wrap_after=120,
)
result = re.sub(r"__identifier__(\w+)", r"{\1}", result)
result = f"{f_string}{quotes}{result}{quotes}"
print(result.strip())