made some refactoring
This commit is contained in:
148
src/Database/Adapter/PDO.php
Normal file
148
src/Database/Adapter/PDO.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Database\Adapter;
|
||||
|
||||
use PDOException;
|
||||
use RuntimeException;
|
||||
use stdClass;
|
||||
|
||||
final class PDO
|
||||
{
|
||||
private ?\PDO $connection = null;
|
||||
private $statement = null;
|
||||
|
||||
public function __construct(
|
||||
string $hostname,
|
||||
string $username,
|
||||
string $password,
|
||||
string $database,
|
||||
string $port = '3306'
|
||||
) {
|
||||
try {
|
||||
$this->connection = new \PDO(
|
||||
"mysql:host=" . $hostname . ";port=" . $port . ";dbname=" . $database,
|
||||
$username,
|
||||
$password,
|
||||
[\PDO::ATTR_PERSISTENT => true]
|
||||
);
|
||||
} catch (PDOException $e) {
|
||||
throw new RuntimeException('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 = ''");
|
||||
}
|
||||
|
||||
public function bindParam($parameter, $variable, $data_type = \PDO::PARAM_STR, $length = 0): void
|
||||
{
|
||||
if ($length) {
|
||||
$this->statement->bindParam($parameter, $variable, $data_type, $length);
|
||||
} else {
|
||||
$this->statement->bindParam($parameter, $variable, $data_type);
|
||||
}
|
||||
}
|
||||
|
||||
public function query($sql, $params = []): stdClass
|
||||
{
|
||||
$this->statement = $this->connection->prepare($sql);
|
||||
|
||||
$result = false;
|
||||
|
||||
try {
|
||||
if ($this->statement && $this->statement->execute($params)) {
|
||||
$data = [];
|
||||
|
||||
while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) {
|
||||
$data[] = $row;
|
||||
}
|
||||
|
||||
$result = new stdClass();
|
||||
$result->row = ($data[0] ?? []);
|
||||
$result->rows = $data;
|
||||
$result->num_rows = $this->statement->rowCount();
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
throw new RuntimeException(
|
||||
'Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql
|
||||
);
|
||||
}
|
||||
|
||||
if ($result) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$result = new stdClass();
|
||||
$result->row = [];
|
||||
$result->rows = [];
|
||||
$result->num_rows = 0;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function prepare($sql): void
|
||||
{
|
||||
$this->statement = $this->connection->prepare($sql);
|
||||
}
|
||||
|
||||
public function execute(): void
|
||||
{
|
||||
try {
|
||||
if ($this->statement && $this->statement->execute()) {
|
||||
$data = [];
|
||||
|
||||
while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) {
|
||||
$data[] = $row;
|
||||
}
|
||||
|
||||
/** @noinspection PhpObjectFieldsAreOnlyWrittenInspection */
|
||||
$result = new stdClass();
|
||||
$result->row = $data[0] ?? [];
|
||||
$result->rows = $data;
|
||||
$result->num_rows = $this->statement->rowCount();
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
throw new RuntimeException('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
public function escape($value): array|string
|
||||
{
|
||||
return str_replace(
|
||||
["\\", "\0", "\n", "\r", "\x1a", "'", '"'],
|
||||
["\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"'],
|
||||
$value
|
||||
);
|
||||
}
|
||||
|
||||
public function countAffected(): int
|
||||
{
|
||||
if ($this->statement) {
|
||||
return $this->statement->rowCount();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public function getLastId(): false|string
|
||||
{
|
||||
return $this->connection->lastInsertId();
|
||||
}
|
||||
|
||||
public function isConnected(): bool
|
||||
{
|
||||
if ($this->connection) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->connection = null;
|
||||
}
|
||||
}
|
||||
BIN
src/Database/Adapter/db.zip
Normal file
BIN
src/Database/Adapter/db.zip
Normal file
Binary file not shown.
105
src/Database/DB.php
Normal file
105
src/Database/DB.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package OpenCart
|
||||
* @author Daniel Kerr
|
||||
* @copyright Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/)
|
||||
* @license https://opensource.org/licenses/GPL-3.0
|
||||
* @link https://www.opencart.com
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Database;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* DB class
|
||||
*/
|
||||
class DB
|
||||
{
|
||||
private mixed $adaptor;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $adaptor
|
||||
* @param string $hostname
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @param string $database
|
||||
* @param int|null $port
|
||||
*
|
||||
*/
|
||||
public function __construct(
|
||||
string $adaptor,
|
||||
string $hostname,
|
||||
string $username,
|
||||
string $password,
|
||||
string $database,
|
||||
int $port = null
|
||||
) {
|
||||
$class = 'DB\\' . $adaptor;
|
||||
|
||||
if (class_exists($class)) {
|
||||
$this->adaptor = new $class($hostname, $username, $password, $database, $port);
|
||||
} else {
|
||||
throw new RuntimeException('Error: Could not load database adaptor ' . $adaptor . '!');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param string $sql
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function query(string $sql): array
|
||||
{
|
||||
return $this->adaptor->query($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function escape(string $value): string
|
||||
{
|
||||
return $this->adaptor->escape($value);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countAffected(): int
|
||||
{
|
||||
return $this->adaptor->countAffected();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLastId(): int
|
||||
{
|
||||
return $this->adaptor->getLastId();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function connected(): bool
|
||||
{
|
||||
return $this->adaptor->connected();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user