-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_tables_postgre.py
388 lines (343 loc) · 22.3 KB
/
script_tables_postgre.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
from connector_postgre import Interface_db_postgre, get_db_info
if __name__ == "__main__":
try:
# CONEXAO COM O POSTGRE
user, password, host, database = get_db_info()
db = Interface_db_postgre(user, password, host, database)
# -----------------------------------------------------------------------------------------------------
# CRIACAO DAS TABELAS BANDA_LARGA E LOG_BANDA_LARGA NO POSTGRE
# -----------------------------------------------------------------------------------------------------
table_banda_larga = "CREATE TABLE IF NOT EXISTS banda_larga(\
id_banda serial primary key,\
ano bigint,\
mes bigint,\
grupo_economico text,\
empresa text,\
cnpj bigint,\
porte_da_prestadora text,\
uf text,\
municipio text,\
codigo_ibge_municipio bigint,\
faixa_de_velocidade text,\
tecnologia text,\
meio_de_acesso text,\
tipo_de_pessoa text,\
acessos bigint)"
db.execute(table_banda_larga)
print(">> Created table banda_larga")
table_log_banda_larga = "CREATE TABLE IF NOT EXISTS log_banda_larga(\
id_log_banda serial primary key,\
usuario text,\
data_registro date,\
dados text)"
db.execute(table_log_banda_larga)
print(">> Created table log_banda_larga")
# -----------------------------------------------------------------------------------------------------
# FUNCTIONS E TRIGGERS DA TABELA BANDA_LARGA PARA LOG
# -----------------------------------------------------------------------------------------------------
# Funcao e trigger para registrar os logs da tabela banda_larga.
# Apos inserir, atualizar ou deletar, criará um log na tabela log_banda_larga,
# contendo usuario, data da execucao e quais dados foram atualizados.
function_log_banda_larga = "CREATE OR REPLACE FUNCTION func_log_banda_larga() RETURNS TRIGGER AS $$ \
BEGIN \
IF (TG_OP = 'INSERT') THEN \
INSERT INTO log_banda_larga(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Inclusão realizada. ' || NEW.* || ' .' ); \
RETURN NEW; \
ELSIF (TG_OP = 'UPDATE') THEN \
INSERT INTO log_banda_larga(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Alteração realizada. Dados antigos: ' || OLD.* || ' para novos dados ' || NEW.* || ' .' ); \
RETURN NEW; \
ELSIF (TG_OP = 'DELETE') THEN \
INSERT INTO log_banda_larga(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Deleção realizada. Dados deletados: ' || OLD.* || ' .' ); \
RETURN OLD; \
END IF; \
RETURN NULLs; \
END; \
$$ \
LANGUAGE 'plpgsql';"
db.execute(function_log_banda_larga)
print(">> Function func_log_banda_larga created")
trigger_log_banda_larga = "CREATE TRIGGER tr_log_banda_larga AFTER INSERT or UPDATE or DELETE ON banda_larga \
FOR EACH ROW EXECUTE PROCEDURE func_log_banda_larga();"
db.execute(trigger_log_banda_larga)
print(">> Trigger tr_log_banda_larga created")
# -----------------------------------------------------------------------------------------------------
# CRIACAO DAS TABELAS COBERTURA_MOVEL E LOG_COBERTURA_MOVEL NO POSTGRE
# -----------------------------------------------------------------------------------------------------
table_cobertura_movel = "CREATE TABLE IF NOT EXISTS cobertura_movel (\
id_movel serial primary key,\
ano int,\
operadora text,\
tecnologia text,\
codigo_setor_censitario bigint,\
bairro text,\
tipo_setor text,\
codigo_localidade bigint,\
nome_localidade text,\
categoria_localidade text,\
localidade_agregadora text,\
codigo_municipio int,\
municipio text,\
uf text,\
regiao text,\
area decimal(38,4),\
domicilios int,\
moradores int,\
percentual_cobertura decimal(38,4));"
db.execute(table_cobertura_movel)
print(">> Created table cobertura_movel")
table_log_cobertura_movel = "CREATE TABLE IF NOT EXISTS log_cobertura_movel(\
id_log_cobertura serial primary key,\
usuario text,\
data_registro date,\
dados text);"
db.execute(table_log_cobertura_movel)
print(">> Created table log_cobertura_movel")
# -----------------------------------------------------------------------------------------------------
# FUNCTIONS E TRIGGERS DA TABELA COBERTURA_MOVEL PARA LOG
# -----------------------------------------------------------------------------------------------------
# Funcao e trigger para registrar os logs da tabela cobertura_movel.
# Apos inserir, atualizar ou deletar, criará um log na tabela log_cobertura_movel,
# contendo usuario, data da execucao e quais dados foram atualizados.
function_log_cobertura_movel = "CREATE OR REPLACE FUNCTION func_log_cobertura_movel() RETURNS TRIGGER AS $$ \
BEGIN \
IF (TG_OP = 'INSERT') THEN \
INSERT INTO log_cobertura_movel(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Inclusão realizada. ' || NEW.* || ' .' ); \
RETURN NEW; \
ELSIF (TG_OP = 'UPDATE') THEN \
INSERT INTO log_cobertura_movel(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Alteração realizada. Operação antiga: ' || OLD.* || ' para nova operação ' || NEW.* || ' .' ); \
RETURN NEW; \
ELSIF (TG_OP = 'DELETE') THEN \
INSERT INTO log_cobertura_movel(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Deleção realizada. Operação deletada: ' || OLD.* || ' .' ); \
RETURN OLD; \
END IF; \
RETURN NULLs; \
END; \
$$ \
LANGUAGE 'plpgsql';"
db.execute(function_log_cobertura_movel)
print(">> Function func_log_cobertura_movel created")
trigger_log_cobertura_movel = "CREATE TRIGGER tr_log_cobertura_movel AFTER INSERT or UPDATE or DELETE ON cobertura_movel \
FOR EACH ROW EXECUTE PROCEDURE func_log_cobertura_movel();"
db.execute(trigger_log_cobertura_movel)
print(">> Trigger tr_log_cobertura_movel created")
# -----------------------------------------------------------------------------------------------------
# CRIACAO DAS TABELAS MUNICIPIOS_ACESSOS E LOG_MUNICIPIOS_ACESSOS NO POSTGRE
# -----------------------------------------------------------------------------------------------------
table_municipios_acessos = "CREATE TABLE IF NOT EXISTS municipios_acessos(\
id_acessos serial primary key,\
ano int,\
mes int,\
acessos int,\
servico text,\
densidade decimal(38,4),\
codigo_ibge int,\
municipio text,\
uf text,\
nome_uf text,\
regiao text,\
codigo_nacional int);"
db.execute(table_municipios_acessos)
print(">> Created table municipios_acessos")
table_log_municipios_acessos = "CREATE TABLE IF NOT EXISTS log_municipios_acessos(\
id_log_acessos serial primary key,\
usuario text,\
data_registro date,\
dados text);"
db.execute(table_log_municipios_acessos)
print(">> Created table log_municipios_acessos")
# -----------------------------------------------------------------------------------------------------
# FUNCTIONS E TRIGGERS DA TABELA MUNICIPIOS_ACESSOS PARA LOG
# -----------------------------------------------------------------------------------------------------
# Funcao e trigger para registrar os logs da tabela municipios_acessos.
# Apos inserir, atualizar ou deletar, criará um log na tabela log_municipios_acessos,
# Contendo usuario, data da execucao e quais dados foram atualizados.
function_log_municipios_acessos = "CREATE OR REPLACE FUNCTION func_log_municipios_acessos() RETURNS TRIGGER AS $$ \
BEGIN \
IF (TG_OP = 'INSERT') THEN \
INSERT INTO log_municipios_acessos(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Inclusão realizada. ' || NEW.* || ' .' ); \
RETURN NEW; \
ELSIF (TG_OP = 'UPDATE') THEN \
INSERT INTO log_municipios_acessos(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Alteração realizada. Operação antiga: ' || OLD.* || ' para nova operação ' || NEW.* || ' .' ); \
RETURN NEW; \
ELSIF (TG_OP = 'DELETE') THEN \
INSERT INTO log_municipios_acessos(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Deleção realizada. Operação deletada: ' || OLD.* || ' .' ); \
RETURN OLD; \
END IF; \
RETURN NULLs; \
END; \
$$ \
LANGUAGE 'plpgsql';"
db.execute(function_log_municipios_acessos)
print(">> Function func_log_municipios_acessos created")
trigger_log_municipios_acessos = "CREATE TRIGGER tr_log_municipios_acessos AFTER INSERT or UPDATE or DELETE ON municipios_acessos \
FOR EACH ROW EXECUTE PROCEDURE func_log_municipios_acessos();"
db.execute(trigger_log_municipios_acessos)
print(">> Trigger tr_log_municipios_acessos created")
# -----------------------------------------------------------------------------------------------------
# CRIACAO DAS TABELAS MUNICIPIOS_COBERTURA E LOG_MUNICIPIOS_COBERTURA NO POSTGRE
# -----------------------------------------------------------------------------------------------------
table_municipios_cobertura = "CREATE TABLE IF NOT EXISTS municipios_cobertura (\
id_cobertura serial primary key,\
operadora text,\
tecnologia_cobertura text,\
moradores_cobertos decimal (38,4),\
domicilios_cobertos decimal (38,4),\
area_coberta decimal (38,4),\
moradores_municipio int,\
domicilios_municipio int,\
area_municipio decimal (38,4),\
ano int,\
codigo_ibge int,\
municipio text,\
uf text,\
nome_uf text,\
regiao text,\
codigo_nacional int);"
db.execute(table_municipios_cobertura)
print(">> Created table municipios_cobertura")
table_log_municipios_cobertura = "CREATE TABLE IF NOT EXISTS log_municipios_cobertura(\
id_log_acessos serial primary key,\
usuario text,\
data_registro date,\
dados text);"
db.execute(table_log_municipios_cobertura)
print(">> Created table log_municipios_cobertura")
# -----------------------------------------------------------------------------------------------------
# FUNCTIONS E TRIGGERS DA TABELA MUNICIPIOS_COBERTURA PARA LOG
# -----------------------------------------------------------------------------------------------------
# Funcao e trigger para registrar os logs da tabela municipios_cobertura.
# Apos inserir, atualizar ou deletar, criará um log na tabela log_municipios_cobertura,
# Contendo usuario, data da execucao e quais dados foram atualizados.
function_log_municipios_cobertura = "CREATE OR REPLACE FUNCTION func_log_municipios_cobertura() RETURNS TRIGGER AS $$ \
BEGIN \
IF (TG_OP = 'INSERT') THEN \
INSERT INTO log_municipios_cobertura(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Inclusão realizada. ' || NEW.* || ' .' ); \
RETURN NEW; \
ELSIF (TG_OP = 'UPDATE') THEN \
INSERT INTO log_municipios_cobertura(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Alteração realizada. Operação antiga: ' || OLD.* || ' para nova operação ' || NEW.* || ' .' ); \
RETURN NEW; \
ELSIF (TG_OP = 'DELETE') THEN \
INSERT INTO log_municipios_cobertura(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Deleção realizada. Operação deletada: ' || OLD.* || ' .' ); \
RETURN OLD; \
END IF; \
RETURN NULLs; \
END; \
$$ \
LANGUAGE 'plpgsql';"
db.execute(function_log_municipios_cobertura)
print(">> Function func_log_municipios_cobertura created")
trigger_log_municipios_cobertura = "CREATE TRIGGER tr_log_municipios_cobertura AFTER INSERT or UPDATE or DELETE ON municipios_cobertura \
FOR EACH ROW EXECUTE PROCEDURE func_log_municipios_cobertura();"
db.execute(trigger_log_municipios_cobertura)
print(">> Trigger tr_log_municipios_cobertura created")
# -----------------------------------------------------------------------------------------------------
# CRIACAO DAS TABELAS TV_ASSINATURA E LOG_TV_ASSINATURA NO POSTGRE
# -----------------------------------------------------------------------------------------------------
table_tv_assinatura = "CREATE TABLE IF NOT EXISTS tv_assinatura(\
id_tv_assinatura serial primary key,\
ano int,\
mes int,\
grupo_economico text,\
empresa text,\
cnpj bigint,\
porte_prestadora text,\
uf text,\
municipio text,\
codigo_ibge_municipio int,\
tecnologia text,\
meio_acesso text,\
tipo_pessoa text,\
acessos int);"
db.execute(table_tv_assinatura)
print(">> Created table tv_assinatura")
table_log_tv_assinatura = "CREATE TABLE IF NOT EXISTS log_tv_assinatura(\
id_log_acessos serial primary key,\
usuario text,\
data_registro date,\
dados text);"
db.execute(table_log_tv_assinatura)
print(">> Created table log_tv_assinatura")
# -----------------------------------------------------------------------------------------------------
# FUNCTIONS E TRIGGERS DA TABELA TV_ASSINATURA PARA LOG
# -----------------------------------------------------------------------------------------------------
# Funcao e trigger para registrar os logs da tabela tv_assinatura.
# Apos inserir, atualizar ou deletar, criará um log na tabela log_tv_assinatura,
# contendo usuario, data da execucao e quais dados foram atualizados.
function_log_tv_assinatura = "CREATE OR REPLACE FUNCTION func_log_tv_assinatura() RETURNS TRIGGER AS $$ \
BEGIN \
IF (TG_OP = 'INSERT') THEN \
INSERT INTO log_tv_assinatura(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Inclusão realizada. ' || NEW.* || ' .' ); \
RETURN NEW; \
ELSIF (TG_OP = 'UPDATE') THEN \
INSERT INTO log_tv_assinatura(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Alteração realizada. Operação antiga: ' || OLD.* || ' para nova operação ' || NEW.* || ' .' ); \
RETURN NEW; \
ELSIF (TG_OP = 'DELETE') THEN \
INSERT INTO log_tv_assinatura(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Deleção realizada. Operação deletada: ' || OLD.* || ' .' ); \
RETURN OLD; \
END IF; \
RETURN NULLs; \
END; \
$$ \
LANGUAGE 'plpgsql';"
db.execute(function_log_tv_assinatura)
print(">> Function func_log_tv_assinatura created")
trigger_log_tv_assinatura = "CREATE TRIGGER tr_log_tv_assinatura AFTER INSERT or UPDATE or DELETE ON tv_assinatura \
FOR EACH ROW EXECUTE PROCEDURE func_log_tv_assinatura();"
db.execute(trigger_log_tv_assinatura)
print(">> Trigger tr_log_tv_assinatura created")
# -----------------------------------------------------------------------------------------------------
# CRIACAO DAS TABELAS VELOCIDADE_CONTRATADA E LOG_VELOCIDADE_CONTRATADA NO POSTGRE
# -----------------------------------------------------------------------------------------------------
table_velocidade_contratada = "CREATE TABLE IF NOT EXISTS velocidade_contratada (\
id_velocidade_contratada serial primary key,\
ano int,\
mes int,\
razao_social text,\
cnpj text,\
velocidade_contratada_mbps decimal(38,4),\
uf text,\
municipio text,\
codigo_ibge int,\
acessos int,\
tipo text,\
municipio_uf text);"
db.execute(table_velocidade_contratada)
print(">> Created table velocidade_contratada")
table_log_velocidade_contratada = "CREATE TABLE IF NOT EXISTS log_velocidade_contratada(\
id_log_acessos serial primary key,\
usuario text,\
data_registro date,\
dados text);"
db.execute(table_log_velocidade_contratada)
print(">> Created table log_velocidade_contratada")
# -----------------------------------------------------------------------------------------------------
# FUNCTIONS E TRIGGERS DA TABELA VELOCIDADE_CONTRATADA PARA LOG
# -----------------------------------------------------------------------------------------------------
# Funcao e trigger para registrar os logs da tabela velocidade_contratada.
# Apos inserir, atualizar ou deletar, criará um log na tabela log_velocidade_contratada,
# contendo usuario, data da execucao e quais dados foram atualizados.
function_log_velocidade_contratada = "CREATE OR REPLACE FUNCTION func_log_velocidade_contratada() RETURNS TRIGGER AS $$ \
BEGIN \
IF (TG_OP = 'INSERT') THEN \
INSERT INTO log_Velocidade_contratada(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Inclusão realizada. ' || NEW.* || ' .' ); \
RETURN NEW; \
ELSIF (TG_OP = 'UPDATE') THEN \
INSERT INTO log_Velocidade_contratada(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Alteração realizada. Operação antiga: ' || OLD.* || ' para nova operação ' || NEW.* || ' .' ); \
RETURN NEW; \
ELSIF (TG_OP = 'DELETE') THEN \
INSERT INTO log_Velocidade_contratada(usuario, data_registro, dados) VALUES (CURRENT_USER, CURRENT_TIMESTAMP, 'Deleção realizada. Operação deletada: ' || OLD.* || ' .' ); \
RETURN OLD; \
END IF; \
RETURN NULLs; \
END; \
$$ \
LANGUAGE 'plpgsql';"
db.execute(function_log_velocidade_contratada)
print(">> Function func_log_velocidade_contratada created")
trigger_log_velocidade_contratada = "CREATE TRIGGER tr_log_velocidade_contratada AFTER INSERT or UPDATE or DELETE ON velocidade_contratada \
FOR EACH ROW EXECUTE PROCEDURE func_log_velocidade_contratada();"
db.execute(trigger_log_velocidade_contratada)
print(">> Trigger tr_log_velocidade_contratada created")
print(">> Finished script")
except Exception as e:
print("Error in script tables postgre: ", str(e))