Put FetchJoin Adapter logic into its own class.

This commit is contained in:
Jan Böhmer 2020-01-17 21:38:36 +01:00
parent ece3a464ce
commit 72e2c0cd6e
3 changed files with 170 additions and 68 deletions

View file

@ -0,0 +1,145 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
namespace App\DataTables\Adapter;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Omines\DataTablesBundle\Adapter\AdapterQuery;
use Omines\DataTablesBundle\Adapter\Doctrine\Event\ORMAdapterQueryEvent;
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapterEvents;
use Omines\DataTablesBundle\Column\AbstractColumn;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Similar to ORMAdapter this class allows to access objects from the doctrine ORM.
* Unlike the default ORMAdapter supports Fetch Joins (additional entites are fetched from DB via joins) using
* the Doctrine Paginator.
* @author Jan Böhmer
*/
class FetchJoinORMAdapter extends ORMAdapter
{
protected $use_simple_total;
public function configure(array $options)
{
parent::configure($options);
$this->use_simple_total = $options['simple_total_query'];
}
protected function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
//Enforce object hydration mode (fetch join only works for objects)
$resolver->addAllowedValues('hydrate', Query::HYDRATE_OBJECT);
/**
* Add the possibility to replace the query for total entity count through a very simple one, to improve performance.
* You can only use this option, if you did not apply any criteria to your total count.
*/
$resolver->setDefault('simple_total_query', false);
return $resolver;
}
protected function prepareQuery(AdapterQuery $query)
{
$state = $query->getState();
$query->set('qb', $builder = $this->createQueryBuilder($state));
$query->set('rootAlias', $rootAlias = $builder->getDQLPart('from')[0]->getAlias());
// Provide default field mappings if needed
foreach ($state->getDataTable()->getColumns() as $column) {
if (null === $column->getField() && isset($this->metadata->fieldMappings[$name = $column->getName()])) {
$column->setOption('field', "{$rootAlias}.{$name}");
}
}
/** @var Query\Expr\From $fromClause */
$fromClause = $builder->getDQLPart('from')[0];
$identifier = "{$fromClause->getAlias()}.{$this->metadata->getSingleIdentifierFieldName()}";
//Use simpler (faster) total count query if the user wanted so...
if ($this->use_simple_total) {
$query->setTotalRows($this->getSimpleTotalCount($builder));
} else {
$query->setTotalRows($this->getCount($builder, $identifier));
}
// Get record count after filtering
$this->buildCriteria($builder, $state);
$query->setFilteredRows($this->getCount($builder, $identifier));
// Perform mapping of all referred fields and implied fields
$aliases = $this->getAliases($query);
$query->set('aliases', $aliases);
$query->setIdentifierPropertyPath($this->mapFieldToPropertyPath($identifier, $aliases));
}
public function getResults(AdapterQuery $query): \Traversable
{
$builder = $query->get('qb');
$state = $query->getState();
// Apply definitive view state for current 'page' of the table
foreach ($state->getOrderBy() as list($column, $direction)) {
/** @var AbstractColumn $column */
if ($column->isOrderable()) {
$builder->addOrderBy($column->getOrderField(), $direction);
}
}
if ($state->getLength() > 0) {
$builder
->setFirstResult($state->getStart())
->setMaxResults($state->getLength());
}
$query = $builder->getQuery();
$event = new ORMAdapterQueryEvent($query);
$state->getDataTable()->getEventDispatcher()->dispatch($event, ORMAdapterEvents::PRE_QUERY);
//Use Doctrine paginator for result iteration
$paginator = new Paginator($query);
foreach ($paginator->getIterator() as $result) {
yield $result;
$this->manager->detach($result);
}
}
public function getCount(QueryBuilder $queryBuilder, $identifier)
{
$paginator = new Paginator($queryBuilder);
return $paginator->count();
}
protected function getSimpleTotalCount(QueryBuilder $queryBuilder)
{
/** The paginator count queries can be rather slow, so when query for total count (100ms or longer),
* just return the entity count.
*/
/** @var Query\Expr\From $from_expr */
$from_expr = $queryBuilder->getDQLPart('from')[0];
return $this->manager->getRepository($from_expr->getFrom())->count([]);
}
}

View file

@ -1,64 +1,50 @@
<?php <?php
declare(strict_types=1); /*
* Symfony DataTables Bundle
/** * (c) Omines Internetbureau B.V. - https://omines.nl/
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
* *
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics) * For the full copyright and license information, please view the LICENSE
* * file that was distributed with this source code.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/ */
declare(strict_types=1);
namespace App\DataTables\Adapter; namespace App\DataTables\Adapter;
use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query; use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Omines\DataTablesBundle\Adapter\AbstractAdapter; use Omines\DataTablesBundle\Adapter\AbstractAdapter;
use Omines\DataTablesBundle\Adapter\AdapterQuery; use Omines\DataTablesBundle\Adapter\AdapterQuery;
use Omines\DataTablesBundle\Adapter\Doctrine\Event\ORMAdapterQueryEvent; use Omines\DataTablesBundle\Adapter\Doctrine\Event\ORMAdapterQueryEvent;
use Omines\DataTablesBundle\Adapter\Doctrine\ORM\AutomaticQueryBuilder; use Omines\DataTablesBundle\Adapter\Doctrine\ORM\AutomaticQueryBuilder;
use Omines\DataTablesBundle\Adapter\Doctrine\ORM\QueryBuilderProcessorInterface; use Omines\DataTablesBundle\Adapter\Doctrine\ORM\QueryBuilderProcessorInterface;
use Omines\DataTablesBundle\Adapter\Doctrine\ORM\SearchCriteriaProvider; use Omines\DataTablesBundle\Adapter\Doctrine\ORM\SearchCriteriaProvider;
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapterEvents;
use Omines\DataTablesBundle\Column\AbstractColumn; use Omines\DataTablesBundle\Column\AbstractColumn;
use Omines\DataTablesBundle\DataTableState; use Omines\DataTablesBundle\DataTableState;
use Omines\DataTablesBundle\Exception\InvalidConfigurationException; use Omines\DataTablesBundle\Exception\InvalidConfigurationException;
use Omines\DataTablesBundle\Exception\MissingDependencyException; use Omines\DataTablesBundle\Exception\MissingDependencyException;
use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
use Traversable;
/** /**
* Override default ORM Adapter, to allow fetch joins (allow addSelect with ManyToOne Collections). * ORMAdapter.
* This should improves performance for Part Tables. *
* Based on: https://github.com/omines/datatables-bundle/blob/master/tests/Fixtures/AppBundle/DataTable/Adapter/CustomORMAdapter.php. * @author Niels Keurentjes <niels.keurentjes@omines.com>
* @author Robbert Beesems <robbert.beesems@omines.com>
*/ */
class CustomORMAdapter extends AbstractAdapter class ORMAdapter extends AbstractAdapter
{ {
/** @var ManagerRegistry */ /** @var ManagerRegistry */
private $registry; private $registry;
/** @var EntityManager */ /** @var EntityManager */
private $manager; protected $manager;
/** @var \Doctrine\ORM\Mapping\ClassMetadata */ /** @var \Doctrine\ORM\Mapping\ClassMetadata */
private $metadata; protected $metadata;
/** @var int */ /** @var int */
private $hydrationMode; private $hydrationMode;
@ -69,10 +55,6 @@ class CustomORMAdapter extends AbstractAdapter
/** @var QueryBuilderProcessorInterface[] */ /** @var QueryBuilderProcessorInterface[] */
protected $criteriaProcessors; protected $criteriaProcessors;
/** @var bool */
protected $allow_fetch_join;
/** /**
* DoctrineAdapter constructor. * DoctrineAdapter constructor.
*/ */
@ -108,7 +90,6 @@ class CustomORMAdapter extends AbstractAdapter
$this->hydrationMode = $options['hydrate']; $this->hydrationMode = $options['hydrate'];
$this->queryBuilderProcessors = $options['query']; $this->queryBuilderProcessors = $options['query'];
$this->criteriaProcessors = $options['criteria']; $this->criteriaProcessors = $options['criteria'];
$this->allow_fetch_join = $options['allow_fetch_join'];
} }
/** /**
@ -135,13 +116,11 @@ class CustomORMAdapter extends AbstractAdapter
/** @var Query\Expr\From $fromClause */ /** @var Query\Expr\From $fromClause */
$fromClause = $builder->getDQLPart('from')[0]; $fromClause = $builder->getDQLPart('from')[0];
$identifier = "{$fromClause->getAlias()}.{$this->metadata->getSingleIdentifierFieldName()}"; $identifier = "{$fromClause->getAlias()}.{$this->metadata->getSingleIdentifierFieldName()}";
$query->setTotalRows($this->getCount($builder, $identifier));
$query->setTotalRows($this->getCount($builder, $identifier, true));
// Get record count after filtering // Get record count after filtering
$this->buildCriteria($builder, $state); $this->buildCriteria($builder, $state);
$query->setFilteredRows($this->getCount($builder, $identifier, false)); $query->setFilteredRows($this->getCount($builder, $identifier));
// Perform mapping of all referred fields and implied fields // Perform mapping of all referred fields and implied fields
$aliases = $this->getAliases($query); $aliases = $this->getAliases($query);
@ -205,26 +184,18 @@ class CustomORMAdapter extends AbstractAdapter
if ($state->getLength() > 0) { if ($state->getLength() > 0) {
$builder $builder
->setFirstResult($state->getStart()) ->setFirstResult($state->getStart())
->setMaxResults($state->getLength()); ->setMaxResults($state->getLength())
;
} }
$query = $builder->getQuery(); $query = $builder->getQuery();
$event = new ORMAdapterQueryEvent($query); $event = new ORMAdapterQueryEvent($query);
$state->getDataTable()->getEventDispatcher()->dispatch($event, ORMAdapterEvents::PRE_QUERY); $state->getDataTable()->getEventDispatcher()->dispatch($event, ORMAdapterEvents::PRE_QUERY);
if ($this->allow_fetch_join && $this->hydrationMode === Query::HYDRATE_OBJECT) { foreach ($query->iterate([], $this->hydrationMode) as $result) {
$paginator = new Paginator($query); yield $entity = array_values($result)[0];
$iterator = $paginator->getIterator();
} else {
$iterator = $query->iterate([], $this->hydrationMode);
}
foreach ($iterator as $result) {
if (Query::HYDRATE_OBJECT === $this->hydrationMode) { if (Query::HYDRATE_OBJECT === $this->hydrationMode) {
yield $entity = $result;
$this->manager->detach($entity); $this->manager->detach($entity);
} else {
yield $entity = array_values($result)[0];
} }
} }
} }
@ -253,22 +224,8 @@ class CustomORMAdapter extends AbstractAdapter
* @param $identifier * @param $identifier
* @return int * @return int
*/ */
protected function getCount(QueryBuilder $queryBuilder, $identifier, $total_count = false) protected function getCount(QueryBuilder $queryBuilder, $identifier)
{ {
if ($this->allow_fetch_join) {
/** The paginator count queries can be rather slow, so when query for total count (100ms or longer),
* just return the entity count.
*/
if ($total_count) {
/** @var Query\Expr\From $from_expr */
$from_expr = $queryBuilder->getDQLPart('from')[0];
return $this->manager->getRepository($from_expr->getFrom())->count([]);
}
$paginator = new Paginator($queryBuilder);
return $paginator->count();
}
$qb = clone $queryBuilder; $qb = clone $queryBuilder;
$qb->resetDQLPart('orderBy'); $qb->resetDQLPart('orderBy');
@ -305,7 +262,7 @@ class CustomORMAdapter extends AbstractAdapter
* @param string $field * @param string $field
* @return string * @return string
*/ */
private function mapFieldToPropertyPath($field, array $aliases = []) protected function mapFieldToPropertyPath($field, array $aliases = [])
{ {
$parts = explode('.', $field); $parts = explode('.', $field);
if (count($parts) < 2) { if (count($parts) < 2) {
@ -338,7 +295,6 @@ class CustomORMAdapter extends AbstractAdapter
$resolver $resolver
->setDefaults([ ->setDefaults([
'hydrate' => Query::HYDRATE_OBJECT, 'hydrate' => Query::HYDRATE_OBJECT,
'allow_fetch_join' => false,
'query' => [], 'query' => [],
'criteria' => function (Options $options) { 'criteria' => function (Options $options) {
return [new SearchCriteriaProvider()]; return [new SearchCriteriaProvider()];

View file

@ -25,6 +25,7 @@ declare(strict_types=1);
namespace App\DataTables; namespace App\DataTables;
use App\DataTables\Adapter\CustomORMAdapter; use App\DataTables\Adapter\CustomORMAdapter;
use App\DataTables\Adapter\FetchJoinORMAdapter;
use App\DataTables\Column\EntityColumn; use App\DataTables\Column\EntityColumn;
use App\DataTables\Column\LocaleDateTimeColumn; use App\DataTables\Column\LocaleDateTimeColumn;
use App\DataTables\Column\MarkdownColumn; use App\DataTables\Column\MarkdownColumn;
@ -215,8 +216,8 @@ final class PartsDataTable implements DataTableTypeInterface
]) ])
->addOrderBy('name') ->addOrderBy('name')
->createAdapter(CustomORMAdapter::class, [ ->createAdapter(FetchJoinORMAdapter::class, [
'allow_fetch_join' => true, 'simple_total_query' => true,
'query' => function (QueryBuilder $builder): void { 'query' => function (QueryBuilder $builder): void {
$this->getQuery($builder); $this->getQuery($builder);
}, },