Skip to content

Commit

Permalink
Databases interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveira131 committed Jul 31, 2021
1 parent d2099f2 commit 1097674
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 227 deletions.
53 changes: 38 additions & 15 deletions system/database/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,6 +28,8 @@ final class Database {
private $cachePrefix = "SQL_";

/**
* Construct the connection.
*
* @param string $driver
* @param string $hostname
* @param string $username
Expand All @@ -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) {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
} */
3 changes: 2 additions & 1 deletion system/database/databases/mpdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
85 changes: 48 additions & 37 deletions system/database/databases/mssql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) . '<br />' . $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);
}
}
4 changes: 3 additions & 1 deletion system/database/databases/mysql_legacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion system/database/databases/mysql_pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down
3 changes: 2 additions & 1 deletion system/database/databases/mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Exception;
use MySQLi as GlobalMysqli;
use Phacil\Framework\Interfaces\Databases;
use stdClass;

/**
Expand All @@ -18,7 +19,7 @@
* Works on most of PHP instalations
*
* @package Phacil\Framework\Databases */
class MySQLi {
class MySQLi implements Databases {
/**
*
* @var GlobalMysqli
Expand Down
9 changes: 8 additions & 1 deletion system/database/databases/nullStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion system/database/databases/oracle.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
namespace Phacil\Framework\Databases;

use Exception;
use Phacil\Framework\Interfaces\Databases;
use stdClass;

/**
* Oracle driver connector
*
* @package Phacil\Framework\Databases
*/
final class Oracle{
final class Oracle implements Databases {
/**
*
* @var resource|false
Expand Down
8 changes: 7 additions & 1 deletion system/database/databases/postgre.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

namespace Phacil\Framework\Databases;

final class Postgre {
use Phacil\Framework\Interfaces\Databases;

final class Postgre implements Databases {
/**
*
* @var resource|false
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion system/database/databases/sqlite3_db.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 1097674

Please sign in to comment.