|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Oro\Bundle\DataGridBundle\Extension\Sorter; |
| 4 | + |
| 5 | +use Doctrine\ORM\Query\Expr\From; |
| 6 | +use Doctrine\ORM\QueryBuilder; |
| 7 | +use Doctrine\ORM\Query\Expr\Select; |
| 8 | + |
| 9 | +use Oro\Bundle\DataGridBundle\Datagrid\Common\DatagridConfiguration; |
| 10 | +use Oro\Bundle\DataGridBundle\Datasource\DatasourceInterface; |
| 11 | +use Oro\Bundle\DataGridBundle\Extension\AbstractExtension; |
| 12 | +use Oro\Bundle\DataGridBundle\Datasource\Orm\OrmDatasource; |
| 13 | +use Oro\Bundle\EntityBundle\ORM\DatabaseDriverInterface; |
| 14 | +use Oro\Bundle\EntityBundle\ORM\EntityClassResolver; |
| 15 | + |
| 16 | +class PostgresqlGridModifier extends AbstractExtension |
| 17 | +{ |
| 18 | + const PRIORITY = -261; |
| 19 | + |
| 20 | + /** @var string */ |
| 21 | + protected $databaseDriver; |
| 22 | + |
| 23 | + /** @var EntityClassResolver */ |
| 24 | + protected $entityClassResolver; |
| 25 | + |
| 26 | + /** |
| 27 | + * @param string $databaseDriver |
| 28 | + * @param EntityClassResolver $entityClassResolver |
| 29 | + */ |
| 30 | + public function __construct($databaseDriver, EntityClassResolver $entityClassResolver) |
| 31 | + { |
| 32 | + $this->databaseDriver = $databaseDriver; |
| 33 | + $this->entityClassResolver = $entityClassResolver; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + public function isApplicable(DatagridConfiguration $config) |
| 40 | + { |
| 41 | + return $this->databaseDriver === DatabaseDriverInterface::DRIVER_POSTGRESQL; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * {@inheritDoc} |
| 46 | + */ |
| 47 | + public function getPriority() |
| 48 | + { |
| 49 | + return self::PRIORITY; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Add sorting by identifier because postgresql return rows in different order on two the same sql, but |
| 54 | + * different LIMIT number |
| 55 | + * |
| 56 | + * @param DatagridConfiguration $config |
| 57 | + * @param DatasourceInterface $datasource |
| 58 | + * @return mixed|void |
| 59 | + */ |
| 60 | + public function visitDatasource(DatagridConfiguration $config, DatasourceInterface $datasource) |
| 61 | + { |
| 62 | + //getQueryBuilder exists only in datagrid orm datasource |
| 63 | + if (!$datasource instanceof OrmDatasource) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + $entityClassName = $this->getEntityClassName($config); |
| 68 | + /** @var QueryBuilder $queryBuilder */ |
| 69 | + $queryBuilder = $datasource->getQueryBuilder(); |
| 70 | + |
| 71 | + if (!$entityClassName) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + $fromParts = $queryBuilder->getDQLPart('from'); |
| 76 | + $alias = false; |
| 77 | + |
| 78 | + $metadata = $queryBuilder->getEntityManager()->getClassMetadata($entityClassName); |
| 79 | + $identifier = $metadata->getSingleIdentifierFieldName(); |
| 80 | + |
| 81 | + /** @var From $fromPart */ |
| 82 | + foreach ($fromParts as $fromPart) { |
| 83 | + if ($this->entityClassResolver->getEntityClass($fromPart->getFrom()) == $entityClassName) { |
| 84 | + $alias = $fromPart->getAlias(); |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if ($alias && $this->isAllowedAddingSorting($alias, $identifier, $queryBuilder)) { |
| 90 | + $field = $alias . '.' . $identifier; |
| 91 | + $orderBy = $queryBuilder->getDQLPart('orderBy'); |
| 92 | + if (!isset($orderBy[$field])) { |
| 93 | + if ($this->isDistinct($queryBuilder)) { |
| 94 | + $this->ensureIdentifierSelected($queryBuilder, $field); |
| 95 | + } |
| 96 | + $queryBuilder->addOrderBy($field, 'ASC'); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @param DatagridConfiguration $config |
| 103 | + * |
| 104 | + * @return null|string |
| 105 | + */ |
| 106 | + protected function getEntityClassName(DatagridConfiguration $config) |
| 107 | + { |
| 108 | + $entityClassName = $config->offsetGetByPath('[extended_entity_name]'); |
| 109 | + if ($entityClassName) { |
| 110 | + return $entityClassName; |
| 111 | + } |
| 112 | + |
| 113 | + $from = $config->offsetGetByPath('[source][query][from]'); |
| 114 | + if (count($from) !== 0) { |
| 115 | + return $this->entityClassResolver->getEntityClass($from[0]['table']); |
| 116 | + } |
| 117 | + |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param string $alias |
| 123 | + * @param string $identifier |
| 124 | + * @param QueryBuilder $queryBuilder |
| 125 | + * @return bool |
| 126 | + */ |
| 127 | + protected function isAllowedAddingSorting($alias, $identifier, QueryBuilder $queryBuilder) |
| 128 | + { |
| 129 | + $groupByParts = $queryBuilder->getDQLPart('groupBy'); |
| 130 | + |
| 131 | + if (!count($groupByParts)) { |
| 132 | + return true; |
| 133 | + } |
| 134 | + |
| 135 | + foreach ($groupByParts as $groupBy) { |
| 136 | + if (in_array($alias.'.'.$identifier, $groupBy->getParts(), true) !== false) { |
| 137 | + return true; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @param QueryBuilder $queryBuilder |
| 146 | + * @return bool |
| 147 | + */ |
| 148 | + protected function isDistinct(QueryBuilder $queryBuilder) |
| 149 | + { |
| 150 | + if ($queryBuilder->getDQLPart('distinct')) { |
| 151 | + return true; |
| 152 | + } |
| 153 | + |
| 154 | + foreach ($queryBuilder->getDQLPart('select') as $select) { |
| 155 | + $selectString = ltrim(strtolower((string)$select)); |
| 156 | + if (strpos($selectString, 'distinct ') === 0) { |
| 157 | + return true; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @param QueryBuilder $queryBuilder |
| 166 | + * @param string $field |
| 167 | + */ |
| 168 | + protected function ensureIdentifierSelected(QueryBuilder $queryBuilder, $field) |
| 169 | + { |
| 170 | + $isSelected = false; |
| 171 | + /** @var Select $select */ |
| 172 | + foreach ($queryBuilder->getDQLPart('select') as $select) { |
| 173 | + $selectString = ltrim(strtolower((string)$select)); |
| 174 | + if (strpos($selectString, 'distinct ') === 0) { |
| 175 | + $selectString = substr($selectString, 9); |
| 176 | + } |
| 177 | + // if field itself or field with alias |
| 178 | + if ($selectString === $field || |
| 179 | + ( |
| 180 | + strpos($selectString, $field) === 0 && |
| 181 | + strpos(strtolower(ltrim(substr($selectString, strlen($field)))), 'as ') === 0 |
| 182 | + ) |
| 183 | + ) { |
| 184 | + $isSelected = true; |
| 185 | + break; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + if (!$isSelected) { |
| 190 | + $queryBuilder->addSelect($field); |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments