Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复 MySQL 检查是否离线的错误码索引 #686

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
{
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 @@
{
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 @@
{
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: 3 additions & 4 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 @@ -29,11 +30,9 @@

/**
* {@inheritDoc}
*
* @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 34 in src/Components/pgsql/src/Db/PgsqlBase.php

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L36 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 @@
{
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 @@
{
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 @@
{
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 @@
{
$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 @@
$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 @@
{
$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 @@
{
$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 @@
$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 @@
{
$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 @@
{
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[0]) && $this->checkCodeIsOffline($e->errorInfo[0]))
$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 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 @@
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0]))
$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 @@
{
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 @@
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0]))
$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 @@
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0]))
$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 @@
$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 @@
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0]))
$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 @@
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0]))
$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 @@
}
catch (\PDOException $e)
{
if (isset($e->errorInfo[0]) && $this->checkCodeIsOffline($e->errorInfo[0]))
$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 execute(array $inputParameters = null): bool
{
$statement = $this->statement;
try
{
$statement = $this->statement;
$statement->closeCursor();
if ($inputParameters)
{
Expand All @@ -147,18 +147,19 @@
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[0]) && $this->db->checkCodeIsOffline($e->errorInfo[0]))
$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 @@
*
* @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 @@
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
Loading