2020-12-26 20:09:15 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class DB
|
|
|
|
|
{
|
2021-01-04 20:47:22 +08:00
|
|
|
private $driver;
|
2020-12-26 20:09:15 +08:00
|
|
|
|
|
|
|
|
private static $instance;
|
|
|
|
|
|
2021-01-04 20:47:22 +08:00
|
|
|
private static $queries = [];
|
2020-12-26 20:09:15 +08:00
|
|
|
|
2021-01-12 21:14:02 +08:00
|
|
|
private $isConnected = false;
|
|
|
|
|
|
2020-12-26 20:09:15 +08:00
|
|
|
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)
|
|
|
|
|
{
|
2021-01-12 21:14:02 +08:00
|
|
|
if (!$this->isConnected) {
|
2021-01-12 01:54:46 +08:00
|
|
|
$this->driver->connect($host, $username, $password, $database, $port);
|
2021-01-12 21:14:02 +08:00
|
|
|
$this->isConnected = true;
|
2021-01-12 01:54:46 +08:00
|
|
|
}
|
2021-01-12 21:14:02 +08:00
|
|
|
return true;
|
2020-12-26 20:09:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function query(string $sql)
|
|
|
|
|
{
|
2020-12-28 02:14:41 +08:00
|
|
|
try {
|
|
|
|
|
return $this->driver->query($sql);
|
|
|
|
|
} catch (\Exception $e) {
|
2021-01-11 22:00:46 +08:00
|
|
|
do_log(sprintf("%s [%s] %s", $e->getMessage(), $sql, $e->getTraceAsString()));
|
2020-12-28 02:14:41 +08:00
|
|
|
throw new \DatabaseException($sql, $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 20:09:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 02:14:41 +08:00
|
|
|
public function fetchArray($result, $type = null)
|
|
|
|
|
{
|
|
|
|
|
return $this->driver->fetchArray($result, $type);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 20:09:15 +08:00
|
|
|
public function affectedRows()
|
|
|
|
|
{
|
|
|
|
|
return $this->driver->affectedRows();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function escapeString(string $string)
|
|
|
|
|
{
|
|
|
|
|
return $this->driver->escapeString($string);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 20:52:54 +08:00
|
|
|
public function lastInsertId()
|
|
|
|
|
{
|
|
|
|
|
return $this->driver->lastInsertId();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-07 17:35:00 +08:00
|
|
|
public function freeResult($result)
|
|
|
|
|
{
|
|
|
|
|
return $this->driver->freeResult($result);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 01:54:46 +08:00
|
|
|
public function isConnected()
|
|
|
|
|
{
|
2021-01-12 21:14:02 +08:00
|
|
|
return $this->isConnected;
|
2021-01-12 01:54:46 +08:00
|
|
|
}
|
|
|
|
|
|
2020-12-26 20:09:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|