From b6ae55ed7e26066da56cadbf4a345913e22cbb82 Mon Sep 17 00:00:00 2001 From: Yurun Date: Sun, 24 Mar 2024 14:46:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20MySQL=20=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E7=A6=BB=E7=BA=BF=E7=9A=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=A0=81=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Db/Drivers/TPdoDriver.php | 21 ++++++++++++++------- src/Db/Drivers/TPdoStatement.php | 9 ++++++++- src/Db/Mysql/Drivers/PdoMysql/Driver.php | 7 +++++++ src/Db/Mysql/Drivers/PdoMysql/Statement.php | 7 +++++++ 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/Db/Drivers/TPdoDriver.php b/src/Db/Drivers/TPdoDriver.php index 10812e386a..feedb6a1c0 100644 --- a/src/Db/Drivers/TPdoDriver.php +++ b/src/Db/Drivers/TPdoDriver.php @@ -45,6 +45,13 @@ trait TPdoDriver */ protected string $statementClass = ''; + /** + * 检查是否离线的错误码索引. + * + * @readonly + */ + protected int $checkIsOfflineCodeIndex = 0; + public function __construct(array $option = []) { $option['password'] ??= ''; @@ -94,7 +101,7 @@ public function ping(): bool } catch (\PDOException $e) { - if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0])) + if (isset($e->errorInfo[$this->checkIsOfflineCodeIndex]) && $this->checkCodeIsOffline($e->errorInfo[$this->checkIsOfflineCodeIndex])) { $this->close(); } @@ -166,7 +173,7 @@ public function beginTransaction(): bool } catch (\PDOException $e) { - if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0])) + if (isset($e->errorInfo[$this->checkIsOfflineCodeIndex]) && $this->checkCodeIsOffline($e->errorInfo[$this->checkIsOfflineCodeIndex])) { $this->close(); } @@ -196,7 +203,7 @@ public function commit(): bool } catch (\PDOException $e) { - if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0])) + if (isset($e->errorInfo[$this->checkIsOfflineCodeIndex]) && $this->checkCodeIsOffline($e->errorInfo[$this->checkIsOfflineCodeIndex])) { $this->close(); } @@ -219,7 +226,7 @@ public function rollBack(?int $levels = null): bool } catch (\PDOException $e) { - if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0])) + if (isset($e->errorInfo[$this->checkIsOfflineCodeIndex]) && $this->checkCodeIsOffline($e->errorInfo[$this->checkIsOfflineCodeIndex])) { $this->close(); } @@ -328,7 +335,7 @@ public function exec(string $sql): int } catch (\PDOException $e) { - if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0])) + if (isset($e->errorInfo[$this->checkIsOfflineCodeIndex]) && $this->checkCodeIsOffline($e->errorInfo[$this->checkIsOfflineCodeIndex])) { $this->close(); } @@ -405,7 +412,7 @@ public function prepare(string $sql, array $driverOptions = []): IStatement } catch (\PDOException $e) { - if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0])) + if (isset($e->errorInfo[$this->checkIsOfflineCodeIndex]) && $this->checkCodeIsOffline($e->errorInfo[$this->checkIsOfflineCodeIndex])) { $this->close(); } @@ -439,7 +446,7 @@ public function query(string $sql): IStatement } catch (\PDOException $e) { - if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0])) + if (isset($e->errorInfo[$this->checkIsOfflineCodeIndex]) && $this->checkCodeIsOffline($e->errorInfo[$this->checkIsOfflineCodeIndex])) { $this->close(); } diff --git a/src/Db/Drivers/TPdoStatement.php b/src/Db/Drivers/TPdoStatement.php index 7e7de8fe5e..62067dd4bc 100644 --- a/src/Db/Drivers/TPdoStatement.php +++ b/src/Db/Drivers/TPdoStatement.php @@ -32,6 +32,13 @@ trait TPdoStatement */ protected $lastInsertId = ''; + /** + * 检查是否离线的错误码索引. + * + * @readonly + */ + protected int $checkIsOfflineCodeIndex = 0; + public function __construct(IDb $db, \PDOStatement $statement, bool $isExecuted = false) { $this->db = $db; @@ -158,7 +165,7 @@ public function execute(array $inputParameters = null): bool } catch (\PDOException $e) { - if (isset($e->errorInfo[0]) && $this->db->checkCodeIsOffline($e->errorInfo[0])) + if (isset($e->errorInfo[$this->checkIsOfflineCodeIndex]) && $this->db->checkCodeIsOffline($e->errorInfo[$this->checkIsOfflineCodeIndex])) { $this->db->close(); } diff --git a/src/Db/Mysql/Drivers/PdoMysql/Driver.php b/src/Db/Mysql/Drivers/PdoMysql/Driver.php index 3ae057ca4f..13695090fb 100644 --- a/src/Db/Mysql/Drivers/PdoMysql/Driver.php +++ b/src/Db/Mysql/Drivers/PdoMysql/Driver.php @@ -20,6 +20,13 @@ class Driver extends MysqlBase __construct as private tPdoDriverConstruct; } + /** + * 检查是否离线的错误码索引. + * + * @readonly + */ + protected int $checkIsOfflineCodeIndex = 1; + public function __construct(array $option = []) { $option['username'] ??= 'root'; diff --git a/src/Db/Mysql/Drivers/PdoMysql/Statement.php b/src/Db/Mysql/Drivers/PdoMysql/Statement.php index c25c6e42b5..33c205473a 100644 --- a/src/Db/Mysql/Drivers/PdoMysql/Statement.php +++ b/src/Db/Mysql/Drivers/PdoMysql/Statement.php @@ -16,4 +16,11 @@ class Statement extends MysqlBaseStatement implements IMysqlStatement { use TPdoStatement; + + /** + * 检查是否离线的错误码索引. + * + * @readonly + */ + protected int $checkIsOfflineCodeIndex = 1; }