From 12218f4659f3eae2b5e4310477e24b1b241cbf52 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Mon, 29 Jun 2020 22:56:48 +0200 Subject: [PATCH] Fixed getting DB platform in Filtering Gateways to be lazy (#83) Reason: getting a database platform in a constructor is a heavy operation which attempts to connect to a database and crashes if there's no database. --- .../Content/Doctrine/DoctrineGateway.php | 21 ++++++++++++------- .../Location/Doctrine/DoctrineGateway.php | 21 ++++++++++++------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/eZ/Publish/Core/Persistence/Legacy/Filter/Gateway/Content/Doctrine/DoctrineGateway.php b/eZ/Publish/Core/Persistence/Legacy/Filter/Gateway/Content/Doctrine/DoctrineGateway.php index 3424272c72..7401f61b46 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Filter/Gateway/Content/Doctrine/DoctrineGateway.php +++ b/eZ/Publish/Core/Persistence/Legacy/Filter/Gateway/Content/Doctrine/DoctrineGateway.php @@ -9,8 +9,11 @@ namespace eZ\Publish\Core\Persistence\Legacy\Filter\Gateway\Content\Doctrine; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\DBALException; use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Query\QueryBuilder; +use eZ\Publish\Core\Base\Exceptions\DatabaseException; use eZ\Publish\Core\Persistence\Legacy\Content\Gateway as ContentGateway; use eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway as LocationGateway; use eZ\Publish\Core\Persistence\Legacy\Filter\Gateway\Gateway; @@ -59,33 +62,35 @@ final class DoctrineGateway implements Gateway /** @var \Doctrine\DBAL\Connection */ private $connection; - /** @var \Doctrine\DBAL\Platforms\AbstractPlatform */ - private $databasePlatform; - /** @var \eZ\Publish\SPI\Persistence\Filter\CriterionVisitor */ private $criterionVisitor; /** @var \eZ\Publish\SPI\Persistence\Filter\SortClauseVisitor */ private $sortClauseVisitor; - /** - * @throws \Doctrine\DBAL\DBALException - */ public function __construct( Connection $connection, CriterionVisitor $criterionVisitor, SortClauseVisitor $sortClauseVisitor ) { $this->connection = $connection; - $this->databasePlatform = $connection->getDatabasePlatform(); $this->criterionVisitor = $criterionVisitor; $this->sortClauseVisitor = $sortClauseVisitor; } + private function getDatabasePlatform(): AbstractPlatform + { + try { + return $this->connection->getDatabasePlatform(); + } catch (DBALException $e) { + throw DatabaseException::wrap($e); + } + } + public function count(FilteringCriterion $criterion): int { $query = $this->buildQuery( - [$this->databasePlatform->getCountExpression('DISTINCT content.id')], + [$this->getDatabasePlatform()->getCountExpression('DISTINCT content.id')], $criterion ); diff --git a/eZ/Publish/Core/Persistence/Legacy/Filter/Gateway/Location/Doctrine/DoctrineGateway.php b/eZ/Publish/Core/Persistence/Legacy/Filter/Gateway/Location/Doctrine/DoctrineGateway.php index 6a74fdb365..dc9755cef6 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Filter/Gateway/Location/Doctrine/DoctrineGateway.php +++ b/eZ/Publish/Core/Persistence/Legacy/Filter/Gateway/Location/Doctrine/DoctrineGateway.php @@ -9,7 +9,10 @@ namespace eZ\Publish\Core\Persistence\Legacy\Filter\Gateway\Location\Doctrine; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\DBALException; use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\Platforms\AbstractPlatform; +use eZ\Publish\Core\Base\Exceptions\DatabaseException; use eZ\Publish\Core\Persistence\Legacy\Content\Gateway as ContentGateway; use eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway as LocationGateway; use eZ\Publish\Core\Persistence\Legacy\Filter\Gateway\Gateway; @@ -26,34 +29,36 @@ final class DoctrineGateway implements Gateway /** @var \Doctrine\DBAL\Connection */ private $connection; - /** @var \Doctrine\DBAL\Platforms\AbstractPlatform */ - private $databasePlatform; - /** @var \eZ\Publish\SPI\Persistence\Filter\CriterionVisitor */ private $criterionVisitor; /** @var \eZ\Publish\SPI\Persistence\Filter\SortClauseVisitor */ private $sortClauseVisitor; - /** - * @throws \Doctrine\DBAL\DBALException - */ public function __construct( Connection $connection, CriterionVisitor $criterionVisitor, SortClauseVisitor $sortClauseVisitor ) { $this->connection = $connection; - $this->databasePlatform = $connection->getDatabasePlatform(); $this->criterionVisitor = $criterionVisitor; $this->sortClauseVisitor = $sortClauseVisitor; } + private function getDatabasePlatform(): AbstractPlatform + { + try { + return $this->connection->getDatabasePlatform(); + } catch (DBALException $e) { + throw DatabaseException::wrap($e); + } + } + public function count(FilteringCriterion $criterion): int { $query = $this->buildQuery($criterion); - $query->select($this->databasePlatform->getCountExpression('DISTINCT location.node_id')); + $query->select($this->getDatabasePlatform()->getCountExpression('DISTINCT location.node_id')); return (int)$query->execute()->fetch(FetchMode::COLUMN); }