From 5e33962ee153747818f4c9ee85684dd0ff47faab Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Thu, 22 Feb 2024 00:15:36 -0300 Subject: [PATCH] Complete remove destructor from DB drivers Also moved the DB Driver interface --- .../Databases/Api/DriverInterface.php} | 17 ++++--- system/database/Databases/MSSQL.php | 19 +++----- system/database/Databases/MySQL_PDO.php | 47 +++++++------------ system/database/Databases/MySQL_legacy.php | 21 ++------- system/database/Databases/MySQLi.php | 4 +- system/database/Databases/Oracle.php | 6 +-- system/database/Databases/Oracle_PDO.php | 9 +--- system/database/Databases/Postgre.php | 33 +++---------- system/database/Databases/SQLSRV.php | 36 ++++---------- system/database/Databases/SQLite3.php | 11 +---- system/database/Databases/mPDO.php | 13 ++--- system/database/Databases/nullStatement.php | 8 +--- system/database/Databases/sqlsrvPDO.php | 40 ++++------------ system/database/autoload.php | 2 +- 14 files changed, 79 insertions(+), 187 deletions(-) rename system/{engine/interfaces/databases.php => database/Databases/Api/DriverInterface.php} (90%) diff --git a/system/engine/interfaces/databases.php b/system/database/Databases/Api/DriverInterface.php similarity index 90% rename from system/engine/interfaces/databases.php rename to system/database/Databases/Api/DriverInterface.php index 7684108..e500f7e 100644 --- a/system/engine/interfaces/databases.php +++ b/system/database/Databases/Api/DriverInterface.php @@ -1,15 +1,20 @@ 0, @@ -68,12 +73,6 @@ * @return bool */ public function isConnected(); - /** - * Destroy the connection - * - * @return void */ - public function __destruct(); - /** * Execute a prepared statement with parameters * diff --git a/system/database/Databases/MSSQL.php b/system/database/Databases/MSSQL.php index d653808..e597e2d 100644 --- a/system/database/Databases/MSSQL.php +++ b/system/database/Databases/MSSQL.php @@ -15,7 +15,7 @@ namespace Phacil\Framework\Databases; * @deprecated 2.0.0 * @see \Phacil\Framework\Databases\sqlsrvPDO * @package Phacil\Framework\Databases */ -class MSSQL implements \Phacil\Framework\Interfaces\Databases +class MSSQL implements \Phacil\Framework\Databases\Api\DriverInterface { const DB_TYPE = 'Microsoft SQL Server Database'; @@ -24,6 +24,7 @@ class MSSQL implements \Phacil\Framework\Interfaces\Databases private $connection; + /** @inheritdoc */ public function __construct($hostname, $username, $password, $database, $port = '1443', $charset = 'utf8') { if (!$this->connection = \mssql_connect($hostname, $username, $password)) { @@ -39,6 +40,7 @@ class MSSQL implements \Phacil\Framework\Interfaces\Databases \mssql_query("SET CHARACTER_SET_CONNECTION=utf8", $this->connection); } + /** @inheritdoc */ public function query($sql) { $resource = \mssql_query($sql, $this->connection); @@ -97,23 +99,14 @@ class MSSQL implements \Phacil\Framework\Interfaces\Databases return $last_id; } + /** @inheritdoc */ function isConnected() { + return !empty($this->connection); } - public function __destruct() - { - \mssql_close($this->connection); - } - /** - * 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 - */ + /** @inheritdoc */ public function execute($sql, array $params = []) { $sql = str_replace(array_keys($params), array_values($params), $sql); diff --git a/system/database/Databases/MySQL_PDO.php b/system/database/Databases/MySQL_PDO.php index 03ef952..46a7410 100644 --- a/system/database/Databases/MySQL_PDO.php +++ b/system/database/Databases/MySQL_PDO.php @@ -8,10 +8,10 @@ namespace Phacil\Framework\Databases; -use Phacil\Framework\Interfaces\Databases; +use Phacil\Framework\Databases\Api\DriverInterface as DatabasesDriver; /** @package Phacil\Framework\Databases */ -class MySQL_PDO implements Databases +class MySQL_PDO implements DatabasesDriver { const DB_TYPE = 'MySQL'; @@ -24,6 +24,7 @@ class MySQL_PDO implements Databases * @var \PDO */ private $dbh; + /** * List of connection settings * @@ -32,26 +33,23 @@ class MySQL_PDO implements Databases private $options = array( 'PDO::ATTR_ERRMODE' => \PDO::ERRMODE_SILENT ); + /** * The number of rows affected by the last operation * * @var int */ private $affectedRows = 0; + /** * The data for the database connection * * @var \stdClass */ private $params = array(); + /** - * Sets the connection and connects to the database - * - * @param string $host server Address - * @param string $user Username - * @param string $pass Password - * @param string $name The database name - * @param string $charset Encoding connection + * @inheritdoc */ public function __construct($host, $user, $pass, $name, $port = '3306', $charset = 'utf8mb4') { @@ -68,6 +66,7 @@ class MySQL_PDO implements Databases $this->connect(); } + /** @inheritdoc */ public function isConnected() { if ($this->dbh) { return true; @@ -91,10 +90,7 @@ class MySQL_PDO implements Databases } /** - * Query the database - * @param string $sql - * @return \Phacil\Framework\Databases\Object\ResultInterface|true - * @throws \PDOException + * @inheritdoc */ public function query($sql) { @@ -110,6 +106,7 @@ class MySQL_PDO implements Databases } return null; } + /** * Concludes the string in quotation marks to be used in the query * @@ -120,24 +117,23 @@ class MySQL_PDO implements Databases { return $this->dbh ? str_replace("'", "", $this->dbh->quote($string)) : null; } + /** - * Gets the number of rows affected by the last operation - * - * @return int + * {@inheritdoc} */ public function countAffected() { return $this->affectedRows; } + /** - * Gets the ID of the last inserted row or sequence of values - * - * @return int + * @inheritdoc */ public function getLastId() { return $this->dbh ? $this->dbh->lastInsertId() : 0; } + /** * Gets the name of the driver * @@ -147,6 +143,7 @@ class MySQL_PDO implements Databases { return $this->dbh ? $this->dbh->getAttribute(\PDO::ATTR_DRIVER_NAME) : null; } + /** * Get information about the version of the client libraries that are used by the PDO driver * @@ -156,6 +153,7 @@ class MySQL_PDO implements Databases { return $this->dbh ? $this->dbh->getAttribute(\PDO::ATTR_CLIENT_VERSION) : null; } + /** * Closing a database connection */ @@ -163,18 +161,9 @@ class MySQL_PDO implements Databases { $this->dbh = null; } - public function __destruct() - { - $this->close(); - } /** - * 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 + * @inheritdoc */ public function execute($sql, array $params = []) { diff --git a/system/database/Databases/MySQL_legacy.php b/system/database/Databases/MySQL_legacy.php index 7ccf476..7585cfc 100644 --- a/system/database/Databases/MySQL_legacy.php +++ b/system/database/Databases/MySQL_legacy.php @@ -16,7 +16,7 @@ namespace Phacil\Framework\Databases; * @deprecated 2.0.0 Use MySQLi class driver instead. * @see \Phacil\Framework\Databases\MySQLi * */ -class MySQL_legacy implements \Phacil\Framework\Interfaces\Databases { +class MySQL_legacy implements \Phacil\Framework\Databases\Api\DriverInterface { const DB_TYPE = 'MySQL'; @@ -24,6 +24,7 @@ class MySQL_legacy implements \Phacil\Framework\Interfaces\Databases { private $connection; + /** {@inheritdoc} */ public function __construct($hostname, $username, $password, $database, $port = '3306', $charset = 'utf8') { if (!$this->connection = \mysql_connect($hostname, $username, $password)) { throw new \Phacil\Framework\Exception('Error: Could not make a database connection using ' . $username . '@' . $hostname); @@ -42,10 +43,7 @@ class MySQL_legacy implements \Phacil\Framework\Interfaces\Databases { public function isConnected() { } /** - * - * @param string $sql - * @return \Phacil\Framework\Databases\Object\ResultInterface|true - * @throws \Phacil\Framework\Exception + * {@inheritdoc} */ public function query($sql) { $resource = \mysql_query($sql, $this->connection); @@ -91,19 +89,10 @@ class MySQL_legacy implements \Phacil\Framework\Interfaces\Databases { public function getLastId() { return \mysql_insert_id($this->connection); - } - - public function __destruct() { - \mysql_close($this->connection); - } + } /** - * 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 + * @inheritdoc */ public function execute($sql, array $params = []) { diff --git a/system/database/Databases/MySQLi.php b/system/database/Databases/MySQLi.php index 6129c47..c74b076 100644 --- a/system/database/Databases/MySQLi.php +++ b/system/database/Databases/MySQLi.php @@ -9,7 +9,7 @@ namespace Phacil\Framework\Databases; use MySQLi as GlobalMysqli; -use Phacil\Framework\Interfaces\Databases; +use Phacil\Framework\Databases\Api\DriverInterface; /** * Default driver to connect a MySQL/MariaDB databases. @@ -17,7 +17,7 @@ use Phacil\Framework\Interfaces\Databases; * Works on most of PHP instalations * * @package Phacil\Framework\Databases */ -class MySQLi implements Databases { +class MySQLi implements DriverInterface { /** * * @var GlobalMysqli diff --git a/system/database/Databases/Oracle.php b/system/database/Databases/Oracle.php index 6a5950f..b0d0f1b 100644 --- a/system/database/Databases/Oracle.php +++ b/system/database/Databases/Oracle.php @@ -8,7 +8,7 @@ namespace Phacil\Framework\Databases; -use Phacil\Framework\Interfaces\Databases; +use Phacil\Framework\Databases\Api\DriverInterface as Databases; /** * Oracle driver connector @@ -95,10 +95,6 @@ class Oracle implements Databases { public function isConnected() { return $this->connection; } - - public function __destruct() { - \oci_close($this->connection); - } /** * Execute a prepared statement with parameters diff --git a/system/database/Databases/Oracle_PDO.php b/system/database/Databases/Oracle_PDO.php index e1d11e5..de2c5db 100644 --- a/system/database/Databases/Oracle_PDO.php +++ b/system/database/Databases/Oracle_PDO.php @@ -9,13 +9,13 @@ namespace Phacil\Framework\Databases; use PDO; -use Phacil\Framework\Interfaces\Databases; +use Phacil\Framework\Databases\Api\DriverInterface; /** * Alternative PDO Oracle connection method. * * @package Phacil\Framework\Databases */ -class Oracle_PDO implements Databases +class Oracle_PDO implements DriverInterface { const DB_TYPE = 'Oracle'; @@ -146,11 +146,6 @@ class Oracle_PDO implements Databases } } - public function __destruct() - { - unset($this->connection); - } - /** * Execute a prepared statement with parameters * diff --git a/system/database/Databases/Postgre.php b/system/database/Databases/Postgre.php index 0f3c761..6d5489a 100644 --- a/system/database/Databases/Postgre.php +++ b/system/database/Databases/Postgre.php @@ -8,9 +8,10 @@ namespace Phacil\Framework\Databases; -use Phacil\Framework\Interfaces\Databases; +use Phacil\Framework\Databases\Api\DriverInterface; -class Postgre implements Databases { +/** @package Phacil\Framework\Databases */ +class Postgre implements DriverInterface { const DB_TYPE = 'Postgre'; @@ -23,15 +24,7 @@ class Postgre implements Databases { private $link; /** - * - * @param string $hostname - * @param string $username - * @param string $password - * @param string $database - * @param string $port - * @param string $charset - * @return void - * @throws \Phacil\Framework\Exception + * @inheritdoc */ 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)) { @@ -93,27 +86,15 @@ class Postgre implements Databases { } /** - * @return mixed - * @throws Exception + * @inheritdoc */ public function getLastId() { $query = $this->query("SELECT LASTVAL() AS `id`"); - return $query->row['id']; - } - - /** @return void */ - public function __destruct() { - pg_close($this->link); + return $query->getRow()->getValue('id'); } - /** - * 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 + * {@inheritdoc} */ public function execute($sql, array $params = []) { diff --git a/system/database/Databases/SQLSRV.php b/system/database/Databases/SQLSRV.php index 8089d59..52b5f36 100644 --- a/system/database/Databases/SQLSRV.php +++ b/system/database/Databases/SQLSRV.php @@ -8,9 +8,10 @@ namespace Phacil\Framework\Databases; -use Phacil\Framework\Interfaces\Databases; +use Phacil\Framework\Databases\Api\DriverInterface; -class SQLSRV implements Databases { +/** @package Phacil\Framework\Databases */ +class SQLSRV implements DriverInterface { const DB_TYPE = 'Microsoft SQL Server Database'; @@ -23,13 +24,7 @@ class SQLSRV implements Databases { private $link; /** - * @param string $hostname - * @param string $username - * @param string $password - * @param string $database - * @param string $port - * @param string $charset - * @return void + * @inheritdoc */ public function __construct($hostname, $username, $password, $database, $port = '1443', $charset = 'utf8') { /* @@ -61,9 +56,7 @@ class SQLSRV implements Databases { /** * - * @param string $sql - * @return \Phacil\Framework\Databases\Object\ResultInterface|true - * @throws \Phacil\Framework\Exception + * @inheritdoc */ public function query($sql) { $resource = \sqlsrv_query($this->link, $sql); @@ -98,8 +91,7 @@ class SQLSRV implements Databases { } /** - * @param string $value - * @return string + * @inheritdoc */ public function escape($value) { $unpacked = unpack('H*hex', $value); @@ -107,12 +99,12 @@ class SQLSRV implements Databases { return '0x' . $unpacked['hex']; } - /** @return int */ + /** @inheritdoc */ public function countAffected() { return \sqlsrv_rows_affected($this->link); } - /** @return false|int */ + /** @inheritdoc */ public function getLastId() { $last_id = false; @@ -127,18 +119,8 @@ class SQLSRV implements Databases { return $last_id; } - /** @return void */ - public function __destruct() { - \sqlsrv_close($this->link); - } - /** - * 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 + * {@inheritdoc} */ public function execute($sql, array $params = []) { diff --git a/system/database/Databases/SQLite3.php b/system/database/Databases/SQLite3.php index facdaf3..4979f04 100644 --- a/system/database/Databases/SQLite3.php +++ b/system/database/Databases/SQLite3.php @@ -8,11 +8,10 @@ namespace Phacil\Framework\Databases; -use Phacil\Framework\Interfaces\Databases; +use Phacil\Framework\Databases\Api\DriverInterface; use \SQLite3 as nativeSQLite3; -use \stdClass; -class SQLite3 implements Databases { +class SQLite3 implements DriverInterface { const DB_TYPE = 'SQLite3'; @@ -102,12 +101,6 @@ class SQLite3 implements Databases { return ($this->connection) ? true : false; } - /** @return void */ - public function __destruct() { - $this->connection->close(); - } - - /** * Execute a prepared statement with parameters * diff --git a/system/database/Databases/mPDO.php b/system/database/Databases/mPDO.php index e22e26a..2731c0a 100644 --- a/system/database/Databases/mPDO.php +++ b/system/database/Databases/mPDO.php @@ -9,13 +9,13 @@ namespace Phacil\Framework\Databases; use PDO; -use Phacil\Framework\Interfaces\Databases; +use Phacil\Framework\Databases\Api\DriverInterface; /** * Alternative PDO MySQL connection method. * * @package Phacil\Framework\Databases */ -class mPDO implements Databases { +class mPDO implements DriverInterface { const DB_TYPE = 'MySQL'; @@ -39,6 +39,7 @@ class mPDO implements Databases { */ private $statement = null; + /** @inheritdoc */ public function __construct($hostname, $username, $password, $database, $port = '3306', $charset = 'UTF8') { try { $dsn = "mysql:host={$hostname};port={$port};dbname={$database};charset={$charset}"; @@ -67,6 +68,7 @@ class mPDO implements Databases { } } + /** {@inheritdoc} */ public function query($sql, $params = array()) { $this->statement = $this->connection->prepare($sql); @@ -117,6 +119,9 @@ class mPDO implements Databases { return $this->connection->lastInsertId(); } + /** + * {@inheritdoc} + */ public function isConnected() { if ($this->connection) { return true; @@ -124,10 +129,6 @@ class mPDO implements Databases { return false; } } - - public function __destruct() { - unset($this->connection); - } /** * Execute a prepared statement with parameters diff --git a/system/database/Databases/nullStatement.php b/system/database/Databases/nullStatement.php index bed8a58..e172c6a 100644 --- a/system/database/Databases/nullStatement.php +++ b/system/database/Databases/nullStatement.php @@ -8,14 +8,14 @@ namespace Phacil\Framework\Databases; -use Phacil\Framework\Interfaces\Databases; +use Phacil\Framework\Databases\Api\DriverInterface; /** * Nullable fake simulated DB connection. * * @package Phacil\Framework\Databases */ -final class nullStatement implements Databases { +final class nullStatement implements DriverInterface { //private $connection; const DB_TYPE = NULL; @@ -72,10 +72,6 @@ final class nullStatement implements Databases { return NULL; } - public function __destruct() { - return NULL; - } - /** * Execute a prepared statement with parameters * diff --git a/system/database/Databases/sqlsrvPDO.php b/system/database/Databases/sqlsrvPDO.php index 86a87dc..1e877b1 100644 --- a/system/database/Databases/sqlsrvPDO.php +++ b/system/database/Databases/sqlsrvPDO.php @@ -9,9 +9,9 @@ namespace Phacil\Framework\Databases; use \PDO as PDONative; -use Phacil\Framework\Interfaces\Databases; +use Phacil\Framework\Databases\Api\DriverInterface; -class sqlsrvPDO implements Databases { +class sqlsrvPDO implements DriverInterface { const DB_TYPE = 'Microsoft SQL Server Database'; @@ -36,15 +36,7 @@ class sqlsrvPDO implements Databases { private $affectedRows = 0; /** - * - * @param string $hostname - * @param string $username - * @param string $password - * @param string $database - * @param string $port - * @param string $charset - * @return void - * @throws \Phacil\Framework\Exception + * {@inheritdoc} */ public function __construct($hostname, $username, $password, $database, $port = '3306', $charset = 'utf8') { try { @@ -81,10 +73,7 @@ class sqlsrvPDO implements Databases { /** * - * @param string $sql - * @param array $params - * @return \Phacil\Framework\Databases\Object\ResultInterface|true - * @throws \Phacil\Framework\Exception + * @inheritdoc */ public function query($sql, $params = array()) { $this->statement = $this->connection->prepare($sql); @@ -119,14 +108,13 @@ class sqlsrvPDO implements Databases { } /** - * @param string $value - * @return string + * @inheritdoc */ public function escape($value) { return str_replace(array("\\", "\0", "\n", "\r", "\x1a", "'", '"'), array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"'), $value); } - /** @return int */ + /** @inheritdoc */ public function countAffected() { if ($this->statement) { return $this->statement->rowCount(); @@ -135,12 +123,12 @@ class sqlsrvPDO implements Databases { } } - /** @return string */ + /** @inheritdoc */ public function getLastId() { return $this->connection->lastInsertId(); } - /** @return bool */ + /** @inheritdoc */ public function isConnected() { if ($this->connection) { return true; @@ -149,18 +137,8 @@ class sqlsrvPDO implements Databases { } } - /** @return void */ - public function __destruct() { - unset($this->connection); - } - /** - * 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 + * @inheritdoc */ public function execute($sql, array $params = []) { diff --git a/system/database/autoload.php b/system/database/autoload.php index f4b117c..bea2f7d 100644 --- a/system/database/autoload.php +++ b/system/database/autoload.php @@ -9,7 +9,7 @@ namespace Phacil\Framework; -use Phacil\Framework\Interfaces\Databases as DatabaseInterface; +use Phacil\Framework\Databases\Api\DriverInterface as DatabaseInterface; use Phacil\Framework\Config; use Phacil\Framework\Api\Database as DatabaseApi;