From 82f38732efa391c4b6b3b73c71853312a3f357f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Wed, 11 Oct 2023 23:51:54 +0800 Subject: [PATCH] eee --- Command.php | 56 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/Command.php b/Command.php index 1fa74fa..c5bdeb0 100644 --- a/Command.php +++ b/Command.php @@ -145,15 +145,7 @@ class Command extends Component private function _execute(): bool|int { try { - $client = $this->connection->getConnection(); - if (($prepare = $client->prepare($this->sql)) === false) { - throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]); - } - if ($prepare->execute($this->params) === false) { - throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]); - } - $result = $client->lastInsertId(); - $prepare->closeCursor(); + [$client, $prepare, $result] = $this->_prepare(); if ($prepare->rowCount() < 1) { return trigger_print_error("更新失败", 'mysql'); } @@ -171,6 +163,50 @@ class Command extends Component } + /** + * @return bool|int + * @throws ConfigException + * @throws Exception + */ + private function _delete(): bool|int + { + try { + [$client, $prepare, $result] = $this->_prepare(); + + $this->connection->release($client); + return $result == 0 ? true : (int)$result; + } catch (Throwable $throwable) { + if ($this->isRefresh($throwable)) { + return $this->_execute(); + } + if (isset($client)) { + $this->connection->release($client); + } + return $this->error($throwable); + } + } + + + /** + * @return array + * @throws Exception + */ + private function _prepare(): array + { + $client = $this->connection->getConnection(); + if (($prepare = $client->prepare($this->sql)) === false) { + throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]); + } + if ($prepare->execute($this->params) === false) { + throw new Exception('(' . $prepare->errorInfo()[0] . ')' . $prepare->errorInfo()[2]); + } + $result = $client->lastInsertId(); + $prepare->closeCursor(); + + return [$client, $prepare, $result]; + } + + /** * @param Throwable $throwable * @return bool @@ -206,7 +242,7 @@ class Command extends Component */ public function delete(): int|bool { - return $this->_execute(); + return $this->_delete(); } /**