This commit is contained in:
2023-04-02 20:03:19 +08:00
parent 67fdee8fd9
commit 0f28608180
+18 -26
View File
@@ -72,12 +72,11 @@ class Command extends Component
return $statement->fetchAll(PDO::FETCH_ASSOC); return $statement->fetchAll(PDO::FETCH_ASSOC);
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { if (!str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
$this->db->restore(); return $this->all();
} else {
return $this->one();
}
return $this->printErrorMessage($throwable); return $this->printErrorMessage($throwable);
}
} finally { } finally {
if (isset($pdo)) { if (isset($pdo)) {
$this->db->release($pdo); $this->db->release($pdo);
@@ -98,12 +97,11 @@ class Command extends Component
return $statement->fetch(PDO::FETCH_ASSOC); return $statement->fetch(PDO::FETCH_ASSOC);
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { if (!str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
$this->db->restore(); return $this->printErrorMessage($throwable);
} else {
return $this->one(); return $this->one();
} }
return $this->printErrorMessage($throwable);
} finally { } finally {
if (isset($pdo)) { if (isset($pdo)) {
$this->db->release($pdo); $this->db->release($pdo);
@@ -124,12 +122,11 @@ class Command extends Component
return $statement->fetchColumn(PDO::FETCH_ASSOC); return $statement->fetchColumn(PDO::FETCH_ASSOC);
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { if (!str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
$this->db->restore(); return $this->printErrorMessage($throwable);
} else {
return $this->fetchColumn(); return $this->fetchColumn();
} }
return $this->printErrorMessage($throwable);
} finally { } finally {
if (isset($pdo)) { if (isset($pdo)) {
$this->db->release($pdo); $this->db->release($pdo);
@@ -150,12 +147,11 @@ class Command extends Component
return $statement->rowCount(); return $statement->rowCount();
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { if (!str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
$this->db->restore(); return $this->printErrorMessage($throwable);
} else {
return $this->rowCount(); return $this->rowCount();
} }
return $this->printErrorMessage($throwable);
} finally { } finally {
if (isset($pdo)) { if (isset($pdo)) {
$this->db->release($pdo); $this->db->release($pdo);
@@ -186,8 +182,8 @@ class Command extends Component
*/ */
private function _execute(): bool|int private function _execute(): bool|int
{ {
$pdo = $this->db->getPdo();
try { try {
$pdo = $this->db->getPdo();
if (!(($prepare = $pdo->prepare($this->sql)) instanceof PDOStatement)) { if (!(($prepare = $pdo->prepare($this->sql)) instanceof PDOStatement)) {
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE); throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
} }
@@ -198,19 +194,15 @@ class Command extends Component
$result = (int)$pdo->lastInsertId(); $result = (int)$pdo->lastInsertId();
$prepare->closeCursor(); $prepare->closeCursor();
$this->db->release($pdo);
return $result == 0 ? true : $result; return $result == 0 ? true : $result;
} catch (\PDOException|\Throwable $throwable) { } catch (\PDOException|\Throwable $throwable) {
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
$this->db->restore();
return $this->_execute(); return $this->_execute();
} else {
return $this->printErrorMessage($throwable);
} }
} finally {
$this->db->release($pdo); isset($pdo) && $this->db->release($pdo);
return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
} }
} }