From 56d0d5687eae35f708787a6d1fb1c7c96dc4d9fc Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Mon, 18 Sep 2023 00:51:04 -0300 Subject: [PATCH] Fix database interface and exceptions --- system/database/databases/mpdo.php | 6 +++--- system/database/databases/mssql.php | 4 ++-- system/database/databases/mysql_legacy.php | 4 ++-- system/database/databases/mysql_pdo.php | 2 +- system/database/databases/mysqli.php | 14 +++++++------- system/database/databases/postgre.php | 10 +++++----- system/database/databases/sqlite3_db.php | 17 ++++++++++++++--- system/database/databases/sqlsrv.php | 3 +-- system/database/databases/sqlsrvpdo.php | 17 ++++++++++++++--- system/engine/interfaces/common/registers.php | 2 +- system/engine/interfaces/databases.php | 2 +- 11 files changed, 51 insertions(+), 30 deletions(-) diff --git a/system/database/databases/mpdo.php b/system/database/databases/mpdo.php index f48089a..1d5c715 100644 --- a/system/database/databases/mpdo.php +++ b/system/database/databases/mpdo.php @@ -43,7 +43,7 @@ final class mPDO implements Databases { $this->connection = new \PDO($dsn, $username, $password, $options); } catch(\PDOException $e) { - throw new \Exception('Failed to connect to database. Reason: \'' . $e->getMessage() . '\''); + throw new \Phacil\Framework\Exception('Failed to connect to database. Reason: \'' . $e->getMessage() . '\''); } $this->connection->exec("SET NAMES 'utf8'"); $this->connection->exec("SET CHARACTER SET utf8"); @@ -74,7 +74,7 @@ final class mPDO implements Databases { $result->num_rows = $this->statement->rowCount(); } } catch(\PDOException $e) { - throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode()); + throw new \Phacil\Framework\Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode()); } } public function query($sql, $params = array()) { @@ -101,7 +101,7 @@ final class mPDO implements Databases { $result->num_rows = $this->rowCount; } } catch (\PDOException $e) { - throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . '
' . $sql); + throw new \Phacil\Framework\Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . '
' . $sql); } if ($result) { return $result; diff --git a/system/database/databases/mssql.php b/system/database/databases/mssql.php index 3519f02..5e800a0 100644 --- a/system/database/databases/mssql.php +++ b/system/database/databases/mssql.php @@ -20,11 +20,11 @@ final class MSSQL implements \Phacil\Framework\Interfaces\Databases public function __construct($hostname, $username, $password, $database, $port = '1443', $charset = 'utf8') { if (!$this->connection = mssql_connect($hostname, $username, $password)) { - exit('Error: Could not make a database connection using ' . $username . '@' . $hostname); + throw new \Phacil\Framework\Exception('Error: Could not make a database connection using ' . $username . '@' . $hostname); } if (!mssql_select_db($database, $this->connection)) { - exit('Error: Could not connect to database ' . $database); + throw new \Phacil\Framework\Exception('Error: Could not connect to database ' . $database); } mssql_query("SET NAMES 'utf8'", $this->connection); diff --git a/system/database/databases/mysql_legacy.php b/system/database/databases/mysql_legacy.php index 24d5f4f..95db79e 100644 --- a/system/database/databases/mysql_legacy.php +++ b/system/database/databases/mysql_legacy.php @@ -19,11 +19,11 @@ final class MySQL_legacy implements \Phacil\Framework\Interfaces\Databases { public function __construct($hostname, $username, $password, $database, $port = '3306', $charset = 'utf8') { if (!$this->connection = \mysql_connect($hostname, $username, $password)) { - exit('Error: Could not make a database connection using ' . $username . '@' . $hostname); + throw new \Phacil\Framework\Exception('Error: Could not make a database connection using ' . $username . '@' . $hostname); } if (!\mysql_select_db($database, $this->connection)) { - exit('Error: Could not connect to database ' . $database); + throw new \Phacil\Framework\Exception('Error: Could not connect to database ' . $database); } \mysql_query("SET NAMES '".$charset."'", $this->connection); diff --git a/system/database/databases/mysql_pdo.php b/system/database/databases/mysql_pdo.php index 1a9f9c1..ff413e5 100644 --- a/system/database/databases/mysql_pdo.php +++ b/system/database/databases/mysql_pdo.php @@ -90,7 +90,7 @@ final class MYSQL_PDO implements Databases * @return \Phacil\Framework\Databases\Object\ResultInterface|true * @throws \PDOException */ - public function query($sql = null) + public function query($sql) { if ($this->dbh) { $data = new \Phacil\Framework\Databases\Object\Result(); diff --git a/system/database/databases/mysqli.php b/system/database/databases/mysqli.php index f101f97..691a8b5 100644 --- a/system/database/databases/mysqli.php +++ b/system/database/databases/mysqli.php @@ -33,33 +33,33 @@ class MySQLi implements Databases { * @param string $port * @param string $charset * @return void - * @throws Exception + * @throws \Phacil\Framework\Exception */ public function __construct($hostname, $username, $password, $database, $port = '3306', $charset = 'utf8mb4') { try { $this->connection = @new \MySQLi($hostname, $username, $password, $database, $port); } catch (\mysqli_sql_exception $e) { - throw new \Exception('Error: ' . $this->connection->error . '
Error No: ' . $this->connection->errno); + throw new \Phacil\Framework\Exception('Error: ' . $this->connection->error . '
Error No: ' . $this->connection->errno); } if (!$this->connection->connect_errno) { if(isset($this->connection->report_mode)) $this->connection->report_mode = MYSQLI_REPORT_ERROR; - + $this->connection->set_charset($charset); //$this->connection->query("SET SESSION sql_mode = 'NO_ZERO_IN_DATE,NO_ENGINE_SUBSTITUTION'"); $this->connection->query("SET SQL_MODE = ''"); } else { - throw new \Exception('Error: ' . $this->connection->error . '
Error No: ' . $this->connection->errno); + throw new \Phacil\Framework\Exception('Error: ' . $this->connection->error . '
Error No: ' . $this->connection->errno); } } - + /** * Execute the SQl Query * @param string $sql * @return \Phacil\Framework\Databases\Object\ResultInterface|true - * @throws \Exception + * @throws \Phacil\Framework\Exception */ public function query($sql) { $query = $this->connection->query($sql); @@ -79,7 +79,7 @@ class MySQLi implements Databases { return true; } } else { - throw new \Exception('Error: ' . $this->connection->error . '
Error No: ' . $this->connection->errno . '
' . $sql); + throw new \Phacil\Framework\Exception('Error: ' . $this->connection->error . '
Error No: ' . $this->connection->errno . '
' . $sql); } } diff --git a/system/database/databases/postgre.php b/system/database/databases/postgre.php index 626fb37..62f386c 100644 --- a/system/database/databases/postgre.php +++ b/system/database/databases/postgre.php @@ -26,14 +26,14 @@ final class Postgre implements Databases { * @param string $port * @param string $charset * @return void - * @throws Exception + * @throws \Phacil\Framework\Exception */ public function __construct($hostname, $username, $password, $database, $port = '5432', $charset = 'UTF8') { if (!$this->link = pg_connect('host=' . $hostname . ' port=' . $port . ' user=' . $username . ' password=' . $password . ' dbname=' . $database)) { - throw new \Exception('Error: Could not make a database link using ' . $username . '@' . $hostname); + throw new \Phacil\Framework\Exception('Error: Could not make a database link using ' . $username . '@' . $hostname); } if (!pg_ping($this->link)) { - throw new \Exception('Error: Could not connect to database ' . $database); + throw new \Phacil\Framework\Exception('Error: Could not connect to database ' . $database); } pg_query($this->link, "SET CLIENT_ENCODING TO '".$charset."'"); } @@ -46,7 +46,7 @@ final class Postgre implements Databases { * * @param string $sql * @return \Phacil\Framework\Databases\Object\ResultInterface|true - * @throws \Exception + * @throws \Phacil\Framework\Exception */ public function query($sql) { $resource = pg_query($this->link, $sql); @@ -69,7 +69,7 @@ final class Postgre implements Databases { return true; } } else { - throw new \Exception('Error: ' . pg_result_error($this->link) . '
' . $sql); + throw new \Phacil\Framework\Exception('Error: ' . pg_result_error($this->link) . '
' . $sql); } } diff --git a/system/database/databases/sqlite3_db.php b/system/database/databases/sqlite3_db.php index 785cae5..492c136 100644 --- a/system/database/databases/sqlite3_db.php +++ b/system/database/databases/sqlite3_db.php @@ -19,12 +19,23 @@ final class Sqlite3_db implements Databases { */ private $connection; + /** + * + * @param string $hostname + * @param string $username + * @param string $password + * @param string $database + * @param string $port + * @param string $charset + * @return void + * @throws \Phacil\Framework\Exception + */ public function __construct($hostname, $username = null, $password = null, $database, $port = '3306', $charset = 'utf8mb4') { $this->connection = new \SQLite3($hostname.$database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $password); if (!$this->connection) { - throw new \Exception('Error: ' . $this->connection->lastErrorMsg() . '
Error No: ' . $this->connection->lastErrorCode()); + throw new \Phacil\Framework\Exception('Error: ' . $this->connection->lastErrorMsg() . '
Error No: ' . $this->connection->lastErrorCode()); } } @@ -32,7 +43,7 @@ final class Sqlite3_db implements Databases { * * @param string $sql * @return \Phacil\Framework\Databases\Object\ResultInterface|true - * @throws \Exception + * @throws \Phacil\Framework\Exception */ public function query($sql){ //$query = $this->connection->query($sql); @@ -57,7 +68,7 @@ final class Sqlite3_db implements Databases { return $result; } else { - throw new \Exception('Error: ' . $this->connection->lastErrorMsg() . '
Error No: ' . $this->connection->lastErrorCode() . '
' . $sql); + throw new \Phacil\Framework\Exception('Error: ' . $this->connection->lastErrorMsg() . '
Error No: ' . $this->connection->lastErrorCode() . '
' . $sql); } } diff --git a/system/database/databases/sqlsrv.php b/system/database/databases/sqlsrv.php index 9efe9e8..5cde809 100644 --- a/system/database/databases/sqlsrv.php +++ b/system/database/databases/sqlsrv.php @@ -9,7 +9,6 @@ namespace Phacil\Framework\Databases; use Phacil\Framework\Interfaces\Databases; -use \stdClass; final class SQLSRV implements Databases { /** @@ -38,7 +37,7 @@ final class SQLSRV implements Databases { ); if (!$this->link = \sqlsrv_connect($hostname, $connectionInfo)) { - exit('Error: Could not make a database connection using ' . $username . '@' . $hostname); + throw new \Phacil\Framework\Exception('Error: Could not make a database connection using ' . $username . '@' . $hostname); } /* diff --git a/system/database/databases/sqlsrvpdo.php b/system/database/databases/sqlsrvpdo.php index e4ad363..280cb8a 100644 --- a/system/database/databases/sqlsrvpdo.php +++ b/system/database/databases/sqlsrvpdo.php @@ -24,11 +24,22 @@ final class sqlsrvPDO implements Databases { */ private $statement = null; + /** + * + * @param string $hostname + * @param string $username + * @param string $password + * @param string $database + * @param string $port + * @param string $charset + * @return void + * @throws \Phacil\Framework\Exception + */ public function __construct($hostname, $username, $password, $database, $port = '3306', $charset = 'utf8') { try { $this->connection = new PDONative("sqlsrv:Server=" . $hostname . ";Database=" . $database, $username, $password, array(\PDO::SQLSRV_ATTR_DIRECT_QUERY => true)); } catch(\PDOException $e) { - throw new \Exception('Failed to connect to database. Reason: \'' . $e->getMessage() . '\''); + throw new \Phacil\Framework\Exception('Failed to connect to database. Reason: \'' . $e->getMessage() . '\''); } @@ -85,7 +96,7 @@ final class sqlsrvPDO implements Databases { * @param string $sql * @param array $params * @return \Phacil\Framework\Databases\Object\ResultInterface|true - * @throws \Exception + * @throws \Phacil\Framework\Exception */ public function query($sql, $params = array()) { $this->statement = $this->connection->prepare($sql); @@ -106,7 +117,7 @@ final class sqlsrvPDO implements Databases { $result->setNumRows($this->statement->rowCount()); } } catch (\PDOException $e) { - throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . '
' . $sql); + throw new \Phacil\Framework\Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . '
' . $sql); } if ($result) { diff --git a/system/engine/interfaces/common/registers.php b/system/engine/interfaces/common/registers.php index a806b6f..00ec3b4 100644 --- a/system/engine/interfaces/common/registers.php +++ b/system/engine/interfaces/common/registers.php @@ -12,7 +12,7 @@ namespace Phacil\Framework\Interfaces\Common; /** * @since 2.0.0 - * @property \Phacil\Framework\Interfaces\Databases $db + * @property \Phacil\Framework\Database $db * @property \Phacil\Framework\Session $session * @property \Phacil\Framework\Request $request * @property \Phacil\Framework\Url $url diff --git a/system/engine/interfaces/databases.php b/system/engine/interfaces/databases.php index dc7883f..b52da9a 100644 --- a/system/engine/interfaces/databases.php +++ b/system/engine/interfaces/databases.php @@ -32,7 +32,7 @@ * @return \Phacil\Framework\Databases\Object\ResultInterface|\Phacil\Framework\MagiQL|bool * @throws Exception */ - public function query($sql = null); + public function query($sql); /** * Important escape to prevent SQL injection.