Skip to content

Commit

Permalink
Add more specified exceptions when no resource returned for db query
Browse files Browse the repository at this point in the history
  • Loading branch information
anokhinAleksey authored and mrVrAlex committed Jun 1, 2022
1 parent 79b1749 commit b7ba459
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 33 deletions.
18 changes: 12 additions & 6 deletions src/Swoole/PgSQL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
use Doctrine\DBAL\ParameterType;
use Exception;
use OpsWay\Doctrine\DBAL\SQLParserUtils;
use OpsWay\Doctrine\DBAL\Swoole\PgSQL\Exception\ConnectionException;
use OpsWay\Doctrine\DBAL\Swoole\PgSQL\Exception\DriverException as SwooleDriverException;

use function is_resource;
use function strlen;
use function substr;

Expand All @@ -21,7 +23,7 @@ public function __construct(private ConnectionWrapperInterface $connection)
/**
* {@inheritdoc}
*
* @throws Exception
* @throws SwooleDriverException
*/
public function prepare(string $sql) : Statement
{
Expand All @@ -46,9 +48,12 @@ public function prepare(string $sql) : Statement
*/
public function query(string $sql) : Result
{
/** @var resource $resource */
$resource = $this->connection->query($sql);

if (! is_resource($resource)) {
throw ConnectionException::fromConnection($this->connection);
}

return new Result($this->connection, $resource);
}

Expand All @@ -69,11 +74,12 @@ public function quote($value, $type = ParameterType::STRING) : string
public function exec(string $sql) : int
{
$query = $this->connection->query($sql);
if ($query !== false) {
return $this->connection->affectedRows($query);

if (! is_resource($query)) {
throw ConnectionException::fromConnection($this->connection);
}

return 0;
return $this->connection->affectedRows($query);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Swoole/PgSQL/ConnectionPullFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* delay: 1,
* },
* }
* @psalm-suppress MissingDependency, UndefinedClass
*/
class ConnectionPullFactory
{
Expand All @@ -30,9 +31,8 @@ class ConnectionPullFactory

/**
* @psalm-param ConnectionPullFactoryConfig $params
* @return mixed
*/
public function __invoke(array $params)
public function __invoke(array $params) : DownscaleableConnectionPool
{
/**
* @var int|null $pullSize
Expand Down
2 changes: 2 additions & 0 deletions src/Swoole/PgSQL/Exception/ConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
/** @psalm-immutable */
class ConnectionException extends Exception implements DBALDriverException
{
use ExceptionFromConnectionTrait;

public function __construct(
string $message = '',
private ?string $errorCode = null,
Expand Down
2 changes: 2 additions & 0 deletions src/Swoole/PgSQL/Exception/DriverException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
/** @psalm-immutable */
class DriverException extends Exception implements DBALDriverException
{
use ExceptionFromConnectionTrait;

public function __construct(
string $message = '',
private ?string $errorCode = null,
Expand Down
26 changes: 26 additions & 0 deletions src/Swoole/PgSQL/Exception/ExceptionFromConnectionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace OpsWay\Doctrine\DBAL\Swoole\PgSQL\Exception;

use ArrayAccess;
use OpsWay\Doctrine\DBAL\Swoole\PgSQL\ConnectionWrapperInterface;

/** @psalm-immutable */
trait ExceptionFromConnectionTrait
{
public static function fromConnection(ConnectionWrapperInterface $connection) : self
{
/** @var ArrayAccess $resultDiag */
$resultDiag = $connection->resultDiag() ?? [];
$sqlstate = (string) ($resultDiag['sqlstate'] ?? '');

return new self(
$connection->error(),
(string) $connection->errorCode(),
$sqlstate,
$connection->errorCode(),
);
}
}
18 changes: 16 additions & 2 deletions src/Swoole/PgSQL/PsqlConnectionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use ArrayAccess;
use Swoole\Coroutine\PostgreSQL;

use function count;
use function is_array;
use function is_resource;
use function mt_rand;
use function time;
use function uniqid;
Expand Down Expand Up @@ -60,8 +63,13 @@ public function isReusable() : bool
public function query(string $sql)
{
$this->usedTimes++;
$result = $this->connection->query($sql);

return $this->connection->query($sql);
if (! is_resource($result)) {
$result = false;
}

return $result;
}

/** @param resource $queryResult */
Expand All @@ -73,7 +81,13 @@ public function affectedRows($queryResult) : int
/** @param resource $queryResult */
public function fetchAssoc($queryResult) : array|bool
{
return $this->connection->fetchAssoc($queryResult);
$result = $this->connection->fetchAssoc($queryResult);

if (is_array($result) && count($result) === 0) {
$result = false;
}

return $result;
}

/** @param resource $queryResult*/
Expand Down
20 changes: 11 additions & 9 deletions src/Swoole/PgSQL/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace OpsWay\Doctrine\DBAL\Swoole\PgSQL;

use Doctrine\DBAL\Driver\Result as ResultInterface;
use Exception;
use OpsWay\Doctrine\DBAL\Swoole\PgSQL\Exception\DriverException as SwooleDriverException;

use function is_resource;

use const OPENSWOOLE_PGSQL_NUM;

Expand All @@ -19,8 +21,8 @@ public function __construct(private ConnectionWrapperInterface $connection, priv
/** {@inheritdoc} */
public function fetchNumeric() : array|bool
{
if (! $this->result) {
throw new Exception('Result expecting been resource here');
if (! is_resource($this->result)) {
throw SwooleDriverException::fromConnection($this->connection);
}
/**
* @psalm-var list<mixed>|false $result
Expand All @@ -34,8 +36,8 @@ public function fetchNumeric() : array|bool
/** {@inheritdoc} */
public function fetchAssociative() : array|bool
{
if (! $this->result) {
throw new Exception('Result expecting been resource here');
if (! is_resource($this->result)) {
throw SwooleDriverException::fromConnection($this->connection);
}
/** @psalm-var array<string,mixed>|false $result */
$result = $this->connection->fetchAssoc($this->result);
Expand Down Expand Up @@ -91,8 +93,8 @@ public function fetchFirstColumn() : array
/** {@inheritdoc} */
public function rowCount() : int
{
if (! $this->result) {
throw new Exception('Result expecting been resource here');
if (! is_resource($this->result)) {
throw SwooleDriverException::fromConnection($this->connection);
}

return $this->connection->affectedRows($this->result);
Expand All @@ -101,8 +103,8 @@ public function rowCount() : int
/** {@inheritdoc} */
public function columnCount() : int
{
if (! $this->result) {
throw new Exception('Result expecting been resource here');
if (! is_resource($this->result)) {
throw SwooleDriverException::fromConnection($this->connection);
}

return $this->connection->fieldCount($this->result);
Expand Down
17 changes: 3 additions & 14 deletions src/Swoole/PgSQL/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace OpsWay\Doctrine\DBAL\Swoole\PgSQL;

use ArrayAccess;
use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\Statement as StatementInterface;
use Doctrine\DBAL\ParameterType;
use Exception;
use OpsWay\Doctrine\DBAL\Swoole\PgSQL\Exception\DriverException as SwooleDriverException;

use function is_array;
Expand All @@ -25,7 +23,7 @@ public function __construct(private ConnectionWrapperInterface $connection, stri
{
$this->key = uniqid('stmt_', true);
if ($this->connection->prepare($this->key, $sql) === false) {
throw new Exception($this->errorInfo());
throw SwooleDriverException::fromConnection($this->connection);
}
}

Expand Down Expand Up @@ -58,7 +56,7 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le

/**
* @param mixed|null $params
* @throws Exception
* @throws SwooleDriverException
* @psalm-suppress ImplementedReturnTypeMismatch
*/
public function execute($params = []) : ResultInterface
Expand All @@ -75,16 +73,7 @@ public function execute($params = []) : ResultInterface

$result = $this->connection->execute($this->key, $mergedParams);
if (! is_resource($result)) {
/** @var ArrayAccess $resultDiag */
$resultDiag = $this->connection->resultDiag() ?? [];
$sqlstate = (string) ($resultDiag['sqlstate'] ?? '');

throw new SwooleDriverException(
$this->errorInfo(),
(string) $this->errorCode(),
$sqlstate,
$this->errorCode(),
);
throw SwooleDriverException::fromConnection($this->connection);
}

return new Result($this->connection, $result);
Expand Down

0 comments on commit b7ba459

Please sign in to comment.