Viewing file: conexao_tiba.php (6.91 KB) -rwxr-xr-x Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
# By Tiba - Inicio -> Carlos E P
# Data: 31/03/2008
class Conexao_Tiba {
var $engine_nome;//Firebird/Mysql/PostGres/ODBC --> F M P O
var $engine;
var $host;
var $porta;
var $usuario;
var $senha;
var $banco;//Local ou nome do banco
var $conectado;
var $conexao;
var $erro_num;
var $erro_msg;
function Conexao_Tiba($opcoes) {
$this->conexao = false;
$this->conectado = false;
$this->erro_num = 0;
$this->erro_msg = "";
if (!$opcoes[engine_nome]) $opcoes[engine_nome] = $opcoes[engine];
$this->host = $opcoes[host] ? $opcoes[host] : "127.0.0.1";
$this->engine_nome = $opcoes[engine_nome] ? $opcoes[engine_nome] : "Firebird";//padrão
$this->porta = $opcoes[porta];
$this->usuario = $opcoes[usuario];
$this->senha = $opcoes[senha];
$this->banco = $opcoes[banco];
if ($this->engine_nome == "Firebird") $this->engine = "F";
elseif ($this->engine_nome == "Mysql") $this->engine = "M";
elseif ($this->engine_nome == "PostGres") $this->engine = "P";
elseif ($this->engine_nome == "ODBC") $this->engine = "O";
else echo "<strong>Conexao -> Engine não definido!</strong>";
if (!$this->porta) {
if ($this->engine == "M") //Mysql
$this->porta = 3306;
elseif ($this->engine == "F") //Firebird
$this->porta = 3050;
elseif ($this->engine == "P") //PostGres
$this->porta = 5432;
}
if (!$this->usuario) {
if ($this->engine == "F")
$this->usuario = "SYSDBA";
elseif ($this->engine == "M")
$this->usuario = "root";
elseif ($this->engine == "P")
$this->usuario = "root";
}
@$this->Conectar();
}
function Conectar() {
if ($this->conectado == true)
return true;
if ($this->engine == "M") {
$this->conexao = mysql_pconnect($this->host . ":" . $this->porta,$this->usuario,$this->senha);
if (!mysql_select_db($this->banco,$this->conexao)) {
echo "<strong>A base $this->banco não existe!</strong><br>";
$this->Erro();
unset($this->conexao);
}
}
elseif ($this->engine == "F") {
//NAO SEI COMO PASSAR A PORTA PARA O FIREBIRD
$this->conexao = ibase_pconnect($this->host . ":" . $this->banco,$this->usuario,$this->senha);
}
elseif ($this->engine == "P") {
$conn_str = "HOST=$this->host, USER=$this->usuario, PASSWORD=$this->senha, PORT=$this->porta";
$this->conexao = pg_pconnect($conn_str);
}
else {
$conn_str = "HOST=$this->host, PORT=$this->porta";
$this->conexao = odbc_pconnect($conn_str,$this->usuario,$this->senha);
}
if (!$this->conexao) {
$this->conectado = false;
return $this->Erro();
}
else {
$this->erro_num = 0;
$this->erro_msg = "";
$this->conectado = true;
return true;
}
}
function Erro() {
if ($this->engine == "M") {
if (!$this->conexao) {
$this->erro_num = 0;
$this->erro_msg = "NAO CONECTADO";
}
else {
$this->erro_num = mysql_errno($this->conexao);
$this->erro_msg = mysql_error($this->conexao);
}
}
elseif ($this->engine == "F") {
$this->erro_num = ibase_errcode();
$this->erro_msg = ibase_errmsg();
}
elseif ($this->engine == "P") {
$this->erro_num = 0;//pg_last_error();
$this->erro_msg = pg_last_error();
}
else {
$this->erro_num = odbc_error();
$this->erro_msg = odbc_errormsg();
}
return $this->erro_num . " -> " . $this->erro_msg;
}
function Executar($sql) {
if (!$this->conectado)
return $this->Erro();
if ($this->engine == "M")
return mysql_query($sql);
elseif ($this->engine == "F")
return ibase_query($this->conexao,$sql);
elseif ($this->engine == "P")
return pg_query($this->conexao,$sql);
else
return odbc_exec($this->conexao,$sql);
return false;
}
function Fetch($resultado,$tipo = "A") {//Assoc ou Row
if (!$this->conectado)
return $this->Erro();
if ($this->engine == "M") {
if ($tipo == "A")
return mysql_fetch_assoc($resultado);
else
return mysql_fetch_row($resultado);
}
elseif ($this->engine == "F") {
if ($tipo == "A")
return ibase_fetch_assoc($resultado);
else
return ibase_fetch_row($resultado);
}
elseif ($this->engine == "P") {
if ($tipo == "A")
return pg_fetch_assoc($resultado);
else
return pg_fetch_row($resultado);
}
else {
if ($tipo == "A")
return odbc_fetch_array($resultado);
else
return odbc_fetch_row($resultado);
}
return false;
}
function UmFetch($sql,$tipo = "A") {
if (!$this->conectado)
return $this->Erro();
return $this->Fetch($this->Executar($sql),$tipo);
}
function Transacao($modo,$arg="") {
if (in_array($modo,array("Iniciar","I","Trans"))) {
if ($this->engine == "M") {
return mysql_query("BEGIN",$this->conexao);
}
elseif ($this->engine == "F") {
if ($arg!="")
return ibase_trans($arg,$this->conexao);
else
return ibase_trans($this->conexao);
}
elseif ($this->engine == "P") {
return false;
}
else {
return odbc_autocommit($this->conexao,false);
}
}
elseif (in_array($modo,array("Finalizar","F","Commit"))) {
if ($this->engine == "M") {
return mysql_query("COMMIT WORK",$this->conexao);
}
elseif ($this->engine == "F") {
if ($arg!="")
return ibase_commit($this->conexao,$arg);//arg -> numero transação
else
return ibase_commit($this->conexao);
}
elseif ($this->engine == "P") {
return false;
}
else {
$ret = odbc_commit($this->conexao);
if ($ret)
return odbc_autocommit($this->conexao,true);
else
return $ret;
}
}
elseif (in_array($modo,array("Cancelar","C","Rollback","R"))){
if ($this->engine == "M") {
return mysql_query("ROLLBACK",$this->conexao);
}
elseif ($this->engine == "F") {
if ($arg)
return ibase_rollback($this->conexao,$arg);
else
return ibase_rollback($this->conexao);
}
elseif ($this->engine == "P") {
return false;
}
}
}
}
/*
echo "FIREBIRD<br>";
$opcoes[host] = "localhost";
$opcoes[engine_nome] = "Firebird";
$opcoes[usuario] = "GERAL";
$opcoes[senha] = "Senha";
$opcoes[banco] = "D:/scuna/PREV2000/data/CREDIARIO.FDB";
$conexao_fb = new Conexao_Tiba($opcoes);
print_r($conexao_fb->UmFetch("SELECT COD_CLIENTE, NOME FROM CRD_CLIENTES WHERE COD_CLIENTE = 2728"));
echo "<br><br>MYSQL<br>";
$opcoes[host] = "localhost";
$opcoes[engine_nome] = "Mysql";
$opcoes[usuario] = "root";
$opcoes[senha] = "Senha";
$opcoes[banco] = "tiba";
$conexao_my = new Conexao_Tiba($opcoes);
print_r($conexao_my->UmFetch("SELECT * FROM usuarios WHERE cod_usuario = 1"));
*/
?>
|