_schema = "public."; $this->_tabela = "{$this->_schema}pais"; $this->_campos_lista = $this->_todos_campos = "idpais, nome, geom"; if( is_numeric( $idpais ) ) { $this->idpais = $idpais; } if( is_string( $nome ) ) { $this->nome = $nome; } if( is_string( $geom ) ) { $this->geom = $geom; } } /** * Cria um novo registro * * @return bool */ function cadastra() { if( is_string( $this->nome ) ) { $db = new clsBanco(); $campos = ""; $valores = ""; $gruda = ""; if( is_string( $this->nome ) ) { $campos .= "{$gruda}nome"; $valores .= "{$gruda}'" . addslashes($this->nome) . "'"; $gruda = ", "; } if( is_string( $this->geom ) ) { $campos .= "{$gruda}geom"; $valores .= "{$gruda}'{$this->geom}'"; $gruda = ", "; } $idpais = $db->campoUnico("SELECT COALESCE( MAX(idpais), 0 ) + 1 FROM {$this->_tabela}" ); $db->Consulta( "INSERT INTO {$this->_tabela} ( idpais, $campos ) VALUES( $idpais, $valores )" ); return $idpais; } return false; } /** * Edita os dados de um registro * * @return bool */ function edita() { if( is_numeric( $this->idpais ) ) { $db = new clsBanco(); $set = ""; if( is_string( $this->nome ) ) { $set .= "{$gruda}nome = '" . addslashes($this->nome) . "'"; $gruda = ", "; } if( is_string( $this->geom ) ) { $set .= "{$gruda}geom = '{$this->geom}'"; $gruda = ", "; } if( $set ) { $db->Consulta( "UPDATE {$this->_tabela} SET $set WHERE idpais = '{$this->idpais}'" ); return true; } } return false; } /** * Retorna uma lista filtrados de acordo com os parametros * * @param string str_nome * @param string str_geom * * @return array */ function lista( $int_idpais = null, $str_nome = null, $str_geom = null ) { $sql = "SELECT {$this->_campos_lista} FROM {$this->_tabela}"; $filtros = ""; $whereAnd = " WHERE "; if( is_numeric( $int_idpais ) ) { $filtros .= "{$whereAnd} idpais = '{$int_idpais}'"; $whereAnd = " AND "; } if( is_string( $str_nome ) ) { $filtros .= "{$whereAnd} nome LIKE E'%" . addslashes($str_nome) . "%'"; $whereAnd = " AND "; } if( is_string( $str_geom ) ) { $filtros .= "{$whereAnd} geom LIKE '%{$str_geom}%'"; $whereAnd = " AND "; } $db = new clsBanco(); $countCampos = count( explode( ",", $this->_campos_lista ) ); $resultado = array(); $sql .= $filtros . $this->getOrderby() . $this->getLimite(); $this->_total = $db->CampoUnico( "SELECT COUNT(0) FROM {$this->_tabela} {$filtros}" ); $db->Consulta( $sql ); if( $countCampos > 1 ) { while ( $db->ProximoRegistro() ) { $tupla = $db->Tupla(); $tupla["_total"] = $this->_total; $resultado[] = $tupla; } } else { while ( $db->ProximoRegistro() ) { $tupla = $db->Tupla(); $resultado[] = $tupla[$this->_campos_lista]; } } if( count( $resultado ) ) { return $resultado; } return false; } /** * Retorna um array com os dados de um registro * * @return array */ function detalhe() { if( is_numeric( $this->idpais ) ) { $db = new clsBanco(); $db->Consulta( "SELECT {$this->_todos_campos} FROM {$this->_tabela} WHERE idpais = '{$this->idpais}'" ); $db->ProximoRegistro(); return $db->Tupla(); } return false; } /** * Retorna true se o registro existir. Caso contrário retorna false. * * @return bool */ function existe() { if( is_numeric( $this->idpais ) ) { $db = new clsBanco(); $db->Consulta( "SELECT 1 FROM {$this->_tabela} WHERE idpais = '{$this->idpais}'" ); if( $db->ProximoRegistro() ) { return true; } } return false; } /** * Exclui um registro * * @return bool */ function excluir() { if( is_numeric( $this->idpais ) ) { // delete $db = new clsBanco(); $db->Consulta( "DELETE FROM {$this->_tabela} WHERE idpais = '{$this->idpais}'" ); return true; } return false; } /** * Define quais campos da tabela serao selecionados na invocacao do metodo lista * * @return null */ function setCamposLista( $str_campos ) { $this->_campos_lista = $str_campos; } /** * Define que o metodo Lista devera retornoar todos os campos da tabela * * @return null */ function resetCamposLista() { $this->_campos_lista = $this->_todos_campos; } /** * Define limites de retorno para o metodo lista * * @return null */ function setLimite( $intLimiteQtd, $intLimiteOffset = null ) { $this->_limite_quantidade = $intLimiteQtd; $this->_limite_offset = $intLimiteOffset; } /** * Retorna a string com o trecho da query resposavel pelo Limite de registros * * @return string */ function getLimite() { if( is_numeric( $this->_limite_quantidade ) ) { $retorno = " LIMIT {$this->_limite_quantidade}"; if( is_numeric( $this->_limite_offset ) ) { $retorno .= " OFFSET {$this->_limite_offset} "; } return $retorno; } return ""; } /** * Define campo para ser utilizado como ordenacao no metolo lista * * @return null */ function setOrderby( $strNomeCampo ) { // limpa a string de possiveis erros (delete, insert, etc) //$strNomeCampo = eregi_replace(); if( is_string( $strNomeCampo ) && $strNomeCampo ) { $this->_campo_order_by = $strNomeCampo; } } /** * Retorna a string com o trecho da query resposavel pela Ordenacao dos registros * * @return string */ function getOrderby() { if( is_string( $this->_campo_order_by ) ) { return " ORDER BY {$this->_campo_order_by} "; } return ""; } } ?>