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 RuntimeException; |
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
|
|
|
/** @var BasicEntityPersister */ |
25
|
|
|
private $persister; |
26
|
|
|
|
27
|
|
|
/** @var ClassMetadata */ |
28
|
|
|
private $classMetadata; |
29
|
|
|
|
30
|
38 |
|
public function __construct(BasicEntityPersister $persister, ClassMetadata $classMetadata) |
31
|
|
|
{ |
32
|
38 |
|
$this->persister = $persister; |
33
|
38 |
|
$this->classMetadata = $classMetadata; |
34
|
38 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Converts a comparison expression into the target query language output. |
38
|
|
|
* |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
38 |
|
public function walkComparison(Comparison $comparison) |
42
|
|
|
{ |
43
|
38 |
|
$field = $comparison->getField(); |
44
|
38 |
|
$value = $comparison->getValue()->getValue(); // shortcut for walkValue() |
45
|
38 |
|
$property = $this->classMetadata->getProperty($field); |
46
|
|
|
|
47
|
38 |
|
if ($property instanceof AssociationMetadata && |
48
|
38 |
|
$value !== null && |
49
|
38 |
|
! is_object($value) && |
50
|
38 |
|
! in_array($comparison->getOperator(), [Comparison::IN, Comparison::NIN], true)) { |
51
|
1 |
|
throw MatchingAssociationFieldRequiresObject::fromClassAndAssociation( |
52
|
1 |
|
$this->classMetadata->getClassName(), |
53
|
|
|
$field |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
37 |
|
return $this->persister->getSelectConditionStatementSQL($field, $value, null, $comparison->getOperator()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Converts a composite expression into the target query language output. |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
* |
65
|
|
|
* @throws RuntimeException |
66
|
|
|
*/ |
67
|
4 |
|
public function walkCompositeExpression(CompositeExpression $expr) |
68
|
|
|
{ |
69
|
4 |
|
$expressionList = []; |
70
|
|
|
|
71
|
4 |
|
foreach ($expr->getExpressionList() as $child) { |
72
|
4 |
|
$expressionList[] = $this->dispatch($child); |
73
|
|
|
} |
74
|
|
|
|
75
|
4 |
|
switch ($expr->getType()) { |
76
|
4 |
|
case CompositeExpression::TYPE_AND: |
77
|
4 |
|
return '(' . implode(' AND ', $expressionList) . ')'; |
78
|
|
|
case CompositeExpression::TYPE_OR: |
79
|
|
|
return '(' . implode(' OR ', $expressionList) . ')'; |
80
|
|
|
default: |
81
|
|
|
throw new RuntimeException('Unknown composite ' . $expr->getType()); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Converts a value expression into the target query language part. |
87
|
|
|
* |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
public function walkValue(Value $value) |
91
|
|
|
{ |
92
|
|
|
return '?'; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|