modify
This commit is contained in:
+224
-216
@@ -22,250 +22,258 @@ 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 $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($isInsert = TRUE, $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 $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, $hasAutoIncrement = null): int|bool|array|string|null
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if ($type === static::EXECUTE) {
|
if ($type === static::EXECUTE) {
|
||||||
$result = $this->insert_or_change($isInsert, $hasAutoIncrement);
|
$result = $this->insert_or_change($isInsert, $hasAutoIncrement);
|
||||||
} else {
|
} else {
|
||||||
$result = $this->search($type);
|
$result = $this->search($type);
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
return $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
|
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
|
||||||
{
|
{
|
||||||
$connect = $this->db->getConnect($this->sql);
|
$connect = $this->db->getConnect($this->sql);
|
||||||
if (!($query = $connect?->query($this->sql))) {
|
if (!($query = $connect?->query($this->sql))) {
|
||||||
return $this->addError($connect->errorInfo()[2] ?? '数据库异常, 请稍后再试.');
|
return $this->addError($connect->errorInfo()[2] ?? '数据库异常, 请稍后再试.');
|
||||||
}
|
}
|
||||||
if ($type === static::FETCH_COLUMN) {
|
if ($type === static::FETCH_COLUMN) {
|
||||||
$data = $query->fetchAll(PDO::FETCH_ASSOC);
|
$data = $query->fetchAll(PDO::FETCH_ASSOC);
|
||||||
} else if ($type === static::ROW_COUNT) {
|
} else if ($type === static::ROW_COUNT) {
|
||||||
$data = $query->rowCount();
|
$data = $query->rowCount();
|
||||||
} else if ($type === static::FETCH_ALL) {
|
} else if ($type === static::FETCH_ALL) {
|
||||||
$data = $query->fetchAll(PDO::FETCH_ASSOC);
|
$data = $query->fetchAll(PDO::FETCH_ASSOC);
|
||||||
} else {
|
} else {
|
||||||
$data = $query->fetch(PDO::FETCH_ASSOC);
|
$data = $query->fetch(PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
$query->closeCursor();
|
$query->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->initPDOStatement()) === false) {
|
if (($result = $this->initPDOStatement()) === false) {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
if ($isInsert === false) {
|
if ($isInsert === false) {
|
||||||
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 initPDOStatement(): bool|int
|
private function initPDOStatement(): 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)) {
|
||||||
$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');
|
||||||
}
|
}
|
||||||
$result = $prepare->execute($this->params);
|
$result = $this->checkResponse($prepare, $connect);
|
||||||
defer(fn() => $prepare->closeCursor());
|
$prepare->closeCursor();
|
||||||
if ($result === false) {
|
return $result;
|
||||||
return $this->addError($connect->errorInfo()[2], 'mysql');
|
}
|
||||||
}
|
|
||||||
return (int)$connect->lastInsertId();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $modelName
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setModelName($modelName): static
|
|
||||||
{
|
|
||||||
$this->_modelName = $modelName;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
private function checkResponse($prepare, $connect)
|
||||||
* @return string
|
{
|
||||||
*/
|
$result = $prepare->execute($this->params);
|
||||||
public function getModelName(): string
|
if ($result === false) {
|
||||||
{
|
return $this->addError($connect->errorInfo()[2], 'mysql');
|
||||||
return $this->_modelName;
|
}
|
||||||
}
|
return (int)$connect->lastInsertId();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @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 $modelName
|
||||||
* @param bool $insert
|
* @return $this
|
||||||
* @return int|bool|array|string|null
|
*/
|
||||||
* @throws Exception
|
public function setModelName($modelName): static
|
||||||
*/
|
{
|
||||||
public function exec($scope = null, $insert = false): int|bool|array|string|null
|
$this->_modelName = $modelName;
|
||||||
{
|
return $this;
|
||||||
return $this->execute(static::EXECUTE, $insert, $scope);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|null $data
|
* @return string
|
||||||
* @return $this
|
*/
|
||||||
*/
|
public function getModelName(): string
|
||||||
public function bindValues(array $data = NULL): static
|
{
|
||||||
{
|
return $this->_modelName;
|
||||||
if (!is_array($this->params)) {
|
}
|
||||||
$this->params = [];
|
|
||||||
}
|
|
||||||
if (!empty($data)) {
|
|
||||||
$this->params = array_merge($this->params, $data);
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $sql
|
* @return int|bool|array|string|null
|
||||||
* @return $this
|
* @throws Exception
|
||||||
* @throws Exception
|
*/
|
||||||
*/
|
public function delete(): int|bool|array|string|null
|
||||||
public function setSql($sql): static
|
{
|
||||||
{
|
return $this->execute(static::EXECUTE);
|
||||||
$this->sql = $sql;
|
}
|
||||||
return $this;
|
|
||||||
}
|
/**
|
||||||
|
* @param null $scope
|
||||||
|
* @param bool $insert
|
||||||
|
* @return int|bool|array|string|null
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function exec($scope = null, $insert = false): int|bool|array|string|null
|
||||||
|
{
|
||||||
|
return $this->execute(static::EXECUTE, $insert, $scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array|null $data
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function bindValues(array $data = NULL): 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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user