-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathsqlglot.py
More file actions
63 lines (49 loc) · 1.58 KB
/
sqlglot.py
File metadata and controls
63 lines (49 loc) · 1.58 KB
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
import json
import sqlglot
from sqlglot import exp
from sqlglot.dialects.dialect import Dialect, DialectType
dialects = json.dumps(
[
{
"dialect_title": "SQLGlot" if dialect == "" else dialect_class.__name__,
"dialect_name": dialect,
}
for dialect, dialect_class in Dialect.classes.items()
]
)
def parse_to_json(sql: str, read: DialectType = None) -> str:
return json.dumps(
[
exp.dump() if exp else {}
for exp in sqlglot.parse(sql, read=read, error_level=sqlglot.ErrorLevel.IGNORE)
]
)
def get_dialect(name: str = "") -> str:
dialect = Dialect.classes.get(name, Dialect)
keywords = dialect.tokenizer_class.KEYWORDS
type_mapping = dialect.generator_class.TYPE_MAPPING
return json.dumps(
{
"keywords": " ".join(keywords),
"types": " ".join(type_mapping.get(t, t.value) for t in exp.DataType.Type),
}
)
def format(sql: str = "", read: DialectType = None) -> str:
return "\n".join(
sqlglot.transpile(sql, read=read, error_level=sqlglot.errors.ErrorLevel.IGNORE, pretty=True)
)
def validate(sql: str = "", read: DialectType = None) -> str:
try:
sqlglot.transpile(
sql, read=read, pretty=False, unsupported_level=sqlglot.errors.ErrorLevel.IMMEDIATE
)
except sqlglot.errors.ParseError:
return json.dumps(False)
return json.dumps(True)
{
"dialects": dialects,
"get_dialect": get_dialect,
"parse_to_json": parse_to_json,
"validate": validate,
"format": format,
}