From 4a8701ff85bf3104845e30e6ad482cddca8c8489 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Fri, 22 Mar 2024 22:11:53 -0300 Subject: [PATCH] Helper for APEX --- .../Conectors/Oracle/ORDS/Connector.php | 35 ++---------- .../Conectors/Oracle/ORDS/Helper/Data.php | 53 +++++++++++++++++++ .../Oracle/ORDS/Model/Handler/Curl.php | 7 ++- .../Conectors/Oracle/ORDS/Model/Query.php | 13 ++++- .../database/Databases/Driver/OracleORDS.php | 1 + 5 files changed, 75 insertions(+), 34 deletions(-) create mode 100644 system/database/Databases/Conectors/Oracle/ORDS/Helper/Data.php diff --git a/system/database/Databases/Conectors/Oracle/ORDS/Connector.php b/system/database/Databases/Conectors/Oracle/ORDS/Connector.php index 9376e70..046b63f 100644 --- a/system/database/Databases/Conectors/Oracle/ORDS/Connector.php +++ b/system/database/Databases/Conectors/Oracle/ORDS/Connector.php @@ -9,6 +9,10 @@ namespace Phacil\Framework\Databases\Conectors\Oracle\ORDS; +/** + * @since 2.0.0 + * @package Phacil\Framework\Databases\Conectors\Oracle\ORDS + */ class Connector { private $host; @@ -46,35 +50,4 @@ class Connector { public function getPass(){ return $this->pass; } - - public function escape($value) - { - $formattedValue = null; - - switch (gettype($value)) { - case 'integer': - $formattedValue = $value; // Números inteiros não precisam de formatação - break; - case 'double': - $formattedValue = sprintf('%F', $value); // Formata números decimais - break; - case 'boolean': - $formattedValue = $value ? 'TRUE' : 'FALSE'; // Converte booleanos para strings 'TRUE' ou 'FALSE' - break; - case 'NULL': - $formattedValue = 'NULL'; // Valores nulos - break; - case 'object': - $formattedValue = "'" . serialize($value) . "'"; // Valores nulos - break; - case 'array': - $formattedValue = "'" . \Phacil\Framework\Json::encode($value) . "'"; // Valores nulos - break; - default: - // Escapa as aspas simples e adiciona aspas simples ao redor de strings - $formattedValue = "'" . str_replace("'", "''", $value) . "'"; - } - - return $formattedValue; - } } \ No newline at end of file diff --git a/system/database/Databases/Conectors/Oracle/ORDS/Helper/Data.php b/system/database/Databases/Conectors/Oracle/ORDS/Helper/Data.php new file mode 100644 index 0000000..d81fa53 --- /dev/null +++ b/system/database/Databases/Conectors/Oracle/ORDS/Helper/Data.php @@ -0,0 +1,53 @@ + + */ + +namespace Phacil\Framework\Databases\Conectors\Oracle\ORDS\Helper; + +/** + * @since 2.0.0 + * @package Phacil\Framework\Databases\Conectors\Oracle\ORDS + */ +class Data extends \Phacil\Framework\AbstractHelper { + + /** + * @param mixed $value + * @return mixed + * @throws \Phacil\Framework\Exception\InvalidArgumentException + */ + public function escape($value) + { + $formattedValue = null; + + switch (gettype($value)) { + case 'integer': + $formattedValue = $value; //Integers don't need formatting + break; + case 'double': + $formattedValue = sprintf('%F', $value); //Formats decimal numbers + break; + case 'boolean': + $formattedValue = $value ? 'TRUE' : 'FALSE'; //Convert boolean to string 'TRUE' or 'FALSE' + break; + case 'NULL': + $formattedValue = 'NULL'; + break; + case 'object': + $formattedValue = "'" . serialize($value) . "'"; //Serialize the object + break; + case 'array': + $formattedValue = "'" . \Phacil\Framework\Json::encode($value) . "'"; + break; + default: + // Escape single quotes and add single quotes around strings + $formattedValue = "'" . str_replace("'", "''", $value) . "'"; + } + + return $formattedValue; + } +} \ No newline at end of file diff --git a/system/database/Databases/Conectors/Oracle/ORDS/Model/Handler/Curl.php b/system/database/Databases/Conectors/Oracle/ORDS/Model/Handler/Curl.php index 389fd68..8294ef9 100644 --- a/system/database/Databases/Conectors/Oracle/ORDS/Model/Handler/Curl.php +++ b/system/database/Databases/Conectors/Oracle/ORDS/Model/Handler/Curl.php @@ -12,6 +12,11 @@ namespace Phacil\Framework\Databases\Conectors\Oracle\ORDS\Model\Handler; use Phacil\Framework\Databases\Conectors\Oracle\ORDS\Api\HandleInterface; use Phacil\Framework\Databases\Conectors\Oracle\ORDS\Connector; +/** + * Tested with Apex 23+ + * + * @package Phacil\Framework\Databases\Conectors\Oracle\ORDS\Model\Handler + */ class Curl implements HandleInterface { /** * @@ -20,7 +25,7 @@ class Curl implements HandleInterface { private $curl; /** - * @var \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Conector + * @var \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Connector */ private $conector; diff --git a/system/database/Databases/Conectors/Oracle/ORDS/Model/Query.php b/system/database/Databases/Conectors/Oracle/ORDS/Model/Query.php index cb3ad68..2828c12 100644 --- a/system/database/Databases/Conectors/Oracle/ORDS/Model/Query.php +++ b/system/database/Databases/Conectors/Oracle/ORDS/Model/Query.php @@ -12,6 +12,7 @@ namespace Phacil\Framework\Databases\Conectors\Oracle\ORDS\Model; use Phacil\Framework\Databases\Conectors\Oracle\ORDS\Connector; use Phacil\Framework\Databases\Conectors\Oracle\ORDS\Api\HandleInterface; use Phacil\Framework\Databases\Conectors\Oracle\ORDS\Api\Query as QueryApi; +use Phacil\Framework\Databases\Conectors\Oracle\ORDS\Helper\Data as DataHelper; class Query implements QueryApi { @@ -45,6 +46,12 @@ class Query implements QueryApi { */ private $items = []; + /** + * + * @var \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Helper\Data + */ + private $helper; + /** * @param \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Connector $conector * @param \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Api\HandleInterface $handle @@ -52,10 +59,12 @@ class Query implements QueryApi { */ public function __construct( Connector $conector, - HandleInterface $handle + HandleInterface $handle, + DataHelper $helper ) { $this->conector = $conector; $this->handle = $handle; + $this->helper = $helper; return $this; } @@ -68,7 +77,7 @@ class Query implements QueryApi { // Percorre os parâmetros a serem vinculados foreach ($bindParams as $key => $value) { // Detecta o tipo do valor e formata conforme necessário - $formattedValue = $this->conector->escape($value); + $formattedValue = $this->helper->escape($value); if(!is_string($key)) $sql = preg_replace('/\?/', $formattedValue, $sql, 1); diff --git a/system/database/Databases/Driver/OracleORDS.php b/system/database/Databases/Driver/OracleORDS.php index af4a1cd..a76be96 100644 --- a/system/database/Databases/Driver/OracleORDS.php +++ b/system/database/Databases/Driver/OracleORDS.php @@ -13,6 +13,7 @@ use Phacil\Framework\Databases\Api\DriverInterface; /** * Alternative ORDS connection method. * + * @since 2.0.0 * @package Phacil\Framework\Databases */ class OracleORDS implements DriverInterface {