From 109767445f15a37db3171e3326b86e4640a17cc1 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Sat, 31 Jul 2021 01:13:58 -0300 Subject: [PATCH] Databases interfaces --- system/database/autoload.php | 53 +++++-- system/database/databases/mpdo.php | 3 +- system/database/databases/mssql.php | 85 +++++----- system/database/databases/mysql_legacy.php | 4 +- system/database/databases/mysql_pdo.php | 12 +- system/database/databases/mysqli.php | 3 +- system/database/databases/nullStatement.php | 9 +- system/database/databases/oracle.php | 3 +- system/database/databases/postgre.php | 8 +- system/database/databases/sqlite3_db.php | 3 +- system/database/databases/sqlsrv.php | 7 +- system/database/databases/sqlsrvpdo.php | 3 +- system/database/library/db.php | 163 -------------------- system/engine/autoload.php | 3 +- system/engine/interfaces/databases.php | 67 ++++++++ system/system.php | 2 +- 16 files changed, 201 insertions(+), 227 deletions(-) delete mode 100644 system/database/library/db.php create mode 100644 system/engine/interfaces/databases.php diff --git a/system/database/autoload.php b/system/database/autoload.php index 1da6f69..5ba3855 100644 --- a/system/database/autoload.php +++ b/system/database/autoload.php @@ -6,13 +6,18 @@ * Phacil PHP Framework - https://github.com/exacti/phacil-framework */ - namespace Phacil\Framework; +use Phacil\Framework\Interfaces\Databases; + +/** + * Principal class to load databases drivers + * + * @package Phacil\Framework */ final class Database { /** * - * @var object + * @var Databases */ private $driver; @@ -23,6 +28,8 @@ final class Database { private $cachePrefix = "SQL_"; /** + * Construct the connection. + * * @param string $driver * @param string $hostname * @param string $username @@ -42,11 +49,28 @@ public function __construct($driver, $hostname, $username, $password, $database) } } + + /** + * Check is connected on database + * @return bool */ + public function isConnected() { + return $this->driver->isConnected(); + } + + /** + * Destroy the connection + * + * @return void */ + public function __destruct() { + unset($this->driver); + } /** + * Execute the SQL Query + * * @param string $sql * @param bool $cacheUse - * @return object|\Phacil\Framework\DB::Cache + * @return object|\Phacil\Framework\Database::Cache * @throws PhpfastcacheInvalidArgumentException */ public function query($sql, $cacheUse = true) { @@ -63,19 +87,27 @@ public function query($sql, $cacheUse = true) { } /** + * Important escape to prevent SQL injection. + * * @param string $value - * @return mixed + * @return string */ public function escape($value) { return $this->driver->escape($value); } - /** @return int */ + /** + * Gets the number of rows affected by the last operation + * + * @return int */ public function countAffected() { return $this->driver->countAffected(); } - /** @return mixed */ + /** + * Gets the ID of the last inserted row or sequence of values + * + * @return int|string */ public function getLastId() { return $this->driver->getLastId(); } @@ -162,12 +194,3 @@ public function createSubBase($nome, $object) { $this->$nome = $object; } } - - -/* if(defined('DB_DRIVER')) { - global $db; - $db = new Phacil\Framework\DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE); -} else { - global $db; - $db = new Phacil\Framework\DB('nullStatement', NULL, NULL, NULL, NULL); -} */ diff --git a/system/database/databases/mpdo.php b/system/database/databases/mpdo.php index 34822db..97b3177 100644 --- a/system/database/databases/mpdo.php +++ b/system/database/databases/mpdo.php @@ -9,12 +9,13 @@ namespace Phacil\Framework\Databases; use PDO; +use Phacil\Framework\Interfaces\Databases; /** * Alternative PDO MySQL connection method. * * @package Phacil\Framework\Databases */ -final class mPDO { +final class mPDO implements Databases { /** * * @var PDO diff --git a/system/database/databases/mssql.php b/system/database/databases/mssql.php index b557417..f0c8083 100644 --- a/system/database/databases/mssql.php +++ b/system/database/databases/mssql.php @@ -13,80 +13,91 @@ * * Doesn't work with PHP 7+ * @package Phacil\Framework\Databases */ -final class MSSQL { +final class MSSQL implements \Phacil\Framework\Interfaces\Databases +{ private $connection; - - public function __construct($hostname, $username, $password, $database, $port = '1443', $charset = 'utf8') { + + 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); - } + exit('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); + } - if (!mssql_select_db($database, $this->connection)) { - exit('Error: Could not connect to database ' . $database); - } - mssql_query("SET NAMES 'utf8'", $this->connection); mssql_query("SET CHARACTER SET utf8", $this->connection); mssql_query("SET CHARACTER_SET_CONNECTION=utf8", $this->connection); - } - - public function query($sql) { + } + + public function query($sql) + { $resource = mssql_query($sql, $this->connection); if ($resource) { if (is_resource($resource)) { $i = 0; - + $data = array(); - + while ($result = mssql_fetch_assoc($resource)) { $data[$i] = $result; - + $i++; } - + mssql_free_result($resource); - + $query = new stdClass(); $query->row = isset($data[0]) ? $data[0] : array(); $query->rows = $data; $query->num_rows = $i; - + unset($data); - return $query; - } else { + return $query; + } else { return true; } } else { trigger_error('Error: ' . mssql_get_last_message($this->connection) . '
' . $sql); - exit(); - } - } - - public function escape($value) { + exit(); + } + } + + public function escape($value) + { return mssql_real_escape_string($value, $this->connection); } - - public function countAffected() { - return mssql_rows_affected($this->connection); - } - public function getLastId() { + public function countAffected() + { + return mssql_rows_affected($this->connection); + } + + public function getLastId() + { $last_id = false; - + $resource = mssql_query("SELECT @@identity AS id", $this->connection); - + if ($row = mssql_fetch_row($resource)) { $last_id = trim($row[0]); } - + mssql_free_result($resource); - + return $last_id; - } - - public function __destruct() { + } + + function isConnected() + { + } + + public function __destruct() + { mssql_close($this->connection); } } diff --git a/system/database/databases/mysql_legacy.php b/system/database/databases/mysql_legacy.php index d0e28ae..12504f6 100644 --- a/system/database/databases/mysql_legacy.php +++ b/system/database/databases/mysql_legacy.php @@ -14,7 +14,7 @@ * Doesn't work with PHP 7+ * @package Phacil\Framework\Databases * */ -final class MySQL_legacy { +final class MySQL_legacy implements \Phacil\Framework\Interfaces\Databases { private $connection; public function __construct($hostname, $username, $password, $database, $port = '3306', $charset = 'utf8') { @@ -31,6 +31,8 @@ public function __construct($hostname, $username, $password, $database, $port = mysql_query("SET CHARACTER_SET_CONNECTION=".$charset."", $this->connection); mysql_query("SET SQL_MODE = ''", $this->connection); } + + public function isConnected() { } public function query($sql) { $resource = mysql_query($sql, $this->connection); diff --git a/system/database/databases/mysql_pdo.php b/system/database/databases/mysql_pdo.php index 4674e2a..0b3e2d4 100644 --- a/system/database/databases/mysql_pdo.php +++ b/system/database/databases/mysql_pdo.php @@ -8,7 +8,9 @@ namespace Phacil\Framework\Databases; -final class MYSQL_PDO +use Phacil\Framework\Interfaces\Databases; + +final class MYSQL_PDO implements Databases { /** * Link to the database connection @@ -59,6 +61,14 @@ public function __construct($host, $user, $pass, $name, $port = '3306', $charset $this->options['PDO::MYSQL_ATTR_INIT_COMMAND'] = "SET NAMES '{$charset}'"; $this->connect(); } + + public function isConnected() { + if ($this->dbh) { + return true; + } else { + return false; + } + } /** * Connect to database */ diff --git a/system/database/databases/mysqli.php b/system/database/databases/mysqli.php index 74baa80..176aef1 100644 --- a/system/database/databases/mysqli.php +++ b/system/database/databases/mysqli.php @@ -10,6 +10,7 @@ use Exception; use MySQLi as GlobalMysqli; +use Phacil\Framework\Interfaces\Databases; use stdClass; /** @@ -18,7 +19,7 @@ * Works on most of PHP instalations * * @package Phacil\Framework\Databases */ -class MySQLi { +class MySQLi implements Databases { /** * * @var GlobalMysqli diff --git a/system/database/databases/nullStatement.php b/system/database/databases/nullStatement.php index 0cd6f7b..800a59d 100644 --- a/system/database/databases/nullStatement.php +++ b/system/database/databases/nullStatement.php @@ -8,18 +8,25 @@ namespace Phacil\Framework\Databases; +use Phacil\Framework\Interfaces\Databases; + /** * Nullable fake simulated DB connection. * * @package Phacil\Framework\Databases */ -final class nullStatement { +final class nullStatement implements Databases { //private $connection; public function __construct($hostname, $username, $password, $database, $charset = 'utf8mb4') { //$this->connection = NULL; } + public function isConnected() { + + return false; + } + public function query($sql) { $result = new \stdClass(); $result->num_rows = NULL; diff --git a/system/database/databases/oracle.php b/system/database/databases/oracle.php index 8138684..0795b12 100644 --- a/system/database/databases/oracle.php +++ b/system/database/databases/oracle.php @@ -9,6 +9,7 @@ namespace Phacil\Framework\Databases; use Exception; +use Phacil\Framework\Interfaces\Databases; use stdClass; /** @@ -16,7 +17,7 @@ * * @package Phacil\Framework\Databases */ -final class Oracle{ +final class Oracle implements Databases { /** * * @var resource|false diff --git a/system/database/databases/postgre.php b/system/database/databases/postgre.php index ac9e5f6..e8b4626 100644 --- a/system/database/databases/postgre.php +++ b/system/database/databases/postgre.php @@ -8,7 +8,9 @@ namespace Phacil\Framework\Databases; -final class Postgre { +use Phacil\Framework\Interfaces\Databases; + +final class Postgre implements Databases { /** * * @var resource|false @@ -36,6 +38,10 @@ public function __construct($hostname, $username, $password, $database, $port = pg_query($this->link, "SET CLIENT_ENCODING TO '".$charset."'"); } + public function isConnected() { + return ($this->link) ? true : false; + } + /** * @param string $sql * @return stdClass|true diff --git a/system/database/databases/sqlite3_db.php b/system/database/databases/sqlite3_db.php index ec4c7f9..a05c9a1 100644 --- a/system/database/databases/sqlite3_db.php +++ b/system/database/databases/sqlite3_db.php @@ -9,10 +9,11 @@ namespace Phacil\Framework\Databases; use Exception; +use Phacil\Framework\Interfaces\Databases; use \SQLite3; use stdClass; -final class Sqlite3_db { +final class Sqlite3_db implements Databases { /** * * @var SQLite3 diff --git a/system/database/databases/sqlsrv.php b/system/database/databases/sqlsrv.php index fcbc664..09141ee 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 \stdClass; -final class SQLSRV { +final class SQLSRV implements Databases { /** * * @var resource @@ -50,6 +51,10 @@ public function __construct($hostname, $username, $password, $database, $port = sqlsrv_query($this->link, "SET CHARACTER SET utf8"); } + public function isConnected() { + return ($this->link) ? true : false; + } + /** * @param string $sql * @return stdClass|true diff --git a/system/database/databases/sqlsrvpdo.php b/system/database/databases/sqlsrvpdo.php index ddfa80e..08de3a7 100644 --- a/system/database/databases/sqlsrvpdo.php +++ b/system/database/databases/sqlsrvpdo.php @@ -10,9 +10,10 @@ use Exception; use \PDO as PDONative; +use Phacil\Framework\Interfaces\Databases; use stdClass; -final class sqlsrvPDO { +final class sqlsrvPDO implements Databases { /** * * @var PDONative diff --git a/system/database/library/db.php b/system/database/library/db.php deleted file mode 100644 index 8add049..0000000 --- a/system/database/library/db.php +++ /dev/null @@ -1,163 +0,0 @@ -driver = new $driver($hostname, $username, $password, $database); - } - - /** - * @param string $sql - * @param bool $cacheUse - * @return object|\Phacil\Framework\DB::Cache - * @throws PhpfastcacheInvalidArgumentException - */ - public function query($sql, $cacheUse = true) { - - if(defined('SQL_CACHE') && SQL_CACHE == true && $cacheUse == true) { - - return $this->Cache($sql); - - } else { - - return $this->driver->query($sql); - } - - } - - /** - * @param string $value - * @return mixed - */ - public function escape($value) { - return $this->driver->escape($value); - } - - /** @return int */ - public function countAffected() { - return $this->driver->countAffected(); - } - - /** @return mixed */ - public function getLastId() { - return $this->driver->getLastId(); - } - - /** - * @param string $sql - * @param int $pageNum_exibe - * @param int $maxRows_exibe - * @param bool $cache - * @param mixed|null $sqlTotal - * @return object - * @throws PhpfastcacheInvalidArgumentException - */ - public function pagination($sql, $pageNum_exibe = 1, $maxRows_exibe = 10, $cache = true, $sqlTotal = null){ - - if (($pageNum_exibe >= 1)) { - $pageNum_exibe = $pageNum_exibe-1; - } - $startRow_exibe = $pageNum_exibe * $maxRows_exibe; - - $query_exibe = $sql; - - $query_limit_exibe = sprintf("%s LIMIT %d, %d", $query_exibe, $startRow_exibe, $maxRows_exibe); - - $exibe = $this->query($query_limit_exibe, $cache); - - $re = '/^(SELECT \*)/i'; - - $all_exibe_query = ($sqlTotal != null) ? $sqlTotal : ((preg_match($re, $query_exibe)) ? preg_replace($re, "SELECT COUNT(*) as __TOTALdeREG_DB_Pagination", $query_exibe) : $query_exibe); - - $all_exibe = $this->query($all_exibe_query, $cache); - $totalRows_exibe = (isset($all_exibe->row['__TOTALdeREG_DB_Pagination'])) ? $all_exibe->row['__TOTALdeREG_DB_Pagination'] : $all_exibe->num_rows; - - if($totalRows_exibe <= 0){ - $all_exibe_query = $query_exibe; - $all_exibe = $this->query($all_exibe_query, $cache); - $totalRows_exibe = (isset($all_exibe->row['__TOTALdeREG_DB_Pagination'])) ? $all_exibe->row['__TOTALdeREG_DB_Pagination'] : $all_exibe->num_rows; - } - - $totalPages_exibe = ceil($totalRows_exibe/$maxRows_exibe); - - $exibe->totalPages_exibe = $totalPages_exibe; - $exibe->totalRows_exibe = $totalRows_exibe; - $exibe->pageNum_exibe = $pageNum_exibe+1; - - return $exibe; - } - - /** - * @param string $sql - * @return object - * @throws PhpfastcacheInvalidArgumentException - */ - private function Cache($sql) { - if(class_exists('Caches')) { - $cache = new Caches(); - - if (stripos($sql, "select") !== false) { - - if($cache->check($this->cachePrefix.md5($sql))) { - - return $cache->get($this->cachePrefix.md5($sql)); - - } else { - $cache->set($this->cachePrefix.md5($sql), $this->driver->query($sql)); - - return $this->driver->query($sql); - } - } else { - return $this->driver->query($sql); - } - } else { - return $this->driver->query($sql); - } - } - - /** - * @param string $nome - * @param object $object - * @return void - */ - public function createSubBase($nome, $object) { - - $this->$nome = $object; - } -} diff --git a/system/engine/autoload.php b/system/engine/autoload.php index b3686ec..a7b9b13 100644 --- a/system/engine/autoload.php +++ b/system/engine/autoload.php @@ -66,7 +66,8 @@ class_alias("\\Phacil\\Framework\\".$class, $class); 'interfaces\\front', 'interfaces\\loader', 'interfaces\\action', - 'traits\\action' + 'traits\\action', + 'interfaces\\databases' ]; if($namespace[0] == "Phacil" && in_array($classNative, $allowed)){ diff --git a/system/engine/interfaces/databases.php b/system/engine/interfaces/databases.php new file mode 100644 index 0000000..b4caa2c --- /dev/null +++ b/system/engine/interfaces/databases.php @@ -0,0 +1,67 @@ +dirs = glob(DIR_SYSTEM.'*/autoload.php', GLOB_BRACE); - require_once (DIR_SYSTEM.'database/autoload.php'); + //require_once (DIR_SYSTEM.'database/autoload.php'); require_once (DIR_SYSTEM.'engine/autoload.php'); }