1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Persisters; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\Expr\Comparison; |
8
|
|
|
use Doctrine\Common\Collections\Expr\CompositeExpression; |
9
|
|
|
use Doctrine\Common\Collections\Expr\ExpressionVisitor; |
10
|
|
|
use Doctrine\Common\Collections\Expr\Value; |
11
|
|
|
use Doctrine\ORM\Mapping\AssociationMetadata; |
12
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
13
|
|
|
use Doctrine\ORM\Persisters\Entity\BasicEntityPersister; |
14
|
|
|
use Doctrine\ORM\Persisters\MatchingAssociationFieldRequiresObject; |
15
|
|
|
use function implode; |
16
|
|
|
use function in_array; |
17
|
|
|
use function is_object; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Visit Expressions and generate SQL WHERE conditions from them. |
21
|
|
|
*/ |
22
|
|
|
class SqlExpressionVisitor extends ExpressionVisitor |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var BasicEntityPersister |
26
|
|
|
*/ |
27
|
|
|
private $persister; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ClassMetadata |
31
|
|
|
*/ |
32
|
|
|
private $classMetadata; |
33
|
|
|
|
34
|
38 |
|
public function __construct(BasicEntityPersister $persister, ClassMetadata $classMetadata) |
35
|
|
|
{ |
36
|
38 |
|
$this->persister = $persister; |
37
|
38 |
|
$this->classMetadata = $classMetadata; |
38
|
38 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Converts a comparison expression into the target query language output. |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
38 |
|
public function walkComparison(Comparison $comparison) |
46
|
|
|
{ |
47
|
38 |
|
$field = $comparison->getField(); |
48
|
38 |
|
$value = $comparison->getValue()->getValue(); // shortcut for walkValue() |
|
|
|
|
49
|
38 |
|
$property = $this->classMetadata->getProperty($field); |
50
|
|
|
|
51
|
38 |
|
if ($property instanceof AssociationMetadata && |
52
|
38 |
|
$value !== null && |
53
|
38 |
|
! is_object($value) && |
54
|
38 |
|
! in_array($comparison->getOperator(), [Comparison::IN, Comparison::NIN], true)) { |
55
|
1 |
|
throw MatchingAssociationFieldRequiresObject::fromClassAndAssociation( |
56
|
1 |
|
$this->classMetadata->getClassName(), |
57
|
1 |
|
$field |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
37 |
|
return $this->persister->getSelectConditionStatementSQL($field, $value, null, $comparison->getOperator()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Converts a composite expression into the target query language output. |
66
|
|
|
* |
67
|
|
|
* @return mixed |
68
|
|
|
* |
69
|
|
|
* @throws \RuntimeException |
70
|
|
|
*/ |
71
|
4 |
|
public function walkCompositeExpression(CompositeExpression $expr) |
72
|
|
|
{ |
73
|
4 |
|
$expressionList = []; |
74
|
|
|
|
75
|
4 |
|
foreach ($expr->getExpressionList() as $child) { |
76
|
4 |
|
$expressionList[] = $this->dispatch($child); |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
switch ($expr->getType()) { |
80
|
4 |
|
case CompositeExpression::TYPE_AND: |
81
|
4 |
|
return '(' . implode(' AND ', $expressionList) . ')'; |
82
|
|
|
|
83
|
|
|
case CompositeExpression::TYPE_OR: |
84
|
|
|
return '(' . implode(' OR ', $expressionList) . ')'; |
85
|
|
|
|
86
|
|
|
default: |
87
|
|
|
throw new \RuntimeException('Unknown composite ' . $expr->getType()); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Converts a value expression into the target query language part. |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function walkValue(Value $value) |
97
|
|
|
{ |
98
|
|
|
return '?'; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.