Commit df46f118915755da442336aacd938e27d28ad870
1 parent
5db27aea
Exists in
master
arquivo de instalacao do banco de dados estava faltando
Showing
1 changed file
with
91 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,91 @@ | @@ -0,0 +1,91 @@ | ||
1 | +<?php | ||
2 | +ini_set ('memory_limit', '-1'); | ||
3 | +ini_set ('max_execution_time', 300); | ||
4 | + | ||
5 | +function inserirBD($sql, $valores, $cx) | ||
6 | +{ | ||
7 | + $valores = substr($valores, 0, -1); | ||
8 | + | ||
9 | + // Executando comando | ||
10 | + return $cx->query($sql.$valores); | ||
11 | +} | ||
12 | + | ||
13 | +function instalarBD($arquivos, $cx) | ||
14 | +{ | ||
15 | + $erros = ''; | ||
16 | + | ||
17 | + // Percorrendo arquivos | ||
18 | + foreach ($arquivos as $arquivo) | ||
19 | + { | ||
20 | + // Verificando a existencia do arquivo | ||
21 | + if(!is_file($arquivo)) | ||
22 | + { | ||
23 | + $erros .= "<p>O arquivo {$arquivo} não existe.</p>"; | ||
24 | + continue; | ||
25 | + } | ||
26 | + | ||
27 | + // Convertendo o arquivo | ||
28 | + $json = json_decode( file_get_contents( $arquivo ), true ); | ||
29 | + if(!$json) | ||
30 | + { | ||
31 | + $erros .= "<p>O arquivo {$arquivo} está corrompido.</p>"; | ||
32 | + continue; | ||
33 | + } | ||
34 | + | ||
35 | + if( | ||
36 | + !isset($json['tabela']) || | ||
37 | + !isset($json['colunas']) || | ||
38 | + !isset($json['valores']) | ||
39 | + ){ | ||
40 | + $erros .= "<p>O arquivo {$arquivo} não está no formato adequado.</p>"; | ||
41 | + continue; | ||
42 | + } | ||
43 | + | ||
44 | + // Montando SQL | ||
45 | + $colunas = implode('`, `', $json['colunas']); | ||
46 | + $sql = "INSERT INTO `{$json['tabela']}` (`{$colunas}`) VALUES "; | ||
47 | + | ||
48 | + // Iniciando transição | ||
49 | + $cx->autocommit(false); | ||
50 | + | ||
51 | + // Percorrendo valores | ||
52 | + $i = 1; | ||
53 | + $valores = ''; | ||
54 | + foreach ($json['valores'] as $valor) | ||
55 | + { | ||
56 | + $valores.= '("'. implode('","', $valor) .'"),'; | ||
57 | + | ||
58 | + | ||
59 | + if($i < 1000) { | ||
60 | + $i++; | ||
61 | + continue; | ||
62 | + } | ||
63 | + | ||
64 | + if(!inserirBD($sql, $valores, $cx)) | ||
65 | + { | ||
66 | + $valores = ''; | ||
67 | + $erros .= "<p>Ocorreu a seguinte falha no arquivo {$arquivo}: {$cx->error}</p>"; | ||
68 | + break; | ||
69 | + } | ||
70 | + | ||
71 | + $valores = ''; | ||
72 | + $i = 1; | ||
73 | + } | ||
74 | + | ||
75 | + // Verificando o restante | ||
76 | + if($valores && !inserirBD($sql, $valores, $cx)) | ||
77 | + { | ||
78 | + $erros.= "<p>Ocorreu a seguinte falha no arquivo {$arquivo}: {$cx->error}</p>"; | ||
79 | + break; | ||
80 | + } | ||
81 | + } | ||
82 | + | ||
83 | + if($erros) { | ||
84 | + $cx->rollback(); | ||
85 | + return $erros; | ||
86 | + } else { | ||
87 | + $cx->commit(); | ||
88 | + return '<p>Dados inseridos com sucesso</p>'; | ||
89 | + } | ||
90 | +} | ||
91 | +?> | ||
0 | \ No newline at end of file | 92 | \ No newline at end of file |