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.
158 lines
4.0 KiB
158 lines
4.0 KiB
6 years ago
|
<?php
|
||
3 years ago
|
/*
|
||
|
* Copyright © 2021 ExacTI Technology Solutions. All rights reserved.
|
||
|
* GPLv3 General License.
|
||
|
* https://exacti.com.br
|
||
|
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
|
||
|
*/
|
||
6 years ago
|
|
||
3 years ago
|
namespace Phacil\Framework\Databases;
|
||
|
|
||
3 years ago
|
use Phacil\Framework\Interfaces\Databases;
|
||
3 years ago
|
|
||
|
/**
|
||
|
* Oracle driver connector
|
||
|
*
|
||
|
* @package Phacil\Framework\Databases
|
||
|
*/
|
||
11 months ago
|
class Oracle implements Databases {
|
||
3 years ago
|
/**
|
||
|
*
|
||
|
* @var resource|false
|
||
|
*/
|
||
6 years ago
|
private $connection;
|
||
3 years ago
|
|
||
2 years ago
|
/**
|
||
|
*
|
||
|
* @var array|false
|
||
|
*/
|
||
|
protected $error;
|
||
|
|
||
3 years ago
|
|
||
|
/**
|
||
|
* @param string $hostname
|
||
|
* @param string $username
|
||
|
* @param string $password
|
||
|
* @param string $database
|
||
|
* @param string $port
|
||
|
* @param string $charset
|
||
|
* @return void
|
||
|
* @throws Exception
|
||
|
*/
|
||
6 years ago
|
public function __construct($hostname, $username, $password, $database, $port = '1521', $charset = 'utf8') {
|
||
11 months ago
|
$this->connection = \oci_connect($username, $password, $hostname.":".$port."/".$database, $charset);
|
||
6 years ago
|
if (!$this->connection) {
|
||
11 months ago
|
$e = \oci_error();
|
||
6 years ago
|
$this->error = $e;
|
||
3 years ago
|
throw new \Phacil\Framework\Exception((htmlentities($e['message'], ENT_QUOTES)));
|
||
6 years ago
|
}
|
||
11 months ago
|
\oci_set_client_info($this->connection, "Administrator");
|
||
3 years ago
|
//oci_set_module_name($this->connection, $module);
|
||
|
//oci_set_client_identifier($this->connection, $cid);
|
||
6 years ago
|
|
||
|
}
|
||
3 years ago
|
|
||
|
/**
|
||
2 years ago
|
*
|
||
3 years ago
|
* @param string $sql
|
||
2 years ago
|
* @return \Phacil\Framework\Databases\Object\ResultInterface|true
|
||
|
* @throws \Phacil\Framework\Exception
|
||
3 years ago
|
*/
|
||
6 years ago
|
public function query($sql) {
|
||
11 months ago
|
$stid = \oci_parse($this->connection, $sql);
|
||
|
\oci_execute($stid);
|
||
6 years ago
|
if (!$this->connection) {
|
||
11 months ago
|
\oci_fetch_all($stid, $res);
|
||
6 years ago
|
|
||
2 years ago
|
$result = new \Phacil\Framework\Databases\Object\Result();
|
||
11 months ago
|
$result->setNumRows(\oci_num_rows($stid));
|
||
2 years ago
|
$result->setRow(isset($res[0]) ? $res[0] : []);
|
||
|
$result->setRows($res);
|
||
6 years ago
|
|
||
|
return $result;
|
||
|
|
||
|
|
||
|
} else {
|
||
3 years ago
|
throw new \Phacil\Framework\Exception('Error: ' . oci_error() . '<br />' . $sql);
|
||
6 years ago
|
}
|
||
|
}
|
||
3 years ago
|
|
||
6 years ago
|
public function escape($value) {
|
||
|
return str_replace("'", "", $value);
|
||
|
}
|
||
|
|
||
|
public function countAffected() {
|
||
|
return NULL;
|
||
|
}
|
||
|
public function getLastId() {
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
public function isConnected() {
|
||
|
return $this->connection;
|
||
|
}
|
||
|
|
||
|
public function __destruct() {
|
||
11 months ago
|
\oci_close($this->connection);
|
||
6 years ago
|
}
|
||
11 months ago
|
|
||
|
/**
|
||
|
* 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 = [])
|
||
|
{
|
||
|
// Verificar se há parâmetros e fazer o bind
|
||
|
if (!empty($params)) {
|
||
|
$sql = $this->replacePlaceholders($sql, array_keys($params));
|
||
|
|
||
11 months ago
|
$stid = \oci_parse($this->connection, $sql);
|
||
11 months ago
|
|
||
|
foreach ($params as $placeholder => &$param) {
|
||
11 months ago
|
\oci_bind_by_name($stid, $placeholder, $param);
|
||
11 months ago
|
}
|
||
|
|
||
11 months ago
|
$result_exec = \oci_execute($stid);
|
||
11 months ago
|
|
||
|
if ($result_exec === false) {
|
||
11 months ago
|
$e = \oci_error($stid);
|
||
11 months ago
|
throw new \Phacil\Framework\Exception('Error executing query: ' . htmlentities($e['message'], ENT_QUOTES));
|
||
|
}
|
||
|
|
||
|
// Processar resultados se for um SELECT
|
||
|
$res = [];
|
||
11 months ago
|
\oci_fetch_all($stid, $res);
|
||
11 months ago
|
|
||
|
$resultObj = new \Phacil\Framework\Databases\Object\Result();
|
||
11 months ago
|
$resultObj->setNumRows(\oci_num_rows($stid));
|
||
11 months ago
|
$resultObj->setRow(isset($res[0]) ? $res[0] : []);
|
||
|
$resultObj->setRows($res);
|
||
|
|
||
|
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 $placeholder) {
|
||
|
$sql = str_replace($placeholder, ':' . $placeholder, $sql);
|
||
|
}
|
||
|
|
||
|
return $sql;
|
||
|
}
|
||
6 years ago
|
}
|