|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\ORM\Query; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
23
|
|
|
|
|
24
|
|
|
use Doctrine\Common\Collections\Expr\ExpressionVisitor; |
|
25
|
|
|
use Doctrine\Common\Collections\Expr\Comparison; |
|
26
|
|
|
use Doctrine\Common\Collections\Expr\CompositeExpression; |
|
27
|
|
|
use Doctrine\Common\Collections\Expr\Value; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Converts Collection expressions to Query expressions. |
|
31
|
|
|
* |
|
32
|
|
|
* @author Kirill chEbba Chebunin <[email protected]> |
|
33
|
|
|
* @since 2.4 |
|
34
|
|
|
*/ |
|
35
|
|
|
class QueryExpressionVisitor extends ExpressionVisitor |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private static $operatorMap = [ |
|
41
|
|
|
Comparison::GT => Expr\Comparison::GT, |
|
42
|
|
|
Comparison::GTE => Expr\Comparison::GTE, |
|
43
|
|
|
Comparison::LT => Expr\Comparison::LT, |
|
44
|
|
|
Comparison::LTE => Expr\Comparison::LTE |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
private $queryAliases; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var Expr |
|
54
|
|
|
*/ |
|
55
|
|
|
private $expr; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
private $parameters = []; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Constructor |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $queryAliases |
|
66
|
|
|
*/ |
|
67
|
36 |
|
public function __construct($queryAliases) |
|
68
|
|
|
{ |
|
69
|
36 |
|
$this->queryAliases = $queryAliases; |
|
70
|
36 |
|
$this->expr = new Expr(); |
|
71
|
36 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Gets bound parameters. |
|
75
|
|
|
* Filled after {@link dispach()}. |
|
76
|
|
|
* |
|
77
|
|
|
* @return \Doctrine\Common\Collections\Collection |
|
78
|
|
|
*/ |
|
79
|
25 |
|
public function getParameters() |
|
80
|
|
|
{ |
|
81
|
25 |
|
return new ArrayCollection($this->parameters); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Clears parameters. |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function clearParameters() |
|
90
|
|
|
{ |
|
91
|
1 |
|
$this->parameters = []; |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Converts Criteria expression to Query one based on static map. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $criteriaOperator |
|
98
|
|
|
* |
|
99
|
|
|
* @return string|null |
|
100
|
|
|
*/ |
|
101
|
11 |
|
private static function convertComparisonOperator($criteriaOperator) |
|
102
|
|
|
{ |
|
103
|
11 |
|
return isset(self::$operatorMap[$criteriaOperator]) ? self::$operatorMap[$criteriaOperator] : null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritDoc} |
|
108
|
|
|
*/ |
|
109
|
10 |
|
public function walkCompositeExpression(CompositeExpression $expr) |
|
110
|
|
|
{ |
|
111
|
10 |
|
$expressionList = []; |
|
112
|
|
|
|
|
113
|
10 |
|
foreach ($expr->getExpressionList() as $child) { |
|
114
|
10 |
|
$expressionList[] = $this->dispatch($child); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
10 |
|
switch($expr->getType()) { |
|
118
|
10 |
|
case CompositeExpression::TYPE_AND: |
|
119
|
9 |
|
return new Expr\Andx($expressionList); |
|
120
|
|
|
|
|
121
|
1 |
|
case CompositeExpression::TYPE_OR: |
|
122
|
1 |
|
return new Expr\Orx($expressionList); |
|
123
|
|
|
|
|
124
|
|
|
default: |
|
125
|
|
|
throw new \RuntimeException("Unknown composite " . $expr->getType()); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritDoc} |
|
131
|
|
|
*/ |
|
132
|
30 |
|
public function walkComparison(Comparison $comparison) |
|
133
|
|
|
{ |
|
134
|
|
|
|
|
135
|
30 |
|
if ( ! isset($this->queryAliases[0])) { |
|
136
|
|
|
throw new QueryException('No aliases are set before invoking walkComparison().'); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
30 |
|
$field = $this->queryAliases[0] . '.' . $comparison->getField(); |
|
140
|
|
|
|
|
141
|
30 |
|
foreach($this->queryAliases as $alias) { |
|
142
|
30 |
|
if(strpos($comparison->getField() . '.', $alias . '.') === 0) { |
|
143
|
5 |
|
$field = $comparison->getField(); |
|
144
|
30 |
|
break; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
30 |
|
$parameterName = str_replace('.', '_', $comparison->getField()); |
|
149
|
|
|
|
|
150
|
30 |
|
foreach ($this->parameters as $parameter) { |
|
151
|
10 |
|
if ($parameter->getName() === $parameterName) { |
|
152
|
4 |
|
$parameterName .= '_' . count($this->parameters); |
|
153
|
10 |
|
break; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
30 |
|
$parameter = new Parameter($parameterName, $this->walkValue($comparison->getValue())); |
|
158
|
30 |
|
$placeholder = ':' . $parameterName; |
|
159
|
|
|
|
|
160
|
30 |
|
switch ($comparison->getOperator()) { |
|
161
|
30 |
|
case Comparison::IN: |
|
162
|
1 |
|
$this->parameters[] = $parameter; |
|
163
|
|
|
|
|
164
|
1 |
|
return $this->expr->in($field, $placeholder); |
|
165
|
29 |
|
case Comparison::NIN: |
|
166
|
1 |
|
$this->parameters[] = $parameter; |
|
167
|
|
|
|
|
168
|
1 |
|
return $this->expr->notIn($field, $placeholder); |
|
169
|
28 |
|
case Comparison::EQ: |
|
170
|
18 |
|
case Comparison::IS: |
|
171
|
17 |
|
if ($this->walkValue($comparison->getValue()) === null) { |
|
172
|
2 |
|
return $this->expr->isNull($field); |
|
173
|
|
|
} |
|
174
|
15 |
|
$this->parameters[] = $parameter; |
|
175
|
|
|
|
|
176
|
15 |
|
return $this->expr->eq($field, $placeholder); |
|
177
|
18 |
|
case Comparison::NEQ: |
|
178
|
2 |
|
if ($this->walkValue($comparison->getValue()) === null) { |
|
179
|
1 |
|
return $this->expr->isNotNull($field); |
|
180
|
|
|
} |
|
181
|
1 |
|
$this->parameters[] = $parameter; |
|
182
|
|
|
|
|
183
|
1 |
|
return $this->expr->neq($field, $placeholder); |
|
184
|
16 |
|
case Comparison::CONTAINS: |
|
185
|
1 |
|
$parameter->setValue('%' . $parameter->getValue() . '%', $parameter->getType()); |
|
186
|
1 |
|
$this->parameters[] = $parameter; |
|
187
|
|
|
|
|
188
|
1 |
|
return $this->expr->like($field, $placeholder); |
|
189
|
15 |
|
case Comparison::MEMBER_OF: |
|
190
|
2 |
|
return $this->expr->isMemberOf($comparison->getField(), $comparison->getValue()->getValue()); |
|
191
|
13 |
|
case Comparison::STARTS_WITH: |
|
192
|
1 |
|
$parameter->setValue($parameter->getValue() . '%', $parameter->getType()); |
|
193
|
1 |
|
$this->parameters[] = $parameter; |
|
194
|
|
|
|
|
195
|
1 |
|
return $this->expr->like($field, $placeholder); |
|
196
|
12 |
|
case Comparison::ENDS_WITH: |
|
197
|
1 |
|
$parameter->setValue('%' . $parameter->getValue(), $parameter->getType()); |
|
198
|
1 |
|
$this->parameters[] = $parameter; |
|
199
|
|
|
|
|
200
|
1 |
|
return $this->expr->like($field, $placeholder); |
|
201
|
|
|
default: |
|
202
|
11 |
|
$operator = self::convertComparisonOperator($comparison->getOperator()); |
|
203
|
11 |
|
if ($operator) { |
|
204
|
11 |
|
$this->parameters[] = $parameter; |
|
205
|
|
|
|
|
206
|
11 |
|
return new Expr\Comparison( |
|
207
|
11 |
|
$field, |
|
208
|
11 |
|
$operator, |
|
209
|
11 |
|
$placeholder |
|
210
|
|
|
); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
throw new \RuntimeException("Unknown comparison operator: " . $comparison->getOperator()); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* {@inheritDoc} |
|
219
|
|
|
*/ |
|
220
|
31 |
|
public function walkValue(Value $value) |
|
221
|
|
|
{ |
|
222
|
31 |
|
return $value->getValue(); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|