|
| 1 | +import os |
| 2 | +import re |
| 3 | + |
| 4 | +from .constants import Color |
| 5 | +from .table import Table |
| 6 | + |
| 7 | + |
| 8 | +class Database: |
| 9 | + |
| 10 | + def __init__(self): |
| 11 | + self.tables = [] |
| 12 | + self.thesaurus_object = None |
| 13 | + |
| 14 | + def set_thesaurus(self, thesaurus): |
| 15 | + self.thesaurus_object = thesaurus |
| 16 | + |
| 17 | + def get_number_of_tables(self): |
| 18 | + return len(self.tables) |
| 19 | + |
| 20 | + def get_tables(self): |
| 21 | + return self.tables |
| 22 | + |
| 23 | + def get_column_with_this_name(self, name): |
| 24 | + for table in self.tables: |
| 25 | + for column in table.get_columns(): |
| 26 | + if column.name == name: |
| 27 | + return column |
| 28 | + |
| 29 | + def get_table_by_name(self, table_name): |
| 30 | + for table in self.tables: |
| 31 | + if table.name == table_name: |
| 32 | + return table |
| 33 | + |
| 34 | + def get_tables_into_dictionary(self): |
| 35 | + data = {} |
| 36 | + for table in self.tables: |
| 37 | + data[table.name] = [] |
| 38 | + for column in table.get_columns(): |
| 39 | + data[table.name].append(column.name) |
| 40 | + return data |
| 41 | + |
| 42 | + def get_primary_keys_by_table(self): |
| 43 | + data = {} |
| 44 | + for table in self.tables: |
| 45 | + data[table.name] = table.get_primary_keys() |
| 46 | + return data |
| 47 | + |
| 48 | + def get_foreign_keys_by_table(self): |
| 49 | + data = {} |
| 50 | + for table in self.tables: |
| 51 | + data[table.name] = table.get_foreign_keys() |
| 52 | + return data |
| 53 | + |
| 54 | + def get_primary_keys_of_table(self, table_name): |
| 55 | + for table in self.tables: |
| 56 | + if table.name == table_name: |
| 57 | + return table.get_primary_keys() |
| 58 | + |
| 59 | + def get_primary_key_names_of_table(self, table_name): |
| 60 | + for table in self.tables: |
| 61 | + if table.name == table_name: |
| 62 | + return table.get_primary_key_names() |
| 63 | + |
| 64 | + def get_foreign_keys_of_table(self, table_name): |
| 65 | + for table in self.tables: |
| 66 | + if table.name == table_name: |
| 67 | + return table.get_foreign_keys() |
| 68 | + |
| 69 | + def get_foreign_key_names_of_table(self, table_name): |
| 70 | + for table in self.tables: |
| 71 | + if table.name == table_name: |
| 72 | + return table.get_foreign_key_names() |
| 73 | + |
| 74 | + def add_table(self, table): |
| 75 | + self.tables.append(table) |
| 76 | + |
| 77 | + @staticmethod |
| 78 | + def _generate_path(path): |
| 79 | + cwd = os.path.dirname(__file__) |
| 80 | + filename = os.path.join(cwd, path) |
| 81 | + return filename |
| 82 | + |
| 83 | + def load(self, path): |
| 84 | + with open(self._generate_path(path)) as f: |
| 85 | + content = f.read() |
| 86 | + tables_string = [p.split(';')[0] for p in content.split('CREATE') if ';' in p] |
| 87 | + for table_string in tables_string: |
| 88 | + if 'TABLE' in table_string: |
| 89 | + table = self.create_table(table_string) |
| 90 | + self.add_table(table) |
| 91 | + alter_tables_string = [p.split(';')[0] for p in content.split('ALTER') if ';' in p] |
| 92 | + for alter_table_string in alter_tables_string: |
| 93 | + if 'TABLE' in alter_table_string: |
| 94 | + self.alter_table(alter_table_string) |
| 95 | + |
| 96 | + def predict_type(self, string): |
| 97 | + if 'int' in string.lower(): |
| 98 | + return 'int' |
| 99 | + elif 'char' in string.lower() or 'text' in string.lower(): |
| 100 | + return 'string' |
| 101 | + elif 'date' in string.lower(): |
| 102 | + return 'date' |
| 103 | + else: |
| 104 | + return 'unknow' |
| 105 | + |
| 106 | + def create_table(self, table_string): |
| 107 | + lines = table_string.split("\n") |
| 108 | + table = Table() |
| 109 | + for line in lines: |
| 110 | + if 'TABLE' in line: |
| 111 | + table_name = re.search("`(\w+)`", line) |
| 112 | + table.name = table_name.group(1) |
| 113 | + if self.thesaurus_object is not None: |
| 114 | + table.equivalences = self.thesaurus_object.get_synonyms_of_a_word(table.name) |
| 115 | + elif 'PRIMARY KEY' in line: |
| 116 | + primary_key_columns = re.findall("`(\w+)`", line) |
| 117 | + for primary_key_column in primary_key_columns: |
| 118 | + table.add_primary_key(primary_key_column) |
| 119 | + else: |
| 120 | + column_name = re.search("`(\w+)`", line) |
| 121 | + if column_name is not None: |
| 122 | + column_type = self.predict_type(line) |
| 123 | + if self.thesaurus_object is not None: |
| 124 | + equivalences = self.thesaurus_object.get_synonyms_of_a_word(column_name.group(1)) |
| 125 | + else: |
| 126 | + equivalences = [] |
| 127 | + table.add_column(column_name.group(1), column_type, equivalences) |
| 128 | + return table |
| 129 | + |
| 130 | + def alter_table(self, alter_string): |
| 131 | + lines = alter_string.replace('\n', ' ').split(';') |
| 132 | + for line in lines: |
| 133 | + if 'PRIMARY KEY' in line: |
| 134 | + table_name = re.search("TABLE `(\w+)`", line).group(1) |
| 135 | + table = self.get_table_by_name(table_name) |
| 136 | + primary_key_columns = re.findall("PRIMARY KEY \(`(\w+)`\)", line) |
| 137 | + for primary_key_column in primary_key_columns: |
| 138 | + table.add_primary_key(primary_key_column) |
| 139 | + elif 'FOREIGN KEY' in line: |
| 140 | + table_name = re.search("TABLE `(\w+)`", line).group(1) |
| 141 | + table = self.get_table_by_name(table_name) |
| 142 | + foreign_keys_list = re.findall("FOREIGN KEY \(`(\w+)`\) REFERENCES `(\w+)` \(`(\w+)`\)", line) |
| 143 | + for column, foreign_table, foreign_column in foreign_keys_list: |
| 144 | + table.add_foreign_key(column, foreign_table, foreign_column) |
| 145 | + |
| 146 | + def print_me(self): |
| 147 | + for table in self.tables: |
| 148 | + print('+-------------------------------------+') |
| 149 | + print("| %25s |" % (table.name.upper())) |
| 150 | + print('+-------------------------------------+') |
| 151 | + for column in table.columns: |
| 152 | + if column.is_primary(): |
| 153 | + print("| 🔑 %31s |" % (Color.BOLD + column.name + ' (' + column.get_type() + ')' + Color.END)) |
| 154 | + elif column.is_foreign(): |
| 155 | + print("| #️⃣ %31s |" % (Color.ITALIC + column.name + ' (' + column.get_type() + ')' + Color.END)) |
| 156 | + else: |
| 157 | + print("| %23s |" % (column.name + ' (' + column.get_type() + ')')) |
| 158 | + print('+-------------------------------------+\n') |
0 commit comments