first commit
This commit is contained in:
131
system/library/db/mysqli.php
Normal file
131
system/library/db/mysqli.php
Normal file
@ -0,0 +1,131 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\DB;
|
||||
/**
|
||||
* Class MySQLi
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class MySQLi {
|
||||
/**
|
||||
* @var object|\mysqli|null
|
||||
*/
|
||||
private object|null $connection;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $hostname
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @param string $database
|
||||
* @param string $port
|
||||
*/
|
||||
public function __construct(string $hostname, string $username, string $password, string $database, string $port = '') {
|
||||
if (!$port) {
|
||||
$port = '3306';
|
||||
}
|
||||
|
||||
try {
|
||||
$mysqli = @new \MySQLi($hostname, $username, $password, $database, $port);
|
||||
|
||||
$this->connection = $mysqli;
|
||||
$this->connection->set_charset('utf8mb4');
|
||||
|
||||
$this->query("SET SESSION sql_mode = 'NO_ZERO_IN_DATE,NO_ENGINE_SUBSTITUTION'");
|
||||
$this->query("SET FOREIGN_KEY_CHECKS = 0");
|
||||
|
||||
// Sync PHP and DB time zones
|
||||
$this->query("SET `time_zone` = '" . $this->escape(date('P')) . "'");
|
||||
} catch (\mysqli_sql_exception $e) {
|
||||
throw new \Exception('Error: Could not make a database link using ' . $username . '@' . $hostname . '!<br/>Message: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Query
|
||||
*
|
||||
* @param string $sql
|
||||
*
|
||||
* @return bool|object
|
||||
*/
|
||||
public function query(string $sql): bool|object {
|
||||
try {
|
||||
$query = $this->connection->query($sql);
|
||||
|
||||
if ($query instanceof \mysqli_result) {
|
||||
$data = [];
|
||||
|
||||
while ($row = $query->fetch_assoc()) {
|
||||
$data[] = $row;
|
||||
}
|
||||
|
||||
$result = new \stdClass();
|
||||
$result->num_rows = $query->num_rows;
|
||||
$result->row = isset($data[0]) ? $data[0] : [];
|
||||
$result->rows = $data;
|
||||
|
||||
$query->close();
|
||||
|
||||
unset($data);
|
||||
|
||||
return $result;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} catch (\mysqli_sql_exception $e) {
|
||||
throw new \Exception('Error: ' . $this->connection->error . '<br/>Error No: ' . $this->connection->errno . '<br/>' . $sql);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape
|
||||
*
|
||||
* @param string value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function escape(string $value): string {
|
||||
return $this->connection->real_escape_string($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* countAffected
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countAffected(): int {
|
||||
return $this->connection->affected_rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* getLastId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLastId(): int {
|
||||
return $this->connection->insert_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* isConnected
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isConnected(): bool {
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*
|
||||
* Closes the DB connection when this object is destroyed.
|
||||
*
|
||||
*/
|
||||
public function __destruct() {
|
||||
if ($this->connection) {
|
||||
$this->connection->close();
|
||||
|
||||
$this->connection = null;
|
||||
}
|
||||
}
|
||||
}
|
147
system/library/db/pdo.php
Normal file
147
system/library/db/pdo.php
Normal file
@ -0,0 +1,147 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\DB;
|
||||
/**
|
||||
* Class PDO
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class PDO {
|
||||
/**
|
||||
* @var object|\PDO|null
|
||||
*/
|
||||
private object|null $connection;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $data = [];
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private int $affected;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $hostname
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @param string $database
|
||||
* @param string $port
|
||||
*/
|
||||
public function __construct(string $hostname, string $username, string $password, string $database, string $port = '') {
|
||||
if (!$port) {
|
||||
$port = '3306';
|
||||
}
|
||||
|
||||
try {
|
||||
$pdo = @new \PDO('mysql:host=' . $hostname . ';port=' . $port . ';dbname=' . $database . ';charset=utf8mb4', $username, $password, array(\PDO::ATTR_PERSISTENT => false, \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4 COLLATE utf8mb4_general_ci'));
|
||||
} catch (\PDOException $e) {
|
||||
throw new \Exception('Error: Could not make a database link using ' . $username . '@' . $hostname . '!');
|
||||
}
|
||||
|
||||
if ($pdo) {
|
||||
$this->connection = $pdo;
|
||||
|
||||
$this->query("SET SESSION sql_mode = 'NO_ZERO_IN_DATE,NO_ENGINE_SUBSTITUTION'");
|
||||
$this->query("SET FOREIGN_KEY_CHECKS = 0");
|
||||
|
||||
// Sync PHP and DB time zones
|
||||
$this->query("SET `time_zone` = '" . $this->escape(date('P')) . "'");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Query
|
||||
*
|
||||
* @param string $sql
|
||||
*
|
||||
* @return bool|object
|
||||
*/
|
||||
public function query(string $sql): bool|object {
|
||||
$sql = preg_replace('/(?:\'\:)([a-z0-9]*.)(?:\')/', ':$1', $sql);
|
||||
|
||||
$statement = $this->connection->prepare($sql);
|
||||
|
||||
try {
|
||||
if ($statement && $statement->execute($this->data)) {
|
||||
$this->data = [];
|
||||
|
||||
if ($statement->columnCount()) {
|
||||
$data = $statement->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
$result = new \stdClass();
|
||||
$result->row = isset($data[0]) ? $data[0] : [];
|
||||
$result->rows = $data;
|
||||
$result->num_rows = count($data);
|
||||
$this->affected = 0;
|
||||
|
||||
return $result;
|
||||
} else {
|
||||
$this->affected = $statement->rowCount();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
$statement->closeCursor();
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} catch (\PDOException $e) {
|
||||
throw new \Exception('Error: ' . $e->getMessage() . ' <br/>Error Code : ' . $e->getCode() . ' <br/>' . $sql);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape
|
||||
*
|
||||
* @param string value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function escape(string $value): string {
|
||||
$key = ':' . count($this->data);
|
||||
|
||||
$this->data[$key] = $value;
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* countAffected
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countAffected(): int {
|
||||
return $this->affected;
|
||||
}
|
||||
|
||||
/**
|
||||
* getLastId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLastId(): int {
|
||||
return $this->connection->lastInsertId();
|
||||
}
|
||||
|
||||
/**
|
||||
* isConnected
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isConnected(): bool {
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*
|
||||
* Closes the DB connection when this object is destroyed.
|
||||
*
|
||||
*/
|
||||
public function __destruct() {
|
||||
$this->connection = null;
|
||||
}
|
||||
}
|
136
system/library/db/pgsql.php
Normal file
136
system/library/db/pgsql.php
Normal file
@ -0,0 +1,136 @@
|
||||
<?php
|
||||
namespace Opencart\System\Library\DB;
|
||||
/**
|
||||
* Class PgSQL
|
||||
*
|
||||
* @package
|
||||
*/
|
||||
class PgSQL {
|
||||
/**
|
||||
* @var object|resource|null
|
||||
*/
|
||||
private object|null $connection;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $hostname
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @param string $database
|
||||
* @param string $port
|
||||
*/
|
||||
public function __construct(string $hostname, string $username, string $password, string $database, string $port = '') {
|
||||
if (!$port) {
|
||||
$port = '5432';
|
||||
}
|
||||
|
||||
try {
|
||||
$pg = @pg_connect('host=' . $hostname . ' port=' . $port . ' user=' . $username . ' password=' . $password . ' dbname=' . $database . ' options=\'--client_encoding=UTF8\' ');
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception('Error: Could not make a database link using ' . $username . '@' . $hostname);
|
||||
}
|
||||
|
||||
if ($pg) {
|
||||
$this->connection = $pg;
|
||||
pg_query($this->connection, "SET CLIENT_ENCODING TO 'UTF8'");
|
||||
|
||||
// Sync PHP and DB time zones
|
||||
pg_query($this->connection, "SET TIMEZONE = '" . $this->escape(date('P')) . "'");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Query
|
||||
*
|
||||
* @param string $sql
|
||||
*
|
||||
* @return bool|object
|
||||
*/
|
||||
public function query(string $sql): bool|object {
|
||||
$resource = pg_query($this->connection, $sql);
|
||||
|
||||
if ($resource) {
|
||||
if (is_resource($resource)) {
|
||||
$i = 0;
|
||||
|
||||
$data = [];
|
||||
|
||||
while ($result = pg_fetch_assoc($resource)) {
|
||||
$data[$i] = $result;
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
pg_free_result($resource);
|
||||
|
||||
$query = new \stdClass();
|
||||
$query->row = isset($data[0]) ? $data[0] : [];
|
||||
$query->rows = $data;
|
||||
$query->num_rows = $i;
|
||||
|
||||
unset($data);
|
||||
|
||||
return $query;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
throw new \Exception('Error: ' . pg_result_error($resource) . '<br/>' . $sql);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape
|
||||
*
|
||||
* @param string value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function escape(string $value): string {
|
||||
return pg_escape_string($this->connection, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* countAffected
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countAffected(): int {
|
||||
return pg_affected_rows($this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* getLastId
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLastId(): int {
|
||||
$query = $this->query("SELECT LASTVAL() AS `id`");
|
||||
|
||||
return $query->row['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* isConnected
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isConnected(): bool {
|
||||
return pg_connection_status($this->connection) == PGSQL_CONNECTION_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*
|
||||
* Closes the DB connection when this object is destroyed.
|
||||
*
|
||||
*/
|
||||
public function __destruct() {
|
||||
if ($this->connection) {
|
||||
pg_close($this->connection);
|
||||
|
||||
$this->connection = null;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user