-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable_details.py
103 lines (77 loc) · 4.58 KB
/
table_details.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
import utility
import table_attributes_details
class TableDetails:
def __init__(self, db):
self.table_set = list()
self.db = db
def collect_tables(self, clauses, overall_details):
self.map_nouns_verbs_to_tables(clauses, overall_details)
self.add_relation_tables()
self.add_children_tables(overall_details)
print("CHECK NOW")
print(self.table_set)
return self.table_set
# CONSTRUCT TABLES SET
# Adds tables from table list to table set that are similar to noun from noun map
# i.e adds tables that are LIKELY to be there in final sql query
def map_nouns_verbs_to_tables(self, clauses, overall_details):
for noun in clauses.noun_map.keys():
# for every table in table list
for table_name in overall_details.tables:
# check if noun and table name is similar
return_temp_value = utility.Utility.check_substring_table(noun, table_name)
if return_temp_value[0]: # if true
if table_name not in self.table_set: # add unique table name in table set
print("Noun '" + noun + "' mapped to table '" + table_name + "'")
self.table_set.append(table_name) # add table name to table set
clauses.clause_flag["F"] = 1 # set FROM clause flag to 1
for verb in clauses.verb_list:
for table_name in overall_details.tables:
return_temp_value = utility.Utility.check_substring_table(verb, table_name, "verb")
if return_temp_value[0]: # if true
if table_name not in self.table_set: # add unique table name in table set
print("Verb '" + verb + "' mapped to table '" + table_name + "'")
self.table_set.append(table_name) # add table name to table set
clauses.clause_flag["F"] = 1 # set FROM clause flag to 1
def add_children_tables(self, overall_details):
if self.table_set:
for table in self.table_set:
reference_keys = table_attributes_details.TableAttributesDetails.test_get_referenced_tables(self.db,
table)
for fk in reference_keys:
for table_name in overall_details.tables:
return_temp_value = utility.Utility.check_substring_table(fk, table_name)
if return_temp_value[0]: # if true
if table_name not in self.table_set: # add unique table name in table set
print("Foreign key '" + fk + "' mapped to table '" + table_name + "'")
self.table_set.append(table_name) # add table name to table set
# clauses.clause_flag["F"] = 1 # set FROM clause flag to 1
def add_relation_tables(self):
break_flag = 0
print("Complete table set ", self.table_set)
related_tables_array_test = []
for table in self.table_set:
related_tables_array_test = table_attributes_details.TableAttributesDetails.test_get_referenced_tables(self.db,
table)
if len(related_tables_array_test)!=0:
print("TESTING")
print(related_tables_array_test)
for table1 in self.table_set:
for table2 in self.table_set:
if table1 == table2:
continue
related_tables_array1 = table_attributes_details.TableAttributesDetails.get_referenced_tables(self.db,
table1)
related_tables_array2 = table_attributes_details.TableAttributesDetails.get_referenced_tables(self.db,
table2)
print(related_tables_array1)
print("...")
print(related_tables_array2)
for table in related_tables_array1:
if table in related_tables_array2 and table not in self.table_set:
self.table_set.append(table)
break_flag = 1
break
if break_flag == 1:
break_flag = 0
break