A super easy PHP Framework for web development! https://github.com/exacti/phacil-framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
3.8 KiB

<?php
/*
* Copyright © 2021 ExacTI Technology Solutions. All rights reserved.
* GPLv3 General License.
* https://exacti.com.br
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
*/
namespace Phacil\Framework\Databases\Driver;
use Phacil\Framework\Databases\Api\DriverInterface as Databases;
/**
* Oracle driver connector
*
* @package Phacil\Framework\Databases
*/
class Oracle implements Databases {
const DB_TYPE = 'Oracle';
const DB_TYPE_ID = 3;
/**
*
* @var resource|false
*/
private $connection;
/**
*
* @var array|false
*/
protected $error;
/**
* {@inheritdoc}
*/
public function __construct($hostname, $username, $password, $database, $port = '1521', $charset = 'utf8') {
$this->connection = \oci_connect($username, $password, $hostname.":".$port."/".$database, $charset);
if (!$this->connection) {
$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_module_name($this->connection, $module);
//oci_set_client_identifier($this->connection, $cid);
}
/**
* {@inheritdoc}
*/
public function query($sql) {
$stid = \oci_parse($this->connection, $sql);
\oci_execute($stid);
if (!$this->connection) {
\oci_fetch_all($stid, $res);
/** @var \Phacil\Framework\Databases\Object\ResultInterface */
$result = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$res]);
$result->setNumRows(\oci_num_rows($stid));
return $result;
} else {
throw new \Phacil\Framework\Exception('Error: ' . oci_error() . '<br />' . $sql);
}
}
/**
* {@inheritdoc}
*/
public function escape($value) {
return str_replace("'", "", $value);
}
/** {@inheritdoc} */
public function countAffected() {
return NULL;
}
/** {@inheritdoc} */
public function getLastId() {
return NULL;
}
/** {@inheritdoc} */
public function isConnected() {
return $this->connection;
}
/**
* {@inheritdoc}
*/
public function execute($sql, array $params = [])
{
// Verificar se há parâmetros e fazer o bind
if (!empty($params)) {
//$sql = $this->replacePlaceholders($sql, array_keys($params));
$stid = \oci_parse($this->connection, $sql);
foreach ($params as $placeholder => &$param) {
\oci_bind_by_name($stid, $placeholder, $param);
}
$result_exec = \oci_execute($stid);
if ($result_exec === false) {
$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);
/** @var \Phacil\Framework\Databases\Object\ResultInterface */
$resultObj = \Phacil\Framework\Registry::getInstance()->create("Phacil\Framework\Databases\Object\ResultInterface", [$res]);
$resultObj->setNumRows(\oci_num_rows($stid));
return $resultObj;
} else {
// Se não há parâmetros, executar diretamente sem consulta preparada
$query = $this->query($sql);
return $query;
}
}
/**
* Replace placeholders in the SQL query with named placeholders
*
* @param string $sql SQL query with named placeholders
* @param array $placeholders Array of named placeholders
* @return string SQL query with named placeholders
*/
private function replacePlaceholders($sql, $placeholders)
{
foreach ($placeholders as $key => $placeholder) {
$sql = str_replace($placeholder, ':' . ($key + 1), $sql);
}
return $sql;
}
/**
* {@inheritdoc}
*/
public function getDBType() {
return self::DB_TYPE;
}
/**
* {@inheritdoc}
*/
public function getDBTypeId() {
return self::DB_TYPE_ID;
}
}