Skip to content

Commit

Permalink
Fixed getting DB platform in Filtering Gateways to be lazy (#83)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alongosz authored Jun 29, 2020
1 parent 9f59de2 commit 12218f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit 12218f4

Please # to comment.