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(); } /**