Skip to content

Commit e17e977

Browse files
committed
New files added
1 parent 7134f91 commit e17e977

36 files changed

+401912
-0
lines changed

docs/database_loading.png

63.7 KB
Loading

docs/fr2sql_recital_2015_paper.pdf

186 KB
Binary file not shown.

docs/graphical_user_interface.png

154 KB
Loading

docs/mvc_class_diagram.png

60.4 KB
Loading

docs/staruml_class_diagram.mdj

Lines changed: 44212 additions & 0 deletions
Large diffs are not rendered by default.

ln2sql/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .ln2sql import Ln2sql

ln2sql/__main__.py

Whitespace-only changes.

ln2sql/column.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Column:
2+
def __init__(self, name='', type=None, equivalences=None):
3+
self._name = name
4+
5+
if not type:
6+
type = []
7+
self._type = type
8+
9+
if not equivalences:
10+
equivalences = []
11+
self._equivalences = equivalences
12+
13+
self.primary = False
14+
self.foreign = False
15+
16+
@property
17+
def name(self):
18+
return self._name
19+
20+
@property
21+
def type(self):
22+
return self._type
23+
24+
def add_type(self, type):
25+
self.type.append(type)
26+
27+
@property
28+
def equivalences(self):
29+
return self._equivalences
30+
31+
def add_equivalence(self, equivalence):
32+
self.equivalences.append(equivalence)
33+
34+
def is_equivalent(self, word):
35+
if word in self.equivalences:
36+
return True
37+
else:
38+
return False
39+
40+
def is_primary(self):
41+
return self.primary
42+
43+
def set_as_primary(self):
44+
self.primary = True
45+
46+
def is_foreign(self):
47+
return self.foreign
48+
49+
def set_as_foreign(self, references):
50+
self.foreign = references

ln2sql/constants.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Color:
2+
PURPLE = '\033[95m'
3+
CYAN = '\033[96m'
4+
DARKCYAN = '\033[36m'
5+
BLUE = '\033[94m'
6+
GREEN = '\033[92m'
7+
YELLOW = '\033[93m'
8+
RED = '\033[91m'
9+
BOLD = '\033[1m'
10+
UNDERLINE = '\033[4m'
11+
END = '\033[0m'
12+
13+
def without_color():
14+
Color.PURPLE = ''
15+
Color.CYAN = ''
16+
Color.DARKCYAN = ''
17+
Color.BLUE = ''
18+
Color.GREEN = ''
19+
Color.YELLOW = ''
20+
Color.RED = ''
21+
Color.BOLD = ''
22+
Color.UNDERLINE = ''
23+
Color.END = ''

ln2sql/database.py

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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

Comments
 (0)