Rework deprecated MySQLi exceptions

parent a7374a24
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\Mysqli\Exception;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use mysqli;
/**
* @internal
*
* @psalm-immutable
*/
final class ConnectionError extends MysqliException
{
public static function new(mysqli $connection): self
{
return new self($connection->error, $connection->sqlstate, $connection->errno);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\Mysqli\Exception;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use mysqli;
/**
* @internal
*
* @psalm-immutable
*/
final class ConnectionFailed extends MysqliException
{
public static function new(mysqli $connection): self
{
return new self($connection->connect_error, 'HY000', $connection->connect_errno);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\Mysqli\Exception;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use function sprintf;
/**
* @internal
*
* @psalm-immutable
*/
final class FailedReadingStreamOffset extends MysqliException
{
public static function new(int $offset): self
{
return new self(sprintf('Failed reading the stream resource for parameter offset %d.', $offset));
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\Mysqli\Exception;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use function sprintf;
/**
* @internal
*
* @psalm-immutable
*/
final class InvalidOption extends MysqliException
{
/**
* @param mixed $option
* @param mixed $value
*/
public static function fromOption($option, $value): self
{
return new self(
sprintf('Failed to set option %d with value "%s"', $option, $value)
);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\Mysqli\Exception;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use mysqli_stmt;
/**
* @internal
*
* @psalm-immutable
*/
final class StatementError extends MysqliException
{
public static function new(mysqli_stmt $statement): self
{
return new self($statement->error, $statement->sqlstate, $statement->errno);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\Mysqli\Exception;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use function sprintf;
/**
* @internal
*
* @psalm-immutable
*/
final class UnknownType extends MysqliException
{
/**
* @param mixed $type
*/
public static function new($type): self
{
return new self(sprintf('Unknown type, %d given.', $type));
}
}
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
namespace Doctrine\DBAL\Driver\Mysqli; namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\Driver\Connection as ConnectionInterface; use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionError;
use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionFailed;
use Doctrine\DBAL\Driver\Mysqli\Exception\InvalidOption;
use Doctrine\DBAL\Driver\PingableConnection; use Doctrine\DBAL\Driver\PingableConnection;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
...@@ -73,7 +76,7 @@ class MysqliConnection implements ConnectionInterface, PingableConnection, Serve ...@@ -73,7 +76,7 @@ class MysqliConnection implements ConnectionInterface, PingableConnection, Serve
}); });
try { try {
if (! $this->conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) { if (! $this->conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) {
throw new MysqliException($this->conn->connect_error, $this->conn->sqlstate ?? 'HY000', $this->conn->connect_errno); throw ConnectionFailed::new($this->conn);
} }
} finally { } finally {
restore_error_handler(); restore_error_handler();
...@@ -163,7 +166,7 @@ class MysqliConnection implements ConnectionInterface, PingableConnection, Serve ...@@ -163,7 +166,7 @@ class MysqliConnection implements ConnectionInterface, PingableConnection, Serve
public function exec($statement) public function exec($statement)
{ {
if ($this->conn->query($statement) === false) { if ($this->conn->query($statement) === false) {
throw new MysqliException($this->conn->error, $this->conn->sqlstate, $this->conn->errno); throw ConnectionError::new($this->conn);
} }
return $this->conn->affected_rows; return $this->conn->affected_rows;
...@@ -257,9 +260,7 @@ class MysqliConnection implements ConnectionInterface, PingableConnection, Serve ...@@ -257,9 +260,7 @@ class MysqliConnection implements ConnectionInterface, PingableConnection, Serve
} }
if (! in_array($option, $supportedDriverOptions, true)) { if (! in_array($option, $supportedDriverOptions, true)) {
throw new MysqliException( throw InvalidOption::fromOption($option, $value);
sprintf($exceptionMsg, 'Unsupported', $option, $value)
);
} }
if (@mysqli_options($this->conn, $option, $value)) { if (@mysqli_options($this->conn, $option, $value)) {
......
...@@ -3,6 +3,10 @@ ...@@ -3,6 +3,10 @@
namespace Doctrine\DBAL\Driver\Mysqli; namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\Driver\FetchUtils; use Doctrine\DBAL\Driver\FetchUtils;
use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionError;
use Doctrine\DBAL\Driver\Mysqli\Exception\FailedReadingStreamOffset;
use Doctrine\DBAL\Driver\Mysqli\Exception\StatementError;
use Doctrine\DBAL\Driver\Mysqli\Exception\UnknownType;
use Doctrine\DBAL\Driver\Result; use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement as StatementInterface; use Doctrine\DBAL\Driver\Statement as StatementInterface;
use Doctrine\DBAL\Driver\StatementIterator; use Doctrine\DBAL\Driver\StatementIterator;
...@@ -89,7 +93,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result ...@@ -89,7 +93,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result
$stmt = $conn->prepare($prepareString); $stmt = $conn->prepare($prepareString);
if ($stmt === false) { if ($stmt === false) {
throw new MysqliException($this->_conn->error, $this->_conn->sqlstate, $this->_conn->errno); throw ConnectionError::new($this->_conn);
} }
$this->_stmt = $stmt; $this->_stmt = $stmt;
...@@ -111,7 +115,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result ...@@ -111,7 +115,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result
assert(is_int($column)); assert(is_int($column));
if (! isset(self::$_paramTypeMap[$type])) { if (! isset(self::$_paramTypeMap[$type])) {
throw new MysqliException(sprintf("Unknown type: '%s'", $type)); throw UnknownType::new($type);
} }
$this->_bindedValues[$column] =& $variable; $this->_bindedValues[$column] =& $variable;
...@@ -128,7 +132,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result ...@@ -128,7 +132,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result
assert(is_int($param)); assert(is_int($param));
if (! isset(self::$_paramTypeMap[$type])) { if (! isset(self::$_paramTypeMap[$type])) {
throw new MysqliException(sprintf("Unknown type: '%s'", $type)); throw UnknownType::new($type);
} }
$this->_values[$param] = $value; $this->_values[$param] = $value;
...@@ -146,7 +150,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result ...@@ -146,7 +150,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result
if ($this->_bindedValues !== null) { if ($this->_bindedValues !== null) {
if ($params !== null) { if ($params !== null) {
if (! $this->bindUntypedValues($params)) { if (! $this->bindUntypedValues($params)) {
throw new MysqliException($this->_stmt->error, $this->_stmt->errno); throw StatementError::new($this->_stmt);
} }
} else { } else {
$this->bindTypedParameters(); $this->bindTypedParameters();
...@@ -154,7 +158,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result ...@@ -154,7 +158,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result
} }
if (! $this->_stmt->execute()) { if (! $this->_stmt->execute()) {
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); throw StatementError::new($this->_stmt);
} }
if ($this->_columnNames === null) { if ($this->_columnNames === null) {
...@@ -201,7 +205,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result ...@@ -201,7 +205,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result
} }
if (! $this->_stmt->bind_result(...$refs)) { if (! $this->_stmt->bind_result(...$refs)) {
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); throw StatementError::new($this->_stmt);
} }
} }
...@@ -243,7 +247,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result ...@@ -243,7 +247,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result
} }
if (! $this->_stmt->bind_param($types, ...$values)) { if (! $this->_stmt->bind_param($types, ...$values)) {
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); throw StatementError::new($this->_stmt);
} }
$this->sendLongData($streams); $this->sendLongData($streams);
...@@ -263,11 +267,11 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result ...@@ -263,11 +267,11 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result
$chunk = fread($stream, 8192); $chunk = fread($stream, 8192);
if ($chunk === false) { if ($chunk === false) {
throw new MysqliException("Failed reading the stream resource for parameter offset ${paramNr}."); throw FailedReadingStreamOffset::new($paramNr);
} }
if (! $this->_stmt->send_long_data($paramNr - 1, $chunk)) { if (! $this->_stmt->send_long_data($paramNr - 1, $chunk)) {
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); throw StatementError::new($this->_stmt);
} }
} }
} }
...@@ -337,7 +341,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result ...@@ -337,7 +341,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result
} }
if ($values === false) { if ($values === false) {
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); throw StatementError::new($this->_stmt);
} }
if ($fetchMode === FetchMode::NUMERIC) { if ($fetchMode === FetchMode::NUMERIC) {
...@@ -423,7 +427,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result ...@@ -423,7 +427,7 @@ class MysqliStatement implements IteratorAggregate, StatementInterface, Result
} }
if ($values === false) { if ($values === false) {
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); throw StatementError::new($this->_stmt);
} }
return $values; return $values;
......
...@@ -87,6 +87,7 @@ ...@@ -87,6 +87,7 @@
https://github.com/squizlabs/PHP_CodeSniffer/issues/2950 https://github.com/squizlabs/PHP_CodeSniffer/issues/2950
--> -->
<exclude-pattern>lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php</exclude-pattern> <exclude-pattern>lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php</exclude-pattern>
<exclude-pattern>lib/Doctrine/DBAL/Driver/Mysqli/Exception/ConnectionFailed.php</exclude-pattern>
<!-- See https://github.com/squizlabs/PHP_CodeSniffer/issues/2837 --> <!-- See https://github.com/squizlabs/PHP_CodeSniffer/issues/2837 -->
<exclude-pattern>lib/Doctrine/DBAL/SQLParserUtils.php</exclude-pattern> <exclude-pattern>lib/Doctrine/DBAL/SQLParserUtils.php</exclude-pattern>
<exclude-pattern>lib/Doctrine/DBAL/Tools/Dumper.php</exclude-pattern> <exclude-pattern>lib/Doctrine/DBAL/Tools/Dumper.php</exclude-pattern>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment