-
Notifications
You must be signed in to change notification settings - Fork 0
/
ep3-1.py
68 lines (49 loc) · 2.2 KB
/
ep3-1.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
import sys, re
import argparse
from tabela import Tabela, ListaTabela
def main():
sqlIgnore = re.compile("^[--].*")
regexNomeTabela = re.compile("CREATE TABLE ([^\s]+)")
regexDependencia = re.compile("REFERENCES ([^\s]+)")
try:
estrutura = open(args.fileName, "r")
except FileNotFoundError as err:
print(err)
#Lista de todas as tabelas referenciadas no arquivo de estrutura
listaTabela = ListaTabela()
#lista de tabelas povoadas
tabelasPovoadas = ListaTabela()
linha = ""
for line in estrutura:
if not (sqlIgnore.match(line)):
linha = linha + line
if (';' in line):
#Faz com que cada bloco do código SQL seja lido em uma única linha
linha = linha.replace('\n', ' ')
nome = regexNomeTabela.search(linha)
dependencias = regexDependencia.findall(linha)
if(nome != None):
tabela = Tabela(nome.group(1)[1:-1])
listaTabela.add_tabela(tabela)
#Adiciona todas as referenências encontradas, retirando os limitadores (aspas)
tabela.add_dependencias([d[1:-1] for d in dependencias])
#Adiciona as tabelas sem dependencias na lista de tabelas povoadas
if not(dependencias): tabelasPovoadas.add_tabela(tabela)
#Limpa a variável para a próxima iteração
linha = ""
while True:
for t in listaTabela.list:
if(len(t.dependencias) > 0 and not tabelasPovoadas.find_nome(t.nome)):
flag_add_table = True
for d in t.dependencias:
if not(tabelasPovoadas.find_nome(d)):
flag_add_table = False
if(flag_add_table):
tabelasPovoadas.add_tabela(t)
if (len(tabelasPovoadas.list) >= len(listaTabela.list)): break
print ('\n'.join([t.nome for t in tabelasPovoadas.list]))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("fileName", help="O arquivo deve estar no mesmo diretório do script")
args = parser.parse_args()
main()