Files
kiri-databases/Command.php
T

233 lines
5.1 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-12-06 18:32:21 +08:00
use PDOStatement;
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
/**
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 save(): 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
/**
2023-08-14 18:56:45 +08:00
* @return mixed
2023-12-12 15:35:35 +08:00
* @throws
2023-07-13 10:37:14 +08:00
*/
2023-08-14 18:56:45 +08:00
public function fetchColumn(): mixed
2023-08-29 22:47:48 +08:00
{
return $this->search('fetchColumn');
}
2023-12-13 14:47:23 +08:00
/**
* @return mixed
* @throws
*/
public function rowCount(): mixed
{
return $this->search('rowCount');
}
/**
* @return bool
* @throws Exception
*/
public function exists(): bool
{
$total = $this->search('rowCount');
if ($total === false) {
throw new Exception('Query data is has error.');
}
return $total > 0;
}
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 {
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-08-16 16:16:29 +08:00
$prepare->execute($this->params);
2023-12-13 14:47:23 +08:00
$prepare->closeCursor();
2023-12-06 18:33:25 +08:00
2023-12-13 14:47:23 +08:00
if ($method == 'rowCount') {
return $prepare->rowCount();
}
2023-12-06 18:32:21 +08:00
return $prepare->{$method}(PDO::FETCH_ASSOC);
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);
}
2023-12-13 14:47:23 +08:00
return $this->getLogger()->failure(throwable($throwable), '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 {
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();
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) {
if ($this->isRefresh($throwable)) {
return $this->_prepare();
}
2023-12-13 14:47:23 +08:00
return $this->getLogger()->failure(throwable($throwable), '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
{
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
/**
* @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-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
}