From 90151874b62e92c2b0721cfb672d7a8349cea6c4 Mon Sep 17 00:00:00 2001 From: Edmar Moretti Date: Thu, 6 Sep 2012 00:25:17 +0000 Subject: [PATCH] Inclusão de gerenciador de tabelas no sistema de metadados estatísticos --- admin/html/estat_editor.html | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ admin/index.html | 4 ++++ admin/js/core.js | 3 ++- admin/js/estat_editor.js | 228 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ admin/php/classe_metaestat.php | 70 ++++++++++++++++++++++++++++++++++++++++++---------------------------- admin/php/metaestat.php | 27 +++++++++++++++++++++++++++ 6 files changed, 402 insertions(+), 29 deletions(-) create mode 100644 admin/html/estat_editor.html create mode 100644 admin/js/estat_editor.js diff --git a/admin/html/estat_editor.html b/admin/html/estat_editor.html new file mode 100644 index 0000000..690fe72 --- /dev/null +++ b/admin/html/estat_editor.html @@ -0,0 +1,99 @@ + + + + +Conexao com banco de dados + + + + + + + +
 
+
+
+

+ Metadados estatísticos - editor de tabelas + de dados +

+ + +
+
+ + \ No newline at end of file diff --git a/admin/index.html b/admin/index.html index a076f5c..bfd9413 100644 --- a/admin/index.html +++ b/admin/index.html @@ -183,6 +183,10 @@
Cadastro de fontes de informação + +
+ Editor de tabelas de dados estatícos + diff --git a/admin/js/core.js b/admin/js/core.js index 2e15d3e..b0e6f58 100644 --- a/admin/js/core.js +++ b/admin/js/core.js @@ -72,11 +72,12 @@ function cabecalhoMetaestat(id,excluir){ botoes = [ {id:"principal",titulo:"Início",link:"../index.html"}, {id:"arvore",titulo:"Árvore completa",link:"estat_variavel.html"}, - {id:"unidade_medida",titulo:"Unidades de medida",link:"estat_unidade_medida.html"}, + {id:"unidade_medida",titulo:"Unidades",link:"estat_unidade_medida.html"}, {id:"periodo_tempo",titulo:"Períodos",link:"estat_tipo_periodo.html"}, {id:"conexao",titulo:"Conexões",link:"estat_conexao.html"}, {id:"tipo_regiao",titulo:"Regiões",link:"estat_tipo_regiao.html"}, {id:"fonteinfo",titulo:"Fonte",link:"estat_fonteinfo.html"}, + {id:"editor",titulo:"Editor",link:"estat_editor.html"}, {id:"login",titulo:"Login",js:"i3GEO.login.dialogo.abreLogin()"} ]; n = botoes.length; diff --git a/admin/js/estat_editor.js b/admin/js/estat_editor.js new file mode 100644 index 0000000..ffb2ba0 --- /dev/null +++ b/admin/js/estat_editor.js @@ -0,0 +1,228 @@ +if(typeof(i3GEOadmin) === 'undefined'){ + var i3GEOadmin = {}; +} +i3GEOadmin.editor = { + /* + * Inicializa o sistema + */ + inicia: function(){ + YAHOO.namespace("editor"); + YAHOO.namespace("admin.container"); + //opcao de escolha da conexao + i3GEOadmin.editor.conexao.lista(); + }, + /* + * algumas operacoes sao permitidas apenas para o esquema i3geo_metaestat do banco de dados + */ + verificaEsquema: function(nomeEsquema){ + if(nomeEsquema != "i3geo_metaestat"){ + alert("Operacao permitida apenas para o esquema i3geo_metaestat"); + return; + } + }, + conexao:{ + onde: "i3GEOadminEditorConexao", + lista: function(){ + var callback = { + success:function(o){ + try { + var dados = YAHOO.lang.JSON.parse(o.responseText), + temp = "Escolha a conexão com o banco: "; + temp += ""; + $i(i3GEOadmin.editor.conexao.onde).innerHTML = temp; + core_carregando("desativa"); + } + catch(e){core_handleFailure(e,o.responseText);} + }, + failure:core_handleFailure, + argument: { foo:"foo", bar:"bar" } + }; + core_carregando("buscando dados..."); + core_makeRequest("../php/metaestat.php?funcao=listaConexao",callback); + } + }, + esquema: { + onde: "i3GEOadminEditorEsquema", + lista: function(){ + var callback = { + success:function(o){ + try { + var dados = YAHOO.lang.JSON.parse(o.responseText), + temp = "
Escolha um esquema: "; + temp += ""; + $i(i3GEOadmin.editor.esquema.onde).innerHTML = temp+"
"; + core_carregando("desativa"); + $i(i3GEOadmin.editor.tabela.onde).innerHTML = ""; + } + catch(e){core_handleFailure(e,o.responseText);} + }, + failure:core_handleFailure, + argument: { foo:"foo", bar:"bar" } + }, + codigo_estat_conexao = $i("i3GEOadmincodigo_estat_conexao").value; + if(codigo_estat_conexao == ""){ + alert("Escolha uma conexao"); + $i(i3GEOadmin.editor.esquema.onde).innerHTML = ""; + $i(i3GEOadmin.editor.tabela.onde).innerHTML = ""; + return; + } + core_carregando("buscando dados..."); + core_makeRequest("../php/metaestat.php?funcao=esquemasConexao&formato=json&codigo_estat_conexao="+codigo_estat_conexao,callback); + }, + }, + tabela:{ + onde: "i3GEOadminEditorTabela", + lista: function(){ + var callback = { + success:function(o){ + try { + var dados = YAHOO.lang.JSON.parse(o.responseText), + temp = "
Escolha uma tabela: "; + temp += ""; + temp += "" + + "

Digite um nome: " + + "" + + "" + + ""; + + $i(i3GEOadmin.editor.tabela.onde).innerHTML = temp+"

"; + core_carregando("desativa"); + $i(i3GEOadmin.editor.coluna.onde).innerHTML = ""; + } + catch(e){core_handleFailure(e,o.responseText);} + }, + failure:core_handleFailure, + argument: { foo:"foo", bar:"bar" } + }, + codigo_estat_conexao = $i("i3GEOadmincodigo_estat_conexao").value, + nome_esquema = $i("i3GEOadminesquema").value; + if(nome_esquema == ""){ + alert("Escolha um esquema"); + $i(i3GEOadmin.editor.tabela.onde).innerHTML = ""; + $i(i3GEOadmin.editor.coluna.onde).innerHTML = ""; + return; + } + core_carregando("adicionando..."); + core_makeRequest("../php/metaestat.php?funcao=tabelasEsquema&formato=json&nome_esquema="+nome_esquema+"&codigo_estat_conexao="+codigo_estat_conexao,callback); + }, + criar: function(){ + if(!window.confirm("Cria nova tabela?")){ + return; + } + var callback = { + success:function(o){ + try { + core_carregando("desativa"); + i3GEOadmin.editor.tabela.lista(); + } + catch(e){core_handleFailure(e,o.responseText);} + }, + failure:core_handleFailure, + argument: { foo:"foo", bar:"bar" } + }, + novaTabela = $i("i3GEOadmintabelaNomeNovo").value, + nomeEsquema = $i("i3GEOadminesquema").value; + if(i3GEOadmin.editor.verificaEsquema(nomeEsquema) == false){ + return; + } + if(novaTabela == ""){ + alert("Digite um nome"); + return; + } + if(i3GEOadmin.editor.tabela.verificaExiste(novaTabela) == false){ + core_carregando("adicionando..."); + core_makeRequest("../php/metaestat.php?funcao=criaTabelaDB&formato=json&nome_tabela="+novaTabela+"&nome_esquema="+nomeEsquema+"&codigo_estat_conexao="+$i("i3GEOadmincodigo_estat_conexao").value,callback); + } + else{ + alert("Tabela ja existe"); + } + }, + alterarNome: function(){ + if(!window.confirm("Altera o nome?")){ + return; + } + var callback = { + success:function(o){ + try { + core_carregando("desativa"); + i3GEOadmin.editor.tabela.lista(); + } + catch(e){core_handleFailure(e,o.responseText);} + }, + failure:core_handleFailure, + argument: { foo:"foo", bar:"bar" } + }, + novaTabela = $i("i3GEOadmintabelaNomeNovo").value, + nomeEsquema = $i("i3GEOadminesquema").value; + if(i3GEOadmin.editor.verificaEsquema(nomeEsquema) == false){ + return; + } + if(novaTabela == ""){ + alert("Digite um nome"); + return; + } + if(i3GEOadmin.editor.tabela.verificaExiste(novaTabela) == false){ + core_carregando("adicionando..."); + core_makeRequest("../php/metaestat.php?funcao=alteraNomeTabelaDB&formato=json&nome_tabela="+$i("i3GEOadmintabela").value+"&novonome_tabela="+novaTabela+"&nome_esquema="+nomeEsquema+"&codigo_estat_conexao="+$i("i3GEOadmincodigo_estat_conexao").value,callback); + } + else{ + alert("Tabela ja existe"); + } + }, + copiar: function(){ + if(!window.confirm("Copiar tabela?")){ + return; + } + var callback = { + success:function(o){ + try { + core_carregando("desativa"); + i3GEOadmin.editor.tabela.lista(); + } + catch(e){core_handleFailure(e,o.responseText);} + }, + failure:core_handleFailure, + argument: { foo:"foo", bar:"bar" } + }, + novaTabela = $i("i3GEOadmintabelaNomeNovo").value, + nomeEsquema = $i("i3GEOadminesquema").value; + if(i3GEOadmin.editor.verificaEsquema(nomeEsquema) == false){ + return; + } + if(novaTabela == ""){ + alert("Digite um nome"); + return; + } + if(i3GEOadmin.editor.tabela.verificaExiste(novaTabela) == false){ + core_carregando("adicionando..."); + core_makeRequest("../php/metaestat.php?funcao=copiaTabelaDB&formato=json&nome_tabela="+$i("i3GEOadmintabela").value+"&novonome_tabela="+novaTabela+"&nome_esquema="+nomeEsquema+"&codigo_estat_conexao="+$i("i3GEOadmincodigo_estat_conexao").value,callback); + } + else{ + alert("Tabela ja existe"); + } + }, + verificaExiste: function(valor){ + var combo = $i("i3GEOadmintabela"), + n = combo.options.length, + i; + for(i=0;idbh->query($sql,PDO::FETCH_ASSOC); } @@ -155,6 +155,9 @@ class Metaestat{ } if($q){ $r = $q->fetchAll(); + if($convTexto == false){ + return $r; + } if($r){ if($id != ""){ if(count($r) == 1){ @@ -177,6 +180,20 @@ class Metaestat{ return false; } } + /* + Function: execSQLDB + + Executa um SQL no banco de dados definido em uma conexao + */ + function execSQLDB($codigo_estat_conexao,$sql){ + $c = $this->listaConexao($codigo_estat_conexao,true); + $dbhold = $this->dbh; + $dbh = new PDO('pgsql:dbname='.$c["bancodedados"].';user='.$c["usuario"].';password='.$c["senha"].';host='.$c["host"].';port='.$c["porta"]); + $this->dbh = $dbh; + $res = $this->execSQL($sql,"",false); + $this->dbh = $dbhold; + return $res; + } function insertId($tabela,$colunatemp,$colunaid){ $idtemp = (rand (9000,10000)) * -1; //echo "INSERT INTO ".$this->esquemaadmin.$tabela." ($colunatemp) VALUES ('$idtemp')";exit; @@ -1041,44 +1058,41 @@ class Metaestat{ return $this->execSQL($sql,$id_agregaregiao); } function esquemasConexao($codigo_estat_conexao){ - $c = $this->listaConexao($codigo_estat_conexao,true); - $dbhold = $this->dbh; - $dbh = new PDO('pgsql:dbname='.$c["bancodedados"].';user='.$c["usuario"].';password='.$c["senha"].';host='.$c["host"].';port='.$c["porta"]); - $this->dbh = $dbh; - $res = $this->execSQL("SELECT oid,nspname as esquema FROM pg_namespace group by nspname,oid order by nspname"); - $this->dbh = $dbhold; - return $res; + return $this->execSQLDB($codigo_estat_conexao,"SELECT oid,nspname as esquema FROM pg_namespace group by nspname,oid order by nspname"); + } + function criaEsquemaDB($codigo_estat_conexao,$nome_esquema){ + return $this->execSQLDB($codigo_estat_conexao,"create schema $nome_esquema"); } function tabelasEsquema($codigo_estat_conexao,$nome_esquema){ - $c = $this->listaConexao($codigo_estat_conexao,true); - $dbhold = $this->dbh; - $dbh = new PDO('pgsql:dbname='.$c["bancodedados"].';user='.$c["usuario"].';password='.$c["senha"].';host='.$c["host"].';port='.$c["porta"]); - $this->dbh = $dbh; - $res = $this->execSQL("SELECT table_name as tabela FROM information_schema.tables where table_schema = '$nome_esquema'"); - $this->dbh = $dbhold; + return $this->execSQLDB($codigo_estat_conexao,"SELECT table_name as tabela FROM information_schema.tables where table_schema = '$nome_esquema'"); + } + function criaTabelaDB($codigo_estat_conexao,$nome_esquema,$nome_tabela){ + return $this->execSQLDB($codigo_estat_conexao,"create table ".$nome_esquema.".".$nome_tabela." (gid serial, CONSTRAINT ".$nome_tabela."_pkey PRIMARY KEY (gid ))"); + } + function alteraNomeTabelaDB($codigo_estat_conexao,$nome_esquema,$nome_tabela,$novonome_tabela){ + $res = $this->execSQLDB($codigo_estat_conexao,"ALTER TABLE ".$nome_esquema.".".$nome_tabela." RENAME TO ".$novonome_tabela ); + $tabela = $this->execSQLDB($codigo_estat_conexao,"SELECT table_name FROM information_schema.tables where table_name = '$novonome_tabela' and table_schema = '$nome_esquema'"); + if(count($tabela) > 0){ + $sql = "UPDATE i3geoestat_medida_variavel SET tabela = '$novonome_tabela' WHERE esquemadb = '$nome_esquema' and tabela = '$nome_tabela'"; + $this->execSQL($sql,"",false); + $sql = "UPDATE i3geoestat_tipo_regiao SET tabela = '$novonome_tabela' WHERE esquemadb = '$nome_esquema' and tabela = '$nome_tabela'"; + $this->execSQL($sql,"",false); + } return $res; } + function copiaTabelaDB($codigo_estat_conexao,$nome_esquema,$nome_tabela,$novonome_tabela){ + return $this->execSQLDB($codigo_estat_conexao,"CREATE TABLE ".$nome_esquema.".".$novonome_tabela." AS select * from ".$nome_esquema.".".$nome_tabela ); + } function colunasTabela($codigo_estat_conexao,$nome_esquema,$nome_tabela){ $colunas = array(); - $c = $this->listaConexao($codigo_estat_conexao,true); - $dbhold = $this->dbh; - $dbh = new PDO('pgsql:dbname='.$c["bancodedados"].';user='.$c["usuario"].';password='.$c["senha"].';host='.$c["host"].';port='.$c["porta"]); - $this->dbh = $dbh; - $res = $this->execSQL("SELECT column_name as coluna FROM information_schema.columns where table_schema = '$nome_esquema' and table_name = '$nome_tabela'"); - $this->dbh = $dbhold; + $res = $this->execSQLDB($codigo_estat_conexao,"SELECT column_name as coluna FROM information_schema.columns where table_schema = '$nome_esquema' and table_name = '$nome_tabela'"); foreach($res as $c){ $colunas[] = $c["coluna"]; } return $colunas; } function descreveColunasTabela($codigo_estat_conexao,$nome_esquema,$nome_tabela){ - $c = $this->listaConexao($codigo_estat_conexao,true); - $dbhold = $this->dbh; - $dbh = new PDO('pgsql:dbname='.$c["bancodedados"].';user='.$c["usuario"].';password='.$c["senha"].';host='.$c["host"].';port='.$c["porta"]); - $this->dbh = $dbh; - $res = $this->execSQL("SELECT a.attnum,a.attname AS field,t.typname AS type,a.attlen AS length,a.atttypmod AS lengthvar,a.attnotnull AS notnull,p.nspname as esquema FROM pg_class c,pg_attribute a,pg_type t,pg_namespace p WHERE c.relname = '$nome_tabela' and p.nspname = '$nome_esquema' and a.attnum > 0 and a.attrelid = c.oid and a.atttypid = t.oid and c.relnamespace = p.oid ORDER BY a.attname"); - $this->dbh = $dbhold; - return $res; + return $this->execSQLDB($codigo_estat_conexao,"SELECT a.attnum,a.attname AS field,t.typname AS type,a.attlen AS length,a.atttypmod AS lengthvar,a.attnotnull AS notnull,p.nspname as esquema FROM pg_class c,pg_attribute a,pg_type t,pg_namespace p WHERE c.relname = '$nome_tabela' and p.nspname = '$nome_esquema' and a.attnum > 0 and a.attrelid = c.oid and a.atttypid = t.oid and c.relnamespace = p.oid ORDER BY a.attname"); } function relatorioCompleto($codigo_variavel=""){ $dados = array(); diff --git a/admin/php/metaestat.php b/admin/php/metaestat.php index c88a3c0..7849914 100644 --- a/admin/php/metaestat.php +++ b/admin/php/metaestat.php @@ -883,6 +883,33 @@ switch (strtoupper($funcao)) } exit; break; + case "CRIATABELADB": + $m = new Metaestat(); + if($nome_esquema != "i3geo_metaestat"){ + retornaJSON("erro"); + exit; + } + retornaJSON($m->criaTabelaDB($codigo_estat_conexao,$nome_esquema,$nome_tabela)); + exit; + break; + case "ALTERANOMETABELADB": + $m = new Metaestat(); + if($nome_esquema != "i3geo_metaestat"){ + retornaJSON("erro"); + exit; + } + retornaJSON($m->alteraNomeTabelaDB($codigo_estat_conexao,$nome_esquema,$nome_tabela,$novonome_tabela)); + exit; + break; + case "COPIATABELADB": + $m = new Metaestat(); + if($nome_esquema != "i3geo_metaestat"){ + retornaJSON("erro"); + exit; + } + retornaJSON($m->copiaTabelaDB($codigo_estat_conexao,$nome_esquema,$nome_tabela,$novonome_tabela)); + exit; + break; /* Valor: COLUNASTABELA -- libgit2 0.21.2