|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Tools\Pagination; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\Query; |
|
8
|
|
|
use Doctrine\ORM\Query\AST\SelectStatement; |
|
9
|
|
|
use Doctrine\ORM\Query\TreeWalkerAdapter; |
|
10
|
|
|
use function array_filter; |
|
11
|
|
|
use function array_key_exists; |
|
12
|
|
|
use function array_merge; |
|
13
|
|
|
use function array_unique; |
|
14
|
|
|
use function in_array; |
|
15
|
|
|
use function reset; |
|
16
|
|
|
|
|
17
|
|
|
class RemoveUselessLeftJoinsWalker extends TreeWalkerAdapter |
|
18
|
|
|
{ |
|
19
|
20 |
|
public function walkSelectStatement(SelectStatement $AST) |
|
20
|
|
|
{ |
|
21
|
20 |
|
$this->removeUnusedJoins($AST); |
|
22
|
20 |
|
} |
|
23
|
|
|
|
|
24
|
20 |
|
private function removeUnusedJoins(SelectStatement $AST) |
|
25
|
|
|
{ |
|
26
|
20 |
|
$from = $AST->fromClause->identificationVariableDeclarations; |
|
27
|
20 |
|
$fromRoot = reset($from); |
|
28
|
|
|
|
|
29
|
20 |
|
if (! isset($AST->whereClause) || ! isset($AST->whereClause->conditionalExpression) || ! isset($AST->whereClause->conditionalExpression->simpleConditionalExpression)) { |
|
30
|
14 |
|
return; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
6 |
|
$expr = $AST->whereClause->conditionalExpression->simpleConditionalExpression; |
|
34
|
|
|
|
|
35
|
6 |
|
if (! isset($expr->subselect)) { |
|
36
|
1 |
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
5 |
|
$subSelect = $expr->subselect; |
|
40
|
5 |
|
$subSelectUsages = $this->findSubSelectUsages($subSelect); |
|
41
|
|
|
|
|
42
|
5 |
|
foreach ($subSelect->subselectFromClause->identificationVariableDeclarations as $declaration) { |
|
43
|
5 |
|
$declaration->joins = $this->filterJoins($declaration->joins, $this->findUnusedJoins($declaration->joins, $subSelectUsages)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
5 |
|
$usages = $this->findSubSelectUsages($AST); |
|
47
|
5 |
|
$fromRoot->joins = $this->filterJoins($fromRoot->joins, $this->findUnusedJoins($fromRoot->joins, $usages)); |
|
48
|
5 |
|
} |
|
49
|
|
|
|
|
50
|
5 |
|
private function recursiveAddUsages($usages, $parents) |
|
51
|
|
|
{ |
|
52
|
5 |
|
foreach ($usages as $id) { |
|
53
|
5 |
|
if (array_key_exists($id, $parents) && ! in_array($parents[$id], $usages)) { |
|
54
|
4 |
|
$usages = $this->recursiveAddUsages(array_merge($usages, [$parents[$id]]), $parents); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
5 |
|
return $usages; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
5 |
|
private function findUnusedJoins($joins, $usages) |
|
62
|
|
|
{ |
|
63
|
5 |
|
$parents = []; |
|
64
|
5 |
|
foreach ($joins as $join) { |
|
65
|
5 |
|
$parents[$join->joinAssociationDeclaration->aliasIdentificationVariable] = $join->joinAssociationDeclaration->joinAssociationPathExpression->identificationVariable; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
5 |
|
$usages = $this->recursiveAddUsages($usages, $parents); |
|
69
|
|
|
|
|
70
|
5 |
|
$unused = []; |
|
71
|
5 |
|
foreach ($joins as $join) { |
|
72
|
5 |
|
if ($join->joinType === Query\AST\Join::JOIN_TYPE_LEFT && ! in_array($join->joinAssociationDeclaration->aliasIdentificationVariable, $usages)) { |
|
73
|
5 |
|
$unused[] = $join; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
5 |
|
return array_unique($unused); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
5 |
|
private function filterJoins($joins, $toRemove) |
|
81
|
|
|
{ |
|
82
|
|
|
return array_filter($joins, static function (Query\AST\Join $join) use ($toRemove) { |
|
83
|
5 |
|
return ! in_array($join, $toRemove); |
|
84
|
5 |
|
}); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
5 |
|
private function extractIdentificationVariable($expression) |
|
88
|
|
|
{ |
|
89
|
5 |
|
if ($expression->simpleArithmeticExpression instanceof Query\AST\Literal) { |
|
90
|
3 |
|
return null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
5 |
|
return $expression->simpleArithmeticExpression->identificationVariable; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
5 |
|
private function extractIdentificationVariableFromExpression($expr) |
|
97
|
|
|
{ |
|
98
|
5 |
|
$usages = []; |
|
99
|
|
|
|
|
100
|
5 |
|
if ($expr instanceof Query\AST\ComparisonExpression) { |
|
101
|
3 |
|
$usages[] = $this->extractIdentificationVariable($expr->leftExpression); |
|
102
|
3 |
|
$usages[] = $this->extractIdentificationVariable($expr->rightExpression); |
|
103
|
5 |
|
} elseif ($expr instanceof Query\AST\PathExpression) { |
|
104
|
1 |
|
$usages[] = $expr->identificationVariable; |
|
105
|
5 |
|
} elseif (isset($expr->expression)) { |
|
106
|
5 |
|
$usages[] = $this->extractIdentificationVariable($expr->expression); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
5 |
|
return $usages; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
5 |
|
private function findSubSelectUsages($select) |
|
113
|
|
|
{ |
|
114
|
5 |
|
$usages = []; |
|
115
|
|
|
|
|
116
|
5 |
|
if (isset($select->whereClause)) { |
|
117
|
5 |
|
$usages = array_merge($usages, $this->extractFromConditionalExpression($select->whereClause->conditionalExpression)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
5 |
|
if (isset($select->orderByClause)) { |
|
121
|
1 |
|
foreach ($select->orderByClause->orderByItems as $item) { |
|
122
|
1 |
|
$usages = array_merge($usages, $this->extractIdentificationVariableFromExpression($item->expression)); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
5 |
|
if (isset($select->havingClause)) { |
|
127
|
1 |
|
$usages = array_merge($usages, $this->extractFromConditionalExpression($select->havingClause->conditionalExpression)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
5 |
|
$usages = array_unique($usages); |
|
131
|
|
|
|
|
132
|
5 |
|
return $usages; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
5 |
|
protected function extractFromConditionalExpression($expression) |
|
136
|
|
|
{ |
|
137
|
5 |
|
$usages = []; |
|
138
|
|
|
|
|
139
|
5 |
|
if ($expression instanceof Query\AST\ConditionalTerm) { |
|
140
|
1 |
|
foreach ($expression->conditionalFactors as $factor) { |
|
141
|
1 |
|
$expr = $factor->simpleConditionalExpression; |
|
142
|
1 |
|
$usages = array_merge($usages, $this->extractIdentificationVariableFromExpression($expr)); |
|
143
|
|
|
} |
|
144
|
5 |
|
} elseif ($expression instanceof Query\AST\ConditionalPrimary) { |
|
145
|
5 |
|
$usages = array_merge($usages, $this->extractIdentificationVariableFromExpression($expression->simpleConditionalExpression)); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
5 |
|
return $usages; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|