2021-08-11 15:03:28 +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 Kiri\Core\Json;
|
2021-08-16 18:10:50 +08:00
|
|
|
use PDO;
|
|
|
|
|
use PDOStatement;
|
2021-08-11 15:03:28 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Command
|
|
|
|
|
* @package Database
|
|
|
|
|
*/
|
|
|
|
|
class Command extends Component
|
|
|
|
|
{
|
|
|
|
|
const ROW_COUNT = 'ROW_COUNT';
|
|
|
|
|
const FETCH = 'FETCH';
|
|
|
|
|
const FETCH_ALL = 'FETCH_ALL';
|
|
|
|
|
const EXECUTE = 'EXECUTE';
|
|
|
|
|
const FETCH_COLUMN = 'FETCH_COLUMN';
|
|
|
|
|
|
|
|
|
|
/** @var Connection */
|
|
|
|
|
public Connection $db;
|
|
|
|
|
|
|
|
|
|
/** @var ?string */
|
|
|
|
|
public ?string $sql = '';
|
|
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
|
public array $params = [];
|
|
|
|
|
|
2021-08-16 18:10:50 +08:00
|
|
|
/** @var string */
|
2021-08-11 15:03:28 +08:00
|
|
|
public string $dbname = '';
|
|
|
|
|
|
2021-08-16 18:10:50 +08:00
|
|
|
/** @var PDOStatement|null */
|
2021-08-11 15:03:28 +08:00
|
|
|
private ?PDOStatement $prepare = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array|bool|int|string|PDOStatement|null
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function incrOrDecr(): array|bool|int|string|PDOStatement|null
|
|
|
|
|
{
|
|
|
|
|
return $this->execute(static::EXECUTE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param bool $isInsert
|
|
|
|
|
* @param mixed $hasAutoIncrement
|
|
|
|
|
* @return int|bool|array|string|null
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function save(bool $isInsert = TRUE, mixed $hasAutoIncrement = null): int|bool|array|string|null
|
|
|
|
|
{
|
|
|
|
|
return $this->execute(static::EXECUTE, $isInsert, $hasAutoIncrement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $type
|
|
|
|
|
* @param null $isInsert
|
|
|
|
|
* @param bool|null $hasAutoIncrement
|
|
|
|
|
* @return int|bool|array|string|null
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function execute($type, $isInsert = null, mixed $hasAutoIncrement = null): int|bool|array|string|null
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$time = microtime(true);
|
|
|
|
|
if ($type === static::EXECUTE) {
|
|
|
|
|
$result = $this->insert_or_change($isInsert, $hasAutoIncrement);
|
|
|
|
|
} else {
|
|
|
|
|
$result = $this->search($type);
|
|
|
|
|
}
|
|
|
|
|
if (microtime(true) - $time >= 0.02) {
|
|
|
|
|
$this->warning('Mysql:' . Json::encode([$this->sql, $this->params]) . (microtime(true) - $time));
|
|
|
|
|
}
|
|
|
|
|
$this->prepare?->closeCursor();
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
$result = $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
|
|
|
|
|
} finally {
|
|
|
|
|
$this->db->release();
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $type
|
2021-08-16 18:10:50 +08:00
|
|
|
* @return array|int
|
2021-08-11 15:03:28 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-08-16 18:10:50 +08:00
|
|
|
private function search($type): array|int
|
2021-08-11 15:03:28 +08:00
|
|
|
{
|
2021-08-16 18:10:50 +08:00
|
|
|
$pdo = $this->db->getConnect($this->sql);
|
2021-08-11 15:03:28 +08:00
|
|
|
if ($type === static::FETCH_COLUMN) {
|
2021-08-16 18:10:50 +08:00
|
|
|
$data = $pdo->fetchColumn($this->sql, $this->params);
|
2021-08-11 15:03:28 +08:00
|
|
|
} else if ($type === static::ROW_COUNT) {
|
2021-08-16 18:10:50 +08:00
|
|
|
$data = $pdo->count($this->sql, $this->params);
|
2021-08-11 15:03:28 +08:00
|
|
|
} else if ($type === static::FETCH_ALL) {
|
2021-08-16 18:10:50 +08:00
|
|
|
$data = $pdo->fetchAll($this->sql, $this->params);
|
2021-08-11 15:03:28 +08:00
|
|
|
} else {
|
2021-08-16 18:10:50 +08:00
|
|
|
$data = $pdo->fetch($this->sql, $this->params);
|
2021-08-11 15:03:28 +08:00
|
|
|
}
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $isInsert
|
|
|
|
|
* @param $hasAutoIncrement
|
2021-08-16 18:10:50 +08:00
|
|
|
* @return bool|int
|
2021-08-11 15:03:28 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-08-16 18:10:50 +08:00
|
|
|
private function insert_or_change($isInsert, $hasAutoIncrement): bool|int
|
2021-08-11 15:03:28 +08:00
|
|
|
{
|
2021-08-16 18:10:50 +08:00
|
|
|
$pdo = $this->db->getConnect($this->sql);
|
|
|
|
|
$result = $pdo->execute($this->sql, $this->params);
|
|
|
|
|
if (($hasAutoIncrement || !$isInsert) && $result == 0){
|
|
|
|
|
return false;
|
2021-08-11 15:03:28 +08:00
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int|bool|array|string|null
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function delete(): int|bool|array|string|null
|
|
|
|
|
{
|
|
|
|
|
return $this->execute(static::EXECUTE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param null $scope
|
|
|
|
|
* @param bool $insert
|
|
|
|
|
* @return int|bool|array|string|null
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function exec($scope = null, bool $insert = false): int|bool|array|string|null
|
|
|
|
|
{
|
|
|
|
|
return $this->execute(static::EXECUTE, $insert, $scope);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function bindValues(array $data = []): static
|
|
|
|
|
{
|
|
|
|
|
if (!is_array($this->params)) {
|
|
|
|
|
$this->params = [];
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|