forked from FerreroJeremy/ln2sql
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDatabase.py
executable file
·152 lines (130 loc) · 5.55 KB
/
Database.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
# -*- coding: utf-8 -*
import sys, re
import unicodedata
from Table import Table
reload(sys)
sys.setdefaultencoding("utf-8")
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
ITALIC = '\033[3m'
UNDERLINE = '\033[4m'
END = '\033[0m'
class Database:
def __init__(self):
self.tables = []
def get_number_of_tables(self):
return len(self.tables)
def get_tables(self):
return self.tables
def get_table_by_name(self, table_name):
for table in self.tables:
if table.get_name() == table_name:
return table
def get_tables_into_dictionnary(self):
data = {}
for table in self.tables:
data[table.get_name()] = []
for column in table.get_columns():
data[table.get_name()].append(column.get_name())
return data
def get_primary_keys_by_table(self):
data = {}
for table in self.tables:
data[table.get_name()] = table.get_primary_keys()
return data
def get_foreign_keys_by_table(self):
data = {}
for table in self.tables:
data[table.get_name()] = table.get_foreign_keys()
return data
def get_primary_keys_of_table(self, table_name):
for table in self.tables:
if table.get_name() == table_name:
return table.get_primary_keys()
def get_primary_key_names_of_table(self, table_name):
for table in self.tables:
if table.get_name() == table_name:
return table.get_primary_key_names()
def get_foreign_keys_of_table(self, table_name):
for table in self.tables:
if table.get_name() == table_name:
return table.get_foreign_keys()
def get_foreign_key_names_of_table(self, table_name):
for table in self.tables:
if table.get_name() == table_name:
return table.get_foreign_key_names()
def add_table(self, table):
self.tables.append(table)
def load(self, path):
with open(path) as f:
content = f.read()
tables_string = [p.split(';')[0] for p in content.split('CREATE') if ';' in p]
for table_string in tables_string:
if 'TABLE' in table_string:
table = self.create_table(table_string)
self.add_table(table)
alter_tables_string = [p.split(';')[0] for p in content.split('ALTER') if ';' in p]
for alter_table_string in alter_tables_string:
if 'TABLE' in alter_table_string:
self.alter_table(alter_table_string)
def predict_type(self, string):
if 'int' in string.lower():
return 'int'
elif 'char' in string.lower() or 'text' in string.lower():
return 'string'
elif 'date' in string.lower():
return 'date'
else:
return 'unknow'
def create_table(self, table_string):
lines = table_string.split("\n")
table = Table()
for line in lines:
if 'TABLE' in line:
table_name = re.search("`(\w+)`", line)
table.set_name(table_name.group(1))
elif 'PRIMARY KEY' in line:
primary_key_columns = re.findall("`(\w+)`", line)
for primary_key_column in primary_key_columns:
table.add_primary_key(primary_key_column)
else:
column_name = re.search("`(\w+)`", line)
if column_name is not None:
column_type = self.predict_type(line)
table.add_column(column_name.group(1), column_type)
return table
def alter_table(self, alter_string):
lines = alter_string.replace('\n', ' ').split(';')
for line in lines:
if 'PRIMARY KEY' in line:
table_name = re.search("TABLE `(\w+)`", line).group(1)
table = self.get_table_by_name(table_name)
primary_key_columns = re.findall("PRIMARY KEY \(`(\w+)`\)", line)
for primary_key_column in primary_key_columns:
table.add_primary_key(primary_key_column)
elif 'FOREIGN KEY' in line:
table_name = re.search("TABLE `(\w+)`", line).group(1)
table = self.get_table_by_name(table_name)
foreign_keys_list = re.findall("FOREIGN KEY \(`(\w+)`\) REFERENCES `(\w+)` \(`(\w+)`\)", line)
for column, foreign_table, foreign_column in foreign_keys_list:
table.add_foreign_key(column, foreign_table, foreign_column)
def print_me(self):
for table in self.tables:
print('+-------------------------------------+')
print("| %25s |" % (table.get_name().upper()))
print('+-------------------------------------+')
for column in table.columns:
if column.is_primary():
print("| 🔑 %31s |" % (color.BOLD + column.get_name() + ' (' + column.get_type() + ')' + color.END))
elif column.is_foreign():
print("| #️⃣ %31s |" % (color.ITALIC + column.get_name() + ' (' + column.get_type() + ')' + color.END))
else:
print("| %23s |" % (column.get_name() + ' (' + column.get_type() + ')'))
print('+-------------------------------------+\n')