Files
kiri-databases/Command.php
T

269 lines
6.2 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-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
* @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
{
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
/**
* @return bool|array|null
* @throws Exception
*/
public function one(): null|bool|array
{
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
/**
2023-08-14 18:56:45 +08:00
* @return mixed
2023-07-13 10:37:14 +08:00
* @throws Exception
*/
2023-08-14 18:56:45 +08:00
public function fetchColumn(): mixed
2023-08-29 22:47:48 +08:00
{
return $this->search('fetchColumn');
}
/**
* @param string $method
* @return mixed
*/
protected function search(string $method): mixed
2023-07-13 10:37:14 +08:00
{
try {
2023-08-29 22:13:37 +08:00
$client = $this->connection->getConnection();
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
}
$prepare->execute($this->params);
2023-08-29 22:47:48 +08:00
$data = $prepare->{$method}(PDO::FETCH_ASSOC);
$this->connection->release($client);
return $data;
2023-07-13 10:37:14 +08:00
} catch (Throwable $throwable) {
2023-08-24 16:01:20 +08:00
if ($this->isRefresh($throwable)) {
2023-08-29 22:47:48 +08:00
return $this->search($method);
}
if (isset($client)) {
$this->connection->release($client);
2023-07-13 10:37:14 +08:00
}
2023-08-24 16:01:20 +08:00
return $this->error($throwable);
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 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 {
2023-12-06 18:24:53 +08:00
/** @var PDO $client */
2023-10-12 00:46:07 +08:00
[$client, $prepare] = $this->_prepare();
$result = $client->lastInsertId();
$prepare->closeCursor();
$this->connection->release($client);
2023-12-06 18:24:53 +08:00
if ($result == 0) {
return $prepare->rowCount() > 0;
} else {
return (int)$result;
}
2023-07-13 10:37:14 +08:00
} catch (Throwable $throwable) {
2023-08-24 16:01:20 +08:00
if ($this->isRefresh($throwable)) {
2023-07-13 10:37:14 +08:00
return $this->_execute();
}
2023-08-29 22:47:48 +08:00
if (isset($client)) {
$this->connection->release($client);
}
2023-08-24 16:01:20 +08:00
return $this->error($throwable);
2023-07-13 10:37:14 +08:00
}
}
2023-04-10 17:13:24 +08:00
2023-08-24 16:01:20 +08:00
2023-10-11 23:51:54 +08:00
/**
* @return bool|int
* @throws ConfigException
* @throws Exception
*/
private function _delete(): bool|int
{
try {
2023-10-12 00:46:07 +08:00
[$client, $prepare] = $this->_prepare();
$prepare->closeCursor();
2023-10-11 23:51:54 +08:00
$this->connection->release($client);
2023-10-12 00:46:07 +08:00
return true;
2023-10-11 23:51:54 +08:00
} catch (Throwable $throwable) {
if ($this->isRefresh($throwable)) {
return $this->_execute();
}
if (isset($client)) {
$this->connection->release($client);
}
return $this->error($throwable);
}
}
/**
* @return array
* @throws Exception
*/
private function _prepare(): array
{
$client = $this->connection->getConnection();
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]);
}
2023-10-12 00:46:07 +08:00
return [$client, $prepare];
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
{
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
return true;
}
if (str_contains($throwable->getMessage(), 'Send of 14 bytes failed with errno=32 Broken pipe')) {
return true;
}
2023-08-24 16:04:24 +08:00
if (str_contains($throwable->getMessage(), 'Lost connection to MySQL server during query')) {
return true;
}
2023-08-24 16:01:20 +08:00
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-08-14 14:02:16 +08:00
return trigger_print_error($this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE) . PHP_EOL . throwable($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
{
2023-10-11 23:51:54 +08:00
return $this->_delete();
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 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
}