From 8188a9ecfa1fa9b2f3aa3f41acc5f4d3771edb78 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Fri, 26 Jan 2024 23:55:44 -0300 Subject: [PATCH] Added Oracle PDO driver --- system/database/databases/oracle.php | 28 +-- system/database/databases/oracle_pdo.php | 212 +++++++++++++++++++++++ 2 files changed, 226 insertions(+), 14 deletions(-) create mode 100644 system/database/databases/oracle_pdo.php diff --git a/system/database/databases/oracle.php b/system/database/databases/oracle.php index 154981e..c2924c2 100644 --- a/system/database/databases/oracle.php +++ b/system/database/databases/oracle.php @@ -40,13 +40,13 @@ class Oracle implements Databases { * @throws Exception */ public function __construct($hostname, $username, $password, $database, $port = '1521', $charset = 'utf8') { - $this->connection = oci_connect($username, $password, $hostname."/".$database, $charset); + $this->connection = \oci_connect($username, $password, $hostname.":".$port."/".$database, $charset); if (!$this->connection) { - $e = oci_error(); + $e = \oci_error(); $this->error = $e; throw new \Phacil\Framework\Exception((htmlentities($e['message'], ENT_QUOTES))); } - oci_set_client_info($this->connection, "Administrator"); + \oci_set_client_info($this->connection, "Administrator"); //oci_set_module_name($this->connection, $module); //oci_set_client_identifier($this->connection, $cid); @@ -59,13 +59,13 @@ class Oracle implements Databases { * @throws \Phacil\Framework\Exception */ public function query($sql) { - $stid = oci_parse($this->connection, $sql); - oci_execute($stid); + $stid = \oci_parse($this->connection, $sql); + \oci_execute($stid); if (!$this->connection) { - oci_fetch_all($stid, $res); + \oci_fetch_all($stid, $res); $result = new \Phacil\Framework\Databases\Object\Result(); - $result->setNumRows(oci_num_rows($stid)); + $result->setNumRows(\oci_num_rows($stid)); $result->setRow(isset($res[0]) ? $res[0] : []); $result->setRows($res); @@ -93,7 +93,7 @@ class Oracle implements Databases { } public function __destruct() { - oci_close($this->connection); + \oci_close($this->connection); } /** @@ -110,25 +110,25 @@ class Oracle implements Databases { if (!empty($params)) { $sql = $this->replacePlaceholders($sql, array_keys($params)); - $stid = oci_parse($this->connection, $sql); + $stid = \oci_parse($this->connection, $sql); foreach ($params as $placeholder => &$param) { - oci_bind_by_name($stid, $placeholder, $param); + \oci_bind_by_name($stid, $placeholder, $param); } - $result_exec = oci_execute($stid); + $result_exec = \oci_execute($stid); if ($result_exec === false) { - $e = oci_error($stid); + $e = \oci_error($stid); throw new \Phacil\Framework\Exception('Error executing query: ' . htmlentities($e['message'], ENT_QUOTES)); } // Processar resultados se for um SELECT $res = []; - oci_fetch_all($stid, $res); + \oci_fetch_all($stid, $res); $resultObj = new \Phacil\Framework\Databases\Object\Result(); - $resultObj->setNumRows(oci_num_rows($stid)); + $resultObj->setNumRows(\oci_num_rows($stid)); $resultObj->setRow(isset($res[0]) ? $res[0] : []); $resultObj->setRows($res); diff --git a/system/database/databases/oracle_pdo.php b/system/database/databases/oracle_pdo.php new file mode 100644 index 0000000..22fb48d --- /dev/null +++ b/system/database/databases/oracle_pdo.php @@ -0,0 +1,212 @@ + true, + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION + ); + + $this->connection = new \PDO($dsn, $username, $password, $options); + } catch (\PDOException $e) { + throw new \Phacil\Framework\Exception('Failed to connect to database. Reason: \'' . $e->getMessage() . '\''); + } + $this->connection->exec("SET NAMES 'utf8'"); + $this->connection->exec("SET CHARACTER SET utf8"); + $this->connection->exec("SET CHARACTER_SET_CONNECTION=utf8"); + $this->connection->exec("SET SQL_MODE = ''"); + $this->rowCount = 0; + } + public function prepare($sql) + { + $this->statement = $this->connection->prepare($sql); + } + public function bindParam($parameter, $variable, $data_type = \PDO::PARAM_STR, $length = 0) + { + if ($length) { + $this->statement->bindParam($parameter, $variable, $data_type, $length); + } else { + $this->statement->bindParam($parameter, $variable, $data_type); + } + } + + public function query($sql, $params = array()) + { + $this->statement = $this->connection->prepare($sql); + + $result = false; + try { + if ($this->statement && $this->statement->execute($params)) { + $data = array(); + $this->rowCount = $this->statement->rowCount(); + if ($this->rowCount > 0) { + try { + $data = $this->statement->fetchAll(\PDO::FETCH_ASSOC); + } catch (\Exception $ex) { + } + } + // free up resources + $this->statement->closeCursor(); + $this->statement = null; + $result = new \Phacil\Framework\Databases\Object\Result(); + $result->row = (isset($data[0]) ? $data[0] : array()); + $result->rows = $data; + $result->num_rows = $this->rowCount; + } + } catch (\PDOException $e) { + throw new \Phacil\Framework\Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . '
' . $sql); + } + if ($result) { + return $result; + } else { + $result = new \Phacil\Framework\Databases\Object\Result(); + $result->row = array(); + $result->rows = array(); + $result->num_rows = 0; + return $result; + } + } + public function escape($value) + { + return str_replace(array("\\", "\0", "\n", "\r", "\x1a", "'", '"'), array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"'), $value); + } + public function countAffected() + { + if ($this->statement) { + return $this->statement->rowCount(); + } else { + return $this->rowCount; + } + } + public function getLastId() + { + return $this->connection->lastInsertId(); + } + + public function isConnected() + { + if ($this->connection) { + return true; + } else { + return false; + } + } + + public function __destruct() + { + unset($this->connection); + } + + /** + * Execute a prepared statement with parameters + * + * @param string $sql SQL query with named placeholders + * @param array $params Associative array of parameters + * @return \Phacil\Framework\Databases\Object\ResultInterface|true + * @throws \Phacil\Framework\Exception + */ + public function execute($sql, array $params = []) + { + try { + $stmt = $this->connection->prepare($sql); + + if ($stmt === false) { + throw new \Phacil\Framework\Exception('Error preparing query: ' . implode(', ', $this->connection->errorInfo())); + } + + // Bind parameters if there are any + if (!empty($params)) { + foreach ($params as $placeholder => &$param) { + $stmt->bindParam($placeholder, $param, $this->getParamType($param)); + } + } + + $stmt->execute(); + + if ($stmt->columnCount()) { + $data = new \Phacil\Framework\Databases\Object\Result(); + $data->setNumRows($stmt->rowCount()); + $data->setRows($stmt->fetchAll(\PDO::FETCH_ASSOC)); + //$data->setRow(isset($data->rows[0]) ? $data->rows[0] : null); + $stmt->closeCursor(); + + return $data; + } else { + $this->rowCount = $stmt->rowCount(); + $stmt->closeCursor(); + + return true; + } + } catch (\PDOException $exception) { + throw new \Phacil\Framework\Exception($exception->getMessage()); + } + } + + /** + * + * @param mixed $param + * @return int + */ + private function getParamType(&$param) + { + $paramType = gettype($param); + + switch ($paramType) { + case 'boolean': + $paramType = \PDO::PARAM_BOOL; + break; + case 'integer': + $paramType = \PDO::PARAM_INT; + break; + case 'double': + case 'float': + $paramType = \PDO::PARAM_STR; + break; + case 'NULL': + $paramType = \PDO::PARAM_NULL; + break; + default: + $paramType = \PDO::PARAM_STR; + break; + } + + return $paramType; + } +}