mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-22 19:13:28 +08:00
add database directory
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
class DB
|
||||
{
|
||||
private DBInterface $driver;
|
||||
|
||||
private static $instance;
|
||||
|
||||
private static array $queries = [];
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private function __clone()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function setDriver(DBInterface $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$instance) {
|
||||
return self::$instance;
|
||||
}
|
||||
$instance = new self;
|
||||
$driver = new DBMysqli();
|
||||
$instance->setDriver($driver);
|
||||
return self::$instance = $instance;
|
||||
}
|
||||
|
||||
|
||||
public function connect($host, $username, $password, $database, $port)
|
||||
{
|
||||
return $this->driver->connect($host, $username, $password, $database, $port);
|
||||
}
|
||||
|
||||
public function query(string $sql)
|
||||
{
|
||||
try {
|
||||
return $this->driver->query($sql);
|
||||
} catch (\Exception $e) {
|
||||
throw new \DatabaseException($sql, $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return $this->driver->error();
|
||||
}
|
||||
|
||||
public function errno()
|
||||
{
|
||||
return $this->driver->errno();
|
||||
}
|
||||
|
||||
public function numRows($result)
|
||||
{
|
||||
return $this->driver->numRows($result);
|
||||
}
|
||||
|
||||
public function select_db($database)
|
||||
{
|
||||
return $this->driver->selectDb($database);
|
||||
}
|
||||
|
||||
public function fetchAssoc($result)
|
||||
{
|
||||
return $this->driver->fetchAssoc($result);
|
||||
}
|
||||
|
||||
public function fetchRow($result)
|
||||
{
|
||||
return $this->driver->fetchRow($result);
|
||||
}
|
||||
|
||||
public function fetchArray($result, $type = null)
|
||||
{
|
||||
return $this->driver->fetchArray($result, $type);
|
||||
}
|
||||
|
||||
public function affectedRows()
|
||||
{
|
||||
return $this->driver->affectedRows();
|
||||
}
|
||||
|
||||
public function escapeString(string $string)
|
||||
{
|
||||
return $this->driver->escapeString($string);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
class DBMysqli implements DBInterface
|
||||
{
|
||||
private mysqli $mysqli;
|
||||
|
||||
public function connect($host, $username, $password, $database, $port)
|
||||
{
|
||||
$mysqli = new mysqli($host, $username, $password, $database, $port);
|
||||
/* check connection */
|
||||
if (mysqli_connect_errno()) {
|
||||
throw new \RuntimeException(mysqli_connect_error());
|
||||
}
|
||||
/* activate reporting */
|
||||
$driver = new mysqli_driver();
|
||||
$driver->report_mode = MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX;
|
||||
|
||||
return $this->mysqli = $mysqli;
|
||||
}
|
||||
|
||||
public function query(string $sql)
|
||||
{
|
||||
return $this->mysqli->query($sql);
|
||||
}
|
||||
|
||||
public function error(): string
|
||||
{
|
||||
return $this->mysqli->error;
|
||||
}
|
||||
|
||||
public function errno(): int
|
||||
{
|
||||
return $this->mysqli->errno;
|
||||
}
|
||||
|
||||
public function numRows($mysqliResult): int
|
||||
{
|
||||
return $mysqliResult->num_rows;
|
||||
}
|
||||
|
||||
public function selectDb($database)
|
||||
{
|
||||
return $this->mysqli->select_db($database);
|
||||
}
|
||||
|
||||
public function fetchAssoc($mysqliResult): array|null
|
||||
{
|
||||
return $mysqliResult->fetch_assoc();
|
||||
}
|
||||
|
||||
public function fetchRow($mysqliResult): array|null
|
||||
{
|
||||
return $mysqliResult->fetch_row();
|
||||
}
|
||||
|
||||
public function fetchArray($mysqliResult, $type): array|null
|
||||
{
|
||||
if (is_null($type)) {
|
||||
$type = MYSQLI_BOTH;
|
||||
}
|
||||
return $mysqliResult->fetch_array($type);
|
||||
}
|
||||
|
||||
public function affectedRows(): int
|
||||
{
|
||||
return $this->mysqli->affected_rows;
|
||||
}
|
||||
|
||||
public function escapeString(string $string): string
|
||||
{
|
||||
return $this->mysqli->real_escape_string($string);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
class DatabaseException extends \Exception
|
||||
{
|
||||
|
||||
public function __construct($query, $message)
|
||||
{
|
||||
parent::__construct("$message [$query]");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
function mysql_connect($host, $username, $password, $database, $port)
|
||||
{
|
||||
return DB::getInstance()->connect($host, $username, $password, $database, $port);
|
||||
}
|
||||
|
||||
function mysql_errno()
|
||||
{
|
||||
return DB::getInstance()->errno();
|
||||
}
|
||||
|
||||
function mysql_error()
|
||||
{
|
||||
return DB::getInstance()->error();
|
||||
}
|
||||
|
||||
function mysql_query(string $sql)
|
||||
{
|
||||
return DB::getInstance()->query($sql);
|
||||
}
|
||||
|
||||
function mysql_select_db($database)
|
||||
{
|
||||
return DB::getInstance()->select_db($database);
|
||||
}
|
||||
|
||||
function mysql_num_rows($result)
|
||||
{
|
||||
return DB::getInstance()->numRows($result);
|
||||
}
|
||||
|
||||
function mysql_fetch_array($result, $type = null)
|
||||
{
|
||||
return DB::getInstance()->fetchArray($result, $type);
|
||||
}
|
||||
|
||||
function mysql_fetch_assoc($result)
|
||||
{
|
||||
return DB::getInstance()->fetchAssoc($result);
|
||||
}
|
||||
|
||||
function mysql_fetch_row($result)
|
||||
{
|
||||
return DB::getInstance()->fetchRow($result);
|
||||
}
|
||||
|
||||
function mysql_affected_rows()
|
||||
{
|
||||
return DB::getInstance()->affectedRows();
|
||||
}
|
||||
|
||||
function mysql_real_escape_string($string)
|
||||
{
|
||||
return DB::getInstance()->escapeString($string);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
interface DBInterface
|
||||
{
|
||||
public function connect($host, $username, $password, $database, $port);
|
||||
|
||||
public function query(string $sql);
|
||||
|
||||
public function error(): string;
|
||||
|
||||
public function errno(): int;
|
||||
|
||||
public function numRows($result): int;
|
||||
|
||||
public function selectDb($database);
|
||||
|
||||
public function fetchAssoc($result): array|null;
|
||||
|
||||
public function fetchRow($result): array|null;
|
||||
|
||||
public function fetchArray($result, $type): array|null;
|
||||
|
||||
public function affectedRows(): int;
|
||||
|
||||
public function escapeString(string $string): string;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user