diff --git a/system/database/Databases/DBMySQLi.php b/system/database/Databases/Driver/DBMySQLi.php similarity index 100% rename from system/database/Databases/DBMySQLi.php rename to system/database/Databases/Driver/DBMySQLi.php diff --git a/system/database/Databases/MSSQL.php b/system/database/Databases/Driver/MSSQL.php similarity index 100% rename from system/database/Databases/MSSQL.php rename to system/database/Databases/Driver/MSSQL.php diff --git a/system/database/Databases/MySQL.php b/system/database/Databases/Driver/MySQL.php similarity index 100% rename from system/database/Databases/MySQL.php rename to system/database/Databases/Driver/MySQL.php diff --git a/system/database/Databases/MySQL_PDO.php b/system/database/Databases/Driver/MySQL_PDO.php similarity index 100% rename from system/database/Databases/MySQL_PDO.php rename to system/database/Databases/Driver/MySQL_PDO.php diff --git a/system/database/Databases/MySQL_legacy.php b/system/database/Databases/Driver/MySQL_legacy.php similarity index 100% rename from system/database/Databases/MySQL_legacy.php rename to system/database/Databases/Driver/MySQL_legacy.php diff --git a/system/database/Databases/MySQLi.php b/system/database/Databases/Driver/MySQLi.php similarity index 100% rename from system/database/Databases/MySQLi.php rename to system/database/Databases/Driver/MySQLi.php diff --git a/system/database/Databases/Oracle.php b/system/database/Databases/Driver/Oracle.php similarity index 100% rename from system/database/Databases/Oracle.php rename to system/database/Databases/Driver/Oracle.php diff --git a/system/database/Databases/Oracle_PDO.php b/system/database/Databases/Driver/Oracle_PDO.php similarity index 100% rename from system/database/Databases/Oracle_PDO.php rename to system/database/Databases/Driver/Oracle_PDO.php diff --git a/system/database/Databases/Postgre.php b/system/database/Databases/Driver/Postgre.php similarity index 100% rename from system/database/Databases/Postgre.php rename to system/database/Databases/Driver/Postgre.php diff --git a/system/database/Databases/SQLSRV.php b/system/database/Databases/Driver/SQLSRV.php similarity index 100% rename from system/database/Databases/SQLSRV.php rename to system/database/Databases/Driver/SQLSRV.php diff --git a/system/database/Databases/SQLite3.php b/system/database/Databases/Driver/SQLite3.php similarity index 100% rename from system/database/Databases/SQLite3.php rename to system/database/Databases/Driver/SQLite3.php diff --git a/system/database/Databases/mPDO.php b/system/database/Databases/Driver/mPDO.php similarity index 100% rename from system/database/Databases/mPDO.php rename to system/database/Databases/Driver/mPDO.php diff --git a/system/database/Databases/nullStatement.php b/system/database/Databases/Driver/nullStatement.php similarity index 100% rename from system/database/Databases/nullStatement.php rename to system/database/Databases/Driver/nullStatement.php diff --git a/system/database/Databases/sqlsrvPDO.php b/system/database/Databases/Driver/sqlsrvPDO.php similarity index 100% rename from system/database/Databases/sqlsrvPDO.php rename to system/database/Databases/Driver/sqlsrvPDO.php diff --git a/system/database/autoload.php b/system/database/autoload.php index bea2f7d..ff986f7 100644 --- a/system/database/autoload.php +++ b/system/database/autoload.php @@ -12,6 +12,7 @@ namespace Phacil\Framework; use Phacil\Framework\Databases\Api\DriverInterface as DatabaseInterface; use Phacil\Framework\Config; use Phacil\Framework\Api\Database as DatabaseApi; +use Phacil\Framework\Exception; /** * Principal class to load databases drivers @@ -32,19 +33,19 @@ class Database implements DatabaseApi { * @var string[] */ static public $legacyDrivers = [ - 'mpdo' => '\Phacil\Framework\Databases\mPDO', - 'mysql' => '\Phacil\Framework\Databases\MySQL', - 'dbmysqli' => '\Phacil\Framework\Databases\DBMySQLi', - 'mssql' => '\Phacil\Framework\Databases\MSSQL', - 'mysql_legacy' => '\Phacil\Framework\Databases\MySQL_legacy', - 'mysql_pdo' => '\Phacil\Framework\Databases\MySQL_PDO', - 'mysqli' => '\Phacil\Framework\Databases\MySQLi', - 'nullstatement' => '\Phacil\Framework\Databases\nullStatement', - 'oracle' => '\Phacil\Framework\Databases\Oracle', - 'postgre' => '\Phacil\Framework\Databases\Postgre', - 'sqlite3_db' => '\Phacil\Framework\Databases\SQLite3', - 'sqlsrv' => '\Phacil\Framework\Databases\SQLSRV', - 'sqlsrvpdo' => '\Phacil\Framework\Databases\sqlsrvPDO' + 'mpdo' => 'Phacil\Framework\Databases\Driver\mPDO', + 'mysql' => 'Phacil\Framework\Databases\Driver\MySQL', + 'dbmysqli' => 'Phacil\Framework\Databases\Driver\DBMySQLi', + 'mssql' => 'Phacil\Framework\Databases\Driver\MSSQL', + 'mysql_legacy' => 'Phacil\Framework\Databases\Driver\MySQL_legacy', + 'mysql_pdo' => 'Phacil\Framework\Databases\Driver\MySQL_PDO', + 'mysqli' => 'Phacil\Framework\Databases\Driver\MySQLi', + 'nullstatement' => 'Phacil\Framework\Databases\Driver\nullStatement', + 'oracle' => 'Phacil\Framework\Databases\Driver\Oracle', + 'postgre' => 'Phacil\Framework\Databases\Driver\Postgre', + 'sqlite3_db' => 'Phacil\Framework\Databases\Driver\SQLite3', + 'sqlsrv' => 'Phacil\Framework\Databases\Driver\SQLSRV', + 'sqlsrvpdo' => 'Phacil\Framework\Databases\Driver\sqlsrvPDO' ]; /** @@ -58,27 +59,32 @@ class Database implements DatabaseApi { * {@inheritdoc} */ public function __construct($driver, $hostname, $username, $password, $database) { - - $driverClass = (isset(self::$legacyDrivers[strtolower($driver)])) ? self::$legacyDrivers[strtolower($driver)] : $driver; + $driverClass = (isset(self::$legacyDrivers[strtolower($driver)])) ? self::$legacyDrivers[strtolower($driver)] : $driver; try { - $this->createDriver(new $driverClass($hostname, $username, $password, $database)); - } catch (\Exception $th) { - throw new \Phacil\Framework\Exception($driver. ' not loaded. '.$th->getMessage(), $th->getCode()); - } - + $this->createDriver( + \Phacil\Framework\Registry::getInstance()->create($driverClass, [ + $hostname, + $username, + $password, + $database + ]) + ); + } catch (\Exception $th) { + throw new \Phacil\Framework\Exception($driver . ' not loaded. ' . $th->getMessage(), $th->getCode()); + } } /** * @param DatabaseInterface $driverObject - * @return never + * @return void * @throws Exception */ private function createDriver(DatabaseInterface $driverObject) { try { $this->driver = $driverObject; - } catch (Exception $th) { + } catch (\Exception $th) { throw new Exception('Error: Could not create the driver! '.$th->getMessage()); } } @@ -90,31 +96,20 @@ class Database implements DatabaseApi { return $this->driver->isConnected(); } - /** - * {@inheritdoc} - */ - public function __destruct() { - //unset($this->driver); - } - /** * {@inheritdoc} */ public function query($sql = null, $cacheUse = true) { if(!$sql) { - return new \Phacil\Framework\MagiQL($this); + return \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\MagiQL::class, [$this]); } if(Config::SQL_CACHE() && $cacheUse == true) { - return $this->Cache($sql); - - } else { - - return $this->driver->query($sql); - } + } - } + return $this->driver->query($sql); + } /** * {@inheritdoc} @@ -128,7 +123,7 @@ class Database implements DatabaseApi { */ public function countAffected() { return $this->driver->countAffected(); - } + } /** * {@inheritdoc} @@ -180,41 +175,48 @@ class Database implements DatabaseApi { } /** + * * @param string $sql - * @return object - * @throws PhpfastcacheInvalidArgumentException + * @return mixed + * @throws \Phacil\Framework\Exception + * @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException */ private function Cache($sql) { - if(class_exists('\Phacil\Framework\Caches')) { - $cache = new Caches(); + /** + * @var \Phacil\Framework\Caches + */ + $cache = \Phacil\Framework\Registry::getInstance(Caches::class); + + if($cache->check($this->cachePrefix.md5($sql))) { + return $cache->get($this->cachePrefix.md5($sql)); + } - 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); + $query = $this->driver->query($sql); + if($query instanceof \Phacil\Framework\Databases\Object\ResultInterface){ + $cache->set($this->cachePrefix.md5($sql), $query); + + return $query; } + + return $query; } /** * {@inheritdoc} */ - public function createSubBase($nome, $object) { - - $this->$nome = $object; + public function createSubBase($name, DatabaseApi $object) { + $this->$name = $object; + return $this; } + public function get($name){ + return $this->$name; + } + + public function id($name) { + return $this->get($name); + } + /** * {@inheritdoc} */