-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql_analyzer.py
170 lines (146 loc) · 5.4 KB
/
sql_analyzer.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
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
from enum import Enum
from typing import List, Tuple, Union, Dict
from sql_parser import parse
from pyparsing import ParseResults
import logging
#####################################
######## CREATE RELATIONSHIP
######################################
def analyze_attribute(attr):
is_primary_key = 'PRIMARY KEY' in list(attr)
is_discriminator = 'DISCRIMINATOR' in list(attr)
is_multivalued = (len(attr) == 3 and attr[2] == '[]')
sub_attributes = []
if attr[1] == 'COMPOSITE':
for i in range(3, len(attr)-1):
sub_attributes.append({'attr_name': attr[i][0], 'attr_type': attr[i][1]})
attr_name = attr[0]
attr_type = attr[1]
return {
'attr_name': attr_name,
'attr_type': attr_type,
'is_primary_key': is_primary_key,
'is_discriminator': is_discriminator,
'is_multivalued': is_multivalued,
'sub_attributes': sub_attributes
}
# Function to convert ParseResults to the desired dictionary format
def convert_parse_results_relationship(parse_results):
# Extract table name
table_name = parse_results.table_name[0]
# Extract and format attributes
attributes = [analyze_attribute(attr) for attr in parse_results.attributes]
# Helper function to process entity modifiers
def process_entity(entity_name, modifier):
cardinality = True if modifier.get('cardinality', 'ONE') == 'ONE' else False
participation = True if modifier.get('participation', 'TOTAL') == 'TOTAL' else False
role_info = list(modifier.get('role', []))
if role_info:
role_name = role_info[0]
else:
role_name = None
return {'name': entity_name, 'one': cardinality, 'total': participation, 'role': role_name}
# Process entity1
entity1 = process_entity(parse_results.entity1[0], parse_results.entity1_modifier)
# Process entity2
entity2 = process_entity(parse_results.entity2[0], parse_results.entity2_modifier)
# Construct the final dictionary
result = {
'table_name': table_name,
'entity1': entity1,
'entity2': entity2,
'attributes': attributes
}
return result
#####################################
######## CREATE ENTITY
######################################
class EntityType(Enum):
REGULAR = 'REGULAR'
WEAK = 'WEAK'
SUBCLASS = 'SUBCLASS'
def convert_entity_parse_results(parse_results) -> Dict[str, Union[str, EntityType, List[Tuple[str, str, bool]]]]:
result = {}
# Determine entity type
if 'WEAK' in list(parse_results):
result['entity_type'] = EntityType.WEAK
elif 'SUBCLASS OF' in list(parse_results):
result['entity_type'] = EntityType.SUBCLASS
else:
result['entity_type'] = EntityType.REGULAR
# Extract table name
result['table_name'] = parse_results.table_name[0]
# Extract parent entity for weak and subclass entities
if result['entity_type'] in [EntityType.WEAK, EntityType.SUBCLASS]:
result['parent_entity'] = parse_results.parent_entity[0]
# Process attributes
result['attributes'] = [analyze_attribute(attr) for attr in parse_results.attributes]
return result
#####################################
######## INSERT STATEMENT
######################################
def analyze_value(value):
if isinstance(value, str):
return value
elif isinstance(value, (int, float)):
return value
elif isinstance(value, ParseResults):
if value[0] == '(' and value[-1] == ')':
return tuple(analyze_values(value[1:-1]))
elif value[0] == '[' and value[-1] == ']':
return analyze_values(value[1:-1])
assert False
def analyze_values(values):
return [analyze_value(v) for v in values]
def analyze_insert(parsed_insert):
table_name = list(parsed_insert['table_name'])[0]
values = parsed_insert['values'][1:-1] # Remove outer parentheses
analyzed_values = analyze_values(values)
return {
'table_name': table_name,
'values': analyzed_values
}
def convert_value(value):
if isinstance(value, str):
return value
elif isinstance(value, (int, float)):
return value
elif isinstance(value, ParseResults):
if value[0] == '(' and value[-1] == ')':
return tuple(convert_values(value[1:-1]))
elif value[0] == '[' and value[-1] == ']':
return list(convert_values(value[1:-1]))
return value
def convert_values(values):
return [convert_value(v) for v in values]
#####################################
######## ALTER
######################################
def analyze_alter(p):
return None
#####################################
######## SELECT
######################################
def analyze_select(p):
lp = list(p)
return {'table_name': lp[3]}
#######################
######### OVERALL
#######################
def parse_and_analyze(s):
logging.debug(f"Parsing: {s}")
p = parse(s)
logging.debug(f"Result: {p}")
lp = list(p)
if 'CREATE' in lp and 'ENTITY' in lp:
return convert_entity_parse_results(p)
elif 'CREATE RELATIONSHIP' in lp:
return convert_parse_results_relationship(p)
elif 'INSERT INTO' in lp:
return analyze_insert(p)
elif 'ALTER' in lp:
return analyze_alter(p)
elif 'SELECT' in lp:
return analyze_select(p)
else:
assert False