type = $type; $this->host = $host; $this->port = $port; $this->dataBase = $dataBase; $this->user = $user; $this->password = $password; } function db_connect() { $conn = null; if ($this->type == DB__MSSQL) { $conn = @mssql_connect($this->host . ($this->port ? ":$this->port" : ''), $this->user, $this->password); if ($conn) mssql_select_db($this->dataBase, $conn); } else if ($this->type == DB__PGSQL) { $connectStr = sprintf("host='%s' port='%s' dbname='%s' user='%s' password='%s'", $this->host, ($this->port ? $this->port : DB__PGSQL_PORT), $this->dataBase, $this->user, $this->password); $conn = @pg_connect($connectStr); } else if ($this->type == DB_FBIIBS) { $connectStr = sprintf("%s/%s:%s", $this->host, ($this->port ? $this->port : DB_FBIIBS_PORT), $this->dataBase); $conn = @ibase_connect($connectStr, $this->user, $this->password); } else if ($this->type == DB_ORACLE) { $connectStr = sprintf("(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=%s)(PORT=%s)))(CONNECT_DATA=(SERVER=DEDICATED)(SID=%s)))", $this->host, ($this->port ? $this->port : DB_ORACLE_PORT), $this->dataBase); $conn = @oci_connect($this->user, $this->password, $connectStr); if (!$conn) { $connectStr = sprintf("(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=%s)(PORT=%s)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=%s)))", $this->host, ($this->port ? $this->port : DB_ORACLE_PORT), $this->dataBase); $conn = @oci_connect($this->user, $this->password, $connectStr); } $this->ociResourceError = $conn; } else if ($this->type == DB__MYSQL) { $connectStr = sprintf("%s:%s", $this->host, ($this->port ? $this->port : DB__MYSQL_PORT)); $conn = mysql_connect($connectStr, $this->user, $this->password); if ($conn && $this->dataBase) mysql_select_db($this->dataBase); } $this->conn = $conn; return $conn ? true : false; } function db_close() { if ($this->type == DB__MSSQL) { return mssql_close($this->conn); } else if ($this->type == DB__PGSQL) { return pg_close($this->conn); } else if ($this->type == DB_FBIIBS) { return ibase_close($this->conn); } else if ($this->type == DB_ORACLE) { return oci_close($this->conn); } else if ($this->type == DB__MYSQL) { return mysql_close($this->conn); } return false; } function db_query($query) { if ($this->type == DB__MSSQL) { return mssql_query($query, $this->conn); } else if ($this->type == DB__PGSQL) { return pg_query($this->conn, $query); } else if ($this->type == DB_FBIIBS) { $this->queryIbase = $query; return ibase_query($this->conn, $query); } else if ($this->type == DB_ORACLE) { $ret = false; $this->queryOracle = $query; $result = oci_parse($this->conn, $query); if ($result) { $this->ociResourceError = $result; $ret = oci_execute($result); } return $ret ? $result : false; } else if ($this->type == DB__MYSQL) { return mysql_query($query, $this->conn); } return false; } function db_fetch_array($result) { if ($this->type == DB__MSSQL) { return $this->PadronizaKey(mssql_fetch_array($result, MSSQL_ASSOC)); } else if ($this->type == DB__PGSQL) { return pg_fetch_array($result, null, PGSQL_ASSOC); } else if ($this->type == DB_FBIIBS) { return $this->PadronizaKey(ibase_fetch_assoc($result)); } else if ($this->type == DB_ORACLE) { return $this->PadronizaKey(oci_fetch_array($result, OCI_ASSOC + OCI_RETURN_NULLS)); } else if ($this->type == DB__MYSQL) { return $this->PadronizaKey(mysql_fetch_array($result, MYSQL_ASSOC)); } return false; } function db_fetch_row($result) { if ($this->type == DB__MSSQL) { return mssql_fetch_row($result); } else if ($this->type == DB__PGSQL) { return pg_fetch_row($result); } else if ($this->type == DB_FBIIBS) { return ibase_fetch_row($result); } else if ($this->type == DB_ORACLE) { return oci_fetch_row($result); } else if ($this->type == DB__MYSQL) { return mysql_fetch_row($result); } return false; } function db_num_rows($result) { if ($this->type == DB__MSSQL) { return mssql_num_rows($result); } else if ($this->type == DB__PGSQL) { return pg_num_rows($result); } else if ($this->type == DB_FBIIBS) { $query = "select count(*) as num_rows " . substr($this->queryIbase, stripos($this->queryIbase, 'from')); $result = ibase_query($this->conn, $query); if ($result) { $row = ibase_fetch_row($result); return $row[0]; } return 0; } else if ($this->type == DB_ORACLE) { $query = "select count(*) as num_rows " . substr($this->queryOracle, stripos($this->queryOracle, 'from')); $result = oci_parse($this->conn, $query); if ($result) { oci_execute($result); $row = oci_fetch_row($result); return $row[0]; } return oci_num_rows($result); } else if ($this->type == DB__MYSQL) { return mysql_num_rows($result); } return false; } function db_rows_affected($result) { if ($this->type == DB__MSSQL) { return mssql_rows_affected($this->conn); } else if ($this->type == DB__PGSQL) { return pg_affected_rows($result); } else if ($this->type == DB_FBIIBS) { return ibase_affected_rows(); } else if ($this->type == DB_ORACLE) { return oci_num_rows($result); } else if ($this->type == DB__MYSQL) { return mysql_affected_rows($this->conn); } return false; } function db_getmessage() { if ($this->type == DB__MSSQL) { return mssql_get_last_message(); } else if ($this->type == DB__PGSQL) { return pg_last_error($this->conn); } else if ($this->type == DB_FBIIBS) { return ibase_errmsg(); } else if ($this->type == DB_ORACLE) { $error = oci_error($this->ociResourceError); return $error["message"]; } else if ($this->type == DB__MYSQL) { return mysql_error($this->conn); } return false; } private function PadronizaKey($ar) { $arResult = array(); foreach ($ar as $key => $value) { $newKey = strtolower($key); $arResult[$newKey] = $value; } return $arResult; } } ?>