Files
kiri-databases/Command.php
T

213 lines
4.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;
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
/** @var Connection */
public Connection $db;
/** @var ?string */
public ?string $sql = '';
/** @var array */
public array $params = [];
/** @var string */
public string $dbname = '';
/**
* @return array|bool|int|string|PDOStatement|null
* @throws Exception
*/
public function incrOrDecr(): array|bool|int|string|PDOStatement|null
{
return $this->execute(static::EXECUTE);
}
/**
* @return int|bool|array|string|null
* @throws Exception
*/
public function save(): int|bool|array|string|null
{
return $this->execute(static::EXECUTE);
}
/**
* @return int|bool|array|string|null
* @throws Exception
*/
public function all(): int|bool|array|string|null
{
return $this->execute(static::FETCH_ALL);
}
/**
* @return array|bool|int|string|null
* @throws Exception
*/
public function one(): null|array|bool|int|string
{
return $this->execute(static::FETCH);
}
/**
* @return int|bool|array|string|null
* @throws Exception
*/
public function fetchColumn(): int|bool|array|string|null
{
return $this->execute(static::FETCH_COLUMN);
}
/**
* @return int|bool|array|string|null
* @throws Exception
*/
public function rowCount(): int|bool|array|string|null
{
return $this->execute(static::ROW_COUNT);
}
/**
* @return int|bool|array|string|null
* @throws Exception
*/
public function flush(): int|bool|array|string|null
{
return $this->execute(static::EXECUTE);
}
2022-02-18 14:45:58 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param string $type
* @return int|bool|array|string|null
* @throws Exception
*/
private function execute(string $type): int|bool|array|string|null
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-02-18 14:45:58 +08:00
$result = $pdo->execute($this->sql, $this->params);
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-06-08 16:13:16 +08:00
$this->db->release($pdo);
2022-01-09 03:49:51 +08:00
return $result;
}
}
/**
* @param string $type
* @return array|int|bool|null
* @throws Exception
*/
2022-06-08 14:08:31 +08:00
private function search(string $type): array|int|bool|null
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 {
$data = $pdo->{$type}($this->sql, $this->params);
} 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
}
}
/**
* @return int|bool|array|string|null
* @throws Exception
*/
public function delete(): int|bool|array|string|null
{
return $this->execute(static::EXECUTE);
}
/**
* @return int|bool|array|string|null
* @throws Exception
*/
public function exec(): int|bool|array|string|null
{
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;
}
}