Skip to content

Commit

Permalink
修复 MySQL 检查是否离线的错误码索引
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Mar 24, 2024
1 parent 55eabb4 commit b6ae55e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/Db/Drivers/TPdoDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ trait TPdoDriver
*/
protected string $statementClass = '';

/**
* 检查是否离线的错误码索引.
*
* @readonly
*/
protected int $checkIsOfflineCodeIndex = 0;

public function __construct(array $option = [])
{
$option['password'] ??= '';
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down
9 changes: 8 additions & 1 deletion src/Db/Drivers/TPdoStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
7 changes: 7 additions & 0 deletions src/Db/Mysql/Drivers/PdoMysql/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
7 changes: 7 additions & 0 deletions src/Db/Mysql/Drivers/PdoMysql/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@
class Statement extends MysqlBaseStatement implements IMysqlStatement
{
use TPdoStatement;

/**
* 检查是否离线的错误码索引.
*
* @readonly
*/
protected int $checkIsOfflineCodeIndex = 1;
}

0 comments on commit b6ae55e

Please sign in to comment.