-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_schema.py
60 lines (52 loc) · 2.05 KB
/
parse_schema.py
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
import re
from schema import *
create_table_re = re.compile(r"CREATE TABLE `(?P<table_name>\w*)`")
end_create_table_re = re.compile(r".*;$")
column_re = re.compile(r"\s*`(?P<name>\w+)`\s(?P<type>[^\s]+)\s(?P<options>[^\,]*)\,")
primary_key_re = re.compile(r"\s*PRIMARY KEY\s+\(`(?P<name>[^`]+)`\)")
foreign_key_re =re.compile(r"\s*CONSTRAINT\s+`(?P<name>\w+)`\s+FOREIGN KEY\s+" +
r"\(`(?P<localcol>[^`]+)`\)\s+REFERENCES\s+" +
r"`(?P<table>[^`]+)`\s+" +
r"\(`(?P<remotecol>[^`]+)`\)")
def parse_schema(lines_in):
schema = Schema()
while True:
line = lines_in.readline()
if line == "": return schema
match = create_table_re.match(line)
if match is not None:
table_name = match.group('table_name')
schema.tables[table_name] = parse_create_table(table_name, lines_in)
def parse_create_table(table_name, lines_in):
table = Table(table_name)
while True:
line = lines_in.readline()
if line == "": raise EOFError()
if end_create_table_re.match(line): return table
match = column_re.match(line)
if match is not None:
col_name = match.group('name')
table.columns[col_name] = Column(
col_name,
match.group('type'),
match.group('options')
)
continue
match = primary_key_re.match(line)
if match is not None:
table.primary_key = match.group('name')
continue
match = foreign_key_re.match(line)
if match is not None:
localcol = match.group('localcol')
table.relationships[localcol] = Relationship(
match.group('name'),
localcol,
match.group('table'),
match.group('remotecol')
)
if __name__ == '__main__':
import sys
import json
from jsonschema import SchemaEncoder
print json.dumps(parse_schema(sys.stdin), cls=SchemaEncoder, indent=4)