1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mouf\Database\TDBM\QueryFactory; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\Schema; |
6
|
|
|
use Mouf\Database\TDBM\OrderByAnalyzer; |
7
|
|
|
use Mouf\Database\TDBM\TDBMService; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This class is in charge of creating the MagicQuery SQL based on parameters passed to findObjects method. |
11
|
|
|
*/ |
12
|
|
|
class FindObjectsQueryFactory extends AbstractQueryFactory |
13
|
|
|
{ |
14
|
|
|
private $mainTable; |
15
|
|
|
private $additionalTablesFetch; |
16
|
|
|
private $filterString; |
17
|
|
|
|
18
|
|
|
public function __construct(string $mainTable, array $additionalTablesFetch, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer) |
19
|
|
|
{ |
20
|
|
|
parent::__construct($tdbmService, $schema, $orderByAnalyzer, $orderBy); |
21
|
|
|
$this->mainTable = $mainTable; |
22
|
|
|
$this->additionalTablesFetch = $additionalTablesFetch; |
23
|
|
|
$this->filterString = $filterString; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function compute() |
27
|
|
|
{ |
28
|
|
|
list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, $this->additionalTablesFetch, $this->orderBy); |
29
|
|
|
|
30
|
|
|
$sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM MAGICJOIN('.$this->mainTable.')'; |
31
|
|
|
|
32
|
|
|
$pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns(); |
33
|
|
View Code Duplication |
$pkColumnNames = array_map(function ($pkColumn) { |
|
|
|
|
34
|
|
|
return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn); |
35
|
|
|
}, $pkColumnNames); |
36
|
|
|
|
37
|
|
|
$countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM MAGICJOIN('.$this->mainTable.')'; |
38
|
|
|
|
39
|
|
View Code Duplication |
if (!empty($this->filterString)) { |
|
|
|
|
40
|
|
|
$sql .= ' WHERE '.$this->filterString; |
41
|
|
|
$countSql .= ' WHERE '.$this->filterString; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (!empty($orderString)) { |
45
|
|
|
$sql .= ' ORDER BY '.$orderString; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->magicSql = $sql; |
49
|
|
|
$this->magicSqlCount = $countSql; |
50
|
|
|
$this->columnDescList = $columnDescList; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.