From 4ac456f86ef38065bf2d23b9f1bfd2961806bc26 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Sat, 1 Jun 2024 23:04:25 -0300 Subject: [PATCH] ORDS url fix, check table exists for Oracle --- system/MagiQL/MagiQL.php | 71 ++++++++++++------- system/config/autoload.php | 39 +++++++++- .../Connectors/Oracle/ORDS/Model/Query.php | 8 +++ .../database/Databases/Driver/OracleORDS.php | 2 +- system/system.php | 17 ++--- system/url/seo_url.php | 28 +++++--- 6 files changed, 113 insertions(+), 52 deletions(-) diff --git a/system/MagiQL/MagiQL.php b/system/MagiQL/MagiQL.php index 336e84b..cd86fb5 100644 --- a/system/MagiQL/MagiQL.php +++ b/system/MagiQL/MagiQL.php @@ -65,33 +65,54 @@ class MagiQL extends AbstractBuilder { * @throws \Phacil\Framework\Exception */ public function isTableExists($tableName) { - if($this->db->getDBTypeId() === DatabaseDriverInterface::LIST_DB_TYPE_ID['MYSQL']){ - $sql = 'SELECT (1) AS tbl_exists FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = :v1 AND TABLE_SCHEMA = :v2'; - $result = $this->db->execute($sql, [ - ':v1' => $tableName, - ':v2' => \Phacil\Framework\Config::DB_DATABASE() - ]); - } - if($this->db->getDBTypeId() === DatabaseDriverInterface::LIST_DB_TYPE_ID['MSSQL']){ - $sql = "SELECT OBJECT_ID(:v1, 'U') AS table_id"; - $result = $this->db->execute($sql, [ - ':v1' => $tableName - ]); - } - if($this->db->getDBTypeId() === DatabaseDriverInterface::LIST_DB_TYPE_ID['POSTGRE']){ - $sql = "SELECT 1 FROM information_schema.tables WHERE table_name = :v1"; - $result = $this->db->execute($sql, [ - ':v1' => $tableName - ]); - } - if($this->db->getDBTypeId() === DatabaseDriverInterface::LIST_DB_TYPE_ID['SQLLITE3']){ - $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name=:v1"; - $result = $this->db->execute($sql, [ - ':v1' => $tableName - ]); + switch ($this->db->getDBTypeId()) { + case DatabaseDriverInterface::LIST_DB_TYPE_ID['MYSQL']: + $sql = 'SELECT (1) AS tbl_exists FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = :v1 AND TABLE_SCHEMA = :v2'; + $result = $this->db->execute($sql, [ + ':v1' => $tableName, + ':v2' => \Phacil\Framework\Config::DB_DATABASE() + ]); + break; + + case DatabaseDriverInterface::LIST_DB_TYPE_ID['MSSQL']: + $sql = "SELECT OBJECT_ID(:v1, 'U') AS table_id"; + $result = $this->db->execute($sql, [ + ':v1' => $tableName + ]); + break; + + case DatabaseDriverInterface::LIST_DB_TYPE_ID['POSTGRE']: + $sql = "SELECT 1 FROM information_schema.tables WHERE table_name = :v1"; + $result = $this->db->execute($sql, [ + ':v1' => $tableName + ]); + break; + + case DatabaseDriverInterface::LIST_DB_TYPE_ID['SQLLITE3']: + $sql = "SELECT 1 FROM information_schema.tables WHERE table_name = :v1"; + $result = $this->db->execute($sql, [ + ':v1' => $tableName + ]); + break; + + case DatabaseDriverInterface::LIST_DB_TYPE_ID['ORACLE']: + $sql = "SELECT table_name FROM all_tables WHERE table_name = :v1 AND owner = :v2"; + $result = $this->db->execute($sql, [ + ':v1' => $tableName, + ':v2' => \Phacil\Framework\Config::DB_DATABASE() + ]); + break; + + default: + $sql = 'SELECT (1) AS tbl_exists FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = :v1 AND TABLE_SCHEMA = :v2'; + $result = $this->db->execute($sql, [ + ':v1' => $tableName, + ':v2' => \Phacil\Framework\Config::DB_DATABASE() + ]); + break; } - if($result && $result->getNumRows() > 0){ + if($result && $result instanceof \Phacil\Framework\Databases\Api\Object\ResultInterface && $result->getNumRows() > 0){ return true; } diff --git a/system/config/autoload.php b/system/config/autoload.php index 2b2f4cf..20b1632 100644 --- a/system/config/autoload.php +++ b/system/config/autoload.php @@ -10,19 +10,54 @@ namespace Phacil\Framework; /** @package Phacil\Framework */ class Config { - + + /** + * @var \Phacil\Framework\startEngineExacTI + */ + private $engine; + /** * @var array */ private $data = array(); + public function __construct( + \Phacil\Framework\startEngineExacTI $engine + ) { + $this->engine = $engine; + } + + /** + * @return void + * @throws \Phacil\Framework\Exception + */ + public function initialize() { + if (\Phacil\Framework\Config::USE_DB_CONFIG() === true) { + if(!$this->engine->db->query()->isTableExists('settings')){ + $erro = new \Phacil\Framework\Exception\NotFoundException(sprintf("The table '%s' does not exist", 'settings'), 3); + return; + } + $query = $this->engine->db->query()->select()->from('settings')->orderBy('setting_id', \Phacil\Framework\MagiQL\Api\Syntax\OrderBy::ASC)->load(); + + if($query instanceof \Phacil\Framework\Databases\Api\Object\ResultInterface){ + foreach ($query as $setting) { + if (!$setting['serialized']) { + $this->set($setting['key'], $setting['value']); + } else { + $this->set($setting['key'], unserialize($setting['value'])); + } + } + } + } + } + /** * @param string|int $key * @return null|array|string */ public function get($key) { return ($this->has($key) ? $this->data[$key] : null); - } + } /** * @param string $key diff --git a/system/database/Databases/Connectors/Oracle/ORDS/Model/Query.php b/system/database/Databases/Connectors/Oracle/ORDS/Model/Query.php index 88926db..ebd860d 100644 --- a/system/database/Databases/Connectors/Oracle/ORDS/Model/Query.php +++ b/system/database/Databases/Connectors/Oracle/ORDS/Model/Query.php @@ -131,6 +131,14 @@ class Query implements QueryApi { $this->num_rows = $resultEnd["result"]; } + if(isset($executed["code"]) && isset($executed["message"])){ + throw new \Phacil\Framework\Exception\RuntimeException($executed["message"]); + } + + if(isset($resultEnd["errorCode"]) && $resultEnd["errorCode"] > 0){ + throw new \Phacil\Framework\Exception\RuntimeException($resultEnd["errorMessage"], $resultEnd["errorCode"]); + } + return $this; } else { throw new \Phacil\Framework\Exception\RuntimeException($this->handle->error()); diff --git a/system/database/Databases/Driver/OracleORDS.php b/system/database/Databases/Driver/OracleORDS.php index 7c06c7c..24c8c8b 100644 --- a/system/database/Databases/Driver/OracleORDS.php +++ b/system/database/Databases/Driver/OracleORDS.php @@ -58,7 +58,7 @@ class OracleORDS implements DriverInterface try { /** @var \Phacil\Framework\Databases\Connectors\Oracle\ORDS\Connector */ $this->connection = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Connectors\Oracle\ORDS\Connector::class, [ - $hostname.$database, + $hostname.strtolower($database), \Phacil\Framework\Config::DB_PORT() ?: $port, $username, $password diff --git a/system/system.php b/system/system.php index c0d7472..2ea964e 100644 --- a/system/system.php +++ b/system/system.php @@ -482,23 +482,14 @@ try { ]); // Settings - if (!empty($configs)) { - foreach ($configs as $key => $confValue) { - $engine->config->set($key, $confValue); + if (!empty($configs) && is_array($configs)) { + foreach ($configs as $key => $config_value) { + $engine->config->set($key, $config_value); } } if (\Phacil\Framework\Config::USE_DB_CONFIG() === true) { - - $query = (\Phacil\Framework\Config::CUSTOM_DB_CONFIG()) ? $engine->db->query(\Phacil\Framework\Config::CUSTOM_DB_CONFIG()) : $engine->db->query()->select()->from('settings')->orderBy('setting_id', \Phacil\Framework\MagiQL\Api\Syntax\OrderBy::ASC)->load(); - - foreach ($query as $setting) { - if (!$setting['serialized']) { - $engine->config->set($setting['key'], $setting['value']); - } else { - $engine->config->set($setting['key'], unserialize($setting['value'])); - } - } + $engine->config->initialize(); } diff --git a/system/url/seo_url.php b/system/url/seo_url.php index 307ee2a..51d1623 100644 --- a/system/url/seo_url.php +++ b/system/url/seo_url.php @@ -16,11 +16,15 @@ use Phacil\Framework\Request; class SystemUrlSeoUrl extends ControllerController { /** - * * @var string */ private $notfound = 'error/not_found'; + /** + * @var bool + */ + private $tableUrlAliasExists = false; + /** * * @var string[] @@ -35,12 +39,10 @@ class SystemUrlSeoUrl extends ControllerController { * @param Registry $registry * @return void */ - public function __construct(\Phacil\Framework\Registry $registry) + public function __construct() { - parent::__construct($registry); - $this->notfound = Config::NOT_FOUND() ?: $this->notfound; - + $this->tableUrlAliasExists = Config::USE_DB_CONFIG() ? $this->db->query()->isTableExists("url_alias") : false; } /** @@ -62,9 +64,11 @@ class SystemUrlSeoUrl extends ControllerController { foreach ($parts as $part) { if(Config::USE_DB_CONFIG()){ - $query = $this->db->query()->select()->setTable("url_alias"); - $query->where()->eq('keyword', $part); - $query = $query->load(); + if($this->tableUrlAliasExists){ + $query = $this->db->query()->select()->setTable("url_alias"); + $query->where()->eq('keyword', $part); + $query = $query->load(); + } } if (isset($query) && $query != false && $query->getNumRows() === 1) { @@ -156,9 +160,11 @@ class SystemUrlSeoUrl extends ControllerController { if (isset($data['route'])) { if(Config::USE_DB_CONFIG()){ - $query = $this->db->query()->select()->from("url_alias"); - $query->where()->eq('query', $value)->end(); - $query = $query->load(); + if ($this->tableUrlAliasExists) { + $query = $this->db->query()->select()->from("url_alias"); + $query->where()->eq('query', $value)->end(); + $query = $query->load(); + } } if (isset($query) && $query->getNumRows() > 0) {