-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathreference.py
More file actions
191 lines (158 loc) · 4.2 KB
/
reference.py
File metadata and controls
191 lines (158 loc) · 4.2 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import pyparsing as pp
from .common import _, _c, c, n
from .generic import name
from pydbml.parser.blueprints import ReferenceBlueprint
pp.ParserElement.set_default_whitespace_chars(' \t\r')
relation = pp.oneOf("> - < <>")
col_name = (
(
name('schema') + '.' + name('table') + '.' - name('field')
) | (
name('table') + '.' + name('field')
)
)
ref_inline = pp.Literal("ref:") - relation('type') - col_name
def parse_inline_relation(s, loc, tok):
'''
ref: < table.column
or
ref: < schema1.table.column
'''
result = {
'type': tok['type'],
'inline': True,
'table2': tok['table'],
'col2': tok['field']
}
if 'schema' in tok:
result['schema2'] = tok['schema']
return ReferenceBlueprint(**result)
ref_inline.set_parse_action(parse_inline_relation)
on_option = (
pp.CaselessLiteral('no action')
| pp.CaselessLiteral('restrict')
| pp.CaselessLiteral('cascade')
| pp.CaselessLiteral('set null')
| pp.CaselessLiteral('set default')
)
update = pp.CaselessLiteral("update:").suppress() + _ + on_option
delete = pp.CaselessLiteral("delete:").suppress() + _ + on_option
ref_setting = _ + (update('update') | delete('delete')) + _
ref_settings = (
'['
+ ref_setting
+ (
','
+ ref_setting
)[...]
+ ']' + c
)
def parse_ref_settings(s, loc, tok):
'''
[delete: cascade]
'''
result = {}
if 'update' in tok:
result['on_update'] = tok['update'][0]
if 'delete' in tok:
result['on_delete'] = tok['delete'][0]
if 'comment' in tok:
result['comment'] = tok['comment'][0]
return result
ref_settings.set_parse_action(parse_ref_settings)
composite_name = (
'(' + pp.White()[...]
- name + pp.White()[...]
+ (
pp.White()[...] + ","
+ pp.White()[...] + name
+ pp.White()[...]
)[...]
+ ')'
)
name_or_composite = name | pp.Combine(composite_name)
ref_cols = (
(
name('schema')
+ pp.Suppress('.') + name('table')
+ pp.Suppress('.') + name_or_composite('field')
) | (
name('table')
+ pp.Suppress('.') + name_or_composite('field')
)
)
def parse_ref_cols(s, loc, tok):
'''
table1.col1
or
schema1.table1.col1
or
schema1.table1.(col1, col2)
'''
result = {
'table': tok['table'],
'field': tok['field'],
}
if 'schema' in tok:
result['schema'] = tok['schema']
return result
ref_cols.set_parse_action(parse_ref_cols)
ref_body = (
ref_cols('col1')
- relation('type')
- ref_cols('col2') + c
+ ref_settings('settings')[0, 1]
)
# ref_body = (
# table_name('table1')
# - '.'
# - name_or_composite('field1')
# - relation('type')
# - table_name('table2')
# - '.'
# - name_or_composite('field2') + c
# + ref_settings('settings')[0, 1]
# )
ref_short = _c + pp.CaselessLiteral('ref') + name('name')[0, 1] + ':' - ref_body
ref_long = _c + (
pp.CaselessLiteral('ref') + _
+ name('name')[0, 1] + _
+ '{' + _
- ref_body + _
- '}'
)
def parse_ref(s, loc, tok):
'''
ref name: table1.col1 > table2.col2
or
ref name {
table1.col1 < table2.col2
}
'''
init_dict = {
'type': tok['type'],
'inline': False,
'table1': tok['col1']['table'],
'col1': tok['col1']['field'],
'table2': tok['col2']['table'],
'col2': tok['col2']['field'],
}
if 'schema' in tok['col1']:
init_dict['schema1'] = tok['col1']['schema']
if 'schema' in tok['col2']:
init_dict['schema2'] = tok['col2']['schema']
if 'name' in tok:
init_dict['name'] = tok['name']
if 'settings' in tok:
init_dict.update(tok['settings'])
# comments after settings have priority
if 'comment' in tok:
init_dict['comment'] = tok['comment'][0]
if 'comment' not in init_dict and 'comment_before' in tok:
comment = '\n'.join(c[0] for c in tok['comment_before'])
init_dict['comment'] = comment
ref = ReferenceBlueprint(**init_dict)
return ref
ref_short.set_parse_action(parse_ref)
ref_long.set_parse_action(parse_ref)
ref = ref_short | ref_long + (n | pp.StringEnd())