Files
kiri-databases/Command.php
T

262 lines
5.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;
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 = [];
/** @var string */
public string $dbname = '';
/**
2022-09-20 11:17:59 +08:00
* @return mixed
2022-01-09 03:49:51 +08:00
* @throws Exception
*/
2022-09-20 11:17:59 +08:00
public function incrOrDecr(): mixed
2022-01-09 03:49:51 +08:00
{
return $this->execute(static::EXECUTE);
}
/**
2022-09-20 11:17:59 +08:00
* @return mixed
2022-01-09 03:49:51 +08:00
* @throws Exception
*/
2022-09-20 11:17:59 +08:00
public function save(): mixed
2022-01-09 03:49:51 +08:00
{
return $this->execute(static::EXECUTE);
}
/**
2022-09-20 11:17:59 +08:00
* @return mixed
2022-01-09 03:49:51 +08:00
* @throws Exception
*/
2022-09-20 11:17:59 +08:00
public function all(): mixed
2022-01-09 03:49:51 +08:00
{
return $this->execute(static::FETCH_ALL);
}
/**
2022-09-20 11:17:59 +08:00
* @return mixed
2022-01-09 03:49:51 +08:00
* @throws Exception
*/
2022-09-20 11:17:59 +08:00
public function one(): mixed
2022-01-09 03:49:51 +08:00
{
return $this->execute(static::FETCH);
}
/**
2022-09-20 11:17:59 +08:00
* @return mixed
2022-01-09 03:49:51 +08:00
* @throws Exception
*/
2022-09-20 11:17:59 +08:00
public function fetchColumn(): mixed
2022-01-09 03:49:51 +08:00
{
return $this->execute(static::FETCH_COLUMN);
}
/**
2022-09-20 11:17:59 +08:00
* @return mixed
2022-01-09 03:49:51 +08:00
* @throws Exception
*/
2022-09-20 11:17:59 +08:00
public function rowCount(): mixed
2022-01-09 03:49:51 +08:00
{
return $this->execute(static::ROW_COUNT);
}
/**
2022-09-20 11:17:59 +08:00
* @return mixed
2022-01-09 03:49:51 +08:00
* @throws Exception
*/
2022-09-20 11:17:59 +08:00
public function flush(): mixed
2022-01-09 03:49:51 +08:00
{
return $this->execute(static::EXECUTE);
}
2022-02-18 14:45:58 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param string $type
2022-09-20 11:17:59 +08:00
* @return mixed
2022-01-09 03:49:51 +08:00
* @throws Exception
*/
2022-09-20 11:17:59 +08:00
private function execute(string $type): mixed
2022-02-18 14:45:58 +08:00
{
$time = microtime(true);
2022-06-08 14:14:17 +08:00
$result = $type !== static::EXECUTE ? $this->search($type) : $this->_execute();
$this->logger->debug('Mysql:' . $this->print_r($time));
return $result;
2022-02-18 14:45:58 +08:00
}
/**
2022-06-08 14:14:17 +08:00
* @param $time
* @return string
2022-02-18 14:45:58 +08:00
*/
2022-06-08 14:14:17 +08:00
private function print_r($time): string
2022-02-18 14:45:58 +08:00
{
2022-06-08 15:13:02 +08:00
return print_r(['time' => microtime(true) - $time, 'sql' => $this->sql, 'param' => $this->params], true);
2022-02-18 14:45:58 +08:00
}
/**
* @return bool|int
* @throws Exception
*/
2022-06-08 14:08:31 +08:00
private function _execute(): bool|int
2022-01-09 03:49:51 +08:00
{
2022-06-08 16:13:16 +08:00
$pdo = $this->db->getPdo();
2022-01-09 03:49:51 +08:00
try {
2022-09-20 18:53:49 +08:00
if (!(($prepare = $pdo->prepare($this->sql)) instanceof PDOStatement)) {
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
}
if ($prepare->execute($this->params) === false) {
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
}
$result = (int)$pdo->lastInsertId();
$prepare->closeCursor();
$result = $result == 0 ? true : $result;
2022-01-09 03:49:51 +08:00
} catch (\Throwable $exception) {
2022-02-23 16:32:08 +08:00
$result = $this->logger->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
2022-01-09 03:49:51 +08:00
} finally {
2022-09-19 18:24:12 +08:00
$this->db->release($pdo, true);
2022-01-09 03:49:51 +08:00
return $result;
}
}
2022-09-20 18:53:49 +08:00
/**
* @param \PDO $pdo
* @param string $sql
* @param array $params
* @return PDOStatement
* @throws Exception
*/
private function queryPrev(\PDO $pdo): PDOStatement
{
try {
if (($statement = $pdo->query($this->sql)) === false) {
throw new Exception($pdo->errorInfo()[1]);
}
foreach ($this->params as $key => $param) {
$statement->bindValue($key, $param);
}
return $statement;
} catch (\PDOException|\Throwable $throwable) {
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
unset($pdo);
return $this->queryPrev($this->db->getSlaveClient());
}
throw new Exception($throwable->getMessage());
}
}
2022-01-09 03:49:51 +08:00
/**
* @param string $type
* @return array|int|bool|null
* @throws Exception
*/
2022-09-20 11:17:59 +08:00
private function search(string $type): mixed
2022-01-09 03:49:51 +08:00
{
2022-06-08 16:13:16 +08:00
$pdo = $this->db->getSlaveClient();
2022-02-18 14:45:58 +08:00
try {
2022-09-20 18:53:49 +08:00
if ($type == self::FETCH_ALL) {
$data = $this->queryPrev($pdo)->fetchAll(\PDO::FETCH_ASSOC);
} else if ($type == self::FETCH) {
$data = $this->queryPrev($pdo)->fetch(\PDO::FETCH_ASSOC);
} else if ($type == self::FETCH_COLUMN) {
$data = $this->queryPrev($pdo)->fetchColumn(\PDO::FETCH_ASSOC);
} else if ($type == self::ROW_COUNT) {
$data = $this->queryPrev($pdo)->rowCount();
} else {
$data = null;
}
2022-02-18 14:45:58 +08:00
} catch (\Throwable $throwable) {
2022-02-23 16:32:08 +08:00
$data = $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
2022-02-18 14:45:58 +08:00
} finally {
2022-06-08 15:13:53 +08:00
$this->db->release($pdo, false);
2022-02-18 14:45:58 +08:00
return $data;
2022-01-09 03:49:51 +08:00
}
}
/**
2022-09-20 11:17:59 +08:00
* @return mixed
2022-01-09 03:49:51 +08:00
* @throws Exception
*/
2022-09-20 11:17:59 +08:00
public function delete(): mixed
2022-01-09 03:49:51 +08:00
{
return $this->execute(static::EXECUTE);
}
/**
2022-09-20 11:17:59 +08:00
* @return mixed
2022-01-09 03:49:51 +08:00
* @throws Exception
*/
2022-09-20 11:17:59 +08:00
public function exec(): mixed
2022-01-09 03:49:51 +08:00
{
return $this->execute(static::EXECUTE);
}
/**
* @param array $data
* @return $this
*/
public function bindValues(array $data = []): static
{
if (!empty($data)) {
$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;
}
}