Skip to content

Commit

Permalink
重构为 sqlState+驱动特定错误的方式判断是否为掉线
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Mar 24, 2024
1 parent 73045da commit 3ad7f9f
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/Components/pgsql/src/Db/Contract/IPgsqlDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ interface IPgsqlDb extends IDb
*
* @param string|null $code
*/
public function checkCodeIsOffline($code): bool;
public function checkCodeIsOffline(string $sqlState, $code = null): bool;
}
4 changes: 4 additions & 0 deletions src/Components/pgsql/src/Db/Drivers/Swoole/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ public function errorCode()
{
return $this->instance->resultDiag['sqlstate'] ?? null;
}
elseif (null !== $this->instance->error)

Check warning on line 271 in src/Components/pgsql/src/Db/Drivers/Swoole/Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Components/pgsql/src/Db/Drivers/Swoole/Driver.php#L271

Added line #L271 was not covered by tests
{
return '08006'; // connection_failure

Check warning on line 273 in src/Components/pgsql/src/Db/Drivers/Swoole/Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Components/pgsql/src/Db/Drivers/Swoole/Driver.php#L273

Added line #L273 was not covered by tests
}
else
{
return '';
Expand Down
4 changes: 4 additions & 0 deletions src/Components/pgsql/src/Db/Drivers/SwooleNew/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ public function errorCode()
{
return $this->instance->resultDiag['sqlstate'] ?? null;
}
elseif (null !== $this->instance->error)

Check warning on line 264 in src/Components/pgsql/src/Db/Drivers/SwooleNew/Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Components/pgsql/src/Db/Drivers/SwooleNew/Driver.php#L264

Added line #L264 was not covered by tests
{
return '08006'; // connection_failure

Check warning on line 266 in src/Components/pgsql/src/Db/Drivers/SwooleNew/Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Components/pgsql/src/Db/Drivers/SwooleNew/Driver.php#L266

Added line #L266 was not covered by tests
}
else
{
return '';
Expand Down
4 changes: 4 additions & 0 deletions src/Components/pgsql/src/Db/Drivers/SwooleNew/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public function errorCode()
{
return $this->stmt->resultDiag['sqlstate'] ?? null;
}
elseif (null !== $this->stmt->error)

Check warning on line 135 in src/Components/pgsql/src/Db/Drivers/SwooleNew/Statement.php

View check run for this annotation

Codecov / codecov/patch

src/Components/pgsql/src/Db/Drivers/SwooleNew/Statement.php#L135

Added line #L135 was not covered by tests
{
return '08006'; // connection_failure

Check warning on line 137 in src/Components/pgsql/src/Db/Drivers/SwooleNew/Statement.php

View check run for this annotation

Codecov / codecov/patch

src/Components/pgsql/src/Db/Drivers/SwooleNew/Statement.php#L137

Added line #L137 was not covered by tests
}
else
{
return '';
Expand Down
7 changes: 5 additions & 2 deletions src/Components/pgsql/src/Db/PgsqlBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Imi\Db\Drivers\Base;
use Imi\Db\Query\Interfaces\IQuery;
use Imi\Db\SqlState;
use Imi\Pgsql\Db\Contract\IPgsqlDb;
use Imi\Pgsql\Db\Query\PgsqlQuery;

Expand All @@ -30,10 +31,12 @@ public function getDbType(): string
/**
* {@inheritDoc}
*
* 只列出了 Postgres 代码中已使用的错误码
*
* @see http://www.postgres.cn/docs/13/errcodes-appendix.html
*/
public function checkCodeIsOffline($code): bool
public function checkCodeIsOffline(string $sqlState, $code = null): bool

Check warning on line 38 in src/Components/pgsql/src/Db/PgsqlBase.php

View check run for this annotation

Codecov / codecov/patch

src/Components/pgsql/src/Db/PgsqlBase.php#L38

Added line #L38 was not covered by tests
{
return null === $code || '57P01' === $code;
return SqlState::checkCodeIsOffline($sqlState);

Check warning on line 40 in src/Components/pgsql/src/Db/PgsqlBase.php

View check run for this annotation

Codecov / codecov/patch

src/Components/pgsql/src/Db/PgsqlBase.php#L40

Added line #L40 was not covered by tests
}
}
14 changes: 7 additions & 7 deletions src/Components/swoole/src/Db/Driver/Swoole/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function ping(): bool
{
return true;
}
if ($this->checkCodeIsOffline($instance->errno))
if ($this->checkCodeIsOffline('HY000', $instance->errno))

Check warning on line 97 in src/Components/swoole/src/Db/Driver/Swoole/Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Components/swoole/src/Db/Driver/Swoole/Driver.php#L97

Added line #L97 was not covered by tests
{
$this->close();
}
Expand Down Expand Up @@ -170,7 +170,7 @@ public function beginTransaction(): bool
{
if (!$this->inTransaction() && !$this->instance->begin())
{
if ($this->checkCodeIsOffline($this->instance->errno))
if ($this->checkCodeIsOffline('HY000', $this->instance->errno))

Check warning on line 173 in src/Components/swoole/src/Db/Driver/Swoole/Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Components/swoole/src/Db/Driver/Swoole/Driver.php#L173

Added line #L173 was not covered by tests
{
$this->close();
}
Expand All @@ -190,7 +190,7 @@ public function commit(): bool
{
if (!$this->instance->commit())
{
if ($this->checkCodeIsOffline($this->instance->errno))
if ($this->checkCodeIsOffline('HY000', $this->instance->errno))

Check warning on line 193 in src/Components/swoole/src/Db/Driver/Swoole/Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Components/swoole/src/Db/Driver/Swoole/Driver.php#L193

Added line #L193 was not covered by tests
{
$this->close();
}
Expand Down Expand Up @@ -219,7 +219,7 @@ public function rollBack(?int $levels = null): bool
{
$this->getTransaction()->rollBack($levels);
}
elseif ($this->checkCodeIsOffline($this->instance->errno))
elseif ($this->checkCodeIsOffline('HY000', $this->instance->errno))

Check warning on line 222 in src/Components/swoole/src/Db/Driver/Swoole/Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Components/swoole/src/Db/Driver/Swoole/Driver.php#L222

Added line #L222 was not covered by tests
{
$this->close();
}
Expand Down Expand Up @@ -297,7 +297,7 @@ public function exec(string $sql): int
$this->lastStmt = null;
$this->lastSql = $sql;
$instance = $this->instance;
if (false === $instance->query($sql) && $this->checkCodeIsOffline($this->instance->errno))
if (false === $instance->query($sql) && $this->checkCodeIsOffline('HY000', $this->instance->errno))
{
$this->close();

Expand Down Expand Up @@ -372,7 +372,7 @@ public function prepare(string $sql, array $driverOptions = []): IMysqlStatement
{
$errorCode = $this->errorCode();
$errorInfo = $this->errorInfo();
if ($this->checkCodeIsOffline($errorCode))
if ($this->checkCodeIsOffline('HY000', $errorCode))

Check warning on line 375 in src/Components/swoole/src/Db/Driver/Swoole/Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Components/swoole/src/Db/Driver/Swoole/Driver.php#L375

Added line #L375 was not covered by tests
{
$this->close();
}
Expand Down Expand Up @@ -400,7 +400,7 @@ public function query(string $sql): IMysqlStatement
{
$errorCode = $this->errorCode();
$errorInfo = $this->errorInfo();
if ($this->checkCodeIsOffline($errorCode))
if ($this->checkCodeIsOffline('HY000', $errorCode))

Check warning on line 403 in src/Components/swoole/src/Db/Driver/Swoole/Driver.php

View check run for this annotation

Codecov / codecov/patch

src/Components/swoole/src/Db/Driver/Swoole/Driver.php#L403

Added line #L403 was not covered by tests
{
$this->close();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Components/swoole/src/Db/Driver/Swoole/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function execute(array $inputParameters = null): bool
$dbInstance = $db->getInstance();
$errorCode = $dbInstance->errorCode();
$errorInfo = $dbInstance->errorInfo();
if ($db->checkCodeIsOffline($errorCode))
if ($db->checkCodeIsOffline('HY000', $errorCode))

Check warning on line 162 in src/Components/swoole/src/Db/Driver/Swoole/Statement.php

View check run for this annotation

Codecov / codecov/patch

src/Components/swoole/src/Db/Driver/Swoole/Statement.php#L162

Added line #L162 was not covered by tests
{
$db->close();
}
Expand Down Expand Up @@ -221,7 +221,7 @@ public function execute(array $inputParameters = null): bool
{
$errorCode = $this->errorCode();
$errorInfo = $this->errorInfo();
if ($this->db->checkCodeIsOffline($errorCode))
if ($this->db->checkCodeIsOffline('HY000', $errorCode))

Check warning on line 224 in src/Components/swoole/src/Db/Driver/Swoole/Statement.php

View check run for this annotation

Codecov / codecov/patch

src/Components/swoole/src/Db/Driver/Swoole/Statement.php#L224

Added line #L224 was not covered by tests
{
$this->db->close();
}
Expand Down
44 changes: 28 additions & 16 deletions src/Db/Drivers/TPdoDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ public function ping(): bool
{
return true;
}
// @phpstan-ignore-next-line
if ($this->checkCodeIsOffline($instance->errorCode()))
$errorInfo = $instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))

Check warning on line 90 in src/Db/Drivers/TPdoDriver.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoDriver.php#L89-L90

Added lines #L89 - L90 were not covered by tests
{
$this->close();
}
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[self::OFFLINE_CODE_INDEX]) && $this->checkCodeIsOffline($e->errorInfo[self::OFFLINE_CODE_INDEX]))
$errorInfo = $instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))

Check warning on line 98 in src/Db/Drivers/TPdoDriver.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoDriver.php#L97-L98

Added lines #L97 - L98 were not covered by tests
{
$this->close();
}
Expand Down Expand Up @@ -149,12 +150,13 @@ public function getInstance(): ?\PDO
*/
public function beginTransaction(): bool
{
$instance = $this->instance;
try
{
if (!$this->inTransaction() && !$this->instance->beginTransaction())
if (!$this->inTransaction() && !$instance->beginTransaction())
{
// @phpstan-ignore-next-line
if ($this->checkCodeIsOffline($this->instance->errorCode()))
$errorInfo = $instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))

Check warning on line 159 in src/Db/Drivers/TPdoDriver.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoDriver.php#L158-L159

Added lines #L158 - L159 were not covered by tests
{
$this->close();
}
Expand All @@ -166,7 +168,8 @@ public function beginTransaction(): bool
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[self::OFFLINE_CODE_INDEX]) && $this->checkCodeIsOffline($e->errorInfo[self::OFFLINE_CODE_INDEX]))
$errorInfo = $instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))

Check warning on line 172 in src/Db/Drivers/TPdoDriver.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoDriver.php#L171-L172

Added lines #L171 - L172 were not covered by tests
{
$this->close();
}
Expand All @@ -185,8 +188,8 @@ public function commit(): bool
{
if (!$this->instance->commit())
{
// @phpstan-ignore-next-line
if ($this->checkCodeIsOffline($this->instance->errorCode()))
$errorInfo = $this->instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))

Check warning on line 192 in src/Db/Drivers/TPdoDriver.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoDriver.php#L191-L192

Added lines #L191 - L192 were not covered by tests
{
$this->close();
}
Expand All @@ -196,7 +199,8 @@ public function commit(): bool
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[self::OFFLINE_CODE_INDEX]) && $this->checkCodeIsOffline($e->errorInfo[self::OFFLINE_CODE_INDEX]))
$errorInfo = $this->instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))

Check warning on line 203 in src/Db/Drivers/TPdoDriver.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoDriver.php#L202-L203

Added lines #L202 - L203 were not covered by tests
{
$this->close();
}
Expand All @@ -219,7 +223,8 @@ public function rollBack(?int $levels = null): bool
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[self::OFFLINE_CODE_INDEX]) && $this->checkCodeIsOffline($e->errorInfo[self::OFFLINE_CODE_INDEX]))
$errorInfo = $this->instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))

Check warning on line 227 in src/Db/Drivers/TPdoDriver.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoDriver.php#L226-L227

Added lines #L226 - L227 were not covered by tests
{
$this->close();
}
Expand All @@ -236,9 +241,13 @@ public function rollBack(?int $levels = null): bool
$this->getTransaction()->rollBack($levels);
}
// @phpstan-ignore-next-line
elseif ($this->checkCodeIsOffline($this->instance->errorCode()))
else
{
$this->close();
$errorInfo = $this->instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))

Check warning on line 247 in src/Db/Drivers/TPdoDriver.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoDriver.php#L246-L247

Added lines #L246 - L247 were not covered by tests
{
$this->close();

Check warning on line 249 in src/Db/Drivers/TPdoDriver.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoDriver.php#L249

Added line #L249 was not covered by tests
}
}

return $result;
Expand Down Expand Up @@ -328,7 +337,8 @@ public function exec(string $sql): int
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[self::OFFLINE_CODE_INDEX]) && $this->checkCodeIsOffline($e->errorInfo[self::OFFLINE_CODE_INDEX]))
$errorInfo = $this->instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))

Check warning on line 341 in src/Db/Drivers/TPdoDriver.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoDriver.php#L340-L341

Added lines #L340 - L341 were not covered by tests
{
$this->close();
}
Expand Down Expand Up @@ -405,7 +415,8 @@ public function prepare(string $sql, array $driverOptions = []): IStatement
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[self::OFFLINE_CODE_INDEX]) && $this->checkCodeIsOffline($e->errorInfo[self::OFFLINE_CODE_INDEX]))
$errorInfo = $this->instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))

Check warning on line 419 in src/Db/Drivers/TPdoDriver.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoDriver.php#L418-L419

Added lines #L418 - L419 were not covered by tests
{
$this->close();
}
Expand Down Expand Up @@ -439,7 +450,8 @@ public function query(string $sql): IStatement
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[self::OFFLINE_CODE_INDEX]) && $this->checkCodeIsOffline($e->errorInfo[self::OFFLINE_CODE_INDEX]))
$errorInfo = $this->instance->errorInfo();
if ($this->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))

Check warning on line 454 in src/Db/Drivers/TPdoDriver.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoDriver.php#L453-L454

Added lines #L453 - L454 were not covered by tests
{
$this->close();
}
Expand Down
11 changes: 6 additions & 5 deletions src/Db/Drivers/TPdoStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ public function getSql(): string
*/
public function execute(array $inputParameters = null): bool
{
$statement = $this->statement;
try
{
$statement = $this->statement;
$statement->closeCursor();
if ($inputParameters)
{
Expand All @@ -147,18 +147,19 @@ public function execute(array $inputParameters = null): bool
if (!$result)
{
$errorCode = $this->errorCode();
$errorInfo = $this->errorInfo();
if ($this->db->checkCodeIsOffline($errorCode))
$errorInfo = $statement->errorInfo();
if ($this->db->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))

Check warning on line 151 in src/Db/Drivers/TPdoStatement.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoStatement.php#L150-L151

Added lines #L150 - L151 were not covered by tests
{
$this->db->close();
}
throw new DbException('SQL query error [' . $errorCode . '] ' . $errorInfo . \PHP_EOL . 'sql: ' . $this->getSql() . \PHP_EOL);
throw new DbException('SQL query error [' . $errorCode . '] ' . $errorInfo[2] . \PHP_EOL . 'sql: ' . $this->getSql() . \PHP_EOL);

Check warning on line 155 in src/Db/Drivers/TPdoStatement.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoStatement.php#L155

Added line #L155 was not covered by tests
}
$this->updateLastInsertId();
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[self::OFFLINE_CODE_INDEX]) && $this->db->checkCodeIsOffline($e->errorInfo[self::OFFLINE_CODE_INDEX]))
$errorInfo = $statement->errorInfo();
if ($this->db->checkCodeIsOffline($errorInfo[0], $errorInfo[1]))

Check warning on line 162 in src/Db/Drivers/TPdoStatement.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Drivers/TPdoStatement.php#L161-L162

Added lines #L161 - L162 were not covered by tests
{
$this->db->close();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Interfaces/IDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,5 @@ public function getOption(): array;
*
* @param mixed $code
*/
public function checkCodeIsOffline($code): bool;
public function checkCodeIsOffline(string $sqlState, $code = null): bool;
}
2 changes: 1 addition & 1 deletion src/Db/Mysql/Contract/IMysqlDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ interface IMysqlDb extends IDb
*
* @param int $code
*/
public function checkCodeIsOffline($code): bool;
public function checkCodeIsOffline(string $sqlState, $code = null): bool;
}
5 changes: 3 additions & 2 deletions src/Db/Mysql/Drivers/MysqlBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Imi\Db\Mysql\Contract\IMysqlDb;
use Imi\Db\Mysql\Query\MysqlQuery;
use Imi\Db\Query\Interfaces\IQuery;
use Imi\Db\SqlState;

abstract class MysqlBase extends Base implements IMysqlDb
{
Expand All @@ -32,7 +33,7 @@ public function getDbType(): string
*
* @see https://github.com/mysql/mysql-server/blob/HEAD/include/errmsg.h
*/
public function checkCodeIsOffline($code): bool
public function checkCodeIsOffline(string $sqlState, $code = null): bool

Check warning on line 36 in src/Db/Mysql/Drivers/MysqlBase.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Mysql/Drivers/MysqlBase.php#L36

Added line #L36 was not covered by tests
{
return \in_array($code, [
2001, // CR_SOCKET_CREATE_ERROR
Expand All @@ -45,6 +46,6 @@ public function checkCodeIsOffline($code): bool
2012, // CR_SERVER_HANDSHAKE_ERR
2013, // CR_SERVER_LOST
2026, // CR_SSL_CONNECTION_ERROR
]);
]) || SqlState::checkCodeIsOffline($sqlState);

Check warning on line 49 in src/Db/Mysql/Drivers/MysqlBase.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Mysql/Drivers/MysqlBase.php#L49

Added line #L49 was not covered by tests
}
}
Loading

0 comments on commit 3ad7f9f

Please sign in to comment.