From 0f286081804e722a2357549b5b715062d08e2af1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Sun, 2 Apr 2023 20:03:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Command.php | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/Command.php b/Command.php index d5b505d..dd3812c 100644 --- a/Command.php +++ b/Command.php @@ -72,12 +72,11 @@ class Command extends Component return $statement->fetchAll(PDO::FETCH_ASSOC); } catch (\Throwable $throwable) { - if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { - $this->db->restore(); - - return $this->one(); + if (!str_contains($throwable->getMessage(), 'MySQL server has gone away')) { + return $this->all(); + } else { + return $this->printErrorMessage($throwable); } - return $this->printErrorMessage($throwable); } finally { if (isset($pdo)) { $this->db->release($pdo); @@ -98,12 +97,11 @@ class Command extends Component return $statement->fetch(PDO::FETCH_ASSOC); } catch (\Throwable $throwable) { - if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { - $this->db->restore(); - + if (!str_contains($throwable->getMessage(), 'MySQL server has gone away')) { + return $this->printErrorMessage($throwable); + } else { return $this->one(); } - return $this->printErrorMessage($throwable); } finally { if (isset($pdo)) { $this->db->release($pdo); @@ -124,12 +122,11 @@ class Command extends Component return $statement->fetchColumn(PDO::FETCH_ASSOC); } catch (\Throwable $throwable) { - if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { - $this->db->restore(); - + if (!str_contains($throwable->getMessage(), 'MySQL server has gone away')) { + return $this->printErrorMessage($throwable); + } else { return $this->fetchColumn(); } - return $this->printErrorMessage($throwable); } finally { if (isset($pdo)) { $this->db->release($pdo); @@ -150,12 +147,11 @@ class Command extends Component return $statement->rowCount(); } catch (\Throwable $throwable) { - if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { - $this->db->restore(); - + if (!str_contains($throwable->getMessage(), 'MySQL server has gone away')) { + return $this->printErrorMessage($throwable); + } else { return $this->rowCount(); } - return $this->printErrorMessage($throwable); } finally { if (isset($pdo)) { $this->db->release($pdo); @@ -186,8 +182,8 @@ class Command extends Component */ private function _execute(): bool|int { - $pdo = $this->db->getPdo(); try { + $pdo = $this->db->getPdo(); if (!(($prepare = $pdo->prepare($this->sql)) instanceof PDOStatement)) { throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE); } @@ -198,19 +194,15 @@ class Command extends Component $result = (int)$pdo->lastInsertId(); $prepare->closeCursor(); - $this->db->release($pdo); - return $result == 0 ? true : $result; } catch (\PDOException|\Throwable $throwable) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { - $this->db->restore(); - return $this->_execute(); + } else { + return $this->printErrorMessage($throwable); } - - $this->db->release($pdo); - - return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql'); + } finally { + isset($pdo) && $this->db->release($pdo); } }