This commit is contained in:
2021-06-25 11:26:23 +08:00
parent aa72ac2eb9
commit 0db42d3a38
+252 -252
View File
@@ -14,7 +14,6 @@ use Exception;
use PDO; use PDO;
use PDOStatement; use PDOStatement;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Swoole\Coroutine;
/** /**
* Class Command * Class Command
@@ -22,291 +21,292 @@ use Swoole\Coroutine;
*/ */
class Command extends Component class Command extends Component
{ {
const ROW_COUNT = 'ROW_COUNT'; const ROW_COUNT = 'ROW_COUNT';
const FETCH = 'FETCH'; const FETCH = 'FETCH';
const FETCH_ALL = 'FETCH_ALL'; const FETCH_ALL = 'FETCH_ALL';
const EXECUTE = 'EXECUTE'; const EXECUTE = 'EXECUTE';
const FETCH_COLUMN = 'FETCH_COLUMN'; const FETCH_COLUMN = 'FETCH_COLUMN';
const DB_ERROR_MESSAGE = 'The system is busy, please try again later.'; const DB_ERROR_MESSAGE = 'The system is busy, please try again later.';
/** @var Connection */ /** @var Connection */
public Connection $db; public Connection $db;
/** @var ?string */ /** @var ?string */
public ?string $sql = ''; public ?string $sql = '';
/** @var array */ /** @var array */
public array $params = []; public array $params = [];
/** @var string */ /** @var string */
private string $_modelName; private string $_modelName;
private ?PDOStatement $prepare = null; private ?PDOStatement $prepare = null;
/** /**
* @return array|bool|int|string|PDOStatement|null * @return array|bool|int|string|PDOStatement|null
* @throws Exception * @throws Exception
*/ */
public function incrOrDecr(): array|bool|int|string|PDOStatement|null public function incrOrDecr(): array|bool|int|string|PDOStatement|null
{ {
return $this->execute(static::EXECUTE); return $this->execute(static::EXECUTE);
} }
/** /**
* @param bool $isInsert * @param bool $isInsert
* @param bool $hasAutoIncrement * @param bool|null $hasAutoIncrement
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function save($isInsert = TRUE, $hasAutoIncrement = null): int|bool|array|string|null public function save(bool $isInsert = TRUE, bool $hasAutoIncrement = null): int|bool|array|string|null
{ {
return $this->execute(static::EXECUTE, $isInsert, $hasAutoIncrement); return $this->execute(static::EXECUTE, $isInsert, $hasAutoIncrement);
} }
/** /**
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function all(): int|bool|array|string|null public function all(): int|bool|array|string|null
{ {
return $this->execute(static::FETCH_ALL); return $this->execute(static::FETCH_ALL);
} }
/** /**
* @return array|bool|int|string|null * @return array|bool|int|string|null
* @throws Exception * @throws Exception
*/ */
public function one(): null|array|bool|int|string public function one(): null|array|bool|int|string
{ {
return $this->execute(static::FETCH); return $this->execute(static::FETCH);
} }
/** /**
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function fetchColumn(): int|bool|array|string|null public function fetchColumn(): int|bool|array|string|null
{ {
return $this->execute(static::FETCH_COLUMN); return $this->execute(static::FETCH_COLUMN);
} }
/** /**
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function rowCount(): int|bool|array|string|null public function rowCount(): int|bool|array|string|null
{ {
return $this->execute(static::ROW_COUNT); return $this->execute(static::ROW_COUNT);
} }
/** /**
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function flush(): int|bool|array|string|null public function flush(): int|bool|array|string|null
{ {
return $this->execute(static::EXECUTE); return $this->execute(static::EXECUTE);
} }
/** /**
* @param $type * @param $type
* @param null $isInsert * @param null $isInsert
* @param bool $hasAutoIncrement * @param bool|null $hasAutoIncrement
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
private function execute($type, $isInsert = null, $hasAutoIncrement = null): int|bool|array|string|null private function execute($type, $isInsert = null, bool $hasAutoIncrement = null): int|bool|array|string|null
{ {
try { try {
if ($type === static::EXECUTE) { $this->debug('Execute: ' . $this->sql);
$result = $this->insert_or_change($isInsert, $hasAutoIncrement); if ($type === static::EXECUTE) {
} else { $result = $this->insert_or_change($isInsert, $hasAutoIncrement);
$result = $this->search($type); } else {
} $result = $this->search($type);
if ($this->prepare) { }
$this->prepare->closeCursor(); if ($this->prepare) {
} $this->prepare->closeCursor();
return $result; }
} catch (\Throwable $exception) { return $result;
return $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); } catch (\Throwable $exception) {
} return $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
} }
}
/** /**
* @param $type * @param $type
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
private function search($type): mixed private function search($type): mixed
{ {
if (($prepare = $this->prepare()) == false) { if (($prepare = $this->prepare()) == false) {
return false; return false;
} }
if ($type === static::FETCH_COLUMN) { if ($type === static::FETCH_COLUMN) {
$data = $prepare->fetchAll(PDO::FETCH_ASSOC); $data = $prepare->fetchAll(PDO::FETCH_ASSOC);
} else if ($type === static::ROW_COUNT) { } else if ($type === static::ROW_COUNT) {
$data = $prepare->rowCount(); $data = $prepare->rowCount();
} else if ($type === static::FETCH_ALL) { } else if ($type === static::FETCH_ALL) {
$data = $prepare->fetchAll(PDO::FETCH_ASSOC); $data = $prepare->fetchAll(PDO::FETCH_ASSOC);
} else { } else {
$data = $prepare->fetch(PDO::FETCH_ASSOC); $data = $prepare->fetch(PDO::FETCH_ASSOC);
} }
$prepare->closeCursor(); $prepare->closeCursor();
return $data; return $data;
} }
/** /**
* @param $isInsert * @param $isInsert
* @param $hasAutoIncrement * @param $hasAutoIncrement
* @return bool|string|int * @return bool|string|int
* @throws Exception * @throws Exception
*/ */
private function insert_or_change($isInsert, $hasAutoIncrement): bool|string|int private function insert_or_change($isInsert, $hasAutoIncrement): bool|string|int
{ {
if (($result = $this->getPdoStatement()) === false) { if (($result = $this->getPdoStatement()) === false) {
return $result; return $result;
} }
if ($isInsert === false || !$hasAutoIncrement) { if ($isInsert === false || !$hasAutoIncrement) {
return true; return true;
} }
if ($result == 0 && $hasAutoIncrement->isAutoIncrement()) { if ($result == 0 && $hasAutoIncrement->isAutoIncrement()) {
return $this->addError(static::DB_ERROR_MESSAGE, 'mysql'); return $this->addError(static::DB_ERROR_MESSAGE, 'mysql');
} }
return $result == 0 ? true : $result; return $result == 0 ? true : $result;
} }
/** /**
* 重新构建 * 重新构建
* @throws * @throws
*/ */
private function getPdoStatement(): bool|int private function getPdoStatement(): bool|int
{ {
if (empty($this->sql)) { if (empty($this->sql)) {
return $this->addError('no sql.', 'mysql'); return $this->addError('no sql.', 'mysql');
} }
if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) { if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) {
return $this->addError('get client error.', 'mysql'); return $this->addError('get client error.', 'mysql');
} }
if (!(($prepare = $connect->prepare($this->sql)) instanceof PDOStatement)) { if (!(($prepare = $connect->prepare($this->sql)) instanceof PDOStatement)) {
return $this->addError($this->errorMessage($prepare), 'mysql'); return $this->addError($this->errorMessage($prepare), 'mysql');
} }
$result = $this->checkResponse($prepare, $connect); $result = $this->checkResponse($prepare, $connect);
$prepare->closeCursor(); $prepare->closeCursor();
return $result; return $result;
} }
/** /**
* @param $prepare * @param $prepare
* @return string * @return string
*/ */
private function errorMessage($prepare) private function errorMessage($prepare): string
{ {
return $this->sql . ':' . ($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE); return $this->sql . ':' . ($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
} }
/** /**
* @return bool|\PDOStatement * @return bool|\PDOStatement
* @throws \Exception * @throws \Exception
*/ */
private function prepare(): bool|PDOStatement private function prepare(): bool|PDOStatement
{ {
if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) { if (!(($connect = $this->db->getConnect($this->sql)) instanceof PDO)) {
return $this->addError('get client error.', 'mysql'); return $this->addError('get client error.', 'mysql');
} }
if (!(($prepare = $connect->query($this->sql)) instanceof PDOStatement)) { if (!(($prepare = $connect->query($this->sql)) instanceof PDOStatement)) {
$error = $prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE; $error = $prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE;
return $this->addError($this->sql . ':' . $error, 'mysql'); return $this->addError($this->sql . ':' . $error, 'mysql');
} }
return $prepare; return $prepare;
} }
/** /**
* @param $prepare * @param $prepare
* @param $connect * @param $connect
* @return bool|int * @return bool|int
* @throws \Exception * @throws \Exception
*/ */
private function checkResponse($prepare, $connect) private function checkResponse($prepare, $connect): bool|int
{ {
$result = $prepare->execute($this->params); $result = $prepare->execute($this->params);
if ($result === false) { if ($result === false) {
return $this->addError($connect->errorInfo()[2], 'mysql'); return $this->addError($connect->errorInfo()[2], 'mysql');
} }
return (int)$connect->lastInsertId(); return (int)$connect->lastInsertId();
} }
/** /**
* @param $modelName * @param $modelName
* @return $this * @return $this
*/ */
public function setModelName($modelName): static public function setModelName($modelName): static
{ {
$this->_modelName = $modelName; $this->_modelName = $modelName;
return $this; return $this;
} }
/** /**
* @return string * @return string
*/ */
public function getModelName(): string public function getModelName(): string
{ {
return $this->_modelName; return $this->_modelName;
} }
/** /**
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function delete(): int|bool|array|string|null public function delete(): int|bool|array|string|null
{ {
return $this->execute(static::EXECUTE); return $this->execute(static::EXECUTE);
} }
/** /**
* @param null $scope * @param null $scope
* @param bool $insert * @param bool $insert
* @return int|bool|array|string|null * @return int|bool|array|string|null
* @throws Exception * @throws Exception
*/ */
public function exec($scope = null, $insert = false): int|bool|array|string|null public function exec($scope = null, bool $insert = false): int|bool|array|string|null
{ {
return $this->execute(static::EXECUTE, $insert, $scope); return $this->execute(static::EXECUTE, $insert, $scope);
} }
/** /**
* @param array $data * @param array $data
* @return $this * @return $this
*/ */
public function bindValues(array $data = []): static public function bindValues(array $data = []): static
{ {
if (!is_array($this->params)) { if (!is_array($this->params)) {
$this->params = []; $this->params = [];
} }
if (!empty($data)) { if (!empty($data)) {
$this->params = array_merge($this->params, $data); $this->params = array_merge($this->params, $data);
} }
return $this; return $this;
} }
/** /**
* @param $sql * @param $sql
* @return $this * @return $this
* @throws Exception * @throws Exception
*/ */
public function setSql($sql): static public function setSql($sql): static
{ {
$this->sql = $sql; $this->sql = $sql;
return $this; return $this;
} }
} }