|
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\Persisters; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\Common\Collections\Expr\ExpressionVisitor; |
|
23
|
|
|
use Doctrine\Common\Collections\Expr\Comparison; |
|
24
|
|
|
use Doctrine\Common\Collections\Expr\Value; |
|
25
|
|
|
use Doctrine\Common\Collections\Expr\CompositeExpression; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Extract the values from a criteria/expression |
|
29
|
|
|
* |
|
30
|
|
|
* @author Benjamin Eberlei <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
class SqlValueVisitor extends ExpressionVisitor |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
private $values = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private $types = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Converts a comparison expression into the target query language output. |
|
46
|
|
|
* |
|
47
|
|
|
* @param \Doctrine\Common\Collections\Expr\Comparison $comparison |
|
48
|
|
|
* |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
67 |
|
public function walkComparison(Comparison $comparison) |
|
52
|
|
|
{ |
|
53
|
67 |
|
$value = $this->getValueFromComparison($comparison); |
|
54
|
67 |
|
$field = $comparison->getField(); |
|
55
|
67 |
|
$operator = $comparison->getOperator(); |
|
56
|
|
|
|
|
57
|
67 |
|
if (($operator === Comparison::EQ || $operator === Comparison::IS) && $value === null) { |
|
58
|
3 |
|
return; |
|
59
|
64 |
|
} else if ($operator === Comparison::NEQ && $value === null) { |
|
60
|
1 |
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
63 |
|
$this->values[] = $value; |
|
64
|
63 |
|
$this->types[] = [$field, $value, $operator]; |
|
65
|
63 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Converts a composite expression into the target query language output. |
|
69
|
|
|
* |
|
70
|
|
|
* @param \Doctrine\Common\Collections\Expr\CompositeExpression $expr |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
9 |
|
public function walkCompositeExpression(CompositeExpression $expr) |
|
75
|
|
|
{ |
|
76
|
9 |
|
foreach ($expr->getExpressionList() as $child) { |
|
77
|
9 |
|
$this->dispatch($child); |
|
78
|
|
|
} |
|
79
|
9 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Converts a value expression into the target query language part. |
|
83
|
|
|
* |
|
84
|
|
|
* @param \Doctrine\Common\Collections\Expr\Value $value |
|
85
|
|
|
* |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
|
|
public function walkValue(Value $value) |
|
89
|
|
|
{ |
|
90
|
|
|
return; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns the Parameters and Types necessary for matching the last visited expression. |
|
95
|
|
|
* |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
67 |
|
public function getParamsAndTypes() |
|
99
|
|
|
{ |
|
100
|
67 |
|
return [$this->values, $this->types]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Returns the value from a Comparison. In case of a CONTAINS comparison, |
|
105
|
|
|
* the value is wrapped in %-signs, because it will be used in a LIKE clause. |
|
106
|
|
|
* |
|
107
|
|
|
* @param \Doctrine\Common\Collections\Expr\Comparison $comparison |
|
108
|
|
|
* @return mixed |
|
109
|
|
|
*/ |
|
110
|
67 |
|
protected function getValueFromComparison(Comparison $comparison) |
|
111
|
|
|
{ |
|
112
|
67 |
|
$value = $comparison->getValue()->getValue(); |
|
113
|
|
|
|
|
114
|
67 |
|
switch ($comparison->getOperator()) { |
|
115
|
|
|
case Comparison::CONTAINS: |
|
116
|
2 |
|
return "%{$value}%"; |
|
117
|
|
|
|
|
118
|
|
|
case Comparison::STARTS_WITH: |
|
119
|
1 |
|
return "{$value}%"; |
|
120
|
|
|
|
|
121
|
|
|
case Comparison::ENDS_WITH: |
|
122
|
1 |
|
return "%{$value}"; |
|
123
|
|
|
|
|
124
|
|
|
default: |
|
125
|
64 |
|
return $value; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|