From 1bb2ba8d0e9ce904fc05a850d719b325e0414c31 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Sun, 28 Jan 2024 18:05:01 -0300 Subject: [PATCH] Added DB Types for drivers --- .../MagiQL/Manipulation/AbstractBaseQuery.php | 13 ++------- system/database/Databases/MSSQL.php | 22 ++++++++++++++ system/database/Databases/MySQL_PDO.php | 22 ++++++++++++++ system/database/Databases/MySQL_legacy.php | 23 +++++++++++++++ system/database/Databases/MySQLi.php | 27 ++++++++++++++++- system/database/Databases/Oracle.php | 19 ++++++++++++ system/database/Databases/Oracle_PDO.php | 25 +++++++++++++--- system/database/Databases/Postgre.php | 19 ++++++++++++ system/database/Databases/SQLSRV.php | 21 ++++++++++++++ system/database/Databases/SQLite3.php | 19 ++++++++++++ system/database/Databases/mPDO.php | 21 ++++++++++++++ system/database/Databases/nullStatement.php | 20 +++++++++++++ system/database/Databases/sqlsrvPDO.php | 21 ++++++++++++++ system/database/autoload.php | 22 ++++++++++++++ system/engine/interfaces/databases.php | 29 +++++++++++++++++++ 15 files changed, 308 insertions(+), 15 deletions(-) diff --git a/system/MagiQL/Manipulation/AbstractBaseQuery.php b/system/MagiQL/Manipulation/AbstractBaseQuery.php index f1a3234..5564b25 100644 --- a/system/MagiQL/Manipulation/AbstractBaseQuery.php +++ b/system/MagiQL/Manipulation/AbstractBaseQuery.php @@ -120,18 +120,11 @@ abstract class AbstractBaseQuery implements QueryInterface, QueryPartInterface /** * Converts this query into an SQL string by using the injected builder. - * Optionally can return the SQL with formatted structure. - * - * @param bool $formatted * * @return string */ - public function getSql($formatted = false) + public function getSql() { - if ($formatted) { - return $this->getBuilder()->writeFormatted($this); - } - return $this->getBuilder()->write($this); } @@ -174,7 +167,7 @@ abstract class AbstractBaseQuery implements QueryInterface, QueryPartInterface /** * @param string $table * - * @return $this + * @return \Phacil\Framework\MagiQL\Manipulation\Update|\Phacil\Framework\MagiQL\Manipulation\Delete|\Phacil\Framework\MagiQL\Manipulation\Select|\Phacil\Framework\MagiQL\Manipulation\Insert|\Phacil\Framework\MagiQL\Manipulation\Intersect|\Phacil\Framework\MagiQL\Manipulation\JoinQuery|\Phacil\Framework\MagiQL\Manipulation\Minus|\Phacil\Framework\MagiQL\Manipulation\Union|\Phacil\Framework\MagiQL\Manipulation\UnionAll|$this */ public function setTable($table) { @@ -186,7 +179,7 @@ abstract class AbstractBaseQuery implements QueryInterface, QueryPartInterface /** * * @param string $table - * @return $this + * @return \Phacil\Framework\MagiQL\Manipulation\Update|\Phacil\Framework\MagiQL\Manipulation\Delete|\Phacil\Framework\MagiQL\Manipulation\Select|\Phacil\Framework\MagiQL\Manipulation\Insert|\Phacil\Framework\MagiQL\Manipulation\Intersect|\Phacil\Framework\MagiQL\Manipulation\JoinQuery|\Phacil\Framework\MagiQL\Manipulation\Minus|\Phacil\Framework\MagiQL\Manipulation\Union|\Phacil\Framework\MagiQL\Manipulation\UnionAll|$this */ public function from($table){ return $this->setTable($table); diff --git a/system/database/Databases/MSSQL.php b/system/database/Databases/MSSQL.php index 4421326..eb84ef9 100644 --- a/system/database/Databases/MSSQL.php +++ b/system/database/Databases/MSSQL.php @@ -13,9 +13,15 @@ namespace Phacil\Framework\Databases; * * Doesn't work with PHP 7+ * @deprecated 2.0.0 + * @see \Phacil\Framework\Databases\sqlsrvPDO * @package Phacil\Framework\Databases */ class MSSQL implements \Phacil\Framework\Interfaces\Databases { + + const DB_TYPE = 'Microsoft SQL Server Database'; + + const DB_TYPE_ID = self::LIST_DB_TYPE_ID['MSSQL']; + private $connection; public function __construct($hostname, $username, $password, $database, $port = '1443', $charset = 'utf8') @@ -114,4 +120,20 @@ class MSSQL implements \Phacil\Framework\Interfaces\Databases $sql = str_replace(array_keys($params), array_values($params), $sql); return $this->query($sql); } + + /** + * + * {@inheritdoc} + */ + public function getDBType() { + return self::DB_TYPE; + } + + /** + * + * {@inheritdoc} + */ + public function getDBTypeId() { + return self::DB_TYPE_ID; + } } diff --git a/system/database/Databases/MySQL_PDO.php b/system/database/Databases/MySQL_PDO.php index 64d131e..31c3d9d 100644 --- a/system/database/Databases/MySQL_PDO.php +++ b/system/database/Databases/MySQL_PDO.php @@ -10,8 +10,14 @@ namespace Phacil\Framework\Databases; use Phacil\Framework\Interfaces\Databases; +/** @package Phacil\Framework\Databases */ class MySQL_PDO implements Databases { + + const DB_TYPE = 'MySQL'; + + const DB_TYPE_ID = self::LIST_DB_TYPE_ID['MYSQL']; + /** * Link to the database connection * @@ -240,4 +246,20 @@ class MySQL_PDO implements Databases return $paramType; } + + /** + * + * {@inheritdoc} + */ + public function getDBType() { + return self::DB_TYPE; + } + + /** + * + * {@inheritdoc} + */ + public function getDBTypeId() { + return self::DB_TYPE_ID; + } } \ No newline at end of file diff --git a/system/database/Databases/MySQL_legacy.php b/system/database/Databases/MySQL_legacy.php index b0bc79b..b500c30 100644 --- a/system/database/Databases/MySQL_legacy.php +++ b/system/database/Databases/MySQL_legacy.php @@ -13,8 +13,15 @@ namespace Phacil\Framework\Databases; * * Doesn't work with PHP 7+ * @package 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 { + + const DB_TYPE = 'MySQL'; + + const DB_TYPE_ID = 1; + private $connection; public function __construct($hostname, $username, $password, $database, $port = '3306', $charset = 'utf8') { @@ -108,4 +115,20 @@ class MySQL_legacy implements \Phacil\Framework\Interfaces\Databases { $sql = str_replace(array_keys($params), array_values($bindParams), $sql); return $this->query($sql); } + + /** + * + * {@inheritdoc} + */ + public function getDBType() { + return self::DB_TYPE; + } + + /** + * + * {@inheritdoc} + */ + public function getDBTypeId() { + return self::DB_TYPE_ID; + } } diff --git a/system/database/Databases/MySQLi.php b/system/database/Databases/MySQLi.php index fe55221..ffb800d 100644 --- a/system/database/Databases/MySQLi.php +++ b/system/database/Databases/MySQLi.php @@ -10,7 +10,6 @@ namespace Phacil\Framework\Databases; use MySQLi as GlobalMysqli; use Phacil\Framework\Interfaces\Databases; -use \stdClass; /** * Default driver to connect a MySQL/MariaDB databases. @@ -25,6 +24,16 @@ class MySQLi implements Databases { */ private $connection; + /** + * @var string + */ + const DB_TYPE = 'MySQL'; + + /** + * @var int + */ + const DB_TYPE_ID = self::LIST_DB_TYPE_ID['MYSQL']; + /** * @param string $hostname * @param string $username @@ -55,6 +64,22 @@ class MySQLi implements Databases { } + /** + * + * {@inheritdoc} + */ + public function getDBType() { + return self::DB_TYPE; + } + + /** + * + * {@inheritdoc} + */ + public function getDBTypeId() { + return self::DB_TYPE_ID; + } + /** * Execute the SQl Query * @param string $sql diff --git a/system/database/Databases/Oracle.php b/system/database/Databases/Oracle.php index c2924c2..34eee3d 100644 --- a/system/database/Databases/Oracle.php +++ b/system/database/Databases/Oracle.php @@ -16,6 +16,11 @@ use Phacil\Framework\Interfaces\Databases; * @package Phacil\Framework\Databases */ class Oracle implements Databases { + + const DB_TYPE = 'Oracle'; + + const DB_TYPE_ID = 3; + /** * * @var resource|false @@ -155,4 +160,18 @@ class Oracle implements Databases { return $sql; } + + /** + * {@inheritdoc} + */ + public function getDBType() { + return self::DB_TYPE; + } + + /** + * {@inheritdoc} + */ + public function getDBTypeId() { + return self::DB_TYPE_ID; + } } \ No newline at end of file diff --git a/system/database/Databases/Oracle_PDO.php b/system/database/Databases/Oracle_PDO.php index 404b7c5..7a26e7e 100644 --- a/system/database/Databases/Oracle_PDO.php +++ b/system/database/Databases/Oracle_PDO.php @@ -17,6 +17,27 @@ use Phacil\Framework\Interfaces\Databases; * @package Phacil\Framework\Databases */ class Oracle_PDO implements Databases { + + const DB_TYPE = 'Oracle'; + + const DB_TYPE_ID = self::LIST_DB_TYPE_ID['ORACLE']; + + /** + * + * {@inheritdoc} + */ + public function getDBType() { + return self::DB_TYPE; + } + + /** + * + * {@inheritdoc} + */ + public function getDBTypeId() { + return self::DB_TYPE_ID; + } + /** * * @var PDO @@ -48,10 +69,6 @@ class Oracle_PDO implements Databases } catch (\PDOException $e) { 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"); - $this->connection->exec("SET CHARACTER_SET_CONNECTION=utf8"); - $this->connection->exec("SET SQL_MODE = ''"); $this->rowCount = 0; } public function prepare($sql) diff --git a/system/database/Databases/Postgre.php b/system/database/Databases/Postgre.php index 2538b2f..befc26e 100644 --- a/system/database/Databases/Postgre.php +++ b/system/database/Databases/Postgre.php @@ -11,6 +11,11 @@ namespace Phacil\Framework\Databases; use Phacil\Framework\Interfaces\Databases; class Postgre implements Databases { + + const DB_TYPE = 'Postgre'; + + const DB_TYPE_ID = 4; + /** * * @var resource|false @@ -155,4 +160,18 @@ class Postgre implements Databases { return $sql; } + + /** + * {@inheritdoc} + */ + public function getDBType() { + return self::DB_TYPE; + } + + /** + * {@inheritdoc} + */ + public function getDBTypeId() { + return self::DB_TYPE_ID; + } } \ No newline at end of file diff --git a/system/database/Databases/SQLSRV.php b/system/database/Databases/SQLSRV.php index 02362a8..72782a5 100644 --- a/system/database/Databases/SQLSRV.php +++ b/system/database/Databases/SQLSRV.php @@ -11,6 +11,11 @@ namespace Phacil\Framework\Databases; use Phacil\Framework\Interfaces\Databases; class SQLSRV implements Databases { + + const DB_TYPE = 'Microsoft SQL Server Database'; + + const DB_TYPE_ID = self::LIST_DB_TYPE_ID['MSSQL']; + /** * * @var resource @@ -174,4 +179,20 @@ class SQLSRV implements Databases { return $this->query($sql); } } + + /** + * + * {@inheritdoc} + */ + public function getDBType() { + return self::DB_TYPE; + } + + /** + * + * {@inheritdoc} + */ + public function getDBTypeId() { + return self::DB_TYPE_ID; + } } \ No newline at end of file diff --git a/system/database/Databases/SQLite3.php b/system/database/Databases/SQLite3.php index 401b882..ef2a995 100644 --- a/system/database/Databases/SQLite3.php +++ b/system/database/Databases/SQLite3.php @@ -13,6 +13,11 @@ use \SQLite3 as nativeSQLite3; use \stdClass; class SQLite3 implements Databases { + + const DB_TYPE = 'SQLite3'; + + const DB_TYPE_ID = 5; + /** * * @var nativeSQLite3 @@ -158,4 +163,18 @@ class SQLite3 implements Databases { return $this->query($sql); } } + + /** + * {@inheritdoc} + */ + public function getDBType() { + return self::DB_TYPE; + } + + /** + * {@inheritdoc} + */ + public function getDBTypeId() { + return self::DB_TYPE_ID; + } } \ No newline at end of file diff --git a/system/database/Databases/mPDO.php b/system/database/Databases/mPDO.php index e442e8c..187caa5 100644 --- a/system/database/Databases/mPDO.php +++ b/system/database/Databases/mPDO.php @@ -16,6 +16,11 @@ use Phacil\Framework\Interfaces\Databases; * * @package Phacil\Framework\Databases */ class mPDO implements Databases { + + const DB_TYPE = 'MySQL'; + + const DB_TYPE_ID = 1; + /** * * @var PDO @@ -198,4 +203,20 @@ class mPDO implements Databases { return $paramType; } + + /** + * + * {@inheritdoc} + */ + public function getDBType() { + return self::DB_TYPE; + } + + /** + * + * {@inheritdoc} + */ + public function getDBTypeId() { + return self::DB_TYPE_ID; + } } \ No newline at end of file diff --git a/system/database/Databases/nullStatement.php b/system/database/Databases/nullStatement.php index 2b421a4..bed8a58 100644 --- a/system/database/Databases/nullStatement.php +++ b/system/database/Databases/nullStatement.php @@ -18,6 +18,26 @@ use Phacil\Framework\Interfaces\Databases; final class nullStatement implements Databases { //private $connection; + const DB_TYPE = NULL; + + const DB_TYPE_ID = 0; + + /** + * + * {@inheritdoc} + */ + public function getDBType() { + return self::DB_TYPE; + } + + /** + * + * {@inheritdoc} + */ + public function getDBTypeId() { + return self::DB_TYPE_ID; + } + public function __construct($hostname, $username, $password, $database, $charset = 'utf8mb4') { //$this->connection = NULL; } diff --git a/system/database/Databases/sqlsrvPDO.php b/system/database/Databases/sqlsrvPDO.php index 1897612..a588b85 100644 --- a/system/database/Databases/sqlsrvPDO.php +++ b/system/database/Databases/sqlsrvPDO.php @@ -12,6 +12,11 @@ use \PDO as PDONative; use Phacil\Framework\Interfaces\Databases; class sqlsrvPDO implements Databases { + + const DB_TYPE = 'Microsoft SQL Server Database'; + + const DB_TYPE_ID = 2; + /** * * @var PDONative @@ -224,4 +229,20 @@ class sqlsrvPDO implements Databases { return $paramType; } + + /** + * + * {@inheritdoc} + */ + public function getDBType() { + return self::DB_TYPE; + } + + /** + * + * {@inheritdoc} + */ + public function getDBTypeId() { + return self::DB_TYPE_ID; + } } \ No newline at end of file diff --git a/system/database/autoload.php b/system/database/autoload.php index ac7f310..5973631 100644 --- a/system/database/autoload.php +++ b/system/database/autoload.php @@ -259,4 +259,26 @@ final class Database { { return $this->driver->execute($sql, $params); } + + /** + * Textual database driver type + * @return string + */ + public function getDBType() { + return $this->driver->getDBType(); + } + + /** + * ID of database driver + * + * @return int 1 = MySQL/MariaDB + * @return int 2 = MS SQL Server + * @return int 3 = Oracle Database + * @return int 4 = Postgre + * @return int 5 = SQLite3 + * @return int 0 = NULL + */ + public function getDBTypeId() { + return $this->driver->getDBTypeId(); + } } diff --git a/system/engine/interfaces/databases.php b/system/engine/interfaces/databases.php index 54b95a9..7684108 100644 --- a/system/engine/interfaces/databases.php +++ b/system/engine/interfaces/databases.php @@ -11,6 +11,15 @@ interface Databases { + const LIST_DB_TYPE_ID = [ + "NULL" => 0, + "MYSQL" => 1, + "MSSQL" => 2, + "ORACLE" => 3, + "POSTGRE" => 4, + "SQLLITE3" => 5, + ]; + /** * Construct the connection. * @@ -74,4 +83,24 @@ * @throws \Phacil\Framework\Exception */ public function execute($sql, array $params = []); + + /** + * Return textual database driver type + * + * @return string + */ + public function getDBType(); + + /** + * Return ID of database type + * + * + * @return int 1 = MySQL/MariaDB + * @return int 2 = MS SQL Server + * @return int 3 = Oracle Database + * @return int 4 = Postgre + * @return int 5 = SQLite3 + * @return int 0 = NULL + */ + public function getDBTypeId(); } \ No newline at end of file