1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SQLParser\Node; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Mouf\MoufManager; |
7
|
|
|
use Mouf\MoufInstanceDescriptor; |
8
|
|
|
use SQLParser\Node\Traverser\NodeTraverser; |
9
|
|
|
use SQLParser\Node\Traverser\VisitorInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This class represents a BETWEEN operation. |
13
|
|
|
* |
14
|
|
|
* @author David Négrier <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Between implements NodeInterface |
17
|
|
|
{ |
18
|
|
|
private $leftOperand; |
19
|
|
|
|
20
|
|
|
public function getLeftOperand() |
21
|
|
|
{ |
22
|
|
|
return $this->leftOperand; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Sets the leftOperand. |
27
|
|
|
* |
28
|
|
|
* @Important |
29
|
|
|
* |
30
|
|
|
* @param NodeInterface|NodeInterface[]|string $leftOperand |
31
|
|
|
*/ |
32
|
|
|
public function setLeftOperand($leftOperand) |
33
|
|
|
{ |
34
|
|
|
$this->leftOperand = $leftOperand; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string|NodeInterface|NodeInterface[] |
39
|
|
|
*/ |
40
|
|
|
private $minValueOperand; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string|NodeInterface|NodeInterface[] |
44
|
|
|
*/ |
45
|
|
|
private $maxValueOperand; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return NodeInterface|NodeInterface[]|string |
49
|
|
|
*/ |
50
|
|
|
public function getMinValueOperand() |
51
|
|
|
{ |
52
|
|
|
return $this->minValueOperand; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param NodeInterface|NodeInterface[]|string $minValueOperand |
57
|
|
|
*/ |
58
|
|
|
public function setMinValueOperand($minValueOperand) |
59
|
|
|
{ |
60
|
|
|
$this->minValueOperand = $minValueOperand; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return NodeInterface|NodeInterface[]|string |
65
|
|
|
*/ |
66
|
|
|
public function getMaxValueOperand() |
67
|
|
|
{ |
68
|
|
|
return $this->maxValueOperand; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param NodeInterface|NodeInterface[]|string $maxValueOperand |
73
|
|
|
*/ |
74
|
|
|
public function setMaxValueOperand($maxValueOperand) |
75
|
|
|
{ |
76
|
|
|
$this->maxValueOperand = $maxValueOperand; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var ConditionInterface |
81
|
|
|
*/ |
82
|
|
|
protected $minValueCondition; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets the condition. |
86
|
|
|
* |
87
|
|
|
* @Important IfSet |
88
|
|
|
* |
89
|
|
|
* @param ConditionInterface $minValueCondition |
90
|
|
|
*/ |
91
|
|
|
public function setMinValueCondition(ConditionInterface $minValueCondition = null) |
92
|
|
|
{ |
93
|
|
|
$this->minValueCondition = $minValueCondition; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var ConditionInterface |
98
|
|
|
*/ |
99
|
|
|
protected $maxValueCondition; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Sets the condition. |
103
|
|
|
* |
104
|
|
|
* @Important IfSet |
105
|
|
|
* |
106
|
|
|
* @param ConditionInterface $maxValueCondition |
107
|
|
|
*/ |
108
|
|
|
public function setMaxValueCondition(ConditionInterface $maxValueCondition = null) |
109
|
|
|
{ |
110
|
|
|
$this->maxValueCondition = $maxValueCondition; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns a Mouf instance descriptor describing this object. |
115
|
|
|
* |
116
|
|
|
* @param MoufManager $moufManager |
117
|
|
|
* |
118
|
|
|
* @return MoufInstanceDescriptor |
119
|
|
|
*/ |
120
|
|
|
public function toInstanceDescriptor(MoufManager $moufManager) |
121
|
|
|
{ |
122
|
|
|
$instanceDescriptor = $moufManager->createInstance(get_called_class()); |
123
|
|
|
$instanceDescriptor->getProperty('leftOperand')->setValue(NodeFactory::nodeToInstanceDescriptor($this->leftOperand, $moufManager)); |
124
|
|
|
$instanceDescriptor->getProperty('minValueOperand')->setValue(NodeFactory::nodeToInstanceDescriptor($this->minValueOperand, $moufManager)); |
125
|
|
|
$instanceDescriptor->getProperty('maxValueOperand')->setValue(NodeFactory::nodeToInstanceDescriptor($this->maxValueOperand, $moufManager)); |
126
|
|
|
|
127
|
|
View Code Duplication |
if ($this->minValueOperand instanceof Parameter) { |
|
|
|
|
128
|
|
|
// Let's add a condition on the parameter. |
129
|
|
|
$conditionDescriptor = $moufManager->createInstance('Mouf\\Database\\QueryWriter\\Condition\\ParamAvailableCondition'); |
130
|
|
|
$conditionDescriptor->getProperty('parameterName')->setValue($this->minValueOperand->getName()); |
131
|
|
|
$instanceDescriptor->getProperty('minValueCondition')->setValue($conditionDescriptor); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
if ($this->maxValueOperand instanceof Parameter) { |
|
|
|
|
135
|
|
|
// Let's add a condition on the parameter. |
136
|
|
|
$conditionDescriptor = $moufManager->createInstance('Mouf\\Database\\QueryWriter\\Condition\\ParamAvailableCondition'); |
137
|
|
|
$conditionDescriptor->getProperty('parameterName')->setValue($this->maxValueOperand->getName()); |
138
|
|
|
$instanceDescriptor->getProperty('maxValueCondition')->setValue($conditionDescriptor); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $instanceDescriptor; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Renders the object as a SQL string. |
146
|
|
|
* |
147
|
|
|
* @param Connection $dbConnection |
148
|
|
|
* @param array $parameters |
149
|
|
|
* @param number $indent |
150
|
|
|
* @param int $conditionsMode |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY) |
155
|
|
|
{ |
156
|
|
|
$minBypass = false; |
157
|
|
|
$maxBypass = false; |
158
|
|
|
|
159
|
|
|
if ($conditionsMode == self::CONDITION_GUESS) { |
160
|
|
View Code Duplication |
if ($this->minValueOperand instanceof Parameter) { |
|
|
|
|
161
|
|
|
if ($this->minValueOperand->isDiscardedOnNull() && !isset($parameters[$this->minValueOperand->getName()])) { |
162
|
|
|
$minBypass = true; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
View Code Duplication |
if ($this->maxValueOperand instanceof Parameter) { |
|
|
|
|
167
|
|
|
if ($this->maxValueOperand->isDiscardedOnNull() && !isset($parameters[$this->maxValueOperand->getName()])) { |
168
|
|
|
$maxBypass = true; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} elseif ($conditionsMode == self::CONDITION_IGNORE) { |
172
|
|
|
$minBypass = false; |
173
|
|
|
$maxBypass = false; |
174
|
|
|
} else { |
175
|
|
|
if ($this->minValueCondition && !$this->minValueCondition->isOk($parameters)) { |
176
|
|
|
$minBypass = true; |
177
|
|
|
} |
178
|
|
|
if ($this->maxValueCondition && !$this->maxValueCondition->isOk($parameters)) { |
179
|
|
|
$maxBypass = true; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
if (!$minBypass && !$maxBypass) { |
184
|
|
|
$sql = NodeFactory::toSql($this->leftOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode); |
185
|
|
|
$sql .= ' BETWEEN '; |
186
|
|
|
$sql .= NodeFactory::toSql($this->minValueOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode); |
187
|
|
|
$sql .= ' AND '; |
188
|
|
|
$sql .= NodeFactory::toSql($this->maxValueOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode); |
189
|
|
|
} elseif (!$minBypass && $maxBypass) { |
190
|
|
|
$sql = NodeFactory::toSql($this->leftOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode); |
191
|
|
|
$sql .= ' >= '; |
192
|
|
|
$sql .= NodeFactory::toSql($this->minValueOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode); |
193
|
|
|
} elseif ($minBypass && !$maxBypass) { |
194
|
|
|
$sql = NodeFactory::toSql($this->leftOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode); |
195
|
|
|
$sql .= ' <= '; |
196
|
|
|
$sql .= NodeFactory::toSql($this->maxValueOperand, $dbConnection, $parameters, ' ', false, $indent, $conditionsMode); |
197
|
|
|
} else { |
198
|
|
|
$sql = null; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $sql; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Walks the tree of nodes, calling the visitor passed in parameter. |
206
|
|
|
* |
207
|
|
|
* @param VisitorInterface $visitor |
208
|
|
|
*/ |
209
|
|
|
public function walk(VisitorInterface $visitor) |
210
|
|
|
{ |
211
|
|
|
$node = $this; |
212
|
|
|
$result = $visitor->enterNode($node); |
213
|
|
|
if ($result instanceof NodeInterface) { |
214
|
|
|
$node = $result; |
215
|
|
|
} |
216
|
|
|
if ($result !== NodeTraverser::DONT_TRAVERSE_CHILDREN) { |
217
|
|
|
$result2 = $this->leftOperand->walk($visitor); |
218
|
|
View Code Duplication |
if ($result2 === NodeTraverser::REMOVE_NODE) { |
|
|
|
|
219
|
|
|
return NodeTraverser::REMOVE_NODE; |
220
|
|
|
} elseif ($result2 instanceof NodeInterface) { |
221
|
|
|
$this->leftOperand = $result2; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$result2 = $this->minValueOperand->walk($visitor); |
225
|
|
|
if ($result2 === NodeTraverser::REMOVE_NODE) { |
226
|
|
|
return NodeTraverser::REMOVE_NODE; |
227
|
|
|
} elseif ($result2 instanceof NodeInterface) { |
228
|
|
|
$this->minValueOperand = $result2; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$result2 = $this->maxValueOperand->walk($visitor); |
232
|
|
|
if ($result2 === NodeTraverser::REMOVE_NODE) { |
233
|
|
|
return NodeTraverser::REMOVE_NODE; |
234
|
|
|
} elseif ($result2 instanceof NodeInterface) { |
235
|
|
|
$this->maxValueOperand = $result2; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $visitor->leaveNode($node); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.