Files
kiri-databases/Command.php
T

275 lines
6.3 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-03 00:43:06 +08:00
use Kiri\Exception\ConfigException;
2023-04-01 20:57:07 +08:00
use PDO;
2023-07-31 23:08:59 +08:00
use PDOStatement;
2023-04-17 10:18:59 +08:00
use ReflectionException;
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
/**
*
*/
const DB_ERROR_MESSAGE = 'The system is busy, please try again later.';
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/** @var Connection */
public Connection $connection;
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/** @var ?string */
public ?string $sql = '';
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/** @var array */
public array $params = [];
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/**
* @param array $params
* @throws Exception
*/
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
/**
* @return int|bool
* @throws Exception
*/
public function incrOrDecr(): int|bool
{
return $this->_execute();
}
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/**
* @return int|bool
* @throws Exception
*/
public function save(): int|bool
{
return $this->_execute();
}
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/**
* @return bool|array
* @throws Exception
*/
public function all(): bool|array
{
try {
2023-07-31 23:08:59 +08:00
return $this->prepare()->fetchAll(PDO::FETCH_ASSOC);
2023-07-13 10:37:14 +08:00
} catch (Throwable $throwable) {
2023-07-27 22:59:00 +08:00
if ($this->canReconnect($throwable->getMessage())) {
2023-07-13 10:37:14 +08:00
return $this->all();
}
return $this->error($throwable);
} finally {
$this->connection->release($client ?? null);
}
}
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/**
* @return bool|array|null
* @throws Exception
*/
public function one(): null|bool|array
{
try {
2023-07-31 23:08:59 +08:00
return $this->prepare()->fetch(PDO::FETCH_ASSOC);
2023-07-13 10:37:14 +08:00
} catch (Throwable $throwable) {
2023-07-27 22:59:00 +08:00
if ($this->canReconnect($throwable->getMessage())) {
2023-07-13 10:37:14 +08:00
return $this->one();
}
return $this->error($throwable);
} finally {
$this->connection->release($client ?? null);
}
}
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/**
* @return bool|array|null
* @throws Exception
*/
public function fetchColumn(): null|bool|array
{
try {
2023-07-31 23:08:59 +08:00
return $this->prepare()->fetchColumn(PDO::FETCH_ASSOC);
2023-07-13 10:37:14 +08:00
} catch (Throwable $throwable) {
2023-07-27 22:59:00 +08:00
if ($this->canReconnect($throwable->getMessage())) {
2023-07-13 10:37:14 +08:00
return $this->fetchColumn();
}
return $this->error($throwable);
} finally {
$this->connection->release($client ?? null);
}
}
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/**
* @return int|bool
* @throws Exception
*/
public function rowCount(): int|bool
{
try {
2023-07-31 23:08:59 +08:00
return $this->prepare()->rowCount();
2023-07-13 10:37:14 +08:00
} catch (Throwable $throwable) {
2023-07-27 22:59:00 +08:00
if ($this->canReconnect($throwable->getMessage())) {
2023-07-13 10:37:14 +08:00
return $this->rowCount();
}
return $this->error($throwable);
} finally {
$this->connection->release($client ?? null);
}
}
2023-04-10 17:13:24 +08:00
2023-07-31 23:08:59 +08:00
/**
* @return PDOStatement
* @throws Exception
*/
protected function prepare(): PDOStatement
{
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]);
}
$prepare->execute($this->params);
return $prepare;
}
2023-07-13 10:37:14 +08:00
/**
* @return int|bool
* @throws Exception
*/
public function flush(): int|bool
{
return $this->_execute();
}
2023-04-10 17:13:24 +08:00
2023-07-27 22:59:12 +08:00
2023-07-13 10:37:14 +08:00
/**
* @return bool|int
* @throws ConfigException
* @throws Exception
*/
private function _execute(): bool|int
{
try {
$client = $this->connection->getTransactionClient();
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]);
}
if ($prepare->execute($this->params) === false) {
throw new Exception($prepare->errorInfo()[1]);
}
$result = $client->lastInsertId();
$prepare->closeCursor();
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
return $result == 0 ? true : (int)$result;
} catch (Throwable $throwable) {
2023-07-27 22:59:00 +08:00
if ($this->canReconnect($throwable->getMessage())) {
2023-07-13 10:37:14 +08:00
return $this->_execute();
}
return $this->error($throwable);
2023-08-11 14:02:51 +08:00
} finally {
if (isset($client) && !$client->inTransaction()) {
$this->connection->release($client);
}
2023-07-13 10:37:14 +08:00
}
}
2023-04-10 17:13:24 +08:00
2023-07-27 22:59:00 +08:00
/**
* @param string $message
* @return bool
*/
protected function canReconnect(string $message): bool
{
$errors = [
'MySQL server has gone away',
'Packets out of order. Expected 1 received 0.'
];
foreach ($errors as $error) {
if (str_contains($message, $error)) {
return true;
}
}
return false;
}
2023-07-13 10:37:14 +08:00
/**
* @param Throwable $throwable
* @return bool
2023-07-06 13:33:50 +08:00
*/
2023-07-13 10:37:14 +08:00
private function error(Throwable $throwable): bool
{
2023-07-31 23:08:59 +08:00
return trigger_print_error($this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE) . PHP_EOL . jTraceEx($throwable), 'mysql');
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 int|bool
* @throws Exception
*/
public function delete(): int|bool
{
return $this->_execute();
}
2023-04-10 17:13:24 +08:00
2023-07-13 10:37:14 +08:00
/**
* @return int|bool
* @throws Exception
*/
public function exec(): int|bool
{
return $this->_execute();
}
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
2023-07-13 10:37:14 +08:00
/**
* @param $sql
* @return $this
* @throws Exception
*/
public function setSql($sql): static
{
$this->sql = $sql;
return $this;
}
2023-04-10 17:13:24 +08:00
2022-01-09 03:49:51 +08:00
}