2022-01-09 03:49:51 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/3/30 0030
|
|
|
|
|
* Time: 15:23
|
|
|
|
|
*/
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Database;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
use Kiri\Abstracts\Component;
|
2023-01-30 11:03:58 +08:00
|
|
|
use Kiri\Di\Context;
|
2023-04-03 00:43:06 +08:00
|
|
|
use Kiri\Exception\ConfigException;
|
2023-04-01 20:57:07 +08:00
|
|
|
use PDO;
|
2022-01-09 03:49:51 +08:00
|
|
|
use PDOStatement;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Command
|
|
|
|
|
* @package Database
|
|
|
|
|
*/
|
|
|
|
|
class Command extends Component
|
|
|
|
|
{
|
2022-02-18 14:45:58 +08:00
|
|
|
const ROW_COUNT = 'count';
|
|
|
|
|
const FETCH = 'fetch';
|
|
|
|
|
const FETCH_ALL = 'fetchAll';
|
|
|
|
|
const EXECUTE = 'execute';
|
|
|
|
|
const FETCH_COLUMN = 'fetchColumn';
|
2022-01-09 03:49:51 +08:00
|
|
|
|
2022-09-20 18:53:49 +08:00
|
|
|
const DB_ERROR_MESSAGE = 'The system is busy, please try again later.';
|
|
|
|
|
|
2022-01-09 03:49:51 +08:00
|
|
|
/** @var Connection */
|
|
|
|
|
public Connection $db;
|
|
|
|
|
|
|
|
|
|
/** @var ?string */
|
|
|
|
|
public ?string $sql = '';
|
|
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
|
public array $params = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-01 00:37:52 +08:00
|
|
|
* @return int|bool
|
2022-01-09 03:49:51 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-01 00:37:52 +08:00
|
|
|
public function incrOrDecr(): int|bool
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2023-04-01 00:37:52 +08:00
|
|
|
return $this->_execute();
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-01 00:37:52 +08:00
|
|
|
* @return int|bool
|
2022-01-09 03:49:51 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-01 00:37:52 +08:00
|
|
|
public function save(): int|bool
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2023-04-01 00:37:52 +08:00
|
|
|
return $this->_execute();
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
2023-04-02 19:53:43 +08:00
|
|
|
|
|
|
|
|
|
2022-01-09 03:49:51 +08:00
|
|
|
/**
|
2023-04-05 10:50:17 +08:00
|
|
|
* @return bool|array|null
|
2022-01-09 03:49:51 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-05 10:50:17 +08:00
|
|
|
public function all(): null|bool|array
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2023-04-05 10:50:17 +08:00
|
|
|
return $this->_query(self::FETCH_ALL);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
2023-04-02 19:53:43 +08:00
|
|
|
|
2022-01-09 03:49:51 +08:00
|
|
|
/**
|
2023-04-05 10:50:17 +08:00
|
|
|
* @return bool|array|null
|
2022-01-09 03:49:51 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-05 10:50:17 +08:00
|
|
|
public function one(): null|bool|array
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2023-04-05 10:50:17 +08:00
|
|
|
return $this->_query(self::FETCH);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-05 10:50:17 +08:00
|
|
|
* @return bool|array|null
|
2022-01-09 03:49:51 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-05 10:50:17 +08:00
|
|
|
public function fetchColumn(): null|bool|array
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2023-04-05 10:50:17 +08:00
|
|
|
return $this->_query(self::FETCH_COLUMN);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-05 10:50:17 +08:00
|
|
|
* @return int|bool
|
2022-01-09 03:49:51 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-05 10:50:17 +08:00
|
|
|
public function rowCount(): int|bool
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2023-04-05 10:50:17 +08:00
|
|
|
return $this->_query(self::ROW_COUNT);
|
2023-04-02 19:53:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function printErrorMessage(\Throwable $throwable): mixed
|
|
|
|
|
{
|
|
|
|
|
$this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
|
|
|
|
|
|
|
|
|
|
return null;
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-01 00:37:52 +08:00
|
|
|
* @return int|bool
|
2022-01-09 03:49:51 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-01 00:37:52 +08:00
|
|
|
public function flush(): int|bool
|
2022-02-18 14:45:58 +08:00
|
|
|
{
|
2023-04-01 00:37:52 +08:00
|
|
|
return $this->_execute();
|
2022-02-18 14:45:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool|int
|
2023-04-03 00:43:06 +08:00
|
|
|
* @throws ConfigException
|
2022-02-18 14:45:58 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-03 00:37:30 +08:00
|
|
|
private function _execute(): bool|int
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2023-04-05 10:53:28 +08:00
|
|
|
return $this->result(static function (PDO $pdo, string $sql, array $params) {
|
|
|
|
|
$prepare = $pdo->prepare($sql);
|
|
|
|
|
if ($prepare === false || $prepare->execute($params) === false) {
|
2023-04-05 10:50:17 +08:00
|
|
|
throw new Exception(($prepare ?? $pdo)->errorInfo()[1]);
|
|
|
|
|
}
|
2023-04-03 00:37:30 +08:00
|
|
|
|
2023-04-05 10:50:17 +08:00
|
|
|
$result = (int)$pdo->lastInsertId();
|
|
|
|
|
$prepare->closeCursor();
|
2023-04-03 00:37:30 +08:00
|
|
|
|
2023-04-05 10:50:17 +08:00
|
|
|
return $result == 0 ? true : $result;
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-04-03 00:37:30 +08:00
|
|
|
|
2023-04-05 10:50:17 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $type
|
|
|
|
|
* @return bool|array|int|null
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function _query(string $type): bool|array|int|null
|
|
|
|
|
{
|
2023-04-05 10:53:28 +08:00
|
|
|
return $this->result(static function (PDO $pdo, string $sql, array $params) use ($type) {
|
|
|
|
|
$prepare = $pdo->query($sql);
|
|
|
|
|
if ($prepare === false || $prepare->execute($params) === false) {
|
2023-04-05 10:50:17 +08:00
|
|
|
throw new Exception(($prepare ?? $pdo)->errorInfo()[1]);
|
|
|
|
|
}
|
2023-04-05 12:35:18 +08:00
|
|
|
// $prepare->closeCursor();
|
|
|
|
|
return match ($type) {
|
2023-04-05 10:50:17 +08:00
|
|
|
Command::FETCH_COLUMN => $prepare->fetchColumn(PDO::FETCH_ASSOC),
|
|
|
|
|
Command::ROW_COUNT => $prepare->rowCount(),
|
|
|
|
|
Command::FETCH_ALL => $prepare->fetchAll(PDO::FETCH_ASSOC),
|
|
|
|
|
Command::FETCH => $prepare->fetch(PDO::FETCH_ASSOC),
|
|
|
|
|
};
|
|
|
|
|
});
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
2023-04-02 19:53:43 +08:00
|
|
|
|
2023-04-05 10:50:17 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param \Closure $callback
|
|
|
|
|
* @return bool|mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function result(\Closure $callback): mixed
|
|
|
|
|
{
|
|
|
|
|
$pdo = $this->db->getPdo();
|
|
|
|
|
try {
|
2023-04-05 10:53:28 +08:00
|
|
|
return $callback($pdo, $this->sql, $this->params);
|
2023-04-05 10:50:17 +08:00
|
|
|
} catch (\Throwable $throwable) {
|
|
|
|
|
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
|
|
|
|
|
return $this->result($callback);
|
|
|
|
|
}
|
|
|
|
|
return $this->error($throwable);
|
|
|
|
|
} finally {
|
|
|
|
|
$this->db->release($pdo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param \Throwable $throwable
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function error(\Throwable $throwable): bool
|
|
|
|
|
{
|
|
|
|
|
$message = $this->sql . '(' . json_encode($this->params, JSON_UNESCAPED_UNICODE) . ');';
|
2023-04-05 10:53:28 +08:00
|
|
|
return $this->logger->addError($message . $throwable->getMessage(), 'mysql');
|
2023-04-05 10:50:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-09-20 18:53:49 +08:00
|
|
|
/**
|
2023-04-02 23:28:23 +08:00
|
|
|
* @return array<Mysql\PDO, PDOStatement>|bool
|
2022-09-20 18:53:49 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-01 20:57:07 +08:00
|
|
|
private function search(): bool|array
|
2022-09-20 18:53:49 +08:00
|
|
|
{
|
2023-03-31 10:37:15 +08:00
|
|
|
$pdo = $this->db->getSlaveClient();
|
2023-04-03 00:37:30 +08:00
|
|
|
return [$pdo, null];
|
2022-09-20 18:53:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:49:51 +08:00
|
|
|
/**
|
2023-04-01 00:37:52 +08:00
|
|
|
* @return int|bool
|
2022-01-09 03:49:51 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-01 00:37:52 +08:00
|
|
|
public function delete(): int|bool
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2023-04-01 00:37:52 +08:00
|
|
|
return $this->_execute();
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-01 00:37:52 +08:00
|
|
|
* @return int|bool
|
2022-01-09 03:49:51 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-04-01 00:37:52 +08:00
|
|
|
public function exec(): int|bool
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2023-04-01 00:37:52 +08:00
|
|
|
return $this->_execute();
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function bindValues(array $data = []): static
|
|
|
|
|
{
|
2023-04-01 00:37:52 +08:00
|
|
|
if (count($data) > 0) {
|
2022-01-09 03:49:51 +08:00
|
|
|
$this->params = array_merge($this->params, $data);
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $sql
|
|
|
|
|
* @return $this
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function setSql($sql): static
|
|
|
|
|
{
|
|
|
|
|
$this->sql = $sql;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|