Commit c6302b245380ecf2ea78c229451ef1d10c292e7b
1 parent
9023b4c4
Exists in
master
--no commit message
Showing
1 changed file
with
44 additions
and
15 deletions
Show diff stats
admin/php/metaestat_uploadcsv_submit.php
@@ -67,7 +67,6 @@ if (ob_get_level() == 0) ob_start(); | @@ -67,7 +67,6 @@ if (ob_get_level() == 0) ob_start(); | ||
67 | } | 67 | } |
68 | echo "<p class='paragrafo' >Separador de colunas identificado: <b>".$separador."</b></p>"; | 68 | echo "<p class='paragrafo' >Separador de colunas identificado: <b>".$separador."</b></p>"; |
69 | echo "<p class='paragrafo' >Total de colunas: <b>".count($colunas)."</b></p>"; | 69 | echo "<p class='paragrafo' >Total de colunas: <b>".count($colunas)."</b></p>"; |
70 | - //var_dump($colunas); | ||
71 | fclose ($handle); | 70 | fclose ($handle); |
72 | //le o csv em um array | 71 | //le o csv em um array |
73 | $handle = fopen ($arqcsv, "r"); | 72 | $handle = fopen ($arqcsv, "r"); |
@@ -77,7 +76,7 @@ if (ob_get_level() == 0) ob_start(); | @@ -77,7 +76,7 @@ if (ob_get_level() == 0) ob_start(); | ||
77 | while (!feof($handle)) { | 76 | while (!feof($handle)) { |
78 | $buffer = fgets($handle); | 77 | $buffer = fgets($handle); |
79 | if($buffer != $cabecalho){ | 78 | if($buffer != $cabecalho){ |
80 | - $buffer = str_replace('"','',$buffer); | 79 | + //$buffer = str_replace('"','',$buffer); |
81 | $buffer = str_replace("'",'',$buffer); | 80 | $buffer = str_replace("'",'',$buffer); |
82 | $buffer = str_replace("\n",'',$buffer); | 81 | $buffer = str_replace("\n",'',$buffer); |
83 | $buffer = str_replace("\r",'',$buffer); | 82 | $buffer = str_replace("\r",'',$buffer); |
@@ -96,7 +95,7 @@ if (ob_get_level() == 0) ob_start(); | @@ -96,7 +95,7 @@ if (ob_get_level() == 0) ob_start(); | ||
96 | } | 95 | } |
97 | fclose ($handle); | 96 | fclose ($handle); |
98 | //decobre o tipo de coluna | 97 | //decobre o tipo de coluna |
99 | - $testar = 500; | 98 | + $testar = 50; |
100 | if(count($linhas) < $testar){ | 99 | if(count($linhas) < $testar){ |
101 | $testar = count($linhas); | 100 | $testar = count($linhas); |
102 | } | 101 | } |
@@ -198,25 +197,36 @@ if (ob_get_level() == 0) ob_start(); | @@ -198,25 +197,36 @@ if (ob_get_level() == 0) ob_start(); | ||
198 | $escapar = "',<,>,%,#,@,(,)"; | 197 | $escapar = "',<,>,%,#,@,(,)"; |
199 | for ($i=0; $i<$nlinhas;$i++){ | 198 | for ($i=0; $i<$nlinhas;$i++){ |
200 | $s = $linhas[$i]; | 199 | $s = $linhas[$i]; |
200 | + $s = str_replace('"','',$s); | ||
201 | $enc = mb_detect_encoding($s); | 201 | $enc = mb_detect_encoding($s); |
202 | if($enc != ""){ | 202 | if($enc != ""){ |
203 | $s = mb_convert_encoding($s,$encodingdb,$enc); | 203 | $s = mb_convert_encoding($s,$encodingdb,$enc); |
204 | } | 204 | } |
205 | $vs = array(); | 205 | $vs = array(); |
206 | for ($j=0; $j<$ncolunas;$j++){ | 206 | for ($j=0; $j<$ncolunas;$j++){ |
207 | - if($tipoColuna[$colunas[$j]] == "varchar"){ | 207 | + $escape = ""; |
208 | + if($tipoColuna[$coluna] == "varchar"){ | ||
208 | $texto = $s[$j]; | 209 | $texto = $s[$j]; |
209 | - $texto = str_replace("'","",$texto); | ||
210 | - $texto = "'".addcslashes($texto,$escapar)."'"; | ||
211 | - if($texto == "''"){ | ||
212 | - $texto = 'null'; | 210 | + $enc = mb_detect_encoding($texto); |
211 | + $textosl = addcslashes($texto,$escapar); | ||
212 | + if($textosl != $texto){ | ||
213 | + $escape = "E"; | ||
213 | } | 214 | } |
214 | - $vs[] = $texto; | 215 | + if($enc != "" && $enc != $encodingdb){ |
216 | + $textosl = "$escape'".mb_convert_encoding($textosl,$encodingdb,$enc)."'"; | ||
217 | + } | ||
218 | + else{ | ||
219 | + $textosl = "$escape'".$textosl."'"; | ||
220 | + } | ||
221 | + if($textosl == "''"){ | ||
222 | + $textosl = 'null'; | ||
223 | + } | ||
224 | + $vs[] = $textosl; | ||
215 | } | 225 | } |
216 | else{ | 226 | else{ |
217 | $valor = $s[$j]; | 227 | $valor = $s[$j]; |
218 | - if($valor == ""){ | ||
219 | - $valor = "nulo"; | 228 | + if($valor == "" || (empty($valor) && $valor != 0)){ |
229 | + $valor = 'nulo'; | ||
220 | } | 230 | } |
221 | $vs[] = $valor; | 231 | $vs[] = $valor; |
222 | } | 232 | } |
@@ -253,20 +263,39 @@ if (ob_get_level() == 0) ob_start(); | @@ -253,20 +263,39 @@ if (ob_get_level() == 0) ob_start(); | ||
253 | echo 'Erro: ' . $e->getMessage(); | 263 | echo 'Erro: ' . $e->getMessage(); |
254 | } | 264 | } |
255 | } | 265 | } |
266 | + $bdcon = pg_connect('dbname='.$conexao["bancodedados"].' user='.$conexao["usuario"].' password='.$conexao["senha"].' host='.$conexao["host"].' port='.$conexao["porta"]."options='-c client_encoding=LATIN1'"); | ||
267 | + | ||
256 | foreach($linhasql as $linha){ | 268 | foreach($linhasql as $linha){ |
257 | try { | 269 | try { |
258 | $res = $dbh->query($linha); | 270 | $res = $dbh->query($linha); |
259 | if($res == false){ | 271 | if($res == false){ |
260 | - echo "<br><br><span style=color:red >Erro em: </span>".$linha; | 272 | + $res = pg_query($bdcon,$linha); |
273 | + if($res == false){ | ||
274 | + $linha = remove_accents($linha); | ||
275 | + $res = $dbh->query($linha); | ||
276 | + if($res == false){ | ||
277 | + $res = pg_query($bdcon,$linha); | ||
278 | + if($res == false){ | ||
279 | + echo "<br><br><span style=color:red >Erro em: </span>".$linha; | ||
280 | + } | ||
281 | + } | ||
282 | + else{ | ||
283 | + echo "<br><br><span style=color:red >Linha com acentos removidos: </span>".$linha; | ||
284 | + } | ||
285 | + } | ||
261 | } | 286 | } |
262 | } catch (PDOException $e) { | 287 | } catch (PDOException $e) { |
263 | echo 'Erro: ' . $e->getMessage(); | 288 | echo 'Erro: ' . $e->getMessage(); |
264 | } | 289 | } |
265 | } | 290 | } |
266 | - echo "<br>Registros existentes no CSV: ". $nlinhas; | ||
267 | $sql = "select * from ".$_POST["i3GEOuploadcsvesquema"].".".$_POST["tabelaDestinocsv"]; | 291 | $sql = "select * from ".$_POST["i3GEOuploadcsvesquema"].".".$_POST["tabelaDestinocsv"]; |
268 | $q = $dbh->query($sql,PDO::FETCH_ASSOC); | 292 | $q = $dbh->query($sql,PDO::FETCH_ASSOC); |
269 | $r = $q->fetchAll(); | 293 | $r = $q->fetchAll(); |
294 | + if($nlinhas != count($r)){ | ||
295 | + echo "<span style='color:red'>"; | ||
296 | + } | ||
297 | + echo "<br>Registros existentes no CSV: ". $nlinhas; | ||
298 | + | ||
270 | echo "<br>Registros na tabela final: ". count($r); | 299 | echo "<br>Registros na tabela final: ". count($r); |
271 | echo "<b><br>Feito!!!<br>Faça o reload da página"; | 300 | echo "<b><br>Feito!!!<br>Faça o reload da página"; |
272 | } | 301 | } |
@@ -274,8 +303,8 @@ if (ob_get_level() == 0) ob_start(); | @@ -274,8 +303,8 @@ if (ob_get_level() == 0) ob_start(); | ||
274 | echo "<p class='paragrafo' >Erro ao enviar o arquivo. Talvez o tamanho do arquivo seja maior do que o permitido.</p>"; | 303 | echo "<p class='paragrafo' >Erro ao enviar o arquivo. Talvez o tamanho do arquivo seja maior do que o permitido.</p>"; |
275 | } | 304 | } |
276 | 305 | ||
277 | - ?> | ||
278 | - <script>window.scrollTo(0,10000);</script> | 306 | + ?> |
307 | + <script>window.scrollTo(0,10000);</script> | ||
279 | 308 | ||
280 | </body> | 309 | </body> |
281 | </html> | 310 | </html> |