From d8e593eae36a155012ee1f886aeff0d2b638802b Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Fri, 22 Mar 2024 21:57:32 -0300 Subject: [PATCH] Oracle ORDS Query interface --- .../Conectors/Oracle/ORDS/Api/Query.php | 45 +++++++++++++++++++ .../Conectors/Oracle/ORDS/Model/Query.php | 40 ++++++++++++++--- .../database/Databases/Driver/OracleORDS.php | 16 +++---- system/etc/preferences.json | 3 +- 4 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 system/database/Databases/Conectors/Oracle/ORDS/Api/Query.php diff --git a/system/database/Databases/Conectors/Oracle/ORDS/Api/Query.php b/system/database/Databases/Conectors/Oracle/ORDS/Api/Query.php new file mode 100644 index 0000000..00d0e11 --- /dev/null +++ b/system/database/Databases/Conectors/Oracle/ORDS/Api/Query.php @@ -0,0 +1,45 @@ + + */ + +namespace Phacil\Framework\Databases\Conectors\Oracle\ORDS\Api; + +interface Query { + /** + * @param string $sql + * @param array $bindParams + * @return $this + * @throws \Phacil\Framework\Exception\InvalidArgumentException + */ + public function prepareSQL($sql, $bindParams = []); + + /** + * @return $this + * @throws \Phacil\Framework\Exception\RuntimeException + * @throws \Phacil\Framework\Exception\InvalidArgumentException + */ + public function execute(); + + /** @return int */ + public function getNumRows(); + + /** @return array */ + public function getItems(); + + /** + * @return void + * @throws \ReflectionException + * @throws \Exception + * @throws \Phacil\Framework\Exception + * @throws \Phacil\Framework\Exception\ReflectionException + */ + public function close(); + + /** @return string */ + public function getError(); +} \ No newline at end of file diff --git a/system/database/Databases/Conectors/Oracle/ORDS/Model/Query.php b/system/database/Databases/Conectors/Oracle/ORDS/Model/Query.php index 6cdecf2..cb3ad68 100644 --- a/system/database/Databases/Conectors/Oracle/ORDS/Model/Query.php +++ b/system/database/Databases/Conectors/Oracle/ORDS/Model/Query.php @@ -11,8 +11,9 @@ 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; -class Query { +class Query implements QueryApi { /** * @@ -20,27 +21,47 @@ class Query { */ private $conector; + /** + * + * @var \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Api\HandleInterface + */ private $handle; + /** + * + * @var string + */ private $sql; + /** + * + * @var int + */ private $num_rows = 0; + /** + * + * @var array + */ private $items = []; + /** + * @param \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Connector $conector + * @param \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Api\HandleInterface $handle + * @return $this + */ public function __construct( Connector $conector, HandleInterface $handle ) { $this->conector = $conector; $this->handle = $handle; + + return $this; } /** - * @param string $sql - * @param array $bindParams - * @return $this - * @throws \Phacil\Framework\Exception\InvalidArgumentException + * @inheritdoc */ public function prepareSQL($sql, $bindParams = []) { @@ -61,6 +82,9 @@ class Query { return $this; } + /** + * {@inheritdoc} + */ public function execute() { if(empty($this->sql)){ throw new \Phacil\Framework\Exception\RuntimeException('Query is empty'); @@ -89,14 +113,19 @@ class Query { } } + /** {@inheritdoc} */ public function getNumRows() { return $this->num_rows; } + /** {@inheritdoc} */ public function getItems() { return $this->items; } + /** + * {@inheritdoc} + */ public function close() { $this->handle->close(); @@ -104,6 +133,7 @@ class Query { $this->handle = \Phacil\Framework\Registry::getInstance()->create(HandleInterface::class); } + /** @inheritdoc */ public function getError(){ return $this->handle->error(); } diff --git a/system/database/Databases/Driver/OracleORDS.php b/system/database/Databases/Driver/OracleORDS.php index 8f41377..af4a1cd 100644 --- a/system/database/Databases/Driver/OracleORDS.php +++ b/system/database/Databases/Driver/OracleORDS.php @@ -39,15 +39,15 @@ class OracleORDS implements DriverInterface /** * - * @var \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Conector + * @var \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Connector */ private $connection = null; /** * - * @var \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Model\Query + * @var \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Api\Query */ - private $statement = null; + private $statement; private $rowCount; @@ -55,8 +55,8 @@ class OracleORDS implements DriverInterface public function __construct($hostname, $username, $password, $database, $port = '8181', $charset = 'UTF8') { try { - /** @var \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Conector */ - $this->connection = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Conectors\Oracle\ORDS\Conector::class, [ + /** @var \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Connector */ + $this->connection = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Conectors\Oracle\ORDS\Connector::class, [ $hostname.$database, \Phacil\Framework\Config::DB_PORT() ?: $port, $username, @@ -77,8 +77,8 @@ class OracleORDS implements DriverInterface public function createStatement() { $this->rowCount = 0; - /** @var \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Model\Query */ - $this->statement = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Conectors\Oracle\ORDS\Model\Query::class); + /** @var \Phacil\Framework\Databases\Conectors\Oracle\ORDS\Api\Query */ + $this->statement = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Conectors\Oracle\ORDS\Api\Query::class); } /** {@inheritdoc} */ @@ -119,7 +119,7 @@ class OracleORDS implements DriverInterface $stmt = $this->statement->prepareSQL($sql, $params)->execute(); if (!$stmt) { - throw new \Phacil\Framework\Exception('Error preparing query: ' . implode(', ', $this->statement->getError())); + throw new \Phacil\Framework\Exception('Error preparing query: ' . $this->statement->getError()); } if ($stmt->getNumRows() && !empty($stmt->getItems())) { diff --git a/system/etc/preferences.json b/system/etc/preferences.json index 1398b20..187d369 100644 --- a/system/etc/preferences.json +++ b/system/etc/preferences.json @@ -13,6 +13,7 @@ "Phacil\\Framework\\Databases\\Api\\LogInterface": "Phacil\\Framework\\Log", "Phacil\\Framework\\Interfaces\\Serializer": "Phacil\\Framework\\Json", "Phacil\\Framework\\MagiQL\\Api\\Factory": "Phacil\\Framework\\MagiQL\\Factory", - "Phacil\\Framework\\Databases\\Conectors\\Oracle\\ORDS\\Api\\HandleInterface": "Phacil\\Framework\\Databases\\Conectors\\Oracle\\ORDS\\Model\\Handler\\Curl" + "Phacil\\Framework\\Databases\\Conectors\\Oracle\\ORDS\\Api\\HandleInterface": "Phacil\\Framework\\Databases\\Conectors\\Oracle\\ORDS\\Model\\Handler\\Curl", + "Phacil\\Framework\\Databases\\Conectors\\Oracle\\ORDS\\Api\\Query": "Phacil\\Framework\\Databases\\Conectors\\Oracle\\ORDS\\Model\\Query" } } \ No newline at end of file