2020-12-26 20:09:15 +08:00
|
|
|
<?php
|
|
|
|
|
|
2021-01-27 16:26:37 +08:00
|
|
|
namespace Nexus\Database;
|
|
|
|
|
|
2020-12-26 20:09:15 +08:00
|
|
|
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-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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-28 18:00:54 +08:00
|
|
|
public function getDriver()
|
|
|
|
|
{
|
|
|
|
|
return $this->driver;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 20:09:15 +08:00
|
|
|
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-28 18:00:54 +08:00
|
|
|
$result = $this->driver->connect($host, $username, $password, $database, $port);
|
|
|
|
|
if (!$result) {
|
|
|
|
|
throw new DatabaseException(sprintf('[%s]: %s', $this->errno(), $this->error()));
|
|
|
|
|
}
|
|
|
|
|
$this->driver->query("SET NAMES UTF8");
|
|
|
|
|
$this->driver->query("SET collation_connection = 'utf8_general_ci'");
|
2021-02-02 00:41:36 +08:00
|
|
|
$this->driver->query("SET sql_mode=''");
|
2021-01-27 17:50:24 +08:00
|
|
|
$this->isConnected = true;
|
2021-01-12 21:14:02 +08:00
|
|
|
return true;
|
2020-12-26 20:09:15 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-27 16:26:37 +08:00
|
|
|
public function autoConnect()
|
|
|
|
|
{
|
|
|
|
|
if ($this->isConnected()) {
|
2021-01-28 18:00:54 +08:00
|
|
|
return null;
|
2021-01-27 16:26:37 +08:00
|
|
|
}
|
2021-04-02 19:48:41 +08:00
|
|
|
$config = nexus_config('nexus.mysql');
|
2021-01-28 18:00:54 +08:00
|
|
|
return $this->connect($config['host'], $config['username'], $config['password'], $config['database'], $config['port']);
|
2021-01-27 16:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
2020-12-26 20:09:15 +08:00
|
|
|
public function query(string $sql)
|
|
|
|
|
{
|
2020-12-28 02:14:41 +08:00
|
|
|
try {
|
2021-01-27 16:26:37 +08:00
|
|
|
$this->autoConnect();
|
2020-12-28 02:14:41 +08:00
|
|
|
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()));
|
2021-01-27 16:26:37 +08:00
|
|
|
throw new DatabaseException($e->getMessage(), $sql);
|
2020-12-28 02:14:41 +08:00
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2021-01-27 16:26:37 +08:00
|
|
|
$this->autoConnect();
|
2020-12-26 20:09:15 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-01-25 01:23:28 +08:00
|
|
|
public static function insert($table, $data)
|
|
|
|
|
{
|
|
|
|
|
if (empty($table) || empty($data) || !is_array($data)) {
|
|
|
|
|
throw new DatabaseException("require table and data(array).");
|
|
|
|
|
}
|
|
|
|
|
$fields = array_map(function ($value) {return "`$value`";}, array_keys($data));
|
|
|
|
|
$values = array_map(function ($value) {return sqlesc($value);}, array_values($data));
|
|
|
|
|
$sql = sprintf("insert into `%s` (%s) values (%s)", $table, implode(', ', $fields), implode(', ', $values));
|
|
|
|
|
sql_query($sql);
|
|
|
|
|
return mysql_insert_id();
|
|
|
|
|
}
|
2020-12-26 20:09:15 +08:00
|
|
|
|
2021-03-02 21:03:02 +08:00
|
|
|
public static function update($table, $data, $whereStr)
|
|
|
|
|
{
|
|
|
|
|
$updateArr = [];
|
|
|
|
|
foreach ($data as $field => $value) {
|
|
|
|
|
$updateArr[] = "`$field` = " . sqlesc($value);
|
|
|
|
|
}
|
|
|
|
|
$sql = sprintf("update `%s` set %s where %s", $table, implode(', ', $updateArr), $whereStr);
|
|
|
|
|
sql_query($sql);
|
|
|
|
|
return mysql_affected_rows();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-03 19:29:29 +08:00
|
|
|
public static function delete($table, $whereStr, $limit = null)
|
|
|
|
|
{
|
|
|
|
|
$sql = "delete from $table where $whereStr";
|
|
|
|
|
if (!is_null($limit)) {
|
|
|
|
|
$sql .= " limit $limit";
|
|
|
|
|
}
|
|
|
|
|
sql_query($sql);
|
|
|
|
|
return mysql_affected_rows();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-03 01:56:07 +08:00
|
|
|
public static function getOne($table, $whereStr, $fields = '*')
|
|
|
|
|
{
|
|
|
|
|
if ($fields != '*') {
|
|
|
|
|
if (is_array($fields)) {
|
|
|
|
|
$fields = implode(', ', $fields);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (empty($fields)) {
|
|
|
|
|
do_log("args: " . json_encode(func_get_args()));
|
|
|
|
|
throw new DatabaseException("empty fields.");
|
|
|
|
|
}
|
|
|
|
|
$sql = "select $fields from $table where $whereStr limit 1";
|
|
|
|
|
$res = sql_query($sql);
|
|
|
|
|
return mysql_fetch_assoc($res);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 21:12:27 +08:00
|
|
|
public static function getAll($table, $whereStr, $fields = '*')
|
|
|
|
|
{
|
|
|
|
|
if ($fields != '*') {
|
|
|
|
|
if (is_array($fields)) {
|
|
|
|
|
$fields = implode(', ', $fields);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (empty($fields)) {
|
|
|
|
|
do_log("args: " . json_encode(func_get_args()));
|
|
|
|
|
throw new DatabaseException("empty fields.");
|
|
|
|
|
}
|
|
|
|
|
$sql = "select $fields from $table where $whereStr";
|
|
|
|
|
$res = sql_query($sql);
|
|
|
|
|
$result = [];
|
|
|
|
|
while ($row = mysql_fetch_assoc($res)) {
|
|
|
|
|
$result[] = $row;
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 19:48:41 +08:00
|
|
|
}
|