Files
kiri-databases/Command.php
T

257 lines
6.0 KiB
PHP
Raw Normal View History

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-04-16 01:45:33 +08:00
use Kiri\Di\Container;
2023-04-01 20:57:07 +08:00
use PDO;
2023-04-17 10:18:59 +08:00
use Throwable;
2022-01-09 03:49:51 +08:00
/**
* Class Command
* @package Database
*/
class Command extends Component
{
2023-04-17 10:34:23 +08:00
2023-07-13 10:37:14 +08:00
public Connection $connection;
2023-11-14 15:01:30 +08:00
public ?string $sql = '';
public array $params = [];
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/**
* @param array $params
2023-12-12 15:35:35 +08:00
* @throws
2023-07-13 10:37:14 +08:00
*/
public function __construct(array $params = [])
{
parent::__construct();
Container::configure($this, $params);
}
2023-04-16 01:45:33 +08:00
2023-07-13 10:37:14 +08:00
/**
2023-12-13 14:47:23 +08:00
* @return bool
2023-12-12 15:35:35 +08:00
* @throws
2023-07-13 10:37:14 +08:00
*/
2023-12-13 14:47:23 +08:00
public function incrOrDecr(): bool
2023-07-13 10:37:14 +08:00
{
2023-12-13 14:47:23 +08:00
return (bool)$this->_prepare();
2023-07-13 10:37:14 +08:00
}
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/**
* @return bool|array
2023-12-12 15:35:35 +08:00
* @throws
2023-07-13 10:37:14 +08:00
*/
public function all(): bool|array
{
2023-08-29 22:47:48 +08:00
return $this->search('fetchAll');
2023-07-13 10:37:14 +08:00
}
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/**
2023-12-13 14:47:23 +08:00
* @return array|bool|null
2023-12-12 15:35:35 +08:00
* @throws
2023-07-13 10:37:14 +08:00
*/
2023-12-13 14:47:23 +08:00
public function one(): array|null|bool
2023-07-13 10:37:14 +08:00
{
2023-08-29 22:47:48 +08:00
return $this->search('fetch');
2023-07-13 10:37:14 +08:00
}
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/**
2024-04-26 15:36:55 +08:00
* @return bool
* @throws Exception
2023-07-13 10:37:14 +08:00
*/
2024-04-26 15:36:55 +08:00
public function exists(): bool
2023-08-29 22:47:48 +08:00
{
2024-04-26 15:39:13 +08:00
$data = $this->search('fetch');
if (!$data) {
return false;
}
return true;
2023-08-29 22:47:48 +08:00
}
2023-12-13 14:47:23 +08:00
/**
* @return mixed
* @throws
*/
2024-04-26 15:36:55 +08:00
public function fetchColumn(): mixed
2023-12-13 14:47:23 +08:00
{
2024-04-26 15:36:55 +08:00
return $this->search('fetchColumn');
2023-12-13 14:47:23 +08:00
}
/**
2024-04-26 15:36:55 +08:00
* @return mixed
* @throws
2023-12-13 14:47:23 +08:00
*/
2024-04-26 15:36:55 +08:00
public function rowCount(): int
2023-12-13 14:47:23 +08:00
{
2024-04-26 15:36:55 +08:00
$data = $this->search('fetch');
return !$data ? 0 : +current($data);
2023-12-13 14:47:23 +08:00
}
2023-08-29 22:47:48 +08:00
/**
* @param string $method
* @return mixed
2023-12-12 15:35:35 +08:00
* @throws
2023-08-29 22:47:48 +08:00
*/
protected function search(string $method): mixed
2023-07-13 10:37:14 +08:00
{
2023-12-06 18:32:21 +08:00
$client = $this->connection->getConnection();
2023-07-13 10:37:14 +08:00
try {
2024-10-23 14:10:14 +08:00
$startTime = microtime(true);
2023-08-16 16:16:29 +08:00
if (($prepare = $client->prepare($this->sql)) === false) {
2023-08-30 23:25:21 +08:00
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $client->errorInfo()[2]);
2023-08-16 16:16:29 +08:00
}
2023-12-06 18:33:25 +08:00
2023-12-13 17:07:01 +08:00
$prepare->execute($this->params);
2023-12-13 17:02:29 +08:00
$result = $method == 'rowCount' ? $prepare->rowCount() : $prepare->{$method}(PDO::FETCH_ASSOC);
2025-05-19 11:09:33 +08:00
$prepare->closeCursor();
2023-12-06 18:33:25 +08:00
2024-10-23 14:10:14 +08:00
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
2024-01-10 17:59:05 +08:00
2023-12-13 17:02:29 +08:00
return $result;
2023-07-13 10:37:14 +08:00
} catch (Throwable $throwable) {
2025-12-31 00:19:29 +08:00
$this->getLogger()->json_log($throwable);
2025-11-23 23:11:49 +08:00
2025-12-31 00:19:29 +08:00
if ($this->isRefresh($throwable)) {
return $this->search($method);
}
2023-12-13 17:43:25 +08:00
2023-12-13 18:27:12 +08:00
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params);
2023-12-13 18:19:36 +08:00
2025-12-31 00:19:29 +08:00
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
2023-12-06 18:32:21 +08:00
} finally {
$this->connection->release($client);
2023-07-13 10:37:14 +08:00
}
}
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/**
2023-12-13 14:47:23 +08:00
* @return bool
2023-12-12 15:35:35 +08:00
* @throws
2023-07-13 10:37:14 +08:00
*/
2023-12-13 14:47:23 +08:00
public function flush(): bool
2023-10-11 23:51:54 +08:00
{
2023-12-13 14:47:23 +08:00
return (bool)$this->_prepare();
2023-10-11 23:51:54 +08:00
}
/**
2023-12-13 14:47:23 +08:00
* @return int|bool
2023-12-12 15:35:35 +08:00
* @throws
2023-10-11 23:51:54 +08:00
*/
2023-12-13 14:47:23 +08:00
private function _prepare(): int|bool
2023-10-11 23:51:54 +08:00
{
$client = $this->connection->getConnection();
2023-12-06 18:32:21 +08:00
try {
2024-10-23 14:10:14 +08:00
$startTime = microtime(true);
2023-12-06 18:32:21 +08:00
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
}
if ($prepare->execute($this->params) === false) {
throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]);
}
$prepare->closeCursor();
$result = $client->lastInsertId();
2024-10-23 14:10:14 +08:00
$this->connection->println($startTime, microtime(true), $this->sql, $this->params);
2024-01-10 17:59:05 +08:00
2023-12-13 14:47:23 +08:00
return $result == 0 ? $prepare->rowCount() : (int)$result;
2023-12-06 18:32:21 +08:00
} catch (Throwable $throwable) {
2025-12-31 00:19:29 +08:00
$this->getLogger()->json_log($throwable);
2025-11-23 23:11:49 +08:00
2025-12-31 00:19:29 +08:00
if ($this->isRefresh($throwable)) {
return $this->_prepare();
}
2023-12-13 17:43:25 +08:00
2023-12-13 18:27:12 +08:00
$errorMsg = $throwable->getMessage() . PHP_EOL . ' Sql: ' . $this->sql . '.' . json_encode($this->params);
2023-12-13 18:19:36 +08:00
2025-12-31 00:19:29 +08:00
return $this->getLogger()->logCategory($errorMsg . PHP_EOL, 'mysql');
2023-12-06 18:32:21 +08:00
} finally {
$this->connection->release($client);
2023-10-11 23:51:54 +08:00
}
}
2023-08-24 16:01:20 +08:00
/**
* @param Throwable $throwable
* @return bool
*/
protected function isRefresh(Throwable $throwable): bool
{
2025-12-16 20:20:08 +08:00
$message = $throwable->getMessage();
// MySQL 错误处理
if (str_contains($message, 'MySQL server has gone away')) {
2023-08-24 16:01:20 +08:00
return true;
}
2025-12-16 20:20:08 +08:00
if (str_contains($message, 'Send of 14 bytes failed with errno=32 Broken pipe')) {
2023-08-24 16:01:20 +08:00
return true;
}
2025-12-16 20:20:08 +08:00
if (str_contains($message, 'Lost connection to MySQL server during query')) {
2023-08-24 16:04:24 +08:00
return true;
}
2025-12-16 20:20:08 +08:00
// PostgreSQL 错误处理
if (str_contains($message, 'server closed the connection unexpectedly')) {
return true;
}
if (str_contains($message, 'Connection refused')) {
return true;
}
if (str_contains($message, 'Broken pipe')) {
return true;
}
if (str_contains($message, 'connection to server was lost')) {
return true;
}
2023-08-24 16:01:20 +08:00
return false;
}
2023-07-13 10:37:14 +08:00
/**
* @return bool
2023-12-12 15:35:35 +08:00
* @throws
2023-07-13 10:37:14 +08:00
*/
2023-12-13 14:47:23 +08:00
public function delete(): bool
2023-07-13 10:37:14 +08:00
{
2023-12-13 14:47:23 +08:00
return (bool)$this->_prepare();
2023-07-13 10:37:14 +08:00
}
2023-04-10 17:13:24 +08:00
2023-12-20 17:26:58 +08:00
2023-07-13 10:37:14 +08:00
/**
* @return int|bool
*/
public function exec(): int|bool
{
2023-12-06 18:33:25 +08:00
return $this->_prepare();
2023-07-13 10:37:14 +08:00
}
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/**
* @param array $data
* @return $this
*/
public function bindValues(array $data = []): static
{
if (count($data) > 0) {
$this->params = array_merge($this->params, $data);
}
return $this;
}
2023-04-10 17:13:24 +08:00
2022-01-09 03:49:51 +08:00
}