Skip to content

Commit

Permalink
New databases structure (based on autoload)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveira131 committed Jul 31, 2021
1 parent 2e50b2c commit d2099f2
Show file tree
Hide file tree
Showing 19 changed files with 634 additions and 95 deletions.
161 changes: 155 additions & 6 deletions system/database/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,167 @@
*/


namespace Phacil\Framework;

if(!defined('DIR_DATABASE'))
define('DIR_DATABASE', (__DIR__)."/");
final class Database {
/**
*
* @var object
*/
private $driver;

/**
*
* @var string
*/
private $cachePrefix = "SQL_";

/**
* @param string $driver
* @param string $hostname
* @param string $username
* @param string $password
* @param string $database
* @return void
*/
public function __construct($driver, $hostname, $username, $password, $database) {

include DIR_DATABASE."/library/db.php";
$driverClass = "\\Phacil\\Framework\\Databases\\".$driver;

use Phacil\Framework;
try {
$this->driver = new $driverClass($hostname, $username, $password, $database);
} catch (Exception $th) {
throw new Exception('Error: Could not load database file ' . $driver . '!');
//exit('Error: Could not load database file ' . $driver . '!');
}

}

/**
* @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();
}

if(defined('DB_DRIVER')) {
/** @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;
}
}


/* 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);
}
} */
51 changes: 0 additions & 51 deletions system/database/database/dbmysqli.php

This file was deleted.

13 changes: 13 additions & 0 deletions system/database/databases/dbmysqli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/*
* Copyright © 2021 ExacTI Technology Solutions. All rights reserved.
* GPLv3 General License.
* https://exacti.com.br
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
*/

namespace Phacil\Framework\Databases;

final class DBMySQLi extends MySQLi {

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
<?php
//namespace DB;
/*
* Copyright © 2021 ExacTI Technology Solutions. All rights reserved.
* GPLv3 General License.
* https://exacti.com.br
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
*/

namespace Phacil\Framework\Databases;

use PDO;

/**
* Alternative PDO MySQL connection method.
*
* @package Phacil\Framework\Databases */
final class mPDO {
/**
*
* @var PDO
*/
private $connection = null;

/**
*
* @var mixed
*/
private $statement = null;

public function __construct($hostname, $username, $password, $database, $port = '3306', $charset = 'UTF8') {
try {
$dsn = "mysql:host={$hostname};port={$port};dbname={$database};charset={$charset}";
Expand Down Expand Up @@ -105,6 +129,6 @@ public function isConnected() {
}

public function __destruct() {
$this->connection = null;
unset($this->connection);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
<?php
/*
* Copyright © 2021 ExacTI Technology Solutions. All rights reserved.
* GPLv3 General License.
* https://exacti.com.br
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
*/

namespace Phacil\Framework\Databases;

/**
* Legacy class to connect a MS SQL Server with PHP 5 legacy driver.
*
* Doesn't work with PHP 7+
* @package Phacil\Framework\Databases */
final class MSSQL {
private $connection;

Expand Down Expand Up @@ -76,4 +90,3 @@ public function __destruct() {
mssql_close($this->connection);
}
}
?>
18 changes: 18 additions & 0 deletions system/database/databases/mysql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/*
* Copyright © 2021 ExacTI Technology Solutions. All rights reserved.
* GPLv3 General License.
* https://exacti.com.br
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
*/

namespace Phacil\Framework\Databases;

/**
* Adaptation to use MySQLi instead MySQL Legacy driver
*
* If you need, use mysql_legacy driver.
* @package Phacil\Framework\Databases */
final class MySQL extends MySQLi {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<?php
final class MySQL {
/*
* Copyright © 2021 ExacTI Technology Solutions. All rights reserved.
* GPLv3 General License.
* https://exacti.com.br
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
*/

namespace Phacil\Framework\Databases;

/**
* Legacy class to connect a MySQL with PHP 5 legacy driver.
*
* Doesn't work with PHP 7+
* @package Phacil\Framework\Databases
* */
final class MySQL_legacy {
private $connection;

public function __construct($hostname, $username, $password, $database, $port = '3306', $charset = 'utf8') {
Expand Down Expand Up @@ -67,4 +82,3 @@ public function __destruct() {
mysql_close($this->connection);
}
}
?>
Loading

0 comments on commit d2099f2

Please sign in to comment.