summaryrefslogtreecommitdiffstats
path: root/sqlglot/__main__.py
blob: 25200c496521c056b20d54834adbaaa219ee4792 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import argparse

import sqlglot

parser = argparse.ArgumentParser(description="Transpile SQL")
parser.add_argument("sql", metavar="sql", type=str, help="SQL string to transpile")
parser.add_argument(
    "--read",
    dest="read",
    type=str,
    default=None,
    help="Dialect to read default is generic",
)
parser.add_argument(
    "--write",
    dest="write",
    type=str,
    default=None,
    help="Dialect to write default is generic",
)
parser.add_argument(
    "--no-identify",
    dest="identify",
    action="store_false",
    help="Don't auto identify fields",
)
parser.add_argument(
    "--no-pretty",
    dest="pretty",
    action="store_false",
    help="Compress sql",
)
parser.add_argument(
    "--parse",
    dest="parse",
    action="store_true",
    help="Parse and return the expression tree",
)
parser.add_argument(
    "--error-level",
    dest="error_level",
    type=str,
    default="RAISE",
    help="IGNORE, WARN, RAISE (default)",
)


args = parser.parse_args()
error_level = sqlglot.ErrorLevel[args.error_level.upper()]

if args.parse:
    sqls = [
        repr(expression)
        for expression in sqlglot.parse(
            args.sql, read=args.read, error_level=error_level
        )
    ]
else:
    sqls = sqlglot.transpile(
        args.sql,
        read=args.read,
        write=args.write,
        identify=args.identify,
        pretty=args.pretty,
        error_level=error_level,
    )

for sql in sqls:
    print(sql)