-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcdep.py
444 lines (312 loc) · 12 KB
/
funcdep.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import functools
import os
import sqlite3
import copy
import utils
class DB:
"""
Cette classe représente une base de données
que l'on veut manipuler.
"""
def __init__(self, db_name: str):
self._name = db_name
self._path = os.path.abspath(self._name)
self._conn = sqlite3.connect(self._path)
@property
def has_df_table(self):
"""True si la table des DF existe dans la base de données"""
return 'FuncDep' in self.tables
@property
def name(self) -> str:
return self._name
@property
def tables(self) -> list:
c = self._conn.cursor()
c.execute('SELECT name FROM sqlite_master WHERE type="table";')
return [t[0] for t in c.fetchall()]
def get_fields(self, table: str) -> list:
# La table doit exister
if table not in self.tables:
raise UnknownTableError()
# La table n'est pas celle des DF
if table == 'FuncDep':
raise DFTableError()
c = self._conn.cursor()
c.execute('PRAGMA table_info(' + table + ')')
return [t[1] for t in c.fetchall()]
def add_df(self, table: str, lhs: str, rhs: str):
# La table doit exister
if table not in self.tables:
raise UnknownTableError()
# La table n'est pas celle des DF
if table == 'FuncDep':
raise DFTableError()
table_fields = self.get_fields(table)
# Tous les champs de la prémisse existent dans la table
for field in lhs.split():
if field not in table_fields:
raise UnknownFieldsError()
# La doit être singulière
if len(rhs.split()) > 1:
raise DFNotSingularError()
# Le champ de déffini doit exister dans la table
if rhs not in self.get_fields(table):
raise UnknownFieldsError()
# Le champ rhs ne doit pas être dans les champs lhs
if rhs in lhs.split():
raise RHSIncludeToLHSError()
c = self._conn.cursor()
# On crée la tables des DF si besoin
if not self.has_df_table:
utils.execute_sql_file(c, os.path.join('misc', 'init_df_table.sql'))
try:
c.execute('INSERT INTO `FuncDep` VALUES (?, ?, ?)', (table, lhs, rhs))
except sqlite3.IntegrityError:
raise DFAddTwiceError()
def del_df(self, table: str, lhs: str, rhs: str):
df = (table, lhs, rhs)
# La table doit exister
if table not in self.tables:
raise UnknownTableError()
# La DF doit exister
if df not in self.list_df():
raise DFNotFoundError()
c = self._conn.cursor()
c.execute('DELETE FROM `FuncDep` WHERE `table` = ? AND `lhs` = ? AND `rhs` = ?', df)
def list_df(self) -> list:
c = self._conn.cursor()
# La table doit exister
if not self.has_df_table:
return []
c.execute('SELECT * FROM `FuncDep`')
return c.fetchall()
def list_table_df(self, table: str) -> list:
# La table doit exister
if table not in self.tables:
raise UnknownTableError()
c = self._conn.cursor()
if not self.has_df_table:
return []
c.execute('SELECT * FROM `FuncDep` WHERE `table` = ?', (table,))
return c.fetchall()
def purge_df(self):
c = self._conn.cursor()
c.execute('DELETE FROM `FuncDep`')
def _check_df_set(self, dfs: list) -> dict:
c = self._conn.cursor()
res = {}
for df in dfs:
unique_lhs = c.execute('SELECT DISTINCT ' + df[1].replace(' ', ', ') + ' FROM ' + df[0] + ';')
for lhs in unique_lhs:
conditions_list = ['{}={}'.format(f, v.__repr__()) for f, v in zip(df[1].split(), lhs)]
conditions = functools.reduce(lambda a, b : a + ' AND ' + b, conditions_list)
c.execute('SELECT DISTINCT {} FROM {} WHERE {};'.format(df[2], df[0], conditions))
assos = c.fetchall()
res[df] = [t for t in c.execute('SELECT DISTINCT * FROM {} WHERE {};'.format(df[0], conditions))] \
if len(assos) > 1 else []
return res
def check_df(self) -> dict:
"""Vérifie si les DF sont respectées"""
return self._check_df_set(self.list_df())
def check_table_df(self, table: str) -> dict:
"""Vérifie si les DF sont respectées"""
# La table doit exister
if table not in self.tables:
raise UnknownTableError()
return self._check_df_set(self.list_table_df(table))
def _is_include(self, sub: list, lset: list) -> bool:
for e in sub:
if e not in lset:
return False
return True
def df_closure(self, attributes: str, dfs: list) -> list:
res = attributes.split()
res_has_changed = True
dfs = copy.deepcopy(dfs)
next_dfs = copy.deepcopy(dfs)
while res_has_changed:
res_has_changed = False
dfs = copy.deepcopy(next_dfs)
for df in dfs:
lhs = df[1].split()
if self._is_include(lhs, res):
next_dfs.remove(df)
if df[2] not in res:
res_has_changed = True
res.append(df[2])
return res
def is_df_useless(self, check_df: tuple) -> bool:
dfs = self.list_df()
if check_df in dfs:
dfs.remove(check_df)
deter = self.df_closure(check_df[1], dfs)
return check_df[2] in deter
def find_useless_df(self) -> list:
res = []
for df in self.list_df():
if self.is_df_useless(df):
res.append(df)
return res
def clean_useless_df(self):
clean = 0 == len(self.find_useless_df())
while not clean:
clean = True
useless_dfs = self.find_useless_df()
if 0 < len(useless_dfs):
clean = False
df1 = useless_dfs[0]
self.del_df(df1[0], df1[1], df1[2])
def clean_inconsistent_df(self):
dfs = self.list_df()
for df in dfs:
if df[0] not in self.tables:
self.del_df(df[0], df[1], df[2])
inconsistent = False
for att in df[1].split()+[df[2]]:
if att not in self.get_fields(df[0]):
inconsistent = True
if inconsistent:
self.del_df(df[0], df[1], df[2])
def clean(self):
self.clean_inconsistent_df()
self.clean_useless_df()
def is_key(self, table: str, attributes: str) -> bool:
all_att = self.get_fields(table)
closure = self.df_closure(attributes, self.list_table_df(table))
return self._is_include(all_att, closure)
def super_key(self, table: str) -> list:
# La table doit exister
if table not in self.tables:
raise UnknownTableError()
att = self.get_fields(table)
return [sub for sub in utils.get_all_subset(att) if self.is_key(table, utils.list2str(sub))]
def key(self, table: str) -> list:
# La table doit exister
if table not in self.tables:
raise UnknownTableError()
super_key = self.super_key(table)
res = []
for sk in super_key:
super_copy = copy.deepcopy(super_key)
super_copy.remove(sk)
is_key = True
for k in super_copy:
if self._is_include(k, sk):
is_key = False
if is_key:
res.append(sk)
return res
def is_bcnf_table(self, table: str) -> list:
# La table doit exister
if table not in self.tables:
raise UnknownTableError()
res = []
for df in self.list_table_df(table):
if not self.is_key(table, df[1]):
res.append(df)
return res
def is_bcnf(self) -> dict:
res = {}
for t in self.tables:
res[t] = self.is_bcnf_table(t)
return res
def is_3nf_table(self, table: str) -> list:
# La table doit exister
if table not in self.tables:
raise UnknownTableError()
bcnf = self.is_bcnf_table(table)
res = []
for df in bcnf:
ok = False
for sk in self.key(table):
if df[2] in sk:
ok = True
if not ok:
res.append(df)
return res
def is_3nf(self) -> dict:
res = {}
for t in self.tables:
res[t] = self.is_3nf_table(t)
return res
def find_fields(self, names: list, description: list) -> list:
res = []
for name in names:
for d in description:
if d[1] == name:
res.append(d)
break
return res
def get_content(self, att: list, table: str) -> list:
para = functools.reduce(lambda a, b: a+', '+b, att)
c = self._conn.cursor()
c.execute('SELECT DISTINCT ' + para + ' FROM ' + table + ';')
return c.fetchall()
def normalize_table(self, table: str):
new_tables = []
dfs = self.list_table_df(table)
c = self._conn.cursor()
c.execute('PRAGMA table_info(' + table + ')')
fields_description = c.fetchall()
for df in self.is_3nf_table(table):
field2rm = self.find_fields([df[2]], fields_description)[0]
new_fields = self.find_fields(df[1].split()+[df[2]], fields_description)
fields_description.remove(field2rm)
content = self.get_content(df[1].split()+[df[2]], table)
dfs.remove(df)
new_tables.append((new_fields, content, [df]))
content = self.get_content([f[1] for f in fields_description], table)
new_tables.append((fields_description, content, dfs))
return new_tables
def add_content(self, c, nt, n, table):
request = 'INSERT INTO `{}` VALUES ('.format(table+'_'+str(n))
insert = ['?' for n in range(len(nt[0]))]
request += functools.reduce(lambda a, b: a+', '+b, insert) if len(insert) > 1 else insert[0]
request += ');'
if len(nt[1]) >= 1:
c.executemany(request, nt[1])
def create_new_table(self, c, nt, n, table):
request = "CREATE TABLE IF NOT EXISTS `{}`(".format(table+'_'+str(n))
fields = []
for field in nt[0]:
freq ="`{}` {}".format(field[1], field[2])
fields.append(freq)
request += functools.reduce(lambda a, b: a+','+b, fields) if len(fields) > 1 else fields[0]
request += ');'
c.execute(request)
def add_new_df(self, c, nt, n, table):
new_df = [(table+'_'+str(n), df[1], df[2]) for df in nt[2]]
if len(new_df) > 0:
c.executemany('INSERT INTO `FuncDep` VALUES (?, ?, ?);', new_df)
def normalize(self):
conn = sqlite3.connect('normalize.sqlite')
c = conn.cursor()
utils.execute_sql_file(c, os.path.join('misc', 'init_df_table.sql'))
tables = self.tables
if 'FuncDep' in tables:
tables.remove('FuncDep')
for table in tables:
decom = self.normalize_table(table)
for n, nt in enumerate(decom):
self.create_new_table(c, nt, n, table)
self.add_content(c, nt, n, table)
self.add_new_df(c, nt, n, table)
conn.commit()
conn.close()
def close(self):
self._conn.commit()
self._conn.close()
class UnknownTableError(Exception):
pass
class UnknownFieldsError(Exception):
pass
class DFNotFoundError(Exception):
pass
class DFNotSingularError(Exception):
pass
class DFAddTwiceError(Exception):
pass
class DFTableError(Exception):
pass
class RHSIncludeToLHSError(Exception):
pass