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; }