-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.py
More file actions
65 lines (50 loc) · 2.68 KB
/
Copy pathtable.py
File metadata and controls
65 lines (50 loc) · 2.68 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
###########################################
# Kyu Hyeok Seo 2018-***** #
###########################################
class Table():
def __init__(self, name, col_name_list, col_dict, not_null_list, pk, fk_referencing_dict, fk_referenced_dict):
self.table_name = name # string
self.column_name_list = col_name_list # list of column names
self.column_dict = col_dict # dictionary : key = col_name, value = col_dtype, col_constraint
self.not_null_list = not_null_list # list for not null column names
self.primary_key = pk # list for pk elements
if self.primary_key is None : # pk can be None when there is no pk declaration while creating a table
self.primary_key = [] # so we have to consider the case
# dictionary : key = referencing col_name, value = referenced table, referenced column
# Show which tables and columns are referenced by this table
self.fk_referencing_dict = fk_referencing_dict
# dictionary : key = referencing table_name, value = referenced column
# Show which tables reference this table
self.fk_referenced_dict = fk_referenced_dict
# add new reference relationship to self.fk_referenced_dict
def add_fk_referenced_dict(self, referencing_table_name, referenced_col):
self.fk_referenced_dict[referencing_table_name] = referenced_col
# return a specific format to show table schema
def get_info(self):
# first line
temp = "-" * 65 + "\n"
# add table name and column names
temp += "table_name [" + self.table_name + "]" + "\n"
temp += "column_name" + " " * 11 + "type" + " " * 11 + "null" + " " * 11 + "key" + " "*10 + "\n"
# For each column, add its information including whether a column is included to fk, pk.
for col_name in list(self.column_dict.keys()):
col_dtype, col_const = self.column_dict[col_name]
temp += col_name + " " * (22-len(col_name)) + col_dtype + " "*(15-len(col_dtype))
# if column is not null
if col_name in self.not_null_list :
temp += 'N'
else :
temp += 'Y'
temp += " " * 14
# if column is one of pk combinations
if col_name in self.primary_key:
temp += 'PRI'
# if column is one of fks
if col_name in self.fk_referencing_dict :
temp += '/FOR'
# if column is one of fks
elif col_name in self.fk_referencing_dict :
temp += 'FOR'
temp += '\n'
temp += "-" * 65
return temp